]> git.ipfire.org Git - thirdparty/bird.git/blob - filter/filter.h
Filter refactoring: Passing the resulting struct f_val as a pointer.
[thirdparty/bird.git] / filter / filter.h
1 /*
2 * BIRD Internet Routing Daemon -- Filters
3 *
4 * (c) 1999 Pavel Machek <pavel@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #ifndef _BIRD_FILT_H_
10 #define _BIRD_FILT_H_
11
12 #include "lib/resource.h"
13 #include "lib/ip.h"
14 #include "nest/route.h"
15 #include "nest/attrs.h"
16
17 /* Filter instruction types */
18
19 #define FI__TWOCHAR(a,b) ((a<<8) | b)
20 #define FI__LIST \
21 F(FI_ADD, 0, '+') \
22 F(FI_SUBTRACT, 0, '-') \
23 F(FI_MULTIPLY, 0, '*') \
24 F(FI_DIVIDE, 0, '/') \
25 F(FI_AND, 0, '&') \
26 F(FI_OR, 0, '|') \
27 F(FI_PAIR_CONSTRUCT, 'm', 'p') \
28 F(FI_EC_CONSTRUCT, 'm', 'c') \
29 F(FI_LC_CONSTRUCT, 'm', 'l') \
30 F(FI_PATHMASK_CONSTRUCT, 'm', 'P') \
31 F(FI_NEQ, '!', '=') \
32 F(FI_EQ, '=', '=') \
33 F(FI_LT, 0, '<') \
34 F(FI_LTE, '<', '=') \
35 F(FI_NOT, 0, '!') \
36 F(FI_MATCH, 0, '~') \
37 F(FI_NOT_MATCH, '!', '~') \
38 F(FI_DEFINED, 'd', 'e') \
39 F(FI_TYPE, 0, 'T') \
40 F(FI_IS_V4, 'I', 'i') \
41 F(FI_SET, 0, 's') \
42 F(FI_CONSTANT, 0, 'c') \
43 F(FI_VARIABLE, 0, 'V') \
44 F(FI_CONSTANT_INDIRECT, 0, 'C') \
45 F(FI_PRINT, 0, 'p') \
46 F(FI_CONDITION, 0, '?') \
47 F(FI_NOP, 0, '0') \
48 F(FI_PRINT_AND_DIE, 'p', ',') \
49 F(FI_RTA_GET, 0, 'a') \
50 F(FI_RTA_SET, 'a', 'S') \
51 F(FI_EA_GET, 'e', 'a') \
52 F(FI_EA_SET, 'e', 'S') \
53 F(FI_PREF_GET, 0, 'P') \
54 F(FI_PREF_SET, 'P', 'S') \
55 F(FI_LENGTH, 0, 'L') \
56 F(FI_ROA_MAXLEN, 'R', 'M') \
57 F(FI_ROA_ASN, 'R', 'A') \
58 F(FI_SADR_SRC, 'n', 's') \
59 F(FI_IP, 'c', 'p') \
60 F(FI_ROUTE_DISTINGUISHER, 'R', 'D') \
61 F(FI_AS_PATH_FIRST, 'a', 'f') \
62 F(FI_AS_PATH_LAST, 'a', 'l') \
63 F(FI_AS_PATH_LAST_NAG, 'a', 'L') \
64 F(FI_RETURN, 0, 'r') \
65 F(FI_CALL, 'c', 'a') \
66 F(FI_CLEAR_LOCAL_VARS, 'c', 'V') \
67 F(FI_SWITCH, 'S', 'W') \
68 F(FI_IP_MASK, 'i', 'M') \
69 F(FI_EMPTY, 0, 'E') \
70 F(FI_PATH_PREPEND, 'A', 'p') \
71 F(FI_CLIST_ADD_DEL, 'C', 'a') \
72 F(FI_ROA_CHECK, 'R', 'C') \
73 F(FI_FORMAT, 0, 'F') \
74 F(FI_ASSERT, 'a', 's')
75
76 enum f_instruction_code {
77 #define F(c,a,b) \
78 c,
79 FI__LIST
80 #undef F
81 FI__MAX,
82 } PACKED;
83
84 const char *f_instruction_name(enum f_instruction_code fi);
85
86 struct f_inst { /* Instruction */
87 struct f_inst *next; /* Structure is 16 bytes, anyway */
88 enum f_instruction_code fi_code;
89 u16 aux; /* Extension to instruction code, T_*, EA_*, EAF_* */
90 union {
91 uint i;
92 void *p;
93 } a1; /* The first argument */
94 union {
95 uint i;
96 void *p;
97 } a2; /* The second argument */
98 union {
99 int i;
100 void *p;
101 } a3; /* The third argument */
102 int lineno;
103 };
104
105 #define arg1 a1.p
106 #define arg2 a2.p
107
108 /* Not enough fields in f_inst for three args used by roa_check() */
109 struct f_inst_roa_check {
110 struct f_inst i;
111 struct rtable_config *rtc;
112 };
113
114 struct f_prefix {
115 net_addr net;
116 u8 lo, hi;
117 };
118
119 struct f_val {
120 int type; /* T_* */
121 union {
122 uint i;
123 u64 ec;
124 lcomm lc;
125 ip_addr ip;
126 const net_addr *net;
127 char *s;
128 struct f_tree *t;
129 struct f_trie *ti;
130 struct adata *ad;
131 struct f_path_mask *path_mask;
132 } val;
133 };
134
135 struct f_dynamic_attr {
136 int type;
137 int f_type;
138 int ea_code;
139 };
140
141 struct f_static_attr {
142 int f_type;
143 int sa_code;
144 int readonly;
145 };
146
147 struct filter {
148 char *name;
149 struct f_inst *root;
150 };
151
152 struct f_inst *f_new_inst(enum f_instruction_code fi_code);
153 struct f_inst *f_new_inst_da(enum f_instruction_code fi_code, struct f_dynamic_attr da);
154 struct f_inst *f_new_inst_sa(enum f_instruction_code fi_code, struct f_static_attr sa);
155 static inline struct f_dynamic_attr f_new_dynamic_attr(int type, int f_type, int code) /* Type as core knows it, type as filters know it, and code of dynamic attribute */
156 { 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 */
157 static inline struct f_static_attr f_new_static_attr(int f_type, int code, int readonly)
158 { return (struct f_static_attr) { .f_type = f_type, .sa_code = code, .readonly = readonly }; }
159 struct f_tree *f_new_tree(void);
160 struct f_inst *f_generate_complex(int operation, int operation_aux, struct f_dynamic_attr da, struct f_inst *argument);
161 struct f_inst *f_generate_roa_check(struct rtable_config *table, struct f_inst *prefix, struct f_inst *asn);
162
163
164 struct f_tree *build_tree(struct f_tree *);
165 struct f_tree *find_tree(struct f_tree *t, struct f_val val);
166 int same_tree(struct f_tree *t1, struct f_tree *t2);
167 void tree_format(struct f_tree *t, buffer *buf);
168
169 struct f_trie *f_new_trie(linpool *lp, uint node_size);
170 void *trie_add_prefix(struct f_trie *t, const net_addr *n, uint l, uint h);
171 int trie_match_net(struct f_trie *t, const net_addr *n);
172 int trie_same(struct f_trie *t1, struct f_trie *t2);
173 void trie_format(struct f_trie *t, buffer *buf);
174
175 struct ea_list;
176 struct rte;
177
178 enum filter_return {
179 F_NOP = 0,
180 F_NONL,
181 F_RETURN,
182 F_ACCEPT, /* Need to preserve ordering: accepts < rejects! */
183 F_REJECT,
184 F_ERROR,
185 F_QUITBIRD,
186 };
187
188 enum filter_return f_run(struct filter *filter, struct rte **rte, struct linpool *tmp_pool, int flags);
189 enum filter_return f_eval_rte(struct f_inst *expr, struct rte **rte, struct linpool *tmp_pool);
190 enum filter_return f_eval(struct f_inst *expr, struct linpool *tmp_pool, struct f_val *pres);
191 uint f_eval_int(struct f_inst *expr);
192
193 char *filter_name(struct filter *filter);
194 int filter_same(struct filter *new, struct filter *old);
195
196 int i_same(struct f_inst *f1, struct f_inst *f2);
197
198 int val_compare(struct f_val v1, struct f_val v2);
199 int val_same(struct f_val v1, struct f_val v2);
200
201 void val_format(struct f_val v, buffer *buf);
202
203 #define FILTER_ACCEPT NULL
204 #define FILTER_REJECT ((void *) 1)
205 #define FILTER_UNDEF ((void *) 2) /* Used in BGP */
206
207 /* Type numbers must be in 0..0xff range */
208 #define T_MASK 0xff
209
210 /* Internal types */
211 /* Do not use type of zero, that way we'll see errors easier. */
212 #define T_VOID 1
213
214 /* User visible types, which fit in int */
215 #define T_INT 0x10
216 #define T_BOOL 0x11
217 #define T_PAIR 0x12 /* Notice that pair is stored as integer: first << 16 | second */
218 #define T_QUAD 0x13
219
220 /* Put enumerational types in 0x30..0x3f range */
221 #define T_ENUM_LO 0x30
222 #define T_ENUM_HI 0x3f
223
224 #define T_ENUM_RTS 0x30
225 #define T_ENUM_BGP_ORIGIN 0x31
226 #define T_ENUM_SCOPE 0x32
227 #define T_ENUM_RTC 0x33
228 #define T_ENUM_RTD 0x34
229 #define T_ENUM_ROA 0x35
230 #define T_ENUM_NETTYPE 0x36
231 #define T_ENUM_RA_PREFERENCE 0x37
232
233 /* new enums go here */
234 #define T_ENUM_EMPTY 0x3f /* Special hack for atomic_aggr */
235
236 #define T_ENUM T_ENUM_LO ... T_ENUM_HI
237
238 /* Bigger ones */
239 #define T_IP 0x20
240 #define T_NET 0x21
241 #define T_STRING 0x22
242 #define T_PATH_MASK 0x23 /* mask for BGP path */
243 #define T_PATH 0x24 /* BGP path */
244 #define T_CLIST 0x25 /* Community list */
245 #define T_EC 0x26 /* Extended community value, u64 */
246 #define T_ECLIST 0x27 /* Extended community list */
247 #define T_LC 0x28 /* Large community value, lcomm */
248 #define T_LCLIST 0x29 /* Large community list */
249 #define T_RD 0x2a /* Route distinguisher for VPN addresses */
250
251 #define T_SET 0x80
252 #define T_PREFIX_SET 0x81
253
254
255 #define SA_FROM 1
256 #define SA_GW 2
257 #define SA_NET 3
258 #define SA_PROTO 4
259 #define SA_SOURCE 5
260 #define SA_SCOPE 6
261 #define SA_DEST 7
262 #define SA_IFNAME 8
263 #define SA_IFINDEX 9
264
265
266 struct f_tree {
267 struct f_tree *left, *right;
268 struct f_val from, to;
269 void *data;
270 };
271
272 struct f_trie_node
273 {
274 ip_addr addr, mask, accept;
275 uint plen;
276 struct f_trie_node *c[2];
277 };
278
279 struct f_trie
280 {
281 linpool *lp;
282 int zero;
283 uint node_size;
284 struct f_trie_node root[0]; /* Root trie node follows */
285 };
286
287 #define NEW_F_VAL struct f_val * val; val = cfg_alloc(sizeof(struct f_val));
288
289 #define FF_SILENT 2 /* Silent filter execution */
290
291 /* Custom route attributes */
292 struct custom_attribute {
293 resource r;
294 struct f_dynamic_attr *fda;
295 const char *name;
296 };
297
298 struct custom_attribute *ca_lookup(pool *p, const char *name, int ea_type);
299
300 /* Bird Tests */
301 struct f_bt_test_suite {
302 node n; /* Node in config->tests */
303 struct f_inst *fn; /* Root of function */
304 const char *fn_name; /* Name of test */
305 const char *dsc; /* Description */
306 };
307
308 /* Hook for call bt_assert() function in configuration */
309 extern void (*bt_assert_hook)(int result, struct f_inst *assert);
310
311 #endif