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