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