1
2
3
4
5
6
7
8
9
10
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>
27
28#include <linux/netfilter/x_tables.h>
29#include <linux/netfilter_arp/arp_tables.h>
30#include "../../netfilter/xt_repldata.h"
31
32MODULE_LICENSE("GPL");
33MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
34MODULE_DESCRIPTION("arptables core");
35
36
37
38
39#ifdef DEBUG_ARP_TABLES
40#define dprintf(format, args...) printk(format , ## args)
41#else
42#define dprintf(format, args...)
43#endif
44
45#ifdef DEBUG_ARP_TABLES_USER
46#define duprintf(format, args...) printk(format , ## args)
47#else
48#define duprintf(format, args...)
49#endif
50
51#ifdef CONFIG_NETFILTER_DEBUG
52#define ARP_NF_ASSERT(x) WARN_ON(!(x))
53#else
54#define ARP_NF_ASSERT(x)
55#endif
56
57void *arpt_alloc_initial_table(const struct xt_table *info)
58{
59 return xt_alloc_initial_table(arpt, ARPT);
60}
61EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
62
63static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
64 const char *hdr_addr, int len)
65{
66 int i, ret;
67
68 if (len > ARPT_DEV_ADDR_LEN_MAX)
69 len = ARPT_DEV_ADDR_LEN_MAX;
70
71 ret = 0;
72 for (i = 0; i < len; i++)
73 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
74
75 return (ret != 0);
76}
77
78
79
80
81
82
83static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
84{
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;
93
94 for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
95 ret |= (a[i] ^ b[i]) & mask[i];
96#endif
97 return ret;
98}
99
100
101static 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)
106{
107 const char *arpptr = (char *)(arphdr + 1);
108 const char *src_devaddr, *tgt_devaddr;
109 __be32 src_ipaddr, tgt_ipaddr;
110 long ret;
111
112#define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
113
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;
120 }
121
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;
128 }
129
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;
136 }
137
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;
144 }
145
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));
153
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");
159
160 return 0;
161 }
162
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");
168
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;
180 }
181
182
183 ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
184
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;
190 }
191
192 ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
193
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;
199 }
200
201 return 1;
202#undef FWINV
203}
204
205static inline int arp_checkentry(const struct arpt_arp *arp)
206{
207 if (arp->flags & ~ARPT_F_MASK) {
208 duprintf("Unknown flag bits set: %08X\n",
209 arp->flags & ~ARPT_F_MASK);
210 return 0;
211 }
212 if (arp->invflags & ~ARPT_INV_MASK) {
213 duprintf("Unknown invflag bits set: %08X\n",
214 arp->invflags & ~ARPT_INV_MASK);
215 return 0;
216 }
217
218 return 1;
219}
220
221static unsigned int
222arpt_error(struct sk_buff *skb, const struct xt_action_param *par)
223{
224 if (net_ratelimit())
225 pr_err("arp_tables: error: '%s'\n",
226 (const char *)par->targinfo);
227
228 return NF_DROP;
229}
230
231static inline const struct arpt_entry_target *
232arpt_get_target_c(const struct arpt_entry *e)
233{
234 return arpt_get_target((struct arpt_entry *)e);
235}
236
237static inline struct arpt_entry *
238get_entry(const void *base, unsigned int offset)
239{
240 return (struct arpt_entry *)(base + offset);
241}
242
243static inline __pure
244struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
245{
246 return (void *)entry + entry->next_offset;
247}
248
249unsigned 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)
254{
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;
263
264 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
265 return NF_DROP;
266
267 indev = in ? in->name : nulldevname;
268 outdev = out ? out->name : nulldevname;
269
270 xt_info_rdlock_bh();
271 private = table->private;
272 table_base = private->entries[smp_processor_id()];
273
274 e = get_entry(table_base, private->hook_entry[hook]);
275 back = get_entry(table_base, private->underflow[hook]);
276
277 acpar.in = in;
278 acpar.out = out;
279 acpar.hooknum = hook;
280 acpar.family = NFPROTO_ARP;
281 acpar.hotdrop = false;
282
283 arp = arp_hdr(skb);
284 do {
285 const struct arpt_entry_target *t;
286 int hdr_len;
287
288 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
289 e = arpt_next_entry(e);
290 continue;
291 }
292
293 hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
294 (2 * skb->dev->addr_len);
295 ADD_COUNTER(e->counters, hdr_len, 1);
296
297 t = arpt_get_target_c(e);
298
299
300 if (!t->u.kernel.target->target) {
301 int v;
302
303 v = ((struct arpt_standard_target *)t)->verdict;
304 if (v < 0) {
305
306 if (v != ARPT_RETURN) {
307 verdict = (unsigned)(-v) - 1;
308 break;
309 }
310 e = back;
311 back = get_entry(table_base, back->comefrom);
312 continue;
313 }
314 if (table_base + v
315 != arpt_next_entry(e)) {
316
317 struct arpt_entry *next = arpt_next_entry(e);
318 next->comefrom = (void *)back - table_base;
319
320
321 back = next;
322 }
323
324 e = get_entry(table_base, v);
325 continue;
326 }
327
328
329
330
331 acpar.target = t->u.kernel.target;
332 acpar.targinfo = t->data;
333 verdict = t->u.kernel.target->target(skb, &acpar);
334
335
336 arp = arp_hdr(skb);
337
338 if (verdict == ARPT_CONTINUE)
339 e = arpt_next_entry(e);
340 else
341
342 break;
343 } while (!acpar.hotdrop);
344 xt_info_rdunlock_bh();
345
346 if (acpar.hotdrop)
347 return NF_DROP;
348 else
349 return verdict;
350}
351
352
353static inline bool unconditional(const struct arpt_arp *arp)
354{
355 static const struct arpt_arp uncond;
356
357 return memcmp(arp, &uncond, sizeof(uncond)) == 0;
358}
359
360
361
362
363static int mark_source_chains(const struct xt_table_info *newinfo,
364 unsigned int valid_hooks, void *entry0)
365{
366 unsigned int hook;
367
368
369
370
371 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
372 unsigned int pos = newinfo->hook_entry[hook];
373 struct arpt_entry *e
374 = (struct arpt_entry *)(entry0 + pos);
375
376 if (!(valid_hooks & (1 << hook)))
377 continue;
378
379
380 e->counters.pcnt = pos;
381
382 for (;;) {
383 const struct arpt_standard_target *t
384 = (void *)arpt_get_target_c(e);
385 int visited = e->comefrom & (1 << hook);
386
387 if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
388 pr_notice("arptables: loop hook %u pos %u %08X.\n",
389 hook, pos, e->comefrom);
390 return 0;
391 }
392 e->comefrom
393 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
394
395
396 if ((e->target_offset == sizeof(struct arpt_entry) &&
397 (strcmp(t->target.u.user.name,
398 ARPT_STANDARD_TARGET) == 0) &&
399 t->verdict < 0 && unconditional(&e->arp)) ||
400 visited) {
401 unsigned int oldpos, size;
402
403 if ((strcmp(t->target.u.user.name,
404 ARPT_STANDARD_TARGET) == 0) &&
405 t->verdict < -NF_MAX_VERDICT - 1) {
406 duprintf("mark_source_chains: bad "
407 "negative verdict (%i)\n",
408 t->verdict);
409 return 0;
410 }
411
412
413
414
415 do {
416 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
417 oldpos = pos;
418 pos = e->counters.pcnt;
419 e->counters.pcnt = 0;
420
421
422 if (pos == oldpos)
423 goto next;
424
425 e = (struct arpt_entry *)
426 (entry0 + pos);
427 } while (oldpos == pos + e->next_offset);
428
429
430 size = e->next_offset;
431 e = (struct arpt_entry *)
432 (entry0 + pos + size);
433 e->counters.pcnt = pos;
434 pos += size;
435 } else {
436 int newpos = t->verdict;
437
438 if (strcmp(t->target.u.user.name,
439 ARPT_STANDARD_TARGET) == 0 &&
440 newpos >= 0) {
441 if (newpos > newinfo->size -
442 sizeof(struct arpt_entry)) {
443 duprintf("mark_source_chains: "
444 "bad verdict (%i)\n",
445 newpos);
446 return 0;
447 }
448
449
450 duprintf("Jump rule %u -> %u\n",
451 pos, newpos);
452 } else {
453
454 newpos = pos + e->next_offset;
455 }
456 e = (struct arpt_entry *)
457 (entry0 + newpos);
458 e->counters.pcnt = pos;
459 pos = newpos;
460 }
461 }
462 next:
463 duprintf("Finished chain %u\n", hook);
464 }
465 return 1;
466}
467
468static inline int check_entry(const struct arpt_entry *e, const char *name)
469{
470 const struct arpt_entry_target *t;
471
472 if (!arp_checkentry(&e->arp)) {
473 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
474 return -EINVAL;
475 }
476
477 if (e->target_offset + sizeof(struct arpt_entry_target) > e->next_offset)
478 return -EINVAL;
479
480 t = arpt_get_target_c(e);
481 if (e->target_offset + t->u.target_size > e->next_offset)
482 return -EINVAL;
483
484 return 0;
485}
486
487static inline int check_target(struct arpt_entry *e, const char *name)
488{
489 struct arpt_entry_target *t = arpt_get_target(e);
490 int ret;
491 struct xt_tgchk_param par = {
492 .table = name,
493 .entryinfo = e,
494 .target = t->u.kernel.target,
495 .targinfo = t->data,
496 .hook_mask = e->comefrom,
497 .family = NFPROTO_ARP,
498 };
499
500 ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
501 if (ret < 0) {
502 duprintf("arp_tables: check failed for `%s'.\n",
503 t->u.kernel.target->name);
504 return ret;
505 }
506 return 0;
507}
508
509static inline int
510find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
511{
512 struct arpt_entry_target *t;
513 struct xt_target *target;
514 int ret;
515
516 ret = check_entry(e, name);
517 if (ret)
518 return ret;
519
520 t = arpt_get_target(e);
521 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
522 t->u.user.revision);
523 if (IS_ERR(target)) {
524 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
525 ret = PTR_ERR(target);
526 goto out;
527 }
528 t->u.kernel.target = target;
529
530 ret = check_target(e, name);
531 if (ret)
532 goto err;
533 return 0;
534err:
535 module_put(t->u.kernel.target->me);
536out:
537 return ret;
538}
539
540static bool check_underflow(const struct arpt_entry *e)
541{
542 const struct arpt_entry_target *t;
543 unsigned int verdict;
544
545 if (!unconditional(&e->arp))
546 return false;
547 t = arpt_get_target_c(e);
548 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
549 return false;
550 verdict = ((struct arpt_standard_target *)t)->verdict;
551 verdict = -verdict - 1;
552 return verdict == NF_DROP || verdict == NF_ACCEPT;
553}
554
555static inline int check_entry_size_and_hooks(struct arpt_entry *e,
556 struct xt_table_info *newinfo,
557 const unsigned char *base,
558 const unsigned char *limit,
559 const unsigned int *hook_entries,
560 const unsigned int *underflows,
561 unsigned int valid_hooks)
562{
563 unsigned int h;
564
565 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
566 (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
567 duprintf("Bad offset %p\n", e);
568 return -EINVAL;
569 }
570
571 if (e->next_offset
572 < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
573 duprintf("checking: element %p size %u\n",
574 e, e->next_offset);
575 return -EINVAL;
576 }
577
578
579 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
580 if (!(valid_hooks & (1 << h)))
581 continue;
582 if ((unsigned char *)e - base == hook_entries[h])
583 newinfo->hook_entry[h] = hook_entries[h];
584 if ((unsigned char *)e - base == underflows[h]) {
585 if (!check_underflow(e)) {
586 pr_err("Underflows must be unconditional and "
587 "use the STANDARD target with "
588 "ACCEPT/DROP\n");
589 return -EINVAL;
590 }
591 newinfo->underflow[h] = underflows[h];
592 }
593 }
594
595
596 e->counters = ((struct xt_counters) { 0, 0 });
597 e->comefrom = 0;
598 return 0;
599}
600
601static inline void cleanup_entry(struct arpt_entry *e)
602{
603 struct xt_tgdtor_param par;
604 struct arpt_entry_target *t;
605
606 t = arpt_get_target(e);
607 par.target = t->u.kernel.target;
608 par.targinfo = t->data;
609 par.family = NFPROTO_ARP;
610 if (par.target->destroy != NULL)
611 par.target->destroy(&par);
612 module_put(par.target->me);
613}
614
615
616
617
618static int translate_table(struct xt_table_info *newinfo, void *entry0,
619 const struct arpt_replace *repl)
620{
621 struct arpt_entry *iter;
622 unsigned int i;
623 int ret = 0;
624
625 newinfo->size = repl->size;
626 newinfo->number = repl->num_entries;
627
628
629 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
630 newinfo->hook_entry[i] = 0xFFFFFFFF;
631 newinfo->underflow[i] = 0xFFFFFFFF;
632 }
633
634 duprintf("translate_table: size %u\n", newinfo->size);
635 i = 0;
636
637
638 xt_entry_foreach(iter, entry0, newinfo->size) {
639 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
640 entry0 + repl->size,
641 repl->hook_entry,
642 repl->underflow,
643 repl->valid_hooks);
644 if (ret != 0)
645 break;
646 ++i;
647 if (strcmp(arpt_get_target(iter)->u.user.name,
648 XT_ERROR_TARGET) == 0)
649 ++newinfo->stacksize;
650 }
651 duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
652 if (ret != 0)
653 return ret;
654
655 if (i != repl->num_entries) {
656 duprintf("translate_table: %u not %u entries\n",
657 i, repl->num_entries);
658 return -EINVAL;
659 }
660
661
662 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
663
664 if (!(repl->valid_hooks & (1 << i)))
665 continue;
666 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
667 duprintf("Invalid hook entry %u %u\n",
668 i, repl->hook_entry[i]);
669 return -EINVAL;
670 }
671 if (newinfo->underflow[i] == 0xFFFFFFFF) {
672 duprintf("Invalid underflow %u %u\n",
673 i, repl->underflow[i]);
674 return -EINVAL;
675 }
676 }
677
678 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0)) {
679 duprintf("Looping hook\n");
680 return -ELOOP;
681 }
682
683
684 i = 0;
685 xt_entry_foreach(iter, entry0, newinfo->size) {
686 ret = find_check_entry(iter, repl->name, repl->size);
687 if (ret != 0)
688 break;
689 ++i;
690 }
691
692 if (ret != 0) {
693 xt_entry_foreach(iter, entry0, newinfo->size) {
694 if (i-- == 0)
695 break;
696 cleanup_entry(iter);
697 }
698 return ret;
699 }
700
701
702 for_each_possible_cpu(i) {
703 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
704 memcpy(newinfo->entries[i], entry0, newinfo->size);
705 }
706
707 return ret;
708}
709
710static void get_counters(const struct xt_table_info *t,
711 struct xt_counters counters[])
712{
713 struct arpt_entry *iter;
714 unsigned int cpu;
715 unsigned int i;
716 unsigned int curcpu;
717
718
719
720
721
722
723
724
725 local_bh_disable();
726 curcpu = smp_processor_id();
727
728 i = 0;
729 xt_entry_foreach(iter, t->entries[curcpu], t->size) {
730 SET_COUNTER(counters[i], iter->counters.bcnt,
731 iter->counters.pcnt);
732 ++i;
733 }
734
735 for_each_possible_cpu(cpu) {
736 if (cpu == curcpu)
737 continue;
738 i = 0;
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;
744 }
745 xt_info_wrunlock(cpu);
746 }
747 local_bh_enable();
748}
749
750static struct xt_counters *alloc_counters(const struct xt_table *table)
751{
752 unsigned int countersize;
753 struct xt_counters *counters;
754 const struct xt_table_info *private = table->private;
755
756
757
758
759
760 countersize = sizeof(struct xt_counters) * private->number;
761 counters = vmalloc_node(countersize, numa_node_id());
762
763 if (counters == NULL)
764 return ERR_PTR(-ENOMEM);
765
766 get_counters(private, counters);
767
768 return counters;
769}
770
771static int copy_entries_to_user(unsigned int total_size,
772 const struct xt_table *table,
773 void __user *userptr)
774{
775 unsigned int off, num;
776 const struct arpt_entry *e;
777 struct xt_counters *counters;
778 struct xt_table_info *private = table->private;
779 int ret = 0;
780 void *loc_cpu_entry;
781
782 counters = alloc_counters(table);
783 if (IS_ERR(counters))
784 return PTR_ERR(counters);
785
786 loc_cpu_entry = private->entries[raw_smp_processor_id()];
787
788 if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
789 ret = -EFAULT;
790 goto free_counters;
791 }
792
793
794
795 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
796 const struct arpt_entry_target *t;
797
798 e = (struct arpt_entry *)(loc_cpu_entry + off);
799 if (copy_to_user(userptr + off
800 + offsetof(struct arpt_entry, counters),
801 &counters[num],
802 sizeof(counters[num])) != 0) {
803 ret = -EFAULT;
804 goto free_counters;
805 }
806
807 t = arpt_get_target_c(e);
808 if (copy_to_user(userptr + off + e->target_offset
809 + offsetof(struct arpt_entry_target,
810 u.user.name),
811 t->u.kernel.target->name,
812 strlen(t->u.kernel.target->name)+1) != 0) {
813 ret = -EFAULT;
814 goto free_counters;
815 }
816 }
817
818 free_counters:
819 vfree(counters);
820 return ret;
821}
822
823#ifdef CONFIG_COMPAT
824static void compat_standard_from_user(void *dst, const void *src)
825{
826 int v = *(compat_int_t *)src;
827
828 if (v > 0)
829 v += xt_compat_calc_jump(NFPROTO_ARP, v);
830 memcpy(dst, &v, sizeof(v));
831}
832
833static int compat_standard_to_user(void __user *dst, const void *src)
834{
835 compat_int_t cv = *(int *)src;
836
837 if (cv > 0)
838 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
839 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
840}
841
842static int compat_calc_entry(const struct arpt_entry *e,
843 const struct xt_table_info *info,
844 const void *base, struct xt_table_info *newinfo)
845{
846 const struct arpt_entry_target *t;
847 unsigned int entry_offset;
848 int off, i, ret;
849
850 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
851 entry_offset = (void *)e - base;
852
853 t = arpt_get_target_c(e);
854 off += xt_compat_target_offset(t->u.kernel.target);
855 newinfo->size -= off;
856 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
857 if (ret)
858 return ret;
859
860 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
861 if (info->hook_entry[i] &&
862 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
863 newinfo->hook_entry[i] -= off;
864 if (info->underflow[i] &&
865 (e < (struct arpt_entry *)(base + info->underflow[i])))
866 newinfo->underflow[i] -= off;
867 }
868 return 0;
869}
870
871static int compat_table_info(const struct xt_table_info *info,
872 struct xt_table_info *newinfo)
873{
874 struct arpt_entry *iter;
875 void *loc_cpu_entry;
876 int ret;
877
878 if (!newinfo || !info)
879 return -EINVAL;
880
881
882 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
883 newinfo->initial_entries = 0;
884 loc_cpu_entry = info->entries[raw_smp_processor_id()];
885 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
886 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
887 if (ret != 0)
888 return ret;
889 }
890 return 0;
891}
892#endif
893
894static int get_info(struct net *net, void __user *user,
895 const int *len, int compat)
896{
897 char name[ARPT_TABLE_MAXNAMELEN];
898 struct xt_table *t;
899 int ret;
900
901 if (*len != sizeof(struct arpt_getinfo)) {
902 duprintf("length %u != %Zu\n", *len,
903 sizeof(struct arpt_getinfo));
904 return -EINVAL;
905 }
906
907 if (copy_from_user(name, user, sizeof(name)) != 0)
908 return -EFAULT;
909
910 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
911#ifdef CONFIG_COMPAT
912 if (compat)
913 xt_compat_lock(NFPROTO_ARP);
914#endif
915 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
916 "arptable_%s", name);
917 if (t && !IS_ERR(t)) {
918 struct arpt_getinfo info;
919 const struct xt_table_info *private = t->private;
920#ifdef CONFIG_COMPAT
921 struct xt_table_info tmp;
922
923 if (compat) {
924 ret = compat_table_info(private, &tmp);
925 xt_compat_flush_offsets(NFPROTO_ARP);
926 private = &tmp;
927 }
928#endif
929 info.valid_hooks = t->valid_hooks;
930 memcpy(info.hook_entry, private->hook_entry,
931 sizeof(info.hook_entry));
932 memcpy(info.underflow, private->underflow,
933 sizeof(info.underflow));
934 info.num_entries = private->number;
935 info.size = private->size;
936 strcpy(info.name, name);
937
938 if (copy_to_user(user, &info, *len) != 0)
939 ret = -EFAULT;
940 else
941 ret = 0;
942 xt_table_unlock(t);
943 module_put(t->me);
944 } else
945 ret = t ? PTR_ERR(t) : -ENOENT;
946#ifdef CONFIG_COMPAT
947 if (compat)
948 xt_compat_unlock(NFPROTO_ARP);
949#endif
950 return ret;
951}
952
953static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
954 const int *len)
955{
956 int ret;
957 struct arpt_get_entries get;
958 struct xt_table *t;
959
960 if (*len < sizeof(get)) {
961 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
962 return -EINVAL;
963 }
964 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
965 return -EFAULT;
966 if (*len != sizeof(struct arpt_get_entries) + get.size) {
967 duprintf("get_entries: %u != %Zu\n", *len,
968 sizeof(struct arpt_get_entries) + get.size);
969 return -EINVAL;
970 }
971
972 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
973 if (t && !IS_ERR(t)) {
974 const struct xt_table_info *private = t->private;
975
976 duprintf("t->private->number = %u\n",
977 private->number);
978 if (get.size == private->size)
979 ret = copy_entries_to_user(private->size,
980 t, uptr->entrytable);
981 else {
982 duprintf("get_entries: I've got %u not %u!\n",
983 private->size, get.size);
984 ret = -EAGAIN;
985 }
986 module_put(t->me);
987 xt_table_unlock(t);
988 } else
989 ret = t ? PTR_ERR(t) : -ENOENT;
990
991 return ret;
992}
993
994static int __do_replace(struct net *net, const char *name,
995 unsigned int valid_hooks,
996 struct xt_table_info *newinfo,
997 unsigned int num_counters,
998 void __user *counters_ptr)
999{
1000 int ret;
1001 struct xt_table *t;
1002 struct xt_table_info *oldinfo;
1003 struct xt_counters *counters;
1004 void *loc_cpu_old_entry;
1005 struct arpt_entry *iter;
1006
1007 ret = 0;
1008 counters = vmalloc_node(num_counters * sizeof(struct xt_counters),
1009 numa_node_id());
1010 if (!counters) {
1011 ret = -ENOMEM;
1012 goto out;
1013 }
1014
1015 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
1016 "arptable_%s", name);
1017 if (!t || IS_ERR(t)) {
1018 ret = t ? PTR_ERR(t) : -ENOENT;
1019 goto free_newinfo_counters_untrans;
1020 }
1021
1022
1023 if (valid_hooks != t->valid_hooks) {
1024 duprintf("Valid hook crap: %08X vs %08X\n",
1025 valid_hooks, t->valid_hooks);
1026 ret = -EINVAL;
1027 goto put_module;
1028 }
1029
1030 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1031 if (!oldinfo)
1032 goto put_module;
1033
1034
1035 duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1036 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1037 if ((oldinfo->number > oldinfo->initial_entries) ||
1038 (newinfo->number <= oldinfo->initial_entries))
1039 module_put(t->me);
1040 if ((oldinfo->number > oldinfo->initial_entries) &&
1041 (newinfo->number <= oldinfo->initial_entries))
1042 module_put(t->me);
1043
1044
1045 get_counters(oldinfo, counters);
1046
1047
1048 loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1049 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
1050 cleanup_entry(iter);
1051
1052 xt_free_table_info(oldinfo);
1053 if (copy_to_user(counters_ptr, counters,
1054 sizeof(struct xt_counters) * num_counters) != 0)
1055 ret = -EFAULT;
1056 vfree(counters);
1057 xt_table_unlock(t);
1058 return ret;
1059
1060 put_module:
1061 module_put(t->me);
1062 xt_table_unlock(t);
1063 free_newinfo_counters_untrans:
1064 vfree(counters);
1065 out:
1066 return ret;
1067}
1068
1069static int do_replace(struct net *net, const void __user *user,
1070 unsigned int len)
1071{
1072 int ret;
1073 struct arpt_replace tmp;
1074 struct xt_table_info *newinfo;
1075 void *loc_cpu_entry;
1076 struct arpt_entry *iter;
1077
1078 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1079 return -EFAULT;
1080
1081
1082 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1083 return -ENOMEM;
1084
1085 newinfo = xt_alloc_table_info(tmp.size);
1086 if (!newinfo)
1087 return -ENOMEM;
1088
1089
1090 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1091 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1092 tmp.size) != 0) {
1093 ret = -EFAULT;
1094 goto free_newinfo;
1095 }
1096
1097 ret = translate_table(newinfo, loc_cpu_entry, &tmp);
1098 if (ret != 0)
1099 goto free_newinfo;
1100
1101 duprintf("arp_tables: Translated table\n");
1102
1103 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1104 tmp.num_counters, tmp.counters);
1105 if (ret)
1106 goto free_newinfo_untrans;
1107 return 0;
1108
1109 free_newinfo_untrans:
1110 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1111 cleanup_entry(iter);
1112 free_newinfo:
1113 xt_free_table_info(newinfo);
1114 return ret;
1115}
1116
1117static int do_add_counters(struct net *net, const void __user *user,
1118 unsigned int len, int compat)
1119{
1120 unsigned int i, curcpu;
1121 struct xt_counters_info tmp;
1122 struct xt_counters *paddc;
1123 unsigned int num_counters;
1124 const char *name;
1125 int size;
1126 void *ptmp;
1127 struct xt_table *t;
1128 const struct xt_table_info *private;
1129 int ret = 0;
1130 void *loc_cpu_entry;
1131 struct arpt_entry *iter;
1132#ifdef CONFIG_COMPAT
1133 struct compat_xt_counters_info compat_tmp;
1134
1135 if (compat) {
1136 ptmp = &compat_tmp;
1137 size = sizeof(struct compat_xt_counters_info);
1138 } else
1139#endif
1140 {
1141 ptmp = &tmp;
1142 size = sizeof(struct xt_counters_info);
1143 }
1144
1145 if (copy_from_user(ptmp, user, size) != 0)
1146 return -EFAULT;
1147
1148#ifdef CONFIG_COMPAT
1149 if (compat) {
1150 num_counters = compat_tmp.num_counters;
1151 name = compat_tmp.name;
1152 } else
1153#endif
1154 {
1155 num_counters = tmp.num_counters;
1156 name = tmp.name;
1157 }
1158
1159 if (len != size + num_counters * sizeof(struct xt_counters))
1160 return -EINVAL;
1161
1162 paddc = vmalloc_node(len - size, numa_node_id());
1163 if (!paddc)
1164 return -ENOMEM;
1165
1166 if (copy_from_user(paddc, user + size, len - size) != 0) {
1167 ret = -EFAULT;
1168 goto free;
1169 }
1170
1171 t = xt_find_table_lock(net, NFPROTO_ARP, name);
1172 if (!t || IS_ERR(t)) {
1173 ret = t ? PTR_ERR(t) : -ENOENT;
1174 goto free;
1175 }
1176
1177 local_bh_disable();
1178 private = t->private;
1179 if (private->number != num_counters) {
1180 ret = -EINVAL;
1181 goto unlock_up_free;
1182 }
1183
1184 i = 0;
1185
1186 curcpu = smp_processor_id();
1187 loc_cpu_entry = private->entries[curcpu];
1188 xt_info_wrlock(curcpu);
1189 xt_entry_foreach(iter, loc_cpu_entry, private->size) {
1190 ADD_COUNTER(iter->counters, paddc[i].bcnt, paddc[i].pcnt);
1191 ++i;
1192 }
1193 xt_info_wrunlock(curcpu);
1194 unlock_up_free:
1195 local_bh_enable();
1196 xt_table_unlock(t);
1197 module_put(t->me);
1198 free:
1199 vfree(paddc);
1200
1201 return ret;
1202}
1203
1204#ifdef CONFIG_COMPAT
1205static inline void compat_release_entry(struct compat_arpt_entry *e)
1206{
1207 struct arpt_entry_target *t;
1208
1209 t = compat_arpt_get_target(e);
1210 module_put(t->u.kernel.target->me);
1211}
1212
1213static inline int
1214check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1215 struct xt_table_info *newinfo,
1216 unsigned int *size,
1217 const unsigned char *base,
1218 const unsigned char *limit,
1219 const unsigned int *hook_entries,
1220 const unsigned int *underflows,
1221 const char *name)
1222{
1223 struct arpt_entry_target *t;
1224 struct xt_target *target;
1225 unsigned int entry_offset;
1226 int ret, off, h;
1227
1228 duprintf("check_compat_entry_size_and_hooks %p\n", e);
1229 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1230 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
1231 duprintf("Bad offset %p, limit = %p\n", e, limit);
1232 return -EINVAL;
1233 }
1234
1235 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1236 sizeof(struct compat_xt_entry_target)) {
1237 duprintf("checking: element %p size %u\n",
1238 e, e->next_offset);
1239 return -EINVAL;
1240 }
1241
1242
1243 ret = check_entry((struct arpt_entry *)e, name);
1244 if (ret)
1245 return ret;
1246
1247 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1248 entry_offset = (void *)e - (void *)base;
1249
1250 t = compat_arpt_get_target(e);
1251 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1252 t->u.user.revision);
1253 if (IS_ERR(target)) {
1254 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1255 t->u.user.name);
1256 ret = PTR_ERR(target);
1257 goto out;
1258 }
1259 t->u.kernel.target = target;
1260
1261 off += xt_compat_target_offset(target);
1262 *size += off;
1263 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1264 if (ret)
1265 goto release_target;
1266
1267
1268 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1269 if ((unsigned char *)e - base == hook_entries[h])
1270 newinfo->hook_entry[h] = hook_entries[h];
1271 if ((unsigned char *)e - base == underflows[h])
1272 newinfo->underflow[h] = underflows[h];
1273 }
1274
1275
1276 memset(&e->counters, 0, sizeof(e->counters));
1277 e->comefrom = 0;
1278 return 0;
1279
1280release_target:
1281 module_put(t->u.kernel.target->me);
1282out:
1283 return ret;
1284}
1285
1286static int
1287compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1288 unsigned int *size, const char *name,
1289 struct xt_table_info *newinfo, unsigned char *base)
1290{
1291 struct arpt_entry_target *t;
1292 struct xt_target *target;
1293 struct arpt_entry *de;
1294 unsigned int origsize;
1295 int ret, h;
1296
1297 ret = 0;
1298 origsize = *size;
1299 de = (struct arpt_entry *)*dstptr;
1300 memcpy(de, e, sizeof(struct arpt_entry));
1301 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1302
1303 *dstptr += sizeof(struct arpt_entry);
1304 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1305
1306 de->target_offset = e->target_offset - (origsize - *size);
1307 t = compat_arpt_get_target(e);
1308 target = t->u.kernel.target;
1309 xt_compat_target_from_user(t, dstptr, size);
1310
1311 de->next_offset = e->next_offset - (origsize - *size);
1312 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1313 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1314 newinfo->hook_entry[h] -= origsize - *size;
1315 if ((unsigned char *)de - base < newinfo->underflow[h])
1316 newinfo->underflow[h] -= origsize - *size;
1317 }
1318 return ret;
1319}
1320
1321static int translate_compat_table(const char *name,
1322 unsigned int valid_hooks,
1323 struct xt_table_info **pinfo,
1324 void **pentry0,
1325 unsigned int total_size,
1326 unsigned int number,
1327 unsigned int *hook_entries,
1328 unsigned int *underflows)
1329{
1330 unsigned int i, j;
1331 struct xt_table_info *newinfo, *info;
1332 void *pos, *entry0, *entry1;
1333 struct compat_arpt_entry *iter0;
1334 struct arpt_entry *iter1;
1335 unsigned int size;
1336 int ret = 0;
1337
1338 info = *pinfo;
1339 entry0 = *pentry0;
1340 size = total_size;
1341 info->number = number;
1342
1343
1344 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1345 info->hook_entry[i] = 0xFFFFFFFF;
1346 info->underflow[i] = 0xFFFFFFFF;
1347 }
1348
1349 duprintf("translate_compat_table: size %u\n", info->size);
1350 j = 0;
1351 xt_compat_lock(NFPROTO_ARP);
1352
1353 xt_entry_foreach(iter0, entry0, total_size) {
1354 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1355 entry0,
1356 entry0 + total_size,
1357 hook_entries,
1358 underflows,
1359 name);
1360 if (ret != 0)
1361 goto out_unlock;
1362 ++j;
1363 }
1364
1365 ret = -EINVAL;
1366 if (j != number) {
1367 duprintf("translate_compat_table: %u not %u entries\n",
1368 j, number);
1369 goto out_unlock;
1370 }
1371
1372
1373 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1374
1375 if (!(valid_hooks & (1 << i)))
1376 continue;
1377 if (info->hook_entry[i] == 0xFFFFFFFF) {
1378 duprintf("Invalid hook entry %u %u\n",
1379 i, hook_entries[i]);
1380 goto out_unlock;
1381 }
1382 if (info->underflow[i] == 0xFFFFFFFF) {
1383 duprintf("Invalid underflow %u %u\n",
1384 i, underflows[i]);
1385 goto out_unlock;
1386 }
1387 }
1388
1389 ret = -ENOMEM;
1390 newinfo = xt_alloc_table_info(size);
1391 if (!newinfo)
1392 goto out_unlock;
1393
1394 newinfo->number = number;
1395 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1396 newinfo->hook_entry[i] = info->hook_entry[i];
1397 newinfo->underflow[i] = info->underflow[i];
1398 }
1399 entry1 = newinfo->entries[raw_smp_processor_id()];
1400 pos = entry1;
1401 size = total_size;
1402 xt_entry_foreach(iter0, entry0, total_size) {
1403 ret = compat_copy_entry_from_user(iter0, &pos, &size,
1404 name, newinfo, entry1);
1405 if (ret != 0)
1406 break;
1407 }
1408 xt_compat_flush_offsets(NFPROTO_ARP);
1409 xt_compat_unlock(NFPROTO_ARP);
1410 if (ret)
1411 goto free_newinfo;
1412
1413 ret = -ELOOP;
1414 if (!mark_source_chains(newinfo, valid_hooks, entry1))
1415 goto free_newinfo;
1416
1417 i = 0;
1418 xt_entry_foreach(iter1, entry1, newinfo->size) {
1419 ret = check_target(iter1, name);
1420 if (ret != 0)
1421 break;
1422 ++i;
1423 if (strcmp(arpt_get_target(iter1)->u.user.name,
1424 XT_ERROR_TARGET) == 0)
1425 ++newinfo->stacksize;
1426 }
1427 if (ret) {
1428
1429
1430
1431
1432
1433 int skip = i;
1434 j -= i;
1435 xt_entry_foreach(iter0, entry0, newinfo->size) {
1436 if (skip-- > 0)
1437 continue;
1438 if (j-- == 0)
1439 break;
1440 compat_release_entry(iter0);
1441 }
1442 xt_entry_foreach(iter1, entry1, newinfo->size) {
1443 if (i-- == 0)
1444 break;
1445 cleanup_entry(iter1);
1446 }
1447 xt_free_table_info(newinfo);
1448 return ret;
1449 }
1450
1451
1452 for_each_possible_cpu(i)
1453 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1454 memcpy(newinfo->entries[i], entry1, newinfo->size);
1455
1456 *pinfo = newinfo;
1457 *pentry0 = entry1;
1458 xt_free_table_info(info);
1459 return 0;
1460
1461free_newinfo:
1462 xt_free_table_info(newinfo);
1463out:
1464 xt_entry_foreach(iter0, entry0, total_size) {
1465 if (j-- == 0)
1466 break;
1467 compat_release_entry(iter0);
1468 }
1469 return ret;
1470out_unlock:
1471 xt_compat_flush_offsets(NFPROTO_ARP);
1472 xt_compat_unlock(NFPROTO_ARP);
1473 goto out;
1474}
1475
1476struct compat_arpt_replace {
1477 char name[ARPT_TABLE_MAXNAMELEN];
1478 u32 valid_hooks;
1479 u32 num_entries;
1480 u32 size;
1481 u32 hook_entry[NF_ARP_NUMHOOKS];
1482 u32 underflow[NF_ARP_NUMHOOKS];
1483 u32 num_counters;
1484 compat_uptr_t counters;
1485 struct compat_arpt_entry entries[0];
1486};
1487
1488static int compat_do_replace(struct net *net, void __user *user,
1489 unsigned int len)
1490{
1491 int ret;
1492 struct compat_arpt_replace tmp;
1493 struct xt_table_info *newinfo;
1494 void *loc_cpu_entry;
1495 struct arpt_entry *iter;
1496
1497 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1498 return -EFAULT;
1499
1500
1501 if (tmp.size >= INT_MAX / num_possible_cpus())
1502 return -ENOMEM;
1503 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1504 return -ENOMEM;
1505
1506 newinfo = xt_alloc_table_info(tmp.size);
1507 if (!newinfo)
1508 return -ENOMEM;
1509
1510
1511 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1512 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1513 ret = -EFAULT;
1514 goto free_newinfo;
1515 }
1516
1517 ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1518 &newinfo, &loc_cpu_entry, tmp.size,
1519 tmp.num_entries, tmp.hook_entry,
1520 tmp.underflow);
1521 if (ret != 0)
1522 goto free_newinfo;
1523
1524 duprintf("compat_do_replace: Translated table\n");
1525
1526 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1527 tmp.num_counters, compat_ptr(tmp.counters));
1528 if (ret)
1529 goto free_newinfo_untrans;
1530 return 0;
1531
1532 free_newinfo_untrans:
1533 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1534 cleanup_entry(iter);
1535 free_newinfo:
1536 xt_free_table_info(newinfo);
1537 return ret;
1538}
1539
1540static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1541 unsigned int len)
1542{
1543 int ret;
1544
1545 if (!capable(CAP_NET_ADMIN))
1546 return -EPERM;
1547
1548 switch (cmd) {
1549 case ARPT_SO_SET_REPLACE:
1550 ret = compat_do_replace(sock_net(sk), user, len);
1551 break;
1552
1553 case ARPT_SO_SET_ADD_COUNTERS:
1554 ret = do_add_counters(sock_net(sk), user, len, 1);
1555 break;
1556
1557 default:
1558 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1559 ret = -EINVAL;
1560 }
1561
1562 return ret;
1563}
1564
1565static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1566 compat_uint_t *size,
1567 struct xt_counters *counters,
1568 unsigned int i)
1569{
1570 struct arpt_entry_target *t;
1571 struct compat_arpt_entry __user *ce;
1572 u_int16_t target_offset, next_offset;
1573 compat_uint_t origsize;
1574 int ret;
1575
1576 origsize = *size;
1577 ce = (struct compat_arpt_entry __user *)*dstptr;
1578 if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1579 copy_to_user(&ce->counters, &counters[i],
1580 sizeof(counters[i])) != 0)
1581 return -EFAULT;
1582
1583 *dstptr += sizeof(struct compat_arpt_entry);
1584 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1585
1586 target_offset = e->target_offset - (origsize - *size);
1587
1588 t = arpt_get_target(e);
1589 ret = xt_compat_target_to_user(t, dstptr, size);
1590 if (ret)
1591 return ret;
1592 next_offset = e->next_offset - (origsize - *size);
1593 if (put_user(target_offset, &ce->target_offset) != 0 ||
1594 put_user(next_offset, &ce->next_offset) != 0)
1595 return -EFAULT;
1596 return 0;
1597}
1598
1599static int compat_copy_entries_to_user(unsigned int total_size,
1600 struct xt_table *table,
1601 void __user *userptr)
1602{
1603 struct xt_counters *counters;
1604 const struct xt_table_info *private = table->private;
1605 void __user *pos;
1606 unsigned int size;
1607 int ret = 0;
1608 void *loc_cpu_entry;
1609 unsigned int i = 0;
1610 struct arpt_entry *iter;
1611
1612 counters = alloc_counters(table);
1613 if (IS_ERR(counters))
1614 return PTR_ERR(counters);
1615
1616
1617 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1618 pos = userptr;
1619 size = total_size;
1620 xt_entry_foreach(iter, loc_cpu_entry, total_size) {
1621 ret = compat_copy_entry_to_user(iter, &pos,
1622 &size, counters, i++);
1623 if (ret != 0)
1624 break;
1625 }
1626 vfree(counters);
1627 return ret;
1628}
1629
1630struct compat_arpt_get_entries {
1631 char name[ARPT_TABLE_MAXNAMELEN];
1632 compat_uint_t size;
1633 struct compat_arpt_entry entrytable[0];
1634};
1635
1636static int compat_get_entries(struct net *net,
1637 struct compat_arpt_get_entries __user *uptr,
1638 int *len)
1639{
1640 int ret;
1641 struct compat_arpt_get_entries get;
1642 struct xt_table *t;
1643
1644 if (*len < sizeof(get)) {
1645 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1646 return -EINVAL;
1647 }
1648 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1649 return -EFAULT;
1650 if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1651 duprintf("compat_get_entries: %u != %zu\n",
1652 *len, sizeof(get) + get.size);
1653 return -EINVAL;
1654 }
1655
1656 xt_compat_lock(NFPROTO_ARP);
1657 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1658 if (t && !IS_ERR(t)) {
1659 const struct xt_table_info *private = t->private;
1660 struct xt_table_info info;
1661
1662 duprintf("t->private->number = %u\n", private->number);
1663 ret = compat_table_info(private, &info);
1664 if (!ret && get.size == info.size) {
1665 ret = compat_copy_entries_to_user(private->size,
1666 t, uptr->entrytable);
1667 } else if (!ret) {
1668 duprintf("compat_get_entries: I've got %u not %u!\n",
1669 private->size, get.size);
1670 ret = -EAGAIN;
1671 }
1672 xt_compat_flush_offsets(NFPROTO_ARP);
1673 module_put(t->me);
1674 xt_table_unlock(t);
1675 } else
1676 ret = t ? PTR_ERR(t) : -ENOENT;
1677
1678 xt_compat_unlock(NFPROTO_ARP);
1679 return ret;
1680}
1681
1682static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1683
1684static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1685 int *len)
1686{
1687 int ret;
1688
1689 if (!capable(CAP_NET_ADMIN))
1690 return -EPERM;
1691
1692 switch (cmd) {
1693 case ARPT_SO_GET_INFO:
1694 ret = get_info(sock_net(sk), user, len, 1);
1695 break;
1696 case ARPT_SO_GET_ENTRIES:
1697 ret = compat_get_entries(sock_net(sk), user, len);
1698 break;
1699 default:
1700 ret = do_arpt_get_ctl(sk, cmd, user, len);
1701 }
1702 return ret;
1703}
1704#endif
1705
1706static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1707{
1708 int ret;
1709
1710 if (!capable(CAP_NET_ADMIN))
1711 return -EPERM;
1712
1713 switch (cmd) {
1714 case ARPT_SO_SET_REPLACE:
1715 ret = do_replace(sock_net(sk), user, len);
1716 break;
1717
1718 case ARPT_SO_SET_ADD_COUNTERS:
1719 ret = do_add_counters(sock_net(sk), user, len, 0);
1720 break;
1721
1722 default:
1723 duprintf("do_arpt_set_ctl: unknown request %i\n", cmd);
1724 ret = -EINVAL;
1725 }
1726
1727 return ret;
1728}
1729
1730static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1731{
1732 int ret;
1733
1734 if (!capable(CAP_NET_ADMIN))
1735 return -EPERM;
1736
1737 switch (cmd) {
1738 case ARPT_SO_GET_INFO:
1739 ret = get_info(sock_net(sk), user, len, 0);
1740 break;
1741
1742 case ARPT_SO_GET_ENTRIES:
1743 ret = get_entries(sock_net(sk), user, len);
1744 break;
1745
1746 case ARPT_SO_GET_REVISION_TARGET: {
1747 struct xt_get_revision rev;
1748
1749 if (*len != sizeof(rev)) {
1750 ret = -EINVAL;
1751 break;
1752 }
1753 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1754 ret = -EFAULT;
1755 break;
1756 }
1757
1758 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1759 rev.revision, 1, &ret),
1760 "arpt_%s", rev.name);
1761 break;
1762 }
1763
1764 default:
1765 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1766 ret = -EINVAL;
1767 }
1768
1769 return ret;
1770}
1771
1772struct xt_table *arpt_register_table(struct net *net,
1773 const struct xt_table *table,
1774 const struct arpt_replace *repl)
1775{
1776 int ret;
1777 struct xt_table_info *newinfo;
1778 struct xt_table_info bootstrap = {0};
1779 void *loc_cpu_entry;
1780 struct xt_table *new_table;
1781
1782 newinfo = xt_alloc_table_info(repl->size);
1783 if (!newinfo) {
1784 ret = -ENOMEM;
1785 goto out;
1786 }
1787
1788
1789 loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1790 memcpy(loc_cpu_entry, repl->entries, repl->size);
1791
1792 ret = translate_table(newinfo, loc_cpu_entry, repl);
1793 duprintf("arpt_register_table: translate table gives %d\n", ret);
1794 if (ret != 0)
1795 goto out_free;
1796
1797 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1798 if (IS_ERR(new_table)) {
1799 ret = PTR_ERR(new_table);
1800 goto out_free;
1801 }
1802 return new_table;
1803
1804out_free:
1805 xt_free_table_info(newinfo);
1806out:
1807 return ERR_PTR(ret);
1808}
1809
1810void arpt_unregister_table(struct xt_table *table)
1811{
1812 struct xt_table_info *private;
1813 void *loc_cpu_entry;
1814 struct module *table_owner = table->me;
1815 struct arpt_entry *iter;
1816
1817 private = xt_unregister_table(table);
1818
1819
1820 loc_cpu_entry = private->entries[raw_smp_processor_id()];
1821 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1822 cleanup_entry(iter);
1823 if (private->number > private->initial_entries)
1824 module_put(table_owner);
1825 xt_free_table_info(private);
1826}
1827
1828
1829static struct xt_target arpt_builtin_tg[] __read_mostly = {
1830 {
1831 .name = ARPT_STANDARD_TARGET,
1832 .targetsize = sizeof(int),
1833 .family = NFPROTO_ARP,
1834#ifdef CONFIG_COMPAT
1835 .compatsize = sizeof(compat_int_t),
1836 .compat_from_user = compat_standard_from_user,
1837 .compat_to_user = compat_standard_to_user,
1838#endif
1839 },
1840 {
1841 .name = ARPT_ERROR_TARGET,
1842 .target = arpt_error,
1843 .targetsize = ARPT_FUNCTION_MAXNAMELEN,
1844 .family = NFPROTO_ARP,
1845 },
1846};
1847
1848static struct nf_sockopt_ops arpt_sockopts = {
1849 .pf = PF_INET,
1850 .set_optmin = ARPT_BASE_CTL,
1851 .set_optmax = ARPT_SO_SET_MAX+1,
1852 .set = do_arpt_set_ctl,
1853#ifdef CONFIG_COMPAT
1854 .compat_set = compat_do_arpt_set_ctl,
1855#endif
1856 .get_optmin = ARPT_BASE_CTL,
1857 .get_optmax = ARPT_SO_GET_MAX+1,
1858 .get = do_arpt_get_ctl,
1859#ifdef CONFIG_COMPAT
1860 .compat_get = compat_do_arpt_get_ctl,
1861#endif
1862 .owner = THIS_MODULE,
1863};
1864
1865static int __net_init arp_tables_net_init(struct net *net)
1866{
1867 return xt_proto_init(net, NFPROTO_ARP);
1868}
1869
1870static void __net_exit arp_tables_net_exit(struct net *net)
1871{
1872 xt_proto_fini(net, NFPROTO_ARP);
1873}
1874
1875static struct pernet_operations arp_tables_net_ops = {
1876 .init = arp_tables_net_init,
1877 .exit = arp_tables_net_exit,
1878};
1879
1880static int __init arp_tables_init(void)
1881{
1882 int ret;
1883
1884 ret = register_pernet_subsys(&arp_tables_net_ops);
1885 if (ret < 0)
1886 goto err1;
1887
1888
1889 ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1890 if (ret < 0)
1891 goto err2;
1892
1893
1894 ret = nf_register_sockopt(&arpt_sockopts);
1895 if (ret < 0)
1896 goto err4;
1897
1898 printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1899 return 0;
1900
1901err4:
1902 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1903err2:
1904 unregister_pernet_subsys(&arp_tables_net_ops);
1905err1:
1906 return ret;
1907}
1908
1909static void __exit arp_tables_fini(void)
1910{
1911 nf_unregister_sockopt(&arpt_sockopts);
1912 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1913 unregister_pernet_subsys(&arp_tables_net_ops);
1914}
1915
1916EXPORT_SYMBOL(arpt_register_table);
1917EXPORT_SYMBOL(arpt_unregister_table);
1918EXPORT_SYMBOL(arpt_do_table);
1919
1920module_init(arp_tables_init);
1921module_exit(arp_tables_fini);
1922