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