]> git.ipfire.org Git - thirdparty/bird.git/blame - filter/f-inst.c
Filter: Implement direct recursion
[thirdparty/bird.git] / filter / f-inst.c
CommitLineData
f62a369f
JMM
1/*
2 * Filters: Instructions themselves
3 *
4 * Copyright 1998 Pavel Machek <pavel@ucw.cz>
5 * Copyright 2018 Maria Matejka <mq@jmq.cz>
6 * Copyright 2018 CZ.NIC z.s.p.o.
7 *
8 * Can be freely distributed and used under the terms of the GNU GPL.
9 *
0da06b71
MM
10 * The filter code goes through several phases:
11 *
12 * 1 Parsing
13 * Flex- and Bison-generated parser decodes the human-readable data into
14 * a struct f_inst tree. This is an infix tree that was interpreted by
15 * depth-first search execution in previous versions of the interpreter.
16 * All instructions have their constructor: f_new_inst(FI_EXAMPLE, ...)
17 * translates into f_new_inst_FI_EXAMPLE(...) and the types are checked in
18 * compile time. If the result of the instruction is always the same,
19 * it's reduced to FI_CONSTANT directly in constructor. This phase also
20 * counts how many instructions are underlying in means of f_line_item
21 * fields to know how much we have to allocate in the next phase.
22 *
23 * 2 Linearize before interpreting
24 * The infix tree is always interpreted in the same order. Therefore we
25 * sort the instructions one after another into struct f_line. Results
26 * and arguments of these instructions are implicitly put on a value
27 * stack; e.g. the + operation just takes two arguments from the value
28 * stack and puts the result on there.
29 *
30 * 3 Interpret
31 * The given line is put on a custom execution stack. If needed (FI_CALL,
32 * FI_SWITCH, FI_AND, FI_OR, FI_CONDITION, ...), another line is put on top
33 * of the stack; when that line finishes, the execution continues on the
34 * older lines on the stack where it stopped before.
35 *
36 * 4 Same
37 * On config reload, the filters have to be compared whether channel
38 * reload is needed or not. The comparison is done by comparing the
39 * struct f_line's recursively.
40 *
41 * The main purpose of this rework was to improve filter performance
42 * by making the interpreter non-recursive.
43 *
44 * The other outcome is concentration of instruction definitions to
45 * one place -- right here. You shall define your instruction only here
e1ac6f1e
MM
46 * and nowhere else.
47 *
48 * Beware. This file is interpreted by M4 macros. These macros
49 * may be more stupid than you could imagine. If something strange
50 * happens after changing this file, compare the results before and
51 * after your change (see the Makefile to find out where the results are)
52 * and see what really happened.
53 *
54 * This file is not directly a C source code -> it is a generator input
55 * for several C sources; every instruction block gets expanded into many
56 * different places.
57 *
26bfe59f
MM
58 * All the arguments are processed literally; if you need an argument including comma,
59 * you have to quote it by [[ ... ]]
60 *
e1ac6f1e
MM
61 * What is the syntax here?
62 * m4_dnl INST(FI_NOP, in, out) { enum value, input args, output args
c0999a14
MM
63 * m4_dnl ARG(num, type); argument, its id (in data fields) and type accessible by v1, v2, v3
64 * m4_dnl ARG_ANY(num); argument with no type check accessible by v1, v2, v3
cde8094c 65 * m4_dnl ARG_TYPE(num, type); just declare the type of argument
c0999a14 66 * m4_dnl VARARG; variable-length argument list; accessible by vv(i) and whati->varcount
e1ac6f1e 67 * m4_dnl LINE(num, unused); this argument has to be converted to its own f_line
63f49457 68 * m4_dnl SYMBOL; symbol handed from config
e1ac6f1e
MM
69 * m4_dnl STATIC_ATTR; static attribute definition
70 * m4_dnl DYNAMIC_ATTR; dynamic attribute definition
71 * m4_dnl RTC; route table config
e1ac6f1e
MM
72 * m4_dnl ACCESS_RTE; this instruction needs route
73 * m4_dnl ACCESS_EATTRS; this instruction needs extended attributes
26bfe59f
MM
74 *
75 * m4_dnl FID_MEMBER( custom instruction member
76 * m4_dnl C type, for storage in structs
b40c0f02 77 * m4_dnl name, how the member is named
30667d50 78 * m4_dnl comparator for same(), if different, this should be TRUE (CAVEAT)
26bfe59f
MM
79 * m4_dnl dump format string debug -> format string for bvsnprintf
80 * m4_dnl dump format args appropriate args
26bfe59f
MM
81 * m4_dnl )
82 *
e1ac6f1e 83 * m4_dnl RESULT(type, union-field, value); putting this on value stack
cde8094c 84 * m4_dnl RESULT_(type, union-field, value); like RESULT(), but do not declare the type
f74d1976 85 * m4_dnl RESULT_VAL(value-struct); pass the struct f_val directly
cde8094c 86 * m4_dnl RESULT_TYPE(type); just declare the type of result value
f74d1976 87 * m4_dnl RESULT_VOID; return undef
e1ac6f1e
MM
88 * m4_dnl }
89 *
0da06b71
MM
90 * Also note that the { ... } blocks are not respected by M4 at all.
91 * If you get weird unmatched-brace-pair errors, check what it generated and why.
92 * What is really considered as one instruction is not the { ... } block
93 * after m4_dnl INST() but all the code between them.
94 *
e1ac6f1e
MM
95 * Other code is just copied into the interpreter part.
96 *
cde8094c
OZ
97 * The filter language uses a simple type system, where values have types
98 * (constants T_*) and also terms (instructions) are statically typed. Our
99 * static typing is partial (some terms do not declare types of arguments
100 * or results), therefore it can detect most but not all type errors and
101 * therefore we still have runtime type checks.
102 *
103 * m4_dnl Types of arguments are declared by macros ARG() and ARG_TYPE(),
104 * m4_dnl types of results are declared by RESULT() and RESULT_TYPE().
105 * m4_dnl Macros ARG_ANY(), RESULT_() and RESULT_VAL() do not declare types
106 * m4_dnl themselves, but can be combined with ARG_TYPE() / RESULT_TYPE().
107 *
108 * m4_dnl Note that types should be declared only once. If there are
109 * m4_dnl multiple RESULT() macros in an instruction definition, they must
110 * m4_dnl use the exact same expression for type, or they should be replaced
111 * m4_dnl by multiple RESULT_() macros and a common RESULT_TYPE() macro.
112 * m4_dnl See e.g. FI_EA_GET or FI_MIN instructions.
113 *
114 *
0da06b71
MM
115 * If you are satisfied with this, you don't need to read the following
116 * detailed description of what is really done with the instruction definitions.
117 *
118 * m4_dnl Now let's look under the cover. The code between each INST()
119 * m4_dnl is copied to several places, namely these (numbered by the M4 diversions
120 * m4_dnl used in filter/decl.m4):
121 *
122 * m4_dnl (102) struct f_inst *f_new_inst(FI_EXAMPLE [[ put it here ]])
123 * m4_dnl {
124 * m4_dnl ... (common code)
125 * m4_dnl (103) [[ put it here ]]
126 * m4_dnl ...
127 * m4_dnl if (all arguments are constant)
26194bd6 128 * m4_dnl (108) [[ put it here ]]
0da06b71
MM
129 * m4_dnl }
130 * m4_dnl For writing directly to constructor argument list, use FID_NEW_ARGS.
131 * m4_dnl For computing something in constructor (103), use FID_NEW_BODY.
132 * m4_dnl For constant pre-interpretation (108), see below at FID_INTERPRET_BODY.
133 *
134 * m4_dnl struct f_inst {
135 * m4_dnl ... (common fields)
136 * m4_dnl union {
137 * m4_dnl struct {
138 * m4_dnl (101) [[ put it here ]]
139 * m4_dnl } i_FI_EXAMPLE;
140 * m4_dnl ...
141 * m4_dnl };
142 * m4_dnl };
143 * m4_dnl This structure is returned from constructor.
144 * m4_dnl For writing directly to this structure, use FID_STRUCT_IN.
145 *
146 * m4_dnl linearize(struct f_line *dest, const struct f_inst *what, uint pos) {
147 * m4_dnl ...
148 * m4_dnl switch (what->fi_code) {
149 * m4_dnl case FI_EXAMPLE:
150 * m4_dnl (105) [[ put it here ]]
151 * m4_dnl break;
152 * m4_dnl }
153 * m4_dnl }
154 * m4_dnl This is called when translating from struct f_inst to struct f_line_item.
155 * m4_dnl For accessing your custom instruction data, use following macros:
156 * m4_dnl whati -> for accessing (struct f_inst).i_FI_EXAMPLE
157 * m4_dnl item -> for accessing (struct f_line)[pos].i_FI_EXAMPLE
158 * m4_dnl For writing directly here, use FID_LINEARIZE_BODY.
159 *
160 * m4_dnl (107) struct f_line_item {
161 * m4_dnl ... (common fields)
162 * m4_dnl union {
163 * m4_dnl struct {
164 * m4_dnl (101) [[ put it here ]]
165 * m4_dnl } i_FI_EXAMPLE;
166 * m4_dnl ...
167 * m4_dnl };
168 * m4_dnl };
169 * m4_dnl The same as FID_STRUCT_IN (101) but for the other structure.
170 * m4_dnl This structure is returned from the linearizer (105).
171 * m4_dnl For writing directly to this structure, use FID_LINE_IN.
172 *
173 * m4_dnl f_dump_line_item_FI_EXAMPLE(const struct f_line_item *item, const int indent)
174 * m4_dnl {
175 * m4_dnl (104) [[ put it here ]]
176 * m4_dnl }
177 * m4_dnl This code dumps the instruction on debug. Note that the argument
178 * m4_dnl is the linearized instruction; if the instruction has arguments,
179 * m4_dnl their code has already been linearized and their value is taken
180 * m4_dnl from the value stack.
181 * m4_dnl For writing directly here, use FID_DUMP_BODY.
182 *
183 * m4_dnl f_same(...)
184 * m4_dnl {
185 * m4_dnl switch (f1_->fi_code) {
186 * m4_dnl case FI_EXAMPLE:
187 * m4_dnl (106) [[ put it here ]]
188 * m4_dnl break;
189 * m4_dnl }
190 * m4_dnl }
191 * m4_dnl This code compares the two given instrucions (f1_ and f2_)
192 * m4_dnl on reconfigure. For accessing your custom instruction data,
193 * m4_dnl use macros f1 and f2.
194 * m4_dnl For writing directly here, use FID_SAME_BODY.
195 *
d06a875b
OZ
196 * m4_dnl f_add_lines(...)
197 * m4_dnl {
198 * m4_dnl switch (what_->fi_code) {
199 * m4_dnl case FI_EXAMPLE:
200 * m4_dnl (109) [[ put it here ]]
201 * m4_dnl break;
202 * m4_dnl }
203 * m4_dnl }
204 * m4_dnl This code adds new filter lines reachable from the instruction
205 * m4_dnl to the filter iterator line buffer. This is for instructions
206 * m4_dnl that changes conrol flow, like FI_CONDITION or FI_CALL, most
207 * m4_dnl instructions do not need to update it. It is used in generic
208 * m4_dnl filter iteration code (FILTER_ITERATE*). For accessing your
209 * m4_dnl custom instruction data, use macros f1 and f2. For writing
210 * m4_dnl directly here, use FID_ITERATE_BODY.
211 *
0da06b71
MM
212 * m4_dnl interpret(...)
213 * m4_dnl {
214 * m4_dnl switch (what->fi_code) {
215 * m4_dnl case FI_EXAMPLE:
216 * m4_dnl (108) [[ put it here ]]
217 * m4_dnl break;
218 * m4_dnl }
219 * m4_dnl }
220 * m4_dnl This code executes the instruction. Every pre-defined macro
221 * m4_dnl resets the output here. For setting it explicitly,
222 * m4_dnl use FID_INTERPRET_BODY.
223 * m4_dnl This code is put on two places; one is the interpreter, the other
224 * m4_dnl is instruction constructor. If you need to distinguish between
225 * m4_dnl these two, use FID_INTERPRET_EXEC or FID_INTERPRET_NEW respectively.
226 * m4_dnl To address the difference between interpreter and constructor
227 * m4_dnl environments, there are several convenience macros defined:
228 * m4_dnl runtime() -> for spitting out runtime error like division by zero
229 * m4_dnl RESULT(...) -> declare result; may overwrite arguments
230 * m4_dnl v1, v2, v3 -> positional arguments, may be overwritten by RESULT()
231 * m4_dnl falloc(size) -> allocate memory from the appropriate linpool
232 * m4_dnl fpool -> the current linpool
233 * m4_dnl NEVER_CONSTANT-> don't generate pre-interpretation code at all
234 * m4_dnl ACCESS_RTE -> check that route is available, also NEVER_CONSTANT
235 * m4_dnl ACCESS_EATTRS -> pre-cache the eattrs; use only with ACCESS_RTE
236 * m4_dnl f_rta_cow(fs) -> function to call before any change to route should be done
237 *
238 * m4_dnl If you are stymied, see FI_CALL or FI_CONSTANT or just search for
239 * m4_dnl the mentioned macros in this file to see what is happening there in wild.
cde8094c
OZ
240 *
241 *
242 * A note about soundness of the type system:
243 *
244 * A type system is sound when types of expressions are consistent with
245 * types of values resulting from evaluation of such expressions. Untyped
246 * expressions are ok, but badly typed expressions are not sound. So is
247 * the type system of BIRD filtering code sound? There are some points:
248 *
249 * All cases of (one) m4_dnl RESULT() macro are obviously ok, as the macro
250 * both declares a type and returns a value. One have to check instructions
251 * that use m4_dnl RESULT_TYPE() macro. There are two issues:
252 *
253 * FI_AND, FI_OR - second argument is statically checked to be T_BOOL and
254 * passed as result without dynamic typecheck, declared to be T_BOOL. If
255 * an untyped non-bool expression is used as a second argument, then
256 * the mismatched type is returned.
257 *
258 * FI_VAR_GET - soundness depends on consistency of declared symbol types
259 * and stored values. This is maintained when values are stored by
260 * FI_VAR_SET, but when they are stored by FI_CALL, only static checking is
261 * used, so when an untyped expression returning mismatched value is used
262 * as a function argument, then inconsistent value is stored and subsequent
263 * FI_VAR_GET would be unsound.
264 *
265 * Both of these issues are inconsequential, as mismatched values from
266 * unsound expressions will be caught by dynamic typechecks like mismatched
267 * values from untyped expressions.
268 *
269 * Also note that FI_CALL is the only expression without properly declared
270 * result type.
f62a369f
JMM
271 */
272
273/* Binary operators */
4c553c5a
MM
274 INST(FI_ADD, 2, 1) {
275 ARG(1,T_INT);
276 ARG(2,T_INT);
f74d1976 277 RESULT(T_INT, i, v1.val.i + v2.val.i);
4c553c5a
MM
278 }
279 INST(FI_SUBTRACT, 2, 1) {
280 ARG(1,T_INT);
281 ARG(2,T_INT);
f74d1976 282 RESULT(T_INT, i, v1.val.i - v2.val.i);
4c553c5a
MM
283 }
284 INST(FI_MULTIPLY, 2, 1) {
285 ARG(1,T_INT);
286 ARG(2,T_INT);
f74d1976 287 RESULT(T_INT, i, v1.val.i * v2.val.i);
4c553c5a
MM
288 }
289 INST(FI_DIVIDE, 2, 1) {
290 ARG(1,T_INT);
291 ARG(2,T_INT);
292 if (v2.val.i == 0) runtime( "Mother told me not to divide by 0" );
f74d1976 293 RESULT(T_INT, i, v1.val.i / v2.val.i);
4c553c5a
MM
294 }
295 INST(FI_AND, 1, 1) {
296 ARG(1,T_BOOL);
ef8c4574 297 ARG_TYPE_STATIC(2,T_BOOL);
10c4cd96
OZ
298 RESULT_TYPE(T_BOOL);
299
f74d1976 300 if (v1.val.i)
4c553c5a
MM
301 LINE(2,0);
302 else
f74d1976 303 RESULT_VAL(v1);
967b88d9 304 }
4c553c5a
MM
305 INST(FI_OR, 1, 1) {
306 ARG(1,T_BOOL);
ef8c4574 307 ARG_TYPE_STATIC(2,T_BOOL);
10c4cd96
OZ
308 RESULT_TYPE(T_BOOL);
309
f74d1976 310 if (!v1.val.i)
4c553c5a
MM
311 LINE(2,0);
312 else
f74d1976 313 RESULT_VAL(v1);
967b88d9 314 }
bfa15a64 315
4c553c5a 316 INST(FI_PAIR_CONSTRUCT, 2, 1) {
f62a369f
JMM
317 ARG(1,T_INT);
318 ARG(2,T_INT);
4c553c5a
MM
319 uint u1 = v1.val.i;
320 uint u2 = v2.val.i;
f62a369f
JMM
321 if ((u1 > 0xFFFF) || (u2 > 0xFFFF))
322 runtime( "Can't operate with value out of bounds in pair constructor" );
4c553c5a 323 RESULT(T_PAIR, i, (u1 << 16) | u2);
967b88d9 324 }
bfa15a64 325
4c553c5a
MM
326 INST(FI_EC_CONSTRUCT, 2, 1) {
327 ARG_ANY(1);
328 ARG(2, T_INT);
26bfe59f 329
f634adc7 330 FID_MEMBER(enum ec_subtype, ecs, f1->ecs != f2->ecs, "ec subtype %s", ec_subtype_str(item->ecs));
f62a369f 331
124d860f 332 int ipv4_used;
4c553c5a 333 u32 key, val;
f62a369f 334
4c553c5a
MM
335 if (v1.type == T_INT) {
336 ipv4_used = 0; key = v1.val.i;
337 }
338 else if (v1.type == T_QUAD) {
339 ipv4_used = 1; key = v1.val.i;
340 }
341 /* IP->Quad implicit conversion */
342 else if (val_is_ip4(&v1)) {
343 ipv4_used = 1; key = ipa_to_u32(v1.val.ip);
344 }
345 else
bfa15a64 346 runtime("Argument 1 of EC constructor must be integer or IPv4 address, got 0x%02x", v1.type);
f62a369f 347
4c553c5a 348 val = v2.val.i;
f62a369f 349
124d860f
MM
350 if (ecs == EC_GENERIC)
351 RESULT(T_EC, ec, ec_generic(key, val));
352 else if (ipv4_used)
353 if (val <= 0xFFFF)
354 RESULT(T_EC, ec, ec_ip4(ecs, key, val));
355 else
356 runtime("4-byte value %u can't be used with IP-address key in extended community", val);
357 else if (key < 0x10000)
358 RESULT(T_EC, ec, ec_as2(ecs, key, val));
359 else
360 if (val <= 0xFFFF)
361 RESULT(T_EC, ec, ec_as4(ecs, key, val));
362 else
363 runtime("4-byte value %u can't be used with 4-byte ASN in extended community", val);
4c553c5a 364 }
f62a369f 365
4c553c5a
MM
366 INST(FI_LC_CONSTRUCT, 3, 1) {
367 ARG(1, T_INT);
368 ARG(2, T_INT);
369 ARG(3, T_INT);
370 RESULT(T_LC, lc, [[(lcomm) { v1.val.i, v2.val.i, v3.val.i }]]);
371 }
f62a369f 372
4c553c5a 373 INST(FI_PATHMASK_CONSTRUCT, 0, 1) {
c0999a14 374 VARARG;
4c553c5a 375
c0999a14
MM
376 struct f_path_mask *pm = falloc(sizeof(struct f_path_mask) + whati->varcount * sizeof(struct f_path_mask_item));
377 pm->len = whati->varcount;
b40c0f02 378
c0999a14
MM
379 for (uint i=0; i<whati->varcount; i++) {
380 switch (vv(i).type) {
4c553c5a 381 case T_PATH_MASK_ITEM:
ec430a7f
OZ
382 if (vv(i).val.pmi.kind == PM_LOOP)
383 {
384 if (i == 0)
385 runtime("Path mask iterator '+' cannot be first");
386
387 /* We want PM_LOOP as prefix operator */
388 pm->item[i] = pm->item[i - 1];
389 pm->item[i - 1] = vv(i).val.pmi;
390 break;
391 }
392
c0999a14 393 pm->item[i] = vv(i).val.pmi;
4c553c5a 394 break;
9f3e0983 395
4c553c5a
MM
396 case T_INT:
397 pm->item[i] = (struct f_path_mask_item) {
c0999a14 398 .asn = vv(i).val.i,
4c553c5a
MM
399 .kind = PM_ASN,
400 };
401 break;
9f3e0983
OZ
402
403 case T_SET:
b2d6d294 404 if (!path_set_type(vv(i).val.t))
9f3e0983
OZ
405 runtime("Only integer sets allowed in path mask");
406
407 pm->item[i] = (struct f_path_mask_item) {
408 .set = vv(i).val.t,
409 .kind = PM_ASN_SET,
410 };
411 break;
412
4c553c5a
MM
413 default:
414 runtime( "Error resolving path mask template: value not an integer" );
415 }
f62a369f 416 }
f62a369f 417
4c553c5a
MM
418 RESULT(T_PATH_MASK, path_mask, pm);
419 }
f62a369f
JMM
420
421/* Relational operators */
422
4c553c5a 423 INST(FI_NEQ, 2, 1) {
c5774939
MM
424 ARG_ANY(1);
425 ARG_ANY(2);
fb1d8f65 426 ARG_PREFER_SAME_TYPE(1, 2);
4c553c5a 427 RESULT(T_BOOL, i, !val_same(&v1, &v2));
967b88d9 428 }
f62a369f 429
4c553c5a 430 INST(FI_EQ, 2, 1) {
c5774939
MM
431 ARG_ANY(1);
432 ARG_ANY(2);
fb1d8f65 433 ARG_PREFER_SAME_TYPE(1, 2);
4c553c5a 434 RESULT(T_BOOL, i, val_same(&v1, &v2));
967b88d9 435 }
c5774939 436
4c553c5a 437 INST(FI_LT, 2, 1) {
c5774939
MM
438 ARG_ANY(1);
439 ARG_ANY(2);
10c4cd96
OZ
440 ARG_SAME_TYPE(1, 2);
441
4c553c5a 442 int i = val_compare(&v1, &v2);
52893045 443 if (i == F_CMP_ERROR)
c5774939 444 runtime( "Can't compare values of incompatible types" );
4c553c5a 445 RESULT(T_BOOL, i, (i == -1));
967b88d9 446 }
f62a369f 447
4c553c5a 448 INST(FI_LTE, 2, 1) {
c5774939
MM
449 ARG_ANY(1);
450 ARG_ANY(2);
10c4cd96
OZ
451 ARG_SAME_TYPE(1, 2);
452
4c553c5a 453 int i = val_compare(&v1, &v2);
52893045 454 if (i == F_CMP_ERROR)
c5774939 455 runtime( "Can't compare values of incompatible types" );
4c553c5a 456 RESULT(T_BOOL, i, (i != 1));
967b88d9 457 }
f62a369f 458
4c553c5a
MM
459 INST(FI_NOT, 1, 1) {
460 ARG(1,T_BOOL);
461 RESULT(T_BOOL, i, !v1.val.i);
967b88d9 462 }
f62a369f 463
4c553c5a 464 INST(FI_MATCH, 2, 1) {
f62a369f
JMM
465 ARG_ANY(1);
466 ARG_ANY(2);
4c553c5a 467 int i = val_in_range(&v1, &v2);
52893045 468 if (i == F_CMP_ERROR)
f62a369f 469 runtime( "~ applied on unknown type pair" );
4c553c5a 470 RESULT(T_BOOL, i, !!i);
967b88d9 471 }
f62a369f 472
4c553c5a 473 INST(FI_NOT_MATCH, 2, 1) {
f62a369f
JMM
474 ARG_ANY(1);
475 ARG_ANY(2);
4c553c5a 476 int i = val_in_range(&v1, &v2);
fe503c7c 477 if (i == F_CMP_ERROR)
f62a369f 478 runtime( "!~ applied on unknown type pair" );
4c553c5a 479 RESULT(T_BOOL, i, !i);
967b88d9 480 }
f62a369f 481
4c553c5a 482 INST(FI_DEFINED, 1, 1) {
f62a369f 483 ARG_ANY(1);
4c553c5a 484 RESULT(T_BOOL, i, (v1.type != T_VOID) && !undef_value(v1));
967b88d9 485 }
4c553c5a
MM
486
487 INST(FI_TYPE, 1, 1) {
f62a369f
JMM
488 ARG_ANY(1); /* There may be more types supporting this operation */
489 switch (v1.type)
490 {
491 case T_NET:
4c553c5a 492 RESULT(T_ENUM_NETTYPE, i, v1.val.net->type);
f62a369f
JMM
493 break;
494 default:
495 runtime( "Can't determine type of this item" );
496 }
967b88d9 497 }
4c553c5a
MM
498
499 INST(FI_IS_V4, 1, 1) {
f62a369f 500 ARG(1, T_IP);
4c553c5a 501 RESULT(T_BOOL, i, ipa_is_ip4(v1.val.ip));
967b88d9 502 }
f62a369f 503
4c553c5a 504 /* Set to indirect value prepared in v1 */
96d757c1 505 INST(FI_VAR_SET, 1, 0) {
b40c0f02 506 NEVER_CONSTANT;
63f49457
MM
507 ARG_ANY(1);
508 SYMBOL;
26194bd6 509 ARG_TYPE(1, sym->class & 0xff);
96d757c1 510
1757a6fc 511 fstk->vstk[curline.vbase + sym->offset] = v1;
96d757c1
JMM
512 }
513
514 INST(FI_VAR_GET, 0, 1) {
26bfe59f 515 SYMBOL;
b40c0f02 516 NEVER_CONSTANT;
6fbcd891 517 RESULT_TYPE(sym->class & 0xff);
f74d1976 518 RESULT_VAL(fstk->vstk[curline.vbase + sym->offset]);
967b88d9 519 }
f62a369f 520
bfa15a64 521 INST(FI_CONSTANT, 0, 1) {
30667d50
MM
522 FID_MEMBER(
523 struct f_val,
524 val,
30667d50 525 [[ !val_same(&(f1->val), &(f2->val)) ]],
f634adc7 526 "value %s",
30667d50
MM
527 val_dump(&(item->val))
528 );
ea4f55e3 529
6fbcd891 530 RESULT_TYPE(val.type);
b40c0f02 531 RESULT_VAL(val);
967b88d9 532 }
bfa15a64 533
4c553c5a
MM
534 INST(FI_CONDITION, 1, 0) {
535 ARG(1, T_BOOL);
b40c0f02 536 if (v1.val.i)
4c553c5a 537 LINE(2,0);
224b77d4 538 else
4c553c5a 539 LINE(3,1);
967b88d9 540 }
9b46748d 541
0206c070
MM
542 INST(FI_PRINT, 0, 0) {
543 NEVER_CONSTANT;
c0999a14 544 VARARG;
0206c070 545
c0999a14
MM
546 if (whati->varcount && !(fs->flags & FF_SILENT))
547 for (uint i=0; i<whati->varcount; i++)
548 val_format(&(vv(i)), &fs->buf);
0206c070
MM
549 }
550
efd7c87b
MM
551 INST(FI_FLUSH, 0, 0) {
552 NEVER_CONSTANT;
553 if (!(fs->flags & FF_SILENT))
554 /* After log_commit, the buffer is reset */
555 log_commit(*L_INFO, &fs->buf);
556 }
557
0206c070
MM
558 INST(FI_DIE, 0, 0) {
559 NEVER_CONSTANT;
f634adc7 560 FID_MEMBER(enum filter_return, fret, f1->fret != f2->fret, "%s", filter_return_str(item->fret));
4c553c5a 561
0206c070 562 switch (whati->fret) {
efd7c87b 563 case F_ACCEPT: /* Should take care about turning ACCEPT into MODIFY */
f62a369f 564 case F_ERROR:
efd7c87b 565 case F_REJECT: /* Maybe print complete route along with reason to reject route? */
4c553c5a 566 return fret; /* We have to return now, no more processing. */
f62a369f
JMM
567 default:
568 bug( "unknown return type: Can't happen");
569 }
967b88d9 570 }
bfa15a64
OZ
571
572 INST(FI_RTA_GET, 0, 1) {
f62a369f 573 {
4c553c5a 574 STATIC_ATTR;
f62a369f
JMM
575 ACCESS_RTE;
576 struct rta *rta = (*fs->rte)->attrs;
f62a369f 577
4c553c5a 578 switch (sa.sa_code)
f62a369f 579 {
4c553c5a
MM
580 case SA_FROM: RESULT(sa.f_type, ip, rta->from); break;
581 case SA_GW: RESULT(sa.f_type, ip, rta->nh.gw); break;
582 case SA_NET: RESULT(sa.f_type, net, (*fs->rte)->net->n.addr); break;
583 case SA_PROTO: RESULT(sa.f_type, s, rta->src->proto->name); break;
584 case SA_SOURCE: RESULT(sa.f_type, i, rta->source); break;
585 case SA_SCOPE: RESULT(sa.f_type, i, rta->scope); break;
586 case SA_DEST: RESULT(sa.f_type, i, rta->dest); break;
587 case SA_IFNAME: RESULT(sa.f_type, s, rta->nh.iface ? rta->nh.iface->name : ""); break;
588 case SA_IFINDEX: RESULT(sa.f_type, i, rta->nh.iface ? rta->nh.iface->index : 0); break;
8cc5bb09 589 case SA_WEIGHT: RESULT(sa.f_type, i, rta->nh.weight + 1); break;
e5468d16 590 case SA_GW_MPLS: RESULT(sa.f_type, i, rta->nh.labels ? rta->nh.label[0] : MPLS_NULL); break;
f62a369f
JMM
591
592 default:
4c553c5a 593 bug("Invalid static attribute access (%u/%u)", sa.f_type, sa.sa_code);
f62a369f
JMM
594 }
595 }
967b88d9 596 }
4c553c5a 597
a84b8b6e
MM
598 INST(FI_RTA_SET, 1, 0) {
599 ACCESS_RTE;
600 ARG_ANY(1);
601 STATIC_ATTR;
26194bd6 602 ARG_TYPE(1, sa.f_type);
a84b8b6e
MM
603
604 f_rta_cow(fs);
605 {
606 struct rta *rta = (*fs->rte)->attrs;
607
608 switch (sa.sa_code)
609 {
610 case SA_FROM:
611 rta->from = v1.val.ip;
612 break;
613
614 case SA_GW:
615 {
616 ip_addr ip = v1.val.ip;
94abefc0
OZ
617 struct iface *ifa = ipa_is_link_local(ip) ? rta->nh.iface : NULL;
618 neighbor *n = neigh_find(rta->src->proto, ip, ifa, 0);
a84b8b6e
MM
619 if (!n || (n->scope == SCOPE_HOST))
620 runtime( "Invalid gw address" );
621
622 rta->dest = RTD_UNICAST;
623 rta->nh.gw = ip;
624 rta->nh.iface = n->iface;
625 rta->nh.next = NULL;
626 rta->hostentry = NULL;
e5468d16 627 rta->nh.labels = 0;
a84b8b6e
MM
628 }
629 break;
630
631 case SA_SCOPE:
632 rta->scope = v1.val.i;
633 break;
634
635 case SA_DEST:
636 {
637 int i = v1.val.i;
638 if ((i != RTD_BLACKHOLE) && (i != RTD_UNREACHABLE) && (i != RTD_PROHIBIT))
639 runtime( "Destination can be changed only to blackhole, unreachable or prohibit" );
640
641 rta->dest = i;
642 rta->nh.gw = IPA_NONE;
643 rta->nh.iface = NULL;
644 rta->nh.next = NULL;
645 rta->hostentry = NULL;
e5468d16 646 rta->nh.labels = 0;
a84b8b6e
MM
647 }
648 break;
649
650 case SA_IFNAME:
651 {
652 struct iface *ifa = if_find_by_name(v1.val.s);
653 if (!ifa)
654 runtime( "Invalid iface name" );
655
656 rta->dest = RTD_UNICAST;
657 rta->nh.gw = IPA_NONE;
658 rta->nh.iface = ifa;
659 rta->nh.next = NULL;
660 rta->hostentry = NULL;
e5468d16
TB
661 rta->nh.labels = 0;
662 }
663 break;
664
665 case SA_GW_MPLS:
666 {
667 if (v1.val.i >= 0x100000)
668 runtime( "Invalid MPLS label" );
669
670 if (v1.val.i != MPLS_NULL)
671 {
672 rta->nh.label[0] = v1.val.i;
673 rta->nh.labels = 1;
674 }
675 else
676 rta->nh.labels = 0;
a84b8b6e
MM
677 }
678 break;
679
8cc5bb09
OZ
680 case SA_WEIGHT:
681 {
682 int i = v1.val.i;
683 if (i < 1 || i > 256)
684 runtime( "Setting weight value out of bounds" );
685 if (rta->dest != RTD_UNICAST)
686 runtime( "Setting weight needs regular nexthop " );
687
688 /* Set weight on all next hops */
689 for (struct nexthop *nh = &rta->nh; nh; nh = nh->next)
690 nh->weight = i - 1;
691 }
692 break;
693
a84b8b6e
MM
694 default:
695 bug("Invalid static attribute access (%u/%u)", sa.f_type, sa.sa_code);
696 }
697 }
698 }
699
4c553c5a
MM
700 INST(FI_EA_GET, 0, 1) { /* Access to extended attributes */
701 DYNAMIC_ATTR;
f62a369f
JMM
702 ACCESS_RTE;
703 ACCESS_EATTRS;
6fbcd891 704 RESULT_TYPE(da.f_type);
f62a369f 705 {
4c553c5a 706 eattr *e = ea_find(*fs->eattrs, da.ea_code);
f62a369f
JMM
707
708 if (!e) {
709 /* A special case: undefined as_path looks like empty as_path */
4c553c5a 710 if (da.type == EAF_TYPE_AS_PATH) {
6fbcd891 711 RESULT_(T_PATH, ad, &null_adata);
f62a369f
JMM
712 break;
713 }
714
715 /* The same special case for int_set */
4c553c5a 716 if (da.type == EAF_TYPE_INT_SET) {
6fbcd891 717 RESULT_(T_CLIST, ad, &null_adata);
f62a369f
JMM
718 break;
719 }
720
721 /* The same special case for ec_set */
4c553c5a 722 if (da.type == EAF_TYPE_EC_SET) {
6fbcd891 723 RESULT_(T_ECLIST, ad, &null_adata);
f62a369f
JMM
724 break;
725 }
726
727 /* The same special case for lc_set */
4c553c5a 728 if (da.type == EAF_TYPE_LC_SET) {
6fbcd891 729 RESULT_(T_LCLIST, ad, &null_adata);
f62a369f
JMM
730 break;
731 }
732
733 /* Undefined value */
f74d1976 734 RESULT_VOID;
f62a369f
JMM
735 break;
736 }
737
738 switch (e->type & EAF_TYPE_MASK) {
739 case EAF_TYPE_INT:
6fbcd891 740 RESULT_(da.f_type, i, e->u.data);
f62a369f
JMM
741 break;
742 case EAF_TYPE_ROUTER_ID:
6fbcd891 743 RESULT_(T_QUAD, i, e->u.data);
f62a369f
JMM
744 break;
745 case EAF_TYPE_OPAQUE:
6fbcd891 746 RESULT_(T_ENUM_EMPTY, i, 0);
f62a369f
JMM
747 break;
748 case EAF_TYPE_IP_ADDRESS:
6fbcd891 749 RESULT_(T_IP, ip, *((ip_addr *) e->u.ptr->data));
f62a369f
JMM
750 break;
751 case EAF_TYPE_AS_PATH:
6fbcd891 752 RESULT_(T_PATH, ad, e->u.ptr);
f62a369f
JMM
753 break;
754 case EAF_TYPE_BITFIELD:
6fbcd891 755 RESULT_(T_BOOL, i, !!(e->u.data & (1u << da.bit)));
f62a369f
JMM
756 break;
757 case EAF_TYPE_INT_SET:
6fbcd891 758 RESULT_(T_CLIST, ad, e->u.ptr);
f62a369f
JMM
759 break;
760 case EAF_TYPE_EC_SET:
6fbcd891 761 RESULT_(T_ECLIST, ad, e->u.ptr);
f62a369f
JMM
762 break;
763 case EAF_TYPE_LC_SET:
6fbcd891 764 RESULT_(T_LCLIST, ad, e->u.ptr);
f62a369f
JMM
765 break;
766 case EAF_TYPE_UNDEF:
f74d1976 767 RESULT_VOID;
f62a369f
JMM
768 break;
769 default:
4c553c5a 770 bug("Unknown dynamic attribute type");
f62a369f
JMM
771 }
772 }
967b88d9 773 }
4c553c5a 774
a84b8b6e
MM
775 INST(FI_EA_SET, 1, 0) {
776 ACCESS_RTE;
777 ACCESS_EATTRS;
778 ARG_ANY(1);
779 DYNAMIC_ATTR;
26194bd6 780 ARG_TYPE(1, da.f_type);
a84b8b6e
MM
781 {
782 struct ea_list *l = lp_alloc(fs->pool, sizeof(struct ea_list) + sizeof(eattr));
783
784 l->next = NULL;
785 l->flags = EALF_SORTED;
786 l->count = 1;
787 l->attrs[0].id = da.ea_code;
788 l->attrs[0].flags = 0;
789 l->attrs[0].type = da.type | EAF_ORIGINATED | EAF_FRESH;
790
791 switch (da.type) {
792 case EAF_TYPE_INT:
a84b8b6e 793 case EAF_TYPE_ROUTER_ID:
a84b8b6e
MM
794 l->attrs[0].u.data = v1.val.i;
795 break;
796
797 case EAF_TYPE_OPAQUE:
798 runtime( "Setting opaque attribute is not allowed" );
799 break;
bfa15a64 800
26194bd6 801 case EAF_TYPE_IP_ADDRESS:;
a84b8b6e
MM
802 int len = sizeof(ip_addr);
803 struct adata *ad = lp_alloc(fs->pool, sizeof(struct adata) + len);
804 ad->length = len;
805 (* (ip_addr *) ad->data) = v1.val.ip;
806 l->attrs[0].u.ptr = ad;
807 break;
bfa15a64 808
a84b8b6e 809 case EAF_TYPE_AS_PATH:
26194bd6
OZ
810 case EAF_TYPE_INT_SET:
811 case EAF_TYPE_EC_SET:
812 case EAF_TYPE_LC_SET:
a84b8b6e
MM
813 l->attrs[0].u.ptr = v1.val.ad;
814 break;
bfa15a64 815
a84b8b6e 816 case EAF_TYPE_BITFIELD:
a84b8b6e
MM
817 {
818 /* First, we have to find the old value */
819 eattr *e = ea_find(*fs->eattrs, da.ea_code);
820 u32 data = e ? e->u.data : 0;
821
822 if (v1.val.i)
823 l->attrs[0].u.data = data | (1u << da.bit);
824 else
825 l->attrs[0].u.data = data & ~(1u << da.bit);
826 }
827 break;
bfa15a64 828
bfa15a64
OZ
829 default:
830 bug("Unknown dynamic attribute type");
a84b8b6e
MM
831 }
832
833 f_rta_cow(fs);
834 l->next = *fs->eattrs;
835 *fs->eattrs = l;
836 }
837 }
838
9b46748d
MM
839 INST(FI_EA_UNSET, 0, 0) {
840 DYNAMIC_ATTR;
841 ACCESS_RTE;
842 ACCESS_EATTRS;
843
844 {
845 struct ea_list *l = lp_alloc(fs->pool, sizeof(struct ea_list) + sizeof(eattr));
846
847 l->next = NULL;
848 l->flags = EALF_SORTED;
849 l->count = 1;
850 l->attrs[0].id = da.ea_code;
851 l->attrs[0].flags = 0;
8d65add6 852 l->attrs[0].type = EAF_TYPE_UNDEF | EAF_ORIGINATED | EAF_FRESH;
9b46748d
MM
853 l->attrs[0].u.data = 0;
854
855 f_rta_cow(fs);
856 l->next = *fs->eattrs;
857 *fs->eattrs = l;
858 }
859 }
860
4c553c5a 861 INST(FI_PREF_GET, 0, 1) {
f62a369f 862 ACCESS_RTE;
4c553c5a 863 RESULT(T_INT, i, (*fs->rte)->pref);
967b88d9 864 }
4c553c5a 865
a84b8b6e
MM
866 INST(FI_PREF_SET, 1, 0) {
867 ACCESS_RTE;
868 ARG(1,T_INT);
869 if (v1.val.i > 0xFFFF)
870 runtime( "Setting preference value out of bounds" );
871 f_rte_cow(fs);
872 (*fs->rte)->pref = v1.val.i;
873 }
874
4c553c5a 875 INST(FI_LENGTH, 1, 1) { /* Get length of */
f62a369f 876 ARG_ANY(1);
f62a369f 877 switch(v1.type) {
4c553c5a
MM
878 case T_NET: RESULT(T_INT, i, net_pxlen(v1.val.net)); break;
879 case T_PATH: RESULT(T_INT, i, as_path_getlen(v1.val.ad)); break;
880 case T_CLIST: RESULT(T_INT, i, int_set_get_size(v1.val.ad)); break;
881 case T_ECLIST: RESULT(T_INT, i, ec_set_get_size(v1.val.ad)); break;
882 case T_LCLIST: RESULT(T_INT, i, lc_set_get_size(v1.val.ad)); break;
f62a369f
JMM
883 default: runtime( "Prefix, path, clist or eclist expected" );
884 }
967b88d9 885 }
4c553c5a 886
ff2ca10c 887 INST(FI_NET_SRC, 1, 1) { /* Get src prefix */
f62a369f 888 ARG(1, T_NET);
f62a369f 889
ff2ca10c 890 net_addr_union *net = (void *) v1.val.net;
b40c0f02 891 net_addr *src = falloc(sizeof(net_addr_ip6));
ff2ca10c
OZ
892 const byte *part;
893
894 switch(v1.val.net->type) {
895 case NET_FLOW4:
896 part = flow4_get_part(&net->flow4, FLOW_TYPE_SRC_PREFIX);
897 if (part)
898 net_fill_ip4(src, flow_read_ip4_part(part), flow_read_pxlen(part));
899 else
900 net_fill_ip4(src, IP4_NONE, 0);
901 break;
902
903 case NET_FLOW6:
904 part = flow6_get_part(&net->flow6, FLOW_TYPE_SRC_PREFIX);
905 if (part)
906 net_fill_ip6(src, flow_read_ip6_part(part), flow_read_pxlen(part));
907 else
908 net_fill_ip6(src, IP6_NONE, 0);
909 break;
910
911 case NET_IP6_SADR:
912 net_fill_ip6(src, net->ip6_sadr.src_prefix, net->ip6_sadr.src_pxlen);
913 break;
914
915 default:
916 runtime( "Flow or SADR expected" );
917 }
f62a369f 918
4c553c5a 919 RESULT(T_NET, net, src);
967b88d9 920 }
4c553c5a 921
ff2ca10c
OZ
922 INST(FI_NET_DST, 1, 1) { /* Get dst prefix */
923 ARG(1, T_NET);
924
925 net_addr_union *net = (void *) v1.val.net;
926 net_addr *dst = falloc(sizeof(net_addr_ip6));
927 const byte *part;
928
929 switch(v1.val.net->type) {
930 case NET_FLOW4:
931 part = flow4_get_part(&net->flow4, FLOW_TYPE_DST_PREFIX);
932 if (part)
933 net_fill_ip4(dst, flow_read_ip4_part(part), flow_read_pxlen(part));
934 else
935 net_fill_ip4(dst, IP4_NONE, 0);
936 break;
937
938 case NET_FLOW6:
939 part = flow6_get_part(&net->flow6, FLOW_TYPE_DST_PREFIX);
940 if (part)
941 net_fill_ip6(dst, flow_read_ip6_part(part), flow_read_pxlen(part));
942 else
943 net_fill_ip6(dst, IP6_NONE, 0);
944 break;
945
946 case NET_IP6_SADR:
947 net_fill_ip6(dst, net->ip6_sadr.dst_prefix, net->ip6_sadr.dst_pxlen);
948 break;
949
950 default:
951 runtime( "Flow or SADR expected" );
952 }
953
954 RESULT(T_NET, net, dst);
955 }
956
4c553c5a 957 INST(FI_ROA_MAXLEN, 1, 1) { /* Get ROA max prefix length */
f62a369f
JMM
958 ARG(1, T_NET);
959 if (!net_is_roa(v1.val.net))
960 runtime( "ROA expected" );
961
4c553c5a 962 RESULT(T_INT, i, (v1.val.net->type == NET_ROA4) ?
f62a369f 963 ((net_addr_roa4 *) v1.val.net)->max_pxlen :
4c553c5a 964 ((net_addr_roa6 *) v1.val.net)->max_pxlen);
967b88d9 965 }
4c553c5a 966
a2a268da
AZ
967 INST(FI_ASN, 1, 1) { /* Get ROA ASN or community ASN part */
968 ARG_ANY(1);
969 RESULT_TYPE(T_INT);
970 switch(v1.type)
971 {
972 case T_NET:
973 if (!net_is_roa(v1.val.net))
974 runtime( "ROA expected" );
f62a369f 975
a2a268da
AZ
976 RESULT_(T_INT, i, (v1.val.net->type == NET_ROA4) ?
977 ((net_addr_roa4 *) v1.val.net)->asn :
978 ((net_addr_roa6 *) v1.val.net)->asn);
979 break;
980
981 case T_PAIR:
982 RESULT_(T_INT, i, v1.val.i >> 16);
983 break;
984
985 case T_LC:
986 RESULT_(T_INT, i, v1.val.lc.asn);
987 break;
988
989 default:
990 runtime( "Net, pair or lc expected" );
991 }
967b88d9 992 }
4c553c5a
MM
993
994 INST(FI_IP, 1, 1) { /* Convert prefix to ... */
f62a369f 995 ARG(1, T_NET);
4c553c5a 996 RESULT(T_IP, ip, net_prefix(v1.val.net));
967b88d9 997 }
4c553c5a
MM
998
999 INST(FI_ROUTE_DISTINGUISHER, 1, 1) {
f62a369f 1000 ARG(1, T_NET);
f62a369f
JMM
1001 if (!net_is_vpn(v1.val.net))
1002 runtime( "VPN address expected" );
4c553c5a 1003 RESULT(T_RD, ec, net_rd(v1.val.net));
967b88d9 1004 }
f62a369f 1005
4c553c5a
MM
1006 INST(FI_AS_PATH_FIRST, 1, 1) { /* Get first ASN from AS PATH */
1007 ARG(1, T_PATH);
10c4cd96 1008 u32 as = 0;
f62a369f 1009 as_path_get_first(v1.val.ad, &as);
4c553c5a 1010 RESULT(T_INT, i, as);
967b88d9 1011 }
f62a369f 1012
bfa15a64 1013 INST(FI_AS_PATH_LAST, 1, 1) { /* Get last ASN from AS PATH */
4c553c5a 1014 ARG(1, T_PATH);
10c4cd96 1015 u32 as = 0;
f62a369f 1016 as_path_get_last(v1.val.ad, &as);
4c553c5a 1017 RESULT(T_INT, i, as);
967b88d9 1018 }
4c553c5a
MM
1019
1020 INST(FI_AS_PATH_LAST_NAG, 1, 1) { /* Get last ASN from non-aggregated part of AS PATH */
f62a369f 1021 ARG(1, T_PATH);
4c553c5a
MM
1022 RESULT(T_INT, i, as_path_get_last_nonaggregated(v1.val.ad));
1023 }
f62a369f 1024
a2a268da
AZ
1025 INST(FI_PAIR_DATA, 1, 1) { /* Get data part from the standard community */
1026 ARG(1, T_PAIR);
1027 RESULT(T_INT, i, v1.val.i & 0xFFFF);
1028 }
1029
1030 INST(FI_LC_DATA1, 1, 1) { /* Get data1 part from the large community */
1031 ARG(1, T_LC);
1032 RESULT(T_INT, i, v1.val.lc.ldp1);
1033 }
1034
1035 INST(FI_LC_DATA2, 1, 1) { /* Get data2 part from the large community */
1036 ARG(1, T_LC);
1037 RESULT(T_INT, i, v1.val.lc.ldp2);
1038 }
1039
8f3c6151 1040 INST(FI_MIN, 1, 1) { /* Get minimum element from list */
0e1fd7ea
AZ
1041 ARG_ANY(1);
1042 RESULT_TYPE(f_type_element_type(v1.type));
1043 switch(v1.type)
1044 {
1045 case T_CLIST:
1046 {
1047 u32 val = 0;
1048 int_set_min(v1.val.ad, &val);
1049 RESULT_(T_PAIR, i, val);
1050 }
1051 break;
1052
1053 case T_ECLIST:
1054 {
1055 u64 val = 0;
1056 ec_set_min(v1.val.ad, &val);
1057 RESULT_(T_EC, ec, val);
1058 }
1059 break;
1060
1061 case T_LCLIST:
1062 {
1063 lcomm val = { 0, 0, 0 };
1064 lc_set_min(v1.val.ad, &val);
1065 RESULT_(T_LC, lc, val);
1066 }
1067 break;
1068
1069 default:
1070 runtime( "Clist or lclist expected" );
1071 }
1072 }
1073
8f3c6151 1074 INST(FI_MAX, 1, 1) { /* Get maximum element from list */
0e1fd7ea
AZ
1075 ARG_ANY(1);
1076 RESULT_TYPE(f_type_element_type(v1.type));
1077 switch(v1.type)
1078 {
1079 case T_CLIST:
1080 {
1081 u32 val = 0;
1082 int_set_max(v1.val.ad, &val);
1083 RESULT_(T_PAIR, i, val);
1084 }
1085 break;
1086
1087 case T_ECLIST:
1088 {
1089 u64 val = 0;
1090 ec_set_max(v1.val.ad, &val);
1091 RESULT_(T_EC, ec, val);
1092 }
1093 break;
1094
1095 case T_LCLIST:
1096 {
1097 lcomm val = { 0, 0, 0 };
1098 lc_set_max(v1.val.ad, &val);
1099 RESULT_(T_LC, lc, val);
1100 }
1101 break;
1102
1103 default:
1104 runtime( "Clist or lclist expected" );
1105 }
1106 }
1107
9b302c13 1108 INST(FI_RETURN, 1, 0) {
b40c0f02 1109 NEVER_CONSTANT;
a84b8b6e
MM
1110 /* Acquire the return value */
1111 ARG_ANY(1);
1112 uint retpos = fstk->vcnt;
1113
1114 /* Drop every sub-block including ourselves */
9e263550
OZ
1115 do fstk->ecnt--;
1116 while ((fstk->ecnt > 0) && !(fstk->estk[fstk->ecnt].emask & FE_RETURN));
a84b8b6e
MM
1117
1118 /* Now we are at the caller frame; if no such, try to convert to accept/reject. */
1119 if (!fstk->ecnt)
9e263550 1120 {
a84b8b6e 1121 if (fstk->vstk[retpos].type == T_BOOL)
9e263550 1122 return (fstk->vstk[retpos].val.i) ? F_ACCEPT : F_REJECT;
a84b8b6e
MM
1123 else
1124 runtime("Can't return non-bool from non-function");
9e263550 1125 }
a84b8b6e
MM
1126
1127 /* Set the value stack position, overwriting the former implicit void */
1128 fstk->vcnt = fstk->estk[fstk->ecnt].ventry - 1;
1129
1130 /* Copy the return value */
1131 RESULT_VAL(fstk->vstk[retpos]);
1132 }
1133
4c553c5a 1134 INST(FI_CALL, 0, 1) {
b40c0f02 1135 NEVER_CONSTANT;
4c0c507b 1136 VARARG;
96d757c1 1137 SYMBOL;
4f082dfa 1138
9b302c13
OZ
1139 /* Fake result type declaration */
1140 RESULT_TYPE(T_VOID);
1141
4c0c507b
OZ
1142 FID_NEW_BODY()
1143 ASSERT(sym->class == SYM_FUNCTION);
1144
1145 if (whati->varcount != sym->function->args)
1146 cf_error("Function '%s' expects %u arguments, got %u arguments",
1147 sym->name, sym->function->args, whati->varcount);
1148
93d6096c
OZ
1149 /* Typecheck individual arguments */
1150 struct f_inst *a = fvar;
1151 struct f_arg *b = sym->function->arg_list;
1152 for (uint i = 1; a && b; a = a->next, b = b->next, i++)
1153 {
1154 enum f_type b_type = b->arg->class & 0xff;
1155
1156 if (a->type && (a->type != b_type) && !f_const_promotion(a, b_type))
1157 cf_error("Argument %u of '%s' must be %s, got %s",
1158 i, sym->name, f_type_name(b_type), f_type_name(a->type));
1159 }
1160 ASSERT(!a && !b);
1161
4c0c507b
OZ
1162 /* Add implicit void slot for the return value */
1163 struct f_inst *tmp = f_new_inst(FI_CONSTANT, (struct f_val) { .type = T_VOID });
1164 tmp->next = whati->fvar;
1165 whati->fvar = tmp;
1166 what->size += tmp->size;
1167
26bc4f99
OZ
1168 /* Mark recursive calls, they have dummy f_line */
1169 if (!sym->function->len)
1170 what->flags |= FIF_RECURSIVE;
1171
3f477ccb 1172 FID_SAME_BODY()
26bc4f99
OZ
1173 if (!(f1->sym->flags & SYM_FLAG_SAME) && !(f1_->flags & FIF_RECURSIVE))
1174 return 0;
d06a875b
OZ
1175
1176 FID_ITERATE_BODY()
26bc4f99 1177 if (!(what->flags & FIF_RECURSIVE))
d06a875b
OZ
1178 BUFFER_PUSH(fit->lines) = whati->sym->function;
1179
3f477ccb
MM
1180 FID_INTERPRET_BODY()
1181
96d757c1
JMM
1182 /* Push the body on stack */
1183 LINEX(sym->function);
4c0c507b 1184 curline.vbase = curline.ventry;
ea4f55e3 1185 curline.emask |= FE_RETURN;
bfa15a64 1186
4c0c507b
OZ
1187 /* Arguments on stack */
1188 fstk->vcnt += sym->function->args;
96d757c1
JMM
1189
1190 /* Storage for local variables */
1757a6fc
MM
1191 memset(&(fstk->vstk[fstk->vcnt]), 0, sizeof(struct f_val) * sym->function->vars);
1192 fstk->vcnt += sym->function->vars;
967b88d9 1193 }
4c553c5a
MM
1194
1195 INST(FI_DROP_RESULT, 1, 0) {
b40c0f02 1196 NEVER_CONSTANT;
4c553c5a 1197 ARG_ANY(1);
967b88d9 1198 }
4c553c5a 1199
4c553c5a 1200 INST(FI_SWITCH, 1, 0) {
f62a369f 1201 ARG_ANY(1);
26bfe59f 1202
f634adc7 1203 FID_MEMBER(struct f_tree *, tree, [[!same_tree(f1->tree, f2->tree)]], "tree %p", item->tree);
26bfe59f 1204
d06a875b
OZ
1205 FID_ITERATE_BODY()
1206 tree_walk(whati->tree, f_add_tree_lines, fit);
1207
1208 FID_INTERPRET_BODY()
32793ab6
MM
1209 const struct f_tree *t = find_tree(tree, &v1);
1210 if (!t) {
4c553c5a 1211 v1.type = T_VOID;
32793ab6
MM
1212 t = find_tree(tree, &v1);
1213 if (!t) {
4c553c5a 1214 debug( "No else statement?\n");
b40c0f02 1215 FID_HIC(,break,return NULL);
f62a369f 1216 }
f62a369f 1217 }
4c553c5a
MM
1218 /* It is actually possible to have t->data NULL */
1219
32793ab6 1220 LINEX(t->data);
967b88d9 1221 }
4c553c5a
MM
1222
1223 INST(FI_IP_MASK, 2, 1) { /* IP.MASK(val) */
f62a369f
JMM
1224 ARG(1, T_IP);
1225 ARG(2, T_INT);
4c553c5a 1226 RESULT(T_IP, ip, [[ ipa_is_ip4(v1.val.ip) ?
f62a369f 1227 ipa_from_ip4(ip4_and(ipa_to_ip4(v1.val.ip), ip4_mkmask(v2.val.i))) :
4c553c5a 1228 ipa_from_ip6(ip6_and(ipa_to_ip6(v1.val.ip), ip6_mkmask(v2.val.i))) ]]);
967b88d9 1229 }
f62a369f 1230
4c553c5a 1231 INST(FI_PATH_PREPEND, 2, 1) { /* Path prepend */
f62a369f
JMM
1232 ARG(1, T_PATH);
1233 ARG(2, T_INT);
b40c0f02 1234 RESULT(T_PATH, ad, [[ as_path_prepend(fpool, v1.val.ad, v2.val.i) ]]);
4c553c5a
MM
1235 }
1236
1237 INST(FI_CLIST_ADD, 2, 1) { /* (Extended) Community list add */
1238 ARG_ANY(1);
1239 ARG_ANY(2);
6fbcd891
OZ
1240 RESULT_TYPE(f1->type);
1241
4c553c5a
MM
1242 if (v1.type == T_PATH)
1243 runtime("Can't add to path");
1244
1245 else if (v1.type == T_CLIST)
1246 {
1247 /* Community (or cluster) list */
1248 struct f_val dummy;
1249
1250 if ((v2.type == T_PAIR) || (v2.type == T_QUAD))
6fbcd891 1251 RESULT_(T_CLIST, ad, [[ int_set_add(fpool, v1.val.ad, v2.val.i) ]]);
4c553c5a
MM
1252 /* IP->Quad implicit conversion */
1253 else if (val_is_ip4(&v2))
6fbcd891 1254 RESULT_(T_CLIST, ad, [[ int_set_add(fpool, v1.val.ad, ipa_to_u32(v2.val.ip)) ]]);
4c553c5a
MM
1255 else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy))
1256 runtime("Can't add set");
1257 else if (v2.type == T_CLIST)
6fbcd891 1258 RESULT_(T_CLIST, ad, [[ int_set_union(fpool, v1.val.ad, v2.val.ad) ]]);
4c553c5a
MM
1259 else
1260 runtime("Can't add non-pair");
1261 }
f62a369f 1262
4c553c5a
MM
1263 else if (v1.type == T_ECLIST)
1264 {
1265 /* v2.val is either EC or EC-set */
1266 if ((v2.type == T_SET) && eclist_set_type(v2.val.t))
1267 runtime("Can't add set");
1268 else if (v2.type == T_ECLIST)
6fbcd891 1269 RESULT_(T_ECLIST, ad, [[ ec_set_union(fpool, v1.val.ad, v2.val.ad) ]]);
4c553c5a
MM
1270 else if (v2.type != T_EC)
1271 runtime("Can't add non-ec");
1272 else
6fbcd891 1273 RESULT_(T_ECLIST, ad, [[ ec_set_add(fpool, v1.val.ad, v2.val.ec) ]]);
4c553c5a
MM
1274 }
1275
1276 else if (v1.type == T_LCLIST)
1277 {
1278 /* v2.val is either LC or LC-set */
1279 if ((v2.type == T_SET) && lclist_set_type(v2.val.t))
1280 runtime("Can't add set");
1281 else if (v2.type == T_LCLIST)
6fbcd891 1282 RESULT_(T_LCLIST, ad, [[ lc_set_union(fpool, v1.val.ad, v2.val.ad) ]]);
4c553c5a
MM
1283 else if (v2.type != T_LC)
1284 runtime("Can't add non-lc");
1285 else
6fbcd891 1286 RESULT_(T_LCLIST, ad, [[ lc_set_add(fpool, v1.val.ad, v2.val.lc) ]]);
4c553c5a
MM
1287
1288 }
1289
1290 else
1291 runtime("Can't add to non-[e|l]clist");
967b88d9 1292 }
f62a369f 1293
4c553c5a 1294 INST(FI_CLIST_DEL, 2, 1) { /* (Extended) Community list add or delete */
f62a369f
JMM
1295 ARG_ANY(1);
1296 ARG_ANY(2);
6fbcd891
OZ
1297 RESULT_TYPE(f1->type);
1298
f62a369f
JMM
1299 if (v1.type == T_PATH)
1300 {
8f3c6151
OZ
1301 if ((v2.type == T_SET) && path_set_type(v2.val.t) || (v2.type == T_INT))
1302 RESULT_(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, &v2, 0) ]]);
f62a369f
JMM
1303 else
1304 runtime("Can't delete non-integer (set)");
f62a369f 1305 }
4c553c5a 1306
f62a369f
JMM
1307 else if (v1.type == T_CLIST)
1308 {
1309 /* Community (or cluster) list */
1310 struct f_val dummy;
f62a369f
JMM
1311
1312 if ((v2.type == T_PAIR) || (v2.type == T_QUAD))
6fbcd891 1313 RESULT_(T_CLIST, ad, [[ int_set_del(fpool, v1.val.ad, v2.val.i) ]]);
f62a369f 1314 /* IP->Quad implicit conversion */
4c553c5a 1315 else if (val_is_ip4(&v2))
6fbcd891 1316 RESULT_(T_CLIST, ad, [[ int_set_del(fpool, v1.val.ad, ipa_to_u32(v2.val.ip)) ]]);
4c553c5a 1317 else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy) || (v2.type == T_CLIST))
6fbcd891 1318 RESULT_(T_CLIST, ad, [[ clist_filter(fpool, v1.val.ad, &v2, 0) ]]);
f62a369f 1319 else
4c553c5a 1320 runtime("Can't delete non-pair");
f62a369f 1321 }
4c553c5a 1322
f62a369f
JMM
1323 else if (v1.type == T_ECLIST)
1324 {
f62a369f 1325 /* v2.val is either EC or EC-set */
4c553c5a 1326 if ((v2.type == T_SET) && eclist_set_type(v2.val.t) || (v2.type == T_ECLIST))
6fbcd891 1327 RESULT_(T_ECLIST, ad, [[ eclist_filter(fpool, v1.val.ad, &v2, 0) ]]);
f62a369f 1328 else if (v2.type != T_EC)
4c553c5a
MM
1329 runtime("Can't delete non-ec");
1330 else
6fbcd891 1331 RESULT_(T_ECLIST, ad, [[ ec_set_del(fpool, v1.val.ad, v2.val.ec) ]]);
f62a369f 1332 }
4c553c5a 1333
f62a369f
JMM
1334 else if (v1.type == T_LCLIST)
1335 {
f62a369f 1336 /* v2.val is either LC or LC-set */
4c553c5a 1337 if ((v2.type == T_SET) && lclist_set_type(v2.val.t) || (v2.type == T_LCLIST))
6fbcd891 1338 RESULT_(T_LCLIST, ad, [[ lclist_filter(fpool, v1.val.ad, &v2, 0) ]]);
f62a369f 1339 else if (v2.type != T_LC)
4c553c5a
MM
1340 runtime("Can't delete non-lc");
1341 else
6fbcd891 1342 RESULT_(T_LCLIST, ad, [[ lc_set_del(fpool, v1.val.ad, v2.val.lc) ]]);
4c553c5a 1343 }
f62a369f 1344
4c553c5a
MM
1345 else
1346 runtime("Can't delete in non-[e|l]clist");
1347 }
f62a369f 1348
4c553c5a
MM
1349 INST(FI_CLIST_FILTER, 2, 1) { /* (Extended) Community list add or delete */
1350 ARG_ANY(1);
1351 ARG_ANY(2);
6fbcd891
OZ
1352 RESULT_TYPE(f1->type);
1353
4c553c5a
MM
1354 if (v1.type == T_PATH)
1355 {
8f3c6151
OZ
1356 if ((v2.type == T_SET) && path_set_type(v2.val.t))
1357 RESULT_(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, &v2, 1) ]]);
4c553c5a
MM
1358 else
1359 runtime("Can't filter integer");
f62a369f 1360 }
f62a369f 1361
4c553c5a
MM
1362 else if (v1.type == T_CLIST)
1363 {
1364 /* Community (or cluster) list */
1365 struct f_val dummy;
f62a369f 1366
4c553c5a 1367 if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy) || (v2.type == T_CLIST))
6fbcd891 1368 RESULT_(T_CLIST, ad, [[ clist_filter(fpool, v1.val.ad, &v2, 1) ]]);
4c553c5a
MM
1369 else
1370 runtime("Can't filter pair");
1371 }
1372
1373 else if (v1.type == T_ECLIST)
f62a369f 1374 {
4c553c5a
MM
1375 /* v2.val is either EC or EC-set */
1376 if ((v2.type == T_SET) && eclist_set_type(v2.val.t) || (v2.type == T_ECLIST))
6fbcd891 1377 RESULT_(T_ECLIST, ad, [[ eclist_filter(fpool, v1.val.ad, &v2, 1) ]]);
4c553c5a
MM
1378 else
1379 runtime("Can't filter ec");
1380 }
f62a369f 1381
4c553c5a
MM
1382 else if (v1.type == T_LCLIST)
1383 {
1384 /* v2.val is either LC or LC-set */
1385 if ((v2.type == T_SET) && lclist_set_type(v2.val.t) || (v2.type == T_LCLIST))
6fbcd891 1386 RESULT_(T_LCLIST, ad, [[ lclist_filter(fpool, v1.val.ad, &v2, 1) ]]);
4c553c5a
MM
1387 else
1388 runtime("Can't filter lc");
f62a369f 1389 }
4c553c5a 1390
f62a369f 1391 else
4c553c5a
MM
1392 runtime("Can't filter non-[e|l]clist");
1393 }
f62a369f 1394
4c553c5a 1395 INST(FI_ROA_CHECK_IMPLICIT, 0, 1) { /* ROA Check */
b40c0f02 1396 NEVER_CONSTANT;
4c553c5a 1397 RTC(1);
b40c0f02 1398 struct rtable *table = rtc->table;
4c553c5a
MM
1399 ACCESS_RTE;
1400 ACCESS_EATTRS;
1401 const net_addr *net = (*fs->rte)->net->n.addr;
f62a369f 1402
4c553c5a
MM
1403 /* We ignore temporary attributes, probably not a problem here */
1404 /* 0x02 is a value of BA_AS_PATH, we don't want to include BGP headers */
1405 eattr *e = ea_find(*fs->eattrs, EA_CODE(PROTOCOL_BGP, 0x02));
f62a369f 1406
4c553c5a
MM
1407 if (!e || ((e->type & EAF_TYPE_MASK) != EAF_TYPE_AS_PATH))
1408 runtime("Missing AS_PATH attribute");
1409
1410 u32 as = 0;
1411 as_path_get_last(e->u.ptr, &as);
f62a369f 1412
f62a369f
JMM
1413 if (!table)
1414 runtime("Missing ROA table");
1415
1416 if (table->addr_type != NET_ROA4 && table->addr_type != NET_ROA6)
1417 runtime("Table type must be either ROA4 or ROA6");
1418
4c553c5a
MM
1419 if (table->addr_type != (net->type == NET_IP4 ? NET_ROA4 : NET_ROA6))
1420 RESULT(T_ENUM_ROA, i, ROA_UNKNOWN); /* Prefix and table type mismatch */
1421 else
1422 RESULT(T_ENUM_ROA, i, [[ net_roa_check(table, net, as) ]]);
1423 }
1424
1425 INST(FI_ROA_CHECK_EXPLICIT, 2, 1) { /* ROA Check */
b40c0f02 1426 NEVER_CONSTANT;
4c553c5a
MM
1427 ARG(1, T_NET);
1428 ARG(2, T_INT);
1429 RTC(3);
b40c0f02 1430 struct rtable *table = rtc->table;
4c553c5a
MM
1431
1432 u32 as = v2.val.i;
1433
1434 if (!table)
1435 runtime("Missing ROA table");
1436
1437 if (table->addr_type != NET_ROA4 && table->addr_type != NET_ROA6)
1438 runtime("Table type must be either ROA4 or ROA6");
f62a369f
JMM
1439
1440 if (table->addr_type != (v1.val.net->type == NET_IP4 ? NET_ROA4 : NET_ROA6))
4c553c5a 1441 RESULT(T_ENUM_ROA, i, ROA_UNKNOWN); /* Prefix and table type mismatch */
f62a369f 1442 else
4c553c5a 1443 RESULT(T_ENUM_ROA, i, [[ net_roa_check(table, v1.val.net, as) ]]);
f62a369f 1444
967b88d9 1445 }
f62a369f 1446
9b302c13 1447 INST(FI_FORMAT, 1, 1) { /* Format */
f62a369f 1448 ARG_ANY(1);
b40c0f02 1449 RESULT(T_STRING, s, val_format_str(fpool, &v1));
967b88d9 1450 }
f62a369f 1451
4c553c5a 1452 INST(FI_ASSERT, 1, 0) { /* Birdtest Assert */
b40c0f02 1453 NEVER_CONSTANT;
f62a369f 1454 ARG(1, T_BOOL);
550a6488 1455
f634adc7 1456 FID_MEMBER(char *, s, [[strcmp(f1->s, f2->s)]], "string %s", item->s);
b40c0f02
MM
1457
1458 ASSERT(s);
26bfe59f 1459
c0e958e0
MM
1460 if (!bt_assert_hook)
1461 runtime("No bt_assert hook registered, can't assert");
1462
b40c0f02 1463 bt_assert_hook(v1.val.i, what);
967b88d9 1464 }