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