netfilter: xtables: move extension arguments into compound structure (4/6)
[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 /* Returns whether packet matches rule or not. */
77 static inline int arp_packet_match(const struct arphdr *arphdr,
78                                    struct net_device *dev,
79                                    const char *indev,
80                                    const char *outdev,
81                                    const struct arpt_arp *arpinfo)
82 {
83         const char *arpptr = (char *)(arphdr + 1);
84         const char *src_devaddr, *tgt_devaddr;
85         __be32 src_ipaddr, tgt_ipaddr;
86         int i, ret;
87
88 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
89
90         if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
91                   ARPT_INV_ARPOP)) {
92                 dprintf("ARP operation field mismatch.\n");
93                 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
94                         arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
95                 return 0;
96         }
97
98         if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
99                   ARPT_INV_ARPHRD)) {
100                 dprintf("ARP hardware address format mismatch.\n");
101                 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
102                         arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
103                 return 0;
104         }
105
106         if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
107                   ARPT_INV_ARPPRO)) {
108                 dprintf("ARP protocol address format mismatch.\n");
109                 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
110                         arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
111                 return 0;
112         }
113
114         if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
115                   ARPT_INV_ARPHLN)) {
116                 dprintf("ARP hardware address length mismatch.\n");
117                 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
118                         arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
119                 return 0;
120         }
121
122         src_devaddr = arpptr;
123         arpptr += dev->addr_len;
124         memcpy(&src_ipaddr, arpptr, sizeof(u32));
125         arpptr += sizeof(u32);
126         tgt_devaddr = arpptr;
127         arpptr += dev->addr_len;
128         memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
129
130         if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
131                   ARPT_INV_SRCDEVADDR) ||
132             FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
133                   ARPT_INV_TGTDEVADDR)) {
134                 dprintf("Source or target device address mismatch.\n");
135
136                 return 0;
137         }
138
139         if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
140                   ARPT_INV_SRCIP) ||
141             FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
142                   ARPT_INV_TGTIP)) {
143                 dprintf("Source or target IP address mismatch.\n");
144
145                 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
146                         NIPQUAD(src_ipaddr),
147                         NIPQUAD(arpinfo->smsk.s_addr),
148                         NIPQUAD(arpinfo->src.s_addr),
149                         arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
150                 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
151                         NIPQUAD(tgt_ipaddr),
152                         NIPQUAD(arpinfo->tmsk.s_addr),
153                         NIPQUAD(arpinfo->tgt.s_addr),
154                         arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
155                 return 0;
156         }
157
158         /* Look for ifname matches.  */
159         for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
160                 ret |= (indev[i] ^ arpinfo->iniface[i])
161                         & arpinfo->iniface_mask[i];
162         }
163
164         if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
165                 dprintf("VIA in mismatch (%s vs %s).%s\n",
166                         indev, arpinfo->iniface,
167                         arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
168                 return 0;
169         }
170
171         for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
172                 ret |= (outdev[i] ^ arpinfo->outiface[i])
173                         & arpinfo->outiface_mask[i];
174         }
175
176         if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
177                 dprintf("VIA out mismatch (%s vs %s).%s\n",
178                         outdev, arpinfo->outiface,
179                         arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
180                 return 0;
181         }
182
183         return 1;
184 #undef FWINV
185 }
186
187 static inline int arp_checkentry(const struct arpt_arp *arp)
188 {
189         if (arp->flags & ~ARPT_F_MASK) {
190                 duprintf("Unknown flag bits set: %08X\n",
191                          arp->flags & ~ARPT_F_MASK);
192                 return 0;
193         }
194         if (arp->invflags & ~ARPT_INV_MASK) {
195                 duprintf("Unknown invflag bits set: %08X\n",
196                          arp->invflags & ~ARPT_INV_MASK);
197                 return 0;
198         }
199
200         return 1;
201 }
202
203 static unsigned int
204 arpt_error(struct sk_buff *skb, const struct xt_target_param *par)
205 {
206         if (net_ratelimit())
207                 printk("arp_tables: error: '%s'\n",
208                        (const char *)par->targinfo);
209
210         return NF_DROP;
211 }
212
213 static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
214 {
215         return (struct arpt_entry *)(base + offset);
216 }
217
218 unsigned int arpt_do_table(struct sk_buff *skb,
219                            unsigned int hook,
220                            const struct net_device *in,
221                            const struct net_device *out,
222                            struct xt_table *table)
223 {
224         static const char nulldevname[IFNAMSIZ];
225         unsigned int verdict = NF_DROP;
226         const struct arphdr *arp;
227         bool hotdrop = false;
228         struct arpt_entry *e, *back;
229         const char *indev, *outdev;
230         void *table_base;
231         const struct xt_table_info *private;
232         struct xt_target_param tgpar;
233
234         if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
235                 return NF_DROP;
236
237         indev = in ? in->name : nulldevname;
238         outdev = out ? out->name : nulldevname;
239
240         read_lock_bh(&table->lock);
241         private = table->private;
242         table_base = (void *)private->entries[smp_processor_id()];
243         e = get_entry(table_base, private->hook_entry[hook]);
244         back = get_entry(table_base, private->underflow[hook]);
245
246         tgpar.in      = in;
247         tgpar.out     = out;
248         tgpar.hooknum = hook;
249
250         arp = arp_hdr(skb);
251         do {
252                 if (arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
253                         struct arpt_entry_target *t;
254                         int hdr_len;
255
256                         hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
257                                 (2 * skb->dev->addr_len);
258                         ADD_COUNTER(e->counters, hdr_len, 1);
259
260                         t = arpt_get_target(e);
261
262                         /* Standard target? */
263                         if (!t->u.kernel.target->target) {
264                                 int v;
265
266                                 v = ((struct arpt_standard_target *)t)->verdict;
267                                 if (v < 0) {
268                                         /* Pop from stack? */
269                                         if (v != ARPT_RETURN) {
270                                                 verdict = (unsigned)(-v) - 1;
271                                                 break;
272                                         }
273                                         e = back;
274                                         back = get_entry(table_base,
275                                                          back->comefrom);
276                                         continue;
277                                 }
278                                 if (table_base + v
279                                     != (void *)e + e->next_offset) {
280                                         /* Save old back ptr in next entry */
281                                         struct arpt_entry *next
282                                                 = (void *)e + e->next_offset;
283                                         next->comefrom =
284                                                 (void *)back - table_base;
285
286                                         /* set back pointer to next entry */
287                                         back = next;
288                                 }
289
290                                 e = get_entry(table_base, v);
291                         } else {
292                                 /* Targets which reenter must return
293                                  * abs. verdicts
294                                  */
295                                 tgpar.target   = t->u.kernel.target;
296                                 tgpar.targinfo = t->data;
297                                 verdict = t->u.kernel.target->target(skb,
298                                                                      &tgpar);
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                         const 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         const 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 xt_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, NFPROTO_ARP,
468                               t->u.target_size - sizeof(*t),
469                               name, e->comefrom, 0, 0, e, t->data);
470         if (ret < 0) {
471                 duprintf("arp_tables: check failed for `%s'.\n",
472                          t->u.kernel.target->name);
473                 return ret;
474         }
475         return 0;
476 }
477
478 static inline int
479 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
480                  unsigned int *i)
481 {
482         struct arpt_entry_target *t;
483         struct xt_target *target;
484         int ret;
485
486         ret = check_entry(e, name);
487         if (ret)
488                 return ret;
489
490         t = arpt_get_target(e);
491         target = try_then_request_module(xt_find_target(NFPROTO_ARP,
492                                                         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 xt_table *table)
711 {
712         unsigned int countersize;
713         struct xt_counters *counters;
714         const 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 xt_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(NFPROTO_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(NFPROTO_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(NFPROTO_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(struct net *net, void __user *user, int *len, int compat)
853 {
854         char name[ARPT_TABLE_MAXNAMELEN];
855         struct xt_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(NFPROTO_ARP);
871 #endif
872         t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
873                                     "arptable_%s", name);
874         if (t && !IS_ERR(t)) {
875                 struct arpt_getinfo info;
876                 const 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(NFPROTO_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(NFPROTO_ARP);
906 #endif
907         return ret;
908 }
909
910 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
911                        int *len)
912 {
913         int ret;
914         struct arpt_get_entries get;
915         struct xt_table *t;
916
917         if (*len < sizeof(get)) {
918                 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
919                 return -EINVAL;
920         }
921         if (copy_from_user(&get, uptr, sizeof(get)) != 0)
922                 return -EFAULT;
923         if (*len != sizeof(struct arpt_get_entries) + get.size) {
924                 duprintf("get_entries: %u != %Zu\n", *len,
925                          sizeof(struct arpt_get_entries) + get.size);
926                 return -EINVAL;
927         }
928
929         t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
930         if (t && !IS_ERR(t)) {
931                 const struct xt_table_info *private = t->private;
932
933                 duprintf("t->private->number = %u\n",
934                          private->number);
935                 if (get.size == private->size)
936                         ret = copy_entries_to_user(private->size,
937                                                    t, uptr->entrytable);
938                 else {
939                         duprintf("get_entries: I've got %u not %u!\n",
940                                  private->size, get.size);
941                         ret = -EAGAIN;
942                 }
943                 module_put(t->me);
944                 xt_table_unlock(t);
945         } else
946                 ret = t ? PTR_ERR(t) : -ENOENT;
947
948         return ret;
949 }
950
951 static int __do_replace(struct net *net, const char *name,
952                         unsigned int valid_hooks,
953                         struct xt_table_info *newinfo,
954                         unsigned int num_counters,
955                         void __user *counters_ptr)
956 {
957         int ret;
958         struct xt_table *t;
959         struct xt_table_info *oldinfo;
960         struct xt_counters *counters;
961         void *loc_cpu_old_entry;
962
963         ret = 0;
964         counters = vmalloc_node(num_counters * sizeof(struct xt_counters),
965                                 numa_node_id());
966         if (!counters) {
967                 ret = -ENOMEM;
968                 goto out;
969         }
970
971         t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
972                                     "arptable_%s", name);
973         if (!t || IS_ERR(t)) {
974                 ret = t ? PTR_ERR(t) : -ENOENT;
975                 goto free_newinfo_counters_untrans;
976         }
977
978         /* You lied! */
979         if (valid_hooks != t->valid_hooks) {
980                 duprintf("Valid hook crap: %08X vs %08X\n",
981                          valid_hooks, t->valid_hooks);
982                 ret = -EINVAL;
983                 goto put_module;
984         }
985
986         oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
987         if (!oldinfo)
988                 goto put_module;
989
990         /* Update module usage count based on number of rules */
991         duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
992                 oldinfo->number, oldinfo->initial_entries, newinfo->number);
993         if ((oldinfo->number > oldinfo->initial_entries) ||
994             (newinfo->number <= oldinfo->initial_entries))
995                 module_put(t->me);
996         if ((oldinfo->number > oldinfo->initial_entries) &&
997             (newinfo->number <= oldinfo->initial_entries))
998                 module_put(t->me);
999
1000         /* Get the old counters. */
1001         get_counters(oldinfo, counters);
1002         /* Decrease module usage counts and free resource */
1003         loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1004         ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1005                            NULL);
1006
1007         xt_free_table_info(oldinfo);
1008         if (copy_to_user(counters_ptr, counters,
1009                          sizeof(struct xt_counters) * num_counters) != 0)
1010                 ret = -EFAULT;
1011         vfree(counters);
1012         xt_table_unlock(t);
1013         return ret;
1014
1015  put_module:
1016         module_put(t->me);
1017         xt_table_unlock(t);
1018  free_newinfo_counters_untrans:
1019         vfree(counters);
1020  out:
1021         return ret;
1022 }
1023
1024 static int do_replace(struct net *net, void __user *user, unsigned int len)
1025 {
1026         int ret;
1027         struct arpt_replace tmp;
1028         struct xt_table_info *newinfo;
1029         void *loc_cpu_entry;
1030
1031         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1032                 return -EFAULT;
1033
1034         /* overflow check */
1035         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1036                 return -ENOMEM;
1037
1038         newinfo = xt_alloc_table_info(tmp.size);
1039         if (!newinfo)
1040                 return -ENOMEM;
1041
1042         /* choose the copy that is on our node/cpu */
1043         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1044         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1045                            tmp.size) != 0) {
1046                 ret = -EFAULT;
1047                 goto free_newinfo;
1048         }
1049
1050         ret = translate_table(tmp.name, tmp.valid_hooks,
1051                               newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
1052                               tmp.hook_entry, tmp.underflow);
1053         if (ret != 0)
1054                 goto free_newinfo;
1055
1056         duprintf("arp_tables: Translated table\n");
1057
1058         ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1059                            tmp.num_counters, tmp.counters);
1060         if (ret)
1061                 goto free_newinfo_untrans;
1062         return 0;
1063
1064  free_newinfo_untrans:
1065         ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1066  free_newinfo:
1067         xt_free_table_info(newinfo);
1068         return ret;
1069 }
1070
1071 /* We're lazy, and add to the first CPU; overflow works its fey magic
1072  * and everything is OK.
1073  */
1074 static inline int add_counter_to_entry(struct arpt_entry *e,
1075                                        const struct xt_counters addme[],
1076                                        unsigned int *i)
1077 {
1078
1079         ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
1080
1081         (*i)++;
1082         return 0;
1083 }
1084
1085 static int do_add_counters(struct net *net, void __user *user, unsigned int len,
1086                            int compat)
1087 {
1088         unsigned int i;
1089         struct xt_counters_info tmp;
1090         struct xt_counters *paddc;
1091         unsigned int num_counters;
1092         const char *name;
1093         int size;
1094         void *ptmp;
1095         struct xt_table *t;
1096         const struct xt_table_info *private;
1097         int ret = 0;
1098         void *loc_cpu_entry;
1099 #ifdef CONFIG_COMPAT
1100         struct compat_xt_counters_info compat_tmp;
1101
1102         if (compat) {
1103                 ptmp = &compat_tmp;
1104                 size = sizeof(struct compat_xt_counters_info);
1105         } else
1106 #endif
1107         {
1108                 ptmp = &tmp;
1109                 size = sizeof(struct xt_counters_info);
1110         }
1111
1112         if (copy_from_user(ptmp, user, size) != 0)
1113                 return -EFAULT;
1114
1115 #ifdef CONFIG_COMPAT
1116         if (compat) {
1117                 num_counters = compat_tmp.num_counters;
1118                 name = compat_tmp.name;
1119         } else
1120 #endif
1121         {
1122                 num_counters = tmp.num_counters;
1123                 name = tmp.name;
1124         }
1125
1126         if (len != size + num_counters * sizeof(struct xt_counters))
1127                 return -EINVAL;
1128
1129         paddc = vmalloc_node(len - size, numa_node_id());
1130         if (!paddc)
1131                 return -ENOMEM;
1132
1133         if (copy_from_user(paddc, user + size, len - size) != 0) {
1134                 ret = -EFAULT;
1135                 goto free;
1136         }
1137
1138         t = xt_find_table_lock(net, NFPROTO_ARP, name);
1139         if (!t || IS_ERR(t)) {
1140                 ret = t ? PTR_ERR(t) : -ENOENT;
1141                 goto free;
1142         }
1143
1144         write_lock_bh(&t->lock);
1145         private = t->private;
1146         if (private->number != num_counters) {
1147                 ret = -EINVAL;
1148                 goto unlock_up_free;
1149         }
1150
1151         i = 0;
1152         /* Choose the copy that is on our node */
1153         loc_cpu_entry = private->entries[smp_processor_id()];
1154         ARPT_ENTRY_ITERATE(loc_cpu_entry,
1155                            private->size,
1156                            add_counter_to_entry,
1157                            paddc,
1158                            &i);
1159  unlock_up_free:
1160         write_unlock_bh(&t->lock);
1161         xt_table_unlock(t);
1162         module_put(t->me);
1163  free:
1164         vfree(paddc);
1165
1166         return ret;
1167 }
1168
1169 #ifdef CONFIG_COMPAT
1170 static inline int
1171 compat_release_entry(struct compat_arpt_entry *e, unsigned int *i)
1172 {
1173         struct arpt_entry_target *t;
1174
1175         if (i && (*i)-- == 0)
1176                 return 1;
1177
1178         t = compat_arpt_get_target(e);
1179         module_put(t->u.kernel.target->me);
1180         return 0;
1181 }
1182
1183 static inline int
1184 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1185                                   struct xt_table_info *newinfo,
1186                                   unsigned int *size,
1187                                   unsigned char *base,
1188                                   unsigned char *limit,
1189                                   unsigned int *hook_entries,
1190                                   unsigned int *underflows,
1191                                   unsigned int *i,
1192                                   const char *name)
1193 {
1194         struct arpt_entry_target *t;
1195         struct xt_target *target;
1196         unsigned int entry_offset;
1197         int ret, off, h;
1198
1199         duprintf("check_compat_entry_size_and_hooks %p\n", e);
1200         if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0
1201             || (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
1202                 duprintf("Bad offset %p, limit = %p\n", e, limit);
1203                 return -EINVAL;
1204         }
1205
1206         if (e->next_offset < sizeof(struct compat_arpt_entry) +
1207                              sizeof(struct compat_xt_entry_target)) {
1208                 duprintf("checking: element %p size %u\n",
1209                          e, e->next_offset);
1210                 return -EINVAL;
1211         }
1212
1213         /* For purposes of check_entry casting the compat entry is fine */
1214         ret = check_entry((struct arpt_entry *)e, name);
1215         if (ret)
1216                 return ret;
1217
1218         off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1219         entry_offset = (void *)e - (void *)base;
1220
1221         t = compat_arpt_get_target(e);
1222         target = try_then_request_module(xt_find_target(NFPROTO_ARP,
1223                                                         t->u.user.name,
1224                                                         t->u.user.revision),
1225                                          "arpt_%s", t->u.user.name);
1226         if (IS_ERR(target) || !target) {
1227                 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1228                          t->u.user.name);
1229                 ret = target ? PTR_ERR(target) : -ENOENT;
1230                 goto out;
1231         }
1232         t->u.kernel.target = target;
1233
1234         off += xt_compat_target_offset(target);
1235         *size += off;
1236         ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1237         if (ret)
1238                 goto release_target;
1239
1240         /* Check hooks & underflows */
1241         for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1242                 if ((unsigned char *)e - base == hook_entries[h])
1243                         newinfo->hook_entry[h] = hook_entries[h];
1244                 if ((unsigned char *)e - base == underflows[h])
1245                         newinfo->underflow[h] = underflows[h];
1246         }
1247
1248         /* Clear counters and comefrom */
1249         memset(&e->counters, 0, sizeof(e->counters));
1250         e->comefrom = 0;
1251
1252         (*i)++;
1253         return 0;
1254
1255 release_target:
1256         module_put(t->u.kernel.target->me);
1257 out:
1258         return ret;
1259 }
1260
1261 static int
1262 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1263                             unsigned int *size, const char *name,
1264                             struct xt_table_info *newinfo, unsigned char *base)
1265 {
1266         struct arpt_entry_target *t;
1267         struct xt_target *target;
1268         struct arpt_entry *de;
1269         unsigned int origsize;
1270         int ret, h;
1271
1272         ret = 0;
1273         origsize = *size;
1274         de = (struct arpt_entry *)*dstptr;
1275         memcpy(de, e, sizeof(struct arpt_entry));
1276         memcpy(&de->counters, &e->counters, sizeof(e->counters));
1277
1278         *dstptr += sizeof(struct arpt_entry);
1279         *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1280
1281         de->target_offset = e->target_offset - (origsize - *size);
1282         t = compat_arpt_get_target(e);
1283         target = t->u.kernel.target;
1284         xt_compat_target_from_user(t, dstptr, size);
1285
1286         de->next_offset = e->next_offset - (origsize - *size);
1287         for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1288                 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1289                         newinfo->hook_entry[h] -= origsize - *size;
1290                 if ((unsigned char *)de - base < newinfo->underflow[h])
1291                         newinfo->underflow[h] -= origsize - *size;
1292         }
1293         return ret;
1294 }
1295
1296 static inline int compat_check_entry(struct arpt_entry *e, const char *name,
1297                                      unsigned int *i)
1298 {
1299         int ret;
1300
1301         ret = check_target(e, name);
1302         if (ret)
1303                 return ret;
1304
1305         (*i)++;
1306         return 0;
1307 }
1308
1309 static int translate_compat_table(const char *name,
1310                                   unsigned int valid_hooks,
1311                                   struct xt_table_info **pinfo,
1312                                   void **pentry0,
1313                                   unsigned int total_size,
1314                                   unsigned int number,
1315                                   unsigned int *hook_entries,
1316                                   unsigned int *underflows)
1317 {
1318         unsigned int i, j;
1319         struct xt_table_info *newinfo, *info;
1320         void *pos, *entry0, *entry1;
1321         unsigned int size;
1322         int ret;
1323
1324         info = *pinfo;
1325         entry0 = *pentry0;
1326         size = total_size;
1327         info->number = number;
1328
1329         /* Init all hooks to impossible value. */
1330         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1331                 info->hook_entry[i] = 0xFFFFFFFF;
1332                 info->underflow[i] = 0xFFFFFFFF;
1333         }
1334
1335         duprintf("translate_compat_table: size %u\n", info->size);
1336         j = 0;
1337         xt_compat_lock(NFPROTO_ARP);
1338         /* Walk through entries, checking offsets. */
1339         ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size,
1340                                         check_compat_entry_size_and_hooks,
1341                                         info, &size, entry0,
1342                                         entry0 + total_size,
1343                                         hook_entries, underflows, &j, name);
1344         if (ret != 0)
1345                 goto out_unlock;
1346
1347         ret = -EINVAL;
1348         if (j != number) {
1349                 duprintf("translate_compat_table: %u not %u entries\n",
1350                          j, number);
1351                 goto out_unlock;
1352         }
1353
1354         /* Check hooks all assigned */
1355         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1356                 /* Only hooks which are valid */
1357                 if (!(valid_hooks & (1 << i)))
1358                         continue;
1359                 if (info->hook_entry[i] == 0xFFFFFFFF) {
1360                         duprintf("Invalid hook entry %u %u\n",
1361                                  i, hook_entries[i]);
1362                         goto out_unlock;
1363                 }
1364                 if (info->underflow[i] == 0xFFFFFFFF) {
1365                         duprintf("Invalid underflow %u %u\n",
1366                                  i, underflows[i]);
1367                         goto out_unlock;
1368                 }
1369         }
1370
1371         ret = -ENOMEM;
1372         newinfo = xt_alloc_table_info(size);
1373         if (!newinfo)
1374                 goto out_unlock;
1375
1376         newinfo->number = number;
1377         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1378                 newinfo->hook_entry[i] = info->hook_entry[i];
1379                 newinfo->underflow[i] = info->underflow[i];
1380         }
1381         entry1 = newinfo->entries[raw_smp_processor_id()];
1382         pos = entry1;
1383         size = total_size;
1384         ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size,
1385                                         compat_copy_entry_from_user,
1386                                         &pos, &size, name, newinfo, entry1);
1387         xt_compat_flush_offsets(NFPROTO_ARP);
1388         xt_compat_unlock(NFPROTO_ARP);
1389         if (ret)
1390                 goto free_newinfo;
1391
1392         ret = -ELOOP;
1393         if (!mark_source_chains(newinfo, valid_hooks, entry1))
1394                 goto free_newinfo;
1395
1396         i = 0;
1397         ret = ARPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
1398                                  name, &i);
1399         if (ret) {
1400                 j -= i;
1401                 COMPAT_ARPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1402                                                    compat_release_entry, &j);
1403                 ARPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1404                 xt_free_table_info(newinfo);
1405                 return ret;
1406         }
1407
1408         /* And one copy for every other CPU */
1409         for_each_possible_cpu(i)
1410                 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1411                         memcpy(newinfo->entries[i], entry1, newinfo->size);
1412
1413         *pinfo = newinfo;
1414         *pentry0 = entry1;
1415         xt_free_table_info(info);
1416         return 0;
1417
1418 free_newinfo:
1419         xt_free_table_info(newinfo);
1420 out:
1421         COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
1422         return ret;
1423 out_unlock:
1424         xt_compat_flush_offsets(NFPROTO_ARP);
1425         xt_compat_unlock(NFPROTO_ARP);
1426         goto out;
1427 }
1428
1429 struct compat_arpt_replace {
1430         char                            name[ARPT_TABLE_MAXNAMELEN];
1431         u32                             valid_hooks;
1432         u32                             num_entries;
1433         u32                             size;
1434         u32                             hook_entry[NF_ARP_NUMHOOKS];
1435         u32                             underflow[NF_ARP_NUMHOOKS];
1436         u32                             num_counters;
1437         compat_uptr_t                   counters;
1438         struct compat_arpt_entry        entries[0];
1439 };
1440
1441 static int compat_do_replace(struct net *net, void __user *user,
1442                              unsigned int len)
1443 {
1444         int ret;
1445         struct compat_arpt_replace tmp;
1446         struct xt_table_info *newinfo;
1447         void *loc_cpu_entry;
1448
1449         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1450                 return -EFAULT;
1451
1452         /* overflow check */
1453         if (tmp.size >= INT_MAX / num_possible_cpus())
1454                 return -ENOMEM;
1455         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1456                 return -ENOMEM;
1457
1458         newinfo = xt_alloc_table_info(tmp.size);
1459         if (!newinfo)
1460                 return -ENOMEM;
1461
1462         /* choose the copy that is on our node/cpu */
1463         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1464         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1465                 ret = -EFAULT;
1466                 goto free_newinfo;
1467         }
1468
1469         ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1470                                      &newinfo, &loc_cpu_entry, tmp.size,
1471                                      tmp.num_entries, tmp.hook_entry,
1472                                      tmp.underflow);
1473         if (ret != 0)
1474                 goto free_newinfo;
1475
1476         duprintf("compat_do_replace: Translated table\n");
1477
1478         ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1479                            tmp.num_counters, compat_ptr(tmp.counters));
1480         if (ret)
1481                 goto free_newinfo_untrans;
1482         return 0;
1483
1484  free_newinfo_untrans:
1485         ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1486  free_newinfo:
1487         xt_free_table_info(newinfo);
1488         return ret;
1489 }
1490
1491 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1492                                   unsigned int len)
1493 {
1494         int ret;
1495
1496         if (!capable(CAP_NET_ADMIN))
1497                 return -EPERM;
1498
1499         switch (cmd) {
1500         case ARPT_SO_SET_REPLACE:
1501                 ret = compat_do_replace(sock_net(sk), user, len);
1502                 break;
1503
1504         case ARPT_SO_SET_ADD_COUNTERS:
1505                 ret = do_add_counters(sock_net(sk), user, len, 1);
1506                 break;
1507
1508         default:
1509                 duprintf("do_arpt_set_ctl:  unknown request %i\n", cmd);
1510                 ret = -EINVAL;
1511         }
1512
1513         return ret;
1514 }
1515
1516 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1517                                      compat_uint_t *size,
1518                                      struct xt_counters *counters,
1519                                      unsigned int *i)
1520 {
1521         struct arpt_entry_target *t;
1522         struct compat_arpt_entry __user *ce;
1523         u_int16_t target_offset, next_offset;
1524         compat_uint_t origsize;
1525         int ret;
1526
1527         ret = -EFAULT;
1528         origsize = *size;
1529         ce = (struct compat_arpt_entry __user *)*dstptr;
1530         if (copy_to_user(ce, e, sizeof(struct arpt_entry)))
1531                 goto out;
1532
1533         if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1534                 goto out;
1535
1536         *dstptr += sizeof(struct compat_arpt_entry);
1537         *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1538
1539         target_offset = e->target_offset - (origsize - *size);
1540
1541         t = arpt_get_target(e);
1542         ret = xt_compat_target_to_user(t, dstptr, size);
1543         if (ret)
1544                 goto out;
1545         ret = -EFAULT;
1546         next_offset = e->next_offset - (origsize - *size);
1547         if (put_user(target_offset, &ce->target_offset))
1548                 goto out;
1549         if (put_user(next_offset, &ce->next_offset))
1550                 goto out;
1551
1552         (*i)++;
1553         return 0;
1554 out:
1555         return ret;
1556 }
1557
1558 static int compat_copy_entries_to_user(unsigned int total_size,
1559                                        struct xt_table *table,
1560                                        void __user *userptr)
1561 {
1562         struct xt_counters *counters;
1563         const struct xt_table_info *private = table->private;
1564         void __user *pos;
1565         unsigned int size;
1566         int ret = 0;
1567         void *loc_cpu_entry;
1568         unsigned int i = 0;
1569
1570         counters = alloc_counters(table);
1571         if (IS_ERR(counters))
1572                 return PTR_ERR(counters);
1573
1574         /* choose the copy on our node/cpu */
1575         loc_cpu_entry = private->entries[raw_smp_processor_id()];
1576         pos = userptr;
1577         size = total_size;
1578         ret = ARPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
1579                                  compat_copy_entry_to_user,
1580                                  &pos, &size, counters, &i);
1581         vfree(counters);
1582         return ret;
1583 }
1584
1585 struct compat_arpt_get_entries {
1586         char name[ARPT_TABLE_MAXNAMELEN];
1587         compat_uint_t size;
1588         struct compat_arpt_entry entrytable[0];
1589 };
1590
1591 static int compat_get_entries(struct net *net,
1592                               struct compat_arpt_get_entries __user *uptr,
1593                               int *len)
1594 {
1595         int ret;
1596         struct compat_arpt_get_entries get;
1597         struct xt_table *t;
1598
1599         if (*len < sizeof(get)) {
1600                 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1601                 return -EINVAL;
1602         }
1603         if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1604                 return -EFAULT;
1605         if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1606                 duprintf("compat_get_entries: %u != %zu\n",
1607                          *len, sizeof(get) + get.size);
1608                 return -EINVAL;
1609         }
1610
1611         xt_compat_lock(NFPROTO_ARP);
1612         t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1613         if (t && !IS_ERR(t)) {
1614                 const struct xt_table_info *private = t->private;
1615                 struct xt_table_info info;
1616
1617                 duprintf("t->private->number = %u\n", private->number);
1618                 ret = compat_table_info(private, &info);
1619                 if (!ret && get.size == info.size) {
1620                         ret = compat_copy_entries_to_user(private->size,
1621                                                           t, uptr->entrytable);
1622                 } else if (!ret) {
1623                         duprintf("compat_get_entries: I've got %u not %u!\n",
1624                                  private->size, get.size);
1625                         ret = -EAGAIN;
1626                 }
1627                 xt_compat_flush_offsets(NFPROTO_ARP);
1628                 module_put(t->me);
1629                 xt_table_unlock(t);
1630         } else
1631                 ret = t ? PTR_ERR(t) : -ENOENT;
1632
1633         xt_compat_unlock(NFPROTO_ARP);
1634         return ret;
1635 }
1636
1637 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1638
1639 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1640                                   int *len)
1641 {
1642         int ret;
1643
1644         if (!capable(CAP_NET_ADMIN))
1645                 return -EPERM;
1646
1647         switch (cmd) {
1648         case ARPT_SO_GET_INFO:
1649                 ret = get_info(sock_net(sk), user, len, 1);
1650                 break;
1651         case ARPT_SO_GET_ENTRIES:
1652                 ret = compat_get_entries(sock_net(sk), user, len);
1653                 break;
1654         default:
1655                 ret = do_arpt_get_ctl(sk, cmd, user, len);
1656         }
1657         return ret;
1658 }
1659 #endif
1660
1661 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1662 {
1663         int ret;
1664
1665         if (!capable(CAP_NET_ADMIN))
1666                 return -EPERM;
1667
1668         switch (cmd) {
1669         case ARPT_SO_SET_REPLACE:
1670                 ret = do_replace(sock_net(sk), user, len);
1671                 break;
1672
1673         case ARPT_SO_SET_ADD_COUNTERS:
1674                 ret = do_add_counters(sock_net(sk), user, len, 0);
1675                 break;
1676
1677         default:
1678                 duprintf("do_arpt_set_ctl:  unknown request %i\n", cmd);
1679                 ret = -EINVAL;
1680         }
1681
1682         return ret;
1683 }
1684
1685 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1686 {
1687         int ret;
1688
1689         if (!capable(CAP_NET_ADMIN))
1690                 return -EPERM;
1691
1692         switch (cmd) {
1693         case ARPT_SO_GET_INFO:
1694                 ret = get_info(sock_net(sk), user, len, 0);
1695                 break;
1696
1697         case ARPT_SO_GET_ENTRIES:
1698                 ret = get_entries(sock_net(sk), user, len);
1699                 break;
1700
1701         case ARPT_SO_GET_REVISION_TARGET: {
1702                 struct xt_get_revision rev;
1703
1704                 if (*len != sizeof(rev)) {
1705                         ret = -EINVAL;
1706                         break;
1707                 }
1708                 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1709                         ret = -EFAULT;
1710                         break;
1711                 }
1712
1713                 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1714                                                          rev.revision, 1, &ret),
1715                                         "arpt_%s", rev.name);
1716                 break;
1717         }
1718
1719         default:
1720                 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1721                 ret = -EINVAL;
1722         }
1723
1724         return ret;
1725 }
1726
1727 struct xt_table *arpt_register_table(struct net *net, struct xt_table *table,
1728                                      const struct arpt_replace *repl)
1729 {
1730         int ret;
1731         struct xt_table_info *newinfo;
1732         struct xt_table_info bootstrap
1733                 = { 0, 0, 0, { 0 }, { 0 }, { } };
1734         void *loc_cpu_entry;
1735         struct xt_table *new_table;
1736
1737         newinfo = xt_alloc_table_info(repl->size);
1738         if (!newinfo) {
1739                 ret = -ENOMEM;
1740                 goto out;
1741         }
1742
1743         /* choose the copy on our node/cpu */
1744         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1745         memcpy(loc_cpu_entry, repl->entries, repl->size);
1746
1747         ret = translate_table(table->name, table->valid_hooks,
1748                               newinfo, loc_cpu_entry, repl->size,
1749                               repl->num_entries,
1750                               repl->hook_entry,
1751                               repl->underflow);
1752
1753         duprintf("arpt_register_table: translate table gives %d\n", ret);
1754         if (ret != 0)
1755                 goto out_free;
1756
1757         new_table = xt_register_table(net, table, &bootstrap, newinfo);
1758         if (IS_ERR(new_table)) {
1759                 ret = PTR_ERR(new_table);
1760                 goto out_free;
1761         }
1762         return new_table;
1763
1764 out_free:
1765         xt_free_table_info(newinfo);
1766 out:
1767         return ERR_PTR(ret);
1768 }
1769
1770 void arpt_unregister_table(struct xt_table *table)
1771 {
1772         struct xt_table_info *private;
1773         void *loc_cpu_entry;
1774         struct module *table_owner = table->me;
1775
1776         private = xt_unregister_table(table);
1777
1778         /* Decrease module usage counts and free resources */
1779         loc_cpu_entry = private->entries[raw_smp_processor_id()];
1780         ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size,
1781                            cleanup_entry, NULL);
1782         if (private->number > private->initial_entries)
1783                 module_put(table_owner);
1784         xt_free_table_info(private);
1785 }
1786
1787 /* The built-in targets: standard (NULL) and error. */
1788 static struct xt_target arpt_standard_target __read_mostly = {
1789         .name           = ARPT_STANDARD_TARGET,
1790         .targetsize     = sizeof(int),
1791         .family         = NFPROTO_ARP,
1792 #ifdef CONFIG_COMPAT
1793         .compatsize     = sizeof(compat_int_t),
1794         .compat_from_user = compat_standard_from_user,
1795         .compat_to_user = compat_standard_to_user,
1796 #endif
1797 };
1798
1799 static struct xt_target arpt_error_target __read_mostly = {
1800         .name           = ARPT_ERROR_TARGET,
1801         .target         = arpt_error,
1802         .targetsize     = ARPT_FUNCTION_MAXNAMELEN,
1803         .family         = NFPROTO_ARP,
1804 };
1805
1806 static struct nf_sockopt_ops arpt_sockopts = {
1807         .pf             = PF_INET,
1808         .set_optmin     = ARPT_BASE_CTL,
1809         .set_optmax     = ARPT_SO_SET_MAX+1,
1810         .set            = do_arpt_set_ctl,
1811 #ifdef CONFIG_COMPAT
1812         .compat_set     = compat_do_arpt_set_ctl,
1813 #endif
1814         .get_optmin     = ARPT_BASE_CTL,
1815         .get_optmax     = ARPT_SO_GET_MAX+1,
1816         .get            = do_arpt_get_ctl,
1817 #ifdef CONFIG_COMPAT
1818         .compat_get     = compat_do_arpt_get_ctl,
1819 #endif
1820         .owner          = THIS_MODULE,
1821 };
1822
1823 static int __net_init arp_tables_net_init(struct net *net)
1824 {
1825         return xt_proto_init(net, NFPROTO_ARP);
1826 }
1827
1828 static void __net_exit arp_tables_net_exit(struct net *net)
1829 {
1830         xt_proto_fini(net, NFPROTO_ARP);
1831 }
1832
1833 static struct pernet_operations arp_tables_net_ops = {
1834         .init = arp_tables_net_init,
1835         .exit = arp_tables_net_exit,
1836 };
1837
1838 static int __init arp_tables_init(void)
1839 {
1840         int ret;
1841
1842         ret = register_pernet_subsys(&arp_tables_net_ops);
1843         if (ret < 0)
1844                 goto err1;
1845
1846         /* Noone else will be downing sem now, so we won't sleep */
1847         ret = xt_register_target(&arpt_standard_target);
1848         if (ret < 0)
1849                 goto err2;
1850         ret = xt_register_target(&arpt_error_target);
1851         if (ret < 0)
1852                 goto err3;
1853
1854         /* Register setsockopt */
1855         ret = nf_register_sockopt(&arpt_sockopts);
1856         if (ret < 0)
1857                 goto err4;
1858
1859         printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1860         return 0;
1861
1862 err4:
1863         xt_unregister_target(&arpt_error_target);
1864 err3:
1865         xt_unregister_target(&arpt_standard_target);
1866 err2:
1867         unregister_pernet_subsys(&arp_tables_net_ops);
1868 err1:
1869         return ret;
1870 }
1871
1872 static void __exit arp_tables_fini(void)
1873 {
1874         nf_unregister_sockopt(&arpt_sockopts);
1875         xt_unregister_target(&arpt_error_target);
1876         xt_unregister_target(&arpt_standard_target);
1877         unregister_pernet_subsys(&arp_tables_net_ops);
1878 }
1879
1880 EXPORT_SYMBOL(arpt_register_table);
1881 EXPORT_SYMBOL(arpt_unregister_table);
1882 EXPORT_SYMBOL(arpt_do_table);
1883
1884 module_init(arp_tables_init);
1885 module_exit(arp_tables_fini);