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