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