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