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