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)":"");
185 static inline int arp_checkentry(const struct arpt_arp *arp)
187 if (arp->flags & ~ARPT_F_MASK) {
188 duprintf("Unknown flag bits set: %08X\n",
189 arp->flags & ~ARPT_F_MASK);
192 if (arp->invflags & ~ARPT_INV_MASK) {
193 duprintf("Unknown invflag bits set: %08X\n",
194 arp->invflags & ~ARPT_INV_MASK);
201 static unsigned int arpt_error(struct sk_buff *skb,
202 const struct net_device *in,
203 const struct net_device *out,
204 unsigned int hooknum,
205 const struct xt_target *target,
206 const void *targinfo)
209 printk("arp_tables: error: '%s'\n", (char *)targinfo);
214 static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
216 return (struct arpt_entry *)(base + offset);
219 unsigned int arpt_do_table(struct sk_buff *skb,
221 const struct net_device *in,
222 const struct net_device *out,
223 struct arpt_table *table)
225 static const char nulldevname[IFNAMSIZ];
226 unsigned int verdict = NF_DROP;
228 bool hotdrop = false;
229 struct arpt_entry *e, *back;
230 const char *indev, *outdev;
232 struct xt_table_info *private;
234 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
235 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
236 (2 * skb->dev->addr_len) +
240 indev = in ? in->name : nulldevname;
241 outdev = out ? out->name : nulldevname;
243 read_lock_bh(&table->lock);
244 private = table->private;
245 table_base = (void *)private->entries[smp_processor_id()];
246 e = get_entry(table_base, private->hook_entry[hook]);
247 back = get_entry(table_base, private->underflow[hook]);
251 if (arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
252 struct arpt_entry_target *t;
255 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
256 (2 * skb->dev->addr_len);
257 ADD_COUNTER(e->counters, hdr_len, 1);
259 t = arpt_get_target(e);
261 /* Standard target? */
262 if (!t->u.kernel.target->target) {
265 v = ((struct arpt_standard_target *)t)->verdict;
267 /* Pop from stack? */
268 if (v != ARPT_RETURN) {
269 verdict = (unsigned)(-v) - 1;
273 back = get_entry(table_base,
278 != (void *)e + e->next_offset) {
279 /* Save old back ptr in next entry */
280 struct arpt_entry *next
281 = (void *)e + e->next_offset;
283 (void *)back - table_base;
285 /* set back pointer to next entry */
289 e = get_entry(table_base, v);
291 /* Targets which reenter must return
294 verdict = t->u.kernel.target->target(skb,
300 /* Target might have changed stuff. */
303 if (verdict == ARPT_CONTINUE)
304 e = (void *)e + e->next_offset;
310 e = (void *)e + e->next_offset;
313 read_unlock_bh(&table->lock);
321 /* All zeroes == unconditional rule. */
322 static inline int unconditional(const struct arpt_arp *arp)
326 for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
327 if (((__u32 *)arp)[i])
333 /* Figures out from what hook each rule can be called: returns 0 if
334 * there are loops. Puts hook bitmask in comefrom.
336 static int mark_source_chains(struct xt_table_info *newinfo,
337 unsigned int valid_hooks, void *entry0)
341 /* No recursion; use packet counter to save back ptrs (reset
342 * to 0 as we leave), and comefrom to save source hook bitmask.
344 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
345 unsigned int pos = newinfo->hook_entry[hook];
347 = (struct arpt_entry *)(entry0 + pos);
349 if (!(valid_hooks & (1 << hook)))
352 /* Set initial back pointer. */
353 e->counters.pcnt = pos;
356 struct arpt_standard_target *t
357 = (void *)arpt_get_target(e);
358 int visited = e->comefrom & (1 << hook);
360 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
361 printk("arptables: loop hook %u pos %u %08X.\n",
362 hook, pos, e->comefrom);
366 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
368 /* Unconditional return/END. */
369 if ((e->target_offset == sizeof(struct arpt_entry)
370 && (strcmp(t->target.u.user.name,
371 ARPT_STANDARD_TARGET) == 0)
373 && unconditional(&e->arp)) || visited) {
374 unsigned int oldpos, size;
376 if (t->verdict < -NF_MAX_VERDICT - 1) {
377 duprintf("mark_source_chains: bad "
378 "negative verdict (%i)\n",
383 /* Return: backtrack through the last
387 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
389 pos = e->counters.pcnt;
390 e->counters.pcnt = 0;
392 /* We're at the start. */
396 e = (struct arpt_entry *)
398 } while (oldpos == pos + e->next_offset);
401 size = e->next_offset;
402 e = (struct arpt_entry *)
403 (entry0 + pos + size);
404 e->counters.pcnt = pos;
407 int newpos = t->verdict;
409 if (strcmp(t->target.u.user.name,
410 ARPT_STANDARD_TARGET) == 0
412 if (newpos > newinfo->size -
413 sizeof(struct arpt_entry)) {
414 duprintf("mark_source_chains: "
415 "bad verdict (%i)\n",
420 /* This a jump; chase it. */
421 duprintf("Jump rule %u -> %u\n",
424 /* ... this is a fallthru */
425 newpos = pos + e->next_offset;
427 e = (struct arpt_entry *)
429 e->counters.pcnt = pos;
434 duprintf("Finished chain %u\n", hook);
439 static inline int check_entry(struct arpt_entry *e, const char *name)
441 struct arpt_entry_target *t;
443 if (!arp_checkentry(&e->arp)) {
444 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
448 if (e->target_offset + sizeof(struct arpt_entry_target) > e->next_offset)
451 t = arpt_get_target(e);
452 if (e->target_offset + t->u.target_size > e->next_offset)
458 static inline int check_target(struct arpt_entry *e, const char *name)
460 struct arpt_entry_target *t;
461 struct arpt_target *target;
464 t = arpt_get_target(e);
465 target = t->u.kernel.target;
467 ret = xt_check_target(target, NF_ARP, t->u.target_size - sizeof(*t),
468 name, e->comefrom, 0, 0);
469 if (!ret && t->u.kernel.target->checkentry
470 && !t->u.kernel.target->checkentry(name, e, target, t->data,
472 duprintf("arp_tables: check failed for `%s'.\n",
473 t->u.kernel.target->name);
480 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
483 struct arpt_entry_target *t;
484 struct arpt_target *target;
487 ret = check_entry(e, name);
491 t = arpt_get_target(e);
492 target = try_then_request_module(xt_find_target(NF_ARP, t->u.user.name,
494 "arpt_%s", t->u.user.name);
495 if (IS_ERR(target) || !target) {
496 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
497 ret = target ? PTR_ERR(target) : -ENOENT;
500 t->u.kernel.target = target;
502 ret = check_target(e, name);
509 module_put(t->u.kernel.target->me);
514 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
515 struct xt_table_info *newinfo,
517 unsigned char *limit,
518 const unsigned int *hook_entries,
519 const unsigned int *underflows,
524 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
525 || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
526 duprintf("Bad offset %p\n", e);
531 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
532 duprintf("checking: element %p size %u\n",
537 /* Check hooks & underflows */
538 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
539 if ((unsigned char *)e - base == hook_entries[h])
540 newinfo->hook_entry[h] = hook_entries[h];
541 if ((unsigned char *)e - base == underflows[h])
542 newinfo->underflow[h] = underflows[h];
545 /* FIXME: underflows must be unconditional, standard verdicts
546 < 0 (not ARPT_RETURN). --RR */
548 /* Clear counters and comefrom */
549 e->counters = ((struct xt_counters) { 0, 0 });
556 static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
558 struct arpt_entry_target *t;
560 if (i && (*i)-- == 0)
563 t = arpt_get_target(e);
564 if (t->u.kernel.target->destroy)
565 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
566 module_put(t->u.kernel.target->me);
570 /* Checks and translates the user-supplied table segment (held in
573 static int translate_table(const char *name,
574 unsigned int valid_hooks,
575 struct xt_table_info *newinfo,
579 const unsigned int *hook_entries,
580 const unsigned int *underflows)
585 newinfo->size = size;
586 newinfo->number = number;
588 /* Init all hooks to impossible value. */
589 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
590 newinfo->hook_entry[i] = 0xFFFFFFFF;
591 newinfo->underflow[i] = 0xFFFFFFFF;
594 duprintf("translate_table: size %u\n", newinfo->size);
597 /* Walk through entries, checking offsets. */
598 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
599 check_entry_size_and_hooks,
603 hook_entries, underflows, &i);
604 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
609 duprintf("translate_table: %u not %u entries\n",
614 /* Check hooks all assigned */
615 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
616 /* Only hooks which are valid */
617 if (!(valid_hooks & (1 << i)))
619 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
620 duprintf("Invalid hook entry %u %u\n",
624 if (newinfo->underflow[i] == 0xFFFFFFFF) {
625 duprintf("Invalid underflow %u %u\n",
631 if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
632 duprintf("Looping hook\n");
636 /* Finally, each sanity check must pass */
638 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
639 find_check_entry, name, size, &i);
642 ARPT_ENTRY_ITERATE(entry0, newinfo->size,
647 /* And one copy for every other CPU */
648 for_each_possible_cpu(i) {
649 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
650 memcpy(newinfo->entries[i], entry0, newinfo->size);
657 static inline int add_entry_to_counter(const struct arpt_entry *e,
658 struct xt_counters total[],
661 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
667 static inline int set_entry_to_counter(const struct arpt_entry *e,
668 struct xt_counters total[],
671 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
677 static void get_counters(const struct xt_table_info *t,
678 struct xt_counters counters[])
684 /* Instead of clearing (by a previous call to memset())
685 * the counters and using adds, we set the counters
686 * with data used by 'current' CPU
687 * We dont care about preemption here.
689 curcpu = raw_smp_processor_id();
692 ARPT_ENTRY_ITERATE(t->entries[curcpu],
694 set_entry_to_counter,
698 for_each_possible_cpu(cpu) {
702 ARPT_ENTRY_ITERATE(t->entries[cpu],
704 add_entry_to_counter,
710 static inline struct xt_counters *alloc_counters(struct arpt_table *table)
712 unsigned int countersize;
713 struct xt_counters *counters;
714 struct xt_table_info *private = table->private;
716 /* We need atomic snapshot of counters: rest doesn't change
717 * (other than comefrom, which userspace doesn't care
720 countersize = sizeof(struct xt_counters) * private->number;
721 counters = vmalloc_node(countersize, numa_node_id());
723 if (counters == NULL)
724 return ERR_PTR(-ENOMEM);
726 /* First, sum counters... */
727 write_lock_bh(&table->lock);
728 get_counters(private, counters);
729 write_unlock_bh(&table->lock);
734 static int copy_entries_to_user(unsigned int total_size,
735 struct arpt_table *table,
736 void __user *userptr)
738 unsigned int off, num;
739 struct arpt_entry *e;
740 struct xt_counters *counters;
741 struct xt_table_info *private = table->private;
745 counters = alloc_counters(table);
746 if (IS_ERR(counters))
747 return PTR_ERR(counters);
749 loc_cpu_entry = private->entries[raw_smp_processor_id()];
750 /* ... then copy entire thing ... */
751 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
756 /* FIXME: use iterator macros --RR */
757 /* ... then go back and fix counters and names */
758 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
759 struct arpt_entry_target *t;
761 e = (struct arpt_entry *)(loc_cpu_entry + off);
762 if (copy_to_user(userptr + off
763 + offsetof(struct arpt_entry, counters),
765 sizeof(counters[num])) != 0) {
770 t = arpt_get_target(e);
771 if (copy_to_user(userptr + off + e->target_offset
772 + offsetof(struct arpt_entry_target,
774 t->u.kernel.target->name,
775 strlen(t->u.kernel.target->name)+1) != 0) {
787 static void compat_standard_from_user(void *dst, void *src)
789 int v = *(compat_int_t *)src;
792 v += xt_compat_calc_jump(NF_ARP, v);
793 memcpy(dst, &v, sizeof(v));
796 static int compat_standard_to_user(void __user *dst, void *src)
798 compat_int_t cv = *(int *)src;
801 cv -= xt_compat_calc_jump(NF_ARP, cv);
802 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
805 static int compat_calc_entry(struct arpt_entry *e,
806 const struct xt_table_info *info,
807 void *base, struct xt_table_info *newinfo)
809 struct arpt_entry_target *t;
810 unsigned int entry_offset;
813 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
814 entry_offset = (void *)e - base;
816 t = arpt_get_target(e);
817 off += xt_compat_target_offset(t->u.kernel.target);
818 newinfo->size -= off;
819 ret = xt_compat_add_offset(NF_ARP, entry_offset, off);
823 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
824 if (info->hook_entry[i] &&
825 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
826 newinfo->hook_entry[i] -= off;
827 if (info->underflow[i] &&
828 (e < (struct arpt_entry *)(base + info->underflow[i])))
829 newinfo->underflow[i] -= off;
834 static int compat_table_info(const struct xt_table_info *info,
835 struct xt_table_info *newinfo)
839 if (!newinfo || !info)
842 /* we dont care about newinfo->entries[] */
843 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
844 newinfo->initial_entries = 0;
845 loc_cpu_entry = info->entries[raw_smp_processor_id()];
846 return ARPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
847 compat_calc_entry, info, loc_cpu_entry,
852 static int get_info(void __user *user, int *len, int compat)
854 char name[ARPT_TABLE_MAXNAMELEN];
855 struct arpt_table *t;
858 if (*len != sizeof(struct arpt_getinfo)) {
859 duprintf("length %u != %Zu\n", *len,
860 sizeof(struct arpt_getinfo));
864 if (copy_from_user(name, user, sizeof(name)) != 0)
867 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
870 xt_compat_lock(NF_ARP);
872 t = try_then_request_module(xt_find_table_lock(NF_ARP, name),
873 "arptable_%s", name);
874 if (t && !IS_ERR(t)) {
875 struct arpt_getinfo info;
876 struct xt_table_info *private = t->private;
880 struct xt_table_info tmp;
881 ret = compat_table_info(private, &tmp);
882 xt_compat_flush_offsets(NF_ARP);
886 info.valid_hooks = t->valid_hooks;
887 memcpy(info.hook_entry, private->hook_entry,
888 sizeof(info.hook_entry));
889 memcpy(info.underflow, private->underflow,
890 sizeof(info.underflow));
891 info.num_entries = private->number;
892 info.size = private->size;
893 strcpy(info.name, name);
895 if (copy_to_user(user, &info, *len) != 0)
902 ret = t ? PTR_ERR(t) : -ENOENT;
905 xt_compat_unlock(NF_ARP);
910 static int get_entries(struct arpt_get_entries __user *uptr, int *len)
913 struct arpt_get_entries get;
914 struct arpt_table *t;
916 if (*len < sizeof(get)) {
917 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
920 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
922 if (*len != sizeof(struct arpt_get_entries) + get.size) {
923 duprintf("get_entries: %u != %Zu\n", *len,
924 sizeof(struct arpt_get_entries) + get.size);
928 t = xt_find_table_lock(NF_ARP, get.name);
929 if (t && !IS_ERR(t)) {
930 struct xt_table_info *private = t->private;
931 duprintf("t->private->number = %u\n",
933 if (get.size == private->size)
934 ret = copy_entries_to_user(private->size,
935 t, uptr->entrytable);
937 duprintf("get_entries: I've got %u not %u!\n",
938 private->size, get.size);
944 ret = t ? PTR_ERR(t) : -ENOENT;
949 static int __do_replace(const char *name, unsigned int valid_hooks,
950 struct xt_table_info *newinfo,
951 unsigned int num_counters,
952 void __user *counters_ptr)
955 struct arpt_table *t;
956 struct xt_table_info *oldinfo;
957 struct xt_counters *counters;
958 void *loc_cpu_old_entry;
961 counters = vmalloc_node(num_counters * sizeof(struct xt_counters),
968 t = try_then_request_module(xt_find_table_lock(NF_ARP, name),
969 "arptable_%s", name);
970 if (!t || IS_ERR(t)) {
971 ret = t ? PTR_ERR(t) : -ENOENT;
972 goto free_newinfo_counters_untrans;
976 if (valid_hooks != t->valid_hooks) {
977 duprintf("Valid hook crap: %08X vs %08X\n",
978 valid_hooks, t->valid_hooks);
983 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
987 /* Update module usage count based on number of rules */
988 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
989 oldinfo->number, oldinfo->initial_entries, newinfo->number);
990 if ((oldinfo->number > oldinfo->initial_entries) ||
991 (newinfo->number <= oldinfo->initial_entries))
993 if ((oldinfo->number > oldinfo->initial_entries) &&
994 (newinfo->number <= oldinfo->initial_entries))
997 /* Get the old counters. */
998 get_counters(oldinfo, counters);
999 /* Decrease module usage counts and free resource */
1000 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1001 ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1004 xt_free_table_info(oldinfo);
1005 if (copy_to_user(counters_ptr, counters,
1006 sizeof(struct xt_counters) * num_counters) != 0)
1015 free_newinfo_counters_untrans:
1021 static int do_replace(void __user *user, unsigned int len)
1024 struct arpt_replace tmp;
1025 struct xt_table_info *newinfo;
1026 void *loc_cpu_entry;
1028 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1031 /* overflow check */
1032 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1035 newinfo = xt_alloc_table_info(tmp.size);
1039 /* choose the copy that is on our node/cpu */
1040 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1041 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1047 ret = translate_table(tmp.name, tmp.valid_hooks,
1048 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
1049 tmp.hook_entry, tmp.underflow);
1053 duprintf("arp_tables: Translated table\n");
1055 ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo,
1056 tmp.num_counters, tmp.counters);
1058 goto free_newinfo_untrans;
1061 free_newinfo_untrans:
1062 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1064 xt_free_table_info(newinfo);
1068 /* We're lazy, and add to the first CPU; overflow works its fey magic
1069 * and everything is OK.
1071 static inline int add_counter_to_entry(struct arpt_entry *e,
1072 const struct xt_counters addme[],
1076 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1082 static int do_add_counters(void __user *user, unsigned int len, int compat)
1085 struct xt_counters_info tmp;
1086 struct xt_counters *paddc;
1087 unsigned int num_counters;
1091 struct arpt_table *t;
1092 struct xt_table_info *private;
1094 void *loc_cpu_entry;
1095 #ifdef CONFIG_COMPAT
1096 struct compat_xt_counters_info compat_tmp;
1100 size = sizeof(struct compat_xt_counters_info);
1105 size = sizeof(struct xt_counters_info);
1108 if (copy_from_user(ptmp, user, size) != 0)
1111 #ifdef CONFIG_COMPAT
1113 num_counters = compat_tmp.num_counters;
1114 name = compat_tmp.name;
1118 num_counters = tmp.num_counters;
1122 if (len != size + num_counters * sizeof(struct xt_counters))
1125 paddc = vmalloc_node(len - size, numa_node_id());
1129 if (copy_from_user(paddc, user + size, len - size) != 0) {
1134 t = xt_find_table_lock(NF_ARP, name);
1135 if (!t || IS_ERR(t)) {
1136 ret = t ? PTR_ERR(t) : -ENOENT;
1140 write_lock_bh(&t->lock);
1141 private = t->private;
1142 if (private->number != num_counters) {
1144 goto unlock_up_free;
1148 /* Choose the copy that is on our node */
1149 loc_cpu_entry = private->entries[smp_processor_id()];
1150 ARPT_ENTRY_ITERATE(loc_cpu_entry,
1152 add_counter_to_entry,
1156 write_unlock_bh(&t->lock);
1165 #ifdef CONFIG_COMPAT
1167 compat_release_entry(struct compat_arpt_entry *e, unsigned int *i)
1169 struct arpt_entry_target *t;
1171 if (i && (*i)-- == 0)
1174 t = compat_arpt_get_target(e);
1175 module_put(t->u.kernel.target->me);
1180 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1181 struct xt_table_info *newinfo,
1183 unsigned char *base,
1184 unsigned char *limit,
1185 unsigned int *hook_entries,
1186 unsigned int *underflows,
1190 struct arpt_entry_target *t;
1191 struct xt_target *target;
1192 unsigned int entry_offset;
1195 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1196 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0
1197 || (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
1198 duprintf("Bad offset %p, limit = %p\n", e, limit);
1202 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1203 sizeof(struct compat_xt_entry_target)) {
1204 duprintf("checking: element %p size %u\n",
1209 /* For purposes of check_entry casting the compat entry is fine */
1210 ret = check_entry((struct arpt_entry *)e, name);
1214 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1215 entry_offset = (void *)e - (void *)base;
1217 t = compat_arpt_get_target(e);
1218 target = try_then_request_module(xt_find_target(NF_ARP,
1220 t->u.user.revision),
1221 "arpt_%s", t->u.user.name);
1222 if (IS_ERR(target) || !target) {
1223 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1225 ret = target ? PTR_ERR(target) : -ENOENT;
1228 t->u.kernel.target = target;
1230 off += xt_compat_target_offset(target);
1232 ret = xt_compat_add_offset(NF_ARP, entry_offset, off);
1234 goto release_target;
1236 /* Check hooks & underflows */
1237 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1238 if ((unsigned char *)e - base == hook_entries[h])
1239 newinfo->hook_entry[h] = hook_entries[h];
1240 if ((unsigned char *)e - base == underflows[h])
1241 newinfo->underflow[h] = underflows[h];
1244 /* Clear counters and comefrom */
1245 memset(&e->counters, 0, sizeof(e->counters));
1252 module_put(t->u.kernel.target->me);
1258 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1259 unsigned int *size, const char *name,
1260 struct xt_table_info *newinfo, unsigned char *base)
1262 struct arpt_entry_target *t;
1263 struct xt_target *target;
1264 struct arpt_entry *de;
1265 unsigned int origsize;
1270 de = (struct arpt_entry *)*dstptr;
1271 memcpy(de, e, sizeof(struct arpt_entry));
1272 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1274 *dstptr += sizeof(struct arpt_entry);
1275 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1277 de->target_offset = e->target_offset - (origsize - *size);
1278 t = compat_arpt_get_target(e);
1279 target = t->u.kernel.target;
1280 xt_compat_target_from_user(t, dstptr, size);
1282 de->next_offset = e->next_offset - (origsize - *size);
1283 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1284 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1285 newinfo->hook_entry[h] -= origsize - *size;
1286 if ((unsigned char *)de - base < newinfo->underflow[h])
1287 newinfo->underflow[h] -= origsize - *size;
1292 static inline int compat_check_entry(struct arpt_entry *e, const char *name,
1297 ret = check_target(e, name);
1305 static int translate_compat_table(const char *name,
1306 unsigned int valid_hooks,
1307 struct xt_table_info **pinfo,
1309 unsigned int total_size,
1310 unsigned int number,
1311 unsigned int *hook_entries,
1312 unsigned int *underflows)
1315 struct xt_table_info *newinfo, *info;
1316 void *pos, *entry0, *entry1;
1323 info->number = number;
1325 /* Init all hooks to impossible value. */
1326 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1327 info->hook_entry[i] = 0xFFFFFFFF;
1328 info->underflow[i] = 0xFFFFFFFF;
1331 duprintf("translate_compat_table: size %u\n", info->size);
1333 xt_compat_lock(NF_ARP);
1334 /* Walk through entries, checking offsets. */
1335 ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size,
1336 check_compat_entry_size_and_hooks,
1337 info, &size, entry0,
1338 entry0 + total_size,
1339 hook_entries, underflows, &j, name);
1345 duprintf("translate_compat_table: %u not %u entries\n",
1350 /* Check hooks all assigned */
1351 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1352 /* Only hooks which are valid */
1353 if (!(valid_hooks & (1 << i)))
1355 if (info->hook_entry[i] == 0xFFFFFFFF) {
1356 duprintf("Invalid hook entry %u %u\n",
1357 i, hook_entries[i]);
1360 if (info->underflow[i] == 0xFFFFFFFF) {
1361 duprintf("Invalid underflow %u %u\n",
1368 newinfo = xt_alloc_table_info(size);
1372 newinfo->number = number;
1373 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1374 newinfo->hook_entry[i] = info->hook_entry[i];
1375 newinfo->underflow[i] = info->underflow[i];
1377 entry1 = newinfo->entries[raw_smp_processor_id()];
1380 ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size,
1381 compat_copy_entry_from_user,
1382 &pos, &size, name, newinfo, entry1);
1383 xt_compat_flush_offsets(NF_ARP);
1384 xt_compat_unlock(NF_ARP);
1389 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1393 ret = ARPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
1397 COMPAT_ARPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1398 compat_release_entry, &j);
1399 ARPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1400 xt_free_table_info(newinfo);
1404 /* And one copy for every other CPU */
1405 for_each_possible_cpu(i)
1406 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1407 memcpy(newinfo->entries[i], entry1, newinfo->size);
1411 xt_free_table_info(info);
1415 xt_free_table_info(newinfo);
1417 COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
1420 xt_compat_flush_offsets(NF_ARP);
1421 xt_compat_unlock(NF_ARP);
1425 struct compat_arpt_replace {
1426 char name[ARPT_TABLE_MAXNAMELEN];
1430 u32 hook_entry[NF_ARP_NUMHOOKS];
1431 u32 underflow[NF_ARP_NUMHOOKS];
1433 compat_uptr_t counters;
1434 struct compat_arpt_entry entries[0];
1437 static int compat_do_replace(void __user *user, unsigned int len)
1440 struct compat_arpt_replace tmp;
1441 struct xt_table_info *newinfo;
1442 void *loc_cpu_entry;
1444 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1447 /* overflow check */
1448 if (tmp.size >= INT_MAX / num_possible_cpus())
1450 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1453 newinfo = xt_alloc_table_info(tmp.size);
1457 /* choose the copy that is on our node/cpu */
1458 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1459 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1464 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1465 &newinfo, &loc_cpu_entry, tmp.size,
1466 tmp.num_entries, tmp.hook_entry,
1471 duprintf("compat_do_replace: Translated table\n");
1473 ret = __do_replace(tmp.name, tmp.valid_hooks, newinfo,
1474 tmp.num_counters, compat_ptr(tmp.counters));
1476 goto free_newinfo_untrans;
1479 free_newinfo_untrans:
1480 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1482 xt_free_table_info(newinfo);
1486 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1491 if (!capable(CAP_NET_ADMIN))
1495 case ARPT_SO_SET_REPLACE:
1496 ret = compat_do_replace(user, len);
1499 case ARPT_SO_SET_ADD_COUNTERS:
1500 ret = do_add_counters(user, len, 1);
1504 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1511 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1512 compat_uint_t *size,
1513 struct xt_counters *counters,
1516 struct arpt_entry_target *t;
1517 struct compat_arpt_entry __user *ce;
1518 u_int16_t target_offset, next_offset;
1519 compat_uint_t origsize;
1524 ce = (struct compat_arpt_entry __user *)*dstptr;
1525 if (copy_to_user(ce, e, sizeof(struct arpt_entry)))
1528 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1531 *dstptr += sizeof(struct compat_arpt_entry);
1532 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1534 target_offset = e->target_offset - (origsize - *size);
1536 t = arpt_get_target(e);
1537 ret = xt_compat_target_to_user(t, dstptr, size);
1541 next_offset = e->next_offset - (origsize - *size);
1542 if (put_user(target_offset, &ce->target_offset))
1544 if (put_user(next_offset, &ce->next_offset))
1553 static int compat_copy_entries_to_user(unsigned int total_size,
1554 struct arpt_table *table,
1555 void __user *userptr)
1557 struct xt_counters *counters;
1558 struct xt_table_info *private = table->private;
1562 void *loc_cpu_entry;
1565 counters = alloc_counters(table);
1566 if (IS_ERR(counters))
1567 return PTR_ERR(counters);
1569 /* choose the copy on our node/cpu */
1570 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1573 ret = ARPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
1574 compat_copy_entry_to_user,
1575 &pos, &size, counters, &i);
1580 struct compat_arpt_get_entries {
1581 char name[ARPT_TABLE_MAXNAMELEN];
1583 struct compat_arpt_entry entrytable[0];
1586 static int compat_get_entries(struct compat_arpt_get_entries __user *uptr,
1590 struct compat_arpt_get_entries get;
1591 struct arpt_table *t;
1593 if (*len < sizeof(get)) {
1594 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1597 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1599 if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1600 duprintf("compat_get_entries: %u != %zu\n",
1601 *len, sizeof(get) + get.size);
1605 xt_compat_lock(NF_ARP);
1606 t = xt_find_table_lock(NF_ARP, get.name);
1607 if (t && !IS_ERR(t)) {
1608 struct xt_table_info *private = t->private;
1609 struct xt_table_info info;
1611 duprintf("t->private->number = %u\n", private->number);
1612 ret = compat_table_info(private, &info);
1613 if (!ret && get.size == info.size) {
1614 ret = compat_copy_entries_to_user(private->size,
1615 t, uptr->entrytable);
1617 duprintf("compat_get_entries: I've got %u not %u!\n",
1618 private->size, get.size);
1621 xt_compat_flush_offsets(NF_ARP);
1625 ret = t ? PTR_ERR(t) : -ENOENT;
1627 xt_compat_unlock(NF_ARP);
1631 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1633 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1638 if (!capable(CAP_NET_ADMIN))
1642 case ARPT_SO_GET_INFO:
1643 ret = get_info(user, len, 1);
1645 case ARPT_SO_GET_ENTRIES:
1646 ret = compat_get_entries(user, len);
1649 ret = do_arpt_get_ctl(sk, cmd, user, len);
1655 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1659 if (!capable(CAP_NET_ADMIN))
1663 case ARPT_SO_SET_REPLACE:
1664 ret = do_replace(user, len);
1667 case ARPT_SO_SET_ADD_COUNTERS:
1668 ret = do_add_counters(user, len, 0);
1672 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1679 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1683 if (!capable(CAP_NET_ADMIN))
1687 case ARPT_SO_GET_INFO:
1688 ret = get_info(user, len, 0);
1691 case ARPT_SO_GET_ENTRIES:
1692 ret = get_entries(user, len);
1695 case ARPT_SO_GET_REVISION_TARGET: {
1696 struct xt_get_revision rev;
1698 if (*len != sizeof(rev)) {
1702 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1707 try_then_request_module(xt_find_revision(NF_ARP, rev.name,
1708 rev.revision, 1, &ret),
1709 "arpt_%s", rev.name);
1714 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1721 int arpt_register_table(struct arpt_table *table,
1722 const struct arpt_replace *repl)
1725 struct xt_table_info *newinfo;
1726 struct xt_table_info bootstrap
1727 = { 0, 0, 0, { 0 }, { 0 }, { } };
1728 void *loc_cpu_entry;
1730 newinfo = xt_alloc_table_info(repl->size);
1736 /* choose the copy on our node/cpu */
1737 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1738 memcpy(loc_cpu_entry, repl->entries, repl->size);
1740 ret = translate_table(table->name, table->valid_hooks,
1741 newinfo, loc_cpu_entry, repl->size,
1746 duprintf("arpt_register_table: translate table gives %d\n", ret);
1748 xt_free_table_info(newinfo);
1752 ret = xt_register_table(table, &bootstrap, newinfo);
1754 xt_free_table_info(newinfo);
1761 void arpt_unregister_table(struct arpt_table *table)
1763 struct xt_table_info *private;
1764 void *loc_cpu_entry;
1766 private = xt_unregister_table(table);
1768 /* Decrease module usage counts and free resources */
1769 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1770 ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size,
1771 cleanup_entry, NULL);
1772 xt_free_table_info(private);
1775 /* The built-in targets: standard (NULL) and error. */
1776 static struct arpt_target arpt_standard_target __read_mostly = {
1777 .name = ARPT_STANDARD_TARGET,
1778 .targetsize = sizeof(int),
1780 #ifdef CONFIG_COMPAT
1781 .compatsize = sizeof(compat_int_t),
1782 .compat_from_user = compat_standard_from_user,
1783 .compat_to_user = compat_standard_to_user,
1787 static struct arpt_target arpt_error_target __read_mostly = {
1788 .name = ARPT_ERROR_TARGET,
1789 .target = arpt_error,
1790 .targetsize = ARPT_FUNCTION_MAXNAMELEN,
1794 static struct nf_sockopt_ops arpt_sockopts = {
1796 .set_optmin = ARPT_BASE_CTL,
1797 .set_optmax = ARPT_SO_SET_MAX+1,
1798 .set = do_arpt_set_ctl,
1799 #ifdef CONFIG_COMPAT
1800 .compat_set = compat_do_arpt_set_ctl,
1802 .get_optmin = ARPT_BASE_CTL,
1803 .get_optmax = ARPT_SO_GET_MAX+1,
1804 .get = do_arpt_get_ctl,
1805 #ifdef CONFIG_COMPAT
1806 .compat_get = compat_do_arpt_get_ctl,
1808 .owner = THIS_MODULE,
1811 static int __init arp_tables_init(void)
1815 ret = xt_proto_init(NF_ARP);
1819 /* Noone else will be downing sem now, so we won't sleep */
1820 ret = xt_register_target(&arpt_standard_target);
1823 ret = xt_register_target(&arpt_error_target);
1827 /* Register setsockopt */
1828 ret = nf_register_sockopt(&arpt_sockopts);
1832 printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1836 xt_unregister_target(&arpt_error_target);
1838 xt_unregister_target(&arpt_standard_target);
1840 xt_proto_fini(NF_ARP);
1845 static void __exit arp_tables_fini(void)
1847 nf_unregister_sockopt(&arpt_sockopts);
1848 xt_unregister_target(&arpt_error_target);
1849 xt_unregister_target(&arpt_standard_target);
1850 xt_proto_fini(NF_ARP);
1853 EXPORT_SYMBOL(arpt_register_table);
1854 EXPORT_SYMBOL(arpt_unregister_table);
1855 EXPORT_SYMBOL(arpt_do_table);
1857 module_init(arp_tables_init);
1858 module_exit(arp_tables_fini);