blob: 4f99d714ad0ba0d0631ccbdbeac47041c3200eb2 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Routing Information Base header
3 * Copyright (C) 1997 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#ifndef _ZEBRA_RIB_H
24#define _ZEBRA_RIB_H
25
paulec1a4282005-11-24 15:15:17 +000026#include "prefix.h"
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +000027#include "table.h"
paulec1a4282005-11-24 15:15:17 +000028
paul718e3742002-12-13 20:15:29 +000029#define DISTANCE_INFINITY 255
30
31/* Routing information base. */
Paul Jakma7514fb72007-05-02 16:05:35 +000032
33union g_addr {
34 struct in_addr ipv4;
35#ifdef HAVE_IPV6
36 struct in6_addr ipv6;
37#endif /* HAVE_IPV6 */
38};
39
paul718e3742002-12-13 20:15:29 +000040struct rib
41{
42 /* Link list. */
43 struct rib *next;
44 struct rib *prev;
Paul Jakmae6d7d052006-03-30 13:32:09 +000045
46 /* Nexthop structure */
47 struct nexthop *nexthop;
48
49 /* Refrence count. */
50 unsigned long refcnt;
51
52 /* Uptime. */
53 time_t uptime;
paul718e3742002-12-13 20:15:29 +000054
55 /* Type fo this route. */
56 int type;
57
58 /* Which routing table */
59 int table;
60
Paul Jakmae6d7d052006-03-30 13:32:09 +000061 /* Metric */
62 u_int32_t metric;
63
paul718e3742002-12-13 20:15:29 +000064 /* Distance. */
65 u_char distance;
66
Paul Jakma6d691122006-07-27 21:49:00 +000067 /* Flags of this route.
68 * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed
69 * to clients via Zserv
70 */
paul718e3742002-12-13 20:15:29 +000071 u_char flags;
72
Paul Jakma6d691122006-07-27 21:49:00 +000073 /* RIB internal status */
74 u_char status;
75#define RIB_ENTRY_REMOVED (1 << 0)
76
paul718e3742002-12-13 20:15:29 +000077 /* Nexthop information. */
78 u_char nexthop_num;
79 u_char nexthop_active_num;
80 u_char nexthop_fib_num;
paul718e3742002-12-13 20:15:29 +000081};
82
Denis Ovsienkoe96f9202008-06-02 12:03:22 +000083/* meta-queue structure:
84 * sub-queue 0: connected, kernel
85 * sub-queue 1: static
86 * sub-queue 2: RIP, RIPng, OSPF, OSPF6, IS-IS
87 * sub-queue 3: iBGP, eBGP
88 * sub-queue 4: any other origin (if any)
89 */
90#define MQ_SIZE 5
91struct meta_queue
92{
93 struct list *subq[MQ_SIZE];
94 u_int32_t size; /* sum of lengths of all subqueues */
95};
96
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +000097/*
98 * Structure that represents a single destination (prefix).
99 */
100typedef struct rib_dest_t_
101{
102
103 /*
104 * Back pointer to the route node for this destination. This helps
105 * us get to the prefix that this structure is for.
106 */
107 struct route_node *rnode;
108
109 /*
110 * Doubly-linked list of routes for this prefix.
111 */
112 struct rib *routes;
113
114 /*
115 * Flags, see below.
116 */
117 u_int32_t flags;
118
119} rib_dest_t;
120
121#define RIB_ROUTE_QUEUED(x) (1 << (x))
122
123/*
124 * The maximum qindex that can be used.
125 */
126#define ZEBRA_MAX_QINDEX (MQ_SIZE - 1)
127
128/*
129 * Macro to iterate over each route for a destination (prefix).
130 */
131#define RIB_DEST_FOREACH_ROUTE(dest, rib) \
132 for ((rib) = (dest) ? (dest)->routes : NULL; (rib); (rib) = (rib)->next)
133
134/*
135 * Same as above, but allows the current node to be unlinked.
136 */
137#define RIB_DEST_FOREACH_ROUTE_SAFE(dest, rib, next) \
138 for ((rib) = (dest) ? (dest)->routes : NULL; \
139 (rib) && ((next) = (rib)->next, 1); \
140 (rib) = (next))
141
142#define RNODE_FOREACH_RIB(rn, rib) \
143 RIB_DEST_FOREACH_ROUTE (rib_dest_from_rnode (rn), rib)
144
145#define RNODE_FOREACH_RIB_SAFE(rn, rib, next) \
146 RIB_DEST_FOREACH_ROUTE_SAFE (rib_dest_from_rnode (rn), rib, next)
147
paul718e3742002-12-13 20:15:29 +0000148/* Static route information. */
149struct static_ipv4
150{
151 /* For linked list. */
152 struct static_ipv4 *prev;
153 struct static_ipv4 *next;
154
155 /* Administrative distance. */
156 u_char distance;
157
158 /* Flag for this static route's type. */
159 u_char type;
160#define STATIC_IPV4_GATEWAY 1
161#define STATIC_IPV4_IFNAME 2
paul595db7f2003-05-25 21:35:06 +0000162#define STATIC_IPV4_BLACKHOLE 3
paul718e3742002-12-13 20:15:29 +0000163
164 /* Nexthop value. */
165 union
166 {
167 struct in_addr ipv4;
168 char *ifname;
169 } gate;
hasso81dfcaa2003-05-25 19:21:25 +0000170
171 /* bit flags */
172 u_char flags;
173/*
174 see ZEBRA_FLAG_REJECT
175 ZEBRA_FLAG_BLACKHOLE
176 */
paul718e3742002-12-13 20:15:29 +0000177};
178
179#ifdef HAVE_IPV6
180/* Static route information. */
181struct static_ipv6
182{
183 /* For linked list. */
184 struct static_ipv6 *prev;
185 struct static_ipv6 *next;
186
187 /* Administrative distance. */
188 u_char distance;
189
190 /* Flag for this static route's type. */
191 u_char type;
192#define STATIC_IPV6_GATEWAY 1
193#define STATIC_IPV6_GATEWAY_IFNAME 2
194#define STATIC_IPV6_IFNAME 3
195
196 /* Nexthop value. */
197 struct in6_addr ipv6;
198 char *ifname;
hasso81dfcaa2003-05-25 19:21:25 +0000199
200 /* bit flags */
201 u_char flags;
202/*
203 see ZEBRA_FLAG_REJECT
204 ZEBRA_FLAG_BLACKHOLE
205 */
paul718e3742002-12-13 20:15:29 +0000206};
207#endif /* HAVE_IPV6 */
208
paul7021c422003-07-15 12:52:22 +0000209enum nexthop_types_t
210{
211 NEXTHOP_TYPE_IFINDEX = 1, /* Directly connected. */
212 NEXTHOP_TYPE_IFNAME, /* Interface route. */
213 NEXTHOP_TYPE_IPV4, /* IPv4 nexthop. */
214 NEXTHOP_TYPE_IPV4_IFINDEX, /* IPv4 nexthop with ifindex. */
215 NEXTHOP_TYPE_IPV4_IFNAME, /* IPv4 nexthop with ifname. */
216 NEXTHOP_TYPE_IPV6, /* IPv6 nexthop. */
217 NEXTHOP_TYPE_IPV6_IFINDEX, /* IPv6 nexthop with ifindex. */
218 NEXTHOP_TYPE_IPV6_IFNAME, /* IPv6 nexthop with ifname. */
219 NEXTHOP_TYPE_BLACKHOLE, /* Null0 nexthop. */
220};
221
paul718e3742002-12-13 20:15:29 +0000222/* Nexthop structure. */
223struct nexthop
224{
225 struct nexthop *next;
226 struct nexthop *prev;
227
Paul Jakmae6d7d052006-03-30 13:32:09 +0000228 /* Interface index. */
229 char *ifname;
230 unsigned int ifindex;
231
paul7021c422003-07-15 12:52:22 +0000232 enum nexthop_types_t type;
paul718e3742002-12-13 20:15:29 +0000233
234 u_char flags;
235#define NEXTHOP_FLAG_ACTIVE (1 << 0) /* This nexthop is alive. */
236#define NEXTHOP_FLAG_FIB (1 << 1) /* FIB nexthop. */
237#define NEXTHOP_FLAG_RECURSIVE (1 << 2) /* Recursive nexthop. */
238
paul718e3742002-12-13 20:15:29 +0000239 /* Nexthop address or interface name. */
Paul Jakma7514fb72007-05-02 16:05:35 +0000240 union g_addr gate;
paul718e3742002-12-13 20:15:29 +0000241
242 /* Recursive lookup nexthop. */
243 u_char rtype;
244 unsigned int rifindex;
Paul Jakma7514fb72007-05-02 16:05:35 +0000245 union g_addr rgate;
246 union g_addr src;
paul718e3742002-12-13 20:15:29 +0000247};
248
249/* Routing table instance. */
250struct vrf
251{
252 /* Identifier. This is same as routing table vector index. */
253 u_int32_t id;
254
255 /* Routing table name. */
256 char *name;
257
258 /* Description. */
259 char *desc;
260
261 /* FIB identifier. */
262 u_char fib_id;
263
264 /* Routing table. */
265 struct route_table *table[AFI_MAX][SAFI_MAX];
266
267 /* Static route configuration. */
268 struct route_table *stable[AFI_MAX][SAFI_MAX];
269};
270
Avneesh Sachdev1b5ed1b2012-11-13 22:48:54 +0000271/*
272 * rib_table_info_t
273 *
274 * Structure that is hung off of a route_table that holds information about
275 * the table.
276 */
277typedef struct rib_table_info_t_
278{
279
280 /*
281 * Back pointer to vrf.
282 */
283 struct vrf *vrf;
284 afi_t afi;
285 safi_t safi;
286
287} rib_table_info_t;
288
Avneesh Sachdev0915bb02012-11-13 22:48:55 +0000289typedef enum
290{
291 RIB_TABLES_ITER_S_INIT,
292 RIB_TABLES_ITER_S_ITERATING,
293 RIB_TABLES_ITER_S_DONE
294} rib_tables_iter_state_t;
295
296/*
297 * Structure that holds state for iterating over all tables in the
298 * Routing Information Base.
299 */
300typedef struct rib_tables_iter_t_
301{
302 uint32_t vrf_id;
303 int afi_safi_ix;
304
305 rib_tables_iter_state_t state;
306} rib_tables_iter_t;
307
paula1ac18c2005-06-28 17:17:12 +0000308extern struct nexthop *nexthop_ifindex_add (struct rib *, unsigned int);
309extern struct nexthop *nexthop_ifname_add (struct rib *, char *);
310extern struct nexthop *nexthop_blackhole_add (struct rib *);
Paul Jakma7514fb72007-05-02 16:05:35 +0000311extern struct nexthop *nexthop_ipv4_add (struct rib *, struct in_addr *,
312 struct in_addr *);
Josh Bailey26e2ae32012-03-22 01:09:21 -0700313extern struct nexthop *nexthop_ipv4_ifindex_add (struct rib *,
314 struct in_addr *,
315 struct in_addr *,
316 unsigned int);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000317extern void rib_lookup_and_dump (struct prefix_ipv4 *);
Denis Ovsienko20e5ff02008-02-26 14:02:24 +0000318extern void rib_lookup_and_pushup (struct prefix_ipv4 *);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000319extern void rib_dump (const char *, const struct prefix_ipv4 *, const struct rib *);
320extern int rib_lookup_ipv4_route (struct prefix_ipv4 *, union sockunion *);
321#define ZEBRA_RIB_LOOKUP_ERROR -1
322#define ZEBRA_RIB_FOUND_EXACT 0
323#define ZEBRA_RIB_FOUND_NOGATE 1
324#define ZEBRA_RIB_FOUND_CONNECTED 2
325#define ZEBRA_RIB_NOTFOUND 3
326
paul718e3742002-12-13 20:15:29 +0000327#ifdef HAVE_IPV6
paula1ac18c2005-06-28 17:17:12 +0000328extern struct nexthop *nexthop_ipv6_add (struct rib *, struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000329#endif /* HAVE_IPV6 */
330
paula1ac18c2005-06-28 17:17:12 +0000331extern struct vrf *vrf_lookup (u_int32_t);
332extern struct route_table *vrf_table (afi_t afi, safi_t safi, u_int32_t id);
333extern struct route_table *vrf_static_table (afi_t afi, safi_t safi, u_int32_t id);
paul718e3742002-12-13 20:15:29 +0000334
hassod24af182005-09-24 14:00:26 +0000335/* NOTE:
336 * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
337 * also implicitly withdraw equal prefix of same type. */
paula1ac18c2005-06-28 17:17:12 +0000338extern int rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
Paul Jakma7514fb72007-05-02 16:05:35 +0000339 struct in_addr *gate, struct in_addr *src,
340 unsigned int ifindex, u_int32_t vrf_id,
G.Balajicddf3912011-11-26 21:59:32 +0400341 u_int32_t, u_char, safi_t);
paul718e3742002-12-13 20:15:29 +0000342
G.Balajicddf3912011-11-26 21:59:32 +0400343extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *, safi_t);
paul718e3742002-12-13 20:15:29 +0000344
paula1ac18c2005-06-28 17:17:12 +0000345extern int rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
346 struct in_addr *gate, unsigned int ifindex,
G.Balajicddf3912011-11-26 21:59:32 +0400347 u_int32_t, safi_t safi);
paul718e3742002-12-13 20:15:29 +0000348
paula1ac18c2005-06-28 17:17:12 +0000349extern struct rib *rib_match_ipv4 (struct in_addr);
paul718e3742002-12-13 20:15:29 +0000350
paula1ac18c2005-06-28 17:17:12 +0000351extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *);
paul718e3742002-12-13 20:15:29 +0000352
paula1ac18c2005-06-28 17:17:12 +0000353extern void rib_update (void);
354extern void rib_weed_tables (void);
355extern void rib_sweep_route (void);
356extern void rib_close (void);
357extern void rib_init (void);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400358extern unsigned long rib_score_proto (u_char proto);
paul718e3742002-12-13 20:15:29 +0000359
paula1ac18c2005-06-28 17:17:12 +0000360extern int
hasso39db97e2004-10-12 20:50:58 +0000361static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
hasso81dfcaa2003-05-25 19:21:25 +0000362 u_char flags, u_char distance, u_int32_t vrf_id);
paul718e3742002-12-13 20:15:29 +0000363
paula1ac18c2005-06-28 17:17:12 +0000364extern int
hasso39db97e2004-10-12 20:50:58 +0000365static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
paul718e3742002-12-13 20:15:29 +0000366 u_char distance, u_int32_t vrf_id);
367
368#ifdef HAVE_IPV6
paula1ac18c2005-06-28 17:17:12 +0000369extern int
paul718e3742002-12-13 20:15:29 +0000370rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
hassobe61c4e2005-08-27 06:05:47 +0000371 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
G.Balajif768f362011-11-26 22:10:39 +0400372 u_int32_t metric, u_char distance, safi_t safi);
paul718e3742002-12-13 20:15:29 +0000373
paula1ac18c2005-06-28 17:17:12 +0000374extern int
paul718e3742002-12-13 20:15:29 +0000375rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
G.Balajif768f362011-11-26 22:10:39 +0400376 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi);
paul718e3742002-12-13 20:15:29 +0000377
paula1ac18c2005-06-28 17:17:12 +0000378extern struct rib *rib_lookup_ipv6 (struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000379
paula1ac18c2005-06-28 17:17:12 +0000380extern struct rib *rib_match_ipv6 (struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000381
382extern struct route_table *rib_table_ipv6;
383
paula1ac18c2005-06-28 17:17:12 +0000384extern int
paul718e3742002-12-13 20:15:29 +0000385static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
hasso39db97e2004-10-12 20:50:58 +0000386 const char *ifname, u_char flags, u_char distance,
387 u_int32_t vrf_id);
paul718e3742002-12-13 20:15:29 +0000388
paula1ac18c2005-06-28 17:17:12 +0000389extern int
paul718e3742002-12-13 20:15:29 +0000390static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
hasso39db97e2004-10-12 20:50:58 +0000391 const char *ifname, u_char distance, u_int32_t vrf_id);
paul718e3742002-12-13 20:15:29 +0000392
393#endif /* HAVE_IPV6 */
394
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000395extern int rib_gc_dest (struct route_node *rn);
Avneesh Sachdev0915bb02012-11-13 22:48:55 +0000396extern struct route_table *rib_tables_iter_next (rib_tables_iter_t *iter);
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000397
398/*
399 * Inline functions.
400 */
401
402/*
Avneesh Sachdev1b5ed1b2012-11-13 22:48:54 +0000403 * rib_table_info
404 */
405static inline rib_table_info_t *
406rib_table_info (struct route_table *table)
407{
408 return (rib_table_info_t *) table->info;
409}
410
411/*
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000412 * rib_dest_from_rnode
413 */
414static inline rib_dest_t *
415rib_dest_from_rnode (struct route_node *rn)
416{
417 return (rib_dest_t *) rn->info;
418}
419
420/*
421 * rnode_to_ribs
422 *
423 * Returns a pointer to the list of routes corresponding to the given
424 * route_node.
425 */
426static inline struct rib *
427rnode_to_ribs (struct route_node *rn)
428{
429 rib_dest_t *dest;
430
431 dest = rib_dest_from_rnode (rn);
432 if (!dest)
433 return NULL;
434
435 return dest->routes;
436}
437
438/*
439 * rib_dest_prefix
440 */
441static inline struct prefix *
442rib_dest_prefix (rib_dest_t *dest)
443{
444 return &dest->rnode->p;
445}
446
447/*
448 * rib_dest_af
449 *
450 * Returns the address family that the destination is for.
451 */
452static inline u_char
453rib_dest_af (rib_dest_t *dest)
454{
455 return dest->rnode->p.family;
456}
457
458/*
459 * rib_dest_table
460 */
461static inline struct route_table *
462rib_dest_table (rib_dest_t *dest)
463{
464 return dest->rnode->table;
465}
466
Avneesh Sachdev1b5ed1b2012-11-13 22:48:54 +0000467/*
468 * rib_dest_vrf
469 */
470static inline struct vrf *
471rib_dest_vrf (rib_dest_t *dest)
472{
473 return rib_table_info (rib_dest_table (dest))->vrf;
474}
475
Avneesh Sachdev0915bb02012-11-13 22:48:55 +0000476/*
477 * rib_tables_iter_init
478 */
479static inline void
480rib_tables_iter_init (rib_tables_iter_t *iter)
481
482{
483 memset (iter, 0, sizeof (*iter));
484 iter->state = RIB_TABLES_ITER_S_INIT;
485}
486
487/*
488 * rib_tables_iter_started
489 *
490 * Returns TRUE if this iterator has started iterating over the set of
491 * tables.
492 */
493static inline int
494rib_tables_iter_started (rib_tables_iter_t *iter)
495{
496 return iter->state != RIB_TABLES_ITER_S_INIT;
497}
498
499/*
500 * rib_tables_iter_cleanup
501 */
502static inline void
503rib_tables_iter_cleanup (rib_tables_iter_t *iter)
504{
505 iter->state = RIB_TABLES_ITER_S_DONE;
506}
507
paul718e3742002-12-13 20:15:29 +0000508#endif /*_ZEBRA_RIB_H */