linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / 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/config.h>
13 #include <linux/kernel.h>
14 #include <linux/skbuff.h>
15 #include <linux/netdevice.h>
16 #include <linux/capability.h>
17 #include <linux/if_arp.h>
18 #include <linux/kmod.h>
19 #include <linux/vmalloc.h>
20 #include <linux/proc_fs.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23
24 #include <asm/uaccess.h>
25 #include <asm/semaphore.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 #include <linux/netfilter_ipv4/listhelp.h>
61
62 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
63                                       char *hdr_addr, int len)
64 {
65         int i, ret;
66
67         if (len > ARPT_DEV_ADDR_LEN_MAX)
68                 len = ARPT_DEV_ADDR_LEN_MAX;
69
70         ret = 0;
71         for (i = 0; i < len; i++)
72                 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
73
74         return (ret != 0);
75 }
76
77 /* Returns whether packet matches rule or not. */
78 static inline int arp_packet_match(const struct arphdr *arphdr,
79                                    struct net_device *dev,
80                                    const char *indev,
81                                    const char *outdev,
82                                    const struct arpt_arp *arpinfo)
83 {
84         char *arpptr = (char *)(arphdr + 1);
85         char *src_devaddr, *tgt_devaddr;
86         u32 src_ipaddr, tgt_ipaddr;
87         int i, ret;
88
89 #define FWINV(bool,invflg) ((bool) ^ !!(arpinfo->invflags & invflg))
90
91         if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
92                   ARPT_INV_ARPOP)) {
93                 dprintf("ARP operation field mismatch.\n");
94                 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
95                         arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
96                 return 0;
97         }
98
99         if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
100                   ARPT_INV_ARPHRD)) {
101                 dprintf("ARP hardware address format mismatch.\n");
102                 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
103                         arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
104                 return 0;
105         }
106
107         if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
108                   ARPT_INV_ARPPRO)) {
109                 dprintf("ARP protocol address format mismatch.\n");
110                 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
111                         arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
112                 return 0;
113         }
114
115         if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
116                   ARPT_INV_ARPHLN)) {
117                 dprintf("ARP hardware address length mismatch.\n");
118                 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
119                         arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
120                 return 0;
121         }
122
123         src_devaddr = arpptr;
124         arpptr += dev->addr_len;
125         memcpy(&src_ipaddr, arpptr, sizeof(u32));
126         arpptr += sizeof(u32);
127         tgt_devaddr = arpptr;
128         arpptr += dev->addr_len;
129         memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
130
131         if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
132                   ARPT_INV_SRCDEVADDR) ||
133             FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
134                   ARPT_INV_TGTDEVADDR)) {
135                 dprintf("Source or target device address mismatch.\n");
136
137                 return 0;
138         }
139
140         if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
141                   ARPT_INV_SRCIP) ||
142             FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
143                   ARPT_INV_TGTIP)) {
144                 dprintf("Source or target IP address mismatch.\n");
145
146                 dprintf("SRC: %u.%u.%u.%u. Mask: %u.%u.%u.%u. Target: %u.%u.%u.%u.%s\n",
147                         NIPQUAD(src_ipaddr),
148                         NIPQUAD(arpinfo->smsk.s_addr),
149                         NIPQUAD(arpinfo->src.s_addr),
150                         arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
151                 dprintf("TGT: %u.%u.%u.%u Mask: %u.%u.%u.%u Target: %u.%u.%u.%u.%s\n",
152                         NIPQUAD(tgt_ipaddr),
153                         NIPQUAD(arpinfo->tmsk.s_addr),
154                         NIPQUAD(arpinfo->tgt.s_addr),
155                         arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
156                 return 0;
157         }
158
159         /* Look for ifname matches.  */
160         for (i = 0, ret = 0; i < IFNAMSIZ; i++) {
161                 ret |= (indev[i] ^ arpinfo->iniface[i])
162                         & arpinfo->iniface_mask[i];
163         }
164
165         if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
166                 dprintf("VIA in mismatch (%s vs %s).%s\n",
167                         indev, arpinfo->iniface,
168                         arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
169                 return 0;
170         }
171
172         for (i = 0, ret = 0; i < IFNAMSIZ/sizeof(unsigned long); i++) {
173                 unsigned long odev;
174                 memcpy(&odev, outdev + i*sizeof(unsigned long),
175                        sizeof(unsigned long));
176                 ret |= (odev
177                         ^ ((const unsigned long *)arpinfo->outiface)[i])
178                         & ((const unsigned long *)arpinfo->outiface_mask)[i];
179         }
180
181         if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
182                 dprintf("VIA out mismatch (%s vs %s).%s\n",
183                         outdev, arpinfo->outiface,
184                         arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
185                 return 0;
186         }
187
188         return 1;
189 }
190
191 static inline int arp_checkentry(const struct arpt_arp *arp)
192 {
193         if (arp->flags & ~ARPT_F_MASK) {
194                 duprintf("Unknown flag bits set: %08X\n",
195                          arp->flags & ~ARPT_F_MASK);
196                 return 0;
197         }
198         if (arp->invflags & ~ARPT_INV_MASK) {
199                 duprintf("Unknown invflag bits set: %08X\n",
200                          arp->invflags & ~ARPT_INV_MASK);
201                 return 0;
202         }
203
204         return 1;
205 }
206
207 static unsigned int arpt_error(struct sk_buff **pskb,
208                                const struct net_device *in,
209                                const struct net_device *out,
210                                unsigned int hooknum,
211                                const void *targinfo,
212                                void *userinfo)
213 {
214         if (net_ratelimit())
215                 printk("arp_tables: error: '%s'\n", (char *)targinfo);
216
217         return NF_DROP;
218 }
219
220 static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
221 {
222         return (struct arpt_entry *)(base + offset);
223 }
224
225 unsigned int arpt_do_table(struct sk_buff **pskb,
226                            unsigned int hook,
227                            const struct net_device *in,
228                            const struct net_device *out,
229                            struct arpt_table *table,
230                            void *userdata)
231 {
232         static const char nulldevname[IFNAMSIZ];
233         unsigned int verdict = NF_DROP;
234         struct arphdr *arp;
235         int hotdrop = 0;
236         struct arpt_entry *e, *back;
237         const char *indev, *outdev;
238         void *table_base;
239         struct xt_table_info *private;
240
241         /* ARP header, plus 2 device addresses, plus 2 IP addresses.  */
242         if (!pskb_may_pull((*pskb), (sizeof(struct arphdr) +
243                                      (2 * (*pskb)->dev->addr_len) +
244                                      (2 * sizeof(u32)))))
245                 return NF_DROP;
246
247         indev = in ? in->name : nulldevname;
248         outdev = out ? out->name : nulldevname;
249
250         read_lock_bh(&table->lock);
251         private = table->private;
252         table_base = (void *)private->entries[smp_processor_id()];
253         e = get_entry(table_base, private->hook_entry[hook]);
254         back = get_entry(table_base, private->underflow[hook]);
255
256         arp = (*pskb)->nh.arph;
257         do {
258                 if (arp_packet_match(arp, (*pskb)->dev, indev, outdev, &e->arp)) {
259                         struct arpt_entry_target *t;
260                         int hdr_len;
261
262                         hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
263                                 (2 * (*pskb)->dev->addr_len);
264                         ADD_COUNTER(e->counters, hdr_len, 1);
265
266                         t = arpt_get_target(e);
267
268                         /* Standard target? */
269                         if (!t->u.kernel.target->target) {
270                                 int v;
271
272                                 v = ((struct arpt_standard_target *)t)->verdict;
273                                 if (v < 0) {
274                                         /* Pop from stack? */
275                                         if (v != ARPT_RETURN) {
276                                                 verdict = (unsigned)(-v) - 1;
277                                                 break;
278                                         }
279                                         e = back;
280                                         back = get_entry(table_base,
281                                                          back->comefrom);
282                                         continue;
283                                 }
284                                 if (table_base + v
285                                     != (void *)e + e->next_offset) {
286                                         /* Save old back ptr in next entry */
287                                         struct arpt_entry *next
288                                                 = (void *)e + e->next_offset;
289                                         next->comefrom =
290                                                 (void *)back - table_base;
291
292                                         /* set back pointer to next entry */
293                                         back = next;
294                                 }
295
296                                 e = get_entry(table_base, v);
297                         } else {
298                                 /* Targets which reenter must return
299                                  * abs. verdicts
300                                  */
301                                 verdict = t->u.kernel.target->target(pskb,
302                                                                      in, out,
303                                                                      hook,
304                                                                      t->data,
305                                                                      userdata);
306
307                                 /* Target might have changed stuff. */
308                                 arp = (*pskb)->nh.arph;
309
310                                 if (verdict == ARPT_CONTINUE)
311                                         e = (void *)e + e->next_offset;
312                                 else
313                                         /* Verdict */
314                                         break;
315                         }
316                 } else {
317                         e = (void *)e + e->next_offset;
318                 }
319         } while (!hotdrop);
320         read_unlock_bh(&table->lock);
321
322         if (hotdrop)
323                 return NF_DROP;
324         else
325                 return verdict;
326 }
327
328 /* All zeroes == unconditional rule. */
329 static inline int unconditional(const struct arpt_arp *arp)
330 {
331         unsigned int i;
332
333         for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
334                 if (((__u32 *)arp)[i])
335                         return 0;
336
337         return 1;
338 }
339
340 /* Figures out from what hook each rule can be called: returns 0 if
341  * there are loops.  Puts hook bitmask in comefrom.
342  */
343 static int mark_source_chains(struct xt_table_info *newinfo,
344                               unsigned int valid_hooks, void *entry0)
345 {
346         unsigned int hook;
347
348         /* No recursion; use packet counter to save back ptrs (reset
349          * to 0 as we leave), and comefrom to save source hook bitmask.
350          */
351         for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
352                 unsigned int pos = newinfo->hook_entry[hook];
353                 struct arpt_entry *e
354                         = (struct arpt_entry *)(entry0 + pos);
355
356                 if (!(valid_hooks & (1 << hook)))
357                         continue;
358
359                 /* Set initial back pointer. */
360                 e->counters.pcnt = pos;
361
362                 for (;;) {
363                         struct arpt_standard_target *t
364                                 = (void *)arpt_get_target(e);
365
366                         if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
367                                 printk("arptables: loop hook %u pos %u %08X.\n",
368                                        hook, pos, e->comefrom);
369                                 return 0;
370                         }
371                         e->comefrom
372                                 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
373
374                         /* Unconditional return/END. */
375                         if (e->target_offset == sizeof(struct arpt_entry)
376                             && (strcmp(t->target.u.user.name,
377                                        ARPT_STANDARD_TARGET) == 0)
378                             && t->verdict < 0
379                             && unconditional(&e->arp)) {
380                                 unsigned int oldpos, size;
381
382                                 /* Return: backtrack through the last
383                                  * big jump.
384                                  */
385                                 do {
386                                         e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
387                                         oldpos = pos;
388                                         pos = e->counters.pcnt;
389                                         e->counters.pcnt = 0;
390
391                                         /* We're at the start. */
392                                         if (pos == oldpos)
393                                                 goto next;
394
395                                         e = (struct arpt_entry *)
396                                                 (entry0 + pos);
397                                 } while (oldpos == pos + e->next_offset);
398
399                                 /* Move along one */
400                                 size = e->next_offset;
401                                 e = (struct arpt_entry *)
402                                         (entry0 + pos + size);
403                                 e->counters.pcnt = pos;
404                                 pos += size;
405                         } else {
406                                 int newpos = t->verdict;
407
408                                 if (strcmp(t->target.u.user.name,
409                                            ARPT_STANDARD_TARGET) == 0
410                                     && newpos >= 0) {
411                                         /* This a jump; chase it. */
412                                         duprintf("Jump rule %u -> %u\n",
413                                                  pos, newpos);
414                                 } else {
415                                         /* ... this is a fallthru */
416                                         newpos = pos + e->next_offset;
417                                 }
418                                 e = (struct arpt_entry *)
419                                         (entry0 + newpos);
420                                 e->counters.pcnt = pos;
421                                 pos = newpos;
422                         }
423                 }
424                 next:
425                 duprintf("Finished chain %u\n", hook);
426         }
427         return 1;
428 }
429
430 static inline int standard_check(const struct arpt_entry_target *t,
431                                  unsigned int max_offset)
432 {
433         struct arpt_standard_target *targ = (void *)t;
434
435         /* Check standard info. */
436         if (t->u.target_size
437             != ARPT_ALIGN(sizeof(struct arpt_standard_target))) {
438                 duprintf("arpt_standard_check: target size %u != %Zu\n",
439                          t->u.target_size,
440                          ARPT_ALIGN(sizeof(struct arpt_standard_target)));
441                 return 0;
442         }
443
444         if (targ->verdict >= 0
445             && targ->verdict > max_offset - sizeof(struct arpt_entry)) {
446                 duprintf("arpt_standard_check: bad verdict (%i)\n",
447                          targ->verdict);
448                 return 0;
449         }
450
451         if (targ->verdict < -NF_MAX_VERDICT - 1) {
452                 duprintf("arpt_standard_check: bad negative verdict (%i)\n",
453                          targ->verdict);
454                 return 0;
455         }
456         return 1;
457 }
458
459 static struct arpt_target arpt_standard_target;
460
461 static inline int check_entry(struct arpt_entry *e, const char *name, unsigned int size,
462                               unsigned int *i)
463 {
464         struct arpt_entry_target *t;
465         struct arpt_target *target;
466         int ret;
467
468         if (!arp_checkentry(&e->arp)) {
469                 duprintf("arp_tables: arp check failed %p %s.\n", e, name);
470                 return -EINVAL;
471         }
472
473         t = arpt_get_target(e);
474         target = try_then_request_module(xt_find_target(NF_ARP, t->u.user.name,
475                                                         t->u.user.revision),
476                                          "arpt_%s", t->u.user.name);
477         if (IS_ERR(target) || !target) {
478                 duprintf("check_entry: `%s' not found\n", t->u.user.name);
479                 ret = target ? PTR_ERR(target) : -ENOENT;
480                 goto out;
481         }
482         t->u.kernel.target = target;
483
484         if (t->u.kernel.target == &arpt_standard_target) {
485                 if (!standard_check(t, size)) {
486                         ret = -EINVAL;
487                         goto out;
488                 }
489         } else if (t->u.kernel.target->checkentry
490                    && !t->u.kernel.target->checkentry(name, e, t->data,
491                                                       t->u.target_size
492                                                       - sizeof(*t),
493                                                       e->comefrom)) {
494                 module_put(t->u.kernel.target->me);
495                 duprintf("arp_tables: check failed for `%s'.\n",
496                          t->u.kernel.target->name);
497                 ret = -EINVAL;
498                 goto out;
499         }
500
501         (*i)++;
502         return 0;
503
504 out:
505         return ret;
506 }
507
508 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
509                                              struct xt_table_info *newinfo,
510                                              unsigned char *base,
511                                              unsigned char *limit,
512                                              const unsigned int *hook_entries,
513                                              const unsigned int *underflows,
514                                              unsigned int *i)
515 {
516         unsigned int h;
517
518         if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
519             || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
520                 duprintf("Bad offset %p\n", e);
521                 return -EINVAL;
522         }
523
524         if (e->next_offset
525             < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
526                 duprintf("checking: element %p size %u\n",
527                          e, e->next_offset);
528                 return -EINVAL;
529         }
530
531         /* Check hooks & underflows */
532         for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
533                 if ((unsigned char *)e - base == hook_entries[h])
534                         newinfo->hook_entry[h] = hook_entries[h];
535                 if ((unsigned char *)e - base == underflows[h])
536                         newinfo->underflow[h] = underflows[h];
537         }
538
539         /* FIXME: underflows must be unconditional, standard verdicts
540            < 0 (not ARPT_RETURN). --RR */
541
542         /* Clear counters and comefrom */
543         e->counters = ((struct xt_counters) { 0, 0 });
544         e->comefrom = 0;
545
546         (*i)++;
547         return 0;
548 }
549
550 static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
551 {
552         struct arpt_entry_target *t;
553
554         if (i && (*i)-- == 0)
555                 return 1;
556
557         t = arpt_get_target(e);
558         if (t->u.kernel.target->destroy)
559                 t->u.kernel.target->destroy(t->data,
560                                             t->u.target_size - sizeof(*t));
561         module_put(t->u.kernel.target->me);
562         return 0;
563 }
564
565 /* Checks and translates the user-supplied table segment (held in
566  * newinfo).
567  */
568 static int translate_table(const char *name,
569                            unsigned int valid_hooks,
570                            struct xt_table_info *newinfo,
571                            void *entry0,
572                            unsigned int size,
573                            unsigned int number,
574                            const unsigned int *hook_entries,
575                            const unsigned int *underflows)
576 {
577         unsigned int i;
578         int ret;
579
580         newinfo->size = size;
581         newinfo->number = number;
582
583         /* Init all hooks to impossible value. */
584         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
585                 newinfo->hook_entry[i] = 0xFFFFFFFF;
586                 newinfo->underflow[i] = 0xFFFFFFFF;
587         }
588
589         duprintf("translate_table: size %u\n", newinfo->size);
590         i = 0;
591
592         /* Walk through entries, checking offsets. */
593         ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
594                                  check_entry_size_and_hooks,
595                                  newinfo,
596                                  entry0,
597                                  entry0 + size,
598                                  hook_entries, underflows, &i);
599         duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
600         if (ret != 0)
601                 return ret;
602
603         if (i != number) {
604                 duprintf("translate_table: %u not %u entries\n",
605                          i, number);
606                 return -EINVAL;
607         }
608
609         /* Check hooks all assigned */
610         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
611                 /* Only hooks which are valid */
612                 if (!(valid_hooks & (1 << i)))
613                         continue;
614                 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
615                         duprintf("Invalid hook entry %u %u\n",
616                                  i, hook_entries[i]);
617                         return -EINVAL;
618                 }
619                 if (newinfo->underflow[i] == 0xFFFFFFFF) {
620                         duprintf("Invalid underflow %u %u\n",
621                                  i, underflows[i]);
622                         return -EINVAL;
623                 }
624         }
625
626         if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
627                 duprintf("Looping hook\n");
628                 return -ELOOP;
629         }
630
631         /* Finally, each sanity check must pass */
632         i = 0;
633         ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
634                                  check_entry, name, size, &i);
635
636         if (ret != 0) {
637                 ARPT_ENTRY_ITERATE(entry0, newinfo->size,
638                                    cleanup_entry, &i);
639                 return ret;
640         }
641
642         /* And one copy for every other CPU */
643         for_each_cpu(i) {
644                 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
645                         memcpy(newinfo->entries[i], entry0, newinfo->size);
646         }
647
648         return ret;
649 }
650
651 /* Gets counters. */
652 static inline int add_entry_to_counter(const struct arpt_entry *e,
653                                        struct xt_counters total[],
654                                        unsigned int *i)
655 {
656         ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
657
658         (*i)++;
659         return 0;
660 }
661
662 static inline int set_entry_to_counter(const struct arpt_entry *e,
663                                        struct xt_counters total[],
664                                        unsigned int *i)
665 {
666         SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
667
668         (*i)++;
669         return 0;
670 }
671
672 static void get_counters(const struct xt_table_info *t,
673                          struct xt_counters counters[])
674 {
675         unsigned int cpu;
676         unsigned int i;
677         unsigned int curcpu;
678
679         /* Instead of clearing (by a previous call to memset())
680          * the counters and using adds, we set the counters
681          * with data used by 'current' CPU
682          * We dont care about preemption here.
683          */
684         curcpu = raw_smp_processor_id();
685
686         i = 0;
687         ARPT_ENTRY_ITERATE(t->entries[curcpu],
688                            t->size,
689                            set_entry_to_counter,
690                            counters,
691                            &i);
692
693         for_each_cpu(cpu) {
694                 if (cpu == curcpu)
695                         continue;
696                 i = 0;
697                 ARPT_ENTRY_ITERATE(t->entries[cpu],
698                                    t->size,
699                                    add_entry_to_counter,
700                                    counters,
701                                    &i);
702         }
703 }
704
705 static int copy_entries_to_user(unsigned int total_size,
706                                 struct arpt_table *table,
707                                 void __user *userptr)
708 {
709         unsigned int off, num, countersize;
710         struct arpt_entry *e;
711         struct xt_counters *counters;
712         struct xt_table_info *private = table->private;
713         int ret = 0;
714         void *loc_cpu_entry;
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 -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         loc_cpu_entry = private->entries[raw_smp_processor_id()];
732         /* ... then copy entire thing ... */
733         if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
734                 ret = -EFAULT;
735                 goto free_counters;
736         }
737
738         /* FIXME: use iterator macros --RR */
739         /* ... then go back and fix counters and names */
740         for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
741                 struct arpt_entry_target *t;
742
743                 e = (struct arpt_entry *)(loc_cpu_entry + off);
744                 if (copy_to_user(userptr + off
745                                  + offsetof(struct arpt_entry, counters),
746                                  &counters[num],
747                                  sizeof(counters[num])) != 0) {
748                         ret = -EFAULT;
749                         goto free_counters;
750                 }
751
752                 t = arpt_get_target(e);
753                 if (copy_to_user(userptr + off + e->target_offset
754                                  + offsetof(struct arpt_entry_target,
755                                             u.user.name),
756                                  t->u.kernel.target->name,
757                                  strlen(t->u.kernel.target->name)+1) != 0) {
758                         ret = -EFAULT;
759                         goto free_counters;
760                 }
761         }
762
763  free_counters:
764         vfree(counters);
765         return ret;
766 }
767
768 static int get_entries(const struct arpt_get_entries *entries,
769                        struct arpt_get_entries __user *uptr)
770 {
771         int ret;
772         struct arpt_table *t;
773
774         t = xt_find_table_lock(NF_ARP, entries->name);
775         if (t && !IS_ERR(t)) {
776                 struct xt_table_info *private = t->private;
777                 duprintf("t->private->number = %u\n",
778                          private->number);
779                 if (entries->size == private->size)
780                         ret = copy_entries_to_user(private->size,
781                                                    t, uptr->entrytable);
782                 else {
783                         duprintf("get_entries: I've got %u not %u!\n",
784                                  private->size, entries->size);
785                         ret = -EINVAL;
786                 }
787                 module_put(t->me);
788                 xt_table_unlock(t);
789         } else
790                 ret = t ? PTR_ERR(t) : -ENOENT;
791
792         return ret;
793 }
794
795 static int do_replace(void __user *user, unsigned int len)
796 {
797         int ret;
798         struct arpt_replace tmp;
799         struct arpt_table *t;
800         struct xt_table_info *newinfo, *oldinfo;
801         struct xt_counters *counters;
802         void *loc_cpu_entry, *loc_cpu_old_entry;
803
804         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
805                 return -EFAULT;
806
807         /* Hack: Causes ipchains to give correct error msg --RR */
808         if (len != sizeof(tmp) + tmp.size)
809                 return -ENOPROTOOPT;
810
811         /* overflow check */
812         if (tmp.size >= (INT_MAX - sizeof(struct xt_table_info)) / NR_CPUS -
813                         SMP_CACHE_BYTES)
814                 return -ENOMEM;
815         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
816                 return -ENOMEM;
817
818         newinfo = xt_alloc_table_info(tmp.size);
819         if (!newinfo)
820                 return -ENOMEM;
821
822         /* choose the copy that is on our node/cpu */
823         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
824         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
825                            tmp.size) != 0) {
826                 ret = -EFAULT;
827                 goto free_newinfo;
828         }
829
830         counters = vmalloc(tmp.num_counters * sizeof(struct xt_counters));
831         if (!counters) {
832                 ret = -ENOMEM;
833                 goto free_newinfo;
834         }
835
836         ret = translate_table(tmp.name, tmp.valid_hooks,
837                               newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
838                               tmp.hook_entry, tmp.underflow);
839         if (ret != 0)
840                 goto free_newinfo_counters;
841
842         duprintf("arp_tables: Translated table\n");
843
844         t = try_then_request_module(xt_find_table_lock(NF_ARP, tmp.name),
845                                     "arptable_%s", tmp.name);
846         if (!t || IS_ERR(t)) {
847                 ret = t ? PTR_ERR(t) : -ENOENT;
848                 goto free_newinfo_counters_untrans;
849         }
850
851         /* You lied! */
852         if (tmp.valid_hooks != t->valid_hooks) {
853                 duprintf("Valid hook crap: %08X vs %08X\n",
854                          tmp.valid_hooks, t->valid_hooks);
855                 ret = -EINVAL;
856                 goto put_module;
857         }
858
859         oldinfo = xt_replace_table(t, tmp.num_counters, newinfo, &ret);
860         if (!oldinfo)
861                 goto put_module;
862
863         /* Update module usage count based on number of rules */
864         duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
865                 oldinfo->number, oldinfo->initial_entries, newinfo->number);
866         if ((oldinfo->number > oldinfo->initial_entries) || 
867             (newinfo->number <= oldinfo->initial_entries)) 
868                 module_put(t->me);
869         if ((oldinfo->number > oldinfo->initial_entries) &&
870             (newinfo->number <= oldinfo->initial_entries))
871                 module_put(t->me);
872
873         /* Get the old counters. */
874         get_counters(oldinfo, counters);
875         /* Decrease module usage counts and free resource */
876         loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
877         ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,NULL);
878
879         xt_free_table_info(oldinfo);
880         if (copy_to_user(tmp.counters, counters,
881                          sizeof(struct xt_counters) * tmp.num_counters) != 0)
882                 ret = -EFAULT;
883         vfree(counters);
884         xt_table_unlock(t);
885         return ret;
886
887  put_module:
888         module_put(t->me);
889         xt_table_unlock(t);
890  free_newinfo_counters_untrans:
891         ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
892  free_newinfo_counters:
893         vfree(counters);
894  free_newinfo:
895         xt_free_table_info(newinfo);
896         return ret;
897 }
898
899 /* We're lazy, and add to the first CPU; overflow works its fey magic
900  * and everything is OK.
901  */
902 static inline int add_counter_to_entry(struct arpt_entry *e,
903                                        const struct xt_counters addme[],
904                                        unsigned int *i)
905 {
906
907         ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
908
909         (*i)++;
910         return 0;
911 }
912
913 static int do_add_counters(void __user *user, unsigned int len)
914 {
915         unsigned int i;
916         struct xt_counters_info tmp, *paddc;
917         struct arpt_table *t;
918         struct xt_table_info *private;
919         int ret = 0;
920         void *loc_cpu_entry;
921
922         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
923                 return -EFAULT;
924
925         if (len != sizeof(tmp) + tmp.num_counters*sizeof(struct xt_counters))
926                 return -EINVAL;
927
928         paddc = vmalloc(len);
929         if (!paddc)
930                 return -ENOMEM;
931
932         if (copy_from_user(paddc, user, len) != 0) {
933                 ret = -EFAULT;
934                 goto free;
935         }
936
937         t = xt_find_table_lock(NF_ARP, tmp.name);
938         if (!t || IS_ERR(t)) {
939                 ret = t ? PTR_ERR(t) : -ENOENT;
940                 goto free;
941         }
942
943         write_lock_bh(&t->lock);
944         private = t->private;
945         if (private->number != tmp.num_counters) {
946                 ret = -EINVAL;
947                 goto unlock_up_free;
948         }
949
950         i = 0;
951         /* Choose the copy that is on our node */
952         loc_cpu_entry = private->entries[smp_processor_id()];
953         ARPT_ENTRY_ITERATE(loc_cpu_entry,
954                            private->size,
955                            add_counter_to_entry,
956                            paddc->counters,
957                            &i);
958  unlock_up_free:
959         write_unlock_bh(&t->lock);
960         xt_table_unlock(t);
961         module_put(t->me);
962  free:
963         vfree(paddc);
964
965         return ret;
966 }
967
968 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
969 {
970         int ret;
971
972         if (!capable(CAP_NET_ADMIN))
973                 return -EPERM;
974
975         switch (cmd) {
976         case ARPT_SO_SET_REPLACE:
977                 ret = do_replace(user, len);
978                 break;
979
980         case ARPT_SO_SET_ADD_COUNTERS:
981                 ret = do_add_counters(user, len);
982                 break;
983
984         default:
985                 duprintf("do_arpt_set_ctl:  unknown request %i\n", cmd);
986                 ret = -EINVAL;
987         }
988
989         return ret;
990 }
991
992 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
993 {
994         int ret;
995
996         if (!capable(CAP_NET_ADMIN))
997                 return -EPERM;
998
999         switch (cmd) {
1000         case ARPT_SO_GET_INFO: {
1001                 char name[ARPT_TABLE_MAXNAMELEN];
1002                 struct arpt_table *t;
1003
1004                 if (*len != sizeof(struct arpt_getinfo)) {
1005                         duprintf("length %u != %Zu\n", *len,
1006                                  sizeof(struct arpt_getinfo));
1007                         ret = -EINVAL;
1008                         break;
1009                 }
1010
1011                 if (copy_from_user(name, user, sizeof(name)) != 0) {
1012                         ret = -EFAULT;
1013                         break;
1014                 }
1015                 name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
1016
1017                 t = try_then_request_module(xt_find_table_lock(NF_ARP, name),
1018                                             "arptable_%s", name);
1019                 if (t && !IS_ERR(t)) {
1020                         struct arpt_getinfo info;
1021                         struct xt_table_info *private = t->private;
1022
1023                         info.valid_hooks = t->valid_hooks;
1024                         memcpy(info.hook_entry, private->hook_entry,
1025                                sizeof(info.hook_entry));
1026                         memcpy(info.underflow, private->underflow,
1027                                sizeof(info.underflow));
1028                         info.num_entries = private->number;
1029                         info.size = private->size;
1030                         strcpy(info.name, name);
1031
1032                         if (copy_to_user(user, &info, *len) != 0)
1033                                 ret = -EFAULT;
1034                         else
1035                                 ret = 0;
1036                         xt_table_unlock(t);
1037                         module_put(t->me);
1038                 } else
1039                         ret = t ? PTR_ERR(t) : -ENOENT;
1040         }
1041         break;
1042
1043         case ARPT_SO_GET_ENTRIES: {
1044                 struct arpt_get_entries get;
1045
1046                 if (*len < sizeof(get)) {
1047                         duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
1048                         ret = -EINVAL;
1049                 } else if (copy_from_user(&get, user, sizeof(get)) != 0) {
1050                         ret = -EFAULT;
1051                 } else if (*len != sizeof(struct arpt_get_entries) + get.size) {
1052                         duprintf("get_entries: %u != %Zu\n", *len,
1053                                  sizeof(struct arpt_get_entries) + get.size);
1054                         ret = -EINVAL;
1055                 } else
1056                         ret = get_entries(&get, user);
1057                 break;
1058         }
1059
1060         case ARPT_SO_GET_REVISION_TARGET: {
1061                 struct xt_get_revision rev;
1062
1063                 if (*len != sizeof(rev)) {
1064                         ret = -EINVAL;
1065                         break;
1066                 }
1067                 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1068                         ret = -EFAULT;
1069                         break;
1070                 }
1071
1072                 try_then_request_module(xt_find_revision(NF_ARP, rev.name,
1073                                                          rev.revision, 1, &ret),
1074                                         "arpt_%s", rev.name);
1075                 break;
1076         }
1077
1078         default:
1079                 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1080                 ret = -EINVAL;
1081         }
1082
1083         return ret;
1084 }
1085
1086 int arpt_register_table(struct arpt_table *table,
1087                         const struct arpt_replace *repl)
1088 {
1089         int ret;
1090         struct xt_table_info *newinfo;
1091         static struct xt_table_info bootstrap
1092                 = { 0, 0, 0, { 0 }, { 0 }, { } };
1093         void *loc_cpu_entry;
1094
1095         newinfo = xt_alloc_table_info(repl->size);
1096         if (!newinfo) {
1097                 ret = -ENOMEM;
1098                 return ret;
1099         }
1100
1101         /* choose the copy on our node/cpu */
1102         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1103         memcpy(loc_cpu_entry, repl->entries, repl->size);
1104
1105         ret = translate_table(table->name, table->valid_hooks,
1106                               newinfo, loc_cpu_entry, repl->size,
1107                               repl->num_entries,
1108                               repl->hook_entry,
1109                               repl->underflow);
1110
1111         duprintf("arpt_register_table: translate table gives %d\n", ret);
1112         if (ret != 0) {
1113                 xt_free_table_info(newinfo);
1114                 return ret;
1115         }
1116
1117         if (xt_register_table(table, &bootstrap, newinfo) != 0) {
1118                 xt_free_table_info(newinfo);
1119                 return ret;
1120         }
1121
1122         return 0;
1123 }
1124
1125 void arpt_unregister_table(struct arpt_table *table)
1126 {
1127         struct xt_table_info *private;
1128         void *loc_cpu_entry;
1129
1130         private = xt_unregister_table(table);
1131
1132         /* Decrease module usage counts and free resources */
1133         loc_cpu_entry = private->entries[raw_smp_processor_id()];
1134         ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size,
1135                            cleanup_entry, NULL);
1136         xt_free_table_info(private);
1137 }
1138
1139 /* The built-in targets: standard (NULL) and error. */
1140 static struct arpt_target arpt_standard_target = {
1141         .name           = ARPT_STANDARD_TARGET,
1142 };
1143
1144 static struct arpt_target arpt_error_target = {
1145         .name           = ARPT_ERROR_TARGET,
1146         .target         = arpt_error,
1147 };
1148
1149 static struct nf_sockopt_ops arpt_sockopts = {
1150         .pf             = PF_INET,
1151         .set_optmin     = ARPT_BASE_CTL,
1152         .set_optmax     = ARPT_SO_SET_MAX+1,
1153         .set            = do_arpt_set_ctl,
1154         .get_optmin     = ARPT_BASE_CTL,
1155         .get_optmax     = ARPT_SO_GET_MAX+1,
1156         .get            = do_arpt_get_ctl,
1157 };
1158
1159 static int __init init(void)
1160 {
1161         int ret;
1162
1163         xt_proto_init(NF_ARP);
1164
1165         /* Noone else will be downing sem now, so we won't sleep */
1166         xt_register_target(NF_ARP, &arpt_standard_target);
1167         xt_register_target(NF_ARP, &arpt_error_target);
1168
1169         /* Register setsockopt */
1170         ret = nf_register_sockopt(&arpt_sockopts);
1171         if (ret < 0) {
1172                 duprintf("Unable to register sockopts.\n");
1173                 return ret;
1174         }
1175
1176         printk("arp_tables: (C) 2002 David S. Miller\n");
1177         return 0;
1178 }
1179
1180 static void __exit fini(void)
1181 {
1182         nf_unregister_sockopt(&arpt_sockopts);
1183         xt_proto_fini(NF_ARP);
1184 }
1185
1186 EXPORT_SYMBOL(arpt_register_table);
1187 EXPORT_SYMBOL(arpt_unregister_table);
1188 EXPORT_SYMBOL(arpt_do_table);
1189
1190 module_init(init);
1191 module_exit(fini);