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