blob: 4ecfaa0d8ee4d7aa15af41b8acc23e5e0e9f7ac5 [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
Avneesh Sachdev78deec42012-11-13 22:48:56 +0000308extern const char *nexthop_type_to_str (enum nexthop_types_t nh_type);
paula1ac18c2005-06-28 17:17:12 +0000309extern struct nexthop *nexthop_ifindex_add (struct rib *, unsigned int);
310extern struct nexthop *nexthop_ifname_add (struct rib *, char *);
311extern struct nexthop *nexthop_blackhole_add (struct rib *);
Paul Jakma7514fb72007-05-02 16:05:35 +0000312extern struct nexthop *nexthop_ipv4_add (struct rib *, struct in_addr *,
313 struct in_addr *);
Josh Bailey26e2ae32012-03-22 01:09:21 -0700314extern struct nexthop *nexthop_ipv4_ifindex_add (struct rib *,
315 struct in_addr *,
316 struct in_addr *,
317 unsigned int);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000318extern void rib_lookup_and_dump (struct prefix_ipv4 *);
Denis Ovsienko20e5ff02008-02-26 14:02:24 +0000319extern void rib_lookup_and_pushup (struct prefix_ipv4 *);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000320extern void rib_dump (const char *, const struct prefix_ipv4 *, const struct rib *);
321extern int rib_lookup_ipv4_route (struct prefix_ipv4 *, union sockunion *);
322#define ZEBRA_RIB_LOOKUP_ERROR -1
323#define ZEBRA_RIB_FOUND_EXACT 0
324#define ZEBRA_RIB_FOUND_NOGATE 1
325#define ZEBRA_RIB_FOUND_CONNECTED 2
326#define ZEBRA_RIB_NOTFOUND 3
327
paul718e3742002-12-13 20:15:29 +0000328#ifdef HAVE_IPV6
paula1ac18c2005-06-28 17:17:12 +0000329extern struct nexthop *nexthop_ipv6_add (struct rib *, struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000330#endif /* HAVE_IPV6 */
331
paula1ac18c2005-06-28 17:17:12 +0000332extern struct vrf *vrf_lookup (u_int32_t);
333extern struct route_table *vrf_table (afi_t afi, safi_t safi, u_int32_t id);
334extern struct route_table *vrf_static_table (afi_t afi, safi_t safi, u_int32_t id);
paul718e3742002-12-13 20:15:29 +0000335
hassod24af182005-09-24 14:00:26 +0000336/* NOTE:
337 * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
338 * also implicitly withdraw equal prefix of same type. */
paula1ac18c2005-06-28 17:17:12 +0000339extern int rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
Paul Jakma7514fb72007-05-02 16:05:35 +0000340 struct in_addr *gate, struct in_addr *src,
341 unsigned int ifindex, u_int32_t vrf_id,
G.Balajicddf3912011-11-26 21:59:32 +0400342 u_int32_t, u_char, safi_t);
paul718e3742002-12-13 20:15:29 +0000343
G.Balajicddf3912011-11-26 21:59:32 +0400344extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *, safi_t);
paul718e3742002-12-13 20:15:29 +0000345
paula1ac18c2005-06-28 17:17:12 +0000346extern int rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
347 struct in_addr *gate, unsigned int ifindex,
G.Balajicddf3912011-11-26 21:59:32 +0400348 u_int32_t, safi_t safi);
paul718e3742002-12-13 20:15:29 +0000349
paula1ac18c2005-06-28 17:17:12 +0000350extern struct rib *rib_match_ipv4 (struct in_addr);
paul718e3742002-12-13 20:15:29 +0000351
paula1ac18c2005-06-28 17:17:12 +0000352extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *);
paul718e3742002-12-13 20:15:29 +0000353
paula1ac18c2005-06-28 17:17:12 +0000354extern void rib_update (void);
355extern void rib_weed_tables (void);
356extern void rib_sweep_route (void);
357extern void rib_close (void);
358extern void rib_init (void);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400359extern unsigned long rib_score_proto (u_char proto);
paul718e3742002-12-13 20:15:29 +0000360
paula1ac18c2005-06-28 17:17:12 +0000361extern int
hasso39db97e2004-10-12 20:50:58 +0000362static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
hasso81dfcaa2003-05-25 19:21:25 +0000363 u_char flags, u_char distance, u_int32_t vrf_id);
paul718e3742002-12-13 20:15:29 +0000364
paula1ac18c2005-06-28 17:17:12 +0000365extern int
hasso39db97e2004-10-12 20:50:58 +0000366static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
paul718e3742002-12-13 20:15:29 +0000367 u_char distance, u_int32_t vrf_id);
368
369#ifdef HAVE_IPV6
paula1ac18c2005-06-28 17:17:12 +0000370extern int
paul718e3742002-12-13 20:15:29 +0000371rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
hassobe61c4e2005-08-27 06:05:47 +0000372 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
G.Balajif768f362011-11-26 22:10:39 +0400373 u_int32_t metric, u_char distance, safi_t safi);
paul718e3742002-12-13 20:15:29 +0000374
paula1ac18c2005-06-28 17:17:12 +0000375extern int
paul718e3742002-12-13 20:15:29 +0000376rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
G.Balajif768f362011-11-26 22:10:39 +0400377 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi);
paul718e3742002-12-13 20:15:29 +0000378
paula1ac18c2005-06-28 17:17:12 +0000379extern struct rib *rib_lookup_ipv6 (struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000380
paula1ac18c2005-06-28 17:17:12 +0000381extern struct rib *rib_match_ipv6 (struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000382
383extern struct route_table *rib_table_ipv6;
384
paula1ac18c2005-06-28 17:17:12 +0000385extern int
paul718e3742002-12-13 20:15:29 +0000386static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
hasso39db97e2004-10-12 20:50:58 +0000387 const char *ifname, u_char flags, u_char distance,
388 u_int32_t vrf_id);
paul718e3742002-12-13 20:15:29 +0000389
paula1ac18c2005-06-28 17:17:12 +0000390extern int
paul718e3742002-12-13 20:15:29 +0000391static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
hasso39db97e2004-10-12 20:50:58 +0000392 const char *ifname, u_char distance, u_int32_t vrf_id);
paul718e3742002-12-13 20:15:29 +0000393
394#endif /* HAVE_IPV6 */
395
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000396extern int rib_gc_dest (struct route_node *rn);
Avneesh Sachdev0915bb02012-11-13 22:48:55 +0000397extern struct route_table *rib_tables_iter_next (rib_tables_iter_t *iter);
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000398
399/*
400 * Inline functions.
401 */
402
403/*
Avneesh Sachdev1b5ed1b2012-11-13 22:48:54 +0000404 * rib_table_info
405 */
406static inline rib_table_info_t *
407rib_table_info (struct route_table *table)
408{
409 return (rib_table_info_t *) table->info;
410}
411
412/*
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000413 * rib_dest_from_rnode
414 */
415static inline rib_dest_t *
416rib_dest_from_rnode (struct route_node *rn)
417{
418 return (rib_dest_t *) rn->info;
419}
420
421/*
422 * rnode_to_ribs
423 *
424 * Returns a pointer to the list of routes corresponding to the given
425 * route_node.
426 */
427static inline struct rib *
428rnode_to_ribs (struct route_node *rn)
429{
430 rib_dest_t *dest;
431
432 dest = rib_dest_from_rnode (rn);
433 if (!dest)
434 return NULL;
435
436 return dest->routes;
437}
438
439/*
440 * rib_dest_prefix
441 */
442static inline struct prefix *
443rib_dest_prefix (rib_dest_t *dest)
444{
445 return &dest->rnode->p;
446}
447
448/*
449 * rib_dest_af
450 *
451 * Returns the address family that the destination is for.
452 */
453static inline u_char
454rib_dest_af (rib_dest_t *dest)
455{
456 return dest->rnode->p.family;
457}
458
459/*
460 * rib_dest_table
461 */
462static inline struct route_table *
463rib_dest_table (rib_dest_t *dest)
464{
465 return dest->rnode->table;
466}
467
Avneesh Sachdev1b5ed1b2012-11-13 22:48:54 +0000468/*
469 * rib_dest_vrf
470 */
471static inline struct vrf *
472rib_dest_vrf (rib_dest_t *dest)
473{
474 return rib_table_info (rib_dest_table (dest))->vrf;
475}
476
Avneesh Sachdev0915bb02012-11-13 22:48:55 +0000477/*
478 * rib_tables_iter_init
479 */
480static inline void
481rib_tables_iter_init (rib_tables_iter_t *iter)
482
483{
484 memset (iter, 0, sizeof (*iter));
485 iter->state = RIB_TABLES_ITER_S_INIT;
486}
487
488/*
489 * rib_tables_iter_started
490 *
491 * Returns TRUE if this iterator has started iterating over the set of
492 * tables.
493 */
494static inline int
495rib_tables_iter_started (rib_tables_iter_t *iter)
496{
497 return iter->state != RIB_TABLES_ITER_S_INIT;
498}
499
500/*
501 * rib_tables_iter_cleanup
502 */
503static inline void
504rib_tables_iter_cleanup (rib_tables_iter_t *iter)
505{
506 iter->state = RIB_TABLES_ITER_S_DONE;
507}
508
paul718e3742002-12-13 20:15:29 +0000509#endif /*_ZEBRA_RIB_H */