]> git.ipfire.org Git - thirdparty/bird.git/blame - filter/filter.c
Merge branch 'master' into mq-filter-stack
[thirdparty/bird.git] / filter / filter.c
CommitLineData
23b1539b
PM
1/*
2 * Filters: utility functions
3 *
4 * Copyright 1998 Pavel Machek <pavel@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
29818140 7 *
23b1539b
PM
8 */
9
2337ade7
PM
10/**
11 * DOC: Filters
12 *
725270cb
MM
13 * You can find sources of the filter language in |filter/|
14 * directory. File |filter/config.Y| contains filter grammar and basically translates
15 * the source from user into a tree of &f_inst structures. These trees are
16 * later interpreted using code in |filter/filter.c|.
fe613ecd 17 *
725270cb
MM
18 * A filter is represented by a tree of &f_inst structures, one structure per
19 * "instruction". Each &f_inst contains @code, @aux value which is
20 * usually the data type this instruction operates on and two generic
7f0ac737
MM
21 * arguments (@a[0], @a[1]). Some instructions contain pointer(s) to other
22 * instructions in their (@a[0], @a[1]) fields.
2337ade7 23 *
725270cb
MM
24 * Filters use a &f_val structure for their data. Each &f_val
25 * contains type and value (types are constants prefixed with %T_). Few
26 * of the types are special; %T_RETURN can be or-ed with a type to indicate
27 * that return from a function or from the whole filter should be
28 * forced. Important thing about &f_val's is that they may be copied
29 * with a simple |=|. That's fine for all currently defined types: strings
2337ade7 30 * are read-only (and therefore okay), paths are copied for each
4c5f93d7
PM
31 * operation (okay too).
32 */
2337ade7 33
9a220cab 34#undef LOCAL_DEBUG
6b9fa320 35
23b1539b
PM
36#include "nest/bird.h"
37#include "lib/lists.h"
38#include "lib/resource.h"
39#include "lib/socket.h"
38506f71 40#include "lib/string.h"
7f77e250 41#include "lib/unaligned.h"
0264ccf6
PT
42#include "lib/net.h"
43#include "lib/ip.h"
23b1539b
PM
44#include "nest/route.h"
45#include "nest/protocol.h"
46#include "nest/iface.h"
159fa4ce 47#include "nest/attrs.h"
23b1539b
PM
48#include "conf/conf.h"
49#include "filter/filter.h"
8bdb05ed 50#include "filter/f-inst.h"
4f082dfa 51#include "filter/data.h"
23b1539b 52
1757a6fc 53
a84b8b6e
MM
54/* Exception bits */
55enum f_exception {
56 FE_RETURN = 0x1,
57};
58
59
1757a6fc
MM
60struct filter_stack {
61 /* Value stack for execution */
62#define F_VAL_STACK_MAX 4096
63 uint vcnt; /* Current value stack size; 0 for empty */
64 uint ecnt; /* Current execute stack size; 0 for empty */
65
66 struct f_val vstk[F_VAL_STACK_MAX]; /* The stack itself */
67
68 /* Instruction stack for execution */
69#define F_EXEC_STACK_MAX 4096
70 struct {
71 const struct f_line *line; /* The line that is being executed */
72 uint pos; /* Instruction index in the line */
73 uint ventry; /* Value stack depth on entry */
74 uint vbase; /* Where to index variable positions from */
75 enum f_exception emask; /* Exception mask */
76 } estk[F_EXEC_STACK_MAX];
77};
78
a946317f 79/* Internal filter state, to be allocated on stack when executing filters */
6479e403 80struct filter_state {
1757a6fc
MM
81 /* Stacks needed for execution */
82 struct filter_stack *stack;
83
20c6ea70 84 /* The route we are processing. This may be NULL to indicate no route available. */
a946317f 85 struct rte **rte;
20c6ea70
JMM
86
87 /* The old rta to be freed after filters are done. */
a946317f 88 struct rta *old_rta;
20c6ea70
JMM
89
90 /* Cached pointer to ea_list */
a946317f 91 struct ea_list **eattrs;
aa6c5f4d
MM
92
93 /* Linpool for adata allocation */
a946317f 94 struct linpool *pool;
aa6c5f4d
MM
95
96 /* Buffer for log output */
a946317f 97 struct buffer buf;
aa6c5f4d
MM
98
99 /* Filter execution flags */
a946317f 100 int flags;
6479e403
JMM
101};
102
103#if HAVE_THREAD_LOCAL
104_Thread_local static struct filter_state filter_state;
1757a6fc
MM
105_Thread_local static struct filter_stack filter_stack;
106#define FS_INIT(...) filter_state = (struct filter_state) { .stack = &filter_stack, __VA_ARGS__ }
6479e403 107#else
1757a6fc 108#define FS_INIT(...) struct filter_state filter_state = { .stack = alloca(sizeof(struct filter_stack)), __VA_ARGS__ };
6479e403 109#endif
a946317f 110
4c553c5a 111void (*bt_assert_hook)(int result, const struct f_line_item *assert);
8f8671bc 112
a946317f 113static inline void f_cache_eattrs(struct filter_state *fs)
13c0be19 114{
a946317f 115 fs->eattrs = &((*fs->rte)->attrs->eattrs);
13c0be19
JMM
116}
117
a946317f 118static inline void f_rte_cow(struct filter_state *fs)
a03ede64 119{
a946317f 120 if (!((*fs->rte)->flags & REF_COW))
13c0be19
JMM
121 return;
122
a946317f 123 *fs->rte = rte_cow(*fs->rte);
a03ede64
OZ
124}
125
4c5f93d7 126/*
b093c328
PM
127 * rta_cow - prepare rta for modification by filter
128 */
9831e591 129static void
a946317f 130f_rta_cow(struct filter_state *fs)
26c09e1d 131{
a946317f 132 if (!rta_is_cached((*fs->rte)->attrs))
8d9eef17
OZ
133 return;
134
135 /* Prepare to modify rte */
a946317f 136 f_rte_cow(fs);
8d9eef17
OZ
137
138 /* Store old rta to free it later, it stores reference from rte_cow() */
a946317f 139 fs->old_rta = (*fs->rte)->attrs;
8d9eef17
OZ
140
141 /*
142 * Get shallow copy of rta. Fields eattrs and nexthops of rta are shared
a946317f 143 * with fs->old_rta (they will be copied when the cached rta will be obtained
8d9eef17
OZ
144 * at the end of f_run()), also the lock of hostentry is inherited (we
145 * suppose hostentry is not changed by filters).
146 */
a946317f 147 (*fs->rte)->attrs = rta_do_cow((*fs->rte)->attrs, fs->pool);
13c0be19
JMM
148
149 /* Re-cache the ea_list */
a946317f 150 f_cache_eattrs(fs);
26c09e1d
PM
151}
152
1123e707 153static struct tbf rl_runtime_err = TBF_DEFAULT_LOG_LIMITS;
cb530392 154
b093c328
PM
155/**
156 * interpret
a946317f 157 * @fs: filter state
2e9b2421 158 * @what: filter to interpret
b093c328 159 *
4c5f93d7 160 * Interpret given tree of filter instructions. This is core function
b093c328 161 * of filter system and does all the hard work.
771ae456
PM
162 *
163 * Each instruction has 4 fields: code (which is instruction code),
164 * aux (which is extension to instruction code, typically type),
165 * arg1 and arg2 - arguments. Depending on instruction, arguments
315f23a0 166 * are either integers, or pointers to instruction trees. Common
771ae456
PM
167 * instructions like +, that have two expressions as arguments use
168 * TWOARGS macro to get both of them evaluated.
b093c328 169 */
7afa1438 170static enum filter_return
4c553c5a 171interpret(struct filter_state *fs, const struct f_line *line, struct f_val *val)
23b1539b 172{
96d757c1
JMM
173 /* No arguments allowed */
174 ASSERT(line->args == 0);
8bdb05ed 175
1757a6fc
MM
176 /* Initialize the filter stack */
177 struct filter_stack *fstk = fs->stack;
96d757c1 178
1757a6fc
MM
179 fstk->vcnt = line->vars;
180 memset(fstk->vstk, 0, sizeof(struct f_val) * line->vars);
8bdb05ed
MM
181
182 /* The same as with the value stack. Not resetting the stack for performance reasons. */
1757a6fc
MM
183 fstk->ecnt = 1;
184 fstk->estk[0].line = line;
185 fstk->estk[0].pos = 0;
fc8df41e 186
1757a6fc 187#define curline fstk->estk[fstk->ecnt-1]
4c553c5a 188
ea4f55e3
MM
189#if DEBUGGING
190 debug("Interpreting line.");
191 f_dump_line(line, 1);
192#endif
193
1757a6fc 194 while (fstk->ecnt > 0) {
4c553c5a
MM
195 while (curline.pos < curline.line->len) {
196 const struct f_line_item *what = &(curline.line->items[curline.pos++]);
7afa1438 197
4c553c5a 198 switch (what->fi_code) {
1757a6fc
MM
199#define res fstk->vstk[fstk->vcnt]
200#define v1 fstk->vstk[fstk->vcnt]
201#define v2 fstk->vstk[fstk->vcnt + 1]
202#define v3 fstk->vstk[fstk->vcnt + 2]
7afa1438 203
a84b8b6e
MM
204#define runtime(fmt, ...) do { \
205 if (!(fs->flags & FF_SILENT)) \
206 log_rl(&rl_runtime_err, L_ERR "filters, line %d: " fmt, what->lineno, ##__VA_ARGS__); \
207 return F_ERROR; \
208} while(0)
209
b40c0f02
MM
210#define falloc(size) lp_alloc(fs->pool, size)
211#define fpool fs->pool
212
a84b8b6e
MM
213#define ACCESS_RTE do { if (!fs->rte) runtime("No route to access"); } while (0)
214#define ACCESS_EATTRS do { if (!fs->eattrs) f_cache_eattrs(fs); } while (0)
215
d1039926 216#include "filter/inst-interpret.c"
7afa1438 217#undef res
4c553c5a
MM
218#undef v1
219#undef v2
220#undef v3
aca82639 221#undef runtime
b40c0f02
MM
222#undef falloc
223#undef fpool
aca82639
JMM
224#undef ACCESS_RTE
225#undef ACCESS_EATTRS
4c553c5a 226 }
a8740d6c 227 }
96d757c1
JMM
228
229 /* End of current line. Drop local variables before exiting. */
1757a6fc
MM
230 fstk->vcnt -= curline.line->vars;
231 fstk->vcnt -= curline.line->args;
232 fstk->ecnt--;
a8740d6c 233 }
23b1539b 234
1757a6fc 235 if (fstk->vcnt == 0) {
96d757c1
JMM
236 if (val) {
237 log_rl(&rl_runtime_err, L_ERR "filters: No value left on stack");
4c553c5a 238 return F_ERROR;
96d757c1
JMM
239 }
240 return F_NOP;
4c553c5a 241 }
96d757c1 242
1757a6fc
MM
243 if (val && (fstk->vcnt == 1)) {
244 *val = fstk->vstk[0];
96d757c1
JMM
245 return F_NOP;
246 }
247
1757a6fc 248 log_rl(&rl_runtime_err, L_ERR "Too many items left on stack: %u", fstk->vcnt);
96d757c1 249 return F_ERROR;
4c553c5a 250}
0ec6b5ec 251
9a4037d4 252
ff95080f 253/**
a03ede64
OZ
254 * f_run - run a filter for a route
255 * @filter: filter to run
256 * @rte: route being filtered, may be modified
ff95080f 257 * @tmp_pool: all filter allocations go from this pool
4c5f93d7 258 * @flags: flags
a03ede64
OZ
259 *
260 * If filter needs to modify the route, there are several
261 * posibilities. @rte might be read-only (with REF_COW flag), in that
262 * case rw copy is obtained by rte_cow() and @rte is replaced. If
263 * @rte is originally rw, it may be directly modified (and it is never
264 * copied).
265 *
266 * The returned rte may reuse the (possibly cached, cloned) rta, or
20c6ea70 267 * (if rta was modified) contains a modified uncached rta, which
a03ede64
OZ
268 * uses parts allocated from @tmp_pool and parts shared from original
269 * rta. There is one exception - if @rte is rw but contains a cached
270 * rta and that is modified, rta in returned rte is also cached.
271 *
272 * Ownership of cached rtas is consistent with rte, i.e.
273 * if a new rte is returned, it has its own clone of cached rta
274 * (and cached rta of read-only source rte is intact), if rte is
275 * modified in place, old cached rta is possibly freed.
ff95080f 276 */
7afa1438 277enum filter_return
4c553c5a 278f_run(const struct filter *filter, struct rte **rte, struct linpool *tmp_pool, int flags)
23b1539b 279{
36da2857
OZ
280 if (filter == FILTER_ACCEPT)
281 return F_ACCEPT;
282
283 if (filter == FILTER_REJECT)
284 return F_REJECT;
285
a03ede64 286 int rte_cow = ((*rte)->flags & REF_COW);
6b9fa320 287 DBG( "Running filter `%s'...", filter->name );
23b1539b 288
20c6ea70 289 /* Initialize the filter state */
6479e403
JMM
290 FS_INIT(
291 .rte = rte,
292 .pool = tmp_pool,
293 .flags = flags,
294 );
0d1b3c4c 295
20c6ea70 296 LOG_BUFFER_INIT(filter_state.buf);
0e175f9f 297
20c6ea70
JMM
298 /* Run the interpreter itself */
299 enum filter_return fret = interpret(&filter_state, filter->root, NULL);
a03ede64 300
20c6ea70 301 if (filter_state.old_rta) {
a03ede64 302 /*
20c6ea70 303 * Cached rta was modified and filter_state->rte contains now an uncached one,
a03ede64 304 * sharing some part with the cached one. The cached rta should
20c6ea70 305 * be freed (if rte was originally COW, filter_state->old_rta is a clone
a03ede64
OZ
306 * obtained during rte_cow()).
307 *
308 * This also implements the exception mentioned in f_run()
309 * description. The reason for this is that rta reuses parts of
20c6ea70 310 * filter_state->old_rta, and these may be freed during rta_free(filter_state->old_rta).
a03ede64
OZ
311 * This is not the problem if rte was COW, because original rte
312 * also holds the same rta.
313 */
20c6ea70
JMM
314 if (!rte_cow) {
315 /* Cache the new attrs */
316 (*filter_state.rte)->attrs = rta_lookup((*filter_state.rte)->attrs);
a03ede64 317
20c6ea70
JMM
318 /* Drop cached ea_list pointer */
319 filter_state.eattrs = NULL;
320 }
a03ede64 321
20c6ea70
JMM
322 /* Uncache the old attrs and drop the pointer as it is invalid now. */
323 rta_free(filter_state.old_rta);
324 filter_state.old_rta = NULL;
325 }
0d1b3c4c 326
20c6ea70 327 /* Process the filter output, log it and return */
7afa1438 328 if (fret < F_ACCEPT) {
20c6ea70 329 if (!(filter_state.flags & FF_SILENT))
f249d0b8 330 log_rl(&rl_runtime_err, L_ERR "Filter %s did not return accept nor reject. Make up your mind", filter_name(filter));
23b1539b 331 return F_ERROR;
0b1cad81 332 }
52e030e1 333 DBG( "done (%u)\n", res.val.i );
7afa1438 334 return fret;
23b1539b
PM
335}
336
20c6ea70
JMM
337/**
338 * f_eval_rte – run a filter line for an uncached route
339 * @expr: filter line to run
340 * @rte: route being filtered, may be modified
341 * @tmp_pool: all filter allocations go from this pool
342 *
343 * This specific filter entry point runs the given filter line
344 * (which must not have any arguments) on the given route.
345 *
346 * The route MUST NOT have REF_COW set and its attributes MUST NOT
347 * be cached by rta_lookup().
348 */
1321e12a 349
7afa1438 350enum filter_return
4c553c5a 351f_eval_rte(const struct f_line *expr, struct rte **rte, struct linpool *tmp_pool)
1321e12a 352{
6479e403
JMM
353 FS_INIT(
354 .rte = rte,
355 .pool = tmp_pool,
356 );
1321e12a 357
20c6ea70
JMM
358 LOG_BUFFER_INIT(filter_state.buf);
359
360 ASSERT(!((*rte)->flags & REF_COW));
361 ASSERT(!rta_is_cached((*rte)->attrs));
1321e12a 362
20c6ea70 363 return interpret(&filter_state, expr, NULL);
1321e12a
OZ
364}
365
20c6ea70
JMM
366/*
367 * f_eval – get a value of a term
368 * @expr: filter line containing the term
369 * @tmp_pool: long data may get allocated from this pool
370 * @pres: here the output will be stored
371 */
7afa1438 372enum filter_return
4c553c5a 373f_eval(const struct f_line *expr, struct linpool *tmp_pool, struct f_val *pres)
1c20608e 374{
6479e403
JMM
375 FS_INIT(
376 .pool = tmp_pool,
377 );
0d1b3c4c 378
20c6ea70 379 LOG_BUFFER_INIT(filter_state.buf);
0e175f9f 380
20c6ea70 381 enum filter_return fret = interpret(&filter_state, expr, pres);
fc8df41e 382 return fret;
508d9360 383}
0d1b3c4c 384
20c6ea70
JMM
385/*
386 * f_eval_int – get an integer value of a term
387 * Called internally from the config parser, uses its internal memory pool
388 * for allocations. Do not call in other cases.
389 */
52e030e1 390uint
4c553c5a 391f_eval_int(const struct f_line *expr)
508d9360
OZ
392{
393 /* Called independently in parse-time to eval expressions */
6479e403
JMM
394 FS_INIT(
395 .pool = cfg_mem,
396 );
fc8df41e 397
4c553c5a
MM
398 struct f_val val;
399
20c6ea70 400 LOG_BUFFER_INIT(filter_state.buf);
fc8df41e 401
20c6ea70 402 if (interpret(&filter_state, expr, &val) > F_RETURN)
7afa1438 403 cf_error("Runtime error while evaluating expression");
0d1b3c4c 404
4c553c5a 405 if (val.type != T_INT)
b1c9d871 406 cf_error("Integer expression expected");
508d9360 407
4c553c5a 408 return val.val.i;
b1c9d871 409}
1c20608e 410
20c6ea70
JMM
411/*
412 * f_eval_buf – get a value of a term and print it to the supplied buffer
413 */
52893045
MM
414enum filter_return
415f_eval_buf(const struct f_line *expr, struct linpool *tmp_pool, buffer *buf)
416{
417 struct f_val val;
418 enum filter_return fret = f_eval(expr, tmp_pool, &val);
419 if (fret > F_RETURN)
420 val_format(&val, buf);
421 return fret;
422}
423
ff95080f
PM
424/**
425 * filter_same - compare two filters
426 * @new: first filter to be compared
0b39b1cb 427 * @old: second filter to be compared
ff95080f
PM
428 *
429 * Returns 1 in case filters are same, otherwise 0. If there are
430 * underlying bugs, it will rather say 0 on same filters than say
431 * 1 on different.
432 */
30a6108c 433int
0b39b1cb 434filter_same(const struct filter *new, const struct filter *old)
30a6108c 435{
81ce667b
MM
436 if (old == new) /* Handle FILTER_ACCEPT and FILTER_REJECT */
437 return 1;
438 if (old == FILTER_ACCEPT || old == FILTER_REJECT ||
439 new == FILTER_ACCEPT || new == FILTER_REJECT)
440 return 0;
f249d0b8
MM
441
442 if ((!old->sym) && (!new->sym))
443 return f_same(new->root, old->root);
444
445 if ((!old->sym) || (!new->sym))
446 return 0;
447
448 if (strcmp(old->sym->name, new->sym->name))
449 return 0;
450
451 return new->sym->flags & SYM_FLAG_SAME;
452}
453
454/**
455 * filter_commit - do filter comparisons on all the named functions and filters
456 */
457void
87c82334 458filter_commit(struct config *new, struct config *old)
f249d0b8
MM
459{
460 if (!old)
461 return;
462
463 struct symbol *sym, *osym;
464 WALK_LIST(sym, new->symbols)
465 switch (sym->class) {
466 case SYM_FUNCTION:
467 if ((osym = cf_find_symbol(old, sym->name)) &&
468 (osym->class == SYM_FUNCTION) &&
469 f_same(sym->function, osym->function))
470 sym->flags |= SYM_FLAG_SAME;
471 else
472 sym->flags &= ~SYM_FLAG_SAME;
473 break;
474
475 case SYM_FILTER:
476 if ((osym = cf_find_symbol(old, sym->name)) &&
477 (osym->class == SYM_FILTER) &&
478 f_same(sym->filter->root, osym->filter->root))
479 sym->flags |= SYM_FLAG_SAME;
480 else
481 sym->flags &= ~SYM_FLAG_SAME;
482 break;
483 }
30a6108c 484}
84ac62d3
MM
485
486void filters_dump_all(void)
487{
488 struct symbol *sym;
489 WALK_LIST(sym, config->symbols) {
490 switch (sym->class) {
491 case SYM_FILTER:
492 debug("Named filter %s:\n", sym->name);
493 f_dump_line(sym->filter->root, 1);
494 break;
495 case SYM_FUNCTION:
496 debug("Function %s:\n", sym->name);
497 f_dump_line(sym->function, 1);
498 break;
499 case SYM_PROTO:
500 {
501 debug("Protocol %s:\n", sym->name);
502 struct channel *c;
503 WALK_LIST(c, sym->proto->proto->channels) {
504 debug(" Channel %s (%s) IMPORT", c->name, net_label[c->net_type]);
505 if (c->in_filter == FILTER_ACCEPT)
506 debug(" ALL\n");
507 else if (c->in_filter == FILTER_REJECT)
508 debug(" NONE\n");
509 else if (c->in_filter == FILTER_UNDEF)
510 debug(" UNDEF\n");
511 else if (c->in_filter->sym) {
512 ASSERT(c->in_filter->sym->filter == c->in_filter);
513 debug(" named filter %s\n", c->in_filter->sym->name);
514 } else {
515 debug("\n");
516 f_dump_line(c->in_filter->root, 2);
517 }
518 }
519 }
520 }
521 }
522}