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