]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/ipv4/netfilter/arp_tables.c
UBUNTU: SAUCE: [nf,v2] netfilter: x_tables: don't rely on well-behaving userspace
[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 <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 static bool next_offset_ok(const struct xt_table_info *t, unsigned int newpos)
299 {
300 if (newpos > t->size - sizeof(struct arpt_entry))
301 return false;
302
303 if (newpos % __alignof__(struct arpt_entry) != 0)
304 return false;
305
306 return true;
307 }
308
309 /* Figures out from what hook each rule can be called: returns 0 if
310 * there are loops. Puts hook bitmask in comefrom.
311 */
312 static int mark_source_chains(const struct xt_table_info *newinfo,
313 unsigned int valid_hooks, void *entry0,
314 unsigned int *offsets)
315 {
316 unsigned int hook;
317
318 /* No recursion; use packet counter to save back ptrs (reset
319 * to 0 as we leave), and comefrom to save source hook bitmask.
320 */
321 for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
322 unsigned int pos = newinfo->hook_entry[hook];
323 struct arpt_entry *e = entry0 + pos;
324
325 if (!(valid_hooks & (1 << hook)))
326 continue;
327
328 /* Set initial back pointer. */
329 e->counters.pcnt = pos;
330
331 for (;;) {
332 const struct xt_standard_target *t
333 = (void *)arpt_get_target_c(e);
334 int visited = e->comefrom & (1 << hook);
335
336 if (e->comefrom & (1 << NF_ARP_NUMHOOKS))
337 return 0;
338
339 e->comefrom
340 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
341
342 /* Unconditional return/END. */
343 if ((unconditional(e) &&
344 (strcmp(t->target.u.user.name,
345 XT_STANDARD_TARGET) == 0) &&
346 t->verdict < 0) || visited) {
347 unsigned int oldpos, size;
348
349 if ((strcmp(t->target.u.user.name,
350 XT_STANDARD_TARGET) == 0) &&
351 t->verdict < -NF_MAX_VERDICT - 1)
352 return 0;
353
354 /* Return: backtrack through the last
355 * big jump.
356 */
357 do {
358 e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
359 oldpos = pos;
360 pos = e->counters.pcnt;
361 e->counters.pcnt = 0;
362
363 /* We're at the start. */
364 if (pos == oldpos)
365 goto next;
366
367 e = entry0 + pos;
368 } while (oldpos == pos + e->next_offset);
369
370 /* Move along one */
371 size = e->next_offset;
372 if (!next_offset_ok(newinfo, pos + size))
373 return 0;
374 e = entry0 + pos + size;
375 if (pos + size >= newinfo->size)
376 return 0;
377 e->counters.pcnt = pos;
378 pos += size;
379 } else {
380 int newpos = t->verdict;
381
382 if (strcmp(t->target.u.user.name,
383 XT_STANDARD_TARGET) == 0 &&
384 newpos >= 0) {
385 /* This a jump; chase it. */
386 if (!xt_find_jump_offset(offsets, newpos,
387 newinfo->number))
388 return 0;
389 e = entry0 + newpos;
390 } else {
391 /* ... this is a fallthru */
392 newpos = pos + e->next_offset;
393 if (newpos >= newinfo->size)
394 return 0;
395 }
396
397 if (!next_offset_ok(newinfo, newpos))
398 return 0;
399
400 e = entry0 + newpos;
401 e->counters.pcnt = pos;
402 pos = newpos;
403 }
404 }
405 next: ;
406 }
407 return 1;
408 }
409
410 static inline int check_target(struct arpt_entry *e, const char *name)
411 {
412 struct xt_entry_target *t = arpt_get_target(e);
413 struct xt_tgchk_param par = {
414 .table = name,
415 .entryinfo = e,
416 .target = t->u.kernel.target,
417 .targinfo = t->data,
418 .hook_mask = e->comefrom,
419 .family = NFPROTO_ARP,
420 };
421
422 return xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
423 }
424
425 static inline int
426 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
427 struct xt_percpu_counter_alloc_state *alloc_state)
428 {
429 struct xt_entry_target *t;
430 struct xt_target *target;
431 int ret;
432
433 if (!xt_percpu_counter_alloc(alloc_state, &e->counters))
434 return -ENOMEM;
435
436 t = arpt_get_target(e);
437 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
438 t->u.user.revision);
439 if (IS_ERR(target)) {
440 ret = PTR_ERR(target);
441 goto out;
442 }
443 t->u.kernel.target = target;
444
445 ret = check_target(e, name);
446 if (ret)
447 goto err;
448 return 0;
449 err:
450 module_put(t->u.kernel.target->me);
451 out:
452 xt_percpu_counter_free(&e->counters);
453
454 return ret;
455 }
456
457 static bool check_underflow(const struct arpt_entry *e)
458 {
459 const struct xt_entry_target *t;
460 unsigned int verdict;
461
462 if (!unconditional(e))
463 return false;
464 t = arpt_get_target_c(e);
465 if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
466 return false;
467 verdict = ((struct xt_standard_target *)t)->verdict;
468 verdict = -verdict - 1;
469 return verdict == NF_DROP || verdict == NF_ACCEPT;
470 }
471
472 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
473 struct xt_table_info *newinfo,
474 const unsigned char *base,
475 const unsigned char *limit,
476 const unsigned int *hook_entries,
477 const unsigned int *underflows,
478 unsigned int valid_hooks)
479 {
480 unsigned int h;
481 int err;
482
483 if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
484 (unsigned char *)e + sizeof(struct arpt_entry) >= limit ||
485 (unsigned char *)e + e->next_offset > limit)
486 return -EINVAL;
487
488 if (e->next_offset
489 < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target))
490 return -EINVAL;
491
492 if (!arp_checkentry(&e->arp))
493 return -EINVAL;
494
495 err = xt_check_entry_offsets(e, e->elems, e->target_offset,
496 e->next_offset);
497 if (err)
498 return err;
499
500 /* Check hooks & underflows */
501 for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
502 if (!(valid_hooks & (1 << h)))
503 continue;
504 if ((unsigned char *)e - base == hook_entries[h])
505 newinfo->hook_entry[h] = hook_entries[h];
506 if ((unsigned char *)e - base == underflows[h]) {
507 if (!check_underflow(e))
508 return -EINVAL;
509
510 newinfo->underflow[h] = underflows[h];
511 }
512 }
513
514 /* Clear counters and comefrom */
515 e->counters = ((struct xt_counters) { 0, 0 });
516 e->comefrom = 0;
517 return 0;
518 }
519
520 static inline void cleanup_entry(struct arpt_entry *e)
521 {
522 struct xt_tgdtor_param par;
523 struct xt_entry_target *t;
524
525 t = arpt_get_target(e);
526 par.target = t->u.kernel.target;
527 par.targinfo = t->data;
528 par.family = NFPROTO_ARP;
529 if (par.target->destroy != NULL)
530 par.target->destroy(&par);
531 module_put(par.target->me);
532 xt_percpu_counter_free(&e->counters);
533 }
534
535 /* Checks and translates the user-supplied table segment (held in
536 * newinfo).
537 */
538 static int translate_table(struct xt_table_info *newinfo, void *entry0,
539 const struct arpt_replace *repl)
540 {
541 struct xt_percpu_counter_alloc_state alloc_state = { 0 };
542 struct arpt_entry *iter;
543 unsigned int *offsets;
544 unsigned int i;
545 int ret = 0;
546
547 newinfo->size = repl->size;
548 newinfo->number = repl->num_entries;
549
550 /* Init all hooks to impossible value. */
551 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
552 newinfo->hook_entry[i] = 0xFFFFFFFF;
553 newinfo->underflow[i] = 0xFFFFFFFF;
554 }
555
556 offsets = xt_alloc_entry_offsets(newinfo->number);
557 if (!offsets)
558 return -ENOMEM;
559 i = 0;
560
561 /* Walk through entries, checking offsets. */
562 xt_entry_foreach(iter, entry0, newinfo->size) {
563 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
564 entry0 + repl->size,
565 repl->hook_entry,
566 repl->underflow,
567 repl->valid_hooks);
568 if (ret != 0)
569 goto out_free;
570 if (i < repl->num_entries)
571 offsets[i] = (void *)iter - entry0;
572 ++i;
573 if (strcmp(arpt_get_target(iter)->u.user.name,
574 XT_ERROR_TARGET) == 0)
575 ++newinfo->stacksize;
576 }
577
578 ret = -EINVAL;
579 if (i != repl->num_entries)
580 goto out_free;
581
582 /* Check hooks all assigned */
583 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
584 /* Only hooks which are valid */
585 if (!(repl->valid_hooks & (1 << i)))
586 continue;
587 if (newinfo->hook_entry[i] == 0xFFFFFFFF)
588 goto out_free;
589 if (newinfo->underflow[i] == 0xFFFFFFFF)
590 goto out_free;
591 }
592
593 if (!mark_source_chains(newinfo, repl->valid_hooks, entry0, offsets)) {
594 ret = -ELOOP;
595 goto out_free;
596 }
597 kvfree(offsets);
598
599 /* Finally, each sanity check must pass */
600 i = 0;
601 xt_entry_foreach(iter, entry0, newinfo->size) {
602 ret = find_check_entry(iter, repl->name, repl->size,
603 &alloc_state);
604 if (ret != 0)
605 break;
606 ++i;
607 }
608
609 if (ret != 0) {
610 xt_entry_foreach(iter, entry0, newinfo->size) {
611 if (i-- == 0)
612 break;
613 cleanup_entry(iter);
614 }
615 return ret;
616 }
617
618 return ret;
619 out_free:
620 kvfree(offsets);
621 return ret;
622 }
623
624 static void get_counters(const struct xt_table_info *t,
625 struct xt_counters counters[])
626 {
627 struct arpt_entry *iter;
628 unsigned int cpu;
629 unsigned int i;
630
631 for_each_possible_cpu(cpu) {
632 seqcount_t *s = &per_cpu(xt_recseq, cpu);
633
634 i = 0;
635 xt_entry_foreach(iter, t->entries, t->size) {
636 struct xt_counters *tmp;
637 u64 bcnt, pcnt;
638 unsigned int start;
639
640 tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
641 do {
642 start = read_seqcount_begin(s);
643 bcnt = tmp->bcnt;
644 pcnt = tmp->pcnt;
645 } while (read_seqcount_retry(s, start));
646
647 ADD_COUNTER(counters[i], bcnt, pcnt);
648 ++i;
649 }
650 }
651 }
652
653 static struct xt_counters *alloc_counters(const struct xt_table *table)
654 {
655 unsigned int countersize;
656 struct xt_counters *counters;
657 const struct xt_table_info *private = table->private;
658
659 /* We need atomic snapshot of counters: rest doesn't change
660 * (other than comefrom, which userspace doesn't care
661 * about).
662 */
663 countersize = sizeof(struct xt_counters) * private->number;
664 counters = vzalloc(countersize);
665
666 if (counters == NULL)
667 return ERR_PTR(-ENOMEM);
668
669 get_counters(private, counters);
670
671 return counters;
672 }
673
674 static int copy_entries_to_user(unsigned int total_size,
675 const struct xt_table *table,
676 void __user *userptr)
677 {
678 unsigned int off, num;
679 const struct arpt_entry *e;
680 struct xt_counters *counters;
681 struct xt_table_info *private = table->private;
682 int ret = 0;
683 void *loc_cpu_entry;
684
685 counters = alloc_counters(table);
686 if (IS_ERR(counters))
687 return PTR_ERR(counters);
688
689 loc_cpu_entry = private->entries;
690
691 /* FIXME: use iterator macros --RR */
692 /* ... then go back and fix counters and names */
693 for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
694 const struct xt_entry_target *t;
695
696 e = loc_cpu_entry + off;
697 if (copy_to_user(userptr + off, e, sizeof(*e))) {
698 ret = -EFAULT;
699 goto free_counters;
700 }
701 if (copy_to_user(userptr + off
702 + offsetof(struct arpt_entry, counters),
703 &counters[num],
704 sizeof(counters[num])) != 0) {
705 ret = -EFAULT;
706 goto free_counters;
707 }
708
709 t = arpt_get_target_c(e);
710 if (xt_target_to_user(t, userptr + off + e->target_offset)) {
711 ret = -EFAULT;
712 goto free_counters;
713 }
714 }
715
716 free_counters:
717 vfree(counters);
718 return ret;
719 }
720
721 #ifdef CONFIG_COMPAT
722 static void compat_standard_from_user(void *dst, const void *src)
723 {
724 int v = *(compat_int_t *)src;
725
726 if (v > 0)
727 v += xt_compat_calc_jump(NFPROTO_ARP, v);
728 memcpy(dst, &v, sizeof(v));
729 }
730
731 static int compat_standard_to_user(void __user *dst, const void *src)
732 {
733 compat_int_t cv = *(int *)src;
734
735 if (cv > 0)
736 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
737 return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
738 }
739
740 static int compat_calc_entry(const struct arpt_entry *e,
741 const struct xt_table_info *info,
742 const void *base, struct xt_table_info *newinfo)
743 {
744 const struct xt_entry_target *t;
745 unsigned int entry_offset;
746 int off, i, ret;
747
748 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
749 entry_offset = (void *)e - base;
750
751 t = arpt_get_target_c(e);
752 off += xt_compat_target_offset(t->u.kernel.target);
753 newinfo->size -= off;
754 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
755 if (ret)
756 return ret;
757
758 for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
759 if (info->hook_entry[i] &&
760 (e < (struct arpt_entry *)(base + info->hook_entry[i])))
761 newinfo->hook_entry[i] -= off;
762 if (info->underflow[i] &&
763 (e < (struct arpt_entry *)(base + info->underflow[i])))
764 newinfo->underflow[i] -= off;
765 }
766 return 0;
767 }
768
769 static int compat_table_info(const struct xt_table_info *info,
770 struct xt_table_info *newinfo)
771 {
772 struct arpt_entry *iter;
773 const void *loc_cpu_entry;
774 int ret;
775
776 if (!newinfo || !info)
777 return -EINVAL;
778
779 /* we dont care about newinfo->entries */
780 memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
781 newinfo->initial_entries = 0;
782 loc_cpu_entry = info->entries;
783 xt_compat_init_offsets(NFPROTO_ARP, info->number);
784 xt_entry_foreach(iter, loc_cpu_entry, info->size) {
785 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
786 if (ret != 0)
787 return ret;
788 }
789 return 0;
790 }
791 #endif
792
793 static int get_info(struct net *net, void __user *user,
794 const int *len, int compat)
795 {
796 char name[XT_TABLE_MAXNAMELEN];
797 struct xt_table *t;
798 int ret;
799
800 if (*len != sizeof(struct arpt_getinfo))
801 return -EINVAL;
802
803 if (copy_from_user(name, user, sizeof(name)) != 0)
804 return -EFAULT;
805
806 name[XT_TABLE_MAXNAMELEN-1] = '\0';
807 #ifdef CONFIG_COMPAT
808 if (compat)
809 xt_compat_lock(NFPROTO_ARP);
810 #endif
811 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
812 "arptable_%s", name);
813 if (t) {
814 struct arpt_getinfo info;
815 const struct xt_table_info *private = t->private;
816 #ifdef CONFIG_COMPAT
817 struct xt_table_info tmp;
818
819 if (compat) {
820 ret = compat_table_info(private, &tmp);
821 xt_compat_flush_offsets(NFPROTO_ARP);
822 private = &tmp;
823 }
824 #endif
825 memset(&info, 0, sizeof(info));
826 info.valid_hooks = t->valid_hooks;
827 memcpy(info.hook_entry, private->hook_entry,
828 sizeof(info.hook_entry));
829 memcpy(info.underflow, private->underflow,
830 sizeof(info.underflow));
831 info.num_entries = private->number;
832 info.size = private->size;
833 strcpy(info.name, name);
834
835 if (copy_to_user(user, &info, *len) != 0)
836 ret = -EFAULT;
837 else
838 ret = 0;
839 xt_table_unlock(t);
840 module_put(t->me);
841 } else
842 ret = -ENOENT;
843 #ifdef CONFIG_COMPAT
844 if (compat)
845 xt_compat_unlock(NFPROTO_ARP);
846 #endif
847 return ret;
848 }
849
850 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
851 const int *len)
852 {
853 int ret;
854 struct arpt_get_entries get;
855 struct xt_table *t;
856
857 if (*len < sizeof(get))
858 return -EINVAL;
859 if (copy_from_user(&get, uptr, sizeof(get)) != 0)
860 return -EFAULT;
861 if (*len != sizeof(struct arpt_get_entries) + get.size)
862 return -EINVAL;
863
864 get.name[sizeof(get.name) - 1] = '\0';
865
866 t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
867 if (t) {
868 const struct xt_table_info *private = t->private;
869
870 if (get.size == private->size)
871 ret = copy_entries_to_user(private->size,
872 t, uptr->entrytable);
873 else
874 ret = -EAGAIN;
875
876 module_put(t->me);
877 xt_table_unlock(t);
878 } else
879 ret = -ENOENT;
880
881 return ret;
882 }
883
884 static int __do_replace(struct net *net, const char *name,
885 unsigned int valid_hooks,
886 struct xt_table_info *newinfo,
887 unsigned int num_counters,
888 void __user *counters_ptr)
889 {
890 int ret;
891 struct xt_table *t;
892 struct xt_table_info *oldinfo;
893 struct xt_counters *counters;
894 void *loc_cpu_old_entry;
895 struct arpt_entry *iter;
896
897 ret = 0;
898 counters = vzalloc(num_counters * sizeof(struct xt_counters));
899 if (!counters) {
900 ret = -ENOMEM;
901 goto out;
902 }
903
904 t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
905 "arptable_%s", name);
906 if (!t) {
907 ret = -ENOENT;
908 goto free_newinfo_counters_untrans;
909 }
910
911 /* You lied! */
912 if (valid_hooks != t->valid_hooks) {
913 ret = -EINVAL;
914 goto put_module;
915 }
916
917 oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
918 if (!oldinfo)
919 goto put_module;
920
921 /* Update module usage count based on number of rules */
922 if ((oldinfo->number > oldinfo->initial_entries) ||
923 (newinfo->number <= oldinfo->initial_entries))
924 module_put(t->me);
925 if ((oldinfo->number > oldinfo->initial_entries) &&
926 (newinfo->number <= oldinfo->initial_entries))
927 module_put(t->me);
928
929 /* Get the old counters, and synchronize with replace */
930 get_counters(oldinfo, counters);
931
932 /* Decrease module usage counts and free resource */
933 loc_cpu_old_entry = oldinfo->entries;
934 xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
935 cleanup_entry(iter);
936
937 xt_free_table_info(oldinfo);
938 if (copy_to_user(counters_ptr, counters,
939 sizeof(struct xt_counters) * num_counters) != 0) {
940 /* Silent error, can't fail, new table is already in place */
941 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
942 }
943 vfree(counters);
944 xt_table_unlock(t);
945 return ret;
946
947 put_module:
948 module_put(t->me);
949 xt_table_unlock(t);
950 free_newinfo_counters_untrans:
951 vfree(counters);
952 out:
953 return ret;
954 }
955
956 static int do_replace(struct net *net, const void __user *user,
957 unsigned int len)
958 {
959 int ret;
960 struct arpt_replace tmp;
961 struct xt_table_info *newinfo;
962 void *loc_cpu_entry;
963 struct arpt_entry *iter;
964
965 if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
966 return -EFAULT;
967
968 /* overflow check */
969 if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
970 return -ENOMEM;
971 if (tmp.num_counters == 0)
972 return -EINVAL;
973
974 tmp.name[sizeof(tmp.name)-1] = 0;
975
976 newinfo = xt_alloc_table_info(tmp.size);
977 if (!newinfo)
978 return -ENOMEM;
979
980 loc_cpu_entry = newinfo->entries;
981 if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
982 tmp.size) != 0) {
983 ret = -EFAULT;
984 goto free_newinfo;
985 }
986
987 ret = translate_table(newinfo, loc_cpu_entry, &tmp);
988 if (ret != 0)
989 goto free_newinfo;
990
991 ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
992 tmp.num_counters, tmp.counters);
993 if (ret)
994 goto free_newinfo_untrans;
995 return 0;
996
997 free_newinfo_untrans:
998 xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
999 cleanup_entry(iter);
1000 free_newinfo:
1001 xt_free_table_info(newinfo);
1002 return ret;
1003 }
1004
1005 static int do_add_counters(struct net *net, const void __user *user,
1006 unsigned int len, int compat)
1007 {
1008 unsigned int i;
1009 struct xt_counters_info tmp;
1010 struct xt_counters *paddc;
1011 struct xt_table *t;
1012 const struct xt_table_info *private;
1013 int ret = 0;
1014 struct arpt_entry *iter;
1015 unsigned int addend;
1016
1017 paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1018 if (IS_ERR(paddc))
1019 return PTR_ERR(paddc);
1020
1021 t = xt_find_table_lock(net, NFPROTO_ARP, tmp.name);
1022 if (!t) {
1023 ret = -ENOENT;
1024 goto free;
1025 }
1026
1027 local_bh_disable();
1028 private = t->private;
1029 if (private->number != tmp.num_counters) {
1030 ret = -EINVAL;
1031 goto unlock_up_free;
1032 }
1033
1034 i = 0;
1035
1036 addend = xt_write_recseq_begin();
1037 xt_entry_foreach(iter, private->entries, private->size) {
1038 struct xt_counters *tmp;
1039
1040 tmp = xt_get_this_cpu_counter(&iter->counters);
1041 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1042 ++i;
1043 }
1044 xt_write_recseq_end(addend);
1045 unlock_up_free:
1046 local_bh_enable();
1047 xt_table_unlock(t);
1048 module_put(t->me);
1049 free:
1050 vfree(paddc);
1051
1052 return ret;
1053 }
1054
1055 #ifdef CONFIG_COMPAT
1056 struct compat_arpt_replace {
1057 char name[XT_TABLE_MAXNAMELEN];
1058 u32 valid_hooks;
1059 u32 num_entries;
1060 u32 size;
1061 u32 hook_entry[NF_ARP_NUMHOOKS];
1062 u32 underflow[NF_ARP_NUMHOOKS];
1063 u32 num_counters;
1064 compat_uptr_t counters;
1065 struct compat_arpt_entry entries[0];
1066 };
1067
1068 static inline void compat_release_entry(struct compat_arpt_entry *e)
1069 {
1070 struct xt_entry_target *t;
1071
1072 t = compat_arpt_get_target(e);
1073 module_put(t->u.kernel.target->me);
1074 }
1075
1076 static int
1077 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1078 struct xt_table_info *newinfo,
1079 unsigned int *size,
1080 const unsigned char *base,
1081 const unsigned char *limit)
1082 {
1083 struct xt_entry_target *t;
1084 struct xt_target *target;
1085 unsigned int entry_offset;
1086 int ret, off;
1087
1088 if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1089 (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit ||
1090 (unsigned char *)e + e->next_offset > limit)
1091 return -EINVAL;
1092
1093 if (e->next_offset < sizeof(struct compat_arpt_entry) +
1094 sizeof(struct compat_xt_entry_target))
1095 return -EINVAL;
1096
1097 if (!arp_checkentry(&e->arp))
1098 return -EINVAL;
1099
1100 ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset,
1101 e->next_offset);
1102 if (ret)
1103 return ret;
1104
1105 off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1106 entry_offset = (void *)e - (void *)base;
1107
1108 t = compat_arpt_get_target(e);
1109 target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1110 t->u.user.revision);
1111 if (IS_ERR(target)) {
1112 ret = PTR_ERR(target);
1113 goto out;
1114 }
1115 t->u.kernel.target = target;
1116
1117 off += xt_compat_target_offset(target);
1118 *size += off;
1119 ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1120 if (ret)
1121 goto release_target;
1122
1123 return 0;
1124
1125 release_target:
1126 module_put(t->u.kernel.target->me);
1127 out:
1128 return ret;
1129 }
1130
1131 static void
1132 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1133 unsigned int *size,
1134 struct xt_table_info *newinfo, unsigned char *base)
1135 {
1136 struct xt_entry_target *t;
1137 struct xt_target *target;
1138 struct arpt_entry *de;
1139 unsigned int origsize;
1140 int h;
1141
1142 origsize = *size;
1143 de = *dstptr;
1144 memcpy(de, e, sizeof(struct arpt_entry));
1145 memcpy(&de->counters, &e->counters, sizeof(e->counters));
1146
1147 *dstptr += sizeof(struct arpt_entry);
1148 *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1149
1150 de->target_offset = e->target_offset - (origsize - *size);
1151 t = compat_arpt_get_target(e);
1152 target = t->u.kernel.target;
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);