netfilter: xtables: compute exact size needed for jumpstack
[firefly-linux-kernel-4.4.55.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 <asm/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 /*#define DEBUG_ARP_TABLES*/
38 /*#define DEBUG_ARP_TABLES_USER*/
39
40 #ifdef DEBUG_ARP_TABLES
41 #define dprintf(format, args...)  printk(format , ## args)
42 #else
43 #define dprintf(format, args...)
44 #endif
45
46 #ifdef DEBUG_ARP_TABLES_USER
47 #define duprintf(format, args...) printk(format , ## args)
48 #else
49 #define duprintf(format, args...)
50 #endif
51
52 #ifdef CONFIG_NETFILTER_DEBUG
53 #define ARP_NF_ASSERT(x)        WARN_ON(!(x))
54 #else
55 #define ARP_NF_ASSERT(x)
56 #endif
57
58 void *arpt_alloc_initial_table(const struct xt_table *info)
59 {
60         return xt_alloc_initial_table(arpt, ARPT);
61 }
62 EXPORT_SYMBOL_GPL(arpt_alloc_initial_table);
63
64 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
65                                       const char *hdr_addr, int len)
66 {
67         int i, ret;
68
69         if (len > ARPT_DEV_ADDR_LEN_MAX)
70                 len = ARPT_DEV_ADDR_LEN_MAX;
71
72         ret = 0;
73         for (i = 0; i < len; i++)
74                 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
75
76         return ret != 0;
77 }
78
79 /*
80  * Unfortunately, _b and _mask are not aligned to an int (or long int)
81  * Some arches dont care, unrolling the loop is a win on them.
82  * For other arches, we only have a 16bit alignement.
83  */
84 static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
85 {
86 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
87         unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
88 #else
89         unsigned long ret = 0;
90         const u16 *a = (const u16 *)_a;
91         const u16 *b = (const u16 *)_b;
92         const u16 *mask = (const u16 *)_mask;
93         int i;
94
95         for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
96                 ret |= (a[i] ^ b[i]) & mask[i];
97 #endif
98         return ret;
99 }
100
101 /* Returns whether packet matches rule or not. */
102 static inline int arp_packet_match(const struct arphdr *arphdr,
103                                    struct net_device *dev,
104                                    const char *indev,
105                                    const char *outdev,
106                                    const struct arpt_arp *arpinfo)
107 {
108         const char *arpptr = (char *)(arphdr + 1);
109         const char *src_devaddr, *tgt_devaddr;
110         __be32 src_ipaddr, tgt_ipaddr;
111         long ret;
112
113 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
114
115         if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
116                   ARPT_INV_ARPOP)) {
117                 dprintf("ARP operation field mismatch.\n");
118                 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
119                         arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
120                 return 0;
121         }
122
123         if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
124                   ARPT_INV_ARPHRD)) {
125                 dprintf("ARP hardware address format mismatch.\n");
126                 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
127                         arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
128                 return 0;
129         }
130
131         if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
132                   ARPT_INV_ARPPRO)) {
133                 dprintf("ARP protocol address format mismatch.\n");
134                 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
135                         arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
136                 return 0;
137         }
138
139         if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
140                   ARPT_INV_ARPHLN)) {
141                 dprintf("ARP hardware address length mismatch.\n");
142                 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
143                         arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
144                 return 0;
145         }
146
147         src_devaddr = arpptr;
148         arpptr += dev->addr_len;
149         memcpy(&src_ipaddr, arpptr, sizeof(u32));
150         arpptr += sizeof(u32);
151         tgt_devaddr = arpptr;
152         arpptr += dev->addr_len;
153         memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
154
155         if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
156                   ARPT_INV_SRCDEVADDR) ||
157             FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
158                   ARPT_INV_TGTDEVADDR)) {
159                 dprintf("Source or target device address mismatch.\n");
160
161                 return 0;
162         }
163
164         if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
165                   ARPT_INV_SRCIP) ||
166             FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
167                   ARPT_INV_TGTIP)) {
168                 dprintf("Source or target IP address mismatch.\n");
169
170                 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
171                         &src_ipaddr,
172                         &arpinfo->smsk.s_addr,
173                         &arpinfo->src.s_addr,
174                         arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
175                 dprintf("TGT: %pI4 Mask: %pI4 Target: %pI4.%s\n",
176                         &tgt_ipaddr,
177                         &arpinfo->tmsk.s_addr,
178                         &arpinfo->tgt.s_addr,
179                         arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
180                 return 0;
181         }
182
183         /* Look for ifname matches.  */
184         ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
185
186         if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
187                 dprintf("VIA in mismatch (%s vs %s).%s\n",
188                         indev, arpinfo->iniface,
189                         arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
190                 return 0;
191         }
192
193         ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
194
195         if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
196                 dprintf("VIA out mismatch (%s vs %s).%s\n",
197                         outdev, arpinfo->outiface,
198                         arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
199                 return 0;
200         }
201
202         return 1;
203 #undef FWINV
204 }
205
206 static inline int arp_checkentry(const struct arpt_arp *arp)
207 {
208         if (arp->flags & ~ARPT_F_MASK) {
209                 duprintf("Unknown flag bits set: %08X\n",
210                          arp->flags & ~ARPT_F_MASK);
211                 return 0;
212         }
213         if (arp->invflags & ~ARPT_INV_MASK) {
214                 duprintf("Unknown invflag bits set: %08X\n",
215                          arp->invflags & ~ARPT_INV_MASK);
216                 return 0;
217         }
218
219         return 1;
220 }
221
222 static unsigned int
223 arpt_error(struct sk_buff *skb, const struct xt_action_param *par)
224 {
225         net_err_ratelimited("arp_tables: error: '%s'\n",
226                             (const char *)par->targinfo);
227
228         return NF_DROP;
229 }
230
231 static inline const struct xt_entry_target *
232 arpt_get_target_c(const struct arpt_entry *e)
233 {
234         return arpt_get_target((struct arpt_entry *)e);
235 }
236
237 static inline struct arpt_entry *
238 get_entry(const void *base, unsigned int offset)
239 {
240         return (struct arpt_entry *)(base + offset);
241 }
242
243 static inline __pure
244 struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
245 {
246         return (void *)entry + entry->next_offset;
247 }
248
249 unsigned int arpt_do_table(struct sk_buff *skb,
250                            unsigned int hook,
251                            const struct nf_hook_state *state,
252                            struct xt_table *table)
253 {
254         static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
255         unsigned int verdict = NF_DROP;
256         const struct arphdr *arp;
257         struct arpt_entry *e, **jumpstack;
258         const char *indev, *outdev;
259         const void *table_base;
260         unsigned int cpu, stackidx = 0;
261         const struct xt_table_info *private;
262         struct xt_action_param acpar;
263         unsigned int addend;
264
265         if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
266                 return NF_DROP;
267
268         indev = state->in ? state->in->name : nulldevname;
269         outdev = state->out ? state->out->name : nulldevname;
270
271         local_bh_disable();
272         addend = xt_write_recseq_begin();
273         private = table->private;
274         cpu     = smp_processor_id();
275         /*
276          * Ensure we load private-> members after we've fetched the base
277          * pointer.
278          */
279         smp_read_barrier_depends();
280         table_base = private->entries;
281         jumpstack  = (struct arpt_entry **)private->jumpstack[cpu];
282
283         e = get_entry(table_base, private->hook_entry[hook]);
284
285         acpar.in      = state->in;
286         acpar.out     = state->out;
287         acpar.hooknum = hook;
288         acpar.family  = NFPROTO_ARP;
289         acpar.hotdrop = false;
290
291         arp = arp_hdr(skb);
292         do {
293                 const struct xt_entry_target *t;
294                 struct xt_counters *counter;
295
296                 if (!arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
297                         e = arpt_next_entry(e);
298                         continue;
299                 }
300
301                 counter = xt_get_this_cpu_counter(&e->counters);
302                 ADD_COUNTER(*counter, arp_hdr_len(skb->dev), 1);
303
304                 t = arpt_get_target_c(e);
305
306                 /* Standard target? */
307                 if (!t->u.kernel.target->target) {
308                         int v;
309
310                         v = ((struct xt_standard_target *)t)->verdict;
311                         if (v < 0) {
312                                 /* Pop from stack? */
313                                 if (v != XT_RETURN) {
314                                         verdict = (unsigned int)(-v) - 1;
315                                         break;
316                                 }
317                                 if (stackidx == 0) {
318                                         e = get_entry(table_base,
319                                                       private->underflow[hook]);
320                                 } else {
321                                         e = jumpstack[--stackidx];
322                                         e = arpt_next_entry(e);
323                                 }
324                                 continue;
325                         }
326                         if (table_base + v
327                             != arpt_next_entry(e)) {
328
329                                 if (stackidx >= private->stacksize) {
330                                         verdict = NF_DROP;
331                                         break;
332                                 }
333                                 jumpstack[stackidx++] = e;
334                         }
335
336                         e = get_entry(table_base, v);
337                         continue;
338                 }
339
340                 /* Targets which reenter must return
341                  * abs. verdicts
342                  */
343                 acpar.target   = t->u.kernel.target;
344                 acpar.targinfo = t->data;
345                 verdict = t->u.kernel.target->target(skb, &acpar);
346
347                 /* Target might have changed stuff. */
348                 arp = arp_hdr(skb);
349
350                 if (verdict == XT_CONTINUE)
351                         e = arpt_next_entry(e);
352                 else
353                         /* Verdict */
354                         break;
355         } while (!acpar.hotdrop);
356         xt_write_recseq_end(addend);
357         local_bh_enable();
358
359         if (acpar.hotdrop)
360                 return NF_DROP;
361         else
362                 return verdict;
363 }
364
365 /* All zeroes == unconditional rule. */
366 static inline bool unconditional(const struct arpt_arp *arp)
367 {
368         static const struct arpt_arp uncond;
369
370         return memcmp(arp, &uncond, sizeof(uncond)) == 0;
371 }
372
373 /* Figures out from what hook each rule can be called: returns 0 if
374  * there are loops.  Puts hook bitmask in comefrom.
375  *
376  * Keeps track of largest call depth seen and stores it in newinfo->stacksize.
377  */
378 static int mark_source_chains(struct xt_table_info *newinfo,
379                               unsigned int valid_hooks, void *entry0)
380 {
381         unsigned int calldepth, max_calldepth = 0;
382         unsigned int hook;
383
384         /* No recursion; use packet counter to save back ptrs (reset
385          * to 0 as we leave), and comefrom to save source hook bitmask.
386          */
387         for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
388                 unsigned int pos = newinfo->hook_entry[hook];
389                 struct arpt_entry *e
390                         = (struct arpt_entry *)(entry0 + pos);
391
392                 if (!(valid_hooks & (1 << hook)))
393                         continue;
394
395                 /* Set initial back pointer. */
396                 e->counters.pcnt = pos;
397                 calldepth = 0;
398
399                 for (;;) {
400                         const struct xt_standard_target *t
401                                 = (void *)arpt_get_target_c(e);
402                         int visited = e->comefrom & (1 << hook);
403
404                         if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
405                                 pr_notice("arptables: loop hook %u pos %u %08X.\n",
406                                        hook, pos, e->comefrom);
407                                 return 0;
408                         }
409                         e->comefrom
410                                 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
411
412                         /* Unconditional return/END. */
413                         if ((e->target_offset == sizeof(struct arpt_entry) &&
414                              (strcmp(t->target.u.user.name,
415                                      XT_STANDARD_TARGET) == 0) &&
416                              t->verdict < 0 && unconditional(&e->arp)) ||
417                             visited) {
418                                 unsigned int oldpos, size;
419
420                                 if ((strcmp(t->target.u.user.name,
421                                             XT_STANDARD_TARGET) == 0) &&
422                                     t->verdict < -NF_MAX_VERDICT - 1) {
423                                         duprintf("mark_source_chains: bad "
424                                                 "negative verdict (%i)\n",
425                                                                 t->verdict);
426                                         return 0;
427                                 }
428
429                                 /* Return: backtrack through the last
430                                  * big jump.
431                                  */
432                                 do {
433                                         e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
434                                         oldpos = pos;
435                                         pos = e->counters.pcnt;
436                                         e->counters.pcnt = 0;
437
438                                         /* We're at the start. */
439                                         if (pos == oldpos)
440                                                 goto next;
441
442                                         e = (struct arpt_entry *)
443                                                 (entry0 + pos);
444                                 } while (oldpos == pos + e->next_offset);
445
446                                 /* Move along one */
447                                 size = e->next_offset;
448                                 e = (struct arpt_entry *)
449                                         (entry0 + pos + size);
450                                 e->counters.pcnt = pos;
451                                 pos += size;
452                                 if (calldepth > 0)
453                                         --calldepth;
454                         } else {
455                                 int newpos = t->verdict;
456
457                                 if (strcmp(t->target.u.user.name,
458                                            XT_STANDARD_TARGET) == 0 &&
459                                     newpos >= 0) {
460                                         if (newpos > newinfo->size -
461                                                 sizeof(struct arpt_entry)) {
462                                                 duprintf("mark_source_chains: "
463                                                         "bad verdict (%i)\n",
464                                                                 newpos);
465                                                 return 0;
466                                         }
467
468                                         if (entry0 + newpos != arpt_next_entry(e) &&
469                                             ++calldepth > max_calldepth)
470                                                 max_calldepth = calldepth;
471
472                                         /* This a jump; chase it. */
473                                         duprintf("Jump rule %u -> %u\n",
474                                                  pos, newpos);
475                                 } else {
476                                         /* ... this is a fallthru */
477                                         newpos = pos + e->next_offset;
478                                 }
479                                 e = (struct arpt_entry *)
480                                         (entry0 + newpos);
481                                 e->counters.pcnt = pos;
482                                 pos = newpos;
483                         }
484                 }
485                 next:
486                 duprintf("Finished chain %u\n", hook);
487         }
488         newinfo->stacksize = max_calldepth;
489         return 1;
490 }
491
492 static inline int check_entry(const struct arpt_entry *e, const char *name)
493 {
494         const struct xt_entry_target *t;
495
496         if (!arp_checkentry(&e->arp)) {
497                 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
498                 return -EINVAL;
499         }
500
501         if (e->target_offset + sizeof(struct xt_entry_target) > e->next_offset)
502                 return -EINVAL;
503
504         t = arpt_get_target_c(e);
505         if (e->target_offset + t->u.target_size > e->next_offset)
506                 return -EINVAL;
507
508         return 0;
509 }
510
511 static inline int check_target(struct arpt_entry *e, const char *name)
512 {
513         struct xt_entry_target *t = arpt_get_target(e);
514         int ret;
515         struct xt_tgchk_param par = {
516                 .table     = name,
517                 .entryinfo = e,
518                 .target    = t->u.kernel.target,
519                 .targinfo  = t->data,
520                 .hook_mask = e->comefrom,
521                 .family    = NFPROTO_ARP,
522         };
523
524         ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
525         if (ret < 0) {
526                 duprintf("arp_tables: check failed for `%s'.\n",
527                          t->u.kernel.target->name);
528                 return ret;
529         }
530         return 0;
531 }
532
533 static inline int
534 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size)
535 {
536         struct xt_entry_target *t;
537         struct xt_target *target;
538         int ret;
539
540         ret = check_entry(e, name);
541         if (ret)
542                 return ret;
543
544         e->counters.pcnt = xt_percpu_counter_alloc();
545         if (IS_ERR_VALUE(e->counters.pcnt))
546                 return -ENOMEM;
547
548         t = arpt_get_target(e);
549         target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
550                                         t->u.user.revision);
551         if (IS_ERR(target)) {
552                 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
553                 ret = PTR_ERR(target);
554                 goto out;
555         }
556         t->u.kernel.target = target;
557
558         ret = check_target(e, name);
559         if (ret)
560                 goto err;
561         return 0;
562 err:
563         module_put(t->u.kernel.target->me);
564 out:
565         xt_percpu_counter_free(e->counters.pcnt);
566
567         return ret;
568 }
569
570 static bool check_underflow(const struct arpt_entry *e)
571 {
572         const struct xt_entry_target *t;
573         unsigned int verdict;
574
575         if (!unconditional(&e->arp))
576                 return false;
577         t = arpt_get_target_c(e);
578         if (strcmp(t->u.user.name, XT_STANDARD_TARGET) != 0)
579                 return false;
580         verdict = ((struct xt_standard_target *)t)->verdict;
581         verdict = -verdict - 1;
582         return verdict == NF_DROP || verdict == NF_ACCEPT;
583 }
584
585 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
586                                              struct xt_table_info *newinfo,
587                                              const unsigned char *base,
588                                              const unsigned char *limit,
589                                              const unsigned int *hook_entries,
590                                              const unsigned int *underflows,
591                                              unsigned int valid_hooks)
592 {
593         unsigned int h;
594
595         if ((unsigned long)e % __alignof__(struct arpt_entry) != 0 ||
596             (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
597                 duprintf("Bad offset %p\n", e);
598                 return -EINVAL;
599         }
600
601         if (e->next_offset
602             < sizeof(struct arpt_entry) + sizeof(struct xt_entry_target)) {
603                 duprintf("checking: element %p size %u\n",
604                          e, e->next_offset);
605                 return -EINVAL;
606         }
607
608         /* Check hooks & underflows */
609         for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
610                 if (!(valid_hooks & (1 << h)))
611                         continue;
612                 if ((unsigned char *)e - base == hook_entries[h])
613                         newinfo->hook_entry[h] = hook_entries[h];
614                 if ((unsigned char *)e - base == underflows[h]) {
615                         if (!check_underflow(e)) {
616                                 pr_err("Underflows must be unconditional and "
617                                        "use the STANDARD target with "
618                                        "ACCEPT/DROP\n");
619                                 return -EINVAL;
620                         }
621                         newinfo->underflow[h] = underflows[h];
622                 }
623         }
624
625         /* Clear counters and comefrom */
626         e->counters = ((struct xt_counters) { 0, 0 });
627         e->comefrom = 0;
628         return 0;
629 }
630
631 static inline void cleanup_entry(struct arpt_entry *e)
632 {
633         struct xt_tgdtor_param par;
634         struct xt_entry_target *t;
635
636         t = arpt_get_target(e);
637         par.target   = t->u.kernel.target;
638         par.targinfo = t->data;
639         par.family   = NFPROTO_ARP;
640         if (par.target->destroy != NULL)
641                 par.target->destroy(&par);
642         module_put(par.target->me);
643         xt_percpu_counter_free(e->counters.pcnt);
644 }
645
646 /* Checks and translates the user-supplied table segment (held in
647  * newinfo).
648  */
649 static int translate_table(struct xt_table_info *newinfo, void *entry0,
650                            const struct arpt_replace *repl)
651 {
652         struct arpt_entry *iter;
653         unsigned int i;
654         int ret = 0;
655
656         newinfo->size = repl->size;
657         newinfo->number = repl->num_entries;
658
659         /* Init all hooks to impossible value. */
660         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
661                 newinfo->hook_entry[i] = 0xFFFFFFFF;
662                 newinfo->underflow[i] = 0xFFFFFFFF;
663         }
664
665         duprintf("translate_table: size %u\n", newinfo->size);
666         i = 0;
667
668         /* Walk through entries, checking offsets. */
669         xt_entry_foreach(iter, entry0, newinfo->size) {
670                 ret = check_entry_size_and_hooks(iter, newinfo, entry0,
671                                                  entry0 + repl->size,
672                                                  repl->hook_entry,
673                                                  repl->underflow,
674                                                  repl->valid_hooks);
675                 if (ret != 0)
676                         break;
677                 ++i;
678         }
679         duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
680         if (ret != 0)
681                 return ret;
682
683         if (i != repl->num_entries) {
684                 duprintf("translate_table: %u not %u entries\n",
685                          i, repl->num_entries);
686                 return -EINVAL;
687         }
688
689         /* Check hooks all assigned */
690         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
691                 /* Only hooks which are valid */
692                 if (!(repl->valid_hooks & (1 << i)))
693                         continue;
694                 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
695                         duprintf("Invalid hook entry %u %u\n",
696                                  i, repl->hook_entry[i]);
697                         return -EINVAL;
698                 }
699                 if (newinfo->underflow[i] == 0xFFFFFFFF) {
700                         duprintf("Invalid underflow %u %u\n",
701                                  i, repl->underflow[i]);
702                         return -EINVAL;
703                 }
704         }
705
706         if (!mark_source_chains(newinfo, repl->valid_hooks, entry0)) {
707                 duprintf("Looping hook\n");
708                 return -ELOOP;
709         }
710
711         /* Finally, each sanity check must pass */
712         i = 0;
713         xt_entry_foreach(iter, entry0, newinfo->size) {
714                 ret = find_check_entry(iter, repl->name, repl->size);
715                 if (ret != 0)
716                         break;
717                 ++i;
718         }
719
720         if (ret != 0) {
721                 xt_entry_foreach(iter, entry0, newinfo->size) {
722                         if (i-- == 0)
723                                 break;
724                         cleanup_entry(iter);
725                 }
726                 return ret;
727         }
728
729         return ret;
730 }
731
732 static void get_counters(const struct xt_table_info *t,
733                          struct xt_counters counters[])
734 {
735         struct arpt_entry *iter;
736         unsigned int cpu;
737         unsigned int i;
738
739         for_each_possible_cpu(cpu) {
740                 seqcount_t *s = &per_cpu(xt_recseq, cpu);
741
742                 i = 0;
743                 xt_entry_foreach(iter, t->entries, t->size) {
744                         struct xt_counters *tmp;
745                         u64 bcnt, pcnt;
746                         unsigned int start;
747
748                         tmp = xt_get_per_cpu_counter(&iter->counters, cpu);
749                         do {
750                                 start = read_seqcount_begin(s);
751                                 bcnt = tmp->bcnt;
752                                 pcnt = tmp->pcnt;
753                         } while (read_seqcount_retry(s, start));
754
755                         ADD_COUNTER(counters[i], bcnt, pcnt);
756                         ++i;
757                 }
758         }
759 }
760
761 static struct xt_counters *alloc_counters(const struct xt_table *table)
762 {
763         unsigned int countersize;
764         struct xt_counters *counters;
765         const struct xt_table_info *private = table->private;
766
767         /* We need atomic snapshot of counters: rest doesn't change
768          * (other than comefrom, which userspace doesn't care
769          * about).
770          */
771         countersize = sizeof(struct xt_counters) * private->number;
772         counters = vzalloc(countersize);
773
774         if (counters == NULL)
775                 return ERR_PTR(-ENOMEM);
776
777         get_counters(private, counters);
778
779         return counters;
780 }
781
782 static int copy_entries_to_user(unsigned int total_size,
783                                 const struct xt_table *table,
784                                 void __user *userptr)
785 {
786         unsigned int off, num;
787         const struct arpt_entry *e;
788         struct xt_counters *counters;
789         struct xt_table_info *private = table->private;
790         int ret = 0;
791         void *loc_cpu_entry;
792
793         counters = alloc_counters(table);
794         if (IS_ERR(counters))
795                 return PTR_ERR(counters);
796
797         loc_cpu_entry = private->entries;
798         /* ... then copy entire thing ... */
799         if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
800                 ret = -EFAULT;
801                 goto free_counters;
802         }
803
804         /* FIXME: use iterator macros --RR */
805         /* ... then go back and fix counters and names */
806         for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
807                 const struct xt_entry_target *t;
808
809                 e = (struct arpt_entry *)(loc_cpu_entry + off);
810                 if (copy_to_user(userptr + off
811                                  + offsetof(struct arpt_entry, counters),
812                                  &counters[num],
813                                  sizeof(counters[num])) != 0) {
814                         ret = -EFAULT;
815                         goto free_counters;
816                 }
817
818                 t = arpt_get_target_c(e);
819                 if (copy_to_user(userptr + off + e->target_offset
820                                  + offsetof(struct xt_entry_target,
821                                             u.user.name),
822                                  t->u.kernel.target->name,
823                                  strlen(t->u.kernel.target->name)+1) != 0) {
824                         ret = -EFAULT;
825                         goto free_counters;
826                 }
827         }
828
829  free_counters:
830         vfree(counters);
831         return ret;
832 }
833
834 #ifdef CONFIG_COMPAT
835 static void compat_standard_from_user(void *dst, const void *src)
836 {
837         int v = *(compat_int_t *)src;
838
839         if (v > 0)
840                 v += xt_compat_calc_jump(NFPROTO_ARP, v);
841         memcpy(dst, &v, sizeof(v));
842 }
843
844 static int compat_standard_to_user(void __user *dst, const void *src)
845 {
846         compat_int_t cv = *(int *)src;
847
848         if (cv > 0)
849                 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
850         return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
851 }
852
853 static int compat_calc_entry(const struct arpt_entry *e,
854                              const struct xt_table_info *info,
855                              const void *base, struct xt_table_info *newinfo)
856 {
857         const struct xt_entry_target *t;
858         unsigned int entry_offset;
859         int off, i, ret;
860
861         off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
862         entry_offset = (void *)e - base;
863
864         t = arpt_get_target_c(e);
865         off += xt_compat_target_offset(t->u.kernel.target);
866         newinfo->size -= off;
867         ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
868         if (ret)
869                 return ret;
870
871         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
872                 if (info->hook_entry[i] &&
873                     (e < (struct arpt_entry *)(base + info->hook_entry[i])))
874                         newinfo->hook_entry[i] -= off;
875                 if (info->underflow[i] &&
876                     (e < (struct arpt_entry *)(base + info->underflow[i])))
877                         newinfo->underflow[i] -= off;
878         }
879         return 0;
880 }
881
882 static int compat_table_info(const struct xt_table_info *info,
883                              struct xt_table_info *newinfo)
884 {
885         struct arpt_entry *iter;
886         const void *loc_cpu_entry;
887         int ret;
888
889         if (!newinfo || !info)
890                 return -EINVAL;
891
892         /* we dont care about newinfo->entries */
893         memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
894         newinfo->initial_entries = 0;
895         loc_cpu_entry = info->entries;
896         xt_compat_init_offsets(NFPROTO_ARP, info->number);
897         xt_entry_foreach(iter, loc_cpu_entry, info->size) {
898                 ret = compat_calc_entry(iter, info, loc_cpu_entry, newinfo);
899                 if (ret != 0)
900                         return ret;
901         }
902         return 0;
903 }
904 #endif
905
906 static int get_info(struct net *net, void __user *user,
907                     const int *len, int compat)
908 {
909         char name[XT_TABLE_MAXNAMELEN];
910         struct xt_table *t;
911         int ret;
912
913         if (*len != sizeof(struct arpt_getinfo)) {
914                 duprintf("length %u != %Zu\n", *len,
915                          sizeof(struct arpt_getinfo));
916                 return -EINVAL;
917         }
918
919         if (copy_from_user(name, user, sizeof(name)) != 0)
920                 return -EFAULT;
921
922         name[XT_TABLE_MAXNAMELEN-1] = '\0';
923 #ifdef CONFIG_COMPAT
924         if (compat)
925                 xt_compat_lock(NFPROTO_ARP);
926 #endif
927         t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
928                                     "arptable_%s", name);
929         if (!IS_ERR_OR_NULL(t)) {
930                 struct arpt_getinfo info;
931                 const struct xt_table_info *private = t->private;
932 #ifdef CONFIG_COMPAT
933                 struct xt_table_info tmp;
934
935                 if (compat) {
936                         ret = compat_table_info(private, &tmp);
937                         xt_compat_flush_offsets(NFPROTO_ARP);
938                         private = &tmp;
939                 }
940 #endif
941                 memset(&info, 0, sizeof(info));
942                 info.valid_hooks = t->valid_hooks;
943                 memcpy(info.hook_entry, private->hook_entry,
944                        sizeof(info.hook_entry));
945                 memcpy(info.underflow, private->underflow,
946                        sizeof(info.underflow));
947                 info.num_entries = private->number;
948                 info.size = private->size;
949                 strcpy(info.name, name);
950
951                 if (copy_to_user(user, &info, *len) != 0)
952                         ret = -EFAULT;
953                 else
954                         ret = 0;
955                 xt_table_unlock(t);
956                 module_put(t->me);
957         } else
958                 ret = t ? PTR_ERR(t) : -ENOENT;
959 #ifdef CONFIG_COMPAT
960         if (compat)
961                 xt_compat_unlock(NFPROTO_ARP);
962 #endif
963         return ret;
964 }
965
966 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
967                        const int *len)
968 {
969         int ret;
970         struct arpt_get_entries get;
971         struct xt_table *t;
972
973         if (*len < sizeof(get)) {
974                 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
975                 return -EINVAL;
976         }
977         if (copy_from_user(&get, uptr, sizeof(get)) != 0)
978                 return -EFAULT;
979         if (*len != sizeof(struct arpt_get_entries) + get.size) {
980                 duprintf("get_entries: %u != %Zu\n", *len,
981                          sizeof(struct arpt_get_entries) + get.size);
982                 return -EINVAL;
983         }
984
985         t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
986         if (!IS_ERR_OR_NULL(t)) {
987                 const struct xt_table_info *private = t->private;
988
989                 duprintf("t->private->number = %u\n",
990                          private->number);
991                 if (get.size == private->size)
992                         ret = copy_entries_to_user(private->size,
993                                                    t, uptr->entrytable);
994                 else {
995                         duprintf("get_entries: I've got %u not %u!\n",
996                                  private->size, get.size);
997                         ret = -EAGAIN;
998                 }
999                 module_put(t->me);
1000                 xt_table_unlock(t);
1001         } else
1002                 ret = t ? PTR_ERR(t) : -ENOENT;
1003
1004         return ret;
1005 }
1006
1007 static int __do_replace(struct net *net, const char *name,
1008                         unsigned int valid_hooks,
1009                         struct xt_table_info *newinfo,
1010                         unsigned int num_counters,
1011                         void __user *counters_ptr)
1012 {
1013         int ret;
1014         struct xt_table *t;
1015         struct xt_table_info *oldinfo;
1016         struct xt_counters *counters;
1017         void *loc_cpu_old_entry;
1018         struct arpt_entry *iter;
1019
1020         ret = 0;
1021         counters = vzalloc(num_counters * sizeof(struct xt_counters));
1022         if (!counters) {
1023                 ret = -ENOMEM;
1024                 goto out;
1025         }
1026
1027         t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
1028                                     "arptable_%s", name);
1029         if (IS_ERR_OR_NULL(t)) {
1030                 ret = t ? PTR_ERR(t) : -ENOENT;
1031                 goto free_newinfo_counters_untrans;
1032         }
1033
1034         /* You lied! */
1035         if (valid_hooks != t->valid_hooks) {
1036                 duprintf("Valid hook crap: %08X vs %08X\n",
1037                          valid_hooks, t->valid_hooks);
1038                 ret = -EINVAL;
1039                 goto put_module;
1040         }
1041
1042         oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1043         if (!oldinfo)
1044                 goto put_module;
1045
1046         /* Update module usage count based on number of rules */
1047         duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1048                 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1049         if ((oldinfo->number > oldinfo->initial_entries) ||
1050             (newinfo->number <= oldinfo->initial_entries))
1051                 module_put(t->me);
1052         if ((oldinfo->number > oldinfo->initial_entries) &&
1053             (newinfo->number <= oldinfo->initial_entries))
1054                 module_put(t->me);
1055
1056         /* Get the old counters, and synchronize with replace */
1057         get_counters(oldinfo, counters);
1058
1059         /* Decrease module usage counts and free resource */
1060         loc_cpu_old_entry = oldinfo->entries;
1061         xt_entry_foreach(iter, loc_cpu_old_entry, oldinfo->size)
1062                 cleanup_entry(iter);
1063
1064         xt_free_table_info(oldinfo);
1065         if (copy_to_user(counters_ptr, counters,
1066                          sizeof(struct xt_counters) * num_counters) != 0) {
1067                 /* Silent error, can't fail, new table is already in place */
1068                 net_warn_ratelimited("arptables: counters copy to user failed while replacing table\n");
1069         }
1070         vfree(counters);
1071         xt_table_unlock(t);
1072         return ret;
1073
1074  put_module:
1075         module_put(t->me);
1076         xt_table_unlock(t);
1077  free_newinfo_counters_untrans:
1078         vfree(counters);
1079  out:
1080         return ret;
1081 }
1082
1083 static int do_replace(struct net *net, const void __user *user,
1084                       unsigned int len)
1085 {
1086         int ret;
1087         struct arpt_replace tmp;
1088         struct xt_table_info *newinfo;
1089         void *loc_cpu_entry;
1090         struct arpt_entry *iter;
1091
1092         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1093                 return -EFAULT;
1094
1095         /* overflow check */
1096         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1097                 return -ENOMEM;
1098         if (tmp.num_counters == 0)
1099                 return -EINVAL;
1100
1101         tmp.name[sizeof(tmp.name)-1] = 0;
1102
1103         newinfo = xt_alloc_table_info(tmp.size);
1104         if (!newinfo)
1105                 return -ENOMEM;
1106
1107         loc_cpu_entry = newinfo->entries;
1108         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1109                            tmp.size) != 0) {
1110                 ret = -EFAULT;
1111                 goto free_newinfo;
1112         }
1113
1114         ret = translate_table(newinfo, loc_cpu_entry, &tmp);
1115         if (ret != 0)
1116                 goto free_newinfo;
1117
1118         duprintf("arp_tables: Translated table\n");
1119
1120         ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1121                            tmp.num_counters, tmp.counters);
1122         if (ret)
1123                 goto free_newinfo_untrans;
1124         return 0;
1125
1126  free_newinfo_untrans:
1127         xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1128                 cleanup_entry(iter);
1129  free_newinfo:
1130         xt_free_table_info(newinfo);
1131         return ret;
1132 }
1133
1134 static int do_add_counters(struct net *net, const void __user *user,
1135                            unsigned int len, int compat)
1136 {
1137         unsigned int i;
1138         struct xt_counters_info tmp;
1139         struct xt_counters *paddc;
1140         unsigned int num_counters;
1141         const char *name;
1142         int size;
1143         void *ptmp;
1144         struct xt_table *t;
1145         const struct xt_table_info *private;
1146         int ret = 0;
1147         struct arpt_entry *iter;
1148         unsigned int addend;
1149 #ifdef CONFIG_COMPAT
1150         struct compat_xt_counters_info compat_tmp;
1151
1152         if (compat) {
1153                 ptmp = &compat_tmp;
1154                 size = sizeof(struct compat_xt_counters_info);
1155         } else
1156 #endif
1157         {
1158                 ptmp = &tmp;
1159                 size = sizeof(struct xt_counters_info);
1160         }
1161
1162         if (copy_from_user(ptmp, user, size) != 0)
1163                 return -EFAULT;
1164
1165 #ifdef CONFIG_COMPAT
1166         if (compat) {
1167                 num_counters = compat_tmp.num_counters;
1168                 name = compat_tmp.name;
1169         } else
1170 #endif
1171         {
1172                 num_counters = tmp.num_counters;
1173                 name = tmp.name;
1174         }
1175
1176         if (len != size + num_counters * sizeof(struct xt_counters))
1177                 return -EINVAL;
1178
1179         paddc = vmalloc(len - size);
1180         if (!paddc)
1181                 return -ENOMEM;
1182
1183         if (copy_from_user(paddc, user + size, len - size) != 0) {
1184                 ret = -EFAULT;
1185                 goto free;
1186         }
1187
1188         t = xt_find_table_lock(net, NFPROTO_ARP, name);
1189         if (IS_ERR_OR_NULL(t)) {
1190                 ret = t ? PTR_ERR(t) : -ENOENT;
1191                 goto free;
1192         }
1193
1194         local_bh_disable();
1195         private = t->private;
1196         if (private->number != num_counters) {
1197                 ret = -EINVAL;
1198                 goto unlock_up_free;
1199         }
1200
1201         i = 0;
1202
1203         addend = xt_write_recseq_begin();
1204         xt_entry_foreach(iter,  private->entries, private->size) {
1205                 struct xt_counters *tmp;
1206
1207                 tmp = xt_get_this_cpu_counter(&iter->counters);
1208                 ADD_COUNTER(*tmp, paddc[i].bcnt, paddc[i].pcnt);
1209                 ++i;
1210         }
1211         xt_write_recseq_end(addend);
1212  unlock_up_free:
1213         local_bh_enable();
1214         xt_table_unlock(t);
1215         module_put(t->me);
1216  free:
1217         vfree(paddc);
1218
1219         return ret;
1220 }
1221
1222 #ifdef CONFIG_COMPAT
1223 static inline void compat_release_entry(struct compat_arpt_entry *e)
1224 {
1225         struct xt_entry_target *t;
1226
1227         t = compat_arpt_get_target(e);
1228         module_put(t->u.kernel.target->me);
1229 }
1230
1231 static inline int
1232 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1233                                   struct xt_table_info *newinfo,
1234                                   unsigned int *size,
1235                                   const unsigned char *base,
1236                                   const unsigned char *limit,
1237                                   const unsigned int *hook_entries,
1238                                   const unsigned int *underflows,
1239                                   const char *name)
1240 {
1241         struct xt_entry_target *t;
1242         struct xt_target *target;
1243         unsigned int entry_offset;
1244         int ret, off, h;
1245
1246         duprintf("check_compat_entry_size_and_hooks %p\n", e);
1247         if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0 ||
1248             (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
1249                 duprintf("Bad offset %p, limit = %p\n", e, limit);
1250                 return -EINVAL;
1251         }
1252
1253         if (e->next_offset < sizeof(struct compat_arpt_entry) +
1254                              sizeof(struct compat_xt_entry_target)) {
1255                 duprintf("checking: element %p size %u\n",
1256                          e, e->next_offset);
1257                 return -EINVAL;
1258         }
1259
1260         /* For purposes of check_entry casting the compat entry is fine */
1261         ret = check_entry((struct arpt_entry *)e, name);
1262         if (ret)
1263                 return ret;
1264
1265         off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1266         entry_offset = (void *)e - (void *)base;
1267
1268         t = compat_arpt_get_target(e);
1269         target = xt_request_find_target(NFPROTO_ARP, t->u.user.name,
1270                                         t->u.user.revision);
1271         if (IS_ERR(target)) {
1272                 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1273                          t->u.user.name);
1274                 ret = PTR_ERR(target);
1275                 goto out;
1276         }
1277         t->u.kernel.target = target;
1278
1279         off += xt_compat_target_offset(target);
1280         *size += off;
1281         ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1282         if (ret)
1283                 goto release_target;
1284
1285         /* Check hooks & underflows */
1286         for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1287                 if ((unsigned char *)e - base == hook_entries[h])
1288                         newinfo->hook_entry[h] = hook_entries[h];
1289                 if ((unsigned char *)e - base == underflows[h])
1290                         newinfo->underflow[h] = underflows[h];
1291         }
1292
1293         /* Clear counters and comefrom */
1294         memset(&e->counters, 0, sizeof(e->counters));
1295         e->comefrom = 0;
1296         return 0;
1297
1298 release_target:
1299         module_put(t->u.kernel.target->me);
1300 out:
1301         return ret;
1302 }
1303
1304 static int
1305 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1306                             unsigned int *size, const char *name,
1307                             struct xt_table_info *newinfo, unsigned char *base)
1308 {
1309         struct xt_entry_target *t;
1310         struct xt_target *target;
1311         struct arpt_entry *de;
1312         unsigned int origsize;
1313         int ret, h;
1314
1315         ret = 0;
1316         origsize = *size;
1317         de = (struct arpt_entry *)*dstptr;
1318         memcpy(de, e, sizeof(struct arpt_entry));
1319         memcpy(&de->counters, &e->counters, sizeof(e->counters));
1320
1321         *dstptr += sizeof(struct arpt_entry);
1322         *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1323
1324         de->target_offset = e->target_offset - (origsize - *size);
1325         t = compat_arpt_get_target(e);
1326         target = t->u.kernel.target;
1327         xt_compat_target_from_user(t, dstptr, size);
1328
1329         de->next_offset = e->next_offset - (origsize - *size);
1330         for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1331                 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1332                         newinfo->hook_entry[h] -= origsize - *size;
1333                 if ((unsigned char *)de - base < newinfo->underflow[h])
1334                         newinfo->underflow[h] -= origsize - *size;
1335         }
1336         return ret;
1337 }
1338
1339 static int translate_compat_table(const char *name,
1340                                   unsigned int valid_hooks,
1341                                   struct xt_table_info **pinfo,
1342                                   void **pentry0,
1343                                   unsigned int total_size,
1344                                   unsigned int number,
1345                                   unsigned int *hook_entries,
1346                                   unsigned int *underflows)
1347 {
1348         unsigned int i, j;
1349         struct xt_table_info *newinfo, *info;
1350         void *pos, *entry0, *entry1;
1351         struct compat_arpt_entry *iter0;
1352         struct arpt_entry *iter1;
1353         unsigned int size;
1354         int ret = 0;
1355
1356         info = *pinfo;
1357         entry0 = *pentry0;
1358         size = total_size;
1359         info->number = number;
1360
1361         /* Init all hooks to impossible value. */
1362         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1363                 info->hook_entry[i] = 0xFFFFFFFF;
1364                 info->underflow[i] = 0xFFFFFFFF;
1365         }
1366
1367         duprintf("translate_compat_table: size %u\n", info->size);
1368         j = 0;
1369         xt_compat_lock(NFPROTO_ARP);
1370         xt_compat_init_offsets(NFPROTO_ARP, number);
1371         /* Walk through entries, checking offsets. */
1372         xt_entry_foreach(iter0, entry0, total_size) {
1373                 ret = check_compat_entry_size_and_hooks(iter0, info, &size,
1374                                                         entry0,
1375                                                         entry0 + total_size,
1376                                                         hook_entries,
1377                                                         underflows,
1378                                                         name);
1379                 if (ret != 0)
1380                         goto out_unlock;
1381                 ++j;
1382         }
1383
1384         ret = -EINVAL;
1385         if (j != number) {
1386                 duprintf("translate_compat_table: %u not %u entries\n",
1387                          j, number);
1388                 goto out_unlock;
1389         }
1390
1391         /* Check hooks all assigned */
1392         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1393                 /* Only hooks which are valid */
1394                 if (!(valid_hooks & (1 << i)))
1395                         continue;
1396                 if (info->hook_entry[i] == 0xFFFFFFFF) {
1397                         duprintf("Invalid hook entry %u %u\n",
1398                                  i, hook_entries[i]);
1399                         goto out_unlock;
1400                 }
1401                 if (info->underflow[i] == 0xFFFFFFFF) {
1402                         duprintf("Invalid underflow %u %u\n",
1403                                  i, underflows[i]);
1404                         goto out_unlock;
1405                 }
1406         }
1407
1408         ret = -ENOMEM;
1409         newinfo = xt_alloc_table_info(size);
1410         if (!newinfo)
1411                 goto out_unlock;
1412
1413         newinfo->number = number;
1414         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1415                 newinfo->hook_entry[i] = info->hook_entry[i];
1416                 newinfo->underflow[i] = info->underflow[i];
1417         }
1418         entry1 = newinfo->entries;
1419         pos = entry1;
1420         size = total_size;
1421         xt_entry_foreach(iter0, entry0, total_size) {
1422                 ret = compat_copy_entry_from_user(iter0, &pos, &size,
1423                                                   name, newinfo, entry1);
1424                 if (ret != 0)
1425                         break;
1426         }
1427         xt_compat_flush_offsets(NFPROTO_ARP);
1428         xt_compat_unlock(NFPROTO_ARP);
1429         if (ret)
1430                 goto free_newinfo;
1431
1432         ret = -ELOOP;
1433         if (!mark_source_chains(newinfo, valid_hooks, entry1))
1434                 goto free_newinfo;
1435
1436         i = 0;
1437         xt_entry_foreach(iter1, entry1, newinfo->size) {
1438                 iter1->counters.pcnt = xt_percpu_counter_alloc();
1439                 if (IS_ERR_VALUE(iter1->counters.pcnt)) {
1440                         ret = -ENOMEM;
1441                         break;
1442                 }
1443
1444                 ret = check_target(iter1, name);
1445                 if (ret != 0) {
1446                         xt_percpu_counter_free(iter1->counters.pcnt);
1447                         break;
1448                 }
1449                 ++i;
1450         }
1451         if (ret) {
1452                 /*
1453                  * The first i matches need cleanup_entry (calls ->destroy)
1454                  * because they had called ->check already. The other j-i
1455                  * entries need only release.
1456                  */
1457                 int skip = i;
1458                 j -= i;
1459                 xt_entry_foreach(iter0, entry0, newinfo->size) {
1460                         if (skip-- > 0)
1461                                 continue;
1462                         if (j-- == 0)
1463                                 break;
1464                         compat_release_entry(iter0);
1465                 }
1466                 xt_entry_foreach(iter1, entry1, newinfo->size) {
1467                         if (i-- == 0)
1468                                 break;
1469                         cleanup_entry(iter1);
1470                 }
1471                 xt_free_table_info(newinfo);
1472                 return ret;
1473         }
1474
1475         *pinfo = newinfo;
1476         *pentry0 = entry1;
1477         xt_free_table_info(info);
1478         return 0;
1479
1480 free_newinfo:
1481         xt_free_table_info(newinfo);
1482 out:
1483         xt_entry_foreach(iter0, entry0, total_size) {
1484                 if (j-- == 0)
1485                         break;
1486                 compat_release_entry(iter0);
1487         }
1488         return ret;
1489 out_unlock:
1490         xt_compat_flush_offsets(NFPROTO_ARP);
1491         xt_compat_unlock(NFPROTO_ARP);
1492         goto out;
1493 }
1494
1495 struct compat_arpt_replace {
1496         char                            name[XT_TABLE_MAXNAMELEN];
1497         u32                             valid_hooks;
1498         u32                             num_entries;
1499         u32                             size;
1500         u32                             hook_entry[NF_ARP_NUMHOOKS];
1501         u32                             underflow[NF_ARP_NUMHOOKS];
1502         u32                             num_counters;
1503         compat_uptr_t                   counters;
1504         struct compat_arpt_entry        entries[0];
1505 };
1506
1507 static int compat_do_replace(struct net *net, void __user *user,
1508                              unsigned int len)
1509 {
1510         int ret;
1511         struct compat_arpt_replace tmp;
1512         struct xt_table_info *newinfo;
1513         void *loc_cpu_entry;
1514         struct arpt_entry *iter;
1515
1516         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1517                 return -EFAULT;
1518
1519         /* overflow check */
1520         if (tmp.size >= INT_MAX / num_possible_cpus())
1521                 return -ENOMEM;
1522         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1523                 return -ENOMEM;
1524         if (tmp.num_counters == 0)
1525                 return -EINVAL;
1526
1527         tmp.name[sizeof(tmp.name)-1] = 0;
1528
1529         newinfo = xt_alloc_table_info(tmp.size);
1530         if (!newinfo)
1531                 return -ENOMEM;
1532
1533         loc_cpu_entry = newinfo->entries;
1534         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1535                 ret = -EFAULT;
1536                 goto free_newinfo;
1537         }
1538
1539         ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1540                                      &newinfo, &loc_cpu_entry, tmp.size,
1541                                      tmp.num_entries, tmp.hook_entry,
1542                                      tmp.underflow);
1543         if (ret != 0)
1544                 goto free_newinfo;
1545
1546         duprintf("compat_do_replace: Translated table\n");
1547
1548         ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1549                            tmp.num_counters, compat_ptr(tmp.counters));
1550         if (ret)
1551                 goto free_newinfo_untrans;
1552         return 0;
1553
1554  free_newinfo_untrans:
1555         xt_entry_foreach(iter, loc_cpu_entry, newinfo->size)
1556                 cleanup_entry(iter);
1557  free_newinfo:
1558         xt_free_table_info(newinfo);
1559         return ret;
1560 }
1561
1562 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1563                                   unsigned int len)
1564 {
1565         int ret;
1566
1567         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1568                 return -EPERM;
1569
1570         switch (cmd) {
1571         case ARPT_SO_SET_REPLACE:
1572                 ret = compat_do_replace(sock_net(sk), user, len);
1573                 break;
1574
1575         case ARPT_SO_SET_ADD_COUNTERS:
1576                 ret = do_add_counters(sock_net(sk), user, len, 1);
1577                 break;
1578
1579         default:
1580                 duprintf("do_arpt_set_ctl:  unknown request %i\n", cmd);
1581                 ret = -EINVAL;
1582         }
1583
1584         return ret;
1585 }
1586
1587 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1588                                      compat_uint_t *size,
1589                                      struct xt_counters *counters,
1590                                      unsigned int i)
1591 {
1592         struct xt_entry_target *t;
1593         struct compat_arpt_entry __user *ce;
1594         u_int16_t target_offset, next_offset;
1595         compat_uint_t origsize;
1596         int ret;
1597
1598         origsize = *size;
1599         ce = (struct compat_arpt_entry __user *)*dstptr;
1600         if (copy_to_user(ce, e, sizeof(struct arpt_entry)) != 0 ||
1601             copy_to_user(&ce->counters, &counters[i],
1602             sizeof(counters[i])) != 0)
1603                 return -EFAULT;
1604
1605         *dstptr += sizeof(struct compat_arpt_entry);
1606         *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1607
1608         target_offset = e->target_offset - (origsize - *size);
1609
1610         t = arpt_get_target(e);
1611         ret = xt_compat_target_to_user(t, dstptr, size);
1612         if (ret)
1613                 return ret;
1614         next_offset = e->next_offset - (origsize - *size);
1615         if (put_user(target_offset, &ce->target_offset) != 0 ||
1616             put_user(next_offset, &ce->next_offset) != 0)
1617                 return -EFAULT;
1618         return 0;
1619 }
1620
1621 static int compat_copy_entries_to_user(unsigned int total_size,
1622                                        struct xt_table *table,
1623                                        void __user *userptr)
1624 {
1625         struct xt_counters *counters;
1626         const struct xt_table_info *private = table->private;
1627         void __user *pos;
1628         unsigned int size;
1629         int ret = 0;
1630         unsigned int i = 0;
1631         struct arpt_entry *iter;
1632
1633         counters = alloc_counters(table);
1634         if (IS_ERR(counters))
1635                 return PTR_ERR(counters);
1636
1637         pos = userptr;
1638         size = total_size;
1639         xt_entry_foreach(iter, private->entries, total_size) {
1640                 ret = compat_copy_entry_to_user(iter, &pos,
1641                                                 &size, counters, i++);
1642                 if (ret != 0)
1643                         break;
1644         }
1645         vfree(counters);
1646         return ret;
1647 }
1648
1649 struct compat_arpt_get_entries {
1650         char name[XT_TABLE_MAXNAMELEN];
1651         compat_uint_t size;
1652         struct compat_arpt_entry entrytable[0];
1653 };
1654
1655 static int compat_get_entries(struct net *net,
1656                               struct compat_arpt_get_entries __user *uptr,
1657                               int *len)
1658 {
1659         int ret;
1660         struct compat_arpt_get_entries get;
1661         struct xt_table *t;
1662
1663         if (*len < sizeof(get)) {
1664                 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1665                 return -EINVAL;
1666         }
1667         if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1668                 return -EFAULT;
1669         if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1670                 duprintf("compat_get_entries: %u != %zu\n",
1671                          *len, sizeof(get) + get.size);
1672                 return -EINVAL;
1673         }
1674
1675         xt_compat_lock(NFPROTO_ARP);
1676         t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1677         if (!IS_ERR_OR_NULL(t)) {
1678                 const struct xt_table_info *private = t->private;
1679                 struct xt_table_info info;
1680
1681                 duprintf("t->private->number = %u\n", private->number);
1682                 ret = compat_table_info(private, &info);
1683                 if (!ret && get.size == info.size) {
1684                         ret = compat_copy_entries_to_user(private->size,
1685                                                           t, uptr->entrytable);
1686                 } else if (!ret) {
1687                         duprintf("compat_get_entries: I've got %u not %u!\n",
1688                                  private->size, get.size);
1689                         ret = -EAGAIN;
1690                 }
1691                 xt_compat_flush_offsets(NFPROTO_ARP);
1692                 module_put(t->me);
1693                 xt_table_unlock(t);
1694         } else
1695                 ret = t ? PTR_ERR(t) : -ENOENT;
1696
1697         xt_compat_unlock(NFPROTO_ARP);
1698         return ret;
1699 }
1700
1701 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1702
1703 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1704                                   int *len)
1705 {
1706         int ret;
1707
1708         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1709                 return -EPERM;
1710
1711         switch (cmd) {
1712         case ARPT_SO_GET_INFO:
1713                 ret = get_info(sock_net(sk), user, len, 1);
1714                 break;
1715         case ARPT_SO_GET_ENTRIES:
1716                 ret = compat_get_entries(sock_net(sk), user, len);
1717                 break;
1718         default:
1719                 ret = do_arpt_get_ctl(sk, cmd, user, len);
1720         }
1721         return ret;
1722 }
1723 #endif
1724
1725 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1726 {
1727         int ret;
1728
1729         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1730                 return -EPERM;
1731
1732         switch (cmd) {
1733         case ARPT_SO_SET_REPLACE:
1734                 ret = do_replace(sock_net(sk), user, len);
1735                 break;
1736
1737         case ARPT_SO_SET_ADD_COUNTERS:
1738                 ret = do_add_counters(sock_net(sk), user, len, 0);
1739                 break;
1740
1741         default:
1742                 duprintf("do_arpt_set_ctl:  unknown request %i\n", cmd);
1743                 ret = -EINVAL;
1744         }
1745
1746         return ret;
1747 }
1748
1749 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1750 {
1751         int ret;
1752
1753         if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1754                 return -EPERM;
1755
1756         switch (cmd) {
1757         case ARPT_SO_GET_INFO:
1758                 ret = get_info(sock_net(sk), user, len, 0);
1759                 break;
1760
1761         case ARPT_SO_GET_ENTRIES:
1762                 ret = get_entries(sock_net(sk), user, len);
1763                 break;
1764
1765         case ARPT_SO_GET_REVISION_TARGET: {
1766                 struct xt_get_revision rev;
1767
1768                 if (*len != sizeof(rev)) {
1769                         ret = -EINVAL;
1770                         break;
1771                 }
1772                 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1773                         ret = -EFAULT;
1774                         break;
1775                 }
1776                 rev.name[sizeof(rev.name)-1] = 0;
1777
1778                 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1779                                                          rev.revision, 1, &ret),
1780                                         "arpt_%s", rev.name);
1781                 break;
1782         }
1783
1784         default:
1785                 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1786                 ret = -EINVAL;
1787         }
1788
1789         return ret;
1790 }
1791
1792 struct xt_table *arpt_register_table(struct net *net,
1793                                      const struct xt_table *table,
1794                                      const struct arpt_replace *repl)
1795 {
1796         int ret;
1797         struct xt_table_info *newinfo;
1798         struct xt_table_info bootstrap = {0};
1799         void *loc_cpu_entry;
1800         struct xt_table *new_table;
1801
1802         newinfo = xt_alloc_table_info(repl->size);
1803         if (!newinfo) {
1804                 ret = -ENOMEM;
1805                 goto out;
1806         }
1807
1808         loc_cpu_entry = newinfo->entries;
1809         memcpy(loc_cpu_entry, repl->entries, repl->size);
1810
1811         ret = translate_table(newinfo, loc_cpu_entry, repl);
1812         duprintf("arpt_register_table: translate table gives %d\n", ret);
1813         if (ret != 0)
1814                 goto out_free;
1815
1816         new_table = xt_register_table(net, table, &bootstrap, newinfo);
1817         if (IS_ERR(new_table)) {
1818                 ret = PTR_ERR(new_table);
1819                 goto out_free;
1820         }
1821         return new_table;
1822
1823 out_free:
1824         xt_free_table_info(newinfo);
1825 out:
1826         return ERR_PTR(ret);
1827 }
1828
1829 void arpt_unregister_table(struct xt_table *table)
1830 {
1831         struct xt_table_info *private;
1832         void *loc_cpu_entry;
1833         struct module *table_owner = table->me;
1834         struct arpt_entry *iter;
1835
1836         private = xt_unregister_table(table);
1837
1838         /* Decrease module usage counts and free resources */
1839         loc_cpu_entry = private->entries;
1840         xt_entry_foreach(iter, loc_cpu_entry, private->size)
1841                 cleanup_entry(iter);
1842         if (private->number > private->initial_entries)
1843                 module_put(table_owner);
1844         xt_free_table_info(private);
1845 }
1846
1847 /* The built-in targets: standard (NULL) and error. */
1848 static struct xt_target arpt_builtin_tg[] __read_mostly = {
1849         {
1850                 .name             = XT_STANDARD_TARGET,
1851                 .targetsize       = sizeof(int),
1852                 .family           = NFPROTO_ARP,
1853 #ifdef CONFIG_COMPAT
1854                 .compatsize       = sizeof(compat_int_t),
1855                 .compat_from_user = compat_standard_from_user,
1856                 .compat_to_user   = compat_standard_to_user,
1857 #endif
1858         },
1859         {
1860                 .name             = XT_ERROR_TARGET,
1861                 .target           = arpt_error,
1862                 .targetsize       = XT_FUNCTION_MAXNAMELEN,
1863                 .family           = NFPROTO_ARP,
1864         },
1865 };
1866
1867 static struct nf_sockopt_ops arpt_sockopts = {
1868         .pf             = PF_INET,
1869         .set_optmin     = ARPT_BASE_CTL,
1870         .set_optmax     = ARPT_SO_SET_MAX+1,
1871         .set            = do_arpt_set_ctl,
1872 #ifdef CONFIG_COMPAT
1873         .compat_set     = compat_do_arpt_set_ctl,
1874 #endif
1875         .get_optmin     = ARPT_BASE_CTL,
1876         .get_optmax     = ARPT_SO_GET_MAX+1,
1877         .get            = do_arpt_get_ctl,
1878 #ifdef CONFIG_COMPAT
1879         .compat_get     = compat_do_arpt_get_ctl,
1880 #endif
1881         .owner          = THIS_MODULE,
1882 };
1883
1884 static int __net_init arp_tables_net_init(struct net *net)
1885 {
1886         return xt_proto_init(net, NFPROTO_ARP);
1887 }
1888
1889 static void __net_exit arp_tables_net_exit(struct net *net)
1890 {
1891         xt_proto_fini(net, NFPROTO_ARP);
1892 }
1893
1894 static struct pernet_operations arp_tables_net_ops = {
1895         .init = arp_tables_net_init,
1896         .exit = arp_tables_net_exit,
1897 };
1898
1899 static int __init arp_tables_init(void)
1900 {
1901         int ret;
1902
1903         ret = register_pernet_subsys(&arp_tables_net_ops);
1904         if (ret < 0)
1905                 goto err1;
1906
1907         /* No one else will be downing sem now, so we won't sleep */
1908         ret = xt_register_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1909         if (ret < 0)
1910                 goto err2;
1911
1912         /* Register setsockopt */
1913         ret = nf_register_sockopt(&arpt_sockopts);
1914         if (ret < 0)
1915                 goto err4;
1916
1917         printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1918         return 0;
1919
1920 err4:
1921         xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1922 err2:
1923         unregister_pernet_subsys(&arp_tables_net_ops);
1924 err1:
1925         return ret;
1926 }
1927
1928 static void __exit arp_tables_fini(void)
1929 {
1930         nf_unregister_sockopt(&arpt_sockopts);
1931         xt_unregister_targets(arpt_builtin_tg, ARRAY_SIZE(arpt_builtin_tg));
1932         unregister_pernet_subsys(&arp_tables_net_ops);
1933 }
1934
1935 EXPORT_SYMBOL(arpt_register_table);
1936 EXPORT_SYMBOL(arpt_unregister_table);
1937 EXPORT_SYMBOL(arpt_do_table);
1938
1939 module_init(arp_tables_init);
1940 module_exit(arp_tables_fini);