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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #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_rnh.h"
51 #include "zebra/interface.h"
52 #include "zebra/connected.h"
54 DEFINE_HOOK(rib_update
, (struct route_node
*rn
, const char *reason
), (rn
, reason
))
56 /* Should we allow non Quagga processes to delete our routes */
57 extern int allow_delete
;
59 /* Hold time for RIB process, should be very minimal.
60 * it is useful to able to set it otherwise for testing, hence exported
61 * as global here for test-rig code.
63 int rib_process_hold_time
= 10;
65 /* Each route type's string and default distance value. */
70 } route_info
[ZEBRA_ROUTE_MAX
] =
72 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
73 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
74 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
75 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
76 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
77 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
78 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
79 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
80 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
81 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
82 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10},
83 /* no entry/default: 150 */
86 /* RPF lookup behaviour */
87 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
90 static void __attribute__((format (printf
, 5, 6)))
91 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
, int priority
,
92 const char *msgfmt
, ...)
94 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
99 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
104 rib_table_info_t
*info
= srcdest_rnode_table_info (rn
);
105 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
107 if (info
->safi
== SAFI_MULTICAST
)
108 strcat(buf
, " (MRIB)");
112 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
115 zlog (priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
118 #define rnode_debug(node, vrf_id, ...) \
119 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
120 #define rnode_info(node, ...) \
121 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
124 route_distance (int type
)
128 if ((unsigned)type
>= array_size(route_info
))
131 distance
= route_info
[type
].distance
;
137 is_zebra_valid_kernel_table(u_int32_t table_id
)
139 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
))
143 if ((table_id
== RT_TABLE_UNSPEC
) ||
144 (table_id
== RT_TABLE_LOCAL
) ||
145 (table_id
== RT_TABLE_COMPAT
))
153 is_zebra_main_routing_table(u_int32_t table_id
)
155 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
161 zebra_check_addr (struct prefix
*p
)
163 if (p
->family
== AF_INET
)
167 addr
= p
->u
.prefix4
.s_addr
;
170 if (IPV4_NET127 (addr
)
172 || IPV4_LINKLOCAL(addr
))
175 if (p
->family
== AF_INET6
)
177 if (IN6_IS_ADDR_LOOPBACK (&p
->u
.prefix6
))
179 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
185 /* Add nexthop to the end of a rib node's nexthop list */
187 rib_nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
189 nexthop_add(&rib
->nexthop
, nexthop
);
196 * copy_nexthop - copy a nexthop to the rib structure.
199 rib_copy_nexthops (struct rib
*rib
, struct nexthop
*nh
)
201 struct nexthop
*nexthop
;
203 nexthop
= nexthop_new();
204 nexthop
->flags
= nh
->flags
;
205 nexthop
->type
= nh
->type
;
206 nexthop
->ifindex
= nh
->ifindex
;
207 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
208 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
210 nexthop_add_labels (nexthop
, nh
->nh_label_type
, nh
->nh_label
->num_labels
,
211 &nh
->nh_label
->label
[0]);
212 rib_nexthop_add(rib
, nexthop
);
213 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
214 copy_nexthops(&nexthop
->resolved
, nh
->resolved
);
217 /* Delete specified nexthop from the list. */
219 rib_nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
222 nexthop
->next
->prev
= nexthop
->prev
;
224 nexthop
->prev
->next
= nexthop
->next
;
226 rib
->nexthop
= nexthop
->next
;
233 rib_nexthop_ifindex_add (struct rib
*rib
, ifindex_t ifindex
)
235 struct nexthop
*nexthop
;
237 nexthop
= nexthop_new();
238 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
239 nexthop
->ifindex
= ifindex
;
241 rib_nexthop_add (rib
, nexthop
);
247 rib_nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
249 struct nexthop
*nexthop
;
251 nexthop
= nexthop_new();
252 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
253 nexthop
->gate
.ipv4
= *ipv4
;
255 nexthop
->src
.ipv4
= *src
;
257 rib_nexthop_add (rib
, nexthop
);
263 rib_nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
264 struct in_addr
*src
, ifindex_t ifindex
)
266 struct nexthop
*nexthop
;
267 struct interface
*ifp
;
269 nexthop
= nexthop_new();
270 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
271 nexthop
->gate
.ipv4
= *ipv4
;
273 nexthop
->src
.ipv4
= *src
;
274 nexthop
->ifindex
= ifindex
;
275 ifp
= if_lookup_by_index (nexthop
->ifindex
, VRF_DEFAULT
);
276 /*Pending: need to think if null ifp here is ok during bootup?
277 There was a crash because ifp here was coming to be NULL */
279 if (connected_is_unnumbered(ifp
)) {
280 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
283 rib_nexthop_add (rib
, nexthop
);
289 rib_nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
291 struct nexthop
*nexthop
;
293 nexthop
= nexthop_new();
294 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
295 nexthop
->gate
.ipv6
= *ipv6
;
297 rib_nexthop_add (rib
, nexthop
);
303 rib_nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
306 struct nexthop
*nexthop
;
308 nexthop
= nexthop_new();
309 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
310 nexthop
->gate
.ipv6
= *ipv6
;
311 nexthop
->ifindex
= ifindex
;
313 rib_nexthop_add (rib
, nexthop
);
319 rib_nexthop_blackhole_add (struct rib
*rib
)
321 struct nexthop
*nexthop
;
323 nexthop
= nexthop_new();
324 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
325 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
327 rib_nexthop_add (rib
, nexthop
);
332 /* This method checks whether a recursive nexthop has at
333 * least one resolved nexthop in the fib.
336 nexthop_has_fib_child(struct nexthop
*nexthop
)
340 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
343 for (nh
= nexthop
->resolved
; nh
; nh
= nh
->next
)
344 if (CHECK_FLAG (nh
->flags
, NEXTHOP_FLAG_FIB
))
350 /* If force flag is not set, do not modify falgs at all for uninstall
351 the route from FIB. */
353 nexthop_active (afi_t afi
, struct rib
*rib
, struct nexthop
*nexthop
, int set
,
354 struct route_node
*top
)
357 struct route_table
*table
;
358 struct route_node
*rn
;
361 struct nexthop
*newhop
, *tnewhop
;
362 struct nexthop
*resolved_hop
;
364 struct interface
*ifp
;
366 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
) || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
367 nexthop
->ifindex
= 0;
371 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
372 zebra_deregister_rnh_static_nexthops(rib
->vrf_id
, nexthop
->resolved
, top
);
373 nexthops_free(nexthop
->resolved
);
374 nexthop
->resolved
= NULL
;
375 rib
->nexthop_mtu
= 0;
378 /* Skip nexthops that have been filtered out due to route-map */
379 /* The nexthops are specific to this route and so the same */
380 /* nexthop for a different route may not have this flag set */
381 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
385 * Check to see if we should trust the passed in information
386 * for UNNUMBERED interfaces as that we won't find the GW
387 * address in the routing table.
389 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
391 ifp
= if_lookup_by_index (nexthop
->ifindex
, VRF_DEFAULT
);
392 if (ifp
&& connected_is_unnumbered(ifp
))
394 if (if_is_operative(ifp
))
403 /* Make lookup prefix. */
404 memset (&p
, 0, sizeof (struct prefix
));
409 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
410 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
414 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
415 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
418 assert (afi
!= AFI_IP
&& afi
!= AFI_IP6
);
422 table
= zebra_vrf_table (afi
, SAFI_UNICAST
, rib
->vrf_id
);
426 rn
= route_node_match (table
, (struct prefix
*) &p
);
429 route_unlock_node (rn
);
431 /* If lookup self prefix return immediately. */
435 /* Pick up selected route. */
436 /* However, do not resolve over default route unless explicitly allowed. */
437 if (is_default_prefix (&rn
->p
) &&
438 !nh_resolve_via_default (p
.family
))
441 RNODE_FOREACH_RIB (rn
, match
)
443 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
446 /* if the next hop is imported from another table, skip it */
447 if (match
->type
== ZEBRA_ROUTE_TABLE
)
449 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
453 /* If there is no selected route or matched route is EGP, go up
459 } while (rn
&& rn
->info
== NULL
);
461 route_lock_node (rn
);
465 /* If the longest prefix match for the nexthop yields
466 * a blackhole, mark it as inactive. */
467 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
468 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
471 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
473 /* Directly point connected route. */
474 newhop
= match
->nexthop
;
477 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
||
478 nexthop
->type
== NEXTHOP_TYPE_IPV6
)
479 nexthop
->ifindex
= newhop
->ifindex
;
483 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
486 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
487 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
488 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
492 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
493 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
495 resolved_hop
= nexthop_new();
496 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
497 /* If the resolving route specifies a gateway, use it */
498 if (newhop
->type
== NEXTHOP_TYPE_IPV4
499 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
501 resolved_hop
->type
= newhop
->type
;
502 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
506 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
507 resolved_hop
->ifindex
= newhop
->ifindex
;
508 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
509 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
512 if (newhop
->type
== NEXTHOP_TYPE_IPV6
513 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
515 resolved_hop
->type
= newhop
->type
;
516 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
520 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
521 resolved_hop
->ifindex
= newhop
->ifindex
;
525 /* If the resolving route is an interface route,
526 * it means the gateway we are looking up is connected
527 * to that interface. (The actual network is _not_ onlink).
528 * Therefore, the resolved route should have the original
529 * gateway as nexthop as it is directly connected.
531 * On Linux, we have to set the onlink netlink flag because
532 * otherwise, the kernel won't accept the route. */
533 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
535 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
538 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
539 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
541 else if (afi
== AFI_IP6
)
543 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
544 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
546 resolved_hop
->ifindex
= newhop
->ifindex
;
549 nexthop_add(&nexthop
->resolved
, resolved_hop
);
555 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
558 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
559 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
563 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
565 resolved_hop
= nexthop_new();
566 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
567 /* If the resolving route specifies a gateway, use it */
568 if (newhop
->type
== NEXTHOP_TYPE_IPV4
569 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
571 resolved_hop
->type
= newhop
->type
;
572 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
576 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
577 resolved_hop
->ifindex
= newhop
->ifindex
;
578 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
579 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
582 if (newhop
->type
== NEXTHOP_TYPE_IPV6
583 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
585 resolved_hop
->type
= newhop
->type
;
586 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
590 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
591 resolved_hop
->ifindex
= newhop
->ifindex
;
595 /* If the resolving route is an interface route,
596 * it means the gateway we are looking up is connected
597 * to that interface. (The actual network is _not_ onlink).
598 * Therefore, the resolved route should have the original
599 * gateway as nexthop as it is directly connected.
601 * On Linux, we have to set the onlink netlink flag because
602 * otherwise, the kernel won't accept the route.
604 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
606 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
609 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
610 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
612 else if (afi
== AFI_IP6
)
614 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
615 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
617 resolved_hop
->ifindex
= newhop
->ifindex
;
620 nexthop_add(&nexthop
->resolved
, resolved_hop
);
625 rib
->nexthop_mtu
= match
->mtu
;
638 rib_match (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
639 union g_addr
*addr
, struct route_node
**rn_out
)
642 struct route_table
*table
;
643 struct route_node
*rn
;
645 struct nexthop
*newhop
, *tnewhop
;
649 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
653 memset (&p
, 0, sizeof (struct prefix
));
657 p
.u
.prefix4
= addr
->ipv4
;
658 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
662 p
.u
.prefix6
= addr
->ipv6
;
663 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
666 rn
= route_node_match (table
, (struct prefix
*) &p
);
670 route_unlock_node (rn
);
672 /* Pick up selected route. */
673 RNODE_FOREACH_RIB (rn
, match
)
675 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
677 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
681 /* If there is no selected route or matched route is EGP, go up
687 } while (rn
&& rn
->info
== NULL
);
689 route_lock_node (rn
);
693 if (match
->type
!= ZEBRA_ROUTE_CONNECT
)
696 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
697 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
715 rib_match_ipv4_multicast (vrf_id_t vrf_id
, struct in_addr addr
, struct route_node
**rn_out
)
717 struct rib
*rib
= NULL
, *mrib
= NULL
, *urib
= NULL
;
718 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
719 union g_addr gaddr
= { .ipv4
= addr
};
721 switch (ipv4_multicast_mode
)
723 case MCAST_MRIB_ONLY
:
724 return rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, rn_out
);
725 case MCAST_URIB_ONLY
:
726 return rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
727 case MCAST_NO_CONFIG
:
728 case MCAST_MIX_MRIB_FIRST
:
729 rib
= mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
731 rib
= urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
733 case MCAST_MIX_DISTANCE
:
734 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
735 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
737 rib
= urib
->distance
< mrib
->distance
? urib
: mrib
;
743 case MCAST_MIX_PFXLEN
:
744 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
745 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
747 rib
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? urib
: mrib
;
756 *rn_out
= (rib
== mrib
) ? m_rn
: u_rn
;
758 if (IS_ZEBRA_DEBUG_RIB
)
761 inet_ntop (AF_INET
, &addr
, buf
, BUFSIZ
);
763 zlog_debug("%s: %s: found %s, using %s",
765 mrib
? (urib
? "MRIB+URIB" : "MRIB") :
766 urib
? "URIB" : "nothing",
767 rib
== urib
? "URIB" : rib
== mrib
? "MRIB" : "none");
773 multicast_mode_ipv4_set (enum multicast_mode mode
)
775 if (IS_ZEBRA_DEBUG_RIB
)
776 zlog_debug("%s: multicast lookup mode set (%d)", __func__
, mode
);
777 ipv4_multicast_mode
= mode
;
781 multicast_mode_ipv4_get (void)
783 return ipv4_multicast_mode
;
787 rib_lookup_ipv4 (struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
789 struct route_table
*table
;
790 struct route_node
*rn
;
792 struct nexthop
*nexthop
, *tnexthop
;
796 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
800 rn
= route_node_lookup (table
, (struct prefix
*) p
);
802 /* No route for this prefix. */
807 route_unlock_node (rn
);
809 RNODE_FOREACH_RIB (rn
, match
)
811 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
813 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
820 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
823 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
824 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
831 * This clone function, unlike its original rib_lookup_ipv4(), checks
832 * if specified IPv4 route record (prefix/mask -> gate) exists in
833 * the whole RIB and has RIB_ENTRY_SELECTED_FIB set.
837 * 0: exact match found
838 * 1: a match was found with a different gate
839 * 2: connected route found
840 * 3: no matches found
843 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
,
846 struct route_table
*table
;
847 struct route_node
*rn
;
849 struct nexthop
*nexthop
, *tnexthop
;
854 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
856 return ZEBRA_RIB_LOOKUP_ERROR
;
858 /* Scan the RIB table for exactly matching RIB entry. */
859 rn
= route_node_lookup (table
, (struct prefix
*) p
);
861 /* No route for this prefix. */
863 return ZEBRA_RIB_NOTFOUND
;
866 route_unlock_node (rn
);
868 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
869 RNODE_FOREACH_RIB (rn
, match
)
871 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
873 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
877 /* None such found :( */
879 return ZEBRA_RIB_NOTFOUND
;
881 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
882 return ZEBRA_RIB_FOUND_CONNECTED
;
884 /* Ok, we have a cood candidate, let's check it's nexthop list... */
886 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
887 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
890 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip (qgate
))
891 return ZEBRA_RIB_FOUND_EXACT
;
892 if (IS_ZEBRA_DEBUG_RIB
)
894 char gate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
895 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
896 inet_ntop (AF_INET
, &sockunion2ip(qgate
), qgate_buf
, INET_ADDRSTRLEN
);
897 zlog_debug ("%s: qgate == %s, %s == %s", __func__
,
898 qgate_buf
, recursing
? "rgate" : "gate", gate_buf
);
903 return ZEBRA_RIB_FOUND_NOGATE
;
905 return ZEBRA_RIB_NOTFOUND
;
908 #define RIB_SYSTEM_ROUTE(R) \
909 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
911 /* This function verifies reachability of one given nexthop, which can be
912 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
913 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
914 * nexthop->ifindex will be updated appropriately as well.
915 * An existing route map can turn (otherwise active) nexthop into inactive, but
918 * The return value is the final value of 'ACTIVE' flag.
922 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
923 struct nexthop
*nexthop
, int set
)
925 struct interface
*ifp
;
926 route_map_result_t ret
= RMAP_MATCH
;
928 char buf
[SRCDEST2STR_BUFFER
];
929 struct prefix
*p
, *src_p
;
930 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
932 if (rn
->p
.family
== AF_INET
)
934 else if (rn
->p
.family
== AF_INET6
)
938 switch (nexthop
->type
)
940 case NEXTHOP_TYPE_IFINDEX
:
941 ifp
= if_lookup_by_index (nexthop
->ifindex
, rib
->vrf_id
);
942 if (ifp
&& if_is_operative(ifp
))
943 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
945 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
947 case NEXTHOP_TYPE_IPV4
:
948 case NEXTHOP_TYPE_IPV4_IFINDEX
:
950 if (nexthop_active (AFI_IP
, rib
, nexthop
, set
, rn
))
951 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
953 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
955 case NEXTHOP_TYPE_IPV6
:
957 if (nexthop_active (AFI_IP6
, rib
, nexthop
, set
, rn
))
958 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
960 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
962 case NEXTHOP_TYPE_IPV6_IFINDEX
:
963 /* RFC 5549, v4 prefix with v6 NH */
964 if (rn
->p
.family
!= AF_INET
)
966 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
968 ifp
= if_lookup_by_index (nexthop
->ifindex
, rib
->vrf_id
);
969 if (ifp
&& if_is_operative(ifp
))
970 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
972 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
976 if (nexthop_active (AFI_IP6
, rib
, nexthop
, set
, rn
))
977 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
979 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
982 case NEXTHOP_TYPE_BLACKHOLE
:
983 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
988 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
991 /* XXX: What exactly do those checks do? Do we support
992 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
993 if (RIB_SYSTEM_ROUTE(rib
) ||
994 (family
== AFI_IP
&& p
->family
!= AF_INET
) ||
995 (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
996 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
998 /* The original code didn't determine the family correctly
999 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1000 * from the rib_table_info in those cases.
1001 * Possibly it may be better to use only the rib_table_info
1006 rib_table_info_t
*info
;
1008 info
= srcdest_rnode_table_info(rn
);
1012 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
1014 /* It'll get set if required inside */
1015 ret
= zebra_route_map_check(family
, rib
->type
, p
, nexthop
, rib
->vrf_id
,
1017 if (ret
== RMAP_DENYMATCH
)
1019 if (IS_ZEBRA_DEBUG_RIB
)
1021 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1022 zlog_debug("%u:%s: Filtering out with NH out %s due to route map",
1024 ifindex2ifname (nexthop
->ifindex
, rib
->vrf_id
));
1026 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1028 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1031 /* Iterate over all nexthops of the given RIB entry and refresh their
1032 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1033 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1034 * is flagged with RIB_ENTRY_CHANGED. The 4th 'set' argument is
1035 * transparently passed to nexthop_active_check().
1037 * Return value is the new number of active nexthops.
1041 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
1043 struct nexthop
*nexthop
;
1044 union g_addr prev_src
;
1045 unsigned int prev_active
, new_active
, old_num_nh
;
1046 ifindex_t prev_index
;
1047 old_num_nh
= rib
->nexthop_active_num
;
1049 rib
->nexthop_active_num
= 0;
1050 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1052 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1054 /* No protocol daemon provides src and so we're skipping tracking it */
1055 prev_src
= nexthop
->rmap_src
;
1056 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1057 prev_index
= nexthop
->ifindex
;
1058 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
1059 rib
->nexthop_active_num
++;
1060 /* Don't allow src setting on IPv6 addr for now */
1061 if (prev_active
!= new_active
||
1062 prev_index
!= nexthop
->ifindex
||
1063 ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
&&
1064 nexthop
->type
< NEXTHOP_TYPE_IPV6
) &&
1065 prev_src
.ipv4
.s_addr
!= nexthop
->rmap_src
.ipv4
.s_addr
) ||
1066 ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
&&
1067 nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
) &&
1068 !(IPV6_ADDR_SAME (&prev_src
.ipv6
, &nexthop
->rmap_src
.ipv6
))))
1070 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1071 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1075 if (old_num_nh
!= rib
->nexthop_active_num
)
1076 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1078 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_CHANGED
))
1080 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1083 return rib
->nexthop_active_num
;
1087 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1088 * (nexthops) must have a label.
1091 zebra_rib_labeled_unicast (struct rib
*rib
)
1093 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1096 if (rib
->type
!= ZEBRA_ROUTE_BGP
)
1099 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1100 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1106 /* Update flag indicates whether this is a "replace" or not. Currently, this
1107 * is only used for IPv4.
1110 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
, struct rib
*old
)
1113 struct nexthop
*nexthop
, *tnexthop
;
1114 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1116 struct prefix
*p
, *src_p
;
1118 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
1120 if (info
->safi
!= SAFI_UNICAST
)
1122 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1123 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1128 * Make sure we update the FPM any time we send new information to
1131 hook_call(rib_update
, rn
, "installing in kernel");
1132 ret
= kernel_route_rib (p
, src_p
, old
, rib
);
1134 /* If install succeeds, update FIB flag for nexthops. */
1137 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1139 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1142 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1143 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1145 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1152 /* Uninstall the route from kernel. */
1154 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
1157 struct nexthop
*nexthop
, *tnexthop
;
1158 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1160 struct prefix
*p
, *src_p
;
1162 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
1164 if (info
->safi
!= SAFI_UNICAST
)
1166 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1167 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1172 * Make sure we update the FPM any time we send new information to
1175 hook_call(rib_update
, rn
, "uninstalling from kernel");
1176 ret
= kernel_route_rib (p
, src_p
, rib
, NULL
);
1178 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1179 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1184 /* Uninstall the route from kernel. */
1186 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
1188 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1190 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
1192 if (info
->safi
== SAFI_UNICAST
)
1193 hook_call(rib_update
, rn
, "rib_uninstall");
1195 if (! RIB_SYSTEM_ROUTE (rib
))
1196 rib_uninstall_kernel (rn
, rib
);
1198 /* If labeled-unicast route, uninstall transit LSP. */
1199 if (zebra_rib_labeled_unicast (rib
))
1200 zebra_mpls_lsp_uninstall (info
->zvrf
, rn
, rib
);
1202 UNSET_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
);
1205 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1207 struct prefix
*p
, *src_p
;
1208 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
1210 redistribute_delete (p
, src_p
, rib
);
1211 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
1216 * rib_can_delete_dest
1218 * Returns TRUE if the given dest can be deleted from the table.
1221 rib_can_delete_dest (rib_dest_t
*dest
)
1229 * Don't delete the dest if we have to update the FPM about this
1232 if (CHECK_FLAG (dest
->flags
, RIB_DEST_UPDATE_FPM
) ||
1233 CHECK_FLAG (dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1242 * Garbage collect the rib dest corresponding to the given route node
1245 * Returns TRUE if the dest was deleted, FALSE otherwise.
1248 rib_gc_dest (struct route_node
*rn
)
1251 struct zebra_vrf
*zvrf
;
1253 dest
= rib_dest_from_rnode (rn
);
1257 if (!rib_can_delete_dest (dest
))
1260 zvrf
= rib_dest_vrf (dest
);
1261 if (IS_ZEBRA_DEBUG_RIB
)
1262 rnode_debug (rn
, zvrf_id (zvrf
), "removing dest from table");
1265 XFREE (MTYPE_RIB_DEST
, dest
);
1269 * Release the one reference that we keep on the route node.
1271 route_unlock_node (rn
);
1276 rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1279 hook_call(rib_update
, rn
, "new route selected");
1281 /* Update real nexthop. This may actually determine if nexthop is active or not. */
1282 if (!nexthop_active_update (rn
, new, 1))
1284 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1288 SET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1289 if (IS_ZEBRA_DEBUG_RIB
)
1291 char buf
[SRCDEST2STR_BUFFER
];
1292 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1293 zlog_debug ("%u:%s: Adding route rn %p, rib %p (type %d)",
1294 zvrf_id (zvrf
), buf
, rn
, new, new->type
);
1297 /* If labeled-unicast route, install transit LSP. */
1298 if (zebra_rib_labeled_unicast (new))
1299 zebra_mpls_lsp_install (zvrf
, rn
, new);
1301 if (!RIB_SYSTEM_ROUTE (new))
1303 if (rib_install_kernel (rn
, new, NULL
))
1305 char buf
[SRCDEST2STR_BUFFER
];
1306 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1307 zlog_warn ("%u:%s: Route install failed",
1308 zvrf_id (zvrf
), buf
);
1312 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1316 rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1319 hook_call(rib_update
, rn
, "removing existing route");
1321 /* Uninstall from kernel. */
1322 if (IS_ZEBRA_DEBUG_RIB
)
1324 char buf
[SRCDEST2STR_BUFFER
];
1325 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1326 zlog_debug ("%u:%s: Deleting route rn %p, rib %p (type %d)",
1327 zvrf_id (zvrf
), buf
, rn
, old
, old
->type
);
1330 /* If labeled-unicast route, uninstall transit LSP. */
1331 if (zebra_rib_labeled_unicast (old
))
1332 zebra_mpls_lsp_uninstall (zvrf
, rn
, old
);
1334 if (!RIB_SYSTEM_ROUTE (old
))
1335 rib_uninstall_kernel (rn
, old
);
1337 UNSET_FLAG (old
->status
, RIB_ENTRY_SELECTED_FIB
);
1339 /* Update nexthop for route, reset changed flag. */
1340 nexthop_active_update (rn
, old
, 1);
1341 UNSET_FLAG(old
->status
, RIB_ENTRY_CHANGED
);
1345 rib_process_update_fib (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1346 struct rib
*old
, struct rib
*new)
1348 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1354 * We have to install or update if a new route has been selected or
1355 * something has changed.
1358 CHECK_FLAG (new->status
, RIB_ENTRY_CHANGED
))
1360 hook_call(rib_update
, rn
, "updating existing route");
1362 /* Update the nexthop; we could determine here that nexthop is inactive. */
1363 if (nexthop_active_update (rn
, new, 1))
1366 /* If nexthop is active, install the selected route, if appropriate. If
1367 * the install succeeds, cleanup flags for prior route, if different from
1372 if (IS_ZEBRA_DEBUG_RIB
)
1374 char buf
[SRCDEST2STR_BUFFER
];
1375 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1377 zlog_debug ("%u:%s: Updating route rn %p, rib %p (type %d) "
1378 "old %p (type %d)", zvrf_id (zvrf
), buf
,
1379 rn
, new, new->type
, old
, old
->type
);
1381 zlog_debug ("%u:%s: Updating route rn %p, rib %p (type %d)",
1382 zvrf_id (zvrf
), buf
, rn
, new, new->type
);
1385 /* If labeled-unicast route, uninstall transit LSP. */
1386 if (zebra_rib_labeled_unicast (old
))
1387 zebra_mpls_lsp_uninstall (zvrf
, rn
, old
);
1389 /* Non-system route should be installed. */
1390 if (!RIB_SYSTEM_ROUTE (new))
1392 /* If labeled-unicast route, install transit LSP. */
1393 if (zebra_rib_labeled_unicast (new))
1394 zebra_mpls_lsp_install (zvrf
, rn
, new);
1396 if (rib_install_kernel (rn
, new, old
))
1398 char buf
[SRCDEST2STR_BUFFER
];
1399 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1401 zlog_warn ("%u:%s: Route install failed", zvrf_id (zvrf
), buf
);
1405 /* If install succeeded or system route, cleanup flags for prior route. */
1406 if (installed
&& new != old
)
1408 if (RIB_SYSTEM_ROUTE(new))
1410 if (!RIB_SYSTEM_ROUTE (old
))
1411 rib_uninstall_kernel (rn
, old
);
1415 for (nexthop
= old
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1416 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1420 /* Update for redistribution. */
1422 SET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1426 * If nexthop for selected route is not active or install failed, we
1427 * may need to uninstall and delete for redistribution.
1429 if (!nh_active
|| !installed
)
1431 if (IS_ZEBRA_DEBUG_RIB
)
1433 char buf
[SRCDEST2STR_BUFFER
];
1434 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1436 zlog_debug ("%u:%s: Deleting route rn %p, rib %p (type %d) "
1437 "old %p (type %d) - %s", zvrf_id (zvrf
), buf
,
1438 rn
, new, new->type
, old
, old
->type
,
1439 nh_active
? "install failed" : "nexthop inactive");
1441 zlog_debug ("%u:%s: Deleting route rn %p, rib %p (type %d) - %s",
1442 zvrf_id (zvrf
), buf
, rn
, new, new->type
,
1443 nh_active
? "install failed" : "nexthop inactive");
1446 /* If labeled-unicast route, uninstall transit LSP. */
1447 if (zebra_rib_labeled_unicast (old
))
1448 zebra_mpls_lsp_uninstall (zvrf
, rn
, old
);
1450 if (!RIB_SYSTEM_ROUTE (old
))
1451 rib_uninstall_kernel (rn
, old
);
1452 UNSET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1458 * Same route selected; check if in the FIB and if not, re-install. This
1459 * is housekeeping code to deal with race conditions in kernel with linux
1460 * netlink reporting interface up before IPv4 or IPv6 protocol is ready
1463 if (!RIB_SYSTEM_ROUTE (new))
1467 for (ALL_NEXTHOPS_RO(new->nexthop
, nexthop
, tnexthop
, recursing
))
1468 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1474 rib_install_kernel (rn
, new, NULL
);
1478 /* Update prior route. */
1481 UNSET_FLAG (old
->status
, RIB_ENTRY_SELECTED_FIB
);
1483 /* Set real nexthop. */
1484 nexthop_active_update (rn
, old
, 1);
1485 UNSET_FLAG(old
->status
, RIB_ENTRY_CHANGED
);
1488 /* Clear changed flag. */
1489 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1492 /* Check if 'alternate' RIB entry is better than 'current'. */
1494 rib_choose_best (struct rib
*current
, struct rib
*alternate
)
1496 if (current
== NULL
)
1499 /* filter route selection in following order:
1500 * - connected beats other types
1501 * - lower distance beats higher
1502 * - lower metric beats higher for equal distance
1503 * - last, hence oldest, route wins tie break.
1506 /* Connected routes. Pick the last connected
1507 * route of the set of lowest metric connected routes.
1509 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
)
1511 if (current
->type
!= ZEBRA_ROUTE_CONNECT
1512 || alternate
->metric
<= current
->metric
)
1518 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1521 /* higher distance loses */
1522 if (alternate
->distance
< current
->distance
)
1524 if (current
->distance
< alternate
->distance
)
1527 /* metric tie-breaks equal distance */
1528 if (alternate
->metric
<= current
->metric
)
1534 /* Core function for processing routing information base. */
1536 rib_process (struct route_node
*rn
)
1540 struct rib
*old_selected
= NULL
;
1541 struct rib
*new_selected
= NULL
;
1542 struct rib
*old_fib
= NULL
;
1543 struct rib
*new_fib
= NULL
;
1544 struct rib
*best
= NULL
;
1545 char buf
[SRCDEST2STR_BUFFER
];
1547 struct zebra_vrf
*zvrf
= NULL
;
1548 struct prefix
*p
, *src_p
;
1549 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1550 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1554 dest
= rib_dest_from_rnode (rn
);
1557 zvrf
= rib_dest_vrf (dest
);
1558 vrf_id
= zvrf_id (zvrf
);
1561 if (IS_ZEBRA_DEBUG_RIB
)
1562 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1564 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1565 zlog_debug ("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1567 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1569 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1570 zlog_debug ("%u:%s: Examine rib %p (type %d) status %x flags %x "
1571 "dist %d metric %d",
1572 vrf_id
, buf
, rib
, rib
->type
, rib
->status
,
1573 rib
->flags
, rib
->distance
, rib
->metric
);
1575 UNSET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1577 /* Currently selected rib. */
1578 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1580 assert (old_selected
== NULL
);
1583 /* Currently in fib */
1584 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
1586 assert (old_fib
== NULL
);
1590 /* Skip deleted entries from selection */
1591 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1594 /* Skip unreachable nexthop. */
1595 /* This first call to nexthop_active_update is merely to determine if
1596 * there's any change to nexthops associated with this RIB entry. Now,
1597 * rib_process() can be invoked due to an external event such as link
1598 * down or due to next-hop-tracking evaluation. In the latter case,
1599 * a decision has already been made that the NHs have changed. So, no
1600 * need to invoke a potentially expensive call again. Further, since
1601 * the change might be in a recursive NH which is not caught in
1602 * the nexthop_active_update() code. Thus, we might miss changes to
1605 if (!CHECK_FLAG(rib
->status
, RIB_ENTRY_CHANGED
) &&
1606 ! nexthop_active_update (rn
, rib
, 0))
1608 if (rib
->type
== ZEBRA_ROUTE_TABLE
)
1610 /* XXX: HERE BE DRAGONS!!!!!
1611 * In all honesty, I have not yet figured out what this part
1612 * does or why the RIB_ENTRY_CHANGED test above is correct
1613 * or why we need to delete a route here, and also not whether
1614 * this concerns both selected and fib route, or only selected
1616 /* This entry was denied by the 'ip protocol table' route-map, we
1617 * need to delete it */
1618 if (rib
!= old_selected
)
1620 if (IS_ZEBRA_DEBUG_RIB
)
1621 zlog_debug ("%s: %s: imported via import-table but denied "
1622 "by the ip protocol table route-map",
1624 rib_unlink (rn
, rib
);
1627 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1633 /* Infinite distance. */
1634 if (rib
->distance
== DISTANCE_INFINITY
)
1636 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1640 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
))
1642 best
= rib_choose_best(new_fib
, rib
);
1643 if (new_fib
&& best
!= new_fib
)
1644 UNSET_FLAG (new_fib
->status
, RIB_ENTRY_CHANGED
);
1649 best
= rib_choose_best(new_selected
, rib
);
1650 if (new_selected
&& best
!= new_selected
)
1651 UNSET_FLAG (new_selected
->status
, RIB_ENTRY_CHANGED
);
1652 new_selected
= best
;
1655 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1656 } /* RNODE_FOREACH_RIB */
1658 /* If no FIB override route, use the selected route also for FIB */
1659 if (new_fib
== NULL
)
1660 new_fib
= new_selected
;
1662 /* After the cycle is finished, the following pointers will be set:
1663 * old_selected --- RIB entry currently having SELECTED
1664 * new_selected --- RIB entry that is newly SELECTED
1665 * old_fib --- RIB entry currently in kernel FIB
1666 * new_fib --- RIB entry that is newly to be in kernel FIB
1668 * new_selected will get SELECTED flag, and is going to be redistributed
1669 * the zclients. new_fib (which can be new_selected) will be installed in kernel.
1672 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1674 zlog_debug ("%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1676 (void *)old_selected
,
1677 (void *)new_selected
,
1682 /* Buffer RIB_ENTRY_CHANGED here, because it will get cleared if
1683 * fib == selected */
1684 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1687 /* Update fib according to selection results */
1688 if (new_fib
&& old_fib
)
1689 rib_process_update_fib (zvrf
, rn
, old_fib
, new_fib
);
1691 rib_process_add_fib (zvrf
, rn
, new_fib
);
1693 rib_process_del_fib (zvrf
, rn
, old_fib
);
1695 /* Redistribute SELECTED entry */
1696 if (old_selected
!= new_selected
|| selected_changed
)
1698 struct nexthop
*nexthop
, *tnexthop
;
1701 /* Check if we have a FIB route for the destination, otherwise,
1702 * don't redistribute it */
1703 for (ALL_NEXTHOPS_RO(new_fib
? new_fib
->nexthop
: NULL
, nexthop
,
1704 tnexthop
, recursing
))
1706 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1712 new_selected
= NULL
;
1714 if (new_selected
&& new_selected
!= new_fib
)
1716 nexthop_active_update(rn
, new_selected
, 1);
1717 UNSET_FLAG(new_selected
->status
, RIB_ENTRY_CHANGED
);
1723 redistribute_delete(p
, src_p
, old_selected
);
1724 if (old_selected
!= new_selected
)
1725 UNSET_FLAG (old_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1730 /* Install new or replace existing redistributed entry */
1731 SET_FLAG (new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1732 redistribute_update (p
, src_p
, new_selected
, old_selected
);
1736 /* Remove all RIB entries queued for removal */
1737 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1739 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1741 if (IS_ZEBRA_DEBUG_RIB
)
1743 rnode_debug (rn
, vrf_id
, "rn %p, removing rib %p",
1744 (void *)rn
, (void *)rib
);
1746 rib_unlink(rn
, rib
);
1751 * Check if the dest can be deleted now.
1756 /* Take a list of route_node structs and return 1, if there was a record
1757 * picked from it and processed by rib_process(). Don't process more,
1758 * than one RN record; operate only in the specified sub-queue.
1761 process_subq (struct list
* subq
, u_char qindex
)
1763 struct listnode
*lnode
= listhead (subq
);
1764 struct route_node
*rnode
;
1766 struct zebra_vrf
*zvrf
= NULL
;
1771 rnode
= listgetdata (lnode
);
1772 dest
= rib_dest_from_rnode (rnode
);
1774 zvrf
= rib_dest_vrf (dest
);
1776 rib_process (rnode
);
1778 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1780 char buf
[SRCDEST2STR_BUFFER
];
1781 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
1782 zlog_debug ("%u:%s: rn %p dequeued from sub-queue %u",
1783 zvrf
? zvrf_id (zvrf
) : 0, buf
, rnode
, qindex
);
1787 UNSET_FLAG (rib_dest_from_rnode (rnode
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1792 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1793 __func__
, rnode
, rnode
->lock
);
1794 zlog_backtrace(LOG_DEBUG
);
1797 route_unlock_node (rnode
);
1798 list_delete_node (subq
, lnode
);
1803 * All meta queues have been processed. Trigger next-hop evaluation.
1806 meta_queue_process_complete (struct work_queue
*dummy
)
1809 struct zebra_vrf
*zvrf
;
1811 /* Evaluate nexthops for those VRFs which underwent route processing. This
1812 * should limit the evaluation to the necessary VRFs in most common
1815 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1818 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
1821 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
1822 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET
, 0, RNH_NEXTHOP_TYPE
, NULL
);
1823 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
1824 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
1825 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET6
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
1828 /* Schedule LSPs for processing, if needed. */
1829 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1830 if (mpls_should_lsps_be_processed(zvrf
))
1832 if (IS_ZEBRA_DEBUG_MPLS
)
1833 zlog_debug ("%u: Scheduling all LSPs upon RIB completion", zvrf_id (zvrf
));
1834 zebra_mpls_lsp_schedule (zvrf
);
1835 mpls_unmark_lsps_for_processing(zvrf
);
1839 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1840 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1841 * is pointed to the meta queue structure.
1843 static wq_item_status
1844 meta_queue_process (struct work_queue
*dummy
, void *data
)
1846 struct meta_queue
* mq
= data
;
1849 for (i
= 0; i
< MQ_SIZE
; i
++)
1850 if (process_subq (mq
->subq
[i
], i
))
1855 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1859 * Map from rib types to queue type (priority) in meta queue
1861 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1862 [ZEBRA_ROUTE_SYSTEM
] = 4,
1863 [ZEBRA_ROUTE_KERNEL
] = 0,
1864 [ZEBRA_ROUTE_CONNECT
] = 0,
1865 [ZEBRA_ROUTE_STATIC
] = 1,
1866 [ZEBRA_ROUTE_RIP
] = 2,
1867 [ZEBRA_ROUTE_RIPNG
] = 2,
1868 [ZEBRA_ROUTE_OSPF
] = 2,
1869 [ZEBRA_ROUTE_OSPF6
] = 2,
1870 [ZEBRA_ROUTE_ISIS
] = 2,
1871 [ZEBRA_ROUTE_NHRP
] = 2,
1872 [ZEBRA_ROUTE_BGP
] = 3,
1873 [ZEBRA_ROUTE_HSLS
] = 4,
1874 [ZEBRA_ROUTE_TABLE
] = 1,
1877 /* Look into the RN and queue it into one or more priority queues,
1878 * increasing the size for each data push done.
1881 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
1885 RNODE_FOREACH_RIB (rn
, rib
)
1887 u_char qindex
= meta_queue_map
[rib
->type
];
1888 struct zebra_vrf
*zvrf
;
1890 /* Invariant: at this point we always have rn->info set. */
1891 if (CHECK_FLAG (rib_dest_from_rnode (rn
)->flags
,
1892 RIB_ROUTE_QUEUED (qindex
)))
1894 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1895 rnode_debug (rn
, rib
->vrf_id
, "rn %p is already queued in sub-queue %u",
1896 (void *)rn
, qindex
);
1900 SET_FLAG (rib_dest_from_rnode (rn
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1901 listnode_add (mq
->subq
[qindex
], rn
);
1902 route_lock_node (rn
);
1905 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1906 rnode_debug (rn
, rib
->vrf_id
, "queued rn %p into sub-queue %u",
1907 (void *)rn
, qindex
);
1909 zvrf
= zebra_vrf_lookup_by_id (rib
->vrf_id
);
1911 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1915 /* Add route_node to work queue and schedule processing */
1917 rib_queue_add (struct route_node
*rn
)
1921 /* Pointless to queue a route_node with no RIB entries to add or remove */
1922 if (!rnode_to_ribs (rn
))
1924 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1925 __func__
, (void *)rn
, rn
->lock
);
1926 zlog_backtrace(LOG_DEBUG
);
1930 if (zebrad
.ribq
== NULL
)
1932 zlog_err ("%s: work_queue does not exist!", __func__
);
1937 * The RIB queue should normally be either empty or holding the only
1938 * work_queue_item element. In the latter case this element would
1939 * hold a pointer to the meta queue structure, which must be used to
1940 * actually queue the route nodes to process. So create the MQ
1941 * holder, if necessary, then push the work into it in any case.
1942 * This semantics was introduced after 0.99.9 release.
1944 if (!zebrad
.ribq
->items
->count
)
1945 work_queue_add (zebrad
.ribq
, zebrad
.mq
);
1947 rib_meta_queue_add (zebrad
.mq
, rn
);
1952 /* Create new meta queue.
1953 A destructor function doesn't seem to be necessary here.
1955 static struct meta_queue
*
1956 meta_queue_new (void)
1958 struct meta_queue
*new;
1961 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
1964 for (i
= 0; i
< MQ_SIZE
; i
++)
1966 new->subq
[i
] = list_new ();
1967 assert(new->subq
[i
]);
1974 meta_queue_free (struct meta_queue
*mq
)
1978 for (i
= 0; i
< MQ_SIZE
; i
++)
1979 list_delete (mq
->subq
[i
]);
1981 XFREE (MTYPE_WORK_QUEUE
, mq
);
1984 /* initialise zebra rib work queue */
1986 rib_queue_init (struct zebra_t
*zebra
)
1990 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1991 "route_node processing")))
1993 zlog_err ("%s: could not initialise work queue!", __func__
);
1997 /* fill in the work queue spec */
1998 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1999 zebra
->ribq
->spec
.errorfunc
= NULL
;
2000 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
2001 /* XXX: TODO: These should be runtime configurable via vty */
2002 zebra
->ribq
->spec
.max_retries
= 3;
2003 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
2005 if (!(zebra
->mq
= meta_queue_new ()))
2007 zlog_err ("%s: could not initialise meta queue!", __func__
);
2013 /* RIB updates are processed via a queue of pointers to route_nodes.
2015 * The queue length is bounded by the maximal size of the routing table,
2016 * as a route_node will not be requeued, if already queued.
2018 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
2019 * state, or static_install_route (when an existing RIB is updated)
2020 * and then submit route_node to queue for best-path selection later.
2021 * Order of add/delete state changes are preserved for any given RIB.
2023 * Deleted RIBs are reaped during best-path selection.
2026 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
2027 * |-------->| | best RIB, if required
2029 * static_install->|->rib_addqueue...... -> rib_process
2031 * |-------->| |-> rib_unlink
2032 * |-> set RIB_ENTRY_REMOVE |
2033 * rib_delnode (RIB freed)
2035 * The 'info' pointer of a route_node points to a rib_dest_t
2036 * ('dest'). Queueing state for a route_node is kept on the dest. The
2037 * dest is created on-demand by rib_link() and is kept around at least
2038 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2040 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2042 * - route_nodes: refcounted by:
2043 * - dest attached to route_node:
2044 * - managed by: rib_link/rib_gc_dest
2045 * - route_node processing queue
2046 * - managed by: rib_addqueue, rib_process.
2050 /* Add RIB to head of the route node. */
2052 rib_link (struct route_node
*rn
, struct rib
*rib
, int process
)
2057 const char *rmap_name
;
2061 dest
= rib_dest_from_rnode (rn
);
2064 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2065 rnode_debug (rn
, rib
->vrf_id
, "rn %p adding dest", rn
);
2067 dest
= XCALLOC (MTYPE_RIB_DEST
, sizeof (rib_dest_t
));
2068 route_lock_node (rn
); /* rn route table reference */
2073 head
= dest
->routes
;
2081 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2082 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2083 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2085 rmap_name
= zebra_get_import_table_route_map (afi
, rib
->table
);
2086 zebra_add_import_table_entry(rn
, rib
, rmap_name
);
2094 rib_addnode (struct route_node
*rn
, struct rib
*rib
, int process
)
2096 /* RIB node has been un-removed before route-node is processed.
2097 * route_node must hence already be on the queue for processing..
2099 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2101 if (IS_ZEBRA_DEBUG_RIB
)
2102 rnode_debug (rn
, rib
->vrf_id
, "rn %p, un-removed rib %p", (void *)rn
, (void *)rib
);
2104 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2107 rib_link (rn
, rib
, process
);
2113 * Detach a rib structure from a route_node.
2115 * Note that a call to rib_unlink() should be followed by a call to
2116 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2117 * longer required to be deleted.
2120 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
2126 if (IS_ZEBRA_DEBUG_RIB
)
2127 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p", (void *)rn
, (void *)rib
);
2129 dest
= rib_dest_from_rnode (rn
);
2132 rib
->next
->prev
= rib
->prev
;
2135 rib
->prev
->next
= rib
->next
;
2138 dest
->routes
= rib
->next
;
2141 /* free RIB and nexthops */
2142 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, rib
->nexthop
, rn
);
2143 nexthops_free(rib
->nexthop
);
2144 XFREE (MTYPE_RIB
, rib
);
2149 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
2153 if (IS_ZEBRA_DEBUG_RIB
)
2154 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p, removing", (void *)rn
, (void *)rib
);
2155 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2157 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2158 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2159 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2161 zebra_del_import_table_entry(rn
, rib
);
2162 /* Just clean up if non main table */
2163 if (IS_ZEBRA_DEBUG_RIB
)
2165 char buf
[SRCDEST2STR_BUFFER
];
2166 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2167 zlog_debug ("%u:%s: Freeing route rn %p, rib %p (type %d)",
2168 rib
->vrf_id
, buf
, rn
, rib
, rib
->type
);
2171 rib_unlink(rn
, rib
);
2179 /* This function dumps the contents of a given RIB entry into
2180 * standard debug log. Calling function name and IP prefix in
2181 * question are passed as 1st and 2nd arguments.
2184 void _rib_dump (const char * func
,
2185 union prefixconstptr pp
,
2186 union prefixconstptr src_pp
,
2187 const struct rib
* rib
)
2189 const struct prefix
*p
= pp
.p
;
2190 const struct prefix
*src_p
= src_pp
.p
;
2191 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2192 char straddr
[PREFIX_STRLEN
];
2193 char srcaddr
[PREFIX_STRLEN
];
2194 struct nexthop
*nexthop
, *tnexthop
;
2197 zlog_debug ("%s: dumping RIB entry %p for %s%s%s vrf %u", func
, (const void *)rib
,
2198 prefix2str(pp
, straddr
, sizeof(straddr
)),
2199 is_srcdst
? " from " : "",
2200 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
)) : "",
2204 "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d",
2207 (unsigned long) rib
->uptime
,
2214 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2224 "%s: nexthop_num == %u, nexthop_active_num == %u",
2227 rib
->nexthop_active_num
2230 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2232 inet_ntop (p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2235 "%s: %s %s with flags %s%s%s",
2237 (recursing
? " NH" : "NH"),
2239 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
2240 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
2241 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
2244 zlog_debug ("%s: dump complete", func
);
2247 /* This is an exported helper to rtm_read() to dump the strange
2248 * RIB entry found by rib_lookup_ipv4_route()
2251 void rib_lookup_and_dump (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2253 struct route_table
*table
;
2254 struct route_node
*rn
;
2256 char prefix_buf
[INET_ADDRSTRLEN
];
2259 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2262 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2266 /* Scan the RIB table for exactly matching RIB entry. */
2267 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2269 /* No route for this prefix. */
2272 zlog_debug ("%s: lookup failed for %s", __func__
,
2273 prefix2str((struct prefix
*) p
, prefix_buf
, sizeof(prefix_buf
)));
2278 route_unlock_node (rn
);
2281 RNODE_FOREACH_RIB (rn
, rib
)
2285 "%s: rn %p, rib %p: %s, %s",
2289 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
2290 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
2292 rib_dump (p
, NULL
, rib
);
2296 /* Check if requested address assignment will fail due to another
2297 * route being installed by zebra in FIB already. Take necessary
2298 * actions, if needed: remove such a route from FIB and deSELECT
2299 * corresponding RIB entry. Then put affected RN into RIBQ head.
2301 void rib_lookup_and_pushup (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2303 struct route_table
*table
;
2304 struct route_node
*rn
;
2306 unsigned changed
= 0;
2308 if (NULL
== (table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
)))
2310 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2314 /* No matches would be the simplest case. */
2315 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
2319 route_unlock_node (rn
);
2321 /* Check all RIB entries. In case any changes have to be done, requeue
2322 * the RN into RIBQ head. If the routing message about the new connected
2323 * route (generated by the IP address we are going to assign very soon)
2324 * comes before the RIBQ is processed, the new RIB entry will join
2325 * RIBQ record already on head. This is necessary for proper revalidation
2326 * of the rest of the RIB.
2328 RNODE_FOREACH_RIB (rn
, rib
)
2330 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
) &&
2331 ! RIB_SYSTEM_ROUTE (rib
))
2334 if (IS_ZEBRA_DEBUG_RIB
)
2336 char buf
[PREFIX_STRLEN
];
2337 zlog_debug ("%u:%s: freeing way for connected prefix",
2338 rib
->vrf_id
, prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2339 rib_dump (&rn
->p
, NULL
, rib
);
2341 rib_uninstall (rn
, rib
);
2349 rib_add_multipath (afi_t afi
, safi_t safi
, struct prefix
*p
,
2350 struct prefix_ipv6
*src_p
, struct rib
*rib
)
2352 struct route_table
*table
;
2353 struct route_node
*rn
;
2355 struct nexthop
*nexthop
;
2362 if (p
->family
== AF_INET
)
2367 assert(!src_p
|| family
== AFI_IP6
);
2370 table
= zebra_vrf_table_with_table_id (family
, safi
, rib
->vrf_id
, rib
->table
);
2374 /* Make it sure prefixlen is applied to the prefix. */
2377 apply_mask_ipv6 (src_p
);
2379 /* Set default distance by route type. */
2380 if (rib
->distance
== 0)
2382 rib
->distance
= route_info
[rib
->type
].distance
;
2384 /* iBGP distance is 200. */
2385 if (rib
->type
== ZEBRA_ROUTE_BGP
2386 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
2387 rib
->distance
= 200;
2390 /* Lookup route node.*/
2391 rn
= srcdest_rnode_get (table
, p
, src_p
);
2393 /* If same type of route are installed, treat it as a implicit
2395 RNODE_FOREACH_RIB (rn
, same
)
2397 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
2400 if (same
->type
== rib
->type
&& same
->instance
== rib
->instance
2401 && same
->table
== rib
->table
2402 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
2406 /* If this route is kernel route, set FIB flag to the route. */
2407 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
2408 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2409 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2411 /* Link new rib to node.*/
2412 if (IS_ZEBRA_DEBUG_RIB
)
2414 rnode_debug(rn
, rib
->vrf_id
, "Inserting route rn %p, rib %p (type %d) existing %p",
2415 (void *)rn
, (void *)rib
, rib
->type
, (void *)same
);
2417 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2418 rib_dump (p
, src_p
, rib
);
2420 rib_addnode (rn
, rib
, 1);
2423 /* Free implicit route.*/
2426 rib_delnode (rn
, same
);
2430 route_unlock_node (rn
);
2435 rib_delete (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
, u_short instance
,
2436 int flags
, struct prefix
*p
, struct prefix_ipv6
*src_p
,
2437 union g_addr
*gate
, ifindex_t ifindex
, u_int32_t table_id
)
2439 struct route_table
*table
;
2440 struct route_node
*rn
;
2442 struct rib
*fib
= NULL
;
2443 struct rib
*same
= NULL
;
2444 struct nexthop
*nexthop
, *tnexthop
;
2446 char buf2
[INET6_ADDRSTRLEN
];
2448 assert(!src_p
|| afi
== AFI_IP6
);
2451 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2458 apply_mask_ipv6 (src_p
);
2460 /* Lookup route node. */
2461 rn
= srcdest_rnode_lookup (table
, p
, src_p
);
2464 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2466 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2467 if (src_p
&& src_p
->prefixlen
)
2468 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2472 if (IS_ZEBRA_DEBUG_RIB
)
2473 zlog_debug ("%u:%s%s%s doesn't exist in rib",
2475 (src_buf
[0] != '\0') ? " from " : "",
2480 /* Lookup same type route. */
2481 RNODE_FOREACH_RIB (rn
, rib
)
2483 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2486 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
2489 if (rib
->type
!= type
)
2491 if (rib
->instance
!= instance
)
2493 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2494 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2496 if (nexthop
->ifindex
!= ifindex
)
2501 route_unlock_node (rn
);
2502 route_unlock_node (rn
);
2508 /* Make sure that the route found has the same gateway. */
2516 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2517 if (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
2518 IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
))
2527 /* If same type of route can't be found and this message is from
2531 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
&&
2532 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
))
2534 if (IS_ZEBRA_DEBUG_RIB
)
2536 rnode_debug (rn
, vrf_id
, "rn %p, rib %p (type %d) was deleted from kernel, adding",
2537 rn
, fib
, fib
->type
);
2542 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2543 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2545 UNSET_FLAG (fib
->status
, RIB_ENTRY_SELECTED_FIB
);
2549 /* This means someone else, other than Zebra, has deleted
2550 * a Zebra router from the kernel. We will add it back */
2551 rib_install_kernel(rn
, fib
, NULL
);
2556 if (IS_ZEBRA_DEBUG_RIB
)
2559 rnode_debug(rn
, vrf_id
, "via %s ifindex %d type %d "
2560 "doesn't exist in rib",
2561 inet_ntop (family2afi(afi
), gate
, buf2
, INET_ADDRSTRLEN
), /* FIXME */
2565 rnode_debug (rn
, vrf_id
, "ifindex %d type %d doesn't exist in rib",
2569 route_unlock_node (rn
);
2575 rib_delnode (rn
, same
);
2577 route_unlock_node (rn
);
2584 rib_add (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2585 u_short instance
, int flags
, struct prefix
*p
,
2586 struct prefix_ipv6
*src_p
, union g_addr
*gate
,
2587 union g_addr
*src
, ifindex_t ifindex
,
2588 u_int32_t table_id
, u_int32_t metric
, u_int32_t mtu
,
2592 struct rib
*same
= NULL
;
2593 struct route_table
*table
;
2594 struct route_node
*rn
;
2595 struct nexthop
*nexthop
;
2597 assert(!src_p
|| afi
== AFI_IP6
);
2600 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2604 /* Make sure mask is applied. */
2607 apply_mask_ipv6 (src_p
);
2609 /* Set default distance by route type. */
2612 if ((unsigned)type
>= array_size(route_info
))
2615 distance
= route_info
[type
].distance
;
2617 /* iBGP distance is 200. */
2618 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2622 /* Lookup route node.*/
2623 rn
= srcdest_rnode_get (table
, p
, src_p
);
2625 /* If same type of route are installed, treat it as a implicit
2627 RNODE_FOREACH_RIB (rn
, rib
)
2629 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2632 if (rib
->type
!= type
)
2634 if (rib
->instance
!= instance
)
2636 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2641 /* Duplicate connected route comes in. */
2642 else if ((nexthop
= rib
->nexthop
) &&
2643 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2644 nexthop
->ifindex
== ifindex
&&
2645 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2652 /* Allocate new rib structure. */
2653 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2656 rib
->instance
= instance
;
2657 rib
->distance
= distance
;
2659 rib
->metric
= metric
;
2661 rib
->table
= table_id
;
2662 rib
->vrf_id
= vrf_id
;
2663 rib
->nexthop_num
= 0;
2664 rib
->uptime
= time (NULL
);
2666 /* Nexthop settings. */
2672 rib_nexthop_ipv6_ifindex_add (rib
, &gate
->ipv6
, ifindex
);
2674 rib_nexthop_ipv6_add (rib
, &gate
->ipv6
);
2679 rib_nexthop_ipv4_ifindex_add (rib
, &gate
->ipv4
, &src
->ipv4
, ifindex
);
2681 rib_nexthop_ipv4_add (rib
, &gate
->ipv4
, &src
->ipv4
);
2685 rib_nexthop_ifindex_add (rib
, ifindex
);
2687 /* If this route is kernel route, set FIB flag to the route. */
2688 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2689 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2690 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2692 /* Link new rib to node.*/
2693 if (IS_ZEBRA_DEBUG_RIB
)
2695 rnode_debug (rn
, vrf_id
, "Inserting route rn %p, rib %p (type %d) existing %p",
2696 (void *)rn
, (void *)rib
, rib
->type
, (void *)same
);
2698 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2699 rib_dump (p
, src_p
, rib
);
2701 rib_addnode (rn
, rib
, 1);
2703 /* Free implicit route.*/
2705 rib_delnode (rn
, same
);
2707 route_unlock_node (rn
);
2711 /* Schedule routes of a particular table (address-family) based on event. */
2713 rib_update_table (struct route_table
*table
, rib_update_event_t event
)
2715 struct route_node
*rn
;
2716 struct rib
*rib
, *next
;
2718 /* Walk all routes and queue for processing, if appropriate for
2719 * the trigger event.
2721 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2725 case RIB_UPDATE_IF_CHANGE
:
2726 /* Examine all routes that won't get processed by the protocol or
2727 * triggered by nexthop evaluation (NHT). This would be system,
2728 * kernel and certain static routes. Note that NHT will get
2729 * triggered upon an interface event as connected routes always
2730 * get queued for processing.
2732 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2734 if (rib
->type
== ZEBRA_ROUTE_OSPF
||
2735 rib
->type
== ZEBRA_ROUTE_OSPF6
||
2736 rib
->type
== ZEBRA_ROUTE_BGP
)
2737 continue; /* protocol will handle. */
2738 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
2741 for (nh
= rib
->nexthop
; nh
; nh
= nh
->next
)
2742 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
||
2743 nh
->type
== NEXTHOP_TYPE_IPV6
))
2746 /* If we only have nexthops to a gateway, NHT will
2757 case RIB_UPDATE_RMAP_CHANGE
:
2758 case RIB_UPDATE_OTHER
:
2759 /* Right now, examine all routes. Can restrict to a protocol in
2760 * some cases (TODO).
2762 if (rnode_to_ribs (rn
))
2772 /* RIB update function. */
2774 rib_update (vrf_id_t vrf_id
, rib_update_event_t event
)
2776 struct route_table
*table
;
2778 /* Process routes of interested address-families. */
2779 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2781 rib_update_table (table
, event
);
2783 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2785 rib_update_table (table
, event
);
2788 /* Remove all routes which comes from non main table. */
2790 rib_weed_table (struct route_table
*table
)
2792 struct route_node
*rn
;
2797 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2798 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2800 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2803 if (rib
->table
!= zebrad
.rtm_table_default
&&
2804 rib
->table
!= RT_TABLE_MAIN
)
2805 rib_delnode (rn
, rib
);
2809 /* Delete all routes from non main table. */
2811 rib_weed_tables (void)
2814 struct zebra_vrf
*zvrf
;
2816 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2817 if ((zvrf
= vrf
->info
) != NULL
)
2819 rib_weed_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2820 rib_weed_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2824 /* Delete self installed routes after zebra is relaunched. */
2826 rib_sweep_table (struct route_table
*table
)
2828 struct route_node
*rn
;
2834 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2835 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2837 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2840 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
2841 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
2843 ret
= rib_uninstall_kernel (rn
, rib
);
2845 rib_delnode (rn
, rib
);
2850 /* Sweep all RIB tables. */
2852 rib_sweep_route (void)
2855 struct zebra_vrf
*zvrf
;
2857 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2858 if ((zvrf
= vrf
->info
) != NULL
)
2860 rib_sweep_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2861 rib_sweep_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2865 /* Remove specific by protocol routes from 'table'. */
2866 static unsigned long
2867 rib_score_proto_table (u_char proto
, u_short instance
, struct route_table
*table
)
2869 struct route_node
*rn
;
2872 unsigned long n
= 0;
2875 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2876 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2878 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2880 if (rib
->type
== proto
&& rib
->instance
== instance
)
2882 rib_delnode (rn
, rib
);
2889 /* Remove specific by protocol routes. */
2891 rib_score_proto (u_char proto
, u_short instance
)
2894 struct zebra_vrf
*zvrf
;
2895 unsigned long cnt
= 0;
2897 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2898 if ((zvrf
= vrf
->info
) != NULL
)
2899 cnt
+= rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
2900 +rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2905 /* Close RIB and clean up kernel routes. */
2907 rib_close_table (struct route_table
*table
)
2909 struct route_node
*rn
;
2910 rib_table_info_t
*info
= table
->info
;
2914 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2915 RNODE_FOREACH_RIB (rn
, rib
)
2917 if (!CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
2920 if (info
->safi
== SAFI_UNICAST
)
2921 hook_call(rib_update
, rn
, NULL
);
2923 if (! RIB_SYSTEM_ROUTE (rib
))
2924 rib_uninstall_kernel (rn
, rib
);
2928 /* Routing information base initialize. */
2932 rib_queue_init (&zebrad
);
2938 * Get the first vrf id that is greater than the given vrf id if any.
2940 * Returns TRUE if a vrf id was found, FALSE otherwise.
2943 vrf_id_get_next (vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
2947 vrf
= vrf_lookup_by_id (vrf_id
);
2950 vrf
= RB_NEXT (vrf_id_head
, &vrfs_by_id
, vrf
);
2952 *next_id_p
= vrf
->vrf_id
;
2961 * rib_tables_iter_next
2963 * Returns the next table in the iteration.
2965 struct route_table
*
2966 rib_tables_iter_next (rib_tables_iter_t
*iter
)
2968 struct route_table
*table
;
2971 * Array that helps us go over all AFI/SAFI combinations via one
2978 { AFI_IP
, SAFI_UNICAST
},
2979 { AFI_IP
, SAFI_MULTICAST
},
2980 { AFI_IP
, SAFI_LABELED_UNICAST
},
2981 { AFI_IP6
, SAFI_UNICAST
},
2982 { AFI_IP6
, SAFI_MULTICAST
},
2983 { AFI_IP6
, SAFI_LABELED_UNICAST
},
2988 switch (iter
->state
)
2991 case RIB_TABLES_ITER_S_INIT
:
2992 iter
->vrf_id
= VRF_DEFAULT
;
2993 iter
->afi_safi_ix
= -1;
2997 case RIB_TABLES_ITER_S_ITERATING
:
2998 iter
->afi_safi_ix
++;
3002 while (iter
->afi_safi_ix
< (int) ZEBRA_NUM_OF (afi_safis
))
3004 table
= zebra_vrf_table (afi_safis
[iter
->afi_safi_ix
].afi
,
3005 afi_safis
[iter
->afi_safi_ix
].safi
,
3010 iter
->afi_safi_ix
++;
3014 * Found another table in this vrf.
3020 * Done with all tables in the current vrf, go to the next
3023 if (!vrf_id_get_next (iter
->vrf_id
, &iter
->vrf_id
))
3026 iter
->afi_safi_ix
= 0;
3031 case RIB_TABLES_ITER_S_DONE
:
3036 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3038 iter
->state
= RIB_TABLES_ITER_S_DONE
;