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