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