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