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
),
57 /* Should we allow non Quagga processes to delete our routes */
58 extern int allow_delete
;
60 /* Each route type's string and default distance value. */
64 } route_info
[ZEBRA_ROUTE_MAX
] = {
65 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
66 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
67 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
68 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
69 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
70 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
71 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
72 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
73 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
74 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
75 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255},
76 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90},
77 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10},
78 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255},
79 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255},
80 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150},
81 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150},
82 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20},
83 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20},
84 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20},
85 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20},
86 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20},
87 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100},
89 /* no entry/default: 150 */
92 /* RPF lookup behaviour */
93 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
96 static void __attribute__((format(printf
, 5, 6)))
97 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
98 int priority
, const char *msgfmt
, ...)
100 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
104 va_start(ap
, msgfmt
);
105 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
109 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
110 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
112 if (info
->safi
== SAFI_MULTICAST
)
113 strcat(buf
, " (MRIB)");
115 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
118 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
121 #define rnode_debug(node, vrf_id, ...) \
122 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
123 #define rnode_info(node, ...) \
124 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
126 uint8_t route_distance(int type
)
130 if ((unsigned)type
>= array_size(route_info
))
133 distance
= route_info
[type
].distance
;
138 int is_zebra_valid_kernel_table(u_int32_t table_id
)
140 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
))
144 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
145 || (table_id
== RT_TABLE_COMPAT
))
152 int is_zebra_main_routing_table(u_int32_t table_id
)
154 if ((table_id
== RT_TABLE_MAIN
)
155 || (table_id
== zebrad
.rtm_table_default
))
160 int zebra_check_addr(struct prefix
*p
)
162 if (p
->family
== AF_INET
) {
165 addr
= p
->u
.prefix4
.s_addr
;
168 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
169 || IPV4_LINKLOCAL(addr
))
172 if (p
->family
== AF_INET6
) {
173 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
175 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
181 /* Add nexthop to the end of a rib node's nexthop list */
182 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
184 nexthop_add(&re
->nexthop
, nexthop
);
190 * copy_nexthop - copy a nexthop to the rib structure.
192 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
194 assert(!re
->nexthop
);
195 copy_nexthops(&re
->nexthop
, nh
, NULL
);
196 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
200 /* Delete specified nexthop from the list. */
201 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
204 nexthop
->next
->prev
= nexthop
->prev
;
206 nexthop
->prev
->next
= nexthop
->next
;
208 re
->nexthop
= nexthop
->next
;
213 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
216 struct nexthop
*nexthop
;
218 nexthop
= nexthop_new();
219 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
220 nexthop
->ifindex
= ifindex
;
222 route_entry_nexthop_add(re
, nexthop
);
227 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
228 struct in_addr
*ipv4
,
231 struct nexthop
*nexthop
;
233 nexthop
= nexthop_new();
234 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
235 nexthop
->gate
.ipv4
= *ipv4
;
237 nexthop
->src
.ipv4
= *src
;
239 route_entry_nexthop_add(re
, nexthop
);
244 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
245 struct in_addr
*ipv4
,
249 struct nexthop
*nexthop
;
250 struct interface
*ifp
;
252 nexthop
= nexthop_new();
253 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
254 nexthop
->gate
.ipv4
= *ipv4
;
256 nexthop
->src
.ipv4
= *src
;
257 nexthop
->ifindex
= ifindex
;
258 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->vrf_id
);
259 /*Pending: need to think if null ifp here is ok during bootup?
260 There was a crash because ifp here was coming to be NULL */
262 if (connected_is_unnumbered(ifp
)) {
263 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
266 route_entry_nexthop_add(re
, nexthop
);
271 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
272 struct in6_addr
*ipv6
)
274 struct nexthop
*nexthop
;
276 nexthop
= nexthop_new();
277 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
278 nexthop
->gate
.ipv6
= *ipv6
;
280 route_entry_nexthop_add(re
, nexthop
);
285 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
286 struct in6_addr
*ipv6
,
289 struct nexthop
*nexthop
;
291 nexthop
= nexthop_new();
292 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
293 nexthop
->gate
.ipv6
= *ipv6
;
294 nexthop
->ifindex
= ifindex
;
296 route_entry_nexthop_add(re
, nexthop
);
301 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
302 enum blackhole_type bh_type
)
304 struct nexthop
*nexthop
;
306 nexthop
= nexthop_new();
307 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
308 nexthop
->bh_type
= bh_type
;
310 route_entry_nexthop_add(re
, nexthop
);
315 static void nexthop_set_resolved(afi_t afi
, struct nexthop
*newhop
,
316 struct nexthop
*nexthop
)
318 struct nexthop
*resolved_hop
;
320 resolved_hop
= nexthop_new();
321 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
323 switch (newhop
->type
) {
324 case NEXTHOP_TYPE_IPV4
:
325 case NEXTHOP_TYPE_IPV4_IFINDEX
:
326 /* If the resolving route specifies a gateway, use it */
327 resolved_hop
->type
= newhop
->type
;
328 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
330 if (newhop
->ifindex
) {
331 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
332 resolved_hop
->ifindex
= newhop
->ifindex
;
333 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
334 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
337 case NEXTHOP_TYPE_IPV6
:
338 case NEXTHOP_TYPE_IPV6_IFINDEX
:
339 resolved_hop
->type
= newhop
->type
;
340 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
342 if (newhop
->ifindex
) {
343 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
344 resolved_hop
->ifindex
= newhop
->ifindex
;
347 case NEXTHOP_TYPE_IFINDEX
:
348 /* If the resolving route is an interface route,
349 * it means the gateway we are looking up is connected
350 * to that interface. (The actual network is _not_ onlink).
351 * Therefore, the resolved route should have the original
352 * gateway as nexthop as it is directly connected.
354 * On Linux, we have to set the onlink netlink flag because
355 * otherwise, the kernel won't accept the route.
357 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
359 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
360 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
361 } else if (afi
== AFI_IP6
) {
362 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
363 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
365 resolved_hop
->ifindex
= newhop
->ifindex
;
367 case NEXTHOP_TYPE_BLACKHOLE
:
368 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
369 resolved_hop
->bh_type
= nexthop
->bh_type
;
373 resolved_hop
->rparent
= nexthop
;
374 nexthop_add(&nexthop
->resolved
, resolved_hop
);
377 /* If force flag is not set, do not modify falgs at all for uninstall
378 the route from FIB. */
379 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
380 struct nexthop
*nexthop
, int set
,
381 struct route_node
*top
)
384 struct route_table
*table
;
385 struct route_node
*rn
;
386 struct route_entry
*match
;
388 struct nexthop
*newhop
;
389 struct interface
*ifp
;
391 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
392 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
393 nexthop
->ifindex
= 0;
396 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
397 zebra_deregister_rnh_static_nexthops(re
->vrf_id
,
398 nexthop
->resolved
, top
);
399 nexthops_free(nexthop
->resolved
);
400 nexthop
->resolved
= NULL
;
404 /* Skip nexthops that have been filtered out due to route-map */
405 /* The nexthops are specific to this route and so the same */
406 /* nexthop for a different route may not have this flag set */
407 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
411 * Check to see if we should trust the passed in information
412 * for UNNUMBERED interfaces as that we won't find the GW
413 * address in the routing table.
415 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
416 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->vrf_id
);
417 if (ifp
&& connected_is_unnumbered(ifp
)) {
418 if (if_is_operative(ifp
))
426 /* Make lookup prefix. */
427 memset(&p
, 0, sizeof(struct prefix
));
431 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
432 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
436 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
437 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
440 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
444 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, re
->vrf_id
);
448 rn
= route_node_match(table
, (struct prefix
*)&p
);
450 route_unlock_node(rn
);
452 /* Lookup should halt if we've matched against ourselves ('top',
453 * if specified) - i.e., we cannot have a nexthop NH1 is
454 * resolved by a route NH1. The exception is if the route is a
457 if (top
&& rn
== top
)
458 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32)) ||
459 ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128)))
462 /* Pick up selected route. */
463 /* However, do not resolve over default route unless explicitly
465 if (is_default_prefix(&rn
->p
)
466 && !nh_resolve_via_default(p
.family
))
469 RNODE_FOREACH_RE (rn
, match
) {
470 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
))
473 /* if the next hop is imported from another table, skip
475 if (match
->type
== ZEBRA_ROUTE_TABLE
)
477 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
481 /* If there is no selected route or matched route is EGP, go up
486 } while (rn
&& rn
->info
== NULL
);
493 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
494 /* Directly point connected route. */
495 newhop
= match
->nexthop
;
497 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
498 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
499 nexthop
->ifindex
= newhop
->ifindex
;
502 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_INTERNAL
)) {
504 for (ALL_NEXTHOPS(match
->nexthop
, newhop
)) {
505 if (!CHECK_FLAG(newhop
->flags
,
508 if (CHECK_FLAG(newhop
->flags
,
509 NEXTHOP_FLAG_RECURSIVE
))
513 SET_FLAG(nexthop
->flags
,
514 NEXTHOP_FLAG_RECURSIVE
);
516 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
517 nexthop_set_resolved(afi
, newhop
,
523 re
->nexthop_mtu
= match
->mtu
;
525 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
527 for (ALL_NEXTHOPS(match
->nexthop
, newhop
)) {
528 if (!CHECK_FLAG(newhop
->flags
,
533 SET_FLAG(nexthop
->flags
,
534 NEXTHOP_FLAG_RECURSIVE
);
535 nexthop_set_resolved(afi
, newhop
,
541 re
->nexthop_mtu
= match
->mtu
;
550 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
551 union g_addr
*addr
, struct route_node
**rn_out
)
554 struct route_table
*table
;
555 struct route_node
*rn
;
556 struct route_entry
*match
;
557 struct nexthop
*newhop
;
560 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
564 memset(&p
, 0, sizeof(struct prefix
));
567 p
.u
.prefix4
= addr
->ipv4
;
568 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
570 p
.u
.prefix6
= addr
->ipv6
;
571 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
574 rn
= route_node_match(table
, (struct prefix
*)&p
);
577 route_unlock_node(rn
);
579 /* Pick up selected route. */
580 RNODE_FOREACH_RE (rn
, match
) {
581 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
))
583 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
587 /* If there is no selected route or matched route is EGP, go up
592 } while (rn
&& rn
->info
== NULL
);
596 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
598 for (ALL_NEXTHOPS(match
->nexthop
, newhop
))
599 if (CHECK_FLAG(newhop
->flags
,
616 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
618 struct route_node
**rn_out
)
620 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
621 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
622 union g_addr gaddr
= {.ipv4
= addr
};
624 switch (ipv4_multicast_mode
) {
625 case MCAST_MRIB_ONLY
:
626 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
628 case MCAST_URIB_ONLY
:
629 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
630 case MCAST_NO_CONFIG
:
631 case MCAST_MIX_MRIB_FIRST
:
632 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
635 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
638 case MCAST_MIX_DISTANCE
:
639 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
640 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
642 re
= ure
->distance
< mre
->distance
? ure
: mre
;
648 case MCAST_MIX_PFXLEN
:
649 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
650 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
652 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
661 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
663 if (IS_ZEBRA_DEBUG_RIB
) {
665 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
667 zlog_debug("%s: %s: found %s, using %s", __func__
, buf
,
668 mre
? (ure
? "MRIB+URIB" : "MRIB")
669 : ure
? "URIB" : "nothing",
670 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
675 void multicast_mode_ipv4_set(enum multicast_mode mode
)
677 if (IS_ZEBRA_DEBUG_RIB
)
678 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
680 ipv4_multicast_mode
= mode
;
683 enum multicast_mode
multicast_mode_ipv4_get(void)
685 return ipv4_multicast_mode
;
688 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
690 struct route_table
*table
;
691 struct route_node
*rn
;
692 struct route_entry
*match
;
693 struct nexthop
*nexthop
;
696 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
700 rn
= route_node_lookup(table
, (struct prefix
*)p
);
702 /* No route for this prefix. */
707 route_unlock_node(rn
);
709 RNODE_FOREACH_RE (rn
, match
) {
710 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
))
712 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
719 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
722 for (ALL_NEXTHOPS(match
->nexthop
, nexthop
))
723 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
730 * This clone function, unlike its original rib_lookup_ipv4(), checks
731 * if specified IPv4 route record (prefix/mask -> gate) exists in
732 * the whole RIB and has ROUTE_ENTRY_SELECTED_FIB set.
736 * 0: exact match found
737 * 1: a match was found with a different gate
738 * 2: connected route found
739 * 3: no matches found
741 int rib_lookup_ipv4_route(struct prefix_ipv4
*p
, union sockunion
*qgate
,
744 struct route_table
*table
;
745 struct route_node
*rn
;
746 struct route_entry
*match
;
747 struct nexthop
*nexthop
;
751 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
753 return ZEBRA_RIB_LOOKUP_ERROR
;
755 /* Scan the RIB table for exactly matching RIB entry. */
756 rn
= route_node_lookup(table
, (struct prefix
*)p
);
758 /* No route for this prefix. */
760 return ZEBRA_RIB_NOTFOUND
;
763 route_unlock_node(rn
);
765 /* Find out if a "selected" RR for the discovered RIB entry exists ever.
767 RNODE_FOREACH_RE (rn
, match
) {
768 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
))
770 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
774 /* None such found :( */
776 return ZEBRA_RIB_NOTFOUND
;
778 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
779 return ZEBRA_RIB_FOUND_CONNECTED
;
781 /* Ok, we have a cood candidate, let's check it's nexthop list... */
783 for (ALL_NEXTHOPS(match
->nexthop
, nexthop
))
784 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
786 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip(qgate
))
787 return ZEBRA_RIB_FOUND_EXACT
;
788 if (IS_ZEBRA_DEBUG_RIB
) {
789 char gate_buf
[INET_ADDRSTRLEN
],
790 qgate_buf
[INET_ADDRSTRLEN
];
791 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
.s_addr
,
792 gate_buf
, INET_ADDRSTRLEN
);
793 inet_ntop(AF_INET
, &sockunion2ip(qgate
),
794 qgate_buf
, INET_ADDRSTRLEN
);
795 zlog_debug("%s: qgate == %s, %s == %s",
797 nexthop
->rparent
? "rgate" : "gate",
803 return ZEBRA_RIB_FOUND_NOGATE
;
805 return ZEBRA_RIB_NOTFOUND
;
808 #define RIB_SYSTEM_ROUTE(R) \
809 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
811 /* This function verifies reachability of one given nexthop, which can be
812 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
813 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
814 * nexthop->ifindex will be updated appropriately as well.
815 * An existing route map can turn (otherwise active) nexthop into inactive, but
818 * The return value is the final value of 'ACTIVE' flag.
821 static unsigned nexthop_active_check(struct route_node
*rn
,
822 struct route_entry
*re
,
823 struct nexthop
*nexthop
, int set
)
825 struct interface
*ifp
;
826 route_map_result_t ret
= RMAP_MATCH
;
828 char buf
[SRCDEST2STR_BUFFER
];
829 struct prefix
*p
, *src_p
;
830 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
832 if (rn
->p
.family
== AF_INET
)
834 else if (rn
->p
.family
== AF_INET6
)
838 switch (nexthop
->type
) {
839 case NEXTHOP_TYPE_IFINDEX
:
840 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->vrf_id
);
841 if (ifp
&& if_is_operative(ifp
))
842 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
844 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
846 case NEXTHOP_TYPE_IPV4
:
847 case NEXTHOP_TYPE_IPV4_IFINDEX
:
849 if (nexthop_active(AFI_IP
, re
, nexthop
, set
, rn
))
850 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
852 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
854 case NEXTHOP_TYPE_IPV6
:
856 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
857 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
859 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
861 case NEXTHOP_TYPE_IPV6_IFINDEX
:
862 /* RFC 5549, v4 prefix with v6 NH */
863 if (rn
->p
.family
!= AF_INET
)
865 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
866 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->vrf_id
);
867 if (ifp
&& if_is_operative(ifp
))
868 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
870 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
872 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
873 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
875 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
878 case NEXTHOP_TYPE_BLACKHOLE
:
879 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
884 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
887 /* XXX: What exactly do those checks do? Do we support
888 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
889 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
890 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
891 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
893 /* The original code didn't determine the family correctly
894 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
895 * from the rib_table_info in those cases.
896 * Possibly it may be better to use only the rib_table_info
900 rib_table_info_t
*info
;
902 info
= srcdest_rnode_table_info(rn
);
906 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
908 /* It'll get set if required inside */
909 ret
= zebra_route_map_check(family
, re
->type
, p
, nexthop
, re
->vrf_id
,
911 if (ret
== RMAP_DENYMATCH
) {
912 if (IS_ZEBRA_DEBUG_RIB
) {
913 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
915 "%u:%s: Filtering out with NH out %s due to route map",
917 ifindex2ifname(nexthop
->ifindex
, re
->vrf_id
));
919 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
921 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
924 /* Iterate over all nexthops of the given RIB entry and refresh their
925 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
926 * nexthop is found to toggle the ACTIVE flag, the whole re structure
927 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
928 * transparently passed to nexthop_active_check().
930 * Return value is the new number of active nexthops.
933 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
,
936 struct nexthop
*nexthop
;
937 union g_addr prev_src
;
938 unsigned int prev_active
, new_active
, old_num_nh
;
939 ifindex_t prev_index
;
940 old_num_nh
= re
->nexthop_active_num
;
942 re
->nexthop_active_num
= 0;
943 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
945 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
946 /* No protocol daemon provides src and so we're skipping
948 prev_src
= nexthop
->rmap_src
;
949 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
950 prev_index
= nexthop
->ifindex
;
951 if ((new_active
= nexthop_active_check(rn
, re
, nexthop
, set
)))
952 re
->nexthop_active_num
++;
953 /* Don't allow src setting on IPv6 addr for now */
954 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
955 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
956 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
957 && prev_src
.ipv4
.s_addr
958 != nexthop
->rmap_src
.ipv4
.s_addr
)
959 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
960 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
961 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
962 &nexthop
->rmap_src
.ipv6
)))) {
963 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
964 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
968 if (old_num_nh
!= re
->nexthop_active_num
)
969 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
971 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
972 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
975 return re
->nexthop_active_num
;
979 * Is this RIB labeled-unicast? It must be of type BGP and all paths
980 * (nexthops) must have a label.
982 int zebra_rib_labeled_unicast(struct route_entry
*re
)
984 struct nexthop
*nexthop
= NULL
;
986 if (re
->type
!= ZEBRA_ROUTE_BGP
)
989 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
990 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
996 void kernel_route_rib_pass_fail(struct prefix
*p
, struct route_entry
*re
,
997 enum southbound_results res
)
999 struct nexthop
*nexthop
;
1000 char buf
[PREFIX_STRLEN
];
1003 case SOUTHBOUND_INSTALL_SUCCESS
:
1004 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1005 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1008 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1009 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1011 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1013 zsend_route_notify_owner(re
->type
, re
->instance
, re
->vrf_id
,
1014 p
, ZAPI_ROUTE_INSTALLED
);
1016 case SOUTHBOUND_INSTALL_FAILURE
:
1017 zsend_route_notify_owner(re
->type
, re
->instance
, re
->vrf_id
,
1018 p
, ZAPI_ROUTE_FAIL_INSTALL
);
1019 zlog_warn("%u:%s: Route install failed", re
->vrf_id
,
1020 prefix2str(p
, buf
, sizeof(buf
)));
1022 case SOUTHBOUND_DELETE_SUCCESS
:
1023 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
1024 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1026 case SOUTHBOUND_DELETE_FAILURE
:
1027 zlog_warn("%u:%s: Route Deletion failure", re
->vrf_id
,
1028 prefix2str(p
, buf
, sizeof(buf
)));
1033 /* Update flag indicates whether this is a "replace" or not. Currently, this
1034 * is only used for IPv4.
1036 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1037 struct route_entry
*old
)
1039 struct nexthop
*nexthop
;
1040 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1041 struct prefix
*p
, *src_p
;
1042 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1044 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1046 if (info
->safi
!= SAFI_UNICAST
) {
1047 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
1048 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1051 struct nexthop
*prev
;
1053 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1054 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1055 for (ALL_NEXTHOPS(re
->nexthop
, prev
)) {
1056 if (prev
== nexthop
)
1058 if (nexthop_same_firsthop (nexthop
, prev
))
1060 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1068 * If this is a replace to a new RE let the originator of the RE
1069 * know that they've lost
1071 if (old
&& old
!= re
)
1072 zsend_route_notify_owner(old
->type
, old
->instance
,
1074 ZAPI_ROUTE_BETTER_ADMIN_WON
);
1077 * Make sure we update the FPM any time we send new information to
1080 hook_call(rib_update
, rn
, "installing in kernel");
1081 kernel_route_rib(p
, src_p
, old
, re
);
1087 /* Uninstall the route from kernel. */
1088 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1090 struct nexthop
*nexthop
;
1091 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1092 struct prefix
*p
, *src_p
;
1093 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1095 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1097 if (info
->safi
!= SAFI_UNICAST
) {
1098 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
1099 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1104 * Make sure we update the FPM any time we send new information to
1107 hook_call(rib_update
, rn
, "uninstalling from kernel");
1108 kernel_route_rib(p
, src_p
, re
, NULL
);
1114 /* Uninstall the route from kernel. */
1115 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1117 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1119 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
)) {
1120 if (info
->safi
== SAFI_UNICAST
)
1121 hook_call(rib_update
, rn
, "rib_uninstall");
1123 if (!RIB_SYSTEM_ROUTE(re
))
1124 rib_uninstall_kernel(rn
, re
);
1126 /* If labeled-unicast route, uninstall transit LSP. */
1127 if (zebra_rib_labeled_unicast(re
))
1128 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1130 UNSET_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
);
1133 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1134 struct prefix
*p
, *src_p
;
1135 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1137 redistribute_delete(p
, src_p
, re
);
1138 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1143 * rib_can_delete_dest
1145 * Returns TRUE if the given dest can be deleted from the table.
1147 static int rib_can_delete_dest(rib_dest_t
*dest
)
1154 * Don't delete the dest if we have to update the FPM about this
1157 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1158 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1167 * Garbage collect the rib dest corresponding to the given route node
1170 * Returns TRUE if the dest was deleted, FALSE otherwise.
1172 int rib_gc_dest(struct route_node
*rn
)
1176 dest
= rib_dest_from_rnode(rn
);
1180 if (!rib_can_delete_dest(dest
))
1183 if (IS_ZEBRA_DEBUG_RIB
) {
1184 struct zebra_vrf
*zvrf
;
1186 zvrf
= rib_dest_vrf(dest
);
1187 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1191 XFREE(MTYPE_RIB_DEST
, dest
);
1195 * Release the one reference that we keep on the route node.
1197 route_unlock_node(rn
);
1201 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1202 struct route_entry
*new)
1204 hook_call(rib_update
, rn
, "new route selected");
1206 /* Update real nexthop. This may actually determine if nexthop is active
1208 if (!nexthop_active_update(rn
, new, 1)) {
1209 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1213 SET_FLAG(new->status
, ROUTE_ENTRY_SELECTED_FIB
);
1214 if (IS_ZEBRA_DEBUG_RIB
) {
1215 char buf
[SRCDEST2STR_BUFFER
];
1216 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1217 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1218 zvrf_id(zvrf
), buf
, rn
, new, new->type
);
1221 /* If labeled-unicast route, install transit LSP. */
1222 if (zebra_rib_labeled_unicast(new))
1223 zebra_mpls_lsp_install(zvrf
, rn
, new);
1225 if (!RIB_SYSTEM_ROUTE(new))
1226 rib_install_kernel(rn
, new, NULL
);
1228 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1231 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1232 struct route_entry
*old
)
1234 hook_call(rib_update
, rn
, "removing existing route");
1236 /* Uninstall from kernel. */
1237 if (IS_ZEBRA_DEBUG_RIB
) {
1238 char buf
[SRCDEST2STR_BUFFER
];
1239 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1240 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1241 zvrf_id(zvrf
), buf
, rn
, old
, old
->type
);
1244 /* If labeled-unicast route, uninstall transit LSP. */
1245 if (zebra_rib_labeled_unicast(old
))
1246 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1248 if (!RIB_SYSTEM_ROUTE(old
))
1249 rib_uninstall_kernel(rn
, old
);
1251 UNSET_FLAG(old
->status
, ROUTE_ENTRY_SELECTED_FIB
);
1253 /* Update nexthop for route, reset changed flag. */
1254 nexthop_active_update(rn
, old
, 1);
1255 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1258 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1259 struct route_node
*rn
,
1260 struct route_entry
*old
,
1261 struct route_entry
*new)
1263 struct nexthop
*nexthop
= NULL
;
1268 * We have to install or update if a new route has been selected or
1269 * something has changed.
1271 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1272 hook_call(rib_update
, rn
, "updating existing route");
1274 /* Update the nexthop; we could determine here that nexthop is
1276 if (nexthop_active_update(rn
, new, 1))
1279 /* If nexthop is active, install the selected route, if
1281 * the install succeeds, cleanup flags for prior route, if
1286 if (IS_ZEBRA_DEBUG_RIB
) {
1287 char buf
[SRCDEST2STR_BUFFER
];
1288 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1291 "%u:%s: Updating route rn %p, re %p (type %d) "
1293 zvrf_id(zvrf
), buf
, rn
, new,
1294 new->type
, old
, old
->type
);
1297 "%u:%s: Updating route rn %p, re %p (type %d)",
1298 zvrf_id(zvrf
), buf
, rn
, new,
1302 /* If labeled-unicast route, uninstall transit LSP. */
1303 if (zebra_rib_labeled_unicast(old
))
1304 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1306 /* Non-system route should be installed. */
1307 if (!RIB_SYSTEM_ROUTE(new)) {
1308 /* If labeled-unicast route, install transit
1310 if (zebra_rib_labeled_unicast(new))
1311 zebra_mpls_lsp_install(zvrf
, rn
, new);
1313 rib_install_kernel(rn
, new, old
);
1316 /* If install succeeded or system route, cleanup flags
1317 * for prior route. */
1318 if (installed
&& new != old
) {
1319 if (RIB_SYSTEM_ROUTE(new)) {
1320 if (!RIB_SYSTEM_ROUTE(old
))
1321 rib_uninstall_kernel(rn
, old
);
1323 for (nexthop
= old
->nexthop
; nexthop
;
1324 nexthop
= nexthop
->next
)
1325 UNSET_FLAG(nexthop
->flags
,
1330 /* Update for redistribution. */
1332 SET_FLAG(new->status
, ROUTE_ENTRY_SELECTED_FIB
);
1336 * If nexthop for selected route is not active or install
1338 * may need to uninstall and delete for redistribution.
1340 if (!nh_active
|| !installed
) {
1341 if (IS_ZEBRA_DEBUG_RIB
) {
1342 char buf
[SRCDEST2STR_BUFFER
];
1343 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1346 "%u:%s: Deleting route rn %p, re %p (type %d) "
1347 "old %p (type %d) - %s",
1348 zvrf_id(zvrf
), buf
, rn
, new,
1349 new->type
, old
, old
->type
,
1350 nh_active
? "install failed"
1351 : "nexthop inactive");
1354 "%u:%s: Deleting route rn %p, re %p (type %d) - %s",
1355 zvrf_id(zvrf
), buf
, rn
, new,
1357 nh_active
? "install failed"
1358 : "nexthop inactive");
1361 /* If labeled-unicast route, uninstall transit LSP. */
1362 if (zebra_rib_labeled_unicast(old
))
1363 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1365 if (!RIB_SYSTEM_ROUTE(old
))
1366 rib_uninstall_kernel(rn
, old
);
1367 UNSET_FLAG(new->status
, ROUTE_ENTRY_SELECTED_FIB
);
1371 * Same route selected; check if in the FIB and if not,
1373 * is housekeeping code to deal with race conditions in kernel
1375 * netlink reporting interface up before IPv4 or IPv6 protocol
1379 if (!RIB_SYSTEM_ROUTE(new)) {
1382 for (ALL_NEXTHOPS(new->nexthop
, nexthop
))
1383 if (CHECK_FLAG(nexthop
->flags
,
1384 NEXTHOP_FLAG_FIB
)) {
1389 rib_install_kernel(rn
, new, NULL
);
1393 /* Update prior route. */
1395 UNSET_FLAG(old
->status
, ROUTE_ENTRY_SELECTED_FIB
);
1397 /* Set real nexthop. */
1398 nexthop_active_update(rn
, old
, 1);
1399 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1402 /* Clear changed flag. */
1403 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1406 /* Check if 'alternate' RIB entry is better than 'current'. */
1407 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1408 struct route_entry
*alternate
)
1410 if (current
== NULL
)
1413 /* filter route selection in following order:
1414 * - connected beats other types
1415 * - lower distance beats higher
1416 * - lower metric beats higher for equal distance
1417 * - last, hence oldest, route wins tie break.
1420 /* Connected routes. Pick the last connected
1421 * route of the set of lowest metric connected routes.
1423 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1424 if (current
->type
!= ZEBRA_ROUTE_CONNECT
1425 || alternate
->metric
<= current
->metric
)
1431 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1434 /* higher distance loses */
1435 if (alternate
->distance
< current
->distance
)
1437 if (current
->distance
< alternate
->distance
)
1440 /* metric tie-breaks equal distance */
1441 if (alternate
->metric
<= current
->metric
)
1447 /* Core function for processing routing information base. */
1448 static void rib_process(struct route_node
*rn
)
1450 struct route_entry
*re
;
1451 struct route_entry
*next
;
1452 struct route_entry
*old_selected
= NULL
;
1453 struct route_entry
*new_selected
= NULL
;
1454 struct route_entry
*old_fib
= NULL
;
1455 struct route_entry
*new_fib
= NULL
;
1456 struct route_entry
*best
= NULL
;
1457 char buf
[SRCDEST2STR_BUFFER
];
1459 struct zebra_vrf
*zvrf
= NULL
;
1460 struct prefix
*p
, *src_p
;
1461 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1462 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1466 dest
= rib_dest_from_rnode(rn
);
1468 zvrf
= rib_dest_vrf(dest
);
1469 vrf_id
= zvrf_id(zvrf
);
1472 if (IS_ZEBRA_DEBUG_RIB
)
1473 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1475 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1476 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1478 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1479 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1481 "%u:%s: Examine re %p (type %d) status %x flags %x "
1482 "dist %d metric %d",
1483 vrf_id
, buf
, re
, re
->type
, re
->status
,
1484 re
->flags
, re
->distance
, re
->metric
);
1486 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1488 /* Currently selected re. */
1489 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1490 assert(old_selected
== NULL
);
1493 /* Currently in fib */
1494 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
)) {
1495 assert(old_fib
== NULL
);
1499 /* Skip deleted entries from selection */
1500 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1503 /* Skip unreachable nexthop. */
1504 /* This first call to nexthop_active_update is merely to
1506 * there's any change to nexthops associated with this RIB
1508 * rib_process() can be invoked due to an external event such as
1510 * down or due to next-hop-tracking evaluation. In the latter
1512 * a decision has already been made that the NHs have changed.
1514 * need to invoke a potentially expensive call again. Further,
1516 * the change might be in a recursive NH which is not caught in
1517 * the nexthop_active_update() code. Thus, we might miss changes
1521 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1522 && !nexthop_active_update(rn
, re
, 0)) {
1523 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1524 /* XXX: HERE BE DRAGONS!!!!!
1525 * In all honesty, I have not yet figured out
1527 * does or why the ROUTE_ENTRY_CHANGED test
1529 * or why we need to delete a route here, and
1531 * this concerns both selected and fib route, or
1534 /* This entry was denied by the 'ip protocol
1535 * table' route-map, we
1536 * need to delete it */
1537 if (re
!= old_selected
) {
1538 if (IS_ZEBRA_DEBUG_RIB
)
1540 "%s: %s: imported via import-table but denied "
1541 "by the ip protocol table route-map",
1545 SET_FLAG(re
->status
,
1546 ROUTE_ENTRY_REMOVED
);
1552 /* Infinite distance. */
1553 if (re
->distance
== DISTANCE_INFINITY
) {
1554 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1558 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1559 best
= rib_choose_best(new_fib
, re
);
1560 if (new_fib
&& best
!= new_fib
)
1561 UNSET_FLAG(new_fib
->status
,
1562 ROUTE_ENTRY_CHANGED
);
1565 best
= rib_choose_best(new_selected
, re
);
1566 if (new_selected
&& best
!= new_selected
)
1567 UNSET_FLAG(new_selected
->status
,
1568 ROUTE_ENTRY_CHANGED
);
1569 new_selected
= best
;
1572 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1573 } /* RNODE_FOREACH_RE */
1575 /* If no FIB override route, use the selected route also for FIB */
1576 if (new_fib
== NULL
)
1577 new_fib
= new_selected
;
1579 /* After the cycle is finished, the following pointers will be set:
1580 * old_selected --- RE entry currently having SELECTED
1581 * new_selected --- RE entry that is newly SELECTED
1582 * old_fib --- RE entry currently in kernel FIB
1583 * new_fib --- RE entry that is newly to be in kernel FIB
1585 * new_selected will get SELECTED flag, and is going to be redistributed
1586 * the zclients. new_fib (which can be new_selected) will be installed
1590 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1592 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1593 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1594 (void *)old_fib
, (void *)new_fib
);
1597 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1598 * fib == selected */
1599 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1600 ROUTE_ENTRY_CHANGED
);
1602 /* Update fib according to selection results */
1603 if (new_fib
&& old_fib
)
1604 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1606 rib_process_add_fib(zvrf
, rn
, new_fib
);
1608 rib_process_del_fib(zvrf
, rn
, old_fib
);
1610 /* Redistribute SELECTED entry */
1611 if (old_selected
!= new_selected
|| selected_changed
) {
1612 struct nexthop
*nexthop
;
1614 /* Check if we have a FIB route for the destination, otherwise,
1615 * don't redistribute it */
1616 for (ALL_NEXTHOPS(new_fib
? new_fib
->nexthop
: NULL
, nexthop
)) {
1617 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1622 new_selected
= NULL
;
1624 if (new_selected
&& new_selected
!= new_fib
) {
1625 nexthop_active_update(rn
, new_selected
, 1);
1626 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1631 redistribute_delete(p
, src_p
, old_selected
);
1632 if (old_selected
!= new_selected
)
1633 UNSET_FLAG(old_selected
->flags
,
1634 ZEBRA_FLAG_SELECTED
);
1638 /* Install new or replace existing redistributed entry
1640 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1641 redistribute_update(p
, src_p
, new_selected
,
1646 /* Remove all RE entries queued for removal */
1647 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1648 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1649 if (IS_ZEBRA_DEBUG_RIB
) {
1650 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1651 (void *)rn
, (void *)re
);
1658 * Check if the dest can be deleted now.
1663 /* Take a list of route_node structs and return 1, if there was a record
1664 * picked from it and processed by rib_process(). Don't process more,
1665 * than one RN record; operate only in the specified sub-queue.
1667 static unsigned int process_subq(struct list
*subq
, u_char qindex
)
1669 struct listnode
*lnode
= listhead(subq
);
1670 struct route_node
*rnode
;
1672 struct zebra_vrf
*zvrf
= NULL
;
1677 rnode
= listgetdata(lnode
);
1678 dest
= rib_dest_from_rnode(rnode
);
1680 zvrf
= rib_dest_vrf(dest
);
1684 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1685 char buf
[SRCDEST2STR_BUFFER
];
1686 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
1687 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
1688 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
1692 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
1693 RIB_ROUTE_QUEUED(qindex
));
1698 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1699 __func__
, rnode
, rnode
->lock
);
1700 zlog_backtrace(LOG_DEBUG
);
1703 route_unlock_node(rnode
);
1704 list_delete_node(subq
, lnode
);
1709 * All meta queues have been processed. Trigger next-hop evaluation.
1711 static void meta_queue_process_complete(struct work_queue
*dummy
)
1714 struct zebra_vrf
*zvrf
;
1716 /* Evaluate nexthops for those VRFs which underwent route processing.
1718 * should limit the evaluation to the necessary VRFs in most common
1721 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1723 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
1726 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
1727 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET
, 0, RNH_NEXTHOP_TYPE
,
1729 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET
, 0,
1730 RNH_IMPORT_CHECK_TYPE
, NULL
);
1731 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET6
, 0, RNH_NEXTHOP_TYPE
,
1733 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET6
, 0,
1734 RNH_IMPORT_CHECK_TYPE
, NULL
);
1737 /* Schedule LSPs for processing, if needed. */
1738 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1739 if (mpls_should_lsps_be_processed(zvrf
)) {
1740 if (IS_ZEBRA_DEBUG_MPLS
)
1742 "%u: Scheduling all LSPs upon RIB completion",
1744 zebra_mpls_lsp_schedule(zvrf
);
1745 mpls_unmark_lsps_for_processing(zvrf
);
1749 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1750 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
1752 * is pointed to the meta queue structure.
1754 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
1756 struct meta_queue
*mq
= data
;
1759 for (i
= 0; i
< MQ_SIZE
; i
++)
1760 if (process_subq(mq
->subq
[i
], i
)) {
1764 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1768 * Map from rib types to queue type (priority) in meta queue
1770 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1771 [ZEBRA_ROUTE_SYSTEM
] = 4,
1772 [ZEBRA_ROUTE_KERNEL
] = 0,
1773 [ZEBRA_ROUTE_CONNECT
] = 0,
1774 [ZEBRA_ROUTE_STATIC
] = 1,
1775 [ZEBRA_ROUTE_RIP
] = 2,
1776 [ZEBRA_ROUTE_RIPNG
] = 2,
1777 [ZEBRA_ROUTE_OSPF
] = 2,
1778 [ZEBRA_ROUTE_OSPF6
] = 2,
1779 [ZEBRA_ROUTE_ISIS
] = 2,
1780 [ZEBRA_ROUTE_BGP
] = 3,
1781 [ZEBRA_ROUTE_PIM
] = 4, // Shouldn't happen but for safety
1782 [ZEBRA_ROUTE_EIGRP
] = 2,
1783 [ZEBRA_ROUTE_NHRP
] = 2,
1784 [ZEBRA_ROUTE_HSLS
] = 4,
1785 [ZEBRA_ROUTE_OLSR
] = 4,
1786 [ZEBRA_ROUTE_TABLE
] = 1,
1787 [ZEBRA_ROUTE_LDP
] = 4,
1788 [ZEBRA_ROUTE_VNC
] = 3,
1789 [ZEBRA_ROUTE_VNC_DIRECT
] = 3,
1790 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = 3,
1791 [ZEBRA_ROUTE_BGP_DIRECT
] = 3,
1792 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = 3,
1793 [ZEBRA_ROUTE_BABEL
] = 2,
1794 [ZEBRA_ROUTE_ALL
] = 4, // Shouldn't happen but for safety
1797 /* Look into the RN and queue it into one or more priority queues,
1798 * increasing the size for each data push done.
1800 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
1802 struct route_entry
*re
;
1804 RNODE_FOREACH_RE (rn
, re
) {
1805 u_char qindex
= meta_queue_map
[re
->type
];
1806 struct zebra_vrf
*zvrf
;
1808 /* Invariant: at this point we always have rn->info set. */
1809 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
1810 RIB_ROUTE_QUEUED(qindex
))) {
1811 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1814 "rn %p is already queued in sub-queue %u",
1815 (void *)rn
, qindex
);
1819 SET_FLAG(rib_dest_from_rnode(rn
)->flags
,
1820 RIB_ROUTE_QUEUED(qindex
));
1821 listnode_add(mq
->subq
[qindex
], rn
);
1822 route_lock_node(rn
);
1825 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1826 rnode_debug(rn
, re
->vrf_id
,
1827 "queued rn %p into sub-queue %u",
1828 (void *)rn
, qindex
);
1830 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
1832 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1836 /* Add route_node to work queue and schedule processing */
1837 void rib_queue_add(struct route_node
*rn
)
1841 /* Pointless to queue a route_node with no RIB entries to add or remove
1843 if (!rnode_to_ribs(rn
)) {
1844 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
1845 __func__
, (void *)rn
, rn
->lock
);
1846 zlog_backtrace(LOG_DEBUG
);
1850 if (zebrad
.ribq
== NULL
) {
1851 zlog_err("%s: work_queue does not exist!", __func__
);
1856 * The RIB queue should normally be either empty or holding the only
1857 * work_queue_item element. In the latter case this element would
1858 * hold a pointer to the meta queue structure, which must be used to
1859 * actually queue the route nodes to process. So create the MQ
1860 * holder, if necessary, then push the work into it in any case.
1861 * This semantics was introduced after 0.99.9 release.
1863 if (work_queue_empty(zebrad
.ribq
))
1864 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
1866 rib_meta_queue_add(zebrad
.mq
, rn
);
1871 /* Create new meta queue.
1872 A destructor function doesn't seem to be necessary here.
1874 static struct meta_queue
*meta_queue_new(void)
1876 struct meta_queue
*new;
1879 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
1882 for (i
= 0; i
< MQ_SIZE
; i
++) {
1883 new->subq
[i
] = list_new();
1884 assert(new->subq
[i
]);
1890 void meta_queue_free(struct meta_queue
*mq
)
1894 for (i
= 0; i
< MQ_SIZE
; i
++)
1895 list_delete_and_null(&mq
->subq
[i
]);
1897 XFREE(MTYPE_WORK_QUEUE
, mq
);
1900 /* initialise zebra rib work queue */
1901 static void rib_queue_init(struct zebra_t
*zebra
)
1906 work_queue_new(zebra
->master
, "route_node processing"))) {
1907 zlog_err("%s: could not initialise work queue!", __func__
);
1911 /* fill in the work queue spec */
1912 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1913 zebra
->ribq
->spec
.errorfunc
= NULL
;
1914 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
1915 /* XXX: TODO: These should be runtime configurable via vty */
1916 zebra
->ribq
->spec
.max_retries
= 3;
1917 zebra
->ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
1919 if (!(zebra
->mq
= meta_queue_new())) {
1920 zlog_err("%s: could not initialise meta queue!", __func__
);
1926 /* RIB updates are processed via a queue of pointers to route_nodes.
1928 * The queue length is bounded by the maximal size of the routing table,
1929 * as a route_node will not be requeued, if already queued.
1931 * REs are submitted via rib_addnode or rib_delnode which set minimal
1932 * state, or static_install_route (when an existing RE is updated)
1933 * and then submit route_node to queue for best-path selection later.
1934 * Order of add/delete state changes are preserved for any given RE.
1936 * Deleted REs are reaped during best-path selection.
1939 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
1940 * |-------->| | best RE, if required
1942 * static_install->|->rib_addqueue...... -> rib_process
1944 * |-------->| |-> rib_unlink
1945 * |-> set ROUTE_ENTRY_REMOVE |
1946 * rib_delnode (RE freed)
1948 * The 'info' pointer of a route_node points to a rib_dest_t
1949 * ('dest'). Queueing state for a route_node is kept on the dest. The
1950 * dest is created on-demand by rib_link() and is kept around at least
1951 * as long as there are ribs hanging off it (@see rib_gc_dest()).
1953 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1955 * - route_nodes: refcounted by:
1956 * - dest attached to route_node:
1957 * - managed by: rib_link/rib_gc_dest
1958 * - route_node processing queue
1959 * - managed by: rib_addqueue, rib_process.
1963 /* Add RE to head of the route node. */
1964 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
1966 struct route_entry
*head
;
1969 const char *rmap_name
;
1973 dest
= rib_dest_from_rnode(rn
);
1975 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1976 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
1978 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
1979 route_lock_node(rn
); /* rn route table reference */
1984 head
= dest
->routes
;
1991 afi
= (rn
->p
.family
== AF_INET
)
1993 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
1994 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
1995 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
1996 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2001 void rib_addnode(struct route_node
*rn
, struct route_entry
*re
, int process
)
2003 /* RE node has been un-removed before route-node is processed.
2004 * route_node must hence already be on the queue for processing..
2006 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2007 if (IS_ZEBRA_DEBUG_RIB
)
2008 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2009 (void *)rn
, (void *)re
);
2011 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2014 rib_link(rn
, re
, process
);
2020 * Detach a rib structure from a route_node.
2022 * Note that a call to rib_unlink() should be followed by a call to
2023 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2024 * longer required to be deleted.
2026 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2032 if (IS_ZEBRA_DEBUG_RIB
)
2033 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2036 dest
= rib_dest_from_rnode(rn
);
2039 re
->next
->prev
= re
->prev
;
2042 re
->prev
->next
= re
->next
;
2044 dest
->routes
= re
->next
;
2047 /* free RE and nexthops */
2048 zebra_deregister_rnh_static_nexthops(re
->vrf_id
, re
->nexthop
, rn
);
2049 nexthops_free(re
->nexthop
);
2050 XFREE(MTYPE_RE
, re
);
2053 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2057 if (IS_ZEBRA_DEBUG_RIB
)
2058 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2059 (void *)rn
, (void *)re
);
2060 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2062 afi
= (rn
->p
.family
== AF_INET
)
2064 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2065 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2066 zebra_del_import_table_entry(rn
, re
);
2067 /* Just clean up if non main table */
2068 if (IS_ZEBRA_DEBUG_RIB
) {
2069 char buf
[SRCDEST2STR_BUFFER
];
2070 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2072 "%u:%s: Freeing route rn %p, re %p (type %d)",
2073 re
->vrf_id
, buf
, rn
, re
, re
->type
);
2082 /* This function dumps the contents of a given RE entry into
2083 * standard debug log. Calling function name and IP prefix in
2084 * question are passed as 1st and 2nd arguments.
2087 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2088 union prefixconstptr src_pp
,
2089 const struct route_entry
*re
)
2091 const struct prefix
*p
= pp
.p
;
2092 const struct prefix
*src_p
= src_pp
.p
;
2093 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2094 char straddr
[PREFIX_STRLEN
];
2095 char srcaddr
[PREFIX_STRLEN
];
2096 struct nexthop
*nexthop
;
2098 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2099 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2100 is_srcdst
? " from " : "",
2101 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2104 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2105 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2108 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2109 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2110 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2111 re
->nexthop_num
, re
->nexthop_active_num
);
2113 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
2114 inet_ntop(p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2115 zlog_debug("%s: %s %s[%u] with flags %s%s%s", func
,
2116 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2118 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2121 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
2124 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2128 zlog_debug("%s: dump complete", func
);
2131 /* This is an exported helper to rtm_read() to dump the strange
2132 * RE entry found by rib_lookup_ipv4_route()
2135 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2137 struct route_table
*table
;
2138 struct route_node
*rn
;
2139 struct route_entry
*re
;
2140 char prefix_buf
[INET_ADDRSTRLEN
];
2143 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2145 zlog_err("%s: zebra_vrf_table() returned NULL", __func__
);
2149 /* Scan the RIB table for exactly matching RE entry. */
2150 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2152 /* No route for this prefix. */
2154 zlog_debug("%s: lookup failed for %s", __func__
,
2155 prefix2str((struct prefix
*)p
, prefix_buf
,
2156 sizeof(prefix_buf
)));
2161 route_unlock_node(rn
);
2164 RNODE_FOREACH_RE (rn
, re
) {
2165 zlog_debug("%s: rn %p, re %p: %s, %s", __func__
, (void *)rn
,
2167 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2170 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2173 route_entry_dump(p
, NULL
, re
);
2177 /* Check if requested address assignment will fail due to another
2178 * route being installed by zebra in FIB already. Take necessary
2179 * actions, if needed: remove such a route from FIB and deSELECT
2180 * corresponding RE entry. Then put affected RN into RIBQ head.
2182 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2184 struct route_table
*table
;
2185 struct route_node
*rn
;
2186 struct route_entry
*re
;
2187 unsigned changed
= 0;
2189 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2190 zlog_err("%s: zebra_vrf_table() returned NULL", __func__
);
2194 /* No matches would be the simplest case. */
2195 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2199 route_unlock_node(rn
);
2201 /* Check all RE entries. In case any changes have to be done, requeue
2202 * the RN into RIBQ head. If the routing message about the new connected
2203 * route (generated by the IP address we are going to assign very soon)
2204 * comes before the RIBQ is processed, the new RE entry will join
2205 * RIBQ record already on head. This is necessary for proper
2207 * of the rest of the RE.
2209 RNODE_FOREACH_RE (rn
, re
) {
2210 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
)
2211 && !RIB_SYSTEM_ROUTE(re
)) {
2213 if (IS_ZEBRA_DEBUG_RIB
) {
2214 char buf
[PREFIX_STRLEN
];
2216 "%u:%s: freeing way for connected prefix",
2218 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2219 route_entry_dump(&rn
->p
, NULL
, re
);
2221 rib_uninstall(rn
, re
);
2228 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2229 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2231 struct route_table
*table
;
2232 struct route_node
*rn
;
2233 struct route_entry
*same
;
2234 struct nexthop
*nexthop
;
2240 assert(!src_p
|| afi
== AFI_IP6
);
2243 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2245 XFREE(MTYPE_RE
, re
);
2249 /* Make it sure prefixlen is applied to the prefix. */
2252 apply_mask_ipv6(src_p
);
2254 /* Set default distance by route type. */
2255 if (re
->distance
== 0) {
2256 re
->distance
= route_distance(re
->type
);
2258 /* iBGP distance is 200. */
2259 if (re
->type
== ZEBRA_ROUTE_BGP
2260 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2264 /* Lookup route node.*/
2265 rn
= srcdest_rnode_get(table
, p
, src_p
);
2267 /* If same type of route are installed, treat it as a implicit
2269 RNODE_FOREACH_RE (rn
, same
) {
2270 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2273 if (same
->type
!= re
->type
)
2275 if (same
->instance
!= re
->instance
)
2277 if (same
->type
== ZEBRA_ROUTE_KERNEL
&&
2278 same
->metric
!= re
->metric
)
2281 * We should allow duplicate connected routes because of
2282 * IPv6 link-local routes and unnumbered interfaces on Linux.
2284 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2288 /* If this route is kernel route, set FIB flag to the route. */
2289 if (RIB_SYSTEM_ROUTE(re
))
2290 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2291 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2293 /* Link new re to node.*/
2294 if (IS_ZEBRA_DEBUG_RIB
) {
2297 "Inserting route rn %p, re %p (type %d) existing %p",
2298 (void *)rn
, (void *)re
, re
->type
, (void *)same
);
2300 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2301 route_entry_dump(p
, src_p
, re
);
2303 rib_addnode(rn
, re
, 1);
2306 /* Free implicit route.*/
2308 rib_delnode(rn
, same
);
2312 route_unlock_node(rn
);
2316 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2317 u_short instance
, int flags
, struct prefix
*p
,
2318 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2319 u_int32_t table_id
, u_int32_t metric
, bool fromkernel
)
2321 struct route_table
*table
;
2322 struct route_node
*rn
;
2323 struct route_entry
*re
;
2324 struct route_entry
*fib
= NULL
;
2325 struct route_entry
*same
= NULL
;
2326 struct nexthop
*rtnh
;
2327 char buf2
[INET6_ADDRSTRLEN
];
2329 assert(!src_p
|| afi
== AFI_IP6
);
2332 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2339 apply_mask_ipv6(src_p
);
2341 /* Lookup route node. */
2342 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2344 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2346 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2347 if (src_p
&& src_p
->prefixlen
)
2348 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2352 if (IS_ZEBRA_DEBUG_RIB
)
2353 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2355 (src_buf
[0] != '\0') ? " from " : "",
2360 /* Lookup same type route. */
2361 RNODE_FOREACH_RE (rn
, re
) {
2362 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2365 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
))
2368 if (re
->type
!= type
)
2370 if (re
->instance
!= instance
)
2372 if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
2373 re
->metric
!= metric
)
2375 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->nexthop
)
2376 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2377 if (rtnh
->ifindex
!= nh
->ifindex
)
2382 /* Make sure that the route found has the same gateway. */
2388 for (ALL_NEXTHOPS(re
->nexthop
, rtnh
))
2389 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2397 /* If same type of route can't be found and this message is from
2401 * In the past(HA!) we could get here because
2402 * we were receiving a route delete from the
2403 * kernel and we're not marking the proto
2404 * as coming from it's appropriate originator.
2405 * Now that we are properly noticing the fact
2406 * that the kernel has deleted our route we
2407 * are not going to get called in this path
2408 * I am going to leave this here because
2409 * this might still work this way on non-linux
2410 * platforms as well as some weird state I have
2411 * not properly thought of yet.
2412 * If we can show that this code path is
2413 * dead then we can remove it.
2415 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
2416 && CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2417 if (IS_ZEBRA_DEBUG_RIB
) {
2420 "rn %p, re %p (type %d) was deleted from kernel, adding",
2421 rn
, fib
, fib
->type
);
2425 for (rtnh
= fib
->nexthop
; rtnh
;
2427 UNSET_FLAG(rtnh
->flags
,
2430 UNSET_FLAG(fib
->status
,
2431 ROUTE_ENTRY_SELECTED_FIB
);
2433 /* This means someone else, other than Zebra,
2435 * a Zebra router from the kernel. We will add
2437 rib_install_kernel(rn
, fib
, NULL
);
2440 if (IS_ZEBRA_DEBUG_RIB
) {
2444 "via %s ifindex %d type %d "
2445 "doesn't exist in rib",
2449 INET_ADDRSTRLEN
), /* FIXME
2455 "type %d doesn't exist in rib",
2458 route_unlock_node(rn
);
2465 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
) &&
2467 rib_install_kernel(rn
, same
, NULL
);
2468 route_unlock_node(rn
);
2472 rib_delnode(rn
, same
);
2475 route_unlock_node(rn
);
2480 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
, u_short instance
,
2481 int flags
, struct prefix
*p
, struct prefix_ipv6
*src_p
,
2482 const struct nexthop
*nh
, u_int32_t table_id
, u_int32_t metric
,
2483 u_int32_t mtu
, uint8_t distance
)
2485 struct route_entry
*re
;
2486 struct nexthop
*nexthop
;
2488 /* Allocate new route_entry structure. */
2489 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2491 re
->instance
= instance
;
2492 re
->distance
= distance
;
2494 re
->metric
= metric
;
2496 re
->table
= table_id
;
2497 re
->vrf_id
= vrf_id
;
2498 re
->nexthop_num
= 0;
2499 re
->uptime
= time(NULL
);
2502 nexthop
= nexthop_new();
2504 route_entry_nexthop_add(re
, nexthop
);
2506 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2509 /* Schedule routes of a particular table (address-family) based on event. */
2510 static void rib_update_table(struct route_table
*table
,
2511 rib_update_event_t event
)
2513 struct route_node
*rn
;
2514 struct route_entry
*re
, *next
;
2516 /* Walk all routes and queue for processing, if appropriate for
2517 * the trigger event.
2519 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2521 * If we are looking at a route node and the node
2522 * has already been queued we don't
2523 * need to queue it up again
2526 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2527 RIB_ROUTE_ANY_QUEUED
))
2530 case RIB_UPDATE_IF_CHANGE
:
2531 /* Examine all routes that won't get processed by the
2533 * triggered by nexthop evaluation (NHT). This would be
2535 * kernel and certain static routes. Note that NHT will
2537 * triggered upon an interface event as connected routes
2539 * get queued for processing.
2541 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2544 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
&&
2545 re
->type
!= ZEBRA_ROUTE_KERNEL
&&
2546 re
->type
!= ZEBRA_ROUTE_CONNECT
&&
2547 re
->type
!= ZEBRA_ROUTE_STATIC
)
2550 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
2555 for (nh
= re
->nexthop
; nh
; nh
= nh
->next
)
2556 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
2557 || nh
->type
== NEXTHOP_TYPE_IPV6
))
2560 /* If we only have nexthops to a
2569 case RIB_UPDATE_RMAP_CHANGE
:
2570 case RIB_UPDATE_OTHER
:
2571 /* Right now, examine all routes. Can restrict to a
2573 * some cases (TODO).
2575 if (rnode_to_ribs(rn
))
2585 /* RIB update function. */
2586 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
2588 struct route_table
*table
;
2590 /* Process routes of interested address-families. */
2591 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2593 rib_update_table(table
, event
);
2595 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2597 rib_update_table(table
, event
);
2600 /* Remove all routes which comes from non main table. */
2601 static void rib_weed_table(struct route_table
*table
)
2603 struct route_node
*rn
;
2604 struct route_entry
*re
;
2605 struct route_entry
*next
;
2608 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2609 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2610 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2613 if (re
->table
!= zebrad
.rtm_table_default
2614 && re
->table
!= RT_TABLE_MAIN
)
2615 rib_delnode(rn
, re
);
2619 /* Delete all routes from non main table. */
2620 void rib_weed_tables(void)
2623 struct zebra_vrf
*zvrf
;
2625 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2626 if ((zvrf
= vrf
->info
) != NULL
) {
2627 rib_weed_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2628 rib_weed_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2632 /* Delete self installed routes after zebra is relaunched. */
2633 static void rib_sweep_table(struct route_table
*table
)
2635 struct route_node
*rn
;
2636 struct route_entry
*re
;
2637 struct route_entry
*next
;
2638 struct nexthop
*nexthop
;
2643 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2644 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2645 if (IS_ZEBRA_DEBUG_RIB
)
2646 route_entry_dump(&rn
->p
, NULL
, re
);
2648 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2651 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
2655 * So we are starting up and have received
2656 * routes from the kernel that we have installed
2657 * from a previous run of zebra but not cleaned
2658 * up ( say a kill -9 )
2659 * But since we haven't actually installed
2660 * them yet( we received them from the kernel )
2661 * we don't think they are active.
2662 * So let's pretend they are active to actually
2664 * In all honesty I'm not sure if we should
2665 * mark them as active when we receive them
2666 * This is startup only so probably ok.
2668 * If we ever decide to move rib_sweep_table
2669 * to a different spot (ie startup )
2670 * this decision needs to be revisited
2672 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
2673 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2675 rib_uninstall_kernel(rn
, re
);
2676 rib_delnode(rn
, re
);
2681 /* Sweep all RIB tables. */
2682 void rib_sweep_route(void)
2685 struct zebra_vrf
*zvrf
;
2687 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2688 if ((zvrf
= vrf
->info
) == NULL
)
2691 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2692 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2696 /* Remove specific by protocol routes from 'table'. */
2697 static unsigned long rib_score_proto_table(u_char proto
, u_short instance
,
2698 struct route_table
*table
)
2700 struct route_node
*rn
;
2701 struct route_entry
*re
;
2702 struct route_entry
*next
;
2703 unsigned long n
= 0;
2706 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2707 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2708 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2710 if (re
->type
== proto
2711 && re
->instance
== instance
) {
2712 rib_delnode(rn
, re
);
2719 /* Remove specific by protocol routes. */
2720 unsigned long rib_score_proto(u_char proto
, u_short instance
)
2723 struct zebra_vrf
*zvrf
;
2724 unsigned long cnt
= 0;
2726 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2727 if ((zvrf
= vrf
->info
) != NULL
)
2728 cnt
+= rib_score_proto_table(
2730 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
2731 + rib_score_proto_table(
2733 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2738 /* Close RIB and clean up kernel routes. */
2739 void rib_close_table(struct route_table
*table
)
2741 struct route_node
*rn
;
2742 rib_table_info_t
*info
;
2743 struct route_entry
*re
;
2750 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2751 RNODE_FOREACH_RE (rn
, re
) {
2752 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
))
2755 if (info
->safi
== SAFI_UNICAST
)
2756 hook_call(rib_update
, rn
, NULL
);
2758 if (!RIB_SYSTEM_ROUTE(re
))
2759 rib_uninstall_kernel(rn
, re
);
2763 /* Routing information base initialize. */
2766 rib_queue_init(&zebrad
);
2772 * Get the first vrf id that is greater than the given vrf id if any.
2774 * Returns TRUE if a vrf id was found, FALSE otherwise.
2776 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
2780 vrf
= vrf_lookup_by_id(vrf_id
);
2782 vrf
= RB_NEXT(vrf_id_head
, vrf
);
2784 *next_id_p
= vrf
->vrf_id
;
2793 * rib_tables_iter_next
2795 * Returns the next table in the iteration.
2797 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
2799 struct route_table
*table
;
2802 * Array that helps us go over all AFI/SAFI combinations via one
2809 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
2810 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
2811 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
2816 switch (iter
->state
) {
2818 case RIB_TABLES_ITER_S_INIT
:
2819 iter
->vrf_id
= VRF_DEFAULT
;
2820 iter
->afi_safi_ix
= -1;
2824 case RIB_TABLES_ITER_S_ITERATING
:
2825 iter
->afi_safi_ix
++;
2828 while (iter
->afi_safi_ix
2829 < (int)ZEBRA_NUM_OF(afi_safis
)) {
2830 table
= zebra_vrf_table(
2831 afi_safis
[iter
->afi_safi_ix
].afi
,
2832 afi_safis
[iter
->afi_safi_ix
].safi
,
2837 iter
->afi_safi_ix
++;
2841 * Found another table in this vrf.
2847 * Done with all tables in the current vrf, go to the
2851 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
2854 iter
->afi_safi_ix
= 0;
2859 case RIB_TABLES_ITER_S_DONE
:
2864 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
2866 iter
->state
= RIB_TABLES_ITER_S_DONE
;