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