netfilter: xtables: replace XT_ENTRY_ITERATE macro
[linux-2.6/x86.git] / net / ipv4 / netfilter / arp_tables.c
blobf7338869fc4c3eb5ab95c83c23a1b607e1d17da3
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,
516 unsigned int *i)
518 struct arpt_entry_target *t;
519 struct xt_target *target;
520 int ret;
522 ret = check_entry(e, name);
523 if (ret)
524 return ret;
526 t = arpt_get_target(e);
527 target = try_then_request_module(xt_find_target(NFPROTO_ARP,
528 t->u.user.name,
529 t->u.user.revision),
530 "arpt_%s", t->u.user.name);
531 if (IS_ERR(target) || !target) {
532 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
533 ret = target ? PTR_ERR(target) : -ENOENT;
534 goto out;
536 t->u.kernel.target = target;
538 ret = check_target(e, name);
539 if (ret)
540 goto err;
542 (*i)++;
543 return 0;
544 err:
545 module_put(t->u.kernel.target->me);
546 out:
547 return ret;
550 static bool check_underflow(const struct arpt_entry *e)
552 const struct arpt_entry_target *t;
553 unsigned int verdict;
555 if (!unconditional(&e->arp))
556 return false;
557 t = arpt_get_target_c(e);
558 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
559 return false;
560 verdict = ((struct arpt_standard_target *)t)->verdict;
561 verdict = -verdict - 1;
562 return verdict == NF_DROP || verdict == NF_ACCEPT;
565 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
566 struct xt_table_info *newinfo,
567 const unsigned char *base,
568 const unsigned char *limit,
569 const unsigned int *hook_entries,
570 const unsigned int *underflows,
571 unsigned int valid_hooks,
572 unsigned int *i)
574 unsigned int h;
576 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
577 (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
578 duprintf("Bad offset %p\n", e);
579 return -EINVAL;
582 if (e->next_offset
583 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
584 duprintf("checking: element %p size %u\n",
585 e, e->next_offset);
586 return -EINVAL;
589 /* Check hooks & underflows */
590 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
591 if (!(valid_hooks & (1 << h)))
592 continue;
593 if ((unsigned char *)e - base == hook_entries[h])
594 newinfo->hook_entry[h] = hook_entries[h];
595 if ((unsigned char *)e - base == underflows[h]) {
596 if (!check_underflow(e)) {
597 pr_err("Underflows must be unconditional and "
598 "use the STANDARD target with "
599 "ACCEPT/DROP\n");
600 return -EINVAL;
602 newinfo->underflow[h] = underflows[h];
606 /* Clear counters and comefrom */
607 e->counters = ((struct xt_counters) { 0, 0 });
608 e->comefrom = 0;
610 (*i)++;
611 return 0;
614 static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
616 struct xt_tgdtor_param par;
617 struct arpt_entry_target *t;
619 if (i && (*i)-- == 0)
620 return 1;
622 t = arpt_get_target(e);
623 par.target = t->u.kernel.target;
624 par.targinfo = t->data;
625 par.family = NFPROTO_ARP;
626 if (par.target->destroy != NULL)
627 par.target->destroy(&par);
628 module_put(par.target->me);
629 return 0;
632 /* Checks and translates the user-supplied table segment (held in
633 * newinfo).
635 static int translate_table(const char *name,
636 unsigned int valid_hooks,
637 struct xt_table_info *newinfo,
638 void *entry0,
639 unsigned int size,
640 unsigned int number,
641 const unsigned int *hook_entries,
642 const unsigned int *underflows)
644 struct arpt_entry *iter;
645 unsigned int i;
646 int ret = 0;
648 newinfo->size = size;
649 newinfo->number = number;
651 /* Init all hooks to impossible value. */
652 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
653 newinfo->hook_entry[i] = 0xFFFFFFFF;
654 newinfo->underflow[i] = 0xFFFFFFFF;
657 duprintf("translate_table: size %u\n", newinfo->size);
658 i = 0;
660 /* Walk through entries, checking offsets. */
661 xt_entry_foreach(iter, entry0, newinfo->size) {
662 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
663 entry0 + size, hook_entries, underflows,
664 valid_hooks, &i);
665 if (ret != 0)
666 break;
668 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
669 if (ret != 0)
670 return ret;
672 if (i != number) {
673 duprintf("translate_table: %u not %u entries\n",
674 i, number);
675 return -EINVAL;
678 /* Check hooks all assigned */
679 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
680 /* Only hooks which are valid */
681 if (!(valid_hooks & (1 << i)))
682 continue;
683 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
684 duprintf("Invalid hook entry %u %u\n",
685 i, hook_entries[i]);
686 return -EINVAL;
688 if (newinfo->underflow[i] == 0xFFFFFFFF) {
689 duprintf("Invalid underflow %u %u\n",
690 i, underflows[i]);
691 return -EINVAL;
695 if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
696 duprintf("Looping hook\n");
697 return -ELOOP;
700 /* Finally, each sanity check must pass */
701 i = 0;
702 xt_entry_foreach(iter, entry0, newinfo->size) {
703 ret = find_check_entry(iter, name, size, &i);
704 if (ret != 0)
705 break;
708 if (ret != 0) {
709 xt_entry_foreach(iter, entry0, newinfo->size)
710 if (cleanup_entry(iter, &i) != 0)
711 break;
712 return ret;
715 /* And one copy for every other CPU */
716 for_each_possible_cpu(i) {
717 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
718 memcpy(newinfo->entries[i], entry0, newinfo->size);
721 return ret;
724 /* Gets counters. */
725 static inline int add_entry_to_counter(const struct arpt_entry *e,
726 struct xt_counters total[],
727 unsigned int *i)
729 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
731 (*i)++;
732 return 0;
735 static inline int set_entry_to_counter(const struct arpt_entry *e,
736 struct xt_counters total[],
737 unsigned int *i)
739 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
741 (*i)++;
742 return 0;
745 static void get_counters(const struct xt_table_info *t,
746 struct xt_counters counters[])
748 struct arpt_entry *iter;
749 unsigned int cpu;
750 unsigned int i;
751 unsigned int curcpu;
753 /* Instead of clearing (by a previous call to memset())
754 * the counters and using adds, we set the counters
755 * with data used by 'current' CPU
757 * Bottom half has to be disabled to prevent deadlock
758 * if new softirq were to run and call ipt_do_table
760 local_bh_disable();
761 curcpu = smp_processor_id();
763 i = 0;
764 xt_entry_foreach(iter, t->entries[curcpu], t->size)
765 if (set_entry_to_counter(iter, counters, &i) != 0)
766 break;
768 for_each_possible_cpu(cpu) {
769 if (cpu == curcpu)
770 continue;
771 i = 0;
772 xt_info_wrlock(cpu);
773 xt_entry_foreach(iter, t->entries[cpu], t->size)
774 if (add_entry_to_counter(iter, counters, &i) != 0)
775 break;
776 xt_info_wrunlock(cpu);
778 local_bh_enable();
781 static struct xt_counters *alloc_counters(const struct xt_table *table)
783 unsigned int countersize;
784 struct xt_counters *counters;
785 const struct xt_table_info *private = table->private;
787 /* We need atomic snapshot of counters: rest doesn't change
788 * (other than comefrom, which userspace doesn't care
789 * about).
791 countersize = sizeof(struct xt_counters) * private->number;
792 counters = vmalloc_node(countersize, numa_node_id());
794 if (counters == NULL)
795 return ERR_PTR(-ENOMEM);
797 get_counters(private, counters);
799 return counters;
802 static int copy_entries_to_user(unsigned int total_size,
803 const struct xt_table *table,
804 void __user *userptr)
806 unsigned int off, num;
807 const struct arpt_entry *e;
808 struct xt_counters *counters;
809 struct xt_table_info *private = table->private;
810 int ret = 0;
811 void *loc_cpu_entry;
813 counters = alloc_counters(table);
814 if (IS_ERR(counters))
815 return PTR_ERR(counters);
817 loc_cpu_entry = private->entries[raw_smp_processor_id()];
818 /* ... then copy entire thing ... */
819 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
820 ret = -EFAULT;
821 goto free_counters;
824 /* FIXME: use iterator macros --RR */
825 /* ... then go back and fix counters and names */
826 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
827 const struct arpt_entry_target *t;
829 e = (struct arpt_entry *)(loc_cpu_entry + off);
830 if (copy_to_user(userptr + off
831 + offsetof(struct arpt_entry, counters),
832 &counters[num],
833 sizeof(counters[num])) != 0) {
834 ret = -EFAULT;
835 goto free_counters;
838 t = arpt_get_target_c(e);
839 if (copy_to_user(userptr + off + e->target_offset
840 + offsetof(struct arpt_entry_target,
841 u.user.name),
842 t->u.kernel.target->name,
843 strlen(t->u.kernel.target->name)+1) != 0) {
844 ret = -EFAULT;
845 goto free_counters;
849 free_counters:
850 vfree(counters);
851 return ret;
854 #ifdef CONFIG_COMPAT
855 static void compat_standard_from_user(void *dst, const void *src)
857 int v = *(compat_int_t *)src;
859 if (v > 0)
860 v += xt_compat_calc_jump(NFPROTO_ARP, v);
861 memcpy(dst, &v, sizeof(v));
864 static int compat_standard_to_user(void __user *dst, const void *src)
866 compat_int_t cv = *(int *)src;
868 if (cv > 0)
869 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
870 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
873 static int compat_calc_entry(const struct arpt_entry *e,
874 const struct xt_table_info *info,
875 const void *base, struct xt_table_info *newinfo)
877 const struct arpt_entry_target *t;
878 unsigned int entry_offset;
879 int off, i, ret;
881 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
882 entry_offset = (void *)e - base;
884 t = arpt_get_target_c(e);
885 off += xt_compat_target_offset(t->u.kernel.target);
886 newinfo->size -= off;
887 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
888 if (ret)
889 return ret;
891 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
892 if (info->hook_entry[i] &&
893 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
894 newinfo->hook_entry[i] -= off;
895 if (info->underflow[i] &&
896 (e < (struct arpt_entry *)(base + info->underflow[i])))
897 newinfo->underflow[i] -= off;
899 return 0;
902 static int compat_table_info(const struct xt_table_info *info,
903 struct xt_table_info *newinfo)
905 struct arpt_entry *iter;
906 void *loc_cpu_entry;
907 int ret = 0;
909 if (!newinfo || !info)
910 return -EINVAL;
912 /* we dont care about newinfo->entries[] */
913 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
914 newinfo->initial_entries = 0;
915 loc_cpu_entry = info->entries[raw_smp_processor_id()];
916 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
917 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
918 if (ret != 0)
919 break;
921 return ret;
923 #endif
925 static int get_info(struct net *net, void __user *user,
926 const int *len, int compat)
928 char name[ARPT_TABLE_MAXNAMELEN];
929 struct xt_table *t;
930 int ret;
932 if (*len != sizeof(struct arpt_getinfo)) {
933 duprintf("length %u != %Zu\n", *len,
934 sizeof(struct arpt_getinfo));
935 return -EINVAL;
938 if (copy_from_user(name, user, sizeof(name)) != 0)
939 return -EFAULT;
941 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
942 #ifdef CONFIG_COMPAT
943 if (compat)
944 xt_compat_lock(NFPROTO_ARP);
945 #endif
946 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
947 "arptable_%s", name);
948 if (t && !IS_ERR(t)) {
949 struct arpt_getinfo info;
950 const struct xt_table_info *private = t->private;
951 #ifdef CONFIG_COMPAT
952 struct xt_table_info tmp;
954 if (compat) {
955 ret = compat_table_info(private, &tmp);
956 xt_compat_flush_offsets(NFPROTO_ARP);
957 private = &tmp;
959 #endif
960 info.valid_hooks = t->valid_hooks;
961 memcpy(info.hook_entry, private->hook_entry,
962 sizeof(info.hook_entry));
963 memcpy(info.underflow, private->underflow,
964 sizeof(info.underflow));
965 info.num_entries = private->number;
966 info.size = private->size;
967 strcpy(info.name, name);
969 if (copy_to_user(user, &info, *len) != 0)
970 ret = -EFAULT;
971 else
972 ret = 0;
973 xt_table_unlock(t);
974 module_put(t->me);
975 } else
976 ret = t ? PTR_ERR(t) : -ENOENT;
977 #ifdef CONFIG_COMPAT
978 if (compat)
979 xt_compat_unlock(NFPROTO_ARP);
980 #endif
981 return ret;
984 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
985 const int *len)
987 int ret;
988 struct arpt_get_entries get;
989 struct xt_table *t;
991 if (*len < sizeof(get)) {
992 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
993 return -EINVAL;
995 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
996 return -EFAULT;
997 if (*len != sizeof(struct arpt_get_entries) + get.size) {
998 duprintf("get_entries: %u != %Zu\n", *len,
999 sizeof(struct arpt_get_entries) + get.size);
1000 return -EINVAL;
1003 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1004 if (t && !IS_ERR(t)) {
1005 const struct xt_table_info *private = t->private;
1007 duprintf("t->private->number = %u\n",
1008 private->number);
1009 if (get.size == private->size)
1010 ret = copy_entries_to_user(private->size,
1011 t, uptr->entrytable);
1012 else {
1013 duprintf("get_entries: I've got %u not %u!\n",
1014 private->size, get.size);
1015 ret = -EAGAIN;
1017 module_put(t->me);
1018 xt_table_unlock(t);
1019 } else
1020 ret = t ? PTR_ERR(t) : -ENOENT;
1022 return ret;
1025 static int __do_replace(struct net *net, const char *name,
1026 unsigned int valid_hooks,
1027 struct xt_table_info *newinfo,
1028 unsigned int num_counters,
1029 void __user *counters_ptr)
1031 int ret;
1032 struct xt_table *t;
1033 struct xt_table_info *oldinfo;
1034 struct xt_counters *counters;
1035 void *loc_cpu_old_entry;
1036 struct arpt_entry *iter;
1038 ret = 0;
1039 counters = vmalloc_node(num_counters * sizeof(struct xt_counters),
1040 numa_node_id());
1041 if (!counters) {
1042 ret = -ENOMEM;
1043 goto out;
1046 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
1047 "arptable_%s", name);
1048 if (!t || IS_ERR(t)) {
1049 ret = t ? PTR_ERR(t) : -ENOENT;
1050 goto free_newinfo_counters_untrans;
1053 /* You lied! */
1054 if (valid_hooks != t->valid_hooks) {
1055 duprintf("Valid hook crap: %08X vs %08X\n",
1056 valid_hooks, t->valid_hooks);
1057 ret = -EINVAL;
1058 goto put_module;
1061 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1062 if (!oldinfo)
1063 goto put_module;
1065 /* Update module usage count based on number of rules */
1066 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1067 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1068 if ((oldinfo->number > oldinfo->initial_entries) ||
1069 (newinfo->number <= oldinfo->initial_entries))
1070 module_put(t->me);
1071 if ((oldinfo->number > oldinfo->initial_entries) &&
1072 (newinfo->number <= oldinfo->initial_entries))
1073 module_put(t->me);
1075 /* Get the old counters, and synchronize with replace */
1076 get_counters(oldinfo, counters);
1078 /* Decrease module usage counts and free resource */
1079 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1080 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
1081 if (cleanup_entry(iter, NULL) != 0)
1082 break;
1084 xt_free_table_info(oldinfo);
1085 if (copy_to_user(counters_ptr, counters,
1086 sizeof(struct xt_counters) * num_counters) != 0)
1087 ret = -EFAULT;
1088 vfree(counters);
1089 xt_table_unlock(t);
1090 return ret;
1092 put_module:
1093 module_put(t->me);
1094 xt_table_unlock(t);
1095 free_newinfo_counters_untrans:
1096 vfree(counters);
1097 out:
1098 return ret;
1101 static int do_replace(struct net *net, const void __user *user,
1102 unsigned int len)
1104 int ret;
1105 struct arpt_replace tmp;
1106 struct xt_table_info *newinfo;
1107 void *loc_cpu_entry;
1108 struct arpt_entry *iter;
1110 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1111 return -EFAULT;
1113 /* overflow check */
1114 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1115 return -ENOMEM;
1117 newinfo = xt_alloc_table_info(tmp.size);
1118 if (!newinfo)
1119 return -ENOMEM;
1121 /* choose the copy that is on our node/cpu */
1122 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1123 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1124 tmp.size) != 0) {
1125 ret = -EFAULT;
1126 goto free_newinfo;
1129 ret = translate_table(tmp.name, tmp.valid_hooks,
1130 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
1131 tmp.hook_entry, tmp.underflow);
1132 if (ret != 0)
1133 goto free_newinfo;
1135 duprintf("arp_tables: Translated table\n");
1137 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1138 tmp.num_counters, tmp.counters);
1139 if (ret)
1140 goto free_newinfo_untrans;
1141 return 0;
1143 free_newinfo_untrans:
1144 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1145 if (cleanup_entry(iter, NULL) != 0)
1146 break;
1147 free_newinfo:
1148 xt_free_table_info(newinfo);
1149 return ret;
1152 /* We're lazy, and add to the first CPU; overflow works its fey magic
1153 * and everything is OK. */
1154 static int
1155 add_counter_to_entry(struct arpt_entry *e,
1156 const struct xt_counters addme[],
1157 unsigned int *i)
1159 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1161 (*i)++;
1162 return 0;
1165 static int do_add_counters(struct net *net, const void __user *user,
1166 unsigned int len, int compat)
1168 unsigned int i, curcpu;
1169 struct xt_counters_info tmp;
1170 struct xt_counters *paddc;
1171 unsigned int num_counters;
1172 const char *name;
1173 int size;
1174 void *ptmp;
1175 struct xt_table *t;
1176 const struct xt_table_info *private;
1177 int ret = 0;
1178 void *loc_cpu_entry;
1179 struct arpt_entry *iter;
1180 #ifdef CONFIG_COMPAT
1181 struct compat_xt_counters_info compat_tmp;
1183 if (compat) {
1184 ptmp = &compat_tmp;
1185 size = sizeof(struct compat_xt_counters_info);
1186 } else
1187 #endif
1189 ptmp = &tmp;
1190 size = sizeof(struct xt_counters_info);
1193 if (copy_from_user(ptmp, user, size) != 0)
1194 return -EFAULT;
1196 #ifdef CONFIG_COMPAT
1197 if (compat) {
1198 num_counters = compat_tmp.num_counters;
1199 name = compat_tmp.name;
1200 } else
1201 #endif
1203 num_counters = tmp.num_counters;
1204 name = tmp.name;
1207 if (len != size + num_counters * sizeof(struct xt_counters))
1208 return -EINVAL;
1210 paddc = vmalloc_node(len - size, numa_node_id());
1211 if (!paddc)
1212 return -ENOMEM;
1214 if (copy_from_user(paddc, user + size, len - size) != 0) {
1215 ret = -EFAULT;
1216 goto free;
1219 t = xt_find_table_lock(net, NFPROTO_ARP, name);
1220 if (!t || IS_ERR(t)) {
1221 ret = t ? PTR_ERR(t) : -ENOENT;
1222 goto free;
1225 local_bh_disable();
1226 private = t->private;
1227 if (private->number != num_counters) {
1228 ret = -EINVAL;
1229 goto unlock_up_free;
1232 i = 0;
1233 /* Choose the copy that is on our node */
1234 curcpu = smp_processor_id();
1235 loc_cpu_entry = private->entries[curcpu];
1236 xt_info_wrlock(curcpu);
1237 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1238 if (add_counter_to_entry(iter, paddc, &i) != 0)
1239 break;
1240 xt_info_wrunlock(curcpu);
1241 unlock_up_free:
1242 local_bh_enable();
1243 xt_table_unlock(t);
1244 module_put(t->me);
1245 free:
1246 vfree(paddc);
1248 return ret;
1251 #ifdef CONFIG_COMPAT
1252 static inline int
1253 compat_release_entry(struct compat_arpt_entry *e, unsigned int *i)
1255 struct arpt_entry_target *t;
1257 if (i && (*i)-- == 0)
1258 return 1;
1260 t = compat_arpt_get_target(e);
1261 module_put(t->u.kernel.target->me);
1262 return 0;
1265 static inline int
1266 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1267 struct xt_table_info *newinfo,
1268 unsigned int *size,
1269 const unsigned char *base,
1270 const unsigned char *limit,
1271 const unsigned int *hook_entries,
1272 const unsigned int *underflows,
1273 unsigned int *i,
1274 const char *name)
1276 struct arpt_entry_target *t;
1277 struct xt_target *target;
1278 unsigned int entry_offset;
1279 int ret, off, h;
1281 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1282 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1283 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
1284 duprintf("Bad offset %p, limit = %p\n", e, limit);
1285 return -EINVAL;
1288 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1289 sizeof(struct compat_xt_entry_target)) {
1290 duprintf("checking: element %p size %u\n",
1291 e, e->next_offset);
1292 return -EINVAL;
1295 /* For purposes of check_entry casting the compat entry is fine */
1296 ret = check_entry((struct arpt_entry *)e, name);
1297 if (ret)
1298 return ret;
1300 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1301 entry_offset = (void *)e - (void *)base;
1303 t = compat_arpt_get_target(e);
1304 target = try_then_request_module(xt_find_target(NFPROTO_ARP,
1305 t->u.user.name,
1306 t->u.user.revision),
1307 "arpt_%s", t->u.user.name);
1308 if (IS_ERR(target) || !target) {
1309 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1310 t->u.user.name);
1311 ret = target ? PTR_ERR(target) : -ENOENT;
1312 goto out;
1314 t->u.kernel.target = target;
1316 off += xt_compat_target_offset(target);
1317 *size += off;
1318 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1319 if (ret)
1320 goto release_target;
1322 /* Check hooks & underflows */
1323 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1324 if ((unsigned char *)e - base == hook_entries[h])
1325 newinfo->hook_entry[h] = hook_entries[h];
1326 if ((unsigned char *)e - base == underflows[h])
1327 newinfo->underflow[h] = underflows[h];
1330 /* Clear counters and comefrom */
1331 memset(&e->counters, 0, sizeof(e->counters));
1332 e->comefrom = 0;
1334 (*i)++;
1335 return 0;
1337 release_target:
1338 module_put(t->u.kernel.target->me);
1339 out:
1340 return ret;
1343 static int
1344 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1345 unsigned int *size, const char *name,
1346 struct xt_table_info *newinfo, unsigned char *base)
1348 struct arpt_entry_target *t;
1349 struct xt_target *target;
1350 struct arpt_entry *de;
1351 unsigned int origsize;
1352 int ret, h;
1354 ret = 0;
1355 origsize = *size;
1356 de = (struct arpt_entry *)*dstptr;
1357 memcpy(de, e, sizeof(struct arpt_entry));
1358 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1360 *dstptr += sizeof(struct arpt_entry);
1361 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1363 de->target_offset = e->target_offset - (origsize - *size);
1364 t = compat_arpt_get_target(e);
1365 target = t->u.kernel.target;
1366 xt_compat_target_from_user(t, dstptr, size);
1368 de->next_offset = e->next_offset - (origsize - *size);
1369 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1370 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1371 newinfo->hook_entry[h] -= origsize - *size;
1372 if ((unsigned char *)de - base < newinfo->underflow[h])
1373 newinfo->underflow[h] -= origsize - *size;
1375 return ret;
1378 static inline int compat_check_entry(struct arpt_entry *e, const char *name,
1379 unsigned int *i)
1381 int ret;
1383 ret = check_target(e, name);
1384 if (ret)
1385 return ret;
1387 (*i)++;
1388 return 0;
1391 static int translate_compat_table(const char *name,
1392 unsigned int valid_hooks,
1393 struct xt_table_info **pinfo,
1394 void **pentry0,
1395 unsigned int total_size,
1396 unsigned int number,
1397 unsigned int *hook_entries,
1398 unsigned int *underflows)
1400 unsigned int i, j;
1401 struct xt_table_info *newinfo, *info;
1402 void *pos, *entry0, *entry1;
1403 struct compat_arpt_entry *iter0;
1404 struct arpt_entry *iter1;
1405 unsigned int size;
1406 int ret = 0;
1408 info = *pinfo;
1409 entry0 = *pentry0;
1410 size = total_size;
1411 info->number = number;
1413 /* Init all hooks to impossible value. */
1414 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1415 info->hook_entry[i] = 0xFFFFFFFF;
1416 info->underflow[i] = 0xFFFFFFFF;
1419 duprintf("translate_compat_table: size %u\n", info->size);
1420 j = 0;
1421 xt_compat_lock(NFPROTO_ARP);
1422 /* Walk through entries, checking offsets. */
1423 xt_entry_foreach(iter0, entry0, total_size) {
1424 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1425 entry0, entry0 + total_size, hook_entries, underflows,
1426 &j, name);
1427 if (ret != 0)
1428 break;
1430 if (ret != 0)
1431 goto out_unlock;
1433 ret = -EINVAL;
1434 if (j != number) {
1435 duprintf("translate_compat_table: %u not %u entries\n",
1436 j, number);
1437 goto out_unlock;
1440 /* Check hooks all assigned */
1441 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1442 /* Only hooks which are valid */
1443 if (!(valid_hooks & (1 << i)))
1444 continue;
1445 if (info->hook_entry[i] == 0xFFFFFFFF) {
1446 duprintf("Invalid hook entry %u %u\n",
1447 i, hook_entries[i]);
1448 goto out_unlock;
1450 if (info->underflow[i] == 0xFFFFFFFF) {
1451 duprintf("Invalid underflow %u %u\n",
1452 i, underflows[i]);
1453 goto out_unlock;
1457 ret = -ENOMEM;
1458 newinfo = xt_alloc_table_info(size);
1459 if (!newinfo)
1460 goto out_unlock;
1462 newinfo->number = number;
1463 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1464 newinfo->hook_entry[i] = info->hook_entry[i];
1465 newinfo->underflow[i] = info->underflow[i];
1467 entry1 = newinfo->entries[raw_smp_processor_id()];
1468 pos = entry1;
1469 size = total_size;
1470 xt_entry_foreach(iter0, entry0, total_size) {
1471 ret = compat_copy_entry_from_user(iter0, &pos,
1472 &size, name, newinfo, entry1);
1473 if (ret != 0)
1474 break;
1476 xt_compat_flush_offsets(NFPROTO_ARP);
1477 xt_compat_unlock(NFPROTO_ARP);
1478 if (ret)
1479 goto free_newinfo;
1481 ret = -ELOOP;
1482 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1483 goto free_newinfo;
1485 i = 0;
1486 xt_entry_foreach(iter1, entry1, newinfo->size) {
1487 ret = compat_check_entry(iter1, name, &i);
1488 if (ret != 0)
1489 break;
1491 if (ret) {
1493 * The first i matches need cleanup_entry (calls ->destroy)
1494 * because they had called ->check already. The other j-i
1495 * entries need only release.
1497 int skip = i;
1498 j -= i;
1499 xt_entry_foreach(iter0, entry0, newinfo->size) {
1500 if (skip-- > 0)
1501 continue;
1502 if (compat_release_entry(iter0, &j) != 0)
1503 break;
1505 xt_entry_foreach(iter1, entry1, newinfo->size)
1506 if (cleanup_entry(iter1, &i) != 0)
1507 break;
1508 xt_free_table_info(newinfo);
1509 return ret;
1512 /* And one copy for every other CPU */
1513 for_each_possible_cpu(i)
1514 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1515 memcpy(newinfo->entries[i], entry1, newinfo->size);
1517 *pinfo = newinfo;
1518 *pentry0 = entry1;
1519 xt_free_table_info(info);
1520 return 0;
1522 free_newinfo:
1523 xt_free_table_info(newinfo);
1524 out:
1525 xt_entry_foreach(iter0, entry0, total_size)
1526 if (compat_release_entry(iter0, &j) != 0)
1527 break;
1528 return ret;
1529 out_unlock:
1530 xt_compat_flush_offsets(NFPROTO_ARP);
1531 xt_compat_unlock(NFPROTO_ARP);
1532 goto out;
1535 struct compat_arpt_replace {
1536 char name[ARPT_TABLE_MAXNAMELEN];
1537 u32 valid_hooks;
1538 u32 num_entries;
1539 u32 size;
1540 u32 hook_entry[NF_ARP_NUMHOOKS];
1541 u32 underflow[NF_ARP_NUMHOOKS];
1542 u32 num_counters;
1543 compat_uptr_t counters;
1544 struct compat_arpt_entry entries[0];
1547 static int compat_do_replace(struct net *net, void __user *user,
1548 unsigned int len)
1550 int ret;
1551 struct compat_arpt_replace tmp;
1552 struct xt_table_info *newinfo;
1553 void *loc_cpu_entry;
1554 struct arpt_entry *iter;
1556 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1557 return -EFAULT;
1559 /* overflow check */
1560 if (tmp.size >= INT_MAX / num_possible_cpus())
1561 return -ENOMEM;
1562 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1563 return -ENOMEM;
1565 newinfo = xt_alloc_table_info(tmp.size);
1566 if (!newinfo)
1567 return -ENOMEM;
1569 /* choose the copy that is on our node/cpu */
1570 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1571 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1572 ret = -EFAULT;
1573 goto free_newinfo;
1576 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1577 &newinfo, &loc_cpu_entry, tmp.size,
1578 tmp.num_entries, tmp.hook_entry,
1579 tmp.underflow);
1580 if (ret != 0)
1581 goto free_newinfo;
1583 duprintf("compat_do_replace: Translated table\n");
1585 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1586 tmp.num_counters, compat_ptr(tmp.counters));
1587 if (ret)
1588 goto free_newinfo_untrans;
1589 return 0;
1591 free_newinfo_untrans:
1592 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1593 if (cleanup_entry(iter, NULL) != 0)
1594 break;
1595 free_newinfo:
1596 xt_free_table_info(newinfo);
1597 return ret;
1600 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1601 unsigned int len)
1603 int ret;
1605 if (!capable(CAP_NET_ADMIN))
1606 return -EPERM;
1608 switch (cmd) {
1609 case ARPT_SO_SET_REPLACE:
1610 ret = compat_do_replace(sock_net(sk), user, len);
1611 break;
1613 case ARPT_SO_SET_ADD_COUNTERS:
1614 ret = do_add_counters(sock_net(sk), user, len, 1);
1615 break;
1617 default:
1618 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1619 ret = -EINVAL;
1622 return ret;
1625 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1626 compat_uint_t *size,
1627 struct xt_counters *counters,
1628 unsigned int *i)
1630 struct arpt_entry_target *t;
1631 struct compat_arpt_entry __user *ce;
1632 u_int16_t target_offset, next_offset;
1633 compat_uint_t origsize;
1634 int ret;
1636 ret = -EFAULT;
1637 origsize = *size;
1638 ce = (struct compat_arpt_entry __user *)*dstptr;
1639 if (copy_to_user(ce, e, sizeof(struct arpt_entry)))
1640 goto out;
1642 if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1643 goto out;
1645 *dstptr += sizeof(struct compat_arpt_entry);
1646 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1648 target_offset = e->target_offset - (origsize - *size);
1650 t = arpt_get_target(e);
1651 ret = xt_compat_target_to_user(t, dstptr, size);
1652 if (ret)
1653 goto out;
1654 ret = -EFAULT;
1655 next_offset = e->next_offset - (origsize - *size);
1656 if (put_user(target_offset, &ce->target_offset))
1657 goto out;
1658 if (put_user(next_offset, &ce->next_offset))
1659 goto out;
1661 (*i)++;
1662 return 0;
1663 out:
1664 return ret;
1667 static int compat_copy_entries_to_user(unsigned int total_size,
1668 struct xt_table *table,
1669 void __user *userptr)
1671 struct xt_counters *counters;
1672 const struct xt_table_info *private = table->private;
1673 void __user *pos;
1674 unsigned int size;
1675 int ret = 0;
1676 void *loc_cpu_entry;
1677 unsigned int i = 0;
1678 struct arpt_entry *iter;
1680 counters = alloc_counters(table);
1681 if (IS_ERR(counters))
1682 return PTR_ERR(counters);
1684 /* choose the copy on our node/cpu */
1685 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1686 pos = userptr;
1687 size = total_size;
1688 xt_entry_foreach(iter, loc_cpu_entry, total_size) {
1689 ret = compat_copy_entry_to_user(iter, &pos,
1690 &size, counters, &i);
1691 if (ret != 0)
1692 break;
1694 vfree(counters);
1695 return ret;
1698 struct compat_arpt_get_entries {
1699 char name[ARPT_TABLE_MAXNAMELEN];
1700 compat_uint_t size;
1701 struct compat_arpt_entry entrytable[0];
1704 static int compat_get_entries(struct net *net,
1705 struct compat_arpt_get_entries __user *uptr,
1706 int *len)
1708 int ret;
1709 struct compat_arpt_get_entries get;
1710 struct xt_table *t;
1712 if (*len < sizeof(get)) {
1713 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1714 return -EINVAL;
1716 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1717 return -EFAULT;
1718 if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1719 duprintf("compat_get_entries: %u != %zu\n",
1720 *len, sizeof(get) + get.size);
1721 return -EINVAL;
1724 xt_compat_lock(NFPROTO_ARP);
1725 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1726 if (t && !IS_ERR(t)) {
1727 const struct xt_table_info *private = t->private;
1728 struct xt_table_info info;
1730 duprintf("t->private->number = %u\n", private->number);
1731 ret = compat_table_info(private, &info);
1732 if (!ret && get.size == info.size) {
1733 ret = compat_copy_entries_to_user(private->size,
1734 t, uptr->entrytable);
1735 } else if (!ret) {
1736 duprintf("compat_get_entries: I've got %u not %u!\n",
1737 private->size, get.size);
1738 ret = -EAGAIN;
1740 xt_compat_flush_offsets(NFPROTO_ARP);
1741 module_put(t->me);
1742 xt_table_unlock(t);
1743 } else
1744 ret = t ? PTR_ERR(t) : -ENOENT;
1746 xt_compat_unlock(NFPROTO_ARP);
1747 return ret;
1750 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1752 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1753 int *len)
1755 int ret;
1757 if (!capable(CAP_NET_ADMIN))
1758 return -EPERM;
1760 switch (cmd) {
1761 case ARPT_SO_GET_INFO:
1762 ret = get_info(sock_net(sk), user, len, 1);
1763 break;
1764 case ARPT_SO_GET_ENTRIES:
1765 ret = compat_get_entries(sock_net(sk), user, len);
1766 break;
1767 default:
1768 ret = do_arpt_get_ctl(sk, cmd, user, len);
1770 return ret;
1772 #endif
1774 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1776 int ret;
1778 if (!capable(CAP_NET_ADMIN))
1779 return -EPERM;
1781 switch (cmd) {
1782 case ARPT_SO_SET_REPLACE:
1783 ret = do_replace(sock_net(sk), user, len);
1784 break;
1786 case ARPT_SO_SET_ADD_COUNTERS:
1787 ret = do_add_counters(sock_net(sk), user, len, 0);
1788 break;
1790 default:
1791 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1792 ret = -EINVAL;
1795 return ret;
1798 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1800 int ret;
1802 if (!capable(CAP_NET_ADMIN))
1803 return -EPERM;
1805 switch (cmd) {
1806 case ARPT_SO_GET_INFO:
1807 ret = get_info(sock_net(sk), user, len, 0);
1808 break;
1810 case ARPT_SO_GET_ENTRIES:
1811 ret = get_entries(sock_net(sk), user, len);
1812 break;
1814 case ARPT_SO_GET_REVISION_TARGET: {
1815 struct xt_get_revision rev;
1817 if (*len != sizeof(rev)) {
1818 ret = -EINVAL;
1819 break;
1821 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1822 ret = -EFAULT;
1823 break;
1826 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1827 rev.revision, 1, &ret),
1828 "arpt_%s", rev.name);
1829 break;
1832 default:
1833 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1834 ret = -EINVAL;
1837 return ret;
1840 struct xt_table *arpt_register_table(struct net *net,
1841 const struct xt_table *table,
1842 const struct arpt_replace *repl)
1844 int ret;
1845 struct xt_table_info *newinfo;
1846 struct xt_table_info bootstrap
1847 = { 0, 0, 0, { 0 }, { 0 }, { } };
1848 void *loc_cpu_entry;
1849 struct xt_table *new_table;
1851 newinfo = xt_alloc_table_info(repl->size);
1852 if (!newinfo) {
1853 ret = -ENOMEM;
1854 goto out;
1857 /* choose the copy on our node/cpu */
1858 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1859 memcpy(loc_cpu_entry, repl->entries, repl->size);
1861 ret = translate_table(table->name, table->valid_hooks,
1862 newinfo, loc_cpu_entry, repl->size,
1863 repl->num_entries,
1864 repl->hook_entry,
1865 repl->underflow);
1867 duprintf("arpt_register_table: translate table gives %d\n", ret);
1868 if (ret != 0)
1869 goto out_free;
1871 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1872 if (IS_ERR(new_table)) {
1873 ret = PTR_ERR(new_table);
1874 goto out_free;
1876 return new_table;
1878 out_free:
1879 xt_free_table_info(newinfo);
1880 out:
1881 return ERR_PTR(ret);
1884 void arpt_unregister_table(struct xt_table *table)
1886 struct xt_table_info *private;
1887 void *loc_cpu_entry;
1888 struct module *table_owner = table->me;
1889 struct arpt_entry *iter;
1891 private = xt_unregister_table(table);
1893 /* Decrease module usage counts and free resources */
1894 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1895 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1896 if (cleanup_entry(iter, NULL) != 0)
1897 break;
1898 if (private->number > private->initial_entries)
1899 module_put(table_owner);
1900 xt_free_table_info(private);
1903 /* The built-in targets: standard (NULL) and error. */
1904 static struct xt_target arpt_standard_target __read_mostly = {
1905 .name = ARPT_STANDARD_TARGET,
1906 .targetsize = sizeof(int),
1907 .family = NFPROTO_ARP,
1908 #ifdef CONFIG_COMPAT
1909 .compatsize = sizeof(compat_int_t),
1910 .compat_from_user = compat_standard_from_user,
1911 .compat_to_user = compat_standard_to_user,
1912 #endif
1915 static struct xt_target arpt_error_target __read_mostly = {
1916 .name = ARPT_ERROR_TARGET,
1917 .target = arpt_error,
1918 .targetsize = ARPT_FUNCTION_MAXNAMELEN,
1919 .family = NFPROTO_ARP,
1922 static struct nf_sockopt_ops arpt_sockopts = {
1923 .pf = PF_INET,
1924 .set_optmin = ARPT_BASE_CTL,
1925 .set_optmax = ARPT_SO_SET_MAX+1,
1926 .set = do_arpt_set_ctl,
1927 #ifdef CONFIG_COMPAT
1928 .compat_set = compat_do_arpt_set_ctl,
1929 #endif
1930 .get_optmin = ARPT_BASE_CTL,
1931 .get_optmax = ARPT_SO_GET_MAX+1,
1932 .get = do_arpt_get_ctl,
1933 #ifdef CONFIG_COMPAT
1934 .compat_get = compat_do_arpt_get_ctl,
1935 #endif
1936 .owner = THIS_MODULE,
1939 static int __net_init arp_tables_net_init(struct net *net)
1941 return xt_proto_init(net, NFPROTO_ARP);
1944 static void __net_exit arp_tables_net_exit(struct net *net)
1946 xt_proto_fini(net, NFPROTO_ARP);
1949 static struct pernet_operations arp_tables_net_ops = {
1950 .init = arp_tables_net_init,
1951 .exit = arp_tables_net_exit,
1954 static int __init arp_tables_init(void)
1956 int ret;
1958 ret = register_pernet_subsys(&arp_tables_net_ops);
1959 if (ret < 0)
1960 goto err1;
1962 /* Noone else will be downing sem now, so we won't sleep */
1963 ret = xt_register_target(&arpt_standard_target);
1964 if (ret < 0)
1965 goto err2;
1966 ret = xt_register_target(&arpt_error_target);
1967 if (ret < 0)
1968 goto err3;
1970 /* Register setsockopt */
1971 ret = nf_register_sockopt(&arpt_sockopts);
1972 if (ret < 0)
1973 goto err4;
1975 printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1976 return 0;
1978 err4:
1979 xt_unregister_target(&arpt_error_target);
1980 err3:
1981 xt_unregister_target(&arpt_standard_target);
1982 err2:
1983 unregister_pernet_subsys(&arp_tables_net_ops);
1984 err1:
1985 return ret;
1988 static void __exit arp_tables_fini(void)
1990 nf_unregister_sockopt(&arpt_sockopts);
1991 xt_unregister_target(&arpt_error_target);
1992 xt_unregister_target(&arpt_standard_target);
1993 unregister_pernet_subsys(&arp_tables_net_ops);
1996 EXPORT_SYMBOL(arpt_register_table);
1997 EXPORT_SYMBOL(arpt_unregister_table);
1998 EXPORT_SYMBOL(arpt_do_table);
2000 module_init(arp_tables_init);
2001 module_exit(arp_tables_fini);