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