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
33 #define DISTANCE_INFINITY 255
41 /* Nexthop structure */
42 struct nexthop *nexthop;
53 /* Type fo this route. */
59 /* Which routing table */
67 u_int32_t nexthop_mtu;
72 /* Flags of this route.
73 * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed
74 * to clients via Zserv
78 /* RIB internal status */
80 #define RIB_ENTRY_REMOVED (1 << 0)
81 #define RIB_ENTRY_CHANGED (1 << 1)
82 #define RIB_ENTRY_SELECTED_FIB (1 << 2)
84 /* Nexthop information. */
86 u_char nexthop_active_num;
87 u_char nexthop_fib_num;
90 /* meta-queue structure:
91 * sub-queue 0: connected, kernel
93 * sub-queue 2: RIP, RIPng, OSPF, OSPF6, IS-IS
94 * sub-queue 3: iBGP, eBGP
95 * sub-queue 4: any other origin (if any)
100 struct list *subq[MQ_SIZE];
101 u_int32_t size; /* sum of lengths of all subqueues */
105 * Structure that represents a single destination (prefix).
107 typedef struct rib_dest_t_
111 * Back pointer to the route node for this destination. This helps
112 * us get to the prefix that this structure is for.
114 struct route_node *rnode;
117 * Doubly-linked list of routes for this prefix.
127 * Linkage to put dest on the FPM processing queue.
129 TAILQ_ENTRY(rib_dest_t_) fpm_q_entries;
133 #define RIB_ROUTE_QUEUED(x) (1 << (x))
136 * The maximum qindex that can be used.
138 #define ZEBRA_MAX_QINDEX (MQ_SIZE - 1)
141 * This flag indicates that a given prefix has been 'advertised' to
142 * the FPM to be installed in the forwarding plane.
144 #define RIB_DEST_SENT_TO_FPM (1 << (ZEBRA_MAX_QINDEX + 1))
147 * This flag is set when we need to send an update to the FPM about a
150 #define RIB_DEST_UPDATE_FPM (1 << (ZEBRA_MAX_QINDEX + 2))
153 * Macro to iterate over each route for a destination (prefix).
155 #define RIB_DEST_FOREACH_ROUTE(dest, rib) \
156 for ((rib) = (dest) ? (dest)->routes : NULL; (rib); (rib) = (rib)->next)
159 * Same as above, but allows the current node to be unlinked.
161 #define RIB_DEST_FOREACH_ROUTE_SAFE(dest, rib, next) \
162 for ((rib) = (dest) ? (dest)->routes : NULL; \
163 (rib) && ((next) = (rib)->next, 1); \
166 #define RNODE_FOREACH_RIB(rn, rib) \
167 RIB_DEST_FOREACH_ROUTE (rib_dest_from_rnode (rn), rib)
169 #define RNODE_FOREACH_RIB_SAFE(rn, rib, next) \
170 RIB_DEST_FOREACH_ROUTE_SAFE (rib_dest_from_rnode (rn), rib, next)
172 /* Static route information. */
175 /* For linked list. */
176 struct static_route *prev;
177 struct static_route *next;
179 /* VRF identifier. */
182 /* Administrative distance. */
188 /* Flag for this static route's type. */
190 #define STATIC_IPV4_GATEWAY 1
191 #define STATIC_IPV4_IFNAME 2
192 #define STATIC_IPV4_BLACKHOLE 3
193 #define STATIC_IPV6_GATEWAY 4
194 #define STATIC_IPV6_GATEWAY_IFNAME 5
195 #define STATIC_IPV6_IFNAME 6
204 see ZEBRA_FLAG_REJECT
209 /* The following for loop allows to iterate over the nexthop
210 * structure of routes.
212 * We have to maintain quite a bit of state:
214 * nexthop: The pointer to the current nexthop, either in the
215 * top-level chain or in the resolved chain of ni.
216 * tnexthop: The pointer to the current nexthop in the top-level
218 * recursing: Information if nh currently is in the top-level chain
219 * (0) or in a resolved chain (1).
221 * Initialization: Set `nexthop' and `tnexthop' to the head of the
222 * top-level chain. As nexthop is in the top level chain, set recursing
225 * Iteration check: Check that the `nexthop' pointer is not NULL.
227 * Iteration step: This is the tricky part. Check if `nexthop' has
228 * NEXTHOP_FLAG_RECURSIVE set. If yes, this implies that `nexthop' is in
229 * the top level chain and has at least one nexthop attached to
230 * `nexthop->resolved'. As we want to descend into `nexthop->resolved',
231 * set `recursing' to 1 and set `nexthop' to `nexthop->resolved'.
232 * `tnexthop' is left alone in that case so we can remember which nexthop
233 * in the top level chain we are currently handling.
235 * If NEXTHOP_FLAG_RECURSIVE is not set, `nexthop' will progress in its
236 * current chain. If we are recursing, `nexthop' will be set to
237 * `nexthop->next' and `tnexthop' will be left alone. If we are not
238 * recursing, both `tnexthop' and `nexthop' will be set to `nexthop->next'
239 * as we are progressing in the top level chain.
240 * If we encounter `nexthop->next == NULL', we will clear the `recursing'
241 * flag as we arived either at the end of the resolved chain or at the end
242 * of the top level chain. In both cases, we set `tnexthop' and `nexthop'
243 * to `tnexthop->next', progressing to the next position in the top-level
244 * chain and possibly to its end marked by NULL.
246 #define ALL_NEXTHOPS_RO(head, nexthop, tnexthop, recursing) \
247 (tnexthop) = (nexthop) = (head), (recursing) = 0; \
249 (nexthop) = CHECK_FLAG((nexthop)->flags, NEXTHOP_FLAG_RECURSIVE) \
250 ? (((recursing) = 1), (nexthop)->resolved) \
251 : ((nexthop)->next ? ((recursing) ? (nexthop)->next \
252 : ((tnexthop) = (nexthop)->next)) \
253 : (((recursing) = 0),((tnexthop) = (tnexthop)->next)))
255 /* Structure holding nexthop & VRF identifier,
256 * used for applying the route-map. */
259 struct nexthop *nexthop;
264 #if defined (HAVE_RTADV)
265 /* Structure which hold status of router advertisement. */
271 int adv_msec_if_count;
273 struct thread *ra_read;
274 struct thread *ra_timer;
276 #endif /* HAVE_RTADV */
279 /* Socket interface to kernel */
284 struct sockaddr_nl snl;
289 /* Routing table instance. */
295 /* Routing table name. */
301 /* FIB identifier. */
305 struct route_table *table[AFI_MAX][SAFI_MAX];
307 /* Static route configuration. */
308 struct route_table *stable[AFI_MAX][SAFI_MAX];
311 struct nlsock netlink; /* kernel messages */
312 struct nlsock netlink_cmd; /* command channel */
313 struct thread *t_netlink;
316 /* 2nd pointer type used primarily to quell a warning on
317 * ALL_LIST_ELEMENTS_RO
319 struct list _rid_all_sorted_list;
320 struct list _rid_lo_sorted_list;
321 struct list *rid_all_sorted_list;
322 struct list *rid_lo_sorted_list;
323 struct prefix rid_user_assigned;
325 #if defined (HAVE_RTADV)
327 #endif /* HAVE_RTADV */
329 /* Recursive Nexthop table */
330 struct route_table *rnh_table[AFI_MAX];
336 * Structure that is hung off of a route_table that holds information about
339 typedef struct rib_table_info_t_
343 * Back pointer to zebra_vrf.
345 struct zebra_vrf *zvrf;
353 RIB_TABLES_ITER_S_INIT,
354 RIB_TABLES_ITER_S_ITERATING,
355 RIB_TABLES_ITER_S_DONE
356 } rib_tables_iter_state_t;
359 * Structure that holds state for iterating over all tables in the
360 * Routing Information Base.
362 typedef struct rib_tables_iter_t_
367 rib_tables_iter_state_t state;
370 /* RPF lookup behaviour */
373 MCAST_NO_CONFIG = 0, /* MIX_MRIB_FIRST, but no show in config write */
374 MCAST_MRIB_ONLY, /* MRIB only */
375 MCAST_URIB_ONLY, /* URIB only */
376 MCAST_MIX_MRIB_FIRST, /* MRIB, if nothing at all then URIB */
377 MCAST_MIX_DISTANCE, /* MRIB & URIB, lower distance wins */
378 MCAST_MIX_PFXLEN, /* MRIB & URIB, longer prefix wins */
379 /* on equal value, MRIB wins for last 2 */
382 extern void multicast_mode_ipv4_set (enum multicast_mode mode);
383 extern enum multicast_mode multicast_mode_ipv4_get (void);
385 extern const char *nexthop_type_to_str (enum nexthop_types_t nh_type);
386 extern struct nexthop *rib_nexthop_ifindex_add (struct rib *, ifindex_t);
387 extern struct nexthop *rib_nexthop_ifname_add (struct rib *, char *);
388 extern struct nexthop *rib_nexthop_blackhole_add (struct rib *);
389 extern struct nexthop *rib_nexthop_ipv4_add (struct rib *, struct in_addr *,
391 extern struct nexthop *rib_nexthop_ipv4_ifindex_add (struct rib *,
396 extern void rib_nexthop_add (struct rib *rib, struct nexthop *nexthop);
398 extern int nexthop_has_fib_child(struct nexthop *);
399 extern void rib_lookup_and_dump (struct prefix_ipv4 *);
400 #define rib_dump(prefix ,rib) _rib_dump(__func__, prefix, rib)
401 extern void _rib_dump (const char *,
402 union prefix46constptr, const struct rib *);
403 extern int rib_lookup_ipv4_route (struct prefix_ipv4 *, union sockunion *,
405 #define ZEBRA_RIB_LOOKUP_ERROR -1
406 #define ZEBRA_RIB_FOUND_EXACT 0
407 #define ZEBRA_RIB_FOUND_NOGATE 1
408 #define ZEBRA_RIB_FOUND_CONNECTED 2
409 #define ZEBRA_RIB_NOTFOUND 3
411 extern struct nexthop *rib_nexthop_ipv6_add (struct rib *, struct in6_addr *);
412 extern struct nexthop *rib_nexthop_ipv6_ifindex_add (struct rib *,
416 extern struct zebra_vrf *zebra_vrf_lookup (vrf_id_t vrf_id);
417 extern struct zebra_vrf *zebra_vrf_alloc (vrf_id_t);
418 extern struct route_table *zebra_vrf_table (afi_t, safi_t, vrf_id_t);
419 extern struct route_table *zebra_vrf_static_table (afi_t, safi_t, vrf_id_t);
422 * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
423 * also implicitly withdraw equal prefix of same type. */
424 extern int rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
425 struct in_addr *gate, struct in_addr *src,
426 ifindex_t ifindex, vrf_id_t vrf_id, int table_id,
427 u_int32_t, u_int32_t, u_char, safi_t);
429 extern int rib_add_ipv4_multipath (struct prefix_ipv4 *, struct rib *, safi_t);
431 extern int rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
432 struct in_addr *gate, ifindex_t ifindex,
433 vrf_id_t, safi_t safi);
435 extern struct rib *rib_match_ipv4_safi (struct in_addr addr, safi_t safi,
436 int skip_bgp, struct route_node **rn_out,
438 extern struct rib *rib_match_ipv4_multicast (struct in_addr addr,
439 struct route_node **rn_out,
442 extern struct rib *rib_lookup_ipv4 (struct prefix_ipv4 *, vrf_id_t);
444 extern void rib_update (vrf_id_t);
445 extern void rib_weed_tables (void);
446 extern void rib_sweep_route (void);
447 extern void rib_close_table (struct route_table *);
448 extern void rib_close (void);
449 extern void rib_init (void);
450 extern unsigned long rib_score_proto (u_char proto);
453 static_add_ipv4_safi (safi_t safi, struct prefix *p, struct in_addr *gate,
454 const char *ifname, u_char flags, route_tag_t,
455 u_char distance, vrf_id_t vrf_id);
457 static_delete_ipv4_safi (safi_t safi, struct prefix *p, struct in_addr *gate,
458 const char *ifname, route_tag_t tag, u_char distance,
462 rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
463 struct in6_addr *gate, ifindex_t ifindex, vrf_id_t vrf_id,
464 int table_id, u_int32_t metric, u_int32_t mtu,
465 u_char distance, safi_t safi);
468 rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
469 struct in6_addr *gate, ifindex_t ifindex, vrf_id_t vrf_id, safi_t safi);
471 extern struct rib *rib_lookup_ipv6 (struct in6_addr *, vrf_id_t);
473 extern struct rib *rib_match_ipv6 (struct in6_addr *, vrf_id_t);
475 extern struct route_table *rib_table_ipv6;
478 static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
479 const char *ifname, u_char flags, route_tag_t,
480 u_char distance, vrf_id_t vrf_id);
483 rib_add_ipv6_multipath (struct prefix_ipv6 *, struct rib *, safi_t);
486 static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
487 const char *ifname, route_tag_t, u_char distance,
490 extern int rib_gc_dest (struct route_node *rn);
491 extern struct route_table *rib_tables_iter_next (rib_tables_iter_t *iter);
500 static inline rib_table_info_t *
501 rib_table_info (struct route_table *table)
503 return (rib_table_info_t *) table->info;
507 * rib_dest_from_rnode
509 static inline rib_dest_t *
510 rib_dest_from_rnode (struct route_node *rn)
512 return (rib_dest_t *) rn->info;
518 * Returns a pointer to the list of routes corresponding to the given
521 static inline struct rib *
522 rnode_to_ribs (struct route_node *rn)
526 dest = rib_dest_from_rnode (rn);
536 static inline struct prefix *
537 rib_dest_prefix (rib_dest_t *dest)
539 return &dest->rnode->p;
545 * Returns the address family that the destination is for.
548 rib_dest_af (rib_dest_t *dest)
550 return dest->rnode->p.family;
556 static inline struct route_table *
557 rib_dest_table (rib_dest_t *dest)
559 return dest->rnode->table;
565 static inline struct zebra_vrf *
566 rib_dest_vrf (rib_dest_t *dest)
568 return rib_table_info (rib_dest_table (dest))->zvrf;
572 * rib_tables_iter_init
575 rib_tables_iter_init (rib_tables_iter_t *iter)
578 memset (iter, 0, sizeof (*iter));
579 iter->state = RIB_TABLES_ITER_S_INIT;
583 * rib_tables_iter_started
585 * Returns TRUE if this iterator has started iterating over the set of
589 rib_tables_iter_started (rib_tables_iter_t *iter)
591 return iter->state != RIB_TABLES_ITER_S_INIT;
595 * rib_tables_iter_cleanup
598 rib_tables_iter_cleanup (rib_tables_iter_t *iter)
600 iter->state = RIB_TABLES_ITER_S_DONE;
603 #endif /*_ZEBRA_RIB_H */