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