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