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