netfilter: x_table: speedup compat operations
[linux-2.6/btrfs-unstable.git] / net / ipv4 / netfilter / arp_tables.c
blob47e5178b998b932d783f9417a29b0c3819be704d
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) WARN_ON(!(x))
53 #else
54 #define ARP_NF_ASSERT(x)
55 #endif
57 void *arpt_alloc_initial_table(const struct xt_table *info)
59 return xt_alloc_initial_table(arpt, ARPT);
61 EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
63 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
64 const char *hdr_addr, int len)
66 int i, ret;
68 if (len > ARPT_DEV_ADDR_LEN_MAX)
69 len = ARPT_DEV_ADDR_LEN_MAX;
71 ret = 0;
72 for (i = 0; i < len; i++)
73 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
75 return ret != 0;
79 * Unfortunatly, _b and _mask are not aligned to an int (or long int)
80 * Some arches dont care, unrolling the loop is a win on them.
81 * For other arches, we only have a 16bit alignement.
83 static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
85 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
86 unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
87 #else
88 unsigned long ret = 0;
89 const u16 *a = (const u16 *)_a;
90 const u16 *b = (const u16 *)_b;
91 const u16 *mask = (const u16 *)_mask;
92 int i;
94 for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
95 ret |= (a[i] ^ b[i]) & mask[i];
96 #endif
97 return ret;
100 /* Returns whether packet matches rule or not. */
101 static inline int arp_packet_match(const struct arphdr *arphdr,
102 struct net_device *dev,
103 const char *indev,
104 const char *outdev,
105 const struct arpt_arp *arpinfo)
107 const char *arpptr = (char *)(arphdr + 1);
108 const char *src_devaddr, *tgt_devaddr;
109 __be32 src_ipaddr, tgt_ipaddr;
110 long ret;
112 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
114 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
115 ARPT_INV_ARPOP)) {
116 dprintf("ARP operation field mismatch.\n");
117 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
118 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
119 return 0;
122 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
123 ARPT_INV_ARPHRD)) {
124 dprintf("ARP hardware address format mismatch.\n");
125 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
126 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
127 return 0;
130 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
131 ARPT_INV_ARPPRO)) {
132 dprintf("ARP protocol address format mismatch.\n");
133 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
134 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
135 return 0;
138 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
139 ARPT_INV_ARPHLN)) {
140 dprintf("ARP hardware address length mismatch.\n");
141 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
142 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
143 return 0;
146 src_devaddr = arpptr;
147 arpptr += dev->addr_len;
148 memcpy(&src_ipaddr, arpptr, sizeof(u32));
149 arpptr += sizeof(u32);
150 tgt_devaddr = arpptr;
151 arpptr += dev->addr_len;
152 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
154 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
155 ARPT_INV_SRCDEVADDR) ||
156 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
157 ARPT_INV_TGTDEVADDR)) {
158 dprintf("Source or target device address mismatch.\n");
160 return 0;
163 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
164 ARPT_INV_SRCIP) ||
165 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
166 ARPT_INV_TGTIP)) {
167 dprintf("Source or target IP address mismatch.\n");
169 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
170 &src_ipaddr,
171 &arpinfo->smsk.s_addr,
172 &arpinfo->src.s_addr,
173 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
174 dprintf("TGT: %pI4 Mask: %pI4 Target: %pI4.%s\n",
175 &tgt_ipaddr,
176 &arpinfo->tmsk.s_addr,
177 &arpinfo->tgt.s_addr,
178 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
179 return 0;
182 /* Look for ifname matches. */
183 ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
185 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
186 dprintf("VIA in mismatch (%s vs %s).%s\n",
187 indev, arpinfo->iniface,
188 arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
189 return 0;
192 ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
194 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
195 dprintf("VIA out mismatch (%s vs %s).%s\n",
196 outdev, arpinfo->outiface,
197 arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
198 return 0;
201 return 1;
202 #undef FWINV
205 static inline int arp_checkentry(const struct arpt_arp *arp)
207 if (arp->flags & ~ARPT_F_MASK) {
208 duprintf("Unknown flag bits set: %08X\n",
209 arp->flags & ~ARPT_F_MASK);
210 return 0;
212 if (arp->invflags & ~ARPT_INV_MASK) {
213 duprintf("Unknown invflag bits set: %08X\n",
214 arp->invflags & ~ARPT_INV_MASK);
215 return 0;
218 return 1;
221 static unsigned int
222 arpt_error(struct sk_buff *skb, const struct xt_action_param *par)
224 if (net_ratelimit())
225 pr_err("arp_tables: error: '%s'\n",
226 (const char *)par->targinfo);
228 return NF_DROP;
231 static inline const struct xt_entry_target *
232 arpt_get_target_c(const struct arpt_entry *e)
234 return arpt_get_target((struct arpt_entry *)e);
237 static inline struct arpt_entry *
238 get_entry(const void *base, unsigned int offset)
240 return (struct arpt_entry *)(base + offset);
243 static inline __pure
244 struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
246 return (void *)entry + entry->next_offset;
249 unsigned int arpt_do_table(struct sk_buff *skb,
250 unsigned int hook,
251 const struct net_device *in,
252 const struct net_device *out,
253 struct xt_table *table)
255 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
256 unsigned int verdict = NF_DROP;
257 const struct arphdr *arp;
258 struct arpt_entry *e, *back;
259 const char *indev, *outdev;
260 void *table_base;
261 const struct xt_table_info *private;
262 struct xt_action_param acpar;
264 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
265 return NF_DROP;
267 indev = in ? in->name : nulldevname;
268 outdev = out ? out->name : nulldevname;
270 xt_info_rdlock_bh();
271 private = table->private;
272 table_base = private->entries[smp_processor_id()];
274 e = get_entry(table_base, private->hook_entry[hook]);
275 back = get_entry(table_base, private->underflow[hook]);
277 acpar.in = in;
278 acpar.out = out;
279 acpar.hooknum = hook;
280 acpar.family = NFPROTO_ARP;
281 acpar.hotdrop = false;
283 arp = arp_hdr(skb);
284 do {
285 const struct xt_entry_target *t;
287 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
288 e = arpt_next_entry(e);
289 continue;
292 ADD_COUNTER(e->counters, arp_hdr_len(skb->dev), 1);
294 t = arpt_get_target_c(e);
296 /* Standard target? */
297 if (!t->u.kernel.target->target) {
298 int v;
300 v = ((struct xt_standard_target *)t)->verdict;
301 if (v < 0) {
302 /* Pop from stack? */
303 if (v != XT_RETURN) {
304 verdict = (unsigned)(-v) - 1;
305 break;
307 e = back;
308 back = get_entry(table_base, back->comefrom);
309 continue;
311 if (table_base + v
312 != arpt_next_entry(e)) {
313 /* Save old back ptr in next entry */
314 struct arpt_entry *next = arpt_next_entry(e);
315 next->comefrom = (void *)back - table_base;
317 /* set back pointer to next entry */
318 back = next;
321 e = get_entry(table_base, v);
322 continue;
325 /* Targets which reenter must return
326 * abs. verdicts
328 acpar.target = t->u.kernel.target;
329 acpar.targinfo = t->data;
330 verdict = t->u.kernel.target->target(skb, &acpar);
332 /* Target might have changed stuff. */
333 arp = arp_hdr(skb);
335 if (verdict == XT_CONTINUE)
336 e = arpt_next_entry(e);
337 else
338 /* Verdict */
339 break;
340 } while (!acpar.hotdrop);
341 xt_info_rdunlock_bh();
343 if (acpar.hotdrop)
344 return NF_DROP;
345 else
346 return verdict;
349 /* All zeroes == unconditional rule. */
350 static inline bool unconditional(const struct arpt_arp *arp)
352 static const struct arpt_arp uncond;
354 return memcmp(arp, &uncond, sizeof(uncond)) == 0;
357 /* Figures out from what hook each rule can be called: returns 0 if
358 * there are loops. Puts hook bitmask in comefrom.
360 static int mark_source_chains(const struct xt_table_info *newinfo,
361 unsigned int valid_hooks, void *entry0)
363 unsigned int hook;
365 /* No recursion; use packet counter to save back ptrs (reset
366 * to 0 as we leave), and comefrom to save source hook bitmask.
368 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
369 unsigned int pos = newinfo->hook_entry[hook];
370 struct arpt_entry *e
371 = (struct arpt_entry *)(entry0 + pos);
373 if (!(valid_hooks & (1 << hook)))
374 continue;
376 /* Set initial back pointer. */
377 e->counters.pcnt = pos;
379 for (;;) {
380 const struct xt_standard_target *t
381 = (void *)arpt_get_target_c(e);
382 int visited = e->comefrom & (1 << hook);
384 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
385 pr_notice("arptables: loop hook %u pos %u %08X.\n",
386 hook, pos, e->comefrom);
387 return 0;
389 e->comefrom
390 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
392 /* Unconditional return/END. */
393 if ((e->target_offset == sizeof(struct arpt_entry) &&
394 (strcmp(t->target.u.user.name,
395 XT_STANDARD_TARGET) == 0) &&
396 t->verdict < 0 && unconditional(&e->arp)) ||
397 visited) {
398 unsigned int oldpos, size;
400 if ((strcmp(t->target.u.user.name,
401 XT_STANDARD_TARGET) == 0) &&
402 t->verdict < -NF_MAX_VERDICT - 1) {
403 duprintf("mark_source_chains: bad "
404 "negative verdict (%i)\n",
405 t->verdict);
406 return 0;
409 /* Return: backtrack through the last
410 * big jump.
412 do {
413 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
414 oldpos = pos;
415 pos = e->counters.pcnt;
416 e->counters.pcnt = 0;
418 /* We're at the start. */
419 if (pos == oldpos)
420 goto next;
422 e = (struct arpt_entry *)
423 (entry0 + pos);
424 } while (oldpos == pos + e->next_offset);
426 /* Move along one */
427 size = e->next_offset;
428 e = (struct arpt_entry *)
429 (entry0 + pos + size);
430 e->counters.pcnt = pos;
431 pos += size;
432 } else {
433 int newpos = t->verdict;
435 if (strcmp(t->target.u.user.name,
436 XT_STANDARD_TARGET) == 0 &&
437 newpos >= 0) {
438 if (newpos > newinfo->size -
439 sizeof(struct arpt_entry)) {
440 duprintf("mark_source_chains: "
441 "bad verdict (%i)\n",
442 newpos);
443 return 0;
446 /* This a jump; chase it. */
447 duprintf("Jump rule %u -> %u\n",
448 pos, newpos);
449 } else {
450 /* ... this is a fallthru */
451 newpos = pos + e->next_offset;
453 e = (struct arpt_entry *)
454 (entry0 + newpos);
455 e->counters.pcnt = pos;
456 pos = newpos;
459 next:
460 duprintf("Finished chain %u\n", hook);
462 return 1;
465 static inline int check_entry(const struct arpt_entry *e, const char *name)
467 const struct xt_entry_target *t;
469 if (!arp_checkentry(&e->arp)) {
470 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
471 return -EINVAL;
474 if (e->target_offset + sizeof(struct xt_entry_target) > e->next_offset)
475 return -EINVAL;
477 t = arpt_get_target_c(e);
478 if (e->target_offset + t->u.target_size > e->next_offset)
479 return -EINVAL;
481 return 0;
484 static inline int check_target(struct arpt_entry *e, const char *name)
486 struct xt_entry_target *t = arpt_get_target(e);
487 int ret;
488 struct xt_tgchk_param par = {
489 .table = name,
490 .entryinfo = e,
491 .target = t->u.kernel.target,
492 .targinfo = t->data,
493 .hook_mask = e->comefrom,
494 .family = NFPROTO_ARP,
497 ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
498 if (ret < 0) {
499 duprintf("arp_tables: check failed for `%s'.\n",
500 t->u.kernel.target->name);
501 return ret;
503 return 0;
506 static inline int
507 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
509 struct xt_entry_target *t;
510 struct xt_target *target;
511 int ret;
513 ret = check_entry(e, name);
514 if (ret)
515 return ret;
517 t = arpt_get_target(e);
518 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
519 t->u.user.revision);
520 if (IS_ERR(target)) {
521 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
522 ret = PTR_ERR(target);
523 goto out;
525 t->u.kernel.target = target;
527 ret = check_target(e, name);
528 if (ret)
529 goto err;
530 return 0;
531 err:
532 module_put(t->u.kernel.target->me);
533 out:
534 return ret;
537 static bool check_underflow(const struct arpt_entry *e)
539 const struct xt_entry_target *t;
540 unsigned int verdict;
542 if (!unconditional(&e->arp))
543 return false;
544 t = arpt_get_target_c(e);
545 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
546 return false;
547 verdict = ((struct xt_standard_target *)t)->verdict;
548 verdict = -verdict - 1;
549 return verdict == NF_DROP || verdict == NF_ACCEPT;
552 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
553 struct xt_table_info *newinfo,
554 const unsigned char *base,
555 const unsigned char *limit,
556 const unsigned int *hook_entries,
557 const unsigned int *underflows,
558 unsigned int valid_hooks)
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 xt_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;
595 return 0;
598 static inline void cleanup_entry(struct arpt_entry *e)
600 struct xt_tgdtor_param par;
601 struct xt_entry_target *t;
603 t = arpt_get_target(e);
604 par.target = t->u.kernel.target;
605 par.targinfo = t->data;
606 par.family = NFPROTO_ARP;
607 if (par.target->destroy != NULL)
608 par.target->destroy(&par);
609 module_put(par.target->me);
612 /* Checks and translates the user-supplied table segment (held in
613 * newinfo).
615 static int translate_table(struct xt_table_info *newinfo, void *entry0,
616 const struct arpt_replace *repl)
618 struct arpt_entry *iter;
619 unsigned int i;
620 int ret = 0;
622 newinfo->size = repl->size;
623 newinfo->number = repl->num_entries;
625 /* Init all hooks to impossible value. */
626 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
627 newinfo->hook_entry[i] = 0xFFFFFFFF;
628 newinfo->underflow[i] = 0xFFFFFFFF;
631 duprintf("translate_table: size %u\n", newinfo->size);
632 i = 0;
634 /* Walk through entries, checking offsets. */
635 xt_entry_foreach(iter, entry0, newinfo->size) {
636 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
637 entry0 + repl->size,
638 repl->hook_entry,
639 repl->underflow,
640 repl->valid_hooks);
641 if (ret != 0)
642 break;
643 ++i;
644 if (strcmp(arpt_get_target(iter)->u.user.name,
645 XT_ERROR_TARGET) == 0)
646 ++newinfo->stacksize;
648 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
649 if (ret != 0)
650 return ret;
652 if (i != repl->num_entries) {
653 duprintf("translate_table: %u not %u entries\n",
654 i, repl->num_entries);
655 return -EINVAL;
658 /* Check hooks all assigned */
659 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
660 /* Only hooks which are valid */
661 if (!(repl->valid_hooks & (1 << i)))
662 continue;
663 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
664 duprintf("Invalid hook entry %u %u\n",
665 i, repl->hook_entry[i]);
666 return -EINVAL;
668 if (newinfo->underflow[i] == 0xFFFFFFFF) {
669 duprintf("Invalid underflow %u %u\n",
670 i, repl->underflow[i]);
671 return -EINVAL;
675 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0)) {
676 duprintf("Looping hook\n");
677 return -ELOOP;
680 /* Finally, each sanity check must pass */
681 i = 0;
682 xt_entry_foreach(iter, entry0, newinfo->size) {
683 ret = find_check_entry(iter, repl->name, repl->size);
684 if (ret != 0)
685 break;
686 ++i;
689 if (ret != 0) {
690 xt_entry_foreach(iter, entry0, newinfo->size) {
691 if (i-- == 0)
692 break;
693 cleanup_entry(iter);
695 return ret;
698 /* And one copy for every other CPU */
699 for_each_possible_cpu(i) {
700 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
701 memcpy(newinfo->entries[i], entry0, newinfo->size);
704 return ret;
707 static void get_counters(const struct xt_table_info *t,
708 struct xt_counters counters[])
710 struct arpt_entry *iter;
711 unsigned int cpu;
712 unsigned int i;
713 unsigned int curcpu = get_cpu();
715 /* Instead of clearing (by a previous call to memset())
716 * the counters and using adds, we set the counters
717 * with data used by 'current' CPU
719 * Bottom half has to be disabled to prevent deadlock
720 * if new softirq were to run and call ipt_do_table
722 local_bh_disable();
723 i = 0;
724 xt_entry_foreach(iter, t->entries[curcpu], t->size) {
725 SET_COUNTER(counters[i], iter->counters.bcnt,
726 iter->counters.pcnt);
727 ++i;
729 local_bh_enable();
730 /* Processing counters from other cpus, we can let bottom half enabled,
731 * (preemption is disabled)
734 for_each_possible_cpu(cpu) {
735 if (cpu == curcpu)
736 continue;
737 i = 0;
738 local_bh_disable();
739 xt_info_wrlock(cpu);
740 xt_entry_foreach(iter, t->entries[cpu], t->size) {
741 ADD_COUNTER(counters[i], iter->counters.bcnt,
742 iter->counters.pcnt);
743 ++i;
745 xt_info_wrunlock(cpu);
746 local_bh_enable();
748 put_cpu();
751 static struct xt_counters *alloc_counters(const struct xt_table *table)
753 unsigned int countersize;
754 struct xt_counters *counters;
755 const struct xt_table_info *private = table->private;
757 /* We need atomic snapshot of counters: rest doesn't change
758 * (other than comefrom, which userspace doesn't care
759 * about).
761 countersize = sizeof(struct xt_counters) * private->number;
762 counters = vmalloc(countersize);
764 if (counters == NULL)
765 return ERR_PTR(-ENOMEM);
767 get_counters(private, counters);
769 return counters;
772 static int copy_entries_to_user(unsigned int total_size,
773 const struct xt_table *table,
774 void __user *userptr)
776 unsigned int off, num;
777 const struct arpt_entry *e;
778 struct xt_counters *counters;
779 struct xt_table_info *private = table->private;
780 int ret = 0;
781 void *loc_cpu_entry;
783 counters = alloc_counters(table);
784 if (IS_ERR(counters))
785 return PTR_ERR(counters);
787 loc_cpu_entry = private->entries[raw_smp_processor_id()];
788 /* ... then copy entire thing ... */
789 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
790 ret = -EFAULT;
791 goto free_counters;
794 /* FIXME: use iterator macros --RR */
795 /* ... then go back and fix counters and names */
796 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
797 const struct xt_entry_target *t;
799 e = (struct arpt_entry *)(loc_cpu_entry + off);
800 if (copy_to_user(userptr + off
801 + offsetof(struct arpt_entry, counters),
802 &counters[num],
803 sizeof(counters[num])) != 0) {
804 ret = -EFAULT;
805 goto free_counters;
808 t = arpt_get_target_c(e);
809 if (copy_to_user(userptr + off + e->target_offset
810 + offsetof(struct xt_entry_target,
811 u.user.name),
812 t->u.kernel.target->name,
813 strlen(t->u.kernel.target->name)+1) != 0) {
814 ret = -EFAULT;
815 goto free_counters;
819 free_counters:
820 vfree(counters);
821 return ret;
824 #ifdef CONFIG_COMPAT
825 static void compat_standard_from_user(void *dst, const void *src)
827 int v = *(compat_int_t *)src;
829 if (v > 0)
830 v += xt_compat_calc_jump(NFPROTO_ARP, v);
831 memcpy(dst, &v, sizeof(v));
834 static int compat_standard_to_user(void __user *dst, const void *src)
836 compat_int_t cv = *(int *)src;
838 if (cv > 0)
839 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
840 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
843 static int compat_calc_entry(const struct arpt_entry *e,
844 const struct xt_table_info *info,
845 const void *base, struct xt_table_info *newinfo)
847 const struct xt_entry_target *t;
848 unsigned int entry_offset;
849 int off, i, ret;
851 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
852 entry_offset = (void *)e - base;
854 t = arpt_get_target_c(e);
855 off += xt_compat_target_offset(t->u.kernel.target);
856 newinfo->size -= off;
857 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
858 if (ret)
859 return ret;
861 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
862 if (info->hook_entry[i] &&
863 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
864 newinfo->hook_entry[i] -= off;
865 if (info->underflow[i] &&
866 (e < (struct arpt_entry *)(base + info->underflow[i])))
867 newinfo->underflow[i] -= off;
869 return 0;
872 static int compat_table_info(const struct xt_table_info *info,
873 struct xt_table_info *newinfo)
875 struct arpt_entry *iter;
876 void *loc_cpu_entry;
877 int ret;
879 if (!newinfo || !info)
880 return -EINVAL;
882 /* we dont care about newinfo->entries[] */
883 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
884 newinfo->initial_entries = 0;
885 loc_cpu_entry = info->entries[raw_smp_processor_id()];
886 xt_compat_init_offsets(NFPROTO_ARP, info->number);
887 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
888 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
889 if (ret != 0)
890 return ret;
892 return 0;
894 #endif
896 static int get_info(struct net *net, void __user *user,
897 const int *len, int compat)
899 char name[XT_TABLE_MAXNAMELEN];
900 struct xt_table *t;
901 int ret;
903 if (*len != sizeof(struct arpt_getinfo)) {
904 duprintf("length %u != %Zu\n", *len,
905 sizeof(struct arpt_getinfo));
906 return -EINVAL;
909 if (copy_from_user(name, user, sizeof(name)) != 0)
910 return -EFAULT;
912 name[XT_TABLE_MAXNAMELEN-1] = '\0';
913 #ifdef CONFIG_COMPAT
914 if (compat)
915 xt_compat_lock(NFPROTO_ARP);
916 #endif
917 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
918 "arptable_%s", name);
919 if (t && !IS_ERR(t)) {
920 struct arpt_getinfo info;
921 const struct xt_table_info *private = t->private;
922 #ifdef CONFIG_COMPAT
923 struct xt_table_info tmp;
925 if (compat) {
926 ret = compat_table_info(private, &tmp);
927 xt_compat_flush_offsets(NFPROTO_ARP);
928 private = &tmp;
930 #endif
931 memset(&info, 0, sizeof(info));
932 info.valid_hooks = t->valid_hooks;
933 memcpy(info.hook_entry, private->hook_entry,
934 sizeof(info.hook_entry));
935 memcpy(info.underflow, private->underflow,
936 sizeof(info.underflow));
937 info.num_entries = private->number;
938 info.size = private->size;
939 strcpy(info.name, name);
941 if (copy_to_user(user, &info, *len) != 0)
942 ret = -EFAULT;
943 else
944 ret = 0;
945 xt_table_unlock(t);
946 module_put(t->me);
947 } else
948 ret = t ? PTR_ERR(t) : -ENOENT;
949 #ifdef CONFIG_COMPAT
950 if (compat)
951 xt_compat_unlock(NFPROTO_ARP);
952 #endif
953 return ret;
956 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
957 const int *len)
959 int ret;
960 struct arpt_get_entries get;
961 struct xt_table *t;
963 if (*len < sizeof(get)) {
964 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
965 return -EINVAL;
967 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
968 return -EFAULT;
969 if (*len != sizeof(struct arpt_get_entries) + get.size) {
970 duprintf("get_entries: %u != %Zu\n", *len,
971 sizeof(struct arpt_get_entries) + get.size);
972 return -EINVAL;
975 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
976 if (t && !IS_ERR(t)) {
977 const struct xt_table_info *private = t->private;
979 duprintf("t->private->number = %u\n",
980 private->number);
981 if (get.size == private->size)
982 ret = copy_entries_to_user(private->size,
983 t, uptr->entrytable);
984 else {
985 duprintf("get_entries: I've got %u not %u!\n",
986 private->size, get.size);
987 ret = -EAGAIN;
989 module_put(t->me);
990 xt_table_unlock(t);
991 } else
992 ret = t ? PTR_ERR(t) : -ENOENT;
994 return ret;
997 static int __do_replace(struct net *net, const char *name,
998 unsigned int valid_hooks,
999 struct xt_table_info *newinfo,
1000 unsigned int num_counters,
1001 void __user *counters_ptr)
1003 int ret;
1004 struct xt_table *t;
1005 struct xt_table_info *oldinfo;
1006 struct xt_counters *counters;
1007 void *loc_cpu_old_entry;
1008 struct arpt_entry *iter;
1010 ret = 0;
1011 counters = vmalloc(num_counters * sizeof(struct xt_counters));
1012 if (!counters) {
1013 ret = -ENOMEM;
1014 goto out;
1017 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
1018 "arptable_%s", name);
1019 if (!t || IS_ERR(t)) {
1020 ret = t ? PTR_ERR(t) : -ENOENT;
1021 goto free_newinfo_counters_untrans;
1024 /* You lied! */
1025 if (valid_hooks != t->valid_hooks) {
1026 duprintf("Valid hook crap: %08X vs %08X\n",
1027 valid_hooks, t->valid_hooks);
1028 ret = -EINVAL;
1029 goto put_module;
1032 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1033 if (!oldinfo)
1034 goto put_module;
1036 /* Update module usage count based on number of rules */
1037 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1038 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1039 if ((oldinfo->number > oldinfo->initial_entries) ||
1040 (newinfo->number <= oldinfo->initial_entries))
1041 module_put(t->me);
1042 if ((oldinfo->number > oldinfo->initial_entries) &&
1043 (newinfo->number <= oldinfo->initial_entries))
1044 module_put(t->me);
1046 /* Get the old counters, and synchronize with replace */
1047 get_counters(oldinfo, counters);
1049 /* Decrease module usage counts and free resource */
1050 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1051 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
1052 cleanup_entry(iter);
1054 xt_free_table_info(oldinfo);
1055 if (copy_to_user(counters_ptr, counters,
1056 sizeof(struct xt_counters) * num_counters) != 0)
1057 ret = -EFAULT;
1058 vfree(counters);
1059 xt_table_unlock(t);
1060 return ret;
1062 put_module:
1063 module_put(t->me);
1064 xt_table_unlock(t);
1065 free_newinfo_counters_untrans:
1066 vfree(counters);
1067 out:
1068 return ret;
1071 static int do_replace(struct net *net, const void __user *user,
1072 unsigned int len)
1074 int ret;
1075 struct arpt_replace tmp;
1076 struct xt_table_info *newinfo;
1077 void *loc_cpu_entry;
1078 struct arpt_entry *iter;
1080 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1081 return -EFAULT;
1083 /* overflow check */
1084 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1085 return -ENOMEM;
1087 newinfo = xt_alloc_table_info(tmp.size);
1088 if (!newinfo)
1089 return -ENOMEM;
1091 /* choose the copy that is on our node/cpu */
1092 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1093 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1094 tmp.size) != 0) {
1095 ret = -EFAULT;
1096 goto free_newinfo;
1099 ret = translate_table(newinfo, loc_cpu_entry, &tmp);
1100 if (ret != 0)
1101 goto free_newinfo;
1103 duprintf("arp_tables: Translated table\n");
1105 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1106 tmp.num_counters, tmp.counters);
1107 if (ret)
1108 goto free_newinfo_untrans;
1109 return 0;
1111 free_newinfo_untrans:
1112 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1113 cleanup_entry(iter);
1114 free_newinfo:
1115 xt_free_table_info(newinfo);
1116 return ret;
1119 static int do_add_counters(struct net *net, const void __user *user,
1120 unsigned int len, int compat)
1122 unsigned int i, curcpu;
1123 struct xt_counters_info tmp;
1124 struct xt_counters *paddc;
1125 unsigned int num_counters;
1126 const char *name;
1127 int size;
1128 void *ptmp;
1129 struct xt_table *t;
1130 const struct xt_table_info *private;
1131 int ret = 0;
1132 void *loc_cpu_entry;
1133 struct arpt_entry *iter;
1134 #ifdef CONFIG_COMPAT
1135 struct compat_xt_counters_info compat_tmp;
1137 if (compat) {
1138 ptmp = &compat_tmp;
1139 size = sizeof(struct compat_xt_counters_info);
1140 } else
1141 #endif
1143 ptmp = &tmp;
1144 size = sizeof(struct xt_counters_info);
1147 if (copy_from_user(ptmp, user, size) != 0)
1148 return -EFAULT;
1150 #ifdef CONFIG_COMPAT
1151 if (compat) {
1152 num_counters = compat_tmp.num_counters;
1153 name = compat_tmp.name;
1154 } else
1155 #endif
1157 num_counters = tmp.num_counters;
1158 name = tmp.name;
1161 if (len != size + num_counters * sizeof(struct xt_counters))
1162 return -EINVAL;
1164 paddc = vmalloc(len - size);
1165 if (!paddc)
1166 return -ENOMEM;
1168 if (copy_from_user(paddc, user + size, len - size) != 0) {
1169 ret = -EFAULT;
1170 goto free;
1173 t = xt_find_table_lock(net, NFPROTO_ARP, name);
1174 if (!t || IS_ERR(t)) {
1175 ret = t ? PTR_ERR(t) : -ENOENT;
1176 goto free;
1179 local_bh_disable();
1180 private = t->private;
1181 if (private->number != num_counters) {
1182 ret = -EINVAL;
1183 goto unlock_up_free;
1186 i = 0;
1187 /* Choose the copy that is on our node */
1188 curcpu = smp_processor_id();
1189 loc_cpu_entry = private->entries[curcpu];
1190 xt_info_wrlock(curcpu);
1191 xt_entry_foreach(iter, loc_cpu_entry, private->size) {
1192 ADD_COUNTER(iter->counters, paddc[i].bcnt, paddc[i].pcnt);
1193 ++i;
1195 xt_info_wrunlock(curcpu);
1196 unlock_up_free:
1197 local_bh_enable();
1198 xt_table_unlock(t);
1199 module_put(t->me);
1200 free:
1201 vfree(paddc);
1203 return ret;
1206 #ifdef CONFIG_COMPAT
1207 static inline void compat_release_entry(struct compat_arpt_entry *e)
1209 struct xt_entry_target *t;
1211 t = compat_arpt_get_target(e);
1212 module_put(t->u.kernel.target->me);
1215 static inline int
1216 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1217 struct xt_table_info *newinfo,
1218 unsigned int *size,
1219 const unsigned char *base,
1220 const unsigned char *limit,
1221 const unsigned int *hook_entries,
1222 const unsigned int *underflows,
1223 const char *name)
1225 struct xt_entry_target *t;
1226 struct xt_target *target;
1227 unsigned int entry_offset;
1228 int ret, off, h;
1230 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1231 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1232 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
1233 duprintf("Bad offset %p, limit = %p\n", e, limit);
1234 return -EINVAL;
1237 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1238 sizeof(struct compat_xt_entry_target)) {
1239 duprintf("checking: element %p size %u\n",
1240 e, e->next_offset);
1241 return -EINVAL;
1244 /* For purposes of check_entry casting the compat entry is fine */
1245 ret = check_entry((struct arpt_entry *)e, name);
1246 if (ret)
1247 return ret;
1249 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1250 entry_offset = (void *)e - (void *)base;
1252 t = compat_arpt_get_target(e);
1253 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1254 t->u.user.revision);
1255 if (IS_ERR(target)) {
1256 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1257 t->u.user.name);
1258 ret = PTR_ERR(target);
1259 goto out;
1261 t->u.kernel.target = target;
1263 off += xt_compat_target_offset(target);
1264 *size += off;
1265 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1266 if (ret)
1267 goto release_target;
1269 /* Check hooks & underflows */
1270 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1271 if ((unsigned char *)e - base == hook_entries[h])
1272 newinfo->hook_entry[h] = hook_entries[h];
1273 if ((unsigned char *)e - base == underflows[h])
1274 newinfo->underflow[h] = underflows[h];
1277 /* Clear counters and comefrom */
1278 memset(&e->counters, 0, sizeof(e->counters));
1279 e->comefrom = 0;
1280 return 0;
1282 release_target:
1283 module_put(t->u.kernel.target->me);
1284 out:
1285 return ret;
1288 static int
1289 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1290 unsigned int *size, const char *name,
1291 struct xt_table_info *newinfo, unsigned char *base)
1293 struct xt_entry_target *t;
1294 struct xt_target *target;
1295 struct arpt_entry *de;
1296 unsigned int origsize;
1297 int ret, h;
1299 ret = 0;
1300 origsize = *size;
1301 de = (struct arpt_entry *)*dstptr;
1302 memcpy(de, e, sizeof(struct arpt_entry));
1303 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1305 *dstptr += sizeof(struct arpt_entry);
1306 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1308 de->target_offset = e->target_offset - (origsize - *size);
1309 t = compat_arpt_get_target(e);
1310 target = t->u.kernel.target;
1311 xt_compat_target_from_user(t, dstptr, size);
1313 de->next_offset = e->next_offset - (origsize - *size);
1314 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1315 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1316 newinfo->hook_entry[h] -= origsize - *size;
1317 if ((unsigned char *)de - base < newinfo->underflow[h])
1318 newinfo->underflow[h] -= origsize - *size;
1320 return ret;
1323 static int translate_compat_table(const char *name,
1324 unsigned int valid_hooks,
1325 struct xt_table_info **pinfo,
1326 void **pentry0,
1327 unsigned int total_size,
1328 unsigned int number,
1329 unsigned int *hook_entries,
1330 unsigned int *underflows)
1332 unsigned int i, j;
1333 struct xt_table_info *newinfo, *info;
1334 void *pos, *entry0, *entry1;
1335 struct compat_arpt_entry *iter0;
1336 struct arpt_entry *iter1;
1337 unsigned int size;
1338 int ret = 0;
1340 info = *pinfo;
1341 entry0 = *pentry0;
1342 size = total_size;
1343 info->number = number;
1345 /* Init all hooks to impossible value. */
1346 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1347 info->hook_entry[i] = 0xFFFFFFFF;
1348 info->underflow[i] = 0xFFFFFFFF;
1351 duprintf("translate_compat_table: size %u\n", info->size);
1352 j = 0;
1353 xt_compat_lock(NFPROTO_ARP);
1354 xt_compat_init_offsets(NFPROTO_ARP, number);
1355 /* Walk through entries, checking offsets. */
1356 xt_entry_foreach(iter0, entry0, total_size) {
1357 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1358 entry0,
1359 entry0 + total_size,
1360 hook_entries,
1361 underflows,
1362 name);
1363 if (ret != 0)
1364 goto out_unlock;
1365 ++j;
1368 ret = -EINVAL;
1369 if (j != number) {
1370 duprintf("translate_compat_table: %u not %u entries\n",
1371 j, number);
1372 goto out_unlock;
1375 /* Check hooks all assigned */
1376 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1377 /* Only hooks which are valid */
1378 if (!(valid_hooks & (1 << i)))
1379 continue;
1380 if (info->hook_entry[i] == 0xFFFFFFFF) {
1381 duprintf("Invalid hook entry %u %u\n",
1382 i, hook_entries[i]);
1383 goto out_unlock;
1385 if (info->underflow[i] == 0xFFFFFFFF) {
1386 duprintf("Invalid underflow %u %u\n",
1387 i, underflows[i]);
1388 goto out_unlock;
1392 ret = -ENOMEM;
1393 newinfo = xt_alloc_table_info(size);
1394 if (!newinfo)
1395 goto out_unlock;
1397 newinfo->number = number;
1398 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1399 newinfo->hook_entry[i] = info->hook_entry[i];
1400 newinfo->underflow[i] = info->underflow[i];
1402 entry1 = newinfo->entries[raw_smp_processor_id()];
1403 pos = entry1;
1404 size = total_size;
1405 xt_entry_foreach(iter0, entry0, total_size) {
1406 ret = compat_copy_entry_from_user(iter0, &pos, &size,
1407 name, newinfo, entry1);
1408 if (ret != 0)
1409 break;
1411 xt_compat_flush_offsets(NFPROTO_ARP);
1412 xt_compat_unlock(NFPROTO_ARP);
1413 if (ret)
1414 goto free_newinfo;
1416 ret = -ELOOP;
1417 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1418 goto free_newinfo;
1420 i = 0;
1421 xt_entry_foreach(iter1, entry1, newinfo->size) {
1422 ret = check_target(iter1, name);
1423 if (ret != 0)
1424 break;
1425 ++i;
1426 if (strcmp(arpt_get_target(iter1)->u.user.name,
1427 XT_ERROR_TARGET) == 0)
1428 ++newinfo->stacksize;
1430 if (ret) {
1432 * The first i matches need cleanup_entry (calls ->destroy)
1433 * because they had called ->check already. The other j-i
1434 * entries need only release.
1436 int skip = i;
1437 j -= i;
1438 xt_entry_foreach(iter0, entry0, newinfo->size) {
1439 if (skip-- > 0)
1440 continue;
1441 if (j-- == 0)
1442 break;
1443 compat_release_entry(iter0);
1445 xt_entry_foreach(iter1, entry1, newinfo->size) {
1446 if (i-- == 0)
1447 break;
1448 cleanup_entry(iter1);
1450 xt_free_table_info(newinfo);
1451 return ret;
1454 /* And one copy for every other CPU */
1455 for_each_possible_cpu(i)
1456 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1457 memcpy(newinfo->entries[i], entry1, newinfo->size);
1459 *pinfo = newinfo;
1460 *pentry0 = entry1;
1461 xt_free_table_info(info);
1462 return 0;
1464 free_newinfo:
1465 xt_free_table_info(newinfo);
1466 out:
1467 xt_entry_foreach(iter0, entry0, total_size) {
1468 if (j-- == 0)
1469 break;
1470 compat_release_entry(iter0);
1472 return ret;
1473 out_unlock:
1474 xt_compat_flush_offsets(NFPROTO_ARP);
1475 xt_compat_unlock(NFPROTO_ARP);
1476 goto out;
1479 struct compat_arpt_replace {
1480 char name[XT_TABLE_MAXNAMELEN];
1481 u32 valid_hooks;
1482 u32 num_entries;
1483 u32 size;
1484 u32 hook_entry[NF_ARP_NUMHOOKS];
1485 u32 underflow[NF_ARP_NUMHOOKS];
1486 u32 num_counters;
1487 compat_uptr_t counters;
1488 struct compat_arpt_entry entries[0];
1491 static int compat_do_replace(struct net *net, void __user *user,
1492 unsigned int len)
1494 int ret;
1495 struct compat_arpt_replace tmp;
1496 struct xt_table_info *newinfo;
1497 void *loc_cpu_entry;
1498 struct arpt_entry *iter;
1500 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1501 return -EFAULT;
1503 /* overflow check */
1504 if (tmp.size >= INT_MAX / num_possible_cpus())
1505 return -ENOMEM;
1506 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1507 return -ENOMEM;
1509 newinfo = xt_alloc_table_info(tmp.size);
1510 if (!newinfo)
1511 return -ENOMEM;
1513 /* choose the copy that is on our node/cpu */
1514 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1515 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1516 ret = -EFAULT;
1517 goto free_newinfo;
1520 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1521 &newinfo, &loc_cpu_entry, tmp.size,
1522 tmp.num_entries, tmp.hook_entry,
1523 tmp.underflow);
1524 if (ret != 0)
1525 goto free_newinfo;
1527 duprintf("compat_do_replace: Translated table\n");
1529 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1530 tmp.num_counters, compat_ptr(tmp.counters));
1531 if (ret)
1532 goto free_newinfo_untrans;
1533 return 0;
1535 free_newinfo_untrans:
1536 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1537 cleanup_entry(iter);
1538 free_newinfo:
1539 xt_free_table_info(newinfo);
1540 return ret;
1543 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1544 unsigned int len)
1546 int ret;
1548 if (!capable(CAP_NET_ADMIN))
1549 return -EPERM;
1551 switch (cmd) {
1552 case ARPT_SO_SET_REPLACE:
1553 ret = compat_do_replace(sock_net(sk), user, len);
1554 break;
1556 case ARPT_SO_SET_ADD_COUNTERS:
1557 ret = do_add_counters(sock_net(sk), user, len, 1);
1558 break;
1560 default:
1561 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1562 ret = -EINVAL;
1565 return ret;
1568 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1569 compat_uint_t *size,
1570 struct xt_counters *counters,
1571 unsigned int i)
1573 struct xt_entry_target *t;
1574 struct compat_arpt_entry __user *ce;
1575 u_int16_t target_offset, next_offset;
1576 compat_uint_t origsize;
1577 int ret;
1579 origsize = *size;
1580 ce = (struct compat_arpt_entry __user *)*dstptr;
1581 if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1582 copy_to_user(&ce->counters, &counters[i],
1583 sizeof(counters[i])) != 0)
1584 return -EFAULT;
1586 *dstptr += sizeof(struct compat_arpt_entry);
1587 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1589 target_offset = e->target_offset - (origsize - *size);
1591 t = arpt_get_target(e);
1592 ret = xt_compat_target_to_user(t, dstptr, size);
1593 if (ret)
1594 return ret;
1595 next_offset = e->next_offset - (origsize - *size);
1596 if (put_user(target_offset, &ce->target_offset) != 0 ||
1597 put_user(next_offset, &ce->next_offset) != 0)
1598 return -EFAULT;
1599 return 0;
1602 static int compat_copy_entries_to_user(unsigned int total_size,
1603 struct xt_table *table,
1604 void __user *userptr)
1606 struct xt_counters *counters;
1607 const struct xt_table_info *private = table->private;
1608 void __user *pos;
1609 unsigned int size;
1610 int ret = 0;
1611 void *loc_cpu_entry;
1612 unsigned int i = 0;
1613 struct arpt_entry *iter;
1615 counters = alloc_counters(table);
1616 if (IS_ERR(counters))
1617 return PTR_ERR(counters);
1619 /* choose the copy on our node/cpu */
1620 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1621 pos = userptr;
1622 size = total_size;
1623 xt_entry_foreach(iter, loc_cpu_entry, total_size) {
1624 ret = compat_copy_entry_to_user(iter, &pos,
1625 &size, counters, i++);
1626 if (ret != 0)
1627 break;
1629 vfree(counters);
1630 return ret;
1633 struct compat_arpt_get_entries {
1634 char name[XT_TABLE_MAXNAMELEN];
1635 compat_uint_t size;
1636 struct compat_arpt_entry entrytable[0];
1639 static int compat_get_entries(struct net *net,
1640 struct compat_arpt_get_entries __user *uptr,
1641 int *len)
1643 int ret;
1644 struct compat_arpt_get_entries get;
1645 struct xt_table *t;
1647 if (*len < sizeof(get)) {
1648 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1649 return -EINVAL;
1651 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1652 return -EFAULT;
1653 if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1654 duprintf("compat_get_entries: %u != %zu\n",
1655 *len, sizeof(get) + get.size);
1656 return -EINVAL;
1659 xt_compat_lock(NFPROTO_ARP);
1660 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1661 if (t && !IS_ERR(t)) {
1662 const struct xt_table_info *private = t->private;
1663 struct xt_table_info info;
1665 duprintf("t->private->number = %u\n", private->number);
1666 ret = compat_table_info(private, &info);
1667 if (!ret && get.size == info.size) {
1668 ret = compat_copy_entries_to_user(private->size,
1669 t, uptr->entrytable);
1670 } else if (!ret) {
1671 duprintf("compat_get_entries: I've got %u not %u!\n",
1672 private->size, get.size);
1673 ret = -EAGAIN;
1675 xt_compat_flush_offsets(NFPROTO_ARP);
1676 module_put(t->me);
1677 xt_table_unlock(t);
1678 } else
1679 ret = t ? PTR_ERR(t) : -ENOENT;
1681 xt_compat_unlock(NFPROTO_ARP);
1682 return ret;
1685 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1687 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1688 int *len)
1690 int ret;
1692 if (!capable(CAP_NET_ADMIN))
1693 return -EPERM;
1695 switch (cmd) {
1696 case ARPT_SO_GET_INFO:
1697 ret = get_info(sock_net(sk), user, len, 1);
1698 break;
1699 case ARPT_SO_GET_ENTRIES:
1700 ret = compat_get_entries(sock_net(sk), user, len);
1701 break;
1702 default:
1703 ret = do_arpt_get_ctl(sk, cmd, user, len);
1705 return ret;
1707 #endif
1709 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1711 int ret;
1713 if (!capable(CAP_NET_ADMIN))
1714 return -EPERM;
1716 switch (cmd) {
1717 case ARPT_SO_SET_REPLACE:
1718 ret = do_replace(sock_net(sk), user, len);
1719 break;
1721 case ARPT_SO_SET_ADD_COUNTERS:
1722 ret = do_add_counters(sock_net(sk), user, len, 0);
1723 break;
1725 default:
1726 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1727 ret = -EINVAL;
1730 return ret;
1733 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1735 int ret;
1737 if (!capable(CAP_NET_ADMIN))
1738 return -EPERM;
1740 switch (cmd) {
1741 case ARPT_SO_GET_INFO:
1742 ret = get_info(sock_net(sk), user, len, 0);
1743 break;
1745 case ARPT_SO_GET_ENTRIES:
1746 ret = get_entries(sock_net(sk), user, len);
1747 break;
1749 case ARPT_SO_GET_REVISION_TARGET: {
1750 struct xt_get_revision rev;
1752 if (*len != sizeof(rev)) {
1753 ret = -EINVAL;
1754 break;
1756 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1757 ret = -EFAULT;
1758 break;
1761 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1762 rev.revision, 1, &ret),
1763 "arpt_%s", rev.name);
1764 break;
1767 default:
1768 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1769 ret = -EINVAL;
1772 return ret;
1775 struct xt_table *arpt_register_table(struct net *net,
1776 const struct xt_table *table,
1777 const struct arpt_replace *repl)
1779 int ret;
1780 struct xt_table_info *newinfo;
1781 struct xt_table_info bootstrap = {0};
1782 void *loc_cpu_entry;
1783 struct xt_table *new_table;
1785 newinfo = xt_alloc_table_info(repl->size);
1786 if (!newinfo) {
1787 ret = -ENOMEM;
1788 goto out;
1791 /* choose the copy on our node/cpu */
1792 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1793 memcpy(loc_cpu_entry, repl->entries, repl->size);
1795 ret = translate_table(newinfo, loc_cpu_entry, repl);
1796 duprintf("arpt_register_table: translate table gives %d\n", ret);
1797 if (ret != 0)
1798 goto out_free;
1800 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1801 if (IS_ERR(new_table)) {
1802 ret = PTR_ERR(new_table);
1803 goto out_free;
1805 return new_table;
1807 out_free:
1808 xt_free_table_info(newinfo);
1809 out:
1810 return ERR_PTR(ret);
1813 void arpt_unregister_table(struct xt_table *table)
1815 struct xt_table_info *private;
1816 void *loc_cpu_entry;
1817 struct module *table_owner = table->me;
1818 struct arpt_entry *iter;
1820 private = xt_unregister_table(table);
1822 /* Decrease module usage counts and free resources */
1823 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1824 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1825 cleanup_entry(iter);
1826 if (private->number > private->initial_entries)
1827 module_put(table_owner);
1828 xt_free_table_info(private);
1831 /* The built-in targets: standard (NULL) and error. */
1832 static struct xt_target arpt_builtin_tg[] __read_mostly = {
1834 .name = XT_STANDARD_TARGET,
1835 .targetsize = sizeof(int),
1836 .family = NFPROTO_ARP,
1837 #ifdef CONFIG_COMPAT
1838 .compatsize = sizeof(compat_int_t),
1839 .compat_from_user = compat_standard_from_user,
1840 .compat_to_user = compat_standard_to_user,
1841 #endif
1844 .name = XT_ERROR_TARGET,
1845 .target = arpt_error,
1846 .targetsize = XT_FUNCTION_MAXNAMELEN,
1847 .family = NFPROTO_ARP,
1851 static struct nf_sockopt_ops arpt_sockopts = {
1852 .pf = PF_INET,
1853 .set_optmin = ARPT_BASE_CTL,
1854 .set_optmax = ARPT_SO_SET_MAX+1,
1855 .set = do_arpt_set_ctl,
1856 #ifdef CONFIG_COMPAT
1857 .compat_set = compat_do_arpt_set_ctl,
1858 #endif
1859 .get_optmin = ARPT_BASE_CTL,
1860 .get_optmax = ARPT_SO_GET_MAX+1,
1861 .get = do_arpt_get_ctl,
1862 #ifdef CONFIG_COMPAT
1863 .compat_get = compat_do_arpt_get_ctl,
1864 #endif
1865 .owner = THIS_MODULE,
1868 static int __net_init arp_tables_net_init(struct net *net)
1870 return xt_proto_init(net, NFPROTO_ARP);
1873 static void __net_exit arp_tables_net_exit(struct net *net)
1875 xt_proto_fini(net, NFPROTO_ARP);
1878 static struct pernet_operations arp_tables_net_ops = {
1879 .init = arp_tables_net_init,
1880 .exit = arp_tables_net_exit,
1883 static int __init arp_tables_init(void)
1885 int ret;
1887 ret = register_pernet_subsys(&arp_tables_net_ops);
1888 if (ret < 0)
1889 goto err1;
1891 /* Noone else will be downing sem now, so we won't sleep */
1892 ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1893 if (ret < 0)
1894 goto err2;
1896 /* Register setsockopt */
1897 ret = nf_register_sockopt(&arpt_sockopts);
1898 if (ret < 0)
1899 goto err4;
1901 printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1902 return 0;
1904 err4:
1905 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1906 err2:
1907 unregister_pernet_subsys(&arp_tables_net_ops);
1908 err1:
1909 return ret;
1912 static void __exit arp_tables_fini(void)
1914 nf_unregister_sockopt(&arpt_sockopts);
1915 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1916 unregister_pernet_subsys(&arp_tables_net_ops);
1919 EXPORT_SYMBOL(arpt_register_table);
1920 EXPORT_SYMBOL(arpt_unregister_table);
1921 EXPORT_SYMBOL(arpt_do_table);
1923 module_init(arp_tables_init);
1924 module_exit(arp_tables_fini);