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
31 #define DISTANCE_INFINITY 255
39 /* Nexthop structure */
40 struct nexthop
*nexthop
;
48 /* Type fo this route. */
51 /* Which routing table */
60 /* Flags of this route.
61 * This flag's definition is in lib/zebra.h ZEBRA_FLAG_* and is exposed
62 * to clients via Zserv
66 /* RIB internal status */
68 #define RIB_ENTRY_REMOVED (1 << 0)
70 /* Nexthop information. */
72 u_char nexthop_active_num
;
73 u_char nexthop_fib_num
;
76 /* meta-queue structure:
77 * sub-queue 0: connected, kernel
79 * sub-queue 2: RIP, RIPng, OSPF, OSPF6, IS-IS
80 * sub-queue 3: iBGP, eBGP
81 * sub-queue 4: any other origin (if any)
86 struct list
*subq
[MQ_SIZE
];
87 u_int32_t size
; /* sum of lengths of all subqueues */
91 * Structure that represents a single destination (prefix).
93 typedef struct rib_dest_t_
97 * Back pointer to the route node for this destination. This helps
98 * us get to the prefix that this structure is for.
100 struct route_node
*rnode
;
103 * Doubly-linked list of routes for this prefix.
113 * Linkage to put dest on the FPM processing queue.
115 TAILQ_ENTRY(rib_dest_t_
) fpm_q_entries
;
119 #define RIB_ROUTE_QUEUED(x) (1 << (x))
122 * The maximum qindex that can be used.
124 #define ZEBRA_MAX_QINDEX (MQ_SIZE - 1)
127 * This flag indicates that a given prefix has been 'advertised' to
128 * the FPM to be installed in the forwarding plane.
130 #define RIB_DEST_SENT_TO_FPM (1 << (ZEBRA_MAX_QINDEX + 1))
133 * This flag is set when we need to send an update to the FPM about a
136 #define RIB_DEST_UPDATE_FPM (1 << (ZEBRA_MAX_QINDEX + 2))
139 * Macro to iterate over each route for a destination (prefix).
141 #define RIB_DEST_FOREACH_ROUTE(dest, rib) \
142 for ((rib) = (dest) ? (dest)->routes : NULL; (rib); (rib) = (rib)->next)
145 * Same as above, but allows the current node to be unlinked.
147 #define RIB_DEST_FOREACH_ROUTE_SAFE(dest, rib, next) \
148 for ((rib) = (dest) ? (dest)->routes : NULL; \
149 (rib) && ((next) = (rib)->next, 1); \
152 #define RNODE_FOREACH_RIB(rn, rib) \
153 RIB_DEST_FOREACH_ROUTE (rib_dest_from_rnode (rn), rib)
155 #define RNODE_FOREACH_RIB_SAFE(rn, rib, next) \
156 RIB_DEST_FOREACH_ROUTE_SAFE (rib_dest_from_rnode (rn), rib, next)
158 /* Static route information. */
161 /* For linked list. */
162 struct static_ipv4
*prev
;
163 struct static_ipv4
*next
;
165 /* Administrative distance. */
168 /* Flag for this static route's type. */
170 #define STATIC_IPV4_GATEWAY 1
171 #define STATIC_IPV4_IFNAME 2
172 #define STATIC_IPV4_BLACKHOLE 3
184 see ZEBRA_FLAG_REJECT
190 /* Static route information. */
193 /* For linked list. */
194 struct static_ipv6
*prev
;
195 struct static_ipv6
*next
;
197 /* Administrative distance. */
200 /* Flag for this static route's type. */
202 #define STATIC_IPV6_GATEWAY 1
203 #define STATIC_IPV6_GATEWAY_IFNAME 2
204 #define STATIC_IPV6_IFNAME 3
207 struct in6_addr ipv6
;
213 see ZEBRA_FLAG_REJECT
217 #endif /* HAVE_IPV6 */
219 /* The following for loop allows to iterate over the nexthop
220 * structure of routes.
222 * We have to maintain quite a bit of state:
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
228 * recursing: Information if nh currently is in the top-level chain
229 * (0) or in a resolved chain (1).
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
235 * Iteration check: Check that the `nexthop' pointer is not NULL.
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.
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.
256 #define ALL_NEXTHOPS_RO(head, nexthop, tnexthop, recursing) \
257 (tnexthop) = (nexthop) = (head), (recursing) = 0; \
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)))
265 /* Routing table instance. */
268 /* Identifier. This is same as routing table vector index. */
271 /* Routing table name. */
277 /* FIB identifier. */
281 struct route_table
*table
[AFI_MAX
][SAFI_MAX
];
283 /* Static route configuration. */
284 struct route_table
*stable
[AFI_MAX
][SAFI_MAX
];
286 /* Recursive Nexthop table */
287 struct route_table
*rnh_table
[AFI_MAX
];
293 * Structure that is hung off of a route_table that holds information about
296 typedef struct rib_table_info_t_
300 * Back pointer to vrf.
310 RIB_TABLES_ITER_S_INIT
,
311 RIB_TABLES_ITER_S_ITERATING
,
312 RIB_TABLES_ITER_S_DONE
313 } rib_tables_iter_state_t
;
316 * Structure that holds state for iterating over all tables in the
317 * Routing Information Base.
319 typedef struct rib_tables_iter_t_
324 rib_tables_iter_state_t state
;
327 extern struct nexthop
*nexthop_ifindex_add (struct rib
*, unsigned int);
328 extern struct nexthop
*nexthop_ifname_add (struct rib
*, char *);
329 extern struct nexthop
*nexthop_blackhole_add (struct rib
*);
330 extern struct nexthop
*nexthop_ipv4_add (struct rib
*, struct in_addr
*,
332 extern struct nexthop
*nexthop_ipv4_ifindex_add (struct rib
*,
336 extern void nexthop_free (struct nexthop
*nexthop
);
337 extern void nexthops_free (struct nexthop
*nexthop
);
338 extern void nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
);
340 extern int nexthop_has_fib_child(struct nexthop
*);
341 extern void rib_lookup_and_dump (struct prefix_ipv4
*);
342 extern void rib_lookup_and_pushup (struct prefix_ipv4
*);
343 #define rib_dump(prefix ,rib) _rib_dump(__func__, prefix, rib)
344 extern void _rib_dump (const char *,
345 union prefix46constptr
, const struct rib
*);
346 extern int rib_lookup_ipv4_route (struct prefix_ipv4
*, union sockunion
*);
347 #define ZEBRA_RIB_LOOKUP_ERROR -1
348 #define ZEBRA_RIB_FOUND_EXACT 0
349 #define ZEBRA_RIB_FOUND_NOGATE 1
350 #define ZEBRA_RIB_FOUND_CONNECTED 2
351 #define ZEBRA_RIB_NOTFOUND 3
354 extern struct nexthop
*nexthop_ipv6_add (struct rib
*, struct in6_addr
*);
355 extern struct nexthop
*nexthop_ipv6_ifindex_add (struct rib
*rib
,
356 struct in6_addr
*ipv6
, unsigned int ifindex
);
357 extern struct nexthop
*nexthop_ipv6_ifname_add (struct rib
*rib
,
358 struct in6_addr
*ipv6
,
361 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
362 struct in6_addr
*gate
, unsigned int ifindex
, int table
);
363 #endif /* HAVE_IPV6 */
365 extern struct vrf
*vrf_lookup (u_int32_t
);
366 extern struct route_table
*vrf_table (afi_t afi
, safi_t safi
, u_int32_t id
);
367 extern struct route_table
*vrf_static_table (afi_t afi
, safi_t safi
, u_int32_t id
);
370 * All rib_add_ipv[46]* functions will not just add prefix into RIB, but
371 * also implicitly withdraw equal prefix of same type. */
372 extern int rib_add_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
373 struct in_addr
*gate
, struct in_addr
*src
,
374 unsigned int ifindex
, u_int32_t vrf_id
,
375 u_int32_t
, u_char
, safi_t
);
377 extern int rib_add_ipv4_multipath (struct prefix_ipv4
*, struct rib
*, safi_t
);
379 extern int rib_delete_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
380 struct in_addr
*gate
, unsigned int ifindex
,
381 u_int32_t
, safi_t safi
);
383 extern struct rib
*rib_match_ipv4 (struct in_addr
);
385 extern struct rib
*rib_lookup_ipv4 (struct prefix_ipv4
*);
387 extern void rib_update (void);
388 extern void rib_weed_tables (void);
389 extern void rib_sweep_route (void);
390 extern void rib_close (void);
391 extern void rib_init (void);
392 extern unsigned long rib_score_proto (u_char proto
);
395 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
396 u_char flags
, u_char distance
, u_int32_t vrf_id
);
399 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
400 u_char distance
, u_int32_t vrf_id
);
404 rib_add_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
405 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
,
406 u_int32_t metric
, u_char distance
, safi_t safi
);
409 rib_delete_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
410 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
, safi_t safi
);
412 extern struct rib
*rib_lookup_ipv6 (struct in6_addr
*);
414 extern struct rib
*rib_match_ipv6 (struct in6_addr
*);
416 extern struct route_table
*rib_table_ipv6
;
419 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
420 const char *ifname
, u_char flags
, u_char distance
,
424 rib_add_ipv6_multipath (struct prefix_ipv6
*, struct rib
*, safi_t
,
428 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
429 const char *ifname
, u_char distance
, u_int32_t vrf_id
);
431 #endif /* HAVE_IPV6 */
433 extern int rib_gc_dest (struct route_node
*rn
);
434 extern struct route_table
*rib_tables_iter_next (rib_tables_iter_t
*iter
);
443 static inline rib_table_info_t
*
444 rib_table_info (struct route_table
*table
)
446 return (rib_table_info_t
*) table
->info
;
450 * rib_dest_from_rnode
452 static inline rib_dest_t
*
453 rib_dest_from_rnode (struct route_node
*rn
)
455 return (rib_dest_t
*) rn
->info
;
461 * Returns a pointer to the list of routes corresponding to the given
464 static inline struct rib
*
465 rnode_to_ribs (struct route_node
*rn
)
469 dest
= rib_dest_from_rnode (rn
);
479 static inline struct prefix
*
480 rib_dest_prefix (rib_dest_t
*dest
)
482 return &dest
->rnode
->p
;
488 * Returns the address family that the destination is for.
491 rib_dest_af (rib_dest_t
*dest
)
493 return dest
->rnode
->p
.family
;
499 static inline struct route_table
*
500 rib_dest_table (rib_dest_t
*dest
)
502 return dest
->rnode
->table
;
508 static inline struct vrf
*
509 rib_dest_vrf (rib_dest_t
*dest
)
511 return rib_table_info (rib_dest_table (dest
))->vrf
;
515 * rib_tables_iter_init
518 rib_tables_iter_init (rib_tables_iter_t
*iter
)
521 memset (iter
, 0, sizeof (*iter
));
522 iter
->state
= RIB_TABLES_ITER_S_INIT
;
526 * rib_tables_iter_started
528 * Returns TRUE if this iterator has started iterating over the set of
532 rib_tables_iter_started (rib_tables_iter_t
*iter
)
534 return iter
->state
!= RIB_TABLES_ITER_S_INIT
;
538 * rib_tables_iter_cleanup
541 rib_tables_iter_cleanup (rib_tables_iter_t
*iter
)
543 iter
->state
= RIB_TABLES_ITER_S_DONE
;
546 #endif /*_ZEBRA_RIB_H */