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