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>
22 #include <linux/mutex.h>
23 #include <linux/err.h>
24 #include <net/compat.h>
25 #include <asm/uaccess.h>
27 #include <linux/netfilter/x_tables.h>
28 #include <linux/netfilter_arp/arp_tables.h>
30 MODULE_LICENSE("GPL");
31 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
32 MODULE_DESCRIPTION("arptables core");
34 /*#define DEBUG_ARP_TABLES*/
35 /*#define DEBUG_ARP_TABLES_USER*/
37 #ifdef DEBUG_ARP_TABLES
38 #define dprintf(format, args...) printk(format , ## args)
40 #define dprintf(format, args...)
43 #ifdef DEBUG_ARP_TABLES_USER
44 #define duprintf(format, args...) printk(format , ## args)
46 #define duprintf(format, args...)
49 #ifdef CONFIG_NETFILTER_DEBUG
50 #define ARP_NF_ASSERT(x) \
53 printk("ARP_NF_ASSERT: %s:%s:%u\n", \
54 __FUNCTION__, __FILE__, __LINE__); \
57 #define ARP_NF_ASSERT(x)
60 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
61 char *hdr_addr, int len)
65 if (len > ARPT_DEV_ADDR_LEN_MAX)
66 len = ARPT_DEV_ADDR_LEN_MAX;
69 for (i = 0; i < len; i++)
70 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
75 /* Returns whether packet matches rule or not. */
76 static inline int arp_packet_match(const struct arphdr *arphdr,
77 struct net_device *dev,
80 const struct arpt_arp *arpinfo)
82 char *arpptr = (char *)(arphdr + 1);
83 char *src_devaddr, *tgt_devaddr;
84 __be32 src_ipaddr, tgt_ipaddr;
87 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
89 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
91 dprintf("ARP operation field mismatch.\n");
92 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
93 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
97 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
99 dprintf("ARP hardware address format mismatch.\n");
100 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
101 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
105 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
107 dprintf("ARP protocol address format mismatch.\n");
108 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
109 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
113 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
115 dprintf("ARP hardware address length mismatch.\n");
116 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
117 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
121 src_devaddr = arpptr;
122 arpptr += dev->addr_len;
123 memcpy(&src_ipaddr, arpptr, sizeof(u32));
124 arpptr += sizeof(u32);
125 tgt_devaddr = arpptr;
126 arpptr += dev->addr_len;
127 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
129 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
130 ARPT_INV_SRCDEVADDR) ||
131 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
132 ARPT_INV_TGTDEVADDR)) {
133 dprintf("Source or target device address mismatch.\n");
138 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
140 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
142 dprintf("Source or target IP address mismatch.\n");
144 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
146 NIPQUAD(arpinfo->smsk.s_addr),
147 NIPQUAD(arpinfo->src.s_addr),
148 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
149 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
151 NIPQUAD(arpinfo->tmsk.s_addr),
152 NIPQUAD(arpinfo->tgt.s_addr),
153 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
157 /* Look for ifname matches. */
158 for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
159 ret |= (indev[i] ^ arpinfo->iniface[i])
160 & arpinfo->iniface_mask[i];
163 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
164 dprintf("VIA in mismatch (%s vs %s).%s\n",
165 indev, arpinfo->iniface,
166 arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
170 for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
171 ret |= (outdev[i] ^ arpinfo->outiface[i])
172 & arpinfo->outiface_mask[i];
175 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
176 dprintf("VIA out mismatch (%s vs %s).%s\n",
177 outdev, arpinfo->outiface,
178 arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
186 static inline int arp_checkentry(const struct arpt_arp *arp)
188 if (arp->flags & ~ARPT_F_MASK) {
189 duprintf("Unknown flag bits set: %08X\n",
190 arp->flags & ~ARPT_F_MASK);
193 if (arp->invflags & ~ARPT_INV_MASK) {
194 duprintf("Unknown invflag bits set: %08X\n",
195 arp->invflags & ~ARPT_INV_MASK);
202 static unsigned int arpt_error(struct sk_buff *skb,
203 const struct net_device *in,
204 const struct net_device *out,
205 unsigned int hooknum,
206 const struct xt_target *target,
207 const void *targinfo)
210 printk("arp_tables: error: '%s'\n", (char *)targinfo);
215 static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
217 return (struct arpt_entry *)(base + offset);
220 unsigned int arpt_do_table(struct sk_buff *skb,
222 const struct net_device *in,
223 const struct net_device *out,
224 struct arpt_table *table)
226 static const char nulldevname[IFNAMSIZ];
227 unsigned int verdict = NF_DROP;
229 bool hotdrop = false;
230 struct arpt_entry *e, *back;
231 const char *indev, *outdev;
233 struct xt_table_info *private;
235 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
236 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
237 (2 * skb->dev->addr_len) +
241 indev = in ? in->name : nulldevname;
242 outdev = out ? out->name : nulldevname;
244 read_lock_bh(&table->lock);
245 private = table->private;
246 table_base = (void *)private->entries[smp_processor_id()];
247 e = get_entry(table_base, private->hook_entry[hook]);
248 back = get_entry(table_base, private->underflow[hook]);
252 if (arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
253 struct arpt_entry_target *t;
256 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
257 (2 * skb->dev->addr_len);
258 ADD_COUNTER(e->counters, hdr_len, 1);
260 t = arpt_get_target(e);
262 /* Standard target? */
263 if (!t->u.kernel.target->target) {
266 v = ((struct arpt_standard_target *)t)->verdict;
268 /* Pop from stack? */
269 if (v != ARPT_RETURN) {
270 verdict = (unsigned)(-v) - 1;
274 back = get_entry(table_base,
279 != (void *)e + e->next_offset) {
280 /* Save old back ptr in next entry */
281 struct arpt_entry *next
282 = (void *)e + e->next_offset;
284 (void *)back - table_base;
286 /* set back pointer to next entry */
290 e = get_entry(table_base, v);
292 /* Targets which reenter must return
295 verdict = t->u.kernel.target->target(skb,
301 /* Target might have changed stuff. */
304 if (verdict == ARPT_CONTINUE)
305 e = (void *)e + e->next_offset;
311 e = (void *)e + e->next_offset;
314 read_unlock_bh(&table->lock);
322 /* All zeroes == unconditional rule. */
323 static inline int unconditional(const struct arpt_arp *arp)
327 for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
328 if (((__u32 *)arp)[i])
334 /* Figures out from what hook each rule can be called: returns 0 if
335 * there are loops. Puts hook bitmask in comefrom.
337 static int mark_source_chains(struct xt_table_info *newinfo,
338 unsigned int valid_hooks, void *entry0)
342 /* No recursion; use packet counter to save back ptrs (reset
343 * to 0 as we leave), and comefrom to save source hook bitmask.
345 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
346 unsigned int pos = newinfo->hook_entry[hook];
348 = (struct arpt_entry *)(entry0 + pos);
350 if (!(valid_hooks & (1 << hook)))
353 /* Set initial back pointer. */
354 e->counters.pcnt = pos;
357 struct arpt_standard_target *t
358 = (void *)arpt_get_target(e);
359 int visited = e->comefrom & (1 << hook);
361 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
362 printk("arptables: loop hook %u pos %u %08X.\n",
363 hook, pos, e->comefrom);
367 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
369 /* Unconditional return/END. */
370 if ((e->target_offset == sizeof(struct arpt_entry)
371 && (strcmp(t->target.u.user.name,
372 ARPT_STANDARD_TARGET) == 0)
374 && unconditional(&e->arp)) || visited) {
375 unsigned int oldpos, size;
377 if (t->verdict < -NF_MAX_VERDICT - 1) {
378 duprintf("mark_source_chains: bad "
379 "negative verdict (%i)\n",
384 /* Return: backtrack through the last
388 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
390 pos = e->counters.pcnt;
391 e->counters.pcnt = 0;
393 /* We're at the start. */
397 e = (struct arpt_entry *)
399 } while (oldpos == pos + e->next_offset);
402 size = e->next_offset;
403 e = (struct arpt_entry *)
404 (entry0 + pos + size);
405 e->counters.pcnt = pos;
408 int newpos = t->verdict;
410 if (strcmp(t->target.u.user.name,
411 ARPT_STANDARD_TARGET) == 0
413 if (newpos > newinfo->size -
414 sizeof(struct arpt_entry)) {
415 duprintf("mark_source_chains: "
416 "bad verdict (%i)\n",
421 /* This a jump; chase it. */
422 duprintf("Jump rule %u -> %u\n",
425 /* ... this is a fallthru */
426 newpos = pos + e->next_offset;
428 e = (struct arpt_entry *)
430 e->counters.pcnt = pos;
435 duprintf("Finished chain %u\n", hook);
440 static inline int check_entry(struct arpt_entry *e, const char *name)
442 struct arpt_entry_target *t;
444 if (!arp_checkentry(&e->arp)) {
445 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
449 if (e->target_offset + sizeof(struct arpt_entry_target) > e->next_offset)
452 t = arpt_get_target(e);
453 if (e->target_offset + t->u.target_size > e->next_offset)
459 static inline int check_target(struct arpt_entry *e, const char *name)
461 struct arpt_entry_target *t;
462 struct arpt_target *target;
465 t = arpt_get_target(e);
466 target = t->u.kernel.target;
468 ret = xt_check_target(target, NF_ARP, t->u.target_size - sizeof(*t),
469 name, e->comefrom, 0, 0);
470 if (!ret && t->u.kernel.target->checkentry
471 && !t->u.kernel.target->checkentry(name, e, target, t->data,
473 duprintf("arp_tables: check failed for `%s'.\n",
474 t->u.kernel.target->name);
481 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
484 struct arpt_entry_target *t;
485 struct arpt_target *target;
488 ret = check_entry(e, name);
492 t = arpt_get_target(e);
493 target = try_then_request_module(xt_find_target(NF_ARP, t->u.user.name,
495 "arpt_%s", t->u.user.name);
496 if (IS_ERR(target) || !target) {
497 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
498 ret = target ? PTR_ERR(target) : -ENOENT;
501 t->u.kernel.target = target;
503 ret = check_target(e, name);
510 module_put(t->u.kernel.target->me);
515 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
516 struct xt_table_info *newinfo,
518 unsigned char *limit,
519 const unsigned int *hook_entries,
520 const unsigned int *underflows,
525 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
526 || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
527 duprintf("Bad offset %p\n", e);
532 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
533 duprintf("checking: element %p size %u\n",
538 /* Check hooks & underflows */
539 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
540 if ((unsigned char *)e - base == hook_entries[h])
541 newinfo->hook_entry[h] = hook_entries[h];
542 if ((unsigned char *)e - base == underflows[h])
543 newinfo->underflow[h] = underflows[h];
546 /* FIXME: underflows must be unconditional, standard verdicts
547 < 0 (not ARPT_RETURN). --RR */
549 /* Clear counters and comefrom */
550 e->counters = ((struct xt_counters) { 0, 0 });
557 static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
559 struct arpt_entry_target *t;
561 if (i && (*i)-- == 0)
564 t = arpt_get_target(e);
565 if (t->u.kernel.target->destroy)
566 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
567 module_put(t->u.kernel.target->me);
571 /* Checks and translates the user-supplied table segment (held in
574 static int translate_table(const char *name,
575 unsigned int valid_hooks,
576 struct xt_table_info *newinfo,
580 const unsigned int *hook_entries,
581 const unsigned int *underflows)
586 newinfo->size = size;
587 newinfo->number = number;
589 /* Init all hooks to impossible value. */
590 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
591 newinfo->hook_entry[i] = 0xFFFFFFFF;
592 newinfo->underflow[i] = 0xFFFFFFFF;
595 duprintf("translate_table: size %u\n", newinfo->size);
598 /* Walk through entries, checking offsets. */
599 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
600 check_entry_size_and_hooks,
604 hook_entries, underflows, &i);
605 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
610 duprintf("translate_table: %u not %u entries\n",
615 /* Check hooks all assigned */
616 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
617 /* Only hooks which are valid */
618 if (!(valid_hooks & (1 << i)))
620 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
621 duprintf("Invalid hook entry %u %u\n",
625 if (newinfo->underflow[i] == 0xFFFFFFFF) {
626 duprintf("Invalid underflow %u %u\n",
632 if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
633 duprintf("Looping hook\n");
637 /* Finally, each sanity check must pass */
639 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
640 find_check_entry, name, size, &i);
643 ARPT_ENTRY_ITERATE(entry0, newinfo->size,
648 /* And one copy for every other CPU */
649 for_each_possible_cpu(i) {
650 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
651 memcpy(newinfo->entries[i], entry0, newinfo->size);
658 static inline int add_entry_to_counter(const struct arpt_entry *e,
659 struct xt_counters total[],
662 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
668 static inline int set_entry_to_counter(const struct arpt_entry *e,
669 struct xt_counters total[],
672 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
678 static void get_counters(const struct xt_table_info *t,
679 struct xt_counters counters[])
685 /* Instead of clearing (by a previous call to memset())
686 * the counters and using adds, we set the counters
687 * with data used by 'current' CPU
688 * We dont care about preemption here.
690 curcpu = raw_smp_processor_id();
693 ARPT_ENTRY_ITERATE(t->entries[curcpu],
695 set_entry_to_counter,
699 for_each_possible_cpu(cpu) {
703 ARPT_ENTRY_ITERATE(t->entries[cpu],
705 add_entry_to_counter,
711 static inline struct xt_counters *alloc_counters(struct arpt_table *table)
713 unsigned int countersize;
714 struct xt_counters *counters;
715 struct xt_table_info *private = table->private;
717 /* We need atomic snapshot of counters: rest doesn't change
718 * (other than comefrom, which userspace doesn't care
721 countersize = sizeof(struct xt_counters) * private->number;
722 counters = vmalloc_node(countersize, numa_node_id());
724 if (counters == NULL)
725 return ERR_PTR(-ENOMEM);
727 /* First, sum counters... */
728 write_lock_bh(&table->lock);
729 get_counters(private, counters);
730 write_unlock_bh(&table->lock);
735 static int copy_entries_to_user(unsigned int total_size,
736 struct arpt_table *table,
737 void __user *userptr)
739 unsigned int off, num;
740 struct arpt_entry *e;
741 struct xt_counters *counters;
742 struct xt_table_info *private = table->private;
746 counters = alloc_counters(table);
747 if (IS_ERR(counters))
748 return PTR_ERR(counters);
750 loc_cpu_entry = private->entries[raw_smp_processor_id()];
751 /* ... then copy entire thing ... */
752 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
757 /* FIXME: use iterator macros --RR */
758 /* ... then go back and fix counters and names */
759 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
760 struct arpt_entry_target *t;
762 e = (struct arpt_entry *)(loc_cpu_entry + off);
763 if (copy_to_user(userptr + off
764 + offsetof(struct arpt_entry, counters),
766 sizeof(counters[num])) != 0) {
771 t = arpt_get_target(e);
772 if (copy_to_user(userptr + off + e->target_offset
773 + offsetof(struct arpt_entry_target,
775 t->u.kernel.target->name,
776 strlen(t->u.kernel.target->name)+1) != 0) {
788 static void compat_standard_from_user(void *dst, void *src)
790 int v = *(compat_int_t *)src;
793 v += xt_compat_calc_jump(NF_ARP, v);
794 memcpy(dst, &v, sizeof(v));
797 static int compat_standard_to_user(void __user *dst, void *src)
799 compat_int_t cv = *(int *)src;
802 cv -= xt_compat_calc_jump(NF_ARP, cv);
803 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
806 static int compat_calc_entry(struct arpt_entry *e,
807 const struct xt_table_info *info,
808 void *base, struct xt_table_info *newinfo)
810 struct arpt_entry_target *t;
811 unsigned int entry_offset;
814 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
815 entry_offset = (void *)e - base;
817 t = arpt_get_target(e);
818 off += xt_compat_target_offset(t->u.kernel.target);
819 newinfo->size -= off;
820 ret = xt_compat_add_offset(NF_ARP, entry_offset, off);
824 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
825 if (info->hook_entry[i] &&
826 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
827 newinfo->hook_entry[i] -= off;
828 if (info->underflow[i] &&
829 (e < (struct arpt_entry *)(base + info->underflow[i])))
830 newinfo->underflow[i] -= off;
835 static int compat_table_info(const struct xt_table_info *info,
836 struct xt_table_info *newinfo)
840 if (!newinfo || !info)
843 /* we dont care about newinfo->entries[] */
844 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
845 newinfo->initial_entries = 0;
846 loc_cpu_entry = info->entries[raw_smp_processor_id()];
847 return ARPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
848 compat_calc_entry, info, loc_cpu_entry,
853 static int get_info(void __user *user, int *len, int compat)
855 char name[ARPT_TABLE_MAXNAMELEN];
856 struct arpt_table *t;
859 if (*len != sizeof(struct arpt_getinfo)) {
860 duprintf("length %u != %Zu\n", *len,
861 sizeof(struct arpt_getinfo));
865 if (copy_from_user(name, user, sizeof(name)) != 0)
868 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
871 xt_compat_lock(NF_ARP);
873 t = try_then_request_module(xt_find_table_lock(NF_ARP, name),
874 "arptable_%s", name);
875 if (t && !IS_ERR(t)) {
876 struct arpt_getinfo info;
877 struct xt_table_info *private = t->private;
881 struct xt_table_info tmp;
882 ret = compat_table_info(private, &tmp);
883 xt_compat_flush_offsets(NF_ARP);
887 info.valid_hooks = t->valid_hooks;
888 memcpy(info.hook_entry, private->hook_entry,
889 sizeof(info.hook_entry));
890 memcpy(info.underflow, private->underflow,
891 sizeof(info.underflow));
892 info.num_entries = private->number;
893 info.size = private->size;
894 strcpy(info.name, name);
896 if (copy_to_user(user, &info, *len) != 0)
903 ret = t ? PTR_ERR(t) : -ENOENT;
906 xt_compat_unlock(NF_ARP);
911 static int get_entries(struct arpt_get_entries __user *uptr, int *len)
914 struct arpt_get_entries get;
915 struct arpt_table *t;
917 if (*len < sizeof(get)) {
918 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
921 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
923 if (*len != sizeof(struct arpt_get_entries) + get.size) {
924 duprintf("get_entries: %u != %Zu\n", *len,
925 sizeof(struct arpt_get_entries) + get.size);
929 t = xt_find_table_lock(NF_ARP, get.name);
930 if (t && !IS_ERR(t)) {
931 struct xt_table_info *private = t->private;
932 duprintf("t->private->number = %u\n",
934 if (get.size == private->size)
935 ret = copy_entries_to_user(private->size,
936 t, uptr->entrytable);
938 duprintf("get_entries: I've got %u not %u!\n",
939 private->size, get.size);
945 ret = t ? PTR_ERR(t) : -ENOENT;
950 static int __do_replace(const char *name, unsigned int valid_hooks,
951 struct xt_table_info *newinfo,
952 unsigned int num_counters,
953 void __user *counters_ptr)
956 struct arpt_table *t;
957 struct xt_table_info *oldinfo;
958 struct xt_counters *counters;
959 void *loc_cpu_old_entry;
962 counters = vmalloc_node(num_counters * sizeof(struct xt_counters),
969 t = try_then_request_module(xt_find_table_lock(NF_ARP, name),
970 "arptable_%s", name);
971 if (!t || IS_ERR(t)) {
972 ret = t ? PTR_ERR(t) : -ENOENT;
973 goto free_newinfo_counters_untrans;
977 if (valid_hooks != t->valid_hooks) {
978 duprintf("Valid hook crap: %08X vs %08X\n",
979 valid_hooks, t->valid_hooks);
984 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
988 /* Update module usage count based on number of rules */
989 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
990 oldinfo->number, oldinfo->initial_entries, newinfo->number);
991 if ((oldinfo->number > oldinfo->initial_entries) ||
992 (newinfo->number <= oldinfo->initial_entries))
994 if ((oldinfo->number > oldinfo->initial_entries) &&
995 (newinfo->number <= oldinfo->initial_entries))
998 /* Get the old counters. */
999 get_counters(oldinfo, counters);
1000 /* Decrease module usage counts and free resource */
1001 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1002 ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1005 xt_free_table_info(oldinfo);
1006 if (copy_to_user(counters_ptr, counters,
1007 sizeof(struct xt_counters) * num_counters) != 0)
1016 free_newinfo_counters_untrans:
1022 static int do_replace(void __user *user, unsigned int len)
1025 struct arpt_replace tmp;
1026 struct xt_table_info *newinfo;
1027 void *loc_cpu_entry;
1029 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1032 /* overflow check */
1033 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1036 newinfo = xt_alloc_table_info(tmp.size);
1040 /* choose the copy that is on our node/cpu */
1041 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1042 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1048 ret = translate_table(tmp.name, tmp.valid_hooks,
1049 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
1050 tmp.hook_entry, tmp.underflow);
1054 duprintf("arp_tables: Translated table\n");
1056 ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo,
1057 tmp.num_counters, tmp.counters);
1059 goto free_newinfo_untrans;
1062 free_newinfo_untrans:
1063 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1065 xt_free_table_info(newinfo);
1069 /* We're lazy, and add to the first CPU; overflow works its fey magic
1070 * and everything is OK.
1072 static inline int add_counter_to_entry(struct arpt_entry *e,
1073 const struct xt_counters addme[],
1077 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1083 static int do_add_counters(void __user *user, unsigned int len, int compat)
1086 struct xt_counters_info tmp;
1087 struct xt_counters *paddc;
1088 unsigned int num_counters;
1092 struct arpt_table *t;
1093 struct xt_table_info *private;
1095 void *loc_cpu_entry;
1096 #ifdef CONFIG_COMPAT
1097 struct compat_xt_counters_info compat_tmp;
1101 size = sizeof(struct compat_xt_counters_info);
1106 size = sizeof(struct xt_counters_info);
1109 if (copy_from_user(ptmp, user, size) != 0)
1112 #ifdef CONFIG_COMPAT
1114 num_counters = compat_tmp.num_counters;
1115 name = compat_tmp.name;
1119 num_counters = tmp.num_counters;
1123 if (len != size + num_counters * sizeof(struct xt_counters))
1126 paddc = vmalloc_node(len - size, numa_node_id());
1130 if (copy_from_user(paddc, user + size, len - size) != 0) {
1135 t = xt_find_table_lock(NF_ARP, name);
1136 if (!t || IS_ERR(t)) {
1137 ret = t ? PTR_ERR(t) : -ENOENT;
1141 write_lock_bh(&t->lock);
1142 private = t->private;
1143 if (private->number != num_counters) {
1145 goto unlock_up_free;
1149 /* Choose the copy that is on our node */
1150 loc_cpu_entry = private->entries[smp_processor_id()];
1151 ARPT_ENTRY_ITERATE(loc_cpu_entry,
1153 add_counter_to_entry,
1157 write_unlock_bh(&t->lock);
1166 #ifdef CONFIG_COMPAT
1168 compat_release_entry(struct compat_arpt_entry *e, unsigned int *i)
1170 struct arpt_entry_target *t;
1172 if (i && (*i)-- == 0)
1175 t = compat_arpt_get_target(e);
1176 module_put(t->u.kernel.target->me);
1181 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1182 struct xt_table_info *newinfo,
1184 unsigned char *base,
1185 unsigned char *limit,
1186 unsigned int *hook_entries,
1187 unsigned int *underflows,
1191 struct arpt_entry_target *t;
1192 struct xt_target *target;
1193 unsigned int entry_offset;
1196 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1197 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0
1198 || (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
1199 duprintf("Bad offset %p, limit = %p\n", e, limit);
1203 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1204 sizeof(struct compat_xt_entry_target)) {
1205 duprintf("checking: element %p size %u\n",
1210 /* For purposes of check_entry casting the compat entry is fine */
1211 ret = check_entry((struct arpt_entry *)e, name);
1215 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1216 entry_offset = (void *)e - (void *)base;
1218 t = compat_arpt_get_target(e);
1219 target = try_then_request_module(xt_find_target(NF_ARP,
1221 t->u.user.revision),
1222 "arpt_%s", t->u.user.name);
1223 if (IS_ERR(target) || !target) {
1224 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1226 ret = target ? PTR_ERR(target) : -ENOENT;
1229 t->u.kernel.target = target;
1231 off += xt_compat_target_offset(target);
1233 ret = xt_compat_add_offset(NF_ARP, entry_offset, off);
1235 goto release_target;
1237 /* Check hooks & underflows */
1238 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1239 if ((unsigned char *)e - base == hook_entries[h])
1240 newinfo->hook_entry[h] = hook_entries[h];
1241 if ((unsigned char *)e - base == underflows[h])
1242 newinfo->underflow[h] = underflows[h];
1245 /* Clear counters and comefrom */
1246 memset(&e->counters, 0, sizeof(e->counters));
1253 module_put(t->u.kernel.target->me);
1259 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1260 unsigned int *size, const char *name,
1261 struct xt_table_info *newinfo, unsigned char *base)
1263 struct arpt_entry_target *t;
1264 struct xt_target *target;
1265 struct arpt_entry *de;
1266 unsigned int origsize;
1271 de = (struct arpt_entry *)*dstptr;
1272 memcpy(de, e, sizeof(struct arpt_entry));
1273 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1275 *dstptr += sizeof(struct arpt_entry);
1276 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1278 de->target_offset = e->target_offset - (origsize - *size);
1279 t = compat_arpt_get_target(e);
1280 target = t->u.kernel.target;
1281 xt_compat_target_from_user(t, dstptr, size);
1283 de->next_offset = e->next_offset - (origsize - *size);
1284 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1285 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1286 newinfo->hook_entry[h] -= origsize - *size;
1287 if ((unsigned char *)de - base < newinfo->underflow[h])
1288 newinfo->underflow[h] -= origsize - *size;
1293 static inline int compat_check_entry(struct arpt_entry *e, const char *name,
1298 ret = check_target(e, name);
1306 static int translate_compat_table(const char *name,
1307 unsigned int valid_hooks,
1308 struct xt_table_info **pinfo,
1310 unsigned int total_size,
1311 unsigned int number,
1312 unsigned int *hook_entries,
1313 unsigned int *underflows)
1316 struct xt_table_info *newinfo, *info;
1317 void *pos, *entry0, *entry1;
1324 info->number = number;
1326 /* Init all hooks to impossible value. */
1327 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1328 info->hook_entry[i] = 0xFFFFFFFF;
1329 info->underflow[i] = 0xFFFFFFFF;
1332 duprintf("translate_compat_table: size %u\n", info->size);
1334 xt_compat_lock(NF_ARP);
1335 /* Walk through entries, checking offsets. */
1336 ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size,
1337 check_compat_entry_size_and_hooks,
1338 info, &size, entry0,
1339 entry0 + total_size,
1340 hook_entries, underflows, &j, name);
1346 duprintf("translate_compat_table: %u not %u entries\n",
1351 /* Check hooks all assigned */
1352 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1353 /* Only hooks which are valid */
1354 if (!(valid_hooks & (1 << i)))
1356 if (info->hook_entry[i] == 0xFFFFFFFF) {
1357 duprintf("Invalid hook entry %u %u\n",
1358 i, hook_entries[i]);
1361 if (info->underflow[i] == 0xFFFFFFFF) {
1362 duprintf("Invalid underflow %u %u\n",
1369 newinfo = xt_alloc_table_info(size);
1373 newinfo->number = number;
1374 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1375 newinfo->hook_entry[i] = info->hook_entry[i];
1376 newinfo->underflow[i] = info->underflow[i];
1378 entry1 = newinfo->entries[raw_smp_processor_id()];
1381 ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size,
1382 compat_copy_entry_from_user,
1383 &pos, &size, name, newinfo, entry1);
1384 xt_compat_flush_offsets(NF_ARP);
1385 xt_compat_unlock(NF_ARP);
1390 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1394 ret = ARPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
1398 COMPAT_ARPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1399 compat_release_entry, &j);
1400 ARPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1401 xt_free_table_info(newinfo);
1405 /* And one copy for every other CPU */
1406 for_each_possible_cpu(i)
1407 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1408 memcpy(newinfo->entries[i], entry1, newinfo->size);
1412 xt_free_table_info(info);
1416 xt_free_table_info(newinfo);
1418 COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
1421 xt_compat_flush_offsets(NF_ARP);
1422 xt_compat_unlock(NF_ARP);
1426 struct compat_arpt_replace {
1427 char name[ARPT_TABLE_MAXNAMELEN];
1431 u32 hook_entry[NF_ARP_NUMHOOKS];
1432 u32 underflow[NF_ARP_NUMHOOKS];
1434 compat_uptr_t counters;
1435 struct compat_arpt_entry entries[0];
1438 static int compat_do_replace(void __user *user, unsigned int len)
1441 struct compat_arpt_replace tmp;
1442 struct xt_table_info *newinfo;
1443 void *loc_cpu_entry;
1445 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1448 /* overflow check */
1449 if (tmp.size >= INT_MAX / num_possible_cpus())
1451 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1454 newinfo = xt_alloc_table_info(tmp.size);
1458 /* choose the copy that is on our node/cpu */
1459 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1460 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1465 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1466 &newinfo, &loc_cpu_entry, tmp.size,
1467 tmp.num_entries, tmp.hook_entry,
1472 duprintf("compat_do_replace: Translated table\n");
1474 ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo,
1475 tmp.num_counters, compat_ptr(tmp.counters));
1477 goto free_newinfo_untrans;
1480 free_newinfo_untrans:
1481 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1483 xt_free_table_info(newinfo);
1487 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1492 if (!capable(CAP_NET_ADMIN))
1496 case ARPT_SO_SET_REPLACE:
1497 ret = compat_do_replace(user, len);
1500 case ARPT_SO_SET_ADD_COUNTERS:
1501 ret = do_add_counters(user, len, 1);
1505 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1512 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1513 compat_uint_t *size,
1514 struct xt_counters *counters,
1517 struct arpt_entry_target *t;
1518 struct compat_arpt_entry __user *ce;
1519 u_int16_t target_offset, next_offset;
1520 compat_uint_t origsize;
1525 ce = (struct compat_arpt_entry __user *)*dstptr;
1526 if (copy_to_user(ce, e, sizeof(struct arpt_entry)))
1529 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1532 *dstptr += sizeof(struct compat_arpt_entry);
1533 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1535 target_offset = e->target_offset - (origsize - *size);
1537 t = arpt_get_target(e);
1538 ret = xt_compat_target_to_user(t, dstptr, size);
1542 next_offset = e->next_offset - (origsize - *size);
1543 if (put_user(target_offset, &ce->target_offset))
1545 if (put_user(next_offset, &ce->next_offset))
1554 static int compat_copy_entries_to_user(unsigned int total_size,
1555 struct arpt_table *table,
1556 void __user *userptr)
1558 struct xt_counters *counters;
1559 struct xt_table_info *private = table->private;
1563 void *loc_cpu_entry;
1566 counters = alloc_counters(table);
1567 if (IS_ERR(counters))
1568 return PTR_ERR(counters);
1570 /* choose the copy on our node/cpu */
1571 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1574 ret = ARPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
1575 compat_copy_entry_to_user,
1576 &pos, &size, counters, &i);
1581 struct compat_arpt_get_entries {
1582 char name[ARPT_TABLE_MAXNAMELEN];
1584 struct compat_arpt_entry entrytable[0];
1587 static int compat_get_entries(struct compat_arpt_get_entries __user *uptr,
1591 struct compat_arpt_get_entries get;
1592 struct arpt_table *t;
1594 if (*len < sizeof(get)) {
1595 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1598 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1600 if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1601 duprintf("compat_get_entries: %u != %zu\n",
1602 *len, sizeof(get) + get.size);
1606 xt_compat_lock(NF_ARP);
1607 t = xt_find_table_lock(NF_ARP, get.name);
1608 if (t && !IS_ERR(t)) {
1609 struct xt_table_info *private = t->private;
1610 struct xt_table_info info;
1612 duprintf("t->private->number = %u\n", private->number);
1613 ret = compat_table_info(private, &info);
1614 if (!ret && get.size == info.size) {
1615 ret = compat_copy_entries_to_user(private->size,
1616 t, uptr->entrytable);
1618 duprintf("compat_get_entries: I've got %u not %u!\n",
1619 private->size, get.size);
1622 xt_compat_flush_offsets(NF_ARP);
1626 ret = t ? PTR_ERR(t) : -ENOENT;
1628 xt_compat_unlock(NF_ARP);
1632 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1634 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1639 if (!capable(CAP_NET_ADMIN))
1643 case ARPT_SO_GET_INFO:
1644 ret = get_info(user, len, 1);
1646 case ARPT_SO_GET_ENTRIES:
1647 ret = compat_get_entries(user, len);
1650 ret = do_arpt_get_ctl(sk, cmd, user, len);
1656 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1660 if (!capable(CAP_NET_ADMIN))
1664 case ARPT_SO_SET_REPLACE:
1665 ret = do_replace(user, len);
1668 case ARPT_SO_SET_ADD_COUNTERS:
1669 ret = do_add_counters(user, len, 0);
1673 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1680 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1684 if (!capable(CAP_NET_ADMIN))
1688 case ARPT_SO_GET_INFO:
1689 ret = get_info(user, len, 0);
1692 case ARPT_SO_GET_ENTRIES:
1693 ret = get_entries(user, len);
1696 case ARPT_SO_GET_REVISION_TARGET: {
1697 struct xt_get_revision rev;
1699 if (*len != sizeof(rev)) {
1703 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1708 try_then_request_module(xt_find_revision(NF_ARP, rev.name,
1709 rev.revision, 1, &ret),
1710 "arpt_%s", rev.name);
1715 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1722 int arpt_register_table(struct arpt_table *table,
1723 const struct arpt_replace *repl)
1726 struct xt_table_info *newinfo;
1727 struct xt_table_info bootstrap
1728 = { 0, 0, 0, { 0 }, { 0 }, { } };
1729 void *loc_cpu_entry;
1730 struct xt_table *new_table;
1732 newinfo = xt_alloc_table_info(repl->size);
1738 /* choose the copy on our node/cpu */
1739 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1740 memcpy(loc_cpu_entry, repl->entries, repl->size);
1742 ret = translate_table(table->name, table->valid_hooks,
1743 newinfo, loc_cpu_entry, repl->size,
1748 duprintf("arpt_register_table: translate table gives %d\n", ret);
1750 xt_free_table_info(newinfo);
1754 new_table = xt_register_table(table, &bootstrap, newinfo);
1755 if (IS_ERR(new_table)) {
1756 xt_free_table_info(newinfo);
1757 return PTR_ERR(new_table);
1763 void arpt_unregister_table(struct arpt_table *table)
1765 struct xt_table_info *private;
1766 void *loc_cpu_entry;
1768 private = xt_unregister_table(table);
1770 /* Decrease module usage counts and free resources */
1771 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1772 ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size,
1773 cleanup_entry, NULL);
1774 xt_free_table_info(private);
1777 /* The built-in targets: standard (NULL) and error. */
1778 static struct arpt_target arpt_standard_target __read_mostly = {
1779 .name = ARPT_STANDARD_TARGET,
1780 .targetsize = sizeof(int),
1782 #ifdef CONFIG_COMPAT
1783 .compatsize = sizeof(compat_int_t),
1784 .compat_from_user = compat_standard_from_user,
1785 .compat_to_user = compat_standard_to_user,
1789 static struct arpt_target arpt_error_target __read_mostly = {
1790 .name = ARPT_ERROR_TARGET,
1791 .target = arpt_error,
1792 .targetsize = ARPT_FUNCTION_MAXNAMELEN,
1796 static struct nf_sockopt_ops arpt_sockopts = {
1798 .set_optmin = ARPT_BASE_CTL,
1799 .set_optmax = ARPT_SO_SET_MAX+1,
1800 .set = do_arpt_set_ctl,
1801 #ifdef CONFIG_COMPAT
1802 .compat_set = compat_do_arpt_set_ctl,
1804 .get_optmin = ARPT_BASE_CTL,
1805 .get_optmax = ARPT_SO_GET_MAX+1,
1806 .get = do_arpt_get_ctl,
1807 #ifdef CONFIG_COMPAT
1808 .compat_get = compat_do_arpt_get_ctl,
1810 .owner = THIS_MODULE,
1813 static int __init arp_tables_init(void)
1817 ret = xt_proto_init(NF_ARP);
1821 /* Noone else will be downing sem now, so we won't sleep */
1822 ret = xt_register_target(&arpt_standard_target);
1825 ret = xt_register_target(&arpt_error_target);
1829 /* Register setsockopt */
1830 ret = nf_register_sockopt(&arpt_sockopts);
1834 printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1838 xt_unregister_target(&arpt_error_target);
1840 xt_unregister_target(&arpt_standard_target);
1842 xt_proto_fini(NF_ARP);
1847 static void __exit arp_tables_fini(void)
1849 nf_unregister_sockopt(&arpt_sockopts);
1850 xt_unregister_target(&arpt_error_target);
1851 xt_unregister_target(&arpt_standard_target);
1852 xt_proto_fini(NF_ARP);
1855 EXPORT_SYMBOL(arpt_register_table);
1856 EXPORT_SYMBOL(arpt_unregister_table);
1857 EXPORT_SYMBOL(arpt_do_table);
1859 module_init(arp_tables_init);
1860 module_exit(arp_tables_fini);