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_CONSTANT, 0, 'c') \
41 F(FI_VARIABLE, 0, 'V') \
42 F(FI_CONSTANT_INDIRECT, 0, 'C') \
44 F(FI_CONDITION, 0, '?') \
46 F(FI_PRINT_AND_DIE, 'p', ',') \
47 F(FI_RTA_GET, 0, 'a') \
48 F(FI_RTA_SET, 'a', 'S') \
49 F(FI_EA_GET, 'e', 'a') \
50 F(FI_EA_SET, 'e', 'S') \
51 F(FI_PREF_GET, 0, 'P') \
52 F(FI_PREF_SET, 'P', 'S') \
53 F(FI_LENGTH, 0, 'L') \
55 F(FI_AS_PATH_FIRST, 'a', 'f') \
56 F(FI_AS_PATH_LAST, 'a', 'l') \
57 F(FI_AS_PATH_LAST_NAG, 'a', 'L') \
58 F(FI_RETURN, 0, 'r') \
59 F(FI_CALL, 'c', 'a') \
60 F(FI_CLEAR_LOCAL_VARS, 'c', 'V') \
61 F(FI_SWITCH, 'S', 'W') \
62 F(FI_IP_MASK, 'i', 'M') \
64 F(FI_PATH_PREPEND, 'A', 'p') \
65 F(FI_CLIST_ADD_DEL, 'C', 'a') \
66 F(FI_ROA_CHECK, 'R', 'C')
68 enum f_instruction_code
{
75 struct f_inst
{ /* Instruction */
76 struct f_inst
*next
; /* Structure is 16 bytes, anyway */
77 enum f_instruction_code fi_code
;
93 /* Not enough fields in f_inst for three args used by roa_check() */
94 struct f_inst_roa_check
{
96 struct roa_table_config
*rtc
;
107 #define INST3(x) (((struct f_inst3 *) x)->a3)
113 #define LEN_MASK 0xff
114 #define LEN_PLUS 0x1000000
115 #define LEN_MINUS 0x2000000
116 #define LEN_RANGE 0x4000000
117 /* If range then prefix must be in range (len >> 16 & 0xff, len >> 8 & 0xff) */
126 /* ip_addr ip; Folded into prefix */
132 struct f_path_mask
*path_mask
;
136 struct f_dynamic_attr
{
142 struct f_static_attr
{
153 struct f_inst
*f_new_inst(enum f_instruction_code fi_code
);
154 struct f_inst
*f_new_inst_da(enum f_instruction_code fi_code
, struct f_dynamic_attr da
);
155 struct f_inst
*f_new_inst_sa(enum f_instruction_code fi_code
, struct f_static_attr sa
);
156 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 */
157 { 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 */
158 static inline struct f_static_attr
f_new_static_attr(int f_type
, int code
, int readonly
)
159 { return (struct f_static_attr
) { .f_type
= f_type
, .sa_code
= code
, .readonly
= readonly
}; }
160 struct f_tree
*f_new_tree(void);
161 struct f_inst
*f_generate_complex(int operation
, int operation_aux
, struct f_dynamic_attr da
, struct f_inst
*argument
);
162 struct f_inst
*f_generate_roa_check(struct symbol
*sym
, struct f_inst
*prefix
, struct f_inst
*asn
);
165 struct f_tree
*build_tree(struct f_tree
*);
166 struct f_tree
*find_tree(struct f_tree
*t
, struct f_val val
);
167 int same_tree(struct f_tree
*t1
, struct f_tree
*t2
);
168 void tree_format(struct f_tree
*t
, buffer
*buf
);
170 struct f_trie
*f_new_trie(linpool
*lp
, uint node_size
);
171 void *trie_add_prefix(struct f_trie
*t
, ip_addr px
, int plen
, int l
, int h
);
172 int trie_match_prefix(struct f_trie
*t
, ip_addr px
, int plen
);
173 int trie_same(struct f_trie
*t1
, struct f_trie
*t2
);
174 void trie_format(struct f_trie
*t
, buffer
*buf
);
176 void fprefix_get_bounds(struct f_prefix
*px
, int *l
, int *h
);
179 trie_add_fprefix(struct f_trie
*t
, struct f_prefix
*px
)
182 fprefix_get_bounds(px
, &l
, &h
);
183 trie_add_prefix(t
, px
->ip
, px
->len
& LEN_MASK
, l
, h
);
187 trie_match_fprefix(struct f_trie
*t
, struct f_prefix
*px
)
189 return trie_match_prefix(t
, px
->ip
, px
->len
& LEN_MASK
);
196 int f_run(struct filter
*filter
, struct rte
**rte
, struct ea_list
**tmp_attrs
, struct linpool
*tmp_pool
, int flags
);
197 struct f_val
f_eval_rte(struct f_inst
*expr
, struct rte
**rte
, struct linpool
*tmp_pool
);
198 struct f_val
f_eval(struct f_inst
*expr
, struct linpool
*tmp_pool
);
199 uint
f_eval_int(struct f_inst
*expr
);
201 char *filter_name(struct filter
*filter
);
202 int filter_same(struct filter
*new, struct filter
*old
);
204 int i_same(struct f_inst
*f1
, struct f_inst
*f2
);
206 int val_compare(struct f_val v1
, struct f_val v2
);
207 int val_same(struct f_val v1
, struct f_val v2
);
209 void val_format(struct f_val v
, buffer
*buf
);
214 #define F_ACCEPT 2 /* Need to preserve ordering: accepts < rejects! */
219 #define FILTER_ACCEPT NULL
220 #define FILTER_REJECT ((void *) 1)
222 /* Type numbers must be in 0..0xff range */
226 /* Do not use type of zero, that way we'll see errors easier. */
229 /* User visible types, which fit in int */
232 #define T_PAIR 0x12 /* Notice that pair is stored as integer: first << 16 | second */
235 /* Put enumerational types in 0x30..0x3f range */
236 #define T_ENUM_LO 0x30
237 #define T_ENUM_HI 0x3f
239 #define T_ENUM_RTS 0x30
240 #define T_ENUM_BGP_ORIGIN 0x31
241 #define T_ENUM_SCOPE 0x32
242 #define T_ENUM_RTC 0x33
243 #define T_ENUM_RTD 0x34
244 #define T_ENUM_ROA 0x35
245 #define T_ENUM_RA_PREFERENCE 0x36
246 /* new enums go here */
247 #define T_ENUM_EMPTY 0x3f /* Special hack for atomic_aggr */
249 #define T_ENUM T_ENUM_LO ... T_ENUM_HI
253 #define T_PREFIX 0x21
254 #define T_STRING 0x22
255 #define T_PATH_MASK 0x23 /* mask for BGP path */
256 #define T_PATH 0x24 /* BGP path */
257 #define T_CLIST 0x25 /* Community list */
258 #define T_EC 0x26 /* Extended community value, u64 */
259 #define T_ECLIST 0x27 /* Extended community list */
260 #define T_LC 0x28 /* Large community value, lcomm */
261 #define T_LCLIST 0x29 /* Large community list */
263 #define T_RETURN 0x40
265 #define T_PREFIX_SET 0x81
277 #define SA_IFINDEX 10
281 struct f_tree
*left
, *right
;
282 struct f_val from
, to
;
288 ip_addr addr
, mask
, accept
;
290 struct f_trie_node
*c
[2];
298 struct f_trie_node root
[0]; /* Root trie node follows */
301 #define NEW_F_VAL struct f_val * val; val = cfg_alloc(sizeof(struct f_val));
303 #define FF_FORCE_TMPATTR 1 /* Force all attributes to be temporary */
304 #define FF_SILENT 2 /* Silent filter execution */