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