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