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