]> git.ipfire.org Git - thirdparty/bird.git/blame - filter/f-inst.c
Filter: Resolving of defined constants in config time
[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 *
e1ac6f1e
MM
10 * Filter instructions. You shall define your instruction only here
11 * and nowhere else.
12 *
13 * Beware. This file is interpreted by M4 macros. These macros
14 * may be more stupid than you could imagine. If something strange
15 * happens after changing this file, compare the results before and
16 * after your change (see the Makefile to find out where the results are)
17 * and see what really happened.
18 *
19 * This file is not directly a C source code -> it is a generator input
20 * for several C sources; every instruction block gets expanded into many
21 * different places.
22 *
26bfe59f
MM
23 * All the arguments are processed literally; if you need an argument including comma,
24 * you have to quote it by [[ ... ]]
25 *
e1ac6f1e
MM
26 * What is the syntax here?
27 * m4_dnl INST(FI_NOP, in, out) { enum value, input args, output args
28 * m4_dnl ARG(num, type); argument, its id (in data fields) and type
29 * m4_dnl ARG_ANY(num); argument with no type check
30 * m4_dnl LINE(num, unused); this argument has to be converted to its own f_line
63f49457 31 * m4_dnl SYMBOL; symbol handed from config
e1ac6f1e
MM
32 * m4_dnl STATIC_ATTR; static attribute definition
33 * m4_dnl DYNAMIC_ATTR; dynamic attribute definition
34 * m4_dnl RTC; route table config
e1ac6f1e
MM
35 * m4_dnl ACCESS_RTE; this instruction needs route
36 * m4_dnl ACCESS_EATTRS; this instruction needs extended attributes
26bfe59f
MM
37 *
38 * m4_dnl FID_MEMBER( custom instruction member
39 * m4_dnl C type, for storage in structs
40 * m4_dnl name in f_inst, how the member is named before linearization
41 * m4_dnl name in f_line_item, how the member is named afterwards
30667d50 42 * m4_dnl comparator for same(), if different, this should be TRUE (CAVEAT)
26bfe59f
MM
43 * m4_dnl dump format string debug -> format string for bvsnprintf
44 * m4_dnl dump format args appropriate args
45 * m4_dnl interpreter body how to deal with this on execution
46 * m4_dnl )
47 *
e1ac6f1e 48 * m4_dnl RESULT(type, union-field, value); putting this on value stack
f74d1976
MM
49 * m4_dnl RESULT_VAL(value-struct); pass the struct f_val directly
50 * m4_dnl RESULT_VOID; return undef
e1ac6f1e
MM
51 * m4_dnl }
52 *
53 * Other code is just copied into the interpreter part.
54 *
55 * If you want to write something really special, see FI_CALL
56 * or FI_CONSTANT or whatever else to see how to use the FID_*
57 * macros.
f62a369f
JMM
58 */
59
60/* Binary operators */
4c553c5a
MM
61 INST(FI_ADD, 2, 1) {
62 ARG(1,T_INT);
63 ARG(2,T_INT);
f74d1976 64 RESULT(T_INT, i, v1.val.i + v2.val.i);
4c553c5a
MM
65 }
66 INST(FI_SUBTRACT, 2, 1) {
67 ARG(1,T_INT);
68 ARG(2,T_INT);
f74d1976 69 RESULT(T_INT, i, v1.val.i - v2.val.i);
4c553c5a
MM
70 }
71 INST(FI_MULTIPLY, 2, 1) {
72 ARG(1,T_INT);
73 ARG(2,T_INT);
f74d1976 74 RESULT(T_INT, i, v1.val.i * v2.val.i);
4c553c5a
MM
75 }
76 INST(FI_DIVIDE, 2, 1) {
77 ARG(1,T_INT);
78 ARG(2,T_INT);
79 if (v2.val.i == 0) runtime( "Mother told me not to divide by 0" );
f74d1976 80 RESULT(T_INT, i, v1.val.i / v2.val.i);
4c553c5a
MM
81 }
82 INST(FI_AND, 1, 1) {
83 ARG(1,T_BOOL);
f74d1976 84 if (v1.val.i)
4c553c5a
MM
85 LINE(2,0);
86 else
f74d1976 87 RESULT_VAL(v1);
967b88d9 88 }
4c553c5a
MM
89 INST(FI_OR, 1, 1) {
90 ARG(1,T_BOOL);
f74d1976 91 if (!v1.val.i)
4c553c5a
MM
92 LINE(2,0);
93 else
f74d1976 94 RESULT_VAL(v1);
967b88d9 95 }
4c553c5a 96 INST(FI_PAIR_CONSTRUCT, 2, 1) {
f62a369f
JMM
97 ARG(1,T_INT);
98 ARG(2,T_INT);
4c553c5a
MM
99 uint u1 = v1.val.i;
100 uint u2 = v2.val.i;
f62a369f
JMM
101 if ((u1 > 0xFFFF) || (u2 > 0xFFFF))
102 runtime( "Can't operate with value out of bounds in pair constructor" );
4c553c5a 103 RESULT(T_PAIR, i, (u1 << 16) | u2);
967b88d9 104 }
4c553c5a
MM
105 INST(FI_EC_CONSTRUCT, 2, 1) {
106 ARG_ANY(1);
107 ARG(2, T_INT);
26bfe59f
MM
108
109 FID_MEMBER(enum ec_subtype, ecs, ecs, f1->ecs != f2->ecs, ec subtype %s, ec_subtype_str(item->ecs), enum ec_subtype ecs = whati->ecs);
f62a369f 110
4c553c5a
MM
111 int check, ipv4_used;
112 u32 key, val;
f62a369f 113
4c553c5a
MM
114 if (v1.type == T_INT) {
115 ipv4_used = 0; key = v1.val.i;
116 }
117 else if (v1.type == T_QUAD) {
118 ipv4_used = 1; key = v1.val.i;
119 }
120 /* IP->Quad implicit conversion */
121 else if (val_is_ip4(&v1)) {
122 ipv4_used = 1; key = ipa_to_u32(v1.val.ip);
123 }
124 else
125 runtime("Argument 1 of instruction FI_EC_CONSTRUCT must be integer or IPv4 address, got 0x%02x");
f62a369f 126
4c553c5a 127 val = v2.val.i;
f62a369f 128
4c553c5a
MM
129 if (ecs == EC_GENERIC) {
130 check = 0; RESULT(T_EC, ec, ec_generic(key, val));
131 }
132 else if (ipv4_used) {
133 check = 1; RESULT(T_EC, ec, ec_ip4(ecs, key, val));
967b88d9 134 }
4c553c5a
MM
135 else if (key < 0x10000) {
136 check = 0; RESULT(T_EC, ec, ec_as2(ecs, key, val));
137 }
138 else {
139 check = 1; RESULT(T_EC, ec, ec_as4(ecs, key, val));
f62a369f
JMM
140 }
141
4c553c5a
MM
142 if (check && (val > 0xFFFF))
143 runtime("Value %u > %u out of bounds in EC constructor", val, 0xFFFF);
144 }
f62a369f 145
4c553c5a
MM
146 INST(FI_LC_CONSTRUCT, 3, 1) {
147 ARG(1, T_INT);
148 ARG(2, T_INT);
149 ARG(3, T_INT);
150 RESULT(T_LC, lc, [[(lcomm) { v1.val.i, v2.val.i, v3.val.i }]]);
151 }
f62a369f 152
4c553c5a
MM
153 INST(FI_PATHMASK_CONSTRUCT, 0, 1) {
154 ARG_ANY(1);
26bfe59f 155 FID_MEMBER(uint, count, count, f1->count != f2->count, number of items %u, item->count);
9b46748d 156
4f082dfa
MM
157 FID_NEW_BODY
158 uint len = 0;
159 uint dyn = 0;
160 for (const struct f_inst *tt = f1; tt; tt = tt->next, len++)
161 if (tt->fi_code != FI_CONSTANT)
162 dyn++;
9b46748d 163
63f49457 164 whati->count = len;
d1039926 165 FID_ALL
9b46748d 166
1757a6fc 167 if (fstk->vcnt < whati->count) /* TODO: make this check systematic */
ea4f55e3 168 runtime("Construction of BGP path mask from %u elements must have at least that number of elements", whati->count);
4c553c5a 169
ea4f55e3
MM
170 struct f_path_mask *pm = lp_alloc(fs->pool, sizeof(struct f_path_mask) + whati->count * sizeof(struct f_path_mask_item));
171 for (uint i=0; i<whati->count; i++) {
1757a6fc 172#define pv fstk->vstk[fstk->vcnt - whati->count + i]
4c553c5a
MM
173 switch (pv.type) {
174 case T_PATH_MASK_ITEM:
175 pm->item[i] = pv.val.pmi;
176 break;
177 case T_INT:
178 pm->item[i] = (struct f_path_mask_item) {
179 .asn = pv.val.i,
180 .kind = PM_ASN,
181 };
182 break;
183 default:
184 runtime( "Error resolving path mask template: value not an integer" );
185 }
f62a369f
JMM
186 }
187
1757a6fc 188 fstk->vcnt -= whati->count;
ea4f55e3 189 pm->len = whati->count;
f62a369f 190
4c553c5a
MM
191 RESULT(T_PATH_MASK, path_mask, pm);
192 }
f62a369f
JMM
193
194/* Relational operators */
195
4c553c5a 196 INST(FI_NEQ, 2, 1) {
c5774939
MM
197 ARG_ANY(1);
198 ARG_ANY(2);
4c553c5a 199 RESULT(T_BOOL, i, !val_same(&v1, &v2));
967b88d9 200 }
f62a369f 201
4c553c5a 202 INST(FI_EQ, 2, 1) {
c5774939
MM
203 ARG_ANY(1);
204 ARG_ANY(2);
4c553c5a 205 RESULT(T_BOOL, i, val_same(&v1, &v2));
967b88d9 206 }
c5774939 207
4c553c5a 208 INST(FI_LT, 2, 1) {
c5774939
MM
209 ARG_ANY(1);
210 ARG_ANY(2);
4c553c5a 211 int i = val_compare(&v1, &v2);
52893045 212 if (i == F_CMP_ERROR)
c5774939 213 runtime( "Can't compare values of incompatible types" );
4c553c5a 214 RESULT(T_BOOL, i, (i == -1));
967b88d9 215 }
f62a369f 216
4c553c5a 217 INST(FI_LTE, 2, 1) {
c5774939
MM
218 ARG_ANY(1);
219 ARG_ANY(2);
4c553c5a 220 int i = val_compare(&v1, &v2);
52893045 221 if (i == F_CMP_ERROR)
c5774939 222 runtime( "Can't compare values of incompatible types" );
4c553c5a 223 RESULT(T_BOOL, i, (i != 1));
967b88d9 224 }
f62a369f 225
4c553c5a
MM
226 INST(FI_NOT, 1, 1) {
227 ARG(1,T_BOOL);
228 RESULT(T_BOOL, i, !v1.val.i);
967b88d9 229 }
f62a369f 230
4c553c5a 231 INST(FI_MATCH, 2, 1) {
f62a369f
JMM
232 ARG_ANY(1);
233 ARG_ANY(2);
4c553c5a 234 int i = val_in_range(&v1, &v2);
52893045 235 if (i == F_CMP_ERROR)
f62a369f 236 runtime( "~ applied on unknown type pair" );
4c553c5a 237 RESULT(T_BOOL, i, !!i);
967b88d9 238 }
f62a369f 239
4c553c5a 240 INST(FI_NOT_MATCH, 2, 1) {
f62a369f
JMM
241 ARG_ANY(1);
242 ARG_ANY(2);
4c553c5a 243 int i = val_in_range(&v1, &v2);
fe503c7c 244 if (i == F_CMP_ERROR)
f62a369f 245 runtime( "!~ applied on unknown type pair" );
4c553c5a 246 RESULT(T_BOOL, i, !i);
967b88d9 247 }
f62a369f 248
4c553c5a 249 INST(FI_DEFINED, 1, 1) {
f62a369f 250 ARG_ANY(1);
4c553c5a 251 RESULT(T_BOOL, i, (v1.type != T_VOID) && !undef_value(v1));
967b88d9 252 }
4c553c5a
MM
253
254 INST(FI_TYPE, 1, 1) {
f62a369f
JMM
255 ARG_ANY(1); /* There may be more types supporting this operation */
256 switch (v1.type)
257 {
258 case T_NET:
4c553c5a 259 RESULT(T_ENUM_NETTYPE, i, v1.val.net->type);
f62a369f
JMM
260 break;
261 default:
262 runtime( "Can't determine type of this item" );
263 }
967b88d9 264 }
4c553c5a
MM
265
266 INST(FI_IS_V4, 1, 1) {
f62a369f 267 ARG(1, T_IP);
4c553c5a 268 RESULT(T_BOOL, i, ipa_is_ip4(v1.val.ip));
967b88d9 269 }
f62a369f 270
4c553c5a 271 /* Set to indirect value prepared in v1 */
96d757c1 272 INST(FI_VAR_SET, 1, 0) {
63f49457
MM
273 ARG_ANY(1);
274 SYMBOL;
4c553c5a 275 if ((sym->class != (SYM_VARIABLE | v1.type)) && (v1.type != T_VOID))
f62a369f
JMM
276 {
277 /* IP->Quad implicit conversion */
4c553c5a 278 if ((sym->class == (SYM_VARIABLE | T_QUAD)) && val_is_ip4(&v1))
96d757c1 279 v1 = (struct f_val) {
4c553c5a
MM
280 .type = T_QUAD,
281 .val.i = ipa_to_u32(v1.val.ip),
96d757c1
JMM
282 };
283 else
284 runtime( "Assigning to variable of incompatible type" );
f62a369f 285 }
96d757c1 286
1757a6fc 287 fstk->vstk[curline.vbase + sym->offset] = v1;
96d757c1
JMM
288 }
289
290 INST(FI_VAR_GET, 0, 1) {
26bfe59f 291 SYMBOL;
f74d1976 292 RESULT_VAL(fstk->vstk[curline.vbase + sym->offset]);
967b88d9 293 }
f62a369f 294
7f0ac737 295 /* some constants have value in a[1], some in *a[0].p, strange. */
4c553c5a 296 INST(FI_CONSTANT, 0, 1) { /* integer (or simple type) constant, string, set, or prefix_set */
30667d50
MM
297 FID_MEMBER(
298 struct f_val,
299 val,
300 val,
301 [[ !val_same(&(f1->val), &(f2->val)) ]],
302 value %s,
303 val_dump(&(item->val))
304 );
ea4f55e3 305
63f49457 306 RESULT_VAL(whati->val);
967b88d9 307 }
4c553c5a 308 INST(FI_PRINT, 1, 0) {
f62a369f 309 ARG_ANY(1);
4c553c5a 310 val_format(&(v1), &fs->buf);
967b88d9 311 }
4c553c5a
MM
312 INST(FI_CONDITION, 1, 0) {
313 ARG(1, T_BOOL);
224b77d4 314 if (res.val.i)
4c553c5a 315 LINE(2,0);
224b77d4 316 else
4c553c5a 317 LINE(3,1);
967b88d9 318 }
4c553c5a 319 INST(FI_PRINT_AND_DIE, 0, 0) {
23e3b1e6 320 FID_LINEARIZE_BODY
dd4d4095
MM
321 {
322 uint opos = pos;
d1039926 323 FID_ALL
9b46748d
MM
324
325 ARG_ANY(1);
326
23e3b1e6 327 FID_LINEARIZE_BODY
dd4d4095
MM
328 if (opos < pos)
329 dest->items[pos].flags |= FIF_PRINTED;
330 }
d1039926 331 FID_ALL
4c553c5a 332
26bfe59f 333 FID_MEMBER(enum filter_return, fret, fret, f1->fret != f2->fret, %s, filter_return_str(item->fret), enum filter_return fret = whati->fret);
4c553c5a
MM
334
335 if ((fret == F_NOP || (fret != F_NONL && (what->flags & FIF_PRINTED))) &&
f62a369f
JMM
336 !(fs->flags & FF_SILENT))
337 log_commit(*L_INFO, &fs->buf);
338
4c553c5a 339 switch (fret) {
f62a369f
JMM
340 case F_QUITBIRD:
341 die( "Filter asked me to die" );
342 case F_ACCEPT:
343 /* Should take care about turning ACCEPT into MODIFY */
344 case F_ERROR:
345 case F_REJECT: /* FIXME (noncritical) Should print complete route along with reason to reject route */
4c553c5a 346 return fret; /* We have to return now, no more processing. */
f62a369f
JMM
347 case F_NONL:
348 case F_NOP:
349 break;
350 default:
351 bug( "unknown return type: Can't happen");
352 }
967b88d9 353 }
4c553c5a
MM
354
355 INST(FI_RTA_GET, 0, 1) { /* rta access */
f62a369f 356 {
4c553c5a 357 STATIC_ATTR;
f62a369f
JMM
358 ACCESS_RTE;
359 struct rta *rta = (*fs->rte)->attrs;
f62a369f 360
4c553c5a 361 switch (sa.sa_code)
f62a369f 362 {
4c553c5a
MM
363 case SA_FROM: RESULT(sa.f_type, ip, rta->from); break;
364 case SA_GW: RESULT(sa.f_type, ip, rta->nh.gw); break;
365 case SA_NET: RESULT(sa.f_type, net, (*fs->rte)->net->n.addr); break;
366 case SA_PROTO: RESULT(sa.f_type, s, rta->src->proto->name); break;
367 case SA_SOURCE: RESULT(sa.f_type, i, rta->source); break;
368 case SA_SCOPE: RESULT(sa.f_type, i, rta->scope); break;
369 case SA_DEST: RESULT(sa.f_type, i, rta->dest); break;
370 case SA_IFNAME: RESULT(sa.f_type, s, rta->nh.iface ? rta->nh.iface->name : ""); break;
371 case SA_IFINDEX: RESULT(sa.f_type, i, rta->nh.iface ? rta->nh.iface->index : 0); break;
f62a369f
JMM
372
373 default:
4c553c5a 374 bug("Invalid static attribute access (%u/%u)", sa.f_type, sa.sa_code);
f62a369f
JMM
375 }
376 }
967b88d9 377 }
4c553c5a 378
a84b8b6e
MM
379 INST(FI_RTA_SET, 1, 0) {
380 ACCESS_RTE;
381 ARG_ANY(1);
382 STATIC_ATTR;
383 if (sa.f_type != v1.type)
384 runtime( "Attempt to set static attribute to incompatible type" );
385
386 f_rta_cow(fs);
387 {
388 struct rta *rta = (*fs->rte)->attrs;
389
390 switch (sa.sa_code)
391 {
392 case SA_FROM:
393 rta->from = v1.val.ip;
394 break;
395
396 case SA_GW:
397 {
398 ip_addr ip = v1.val.ip;
399 neighbor *n = neigh_find(rta->src->proto, ip, NULL, 0);
400 if (!n || (n->scope == SCOPE_HOST))
401 runtime( "Invalid gw address" );
402
403 rta->dest = RTD_UNICAST;
404 rta->nh.gw = ip;
405 rta->nh.iface = n->iface;
406 rta->nh.next = NULL;
407 rta->hostentry = NULL;
408 }
409 break;
410
411 case SA_SCOPE:
412 rta->scope = v1.val.i;
413 break;
414
415 case SA_DEST:
416 {
417 int i = v1.val.i;
418 if ((i != RTD_BLACKHOLE) && (i != RTD_UNREACHABLE) && (i != RTD_PROHIBIT))
419 runtime( "Destination can be changed only to blackhole, unreachable or prohibit" );
420
421 rta->dest = i;
422 rta->nh.gw = IPA_NONE;
423 rta->nh.iface = NULL;
424 rta->nh.next = NULL;
425 rta->hostentry = NULL;
426 }
427 break;
428
429 case SA_IFNAME:
430 {
431 struct iface *ifa = if_find_by_name(v1.val.s);
432 if (!ifa)
433 runtime( "Invalid iface name" );
434
435 rta->dest = RTD_UNICAST;
436 rta->nh.gw = IPA_NONE;
437 rta->nh.iface = ifa;
438 rta->nh.next = NULL;
439 rta->hostentry = NULL;
440 }
441 break;
442
443 default:
444 bug("Invalid static attribute access (%u/%u)", sa.f_type, sa.sa_code);
445 }
446 }
447 }
448
4c553c5a
MM
449 INST(FI_EA_GET, 0, 1) { /* Access to extended attributes */
450 DYNAMIC_ATTR;
f62a369f
JMM
451 ACCESS_RTE;
452 ACCESS_EATTRS;
453 {
4c553c5a 454 eattr *e = ea_find(*fs->eattrs, da.ea_code);
f62a369f
JMM
455
456 if (!e) {
457 /* A special case: undefined as_path looks like empty as_path */
4c553c5a
MM
458 if (da.type == EAF_TYPE_AS_PATH) {
459 RESULT(T_PATH, ad, &null_adata);
f62a369f
JMM
460 break;
461 }
462
463 /* The same special case for int_set */
4c553c5a
MM
464 if (da.type == EAF_TYPE_INT_SET) {
465 RESULT(T_CLIST, ad, &null_adata);
f62a369f
JMM
466 break;
467 }
468
469 /* The same special case for ec_set */
4c553c5a
MM
470 if (da.type == EAF_TYPE_EC_SET) {
471 RESULT(T_ECLIST, ad, &null_adata);
f62a369f
JMM
472 break;
473 }
474
475 /* The same special case for lc_set */
4c553c5a
MM
476 if (da.type == EAF_TYPE_LC_SET) {
477 RESULT(T_LCLIST, ad, &null_adata);
f62a369f
JMM
478 break;
479 }
480
481 /* Undefined value */
f74d1976 482 RESULT_VOID;
f62a369f
JMM
483 break;
484 }
485
486 switch (e->type & EAF_TYPE_MASK) {
487 case EAF_TYPE_INT:
4c553c5a 488 RESULT(da.f_type, i, e->u.data);
f62a369f
JMM
489 break;
490 case EAF_TYPE_ROUTER_ID:
4c553c5a 491 RESULT(T_QUAD, i, e->u.data);
f62a369f
JMM
492 break;
493 case EAF_TYPE_OPAQUE:
4c553c5a 494 RESULT(T_ENUM_EMPTY, i, 0);
f62a369f
JMM
495 break;
496 case EAF_TYPE_IP_ADDRESS:
4c553c5a 497 RESULT(T_IP, ip, *((ip_addr *) e->u.ptr->data));
f62a369f
JMM
498 break;
499 case EAF_TYPE_AS_PATH:
4c553c5a 500 RESULT(T_PATH, ad, e->u.ptr);
f62a369f
JMM
501 break;
502 case EAF_TYPE_BITFIELD:
4c553c5a 503 RESULT(T_BOOL, i, !!(e->u.data & (1u << da.bit)));
f62a369f
JMM
504 break;
505 case EAF_TYPE_INT_SET:
4c553c5a 506 RESULT(T_CLIST, ad, e->u.ptr);
f62a369f
JMM
507 break;
508 case EAF_TYPE_EC_SET:
4c553c5a 509 RESULT(T_ECLIST, ad, e->u.ptr);
f62a369f
JMM
510 break;
511 case EAF_TYPE_LC_SET:
4c553c5a 512 RESULT(T_LCLIST, ad, e->u.ptr);
f62a369f
JMM
513 break;
514 case EAF_TYPE_UNDEF:
f74d1976 515 RESULT_VOID;
f62a369f
JMM
516 break;
517 default:
4c553c5a 518 bug("Unknown dynamic attribute type");
f62a369f
JMM
519 }
520 }
967b88d9 521 }
4c553c5a 522
a84b8b6e
MM
523 INST(FI_EA_SET, 1, 0) {
524 ACCESS_RTE;
525 ACCESS_EATTRS;
526 ARG_ANY(1);
527 DYNAMIC_ATTR;
528 {
529 struct ea_list *l = lp_alloc(fs->pool, sizeof(struct ea_list) + sizeof(eattr));
530
531 l->next = NULL;
532 l->flags = EALF_SORTED;
533 l->count = 1;
534 l->attrs[0].id = da.ea_code;
535 l->attrs[0].flags = 0;
536 l->attrs[0].type = da.type | EAF_ORIGINATED | EAF_FRESH;
537
538 switch (da.type) {
539 case EAF_TYPE_INT:
540 if (v1.type != da.f_type)
541 runtime( "Setting int attribute to non-int value" );
542 l->attrs[0].u.data = v1.val.i;
543 break;
544
545 case EAF_TYPE_ROUTER_ID:
546 /* IP->Quad implicit conversion */
547 if (val_is_ip4(&v1)) {
548 l->attrs[0].u.data = ipa_to_u32(v1.val.ip);
549 break;
550 }
551 /* T_INT for backward compatibility */
552 if ((v1.type != T_QUAD) && (v1.type != T_INT))
553 runtime( "Setting quad attribute to non-quad value" );
554 l->attrs[0].u.data = v1.val.i;
555 break;
556
557 case EAF_TYPE_OPAQUE:
558 runtime( "Setting opaque attribute is not allowed" );
559 break;
560 case EAF_TYPE_IP_ADDRESS:
561 if (v1.type != T_IP)
562 runtime( "Setting ip attribute to non-ip value" );
563 int len = sizeof(ip_addr);
564 struct adata *ad = lp_alloc(fs->pool, sizeof(struct adata) + len);
565 ad->length = len;
566 (* (ip_addr *) ad->data) = v1.val.ip;
567 l->attrs[0].u.ptr = ad;
568 break;
569 case EAF_TYPE_AS_PATH:
570 if (v1.type != T_PATH)
571 runtime( "Setting path attribute to non-path value" );
572 l->attrs[0].u.ptr = v1.val.ad;
573 break;
574 case EAF_TYPE_BITFIELD:
575 if (v1.type != T_BOOL)
576 runtime( "Setting bit in bitfield attribute to non-bool value" );
577 {
578 /* First, we have to find the old value */
579 eattr *e = ea_find(*fs->eattrs, da.ea_code);
580 u32 data = e ? e->u.data : 0;
581
582 if (v1.val.i)
583 l->attrs[0].u.data = data | (1u << da.bit);
584 else
585 l->attrs[0].u.data = data & ~(1u << da.bit);
586 }
587 break;
588 case EAF_TYPE_INT_SET:
589 if (v1.type != T_CLIST)
590 runtime( "Setting clist attribute to non-clist value" );
591 l->attrs[0].u.ptr = v1.val.ad;
592 break;
593 case EAF_TYPE_EC_SET:
594 if (v1.type != T_ECLIST)
595 runtime( "Setting eclist attribute to non-eclist value" );
596 l->attrs[0].u.ptr = v1.val.ad;
597 break;
598 case EAF_TYPE_LC_SET:
599 if (v1.type != T_LCLIST)
600 runtime( "Setting lclist attribute to non-lclist value" );
601 l->attrs[0].u.ptr = v1.val.ad;
602 break;
603 default: bug("Unknown type in e,S");
604 }
605
606 f_rta_cow(fs);
607 l->next = *fs->eattrs;
608 *fs->eattrs = l;
609 }
610 }
611
9b46748d
MM
612 INST(FI_EA_UNSET, 0, 0) {
613 DYNAMIC_ATTR;
614 ACCESS_RTE;
615 ACCESS_EATTRS;
616
617 {
618 struct ea_list *l = lp_alloc(fs->pool, sizeof(struct ea_list) + sizeof(eattr));
619
620 l->next = NULL;
621 l->flags = EALF_SORTED;
622 l->count = 1;
623 l->attrs[0].id = da.ea_code;
624 l->attrs[0].flags = 0;
8d65add6 625 l->attrs[0].type = EAF_TYPE_UNDEF | EAF_ORIGINATED | EAF_FRESH;
9b46748d
MM
626 l->attrs[0].u.data = 0;
627
628 f_rta_cow(fs);
629 l->next = *fs->eattrs;
630 *fs->eattrs = l;
631 }
632 }
633
4c553c5a 634 INST(FI_PREF_GET, 0, 1) {
f62a369f 635 ACCESS_RTE;
4c553c5a 636 RESULT(T_INT, i, (*fs->rte)->pref);
967b88d9 637 }
4c553c5a 638
a84b8b6e
MM
639 INST(FI_PREF_SET, 1, 0) {
640 ACCESS_RTE;
641 ARG(1,T_INT);
642 if (v1.val.i > 0xFFFF)
643 runtime( "Setting preference value out of bounds" );
644 f_rte_cow(fs);
645 (*fs->rte)->pref = v1.val.i;
646 }
647
4c553c5a 648 INST(FI_LENGTH, 1, 1) { /* Get length of */
f62a369f 649 ARG_ANY(1);
f62a369f 650 switch(v1.type) {
4c553c5a
MM
651 case T_NET: RESULT(T_INT, i, net_pxlen(v1.val.net)); break;
652 case T_PATH: RESULT(T_INT, i, as_path_getlen(v1.val.ad)); break;
653 case T_CLIST: RESULT(T_INT, i, int_set_get_size(v1.val.ad)); break;
654 case T_ECLIST: RESULT(T_INT, i, ec_set_get_size(v1.val.ad)); break;
655 case T_LCLIST: RESULT(T_INT, i, lc_set_get_size(v1.val.ad)); break;
f62a369f
JMM
656 default: runtime( "Prefix, path, clist or eclist expected" );
657 }
967b88d9 658 }
4c553c5a
MM
659
660 INST(FI_SADR_SRC, 1, 1) { /* Get SADR src prefix */
f62a369f
JMM
661 ARG(1, T_NET);
662 if (!net_is_sadr(v1.val.net))
663 runtime( "SADR expected" );
664
4c553c5a
MM
665 net_addr_ip6_sadr *net = (void *) v1.val.net;
666 net_addr *src = lp_alloc(fs->pool, sizeof(net_addr_ip6));
667 net_fill_ip6(src, net->src_prefix, net->src_pxlen);
f62a369f 668
4c553c5a 669 RESULT(T_NET, net, src);
967b88d9 670 }
4c553c5a
MM
671
672 INST(FI_ROA_MAXLEN, 1, 1) { /* Get ROA max prefix length */
f62a369f
JMM
673 ARG(1, T_NET);
674 if (!net_is_roa(v1.val.net))
675 runtime( "ROA expected" );
676
4c553c5a 677 RESULT(T_INT, i, (v1.val.net->type == NET_ROA4) ?
f62a369f 678 ((net_addr_roa4 *) v1.val.net)->max_pxlen :
4c553c5a 679 ((net_addr_roa6 *) v1.val.net)->max_pxlen);
967b88d9 680 }
4c553c5a
MM
681
682 INST(FI_ROA_ASN, 1, 1) { /* Get ROA ASN */
f62a369f
JMM
683 ARG(1, T_NET);
684 if (!net_is_roa(v1.val.net))
685 runtime( "ROA expected" );
686
4c553c5a 687 RESULT(T_INT, i, (v1.val.net->type == NET_ROA4) ?
f62a369f 688 ((net_addr_roa4 *) v1.val.net)->asn :
4c553c5a 689 ((net_addr_roa6 *) v1.val.net)->asn);
967b88d9 690 }
4c553c5a
MM
691
692 INST(FI_IP, 1, 1) { /* Convert prefix to ... */
f62a369f 693 ARG(1, T_NET);
4c553c5a 694 RESULT(T_IP, ip, net_prefix(v1.val.net));
967b88d9 695 }
4c553c5a
MM
696
697 INST(FI_ROUTE_DISTINGUISHER, 1, 1) {
f62a369f 698 ARG(1, T_NET);
f62a369f
JMM
699 if (!net_is_vpn(v1.val.net))
700 runtime( "VPN address expected" );
4c553c5a 701 RESULT(T_RD, ec, net_rd(v1.val.net));
967b88d9 702 }
f62a369f 703
4c553c5a
MM
704 INST(FI_AS_PATH_FIRST, 1, 1) { /* Get first ASN from AS PATH */
705 ARG(1, T_PATH);
706 int as = 0;
f62a369f 707 as_path_get_first(v1.val.ad, &as);
4c553c5a 708 RESULT(T_INT, i, as);
967b88d9 709 }
f62a369f 710
4c553c5a
MM
711 INST(FI_AS_PATH_LAST, 1, 1) { /* Get last ASN from AS PATH */
712 ARG(1, T_PATH);
713 int as = 0;
f62a369f 714 as_path_get_last(v1.val.ad, &as);
4c553c5a 715 RESULT(T_INT, i, as);
967b88d9 716 }
4c553c5a
MM
717
718 INST(FI_AS_PATH_LAST_NAG, 1, 1) { /* Get last ASN from non-aggregated part of AS PATH */
f62a369f 719 ARG(1, T_PATH);
4c553c5a
MM
720 RESULT(T_INT, i, as_path_get_last_nonaggregated(v1.val.ad));
721 }
f62a369f 722
a84b8b6e
MM
723 INST(FI_RETURN, 1, 1) {
724 /* Acquire the return value */
725 ARG_ANY(1);
726 uint retpos = fstk->vcnt;
727
728 /* Drop every sub-block including ourselves */
729 while ((fstk->ecnt-- > 0) && !(fstk->estk[fstk->ecnt].emask & FE_RETURN))
730 ;
731
732 /* Now we are at the caller frame; if no such, try to convert to accept/reject. */
733 if (!fstk->ecnt)
734 if (fstk->vstk[retpos].type == T_BOOL)
735 if (fstk->vstk[retpos].val.i)
736
737 return F_ACCEPT;
738 else
739 return F_REJECT;
740 else
741 runtime("Can't return non-bool from non-function");
742
743 /* Set the value stack position, overwriting the former implicit void */
744 fstk->vcnt = fstk->estk[fstk->ecnt].ventry - 1;
745
746 /* Copy the return value */
747 RESULT_VAL(fstk->vstk[retpos]);
748 }
749
4c553c5a 750 INST(FI_CALL, 0, 1) {
96d757c1 751 SYMBOL;
4f082dfa 752
96d757c1
JMM
753 /* Push the body on stack */
754 LINEX(sym->function);
ea4f55e3 755 curline.emask |= FE_RETURN;
96d757c1
JMM
756
757 /* Before this instruction was called, there was the T_VOID
758 * automatic return value pushed on value stack and also
759 * sym->function->args function arguments. Setting the
760 * vbase to point to first argument. */
761 ASSERT(curline.ventry >= sym->function->args);
762 curline.ventry -= sym->function->args;
763 curline.vbase = curline.ventry;
764
765 /* Storage for local variables */
1757a6fc
MM
766 memset(&(fstk->vstk[fstk->vcnt]), 0, sizeof(struct f_val) * sym->function->vars);
767 fstk->vcnt += sym->function->vars;
967b88d9 768 }
4c553c5a
MM
769
770 INST(FI_DROP_RESULT, 1, 0) {
771 ARG_ANY(1);
967b88d9 772 }
4c553c5a 773
4c553c5a 774 INST(FI_SWITCH, 1, 0) {
f62a369f 775 ARG_ANY(1);
26bfe59f
MM
776
777 FID_MEMBER(const struct f_tree *, tree, tree, [[!same_tree(f1->tree, f2->tree)]], tree %p, item->tree, const struct f_tree *tree = whati->tree);
778
32793ab6
MM
779 const struct f_tree *t = find_tree(tree, &v1);
780 if (!t) {
4c553c5a 781 v1.type = T_VOID;
32793ab6
MM
782 t = find_tree(tree, &v1);
783 if (!t) {
4c553c5a
MM
784 debug( "No else statement?\n");
785 break;
f62a369f 786 }
f62a369f 787 }
4c553c5a
MM
788 /* It is actually possible to have t->data NULL */
789
32793ab6 790 LINEX(t->data);
967b88d9 791 }
4c553c5a
MM
792
793 INST(FI_IP_MASK, 2, 1) { /* IP.MASK(val) */
f62a369f
JMM
794 ARG(1, T_IP);
795 ARG(2, T_INT);
4c553c5a 796 RESULT(T_IP, ip, [[ ipa_is_ip4(v1.val.ip) ?
f62a369f 797 ipa_from_ip4(ip4_and(ipa_to_ip4(v1.val.ip), ip4_mkmask(v2.val.i))) :
4c553c5a 798 ipa_from_ip6(ip6_and(ipa_to_ip6(v1.val.ip), ip6_mkmask(v2.val.i))) ]]);
967b88d9 799 }
f62a369f 800
4c553c5a 801 INST(FI_PATH_PREPEND, 2, 1) { /* Path prepend */
f62a369f
JMM
802 ARG(1, T_PATH);
803 ARG(2, T_INT);
4c553c5a
MM
804 RESULT(T_PATH, ad, [[ as_path_prepend(fs->pool, v1.val.ad, v2.val.i) ]]);
805 }
806
807 INST(FI_CLIST_ADD, 2, 1) { /* (Extended) Community list add */
808 ARG_ANY(1);
809 ARG_ANY(2);
810 if (v1.type == T_PATH)
811 runtime("Can't add to path");
812
813 else if (v1.type == T_CLIST)
814 {
815 /* Community (or cluster) list */
816 struct f_val dummy;
817
818 if ((v2.type == T_PAIR) || (v2.type == T_QUAD))
819 RESULT(T_CLIST, ad, [[ int_set_add(fs->pool, v1.val.ad, v2.val.i) ]]);
820 /* IP->Quad implicit conversion */
821 else if (val_is_ip4(&v2))
822 RESULT(T_CLIST, ad, [[ int_set_add(fs->pool, v1.val.ad, ipa_to_u32(v2.val.ip)) ]]);
823 else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy))
824 runtime("Can't add set");
825 else if (v2.type == T_CLIST)
826 RESULT(T_CLIST, ad, [[ int_set_union(fs->pool, v1.val.ad, v2.val.ad) ]]);
827 else
828 runtime("Can't add non-pair");
829 }
f62a369f 830
4c553c5a
MM
831 else if (v1.type == T_ECLIST)
832 {
833 /* v2.val is either EC or EC-set */
834 if ((v2.type == T_SET) && eclist_set_type(v2.val.t))
835 runtime("Can't add set");
836 else if (v2.type == T_ECLIST)
837 RESULT(T_ECLIST, ad, [[ ec_set_union(fs->pool, v1.val.ad, v2.val.ad) ]]);
838 else if (v2.type != T_EC)
839 runtime("Can't add non-ec");
840 else
841 RESULT(T_ECLIST, ad, [[ ec_set_add(fs->pool, v1.val.ad, v2.val.ec) ]]);
842 }
843
844 else if (v1.type == T_LCLIST)
845 {
846 /* v2.val is either LC or LC-set */
847 if ((v2.type == T_SET) && lclist_set_type(v2.val.t))
848 runtime("Can't add set");
849 else if (v2.type == T_LCLIST)
850 RESULT(T_LCLIST, ad, [[ lc_set_union(fs->pool, v1.val.ad, v2.val.ad) ]]);
851 else if (v2.type != T_LC)
852 runtime("Can't add non-lc");
853 else
854 RESULT(T_LCLIST, ad, [[ lc_set_add(fs->pool, v1.val.ad, v2.val.lc) ]]);
855
856 }
857
858 else
859 runtime("Can't add to non-[e|l]clist");
967b88d9 860 }
f62a369f 861
4c553c5a 862 INST(FI_CLIST_DEL, 2, 1) { /* (Extended) Community list add or delete */
f62a369f
JMM
863 ARG_ANY(1);
864 ARG_ANY(2);
865 if (v1.type == T_PATH)
866 {
4c553c5a 867 const struct f_tree *set = NULL;
f62a369f 868 u32 key = 0;
f62a369f
JMM
869
870 if (v2.type == T_INT)
871 key = v2.val.i;
872 else if ((v2.type == T_SET) && (v2.val.t->from.type == T_INT))
873 set = v2.val.t;
874 else
875 runtime("Can't delete non-integer (set)");
876
4c553c5a 877 RESULT(T_PATH, ad, [[ as_path_filter(fs->pool, v1.val.ad, set, key, 0) ]]);
f62a369f 878 }
4c553c5a 879
f62a369f
JMM
880 else if (v1.type == T_CLIST)
881 {
882 /* Community (or cluster) list */
883 struct f_val dummy;
f62a369f
JMM
884
885 if ((v2.type == T_PAIR) || (v2.type == T_QUAD))
4c553c5a 886 RESULT(T_CLIST, ad, [[ int_set_del(fs->pool, v1.val.ad, v2.val.i) ]]);
f62a369f 887 /* IP->Quad implicit conversion */
4c553c5a
MM
888 else if (val_is_ip4(&v2))
889 RESULT(T_CLIST, ad, [[ int_set_del(fs->pool, v1.val.ad, ipa_to_u32(v2.val.ip)) ]]);
890 else if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy) || (v2.type == T_CLIST))
891 RESULT(T_CLIST, ad, [[ clist_filter(fs->pool, v1.val.ad, &v2, 0) ]]);
f62a369f 892 else
4c553c5a 893 runtime("Can't delete non-pair");
f62a369f 894 }
4c553c5a 895
f62a369f
JMM
896 else if (v1.type == T_ECLIST)
897 {
f62a369f 898 /* v2.val is either EC or EC-set */
4c553c5a
MM
899 if ((v2.type == T_SET) && eclist_set_type(v2.val.t) || (v2.type == T_ECLIST))
900 RESULT(T_ECLIST, ad, [[ eclist_filter(fs->pool, v1.val.ad, &v2, 0) ]]);
f62a369f 901 else if (v2.type != T_EC)
4c553c5a
MM
902 runtime("Can't delete non-ec");
903 else
904 RESULT(T_ECLIST, ad, [[ ec_set_del(fs->pool, v1.val.ad, v2.val.ec) ]]);
f62a369f 905 }
4c553c5a 906
f62a369f
JMM
907 else if (v1.type == T_LCLIST)
908 {
f62a369f 909 /* v2.val is either LC or LC-set */
4c553c5a
MM
910 if ((v2.type == T_SET) && lclist_set_type(v2.val.t) || (v2.type == T_LCLIST))
911 RESULT(T_LCLIST, ad, [[ lclist_filter(fs->pool, v1.val.ad, &v2, 0) ]]);
f62a369f 912 else if (v2.type != T_LC)
4c553c5a
MM
913 runtime("Can't delete non-lc");
914 else
915 RESULT(T_LCLIST, ad, [[ lc_set_del(fs->pool, v1.val.ad, v2.val.lc) ]]);
916 }
f62a369f 917
4c553c5a
MM
918 else
919 runtime("Can't delete in non-[e|l]clist");
920 }
f62a369f 921
4c553c5a
MM
922 INST(FI_CLIST_FILTER, 2, 1) { /* (Extended) Community list add or delete */
923 ARG_ANY(1);
924 ARG_ANY(2);
925 if (v1.type == T_PATH)
926 {
927 u32 key = 0;
f62a369f 928
4c553c5a
MM
929 if ((v2.type == T_SET) && (v2.val.t->from.type == T_INT))
930 RESULT(T_PATH, ad, [[ as_path_filter(fs->pool, v1.val.ad, v2.val.t, key, 1) ]]);
931 else
932 runtime("Can't filter integer");
f62a369f 933 }
f62a369f 934
4c553c5a
MM
935 else if (v1.type == T_CLIST)
936 {
937 /* Community (or cluster) list */
938 struct f_val dummy;
f62a369f 939
4c553c5a
MM
940 if ((v2.type == T_SET) && clist_set_type(v2.val.t, &dummy) || (v2.type == T_CLIST))
941 RESULT(T_CLIST, ad, [[ clist_filter(fs->pool, v1.val.ad, &v2, 1) ]]);
942 else
943 runtime("Can't filter pair");
944 }
945
946 else if (v1.type == T_ECLIST)
f62a369f 947 {
4c553c5a
MM
948 /* v2.val is either EC or EC-set */
949 if ((v2.type == T_SET) && eclist_set_type(v2.val.t) || (v2.type == T_ECLIST))
950 RESULT(T_ECLIST, ad, [[ eclist_filter(fs->pool, v1.val.ad, &v2, 1) ]]);
951 else
952 runtime("Can't filter ec");
953 }
f62a369f 954
4c553c5a
MM
955 else if (v1.type == T_LCLIST)
956 {
957 /* v2.val is either LC or LC-set */
958 if ((v2.type == T_SET) && lclist_set_type(v2.val.t) || (v2.type == T_LCLIST))
959 RESULT(T_LCLIST, ad, [[ lclist_filter(fs->pool, v1.val.ad, &v2, 1) ]]);
960 else
961 runtime("Can't filter lc");
f62a369f 962 }
4c553c5a 963
f62a369f 964 else
4c553c5a
MM
965 runtime("Can't filter non-[e|l]clist");
966 }
f62a369f 967
4c553c5a
MM
968 INST(FI_ROA_CHECK_IMPLICIT, 0, 1) { /* ROA Check */
969 RTC(1);
970 ACCESS_RTE;
971 ACCESS_EATTRS;
972 const net_addr *net = (*fs->rte)->net->n.addr;
f62a369f 973
4c553c5a
MM
974 /* We ignore temporary attributes, probably not a problem here */
975 /* 0x02 is a value of BA_AS_PATH, we don't want to include BGP headers */
976 eattr *e = ea_find(*fs->eattrs, EA_CODE(PROTOCOL_BGP, 0x02));
f62a369f 977
4c553c5a
MM
978 if (!e || ((e->type & EAF_TYPE_MASK) != EAF_TYPE_AS_PATH))
979 runtime("Missing AS_PATH attribute");
980
981 u32 as = 0;
982 as_path_get_last(e->u.ptr, &as);
f62a369f 983
f62a369f
JMM
984 if (!table)
985 runtime("Missing ROA table");
986
987 if (table->addr_type != NET_ROA4 && table->addr_type != NET_ROA6)
988 runtime("Table type must be either ROA4 or ROA6");
989
4c553c5a
MM
990 if (table->addr_type != (net->type == NET_IP4 ? NET_ROA4 : NET_ROA6))
991 RESULT(T_ENUM_ROA, i, ROA_UNKNOWN); /* Prefix and table type mismatch */
992 else
993 RESULT(T_ENUM_ROA, i, [[ net_roa_check(table, net, as) ]]);
994 }
995
996 INST(FI_ROA_CHECK_EXPLICIT, 2, 1) { /* ROA Check */
997 ARG(1, T_NET);
998 ARG(2, T_INT);
999 RTC(3);
1000
1001 u32 as = v2.val.i;
1002
1003 if (!table)
1004 runtime("Missing ROA table");
1005
1006 if (table->addr_type != NET_ROA4 && table->addr_type != NET_ROA6)
1007 runtime("Table type must be either ROA4 or ROA6");
f62a369f
JMM
1008
1009 if (table->addr_type != (v1.val.net->type == NET_IP4 ? NET_ROA4 : NET_ROA6))
4c553c5a 1010 RESULT(T_ENUM_ROA, i, ROA_UNKNOWN); /* Prefix and table type mismatch */
f62a369f 1011 else
4c553c5a 1012 RESULT(T_ENUM_ROA, i, [[ net_roa_check(table, v1.val.net, as) ]]);
f62a369f 1013
967b88d9 1014 }
f62a369f 1015
4c553c5a 1016 INST(FI_FORMAT, 1, 0) { /* Format */
f62a369f 1017 ARG_ANY(1);
4c553c5a 1018 RESULT(T_STRING, s, val_format_str(fs, &v1));
967b88d9 1019 }
f62a369f 1020
4c553c5a 1021 INST(FI_ASSERT, 1, 0) { /* Birdtest Assert */
f62a369f 1022 ARG(1, T_BOOL);
26bfe59f
MM
1023 FID_MEMBER(const char *, s, s, [[strcmp(f1->s, f2->s)]], string \"%s\", item->s);
1024
c0e958e0
MM
1025 if (!bt_assert_hook)
1026 runtime("No bt_assert hook registered, can't assert");
1027
1028 bt_assert_hook(res.val.i, what);
967b88d9 1029 }