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