2 * Routing Information Base header
3 * Copyright (C) 1997 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
32 #define DISTANCE_INFINITY 255
33 #define ZEBRA_KERNEL_TABLE_MAX 252 /* support for no more than this rt tables */
41 /* Nexthop structure */
42 struct nexthop
*nexthop
;
50 /* Type fo this route. */
53 /* Source protocol instance */
59 /* Which routing table */
71 /* Flags of this route.
72 * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed
73 * to clients via Zserv
77 /* RIB internal status */
79 #define RIB_ENTRY_REMOVED 0x1
80 /* to simplify NHT logic when NHs change, instead of doing a NH by NH cmp */
81 #define RIB_ENTRY_NEXTHOPS_CHANGED 0x2
83 /* Nexthop information. */
85 u_char nexthop_active_num
;
86 u_char nexthop_fib_num
;
89 /* meta-queue structure:
90 * sub-queue 0: connected, kernel
92 * sub-queue 2: RIP, RIPng, OSPF, OSPF6, IS-IS
93 * sub-queue 3: iBGP, eBGP
94 * sub-queue 4: any other origin (if any)
99 struct list
*subq
[MQ_SIZE
];
100 u_int32_t size
; /* sum of lengths of all subqueues */
104 * Structure that represents a single destination (prefix).
106 typedef struct rib_dest_t_
110 * Back pointer to the route node for this destination. This helps
111 * us get to the prefix that this structure is for.
113 struct route_node
*rnode
;
116 * Doubly-linked list of routes for this prefix.
126 * Linkage to put dest on the FPM processing queue.
128 TAILQ_ENTRY(rib_dest_t_
) fpm_q_entries
;
132 #define RIB_ROUTE_QUEUED(x) (1 << (x))
135 * The maximum qindex that can be used.
137 #define ZEBRA_MAX_QINDEX (MQ_SIZE - 1)
140 * This flag indicates that a given prefix has been 'advertised' to
141 * the FPM to be installed in the forwarding plane.
143 #define RIB_DEST_SENT_TO_FPM (1 << (ZEBRA_MAX_QINDEX + 1))
146 * This flag is set when we need to send an update to the FPM about a
149 #define RIB_DEST_UPDATE_FPM (1 << (ZEBRA_MAX_QINDEX + 2))
152 * Macro to iterate over each route for a destination (prefix).
154 #define RIB_DEST_FOREACH_ROUTE(dest, rib) \
155 for ((rib) = (dest) ? (dest)->routes : NULL; (rib); (rib) = (rib)->next)
158 * Same as above, but allows the current node to be unlinked.
160 #define RIB_DEST_FOREACH_ROUTE_SAFE(dest, rib, next) \
161 for ((rib) = (dest) ? (dest)->routes : NULL; \
162 (rib) && ((next) = (rib)->next, 1); \
165 #define RNODE_FOREACH_RIB(rn, rib) \
166 RIB_DEST_FOREACH_ROUTE (rib_dest_from_rnode (rn), rib)
168 #define RNODE_FOREACH_RIB_SAFE(rn, rib, next) \
169 RIB_DEST_FOREACH_ROUTE_SAFE (rib_dest_from_rnode (rn), rib, next)
171 /* Static route information. */
174 /* For linked list. */
175 struct static_ipv4
*prev
;
176 struct static_ipv4
*next
;
178 /* VRF identifier. */
181 /* Administrative distance. */
187 /* Flag for this static route's type. */
189 #define STATIC_IPV4_GATEWAY 1
190 #define STATIC_IPV4_IFNAME 2
191 #define STATIC_IPV4_BLACKHOLE 3
203 see ZEBRA_FLAG_REJECT
209 /* Static route information. */
212 /* For linked list. */
213 struct static_ipv6
*prev
;
214 struct static_ipv6
*next
;
216 /* VRF identifier. */
219 /* Administrative distance. */
225 /* Flag for this static route's type. */
227 #define STATIC_IPV6_GATEWAY 1
228 #define STATIC_IPV6_GATEWAY_IFNAME 2
229 #define STATIC_IPV6_IFNAME 3
232 struct in6_addr ipv6
;
238 see ZEBRA_FLAG_REJECT
242 #endif /* HAVE_IPV6 */
244 /* The following for loop allows to iterate over the nexthop
245 * structure of routes.
247 * We have to maintain quite a bit of state:
249 * nexthop: The pointer to the current nexthop, either in the
250 * top-level chain or in the resolved chain of ni.
251 * tnexthop: The pointer to the current nexthop in the top-level
253 * recursing: Information if nh currently is in the top-level chain
254 * (0) or in a resolved chain (1).
256 * Initialization: Set `nexthop' and `tnexthop' to the head of the
257 * top-level chain. As nexthop is in the top level chain, set recursing
260 * Iteration check: Check that the `nexthop' pointer is not NULL.
262 * Iteration step: This is the tricky part. Check if `nexthop' has
263 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' is in
264 * the top level chain and has at least one nexthop attached to
265 * `nexthop->resolved'. As we want to descend into `nexthop->resolved',
266 * set `recursing' to 1 and set `nexthop' to `nexthop->resolved'.
267 * `tnexthop' is left alone in that case so we can remember which nexthop
268 * in the top level chain we are currently handling.
270 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
271 * current chain. If we are recursing, `nexthop' will be set to
272 * `nexthop->next' and `tnexthop' will be left alone. If we are not
273 * recursing, both `tnexthop' and `nexthop' will be set to `nexthop->next'
274 * as we are progressing in the top level chain.
275 * If we encounter `nexthop->next == NULL', we will clear the `recursing'
276 * flag as we arived either at the end of the resolved chain or at the end
277 * of the top level chain. In both cases, we set `tnexthop' and `nexthop'
278 * to `tnexthop->next', progressing to the next position in the top-level
279 * chain and possibly to its end marked by NULL.
281 #define ALL_NEXTHOPS_RO(head, nexthop, tnexthop, recursing) \
282 (tnexthop) = (nexthop) = (head), (recursing) = 0; \
284 (nexthop) = CHECK_FLAG((nexthop)->flags, NEXTHOP_FLAG_RECURSIVE) \
285 ? (((recursing) = 1), (nexthop)->resolved) \
286 : ((nexthop)->next ? ((recursing) ? (nexthop)->next \
287 : ((tnexthop) = (nexthop)->next)) \
288 : (((recursing) = 0),((tnexthop) = (tnexthop)->next)))
290 /* Router advertisement feature. */
292 #if (defined(LINUX_IPV6) && (defined(__GLIBC__) && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 1)) || defined(KAME)
299 #if defined (HAVE_IPV6) && defined (RTADV)
300 /* Structure which hold status of router advertisement. */
306 int adv_msec_if_count
;
308 struct thread
*ra_read
;
309 struct thread
*ra_timer
;
311 #endif /* RTADV && HAVE_IPV6 */
314 /* Socket interface to kernel */
319 struct sockaddr_nl snl
;
324 /* Routing table instance. */
330 /* Routing table name. */
336 /* FIB identifier. */
340 struct route_table
*table
[AFI_MAX
][SAFI_MAX
];
342 /* Static route configuration. */
343 struct route_table
*stable
[AFI_MAX
][SAFI_MAX
];
345 /* Recursive Nexthop table */
346 struct route_table
*rnh_table
[AFI_MAX
];
348 /* Import check table (used mostly by BGP */
349 struct route_table
*import_check_table
[AFI_MAX
];
351 /* Routing tables off of main table for redistribute table */
352 struct route_table
*other_table
[AFI_MAX
][ZEBRA_KERNEL_TABLE_MAX
];
355 struct nlsock netlink
; /* kernel messages */
356 struct nlsock netlink_cmd
; /* command channel */
357 struct thread
*t_netlink
;
360 /* 2nd pointer type used primarily to quell a warning on
361 * ALL_LIST_ELEMENTS_RO
363 struct list _rid_all_sorted_list
;
364 struct list _rid_lo_sorted_list
;
365 struct list
*rid_all_sorted_list
;
366 struct list
*rid_lo_sorted_list
;
367 struct prefix rid_user_assigned
;
369 #if defined (HAVE_IPV6) && defined (RTADV)
371 #endif /* RTADV && HAVE_IPV6 */
377 * Structure that is hung off of a route_table that holds information about
380 typedef struct rib_table_info_t_
384 * Back pointer to zebra_vrf.
386 struct zebra_vrf
*zvrf
;
394 RIB_TABLES_ITER_S_INIT
,
395 RIB_TABLES_ITER_S_ITERATING
,
396 RIB_TABLES_ITER_S_DONE
397 } rib_tables_iter_state_t
;
400 * Structure that holds state for iterating over all tables in the
401 * Routing Information Base.
403 typedef struct rib_tables_iter_t_
408 rib_tables_iter_state_t state
;
411 extern struct nexthop
*nexthop_ifindex_add (struct rib
*, unsigned int);
412 extern struct nexthop
*nexthop_ifname_add (struct rib
*, char *);
413 extern struct nexthop
*nexthop_blackhole_add (struct rib
*);
414 extern struct nexthop
*nexthop_ipv4_add (struct rib
*, struct in_addr
*,
416 extern struct nexthop
* nexthop_ipv4_ifindex_ol_add (struct rib
*, const struct in_addr
*,
417 const struct in_addr
*, const unsigned);
418 extern struct nexthop
*nexthop_ipv4_ifindex_add (struct rib
*,
422 extern void nexthop_free (struct nexthop
*nexthop
, struct route_node
*);
423 extern void nexthops_free (struct nexthop
*nexthop
, struct route_node
*);
424 extern void nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
);
425 extern void copy_nexthops (struct rib
*rib
, struct nexthop
*nh
);
427 extern int nexthop_has_fib_child(struct nexthop
*);
428 extern void rib_lookup_and_dump (struct prefix_ipv4
*);
429 extern void rib_lookup_and_pushup (struct prefix_ipv4
*);
430 #define rib_dump(prefix ,rib) _rib_dump(__func__, prefix, rib)
431 extern void _rib_dump (const char *,
432 union prefix46constptr
, const struct rib
*);
433 extern int rib_lookup_ipv4_route (struct prefix_ipv4
*, union sockunion
*,
435 #define ZEBRA_RIB_LOOKUP_ERROR -1
436 #define ZEBRA_RIB_FOUND_EXACT 0
437 #define ZEBRA_RIB_FOUND_NOGATE 1
438 #define ZEBRA_RIB_FOUND_CONNECTED 2
439 #define ZEBRA_RIB_NOTFOUND 3
442 extern struct nexthop
*nexthop_ipv6_add (struct rib
*, struct in6_addr
*);
443 extern struct nexthop
*nexthop_ipv6_ifindex_add (struct rib
*rib
,
444 struct in6_addr
*ipv6
, unsigned int ifindex
);
445 extern struct nexthop
*nexthop_ipv6_ifname_add (struct rib
*rib
,
446 struct in6_addr
*ipv6
,
449 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
450 struct in6_addr
*gate
, unsigned int ifindex
, int table
);
451 #endif /* HAVE_IPV6 */
453 extern struct zebra_vrf
*zebra_vrf_lookup (vrf_id_t vrf_id
);
454 extern struct zebra_vrf
*zebra_vrf_alloc (vrf_id_t
);
455 extern struct route_table
*zebra_vrf_table (afi_t
, safi_t
, vrf_id_t
);
456 extern struct route_table
*zebra_vrf_static_table (afi_t
, safi_t
, vrf_id_t
);
457 extern struct route_table
*zebra_vrf_other_route_table (afi_t afi
, u_int32_t table_id
,
459 extern int is_zebra_valid_kernel_table(u_int32_t table_id
);
460 extern int is_zebra_main_routing_table(u_int32_t table_id
);
461 extern int zebra_check_addr (struct prefix
*p
);
464 * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
465 * also implicitly withdraw equal prefix of same type. */
466 extern int rib_add_ipv4 (int type
, u_short instance
, int flags
, struct prefix_ipv4
*p
,
467 struct in_addr
*gate
, struct in_addr
*src
,
468 unsigned int ifindex
, vrf_id_t vrf_id
, u_int32_t table_id
,
469 u_int32_t
, u_char
, safi_t
);
471 extern int rib_add_ipv4_multipath (struct prefix_ipv4
*, struct rib
*, safi_t
);
473 extern int rib_delete_ipv4 (int type
, u_short instance
, int flags
, struct prefix_ipv4
*p
,
474 struct in_addr
*gate
, unsigned int ifindex
,
475 vrf_id_t
, u_int32_t
, safi_t safi
);
477 extern struct rib
*rib_match_ipv4 (struct in_addr
, vrf_id_t
);
479 extern struct rib
*rib_lookup_ipv4 (struct prefix_ipv4
*, vrf_id_t
);
481 extern void rib_update (vrf_id_t
);
482 extern void rib_update_static (vrf_id_t
);
483 extern void rib_weed_tables (void);
484 extern void rib_sweep_route (void);
485 extern void rib_close_table (struct route_table
*);
486 extern void rib_close (void);
487 extern void rib_init (void);
488 extern unsigned long rib_score_proto (u_char proto
, u_short instance
);
490 extern void rib_queue_add (struct zebra_t
*zebra
, struct route_node
*rn
);
494 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
495 u_char flags
, u_short tag
, u_char distance
, vrf_id_t vrf_id
);
498 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
499 u_short tag
, u_char distance
, vrf_id_t vrf_id
);
503 rib_add_ipv6 (int type
, u_short instance
, int flags
, struct prefix_ipv6
*p
,
504 struct in6_addr
*gate
, unsigned int ifindex
, vrf_id_t vrf_id
,
505 u_int32_t table_id
, u_int32_t metric
, u_char distance
, safi_t safi
);
508 rib_delete_ipv6 (int type
, u_short instance
, int flags
, struct prefix_ipv6
*p
,
509 struct in6_addr
*gate
, unsigned int ifindex
, vrf_id_t vrf_id
,
510 u_int32_t table_id
, safi_t safi
);
512 extern struct rib
*rib_lookup_ipv6 (struct in6_addr
*, vrf_id_t
);
514 extern struct rib
*rib_match_ipv6 (struct in6_addr
*, vrf_id_t
);
516 extern struct route_table
*rib_table_ipv6
;
519 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
520 const char *ifname
, u_char flags
, u_short tag
,
521 u_char distance
, vrf_id_t vrf_id
);
524 rib_add_ipv6_multipath (struct prefix
*, struct rib
*, safi_t
,
528 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
529 const char *ifname
, u_short tag
, u_char distance
,
532 #endif /* HAVE_IPV6 */
534 extern int rib_gc_dest (struct route_node
*rn
);
535 extern struct route_table
*rib_tables_iter_next (rib_tables_iter_t
*iter
);
544 static inline rib_table_info_t
*
545 rib_table_info (struct route_table
*table
)
547 return (rib_table_info_t
*) table
->info
;
551 * rib_dest_from_rnode
553 static inline rib_dest_t
*
554 rib_dest_from_rnode (struct route_node
*rn
)
556 return (rib_dest_t
*) rn
->info
;
562 * Returns a pointer to the list of routes corresponding to the given
565 static inline struct rib
*
566 rnode_to_ribs (struct route_node
*rn
)
570 dest
= rib_dest_from_rnode (rn
);
580 static inline struct prefix
*
581 rib_dest_prefix (rib_dest_t
*dest
)
583 return &dest
->rnode
->p
;
589 * Returns the address family that the destination is for.
592 rib_dest_af (rib_dest_t
*dest
)
594 return dest
->rnode
->p
.family
;
600 static inline struct route_table
*
601 rib_dest_table (rib_dest_t
*dest
)
603 return dest
->rnode
->table
;
609 static inline struct zebra_vrf
*
610 rib_dest_vrf (rib_dest_t
*dest
)
612 return rib_table_info (rib_dest_table (dest
))->zvrf
;
616 * rib_tables_iter_init
619 rib_tables_iter_init (rib_tables_iter_t
*iter
)
622 memset (iter
, 0, sizeof (*iter
));
623 iter
->state
= RIB_TABLES_ITER_S_INIT
;
627 * rib_tables_iter_started
629 * Returns TRUE if this iterator has started iterating over the set of
633 rib_tables_iter_started (rib_tables_iter_t
*iter
)
635 return iter
->state
!= RIB_TABLES_ITER_S_INIT
;
639 * rib_tables_iter_cleanup
642 rib_tables_iter_cleanup (rib_tables_iter_t
*iter
)
644 iter
->state
= RIB_TABLES_ITER_S_DONE
;
647 #endif /*_ZEBRA_RIB_H */