]>
Commit | Line | Data |
---|---|---|
8bdb05ed | 1 | /* |
4f082dfa | 2 | * BIRD Internet Routing Daemon -- Dynamic data structures |
8bdb05ed MM |
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 | ||
4f082dfa MM |
10 | #ifndef _BIRD_FILTER_DATA_H_ |
11 | #define _BIRD_FILTER_DATA_H_ | |
8bdb05ed | 12 | |
4f082dfa | 13 | #include "nest/bird.h" |
8bdb05ed MM |
14 | |
15 | /* Type numbers must be in 0..0xff range */ | |
16 | #define T_MASK 0xff | |
17 | ||
18 | /* Internal types */ | |
19 | enum f_type { | |
96d757c1 JMM |
20 | /* Nothing. Simply nothing. */ |
21 | T_VOID = 0, | |
8bdb05ed MM |
22 | |
23 | /* User visible types, which fit in int */ | |
24 | T_INT = 0x10, | |
25 | T_BOOL = 0x11, | |
26 | T_PAIR = 0x12, /* Notice that pair is stored as integer: first << 16 | second */ | |
27 | T_QUAD = 0x13, | |
28 | ||
29 | /* Put enumerational types in 0x30..0x3f range */ | |
30 | T_ENUM_LO = 0x30, | |
31 | T_ENUM_HI = 0x3f, | |
32 | ||
33 | T_ENUM_RTS = 0x30, | |
34 | T_ENUM_BGP_ORIGIN = 0x31, | |
35 | T_ENUM_SCOPE = 0x32, | |
36 | T_ENUM_RTC = 0x33, | |
37 | T_ENUM_RTD = 0x34, | |
38 | T_ENUM_ROA = 0x35, | |
39 | T_ENUM_NETTYPE = 0x36, | |
40 | T_ENUM_RA_PREFERENCE = 0x37, | |
0edf0c8c | 41 | T_ENUM_AF = 0x38, |
8bdb05ed MM |
42 | |
43 | /* new enums go here */ | |
44 | T_ENUM_EMPTY = 0x3f, /* Special hack for atomic_aggr */ | |
45 | ||
46 | #define T_ENUM T_ENUM_LO ... T_ENUM_HI | |
47 | ||
48 | /* Bigger ones */ | |
49 | T_IP = 0x20, | |
50 | T_NET = 0x21, | |
51 | T_STRING = 0x22, | |
52 | T_PATH_MASK = 0x23, /* mask for BGP path */ | |
53 | T_PATH = 0x24, /* BGP path */ | |
54 | T_CLIST = 0x25, /* Community list */ | |
55 | T_EC = 0x26, /* Extended community value, u64 */ | |
56 | T_ECLIST = 0x27, /* Extended community list */ | |
57 | T_LC = 0x28, /* Large community value, lcomm */ | |
58 | T_LCLIST = 0x29, /* Large community list */ | |
59 | T_RD = 0x2a, /* Route distinguisher for VPN addresses */ | |
60 | T_PATH_MASK_ITEM = 0x2b, /* Path mask item for path mask constructors */ | |
fc354788 | 61 | T_BYTESTRING = 0x2c, |
8bdb05ed MM |
62 | |
63 | T_SET = 0x80, | |
64 | T_PREFIX_SET = 0x81, | |
65 | } PACKED; | |
66 | ||
67 | /* Filter value; size of this affects filter memory consumption */ | |
68 | struct f_val { | |
69 | enum f_type type; /* T_* */ | |
70 | union { | |
71 | uint i; | |
72 | u64 ec; | |
73 | lcomm lc; | |
74 | ip_addr ip; | |
75 | const net_addr *net; | |
fd9f0c06 | 76 | const char *s; |
fc354788 | 77 | const struct bytestring *bs; |
8bdb05ed MM |
78 | const struct f_tree *t; |
79 | const struct f_trie *ti; | |
80 | const struct adata *ad; | |
81 | const struct f_path_mask *path_mask; | |
82 | struct f_path_mask_item pmi; | |
83 | } val; | |
84 | }; | |
85 | ||
8bdb05ed MM |
86 | /* Dynamic attribute definition (eattrs) */ |
87 | struct f_dynamic_attr { | |
88 | u8 type; /* EA type (EAF_*) */ | |
89 | u8 bit; /* For bitfield accessors */ | |
90 | enum f_type f_type; /* Filter type */ | |
91 | uint ea_code; /* EA code */ | |
92 | }; | |
93 | ||
94 | enum f_sa_code { | |
95 | SA_FROM = 1, | |
96 | SA_GW, | |
97 | SA_NET, | |
98 | SA_PROTO, | |
99 | SA_SOURCE, | |
100 | SA_SCOPE, | |
101 | SA_DEST, | |
102 | SA_IFNAME, | |
103 | SA_IFINDEX, | |
8cc5bb09 | 104 | SA_WEIGHT, |
eb937358 | 105 | SA_PREF, |
e5468d16 | 106 | SA_GW_MPLS, |
7144c9ca | 107 | SA_ONLINK, |
8bdb05ed MM |
108 | } PACKED; |
109 | ||
110 | /* Static attribute definition (members of struct rta) */ | |
111 | struct f_static_attr { | |
112 | enum f_type f_type; /* Filter type */ | |
113 | enum f_sa_code sa_code; /* Static attribute id */ | |
114 | int readonly:1; /* Don't allow writing */ | |
115 | }; | |
116 | ||
4f082dfa MM |
117 | /* Filter l-value type */ |
118 | enum f_lval_type { | |
119 | F_LVAL_VARIABLE, | |
120 | F_LVAL_PREFERENCE, | |
121 | F_LVAL_SA, | |
122 | F_LVAL_EA, | |
123 | }; | |
124 | ||
125 | /* Filter l-value */ | |
126 | struct f_lval { | |
127 | enum f_lval_type type; | |
128 | union { | |
b40c0f02 | 129 | struct symbol *sym; |
4f082dfa MM |
130 | struct f_dynamic_attr da; |
131 | struct f_static_attr sa; | |
132 | }; | |
133 | }; | |
134 | ||
135 | /* IP prefix range structure */ | |
136 | struct f_prefix { | |
137 | net_addr net; /* The matching prefix must match this net */ | |
138 | u8 lo, hi; /* And its length must fit between lo and hi */ | |
139 | }; | |
140 | ||
8bdb05ed MM |
141 | struct f_tree { |
142 | struct f_tree *left, *right; | |
143 | struct f_val from, to; | |
144 | void *data; | |
145 | }; | |
146 | ||
d3f50ede OZ |
147 | #ifdef ENABLE_COMPACT_TRIES |
148 | /* Compact 4-way tries */ | |
149 | #define TRIE_STEP 2 | |
150 | #define TRIE_STACK_LENGTH 65 | |
151 | #else | |
152 | /* Faster 16-way tries */ | |
062e69bf OZ |
153 | #define TRIE_STEP 4 |
154 | #define TRIE_STACK_LENGTH 33 | |
d3f50ede | 155 | #endif |
13225f1d | 156 | |
27550028 OZ |
157 | struct f_trie_node4 |
158 | { | |
159 | ip4_addr addr, mask, accept; | |
13225f1d OZ |
160 | u16 plen; |
161 | u16 local; | |
162 | struct f_trie_node4 *c[1 << TRIE_STEP]; | |
27550028 OZ |
163 | }; |
164 | ||
165 | struct f_trie_node6 | |
8bdb05ed | 166 | { |
27550028 | 167 | ip6_addr addr, mask, accept; |
13225f1d OZ |
168 | u16 plen; |
169 | u16 local; | |
170 | struct f_trie_node6 *c[1 << TRIE_STEP]; | |
27550028 OZ |
171 | }; |
172 | ||
173 | struct f_trie_node | |
174 | { | |
175 | union { | |
176 | struct f_trie_node4 v4; | |
177 | struct f_trie_node6 v6; | |
178 | }; | |
8bdb05ed MM |
179 | }; |
180 | ||
181 | struct f_trie | |
182 | { | |
183 | linpool *lp; | |
27550028 OZ |
184 | u8 zero; |
185 | s8 ipv4; /* -1 for undefined / empty */ | |
186 | u16 data_size; /* Additional data for each trie node */ | |
ba5aec94 | 187 | u32 prefix_count; /* Works only for restricted tries (pxlen == l == h) */ |
27550028 | 188 | struct f_trie_node root; /* Root trie node */ |
8bdb05ed MM |
189 | }; |
190 | ||
062e69bf OZ |
191 | struct f_trie_walk_state |
192 | { | |
193 | u8 ipv4; | |
194 | u8 accept_length; /* Current inter-node prefix position */ | |
195 | u8 start_pos; /* Initial prefix position in stack[0] */ | |
196 | u8 local_pos; /* Current intra-node prefix position */ | |
197 | u8 stack_pos; /* Current node in stack below */ | |
198 | const struct f_trie_node *stack[TRIE_STACK_LENGTH]; | |
199 | }; | |
200 | ||
8bdb05ed MM |
201 | struct f_tree *f_new_tree(void); |
202 | struct f_tree *build_tree(struct f_tree *); | |
203 | const struct f_tree *find_tree(const struct f_tree *t, const struct f_val *val); | |
e20bef69 | 204 | const struct f_tree *find_tree_linear(const struct f_tree *t, const struct f_val *val); |
8bdb05ed | 205 | int same_tree(const struct f_tree *t0, const struct f_tree *t2); |
92a85655 | 206 | int tree_node_count(const struct f_tree *t); |
8bdb05ed | 207 | void tree_format(const struct f_tree *t, buffer *buf); |
d06a875b | 208 | void tree_walk(const struct f_tree *t, void (*hook)(const struct f_tree *, void *), void *data); |
8bdb05ed | 209 | |
27550028 | 210 | struct f_trie *f_new_trie(linpool *lp, uint data_size); |
8bdb05ed MM |
211 | void *trie_add_prefix(struct f_trie *t, const net_addr *n, uint l, uint h); |
212 | int trie_match_net(const struct f_trie *t, const net_addr *n); | |
14fc24f3 OZ |
213 | int trie_match_longest_ip4(const struct f_trie *t, const net_addr_ip4 *net, net_addr_ip4 *dst, ip4_addr *found0); |
214 | int trie_match_longest_ip6(const struct f_trie *t, const net_addr_ip6 *net, net_addr_ip6 *dst, ip6_addr *found0); | |
062e69bf OZ |
215 | void trie_walk_init(struct f_trie_walk_state *s, const struct f_trie *t, const net_addr *from); |
216 | int trie_walk_next(struct f_trie_walk_state *s, net_addr *net); | |
8bdb05ed MM |
217 | int trie_same(const struct f_trie *t1, const struct f_trie *t2); |
218 | void trie_format(const struct f_trie *t, buffer *buf); | |
219 | ||
14fc24f3 OZ |
220 | static inline int |
221 | trie_match_next_longest_ip4(net_addr_ip4 *n, ip4_addr *found) | |
222 | { | |
223 | while (n->pxlen) | |
224 | { | |
225 | n->pxlen--; | |
226 | ip4_clrbit(&n->prefix, n->pxlen); | |
227 | ||
228 | if (ip4_getbit(*found, n->pxlen)) | |
229 | return 1; | |
230 | } | |
231 | ||
232 | return 0; | |
233 | } | |
234 | ||
235 | static inline int | |
236 | trie_match_next_longest_ip6(net_addr_ip6 *n, ip6_addr *found) | |
237 | { | |
238 | while (n->pxlen) | |
239 | { | |
240 | n->pxlen--; | |
241 | ip6_clrbit(&n->prefix, n->pxlen); | |
242 | ||
243 | if (ip6_getbit(*found, n->pxlen)) | |
244 | return 1; | |
245 | } | |
246 | ||
247 | return 0; | |
248 | } | |
249 | ||
250 | ||
251 | #define TRIE_WALK_TO_ROOT_IP4(trie, net, dst) ({ \ | |
252 | net_addr_ip4 dst; \ | |
253 | ip4_addr _found; \ | |
254 | for (int _n = trie_match_longest_ip4(trie, net, &dst, &_found); \ | |
255 | _n; \ | |
256 | _n = trie_match_next_longest_ip4(&dst, &_found)) | |
257 | ||
258 | #define TRIE_WALK_TO_ROOT_IP6(trie, net, dst) ({ \ | |
259 | net_addr_ip6 dst; \ | |
260 | ip6_addr _found; \ | |
261 | for (int _n = trie_match_longest_ip6(trie, net, &dst, &_found); \ | |
262 | _n; \ | |
263 | _n = trie_match_next_longest_ip6(&dst, &_found)) | |
264 | ||
265 | #define TRIE_WALK_TO_ROOT_END }) | |
266 | ||
267 | ||
062e69bf OZ |
268 | #define TRIE_WALK(trie, net, from) ({ \ |
269 | net_addr net; \ | |
270 | struct f_trie_walk_state tws_; \ | |
271 | trie_walk_init(&tws_, trie, from); \ | |
272 | while (trie_walk_next(&tws_, &net)) | |
273 | ||
274 | #define TRIE_WALK_END }) | |
275 | ||
14fc24f3 | 276 | |
52893045 MM |
277 | #define F_CMP_ERROR 999 |
278 | ||
87512e97 OZ |
279 | const char *f_type_name(enum f_type t); |
280 | ||
0e1fd7ea AZ |
281 | enum f_type f_type_element_type(enum f_type t); |
282 | ||
52893045 MM |
283 | int val_same(const struct f_val *v1, const struct f_val *v2); |
284 | int val_compare(const struct f_val *v1, const struct f_val *v2); | |
285 | void val_format(const struct f_val *v, buffer *buf); | |
b40c0f02 | 286 | char *val_format_str(struct linpool *lp, const struct f_val *v); |
52893045 MM |
287 | const char *val_dump(const struct f_val *v); |
288 | ||
289 | static inline int val_is_ip4(const struct f_val *v) | |
290 | { return (v->type == T_IP) && ipa_is_ip4(v->val.ip); } | |
291 | int val_in_range(const struct f_val *v1, const struct f_val *v2); | |
292 | ||
293 | int clist_set_type(const struct f_tree *set, struct f_val *v); | |
294 | static inline int eclist_set_type(const struct f_tree *set) | |
b2d6d294 | 295 | { return !set || set->from.type == T_EC; } |
52893045 | 296 | static inline int lclist_set_type(const struct f_tree *set) |
b2d6d294 | 297 | { return !set || set->from.type == T_LC; } |
8f3c6151 | 298 | static inline int path_set_type(const struct f_tree *set) |
b2d6d294 | 299 | { return !set || set->from.type == T_INT; } |
52893045 | 300 | |
92a85655 OZ |
301 | int clist_match_set(const struct adata *clist, const struct f_tree *set); |
302 | int eclist_match_set(const struct adata *list, const struct f_tree *set); | |
303 | int lclist_match_set(const struct adata *list, const struct f_tree *set); | |
304 | ||
52893045 MM |
305 | const struct adata *clist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos); |
306 | const struct adata *eclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos); | |
307 | const struct adata *lclist_filter(struct linpool *pool, const struct adata *list, const struct f_val *set, int pos); | |
308 | ||
309 | ||
310 | /* Special undef value for paths and clists */ | |
311 | static inline int | |
312 | undef_value(struct f_val v) | |
313 | { | |
314 | return ((v.type == T_PATH) || (v.type == T_CLIST) || | |
315 | (v.type == T_ECLIST) || (v.type == T_LCLIST)) && | |
316 | (v.val.ad == &null_adata); | |
317 | } | |
318 | ||
b2d6d294 | 319 | extern const struct f_val f_const_empty_path, f_const_empty_clist, f_const_empty_eclist, f_const_empty_lclist, f_const_empty_prefix_set; |
8bdb05ed | 320 | |
52893045 MM |
321 | enum filter_return f_eval(const struct f_line *expr, struct linpool *tmp_pool, struct f_val *pres); |
322 | ||
8bdb05ed | 323 | #endif |