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