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