[NETFILTER]: x_tables: struct xt_table_info diet
[linux-2.6/verdex.git] / net / ipv4 / netfilter / arp_tables.c
bloba21722d5c9fa44736e99351625a2617f2060c293
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);
74 /* Returns whether packet matches rule or not. */
75 static inline int arp_packet_match(const struct arphdr *arphdr,
76 struct net_device *dev,
77 const char *indev,
78 const char *outdev,
79 const struct arpt_arp *arpinfo)
81 char *arpptr = (char *)(arphdr + 1);
82 char *src_devaddr, *tgt_devaddr;
83 __be32 src_ipaddr, tgt_ipaddr;
84 int i, ret;
86 #define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
88 if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
89 ARPT_INV_ARPOP)) {
90 dprintf("ARP operation field mismatch.\n");
91 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
92 arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
93 return 0;
96 if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
97 ARPT_INV_ARPHRD)) {
98 dprintf("ARP hardware address format mismatch.\n");
99 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
100 arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
101 return 0;
104 if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
105 ARPT_INV_ARPPRO)) {
106 dprintf("ARP protocol address format mismatch.\n");
107 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
108 arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
109 return 0;
112 if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
113 ARPT_INV_ARPHLN)) {
114 dprintf("ARP hardware address length mismatch.\n");
115 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
116 arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
117 return 0;
120 src_devaddr = arpptr;
121 arpptr += dev->addr_len;
122 memcpy(&src_ipaddr, arpptr, sizeof(u32));
123 arpptr += sizeof(u32);
124 tgt_devaddr = arpptr;
125 arpptr += dev->addr_len;
126 memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
128 if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
129 ARPT_INV_SRCDEVADDR) ||
130 FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
131 ARPT_INV_TGTDEVADDR)) {
132 dprintf("Source or target device address mismatch.\n");
134 return 0;
137 if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
138 ARPT_INV_SRCIP) ||
139 FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
140 ARPT_INV_TGTIP)) {
141 dprintf("Source or target IP address mismatch.\n");
143 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
144 NIPQUAD(src_ipaddr),
145 NIPQUAD(arpinfo->smsk.s_addr),
146 NIPQUAD(arpinfo->src.s_addr),
147 arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
148 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
149 NIPQUAD(tgt_ipaddr),
150 NIPQUAD(arpinfo->tmsk.s_addr),
151 NIPQUAD(arpinfo->tgt.s_addr),
152 arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
153 return 0;
156 /* Look for ifname matches. */
157 for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
158 ret |= (indev[i] ^ arpinfo->iniface[i])
159 & arpinfo->iniface_mask[i];
162 if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
163 dprintf("VIA in mismatch (%s vs %s).%s\n",
164 indev, arpinfo->iniface,
165 arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
166 return 0;
169 for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
170 ret |= (outdev[i] ^ arpinfo->outiface[i])
171 & arpinfo->outiface_mask[i];
174 if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
175 dprintf("VIA out mismatch (%s vs %s).%s\n",
176 outdev, arpinfo->outiface,
177 arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
178 return 0;
181 return 1;
184 static inline int arp_checkentry(const struct arpt_arp *arp)
186 if (arp->flags & ~ARPT_F_MASK) {
187 duprintf("Unknown flag bits set: %08X\n",
188 arp->flags & ~ARPT_F_MASK);
189 return 0;
191 if (arp->invflags & ~ARPT_INV_MASK) {
192 duprintf("Unknown invflag bits set: %08X\n",
193 arp->invflags & ~ARPT_INV_MASK);
194 return 0;
197 return 1;
200 static unsigned int arpt_error(struct sk_buff *skb,
201 const struct net_device *in,
202 const struct net_device *out,
203 unsigned int hooknum,
204 const struct xt_target *target,
205 const void *targinfo)
207 if (net_ratelimit())
208 printk("arp_tables: error: '%s'\n", (char *)targinfo);
210 return NF_DROP;
213 static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
215 return (struct arpt_entry *)(base + offset);
218 unsigned int arpt_do_table(struct sk_buff *skb,
219 unsigned int hook,
220 const struct net_device *in,
221 const struct net_device *out,
222 struct arpt_table *table)
224 static const char nulldevname[IFNAMSIZ];
225 unsigned int verdict = NF_DROP;
226 struct arphdr *arp;
227 bool hotdrop = false;
228 struct arpt_entry *e, *back;
229 const char *indev, *outdev;
230 void *table_base;
231 struct xt_table_info *private;
233 /* ARP header, plus 2 device addresses, plus 2 IP addresses. */
234 if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
235 (2 * skb->dev->addr_len) +
236 (2 * sizeof(u32)))))
237 return NF_DROP;
239 indev = in ? in->name : nulldevname;
240 outdev = out ? out->name : nulldevname;
242 read_lock_bh(&table->lock);
243 private = table->private;
244 table_base = (void *)private->entries[smp_processor_id()];
245 e = get_entry(table_base, private->hook_entry[hook]);
246 back = get_entry(table_base, private->underflow[hook]);
248 arp = arp_hdr(skb);
249 do {
250 if (arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
251 struct arpt_entry_target *t;
252 int hdr_len;
254 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
255 (2 * skb->dev->addr_len);
256 ADD_COUNTER(e->counters, hdr_len, 1);
258 t = arpt_get_target(e);
260 /* Standard target? */
261 if (!t->u.kernel.target->target) {
262 int v;
264 v = ((struct arpt_standard_target *)t)->verdict;
265 if (v < 0) {
266 /* Pop from stack? */
267 if (v != ARPT_RETURN) {
268 verdict = (unsigned)(-v) - 1;
269 break;
271 e = back;
272 back = get_entry(table_base,
273 back->comefrom);
274 continue;
276 if (table_base + v
277 != (void *)e + e->next_offset) {
278 /* Save old back ptr in next entry */
279 struct arpt_entry *next
280 = (void *)e + e->next_offset;
281 next->comefrom =
282 (void *)back - table_base;
284 /* set back pointer to next entry */
285 back = next;
288 e = get_entry(table_base, v);
289 } else {
290 /* Targets which reenter must return
291 * abs. verdicts
293 verdict = t->u.kernel.target->target(skb,
294 in, out,
295 hook,
296 t->u.kernel.target,
297 t->data);
299 /* Target might have changed stuff. */
300 arp = arp_hdr(skb);
302 if (verdict == ARPT_CONTINUE)
303 e = (void *)e + e->next_offset;
304 else
305 /* Verdict */
306 break;
308 } else {
309 e = (void *)e + e->next_offset;
311 } while (!hotdrop);
312 read_unlock_bh(&table->lock);
314 if (hotdrop)
315 return NF_DROP;
316 else
317 return verdict;
320 /* All zeroes == unconditional rule. */
321 static inline int unconditional(const struct arpt_arp *arp)
323 unsigned int i;
325 for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
326 if (((__u32 *)arp)[i])
327 return 0;
329 return 1;
332 /* Figures out from what hook each rule can be called: returns 0 if
333 * there are loops. Puts hook bitmask in comefrom.
335 static int mark_source_chains(struct xt_table_info *newinfo,
336 unsigned int valid_hooks, void *entry0)
338 unsigned int hook;
340 /* No recursion; use packet counter to save back ptrs (reset
341 * to 0 as we leave), and comefrom to save source hook bitmask.
343 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
344 unsigned int pos = newinfo->hook_entry[hook];
345 struct arpt_entry *e
346 = (struct arpt_entry *)(entry0 + pos);
348 if (!(valid_hooks & (1 << hook)))
349 continue;
351 /* Set initial back pointer. */
352 e->counters.pcnt = pos;
354 for (;;) {
355 struct arpt_standard_target *t
356 = (void *)arpt_get_target(e);
357 int visited = e->comefrom & (1 << hook);
359 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
360 printk("arptables: loop hook %u pos %u %08X.\n",
361 hook, pos, e->comefrom);
362 return 0;
364 e->comefrom
365 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
367 /* Unconditional return/END. */
368 if ((e->target_offset == sizeof(struct arpt_entry)
369 && (strcmp(t->target.u.user.name,
370 ARPT_STANDARD_TARGET) == 0)
371 && t->verdict < 0
372 && unconditional(&e->arp)) || visited) {
373 unsigned int oldpos, size;
375 if (t->verdict < -NF_MAX_VERDICT - 1) {
376 duprintf("mark_source_chains: bad "
377 "negative verdict (%i)\n",
378 t->verdict);
379 return 0;
382 /* Return: backtrack through the last
383 * big jump.
385 do {
386 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
387 oldpos = pos;
388 pos = e->counters.pcnt;
389 e->counters.pcnt = 0;
391 /* We're at the start. */
392 if (pos == oldpos)
393 goto next;
395 e = (struct arpt_entry *)
396 (entry0 + pos);
397 } while (oldpos == pos + e->next_offset);
399 /* Move along one */
400 size = e->next_offset;
401 e = (struct arpt_entry *)
402 (entry0 + pos + size);
403 e->counters.pcnt = pos;
404 pos += size;
405 } else {
406 int newpos = t->verdict;
408 if (strcmp(t->target.u.user.name,
409 ARPT_STANDARD_TARGET) == 0
410 && newpos >= 0) {
411 if (newpos > newinfo->size -
412 sizeof(struct arpt_entry)) {
413 duprintf("mark_source_chains: "
414 "bad verdict (%i)\n",
415 newpos);
416 return 0;
419 /* This a jump; chase it. */
420 duprintf("Jump rule %u -> %u\n",
421 pos, newpos);
422 } else {
423 /* ... this is a fallthru */
424 newpos = pos + e->next_offset;
426 e = (struct arpt_entry *)
427 (entry0 + newpos);
428 e->counters.pcnt = pos;
429 pos = newpos;
432 next:
433 duprintf("Finished chain %u\n", hook);
435 return 1;
438 static inline int standard_check(const struct arpt_entry_target *t,
439 unsigned int max_offset)
441 /* Check standard info. */
442 if (t->u.target_size
443 != ARPT_ALIGN(sizeof(struct arpt_standard_target))) {
444 duprintf("arpt_standard_check: target size %u != %Zu\n",
445 t->u.target_size,
446 ARPT_ALIGN(sizeof(struct arpt_standard_target)));
447 return 0;
450 return 1;
453 static struct arpt_target arpt_standard_target;
455 static inline int check_entry(struct arpt_entry *e, const char *name, unsigned int size,
456 unsigned int *i)
458 struct arpt_entry_target *t;
459 struct arpt_target *target;
460 int ret;
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 target = try_then_request_module(xt_find_target(NF_ARP, t->u.user.name,
475 t->u.user.revision),
476 "arpt_%s", t->u.user.name);
477 if (IS_ERR(target) || !target) {
478 duprintf("check_entry: `%s' not found\n", t->u.user.name);
479 ret = target ? PTR_ERR(target) : -ENOENT;
480 goto out;
482 t->u.kernel.target = target;
484 ret = xt_check_target(target, NF_ARP, t->u.target_size - sizeof(*t),
485 name, e->comefrom, 0, 0);
486 if (ret)
487 goto err;
489 if (t->u.kernel.target == &arpt_standard_target) {
490 if (!standard_check(t, size)) {
491 ret = -EINVAL;
492 goto err;
494 } else if (t->u.kernel.target->checkentry
495 && !t->u.kernel.target->checkentry(name, e, target, t->data,
496 e->comefrom)) {
497 duprintf("arp_tables: check failed for `%s'.\n",
498 t->u.kernel.target->name);
499 ret = -EINVAL;
500 goto err;
503 (*i)++;
504 return 0;
505 err:
506 module_put(t->u.kernel.target->me);
507 out:
508 return ret;
511 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
512 struct xt_table_info *newinfo,
513 unsigned char *base,
514 unsigned char *limit,
515 const unsigned int *hook_entries,
516 const unsigned int *underflows,
517 unsigned int *i)
519 unsigned int h;
521 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
522 || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
523 duprintf("Bad offset %p\n", e);
524 return -EINVAL;
527 if (e->next_offset
528 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
529 duprintf("checking: element %p size %u\n",
530 e, e->next_offset);
531 return -EINVAL;
534 /* Check hooks & underflows */
535 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
536 if ((unsigned char *)e - base == hook_entries[h])
537 newinfo->hook_entry[h] = hook_entries[h];
538 if ((unsigned char *)e - base == underflows[h])
539 newinfo->underflow[h] = underflows[h];
542 /* FIXME: underflows must be unconditional, standard verdicts
543 < 0 (not ARPT_RETURN). --RR */
545 /* Clear counters and comefrom */
546 e->counters = ((struct xt_counters) { 0, 0 });
547 e->comefrom = 0;
549 (*i)++;
550 return 0;
553 static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
555 struct arpt_entry_target *t;
557 if (i && (*i)-- == 0)
558 return 1;
560 t = arpt_get_target(e);
561 if (t->u.kernel.target->destroy)
562 t->u.kernel.target->destroy(t->u.kernel.target, t->data);
563 module_put(t->u.kernel.target->me);
564 return 0;
567 /* Checks and translates the user-supplied table segment (held in
568 * newinfo).
570 static int translate_table(const char *name,
571 unsigned int valid_hooks,
572 struct xt_table_info *newinfo,
573 void *entry0,
574 unsigned int size,
575 unsigned int number,
576 const unsigned int *hook_entries,
577 const unsigned int *underflows)
579 unsigned int i;
580 int ret;
582 newinfo->size = size;
583 newinfo->number = number;
585 /* Init all hooks to impossible value. */
586 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
587 newinfo->hook_entry[i] = 0xFFFFFFFF;
588 newinfo->underflow[i] = 0xFFFFFFFF;
591 duprintf("translate_table: size %u\n", newinfo->size);
592 i = 0;
594 /* Walk through entries, checking offsets. */
595 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
596 check_entry_size_and_hooks,
597 newinfo,
598 entry0,
599 entry0 + size,
600 hook_entries, underflows, &i);
601 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
602 if (ret != 0)
603 return ret;
605 if (i != number) {
606 duprintf("translate_table: %u not %u entries\n",
607 i, number);
608 return -EINVAL;
611 /* Check hooks all assigned */
612 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
613 /* Only hooks which are valid */
614 if (!(valid_hooks & (1 << i)))
615 continue;
616 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
617 duprintf("Invalid hook entry %u %u\n",
618 i, hook_entries[i]);
619 return -EINVAL;
621 if (newinfo->underflow[i] == 0xFFFFFFFF) {
622 duprintf("Invalid underflow %u %u\n",
623 i, underflows[i]);
624 return -EINVAL;
628 if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
629 duprintf("Looping hook\n");
630 return -ELOOP;
633 /* Finally, each sanity check must pass */
634 i = 0;
635 ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
636 check_entry, name, size, &i);
638 if (ret != 0) {
639 ARPT_ENTRY_ITERATE(entry0, newinfo->size,
640 cleanup_entry, &i);
641 return ret;
644 /* And one copy for every other CPU */
645 for_each_possible_cpu(i) {
646 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
647 memcpy(newinfo->entries[i], entry0, newinfo->size);
650 return ret;
653 /* Gets counters. */
654 static inline int add_entry_to_counter(const struct arpt_entry *e,
655 struct xt_counters total[],
656 unsigned int *i)
658 ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
660 (*i)++;
661 return 0;
664 static inline int set_entry_to_counter(const struct arpt_entry *e,
665 struct xt_counters total[],
666 unsigned int *i)
668 SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
670 (*i)++;
671 return 0;
674 static void get_counters(const struct xt_table_info *t,
675 struct xt_counters counters[])
677 unsigned int cpu;
678 unsigned int i;
679 unsigned int curcpu;
681 /* Instead of clearing (by a previous call to memset())
682 * the counters and using adds, we set the counters
683 * with data used by 'current' CPU
684 * We dont care about preemption here.
686 curcpu = raw_smp_processor_id();
688 i = 0;
689 ARPT_ENTRY_ITERATE(t->entries[curcpu],
690 t->size,
691 set_entry_to_counter,
692 counters,
693 &i);
695 for_each_possible_cpu(cpu) {
696 if (cpu == curcpu)
697 continue;
698 i = 0;
699 ARPT_ENTRY_ITERATE(t->entries[cpu],
700 t->size,
701 add_entry_to_counter,
702 counters,
703 &i);
707 static int copy_entries_to_user(unsigned int total_size,
708 struct arpt_table *table,
709 void __user *userptr)
711 unsigned int off, num, countersize;
712 struct arpt_entry *e;
713 struct xt_counters *counters;
714 struct xt_table_info *private = table->private;
715 int ret = 0;
716 void *loc_cpu_entry;
718 /* We need atomic snapshot of counters: rest doesn't change
719 * (other than comefrom, which userspace doesn't care
720 * about).
722 countersize = sizeof(struct xt_counters) * private->number;
723 counters = vmalloc_node(countersize, numa_node_id());
725 if (counters == NULL)
726 return -ENOMEM;
728 /* First, sum counters... */
729 write_lock_bh(&table->lock);
730 get_counters(private, counters);
731 write_unlock_bh(&table->lock);
733 loc_cpu_entry = private->entries[raw_smp_processor_id()];
734 /* ... then copy entire thing ... */
735 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
736 ret = -EFAULT;
737 goto free_counters;
740 /* FIXME: use iterator macros --RR */
741 /* ... then go back and fix counters and names */
742 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
743 struct arpt_entry_target *t;
745 e = (struct arpt_entry *)(loc_cpu_entry + off);
746 if (copy_to_user(userptr + off
747 + offsetof(struct arpt_entry, counters),
748 &counters[num],
749 sizeof(counters[num])) != 0) {
750 ret = -EFAULT;
751 goto free_counters;
754 t = arpt_get_target(e);
755 if (copy_to_user(userptr + off + e->target_offset
756 + offsetof(struct arpt_entry_target,
757 u.user.name),
758 t->u.kernel.target->name,
759 strlen(t->u.kernel.target->name)+1) != 0) {
760 ret = -EFAULT;
761 goto free_counters;
765 free_counters:
766 vfree(counters);
767 return ret;
770 static int get_entries(const struct arpt_get_entries *entries,
771 struct arpt_get_entries __user *uptr)
773 int ret;
774 struct arpt_table *t;
776 t = xt_find_table_lock(NF_ARP, entries->name);
777 if (t && !IS_ERR(t)) {
778 struct xt_table_info *private = t->private;
779 duprintf("t->private->number = %u\n",
780 private->number);
781 if (entries->size == private->size)
782 ret = copy_entries_to_user(private->size,
783 t, uptr->entrytable);
784 else {
785 duprintf("get_entries: I've got %u not %u!\n",
786 private->size, entries->size);
787 ret = -EINVAL;
789 module_put(t->me);
790 xt_table_unlock(t);
791 } else
792 ret = t ? PTR_ERR(t) : -ENOENT;
794 return ret;
797 static int do_replace(void __user *user, unsigned int len)
799 int ret;
800 struct arpt_replace tmp;
801 struct arpt_table *t;
802 struct xt_table_info *newinfo, *oldinfo;
803 struct xt_counters *counters;
804 void *loc_cpu_entry, *loc_cpu_old_entry;
806 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
807 return -EFAULT;
809 /* Hack: Causes ipchains to give correct error msg --RR */
810 if (len != sizeof(tmp) + tmp.size)
811 return -ENOPROTOOPT;
813 /* overflow check */
814 if (tmp.size >= INT_MAX / num_possible_cpus())
815 return -ENOMEM;
816 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
817 return -ENOMEM;
819 newinfo = xt_alloc_table_info(tmp.size);
820 if (!newinfo)
821 return -ENOMEM;
823 /* choose the copy that is on our node/cpu */
824 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
825 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
826 tmp.size) != 0) {
827 ret = -EFAULT;
828 goto free_newinfo;
831 counters = vmalloc(tmp.num_counters * sizeof(struct xt_counters));
832 if (!counters) {
833 ret = -ENOMEM;
834 goto free_newinfo;
837 ret = translate_table(tmp.name, tmp.valid_hooks,
838 newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
839 tmp.hook_entry, tmp.underflow);
840 if (ret != 0)
841 goto free_newinfo_counters;
843 duprintf("arp_tables: Translated table\n");
845 t = try_then_request_module(xt_find_table_lock(NF_ARP, tmp.name),
846 "arptable_%s", tmp.name);
847 if (!t || IS_ERR(t)) {
848 ret = t ? PTR_ERR(t) : -ENOENT;
849 goto free_newinfo_counters_untrans;
852 /* You lied! */
853 if (tmp.valid_hooks != t->valid_hooks) {
854 duprintf("Valid hook crap: %08X vs %08X\n",
855 tmp.valid_hooks, t->valid_hooks);
856 ret = -EINVAL;
857 goto put_module;
860 oldinfo = xt_replace_table(t, tmp.num_counters, newinfo, &ret);
861 if (!oldinfo)
862 goto put_module;
864 /* Update module usage count based on number of rules */
865 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
866 oldinfo->number, oldinfo->initial_entries, newinfo->number);
867 if ((oldinfo->number > oldinfo->initial_entries) ||
868 (newinfo->number <= oldinfo->initial_entries))
869 module_put(t->me);
870 if ((oldinfo->number > oldinfo->initial_entries) &&
871 (newinfo->number <= oldinfo->initial_entries))
872 module_put(t->me);
874 /* Get the old counters. */
875 get_counters(oldinfo, counters);
876 /* Decrease module usage counts and free resource */
877 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
878 ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL);
880 xt_free_table_info(oldinfo);
881 if (copy_to_user(tmp.counters, counters,
882 sizeof(struct xt_counters) * tmp.num_counters) != 0)
883 ret = -EFAULT;
884 vfree(counters);
885 xt_table_unlock(t);
886 return ret;
888 put_module:
889 module_put(t->me);
890 xt_table_unlock(t);
891 free_newinfo_counters_untrans:
892 ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
893 free_newinfo_counters:
894 vfree(counters);
895 free_newinfo:
896 xt_free_table_info(newinfo);
897 return ret;
900 /* We're lazy, and add to the first CPU; overflow works its fey magic
901 * and everything is OK.
903 static inline int add_counter_to_entry(struct arpt_entry *e,
904 const struct xt_counters addme[],
905 unsigned int *i)
908 ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
910 (*i)++;
911 return 0;
914 static int do_add_counters(void __user *user, unsigned int len)
916 unsigned int i;
917 struct xt_counters_info tmp, *paddc;
918 struct arpt_table *t;
919 struct xt_table_info *private;
920 int ret = 0;
921 void *loc_cpu_entry;
923 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
924 return -EFAULT;
926 if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct xt_counters))
927 return -EINVAL;
929 paddc = vmalloc(len);
930 if (!paddc)
931 return -ENOMEM;
933 if (copy_from_user(paddc, user, len) != 0) {
934 ret = -EFAULT;
935 goto free;
938 t = xt_find_table_lock(NF_ARP, tmp.name);
939 if (!t || IS_ERR(t)) {
940 ret = t ? PTR_ERR(t) : -ENOENT;
941 goto free;
944 write_lock_bh(&t->lock);
945 private = t->private;
946 if (private->number != tmp.num_counters) {
947 ret = -EINVAL;
948 goto unlock_up_free;
951 i = 0;
952 /* Choose the copy that is on our node */
953 loc_cpu_entry = private->entries[smp_processor_id()];
954 ARPT_ENTRY_ITERATE(loc_cpu_entry,
955 private->size,
956 add_counter_to_entry,
957 paddc->counters,
958 &i);
959 unlock_up_free:
960 write_unlock_bh(&t->lock);
961 xt_table_unlock(t);
962 module_put(t->me);
963 free:
964 vfree(paddc);
966 return ret;
969 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
971 int ret;
973 if (!capable(CAP_NET_ADMIN))
974 return -EPERM;
976 switch (cmd) {
977 case ARPT_SO_SET_REPLACE:
978 ret = do_replace(user, len);
979 break;
981 case ARPT_SO_SET_ADD_COUNTERS:
982 ret = do_add_counters(user, len);
983 break;
985 default:
986 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
987 ret = -EINVAL;
990 return ret;
993 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
995 int ret;
997 if (!capable(CAP_NET_ADMIN))
998 return -EPERM;
1000 switch (cmd) {
1001 case ARPT_SO_GET_INFO: {
1002 char name[ARPT_TABLE_MAXNAMELEN];
1003 struct arpt_table *t;
1005 if (*len != sizeof(struct arpt_getinfo)) {
1006 duprintf("length %u != %Zu\n", *len,
1007 sizeof(struct arpt_getinfo));
1008 ret = -EINVAL;
1009 break;
1012 if (copy_from_user(name, user, sizeof(name)) != 0) {
1013 ret = -EFAULT;
1014 break;
1016 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
1018 t = try_then_request_module(xt_find_table_lock(NF_ARP, name),
1019 "arptable_%s", name);
1020 if (t && !IS_ERR(t)) {
1021 struct arpt_getinfo info;
1022 struct xt_table_info *private = t->private;
1024 info.valid_hooks = t->valid_hooks;
1025 memcpy(info.hook_entry, private->hook_entry,
1026 sizeof(info.hook_entry));
1027 memcpy(info.underflow, private->underflow,
1028 sizeof(info.underflow));
1029 info.num_entries = private->number;
1030 info.size = private->size;
1031 strcpy(info.name, name);
1033 if (copy_to_user(user, &info, *len) != 0)
1034 ret = -EFAULT;
1035 else
1036 ret = 0;
1037 xt_table_unlock(t);
1038 module_put(t->me);
1039 } else
1040 ret = t ? PTR_ERR(t) : -ENOENT;
1042 break;
1044 case ARPT_SO_GET_ENTRIES: {
1045 struct arpt_get_entries get;
1047 if (*len < sizeof(get)) {
1048 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
1049 ret = -EINVAL;
1050 } else if (copy_from_user(&get, user, sizeof(get)) != 0) {
1051 ret = -EFAULT;
1052 } else if (*len != sizeof(struct arpt_get_entries) + get.size) {
1053 duprintf("get_entries: %u != %Zu\n", *len,
1054 sizeof(struct arpt_get_entries) + get.size);
1055 ret = -EINVAL;
1056 } else
1057 ret = get_entries(&get, user);
1058 break;
1061 case ARPT_SO_GET_REVISION_TARGET: {
1062 struct xt_get_revision rev;
1064 if (*len != sizeof(rev)) {
1065 ret = -EINVAL;
1066 break;
1068 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1069 ret = -EFAULT;
1070 break;
1073 try_then_request_module(xt_find_revision(NF_ARP, rev.name,
1074 rev.revision, 1, &ret),
1075 "arpt_%s", rev.name);
1076 break;
1079 default:
1080 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1081 ret = -EINVAL;
1084 return ret;
1087 int arpt_register_table(struct arpt_table *table,
1088 const struct arpt_replace *repl)
1090 int ret;
1091 struct xt_table_info *newinfo;
1092 struct xt_table_info bootstrap
1093 = { 0, 0, 0, { 0 }, { 0 }, { } };
1094 void *loc_cpu_entry;
1096 newinfo = xt_alloc_table_info(repl->size);
1097 if (!newinfo) {
1098 ret = -ENOMEM;
1099 return ret;
1102 /* choose the copy on our node/cpu */
1103 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1104 memcpy(loc_cpu_entry, repl->entries, repl->size);
1106 ret = translate_table(table->name, table->valid_hooks,
1107 newinfo, loc_cpu_entry, repl->size,
1108 repl->num_entries,
1109 repl->hook_entry,
1110 repl->underflow);
1112 duprintf("arpt_register_table: translate table gives %d\n", ret);
1113 if (ret != 0) {
1114 xt_free_table_info(newinfo);
1115 return ret;
1118 ret = xt_register_table(table, &bootstrap, newinfo);
1119 if (ret != 0) {
1120 xt_free_table_info(newinfo);
1121 return ret;
1124 return 0;
1127 void arpt_unregister_table(struct arpt_table *table)
1129 struct xt_table_info *private;
1130 void *loc_cpu_entry;
1132 private = xt_unregister_table(table);
1134 /* Decrease module usage counts and free resources */
1135 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1136 ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size,
1137 cleanup_entry, NULL);
1138 xt_free_table_info(private);
1141 /* The built-in targets: standard (NULL) and error. */
1142 static struct arpt_target arpt_standard_target __read_mostly = {
1143 .name = ARPT_STANDARD_TARGET,
1144 .targetsize = sizeof(int),
1145 .family = NF_ARP,
1148 static struct arpt_target arpt_error_target __read_mostly = {
1149 .name = ARPT_ERROR_TARGET,
1150 .target = arpt_error,
1151 .targetsize = ARPT_FUNCTION_MAXNAMELEN,
1152 .family = NF_ARP,
1155 static struct nf_sockopt_ops arpt_sockopts = {
1156 .pf = PF_INET,
1157 .set_optmin = ARPT_BASE_CTL,
1158 .set_optmax = ARPT_SO_SET_MAX+1,
1159 .set = do_arpt_set_ctl,
1160 .get_optmin = ARPT_BASE_CTL,
1161 .get_optmax = ARPT_SO_GET_MAX+1,
1162 .get = do_arpt_get_ctl,
1163 .owner = THIS_MODULE,
1166 static int __init arp_tables_init(void)
1168 int ret;
1170 ret = xt_proto_init(NF_ARP);
1171 if (ret < 0)
1172 goto err1;
1174 /* Noone else will be downing sem now, so we won't sleep */
1175 ret = xt_register_target(&arpt_standard_target);
1176 if (ret < 0)
1177 goto err2;
1178 ret = xt_register_target(&arpt_error_target);
1179 if (ret < 0)
1180 goto err3;
1182 /* Register setsockopt */
1183 ret = nf_register_sockopt(&arpt_sockopts);
1184 if (ret < 0)
1185 goto err4;
1187 printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1188 return 0;
1190 err4:
1191 xt_unregister_target(&arpt_error_target);
1192 err3:
1193 xt_unregister_target(&arpt_standard_target);
1194 err2:
1195 xt_proto_fini(NF_ARP);
1196 err1:
1197 return ret;
1200 static void __exit arp_tables_fini(void)
1202 nf_unregister_sockopt(&arpt_sockopts);
1203 xt_unregister_target(&arpt_error_target);
1204 xt_unregister_target(&arpt_standard_target);
1205 xt_proto_fini(NF_ARP);
1208 EXPORT_SYMBOL(arpt_register_table);
1209 EXPORT_SYMBOL(arpt_unregister_table);
1210 EXPORT_SYMBOL(arpt_do_table);
1212 module_init(arp_tables_init);
1213 module_exit(arp_tables_fini);