]> git.ipfire.org Git - thirdparty/bird.git/blame - filter/filter.c
Custom route attributes
[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"
0264ccf6
PT
42#include "lib/net.h"
43#include "lib/ip.h"
23b1539b
PM
44#include "nest/route.h"
45#include "nest/protocol.h"
46#include "nest/iface.h"
159fa4ce 47#include "nest/attrs.h"
23b1539b
PM
48#include "conf/conf.h"
49#include "filter/filter.h"
50
38506f71
PM
51#define CMP_ERROR 999
52
9b0a0ba9
OZ
53void (*bt_assert_hook)(int result, struct f_inst *assert);
54
8f8671bc
OZ
55static struct adata undef_adata; /* adata of length 0 used for undefined */
56
57/* Special undef value for paths and clists */
58static inline int
59undef_value(struct f_val v)
60{
61 return ((v.type == T_PATH) || (v.type == T_CLIST) ||
62 (v.type == T_ECLIST) || (v.type == T_LCLIST)) &&
63 (v.val.ad == &undef_adata);
64}
65
9831e591 66static struct adata *
42a0c054 67adata_empty(struct linpool *pool, int l)
ad9074e9 68{
42a0c054
OZ
69 struct adata *res = lp_alloc(pool, sizeof(struct adata) + l);
70 res->length = l;
ad9074e9
PM
71 return res;
72}
73
11cb6202 74static void
0e175f9f 75pm_format(struct f_path_mask *p, buffer *buf)
11cb6202 76{
0e175f9f 77 buffer_puts(buf, "[= ");
11cb6202
OZ
78
79 while (p)
0e175f9f
OZ
80 {
81 switch(p->kind)
11cb6202 82 {
0e175f9f
OZ
83 case PM_ASN:
84 buffer_print(buf, "%u ", p->val);
85 break;
92a72a4c 86
0e175f9f
OZ
87 case PM_QUESTION:
88 buffer_puts(buf, "? ");
89 break;
92a72a4c 90
0e175f9f
OZ
91 case PM_ASTERISK:
92 buffer_puts(buf, "* ");
93 break;
11cb6202 94
a0fe1944
OF
95 case PM_ASN_RANGE:
96 buffer_print(buf, "%u..%u ", p->val, p->val2);
97 break;
98
0e175f9f 99 case PM_ASN_EXPR:
e8bc64e3 100 ASSERT(0);
11cb6202
OZ
101 }
102
0e175f9f
OZ
103 p = p->next;
104 }
105
106 buffer_puts(buf, "=]");
11cb6202
OZ
107}
108
5e173e9f
JMM
109static inline int val_is_ip4(const struct f_val v)
110{ return (v.type == T_IP) && ipa_is_ip4(v.val.ip); }
42a0c054 111
66dbdbd9
OZ
112static inline int
113lcomm_cmp(lcomm v1, lcomm v2)
114{
115 if (v1.asn != v2.asn)
116 return (v1.asn > v2.asn) ? 1 : -1;
117 if (v1.ldp1 != v2.ldp1)
118 return (v1.ldp1 > v2.ldp1) ? 1 : -1;
119 if (v1.ldp2 != v2.ldp2)
120 return (v1.ldp2 > v2.ldp2) ? 1 : -1;
121 return 0;
122}
123
8dcf2544 124/**
3e82b32d 125 * val_compare - compare two values
8dcf2544
PM
126 * @v1: first value
127 * @v2: second value
128 *
28a10f84
OZ
129 * Compares two values and returns -1, 0, 1 on <, =, > or CMP_ERROR on
130 * error. Tree module relies on this giving consistent results so
131 * that it can be used for building balanced trees.
b093c328 132 */
38506f71
PM
133int
134val_compare(struct f_val v1, struct f_val v2)
135{
f71bded6 136 if (v1.type != v2.type) {
70c57805
OZ
137 if (v1.type == T_VOID) /* Hack for else */
138 return -1;
139 if (v2.type == T_VOID)
140 return 1;
141
126683fe 142 /* IP->Quad implicit conversion */
5e173e9f
JMM
143 if ((v1.type == T_QUAD) && val_is_ip4(v2))
144 return uint_cmp(v1.val.i, ipa_to_u32(v2.val.ip));
145 if (val_is_ip4(v1) && (v2.type == T_QUAD))
146 return uint_cmp(ipa_to_u32(v1.val.ip), v2.val.i);
126683fe 147
f71bded6 148 debug( "Types do not match in val_compare\n" );
7db7b7db 149 return CMP_ERROR;
f71bded6 150 }
28a10f84 151
38506f71 152 switch (v1.type) {
28a10f84
OZ
153 case T_VOID:
154 return 0;
f4536657 155 case T_ENUM:
a6c9f064
OF
156 case T_INT:
157 case T_BOOL:
d3dd620b 158 case T_PAIR:
126683fe 159 case T_QUAD:
2dec1e34 160 return uint_cmp(v1.val.i, v2.val.i);
42a0c054 161 case T_EC:
8c9986d3 162 case T_RD:
42a0c054 163 return u64_cmp(v1.val.ec, v2.val.ec);
66dbdbd9
OZ
164 case T_LC:
165 return lcomm_cmp(v1.val.lc, v2.val.lc);
43fc099b 166 case T_IP:
5e173e9f
JMM
167 return ipa_compare(v1.val.ip, v2.val.ip);
168 case T_NET:
169 return net_compare(v1.val.net, v2.val.net);
e29fa06e
OZ
170 case T_STRING:
171 return strcmp(v1.val.s, v2.val.s);
3076b5ae
MM
172 default:
173 return CMP_ERROR;
38506f71
PM
174 }
175}
176
28a10f84 177static int
122deb6d 178pm_same(struct f_path_mask *m1, struct f_path_mask *m2)
dfd48621 179{
28a10f84
OZ
180 while (m1 && m2)
181 {
122deb6d 182 if (m1->kind != m2->kind)
28a10f84
OZ
183 return 0;
184
122deb6d
OZ
185 if (m1->kind == PM_ASN_EXPR)
186 {
187 if (!i_same((struct f_inst *) m1->val, (struct f_inst *) m2->val))
188 return 0;
189 }
190 else
191 {
192 if ((m1->val != m2->val) || (m1->val2 != m2->val2))
193 return 0;
194 }
195
28a10f84
OZ
196 m1 = m1->next;
197 m2 = m2->next;
198 }
199
33d22f0e 200 return !m1 && !m2;
28a10f84
OZ
201}
202
203/**
204 * val_same - compare two values
205 * @v1: first value
206 * @v2: second value
207 *
208 * Compares two values and returns 1 if they are same and 0 if not.
209 * Comparison of values of different types is valid and returns 0.
210 */
211int
212val_same(struct f_val v1, struct f_val v2)
dfd48621 213{
28a10f84
OZ
214 int rc;
215
216 rc = val_compare(v1, v2);
217 if (rc != CMP_ERROR)
218 return !rc;
219
220 if (v1.type != v2.type)
221 return 0;
222
223 switch (v1.type) {
224 case T_PATH_MASK:
122deb6d 225 return pm_same(v1.val.path_mask, v2.val.path_mask);
28a10f84
OZ
226 case T_PATH:
227 case T_CLIST:
228 case T_ECLIST:
66dbdbd9 229 case T_LCLIST:
28a10f84
OZ
230 return adata_same(v1.val.ad, v2.val.ad);
231 case T_SET:
232 return same_tree(v1.val.t, v2.val.t);
233 case T_PREFIX_SET:
234 return trie_same(v1.val.ti, v2.val.ti);
235 default:
236 bug("Invalid type in val_same(): %x", v1.type);
237 }
dfd48621 238}
b1a597e0 239
ba5c0057
OZ
240static int
241clist_set_type(struct f_tree *set, struct f_val *v)
242{
e92a4b85
OZ
243 switch (set->from.type)
244 {
ba5c0057
OZ
245 case T_PAIR:
246 v->type = T_PAIR;
247 return 1;
e92a4b85 248
ba5c0057 249 case T_QUAD:
ba5c0057
OZ
250 v->type = T_QUAD;
251 return 1;
e92a4b85
OZ
252
253 case T_IP:
254 if (val_is_ip4(set->from) && val_is_ip4(set->to))
255 {
256 v->type = T_QUAD;
257 return 1;
258 }
259 /* Fall through */
ba5c0057
OZ
260 default:
261 v->type = T_VOID;
262 return 0;
263 }
264}
265
42a0c054
OZ
266static inline int
267eclist_set_type(struct f_tree *set)
268{ return set->from.type == T_EC; }
269
66dbdbd9
OZ
270static inline int
271lclist_set_type(struct f_tree *set)
272{ return set->from.type == T_LC; }
273
ba5c0057
OZ
274static int
275clist_match_set(struct adata *clist, struct f_tree *set)
276{
277 if (!clist)
278 return 0;
279
280 struct f_val v;
281 if (!clist_set_type(set, &v))
282 return CMP_ERROR;
283
284 u32 *l = (u32 *) clist->data;
285 u32 *end = l + clist->length/4;
42a0c054 286
ba5c0057
OZ
287 while (l < end) {
288 v.val.i = *l++;
289 if (find_tree(set, v))
290 return 1;
291 }
292 return 0;
293}
294
42a0c054
OZ
295static int
296eclist_match_set(struct adata *list, struct f_tree *set)
297{
298 if (!list)
299 return 0;
300
301 if (!eclist_set_type(set))
302 return CMP_ERROR;
303
304 struct f_val v;
305 u32 *l = int_set_get_data(list);
306 int len = int_set_get_size(list);
307 int i;
308
309 v.type = T_EC;
310 for (i = 0; i < len; i += 2) {
311 v.val.ec = ec_get(l, i);
312 if (find_tree(set, v))
313 return 1;
314 }
315
316 return 0;
317}
318
66dbdbd9
OZ
319static int
320lclist_match_set(struct adata *list, struct f_tree *set)
321{
322 if (!list)
323 return 0;
324
325 if (!lclist_set_type(set))
326 return CMP_ERROR;
327
328 struct f_val v;
329 u32 *l = int_set_get_data(list);
330 int len = int_set_get_size(list);
331 int i;
332
333 v.type = T_LC;
334 for (i = 0; i < len; i += 3) {
335 v.val.lc = lc_get(l, i);
336 if (find_tree(set, v))
337 return 1;
338 }
339
340 return 0;
341}
342
ba5c0057 343static struct adata *
0888a737 344clist_filter(struct linpool *pool, struct adata *list, struct f_val set, int pos)
ba5c0057 345{
0888a737 346 if (!list)
ba5c0057
OZ
347 return NULL;
348
0888a737 349 int tree = (set.type == T_SET); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
ba5c0057 350 struct f_val v;
0888a737
OZ
351 if (tree)
352 clist_set_type(set.val.t, &v);
353 else
354 v.type = T_PAIR;
ba5c0057 355
0888a737
OZ
356 int len = int_set_get_size(list);
357 u32 *l = int_set_get_data(list);
358 u32 tmp[len];
ba5c0057 359 u32 *k = tmp;
0888a737 360 u32 *end = l + len;
ba5c0057
OZ
361
362 while (l < end) {
363 v.val.i = *l++;
0888a737
OZ
364 /* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
365 if ((tree ? !!find_tree(set.val.t, v) : int_set_contains(set.val.ad, v.val.i)) == pos)
ba5c0057
OZ
366 *k++ = v.val.i;
367 }
368
3e236955 369 uint nl = (k - tmp) * sizeof(u32);
0888a737
OZ
370 if (nl == list->length)
371 return list;
ba5c0057 372
42a0c054
OZ
373 struct adata *res = adata_empty(pool, nl);
374 memcpy(res->data, tmp, nl);
375 return res;
376}
377
378static struct adata *
0888a737 379eclist_filter(struct linpool *pool, struct adata *list, struct f_val set, int pos)
42a0c054
OZ
380{
381 if (!list)
382 return NULL;
383
0888a737 384 int tree = (set.type == T_SET); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
42a0c054
OZ
385 struct f_val v;
386
387 int len = int_set_get_size(list);
388 u32 *l = int_set_get_data(list);
389 u32 tmp[len];
390 u32 *k = tmp;
391 int i;
392
393 v.type = T_EC;
394 for (i = 0; i < len; i += 2) {
395 v.val.ec = ec_get(l, i);
0888a737
OZ
396 /* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
397 if ((tree ? !!find_tree(set.val.t, v) : ec_set_contains(set.val.ad, v.val.ec)) == pos) {
42a0c054
OZ
398 *k++ = l[i];
399 *k++ = l[i+1];
400 }
401 }
402
3e236955 403 uint nl = (k - tmp) * sizeof(u32);
42a0c054
OZ
404 if (nl == list->length)
405 return list;
406
407 struct adata *res = adata_empty(pool, nl);
ba5c0057
OZ
408 memcpy(res->data, tmp, nl);
409 return res;
410}
411
66dbdbd9
OZ
412static struct adata *
413lclist_filter(struct linpool *pool, struct adata *list, struct f_val set, int pos)
414{
415 if (!list)
416 return NULL;
417
418 int tree = (set.type == T_SET); /* 1 -> set is T_SET, 0 -> set is T_CLIST */
419 struct f_val v;
420
421 int len = int_set_get_size(list);
422 u32 *l = int_set_get_data(list);
423 u32 tmp[len];
424 u32 *k = tmp;
425 int i;
426
427 v.type = T_LC;
428 for (i = 0; i < len; i += 3) {
429 v.val.lc = lc_get(l, i);
430 /* pos && member(val, set) || !pos && !member(val, set), member() depends on tree */
431 if ((tree ? !!find_tree(set.val.t, v) : lc_set_contains(set.val.ad, v.val.lc)) == pos)
432 k = lc_copy(k, l+i);
433 }
434
3e236955 435 uint nl = (k - tmp) * sizeof(u32);
42a0c054
OZ
436 if (nl == list->length)
437 return list;
438
439 struct adata *res = adata_empty(pool, nl);
ba5c0057
OZ
440 memcpy(res->data, tmp, nl);
441 return res;
442}
443
8dcf2544 444/**
3e82b32d 445 * val_in_range - implement |~| operator
8dcf2544
PM
446 * @v1: element
447 * @v2: set
448 *
b655596d 449 * Checks if @v1 is element (|~| operator) of @v2.
b093c328 450 */
9831e591 451static int
7db7b7db
PM
452val_in_range(struct f_val v1, struct f_val v2)
453{
b655596d
OZ
454 if ((v1.type == T_PATH) && (v2.type == T_PATH_MASK))
455 return as_path_match(v1.val.ad, v2.val.path_mask);
6dc7a0cb 456
b655596d 457 if ((v1.type == T_INT) && (v2.type == T_PATH))
a15dab76 458 return as_path_contains(v2.val.ad, v1.val.i, 1);
6dc7a0cb 459
b655596d
OZ
460 if (((v1.type == T_PAIR) || (v1.type == T_QUAD)) && (v2.type == T_CLIST))
461 return int_set_contains(v2.val.ad, v1.val.i);
b655596d 462 /* IP->Quad implicit conversion */
5e173e9f
JMM
463 if (val_is_ip4(v1) && (v2.type == T_CLIST))
464 return int_set_contains(v2.val.ad, ipa_to_u32(v1.val.ip));
b1a597e0 465
b655596d
OZ
466 if ((v1.type == T_EC) && (v2.type == T_ECLIST))
467 return ec_set_contains(v2.val.ad, v1.val.ec);
ba5c0057 468
66dbdbd9
OZ
469 if ((v1.type == T_LC) && (v2.type == T_LCLIST))
470 return lc_set_contains(v2.val.ad, v1.val.lc);
471
b655596d
OZ
472 if ((v1.type == T_STRING) && (v2.type == T_STRING))
473 return patmatch(v2.val.s, v1.val.s);
42a0c054 474
5e173e9f
JMM
475 if ((v1.type == T_IP) && (v2.type == T_NET))
476 return ipa_in_netX(v1.val.ip, v2.val.net);
7db7b7db 477
5e173e9f
JMM
478 if ((v1.type == T_NET) && (v2.type == T_NET))
479 return net_in_netX(v1.val.net, v2.val.net);
0d1b3c4c 480
5e173e9f
JMM
481 if ((v1.type == T_NET) && (v2.type == T_PREFIX_SET))
482 return trie_match_net(v2.val.ti, v1.val.net);
0d1b3c4c 483
b655596d
OZ
484 if (v2.type != T_SET)
485 return CMP_ERROR;
0d1b3c4c 486
b655596d
OZ
487 /* With integrated Quad<->IP implicit conversion */
488 if ((v1.type == v2.val.t->from.type) ||
e92a4b85 489 ((v1.type == T_QUAD) && val_is_ip4(v2.val.t->from) && val_is_ip4(v2.val.t->to)))
b655596d 490 return !!find_tree(v2.val.t, v1);
0d1b3c4c 491
b655596d 492 if (v1.type == T_CLIST)
ba5c0057 493 return clist_match_set(v1.val.ad, v2.val.t);
0d1b3c4c 494
b655596d 495 if (v1.type == T_ECLIST)
42a0c054
OZ
496 return eclist_match_set(v1.val.ad, v2.val.t);
497
66dbdbd9
OZ
498 if (v1.type == T_LCLIST)
499 return lclist_match_set(v1.val.ad, v2.val.t);
500
b655596d 501 if (v1.type == T_PATH)
cc31b75a
OZ
502 return as_path_match_set(v1.val.ad, v2.val.t);
503
7db7b7db 504 return CMP_ERROR;
38506f71
PM
505}
506
4c5f93d7 507/*
0e175f9f 508 * val_format - format filter value
b093c328 509 */
508d9360 510void
0e175f9f 511val_format(struct f_val v, buffer *buf)
38506f71 512{
ecd25633 513 char buf2[1024];
0e175f9f
OZ
514 switch (v.type)
515 {
516 case T_VOID: buffer_puts(buf, "(void)"); return;
517 case T_BOOL: buffer_puts(buf, v.val.i ? "TRUE" : "FALSE"); return;
52e030e1 518 case T_INT: buffer_print(buf, "%u", v.val.i); return;
0e175f9f 519 case T_STRING: buffer_print(buf, "%s", v.val.s); return;
5e173e9f
JMM
520 case T_IP: buffer_print(buf, "%I", v.val.ip); return;
521 case T_NET: buffer_print(buf, "%N", v.val.net); return;
52e030e1 522 case T_PAIR: buffer_print(buf, "(%u,%u)", v.val.i >> 16, v.val.i & 0xffff); return;
0e175f9f
OZ
523 case T_QUAD: buffer_print(buf, "%R", v.val.i); return;
524 case T_EC: ec_format(buf2, v.val.ec); buffer_print(buf, "%s", buf2); return;
66dbdbd9 525 case T_LC: lc_format(buf2, v.val.lc); buffer_print(buf, "%s", buf2); return;
8c9986d3 526 case T_RD: rd_format(v.val.ec, buf2, 1024); buffer_print(buf, "%s", buf2); return;
0e175f9f
OZ
527 case T_PREFIX_SET: trie_format(v.val.ti, buf); return;
528 case T_SET: tree_format(v.val.t, buf); return;
52e030e1 529 case T_ENUM: buffer_print(buf, "(enum %x)%u", v.type, v.val.i); return;
0e175f9f
OZ
530 case T_PATH: as_path_format(v.val.ad, buf2, 1000); buffer_print(buf, "(path %s)", buf2); return;
531 case T_CLIST: int_set_format(v.val.ad, 1, -1, buf2, 1000); buffer_print(buf, "(clist %s)", buf2); return;
532 case T_ECLIST: ec_set_format(v.val.ad, -1, buf2, 1000); buffer_print(buf, "(eclist %s)", buf2); return;
66dbdbd9 533 case T_LCLIST: lc_set_format(v.val.ad, -1, buf2, 1000); buffer_print(buf, "(lclist %s)", buf2); return;
0e175f9f
OZ
534 case T_PATH_MASK: pm_format(v.val.path_mask, buf); return;
535 default: buffer_print(buf, "[unknown type %x]", v.type); return;
38506f71 536 }
38506f71
PM
537}
538
a03ede64
OZ
539static struct rte **f_rte;
540static struct rta *f_old_rta;
13c0be19 541static struct ea_list **f_eattrs;
a03ede64 542static struct linpool *f_pool;
0e175f9f 543static struct buffer f_buf;
0a06a9b8 544static int f_flags;
36bbfc70 545
13c0be19
JMM
546static inline void f_cache_eattrs(void)
547{
548 f_eattrs = &((*f_rte)->attrs->eattrs);
549}
550
a03ede64
OZ
551static inline void f_rte_cow(void)
552{
13c0be19
JMM
553 if (!((*f_rte)->flags & REF_COW))
554 return;
555
556 *f_rte = rte_do_cow(*f_rte);
a03ede64
OZ
557}
558
4c5f93d7 559/*
b093c328
PM
560 * rta_cow - prepare rta for modification by filter
561 */
9831e591 562static void
a03ede64 563f_rta_cow(void)
26c09e1d 564{
8d9eef17
OZ
565 if (!rta_is_cached((*f_rte)->attrs))
566 return;
567
568 /* Prepare to modify rte */
569 f_rte_cow();
570
571 /* Store old rta to free it later, it stores reference from rte_cow() */
572 f_old_rta = (*f_rte)->attrs;
573
574 /*
575 * Get shallow copy of rta. Fields eattrs and nexthops of rta are shared
576 * with f_old_rta (they will be copied when the cached rta will be obtained
577 * at the end of f_run()), also the lock of hostentry is inherited (we
578 * suppose hostentry is not changed by filters).
579 */
580 (*f_rte)->attrs = rta_do_cow((*f_rte)->attrs, f_pool);
13c0be19
JMM
581
582 /* Re-cache the ea_list */
583 f_cache_eattrs();
26c09e1d
PM
584}
585
4b135d09
PT
586static char *
587val_format_str(struct f_val v) {
588 buffer b;
589 LOG_BUFFER_INIT(b);
590 val_format(v, &b);
591 return lp_strdup(f_pool, b.start);
592}
593
1123e707 594static struct tbf rl_runtime_err = TBF_DEFAULT_LOG_LIMITS;
cb530392 595
9b0a0ba9 596#define runtime(fmt, ...) do { \
b9405791 597 if (!(f_flags & FF_SILENT)) \
75d98b60 598 log_rl(&rl_runtime_err, L_ERR "filters, line %d: " fmt, what->lineno, ##__VA_ARGS__); \
9a4037d4
PM
599 res.type = T_RETURN; \
600 res.val.i = F_ERROR; \
601 return res; \
602 } while(0)
603
c3becfe1 604#define ARG_ANY(n) INTERPRET(v##n, what->a##n.p)
0ec6b5ec
JMM
605
606#define ARG(n,t) ARG_ANY(n) \
607 if (v##n.type != t) \
608 runtime("Argument %d of instruction %s must be of type %02x, got %02x", \
609 n, f_instruction_name(what->fi_code), t, v##n.type);
610
c3becfe1
JMM
611#define INTERPRET(val, what_) \
612 val = interpret(what_); \
613 if (val.type & T_RETURN) \
614 return val;
615
508d9360 616#define ACCESS_RTE \
1771f70d 617 do { if (!f_rte) runtime("No route to access"); } while (0)
13c0be19
JMM
618
619#define ACCESS_EATTRS \
620 do { if (!f_eattrs) f_cache_eattrs(); } while (0)
7db7b7db 621
315f23a0
OZ
622#define BITFIELD_MASK(what) \
623 (1u << (what->a2.i >> 24))
624
b093c328
PM
625/**
626 * interpret
2e9b2421 627 * @what: filter to interpret
b093c328 628 *
4c5f93d7 629 * Interpret given tree of filter instructions. This is core function
b093c328 630 * of filter system and does all the hard work.
771ae456
PM
631 *
632 * Each instruction has 4 fields: code (which is instruction code),
633 * aux (which is extension to instruction code, typically type),
634 * arg1 and arg2 - arguments. Depending on instruction, arguments
315f23a0 635 * are either integers, or pointers to instruction trees. Common
771ae456
PM
636 * instructions like +, that have two expressions as arguments use
637 * TWOARGS macro to get both of them evaluated.
638 *
639 * &f_val structures are copied around, so there are no problems with
640 * memory managment.
b093c328 641 */
23b1539b
PM
642static struct f_val
643interpret(struct f_inst *what)
644{
645 struct symbol *sym;
478f9bab 646 struct f_val v1, v2, v3, res = { .type = T_VOID }, *vp;
92a72a4c 647 unsigned u1, u2;
6a57bb31 648 int i;
7ea5b00f 649 u32 as;
23b1539b 650
7c601e6b 651 for ( ; what; what = what->next) {
23b1539b 652 res.type = T_VOID;
5a14df39 653 switch(what->fi_code) {
23b1539b 654/* Binary operators */
5a14df39 655 case FI_ADD:
0ec6b5ec
JMM
656 ARG(1,T_INT);
657 ARG(2,T_INT);
658 res.type = T_INT;
659 res.val.i = v1.val.i + v2.val.i;
23b1539b 660 break;
5a14df39 661 case FI_SUBTRACT:
0ec6b5ec
JMM
662 ARG(1,T_INT);
663 ARG(2,T_INT);
664 res.type = T_INT;
665 res.val.i = v1.val.i - v2.val.i;
9f0d45d6 666 break;
5a14df39 667 case FI_MULTIPLY:
0ec6b5ec
JMM
668 ARG(1,T_INT);
669 ARG(2,T_INT);
670 res.type = T_INT;
671 res.val.i = v1.val.i * v2.val.i;
9f0d45d6 672 break;
5a14df39 673 case FI_DIVIDE:
0ec6b5ec
JMM
674 ARG(1,T_INT);
675 ARG(2,T_INT);
676 res.type = T_INT;
677 if (v2.val.i == 0) runtime( "Mother told me not to divide by 0" );
678 res.val.i = v1.val.i / v2.val.i;
23b1539b 679 break;
5a14df39
MJM
680 case FI_AND:
681 case FI_OR:
0ec6b5ec 682 ARG(1,T_BOOL);
5a14df39 683 if (v1.val.i == (what->fi_code == FI_OR)) {
0aa88530
OZ
684 res.type = T_BOOL;
685 res.val.i = v1.val.i;
0ec6b5ec
JMM
686 } else {
687 ARG(2,T_BOOL);
688 res = v2;
0aa88530 689 }
5f4aee76 690 break;
5a14df39 691 case FI_PAIR_CONSTRUCT:
0ec6b5ec
JMM
692 ARG(1,T_INT);
693 ARG(2,T_INT);
92a72a4c
OZ
694 u1 = v1.val.i;
695 u2 = v2.val.i;
696 if ((u1 > 0xFFFF) || (u2 > 0xFFFF))
697 runtime( "Can't operate with value out of bounds in pair constructor" );
698 res.val.i = (u1 << 16) | u2;
699 res.type = T_PAIR;
700 break;
701
5a14df39 702 case FI_EC_CONSTRUCT:
42a0c054 703 {
0ec6b5ec
JMM
704 ARG_ANY(1);
705 ARG(2, T_INT);
42a0c054
OZ
706
707 int check, ipv4_used;
708 u32 key, val;
709
710 if (v1.type == T_INT) {
711 ipv4_used = 0; key = v1.val.i;
315f23a0 712 }
42a0c054
OZ
713 else if (v1.type == T_QUAD) {
714 ipv4_used = 1; key = v1.val.i;
715 }
42a0c054 716 /* IP->Quad implicit conversion */
5e173e9f
JMM
717 else if (val_is_ip4(v1)) {
718 ipv4_used = 1; key = ipa_to_u32(v1.val.ip);
42a0c054 719 }
42a0c054
OZ
720 else
721 runtime("Can't operate with key of non-integer/IPv4 type in EC constructor");
722
42a0c054
OZ
723 val = v2.val.i;
724
66dbdbd9 725 /* XXXX */
42a0c054
OZ
726 res.type = T_EC;
727
728 if (what->aux == EC_GENERIC) {
729 check = 0; res.val.ec = ec_generic(key, val);
730 }
731 else if (ipv4_used) {
732 check = 1; res.val.ec = ec_ip4(what->aux, key, val);
733 }
734 else if (key < 0x10000) {
735 check = 0; res.val.ec = ec_as2(what->aux, key, val);
736 }
737 else {
738 check = 1; res.val.ec = ec_as4(what->aux, key, val);
739 }
740
741 if (check && (val > 0xFFFF))
742 runtime("Can't operate with value out of bounds in EC constructor");
743
744 break;
745 }
746
5a14df39 747 case FI_LC_CONSTRUCT:
66dbdbd9 748 {
0ec6b5ec
JMM
749 ARG(1, T_INT);
750 ARG(2, T_INT);
751 ARG(3, T_INT);
66dbdbd9
OZ
752
753 res.type = T_LC;
754 res.val.lc = (lcomm) { v1.val.i, v2.val.i, v3.val.i };
755
756 break;
757 }
758
e8bc64e3
JMM
759 case FI_PATHMASK_CONSTRUCT:
760 {
761 struct f_path_mask *tt = what->a1.p, *vbegin, **vv = &vbegin;
762
763 while (tt) {
764 *vv = lp_alloc(f_pool, sizeof(struct f_path_mask));
765 if (tt->kind == PM_ASN_EXPR) {
c3becfe1
JMM
766 struct f_val res;
767 INTERPRET(res, (struct f_inst *) tt->val);
e8bc64e3
JMM
768 (*vv)->kind = PM_ASN;
769 if (res.type != T_INT) {
770 runtime( "Error resolving path mask template: value not an integer" );
771 return (struct f_val) { .type = T_VOID };
772 }
773
774 (*vv)->val = res.val.i;
775 } else {
776 **vv = *tt;
777 }
778 tt = tt->next;
779 vv = &((*vv)->next);
780 }
781
782 res = (struct f_val) { .type = T_PATH_MASK, .val.path_mask = vbegin };
783 break;
784 }
785
23b1539b 786/* Relational operators */
38506f71
PM
787
788#define COMPARE(x) \
0ec6b5ec
JMM
789 ARG_ANY(1); \
790 ARG_ANY(2); \
38506f71
PM
791 i = val_compare(v1, v2); \
792 if (i==CMP_ERROR) \
126683fe
OZ
793 runtime( "Can't compare values of incompatible types" ); \
794 res.type = T_BOOL; \
38506f71 795 res.val.i = (x); \
23b1539b 796 break;
38506f71 797
28a10f84 798#define SAME(x) \
0ec6b5ec
JMM
799 ARG_ANY(1); \
800 ARG_ANY(2); \
28a10f84
OZ
801 i = val_same(v1, v2); \
802 res.type = T_BOOL; \
803 res.val.i = (x); \
804 break;
805
5a14df39
MJM
806 case FI_NEQ: SAME(!i);
807 case FI_EQ: SAME(i);
808 case FI_LT: COMPARE(i==-1);
809 case FI_LTE: COMPARE(i!=1);
38506f71 810
5a14df39 811 case FI_NOT:
0ec6b5ec 812 ARG(1,T_BOOL);
995e5894
PM
813 res = v1;
814 res.val.i = !res.val.i;
815 break;
816
5a14df39 817 case FI_MATCH:
0ec6b5ec
JMM
818 ARG_ANY(1);
819 ARG_ANY(2);
23b1539b 820 res.type = T_BOOL;
7db7b7db
PM
821 res.val.i = val_in_range(v1, v2);
822 if (res.val.i == CMP_ERROR)
823 runtime( "~ applied on unknown type pair" );
0aa88530 824 res.val.i = !!res.val.i;
23b1539b 825 break;
768d5e10 826
5a14df39 827 case FI_NOT_MATCH:
0ec6b5ec
JMM
828 ARG_ANY(1);
829 ARG_ANY(2);
768d5e10
PT
830 res.type = T_BOOL;
831 res.val.i = val_in_range(v1, v2);
832 if (res.val.i == CMP_ERROR)
833 runtime( "!~ applied on unknown type pair" );
834 res.val.i = !res.val.i;
835 break;
836
5a14df39 837 case FI_DEFINED:
0ec6b5ec 838 ARG_ANY(1);
f4536657 839 res.type = T_BOOL;
8f8671bc 840 res.val.i = (v1.type != T_VOID) && !undef_value(v1);
f4536657 841 break;
d1ba927b 842 case FI_TYPE:
0ec6b5ec 843 ARG_ANY(1); /* There may be more types supporting this operation */
8c9986d3
JMM
844 switch (v1.type)
845 {
846 case T_NET:
847 res.type = T_ENUM_NETTYPE;
848 res.val.i = v1.val.net->type;
849 break;
850 default:
851 runtime( "Can't determine type of this item" );
852 }
853 break;
d1ba927b 854 case FI_IS_V4:
0ec6b5ec 855 ARG(1, T_IP);
61e501da
JMM
856 res.type = T_BOOL;
857 res.val.i = ipa_is_ip4(v1.val.ip);
858 break;
23b1539b 859
d3dd620b 860 /* Set to indirect value, a1 = variable, a2 = value */
5a14df39 861 case FI_SET:
0ec6b5ec 862 ARG_ANY(2);
2db3b288 863 sym = what->a1.p;
126683fe 864 vp = sym->def;
e92a4b85
OZ
865 if ((sym->class != (SYM_VARIABLE | v2.type)) && (v2.type != T_VOID))
866 {
126683fe 867 /* IP->Quad implicit conversion */
5e173e9f
JMM
868 if ((sym->class == (SYM_VARIABLE | T_QUAD)) && val_is_ip4(v2))
869 {
126683fe 870 vp->type = T_QUAD;
5e173e9f 871 vp->val.i = ipa_to_u32(v2.val.ip);
126683fe
OZ
872 break;
873 }
aa461248 874 runtime( "Assigning to variable of incompatible type" );
126683fe 875 }
315f23a0 876 *vp = v2;
23b1539b
PM
877 break;
878
083c43e2 879 /* some constants have value in a2, some in *a1.p, strange. */
5a14df39 880 case FI_CONSTANT: /* integer (or simple type) constant, string, set, or prefix_set */
c7b43f33 881 res.type = what->aux;
083c43e2 882
b1a597e0
OZ
883 if (res.type == T_PREFIX_SET)
884 res.val.ti = what->a2.p;
885 else if (res.type == T_SET)
083c43e2
OZ
886 res.val.t = what->a2.p;
887 else if (res.type == T_STRING)
888 res.val.s = what->a2.p;
889 else
890 res.val.i = what->a2.i;
23b1539b 891 break;
5a14df39
MJM
892 case FI_VARIABLE:
893 case FI_CONSTANT_INDIRECT:
38506f71
PM
894 res = * ((struct f_val *) what->a1.p);
895 break;
5a14df39 896 case FI_PRINT:
0ec6b5ec 897 ARG_ANY(1);
0e175f9f 898 val_format(v1, &f_buf);
23b1539b 899 break;
5a14df39 900 case FI_CONDITION: /* ? has really strange error value, so we can implement if ... else nicely :-) */
0ec6b5ec 901 ARG(1, T_BOOL);
23b1539b 902 if (v1.val.i) {
0ec6b5ec 903 ARG_ANY(2);
23b1539b 904 res.val.i = 0;
0ec6b5ec
JMM
905 } else
906 res.val.i = 1;
23b1539b
PM
907 res.type = T_BOOL;
908 break;
5a14df39 909 case FI_NOP:
3cf4a2e2 910 debug( "No operation\n" );
23b1539b 911 break;
5a14df39 912 case FI_PRINT_AND_DIE:
0ec6b5ec 913 ARG_ANY(1);
b9405791
OZ
914 if ((what->a2.i == F_NOP || (what->a2.i != F_NONL && what->a1.p)) &&
915 !(f_flags & FF_SILENT))
0e175f9f 916 log_commit(*L_INFO, &f_buf);
23b1539b 917
2db3b288 918 switch (what->a2.i) {
23b1539b
PM
919 case F_QUITBIRD:
920 die( "Filter asked me to die" );
921 case F_ACCEPT:
922 /* Should take care about turning ACCEPT into MODIFY */
923 case F_ERROR:
2ad6dcdb 924 case F_REJECT: /* FIXME (noncritical) Should print complete route along with reason to reject route */
23b1539b 925 res.type = T_RETURN;
2ad6dcdb 926 res.val.i = what->a2.i;
7e1f9971 927 return res; /* We have to return now, no more processing. */
d3dd620b 928 case F_NONL:
23b1539b
PM
929 case F_NOP:
930 break;
931 default:
b178d92a 932 bug( "unknown return type: Can't happen");
23b1539b
PM
933 }
934 break;
5a14df39 935 case FI_RTA_GET: /* rta access */
36bbfc70 936 {
508d9360 937 ACCESS_RTE;
36bbfc70 938 struct rta *rta = (*f_rte)->attrs;
c7b43f33 939 res.type = what->aux;
a5fc5958
OZ
940
941 switch (what->a2.i)
942 {
5e173e9f 943 case SA_FROM: res.val.ip = rta->from; break;
4e276a89 944 case SA_GW: res.val.ip = rta->nh.gw; break;
5e173e9f 945 case SA_NET: res.val.net = (*f_rte)->net->n.addr; break;
736e143f 946 case SA_PROTO: res.val.s = rta->src->proto->name; break;
a5fc5958
OZ
947 case SA_SOURCE: res.val.i = rta->source; break;
948 case SA_SCOPE: res.val.i = rta->scope; break;
a5fc5958 949 case SA_DEST: res.val.i = rta->dest; break;
4e276a89
JMM
950 case SA_IFNAME: res.val.s = rta->nh.iface ? rta->nh.iface->name : ""; break;
951 case SA_IFINDEX: res.val.i = rta->nh.iface ? rta->nh.iface->index : 0; break;
a5fc5958 952
36bbfc70 953 default:
a5fc5958 954 bug("Invalid static attribute access (%x)", res.type);
36bbfc70
PM
955 }
956 }
957 break;
5a14df39 958 case FI_RTA_SET:
508d9360 959 ACCESS_RTE;
0ec6b5ec 960 ARG_ANY(1);
0dc4431c 961 if (what->aux != v1.type)
98da26a0 962 runtime( "Attempt to set static attribute to incompatible type" );
a5fc5958 963
a03ede64 964 f_rta_cow();
0dc4431c
PM
965 {
966 struct rta *rta = (*f_rte)->attrs;
182a7895 967
a5fc5958
OZ
968 switch (what->a2.i)
969 {
970 case SA_FROM:
5e173e9f 971 rta->from = v1.val.ip;
0dc4431c 972 break;
182a7895 973
a5fc5958 974 case SA_GW:
00192d5a 975 {
5e173e9f 976 ip_addr ip = v1.val.ip;
586c1800 977 neighbor *n = neigh_find(rta->src->proto, ip, NULL, 0);
00192d5a
OZ
978 if (!n || (n->scope == SCOPE_HOST))
979 runtime( "Invalid gw address" );
980
4e276a89
JMM
981 rta->dest = RTD_UNICAST;
982 rta->nh.gw = ip;
983 rta->nh.iface = n->iface;
984 rta->nh.next = NULL;
00192d5a
OZ
985 rta->hostentry = NULL;
986 }
0dc4431c 987 break;
182a7895 988
a5fc5958 989 case SA_SCOPE:
182a7895
OZ
990 rta->scope = v1.val.i;
991 break;
992
a5fc5958 993 case SA_DEST:
182a7895
OZ
994 i = v1.val.i;
995 if ((i != RTD_BLACKHOLE) && (i != RTD_UNREACHABLE) && (i != RTD_PROHIBIT))
996 runtime( "Destination can be changed only to blackhole, unreachable or prohibit" );
00192d5a 997
182a7895 998 rta->dest = i;
4e276a89
JMM
999 rta->nh.gw = IPA_NONE;
1000 rta->nh.iface = NULL;
1001 rta->nh.next = NULL;
00192d5a 1002 rta->hostentry = NULL;
182a7895
OZ
1003 break;
1004
f2d8e680
OZ
1005 case SA_IFNAME:
1006 {
1007 struct iface *ifa = if_find_by_name(v1.val.s);
1008 if (!ifa)
1009 runtime( "Invalid iface name" );
1010
1011 rta->dest = RTD_UNICAST;
1012 rta->nh.gw = IPA_NONE;
1013 rta->nh.iface = ifa;
1014 rta->nh.next = NULL;
1015 rta->hostentry = NULL;
1016 }
1017 break;
1018
0dc4431c 1019 default:
a5fc5958 1020 bug("Invalid static attribute access (%x)", res.type);
0dc4431c
PM
1021 }
1022 }
1023 break;
5a14df39 1024 case FI_EA_GET: /* Access to extended attributes */
508d9360 1025 ACCESS_RTE;
13c0be19 1026 ACCESS_EATTRS;
91447965 1027 {
315f23a0 1028 u16 code = what->a2.i;
1561ee79 1029 int f_type = what->aux >> 8;
13c0be19 1030 eattr *e = ea_find(*f_eattrs, code);
e8da1bd0
OZ
1031
1032 if (!e) {
8f8671bc
OZ
1033 /* A special case: undefined as_path looks like empty as_path */
1034 if ((what->aux & EAF_TYPE_MASK) == EAF_TYPE_AS_PATH) {
1035 res.type = T_PATH;
1036 res.val.ad = &undef_adata;
1037 break;
1038 }
1039
1040 /* The same special case for int_set */
0277cc0b
OZ
1041 if ((what->aux & EAF_TYPE_MASK) == EAF_TYPE_INT_SET) {
1042 res.type = T_CLIST;
8f8671bc 1043 res.val.ad = &undef_adata;
42a0c054
OZ
1044 break;
1045 }
315f23a0 1046
42a0c054 1047 /* The same special case for ec_set */
315f23a0 1048 if ((what->aux & EAF_TYPE_MASK) == EAF_TYPE_EC_SET) {
42a0c054 1049 res.type = T_ECLIST;
8f8671bc 1050 res.val.ad = &undef_adata;
0277cc0b
OZ
1051 break;
1052 }
42a0c054 1053
66dbdbd9
OZ
1054 /* The same special case for lc_set */
1055 if ((what->aux & EAF_TYPE_MASK) == EAF_TYPE_LC_SET) {
1056 res.type = T_LCLIST;
8f8671bc 1057 res.val.ad = &undef_adata;
66dbdbd9
OZ
1058 break;
1059 }
1060
e8da1bd0
OZ
1061 /* Undefined value */
1062 res.type = T_VOID;
1063 break;
1064 }
1065
265419a3 1066 switch (e->type & EAF_TYPE_MASK) {
e8da1bd0 1067 case EAF_TYPE_INT:
1561ee79 1068 res.type = f_type;
91447965
PM
1069 res.val.i = e->u.data;
1070 break;
126683fe
OZ
1071 case EAF_TYPE_ROUTER_ID:
1072 res.type = T_QUAD;
1073 res.val.i = e->u.data;
1074 break;
e8da1bd0
OZ
1075 case EAF_TYPE_OPAQUE:
1076 res.type = T_ENUM_EMPTY;
1077 res.val.i = 0;
1078 break;
330aecea 1079 case EAF_TYPE_IP_ADDRESS:
330aecea
OZ
1080 res.type = T_IP;
1081 struct adata * ad = e->u.ptr;
5e173e9f 1082 res.val.ip = * (ip_addr *) ad->data;
330aecea 1083 break;
0150e521
PM
1084 case EAF_TYPE_AS_PATH:
1085 res.type = T_PATH;
1086 res.val.ad = e->u.ptr;
1087 break;
315f23a0
OZ
1088 case EAF_TYPE_BITFIELD:
1089 res.type = T_BOOL;
1090 res.val.i = !!(e->u.data & BITFIELD_MASK(what));
1091 break;
0150e521
PM
1092 case EAF_TYPE_INT_SET:
1093 res.type = T_CLIST;
10a53608
PM
1094 res.val.ad = e->u.ptr;
1095 break;
42a0c054
OZ
1096 case EAF_TYPE_EC_SET:
1097 res.type = T_ECLIST;
1098 res.val.ad = e->u.ptr;
1099 break;
66dbdbd9
OZ
1100 case EAF_TYPE_LC_SET:
1101 res.type = T_LCLIST;
1102 res.val.ad = e->u.ptr;
1103 break;
e8da1bd0
OZ
1104 case EAF_TYPE_UNDEF:
1105 res.type = T_VOID;
1106 break;
2803c9dd 1107 default:
ad9074e9 1108 bug("Unknown type in e,a");
91447965
PM
1109 }
1110 }
6dc7a0cb 1111 break;
5a14df39 1112 case FI_EA_SET:
508d9360 1113 ACCESS_RTE;
13c0be19 1114 ACCESS_EATTRS;
0ec6b5ec 1115 ARG_ANY(1);
f31156ca
PM
1116 {
1117 struct ea_list *l = lp_alloc(f_pool, sizeof(struct ea_list) + sizeof(eattr));
315f23a0 1118 u16 code = what->a2.i;
1561ee79 1119 int f_type = what->aux >> 8;
f31156ca
PM
1120
1121 l->next = NULL;
1122 l->flags = EALF_SORTED;
1123 l->count = 1;
315f23a0 1124 l->attrs[0].id = code;
913ce95b 1125 l->attrs[0].flags = 0;
1561ee79 1126 l->attrs[0].type = (what->aux & 0xff) | EAF_ORIGINATED | EAF_FRESH;
315f23a0 1127
31e79264
PM
1128 switch (what->aux & EAF_TYPE_MASK) {
1129 case EAF_TYPE_INT:
1561ee79 1130 if (v1.type != f_type)
31e79264 1131 runtime( "Setting int attribute to non-int value" );
f31156ca
PM
1132 l->attrs[0].u.data = v1.val.i;
1133 break;
3e40f3e7
OZ
1134
1135 case EAF_TYPE_ROUTER_ID:
3e40f3e7 1136 /* IP->Quad implicit conversion */
5e173e9f
JMM
1137 if (val_is_ip4(v1)) {
1138 l->attrs[0].u.data = ipa_to_u32(v1.val.ip);
3e40f3e7
OZ
1139 break;
1140 }
3e40f3e7
OZ
1141 /* T_INT for backward compatibility */
1142 if ((v1.type != T_QUAD) && (v1.type != T_INT))
1143 runtime( "Setting quad attribute to non-quad value" );
1144 l->attrs[0].u.data = v1.val.i;
1145 break;
1146
e8da1bd0
OZ
1147 case EAF_TYPE_OPAQUE:
1148 runtime( "Setting opaque attribute is not allowed" );
1149 break;
330aecea
OZ
1150 case EAF_TYPE_IP_ADDRESS:
1151 if (v1.type != T_IP)
1152 runtime( "Setting ip attribute to non-ip value" );
1153 int len = sizeof(ip_addr);
1154 struct adata *ad = lp_alloc(f_pool, sizeof(struct adata) + len);
1155 ad->length = len;
5e173e9f 1156 (* (ip_addr *) ad->data) = v1.val.ip;
54fe0d92 1157 l->attrs[0].u.ptr = ad;
330aecea 1158 break;
10a53608
PM
1159 case EAF_TYPE_AS_PATH:
1160 if (v1.type != T_PATH)
1161 runtime( "Setting path attribute to non-path value" );
1162 l->attrs[0].u.ptr = v1.val.ad;
1163 break;
315f23a0
OZ
1164 case EAF_TYPE_BITFIELD:
1165 if (v1.type != T_BOOL)
1166 runtime( "Setting bit in bitfield attribute to non-bool value" );
1167 {
1168 /* First, we have to find the old value */
13c0be19 1169 eattr *e = ea_find(*f_eattrs, code);
315f23a0
OZ
1170 u32 data = e ? e->u.data : 0;
1171
1172 if (v1.val.i)
1173 l->attrs[0].u.data = data | BITFIELD_MASK(what);
1174 else
1175 l->attrs[0].u.data = data & ~BITFIELD_MASK(what);;
1176 }
1177 break;
708711c3
PM
1178 case EAF_TYPE_INT_SET:
1179 if (v1.type != T_CLIST)
42a0c054
OZ
1180 runtime( "Setting clist attribute to non-clist value" );
1181 l->attrs[0].u.ptr = v1.val.ad;
1182 break;
1183 case EAF_TYPE_EC_SET:
1184 if (v1.type != T_ECLIST)
1185 runtime( "Setting eclist attribute to non-eclist value" );
708711c3
PM
1186 l->attrs[0].u.ptr = v1.val.ad;
1187 break;
66dbdbd9
OZ
1188 case EAF_TYPE_LC_SET:
1189 if (v1.type != T_LCLIST)
1190 runtime( "Setting lclist attribute to non-lclist value" );
1191 l->attrs[0].u.ptr = v1.val.ad;
1192 break;
31e79264
PM
1193 case EAF_TYPE_UNDEF:
1194 if (v1.type != T_VOID)
1195 runtime( "Setting void attribute to non-void value" );
48f9e019
PM
1196 l->attrs[0].u.data = 0;
1197 break;
0150e521 1198 default: bug("Unknown type in e,S");
f31156ca 1199 }
31e79264 1200
13c0be19
JMM
1201 f_rta_cow();
1202 l->next = *f_eattrs;
1203 *f_eattrs = l;
f31156ca 1204 }
f31156ca 1205 break;
5a14df39 1206 case FI_PREF_GET:
508d9360 1207 ACCESS_RTE;
0dc4431c
PM
1208 res.type = T_INT;
1209 res.val.i = (*f_rte)->pref;
1210 break;
5a14df39 1211 case FI_PREF_SET:
508d9360 1212 ACCESS_RTE;
0ec6b5ec 1213 ARG(1,T_INT);
7d37bf79 1214 if (v1.val.i > 0xFFFF)
f4c6ca8c 1215 runtime( "Setting preference value out of bounds" );
a03ede64 1216 f_rte_cow();
0dc4431c
PM
1217 (*f_rte)->pref = v1.val.i;
1218 break;
5a14df39 1219 case FI_LENGTH: /* Get length of */
0ec6b5ec 1220 ARG_ANY(1);
684c6f5a
PM
1221 res.type = T_INT;
1222 switch(v1.type) {
5e173e9f 1223 case T_NET: res.val.i = net_pxlen(v1.val.net); break;
684c6f5a 1224 case T_PATH: res.val.i = as_path_getlen(v1.val.ad); break;
7ccb36d3
OZ
1225 case T_CLIST: res.val.i = int_set_get_size(v1.val.ad); break;
1226 case T_ECLIST: res.val.i = ec_set_get_size(v1.val.ad); break;
66dbdbd9 1227 case T_LCLIST: res.val.i = lc_set_get_size(v1.val.ad); break;
7ccb36d3 1228 default: runtime( "Prefix, path, clist or eclist expected" );
684c6f5a
PM
1229 }
1230 break;
b24b7811 1231 case FI_SADR_SRC: /* Get SADR src prefix */
0ec6b5ec
JMM
1232 ARG(1, T_NET);
1233 if (!net_is_sadr(v1.val.net))
b24b7811
OZ
1234 runtime( "SADR expected" );
1235
1236 {
1237 net_addr_ip6_sadr *net = (void *) v1.val.net;
1238 net_addr *src = lp_alloc(f_pool, sizeof(net_addr_ip6));
1239 net_fill_ip6(src, net->src_prefix, net->src_pxlen);
1240
1241 res.type = T_NET;
1242 res.val.net = src;
1243 }
1244 break;
d1ba927b 1245 case FI_ROA_MAXLEN: /* Get ROA max prefix length */
0ec6b5ec
JMM
1246 ARG(1, T_NET);
1247 if (!net_is_roa(v1.val.net))
e58f8c28
PT
1248 runtime( "ROA expected" );
1249
1250 res.type = T_INT;
1251 res.val.i = (v1.val.net->type == NET_ROA4) ?
1252 ((net_addr_roa4 *) v1.val.net)->max_pxlen :
1253 ((net_addr_roa6 *) v1.val.net)->max_pxlen;
1254 break;
d1ba927b 1255 case FI_ROA_ASN: /* Get ROA ASN */
0ec6b5ec
JMM
1256 ARG(1, T_NET);
1257 if (!net_is_roa(v1.val.net))
69ae5784
PT
1258 runtime( "ROA expected" );
1259
1260 res.type = T_INT;
1261 res.val.i = (v1.val.net->type == NET_ROA4) ?
1262 ((net_addr_roa4 *) v1.val.net)->asn :
1263 ((net_addr_roa6 *) v1.val.net)->asn;
1264 break;
5a14df39 1265 case FI_IP: /* Convert prefix to ... */
0ec6b5ec 1266 ARG(1, T_NET);
5e173e9f
JMM
1267 res.type = T_IP;
1268 res.val.ip = net_prefix(v1.val.net);
36bbfc70 1269 break;
d1ba927b 1270 case FI_ROUTE_DISTINGUISHER:
0ec6b5ec
JMM
1271 ARG(1, T_NET);
1272 res.type = T_IP;
a5d2a344 1273 if (!net_is_vpn(v1.val.net))
8c9986d3 1274 runtime( "VPN address expected" );
a5d2a344
OZ
1275 res.type = T_RD;
1276 res.val.ec = net_rd(v1.val.net);
8c9986d3 1277 break;
5a14df39 1278 case FI_AS_PATH_FIRST: /* Get first ASN from AS PATH */
0ec6b5ec 1279 ARG(1, T_PATH);
7ea5b00f
OZ
1280
1281 as = 0;
52b9b2a1 1282 as_path_get_first(v1.val.ad, &as);
7ea5b00f
OZ
1283 res.type = T_INT;
1284 res.val.i = as;
1285 break;
5a14df39 1286 case FI_AS_PATH_LAST: /* Get last ASN from AS PATH */
0ec6b5ec 1287 ARG(1, T_PATH);
7ea5b00f
OZ
1288
1289 as = 0;
52b9b2a1 1290 as_path_get_last(v1.val.ad, &as);
7ea5b00f
OZ
1291 res.type = T_INT;
1292 res.val.i = as;
1293 break;
5a14df39 1294 case FI_AS_PATH_LAST_NAG: /* Get last ASN from non-aggregated part of AS PATH */
0ec6b5ec 1295 ARG(1, T_PATH);
9c9cc35c
OZ
1296
1297 res.type = T_INT;
1298 res.val.i = as_path_get_last_nonaggregated(v1.val.ad);
1299 break;
5a14df39 1300 case FI_RETURN:
0ec6b5ec 1301 ARG_ANY(1);
2d496d20
PM
1302 res = v1;
1303 res.type |= T_RETURN;
44711e0c 1304 return res;
5a14df39 1305 case FI_CALL: /* CALL: this is special: if T_RETURN and returning some value, mask it out */
0ec6b5ec 1306 ARG_ANY(1);
6542ece9 1307 res = interpret(what->a2.p);
2d496d20
PM
1308 if (res.type == T_RETURN)
1309 return res;
315f23a0 1310 res.type &= ~T_RETURN;
6542ece9 1311 break;
5a14df39 1312 case FI_CLEAR_LOCAL_VARS: /* Clear local variables */
aa461248
OZ
1313 for (sym = what->a1.p; sym != NULL; sym = sym->aux2)
1314 ((struct f_val *) sym->def)->type = T_VOID;
1315 break;
5a14df39 1316 case FI_SWITCH:
0ec6b5ec 1317 ARG_ANY(1);
41be4444
PM
1318 {
1319 struct f_tree *t = find_tree(what->a2.p, v1);
1320 if (!t) {
1321 v1.type = T_VOID;
1322 t = find_tree(what->a2.p, v1);
1323 if (!t) {
ad9074e9 1324 debug( "No else statement?\n");
41be4444
PM
1325 break;
1326 }
315f23a0 1327 }
1877dab2 1328 /* It is actually possible to have t->data NULL */
44711e0c 1329
c3becfe1 1330 INTERPRET(res, t->data);
41be4444 1331 }
7db7b7db 1332 break;
5a14df39 1333 case FI_IP_MASK: /* IP.MASK(val) */
0ec6b5ec
JMM
1334 ARG(1, T_IP);
1335 ARG(2, T_INT);
0bf95f99
OZ
1336
1337 res.type = T_IP;
1338 res.val.ip = ipa_is_ip4(v1.val.ip) ?
1339 ipa_from_ip4(ip4_and(ipa_to_ip4(v1.val.ip), ip4_mkmask(v2.val.i))) :
1340 ipa_from_ip6(ip6_and(ipa_to_ip6(v1.val.ip), ip6_mkmask(v2.val.i)));
d3dd620b 1341 break;
afc54517 1342
5a14df39 1343 case FI_EMPTY: /* Create empty attribute */
afc54517 1344 res.type = what->aux;
42a0c054 1345 res.val.ad = adata_empty(f_pool, 0);
afc54517 1346 break;
5a14df39 1347 case FI_PATH_PREPEND: /* Path prepend */
0ec6b5ec
JMM
1348 ARG(1, T_PATH);
1349 ARG(2, T_INT);
afc54517
PM
1350
1351 res.type = T_PATH;
1352 res.val.ad = as_path_prepend(f_pool, v1.val.ad, v2.val.i);
1353 break;
1354
5a14df39 1355 case FI_CLIST_ADD_DEL: /* (Extended) Community list add or delete */
0ec6b5ec
JMM
1356 ARG_ANY(1);
1357 ARG_ANY(2);
bff9ce51
OZ
1358 if (v1.type == T_PATH)
1359 {
1360 struct f_tree *set = NULL;
1361 u32 key = 0;
1362 int pos;
1363
1364 if (v2.type == T_INT)
1365 key = v2.val.i;
1366 else if ((v2.type == T_SET) && (v2.val.t->from.type == T_INT))
1367 set = v2.val.t;
1368 else
1369 runtime("Can't delete non-integer (set)");
1370
1371 switch (what->aux)
1372 {
1373 case 'a': runtime("Can't add to path");
1374 case 'd': pos = 0; break;
1375 case 'f': pos = 1; break;
1376 default: bug("unknown Ca operation");
1377 }
1378
1379 if (pos && !set)
1380 runtime("Can't filter integer");
1381
1382 res.type = T_PATH;
1383 res.val.ad = as_path_filter(f_pool, v1.val.ad, set, key, pos);
1384 }
1385 else if (v1.type == T_CLIST)
42a0c054
OZ
1386 {
1387 /* Community (or cluster) list */
1388 struct f_val dummy;
1389 int arg_set = 0;
52e030e1 1390 uint n = 0;
a58022a6 1391
42a0c054 1392 if ((v2.type == T_PAIR) || (v2.type == T_QUAD))
52e030e1 1393 n = v2.val.i;
42a0c054 1394 /* IP->Quad implicit conversion */
e92a4b85 1395 else if (val_is_ip4(v2))
5e173e9f 1396 n = ipa_to_u32(v2.val.ip);
42a0c054
OZ
1397 else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy))
1398 arg_set = 1;
0888a737
OZ
1399 else if (v2.type == T_CLIST)
1400 arg_set = 2;
42a0c054
OZ
1401 else
1402 runtime("Can't add/delete non-pair");
1403
1404 res.type = T_CLIST;
1405 switch (what->aux)
1406 {
1407 case 'a':
0888a737 1408 if (arg_set == 1)
42a0c054 1409 runtime("Can't add set");
0888a737 1410 else if (!arg_set)
52e030e1 1411 res.val.ad = int_set_add(f_pool, v1.val.ad, n);
315f23a0 1412 else
0888a737 1413 res.val.ad = int_set_union(f_pool, v1.val.ad, v2.val.ad);
42a0c054 1414 break;
315f23a0 1415
42a0c054
OZ
1416 case 'd':
1417 if (!arg_set)
52e030e1 1418 res.val.ad = int_set_del(f_pool, v1.val.ad, n);
42a0c054 1419 else
0888a737 1420 res.val.ad = clist_filter(f_pool, v1.val.ad, v2, 0);
42a0c054 1421 break;
9c400ec9 1422
42a0c054
OZ
1423 case 'f':
1424 if (!arg_set)
1425 runtime("Can't filter pair");
0888a737 1426 res.val.ad = clist_filter(f_pool, v1.val.ad, v2, 1);
42a0c054
OZ
1427 break;
1428
1429 default:
1430 bug("unknown Ca operation");
1431 }
1432 }
1433 else if (v1.type == T_ECLIST)
e08d2ff0 1434 {
42a0c054
OZ
1435 /* Extended community list */
1436 int arg_set = 0;
315f23a0 1437
42a0c054
OZ
1438 /* v2.val is either EC or EC-set */
1439 if ((v2.type == T_SET) && eclist_set_type(v2.val.t))
1440 arg_set = 1;
0888a737
OZ
1441 else if (v2.type == T_ECLIST)
1442 arg_set = 2;
42a0c054 1443 else if (v2.type != T_EC)
66dbdbd9 1444 runtime("Can't add/delete non-ec");
42a0c054
OZ
1445
1446 res.type = T_ECLIST;
1447 switch (what->aux)
1448 {
1449 case 'a':
0888a737 1450 if (arg_set == 1)
42a0c054 1451 runtime("Can't add set");
0888a737
OZ
1452 else if (!arg_set)
1453 res.val.ad = ec_set_add(f_pool, v1.val.ad, v2.val.ec);
315f23a0 1454 else
0888a737 1455 res.val.ad = ec_set_union(f_pool, v1.val.ad, v2.val.ad);
42a0c054 1456 break;
315f23a0 1457
42a0c054
OZ
1458 case 'd':
1459 if (!arg_set)
1460 res.val.ad = ec_set_del(f_pool, v1.val.ad, v2.val.ec);
1461 else
0888a737 1462 res.val.ad = eclist_filter(f_pool, v1.val.ad, v2, 0);
42a0c054 1463 break;
e08d2ff0 1464
42a0c054
OZ
1465 case 'f':
1466 if (!arg_set)
1467 runtime("Can't filter ec");
0888a737 1468 res.val.ad = eclist_filter(f_pool, v1.val.ad, v2, 1);
42a0c054 1469 break;
e08d2ff0 1470
42a0c054
OZ
1471 default:
1472 bug("unknown Ca operation");
1473 }
9c400ec9 1474 }
66dbdbd9
OZ
1475 else if (v1.type == T_LCLIST)
1476 {
1477 /* Large community list */
1478 int arg_set = 0;
1479
1480 /* v2.val is either LC or LC-set */
1481 if ((v2.type == T_SET) && lclist_set_type(v2.val.t))
1482 arg_set = 1;
1483 else if (v2.type == T_LCLIST)
1484 arg_set = 2;
1485 else if (v2.type != T_LC)
1486 runtime("Can't add/delete non-lc");
1487
1488 res.type = T_LCLIST;
1489 switch (what->aux)
1490 {
1491 case 'a':
1492 if (arg_set == 1)
1493 runtime("Can't add set");
1494 else if (!arg_set)
1495 res.val.ad = lc_set_add(f_pool, v1.val.ad, v2.val.lc);
1496 else
1497 res.val.ad = lc_set_union(f_pool, v1.val.ad, v2.val.ad);
1498 break;
1499
1500 case 'd':
1501 if (!arg_set)
1502 res.val.ad = lc_set_del(f_pool, v1.val.ad, v2.val.lc);
1503 else
1504 res.val.ad = lclist_filter(f_pool, v1.val.ad, v2, 0);
1505 break;
1506
1507 case 'f':
1508 if (!arg_set)
1509 runtime("Can't filter lc");
1510 res.val.ad = lclist_filter(f_pool, v1.val.ad, v2, 1);
1511 break;
1512
1513 default:
1514 bug("unknown Ca operation");
1515 }
1516 }
42a0c054 1517 else
66dbdbd9 1518 runtime("Can't add/delete to non-[e|l]clist");
42a0c054 1519
9c400ec9
PM
1520 break;
1521
5a14df39 1522 case FI_ROA_CHECK: /* ROA Check */
af582c48
OZ
1523 if (what->arg1)
1524 {
0ec6b5ec
JMM
1525 ARG(1, T_NET);
1526 ARG(2, T_INT);
af582c48
OZ
1527
1528 as = v2.val.i;
1529 }
1530 else
1531 {
508d9360 1532 ACCESS_RTE;
13c0be19 1533 ACCESS_EATTRS;
5e173e9f 1534 v1.val.net = (*f_rte)->net->n.addr;
af582c48
OZ
1535
1536 /* We ignore temporary attributes, probably not a problem here */
1537 /* 0x02 is a value of BA_AS_PATH, we don't want to include BGP headers */
13c0be19 1538 eattr *e = ea_find(*f_eattrs, EA_CODE(PROTOCOL_BGP, 0x02));
af582c48 1539
41b83e52 1540 if (!e || ((e->type & EAF_TYPE_MASK) != EAF_TYPE_AS_PATH))
af582c48
OZ
1541 runtime("Missing AS_PATH attribute");
1542
1543 as_path_get_last(e->u.ptr, &as);
1544 }
1545
0264ccf6 1546 struct rtable *table = ((struct f_inst_roa_check *) what)->rtc->table;
286e2011 1547 if (!table)
af582c48
OZ
1548 runtime("Missing ROA table");
1549
65d2a88d
PT
1550 if (table->addr_type != NET_ROA4 && table->addr_type != NET_ROA6)
1551 runtime("Table type must be either ROA4 or ROA6");
286e2011 1552
af582c48 1553 res.type = T_ENUM_ROA;
65d2a88d
PT
1554
1555 if (table->addr_type != (v1.val.net->type == NET_IP4 ? NET_ROA4 : NET_ROA6))
1556 res.val.i = ROA_UNKNOWN; /* Prefix and table type mismatch */
1557 else
1558 res.val.i = net_roa_check(table, v1.val.net, as);
0264ccf6 1559
af582c48 1560 break;
0264ccf6 1561
d1ba927b 1562 case FI_FORMAT: /* Format */
0ec6b5ec 1563 ARG_ANY(1);
4b135d09
PT
1564
1565 res.type = T_STRING;
1566 res.val.s = val_format_str(v1);
1567 break;
1568
d1ba927b 1569 case FI_ASSERT: /* Birdtest Assert */
0ec6b5ec 1570 ARG(1, T_BOOL);
9b0a0ba9
OZ
1571
1572 res.type = v1.type;
1573 res.val = v1.val;
1574
1575 CALL(bt_assert_hook, res.val.i, what);
1576 break;
af582c48 1577
23b1539b 1578 default:
5a14df39 1579 bug( "Unknown instruction %d (%c)", what->fi_code, what->fi_code & 0xff);
7c601e6b 1580 }}
23b1539b
PM
1581 return res;
1582}
1583
2d496d20 1584#undef ARG
0ec6b5ec
JMM
1585#undef ARG_ANY
1586
1587#define ARG(n) \
1588 if (!i_same(f1->a##n.p, f2->a##n.p)) \
9a4037d4
PM
1589 return 0;
1590
0ec6b5ec
JMM
1591#define ONEARG ARG(1);
1592#define TWOARGS ONEARG; ARG(2);
1593#define THREEARGS TWOARGS; ARG(3);
9a4037d4
PM
1594
1595#define A2_SAME if (f1->a2.i != f2->a2.i) return 0;
1596
4c5f93d7 1597/*
b093c328
PM
1598 * i_same - function that does real comparing of instruction trees, you should call filter_same from outside
1599 */
9a4037d4
PM
1600int
1601i_same(struct f_inst *f1, struct f_inst *f2)
1602{
9a4037d4
PM
1603 if ((!!f1) != (!!f2))
1604 return 0;
d4d75628
PM
1605 if (!f1)
1606 return 1;
9a4037d4
PM
1607 if (f1->aux != f2->aux)
1608 return 0;
5a14df39 1609 if (f1->fi_code != f2->fi_code)
9a4037d4 1610 return 0;
d4d75628
PM
1611 if (f1 == f2) /* It looks strange, but it is possible with call rewriting trickery */
1612 return 1;
9a4037d4 1613
5a14df39 1614 switch(f1->fi_code) {
74bfd2f9 1615 case FI_ADD: /* fall through */
5a14df39
MJM
1616 case FI_SUBTRACT:
1617 case FI_MULTIPLY:
1618 case FI_DIVIDE:
1619 case FI_OR:
1620 case FI_AND:
1621 case FI_PAIR_CONSTRUCT:
1622 case FI_EC_CONSTRUCT:
1623 case FI_NEQ:
1624 case FI_EQ:
1625 case FI_LT:
1626 case FI_LTE: TWOARGS; break;
1627
e8bc64e3
JMM
1628 case FI_PATHMASK_CONSTRUCT: if (!pm_same(f1->a1.p, f2->a1.p)) return 0; break;
1629
5a14df39
MJM
1630 case FI_NOT: ONEARG; break;
1631 case FI_NOT_MATCH:
1632 case FI_MATCH: TWOARGS; break;
1633 case FI_DEFINED: ONEARG; break;
d1ba927b 1634 case FI_TYPE: ONEARG; break;
5a14df39
MJM
1635
1636 case FI_LC_CONSTRUCT:
478f9bab 1637 THREEARGS;
66dbdbd9
OZ
1638 break;
1639
5a14df39 1640 case FI_SET:
0ec6b5ec 1641 ARG(2);
9a4037d4
PM
1642 {
1643 struct symbol *s1, *s2;
1644 s1 = f1->a1.p;
1645 s2 = f2->a1.p;
1646 if (strcmp(s1->name, s2->name))
1647 return 0;
1648 if (s1->class != s2->class)
1649 return 0;
1650 }
1651 break;
1652
5a14df39 1653 case FI_CONSTANT:
b1a597e0
OZ
1654 switch (f1->aux) {
1655
1656 case T_PREFIX_SET:
1657 if (!trie_same(f1->a2.p, f2->a2.p))
1658 return 0;
9be1086d 1659 break;
b1a597e0
OZ
1660
1661 case T_SET:
4bb18dd2
PM
1662 if (!same_tree(f1->a2.p, f2->a2.p))
1663 return 0;
9be1086d 1664 break;
b1a597e0 1665
4bb18dd2
PM
1666 case T_STRING:
1667 if (strcmp(f1->a2.p, f2->a2.p))
1668 return 0;
1669 break;
b1a597e0 1670
4bb18dd2
PM
1671 default:
1672 A2_SAME;
1673 }
1674 break;
507e182a 1675
5a14df39 1676 case FI_CONSTANT_INDIRECT:
28a10f84 1677 if (!val_same(* (struct f_val *) f1->a1.p, * (struct f_val *) f2->a1.p))
9a4037d4
PM
1678 return 0;
1679 break;
28a10f84 1680
5a14df39 1681 case FI_VARIABLE:
9be1086d
OF
1682 if (strcmp((char *) f1->a2.p, (char *) f2->a2.p))
1683 return 0;
1684 break;
5a14df39
MJM
1685 case FI_PRINT: case FI_LENGTH: ONEARG; break;
1686 case FI_CONDITION: TWOARGS; break;
1687 case FI_NOP: case FI_EMPTY: break;
1688 case FI_PRINT_AND_DIE: ONEARG; A2_SAME; break;
1689 case FI_PREF_GET:
1690 case FI_RTA_GET: A2_SAME; break;
1691 case FI_EA_GET: A2_SAME; break;
1692 case FI_PREF_SET:
1693 case FI_RTA_SET:
1694 case FI_EA_SET: ONEARG; A2_SAME; break;
1695
1696 case FI_RETURN: ONEARG; break;
823ad121
JMM
1697 case FI_ROA_MAXLEN: ONEARG; break;
1698 case FI_ROA_ASN: ONEARG; break;
b24b7811 1699 case FI_SADR_SRC: ONEARG; break;
5a14df39 1700 case FI_IP: ONEARG; break;
823ad121 1701 case FI_IS_V4: ONEARG; break;
d1ba927b 1702 case FI_ROUTE_DISTINGUISHER: ONEARG; break;
5a14df39 1703 case FI_CALL: /* Call rewriting trickery to avoid exponential behaviour */
315f23a0 1704 ONEARG;
d4d75628 1705 if (!i_same(f1->a2.p, f2->a2.p))
315f23a0 1706 return 0;
d4d75628
PM
1707 f2->a2.p = f1->a2.p;
1708 break;
5a14df39
MJM
1709 case FI_CLEAR_LOCAL_VARS: break; /* internal instruction */
1710 case FI_SWITCH: ONEARG; if (!same_tree(f1->a2.p, f2->a2.p)) return 0; break;
1711 case FI_IP_MASK: TWOARGS; break;
1712 case FI_PATH_PREPEND: TWOARGS; break;
1713 case FI_CLIST_ADD_DEL: TWOARGS; break;
1714 case FI_AS_PATH_FIRST:
1715 case FI_AS_PATH_LAST:
1716 case FI_AS_PATH_LAST_NAG: ONEARG; break;
1717 case FI_ROA_CHECK:
af582c48 1718 TWOARGS;
6f535924 1719 /* Does not really make sense - ROA check results may change anyway */
315f23a0 1720 if (strcmp(((struct f_inst_roa_check *) f1)->rtc->name,
af582c48
OZ
1721 ((struct f_inst_roa_check *) f2)->rtc->name))
1722 return 0;
1723 break;
823ad121
JMM
1724 case FI_FORMAT: ONEARG; break;
1725 case FI_ASSERT: ONEARG; break;
9a4037d4 1726 default:
5a14df39 1727 bug( "Unknown instruction %d in same (%c)", f1->fi_code, f1->fi_code & 0xff);
9a4037d4
PM
1728 }
1729 return i_same(f1->next, f2->next);
1730}
1731
ff95080f 1732/**
a03ede64
OZ
1733 * f_run - run a filter for a route
1734 * @filter: filter to run
1735 * @rte: route being filtered, may be modified
ff95080f 1736 * @tmp_pool: all filter allocations go from this pool
4c5f93d7 1737 * @flags: flags
a03ede64
OZ
1738 *
1739 * If filter needs to modify the route, there are several
1740 * posibilities. @rte might be read-only (with REF_COW flag), in that
1741 * case rw copy is obtained by rte_cow() and @rte is replaced. If
1742 * @rte is originally rw, it may be directly modified (and it is never
1743 * copied).
1744 *
1745 * The returned rte may reuse the (possibly cached, cloned) rta, or
1746 * (if rta was modificied) contains a modified uncached rta, which
1747 * uses parts allocated from @tmp_pool and parts shared from original
1748 * rta. There is one exception - if @rte is rw but contains a cached
1749 * rta and that is modified, rta in returned rte is also cached.
1750 *
1751 * Ownership of cached rtas is consistent with rte, i.e.
1752 * if a new rte is returned, it has its own clone of cached rta
1753 * (and cached rta of read-only source rte is intact), if rte is
1754 * modified in place, old cached rta is possibly freed.
ff95080f 1755 */
23b1539b 1756int
13c0be19 1757f_run(struct filter *filter, struct rte **rte, struct linpool *tmp_pool, int flags)
23b1539b 1758{
36da2857
OZ
1759 if (filter == FILTER_ACCEPT)
1760 return F_ACCEPT;
1761
1762 if (filter == FILTER_REJECT)
1763 return F_REJECT;
1764
a03ede64 1765 int rte_cow = ((*rte)->flags & REF_COW);
6b9fa320 1766 DBG( "Running filter `%s'...", filter->name );
23b1539b 1767
36bbfc70 1768 f_rte = rte;
1771f70d 1769 f_eattrs = NULL;
a03ede64 1770 f_old_rta = NULL;
f31156ca 1771 f_pool = tmp_pool;
a03ede64 1772 f_flags = flags;
0d1b3c4c 1773
0e175f9f
OZ
1774 LOG_BUFFER_INIT(f_buf);
1775
a03ede64
OZ
1776 struct f_val res = interpret(filter->root);
1777
1778 if (f_old_rta) {
1779 /*
1780 * Cached rta was modified and f_rte contains now an uncached one,
1781 * sharing some part with the cached one. The cached rta should
1782 * be freed (if rte was originally COW, f_old_rta is a clone
1783 * obtained during rte_cow()).
1784 *
1785 * This also implements the exception mentioned in f_run()
1786 * description. The reason for this is that rta reuses parts of
1787 * f_old_rta, and these may be freed during rta_free(f_old_rta).
1788 * This is not the problem if rte was COW, because original rte
1789 * also holds the same rta.
1790 */
1791 if (!rte_cow)
1792 (*f_rte)->attrs = rta_lookup((*f_rte)->attrs);
1793
1794 rta_free(f_old_rta);
1795 }
1796
0d1b3c4c 1797
0b1cad81 1798 if (res.type != T_RETURN) {
b9405791
OZ
1799 if (!(f_flags & FF_SILENT))
1800 log_rl(&rl_runtime_err, L_ERR "Filter %s did not return accept nor reject. Make up your mind", filter->name);
23b1539b 1801 return F_ERROR;
0b1cad81 1802 }
52e030e1 1803 DBG( "done (%u)\n", res.val.i );
23b1539b
PM
1804 return res.val.i;
1805}
1806
1321e12a
OZ
1807/* TODO: perhaps we could integrate f_eval(), f_eval_rte() and f_run() */
1808
1809struct f_val
1810f_eval_rte(struct f_inst *expr, struct rte **rte, struct linpool *tmp_pool)
1811{
1321e12a
OZ
1812
1813 f_rte = rte;
1771f70d 1814 f_eattrs = NULL;
1321e12a 1815 f_old_rta = NULL;
1321e12a
OZ
1816 f_pool = tmp_pool;
1817 f_flags = 0;
1818
1819 LOG_BUFFER_INIT(f_buf);
1820
1821 /* Note that in this function we assume that rte->attrs is private / uncached */
1822 struct f_val res = interpret(expr);
1823
1321e12a
OZ
1824 return res;
1825}
1826
508d9360
OZ
1827struct f_val
1828f_eval(struct f_inst *expr, struct linpool *tmp_pool)
1c20608e 1829{
b1c9d871 1830 f_flags = 0;
1771f70d 1831 f_eattrs = NULL;
b1c9d871 1832 f_rte = NULL;
508d9360 1833 f_pool = tmp_pool;
0d1b3c4c 1834
0e175f9f
OZ
1835 LOG_BUFFER_INIT(f_buf);
1836
508d9360
OZ
1837 return interpret(expr);
1838}
0d1b3c4c 1839
52e030e1 1840uint
508d9360
OZ
1841f_eval_int(struct f_inst *expr)
1842{
1843 /* Called independently in parse-time to eval expressions */
1844 struct f_val res = f_eval(expr, cfg_mem);
0d1b3c4c 1845
b1c9d871
MM
1846 if (res.type != T_INT)
1847 cf_error("Integer expression expected");
508d9360 1848
b1c9d871
MM
1849 return res.val.i;
1850}
1c20608e 1851
ff95080f
PM
1852/**
1853 * filter_same - compare two filters
1854 * @new: first filter to be compared
1855 * @old: second filter to be compared, notice that this filter is
1856 * damaged while comparing.
1857 *
1858 * Returns 1 in case filters are same, otherwise 0. If there are
1859 * underlying bugs, it will rather say 0 on same filters than say
1860 * 1 on different.
1861 */
30a6108c
MM
1862int
1863filter_same(struct filter *new, struct filter *old)
1864{
81ce667b
MM
1865 if (old == new) /* Handle FILTER_ACCEPT and FILTER_REJECT */
1866 return 1;
1867 if (old == FILTER_ACCEPT || old == FILTER_REJECT ||
1868 new == FILTER_ACCEPT || new == FILTER_REJECT)
1869 return 0;
9a4037d4 1870 return i_same(new->root, old->root);
30a6108c 1871}