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