blob: c98d99a4d008a1933402a8c80eaa5079694bc381 [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
paula1ac18c2005-06-28 17:17:12 +0000289extern struct nexthop *nexthop_ifindex_add (struct rib *, unsigned int);
290extern struct nexthop *nexthop_ifname_add (struct rib *, char *);
291extern struct nexthop *nexthop_blackhole_add (struct rib *);
Paul Jakma7514fb72007-05-02 16:05:35 +0000292extern struct nexthop *nexthop_ipv4_add (struct rib *, struct in_addr *,
293 struct in_addr *);
Josh Bailey26e2ae32012-03-22 01:09:21 -0700294extern struct nexthop *nexthop_ipv4_ifindex_add (struct rib *,
295 struct in_addr *,
296 struct in_addr *,
297 unsigned int);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000298extern void rib_lookup_and_dump (struct prefix_ipv4 *);
Denis Ovsienko20e5ff02008-02-26 14:02:24 +0000299extern void rib_lookup_and_pushup (struct prefix_ipv4 *);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000300extern void rib_dump (const char *, const struct prefix_ipv4 *, const struct rib *);
301extern int rib_lookup_ipv4_route (struct prefix_ipv4 *, union sockunion *);
302#define ZEBRA_RIB_LOOKUP_ERROR -1
303#define ZEBRA_RIB_FOUND_EXACT 0
304#define ZEBRA_RIB_FOUND_NOGATE 1
305#define ZEBRA_RIB_FOUND_CONNECTED 2
306#define ZEBRA_RIB_NOTFOUND 3
307
paul718e3742002-12-13 20:15:29 +0000308#ifdef HAVE_IPV6
paula1ac18c2005-06-28 17:17:12 +0000309extern struct nexthop *nexthop_ipv6_add (struct rib *, struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000310#endif /* HAVE_IPV6 */
311
paula1ac18c2005-06-28 17:17:12 +0000312extern struct vrf *vrf_lookup (u_int32_t);
313extern struct route_table *vrf_table (afi_t afi, safi_t safi, u_int32_t id);
314extern struct route_table *vrf_static_table (afi_t afi, safi_t safi, u_int32_t id);
paul718e3742002-12-13 20:15:29 +0000315
hassod24af182005-09-24 14:00:26 +0000316/* NOTE:
317 * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
318 * also implicitly withdraw equal prefix of same type. */
paula1ac18c2005-06-28 17:17:12 +0000319extern int rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
Paul Jakma7514fb72007-05-02 16:05:35 +0000320 struct in_addr *gate, struct in_addr *src,
321 unsigned int ifindex, u_int32_t vrf_id,
G.Balajicddf3912011-11-26 21:59:32 +0400322 u_int32_t, u_char, safi_t);
paul718e3742002-12-13 20:15:29 +0000323
G.Balajicddf3912011-11-26 21:59:32 +0400324extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *, safi_t);
paul718e3742002-12-13 20:15:29 +0000325
paula1ac18c2005-06-28 17:17:12 +0000326extern int rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
327 struct in_addr *gate, unsigned int ifindex,
G.Balajicddf3912011-11-26 21:59:32 +0400328 u_int32_t, safi_t safi);
paul718e3742002-12-13 20:15:29 +0000329
paula1ac18c2005-06-28 17:17:12 +0000330extern struct rib *rib_match_ipv4 (struct in_addr);
paul718e3742002-12-13 20:15:29 +0000331
paula1ac18c2005-06-28 17:17:12 +0000332extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *);
paul718e3742002-12-13 20:15:29 +0000333
paula1ac18c2005-06-28 17:17:12 +0000334extern void rib_update (void);
335extern void rib_weed_tables (void);
336extern void rib_sweep_route (void);
337extern void rib_close (void);
338extern void rib_init (void);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400339extern unsigned long rib_score_proto (u_char proto);
paul718e3742002-12-13 20:15:29 +0000340
paula1ac18c2005-06-28 17:17:12 +0000341extern int
hasso39db97e2004-10-12 20:50:58 +0000342static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
hasso81dfcaa2003-05-25 19:21:25 +0000343 u_char flags, u_char distance, u_int32_t vrf_id);
paul718e3742002-12-13 20:15:29 +0000344
paula1ac18c2005-06-28 17:17:12 +0000345extern int
hasso39db97e2004-10-12 20:50:58 +0000346static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
paul718e3742002-12-13 20:15:29 +0000347 u_char distance, u_int32_t vrf_id);
348
349#ifdef HAVE_IPV6
paula1ac18c2005-06-28 17:17:12 +0000350extern int
paul718e3742002-12-13 20:15:29 +0000351rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
hassobe61c4e2005-08-27 06:05:47 +0000352 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
G.Balajif768f362011-11-26 22:10:39 +0400353 u_int32_t metric, u_char distance, safi_t safi);
paul718e3742002-12-13 20:15:29 +0000354
paula1ac18c2005-06-28 17:17:12 +0000355extern int
paul718e3742002-12-13 20:15:29 +0000356rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
G.Balajif768f362011-11-26 22:10:39 +0400357 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi);
paul718e3742002-12-13 20:15:29 +0000358
paula1ac18c2005-06-28 17:17:12 +0000359extern struct rib *rib_lookup_ipv6 (struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000360
paula1ac18c2005-06-28 17:17:12 +0000361extern struct rib *rib_match_ipv6 (struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000362
363extern struct route_table *rib_table_ipv6;
364
paula1ac18c2005-06-28 17:17:12 +0000365extern int
paul718e3742002-12-13 20:15:29 +0000366static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
hasso39db97e2004-10-12 20:50:58 +0000367 const char *ifname, u_char flags, u_char distance,
368 u_int32_t vrf_id);
paul718e3742002-12-13 20:15:29 +0000369
paula1ac18c2005-06-28 17:17:12 +0000370extern int
paul718e3742002-12-13 20:15:29 +0000371static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
hasso39db97e2004-10-12 20:50:58 +0000372 const char *ifname, u_char distance, u_int32_t vrf_id);
paul718e3742002-12-13 20:15:29 +0000373
374#endif /* HAVE_IPV6 */
375
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000376extern int rib_gc_dest (struct route_node *rn);
377
378/*
379 * Inline functions.
380 */
381
382/*
Avneesh Sachdev1b5ed1b2012-11-13 22:48:54 +0000383 * rib_table_info
384 */
385static inline rib_table_info_t *
386rib_table_info (struct route_table *table)
387{
388 return (rib_table_info_t *) table->info;
389}
390
391/*
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000392 * rib_dest_from_rnode
393 */
394static inline rib_dest_t *
395rib_dest_from_rnode (struct route_node *rn)
396{
397 return (rib_dest_t *) rn->info;
398}
399
400/*
401 * rnode_to_ribs
402 *
403 * Returns a pointer to the list of routes corresponding to the given
404 * route_node.
405 */
406static inline struct rib *
407rnode_to_ribs (struct route_node *rn)
408{
409 rib_dest_t *dest;
410
411 dest = rib_dest_from_rnode (rn);
412 if (!dest)
413 return NULL;
414
415 return dest->routes;
416}
417
418/*
419 * rib_dest_prefix
420 */
421static inline struct prefix *
422rib_dest_prefix (rib_dest_t *dest)
423{
424 return &dest->rnode->p;
425}
426
427/*
428 * rib_dest_af
429 *
430 * Returns the address family that the destination is for.
431 */
432static inline u_char
433rib_dest_af (rib_dest_t *dest)
434{
435 return dest->rnode->p.family;
436}
437
438/*
439 * rib_dest_table
440 */
441static inline struct route_table *
442rib_dest_table (rib_dest_t *dest)
443{
444 return dest->rnode->table;
445}
446
Avneesh Sachdev1b5ed1b2012-11-13 22:48:54 +0000447/*
448 * rib_dest_vrf
449 */
450static inline struct vrf *
451rib_dest_vrf (rib_dest_t *dest)
452{
453 return rib_table_info (rib_dest_table (dest))->vrf;
454}
455
paul718e3742002-12-13 20:15:29 +0000456#endif /*_ZEBRA_RIB_H */