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