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