]> git.ipfire.org Git - thirdparty/bird.git/blame - filter/filter.c
Fixes header file name.
[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
21 * arguments (@a1, @a2). Some instructions contain pointer(s) to other
22 * instructions in their (@a1, @a2) 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"
23b1539b
PM
42#include "nest/route.h"
43#include "nest/protocol.h"
44#include "nest/iface.h"
159fa4ce 45#include "nest/attrs.h"
23b1539b
PM
46#include "conf/conf.h"
47#include "filter/filter.h"
48
2d496d20
PM
49#define P(a,b) ((a<<8) | b)
50
38506f71
PM
51#define CMP_ERROR 999
52
9831e591 53static struct adata *
42a0c054 54adata_empty(struct linpool *pool, int l)
ad9074e9 55{
42a0c054
OZ
56 struct adata *res = lp_alloc(pool, sizeof(struct adata) + l);
57 res->length = l;
ad9074e9
PM
58 return res;
59}
60
b0c9c21c 61static int
2e5a8735
PM
62pm_path_compare(struct f_path_mask *m1, struct f_path_mask *m2)
63{
64 while (1) {
65 if ((!m1) || (!m2))
66 return !((!m1) && (!m2));
43de796b 67
126683fe 68 /* FIXME: buggy, should return -1, 0, 1; but it doesn't matter */
43de796b 69 if ((m1->kind != m2->kind) || (m1->val != m2->val)) return 1;
2e5a8735
PM
70 m1 = m1->next;
71 m2 = m2->next;
72 }
73}
74
92a72a4c
OZ
75u32 f_eval_asn(struct f_inst *expr);
76
11cb6202
OZ
77static void
78pm_format(struct f_path_mask *p, byte *buf, unsigned int size)
79{
80 byte *end = buf + size - 16;
81
82 while (p)
83 {
84 if (buf > end)
85 {
86 strcpy(buf, " ...");
87 return;
88 }
89
92a72a4c
OZ
90 switch(p->kind)
91 {
92 case PM_ASN:
93 buf += bsprintf(buf, " %u", p->val);
94 break;
95
96 case PM_QUESTION:
97 buf += bsprintf(buf, " ?");
98 break;
99
100 case PM_ASTERISK:
101 buf += bsprintf(buf, " *");
102 break;
103
104 case PM_ASN_EXPR:
105 buf += bsprintf(buf, " %u", f_eval_asn((struct f_inst *) p->val));
106 break;
107 }
11cb6202
OZ
108
109 p = p->next;
110 }
111
112 *buf = 0;
113}
114
126683fe
OZ
115static inline int int_cmp(int i1, int i2)
116{
117 if (i1 == i2) return 0;
118 if (i1 < i2) return -1;
119 else return 1;
120}
121
2dec1e34
OZ
122static inline int uint_cmp(unsigned int i1, unsigned int i2)
123{
124 if (i1 == i2) return 0;
125 if (i1 < i2) return -1;
126 else return 1;
127}
128
42a0c054
OZ
129static inline int u64_cmp(u64 i1, u64 i2)
130{
131 if (i1 == i2) return 0;
132 if (i1 < i2) return -1;
133 else return 1;
134}
135
8dcf2544 136/**
3e82b32d 137 * val_compare - compare two values
8dcf2544
PM
138 * @v1: first value
139 * @v2: second value
140 *
141 * Compares two values and returns -1, 0, 1 on <, =, > or 999 on error.
142 * Tree module relies on this giving consistent results so that it can
143 * build balanced trees.
b093c328 144 */
38506f71
PM
145int
146val_compare(struct f_val v1, struct f_val v2)
147{
d85e1f0e
MM
148 int rc;
149
41be4444
PM
150 if ((v1.type == T_VOID) && (v2.type == T_VOID))
151 return 0;
152 if (v1.type == T_VOID) /* Hack for else */
153 return -1;
154 if (v2.type == T_VOID)
155 return 1;
156
f71bded6 157 if (v1.type != v2.type) {
126683fe
OZ
158#ifndef IPV6
159 /* IP->Quad implicit conversion */
160 if ((v1.type == T_QUAD) && (v2.type == T_IP))
2dec1e34 161 return uint_cmp(v1.val.i, ipa_to_u32(v2.val.px.ip));
126683fe 162 if ((v1.type == T_IP) && (v2.type == T_QUAD))
2dec1e34 163 return uint_cmp(ipa_to_u32(v1.val.px.ip), v2.val.i);
126683fe
OZ
164#endif
165
f71bded6 166 debug( "Types do not match in val_compare\n" );
7db7b7db 167 return CMP_ERROR;
f71bded6 168 }
38506f71 169 switch (v1.type) {
f4536657 170 case T_ENUM:
a6c9f064
OF
171 case T_INT:
172 case T_BOOL:
2dec1e34 173 return int_cmp(v1.val.i, v2.val.i);
d3dd620b 174 case T_PAIR:
126683fe 175 case T_QUAD:
2dec1e34 176 return uint_cmp(v1.val.i, v2.val.i);
42a0c054
OZ
177 case T_EC:
178 return u64_cmp(v1.val.ec, v2.val.ec);
43fc099b 179 case T_IP:
6dc7a0cb 180 return ipa_compare(v1.val.px.ip, v2.val.px.ip);
d85e1f0e
MM
181 case T_PREFIX:
182 if (rc = ipa_compare(v1.val.px.ip, v2.val.px.ip))
183 return rc;
184 if (v1.val.px.len < v2.val.px.len)
185 return -1;
186 if (v1.val.px.len > v2.val.px.len)
187 return 1;
188 return 0;
2e5a8735
PM
189 case T_PATH_MASK:
190 return pm_path_compare(v1.val.path_mask, v2.val.path_mask);
e29fa06e
OZ
191 case T_STRING:
192 return strcmp(v1.val.s, v2.val.s);
3076b5ae 193 default:
d59405ec 194 debug( "Compare of unknown entities: %x\n", v1.type );
3076b5ae 195 return CMP_ERROR;
38506f71
PM
196 }
197}
198
dfd48621
OZ
199int
200tree_compare(const void *p1, const void *p2)
201{
202 return val_compare((* (struct f_tree **) p1)->from, (* (struct f_tree **) p2)->from);
203}
b1a597e0
OZ
204
205void
7f0d245a 206fprefix_get_bounds(struct f_prefix *px, int *l, int *h)
b1a597e0
OZ
207{
208 *l = *h = px->len & LEN_MASK;
209
210 if (px->len & LEN_MINUS)
211 *l = 0;
212
213 else if (px->len & LEN_PLUS)
214 *h = MAX_PREFIX_LENGTH;
215
216 else if (px->len & LEN_RANGE)
217 {
218 *l = 0xff & (px->len >> 16);
219 *h = 0xff & (px->len >> 8);
220 }
221}
222
4c5f93d7 223/*
b093c328
PM
224 * val_simple_in_range - check if @v1 ~ @v2 for everything except sets
225 */
9831e591 226static int
6dc7a0cb
PM
227val_simple_in_range(struct f_val v1, struct f_val v2)
228{
10a53608 229 if ((v1.type == T_PATH) && (v2.type == T_PATH_MASK))
2a40efa5 230 return as_path_match(v1.val.ad, v2.val.path_mask);
0888a737
OZ
231 if ((v1.type == T_INT) && (v2.type == T_PATH))
232 return as_path_is_member(v2.val.ad, v1.val.i);
233
126683fe 234 if (((v1.type == T_PAIR) || (v1.type == T_QUAD)) && (v2.type == T_CLIST))
2bd2de01 235 return int_set_contains(v2.val.ad, v1.val.i);
126683fe
OZ
236#ifndef IPV6
237 /* IP->Quad implicit conversion */
238 if ((v1.type == T_IP) && (v2.type == T_CLIST))
239 return int_set_contains(v2.val.ad, ipa_to_u32(v1.val.px.ip));
240#endif
42a0c054
OZ
241 if ((v1.type == T_EC) && (v2.type == T_ECLIST))
242 return ec_set_contains(v2.val.ad, v1.val.ec);
243
e29fa06e
OZ
244 if ((v1.type == T_STRING) && (v2.type == T_STRING))
245 return patmatch(v2.val.s, v1.val.s);
10a53608 246
6dc7a0cb 247 if ((v1.type == T_IP) && (v2.type == T_PREFIX))
23e563d8 248 return ipa_in_net(v1.val.px.ip, v2.val.px.ip, v2.val.px.len);
6dc7a0cb 249
23e563d8 250 if ((v1.type == T_PREFIX) && (v2.type == T_PREFIX))
af582c48 251 return net_in_net(v1.val.px.ip, v1.val.px.len, v2.val.px.ip, v2.val.px.len);
b1a597e0 252
6dc7a0cb
PM
253 return CMP_ERROR;
254}
255
ba5c0057
OZ
256static int
257clist_set_type(struct f_tree *set, struct f_val *v)
258{
259 switch (set->from.type) {
260 case T_PAIR:
261 v->type = T_PAIR;
262 return 1;
263 case T_QUAD:
264#ifndef IPV6
265 case T_IP:
266#endif
267 v->type = T_QUAD;
268 return 1;
269 break;
270 default:
271 v->type = T_VOID;
272 return 0;
273 }
274}
275
42a0c054
OZ
276static inline int
277eclist_set_type(struct f_tree *set)
278{ return set->from.type == T_EC; }
279
ba5c0057
OZ
280static int
281clist_match_set(struct adata *clist, struct f_tree *set)
282{
283 if (!clist)
284 return 0;
285
286 struct f_val v;
287 if (!clist_set_type(set, &v))
288 return CMP_ERROR;
289
290 u32 *l = (u32 *) clist->data;
291 u32 *end = l + clist->length/4;
42a0c054 292
ba5c0057
OZ
293 while (l < end) {
294 v.val.i = *l++;
295 if (find_tree(set, v))
296 return 1;
297 }
298 return 0;
299}
300
42a0c054
OZ
301static int
302eclist_match_set(struct adata *list, struct f_tree *set)
303{
304 if (!list)
305 return 0;
306
307 if (!eclist_set_type(set))
308 return CMP_ERROR;
309
310 struct f_val v;
311 u32 *l = int_set_get_data(list);
312 int len = int_set_get_size(list);
313 int i;
314
315 v.type = T_EC;
316 for (i = 0; i < len; i += 2) {
317 v.val.ec = ec_get(l, i);
318 if (find_tree(set, v))
319 return 1;
320 }
321
322 return 0;
323}
324
ba5c0057 325static struct adata *
0888a737 326clist_filter(struct linpool *pool, struct adata *list, struct f_val set, int pos)
ba5c0057 327{
0888a737 328 if (!list)
ba5c0057
OZ
329 return NULL;
330
0888a737 331 int tree = (set.type == T_SET); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
ba5c0057 332 struct f_val v;
0888a737
OZ
333 if (tree)
334 clist_set_type(set.val.t, &v);
335 else
336 v.type = T_PAIR;
ba5c0057 337
0888a737
OZ
338 int len = int_set_get_size(list);
339 u32 *l = int_set_get_data(list);
340 u32 tmp[len];
ba5c0057 341 u32 *k = tmp;
0888a737 342 u32 *end = l + len;
ba5c0057
OZ
343
344 while (l < end) {
345 v.val.i = *l++;
0888a737
OZ
346 /* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
347 if ((tree ? !!find_tree(set.val.t, v) : int_set_contains(set.val.ad, v.val.i)) == pos)
ba5c0057
OZ
348 *k++ = v.val.i;
349 }
350
351 int nl = (k - tmp) * 4;
0888a737
OZ
352 if (nl == list->length)
353 return list;
ba5c0057 354
42a0c054
OZ
355 struct adata *res = adata_empty(pool, nl);
356 memcpy(res->data, tmp, nl);
357 return res;
358}
359
360static struct adata *
0888a737 361eclist_filter(struct linpool *pool, struct adata *list, struct f_val set, int pos)
42a0c054
OZ
362{
363 if (!list)
364 return NULL;
365
0888a737 366 int tree = (set.type == T_SET); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
42a0c054
OZ
367 struct f_val v;
368
369 int len = int_set_get_size(list);
370 u32 *l = int_set_get_data(list);
371 u32 tmp[len];
372 u32 *k = tmp;
373 int i;
374
375 v.type = T_EC;
376 for (i = 0; i < len; i += 2) {
377 v.val.ec = ec_get(l, i);
0888a737
OZ
378 /* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
379 if ((tree ? !!find_tree(set.val.t, v) : ec_set_contains(set.val.ad, v.val.ec)) == pos) {
42a0c054
OZ
380 *k++ = l[i];
381 *k++ = l[i+1];
382 }
383 }
384
385 int nl = (k - tmp) * 4;
386 if (nl == list->length)
387 return list;
388
389 struct adata *res = adata_empty(pool, nl);
ba5c0057
OZ
390 memcpy(res->data, tmp, nl);
391 return res;
392}
393
8dcf2544 394/**
3e82b32d 395 * val_in_range - implement |~| operator
8dcf2544
PM
396 * @v1: element
397 * @v2: set
398 *
399 * Checks if @v1 is element (|~| operator) of @v2. Sets are internally represented as balanced trees, see
3e82b32d 400 * |tree.c| module (this is not limited to sets, but for non-set cases, val_simple_in_range() is called early).
b093c328 401 */
9831e591 402static int
7db7b7db
PM
403val_in_range(struct f_val v1, struct f_val v2)
404{
6dc7a0cb
PM
405 int res;
406
407 res = val_simple_in_range(v1, v2);
408
409 if (res != CMP_ERROR)
410 return res;
1895e81e 411
b1a597e0 412 if ((v1.type == T_PREFIX) && (v2.type == T_PREFIX_SET))
7f0d245a 413 return trie_match_fprefix(v2.val.ti, &v1.val.px);
b1a597e0 414
ba5c0057
OZ
415 if ((v1.type == T_CLIST) && (v2.type == T_SET))
416 return clist_match_set(v1.val.ad, v2.val.t);
417
42a0c054
OZ
418 if ((v1.type == T_ECLIST) && (v2.type == T_SET))
419 return eclist_match_set(v1.val.ad, v2.val.t);
420
cc31b75a
OZ
421 if ((v1.type == T_PATH) && (v2.type == T_SET))
422 return as_path_match_set(v1.val.ad, v2.val.t);
423
1895e81e
PM
424 if (v2.type == T_SET)
425 switch (v1.type) {
426 case T_ENUM:
427 case T_INT:
126683fe
OZ
428 case T_PAIR:
429 case T_QUAD:
1895e81e 430 case T_IP:
42a0c054 431 case T_EC:
1895e81e
PM
432 {
433 struct f_tree *n;
434 n = find_tree(v2.val.t, v1);
435 if (!n)
436 return 0;
437 return !! (val_simple_in_range(v1, n->from)); /* We turn CMP_ERROR into compared ok, and that's fine */
438 }
439 }
7db7b7db
PM
440 return CMP_ERROR;
441}
442
0d1b3c4c
OZ
443static void val_print(struct f_val v);
444
38506f71 445static void
0d1b3c4c 446tree_node_print(struct f_tree *t, char **sep)
38506f71 447{
0d1b3c4c 448 if (t == NULL)
38506f71 449 return;
0d1b3c4c
OZ
450
451 tree_node_print(t->left, sep);
452
453 logn(*sep);
454 val_print(t->from);
455 if (val_compare(t->from, t->to) != 0)
456 {
457 logn( ".." );
458 val_print(t->to);
459 }
460 *sep = ", ";
461
462 tree_node_print(t->right, sep);
463}
464
465static void
466tree_print(struct f_tree *t)
467{
468 char *sep = "";
469 logn( "[" );
470 tree_node_print(t, &sep);
471 logn( "] " );
38506f71
PM
472}
473
4c5f93d7 474/*
b093c328
PM
475 * val_print - format filter value
476 */
0d1b3c4c 477static void
38506f71
PM
478val_print(struct f_val v)
479{
ecd25633 480 char buf2[1024];
38506f71 481 switch (v.type) {
0d1b3c4c
OZ
482 case T_VOID: logn("(void)"); return;
483 case T_BOOL: logn(v.val.i ? "TRUE" : "FALSE"); return;
484 case T_INT: logn("%d", v.val.i); return;
485 case T_STRING: logn("%s", v.val.s); return;
486 case T_IP: logn("%I", v.val.px.ip); return;
487 case T_PREFIX: logn("%I/%d", v.val.px.ip, v.val.px.len); return;
488 case T_PAIR: logn("(%d,%d)", v.val.i >> 16, v.val.i & 0xffff); return;
489 case T_QUAD: logn("%R", v.val.i); return;
42a0c054 490 case T_EC: ec_format(buf2, v.val.ec); logn("%s", buf2); return;
0d1b3c4c
OZ
491 case T_PREFIX_SET: trie_print(v.val.ti); return;
492 case T_SET: tree_print(v.val.t); return;
493 case T_ENUM: logn("(enum %x)%d", v.type, v.val.i); return;
494 case T_PATH: as_path_format(v.val.ad, buf2, 1000); logn("(path %s)", buf2); return;
fdf16eb6 495 case T_CLIST: int_set_format(v.val.ad, 1, -1, buf2, 1000); logn("(clist %s)", buf2); return;
42a0c054 496 case T_ECLIST: ec_set_format(v.val.ad, -1, buf2, 1000); logn("(eclist %s)", buf2); return;
0d1b3c4c
OZ
497 case T_PATH_MASK: pm_format(v.val.path_mask, buf2, 1000); logn("(pathmask%s)", buf2); return;
498 default: logn( "[unknown type %x]", v.type ); return;
38506f71 499 }
38506f71
PM
500}
501
a03ede64
OZ
502static struct rte **f_rte;
503static struct rta *f_old_rta;
31e79264 504static struct ea_list **f_tmp_attrs;
a03ede64 505static struct linpool *f_pool;
0a06a9b8 506static int f_flags;
36bbfc70 507
a03ede64
OZ
508static inline void f_rte_cow(void)
509{
510 *f_rte = rte_cow(*f_rte);
511}
512
4c5f93d7 513/*
b093c328
PM
514 * rta_cow - prepare rta for modification by filter
515 */
9831e591 516static void
a03ede64 517f_rta_cow(void)
26c09e1d 518{
db96fccb 519 if ((*f_rte)->attrs->aflags & RTAF_CACHED) {
a03ede64
OZ
520
521 /* Prepare to modify rte */
522 f_rte_cow();
523
524 /* Store old rta to free it later */
525 f_old_rta = (*f_rte)->attrs;
526
527 /*
528 * Alloc new rta, do shallow copy and update rte. Fields eattrs
529 * and nexthops of rta are shared with f_old_rta (they will be
530 * copied when the cached rta will be obtained at the end of
531 * f_run()), also the lock of hostentry is inherited (we suppose
532 * hostentry is not changed by filters).
533 */
534 rta *ra = lp_alloc(f_pool, sizeof(rta));
535 memcpy(ra, f_old_rta, sizeof(rta));
536 ra->aflags = 0;
537 (*f_rte)->attrs = ra;
26c09e1d
PM
538 }
539}
540
cb530392
OZ
541static struct rate_limit rl_runtime_err;
542
9a4037d4 543#define runtime(x) do { \
cb530392 544 log_rl(&rl_runtime_err, L_ERR "filters, line %d: %s", what->lineno, x); \
9a4037d4
PM
545 res.type = T_RETURN; \
546 res.val.i = F_ERROR; \
547 return res; \
548 } while(0)
549
550#define ARG(x,y) \
551 x = interpret(what->y); \
2d496d20 552 if (x.type & T_RETURN) \
9a4037d4
PM
553 return x;
554
555#define ONEARG ARG(v1, a1.p)
556#define TWOARGS ARG(v1, a1.p) \
557 ARG(v2, a2.p)
558#define TWOARGS_C TWOARGS \
559 if (v1.type != v2.type) \
b178d92a 560 runtime( "Can't operate with values of incompatible types" );
7db7b7db 561
b093c328
PM
562/**
563 * interpret
2e9b2421 564 * @what: filter to interpret
b093c328 565 *
4c5f93d7 566 * Interpret given tree of filter instructions. This is core function
b093c328 567 * of filter system and does all the hard work.
771ae456
PM
568 *
569 * Each instruction has 4 fields: code (which is instruction code),
570 * aux (which is extension to instruction code, typically type),
571 * arg1 and arg2 - arguments. Depending on instruction, arguments
572 * are either integers, or pointers to instruction trees. Common
573 * instructions like +, that have two expressions as arguments use
574 * TWOARGS macro to get both of them evaluated.
575 *
576 * &f_val structures are copied around, so there are no problems with
577 * memory managment.
b093c328 578 */
23b1539b
PM
579static struct f_val
580interpret(struct f_inst *what)
581{
582 struct symbol *sym;
126683fe 583 struct f_val v1, v2, res, *vp;
92a72a4c 584 unsigned u1, u2;
6a57bb31 585 int i;
7ea5b00f 586 u32 as;
23b1539b
PM
587
588 res.type = T_VOID;
589 if (!what)
590 return res;
591
592 switch(what->code) {
593 case ',':
594 TWOARGS;
595 break;
596
597/* Binary operators */
598 case '+':
599 TWOARGS_C;
600 switch (res.type = v1.type) {
b178d92a 601 case T_VOID: runtime( "Can't operate with values of type void" );
23b1539b
PM
602 case T_INT: res.val.i = v1.val.i + v2.val.i; break;
603 default: runtime( "Usage of unknown type" );
604 }
605 break;
9f0d45d6
PM
606 case '-':
607 TWOARGS_C;
608 switch (res.type = v1.type) {
b178d92a 609 case T_VOID: runtime( "Can't operate with values of type void" );
9f0d45d6
PM
610 case T_INT: res.val.i = v1.val.i - v2.val.i; break;
611 default: runtime( "Usage of unknown type" );
612 }
613 break;
614 case '*':
615 TWOARGS_C;
616 switch (res.type = v1.type) {
b178d92a 617 case T_VOID: runtime( "Can't operate with values of type void" );
9f0d45d6
PM
618 case T_INT: res.val.i = v1.val.i * v2.val.i; break;
619 default: runtime( "Usage of unknown type" );
620 }
621 break;
23b1539b
PM
622 case '/':
623 TWOARGS_C;
624 switch (res.type = v1.type) {
b178d92a 625 case T_VOID: runtime( "Can't operate with values of type void" );
64ba9f7b
PM
626 case T_INT: if (v2.val.i == 0) runtime( "Mother told me not to divide by 0" );
627 res.val.i = v1.val.i / v2.val.i; break;
23b1539b
PM
628 default: runtime( "Usage of unknown type" );
629 }
630 break;
5f4aee76
PM
631
632 case '&':
5f4aee76 633 case '|':
0aa88530
OZ
634 ARG(v1, a1.p);
635 if (v1.type != T_BOOL)
636 runtime( "Can't do boolean operation on non-booleans" );
637 if (v1.val.i == (what->code == '|')) {
638 res.type = T_BOOL;
639 res.val.i = v1.val.i;
640 break;
641 }
642
643 ARG(v2, a2.p);
644 if (v2.type != T_BOOL)
645 runtime( "Can't do boolean operation on non-booleans" );
646 res.type = T_BOOL;
647 res.val.i = v2.val.i;
5f4aee76 648 break;
23b1539b 649
92a72a4c 650 case P('m','p'):
42a0c054 651 TWOARGS;
92a72a4c
OZ
652 if ((v1.type != T_INT) || (v2.type != T_INT))
653 runtime( "Can't operate with value of non-integer type in pair constructor" );
654 u1 = v1.val.i;
655 u2 = v2.val.i;
656 if ((u1 > 0xFFFF) || (u2 > 0xFFFF))
657 runtime( "Can't operate with value out of bounds in pair constructor" );
658 res.val.i = (u1 << 16) | u2;
659 res.type = T_PAIR;
660 break;
661
42a0c054
OZ
662 case P('m','c'):
663 {
664 TWOARGS;
665
666 int check, ipv4_used;
667 u32 key, val;
668
669 if (v1.type == T_INT) {
670 ipv4_used = 0; key = v1.val.i;
671 }
672 else if (v1.type == T_QUAD) {
673 ipv4_used = 1; key = v1.val.i;
674 }
675#ifndef IPV6
676 /* IP->Quad implicit conversion */
677 else if (v1.type == T_IP) {
678 ipv4_used = 1; key = ipa_to_u32(v1.val.px.ip);
679 }
680#endif
681 else
682 runtime("Can't operate with key of non-integer/IPv4 type in EC constructor");
683
684 if (v2.type != T_INT)
685 runtime("Can't operate with value of non-integer type in EC constructor");
686 val = v2.val.i;
687
688 res.type = T_EC;
689
690 if (what->aux == EC_GENERIC) {
691 check = 0; res.val.ec = ec_generic(key, val);
692 }
693 else if (ipv4_used) {
694 check = 1; res.val.ec = ec_ip4(what->aux, key, val);
695 }
696 else if (key < 0x10000) {
697 check = 0; res.val.ec = ec_as2(what->aux, key, val);
698 }
699 else {
700 check = 1; res.val.ec = ec_as4(what->aux, key, val);
701 }
702
703 if (check && (val > 0xFFFF))
704 runtime("Can't operate with value out of bounds in EC constructor");
705
706 break;
707 }
708
23b1539b 709/* Relational operators */
38506f71
PM
710
711#define COMPARE(x) \
126683fe 712 TWOARGS; \
38506f71
PM
713 i = val_compare(v1, v2); \
714 if (i==CMP_ERROR) \
126683fe
OZ
715 runtime( "Can't compare values of incompatible types" ); \
716 res.type = T_BOOL; \
38506f71 717 res.val.i = (x); \
23b1539b 718 break;
38506f71 719
2d496d20
PM
720 case P('!','='): COMPARE(i!=0);
721 case P('=','='): COMPARE(i==0);
38506f71 722 case '<': COMPARE(i==-1);
2d496d20 723 case P('<','='): COMPARE(i!=1);
38506f71 724
995e5894
PM
725 case '!':
726 ONEARG;
727 if (v1.type != T_BOOL)
b178d92a 728 runtime( "Not applied to non-boolean" );
995e5894
PM
729 res = v1;
730 res.val.i = !res.val.i;
731 break;
732
38506f71
PM
733 case '~':
734 TWOARGS;
23b1539b 735 res.type = T_BOOL;
7db7b7db
PM
736 res.val.i = val_in_range(v1, v2);
737 if (res.val.i == CMP_ERROR)
738 runtime( "~ applied on unknown type pair" );
0aa88530 739 res.val.i = !!res.val.i;
23b1539b 740 break;
2d496d20 741 case P('d','e'):
f4536657
PM
742 ONEARG;
743 res.type = T_BOOL;
744 res.val.i = (v1.type != T_VOID);
745 break;
23b1539b 746
d3dd620b 747 /* Set to indirect value, a1 = variable, a2 = value */
23b1539b 748 case 's':
2db3b288
PM
749 ARG(v2, a2.p);
750 sym = what->a1.p;
126683fe
OZ
751 vp = sym->def;
752 if ((sym->class != (SYM_VARIABLE | v2.type)) && (v2.type != T_VOID)) {
753#ifndef IPV6
754 /* IP->Quad implicit conversion */
755 if ((sym->class == (SYM_VARIABLE | T_QUAD)) && (v2.type == T_IP)) {
756 vp->type = T_QUAD;
757 vp->val.i = ipa_to_u32(v2.val.px.ip);
758 break;
759 }
760#endif
aa461248 761 runtime( "Assigning to variable of incompatible type" );
126683fe
OZ
762 }
763 *vp = v2;
23b1539b
PM
764 break;
765
083c43e2 766 /* some constants have value in a2, some in *a1.p, strange. */
b1a597e0 767 case 'c': /* integer (or simple type) constant, string, set, or prefix_set */
c7b43f33 768 res.type = what->aux;
083c43e2 769
b1a597e0
OZ
770 if (res.type == T_PREFIX_SET)
771 res.val.ti = what->a2.p;
772 else if (res.type == T_SET)
083c43e2
OZ
773 res.val.t = what->a2.p;
774 else if (res.type == T_STRING)
775 res.val.s = what->a2.p;
776 else
777 res.val.i = what->a2.i;
23b1539b 778 break;
9be1086d 779 case 'V':
38506f71
PM
780 case 'C':
781 res = * ((struct f_val *) what->a1.p);
782 break;
23b1539b
PM
783 case 'p':
784 ONEARG;
38506f71 785 val_print(v1);
23b1539b
PM
786 break;
787 case '?': /* ? has really strange error value, so we can implement if ... else nicely :-) */
788 ONEARG;
789 if (v1.type != T_BOOL)
98da26a0 790 runtime( "If requires boolean expression" );
23b1539b 791 if (v1.val.i) {
2db3b288 792 ARG(res,a2.p);
23b1539b
PM
793 res.val.i = 0;
794 } else res.val.i = 1;
795 res.type = T_BOOL;
796 break;
797 case '0':
3cf4a2e2 798 debug( "No operation\n" );
23b1539b 799 break;
2d496d20 800 case P('p',','):
23b1539b 801 ONEARG;
798df5b1 802 if (what->a2.i == F_NOP || (what->a2.i != F_NONL && what->a1.p))
0d1b3c4c 803 log_commit(*L_INFO);
23b1539b 804
2db3b288 805 switch (what->a2.i) {
23b1539b
PM
806 case F_QUITBIRD:
807 die( "Filter asked me to die" );
808 case F_ACCEPT:
809 /* Should take care about turning ACCEPT into MODIFY */
810 case F_ERROR:
2ad6dcdb 811 case F_REJECT: /* FIXME (noncritical) Should print complete route along with reason to reject route */
23b1539b 812 res.type = T_RETURN;
2ad6dcdb 813 res.val.i = what->a2.i;
7e1f9971 814 return res; /* We have to return now, no more processing. */
d3dd620b 815 case F_NONL:
23b1539b
PM
816 case F_NOP:
817 break;
818 default:
b178d92a 819 bug( "unknown return type: Can't happen");
23b1539b
PM
820 }
821 break;
36bbfc70
PM
822 case 'a': /* rta access */
823 {
824 struct rta *rta = (*f_rte)->attrs;
c7b43f33 825 res.type = what->aux;
36bbfc70
PM
826 switch(res.type) {
827 case T_IP:
6dc7a0cb 828 res.val.px.ip = * (ip_addr *) ((char *) rta + what->a2.i);
36bbfc70 829 break;
c7b43f33
PM
830 case T_ENUM:
831 res.val.i = * ((char *) rta + what->a2.i);
832 break;
e29fa06e
OZ
833 case T_STRING: /* Warning: this is a special case for proto attribute */
834 res.val.s = rta->proto->name;
835 break;
36bbfc70
PM
836 case T_PREFIX: /* Warning: this works only for prefix of network */
837 {
838 res.val.px.ip = (*f_rte)->net->n.prefix;
839 res.val.px.len = (*f_rte)->net->n.pxlen;
840 break;
841 }
842 default:
3076b5ae 843 bug( "Invalid type for rta access (%x)", res.type );
36bbfc70
PM
844 }
845 }
846 break;
0dc4431c
PM
847 case P('a','S'):
848 ONEARG;
849 if (what->aux != v1.type)
98da26a0 850 runtime( "Attempt to set static attribute to incompatible type" );
a03ede64 851 f_rta_cow();
0dc4431c
PM
852 {
853 struct rta *rta = (*f_rte)->attrs;
854 switch (what->aux) {
182a7895 855
0dc4431c
PM
856 case T_IP:
857 * (ip_addr *) ((char *) rta + what->a2.i) = v1.val.px.ip;
858 break;
182a7895
OZ
859
860 case T_ENUM_SCOPE:
861 rta->scope = v1.val.i;
862 break;
863
864 case T_ENUM_RTD:
865 i = v1.val.i;
866 if ((i != RTD_BLACKHOLE) && (i != RTD_UNREACHABLE) && (i != RTD_PROHIBIT))
867 runtime( "Destination can be changed only to blackhole, unreachable or prohibit" );
868 rta->dest = i;
869 rta->gw = IPA_NONE;
870 rta->iface = NULL;
871 rta->nexthops = NULL;
872 break;
873
0dc4431c
PM
874 default:
875 bug( "Unknown type in set of static attribute" );
876 }
877 }
878 break;
2d496d20 879 case P('e','a'): /* Access to extended attributes */
91447965 880 {
0a06a9b8 881 eattr *e = NULL;
3076b5ae 882 if (!(f_flags & FF_FORCE_TMPATTR))
0a06a9b8 883 e = ea_find( (*f_rte)->attrs->eattrs, what->a2.i );
31e79264
PM
884 if (!e)
885 e = ea_find( (*f_tmp_attrs), what->a2.i );
3076b5ae 886 if ((!e) && (f_flags & FF_FORCE_TMPATTR))
0a06a9b8 887 e = ea_find( (*f_rte)->attrs->eattrs, what->a2.i );
e8da1bd0
OZ
888
889 if (!e) {
0277cc0b
OZ
890 /* A special case: undefined int_set looks like empty int_set */
891 if ((what->aux & EAF_TYPE_MASK) == EAF_TYPE_INT_SET) {
892 res.type = T_CLIST;
42a0c054
OZ
893 res.val.ad = adata_empty(f_pool, 0);
894 break;
895 }
896 /* The same special case for ec_set */
897 else if ((what->aux & EAF_TYPE_MASK) == EAF_TYPE_EC_SET) {
898 res.type = T_ECLIST;
899 res.val.ad = adata_empty(f_pool, 0);
0277cc0b
OZ
900 break;
901 }
42a0c054 902
e8da1bd0
OZ
903 /* Undefined value */
904 res.type = T_VOID;
905 break;
906 }
907
908 switch (what->aux & EAF_TYPE_MASK) {
909 case EAF_TYPE_INT:
0150e521 910 res.type = T_INT;
91447965
PM
911 res.val.i = e->u.data;
912 break;
126683fe
OZ
913 case EAF_TYPE_ROUTER_ID:
914 res.type = T_QUAD;
915 res.val.i = e->u.data;
916 break;
e8da1bd0
OZ
917 case EAF_TYPE_OPAQUE:
918 res.type = T_ENUM_EMPTY;
919 res.val.i = 0;
920 break;
330aecea 921 case EAF_TYPE_IP_ADDRESS:
330aecea
OZ
922 res.type = T_IP;
923 struct adata * ad = e->u.ptr;
924 res.val.px.ip = * (ip_addr *) ad->data;
925 break;
0150e521
PM
926 case EAF_TYPE_AS_PATH:
927 res.type = T_PATH;
928 res.val.ad = e->u.ptr;
929 break;
930 case EAF_TYPE_INT_SET:
931 res.type = T_CLIST;
10a53608
PM
932 res.val.ad = e->u.ptr;
933 break;
42a0c054
OZ
934 case EAF_TYPE_EC_SET:
935 res.type = T_ECLIST;
936 res.val.ad = e->u.ptr;
937 break;
e8da1bd0
OZ
938 case EAF_TYPE_UNDEF:
939 res.type = T_VOID;
940 break;
2803c9dd 941 default:
ad9074e9 942 bug("Unknown type in e,a");
91447965
PM
943 }
944 }
6dc7a0cb 945 break;
2d496d20 946 case P('e','S'):
f31156ca 947 ONEARG;
f31156ca
PM
948 {
949 struct ea_list *l = lp_alloc(f_pool, sizeof(struct ea_list) + sizeof(eattr));
950
951 l->next = NULL;
952 l->flags = EALF_SORTED;
953 l->count = 1;
954 l->attrs[0].id = what->a2.i;
913ce95b
PM
955 l->attrs[0].flags = 0;
956 l->attrs[0].type = what->aux | EAF_ORIGINATED;
31e79264
PM
957 switch (what->aux & EAF_TYPE_MASK) {
958 case EAF_TYPE_INT:
959 if (v1.type != T_INT)
960 runtime( "Setting int attribute to non-int value" );
f31156ca
PM
961 l->attrs[0].u.data = v1.val.i;
962 break;
3e40f3e7
OZ
963
964 case EAF_TYPE_ROUTER_ID:
965#ifndef IPV6
966 /* IP->Quad implicit conversion */
967 if (v1.type == T_IP) {
968 l->attrs[0].u.data = ipa_to_u32(v1.val.px.ip);
969 break;
970 }
971#endif
972 /* T_INT for backward compatibility */
973 if ((v1.type != T_QUAD) && (v1.type != T_INT))
974 runtime( "Setting quad attribute to non-quad value" );
975 l->attrs[0].u.data = v1.val.i;
976 break;
977
e8da1bd0
OZ
978 case EAF_TYPE_OPAQUE:
979 runtime( "Setting opaque attribute is not allowed" );
980 break;
330aecea
OZ
981 case EAF_TYPE_IP_ADDRESS:
982 if (v1.type != T_IP)
983 runtime( "Setting ip attribute to non-ip value" );
984 int len = sizeof(ip_addr);
985 struct adata *ad = lp_alloc(f_pool, sizeof(struct adata) + len);
986 ad->length = len;
987 (* (ip_addr *) ad->data) = v1.val.px.ip;
54fe0d92 988 l->attrs[0].u.ptr = ad;
330aecea 989 break;
10a53608
PM
990 case EAF_TYPE_AS_PATH:
991 if (v1.type != T_PATH)
992 runtime( "Setting path attribute to non-path value" );
993 l->attrs[0].u.ptr = v1.val.ad;
994 break;
708711c3
PM
995 case EAF_TYPE_INT_SET:
996 if (v1.type != T_CLIST)
42a0c054
OZ
997 runtime( "Setting clist attribute to non-clist value" );
998 l->attrs[0].u.ptr = v1.val.ad;
999 break;
1000 case EAF_TYPE_EC_SET:
1001 if (v1.type != T_ECLIST)
1002 runtime( "Setting eclist attribute to non-eclist value" );
708711c3
PM
1003 l->attrs[0].u.ptr = v1.val.ad;
1004 break;
31e79264
PM
1005 case EAF_TYPE_UNDEF:
1006 if (v1.type != T_VOID)
1007 runtime( "Setting void attribute to non-void value" );
48f9e019
PM
1008 l->attrs[0].u.data = 0;
1009 break;
0150e521 1010 default: bug("Unknown type in e,S");
f31156ca 1011 }
31e79264 1012
3076b5ae 1013 if (!(what->aux & EAF_TEMP) && (!(f_flags & FF_FORCE_TMPATTR))) {
a03ede64 1014 f_rta_cow();
db96fccb
OZ
1015 l->next = (*f_rte)->attrs->eattrs;
1016 (*f_rte)->attrs->eattrs = l;
31e79264
PM
1017 } else {
1018 l->next = (*f_tmp_attrs);
1019 (*f_tmp_attrs) = l;
1020 }
f31156ca 1021 }
f31156ca 1022 break;
0dc4431c
PM
1023 case 'P':
1024 res.type = T_INT;
1025 res.val.i = (*f_rte)->pref;
1026 break;
1027 case P('P','S'):
1028 ONEARG;
1029 if (v1.type != T_INT)
b178d92a 1030 runtime( "Can't set preference to non-integer" );
f4c6ca8c
OZ
1031 if ((v1.val.i < 0) || (v1.val.i > 0xFFFF))
1032 runtime( "Setting preference value out of bounds" );
a03ede64 1033 f_rte_cow();
0dc4431c
PM
1034 (*f_rte)->pref = v1.val.i;
1035 break;
684c6f5a
PM
1036 case 'L': /* Get length of */
1037 ONEARG;
1038 res.type = T_INT;
1039 switch(v1.type) {
1040 case T_PREFIX: res.val.i = v1.val.px.len; break;
1041 case T_PATH: res.val.i = as_path_getlen(v1.val.ad); break;
82ba9032 1042 default: runtime( "Prefix or path expected" );
684c6f5a
PM
1043 }
1044 break;
2d496d20 1045 case P('c','p'): /* Convert prefix to ... */
36bbfc70
PM
1046 ONEARG;
1047 if (v1.type != T_PREFIX)
b178d92a 1048 runtime( "Prefix expected" );
c7b43f33 1049 res.type = what->aux;
36bbfc70 1050 switch(res.type) {
684c6f5a 1051 /* case T_INT: res.val.i = v1.val.px.len; break; Not needed any more */
6dc7a0cb 1052 case T_IP: res.val.px.ip = v1.val.px.ip; break;
3076b5ae 1053 default: bug( "Unknown prefix to conversion" );
36bbfc70
PM
1054 }
1055 break;
7ea5b00f
OZ
1056 case P('a','f'): /* Get first ASN from AS PATH */
1057 ONEARG;
1058 if (v1.type != T_PATH)
2eece54a 1059 runtime( "AS path expected" );
7ea5b00f
OZ
1060
1061 as = 0;
52b9b2a1 1062 as_path_get_first(v1.val.ad, &as);
7ea5b00f
OZ
1063 res.type = T_INT;
1064 res.val.i = as;
1065 break;
1066 case P('a','l'): /* Get last ASN from AS PATH */
1067 ONEARG;
1068 if (v1.type != T_PATH)
1069 runtime( "AS path expected" );
1070
1071 as = 0;
52b9b2a1 1072 as_path_get_last(v1.val.ad, &as);
7ea5b00f
OZ
1073 res.type = T_INT;
1074 res.val.i = as;
1075 break;
2d496d20
PM
1076 case 'r':
1077 ONEARG;
1078 res = v1;
1079 res.type |= T_RETURN;
44711e0c 1080 return res;
2d496d20 1081 case P('c','a'): /* CALL: this is special: if T_RETURN and returning some value, mask it out */
6542ece9
PM
1082 ONEARG;
1083 res = interpret(what->a2.p);
2d496d20
PM
1084 if (res.type == T_RETURN)
1085 return res;
1086 res.type &= ~T_RETURN;
6542ece9 1087 break;
aa461248
OZ
1088 case P('c','v'): /* Clear local variables */
1089 for (sym = what->a1.p; sym != NULL; sym = sym->aux2)
1090 ((struct f_val *) sym->def)->type = T_VOID;
1091 break;
2d496d20 1092 case P('S','W'):
7db7b7db 1093 ONEARG;
41be4444
PM
1094 {
1095 struct f_tree *t = find_tree(what->a2.p, v1);
1096 if (!t) {
1097 v1.type = T_VOID;
1098 t = find_tree(what->a2.p, v1);
1099 if (!t) {
ad9074e9 1100 debug( "No else statement?\n");
41be4444
PM
1101 break;
1102 }
1103 }
1877dab2 1104 /* It is actually possible to have t->data NULL */
44711e0c
OZ
1105
1106 res = interpret(t->data);
1107 if (res.type & T_RETURN)
1108 return res;
41be4444 1109 }
7db7b7db 1110 break;
2d496d20 1111 case P('i','M'): /* IP.MASK(val) */
f4536657
PM
1112 TWOARGS;
1113 if (v2.type != T_INT)
b178d92a 1114 runtime( "Integer expected");
f4536657 1115 if (v1.type != T_IP)
b178d92a 1116 runtime( "You can mask only IP addresses" );
f4536657
PM
1117 {
1118 ip_addr mask = ipa_mkmask(v2.val.i);
1119 res.type = T_IP;
1120 res.val.px.ip = ipa_and(mask, v1.val.px.ip);
1121 }
d3dd620b 1122 break;
afc54517
PM
1123
1124 case 'E': /* Create empty attribute */
1125 res.type = what->aux;
42a0c054 1126 res.val.ad = adata_empty(f_pool, 0);
afc54517
PM
1127 break;
1128 case P('A','p'): /* Path prepend */
1129 TWOARGS;
1130 if (v1.type != T_PATH)
1131 runtime("Can't prepend to non-path");
1132 if (v2.type != T_INT)
1133 runtime("Can't prepend non-integer");
1134
1135 res.type = T_PATH;
1136 res.val.ad = as_path_prepend(f_pool, v1.val.ad, v2.val.i);
1137 break;
1138
42a0c054 1139 case P('C','a'): /* (Extended) Community list add or delete */
9c400ec9 1140 TWOARGS;
42a0c054
OZ
1141 if (v1.type == T_CLIST)
1142 {
1143 /* Community (or cluster) list */
1144 struct f_val dummy;
1145 int arg_set = 0;
1146 i = 0;
a58022a6 1147
42a0c054
OZ
1148 if ((v2.type == T_PAIR) || (v2.type == T_QUAD))
1149 i = v2.val.i;
126683fe 1150#ifndef IPV6
42a0c054
OZ
1151 /* IP->Quad implicit conversion */
1152 else if (v2.type == T_IP)
1153 i = ipa_to_u32(v2.val.px.ip);
126683fe 1154#endif
42a0c054
OZ
1155 else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy))
1156 arg_set = 1;
0888a737
OZ
1157 else if (v2.type == T_CLIST)
1158 arg_set = 2;
42a0c054
OZ
1159 else
1160 runtime("Can't add/delete non-pair");
1161
1162 res.type = T_CLIST;
1163 switch (what->aux)
1164 {
1165 case 'a':
0888a737 1166 if (arg_set == 1)
42a0c054 1167 runtime("Can't add set");
0888a737
OZ
1168 else if (!arg_set)
1169 res.val.ad = int_set_add(f_pool, v1.val.ad, i);
1170 else
1171 res.val.ad = int_set_union(f_pool, v1.val.ad, v2.val.ad);
42a0c054
OZ
1172 break;
1173
1174 case 'd':
1175 if (!arg_set)
1176 res.val.ad = int_set_del(f_pool, v1.val.ad, i);
1177 else
0888a737 1178 res.val.ad = clist_filter(f_pool, v1.val.ad, v2, 0);
42a0c054 1179 break;
9c400ec9 1180
42a0c054
OZ
1181 case 'f':
1182 if (!arg_set)
1183 runtime("Can't filter pair");
0888a737 1184 res.val.ad = clist_filter(f_pool, v1.val.ad, v2, 1);
42a0c054
OZ
1185 break;
1186
1187 default:
1188 bug("unknown Ca operation");
1189 }
1190 }
1191 else if (v1.type == T_ECLIST)
e08d2ff0 1192 {
42a0c054
OZ
1193 /* Extended community list */
1194 int arg_set = 0;
e08d2ff0 1195
42a0c054
OZ
1196 /* v2.val is either EC or EC-set */
1197 if ((v2.type == T_SET) && eclist_set_type(v2.val.t))
1198 arg_set = 1;
0888a737
OZ
1199 else if (v2.type == T_ECLIST)
1200 arg_set = 2;
42a0c054
OZ
1201 else if (v2.type != T_EC)
1202 runtime("Can't add/delete non-pair");
1203
1204 res.type = T_ECLIST;
1205 switch (what->aux)
1206 {
1207 case 'a':
0888a737 1208 if (arg_set == 1)
42a0c054 1209 runtime("Can't add set");
0888a737
OZ
1210 else if (!arg_set)
1211 res.val.ad = ec_set_add(f_pool, v1.val.ad, v2.val.ec);
1212 else
1213 res.val.ad = ec_set_union(f_pool, v1.val.ad, v2.val.ad);
42a0c054
OZ
1214 break;
1215
1216 case 'd':
1217 if (!arg_set)
1218 res.val.ad = ec_set_del(f_pool, v1.val.ad, v2.val.ec);
1219 else
0888a737 1220 res.val.ad = eclist_filter(f_pool, v1.val.ad, v2, 0);
42a0c054 1221 break;
e08d2ff0 1222
42a0c054
OZ
1223 case 'f':
1224 if (!arg_set)
1225 runtime("Can't filter ec");
0888a737 1226 res.val.ad = eclist_filter(f_pool, v1.val.ad, v2, 1);
42a0c054 1227 break;
e08d2ff0 1228
42a0c054
OZ
1229 default:
1230 bug("unknown Ca operation");
1231 }
9c400ec9 1232 }
42a0c054
OZ
1233 else
1234 runtime("Can't add/delete to non-(e)clist");
1235
9c400ec9
PM
1236 break;
1237
af582c48
OZ
1238 case P('R','C'): /* ROA Check */
1239 if (what->arg1)
1240 {
1241 TWOARGS;
1242 if ((v1.type != T_PREFIX) || (v2.type != T_INT))
1243 runtime("Invalid argument to roa_check()");
1244
1245 as = v2.val.i;
1246 }
1247 else
1248 {
1249 v1.val.px.ip = (*f_rte)->net->n.prefix;
1250 v1.val.px.len = (*f_rte)->net->n.pxlen;
1251
1252 /* We ignore temporary attributes, probably not a problem here */
1253 /* 0x02 is a value of BA_AS_PATH, we don't want to include BGP headers */
1254 eattr *e = ea_find((*f_rte)->attrs->eattrs, EA_CODE(EAP_BGP, 0x02));
1255
1256 if (!e || e->type != EAF_TYPE_AS_PATH)
1257 runtime("Missing AS_PATH attribute");
1258
1259 as_path_get_last(e->u.ptr, &as);
1260 }
1261
1262 struct roa_table_config *rtc = ((struct f_inst_roa_check *) what)->rtc;
1263 if (!rtc->table)
1264 runtime("Missing ROA table");
1265
1266 res.type = T_ENUM_ROA;
1267 res.val.i = roa_check(rtc->table, v1.val.px.ip, v1.val.px.len, as);
1268 break;
1269
23b1539b
PM
1270 default:
1271 bug( "Unknown instruction %d (%c)", what->code, what->code & 0xff);
1272 }
1273 if (what->next)
1274 return interpret(what->next);
1275 return res;
1276}
1277
2d496d20 1278#undef ARG
9a4037d4
PM
1279#define ARG(x,y) \
1280 if (!i_same(f1->y, f2->y)) \
1281 return 0;
1282
1283#define ONEARG ARG(v1, a1.p)
1284#define TWOARGS ARG(v1, a1.p) \
1285 ARG(v2, a2.p)
1286
1287#define A2_SAME if (f1->a2.i != f2->a2.i) return 0;
1288
4c5f93d7 1289/*
b093c328
PM
1290 * i_same - function that does real comparing of instruction trees, you should call filter_same from outside
1291 */
9a4037d4
PM
1292int
1293i_same(struct f_inst *f1, struct f_inst *f2)
1294{
9a4037d4
PM
1295 if ((!!f1) != (!!f2))
1296 return 0;
d4d75628
PM
1297 if (!f1)
1298 return 1;
9a4037d4
PM
1299 if (f1->aux != f2->aux)
1300 return 0;
1301 if (f1->code != f2->code)
1302 return 0;
d4d75628
PM
1303 if (f1 == f2) /* It looks strange, but it is possible with call rewriting trickery */
1304 return 1;
9a4037d4
PM
1305
1306 switch(f1->code) {
1307 case ',': /* fall through */
1308 case '+':
9f0d45d6
PM
1309 case '-':
1310 case '*':
9a4037d4 1311 case '/':
5f4aee76
PM
1312 case '|':
1313 case '&':
92a72a4c 1314 case P('m','p'):
4271f2b7 1315 case P('m','c'):
2d496d20
PM
1316 case P('!','='):
1317 case P('=','='):
9a4037d4 1318 case '<':
2d496d20 1319 case P('<','='): TWOARGS; break;
9a4037d4 1320
995e5894 1321 case '!': ONEARG; break;
9a4037d4 1322 case '~': TWOARGS; break;
2d496d20 1323 case P('d','e'): ONEARG; break;
9a4037d4
PM
1324
1325 case 's':
1326 ARG(v2, a2.p);
1327 {
1328 struct symbol *s1, *s2;
1329 s1 = f1->a1.p;
1330 s2 = f2->a1.p;
1331 if (strcmp(s1->name, s2->name))
1332 return 0;
1333 if (s1->class != s2->class)
1334 return 0;
1335 }
1336 break;
1337
4bb18dd2 1338 case 'c':
b1a597e0
OZ
1339 switch (f1->aux) {
1340
1341 case T_PREFIX_SET:
1342 if (!trie_same(f1->a2.p, f2->a2.p))
1343 return 0;
9be1086d 1344 break;
b1a597e0
OZ
1345
1346 case T_SET:
4bb18dd2
PM
1347 if (!same_tree(f1->a2.p, f2->a2.p))
1348 return 0;
9be1086d 1349 break;
b1a597e0 1350
4bb18dd2
PM
1351 case T_STRING:
1352 if (strcmp(f1->a2.p, f2->a2.p))
1353 return 0;
1354 break;
b1a597e0 1355
4bb18dd2
PM
1356 default:
1357 A2_SAME;
1358 }
1359 break;
9a4037d4 1360 case 'C':
f71bded6 1361 if (val_compare(* (struct f_val *) f1->a1.p, * (struct f_val *) f2->a1.p))
9a4037d4
PM
1362 return 0;
1363 break;
9be1086d
OF
1364 case 'V':
1365 if (strcmp((char *) f1->a2.p, (char *) f2->a2.p))
1366 return 0;
1367 break;
684c6f5a 1368 case 'p': case 'L': ONEARG; break;
9a4037d4 1369 case '?': TWOARGS; break;
afc54517 1370 case '0': case 'E': break;
2d496d20 1371 case P('p',','): ONEARG; A2_SAME; break;
0dc4431c 1372 case 'P':
9a4037d4 1373 case 'a': A2_SAME; break;
2d496d20 1374 case P('e','a'): A2_SAME; break;
0dc4431c
PM
1375 case P('P','S'):
1376 case P('a','S'):
2d496d20 1377 case P('e','S'): ONEARG; A2_SAME; break;
9a4037d4 1378
2d496d20
PM
1379 case 'r': ONEARG; break;
1380 case P('c','p'): ONEARG; break;
d4d75628
PM
1381 case P('c','a'): /* Call rewriting trickery to avoid exponential behaviour */
1382 ONEARG;
1383 if (!i_same(f1->a2.p, f2->a2.p))
1384 return 0;
1385 f2->a2.p = f1->a2.p;
1386 break;
aa461248 1387 case P('c','v'): break; /* internal instruction */
2d496d20
PM
1388 case P('S','W'): ONEARG; if (!same_tree(f1->a2.p, f2->a2.p)) return 0; break;
1389 case P('i','M'): TWOARGS; break;
afc54517 1390 case P('A','p'): TWOARGS; break;
9c400ec9 1391 case P('C','a'): TWOARGS; break;
2eece54a
OZ
1392 case P('a','f'):
1393 case P('a','l'): ONEARG; break;
af582c48
OZ
1394 case P('R','C'):
1395 TWOARGS;
1396 /* Does not really make sense - ROA check resuls may change anyway */
1397 if (strcmp(((struct f_inst_roa_check *) f1)->rtc->name,
1398 ((struct f_inst_roa_check *) f2)->rtc->name))
1399 return 0;
1400 break;
9a4037d4
PM
1401 default:
1402 bug( "Unknown instruction %d in same (%c)", f1->code, f1->code & 0xff);
1403 }
1404 return i_same(f1->next, f2->next);
1405}
1406
ff95080f 1407/**
a03ede64
OZ
1408 * f_run - run a filter for a route
1409 * @filter: filter to run
1410 * @rte: route being filtered, may be modified
1411 * @tmp_attrs: temporary attributes, prepared by caller or generated by f_run()
ff95080f 1412 * @tmp_pool: all filter allocations go from this pool
4c5f93d7 1413 * @flags: flags
a03ede64
OZ
1414 *
1415 * If filter needs to modify the route, there are several
1416 * posibilities. @rte might be read-only (with REF_COW flag), in that
1417 * case rw copy is obtained by rte_cow() and @rte is replaced. If
1418 * @rte is originally rw, it may be directly modified (and it is never
1419 * copied).
1420 *
1421 * The returned rte may reuse the (possibly cached, cloned) rta, or
1422 * (if rta was modificied) contains a modified uncached rta, which
1423 * uses parts allocated from @tmp_pool and parts shared from original
1424 * rta. There is one exception - if @rte is rw but contains a cached
1425 * rta and that is modified, rta in returned rte is also cached.
1426 *
1427 * Ownership of cached rtas is consistent with rte, i.e.
1428 * if a new rte is returned, it has its own clone of cached rta
1429 * (and cached rta of read-only source rte is intact), if rte is
1430 * modified in place, old cached rta is possibly freed.
ff95080f 1431 */
23b1539b 1432int
0a06a9b8 1433f_run(struct filter *filter, struct rte **rte, struct ea_list **tmp_attrs, struct linpool *tmp_pool, int flags)
23b1539b 1434{
36da2857
OZ
1435 if (filter == FILTER_ACCEPT)
1436 return F_ACCEPT;
1437
1438 if (filter == FILTER_REJECT)
1439 return F_REJECT;
1440
a03ede64 1441 int rte_cow = ((*rte)->flags & REF_COW);
6b9fa320 1442 DBG( "Running filter `%s'...", filter->name );
23b1539b 1443
36bbfc70 1444 f_rte = rte;
a03ede64
OZ
1445 f_old_rta = NULL;
1446 f_tmp_attrs = tmp_attrs;
f31156ca 1447 f_pool = tmp_pool;
a03ede64 1448 f_flags = flags;
0d1b3c4c
OZ
1449
1450 log_reset();
a03ede64
OZ
1451 struct f_val res = interpret(filter->root);
1452
1453 if (f_old_rta) {
1454 /*
1455 * Cached rta was modified and f_rte contains now an uncached one,
1456 * sharing some part with the cached one. The cached rta should
1457 * be freed (if rte was originally COW, f_old_rta is a clone
1458 * obtained during rte_cow()).
1459 *
1460 * This also implements the exception mentioned in f_run()
1461 * description. The reason for this is that rta reuses parts of
1462 * f_old_rta, and these may be freed during rta_free(f_old_rta).
1463 * This is not the problem if rte was COW, because original rte
1464 * also holds the same rta.
1465 */
1466 if (!rte_cow)
1467 (*f_rte)->attrs = rta_lookup((*f_rte)->attrs);
1468
1469 rta_free(f_old_rta);
1470 }
1471
0d1b3c4c 1472
0b1cad81
PM
1473 if (res.type != T_RETURN) {
1474 log( L_ERR "Filter %s did not return accept nor reject. Make up your mind", filter->name);
23b1539b 1475 return F_ERROR;
0b1cad81 1476 }
6b9fa320 1477 DBG( "done (%d)\n", res.val.i );
23b1539b
PM
1478 return res.val.i;
1479}
1480
b1c9d871
MM
1481int
1482f_eval_int(struct f_inst *expr)
1c20608e 1483{
0d1b3c4c 1484 /* Called independently in parse-time to eval expressions */
1c20608e
MM
1485 struct f_val res;
1486
b1c9d871
MM
1487 f_flags = 0;
1488 f_tmp_attrs = NULL;
1489 f_rte = NULL;
b1c9d871 1490 f_pool = cfg_mem;
0d1b3c4c
OZ
1491
1492 log_reset();
b1c9d871 1493 res = interpret(expr);
0d1b3c4c 1494
b1c9d871
MM
1495 if (res.type != T_INT)
1496 cf_error("Integer expression expected");
1497 return res.val.i;
1498}
1c20608e 1499
92a72a4c
OZ
1500u32
1501f_eval_asn(struct f_inst *expr)
1502{
0d1b3c4c 1503 /* Called as a part of another interpret call, therefore no log_reset() */
92a72a4c 1504 struct f_val res = interpret(expr);
938b191b 1505 return (res.type == T_INT) ? res.val.i : 0;
92a72a4c
OZ
1506}
1507
ff95080f
PM
1508/**
1509 * filter_same - compare two filters
1510 * @new: first filter to be compared
1511 * @old: second filter to be compared, notice that this filter is
1512 * damaged while comparing.
1513 *
1514 * Returns 1 in case filters are same, otherwise 0. If there are
1515 * underlying bugs, it will rather say 0 on same filters than say
1516 * 1 on different.
1517 */
30a6108c
MM
1518int
1519filter_same(struct filter *new, struct filter *old)
1520{
81ce667b
MM
1521 if (old == new) /* Handle FILTER_ACCEPT and FILTER_REJECT */
1522 return 1;
1523 if (old == FILTER_ACCEPT || old == FILTER_REJECT ||
1524 new == FILTER_ACCEPT || new == FILTER_REJECT)
1525 return 0;
9a4037d4 1526 return i_same(new->root, old->root);
30a6108c 1527}