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