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