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