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