GNU Linux-libre 4.14.294-gnu1
[releases.git] / net / bridge / netfilter / ebtables.c
1 /*
2  *  ebtables
3  *
4  *  Author:
5  *  Bart De Schuymer            <bdschuym@pandora.be>
6  *
7  *  ebtables.c,v 2.0, July, 2002
8  *
9  *  This code is strongly inspired by the iptables code which is
10  *  Copyright (C) 1999 Paul `Rusty' Russell & Michael J. Neuling
11  *
12  *  This program is free software; you can redistribute it and/or
13  *  modify it under the terms of the GNU General Public License
14  *  as published by the Free Software Foundation; either version
15  *  2 of the License, or (at your option) any later version.
16  */
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/kmod.h>
19 #include <linux/module.h>
20 #include <linux/vmalloc.h>
21 #include <linux/netfilter/x_tables.h>
22 #include <linux/netfilter_bridge/ebtables.h>
23 #include <linux/spinlock.h>
24 #include <linux/mutex.h>
25 #include <linux/slab.h>
26 #include <linux/uaccess.h>
27 #include <linux/smp.h>
28 #include <linux/cpumask.h>
29 #include <linux/audit.h>
30 #include <net/sock.h>
31 /* needed for logical [in,out]-dev filtering */
32 #include "../br_private.h"
33
34 /* Each cpu has its own set of counters, so there is no need for write_lock in
35  * the softirq
36  * For reading or updating the counters, the user context needs to
37  * get a write_lock
38  */
39
40 /* The size of each set of counters is altered to get cache alignment */
41 #define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
42 #define COUNTER_OFFSET(n) (SMP_ALIGN(n * sizeof(struct ebt_counter)))
43 #define COUNTER_BASE(c, n, cpu) ((struct ebt_counter *)(((char *)c) + \
44                                  COUNTER_OFFSET(n) * cpu))
45
46
47
48 static DEFINE_MUTEX(ebt_mutex);
49
50 #ifdef CONFIG_COMPAT
51 static void ebt_standard_compat_from_user(void *dst, const void *src)
52 {
53         int v = *(compat_int_t *)src;
54
55         if (v >= 0)
56                 v += xt_compat_calc_jump(NFPROTO_BRIDGE, v);
57         memcpy(dst, &v, sizeof(v));
58 }
59
60 static int ebt_standard_compat_to_user(void __user *dst, const void *src)
61 {
62         compat_int_t cv = *(int *)src;
63
64         if (cv >= 0)
65                 cv -= xt_compat_calc_jump(NFPROTO_BRIDGE, cv);
66         return copy_to_user(dst, &cv, sizeof(cv)) ? -EFAULT : 0;
67 }
68 #endif
69
70
71 static struct xt_target ebt_standard_target = {
72         .name       = "standard",
73         .revision   = 0,
74         .family     = NFPROTO_BRIDGE,
75         .targetsize = sizeof(int),
76 #ifdef CONFIG_COMPAT
77         .compatsize = sizeof(compat_int_t),
78         .compat_from_user = ebt_standard_compat_from_user,
79         .compat_to_user =  ebt_standard_compat_to_user,
80 #endif
81 };
82
83 static inline int
84 ebt_do_watcher(const struct ebt_entry_watcher *w, struct sk_buff *skb,
85                struct xt_action_param *par)
86 {
87         par->target   = w->u.watcher;
88         par->targinfo = w->data;
89         w->u.watcher->target(skb, par);
90         /* watchers don't give a verdict */
91         return 0;
92 }
93
94 static inline int
95 ebt_do_match(struct ebt_entry_match *m, const struct sk_buff *skb,
96              struct xt_action_param *par)
97 {
98         par->match     = m->u.match;
99         par->matchinfo = m->data;
100         return m->u.match->match(skb, par) ? EBT_MATCH : EBT_NOMATCH;
101 }
102
103 static inline int
104 ebt_dev_check(const char *entry, const struct net_device *device)
105 {
106         int i = 0;
107         const char *devname;
108
109         if (*entry == '\0')
110                 return 0;
111         if (!device)
112                 return 1;
113         devname = device->name;
114         /* 1 is the wildcard token */
115         while (entry[i] != '\0' && entry[i] != 1 && entry[i] == devname[i])
116                 i++;
117         return devname[i] != entry[i] && entry[i] != 1;
118 }
119
120 /* process standard matches */
121 static inline int
122 ebt_basic_match(const struct ebt_entry *e, const struct sk_buff *skb,
123                 const struct net_device *in, const struct net_device *out)
124 {
125         const struct ethhdr *h = eth_hdr(skb);
126         const struct net_bridge_port *p;
127         __be16 ethproto;
128
129         if (skb_vlan_tag_present(skb))
130                 ethproto = htons(ETH_P_8021Q);
131         else
132                 ethproto = h->h_proto;
133
134         if (e->bitmask & EBT_802_3) {
135                 if (NF_INVF(e, EBT_IPROTO, eth_proto_is_802_3(ethproto)))
136                         return 1;
137         } else if (!(e->bitmask & EBT_NOPROTO) &&
138                    NF_INVF(e, EBT_IPROTO, e->ethproto != ethproto))
139                 return 1;
140
141         if (NF_INVF(e, EBT_IIN, ebt_dev_check(e->in, in)))
142                 return 1;
143         if (NF_INVF(e, EBT_IOUT, ebt_dev_check(e->out, out)))
144                 return 1;
145         /* rcu_read_lock()ed by nf_hook_thresh */
146         if (in && (p = br_port_get_rcu(in)) != NULL &&
147             NF_INVF(e, EBT_ILOGICALIN,
148                     ebt_dev_check(e->logical_in, p->br->dev)))
149                 return 1;
150         if (out && (p = br_port_get_rcu(out)) != NULL &&
151             NF_INVF(e, EBT_ILOGICALOUT,
152                     ebt_dev_check(e->logical_out, p->br->dev)))
153                 return 1;
154
155         if (e->bitmask & EBT_SOURCEMAC) {
156                 if (NF_INVF(e, EBT_ISOURCE,
157                             !ether_addr_equal_masked(h->h_source, e->sourcemac,
158                                                      e->sourcemsk)))
159                         return 1;
160         }
161         if (e->bitmask & EBT_DESTMAC) {
162                 if (NF_INVF(e, EBT_IDEST,
163                             !ether_addr_equal_masked(h->h_dest, e->destmac,
164                                                      e->destmsk)))
165                         return 1;
166         }
167         return 0;
168 }
169
170 static inline
171 struct ebt_entry *ebt_next_entry(const struct ebt_entry *entry)
172 {
173         return (void *)entry + entry->next_offset;
174 }
175
176 /* Do some firewalling */
177 unsigned int ebt_do_table(struct sk_buff *skb,
178                           const struct nf_hook_state *state,
179                           struct ebt_table *table)
180 {
181         unsigned int hook = state->hook;
182         int i, nentries;
183         struct ebt_entry *point;
184         struct ebt_counter *counter_base, *cb_base;
185         const struct ebt_entry_target *t;
186         int verdict, sp = 0;
187         struct ebt_chainstack *cs;
188         struct ebt_entries *chaininfo;
189         const char *base;
190         const struct ebt_table_info *private;
191         struct xt_action_param acpar;
192
193         acpar.state   = state;
194         acpar.hotdrop = false;
195
196         read_lock_bh(&table->lock);
197         private = table->private;
198         cb_base = COUNTER_BASE(private->counters, private->nentries,
199            smp_processor_id());
200         if (private->chainstack)
201                 cs = private->chainstack[smp_processor_id()];
202         else
203                 cs = NULL;
204         chaininfo = private->hook_entry[hook];
205         nentries = private->hook_entry[hook]->nentries;
206         point = (struct ebt_entry *)(private->hook_entry[hook]->data);
207         counter_base = cb_base + private->hook_entry[hook]->counter_offset;
208         /* base for chain jumps */
209         base = private->entries;
210         i = 0;
211         while (i < nentries) {
212                 if (ebt_basic_match(point, skb, state->in, state->out))
213                         goto letscontinue;
214
215                 if (EBT_MATCH_ITERATE(point, ebt_do_match, skb, &acpar) != 0)
216                         goto letscontinue;
217                 if (acpar.hotdrop) {
218                         read_unlock_bh(&table->lock);
219                         return NF_DROP;
220                 }
221
222                 /* increase counter */
223                 (*(counter_base + i)).pcnt++;
224                 (*(counter_base + i)).bcnt += skb->len;
225
226                 /* these should only watch: not modify, nor tell us
227                  * what to do with the packet
228                  */
229                 EBT_WATCHER_ITERATE(point, ebt_do_watcher, skb, &acpar);
230
231                 t = (struct ebt_entry_target *)
232                    (((char *)point) + point->target_offset);
233                 /* standard target */
234                 if (!t->u.target->target)
235                         verdict = ((struct ebt_standard_target *)t)->verdict;
236                 else {
237                         acpar.target   = t->u.target;
238                         acpar.targinfo = t->data;
239                         verdict = t->u.target->target(skb, &acpar);
240                 }
241                 if (verdict == EBT_ACCEPT) {
242                         read_unlock_bh(&table->lock);
243                         return NF_ACCEPT;
244                 }
245                 if (verdict == EBT_DROP) {
246                         read_unlock_bh(&table->lock);
247                         return NF_DROP;
248                 }
249                 if (verdict == EBT_RETURN) {
250 letsreturn:
251                         if (WARN(sp == 0, "RETURN on base chain")) {
252                                 /* act like this is EBT_CONTINUE */
253                                 goto letscontinue;
254                         }
255
256                         sp--;
257                         /* put all the local variables right */
258                         i = cs[sp].n;
259                         chaininfo = cs[sp].chaininfo;
260                         nentries = chaininfo->nentries;
261                         point = cs[sp].e;
262                         counter_base = cb_base +
263                            chaininfo->counter_offset;
264                         continue;
265                 }
266                 if (verdict == EBT_CONTINUE)
267                         goto letscontinue;
268
269                 if (WARN(verdict < 0, "bogus standard verdict\n")) {
270                         read_unlock_bh(&table->lock);
271                         return NF_DROP;
272                 }
273
274                 /* jump to a udc */
275                 cs[sp].n = i + 1;
276                 cs[sp].chaininfo = chaininfo;
277                 cs[sp].e = ebt_next_entry(point);
278                 i = 0;
279                 chaininfo = (struct ebt_entries *) (base + verdict);
280
281                 if (WARN(chaininfo->distinguisher, "jump to non-chain\n")) {
282                         read_unlock_bh(&table->lock);
283                         return NF_DROP;
284                 }
285
286                 nentries = chaininfo->nentries;
287                 point = (struct ebt_entry *)chaininfo->data;
288                 counter_base = cb_base + chaininfo->counter_offset;
289                 sp++;
290                 continue;
291 letscontinue:
292                 point = ebt_next_entry(point);
293                 i++;
294         }
295
296         /* I actually like this :) */
297         if (chaininfo->policy == EBT_RETURN)
298                 goto letsreturn;
299         if (chaininfo->policy == EBT_ACCEPT) {
300                 read_unlock_bh(&table->lock);
301                 return NF_ACCEPT;
302         }
303         read_unlock_bh(&table->lock);
304         return NF_DROP;
305 }
306
307 /* If it succeeds, returns element and locks mutex */
308 static inline void *
309 find_inlist_lock_noload(struct list_head *head, const char *name, int *error,
310                         struct mutex *mutex)
311 {
312         struct {
313                 struct list_head list;
314                 char name[EBT_FUNCTION_MAXNAMELEN];
315         } *e;
316
317         mutex_lock(mutex);
318         list_for_each_entry(e, head, list) {
319                 if (strcmp(e->name, name) == 0)
320                         return e;
321         }
322         *error = -ENOENT;
323         mutex_unlock(mutex);
324         return NULL;
325 }
326
327 static void *
328 find_inlist_lock(struct list_head *head, const char *name, const char *prefix,
329                  int *error, struct mutex *mutex)
330 {
331         return try_then_request_module(
332                         find_inlist_lock_noload(head, name, error, mutex),
333                         "%s%s", prefix, name);
334 }
335
336 static inline struct ebt_table *
337 find_table_lock(struct net *net, const char *name, int *error,
338                 struct mutex *mutex)
339 {
340         return find_inlist_lock(&net->xt.tables[NFPROTO_BRIDGE], name,
341                                 "ebtable_", error, mutex);
342 }
343
344 static inline int
345 ebt_check_match(struct ebt_entry_match *m, struct xt_mtchk_param *par,
346                 unsigned int *cnt)
347 {
348         const struct ebt_entry *e = par->entryinfo;
349         struct xt_match *match;
350         size_t left = ((char *)e + e->watchers_offset) - (char *)m;
351         int ret;
352
353         if (left < sizeof(struct ebt_entry_match) ||
354             left - sizeof(struct ebt_entry_match) < m->match_size)
355                 return -EINVAL;
356
357         match = xt_find_match(NFPROTO_BRIDGE, m->u.name, 0);
358         if (IS_ERR(match) || match->family != NFPROTO_BRIDGE) {
359                 if (!IS_ERR(match))
360                         module_put(match->me);
361                 request_module("ebt_%s", m->u.name);
362                 match = xt_find_match(NFPROTO_BRIDGE, m->u.name, 0);
363         }
364         if (IS_ERR(match))
365                 return PTR_ERR(match);
366         m->u.match = match;
367
368         par->match     = match;
369         par->matchinfo = m->data;
370         ret = xt_check_match(par, m->match_size,
371               e->ethproto, e->invflags & EBT_IPROTO);
372         if (ret < 0) {
373                 module_put(match->me);
374                 return ret;
375         }
376
377         (*cnt)++;
378         return 0;
379 }
380
381 static inline int
382 ebt_check_watcher(struct ebt_entry_watcher *w, struct xt_tgchk_param *par,
383                   unsigned int *cnt)
384 {
385         const struct ebt_entry *e = par->entryinfo;
386         struct xt_target *watcher;
387         size_t left = ((char *)e + e->target_offset) - (char *)w;
388         int ret;
389
390         if (left < sizeof(struct ebt_entry_watcher) ||
391            left - sizeof(struct ebt_entry_watcher) < w->watcher_size)
392                 return -EINVAL;
393
394         watcher = xt_request_find_target(NFPROTO_BRIDGE, w->u.name, 0);
395         if (IS_ERR(watcher))
396                 return PTR_ERR(watcher);
397
398         if (watcher->family != NFPROTO_BRIDGE) {
399                 module_put(watcher->me);
400                 return -ENOENT;
401         }
402
403         w->u.watcher = watcher;
404
405         par->target   = watcher;
406         par->targinfo = w->data;
407         ret = xt_check_target(par, w->watcher_size,
408               e->ethproto, e->invflags & EBT_IPROTO);
409         if (ret < 0) {
410                 module_put(watcher->me);
411                 return ret;
412         }
413
414         (*cnt)++;
415         return 0;
416 }
417
418 static int ebt_verify_pointers(const struct ebt_replace *repl,
419                                struct ebt_table_info *newinfo)
420 {
421         unsigned int limit = repl->entries_size;
422         unsigned int valid_hooks = repl->valid_hooks;
423         unsigned int offset = 0;
424         int i;
425
426         for (i = 0; i < NF_BR_NUMHOOKS; i++)
427                 newinfo->hook_entry[i] = NULL;
428
429         newinfo->entries_size = repl->entries_size;
430         newinfo->nentries = repl->nentries;
431
432         while (offset < limit) {
433                 size_t left = limit - offset;
434                 struct ebt_entry *e = (void *)newinfo->entries + offset;
435
436                 if (left < sizeof(unsigned int))
437                         break;
438
439                 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
440                         if ((valid_hooks & (1 << i)) == 0)
441                                 continue;
442                         if ((char __user *)repl->hook_entry[i] ==
443                              repl->entries + offset)
444                                 break;
445                 }
446
447                 if (i != NF_BR_NUMHOOKS || !(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
448                         if (e->bitmask != 0) {
449                                 /* we make userspace set this right,
450                                  * so there is no misunderstanding
451                                  */
452                                 return -EINVAL;
453                         }
454                         if (i != NF_BR_NUMHOOKS)
455                                 newinfo->hook_entry[i] = (struct ebt_entries *)e;
456                         if (left < sizeof(struct ebt_entries))
457                                 break;
458                         offset += sizeof(struct ebt_entries);
459                 } else {
460                         if (left < sizeof(struct ebt_entry))
461                                 break;
462                         if (left < e->next_offset)
463                                 break;
464                         if (e->next_offset < sizeof(struct ebt_entry))
465                                 return -EINVAL;
466                         offset += e->next_offset;
467                 }
468         }
469         if (offset != limit)
470                 return -EINVAL;
471
472         /* check if all valid hooks have a chain */
473         for (i = 0; i < NF_BR_NUMHOOKS; i++) {
474                 if (!newinfo->hook_entry[i] &&
475                    (valid_hooks & (1 << i)))
476                         return -EINVAL;
477         }
478         return 0;
479 }
480
481 /* this one is very careful, as it is the first function
482  * to parse the userspace data
483  */
484 static inline int
485 ebt_check_entry_size_and_hooks(const struct ebt_entry *e,
486                                const struct ebt_table_info *newinfo,
487                                unsigned int *n, unsigned int *cnt,
488                                unsigned int *totalcnt, unsigned int *udc_cnt)
489 {
490         int i;
491
492         for (i = 0; i < NF_BR_NUMHOOKS; i++) {
493                 if ((void *)e == (void *)newinfo->hook_entry[i])
494                         break;
495         }
496         /* beginning of a new chain
497          * if i == NF_BR_NUMHOOKS it must be a user defined chain
498          */
499         if (i != NF_BR_NUMHOOKS || !e->bitmask) {
500                 /* this checks if the previous chain has as many entries
501                  * as it said it has
502                  */
503                 if (*n != *cnt)
504                         return -EINVAL;
505
506                 if (((struct ebt_entries *)e)->policy != EBT_DROP &&
507                    ((struct ebt_entries *)e)->policy != EBT_ACCEPT) {
508                         /* only RETURN from udc */
509                         if (i != NF_BR_NUMHOOKS ||
510                            ((struct ebt_entries *)e)->policy != EBT_RETURN)
511                                 return -EINVAL;
512                 }
513                 if (i == NF_BR_NUMHOOKS) /* it's a user defined chain */
514                         (*udc_cnt)++;
515                 if (((struct ebt_entries *)e)->counter_offset != *totalcnt)
516                         return -EINVAL;
517                 *n = ((struct ebt_entries *)e)->nentries;
518                 *cnt = 0;
519                 return 0;
520         }
521         /* a plain old entry, heh */
522         if (sizeof(struct ebt_entry) > e->watchers_offset ||
523            e->watchers_offset > e->target_offset ||
524            e->target_offset >= e->next_offset)
525                 return -EINVAL;
526
527         /* this is not checked anywhere else */
528         if (e->next_offset - e->target_offset < sizeof(struct ebt_entry_target))
529                 return -EINVAL;
530
531         (*cnt)++;
532         (*totalcnt)++;
533         return 0;
534 }
535
536 struct ebt_cl_stack {
537         struct ebt_chainstack cs;
538         int from;
539         unsigned int hookmask;
540 };
541
542 /* We need these positions to check that the jumps to a different part of the
543  * entries is a jump to the beginning of a new chain.
544  */
545 static inline int
546 ebt_get_udc_positions(struct ebt_entry *e, struct ebt_table_info *newinfo,
547                       unsigned int *n, struct ebt_cl_stack *udc)
548 {
549         int i;
550
551         /* we're only interested in chain starts */
552         if (e->bitmask)
553                 return 0;
554         for (i = 0; i < NF_BR_NUMHOOKS; i++) {
555                 if (newinfo->hook_entry[i] == (struct ebt_entries *)e)
556                         break;
557         }
558         /* only care about udc */
559         if (i != NF_BR_NUMHOOKS)
560                 return 0;
561
562         udc[*n].cs.chaininfo = (struct ebt_entries *)e;
563         /* these initialisations are depended on later in check_chainloops() */
564         udc[*n].cs.n = 0;
565         udc[*n].hookmask = 0;
566
567         (*n)++;
568         return 0;
569 }
570
571 static inline int
572 ebt_cleanup_match(struct ebt_entry_match *m, struct net *net, unsigned int *i)
573 {
574         struct xt_mtdtor_param par;
575
576         if (i && (*i)-- == 0)
577                 return 1;
578
579         par.net       = net;
580         par.match     = m->u.match;
581         par.matchinfo = m->data;
582         par.family    = NFPROTO_BRIDGE;
583         if (par.match->destroy != NULL)
584                 par.match->destroy(&par);
585         module_put(par.match->me);
586         return 0;
587 }
588
589 static inline int
590 ebt_cleanup_watcher(struct ebt_entry_watcher *w, struct net *net, unsigned int *i)
591 {
592         struct xt_tgdtor_param par;
593
594         if (i && (*i)-- == 0)
595                 return 1;
596
597         par.net      = net;
598         par.target   = w->u.watcher;
599         par.targinfo = w->data;
600         par.family   = NFPROTO_BRIDGE;
601         if (par.target->destroy != NULL)
602                 par.target->destroy(&par);
603         module_put(par.target->me);
604         return 0;
605 }
606
607 static inline int
608 ebt_cleanup_entry(struct ebt_entry *e, struct net *net, unsigned int *cnt)
609 {
610         struct xt_tgdtor_param par;
611         struct ebt_entry_target *t;
612
613         if (e->bitmask == 0)
614                 return 0;
615         /* we're done */
616         if (cnt && (*cnt)-- == 0)
617                 return 1;
618         EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, net, NULL);
619         EBT_MATCH_ITERATE(e, ebt_cleanup_match, net, NULL);
620         t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
621
622         par.net      = net;
623         par.target   = t->u.target;
624         par.targinfo = t->data;
625         par.family   = NFPROTO_BRIDGE;
626         if (par.target->destroy != NULL)
627                 par.target->destroy(&par);
628         module_put(par.target->me);
629         return 0;
630 }
631
632 static inline int
633 ebt_check_entry(struct ebt_entry *e, struct net *net,
634                 const struct ebt_table_info *newinfo,
635                 const char *name, unsigned int *cnt,
636                 struct ebt_cl_stack *cl_s, unsigned int udc_cnt)
637 {
638         struct ebt_entry_target *t;
639         struct xt_target *target;
640         unsigned int i, j, hook = 0, hookmask = 0;
641         size_t gap;
642         int ret;
643         struct xt_mtchk_param mtpar;
644         struct xt_tgchk_param tgpar;
645
646         /* don't mess with the struct ebt_entries */
647         if (e->bitmask == 0)
648                 return 0;
649
650         if (e->bitmask & ~EBT_F_MASK)
651                 return -EINVAL;
652
653         if (e->invflags & ~EBT_INV_MASK)
654                 return -EINVAL;
655
656         if ((e->bitmask & EBT_NOPROTO) && (e->bitmask & EBT_802_3))
657                 return -EINVAL;
658
659         /* what hook do we belong to? */
660         for (i = 0; i < NF_BR_NUMHOOKS; i++) {
661                 if (!newinfo->hook_entry[i])
662                         continue;
663                 if ((char *)newinfo->hook_entry[i] < (char *)e)
664                         hook = i;
665                 else
666                         break;
667         }
668         /* (1 << NF_BR_NUMHOOKS) tells the check functions the rule is on
669          * a base chain
670          */
671         if (i < NF_BR_NUMHOOKS)
672                 hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
673         else {
674                 for (i = 0; i < udc_cnt; i++)
675                         if ((char *)(cl_s[i].cs.chaininfo) > (char *)e)
676                                 break;
677                 if (i == 0)
678                         hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
679                 else
680                         hookmask = cl_s[i - 1].hookmask;
681         }
682         i = 0;
683
684         memset(&mtpar, 0, sizeof(mtpar));
685         memset(&tgpar, 0, sizeof(tgpar));
686         mtpar.net       = tgpar.net       = net;
687         mtpar.table     = tgpar.table     = name;
688         mtpar.entryinfo = tgpar.entryinfo = e;
689         mtpar.hook_mask = tgpar.hook_mask = hookmask;
690         mtpar.family    = tgpar.family    = NFPROTO_BRIDGE;
691         ret = EBT_MATCH_ITERATE(e, ebt_check_match, &mtpar, &i);
692         if (ret != 0)
693                 goto cleanup_matches;
694         j = 0;
695         ret = EBT_WATCHER_ITERATE(e, ebt_check_watcher, &tgpar, &j);
696         if (ret != 0)
697                 goto cleanup_watchers;
698         t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
699         gap = e->next_offset - e->target_offset;
700
701         target = xt_request_find_target(NFPROTO_BRIDGE, t->u.name, 0);
702         if (IS_ERR(target)) {
703                 ret = PTR_ERR(target);
704                 goto cleanup_watchers;
705         }
706
707         /* Reject UNSPEC, xtables verdicts/return values are incompatible */
708         if (target->family != NFPROTO_BRIDGE) {
709                 module_put(target->me);
710                 ret = -ENOENT;
711                 goto cleanup_watchers;
712         }
713
714         t->u.target = target;
715         if (t->u.target == &ebt_standard_target) {
716                 if (gap < sizeof(struct ebt_standard_target)) {
717                         ret = -EFAULT;
718                         goto cleanup_watchers;
719                 }
720                 if (((struct ebt_standard_target *)t)->verdict <
721                    -NUM_STANDARD_TARGETS) {
722                         ret = -EFAULT;
723                         goto cleanup_watchers;
724                 }
725         } else if (t->target_size > gap - sizeof(struct ebt_entry_target)) {
726                 module_put(t->u.target->me);
727                 ret = -EFAULT;
728                 goto cleanup_watchers;
729         }
730
731         tgpar.target   = target;
732         tgpar.targinfo = t->data;
733         ret = xt_check_target(&tgpar, t->target_size,
734               e->ethproto, e->invflags & EBT_IPROTO);
735         if (ret < 0) {
736                 module_put(target->me);
737                 goto cleanup_watchers;
738         }
739         (*cnt)++;
740         return 0;
741 cleanup_watchers:
742         EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, net, &j);
743 cleanup_matches:
744         EBT_MATCH_ITERATE(e, ebt_cleanup_match, net, &i);
745         return ret;
746 }
747
748 /* checks for loops and sets the hook mask for udc
749  * the hook mask for udc tells us from which base chains the udc can be
750  * accessed. This mask is a parameter to the check() functions of the extensions
751  */
752 static int check_chainloops(const struct ebt_entries *chain, struct ebt_cl_stack *cl_s,
753                             unsigned int udc_cnt, unsigned int hooknr, char *base)
754 {
755         int i, chain_nr = -1, pos = 0, nentries = chain->nentries, verdict;
756         const struct ebt_entry *e = (struct ebt_entry *)chain->data;
757         const struct ebt_entry_target *t;
758
759         while (pos < nentries || chain_nr != -1) {
760                 /* end of udc, go back one 'recursion' step */
761                 if (pos == nentries) {
762                         /* put back values of the time when this chain was called */
763                         e = cl_s[chain_nr].cs.e;
764                         if (cl_s[chain_nr].from != -1)
765                                 nentries =
766                                 cl_s[cl_s[chain_nr].from].cs.chaininfo->nentries;
767                         else
768                                 nentries = chain->nentries;
769                         pos = cl_s[chain_nr].cs.n;
770                         /* make sure we won't see a loop that isn't one */
771                         cl_s[chain_nr].cs.n = 0;
772                         chain_nr = cl_s[chain_nr].from;
773                         if (pos == nentries)
774                                 continue;
775                 }
776                 t = (struct ebt_entry_target *)
777                    (((char *)e) + e->target_offset);
778                 if (strcmp(t->u.name, EBT_STANDARD_TARGET))
779                         goto letscontinue;
780                 if (e->target_offset + sizeof(struct ebt_standard_target) >
781                    e->next_offset)
782                         return -1;
783
784                 verdict = ((struct ebt_standard_target *)t)->verdict;
785                 if (verdict >= 0) { /* jump to another chain */
786                         struct ebt_entries *hlp2 =
787                            (struct ebt_entries *)(base + verdict);
788                         for (i = 0; i < udc_cnt; i++)
789                                 if (hlp2 == cl_s[i].cs.chaininfo)
790                                         break;
791                         /* bad destination or loop */
792                         if (i == udc_cnt)
793                                 return -1;
794
795                         if (cl_s[i].cs.n)
796                                 return -1;
797
798                         if (cl_s[i].hookmask & (1 << hooknr))
799                                 goto letscontinue;
800                         /* this can't be 0, so the loop test is correct */
801                         cl_s[i].cs.n = pos + 1;
802                         pos = 0;
803                         cl_s[i].cs.e = ebt_next_entry(e);
804                         e = (struct ebt_entry *)(hlp2->data);
805                         nentries = hlp2->nentries;
806                         cl_s[i].from = chain_nr;
807                         chain_nr = i;
808                         /* this udc is accessible from the base chain for hooknr */
809                         cl_s[i].hookmask |= (1 << hooknr);
810                         continue;
811                 }
812 letscontinue:
813                 e = ebt_next_entry(e);
814                 pos++;
815         }
816         return 0;
817 }
818
819 /* do the parsing of the table/chains/entries/matches/watchers/targets, heh */
820 static int translate_table(struct net *net, const char *name,
821                            struct ebt_table_info *newinfo)
822 {
823         unsigned int i, j, k, udc_cnt;
824         int ret;
825         struct ebt_cl_stack *cl_s = NULL; /* used in the checking for chain loops */
826
827         i = 0;
828         while (i < NF_BR_NUMHOOKS && !newinfo->hook_entry[i])
829                 i++;
830         if (i == NF_BR_NUMHOOKS)
831                 return -EINVAL;
832
833         if (newinfo->hook_entry[i] != (struct ebt_entries *)newinfo->entries)
834                 return -EINVAL;
835
836         /* make sure chains are ordered after each other in same order
837          * as their corresponding hooks
838          */
839         for (j = i + 1; j < NF_BR_NUMHOOKS; j++) {
840                 if (!newinfo->hook_entry[j])
841                         continue;
842                 if (newinfo->hook_entry[j] <= newinfo->hook_entry[i])
843                         return -EINVAL;
844
845                 i = j;
846         }
847
848         /* do some early checkings and initialize some things */
849         i = 0; /* holds the expected nr. of entries for the chain */
850         j = 0; /* holds the up to now counted entries for the chain */
851         k = 0; /* holds the total nr. of entries, should equal
852                 * newinfo->nentries afterwards
853                 */
854         udc_cnt = 0; /* will hold the nr. of user defined chains (udc) */
855         ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
856            ebt_check_entry_size_and_hooks, newinfo,
857            &i, &j, &k, &udc_cnt);
858
859         if (ret != 0)
860                 return ret;
861
862         if (i != j)
863                 return -EINVAL;
864
865         if (k != newinfo->nentries)
866                 return -EINVAL;
867
868         /* get the location of the udc, put them in an array
869          * while we're at it, allocate the chainstack
870          */
871         if (udc_cnt) {
872                 /* this will get free'd in do_replace()/ebt_register_table()
873                  * if an error occurs
874                  */
875                 newinfo->chainstack =
876                         vmalloc(nr_cpu_ids * sizeof(*(newinfo->chainstack)));
877                 if (!newinfo->chainstack)
878                         return -ENOMEM;
879                 for_each_possible_cpu(i) {
880                         newinfo->chainstack[i] =
881                           vmalloc(udc_cnt * sizeof(*(newinfo->chainstack[0])));
882                         if (!newinfo->chainstack[i]) {
883                                 while (i)
884                                         vfree(newinfo->chainstack[--i]);
885                                 vfree(newinfo->chainstack);
886                                 newinfo->chainstack = NULL;
887                                 return -ENOMEM;
888                         }
889                 }
890
891                 cl_s = vmalloc(udc_cnt * sizeof(*cl_s));
892                 if (!cl_s)
893                         return -ENOMEM;
894                 i = 0; /* the i'th udc */
895                 EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
896                    ebt_get_udc_positions, newinfo, &i, cl_s);
897                 /* sanity check */
898                 if (i != udc_cnt) {
899                         vfree(cl_s);
900                         return -EFAULT;
901                 }
902         }
903
904         /* Check for loops */
905         for (i = 0; i < NF_BR_NUMHOOKS; i++)
906                 if (newinfo->hook_entry[i])
907                         if (check_chainloops(newinfo->hook_entry[i],
908                            cl_s, udc_cnt, i, newinfo->entries)) {
909                                 vfree(cl_s);
910                                 return -EINVAL;
911                         }
912
913         /* we now know the following (along with E=mc²):
914          *  - the nr of entries in each chain is right
915          *  - the size of the allocated space is right
916          *  - all valid hooks have a corresponding chain
917          *  - there are no loops
918          *  - wrong data can still be on the level of a single entry
919          *  - could be there are jumps to places that are not the
920          *    beginning of a chain. This can only occur in chains that
921          *    are not accessible from any base chains, so we don't care.
922          */
923
924         /* used to know what we need to clean up if something goes wrong */
925         i = 0;
926         ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
927            ebt_check_entry, net, newinfo, name, &i, cl_s, udc_cnt);
928         if (ret != 0) {
929                 EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
930                                   ebt_cleanup_entry, net, &i);
931         }
932         vfree(cl_s);
933         return ret;
934 }
935
936 /* called under write_lock */
937 static void get_counters(const struct ebt_counter *oldcounters,
938                          struct ebt_counter *counters, unsigned int nentries)
939 {
940         int i, cpu;
941         struct ebt_counter *counter_base;
942
943         /* counters of cpu 0 */
944         memcpy(counters, oldcounters,
945                sizeof(struct ebt_counter) * nentries);
946
947         /* add other counters to those of cpu 0 */
948         for_each_possible_cpu(cpu) {
949                 if (cpu == 0)
950                         continue;
951                 counter_base = COUNTER_BASE(oldcounters, nentries, cpu);
952                 for (i = 0; i < nentries; i++) {
953                         counters[i].pcnt += counter_base[i].pcnt;
954                         counters[i].bcnt += counter_base[i].bcnt;
955                 }
956         }
957 }
958
959 static int do_replace_finish(struct net *net, struct ebt_replace *repl,
960                               struct ebt_table_info *newinfo)
961 {
962         int ret, i;
963         struct ebt_counter *counterstmp = NULL;
964         /* used to be able to unlock earlier */
965         struct ebt_table_info *table;
966         struct ebt_table *t;
967
968         /* the user wants counters back
969          * the check on the size is done later, when we have the lock
970          */
971         if (repl->num_counters) {
972                 unsigned long size = repl->num_counters * sizeof(*counterstmp);
973                 counterstmp = vmalloc(size);
974                 if (!counterstmp)
975                         return -ENOMEM;
976         }
977
978         newinfo->chainstack = NULL;
979         ret = ebt_verify_pointers(repl, newinfo);
980         if (ret != 0)
981                 goto free_counterstmp;
982
983         ret = translate_table(net, repl->name, newinfo);
984
985         if (ret != 0)
986                 goto free_counterstmp;
987
988         t = find_table_lock(net, repl->name, &ret, &ebt_mutex);
989         if (!t) {
990                 ret = -ENOENT;
991                 goto free_iterate;
992         }
993
994         if (repl->valid_hooks != t->valid_hooks)
995                 goto free_unlock;
996
997         if (repl->num_counters && repl->num_counters != t->private->nentries) {
998                 ret = -EINVAL;
999                 goto free_unlock;
1000         }
1001
1002         /* we have the mutex lock, so no danger in reading this pointer */
1003         table = t->private;
1004         /* make sure the table can only be rmmod'ed if it contains no rules */
1005         if (!table->nentries && newinfo->nentries && !try_module_get(t->me)) {
1006                 ret = -ENOENT;
1007                 goto free_unlock;
1008         } else if (table->nentries && !newinfo->nentries)
1009                 module_put(t->me);
1010         /* we need an atomic snapshot of the counters */
1011         write_lock_bh(&t->lock);
1012         if (repl->num_counters)
1013                 get_counters(t->private->counters, counterstmp,
1014                    t->private->nentries);
1015
1016         t->private = newinfo;
1017         write_unlock_bh(&t->lock);
1018         mutex_unlock(&ebt_mutex);
1019         /* so, a user can change the chains while having messed up her counter
1020          * allocation. Only reason why this is done is because this way the lock
1021          * is held only once, while this doesn't bring the kernel into a
1022          * dangerous state.
1023          */
1024         if (repl->num_counters &&
1025            copy_to_user(repl->counters, counterstmp,
1026            repl->num_counters * sizeof(struct ebt_counter))) {
1027                 /* Silent error, can't fail, new table is already in place */
1028                 net_warn_ratelimited("ebtables: counters copy to user failed while replacing table\n");
1029         }
1030
1031         /* decrease module count and free resources */
1032         EBT_ENTRY_ITERATE(table->entries, table->entries_size,
1033                           ebt_cleanup_entry, net, NULL);
1034
1035         vfree(table->entries);
1036         if (table->chainstack) {
1037                 for_each_possible_cpu(i)
1038                         vfree(table->chainstack[i]);
1039                 vfree(table->chainstack);
1040         }
1041         vfree(table);
1042
1043         vfree(counterstmp);
1044
1045 #ifdef CONFIG_AUDIT
1046         if (audit_enabled) {
1047                 audit_log(current->audit_context, GFP_KERNEL,
1048                           AUDIT_NETFILTER_CFG,
1049                           "table=%s family=%u entries=%u",
1050                           repl->name, AF_BRIDGE, repl->nentries);
1051         }
1052 #endif
1053         return ret;
1054
1055 free_unlock:
1056         mutex_unlock(&ebt_mutex);
1057 free_iterate:
1058         EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
1059                           ebt_cleanup_entry, net, NULL);
1060 free_counterstmp:
1061         vfree(counterstmp);
1062         /* can be initialized in translate_table() */
1063         if (newinfo->chainstack) {
1064                 for_each_possible_cpu(i)
1065                         vfree(newinfo->chainstack[i]);
1066                 vfree(newinfo->chainstack);
1067         }
1068         return ret;
1069 }
1070
1071 /* replace the table */
1072 static int do_replace(struct net *net, const void __user *user,
1073                       unsigned int len)
1074 {
1075         int ret, countersize;
1076         struct ebt_table_info *newinfo;
1077         struct ebt_replace tmp;
1078
1079         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1080                 return -EFAULT;
1081
1082         if (len != sizeof(tmp) + tmp.entries_size)
1083                 return -EINVAL;
1084
1085         if (tmp.entries_size == 0)
1086                 return -EINVAL;
1087
1088         /* overflow check */
1089         if (tmp.nentries >= ((INT_MAX - sizeof(struct ebt_table_info)) /
1090                         NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter))
1091                 return -ENOMEM;
1092         if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter))
1093                 return -ENOMEM;
1094
1095         tmp.name[sizeof(tmp.name) - 1] = 0;
1096
1097         countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids;
1098         newinfo = __vmalloc(sizeof(*newinfo) + countersize, GFP_KERNEL_ACCOUNT,
1099                             PAGE_KERNEL);
1100         if (!newinfo)
1101                 return -ENOMEM;
1102
1103         if (countersize)
1104                 memset(newinfo->counters, 0, countersize);
1105
1106         newinfo->entries = __vmalloc(tmp.entries_size, GFP_KERNEL_ACCOUNT,
1107                                      PAGE_KERNEL);
1108         if (!newinfo->entries) {
1109                 ret = -ENOMEM;
1110                 goto free_newinfo;
1111         }
1112         if (copy_from_user(
1113            newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
1114                 ret = -EFAULT;
1115                 goto free_entries;
1116         }
1117
1118         ret = do_replace_finish(net, &tmp, newinfo);
1119         if (ret == 0)
1120                 return ret;
1121 free_entries:
1122         vfree(newinfo->entries);
1123 free_newinfo:
1124         vfree(newinfo);
1125         return ret;
1126 }
1127
1128 static void __ebt_unregister_table(struct net *net, struct ebt_table *table)
1129 {
1130         int i;
1131
1132         mutex_lock(&ebt_mutex);
1133         list_del(&table->list);
1134         mutex_unlock(&ebt_mutex);
1135         EBT_ENTRY_ITERATE(table->private->entries, table->private->entries_size,
1136                           ebt_cleanup_entry, net, NULL);
1137         if (table->private->nentries)
1138                 module_put(table->me);
1139         vfree(table->private->entries);
1140         if (table->private->chainstack) {
1141                 for_each_possible_cpu(i)
1142                         vfree(table->private->chainstack[i]);
1143                 vfree(table->private->chainstack);
1144         }
1145         vfree(table->private);
1146         kfree(table);
1147 }
1148
1149 int ebt_register_table(struct net *net, const struct ebt_table *input_table,
1150                        const struct nf_hook_ops *ops, struct ebt_table **res)
1151 {
1152         struct ebt_table_info *newinfo;
1153         struct ebt_table *t, *table;
1154         struct ebt_replace_kernel *repl;
1155         int ret, i, countersize;
1156         void *p;
1157
1158         if (input_table == NULL || (repl = input_table->table) == NULL ||
1159             repl->entries == NULL || repl->entries_size == 0 ||
1160             repl->counters != NULL || input_table->private != NULL)
1161                 return -EINVAL;
1162
1163         /* Don't add one table to multiple lists. */
1164         table = kmemdup(input_table, sizeof(struct ebt_table), GFP_KERNEL);
1165         if (!table) {
1166                 ret = -ENOMEM;
1167                 goto out;
1168         }
1169
1170         countersize = COUNTER_OFFSET(repl->nentries) * nr_cpu_ids;
1171         newinfo = vmalloc(sizeof(*newinfo) + countersize);
1172         ret = -ENOMEM;
1173         if (!newinfo)
1174                 goto free_table;
1175
1176         p = vmalloc(repl->entries_size);
1177         if (!p)
1178                 goto free_newinfo;
1179
1180         memcpy(p, repl->entries, repl->entries_size);
1181         newinfo->entries = p;
1182
1183         newinfo->entries_size = repl->entries_size;
1184         newinfo->nentries = repl->nentries;
1185
1186         if (countersize)
1187                 memset(newinfo->counters, 0, countersize);
1188
1189         /* fill in newinfo and parse the entries */
1190         newinfo->chainstack = NULL;
1191         for (i = 0; i < NF_BR_NUMHOOKS; i++) {
1192                 if ((repl->valid_hooks & (1 << i)) == 0)
1193                         newinfo->hook_entry[i] = NULL;
1194                 else
1195                         newinfo->hook_entry[i] = p +
1196                                 ((char *)repl->hook_entry[i] - repl->entries);
1197         }
1198         ret = translate_table(net, repl->name, newinfo);
1199         if (ret != 0)
1200                 goto free_chainstack;
1201
1202         table->private = newinfo;
1203         rwlock_init(&table->lock);
1204         mutex_lock(&ebt_mutex);
1205         list_for_each_entry(t, &net->xt.tables[NFPROTO_BRIDGE], list) {
1206                 if (strcmp(t->name, table->name) == 0) {
1207                         ret = -EEXIST;
1208                         goto free_unlock;
1209                 }
1210         }
1211
1212         /* Hold a reference count if the chains aren't empty */
1213         if (newinfo->nentries && !try_module_get(table->me)) {
1214                 ret = -ENOENT;
1215                 goto free_unlock;
1216         }
1217         list_add(&table->list, &net->xt.tables[NFPROTO_BRIDGE]);
1218         mutex_unlock(&ebt_mutex);
1219
1220         WRITE_ONCE(*res, table);
1221
1222         if (!ops)
1223                 return 0;
1224
1225         ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1226         if (ret) {
1227                 __ebt_unregister_table(net, table);
1228                 *res = NULL;
1229         }
1230
1231         return ret;
1232 free_unlock:
1233         mutex_unlock(&ebt_mutex);
1234 free_chainstack:
1235         if (newinfo->chainstack) {
1236                 for_each_possible_cpu(i)
1237                         vfree(newinfo->chainstack[i]);
1238                 vfree(newinfo->chainstack);
1239         }
1240         vfree(newinfo->entries);
1241 free_newinfo:
1242         vfree(newinfo);
1243 free_table:
1244         kfree(table);
1245 out:
1246         return ret;
1247 }
1248
1249 void ebt_unregister_table(struct net *net, struct ebt_table *table,
1250                           const struct nf_hook_ops *ops)
1251 {
1252         if (ops)
1253                 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1254         __ebt_unregister_table(net, table);
1255 }
1256
1257 /* userspace just supplied us with counters */
1258 static int do_update_counters(struct net *net, const char *name,
1259                                 struct ebt_counter __user *counters,
1260                                 unsigned int num_counters,
1261                                 const void __user *user, unsigned int len)
1262 {
1263         int i, ret;
1264         struct ebt_counter *tmp;
1265         struct ebt_table *t;
1266
1267         if (num_counters == 0)
1268                 return -EINVAL;
1269
1270         tmp = vmalloc(num_counters * sizeof(*tmp));
1271         if (!tmp)
1272                 return -ENOMEM;
1273
1274         t = find_table_lock(net, name, &ret, &ebt_mutex);
1275         if (!t)
1276                 goto free_tmp;
1277
1278         if (num_counters != t->private->nentries) {
1279                 ret = -EINVAL;
1280                 goto unlock_mutex;
1281         }
1282
1283         if (copy_from_user(tmp, counters, num_counters * sizeof(*counters))) {
1284                 ret = -EFAULT;
1285                 goto unlock_mutex;
1286         }
1287
1288         /* we want an atomic add of the counters */
1289         write_lock_bh(&t->lock);
1290
1291         /* we add to the counters of the first cpu */
1292         for (i = 0; i < num_counters; i++) {
1293                 t->private->counters[i].pcnt += tmp[i].pcnt;
1294                 t->private->counters[i].bcnt += tmp[i].bcnt;
1295         }
1296
1297         write_unlock_bh(&t->lock);
1298         ret = 0;
1299 unlock_mutex:
1300         mutex_unlock(&ebt_mutex);
1301 free_tmp:
1302         vfree(tmp);
1303         return ret;
1304 }
1305
1306 static int update_counters(struct net *net, const void __user *user,
1307                             unsigned int len)
1308 {
1309         struct ebt_replace hlp;
1310
1311         if (copy_from_user(&hlp, user, sizeof(hlp)))
1312                 return -EFAULT;
1313
1314         if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
1315                 return -EINVAL;
1316
1317         return do_update_counters(net, hlp.name, hlp.counters,
1318                                 hlp.num_counters, user, len);
1319 }
1320
1321 static inline int ebt_obj_to_user(char __user *um, const char *_name,
1322                                   const char *data, int entrysize,
1323                                   int usersize, int datasize)
1324 {
1325         char name[EBT_FUNCTION_MAXNAMELEN] = {0};
1326
1327         /* ebtables expects 32 bytes long names but xt_match names are 29 bytes
1328          * long. Copy 29 bytes and fill remaining bytes with zeroes.
1329          */
1330         strlcpy(name, _name, sizeof(name));
1331         if (copy_to_user(um, name, EBT_FUNCTION_MAXNAMELEN) ||
1332             put_user(datasize, (int __user *)(um + EBT_FUNCTION_MAXNAMELEN)) ||
1333             xt_data_to_user(um + entrysize, data, usersize, datasize,
1334                             XT_ALIGN(datasize)))
1335                 return -EFAULT;
1336
1337         return 0;
1338 }
1339
1340 static inline int ebt_match_to_user(const struct ebt_entry_match *m,
1341                                     const char *base, char __user *ubase)
1342 {
1343         return ebt_obj_to_user(ubase + ((char *)m - base),
1344                                m->u.match->name, m->data, sizeof(*m),
1345                                m->u.match->usersize, m->match_size);
1346 }
1347
1348 static inline int ebt_watcher_to_user(const struct ebt_entry_watcher *w,
1349                                       const char *base, char __user *ubase)
1350 {
1351         return ebt_obj_to_user(ubase + ((char *)w - base),
1352                                w->u.watcher->name, w->data, sizeof(*w),
1353                                w->u.watcher->usersize, w->watcher_size);
1354 }
1355
1356 static inline int ebt_entry_to_user(struct ebt_entry *e, const char *base,
1357                                     char __user *ubase)
1358 {
1359         int ret;
1360         char __user *hlp;
1361         const struct ebt_entry_target *t;
1362
1363         if (e->bitmask == 0) {
1364                 /* special case !EBT_ENTRY_OR_ENTRIES */
1365                 if (copy_to_user(ubase + ((char *)e - base), e,
1366                                  sizeof(struct ebt_entries)))
1367                         return -EFAULT;
1368                 return 0;
1369         }
1370
1371         if (copy_to_user(ubase + ((char *)e - base), e, sizeof(*e)))
1372                 return -EFAULT;
1373
1374         hlp = ubase + (((char *)e + e->target_offset) - base);
1375         t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
1376
1377         ret = EBT_MATCH_ITERATE(e, ebt_match_to_user, base, ubase);
1378         if (ret != 0)
1379                 return ret;
1380         ret = EBT_WATCHER_ITERATE(e, ebt_watcher_to_user, base, ubase);
1381         if (ret != 0)
1382                 return ret;
1383         ret = ebt_obj_to_user(hlp, t->u.target->name, t->data, sizeof(*t),
1384                               t->u.target->usersize, t->target_size);
1385         if (ret != 0)
1386                 return ret;
1387
1388         return 0;
1389 }
1390
1391 static int copy_counters_to_user(struct ebt_table *t,
1392                                  const struct ebt_counter *oldcounters,
1393                                  void __user *user, unsigned int num_counters,
1394                                  unsigned int nentries)
1395 {
1396         struct ebt_counter *counterstmp;
1397         int ret = 0;
1398
1399         /* userspace might not need the counters */
1400         if (num_counters == 0)
1401                 return 0;
1402
1403         if (num_counters != nentries)
1404                 return -EINVAL;
1405
1406         counterstmp = vmalloc(nentries * sizeof(*counterstmp));
1407         if (!counterstmp)
1408                 return -ENOMEM;
1409
1410         write_lock_bh(&t->lock);
1411         get_counters(oldcounters, counterstmp, nentries);
1412         write_unlock_bh(&t->lock);
1413
1414         if (copy_to_user(user, counterstmp,
1415            nentries * sizeof(struct ebt_counter)))
1416                 ret = -EFAULT;
1417         vfree(counterstmp);
1418         return ret;
1419 }
1420
1421 /* called with ebt_mutex locked */
1422 static int copy_everything_to_user(struct ebt_table *t, void __user *user,
1423                                    const int *len, int cmd)
1424 {
1425         struct ebt_replace tmp;
1426         const struct ebt_counter *oldcounters;
1427         unsigned int entries_size, nentries;
1428         int ret;
1429         char *entries;
1430
1431         if (cmd == EBT_SO_GET_ENTRIES) {
1432                 entries_size = t->private->entries_size;
1433                 nentries = t->private->nentries;
1434                 entries = t->private->entries;
1435                 oldcounters = t->private->counters;
1436         } else {
1437                 entries_size = t->table->entries_size;
1438                 nentries = t->table->nentries;
1439                 entries = t->table->entries;
1440                 oldcounters = t->table->counters;
1441         }
1442
1443         if (copy_from_user(&tmp, user, sizeof(tmp)))
1444                 return -EFAULT;
1445
1446         if (*len != sizeof(struct ebt_replace) + entries_size +
1447            (tmp.num_counters ? nentries * sizeof(struct ebt_counter) : 0))
1448                 return -EINVAL;
1449
1450         if (tmp.nentries != nentries)
1451                 return -EINVAL;
1452
1453         if (tmp.entries_size != entries_size)
1454                 return -EINVAL;
1455
1456         ret = copy_counters_to_user(t, oldcounters, tmp.counters,
1457                                         tmp.num_counters, nentries);
1458         if (ret)
1459                 return ret;
1460
1461         /* set the match/watcher/target names right */
1462         return EBT_ENTRY_ITERATE(entries, entries_size,
1463            ebt_entry_to_user, entries, tmp.entries);
1464 }
1465
1466 static int do_ebt_set_ctl(struct sock *sk,
1467         int cmd, void __user *user, unsigned int len)
1468 {
1469         int ret;
1470         struct net *net = sock_net(sk);
1471
1472         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1473                 return -EPERM;
1474
1475         switch (cmd) {
1476         case EBT_SO_SET_ENTRIES:
1477                 ret = do_replace(net, user, len);
1478                 break;
1479         case EBT_SO_SET_COUNTERS:
1480                 ret = update_counters(net, user, len);
1481                 break;
1482         default:
1483                 ret = -EINVAL;
1484         }
1485         return ret;
1486 }
1487
1488 static int do_ebt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1489 {
1490         int ret;
1491         struct ebt_replace tmp;
1492         struct ebt_table *t;
1493         struct net *net = sock_net(sk);
1494
1495         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1496                 return -EPERM;
1497
1498         if (copy_from_user(&tmp, user, sizeof(tmp)))
1499                 return -EFAULT;
1500
1501         tmp.name[sizeof(tmp.name) - 1] = '\0';
1502
1503         t = find_table_lock(net, tmp.name, &ret, &ebt_mutex);
1504         if (!t)
1505                 return ret;
1506
1507         switch (cmd) {
1508         case EBT_SO_GET_INFO:
1509         case EBT_SO_GET_INIT_INFO:
1510                 if (*len != sizeof(struct ebt_replace)) {
1511                         ret = -EINVAL;
1512                         mutex_unlock(&ebt_mutex);
1513                         break;
1514                 }
1515                 if (cmd == EBT_SO_GET_INFO) {
1516                         tmp.nentries = t->private->nentries;
1517                         tmp.entries_size = t->private->entries_size;
1518                         tmp.valid_hooks = t->valid_hooks;
1519                 } else {
1520                         tmp.nentries = t->table->nentries;
1521                         tmp.entries_size = t->table->entries_size;
1522                         tmp.valid_hooks = t->table->valid_hooks;
1523                 }
1524                 mutex_unlock(&ebt_mutex);
1525                 if (copy_to_user(user, &tmp, *len) != 0) {
1526                         ret = -EFAULT;
1527                         break;
1528                 }
1529                 ret = 0;
1530                 break;
1531
1532         case EBT_SO_GET_ENTRIES:
1533         case EBT_SO_GET_INIT_ENTRIES:
1534                 ret = copy_everything_to_user(t, user, len, cmd);
1535                 mutex_unlock(&ebt_mutex);
1536                 break;
1537
1538         default:
1539                 mutex_unlock(&ebt_mutex);
1540                 ret = -EINVAL;
1541         }
1542
1543         return ret;
1544 }
1545
1546 #ifdef CONFIG_COMPAT
1547 /* 32 bit-userspace compatibility definitions. */
1548 struct compat_ebt_replace {
1549         char name[EBT_TABLE_MAXNAMELEN];
1550         compat_uint_t valid_hooks;
1551         compat_uint_t nentries;
1552         compat_uint_t entries_size;
1553         /* start of the chains */
1554         compat_uptr_t hook_entry[NF_BR_NUMHOOKS];
1555         /* nr of counters userspace expects back */
1556         compat_uint_t num_counters;
1557         /* where the kernel will put the old counters. */
1558         compat_uptr_t counters;
1559         compat_uptr_t entries;
1560 };
1561
1562 /* struct ebt_entry_match, _target and _watcher have same layout */
1563 struct compat_ebt_entry_mwt {
1564         union {
1565                 char name[EBT_FUNCTION_MAXNAMELEN];
1566                 compat_uptr_t ptr;
1567         } u;
1568         compat_uint_t match_size;
1569         compat_uint_t data[0];
1570 };
1571
1572 /* account for possible padding between match_size and ->data */
1573 static int ebt_compat_entry_padsize(void)
1574 {
1575         BUILD_BUG_ON(XT_ALIGN(sizeof(struct ebt_entry_match)) <
1576                         COMPAT_XT_ALIGN(sizeof(struct compat_ebt_entry_mwt)));
1577         return (int) XT_ALIGN(sizeof(struct ebt_entry_match)) -
1578                         COMPAT_XT_ALIGN(sizeof(struct compat_ebt_entry_mwt));
1579 }
1580
1581 static int ebt_compat_match_offset(const struct xt_match *match,
1582                                    unsigned int userlen)
1583 {
1584         /* ebt_among needs special handling. The kernel .matchsize is
1585          * set to -1 at registration time; at runtime an EBT_ALIGN()ed
1586          * value is expected.
1587          * Example: userspace sends 4500, ebt_among.c wants 4504.
1588          */
1589         if (unlikely(match->matchsize == -1))
1590                 return XT_ALIGN(userlen) - COMPAT_XT_ALIGN(userlen);
1591         return xt_compat_match_offset(match);
1592 }
1593
1594 static int compat_match_to_user(struct ebt_entry_match *m, void __user **dstptr,
1595                                 unsigned int *size)
1596 {
1597         const struct xt_match *match = m->u.match;
1598         struct compat_ebt_entry_mwt __user *cm = *dstptr;
1599         int off = ebt_compat_match_offset(match, m->match_size);
1600         compat_uint_t msize = m->match_size - off;
1601
1602         if (WARN_ON(off >= m->match_size))
1603                 return -EINVAL;
1604
1605         if (copy_to_user(cm->u.name, match->name,
1606             strlen(match->name) + 1) || put_user(msize, &cm->match_size))
1607                 return -EFAULT;
1608
1609         if (match->compat_to_user) {
1610                 if (match->compat_to_user(cm->data, m->data))
1611                         return -EFAULT;
1612         } else {
1613                 if (xt_data_to_user(cm->data, m->data, match->usersize, msize,
1614                                     COMPAT_XT_ALIGN(msize)))
1615                         return -EFAULT;
1616         }
1617
1618         *size -= ebt_compat_entry_padsize() + off;
1619         *dstptr = cm->data;
1620         *dstptr += msize;
1621         return 0;
1622 }
1623
1624 static int compat_target_to_user(struct ebt_entry_target *t,
1625                                  void __user **dstptr,
1626                                  unsigned int *size)
1627 {
1628         const struct xt_target *target = t->u.target;
1629         struct compat_ebt_entry_mwt __user *cm = *dstptr;
1630         int off = xt_compat_target_offset(target);
1631         compat_uint_t tsize = t->target_size - off;
1632
1633         if (WARN_ON(off >= t->target_size))
1634                 return -EINVAL;
1635
1636         if (copy_to_user(cm->u.name, target->name,
1637             strlen(target->name) + 1) || put_user(tsize, &cm->match_size))
1638                 return -EFAULT;
1639
1640         if (target->compat_to_user) {
1641                 if (target->compat_to_user(cm->data, t->data))
1642                         return -EFAULT;
1643         } else {
1644                 if (xt_data_to_user(cm->data, t->data, target->usersize, tsize,
1645                                     COMPAT_XT_ALIGN(tsize)))
1646                         return -EFAULT;
1647         }
1648
1649         *size -= ebt_compat_entry_padsize() + off;
1650         *dstptr = cm->data;
1651         *dstptr += tsize;
1652         return 0;
1653 }
1654
1655 static int compat_watcher_to_user(struct ebt_entry_watcher *w,
1656                                   void __user **dstptr,
1657                                   unsigned int *size)
1658 {
1659         return compat_target_to_user((struct ebt_entry_target *)w,
1660                                                         dstptr, size);
1661 }
1662
1663 static int compat_copy_entry_to_user(struct ebt_entry *e, void __user **dstptr,
1664                                 unsigned int *size)
1665 {
1666         struct ebt_entry_target *t;
1667         struct ebt_entry __user *ce;
1668         u32 watchers_offset, target_offset, next_offset;
1669         compat_uint_t origsize;
1670         int ret;
1671
1672         if (e->bitmask == 0) {
1673                 if (*size < sizeof(struct ebt_entries))
1674                         return -EINVAL;
1675                 if (copy_to_user(*dstptr, e, sizeof(struct ebt_entries)))
1676                         return -EFAULT;
1677
1678                 *dstptr += sizeof(struct ebt_entries);
1679                 *size -= sizeof(struct ebt_entries);
1680                 return 0;
1681         }
1682
1683         if (*size < sizeof(*ce))
1684                 return -EINVAL;
1685
1686         ce = *dstptr;
1687         if (copy_to_user(ce, e, sizeof(*ce)))
1688                 return -EFAULT;
1689
1690         origsize = *size;
1691         *dstptr += sizeof(*ce);
1692
1693         ret = EBT_MATCH_ITERATE(e, compat_match_to_user, dstptr, size);
1694         if (ret)
1695                 return ret;
1696         watchers_offset = e->watchers_offset - (origsize - *size);
1697
1698         ret = EBT_WATCHER_ITERATE(e, compat_watcher_to_user, dstptr, size);
1699         if (ret)
1700                 return ret;
1701         target_offset = e->target_offset - (origsize - *size);
1702
1703         t = (struct ebt_entry_target *) ((char *) e + e->target_offset);
1704
1705         ret = compat_target_to_user(t, dstptr, size);
1706         if (ret)
1707                 return ret;
1708         next_offset = e->next_offset - (origsize - *size);
1709
1710         if (put_user(watchers_offset, &ce->watchers_offset) ||
1711             put_user(target_offset, &ce->target_offset) ||
1712             put_user(next_offset, &ce->next_offset))
1713                 return -EFAULT;
1714
1715         *size -= sizeof(*ce);
1716         return 0;
1717 }
1718
1719 static int compat_calc_match(struct ebt_entry_match *m, int *off)
1720 {
1721         *off += ebt_compat_match_offset(m->u.match, m->match_size);
1722         *off += ebt_compat_entry_padsize();
1723         return 0;
1724 }
1725
1726 static int compat_calc_watcher(struct ebt_entry_watcher *w, int *off)
1727 {
1728         *off += xt_compat_target_offset(w->u.watcher);
1729         *off += ebt_compat_entry_padsize();
1730         return 0;
1731 }
1732
1733 static int compat_calc_entry(const struct ebt_entry *e,
1734                              const struct ebt_table_info *info,
1735                              const void *base,
1736                              struct compat_ebt_replace *newinfo)
1737 {
1738         const struct ebt_entry_target *t;
1739         unsigned int entry_offset;
1740         int off, ret, i;
1741
1742         if (e->bitmask == 0)
1743                 return 0;
1744
1745         off = 0;
1746         entry_offset = (void *)e - base;
1747
1748         EBT_MATCH_ITERATE(e, compat_calc_match, &off);
1749         EBT_WATCHER_ITERATE(e, compat_calc_watcher, &off);
1750
1751         t = (const struct ebt_entry_target *) ((char *) e + e->target_offset);
1752
1753         off += xt_compat_target_offset(t->u.target);
1754         off += ebt_compat_entry_padsize();
1755
1756         newinfo->entries_size -= off;
1757
1758         ret = xt_compat_add_offset(NFPROTO_BRIDGE, entry_offset, off);
1759         if (ret)
1760                 return ret;
1761
1762         for (i = 0; i < NF_BR_NUMHOOKS; i++) {
1763                 const void *hookptr = info->hook_entry[i];
1764                 if (info->hook_entry[i] &&
1765                     (e < (struct ebt_entry *)(base - hookptr))) {
1766                         newinfo->hook_entry[i] -= off;
1767                         pr_debug("0x%08X -> 0x%08X\n",
1768                                         newinfo->hook_entry[i] + off,
1769                                         newinfo->hook_entry[i]);
1770                 }
1771         }
1772
1773         return 0;
1774 }
1775
1776 static int ebt_compat_init_offsets(unsigned int number)
1777 {
1778         if (number > INT_MAX)
1779                 return -EINVAL;
1780
1781         /* also count the base chain policies */
1782         number += NF_BR_NUMHOOKS;
1783
1784         return xt_compat_init_offsets(NFPROTO_BRIDGE, number);
1785 }
1786
1787 static int compat_table_info(const struct ebt_table_info *info,
1788                              struct compat_ebt_replace *newinfo)
1789 {
1790         unsigned int size = info->entries_size;
1791         const void *entries = info->entries;
1792         int ret;
1793
1794         newinfo->entries_size = size;
1795         ret = ebt_compat_init_offsets(info->nentries);
1796         if (ret)
1797                 return ret;
1798
1799         return EBT_ENTRY_ITERATE(entries, size, compat_calc_entry, info,
1800                                                         entries, newinfo);
1801 }
1802
1803 static int compat_copy_everything_to_user(struct ebt_table *t,
1804                                           void __user *user, int *len, int cmd)
1805 {
1806         struct compat_ebt_replace repl, tmp;
1807         struct ebt_counter *oldcounters;
1808         struct ebt_table_info tinfo;
1809         int ret;
1810         void __user *pos;
1811
1812         memset(&tinfo, 0, sizeof(tinfo));
1813
1814         if (cmd == EBT_SO_GET_ENTRIES) {
1815                 tinfo.entries_size = t->private->entries_size;
1816                 tinfo.nentries = t->private->nentries;
1817                 tinfo.entries = t->private->entries;
1818                 oldcounters = t->private->counters;
1819         } else {
1820                 tinfo.entries_size = t->table->entries_size;
1821                 tinfo.nentries = t->table->nentries;
1822                 tinfo.entries = t->table->entries;
1823                 oldcounters = t->table->counters;
1824         }
1825
1826         if (copy_from_user(&tmp, user, sizeof(tmp)))
1827                 return -EFAULT;
1828
1829         if (tmp.nentries != tinfo.nentries ||
1830            (tmp.num_counters && tmp.num_counters != tinfo.nentries))
1831                 return -EINVAL;
1832
1833         memcpy(&repl, &tmp, sizeof(repl));
1834         if (cmd == EBT_SO_GET_ENTRIES)
1835                 ret = compat_table_info(t->private, &repl);
1836         else
1837                 ret = compat_table_info(&tinfo, &repl);
1838         if (ret)
1839                 return ret;
1840
1841         if (*len != sizeof(tmp) + repl.entries_size +
1842            (tmp.num_counters? tinfo.nentries * sizeof(struct ebt_counter): 0)) {
1843                 pr_err("wrong size: *len %d, entries_size %u, replsz %d\n",
1844                                 *len, tinfo.entries_size, repl.entries_size);
1845                 return -EINVAL;
1846         }
1847
1848         /* userspace might not need the counters */
1849         ret = copy_counters_to_user(t, oldcounters, compat_ptr(tmp.counters),
1850                                         tmp.num_counters, tinfo.nentries);
1851         if (ret)
1852                 return ret;
1853
1854         pos = compat_ptr(tmp.entries);
1855         return EBT_ENTRY_ITERATE(tinfo.entries, tinfo.entries_size,
1856                         compat_copy_entry_to_user, &pos, &tmp.entries_size);
1857 }
1858
1859 struct ebt_entries_buf_state {
1860         char *buf_kern_start;   /* kernel buffer to copy (translated) data to */
1861         u32 buf_kern_len;       /* total size of kernel buffer */
1862         u32 buf_kern_offset;    /* amount of data copied so far */
1863         u32 buf_user_offset;    /* read position in userspace buffer */
1864 };
1865
1866 static int ebt_buf_count(struct ebt_entries_buf_state *state, unsigned int sz)
1867 {
1868         state->buf_kern_offset += sz;
1869         return state->buf_kern_offset >= sz ? 0 : -EINVAL;
1870 }
1871
1872 static int ebt_buf_add(struct ebt_entries_buf_state *state,
1873                        const void *data, unsigned int sz)
1874 {
1875         if (state->buf_kern_start == NULL)
1876                 goto count_only;
1877
1878         if (WARN_ON(state->buf_kern_offset + sz > state->buf_kern_len))
1879                 return -EINVAL;
1880
1881         memcpy(state->buf_kern_start + state->buf_kern_offset, data, sz);
1882
1883  count_only:
1884         state->buf_user_offset += sz;
1885         return ebt_buf_count(state, sz);
1886 }
1887
1888 static int ebt_buf_add_pad(struct ebt_entries_buf_state *state, unsigned int sz)
1889 {
1890         char *b = state->buf_kern_start;
1891
1892         if (WARN_ON(b && state->buf_kern_offset > state->buf_kern_len))
1893                 return -EINVAL;
1894
1895         if (b != NULL && sz > 0)
1896                 memset(b + state->buf_kern_offset, 0, sz);
1897         /* do not adjust ->buf_user_offset here, we added kernel-side padding */
1898         return ebt_buf_count(state, sz);
1899 }
1900
1901 enum compat_mwt {
1902         EBT_COMPAT_MATCH,
1903         EBT_COMPAT_WATCHER,
1904         EBT_COMPAT_TARGET,
1905 };
1906
1907 static int compat_mtw_from_user(const struct compat_ebt_entry_mwt *mwt,
1908                                 enum compat_mwt compat_mwt,
1909                                 struct ebt_entries_buf_state *state,
1910                                 const unsigned char *base)
1911 {
1912         char name[EBT_FUNCTION_MAXNAMELEN];
1913         struct xt_match *match;
1914         struct xt_target *wt;
1915         void *dst = NULL;
1916         int off, pad = 0;
1917         unsigned int size_kern, match_size = mwt->match_size;
1918
1919         if (strscpy(name, mwt->u.name, sizeof(name)) < 0)
1920                 return -EINVAL;
1921
1922         if (state->buf_kern_start)
1923                 dst = state->buf_kern_start + state->buf_kern_offset;
1924
1925         switch (compat_mwt) {
1926         case EBT_COMPAT_MATCH:
1927                 match = xt_request_find_match(NFPROTO_BRIDGE, name, 0);
1928                 if (IS_ERR(match))
1929                         return PTR_ERR(match);
1930
1931                 off = ebt_compat_match_offset(match, match_size);
1932                 if (dst) {
1933                         if (match->compat_from_user)
1934                                 match->compat_from_user(dst, mwt->data);
1935                         else
1936                                 memcpy(dst, mwt->data, match_size);
1937                 }
1938
1939                 size_kern = match->matchsize;
1940                 if (unlikely(size_kern == -1))
1941                         size_kern = match_size;
1942                 module_put(match->me);
1943                 break;
1944         case EBT_COMPAT_WATCHER: /* fallthrough */
1945         case EBT_COMPAT_TARGET:
1946                 wt = xt_request_find_target(NFPROTO_BRIDGE, name, 0);
1947                 if (IS_ERR(wt))
1948                         return PTR_ERR(wt);
1949                 off = xt_compat_target_offset(wt);
1950
1951                 if (dst) {
1952                         if (wt->compat_from_user)
1953                                 wt->compat_from_user(dst, mwt->data);
1954                         else
1955                                 memcpy(dst, mwt->data, match_size);
1956                 }
1957
1958                 size_kern = wt->targetsize;
1959                 module_put(wt->me);
1960                 break;
1961
1962         default:
1963                 return -EINVAL;
1964         }
1965
1966         state->buf_kern_offset += match_size + off;
1967         state->buf_user_offset += match_size;
1968         pad = XT_ALIGN(size_kern) - size_kern;
1969
1970         if (pad > 0 && dst) {
1971                 if (WARN_ON(state->buf_kern_len <= pad))
1972                         return -EINVAL;
1973                 if (WARN_ON(state->buf_kern_offset - (match_size + off) + size_kern > state->buf_kern_len - pad))
1974                         return -EINVAL;
1975                 memset(dst + size_kern, 0, pad);
1976         }
1977         return off + match_size;
1978 }
1979
1980 /* return size of all matches, watchers or target, including necessary
1981  * alignment and padding.
1982  */
1983 static int ebt_size_mwt(const struct compat_ebt_entry_mwt *match32,
1984                         unsigned int size_left, enum compat_mwt type,
1985                         struct ebt_entries_buf_state *state, const void *base)
1986 {
1987         const char *buf = (const char *)match32;
1988         int growth = 0;
1989
1990         if (size_left == 0)
1991                 return 0;
1992
1993         do {
1994                 struct ebt_entry_match *match_kern;
1995                 int ret;
1996
1997                 if (size_left < sizeof(*match32))
1998                         return -EINVAL;
1999
2000                 match_kern = (struct ebt_entry_match *) state->buf_kern_start;
2001                 if (match_kern) {
2002                         char *tmp;
2003                         tmp = state->buf_kern_start + state->buf_kern_offset;
2004                         match_kern = (struct ebt_entry_match *) tmp;
2005                 }
2006                 ret = ebt_buf_add(state, buf, sizeof(*match32));
2007                 if (ret < 0)
2008                         return ret;
2009                 size_left -= sizeof(*match32);
2010
2011                 /* add padding before match->data (if any) */
2012                 ret = ebt_buf_add_pad(state, ebt_compat_entry_padsize());
2013                 if (ret < 0)
2014                         return ret;
2015
2016                 if (match32->match_size > size_left)
2017                         return -EINVAL;
2018
2019                 size_left -= match32->match_size;
2020
2021                 ret = compat_mtw_from_user(match32, type, state, base);
2022                 if (ret < 0)
2023                         return ret;
2024
2025                 if (WARN_ON(ret < match32->match_size))
2026                         return -EINVAL;
2027                 growth += ret - match32->match_size;
2028                 growth += ebt_compat_entry_padsize();
2029
2030                 buf += sizeof(*match32);
2031                 buf += match32->match_size;
2032
2033                 if (match_kern)
2034                         match_kern->match_size = ret;
2035
2036                 match32 = (struct compat_ebt_entry_mwt *) buf;
2037         } while (size_left);
2038
2039         return growth;
2040 }
2041
2042 /* called for all ebt_entry structures. */
2043 static int size_entry_mwt(const struct ebt_entry *entry, const unsigned char *base,
2044                           unsigned int *total,
2045                           struct ebt_entries_buf_state *state)
2046 {
2047         unsigned int i, j, startoff, next_expected_off, new_offset = 0;
2048         /* stores match/watchers/targets & offset of next struct ebt_entry: */
2049         unsigned int offsets[4];
2050         unsigned int *offsets_update = NULL;
2051         int ret;
2052         char *buf_start;
2053
2054         if (*total < sizeof(struct ebt_entries))
2055                 return -EINVAL;
2056
2057         if (!entry->bitmask) {
2058                 *total -= sizeof(struct ebt_entries);
2059                 return ebt_buf_add(state, entry, sizeof(struct ebt_entries));
2060         }
2061         if (*total < sizeof(*entry) || entry->next_offset < sizeof(*entry))
2062                 return -EINVAL;
2063
2064         startoff = state->buf_user_offset;
2065         /* pull in most part of ebt_entry, it does not need to be changed. */
2066         ret = ebt_buf_add(state, entry,
2067                         offsetof(struct ebt_entry, watchers_offset));
2068         if (ret < 0)
2069                 return ret;
2070
2071         offsets[0] = sizeof(struct ebt_entry); /* matches come first */
2072         memcpy(&offsets[1], &entry->watchers_offset,
2073                         sizeof(offsets) - sizeof(offsets[0]));
2074
2075         if (state->buf_kern_start) {
2076                 buf_start = state->buf_kern_start + state->buf_kern_offset;
2077                 offsets_update = (unsigned int *) buf_start;
2078         }
2079         ret = ebt_buf_add(state, &offsets[1],
2080                         sizeof(offsets) - sizeof(offsets[0]));
2081         if (ret < 0)
2082                 return ret;
2083         buf_start = (char *) entry;
2084         /* 0: matches offset, always follows ebt_entry.
2085          * 1: watchers offset, from ebt_entry structure
2086          * 2: target offset, from ebt_entry structure
2087          * 3: next ebt_entry offset, from ebt_entry structure
2088          *
2089          * offsets are relative to beginning of struct ebt_entry (i.e., 0).
2090          */
2091         for (i = 0; i < 4 ; ++i) {
2092                 if (offsets[i] > *total)
2093                         return -EINVAL;
2094
2095                 if (i < 3 && offsets[i] == *total)
2096                         return -EINVAL;
2097
2098                 if (i == 0)
2099                         continue;
2100                 if (offsets[i-1] > offsets[i])
2101                         return -EINVAL;
2102         }
2103
2104         for (i = 0, j = 1 ; j < 4 ; j++, i++) {
2105                 struct compat_ebt_entry_mwt *match32;
2106                 unsigned int size;
2107                 char *buf = buf_start;
2108
2109                 buf = buf_start + offsets[i];
2110                 if (offsets[i] > offsets[j])
2111                         return -EINVAL;
2112
2113                 match32 = (struct compat_ebt_entry_mwt *) buf;
2114                 size = offsets[j] - offsets[i];
2115                 ret = ebt_size_mwt(match32, size, i, state, base);
2116                 if (ret < 0)
2117                         return ret;
2118                 new_offset += ret;
2119                 if (offsets_update && new_offset) {
2120                         pr_debug("change offset %d to %d\n",
2121                                 offsets_update[i], offsets[j] + new_offset);
2122                         offsets_update[i] = offsets[j] + new_offset;
2123                 }
2124         }
2125
2126         if (state->buf_kern_start == NULL) {
2127                 unsigned int offset = buf_start - (char *) base;
2128
2129                 ret = xt_compat_add_offset(NFPROTO_BRIDGE, offset, new_offset);
2130                 if (ret < 0)
2131                         return ret;
2132         }
2133
2134         next_expected_off = state->buf_user_offset - startoff;
2135         if (next_expected_off != entry->next_offset)
2136                 return -EINVAL;
2137
2138         if (*total < entry->next_offset)
2139                 return -EINVAL;
2140         *total -= entry->next_offset;
2141         return 0;
2142 }
2143
2144 /* repl->entries_size is the size of the ebt_entry blob in userspace.
2145  * It might need more memory when copied to a 64 bit kernel in case
2146  * userspace is 32-bit. So, first task: find out how much memory is needed.
2147  *
2148  * Called before validation is performed.
2149  */
2150 static int compat_copy_entries(unsigned char *data, unsigned int size_user,
2151                                 struct ebt_entries_buf_state *state)
2152 {
2153         unsigned int size_remaining = size_user;
2154         int ret;
2155
2156         ret = EBT_ENTRY_ITERATE(data, size_user, size_entry_mwt, data,
2157                                         &size_remaining, state);
2158         if (ret < 0)
2159                 return ret;
2160
2161         if (size_remaining)
2162                 return -EINVAL;
2163
2164         return state->buf_kern_offset;
2165 }
2166
2167
2168 static int compat_copy_ebt_replace_from_user(struct ebt_replace *repl,
2169                                             void __user *user, unsigned int len)
2170 {
2171         struct compat_ebt_replace tmp;
2172         int i;
2173
2174         if (len < sizeof(tmp))
2175                 return -EINVAL;
2176
2177         if (copy_from_user(&tmp, user, sizeof(tmp)))
2178                 return -EFAULT;
2179
2180         if (len != sizeof(tmp) + tmp.entries_size)
2181                 return -EINVAL;
2182
2183         if (tmp.entries_size == 0)
2184                 return -EINVAL;
2185
2186         if (tmp.nentries >= ((INT_MAX - sizeof(struct ebt_table_info)) /
2187                         NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter))
2188                 return -ENOMEM;
2189         if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter))
2190                 return -ENOMEM;
2191
2192         memcpy(repl, &tmp, offsetof(struct ebt_replace, hook_entry));
2193
2194         /* starting with hook_entry, 32 vs. 64 bit structures are different */
2195         for (i = 0; i < NF_BR_NUMHOOKS; i++)
2196                 repl->hook_entry[i] = compat_ptr(tmp.hook_entry[i]);
2197
2198         repl->num_counters = tmp.num_counters;
2199         repl->counters = compat_ptr(tmp.counters);
2200         repl->entries = compat_ptr(tmp.entries);
2201         return 0;
2202 }
2203
2204 static int compat_do_replace(struct net *net, void __user *user,
2205                              unsigned int len)
2206 {
2207         int ret, i, countersize, size64;
2208         struct ebt_table_info *newinfo;
2209         struct ebt_replace tmp;
2210         struct ebt_entries_buf_state state;
2211         void *entries_tmp;
2212
2213         ret = compat_copy_ebt_replace_from_user(&tmp, user, len);
2214         if (ret) {
2215                 /* try real handler in case userland supplied needed padding */
2216                 if (ret == -EINVAL && do_replace(net, user, len) == 0)
2217                         ret = 0;
2218                 return ret;
2219         }
2220
2221         countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids;
2222         newinfo = vmalloc(sizeof(*newinfo) + countersize);
2223         if (!newinfo)
2224                 return -ENOMEM;
2225
2226         if (countersize)
2227                 memset(newinfo->counters, 0, countersize);
2228
2229         memset(&state, 0, sizeof(state));
2230
2231         newinfo->entries = vmalloc(tmp.entries_size);
2232         if (!newinfo->entries) {
2233                 ret = -ENOMEM;
2234                 goto free_newinfo;
2235         }
2236         if (copy_from_user(
2237            newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
2238                 ret = -EFAULT;
2239                 goto free_entries;
2240         }
2241
2242         entries_tmp = newinfo->entries;
2243
2244         xt_compat_lock(NFPROTO_BRIDGE);
2245
2246         ret = ebt_compat_init_offsets(tmp.nentries);
2247         if (ret < 0)
2248                 goto out_unlock;
2249
2250         ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
2251         if (ret < 0)
2252                 goto out_unlock;
2253
2254         pr_debug("tmp.entries_size %d, kern off %d, user off %d delta %d\n",
2255                 tmp.entries_size, state.buf_kern_offset, state.buf_user_offset,
2256                 xt_compat_calc_jump(NFPROTO_BRIDGE, tmp.entries_size));
2257
2258         size64 = ret;
2259         newinfo->entries = vmalloc(size64);
2260         if (!newinfo->entries) {
2261                 vfree(entries_tmp);
2262                 ret = -ENOMEM;
2263                 goto out_unlock;
2264         }
2265
2266         memset(&state, 0, sizeof(state));
2267         state.buf_kern_start = newinfo->entries;
2268         state.buf_kern_len = size64;
2269
2270         ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
2271         if (WARN_ON(ret < 0)) {
2272                 vfree(entries_tmp);
2273                 goto out_unlock;
2274         }
2275
2276         vfree(entries_tmp);
2277         tmp.entries_size = size64;
2278
2279         for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2280                 char __user *usrptr;
2281                 if (tmp.hook_entry[i]) {
2282                         unsigned int delta;
2283                         usrptr = (char __user *) tmp.hook_entry[i];
2284                         delta = usrptr - tmp.entries;
2285                         usrptr += xt_compat_calc_jump(NFPROTO_BRIDGE, delta);
2286                         tmp.hook_entry[i] = (struct ebt_entries __user *)usrptr;
2287                 }
2288         }
2289
2290         xt_compat_flush_offsets(NFPROTO_BRIDGE);
2291         xt_compat_unlock(NFPROTO_BRIDGE);
2292
2293         ret = do_replace_finish(net, &tmp, newinfo);
2294         if (ret == 0)
2295                 return ret;
2296 free_entries:
2297         vfree(newinfo->entries);
2298 free_newinfo:
2299         vfree(newinfo);
2300         return ret;
2301 out_unlock:
2302         xt_compat_flush_offsets(NFPROTO_BRIDGE);
2303         xt_compat_unlock(NFPROTO_BRIDGE);
2304         goto free_entries;
2305 }
2306
2307 static int compat_update_counters(struct net *net, void __user *user,
2308                                   unsigned int len)
2309 {
2310         struct compat_ebt_replace hlp;
2311
2312         if (copy_from_user(&hlp, user, sizeof(hlp)))
2313                 return -EFAULT;
2314
2315         /* try real handler in case userland supplied needed padding */
2316         if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
2317                 return update_counters(net, user, len);
2318
2319         return do_update_counters(net, hlp.name, compat_ptr(hlp.counters),
2320                                         hlp.num_counters, user, len);
2321 }
2322
2323 static int compat_do_ebt_set_ctl(struct sock *sk,
2324                 int cmd, void __user *user, unsigned int len)
2325 {
2326         int ret;
2327         struct net *net = sock_net(sk);
2328
2329         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2330                 return -EPERM;
2331
2332         switch (cmd) {
2333         case EBT_SO_SET_ENTRIES:
2334                 ret = compat_do_replace(net, user, len);
2335                 break;
2336         case EBT_SO_SET_COUNTERS:
2337                 ret = compat_update_counters(net, user, len);
2338                 break;
2339         default:
2340                 ret = -EINVAL;
2341         }
2342         return ret;
2343 }
2344
2345 static int compat_do_ebt_get_ctl(struct sock *sk, int cmd,
2346                 void __user *user, int *len)
2347 {
2348         int ret;
2349         struct compat_ebt_replace tmp;
2350         struct ebt_table *t;
2351         struct net *net = sock_net(sk);
2352
2353         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2354                 return -EPERM;
2355
2356         /* try real handler in case userland supplied needed padding */
2357         if ((cmd == EBT_SO_GET_INFO ||
2358              cmd == EBT_SO_GET_INIT_INFO) && *len != sizeof(tmp))
2359                         return do_ebt_get_ctl(sk, cmd, user, len);
2360
2361         if (copy_from_user(&tmp, user, sizeof(tmp)))
2362                 return -EFAULT;
2363
2364         tmp.name[sizeof(tmp.name) - 1] = '\0';
2365
2366         t = find_table_lock(net, tmp.name, &ret, &ebt_mutex);
2367         if (!t)
2368                 return ret;
2369
2370         xt_compat_lock(NFPROTO_BRIDGE);
2371         switch (cmd) {
2372         case EBT_SO_GET_INFO:
2373                 tmp.nentries = t->private->nentries;
2374                 ret = compat_table_info(t->private, &tmp);
2375                 if (ret)
2376                         goto out;
2377                 tmp.valid_hooks = t->valid_hooks;
2378
2379                 if (copy_to_user(user, &tmp, *len) != 0) {
2380                         ret = -EFAULT;
2381                         break;
2382                 }
2383                 ret = 0;
2384                 break;
2385         case EBT_SO_GET_INIT_INFO:
2386                 tmp.nentries = t->table->nentries;
2387                 tmp.entries_size = t->table->entries_size;
2388                 tmp.valid_hooks = t->table->valid_hooks;
2389
2390                 if (copy_to_user(user, &tmp, *len) != 0) {
2391                         ret = -EFAULT;
2392                         break;
2393                 }
2394                 ret = 0;
2395                 break;
2396         case EBT_SO_GET_ENTRIES:
2397         case EBT_SO_GET_INIT_ENTRIES:
2398                 /* try real handler first in case of userland-side padding.
2399                  * in case we are dealing with an 'ordinary' 32 bit binary
2400                  * without 64bit compatibility padding, this will fail right
2401                  * after copy_from_user when the *len argument is validated.
2402                  *
2403                  * the compat_ variant needs to do one pass over the kernel
2404                  * data set to adjust for size differences before it the check.
2405                  */
2406                 if (copy_everything_to_user(t, user, len, cmd) == 0)
2407                         ret = 0;
2408                 else
2409                         ret = compat_copy_everything_to_user(t, user, len, cmd);
2410                 break;
2411         default:
2412                 ret = -EINVAL;
2413         }
2414  out:
2415         xt_compat_flush_offsets(NFPROTO_BRIDGE);
2416         xt_compat_unlock(NFPROTO_BRIDGE);
2417         mutex_unlock(&ebt_mutex);
2418         return ret;
2419 }
2420 #endif
2421
2422 static struct nf_sockopt_ops ebt_sockopts = {
2423         .pf             = PF_INET,
2424         .set_optmin     = EBT_BASE_CTL,
2425         .set_optmax     = EBT_SO_SET_MAX + 1,
2426         .set            = do_ebt_set_ctl,
2427 #ifdef CONFIG_COMPAT
2428         .compat_set     = compat_do_ebt_set_ctl,
2429 #endif
2430         .get_optmin     = EBT_BASE_CTL,
2431         .get_optmax     = EBT_SO_GET_MAX + 1,
2432         .get            = do_ebt_get_ctl,
2433 #ifdef CONFIG_COMPAT
2434         .compat_get     = compat_do_ebt_get_ctl,
2435 #endif
2436         .owner          = THIS_MODULE,
2437 };
2438
2439 static int __init ebtables_init(void)
2440 {
2441         int ret;
2442
2443         ret = xt_register_target(&ebt_standard_target);
2444         if (ret < 0)
2445                 return ret;
2446         ret = nf_register_sockopt(&ebt_sockopts);
2447         if (ret < 0) {
2448                 xt_unregister_target(&ebt_standard_target);
2449                 return ret;
2450         }
2451
2452         printk(KERN_INFO "Ebtables v2.0 registered\n");
2453         return 0;
2454 }
2455
2456 static void __exit ebtables_fini(void)
2457 {
2458         nf_unregister_sockopt(&ebt_sockopts);
2459         xt_unregister_target(&ebt_standard_target);
2460         printk(KERN_INFO "Ebtables v2.0 unregistered\n");
2461 }
2462
2463 EXPORT_SYMBOL(ebt_register_table);
2464 EXPORT_SYMBOL(ebt_unregister_table);
2465 EXPORT_SYMBOL(ebt_do_table);
2466 module_init(ebtables_init);
2467 module_exit(ebtables_fini);
2468 MODULE_LICENSE("GPL");