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