[EBTABLES]: translate_table(): switch direct uses of repl->hook_info to newinfo
[linux-2.6-block.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 *
9 * This code is stongly inspired on 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
18/* used for print_string */
19#include <linux/sched.h>
20#include <linux/tty.h>
21
22#include <linux/kmod.h>
23#include <linux/module.h>
24#include <linux/vmalloc.h>
25#include <linux/netfilter_bridge/ebtables.h>
26#include <linux/spinlock.h>
df0933dc 27#include <linux/mutex.h>
1da177e4
LT
28#include <asm/uaccess.h>
29#include <linux/smp.h>
c8923c6b 30#include <linux/cpumask.h>
1da177e4
LT
31#include <net/sock.h>
32/* needed for logical [in,out]-dev filtering */
33#include "../br_private.h"
34
1da177e4
LT
35#define BUGPRINT(format, args...) printk("kernel msg: ebtables bug: please "\
36 "report to author: "format, ## args)
37/* #define BUGPRINT(format, args...) */
1da177e4
LT
38#define MEMPRINT(format, args...) printk("kernel msg: ebtables "\
39 ": out of memory: "format, ## args)
40/* #define MEMPRINT(format, args...) */
41
42
43
44/*
45 * Each cpu has its own set of counters, so there is no need for write_lock in
46 * the softirq
47 * For reading or updating the counters, the user context needs to
48 * get a write_lock
49 */
50
51/* The size of each set of counters is altered to get cache alignment */
52#define SMP_ALIGN(x) (((x) + SMP_CACHE_BYTES-1) & ~(SMP_CACHE_BYTES-1))
53#define COUNTER_OFFSET(n) (SMP_ALIGN(n * sizeof(struct ebt_counter)))
54#define COUNTER_BASE(c, n, cpu) ((struct ebt_counter *)(((char *)c) + \
55 COUNTER_OFFSET(n) * cpu))
56
57
58
57b47a53 59static DEFINE_MUTEX(ebt_mutex);
1da177e4
LT
60static LIST_HEAD(ebt_tables);
61static LIST_HEAD(ebt_targets);
62static LIST_HEAD(ebt_matches);
63static LIST_HEAD(ebt_watchers);
64
65static struct ebt_target ebt_standard_target =
66{ {NULL, NULL}, EBT_STANDARD_TARGET, NULL, NULL, NULL, NULL};
67
68static inline int ebt_do_watcher (struct ebt_entry_watcher *w,
69 const struct sk_buff *skb, unsigned int hooknr, const struct net_device *in,
70 const struct net_device *out)
71{
72 w->u.watcher->watcher(skb, hooknr, in, out, w->data,
73 w->watcher_size);
74 /* watchers don't give a verdict */
75 return 0;
76}
77
78static inline int ebt_do_match (struct ebt_entry_match *m,
79 const struct sk_buff *skb, const struct net_device *in,
80 const struct net_device *out)
81{
82 return m->u.match->match(skb, in, out, m->data,
83 m->match_size);
84}
85
86static inline int ebt_dev_check(char *entry, const struct net_device *device)
87{
88 int i = 0;
6f5b7ef6 89 const char *devname = device->name;
1da177e4
LT
90
91 if (*entry == '\0')
92 return 0;
93 if (!device)
94 return 1;
95 /* 1 is the wildcard token */
96 while (entry[i] != '\0' && entry[i] != 1 && entry[i] == devname[i])
97 i++;
98 return (devname[i] != entry[i] && entry[i] != 1);
99}
100
101#define FWINV2(bool,invflg) ((bool) ^ !!(e->invflags & invflg))
102/* process standard matches */
103static inline int ebt_basic_match(struct ebt_entry *e, struct ethhdr *h,
104 const struct net_device *in, const struct net_device *out)
105{
106 int verdict, i;
107
108 if (e->bitmask & EBT_802_3) {
109 if (FWINV2(ntohs(h->h_proto) >= 1536, EBT_IPROTO))
110 return 1;
111 } else if (!(e->bitmask & EBT_NOPROTO) &&
112 FWINV2(e->ethproto != h->h_proto, EBT_IPROTO))
113 return 1;
114
115 if (FWINV2(ebt_dev_check(e->in, in), EBT_IIN))
116 return 1;
117 if (FWINV2(ebt_dev_check(e->out, out), EBT_IOUT))
118 return 1;
119 if ((!in || !in->br_port) ? 0 : FWINV2(ebt_dev_check(
120 e->logical_in, in->br_port->br->dev), EBT_ILOGICALIN))
121 return 1;
122 if ((!out || !out->br_port) ? 0 : FWINV2(ebt_dev_check(
123 e->logical_out, out->br_port->br->dev), EBT_ILOGICALOUT))
124 return 1;
125
126 if (e->bitmask & EBT_SOURCEMAC) {
127 verdict = 0;
128 for (i = 0; i < 6; i++)
129 verdict |= (h->h_source[i] ^ e->sourcemac[i]) &
130 e->sourcemsk[i];
131 if (FWINV2(verdict != 0, EBT_ISOURCE) )
132 return 1;
133 }
134 if (e->bitmask & EBT_DESTMAC) {
135 verdict = 0;
136 for (i = 0; i < 6; i++)
137 verdict |= (h->h_dest[i] ^ e->destmac[i]) &
138 e->destmsk[i];
139 if (FWINV2(verdict != 0, EBT_IDEST) )
140 return 1;
141 }
142 return 0;
143}
144
145/* Do some firewalling */
146unsigned int ebt_do_table (unsigned int hook, struct sk_buff **pskb,
147 const struct net_device *in, const struct net_device *out,
148 struct ebt_table *table)
149{
150 int i, nentries;
151 struct ebt_entry *point;
152 struct ebt_counter *counter_base, *cb_base;
153 struct ebt_entry_target *t;
154 int verdict, sp = 0;
155 struct ebt_chainstack *cs;
156 struct ebt_entries *chaininfo;
157 char *base;
158 struct ebt_table_info *private;
159
160 read_lock_bh(&table->lock);
161 private = table->private;
162 cb_base = COUNTER_BASE(private->counters, private->nentries,
163 smp_processor_id());
164 if (private->chainstack)
165 cs = private->chainstack[smp_processor_id()];
166 else
167 cs = NULL;
168 chaininfo = private->hook_entry[hook];
169 nentries = private->hook_entry[hook]->nentries;
170 point = (struct ebt_entry *)(private->hook_entry[hook]->data);
171 counter_base = cb_base + private->hook_entry[hook]->counter_offset;
172 /* base for chain jumps */
173 base = private->entries;
174 i = 0;
175 while (i < nentries) {
176 if (ebt_basic_match(point, eth_hdr(*pskb), in, out))
177 goto letscontinue;
178
179 if (EBT_MATCH_ITERATE(point, ebt_do_match, *pskb, in, out) != 0)
180 goto letscontinue;
181
182 /* increase counter */
183 (*(counter_base + i)).pcnt++;
184 (*(counter_base + i)).bcnt+=(**pskb).len;
185
186 /* these should only watch: not modify, nor tell us
187 what to do with the packet */
188 EBT_WATCHER_ITERATE(point, ebt_do_watcher, *pskb, hook, in,
189 out);
190
191 t = (struct ebt_entry_target *)
192 (((char *)point) + point->target_offset);
193 /* standard target */
194 if (!t->u.target->target)
195 verdict = ((struct ebt_standard_target *)t)->verdict;
196 else
197 verdict = t->u.target->target(pskb, hook,
198 in, out, t->data, t->target_size);
199 if (verdict == EBT_ACCEPT) {
200 read_unlock_bh(&table->lock);
201 return NF_ACCEPT;
202 }
203 if (verdict == EBT_DROP) {
204 read_unlock_bh(&table->lock);
205 return NF_DROP;
206 }
207 if (verdict == EBT_RETURN) {
208letsreturn:
209#ifdef CONFIG_NETFILTER_DEBUG
210 if (sp == 0) {
211 BUGPRINT("RETURN on base chain");
212 /* act like this is EBT_CONTINUE */
213 goto letscontinue;
214 }
215#endif
216 sp--;
217 /* put all the local variables right */
218 i = cs[sp].n;
219 chaininfo = cs[sp].chaininfo;
220 nentries = chaininfo->nentries;
221 point = cs[sp].e;
222 counter_base = cb_base +
223 chaininfo->counter_offset;
224 continue;
225 }
226 if (verdict == EBT_CONTINUE)
227 goto letscontinue;
228#ifdef CONFIG_NETFILTER_DEBUG
229 if (verdict < 0) {
230 BUGPRINT("bogus standard verdict\n");
231 read_unlock_bh(&table->lock);
232 return NF_DROP;
233 }
234#endif
235 /* jump to a udc */
236 cs[sp].n = i + 1;
237 cs[sp].chaininfo = chaininfo;
238 cs[sp].e = (struct ebt_entry *)
239 (((char *)point) + point->next_offset);
240 i = 0;
241 chaininfo = (struct ebt_entries *) (base + verdict);
242#ifdef CONFIG_NETFILTER_DEBUG
243 if (chaininfo->distinguisher) {
244 BUGPRINT("jump to non-chain\n");
245 read_unlock_bh(&table->lock);
246 return NF_DROP;
247 }
248#endif
249 nentries = chaininfo->nentries;
250 point = (struct ebt_entry *)chaininfo->data;
251 counter_base = cb_base + chaininfo->counter_offset;
252 sp++;
253 continue;
254letscontinue:
255 point = (struct ebt_entry *)
256 (((char *)point) + point->next_offset);
257 i++;
258 }
259
260 /* I actually like this :) */
261 if (chaininfo->policy == EBT_RETURN)
262 goto letsreturn;
263 if (chaininfo->policy == EBT_ACCEPT) {
264 read_unlock_bh(&table->lock);
265 return NF_ACCEPT;
266 }
267 read_unlock_bh(&table->lock);
268 return NF_DROP;
269}
270
271/* If it succeeds, returns element and locks mutex */
272static inline void *
273find_inlist_lock_noload(struct list_head *head, const char *name, int *error,
57b47a53 274 struct mutex *mutex)
1da177e4 275{
df0933dc
PM
276 struct {
277 struct list_head list;
278 char name[EBT_FUNCTION_MAXNAMELEN];
279 } *e;
1da177e4 280
57b47a53 281 *error = mutex_lock_interruptible(mutex);
1da177e4
LT
282 if (*error != 0)
283 return NULL;
284
df0933dc
PM
285 list_for_each_entry(e, head, list) {
286 if (strcmp(e->name, name) == 0)
287 return e;
1da177e4 288 }
df0933dc
PM
289 *error = -ENOENT;
290 mutex_unlock(mutex);
291 return NULL;
1da177e4
LT
292}
293
294#ifndef CONFIG_KMOD
295#define find_inlist_lock(h,n,p,e,m) find_inlist_lock_noload((h),(n),(e),(m))
296#else
297static void *
298find_inlist_lock(struct list_head *head, const char *name, const char *prefix,
57b47a53 299 int *error, struct mutex *mutex)
1da177e4
LT
300{
301 void *ret;
302
303 ret = find_inlist_lock_noload(head, name, error, mutex);
304 if (!ret) {
305 request_module("%s%s", prefix, name);
306 ret = find_inlist_lock_noload(head, name, error, mutex);
307 }
308 return ret;
309}
310#endif
311
312static inline struct ebt_table *
57b47a53 313find_table_lock(const char *name, int *error, struct mutex *mutex)
1da177e4
LT
314{
315 return find_inlist_lock(&ebt_tables, name, "ebtable_", error, mutex);
316}
317
318static inline struct ebt_match *
57b47a53 319find_match_lock(const char *name, int *error, struct mutex *mutex)
1da177e4
LT
320{
321 return find_inlist_lock(&ebt_matches, name, "ebt_", error, mutex);
322}
323
324static inline struct ebt_watcher *
57b47a53 325find_watcher_lock(const char *name, int *error, struct mutex *mutex)
1da177e4
LT
326{
327 return find_inlist_lock(&ebt_watchers, name, "ebt_", error, mutex);
328}
329
330static inline struct ebt_target *
57b47a53 331find_target_lock(const char *name, int *error, struct mutex *mutex)
1da177e4
LT
332{
333 return find_inlist_lock(&ebt_targets, name, "ebt_", error, mutex);
334}
335
336static inline int
337ebt_check_match(struct ebt_entry_match *m, struct ebt_entry *e,
338 const char *name, unsigned int hookmask, unsigned int *cnt)
339{
340 struct ebt_match *match;
14197d54 341 size_t left = ((char *)e + e->watchers_offset) - (char *)m;
1da177e4
LT
342 int ret;
343
14197d54
AV
344 if (left < sizeof(struct ebt_entry_match) ||
345 left - sizeof(struct ebt_entry_match) < m->match_size)
1da177e4
LT
346 return -EINVAL;
347 match = find_match_lock(m->u.name, &ret, &ebt_mutex);
348 if (!match)
349 return ret;
350 m->u.match = match;
351 if (!try_module_get(match->me)) {
57b47a53 352 mutex_unlock(&ebt_mutex);
1da177e4
LT
353 return -ENOENT;
354 }
57b47a53 355 mutex_unlock(&ebt_mutex);
1da177e4
LT
356 if (match->check &&
357 match->check(name, hookmask, e, m->data, m->match_size) != 0) {
358 BUGPRINT("match->check failed\n");
359 module_put(match->me);
360 return -EINVAL;
361 }
362 (*cnt)++;
363 return 0;
364}
365
366static inline int
367ebt_check_watcher(struct ebt_entry_watcher *w, struct ebt_entry *e,
368 const char *name, unsigned int hookmask, unsigned int *cnt)
369{
370 struct ebt_watcher *watcher;
14197d54 371 size_t left = ((char *)e + e->target_offset) - (char *)w;
1da177e4
LT
372 int ret;
373
14197d54
AV
374 if (left < sizeof(struct ebt_entry_watcher) ||
375 left - sizeof(struct ebt_entry_watcher) < w->watcher_size)
1da177e4
LT
376 return -EINVAL;
377 watcher = find_watcher_lock(w->u.name, &ret, &ebt_mutex);
378 if (!watcher)
379 return ret;
380 w->u.watcher = watcher;
381 if (!try_module_get(watcher->me)) {
57b47a53 382 mutex_unlock(&ebt_mutex);
1da177e4
LT
383 return -ENOENT;
384 }
57b47a53 385 mutex_unlock(&ebt_mutex);
1da177e4
LT
386 if (watcher->check &&
387 watcher->check(name, hookmask, e, w->data, w->watcher_size) != 0) {
388 BUGPRINT("watcher->check failed\n");
389 module_put(watcher->me);
390 return -EINVAL;
391 }
392 (*cnt)++;
393 return 0;
394}
395
70fe9af4
AV
396static int ebt_verify_pointers(struct ebt_replace *repl,
397 struct ebt_table_info *newinfo)
1da177e4 398{
70fe9af4
AV
399 unsigned int limit = repl->entries_size;
400 unsigned int valid_hooks = repl->valid_hooks;
401 unsigned int offset = 0;
1da177e4
LT
402 int i;
403
e4fd77de
AV
404 for (i = 0; i < NF_BR_NUMHOOKS; i++)
405 newinfo->hook_entry[i] = NULL;
406
407 newinfo->entries_size = repl->entries_size;
408 newinfo->nentries = repl->nentries;
409
70fe9af4
AV
410 while (offset < limit) {
411 size_t left = limit - offset;
412 struct ebt_entry *e = (void *)newinfo->entries + offset;
bb2ef25c 413
70fe9af4 414 if (left < sizeof(unsigned int))
1da177e4 415 break;
70fe9af4
AV
416
417 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
418 if ((valid_hooks & (1 << i)) == 0)
419 continue;
420 if ((char *)repl->hook_entry[i] == repl->entries + offset)
421 break;
422 }
423
424 if (i != NF_BR_NUMHOOKS || !(e->bitmask & EBT_ENTRY_OR_ENTRIES)) {
425 if (e->bitmask != 0) {
426 /* we make userspace set this right,
427 so there is no misunderstanding */
428 BUGPRINT("EBT_ENTRY_OR_ENTRIES shouldn't be set "
429 "in distinguisher\n");
430 return -EINVAL;
431 }
432 if (i != NF_BR_NUMHOOKS)
433 newinfo->hook_entry[i] = (struct ebt_entries *)e;
434 if (left < sizeof(struct ebt_entries))
435 break;
436 offset += sizeof(struct ebt_entries);
437 } else {
438 if (left < sizeof(struct ebt_entry))
439 break;
440 if (left < e->next_offset)
441 break;
442 offset += e->next_offset;
1da177e4 443 }
22b440bf 444 }
70fe9af4
AV
445 if (offset != limit) {
446 BUGPRINT("entries_size too small\n");
447 return -EINVAL;
448 }
e4fd77de
AV
449
450 /* check if all valid hooks have a chain */
451 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
452 if (!newinfo->hook_entry[i] &&
453 (valid_hooks & (1 << i))) {
454 BUGPRINT("Valid hook without chain\n");
455 return -EINVAL;
456 }
457 }
22b440bf 458 return 0;
22b440bf
AV
459}
460
461/*
462 * this one is very careful, as it is the first function
463 * to parse the userspace data
464 */
465static inline int
466ebt_check_entry_size_and_hooks(struct ebt_entry *e,
467 struct ebt_table_info *newinfo, char *base,
468 struct ebt_entries **hook_entries, unsigned int *n, unsigned int *cnt,
469 unsigned int *totalcnt, unsigned int *udc_cnt, unsigned int valid_hooks)
470{
471 unsigned int offset = (char *)e - newinfo->entries;
472 int i;
473
474 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
475 if ((valid_hooks & (1 << i)) == 0)
476 continue;
477 if ((char *)hook_entries[i] == base + offset)
478 break;
479 }
480 /* beginning of a new chain
481 if i == NF_BR_NUMHOOKS it must be a user defined chain */
482 if (i != NF_BR_NUMHOOKS || !e->bitmask) {
1da177e4
LT
483 /* this checks if the previous chain has as many entries
484 as it said it has */
485 if (*n != *cnt) {
486 BUGPRINT("nentries does not equal the nr of entries "
487 "in the chain\n");
488 return -EINVAL;
489 }
1da177e4
LT
490 if (((struct ebt_entries *)e)->policy != EBT_DROP &&
491 ((struct ebt_entries *)e)->policy != EBT_ACCEPT) {
492 /* only RETURN from udc */
493 if (i != NF_BR_NUMHOOKS ||
494 ((struct ebt_entries *)e)->policy != EBT_RETURN) {
495 BUGPRINT("bad policy\n");
496 return -EINVAL;
497 }
498 }
499 if (i == NF_BR_NUMHOOKS) /* it's a user defined chain */
500 (*udc_cnt)++;
1da177e4
LT
501 if (((struct ebt_entries *)e)->counter_offset != *totalcnt) {
502 BUGPRINT("counter_offset != totalcnt");
503 return -EINVAL;
504 }
505 *n = ((struct ebt_entries *)e)->nentries;
506 *cnt = 0;
507 return 0;
508 }
509 /* a plain old entry, heh */
510 if (sizeof(struct ebt_entry) > e->watchers_offset ||
511 e->watchers_offset > e->target_offset ||
512 e->target_offset >= e->next_offset) {
513 BUGPRINT("entry offsets not in right order\n");
514 return -EINVAL;
515 }
516 /* this is not checked anywhere else */
517 if (e->next_offset - e->target_offset < sizeof(struct ebt_entry_target)) {
518 BUGPRINT("target size too small\n");
519 return -EINVAL;
520 }
1da177e4
LT
521 (*cnt)++;
522 (*totalcnt)++;
523 return 0;
524}
525
526struct ebt_cl_stack
527{
528 struct ebt_chainstack cs;
529 int from;
530 unsigned int hookmask;
531};
532
533/*
534 * we need these positions to check that the jumps to a different part of the
535 * entries is a jump to the beginning of a new chain.
536 */
537static inline int
538ebt_get_udc_positions(struct ebt_entry *e, struct ebt_table_info *newinfo,
539 struct ebt_entries **hook_entries, unsigned int *n, unsigned int valid_hooks,
540 struct ebt_cl_stack *udc)
541{
542 int i;
543
544 /* we're only interested in chain starts */
40642f95 545 if (e->bitmask)
1da177e4
LT
546 return 0;
547 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
548 if ((valid_hooks & (1 << i)) == 0)
549 continue;
550 if (newinfo->hook_entry[i] == (struct ebt_entries *)e)
551 break;
552 }
553 /* only care about udc */
554 if (i != NF_BR_NUMHOOKS)
555 return 0;
556
557 udc[*n].cs.chaininfo = (struct ebt_entries *)e;
558 /* these initialisations are depended on later in check_chainloops() */
559 udc[*n].cs.n = 0;
560 udc[*n].hookmask = 0;
561
562 (*n)++;
563 return 0;
564}
565
566static inline int
567ebt_cleanup_match(struct ebt_entry_match *m, unsigned int *i)
568{
569 if (i && (*i)-- == 0)
570 return 1;
571 if (m->u.match->destroy)
572 m->u.match->destroy(m->data, m->match_size);
573 module_put(m->u.match->me);
574
575 return 0;
576}
577
578static inline int
579ebt_cleanup_watcher(struct ebt_entry_watcher *w, unsigned int *i)
580{
581 if (i && (*i)-- == 0)
582 return 1;
583 if (w->u.watcher->destroy)
584 w->u.watcher->destroy(w->data, w->watcher_size);
585 module_put(w->u.watcher->me);
586
587 return 0;
588}
589
590static inline int
591ebt_cleanup_entry(struct ebt_entry *e, unsigned int *cnt)
592{
593 struct ebt_entry_target *t;
594
40642f95 595 if (e->bitmask == 0)
1da177e4
LT
596 return 0;
597 /* we're done */
598 if (cnt && (*cnt)-- == 0)
599 return 1;
600 EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, NULL);
601 EBT_MATCH_ITERATE(e, ebt_cleanup_match, NULL);
602 t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
603 if (t->u.target->destroy)
604 t->u.target->destroy(t->data, t->target_size);
605 module_put(t->u.target->me);
606
607 return 0;
608}
609
610static inline int
611ebt_check_entry(struct ebt_entry *e, struct ebt_table_info *newinfo,
612 const char *name, unsigned int *cnt, unsigned int valid_hooks,
613 struct ebt_cl_stack *cl_s, unsigned int udc_cnt)
614{
615 struct ebt_entry_target *t;
616 struct ebt_target *target;
617 unsigned int i, j, hook = 0, hookmask = 0;
14197d54 618 size_t gap = e->next_offset - e->target_offset;
1da177e4
LT
619 int ret;
620
621 /* don't mess with the struct ebt_entries */
40642f95 622 if (e->bitmask == 0)
1da177e4
LT
623 return 0;
624
625 if (e->bitmask & ~EBT_F_MASK) {
626 BUGPRINT("Unknown flag for bitmask\n");
627 return -EINVAL;
628 }
629 if (e->invflags & ~EBT_INV_MASK) {
630 BUGPRINT("Unknown flag for inv bitmask\n");
631 return -EINVAL;
632 }
633 if ( (e->bitmask & EBT_NOPROTO) && (e->bitmask & EBT_802_3) ) {
634 BUGPRINT("NOPROTO & 802_3 not allowed\n");
635 return -EINVAL;
636 }
637 /* what hook do we belong to? */
638 for (i = 0; i < NF_BR_NUMHOOKS; i++) {
639 if ((valid_hooks & (1 << i)) == 0)
640 continue;
641 if ((char *)newinfo->hook_entry[i] < (char *)e)
642 hook = i;
643 else
644 break;
645 }
646 /* (1 << NF_BR_NUMHOOKS) tells the check functions the rule is on
647 a base chain */
648 if (i < NF_BR_NUMHOOKS)
649 hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
650 else {
651 for (i = 0; i < udc_cnt; i++)
652 if ((char *)(cl_s[i].cs.chaininfo) > (char *)e)
653 break;
654 if (i == 0)
655 hookmask = (1 << hook) | (1 << NF_BR_NUMHOOKS);
656 else
657 hookmask = cl_s[i - 1].hookmask;
658 }
659 i = 0;
660 ret = EBT_MATCH_ITERATE(e, ebt_check_match, e, name, hookmask, &i);
661 if (ret != 0)
662 goto cleanup_matches;
663 j = 0;
664 ret = EBT_WATCHER_ITERATE(e, ebt_check_watcher, e, name, hookmask, &j);
665 if (ret != 0)
666 goto cleanup_watchers;
667 t = (struct ebt_entry_target *)(((char *)e) + e->target_offset);
668 target = find_target_lock(t->u.name, &ret, &ebt_mutex);
669 if (!target)
670 goto cleanup_watchers;
671 if (!try_module_get(target->me)) {
57b47a53 672 mutex_unlock(&ebt_mutex);
1da177e4
LT
673 ret = -ENOENT;
674 goto cleanup_watchers;
675 }
57b47a53 676 mutex_unlock(&ebt_mutex);
1da177e4
LT
677
678 t->u.target = target;
679 if (t->u.target == &ebt_standard_target) {
14197d54 680 if (gap < sizeof(struct ebt_standard_target)) {
1da177e4
LT
681 BUGPRINT("Standard target size too big\n");
682 ret = -EFAULT;
683 goto cleanup_watchers;
684 }
685 if (((struct ebt_standard_target *)t)->verdict <
686 -NUM_STANDARD_TARGETS) {
687 BUGPRINT("Invalid standard target\n");
688 ret = -EFAULT;
689 goto cleanup_watchers;
690 }
14197d54 691 } else if (t->target_size > gap - sizeof(struct ebt_entry_target) ||
1da177e4
LT
692 (t->u.target->check &&
693 t->u.target->check(name, hookmask, e, t->data, t->target_size) != 0)){
694 module_put(t->u.target->me);
695 ret = -EFAULT;
696 goto cleanup_watchers;
697 }
698 (*cnt)++;
699 return 0;
700cleanup_watchers:
701 EBT_WATCHER_ITERATE(e, ebt_cleanup_watcher, &j);
702cleanup_matches:
703 EBT_MATCH_ITERATE(e, ebt_cleanup_match, &i);
704 return ret;
705}
706
707/*
708 * checks for loops and sets the hook mask for udc
709 * the hook mask for udc tells us from which base chains the udc can be
710 * accessed. This mask is a parameter to the check() functions of the extensions
711 */
712static int check_chainloops(struct ebt_entries *chain, struct ebt_cl_stack *cl_s,
713 unsigned int udc_cnt, unsigned int hooknr, char *base)
714{
715 int i, chain_nr = -1, pos = 0, nentries = chain->nentries, verdict;
716 struct ebt_entry *e = (struct ebt_entry *)chain->data;
717 struct ebt_entry_target *t;
718
719 while (pos < nentries || chain_nr != -1) {
720 /* end of udc, go back one 'recursion' step */
721 if (pos == nentries) {
722 /* put back values of the time when this chain was called */
723 e = cl_s[chain_nr].cs.e;
724 if (cl_s[chain_nr].from != -1)
725 nentries =
726 cl_s[cl_s[chain_nr].from].cs.chaininfo->nentries;
727 else
728 nentries = chain->nentries;
729 pos = cl_s[chain_nr].cs.n;
730 /* make sure we won't see a loop that isn't one */
731 cl_s[chain_nr].cs.n = 0;
732 chain_nr = cl_s[chain_nr].from;
733 if (pos == nentries)
734 continue;
735 }
736 t = (struct ebt_entry_target *)
737 (((char *)e) + e->target_offset);
738 if (strcmp(t->u.name, EBT_STANDARD_TARGET))
739 goto letscontinue;
740 if (e->target_offset + sizeof(struct ebt_standard_target) >
741 e->next_offset) {
742 BUGPRINT("Standard target size too big\n");
743 return -1;
744 }
745 verdict = ((struct ebt_standard_target *)t)->verdict;
746 if (verdict >= 0) { /* jump to another chain */
747 struct ebt_entries *hlp2 =
748 (struct ebt_entries *)(base + verdict);
749 for (i = 0; i < udc_cnt; i++)
750 if (hlp2 == cl_s[i].cs.chaininfo)
751 break;
752 /* bad destination or loop */
753 if (i == udc_cnt) {
754 BUGPRINT("bad destination\n");
755 return -1;
756 }
757 if (cl_s[i].cs.n) {
758 BUGPRINT("loop\n");
759 return -1;
760 }
98a0824a
AV
761 if (cl_s[i].hookmask & (1 << hooknr))
762 goto letscontinue;
763 /* this can't be 0, so the loop test is correct */
1da177e4
LT
764 cl_s[i].cs.n = pos + 1;
765 pos = 0;
766 cl_s[i].cs.e = ((void *)e + e->next_offset);
767 e = (struct ebt_entry *)(hlp2->data);
768 nentries = hlp2->nentries;
769 cl_s[i].from = chain_nr;
770 chain_nr = i;
771 /* this udc is accessible from the base chain for hooknr */
772 cl_s[i].hookmask |= (1 << hooknr);
773 continue;
774 }
775letscontinue:
776 e = (void *)e + e->next_offset;
777 pos++;
778 }
779 return 0;
780}
781
782/* do the parsing of the table/chains/entries/matches/watchers/targets, heh */
783static int translate_table(struct ebt_replace *repl,
784 struct ebt_table_info *newinfo)
785{
786 unsigned int i, j, k, udc_cnt;
787 int ret;
788 struct ebt_cl_stack *cl_s = NULL; /* used in the checking for chain loops */
789
e4fd77de
AV
790 ret = ebt_verify_pointers(repl, newinfo);
791 if (ret != 0)
792 return ret;
793
1da177e4 794 i = 0;
1f072c96 795 while (i < NF_BR_NUMHOOKS && !newinfo->hook_entry[i])
1da177e4
LT
796 i++;
797 if (i == NF_BR_NUMHOOKS) {
798 BUGPRINT("No valid hooks specified\n");
799 return -EINVAL;
800 }
1f072c96 801 if (newinfo->hook_entry[i] != (struct ebt_entries *)newinfo->entries) {
1da177e4
LT
802 BUGPRINT("Chains don't start at beginning\n");
803 return -EINVAL;
804 }
805 /* make sure chains are ordered after each other in same order
806 as their corresponding hooks */
807 for (j = i + 1; j < NF_BR_NUMHOOKS; j++) {
1f072c96 808 if (!newinfo->hook_entry[j])
1da177e4 809 continue;
1f072c96 810 if (newinfo->hook_entry[j] <= newinfo->hook_entry[i]) {
1da177e4
LT
811 BUGPRINT("Hook order must be followed\n");
812 return -EINVAL;
813 }
814 i = j;
815 }
816
1da177e4
LT
817 /* do some early checkings and initialize some things */
818 i = 0; /* holds the expected nr. of entries for the chain */
819 j = 0; /* holds the up to now counted entries for the chain */
820 k = 0; /* holds the total nr. of entries, should equal
821 newinfo->nentries afterwards */
822 udc_cnt = 0; /* will hold the nr. of user defined chains (udc) */
823 ret = EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
824 ebt_check_entry_size_and_hooks, newinfo, repl->entries,
22b440bf 825 repl->hook_entry, &i, &j, &k,
1da177e4
LT
826 &udc_cnt, repl->valid_hooks);
827
828 if (ret != 0)
829 return ret;
830
831 if (i != j) {
832 BUGPRINT("nentries does not equal the nr of entries in the "
833 "(last) chain\n");
834 return -EINVAL;
835 }
836 if (k != newinfo->nentries) {
837 BUGPRINT("Total nentries is wrong\n");
838 return -EINVAL;
839 }
840
1da177e4
LT
841 /* get the location of the udc, put them in an array
842 while we're at it, allocate the chainstack */
843 if (udc_cnt) {
844 /* this will get free'd in do_replace()/ebt_register_table()
845 if an error occurs */
7ad4d2f6
J
846 newinfo->chainstack =
847 vmalloc((highest_possible_processor_id()+1)
848 * sizeof(*(newinfo->chainstack)));
1da177e4
LT
849 if (!newinfo->chainstack)
850 return -ENOMEM;
6f912042 851 for_each_possible_cpu(i) {
1da177e4 852 newinfo->chainstack[i] =
18bc89aa 853 vmalloc(udc_cnt * sizeof(*(newinfo->chainstack[0])));
1da177e4
LT
854 if (!newinfo->chainstack[i]) {
855 while (i)
856 vfree(newinfo->chainstack[--i]);
857 vfree(newinfo->chainstack);
858 newinfo->chainstack = NULL;
859 return -ENOMEM;
860 }
861 }
862
18bc89aa 863 cl_s = vmalloc(udc_cnt * sizeof(*cl_s));
1da177e4
LT
864 if (!cl_s)
865 return -ENOMEM;
866 i = 0; /* the i'th udc */
867 EBT_ENTRY_ITERATE(newinfo->entries, newinfo->entries_size,
868 ebt_get_udc_positions, newinfo, repl->hook_entry, &i,
869 repl->valid_hooks, cl_s);
870 /* sanity check */
871 if (i != udc_cnt) {
872 BUGPRINT("i != udc_cnt\n");
873 vfree(cl_s);
874 return -EFAULT;
875 }
876 }
877
878 /* Check for loops */
879 for (i = 0; i < NF_BR_NUMHOOKS; i++)
1f072c96 880 if (newinfo->hook_entry[i])
1da177e4
LT
881 if (check_chainloops(newinfo->hook_entry[i],
882 cl_s, udc_cnt, i, newinfo->entries)) {
68d31872 883 vfree(cl_s);
1da177e4
LT
884 return -EINVAL;
885 }
886
887