]> git.proxmox.com Git - mirror_frr.git/blame - zebra/rib.h
zebra: remove recursively derived static routes correctly
[mirror_frr.git] / zebra / rib.h
CommitLineData
718e3744 1/*
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
c6ffe645 26#include "linklist.h"
ec1a4283 27#include "prefix.h"
9fd92e3c 28#include "table.h"
5adc2528 29#include "queue.h"
fb018d25 30#include "nexthop.h"
244a59d2 31#include "vrf.h"
fb148af4 32#include "if.h"
ec1a4283 33
718e3744 34#define DISTANCE_INFINITY 255
7a4bb9c5 35#define ZEBRA_KERNEL_TABLE_MAX 252 /* support for no more than this rt tables */
718e3744 36
718e3744 37struct rib
38{
39 /* Link list. */
40 struct rib *next;
41 struct rib *prev;
e6d7d054
PJ
42
43 /* Nexthop structure */
44 struct nexthop *nexthop;
45
46 /* Refrence count. */
47 unsigned long refcnt;
48
49 /* Uptime. */
50 time_t uptime;
718e3744 51
52 /* Type fo this route. */
53 int type;
54
7c8ff89e
DS
55 /* Source protocol instance */
56 u_short instance;
57
78104b9b
FL
58 /* VRF identifier. */
59 vrf_id_t vrf_id;
60
718e3744 61 /* Which routing table */
4e3afb14 62 uint32_t table;
718e3744 63
e6d7d054
PJ
64 /* Metric */
65 u_int32_t metric;
66
718e3744 67 /* Distance. */
68 u_char distance;
69
0d9551dc
DS
70 /* Tag */
71 u_short tag;
72
6d691129
PJ
73 /* Flags of this route.
74 * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed
75 * to clients via Zserv
76 */
718e3744 77 u_char flags;
78
6d691129
PJ
79 /* RIB internal status */
80 u_char status;
6e26278c
DS
81#define RIB_ENTRY_REMOVED 0x1
82 /* to simplify NHT logic when NHs change, instead of doing a NH by NH cmp */
83#define RIB_ENTRY_NEXTHOPS_CHANGED 0x2
6d691129 84
718e3744 85 /* Nexthop information. */
86 u_char nexthop_num;
87 u_char nexthop_active_num;
88 u_char nexthop_fib_num;
718e3744 89};
90
e96f9203
DO
91/* meta-queue structure:
92 * sub-queue 0: connected, kernel
93 * sub-queue 1: static
94 * sub-queue 2: RIP, RIPng, OSPF, OSPF6, IS-IS
95 * sub-queue 3: iBGP, eBGP
96 * sub-queue 4: any other origin (if any)
97 */
98#define MQ_SIZE 5
99struct meta_queue
100{
101 struct list *subq[MQ_SIZE];
102 u_int32_t size; /* sum of lengths of all subqueues */
103};
104
9fd92e3c
AS
105/*
106 * Structure that represents a single destination (prefix).
107 */
108typedef struct rib_dest_t_
109{
110
111 /*
112 * Back pointer to the route node for this destination. This helps
113 * us get to the prefix that this structure is for.
114 */
115 struct route_node *rnode;
116
117 /*
118 * Doubly-linked list of routes for this prefix.
119 */
120 struct rib *routes;
121
122 /*
123 * Flags, see below.
124 */
125 u_int32_t flags;
126
5adc2528
AS
127 /*
128 * Linkage to put dest on the FPM processing queue.
129 */
130 TAILQ_ENTRY(rib_dest_t_) fpm_q_entries;
131
9fd92e3c
AS
132} rib_dest_t;
133
134#define RIB_ROUTE_QUEUED(x) (1 << (x))
135
136/*
137 * The maximum qindex that can be used.
138 */
139#define ZEBRA_MAX_QINDEX (MQ_SIZE - 1)
140
5adc2528
AS
141/*
142 * This flag indicates that a given prefix has been 'advertised' to
143 * the FPM to be installed in the forwarding plane.
144 */
145#define RIB_DEST_SENT_TO_FPM (1 << (ZEBRA_MAX_QINDEX + 1))
146
147/*
148 * This flag is set when we need to send an update to the FPM about a
149 * dest.
150 */
151#define RIB_DEST_UPDATE_FPM (1 << (ZEBRA_MAX_QINDEX + 2))
152
9fd92e3c
AS
153/*
154 * Macro to iterate over each route for a destination (prefix).
155 */
156#define RIB_DEST_FOREACH_ROUTE(dest, rib) \
157 for ((rib) = (dest) ? (dest)->routes : NULL; (rib); (rib) = (rib)->next)
158
159/*
160 * Same as above, but allows the current node to be unlinked.
161 */
162#define RIB_DEST_FOREACH_ROUTE_SAFE(dest, rib, next) \
163 for ((rib) = (dest) ? (dest)->routes : NULL; \
164 (rib) && ((next) = (rib)->next, 1); \
165 (rib) = (next))
166
167#define RNODE_FOREACH_RIB(rn, rib) \
168 RIB_DEST_FOREACH_ROUTE (rib_dest_from_rnode (rn), rib)
169
170#define RNODE_FOREACH_RIB_SAFE(rn, rib, next) \
171 RIB_DEST_FOREACH_ROUTE_SAFE (rib_dest_from_rnode (rn), rib, next)
172
718e3744 173/* Static route information. */
c0551cbb 174struct static_route
718e3744 175{
176 /* For linked list. */
c0551cbb
DS
177 struct static_route *prev;
178 struct static_route *next;
718e3744 179
8f527c5e
FL
180 /* VRF identifier. */
181 vrf_id_t vrf_id;
182
718e3744 183 /* Administrative distance. */
184 u_char distance;
185
0d9551dc
DS
186 /* Tag */
187 u_short tag;
188
718e3744 189 /* Flag for this static route's type. */
190 u_char type;
ba779241
DS
191#define STATIC_IFINDEX 1
192#define STATIC_IPV4_GATEWAY 2
61a091c5 193#define STATIC_BLACKHOLE 3
c0551cbb 194#define STATIC_IPV6_GATEWAY 4
ba779241 195#define STATIC_IPV6_GATEWAY_IFINDEX 5
718e3744 196
c0551cbb 197 /*
be5e48ab 198 * Nexthop value.
c0551cbb 199 *
ba779241 200 * Under IPv4 addr and ifindex are
c0551cbb
DS
201 * used independentyly.
202 * STATIC_IPV4_GATEWAY uses addr
ba779241 203 * STATIC_IFINDEX uses ifindex
c0551cbb
DS
204 */
205 union g_addr addr;
ba779241 206 unsigned int ifindex;
81dfcaa2 207
fb148af4
DS
208 char ifname[INTERFACE_NAMSIZ + 1];
209
81dfcaa2 210 /* bit flags */
211 u_char flags;
212/*
213 see ZEBRA_FLAG_REJECT
214 ZEBRA_FLAG_BLACKHOLE
215 */
718e3744 216};
718e3744 217
718e3744 218
fa713d9e
CF
219/* The following for loop allows to iterate over the nexthop
220 * structure of routes.
221 *
222 * We have to maintain quite a bit of state:
223 *
224 * nexthop: The pointer to the current nexthop, either in the
225 * top-level chain or in the resolved chain of ni.
226 * tnexthop: The pointer to the current nexthop in the top-level
227 * nexthop chain.
228 * recursing: Information if nh currently is in the top-level chain
229 * (0) or in a resolved chain (1).
230 *
231 * Initialization: Set `nexthop' and `tnexthop' to the head of the
232 * top-level chain. As nexthop is in the top level chain, set recursing
233 * to 0.
234 *
235 * Iteration check: Check that the `nexthop' pointer is not NULL.
236 *
237 * Iteration step: This is the tricky part. Check if `nexthop' has
238 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' is in
239 * the top level chain and has at least one nexthop attached to
240 * `nexthop->resolved'. As we want to descend into `nexthop->resolved',
241 * set `recursing' to 1 and set `nexthop' to `nexthop->resolved'.
242 * `tnexthop' is left alone in that case so we can remember which nexthop
243 * in the top level chain we are currently handling.
244 *
245 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
246 * current chain. If we are recursing, `nexthop' will be set to
247 * `nexthop->next' and `tnexthop' will be left alone. If we are not
248 * recursing, both `tnexthop' and `nexthop' will be set to `nexthop->next'
249 * as we are progressing in the top level chain.
250 * If we encounter `nexthop->next == NULL', we will clear the `recursing'
251 * flag as we arived either at the end of the resolved chain or at the end
252 * of the top level chain. In both cases, we set `tnexthop' and `nexthop'
253 * to `tnexthop->next', progressing to the next position in the top-level
254 * chain and possibly to its end marked by NULL.
255 */
256#define ALL_NEXTHOPS_RO(head, nexthop, tnexthop, recursing) \
257 (tnexthop) = (nexthop) = (head), (recursing) = 0; \
258 (nexthop); \
259 (nexthop) = CHECK_FLAG((nexthop)->flags, NEXTHOP_FLAG_RECURSIVE) \
260 ? (((recursing) = 1), (nexthop)->resolved) \
261 : ((nexthop)->next ? ((recursing) ? (nexthop)->next \
262 : ((tnexthop) = (nexthop)->next)) \
263 : (((recursing) = 0),((tnexthop) = (tnexthop)->next)))
264
8da4e946 265#if defined (HAVE_RTADV)
cd80d74f
FL
266/* Structure which hold status of router advertisement. */
267struct rtadv
268{
269 int sock;
270
271 int adv_if_count;
272 int adv_msec_if_count;
273
274 struct thread *ra_read;
275 struct thread *ra_timer;
276};
8da4e946 277#endif /* HAVE_RTADV */
cd80d74f 278
1b5ed1b0
AS
279/*
280 * rib_table_info_t
281 *
282 * Structure that is hung off of a route_table that holds information about
283 * the table.
284 */
285typedef struct rib_table_info_t_
286{
287
288 /*
b72ede27 289 * Back pointer to zebra_vrf.
1b5ed1b0 290 */
b72ede27 291 struct zebra_vrf *zvrf;
1b5ed1b0
AS
292 afi_t afi;
293 safi_t safi;
294
295} rib_table_info_t;
296
0915bb0c
AS
297typedef enum
298{
299 RIB_TABLES_ITER_S_INIT,
300 RIB_TABLES_ITER_S_ITERATING,
301 RIB_TABLES_ITER_S_DONE
302} rib_tables_iter_state_t;
303
304/*
305 * Structure that holds state for iterating over all tables in the
306 * Routing Information Base.
307 */
308typedef struct rib_tables_iter_t_
309{
b72ede27 310 vrf_id_t vrf_id;
0915bb0c
AS
311 int afi_safi_ix;
312
313 rib_tables_iter_state_t state;
314} rib_tables_iter_t;
315
1c848137 316/* Events/reasons triggering a RIB update. */
317typedef enum
318{
319 RIB_UPDATE_IF_CHANGE,
320 RIB_UPDATE_RMAP_CHANGE,
321 RIB_UPDATE_OTHER
322} rib_update_event_t;
323
a399694f 324extern struct nexthop *rib_nexthop_ifindex_add (struct rib *, unsigned int);
a399694f
DS
325extern struct nexthop *rib_nexthop_blackhole_add (struct rib *);
326extern struct nexthop *rib_nexthop_ipv4_add (struct rib *, struct in_addr *,
327 struct in_addr *);
328extern struct nexthop *rib_nexthop_ipv4_ifindex_add (struct rib *,
329 struct in_addr *,
330 struct in_addr *,
331 unsigned int);
332extern void rib_nexthop_add (struct rib *rib, struct nexthop *nexthop);
333extern void rib_copy_nexthops (struct rib *rib, struct nexthop *nh);
fb018d25 334
4623d897
DL
335/* RPF lookup behaviour */
336enum multicast_mode
337{
338 MCAST_NO_CONFIG = 0, /* MIX_MRIB_FIRST, but no show in config write */
339 MCAST_MRIB_ONLY, /* MRIB only */
340 MCAST_URIB_ONLY, /* URIB only */
341 MCAST_MIX_MRIB_FIRST, /* MRIB, if nothing at all then URIB */
342 MCAST_MIX_DISTANCE, /* MRIB & URIB, lower distance wins */
343 MCAST_MIX_PFXLEN, /* MRIB & URIB, longer prefix wins */
344 /* on equal value, MRIB wins for last 2 */
345};
346
347extern void multicast_mode_ipv4_set (enum multicast_mode mode);
348extern enum multicast_mode multicast_mode_ipv4_get (void);
349
fa713d9e 350extern int nexthop_has_fib_child(struct nexthop *);
12f6fb97
DS
351extern void rib_lookup_and_dump (struct prefix_ipv4 *, vrf_id_t);
352extern void rib_lookup_and_pushup (struct prefix_ipv4 *, vrf_id_t);
f7bf4153
DL
353#define rib_dump(prefix ,rib) _rib_dump(__func__, prefix, rib)
354extern void _rib_dump (const char *,
355 union prefix46constptr, const struct rib *);
78104b9b
FL
356extern int rib_lookup_ipv4_route (struct prefix_ipv4 *, union sockunion *,
357 vrf_id_t);
dc95824a
DO
358#define ZEBRA_RIB_LOOKUP_ERROR -1
359#define ZEBRA_RIB_FOUND_EXACT 0
360#define ZEBRA_RIB_FOUND_NOGATE 1
361#define ZEBRA_RIB_FOUND_CONNECTED 2
362#define ZEBRA_RIB_NOTFOUND 3
363
a399694f
DS
364extern struct nexthop *rib_nexthop_ipv6_add (struct rib *, struct in6_addr *);
365extern struct nexthop *rib_nexthop_ipv6_ifindex_add (struct rib *rib,
366 struct in6_addr *ipv6,
367 unsigned int ifindex);
718e3744 368
7a4bb9c5
DS
369extern int is_zebra_valid_kernel_table(u_int32_t table_id);
370extern int is_zebra_main_routing_table(u_int32_t table_id);
0aabccc0 371extern int zebra_check_addr (struct prefix *p);
718e3744 372
d24af186 373/* NOTE:
374 * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
375 * also implicitly withdraw equal prefix of same type. */
7c8ff89e 376extern int rib_add_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p,
7514fb77 377 struct in_addr *gate, struct in_addr *src,
78104b9b 378 unsigned int ifindex, vrf_id_t vrf_id, u_int32_t table_id,
cddf391b 379 u_int32_t, u_char, safi_t);
718e3744 380
cddf391b 381extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *, safi_t);
718e3744 382
7c8ff89e 383extern int rib_delete_ipv4 (int type, u_short instance, int flags, struct prefix_ipv4 *p,
a1ac18c4 384 struct in_addr *gate, unsigned int ifindex,
78104b9b 385 vrf_id_t, u_int32_t, safi_t safi);
718e3744 386
33550aa8
DL
387extern struct rib *rib_match_ipv4 (struct in_addr, safi_t safi, vrf_id_t,
388 struct route_node **rn_out);
4623d897
DL
389extern struct rib *rib_match_ipv4_multicast (struct in_addr addr,
390 struct route_node **rn_out);
718e3744 391
78104b9b 392extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *, vrf_id_t);
718e3744 393
1c848137 394extern void rib_update (vrf_id_t, rib_update_event_t);
a1ac18c4 395extern void rib_weed_tables (void);
396extern void rib_sweep_route (void);
a31c5886 397extern void rib_close_table (struct route_table *);
a1ac18c4 398extern void rib_close (void);
399extern void rib_init (void);
7c8ff89e 400extern unsigned long rib_score_proto (u_char proto, u_short instance);
44e9909d 401extern void rib_queue_add (struct route_node *rn);
6e26278c 402
fb148af4
DS
403extern void
404static_install_route (afi_t afi, safi_t safi, struct prefix *p, struct static_route *si);
405extern void
406static_uninstall_route (afi_t afi, safi_t safi, struct prefix *p, struct static_route *si);
718e3744 407
a1ac18c4 408extern int
b78a80d7 409static_add_ipv4 (safi_t safi, struct prefix *p, struct in_addr *gate, unsigned int ifindex,
a3d21ef3
DS
410 const char *ifname, u_char flags, u_short tag,
411 u_char distance, struct zebra_vrf *zvrf);
718e3744 412
a1ac18c4 413extern int
b78a80d7 414static_delete_ipv4 (safi_t safi, struct prefix *p, struct in_addr *gate, unsigned int ifindex,
b9f1114e 415 u_short tag, u_char distance, struct zebra_vrf *zvrf);
718e3744 416
a1ac18c4 417extern int
7c8ff89e 418rib_add_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
78104b9b
FL
419 struct in6_addr *gate, unsigned int ifindex, vrf_id_t vrf_id,
420 u_int32_t table_id, u_int32_t metric, u_char distance, safi_t safi);
718e3744 421
a1ac18c4 422extern int
7c8ff89e 423rib_delete_ipv6 (int type, u_short instance, int flags, struct prefix_ipv6 *p,
78104b9b
FL
424 struct in6_addr *gate, unsigned int ifindex, vrf_id_t vrf_id,
425 u_int32_t table_id, safi_t safi);
718e3744 426
78104b9b 427extern struct rib *rib_lookup_ipv6 (struct in6_addr *, vrf_id_t);
718e3744 428
78104b9b 429extern struct rib *rib_match_ipv6 (struct in6_addr *, vrf_id_t);
718e3744 430
431extern struct route_table *rib_table_ipv6;
432
a1ac18c4 433extern int
718e3744 434static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
a3d21ef3
DS
435 unsigned int ifindex, const char *ifname, u_char flags,
436 u_short tag, u_char distance, struct zebra_vrf *zvrf);
718e3744 437
41fc2714 438extern int
8a92a8a0 439rib_add_ipv6_multipath (struct prefix *, struct rib *, safi_t,
41fc2714
DS
440 unsigned long);
441
a1ac18c4 442extern int
718e3744 443static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
ba779241 444 unsigned int ifindex, u_short tag, u_char distance,
b9f1114e 445 struct zebra_vrf *zvrf);
718e3744 446
9fd92e3c 447extern int rib_gc_dest (struct route_node *rn);
0915bb0c 448extern struct route_table *rib_tables_iter_next (rib_tables_iter_t *iter);
9fd92e3c
AS
449
450/*
451 * Inline functions.
452 */
453
1b5ed1b0
AS
454/*
455 * rib_table_info
456 */
457static inline rib_table_info_t *
458rib_table_info (struct route_table *table)
459{
460 return (rib_table_info_t *) table->info;
461}
462
9fd92e3c
AS
463/*
464 * rib_dest_from_rnode
465 */
466static inline rib_dest_t *
467rib_dest_from_rnode (struct route_node *rn)
468{
469 return (rib_dest_t *) rn->info;
470}
471
472/*
473 * rnode_to_ribs
474 *
475 * Returns a pointer to the list of routes corresponding to the given
476 * route_node.
477 */
478static inline struct rib *
479rnode_to_ribs (struct route_node *rn)
480{
481 rib_dest_t *dest;
482
483 dest = rib_dest_from_rnode (rn);
484 if (!dest)
485 return NULL;
486
487 return dest->routes;
488}
489
490/*
491 * rib_dest_prefix
492 */
493static inline struct prefix *
494rib_dest_prefix (rib_dest_t *dest)
495{
496 return &dest->rnode->p;
497}
498
499/*
500 * rib_dest_af
501 *
502 * Returns the address family that the destination is for.
503 */
504static inline u_char
505rib_dest_af (rib_dest_t *dest)
506{
507 return dest->rnode->p.family;
508}
509
510/*
511 * rib_dest_table
512 */
513static inline struct route_table *
514rib_dest_table (rib_dest_t *dest)
515{
516 return dest->rnode->table;
517}
518
1b5ed1b0
AS
519/*
520 * rib_dest_vrf
521 */
b72ede27 522static inline struct zebra_vrf *
1b5ed1b0
AS
523rib_dest_vrf (rib_dest_t *dest)
524{
b72ede27 525 return rib_table_info (rib_dest_table (dest))->zvrf;
1b5ed1b0
AS
526}
527
0915bb0c
AS
528/*
529 * rib_tables_iter_init
530 */
531static inline void
532rib_tables_iter_init (rib_tables_iter_t *iter)
533
534{
535 memset (iter, 0, sizeof (*iter));
536 iter->state = RIB_TABLES_ITER_S_INIT;
537}
538
539/*
540 * rib_tables_iter_started
541 *
542 * Returns TRUE if this iterator has started iterating over the set of
543 * tables.
544 */
545static inline int
546rib_tables_iter_started (rib_tables_iter_t *iter)
547{
548 return iter->state != RIB_TABLES_ITER_S_INIT;
549}
550
551/*
552 * rib_tables_iter_cleanup
553 */
554static inline void
555rib_tables_iter_cleanup (rib_tables_iter_t *iter)
556{
557 iter->state = RIB_TABLES_ITER_S_DONE;
558}
559
718e3744 560#endif /*_ZEBRA_RIB_H */