356ae7da4f1634e42e2c0b57c08d9d5770bb36d1
[GitHub/moto-9609/android_kernel_motorola_exynos9610.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 <linux/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 if (unlikely(stackidx >= private->stacksize)) {
261 verdict = NF_DROP;
262 break;
263 }
264 jumpstack[stackidx++] = e;
265 }
266
267 e = get_entry(table_base, v);
268 continue;
269 }
270
271 acpar.target = t->u.kernel.target;
272 acpar.targinfo = t->data;
273 verdict = t->u.kernel.target->target(skb, &acpar);
274
275 if (verdict == XT_CONTINUE) {
276 /* Target might have changed stuff. */
277 arp = arp_hdr(skb);
278 e = arpt_next_entry(e);
279 } else {
280 /* Verdict */
281 break;
282 }
283 } while (!acpar.hotdrop);
284 xt_write_recseq_end(addend);
285 local_bh_enable();
286
287 if (acpar.hotdrop)
288 return NF_DROP;
289 else
290 return verdict;
291 }
292
293 /* All zeroes == unconditional rule. */
294 static inline bool unconditional(const struct arpt_entry *e)
295 {
296 static const struct arpt_arp uncond;
297
298 return e->target_offset == sizeof(struct arpt_entry) &&
299 memcmp(&e->arp, &uncond, sizeof(uncond)) == 0;
300 }
301
302 /* Figures out from what hook each rule can be called: returns 0 if
303 * there are loops. Puts hook bitmask in comefrom.
304 */
305 static int mark_source_chains(const struct xt_table_info *newinfo,
306 unsigned int valid_hooks, void *entry0,
307 unsigned int *offsets)
308 {
309 unsigned int hook;
310
311 /* No recursion; use packet counter to save back ptrs (reset
312 * to 0 as we leave), and comefrom to save source hook bitmask.
313 */
314 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
315 unsigned int pos = newinfo->hook_entry[hook];
316 struct arpt_entry *e = entry0 + pos;
317
318 if (!(valid_hooks & (1 << hook)))
319 continue;
320
321 /* Set initial back pointer. */
322 e->counters.pcnt = pos;
323
324 for (;;) {
325 const struct xt_standard_target *t
326 = (void *)arpt_get_target_c(e);
327 int visited = e->comefrom & (1 << hook);
328
329 if (e->comefrom & (1 << NF_ARP_NUMHOOKS))
330 return 0;
331
332 e->comefrom
333 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
334
335 /* Unconditional return/END. */
336 if ((unconditional(e) &&
337 (strcmp(t->target.u.user.name,
338 XT_STANDARD_TARGET) == 0) &&
339 t->verdict < 0) || visited) {
340 unsigned int oldpos, size;
341
342 if ((strcmp(t->target.u.user.name,
343 XT_STANDARD_TARGET) == 0) &&
344 t->verdict < -NF_MAX_VERDICT - 1)
345 return 0;
346
347 /* Return: backtrack through the last
348 * big jump.
349 */
350 do {
351 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
352 oldpos = pos;
353 pos = e->counters.pcnt;
354 e->counters.pcnt = 0;
355
356 /* We're at the start. */
357 if (pos == oldpos)
358 goto next;
359
360 e = entry0 + pos;
361 } while (oldpos == pos + e->next_offset);
362
363 /* Move along one */
364 size = e->next_offset;
365 e = entry0 + pos + size;
366 if (pos + size >= newinfo->size)
367 return 0;
368 e->counters.pcnt = pos;
369 pos += size;
370 } else {
371 int newpos = t->verdict;
372
373 if (strcmp(t->target.u.user.name,
374 XT_STANDARD_TARGET) == 0 &&
375 newpos >= 0) {
376 /* This a jump; chase it. */
377 if (!xt_find_jump_offset(offsets, newpos,
378 newinfo->number))
379 return 0;
380 e = 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 = entry0 + newpos;
388 e->counters.pcnt = pos;
389 pos = newpos;
390 }
391 }
392 next: ;
393 }
394 return 1;
395 }
396
397 static inline int check_target(struct arpt_entry *e, const char *name)
398 {
399 struct xt_entry_target *t = arpt_get_target(e);
400 struct xt_tgchk_param par = {
401 .table = name,
402 .entryinfo = e,
403 .target = t->u.kernel.target,
404 .targinfo = t->data,
405 .hook_mask = e->comefrom,
406 .family = NFPROTO_ARP,
407 };
408
409 return xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
410 }
411
412 static inline int
413 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
414 struct xt_percpu_counter_alloc_state *alloc_state)
415 {
416 struct xt_entry_target *t;
417 struct xt_target *target;
418 int ret;
419
420 if (!xt_percpu_counter_alloc(alloc_state, &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 xt_percpu_counter_alloc_state alloc_state = { 0 };
529 struct arpt_entry *iter;
530 unsigned int *offsets;
531 unsigned int i;
532 int ret = 0;
533
534 newinfo->size = repl->size;
535 newinfo->number = repl->num_entries;
536
537 /* Init all hooks to impossible value. */
538 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
539 newinfo->hook_entry[i] = 0xFFFFFFFF;
540 newinfo->underflow[i] = 0xFFFFFFFF;
541 }
542
543 offsets = xt_alloc_entry_offsets(newinfo->number);
544 if (!offsets)
545 return -ENOMEM;
546 i = 0;
547
548 /* Walk through entries, checking offsets. */
549 xt_entry_foreach(iter, entry0, newinfo->size) {
550 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
551 entry0 + repl->size,
552 repl->hook_entry,
553 repl->underflow,
554 repl->valid_hooks);
555 if (ret != 0)
556 goto out_free;
557 if (i < repl->num_entries)
558 offsets[i] = (void *)iter - entry0;
559 ++i;
560 if (strcmp(arpt_get_target(iter)->u.user.name,
561 XT_ERROR_TARGET) == 0)
562 ++newinfo->stacksize;
563 }
564
565 ret = -EINVAL;
566 if (i != repl->num_entries)
567 goto out_free;
568
569 /* Check hooks all assigned */
570 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
571 /* Only hooks which are valid */
572 if (!(repl->valid_hooks & (1 << i)))
573 continue;
574 if (newinfo->hook_entry[i] == 0xFFFFFFFF)
575 goto out_free;
576 if (newinfo->underflow[i] == 0xFFFFFFFF)
577 goto out_free;
578 }
579
580 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
581 ret = -ELOOP;
582 goto out_free;
583 }
584 kvfree(offsets);
585
586 /* Finally, each sanity check must pass */
587 i = 0;
588 xt_entry_foreach(iter, entry0, newinfo->size) {
589 ret = find_check_entry(iter, repl->name, repl->size,
590 &alloc_state);
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 cond_resched();
637 }
638 }
639 }
640
641 static struct xt_counters *alloc_counters(const struct xt_table *table)
642 {
643 unsigned int countersize;
644 struct xt_counters *counters;
645 const struct xt_table_info *private = table->private;
646
647 /* We need atomic snapshot of counters: rest doesn't change
648 * (other than comefrom, which userspace doesn't care
649 * about).
650 */
651 countersize = sizeof(struct xt_counters) * private->number;
652 counters = vzalloc(countersize);
653
654 if (counters == NULL)
655 return ERR_PTR(-ENOMEM);
656
657 get_counters(private, counters);
658
659 return counters;
660 }
661
662 static int copy_entries_to_user(unsigned int total_size,
663 const struct xt_table *table,
664 void __user *userptr)
665 {
666 unsigned int off, num;
667 const struct arpt_entry *e;
668 struct xt_counters *counters;
669 struct xt_table_info *private = table->private;
670 int ret = 0;
671 void *loc_cpu_entry;
672
673 counters = alloc_counters(table);
674 if (IS_ERR(counters))
675 return PTR_ERR(counters);
676
677 loc_cpu_entry = private->entries;
678
679 /* FIXME: use iterator macros --RR */
680 /* ... then go back and fix counters and names */
681 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
682 const struct xt_entry_target *t;
683
684 e = loc_cpu_entry + off;
685 if (copy_to_user(userptr + off, e, sizeof(*e))) {
686 ret = -EFAULT;
687 goto free_counters;
688 }
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 (xt_target_to_user(t, userptr + off + e->target_offset)) {
699 ret = -EFAULT;
700 goto free_counters;
701 }
702 }
703
704 free_counters:
705 vfree(counters);
706 return ret;
707 }
708
709 #ifdef CONFIG_COMPAT
710 static void compat_standard_from_user(void *dst, const void *src)
711 {
712 int v = *(compat_int_t *)src;
713
714 if (v > 0)
715 v += xt_compat_calc_jump(NFPROTO_ARP, v);
716 memcpy(dst, &v, sizeof(v));
717 }
718
719 static int compat_standard_to_user(void __user *dst, const void *src)
720 {
721 compat_int_t cv = *(int *)src;
722
723 if (cv > 0)
724 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
725 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
726 }
727
728 static int compat_calc_entry(const struct arpt_entry *e,
729 const struct xt_table_info *info,
730 const void *base, struct xt_table_info *newinfo)
731 {
732 const struct xt_entry_target *t;
733 unsigned int entry_offset;
734 int off, i, ret;
735
736 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
737 entry_offset = (void *)e - base;
738
739 t = arpt_get_target_c(e);
740 off += xt_compat_target_offset(t->u.kernel.target);
741 newinfo->size -= off;
742 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
743 if (ret)
744 return ret;
745
746 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
747 if (info->hook_entry[i] &&
748 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
749 newinfo->hook_entry[i] -= off;
750 if (info->underflow[i] &&
751 (e < (struct arpt_entry *)(base + info->underflow[i])))
752 newinfo->underflow[i] -= off;
753 }
754 return 0;
755 }
756
757 static int compat_table_info(const struct xt_table_info *info,
758 struct xt_table_info *newinfo)
759 {
760 struct arpt_entry *iter;
761 const void *loc_cpu_entry;
762 int ret;
763
764 if (!newinfo || !info)
765 return -EINVAL;
766
767 /* we dont care about newinfo->entries */
768 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
769 newinfo->initial_entries = 0;
770 loc_cpu_entry = info->entries;
771 ret = xt_compat_init_offsets(NFPROTO_ARP, info->number);
772 if (ret)
773 return ret;
774 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
775 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
776 if (ret != 0)
777 return ret;
778 }
779 return 0;
780 }
781 #endif
782
783 static int get_info(struct net *net, void __user *user,
784 const int *len, int compat)
785 {
786 char name[XT_TABLE_MAXNAMELEN];
787 struct xt_table *t;
788 int ret;
789
790 if (*len != sizeof(struct arpt_getinfo))
791 return -EINVAL;
792
793 if (copy_from_user(name, user, sizeof(name)) != 0)
794 return -EFAULT;
795
796 name[XT_TABLE_MAXNAMELEN-1] = '\0';
797 #ifdef CONFIG_COMPAT
798 if (compat)
799 xt_compat_lock(NFPROTO_ARP);
800 #endif
801 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
802 "arptable_%s", name);
803 if (t) {
804 struct arpt_getinfo info;
805 const struct xt_table_info *private = t->private;
806 #ifdef CONFIG_COMPAT
807 struct xt_table_info tmp;
808
809 if (compat) {
810 ret = compat_table_info(private, &tmp);
811 xt_compat_flush_offsets(NFPROTO_ARP);
812 private = &tmp;
813 }
814 #endif
815 memset(&info, 0, sizeof(info));
816 info.valid_hooks = t->valid_hooks;
817 memcpy(info.hook_entry, private->hook_entry,
818 sizeof(info.hook_entry));
819 memcpy(info.underflow, private->underflow,
820 sizeof(info.underflow));
821 info.num_entries = private->number;
822 info.size = private->size;
823 strcpy(info.name, name);
824
825 if (copy_to_user(user, &info, *len) != 0)
826 ret = -EFAULT;
827 else
828 ret = 0;
829 xt_table_unlock(t);
830 module_put(t->me);
831 } else
832 ret = -ENOENT;
833 #ifdef CONFIG_COMPAT
834 if (compat)
835 xt_compat_unlock(NFPROTO_ARP);
836 #endif
837 return ret;
838 }
839
840 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
841 const int *len)
842 {
843 int ret;
844 struct arpt_get_entries get;
845 struct xt_table *t;
846
847 if (*len < sizeof(get))
848 return -EINVAL;
849 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
850 return -EFAULT;
851 if (*len != sizeof(struct arpt_get_entries) + get.size)
852 return -EINVAL;
853
854 get.name[sizeof(get.name) - 1] = '\0';
855
856 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
857 if (t) {
858 const struct xt_table_info *private = t->private;
859
860 if (get.size == private->size)
861 ret = copy_entries_to_user(private->size,
862 t, uptr->entrytable);
863 else
864 ret = -EAGAIN;
865
866 module_put(t->me);
867 xt_table_unlock(t);
868 } else
869 ret = -ENOENT;
870
871 return ret;
872 }
873
874 static int __do_replace(struct net *net, const char *name,
875 unsigned int valid_hooks,
876 struct xt_table_info *newinfo,
877 unsigned int num_counters,
878 void __user *counters_ptr)
879 {
880 int ret;
881 struct xt_table *t;
882 struct xt_table_info *oldinfo;
883 struct xt_counters *counters;
884 void *loc_cpu_old_entry;
885 struct arpt_entry *iter;
886
887 ret = 0;
888 counters = xt_counters_alloc(num_counters);
889 if (!counters) {
890 ret = -ENOMEM;
891 goto out;
892 }
893
894 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
895 "arptable_%s", name);
896 if (!t) {
897 ret = -ENOENT;
898 goto free_newinfo_counters_untrans;
899 }
900
901 /* You lied! */
902 if (valid_hooks != t->valid_hooks) {
903 ret = -EINVAL;
904 goto put_module;
905 }
906
907 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
908 if (!oldinfo)
909 goto put_module;
910
911 /* Update module usage count based on number of rules */
912 if ((oldinfo->number > oldinfo->initial_entries) ||
913 (newinfo->number <= oldinfo->initial_entries))
914 module_put(t->me);
915 if ((oldinfo->number > oldinfo->initial_entries) &&
916 (newinfo->number <= oldinfo->initial_entries))
917 module_put(t->me);
918
919 /* Get the old counters, and synchronize with replace */
920 get_counters(oldinfo, counters);
921
922 /* Decrease module usage counts and free resource */
923 loc_cpu_old_entry = oldinfo->entries;
924 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
925 cleanup_entry(iter);
926
927 xt_free_table_info(oldinfo);
928 if (copy_to_user(counters_ptr, counters,
929 sizeof(struct xt_counters) * num_counters) != 0) {
930 /* Silent error, can't fail, new table is already in place */
931 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
932 }
933 vfree(counters);
934 xt_table_unlock(t);
935 return ret;
936
937 put_module:
938 module_put(t->me);
939 xt_table_unlock(t);
940 free_newinfo_counters_untrans:
941 vfree(counters);
942 out:
943 return ret;
944 }
945
946 static int do_replace(struct net *net, const void __user *user,
947 unsigned int len)
948 {
949 int ret;
950 struct arpt_replace tmp;
951 struct xt_table_info *newinfo;
952 void *loc_cpu_entry;
953 struct arpt_entry *iter;
954
955 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
956 return -EFAULT;
957
958 /* overflow check */
959 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
960 return -ENOMEM;
961 if (tmp.num_counters == 0)
962 return -EINVAL;
963
964 tmp.name[sizeof(tmp.name)-1] = 0;
965
966 newinfo = xt_alloc_table_info(tmp.size);
967 if (!newinfo)
968 return -ENOMEM;
969
970 loc_cpu_entry = newinfo->entries;
971 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
972 tmp.size) != 0) {
973 ret = -EFAULT;
974 goto free_newinfo;
975 }
976
977 ret = translate_table(newinfo, loc_cpu_entry, &tmp);
978 if (ret != 0)
979 goto free_newinfo;
980
981 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
982 tmp.num_counters, tmp.counters);
983 if (ret)
984 goto free_newinfo_untrans;
985 return 0;
986
987 free_newinfo_untrans:
988 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
989 cleanup_entry(iter);
990 free_newinfo:
991 xt_free_table_info(newinfo);
992 return ret;
993 }
994
995 static int do_add_counters(struct net *net, const void __user *user,
996 unsigned int len, int compat)
997 {
998 unsigned int i;
999 struct xt_counters_info tmp;
1000 struct xt_counters *paddc;
1001 struct xt_table *t;
1002 const struct xt_table_info *private;
1003 int ret = 0;
1004 struct arpt_entry *iter;
1005 unsigned int addend;
1006
1007 paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1008 if (IS_ERR(paddc))
1009 return PTR_ERR(paddc);
1010
1011 t = xt_find_table_lock(net, NFPROTO_ARP, tmp.name);
1012 if (!t) {
1013 ret = -ENOENT;
1014 goto free;
1015 }
1016
1017 local_bh_disable();
1018 private = t->private;
1019 if (private->number != tmp.num_counters) {
1020 ret = -EINVAL;
1021 goto unlock_up_free;
1022 }
1023
1024 i = 0;
1025
1026 addend = xt_write_recseq_begin();
1027 xt_entry_foreach(iter, private->entries, private->size) {
1028 struct xt_counters *tmp;
1029
1030 tmp = xt_get_this_cpu_counter(&iter->counters);
1031 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1032 ++i;
1033 }
1034 xt_write_recseq_end(addend);
1035 unlock_up_free:
1036 local_bh_enable();
1037 xt_table_unlock(t);
1038 module_put(t->me);
1039 free:
1040 vfree(paddc);
1041
1042 return ret;
1043 }
1044
1045 #ifdef CONFIG_COMPAT
1046 struct compat_arpt_replace {
1047 char name[XT_TABLE_MAXNAMELEN];
1048 u32 valid_hooks;
1049 u32 num_entries;
1050 u32 size;
1051 u32 hook_entry[NF_ARP_NUMHOOKS];
1052 u32 underflow[NF_ARP_NUMHOOKS];
1053 u32 num_counters;
1054 compat_uptr_t counters;
1055 struct compat_arpt_entry entries[0];
1056 };
1057
1058 static inline void compat_release_entry(struct compat_arpt_entry *e)
1059 {
1060 struct xt_entry_target *t;
1061
1062 t = compat_arpt_get_target(e);
1063 module_put(t->u.kernel.target->me);
1064 }
1065
1066 static int
1067 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1068 struct xt_table_info *newinfo,
1069 unsigned int *size,
1070 const unsigned char *base,
1071 const unsigned char *limit)
1072 {
1073 struct xt_entry_target *t;
1074 struct xt_target *target;
1075 unsigned int entry_offset;
1076 int ret, off;
1077
1078 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1079 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit ||
1080 (unsigned char *)e + e->next_offset > limit)
1081 return -EINVAL;
1082
1083 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1084 sizeof(struct compat_xt_entry_target))
1085 return -EINVAL;
1086
1087 if (!arp_checkentry(&e->arp))
1088 return -EINVAL;
1089
1090 ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset,
1091 e->next_offset);
1092 if (ret)
1093 return ret;
1094
1095 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1096 entry_offset = (void *)e - (void *)base;
1097
1098 t = compat_arpt_get_target(e);
1099 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1100 t->u.user.revision);
1101 if (IS_ERR(target)) {
1102 ret = PTR_ERR(target);
1103 goto out;
1104 }
1105 t->u.kernel.target = target;
1106
1107 off += xt_compat_target_offset(target);
1108 *size += off;
1109 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1110 if (ret)
1111 goto release_target;
1112
1113 return 0;
1114
1115 release_target:
1116 module_put(t->u.kernel.target->me);
1117 out:
1118 return ret;
1119 }
1120
1121 static void
1122 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1123 unsigned int *size,
1124 struct xt_table_info *newinfo, unsigned char *base)
1125 {
1126 struct xt_entry_target *t;
1127 struct arpt_entry *de;
1128 unsigned int origsize;
1129 int h;
1130
1131 origsize = *size;
1132 de = *dstptr;
1133 memcpy(de, e, sizeof(struct arpt_entry));
1134 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1135
1136 *dstptr += sizeof(struct arpt_entry);
1137 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1138
1139 de->target_offset = e->target_offset - (origsize - *size);
1140 t = compat_arpt_get_target(e);
1141 xt_compat_target_from_user(t, dstptr, size);
1142
1143 de->next_offset = e->next_offset - (origsize - *size);
1144 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1145 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1146 newinfo->hook_entry[h] -= origsize - *size;
1147 if ((unsigned char *)de - base < newinfo->underflow[h])
1148 newinfo->underflow[h] -= origsize - *size;
1149 }
1150 }
1151
1152 static int translate_compat_table(struct xt_table_info **pinfo,
1153 void **pentry0,
1154 const struct compat_arpt_replace *compatr)
1155 {
1156 unsigned int i, j;
1157 struct xt_table_info *newinfo, *info;
1158 void *pos, *entry0, *entry1;
1159 struct compat_arpt_entry *iter0;
1160 struct arpt_replace repl;
1161 unsigned int size;
1162 int ret;
1163
1164 info = *pinfo;
1165 entry0 = *pentry0;
1166 size = compatr->size;
1167 info->number = compatr->num_entries;
1168
1169 j = 0;
1170 xt_compat_lock(NFPROTO_ARP);
1171 ret = xt_compat_init_offsets(NFPROTO_ARP, compatr->num_entries);
1172 if (ret)
1173 goto out_unlock;
1174 /* Walk through entries, checking offsets. */
1175 xt_entry_foreach(iter0, entry0, compatr->size) {
1176 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1177 entry0,
1178 entry0 + compatr->size);
1179 if (ret != 0)
1180 goto out_unlock;
1181 ++j;
1182 }
1183
1184 ret = -EINVAL;
1185 if (j != compatr->num_entries)
1186 goto out_unlock;
1187
1188 ret = -ENOMEM;
1189 newinfo = xt_alloc_table_info(size);
1190 if (!newinfo)
1191 goto out_unlock;
1192
1193 newinfo->number = compatr->num_entries;
1194 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1195 newinfo->hook_entry[i] = compatr->hook_entry[i];
1196 newinfo->underflow[i] = compatr->underflow[i];
1197 }
1198 entry1 = newinfo->entries;
1199 pos = entry1;
1200 size = compatr->size;
1201 xt_entry_foreach(iter0, entry0, compatr->size)
1202 compat_copy_entry_from_user(iter0, &pos, &size,
1203 newinfo, entry1);
1204
1205 /* all module references in entry0 are now gone */
1206
1207 xt_compat_flush_offsets(NFPROTO_ARP);
1208 xt_compat_unlock(NFPROTO_ARP);
1209
1210 memcpy(&repl, compatr, sizeof(*compatr));
1211
1212 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1213 repl.hook_entry[i] = newinfo->hook_entry[i];
1214 repl.underflow[i] = newinfo->underflow[i];
1215 }
1216
1217 repl.num_counters = 0;
1218 repl.counters = NULL;
1219 repl.size = newinfo->size;
1220 ret = translate_table(newinfo, entry1, &repl);
1221 if (ret)
1222 goto free_newinfo;
1223
1224 *pinfo = newinfo;
1225 *pentry0 = entry1;
1226 xt_free_table_info(info);
1227 return 0;
1228
1229 free_newinfo:
1230 xt_free_table_info(newinfo);
1231 return ret;
1232 out_unlock:
1233 xt_compat_flush_offsets(NFPROTO_ARP);
1234 xt_compat_unlock(NFPROTO_ARP);
1235 xt_entry_foreach(iter0, entry0, compatr->size) {
1236 if (j-- == 0)
1237 break;
1238 compat_release_entry(iter0);
1239 }
1240 return ret;
1241 }
1242
1243 static int compat_do_replace(struct net *net, void __user *user,
1244 unsigned int len)
1245 {
1246 int ret;
1247 struct compat_arpt_replace tmp;
1248 struct xt_table_info *newinfo;
1249 void *loc_cpu_entry;
1250 struct arpt_entry *iter;
1251
1252 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1253 return -EFAULT;
1254
1255 /* overflow check */
1256 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1257 return -ENOMEM;
1258 if (tmp.num_counters == 0)
1259 return -EINVAL;
1260
1261 tmp.name[sizeof(tmp.name)-1] = 0;
1262
1263 newinfo = xt_alloc_table_info(tmp.size);
1264 if (!newinfo)
1265 return -ENOMEM;
1266
1267 loc_cpu_entry = newinfo->entries;
1268 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1269 ret = -EFAULT;
1270 goto free_newinfo;
1271 }
1272
1273 ret = translate_compat_table(&newinfo, &loc_cpu_entry, &tmp);
1274 if (ret != 0)
1275 goto free_newinfo;
1276
1277 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1278 tmp.num_counters, compat_ptr(tmp.counters));
1279 if (ret)
1280 goto free_newinfo_untrans;
1281 return 0;
1282
1283 free_newinfo_untrans:
1284 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1285 cleanup_entry(iter);
1286 free_newinfo:
1287 xt_free_table_info(newinfo);
1288 return ret;
1289 }
1290
1291 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1292 unsigned int len)
1293 {
1294 int ret;
1295
1296 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1297 return -EPERM;
1298
1299 switch (cmd) {
1300 case ARPT_SO_SET_REPLACE:
1301 ret = compat_do_replace(sock_net(sk), user, len);
1302 break;
1303
1304 case ARPT_SO_SET_ADD_COUNTERS:
1305 ret = do_add_counters(sock_net(sk), user, len, 1);
1306 break;
1307
1308 default:
1309 ret = -EINVAL;
1310 }
1311
1312 return ret;
1313 }
1314
1315 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1316 compat_uint_t *size,
1317 struct xt_counters *counters,
1318 unsigned int i)
1319 {
1320 struct xt_entry_target *t;
1321 struct compat_arpt_entry __user *ce;
1322 u_int16_t target_offset, next_offset;
1323 compat_uint_t origsize;
1324 int ret;
1325
1326 origsize = *size;
1327 ce = *dstptr;
1328 if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1329 copy_to_user(&ce->counters, &counters[i],
1330 sizeof(counters[i])) != 0)
1331 return -EFAULT;
1332
1333 *dstptr += sizeof(struct compat_arpt_entry);
1334 *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1335
1336 target_offset = e->target_offset - (origsize - *size);
1337
1338 t = arpt_get_target(e);
1339 ret = xt_compat_target_to_user(t, dstptr, size);
1340 if (ret)
1341 return ret;
1342 next_offset = e->next_offset - (origsize - *size);
1343 if (put_user(target_offset, &ce->target_offset) != 0 ||
1344 put_user(next_offset, &ce->next_offset) != 0)
1345 return -EFAULT;
1346 return 0;
1347 }
1348
1349 static int compat_copy_entries_to_user(unsigned int total_size,
1350 struct xt_table *table,
1351 void __user *userptr)
1352 {
1353 struct xt_counters *counters;
1354 const struct xt_table_info *private = table->private;
1355 void __user *pos;
1356 unsigned int size;
1357 int ret = 0;
1358 unsigned int i = 0;
1359 struct arpt_entry *iter;
1360
1361 counters = alloc_counters(table);
1362 if (IS_ERR(counters))
1363 return PTR_ERR(counters);
1364
1365 pos = userptr;
1366 size = total_size;
1367 xt_entry_foreach(iter, private->entries, total_size) {
1368 ret = compat_copy_entry_to_user(iter, &pos,
1369 &size, counters, i++);
1370 if (ret != 0)
1371 break;
1372 }
1373 vfree(counters);
1374 return ret;
1375 }
1376
1377 struct compat_arpt_get_entries {
1378 char name[XT_TABLE_MAXNAMELEN];
1379 compat_uint_t size;
1380 struct compat_arpt_entry entrytable[0];
1381 };
1382
1383 static int compat_get_entries(struct net *net,
1384 struct compat_arpt_get_entries __user *uptr,
1385 int *len)
1386 {
1387 int ret;
1388 struct compat_arpt_get_entries get;
1389 struct xt_table *t;
1390
1391 if (*len < sizeof(get))
1392 return -EINVAL;
1393 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1394 return -EFAULT;
1395 if (*len != sizeof(struct compat_arpt_get_entries) + get.size)
1396 return -EINVAL;
1397
1398 get.name[sizeof(get.name) - 1] = '\0';
1399
1400 xt_compat_lock(NFPROTO_ARP);
1401 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1402 if (t) {
1403 const struct xt_table_info *private = t->private;
1404 struct xt_table_info info;
1405
1406 ret = compat_table_info(private, &info);
1407 if (!ret && get.size == info.size) {
1408 ret = compat_copy_entries_to_user(private->size,
1409 t, uptr->entrytable);
1410 } else if (!ret)
1411 ret = -EAGAIN;
1412
1413 xt_compat_flush_offsets(NFPROTO_ARP);
1414 module_put(t->me);
1415 xt_table_unlock(t);
1416 } else
1417 ret = -ENOENT;
1418
1419 xt_compat_unlock(NFPROTO_ARP);
1420 return ret;
1421 }
1422
1423 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1424
1425 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1426 int *len)
1427 {
1428 int ret;
1429
1430 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1431 return -EPERM;
1432
1433 switch (cmd) {
1434 case ARPT_SO_GET_INFO:
1435 ret = get_info(sock_net(sk), user, len, 1);
1436 break;
1437 case ARPT_SO_GET_ENTRIES:
1438 ret = compat_get_entries(sock_net(sk), user, len);
1439 break;
1440 default:
1441 ret = do_arpt_get_ctl(sk, cmd, user, len);
1442 }
1443 return ret;
1444 }
1445 #endif
1446
1447 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1448 {
1449 int ret;
1450
1451 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1452 return -EPERM;
1453
1454 switch (cmd) {
1455 case ARPT_SO_SET_REPLACE:
1456 ret = do_replace(sock_net(sk), user, len);
1457 break;
1458
1459 case ARPT_SO_SET_ADD_COUNTERS:
1460 ret = do_add_counters(sock_net(sk), user, len, 0);
1461 break;
1462
1463 default:
1464 ret = -EINVAL;
1465 }
1466
1467 return ret;
1468 }
1469
1470 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1471 {
1472 int ret;
1473
1474 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1475 return -EPERM;
1476
1477 switch (cmd) {
1478 case ARPT_SO_GET_INFO:
1479 ret = get_info(sock_net(sk), user, len, 0);
1480 break;
1481
1482 case ARPT_SO_GET_ENTRIES:
1483 ret = get_entries(sock_net(sk), user, len);
1484 break;
1485
1486 case ARPT_SO_GET_REVISION_TARGET: {
1487 struct xt_get_revision rev;
1488
1489 if (*len != sizeof(rev)) {
1490 ret = -EINVAL;
1491 break;
1492 }
1493 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1494 ret = -EFAULT;
1495 break;
1496 }
1497 rev.name[sizeof(rev.name)-1] = 0;
1498
1499 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1500 rev.revision, 1, &ret),
1501 "arpt_%s", rev.name);
1502 break;
1503 }
1504
1505 default:
1506 ret = -EINVAL;
1507 }
1508
1509 return ret;
1510 }
1511
1512 static void __arpt_unregister_table(struct xt_table *table)
1513 {
1514 struct xt_table_info *private;
1515 void *loc_cpu_entry;
1516 struct module *table_owner = table->me;
1517 struct arpt_entry *iter;
1518
1519 private = xt_unregister_table(table);
1520
1521 /* Decrease module usage counts and free resources */
1522 loc_cpu_entry = private->entries;
1523 xt_entry_foreach(iter, loc_cpu_entry, private->size)
1524 cleanup_entry(iter);
1525 if (private->number > private->initial_entries)
1526 module_put(table_owner);
1527 xt_free_table_info(private);
1528 }
1529
1530 int arpt_register_table(struct net *net,
1531 const struct xt_table *table,
1532 const struct arpt_replace *repl,
1533 const struct nf_hook_ops *ops,
1534 struct xt_table **res)
1535 {
1536 int ret;
1537 struct xt_table_info *newinfo;
1538 struct xt_table_info bootstrap = {0};
1539 void *loc_cpu_entry;
1540 struct xt_table *new_table;
1541
1542 newinfo = xt_alloc_table_info(repl->size);
1543 if (!newinfo)
1544 return -ENOMEM;
1545
1546 loc_cpu_entry = newinfo->entries;
1547 memcpy(loc_cpu_entry, repl->entries, repl->size);
1548
1549 ret = translate_table(newinfo, loc_cpu_entry, repl);
1550 if (ret != 0)
1551 goto out_free;
1552
1553 new_table = xt_register_table(net, table, &bootstrap, newinfo);
1554 if (IS_ERR(new_table)) {
1555 ret = PTR_ERR(new_table);
1556 goto out_free;
1557 }
1558
1559 /* set res now, will see skbs right after nf_register_net_hooks */
1560 WRITE_ONCE(*res, new_table);
1561
1562 ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1563 if (ret != 0) {
1564 __arpt_unregister_table(new_table);
1565 *res = NULL;
1566 }
1567
1568 return ret;
1569
1570 out_free:
1571 xt_free_table_info(newinfo);
1572 return ret;
1573 }
1574
1575 void arpt_unregister_table(struct net *net, struct xt_table *table,
1576 const struct nf_hook_ops *ops)
1577 {
1578 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1579 __arpt_unregister_table(table);
1580 }
1581
1582 /* The built-in targets: standard (NULL) and error. */
1583 static struct xt_target arpt_builtin_tg[] __read_mostly = {
1584 {
1585 .name = XT_STANDARD_TARGET,
1586 .targetsize = sizeof(int),
1587 .family = NFPROTO_ARP,
1588 #ifdef CONFIG_COMPAT
1589 .compatsize = sizeof(compat_int_t),
1590 .compat_from_user = compat_standard_from_user,
1591 .compat_to_user = compat_standard_to_user,
1592 #endif
1593 },
1594 {
1595 .name = XT_ERROR_TARGET,
1596 .target = arpt_error,
1597 .targetsize = XT_FUNCTION_MAXNAMELEN,
1598 .family = NFPROTO_ARP,
1599 },
1600 };
1601
1602 static struct nf_sockopt_ops arpt_sockopts = {
1603 .pf = PF_INET,
1604 .set_optmin = ARPT_BASE_CTL,
1605 .set_optmax = ARPT_SO_SET_MAX+1,
1606 .set = do_arpt_set_ctl,
1607 #ifdef CONFIG_COMPAT
1608 .compat_set = compat_do_arpt_set_ctl,
1609 #endif
1610 .get_optmin = ARPT_BASE_CTL,
1611 .get_optmax = ARPT_SO_GET_MAX+1,
1612 .get = do_arpt_get_ctl,
1613 #ifdef CONFIG_COMPAT
1614 .compat_get = compat_do_arpt_get_ctl,
1615 #endif
1616 .owner = THIS_MODULE,
1617 };
1618
1619 static int __net_init arp_tables_net_init(struct net *net)
1620 {
1621 return xt_proto_init(net, NFPROTO_ARP);
1622 }
1623
1624 static void __net_exit arp_tables_net_exit(struct net *net)
1625 {
1626 xt_proto_fini(net, NFPROTO_ARP);
1627 }
1628
1629 static struct pernet_operations arp_tables_net_ops = {
1630 .init = arp_tables_net_init,
1631 .exit = arp_tables_net_exit,
1632 };
1633
1634 static int __init arp_tables_init(void)
1635 {
1636 int ret;
1637
1638 ret = register_pernet_subsys(&arp_tables_net_ops);
1639 if (ret < 0)
1640 goto err1;
1641
1642 /* No one else will be downing sem now, so we won't sleep */
1643 ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1644 if (ret < 0)
1645 goto err2;
1646
1647 /* Register setsockopt */
1648 ret = nf_register_sockopt(&arpt_sockopts);
1649 if (ret < 0)
1650 goto err4;
1651
1652 pr_info("arp_tables: (C) 2002 David S. Miller\n");
1653 return 0;
1654
1655 err4:
1656 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1657 err2:
1658 unregister_pernet_subsys(&arp_tables_net_ops);
1659 err1:
1660 return ret;
1661 }
1662
1663 static void __exit arp_tables_fini(void)
1664 {
1665 nf_unregister_sockopt(&arpt_sockopts);
1666 xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1667 unregister_pernet_subsys(&arp_tables_net_ops);
1668 }
1669
1670 EXPORT_SYMBOL(arpt_register_table);
1671 EXPORT_SYMBOL(arpt_unregister_table);
1672 EXPORT_SYMBOL(arpt_do_table);
1673
1674 module_init(arp_tables_init);
1675 module_exit(arp_tables_fini);