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