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