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/capability.h>
17 #include <linux/if_arp.h>
18 #include <linux/kmod.h>
19 #include <linux/vmalloc.h>
20 #include <linux/proc_fs.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <asm/uaccess.h>
25 #include <asm/semaphore.h>
27 #include <linux/netfilter_arp/arp_tables.h>
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
31 MODULE_DESCRIPTION("arptables core");
33 /*#define DEBUG_ARP_TABLES*/
34 /*#define DEBUG_ARP_TABLES_USER*/
36 #ifdef DEBUG_ARP_TABLES
37 #define dprintf(format, args...) printk(format , ## args)
39 #define dprintf(format, args...)
42 #ifdef DEBUG_ARP_TABLES_USER
43 #define duprintf(format, args...) printk(format , ## args)
45 #define duprintf(format, args...)
48 #ifdef CONFIG_NETFILTER_DEBUG
49 #define ARP_NF_ASSERT(x) \
52 printk("ARP_NF_ASSERT: %s:%s:%u\n", \
53 __FUNCTION__, __FILE__, __LINE__); \
56 #define ARP_NF_ASSERT(x)
58 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
60 static DECLARE_MUTEX(arpt_mutex);
62 #define ASSERT_READ_LOCK(x) ARP_NF_ASSERT(down_trylock(&arpt_mutex) != 0)
63 #define ASSERT_WRITE_LOCK(x) ARP_NF_ASSERT(down_trylock(&arpt_mutex) != 0)
64 #include <linux/netfilter_ipv4/listhelp.h>
66 struct arpt_table_info {
69 unsigned int initial_entries;
70 unsigned int hook_entry[NF_ARP_NUMHOOKS];
71 unsigned int underflow[NF_ARP_NUMHOOKS];
72 void *entries[NR_CPUS];
75 static LIST_HEAD(arpt_target);
76 static LIST_HEAD(arpt_tables);
77 #define SET_COUNTER(c,b,p) do { (c).bcnt = (b); (c).pcnt = (p); } while(0)
78 #define ADD_COUNTER(c,b,p) do { (c).bcnt += (b); (c).pcnt += (p); } while(0)
80 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
81 char *hdr_addr, int len)
85 if (len > ARPT_DEV_ADDR_LEN_MAX)
86 len = ARPT_DEV_ADDR_LEN_MAX;
89 for (i = 0; i < len; i++)
90 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
95 /* Returns whether packet matches rule or not. */
96 static inline int arp_packet_match(const struct arphdr *arphdr,
97 struct net_device *dev,
100 const struct arpt_arp *arpinfo)
102 char *arpptr = (char *)(arphdr + 1);
103 char *src_devaddr, *tgt_devaddr;
104 u32 src_ipaddr, tgt_ipaddr;
107 #define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
109 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
111 dprintf("ARP operation field mismatch.\n");
112 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
113 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
117 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
119 dprintf("ARP hardware address format mismatch.\n");
120 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
121 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
125 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
127 dprintf("ARP protocol address format mismatch.\n");
128 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
129 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
133 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
135 dprintf("ARP hardware address length mismatch.\n");
136 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
137 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
141 src_devaddr = arpptr;
142 arpptr += dev->addr_len;
143 memcpy(&src_ipaddr, arpptr, sizeof(u32));
144 arpptr += sizeof(u32);
145 tgt_devaddr = arpptr;
146 arpptr += dev->addr_len;
147 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
149 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
150 ARPT_INV_SRCDEVADDR) ||
151 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
152 ARPT_INV_TGTDEVADDR)) {
153 dprintf("Source or target device address mismatch.\n");
158 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
160 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
162 dprintf("Source or target IP address mismatch.\n");
164 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
166 NIPQUAD(arpinfo->smsk.s_addr),
167 NIPQUAD(arpinfo->src.s_addr),
168 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
169 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
171 NIPQUAD(arpinfo->tmsk.s_addr),
172 NIPQUAD(arpinfo->tgt.s_addr),
173 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
177 /* Look for ifname matches. */
178 for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
179 ret |= (indev[i] ^ arpinfo->iniface[i])
180 & arpinfo->iniface_mask[i];
183 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
184 dprintf("VIA in mismatch (%s vs %s).%s\n",
185 indev, arpinfo->iniface,
186 arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
190 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
192 memcpy(&odev, outdev + i*sizeof(unsigned long),
193 sizeof(unsigned long));
195 ^ ((const unsigned long *)arpinfo->outiface)[i])
196 & ((const unsigned long *)arpinfo->outiface_mask)[i];
199 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
200 dprintf("VIA out mismatch (%s vs %s).%s\n",
201 outdev, arpinfo->outiface,
202 arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
209 static inline int arp_checkentry(const struct arpt_arp *arp)
211 if (arp->flags & ~ARPT_F_MASK) {
212 duprintf("Unknown flag bits set: %08X\n",
213 arp->flags & ~ARPT_F_MASK);
216 if (arp->invflags & ~ARPT_INV_MASK) {
217 duprintf("Unknown invflag bits set: %08X\n",
218 arp->invflags & ~ARPT_INV_MASK);
225 static unsigned int arpt_error(struct sk_buff **pskb,
226 unsigned int hooknum,
227 const struct net_device *in,
228 const struct net_device *out,
229 const void *targinfo,
233 printk("arp_tables: error: '%s'\n", (char *)targinfo);
238 static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
240 return (struct arpt_entry *)(base + offset);
243 unsigned int arpt_do_table(struct sk_buff **pskb,
245 const struct net_device *in,
246 const struct net_device *out,
247 struct arpt_table *table,
250 static const char nulldevname[IFNAMSIZ];
251 unsigned int verdict = NF_DROP;
254 struct arpt_entry *e, *back;
255 const char *indev, *outdev;
258 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
259 if (!pskb_may_pull((*pskb), (sizeof(struct arphdr) +
260 (2 * (*pskb)->dev->addr_len) +
264 indev = in ? in->name : nulldevname;
265 outdev = out ? out->name : nulldevname;
267 read_lock_bh(&table->lock);
268 table_base = (void *)table->private->entries[smp_processor_id()];
269 e = get_entry(table_base, table->private->hook_entry[hook]);
270 back = get_entry(table_base, table->private->underflow[hook]);
272 arp = (*pskb)->nh.arph;
274 if (arp_packet_match(arp, (*pskb)->dev, indev, outdev, &e->arp)) {
275 struct arpt_entry_target *t;
278 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
279 (2 * (*pskb)->dev->addr_len);
280 ADD_COUNTER(e->counters, hdr_len, 1);
282 t = arpt_get_target(e);
284 /* Standard target? */
285 if (!t->u.kernel.target->target) {
288 v = ((struct arpt_standard_target *)t)->verdict;
290 /* Pop from stack? */
291 if (v != ARPT_RETURN) {
292 verdict = (unsigned)(-v) - 1;
296 back = get_entry(table_base,
301 != (void *)e + e->next_offset) {
302 /* Save old back ptr in next entry */
303 struct arpt_entry *next
304 = (void *)e + e->next_offset;
306 (void *)back - table_base;
308 /* set back pointer to next entry */
312 e = get_entry(table_base, v);
314 /* Targets which reenter must return
317 verdict = t->u.kernel.target->target(pskb,
323 /* Target might have changed stuff. */
324 arp = (*pskb)->nh.arph;
326 if (verdict == ARPT_CONTINUE)
327 e = (void *)e + e->next_offset;
333 e = (void *)e + e->next_offset;
336 read_unlock_bh(&table->lock);
345 * These are weird, but module loading must not be done with mutex
346 * held (since they will register), and we have to have a single
347 * function to use try_then_request_module().
350 /* Find table by name, grabs mutex & ref. Returns ERR_PTR() on error. */
351 static inline struct arpt_table *find_table_lock(const char *name)
353 struct arpt_table *t;
355 if (down_interruptible(&arpt_mutex) != 0)
356 return ERR_PTR(-EINTR);
358 list_for_each_entry(t, &arpt_tables, list)
359 if (strcmp(t->name, name) == 0 && try_module_get(t->me))
366 /* Find target, grabs ref. Returns ERR_PTR() on error. */
367 static inline struct arpt_target *find_target(const char *name, u8 revision)
369 struct arpt_target *t;
372 if (down_interruptible(&arpt_mutex) != 0)
373 return ERR_PTR(-EINTR);
375 list_for_each_entry(t, &arpt_target, list) {
376 if (strcmp(t->name, name) == 0) {
377 if (t->revision == revision) {
378 if (try_module_get(t->me)) {
383 err = -EPROTOTYPE; /* Found something. */
390 struct arpt_target *arpt_find_target(const char *name, u8 revision)
392 struct arpt_target *target;
394 target = try_then_request_module(find_target(name, revision),
396 if (IS_ERR(target) || !target)
401 static int target_revfn(const char *name, u8 revision, int *bestp)
403 struct arpt_target *t;
406 list_for_each_entry(t, &arpt_target, list) {
407 if (strcmp(t->name, name) == 0) {
408 if (t->revision > *bestp)
409 *bestp = t->revision;
410 if (t->revision == revision)
417 /* Returns true or false (if no such extension at all) */
418 static inline int find_revision(const char *name, u8 revision,
419 int (*revfn)(const char *, u8, int *),
422 int have_rev, best = -1;
424 if (down_interruptible(&arpt_mutex) != 0) {
428 have_rev = revfn(name, revision, &best);
431 /* Nothing at all? Return 0 to try loading module. */
439 *err = -EPROTONOSUPPORT;
444 /* All zeroes == unconditional rule. */
445 static inline int unconditional(const struct arpt_arp *arp)
449 for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
450 if (((__u32 *)arp)[i])
456 /* Figures out from what hook each rule can be called: returns 0 if
457 * there are loops. Puts hook bitmask in comefrom.
459 static int mark_source_chains(struct arpt_table_info *newinfo,
460 unsigned int valid_hooks, void *entry0)
464 /* No recursion; use packet counter to save back ptrs (reset
465 * to 0 as we leave), and comefrom to save source hook bitmask.
467 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
468 unsigned int pos = newinfo->hook_entry[hook];
470 = (struct arpt_entry *)(entry0 + pos);
472 if (!(valid_hooks & (1 << hook)))
475 /* Set initial back pointer. */
476 e->counters.pcnt = pos;
479 struct arpt_standard_target *t
480 = (void *)arpt_get_target(e);
482 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
483 printk("arptables: loop hook %u pos %u %08X.\n",
484 hook, pos, e->comefrom);
488 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
490 /* Unconditional return/END. */
491 if (e->target_offset == sizeof(struct arpt_entry)
492 && (strcmp(t->target.u.user.name,
493 ARPT_STANDARD_TARGET) == 0)
495 && unconditional(&e->arp)) {
496 unsigned int oldpos, size;
498 /* Return: backtrack through the last
502 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
504 pos = e->counters.pcnt;
505 e->counters.pcnt = 0;
507 /* We're at the start. */
511 e = (struct arpt_entry *)
513 } while (oldpos == pos + e->next_offset);
516 size = e->next_offset;
517 e = (struct arpt_entry *)
518 (entry0 + pos + size);
519 e->counters.pcnt = pos;
522 int newpos = t->verdict;
524 if (strcmp(t->target.u.user.name,
525 ARPT_STANDARD_TARGET) == 0
527 /* This a jump; chase it. */
528 duprintf("Jump rule %u -> %u\n",
531 /* ... this is a fallthru */
532 newpos = pos + e->next_offset;
534 e = (struct arpt_entry *)
536 e->counters.pcnt = pos;
541 duprintf("Finished chain %u\n", hook);
546 static inline int standard_check(const struct arpt_entry_target *t,
547 unsigned int max_offset)
549 struct arpt_standard_target *targ = (void *)t;
551 /* Check standard info. */
553 != ARPT_ALIGN(sizeof(struct arpt_standard_target))) {
554 duprintf("arpt_standard_check: target size %u != %Zu\n",
556 ARPT_ALIGN(sizeof(struct arpt_standard_target)));
560 if (targ->verdict >= 0
561 && targ->verdict > max_offset - sizeof(struct arpt_entry)) {
562 duprintf("arpt_standard_check: bad verdict (%i)\n",
567 if (targ->verdict < -NF_MAX_VERDICT - 1) {
568 duprintf("arpt_standard_check: bad negative verdict (%i)\n",
575 static struct arpt_target arpt_standard_target;
577 static inline int check_entry(struct arpt_entry *e, const char *name, unsigned int size,
580 struct arpt_entry_target *t;
581 struct arpt_target *target;
584 if (!arp_checkentry(&e->arp)) {
585 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
589 t = arpt_get_target(e);
590 target = try_then_request_module(find_target(t->u.user.name,
592 "arpt_%s", t->u.user.name);
593 if (IS_ERR(target) || !target) {
594 duprintf("check_entry: `%s' not found\n", t->u.user.name);
595 ret = target ? PTR_ERR(target) : -ENOENT;
598 t->u.kernel.target = target;
600 if (t->u.kernel.target == &arpt_standard_target) {
601 if (!standard_check(t, size)) {
605 } else if (t->u.kernel.target->checkentry
606 && !t->u.kernel.target->checkentry(name, e, t->data,
610 module_put(t->u.kernel.target->me);
611 duprintf("arp_tables: check failed for `%s'.\n",
612 t->u.kernel.target->name);
624 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
625 struct arpt_table_info *newinfo,
627 unsigned char *limit,
628 const unsigned int *hook_entries,
629 const unsigned int *underflows,
634 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
635 || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
636 duprintf("Bad offset %p\n", e);
641 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
642 duprintf("checking: element %p size %u\n",
647 /* Check hooks & underflows */
648 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
649 if ((unsigned char *)e - base == hook_entries[h])
650 newinfo->hook_entry[h] = hook_entries[h];
651 if ((unsigned char *)e - base == underflows[h])
652 newinfo->underflow[h] = underflows[h];
655 /* FIXME: underflows must be unconditional, standard verdicts
656 < 0 (not ARPT_RETURN). --RR */
658 /* Clear counters and comefrom */
659 e->counters = ((struct arpt_counters) { 0, 0 });
666 static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
668 struct arpt_entry_target *t;
670 if (i && (*i)-- == 0)
673 t = arpt_get_target(e);
674 if (t->u.kernel.target->destroy)
675 t->u.kernel.target->destroy(t->data,
676 t->u.target_size - sizeof(*t));
677 module_put(t->u.kernel.target->me);
681 /* Checks and translates the user-supplied table segment (held in
684 static int translate_table(const char *name,
685 unsigned int valid_hooks,
686 struct arpt_table_info *newinfo,
690 const unsigned int *hook_entries,
691 const unsigned int *underflows)
696 newinfo->size = size;
697 newinfo->number = number;
699 /* Init all hooks to impossible value. */
700 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
701 newinfo->hook_entry[i] = 0xFFFFFFFF;
702 newinfo->underflow[i] = 0xFFFFFFFF;
705 duprintf("translate_table: size %u\n", newinfo->size);
708 /* Walk through entries, checking offsets. */
709 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
710 check_entry_size_and_hooks,
714 hook_entries, underflows, &i);
715 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
720 duprintf("translate_table: %u not %u entries\n",
725 /* Check hooks all assigned */
726 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
727 /* Only hooks which are valid */
728 if (!(valid_hooks & (1 << i)))
730 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
731 duprintf("Invalid hook entry %u %u\n",
735 if (newinfo->underflow[i] == 0xFFFFFFFF) {
736 duprintf("Invalid underflow %u %u\n",
742 if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
743 duprintf("Looping hook\n");
747 /* Finally, each sanity check must pass */
749 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
750 check_entry, name, size, &i);
753 ARPT_ENTRY_ITERATE(entry0, newinfo->size,
758 /* And one copy for every other CPU */
760 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
761 memcpy(newinfo->entries[i], entry0, newinfo->size);
767 static struct arpt_table_info *replace_table(struct arpt_table *table,
768 unsigned int num_counters,
769 struct arpt_table_info *newinfo,
772 struct arpt_table_info *oldinfo;
774 /* Do the substitution. */
775 write_lock_bh(&table->lock);
776 /* Check inside lock: is the old number correct? */
777 if (num_counters != table->private->number) {
778 duprintf("num_counters != table->private->number (%u/%u)\n",
779 num_counters, table->private->number);
780 write_unlock_bh(&table->lock);
784 oldinfo = table->private;
785 table->private = newinfo;
786 newinfo->initial_entries = oldinfo->initial_entries;
787 write_unlock_bh(&table->lock);
793 static inline int add_entry_to_counter(const struct arpt_entry *e,
794 struct arpt_counters total[],
797 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
803 static inline int set_entry_to_counter(const struct arpt_entry *e,
804 struct arpt_counters total[],
807 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
813 static void get_counters(const struct arpt_table_info *t,
814 struct arpt_counters counters[])
820 /* Instead of clearing (by a previous call to memset())
821 * the counters and using adds, we set the counters
822 * with data used by 'current' CPU
823 * We dont care about preemption here.
825 curcpu = raw_smp_processor_id();
828 ARPT_ENTRY_ITERATE(t->entries[curcpu],
830 set_entry_to_counter,
838 ARPT_ENTRY_ITERATE(t->entries[cpu],
840 add_entry_to_counter,
846 static int copy_entries_to_user(unsigned int total_size,
847 struct arpt_table *table,
848 void __user *userptr)
850 unsigned int off, num, countersize;
851 struct arpt_entry *e;
852 struct arpt_counters *counters;
856 /* We need atomic snapshot of counters: rest doesn't change
857 * (other than comefrom, which userspace doesn't care
860 countersize = sizeof(struct arpt_counters) * table->private->number;
861 counters = vmalloc(countersize);
863 if (counters == NULL)
866 /* First, sum counters... */
867 write_lock_bh(&table->lock);
868 get_counters(table->private, counters);
869 write_unlock_bh(&table->lock);
871 loc_cpu_entry = table->private->entries[raw_smp_processor_id()];
872 /* ... then copy entire thing ... */
873 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
878 /* FIXME: use iterator macros --RR */
879 /* ... then go back and fix counters and names */
880 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
881 struct arpt_entry_target *t;
883 e = (struct arpt_entry *)(loc_cpu_entry + off);
884 if (copy_to_user(userptr + off
885 + offsetof(struct arpt_entry, counters),
887 sizeof(counters[num])) != 0) {
892 t = arpt_get_target(e);
893 if (copy_to_user(userptr + off + e->target_offset
894 + offsetof(struct arpt_entry_target,
896 t->u.kernel.target->name,
897 strlen(t->u.kernel.target->name)+1) != 0) {
908 static int get_entries(const struct arpt_get_entries *entries,
909 struct arpt_get_entries __user *uptr)
912 struct arpt_table *t;
914 t = find_table_lock(entries->name);
915 if (t || !IS_ERR(t)) {
916 duprintf("t->private->number = %u\n",
918 if (entries->size == t->private->size)
919 ret = copy_entries_to_user(t->private->size,
920 t, uptr->entrytable);
922 duprintf("get_entries: I've got %u not %u!\n",
930 ret = t ? PTR_ERR(t) : -ENOENT;
935 static void free_table_info(struct arpt_table_info *info)
939 if (info->size <= PAGE_SIZE)
940 kfree(info->entries[cpu]);
942 vfree(info->entries[cpu]);
947 static struct arpt_table_info *alloc_table_info(unsigned int size)
949 struct arpt_table_info *newinfo;
952 newinfo = kzalloc(sizeof(struct arpt_table_info), GFP_KERNEL);
956 newinfo->size = size;
959 if (size <= PAGE_SIZE)
960 newinfo->entries[cpu] = kmalloc_node(size,
964 newinfo->entries[cpu] = vmalloc_node(size,
967 if (newinfo->entries[cpu] == NULL) {
968 free_table_info(newinfo);
976 static int do_replace(void __user *user, unsigned int len)
979 struct arpt_replace tmp;
980 struct arpt_table *t;
981 struct arpt_table_info *newinfo, *oldinfo;
982 struct arpt_counters *counters;
983 void *loc_cpu_entry, *loc_cpu_old_entry;
985 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
988 /* Hack: Causes ipchains to give correct error msg --RR */
989 if (len != sizeof(tmp) + tmp.size)
992 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
993 if ((SMP_ALIGN(tmp.size) >> PAGE_SHIFT) + 2 > num_physpages)
996 newinfo = alloc_table_info(tmp.size);
1000 /* choose the copy that is on our node/cpu */
1001 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1002 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1008 counters = vmalloc(tmp.num_counters * sizeof(struct arpt_counters));
1014 ret = translate_table(tmp.name, tmp.valid_hooks,
1015 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
1016 tmp.hook_entry, tmp.underflow);
1018 goto free_newinfo_counters;
1020 duprintf("arp_tables: Translated table\n");
1022 t = try_then_request_module(find_table_lock(tmp.name),
1023 "arptable_%s", tmp.name);
1024 if (!t || IS_ERR(t)) {
1025 ret = t ? PTR_ERR(t) : -ENOENT;
1026 goto free_newinfo_counters_untrans;
1030 if (tmp.valid_hooks != t->valid_hooks) {
1031 duprintf("Valid hook crap: %08X vs %08X\n",
1032 tmp.valid_hooks, t->valid_hooks);
1037 oldinfo = replace_table(t, tmp.num_counters, newinfo, &ret);
1041 /* Update module usage count based on number of rules */
1042 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1043 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1044 if ((oldinfo->number > oldinfo->initial_entries) ||
1045 (newinfo->number <= oldinfo->initial_entries))
1047 if ((oldinfo->number > oldinfo->initial_entries) &&
1048 (newinfo->number <= oldinfo->initial_entries))
1051 /* Get the old counters. */
1052 get_counters(oldinfo, counters);
1053 /* Decrease module usage counts and free resource */
1054 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1055 ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL);
1057 free_table_info(oldinfo);
1058 if (copy_to_user(tmp.counters, counters,
1059 sizeof(struct arpt_counters) * tmp.num_counters) != 0)
1068 free_newinfo_counters_untrans:
1069 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1070 free_newinfo_counters:
1073 free_table_info(newinfo);
1077 /* We're lazy, and add to the first CPU; overflow works its fey magic
1078 * and everything is OK.
1080 static inline int add_counter_to_entry(struct arpt_entry *e,
1081 const struct arpt_counters addme[],
1085 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1091 static int do_add_counters(void __user *user, unsigned int len)
1094 struct arpt_counters_info tmp, *paddc;
1095 struct arpt_table *t;
1097 void *loc_cpu_entry;
1099 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1102 if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct arpt_counters))
1105 paddc = vmalloc(len);
1109 if (copy_from_user(paddc, user, len) != 0) {
1114 t = find_table_lock(tmp.name);
1115 if (!t || IS_ERR(t)) {
1116 ret = t ? PTR_ERR(t) : -ENOENT;
1120 write_lock_bh(&t->lock);
1121 if (t->private->number != paddc->num_counters) {
1123 goto unlock_up_free;
1127 /* Choose the copy that is on our node */
1128 loc_cpu_entry = t->private->entries[smp_processor_id()];
1129 ARPT_ENTRY_ITERATE(loc_cpu_entry,
1131 add_counter_to_entry,
1135 write_unlock_bh(&t->lock);
1144 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1148 if (!capable(CAP_NET_ADMIN))
1152 case ARPT_SO_SET_REPLACE:
1153 ret = do_replace(user, len);
1156 case ARPT_SO_SET_ADD_COUNTERS:
1157 ret = do_add_counters(user, len);
1161 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1168 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1172 if (!capable(CAP_NET_ADMIN))
1176 case ARPT_SO_GET_INFO: {
1177 char name[ARPT_TABLE_MAXNAMELEN];
1178 struct arpt_table *t;
1180 if (*len != sizeof(struct arpt_getinfo)) {
1181 duprintf("length %u != %Zu\n", *len,
1182 sizeof(struct arpt_getinfo));
1187 if (copy_from_user(name, user, sizeof(name)) != 0) {
1191 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
1193 t = try_then_request_module(find_table_lock(name),
1194 "arptable_%s", name);
1195 if (t && !IS_ERR(t)) {
1196 struct arpt_getinfo info;
1198 info.valid_hooks = t->valid_hooks;
1199 memcpy(info.hook_entry, t->private->hook_entry,
1200 sizeof(info.hook_entry));
1201 memcpy(info.underflow, t->private->underflow,
1202 sizeof(info.underflow));
1203 info.num_entries = t->private->number;
1204 info.size = t->private->size;
1205 strcpy(info.name, name);
1207 if (copy_to_user(user, &info, *len) != 0)
1214 ret = t ? PTR_ERR(t) : -ENOENT;
1218 case ARPT_SO_GET_ENTRIES: {
1219 struct arpt_get_entries get;
1221 if (*len < sizeof(get)) {
1222 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
1224 } else if (copy_from_user(&get, user, sizeof(get)) != 0) {
1226 } else if (*len != sizeof(struct arpt_get_entries) + get.size) {
1227 duprintf("get_entries: %u != %Zu\n", *len,
1228 sizeof(struct arpt_get_entries) + get.size);
1231 ret = get_entries(&get, user);
1235 case ARPT_SO_GET_REVISION_TARGET: {
1236 struct arpt_get_revision rev;
1238 if (*len != sizeof(rev)) {
1242 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1247 try_then_request_module(find_revision(rev.name, rev.revision,
1248 target_revfn, &ret),
1249 "arpt_%s", rev.name);
1254 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1261 /* Registration hooks for targets. */
1262 int arpt_register_target(struct arpt_target *target)
1266 ret = down_interruptible(&arpt_mutex);
1270 list_add(&target->list, &arpt_target);
1276 void arpt_unregister_target(struct arpt_target *target)
1279 LIST_DELETE(&arpt_target, target);
1283 int arpt_register_table(struct arpt_table *table,
1284 const struct arpt_replace *repl)
1287 struct arpt_table_info *newinfo;
1288 static struct arpt_table_info bootstrap
1289 = { 0, 0, 0, { 0 }, { 0 }, { } };
1290 void *loc_cpu_entry;
1292 newinfo = alloc_table_info(repl->size);
1298 /* choose the copy on our node/cpu */
1299 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1300 memcpy(loc_cpu_entry, repl->entries, repl->size);
1302 ret = translate_table(table->name, table->valid_hooks,
1303 newinfo, loc_cpu_entry, repl->size,
1307 duprintf("arpt_register_table: translate table gives %d\n", ret);
1309 free_table_info(newinfo);
1313 ret = down_interruptible(&arpt_mutex);
1315 free_table_info(newinfo);
1319 /* Don't autoload: we'd eat our tail... */
1320 if (list_named_find(&arpt_tables, table->name)) {
1325 /* Simplifies replace_table code. */
1326 table->private = &bootstrap;
1327 if (!replace_table(table, 0, newinfo, &ret))
1330 duprintf("table->private->number = %u\n",
1331 table->private->number);
1333 /* save number of initial entries */
1334 table->private->initial_entries = table->private->number;
1336 rwlock_init(&table->lock);
1337 list_prepend(&arpt_tables, table);
1344 free_table_info(newinfo);
1348 void arpt_unregister_table(struct arpt_table *table)
1350 void *loc_cpu_entry;
1353 LIST_DELETE(&arpt_tables, table);
1356 /* Decrease module usage counts and free resources */
1357 loc_cpu_entry = table->private->entries[raw_smp_processor_id()];
1358 ARPT_ENTRY_ITERATE(loc_cpu_entry, table->private->size,
1359 cleanup_entry, NULL);
1360 free_table_info(table->private);
1363 /* The built-in targets: standard (NULL) and error. */
1364 static struct arpt_target arpt_standard_target = {
1365 .name = ARPT_STANDARD_TARGET,
1368 static struct arpt_target arpt_error_target = {
1369 .name = ARPT_ERROR_TARGET,
1370 .target = arpt_error,
1373 static struct nf_sockopt_ops arpt_sockopts = {
1375 .set_optmin = ARPT_BASE_CTL,
1376 .set_optmax = ARPT_SO_SET_MAX+1,
1377 .set = do_arpt_set_ctl,
1378 .get_optmin = ARPT_BASE_CTL,
1379 .get_optmax = ARPT_SO_GET_MAX+1,
1380 .get = do_arpt_get_ctl,
1383 #ifdef CONFIG_PROC_FS
1384 static inline int print_name(const struct arpt_table *t,
1385 off_t start_offset, char *buffer, int length,
1386 off_t *pos, unsigned int *count)
1388 if ((*count)++ >= start_offset) {
1389 unsigned int namelen;
1391 namelen = sprintf(buffer + *pos, "%s\n", t->name);
1392 if (*pos + namelen > length) {
1393 /* Stop iterating */
1401 static int arpt_get_tables(char *buffer, char **start, off_t offset, int length)
1404 unsigned int count = 0;
1406 if (down_interruptible(&arpt_mutex) != 0)
1409 LIST_FIND(&arpt_tables, print_name, struct arpt_table *,
1410 offset, buffer, length, &pos, &count);
1414 /* `start' hack - see fs/proc/generic.c line ~105 */
1415 *start=(char *)((unsigned long)count-offset);
1418 #endif /*CONFIG_PROC_FS*/
1420 static int __init init(void)
1424 /* Noone else will be downing sem now, so we won't sleep */
1426 list_append(&arpt_target, &arpt_standard_target);
1427 list_append(&arpt_target, &arpt_error_target);
1430 /* Register setsockopt */
1431 ret = nf_register_sockopt(&arpt_sockopts);
1433 duprintf("Unable to register sockopts.\n");
1437 #ifdef CONFIG_PROC_FS
1439 struct proc_dir_entry *proc;
1441 proc = proc_net_create("arp_tables_names", 0, arpt_get_tables);
1443 nf_unregister_sockopt(&arpt_sockopts);
1446 proc->owner = THIS_MODULE;
1450 printk("arp_tables: (C) 2002 David S. Miller\n");
1454 static void __exit fini(void)
1456 nf_unregister_sockopt(&arpt_sockopts);
1457 #ifdef CONFIG_PROC_FS
1458 proc_net_remove("arp_tables_names");
1462 EXPORT_SYMBOL(arpt_register_table);
1463 EXPORT_SYMBOL(arpt_unregister_table);
1464 EXPORT_SYMBOL(arpt_do_table);
1465 EXPORT_SYMBOL(arpt_register_target);
1466 EXPORT_SYMBOL(arpt_unregister_target);