]> git.ipfire.org Git - thirdparty/bird.git/blame - filter/f-inst.c
RIP: Fix handling of passive mode for demand circuit interfaces
[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
65 * m4_dnl VARARG; variable-length argument list; accessible by vv(i) and whati->varcount
e1ac6f1e 66 * m4_dnl LINE(num, unused); this argument has to be converted to its own f_line
63f49457 67 * m4_dnl SYMBOL; symbol handed from config
e1ac6f1e
MM
68 * m4_dnl STATIC_ATTR; static attribute definition
69 * m4_dnl DYNAMIC_ATTR; dynamic attribute definition
70 * m4_dnl RTC; route table config
e1ac6f1e
MM
71 * m4_dnl ACCESS_RTE; this instruction needs route
72 * m4_dnl ACCESS_EATTRS; this instruction needs extended attributes
26bfe59f
MM
73 *
74 * m4_dnl FID_MEMBER( custom instruction member
75 * m4_dnl C type, for storage in structs
b40c0f02 76 * m4_dnl name, how the member is named
30667d50 77 * m4_dnl comparator for same(), if different, this should be TRUE (CAVEAT)
26bfe59f
MM
78 * m4_dnl dump format string debug -> format string for bvsnprintf
79 * m4_dnl dump format args appropriate args
26bfe59f
MM
80 * m4_dnl )
81 *
e1ac6f1e 82 * m4_dnl RESULT(type, union-field, value); putting this on value stack
f74d1976
MM
83 * m4_dnl RESULT_VAL(value-struct); pass the struct f_val directly
84 * m4_dnl RESULT_VOID; return undef
e1ac6f1e
MM
85 * m4_dnl }
86 *
0da06b71
MM
87 * Also note that the { ... } blocks are not respected by M4 at all.
88 * If you get weird unmatched-brace-pair errors, check what it generated and why.
89 * What is really considered as one instruction is not the { ... } block
90 * after m4_dnl INST() but all the code between them.
91 *
e1ac6f1e
MM
92 * Other code is just copied into the interpreter part.
93 *
0da06b71
MM
94 * If you are satisfied with this, you don't need to read the following
95 * detailed description of what is really done with the instruction definitions.
96 *
97 * m4_dnl Now let's look under the cover. The code between each INST()
98 * m4_dnl is copied to several places, namely these (numbered by the M4 diversions
99 * m4_dnl used in filter/decl.m4):
100 *
101 * m4_dnl (102) struct f_inst *f_new_inst(FI_EXAMPLE [[ put it here ]])
102 * m4_dnl {
103 * m4_dnl ... (common code)
104 * m4_dnl (103) [[ put it here ]]
105 * m4_dnl ...
106 * m4_dnl if (all arguments are constant)
26194bd6 107 * m4_dnl (108) [[ put it here ]]
0da06b71
MM
108 * m4_dnl }
109 * m4_dnl For writing directly to constructor argument list, use FID_NEW_ARGS.
110 * m4_dnl For computing something in constructor (103), use FID_NEW_BODY.
111 * m4_dnl For constant pre-interpretation (108), see below at FID_INTERPRET_BODY.
112 *
113 * m4_dnl struct f_inst {
114 * m4_dnl ... (common fields)
115 * m4_dnl union {
116 * m4_dnl struct {
117 * m4_dnl (101) [[ put it here ]]
118 * m4_dnl } i_FI_EXAMPLE;
119 * m4_dnl ...
120 * m4_dnl };
121 * m4_dnl };
122 * m4_dnl This structure is returned from constructor.
123 * m4_dnl For writing directly to this structure, use FID_STRUCT_IN.
124 *
125 * m4_dnl linearize(struct f_line *dest, const struct f_inst *what, uint pos) {
126 * m4_dnl ...
127 * m4_dnl switch (what->fi_code) {
128 * m4_dnl case FI_EXAMPLE:
129 * m4_dnl (105) [[ put it here ]]
130 * m4_dnl break;
131 * m4_dnl }
132 * m4_dnl }
133 * m4_dnl This is called when translating from struct f_inst to struct f_line_item.
134 * m4_dnl For accessing your custom instruction data, use following macros:
135 * m4_dnl whati -> for accessing (struct f_inst).i_FI_EXAMPLE
136 * m4_dnl item -> for accessing (struct f_line)[pos].i_FI_EXAMPLE
137 * m4_dnl For writing directly here, use FID_LINEARIZE_BODY.
138 *
139 * m4_dnl (107) struct f_line_item {
140 * m4_dnl ... (common fields)
141 * m4_dnl union {
142 * m4_dnl struct {
143 * m4_dnl (101) [[ put it here ]]
144 * m4_dnl } i_FI_EXAMPLE;
145 * m4_dnl ...
146 * m4_dnl };
147 * m4_dnl };
148 * m4_dnl The same as FID_STRUCT_IN (101) but for the other structure.
149 * m4_dnl This structure is returned from the linearizer (105).
150 * m4_dnl For writing directly to this structure, use FID_LINE_IN.
151 *
152 * m4_dnl f_dump_line_item_FI_EXAMPLE(const struct f_line_item *item, const int indent)
153 * m4_dnl {
154 * m4_dnl (104) [[ put it here ]]
155 * m4_dnl }
156 * m4_dnl This code dumps the instruction on debug. Note that the argument
157 * m4_dnl is the linearized instruction; if the instruction has arguments,
158 * m4_dnl their code has already been linearized and their value is taken
159 * m4_dnl from the value stack.
160 * m4_dnl For writing directly here, use FID_DUMP_BODY.
161 *
162 * m4_dnl f_same(...)
163 * m4_dnl {
164 * m4_dnl switch (f1_->fi_code) {
165 * m4_dnl case FI_EXAMPLE:
166 * m4_dnl (106) [[ put it here ]]
167 * m4_dnl break;
168 * m4_dnl }
169 * m4_dnl }
170 * m4_dnl This code compares the two given instrucions (f1_ and f2_)
171 * m4_dnl on reconfigure. For accessing your custom instruction data,
172 * m4_dnl use macros f1 and f2.
173 * m4_dnl For writing directly here, use FID_SAME_BODY.
174 *
175 * m4_dnl interpret(...)
176 * m4_dnl {
177 * m4_dnl switch (what->fi_code) {
178 * m4_dnl case FI_EXAMPLE:
179 * m4_dnl (108) [[ put it here ]]
180 * m4_dnl break;
181 * m4_dnl }
182 * m4_dnl }
183 * m4_dnl This code executes the instruction. Every pre-defined macro
184 * m4_dnl resets the output here. For setting it explicitly,
185 * m4_dnl use FID_INTERPRET_BODY.
186 * m4_dnl This code is put on two places; one is the interpreter, the other
187 * m4_dnl is instruction constructor. If you need to distinguish between
188 * m4_dnl these two, use FID_INTERPRET_EXEC or FID_INTERPRET_NEW respectively.
189 * m4_dnl To address the difference between interpreter and constructor
190 * m4_dnl environments, there are several convenience macros defined:
191 * m4_dnl runtime() -> for spitting out runtime error like division by zero
192 * m4_dnl RESULT(...) -> declare result; may overwrite arguments
193 * m4_dnl v1, v2, v3 -> positional arguments, may be overwritten by RESULT()
194 * m4_dnl falloc(size) -> allocate memory from the appropriate linpool
195 * m4_dnl fpool -> the current linpool
196 * m4_dnl NEVER_CONSTANT-> don't generate pre-interpretation code at all
197 * m4_dnl ACCESS_RTE -> check that route is available, also NEVER_CONSTANT
198 * m4_dnl ACCESS_EATTRS -> pre-cache the eattrs; use only with ACCESS_RTE
199 * m4_dnl f_rta_cow(fs) -> function to call before any change to route should be done
200 *
201 * m4_dnl If you are stymied, see FI_CALL or FI_CONSTANT or just search for
202 * m4_dnl the mentioned macros in this file to see what is happening there in wild.
f62a369f
JMM
203 */
204
205/* Binary operators */
4c553c5a
MM
206 INST(FI_ADD, 2, 1) {
207 ARG(1,T_INT);
208 ARG(2,T_INT);
f74d1976 209 RESULT(T_INT, i, v1.val.i + v2.val.i);
4c553c5a
MM
210 }
211 INST(FI_SUBTRACT, 2, 1) {
212 ARG(1,T_INT);
213 ARG(2,T_INT);
f74d1976 214 RESULT(T_INT, i, v1.val.i - v2.val.i);
4c553c5a
MM
215 }
216 INST(FI_MULTIPLY, 2, 1) {
217 ARG(1,T_INT);
218 ARG(2,T_INT);
f74d1976 219 RESULT(T_INT, i, v1.val.i * v2.val.i);
4c553c5a
MM
220 }
221 INST(FI_DIVIDE, 2, 1) {
222 ARG(1,T_INT);
223 ARG(2,T_INT);
224 if (v2.val.i == 0) runtime( "Mother told me not to divide by 0" );
f74d1976 225 RESULT(T_INT, i, v1.val.i / v2.val.i);
4c553c5a
MM
226 }
227 INST(FI_AND, 1, 1) {
228 ARG(1,T_BOOL);
ef8c4574 229 ARG_TYPE_STATIC(2,T_BOOL);
10c4cd96
OZ
230 RESULT_TYPE(T_BOOL);
231
f74d1976 232 if (v1.val.i)
4c553c5a
MM
233 LINE(2,0);
234 else
f74d1976 235 RESULT_VAL(v1);
967b88d9 236 }
4c553c5a
MM
237 INST(FI_OR, 1, 1) {
238 ARG(1,T_BOOL);
ef8c4574 239 ARG_TYPE_STATIC(2,T_BOOL);
10c4cd96
OZ
240 RESULT_TYPE(T_BOOL);
241
f74d1976 242 if (!v1.val.i)
4c553c5a
MM
243 LINE(2,0);
244 else
f74d1976 245 RESULT_VAL(v1);
967b88d9 246 }
bfa15a64 247
4c553c5a 248 INST(FI_PAIR_CONSTRUCT, 2, 1) {
f62a369f
JMM
249 ARG(1,T_INT);
250 ARG(2,T_INT);
4c553c5a
MM
251 uint u1 = v1.val.i;
252 uint u2 = v2.val.i;
f62a369f
JMM
253 if ((u1 > 0xFFFF) || (u2 > 0xFFFF))
254 runtime( "Can't operate with value out of bounds in pair constructor" );
4c553c5a 255 RESULT(T_PAIR, i, (u1 << 16) | u2);
967b88d9 256 }
bfa15a64 257
4c553c5a
MM
258 INST(FI_EC_CONSTRUCT, 2, 1) {
259 ARG_ANY(1);
260 ARG(2, T_INT);
26bfe59f 261
f634adc7 262 FID_MEMBER(enum ec_subtype, ecs, f1->ecs != f2->ecs, "ec subtype %s", ec_subtype_str(item->ecs));
f62a369f 263
124d860f 264 int ipv4_used;
4c553c5a 265 u32 key, val;
f62a369f 266
4c553c5a
MM
267 if (v1.type == T_INT) {
268 ipv4_used = 0; key = v1.val.i;
269 }
270 else if (v1.type == T_QUAD) {
271 ipv4_used = 1; key = v1.val.i;
272 }
273 /* IP->Quad implicit conversion */
274 else if (val_is_ip4(&v1)) {
275 ipv4_used = 1; key = ipa_to_u32(v1.val.ip);
276 }
277 else
bfa15a64 278 runtime("Argument 1 of EC constructor must be integer or IPv4 address, got 0x%02x", v1.type);
f62a369f 279
4c553c5a 280 val = v2.val.i;
f62a369f 281
124d860f
MM
282 if (ecs == EC_GENERIC)
283 RESULT(T_EC, ec, ec_generic(key, val));
284 else if (ipv4_used)
285 if (val <= 0xFFFF)
286 RESULT(T_EC, ec, ec_ip4(ecs, key, val));
287 else
288 runtime("4-byte value %u can't be used with IP-address key in extended community", val);
289 else if (key < 0x10000)
290 RESULT(T_EC, ec, ec_as2(ecs, key, val));
291 else
292 if (val <= 0xFFFF)
293 RESULT(T_EC, ec, ec_as4(ecs, key, val));
294 else
295 runtime("4-byte value %u can't be used with 4-byte ASN in extended community", val);
4c553c5a 296 }
f62a369f 297
4c553c5a
MM
298 INST(FI_LC_CONSTRUCT, 3, 1) {
299 ARG(1, T_INT);
300 ARG(2, T_INT);
301 ARG(3, T_INT);
302 RESULT(T_LC, lc, [[(lcomm) { v1.val.i, v2.val.i, v3.val.i }]]);
303 }
f62a369f 304
4c553c5a 305 INST(FI_PATHMASK_CONSTRUCT, 0, 1) {
c0999a14 306 VARARG;
4c553c5a 307
c0999a14
MM
308 struct f_path_mask *pm = falloc(sizeof(struct f_path_mask) + whati->varcount * sizeof(struct f_path_mask_item));
309 pm->len = whati->varcount;
b40c0f02 310
c0999a14
MM
311 for (uint i=0; i<whati->varcount; i++) {
312 switch (vv(i).type) {
4c553c5a 313 case T_PATH_MASK_ITEM:
c0999a14 314 pm->item[i] = vv(i).val.pmi;
4c553c5a 315 break;
9f3e0983 316
4c553c5a
MM
317 case T_INT:
318 pm->item[i] = (struct f_path_mask_item) {
c0999a14 319 .asn = vv(i).val.i,
4c553c5a
MM
320 .kind = PM_ASN,
321 };
322 break;
9f3e0983
OZ
323
324 case T_SET:
325 if (vv(i).val.t->from.type != T_INT)
326 runtime("Only integer sets allowed in path mask");
327
328 pm->item[i] = (struct f_path_mask_item) {
329 .set = vv(i).val.t,
330 .kind = PM_ASN_SET,
331 };
332 break;
333
4c553c5a
MM
334 default:
335 runtime( "Error resolving path mask template: value not an integer" );
336 }
f62a369f 337 }
f62a369f 338
4c553c5a
MM
339 RESULT(T_PATH_MASK, path_mask, pm);
340 }
f62a369f
JMM
341
342/* Relational operators */
343
4c553c5a 344 INST(FI_NEQ, 2, 1) {
c5774939
MM
345 ARG_ANY(1);
346 ARG_ANY(2);
4c553c5a 347 RESULT(T_BOOL, i, !val_same(&v1, &v2));
967b88d9 348 }
f62a369f 349
4c553c5a 350 INST(FI_EQ, 2, 1) {
c5774939
MM
351 ARG_ANY(1);
352 ARG_ANY(2);
4c553c5a 353 RESULT(T_BOOL, i, val_same(&v1, &v2));
967b88d9 354 }
c5774939 355
4c553c5a 356 INST(FI_LT, 2, 1) {
c5774939
MM
357 ARG_ANY(1);
358 ARG_ANY(2);
10c4cd96
OZ
359 ARG_SAME_TYPE(1, 2);
360
4c553c5a 361 int i = val_compare(&v1, &v2);
52893045 362 if (i == F_CMP_ERROR)
c5774939 363 runtime( "Can't compare values of incompatible types" );
4c553c5a 364 RESULT(T_BOOL, i, (i == -1));
967b88d9 365 }
f62a369f 366
4c553c5a 367 INST(FI_LTE, 2, 1) {
c5774939
MM
368 ARG_ANY(1);
369 ARG_ANY(2);
10c4cd96
OZ
370 ARG_SAME_TYPE(1, 2);
371
4c553c5a 372 int i = val_compare(&v1, &v2);
52893045 373 if (i == F_CMP_ERROR)
c5774939 374 runtime( "Can't compare values of incompatible types" );
4c553c5a 375 RESULT(T_BOOL, i, (i != 1));
967b88d9 376 }
f62a369f 377
4c553c5a
MM
378 INST(FI_NOT, 1, 1) {
379 ARG(1,T_BOOL);
380 RESULT(T_BOOL, i, !v1.val.i);
967b88d9 381 }
f62a369f 382
4c553c5a 383 INST(FI_MATCH, 2, 1) {
f62a369f
JMM
384 ARG_ANY(1);
385 ARG_ANY(2);
4c553c5a 386 int i = val_in_range(&v1, &v2);
52893045 387 if (i == F_CMP_ERROR)
f62a369f 388 runtime( "~ applied on unknown type pair" );
4c553c5a 389 RESULT(T_BOOL, i, !!i);
967b88d9 390 }
f62a369f 391
4c553c5a 392 INST(FI_NOT_MATCH, 2, 1) {
f62a369f
JMM
393 ARG_ANY(1);
394 ARG_ANY(2);
4c553c5a 395 int i = val_in_range(&v1, &v2);
fe503c7c 396 if (i == F_CMP_ERROR)
f62a369f 397 runtime( "!~ applied on unknown type pair" );
4c553c5a 398 RESULT(T_BOOL, i, !i);
967b88d9 399 }
f62a369f 400
4c553c5a 401 INST(FI_DEFINED, 1, 1) {
f62a369f 402 ARG_ANY(1);
4c553c5a 403 RESULT(T_BOOL, i, (v1.type != T_VOID) && !undef_value(v1));
967b88d9 404 }
4c553c5a
MM
405
406 INST(FI_TYPE, 1, 1) {
f62a369f
JMM
407 ARG_ANY(1); /* There may be more types supporting this operation */
408 switch (v1.type)
409 {
410 case T_NET:
4c553c5a 411 RESULT(T_ENUM_NETTYPE, i, v1.val.net->type);
f62a369f
JMM
412 break;
413 default:
414 runtime( "Can't determine type of this item" );
415 }
967b88d9 416 }
4c553c5a
MM
417
418 INST(FI_IS_V4, 1, 1) {
f62a369f 419 ARG(1, T_IP);
4c553c5a 420 RESULT(T_BOOL, i, ipa_is_ip4(v1.val.ip));
967b88d9 421 }
f62a369f 422
4c553c5a 423 /* Set to indirect value prepared in v1 */
96d757c1 424 INST(FI_VAR_SET, 1, 0) {
b40c0f02 425 NEVER_CONSTANT;
63f49457
MM
426 ARG_ANY(1);
427 SYMBOL;
26194bd6 428 ARG_TYPE(1, sym->class & 0xff);
96d757c1 429
1757a6fc 430 fstk->vstk[curline.vbase + sym->offset] = v1;
96d757c1
JMM
431 }
432
433 INST(FI_VAR_GET, 0, 1) {
26bfe59f 434 SYMBOL;
b40c0f02 435 NEVER_CONSTANT;
6fbcd891 436 RESULT_TYPE(sym->class & 0xff);
f74d1976 437 RESULT_VAL(fstk->vstk[curline.vbase + sym->offset]);
967b88d9 438 }
f62a369f 439
bfa15a64 440 INST(FI_CONSTANT, 0, 1) {
30667d50
MM
441 FID_MEMBER(
442 struct f_val,
443 val,
30667d50 444 [[ !val_same(&(f1->val), &(f2->val)) ]],
f634adc7 445 "value %s",
30667d50
MM
446 val_dump(&(item->val))
447 );
ea4f55e3 448
6fbcd891 449 RESULT_TYPE(val.type);
b40c0f02 450 RESULT_VAL(val);
967b88d9 451 }
bfa15a64 452
4c553c5a
MM
453 INST(FI_CONDITION, 1, 0) {
454 ARG(1, T_BOOL);
b40c0f02 455 if (v1.val.i)
4c553c5a 456 LINE(2,0);
224b77d4 457 else
4c553c5a 458 LINE(3,1);
967b88d9 459 }
9b46748d 460
0206c070
MM
461 INST(FI_PRINT, 0, 0) {
462 NEVER_CONSTANT;
c0999a14 463 VARARG;
0206c070 464
c0999a14
MM
465 if (whati->varcount && !(fs->flags & FF_SILENT))
466 for (uint i=0; i<whati->varcount; i++)
467 val_format(&(vv(i)), &fs->buf);
0206c070
MM
468 }
469
efd7c87b
MM
470 INST(FI_FLUSH, 0, 0) {
471 NEVER_CONSTANT;
472 if (!(fs->flags & FF_SILENT))
473 /* After log_commit, the buffer is reset */
474 log_commit(*L_INFO, &fs->buf);
475 }
476
0206c070
MM
477 INST(FI_DIE, 0, 0) {
478 NEVER_CONSTANT;
f634adc7 479 FID_MEMBER(enum filter_return, fret, f1->fret != f2->fret, "%s", filter_return_str(item->fret));
4c553c5a 480
0206c070 481 switch (whati->fret) {
efd7c87b 482 case F_ACCEPT: /* Should take care about turning ACCEPT into MODIFY */
f62a369f 483 case F_ERROR:
efd7c87b 484 case F_REJECT: /* Maybe print complete route along with reason to reject route? */
4c553c5a 485 return fret; /* We have to return now, no more processing. */
f62a369f
JMM
486 default:
487 bug( "unknown return type: Can't happen");
488 }
967b88d9 489 }
bfa15a64
OZ
490
491 INST(FI_RTA_GET, 0, 1) {
f62a369f 492 {
4c553c5a 493 STATIC_ATTR;
f62a369f
JMM
494 ACCESS_RTE;
495 struct rta *rta = (*fs->rte)->attrs;
f62a369f 496
4c553c5a 497 switch (sa.sa_code)
f62a369f 498 {
4c553c5a
MM
499 case SA_FROM: RESULT(sa.f_type, ip, rta->from); break;
500 case SA_GW: RESULT(sa.f_type, ip, rta->nh.gw); break;
501 case SA_NET: RESULT(sa.f_type, net, (*fs->rte)->net->n.addr); break;
502 case SA_PROTO: RESULT(sa.f_type, s, rta->src->proto->name); break;
503 case SA_SOURCE: RESULT(sa.f_type, i, rta->source); break;
504 case SA_SCOPE: RESULT(sa.f_type, i, rta->scope); break;
505 case SA_DEST: RESULT(sa.f_type, i, rta->dest); break;
506 case SA_IFNAME: RESULT(sa.f_type, s, rta->nh.iface ? rta->nh.iface->name : ""); break;
507 case SA_IFINDEX: RESULT(sa.f_type, i, rta->nh.iface ? rta->nh.iface->index : 0); break;
f62a369f
JMM
508
509 default:
4c553c5a 510 bug("Invalid static attribute access (%u/%u)", sa.f_type, sa.sa_code);
f62a369f
JMM
511 }
512 }
967b88d9 513 }
4c553c5a 514
a84b8b6e
MM
515 INST(FI_RTA_SET, 1, 0) {
516 ACCESS_RTE;
517 ARG_ANY(1);
518 STATIC_ATTR;
26194bd6 519 ARG_TYPE(1, sa.f_type);
a84b8b6e
MM
520
521 f_rta_cow(fs);
522 {
523 struct rta *rta = (*fs->rte)->attrs;
524
525 switch (sa.sa_code)
526 {
527 case SA_FROM:
528 rta->from = v1.val.ip;
529 break;
530
531 case SA_GW:
532 {
533 ip_addr ip = v1.val.ip;
534 neighbor *n = neigh_find(rta->src->proto, ip, NULL, 0);
535 if (!n || (n->scope == SCOPE_HOST))
536 runtime( "Invalid gw address" );
537
538 rta->dest = RTD_UNICAST;
539 rta->nh.gw = ip;
540 rta->nh.iface = n->iface;
541 rta->nh.next = NULL;
542 rta->hostentry = NULL;
543 }
544 break;
545
546 case SA_SCOPE:
547 rta->scope = v1.val.i;
548 break;
549
550 case SA_DEST:
551 {
552 int i = v1.val.i;
553 if ((i != RTD_BLACKHOLE) && (i != RTD_UNREACHABLE) && (i != RTD_PROHIBIT))
554 runtime( "Destination can be changed only to blackhole, unreachable or prohibit" );
555
556 rta->dest = i;
557 rta->nh.gw = IPA_NONE;
558 rta->nh.iface = NULL;
559 rta->nh.next = NULL;
560 rta->hostentry = NULL;
561 }
562 break;
563
564 case SA_IFNAME:
565 {
566 struct iface *ifa = if_find_by_name(v1.val.s);
567 if (!ifa)
568 runtime( "Invalid iface name" );
569
570 rta->dest = RTD_UNICAST;
571 rta->nh.gw = IPA_NONE;
572 rta->nh.iface = ifa;
573 rta->nh.next = NULL;
574 rta->hostentry = NULL;
575 }
576 break;
577
578 default:
579 bug("Invalid static attribute access (%u/%u)", sa.f_type, sa.sa_code);
580 }
581 }
582 }
583
4c553c5a
MM
584 INST(FI_EA_GET, 0, 1) { /* Access to extended attributes */
585 DYNAMIC_ATTR;
f62a369f
JMM
586 ACCESS_RTE;
587 ACCESS_EATTRS;
6fbcd891 588 RESULT_TYPE(da.f_type);
f62a369f 589 {
4c553c5a 590 eattr *e = ea_find(*fs->eattrs, da.ea_code);
f62a369f
JMM
591
592 if (!e) {
593 /* A special case: undefined as_path looks like empty as_path */
4c553c5a 594 if (da.type == EAF_TYPE_AS_PATH) {
6fbcd891 595 RESULT_(T_PATH, ad, &null_adata);
f62a369f
JMM
596 break;
597 }
598
599 /* The same special case for int_set */
4c553c5a 600 if (da.type == EAF_TYPE_INT_SET) {
6fbcd891 601 RESULT_(T_CLIST, ad, &null_adata);
f62a369f
JMM
602 break;
603 }
604
605 /* The same special case for ec_set */
4c553c5a 606 if (da.type == EAF_TYPE_EC_SET) {
6fbcd891 607 RESULT_(T_ECLIST, ad, &null_adata);
f62a369f
JMM
608 break;
609 }
610
611 /* The same special case for lc_set */
4c553c5a 612 if (da.type == EAF_TYPE_LC_SET) {
6fbcd891 613 RESULT_(T_LCLIST, ad, &null_adata);
f62a369f
JMM
614 break;
615 }
616
617 /* Undefined value */
f74d1976 618 RESULT_VOID;
f62a369f
JMM
619 break;
620 }
621
622 switch (e->type & EAF_TYPE_MASK) {
623 case EAF_TYPE_INT:
6fbcd891 624 RESULT_(da.f_type, i, e->u.data);
f62a369f
JMM
625 break;
626 case EAF_TYPE_ROUTER_ID:
6fbcd891 627 RESULT_(T_QUAD, i, e->u.data);
f62a369f
JMM
628 break;
629 case EAF_TYPE_OPAQUE:
6fbcd891 630 RESULT_(T_ENUM_EMPTY, i, 0);
f62a369f
JMM
631 break;
632 case EAF_TYPE_IP_ADDRESS:
6fbcd891 633 RESULT_(T_IP, ip, *((ip_addr *) e->u.ptr->data));
f62a369f
JMM
634 break;
635 case EAF_TYPE_AS_PATH:
6fbcd891 636 RESULT_(T_PATH, ad, e->u.ptr);
f62a369f
JMM
637 break;
638 case EAF_TYPE_BITFIELD:
6fbcd891 639 RESULT_(T_BOOL, i, !!(e->u.data & (1u << da.bit)));
f62a369f
JMM
640 break;
641 case EAF_TYPE_INT_SET:
6fbcd891 642 RESULT_(T_CLIST, ad, e->u.ptr);
f62a369f
JMM
643 break;
644 case EAF_TYPE_EC_SET:
6fbcd891 645 RESULT_(T_ECLIST, ad, e->u.ptr);
f62a369f
JMM
646 break;
647 case EAF_TYPE_LC_SET:
6fbcd891 648 RESULT_(T_LCLIST, ad, e->u.ptr);
f62a369f
JMM
649 break;
650 case EAF_TYPE_UNDEF:
f74d1976 651 RESULT_VOID;
f62a369f
JMM
652 break;
653 default:
4c553c5a 654 bug("Unknown dynamic attribute type");
f62a369f
JMM
655 }
656 }
967b88d9 657 }
4c553c5a 658
a84b8b6e
MM
659 INST(FI_EA_SET, 1, 0) {
660 ACCESS_RTE;
661 ACCESS_EATTRS;
662 ARG_ANY(1);
663 DYNAMIC_ATTR;
26194bd6 664 ARG_TYPE(1, da.f_type);
a84b8b6e
MM
665 {
666 struct ea_list *l = lp_alloc(fs->pool, sizeof(struct ea_list) + sizeof(eattr));
667
668 l->next = NULL;
669 l->flags = EALF_SORTED;
670 l->count = 1;
671 l->attrs[0].id = da.ea_code;
672 l->attrs[0].flags = 0;
673 l->attrs[0].type = da.type | EAF_ORIGINATED | EAF_FRESH;
674
675 switch (da.type) {
676 case EAF_TYPE_INT:
a84b8b6e 677 case EAF_TYPE_ROUTER_ID:
a84b8b6e
MM
678 l->attrs[0].u.data = v1.val.i;
679 break;
680
681 case EAF_TYPE_OPAQUE:
682 runtime( "Setting opaque attribute is not allowed" );
683 break;
bfa15a64 684
26194bd6 685 case EAF_TYPE_IP_ADDRESS:;
a84b8b6e
MM
686 int len = sizeof(ip_addr);
687 struct adata *ad = lp_alloc(fs->pool, sizeof(struct adata) + len);
688 ad->length = len;
689 (* (ip_addr *) ad->data) = v1.val.ip;
690 l->attrs[0].u.ptr = ad;
691 break;
bfa15a64 692
a84b8b6e 693 case EAF_TYPE_AS_PATH:
26194bd6
OZ
694 case EAF_TYPE_INT_SET:
695 case EAF_TYPE_EC_SET:
696 case EAF_TYPE_LC_SET:
a84b8b6e
MM
697 l->attrs[0].u.ptr = v1.val.ad;
698 break;
bfa15a64 699
a84b8b6e 700 case EAF_TYPE_BITFIELD:
a84b8b6e
MM
701 {
702 /* First, we have to find the old value */
703 eattr *e = ea_find(*fs->eattrs, da.ea_code);
704 u32 data = e ? e->u.data : 0;
705
706 if (v1.val.i)
707 l->attrs[0].u.data = data | (1u << da.bit);
708 else
709 l->attrs[0].u.data = data & ~(1u << da.bit);
710 }
711 break;
bfa15a64 712
bfa15a64
OZ
713 default:
714 bug("Unknown dynamic attribute type");
a84b8b6e
MM
715 }
716
717 f_rta_cow(fs);
718 l->next = *fs->eattrs;
719 *fs->eattrs = l;
720 }
721 }
722
9b46748d
MM
723 INST(FI_EA_UNSET, 0, 0) {
724 DYNAMIC_ATTR;
725 ACCESS_RTE;
726 ACCESS_EATTRS;
727
728 {
729 struct ea_list *l = lp_alloc(fs->pool, sizeof(struct ea_list) + sizeof(eattr));
730
731 l->next = NULL;
732 l->flags = EALF_SORTED;
733 l->count = 1;
734 l->attrs[0].id = da.ea_code;
735 l->attrs[0].flags = 0;
8d65add6 736 l->attrs[0].type = EAF_TYPE_UNDEF | EAF_ORIGINATED | EAF_FRESH;
9b46748d
MM
737 l->attrs[0].u.data = 0;
738
739 f_rta_cow(fs);
740 l->next = *fs->eattrs;
741 *fs->eattrs = l;
742 }
743 }
744
4c553c5a 745 INST(FI_PREF_GET, 0, 1) {
f62a369f 746 ACCESS_RTE;
4c553c5a 747 RESULT(T_INT, i, (*fs->rte)->pref);
967b88d9 748 }
4c553c5a 749
a84b8b6e
MM
750 INST(FI_PREF_SET, 1, 0) {
751 ACCESS_RTE;
752 ARG(1,T_INT);
753 if (v1.val.i > 0xFFFF)
754 runtime( "Setting preference value out of bounds" );
755 f_rte_cow(fs);
756 (*fs->rte)->pref = v1.val.i;
757 }
758
4c553c5a 759 INST(FI_LENGTH, 1, 1) { /* Get length of */
f62a369f 760 ARG_ANY(1);
f62a369f 761 switch(v1.type) {
4c553c5a
MM
762 case T_NET: RESULT(T_INT, i, net_pxlen(v1.val.net)); break;
763 case T_PATH: RESULT(T_INT, i, as_path_getlen(v1.val.ad)); break;
764 case T_CLIST: RESULT(T_INT, i, int_set_get_size(v1.val.ad)); break;
765 case T_ECLIST: RESULT(T_INT, i, ec_set_get_size(v1.val.ad)); break;
766 case T_LCLIST: RESULT(T_INT, i, lc_set_get_size(v1.val.ad)); break;
f62a369f
JMM
767 default: runtime( "Prefix, path, clist or eclist expected" );
768 }
967b88d9 769 }
4c553c5a 770
ff2ca10c 771 INST(FI_NET_SRC, 1, 1) { /* Get src prefix */
f62a369f 772 ARG(1, T_NET);
f62a369f 773
ff2ca10c 774 net_addr_union *net = (void *) v1.val.net;
b40c0f02 775 net_addr *src = falloc(sizeof(net_addr_ip6));
ff2ca10c
OZ
776 const byte *part;
777
778 switch(v1.val.net->type) {
779 case NET_FLOW4:
780 part = flow4_get_part(&net->flow4, FLOW_TYPE_SRC_PREFIX);
781 if (part)
782 net_fill_ip4(src, flow_read_ip4_part(part), flow_read_pxlen(part));
783 else
784 net_fill_ip4(src, IP4_NONE, 0);
785 break;
786
787 case NET_FLOW6:
788 part = flow6_get_part(&net->flow6, FLOW_TYPE_SRC_PREFIX);
789 if (part)
790 net_fill_ip6(src, flow_read_ip6_part(part), flow_read_pxlen(part));
791 else
792 net_fill_ip6(src, IP6_NONE, 0);
793 break;
794
795 case NET_IP6_SADR:
796 net_fill_ip6(src, net->ip6_sadr.src_prefix, net->ip6_sadr.src_pxlen);
797 break;
798
799 default:
800 runtime( "Flow or SADR expected" );
801 }
f62a369f 802
4c553c5a 803 RESULT(T_NET, net, src);
967b88d9 804 }
4c553c5a 805
ff2ca10c
OZ
806 INST(FI_NET_DST, 1, 1) { /* Get dst prefix */
807 ARG(1, T_NET);
808
809 net_addr_union *net = (void *) v1.val.net;
810 net_addr *dst = falloc(sizeof(net_addr_ip6));
811 const byte *part;
812
813 switch(v1.val.net->type) {
814 case NET_FLOW4:
815 part = flow4_get_part(&net->flow4, FLOW_TYPE_DST_PREFIX);
816 if (part)
817 net_fill_ip4(dst, flow_read_ip4_part(part), flow_read_pxlen(part));
818 else
819 net_fill_ip4(dst, IP4_NONE, 0);
820 break;
821
822 case NET_FLOW6:
823 part = flow6_get_part(&net->flow6, FLOW_TYPE_DST_PREFIX);
824 if (part)
825 net_fill_ip6(dst, flow_read_ip6_part(part), flow_read_pxlen(part));
826 else
827 net_fill_ip6(dst, IP6_NONE, 0);
828 break;
829
830 case NET_IP6_SADR:
831 net_fill_ip6(dst, net->ip6_sadr.dst_prefix, net->ip6_sadr.dst_pxlen);
832 break;
833
834 default:
835 runtime( "Flow or SADR expected" );
836 }
837
838 RESULT(T_NET, net, dst);
839 }
840
4c553c5a 841 INST(FI_ROA_MAXLEN, 1, 1) { /* Get ROA max prefix length */
f62a369f
JMM
842 ARG(1, T_NET);
843 if (!net_is_roa(v1.val.net))
844 runtime( "ROA expected" );
845
4c553c5a 846 RESULT(T_INT, i, (v1.val.net->type == NET_ROA4) ?
f62a369f 847 ((net_addr_roa4 *) v1.val.net)->max_pxlen :
4c553c5a 848 ((net_addr_roa6 *) v1.val.net)->max_pxlen);
967b88d9 849 }
4c553c5a
MM
850
851 INST(FI_ROA_ASN, 1, 1) { /* Get ROA ASN */
f62a369f
JMM
852 ARG(1, T_NET);
853 if (!net_is_roa(v1.val.net))
854 runtime( "ROA expected" );
855
4c553c5a 856 RESULT(T_INT, i, (v1.val.net->type == NET_ROA4) ?
f62a369f 857 ((net_addr_roa4 *) v1.val.net)->asn :
4c553c5a 858 ((net_addr_roa6 *) v1.val.net)->asn);
967b88d9 859 }
4c553c5a
MM
860
861 INST(FI_IP, 1, 1) { /* Convert prefix to ... */
f62a369f 862 ARG(1, T_NET);
4c553c5a 863 RESULT(T_IP, ip, net_prefix(v1.val.net));
967b88d9 864 }
4c553c5a
MM
865
866 INST(FI_ROUTE_DISTINGUISHER, 1, 1) {
f62a369f 867 ARG(1, T_NET);
f62a369f
JMM
868 if (!net_is_vpn(v1.val.net))
869 runtime( "VPN address expected" );
4c553c5a 870 RESULT(T_RD, ec, net_rd(v1.val.net));
967b88d9 871 }
f62a369f 872
4c553c5a
MM
873 INST(FI_AS_PATH_FIRST, 1, 1) { /* Get first ASN from AS PATH */
874 ARG(1, T_PATH);
10c4cd96 875 u32 as = 0;
f62a369f 876 as_path_get_first(v1.val.ad, &as);
4c553c5a 877 RESULT(T_INT, i, as);
967b88d9 878 }
f62a369f 879
bfa15a64 880 INST(FI_AS_PATH_LAST, 1, 1) { /* Get last ASN from AS PATH */
4c553c5a 881 ARG(1, T_PATH);
10c4cd96 882 u32 as = 0;
f62a369f 883 as_path_get_last(v1.val.ad, &as);
4c553c5a 884 RESULT(T_INT, i, as);
967b88d9 885 }
4c553c5a
MM
886
887 INST(FI_AS_PATH_LAST_NAG, 1, 1) { /* Get last ASN from non-aggregated part of AS PATH */
f62a369f 888 ARG(1, T_PATH);
4c553c5a
MM
889 RESULT(T_INT, i, as_path_get_last_nonaggregated(v1.val.ad));
890 }
f62a369f 891
a84b8b6e 892 INST(FI_RETURN, 1, 1) {
b40c0f02 893 NEVER_CONSTANT;
a84b8b6e
MM
894 /* Acquire the return value */
895 ARG_ANY(1);
896 uint retpos = fstk->vcnt;
897
898 /* Drop every sub-block including ourselves */
899 while ((fstk->ecnt-- > 0) && !(fstk->estk[fstk->ecnt].emask & FE_RETURN))
900 ;
901
902 /* Now we are at the caller frame; if no such, try to convert to accept/reject. */
903 if (!fstk->ecnt)
904 if (fstk->vstk[retpos].type == T_BOOL)
905 if (fstk->vstk[retpos].val.i)
a84b8b6e
MM
906 return F_ACCEPT;
907 else
908 return F_REJECT;
909 else
910 runtime("Can't return non-bool from non-function");
911
912 /* Set the value stack position, overwriting the former implicit void */
913 fstk->vcnt = fstk->estk[fstk->ecnt].ventry - 1;
914
915 /* Copy the return value */
916 RESULT_VAL(fstk->vstk[retpos]);
917 }
918
4c553c5a 919 INST(FI_CALL, 0, 1) {
b40c0f02 920 NEVER_CONSTANT;
96d757c1 921 SYMBOL;
4f082dfa 922
3f477ccb 923 FID_SAME_BODY()
dfb3eb77 924 if (!(f1->sym->flags & SYM_FLAG_SAME))
3f477ccb
MM
925 return 0;
926 FID_INTERPRET_BODY()
927
96d757c1
JMM
928 /* Push the body on stack */
929 LINEX(sym->function);
ea4f55e3 930 curline.emask |= FE_RETURN;
bfa15a64 931
96d757c1
JMM
932 /* Before this instruction was called, there was the T_VOID
933 * automatic return value pushed on value stack and also
934 * sym->function->args function arguments. Setting the
935 * vbase to point to first argument. */
936 ASSERT(curline.ventry >= sym->function->args);
937 curline.ventry -= sym->function->args;
938 curline.vbase = curline.ventry;
939
940 /* Storage for local variables */
1757a6fc
MM
941 memset(&(fstk->vstk[fstk->vcnt]), 0, sizeof(struct f_val) * sym->function->vars);
942 fstk->vcnt += sym->function->vars;
967b88d9 943 }
4c553c5a
MM
944
945 INST(FI_DROP_RESULT, 1, 0) {
b40c0f02 946 NEVER_CONSTANT;
4c553c5a 947 ARG_ANY(1);
967b88d9 948 }
4c553c5a 949
4c553c5a 950 INST(FI_SWITCH, 1, 0) {
f62a369f 951 ARG_ANY(1);
26bfe59f 952
f634adc7 953 FID_MEMBER(struct f_tree *, tree, [[!same_tree(f1->tree, f2->tree)]], "tree %p", item->tree);
26bfe59f 954
32793ab6
MM
955 const struct f_tree *t = find_tree(tree, &v1);
956 if (!t) {
4c553c5a 957 v1.type = T_VOID;
32793ab6
MM
958 t = find_tree(tree, &v1);
959 if (!t) {
4c553c5a 960 debug( "No else statement?\n");
b40c0f02 961 FID_HIC(,break,return NULL);
f62a369f 962 }
f62a369f 963 }
4c553c5a
MM
964 /* It is actually possible to have t->data NULL */
965
32793ab6 966 LINEX(t->data);
967b88d9 967 }
4c553c5a
MM
968
969 INST(FI_IP_MASK, 2, 1) { /* IP.MASK(val) */
f62a369f
JMM
970 ARG(1, T_IP);
971 ARG(2, T_INT);
4c553c5a 972 RESULT(T_IP, ip, [[ ipa_is_ip4(v1.val.ip) ?
f62a369f 973 ipa_from_ip4(ip4_and(ipa_to_ip4(v1.val.ip), ip4_mkmask(v2.val.i))) :
4c553c5a 974 ipa_from_ip6(ip6_and(ipa_to_ip6(v1.val.ip), ip6_mkmask(v2.val.i))) ]]);
967b88d9 975 }
f62a369f 976
4c553c5a 977 INST(FI_PATH_PREPEND, 2, 1) { /* Path prepend */
f62a369f
JMM
978 ARG(1, T_PATH);
979 ARG(2, T_INT);
b40c0f02 980 RESULT(T_PATH, ad, [[ as_path_prepend(fpool, v1.val.ad, v2.val.i) ]]);
4c553c5a
MM
981 }
982
983 INST(FI_CLIST_ADD, 2, 1) { /* (Extended) Community list add */
984 ARG_ANY(1);
985 ARG_ANY(2);
6fbcd891
OZ
986 RESULT_TYPE(f1->type);
987
4c553c5a
MM
988 if (v1.type == T_PATH)
989 runtime("Can't add to path");
990
991 else if (v1.type == T_CLIST)
992 {
993 /* Community (or cluster) list */
994 struct f_val dummy;
995
996 if ((v2.type == T_PAIR) || (v2.type == T_QUAD))
6fbcd891 997 RESULT_(T_CLIST, ad, [[ int_set_add(fpool, v1.val.ad, v2.val.i) ]]);
4c553c5a
MM
998 /* IP->Quad implicit conversion */
999 else if (val_is_ip4(&v2))
6fbcd891 1000 RESULT_(T_CLIST, ad, [[ int_set_add(fpool, v1.val.ad, ipa_to_u32(v2.val.ip)) ]]);
4c553c5a
MM
1001 else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy))
1002 runtime("Can't add set");
1003 else if (v2.type == T_CLIST)
6fbcd891 1004 RESULT_(T_CLIST, ad, [[ int_set_union(fpool, v1.val.ad, v2.val.ad) ]]);
4c553c5a
MM
1005 else
1006 runtime("Can't add non-pair");
1007 }
f62a369f 1008
4c553c5a
MM
1009 else if (v1.type == T_ECLIST)
1010 {
1011 /* v2.val is either EC or EC-set */
1012 if ((v2.type == T_SET) && eclist_set_type(v2.val.t))
1013 runtime("Can't add set");
1014 else if (v2.type == T_ECLIST)
6fbcd891 1015 RESULT_(T_ECLIST, ad, [[ ec_set_union(fpool, v1.val.ad, v2.val.ad) ]]);
4c553c5a
MM
1016 else if (v2.type != T_EC)
1017 runtime("Can't add non-ec");
1018 else
6fbcd891 1019 RESULT_(T_ECLIST, ad, [[ ec_set_add(fpool, v1.val.ad, v2.val.ec) ]]);
4c553c5a
MM
1020 }
1021
1022 else if (v1.type == T_LCLIST)
1023 {
1024 /* v2.val is either LC or LC-set */
1025 if ((v2.type == T_SET) && lclist_set_type(v2.val.t))
1026 runtime("Can't add set");
1027 else if (v2.type == T_LCLIST)
6fbcd891 1028 RESULT_(T_LCLIST, ad, [[ lc_set_union(fpool, v1.val.ad, v2.val.ad) ]]);
4c553c5a
MM
1029 else if (v2.type != T_LC)
1030 runtime("Can't add non-lc");
1031 else
6fbcd891 1032 RESULT_(T_LCLIST, ad, [[ lc_set_add(fpool, v1.val.ad, v2.val.lc) ]]);
4c553c5a
MM
1033
1034 }
1035
1036 else
1037 runtime("Can't add to non-[e|l]clist");
967b88d9 1038 }
f62a369f 1039
4c553c5a 1040 INST(FI_CLIST_DEL, 2, 1) { /* (Extended) Community list add or delete */
f62a369f
JMM
1041 ARG_ANY(1);
1042 ARG_ANY(2);
6fbcd891
OZ
1043 RESULT_TYPE(f1->type);
1044
f62a369f
JMM
1045 if (v1.type == T_PATH)
1046 {
4c553c5a 1047 const struct f_tree *set = NULL;
f62a369f 1048 u32 key = 0;
f62a369f
JMM
1049
1050 if (v2.type == T_INT)
1051 key = v2.val.i;
1052 else if ((v2.type == T_SET) && (v2.val.t->from.type == T_INT))
1053 set = v2.val.t;
1054 else
1055 runtime("Can't delete non-integer (set)");
1056
6fbcd891 1057 RESULT_(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, set, key, 0) ]]);
f62a369f 1058 }
4c553c5a 1059
f62a369f
JMM
1060 else if (v1.type == T_CLIST)
1061 {
1062 /* Community (or cluster) list */
1063 struct f_val dummy;
f62a369f
JMM
1064
1065 if ((v2.type == T_PAIR) || (v2.type == T_QUAD))
6fbcd891 1066 RESULT_(T_CLIST, ad, [[ int_set_del(fpool, v1.val.ad, v2.val.i) ]]);
f62a369f 1067 /* IP->Quad implicit conversion */
4c553c5a 1068 else if (val_is_ip4(&v2))
6fbcd891 1069 RESULT_(T_CLIST, ad, [[ int_set_del(fpool, v1.val.ad, ipa_to_u32(v2.val.ip)) ]]);
4c553c5a 1070 else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy) || (v2.type == T_CLIST))
6fbcd891 1071 RESULT_(T_CLIST, ad, [[ clist_filter(fpool, v1.val.ad, &v2, 0) ]]);
f62a369f 1072 else
4c553c5a 1073 runtime("Can't delete non-pair");
f62a369f 1074 }
4c553c5a 1075
f62a369f
JMM
1076 else if (v1.type == T_ECLIST)
1077 {
f62a369f 1078 /* v2.val is either EC or EC-set */
4c553c5a 1079 if ((v2.type == T_SET) && eclist_set_type(v2.val.t) || (v2.type == T_ECLIST))
6fbcd891 1080 RESULT_(T_ECLIST, ad, [[ eclist_filter(fpool, v1.val.ad, &v2, 0) ]]);
f62a369f 1081 else if (v2.type != T_EC)
4c553c5a
MM
1082 runtime("Can't delete non-ec");
1083 else
6fbcd891 1084 RESULT_(T_ECLIST, ad, [[ ec_set_del(fpool, v1.val.ad, v2.val.ec) ]]);
f62a369f 1085 }
4c553c5a 1086
f62a369f
JMM
1087 else if (v1.type == T_LCLIST)
1088 {
f62a369f 1089 /* v2.val is either LC or LC-set */
4c553c5a 1090 if ((v2.type == T_SET) && lclist_set_type(v2.val.t) || (v2.type == T_LCLIST))
6fbcd891 1091 RESULT_(T_LCLIST, ad, [[ lclist_filter(fpool, v1.val.ad, &v2, 0) ]]);
f62a369f 1092 else if (v2.type != T_LC)
4c553c5a
MM
1093 runtime("Can't delete non-lc");
1094 else
6fbcd891 1095 RESULT_(T_LCLIST, ad, [[ lc_set_del(fpool, v1.val.ad, v2.val.lc) ]]);
4c553c5a 1096 }
f62a369f 1097
4c553c5a
MM
1098 else
1099 runtime("Can't delete in non-[e|l]clist");
1100 }
f62a369f 1101
4c553c5a
MM
1102 INST(FI_CLIST_FILTER, 2, 1) { /* (Extended) Community list add or delete */
1103 ARG_ANY(1);
1104 ARG_ANY(2);
6fbcd891
OZ
1105 RESULT_TYPE(f1->type);
1106
4c553c5a
MM
1107 if (v1.type == T_PATH)
1108 {
1109 u32 key = 0;
f62a369f 1110
4c553c5a 1111 if ((v2.type == T_SET) && (v2.val.t->from.type == T_INT))
6fbcd891 1112 RESULT_(T_PATH, ad, [[ as_path_filter(fpool, v1.val.ad, v2.val.t, key, 1) ]]);
4c553c5a
MM
1113 else
1114 runtime("Can't filter integer");
f62a369f 1115 }
f62a369f 1116
4c553c5a
MM
1117 else if (v1.type == T_CLIST)
1118 {
1119 /* Community (or cluster) list */
1120 struct f_val dummy;
f62a369f 1121
4c553c5a 1122 if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy) || (v2.type == T_CLIST))
6fbcd891 1123 RESULT_(T_CLIST, ad, [[ clist_filter(fpool, v1.val.ad, &v2, 1) ]]);
4c553c5a
MM
1124 else
1125 runtime("Can't filter pair");
1126 }
1127
1128 else if (v1.type == T_ECLIST)
f62a369f 1129 {
4c553c5a
MM
1130 /* v2.val is either EC or EC-set */
1131 if ((v2.type == T_SET) && eclist_set_type(v2.val.t) || (v2.type == T_ECLIST))
6fbcd891 1132 RESULT_(T_ECLIST, ad, [[ eclist_filter(fpool, v1.val.ad, &v2, 1) ]]);
4c553c5a
MM
1133 else
1134 runtime("Can't filter ec");
1135 }
f62a369f 1136
4c553c5a
MM
1137 else if (v1.type == T_LCLIST)
1138 {
1139 /* v2.val is either LC or LC-set */
1140 if ((v2.type == T_SET) && lclist_set_type(v2.val.t) || (v2.type == T_LCLIST))
6fbcd891 1141 RESULT_(T_LCLIST, ad, [[ lclist_filter(fpool, v1.val.ad, &v2, 1) ]]);
4c553c5a
MM
1142 else
1143 runtime("Can't filter lc");
f62a369f 1144 }
4c553c5a 1145
f62a369f 1146 else
4c553c5a
MM
1147 runtime("Can't filter non-[e|l]clist");
1148 }
f62a369f 1149
4c553c5a 1150 INST(FI_ROA_CHECK_IMPLICIT, 0, 1) { /* ROA Check */
b40c0f02 1151 NEVER_CONSTANT;
4c553c5a 1152 RTC(1);
b40c0f02 1153 struct rtable *table = rtc->table;
4c553c5a
MM
1154 ACCESS_RTE;
1155 ACCESS_EATTRS;
1156 const net_addr *net = (*fs->rte)->net->n.addr;
f62a369f 1157
4c553c5a
MM
1158 /* We ignore temporary attributes, probably not a problem here */
1159 /* 0x02 is a value of BA_AS_PATH, we don't want to include BGP headers */
1160 eattr *e = ea_find(*fs->eattrs, EA_CODE(PROTOCOL_BGP, 0x02));
f62a369f 1161
4c553c5a
MM
1162 if (!e || ((e->type & EAF_TYPE_MASK) != EAF_TYPE_AS_PATH))
1163 runtime("Missing AS_PATH attribute");
1164
1165 u32 as = 0;
1166 as_path_get_last(e->u.ptr, &as);
f62a369f 1167
f62a369f
JMM
1168 if (!table)
1169 runtime("Missing ROA table");
1170
1171 if (table->addr_type != NET_ROA4 && table->addr_type != NET_ROA6)
1172 runtime("Table type must be either ROA4 or ROA6");
1173
4c553c5a
MM
1174 if (table->addr_type != (net->type == NET_IP4 ? NET_ROA4 : NET_ROA6))
1175 RESULT(T_ENUM_ROA, i, ROA_UNKNOWN); /* Prefix and table type mismatch */
1176 else
1177 RESULT(T_ENUM_ROA, i, [[ net_roa_check(table, net, as) ]]);
1178 }
1179
1180 INST(FI_ROA_CHECK_EXPLICIT, 2, 1) { /* ROA Check */
b40c0f02 1181 NEVER_CONSTANT;
4c553c5a
MM
1182 ARG(1, T_NET);
1183 ARG(2, T_INT);
1184 RTC(3);
b40c0f02 1185 struct rtable *table = rtc->table;
4c553c5a
MM
1186
1187 u32 as = v2.val.i;
1188
1189 if (!table)
1190 runtime("Missing ROA table");
1191
1192 if (table->addr_type != NET_ROA4 && table->addr_type != NET_ROA6)
1193 runtime("Table type must be either ROA4 or ROA6");
f62a369f
JMM
1194
1195 if (table->addr_type != (v1.val.net->type == NET_IP4 ? NET_ROA4 : NET_ROA6))
4c553c5a 1196 RESULT(T_ENUM_ROA, i, ROA_UNKNOWN); /* Prefix and table type mismatch */
f62a369f 1197 else
4c553c5a 1198 RESULT(T_ENUM_ROA, i, [[ net_roa_check(table, v1.val.net, as) ]]);
f62a369f 1199
967b88d9 1200 }
f62a369f 1201
4c553c5a 1202 INST(FI_FORMAT, 1, 0) { /* Format */
f62a369f 1203 ARG_ANY(1);
b40c0f02 1204 RESULT(T_STRING, s, val_format_str(fpool, &v1));
967b88d9 1205 }
f62a369f 1206
4c553c5a 1207 INST(FI_ASSERT, 1, 0) { /* Birdtest Assert */
b40c0f02 1208 NEVER_CONSTANT;
f62a369f 1209 ARG(1, T_BOOL);
550a6488 1210
f634adc7 1211 FID_MEMBER(char *, s, [[strcmp(f1->s, f2->s)]], "string %s", item->s);
b40c0f02
MM
1212
1213 ASSERT(s);
26bfe59f 1214
c0e958e0
MM
1215 if (!bt_assert_hook)
1216 runtime("No bt_assert hook registered, can't assert");
1217
b40c0f02 1218 bt_assert_hook(v1.val.i, what);
967b88d9 1219 }