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