]>
Commit | Line | Data |
---|---|---|
58ef912c MM |
1 | /* |
2 | * BIRD Internet Routing Daemon -- Routing Table | |
3 | * | |
e2dc2f30 | 4 | * (c) 1998--1999 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; |
2326b001 | 18 | |
58ef912c MM |
19 | /* |
20 | * Generic data structure for storing network prefixes. Also used | |
3ab001b9 | 21 | * for the master routing table. Currently implemented as a hash |
62aa008a | 22 | * table. |
58ef912c MM |
23 | * |
24 | * Available operations: | |
25 | * - insertion of new entry | |
26 | * - deletion of entry | |
62aa008a | 27 | * - searching for entry by network prefix |
3ab001b9 | 28 | * - asynchronous retrieval of fib contents |
58ef912c MM |
29 | */ |
30 | ||
31 | struct fib_node { | |
3ab001b9 MM |
32 | struct fib_node *next; /* Next in hash chain */ |
33 | struct fib_iterator *readers; /* List of readers of this node */ | |
58ef912c | 34 | byte pxlen; |
4c45595e | 35 | byte flags; /* User-defined */ |
12df4d90 | 36 | byte x0, x1; /* User-defined */ |
3ab001b9 MM |
37 | ip_addr prefix; /* In host order */ |
38 | }; | |
39 | ||
40 | struct fib_iterator { /* See lib/slists.h for an explanation */ | |
41 | struct fib_iterator *prev, *next; /* Must be synced with struct fib_node! */ | |
42 | struct fib_node *node; /* Or NULL if freshly merged */ | |
43 | byte efef; /* 0xff to distinguish between iterator and node */ | |
44 | byte pad[3]; | |
45 | unsigned int hash; | |
58ef912c MM |
46 | }; |
47 | ||
48 | struct fib { | |
62aa008a MM |
49 | pool *fib_pool; /* Pool holding all our data */ |
50 | slab *fib_slab; /* Slab holding all fib nodes */ | |
51 | struct fib_node **hash_table; /* Node hash table */ | |
a8b60382 | 52 | unsigned int hash_size; /* Number of hash table entries (a power of two) */ |
3ab001b9 MM |
53 | unsigned int hash_order; /* Binary logarithm of hash_size */ |
54 | unsigned int hash_shift; /* 16 - hash_log */ | |
a8b60382 MM |
55 | unsigned int entries; /* Number of entries */ |
56 | unsigned int entries_min, entries_max;/* Entry count limits (else start rehashing) */ | |
58ef912c MM |
57 | void (*init)(struct fib_node *); /* Constructor */ |
58 | }; | |
59 | ||
3ab001b9 | 60 | void fib_init(struct fib *, pool *, unsigned node_size, unsigned hash_order, void (*init)(struct fib_node *)); |
58ef912c | 61 | void *fib_find(struct fib *, ip_addr *, int); /* Find or return NULL if doesn't exist */ |
58ef912c | 62 | void *fib_get(struct fib *, ip_addr *, int); /* Find or create new if nonexistent */ |
62aa008a | 63 | void fib_delete(struct fib *, void *); /* Remove fib entry */ |
a8b60382 | 64 | void fib_free(struct fib *); /* Destroy the fib */ |
3ab001b9 MM |
65 | void fib_check(struct fib *); /* Consistency check for debugging */ |
66 | ||
67 | void fit_init(struct fib_iterator *, struct fib *); /* Internal functions, don't call */ | |
68 | struct fib_node *fit_get(struct fib *, struct fib_iterator *); | |
69 | void fit_put(struct fib_iterator *, struct fib_node *); | |
a8b60382 | 70 | |
236d4eb8 | 71 | #define FIB_WALK(fib, z) do { \ |
62aa008a | 72 | struct fib_node *z, **ff = (fib)->hash_table; \ |
a8b60382 MM |
73 | unsigned int count = (fib)->hash_size; \ |
74 | while (count--) \ | |
236d4eb8 MM |
75 | for(z = *ff++; z; z=z->next) |
76 | ||
77 | #define FIB_WALK_END } while (0) | |
a8b60382 | 78 | |
3ab001b9 MM |
79 | #define FIB_ITERATE_INIT(it, fib) fit_init(it, fib) |
80 | ||
81 | #define FIB_ITERATE_START(fib, it, z) do { \ | |
82 | struct fib_node *z = fit_get(fib, it); \ | |
83 | unsigned int count = (fib)->hash_size; \ | |
84 | unsigned int hpos = (it)->hash; \ | |
85 | for(;;) { \ | |
2569bc40 | 86 | fis_again: if (!z) { \ |
3ab001b9 MM |
87 | if (++hpos >= count) \ |
88 | break; \ | |
89 | z = (fib)->hash_table[hpos]; \ | |
2569bc40 | 90 | goto fis_again; \ |
3ab001b9 MM |
91 | } |
92 | ||
2569bc40 | 93 | #define FIB_ITERATE_END(z) z = z->next; } } while(0) |
3ab001b9 MM |
94 | |
95 | #define FIB_ITERATE_PUT(it, z) fit_put(it, z) | |
96 | ||
58ef912c | 97 | /* |
08e2d625 MM |
98 | * Master Routing Tables. Generally speaking, each of them contains a FIB |
99 | * with each entry pointing to a list of route entries representing routes | |
100 | * to given network (with the selected one at the head). | |
101 | * | |
58ef912c | 102 | * Each of the RTE's contains variable data (the preference and protocol-dependent |
62aa008a | 103 | * metrics) and a pointer to a route attribute block common for many routes). |
08e2d625 MM |
104 | * |
105 | * It's guaranteed that there is at most one RTE for every (prefix,proto) pair. | |
58ef912c MM |
106 | */ |
107 | ||
0e02abfd MM |
108 | struct rtable_config { |
109 | node n; | |
110 | char *name; | |
111 | struct rtable *table; | |
7de45ba4 | 112 | struct proto_config *krt_attached; /* Kernel syncer attached to this table */ |
0e02abfd MM |
113 | }; |
114 | ||
62aa008a | 115 | typedef struct rtable { |
0e02abfd | 116 | node n; /* Node in list of all tables */ |
62aa008a MM |
117 | struct fib fib; |
118 | char *name; /* Name of this table */ | |
0e02abfd | 119 | list hooks; /* List of announcement hooks */ |
9c11ec9e | 120 | int pipe_busy; /* Pipe loop detection */ |
62aa008a MM |
121 | } rtable; |
122 | ||
58ef912c | 123 | typedef struct network { |
3ab001b9 | 124 | struct fib_node n; /* FIB flags reserved for kernel syncer */ |
58ef912c | 125 | struct rte *routes; /* Available routes for this network */ |
58ef912c MM |
126 | } net; |
127 | ||
128 | typedef struct rte { | |
129 | struct rte *next; | |
a0762910 | 130 | net *net; /* Network this RTE belongs to */ |
1b769b08 | 131 | struct rta *attrs; /* Attributes of this route */ |
58ef912c | 132 | byte flags; /* Flags (REF_...) */ |
481f6985 | 133 | byte pflags; /* Protocol-specific flags */ |
58ef912c | 134 | word pref; /* Route preference */ |
a2ccbb0b | 135 | bird_clock_t lastmod; /* Last modified */ |
58ef912c | 136 | union { /* Protocol-dependent data (metrics etc.) */ |
58ef912c MM |
137 | #ifdef CONFIG_RIP |
138 | struct { | |
feb6abe0 | 139 | node garbage; /* List for garbage collection */ |
58ef912c | 140 | byte metric; /* RIP metric */ |
481f6985 | 141 | u16 tag; /* External route tag */ |
58ef912c MM |
142 | } rip; |
143 | #endif | |
144 | #ifdef CONFIG_OSPF | |
145 | struct { | |
146 | u32 metric1, metric2; /* OSPF Type 1 and Type 2 metrics */ | |
481f6985 | 147 | u32 tag; /* External route tag */ |
58ef912c MM |
148 | } ospf; |
149 | #endif | |
150 | #ifdef CONFIG_BGP | |
151 | struct { | |
152 | } bgp; | |
153 | #endif | |
c10421d3 MM |
154 | struct { /* Routes generated by krt sync (both temporary and inherited ones) */ |
155 | s8 src; /* Alleged route source (see krt.h) */ | |
156 | u8 proto; /* Kernel source protocol ID */ | |
157 | u8 type; /* Kernel route type */ | |
158 | u8 seen; /* Seen during last scan */ | |
159 | u32 metric; /* Kernel metric */ | |
160 | } krt; | |
58ef912c MM |
161 | } u; |
162 | } rte; | |
163 | ||
e2dc2f30 MM |
164 | #define REF_COW 1 /* Copy this rte on write */ |
165 | ||
0e02abfd | 166 | struct config; |
2326b001 MM |
167 | |
168 | void rt_init(void); | |
0e02abfd MM |
169 | void rt_preconfig(struct config *); |
170 | void rt_commit(struct config *); | |
c10421d3 | 171 | void rt_setup(pool *, rtable *, char *); |
08e2d625 MM |
172 | static inline net *net_find(rtable *tab, ip_addr addr, unsigned len) { return (net *) fib_find(&tab->fib, &addr, len); } |
173 | static inline net *net_get(rtable *tab, ip_addr addr, unsigned len) { return (net *) fib_get(&tab->fib, &addr, len); } | |
2326b001 | 174 | rte *rte_find(net *net, struct proto *p); |
1b769b08 | 175 | rte *rte_get_temp(struct rta *); |
0e02abfd MM |
176 | void rte_update(rtable *tab, net *net, struct proto *p, rte *new); |
177 | void rte_discard(rtable *tab, rte *old); | |
a0762910 | 178 | void rte_dump(rte *); |
04925e90 | 179 | void rte_free(rte *); |
e2dc2f30 MM |
180 | rte *rte_do_cow(rte *); |
181 | static inline rte * rte_cow(rte *r) { return (r->flags & REF_COW) ? rte_do_cow(r) : r; } | |
2326b001 | 182 | void rt_dump(rtable *); |
a2ccbb0b | 183 | void rt_dump_all(void); |
47b79306 | 184 | void rt_feed_baby(struct proto *p); |
2569bc40 | 185 | void rt_prune(rtable *tab); |
0e02abfd | 186 | void rt_prune_all(void); |
2326b001 | 187 | |
58ef912c MM |
188 | /* |
189 | * Route Attributes | |
190 | * | |
191 | * Beware: All standard BGP attributes must be represented here instead | |
192 | * of making them local to the route. This is needed to ensure proper | |
193 | * construction of BGP route attribute lists. | |
194 | */ | |
195 | ||
1b769b08 MM |
196 | typedef struct rta { |
197 | struct rta *next, *prev; /* Hash chain */ | |
198 | struct rta *garbage; /* Garbage collector chain */ | |
58ef912c MM |
199 | struct proto *proto; /* Protocol instance */ |
200 | unsigned uc; /* Use count */ | |
201 | byte source; /* Route source (RTS_...) */ | |
a8b60382 | 202 | byte scope; /* Route scope (SCOPE_... -- see ip.h) */ |
58ef912c MM |
203 | byte cast; /* Casting type (RTC_...) */ |
204 | byte dest; /* Route destination type (RTD_...) */ | |
58ef912c | 205 | byte flags; /* Route flags (RTF_...) */ |
04925e90 | 206 | byte aflags; /* Attribute cache flags (RTAF_...) */ |
08e2d625 | 207 | byte rfu, rfu2; /* Padding */ |
58ef912c | 208 | ip_addr gw; /* Next hop */ |
a8b60382 | 209 | ip_addr from; /* Advertising router */ |
58ef912c | 210 | struct iface *iface; /* Outgoing interface */ |
2727bb7c | 211 | struct ea_list *eattrs; /* Extended Attribute chain */ |
58ef912c MM |
212 | } rta; |
213 | ||
618533af | 214 | #define RTS_DUMMY 0 /* Dummy route to be removed soon */ |
58ef912c MM |
215 | #define RTS_STATIC 1 /* Normal static route */ |
216 | #define RTS_INHERIT 2 /* Route inherited from kernel */ | |
217 | #define RTS_DEVICE 3 /* Device route */ | |
218 | #define RTS_STATIC_DEVICE 4 /* Static device route */ | |
219 | #define RTS_REDIRECT 5 /* Learned via redirect */ | |
220 | #define RTS_RIP 6 /* RIP route */ | |
221 | #define RTS_RIP_EXT 7 /* RIP external route */ | |
222 | #define RTS_OSPF 8 /* OSPF route */ | |
223 | #define RTS_OSPF_EXT 9 /* OSPF external route */ | |
224 | #define RTS_OSPF_IA 10 /* OSPF inter-area route */ | |
481f6985 | 225 | #define RTS_OSPF_BOUNDARY 11 /* OSPF route to boundary router (???) */ |
58ef912c | 226 | #define RTS_BGP 12 /* BGP route */ |
9c11ec9e | 227 | #define RTS_PIPE 13 /* Inter-table wormhole */ |
58ef912c | 228 | |
58ef912c MM |
229 | #define RTC_UNICAST 0 |
230 | #define RTC_BROADCAST 1 | |
231 | #define RTC_MULTICAST 2 | |
232 | #define RTC_ANYCAST 3 /* IPv6 Anycast */ | |
233 | ||
234 | #define RTD_ROUTER 0 /* Next hop is neighbor router */ | |
235 | #define RTD_DEVICE 1 /* Points to device */ | |
236 | #define RTD_BLACKHOLE 2 /* Silently drop packets */ | |
237 | #define RTD_UNREACHABLE 3 /* Reject as unreachable */ | |
238 | #define RTD_PROHIBIT 4 /* Administratively prohibited */ | |
239 | ||
481f6985 | 240 | #define RTF_EXTERIOR 1 /* Learned via exterior protocol */ |
a8b60382 | 241 | #define RTF_TAGGED 2 /* Tagged external route learned via IGP */ |
481f6985 | 242 | |
04925e90 MM |
243 | #define RTAF_CACHED 1 /* This is a cached rta */ |
244 | ||
58ef912c MM |
245 | /* |
246 | * Extended Route Attributes | |
247 | */ | |
248 | ||
249 | typedef struct eattr { | |
b77ae37d MM |
250 | word id; /* EA_CODE(EAP_..., protocol-dependent ID) */ |
251 | byte flags; /* Protocol-dependent flags */ | |
252 | byte type; /* Attribute type and several flags (EAF_...) */ | |
58ef912c MM |
253 | union { |
254 | u32 data; | |
255 | struct adata *ptr; /* Attribute data elsewhere */ | |
256 | } u; | |
257 | } eattr; | |
258 | ||
b77ae37d | 259 | /* FIXME: Introduce real protocol numbers? */ |
58ef912c MM |
260 | #define EAP_GENERIC 0 /* Generic attributes */ |
261 | #define EAP_BGP 1 /* BGP attributes */ | |
9c9e49ac | 262 | #define EAP_RIP 2 /* RIP */ |
58ef912c | 263 | |
b77ae37d MM |
264 | #define EA_CODE(proto,id) (((proto) << 8) | (id)) |
265 | #define EA_PROTO(ea) ((ea) >> 8) | |
266 | #define EA_ID(ea) ((ea) & 0xff) | |
267 | ||
268 | #define EAF_TYPE_MASK 0x0f /* Mask with this to get type */ | |
269 | #define EAF_TYPE_INT 0x01 /* 32-bit signed integer number */ | |
270 | #define EAF_TYPE_OPAQUE 0x02 /* Opaque byte string (not filterable) */ | |
271 | #define EAF_TYPE_IP_ADDRESS 0x04 /* IP address [FIXME: embed at least for IPv4?] */ | |
272 | #define EAF_TYPE_AS_PATH 0x06 /* BGP AS path [FIXME: define storage layout] */ | |
273 | #define EAF_TYPE_INT_SET 0x0a /* Set of integers (e.g., a community list) */ | |
274 | #define EAF_EMBEDDED 0x01 /* Data stored in eattr.u.data (part of type spec) */ | |
275 | #define EAF_VAR_LENGTH 0x02 /* Attribute length is variable */ | |
276 | #define EAF_INLINE 0x80 /* Copy of an attribute inlined in rte (temporary ea_lists only) */ | |
58ef912c MM |
277 | |
278 | struct adata { | |
279 | unsigned int length; | |
280 | byte data[0]; | |
281 | }; | |
282 | ||
283 | typedef struct ea_list { | |
284 | struct ea_list *next; /* In case we have an override list */ | |
b77ae37d | 285 | byte flags; /* Flags: EALF_... */ |
58ef912c | 286 | byte rfu; |
b77ae37d | 287 | word count; /* Number of attributes */ |
58ef912c MM |
288 | eattr attrs[0]; /* Attribute definitions themselves */ |
289 | } ea_list; | |
290 | ||
b77ae37d MM |
291 | #define EALF_SORTED 1 /* Attributes are sorted by code */ |
292 | #define EALF_BISECT 2 /* Use interval bisection for searching */ | |
293 | #define EALF_CACHED 4 /* Attributes belonging to cached rta */ | |
58ef912c | 294 | |
b77ae37d MM |
295 | eattr *ea_find(ea_list *, unsigned ea); |
296 | void ea_dump(ea_list *); | |
297 | void ea_sort(ea_list *); /* Sort entries in all sub-lists */ | |
298 | unsigned ea_scan(ea_list *); /* How many bytes do we need for merged ea_list (0=merge not needed) */ | |
299 | void ea_merge(ea_list *from, ea_list *to); /* Merge sub-lists to allocated buffer */ | |
58ef912c | 300 | |
2326b001 MM |
301 | void rta_init(void); |
302 | rta *rta_lookup(rta *); /* Get rta equivalent to this one, uc++ */ | |
303 | static inline rta *rta_clone(rta *r) { r->uc++; return r; } | |
b77ae37d MM |
304 | void rta__free(rta *r); |
305 | static inline void rta_free(rta *r) { if (r && !--r->uc) rta__free(r); } | |
2326b001 MM |
306 | void rta_dump(rta *); |
307 | void rta_dump_all(void); | |
2727bb7c | 308 | static inline eattr * rta_find(rta *a, unsigned ea) { return ea_find(a->eattrs, ea); } |
2326b001 | 309 | |
a8b60382 MM |
310 | /* |
311 | * Default protocol preferences | |
312 | */ | |
313 | ||
314 | #define DEF_PREF_DIRECT 240 /* Directly connected */ | |
315 | #define DEF_PREF_STATIC 200 /* Static route */ | |
316 | #define DEF_PREF_OSPF_INTERNAL 150 /* OSPF intra-area, inter-area and type 1 external routes */ | |
317 | #define DEF_PREF_RIP 120 /* RIP */ | |
318 | #define DEF_PREF_BGP 100 /* BGP */ | |
319 | #define DEF_PREF_OSPF_EXTERNAL 80 /* OSPF external routes */ | |
320 | #define DEF_PREF_RIP_EXTERNAL 70 /* RIP external routes */ | |
9c11ec9e | 321 | #define DEF_PREF_PIPE 60 /* Routes piped from other tables */ |
a8b60382 | 322 | |
58ef912c | 323 | #endif |