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