1 /*
2  *	BIRD Internet Routing Daemon -- Filter instructions
3  *
4  *	(c) 1999 Pavel Machek <pavel@ucw.cz>
5  *	(c) 2018--2019 Maria Matejka <mq@jmq.cz>
6  *
7  *	Can be freely distributed and used under the terms of the GNU GPL.
8  *
9  *	Filter interpreter data structures and internal API.
10  *	See filter/f-inst.c for documentation.
11  */
12 
13 #ifndef _BIRD_F_INST_H_
14 #define _BIRD_F_INST_H_
15 
16 #include "nest/bird.h"
17 #include "conf/conf.h"
18 #include "filter/filter.h"
19 #include "filter/data.h"
20 #include "lib/buffer.h"
21 #include "lib/flowspec.h"
22 
23 /* Flags for instructions */
24 enum f_instruction_flags {
25   FIF_PRINTED = 1,		/* FI_PRINT_AND_DIE: message put in buffer */
26 } PACKED;
27 
28 /* Include generated filter instruction declarations */
29 #include "filter/inst-gen.h"
30 
31 #define f_new_inst(...) MACRO_CONCAT_AFTER(f_new_inst_, MACRO_FIRST(__VA_ARGS__))(__VA_ARGS__)
32 
33 /* Convert the instruction back to the enum name */
34 const char *f_instruction_name_(enum f_instruction_code fi);
f_instruction_name(enum f_instruction_code fi)35 static inline const char *f_instruction_name(enum f_instruction_code fi)
36 { return f_instruction_name_(fi) + 3; }
37 
38 /* Filter structures for execution */
39 /* Line of instructions to be unconditionally executed one after another */
40 struct f_line {
41   uint len;				/* Line length */
42   u8 args;				/* Function: Args required */
43   u8 vars;
44   struct f_line_item items[0];		/* The items themselves */
45 };
46 
47 /* Convert the f_inst infix tree to the f_line structures */
48 struct f_line *f_linearize_concat(const struct f_inst * const inst[], uint count);
f_linearize(const struct f_inst * root)49 static inline struct f_line *f_linearize(const struct f_inst *root)
50 { return f_linearize_concat(&root, 1); }
51 
52 void f_dump_line(const struct f_line *, uint indent);
53 
54 
55 /* Recursive iteration over filter instructions */
56 
57 struct filter_iterator {
58   BUFFER_(const struct f_line *) lines;
59 };
60 
61 void f_add_lines(const struct f_line_item *what, struct filter_iterator *fit);
62 
63 #define FILTER_ITERATE_INIT(fit, filter, pool)			\
64   ({								\
65     BUFFER_INIT((fit)->lines, (pool), 32);			\
66     BUFFER_PUSH((fit)->lines) = (filter)->root;			\
67   })
68 
69 #define FILTER_ITERATE(fit, fi) ({				\
70   const struct f_line *fl_;					\
71   while (!BUFFER_EMPTY((fit)->lines))				\
72   {								\
73     BUFFER_POP((fit)->lines);					\
74     fl_ = (fit)->lines.data[(fit)->lines.used];			\
75     for (uint i_ = 0; i_ < fl_->len; i_++)			\
76     {								\
77       const struct f_line_item *fi = &fl_->items[i_];		\
78       f_add_lines(fi, (fit));
79 
80 #define FILTER_ITERATE_END } } })
81 
82 #define FILTER_ITERATE_CLEANUP(fit)				\
83   ({								\
84     mb_free((fit)->lines.data);					\
85     memset((fit), 0, sizeof(struct filter_iterator));		\
86   })
87 
88 
89 struct filter *f_new_where(struct f_inst *);
f_new_dynamic_attr(u8 type,enum f_type f_type,uint code)90 static inline struct f_dynamic_attr f_new_dynamic_attr(u8 type, enum f_type f_type, uint code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
91 { return (struct f_dynamic_attr) { .type = type, .f_type = f_type, .ea_code = code }; }   /* f_type currently unused; will be handy for static type checking */
f_new_dynamic_attr_bit(u8 bit,enum f_type f_type,uint code)92 static inline struct f_dynamic_attr f_new_dynamic_attr_bit(u8 bit, enum f_type f_type, uint code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
93 { return (struct f_dynamic_attr) { .type = EAF_TYPE_BITFIELD, .bit = bit, .f_type = f_type, .ea_code = code }; }   /* f_type currently unused; will be handy for static type checking */
f_new_static_attr(int f_type,int code,int readonly)94 static inline struct f_static_attr f_new_static_attr(int f_type, int code, int readonly)
95 { return (struct f_static_attr) { .f_type = f_type, .sa_code = code, .readonly = readonly }; }
96 struct f_inst *f_generate_complex(enum f_instruction_code fi_code, struct f_dynamic_attr da, struct f_inst *argument);
97 struct f_inst *f_generate_roa_check(struct rtable_config *table, struct f_inst *prefix, struct f_inst *asn);
98 
99 /* Hook for call bt_assert() function in configuration */
100 extern void (*bt_assert_hook)(int result, const struct f_line_item *assert);
101 
102 /* Bird Tests */
103 struct f_bt_test_suite {
104   node n;			/* Node in config->tests */
105   const struct f_line *fn;	/* Root of function */
106   const struct f_line *cmp;	/* Compare to this function */
107   const char *fn_name;		/* Name of test */
108   const char *dsc;		/* Description */
109   int result;			/* Desired result */
110 };
111 
112 #endif
113