2 * Packet matching code for ARP packets.
4 * Based heavily, if not almost entirely, upon ip_tables.c framework.
6 * Some ARP specific bits are:
8 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/skbuff.h>
15 #include <linux/netdevice.h>
16 #include <linux/if_arp.h>
17 #include <linux/kmod.h>
18 #include <linux/vmalloc.h>
19 #include <linux/proc_fs.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
23 #include <asm/uaccess.h>
24 #include <asm/semaphore.h>
26 #include <linux/netfilter_arp/arp_tables.h>
28 MODULE_LICENSE("GPL");
29 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
30 MODULE_DESCRIPTION("arptables core");
32 /*#define DEBUG_ARP_TABLES*/
33 /*#define DEBUG_ARP_TABLES_USER*/
35 #ifdef DEBUG_ARP_TABLES
36 #define dprintf(format, args...) printk(format , ## args)
38 #define dprintf(format, args...)
41 #ifdef DEBUG_ARP_TABLES_USER
42 #define duprintf(format, args...) printk(format , ## args)
44 #define duprintf(format, args...)
47 #ifdef CONFIG_NETFILTER_DEBUG
48 #define ARP_NF_ASSERT(x) \
51 printk("ARP_NF_ASSERT: %s:%s:%u\n", \
52 __FUNCTION__, __FILE__, __LINE__); \
55 #define ARP_NF_ASSERT(x)
57 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
59 static DECLARE_MUTEX(arpt_mutex);
61 #define ASSERT_READ_LOCK(x) ARP_NF_ASSERT(down_trylock(&arpt_mutex) != 0)
62 #define ASSERT_WRITE_LOCK(x) ARP_NF_ASSERT(down_trylock(&arpt_mutex) != 0)
63 #include <linux/netfilter_ipv4/listhelp.h>
65 struct arpt_table_info {
68 unsigned int initial_entries;
69 unsigned int hook_entry[NF_ARP_NUMHOOKS];
70 unsigned int underflow[NF_ARP_NUMHOOKS];
71 void *entries[NR_CPUS];
74 static LIST_HEAD(arpt_target);
75 static LIST_HEAD(arpt_tables);
76 #define SET_COUNTER(c,b,p) do { (c).bcnt = (b); (c).pcnt = (p); } while(0)
77 #define ADD_COUNTER(c,b,p) do { (c).bcnt += (b); (c).pcnt += (p); } while(0)
79 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
80 char *hdr_addr, int len)
84 if (len > ARPT_DEV_ADDR_LEN_MAX)
85 len = ARPT_DEV_ADDR_LEN_MAX;
88 for (i = 0; i < len; i++)
89 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
94 /* Returns whether packet matches rule or not. */
95 static inline int arp_packet_match(const struct arphdr *arphdr,
96 struct net_device *dev,
99 const struct arpt_arp *arpinfo)
101 char *arpptr = (char *)(arphdr + 1);
102 char *src_devaddr, *tgt_devaddr;
103 u32 src_ipaddr, tgt_ipaddr;
106 #define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
108 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
110 dprintf("ARP operation field mismatch.\n");
111 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
112 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
116 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
118 dprintf("ARP hardware address format mismatch.\n");
119 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
120 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
124 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
126 dprintf("ARP protocol address format mismatch.\n");
127 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
128 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
132 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
134 dprintf("ARP hardware address length mismatch.\n");
135 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
136 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
140 src_devaddr = arpptr;
141 arpptr += dev->addr_len;
142 memcpy(&src_ipaddr, arpptr, sizeof(u32));
143 arpptr += sizeof(u32);
144 tgt_devaddr = arpptr;
145 arpptr += dev->addr_len;
146 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
148 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
149 ARPT_INV_SRCDEVADDR) ||
150 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
151 ARPT_INV_TGTDEVADDR)) {
152 dprintf("Source or target device address mismatch.\n");
157 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
159 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
161 dprintf("Source or target IP address mismatch.\n");
163 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
165 NIPQUAD(arpinfo->smsk.s_addr),
166 NIPQUAD(arpinfo->src.s_addr),
167 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
168 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
170 NIPQUAD(arpinfo->tmsk.s_addr),
171 NIPQUAD(arpinfo->tgt.s_addr),
172 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
176 /* Look for ifname matches. */
177 for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
178 ret |= (indev[i] ^ arpinfo->iniface[i])
179 & arpinfo->iniface_mask[i];
182 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
183 dprintf("VIA in mismatch (%s vs %s).%s\n",
184 indev, arpinfo->iniface,
185 arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
189 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
191 memcpy(&odev, outdev + i*sizeof(unsigned long),
192 sizeof(unsigned long));
194 ^ ((const unsigned long *)arpinfo->outiface)[i])
195 & ((const unsigned long *)arpinfo->outiface_mask)[i];
198 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
199 dprintf("VIA out mismatch (%s vs %s).%s\n",
200 outdev, arpinfo->outiface,
201 arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
208 static inline int arp_checkentry(const struct arpt_arp *arp)
210 if (arp->flags & ~ARPT_F_MASK) {
211 duprintf("Unknown flag bits set: %08X\n",
212 arp->flags & ~ARPT_F_MASK);
215 if (arp->invflags & ~ARPT_INV_MASK) {
216 duprintf("Unknown invflag bits set: %08X\n",
217 arp->invflags & ~ARPT_INV_MASK);
224 static unsigned int arpt_error(struct sk_buff **pskb,
225 unsigned int hooknum,
226 const struct net_device *in,
227 const struct net_device *out,
228 const void *targinfo,
232 printk("arp_tables: error: '%s'\n", (char *)targinfo);
237 static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
239 return (struct arpt_entry *)(base + offset);
242 unsigned int arpt_do_table(struct sk_buff **pskb,
244 const struct net_device *in,
245 const struct net_device *out,
246 struct arpt_table *table,
249 static const char nulldevname[IFNAMSIZ];
250 unsigned int verdict = NF_DROP;
253 struct arpt_entry *e, *back;
254 const char *indev, *outdev;
257 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
258 if (!pskb_may_pull((*pskb), (sizeof(struct arphdr) +
259 (2 * (*pskb)->dev->addr_len) +
263 indev = in ? in->name : nulldevname;
264 outdev = out ? out->name : nulldevname;
266 read_lock_bh(&table->lock);
267 table_base = (void *)table->private->entries[smp_processor_id()];
268 e = get_entry(table_base, table->private->hook_entry[hook]);
269 back = get_entry(table_base, table->private->underflow[hook]);
271 arp = (*pskb)->nh.arph;
273 if (arp_packet_match(arp, (*pskb)->dev, indev, outdev, &e->arp)) {
274 struct arpt_entry_target *t;
277 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
278 (2 * (*pskb)->dev->addr_len);
279 ADD_COUNTER(e->counters, hdr_len, 1);
281 t = arpt_get_target(e);
283 /* Standard target? */
284 if (!t->u.kernel.target->target) {
287 v = ((struct arpt_standard_target *)t)->verdict;
289 /* Pop from stack? */
290 if (v != ARPT_RETURN) {
291 verdict = (unsigned)(-v) - 1;
295 back = get_entry(table_base,
300 != (void *)e + e->next_offset) {
301 /* Save old back ptr in next entry */
302 struct arpt_entry *next
303 = (void *)e + e->next_offset;
305 (void *)back - table_base;
307 /* set back pointer to next entry */
311 e = get_entry(table_base, v);
313 /* Targets which reenter must return
316 verdict = t->u.kernel.target->target(pskb,
322 /* Target might have changed stuff. */
323 arp = (*pskb)->nh.arph;
325 if (verdict == ARPT_CONTINUE)
326 e = (void *)e + e->next_offset;
332 e = (void *)e + e->next_offset;
335 read_unlock_bh(&table->lock);
344 * These are weird, but module loading must not be done with mutex
345 * held (since they will register), and we have to have a single
346 * function to use try_then_request_module().
349 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
350 static inline struct arpt_table *find_table_lock(const char *name)
352 struct arpt_table *t;
354 if (down_interruptible(&arpt_mutex) != 0)
355 return ERR_PTR(-EINTR);
357 list_for_each_entry(t, &arpt_tables, list)
358 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
365 /* Find target, grabs ref. Returns ERR_PTR() on error. */
366 static inline struct arpt_target *find_target(const char *name, u8 revision)
368 struct arpt_target *t;
371 if (down_interruptible(&arpt_mutex) != 0)
372 return ERR_PTR(-EINTR);
374 list_for_each_entry(t, &arpt_target, list) {
375 if (strcmp(t->name, name) == 0) {
376 if (t->revision == revision) {
377 if (try_module_get(t->me)) {
382 err = -EPROTOTYPE; /* Found something. */
389 struct arpt_target *arpt_find_target(const char *name, u8 revision)
391 struct arpt_target *target;
393 target = try_then_request_module(find_target(name, revision),
395 if (IS_ERR(target) || !target)
400 static int target_revfn(const char *name, u8 revision, int *bestp)
402 struct arpt_target *t;
405 list_for_each_entry(t, &arpt_target, list) {
406 if (strcmp(t->name, name) == 0) {
407 if (t->revision > *bestp)
408 *bestp = t->revision;
409 if (t->revision == revision)
416 /* Returns true or false (if no such extension at all) */
417 static inline int find_revision(const char *name, u8 revision,
418 int (*revfn)(const char *, u8, int *),
421 int have_rev, best = -1;
423 if (down_interruptible(&arpt_mutex) != 0) {
427 have_rev = revfn(name, revision, &best);
430 /* Nothing at all? Return 0 to try loading module. */
438 *err = -EPROTONOSUPPORT;
443 /* All zeroes == unconditional rule. */
444 static inline int unconditional(const struct arpt_arp *arp)
448 for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
449 if (((__u32 *)arp)[i])
455 /* Figures out from what hook each rule can be called: returns 0 if
456 * there are loops. Puts hook bitmask in comefrom.
458 static int mark_source_chains(struct arpt_table_info *newinfo,
459 unsigned int valid_hooks, void *entry0)
463 /* No recursion; use packet counter to save back ptrs (reset
464 * to 0 as we leave), and comefrom to save source hook bitmask.
466 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
467 unsigned int pos = newinfo->hook_entry[hook];
469 = (struct arpt_entry *)(entry0 + pos);
471 if (!(valid_hooks & (1 << hook)))
474 /* Set initial back pointer. */
475 e->counters.pcnt = pos;
478 struct arpt_standard_target *t
479 = (void *)arpt_get_target(e);
481 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
482 printk("arptables: loop hook %u pos %u %08X.\n",
483 hook, pos, e->comefrom);
487 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
489 /* Unconditional return/END. */
490 if (e->target_offset == sizeof(struct arpt_entry)
491 && (strcmp(t->target.u.user.name,
492 ARPT_STANDARD_TARGET) == 0)
494 && unconditional(&e->arp)) {
495 unsigned int oldpos, size;
497 /* Return: backtrack through the last
501 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
503 pos = e->counters.pcnt;
504 e->counters.pcnt = 0;
506 /* We're at the start. */
510 e = (struct arpt_entry *)
512 } while (oldpos == pos + e->next_offset);
515 size = e->next_offset;
516 e = (struct arpt_entry *)
517 (entry0 + pos + size);
518 e->counters.pcnt = pos;
521 int newpos = t->verdict;
523 if (strcmp(t->target.u.user.name,
524 ARPT_STANDARD_TARGET) == 0
526 /* This a jump; chase it. */
527 duprintf("Jump rule %u -> %u\n",
530 /* ... this is a fallthru */
531 newpos = pos + e->next_offset;
533 e = (struct arpt_entry *)
535 e->counters.pcnt = pos;
540 duprintf("Finished chain %u\n", hook);
545 static inline int standard_check(const struct arpt_entry_target *t,
546 unsigned int max_offset)
548 struct arpt_standard_target *targ = (void *)t;
550 /* Check standard info. */
552 != ARPT_ALIGN(sizeof(struct arpt_standard_target))) {
553 duprintf("arpt_standard_check: target size %u != %Zu\n",
555 ARPT_ALIGN(sizeof(struct arpt_standard_target)));
559 if (targ->verdict >= 0
560 && targ->verdict > max_offset - sizeof(struct arpt_entry)) {
561 duprintf("arpt_standard_check: bad verdict (%i)\n",
566 if (targ->verdict < -NF_MAX_VERDICT - 1) {
567 duprintf("arpt_standard_check: bad negative verdict (%i)\n",
574 static struct arpt_target arpt_standard_target;
576 static inline int check_entry(struct arpt_entry *e, const char *name, unsigned int size,
579 struct arpt_entry_target *t;
580 struct arpt_target *target;
583 if (!arp_checkentry(&e->arp)) {
584 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
588 t = arpt_get_target(e);
589 target = try_then_request_module(find_target(t->u.user.name,
591 "arpt_%s", t->u.user.name);
592 if (IS_ERR(target) || !target) {
593 duprintf("check_entry: `%s' not found\n", t->u.user.name);
594 ret = target ? PTR_ERR(target) : -ENOENT;
597 t->u.kernel.target = target;
599 if (t->u.kernel.target == &arpt_standard_target) {
600 if (!standard_check(t, size)) {
604 } else if (t->u.kernel.target->checkentry
605 && !t->u.kernel.target->checkentry(name, e, t->data,
609 module_put(t->u.kernel.target->me);
610 duprintf("arp_tables: check failed for `%s'.\n",
611 t->u.kernel.target->name);
623 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
624 struct arpt_table_info *newinfo,
626 unsigned char *limit,
627 const unsigned int *hook_entries,
628 const unsigned int *underflows,
633 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
634 || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
635 duprintf("Bad offset %p\n", e);
640 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
641 duprintf("checking: element %p size %u\n",
646 /* Check hooks & underflows */
647 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
648 if ((unsigned char *)e - base == hook_entries[h])
649 newinfo->hook_entry[h] = hook_entries[h];
650 if ((unsigned char *)e - base == underflows[h])
651 newinfo->underflow[h] = underflows[h];
654 /* FIXME: underflows must be unconditional, standard verdicts
655 < 0 (not ARPT_RETURN). --RR */
657 /* Clear counters and comefrom */
658 e->counters = ((struct arpt_counters) { 0, 0 });
665 static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
667 struct arpt_entry_target *t;
669 if (i && (*i)-- == 0)
672 t = arpt_get_target(e);
673 if (t->u.kernel.target->destroy)
674 t->u.kernel.target->destroy(t->data,
675 t->u.target_size - sizeof(*t));
676 module_put(t->u.kernel.target->me);
680 /* Checks and translates the user-supplied table segment (held in
683 static int translate_table(const char *name,
684 unsigned int valid_hooks,
685 struct arpt_table_info *newinfo,
689 const unsigned int *hook_entries,
690 const unsigned int *underflows)
695 newinfo->size = size;
696 newinfo->number = number;
698 /* Init all hooks to impossible value. */
699 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
700 newinfo->hook_entry[i] = 0xFFFFFFFF;
701 newinfo->underflow[i] = 0xFFFFFFFF;
704 duprintf("translate_table: size %u\n", newinfo->size);
707 /* Walk through entries, checking offsets. */
708 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
709 check_entry_size_and_hooks,
713 hook_entries, underflows, &i);
714 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
719 duprintf("translate_table: %u not %u entries\n",
724 /* Check hooks all assigned */
725 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
726 /* Only hooks which are valid */
727 if (!(valid_hooks & (1 << i)))
729 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
730 duprintf("Invalid hook entry %u %u\n",
734 if (newinfo->underflow[i] == 0xFFFFFFFF) {
735 duprintf("Invalid underflow %u %u\n",
741 if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
742 duprintf("Looping hook\n");
746 /* Finally, each sanity check must pass */
748 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
749 check_entry, name, size, &i);
752 ARPT_ENTRY_ITERATE(entry0, newinfo->size,
757 /* And one copy for every other CPU */
759 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
760 memcpy(newinfo->entries[i], entry0, newinfo->size);
766 static struct arpt_table_info *replace_table(struct arpt_table *table,
767 unsigned int num_counters,
768 struct arpt_table_info *newinfo,
771 struct arpt_table_info *oldinfo;
773 /* Do the substitution. */
774 write_lock_bh(&table->lock);
775 /* Check inside lock: is the old number correct? */
776 if (num_counters != table->private->number) {
777 duprintf("num_counters != table->private->number (%u/%u)\n",
778 num_counters, table->private->number);
779 write_unlock_bh(&table->lock);
783 oldinfo = table->private;
784 table->private = newinfo;
785 newinfo->initial_entries = oldinfo->initial_entries;
786 write_unlock_bh(&table->lock);
792 static inline int add_entry_to_counter(const struct arpt_entry *e,
793 struct arpt_counters total[],
796 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
802 static inline int set_entry_to_counter(const struct arpt_entry *e,
803 struct arpt_counters total[],
806 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
812 static void get_counters(const struct arpt_table_info *t,
813 struct arpt_counters counters[])
819 /* Instead of clearing (by a previous call to memset())
820 * the counters and using adds, we set the counters
821 * with data used by 'current' CPU
822 * We dont care about preemption here.
824 curcpu = raw_smp_processor_id();
827 ARPT_ENTRY_ITERATE(t->entries[curcpu],
829 set_entry_to_counter,
837 ARPT_ENTRY_ITERATE(t->entries[cpu],
839 add_entry_to_counter,
845 static int copy_entries_to_user(unsigned int total_size,
846 struct arpt_table *table,
847 void __user *userptr)
849 unsigned int off, num, countersize;
850 struct arpt_entry *e;
851 struct arpt_counters *counters;
855 /* We need atomic snapshot of counters: rest doesn't change
856 * (other than comefrom, which userspace doesn't care
859 countersize = sizeof(struct arpt_counters) * table->private->number;
860 counters = vmalloc(countersize);
862 if (counters == NULL)
865 /* First, sum counters... */
866 write_lock_bh(&table->lock);
867 get_counters(table->private, counters);
868 write_unlock_bh(&table->lock);
870 loc_cpu_entry = table->private->entries[raw_smp_processor_id()];
871 /* ... then copy entire thing ... */
872 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
877 /* FIXME: use iterator macros --RR */
878 /* ... then go back and fix counters and names */
879 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
880 struct arpt_entry_target *t;
882 e = (struct arpt_entry *)(loc_cpu_entry + off);
883 if (copy_to_user(userptr + off
884 + offsetof(struct arpt_entry, counters),
886 sizeof(counters[num])) != 0) {
891 t = arpt_get_target(e);
892 if (copy_to_user(userptr + off + e->target_offset
893 + offsetof(struct arpt_entry_target,
895 t->u.kernel.target->name,
896 strlen(t->u.kernel.target->name)+1) != 0) {
907 static int get_entries(const struct arpt_get_entries *entries,
908 struct arpt_get_entries __user *uptr)
911 struct arpt_table *t;
913 t = find_table_lock(entries->name);
914 if (t || !IS_ERR(t)) {
915 duprintf("t->private->number = %u\n",
917 if (entries->size == t->private->size)
918 ret = copy_entries_to_user(t->private->size,
919 t, uptr->entrytable);
921 duprintf("get_entries: I've got %u not %u!\n",
929 ret = t ? PTR_ERR(t) : -ENOENT;
934 static void free_table_info(struct arpt_table_info *info)
938 if (info->size <= PAGE_SIZE)
939 kfree(info->entries[cpu]);
941 vfree(info->entries[cpu]);
946 static struct arpt_table_info *alloc_table_info(unsigned int size)
948 struct arpt_table_info *newinfo;
951 newinfo = kzalloc(sizeof(struct arpt_table_info), GFP_KERNEL);
955 newinfo->size = size;
958 if (size <= PAGE_SIZE)
959 newinfo->entries[cpu] = kmalloc_node(size,
963 newinfo->entries[cpu] = vmalloc_node(size,
966 if (newinfo->entries[cpu] == NULL) {
967 free_table_info(newinfo);
975 static int do_replace(void __user *user, unsigned int len)
978 struct arpt_replace tmp;
979 struct arpt_table *t;
980 struct arpt_table_info *newinfo, *oldinfo;
981 struct arpt_counters *counters;
982 void *loc_cpu_entry, *loc_cpu_old_entry;
984 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
987 /* Hack: Causes ipchains to give correct error msg --RR */
988 if (len != sizeof(tmp) + tmp.size)
991 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
992 if ((SMP_ALIGN(tmp.size) >> PAGE_SHIFT) + 2 > num_physpages)
995 newinfo = alloc_table_info(tmp.size);
999 /* choose the copy that is on our node/cpu */
1000 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1001 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1007 counters = vmalloc(tmp.num_counters * sizeof(struct arpt_counters));
1013 ret = translate_table(tmp.name, tmp.valid_hooks,
1014 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
1015 tmp.hook_entry, tmp.underflow);
1017 goto free_newinfo_counters;
1019 duprintf("arp_tables: Translated table\n");
1021 t = try_then_request_module(find_table_lock(tmp.name),
1022 "arptable_%s", tmp.name);
1023 if (!t || IS_ERR(t)) {
1024 ret = t ? PTR_ERR(t) : -ENOENT;
1025 goto free_newinfo_counters_untrans;
1029 if (tmp.valid_hooks != t->valid_hooks) {
1030 duprintf("Valid hook crap: %08X vs %08X\n",
1031 tmp.valid_hooks, t->valid_hooks);
1036 oldinfo = replace_table(t, tmp.num_counters, newinfo, &ret);
1040 /* Update module usage count based on number of rules */
1041 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1042 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1043 if ((oldinfo->number > oldinfo->initial_entries) ||
1044 (newinfo->number <= oldinfo->initial_entries))
1046 if ((oldinfo->number > oldinfo->initial_entries) &&
1047 (newinfo->number <= oldinfo->initial_entries))
1050 /* Get the old counters. */
1051 get_counters(oldinfo, counters);
1052 /* Decrease module usage counts and free resource */
1053 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1054 ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL);
1056 free_table_info(oldinfo);
1057 if (copy_to_user(tmp.counters, counters,
1058 sizeof(struct arpt_counters) * tmp.num_counters) != 0)
1067 free_newinfo_counters_untrans:
1068 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1069 free_newinfo_counters:
1072 free_table_info(newinfo);
1076 /* We're lazy, and add to the first CPU; overflow works its fey magic
1077 * and everything is OK.
1079 static inline int add_counter_to_entry(struct arpt_entry *e,
1080 const struct arpt_counters addme[],
1084 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1090 static int do_add_counters(void __user *user, unsigned int len)
1093 struct arpt_counters_info tmp, *paddc;
1094 struct arpt_table *t;
1096 void *loc_cpu_entry;
1098 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1101 if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct arpt_counters))
1104 paddc = vmalloc(len);
1108 if (copy_from_user(paddc, user, len) != 0) {
1113 t = find_table_lock(tmp.name);
1114 if (!t || IS_ERR(t)) {
1115 ret = t ? PTR_ERR(t) : -ENOENT;
1119 write_lock_bh(&t->lock);
1120 if (t->private->number != paddc->num_counters) {
1122 goto unlock_up_free;
1126 /* Choose the copy that is on our node */
1127 loc_cpu_entry = t->private->entries[smp_processor_id()];
1128 ARPT_ENTRY_ITERATE(loc_cpu_entry,
1130 add_counter_to_entry,
1134 write_unlock_bh(&t->lock);
1143 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1147 if (!capable(CAP_NET_ADMIN))
1151 case ARPT_SO_SET_REPLACE:
1152 ret = do_replace(user, len);
1155 case ARPT_SO_SET_ADD_COUNTERS:
1156 ret = do_add_counters(user, len);
1160 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1167 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1171 if (!capable(CAP_NET_ADMIN))
1175 case ARPT_SO_GET_INFO: {
1176 char name[ARPT_TABLE_MAXNAMELEN];
1177 struct arpt_table *t;
1179 if (*len != sizeof(struct arpt_getinfo)) {
1180 duprintf("length %u != %Zu\n", *len,
1181 sizeof(struct arpt_getinfo));
1186 if (copy_from_user(name, user, sizeof(name)) != 0) {
1190 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
1192 t = try_then_request_module(find_table_lock(name),
1193 "arptable_%s", name);
1194 if (t && !IS_ERR(t)) {
1195 struct arpt_getinfo info;
1197 info.valid_hooks = t->valid_hooks;
1198 memcpy(info.hook_entry, t->private->hook_entry,
1199 sizeof(info.hook_entry));
1200 memcpy(info.underflow, t->private->underflow,
1201 sizeof(info.underflow));
1202 info.num_entries = t->private->number;
1203 info.size = t->private->size;
1204 strcpy(info.name, name);
1206 if (copy_to_user(user, &info, *len) != 0)
1213 ret = t ? PTR_ERR(t) : -ENOENT;
1217 case ARPT_SO_GET_ENTRIES: {
1218 struct arpt_get_entries get;
1220 if (*len < sizeof(get)) {
1221 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
1223 } else if (copy_from_user(&get, user, sizeof(get)) != 0) {
1225 } else if (*len != sizeof(struct arpt_get_entries) + get.size) {
1226 duprintf("get_entries: %u != %Zu\n", *len,
1227 sizeof(struct arpt_get_entries) + get.size);
1230 ret = get_entries(&get, user);
1234 case ARPT_SO_GET_REVISION_TARGET: {
1235 struct arpt_get_revision rev;
1237 if (*len != sizeof(rev)) {
1241 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1246 try_then_request_module(find_revision(rev.name, rev.revision,
1247 target_revfn, &ret),
1248 "arpt_%s", rev.name);
1253 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1260 /* Registration hooks for targets. */
1261 int arpt_register_target(struct arpt_target *target)
1265 ret = down_interruptible(&arpt_mutex);
1269 list_add(&target->list, &arpt_target);
1275 void arpt_unregister_target(struct arpt_target *target)
1278 LIST_DELETE(&arpt_target, target);
1282 int arpt_register_table(struct arpt_table *table,
1283 const struct arpt_replace *repl)
1286 struct arpt_table_info *newinfo;
1287 static struct arpt_table_info bootstrap
1288 = { 0, 0, 0, { 0 }, { 0 }, { } };
1289 void *loc_cpu_entry;
1291 newinfo = alloc_table_info(repl->size);
1297 /* choose the copy on our node/cpu */
1298 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1299 memcpy(loc_cpu_entry, repl->entries, repl->size);
1301 ret = translate_table(table->name, table->valid_hooks,
1302 newinfo, loc_cpu_entry, repl->size,
1306 duprintf("arpt_register_table: translate table gives %d\n", ret);
1308 free_table_info(newinfo);
1312 ret = down_interruptible(&arpt_mutex);
1314 free_table_info(newinfo);
1318 /* Don't autoload: we'd eat our tail... */
1319 if (list_named_find(&arpt_tables, table->name)) {
1324 /* Simplifies replace_table code. */
1325 table->private = &bootstrap;
1326 if (!replace_table(table, 0, newinfo, &ret))
1329 duprintf("table->private->number = %u\n",
1330 table->private->number);
1332 /* save number of initial entries */
1333 table->private->initial_entries = table->private->number;
1335 rwlock_init(&table->lock);
1336 list_prepend(&arpt_tables, table);
1343 free_table_info(newinfo);
1347 void arpt_unregister_table(struct arpt_table *table)
1349 void *loc_cpu_entry;
1352 LIST_DELETE(&arpt_tables, table);
1355 /* Decrease module usage counts and free resources */
1356 loc_cpu_entry = table->private->entries[raw_smp_processor_id()];
1357 ARPT_ENTRY_ITERATE(loc_cpu_entry, table->private->size,
1358 cleanup_entry, NULL);
1359 free_table_info(table->private);
1362 /* The built-in targets: standard (NULL) and error. */
1363 static struct arpt_target arpt_standard_target = {
1364 .name = ARPT_STANDARD_TARGET,
1367 static struct arpt_target arpt_error_target = {
1368 .name = ARPT_ERROR_TARGET,
1369 .target = arpt_error,
1372 static struct nf_sockopt_ops arpt_sockopts = {
1374 .set_optmin = ARPT_BASE_CTL,
1375 .set_optmax = ARPT_SO_SET_MAX+1,
1376 .set = do_arpt_set_ctl,
1377 .get_optmin = ARPT_BASE_CTL,
1378 .get_optmax = ARPT_SO_GET_MAX+1,
1379 .get = do_arpt_get_ctl,
1382 #ifdef CONFIG_PROC_FS
1383 static inline int print_name(const struct arpt_table *t,
1384 off_t start_offset, char *buffer, int length,
1385 off_t *pos, unsigned int *count)
1387 if ((*count)++ >= start_offset) {
1388 unsigned int namelen;
1390 namelen = sprintf(buffer + *pos, "%s\n", t->name);
1391 if (*pos + namelen > length) {
1392 /* Stop iterating */
1400 static int arpt_get_tables(char *buffer, char **start, off_t offset, int length)
1403 unsigned int count = 0;
1405 if (down_interruptible(&arpt_mutex) != 0)
1408 LIST_FIND(&arpt_tables, print_name, struct arpt_table *,
1409 offset, buffer, length, &pos, &count);
1413 /* `start' hack - see fs/proc/generic.c line ~105 */
1414 *start=(char *)((unsigned long)count-offset);
1417 #endif /*CONFIG_PROC_FS*/
1419 static int __init init(void)
1423 /* Noone else will be downing sem now, so we won't sleep */
1425 list_append(&arpt_target, &arpt_standard_target);
1426 list_append(&arpt_target, &arpt_error_target);
1429 /* Register setsockopt */
1430 ret = nf_register_sockopt(&arpt_sockopts);
1432 duprintf("Unable to register sockopts.\n");
1436 #ifdef CONFIG_PROC_FS
1438 struct proc_dir_entry *proc;
1440 proc = proc_net_create("arp_tables_names", 0, arpt_get_tables);
1442 nf_unregister_sockopt(&arpt_sockopts);
1445 proc->owner = THIS_MODULE;
1449 printk("arp_tables: (C) 2002 David S. Miller\n");
1453 static void __exit fini(void)
1455 nf_unregister_sockopt(&arpt_sockopts);
1456 #ifdef CONFIG_PROC_FS
1457 proc_net_remove("arp_tables_names");
1461 EXPORT_SYMBOL(arpt_register_table);
1462 EXPORT_SYMBOL(arpt_unregister_table);
1463 EXPORT_SYMBOL(arpt_do_table);
1464 EXPORT_SYMBOL(arpt_register_target);
1465 EXPORT_SYMBOL(arpt_unregister_target);