GNU Linux-libre 4.14.259-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         /* the table doesn't like it */
995         if (t->check && (ret = t->check(newinfo, repl->valid_hooks)))
996                 goto free_unlock;
997
998         if (repl->num_counters && repl->num_counters != t->private->nentries) {
999                 ret = -EINVAL;
1000                 goto free_unlock;
1001         }
1002
1003         /* we have the mutex lock, so no danger in reading this pointer */
1004         table = t->private;
1005         /* make sure the table can only be rmmod'ed if it contains no rules */
1006         if (!table->nentries && newinfo->nentries && !try_module_get(t->me)) {
1007                 ret = -ENOENT;
1008                 goto free_unlock;
1009         } else if (table->nentries && !newinfo->nentries)
1010                 module_put(t->me);
1011         /* we need an atomic snapshot of the counters */
1012         write_lock_bh(&t->lock);
1013         if (repl->num_counters)
1014                 get_counters(t->private->counters, counterstmp,
1015                    t->private->nentries);
1016
1017         t->private = newinfo;
1018         write_unlock_bh(&t->lock);
1019         mutex_unlock(&ebt_mutex);
1020         /* so, a user can change the chains while having messed up her counter
1021          * allocation. Only reason why this is done is because this way the lock
1022          * is held only once, while this doesn't bring the kernel into a
1023          * dangerous state.
1024          */
1025         if (repl->num_counters &&
1026            copy_to_user(repl->counters, counterstmp,
1027            repl->num_counters * sizeof(struct ebt_counter))) {
1028                 /* Silent error, can't fail, new table is already in place */
1029                 net_warn_ratelimited("ebtables: counters copy to user failed while replacing table\n");
1030         }
1031
1032         /* decrease module count and free resources */
1033         EBT_ENTRY_ITERATE(table->entries, table->entries_size,
1034                           ebt_cleanup_entry, net, NULL);
1035
1036         vfree(table->entries);
1037         if (table->chainstack) {
1038                 for_each_possible_cpu(i)
1039                         vfree(table->chainstack[i]);
1040                 vfree(table->chainstack);
1041         }
1042         vfree(table);
1043
1044         vfree(counterstmp);
1045
1046 #ifdef CONFIG_AUDIT
1047         if (audit_enabled) {
1048                 audit_log(current->audit_context, GFP_KERNEL,
1049                           AUDIT_NETFILTER_CFG,
1050                           "table=%s family=%u entries=%u",
1051                           repl->name, AF_BRIDGE, repl->nentries);
1052         }
1053 #endif
1054         return ret;
1055
1056 free_unlock:
1057         mutex_unlock(&ebt_mutex);
1058 free_iterate:
1059         EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
1060                           ebt_cleanup_entry, net, NULL);
1061 free_counterstmp:
1062         vfree(counterstmp);
1063         /* can be initialized in translate_table() */
1064         if (newinfo->chainstack) {
1065                 for_each_possible_cpu(i)
1066                         vfree(newinfo->chainstack[i]);
1067                 vfree(newinfo->chainstack);
1068         }
1069         return ret;
1070 }
1071
1072 /* replace the table */
1073 static int do_replace(struct net *net, const void __user *user,
1074                       unsigned int len)
1075 {
1076         int ret, countersize;
1077         struct ebt_table_info *newinfo;
1078         struct ebt_replace tmp;
1079
1080         if (copy_from_user(&tmp, user, sizeof(tmp)) != 0)
1081                 return -EFAULT;
1082
1083         if (len != sizeof(tmp) + tmp.entries_size)
1084                 return -EINVAL;
1085
1086         if (tmp.entries_size == 0)
1087                 return -EINVAL;
1088
1089         /* overflow check */
1090         if (tmp.nentries >= ((INT_MAX - sizeof(struct ebt_table_info)) /
1091                         NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter))
1092                 return -ENOMEM;
1093         if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter))
1094                 return -ENOMEM;
1095
1096         tmp.name[sizeof(tmp.name) - 1] = 0;
1097
1098         countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids;
1099         newinfo = __vmalloc(sizeof(*newinfo) + countersize, GFP_KERNEL_ACCOUNT,
1100                             PAGE_KERNEL);
1101         if (!newinfo)
1102                 return -ENOMEM;
1103
1104         if (countersize)
1105                 memset(newinfo->counters, 0, countersize);
1106
1107         newinfo->entries = __vmalloc(tmp.entries_size, GFP_KERNEL_ACCOUNT,
1108                                      PAGE_KERNEL);
1109         if (!newinfo->entries) {
1110                 ret = -ENOMEM;
1111                 goto free_newinfo;
1112         }
1113         if (copy_from_user(
1114            newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
1115                 ret = -EFAULT;
1116                 goto free_entries;
1117         }
1118
1119         ret = do_replace_finish(net, &tmp, newinfo);
1120         if (ret == 0)
1121                 return ret;
1122 free_entries:
1123         vfree(newinfo->entries);
1124 free_newinfo:
1125         vfree(newinfo);
1126         return ret;
1127 }
1128
1129 static void __ebt_unregister_table(struct net *net, struct ebt_table *table)
1130 {
1131         int i;
1132
1133         mutex_lock(&ebt_mutex);
1134         list_del(&table->list);
1135         mutex_unlock(&ebt_mutex);
1136         EBT_ENTRY_ITERATE(table->private->entries, table->private->entries_size,
1137                           ebt_cleanup_entry, net, NULL);
1138         if (table->private->nentries)
1139                 module_put(table->me);
1140         vfree(table->private->entries);
1141         if (table->private->chainstack) {
1142                 for_each_possible_cpu(i)
1143                         vfree(table->private->chainstack[i]);
1144                 vfree(table->private->chainstack);
1145         }
1146         vfree(table->private);
1147         kfree(table);
1148 }
1149
1150 int ebt_register_table(struct net *net, const struct ebt_table *input_table,
1151                        const struct nf_hook_ops *ops, struct ebt_table **res)
1152 {
1153         struct ebt_table_info *newinfo;
1154         struct ebt_table *t, *table;
1155         struct ebt_replace_kernel *repl;
1156         int ret, i, countersize;
1157         void *p;
1158
1159         if (input_table == NULL || (repl = input_table->table) == NULL ||
1160             repl->entries == NULL || repl->entries_size == 0 ||
1161             repl->counters != NULL || input_table->private != NULL)
1162                 return -EINVAL;
1163
1164         /* Don't add one table to multiple lists. */
1165         table = kmemdup(input_table, sizeof(struct ebt_table), GFP_KERNEL);
1166         if (!table) {
1167                 ret = -ENOMEM;
1168                 goto out;
1169         }
1170
1171         countersize = COUNTER_OFFSET(repl->nentries) * nr_cpu_ids;
1172         newinfo = vmalloc(sizeof(*newinfo) + countersize);
1173         ret = -ENOMEM;
1174         if (!newinfo)
1175                 goto free_table;
1176
1177         p = vmalloc(repl->entries_size);
1178         if (!p)
1179                 goto free_newinfo;
1180
1181         memcpy(p, repl->entries, repl->entries_size);
1182         newinfo->entries = p;
1183
1184         newinfo->entries_size = repl->entries_size;
1185         newinfo->nentries = repl->nentries;
1186
1187         if (countersize)
1188                 memset(newinfo->counters, 0, countersize);
1189
1190         /* fill in newinfo and parse the entries */
1191         newinfo->chainstack = NULL;
1192         for (i = 0; i < NF_BR_NUMHOOKS; i++) {
1193                 if ((repl->valid_hooks & (1 << i)) == 0)
1194                         newinfo->hook_entry[i] = NULL;
1195                 else
1196                         newinfo->hook_entry[i] = p +
1197                                 ((char *)repl->hook_entry[i] - repl->entries);
1198         }
1199         ret = translate_table(net, repl->name, newinfo);
1200         if (ret != 0)
1201                 goto free_chainstack;
1202
1203         if (table->check && table->check(newinfo, table->valid_hooks)) {
1204                 ret = -EINVAL;
1205                 goto free_chainstack;
1206         }
1207
1208         table->private = newinfo;
1209         rwlock_init(&table->lock);
1210         mutex_lock(&ebt_mutex);
1211         list_for_each_entry(t, &net->xt.tables[NFPROTO_BRIDGE], list) {
1212                 if (strcmp(t->name, table->name) == 0) {
1213                         ret = -EEXIST;
1214                         goto free_unlock;
1215                 }
1216         }
1217
1218         /* Hold a reference count if the chains aren't empty */
1219         if (newinfo->nentries && !try_module_get(table->me)) {
1220                 ret = -ENOENT;
1221                 goto free_unlock;
1222         }
1223         list_add(&table->list, &net->xt.tables[NFPROTO_BRIDGE]);
1224         mutex_unlock(&ebt_mutex);
1225
1226         WRITE_ONCE(*res, table);
1227
1228         if (!ops)
1229                 return 0;
1230
1231         ret = nf_register_net_hooks(net, ops, hweight32(table->valid_hooks));
1232         if (ret) {
1233                 __ebt_unregister_table(net, table);
1234                 *res = NULL;
1235         }
1236
1237         return ret;
1238 free_unlock:
1239         mutex_unlock(&ebt_mutex);
1240 free_chainstack:
1241         if (newinfo->chainstack) {
1242                 for_each_possible_cpu(i)
1243                         vfree(newinfo->chainstack[i]);
1244                 vfree(newinfo->chainstack);
1245         }
1246         vfree(newinfo->entries);
1247 free_newinfo:
1248         vfree(newinfo);
1249 free_table:
1250         kfree(table);
1251 out:
1252         return ret;
1253 }
1254
1255 void ebt_unregister_table(struct net *net, struct ebt_table *table,
1256                           const struct nf_hook_ops *ops)
1257 {
1258         if (ops)
1259                 nf_unregister_net_hooks(net, ops, hweight32(table->valid_hooks));
1260         __ebt_unregister_table(net, table);
1261 }
1262
1263 /* userspace just supplied us with counters */
1264 static int do_update_counters(struct net *net, const char *name,
1265                                 struct ebt_counter __user *counters,
1266                                 unsigned int num_counters,
1267                                 const void __user *user, unsigned int len)
1268 {
1269         int i, ret;
1270         struct ebt_counter *tmp;
1271         struct ebt_table *t;
1272
1273         if (num_counters == 0)
1274                 return -EINVAL;
1275
1276         tmp = vmalloc(num_counters * sizeof(*tmp));
1277         if (!tmp)
1278                 return -ENOMEM;
1279
1280         t = find_table_lock(net, name, &ret, &ebt_mutex);
1281         if (!t)
1282                 goto free_tmp;
1283
1284         if (num_counters != t->private->nentries) {
1285                 ret = -EINVAL;
1286                 goto unlock_mutex;
1287         }
1288
1289         if (copy_from_user(tmp, counters, num_counters * sizeof(*counters))) {
1290                 ret = -EFAULT;
1291                 goto unlock_mutex;
1292         }
1293
1294         /* we want an atomic add of the counters */
1295         write_lock_bh(&t->lock);
1296
1297         /* we add to the counters of the first cpu */
1298         for (i = 0; i < num_counters; i++) {
1299                 t->private->counters[i].pcnt += tmp[i].pcnt;
1300                 t->private->counters[i].bcnt += tmp[i].bcnt;
1301         }
1302
1303         write_unlock_bh(&t->lock);
1304         ret = 0;
1305 unlock_mutex:
1306         mutex_unlock(&ebt_mutex);
1307 free_tmp:
1308         vfree(tmp);
1309         return ret;
1310 }
1311
1312 static int update_counters(struct net *net, const void __user *user,
1313                             unsigned int len)
1314 {
1315         struct ebt_replace hlp;
1316
1317         if (copy_from_user(&hlp, user, sizeof(hlp)))
1318                 return -EFAULT;
1319
1320         if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
1321                 return -EINVAL;
1322
1323         return do_update_counters(net, hlp.name, hlp.counters,
1324                                 hlp.num_counters, user, len);
1325 }
1326
1327 static inline int ebt_obj_to_user(char __user *um, const char *_name,
1328                                   const char *data, int entrysize,
1329                                   int usersize, int datasize)
1330 {
1331         char name[EBT_FUNCTION_MAXNAMELEN] = {0};
1332
1333         /* ebtables expects 32 bytes long names but xt_match names are 29 bytes
1334          * long. Copy 29 bytes and fill remaining bytes with zeroes.
1335          */
1336         strlcpy(name, _name, sizeof(name));
1337         if (copy_to_user(um, name, EBT_FUNCTION_MAXNAMELEN) ||
1338             put_user(datasize, (int __user *)(um + EBT_FUNCTION_MAXNAMELEN)) ||
1339             xt_data_to_user(um + entrysize, data, usersize, datasize,
1340                             XT_ALIGN(datasize)))
1341                 return -EFAULT;
1342
1343         return 0;
1344 }
1345
1346 static inline int ebt_match_to_user(const struct ebt_entry_match *m,
1347                                     const char *base, char __user *ubase)
1348 {
1349         return ebt_obj_to_user(ubase + ((char *)m - base),
1350                                m->u.match->name, m->data, sizeof(*m),
1351                                m->u.match->usersize, m->match_size);
1352 }
1353
1354 static inline int ebt_watcher_to_user(const struct ebt_entry_watcher *w,
1355                                       const char *base, char __user *ubase)
1356 {
1357         return ebt_obj_to_user(ubase + ((char *)w - base),
1358                                w->u.watcher->name, w->data, sizeof(*w),
1359                                w->u.watcher->usersize, w->watcher_size);
1360 }
1361
1362 static inline int ebt_entry_to_user(struct ebt_entry *e, const char *base,
1363                                     char __user *ubase)
1364 {
1365         int ret;
1366         char __user *hlp;
1367         const struct ebt_entry_target *t;
1368
1369         if (e->bitmask == 0) {
1370                 /* special case !EBT_ENTRY_OR_ENTRIES */
1371                 if (copy_to_user(ubase + ((char *)e - base), e,
1372                                  sizeof(struct ebt_entries)))
1373                         return -EFAULT;
1374                 return 0;
1375         }
1376
1377         if (copy_to_user(ubase + ((char *)e - base), e, sizeof(*e)))
1378                 return -EFAULT;
1379
1380         hlp = ubase + (((char *)e + e->target_offset) - base);
1381         t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
1382
1383         ret = EBT_MATCH_ITERATE(e, ebt_match_to_user, base, ubase);
1384         if (ret != 0)
1385                 return ret;
1386         ret = EBT_WATCHER_ITERATE(e, ebt_watcher_to_user, base, ubase);
1387         if (ret != 0)
1388                 return ret;
1389         ret = ebt_obj_to_user(hlp, t->u.target->name, t->data, sizeof(*t),
1390                               t->u.target->usersize, t->target_size);
1391         if (ret != 0)
1392                 return ret;
1393
1394         return 0;
1395 }
1396
1397 static int copy_counters_to_user(struct ebt_table *t,
1398                                  const struct ebt_counter *oldcounters,
1399                                  void __user *user, unsigned int num_counters,
1400                                  unsigned int nentries)
1401 {
1402         struct ebt_counter *counterstmp;
1403         int ret = 0;
1404
1405         /* userspace might not need the counters */
1406         if (num_counters == 0)
1407                 return 0;
1408
1409         if (num_counters != nentries)
1410                 return -EINVAL;
1411
1412         counterstmp = vmalloc(nentries * sizeof(*counterstmp));
1413         if (!counterstmp)
1414                 return -ENOMEM;
1415
1416         write_lock_bh(&t->lock);
1417         get_counters(oldcounters, counterstmp, nentries);
1418         write_unlock_bh(&t->lock);
1419
1420         if (copy_to_user(user, counterstmp,
1421            nentries * sizeof(struct ebt_counter)))
1422                 ret = -EFAULT;
1423         vfree(counterstmp);
1424         return ret;
1425 }
1426
1427 /* called with ebt_mutex locked */
1428 static int copy_everything_to_user(struct ebt_table *t, void __user *user,
1429                                    const int *len, int cmd)
1430 {
1431         struct ebt_replace tmp;
1432         const struct ebt_counter *oldcounters;
1433         unsigned int entries_size, nentries;
1434         int ret;
1435         char *entries;
1436
1437         if (cmd == EBT_SO_GET_ENTRIES) {
1438                 entries_size = t->private->entries_size;
1439                 nentries = t->private->nentries;
1440                 entries = t->private->entries;
1441                 oldcounters = t->private->counters;
1442         } else {
1443                 entries_size = t->table->entries_size;
1444                 nentries = t->table->nentries;
1445                 entries = t->table->entries;
1446                 oldcounters = t->table->counters;
1447         }
1448
1449         if (copy_from_user(&tmp, user, sizeof(tmp)))
1450                 return -EFAULT;
1451
1452         if (*len != sizeof(struct ebt_replace) + entries_size +
1453            (tmp.num_counters ? nentries * sizeof(struct ebt_counter) : 0))
1454                 return -EINVAL;
1455
1456         if (tmp.nentries != nentries)
1457                 return -EINVAL;
1458
1459         if (tmp.entries_size != entries_size)
1460                 return -EINVAL;
1461
1462         ret = copy_counters_to_user(t, oldcounters, tmp.counters,
1463                                         tmp.num_counters, nentries);
1464         if (ret)
1465                 return ret;
1466
1467         /* set the match/watcher/target names right */
1468         return EBT_ENTRY_ITERATE(entries, entries_size,
1469            ebt_entry_to_user, entries, tmp.entries);
1470 }
1471
1472 static int do_ebt_set_ctl(struct sock *sk,
1473         int cmd, void __user *user, unsigned int len)
1474 {
1475         int ret;
1476         struct net *net = sock_net(sk);
1477
1478         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1479                 return -EPERM;
1480
1481         switch (cmd) {
1482         case EBT_SO_SET_ENTRIES:
1483                 ret = do_replace(net, user, len);
1484                 break;
1485         case EBT_SO_SET_COUNTERS:
1486                 ret = update_counters(net, user, len);
1487                 break;
1488         default:
1489                 ret = -EINVAL;
1490         }
1491         return ret;
1492 }
1493
1494 static int do_ebt_get_ctl(struct sock *sk, int cmd, void __user *user, int *len)
1495 {
1496         int ret;
1497         struct ebt_replace tmp;
1498         struct ebt_table *t;
1499         struct net *net = sock_net(sk);
1500
1501         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1502                 return -EPERM;
1503
1504         if (copy_from_user(&tmp, user, sizeof(tmp)))
1505                 return -EFAULT;
1506
1507         tmp.name[sizeof(tmp.name) - 1] = '\0';
1508
1509         t = find_table_lock(net, tmp.name, &ret, &ebt_mutex);
1510         if (!t)
1511                 return ret;
1512
1513         switch (cmd) {
1514         case EBT_SO_GET_INFO:
1515         case EBT_SO_GET_INIT_INFO:
1516                 if (*len != sizeof(struct ebt_replace)) {
1517                         ret = -EINVAL;
1518                         mutex_unlock(&ebt_mutex);
1519                         break;
1520                 }
1521                 if (cmd == EBT_SO_GET_INFO) {
1522                         tmp.nentries = t->private->nentries;
1523                         tmp.entries_size = t->private->entries_size;
1524                         tmp.valid_hooks = t->valid_hooks;
1525                 } else {
1526                         tmp.nentries = t->table->nentries;
1527                         tmp.entries_size = t->table->entries_size;
1528                         tmp.valid_hooks = t->table->valid_hooks;
1529                 }
1530                 mutex_unlock(&ebt_mutex);
1531                 if (copy_to_user(user, &tmp, *len) != 0) {
1532                         ret = -EFAULT;
1533                         break;
1534                 }
1535                 ret = 0;
1536                 break;
1537
1538         case EBT_SO_GET_ENTRIES:
1539         case EBT_SO_GET_INIT_ENTRIES:
1540                 ret = copy_everything_to_user(t, user, len, cmd);
1541                 mutex_unlock(&ebt_mutex);
1542                 break;
1543
1544         default:
1545                 mutex_unlock(&ebt_mutex);
1546                 ret = -EINVAL;
1547         }
1548
1549         return ret;
1550 }
1551
1552 #ifdef CONFIG_COMPAT
1553 /* 32 bit-userspace compatibility definitions. */
1554 struct compat_ebt_replace {
1555         char name[EBT_TABLE_MAXNAMELEN];
1556         compat_uint_t valid_hooks;
1557         compat_uint_t nentries;
1558         compat_uint_t entries_size;
1559         /* start of the chains */
1560         compat_uptr_t hook_entry[NF_BR_NUMHOOKS];
1561         /* nr of counters userspace expects back */
1562         compat_uint_t num_counters;
1563         /* where the kernel will put the old counters. */
1564         compat_uptr_t counters;
1565         compat_uptr_t entries;
1566 };
1567
1568 /* struct ebt_entry_match, _target and _watcher have same layout */
1569 struct compat_ebt_entry_mwt {
1570         union {
1571                 char name[EBT_FUNCTION_MAXNAMELEN];
1572                 compat_uptr_t ptr;
1573         } u;
1574         compat_uint_t match_size;
1575         compat_uint_t data[0];
1576 };
1577
1578 /* account for possible padding between match_size and ->data */
1579 static int ebt_compat_entry_padsize(void)
1580 {
1581         BUILD_BUG_ON(XT_ALIGN(sizeof(struct ebt_entry_match)) <
1582                         COMPAT_XT_ALIGN(sizeof(struct compat_ebt_entry_mwt)));
1583         return (int) XT_ALIGN(sizeof(struct ebt_entry_match)) -
1584                         COMPAT_XT_ALIGN(sizeof(struct compat_ebt_entry_mwt));
1585 }
1586
1587 static int ebt_compat_match_offset(const struct xt_match *match,
1588                                    unsigned int userlen)
1589 {
1590         /* ebt_among needs special handling. The kernel .matchsize is
1591          * set to -1 at registration time; at runtime an EBT_ALIGN()ed
1592          * value is expected.
1593          * Example: userspace sends 4500, ebt_among.c wants 4504.
1594          */
1595         if (unlikely(match->matchsize == -1))
1596                 return XT_ALIGN(userlen) - COMPAT_XT_ALIGN(userlen);
1597         return xt_compat_match_offset(match);
1598 }
1599
1600 static int compat_match_to_user(struct ebt_entry_match *m, void __user **dstptr,
1601                                 unsigned int *size)
1602 {
1603         const struct xt_match *match = m->u.match;
1604         struct compat_ebt_entry_mwt __user *cm = *dstptr;
1605         int off = ebt_compat_match_offset(match, m->match_size);
1606         compat_uint_t msize = m->match_size - off;
1607
1608         if (WARN_ON(off >= m->match_size))
1609                 return -EINVAL;
1610
1611         if (copy_to_user(cm->u.name, match->name,
1612             strlen(match->name) + 1) || put_user(msize, &cm->match_size))
1613                 return -EFAULT;
1614
1615         if (match->compat_to_user) {
1616                 if (match->compat_to_user(cm->data, m->data))
1617                         return -EFAULT;
1618         } else {
1619                 if (xt_data_to_user(cm->data, m->data, match->usersize, msize,
1620                                     COMPAT_XT_ALIGN(msize)))
1621                         return -EFAULT;
1622         }
1623
1624         *size -= ebt_compat_entry_padsize() + off;
1625         *dstptr = cm->data;
1626         *dstptr += msize;
1627         return 0;
1628 }
1629
1630 static int compat_target_to_user(struct ebt_entry_target *t,
1631                                  void __user **dstptr,
1632                                  unsigned int *size)
1633 {
1634         const struct xt_target *target = t->u.target;
1635         struct compat_ebt_entry_mwt __user *cm = *dstptr;
1636         int off = xt_compat_target_offset(target);
1637         compat_uint_t tsize = t->target_size - off;
1638
1639         if (WARN_ON(off >= t->target_size))
1640                 return -EINVAL;
1641
1642         if (copy_to_user(cm->u.name, target->name,
1643             strlen(target->name) + 1) || put_user(tsize, &cm->match_size))
1644                 return -EFAULT;
1645
1646         if (target->compat_to_user) {
1647                 if (target->compat_to_user(cm->data, t->data))
1648                         return -EFAULT;
1649         } else {
1650                 if (xt_data_to_user(cm->data, t->data, target->usersize, tsize,
1651                                     COMPAT_XT_ALIGN(tsize)))
1652                         return -EFAULT;
1653         }
1654
1655         *size -= ebt_compat_entry_padsize() + off;
1656         *dstptr = cm->data;
1657         *dstptr += tsize;
1658         return 0;
1659 }
1660
1661 static int compat_watcher_to_user(struct ebt_entry_watcher *w,
1662                                   void __user **dstptr,
1663                                   unsigned int *size)
1664 {
1665         return compat_target_to_user((struct ebt_entry_target *)w,
1666                                                         dstptr, size);
1667 }
1668
1669 static int compat_copy_entry_to_user(struct ebt_entry *e, void __user **dstptr,
1670                                 unsigned int *size)
1671 {
1672         struct ebt_entry_target *t;
1673         struct ebt_entry __user *ce;
1674         u32 watchers_offset, target_offset, next_offset;
1675         compat_uint_t origsize;
1676         int ret;
1677
1678         if (e->bitmask == 0) {
1679                 if (*size < sizeof(struct ebt_entries))
1680                         return -EINVAL;
1681                 if (copy_to_user(*dstptr, e, sizeof(struct ebt_entries)))
1682                         return -EFAULT;
1683
1684                 *dstptr += sizeof(struct ebt_entries);
1685                 *size -= sizeof(struct ebt_entries);
1686                 return 0;
1687         }
1688
1689         if (*size < sizeof(*ce))
1690                 return -EINVAL;
1691
1692         ce = *dstptr;
1693         if (copy_to_user(ce, e, sizeof(*ce)))
1694                 return -EFAULT;
1695
1696         origsize = *size;
1697         *dstptr += sizeof(*ce);
1698
1699         ret = EBT_MATCH_ITERATE(e, compat_match_to_user, dstptr, size);
1700         if (ret)
1701                 return ret;
1702         watchers_offset = e->watchers_offset - (origsize - *size);
1703
1704         ret = EBT_WATCHER_ITERATE(e, compat_watcher_to_user, dstptr, size);
1705         if (ret)
1706                 return ret;
1707         target_offset = e->target_offset - (origsize - *size);
1708
1709         t = (struct ebt_entry_target *) ((char *) e + e->target_offset);
1710
1711         ret = compat_target_to_user(t, dstptr, size);
1712         if (ret)
1713                 return ret;
1714         next_offset = e->next_offset - (origsize - *size);
1715
1716         if (put_user(watchers_offset, &ce->watchers_offset) ||
1717             put_user(target_offset, &ce->target_offset) ||
1718             put_user(next_offset, &ce->next_offset))
1719                 return -EFAULT;
1720
1721         *size -= sizeof(*ce);
1722         return 0;
1723 }
1724
1725 static int compat_calc_match(struct ebt_entry_match *m, int *off)
1726 {
1727         *off += ebt_compat_match_offset(m->u.match, m->match_size);
1728         *off += ebt_compat_entry_padsize();
1729         return 0;
1730 }
1731
1732 static int compat_calc_watcher(struct ebt_entry_watcher *w, int *off)
1733 {
1734         *off += xt_compat_target_offset(w->u.watcher);
1735         *off += ebt_compat_entry_padsize();
1736         return 0;
1737 }
1738
1739 static int compat_calc_entry(const struct ebt_entry *e,
1740                              const struct ebt_table_info *info,
1741                              const void *base,
1742                              struct compat_ebt_replace *newinfo)
1743 {
1744         const struct ebt_entry_target *t;
1745         unsigned int entry_offset;
1746         int off, ret, i;
1747
1748         if (e->bitmask == 0)
1749                 return 0;
1750
1751         off = 0;
1752         entry_offset = (void *)e - base;
1753
1754         EBT_MATCH_ITERATE(e, compat_calc_match, &off);
1755         EBT_WATCHER_ITERATE(e, compat_calc_watcher, &off);
1756
1757         t = (const struct ebt_entry_target *) ((char *) e + e->target_offset);
1758
1759         off += xt_compat_target_offset(t->u.target);
1760         off += ebt_compat_entry_padsize();
1761
1762         newinfo->entries_size -= off;
1763
1764         ret = xt_compat_add_offset(NFPROTO_BRIDGE, entry_offset, off);
1765         if (ret)
1766                 return ret;
1767
1768         for (i = 0; i < NF_BR_NUMHOOKS; i++) {
1769                 const void *hookptr = info->hook_entry[i];
1770                 if (info->hook_entry[i] &&
1771                     (e < (struct ebt_entry *)(base - hookptr))) {
1772                         newinfo->hook_entry[i] -= off;
1773                         pr_debug("0x%08X -> 0x%08X\n",
1774                                         newinfo->hook_entry[i] + off,
1775                                         newinfo->hook_entry[i]);
1776                 }
1777         }
1778
1779         return 0;
1780 }
1781
1782 static int ebt_compat_init_offsets(unsigned int number)
1783 {
1784         if (number > INT_MAX)
1785                 return -EINVAL;
1786
1787         /* also count the base chain policies */
1788         number += NF_BR_NUMHOOKS;
1789
1790         return xt_compat_init_offsets(NFPROTO_BRIDGE, number);
1791 }
1792
1793 static int compat_table_info(const struct ebt_table_info *info,
1794                              struct compat_ebt_replace *newinfo)
1795 {
1796         unsigned int size = info->entries_size;
1797         const void *entries = info->entries;
1798         int ret;
1799
1800         newinfo->entries_size = size;
1801         ret = ebt_compat_init_offsets(info->nentries);
1802         if (ret)
1803                 return ret;
1804
1805         return EBT_ENTRY_ITERATE(entries, size, compat_calc_entry, info,
1806                                                         entries, newinfo);
1807 }
1808
1809 static int compat_copy_everything_to_user(struct ebt_table *t,
1810                                           void __user *user, int *len, int cmd)
1811 {
1812         struct compat_ebt_replace repl, tmp;
1813         struct ebt_counter *oldcounters;
1814         struct ebt_table_info tinfo;
1815         int ret;
1816         void __user *pos;
1817
1818         memset(&tinfo, 0, sizeof(tinfo));
1819
1820         if (cmd == EBT_SO_GET_ENTRIES) {
1821                 tinfo.entries_size = t->private->entries_size;
1822                 tinfo.nentries = t->private->nentries;
1823                 tinfo.entries = t->private->entries;
1824                 oldcounters = t->private->counters;
1825         } else {
1826                 tinfo.entries_size = t->table->entries_size;
1827                 tinfo.nentries = t->table->nentries;
1828                 tinfo.entries = t->table->entries;
1829                 oldcounters = t->table->counters;
1830         }
1831
1832         if (copy_from_user(&tmp, user, sizeof(tmp)))
1833                 return -EFAULT;
1834
1835         if (tmp.nentries != tinfo.nentries ||
1836            (tmp.num_counters && tmp.num_counters != tinfo.nentries))
1837                 return -EINVAL;
1838
1839         memcpy(&repl, &tmp, sizeof(repl));
1840         if (cmd == EBT_SO_GET_ENTRIES)
1841                 ret = compat_table_info(t->private, &repl);
1842         else
1843                 ret = compat_table_info(&tinfo, &repl);
1844         if (ret)
1845                 return ret;
1846
1847         if (*len != sizeof(tmp) + repl.entries_size +
1848            (tmp.num_counters? tinfo.nentries * sizeof(struct ebt_counter): 0)) {
1849                 pr_err("wrong size: *len %d, entries_size %u, replsz %d\n",
1850                                 *len, tinfo.entries_size, repl.entries_size);
1851                 return -EINVAL;
1852         }
1853
1854         /* userspace might not need the counters */
1855         ret = copy_counters_to_user(t, oldcounters, compat_ptr(tmp.counters),
1856                                         tmp.num_counters, tinfo.nentries);
1857         if (ret)
1858                 return ret;
1859
1860         pos = compat_ptr(tmp.entries);
1861         return EBT_ENTRY_ITERATE(tinfo.entries, tinfo.entries_size,
1862                         compat_copy_entry_to_user, &pos, &tmp.entries_size);
1863 }
1864
1865 struct ebt_entries_buf_state {
1866         char *buf_kern_start;   /* kernel buffer to copy (translated) data to */
1867         u32 buf_kern_len;       /* total size of kernel buffer */
1868         u32 buf_kern_offset;    /* amount of data copied so far */
1869         u32 buf_user_offset;    /* read position in userspace buffer */
1870 };
1871
1872 static int ebt_buf_count(struct ebt_entries_buf_state *state, unsigned int sz)
1873 {
1874         state->buf_kern_offset += sz;
1875         return state->buf_kern_offset >= sz ? 0 : -EINVAL;
1876 }
1877
1878 static int ebt_buf_add(struct ebt_entries_buf_state *state,
1879                        const void *data, unsigned int sz)
1880 {
1881         if (state->buf_kern_start == NULL)
1882                 goto count_only;
1883
1884         if (WARN_ON(state->buf_kern_offset + sz > state->buf_kern_len))
1885                 return -EINVAL;
1886
1887         memcpy(state->buf_kern_start + state->buf_kern_offset, data, sz);
1888
1889  count_only:
1890         state->buf_user_offset += sz;
1891         return ebt_buf_count(state, sz);
1892 }
1893
1894 static int ebt_buf_add_pad(struct ebt_entries_buf_state *state, unsigned int sz)
1895 {
1896         char *b = state->buf_kern_start;
1897
1898         if (WARN_ON(b && state->buf_kern_offset > state->buf_kern_len))
1899                 return -EINVAL;
1900
1901         if (b != NULL && sz > 0)
1902                 memset(b + state->buf_kern_offset, 0, sz);
1903         /* do not adjust ->buf_user_offset here, we added kernel-side padding */
1904         return ebt_buf_count(state, sz);
1905 }
1906
1907 enum compat_mwt {
1908         EBT_COMPAT_MATCH,
1909         EBT_COMPAT_WATCHER,
1910         EBT_COMPAT_TARGET,
1911 };
1912
1913 static int compat_mtw_from_user(const struct compat_ebt_entry_mwt *mwt,
1914                                 enum compat_mwt compat_mwt,
1915                                 struct ebt_entries_buf_state *state,
1916                                 const unsigned char *base)
1917 {
1918         char name[EBT_FUNCTION_MAXNAMELEN];
1919         struct xt_match *match;
1920         struct xt_target *wt;
1921         void *dst = NULL;
1922         int off, pad = 0;
1923         unsigned int size_kern, match_size = mwt->match_size;
1924
1925         if (strscpy(name, mwt->u.name, sizeof(name)) < 0)
1926                 return -EINVAL;
1927
1928         if (state->buf_kern_start)
1929                 dst = state->buf_kern_start + state->buf_kern_offset;
1930
1931         switch (compat_mwt) {
1932         case EBT_COMPAT_MATCH:
1933                 match = xt_request_find_match(NFPROTO_BRIDGE, name, 0);
1934                 if (IS_ERR(match))
1935                         return PTR_ERR(match);
1936
1937                 off = ebt_compat_match_offset(match, match_size);
1938                 if (dst) {
1939                         if (match->compat_from_user)
1940                                 match->compat_from_user(dst, mwt->data);
1941                         else
1942                                 memcpy(dst, mwt->data, match_size);
1943                 }
1944
1945                 size_kern = match->matchsize;
1946                 if (unlikely(size_kern == -1))
1947                         size_kern = match_size;
1948                 module_put(match->me);
1949                 break;
1950         case EBT_COMPAT_WATCHER: /* fallthrough */
1951         case EBT_COMPAT_TARGET:
1952                 wt = xt_request_find_target(NFPROTO_BRIDGE, name, 0);
1953                 if (IS_ERR(wt))
1954                         return PTR_ERR(wt);
1955                 off = xt_compat_target_offset(wt);
1956
1957                 if (dst) {
1958                         if (wt->compat_from_user)
1959                                 wt->compat_from_user(dst, mwt->data);
1960                         else
1961                                 memcpy(dst, mwt->data, match_size);
1962                 }
1963
1964                 size_kern = wt->targetsize;
1965                 module_put(wt->me);
1966                 break;
1967
1968         default:
1969                 return -EINVAL;
1970         }
1971
1972         state->buf_kern_offset += match_size + off;
1973         state->buf_user_offset += match_size;
1974         pad = XT_ALIGN(size_kern) - size_kern;
1975
1976         if (pad > 0 && dst) {
1977                 if (WARN_ON(state->buf_kern_len <= pad))
1978                         return -EINVAL;
1979                 if (WARN_ON(state->buf_kern_offset - (match_size + off) + size_kern > state->buf_kern_len - pad))
1980                         return -EINVAL;
1981                 memset(dst + size_kern, 0, pad);
1982         }
1983         return off + match_size;
1984 }
1985
1986 /* return size of all matches, watchers or target, including necessary
1987  * alignment and padding.
1988  */
1989 static int ebt_size_mwt(const struct compat_ebt_entry_mwt *match32,
1990                         unsigned int size_left, enum compat_mwt type,
1991                         struct ebt_entries_buf_state *state, const void *base)
1992 {
1993         const char *buf = (const char *)match32;
1994         int growth = 0;
1995
1996         if (size_left == 0)
1997                 return 0;
1998
1999         do {
2000                 struct ebt_entry_match *match_kern;
2001                 int ret;
2002
2003                 if (size_left < sizeof(*match32))
2004                         return -EINVAL;
2005
2006                 match_kern = (struct ebt_entry_match *) state->buf_kern_start;
2007                 if (match_kern) {
2008                         char *tmp;
2009                         tmp = state->buf_kern_start + state->buf_kern_offset;
2010                         match_kern = (struct ebt_entry_match *) tmp;
2011                 }
2012                 ret = ebt_buf_add(state, buf, sizeof(*match32));
2013                 if (ret < 0)
2014                         return ret;
2015                 size_left -= sizeof(*match32);
2016
2017                 /* add padding before match->data (if any) */
2018                 ret = ebt_buf_add_pad(state, ebt_compat_entry_padsize());
2019                 if (ret < 0)
2020                         return ret;
2021
2022                 if (match32->match_size > size_left)
2023                         return -EINVAL;
2024
2025                 size_left -= match32->match_size;
2026
2027                 ret = compat_mtw_from_user(match32, type, state, base);
2028                 if (ret < 0)
2029                         return ret;
2030
2031                 if (WARN_ON(ret < match32->match_size))
2032                         return -EINVAL;
2033                 growth += ret - match32->match_size;
2034                 growth += ebt_compat_entry_padsize();
2035
2036                 buf += sizeof(*match32);
2037                 buf += match32->match_size;
2038
2039                 if (match_kern)
2040                         match_kern->match_size = ret;
2041
2042                 match32 = (struct compat_ebt_entry_mwt *) buf;
2043         } while (size_left);
2044
2045         return growth;
2046 }
2047
2048 /* called for all ebt_entry structures. */
2049 static int size_entry_mwt(const struct ebt_entry *entry, const unsigned char *base,
2050                           unsigned int *total,
2051                           struct ebt_entries_buf_state *state)
2052 {
2053         unsigned int i, j, startoff, next_expected_off, new_offset = 0;
2054         /* stores match/watchers/targets & offset of next struct ebt_entry: */
2055         unsigned int offsets[4];
2056         unsigned int *offsets_update = NULL;
2057         int ret;
2058         char *buf_start;
2059
2060         if (*total < sizeof(struct ebt_entries))
2061                 return -EINVAL;
2062
2063         if (!entry->bitmask) {
2064                 *total -= sizeof(struct ebt_entries);
2065                 return ebt_buf_add(state, entry, sizeof(struct ebt_entries));
2066         }
2067         if (*total < sizeof(*entry) || entry->next_offset < sizeof(*entry))
2068                 return -EINVAL;
2069
2070         startoff = state->buf_user_offset;
2071         /* pull in most part of ebt_entry, it does not need to be changed. */
2072         ret = ebt_buf_add(state, entry,
2073                         offsetof(struct ebt_entry, watchers_offset));
2074         if (ret < 0)
2075                 return ret;
2076
2077         offsets[0] = sizeof(struct ebt_entry); /* matches come first */
2078         memcpy(&offsets[1], &entry->watchers_offset,
2079                         sizeof(offsets) - sizeof(offsets[0]));
2080
2081         if (state->buf_kern_start) {
2082                 buf_start = state->buf_kern_start + state->buf_kern_offset;
2083                 offsets_update = (unsigned int *) buf_start;
2084         }
2085         ret = ebt_buf_add(state, &offsets[1],
2086                         sizeof(offsets) - sizeof(offsets[0]));
2087         if (ret < 0)
2088                 return ret;
2089         buf_start = (char *) entry;
2090         /* 0: matches offset, always follows ebt_entry.
2091          * 1: watchers offset, from ebt_entry structure
2092          * 2: target offset, from ebt_entry structure
2093          * 3: next ebt_entry offset, from ebt_entry structure
2094          *
2095          * offsets are relative to beginning of struct ebt_entry (i.e., 0).
2096          */
2097         for (i = 0; i < 4 ; ++i) {
2098                 if (offsets[i] > *total)
2099                         return -EINVAL;
2100
2101                 if (i < 3 && offsets[i] == *total)
2102                         return -EINVAL;
2103
2104                 if (i == 0)
2105                         continue;
2106                 if (offsets[i-1] > offsets[i])
2107                         return -EINVAL;
2108         }
2109
2110         for (i = 0, j = 1 ; j < 4 ; j++, i++) {
2111                 struct compat_ebt_entry_mwt *match32;
2112                 unsigned int size;
2113                 char *buf = buf_start;
2114
2115                 buf = buf_start + offsets[i];
2116                 if (offsets[i] > offsets[j])
2117                         return -EINVAL;
2118
2119                 match32 = (struct compat_ebt_entry_mwt *) buf;
2120                 size = offsets[j] - offsets[i];
2121                 ret = ebt_size_mwt(match32, size, i, state, base);
2122                 if (ret < 0)
2123                         return ret;
2124                 new_offset += ret;
2125                 if (offsets_update && new_offset) {
2126                         pr_debug("change offset %d to %d\n",
2127                                 offsets_update[i], offsets[j] + new_offset);
2128                         offsets_update[i] = offsets[j] + new_offset;
2129                 }
2130         }
2131
2132         if (state->buf_kern_start == NULL) {
2133                 unsigned int offset = buf_start - (char *) base;
2134
2135                 ret = xt_compat_add_offset(NFPROTO_BRIDGE, offset, new_offset);
2136                 if (ret < 0)
2137                         return ret;
2138         }
2139
2140         next_expected_off = state->buf_user_offset - startoff;
2141         if (next_expected_off != entry->next_offset)
2142                 return -EINVAL;
2143
2144         if (*total < entry->next_offset)
2145                 return -EINVAL;
2146         *total -= entry->next_offset;
2147         return 0;
2148 }
2149
2150 /* repl->entries_size is the size of the ebt_entry blob in userspace.
2151  * It might need more memory when copied to a 64 bit kernel in case
2152  * userspace is 32-bit. So, first task: find out how much memory is needed.
2153  *
2154  * Called before validation is performed.
2155  */
2156 static int compat_copy_entries(unsigned char *data, unsigned int size_user,
2157                                 struct ebt_entries_buf_state *state)
2158 {
2159         unsigned int size_remaining = size_user;
2160         int ret;
2161
2162         ret = EBT_ENTRY_ITERATE(data, size_user, size_entry_mwt, data,
2163                                         &size_remaining, state);
2164         if (ret < 0)
2165                 return ret;
2166
2167         if (size_remaining)
2168                 return -EINVAL;
2169
2170         return state->buf_kern_offset;
2171 }
2172
2173
2174 static int compat_copy_ebt_replace_from_user(struct ebt_replace *repl,
2175                                             void __user *user, unsigned int len)
2176 {
2177         struct compat_ebt_replace tmp;
2178         int i;
2179
2180         if (len < sizeof(tmp))
2181                 return -EINVAL;
2182
2183         if (copy_from_user(&tmp, user, sizeof(tmp)))
2184                 return -EFAULT;
2185
2186         if (len != sizeof(tmp) + tmp.entries_size)
2187                 return -EINVAL;
2188
2189         if (tmp.entries_size == 0)
2190                 return -EINVAL;
2191
2192         if (tmp.nentries >= ((INT_MAX - sizeof(struct ebt_table_info)) /
2193                         NR_CPUS - SMP_CACHE_BYTES) / sizeof(struct ebt_counter))
2194                 return -ENOMEM;
2195         if (tmp.num_counters >= INT_MAX / sizeof(struct ebt_counter))
2196                 return -ENOMEM;
2197
2198         memcpy(repl, &tmp, offsetof(struct ebt_replace, hook_entry));
2199
2200         /* starting with hook_entry, 32 vs. 64 bit structures are different */
2201         for (i = 0; i < NF_BR_NUMHOOKS; i++)
2202                 repl->hook_entry[i] = compat_ptr(tmp.hook_entry[i]);
2203
2204         repl->num_counters = tmp.num_counters;
2205         repl->counters = compat_ptr(tmp.counters);
2206         repl->entries = compat_ptr(tmp.entries);
2207         return 0;
2208 }
2209
2210 static int compat_do_replace(struct net *net, void __user *user,
2211                              unsigned int len)
2212 {
2213         int ret, i, countersize, size64;
2214         struct ebt_table_info *newinfo;
2215         struct ebt_replace tmp;
2216         struct ebt_entries_buf_state state;
2217         void *entries_tmp;
2218
2219         ret = compat_copy_ebt_replace_from_user(&tmp, user, len);
2220         if (ret) {
2221                 /* try real handler in case userland supplied needed padding */
2222                 if (ret == -EINVAL && do_replace(net, user, len) == 0)
2223                         ret = 0;
2224                 return ret;
2225         }
2226
2227         countersize = COUNTER_OFFSET(tmp.nentries) * nr_cpu_ids;
2228         newinfo = vmalloc(sizeof(*newinfo) + countersize);
2229         if (!newinfo)
2230                 return -ENOMEM;
2231
2232         if (countersize)
2233                 memset(newinfo->counters, 0, countersize);
2234
2235         memset(&state, 0, sizeof(state));
2236
2237         newinfo->entries = vmalloc(tmp.entries_size);
2238         if (!newinfo->entries) {
2239                 ret = -ENOMEM;
2240                 goto free_newinfo;
2241         }
2242         if (copy_from_user(
2243            newinfo->entries, tmp.entries, tmp.entries_size) != 0) {
2244                 ret = -EFAULT;
2245                 goto free_entries;
2246         }
2247
2248         entries_tmp = newinfo->entries;
2249
2250         xt_compat_lock(NFPROTO_BRIDGE);
2251
2252         ret = ebt_compat_init_offsets(tmp.nentries);
2253         if (ret < 0)
2254                 goto out_unlock;
2255
2256         ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
2257         if (ret < 0)
2258                 goto out_unlock;
2259
2260         pr_debug("tmp.entries_size %d, kern off %d, user off %d delta %d\n",
2261                 tmp.entries_size, state.buf_kern_offset, state.buf_user_offset,
2262                 xt_compat_calc_jump(NFPROTO_BRIDGE, tmp.entries_size));
2263
2264         size64 = ret;
2265         newinfo->entries = vmalloc(size64);
2266         if (!newinfo->entries) {
2267                 vfree(entries_tmp);
2268                 ret = -ENOMEM;
2269                 goto out_unlock;
2270         }
2271
2272         memset(&state, 0, sizeof(state));
2273         state.buf_kern_start = newinfo->entries;
2274         state.buf_kern_len = size64;
2275
2276         ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
2277         if (WARN_ON(ret < 0)) {
2278                 vfree(entries_tmp);
2279                 goto out_unlock;
2280         }
2281
2282         vfree(entries_tmp);
2283         tmp.entries_size = size64;
2284
2285         for (i = 0; i < NF_BR_NUMHOOKS; i++) {
2286                 char __user *usrptr;
2287                 if (tmp.hook_entry[i]) {
2288                         unsigned int delta;
2289                         usrptr = (char __user *) tmp.hook_entry[i];
2290                         delta = usrptr - tmp.entries;
2291                         usrptr += xt_compat_calc_jump(NFPROTO_BRIDGE, delta);
2292                         tmp.hook_entry[i] = (struct ebt_entries __user *)usrptr;
2293                 }
2294         }
2295
2296         xt_compat_flush_offsets(NFPROTO_BRIDGE);
2297         xt_compat_unlock(NFPROTO_BRIDGE);
2298
2299         ret = do_replace_finish(net, &tmp, newinfo);
2300         if (ret == 0)
2301                 return ret;
2302 free_entries:
2303         vfree(newinfo->entries);
2304 free_newinfo:
2305         vfree(newinfo);
2306         return ret;
2307 out_unlock:
2308         xt_compat_flush_offsets(NFPROTO_BRIDGE);
2309         xt_compat_unlock(NFPROTO_BRIDGE);
2310         goto free_entries;
2311 }
2312
2313 static int compat_update_counters(struct net *net, void __user *user,
2314                                   unsigned int len)
2315 {
2316         struct compat_ebt_replace hlp;
2317
2318         if (copy_from_user(&hlp, user, sizeof(hlp)))
2319                 return -EFAULT;
2320
2321         /* try real handler in case userland supplied needed padding */
2322         if (len != sizeof(hlp) + hlp.num_counters * sizeof(struct ebt_counter))
2323                 return update_counters(net, user, len);
2324
2325         return do_update_counters(net, hlp.name, compat_ptr(hlp.counters),
2326                                         hlp.num_counters, user, len);
2327 }
2328
2329 static int compat_do_ebt_set_ctl(struct sock *sk,
2330                 int cmd, void __user *user, unsigned int len)
2331 {
2332         int ret;
2333         struct net *net = sock_net(sk);
2334
2335         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2336                 return -EPERM;
2337
2338         switch (cmd) {
2339         case EBT_SO_SET_ENTRIES:
2340                 ret = compat_do_replace(net, user, len);
2341                 break;
2342         case EBT_SO_SET_COUNTERS:
2343                 ret = compat_update_counters(net, user, len);
2344                 break;
2345         default:
2346                 ret = -EINVAL;
2347         }
2348         return ret;
2349 }
2350
2351 static int compat_do_ebt_get_ctl(struct sock *sk, int cmd,
2352                 void __user *user, int *len)
2353 {
2354         int ret;
2355         struct compat_ebt_replace tmp;
2356         struct ebt_table *t;
2357         struct net *net = sock_net(sk);
2358
2359         if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2360                 return -EPERM;
2361
2362         /* try real handler in case userland supplied needed padding */
2363         if ((cmd == EBT_SO_GET_INFO ||
2364              cmd == EBT_SO_GET_INIT_INFO) && *len != sizeof(tmp))
2365                         return do_ebt_get_ctl(sk, cmd, user, len);
2366
2367         if (copy_from_user(&tmp, user, sizeof(tmp)))
2368                 return -EFAULT;
2369
2370         tmp.name[sizeof(tmp.name) - 1] = '\0';
2371
2372         t = find_table_lock(net, tmp.name, &ret, &ebt_mutex);
2373         if (!t)
2374                 return ret;
2375
2376         xt_compat_lock(NFPROTO_BRIDGE);
2377         switch (cmd) {
2378         case EBT_SO_GET_INFO:
2379                 tmp.nentries = t->private->nentries;
2380                 ret = compat_table_info(t->private, &tmp);
2381                 if (ret)
2382                         goto out;
2383                 tmp.valid_hooks = t->valid_hooks;
2384
2385                 if (copy_to_user(user, &tmp, *len) != 0) {
2386                         ret = -EFAULT;
2387                         break;
2388                 }
2389                 ret = 0;
2390                 break;
2391         case EBT_SO_GET_INIT_INFO:
2392                 tmp.nentries = t->table->nentries;
2393                 tmp.entries_size = t->table->entries_size;
2394                 tmp.valid_hooks = t->table->valid_hooks;
2395
2396                 if (copy_to_user(user, &tmp, *len) != 0) {
2397                         ret = -EFAULT;
2398                         break;
2399                 }
2400                 ret = 0;
2401                 break;
2402         case EBT_SO_GET_ENTRIES:
2403         case EBT_SO_GET_INIT_ENTRIES:
2404                 /* try real handler first in case of userland-side padding.
2405                  * in case we are dealing with an 'ordinary' 32 bit binary
2406                  * without 64bit compatibility padding, this will fail right
2407                  * after copy_from_user when the *len argument is validated.
2408                  *
2409                  * the compat_ variant needs to do one pass over the kernel
2410                  * data set to adjust for size differences before it the check.
2411                  */
2412                 if (copy_everything_to_user(t, user, len, cmd) == 0)
2413                         ret = 0;
2414                 else
2415                         ret = compat_copy_everything_to_user(t, user, len, cmd);
2416                 break;
2417         default:
2418                 ret = -EINVAL;
2419         }
2420  out:
2421         xt_compat_flush_offsets(NFPROTO_BRIDGE);
2422         xt_compat_unlock(NFPROTO_BRIDGE);
2423         mutex_unlock(&ebt_mutex);
2424         return ret;
2425 }
2426 #endif
2427
2428 static struct nf_sockopt_ops ebt_sockopts = {
2429         .pf             = PF_INET,
2430         .set_optmin     = EBT_BASE_CTL,
2431         .set_optmax     = EBT_SO_SET_MAX + 1,
2432         .set            = do_ebt_set_ctl,
2433 #ifdef CONFIG_COMPAT
2434         .compat_set     = compat_do_ebt_set_ctl,
2435 #endif
2436         .get_optmin     = EBT_BASE_CTL,
2437         .get_optmax     = EBT_SO_GET_MAX + 1,
2438         .get            = do_ebt_get_ctl,
2439 #ifdef CONFIG_COMPAT
2440         .compat_get     = compat_do_ebt_get_ctl,
2441 #endif
2442         .owner          = THIS_MODULE,
2443 };
2444
2445 static int __init ebtables_init(void)
2446 {
2447         int ret;
2448
2449         ret = xt_register_target(&ebt_standard_target);
2450         if (ret < 0)
2451                 return ret;
2452         ret = nf_register_sockopt(&ebt_sockopts);
2453         if (ret < 0) {
2454                 xt_unregister_target(&ebt_standard_target);
2455                 return ret;
2456         }
2457
2458         printk(KERN_INFO "Ebtables v2.0 registered\n");
2459         return 0;
2460 }
2461
2462 static void __exit ebtables_fini(void)
2463 {
2464         nf_unregister_sockopt(&ebt_sockopts);
2465         xt_unregister_target(&ebt_standard_target);
2466         printk(KERN_INFO "Ebtables v2.0 unregistered\n");
2467 }
2468
2469 EXPORT_SYMBOL(ebt_register_table);
2470 EXPORT_SYMBOL(ebt_unregister_table);
2471 EXPORT_SYMBOL(ebt_do_table);
2472 module_init(ebtables_init);
2473 module_exit(ebtables_fini);
2474 MODULE_LICENSE("GPL");