Merge branch 'for-4.14-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[sfrench/cifs-2.6.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 struct xt_counters *alloc_counters(const struct xt_table *table)
638 {
639         unsigned int countersize;
640         struct xt_counters *counters;
641         const struct xt_table_info *private = table->private;
642
643         /* We need atomic snapshot of counters: rest doesn't change
644          * (other than comefrom, which userspace doesn't care
645          * about).
646          */
647         countersize = sizeof(struct xt_counters) * private->number;
648         counters = vzalloc(countersize);
649
650         if (counters == NULL)
651                 return ERR_PTR(-ENOMEM);
652
653         get_counters(private, counters);
654
655         return counters;
656 }
657
658 static int copy_entries_to_user(unsigned int total_size,
659                                 const struct xt_table *table,
660                                 void __user *userptr)
661 {
662         unsigned int off, num;
663         const struct arpt_entry *e;
664         struct xt_counters *counters;
665         struct xt_table_info *private = table->private;
666         int ret = 0;
667         void *loc_cpu_entry;
668
669         counters = alloc_counters(table);
670         if (IS_ERR(counters))
671                 return PTR_ERR(counters);
672
673         loc_cpu_entry = private->entries;
674
675         /* FIXME: use iterator macros --RR */
676         /* ... then go back and fix counters and names */
677         for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
678                 const struct xt_entry_target *t;
679
680                 e = loc_cpu_entry + off;
681                 if (copy_to_user(userptr + off, e, sizeof(*e))) {
682                         ret = -EFAULT;
683                         goto free_counters;
684                 }
685                 if (copy_to_user(userptr + off
686                                  + offsetof(struct arpt_entry, counters),
687                                  &counters[num],
688                                  sizeof(counters[num])) != 0) {
689                         ret = -EFAULT;
690                         goto free_counters;
691                 }
692
693                 t = arpt_get_target_c(e);
694                 if (xt_target_to_user(t, userptr + off + e->target_offset)) {
695                         ret = -EFAULT;
696                         goto free_counters;
697                 }
698         }
699
700  free_counters:
701         vfree(counters);
702         return ret;
703 }
704
705 #ifdef CONFIG_COMPAT
706 static void compat_standard_from_user(void *dst, const void *src)
707 {
708         int v = *(compat_int_t *)src;
709
710         if (v > 0)
711                 v += xt_compat_calc_jump(NFPROTO_ARP, v);
712         memcpy(dst, &v, sizeof(v));
713 }
714
715 static int compat_standard_to_user(void __user *dst, const void *src)
716 {
717         compat_int_t cv = *(int *)src;
718
719         if (cv > 0)
720                 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
721         return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
722 }
723
724 static int compat_calc_entry(const struct arpt_entry *e,
725                              const struct xt_table_info *info,
726                              const void *base, struct xt_table_info *newinfo)
727 {
728         const struct xt_entry_target *t;
729         unsigned int entry_offset;
730         int off, i, ret;
731
732         off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
733         entry_offset = (void *)e - base;
734
735         t = arpt_get_target_c(e);
736         off += xt_compat_target_offset(t->u.kernel.target);
737         newinfo->size -= off;
738         ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
739         if (ret)
740                 return ret;
741
742         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
743                 if (info->hook_entry[i] &&
744                     (e < (struct arpt_entry *)(base + info->hook_entry[i])))
745                         newinfo->hook_entry[i] -= off;
746                 if (info->underflow[i] &&
747                     (e < (struct arpt_entry *)(base + info->underflow[i])))
748                         newinfo->underflow[i] -= off;
749         }
750         return 0;
751 }
752
753 static int compat_table_info(const struct xt_table_info *info,
754                              struct xt_table_info *newinfo)
755 {
756         struct arpt_entry *iter;
757         const void *loc_cpu_entry;
758         int ret;
759
760         if (!newinfo || !info)
761                 return -EINVAL;
762
763         /* we dont care about newinfo->entries */
764         memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
765         newinfo->initial_entries = 0;
766         loc_cpu_entry = info->entries;
767         xt_compat_init_offsets(NFPROTO_ARP, info->number);
768         xt_entry_foreach(iter, loc_cpu_entry, info->size) {
769                 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
770                 if (ret != 0)
771                         return ret;
772         }
773         return 0;
774 }
775 #endif
776
777 static int get_info(struct net *net, void __user *user,
778                     const int *len, int compat)
779 {
780         char name[XT_TABLE_MAXNAMELEN];
781         struct xt_table *t;
782         int ret;
783
784         if (*len != sizeof(struct arpt_getinfo))
785                 return -EINVAL;
786
787         if (copy_from_user(name, user, sizeof(name)) != 0)
788                 return -EFAULT;
789
790         name[XT_TABLE_MAXNAMELEN-1] = '\0';
791 #ifdef CONFIG_COMPAT
792         if (compat)
793                 xt_compat_lock(NFPROTO_ARP);
794 #endif
795         t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
796                                     "arptable_%s", name);
797         if (t) {
798                 struct arpt_getinfo info;
799                 const struct xt_table_info *private = t->private;
800 #ifdef CONFIG_COMPAT
801                 struct xt_table_info tmp;
802
803                 if (compat) {
804                         ret = compat_table_info(private, &tmp);
805                         xt_compat_flush_offsets(NFPROTO_ARP);
806                         private = &tmp;
807                 }
808 #endif
809                 memset(&info, 0, sizeof(info));
810                 info.valid_hooks = t->valid_hooks;
811                 memcpy(info.hook_entry, private->hook_entry,
812                        sizeof(info.hook_entry));
813                 memcpy(info.underflow, private->underflow,
814                        sizeof(info.underflow));
815                 info.num_entries = private->number;
816                 info.size = private->size;
817                 strcpy(info.name, name);
818
819                 if (copy_to_user(user, &info, *len) != 0)
820                         ret = -EFAULT;
821                 else
822                         ret = 0;
823                 xt_table_unlock(t);
824                 module_put(t->me);
825         } else
826                 ret = -ENOENT;
827 #ifdef CONFIG_COMPAT
828         if (compat)
829                 xt_compat_unlock(NFPROTO_ARP);
830 #endif
831         return ret;
832 }
833
834 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
835                        const int *len)
836 {
837         int ret;
838         struct arpt_get_entries get;
839         struct xt_table *t;
840
841         if (*len < sizeof(get))
842                 return -EINVAL;
843         if (copy_from_user(&get, uptr, sizeof(get)) != 0)
844                 return -EFAULT;
845         if (*len != sizeof(struct arpt_get_entries) + get.size)
846                 return -EINVAL;
847
848         get.name[sizeof(get.name) - 1] = '\0';
849
850         t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
851         if (t) {
852                 const struct xt_table_info *private = t->private;
853
854                 if (get.size == private->size)
855                         ret = copy_entries_to_user(private->size,
856                                                    t, uptr->entrytable);
857                 else
858                         ret = -EAGAIN;
859
860                 module_put(t->me);
861                 xt_table_unlock(t);
862         } else
863                 ret = -ENOENT;
864
865         return ret;
866 }
867
868 static int __do_replace(struct net *net, const char *name,
869                         unsigned int valid_hooks,
870                         struct xt_table_info *newinfo,
871                         unsigned int num_counters,
872                         void __user *counters_ptr)
873 {
874         int ret;
875         struct xt_table *t;
876         struct xt_table_info *oldinfo;
877         struct xt_counters *counters;
878         void *loc_cpu_old_entry;
879         struct arpt_entry *iter;
880
881         ret = 0;
882         counters = vzalloc(num_counters * sizeof(struct xt_counters));
883         if (!counters) {
884                 ret = -ENOMEM;
885                 goto out;
886         }
887
888         t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
889                                     "arptable_%s", name);
890         if (!t) {
891                 ret = -ENOENT;
892                 goto free_newinfo_counters_untrans;
893         }
894
895         /* You lied! */
896         if (valid_hooks != t->valid_hooks) {
897                 ret = -EINVAL;
898                 goto put_module;
899         }
900
901         oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
902         if (!oldinfo)
903                 goto put_module;
904
905         /* Update module usage count based on number of rules */
906         if ((oldinfo->number > oldinfo->initial_entries) ||
907             (newinfo->number <= oldinfo->initial_entries))
908                 module_put(t->me);
909         if ((oldinfo->number > oldinfo->initial_entries) &&
910             (newinfo->number <= oldinfo->initial_entries))
911                 module_put(t->me);
912
913         /* Get the old counters, and synchronize with replace */
914         get_counters(oldinfo, counters);
915
916         /* Decrease module usage counts and free resource */
917         loc_cpu_old_entry = oldinfo->entries;
918         xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
919                 cleanup_entry(iter);
920
921         xt_free_table_info(oldinfo);
922         if (copy_to_user(counters_ptr, counters,
923                          sizeof(struct xt_counters) * num_counters) != 0) {
924                 /* Silent error, can't fail, new table is already in place */
925                 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
926         }
927         vfree(counters);
928         xt_table_unlock(t);
929         return ret;
930
931  put_module:
932         module_put(t->me);
933         xt_table_unlock(t);
934  free_newinfo_counters_untrans:
935         vfree(counters);
936  out:
937         return ret;
938 }
939
940 static int do_replace(struct net *net, const void __user *user,
941                       unsigned int len)
942 {
943         int ret;
944         struct arpt_replace tmp;
945         struct xt_table_info *newinfo;
946         void *loc_cpu_entry;
947         struct arpt_entry *iter;
948
949         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
950                 return -EFAULT;
951
952         /* overflow check */
953         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
954                 return -ENOMEM;
955         if (tmp.num_counters == 0)
956                 return -EINVAL;
957
958         tmp.name[sizeof(tmp.name)-1] = 0;
959
960         newinfo = xt_alloc_table_info(tmp.size);
961         if (!newinfo)
962                 return -ENOMEM;
963
964         loc_cpu_entry = newinfo->entries;
965         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
966                            tmp.size) != 0) {
967                 ret = -EFAULT;
968                 goto free_newinfo;
969         }
970
971         ret = translate_table(newinfo, loc_cpu_entry, &tmp);
972         if (ret != 0)
973                 goto free_newinfo;
974
975         ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
976                            tmp.num_counters, tmp.counters);
977         if (ret)
978                 goto free_newinfo_untrans;
979         return 0;
980
981  free_newinfo_untrans:
982         xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
983                 cleanup_entry(iter);
984  free_newinfo:
985         xt_free_table_info(newinfo);
986         return ret;
987 }
988
989 static int do_add_counters(struct net *net, const void __user *user,
990                            unsigned int len, int compat)
991 {
992         unsigned int i;
993         struct xt_counters_info tmp;
994         struct xt_counters *paddc;
995         struct xt_table *t;
996         const struct xt_table_info *private;
997         int ret = 0;
998         struct arpt_entry *iter;
999         unsigned int addend;
1000
1001         paddc = xt_copy_counters_from_user(user, len, &tmp, compat);
1002         if (IS_ERR(paddc))
1003                 return PTR_ERR(paddc);
1004
1005         t = xt_find_table_lock(net, NFPROTO_ARP, tmp.name);
1006         if (!t) {
1007                 ret = -ENOENT;
1008                 goto free;
1009         }
1010
1011         local_bh_disable();
1012         private = t->private;
1013         if (private->number != tmp.num_counters) {
1014                 ret = -EINVAL;
1015                 goto unlock_up_free;
1016         }
1017
1018         i = 0;
1019
1020         addend = xt_write_recseq_begin();
1021         xt_entry_foreach(iter,  private->entries, private->size) {
1022                 struct xt_counters *tmp;
1023
1024                 tmp = xt_get_this_cpu_counter(&iter->counters);
1025                 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1026                 ++i;
1027         }
1028         xt_write_recseq_end(addend);
1029  unlock_up_free:
1030         local_bh_enable();
1031         xt_table_unlock(t);
1032         module_put(t->me);
1033  free:
1034         vfree(paddc);
1035
1036         return ret;
1037 }
1038
1039 #ifdef CONFIG_COMPAT
1040 struct compat_arpt_replace {
1041         char                            name[XT_TABLE_MAXNAMELEN];
1042         u32                             valid_hooks;
1043         u32                             num_entries;
1044         u32                             size;
1045         u32                             hook_entry[NF_ARP_NUMHOOKS];
1046         u32                             underflow[NF_ARP_NUMHOOKS];
1047         u32                             num_counters;
1048         compat_uptr_t                   counters;
1049         struct compat_arpt_entry        entries[0];
1050 };
1051
1052 static inline void compat_release_entry(struct compat_arpt_entry *e)
1053 {
1054         struct xt_entry_target *t;
1055
1056         t = compat_arpt_get_target(e);
1057         module_put(t->u.kernel.target->me);
1058 }
1059
1060 static int
1061 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1062                                   struct xt_table_info *newinfo,
1063                                   unsigned int *size,
1064                                   const unsigned char *base,
1065                                   const unsigned char *limit)
1066 {
1067         struct xt_entry_target *t;
1068         struct xt_target *target;
1069         unsigned int entry_offset;
1070         int ret, off;
1071
1072         if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1073             (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit ||
1074             (unsigned char *)e + e->next_offset > limit)
1075                 return -EINVAL;
1076
1077         if (e->next_offset < sizeof(struct compat_arpt_entry) +
1078                              sizeof(struct compat_xt_entry_target))
1079                 return -EINVAL;
1080
1081         if (!arp_checkentry(&e->arp))
1082                 return -EINVAL;
1083
1084         ret = xt_compat_check_entry_offsets(e, e->elems, e->target_offset,
1085                                             e->next_offset);
1086         if (ret)
1087                 return ret;
1088
1089         off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1090         entry_offset = (void *)e - (void *)base;
1091
1092         t = compat_arpt_get_target(e);
1093         target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1094                                         t->u.user.revision);
1095         if (IS_ERR(target)) {
1096                 ret = PTR_ERR(target);
1097                 goto out;
1098         }
1099         t->u.kernel.target = target;
1100
1101         off += xt_compat_target_offset(target);
1102         *size += off;
1103         ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1104         if (ret)
1105                 goto release_target;
1106
1107         return 0;
1108
1109 release_target:
1110         module_put(t->u.kernel.target->me);
1111 out:
1112         return ret;
1113 }
1114
1115 static void
1116 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1117                             unsigned int *size,
1118                             struct xt_table_info *newinfo, unsigned char *base)
1119 {
1120         struct xt_entry_target *t;
1121         struct arpt_entry *de;
1122         unsigned int origsize;
1123         int h;
1124
1125         origsize = *size;
1126         de = *dstptr;
1127         memcpy(de, e, sizeof(struct arpt_entry));
1128         memcpy(&de->counters, &e->counters, sizeof(e->counters));
1129
1130         *dstptr += sizeof(struct arpt_entry);
1131         *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1132
1133         de->target_offset = e->target_offset - (origsize - *size);
1134         t = compat_arpt_get_target(e);
1135         xt_compat_target_from_user(t, dstptr, size);
1136
1137         de->next_offset = e->next_offset - (origsize - *size);
1138         for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1139                 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1140                         newinfo->hook_entry[h] -= origsize - *size;
1141                 if ((unsigned char *)de - base < newinfo->underflow[h])
1142                         newinfo->underflow[h] -= origsize - *size;
1143         }
1144 }
1145
1146 static int translate_compat_table(struct xt_table_info **pinfo,
1147                                   void **pentry0,
1148                                   const struct compat_arpt_replace *compatr)
1149 {
1150         unsigned int i, j;
1151         struct xt_table_info *newinfo, *info;
1152         void *pos, *entry0, *entry1;
1153         struct compat_arpt_entry *iter0;
1154         struct arpt_replace repl;
1155         unsigned int size;
1156         int ret = 0;
1157
1158         info = *pinfo;
1159         entry0 = *pentry0;
1160         size = compatr->size;
1161         info->number = compatr->num_entries;
1162
1163         j = 0;
1164         xt_compat_lock(NFPROTO_ARP);
1165         xt_compat_init_offsets(NFPROTO_ARP, compatr->num_entries);
1166         /* Walk through entries, checking offsets. */
1167         xt_entry_foreach(iter0, entry0, compatr->size) {
1168                 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1169                                                         entry0,
1170                                                         entry0 + compatr->size);
1171                 if (ret != 0)
1172                         goto out_unlock;
1173                 ++j;
1174         }
1175
1176         ret = -EINVAL;
1177         if (j != compatr->num_entries)
1178                 goto out_unlock;
1179
1180         ret = -ENOMEM;
1181         newinfo = xt_alloc_table_info(size);
1182         if (!newinfo)
1183                 goto out_unlock;
1184
1185         newinfo->number = compatr->num_entries;
1186         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1187                 newinfo->hook_entry[i] = compatr->hook_entry[i];
1188                 newinfo->underflow[i] = compatr->underflow[i];
1189         }
1190         entry1 = newinfo->entries;
1191         pos = entry1;
1192         size = compatr->size;
1193         xt_entry_foreach(iter0, entry0, compatr->size)
1194                 compat_copy_entry_from_user(iter0, &pos, &size,
1195                                             newinfo, entry1);
1196
1197         /* all module references in entry0 are now gone */
1198
1199         xt_compat_flush_offsets(NFPROTO_ARP);
1200         xt_compat_unlock(NFPROTO_ARP);
1201
1202         memcpy(&repl, compatr, sizeof(*compatr));
1203
1204         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1205                 repl.hook_entry[i] = newinfo->hook_entry[i];
1206                 repl.underflow[i] = newinfo->underflow[i];
1207         }
1208
1209         repl.num_counters = 0;
1210         repl.counters = NULL;
1211         repl.size = newinfo->size;
1212         ret = translate_table(newinfo, entry1, &repl);
1213         if (ret)
1214                 goto free_newinfo;
1215
1216         *pinfo = newinfo;
1217         *pentry0 = entry1;
1218         xt_free_table_info(info);
1219         return 0;
1220
1221 free_newinfo:
1222         xt_free_table_info(newinfo);
1223         return ret;
1224 out_unlock:
1225         xt_compat_flush_offsets(NFPROTO_ARP);
1226         xt_compat_unlock(NFPROTO_ARP);
1227         xt_entry_foreach(iter0, entry0, compatr->size) {
1228                 if (j-- == 0)
1229                         break;
1230                 compat_release_entry(iter0);
1231         }
1232         return ret;
1233 }
1234
1235 static int compat_do_replace(struct net *net, void __user *user,
1236                              unsigned int len)
1237 {
1238         int ret;
1239         struct compat_arpt_replace tmp;
1240         struct xt_table_info *newinfo;
1241         void *loc_cpu_entry;
1242         struct arpt_entry *iter;
1243
1244         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1245                 return -EFAULT;
1246
1247         /* overflow check */
1248         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1249                 return -ENOMEM;
1250         if (tmp.num_counters == 0)
1251                 return -EINVAL;
1252
1253         tmp.name[sizeof(tmp.name)-1] = 0;
1254
1255         newinfo = xt_alloc_table_info(tmp.size);
1256         if (!newinfo)
1257                 return -ENOMEM;
1258
1259         loc_cpu_entry = newinfo->entries;
1260         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1261                 ret = -EFAULT;
1262                 goto free_newinfo;
1263         }
1264
1265         ret = translate_compat_table(&newinfo, &loc_cpu_entry, &tmp);
1266         if (ret != 0)
1267                 goto free_newinfo;
1268
1269         ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1270                            tmp.num_counters, compat_ptr(tmp.counters));
1271         if (ret)
1272                 goto free_newinfo_untrans;
1273         return 0;
1274
1275  free_newinfo_untrans:
1276         xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1277                 cleanup_entry(iter);
1278  free_newinfo:
1279         xt_free_table_info(newinfo);
1280         return ret;
1281 }
1282
1283 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1284                                   unsigned int len)
1285 {
1286         int ret;
1287
1288         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1289                 return -EPERM;
1290
1291         switch (cmd) {
1292         case ARPT_SO_SET_REPLACE:
1293                 ret = compat_do_replace(sock_net(sk), user, len);
1294                 break;
1295
1296         case ARPT_SO_SET_ADD_COUNTERS:
1297                 ret = do_add_counters(sock_net(sk), user, len, 1);
1298                 break;
1299
1300         default:
1301                 ret = -EINVAL;
1302         }
1303
1304         return ret;
1305 }
1306
1307 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1308                                      compat_uint_t *size,
1309                                      struct xt_counters *counters,
1310                                      unsigned int i)
1311 {
1312         struct xt_entry_target *t;
1313         struct compat_arpt_entry __user *ce;
1314         u_int16_t target_offset, next_offset;
1315         compat_uint_t origsize;
1316         int ret;
1317
1318         origsize = *size;
1319         ce = *dstptr;
1320         if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1321             copy_to_user(&ce->counters, &counters[i],
1322             sizeof(counters[i])) != 0)
1323                 return -EFAULT;
1324
1325         *dstptr += sizeof(struct compat_arpt_entry);
1326         *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1327
1328         target_offset = e->target_offset - (origsize - *size);
1329
1330         t = arpt_get_target(e);
1331         ret = xt_compat_target_to_user(t, dstptr, size);
1332         if (ret)
1333                 return ret;
1334         next_offset = e->next_offset - (origsize - *size);
1335         if (put_user(target_offset, &ce->target_offset) != 0 ||
1336             put_user(next_offset, &ce->next_offset) != 0)
1337                 return -EFAULT;
1338         return 0;
1339 }
1340
1341 static int compat_copy_entries_to_user(unsigned int total_size,
1342                                        struct xt_table *table,
1343                                        void __user *userptr)
1344 {
1345         struct xt_counters *counters;
1346         const struct xt_table_info *private = table->private;
1347         void __user *pos;
1348         unsigned int size;
1349         int ret = 0;
1350         unsigned int i = 0;
1351         struct arpt_entry *iter;
1352
1353         counters = alloc_counters(table);
1354         if (IS_ERR(counters))
1355                 return PTR_ERR(counters);
1356
1357         pos = userptr;
1358         size = total_size;
1359         xt_entry_foreach(iter, private->entries, total_size) {
1360                 ret = compat_copy_entry_to_user(iter, &pos,
1361                                                 &size, counters, i++);
1362                 if (ret != 0)
1363                         break;
1364         }
1365         vfree(counters);
1366         return ret;
1367 }
1368
1369 struct compat_arpt_get_entries {
1370         char name[XT_TABLE_MAXNAMELEN];
1371         compat_uint_t size;
1372         struct compat_arpt_entry entrytable[0];
1373 };
1374
1375 static int compat_get_entries(struct net *net,
1376                               struct compat_arpt_get_entries __user *uptr,
1377                               int *len)
1378 {
1379         int ret;
1380         struct compat_arpt_get_entries get;
1381         struct xt_table *t;
1382
1383         if (*len < sizeof(get))
1384                 return -EINVAL;
1385         if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1386                 return -EFAULT;
1387         if (*len != sizeof(struct compat_arpt_get_entries) + get.size)
1388                 return -EINVAL;
1389
1390         get.name[sizeof(get.name) - 1] = '\0';
1391
1392         xt_compat_lock(NFPROTO_ARP);
1393         t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1394         if (t) {
1395                 const struct xt_table_info *private = t->private;
1396                 struct xt_table_info info;
1397
1398                 ret = compat_table_info(private, &info);
1399                 if (!ret && get.size == info.size) {
1400                         ret = compat_copy_entries_to_user(private->size,
1401                                                           t, uptr->entrytable);
1402                 } else if (!ret)
1403                         ret = -EAGAIN;
1404
1405                 xt_compat_flush_offsets(NFPROTO_ARP);
1406                 module_put(t->me);
1407                 xt_table_unlock(t);
1408         } else
1409                 ret = -ENOENT;
1410
1411         xt_compat_unlock(NFPROTO_ARP);
1412         return ret;
1413 }
1414
1415 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1416
1417 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1418                                   int *len)
1419 {
1420         int ret;
1421
1422         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1423                 return -EPERM;
1424
1425         switch (cmd) {
1426         case ARPT_SO_GET_INFO:
1427                 ret = get_info(sock_net(sk), user, len, 1);
1428                 break;
1429         case ARPT_SO_GET_ENTRIES:
1430                 ret = compat_get_entries(sock_net(sk), user, len);
1431                 break;
1432         default:
1433                 ret = do_arpt_get_ctl(sk, cmd, user, len);
1434         }
1435         return ret;
1436 }
1437 #endif
1438
1439 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1440 {
1441         int ret;
1442
1443         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1444                 return -EPERM;
1445
1446         switch (cmd) {
1447         case ARPT_SO_SET_REPLACE:
1448                 ret = do_replace(sock_net(sk), user, len);
1449                 break;
1450
1451         case ARPT_SO_SET_ADD_COUNTERS:
1452                 ret = do_add_counters(sock_net(sk), user, len, 0);
1453                 break;
1454
1455         default:
1456                 ret = -EINVAL;
1457         }
1458
1459         return ret;
1460 }
1461
1462 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1463 {
1464         int ret;
1465
1466         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1467                 return -EPERM;
1468
1469         switch (cmd) {
1470         case ARPT_SO_GET_INFO:
1471                 ret = get_info(sock_net(sk), user, len, 0);
1472                 break;
1473
1474         case ARPT_SO_GET_ENTRIES:
1475                 ret = get_entries(sock_net(sk), user, len);
1476                 break;
1477
1478         case ARPT_SO_GET_REVISION_TARGET: {
1479                 struct xt_get_revision rev;
1480
1481                 if (*len != sizeof(rev)) {
1482                         ret = -EINVAL;
1483                         break;
1484                 }
1485                 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1486                         ret = -EFAULT;
1487                         break;
1488                 }
1489                 rev.name[sizeof(rev.name)-1] = 0;
1490
1491                 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1492                                                          rev.revision, 1, &ret),
1493                                         "arpt_%s", rev.name);
1494                 break;
1495         }
1496
1497         default:
1498                 ret = -EINVAL;
1499         }
1500
1501         return ret;
1502 }
1503
1504 static void __arpt_unregister_table(struct xt_table *table)
1505 {
1506         struct xt_table_info *private;
1507         void *loc_cpu_entry;
1508         struct module *table_owner = table->me;
1509         struct arpt_entry *iter;
1510
1511         private = xt_unregister_table(table);
1512
1513         /* Decrease module usage counts and free resources */
1514         loc_cpu_entry = private->entries;
1515         xt_entry_foreach(iter, loc_cpu_entry, private->size)
1516                 cleanup_entry(iter);
1517         if (private->number > private->initial_entries)
1518                 module_put(table_owner);
1519         xt_free_table_info(private);
1520 }
1521
1522 int arpt_register_table(struct net *net,
1523                         const struct xt_table *table,
1524                         const struct arpt_replace *repl,
1525                         const struct nf_hook_ops *ops,
1526                         struct xt_table **res)
1527 {
1528         int ret;
1529         struct xt_table_info *newinfo;
1530         struct xt_table_info bootstrap = {0};
1531         void *loc_cpu_entry;
1532         struct xt_table *new_table;
1533
1534         newinfo = xt_alloc_table_info(repl->size);
1535         if (!newinfo)
1536                 return -ENOMEM;
1537
1538         loc_cpu_entry = newinfo->entries;
1539         memcpy(loc_cpu_entry, repl->entries, repl->size);
1540
1541         ret = translate_table(newinfo, loc_cpu_entry, repl);
1542         if (ret != 0)
1543                 goto out_free;
1544
1545         new_table = xt_register_table(net, table, &bootstrap, newinfo);
1546         if (IS_ERR(new_table)) {
1547                 ret = PTR_ERR(new_table);
1548                 goto out_free;
1549         }
1550
1551         /* set res now, will see skbs right after nf_register_net_hooks */
1552         WRITE_ONCE(*res, new_table);
1553
1554         ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1555         if (ret != 0) {
1556                 __arpt_unregister_table(new_table);
1557                 *res = NULL;
1558         }
1559
1560         return ret;
1561
1562 out_free:
1563         xt_free_table_info(newinfo);
1564         return ret;
1565 }
1566
1567 void arpt_unregister_table(struct net *net, struct xt_table *table,
1568                            const struct nf_hook_ops *ops)
1569 {
1570         nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1571         __arpt_unregister_table(table);
1572 }
1573
1574 /* The built-in targets: standard (NULL) and error. */
1575 static struct xt_target arpt_builtin_tg[] __read_mostly = {
1576         {
1577                 .name             = XT_STANDARD_TARGET,
1578                 .targetsize       = sizeof(int),
1579                 .family           = NFPROTO_ARP,
1580 #ifdef CONFIG_COMPAT
1581                 .compatsize       = sizeof(compat_int_t),
1582                 .compat_from_user = compat_standard_from_user,
1583                 .compat_to_user   = compat_standard_to_user,
1584 #endif
1585         },
1586         {
1587                 .name             = XT_ERROR_TARGET,
1588                 .target           = arpt_error,
1589                 .targetsize       = XT_FUNCTION_MAXNAMELEN,
1590                 .family           = NFPROTO_ARP,
1591         },
1592 };
1593
1594 static struct nf_sockopt_ops arpt_sockopts = {
1595         .pf             = PF_INET,
1596         .set_optmin     = ARPT_BASE_CTL,
1597         .set_optmax     = ARPT_SO_SET_MAX+1,
1598         .set            = do_arpt_set_ctl,
1599 #ifdef CONFIG_COMPAT
1600         .compat_set     = compat_do_arpt_set_ctl,
1601 #endif
1602         .get_optmin     = ARPT_BASE_CTL,
1603         .get_optmax     = ARPT_SO_GET_MAX+1,
1604         .get            = do_arpt_get_ctl,
1605 #ifdef CONFIG_COMPAT
1606         .compat_get     = compat_do_arpt_get_ctl,
1607 #endif
1608         .owner          = THIS_MODULE,
1609 };
1610
1611 static int __net_init arp_tables_net_init(struct net *net)
1612 {
1613         return xt_proto_init(net, NFPROTO_ARP);
1614 }
1615
1616 static void __net_exit arp_tables_net_exit(struct net *net)
1617 {
1618         xt_proto_fini(net, NFPROTO_ARP);
1619 }
1620
1621 static struct pernet_operations arp_tables_net_ops = {
1622         .init = arp_tables_net_init,
1623         .exit = arp_tables_net_exit,
1624 };
1625
1626 static int __init arp_tables_init(void)
1627 {
1628         int ret;
1629
1630         ret = register_pernet_subsys(&arp_tables_net_ops);
1631         if (ret < 0)
1632                 goto err1;
1633
1634         /* No one else will be downing sem now, so we won't sleep */
1635         ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1636         if (ret < 0)
1637                 goto err2;
1638
1639         /* Register setsockopt */
1640         ret = nf_register_sockopt(&arpt_sockopts);
1641         if (ret < 0)
1642                 goto err4;
1643
1644         pr_info("arp_tables: (C) 2002 David S. Miller\n");
1645         return 0;
1646
1647 err4:
1648         xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1649 err2:
1650         unregister_pernet_subsys(&arp_tables_net_ops);
1651 err1:
1652         return ret;
1653 }
1654
1655 static void __exit arp_tables_fini(void)
1656 {
1657         nf_unregister_sockopt(&arpt_sockopts);
1658         xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1659         unregister_pernet_subsys(&arp_tables_net_ops);
1660 }
1661
1662 EXPORT_SYMBOL(arpt_register_table);
1663 EXPORT_SYMBOL(arpt_unregister_table);
1664 EXPORT_SYMBOL(arpt_do_table);
1665
1666 module_init(arp_tables_init);
1667 module_exit(arp_tables_fini);