netfilter: xtables: optimize call flow around xt_entry_foreach
[linux-2.6/btrfs-unstable.git] / net / ipv4 / netfilter / arp_tables.c
blob5fdedeb46218991c40a4686e72bcb89d4dae3114
1 /*
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)
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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 <net/sock.h>
26 #include <asm/uaccess.h>
28 #include <linux/netfilter/x_tables.h>
29 #include <linux/netfilter_arp/arp_tables.h>
30 #include "../../netfilter/xt_repldata.h"
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
34 MODULE_DESCRIPTION("arptables core");
36 /*#define DEBUG_ARP_TABLES*/
37 /*#define DEBUG_ARP_TABLES_USER*/
39 #ifdef DEBUG_ARP_TABLES
40 #define dprintf(format, args...) printk(format , ## args)
41 #else
42 #define dprintf(format, args...)
43 #endif
45 #ifdef DEBUG_ARP_TABLES_USER
46 #define duprintf(format, args...) printk(format , ## args)
47 #else
48 #define duprintf(format, args...)
49 #endif
51 #ifdef CONFIG_NETFILTER_DEBUG
52 #define ARP_NF_ASSERT(x) \
53 do { \
54 if (!(x)) \
55 printk("ARP_NF_ASSERT: %s:%s:%u\n", \
56 __func__, __FILE__, __LINE__); \
57 } while(0)
58 #else
59 #define ARP_NF_ASSERT(x)
60 #endif
62 void *arpt_alloc_initial_table(const struct xt_table *info)
64 return xt_alloc_initial_table(arpt, ARPT);
66 EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
68 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
69 const char *hdr_addr, int len)
71 int i, ret;
73 if (len > ARPT_DEV_ADDR_LEN_MAX)
74 len = ARPT_DEV_ADDR_LEN_MAX;
76 ret = 0;
77 for (i = 0; i < len; i++)
78 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
80 return (ret != 0);
84 * Unfortunatly, _b and _mask are not aligned to an int (or long int)
85 * Some arches dont care, unrolling the loop is a win on them.
86 * For other arches, we only have a 16bit alignement.
88 static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
90 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
91 unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
92 #else
93 unsigned long ret = 0;
94 const u16 *a = (const u16 *)_a;
95 const u16 *b = (const u16 *)_b;
96 const u16 *mask = (const u16 *)_mask;
97 int i;
99 for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
100 ret |= (a[i] ^ b[i]) & mask[i];
101 #endif
102 return ret;
105 /* Returns whether packet matches rule or not. */
106 static inline int arp_packet_match(const struct arphdr *arphdr,
107 struct net_device *dev,
108 const char *indev,
109 const char *outdev,
110 const struct arpt_arp *arpinfo)
112 const char *arpptr = (char *)(arphdr + 1);
113 const char *src_devaddr, *tgt_devaddr;
114 __be32 src_ipaddr, tgt_ipaddr;
115 long ret;
117 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
119 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
120 ARPT_INV_ARPOP)) {
121 dprintf("ARP operation field mismatch.\n");
122 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
123 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
124 return 0;
127 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
128 ARPT_INV_ARPHRD)) {
129 dprintf("ARP hardware address format mismatch.\n");
130 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
131 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
132 return 0;
135 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
136 ARPT_INV_ARPPRO)) {
137 dprintf("ARP protocol address format mismatch.\n");
138 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
139 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
140 return 0;
143 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
144 ARPT_INV_ARPHLN)) {
145 dprintf("ARP hardware address length mismatch.\n");
146 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
147 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
148 return 0;
151 src_devaddr = arpptr;
152 arpptr += dev->addr_len;
153 memcpy(&src_ipaddr, arpptr, sizeof(u32));
154 arpptr += sizeof(u32);
155 tgt_devaddr = arpptr;
156 arpptr += dev->addr_len;
157 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
159 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
160 ARPT_INV_SRCDEVADDR) ||
161 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
162 ARPT_INV_TGTDEVADDR)) {
163 dprintf("Source or target device address mismatch.\n");
165 return 0;
168 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
169 ARPT_INV_SRCIP) ||
170 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
171 ARPT_INV_TGTIP)) {
172 dprintf("Source or target IP address mismatch.\n");
174 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
175 &src_ipaddr,
176 &arpinfo->smsk.s_addr,
177 &arpinfo->src.s_addr,
178 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
179 dprintf("TGT: %pI4 Mask: %pI4 Target: %pI4.%s\n",
180 &tgt_ipaddr,
181 &arpinfo->tmsk.s_addr,
182 &arpinfo->tgt.s_addr,
183 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
184 return 0;
187 /* Look for ifname matches. */
188 ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
190 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
191 dprintf("VIA in mismatch (%s vs %s).%s\n",
192 indev, arpinfo->iniface,
193 arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
194 return 0;
197 ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
199 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
200 dprintf("VIA out mismatch (%s vs %s).%s\n",
201 outdev, arpinfo->outiface,
202 arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
203 return 0;
206 return 1;
207 #undef FWINV
210 static inline int arp_checkentry(const struct arpt_arp *arp)
212 if (arp->flags & ~ARPT_F_MASK) {
213 duprintf("Unknown flag bits set: %08X\n",
214 arp->flags & ~ARPT_F_MASK);
215 return 0;
217 if (arp->invflags & ~ARPT_INV_MASK) {
218 duprintf("Unknown invflag bits set: %08X\n",
219 arp->invflags & ~ARPT_INV_MASK);
220 return 0;
223 return 1;
226 static unsigned int
227 arpt_error(struct sk_buff *skb, const struct xt_target_param *par)
229 if (net_ratelimit())
230 printk("arp_tables: error: '%s'\n",
231 (const char *)par->targinfo);
233 return NF_DROP;
236 static inline const struct arpt_entry_target *
237 arpt_get_target_c(const struct arpt_entry *e)
239 return arpt_get_target((struct arpt_entry *)e);
242 static inline struct arpt_entry *
243 get_entry(const void *base, unsigned int offset)
245 return (struct arpt_entry *)(base + offset);
248 static inline __pure
249 struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
251 return (void *)entry + entry->next_offset;
254 unsigned int arpt_do_table(struct sk_buff *skb,
255 unsigned int hook,
256 const struct net_device *in,
257 const struct net_device *out,
258 struct xt_table *table)
260 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
261 unsigned int verdict = NF_DROP;
262 const struct arphdr *arp;
263 bool hotdrop = false;
264 struct arpt_entry *e, *back;
265 const char *indev, *outdev;
266 void *table_base;
267 const struct xt_table_info *private;
268 struct xt_target_param tgpar;
270 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
271 return NF_DROP;
273 indev = in ? in->name : nulldevname;
274 outdev = out ? out->name : nulldevname;
276 xt_info_rdlock_bh();
277 private = table->private;
278 table_base = private->entries[smp_processor_id()];
280 e = get_entry(table_base, private->hook_entry[hook]);
281 back = get_entry(table_base, private->underflow[hook]);
283 tgpar.in = in;
284 tgpar.out = out;
285 tgpar.hooknum = hook;
286 tgpar.family = NFPROTO_ARP;
288 arp = arp_hdr(skb);
289 do {
290 const struct arpt_entry_target *t;
291 int hdr_len;
293 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
294 e = arpt_next_entry(e);
295 continue;
298 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
299 (2 * skb->dev->addr_len);
300 ADD_COUNTER(e->counters, hdr_len, 1);
302 t = arpt_get_target_c(e);
304 /* Standard target? */
305 if (!t->u.kernel.target->target) {
306 int v;
308 v = ((struct arpt_standard_target *)t)->verdict;
309 if (v < 0) {
310 /* Pop from stack? */
311 if (v != ARPT_RETURN) {
312 verdict = (unsigned)(-v) - 1;
313 break;
315 e = back;
316 back = get_entry(table_base, back->comefrom);
317 continue;
319 if (table_base + v
320 != arpt_next_entry(e)) {
321 /* Save old back ptr in next entry */
322 struct arpt_entry *next = arpt_next_entry(e);
323 next->comefrom = (void *)back - table_base;
325 /* set back pointer to next entry */
326 back = next;
329 e = get_entry(table_base, v);
330 continue;
333 /* Targets which reenter must return
334 * abs. verdicts
336 tgpar.target = t->u.kernel.target;
337 tgpar.targinfo = t->data;
338 verdict = t->u.kernel.target->target(skb, &tgpar);
340 /* Target might have changed stuff. */
341 arp = arp_hdr(skb);
343 if (verdict == ARPT_CONTINUE)
344 e = arpt_next_entry(e);
345 else
346 /* Verdict */
347 break;
348 } while (!hotdrop);
349 xt_info_rdunlock_bh();
351 if (hotdrop)
352 return NF_DROP;
353 else
354 return verdict;
357 /* All zeroes == unconditional rule. */
358 static inline bool unconditional(const struct arpt_arp *arp)
360 static const struct arpt_arp uncond;
362 return memcmp(arp, &uncond, sizeof(uncond)) == 0;
365 /* Figures out from what hook each rule can be called: returns 0 if
366 * there are loops. Puts hook bitmask in comefrom.
368 static int mark_source_chains(const struct xt_table_info *newinfo,
369 unsigned int valid_hooks, void *entry0)
371 unsigned int hook;
373 /* No recursion; use packet counter to save back ptrs (reset
374 * to 0 as we leave), and comefrom to save source hook bitmask.
376 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
377 unsigned int pos = newinfo->hook_entry[hook];
378 struct arpt_entry *e
379 = (struct arpt_entry *)(entry0 + pos);
381 if (!(valid_hooks & (1 << hook)))
382 continue;
384 /* Set initial back pointer. */
385 e->counters.pcnt = pos;
387 for (;;) {
388 const struct arpt_standard_target *t
389 = (void *)arpt_get_target_c(e);
390 int visited = e->comefrom & (1 << hook);
392 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
393 printk("arptables: loop hook %u pos %u %08X.\n",
394 hook, pos, e->comefrom);
395 return 0;
397 e->comefrom
398 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
400 /* Unconditional return/END. */
401 if ((e->target_offset == sizeof(struct arpt_entry) &&
402 (strcmp(t->target.u.user.name,
403 ARPT_STANDARD_TARGET) == 0) &&
404 t->verdict < 0 && unconditional(&e->arp)) ||
405 visited) {
406 unsigned int oldpos, size;
408 if ((strcmp(t->target.u.user.name,
409 ARPT_STANDARD_TARGET) == 0) &&
410 t->verdict < -NF_MAX_VERDICT - 1) {
411 duprintf("mark_source_chains: bad "
412 "negative verdict (%i)\n",
413 t->verdict);
414 return 0;
417 /* Return: backtrack through the last
418 * big jump.
420 do {
421 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
422 oldpos = pos;
423 pos = e->counters.pcnt;
424 e->counters.pcnt = 0;
426 /* We're at the start. */
427 if (pos == oldpos)
428 goto next;
430 e = (struct arpt_entry *)
431 (entry0 + pos);
432 } while (oldpos == pos + e->next_offset);
434 /* Move along one */
435 size = e->next_offset;
436 e = (struct arpt_entry *)
437 (entry0 + pos + size);
438 e->counters.pcnt = pos;
439 pos += size;
440 } else {
441 int newpos = t->verdict;
443 if (strcmp(t->target.u.user.name,
444 ARPT_STANDARD_TARGET) == 0 &&
445 newpos >= 0) {
446 if (newpos > newinfo->size -
447 sizeof(struct arpt_entry)) {
448 duprintf("mark_source_chains: "
449 "bad verdict (%i)\n",
450 newpos);
451 return 0;
454 /* This a jump; chase it. */
455 duprintf("Jump rule %u -> %u\n",
456 pos, newpos);
457 } else {
458 /* ... this is a fallthru */
459 newpos = pos + e->next_offset;
461 e = (struct arpt_entry *)
462 (entry0 + newpos);
463 e->counters.pcnt = pos;
464 pos = newpos;
467 next:
468 duprintf("Finished chain %u\n", hook);
470 return 1;
473 static inline int check_entry(const struct arpt_entry *e, const char *name)
475 const struct arpt_entry_target *t;
477 if (!arp_checkentry(&e->arp)) {
478 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
479 return -EINVAL;
482 if (e->target_offset + sizeof(struct arpt_entry_target) > e->next_offset)
483 return -EINVAL;
485 t = arpt_get_target_c(e);
486 if (e->target_offset + t->u.target_size > e->next_offset)
487 return -EINVAL;
489 return 0;
492 static inline int check_target(struct arpt_entry *e, const char *name)
494 struct arpt_entry_target *t = arpt_get_target(e);
495 int ret;
496 struct xt_tgchk_param par = {
497 .table = name,
498 .entryinfo = e,
499 .target = t->u.kernel.target,
500 .targinfo = t->data,
501 .hook_mask = e->comefrom,
502 .family = NFPROTO_ARP,
505 ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
506 if (ret < 0) {
507 duprintf("arp_tables: check failed for `%s'.\n",
508 t->u.kernel.target->name);
509 return ret;
511 return 0;
514 static inline int
515 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
517 struct arpt_entry_target *t;
518 struct xt_target *target;
519 int ret;
521 ret = check_entry(e, name);
522 if (ret)
523 return ret;
525 t = arpt_get_target(e);
526 target = try_then_request_module(xt_find_target(NFPROTO_ARP,
527 t->u.user.name,
528 t->u.user.revision),
529 "arpt_%s", t->u.user.name);
530 if (IS_ERR(target) || !target) {
531 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
532 ret = target ? PTR_ERR(target) : -ENOENT;
533 goto out;
535 t->u.kernel.target = target;
537 ret = check_target(e, name);
538 if (ret)
539 goto err;
540 return 0;
541 err:
542 module_put(t->u.kernel.target->me);
543 out:
544 return ret;
547 static bool check_underflow(const struct arpt_entry *e)
549 const struct arpt_entry_target *t;
550 unsigned int verdict;
552 if (!unconditional(&e->arp))
553 return false;
554 t = arpt_get_target_c(e);
555 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
556 return false;
557 verdict = ((struct arpt_standard_target *)t)->verdict;
558 verdict = -verdict - 1;
559 return verdict == NF_DROP || verdict == NF_ACCEPT;
562 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
563 struct xt_table_info *newinfo,
564 const unsigned char *base,
565 const unsigned char *limit,
566 const unsigned int *hook_entries,
567 const unsigned int *underflows,
568 unsigned int valid_hooks)
570 unsigned int h;
572 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
573 (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
574 duprintf("Bad offset %p\n", e);
575 return -EINVAL;
578 if (e->next_offset
579 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
580 duprintf("checking: element %p size %u\n",
581 e, e->next_offset);
582 return -EINVAL;
585 /* Check hooks & underflows */
586 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
587 if (!(valid_hooks & (1 << h)))
588 continue;
589 if ((unsigned char *)e - base == hook_entries[h])
590 newinfo->hook_entry[h] = hook_entries[h];
591 if ((unsigned char *)e - base == underflows[h]) {
592 if (!check_underflow(e)) {
593 pr_err("Underflows must be unconditional and "
594 "use the STANDARD target with "
595 "ACCEPT/DROP\n");
596 return -EINVAL;
598 newinfo->underflow[h] = underflows[h];
602 /* Clear counters and comefrom */
603 e->counters = ((struct xt_counters) { 0, 0 });
604 e->comefrom = 0;
605 return 0;
608 static inline void cleanup_entry(struct arpt_entry *e)
610 struct xt_tgdtor_param par;
611 struct arpt_entry_target *t;
613 t = arpt_get_target(e);
614 par.target = t->u.kernel.target;
615 par.targinfo = t->data;
616 par.family = NFPROTO_ARP;
617 if (par.target->destroy != NULL)
618 par.target->destroy(&par);
619 module_put(par.target->me);
622 /* Checks and translates the user-supplied table segment (held in
623 * newinfo).
625 static int translate_table(const char *name,
626 unsigned int valid_hooks,
627 struct xt_table_info *newinfo,
628 void *entry0,
629 unsigned int size,
630 unsigned int number,
631 const unsigned int *hook_entries,
632 const unsigned int *underflows)
634 struct arpt_entry *iter;
635 unsigned int i;
636 int ret = 0;
638 newinfo->size = size;
639 newinfo->number = number;
641 /* Init all hooks to impossible value. */
642 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
643 newinfo->hook_entry[i] = 0xFFFFFFFF;
644 newinfo->underflow[i] = 0xFFFFFFFF;
647 duprintf("translate_table: size %u\n", newinfo->size);
648 i = 0;
650 /* Walk through entries, checking offsets. */
651 xt_entry_foreach(iter, entry0, newinfo->size) {
652 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
653 entry0 + size, hook_entries, underflows, valid_hooks);
654 if (ret != 0)
655 break;
656 ++i;
658 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
659 if (ret != 0)
660 return ret;
662 if (i != number) {
663 duprintf("translate_table: %u not %u entries\n",
664 i, number);
665 return -EINVAL;
668 /* Check hooks all assigned */
669 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
670 /* Only hooks which are valid */
671 if (!(valid_hooks & (1 << i)))
672 continue;
673 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
674 duprintf("Invalid hook entry %u %u\n",
675 i, hook_entries[i]);
676 return -EINVAL;
678 if (newinfo->underflow[i] == 0xFFFFFFFF) {
679 duprintf("Invalid underflow %u %u\n",
680 i, underflows[i]);
681 return -EINVAL;
685 if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
686 duprintf("Looping hook\n");
687 return -ELOOP;
690 /* Finally, each sanity check must pass */
691 i = 0;
692 xt_entry_foreach(iter, entry0, newinfo->size) {
693 ret = find_check_entry(iter, name, size);
694 if (ret != 0)
695 break;
696 ++i;
699 if (ret != 0) {
700 xt_entry_foreach(iter, entry0, newinfo->size) {
701 if (i-- == 0)
702 break;
703 cleanup_entry(iter);
705 return ret;
708 /* And one copy for every other CPU */
709 for_each_possible_cpu(i) {
710 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
711 memcpy(newinfo->entries[i], entry0, newinfo->size);
714 return ret;
717 static void get_counters(const struct xt_table_info *t,
718 struct xt_counters counters[])
720 struct arpt_entry *iter;
721 unsigned int cpu;
722 unsigned int i;
723 unsigned int curcpu;
725 /* Instead of clearing (by a previous call to memset())
726 * the counters and using adds, we set the counters
727 * with data used by 'current' CPU
729 * Bottom half has to be disabled to prevent deadlock
730 * if new softirq were to run and call ipt_do_table
732 local_bh_disable();
733 curcpu = smp_processor_id();
735 i = 0;
736 xt_entry_foreach(iter, t->entries[curcpu], t->size) {
737 SET_COUNTER(counters[i], iter->counters.bcnt,
738 iter->counters.pcnt);
739 ++i;
742 for_each_possible_cpu(cpu) {
743 if (cpu == curcpu)
744 continue;
745 i = 0;
746 xt_info_wrlock(cpu);
747 xt_entry_foreach(iter, t->entries[cpu], t->size) {
748 ADD_COUNTER(counters[i], iter->counters.bcnt,
749 iter->counters.pcnt);
750 ++i;
752 xt_info_wrunlock(cpu);
754 local_bh_enable();
757 static struct xt_counters *alloc_counters(const struct xt_table *table)
759 unsigned int countersize;
760 struct xt_counters *counters;
761 const struct xt_table_info *private = table->private;
763 /* We need atomic snapshot of counters: rest doesn't change
764 * (other than comefrom, which userspace doesn't care
765 * about).
767 countersize = sizeof(struct xt_counters) * private->number;
768 counters = vmalloc_node(countersize, numa_node_id());
770 if (counters == NULL)
771 return ERR_PTR(-ENOMEM);
773 get_counters(private, counters);
775 return counters;
778 static int copy_entries_to_user(unsigned int total_size,
779 const struct xt_table *table,
780 void __user *userptr)
782 unsigned int off, num;
783 const struct arpt_entry *e;
784 struct xt_counters *counters;
785 struct xt_table_info *private = table->private;
786 int ret = 0;
787 void *loc_cpu_entry;
789 counters = alloc_counters(table);
790 if (IS_ERR(counters))
791 return PTR_ERR(counters);
793 loc_cpu_entry = private->entries[raw_smp_processor_id()];
794 /* ... then copy entire thing ... */
795 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
796 ret = -EFAULT;
797 goto free_counters;
800 /* FIXME: use iterator macros --RR */
801 /* ... then go back and fix counters and names */
802 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
803 const struct arpt_entry_target *t;
805 e = (struct arpt_entry *)(loc_cpu_entry + off);
806 if (copy_to_user(userptr + off
807 + offsetof(struct arpt_entry, counters),
808 &counters[num],
809 sizeof(counters[num])) != 0) {
810 ret = -EFAULT;
811 goto free_counters;
814 t = arpt_get_target_c(e);
815 if (copy_to_user(userptr + off + e->target_offset
816 + offsetof(struct arpt_entry_target,
817 u.user.name),
818 t->u.kernel.target->name,
819 strlen(t->u.kernel.target->name)+1) != 0) {
820 ret = -EFAULT;
821 goto free_counters;
825 free_counters:
826 vfree(counters);
827 return ret;
830 #ifdef CONFIG_COMPAT
831 static void compat_standard_from_user(void *dst, const void *src)
833 int v = *(compat_int_t *)src;
835 if (v > 0)
836 v += xt_compat_calc_jump(NFPROTO_ARP, v);
837 memcpy(dst, &v, sizeof(v));
840 static int compat_standard_to_user(void __user *dst, const void *src)
842 compat_int_t cv = *(int *)src;
844 if (cv > 0)
845 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
846 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
849 static int compat_calc_entry(const struct arpt_entry *e,
850 const struct xt_table_info *info,
851 const void *base, struct xt_table_info *newinfo)
853 const struct arpt_entry_target *t;
854 unsigned int entry_offset;
855 int off, i, ret;
857 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
858 entry_offset = (void *)e - base;
860 t = arpt_get_target_c(e);
861 off += xt_compat_target_offset(t->u.kernel.target);
862 newinfo->size -= off;
863 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
864 if (ret)
865 return ret;
867 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
868 if (info->hook_entry[i] &&
869 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
870 newinfo->hook_entry[i] -= off;
871 if (info->underflow[i] &&
872 (e < (struct arpt_entry *)(base + info->underflow[i])))
873 newinfo->underflow[i] -= off;
875 return 0;
878 static int compat_table_info(const struct xt_table_info *info,
879 struct xt_table_info *newinfo)
881 struct arpt_entry *iter;
882 void *loc_cpu_entry;
883 int ret;
885 if (!newinfo || !info)
886 return -EINVAL;
888 /* we dont care about newinfo->entries[] */
889 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
890 newinfo->initial_entries = 0;
891 loc_cpu_entry = info->entries[raw_smp_processor_id()];
892 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
893 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
894 if (ret != 0)
895 return ret;
897 return 0;
899 #endif
901 static int get_info(struct net *net, void __user *user,
902 const int *len, int compat)
904 char name[ARPT_TABLE_MAXNAMELEN];
905 struct xt_table *t;
906 int ret;
908 if (*len != sizeof(struct arpt_getinfo)) {
909 duprintf("length %u != %Zu\n", *len,
910 sizeof(struct arpt_getinfo));
911 return -EINVAL;
914 if (copy_from_user(name, user, sizeof(name)) != 0)
915 return -EFAULT;
917 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
918 #ifdef CONFIG_COMPAT
919 if (compat)
920 xt_compat_lock(NFPROTO_ARP);
921 #endif
922 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
923 "arptable_%s", name);
924 if (t && !IS_ERR(t)) {
925 struct arpt_getinfo info;
926 const struct xt_table_info *private = t->private;
927 #ifdef CONFIG_COMPAT
928 struct xt_table_info tmp;
930 if (compat) {
931 ret = compat_table_info(private, &tmp);
932 xt_compat_flush_offsets(NFPROTO_ARP);
933 private = &tmp;
935 #endif
936 info.valid_hooks = t->valid_hooks;
937 memcpy(info.hook_entry, private->hook_entry,
938 sizeof(info.hook_entry));
939 memcpy(info.underflow, private->underflow,
940 sizeof(info.underflow));
941 info.num_entries = private->number;
942 info.size = private->size;
943 strcpy(info.name, name);
945 if (copy_to_user(user, &info, *len) != 0)
946 ret = -EFAULT;
947 else
948 ret = 0;
949 xt_table_unlock(t);
950 module_put(t->me);
951 } else
952 ret = t ? PTR_ERR(t) : -ENOENT;
953 #ifdef CONFIG_COMPAT
954 if (compat)
955 xt_compat_unlock(NFPROTO_ARP);
956 #endif
957 return ret;
960 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
961 const int *len)
963 int ret;
964 struct arpt_get_entries get;
965 struct xt_table *t;
967 if (*len < sizeof(get)) {
968 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
969 return -EINVAL;
971 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
972 return -EFAULT;
973 if (*len != sizeof(struct arpt_get_entries) + get.size) {
974 duprintf("get_entries: %u != %Zu\n", *len,
975 sizeof(struct arpt_get_entries) + get.size);
976 return -EINVAL;
979 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
980 if (t && !IS_ERR(t)) {
981 const struct xt_table_info *private = t->private;
983 duprintf("t->private->number = %u\n",
984 private->number);
985 if (get.size == private->size)
986 ret = copy_entries_to_user(private->size,
987 t, uptr->entrytable);
988 else {
989 duprintf("get_entries: I've got %u not %u!\n",
990 private->size, get.size);
991 ret = -EAGAIN;
993 module_put(t->me);
994 xt_table_unlock(t);
995 } else
996 ret = t ? PTR_ERR(t) : -ENOENT;
998 return ret;
1001 static int __do_replace(struct net *net, const char *name,
1002 unsigned int valid_hooks,
1003 struct xt_table_info *newinfo,
1004 unsigned int num_counters,
1005 void __user *counters_ptr)
1007 int ret;
1008 struct xt_table *t;
1009 struct xt_table_info *oldinfo;
1010 struct xt_counters *counters;
1011 void *loc_cpu_old_entry;
1012 struct arpt_entry *iter;
1014 ret = 0;
1015 counters = vmalloc_node(num_counters * sizeof(struct xt_counters),
1016 numa_node_id());
1017 if (!counters) {
1018 ret = -ENOMEM;
1019 goto out;
1022 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
1023 "arptable_%s", name);
1024 if (!t || IS_ERR(t)) {
1025 ret = t ? PTR_ERR(t) : -ENOENT;
1026 goto free_newinfo_counters_untrans;
1029 /* You lied! */
1030 if (valid_hooks != t->valid_hooks) {
1031 duprintf("Valid hook crap: %08X vs %08X\n",
1032 valid_hooks, t->valid_hooks);
1033 ret = -EINVAL;
1034 goto put_module;
1037 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1038 if (!oldinfo)
1039 goto put_module;
1041 /* Update module usage count based on number of rules */
1042 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1043 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1044 if ((oldinfo->number > oldinfo->initial_entries) ||
1045 (newinfo->number <= oldinfo->initial_entries))
1046 module_put(t->me);
1047 if ((oldinfo->number > oldinfo->initial_entries) &&
1048 (newinfo->number <= oldinfo->initial_entries))
1049 module_put(t->me);
1051 /* Get the old counters, and synchronize with replace */
1052 get_counters(oldinfo, counters);
1054 /* Decrease module usage counts and free resource */
1055 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1056 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
1057 cleanup_entry(iter);
1059 xt_free_table_info(oldinfo);
1060 if (copy_to_user(counters_ptr, counters,
1061 sizeof(struct xt_counters) * num_counters) != 0)
1062 ret = -EFAULT;
1063 vfree(counters);
1064 xt_table_unlock(t);
1065 return ret;
1067 put_module:
1068 module_put(t->me);
1069 xt_table_unlock(t);
1070 free_newinfo_counters_untrans:
1071 vfree(counters);
1072 out:
1073 return ret;
1076 static int do_replace(struct net *net, const void __user *user,
1077 unsigned int len)
1079 int ret;
1080 struct arpt_replace tmp;
1081 struct xt_table_info *newinfo;
1082 void *loc_cpu_entry;
1083 struct arpt_entry *iter;
1085 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1086 return -EFAULT;
1088 /* overflow check */
1089 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1090 return -ENOMEM;
1092 newinfo = xt_alloc_table_info(tmp.size);
1093 if (!newinfo)
1094 return -ENOMEM;
1096 /* choose the copy that is on our node/cpu */
1097 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1098 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1099 tmp.size) != 0) {
1100 ret = -EFAULT;
1101 goto free_newinfo;
1104 ret = translate_table(tmp.name, tmp.valid_hooks,
1105 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
1106 tmp.hook_entry, tmp.underflow);
1107 if (ret != 0)
1108 goto free_newinfo;
1110 duprintf("arp_tables: Translated table\n");
1112 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1113 tmp.num_counters, tmp.counters);
1114 if (ret)
1115 goto free_newinfo_untrans;
1116 return 0;
1118 free_newinfo_untrans:
1119 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1120 cleanup_entry(iter);
1121 free_newinfo:
1122 xt_free_table_info(newinfo);
1123 return ret;
1126 static int do_add_counters(struct net *net, const void __user *user,
1127 unsigned int len, int compat)
1129 unsigned int i, curcpu;
1130 struct xt_counters_info tmp;
1131 struct xt_counters *paddc;
1132 unsigned int num_counters;
1133 const char *name;
1134 int size;
1135 void *ptmp;
1136 struct xt_table *t;
1137 const struct xt_table_info *private;
1138 int ret = 0;
1139 void *loc_cpu_entry;
1140 struct arpt_entry *iter;
1141 #ifdef CONFIG_COMPAT
1142 struct compat_xt_counters_info compat_tmp;
1144 if (compat) {
1145 ptmp = &compat_tmp;
1146 size = sizeof(struct compat_xt_counters_info);
1147 } else
1148 #endif
1150 ptmp = &tmp;
1151 size = sizeof(struct xt_counters_info);
1154 if (copy_from_user(ptmp, user, size) != 0)
1155 return -EFAULT;
1157 #ifdef CONFIG_COMPAT
1158 if (compat) {
1159 num_counters = compat_tmp.num_counters;
1160 name = compat_tmp.name;
1161 } else
1162 #endif
1164 num_counters = tmp.num_counters;
1165 name = tmp.name;
1168 if (len != size + num_counters * sizeof(struct xt_counters))
1169 return -EINVAL;
1171 paddc = vmalloc_node(len - size, numa_node_id());
1172 if (!paddc)
1173 return -ENOMEM;
1175 if (copy_from_user(paddc, user + size, len - size) != 0) {
1176 ret = -EFAULT;
1177 goto free;
1180 t = xt_find_table_lock(net, NFPROTO_ARP, name);
1181 if (!t || IS_ERR(t)) {
1182 ret = t ? PTR_ERR(t) : -ENOENT;
1183 goto free;
1186 local_bh_disable();
1187 private = t->private;
1188 if (private->number != num_counters) {
1189 ret = -EINVAL;
1190 goto unlock_up_free;
1193 i = 0;
1194 /* Choose the copy that is on our node */
1195 curcpu = smp_processor_id();
1196 loc_cpu_entry = private->entries[curcpu];
1197 xt_info_wrlock(curcpu);
1198 xt_entry_foreach(iter, loc_cpu_entry, private->size) {
1199 ADD_COUNTER(iter->counters, paddc[i].bcnt, paddc[i].pcnt);
1200 ++i;
1202 xt_info_wrunlock(curcpu);
1203 unlock_up_free:
1204 local_bh_enable();
1205 xt_table_unlock(t);
1206 module_put(t->me);
1207 free:
1208 vfree(paddc);
1210 return ret;
1213 #ifdef CONFIG_COMPAT
1214 static inline void compat_release_entry(struct compat_arpt_entry *e)
1216 struct arpt_entry_target *t;
1218 t = compat_arpt_get_target(e);
1219 module_put(t->u.kernel.target->me);
1222 static inline int
1223 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1224 struct xt_table_info *newinfo,
1225 unsigned int *size,
1226 const unsigned char *base,
1227 const unsigned char *limit,
1228 const unsigned int *hook_entries,
1229 const unsigned int *underflows,
1230 const char *name)
1232 struct arpt_entry_target *t;
1233 struct xt_target *target;
1234 unsigned int entry_offset;
1235 int ret, off, h;
1237 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1238 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1239 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
1240 duprintf("Bad offset %p, limit = %p\n", e, limit);
1241 return -EINVAL;
1244 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1245 sizeof(struct compat_xt_entry_target)) {
1246 duprintf("checking: element %p size %u\n",
1247 e, e->next_offset);
1248 return -EINVAL;
1251 /* For purposes of check_entry casting the compat entry is fine */
1252 ret = check_entry((struct arpt_entry *)e, name);
1253 if (ret)
1254 return ret;
1256 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1257 entry_offset = (void *)e - (void *)base;
1259 t = compat_arpt_get_target(e);
1260 target = try_then_request_module(xt_find_target(NFPROTO_ARP,
1261 t->u.user.name,
1262 t->u.user.revision),
1263 "arpt_%s", t->u.user.name);
1264 if (IS_ERR(target) || !target) {
1265 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1266 t->u.user.name);
1267 ret = target ? PTR_ERR(target) : -ENOENT;
1268 goto out;
1270 t->u.kernel.target = target;
1272 off += xt_compat_target_offset(target);
1273 *size += off;
1274 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1275 if (ret)
1276 goto release_target;
1278 /* Check hooks & underflows */
1279 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1280 if ((unsigned char *)e - base == hook_entries[h])
1281 newinfo->hook_entry[h] = hook_entries[h];
1282 if ((unsigned char *)e - base == underflows[h])
1283 newinfo->underflow[h] = underflows[h];
1286 /* Clear counters and comefrom */
1287 memset(&e->counters, 0, sizeof(e->counters));
1288 e->comefrom = 0;
1289 return 0;
1291 release_target:
1292 module_put(t->u.kernel.target->me);
1293 out:
1294 return ret;
1297 static int
1298 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1299 unsigned int *size, const char *name,
1300 struct xt_table_info *newinfo, unsigned char *base)
1302 struct arpt_entry_target *t;
1303 struct xt_target *target;
1304 struct arpt_entry *de;
1305 unsigned int origsize;
1306 int ret, h;
1308 ret = 0;
1309 origsize = *size;
1310 de = (struct arpt_entry *)*dstptr;
1311 memcpy(de, e, sizeof(struct arpt_entry));
1312 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1314 *dstptr += sizeof(struct arpt_entry);
1315 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1317 de->target_offset = e->target_offset - (origsize - *size);
1318 t = compat_arpt_get_target(e);
1319 target = t->u.kernel.target;
1320 xt_compat_target_from_user(t, dstptr, size);
1322 de->next_offset = e->next_offset - (origsize - *size);
1323 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1324 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1325 newinfo->hook_entry[h] -= origsize - *size;
1326 if ((unsigned char *)de - base < newinfo->underflow[h])
1327 newinfo->underflow[h] -= origsize - *size;
1329 return ret;
1332 static int translate_compat_table(const char *name,
1333 unsigned int valid_hooks,
1334 struct xt_table_info **pinfo,
1335 void **pentry0,
1336 unsigned int total_size,
1337 unsigned int number,
1338 unsigned int *hook_entries,
1339 unsigned int *underflows)
1341 unsigned int i, j;
1342 struct xt_table_info *newinfo, *info;
1343 void *pos, *entry0, *entry1;
1344 struct compat_arpt_entry *iter0;
1345 struct arpt_entry *iter1;
1346 unsigned int size;
1347 int ret = 0;
1349 info = *pinfo;
1350 entry0 = *pentry0;
1351 size = total_size;
1352 info->number = number;
1354 /* Init all hooks to impossible value. */
1355 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1356 info->hook_entry[i] = 0xFFFFFFFF;
1357 info->underflow[i] = 0xFFFFFFFF;
1360 duprintf("translate_compat_table: size %u\n", info->size);
1361 j = 0;
1362 xt_compat_lock(NFPROTO_ARP);
1363 /* Walk through entries, checking offsets. */
1364 xt_entry_foreach(iter0, entry0, total_size) {
1365 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1366 entry0, entry0 + total_size, hook_entries, underflows,
1367 name);
1368 if (ret != 0)
1369 goto out_unlock;
1370 ++j;
1373 ret = -EINVAL;
1374 if (j != number) {
1375 duprintf("translate_compat_table: %u not %u entries\n",
1376 j, number);
1377 goto out_unlock;
1380 /* Check hooks all assigned */
1381 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1382 /* Only hooks which are valid */
1383 if (!(valid_hooks & (1 << i)))
1384 continue;
1385 if (info->hook_entry[i] == 0xFFFFFFFF) {
1386 duprintf("Invalid hook entry %u %u\n",
1387 i, hook_entries[i]);
1388 goto out_unlock;
1390 if (info->underflow[i] == 0xFFFFFFFF) {
1391 duprintf("Invalid underflow %u %u\n",
1392 i, underflows[i]);
1393 goto out_unlock;
1397 ret = -ENOMEM;
1398 newinfo = xt_alloc_table_info(size);
1399 if (!newinfo)
1400 goto out_unlock;
1402 newinfo->number = number;
1403 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1404 newinfo->hook_entry[i] = info->hook_entry[i];
1405 newinfo->underflow[i] = info->underflow[i];
1407 entry1 = newinfo->entries[raw_smp_processor_id()];
1408 pos = entry1;
1409 size = total_size;
1410 xt_entry_foreach(iter0, entry0, total_size) {
1411 ret = compat_copy_entry_from_user(iter0, &pos,
1412 &size, name, newinfo, entry1);
1413 if (ret != 0)
1414 break;
1416 xt_compat_flush_offsets(NFPROTO_ARP);
1417 xt_compat_unlock(NFPROTO_ARP);
1418 if (ret)
1419 goto free_newinfo;
1421 ret = -ELOOP;
1422 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1423 goto free_newinfo;
1425 i = 0;
1426 xt_entry_foreach(iter1, entry1, newinfo->size) {
1427 ret = check_target(iter1, name);
1428 if (ret != 0)
1429 break;
1430 ++i;
1432 if (ret) {
1434 * The first i matches need cleanup_entry (calls ->destroy)
1435 * because they had called ->check already. The other j-i
1436 * entries need only release.
1438 int skip = i;
1439 j -= i;
1440 xt_entry_foreach(iter0, entry0, newinfo->size) {
1441 if (skip-- > 0)
1442 continue;
1443 if (j-- == 0)
1444 break;
1445 compat_release_entry(iter0);
1447 xt_entry_foreach(iter1, entry1, newinfo->size) {
1448 if (i-- == 0)
1449 break;
1450 cleanup_entry(iter1);
1452 xt_free_table_info(newinfo);
1453 return ret;
1456 /* And one copy for every other CPU */
1457 for_each_possible_cpu(i)
1458 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1459 memcpy(newinfo->entries[i], entry1, newinfo->size);
1461 *pinfo = newinfo;
1462 *pentry0 = entry1;
1463 xt_free_table_info(info);
1464 return 0;
1466 free_newinfo:
1467 xt_free_table_info(newinfo);
1468 out:
1469 xt_entry_foreach(iter0, entry0, total_size) {
1470 if (j-- == 0)
1471 break;
1472 compat_release_entry(iter0);
1474 return ret;
1475 out_unlock:
1476 xt_compat_flush_offsets(NFPROTO_ARP);
1477 xt_compat_unlock(NFPROTO_ARP);
1478 goto out;
1481 struct compat_arpt_replace {
1482 char name[ARPT_TABLE_MAXNAMELEN];
1483 u32 valid_hooks;
1484 u32 num_entries;
1485 u32 size;
1486 u32 hook_entry[NF_ARP_NUMHOOKS];
1487 u32 underflow[NF_ARP_NUMHOOKS];
1488 u32 num_counters;
1489 compat_uptr_t counters;
1490 struct compat_arpt_entry entries[0];
1493 static int compat_do_replace(struct net *net, void __user *user,
1494 unsigned int len)
1496 int ret;
1497 struct compat_arpt_replace tmp;
1498 struct xt_table_info *newinfo;
1499 void *loc_cpu_entry;
1500 struct arpt_entry *iter;
1502 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1503 return -EFAULT;
1505 /* overflow check */
1506 if (tmp.size >= INT_MAX / num_possible_cpus())
1507 return -ENOMEM;
1508 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1509 return -ENOMEM;
1511 newinfo = xt_alloc_table_info(tmp.size);
1512 if (!newinfo)
1513 return -ENOMEM;
1515 /* choose the copy that is on our node/cpu */
1516 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1517 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1518 ret = -EFAULT;
1519 goto free_newinfo;
1522 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1523 &newinfo, &loc_cpu_entry, tmp.size,
1524 tmp.num_entries, tmp.hook_entry,
1525 tmp.underflow);
1526 if (ret != 0)
1527 goto free_newinfo;
1529 duprintf("compat_do_replace: Translated table\n");
1531 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1532 tmp.num_counters, compat_ptr(tmp.counters));
1533 if (ret)
1534 goto free_newinfo_untrans;
1535 return 0;
1537 free_newinfo_untrans:
1538 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1539 cleanup_entry(iter);
1540 free_newinfo:
1541 xt_free_table_info(newinfo);
1542 return ret;
1545 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1546 unsigned int len)
1548 int ret;
1550 if (!capable(CAP_NET_ADMIN))
1551 return -EPERM;
1553 switch (cmd) {
1554 case ARPT_SO_SET_REPLACE:
1555 ret = compat_do_replace(sock_net(sk), user, len);
1556 break;
1558 case ARPT_SO_SET_ADD_COUNTERS:
1559 ret = do_add_counters(sock_net(sk), user, len, 1);
1560 break;
1562 default:
1563 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1564 ret = -EINVAL;
1567 return ret;
1570 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1571 compat_uint_t *size,
1572 struct xt_counters *counters,
1573 unsigned int i)
1575 struct arpt_entry_target *t;
1576 struct compat_arpt_entry __user *ce;
1577 u_int16_t target_offset, next_offset;
1578 compat_uint_t origsize;
1579 int ret;
1581 origsize = *size;
1582 ce = (struct compat_arpt_entry __user *)*dstptr;
1583 if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1584 copy_to_user(&ce->counters, &counters[i],
1585 sizeof(counters[i])) != 0)
1586 return -EFAULT;
1588 *dstptr += sizeof(struct compat_arpt_entry);
1589 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1591 target_offset = e->target_offset - (origsize - *size);
1593 t = arpt_get_target(e);
1594 ret = xt_compat_target_to_user(t, dstptr, size);
1595 if (ret)
1596 return ret;
1597 next_offset = e->next_offset - (origsize - *size);
1598 if (put_user(target_offset, &ce->target_offset) != 0 ||
1599 put_user(next_offset, &ce->next_offset) != 0)
1600 return -EFAULT;
1601 return 0;
1604 static int compat_copy_entries_to_user(unsigned int total_size,
1605 struct xt_table *table,
1606 void __user *userptr)
1608 struct xt_counters *counters;
1609 const struct xt_table_info *private = table->private;
1610 void __user *pos;
1611 unsigned int size;
1612 int ret = 0;
1613 void *loc_cpu_entry;
1614 unsigned int i = 0;
1615 struct arpt_entry *iter;
1617 counters = alloc_counters(table);
1618 if (IS_ERR(counters))
1619 return PTR_ERR(counters);
1621 /* choose the copy on our node/cpu */
1622 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1623 pos = userptr;
1624 size = total_size;
1625 xt_entry_foreach(iter, loc_cpu_entry, total_size) {
1626 ret = compat_copy_entry_to_user(iter, &pos,
1627 &size, counters, i++);
1628 if (ret != 0)
1629 break;
1631 vfree(counters);
1632 return ret;
1635 struct compat_arpt_get_entries {
1636 char name[ARPT_TABLE_MAXNAMELEN];
1637 compat_uint_t size;
1638 struct compat_arpt_entry entrytable[0];
1641 static int compat_get_entries(struct net *net,
1642 struct compat_arpt_get_entries __user *uptr,
1643 int *len)
1645 int ret;
1646 struct compat_arpt_get_entries get;
1647 struct xt_table *t;
1649 if (*len < sizeof(get)) {
1650 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1651 return -EINVAL;
1653 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1654 return -EFAULT;
1655 if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1656 duprintf("compat_get_entries: %u != %zu\n",
1657 *len, sizeof(get) + get.size);
1658 return -EINVAL;
1661 xt_compat_lock(NFPROTO_ARP);
1662 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1663 if (t && !IS_ERR(t)) {
1664 const struct xt_table_info *private = t->private;
1665 struct xt_table_info info;
1667 duprintf("t->private->number = %u\n", private->number);
1668 ret = compat_table_info(private, &info);
1669 if (!ret && get.size == info.size) {
1670 ret = compat_copy_entries_to_user(private->size,
1671 t, uptr->entrytable);
1672 } else if (!ret) {
1673 duprintf("compat_get_entries: I've got %u not %u!\n",
1674 private->size, get.size);
1675 ret = -EAGAIN;
1677 xt_compat_flush_offsets(NFPROTO_ARP);
1678 module_put(t->me);
1679 xt_table_unlock(t);
1680 } else
1681 ret = t ? PTR_ERR(t) : -ENOENT;
1683 xt_compat_unlock(NFPROTO_ARP);
1684 return ret;
1687 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1689 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1690 int *len)
1692 int ret;
1694 if (!capable(CAP_NET_ADMIN))
1695 return -EPERM;
1697 switch (cmd) {
1698 case ARPT_SO_GET_INFO:
1699 ret = get_info(sock_net(sk), user, len, 1);
1700 break;
1701 case ARPT_SO_GET_ENTRIES:
1702 ret = compat_get_entries(sock_net(sk), user, len);
1703 break;
1704 default:
1705 ret = do_arpt_get_ctl(sk, cmd, user, len);
1707 return ret;
1709 #endif
1711 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1713 int ret;
1715 if (!capable(CAP_NET_ADMIN))
1716 return -EPERM;
1718 switch (cmd) {
1719 case ARPT_SO_SET_REPLACE:
1720 ret = do_replace(sock_net(sk), user, len);
1721 break;
1723 case ARPT_SO_SET_ADD_COUNTERS:
1724 ret = do_add_counters(sock_net(sk), user, len, 0);
1725 break;
1727 default:
1728 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1729 ret = -EINVAL;
1732 return ret;
1735 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1737 int ret;
1739 if (!capable(CAP_NET_ADMIN))
1740 return -EPERM;
1742 switch (cmd) {
1743 case ARPT_SO_GET_INFO:
1744 ret = get_info(sock_net(sk), user, len, 0);
1745 break;
1747 case ARPT_SO_GET_ENTRIES:
1748 ret = get_entries(sock_net(sk), user, len);
1749 break;
1751 case ARPT_SO_GET_REVISION_TARGET: {
1752 struct xt_get_revision rev;
1754 if (*len != sizeof(rev)) {
1755 ret = -EINVAL;
1756 break;
1758 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1759 ret = -EFAULT;
1760 break;
1763 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1764 rev.revision, 1, &ret),
1765 "arpt_%s", rev.name);
1766 break;
1769 default:
1770 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1771 ret = -EINVAL;
1774 return ret;
1777 struct xt_table *arpt_register_table(struct net *net,
1778 const struct xt_table *table,
1779 const struct arpt_replace *repl)
1781 int ret;
1782 struct xt_table_info *newinfo;
1783 struct xt_table_info bootstrap
1784 = { 0, 0, 0, { 0 }, { 0 }, { } };
1785 void *loc_cpu_entry;
1786 struct xt_table *new_table;
1788 newinfo = xt_alloc_table_info(repl->size);
1789 if (!newinfo) {
1790 ret = -ENOMEM;
1791 goto out;
1794 /* choose the copy on our node/cpu */
1795 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1796 memcpy(loc_cpu_entry, repl->entries, repl->size);
1798 ret = translate_table(table->name, table->valid_hooks,
1799 newinfo, loc_cpu_entry, repl->size,
1800 repl->num_entries,
1801 repl->hook_entry,
1802 repl->underflow);
1804 duprintf("arpt_register_table: translate table gives %d\n", ret);
1805 if (ret != 0)
1806 goto out_free;
1808 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1809 if (IS_ERR(new_table)) {
1810 ret = PTR_ERR(new_table);
1811 goto out_free;
1813 return new_table;
1815 out_free:
1816 xt_free_table_info(newinfo);
1817 out:
1818 return ERR_PTR(ret);
1821 void arpt_unregister_table(struct xt_table *table)
1823 struct xt_table_info *private;
1824 void *loc_cpu_entry;
1825 struct module *table_owner = table->me;
1826 struct arpt_entry *iter;
1828 private = xt_unregister_table(table);
1830 /* Decrease module usage counts and free resources */
1831 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1832 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1833 cleanup_entry(iter);
1834 if (private->number > private->initial_entries)
1835 module_put(table_owner);
1836 xt_free_table_info(private);
1839 /* The built-in targets: standard (NULL) and error. */
1840 static struct xt_target arpt_standard_target __read_mostly = {
1841 .name = ARPT_STANDARD_TARGET,
1842 .targetsize = sizeof(int),
1843 .family = NFPROTO_ARP,
1844 #ifdef CONFIG_COMPAT
1845 .compatsize = sizeof(compat_int_t),
1846 .compat_from_user = compat_standard_from_user,
1847 .compat_to_user = compat_standard_to_user,
1848 #endif
1851 static struct xt_target arpt_error_target __read_mostly = {
1852 .name = ARPT_ERROR_TARGET,
1853 .target = arpt_error,
1854 .targetsize = ARPT_FUNCTION_MAXNAMELEN,
1855 .family = NFPROTO_ARP,
1858 static struct nf_sockopt_ops arpt_sockopts = {
1859 .pf = PF_INET,
1860 .set_optmin = ARPT_BASE_CTL,
1861 .set_optmax = ARPT_SO_SET_MAX+1,
1862 .set = do_arpt_set_ctl,
1863 #ifdef CONFIG_COMPAT
1864 .compat_set = compat_do_arpt_set_ctl,
1865 #endif
1866 .get_optmin = ARPT_BASE_CTL,
1867 .get_optmax = ARPT_SO_GET_MAX+1,
1868 .get = do_arpt_get_ctl,
1869 #ifdef CONFIG_COMPAT
1870 .compat_get = compat_do_arpt_get_ctl,
1871 #endif
1872 .owner = THIS_MODULE,
1875 static int __net_init arp_tables_net_init(struct net *net)
1877 return xt_proto_init(net, NFPROTO_ARP);
1880 static void __net_exit arp_tables_net_exit(struct net *net)
1882 xt_proto_fini(net, NFPROTO_ARP);
1885 static struct pernet_operations arp_tables_net_ops = {
1886 .init = arp_tables_net_init,
1887 .exit = arp_tables_net_exit,
1890 static int __init arp_tables_init(void)
1892 int ret;
1894 ret = register_pernet_subsys(&arp_tables_net_ops);
1895 if (ret < 0)
1896 goto err1;
1898 /* Noone else will be downing sem now, so we won't sleep */
1899 ret = xt_register_target(&arpt_standard_target);
1900 if (ret < 0)
1901 goto err2;
1902 ret = xt_register_target(&arpt_error_target);
1903 if (ret < 0)
1904 goto err3;
1906 /* Register setsockopt */
1907 ret = nf_register_sockopt(&arpt_sockopts);
1908 if (ret < 0)
1909 goto err4;
1911 printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1912 return 0;
1914 err4:
1915 xt_unregister_target(&arpt_error_target);
1916 err3:
1917 xt_unregister_target(&arpt_standard_target);
1918 err2:
1919 unregister_pernet_subsys(&arp_tables_net_ops);
1920 err1:
1921 return ret;
1924 static void __exit arp_tables_fini(void)
1926 nf_unregister_sockopt(&arpt_sockopts);
1927 xt_unregister_target(&arpt_error_target);
1928 xt_unregister_target(&arpt_standard_target);
1929 unregister_pernet_subsys(&arp_tables_net_ops);
1932 EXPORT_SYMBOL(arpt_register_table);
1933 EXPORT_SYMBOL(arpt_unregister_table);
1934 EXPORT_SYMBOL(arpt_do_table);
1936 module_init(arp_tables_init);
1937 module_exit(arp_tables_fini);