blob: 084f35166293f1ce73ca3867875823228dd966b5 [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
paula1ac18c2005-06-28 17:17:12 +0000271extern struct nexthop *nexthop_ifindex_add (struct rib *, unsigned int);
272extern struct nexthop *nexthop_ifname_add (struct rib *, char *);
273extern struct nexthop *nexthop_blackhole_add (struct rib *);
Paul Jakma7514fb72007-05-02 16:05:35 +0000274extern struct nexthop *nexthop_ipv4_add (struct rib *, struct in_addr *,
275 struct in_addr *);
Josh Bailey26e2ae32012-03-22 01:09:21 -0700276extern struct nexthop *nexthop_ipv4_ifindex_add (struct rib *,
277 struct in_addr *,
278 struct in_addr *,
279 unsigned int);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000280extern void rib_lookup_and_dump (struct prefix_ipv4 *);
Denis Ovsienko20e5ff02008-02-26 14:02:24 +0000281extern void rib_lookup_and_pushup (struct prefix_ipv4 *);
Denis Ovsienkodc958242007-08-13 16:03:06 +0000282extern void rib_dump (const char *, const struct prefix_ipv4 *, const struct rib *);
283extern int rib_lookup_ipv4_route (struct prefix_ipv4 *, union sockunion *);
284#define ZEBRA_RIB_LOOKUP_ERROR -1
285#define ZEBRA_RIB_FOUND_EXACT 0
286#define ZEBRA_RIB_FOUND_NOGATE 1
287#define ZEBRA_RIB_FOUND_CONNECTED 2
288#define ZEBRA_RIB_NOTFOUND 3
289
paul718e3742002-12-13 20:15:29 +0000290#ifdef HAVE_IPV6
paula1ac18c2005-06-28 17:17:12 +0000291extern struct nexthop *nexthop_ipv6_add (struct rib *, struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000292#endif /* HAVE_IPV6 */
293
paula1ac18c2005-06-28 17:17:12 +0000294extern struct vrf *vrf_lookup (u_int32_t);
295extern struct route_table *vrf_table (afi_t afi, safi_t safi, u_int32_t id);
296extern struct route_table *vrf_static_table (afi_t afi, safi_t safi, u_int32_t id);
paul718e3742002-12-13 20:15:29 +0000297
hassod24af182005-09-24 14:00:26 +0000298/* NOTE:
299 * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
300 * also implicitly withdraw equal prefix of same type. */
paula1ac18c2005-06-28 17:17:12 +0000301extern int rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
Paul Jakma7514fb72007-05-02 16:05:35 +0000302 struct in_addr *gate, struct in_addr *src,
303 unsigned int ifindex, u_int32_t vrf_id,
G.Balajicddf3912011-11-26 21:59:32 +0400304 u_int32_t, u_char, safi_t);
paul718e3742002-12-13 20:15:29 +0000305
G.Balajicddf3912011-11-26 21:59:32 +0400306extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *, safi_t);
paul718e3742002-12-13 20:15:29 +0000307
paula1ac18c2005-06-28 17:17:12 +0000308extern int rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
309 struct in_addr *gate, unsigned int ifindex,
G.Balajicddf3912011-11-26 21:59:32 +0400310 u_int32_t, safi_t safi);
paul718e3742002-12-13 20:15:29 +0000311
paula1ac18c2005-06-28 17:17:12 +0000312extern struct rib *rib_match_ipv4 (struct in_addr);
paul718e3742002-12-13 20:15:29 +0000313
paula1ac18c2005-06-28 17:17:12 +0000314extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *);
paul718e3742002-12-13 20:15:29 +0000315
paula1ac18c2005-06-28 17:17:12 +0000316extern void rib_update (void);
317extern void rib_weed_tables (void);
318extern void rib_sweep_route (void);
319extern void rib_close (void);
320extern void rib_init (void);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +0400321extern unsigned long rib_score_proto (u_char proto);
paul718e3742002-12-13 20:15:29 +0000322
paula1ac18c2005-06-28 17:17:12 +0000323extern int
hasso39db97e2004-10-12 20:50:58 +0000324static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
hasso81dfcaa2003-05-25 19:21:25 +0000325 u_char flags, u_char distance, u_int32_t vrf_id);
paul718e3742002-12-13 20:15:29 +0000326
paula1ac18c2005-06-28 17:17:12 +0000327extern int
hasso39db97e2004-10-12 20:50:58 +0000328static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname,
paul718e3742002-12-13 20:15:29 +0000329 u_char distance, u_int32_t vrf_id);
330
331#ifdef HAVE_IPV6
paula1ac18c2005-06-28 17:17:12 +0000332extern int
paul718e3742002-12-13 20:15:29 +0000333rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
hassobe61c4e2005-08-27 06:05:47 +0000334 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id,
G.Balajif768f362011-11-26 22:10:39 +0400335 u_int32_t metric, u_char distance, safi_t safi);
paul718e3742002-12-13 20:15:29 +0000336
paula1ac18c2005-06-28 17:17:12 +0000337extern int
paul718e3742002-12-13 20:15:29 +0000338rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
G.Balajif768f362011-11-26 22:10:39 +0400339 struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi);
paul718e3742002-12-13 20:15:29 +0000340
paula1ac18c2005-06-28 17:17:12 +0000341extern struct rib *rib_lookup_ipv6 (struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000342
paula1ac18c2005-06-28 17:17:12 +0000343extern struct rib *rib_match_ipv6 (struct in6_addr *);
paul718e3742002-12-13 20:15:29 +0000344
345extern struct route_table *rib_table_ipv6;
346
paula1ac18c2005-06-28 17:17:12 +0000347extern int
paul718e3742002-12-13 20:15:29 +0000348static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
hasso39db97e2004-10-12 20:50:58 +0000349 const char *ifname, u_char flags, u_char distance,
350 u_int32_t vrf_id);
paul718e3742002-12-13 20:15:29 +0000351
paula1ac18c2005-06-28 17:17:12 +0000352extern int
paul718e3742002-12-13 20:15:29 +0000353static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
hasso39db97e2004-10-12 20:50:58 +0000354 const char *ifname, u_char distance, u_int32_t vrf_id);
paul718e3742002-12-13 20:15:29 +0000355
356#endif /* HAVE_IPV6 */
357
Avneesh Sachdev9fd92e32012-11-13 22:48:53 +0000358extern int rib_gc_dest (struct route_node *rn);
359
360/*
361 * Inline functions.
362 */
363
364/*
365 * rib_dest_from_rnode
366 */
367static inline rib_dest_t *
368rib_dest_from_rnode (struct route_node *rn)
369{
370 return (rib_dest_t *) rn->info;
371}
372
373/*
374 * rnode_to_ribs
375 *
376 * Returns a pointer to the list of routes corresponding to the given
377 * route_node.
378 */
379static inline struct rib *
380rnode_to_ribs (struct route_node *rn)
381{
382 rib_dest_t *dest;
383
384 dest = rib_dest_from_rnode (rn);
385 if (!dest)
386 return NULL;
387
388 return dest->routes;
389}
390
391/*
392 * rib_dest_prefix
393 */
394static inline struct prefix *
395rib_dest_prefix (rib_dest_t *dest)
396{
397 return &dest->rnode->p;
398}
399
400/*
401 * rib_dest_af
402 *
403 * Returns the address family that the destination is for.
404 */
405static inline u_char
406rib_dest_af (rib_dest_t *dest)
407{
408 return dest->rnode->p.family;
409}
410
411/*
412 * rib_dest_table
413 */
414static inline struct route_table *
415rib_dest_table (rib_dest_t *dest)
416{
417 return dest->rnode->table;
418}
419
paul718e3742002-12-13 20:15:29 +0000420#endif /*_ZEBRA_RIB_H */