1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include "zebra_memory.h"
31 #include "sockunion.h"
34 #include "workqueue.h"
40 #include "srcdest_table.h"
42 #include "zebra/rib.h"
44 #include "zebra/zebra_ns.h"
45 #include "zebra/zserv.h"
46 #include "zebra/zebra_vrf.h"
47 #include "zebra/redistribute.h"
48 #include "zebra/zebra_routemap.h"
49 #include "zebra/debug.h"
50 #include "zebra/zebra_fpm.h"
51 #include "zebra/zebra_rnh.h"
52 #include "zebra/interface.h"
53 #include "zebra/connected.h"
55 /* Should we allow non Quagga processes to delete our routes */
56 extern int allow_delete
;
58 /* Hold time for RIB process, should be very minimal.
59 * it is useful to able to set it otherwise for testing, hence exported
60 * as global here for test-rig code.
62 int rib_process_hold_time
= 10;
64 /* Each route type's string and default distance value. */
69 } route_info
[ZEBRA_ROUTE_MAX
] =
71 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
72 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
73 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
74 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
75 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
76 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
77 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
78 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
79 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
80 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
81 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10},
82 /* no entry/default: 150 */
85 /* RPF lookup behaviour */
86 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
89 static void __attribute__((format (printf
, 5, 6)))
90 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
, int priority
,
91 const char *msgfmt
, ...)
93 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
98 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
103 rib_table_info_t
*info
= srcdest_rnode_table_info (rn
);
104 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
106 if (info
->safi
== SAFI_MULTICAST
)
107 strcat(buf
, " (MRIB)");
111 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
114 zlog (NULL
, priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
117 #define rnode_debug(node, vrf_id, ...) \
118 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
119 #define rnode_info(node, ...) \
120 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
123 route_distance (int type
)
127 if ((unsigned)type
>= array_size(route_info
))
130 distance
= route_info
[type
].distance
;
136 is_zebra_valid_kernel_table(u_int32_t table_id
)
138 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
))
142 if ((table_id
== RT_TABLE_UNSPEC
) ||
143 (table_id
== RT_TABLE_LOCAL
) ||
144 (table_id
== RT_TABLE_COMPAT
))
152 is_zebra_main_routing_table(u_int32_t table_id
)
154 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
160 zebra_check_addr (struct prefix
*p
)
162 if (p
->family
== AF_INET
)
166 addr
= p
->u
.prefix4
.s_addr
;
169 if (IPV4_NET127 (addr
)
171 || IPV4_LINKLOCAL(addr
))
174 if (p
->family
== AF_INET6
)
176 if (IN6_IS_ADDR_LOOPBACK (&p
->u
.prefix6
))
178 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
184 /* Add nexthop to the end of a rib node's nexthop list */
186 rib_nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
188 nexthop_add(&rib
->nexthop
, nexthop
);
195 * copy_nexthop - copy a nexthop to the rib structure.
198 rib_copy_nexthops (struct rib
*rib
, struct nexthop
*nh
)
200 struct nexthop
*nexthop
;
202 nexthop
= nexthop_new();
203 nexthop
->flags
= nh
->flags
;
204 nexthop
->type
= nh
->type
;
205 nexthop
->ifindex
= nh
->ifindex
;
206 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
207 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
209 nexthop_add_labels (nexthop
, nh
->nh_label_type
, nh
->nh_label
->num_labels
,
210 &nh
->nh_label
->label
[0]);
211 rib_nexthop_add(rib
, nexthop
);
212 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
213 copy_nexthops(&nexthop
->resolved
, nh
->resolved
);
216 /* Delete specified nexthop from the list. */
218 rib_nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
221 nexthop
->next
->prev
= nexthop
->prev
;
223 nexthop
->prev
->next
= nexthop
->next
;
225 rib
->nexthop
= nexthop
->next
;
232 rib_nexthop_ifindex_add (struct rib
*rib
, ifindex_t ifindex
)
234 struct nexthop
*nexthop
;
236 nexthop
= nexthop_new();
237 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
238 nexthop
->ifindex
= ifindex
;
240 rib_nexthop_add (rib
, nexthop
);
246 rib_nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
248 struct nexthop
*nexthop
;
250 nexthop
= nexthop_new();
251 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
252 nexthop
->gate
.ipv4
= *ipv4
;
254 nexthop
->src
.ipv4
= *src
;
256 rib_nexthop_add (rib
, nexthop
);
262 rib_nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
263 struct in_addr
*src
, ifindex_t ifindex
)
265 struct nexthop
*nexthop
;
266 struct interface
*ifp
;
268 nexthop
= nexthop_new();
269 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
270 nexthop
->gate
.ipv4
= *ipv4
;
272 nexthop
->src
.ipv4
= *src
;
273 nexthop
->ifindex
= ifindex
;
274 ifp
= if_lookup_by_index (nexthop
->ifindex
);
275 /*Pending: need to think if null ifp here is ok during bootup?
276 There was a crash because ifp here was coming to be NULL */
278 if (connected_is_unnumbered(ifp
)) {
279 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
282 rib_nexthop_add (rib
, nexthop
);
288 rib_nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
290 struct nexthop
*nexthop
;
292 nexthop
= nexthop_new();
293 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
294 nexthop
->gate
.ipv6
= *ipv6
;
296 rib_nexthop_add (rib
, nexthop
);
302 rib_nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
305 struct nexthop
*nexthop
;
307 nexthop
= nexthop_new();
308 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
309 nexthop
->gate
.ipv6
= *ipv6
;
310 nexthop
->ifindex
= ifindex
;
312 rib_nexthop_add (rib
, nexthop
);
318 rib_nexthop_blackhole_add (struct rib
*rib
)
320 struct nexthop
*nexthop
;
322 nexthop
= nexthop_new();
323 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
324 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
326 rib_nexthop_add (rib
, nexthop
);
331 /* This method checks whether a recursive nexthop has at
332 * least one resolved nexthop in the fib.
335 nexthop_has_fib_child(struct nexthop
*nexthop
)
339 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
342 for (nh
= nexthop
->resolved
; nh
; nh
= nh
->next
)
343 if (CHECK_FLAG (nh
->flags
, NEXTHOP_FLAG_FIB
))
349 /* If force flag is not set, do not modify falgs at all for uninstall
350 the route from FIB. */
352 nexthop_active (afi_t afi
, struct rib
*rib
, struct nexthop
*nexthop
, int set
,
353 struct route_node
*top
)
356 struct route_table
*table
;
357 struct route_node
*rn
;
360 struct nexthop
*newhop
, *tnewhop
;
361 struct nexthop
*resolved_hop
;
363 struct interface
*ifp
;
365 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
) || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
366 nexthop
->ifindex
= 0;
370 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
371 zebra_deregister_rnh_static_nexthops(rib
->vrf_id
, nexthop
->resolved
, top
);
372 nexthops_free(nexthop
->resolved
);
373 nexthop
->resolved
= NULL
;
374 rib
->nexthop_mtu
= 0;
377 /* Skip nexthops that have been filtered out due to route-map */
378 /* The nexthops are specific to this route and so the same */
379 /* nexthop for a different route may not have this flag set */
380 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
384 * Check to see if we should trust the passed in information
385 * for UNNUMBERED interfaces as that we won't find the GW
386 * address in the routing table.
388 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
390 ifp
= if_lookup_by_index (nexthop
->ifindex
);
391 if (ifp
&& connected_is_unnumbered(ifp
))
393 if (if_is_operative(ifp
))
402 /* Make lookup prefix. */
403 memset (&p
, 0, sizeof (struct prefix
));
408 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
409 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
413 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
414 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
417 assert (afi
!= AFI_IP
&& afi
!= AFI_IP6
);
421 table
= zebra_vrf_table (afi
, SAFI_UNICAST
, rib
->vrf_id
);
425 rn
= route_node_match (table
, (struct prefix
*) &p
);
428 route_unlock_node (rn
);
430 /* If lookup self prefix return immediately. */
434 /* Pick up selected route. */
435 /* However, do not resolve over default route unless explicitly allowed. */
436 if (is_default_prefix (&rn
->p
) &&
437 !nh_resolve_via_default (p
.family
))
440 RNODE_FOREACH_RIB (rn
, match
)
442 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
445 /* if the next hop is imported from another table, skip it */
446 if (match
->type
== ZEBRA_ROUTE_TABLE
)
448 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
452 /* If there is no selected route or matched route is EGP, go up
458 } while (rn
&& rn
->info
== NULL
);
460 route_lock_node (rn
);
464 /* If the longest prefix match for the nexthop yields
465 * a blackhole, mark it as inactive. */
466 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
467 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
470 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
472 /* Directly point connected route. */
473 newhop
= match
->nexthop
;
476 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
||
477 nexthop
->type
== NEXTHOP_TYPE_IPV6
)
478 nexthop
->ifindex
= newhop
->ifindex
;
482 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
485 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
486 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
487 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
491 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
492 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
494 resolved_hop
= nexthop_new();
495 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
496 /* If the resolving route specifies a gateway, use it */
497 if (newhop
->type
== NEXTHOP_TYPE_IPV4
498 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
500 resolved_hop
->type
= newhop
->type
;
501 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
505 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
506 resolved_hop
->ifindex
= newhop
->ifindex
;
507 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
508 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
511 if (newhop
->type
== NEXTHOP_TYPE_IPV6
512 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
514 resolved_hop
->type
= newhop
->type
;
515 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
519 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
520 resolved_hop
->ifindex
= newhop
->ifindex
;
524 /* If the resolving route is an interface route,
525 * it means the gateway we are looking up is connected
526 * to that interface. (The actual network is _not_ onlink).
527 * Therefore, the resolved route should have the original
528 * gateway as nexthop as it is directly connected.
530 * On Linux, we have to set the onlink netlink flag because
531 * otherwise, the kernel won't accept the route. */
532 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
534 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
537 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
538 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
540 else if (afi
== AFI_IP6
)
542 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
543 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
545 resolved_hop
->ifindex
= newhop
->ifindex
;
548 nexthop_add(&nexthop
->resolved
, resolved_hop
);
554 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
557 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
558 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
562 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
564 resolved_hop
= nexthop_new();
565 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
566 /* If the resolving route specifies a gateway, use it */
567 if (newhop
->type
== NEXTHOP_TYPE_IPV4
568 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
570 resolved_hop
->type
= newhop
->type
;
571 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
575 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
576 resolved_hop
->ifindex
= newhop
->ifindex
;
577 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
578 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
581 if (newhop
->type
== NEXTHOP_TYPE_IPV6
582 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
584 resolved_hop
->type
= newhop
->type
;
585 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
589 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
590 resolved_hop
->ifindex
= newhop
->ifindex
;
594 /* If the resolving route is an interface route,
595 * it means the gateway we are looking up is connected
596 * to that interface. (The actual network is _not_ onlink).
597 * Therefore, the resolved route should have the original
598 * gateway as nexthop as it is directly connected.
600 * On Linux, we have to set the onlink netlink flag because
601 * otherwise, the kernel won't accept the route.
603 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
605 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
608 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
609 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
611 else if (afi
== AFI_IP6
)
613 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
614 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
616 resolved_hop
->ifindex
= newhop
->ifindex
;
619 nexthop_add(&nexthop
->resolved
, resolved_hop
);
624 rib
->nexthop_mtu
= match
->mtu
;
637 rib_match (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
638 union g_addr
*addr
, struct route_node
**rn_out
)
641 struct route_table
*table
;
642 struct route_node
*rn
;
644 struct nexthop
*newhop
, *tnewhop
;
648 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
652 memset (&p
, 0, sizeof (struct prefix
));
656 p
.u
.prefix4
= addr
->ipv4
;
657 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
661 p
.u
.prefix6
= addr
->ipv6
;
662 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
665 rn
= route_node_match (table
, (struct prefix
*) &p
);
669 route_unlock_node (rn
);
671 /* Pick up selected route. */
672 RNODE_FOREACH_RIB (rn
, match
)
674 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
676 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
680 /* If there is no selected route or matched route is EGP, go up
686 } while (rn
&& rn
->info
== NULL
);
688 route_lock_node (rn
);
692 if (match
->type
!= ZEBRA_ROUTE_CONNECT
)
695 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
696 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
714 rib_match_ipv4_multicast (vrf_id_t vrf_id
, struct in_addr addr
, struct route_node
**rn_out
)
716 struct rib
*rib
= NULL
, *mrib
= NULL
, *urib
= NULL
;
717 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
718 union g_addr gaddr
= { .ipv4
= addr
};
720 switch (ipv4_multicast_mode
)
722 case MCAST_MRIB_ONLY
:
723 return rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, rn_out
);
724 case MCAST_URIB_ONLY
:
725 return rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
726 case MCAST_NO_CONFIG
:
727 case MCAST_MIX_MRIB_FIRST
:
728 rib
= mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
730 rib
= urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
732 case MCAST_MIX_DISTANCE
:
733 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
734 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
736 rib
= urib
->distance
< mrib
->distance
? urib
: mrib
;
742 case MCAST_MIX_PFXLEN
:
743 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
744 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
746 rib
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? urib
: mrib
;
755 *rn_out
= (rib
== mrib
) ? m_rn
: u_rn
;
757 if (IS_ZEBRA_DEBUG_RIB
)
760 inet_ntop (AF_INET
, &addr
, buf
, BUFSIZ
);
762 zlog_debug("%s: %s: found %s, using %s",
764 mrib
? (urib
? "MRIB+URIB" : "MRIB") :
765 urib
? "URIB" : "nothing",
766 rib
== urib
? "URIB" : rib
== mrib
? "MRIB" : "none");
772 multicast_mode_ipv4_set (enum multicast_mode mode
)
774 if (IS_ZEBRA_DEBUG_RIB
)
775 zlog_debug("%s: multicast lookup mode set (%d)", __func__
, mode
);
776 ipv4_multicast_mode
= mode
;
780 multicast_mode_ipv4_get (void)
782 return ipv4_multicast_mode
;
786 rib_lookup_ipv4 (struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
788 struct route_table
*table
;
789 struct route_node
*rn
;
791 struct nexthop
*nexthop
, *tnexthop
;
795 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
799 rn
= route_node_lookup (table
, (struct prefix
*) p
);
801 /* No route for this prefix. */
806 route_unlock_node (rn
);
808 RNODE_FOREACH_RIB (rn
, match
)
810 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
812 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
819 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
822 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
823 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
830 * This clone function, unlike its original rib_lookup_ipv4(), checks
831 * if specified IPv4 route record (prefix/mask -> gate) exists in
832 * the whole RIB and has RIB_ENTRY_SELECTED_FIB set.
836 * 0: exact match found
837 * 1: a match was found with a different gate
838 * 2: connected route found
839 * 3: no matches found
842 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
,
845 struct route_table
*table
;
846 struct route_node
*rn
;
848 struct nexthop
*nexthop
, *tnexthop
;
853 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
855 return ZEBRA_RIB_LOOKUP_ERROR
;
857 /* Scan the RIB table for exactly matching RIB entry. */
858 rn
= route_node_lookup (table
, (struct prefix
*) p
);
860 /* No route for this prefix. */
862 return ZEBRA_RIB_NOTFOUND
;
865 route_unlock_node (rn
);
867 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
868 RNODE_FOREACH_RIB (rn
, match
)
870 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
872 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
876 /* None such found :( */
878 return ZEBRA_RIB_NOTFOUND
;
880 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
881 return ZEBRA_RIB_FOUND_CONNECTED
;
883 /* Ok, we have a cood candidate, let's check it's nexthop list... */
885 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
886 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
889 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip (qgate
))
890 return ZEBRA_RIB_FOUND_EXACT
;
891 if (IS_ZEBRA_DEBUG_RIB
)
893 char gate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
894 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
895 inet_ntop (AF_INET
, &sockunion2ip(qgate
), qgate_buf
, INET_ADDRSTRLEN
);
896 zlog_debug ("%s: qgate == %s, %s == %s", __func__
,
897 qgate_buf
, recursing
? "rgate" : "gate", gate_buf
);
902 return ZEBRA_RIB_FOUND_NOGATE
;
904 return ZEBRA_RIB_NOTFOUND
;
907 #define RIB_SYSTEM_ROUTE(R) \
908 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
910 /* This function verifies reachability of one given nexthop, which can be
911 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
912 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
913 * nexthop->ifindex will be updated appropriately as well.
914 * An existing route map can turn (otherwise active) nexthop into inactive, but
917 * The return value is the final value of 'ACTIVE' flag.
921 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
922 struct nexthop
*nexthop
, int set
)
924 struct interface
*ifp
;
925 route_map_result_t ret
= RMAP_MATCH
;
927 char buf
[SRCDEST2STR_BUFFER
];
928 struct prefix
*p
, *src_p
;
929 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
931 if (rn
->p
.family
== AF_INET
)
933 else if (rn
->p
.family
== AF_INET6
)
937 switch (nexthop
->type
)
939 case NEXTHOP_TYPE_IFINDEX
:
940 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
941 if (ifp
&& if_is_operative(ifp
))
942 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
944 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
946 case NEXTHOP_TYPE_IPV4
:
947 case NEXTHOP_TYPE_IPV4_IFINDEX
:
949 if (nexthop_active (AFI_IP
, rib
, nexthop
, set
, rn
))
950 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
952 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
954 case NEXTHOP_TYPE_IPV6
:
956 if (nexthop_active (AFI_IP6
, rib
, nexthop
, set
, rn
))
957 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
959 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
961 case NEXTHOP_TYPE_IPV6_IFINDEX
:
962 /* RFC 5549, v4 prefix with v6 NH */
963 if (rn
->p
.family
!= AF_INET
)
965 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
967 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
968 if (ifp
&& if_is_operative(ifp
))
969 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
971 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
975 if (nexthop_active (AFI_IP6
, rib
, nexthop
, set
, rn
))
976 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
978 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
981 case NEXTHOP_TYPE_BLACKHOLE
:
982 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
987 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
990 /* XXX: What exactly do those checks do? Do we support
991 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
992 if (RIB_SYSTEM_ROUTE(rib
) ||
993 (family
== AFI_IP
&& p
->family
!= AF_INET
) ||
994 (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
995 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
997 /* The original code didn't determine the family correctly
998 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
999 * from the rib_table_info in those cases.
1000 * Possibly it may be better to use only the rib_table_info
1005 rib_table_info_t
*info
;
1007 info
= srcdest_rnode_table_info(rn
);
1011 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
1013 /* It'll get set if required inside */
1014 ret
= zebra_route_map_check(family
, rib
->type
, p
, nexthop
, rib
->vrf_id
,
1016 if (ret
== RMAP_DENYMATCH
)
1018 if (IS_ZEBRA_DEBUG_RIB
)
1020 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1021 zlog_debug("%u:%s: Filtering out with NH out %s due to route map",
1023 ifindex2ifname_vrf (nexthop
->ifindex
, rib
->vrf_id
));
1025 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1027 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1030 /* Iterate over all nexthops of the given RIB entry and refresh their
1031 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1032 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1033 * is flagged with RIB_ENTRY_CHANGED. The 4th 'set' argument is
1034 * transparently passed to nexthop_active_check().
1036 * Return value is the new number of active nexthops.
1040 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
1042 struct nexthop
*nexthop
;
1043 union g_addr prev_src
;
1044 unsigned int prev_active
, new_active
, old_num_nh
;
1045 ifindex_t prev_index
;
1046 old_num_nh
= rib
->nexthop_active_num
;
1048 rib
->nexthop_active_num
= 0;
1049 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1051 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1053 /* No protocol daemon provides src and so we're skipping tracking it */
1054 prev_src
= nexthop
->rmap_src
;
1055 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1056 prev_index
= nexthop
->ifindex
;
1057 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
1058 rib
->nexthop_active_num
++;
1059 /* Don't allow src setting on IPv6 addr for now */
1060 if (prev_active
!= new_active
||
1061 prev_index
!= nexthop
->ifindex
||
1062 ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
&&
1063 nexthop
->type
< NEXTHOP_TYPE_IPV6
) &&
1064 prev_src
.ipv4
.s_addr
!= nexthop
->rmap_src
.ipv4
.s_addr
) ||
1065 ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
&&
1066 nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
) &&
1067 !(IPV6_ADDR_SAME (&prev_src
.ipv6
, &nexthop
->rmap_src
.ipv6
))))
1069 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1070 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1074 if (old_num_nh
!= rib
->nexthop_active_num
)
1075 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1077 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_CHANGED
))
1079 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1082 return rib
->nexthop_active_num
;
1087 /* Update flag indicates whether this is a "replace" or not. Currently, this
1088 * is only used for IPv4.
1091 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
, struct rib
*old
)
1094 struct nexthop
*nexthop
, *tnexthop
;
1095 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1097 struct prefix
*p
, *src_p
;
1099 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
1101 if (info
->safi
!= SAFI_UNICAST
)
1103 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1104 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1109 * Make sure we update the FPM any time we send new information to
1112 zfpm_trigger_update (rn
, "installing in kernel");
1113 ret
= kernel_route_rib (p
, src_p
, old
, rib
);
1115 /* If install succeeds, update FIB flag for nexthops. */
1118 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1120 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1123 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1124 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1126 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1133 /* Uninstall the route from kernel. */
1135 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
1138 struct nexthop
*nexthop
, *tnexthop
;
1139 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1141 struct prefix
*p
, *src_p
;
1143 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
1145 if (info
->safi
!= SAFI_UNICAST
)
1147 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1148 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1153 * Make sure we update the FPM any time we send new information to
1156 zfpm_trigger_update (rn
, "uninstalling from kernel");
1157 ret
= kernel_route_rib (p
, src_p
, rib
, NULL
);
1159 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1160 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1165 /* Uninstall the route from kernel. */
1167 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
1169 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1171 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
1173 if (info
->safi
== SAFI_UNICAST
)
1174 zfpm_trigger_update (rn
, "rib_uninstall");
1176 if (! RIB_SYSTEM_ROUTE (rib
))
1177 rib_uninstall_kernel (rn
, rib
);
1178 UNSET_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
);
1181 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1183 struct prefix
*p
, *src_p
;
1184 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
1186 redistribute_delete (p
, src_p
, rib
);
1187 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
1192 * rib_can_delete_dest
1194 * Returns TRUE if the given dest can be deleted from the table.
1197 rib_can_delete_dest (rib_dest_t
*dest
)
1205 * Don't delete the dest if we have to update the FPM about this
1208 if (CHECK_FLAG (dest
->flags
, RIB_DEST_UPDATE_FPM
) ||
1209 CHECK_FLAG (dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1218 * Garbage collect the rib dest corresponding to the given route node
1221 * Returns TRUE if the dest was deleted, FALSE otherwise.
1224 rib_gc_dest (struct route_node
*rn
)
1227 struct zebra_vrf
*zvrf
;
1229 dest
= rib_dest_from_rnode (rn
);
1233 if (!rib_can_delete_dest (dest
))
1236 zvrf
= rib_dest_vrf (dest
);
1237 if (IS_ZEBRA_DEBUG_RIB
)
1238 rnode_debug (rn
, zvrf_id (zvrf
), "removing dest from table");
1241 XFREE (MTYPE_RIB_DEST
, dest
);
1245 * Release the one reference that we keep on the route node.
1247 route_unlock_node (rn
);
1252 rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1255 zfpm_trigger_update (rn
, "new route selected");
1257 /* Update real nexthop. This may actually determine if nexthop is active or not. */
1258 if (!nexthop_active_update (rn
, new, 1))
1260 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1264 SET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1265 if (IS_ZEBRA_DEBUG_RIB
)
1267 char buf
[SRCDEST2STR_BUFFER
];
1268 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1269 zlog_debug ("%u:%s: Adding route rn %p, rib %p (type %d)",
1270 zvrf_id (zvrf
), buf
, rn
, new, new->type
);
1273 if (!RIB_SYSTEM_ROUTE (new))
1275 if (rib_install_kernel (rn
, new, NULL
))
1277 char buf
[SRCDEST2STR_BUFFER
];
1278 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1279 zlog_warn ("%u:%s: Route install failed",
1280 zvrf_id (zvrf
), buf
);
1284 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1288 rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1291 zfpm_trigger_update (rn
, "removing existing route");
1293 /* Uninstall from kernel. */
1294 if (IS_ZEBRA_DEBUG_RIB
)
1296 char buf
[SRCDEST2STR_BUFFER
];
1297 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1298 zlog_debug ("%u:%s: Deleting route rn %p, rib %p (type %d)",
1299 zvrf_id (zvrf
), buf
, rn
, old
, old
->type
);
1302 if (!RIB_SYSTEM_ROUTE (old
))
1303 rib_uninstall_kernel (rn
, old
);
1305 UNSET_FLAG (old
->status
, RIB_ENTRY_SELECTED_FIB
);
1307 /* Update nexthop for route, reset changed flag. */
1308 nexthop_active_update (rn
, old
, 1);
1309 UNSET_FLAG(old
->status
, RIB_ENTRY_CHANGED
);
1313 rib_process_update_fib (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1314 struct rib
*old
, struct rib
*new)
1316 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1322 * We have to install or update if a new route has been selected or
1323 * something has changed.
1326 CHECK_FLAG (new->status
, RIB_ENTRY_CHANGED
))
1328 zfpm_trigger_update (rn
, "updating existing route");
1330 /* Update the nexthop; we could determine here that nexthop is inactive. */
1331 if (nexthop_active_update (rn
, new, 1))
1334 /* If nexthop is active, install the selected route, if appropriate. If
1335 * the install succeeds, cleanup flags for prior route, if different from
1340 if (IS_ZEBRA_DEBUG_RIB
)
1342 char buf
[SRCDEST2STR_BUFFER
];
1343 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1345 zlog_debug ("%u:%s: Updating route rn %p, rib %p (type %d) "
1346 "old %p (type %d)", zvrf_id (zvrf
), buf
,
1347 rn
, new, new->type
, old
, old
->type
);
1349 zlog_debug ("%u:%s: Updating route rn %p, rib %p (type %d)",
1350 zvrf_id (zvrf
), buf
, rn
, new, new->type
);
1352 /* Non-system route should be installed. */
1353 if (!RIB_SYSTEM_ROUTE (new))
1355 if (rib_install_kernel (rn
, new, old
))
1357 char buf
[SRCDEST2STR_BUFFER
];
1358 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1360 zlog_warn ("%u:%s: Route install failed", zvrf_id (zvrf
), buf
);
1364 /* If install succeeded or system route, cleanup flags for prior route. */
1365 if (installed
&& new != old
)
1367 if (RIB_SYSTEM_ROUTE(new))
1369 if (!RIB_SYSTEM_ROUTE (old
))
1370 rib_uninstall_kernel (rn
, old
);
1374 for (nexthop
= old
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1375 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1379 /* Update for redistribution. */
1381 SET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1385 * If nexthop for selected route is not active or install failed, we
1386 * may need to uninstall and delete for redistribution.
1388 if (!nh_active
|| !installed
)
1390 if (IS_ZEBRA_DEBUG_RIB
)
1392 char buf
[SRCDEST2STR_BUFFER
];
1393 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1395 zlog_debug ("%u:%s: Deleting route rn %p, rib %p (type %d) "
1396 "old %p (type %d) - %s", zvrf_id (zvrf
), buf
,
1397 rn
, new, new->type
, old
, old
->type
,
1398 nh_active
? "install failed" : "nexthop inactive");
1400 zlog_debug ("%u:%s: Deleting route rn %p, rib %p (type %d) - %s",
1401 zvrf_id (zvrf
), buf
, rn
, new, new->type
,
1402 nh_active
? "install failed" : "nexthop inactive");
1405 if (!RIB_SYSTEM_ROUTE (old
))
1406 rib_uninstall_kernel (rn
, old
);
1407 UNSET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1413 * Same route selected; check if in the FIB and if not, re-install. This
1414 * is housekeeping code to deal with race conditions in kernel with linux
1415 * netlink reporting interface up before IPv4 or IPv6 protocol is ready
1418 if (!RIB_SYSTEM_ROUTE (new))
1422 for (ALL_NEXTHOPS_RO(new->nexthop
, nexthop
, tnexthop
, recursing
))
1423 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1429 rib_install_kernel (rn
, new, NULL
);
1433 /* Update prior route. */
1436 UNSET_FLAG (old
->status
, RIB_ENTRY_SELECTED_FIB
);
1438 /* Set real nexthop. */
1439 nexthop_active_update (rn
, old
, 1);
1440 UNSET_FLAG(old
->status
, RIB_ENTRY_CHANGED
);
1443 /* Clear changed flag. */
1444 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1447 /* Check if 'alternate' RIB entry is better than 'current'. */
1449 rib_choose_best (struct rib
*current
, struct rib
*alternate
)
1451 if (current
== NULL
)
1454 /* filter route selection in following order:
1455 * - connected beats other types
1456 * - lower distance beats higher
1457 * - lower metric beats higher for equal distance
1458 * - last, hence oldest, route wins tie break.
1461 /* Connected routes. Pick the last connected
1462 * route of the set of lowest metric connected routes.
1464 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
)
1466 if (current
->type
!= ZEBRA_ROUTE_CONNECT
1467 || alternate
->metric
<= current
->metric
)
1473 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1476 /* higher distance loses */
1477 if (alternate
->distance
< current
->distance
)
1479 if (current
->distance
< alternate
->distance
)
1482 /* metric tie-breaks equal distance */
1483 if (alternate
->metric
<= current
->metric
)
1489 /* Core function for processing routing information base. */
1491 rib_process (struct route_node
*rn
)
1495 struct rib
*old_selected
= NULL
;
1496 struct rib
*new_selected
= NULL
;
1497 struct rib
*old_fib
= NULL
;
1498 struct rib
*new_fib
= NULL
;
1499 struct rib
*best
= NULL
;
1500 char buf
[SRCDEST2STR_BUFFER
];
1502 struct zebra_vrf
*zvrf
= NULL
;
1503 struct prefix
*p
, *src_p
;
1504 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1505 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1509 dest
= rib_dest_from_rnode (rn
);
1512 zvrf
= rib_dest_vrf (dest
);
1513 vrf_id
= zvrf_id (zvrf
);
1516 if (IS_ZEBRA_DEBUG_RIB
)
1517 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1519 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1520 zlog_debug ("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1522 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1524 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1525 zlog_debug ("%u:%s: Examine rib %p (type %d) status %x flags %x "
1526 "dist %d metric %d",
1527 vrf_id
, buf
, rib
, rib
->type
, rib
->status
,
1528 rib
->flags
, rib
->distance
, rib
->metric
);
1530 UNSET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1532 /* Currently selected rib. */
1533 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1535 assert (old_selected
== NULL
);
1538 /* Currently in fib */
1539 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
1541 assert (old_fib
== NULL
);
1545 /* Skip deleted entries from selection */
1546 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1549 /* Skip unreachable nexthop. */
1550 /* This first call to nexthop_active_update is merely to determine if
1551 * there's any change to nexthops associated with this RIB entry. Now,
1552 * rib_process() can be invoked due to an external event such as link
1553 * down or due to next-hop-tracking evaluation. In the latter case,
1554 * a decision has already been made that the NHs have changed. So, no
1555 * need to invoke a potentially expensive call again. Further, since
1556 * the change might be in a recursive NH which is not caught in
1557 * the nexthop_active_update() code. Thus, we might miss changes to
1560 if (!CHECK_FLAG(rib
->status
, RIB_ENTRY_CHANGED
) &&
1561 ! nexthop_active_update (rn
, rib
, 0))
1563 if (rib
->type
== ZEBRA_ROUTE_TABLE
)
1565 /* XXX: HERE BE DRAGONS!!!!!
1566 * In all honesty, I have not yet figured out what this part
1567 * does or why the RIB_ENTRY_CHANGED test above is correct
1568 * or why we need to delete a route here, and also not whether
1569 * this concerns both selected and fib route, or only selected
1571 /* This entry was denied by the 'ip protocol table' route-map, we
1572 * need to delete it */
1573 if (rib
!= old_selected
)
1575 if (IS_ZEBRA_DEBUG_RIB
)
1576 zlog_debug ("%s: %s: imported via import-table but denied "
1577 "by the ip protocol table route-map",
1579 rib_unlink (rn
, rib
);
1582 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1588 /* Infinite distance. */
1589 if (rib
->distance
== DISTANCE_INFINITY
)
1591 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1595 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
))
1597 best
= rib_choose_best(new_fib
, rib
);
1598 if (new_fib
&& best
!= new_fib
)
1599 UNSET_FLAG (new_fib
->status
, RIB_ENTRY_CHANGED
);
1604 best
= rib_choose_best(new_selected
, rib
);
1605 if (new_selected
&& best
!= new_selected
)
1606 UNSET_FLAG (new_selected
->status
, RIB_ENTRY_CHANGED
);
1607 new_selected
= best
;
1610 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1611 } /* RNODE_FOREACH_RIB */
1613 /* If no FIB override route, use the selected route also for FIB */
1614 if (new_fib
== NULL
)
1615 new_fib
= new_selected
;
1617 /* After the cycle is finished, the following pointers will be set:
1618 * old_selected --- RIB entry currently having SELECTED
1619 * new_selected --- RIB entry that is newly SELECTED
1620 * old_fib --- RIB entry currently in kernel FIB
1621 * new_fib --- RIB entry that is newly to be in kernel FIB
1623 * new_selected will get SELECTED flag, and is going to be redistributed
1624 * the zclients. new_fib (which can be new_selected) will be installed in kernel.
1627 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1629 zlog_debug ("%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1631 (void *)old_selected
,
1632 (void *)new_selected
,
1637 /* Buffer RIB_ENTRY_CHANGED here, because it will get cleared if
1638 * fib == selected */
1639 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1642 /* Update fib according to selection results */
1643 if (new_fib
&& old_fib
)
1644 rib_process_update_fib (zvrf
, rn
, old_fib
, new_fib
);
1646 rib_process_add_fib (zvrf
, rn
, new_fib
);
1648 rib_process_del_fib (zvrf
, rn
, old_fib
);
1650 /* Redistribute SELECTED entry */
1651 if (old_selected
!= new_selected
|| selected_changed
)
1653 struct nexthop
*nexthop
, *tnexthop
;
1656 /* Check if we have a FIB route for the destination, otherwise,
1657 * don't redistribute it */
1658 for (ALL_NEXTHOPS_RO(new_fib
? new_fib
->nexthop
: NULL
, nexthop
,
1659 tnexthop
, recursing
))
1661 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1667 new_selected
= NULL
;
1669 if (new_selected
&& new_selected
!= new_fib
)
1671 nexthop_active_update(rn
, new_selected
, 1);
1672 UNSET_FLAG(new_selected
->status
, RIB_ENTRY_CHANGED
);
1678 redistribute_delete(p
, src_p
, old_selected
);
1679 if (old_selected
!= new_selected
)
1680 UNSET_FLAG (old_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1685 /* Install new or replace existing redistributed entry */
1686 SET_FLAG (new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1687 redistribute_update (p
, src_p
, new_selected
, old_selected
);
1691 /* Remove all RIB entries queued for removal */
1692 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1694 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1696 if (IS_ZEBRA_DEBUG_RIB
)
1698 rnode_debug (rn
, vrf_id
, "rn %p, removing rib %p",
1699 (void *)rn
, (void *)rib
);
1701 rib_unlink(rn
, rib
);
1706 * Check if the dest can be deleted now.
1711 /* Take a list of route_node structs and return 1, if there was a record
1712 * picked from it and processed by rib_process(). Don't process more,
1713 * than one RN record; operate only in the specified sub-queue.
1716 process_subq (struct list
* subq
, u_char qindex
)
1718 struct listnode
*lnode
= listhead (subq
);
1719 struct route_node
*rnode
;
1721 struct zebra_vrf
*zvrf
= NULL
;
1726 rnode
= listgetdata (lnode
);
1727 dest
= rib_dest_from_rnode (rnode
);
1729 zvrf
= rib_dest_vrf (dest
);
1731 rib_process (rnode
);
1733 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1735 char buf
[SRCDEST2STR_BUFFER
];
1736 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
1737 zlog_debug ("%u:%s: rn %p dequeued from sub-queue %u",
1738 zvrf
? zvrf_id (zvrf
) : 0, buf
, rnode
, qindex
);
1742 UNSET_FLAG (rib_dest_from_rnode (rnode
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1747 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1748 __func__
, rnode
, rnode
->lock
);
1749 zlog_backtrace(LOG_DEBUG
);
1752 route_unlock_node (rnode
);
1753 list_delete_node (subq
, lnode
);
1758 * All meta queues have been processed. Trigger next-hop evaluation.
1761 meta_queue_process_complete (struct work_queue
*dummy
)
1764 struct zebra_vrf
*zvrf
;
1766 /* Evaluate nexthops for those VRFs which underwent route processing. This
1767 * should limit the evaluation to the necessary VRFs in most common
1770 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1773 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
1776 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
1777 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET
, 0, RNH_NEXTHOP_TYPE
, NULL
);
1778 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
1779 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
1780 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET6
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
1783 /* Schedule LSPs for processing, if needed. */
1784 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1785 if (mpls_should_lsps_be_processed(zvrf
))
1787 if (IS_ZEBRA_DEBUG_MPLS
)
1788 zlog_debug ("%u: Scheduling all LSPs upon RIB completion", zvrf_id (zvrf
));
1789 zebra_mpls_lsp_schedule (zvrf
);
1790 mpls_unmark_lsps_for_processing(zvrf
);
1794 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1795 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1796 * is pointed to the meta queue structure.
1798 static wq_item_status
1799 meta_queue_process (struct work_queue
*dummy
, void *data
)
1801 struct meta_queue
* mq
= data
;
1804 for (i
= 0; i
< MQ_SIZE
; i
++)
1805 if (process_subq (mq
->subq
[i
], i
))
1810 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1814 * Map from rib types to queue type (priority) in meta queue
1816 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1817 [ZEBRA_ROUTE_SYSTEM
] = 4,
1818 [ZEBRA_ROUTE_KERNEL
] = 0,
1819 [ZEBRA_ROUTE_CONNECT
] = 0,
1820 [ZEBRA_ROUTE_STATIC
] = 1,
1821 [ZEBRA_ROUTE_RIP
] = 2,
1822 [ZEBRA_ROUTE_RIPNG
] = 2,
1823 [ZEBRA_ROUTE_OSPF
] = 2,
1824 [ZEBRA_ROUTE_OSPF6
] = 2,
1825 [ZEBRA_ROUTE_ISIS
] = 2,
1826 [ZEBRA_ROUTE_NHRP
] = 2,
1827 [ZEBRA_ROUTE_BGP
] = 3,
1828 [ZEBRA_ROUTE_HSLS
] = 4,
1829 [ZEBRA_ROUTE_TABLE
] = 1,
1832 /* Look into the RN and queue it into one or more priority queues,
1833 * increasing the size for each data push done.
1836 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
1840 RNODE_FOREACH_RIB (rn
, rib
)
1842 u_char qindex
= meta_queue_map
[rib
->type
];
1843 struct zebra_vrf
*zvrf
;
1845 /* Invariant: at this point we always have rn->info set. */
1846 if (CHECK_FLAG (rib_dest_from_rnode (rn
)->flags
,
1847 RIB_ROUTE_QUEUED (qindex
)))
1849 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1850 rnode_debug (rn
, rib
->vrf_id
, "rn %p is already queued in sub-queue %u",
1851 (void *)rn
, qindex
);
1855 SET_FLAG (rib_dest_from_rnode (rn
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1856 listnode_add (mq
->subq
[qindex
], rn
);
1857 route_lock_node (rn
);
1860 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1861 rnode_debug (rn
, rib
->vrf_id
, "queued rn %p into sub-queue %u",
1862 (void *)rn
, qindex
);
1864 zvrf
= zebra_vrf_lookup_by_id (rib
->vrf_id
);
1866 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1870 /* Add route_node to work queue and schedule processing */
1872 rib_queue_add (struct route_node
*rn
)
1876 /* Pointless to queue a route_node with no RIB entries to add or remove */
1877 if (!rnode_to_ribs (rn
))
1879 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1880 __func__
, (void *)rn
, rn
->lock
);
1881 zlog_backtrace(LOG_DEBUG
);
1885 if (zebrad
.ribq
== NULL
)
1887 zlog_err ("%s: work_queue does not exist!", __func__
);
1892 * The RIB queue should normally be either empty or holding the only
1893 * work_queue_item element. In the latter case this element would
1894 * hold a pointer to the meta queue structure, which must be used to
1895 * actually queue the route nodes to process. So create the MQ
1896 * holder, if necessary, then push the work into it in any case.
1897 * This semantics was introduced after 0.99.9 release.
1899 if (!zebrad
.ribq
->items
->count
)
1900 work_queue_add (zebrad
.ribq
, zebrad
.mq
);
1902 rib_meta_queue_add (zebrad
.mq
, rn
);
1907 /* Create new meta queue.
1908 A destructor function doesn't seem to be necessary here.
1910 static struct meta_queue
*
1911 meta_queue_new (void)
1913 struct meta_queue
*new;
1916 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
1919 for (i
= 0; i
< MQ_SIZE
; i
++)
1921 new->subq
[i
] = list_new ();
1922 assert(new->subq
[i
]);
1929 meta_queue_free (struct meta_queue
*mq
)
1933 for (i
= 0; i
< MQ_SIZE
; i
++)
1934 list_delete (mq
->subq
[i
]);
1936 XFREE (MTYPE_WORK_QUEUE
, mq
);
1939 /* initialise zebra rib work queue */
1941 rib_queue_init (struct zebra_t
*zebra
)
1945 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1946 "route_node processing")))
1948 zlog_err ("%s: could not initialise work queue!", __func__
);
1952 /* fill in the work queue spec */
1953 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1954 zebra
->ribq
->spec
.errorfunc
= NULL
;
1955 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
1956 /* XXX: TODO: These should be runtime configurable via vty */
1957 zebra
->ribq
->spec
.max_retries
= 3;
1958 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
1960 if (!(zebra
->mq
= meta_queue_new ()))
1962 zlog_err ("%s: could not initialise meta queue!", __func__
);
1968 /* RIB updates are processed via a queue of pointers to route_nodes.
1970 * The queue length is bounded by the maximal size of the routing table,
1971 * as a route_node will not be requeued, if already queued.
1973 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
1974 * state, or static_install_route (when an existing RIB is updated)
1975 * and then submit route_node to queue for best-path selection later.
1976 * Order of add/delete state changes are preserved for any given RIB.
1978 * Deleted RIBs are reaped during best-path selection.
1981 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
1982 * |-------->| | best RIB, if required
1984 * static_install->|->rib_addqueue...... -> rib_process
1986 * |-------->| |-> rib_unlink
1987 * |-> set RIB_ENTRY_REMOVE |
1988 * rib_delnode (RIB freed)
1990 * The 'info' pointer of a route_node points to a rib_dest_t
1991 * ('dest'). Queueing state for a route_node is kept on the dest. The
1992 * dest is created on-demand by rib_link() and is kept around at least
1993 * as long as there are ribs hanging off it (@see rib_gc_dest()).
1995 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1997 * - route_nodes: refcounted by:
1998 * - dest attached to route_node:
1999 * - managed by: rib_link/rib_gc_dest
2000 * - route_node processing queue
2001 * - managed by: rib_addqueue, rib_process.
2005 /* Add RIB to head of the route node. */
2007 rib_link (struct route_node
*rn
, struct rib
*rib
, int process
)
2012 const char *rmap_name
;
2016 dest
= rib_dest_from_rnode (rn
);
2019 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2020 rnode_debug (rn
, rib
->vrf_id
, "rn %p adding dest", rn
);
2022 dest
= XCALLOC (MTYPE_RIB_DEST
, sizeof (rib_dest_t
));
2023 route_lock_node (rn
); /* rn route table reference */
2028 head
= dest
->routes
;
2036 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2037 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2038 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2040 rmap_name
= zebra_get_import_table_route_map (afi
, rib
->table
);
2041 zebra_add_import_table_entry(rn
, rib
, rmap_name
);
2049 rib_addnode (struct route_node
*rn
, struct rib
*rib
, int process
)
2051 /* RIB node has been un-removed before route-node is processed.
2052 * route_node must hence already be on the queue for processing..
2054 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2056 if (IS_ZEBRA_DEBUG_RIB
)
2057 rnode_debug (rn
, rib
->vrf_id
, "rn %p, un-removed rib %p", (void *)rn
, (void *)rib
);
2059 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2062 rib_link (rn
, rib
, process
);
2068 * Detach a rib structure from a route_node.
2070 * Note that a call to rib_unlink() should be followed by a call to
2071 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2072 * longer required to be deleted.
2075 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
2081 if (IS_ZEBRA_DEBUG_RIB
)
2082 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p", (void *)rn
, (void *)rib
);
2084 dest
= rib_dest_from_rnode (rn
);
2087 rib
->next
->prev
= rib
->prev
;
2090 rib
->prev
->next
= rib
->next
;
2093 dest
->routes
= rib
->next
;
2096 /* free RIB and nexthops */
2097 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, rib
->nexthop
, rn
);
2098 nexthops_free(rib
->nexthop
);
2099 XFREE (MTYPE_RIB
, rib
);
2104 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
2108 if (IS_ZEBRA_DEBUG_RIB
)
2109 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p, removing", (void *)rn
, (void *)rib
);
2110 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2112 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2113 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2114 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2116 zebra_del_import_table_entry(rn
, rib
);
2117 /* Just clean up if non main table */
2118 if (IS_ZEBRA_DEBUG_RIB
)
2120 char buf
[SRCDEST2STR_BUFFER
];
2121 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2122 zlog_debug ("%u:%s: Freeing route rn %p, rib %p (type %d)",
2123 rib
->vrf_id
, buf
, rn
, rib
, rib
->type
);
2126 rib_unlink(rn
, rib
);
2134 /* This function dumps the contents of a given RIB entry into
2135 * standard debug log. Calling function name and IP prefix in
2136 * question are passed as 1st and 2nd arguments.
2139 void _rib_dump (const char * func
,
2140 union prefixconstptr pp
,
2141 union prefixconstptr src_pp
,
2142 const struct rib
* rib
)
2144 const struct prefix
*p
= pp
.p
;
2145 const struct prefix
*src_p
= src_pp
.p
;
2146 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2147 char straddr
[PREFIX_STRLEN
];
2148 char srcaddr
[PREFIX_STRLEN
];
2149 struct nexthop
*nexthop
, *tnexthop
;
2152 zlog_debug ("%s: dumping RIB entry %p for %s%s%s vrf %u", func
, (const void *)rib
,
2153 prefix2str(pp
, straddr
, sizeof(straddr
)),
2154 is_srcdst
? " from " : "",
2155 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
)) : "",
2159 "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d",
2162 (unsigned long) rib
->uptime
,
2169 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2179 "%s: nexthop_num == %u, nexthop_active_num == %u",
2182 rib
->nexthop_active_num
2185 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2187 inet_ntop (p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2190 "%s: %s %s with flags %s%s%s",
2192 (recursing
? " NH" : "NH"),
2194 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
2195 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
2196 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
2199 zlog_debug ("%s: dump complete", func
);
2202 /* This is an exported helper to rtm_read() to dump the strange
2203 * RIB entry found by rib_lookup_ipv4_route()
2206 void rib_lookup_and_dump (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2208 struct route_table
*table
;
2209 struct route_node
*rn
;
2211 char prefix_buf
[INET_ADDRSTRLEN
];
2214 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2217 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2221 /* Scan the RIB table for exactly matching RIB entry. */
2222 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2224 /* No route for this prefix. */
2227 zlog_debug ("%s: lookup failed for %s", __func__
,
2228 prefix2str((struct prefix
*) p
, prefix_buf
, sizeof(prefix_buf
)));
2233 route_unlock_node (rn
);
2236 RNODE_FOREACH_RIB (rn
, rib
)
2240 "%s: rn %p, rib %p: %s, %s",
2244 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
2245 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
2247 rib_dump (p
, NULL
, rib
);
2251 /* Check if requested address assignment will fail due to another
2252 * route being installed by zebra in FIB already. Take necessary
2253 * actions, if needed: remove such a route from FIB and deSELECT
2254 * corresponding RIB entry. Then put affected RN into RIBQ head.
2256 void rib_lookup_and_pushup (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2258 struct route_table
*table
;
2259 struct route_node
*rn
;
2261 unsigned changed
= 0;
2263 if (NULL
== (table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
)))
2265 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2269 /* No matches would be the simplest case. */
2270 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
2274 route_unlock_node (rn
);
2276 /* Check all RIB entries. In case any changes have to be done, requeue
2277 * the RN into RIBQ head. If the routing message about the new connected
2278 * route (generated by the IP address we are going to assign very soon)
2279 * comes before the RIBQ is processed, the new RIB entry will join
2280 * RIBQ record already on head. This is necessary for proper revalidation
2281 * of the rest of the RIB.
2283 RNODE_FOREACH_RIB (rn
, rib
)
2285 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
) &&
2286 ! RIB_SYSTEM_ROUTE (rib
))
2289 if (IS_ZEBRA_DEBUG_RIB
)
2291 char buf
[PREFIX_STRLEN
];
2292 zlog_debug ("%u:%s: freeing way for connected prefix",
2293 rib
->vrf_id
, prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2294 rib_dump (&rn
->p
, NULL
, rib
);
2296 rib_uninstall (rn
, rib
);
2304 rib_add_multipath (afi_t afi
, safi_t safi
, struct prefix
*p
,
2305 struct prefix_ipv6
*src_p
, struct rib
*rib
)
2307 struct route_table
*table
;
2308 struct route_node
*rn
;
2310 struct nexthop
*nexthop
;
2317 if (p
->family
== AF_INET
)
2322 assert(!src_p
|| family
== AFI_IP6
);
2325 table
= zebra_vrf_table_with_table_id (family
, safi
, rib
->vrf_id
, rib
->table
);
2329 /* Make it sure prefixlen is applied to the prefix. */
2332 apply_mask_ipv6 (src_p
);
2334 /* Set default distance by route type. */
2335 if (rib
->distance
== 0)
2337 rib
->distance
= route_info
[rib
->type
].distance
;
2339 /* iBGP distance is 200. */
2340 if (rib
->type
== ZEBRA_ROUTE_BGP
2341 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
2342 rib
->distance
= 200;
2345 /* Lookup route node.*/
2346 rn
= srcdest_rnode_get (table
, p
, src_p
);
2348 /* If same type of route are installed, treat it as a implicit
2350 RNODE_FOREACH_RIB (rn
, same
)
2352 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
2355 if (same
->type
== rib
->type
&& same
->instance
== rib
->instance
2356 && same
->table
== rib
->table
2357 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
2361 /* If this route is kernel route, set FIB flag to the route. */
2362 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
2363 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2364 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2366 /* Link new rib to node.*/
2367 if (IS_ZEBRA_DEBUG_RIB
)
2369 rnode_debug(rn
, rib
->vrf_id
, "Inserting route rn %p, rib %p (type %d) existing %p",
2370 (void *)rn
, (void *)rib
, rib
->type
, (void *)same
);
2372 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2373 rib_dump (p
, src_p
, rib
);
2375 rib_addnode (rn
, rib
, 1);
2378 /* Free implicit route.*/
2381 rib_delnode (rn
, same
);
2385 route_unlock_node (rn
);
2390 rib_delete (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
, u_short instance
,
2391 int flags
, struct prefix
*p
, struct prefix_ipv6
*src_p
,
2392 union g_addr
*gate
, ifindex_t ifindex
, u_int32_t table_id
)
2394 struct route_table
*table
;
2395 struct route_node
*rn
;
2397 struct rib
*fib
= NULL
;
2398 struct rib
*same
= NULL
;
2399 struct nexthop
*nexthop
, *tnexthop
;
2401 char buf2
[INET6_ADDRSTRLEN
];
2403 assert(!src_p
|| afi
== AFI_IP6
);
2406 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2413 apply_mask_ipv6 (src_p
);
2415 /* Lookup route node. */
2416 rn
= srcdest_rnode_lookup (table
, p
, src_p
);
2419 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2421 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2422 if (src_p
&& src_p
->prefixlen
)
2423 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2427 if (IS_ZEBRA_DEBUG_RIB
)
2428 zlog_debug ("%u:%s%s%s doesn't exist in rib",
2430 (src_buf
[0] != '\0') ? " from " : "",
2435 /* Lookup same type route. */
2436 RNODE_FOREACH_RIB (rn
, rib
)
2438 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2441 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
2444 if (rib
->type
!= type
)
2446 if (rib
->instance
!= instance
)
2448 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2449 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2451 if (nexthop
->ifindex
!= ifindex
)
2456 route_unlock_node (rn
);
2457 route_unlock_node (rn
);
2463 /* Make sure that the route found has the same gateway. */
2471 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2472 if (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
2473 IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
))
2482 /* If same type of route can't be found and this message is from
2486 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
&&
2487 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
))
2489 if (IS_ZEBRA_DEBUG_RIB
)
2491 rnode_debug (rn
, vrf_id
, "rn %p, rib %p (type %d) was deleted from kernel, adding",
2492 rn
, fib
, fib
->type
);
2497 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2498 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2500 UNSET_FLAG (fib
->status
, RIB_ENTRY_SELECTED_FIB
);
2504 /* This means someone else, other than Zebra, has deleted
2505 * a Zebra router from the kernel. We will add it back */
2506 rib_install_kernel(rn
, fib
, NULL
);
2511 if (IS_ZEBRA_DEBUG_RIB
)
2514 rnode_debug(rn
, vrf_id
, "via %s ifindex %d type %d "
2515 "doesn't exist in rib",
2516 inet_ntop (family2afi(afi
), gate
, buf2
, INET_ADDRSTRLEN
), /* FIXME */
2520 rnode_debug (rn
, vrf_id
, "ifindex %d type %d doesn't exist in rib",
2524 route_unlock_node (rn
);
2530 rib_delnode (rn
, same
);
2532 route_unlock_node (rn
);
2539 rib_add (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2540 u_short instance
, int flags
, struct prefix
*p
,
2541 struct prefix_ipv6
*src_p
, union g_addr
*gate
,
2542 union g_addr
*src
, ifindex_t ifindex
,
2543 u_int32_t table_id
, u_int32_t metric
, u_int32_t mtu
,
2547 struct rib
*same
= NULL
;
2548 struct route_table
*table
;
2549 struct route_node
*rn
;
2550 struct nexthop
*nexthop
;
2552 assert(!src_p
|| afi
== AFI_IP6
);
2555 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2559 /* Make sure mask is applied. */
2562 apply_mask_ipv6 (src_p
);
2564 /* Set default distance by route type. */
2567 if ((unsigned)type
>= array_size(route_info
))
2570 distance
= route_info
[type
].distance
;
2572 /* iBGP distance is 200. */
2573 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2577 /* Lookup route node.*/
2578 rn
= srcdest_rnode_get (table
, p
, src_p
);
2580 /* If same type of route are installed, treat it as a implicit
2582 RNODE_FOREACH_RIB (rn
, rib
)
2584 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2587 if (rib
->type
!= type
)
2589 if (rib
->instance
!= instance
)
2591 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2596 /* Duplicate connected route comes in. */
2597 else if ((nexthop
= rib
->nexthop
) &&
2598 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2599 nexthop
->ifindex
== ifindex
&&
2600 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2607 /* Allocate new rib structure. */
2608 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2611 rib
->instance
= instance
;
2612 rib
->distance
= distance
;
2614 rib
->metric
= metric
;
2616 rib
->table
= table_id
;
2617 rib
->vrf_id
= vrf_id
;
2618 rib
->nexthop_num
= 0;
2619 rib
->uptime
= time (NULL
);
2621 /* Nexthop settings. */
2627 rib_nexthop_ipv6_ifindex_add (rib
, &gate
->ipv6
, ifindex
);
2629 rib_nexthop_ipv6_add (rib
, &gate
->ipv6
);
2634 rib_nexthop_ipv4_ifindex_add (rib
, &gate
->ipv4
, &src
->ipv4
, ifindex
);
2636 rib_nexthop_ipv4_add (rib
, &gate
->ipv4
, &src
->ipv4
);
2640 rib_nexthop_ifindex_add (rib
, ifindex
);
2642 /* If this route is kernel route, set FIB flag to the route. */
2643 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2644 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2645 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2647 /* Link new rib to node.*/
2648 if (IS_ZEBRA_DEBUG_RIB
)
2650 rnode_debug (rn
, vrf_id
, "Inserting route rn %p, rib %p (type %d) existing %p",
2651 (void *)rn
, (void *)rib
, rib
->type
, (void *)same
);
2653 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2654 rib_dump (p
, src_p
, rib
);
2656 rib_addnode (rn
, rib
, 1);
2658 /* Free implicit route.*/
2660 rib_delnode (rn
, same
);
2662 route_unlock_node (rn
);
2666 /* Schedule routes of a particular table (address-family) based on event. */
2668 rib_update_table (struct route_table
*table
, rib_update_event_t event
)
2670 struct route_node
*rn
;
2671 struct rib
*rib
, *next
;
2673 /* Walk all routes and queue for processing, if appropriate for
2674 * the trigger event.
2676 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2680 case RIB_UPDATE_IF_CHANGE
:
2681 /* Examine all routes that won't get processed by the protocol or
2682 * triggered by nexthop evaluation (NHT). This would be system,
2683 * kernel and certain static routes. Note that NHT will get
2684 * triggered upon an interface event as connected routes always
2685 * get queued for processing.
2687 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2689 if (rib
->type
== ZEBRA_ROUTE_OSPF
||
2690 rib
->type
== ZEBRA_ROUTE_OSPF6
||
2691 rib
->type
== ZEBRA_ROUTE_BGP
)
2692 continue; /* protocol will handle. */
2693 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
2696 for (nh
= rib
->nexthop
; nh
; nh
= nh
->next
)
2697 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
||
2698 nh
->type
== NEXTHOP_TYPE_IPV6
))
2701 /* If we only have nexthops to a gateway, NHT will
2712 case RIB_UPDATE_RMAP_CHANGE
:
2713 case RIB_UPDATE_OTHER
:
2714 /* Right now, examine all routes. Can restrict to a protocol in
2715 * some cases (TODO).
2717 if (rnode_to_ribs (rn
))
2727 /* RIB update function. */
2729 rib_update (vrf_id_t vrf_id
, rib_update_event_t event
)
2731 struct route_table
*table
;
2733 /* Process routes of interested address-families. */
2734 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2736 rib_update_table (table
, event
);
2738 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2740 rib_update_table (table
, event
);
2743 /* Remove all routes which comes from non main table. */
2745 rib_weed_table (struct route_table
*table
)
2747 struct route_node
*rn
;
2752 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2753 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2755 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2758 if (rib
->table
!= zebrad
.rtm_table_default
&&
2759 rib
->table
!= RT_TABLE_MAIN
)
2760 rib_delnode (rn
, rib
);
2764 /* Delete all routes from non main table. */
2766 rib_weed_tables (void)
2769 struct zebra_vrf
*zvrf
;
2771 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2772 if ((zvrf
= vrf
->info
) != NULL
)
2774 rib_weed_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2775 rib_weed_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2779 /* Delete self installed routes after zebra is relaunched. */
2781 rib_sweep_table (struct route_table
*table
)
2783 struct route_node
*rn
;
2789 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2790 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2792 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2795 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
2796 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
2798 ret
= rib_uninstall_kernel (rn
, rib
);
2800 rib_delnode (rn
, rib
);
2805 /* Sweep all RIB tables. */
2807 rib_sweep_route (void)
2810 struct zebra_vrf
*zvrf
;
2812 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2813 if ((zvrf
= vrf
->info
) != NULL
)
2815 rib_sweep_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2816 rib_sweep_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2820 /* Remove specific by protocol routes from 'table'. */
2821 static unsigned long
2822 rib_score_proto_table (u_char proto
, u_short instance
, struct route_table
*table
)
2824 struct route_node
*rn
;
2827 unsigned long n
= 0;
2830 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2831 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2833 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2835 if (rib
->type
== proto
&& rib
->instance
== instance
)
2837 rib_delnode (rn
, rib
);
2844 /* Remove specific by protocol routes. */
2846 rib_score_proto (u_char proto
, u_short instance
)
2849 struct zebra_vrf
*zvrf
;
2850 unsigned long cnt
= 0;
2852 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2853 if ((zvrf
= vrf
->info
) != NULL
)
2854 cnt
+= rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
2855 +rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2860 /* Close RIB and clean up kernel routes. */
2862 rib_close_table (struct route_table
*table
)
2864 struct route_node
*rn
;
2865 rib_table_info_t
*info
= table
->info
;
2869 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2870 RNODE_FOREACH_RIB (rn
, rib
)
2872 if (!CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
2875 if (info
->safi
== SAFI_UNICAST
)
2876 zfpm_trigger_update (rn
, NULL
);
2878 if (! RIB_SYSTEM_ROUTE (rib
))
2879 rib_uninstall_kernel (rn
, rib
);
2883 /* Routing information base initialize. */
2887 rib_queue_init (&zebrad
);
2893 * Get the first vrf id that is greater than the given vrf id if any.
2895 * Returns TRUE if a vrf id was found, FALSE otherwise.
2898 vrf_id_get_next (vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
2902 vrf
= vrf_lookup_by_id (vrf_id
);
2905 vrf
= RB_NEXT (vrf_id_head
, &vrfs_by_id
, vrf
);
2907 *next_id_p
= vrf
->vrf_id
;
2916 * rib_tables_iter_next
2918 * Returns the next table in the iteration.
2920 struct route_table
*
2921 rib_tables_iter_next (rib_tables_iter_t
*iter
)
2923 struct route_table
*table
;
2926 * Array that helps us go over all AFI/SAFI combinations via one
2933 { AFI_IP
, SAFI_UNICAST
},
2934 { AFI_IP
, SAFI_MULTICAST
},
2935 { AFI_IP6
, SAFI_UNICAST
},
2936 { AFI_IP6
, SAFI_MULTICAST
},
2941 switch (iter
->state
)
2944 case RIB_TABLES_ITER_S_INIT
:
2945 iter
->vrf_id
= VRF_DEFAULT
;
2946 iter
->afi_safi_ix
= -1;
2950 case RIB_TABLES_ITER_S_ITERATING
:
2951 iter
->afi_safi_ix
++;
2955 while (iter
->afi_safi_ix
< (int) ZEBRA_NUM_OF (afi_safis
))
2957 table
= zebra_vrf_table (afi_safis
[iter
->afi_safi_ix
].afi
,
2958 afi_safis
[iter
->afi_safi_ix
].safi
,
2963 iter
->afi_safi_ix
++;
2967 * Found another table in this vrf.
2973 * Done with all tables in the current vrf, go to the next
2976 if (!vrf_id_get_next (iter
->vrf_id
, &iter
->vrf_id
))
2979 iter
->afi_safi_ix
= 0;
2984 case RIB_TABLES_ITER_S_DONE
:
2989 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
2991 iter
->state
= RIB_TABLES_ITER_S_DONE
;