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/lockhelp.h>
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 char entries[0] __attribute__((aligned(SMP_CACHE_BYTES)));
75 static LIST_HEAD(arpt_target);
76 static LIST_HEAD(arpt_tables);
77 #define ADD_COUNTER(c,b,p) do { (c).bcnt += (b); (c).pcnt += (p); } while(0)
80 #define TABLE_OFFSET(t,p) (SMP_ALIGN((t)->size)*(p))
82 #define TABLE_OFFSET(t,p) 0
85 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
86 char *hdr_addr, int len)
90 if (len > ARPT_DEV_ADDR_LEN_MAX)
91 len = ARPT_DEV_ADDR_LEN_MAX;
94 for (i = 0; i < len; i++)
95 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
100 /* Returns whether packet matches rule or not. */
101 static inline int arp_packet_match(const struct arphdr *arphdr,
102 struct net_device *dev,
105 const struct arpt_arp *arpinfo)
107 char *arpptr = (char *)(arphdr + 1);
108 char *src_devaddr, *tgt_devaddr;
109 u32 *src_ipaddr, *tgt_ipaddr;
112 #define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
114 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
116 dprintf("ARP operation field mismatch.\n");
117 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
118 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
122 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
124 dprintf("ARP hardware address format mismatch.\n");
125 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
126 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
130 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
132 dprintf("ARP protocol address format mismatch.\n");
133 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
134 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
138 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
140 dprintf("ARP hardware address length mismatch.\n");
141 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
142 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
146 src_devaddr = arpptr;
147 arpptr += dev->addr_len;
148 src_ipaddr = (u32 *) arpptr;
149 arpptr += sizeof(u32);
150 tgt_devaddr = arpptr;
151 arpptr += dev->addr_len;
152 tgt_ipaddr = (u32 *) arpptr;
154 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
155 ARPT_INV_SRCDEVADDR) ||
156 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
157 ARPT_INV_TGTDEVADDR)) {
158 dprintf("Source or target device address mismatch.\n");
163 if (FWINV(((*src_ipaddr) & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
165 FWINV((((*tgt_ipaddr) & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
167 dprintf("Source or target IP address mismatch.\n");
169 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
170 NIPQUAD(*src_ipaddr),
171 NIPQUAD(arpinfo->smsk.s_addr),
172 NIPQUAD(arpinfo->src.s_addr),
173 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
174 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
175 NIPQUAD(*tgt_ipaddr),
176 NIPQUAD(arpinfo->tmsk.s_addr),
177 NIPQUAD(arpinfo->tgt.s_addr),
178 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
182 /* Look for ifname matches. */
183 for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
184 ret |= (indev[i] ^ arpinfo->iniface[i])
185 & arpinfo->iniface_mask[i];
188 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
189 dprintf("VIA in mismatch (%s vs %s).%s\n",
190 indev, arpinfo->iniface,
191 arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
195 for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
196 ret |= (((const unsigned long *)outdev)[i]
197 ^ ((const unsigned long *)arpinfo->outiface)[i])
198 & ((const unsigned long *)arpinfo->outiface_mask)[i];
201 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
202 dprintf("VIA out mismatch (%s vs %s).%s\n",
203 outdev, arpinfo->outiface,
204 arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
211 static inline int arp_checkentry(const struct arpt_arp *arp)
213 if (arp->flags & ~ARPT_F_MASK) {
214 duprintf("Unknown flag bits set: %08X\n",
215 arp->flags & ~ARPT_F_MASK);
218 if (arp->invflags & ~ARPT_INV_MASK) {
219 duprintf("Unknown invflag bits set: %08X\n",
220 arp->invflags & ~ARPT_INV_MASK);
227 static unsigned int arpt_error(struct sk_buff **pskb,
228 unsigned int hooknum,
229 const struct net_device *in,
230 const struct net_device *out,
231 const void *targinfo,
235 printk("arp_tables: error: '%s'\n", (char *)targinfo);
240 static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
242 return (struct arpt_entry *)(base + offset);
245 unsigned int arpt_do_table(struct sk_buff **pskb,
247 const struct net_device *in,
248 const struct net_device *out,
249 struct arpt_table *table,
252 static const char nulldevname[IFNAMSIZ];
253 unsigned int verdict = NF_DROP;
256 struct arpt_entry *e, *back;
257 const char *indev, *outdev;
260 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
261 if (!pskb_may_pull((*pskb), (sizeof(struct arphdr) +
262 (2 * (*pskb)->dev->addr_len) +
266 indev = in ? in->name : nulldevname;
267 outdev = out ? out->name : nulldevname;
269 read_lock_bh(&table->lock);
270 table_base = (void *)table->private->entries
271 + TABLE_OFFSET(table->private,
273 e = get_entry(table_base, table->private->hook_entry[hook]);
274 back = get_entry(table_base, table->private->underflow[hook]);
276 arp = (*pskb)->nh.arph;
278 if (arp_packet_match(arp, (*pskb)->dev, indev, outdev, &e->arp)) {
279 struct arpt_entry_target *t;
282 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
283 (2 * (*pskb)->dev->addr_len);
284 ADD_COUNTER(e->counters, hdr_len, 1);
286 t = arpt_get_target(e);
288 /* Standard target? */
289 if (!t->u.kernel.target->target) {
292 v = ((struct arpt_standard_target *)t)->verdict;
294 /* Pop from stack? */
295 if (v != ARPT_RETURN) {
296 verdict = (unsigned)(-v) - 1;
300 back = get_entry(table_base,
305 != (void *)e + e->next_offset) {
306 /* Save old back ptr in next entry */
307 struct arpt_entry *next
308 = (void *)e + e->next_offset;
310 (void *)back - table_base;
312 /* set back pointer to next entry */
316 e = get_entry(table_base, v);
318 /* Targets which reenter must return
321 verdict = t->u.kernel.target->target(pskb,
327 /* Target might have changed stuff. */
328 arp = (*pskb)->nh.arph;
330 if (verdict == ARPT_CONTINUE)
331 e = (void *)e + e->next_offset;
337 e = (void *)e + e->next_offset;
340 read_unlock_bh(&table->lock);
348 static inline void *find_inlist_lock_noload(struct list_head *head,
351 struct semaphore *mutex)
355 *error = down_interruptible(mutex);
359 ret = list_named_find(head, name);
368 #define find_inlist_lock(h,n,p,e,m) find_inlist_lock_noload((h),(n),(e),(m))
371 find_inlist_lock(struct list_head *head,
375 struct semaphore *mutex)
379 ret = find_inlist_lock_noload(head, name, error, mutex);
381 duprintf("find_inlist: loading `%s%s'.\n", prefix, name);
382 request_module("%s%s", prefix, name);
383 ret = find_inlist_lock_noload(head, name, error, mutex);
390 static inline struct arpt_table *arpt_find_table_lock(const char *name, int *error, struct semaphore *mutex)
392 return find_inlist_lock(&arpt_tables, name, "arptable_", error, mutex);
395 struct arpt_target *arpt_find_target_lock(const char *name, int *error, struct semaphore *mutex)
397 return find_inlist_lock(&arpt_target, name, "arpt_", error, mutex);
400 /* All zeroes == unconditional rule. */
401 static inline int unconditional(const struct arpt_arp *arp)
405 for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
406 if (((__u32 *)arp)[i])
412 /* Figures out from what hook each rule can be called: returns 0 if
413 * there are loops. Puts hook bitmask in comefrom.
415 static int mark_source_chains(struct arpt_table_info *newinfo, unsigned int valid_hooks)
419 /* No recursion; use packet counter to save back ptrs (reset
420 * to 0 as we leave), and comefrom to save source hook bitmask.
422 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
423 unsigned int pos = newinfo->hook_entry[hook];
425 = (struct arpt_entry *)(newinfo->entries + pos);
427 if (!(valid_hooks & (1 << hook)))
430 /* Set initial back pointer. */
431 e->counters.pcnt = pos;
434 struct arpt_standard_target *t
435 = (void *)arpt_get_target(e);
437 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
438 printk("arptables: loop hook %u pos %u %08X.\n",
439 hook, pos, e->comefrom);
443 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
445 /* Unconditional return/END. */
446 if (e->target_offset == sizeof(struct arpt_entry)
447 && (strcmp(t->target.u.user.name,
448 ARPT_STANDARD_TARGET) == 0)
450 && unconditional(&e->arp)) {
451 unsigned int oldpos, size;
453 /* Return: backtrack through the last
457 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
459 pos = e->counters.pcnt;
460 e->counters.pcnt = 0;
462 /* We're at the start. */
466 e = (struct arpt_entry *)
467 (newinfo->entries + pos);
468 } while (oldpos == pos + e->next_offset);
471 size = e->next_offset;
472 e = (struct arpt_entry *)
473 (newinfo->entries + pos + size);
474 e->counters.pcnt = pos;
477 int newpos = t->verdict;
479 if (strcmp(t->target.u.user.name,
480 ARPT_STANDARD_TARGET) == 0
482 /* This a jump; chase it. */
483 duprintf("Jump rule %u -> %u\n",
486 /* ... this is a fallthru */
487 newpos = pos + e->next_offset;
489 e = (struct arpt_entry *)
490 (newinfo->entries + newpos);
491 e->counters.pcnt = pos;
496 duprintf("Finished chain %u\n", hook);
501 static inline int standard_check(const struct arpt_entry_target *t,
502 unsigned int max_offset)
504 struct arpt_standard_target *targ = (void *)t;
506 /* Check standard info. */
508 != ARPT_ALIGN(sizeof(struct arpt_standard_target))) {
509 duprintf("arpt_standard_check: target size %u != %Zu\n",
511 ARPT_ALIGN(sizeof(struct arpt_standard_target)));
515 if (targ->verdict >= 0
516 && targ->verdict > max_offset - sizeof(struct arpt_entry)) {
517 duprintf("arpt_standard_check: bad verdict (%i)\n",
522 if (targ->verdict < -NF_MAX_VERDICT - 1) {
523 duprintf("arpt_standard_check: bad negative verdict (%i)\n",
530 static struct arpt_target arpt_standard_target;
532 static inline int check_entry(struct arpt_entry *e, const char *name, unsigned int size,
535 struct arpt_entry_target *t;
536 struct arpt_target *target;
539 if (!arp_checkentry(&e->arp)) {
540 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
544 t = arpt_get_target(e);
545 target = arpt_find_target_lock(t->u.user.name, &ret, &arpt_mutex);
547 duprintf("check_entry: `%s' not found\n", t->u.user.name);
550 if (!try_module_get((target->me))) {
554 t->u.kernel.target = target;
557 if (t->u.kernel.target == &arpt_standard_target) {
558 if (!standard_check(t, size)) {
562 } else if (t->u.kernel.target->checkentry
563 && !t->u.kernel.target->checkentry(name, e, t->data,
567 module_put(t->u.kernel.target->me);
568 duprintf("arp_tables: check failed for `%s'.\n",
569 t->u.kernel.target->name);
583 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
584 struct arpt_table_info *newinfo,
586 unsigned char *limit,
587 const unsigned int *hook_entries,
588 const unsigned int *underflows,
593 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
594 || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
595 duprintf("Bad offset %p\n", e);
600 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
601 duprintf("checking: element %p size %u\n",
606 /* Check hooks & underflows */
607 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
608 if ((unsigned char *)e - base == hook_entries[h])
609 newinfo->hook_entry[h] = hook_entries[h];
610 if ((unsigned char *)e - base == underflows[h])
611 newinfo->underflow[h] = underflows[h];
614 /* FIXME: underflows must be unconditional, standard verdicts
615 < 0 (not ARPT_RETURN). --RR */
617 /* Clear counters and comefrom */
618 e->counters = ((struct arpt_counters) { 0, 0 });
625 static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
627 struct arpt_entry_target *t;
629 if (i && (*i)-- == 0)
632 t = arpt_get_target(e);
633 if (t->u.kernel.target->destroy)
634 t->u.kernel.target->destroy(t->data,
635 t->u.target_size - sizeof(*t));
636 module_put(t->u.kernel.target->me);
640 /* Checks and translates the user-supplied table segment (held in
643 static int translate_table(const char *name,
644 unsigned int valid_hooks,
645 struct arpt_table_info *newinfo,
648 const unsigned int *hook_entries,
649 const unsigned int *underflows)
654 newinfo->size = size;
655 newinfo->number = number;
657 /* Init all hooks to impossible value. */
658 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
659 newinfo->hook_entry[i] = 0xFFFFFFFF;
660 newinfo->underflow[i] = 0xFFFFFFFF;
663 duprintf("translate_table: size %u\n", newinfo->size);
666 /* Walk through entries, checking offsets. */
667 ret = ARPT_ENTRY_ITERATE(newinfo->entries, newinfo->size,
668 check_entry_size_and_hooks,
671 newinfo->entries + size,
672 hook_entries, underflows, &i);
673 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
678 duprintf("translate_table: %u not %u entries\n",
683 /* Check hooks all assigned */
684 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
685 /* Only hooks which are valid */
686 if (!(valid_hooks & (1 << i)))
688 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
689 duprintf("Invalid hook entry %u %u\n",
693 if (newinfo->underflow[i] == 0xFFFFFFFF) {
694 duprintf("Invalid underflow %u %u\n",
700 if (!mark_source_chains(newinfo, valid_hooks)) {
701 duprintf("Looping hook\n");
705 /* Finally, each sanity check must pass */
707 ret = ARPT_ENTRY_ITERATE(newinfo->entries, newinfo->size,
708 check_entry, name, size, &i);
711 ARPT_ENTRY_ITERATE(newinfo->entries, newinfo->size,
716 /* And one copy for every other CPU */
717 for (i = 1; i < NR_CPUS; i++) {
718 memcpy(newinfo->entries + SMP_ALIGN(newinfo->size)*i,
720 SMP_ALIGN(newinfo->size));
726 static struct arpt_table_info *replace_table(struct arpt_table *table,
727 unsigned int num_counters,
728 struct arpt_table_info *newinfo,
731 struct arpt_table_info *oldinfo;
733 /* Do the substitution. */
734 write_lock_bh(&table->lock);
735 /* Check inside lock: is the old number correct? */
736 if (num_counters != table->private->number) {
737 duprintf("num_counters != table->private->number (%u/%u)\n",
738 num_counters, table->private->number);
739 write_unlock_bh(&table->lock);
743 oldinfo = table->private;
744 table->private = newinfo;
745 newinfo->initial_entries = oldinfo->initial_entries;
746 write_unlock_bh(&table->lock);
752 static inline int add_entry_to_counter(const struct arpt_entry *e,
753 struct arpt_counters total[],
756 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
762 static void get_counters(const struct arpt_table_info *t,
763 struct arpt_counters counters[])
768 for (cpu = 0; cpu < NR_CPUS; cpu++) {
770 ARPT_ENTRY_ITERATE(t->entries + TABLE_OFFSET(t, cpu),
772 add_entry_to_counter,
778 static int copy_entries_to_user(unsigned int total_size,
779 struct arpt_table *table,
780 void __user *userptr)
782 unsigned int off, num, countersize;
783 struct arpt_entry *e;
784 struct arpt_counters *counters;
787 /* We need atomic snapshot of counters: rest doesn't change
788 * (other than comefrom, which userspace doesn't care
791 countersize = sizeof(struct arpt_counters) * table->private->number;
792 counters = vmalloc(countersize);
794 if (counters == NULL)
797 /* First, sum counters... */
798 memset(counters, 0, countersize);
799 write_lock_bh(&table->lock);
800 get_counters(table->private, counters);
801 write_unlock_bh(&table->lock);
803 /* ... then copy entire thing from CPU 0... */
804 if (copy_to_user(userptr, table->private->entries, total_size) != 0) {
809 /* FIXME: use iterator macros --RR */
810 /* ... then go back and fix counters and names */
811 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
812 struct arpt_entry_target *t;
814 e = (struct arpt_entry *)(table->private->entries + off);
815 if (copy_to_user(userptr + off
816 + offsetof(struct arpt_entry, counters),
818 sizeof(counters[num])) != 0) {
823 t = arpt_get_target(e);
824 if (copy_to_user(userptr + off + e->target_offset
825 + offsetof(struct arpt_entry_target,
827 t->u.kernel.target->name,
828 strlen(t->u.kernel.target->name)+1) != 0) {
839 static int get_entries(const struct arpt_get_entries *entries,
840 struct arpt_get_entries __user *uptr)
843 struct arpt_table *t;
845 t = arpt_find_table_lock(entries->name, &ret, &arpt_mutex);
847 duprintf("t->private->number = %u\n",
849 if (entries->size == t->private->size)
850 ret = copy_entries_to_user(t->private->size,
851 t, uptr->entrytable);
853 duprintf("get_entries: I've got %u not %u!\n",
860 duprintf("get_entries: Can't find %s!\n",
866 static int do_replace(void __user *user, unsigned int len)
869 struct arpt_replace tmp;
870 struct arpt_table *t;
871 struct arpt_table_info *newinfo, *oldinfo;
872 struct arpt_counters *counters;
874 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
877 /* Hack: Causes ipchains to give correct error msg --RR */
878 if (len != sizeof(tmp) + tmp.size)
881 /* Pedantry: prevent them from hitting BUG() in vmalloc.c --RR */
882 if ((SMP_ALIGN(tmp.size) >> PAGE_SHIFT) + 2 > num_physpages)
885 newinfo = vmalloc(sizeof(struct arpt_table_info)
886 + SMP_ALIGN(tmp.size) * NR_CPUS);
890 if (copy_from_user(newinfo->entries, user + sizeof(tmp),
896 counters = vmalloc(tmp.num_counters * sizeof(struct arpt_counters));
901 memset(counters, 0, tmp.num_counters * sizeof(struct arpt_counters));
903 ret = translate_table(tmp.name, tmp.valid_hooks,
904 newinfo, tmp.size, tmp.num_entries,
905 tmp.hook_entry, tmp.underflow);
907 goto free_newinfo_counters;
909 duprintf("arp_tables: Translated table\n");
911 t = arpt_find_table_lock(tmp.name, &ret, &arpt_mutex);
913 goto free_newinfo_counters_untrans;
916 if (tmp.valid_hooks != t->valid_hooks) {
917 duprintf("Valid hook crap: %08X vs %08X\n",
918 tmp.valid_hooks, t->valid_hooks);
920 goto free_newinfo_counters_untrans_unlock;
923 /* Get a reference in advance, we're not allowed fail later */
924 if (!try_module_get(t->me)) {
926 goto free_newinfo_counters_untrans_unlock;
929 oldinfo = replace_table(t, tmp.num_counters, newinfo, &ret);
933 /* Update module usage count based on number of rules */
934 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
935 oldinfo->number, oldinfo->initial_entries, newinfo->number);
936 if ((oldinfo->number > oldinfo->initial_entries) ||
937 (newinfo->number <= oldinfo->initial_entries))
939 if ((oldinfo->number > oldinfo->initial_entries) &&
940 (newinfo->number <= oldinfo->initial_entries))
943 /* Get the old counters. */
944 get_counters(oldinfo, counters);
945 /* Decrease module usage counts and free resource */
946 ARPT_ENTRY_ITERATE(oldinfo->entries, oldinfo->size, cleanup_entry,NULL);
948 /* Silent error: too late now. */
949 copy_to_user(tmp.counters, counters,
950 sizeof(struct arpt_counters) * tmp.num_counters);
957 free_newinfo_counters_untrans_unlock:
959 free_newinfo_counters_untrans:
960 ARPT_ENTRY_ITERATE(newinfo->entries, newinfo->size, cleanup_entry, NULL);
961 free_newinfo_counters:
968 /* We're lazy, and add to the first CPU; overflow works its fey magic
969 * and everything is OK.
971 static inline int add_counter_to_entry(struct arpt_entry *e,
972 const struct arpt_counters addme[],
976 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
982 static int do_add_counters(void __user *user, unsigned int len)
985 struct arpt_counters_info tmp, *paddc;
986 struct arpt_table *t;
989 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
992 if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct arpt_counters))
995 paddc = vmalloc(len);
999 if (copy_from_user(paddc, user, len) != 0) {
1004 t = arpt_find_table_lock(tmp.name, &ret, &arpt_mutex);
1008 write_lock_bh(&t->lock);
1009 if (t->private->number != paddc->num_counters) {
1011 goto unlock_up_free;
1015 ARPT_ENTRY_ITERATE(t->private->entries,
1017 add_counter_to_entry,
1021 write_unlock_bh(&t->lock);
1029 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1033 if (!capable(CAP_NET_ADMIN))
1037 case ARPT_SO_SET_REPLACE:
1038 ret = do_replace(user, len);
1041 case ARPT_SO_SET_ADD_COUNTERS:
1042 ret = do_add_counters(user, len);
1046 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1053 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1057 if (!capable(CAP_NET_ADMIN))
1061 case ARPT_SO_GET_INFO: {
1062 char name[ARPT_TABLE_MAXNAMELEN];
1063 struct arpt_table *t;
1065 if (*len != sizeof(struct arpt_getinfo)) {
1066 duprintf("length %u != %Zu\n", *len,
1067 sizeof(struct arpt_getinfo));
1072 if (copy_from_user(name, user, sizeof(name)) != 0) {
1076 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
1077 t = arpt_find_table_lock(name, &ret, &arpt_mutex);
1079 struct arpt_getinfo info;
1081 info.valid_hooks = t->valid_hooks;
1082 memcpy(info.hook_entry, t->private->hook_entry,
1083 sizeof(info.hook_entry));
1084 memcpy(info.underflow, t->private->underflow,
1085 sizeof(info.underflow));
1086 info.num_entries = t->private->number;
1087 info.size = t->private->size;
1088 strcpy(info.name, name);
1090 if (copy_to_user(user, &info, *len) != 0)
1100 case ARPT_SO_GET_ENTRIES: {
1101 struct arpt_get_entries get;
1103 if (*len < sizeof(get)) {
1104 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
1106 } else if (copy_from_user(&get, user, sizeof(get)) != 0) {
1108 } else if (*len != sizeof(struct arpt_get_entries) + get.size) {
1109 duprintf("get_entries: %u != %Zu\n", *len,
1110 sizeof(struct arpt_get_entries) + get.size);
1113 ret = get_entries(&get, user);
1118 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1125 /* Registration hooks for targets. */
1126 int arpt_register_target(struct arpt_target *target)
1130 ret = down_interruptible(&arpt_mutex);
1134 if (!list_named_insert(&arpt_target, target)) {
1135 duprintf("arpt_register_target: `%s' already in list!\n",
1143 void arpt_unregister_target(struct arpt_target *target)
1146 LIST_DELETE(&arpt_target, target);
1150 int arpt_register_table(struct arpt_table *table)
1153 struct arpt_table_info *newinfo;
1154 static struct arpt_table_info bootstrap
1155 = { 0, 0, 0, { 0 }, { 0 }, { } };
1157 newinfo = vmalloc(sizeof(struct arpt_table_info)
1158 + SMP_ALIGN(table->table->size) * NR_CPUS);
1163 memcpy(newinfo->entries, table->table->entries, table->table->size);
1165 ret = translate_table(table->name, table->valid_hooks,
1166 newinfo, table->table->size,
1167 table->table->num_entries,
1168 table->table->hook_entry,
1169 table->table->underflow);
1170 duprintf("arpt_register_table: translate table gives %d\n", ret);
1176 ret = down_interruptible(&arpt_mutex);
1182 /* Don't autoload: we'd eat our tail... */
1183 if (list_named_find(&arpt_tables, table->name)) {
1188 /* Simplifies replace_table code. */
1189 table->private = &bootstrap;
1190 if (!replace_table(table, 0, newinfo, &ret))
1193 duprintf("table->private->number = %u\n",
1194 table->private->number);
1196 /* save number of initial entries */
1197 table->private->initial_entries = table->private->number;
1199 table->lock = RW_LOCK_UNLOCKED;
1200 list_prepend(&arpt_tables, table);
1211 void arpt_unregister_table(struct arpt_table *table)
1214 LIST_DELETE(&arpt_tables, table);
1217 /* Decrease module usage counts and free resources */
1218 ARPT_ENTRY_ITERATE(table->private->entries, table->private->size,
1219 cleanup_entry, NULL);
1220 vfree(table->private);
1223 /* The built-in targets: standard (NULL) and error. */
1224 static struct arpt_target arpt_standard_target = {
1225 .name = ARPT_STANDARD_TARGET,
1228 static struct arpt_target arpt_error_target = {
1229 .name = ARPT_ERROR_TARGET,
1230 .target = arpt_error,
1233 static struct nf_sockopt_ops arpt_sockopts = {
1235 .set_optmin = ARPT_BASE_CTL,
1236 .set_optmax = ARPT_SO_SET_MAX+1,
1237 .set = do_arpt_set_ctl,
1238 .get_optmin = ARPT_BASE_CTL,
1239 .get_optmax = ARPT_SO_GET_MAX+1,
1240 .get = do_arpt_get_ctl,
1243 #ifdef CONFIG_PROC_FS
1244 static inline int print_name(const struct arpt_table *t,
1245 off_t start_offset, char *buffer, int length,
1246 off_t *pos, unsigned int *count)
1248 if ((*count)++ >= start_offset) {
1249 unsigned int namelen;
1251 namelen = sprintf(buffer + *pos, "%s\n", t->name);
1252 if (*pos + namelen > length) {
1253 /* Stop iterating */
1261 static int arpt_get_tables(char *buffer, char **start, off_t offset, int length)
1264 unsigned int count = 0;
1266 if (down_interruptible(&arpt_mutex) != 0)
1269 LIST_FIND(&arpt_tables, print_name, struct arpt_table *,
1270 offset, buffer, length, &pos, &count);
1274 /* `start' hack - see fs/proc/generic.c line ~105 */
1275 *start=(char *)((unsigned long)count-offset);
1278 #endif /*CONFIG_PROC_FS*/
1280 static int __init init(void)
1284 /* Noone else will be downing sem now, so we won't sleep */
1286 list_append(&arpt_target, &arpt_standard_target);
1287 list_append(&arpt_target, &arpt_error_target);
1290 /* Register setsockopt */
1291 ret = nf_register_sockopt(&arpt_sockopts);
1293 duprintf("Unable to register sockopts.\n");
1297 #ifdef CONFIG_PROC_FS
1299 struct proc_dir_entry *proc;
1301 proc = proc_net_create("arp_tables_names", 0, arpt_get_tables);
1303 nf_unregister_sockopt(&arpt_sockopts);
1306 proc->owner = THIS_MODULE;
1310 printk("arp_tables: (C) 2002 David S. Miller\n");
1314 static void __exit fini(void)
1316 nf_unregister_sockopt(&arpt_sockopts);
1317 #ifdef CONFIG_PROC_FS
1318 proc_net_remove("arp_tables_names");
1322 EXPORT_SYMBOL(arpt_register_table);
1323 EXPORT_SYMBOL(arpt_unregister_table);
1324 EXPORT_SYMBOL(arpt_do_table);
1325 EXPORT_SYMBOL(arpt_find_target_lock);
1326 EXPORT_SYMBOL(arpt_register_target);
1327 EXPORT_SYMBOL(arpt_unregister_target);