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