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