2 * BIRD Internet Routing Daemon -- Filters
4 * (c) 1999 Pavel Machek <pavel@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
12 #include "lib/resource.h"
14 #include "nest/route.h"
15 #include "nest/attrs.h"
17 /* Filter instruction types */
19 #define FI__TWOCHAR(a,b) ((a<<8) | b)
22 F(FI_SUBTRACT, 0, '-') \
23 F(FI_MULTIPLY, 0, '*') \
24 F(FI_DIVIDE, 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') \
37 F(FI_NOT_MATCH, '!', '~') \
38 F(FI_DEFINED, 'd', 'e') \
40 F(FI_IS_V4, 'I', 'i') \
42 F(FI_CONSTANT, 0, 'c') \
43 F(FI_VARIABLE, 0, 'V') \
44 F(FI_CONSTANT_INDIRECT, 0, 'C') \
46 F(FI_CONDITION, 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') \
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') \
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')
76 enum f_instruction_code
{
84 const char *f_instruction_name(enum f_instruction_code fi
);
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_* */
93 } a1
; /* The first argument */
97 } a2
; /* The second argument */
101 } a3
; /* The third argument */
108 /* Not enough fields in f_inst for three args used by roa_check() */
109 struct f_inst_roa_check
{
111 struct rtable_config
*rtc
;
131 struct f_path_mask
*path_mask
;
135 struct f_dynamic_attr
{
141 struct f_static_attr
{
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
);
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
);
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
);
182 F_ACCEPT
, /* Need to preserve ordering: accepts < rejects! */
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
);
193 char *filter_name(struct filter
*filter
);
194 int filter_same(struct filter
*new, struct filter
*old
);
196 int i_same(struct f_inst
*f1
, struct f_inst
*f2
);
198 int val_compare(struct f_val v1
, struct f_val v2
);
199 int val_same(struct f_val v1
, struct f_val v2
);
201 void val_format(struct f_val v
, buffer
*buf
);
203 #define FILTER_ACCEPT NULL
204 #define FILTER_REJECT ((void *) 1)
205 #define FILTER_UNDEF ((void *) 2) /* Used in BGP */
207 /* Type numbers must be in 0..0xff range */
211 /* Do not use type of zero, that way we'll see errors easier. */
214 /* User visible types, which fit in int */
217 #define T_PAIR 0x12 /* Notice that pair is stored as integer: first << 16 | second */
220 /* Put enumerational types in 0x30..0x3f range */
221 #define T_ENUM_LO 0x30
222 #define T_ENUM_HI 0x3f
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
233 /* new enums go here */
234 #define T_ENUM_EMPTY 0x3f /* Special hack for atomic_aggr */
236 #define T_ENUM T_ENUM_LO ... T_ENUM_HI
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 */
252 #define T_PREFIX_SET 0x81
267 struct f_tree
*left
, *right
;
268 struct f_val from
, to
;
274 ip_addr addr
, mask
, accept
;
276 struct f_trie_node
*c
[2];
284 struct f_trie_node root
[0]; /* Root trie node follows */
287 #define NEW_F_VAL struct f_val * val; val = cfg_alloc(sizeof(struct f_val));
289 #define FF_SILENT 2 /* Silent filter execution */
291 /* Custom route attributes */
292 struct custom_attribute
{
294 struct f_dynamic_attr
*fda
;
298 struct custom_attribute
*ca_lookup(pool
*p
, const char *name
, int ea_type
);
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 */
308 /* Hook for call bt_assert() function in configuration */
309 extern void (*bt_assert_hook
)(int result
, struct f_inst
*assert);