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