]>
Commit | Line | Data |
---|---|---|
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 | 53 | static struct adata * |
ad9074e9 PM |
54 | adata_empty(struct linpool *pool) |
55 | { | |
56 | struct adata *res = lp_alloc(pool, sizeof(struct adata)); | |
57 | res->length = 0; | |
58 | return res; | |
59 | } | |
60 | ||
b0c9c21c | 61 | static int |
2e5a8735 PM |
62 | pm_path_compare(struct f_path_mask *m1, struct f_path_mask *m2) |
63 | { | |
64 | while (1) { | |
65 | if ((!m1) || (!m2)) | |
66 | return !((!m1) && (!m2)); | |
43de796b | 67 | |
126683fe | 68 | /* FIXME: buggy, should return -1, 0, 1; but it doesn't matter */ |
43de796b | 69 | if ((m1->kind != m2->kind) || (m1->val != m2->val)) return 1; |
2e5a8735 PM |
70 | m1 = m1->next; |
71 | m2 = m2->next; | |
72 | } | |
73 | } | |
74 | ||
92a72a4c OZ |
75 | u32 f_eval_asn(struct f_inst *expr); |
76 | ||
11cb6202 OZ |
77 | static void |
78 | pm_format(struct f_path_mask *p, byte *buf, unsigned int size) | |
79 | { | |
80 | byte *end = buf + size - 16; | |
81 | ||
82 | while (p) | |
83 | { | |
84 | if (buf > end) | |
85 | { | |
86 | strcpy(buf, " ..."); | |
87 | return; | |
88 | } | |
89 | ||
92a72a4c OZ |
90 | switch(p->kind) |
91 | { | |
92 | case PM_ASN: | |
93 | buf += bsprintf(buf, " %u", p->val); | |
94 | break; | |
95 | ||
96 | case PM_QUESTION: | |
97 | buf += bsprintf(buf, " ?"); | |
98 | break; | |
99 | ||
100 | case PM_ASTERISK: | |
101 | buf += bsprintf(buf, " *"); | |
102 | break; | |
103 | ||
104 | case PM_ASN_EXPR: | |
105 | buf += bsprintf(buf, " %u", f_eval_asn((struct f_inst *) p->val)); | |
106 | break; | |
107 | } | |
11cb6202 OZ |
108 | |
109 | p = p->next; | |
110 | } | |
111 | ||
112 | *buf = 0; | |
113 | } | |
114 | ||
126683fe OZ |
115 | static inline int int_cmp(int i1, int i2) |
116 | { | |
117 | if (i1 == i2) return 0; | |
118 | if (i1 < i2) return -1; | |
119 | else return 1; | |
120 | } | |
121 | ||
8dcf2544 | 122 | /** |
3e82b32d | 123 | * val_compare - compare two values |
8dcf2544 PM |
124 | * @v1: first value |
125 | * @v2: second value | |
126 | * | |
127 | * Compares two values and returns -1, 0, 1 on <, =, > or 999 on error. | |
128 | * Tree module relies on this giving consistent results so that it can | |
129 | * build balanced trees. | |
b093c328 | 130 | */ |
38506f71 PM |
131 | int |
132 | val_compare(struct f_val v1, struct f_val v2) | |
133 | { | |
d85e1f0e MM |
134 | int rc; |
135 | ||
41be4444 PM |
136 | if ((v1.type == T_VOID) && (v2.type == T_VOID)) |
137 | return 0; | |
138 | if (v1.type == T_VOID) /* Hack for else */ | |
139 | return -1; | |
140 | if (v2.type == T_VOID) | |
141 | return 1; | |
142 | ||
f71bded6 | 143 | if (v1.type != v2.type) { |
126683fe OZ |
144 | #ifndef IPV6 |
145 | /* IP->Quad implicit conversion */ | |
146 | if ((v1.type == T_QUAD) && (v2.type == T_IP)) | |
147 | return int_cmp(v1.val.i, ipa_to_u32(v2.val.px.ip)); | |
148 | if ((v1.type == T_IP) && (v2.type == T_QUAD)) | |
149 | return int_cmp(ipa_to_u32(v1.val.px.ip), v2.val.i); | |
150 | #endif | |
151 | ||
f71bded6 | 152 | debug( "Types do not match in val_compare\n" ); |
7db7b7db | 153 | return CMP_ERROR; |
f71bded6 | 154 | } |
38506f71 | 155 | switch (v1.type) { |
f4536657 | 156 | case T_ENUM: |
a6c9f064 OF |
157 | case T_INT: |
158 | case T_BOOL: | |
d3dd620b | 159 | case T_PAIR: |
126683fe OZ |
160 | case T_QUAD: |
161 | return int_cmp(v1.val.i, v2.val.i); | |
43fc099b | 162 | case T_IP: |
6dc7a0cb | 163 | return ipa_compare(v1.val.px.ip, v2.val.px.ip); |
d85e1f0e MM |
164 | case T_PREFIX: |
165 | if (rc = ipa_compare(v1.val.px.ip, v2.val.px.ip)) | |
166 | return rc; | |
167 | if (v1.val.px.len < v2.val.px.len) | |
168 | return -1; | |
169 | if (v1.val.px.len > v2.val.px.len) | |
170 | return 1; | |
171 | return 0; | |
2e5a8735 PM |
172 | case T_PATH_MASK: |
173 | return pm_path_compare(v1.val.path_mask, v2.val.path_mask); | |
e29fa06e OZ |
174 | case T_STRING: |
175 | return strcmp(v1.val.s, v2.val.s); | |
3076b5ae | 176 | default: |
d59405ec | 177 | debug( "Compare of unknown entities: %x\n", v1.type ); |
3076b5ae | 178 | return CMP_ERROR; |
38506f71 PM |
179 | } |
180 | } | |
181 | ||
dfd48621 OZ |
182 | int |
183 | tree_compare(const void *p1, const void *p2) | |
184 | { | |
185 | return val_compare((* (struct f_tree **) p1)->from, (* (struct f_tree **) p2)->from); | |
186 | } | |
b1a597e0 OZ |
187 | |
188 | void | |
7f0d245a | 189 | fprefix_get_bounds(struct f_prefix *px, int *l, int *h) |
b1a597e0 OZ |
190 | { |
191 | *l = *h = px->len & LEN_MASK; | |
192 | ||
193 | if (px->len & LEN_MINUS) | |
194 | *l = 0; | |
195 | ||
196 | else if (px->len & LEN_PLUS) | |
197 | *h = MAX_PREFIX_LENGTH; | |
198 | ||
199 | else if (px->len & LEN_RANGE) | |
200 | { | |
201 | *l = 0xff & (px->len >> 16); | |
202 | *h = 0xff & (px->len >> 8); | |
203 | } | |
204 | } | |
205 | ||
4c5f93d7 | 206 | /* |
b093c328 PM |
207 | * val_simple_in_range - check if @v1 ~ @v2 for everything except sets |
208 | */ | |
9831e591 | 209 | static int |
6dc7a0cb PM |
210 | val_simple_in_range(struct f_val v1, struct f_val v2) |
211 | { | |
10a53608 | 212 | if ((v1.type == T_PATH) && (v2.type == T_PATH_MASK)) |
2a40efa5 | 213 | return as_path_match(v1.val.ad, v2.val.path_mask); |
126683fe | 214 | if (((v1.type == T_PAIR) || (v1.type == T_QUAD)) && (v2.type == T_CLIST)) |
2bd2de01 | 215 | return int_set_contains(v2.val.ad, v1.val.i); |
126683fe OZ |
216 | #ifndef IPV6 |
217 | /* IP->Quad implicit conversion */ | |
218 | if ((v1.type == T_IP) && (v2.type == T_CLIST)) | |
219 | return int_set_contains(v2.val.ad, ipa_to_u32(v1.val.px.ip)); | |
220 | #endif | |
e29fa06e OZ |
221 | if ((v1.type == T_STRING) && (v2.type == T_STRING)) |
222 | return patmatch(v2.val.s, v1.val.s); | |
10a53608 | 223 | |
6dc7a0cb | 224 | if ((v1.type == T_IP) && (v2.type == T_PREFIX)) |
23e563d8 | 225 | return ipa_in_net(v1.val.px.ip, v2.val.px.ip, v2.val.px.len); |
6dc7a0cb | 226 | |
23e563d8 OZ |
227 | if ((v1.type == T_PREFIX) && (v2.type == T_PREFIX)) |
228 | return ipa_in_net(v1.val.px.ip, v2.val.px.ip, v2.val.px.len) && (v1.val.px.len >= v2.val.px.len); | |
b1a597e0 | 229 | |
6dc7a0cb PM |
230 | return CMP_ERROR; |
231 | } | |
232 | ||
ba5c0057 OZ |
233 | static int |
234 | clist_set_type(struct f_tree *set, struct f_val *v) | |
235 | { | |
236 | switch (set->from.type) { | |
237 | case T_PAIR: | |
238 | v->type = T_PAIR; | |
239 | return 1; | |
240 | case T_QUAD: | |
241 | #ifndef IPV6 | |
242 | case T_IP: | |
243 | #endif | |
244 | v->type = T_QUAD; | |
245 | return 1; | |
246 | break; | |
247 | default: | |
248 | v->type = T_VOID; | |
249 | return 0; | |
250 | } | |
251 | } | |
252 | ||
253 | static int | |
254 | clist_match_set(struct adata *clist, struct f_tree *set) | |
255 | { | |
256 | if (!clist) | |
257 | return 0; | |
258 | ||
259 | struct f_val v; | |
260 | if (!clist_set_type(set, &v)) | |
261 | return CMP_ERROR; | |
262 | ||
263 | u32 *l = (u32 *) clist->data; | |
264 | u32 *end = l + clist->length/4; | |
265 | while (l < end) { | |
266 | v.val.i = *l++; | |
267 | if (find_tree(set, v)) | |
268 | return 1; | |
269 | } | |
270 | return 0; | |
271 | } | |
272 | ||
273 | static struct adata * | |
274 | clist_del_matching(struct linpool *pool, struct adata *clist, struct f_tree *set) | |
275 | { | |
276 | if (!clist) | |
277 | return NULL; | |
278 | ||
279 | struct f_val v; | |
280 | clist_set_type(set, &v); | |
281 | ||
282 | u32 tmp[clist->length/4]; | |
283 | u32 *l = (u32 *) clist->data; | |
284 | u32 *k = tmp; | |
285 | u32 *end = l + clist->length/4; | |
286 | ||
287 | while (l < end) { | |
288 | v.val.i = *l++; | |
289 | if (!find_tree(set, v)) | |
290 | *k++ = v.val.i; | |
291 | } | |
292 | ||
293 | int nl = (k - tmp) * 4; | |
294 | if (nl == clist->length) | |
295 | return clist; | |
296 | ||
297 | struct adata *res = lp_alloc(pool, sizeof(struct adata) + nl); | |
298 | res->length = nl; | |
299 | memcpy(res->data, tmp, nl); | |
300 | return res; | |
301 | } | |
302 | ||
8dcf2544 | 303 | /** |
3e82b32d | 304 | * val_in_range - implement |~| operator |
8dcf2544 PM |
305 | * @v1: element |
306 | * @v2: set | |
307 | * | |
308 | * Checks if @v1 is element (|~| operator) of @v2. Sets are internally represented as balanced trees, see | |
3e82b32d | 309 | * |tree.c| module (this is not limited to sets, but for non-set cases, val_simple_in_range() is called early). |
b093c328 | 310 | */ |
9831e591 | 311 | static int |
7db7b7db PM |
312 | val_in_range(struct f_val v1, struct f_val v2) |
313 | { | |
6dc7a0cb PM |
314 | int res; |
315 | ||
316 | res = val_simple_in_range(v1, v2); | |
317 | ||
318 | if (res != CMP_ERROR) | |
319 | return res; | |
1895e81e | 320 | |
b1a597e0 | 321 | if ((v1.type == T_PREFIX) && (v2.type == T_PREFIX_SET)) |
7f0d245a | 322 | return trie_match_fprefix(v2.val.ti, &v1.val.px); |
b1a597e0 | 323 | |
ba5c0057 OZ |
324 | if ((v1.type == T_CLIST) && (v2.type == T_SET)) |
325 | return clist_match_set(v1.val.ad, v2.val.t); | |
326 | ||
1895e81e PM |
327 | if (v2.type == T_SET) |
328 | switch (v1.type) { | |
329 | case T_ENUM: | |
330 | case T_INT: | |
126683fe OZ |
331 | case T_PAIR: |
332 | case T_QUAD: | |
1895e81e | 333 | case T_IP: |
1895e81e PM |
334 | { |
335 | struct f_tree *n; | |
336 | n = find_tree(v2.val.t, v1); | |
337 | if (!n) | |
338 | return 0; | |
339 | return !! (val_simple_in_range(v1, n->from)); /* We turn CMP_ERROR into compared ok, and that's fine */ | |
340 | } | |
341 | } | |
7db7b7db PM |
342 | return CMP_ERROR; |
343 | } | |
344 | ||
38506f71 PM |
345 | static void |
346 | tree_print(struct f_tree *t) | |
347 | { | |
348 | if (!t) { | |
3cf4a2e2 | 349 | debug( "() " ); |
38506f71 PM |
350 | return; |
351 | } | |
3cf4a2e2 | 352 | debug( "[ " ); |
38506f71 | 353 | tree_print( t->left ); |
3cf4a2e2 | 354 | debug( ", " ); val_print( t->from ); debug( ".." ); val_print( t->to ); debug( ", " ); |
38506f71 | 355 | tree_print( t->right ); |
3cf4a2e2 | 356 | debug( "] " ); |
38506f71 PM |
357 | } |
358 | ||
4c5f93d7 | 359 | /* |
b093c328 PM |
360 | * val_print - format filter value |
361 | */ | |
38506f71 PM |
362 | void |
363 | val_print(struct f_val v) | |
364 | { | |
365 | char buf[2048]; | |
ecd25633 | 366 | char buf2[1024]; |
38506f71 PM |
367 | #define PRINTF(a...) bsnprintf( buf, 2040, a ) |
368 | buf[0] = 0; | |
369 | switch (v.type) { | |
370 | case T_VOID: PRINTF( "(void)" ); break; | |
371 | case T_BOOL: PRINTF( v.val.i ? "TRUE" : "FALSE" ); break; | |
372 | case T_INT: PRINTF( "%d ", v.val.i ); break; | |
373 | case T_STRING: PRINTF( "%s", v.val.s ); break; | |
6dc7a0cb | 374 | case T_IP: PRINTF( "%I", v.val.px.ip ); break; |
720d911d PM |
375 | case T_PREFIX: PRINTF( "%I/%d", v.val.px.ip, v.val.px.len ); break; |
376 | case T_PAIR: PRINTF( "(%d,%d)", v.val.i >> 16, v.val.i & 0xffff ); break; | |
126683fe | 377 | case T_QUAD: PRINTF( "%R", v.val.i ); break; |
b1a597e0 | 378 | case T_PREFIX_SET: trie_print(v.val.ti, buf, 2040); break; |
38506f71 | 379 | case T_SET: tree_print( v.val.t ); PRINTF( "\n" ); break; |
346a12c2 | 380 | case T_ENUM: PRINTF( "(enum %x)%d", v.type, v.val.i ); break; |
ecd25633 | 381 | case T_PATH: as_path_format(v.val.ad, buf2, 1020); PRINTF( "(path %s)", buf2 ); break; |
aebe06b4 | 382 | case T_CLIST: int_set_format(v.val.ad, 1, buf2, 1020); PRINTF( "(clist %s)", buf2 ); break; |
cf186034 | 383 | case T_PATH_MASK: pm_format(v.val.path_mask, buf2, 1020); PRINTF( "(pathmask%s)", buf2 ); break; |
38506f71 | 384 | default: PRINTF( "[unknown type %x]", v.type ); |
7f77e250 | 385 | #undef PRINTF |
38506f71 | 386 | } |
3cf4a2e2 | 387 | debug( buf ); |
38506f71 PM |
388 | } |
389 | ||
48f9e019 | 390 | static struct rte **f_rte, *f_rte_old; |
f31156ca | 391 | static struct linpool *f_pool; |
31e79264 | 392 | static struct ea_list **f_tmp_attrs; |
0a06a9b8 | 393 | static int f_flags; |
36bbfc70 | 394 | |
4c5f93d7 | 395 | /* |
b093c328 PM |
396 | * rta_cow - prepare rta for modification by filter |
397 | */ | |
9831e591 | 398 | static void |
26c09e1d PM |
399 | rta_cow(void) |
400 | { | |
db96fccb OZ |
401 | if ((*f_rte)->attrs->aflags & RTAF_CACHED) { |
402 | rta *f_rta_copy = lp_alloc(f_pool, sizeof(rta)); | |
26c09e1d PM |
403 | memcpy(f_rta_copy, (*f_rte)->attrs, sizeof(rta)); |
404 | f_rta_copy->aflags = 0; | |
405 | *f_rte = rte_cow(*f_rte); | |
f7667ba1 | 406 | rta_free((*f_rte)->attrs); |
26c09e1d PM |
407 | (*f_rte)->attrs = f_rta_copy; |
408 | } | |
409 | } | |
410 | ||
cb530392 OZ |
411 | static struct rate_limit rl_runtime_err; |
412 | ||
9a4037d4 | 413 | #define runtime(x) do { \ |
cb530392 | 414 | log_rl(&rl_runtime_err, L_ERR "filters, line %d: %s", what->lineno, x); \ |
9a4037d4 PM |
415 | res.type = T_RETURN; \ |
416 | res.val.i = F_ERROR; \ | |
417 | return res; \ | |
418 | } while(0) | |
419 | ||
420 | #define ARG(x,y) \ | |
421 | x = interpret(what->y); \ | |
2d496d20 | 422 | if (x.type & T_RETURN) \ |
9a4037d4 PM |
423 | return x; |
424 | ||
425 | #define ONEARG ARG(v1, a1.p) | |
426 | #define TWOARGS ARG(v1, a1.p) \ | |
427 | ARG(v2, a2.p) | |
428 | #define TWOARGS_C TWOARGS \ | |
429 | if (v1.type != v2.type) \ | |
b178d92a | 430 | runtime( "Can't operate with values of incompatible types" ); |
7db7b7db | 431 | |
b093c328 PM |
432 | /** |
433 | * interpret | |
2e9b2421 | 434 | * @what: filter to interpret |
b093c328 | 435 | * |
4c5f93d7 | 436 | * Interpret given tree of filter instructions. This is core function |
b093c328 | 437 | * of filter system and does all the hard work. |
771ae456 PM |
438 | * |
439 | * Each instruction has 4 fields: code (which is instruction code), | |
440 | * aux (which is extension to instruction code, typically type), | |
441 | * arg1 and arg2 - arguments. Depending on instruction, arguments | |
442 | * are either integers, or pointers to instruction trees. Common | |
443 | * instructions like +, that have two expressions as arguments use | |
444 | * TWOARGS macro to get both of them evaluated. | |
445 | * | |
446 | * &f_val structures are copied around, so there are no problems with | |
447 | * memory managment. | |
b093c328 | 448 | */ |
23b1539b PM |
449 | static struct f_val |
450 | interpret(struct f_inst *what) | |
451 | { | |
452 | struct symbol *sym; | |
126683fe | 453 | struct f_val v1, v2, res, *vp; |
92a72a4c | 454 | unsigned u1, u2; |
6a57bb31 | 455 | int i; |
7ea5b00f | 456 | u32 as; |
23b1539b PM |
457 | |
458 | res.type = T_VOID; | |
459 | if (!what) | |
460 | return res; | |
461 | ||
462 | switch(what->code) { | |
463 | case ',': | |
464 | TWOARGS; | |
465 | break; | |
466 | ||
467 | /* Binary operators */ | |
468 | case '+': | |
469 | TWOARGS_C; | |
470 | switch (res.type = v1.type) { | |
b178d92a | 471 | case T_VOID: runtime( "Can't operate with values of type void" ); |
23b1539b PM |
472 | case T_INT: res.val.i = v1.val.i + v2.val.i; break; |
473 | default: runtime( "Usage of unknown type" ); | |
474 | } | |
475 | break; | |
9f0d45d6 PM |
476 | case '-': |
477 | TWOARGS_C; | |
478 | switch (res.type = v1.type) { | |
b178d92a | 479 | case T_VOID: runtime( "Can't operate with values of type void" ); |
9f0d45d6 PM |
480 | case T_INT: res.val.i = v1.val.i - v2.val.i; break; |
481 | default: runtime( "Usage of unknown type" ); | |
482 | } | |
483 | break; | |
484 | case '*': | |
485 | TWOARGS_C; | |
486 | switch (res.type = v1.type) { | |
b178d92a | 487 | case T_VOID: runtime( "Can't operate with values of type void" ); |
9f0d45d6 PM |
488 | case T_INT: res.val.i = v1.val.i * v2.val.i; break; |
489 | default: runtime( "Usage of unknown type" ); | |
490 | } | |
491 | break; | |
23b1539b PM |
492 | case '/': |
493 | TWOARGS_C; | |
494 | switch (res.type = v1.type) { | |
b178d92a | 495 | case T_VOID: runtime( "Can't operate with values of type void" ); |
64ba9f7b PM |
496 | case T_INT: if (v2.val.i == 0) runtime( "Mother told me not to divide by 0" ); |
497 | res.val.i = v1.val.i / v2.val.i; break; | |
23b1539b | 498 | case T_IP: if (v2.type != T_INT) |
b178d92a | 499 | runtime( "Incompatible types in / operator" ); |
23b1539b PM |
500 | break; |
501 | default: runtime( "Usage of unknown type" ); | |
502 | } | |
503 | break; | |
5f4aee76 PM |
504 | |
505 | case '&': | |
506 | TWOARGS_C; | |
507 | res.type = v1.type; | |
b178d92a | 508 | if (res.type != T_BOOL) runtime( "Can't do boolean operation on non-booleans" ); |
5f4aee76 PM |
509 | res.val.i = v1.val.i && v2.val.i; |
510 | break; | |
511 | case '|': | |
512 | TWOARGS_C; | |
513 | res.type = v1.type; | |
b178d92a | 514 | if (res.type != T_BOOL) runtime( "Can't do boolean operation on non-booleans" ); |
5f4aee76 PM |
515 | res.val.i = v1.val.i || v2.val.i; |
516 | break; | |
23b1539b | 517 | |
92a72a4c OZ |
518 | case P('m','p'): |
519 | TWOARGS_C; | |
520 | if ((v1.type != T_INT) || (v2.type != T_INT)) | |
521 | runtime( "Can't operate with value of non-integer type in pair constructor" ); | |
522 | u1 = v1.val.i; | |
523 | u2 = v2.val.i; | |
524 | if ((u1 > 0xFFFF) || (u2 > 0xFFFF)) | |
525 | runtime( "Can't operate with value out of bounds in pair constructor" ); | |
526 | res.val.i = (u1 << 16) | u2; | |
527 | res.type = T_PAIR; | |
528 | break; | |
529 | ||
23b1539b | 530 | /* Relational operators */ |
38506f71 PM |
531 | |
532 | #define COMPARE(x) \ | |
126683fe | 533 | TWOARGS; \ |
38506f71 PM |
534 | i = val_compare(v1, v2); \ |
535 | if (i==CMP_ERROR) \ | |
126683fe OZ |
536 | runtime( "Can't compare values of incompatible types" ); \ |
537 | res.type = T_BOOL; \ | |
38506f71 | 538 | res.val.i = (x); \ |
23b1539b | 539 | break; |
38506f71 | 540 | |
2d496d20 PM |
541 | case P('!','='): COMPARE(i!=0); |
542 | case P('=','='): COMPARE(i==0); | |
38506f71 | 543 | case '<': COMPARE(i==-1); |
2d496d20 | 544 | case P('<','='): COMPARE(i!=1); |
38506f71 | 545 | |
995e5894 PM |
546 | case '!': |
547 | ONEARG; | |
548 | if (v1.type != T_BOOL) | |
b178d92a | 549 | runtime( "Not applied to non-boolean" ); |
995e5894 PM |
550 | res = v1; |
551 | res.val.i = !res.val.i; | |
552 | break; | |
553 | ||
38506f71 PM |
554 | case '~': |
555 | TWOARGS; | |
23b1539b | 556 | res.type = T_BOOL; |
7db7b7db PM |
557 | res.val.i = val_in_range(v1, v2); |
558 | if (res.val.i == CMP_ERROR) | |
559 | runtime( "~ applied on unknown type pair" ); | |
23b1539b | 560 | break; |
2d496d20 | 561 | case P('d','e'): |
f4536657 PM |
562 | ONEARG; |
563 | res.type = T_BOOL; | |
564 | res.val.i = (v1.type != T_VOID); | |
565 | break; | |
23b1539b | 566 | |
d3dd620b | 567 | /* Set to indirect value, a1 = variable, a2 = value */ |
23b1539b | 568 | case 's': |
2db3b288 PM |
569 | ARG(v2, a2.p); |
570 | sym = what->a1.p; | |
126683fe OZ |
571 | vp = sym->def; |
572 | if ((sym->class != (SYM_VARIABLE | v2.type)) && (v2.type != T_VOID)) { | |
573 | #ifndef IPV6 | |
574 | /* IP->Quad implicit conversion */ | |
575 | if ((sym->class == (SYM_VARIABLE | T_QUAD)) && (v2.type == T_IP)) { | |
576 | vp->type = T_QUAD; | |
577 | vp->val.i = ipa_to_u32(v2.val.px.ip); | |
578 | break; | |
579 | } | |
580 | #endif | |
aa461248 | 581 | runtime( "Assigning to variable of incompatible type" ); |
126683fe OZ |
582 | } |
583 | *vp = v2; | |
23b1539b PM |
584 | break; |
585 | ||
083c43e2 | 586 | /* some constants have value in a2, some in *a1.p, strange. */ |
b1a597e0 | 587 | case 'c': /* integer (or simple type) constant, string, set, or prefix_set */ |
c7b43f33 | 588 | res.type = what->aux; |
083c43e2 | 589 | |
b1a597e0 OZ |
590 | if (res.type == T_PREFIX_SET) |
591 | res.val.ti = what->a2.p; | |
592 | else if (res.type == T_SET) | |
083c43e2 OZ |
593 | res.val.t = what->a2.p; |
594 | else if (res.type == T_STRING) | |
595 | res.val.s = what->a2.p; | |
596 | else | |
597 | res.val.i = what->a2.i; | |
23b1539b | 598 | break; |
9be1086d | 599 | case 'V': |
38506f71 PM |
600 | case 'C': |
601 | res = * ((struct f_val *) what->a1.p); | |
602 | break; | |
23b1539b PM |
603 | case 'p': |
604 | ONEARG; | |
38506f71 | 605 | val_print(v1); |
23b1539b PM |
606 | break; |
607 | case '?': /* ? has really strange error value, so we can implement if ... else nicely :-) */ | |
608 | ONEARG; | |
609 | if (v1.type != T_BOOL) | |
98da26a0 | 610 | runtime( "If requires boolean expression" ); |
23b1539b | 611 | if (v1.val.i) { |
2db3b288 | 612 | ARG(res,a2.p); |
23b1539b PM |
613 | res.val.i = 0; |
614 | } else res.val.i = 1; | |
615 | res.type = T_BOOL; | |
616 | break; | |
617 | case '0': | |
3cf4a2e2 | 618 | debug( "No operation\n" ); |
23b1539b | 619 | break; |
2d496d20 | 620 | case P('p',','): |
23b1539b | 621 | ONEARG; |
798df5b1 | 622 | if (what->a2.i == F_NOP || (what->a2.i != F_NONL && what->a1.p)) |
3cf4a2e2 | 623 | debug( "\n" ); |
23b1539b | 624 | |
2db3b288 | 625 | switch (what->a2.i) { |
23b1539b PM |
626 | case F_QUITBIRD: |
627 | die( "Filter asked me to die" ); | |
628 | case F_ACCEPT: | |
629 | /* Should take care about turning ACCEPT into MODIFY */ | |
630 | case F_ERROR: | |
2ad6dcdb | 631 | case F_REJECT: /* FIXME (noncritical) Should print complete route along with reason to reject route */ |
23b1539b | 632 | res.type = T_RETURN; |
2ad6dcdb | 633 | res.val.i = what->a2.i; |
7e1f9971 | 634 | return res; /* We have to return now, no more processing. */ |
d3dd620b | 635 | case F_NONL: |
23b1539b PM |
636 | case F_NOP: |
637 | break; | |
638 | default: | |
b178d92a | 639 | bug( "unknown return type: Can't happen"); |
23b1539b PM |
640 | } |
641 | break; | |
36bbfc70 PM |
642 | case 'a': /* rta access */ |
643 | { | |
644 | struct rta *rta = (*f_rte)->attrs; | |
c7b43f33 | 645 | res.type = what->aux; |
36bbfc70 PM |
646 | switch(res.type) { |
647 | case T_IP: | |
6dc7a0cb | 648 | res.val.px.ip = * (ip_addr *) ((char *) rta + what->a2.i); |
36bbfc70 | 649 | break; |
c7b43f33 PM |
650 | case T_ENUM: |
651 | res.val.i = * ((char *) rta + what->a2.i); | |
652 | break; | |
e29fa06e OZ |
653 | case T_STRING: /* Warning: this is a special case for proto attribute */ |
654 | res.val.s = rta->proto->name; | |
655 | break; | |
36bbfc70 PM |
656 | case T_PREFIX: /* Warning: this works only for prefix of network */ |
657 | { | |
658 | res.val.px.ip = (*f_rte)->net->n.prefix; | |
659 | res.val.px.len = (*f_rte)->net->n.pxlen; | |
660 | break; | |
661 | } | |
662 | default: | |
3076b5ae | 663 | bug( "Invalid type for rta access (%x)", res.type ); |
36bbfc70 PM |
664 | } |
665 | } | |
666 | break; | |
0dc4431c PM |
667 | case P('a','S'): |
668 | ONEARG; | |
669 | if (what->aux != v1.type) | |
98da26a0 | 670 | runtime( "Attempt to set static attribute to incompatible type" ); |
0dc4431c PM |
671 | rta_cow(); |
672 | { | |
673 | struct rta *rta = (*f_rte)->attrs; | |
674 | switch (what->aux) { | |
675 | case T_ENUM: | |
676 | * ((char *) rta + what->a2.i) = v1.val.i; | |
677 | break; | |
678 | case T_IP: | |
679 | * (ip_addr *) ((char *) rta + what->a2.i) = v1.val.px.ip; | |
680 | break; | |
681 | default: | |
682 | bug( "Unknown type in set of static attribute" ); | |
683 | } | |
684 | } | |
685 | break; | |
2d496d20 | 686 | case P('e','a'): /* Access to extended attributes */ |
91447965 | 687 | { |
0a06a9b8 | 688 | eattr *e = NULL; |
3076b5ae | 689 | if (!(f_flags & FF_FORCE_TMPATTR)) |
0a06a9b8 | 690 | e = ea_find( (*f_rte)->attrs->eattrs, what->a2.i ); |
31e79264 PM |
691 | if (!e) |
692 | e = ea_find( (*f_tmp_attrs), what->a2.i ); | |
3076b5ae | 693 | if ((!e) && (f_flags & FF_FORCE_TMPATTR)) |
0a06a9b8 | 694 | e = ea_find( (*f_rte)->attrs->eattrs, what->a2.i ); |
e8da1bd0 OZ |
695 | |
696 | if (!e) { | |
0277cc0b OZ |
697 | /* A special case: undefined int_set looks like empty int_set */ |
698 | if ((what->aux & EAF_TYPE_MASK) == EAF_TYPE_INT_SET) { | |
699 | res.type = T_CLIST; | |
700 | res.val.ad = adata_empty(f_pool); | |
701 | break; | |
702 | } | |
e8da1bd0 OZ |
703 | /* Undefined value */ |
704 | res.type = T_VOID; | |
705 | break; | |
706 | } | |
707 | ||
708 | switch (what->aux & EAF_TYPE_MASK) { | |
709 | case EAF_TYPE_INT: | |
0150e521 | 710 | res.type = T_INT; |
91447965 PM |
711 | res.val.i = e->u.data; |
712 | break; | |
126683fe OZ |
713 | case EAF_TYPE_ROUTER_ID: |
714 | res.type = T_QUAD; | |
715 | res.val.i = e->u.data; | |
716 | break; | |
e8da1bd0 OZ |
717 | case EAF_TYPE_OPAQUE: |
718 | res.type = T_ENUM_EMPTY; | |
719 | res.val.i = 0; | |
720 | break; | |
330aecea | 721 | case EAF_TYPE_IP_ADDRESS: |
330aecea OZ |
722 | res.type = T_IP; |
723 | struct adata * ad = e->u.ptr; | |
724 | res.val.px.ip = * (ip_addr *) ad->data; | |
725 | break; | |
0150e521 PM |
726 | case EAF_TYPE_AS_PATH: |
727 | res.type = T_PATH; | |
728 | res.val.ad = e->u.ptr; | |
729 | break; | |
730 | case EAF_TYPE_INT_SET: | |
731 | res.type = T_CLIST; | |
10a53608 PM |
732 | res.val.ad = e->u.ptr; |
733 | break; | |
e8da1bd0 OZ |
734 | case EAF_TYPE_UNDEF: |
735 | res.type = T_VOID; | |
736 | break; | |
2803c9dd | 737 | default: |
ad9074e9 | 738 | bug("Unknown type in e,a"); |
91447965 PM |
739 | } |
740 | } | |
6dc7a0cb | 741 | break; |
2d496d20 | 742 | case P('e','S'): |
f31156ca | 743 | ONEARG; |
f31156ca PM |
744 | { |
745 | struct ea_list *l = lp_alloc(f_pool, sizeof(struct ea_list) + sizeof(eattr)); | |
746 | ||
747 | l->next = NULL; | |
748 | l->flags = EALF_SORTED; | |
749 | l->count = 1; | |
750 | l->attrs[0].id = what->a2.i; | |
913ce95b PM |
751 | l->attrs[0].flags = 0; |
752 | l->attrs[0].type = what->aux | EAF_ORIGINATED; | |
31e79264 PM |
753 | switch (what->aux & EAF_TYPE_MASK) { |
754 | case EAF_TYPE_INT: | |
e8da1bd0 | 755 | case EAF_TYPE_ROUTER_ID: |
31e79264 PM |
756 | if (v1.type != T_INT) |
757 | runtime( "Setting int attribute to non-int value" ); | |
f31156ca PM |
758 | l->attrs[0].u.data = v1.val.i; |
759 | break; | |
e8da1bd0 OZ |
760 | case EAF_TYPE_OPAQUE: |
761 | runtime( "Setting opaque attribute is not allowed" ); | |
762 | break; | |
330aecea OZ |
763 | case EAF_TYPE_IP_ADDRESS: |
764 | if (v1.type != T_IP) | |
765 | runtime( "Setting ip attribute to non-ip value" ); | |
766 | int len = sizeof(ip_addr); | |
767 | struct adata *ad = lp_alloc(f_pool, sizeof(struct adata) + len); | |
768 | ad->length = len; | |
769 | (* (ip_addr *) ad->data) = v1.val.px.ip; | |
54fe0d92 | 770 | l->attrs[0].u.ptr = ad; |
330aecea | 771 | break; |
10a53608 PM |
772 | case EAF_TYPE_AS_PATH: |
773 | if (v1.type != T_PATH) | |
774 | runtime( "Setting path attribute to non-path value" ); | |
775 | l->attrs[0].u.ptr = v1.val.ad; | |
776 | break; | |
708711c3 PM |
777 | case EAF_TYPE_INT_SET: |
778 | if (v1.type != T_CLIST) | |
b178d92a | 779 | runtime( "Setting int set attribute to non-clist value" ); |
708711c3 PM |
780 | l->attrs[0].u.ptr = v1.val.ad; |
781 | break; | |
31e79264 PM |
782 | case EAF_TYPE_UNDEF: |
783 | if (v1.type != T_VOID) | |
784 | runtime( "Setting void attribute to non-void value" ); | |
48f9e019 PM |
785 | l->attrs[0].u.data = 0; |
786 | break; | |
0150e521 | 787 | default: bug("Unknown type in e,S"); |
f31156ca | 788 | } |
31e79264 | 789 | |
3076b5ae | 790 | if (!(what->aux & EAF_TEMP) && (!(f_flags & FF_FORCE_TMPATTR))) { |
26c09e1d | 791 | rta_cow(); |
db96fccb OZ |
792 | l->next = (*f_rte)->attrs->eattrs; |
793 | (*f_rte)->attrs->eattrs = l; | |
31e79264 PM |
794 | } else { |
795 | l->next = (*f_tmp_attrs); | |
796 | (*f_tmp_attrs) = l; | |
797 | } | |
f31156ca | 798 | } |
f31156ca | 799 | break; |
0dc4431c PM |
800 | case 'P': |
801 | res.type = T_INT; | |
802 | res.val.i = (*f_rte)->pref; | |
803 | break; | |
804 | case P('P','S'): | |
805 | ONEARG; | |
806 | if (v1.type != T_INT) | |
b178d92a | 807 | runtime( "Can't set preference to non-integer" ); |
f4c6ca8c OZ |
808 | if ((v1.val.i < 0) || (v1.val.i > 0xFFFF)) |
809 | runtime( "Setting preference value out of bounds" ); | |
0dc4431c PM |
810 | *f_rte = rte_cow(*f_rte); |
811 | (*f_rte)->pref = v1.val.i; | |
812 | break; | |
684c6f5a PM |
813 | case 'L': /* Get length of */ |
814 | ONEARG; | |
815 | res.type = T_INT; | |
816 | switch(v1.type) { | |
817 | case T_PREFIX: res.val.i = v1.val.px.len; break; | |
818 | case T_PATH: res.val.i = as_path_getlen(v1.val.ad); break; | |
82ba9032 | 819 | default: runtime( "Prefix or path expected" ); |
684c6f5a PM |
820 | } |
821 | break; | |
2d496d20 | 822 | case P('c','p'): /* Convert prefix to ... */ |
36bbfc70 PM |
823 | ONEARG; |
824 | if (v1.type != T_PREFIX) | |
b178d92a | 825 | runtime( "Prefix expected" ); |
c7b43f33 | 826 | res.type = what->aux; |
36bbfc70 | 827 | switch(res.type) { |
684c6f5a | 828 | /* case T_INT: res.val.i = v1.val.px.len; break; Not needed any more */ |
6dc7a0cb | 829 | case T_IP: res.val.px.ip = v1.val.px.ip; break; |
3076b5ae | 830 | default: bug( "Unknown prefix to conversion" ); |
36bbfc70 PM |
831 | } |
832 | break; | |
7ea5b00f OZ |
833 | case P('a','f'): /* Get first ASN from AS PATH */ |
834 | ONEARG; | |
835 | if (v1.type != T_PATH) | |
2eece54a | 836 | runtime( "AS path expected" ); |
7ea5b00f OZ |
837 | |
838 | as = 0; | |
52b9b2a1 | 839 | as_path_get_first(v1.val.ad, &as); |
7ea5b00f OZ |
840 | res.type = T_INT; |
841 | res.val.i = as; | |
842 | break; | |
843 | case P('a','l'): /* Get last ASN from AS PATH */ | |
844 | ONEARG; | |
845 | if (v1.type != T_PATH) | |
846 | runtime( "AS path expected" ); | |
847 | ||
848 | as = 0; | |
52b9b2a1 | 849 | as_path_get_last(v1.val.ad, &as); |
7ea5b00f OZ |
850 | res.type = T_INT; |
851 | res.val.i = as; | |
852 | break; | |
2d496d20 PM |
853 | case 'r': |
854 | ONEARG; | |
855 | res = v1; | |
856 | res.type |= T_RETURN; | |
44711e0c | 857 | return res; |
2d496d20 | 858 | case P('c','a'): /* CALL: this is special: if T_RETURN and returning some value, mask it out */ |
6542ece9 PM |
859 | ONEARG; |
860 | res = interpret(what->a2.p); | |
2d496d20 PM |
861 | if (res.type == T_RETURN) |
862 | return res; | |
863 | res.type &= ~T_RETURN; | |
6542ece9 | 864 | break; |
aa461248 OZ |
865 | case P('c','v'): /* Clear local variables */ |
866 | for (sym = what->a1.p; sym != NULL; sym = sym->aux2) | |
867 | ((struct f_val *) sym->def)->type = T_VOID; | |
868 | break; | |
2d496d20 | 869 | case P('S','W'): |
7db7b7db | 870 | ONEARG; |
41be4444 PM |
871 | { |
872 | struct f_tree *t = find_tree(what->a2.p, v1); | |
873 | if (!t) { | |
874 | v1.type = T_VOID; | |
875 | t = find_tree(what->a2.p, v1); | |
876 | if (!t) { | |
ad9074e9 | 877 | debug( "No else statement?\n"); |
41be4444 PM |
878 | break; |
879 | } | |
880 | } | |
1877dab2 | 881 | /* It is actually possible to have t->data NULL */ |
44711e0c OZ |
882 | |
883 | res = interpret(t->data); | |
884 | if (res.type & T_RETURN) | |
885 | return res; | |
41be4444 | 886 | } |
7db7b7db | 887 | break; |
2d496d20 | 888 | case P('i','M'): /* IP.MASK(val) */ |
f4536657 PM |
889 | TWOARGS; |
890 | if (v2.type != T_INT) | |
b178d92a | 891 | runtime( "Integer expected"); |
f4536657 | 892 | if (v1.type != T_IP) |
b178d92a | 893 | runtime( "You can mask only IP addresses" ); |
f4536657 PM |
894 | { |
895 | ip_addr mask = ipa_mkmask(v2.val.i); | |
896 | res.type = T_IP; | |
897 | res.val.px.ip = ipa_and(mask, v1.val.px.ip); | |
898 | } | |
d3dd620b | 899 | break; |
afc54517 PM |
900 | |
901 | case 'E': /* Create empty attribute */ | |
902 | res.type = what->aux; | |
903 | res.val.ad = adata_empty(f_pool); | |
904 | break; | |
905 | case P('A','p'): /* Path prepend */ | |
906 | TWOARGS; | |
907 | if (v1.type != T_PATH) | |
908 | runtime("Can't prepend to non-path"); | |
909 | if (v2.type != T_INT) | |
910 | runtime("Can't prepend non-integer"); | |
911 | ||
912 | res.type = T_PATH; | |
913 | res.val.ad = as_path_prepend(f_pool, v1.val.ad, v2.val.i); | |
914 | break; | |
915 | ||
9c400ec9 PM |
916 | case P('C','a'): /* Community list add or delete */ |
917 | TWOARGS; | |
0277cc0b | 918 | if (v1.type != T_CLIST) |
9c400ec9 | 919 | runtime("Can't add/delete to non-clist"); |
126683fe | 920 | |
ba5c0057 | 921 | struct f_val dummy; |
a58022a6 OZ |
922 | u16 op = what->aux; |
923 | i = 0; | |
924 | ||
126683fe OZ |
925 | if ((v2.type == T_PAIR) || (v2.type == T_QUAD)) |
926 | i = v2.val.i; | |
927 | #ifndef IPV6 | |
928 | /* IP->Quad implicit conversion */ | |
929 | else if (v2.type == T_IP) | |
930 | i = ipa_to_u32(v2.val.px.ip); | |
931 | #endif | |
a58022a6 OZ |
932 | else if ((v2.type == T_SET) && (op == 'd') && clist_set_type(v2.val.t, &dummy)) |
933 | op = 'D'; | |
126683fe | 934 | else |
9c400ec9 PM |
935 | runtime("Can't add/delete non-pair"); |
936 | ||
937 | res.type = T_CLIST; | |
a58022a6 | 938 | switch (op) { |
126683fe OZ |
939 | case 'a': res.val.ad = int_set_add(f_pool, v1.val.ad, i); break; |
940 | case 'd': res.val.ad = int_set_del(f_pool, v1.val.ad, i); break; | |
ba5c0057 | 941 | case 'D': res.val.ad = clist_del_matching(f_pool, v1.val.ad, v2.val.t); break; |
9c400ec9 PM |
942 | default: bug("unknown Ca operation"); |
943 | } | |
944 | break; | |
945 | ||
23b1539b PM |
946 | default: |
947 | bug( "Unknown instruction %d (%c)", what->code, what->code & 0xff); | |
948 | } | |
949 | if (what->next) | |
950 | return interpret(what->next); | |
951 | return res; | |
952 | } | |
953 | ||
2d496d20 | 954 | #undef ARG |
9a4037d4 PM |
955 | #define ARG(x,y) \ |
956 | if (!i_same(f1->y, f2->y)) \ | |
957 | return 0; | |
958 | ||
959 | #define ONEARG ARG(v1, a1.p) | |
960 | #define TWOARGS ARG(v1, a1.p) \ | |
961 | ARG(v2, a2.p) | |
962 | ||
963 | #define A2_SAME if (f1->a2.i != f2->a2.i) return 0; | |
964 | ||
4c5f93d7 | 965 | /* |
b093c328 PM |
966 | * i_same - function that does real comparing of instruction trees, you should call filter_same from outside |
967 | */ | |
9a4037d4 PM |
968 | int |
969 | i_same(struct f_inst *f1, struct f_inst *f2) | |
970 | { | |
9a4037d4 PM |
971 | if ((!!f1) != (!!f2)) |
972 | return 0; | |
d4d75628 PM |
973 | if (!f1) |
974 | return 1; | |
9a4037d4 PM |
975 | if (f1->aux != f2->aux) |
976 | return 0; | |
977 | if (f1->code != f2->code) | |
978 | return 0; | |
d4d75628 PM |
979 | if (f1 == f2) /* It looks strange, but it is possible with call rewriting trickery */ |
980 | return 1; | |
9a4037d4 PM |
981 | |
982 | switch(f1->code) { | |
983 | case ',': /* fall through */ | |
984 | case '+': | |
9f0d45d6 PM |
985 | case '-': |
986 | case '*': | |
9a4037d4 | 987 | case '/': |
5f4aee76 PM |
988 | case '|': |
989 | case '&': | |
92a72a4c | 990 | case P('m','p'): |
2d496d20 PM |
991 | case P('!','='): |
992 | case P('=','='): | |
9a4037d4 | 993 | case '<': |
2d496d20 | 994 | case P('<','='): TWOARGS; break; |
9a4037d4 | 995 | |
995e5894 | 996 | case '!': ONEARG; break; |
9a4037d4 | 997 | case '~': TWOARGS; break; |
2d496d20 | 998 | case P('d','e'): ONEARG; break; |
9a4037d4 PM |
999 | |
1000 | case 's': | |
1001 | ARG(v2, a2.p); | |
1002 | { | |
1003 | struct symbol *s1, *s2; | |
1004 | s1 = f1->a1.p; | |
1005 | s2 = f2->a1.p; | |
1006 | if (strcmp(s1->name, s2->name)) | |
1007 | return 0; | |
1008 | if (s1->class != s2->class) | |
1009 | return 0; | |
1010 | } | |
1011 | break; | |
1012 | ||
4bb18dd2 | 1013 | case 'c': |
b1a597e0 OZ |
1014 | switch (f1->aux) { |
1015 | ||
1016 | case T_PREFIX_SET: | |
1017 | if (!trie_same(f1->a2.p, f2->a2.p)) | |
1018 | return 0; | |
9be1086d | 1019 | break; |
b1a597e0 OZ |
1020 | |
1021 | case T_SET: | |
4bb18dd2 PM |
1022 | if (!same_tree(f1->a2.p, f2->a2.p)) |
1023 | return 0; | |
9be1086d | 1024 | break; |
b1a597e0 | 1025 | |
4bb18dd2 PM |
1026 | case T_STRING: |
1027 | if (strcmp(f1->a2.p, f2->a2.p)) | |
1028 | return 0; | |
1029 | break; | |
b1a597e0 | 1030 | |
4bb18dd2 PM |
1031 | default: |
1032 | A2_SAME; | |
1033 | } | |
1034 | break; | |
9a4037d4 | 1035 | case 'C': |
f71bded6 | 1036 | if (val_compare(* (struct f_val *) f1->a1.p, * (struct f_val *) f2->a1.p)) |
9a4037d4 PM |
1037 | return 0; |
1038 | break; | |
9be1086d OF |
1039 | case 'V': |
1040 | if (strcmp((char *) f1->a2.p, (char *) f2->a2.p)) | |
1041 | return 0; | |
1042 | break; | |
684c6f5a | 1043 | case 'p': case 'L': ONEARG; break; |
9a4037d4 | 1044 | case '?': TWOARGS; break; |
afc54517 | 1045 | case '0': case 'E': break; |
2d496d20 | 1046 | case P('p',','): ONEARG; A2_SAME; break; |
0dc4431c | 1047 | case 'P': |
9a4037d4 | 1048 | case 'a': A2_SAME; break; |
2d496d20 | 1049 | case P('e','a'): A2_SAME; break; |
0dc4431c PM |
1050 | case P('P','S'): |
1051 | case P('a','S'): | |
2d496d20 | 1052 | case P('e','S'): ONEARG; A2_SAME; break; |
9a4037d4 | 1053 | |
2d496d20 PM |
1054 | case 'r': ONEARG; break; |
1055 | case P('c','p'): ONEARG; break; | |
d4d75628 PM |
1056 | case P('c','a'): /* Call rewriting trickery to avoid exponential behaviour */ |
1057 | ONEARG; | |
1058 | if (!i_same(f1->a2.p, f2->a2.p)) | |
1059 | return 0; | |
1060 | f2->a2.p = f1->a2.p; | |
1061 | break; | |
aa461248 | 1062 | case P('c','v'): break; /* internal instruction */ |
2d496d20 PM |
1063 | case P('S','W'): ONEARG; if (!same_tree(f1->a2.p, f2->a2.p)) return 0; break; |
1064 | case P('i','M'): TWOARGS; break; | |
afc54517 | 1065 | case P('A','p'): TWOARGS; break; |
9c400ec9 | 1066 | case P('C','a'): TWOARGS; break; |
2eece54a OZ |
1067 | case P('a','f'): |
1068 | case P('a','l'): ONEARG; break; | |
9a4037d4 PM |
1069 | default: |
1070 | bug( "Unknown instruction %d in same (%c)", f1->code, f1->code & 0xff); | |
1071 | } | |
1072 | return i_same(f1->next, f2->next); | |
1073 | } | |
1074 | ||
ff95080f PM |
1075 | /** |
1076 | * f_run - external entry point to filters | |
1077 | * @filter: pointer to filter to run | |
4c5f93d7 | 1078 | * @tmp_attrs: where to store newly generated temporary attributes |
2e9b2421 | 1079 | * @rte: pointer to pointer to &rte being filtered. When route is modified, this is changed with rte_cow(). |
ff95080f | 1080 | * @tmp_pool: all filter allocations go from this pool |
4c5f93d7 | 1081 | * @flags: flags |
ff95080f | 1082 | */ |
23b1539b | 1083 | int |
0a06a9b8 | 1084 | f_run(struct filter *filter, struct rte **rte, struct ea_list **tmp_attrs, struct linpool *tmp_pool, int flags) |
23b1539b PM |
1085 | { |
1086 | struct f_inst *inst; | |
1087 | struct f_val res; | |
6b9fa320 | 1088 | DBG( "Running filter `%s'...", filter->name ); |
23b1539b | 1089 | |
0a06a9b8 | 1090 | f_flags = flags; |
31e79264 | 1091 | f_tmp_attrs = tmp_attrs; |
36bbfc70 | 1092 | f_rte = rte; |
48f9e019 | 1093 | f_rte_old = *rte; |
f31156ca | 1094 | f_pool = tmp_pool; |
23b1539b PM |
1095 | inst = filter->root; |
1096 | res = interpret(inst); | |
0b1cad81 PM |
1097 | if (res.type != T_RETURN) { |
1098 | log( L_ERR "Filter %s did not return accept nor reject. Make up your mind", filter->name); | |
23b1539b | 1099 | return F_ERROR; |
0b1cad81 | 1100 | } |
6b9fa320 | 1101 | DBG( "done (%d)\n", res.val.i ); |
23b1539b PM |
1102 | return res.val.i; |
1103 | } | |
1104 | ||
b1c9d871 MM |
1105 | int |
1106 | f_eval_int(struct f_inst *expr) | |
1c20608e MM |
1107 | { |
1108 | struct f_val res; | |
1109 | ||
b1c9d871 MM |
1110 | f_flags = 0; |
1111 | f_tmp_attrs = NULL; | |
1112 | f_rte = NULL; | |
1113 | f_rte_old = NULL; | |
b1c9d871 MM |
1114 | f_pool = cfg_mem; |
1115 | res = interpret(expr); | |
1116 | if (res.type != T_INT) | |
1117 | cf_error("Integer expression expected"); | |
1118 | return res.val.i; | |
1119 | } | |
1c20608e | 1120 | |
92a72a4c OZ |
1121 | u32 |
1122 | f_eval_asn(struct f_inst *expr) | |
1123 | { | |
1124 | struct f_val res = interpret(expr); | |
1125 | if (res.type != T_INT) | |
1126 | cf_error("Can't operate with value of non-integer type in AS path mask constructor"); | |
1127 | ||
1128 | return res.val.i; | |
1129 | } | |
1130 | ||
ff95080f PM |
1131 | /** |
1132 | * filter_same - compare two filters | |
1133 | * @new: first filter to be compared | |
1134 | * @old: second filter to be compared, notice that this filter is | |
1135 | * damaged while comparing. | |
1136 | * | |
1137 | * Returns 1 in case filters are same, otherwise 0. If there are | |
1138 | * underlying bugs, it will rather say 0 on same filters than say | |
1139 | * 1 on different. | |
1140 | */ | |
30a6108c MM |
1141 | int |
1142 | filter_same(struct filter *new, struct filter *old) | |
1143 | { | |
81ce667b MM |
1144 | if (old == new) /* Handle FILTER_ACCEPT and FILTER_REJECT */ |
1145 | return 1; | |
1146 | if (old == FILTER_ACCEPT || old == FILTER_REJECT || | |
1147 | new == FILTER_ACCEPT || new == FILTER_REJECT) | |
1148 | return 0; | |
9a4037d4 | 1149 | return i_same(new->root, old->root); |
30a6108c | 1150 | } |