initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / net / ipv4 / netfilter / arp_tables.c
blobc8b0cc35d14839fba9775394f64716a79fb5e626
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>
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
33 MODULE_DESCRIPTION("arptables core");
35 /*#define DEBUG_ARP_TABLES*/
36 /*#define DEBUG_ARP_TABLES_USER*/
38 #ifdef DEBUG_ARP_TABLES
39 #define dprintf(format, args...) printk(format , ## args)
40 #else
41 #define dprintf(format, args...)
42 #endif
44 #ifdef DEBUG_ARP_TABLES_USER
45 #define duprintf(format, args...) printk(format , ## args)
46 #else
47 #define duprintf(format, args...)
48 #endif
50 #ifdef CONFIG_NETFILTER_DEBUG
51 #define ARP_NF_ASSERT(x) \
52 do { \
53 if (!(x)) \
54 printk("ARP_NF_ASSERT: %s:%s:%u\n", \
55 __func__, __FILE__, __LINE__); \
56 } while(0)
57 #else
58 #define ARP_NF_ASSERT(x)
59 #endif
61 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
62 const char *hdr_addr, int len)
64 int i, ret;
66 if (len > ARPT_DEV_ADDR_LEN_MAX)
67 len = ARPT_DEV_ADDR_LEN_MAX;
69 ret = 0;
70 for (i = 0; i < len; i++)
71 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
73 return (ret != 0);
77 * Unfortunatly, _b and _mask are not aligned to an int (or long int)
78 * Some arches dont care, unrolling the loop is a win on them.
79 * For other arches, we only have a 16bit alignement.
81 static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
83 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
84 unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
85 #else
86 unsigned long ret = 0;
87 const u16 *a = (const u16 *)_a;
88 const u16 *b = (const u16 *)_b;
89 const u16 *mask = (const u16 *)_mask;
90 int i;
92 for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
93 ret |= (a[i] ^ b[i]) & mask[i];
94 #endif
95 return ret;
98 /* Returns whether packet matches rule or not. */
99 static inline int arp_packet_match(const struct arphdr *arphdr,
100 struct net_device *dev,
101 const char *indev,
102 const char *outdev,
103 const struct arpt_arp *arpinfo)
105 const char *arpptr = (char *)(arphdr + 1);
106 const char *src_devaddr, *tgt_devaddr;
107 __be32 src_ipaddr, tgt_ipaddr;
108 long ret;
110 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
112 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
113 ARPT_INV_ARPOP)) {
114 dprintf("ARP operation field mismatch.\n");
115 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
116 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
117 return 0;
120 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
121 ARPT_INV_ARPHRD)) {
122 dprintf("ARP hardware address format mismatch.\n");
123 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
124 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
125 return 0;
128 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
129 ARPT_INV_ARPPRO)) {
130 dprintf("ARP protocol address format mismatch.\n");
131 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
132 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
133 return 0;
136 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
137 ARPT_INV_ARPHLN)) {
138 dprintf("ARP hardware address length mismatch.\n");
139 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
140 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
141 return 0;
144 src_devaddr = arpptr;
145 arpptr += dev->addr_len;
146 memcpy(&src_ipaddr, arpptr, sizeof(u32));
147 arpptr += sizeof(u32);
148 tgt_devaddr = arpptr;
149 arpptr += dev->addr_len;
150 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
152 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
153 ARPT_INV_SRCDEVADDR) ||
154 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
155 ARPT_INV_TGTDEVADDR)) {
156 dprintf("Source or target device address mismatch.\n");
158 return 0;
161 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
162 ARPT_INV_SRCIP) ||
163 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
164 ARPT_INV_TGTIP)) {
165 dprintf("Source or target IP address mismatch.\n");
167 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
168 &src_ipaddr,
169 &arpinfo->smsk.s_addr,
170 &arpinfo->src.s_addr,
171 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
172 dprintf("TGT: %pI4 Mask: %pI4 Target: %pI4.%s\n",
173 &tgt_ipaddr,
174 &arpinfo->tmsk.s_addr,
175 &arpinfo->tgt.s_addr,
176 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
177 return 0;
180 /* Look for ifname matches. */
181 ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
183 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
184 dprintf("VIA in mismatch (%s vs %s).%s\n",
185 indev, arpinfo->iniface,
186 arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
187 return 0;
190 ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
192 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
193 dprintf("VIA out mismatch (%s vs %s).%s\n",
194 outdev, arpinfo->outiface,
195 arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
196 return 0;
199 return 1;
200 #undef FWINV
203 static inline int arp_checkentry(const struct arpt_arp *arp)
205 if (arp->flags & ~ARPT_F_MASK) {
206 duprintf("Unknown flag bits set: %08X\n",
207 arp->flags & ~ARPT_F_MASK);
208 return 0;
210 if (arp->invflags & ~ARPT_INV_MASK) {
211 duprintf("Unknown invflag bits set: %08X\n",
212 arp->invflags & ~ARPT_INV_MASK);
213 return 0;
216 return 1;
219 static unsigned int
220 arpt_error(struct sk_buff *skb, const struct xt_target_param *par)
222 if (net_ratelimit())
223 printk("arp_tables: error: '%s'\n",
224 (const char *)par->targinfo);
226 return NF_DROP;
229 static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
231 return (struct arpt_entry *)(base + offset);
234 static inline __pure
235 struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
237 return (void *)entry + entry->next_offset;
240 unsigned int arpt_do_table(struct sk_buff *skb,
241 unsigned int hook,
242 const struct net_device *in,
243 const struct net_device *out,
244 struct xt_table *table)
246 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
247 unsigned int verdict = NF_DROP;
248 const struct arphdr *arp;
249 bool hotdrop = false;
250 struct arpt_entry *e, *back;
251 const char *indev, *outdev;
252 void *table_base;
253 const struct xt_table_info *private;
254 struct xt_target_param tgpar;
256 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
257 return NF_DROP;
259 indev = in ? in->name : nulldevname;
260 outdev = out ? out->name : nulldevname;
262 xt_info_rdlock_bh();
263 private = table->private;
264 table_base = private->entries[smp_processor_id()];
266 e = get_entry(table_base, private->hook_entry[hook]);
267 back = get_entry(table_base, private->underflow[hook]);
269 tgpar.in = in;
270 tgpar.out = out;
271 tgpar.hooknum = hook;
272 tgpar.family = NFPROTO_ARP;
274 arp = arp_hdr(skb);
275 do {
276 struct arpt_entry_target *t;
277 int hdr_len;
279 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
280 e = arpt_next_entry(e);
281 continue;
284 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
285 (2 * skb->dev->addr_len);
286 ADD_COUNTER(e->counters, hdr_len, 1);
288 t = arpt_get_target(e);
290 /* Standard target? */
291 if (!t->u.kernel.target->target) {
292 int v;
294 v = ((struct arpt_standard_target *)t)->verdict;
295 if (v < 0) {
296 /* Pop from stack? */
297 if (v != ARPT_RETURN) {
298 verdict = (unsigned)(-v) - 1;
299 break;
301 e = back;
302 back = get_entry(table_base, back->comefrom);
303 continue;
305 if (table_base + v
306 != arpt_next_entry(e)) {
307 /* Save old back ptr in next entry */
308 struct arpt_entry *next = arpt_next_entry(e);
309 next->comefrom = (void *)back - table_base;
311 /* set back pointer to next entry */
312 back = next;
315 e = get_entry(table_base, v);
316 continue;
319 /* Targets which reenter must return
320 * abs. verdicts
322 tgpar.target = t->u.kernel.target;
323 tgpar.targinfo = t->data;
324 verdict = t->u.kernel.target->target(skb, &tgpar);
326 /* Target might have changed stuff. */
327 arp = arp_hdr(skb);
329 if (verdict == ARPT_CONTINUE)
330 e = arpt_next_entry(e);
331 else
332 /* Verdict */
333 break;
334 } while (!hotdrop);
335 xt_info_rdunlock_bh();
337 if (hotdrop)
338 return NF_DROP;
339 else
340 return verdict;
343 /* All zeroes == unconditional rule. */
344 static inline bool unconditional(const struct arpt_arp *arp)
346 static const struct arpt_arp uncond;
348 return memcmp(arp, &uncond, sizeof(uncond)) == 0;
351 /* Figures out from what hook each rule can be called: returns 0 if
352 * there are loops. Puts hook bitmask in comefrom.
354 static int mark_source_chains(struct xt_table_info *newinfo,
355 unsigned int valid_hooks, void *entry0)
357 unsigned int hook;
359 /* No recursion; use packet counter to save back ptrs (reset
360 * to 0 as we leave), and comefrom to save source hook bitmask.
362 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
363 unsigned int pos = newinfo->hook_entry[hook];
364 struct arpt_entry *e
365 = (struct arpt_entry *)(entry0 + pos);
367 if (!(valid_hooks & (1 << hook)))
368 continue;
370 /* Set initial back pointer. */
371 e->counters.pcnt = pos;
373 for (;;) {
374 const struct arpt_standard_target *t
375 = (void *)arpt_get_target(e);
376 int visited = e->comefrom & (1 << hook);
378 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
379 printk("arptables: loop hook %u pos %u %08X.\n",
380 hook, pos, e->comefrom);
381 return 0;
383 e->comefrom
384 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
386 /* Unconditional return/END. */
387 if ((e->target_offset == sizeof(struct arpt_entry)
388 && (strcmp(t->target.u.user.name,
389 ARPT_STANDARD_TARGET) == 0)
390 && t->verdict < 0
391 && unconditional(&e->arp)) || visited) {
392 unsigned int oldpos, size;
394 if ((strcmp(t->target.u.user.name,
395 ARPT_STANDARD_TARGET) == 0) &&
396 t->verdict < -NF_MAX_VERDICT - 1) {
397 duprintf("mark_source_chains: bad "
398 "negative verdict (%i)\n",
399 t->verdict);
400 return 0;
403 /* Return: backtrack through the last
404 * big jump.
406 do {
407 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
408 oldpos = pos;
409 pos = e->counters.pcnt;
410 e->counters.pcnt = 0;
412 /* We're at the start. */
413 if (pos == oldpos)
414 goto next;
416 e = (struct arpt_entry *)
417 (entry0 + pos);
418 } while (oldpos == pos + e->next_offset);
420 /* Move along one */
421 size = e->next_offset;
422 e = (struct arpt_entry *)
423 (entry0 + pos + size);
424 e->counters.pcnt = pos;
425 pos += size;
426 } else {
427 int newpos = t->verdict;
429 if (strcmp(t->target.u.user.name,
430 ARPT_STANDARD_TARGET) == 0
431 && newpos >= 0) {
432 if (newpos > newinfo->size -
433 sizeof(struct arpt_entry)) {
434 duprintf("mark_source_chains: "
435 "bad verdict (%i)\n",
436 newpos);
437 return 0;
440 /* This a jump; chase it. */
441 duprintf("Jump rule %u -> %u\n",
442 pos, newpos);
443 } else {
444 /* ... this is a fallthru */
445 newpos = pos + e->next_offset;
447 e = (struct arpt_entry *)
448 (entry0 + newpos);
449 e->counters.pcnt = pos;
450 pos = newpos;
453 next:
454 duprintf("Finished chain %u\n", hook);
456 return 1;
459 static inline int check_entry(struct arpt_entry *e, const char *name)
461 const struct arpt_entry_target *t;
463 if (!arp_checkentry(&e->arp)) {
464 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
465 return -EINVAL;
468 if (e->target_offset + sizeof(struct arpt_entry_target) > e->next_offset)
469 return -EINVAL;
471 t = arpt_get_target(e);
472 if (e->target_offset + t->u.target_size > e->next_offset)
473 return -EINVAL;
475 return 0;
478 static inline int check_target(struct arpt_entry *e, const char *name)
480 struct arpt_entry_target *t = arpt_get_target(e);
481 int ret;
482 struct xt_tgchk_param par = {
483 .table = name,
484 .entryinfo = e,
485 .target = t->u.kernel.target,
486 .targinfo = t->data,
487 .hook_mask = e->comefrom,
488 .family = NFPROTO_ARP,
491 ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
492 if (ret < 0) {
493 duprintf("arp_tables: check failed for `%s'.\n",
494 t->u.kernel.target->name);
495 return ret;
497 return 0;
500 static inline int
501 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
502 unsigned int *i)
504 struct arpt_entry_target *t;
505 struct xt_target *target;
506 int ret;
508 ret = check_entry(e, name);
509 if (ret)
510 return ret;
512 t = arpt_get_target(e);
513 target = try_then_request_module(xt_find_target(NFPROTO_ARP,
514 t->u.user.name,
515 t->u.user.revision),
516 "arpt_%s", t->u.user.name);
517 if (IS_ERR(target) || !target) {
518 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
519 ret = target ? PTR_ERR(target) : -ENOENT;
520 goto out;
522 t->u.kernel.target = target;
524 ret = check_target(e, name);
525 if (ret)
526 goto err;
528 (*i)++;
529 return 0;
530 err:
531 module_put(t->u.kernel.target->me);
532 out:
533 return ret;
536 static bool check_underflow(struct arpt_entry *e)
538 const struct arpt_entry_target *t;
539 unsigned int verdict;
541 if (!unconditional(&e->arp))
542 return false;
543 t = arpt_get_target(e);
544 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
545 return false;
546 verdict = ((struct arpt_standard_target *)t)->verdict;
547 verdict = -verdict - 1;
548 return verdict == NF_DROP || verdict == NF_ACCEPT;
551 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
552 struct xt_table_info *newinfo,
553 unsigned char *base,
554 unsigned char *limit,
555 const unsigned int *hook_entries,
556 const unsigned int *underflows,
557 unsigned int valid_hooks,
558 unsigned int *i)
560 unsigned int h;
562 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
563 || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
564 duprintf("Bad offset %p\n", e);
565 return -EINVAL;
568 if (e->next_offset
569 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
570 duprintf("checking: element %p size %u\n",
571 e, e->next_offset);
572 return -EINVAL;
575 /* Check hooks & underflows */
576 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
577 if (!(valid_hooks & (1 << h)))
578 continue;
579 if ((unsigned char *)e - base == hook_entries[h])
580 newinfo->hook_entry[h] = hook_entries[h];
581 if ((unsigned char *)e - base == underflows[h]) {
582 if (!check_underflow(e)) {
583 pr_err("Underflows must be unconditional and "
584 "use the STANDARD target with "
585 "ACCEPT/DROP\n");
586 return -EINVAL;
588 newinfo->underflow[h] = underflows[h];
592 /* Clear counters and comefrom */
593 e->counters = ((struct xt_counters) { 0, 0 });
594 e->comefrom = 0;
596 (*i)++;
597 return 0;
600 static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
602 struct xt_tgdtor_param par;
603 struct arpt_entry_target *t;
605 if (i && (*i)-- == 0)
606 return 1;
608 t = arpt_get_target(e);
609 par.target = t->u.kernel.target;
610 par.targinfo = t->data;
611 par.family = NFPROTO_ARP;
612 if (par.target->destroy != NULL)
613 par.target->destroy(&par);
614 module_put(par.target->me);
615 return 0;
618 /* Checks and translates the user-supplied table segment (held in
619 * newinfo).
621 static int translate_table(const char *name,
622 unsigned int valid_hooks,
623 struct xt_table_info *newinfo,
624 void *entry0,
625 unsigned int size,
626 unsigned int number,
627 const unsigned int *hook_entries,
628 const unsigned int *underflows)
630 unsigned int i;
631 int ret;
633 newinfo->size = size;
634 newinfo->number = number;
636 /* Init all hooks to impossible value. */
637 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
638 newinfo->hook_entry[i] = 0xFFFFFFFF;
639 newinfo->underflow[i] = 0xFFFFFFFF;
642 duprintf("translate_table: size %u\n", newinfo->size);
643 i = 0;
645 /* Walk through entries, checking offsets. */
646 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
647 check_entry_size_and_hooks,
648 newinfo,
649 entry0,
650 entry0 + size,
651 hook_entries, underflows, valid_hooks, &i);
652 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
653 if (ret != 0)
654 return ret;
656 if (i != number) {
657 duprintf("translate_table: %u not %u entries\n",
658 i, number);
659 return -EINVAL;
662 /* Check hooks all assigned */
663 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
664 /* Only hooks which are valid */
665 if (!(valid_hooks & (1 << i)))
666 continue;
667 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
668 duprintf("Invalid hook entry %u %u\n",
669 i, hook_entries[i]);
670 return -EINVAL;
672 if (newinfo->underflow[i] == 0xFFFFFFFF) {
673 duprintf("Invalid underflow %u %u\n",
674 i, underflows[i]);
675 return -EINVAL;
679 if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
680 duprintf("Looping hook\n");
681 return -ELOOP;
684 /* Finally, each sanity check must pass */
685 i = 0;
686 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
687 find_check_entry, name, size, &i);
689 if (ret != 0) {
690 ARPT_ENTRY_ITERATE(entry0, newinfo->size,
691 cleanup_entry, &i);
692 return ret;
695 /* And one copy for every other CPU */
696 for_each_possible_cpu(i) {
697 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
698 memcpy(newinfo->entries[i], entry0, newinfo->size);
701 return ret;
704 /* Gets counters. */
705 static inline int add_entry_to_counter(const struct arpt_entry *e,
706 struct xt_counters total[],
707 unsigned int *i)
709 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
711 (*i)++;
712 return 0;
715 static inline int set_entry_to_counter(const struct arpt_entry *e,
716 struct xt_counters total[],
717 unsigned int *i)
719 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
721 (*i)++;
722 return 0;
725 static void get_counters(const struct xt_table_info *t,
726 struct xt_counters counters[])
728 unsigned int cpu;
729 unsigned int i;
730 unsigned int curcpu;
732 /* Instead of clearing (by a previous call to memset())
733 * the counters and using adds, we set the counters
734 * with data used by 'current' CPU
736 * Bottom half has to be disabled to prevent deadlock
737 * if new softirq were to run and call ipt_do_table
739 local_bh_disable();
740 curcpu = smp_processor_id();
742 i = 0;
743 ARPT_ENTRY_ITERATE(t->entries[curcpu],
744 t->size,
745 set_entry_to_counter,
746 counters,
747 &i);
749 for_each_possible_cpu(cpu) {
750 if (cpu == curcpu)
751 continue;
752 i = 0;
753 xt_info_wrlock(cpu);
754 ARPT_ENTRY_ITERATE(t->entries[cpu],
755 t->size,
756 add_entry_to_counter,
757 counters,
758 &i);
759 xt_info_wrunlock(cpu);
761 local_bh_enable();
764 static struct xt_counters *alloc_counters(struct xt_table *table)
766 unsigned int countersize;
767 struct xt_counters *counters;
768 struct xt_table_info *private = table->private;
770 /* We need atomic snapshot of counters: rest doesn't change
771 * (other than comefrom, which userspace doesn't care
772 * about).
774 countersize = sizeof(struct xt_counters) * private->number;
775 counters = vmalloc_node(countersize, numa_node_id());
777 if (counters == NULL)
778 return ERR_PTR(-ENOMEM);
780 get_counters(private, counters);
782 return counters;
785 static int copy_entries_to_user(unsigned int total_size,
786 struct xt_table *table,
787 void __user *userptr)
789 unsigned int off, num;
790 struct arpt_entry *e;
791 struct xt_counters *counters;
792 struct xt_table_info *private = table->private;
793 int ret = 0;
794 void *loc_cpu_entry;
796 counters = alloc_counters(table);
797 if (IS_ERR(counters))
798 return PTR_ERR(counters);
800 loc_cpu_entry = private->entries[raw_smp_processor_id()];
801 /* ... then copy entire thing ... */
802 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
803 ret = -EFAULT;
804 goto free_counters;
807 /* FIXME: use iterator macros --RR */
808 /* ... then go back and fix counters and names */
809 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
810 struct arpt_entry_target *t;
812 e = (struct arpt_entry *)(loc_cpu_entry + off);
813 if (copy_to_user(userptr + off
814 + offsetof(struct arpt_entry, counters),
815 &counters[num],
816 sizeof(counters[num])) != 0) {
817 ret = -EFAULT;
818 goto free_counters;
821 t = arpt_get_target(e);
822 if (copy_to_user(userptr + off + e->target_offset
823 + offsetof(struct arpt_entry_target,
824 u.user.name),
825 t->u.kernel.target->name,
826 strlen(t->u.kernel.target->name)+1) != 0) {
827 ret = -EFAULT;
828 goto free_counters;
832 free_counters:
833 vfree(counters);
834 return ret;
837 #ifdef CONFIG_COMPAT
838 static void compat_standard_from_user(void *dst, void *src)
840 int v = *(compat_int_t *)src;
842 if (v > 0)
843 v += xt_compat_calc_jump(NFPROTO_ARP, v);
844 memcpy(dst, &v, sizeof(v));
847 static int compat_standard_to_user(void __user *dst, void *src)
849 compat_int_t cv = *(int *)src;
851 if (cv > 0)
852 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
853 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
856 static int compat_calc_entry(struct arpt_entry *e,
857 const struct xt_table_info *info,
858 void *base, struct xt_table_info *newinfo)
860 struct arpt_entry_target *t;
861 unsigned int entry_offset;
862 int off, i, ret;
864 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
865 entry_offset = (void *)e - base;
867 t = arpt_get_target(e);
868 off += xt_compat_target_offset(t->u.kernel.target);
869 newinfo->size -= off;
870 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
871 if (ret)
872 return ret;
874 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
875 if (info->hook_entry[i] &&
876 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
877 newinfo->hook_entry[i] -= off;
878 if (info->underflow[i] &&
879 (e < (struct arpt_entry *)(base + info->underflow[i])))
880 newinfo->underflow[i] -= off;
882 return 0;
885 static int compat_table_info(const struct xt_table_info *info,
886 struct xt_table_info *newinfo)
888 void *loc_cpu_entry;
890 if (!newinfo || !info)
891 return -EINVAL;
893 /* we dont care about newinfo->entries[] */
894 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
895 newinfo->initial_entries = 0;
896 loc_cpu_entry = info->entries[raw_smp_processor_id()];
897 return ARPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
898 compat_calc_entry, info, loc_cpu_entry,
899 newinfo);
901 #endif
903 static int get_info(struct net *net, void __user *user, int *len, int compat)
905 char name[ARPT_TABLE_MAXNAMELEN];
906 struct xt_table *t;
907 int ret;
909 if (*len != sizeof(struct arpt_getinfo)) {
910 duprintf("length %u != %Zu\n", *len,
911 sizeof(struct arpt_getinfo));
912 return -EINVAL;
915 if (copy_from_user(name, user, sizeof(name)) != 0)
916 return -EFAULT;
918 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
919 #ifdef CONFIG_COMPAT
920 if (compat)
921 xt_compat_lock(NFPROTO_ARP);
922 #endif
923 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
924 "arptable_%s", name);
925 if (t && !IS_ERR(t)) {
926 struct arpt_getinfo info;
927 const struct xt_table_info *private = t->private;
928 #ifdef CONFIG_COMPAT
929 struct xt_table_info tmp;
931 if (compat) {
932 ret = compat_table_info(private, &tmp);
933 xt_compat_flush_offsets(NFPROTO_ARP);
934 private = &tmp;
936 #endif
937 info.valid_hooks = t->valid_hooks;
938 memcpy(info.hook_entry, private->hook_entry,
939 sizeof(info.hook_entry));
940 memcpy(info.underflow, private->underflow,
941 sizeof(info.underflow));
942 info.num_entries = private->number;
943 info.size = private->size;
944 strcpy(info.name, name);
946 if (copy_to_user(user, &info, *len) != 0)
947 ret = -EFAULT;
948 else
949 ret = 0;
950 xt_table_unlock(t);
951 module_put(t->me);
952 } else
953 ret = t ? PTR_ERR(t) : -ENOENT;
954 #ifdef CONFIG_COMPAT
955 if (compat)
956 xt_compat_unlock(NFPROTO_ARP);
957 #endif
958 return ret;
961 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
962 int *len)
964 int ret;
965 struct arpt_get_entries get;
966 struct xt_table *t;
968 if (*len < sizeof(get)) {
969 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
970 return -EINVAL;
972 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
973 return -EFAULT;
974 if (*len != sizeof(struct arpt_get_entries) + get.size) {
975 duprintf("get_entries: %u != %Zu\n", *len,
976 sizeof(struct arpt_get_entries) + get.size);
977 return -EINVAL;
980 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
981 if (t && !IS_ERR(t)) {
982 const struct xt_table_info *private = t->private;
984 duprintf("t->private->number = %u\n",
985 private->number);
986 if (get.size == private->size)
987 ret = copy_entries_to_user(private->size,
988 t, uptr->entrytable);
989 else {
990 duprintf("get_entries: I've got %u not %u!\n",
991 private->size, get.size);
992 ret = -EAGAIN;
994 module_put(t->me);
995 xt_table_unlock(t);
996 } else
997 ret = t ? PTR_ERR(t) : -ENOENT;
999 return ret;
1002 static int __do_replace(struct net *net, const char *name,
1003 unsigned int valid_hooks,
1004 struct xt_table_info *newinfo,
1005 unsigned int num_counters,
1006 void __user *counters_ptr)
1008 int ret;
1009 struct xt_table *t;
1010 struct xt_table_info *oldinfo;
1011 struct xt_counters *counters;
1012 void *loc_cpu_old_entry;
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 ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1057 NULL);
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, void __user *user, unsigned int len)
1078 int ret;
1079 struct arpt_replace tmp;
1080 struct xt_table_info *newinfo;
1081 void *loc_cpu_entry;
1083 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1084 return -EFAULT;
1086 /* overflow check */
1087 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1088 return -ENOMEM;
1089 tmp.name[sizeof(tmp.name)-1] = 0;
1091 newinfo = xt_alloc_table_info(tmp.size);
1092 if (!newinfo)
1093 return -ENOMEM;
1095 /* choose the copy that is on our node/cpu */
1096 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1097 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1098 tmp.size) != 0) {
1099 ret = -EFAULT;
1100 goto free_newinfo;
1103 ret = translate_table(tmp.name, tmp.valid_hooks,
1104 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
1105 tmp.hook_entry, tmp.underflow);
1106 if (ret != 0)
1107 goto free_newinfo;
1109 duprintf("arp_tables: Translated table\n");
1111 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1112 tmp.num_counters, tmp.counters);
1113 if (ret)
1114 goto free_newinfo_untrans;
1115 return 0;
1117 free_newinfo_untrans:
1118 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1119 free_newinfo:
1120 xt_free_table_info(newinfo);
1121 return ret;
1124 /* We're lazy, and add to the first CPU; overflow works its fey magic
1125 * and everything is OK. */
1126 static int
1127 add_counter_to_entry(struct arpt_entry *e,
1128 const struct xt_counters addme[],
1129 unsigned int *i)
1131 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1133 (*i)++;
1134 return 0;
1137 static int do_add_counters(struct net *net, void __user *user, unsigned int len,
1138 int compat)
1140 unsigned int i, curcpu;
1141 struct xt_counters_info tmp;
1142 struct xt_counters *paddc;
1143 unsigned int num_counters;
1144 const char *name;
1145 int size;
1146 void *ptmp;
1147 struct xt_table *t;
1148 const struct xt_table_info *private;
1149 int ret = 0;
1150 void *loc_cpu_entry;
1151 #ifdef CONFIG_COMPAT
1152 struct compat_xt_counters_info compat_tmp;
1154 if (compat) {
1155 ptmp = &compat_tmp;
1156 size = sizeof(struct compat_xt_counters_info);
1157 } else
1158 #endif
1160 ptmp = &tmp;
1161 size = sizeof(struct xt_counters_info);
1164 if (copy_from_user(ptmp, user, size) != 0)
1165 return -EFAULT;
1167 #ifdef CONFIG_COMPAT
1168 if (compat) {
1169 num_counters = compat_tmp.num_counters;
1170 name = compat_tmp.name;
1171 } else
1172 #endif
1174 num_counters = tmp.num_counters;
1175 name = tmp.name;
1178 if (len != size + num_counters * sizeof(struct xt_counters))
1179 return -EINVAL;
1181 paddc = vmalloc_node(len - size, numa_node_id());
1182 if (!paddc)
1183 return -ENOMEM;
1185 if (copy_from_user(paddc, user + size, len - size) != 0) {
1186 ret = -EFAULT;
1187 goto free;
1190 t = xt_find_table_lock(net, NFPROTO_ARP, name);
1191 if (!t || IS_ERR(t)) {
1192 ret = t ? PTR_ERR(t) : -ENOENT;
1193 goto free;
1196 local_bh_disable();
1197 private = t->private;
1198 if (private->number != num_counters) {
1199 ret = -EINVAL;
1200 goto unlock_up_free;
1203 i = 0;
1204 /* Choose the copy that is on our node */
1205 curcpu = smp_processor_id();
1206 loc_cpu_entry = private->entries[curcpu];
1207 xt_info_wrlock(curcpu);
1208 ARPT_ENTRY_ITERATE(loc_cpu_entry,
1209 private->size,
1210 add_counter_to_entry,
1211 paddc,
1212 &i);
1213 xt_info_wrunlock(curcpu);
1214 unlock_up_free:
1215 local_bh_enable();
1216 xt_table_unlock(t);
1217 module_put(t->me);
1218 free:
1219 vfree(paddc);
1221 return ret;
1224 #ifdef CONFIG_COMPAT
1225 static inline int
1226 compat_release_entry(struct compat_arpt_entry *e, unsigned int *i)
1228 struct arpt_entry_target *t;
1230 if (i && (*i)-- == 0)
1231 return 1;
1233 t = compat_arpt_get_target(e);
1234 module_put(t->u.kernel.target->me);
1235 return 0;
1238 static inline int
1239 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1240 struct xt_table_info *newinfo,
1241 unsigned int *size,
1242 unsigned char *base,
1243 unsigned char *limit,
1244 unsigned int *hook_entries,
1245 unsigned int *underflows,
1246 unsigned int *i,
1247 const char *name)
1249 struct arpt_entry_target *t;
1250 struct xt_target *target;
1251 unsigned int entry_offset;
1252 int ret, off, h;
1254 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1255 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0
1256 || (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
1257 duprintf("Bad offset %p, limit = %p\n", e, limit);
1258 return -EINVAL;
1261 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1262 sizeof(struct compat_xt_entry_target)) {
1263 duprintf("checking: element %p size %u\n",
1264 e, e->next_offset);
1265 return -EINVAL;
1268 /* For purposes of check_entry casting the compat entry is fine */
1269 ret = check_entry((struct arpt_entry *)e, name);
1270 if (ret)
1271 return ret;
1273 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1274 entry_offset = (void *)e - (void *)base;
1276 t = compat_arpt_get_target(e);
1277 target = try_then_request_module(xt_find_target(NFPROTO_ARP,
1278 t->u.user.name,
1279 t->u.user.revision),
1280 "arpt_%s", t->u.user.name);
1281 if (IS_ERR(target) || !target) {
1282 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1283 t->u.user.name);
1284 ret = target ? PTR_ERR(target) : -ENOENT;
1285 goto out;
1287 t->u.kernel.target = target;
1289 off += xt_compat_target_offset(target);
1290 *size += off;
1291 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1292 if (ret)
1293 goto release_target;
1295 /* Check hooks & underflows */
1296 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1297 if ((unsigned char *)e - base == hook_entries[h])
1298 newinfo->hook_entry[h] = hook_entries[h];
1299 if ((unsigned char *)e - base == underflows[h])
1300 newinfo->underflow[h] = underflows[h];
1303 /* Clear counters and comefrom */
1304 memset(&e->counters, 0, sizeof(e->counters));
1305 e->comefrom = 0;
1307 (*i)++;
1308 return 0;
1310 release_target:
1311 module_put(t->u.kernel.target->me);
1312 out:
1313 return ret;
1316 static int
1317 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1318 unsigned int *size, const char *name,
1319 struct xt_table_info *newinfo, unsigned char *base)
1321 struct arpt_entry_target *t;
1322 struct xt_target *target;
1323 struct arpt_entry *de;
1324 unsigned int origsize;
1325 int ret, h;
1327 ret = 0;
1328 origsize = *size;
1329 de = (struct arpt_entry *)*dstptr;
1330 memcpy(de, e, sizeof(struct arpt_entry));
1331 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1333 *dstptr += sizeof(struct arpt_entry);
1334 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1336 de->target_offset = e->target_offset - (origsize - *size);
1337 t = compat_arpt_get_target(e);
1338 target = t->u.kernel.target;
1339 xt_compat_target_from_user(t, dstptr, size);
1341 de->next_offset = e->next_offset - (origsize - *size);
1342 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1343 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1344 newinfo->hook_entry[h] -= origsize - *size;
1345 if ((unsigned char *)de - base < newinfo->underflow[h])
1346 newinfo->underflow[h] -= origsize - *size;
1348 return ret;
1351 static inline int compat_check_entry(struct arpt_entry *e, const char *name,
1352 unsigned int *i)
1354 int ret;
1356 ret = check_target(e, name);
1357 if (ret)
1358 return ret;
1360 (*i)++;
1361 return 0;
1364 static int translate_compat_table(const char *name,
1365 unsigned int valid_hooks,
1366 struct xt_table_info **pinfo,
1367 void **pentry0,
1368 unsigned int total_size,
1369 unsigned int number,
1370 unsigned int *hook_entries,
1371 unsigned int *underflows)
1373 unsigned int i, j;
1374 struct xt_table_info *newinfo, *info;
1375 void *pos, *entry0, *entry1;
1376 unsigned int size;
1377 int ret;
1379 info = *pinfo;
1380 entry0 = *pentry0;
1381 size = total_size;
1382 info->number = number;
1384 /* Init all hooks to impossible value. */
1385 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1386 info->hook_entry[i] = 0xFFFFFFFF;
1387 info->underflow[i] = 0xFFFFFFFF;
1390 duprintf("translate_compat_table: size %u\n", info->size);
1391 j = 0;
1392 xt_compat_lock(NFPROTO_ARP);
1393 /* Walk through entries, checking offsets. */
1394 ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size,
1395 check_compat_entry_size_and_hooks,
1396 info, &size, entry0,
1397 entry0 + total_size,
1398 hook_entries, underflows, &j, name);
1399 if (ret != 0)
1400 goto out_unlock;
1402 ret = -EINVAL;
1403 if (j != number) {
1404 duprintf("translate_compat_table: %u not %u entries\n",
1405 j, number);
1406 goto out_unlock;
1409 /* Check hooks all assigned */
1410 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1411 /* Only hooks which are valid */
1412 if (!(valid_hooks & (1 << i)))
1413 continue;
1414 if (info->hook_entry[i] == 0xFFFFFFFF) {
1415 duprintf("Invalid hook entry %u %u\n",
1416 i, hook_entries[i]);
1417 goto out_unlock;
1419 if (info->underflow[i] == 0xFFFFFFFF) {
1420 duprintf("Invalid underflow %u %u\n",
1421 i, underflows[i]);
1422 goto out_unlock;
1426 ret = -ENOMEM;
1427 newinfo = xt_alloc_table_info(size);
1428 if (!newinfo)
1429 goto out_unlock;
1431 newinfo->number = number;
1432 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1433 newinfo->hook_entry[i] = info->hook_entry[i];
1434 newinfo->underflow[i] = info->underflow[i];
1436 entry1 = newinfo->entries[raw_smp_processor_id()];
1437 pos = entry1;
1438 size = total_size;
1439 ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size,
1440 compat_copy_entry_from_user,
1441 &pos, &size, name, newinfo, entry1);
1442 xt_compat_flush_offsets(NFPROTO_ARP);
1443 xt_compat_unlock(NFPROTO_ARP);
1444 if (ret)
1445 goto free_newinfo;
1447 ret = -ELOOP;
1448 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1449 goto free_newinfo;
1451 i = 0;
1452 ret = ARPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
1453 name, &i);
1454 if (ret) {
1455 j -= i;
1456 COMPAT_ARPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1457 compat_release_entry, &j);
1458 ARPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1459 xt_free_table_info(newinfo);
1460 return ret;
1463 /* And one copy for every other CPU */
1464 for_each_possible_cpu(i)
1465 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1466 memcpy(newinfo->entries[i], entry1, newinfo->size);
1468 *pinfo = newinfo;
1469 *pentry0 = entry1;
1470 xt_free_table_info(info);
1471 return 0;
1473 free_newinfo:
1474 xt_free_table_info(newinfo);
1475 out:
1476 COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
1477 return ret;
1478 out_unlock:
1479 xt_compat_flush_offsets(NFPROTO_ARP);
1480 xt_compat_unlock(NFPROTO_ARP);
1481 goto out;
1484 struct compat_arpt_replace {
1485 char name[ARPT_TABLE_MAXNAMELEN];
1486 u32 valid_hooks;
1487 u32 num_entries;
1488 u32 size;
1489 u32 hook_entry[NF_ARP_NUMHOOKS];
1490 u32 underflow[NF_ARP_NUMHOOKS];
1491 u32 num_counters;
1492 compat_uptr_t counters;
1493 struct compat_arpt_entry entries[0];
1496 static int compat_do_replace(struct net *net, void __user *user,
1497 unsigned int len)
1499 int ret;
1500 struct compat_arpt_replace tmp;
1501 struct xt_table_info *newinfo;
1502 void *loc_cpu_entry;
1504 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1505 return -EFAULT;
1507 /* overflow check */
1508 if (tmp.size >= INT_MAX / num_possible_cpus())
1509 return -ENOMEM;
1510 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1511 return -ENOMEM;
1512 tmp.name[sizeof(tmp.name)-1] = 0;
1514 newinfo = xt_alloc_table_info(tmp.size);
1515 if (!newinfo)
1516 return -ENOMEM;
1518 /* choose the copy that is on our node/cpu */
1519 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1520 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1521 ret = -EFAULT;
1522 goto free_newinfo;
1525 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1526 &newinfo, &loc_cpu_entry, tmp.size,
1527 tmp.num_entries, tmp.hook_entry,
1528 tmp.underflow);
1529 if (ret != 0)
1530 goto free_newinfo;
1532 duprintf("compat_do_replace: Translated table\n");
1534 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1535 tmp.num_counters, compat_ptr(tmp.counters));
1536 if (ret)
1537 goto free_newinfo_untrans;
1538 return 0;
1540 free_newinfo_untrans:
1541 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1542 free_newinfo:
1543 xt_free_table_info(newinfo);
1544 return ret;
1547 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1548 unsigned int len)
1550 int ret;
1552 if (!capable(CAP_NET_ADMIN))
1553 return -EPERM;
1555 switch (cmd) {
1556 case ARPT_SO_SET_REPLACE:
1557 ret = compat_do_replace(sock_net(sk), user, len);
1558 break;
1560 case ARPT_SO_SET_ADD_COUNTERS:
1561 ret = do_add_counters(sock_net(sk), user, len, 1);
1562 break;
1564 default:
1565 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1566 ret = -EINVAL;
1569 return ret;
1572 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1573 compat_uint_t *size,
1574 struct xt_counters *counters,
1575 unsigned int *i)
1577 struct arpt_entry_target *t;
1578 struct compat_arpt_entry __user *ce;
1579 u_int16_t target_offset, next_offset;
1580 compat_uint_t origsize;
1581 int ret;
1583 ret = -EFAULT;
1584 origsize = *size;
1585 ce = (struct compat_arpt_entry __user *)*dstptr;
1586 if (copy_to_user(ce, e, sizeof(struct arpt_entry)))
1587 goto out;
1589 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1590 goto out;
1592 *dstptr += sizeof(struct compat_arpt_entry);
1593 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1595 target_offset = e->target_offset - (origsize - *size);
1597 t = arpt_get_target(e);
1598 ret = xt_compat_target_to_user(t, dstptr, size);
1599 if (ret)
1600 goto out;
1601 ret = -EFAULT;
1602 next_offset = e->next_offset - (origsize - *size);
1603 if (put_user(target_offset, &ce->target_offset))
1604 goto out;
1605 if (put_user(next_offset, &ce->next_offset))
1606 goto out;
1608 (*i)++;
1609 return 0;
1610 out:
1611 return ret;
1614 static int compat_copy_entries_to_user(unsigned int total_size,
1615 struct xt_table *table,
1616 void __user *userptr)
1618 struct xt_counters *counters;
1619 const struct xt_table_info *private = table->private;
1620 void __user *pos;
1621 unsigned int size;
1622 int ret = 0;
1623 void *loc_cpu_entry;
1624 unsigned int i = 0;
1626 counters = alloc_counters(table);
1627 if (IS_ERR(counters))
1628 return PTR_ERR(counters);
1630 /* choose the copy on our node/cpu */
1631 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1632 pos = userptr;
1633 size = total_size;
1634 ret = ARPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
1635 compat_copy_entry_to_user,
1636 &pos, &size, counters, &i);
1637 vfree(counters);
1638 return ret;
1641 struct compat_arpt_get_entries {
1642 char name[ARPT_TABLE_MAXNAMELEN];
1643 compat_uint_t size;
1644 struct compat_arpt_entry entrytable[0];
1647 static int compat_get_entries(struct net *net,
1648 struct compat_arpt_get_entries __user *uptr,
1649 int *len)
1651 int ret;
1652 struct compat_arpt_get_entries get;
1653 struct xt_table *t;
1655 if (*len < sizeof(get)) {
1656 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1657 return -EINVAL;
1659 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1660 return -EFAULT;
1661 if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1662 duprintf("compat_get_entries: %u != %zu\n",
1663 *len, sizeof(get) + get.size);
1664 return -EINVAL;
1667 xt_compat_lock(NFPROTO_ARP);
1668 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1669 if (t && !IS_ERR(t)) {
1670 const struct xt_table_info *private = t->private;
1671 struct xt_table_info info;
1673 duprintf("t->private->number = %u\n", private->number);
1674 ret = compat_table_info(private, &info);
1675 if (!ret && get.size == info.size) {
1676 ret = compat_copy_entries_to_user(private->size,
1677 t, uptr->entrytable);
1678 } else if (!ret) {
1679 duprintf("compat_get_entries: I've got %u not %u!\n",
1680 private->size, get.size);
1681 ret = -EAGAIN;
1683 xt_compat_flush_offsets(NFPROTO_ARP);
1684 module_put(t->me);
1685 xt_table_unlock(t);
1686 } else
1687 ret = t ? PTR_ERR(t) : -ENOENT;
1689 xt_compat_unlock(NFPROTO_ARP);
1690 return ret;
1693 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1695 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1696 int *len)
1698 int ret;
1700 if (!capable(CAP_NET_ADMIN))
1701 return -EPERM;
1703 switch (cmd) {
1704 case ARPT_SO_GET_INFO:
1705 ret = get_info(sock_net(sk), user, len, 1);
1706 break;
1707 case ARPT_SO_GET_ENTRIES:
1708 ret = compat_get_entries(sock_net(sk), user, len);
1709 break;
1710 default:
1711 ret = do_arpt_get_ctl(sk, cmd, user, len);
1713 return ret;
1715 #endif
1717 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1719 int ret;
1721 if (!capable(CAP_NET_ADMIN))
1722 return -EPERM;
1724 switch (cmd) {
1725 case ARPT_SO_SET_REPLACE:
1726 ret = do_replace(sock_net(sk), user, len);
1727 break;
1729 case ARPT_SO_SET_ADD_COUNTERS:
1730 ret = do_add_counters(sock_net(sk), user, len, 0);
1731 break;
1733 default:
1734 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1735 ret = -EINVAL;
1738 return ret;
1741 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1743 int ret;
1745 if (!capable(CAP_NET_ADMIN))
1746 return -EPERM;
1748 switch (cmd) {
1749 case ARPT_SO_GET_INFO:
1750 ret = get_info(sock_net(sk), user, len, 0);
1751 break;
1753 case ARPT_SO_GET_ENTRIES:
1754 ret = get_entries(sock_net(sk), user, len);
1755 break;
1757 case ARPT_SO_GET_REVISION_TARGET: {
1758 struct xt_get_revision rev;
1760 if (*len != sizeof(rev)) {
1761 ret = -EINVAL;
1762 break;
1764 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1765 ret = -EFAULT;
1766 break;
1768 rev.name[sizeof(rev.name)-1] = 0;
1770 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1771 rev.revision, 1, &ret),
1772 "arpt_%s", rev.name);
1773 break;
1776 default:
1777 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1778 ret = -EINVAL;
1781 return ret;
1784 struct xt_table *arpt_register_table(struct net *net,
1785 const struct xt_table *table,
1786 const struct arpt_replace *repl)
1788 int ret;
1789 struct xt_table_info *newinfo;
1790 struct xt_table_info bootstrap
1791 = { 0, 0, 0, { 0 }, { 0 }, { } };
1792 void *loc_cpu_entry;
1793 struct xt_table *new_table;
1795 newinfo = xt_alloc_table_info(repl->size);
1796 if (!newinfo) {
1797 ret = -ENOMEM;
1798 goto out;
1801 /* choose the copy on our node/cpu */
1802 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1803 memcpy(loc_cpu_entry, repl->entries, repl->size);
1805 ret = translate_table(table->name, table->valid_hooks,
1806 newinfo, loc_cpu_entry, repl->size,
1807 repl->num_entries,
1808 repl->hook_entry,
1809 repl->underflow);
1811 duprintf("arpt_register_table: translate table gives %d\n", ret);
1812 if (ret != 0)
1813 goto out_free;
1815 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1816 if (IS_ERR(new_table)) {
1817 ret = PTR_ERR(new_table);
1818 goto out_free;
1820 return new_table;
1822 out_free:
1823 xt_free_table_info(newinfo);
1824 out:
1825 return ERR_PTR(ret);
1828 void arpt_unregister_table(struct xt_table *table)
1830 struct xt_table_info *private;
1831 void *loc_cpu_entry;
1832 struct module *table_owner = table->me;
1834 private = xt_unregister_table(table);
1836 /* Decrease module usage counts and free resources */
1837 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1838 ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size,
1839 cleanup_entry, NULL);
1840 if (private->number > private->initial_entries)
1841 module_put(table_owner);
1842 xt_free_table_info(private);
1845 /* The built-in targets: standard (NULL) and error. */
1846 static struct xt_target arpt_standard_target __read_mostly = {
1847 .name = ARPT_STANDARD_TARGET,
1848 .targetsize = sizeof(int),
1849 .family = NFPROTO_ARP,
1850 #ifdef CONFIG_COMPAT
1851 .compatsize = sizeof(compat_int_t),
1852 .compat_from_user = compat_standard_from_user,
1853 .compat_to_user = compat_standard_to_user,
1854 #endif
1857 static struct xt_target arpt_error_target __read_mostly = {
1858 .name = ARPT_ERROR_TARGET,
1859 .target = arpt_error,
1860 .targetsize = ARPT_FUNCTION_MAXNAMELEN,
1861 .family = NFPROTO_ARP,
1864 static struct nf_sockopt_ops arpt_sockopts = {
1865 .pf = PF_INET,
1866 .set_optmin = ARPT_BASE_CTL,
1867 .set_optmax = ARPT_SO_SET_MAX+1,
1868 .set = do_arpt_set_ctl,
1869 #ifdef CONFIG_COMPAT
1870 .compat_set = compat_do_arpt_set_ctl,
1871 #endif
1872 .get_optmin = ARPT_BASE_CTL,
1873 .get_optmax = ARPT_SO_GET_MAX+1,
1874 .get = do_arpt_get_ctl,
1875 #ifdef CONFIG_COMPAT
1876 .compat_get = compat_do_arpt_get_ctl,
1877 #endif
1878 .owner = THIS_MODULE,
1881 static int __net_init arp_tables_net_init(struct net *net)
1883 return xt_proto_init(net, NFPROTO_ARP);
1886 static void __net_exit arp_tables_net_exit(struct net *net)
1888 xt_proto_fini(net, NFPROTO_ARP);
1891 static struct pernet_operations arp_tables_net_ops = {
1892 .init = arp_tables_net_init,
1893 .exit = arp_tables_net_exit,
1896 static int __init arp_tables_init(void)
1898 int ret;
1900 ret = register_pernet_subsys(&arp_tables_net_ops);
1901 if (ret < 0)
1902 goto err1;
1904 /* Noone else will be downing sem now, so we won't sleep */
1905 ret = xt_register_target(&arpt_standard_target);
1906 if (ret < 0)
1907 goto err2;
1908 ret = xt_register_target(&arpt_error_target);
1909 if (ret < 0)
1910 goto err3;
1912 /* Register setsockopt */
1913 ret = nf_register_sockopt(&arpt_sockopts);
1914 if (ret < 0)
1915 goto err4;
1917 printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1918 return 0;
1920 err4:
1921 xt_unregister_target(&arpt_error_target);
1922 err3:
1923 xt_unregister_target(&arpt_standard_target);
1924 err2:
1925 unregister_pernet_subsys(&arp_tables_net_ops);
1926 err1:
1927 return ret;
1930 static void __exit arp_tables_fini(void)
1932 nf_unregister_sockopt(&arpt_sockopts);
1933 xt_unregister_target(&arpt_error_target);
1934 xt_unregister_target(&arpt_standard_target);
1935 unregister_pernet_subsys(&arp_tables_net_ops);
1938 EXPORT_SYMBOL(arpt_register_table);
1939 EXPORT_SYMBOL(arpt_unregister_table);
1940 EXPORT_SYMBOL(arpt_do_table);
1942 module_init(arp_tables_init);
1943 module_exit(arp_tables_fini);