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/kernel.h>
13 #include <linux/skbuff.h>
14 #include <linux/netdevice.h>
15 #include <linux/capability.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 <linux/mutex.h>
26 #include <linux/netfilter/x_tables.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)
59 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
60 char *hdr_addr, int len)
64 if (len > ARPT_DEV_ADDR_LEN_MAX)
65 len = ARPT_DEV_ADDR_LEN_MAX;
68 for (i = 0; i < len; i++)
69 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
74 /* Returns whether packet matches rule or not. */
75 static inline int arp_packet_match(const struct arphdr *arphdr,
76 struct net_device *dev,
79 const struct arpt_arp *arpinfo)
81 char *arpptr = (char *)(arphdr + 1);
82 char *src_devaddr, *tgt_devaddr;
83 __be32 src_ipaddr, tgt_ipaddr;
86 #define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
88 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
90 dprintf("ARP operation field mismatch.\n");
91 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
92 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
96 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
98 dprintf("ARP hardware address format mismatch.\n");
99 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
100 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
104 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
106 dprintf("ARP protocol address format mismatch.\n");
107 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
108 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
112 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
114 dprintf("ARP hardware address length mismatch.\n");
115 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
116 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
120 src_devaddr = arpptr;
121 arpptr += dev->addr_len;
122 memcpy(&src_ipaddr, arpptr, sizeof(u32));
123 arpptr += sizeof(u32);
124 tgt_devaddr = arpptr;
125 arpptr += dev->addr_len;
126 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
128 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
129 ARPT_INV_SRCDEVADDR) ||
130 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
131 ARPT_INV_TGTDEVADDR)) {
132 dprintf("Source or target device address mismatch.\n");
137 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
139 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
141 dprintf("Source or target IP address mismatch.\n");
143 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
145 NIPQUAD(arpinfo->smsk.s_addr),
146 NIPQUAD(arpinfo->src.s_addr),
147 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
148 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
150 NIPQUAD(arpinfo->tmsk.s_addr),
151 NIPQUAD(arpinfo->tgt.s_addr),
152 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
156 /* Look for ifname matches. */
157 for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
158 ret |= (indev[i] ^ arpinfo->iniface[i])
159 & arpinfo->iniface_mask[i];
162 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
163 dprintf("VIA in mismatch (%s vs %s).%s\n",
164 indev, arpinfo->iniface,
165 arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
169 for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
170 ret |= (outdev[i] ^ arpinfo->outiface[i])
171 & arpinfo->outiface_mask[i];
174 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
175 dprintf("VIA out mismatch (%s vs %s).%s\n",
176 outdev, arpinfo->outiface,
177 arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
184 static inline int arp_checkentry(const struct arpt_arp *arp)
186 if (arp->flags & ~ARPT_F_MASK) {
187 duprintf("Unknown flag bits set: %08X\n",
188 arp->flags & ~ARPT_F_MASK);
191 if (arp->invflags & ~ARPT_INV_MASK) {
192 duprintf("Unknown invflag bits set: %08X\n",
193 arp->invflags & ~ARPT_INV_MASK);
200 static unsigned int arpt_error(struct sk_buff *skb,
201 const struct net_device *in,
202 const struct net_device *out,
203 unsigned int hooknum,
204 const struct xt_target *target,
205 const void *targinfo)
208 printk("arp_tables: error: '%s'\n", (char *)targinfo);
213 static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
215 return (struct arpt_entry *)(base + offset);
218 unsigned int arpt_do_table(struct sk_buff *skb,
220 const struct net_device *in,
221 const struct net_device *out,
222 struct arpt_table *table)
224 static const char nulldevname[IFNAMSIZ];
225 unsigned int verdict = NF_DROP;
227 bool hotdrop = false;
228 struct arpt_entry *e, *back;
229 const char *indev, *outdev;
231 struct xt_table_info *private;
233 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
234 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
235 (2 * skb->dev->addr_len) +
239 indev = in ? in->name : nulldevname;
240 outdev = out ? out->name : nulldevname;
242 read_lock_bh(&table->lock);
243 private = table->private;
244 table_base = (void *)private->entries[smp_processor_id()];
245 e = get_entry(table_base, private->hook_entry[hook]);
246 back = get_entry(table_base, private->underflow[hook]);
250 if (arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
251 struct arpt_entry_target *t;
254 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
255 (2 * skb->dev->addr_len);
256 ADD_COUNTER(e->counters, hdr_len, 1);
258 t = arpt_get_target(e);
260 /* Standard target? */
261 if (!t->u.kernel.target->target) {
264 v = ((struct arpt_standard_target *)t)->verdict;
266 /* Pop from stack? */
267 if (v != ARPT_RETURN) {
268 verdict = (unsigned)(-v) - 1;
272 back = get_entry(table_base,
277 != (void *)e + e->next_offset) {
278 /* Save old back ptr in next entry */
279 struct arpt_entry *next
280 = (void *)e + e->next_offset;
282 (void *)back - table_base;
284 /* set back pointer to next entry */
288 e = get_entry(table_base, v);
290 /* Targets which reenter must return
293 verdict = t->u.kernel.target->target(skb,
299 /* Target might have changed stuff. */
302 if (verdict == ARPT_CONTINUE)
303 e = (void *)e + e->next_offset;
309 e = (void *)e + e->next_offset;
312 read_unlock_bh(&table->lock);
320 /* All zeroes == unconditional rule. */
321 static inline int unconditional(const struct arpt_arp *arp)
325 for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
326 if (((__u32 *)arp)[i])
332 /* Figures out from what hook each rule can be called: returns 0 if
333 * there are loops. Puts hook bitmask in comefrom.
335 static int mark_source_chains(struct xt_table_info *newinfo,
336 unsigned int valid_hooks, void *entry0)
340 /* No recursion; use packet counter to save back ptrs (reset
341 * to 0 as we leave), and comefrom to save source hook bitmask.
343 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
344 unsigned int pos = newinfo->hook_entry[hook];
346 = (struct arpt_entry *)(entry0 + pos);
348 if (!(valid_hooks & (1 << hook)))
351 /* Set initial back pointer. */
352 e->counters.pcnt = pos;
355 struct arpt_standard_target *t
356 = (void *)arpt_get_target(e);
357 int visited = e->comefrom & (1 << hook);
359 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
360 printk("arptables: loop hook %u pos %u %08X.\n",
361 hook, pos, e->comefrom);
365 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
367 /* Unconditional return/END. */
368 if ((e->target_offset == sizeof(struct arpt_entry)
369 && (strcmp(t->target.u.user.name,
370 ARPT_STANDARD_TARGET) == 0)
372 && unconditional(&e->arp)) || visited) {
373 unsigned int oldpos, size;
375 if (t->verdict < -NF_MAX_VERDICT - 1) {
376 duprintf("mark_source_chains: bad "
377 "negative verdict (%i)\n",
382 /* Return: backtrack through the last
386 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
388 pos = e->counters.pcnt;
389 e->counters.pcnt = 0;
391 /* We're at the start. */
395 e = (struct arpt_entry *)
397 } while (oldpos == pos + e->next_offset);
400 size = e->next_offset;
401 e = (struct arpt_entry *)
402 (entry0 + pos + size);
403 e->counters.pcnt = pos;
406 int newpos = t->verdict;
408 if (strcmp(t->target.u.user.name,
409 ARPT_STANDARD_TARGET) == 0
411 if (newpos > newinfo->size -
412 sizeof(struct arpt_entry)) {
413 duprintf("mark_source_chains: "
414 "bad verdict (%i)\n",
419 /* This a jump; chase it. */
420 duprintf("Jump rule %u -> %u\n",
423 /* ... this is a fallthru */
424 newpos = pos + e->next_offset;
426 e = (struct arpt_entry *)
428 e->counters.pcnt = pos;
433 duprintf("Finished chain %u\n", hook);
438 static inline int check_entry(struct arpt_entry *e, const char *name)
440 struct arpt_entry_target *t;
442 if (!arp_checkentry(&e->arp)) {
443 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
447 if (e->target_offset + sizeof(struct arpt_entry_target) > e->next_offset)
450 t = arpt_get_target(e);
451 if (e->target_offset + t->u.target_size > e->next_offset)
457 static inline int check_target(struct arpt_entry *e, const char *name)
459 struct arpt_entry_target *t;
460 struct arpt_target *target;
463 t = arpt_get_target(e);
464 target = t->u.kernel.target;
466 ret = xt_check_target(target, NF_ARP, t->u.target_size - sizeof(*t),
467 name, e->comefrom, 0, 0);
468 if (!ret && t->u.kernel.target->checkentry
469 && !t->u.kernel.target->checkentry(name, e, target, t->data,
471 duprintf("arp_tables: check failed for `%s'.\n",
472 t->u.kernel.target->name);
479 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
482 struct arpt_entry_target *t;
483 struct arpt_target *target;
486 ret = check_entry(e, name);
490 t = arpt_get_target(e);
491 target = try_then_request_module(xt_find_target(NF_ARP, t->u.user.name,
493 "arpt_%s", t->u.user.name);
494 if (IS_ERR(target) || !target) {
495 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
496 ret = target ? PTR_ERR(target) : -ENOENT;
499 t->u.kernel.target = target;
501 ret = check_target(e, name);
508 module_put(t->u.kernel.target->me);
513 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
514 struct xt_table_info *newinfo,
516 unsigned char *limit,
517 const unsigned int *hook_entries,
518 const unsigned int *underflows,
523 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
524 || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
525 duprintf("Bad offset %p\n", e);
530 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
531 duprintf("checking: element %p size %u\n",
536 /* Check hooks & underflows */
537 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
538 if ((unsigned char *)e - base == hook_entries[h])
539 newinfo->hook_entry[h] = hook_entries[h];
540 if ((unsigned char *)e - base == underflows[h])
541 newinfo->underflow[h] = underflows[h];
544 /* FIXME: underflows must be unconditional, standard verdicts
545 < 0 (not ARPT_RETURN). --RR */
547 /* Clear counters and comefrom */
548 e->counters = ((struct xt_counters) { 0, 0 });
555 static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
557 struct arpt_entry_target *t;
559 if (i && (*i)-- == 0)
562 t = arpt_get_target(e);
563 if (t->u.kernel.target->destroy)
564 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
565 module_put(t->u.kernel.target->me);
569 /* Checks and translates the user-supplied table segment (held in
572 static int translate_table(const char *name,
573 unsigned int valid_hooks,
574 struct xt_table_info *newinfo,
578 const unsigned int *hook_entries,
579 const unsigned int *underflows)
584 newinfo->size = size;
585 newinfo->number = number;
587 /* Init all hooks to impossible value. */
588 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
589 newinfo->hook_entry[i] = 0xFFFFFFFF;
590 newinfo->underflow[i] = 0xFFFFFFFF;
593 duprintf("translate_table: size %u\n", newinfo->size);
596 /* Walk through entries, checking offsets. */
597 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
598 check_entry_size_and_hooks,
602 hook_entries, underflows, &i);
603 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
608 duprintf("translate_table: %u not %u entries\n",
613 /* Check hooks all assigned */
614 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
615 /* Only hooks which are valid */
616 if (!(valid_hooks & (1 << i)))
618 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
619 duprintf("Invalid hook entry %u %u\n",
623 if (newinfo->underflow[i] == 0xFFFFFFFF) {
624 duprintf("Invalid underflow %u %u\n",
630 if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
631 duprintf("Looping hook\n");
635 /* Finally, each sanity check must pass */
637 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
638 find_check_entry, name, size, &i);
641 ARPT_ENTRY_ITERATE(entry0, newinfo->size,
646 /* And one copy for every other CPU */
647 for_each_possible_cpu(i) {
648 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
649 memcpy(newinfo->entries[i], entry0, newinfo->size);
656 static inline int add_entry_to_counter(const struct arpt_entry *e,
657 struct xt_counters total[],
660 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
666 static inline int set_entry_to_counter(const struct arpt_entry *e,
667 struct xt_counters total[],
670 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
676 static void get_counters(const struct xt_table_info *t,
677 struct xt_counters counters[])
683 /* Instead of clearing (by a previous call to memset())
684 * the counters and using adds, we set the counters
685 * with data used by 'current' CPU
686 * We dont care about preemption here.
688 curcpu = raw_smp_processor_id();
691 ARPT_ENTRY_ITERATE(t->entries[curcpu],
693 set_entry_to_counter,
697 for_each_possible_cpu(cpu) {
701 ARPT_ENTRY_ITERATE(t->entries[cpu],
703 add_entry_to_counter,
709 static inline struct xt_counters *alloc_counters(struct arpt_table *table)
711 unsigned int countersize;
712 struct xt_counters *counters;
713 struct xt_table_info *private = table->private;
715 /* We need atomic snapshot of counters: rest doesn't change
716 * (other than comefrom, which userspace doesn't care
719 countersize = sizeof(struct xt_counters) * private->number;
720 counters = vmalloc_node(countersize, numa_node_id());
722 if (counters == NULL)
723 return ERR_PTR(-ENOMEM);
725 /* First, sum counters... */
726 write_lock_bh(&table->lock);
727 get_counters(private, counters);
728 write_unlock_bh(&table->lock);
733 static int copy_entries_to_user(unsigned int total_size,
734 struct arpt_table *table,
735 void __user *userptr)
737 unsigned int off, num;
738 struct arpt_entry *e;
739 struct xt_counters *counters;
740 struct xt_table_info *private = table->private;
744 counters = alloc_counters(table);
745 if (IS_ERR(counters))
746 return PTR_ERR(counters);
748 loc_cpu_entry = private->entries[raw_smp_processor_id()];
749 /* ... then copy entire thing ... */
750 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
755 /* FIXME: use iterator macros --RR */
756 /* ... then go back and fix counters and names */
757 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
758 struct arpt_entry_target *t;
760 e = (struct arpt_entry *)(loc_cpu_entry + off);
761 if (copy_to_user(userptr + off
762 + offsetof(struct arpt_entry, counters),
764 sizeof(counters[num])) != 0) {
769 t = arpt_get_target(e);
770 if (copy_to_user(userptr + off + e->target_offset
771 + offsetof(struct arpt_entry_target,
773 t->u.kernel.target->name,
774 strlen(t->u.kernel.target->name)+1) != 0) {
785 static int get_info(void __user *user, int *len)
787 char name[ARPT_TABLE_MAXNAMELEN];
788 struct arpt_table *t;
791 if (*len != sizeof(struct arpt_getinfo)) {
792 duprintf("length %u != %Zu\n", *len,
793 sizeof(struct arpt_getinfo));
797 if (copy_from_user(name, user, sizeof(name)) != 0)
800 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
802 t = try_then_request_module(xt_find_table_lock(NF_ARP, name),
803 "arptable_%s", name);
804 if (t && !IS_ERR(t)) {
805 struct arpt_getinfo info;
806 struct xt_table_info *private = t->private;
808 info.valid_hooks = t->valid_hooks;
809 memcpy(info.hook_entry, private->hook_entry,
810 sizeof(info.hook_entry));
811 memcpy(info.underflow, private->underflow,
812 sizeof(info.underflow));
813 info.num_entries = private->number;
814 info.size = private->size;
815 strcpy(info.name, name);
817 if (copy_to_user(user, &info, *len) != 0)
824 ret = t ? PTR_ERR(t) : -ENOENT;
828 static int get_entries(const struct arpt_get_entries *entries,
829 struct arpt_get_entries __user *uptr)
832 struct arpt_table *t;
834 t = xt_find_table_lock(NF_ARP, entries->name);
835 if (t && !IS_ERR(t)) {
836 struct xt_table_info *private = t->private;
837 duprintf("t->private->number = %u\n",
839 if (entries->size == private->size)
840 ret = copy_entries_to_user(private->size,
841 t, uptr->entrytable);
843 duprintf("get_entries: I've got %u not %u!\n",
844 private->size, entries->size);
850 ret = t ? PTR_ERR(t) : -ENOENT;
855 static int do_replace(void __user *user, unsigned int len)
858 struct arpt_replace tmp;
859 struct arpt_table *t;
860 struct xt_table_info *newinfo, *oldinfo;
861 struct xt_counters *counters;
862 void *loc_cpu_entry, *loc_cpu_old_entry;
864 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
868 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
871 newinfo = xt_alloc_table_info(tmp.size);
875 /* choose the copy that is on our node/cpu */
876 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
877 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
883 counters = vmalloc_node(tmp.num_counters * sizeof(struct xt_counters),
890 ret = translate_table(tmp.name, tmp.valid_hooks,
891 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
892 tmp.hook_entry, tmp.underflow);
894 goto free_newinfo_counters;
896 duprintf("arp_tables: Translated table\n");
898 t = try_then_request_module(xt_find_table_lock(NF_ARP, tmp.name),
899 "arptable_%s", tmp.name);
900 if (!t || IS_ERR(t)) {
901 ret = t ? PTR_ERR(t) : -ENOENT;
902 goto free_newinfo_counters_untrans;
906 if (tmp.valid_hooks != t->valid_hooks) {
907 duprintf("Valid hook crap: %08X vs %08X\n",
908 tmp.valid_hooks, t->valid_hooks);
913 oldinfo = xt_replace_table(t, tmp.num_counters, newinfo, &ret);
917 /* Update module usage count based on number of rules */
918 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
919 oldinfo->number, oldinfo->initial_entries, newinfo->number);
920 if ((oldinfo->number > oldinfo->initial_entries) ||
921 (newinfo->number <= oldinfo->initial_entries))
923 if ((oldinfo->number > oldinfo->initial_entries) &&
924 (newinfo->number <= oldinfo->initial_entries))
927 /* Get the old counters. */
928 get_counters(oldinfo, counters);
929 /* Decrease module usage counts and free resource */
930 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
931 ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL);
933 xt_free_table_info(oldinfo);
934 if (copy_to_user(tmp.counters, counters,
935 sizeof(struct xt_counters) * tmp.num_counters) != 0)
944 free_newinfo_counters_untrans:
945 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
946 free_newinfo_counters:
949 xt_free_table_info(newinfo);
953 /* We're lazy, and add to the first CPU; overflow works its fey magic
954 * and everything is OK.
956 static inline int add_counter_to_entry(struct arpt_entry *e,
957 const struct xt_counters addme[],
961 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
967 static int do_add_counters(void __user *user, unsigned int len)
970 struct xt_counters_info tmp, *paddc;
971 struct arpt_table *t;
972 struct xt_table_info *private;
976 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
979 if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct xt_counters))
982 paddc = vmalloc_node(len, numa_node_id());
986 if (copy_from_user(paddc, user, len) != 0) {
991 t = xt_find_table_lock(NF_ARP, tmp.name);
992 if (!t || IS_ERR(t)) {
993 ret = t ? PTR_ERR(t) : -ENOENT;
997 write_lock_bh(&t->lock);
998 private = t->private;
999 if (private->number != tmp.num_counters) {
1001 goto unlock_up_free;
1005 /* Choose the copy that is on our node */
1006 loc_cpu_entry = private->entries[smp_processor_id()];
1007 ARPT_ENTRY_ITERATE(loc_cpu_entry,
1009 add_counter_to_entry,
1013 write_unlock_bh(&t->lock);
1022 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1026 if (!capable(CAP_NET_ADMIN))
1030 case ARPT_SO_SET_REPLACE:
1031 ret = do_replace(user, len);
1034 case ARPT_SO_SET_ADD_COUNTERS:
1035 ret = do_add_counters(user, len);
1039 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1046 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1050 if (!capable(CAP_NET_ADMIN))
1054 case ARPT_SO_GET_INFO:
1055 ret = get_info(user, len);
1058 case ARPT_SO_GET_ENTRIES: {
1059 struct arpt_get_entries get;
1061 if (*len < sizeof(get)) {
1062 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
1064 } else if (copy_from_user(&get, user, sizeof(get)) != 0) {
1066 } else if (*len != sizeof(struct arpt_get_entries) + get.size) {
1067 duprintf("get_entries: %u != %Zu\n", *len,
1068 sizeof(struct arpt_get_entries) + get.size);
1071 ret = get_entries(&get, user);
1075 case ARPT_SO_GET_REVISION_TARGET: {
1076 struct xt_get_revision rev;
1078 if (*len != sizeof(rev)) {
1082 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1087 try_then_request_module(xt_find_revision(NF_ARP, rev.name,
1088 rev.revision, 1, &ret),
1089 "arpt_%s", rev.name);
1094 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1101 int arpt_register_table(struct arpt_table *table,
1102 const struct arpt_replace *repl)
1105 struct xt_table_info *newinfo;
1106 struct xt_table_info bootstrap
1107 = { 0, 0, 0, { 0 }, { 0 }, { } };
1108 void *loc_cpu_entry;
1110 newinfo = xt_alloc_table_info(repl->size);
1116 /* choose the copy on our node/cpu */
1117 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1118 memcpy(loc_cpu_entry, repl->entries, repl->size);
1120 ret = translate_table(table->name, table->valid_hooks,
1121 newinfo, loc_cpu_entry, repl->size,
1126 duprintf("arpt_register_table: translate table gives %d\n", ret);
1128 xt_free_table_info(newinfo);
1132 ret = xt_register_table(table, &bootstrap, newinfo);
1134 xt_free_table_info(newinfo);
1141 void arpt_unregister_table(struct arpt_table *table)
1143 struct xt_table_info *private;
1144 void *loc_cpu_entry;
1146 private = xt_unregister_table(table);
1148 /* Decrease module usage counts and free resources */
1149 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1150 ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size,
1151 cleanup_entry, NULL);
1152 xt_free_table_info(private);
1155 /* The built-in targets: standard (NULL) and error. */
1156 static struct arpt_target arpt_standard_target __read_mostly = {
1157 .name = ARPT_STANDARD_TARGET,
1158 .targetsize = sizeof(int),
1162 static struct arpt_target arpt_error_target __read_mostly = {
1163 .name = ARPT_ERROR_TARGET,
1164 .target = arpt_error,
1165 .targetsize = ARPT_FUNCTION_MAXNAMELEN,
1169 static struct nf_sockopt_ops arpt_sockopts = {
1171 .set_optmin = ARPT_BASE_CTL,
1172 .set_optmax = ARPT_SO_SET_MAX+1,
1173 .set = do_arpt_set_ctl,
1174 .get_optmin = ARPT_BASE_CTL,
1175 .get_optmax = ARPT_SO_GET_MAX+1,
1176 .get = do_arpt_get_ctl,
1177 .owner = THIS_MODULE,
1180 static int __init arp_tables_init(void)
1184 ret = xt_proto_init(NF_ARP);
1188 /* Noone else will be downing sem now, so we won't sleep */
1189 ret = xt_register_target(&arpt_standard_target);
1192 ret = xt_register_target(&arpt_error_target);
1196 /* Register setsockopt */
1197 ret = nf_register_sockopt(&arpt_sockopts);
1201 printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1205 xt_unregister_target(&arpt_error_target);
1207 xt_unregister_target(&arpt_standard_target);
1209 xt_proto_fini(NF_ARP);
1214 static void __exit arp_tables_fini(void)
1216 nf_unregister_sockopt(&arpt_sockopts);
1217 xt_unregister_target(&arpt_error_target);
1218 xt_unregister_target(&arpt_standard_target);
1219 xt_proto_fini(NF_ARP);
1222 EXPORT_SYMBOL(arpt_register_table);
1223 EXPORT_SYMBOL(arpt_unregister_table);
1224 EXPORT_SYMBOL(arpt_do_table);
1226 module_init(arp_tables_init);
1227 module_exit(arp_tables_fini);