NETFILTER: remove unnecessary goto statement for error recovery
[tomato.git] / release / src-rt / linux / linux-2.6 / net / ipv4 / netfilter / arp_tables.c
blob404b3ec10e1c46901442a67356333b11bc97717d
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)
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>
23 #include <asm/uaccess.h>
24 #include <linux/mutex.h>
26 #include <linux/netfilter/x_tables.h>
27 #include <linux/netfilter_arp/arp_tables.h>
29 MODULE_LICENSE("GPL");
30 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
31 MODULE_DESCRIPTION("arptables core");
33 /*#define DEBUG_ARP_TABLES*/
34 /*#define DEBUG_ARP_TABLES_USER*/
36 #ifdef DEBUG_ARP_TABLES
37 #define dprintf(format, args...) printk(format , ## args)
38 #else
39 #define dprintf(format, args...)
40 #endif
42 #ifdef DEBUG_ARP_TABLES_USER
43 #define duprintf(format, args...) printk(format , ## args)
44 #else
45 #define duprintf(format, args...)
46 #endif
48 #ifdef CONFIG_NETFILTER_DEBUG
49 #define ARP_NF_ASSERT(x) \
50 do { \
51 if (!(x)) \
52 printk("ARP_NF_ASSERT: %s:%s:%u\n", \
53 __FUNCTION__, __FILE__, __LINE__); \
54 } while(0)
55 #else
56 #define ARP_NF_ASSERT(x)
57 #endif
59 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
60 char *hdr_addr, int len)
62 int i, ret;
64 if (len > ARPT_DEV_ADDR_LEN_MAX)
65 len = ARPT_DEV_ADDR_LEN_MAX;
67 ret = 0;
68 for (i = 0; i < len; i++)
69 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
71 return (ret != 0);
75 * Unfortunatly, _b and _mask are not aligned to an int (or long int)
76 * Some arches dont care, unrolling the loop is a win on them.
77 * For other arches, we only have a 16bit alignement.
79 static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
81 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
82 unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
83 #else
84 unsigned long ret = 0;
85 const u16 *a = (const u16 *)_a;
86 const u16 *b = (const u16 *)_b;
87 const u16 *mask = (const u16 *)_mask;
88 int i;
90 for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
91 ret |= (a[i] ^ b[i]) & mask[i];
92 #endif
93 return ret;
96 /* Returns whether packet matches rule or not. */
97 static inline int arp_packet_match(const struct arphdr *arphdr,
98 struct net_device *dev,
99 const char *indev,
100 const char *outdev,
101 const struct arpt_arp *arpinfo)
103 char *arpptr = (char *)(arphdr + 1);
104 char *src_devaddr, *tgt_devaddr;
105 __be32 src_ipaddr, tgt_ipaddr;
106 long ret;
108 #define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
110 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
111 ARPT_INV_ARPOP)) {
112 dprintf("ARP operation field mismatch.\n");
113 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
114 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
115 return 0;
118 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
119 ARPT_INV_ARPHRD)) {
120 dprintf("ARP hardware address format mismatch.\n");
121 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
122 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
123 return 0;
126 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
127 ARPT_INV_ARPPRO)) {
128 dprintf("ARP protocol address format mismatch.\n");
129 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
130 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
131 return 0;
134 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
135 ARPT_INV_ARPHLN)) {
136 dprintf("ARP hardware address length mismatch.\n");
137 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
138 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
139 return 0;
142 src_devaddr = arpptr;
143 arpptr += dev->addr_len;
144 memcpy(&src_ipaddr, arpptr, sizeof(u32));
145 arpptr += sizeof(u32);
146 tgt_devaddr = arpptr;
147 arpptr += dev->addr_len;
148 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
150 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
151 ARPT_INV_SRCDEVADDR) ||
152 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
153 ARPT_INV_TGTDEVADDR)) {
154 dprintf("Source or target device address mismatch.\n");
156 return 0;
159 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
160 ARPT_INV_SRCIP) ||
161 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
162 ARPT_INV_TGTIP)) {
163 dprintf("Source or target IP address mismatch.\n");
165 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
166 NIPQUAD(src_ipaddr),
167 NIPQUAD(arpinfo->smsk.s_addr),
168 NIPQUAD(arpinfo->src.s_addr),
169 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
170 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
171 NIPQUAD(tgt_ipaddr),
172 NIPQUAD(arpinfo->tmsk.s_addr),
173 NIPQUAD(arpinfo->tgt.s_addr),
174 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
175 return 0;
178 /* Look for ifname matches. */
179 ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
181 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
182 dprintf("VIA in mismatch (%s vs %s).%s\n",
183 indev, arpinfo->iniface,
184 arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
185 return 0;
188 ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
190 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
191 dprintf("VIA out mismatch (%s vs %s).%s\n",
192 outdev, arpinfo->outiface,
193 arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
194 return 0;
197 return 1;
200 static inline int arp_checkentry(const struct arpt_arp *arp)
202 if (arp->flags & ~ARPT_F_MASK) {
203 duprintf("Unknown flag bits set: %08X\n",
204 arp->flags & ~ARPT_F_MASK);
205 return 0;
207 if (arp->invflags & ~ARPT_INV_MASK) {
208 duprintf("Unknown invflag bits set: %08X\n",
209 arp->invflags & ~ARPT_INV_MASK);
210 return 0;
213 return 1;
216 static unsigned int arpt_error(struct sk_buff *skb,
217 const struct net_device *in,
218 const struct net_device *out,
219 unsigned int hooknum,
220 const struct xt_target *target,
221 const void *targinfo)
223 if (net_ratelimit())
224 printk("arp_tables: error: '%s'\n", (char *)targinfo);
226 return NF_DROP;
229 static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
231 return (struct arpt_entry *)(base + offset);
234 unsigned int arpt_do_table(struct sk_buff *skb,
235 unsigned int hook,
236 const struct net_device *in,
237 const struct net_device *out,
238 struct arpt_table *table)
240 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
241 unsigned int verdict = NF_DROP;
242 struct arphdr *arp;
243 int hotdrop = 0;
244 struct arpt_entry *e, *back;
245 const char *indev, *outdev;
246 void *table_base;
247 struct xt_table_info *private;
249 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
250 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
251 (2 * skb->dev->addr_len) +
252 (2 * sizeof(u32)))))
253 return NF_DROP;
255 indev = in ? in->name : nulldevname;
256 outdev = out ? out->name : nulldevname;
258 xt_info_rdlock_bh();
259 private = table->private;
260 table_base = private->entries[smp_processor_id()];
262 e = get_entry(table_base, private->hook_entry[hook]);
263 back = get_entry(table_base, private->underflow[hook]);
265 arp = arp_hdr(skb);
266 do {
267 if (arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
268 struct arpt_entry_target *t;
269 int hdr_len;
271 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
272 (2 * skb->dev->addr_len);
274 ADD_COUNTER(e->counters, hdr_len, 1);
276 t = arpt_get_target(e);
278 /* Standard target? */
279 if (!t->u.kernel.target->target) {
280 int v;
282 v = ((struct arpt_standard_target *)t)->verdict;
283 if (v < 0) {
284 /* Pop from stack? */
285 if (v != ARPT_RETURN) {
286 verdict = (unsigned)(-v) - 1;
287 break;
289 e = back;
290 back = get_entry(table_base,
291 back->comefrom);
292 continue;
294 if (table_base + v
295 != (void *)e + e->next_offset) {
296 /* Save old back ptr in next entry */
297 struct arpt_entry *next
298 = (void *)e + e->next_offset;
299 next->comefrom =
300 (void *)back - table_base;
302 /* set back pointer to next entry */
303 back = next;
306 e = get_entry(table_base, v);
307 } else {
308 /* Targets which reenter must return
309 * abs. verdicts
311 verdict = t->u.kernel.target->target(skb,
312 in, out,
313 hook,
314 t->u.kernel.target,
315 t->data);
317 /* Target might have changed stuff. */
318 arp = arp_hdr(skb);
320 if (verdict == ARPT_CONTINUE)
321 e = (void *)e + e->next_offset;
322 else
323 /* Verdict */
324 break;
326 } else {
327 e = (void *)e + e->next_offset;
329 } while (!hotdrop);
330 xt_info_rdunlock_bh();
332 if (hotdrop)
333 return NF_DROP;
334 else
335 return verdict;
338 /* All zeroes == unconditional rule. */
339 static inline int unconditional(const struct arpt_arp *arp)
341 unsigned int i;
343 for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
344 if (((__u32 *)arp)[i])
345 return 0;
347 return 1;
350 /* Figures out from what hook each rule can be called: returns 0 if
351 * there are loops. Puts hook bitmask in comefrom.
353 static int mark_source_chains(struct xt_table_info *newinfo,
354 unsigned int valid_hooks, void *entry0)
356 unsigned int hook;
358 /* No recursion; use packet counter to save back ptrs (reset
359 * to 0 as we leave), and comefrom to save source hook bitmask.
361 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
362 unsigned int pos = newinfo->hook_entry[hook];
363 struct arpt_entry *e
364 = (struct arpt_entry *)(entry0 + pos);
366 if (!(valid_hooks & (1 << hook)))
367 continue;
369 /* Set initial back pointer. */
370 e->counters.pcnt = pos;
372 for (;;) {
373 struct arpt_standard_target *t
374 = (void *)arpt_get_target(e);
375 int visited = e->comefrom & (1 << hook);
377 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
378 printk("arptables: loop hook %u pos %u %08X.\n",
379 hook, pos, e->comefrom);
380 return 0;
382 e->comefrom
383 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
385 /* Unconditional return/END. */
386 if ((e->target_offset == sizeof(struct arpt_entry)
387 && (strcmp(t->target.u.user.name,
388 ARPT_STANDARD_TARGET) == 0)
389 && t->verdict < 0
390 && unconditional(&e->arp)) || visited) {
391 unsigned int oldpos, size;
393 if ((strcmp(t->target.u.user.name,
394 ARPT_STANDARD_TARGET) == 0) &&
395 t->verdict < -NF_MAX_VERDICT - 1) {
396 duprintf("mark_source_chains: bad "
397 "negative verdict (%i)\n",
398 t->verdict);
399 return 0;
402 /* Return: backtrack through the last
403 * big jump.
405 do {
406 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
407 oldpos = pos;
408 pos = e->counters.pcnt;
409 e->counters.pcnt = 0;
411 /* We're at the start. */
412 if (pos == oldpos)
413 goto next;
415 e = (struct arpt_entry *)
416 (entry0 + pos);
417 } while (oldpos == pos + e->next_offset);
419 /* Move along one */
420 size = e->next_offset;
421 e = (struct arpt_entry *)
422 (entry0 + pos + size);
423 e->counters.pcnt = pos;
424 pos += size;
425 } else {
426 int newpos = t->verdict;
428 if (strcmp(t->target.u.user.name,
429 ARPT_STANDARD_TARGET) == 0
430 && newpos >= 0) {
431 if (newpos > newinfo->size -
432 sizeof(struct arpt_entry)) {
433 duprintf("mark_source_chains: "
434 "bad verdict (%i)\n",
435 newpos);
436 return 0;
439 /* This a jump; chase it. */
440 duprintf("Jump rule %u -> %u\n",
441 pos, newpos);
442 } else {
443 /* ... this is a fallthru */
444 newpos = pos + e->next_offset;
446 e = (struct arpt_entry *)
447 (entry0 + newpos);
448 e->counters.pcnt = pos;
449 pos = newpos;
452 next:
453 duprintf("Finished chain %u\n", hook);
455 return 1;
458 static inline int check_entry(struct arpt_entry *e, const char *name)
460 struct arpt_entry_target *t;
462 if (!arp_checkentry(&e->arp)) {
463 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
464 return -EINVAL;
467 if (e->target_offset + sizeof(struct arpt_entry_target) > e->next_offset)
468 return -EINVAL;
470 t = arpt_get_target(e);
471 if (e->target_offset + t->u.target_size > e->next_offset)
472 return -EINVAL;
474 return 0;
477 static inline int check_target(struct arpt_entry *e, const char *name)
479 struct arpt_entry_target *t;
480 struct arpt_target *target;
481 int ret;
483 t = arpt_get_target(e);
484 target = t->u.kernel.target;
486 ret = xt_check_target(target, NF_ARP, t->u.target_size - sizeof(*t),
487 name, e->comefrom, 0, 0);
488 if (!ret && t->u.kernel.target->checkentry
489 && !t->u.kernel.target->checkentry(name, e, target, t->data,
490 e->comefrom)) {
491 duprintf("arp_tables: check failed for `%s'.\n",
492 t->u.kernel.target->name);
493 ret = -EINVAL;
495 return ret;
498 static inline int
499 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
500 unsigned int *i)
502 struct arpt_entry_target *t;
503 struct arpt_target *target;
504 int ret;
506 ret = check_entry(e, name);
507 if (ret)
508 return ret;
510 t = arpt_get_target(e);
511 target = try_then_request_module(xt_find_target(NF_ARP, t->u.user.name,
512 t->u.user.revision),
513 "arpt_%s", t->u.user.name);
514 if (IS_ERR(target) || !target) {
515 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
516 ret = target ? PTR_ERR(target) : -ENOENT;
517 goto out;
519 t->u.kernel.target = target;
521 ret = check_target(e, name);
522 if (ret)
523 goto err;
525 (*i)++;
526 return 0;
527 err:
528 module_put(t->u.kernel.target->me);
529 out:
530 return ret;
533 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
534 struct xt_table_info *newinfo,
535 unsigned char *base,
536 unsigned char *limit,
537 const unsigned int *hook_entries,
538 const unsigned int *underflows,
539 unsigned int *i)
541 unsigned int h;
543 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
544 || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
545 duprintf("Bad offset %p\n", e);
546 return -EINVAL;
549 if (e->next_offset
550 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
551 duprintf("checking: element %p size %u\n",
552 e, e->next_offset);
553 return -EINVAL;
556 /* Check hooks & underflows */
557 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
558 if ((unsigned char *)e - base == hook_entries[h])
559 newinfo->hook_entry[h] = hook_entries[h];
560 if ((unsigned char *)e - base == underflows[h])
561 newinfo->underflow[h] = underflows[h];
564 /* FIXME: underflows must be unconditional, standard verdicts
565 < 0 (not ARPT_RETURN). --RR */
567 /* Clear counters and comefrom */
568 e->counters = ((struct xt_counters) { 0, 0 });
569 e->comefrom = 0;
571 (*i)++;
572 return 0;
575 static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
577 struct arpt_entry_target *t;
579 if (i && (*i)-- == 0)
580 return 1;
582 t = arpt_get_target(e);
583 if (t->u.kernel.target->destroy)
584 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
585 module_put(t->u.kernel.target->me);
586 return 0;
589 /* Checks and translates the user-supplied table segment (held in
590 * newinfo).
592 static int translate_table(const char *name,
593 unsigned int valid_hooks,
594 struct xt_table_info *newinfo,
595 void *entry0,
596 unsigned int size,
597 unsigned int number,
598 const unsigned int *hook_entries,
599 const unsigned int *underflows)
601 unsigned int i;
602 int ret;
604 newinfo->size = size;
605 newinfo->number = number;
607 /* Init all hooks to impossible value. */
608 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
609 newinfo->hook_entry[i] = 0xFFFFFFFF;
610 newinfo->underflow[i] = 0xFFFFFFFF;
613 duprintf("translate_table: size %u\n", newinfo->size);
614 i = 0;
616 /* Walk through entries, checking offsets. */
617 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
618 check_entry_size_and_hooks,
619 newinfo,
620 entry0,
621 entry0 + size,
622 hook_entries, underflows, &i);
623 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
624 if (ret != 0)
625 return ret;
627 if (i != number) {
628 duprintf("translate_table: %u not %u entries\n",
629 i, number);
630 return -EINVAL;
633 /* Check hooks all assigned */
634 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
635 /* Only hooks which are valid */
636 if (!(valid_hooks & (1 << i)))
637 continue;
638 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
639 duprintf("Invalid hook entry %u %u\n",
640 i, hook_entries[i]);
641 return -EINVAL;
643 if (newinfo->underflow[i] == 0xFFFFFFFF) {
644 duprintf("Invalid underflow %u %u\n",
645 i, underflows[i]);
646 return -EINVAL;
650 if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
651 duprintf("Looping hook\n");
652 return -ELOOP;
655 /* Finally, each sanity check must pass */
656 i = 0;
657 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
658 find_check_entry, name, size, &i);
660 if (ret != 0) {
661 ARPT_ENTRY_ITERATE(entry0, newinfo->size,
662 cleanup_entry, &i);
663 return ret;
666 /* And one copy for every other CPU */
667 for_each_possible_cpu(i) {
668 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
669 memcpy(newinfo->entries[i], entry0, newinfo->size);
672 return ret;
675 /* Gets counters. */
676 static inline int add_entry_to_counter(const struct arpt_entry *e,
677 struct xt_counters total[],
678 unsigned int *i)
680 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
682 (*i)++;
683 return 0;
686 static inline int set_entry_to_counter(const struct arpt_entry *e,
687 struct xt_counters total[],
688 unsigned int *i)
690 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
692 (*i)++;
693 return 0;
696 static void get_counters(const struct xt_table_info *t,
697 struct xt_counters counters[])
699 unsigned int cpu;
700 unsigned int i;
701 unsigned int curcpu;
703 /* Instead of clearing (by a previous call to memset())
704 * the counters and using adds, we set the counters
705 * with data used by 'current' CPU
707 * Bottom half has to be disabled to prevent deadlock
708 * if new softirq were to run and call ipt_do_table
710 local_bh_disable();
711 curcpu = smp_processor_id();
713 i = 0;
714 ARPT_ENTRY_ITERATE(t->entries[curcpu],
715 t->size,
716 set_entry_to_counter,
717 counters,
718 &i);
720 for_each_possible_cpu(cpu) {
721 if (cpu == curcpu)
722 continue;
723 i = 0;
724 xt_info_wrlock(cpu);
725 ARPT_ENTRY_ITERATE(t->entries[cpu],
726 t->size,
727 add_entry_to_counter,
728 counters,
729 &i);
730 xt_info_wrunlock(cpu);
732 local_bh_enable();
735 static struct xt_counters *alloc_counters(struct arpt_table *table)
737 unsigned int countersize;
738 struct xt_counters *counters;
739 struct xt_table_info *private = table->private;
741 /* We need atomic snapshot of counters: rest doesn't change
742 * (other than comefrom, which userspace doesn't care
743 * about).
745 countersize = sizeof(struct xt_counters) * private->number;
746 counters = vmalloc(countersize);
748 if (counters == NULL)
749 return ERR_PTR(-ENOMEM);
751 get_counters(private, counters);
753 return counters;
756 static int copy_entries_to_user(unsigned int total_size,
757 struct arpt_table *table,
758 void __user *userptr)
760 unsigned int off, num;
761 struct arpt_entry *e;
762 struct xt_counters *counters;
763 struct xt_table_info *private = table->private;
764 int ret = 0;
765 void *loc_cpu_entry;
767 counters = alloc_counters(table);
768 if (IS_ERR(counters))
769 return PTR_ERR(counters);
771 loc_cpu_entry = private->entries[raw_smp_processor_id()];
772 /* ... then copy entire thing ... */
773 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
774 ret = -EFAULT;
775 goto free_counters;
778 /* FIXME: use iterator macros --RR */
779 /* ... then go back and fix counters and names */
780 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
781 struct arpt_entry_target *t;
783 e = (struct arpt_entry *)(loc_cpu_entry + off);
784 if (copy_to_user(userptr + off
785 + offsetof(struct arpt_entry, counters),
786 &counters[num],
787 sizeof(counters[num])) != 0) {
788 ret = -EFAULT;
789 goto free_counters;
792 t = arpt_get_target(e);
793 if (copy_to_user(userptr + off + e->target_offset
794 + offsetof(struct arpt_entry_target,
795 u.user.name),
796 t->u.kernel.target->name,
797 strlen(t->u.kernel.target->name)+1) != 0) {
798 ret = -EFAULT;
799 goto free_counters;
803 free_counters:
804 vfree(counters);
805 return ret;
808 static int get_entries(const struct arpt_get_entries *entries,
809 struct arpt_get_entries __user *uptr)
811 int ret;
812 struct arpt_table *t;
814 t = xt_find_table_lock(NF_ARP, entries->name);
815 if (t && !IS_ERR(t)) {
816 struct xt_table_info *private = t->private;
817 duprintf("t->private->number = %u\n",
818 private->number);
819 if (entries->size == private->size)
820 ret = copy_entries_to_user(private->size,
821 t, uptr->entrytable);
822 else {
823 duprintf("get_entries: I've got %u not %u!\n",
824 private->size, entries->size);
825 ret = -EINVAL;
827 module_put(t->me);
828 xt_table_unlock(t);
829 } else
830 ret = t ? PTR_ERR(t) : -ENOENT;
832 return ret;
835 static int do_replace(void __user *user, unsigned int len)
837 int ret;
838 struct arpt_replace tmp;
839 struct arpt_table *t;
840 struct xt_table_info *newinfo, *oldinfo;
841 struct xt_counters *counters;
842 void *loc_cpu_entry, *loc_cpu_old_entry;
844 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
845 return -EFAULT;
847 /* overflow check */
848 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
849 return -ENOMEM;
851 newinfo = xt_alloc_table_info(tmp.size);
852 if (!newinfo)
853 return -ENOMEM;
855 /* choose the copy that is on our node/cpu */
856 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
857 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
858 tmp.size) != 0) {
859 ret = -EFAULT;
860 goto free_newinfo;
863 counters = vmalloc(tmp.num_counters * sizeof(struct xt_counters));
864 if (!counters) {
865 ret = -ENOMEM;
866 goto free_newinfo;
869 ret = translate_table(tmp.name, tmp.valid_hooks,
870 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
871 tmp.hook_entry, tmp.underflow);
872 if (ret != 0)
873 goto free_newinfo_counters;
875 duprintf("arp_tables: Translated table\n");
877 t = try_then_request_module(xt_find_table_lock(NF_ARP, tmp.name),
878 "arptable_%s", tmp.name);
879 if (!t || IS_ERR(t)) {
880 ret = t ? PTR_ERR(t) : -ENOENT;
881 goto free_newinfo_counters_untrans;
884 /* You lied! */
885 if (tmp.valid_hooks != t->valid_hooks) {
886 duprintf("Valid hook crap: %08X vs %08X\n",
887 tmp.valid_hooks, t->valid_hooks);
888 ret = -EINVAL;
889 goto put_module;
892 oldinfo = xt_replace_table(t, tmp.num_counters, newinfo, &ret);
893 if (!oldinfo)
894 goto put_module;
896 /* Update module usage count based on number of rules */
897 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
898 oldinfo->number, oldinfo->initial_entries, newinfo->number);
899 if ((oldinfo->number > oldinfo->initial_entries) ||
900 (newinfo->number <= oldinfo->initial_entries))
901 module_put(t->me);
902 if ((oldinfo->number > oldinfo->initial_entries) &&
903 (newinfo->number <= oldinfo->initial_entries))
904 module_put(t->me);
906 /* Get the old counters, and synchronize with replace */
907 get_counters(oldinfo, counters);
909 /* Decrease module usage counts and free resource */
910 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
911 ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL);
913 xt_free_table_info(oldinfo);
914 if (copy_to_user(tmp.counters, counters,
915 sizeof(struct xt_counters) * tmp.num_counters) != 0)
916 ret = -EFAULT;
917 vfree(counters);
918 xt_table_unlock(t);
919 return ret;
921 put_module:
922 module_put(t->me);
923 xt_table_unlock(t);
924 free_newinfo_counters_untrans:
925 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
926 free_newinfo_counters:
927 vfree(counters);
928 free_newinfo:
929 xt_free_table_info(newinfo);
930 return ret;
933 /* We're lazy, and add to the first CPU; overflow works its fey magic
934 * and everything is OK. */
935 static int
936 add_counter_to_entry(struct arpt_entry *e,
937 const struct xt_counters addme[],
938 unsigned int *i)
940 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
942 (*i)++;
943 return 0;
946 static int do_add_counters(void __user *user, unsigned int len)
948 unsigned int i, curcpu;
949 struct xt_counters_info tmp, *paddc;
950 struct arpt_table *t;
951 struct xt_table_info *private;
952 int ret = 0;
953 void *loc_cpu_entry;
955 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
956 return -EFAULT;
958 if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct xt_counters))
959 return -EINVAL;
961 paddc = vmalloc(len);
962 if (!paddc)
963 return -ENOMEM;
965 if (copy_from_user(paddc, user, len) != 0) {
966 ret = -EFAULT;
967 goto free;
970 t = xt_find_table_lock(NF_ARP, tmp.name);
971 if (!t || IS_ERR(t)) {
972 ret = t ? PTR_ERR(t) : -ENOENT;
973 goto free;
976 local_bh_disable();
977 private = t->private;
978 if (private->number != tmp.num_counters) {
979 ret = -EINVAL;
980 goto unlock_up_free;
983 i = 0;
984 /* Choose the copy that is on our node */
985 curcpu = smp_processor_id();
986 loc_cpu_entry = private->entries[curcpu];
987 xt_info_wrlock(curcpu);
988 ARPT_ENTRY_ITERATE(loc_cpu_entry,
989 private->size,
990 add_counter_to_entry,
991 paddc->counters,
992 &i);
993 xt_info_wrunlock(curcpu);
994 unlock_up_free:
995 local_bh_enable();
996 xt_table_unlock(t);
997 module_put(t->me);
998 free:
999 vfree(paddc);
1001 return ret;
1004 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1006 int ret;
1008 if (!capable(CAP_NET_ADMIN))
1009 return -EPERM;
1011 switch (cmd) {
1012 case ARPT_SO_SET_REPLACE:
1013 ret = do_replace(user, len);
1014 break;
1016 case ARPT_SO_SET_ADD_COUNTERS:
1017 ret = do_add_counters(user, len);
1018 break;
1020 default:
1021 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1022 ret = -EINVAL;
1025 return ret;
1028 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1030 int ret;
1032 if (!capable(CAP_NET_ADMIN))
1033 return -EPERM;
1035 switch (cmd) {
1036 case ARPT_SO_GET_INFO: {
1037 char name[ARPT_TABLE_MAXNAMELEN];
1038 struct arpt_table *t;
1040 if (*len != sizeof(struct arpt_getinfo)) {
1041 duprintf("length %u != %Zu\n", *len,
1042 sizeof(struct arpt_getinfo));
1043 ret = -EINVAL;
1044 break;
1047 if (copy_from_user(name, user, sizeof(name)) != 0) {
1048 ret = -EFAULT;
1049 break;
1051 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
1053 t = try_then_request_module(xt_find_table_lock(NF_ARP, name),
1054 "arptable_%s", name);
1055 if (t && !IS_ERR(t)) {
1056 struct arpt_getinfo info;
1057 struct xt_table_info *private = t->private;
1059 info.valid_hooks = t->valid_hooks;
1060 memcpy(info.hook_entry, private->hook_entry,
1061 sizeof(info.hook_entry));
1062 memcpy(info.underflow, private->underflow,
1063 sizeof(info.underflow));
1064 info.num_entries = private->number;
1065 info.size = private->size;
1066 strcpy(info.name, name);
1068 if (copy_to_user(user, &info, *len) != 0)
1069 ret = -EFAULT;
1070 else
1071 ret = 0;
1072 xt_table_unlock(t);
1073 module_put(t->me);
1074 } else
1075 ret = t ? PTR_ERR(t) : -ENOENT;
1077 break;
1079 case ARPT_SO_GET_ENTRIES: {
1080 struct arpt_get_entries get;
1082 if (*len < sizeof(get)) {
1083 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
1084 ret = -EINVAL;
1085 } else if (copy_from_user(&get, user, sizeof(get)) != 0) {
1086 ret = -EFAULT;
1087 } else if (*len != sizeof(struct arpt_get_entries) + get.size) {
1088 duprintf("get_entries: %u != %Zu\n", *len,
1089 sizeof(struct arpt_get_entries) + get.size);
1090 ret = -EINVAL;
1091 } else
1092 ret = get_entries(&get, user);
1093 break;
1096 case ARPT_SO_GET_REVISION_TARGET: {
1097 struct xt_get_revision rev;
1099 if (*len != sizeof(rev)) {
1100 ret = -EINVAL;
1101 break;
1103 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1104 ret = -EFAULT;
1105 break;
1108 try_then_request_module(xt_find_revision(NF_ARP, rev.name,
1109 rev.revision, 1, &ret),
1110 "arpt_%s", rev.name);
1111 break;
1114 default:
1115 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1116 ret = -EINVAL;
1119 return ret;
1122 int arpt_register_table(struct arpt_table *table,
1123 const struct arpt_replace *repl)
1125 int ret;
1126 struct xt_table_info *newinfo;
1127 struct xt_table_info bootstrap
1128 = { 0, 0, 0, { 0 }, { 0 }, { } };
1129 void *loc_cpu_entry;
1131 newinfo = xt_alloc_table_info(repl->size);
1132 if (!newinfo) {
1133 ret = -ENOMEM;
1134 return ret;
1137 /* choose the copy on our node/cpu */
1138 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1139 memcpy(loc_cpu_entry, repl->entries, repl->size);
1141 ret = translate_table(table->name, table->valid_hooks,
1142 newinfo, loc_cpu_entry, repl->size,
1143 repl->num_entries,
1144 repl->hook_entry,
1145 repl->underflow);
1147 duprintf("arpt_register_table: translate table gives %d\n", ret);
1148 if (ret != 0) {
1149 xt_free_table_info(newinfo);
1150 return ret;
1153 ret = xt_register_table(table, &bootstrap, newinfo);
1154 if (ret != 0) {
1155 xt_free_table_info(newinfo);
1156 return ret;
1159 return 0;
1162 void arpt_unregister_table(struct arpt_table *table)
1164 struct xt_table_info *private;
1165 void *loc_cpu_entry;
1167 private = xt_unregister_table(table);
1169 /* Decrease module usage counts and free resources */
1170 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1171 ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size,
1172 cleanup_entry, NULL);
1173 xt_free_table_info(private);
1176 /* The built-in targets: standard (NULL) and error. */
1177 static struct arpt_target arpt_standard_target = {
1178 .name = ARPT_STANDARD_TARGET,
1179 .targetsize = sizeof(int),
1180 .family = NF_ARP,
1183 static struct arpt_target arpt_error_target = {
1184 .name = ARPT_ERROR_TARGET,
1185 .target = arpt_error,
1186 .targetsize = ARPT_FUNCTION_MAXNAMELEN,
1187 .family = NF_ARP,
1190 static struct nf_sockopt_ops arpt_sockopts = {
1191 .pf = PF_INET,
1192 .set_optmin = ARPT_BASE_CTL,
1193 .set_optmax = ARPT_SO_SET_MAX+1,
1194 .set = do_arpt_set_ctl,
1195 .get_optmin = ARPT_BASE_CTL,
1196 .get_optmax = ARPT_SO_GET_MAX+1,
1197 .get = do_arpt_get_ctl,
1200 static int __init arp_tables_init(void)
1202 int ret;
1204 ret = xt_proto_init(NF_ARP);
1205 if (ret < 0)
1206 goto err1;
1208 /* Noone else will be downing sem now, so we won't sleep */
1209 ret = xt_register_target(&arpt_standard_target);
1210 if (ret < 0)
1211 goto err2;
1212 ret = xt_register_target(&arpt_error_target);
1213 if (ret < 0)
1214 goto err3;
1216 /* Register setsockopt */
1217 ret = nf_register_sockopt(&arpt_sockopts);
1218 if (ret < 0)
1219 goto err4;
1221 printk("arp_tables: (C) 2002 David S. Miller\n");
1222 return 0;
1224 err4:
1225 xt_unregister_target(&arpt_error_target);
1226 err3:
1227 xt_unregister_target(&arpt_standard_target);
1228 err2:
1229 xt_proto_fini(NF_ARP);
1230 err1:
1231 return ret;
1234 static void __exit arp_tables_fini(void)
1236 nf_unregister_sockopt(&arpt_sockopts);
1237 xt_unregister_target(&arpt_error_target);
1238 xt_unregister_target(&arpt_standard_target);
1239 xt_proto_fini(NF_ARP);
1242 EXPORT_SYMBOL(arpt_register_table);
1243 EXPORT_SYMBOL(arpt_unregister_table);
1244 EXPORT_SYMBOL(arpt_do_table);
1246 module_init(arp_tables_init);
1247 module_exit(arp_tables_fini);