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