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