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