5ba533d234db60c88504af505cdb4eee7a9a1dc2
[linux-2.6.git] / net / ipv4 / netfilter / arp_tables.c
1 /*
2  * Packet matching code for ARP packets.
3  *
4  * Based heavily, if not almost entirely, upon ip_tables.c framework.
5  *
6  * Some ARP specific bits are:
7  *
8  * Copyright (C) 2002 David S. Miller (davem@redhat.com)
9  *
10  */
11
12 #include <linux/kernel.h>
13 #include <linux/skbuff.h>
14 #include <linux/netdevice.h>
15 #include <linux/capability.h>
16 #include <linux/if_arp.h>
17 #include <linux/kmod.h>
18 #include <linux/vmalloc.h>
19 #include <linux/proc_fs.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/mutex.h>
23 #include <linux/err.h>
24 #include <net/compat.h>
25 #include <net/sock.h>
26 #include <asm/uaccess.h>
27
28 #include <linux/netfilter/x_tables.h>
29 #include <linux/netfilter_arp/arp_tables.h>
30
31 MODULE_LICENSE("GPL");
32 MODULE_AUTHOR("David S. Miller <davem@redhat.com>");
33 MODULE_DESCRIPTION("arptables core");
34
35 /*#define DEBUG_ARP_TABLES*/
36 /*#define DEBUG_ARP_TABLES_USER*/
37
38 #ifdef DEBUG_ARP_TABLES
39 #define dprintf(format, args...)  printk(format , ## args)
40 #else
41 #define dprintf(format, args...)
42 #endif
43
44 #ifdef DEBUG_ARP_TABLES_USER
45 #define duprintf(format, args...) printk(format , ## args)
46 #else
47 #define duprintf(format, args...)
48 #endif
49
50 #ifdef CONFIG_NETFILTER_DEBUG
51 #define ARP_NF_ASSERT(x)                                        \
52 do {                                                            \
53         if (!(x))                                               \
54                 printk("ARP_NF_ASSERT: %s:%s:%u\n",             \
55                        __func__, __FILE__, __LINE__);   \
56 } while(0)
57 #else
58 #define ARP_NF_ASSERT(x)
59 #endif
60
61 static inline int arp_devaddr_compare(const struct arpt_devaddr_info *ap,
62                                       const char *hdr_addr, int len)
63 {
64         int i, ret;
65
66         if (len > ARPT_DEV_ADDR_LEN_MAX)
67                 len = ARPT_DEV_ADDR_LEN_MAX;
68
69         ret = 0;
70         for (i = 0; i < len; i++)
71                 ret |= (hdr_addr[i] ^ ap->addr[i]) & ap->mask[i];
72
73         return (ret != 0);
74 }
75
76 /*
77  * Unfortunatly, _b and _mask are not aligned to an int (or long int)
78  * Some arches dont care, unrolling the loop is a win on them.
79  * For other arches, we only have a 16bit alignement.
80  */
81 static unsigned long ifname_compare(const char *_a, const char *_b, const char *_mask)
82 {
83 #ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
84         unsigned long ret = ifname_compare_aligned(_a, _b, _mask);
85 #else
86         unsigned long ret = 0;
87         const u16 *a = (const u16 *)_a;
88         const u16 *b = (const u16 *)_b;
89         const u16 *mask = (const u16 *)_mask;
90         int i;
91
92         for (i = 0; i < IFNAMSIZ/sizeof(u16); i++)
93                 ret |= (a[i] ^ b[i]) & mask[i];
94 #endif
95         return ret;
96 }
97
98 /* Returns whether packet matches rule or not. */
99 static inline int arp_packet_match(const struct arphdr *arphdr,
100                                    struct net_device *dev,
101                                    const char *indev,
102                                    const char *outdev,
103                                    const struct arpt_arp *arpinfo)
104 {
105         const char *arpptr = (char *)(arphdr + 1);
106         const char *src_devaddr, *tgt_devaddr;
107         __be32 src_ipaddr, tgt_ipaddr;
108         long ret;
109
110 #define FWINV(bool, invflg) ((bool) ^ !!(arpinfo->invflags & (invflg)))
111
112         if (FWINV((arphdr->ar_op & arpinfo->arpop_mask) != arpinfo->arpop,
113                   ARPT_INV_ARPOP)) {
114                 dprintf("ARP operation field mismatch.\n");
115                 dprintf("ar_op: %04x info->arpop: %04x info->arpop_mask: %04x\n",
116                         arphdr->ar_op, arpinfo->arpop, arpinfo->arpop_mask);
117                 return 0;
118         }
119
120         if (FWINV((arphdr->ar_hrd & arpinfo->arhrd_mask) != arpinfo->arhrd,
121                   ARPT_INV_ARPHRD)) {
122                 dprintf("ARP hardware address format mismatch.\n");
123                 dprintf("ar_hrd: %04x info->arhrd: %04x info->arhrd_mask: %04x\n",
124                         arphdr->ar_hrd, arpinfo->arhrd, arpinfo->arhrd_mask);
125                 return 0;
126         }
127
128         if (FWINV((arphdr->ar_pro & arpinfo->arpro_mask) != arpinfo->arpro,
129                   ARPT_INV_ARPPRO)) {
130                 dprintf("ARP protocol address format mismatch.\n");
131                 dprintf("ar_pro: %04x info->arpro: %04x info->arpro_mask: %04x\n",
132                         arphdr->ar_pro, arpinfo->arpro, arpinfo->arpro_mask);
133                 return 0;
134         }
135
136         if (FWINV((arphdr->ar_hln & arpinfo->arhln_mask) != arpinfo->arhln,
137                   ARPT_INV_ARPHLN)) {
138                 dprintf("ARP hardware address length mismatch.\n");
139                 dprintf("ar_hln: %02x info->arhln: %02x info->arhln_mask: %02x\n",
140                         arphdr->ar_hln, arpinfo->arhln, arpinfo->arhln_mask);
141                 return 0;
142         }
143
144         src_devaddr = arpptr;
145         arpptr += dev->addr_len;
146         memcpy(&src_ipaddr, arpptr, sizeof(u32));
147         arpptr += sizeof(u32);
148         tgt_devaddr = arpptr;
149         arpptr += dev->addr_len;
150         memcpy(&tgt_ipaddr, arpptr, sizeof(u32));
151
152         if (FWINV(arp_devaddr_compare(&arpinfo->src_devaddr, src_devaddr, dev->addr_len),
153                   ARPT_INV_SRCDEVADDR) ||
154             FWINV(arp_devaddr_compare(&arpinfo->tgt_devaddr, tgt_devaddr, dev->addr_len),
155                   ARPT_INV_TGTDEVADDR)) {
156                 dprintf("Source or target device address mismatch.\n");
157
158                 return 0;
159         }
160
161         if (FWINV((src_ipaddr & arpinfo->smsk.s_addr) != arpinfo->src.s_addr,
162                   ARPT_INV_SRCIP) ||
163             FWINV(((tgt_ipaddr & arpinfo->tmsk.s_addr) != arpinfo->tgt.s_addr),
164                   ARPT_INV_TGTIP)) {
165                 dprintf("Source or target IP address mismatch.\n");
166
167                 dprintf("SRC: %pI4. Mask: %pI4. Target: %pI4.%s\n",
168                         &src_ipaddr,
169                         &arpinfo->smsk.s_addr,
170                         &arpinfo->src.s_addr,
171                         arpinfo->invflags & ARPT_INV_SRCIP ? " (INV)" : "");
172                 dprintf("TGT: %pI4 Mask: %pI4 Target: %pI4.%s\n",
173                         &tgt_ipaddr,
174                         &arpinfo->tmsk.s_addr,
175                         &arpinfo->tgt.s_addr,
176                         arpinfo->invflags & ARPT_INV_TGTIP ? " (INV)" : "");
177                 return 0;
178         }
179
180         /* Look for ifname matches.  */
181         ret = ifname_compare(indev, arpinfo->iniface, arpinfo->iniface_mask);
182
183         if (FWINV(ret != 0, ARPT_INV_VIA_IN)) {
184                 dprintf("VIA in mismatch (%s vs %s).%s\n",
185                         indev, arpinfo->iniface,
186                         arpinfo->invflags&ARPT_INV_VIA_IN ?" (INV)":"");
187                 return 0;
188         }
189
190         ret = ifname_compare(outdev, arpinfo->outiface, arpinfo->outiface_mask);
191
192         if (FWINV(ret != 0, ARPT_INV_VIA_OUT)) {
193                 dprintf("VIA out mismatch (%s vs %s).%s\n",
194                         outdev, arpinfo->outiface,
195                         arpinfo->invflags&ARPT_INV_VIA_OUT ?" (INV)":"");
196                 return 0;
197         }
198
199         return 1;
200 #undef FWINV
201 }
202
203 static inline int arp_checkentry(const struct arpt_arp *arp)
204 {
205         if (arp->flags & ~ARPT_F_MASK) {
206                 duprintf("Unknown flag bits set: %08X\n",
207                          arp->flags & ~ARPT_F_MASK);
208                 return 0;
209         }
210         if (arp->invflags & ~ARPT_INV_MASK) {
211                 duprintf("Unknown invflag bits set: %08X\n",
212                          arp->invflags & ~ARPT_INV_MASK);
213                 return 0;
214         }
215
216         return 1;
217 }
218
219 static unsigned int
220 arpt_error(struct sk_buff *skb, const struct xt_target_param *par)
221 {
222         if (net_ratelimit())
223                 printk("arp_tables: error: '%s'\n",
224                        (const char *)par->targinfo);
225
226         return NF_DROP;
227 }
228
229 static inline struct arpt_entry *get_entry(void *base, unsigned int offset)
230 {
231         return (struct arpt_entry *)(base + offset);
232 }
233
234 unsigned int arpt_do_table(struct sk_buff *skb,
235                            unsigned int hook,
236                            const struct net_device *in,
237                            const struct net_device *out,
238                            struct xt_table *table)
239 {
240         static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
241         unsigned int verdict = NF_DROP;
242         const struct arphdr *arp;
243         bool hotdrop = false;
244         struct arpt_entry *e, *back;
245         const char *indev, *outdev;
246         void *table_base;
247         const struct xt_table_info *private;
248         struct xt_target_param tgpar;
249
250         if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
251                 return NF_DROP;
252
253         indev = in ? in->name : nulldevname;
254         outdev = out ? out->name : nulldevname;
255
256         rcu_read_lock_bh();
257         private = rcu_dereference(table->private);
258         table_base = rcu_dereference(private->entries[smp_processor_id()]);
259
260         e = get_entry(table_base, private->hook_entry[hook]);
261         back = get_entry(table_base, private->underflow[hook]);
262
263         tgpar.in      = in;
264         tgpar.out     = out;
265         tgpar.hooknum = hook;
266         tgpar.family  = NFPROTO_ARP;
267
268         arp = arp_hdr(skb);
269         do {
270                 if (arp_packet_match(arp, skb->dev, indev, outdev, &e->arp)) {
271                         struct arpt_entry_target *t;
272                         int hdr_len;
273
274                         hdr_len = sizeof(*arp) + (2 * sizeof(struct in_addr)) +
275                                 (2 * skb->dev->addr_len);
276                         ADD_COUNTER(e->counters, hdr_len, 1);
277
278                         t = arpt_get_target(e);
279
280                         /* Standard target? */
281                         if (!t->u.kernel.target->target) {
282                                 int v;
283
284                                 v = ((struct arpt_standard_target *)t)->verdict;
285                                 if (v < 0) {
286                                         /* Pop from stack? */
287                                         if (v != ARPT_RETURN) {
288                                                 verdict = (unsigned)(-v) - 1;
289                                                 break;
290                                         }
291                                         e = back;
292                                         back = get_entry(table_base,
293                                                          back->comefrom);
294                                         continue;
295                                 }
296                                 if (table_base + v
297                                     != (void *)e + e->next_offset) {
298                                         /* Save old back ptr in next entry */
299                                         struct arpt_entry *next
300                                                 = (void *)e + e->next_offset;
301                                         next->comefrom =
302                                                 (void *)back - table_base;
303
304                                         /* set back pointer to next entry */
305                                         back = next;
306                                 }
307
308                                 e = get_entry(table_base, v);
309                         } else {
310                                 /* Targets which reenter must return
311                                  * abs. verdicts
312                                  */
313                                 tgpar.target   = t->u.kernel.target;
314                                 tgpar.targinfo = t->data;
315                                 verdict = t->u.kernel.target->target(skb,
316                                                                      &tgpar);
317
318                                 /* Target might have changed stuff. */
319                                 arp = arp_hdr(skb);
320
321                                 if (verdict == ARPT_CONTINUE)
322                                         e = (void *)e + e->next_offset;
323                                 else
324                                         /* Verdict */
325                                         break;
326                         }
327                 } else {
328                         e = (void *)e + e->next_offset;
329                 }
330         } while (!hotdrop);
331
332         rcu_read_unlock_bh();
333
334         if (hotdrop)
335                 return NF_DROP;
336         else
337                 return verdict;
338 }
339
340 /* All zeroes == unconditional rule. */
341 static inline int unconditional(const struct arpt_arp *arp)
342 {
343         unsigned int i;
344
345         for (i = 0; i < sizeof(*arp)/sizeof(__u32); i++)
346                 if (((__u32 *)arp)[i])
347                         return 0;
348
349         return 1;
350 }
351
352 /* Figures out from what hook each rule can be called: returns 0 if
353  * there are loops.  Puts hook bitmask in comefrom.
354  */
355 static int mark_source_chains(struct xt_table_info *newinfo,
356                               unsigned int valid_hooks, void *entry0)
357 {
358         unsigned int hook;
359
360         /* No recursion; use packet counter to save back ptrs (reset
361          * to 0 as we leave), and comefrom to save source hook bitmask.
362          */
363         for (hook = 0; hook < NF_ARP_NUMHOOKS; hook++) {
364                 unsigned int pos = newinfo->hook_entry[hook];
365                 struct arpt_entry *e
366                         = (struct arpt_entry *)(entry0 + pos);
367
368                 if (!(valid_hooks & (1 << hook)))
369                         continue;
370
371                 /* Set initial back pointer. */
372                 e->counters.pcnt = pos;
373
374                 for (;;) {
375                         const struct arpt_standard_target *t
376                                 = (void *)arpt_get_target(e);
377                         int visited = e->comefrom & (1 << hook);
378
379                         if (e->comefrom & (1 << NF_ARP_NUMHOOKS)) {
380                                 printk("arptables: loop hook %u pos %u %08X.\n",
381                                        hook, pos, e->comefrom);
382                                 return 0;
383                         }
384                         e->comefrom
385                                 |= ((1 << hook) | (1 << NF_ARP_NUMHOOKS));
386
387                         /* Unconditional return/END. */
388                         if ((e->target_offset == sizeof(struct arpt_entry)
389                             && (strcmp(t->target.u.user.name,
390                                        ARPT_STANDARD_TARGET) == 0)
391                             && t->verdict < 0
392                             && unconditional(&e->arp)) || visited) {
393                                 unsigned int oldpos, size;
394
395                                 if ((strcmp(t->target.u.user.name,
396                                             ARPT_STANDARD_TARGET) == 0) &&
397                                     t->verdict < -NF_MAX_VERDICT - 1) {
398                                         duprintf("mark_source_chains: bad "
399                                                 "negative verdict (%i)\n",
400                                                                 t->verdict);
401                                         return 0;
402                                 }
403
404                                 /* Return: backtrack through the last
405                                  * big jump.
406                                  */
407                                 do {
408                                         e->comefrom ^= (1<<NF_ARP_NUMHOOKS);
409                                         oldpos = pos;
410                                         pos = e->counters.pcnt;
411                                         e->counters.pcnt = 0;
412
413                                         /* We're at the start. */
414                                         if (pos == oldpos)
415                                                 goto next;
416
417                                         e = (struct arpt_entry *)
418                                                 (entry0 + pos);
419                                 } while (oldpos == pos + e->next_offset);
420
421                                 /* Move along one */
422                                 size = e->next_offset;
423                                 e = (struct arpt_entry *)
424                                         (entry0 + pos + size);
425                                 e->counters.pcnt = pos;
426                                 pos += size;
427                         } else {
428                                 int newpos = t->verdict;
429
430                                 if (strcmp(t->target.u.user.name,
431                                            ARPT_STANDARD_TARGET) == 0
432                                     && newpos >= 0) {
433                                         if (newpos > newinfo->size -
434                                                 sizeof(struct arpt_entry)) {
435                                                 duprintf("mark_source_chains: "
436                                                         "bad verdict (%i)\n",
437                                                                 newpos);
438                                                 return 0;
439                                         }
440
441                                         /* This a jump; chase it. */
442                                         duprintf("Jump rule %u -> %u\n",
443                                                  pos, newpos);
444                                 } else {
445                                         /* ... this is a fallthru */
446                                         newpos = pos + e->next_offset;
447                                 }
448                                 e = (struct arpt_entry *)
449                                         (entry0 + newpos);
450                                 e->counters.pcnt = pos;
451                                 pos = newpos;
452                         }
453                 }
454                 next:
455                 duprintf("Finished chain %u\n", hook);
456         }
457         return 1;
458 }
459
460 static inline int check_entry(struct arpt_entry *e, const char *name)
461 {
462         const struct arpt_entry_target *t;
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         if (e->target_offset + sizeof(struct arpt_entry_target) > e->next_offset)
470                 return -EINVAL;
471
472         t = arpt_get_target(e);
473         if (e->target_offset + t->u.target_size > e->next_offset)
474                 return -EINVAL;
475
476         return 0;
477 }
478
479 static inline int check_target(struct arpt_entry *e, const char *name)
480 {
481         struct arpt_entry_target *t = arpt_get_target(e);
482         int ret;
483         struct xt_tgchk_param par = {
484                 .table     = name,
485                 .entryinfo = e,
486                 .target    = t->u.kernel.target,
487                 .targinfo  = t->data,
488                 .hook_mask = e->comefrom,
489                 .family    = NFPROTO_ARP,
490         };
491
492         ret = xt_check_target(&par, t->u.target_size - sizeof(*t), 0, false);
493         if (ret < 0) {
494                 duprintf("arp_tables: check failed for `%s'.\n",
495                          t->u.kernel.target->name);
496                 return ret;
497         }
498         return 0;
499 }
500
501 static inline int
502 find_check_entry(struct arpt_entry *e, const char *name, unsigned int size,
503                  unsigned int *i)
504 {
505         struct arpt_entry_target *t;
506         struct xt_target *target;
507         int ret;
508
509         ret = check_entry(e, name);
510         if (ret)
511                 return ret;
512
513         t = arpt_get_target(e);
514         target = try_then_request_module(xt_find_target(NFPROTO_ARP,
515                                                         t->u.user.name,
516                                                         t->u.user.revision),
517                                          "arpt_%s", t->u.user.name);
518         if (IS_ERR(target) || !target) {
519                 duprintf("find_check_entry: `%s' not found\n", t->u.user.name);
520                 ret = target ? PTR_ERR(target) : -ENOENT;
521                 goto out;
522         }
523         t->u.kernel.target = target;
524
525         ret = check_target(e, name);
526         if (ret)
527                 goto err;
528
529         (*i)++;
530         return 0;
531 err:
532         module_put(t->u.kernel.target->me);
533 out:
534         return ret;
535 }
536
537 static inline int check_entry_size_and_hooks(struct arpt_entry *e,
538                                              struct xt_table_info *newinfo,
539                                              unsigned char *base,
540                                              unsigned char *limit,
541                                              const unsigned int *hook_entries,
542                                              const unsigned int *underflows,
543                                              unsigned int *i)
544 {
545         unsigned int h;
546
547         if ((unsigned long)e % __alignof__(struct arpt_entry) != 0
548             || (unsigned char *)e + sizeof(struct arpt_entry) >= limit) {
549                 duprintf("Bad offset %p\n", e);
550                 return -EINVAL;
551         }
552
553         if (e->next_offset
554             < sizeof(struct arpt_entry) + sizeof(struct arpt_entry_target)) {
555                 duprintf("checking: element %p size %u\n",
556                          e, e->next_offset);
557                 return -EINVAL;
558         }
559
560         /* Check hooks & underflows */
561         for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
562                 if ((unsigned char *)e - base == hook_entries[h])
563                         newinfo->hook_entry[h] = hook_entries[h];
564                 if ((unsigned char *)e - base == underflows[h])
565                         newinfo->underflow[h] = underflows[h];
566         }
567
568         /* FIXME: underflows must be unconditional, standard verdicts
569            < 0 (not ARPT_RETURN). --RR */
570
571         /* Clear counters and comefrom */
572         e->counters = ((struct xt_counters) { 0, 0 });
573         e->comefrom = 0;
574
575         (*i)++;
576         return 0;
577 }
578
579 static inline int cleanup_entry(struct arpt_entry *e, unsigned int *i)
580 {
581         struct xt_tgdtor_param par;
582         struct arpt_entry_target *t;
583
584         if (i && (*i)-- == 0)
585                 return 1;
586
587         t = arpt_get_target(e);
588         par.target   = t->u.kernel.target;
589         par.targinfo = t->data;
590         par.family   = NFPROTO_ARP;
591         if (par.target->destroy != NULL)
592                 par.target->destroy(&par);
593         module_put(par.target->me);
594         return 0;
595 }
596
597 /* Checks and translates the user-supplied table segment (held in
598  * newinfo).
599  */
600 static int translate_table(const char *name,
601                            unsigned int valid_hooks,
602                            struct xt_table_info *newinfo,
603                            void *entry0,
604                            unsigned int size,
605                            unsigned int number,
606                            const unsigned int *hook_entries,
607                            const unsigned int *underflows)
608 {
609         unsigned int i;
610         int ret;
611
612         newinfo->size = size;
613         newinfo->number = number;
614
615         /* Init all hooks to impossible value. */
616         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
617                 newinfo->hook_entry[i] = 0xFFFFFFFF;
618                 newinfo->underflow[i] = 0xFFFFFFFF;
619         }
620
621         duprintf("translate_table: size %u\n", newinfo->size);
622         i = 0;
623
624         /* Walk through entries, checking offsets. */
625         ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
626                                  check_entry_size_and_hooks,
627                                  newinfo,
628                                  entry0,
629                                  entry0 + size,
630                                  hook_entries, underflows, &i);
631         duprintf("translate_table: ARPT_ENTRY_ITERATE gives %d\n", ret);
632         if (ret != 0)
633                 return ret;
634
635         if (i != number) {
636                 duprintf("translate_table: %u not %u entries\n",
637                          i, number);
638                 return -EINVAL;
639         }
640
641         /* Check hooks all assigned */
642         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
643                 /* Only hooks which are valid */
644                 if (!(valid_hooks & (1 << i)))
645                         continue;
646                 if (newinfo->hook_entry[i] == 0xFFFFFFFF) {
647                         duprintf("Invalid hook entry %u %u\n",
648                                  i, hook_entries[i]);
649                         return -EINVAL;
650                 }
651                 if (newinfo->underflow[i] == 0xFFFFFFFF) {
652                         duprintf("Invalid underflow %u %u\n",
653                                  i, underflows[i]);
654                         return -EINVAL;
655                 }
656         }
657
658         if (!mark_source_chains(newinfo, valid_hooks, entry0)) {
659                 duprintf("Looping hook\n");
660                 return -ELOOP;
661         }
662
663         /* Finally, each sanity check must pass */
664         i = 0;
665         ret = ARPT_ENTRY_ITERATE(entry0, newinfo->size,
666                                  find_check_entry, name, size, &i);
667
668         if (ret != 0) {
669                 ARPT_ENTRY_ITERATE(entry0, newinfo->size,
670                                 cleanup_entry, &i);
671                 return ret;
672         }
673
674         /* And one copy for every other CPU */
675         for_each_possible_cpu(i) {
676                 if (newinfo->entries[i] && newinfo->entries[i] != entry0)
677                         memcpy(newinfo->entries[i], entry0, newinfo->size);
678         }
679
680         return ret;
681 }
682
683 /* Gets counters. */
684 static inline int add_entry_to_counter(const struct arpt_entry *e,
685                                        struct xt_counters total[],
686                                        unsigned int *i)
687 {
688         ADD_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
689
690         (*i)++;
691         return 0;
692 }
693
694 static inline int set_entry_to_counter(const struct arpt_entry *e,
695                                        struct xt_counters total[],
696                                        unsigned int *i)
697 {
698         SET_COUNTER(total[*i], e->counters.bcnt, e->counters.pcnt);
699
700         (*i)++;
701         return 0;
702 }
703
704 static void get_counters(const struct xt_table_info *t,
705                          struct xt_counters counters[])
706 {
707         unsigned int cpu;
708         unsigned int i;
709         unsigned int curcpu;
710
711         /* Instead of clearing (by a previous call to memset())
712          * the counters and using adds, we set the counters
713          * with data used by 'current' CPU
714          * We dont care about preemption here.
715          */
716         curcpu = raw_smp_processor_id();
717
718         i = 0;
719         ARPT_ENTRY_ITERATE(t->entries[curcpu],
720                            t->size,
721                            set_entry_to_counter,
722                            counters,
723                            &i);
724
725         for_each_possible_cpu(cpu) {
726                 if (cpu == curcpu)
727                         continue;
728                 i = 0;
729                 ARPT_ENTRY_ITERATE(t->entries[cpu],
730                                    t->size,
731                                    add_entry_to_counter,
732                                    counters,
733                                    &i);
734         }
735 }
736
737
738 /* We're lazy, and add to the first CPU; overflow works its fey magic
739  * and everything is OK. */
740 static int
741 add_counter_to_entry(struct arpt_entry *e,
742                      const struct xt_counters addme[],
743                      unsigned int *i)
744 {
745         ADD_COUNTER(e->counters, addme[*i].bcnt, addme[*i].pcnt);
746
747         (*i)++;
748         return 0;
749 }
750
751 /* Take values from counters and add them back onto the current cpu */
752 static void put_counters(struct xt_table_info *t,
753                          const struct xt_counters counters[])
754 {
755         unsigned int i, cpu;
756
757         local_bh_disable();
758         cpu = smp_processor_id();
759         i = 0;
760         ARPT_ENTRY_ITERATE(t->entries[cpu],
761                           t->size,
762                           add_counter_to_entry,
763                           counters,
764                           &i);
765         local_bh_enable();
766 }
767
768 static inline int
769 zero_entry_counter(struct arpt_entry *e, void *arg)
770 {
771         e->counters.bcnt = 0;
772         e->counters.pcnt = 0;
773         return 0;
774 }
775
776 static void
777 clone_counters(struct xt_table_info *newinfo, const struct xt_table_info *info)
778 {
779         unsigned int cpu;
780         const void *loc_cpu_entry = info->entries[raw_smp_processor_id()];
781
782         memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
783         for_each_possible_cpu(cpu) {
784                 memcpy(newinfo->entries[cpu], loc_cpu_entry, info->size);
785                 ARPT_ENTRY_ITERATE(newinfo->entries[cpu], newinfo->size,
786                                   zero_entry_counter, NULL);
787         }
788 }
789
790 static struct xt_counters *alloc_counters(struct xt_table *table)
791 {
792         unsigned int countersize;
793         struct xt_counters *counters;
794         struct xt_table_info *private = table->private;
795         struct xt_table_info *info;
796
797         /* We need atomic snapshot of counters: rest doesn't change
798          * (other than comefrom, which userspace doesn't care
799          * about).
800          */
801         countersize = sizeof(struct xt_counters) * private->number;
802         counters = vmalloc_node(countersize, numa_node_id());
803
804         if (counters == NULL)
805                 goto nomem;
806
807         info = xt_alloc_table_info(private->size);
808         if (!info)
809                 goto free_counters;
810
811         clone_counters(info, private);
812
813         mutex_lock(&table->lock);
814         xt_table_entry_swap_rcu(private, info);
815         synchronize_net();      /* Wait until smoke has cleared */
816
817         get_counters(info, counters);
818         put_counters(private, counters);
819         mutex_unlock(&table->lock);
820
821         xt_free_table_info(info);
822
823         return counters;
824
825  free_counters:
826         vfree(counters);
827  nomem:
828         return ERR_PTR(-ENOMEM);
829 }
830
831 static int copy_entries_to_user(unsigned int total_size,
832                                 struct xt_table *table,
833                                 void __user *userptr)
834 {
835         unsigned int off, num;
836         struct arpt_entry *e;
837         struct xt_counters *counters;
838         struct xt_table_info *private = table->private;
839         int ret = 0;
840         void *loc_cpu_entry;
841
842         counters = alloc_counters(table);
843         if (IS_ERR(counters))
844                 return PTR_ERR(counters);
845
846         loc_cpu_entry = private->entries[raw_smp_processor_id()];
847         /* ... then copy entire thing ... */
848         if (copy_to_user(userptr, loc_cpu_entry, total_size) != 0) {
849                 ret = -EFAULT;
850                 goto free_counters;
851         }
852
853         /* FIXME: use iterator macros --RR */
854         /* ... then go back and fix counters and names */
855         for (off = 0, num = 0; off < total_size; off += e->next_offset, num++){
856                 struct arpt_entry_target *t;
857
858                 e = (struct arpt_entry *)(loc_cpu_entry + off);
859                 if (copy_to_user(userptr + off
860                                  + offsetof(struct arpt_entry, counters),
861                                  &counters[num],
862                                  sizeof(counters[num])) != 0) {
863                         ret = -EFAULT;
864                         goto free_counters;
865                 }
866
867                 t = arpt_get_target(e);
868                 if (copy_to_user(userptr + off + e->target_offset
869                                  + offsetof(struct arpt_entry_target,
870                                             u.user.name),
871                                  t->u.kernel.target->name,
872                                  strlen(t->u.kernel.target->name)+1) != 0) {
873                         ret = -EFAULT;
874                         goto free_counters;
875                 }
876         }
877
878  free_counters:
879         vfree(counters);
880         return ret;
881 }
882
883 #ifdef CONFIG_COMPAT
884 static void compat_standard_from_user(void *dst, void *src)
885 {
886         int v = *(compat_int_t *)src;
887
888         if (v > 0)
889                 v += xt_compat_calc_jump(NFPROTO_ARP, v);
890         memcpy(dst, &v, sizeof(v));
891 }
892
893 static int compat_standard_to_user(void __user *dst, void *src)
894 {
895         compat_int_t cv = *(int *)src;
896
897         if (cv > 0)
898                 cv -= xt_compat_calc_jump(NFPROTO_ARP, cv);
899         return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
900 }
901
902 static int compat_calc_entry(struct arpt_entry *e,
903                              const struct xt_table_info *info,
904                              void *base, struct xt_table_info *newinfo)
905 {
906         struct arpt_entry_target *t;
907         unsigned int entry_offset;
908         int off, i, ret;
909
910         off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
911         entry_offset = (void *)e - base;
912
913         t = arpt_get_target(e);
914         off += xt_compat_target_offset(t->u.kernel.target);
915         newinfo->size -= off;
916         ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
917         if (ret)
918                 return ret;
919
920         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
921                 if (info->hook_entry[i] &&
922                     (e < (struct arpt_entry *)(base + info->hook_entry[i])))
923                         newinfo->hook_entry[i] -= off;
924                 if (info->underflow[i] &&
925                     (e < (struct arpt_entry *)(base + info->underflow[i])))
926                         newinfo->underflow[i] -= off;
927         }
928         return 0;
929 }
930
931 static int compat_table_info(const struct xt_table_info *info,
932                              struct xt_table_info *newinfo)
933 {
934         void *loc_cpu_entry;
935
936         if (!newinfo || !info)
937                 return -EINVAL;
938
939         /* we dont care about newinfo->entries[] */
940         memcpy(newinfo, info, offsetof(struct xt_table_info, entries));
941         newinfo->initial_entries = 0;
942         loc_cpu_entry = info->entries[raw_smp_processor_id()];
943         return ARPT_ENTRY_ITERATE(loc_cpu_entry, info->size,
944                                   compat_calc_entry, info, loc_cpu_entry,
945                                   newinfo);
946 }
947 #endif
948
949 static int get_info(struct net *net, void __user *user, int *len, int compat)
950 {
951         char name[ARPT_TABLE_MAXNAMELEN];
952         struct xt_table *t;
953         int ret;
954
955         if (*len != sizeof(struct arpt_getinfo)) {
956                 duprintf("length %u != %Zu\n", *len,
957                          sizeof(struct arpt_getinfo));
958                 return -EINVAL;
959         }
960
961         if (copy_from_user(name, user, sizeof(name)) != 0)
962                 return -EFAULT;
963
964         name[ARPT_TABLE_MAXNAMELEN-1] = '\0';
965 #ifdef CONFIG_COMPAT
966         if (compat)
967                 xt_compat_lock(NFPROTO_ARP);
968 #endif
969         t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
970                                     "arptable_%s", name);
971         if (t && !IS_ERR(t)) {
972                 struct arpt_getinfo info;
973                 const struct xt_table_info *private = t->private;
974
975 #ifdef CONFIG_COMPAT
976                 if (compat) {
977                         struct xt_table_info tmp;
978                         ret = compat_table_info(private, &tmp);
979                         xt_compat_flush_offsets(NFPROTO_ARP);
980                         private = &tmp;
981                 }
982 #endif
983                 info.valid_hooks = t->valid_hooks;
984                 memcpy(info.hook_entry, private->hook_entry,
985                        sizeof(info.hook_entry));
986                 memcpy(info.underflow, private->underflow,
987                        sizeof(info.underflow));
988                 info.num_entries = private->number;
989                 info.size = private->size;
990                 strcpy(info.name, name);
991
992                 if (copy_to_user(user, &info, *len) != 0)
993                         ret = -EFAULT;
994                 else
995                         ret = 0;
996                 xt_table_unlock(t);
997                 module_put(t->me);
998         } else
999                 ret = t ? PTR_ERR(t) : -ENOENT;
1000 #ifdef CONFIG_COMPAT
1001         if (compat)
1002                 xt_compat_unlock(NFPROTO_ARP);
1003 #endif
1004         return ret;
1005 }
1006
1007 static int get_entries(struct net *net, struct arpt_get_entries __user *uptr,
1008                        int *len)
1009 {
1010         int ret;
1011         struct arpt_get_entries get;
1012         struct xt_table *t;
1013
1014         if (*len < sizeof(get)) {
1015                 duprintf("get_entries: %u < %Zu\n", *len, sizeof(get));
1016                 return -EINVAL;
1017         }
1018         if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1019                 return -EFAULT;
1020         if (*len != sizeof(struct arpt_get_entries) + get.size) {
1021                 duprintf("get_entries: %u != %Zu\n", *len,
1022                          sizeof(struct arpt_get_entries) + get.size);
1023                 return -EINVAL;
1024         }
1025
1026         t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1027         if (t && !IS_ERR(t)) {
1028                 const struct xt_table_info *private = t->private;
1029
1030                 duprintf("t->private->number = %u\n",
1031                          private->number);
1032                 if (get.size == private->size)
1033                         ret = copy_entries_to_user(private->size,
1034                                                    t, uptr->entrytable);
1035                 else {
1036                         duprintf("get_entries: I've got %u not %u!\n",
1037                                  private->size, get.size);
1038                         ret = -EAGAIN;
1039                 }
1040                 module_put(t->me);
1041                 xt_table_unlock(t);
1042         } else
1043                 ret = t ? PTR_ERR(t) : -ENOENT;
1044
1045         return ret;
1046 }
1047
1048 static int __do_replace(struct net *net, const char *name,
1049                         unsigned int valid_hooks,
1050                         struct xt_table_info *newinfo,
1051                         unsigned int num_counters,
1052                         void __user *counters_ptr)
1053 {
1054         int ret;
1055         struct xt_table *t;
1056         struct xt_table_info *oldinfo;
1057         struct xt_counters *counters;
1058         void *loc_cpu_old_entry;
1059
1060         ret = 0;
1061         counters = vmalloc_node(num_counters * sizeof(struct xt_counters),
1062                                 numa_node_id());
1063         if (!counters) {
1064                 ret = -ENOMEM;
1065                 goto out;
1066         }
1067
1068         t = try_then_request_module(xt_find_table_lock(net, NFPROTO_ARP, name),
1069                                     "arptable_%s", name);
1070         if (!t || IS_ERR(t)) {
1071                 ret = t ? PTR_ERR(t) : -ENOENT;
1072                 goto free_newinfo_counters_untrans;
1073         }
1074
1075         /* You lied! */
1076         if (valid_hooks != t->valid_hooks) {
1077                 duprintf("Valid hook crap: %08X vs %08X\n",
1078                          valid_hooks, t->valid_hooks);
1079                 ret = -EINVAL;
1080                 goto put_module;
1081         }
1082
1083         oldinfo = xt_replace_table(t, num_counters, newinfo, &ret);
1084         if (!oldinfo)
1085                 goto put_module;
1086
1087         /* Update module usage count based on number of rules */
1088         duprintf("do_replace: oldnum=%u, initnum=%u, newnum=%u\n",
1089                 oldinfo->number, oldinfo->initial_entries, newinfo->number);
1090         if ((oldinfo->number > oldinfo->initial_entries) ||
1091             (newinfo->number <= oldinfo->initial_entries))
1092                 module_put(t->me);
1093         if ((oldinfo->number > oldinfo->initial_entries) &&
1094             (newinfo->number <= oldinfo->initial_entries))
1095                 module_put(t->me);
1096
1097         /* Get the old counters. */
1098         get_counters(oldinfo, counters);
1099         /* Decrease module usage counts and free resource */
1100         loc_cpu_old_entry = oldinfo->entries[raw_smp_processor_id()];
1101         ARPT_ENTRY_ITERATE(loc_cpu_old_entry, oldinfo->size, cleanup_entry,
1102                            NULL);
1103
1104         xt_free_table_info(oldinfo);
1105         if (copy_to_user(counters_ptr, counters,
1106                          sizeof(struct xt_counters) * num_counters) != 0)
1107                 ret = -EFAULT;
1108         vfree(counters);
1109         xt_table_unlock(t);
1110         return ret;
1111
1112  put_module:
1113         module_put(t->me);
1114         xt_table_unlock(t);
1115  free_newinfo_counters_untrans:
1116         vfree(counters);
1117  out:
1118         return ret;
1119 }
1120
1121 static int do_replace(struct net *net, void __user *user, unsigned int len)
1122 {
1123         int ret;
1124         struct arpt_replace tmp;
1125         struct xt_table_info *newinfo;
1126         void *loc_cpu_entry;
1127
1128         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1129                 return -EFAULT;
1130
1131         /* overflow check */
1132         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1133                 return -ENOMEM;
1134
1135         newinfo = xt_alloc_table_info(tmp.size);
1136         if (!newinfo)
1137                 return -ENOMEM;
1138
1139         /* choose the copy that is on our node/cpu */
1140         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1141         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp),
1142                            tmp.size) != 0) {
1143                 ret = -EFAULT;
1144                 goto free_newinfo;
1145         }
1146
1147         ret = translate_table(tmp.name, tmp.valid_hooks,
1148                               newinfo, loc_cpu_entry, tmp.size, tmp.num_entries,
1149                               tmp.hook_entry, tmp.underflow);
1150         if (ret != 0)
1151                 goto free_newinfo;
1152
1153         duprintf("arp_tables: Translated table\n");
1154
1155         ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1156                            tmp.num_counters, tmp.counters);
1157         if (ret)
1158                 goto free_newinfo_untrans;
1159         return 0;
1160
1161  free_newinfo_untrans:
1162         ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1163  free_newinfo:
1164         xt_free_table_info(newinfo);
1165         return ret;
1166 }
1167
1168 static int do_add_counters(struct net *net, void __user *user, unsigned int len,
1169                            int compat)
1170 {
1171         unsigned int i;
1172         struct xt_counters_info tmp;
1173         struct xt_counters *paddc;
1174         unsigned int num_counters;
1175         const char *name;
1176         int size;
1177         void *ptmp;
1178         struct xt_table *t;
1179         const struct xt_table_info *private;
1180         int ret = 0;
1181         void *loc_cpu_entry;
1182 #ifdef CONFIG_COMPAT
1183         struct compat_xt_counters_info compat_tmp;
1184
1185         if (compat) {
1186                 ptmp = &compat_tmp;
1187                 size = sizeof(struct compat_xt_counters_info);
1188         } else
1189 #endif
1190         {
1191                 ptmp = &tmp;
1192                 size = sizeof(struct xt_counters_info);
1193         }
1194
1195         if (copy_from_user(ptmp, user, size) != 0)
1196                 return -EFAULT;
1197
1198 #ifdef CONFIG_COMPAT
1199         if (compat) {
1200                 num_counters = compat_tmp.num_counters;
1201                 name = compat_tmp.name;
1202         } else
1203 #endif
1204         {
1205                 num_counters = tmp.num_counters;
1206                 name = tmp.name;
1207         }
1208
1209         if (len != size + num_counters * sizeof(struct xt_counters))
1210                 return -EINVAL;
1211
1212         paddc = vmalloc_node(len - size, numa_node_id());
1213         if (!paddc)
1214                 return -ENOMEM;
1215
1216         if (copy_from_user(paddc, user + size, len - size) != 0) {
1217                 ret = -EFAULT;
1218                 goto free;
1219         }
1220
1221         t = xt_find_table_lock(net, NFPROTO_ARP, name);
1222         if (!t || IS_ERR(t)) {
1223                 ret = t ? PTR_ERR(t) : -ENOENT;
1224                 goto free;
1225         }
1226
1227         mutex_lock(&t->lock);
1228         private = t->private;
1229         if (private->number != num_counters) {
1230                 ret = -EINVAL;
1231                 goto unlock_up_free;
1232         }
1233
1234         preempt_disable();
1235         i = 0;
1236         /* Choose the copy that is on our node */
1237         loc_cpu_entry = private->entries[smp_processor_id()];
1238         ARPT_ENTRY_ITERATE(loc_cpu_entry,
1239                            private->size,
1240                            add_counter_to_entry,
1241                            paddc,
1242                            &i);
1243         preempt_enable();
1244  unlock_up_free:
1245         mutex_unlock(&t->lock);
1246
1247         xt_table_unlock(t);
1248         module_put(t->me);
1249  free:
1250         vfree(paddc);
1251
1252         return ret;
1253 }
1254
1255 #ifdef CONFIG_COMPAT
1256 static inline int
1257 compat_release_entry(struct compat_arpt_entry *e, unsigned int *i)
1258 {
1259         struct arpt_entry_target *t;
1260
1261         if (i && (*i)-- == 0)
1262                 return 1;
1263
1264         t = compat_arpt_get_target(e);
1265         module_put(t->u.kernel.target->me);
1266         return 0;
1267 }
1268
1269 static inline int
1270 check_compat_entry_size_and_hooks(struct compat_arpt_entry *e,
1271                                   struct xt_table_info *newinfo,
1272                                   unsigned int *size,
1273                                   unsigned char *base,
1274                                   unsigned char *limit,
1275                                   unsigned int *hook_entries,
1276                                   unsigned int *underflows,
1277                                   unsigned int *i,
1278                                   const char *name)
1279 {
1280         struct arpt_entry_target *t;
1281         struct xt_target *target;
1282         unsigned int entry_offset;
1283         int ret, off, h;
1284
1285         duprintf("check_compat_entry_size_and_hooks %p\n", e);
1286         if ((unsigned long)e % __alignof__(struct compat_arpt_entry) != 0
1287             || (unsigned char *)e + sizeof(struct compat_arpt_entry) >= limit) {
1288                 duprintf("Bad offset %p, limit = %p\n", e, limit);
1289                 return -EINVAL;
1290         }
1291
1292         if (e->next_offset < sizeof(struct compat_arpt_entry) +
1293                              sizeof(struct compat_xt_entry_target)) {
1294                 duprintf("checking: element %p size %u\n",
1295                          e, e->next_offset);
1296                 return -EINVAL;
1297         }
1298
1299         /* For purposes of check_entry casting the compat entry is fine */
1300         ret = check_entry((struct arpt_entry *)e, name);
1301         if (ret)
1302                 return ret;
1303
1304         off = sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1305         entry_offset = (void *)e - (void *)base;
1306
1307         t = compat_arpt_get_target(e);
1308         target = try_then_request_module(xt_find_target(NFPROTO_ARP,
1309                                                         t->u.user.name,
1310                                                         t->u.user.revision),
1311                                          "arpt_%s", t->u.user.name);
1312         if (IS_ERR(target) || !target) {
1313                 duprintf("check_compat_entry_size_and_hooks: `%s' not found\n",
1314                          t->u.user.name);
1315                 ret = target ? PTR_ERR(target) : -ENOENT;
1316                 goto out;
1317         }
1318         t->u.kernel.target = target;
1319
1320         off += xt_compat_target_offset(target);
1321         *size += off;
1322         ret = xt_compat_add_offset(NFPROTO_ARP, entry_offset, off);
1323         if (ret)
1324                 goto release_target;
1325
1326         /* Check hooks & underflows */
1327         for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1328                 if ((unsigned char *)e - base == hook_entries[h])
1329                         newinfo->hook_entry[h] = hook_entries[h];
1330                 if ((unsigned char *)e - base == underflows[h])
1331                         newinfo->underflow[h] = underflows[h];
1332         }
1333
1334         /* Clear counters and comefrom */
1335         memset(&e->counters, 0, sizeof(e->counters));
1336         e->comefrom = 0;
1337
1338         (*i)++;
1339         return 0;
1340
1341 release_target:
1342         module_put(t->u.kernel.target->me);
1343 out:
1344         return ret;
1345 }
1346
1347 static int
1348 compat_copy_entry_from_user(struct compat_arpt_entry *e, void **dstptr,
1349                             unsigned int *size, const char *name,
1350                             struct xt_table_info *newinfo, unsigned char *base)
1351 {
1352         struct arpt_entry_target *t;
1353         struct xt_target *target;
1354         struct arpt_entry *de;
1355         unsigned int origsize;
1356         int ret, h;
1357
1358         ret = 0;
1359         origsize = *size;
1360         de = (struct arpt_entry *)*dstptr;
1361         memcpy(de, e, sizeof(struct arpt_entry));
1362         memcpy(&de->counters, &e->counters, sizeof(e->counters));
1363
1364         *dstptr += sizeof(struct arpt_entry);
1365         *size += sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1366
1367         de->target_offset = e->target_offset - (origsize - *size);
1368         t = compat_arpt_get_target(e);
1369         target = t->u.kernel.target;
1370         xt_compat_target_from_user(t, dstptr, size);
1371
1372         de->next_offset = e->next_offset - (origsize - *size);
1373         for (h = 0; h < NF_ARP_NUMHOOKS; h++) {
1374                 if ((unsigned char *)de - base < newinfo->hook_entry[h])
1375                         newinfo->hook_entry[h] -= origsize - *size;
1376                 if ((unsigned char *)de - base < newinfo->underflow[h])
1377                         newinfo->underflow[h] -= origsize - *size;
1378         }
1379         return ret;
1380 }
1381
1382 static inline int compat_check_entry(struct arpt_entry *e, const char *name,
1383                                      unsigned int *i)
1384 {
1385         int ret;
1386
1387         ret = check_target(e, name);
1388         if (ret)
1389                 return ret;
1390
1391         (*i)++;
1392         return 0;
1393 }
1394
1395 static int translate_compat_table(const char *name,
1396                                   unsigned int valid_hooks,
1397                                   struct xt_table_info **pinfo,
1398                                   void **pentry0,
1399                                   unsigned int total_size,
1400                                   unsigned int number,
1401                                   unsigned int *hook_entries,
1402                                   unsigned int *underflows)
1403 {
1404         unsigned int i, j;
1405         struct xt_table_info *newinfo, *info;
1406         void *pos, *entry0, *entry1;
1407         unsigned int size;
1408         int ret;
1409
1410         info = *pinfo;
1411         entry0 = *pentry0;
1412         size = total_size;
1413         info->number = number;
1414
1415         /* Init all hooks to impossible value. */
1416         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1417                 info->hook_entry[i] = 0xFFFFFFFF;
1418                 info->underflow[i] = 0xFFFFFFFF;
1419         }
1420
1421         duprintf("translate_compat_table: size %u\n", info->size);
1422         j = 0;
1423         xt_compat_lock(NFPROTO_ARP);
1424         /* Walk through entries, checking offsets. */
1425         ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size,
1426                                         check_compat_entry_size_and_hooks,
1427                                         info, &size, entry0,
1428                                         entry0 + total_size,
1429                                         hook_entries, underflows, &j, name);
1430         if (ret != 0)
1431                 goto out_unlock;
1432
1433         ret = -EINVAL;
1434         if (j != number) {
1435                 duprintf("translate_compat_table: %u not %u entries\n",
1436                          j, number);
1437                 goto out_unlock;
1438         }
1439
1440         /* Check hooks all assigned */
1441         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1442                 /* Only hooks which are valid */
1443                 if (!(valid_hooks & (1 << i)))
1444                         continue;
1445                 if (info->hook_entry[i] == 0xFFFFFFFF) {
1446                         duprintf("Invalid hook entry %u %u\n",
1447                                  i, hook_entries[i]);
1448                         goto out_unlock;
1449                 }
1450                 if (info->underflow[i] == 0xFFFFFFFF) {
1451                         duprintf("Invalid underflow %u %u\n",
1452                                  i, underflows[i]);
1453                         goto out_unlock;
1454                 }
1455         }
1456
1457         ret = -ENOMEM;
1458         newinfo = xt_alloc_table_info(size);
1459         if (!newinfo)
1460                 goto out_unlock;
1461
1462         newinfo->number = number;
1463         for (i = 0; i < NF_ARP_NUMHOOKS; i++) {
1464                 newinfo->hook_entry[i] = info->hook_entry[i];
1465                 newinfo->underflow[i] = info->underflow[i];
1466         }
1467         entry1 = newinfo->entries[raw_smp_processor_id()];
1468         pos = entry1;
1469         size = total_size;
1470         ret = COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size,
1471                                         compat_copy_entry_from_user,
1472                                         &pos, &size, name, newinfo, entry1);
1473         xt_compat_flush_offsets(NFPROTO_ARP);
1474         xt_compat_unlock(NFPROTO_ARP);
1475         if (ret)
1476                 goto free_newinfo;
1477
1478         ret = -ELOOP;
1479         if (!mark_source_chains(newinfo, valid_hooks, entry1))
1480                 goto free_newinfo;
1481
1482         i = 0;
1483         ret = ARPT_ENTRY_ITERATE(entry1, newinfo->size, compat_check_entry,
1484                                  name, &i);
1485         if (ret) {
1486                 j -= i;
1487                 COMPAT_ARPT_ENTRY_ITERATE_CONTINUE(entry0, newinfo->size, i,
1488                                                    compat_release_entry, &j);
1489                 ARPT_ENTRY_ITERATE(entry1, newinfo->size, cleanup_entry, &i);
1490                 xt_free_table_info(newinfo);
1491                 return ret;
1492         }
1493
1494         /* And one copy for every other CPU */
1495         for_each_possible_cpu(i)
1496                 if (newinfo->entries[i] && newinfo->entries[i] != entry1)
1497                         memcpy(newinfo->entries[i], entry1, newinfo->size);
1498
1499         *pinfo = newinfo;
1500         *pentry0 = entry1;
1501         xt_free_table_info(info);
1502         return 0;
1503
1504 free_newinfo:
1505         xt_free_table_info(newinfo);
1506 out:
1507         COMPAT_ARPT_ENTRY_ITERATE(entry0, total_size, compat_release_entry, &j);
1508         return ret;
1509 out_unlock:
1510         xt_compat_flush_offsets(NFPROTO_ARP);
1511         xt_compat_unlock(NFPROTO_ARP);
1512         goto out;
1513 }
1514
1515 struct compat_arpt_replace {
1516         char                            name[ARPT_TABLE_MAXNAMELEN];
1517         u32                             valid_hooks;
1518         u32                             num_entries;
1519         u32                             size;
1520         u32                             hook_entry[NF_ARP_NUMHOOKS];
1521         u32                             underflow[NF_ARP_NUMHOOKS];
1522         u32                             num_counters;
1523         compat_uptr_t                   counters;
1524         struct compat_arpt_entry        entries[0];
1525 };
1526
1527 static int compat_do_replace(struct net *net, void __user *user,
1528                              unsigned int len)
1529 {
1530         int ret;
1531         struct compat_arpt_replace tmp;
1532         struct xt_table_info *newinfo;
1533         void *loc_cpu_entry;
1534
1535         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1536                 return -EFAULT;
1537
1538         /* overflow check */
1539         if (tmp.size >= INT_MAX / num_possible_cpus())
1540                 return -ENOMEM;
1541         if (tmp.num_counters >= INT_MAX / sizeof(struct xt_counters))
1542                 return -ENOMEM;
1543
1544         newinfo = xt_alloc_table_info(tmp.size);
1545         if (!newinfo)
1546                 return -ENOMEM;
1547
1548         /* choose the copy that is on our node/cpu */
1549         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1550         if (copy_from_user(loc_cpu_entry, user + sizeof(tmp), tmp.size) != 0) {
1551                 ret = -EFAULT;
1552                 goto free_newinfo;
1553         }
1554
1555         ret = translate_compat_table(tmp.name, tmp.valid_hooks,
1556                                      &newinfo, &loc_cpu_entry, tmp.size,
1557                                      tmp.num_entries, tmp.hook_entry,
1558                                      tmp.underflow);
1559         if (ret != 0)
1560                 goto free_newinfo;
1561
1562         duprintf("compat_do_replace: Translated table\n");
1563
1564         ret = __do_replace(net, tmp.name, tmp.valid_hooks, newinfo,
1565                            tmp.num_counters, compat_ptr(tmp.counters));
1566         if (ret)
1567                 goto free_newinfo_untrans;
1568         return 0;
1569
1570  free_newinfo_untrans:
1571         ARPT_ENTRY_ITERATE(loc_cpu_entry, newinfo->size, cleanup_entry, NULL);
1572  free_newinfo:
1573         xt_free_table_info(newinfo);
1574         return ret;
1575 }
1576
1577 static int compat_do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user,
1578                                   unsigned int len)
1579 {
1580         int ret;
1581
1582         if (!capable(CAP_NET_ADMIN))
1583                 return -EPERM;
1584
1585         switch (cmd) {
1586         case ARPT_SO_SET_REPLACE:
1587                 ret = compat_do_replace(sock_net(sk), user, len);
1588                 break;
1589
1590         case ARPT_SO_SET_ADD_COUNTERS:
1591                 ret = do_add_counters(sock_net(sk), user, len, 1);
1592                 break;
1593
1594         default:
1595                 duprintf("do_arpt_set_ctl:  unknown request %i\n", cmd);
1596                 ret = -EINVAL;
1597         }
1598
1599         return ret;
1600 }
1601
1602 static int compat_copy_entry_to_user(struct arpt_entry *e, void __user **dstptr,
1603                                      compat_uint_t *size,
1604                                      struct xt_counters *counters,
1605                                      unsigned int *i)
1606 {
1607         struct arpt_entry_target *t;
1608         struct compat_arpt_entry __user *ce;
1609         u_int16_t target_offset, next_offset;
1610         compat_uint_t origsize;
1611         int ret;
1612
1613         ret = -EFAULT;
1614         origsize = *size;
1615         ce = (struct compat_arpt_entry __user *)*dstptr;
1616         if (copy_to_user(ce, e, sizeof(struct arpt_entry)))
1617                 goto out;
1618
1619         if (copy_to_user(&ce->counters, &counters[*i], sizeof(counters[*i])))
1620                 goto out;
1621
1622         *dstptr += sizeof(struct compat_arpt_entry);
1623         *size -= sizeof(struct arpt_entry) - sizeof(struct compat_arpt_entry);
1624
1625         target_offset = e->target_offset - (origsize - *size);
1626
1627         t = arpt_get_target(e);
1628         ret = xt_compat_target_to_user(t, dstptr, size);
1629         if (ret)
1630                 goto out;
1631         ret = -EFAULT;
1632         next_offset = e->next_offset - (origsize - *size);
1633         if (put_user(target_offset, &ce->target_offset))
1634                 goto out;
1635         if (put_user(next_offset, &ce->next_offset))
1636                 goto out;
1637
1638         (*i)++;
1639         return 0;
1640 out:
1641         return ret;
1642 }
1643
1644 static int compat_copy_entries_to_user(unsigned int total_size,
1645                                        struct xt_table *table,
1646                                        void __user *userptr)
1647 {
1648         struct xt_counters *counters;
1649         const struct xt_table_info *private = table->private;
1650         void __user *pos;
1651         unsigned int size;
1652         int ret = 0;
1653         void *loc_cpu_entry;
1654         unsigned int i = 0;
1655
1656         counters = alloc_counters(table);
1657         if (IS_ERR(counters))
1658                 return PTR_ERR(counters);
1659
1660         /* choose the copy on our node/cpu */
1661         loc_cpu_entry = private->entries[raw_smp_processor_id()];
1662         pos = userptr;
1663         size = total_size;
1664         ret = ARPT_ENTRY_ITERATE(loc_cpu_entry, total_size,
1665                                  compat_copy_entry_to_user,
1666                                  &pos, &size, counters, &i);
1667         vfree(counters);
1668         return ret;
1669 }
1670
1671 struct compat_arpt_get_entries {
1672         char name[ARPT_TABLE_MAXNAMELEN];
1673         compat_uint_t size;
1674         struct compat_arpt_entry entrytable[0];
1675 };
1676
1677 static int compat_get_entries(struct net *net,
1678                               struct compat_arpt_get_entries __user *uptr,
1679                               int *len)
1680 {
1681         int ret;
1682         struct compat_arpt_get_entries get;
1683         struct xt_table *t;
1684
1685         if (*len < sizeof(get)) {
1686                 duprintf("compat_get_entries: %u < %zu\n", *len, sizeof(get));
1687                 return -EINVAL;
1688         }
1689         if (copy_from_user(&get, uptr, sizeof(get)) != 0)
1690                 return -EFAULT;
1691         if (*len != sizeof(struct compat_arpt_get_entries) + get.size) {
1692                 duprintf("compat_get_entries: %u != %zu\n",
1693                          *len, sizeof(get) + get.size);
1694                 return -EINVAL;
1695         }
1696
1697         xt_compat_lock(NFPROTO_ARP);
1698         t = xt_find_table_lock(net, NFPROTO_ARP, get.name);
1699         if (t && !IS_ERR(t)) {
1700                 const struct xt_table_info *private = t->private;
1701                 struct xt_table_info info;
1702
1703                 duprintf("t->private->number = %u\n", private->number);
1704                 ret = compat_table_info(private, &info);
1705                 if (!ret && get.size == info.size) {
1706                         ret = compat_copy_entries_to_user(private->size,
1707                                                           t, uptr->entrytable);
1708                 } else if (!ret) {
1709                         duprintf("compat_get_entries: I've got %u not %u!\n",
1710                                  private->size, get.size);
1711                         ret = -EAGAIN;
1712                 }
1713                 xt_compat_flush_offsets(NFPROTO_ARP);
1714                 module_put(t->me);
1715                 xt_table_unlock(t);
1716         } else
1717                 ret = t ? PTR_ERR(t) : -ENOENT;
1718
1719         xt_compat_unlock(NFPROTO_ARP);
1720         return ret;
1721 }
1722
1723 static int do_arpt_get_ctl(struct sock *, int, void __user *, int *);
1724
1725 static int compat_do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user,
1726                                   int *len)
1727 {
1728         int ret;
1729
1730         if (!capable(CAP_NET_ADMIN))
1731                 return -EPERM;
1732
1733         switch (cmd) {
1734         case ARPT_SO_GET_INFO:
1735                 ret = get_info(sock_net(sk), user, len, 1);
1736                 break;
1737         case ARPT_SO_GET_ENTRIES:
1738                 ret = compat_get_entries(sock_net(sk), user, len);
1739                 break;
1740         default:
1741                 ret = do_arpt_get_ctl(sk, cmd, user, len);
1742         }
1743         return ret;
1744 }
1745 #endif
1746
1747 static int do_arpt_set_ctl(struct sock *sk, int cmd, void __user *user, unsigned int len)
1748 {
1749         int ret;
1750
1751         if (!capable(CAP_NET_ADMIN))
1752                 return -EPERM;
1753
1754         switch (cmd) {
1755         case ARPT_SO_SET_REPLACE:
1756                 ret = do_replace(sock_net(sk), user, len);
1757                 break;
1758
1759         case ARPT_SO_SET_ADD_COUNTERS:
1760                 ret = do_add_counters(sock_net(sk), user, len, 0);
1761                 break;
1762
1763         default:
1764                 duprintf("do_arpt_set_ctl:  unknown request %i\n", cmd);
1765                 ret = -EINVAL;
1766         }
1767
1768         return ret;
1769 }
1770
1771 static int do_arpt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1772 {
1773         int ret;
1774
1775         if (!capable(CAP_NET_ADMIN))
1776                 return -EPERM;
1777
1778         switch (cmd) {
1779         case ARPT_SO_GET_INFO:
1780                 ret = get_info(sock_net(sk), user, len, 0);
1781                 break;
1782
1783         case ARPT_SO_GET_ENTRIES:
1784                 ret = get_entries(sock_net(sk), user, len);
1785                 break;
1786
1787         case ARPT_SO_GET_REVISION_TARGET: {
1788                 struct xt_get_revision rev;
1789
1790                 if (*len != sizeof(rev)) {
1791                         ret = -EINVAL;
1792                         break;
1793                 }
1794                 if (copy_from_user(&rev, user, sizeof(rev)) != 0) {
1795                         ret = -EFAULT;
1796                         break;
1797                 }
1798
1799                 try_then_request_module(xt_find_revision(NFPROTO_ARP, rev.name,
1800                                                          rev.revision, 1, &ret),
1801                                         "arpt_%s", rev.name);
1802                 break;
1803         }
1804
1805         default:
1806                 duprintf("do_arpt_get_ctl: unknown request %i\n", cmd);
1807                 ret = -EINVAL;
1808         }
1809
1810         return ret;
1811 }
1812
1813 struct xt_table *arpt_register_table(struct net *net, struct xt_table *table,
1814                                      const struct arpt_replace *repl)
1815 {
1816         int ret;
1817         struct xt_table_info *newinfo;
1818         struct xt_table_info bootstrap
1819                 = { 0, 0, 0, { 0 }, { 0 }, { } };
1820         void *loc_cpu_entry;
1821         struct xt_table *new_table;
1822
1823         newinfo = xt_alloc_table_info(repl->size);
1824         if (!newinfo) {
1825                 ret = -ENOMEM;
1826                 goto out;
1827         }
1828
1829         /* choose the copy on our node/cpu */
1830         loc_cpu_entry = newinfo->entries[raw_smp_processor_id()];
1831         memcpy(loc_cpu_entry, repl->entries, repl->size);
1832
1833         ret = translate_table(table->name, table->valid_hooks,
1834                               newinfo, loc_cpu_entry, repl->size,
1835                               repl->num_entries,
1836                               repl->hook_entry,
1837                               repl->underflow);
1838
1839         duprintf("arpt_register_table: translate table gives %d\n", ret);
1840         if (ret != 0)
1841                 goto out_free;
1842
1843         new_table = xt_register_table(net, table, &bootstrap, newinfo);
1844         if (IS_ERR(new_table)) {
1845                 ret = PTR_ERR(new_table);
1846                 goto out_free;
1847         }
1848         return new_table;
1849
1850 out_free:
1851         xt_free_table_info(newinfo);
1852 out:
1853         return ERR_PTR(ret);
1854 }
1855
1856 void arpt_unregister_table(struct xt_table *table)
1857 {
1858         struct xt_table_info *private;
1859         void *loc_cpu_entry;
1860         struct module *table_owner = table->me;
1861
1862         private = xt_unregister_table(table);
1863
1864         /* Decrease module usage counts and free resources */
1865         loc_cpu_entry = private->entries[raw_smp_processor_id()];
1866         ARPT_ENTRY_ITERATE(loc_cpu_entry, private->size,
1867                            cleanup_entry, NULL);
1868         if (private->number > private->initial_entries)
1869                 module_put(table_owner);
1870         xt_free_table_info(private);
1871 }
1872
1873 /* The built-in targets: standard (NULL) and error. */
1874 static struct xt_target arpt_standard_target __read_mostly = {
1875         .name           = ARPT_STANDARD_TARGET,
1876         .targetsize     = sizeof(int),
1877         .family         = NFPROTO_ARP,
1878 #ifdef CONFIG_COMPAT
1879         .compatsize     = sizeof(compat_int_t),
1880         .compat_from_user = compat_standard_from_user,
1881         .compat_to_user = compat_standard_to_user,
1882 #endif
1883 };
1884
1885 static struct xt_target arpt_error_target __read_mostly = {
1886         .name           = ARPT_ERROR_TARGET,
1887         .target         = arpt_error,
1888         .targetsize     = ARPT_FUNCTION_MAXNAMELEN,
1889         .family         = NFPROTO_ARP,
1890 };
1891
1892 static struct nf_sockopt_ops arpt_sockopts = {
1893         .pf             = PF_INET,
1894         .set_optmin     = ARPT_BASE_CTL,
1895         .set_optmax     = ARPT_SO_SET_MAX+1,
1896         .set            = do_arpt_set_ctl,
1897 #ifdef CONFIG_COMPAT
1898         .compat_set     = compat_do_arpt_set_ctl,
1899 #endif
1900         .get_optmin     = ARPT_BASE_CTL,
1901         .get_optmax     = ARPT_SO_GET_MAX+1,
1902         .get            = do_arpt_get_ctl,
1903 #ifdef CONFIG_COMPAT
1904         .compat_get     = compat_do_arpt_get_ctl,
1905 #endif
1906         .owner          = THIS_MODULE,
1907 };
1908
1909 static int __net_init arp_tables_net_init(struct net *net)
1910 {
1911         return xt_proto_init(net, NFPROTO_ARP);
1912 }
1913
1914 static void __net_exit arp_tables_net_exit(struct net *net)
1915 {
1916         xt_proto_fini(net, NFPROTO_ARP);
1917 }
1918
1919 static struct pernet_operations arp_tables_net_ops = {
1920         .init = arp_tables_net_init,
1921         .exit = arp_tables_net_exit,
1922 };
1923
1924 static int __init arp_tables_init(void)
1925 {
1926         int ret;
1927
1928         ret = register_pernet_subsys(&arp_tables_net_ops);
1929         if (ret < 0)
1930                 goto err1;
1931
1932         /* Noone else will be downing sem now, so we won't sleep */
1933         ret = xt_register_target(&arpt_standard_target);
1934         if (ret < 0)
1935                 goto err2;
1936         ret = xt_register_target(&arpt_error_target);
1937         if (ret < 0)
1938                 goto err3;
1939
1940         /* Register setsockopt */
1941         ret = nf_register_sockopt(&arpt_sockopts);
1942         if (ret < 0)
1943                 goto err4;
1944
1945         printk(KERN_INFO "arp_tables: (C) 2002 David S. Miller\n");
1946         return 0;
1947
1948 err4:
1949         xt_unregister_target(&arpt_error_target);
1950 err3:
1951         xt_unregister_target(&arpt_standard_target);
1952 err2:
1953         unregister_pernet_subsys(&arp_tables_net_ops);
1954 err1:
1955         return ret;
1956 }
1957
1958 static void __exit arp_tables_fini(void)
1959 {
1960         nf_unregister_sockopt(&arpt_sockopts);
1961         xt_unregister_target(&arpt_error_target);
1962         xt_unregister_target(&arpt_standard_target);
1963         unregister_pernet_subsys(&arp_tables_net_ops);
1964 }
1965
1966 EXPORT_SYMBOL(arpt_register_table);
1967 EXPORT_SYMBOL(arpt_unregister_table);
1968 EXPORT_SYMBOL(arpt_do_table);
1969
1970 module_init(arp_tables_init);
1971 module_exit(arp_tables_fini);