]>
Commit | Line | Data |
---|---|---|
58ef912c MM |
1 | /* |
2 | * BIRD Internet Routing Daemon -- Routing Table | |
3 | * | |
50fe90ed | 4 | * (c) 1998--2000 Martin Mares <mj@ucw.cz> |
58ef912c MM |
5 | * |
6 | * Can be freely distributed and used under the terms of the GNU GPL. | |
7 | */ | |
8 | ||
9 | #ifndef _BIRD_ROUTE_H_ | |
10 | #define _BIRD_ROUTE_H_ | |
11 | ||
0e02abfd | 12 | #include "lib/lists.h" |
1feea03e | 13 | #include "lib/resource.h" |
a2ccbb0b | 14 | #include "lib/timer.h" |
58ef912c | 15 | |
2326b001 | 16 | struct protocol; |
4cc78c50 | 17 | struct proto; |
730f2e2c MM |
18 | struct symbol; |
19 | struct filter; | |
20 | struct cli; | |
2326b001 | 21 | |
58ef912c MM |
22 | /* |
23 | * Generic data structure for storing network prefixes. Also used | |
3ab001b9 | 24 | * for the master routing table. Currently implemented as a hash |
62aa008a | 25 | * table. |
58ef912c MM |
26 | * |
27 | * Available operations: | |
28 | * - insertion of new entry | |
29 | * - deletion of entry | |
62aa008a | 30 | * - searching for entry by network prefix |
3ab001b9 | 31 | * - asynchronous retrieval of fib contents |
58ef912c MM |
32 | */ |
33 | ||
34 | struct fib_node { | |
3ab001b9 MM |
35 | struct fib_node *next; /* Next in hash chain */ |
36 | struct fib_iterator *readers; /* List of readers of this node */ | |
58ef912c | 37 | byte pxlen; |
4c45595e | 38 | byte flags; /* User-defined */ |
12df4d90 | 39 | byte x0, x1; /* User-defined */ |
3ab001b9 MM |
40 | ip_addr prefix; /* In host order */ |
41 | }; | |
42 | ||
43 | struct fib_iterator { /* See lib/slists.h for an explanation */ | |
44 | struct fib_iterator *prev, *next; /* Must be synced with struct fib_node! */ | |
3ab001b9 MM |
45 | byte efef; /* 0xff to distinguish between iterator and node */ |
46 | byte pad[3]; | |
0ba8a614 | 47 | struct fib_node *node; /* Or NULL if freshly merged */ |
3ab001b9 | 48 | unsigned int hash; |
58ef912c MM |
49 | }; |
50 | ||
ce4aca09 MM |
51 | typedef void (*fib_init_func)(struct fib_node *); |
52 | ||
58ef912c | 53 | struct fib { |
62aa008a MM |
54 | pool *fib_pool; /* Pool holding all our data */ |
55 | slab *fib_slab; /* Slab holding all fib nodes */ | |
56 | struct fib_node **hash_table; /* Node hash table */ | |
a8b60382 | 57 | unsigned int hash_size; /* Number of hash table entries (a power of two) */ |
3ab001b9 MM |
58 | unsigned int hash_order; /* Binary logarithm of hash_size */ |
59 | unsigned int hash_shift; /* 16 - hash_log */ | |
a8b60382 MM |
60 | unsigned int entries; /* Number of entries */ |
61 | unsigned int entries_min, entries_max;/* Entry count limits (else start rehashing) */ | |
ce4aca09 | 62 | fib_init_func init; /* Constructor */ |
58ef912c MM |
63 | }; |
64 | ||
ce4aca09 | 65 | void fib_init(struct fib *, pool *, unsigned node_size, unsigned hash_order, fib_init_func init); |
58ef912c | 66 | void *fib_find(struct fib *, ip_addr *, int); /* Find or return NULL if doesn't exist */ |
58ef912c | 67 | void *fib_get(struct fib *, ip_addr *, int); /* Find or create new if nonexistent */ |
56d6c530 | 68 | void *fib_route(struct fib *, ip_addr, int); /* Longest-match routing lookup */ |
62aa008a | 69 | void fib_delete(struct fib *, void *); /* Remove fib entry */ |
a8b60382 | 70 | void fib_free(struct fib *); /* Destroy the fib */ |
3ab001b9 MM |
71 | void fib_check(struct fib *); /* Consistency check for debugging */ |
72 | ||
73 | void fit_init(struct fib_iterator *, struct fib *); /* Internal functions, don't call */ | |
74 | struct fib_node *fit_get(struct fib *, struct fib_iterator *); | |
75 | void fit_put(struct fib_iterator *, struct fib_node *); | |
a8b60382 | 76 | |
236d4eb8 | 77 | #define FIB_WALK(fib, z) do { \ |
62aa008a | 78 | struct fib_node *z, **ff = (fib)->hash_table; \ |
a8b60382 MM |
79 | unsigned int count = (fib)->hash_size; \ |
80 | while (count--) \ | |
236d4eb8 MM |
81 | for(z = *ff++; z; z=z->next) |
82 | ||
83 | #define FIB_WALK_END } while (0) | |
a8b60382 | 84 | |
3ab001b9 MM |
85 | #define FIB_ITERATE_INIT(it, fib) fit_init(it, fib) |
86 | ||
87 | #define FIB_ITERATE_START(fib, it, z) do { \ | |
88 | struct fib_node *z = fit_get(fib, it); \ | |
89 | unsigned int count = (fib)->hash_size; \ | |
90 | unsigned int hpos = (it)->hash; \ | |
91 | for(;;) { \ | |
8abbde02 MM |
92 | if (!z) \ |
93 | { \ | |
94 | if (++hpos >= count) \ | |
95 | break; \ | |
96 | z = (fib)->hash_table[hpos]; \ | |
97 | continue; \ | |
98 | } | |
3ab001b9 | 99 | |
2569bc40 | 100 | #define FIB_ITERATE_END(z) z = z->next; } } while(0) |
3ab001b9 MM |
101 | |
102 | #define FIB_ITERATE_PUT(it, z) fit_put(it, z) | |
103 | ||
58ef912c | 104 | /* |
08e2d625 MM |
105 | * Master Routing Tables. Generally speaking, each of them contains a FIB |
106 | * with each entry pointing to a list of route entries representing routes | |
107 | * to given network (with the selected one at the head). | |
108 | * | |
58ef912c | 109 | * Each of the RTE's contains variable data (the preference and protocol-dependent |
62aa008a | 110 | * metrics) and a pointer to a route attribute block common for many routes). |
08e2d625 MM |
111 | * |
112 | * It's guaranteed that there is at most one RTE for every (prefix,proto) pair. | |
58ef912c MM |
113 | */ |
114 | ||
0e02abfd MM |
115 | struct rtable_config { |
116 | node n; | |
117 | char *name; | |
118 | struct rtable *table; | |
7de45ba4 | 119 | struct proto_config *krt_attached; /* Kernel syncer attached to this table */ |
b9626ec6 MM |
120 | int gc_max_ops; /* Maximum number of operations before GC is run */ |
121 | int gc_min_time; /* Minimum time between two consecutive GC runs */ | |
0e02abfd MM |
122 | }; |
123 | ||
62aa008a | 124 | typedef struct rtable { |
0e02abfd | 125 | node n; /* Node in list of all tables */ |
62aa008a MM |
126 | struct fib fib; |
127 | char *name; /* Name of this table */ | |
0e02abfd | 128 | list hooks; /* List of announcement hooks */ |
9c11ec9e | 129 | int pipe_busy; /* Pipe loop detection */ |
50fe90ed | 130 | int use_count; /* Number of protocols using this table */ |
b9626ec6 | 131 | struct rtable_config *config; /* Configuration of this table */ |
50fe90ed MM |
132 | struct config *deleted; /* Table doesn't exist in current configuration, |
133 | * delete as soon as use_count becomes 0 and remove | |
134 | * obstacle from this routing table. | |
135 | */ | |
b9626ec6 MM |
136 | struct event *gc_event; /* Garbage collector event */ |
137 | int gc_counter; /* Number of operations since last GC */ | |
138 | bird_clock_t gc_time; /* Time of last GC */ | |
62aa008a MM |
139 | } rtable; |
140 | ||
58ef912c | 141 | typedef struct network { |
3ab001b9 | 142 | struct fib_node n; /* FIB flags reserved for kernel syncer */ |
58ef912c | 143 | struct rte *routes; /* Available routes for this network */ |
58ef912c MM |
144 | } net; |
145 | ||
146 | typedef struct rte { | |
147 | struct rte *next; | |
a0762910 | 148 | net *net; /* Network this RTE belongs to */ |
1b769b08 | 149 | struct rta *attrs; /* Attributes of this route */ |
58ef912c | 150 | byte flags; /* Flags (REF_...) */ |
481f6985 | 151 | byte pflags; /* Protocol-specific flags */ |
58ef912c | 152 | word pref; /* Route preference */ |
a2ccbb0b | 153 | bird_clock_t lastmod; /* Last modified */ |
58ef912c | 154 | union { /* Protocol-dependent data (metrics etc.) */ |
58ef912c MM |
155 | #ifdef CONFIG_RIP |
156 | struct { | |
feb6abe0 | 157 | node garbage; /* List for garbage collection */ |
58ef912c | 158 | byte metric; /* RIP metric */ |
481f6985 | 159 | u16 tag; /* External route tag */ |
ec21aecf | 160 | struct rip_entry *entry; |
58ef912c MM |
161 | } rip; |
162 | #endif | |
163 | #ifdef CONFIG_OSPF | |
164 | struct { | |
165 | u32 metric1, metric2; /* OSPF Type 1 and Type 2 metrics */ | |
481f6985 | 166 | u32 tag; /* External route tag */ |
58ef912c | 167 | } ospf; |
58ef912c | 168 | #endif |
c10421d3 MM |
169 | struct { /* Routes generated by krt sync (both temporary and inherited ones) */ |
170 | s8 src; /* Alleged route source (see krt.h) */ | |
171 | u8 proto; /* Kernel source protocol ID */ | |
172 | u8 type; /* Kernel route type */ | |
173 | u8 seen; /* Seen during last scan */ | |
174 | u32 metric; /* Kernel metric */ | |
175 | } krt; | |
58ef912c MM |
176 | } u; |
177 | } rte; | |
178 | ||
e2dc2f30 MM |
179 | #define REF_COW 1 /* Copy this rte on write */ |
180 | ||
0e02abfd | 181 | struct config; |
2326b001 MM |
182 | |
183 | void rt_init(void); | |
0e02abfd | 184 | void rt_preconfig(struct config *); |
50fe90ed MM |
185 | void rt_commit(struct config *new, struct config *old); |
186 | void rt_lock_table(rtable *); | |
187 | void rt_unlock_table(rtable *); | |
b9626ec6 | 188 | void rt_setup(pool *, rtable *, char *, struct rtable_config *); |
08e2d625 MM |
189 | static inline net *net_find(rtable *tab, ip_addr addr, unsigned len) { return (net *) fib_find(&tab->fib, &addr, len); } |
190 | static inline net *net_get(rtable *tab, ip_addr addr, unsigned len) { return (net *) fib_get(&tab->fib, &addr, len); } | |
2326b001 | 191 | rte *rte_find(net *net, struct proto *p); |
1b769b08 | 192 | rte *rte_get_temp(struct rta *); |
0e02abfd MM |
193 | void rte_update(rtable *tab, net *net, struct proto *p, rte *new); |
194 | void rte_discard(rtable *tab, rte *old); | |
a0762910 | 195 | void rte_dump(rte *); |
04925e90 | 196 | void rte_free(rte *); |
e2dc2f30 MM |
197 | rte *rte_do_cow(rte *); |
198 | static inline rte * rte_cow(rte *r) { return (r->flags & REF_COW) ? rte_do_cow(r) : r; } | |
2326b001 | 199 | void rt_dump(rtable *); |
a2ccbb0b | 200 | void rt_dump_all(void); |
ac5d8012 MM |
201 | int rt_feed_baby(struct proto *p); |
202 | void rt_feed_baby_abort(struct proto *p); | |
2569bc40 | 203 | void rt_prune(rtable *tab); |
0e02abfd | 204 | void rt_prune_all(void); |
b9626ec6 | 205 | struct rtable_config *rt_new_table(struct symbol *s); |
2326b001 | 206 | |
730f2e2c MM |
207 | struct rt_show_data { |
208 | ip_addr prefix; | |
209 | unsigned pxlen; | |
210 | rtable *table; | |
211 | struct filter *filter; | |
212 | int verbose; | |
213 | struct fib_iterator fit; | |
4d176e14 | 214 | struct proto *show_protocol; |
ea2ae6dd OZ |
215 | struct proto *export_protocol; |
216 | int export_mode, primary_only; | |
ce1da96e | 217 | struct config *running_on_config; |
23693958 | 218 | int net_counter, rt_counter, show_counter; |
56d6c530 | 219 | int stats, show_for; |
730f2e2c MM |
220 | }; |
221 | void rt_show(struct rt_show_data *); | |
222 | ||
58ef912c MM |
223 | /* |
224 | * Route Attributes | |
225 | * | |
226 | * Beware: All standard BGP attributes must be represented here instead | |
227 | * of making them local to the route. This is needed to ensure proper | |
228 | * construction of BGP route attribute lists. | |
229 | */ | |
230 | ||
1b769b08 | 231 | typedef struct rta { |
ee76a92a | 232 | struct rta *next, **pprev; /* Hash chain */ |
58ef912c MM |
233 | struct proto *proto; /* Protocol instance */ |
234 | unsigned uc; /* Use count */ | |
235 | byte source; /* Route source (RTS_...) */ | |
a8b60382 | 236 | byte scope; /* Route scope (SCOPE_... -- see ip.h) */ |
58ef912c MM |
237 | byte cast; /* Casting type (RTC_...) */ |
238 | byte dest; /* Route destination type (RTD_...) */ | |
c8518ae1 | 239 | byte flags; /* Route flags (RTF_...), now unused */ |
04925e90 | 240 | byte aflags; /* Attribute cache flags (RTAF_...) */ |
ee76a92a | 241 | u16 hash_key; /* Hash over important fields */ |
58ef912c | 242 | ip_addr gw; /* Next hop */ |
a8b60382 | 243 | ip_addr from; /* Advertising router */ |
58ef912c | 244 | struct iface *iface; /* Outgoing interface */ |
2727bb7c | 245 | struct ea_list *eattrs; /* Extended Attribute chain */ |
58ef912c MM |
246 | } rta; |
247 | ||
618533af | 248 | #define RTS_DUMMY 0 /* Dummy route to be removed soon */ |
58ef912c MM |
249 | #define RTS_STATIC 1 /* Normal static route */ |
250 | #define RTS_INHERIT 2 /* Route inherited from kernel */ | |
251 | #define RTS_DEVICE 3 /* Device route */ | |
252 | #define RTS_STATIC_DEVICE 4 /* Static device route */ | |
253 | #define RTS_REDIRECT 5 /* Learned via redirect */ | |
254 | #define RTS_RIP 6 /* RIP route */ | |
beaf86e1 | 255 | #define RTS_OSPF 7 /* OSPF route */ |
8bf684ec | 256 | #define RTS_OSPF_IA 8 /* OSPF inter-area route */ |
98ac6176 OF |
257 | #define RTS_OSPF_EXT1 9 /* OSPF external route type 1 */ |
258 | #define RTS_OSPF_EXT2 10 /* OSPF external route type 2 */ | |
259 | #define RTS_BGP 11 /* BGP route */ | |
260 | #define RTS_PIPE 12 /* Inter-table wormhole */ | |
58ef912c | 261 | |
58ef912c MM |
262 | #define RTC_UNICAST 0 |
263 | #define RTC_BROADCAST 1 | |
264 | #define RTC_MULTICAST 2 | |
265 | #define RTC_ANYCAST 3 /* IPv6 Anycast */ | |
266 | ||
267 | #define RTD_ROUTER 0 /* Next hop is neighbor router */ | |
268 | #define RTD_DEVICE 1 /* Points to device */ | |
269 | #define RTD_BLACKHOLE 2 /* Silently drop packets */ | |
270 | #define RTD_UNREACHABLE 3 /* Reject as unreachable */ | |
271 | #define RTD_PROHIBIT 4 /* Administratively prohibited */ | |
a60277b9 | 272 | #define RTD_NONE 5 /* Invalid RTD */ |
58ef912c | 273 | |
04925e90 MM |
274 | #define RTAF_CACHED 1 /* This is a cached rta */ |
275 | ||
58ef912c MM |
276 | /* |
277 | * Extended Route Attributes | |
278 | */ | |
279 | ||
280 | typedef struct eattr { | |
b77ae37d MM |
281 | word id; /* EA_CODE(EAP_..., protocol-dependent ID) */ |
282 | byte flags; /* Protocol-dependent flags */ | |
283 | byte type; /* Attribute type and several flags (EAF_...) */ | |
58ef912c MM |
284 | union { |
285 | u32 data; | |
286 | struct adata *ptr; /* Attribute data elsewhere */ | |
287 | } u; | |
288 | } eattr; | |
289 | ||
290 | #define EAP_GENERIC 0 /* Generic attributes */ | |
291 | #define EAP_BGP 1 /* BGP attributes */ | |
9c9e49ac | 292 | #define EAP_RIP 2 /* RIP */ |
5919c66e MM |
293 | #define EAP_OSPF 3 /* OSPF */ |
294 | #define EAP_MAX 4 | |
58ef912c | 295 | |
b77ae37d MM |
296 | #define EA_CODE(proto,id) (((proto) << 8) | (id)) |
297 | #define EA_PROTO(ea) ((ea) >> 8) | |
298 | #define EA_ID(ea) ((ea) & 0xff) | |
299 | ||
8d24b689 MM |
300 | #define EA_CODE_MASK 0xffff |
301 | #define EA_ALLOW_UNDEF 0x10000 /* ea_find: allow EAF_TYPE_UNDEF */ | |
302 | ||
b77ae37d MM |
303 | #define EAF_TYPE_MASK 0x0f /* Mask with this to get type */ |
304 | #define EAF_TYPE_INT 0x01 /* 32-bit signed integer number */ | |
305 | #define EAF_TYPE_OPAQUE 0x02 /* Opaque byte string (not filterable) */ | |
ca97b489 MM |
306 | #define EAF_TYPE_IP_ADDRESS 0x04 /* IP address */ |
307 | #define EAF_TYPE_ROUTER_ID 0x05 /* Router ID (IPv4 address) */ | |
b475c543 MM |
308 | #define EAF_TYPE_AS_PATH 0x06 /* BGP AS path (encoding per RFC 1771:4.3) */ |
309 | #define EAF_TYPE_INT_SET 0x0a /* Set of u32's (e.g., a community list) */ | |
8d24b689 | 310 | #define EAF_TYPE_UNDEF 0x0f /* `force undefined' entry */ |
b77ae37d | 311 | #define EAF_EMBEDDED 0x01 /* Data stored in eattr.u.data (part of type spec) */ |
ca97b489 | 312 | #define EAF_VAR_LENGTH 0x02 /* Attribute length is variable (part of type spec) */ |
51a183af | 313 | #define EAF_ORIGINATED 0x40 /* The attribute has originated locally */ |
9f4929e7 | 314 | #define EAF_TEMP 0x80 /* A temporary attribute (the one stored in the tmp attr list) */ |
58ef912c MM |
315 | |
316 | struct adata { | |
b475c543 | 317 | unsigned int length; /* Length of data */ |
58ef912c MM |
318 | byte data[0]; |
319 | }; | |
320 | ||
321 | typedef struct ea_list { | |
322 | struct ea_list *next; /* In case we have an override list */ | |
b77ae37d | 323 | byte flags; /* Flags: EALF_... */ |
58ef912c | 324 | byte rfu; |
b77ae37d | 325 | word count; /* Number of attributes */ |
58ef912c MM |
326 | eattr attrs[0]; /* Attribute definitions themselves */ |
327 | } ea_list; | |
328 | ||
b77ae37d MM |
329 | #define EALF_SORTED 1 /* Attributes are sorted by code */ |
330 | #define EALF_BISECT 2 /* Use interval bisection for searching */ | |
331 | #define EALF_CACHED 4 /* Attributes belonging to cached rta */ | |
58ef912c | 332 | |
b77ae37d | 333 | eattr *ea_find(ea_list *, unsigned ea); |
c0100454 | 334 | int ea_get_int(ea_list *, unsigned ea, int def); |
b77ae37d MM |
335 | void ea_dump(ea_list *); |
336 | void ea_sort(ea_list *); /* Sort entries in all sub-lists */ | |
6f57dcc0 | 337 | unsigned ea_scan(ea_list *); /* How many bytes do we need for merged ea_list */ |
b77ae37d | 338 | void ea_merge(ea_list *from, ea_list *to); /* Merge sub-lists to allocated buffer */ |
6f57dcc0 MM |
339 | int ea_same(ea_list *x, ea_list *y); /* Test whether two ea_lists are identical */ |
340 | unsigned int ea_hash(ea_list *e); /* Calculate 16-bit hash value */ | |
341 | void ea_format(eattr *e, byte *buf); | |
c6add07f | 342 | #define EA_FORMAT_BUF_SIZE 256 |
ce1da96e | 343 | ea_list *ea_append(ea_list *to, ea_list *what); |
58ef912c | 344 | |
2326b001 MM |
345 | void rta_init(void); |
346 | rta *rta_lookup(rta *); /* Get rta equivalent to this one, uc++ */ | |
347 | static inline rta *rta_clone(rta *r) { r->uc++; return r; } | |
b77ae37d MM |
348 | void rta__free(rta *r); |
349 | static inline void rta_free(rta *r) { if (r && !--r->uc) rta__free(r); } | |
2326b001 MM |
350 | void rta_dump(rta *); |
351 | void rta_dump_all(void); | |
ce1da96e | 352 | void rta_show(struct cli *, rta *, ea_list *); |
2326b001 | 353 | |
3991d84e MM |
354 | extern struct protocol *attr_class_to_protocol[EAP_MAX]; |
355 | ||
a8b60382 MM |
356 | /* |
357 | * Default protocol preferences | |
358 | */ | |
359 | ||
360 | #define DEF_PREF_DIRECT 240 /* Directly connected */ | |
361 | #define DEF_PREF_STATIC 200 /* Static route */ | |
916c8c0a | 362 | #define DEF_PREF_OSPF 150 /* OSPF intra-area, inter-area and type 1 external routes */ |
a8b60382 MM |
363 | #define DEF_PREF_RIP 120 /* RIP */ |
364 | #define DEF_PREF_BGP 100 /* BGP */ | |
beaf86e1 | 365 | #define DEF_PREF_PIPE 70 /* Routes piped from other tables */ |
916c8c0a | 366 | #define DEF_PREF_INHERITED 10 /* Routes inherited from other routing daemons */ |
a8b60382 | 367 | |
58ef912c | 368 | #endif |