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