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 /* Hold time for RIB process, should be very minimal.
61 * it is useful to able to set it otherwise for testing, hence exported
62 * as global here for test-rig code.
64 int rib_process_hold_time
= 10;
66 /* Each route type's string and default distance value. */
70 } route_info
[ZEBRA_ROUTE_MAX
] = {
71 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
72 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
73 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
74 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
75 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
76 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
77 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
78 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
79 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
80 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
81 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255},
82 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90},
83 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10},
84 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255},
85 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255},
86 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150},
87 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150},
88 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20},
89 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20},
90 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20},
91 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20},
92 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20},
93 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100},
95 /* no entry/default: 150 */
98 /* RPF lookup behaviour */
99 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
102 static void __attribute__((format(printf
, 5, 6)))
103 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
104 int priority
, const char *msgfmt
, ...)
106 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
110 va_start(ap
, msgfmt
);
111 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
115 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
116 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
118 if (info
->safi
== SAFI_MULTICAST
)
119 strcat(buf
, " (MRIB)");
121 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
124 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
127 #define rnode_debug(node, vrf_id, ...) \
128 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
129 #define rnode_info(node, ...) \
130 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
132 u_char
route_distance(int type
)
136 if ((unsigned)type
>= array_size(route_info
))
139 distance
= route_info
[type
].distance
;
144 int is_zebra_valid_kernel_table(u_int32_t table_id
)
146 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
))
150 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
151 || (table_id
== RT_TABLE_COMPAT
))
158 int is_zebra_main_routing_table(u_int32_t table_id
)
160 if ((table_id
== RT_TABLE_MAIN
)
161 || (table_id
== zebrad
.rtm_table_default
))
166 int zebra_check_addr(struct prefix
*p
)
168 if (p
->family
== AF_INET
) {
171 addr
= p
->u
.prefix4
.s_addr
;
174 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
175 || IPV4_LINKLOCAL(addr
))
178 if (p
->family
== AF_INET6
) {
179 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
181 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
187 /* Add nexthop to the end of a rib node's nexthop list */
188 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
190 nexthop_add(&re
->nexthop
, nexthop
);
196 * copy_nexthop - copy a nexthop to the rib structure.
198 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
200 assert(!re
->nexthop
);
201 copy_nexthops(&re
->nexthop
, nh
, NULL
);
202 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
206 /* Delete specified nexthop from the list. */
207 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
210 nexthop
->next
->prev
= nexthop
->prev
;
212 nexthop
->prev
->next
= nexthop
->next
;
214 re
->nexthop
= nexthop
->next
;
219 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
222 struct nexthop
*nexthop
;
224 nexthop
= nexthop_new();
225 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
226 nexthop
->ifindex
= ifindex
;
228 route_entry_nexthop_add(re
, nexthop
);
233 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
234 struct in_addr
*ipv4
,
237 struct nexthop
*nexthop
;
239 nexthop
= nexthop_new();
240 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
241 nexthop
->gate
.ipv4
= *ipv4
;
243 nexthop
->src
.ipv4
= *src
;
245 route_entry_nexthop_add(re
, nexthop
);
250 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
251 struct in_addr
*ipv4
,
255 struct nexthop
*nexthop
;
256 struct interface
*ifp
;
258 nexthop
= nexthop_new();
259 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
260 nexthop
->gate
.ipv4
= *ipv4
;
262 nexthop
->src
.ipv4
= *src
;
263 nexthop
->ifindex
= ifindex
;
264 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->vrf_id
);
265 /*Pending: need to think if null ifp here is ok during bootup?
266 There was a crash because ifp here was coming to be NULL */
268 if (connected_is_unnumbered(ifp
)) {
269 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
272 route_entry_nexthop_add(re
, nexthop
);
277 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
278 struct in6_addr
*ipv6
)
280 struct nexthop
*nexthop
;
282 nexthop
= nexthop_new();
283 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
284 nexthop
->gate
.ipv6
= *ipv6
;
286 route_entry_nexthop_add(re
, nexthop
);
291 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
292 struct in6_addr
*ipv6
,
295 struct nexthop
*nexthop
;
297 nexthop
= nexthop_new();
298 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
299 nexthop
->gate
.ipv6
= *ipv6
;
300 nexthop
->ifindex
= ifindex
;
302 route_entry_nexthop_add(re
, nexthop
);
307 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
308 enum blackhole_type bh_type
)
310 struct nexthop
*nexthop
;
312 nexthop
= nexthop_new();
313 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
314 nexthop
->bh_type
= bh_type
;
316 route_entry_nexthop_add(re
, nexthop
);
321 static void nexthop_set_resolved(afi_t afi
, struct nexthop
*newhop
,
322 struct nexthop
*nexthop
)
324 struct nexthop
*resolved_hop
;
326 resolved_hop
= nexthop_new();
327 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
328 /* If the resolving route specifies a gateway, use it */
329 if (newhop
->type
== NEXTHOP_TYPE_IPV4
330 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
) {
331 resolved_hop
->type
= newhop
->type
;
332 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
334 if (newhop
->ifindex
) {
335 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
336 resolved_hop
->ifindex
= newhop
->ifindex
;
337 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
338 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
341 if (newhop
->type
== NEXTHOP_TYPE_IPV6
342 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
) {
343 resolved_hop
->type
= newhop
->type
;
344 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
346 if (newhop
->ifindex
) {
347 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
348 resolved_hop
->ifindex
= newhop
->ifindex
;
352 /* If the resolving route is an interface route,
353 * it means the gateway we are looking up is connected
354 * to that interface. (The actual network is _not_ onlink).
355 * Therefore, the resolved route should have the original
356 * gateway as nexthop as it is directly connected.
358 * On Linux, we have to set the onlink netlink flag because
359 * otherwise, the kernel won't accept the route.
361 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
) {
362 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
364 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
365 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
366 } else if (afi
== AFI_IP6
) {
367 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
368 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
370 resolved_hop
->ifindex
= newhop
->ifindex
;
373 if (newhop
->type
== NEXTHOP_TYPE_BLACKHOLE
) {
374 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
375 resolved_hop
->bh_type
= nexthop
->bh_type
;
377 resolved_hop
->rparent
= nexthop
;
378 nexthop_add(&nexthop
->resolved
, resolved_hop
);
381 /* If force flag is not set, do not modify falgs at all for uninstall
382 the route from FIB. */
383 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
384 struct nexthop
*nexthop
, int set
,
385 struct route_node
*top
)
388 struct route_table
*table
;
389 struct route_node
*rn
;
390 struct route_entry
*match
;
392 struct nexthop
*newhop
;
393 struct interface
*ifp
;
395 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
396 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
397 nexthop
->ifindex
= 0;
400 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
401 zebra_deregister_rnh_static_nexthops(re
->vrf_id
,
402 nexthop
->resolved
, top
);
403 nexthops_free(nexthop
->resolved
);
404 nexthop
->resolved
= NULL
;
408 /* Skip nexthops that have been filtered out due to route-map */
409 /* The nexthops are specific to this route and so the same */
410 /* nexthop for a different route may not have this flag set */
411 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
415 * Check to see if we should trust the passed in information
416 * for UNNUMBERED interfaces as that we won't find the GW
417 * address in the routing table.
419 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
420 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->vrf_id
);
421 if (ifp
&& connected_is_unnumbered(ifp
)) {
422 if (if_is_operative(ifp
))
430 /* Make lookup prefix. */
431 memset(&p
, 0, sizeof(struct prefix
));
435 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
436 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
440 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
441 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
444 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
448 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, re
->vrf_id
);
452 rn
= route_node_match(table
, (struct prefix
*)&p
);
454 route_unlock_node(rn
);
456 /* If lookup self prefix return immediately. */
460 /* Pick up selected route. */
461 /* However, do not resolve over default route unless explicitly
463 if (is_default_prefix(&rn
->p
)
464 && !nh_resolve_via_default(p
.family
))
467 RNODE_FOREACH_RE (rn
, match
) {
468 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
))
471 /* if the next hop is imported from another table, skip
473 if (match
->type
== ZEBRA_ROUTE_TABLE
)
475 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
479 /* If there is no selected route or matched route is EGP, go up
484 } while (rn
&& rn
->info
== NULL
);
491 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
492 /* Directly point connected route. */
493 newhop
= match
->nexthop
;
495 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
496 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
497 nexthop
->ifindex
= newhop
->ifindex
;
500 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_INTERNAL
)) {
502 for (ALL_NEXTHOPS(match
->nexthop
, newhop
)) {
503 if (!CHECK_FLAG(newhop
->flags
,
506 if (CHECK_FLAG(newhop
->flags
,
507 NEXTHOP_FLAG_RECURSIVE
))
511 SET_FLAG(nexthop
->flags
,
512 NEXTHOP_FLAG_RECURSIVE
);
514 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
515 nexthop_set_resolved(afi
, newhop
,
521 re
->nexthop_mtu
= match
->mtu
;
523 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
525 for (ALL_NEXTHOPS(match
->nexthop
, newhop
)) {
526 if (!CHECK_FLAG(newhop
->flags
,
531 SET_FLAG(nexthop
->flags
,
532 NEXTHOP_FLAG_RECURSIVE
);
533 nexthop_set_resolved(afi
, newhop
,
539 re
->nexthop_mtu
= match
->mtu
;
548 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
549 union g_addr
*addr
, struct route_node
**rn_out
)
552 struct route_table
*table
;
553 struct route_node
*rn
;
554 struct route_entry
*match
;
555 struct nexthop
*newhop
;
558 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
562 memset(&p
, 0, sizeof(struct prefix
));
565 p
.u
.prefix4
= addr
->ipv4
;
566 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
568 p
.u
.prefix6
= addr
->ipv6
;
569 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
572 rn
= route_node_match(table
, (struct prefix
*)&p
);
575 route_unlock_node(rn
);
577 /* Pick up selected route. */
578 RNODE_FOREACH_RE (rn
, match
) {
579 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
))
581 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
585 /* If there is no selected route or matched route is EGP, go up
590 } while (rn
&& rn
->info
== NULL
);
594 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
596 for (ALL_NEXTHOPS(match
->nexthop
, newhop
))
597 if (CHECK_FLAG(newhop
->flags
,
614 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
616 struct route_node
**rn_out
)
618 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
619 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
620 union g_addr gaddr
= {.ipv4
= addr
};
622 switch (ipv4_multicast_mode
) {
623 case MCAST_MRIB_ONLY
:
624 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
626 case MCAST_URIB_ONLY
:
627 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
628 case MCAST_NO_CONFIG
:
629 case MCAST_MIX_MRIB_FIRST
:
630 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
633 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
636 case MCAST_MIX_DISTANCE
:
637 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
638 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
640 re
= ure
->distance
< mre
->distance
? ure
: mre
;
646 case MCAST_MIX_PFXLEN
:
647 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
648 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
650 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
659 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
661 if (IS_ZEBRA_DEBUG_RIB
) {
663 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
665 zlog_debug("%s: %s: found %s, using %s", __func__
, buf
,
666 mre
? (ure
? "MRIB+URIB" : "MRIB")
667 : ure
? "URIB" : "nothing",
668 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
673 void multicast_mode_ipv4_set(enum multicast_mode mode
)
675 if (IS_ZEBRA_DEBUG_RIB
)
676 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
678 ipv4_multicast_mode
= mode
;
681 enum multicast_mode
multicast_mode_ipv4_get(void)
683 return ipv4_multicast_mode
;
686 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
688 struct route_table
*table
;
689 struct route_node
*rn
;
690 struct route_entry
*match
;
691 struct nexthop
*nexthop
;
694 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
698 rn
= route_node_lookup(table
, (struct prefix
*)p
);
700 /* No route for this prefix. */
705 route_unlock_node(rn
);
707 RNODE_FOREACH_RE (rn
, match
) {
708 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
))
710 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
717 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
720 for (ALL_NEXTHOPS(match
->nexthop
, nexthop
))
721 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
728 * This clone function, unlike its original rib_lookup_ipv4(), checks
729 * if specified IPv4 route record (prefix/mask -> gate) exists in
730 * the whole RIB and has ROUTE_ENTRY_SELECTED_FIB set.
734 * 0: exact match found
735 * 1: a match was found with a different gate
736 * 2: connected route found
737 * 3: no matches found
739 int rib_lookup_ipv4_route(struct prefix_ipv4
*p
, union sockunion
*qgate
,
742 struct route_table
*table
;
743 struct route_node
*rn
;
744 struct route_entry
*match
;
745 struct nexthop
*nexthop
;
749 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
751 return ZEBRA_RIB_LOOKUP_ERROR
;
753 /* Scan the RIB table for exactly matching RIB entry. */
754 rn
= route_node_lookup(table
, (struct prefix
*)p
);
756 /* No route for this prefix. */
758 return ZEBRA_RIB_NOTFOUND
;
761 route_unlock_node(rn
);
763 /* Find out if a "selected" RR for the discovered RIB entry exists ever.
765 RNODE_FOREACH_RE (rn
, match
) {
766 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_REMOVED
))
768 if (CHECK_FLAG(match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
772 /* None such found :( */
774 return ZEBRA_RIB_NOTFOUND
;
776 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
777 return ZEBRA_RIB_FOUND_CONNECTED
;
779 /* Ok, we have a cood candidate, let's check it's nexthop list... */
781 for (ALL_NEXTHOPS(match
->nexthop
, nexthop
))
782 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
784 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip(qgate
))
785 return ZEBRA_RIB_FOUND_EXACT
;
786 if (IS_ZEBRA_DEBUG_RIB
) {
787 char gate_buf
[INET_ADDRSTRLEN
],
788 qgate_buf
[INET_ADDRSTRLEN
];
789 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
.s_addr
,
790 gate_buf
, INET_ADDRSTRLEN
);
791 inet_ntop(AF_INET
, &sockunion2ip(qgate
),
792 qgate_buf
, INET_ADDRSTRLEN
);
793 zlog_debug("%s: qgate == %s, %s == %s",
795 nexthop
->rparent
? "rgate" : "gate",
801 return ZEBRA_RIB_FOUND_NOGATE
;
803 return ZEBRA_RIB_NOTFOUND
;
806 #define RIB_SYSTEM_ROUTE(R) \
807 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
809 /* This function verifies reachability of one given nexthop, which can be
810 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
811 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
812 * nexthop->ifindex will be updated appropriately as well.
813 * An existing route map can turn (otherwise active) nexthop into inactive, but
816 * The return value is the final value of 'ACTIVE' flag.
819 static unsigned nexthop_active_check(struct route_node
*rn
,
820 struct route_entry
*re
,
821 struct nexthop
*nexthop
, int set
)
823 struct interface
*ifp
;
824 route_map_result_t ret
= RMAP_MATCH
;
826 char buf
[SRCDEST2STR_BUFFER
];
827 struct prefix
*p
, *src_p
;
828 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
830 if (rn
->p
.family
== AF_INET
)
832 else if (rn
->p
.family
== AF_INET6
)
836 switch (nexthop
->type
) {
837 case NEXTHOP_TYPE_IFINDEX
:
838 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->vrf_id
);
839 if (ifp
&& if_is_operative(ifp
))
840 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
842 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
844 case NEXTHOP_TYPE_IPV4
:
845 case NEXTHOP_TYPE_IPV4_IFINDEX
:
847 if (nexthop_active(AFI_IP
, re
, nexthop
, set
, rn
))
848 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
850 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
852 case NEXTHOP_TYPE_IPV6
:
854 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
855 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
857 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
859 case NEXTHOP_TYPE_IPV6_IFINDEX
:
860 /* RFC 5549, v4 prefix with v6 NH */
861 if (rn
->p
.family
!= AF_INET
)
863 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
864 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->vrf_id
);
865 if (ifp
&& if_is_operative(ifp
))
866 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
868 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
870 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
871 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
873 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
876 case NEXTHOP_TYPE_BLACKHOLE
:
877 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
882 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
885 /* XXX: What exactly do those checks do? Do we support
886 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
887 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
888 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
889 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
891 /* The original code didn't determine the family correctly
892 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
893 * from the rib_table_info in those cases.
894 * Possibly it may be better to use only the rib_table_info
898 rib_table_info_t
*info
;
900 info
= srcdest_rnode_table_info(rn
);
904 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
906 /* It'll get set if required inside */
907 ret
= zebra_route_map_check(family
, re
->type
, p
, nexthop
, re
->vrf_id
,
909 if (ret
== RMAP_DENYMATCH
) {
910 if (IS_ZEBRA_DEBUG_RIB
) {
911 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
913 "%u:%s: Filtering out with NH out %s due to route map",
915 ifindex2ifname(nexthop
->ifindex
, re
->vrf_id
));
917 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
919 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
922 /* Iterate over all nexthops of the given RIB entry and refresh their
923 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
924 * nexthop is found to toggle the ACTIVE flag, the whole re structure
925 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
926 * transparently passed to nexthop_active_check().
928 * Return value is the new number of active nexthops.
931 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
,
934 struct nexthop
*nexthop
;
935 union g_addr prev_src
;
936 unsigned int prev_active
, new_active
, old_num_nh
;
937 ifindex_t prev_index
;
938 old_num_nh
= re
->nexthop_active_num
;
940 re
->nexthop_active_num
= 0;
941 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
943 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
944 /* No protocol daemon provides src and so we're skipping
946 prev_src
= nexthop
->rmap_src
;
947 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
948 prev_index
= nexthop
->ifindex
;
949 if ((new_active
= nexthop_active_check(rn
, re
, nexthop
, set
)))
950 re
->nexthop_active_num
++;
951 /* Don't allow src setting on IPv6 addr for now */
952 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
953 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
954 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
955 && prev_src
.ipv4
.s_addr
956 != nexthop
->rmap_src
.ipv4
.s_addr
)
957 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
958 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
959 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
960 &nexthop
->rmap_src
.ipv6
)))) {
961 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
962 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
966 if (old_num_nh
!= re
->nexthop_active_num
)
967 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
969 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
970 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
973 return re
->nexthop_active_num
;
977 * Is this RIB labeled-unicast? It must be of type BGP and all paths
978 * (nexthops) must have a label.
980 int zebra_rib_labeled_unicast(struct route_entry
*re
)
982 struct nexthop
*nexthop
= NULL
;
984 if (re
->type
!= ZEBRA_ROUTE_BGP
)
987 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
988 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
994 /* Update flag indicates whether this is a "replace" or not. Currently, this
995 * is only used for IPv4.
997 int rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
998 struct route_entry
*old
)
1001 struct nexthop
*nexthop
;
1002 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1003 struct prefix
*p
, *src_p
;
1004 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1006 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1008 if (info
->safi
!= SAFI_UNICAST
) {
1009 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
1010 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1013 struct nexthop
*prev
;
1015 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1016 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1017 for (ALL_NEXTHOPS(re
->nexthop
, prev
)) {
1018 if (prev
== nexthop
)
1020 if (nexthop_same_firsthop (nexthop
, prev
))
1022 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1031 * Make sure we update the FPM any time we send new information to
1034 hook_call(rib_update
, rn
, "installing in kernel");
1035 ret
= kernel_route_rib(p
, src_p
, old
, re
);
1038 /* If install succeeds, update FIB flag for nexthops. */
1040 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1041 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1044 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1045 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1047 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1054 /* Uninstall the route from kernel. */
1055 int rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1058 struct nexthop
*nexthop
;
1059 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1060 struct prefix
*p
, *src_p
;
1061 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1063 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1065 if (info
->safi
!= SAFI_UNICAST
) {
1066 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
1067 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1072 * Make sure we update the FPM any time we send new information to
1075 hook_call(rib_update
, rn
, "uninstalling from kernel");
1076 ret
= kernel_route_rib(p
, src_p
, re
, NULL
);
1079 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
1080 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1085 /* Uninstall the route from kernel. */
1086 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1088 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1090 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
)) {
1091 if (info
->safi
== SAFI_UNICAST
)
1092 hook_call(rib_update
, rn
, "rib_uninstall");
1094 if (!RIB_SYSTEM_ROUTE(re
))
1095 rib_uninstall_kernel(rn
, re
);
1097 /* If labeled-unicast route, uninstall transit LSP. */
1098 if (zebra_rib_labeled_unicast(re
))
1099 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1101 UNSET_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
);
1104 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1105 struct prefix
*p
, *src_p
;
1106 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1108 redistribute_delete(p
, src_p
, re
);
1109 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1114 * rib_can_delete_dest
1116 * Returns TRUE if the given dest can be deleted from the table.
1118 static int rib_can_delete_dest(rib_dest_t
*dest
)
1125 * Don't delete the dest if we have to update the FPM about this
1128 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1129 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1138 * Garbage collect the rib dest corresponding to the given route node
1141 * Returns TRUE if the dest was deleted, FALSE otherwise.
1143 int rib_gc_dest(struct route_node
*rn
)
1147 dest
= rib_dest_from_rnode(rn
);
1151 if (!rib_can_delete_dest(dest
))
1154 if (IS_ZEBRA_DEBUG_RIB
) {
1155 struct zebra_vrf
*zvrf
;
1157 zvrf
= rib_dest_vrf(dest
);
1158 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1162 XFREE(MTYPE_RIB_DEST
, dest
);
1166 * Release the one reference that we keep on the route node.
1168 route_unlock_node(rn
);
1172 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1173 struct route_entry
*new)
1175 hook_call(rib_update
, rn
, "new route selected");
1177 /* Update real nexthop. This may actually determine if nexthop is active
1179 if (!nexthop_active_update(rn
, new, 1)) {
1180 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1184 SET_FLAG(new->status
, ROUTE_ENTRY_SELECTED_FIB
);
1185 if (IS_ZEBRA_DEBUG_RIB
) {
1186 char buf
[SRCDEST2STR_BUFFER
];
1187 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1188 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1189 zvrf_id(zvrf
), buf
, rn
, new, new->type
);
1192 /* If labeled-unicast route, install transit LSP. */
1193 if (zebra_rib_labeled_unicast(new))
1194 zebra_mpls_lsp_install(zvrf
, rn
, new);
1196 if (!RIB_SYSTEM_ROUTE(new)) {
1197 if (rib_install_kernel(rn
, new, NULL
)) {
1198 char buf
[SRCDEST2STR_BUFFER
];
1199 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1200 zlog_warn("%u:%s: Route install failed", zvrf_id(zvrf
),
1205 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1208 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1209 struct route_entry
*old
)
1211 hook_call(rib_update
, rn
, "removing existing route");
1213 /* Uninstall from kernel. */
1214 if (IS_ZEBRA_DEBUG_RIB
) {
1215 char buf
[SRCDEST2STR_BUFFER
];
1216 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1217 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1218 zvrf_id(zvrf
), buf
, rn
, old
, old
->type
);
1221 /* If labeled-unicast route, uninstall transit LSP. */
1222 if (zebra_rib_labeled_unicast(old
))
1223 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1225 if (!RIB_SYSTEM_ROUTE(old
))
1226 rib_uninstall_kernel(rn
, old
);
1228 UNSET_FLAG(old
->status
, ROUTE_ENTRY_SELECTED_FIB
);
1230 /* Update nexthop for route, reset changed flag. */
1231 nexthop_active_update(rn
, old
, 1);
1232 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1235 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1236 struct route_node
*rn
,
1237 struct route_entry
*old
,
1238 struct route_entry
*new)
1240 struct nexthop
*nexthop
= NULL
;
1245 * We have to install or update if a new route has been selected or
1246 * something has changed.
1248 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1249 hook_call(rib_update
, rn
, "updating existing route");
1251 /* Update the nexthop; we could determine here that nexthop is
1253 if (nexthop_active_update(rn
, new, 1))
1256 /* If nexthop is active, install the selected route, if
1258 * the install succeeds, cleanup flags for prior route, if
1263 if (IS_ZEBRA_DEBUG_RIB
) {
1264 char buf
[SRCDEST2STR_BUFFER
];
1265 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1268 "%u:%s: Updating route rn %p, re %p (type %d) "
1270 zvrf_id(zvrf
), buf
, rn
, new,
1271 new->type
, old
, old
->type
);
1274 "%u:%s: Updating route rn %p, re %p (type %d)",
1275 zvrf_id(zvrf
), buf
, rn
, new,
1279 /* If labeled-unicast route, uninstall transit LSP. */
1280 if (zebra_rib_labeled_unicast(old
))
1281 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1283 /* Non-system route should be installed. */
1284 if (!RIB_SYSTEM_ROUTE(new)) {
1285 /* If labeled-unicast route, install transit
1287 if (zebra_rib_labeled_unicast(new))
1288 zebra_mpls_lsp_install(zvrf
, rn
, new);
1290 if (rib_install_kernel(rn
, new, old
)) {
1291 char buf
[SRCDEST2STR_BUFFER
];
1292 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1294 zlog_warn("%u:%s: Route install failed",
1295 zvrf_id(zvrf
), buf
);
1299 /* If install succeeded or system route, cleanup flags
1300 * for prior route. */
1301 if (installed
&& new != old
) {
1302 if (RIB_SYSTEM_ROUTE(new)) {
1303 if (!RIB_SYSTEM_ROUTE(old
))
1304 rib_uninstall_kernel(rn
, old
);
1306 for (nexthop
= old
->nexthop
; nexthop
;
1307 nexthop
= nexthop
->next
)
1308 UNSET_FLAG(nexthop
->flags
,
1313 /* Update for redistribution. */
1315 SET_FLAG(new->status
, ROUTE_ENTRY_SELECTED_FIB
);
1319 * If nexthop for selected route is not active or install
1321 * may need to uninstall and delete for redistribution.
1323 if (!nh_active
|| !installed
) {
1324 if (IS_ZEBRA_DEBUG_RIB
) {
1325 char buf
[SRCDEST2STR_BUFFER
];
1326 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1329 "%u:%s: Deleting route rn %p, re %p (type %d) "
1330 "old %p (type %d) - %s",
1331 zvrf_id(zvrf
), buf
, rn
, new,
1332 new->type
, old
, old
->type
,
1333 nh_active
? "install failed"
1334 : "nexthop inactive");
1337 "%u:%s: Deleting route rn %p, re %p (type %d) - %s",
1338 zvrf_id(zvrf
), buf
, rn
, new,
1340 nh_active
? "install failed"
1341 : "nexthop inactive");
1344 /* If labeled-unicast route, uninstall transit LSP. */
1345 if (zebra_rib_labeled_unicast(old
))
1346 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1348 if (!RIB_SYSTEM_ROUTE(old
))
1349 rib_uninstall_kernel(rn
, old
);
1350 UNSET_FLAG(new->status
, ROUTE_ENTRY_SELECTED_FIB
);
1354 * Same route selected; check if in the FIB and if not,
1356 * is housekeeping code to deal with race conditions in kernel
1358 * netlink reporting interface up before IPv4 or IPv6 protocol
1362 if (!RIB_SYSTEM_ROUTE(new)) {
1365 for (ALL_NEXTHOPS(new->nexthop
, nexthop
))
1366 if (CHECK_FLAG(nexthop
->flags
,
1367 NEXTHOP_FLAG_FIB
)) {
1372 rib_install_kernel(rn
, new, NULL
);
1376 /* Update prior route. */
1378 UNSET_FLAG(old
->status
, ROUTE_ENTRY_SELECTED_FIB
);
1380 /* Set real nexthop. */
1381 nexthop_active_update(rn
, old
, 1);
1382 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1385 /* Clear changed flag. */
1386 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1389 /* Check if 'alternate' RIB entry is better than 'current'. */
1390 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1391 struct route_entry
*alternate
)
1393 if (current
== NULL
)
1396 /* filter route selection in following order:
1397 * - connected beats other types
1398 * - lower distance beats higher
1399 * - lower metric beats higher for equal distance
1400 * - last, hence oldest, route wins tie break.
1403 /* Connected routes. Pick the last connected
1404 * route of the set of lowest metric connected routes.
1406 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1407 if (current
->type
!= ZEBRA_ROUTE_CONNECT
1408 || alternate
->metric
<= current
->metric
)
1414 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1417 /* higher distance loses */
1418 if (alternate
->distance
< current
->distance
)
1420 if (current
->distance
< alternate
->distance
)
1423 /* metric tie-breaks equal distance */
1424 if (alternate
->metric
<= current
->metric
)
1430 /* Core function for processing routing information base. */
1431 static void rib_process(struct route_node
*rn
)
1433 struct route_entry
*re
;
1434 struct route_entry
*next
;
1435 struct route_entry
*old_selected
= NULL
;
1436 struct route_entry
*new_selected
= NULL
;
1437 struct route_entry
*old_fib
= NULL
;
1438 struct route_entry
*new_fib
= NULL
;
1439 struct route_entry
*best
= NULL
;
1440 char buf
[SRCDEST2STR_BUFFER
];
1442 struct zebra_vrf
*zvrf
= NULL
;
1443 struct prefix
*p
, *src_p
;
1444 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1445 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1449 dest
= rib_dest_from_rnode(rn
);
1451 zvrf
= rib_dest_vrf(dest
);
1452 vrf_id
= zvrf_id(zvrf
);
1455 if (IS_ZEBRA_DEBUG_RIB
)
1456 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1458 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1459 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1461 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1462 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1464 "%u:%s: Examine re %p (type %d) status %x flags %x "
1465 "dist %d metric %d",
1466 vrf_id
, buf
, re
, re
->type
, re
->status
,
1467 re
->flags
, re
->distance
, re
->metric
);
1469 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1471 /* Currently selected re. */
1472 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1473 assert(old_selected
== NULL
);
1476 /* Currently in fib */
1477 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
)) {
1478 assert(old_fib
== NULL
);
1482 /* Skip deleted entries from selection */
1483 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1486 /* Skip unreachable nexthop. */
1487 /* This first call to nexthop_active_update is merely to
1489 * there's any change to nexthops associated with this RIB
1491 * rib_process() can be invoked due to an external event such as
1493 * down or due to next-hop-tracking evaluation. In the latter
1495 * a decision has already been made that the NHs have changed.
1497 * need to invoke a potentially expensive call again. Further,
1499 * the change might be in a recursive NH which is not caught in
1500 * the nexthop_active_update() code. Thus, we might miss changes
1504 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1505 && !nexthop_active_update(rn
, re
, 0)) {
1506 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1507 /* XXX: HERE BE DRAGONS!!!!!
1508 * In all honesty, I have not yet figured out
1510 * does or why the ROUTE_ENTRY_CHANGED test
1512 * or why we need to delete a route here, and
1514 * this concerns both selected and fib route, or
1517 /* This entry was denied by the 'ip protocol
1518 * table' route-map, we
1519 * need to delete it */
1520 if (re
!= old_selected
) {
1521 if (IS_ZEBRA_DEBUG_RIB
)
1523 "%s: %s: imported via import-table but denied "
1524 "by the ip protocol table route-map",
1528 SET_FLAG(re
->status
,
1529 ROUTE_ENTRY_REMOVED
);
1535 /* Infinite distance. */
1536 if (re
->distance
== DISTANCE_INFINITY
) {
1537 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1541 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1542 best
= rib_choose_best(new_fib
, re
);
1543 if (new_fib
&& best
!= new_fib
)
1544 UNSET_FLAG(new_fib
->status
,
1545 ROUTE_ENTRY_CHANGED
);
1548 best
= rib_choose_best(new_selected
, re
);
1549 if (new_selected
&& best
!= new_selected
)
1550 UNSET_FLAG(new_selected
->status
,
1551 ROUTE_ENTRY_CHANGED
);
1552 new_selected
= best
;
1555 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1556 } /* RNODE_FOREACH_RE */
1558 /* If no FIB override route, use the selected route also for FIB */
1559 if (new_fib
== NULL
)
1560 new_fib
= new_selected
;
1562 /* After the cycle is finished, the following pointers will be set:
1563 * old_selected --- RE entry currently having SELECTED
1564 * new_selected --- RE entry that is newly SELECTED
1565 * old_fib --- RE entry currently in kernel FIB
1566 * new_fib --- RE entry that is newly to be in kernel FIB
1568 * new_selected will get SELECTED flag, and is going to be redistributed
1569 * the zclients. new_fib (which can be new_selected) will be installed
1573 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1575 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1576 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1577 (void *)old_fib
, (void *)new_fib
);
1580 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1581 * fib == selected */
1582 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1583 ROUTE_ENTRY_CHANGED
);
1585 /* Update fib according to selection results */
1586 if (new_fib
&& old_fib
)
1587 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1589 rib_process_add_fib(zvrf
, rn
, new_fib
);
1591 rib_process_del_fib(zvrf
, rn
, old_fib
);
1593 /* Redistribute SELECTED entry */
1594 if (old_selected
!= new_selected
|| selected_changed
) {
1595 struct nexthop
*nexthop
;
1597 /* Check if we have a FIB route for the destination, otherwise,
1598 * don't redistribute it */
1599 for (ALL_NEXTHOPS(new_fib
? new_fib
->nexthop
: NULL
, nexthop
)) {
1600 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1605 new_selected
= NULL
;
1607 if (new_selected
&& new_selected
!= new_fib
) {
1608 nexthop_active_update(rn
, new_selected
, 1);
1609 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1614 redistribute_delete(p
, src_p
, old_selected
);
1615 if (old_selected
!= new_selected
)
1616 UNSET_FLAG(old_selected
->flags
,
1617 ZEBRA_FLAG_SELECTED
);
1621 /* Install new or replace existing redistributed entry
1623 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1624 redistribute_update(p
, src_p
, new_selected
,
1629 /* Remove all RE entries queued for removal */
1630 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1631 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1632 if (IS_ZEBRA_DEBUG_RIB
) {
1633 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1634 (void *)rn
, (void *)re
);
1641 * Check if the dest can be deleted now.
1646 /* Take a list of route_node structs and return 1, if there was a record
1647 * picked from it and processed by rib_process(). Don't process more,
1648 * than one RN record; operate only in the specified sub-queue.
1650 static unsigned int process_subq(struct list
*subq
, u_char qindex
)
1652 struct listnode
*lnode
= listhead(subq
);
1653 struct route_node
*rnode
;
1655 struct zebra_vrf
*zvrf
= NULL
;
1660 rnode
= listgetdata(lnode
);
1661 dest
= rib_dest_from_rnode(rnode
);
1663 zvrf
= rib_dest_vrf(dest
);
1667 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1668 char buf
[SRCDEST2STR_BUFFER
];
1669 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
1670 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
1671 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
1675 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
1676 RIB_ROUTE_QUEUED(qindex
));
1681 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1682 __func__
, rnode
, rnode
->lock
);
1683 zlog_backtrace(LOG_DEBUG
);
1686 route_unlock_node(rnode
);
1687 list_delete_node(subq
, lnode
);
1692 * All meta queues have been processed. Trigger next-hop evaluation.
1694 static void meta_queue_process_complete(struct work_queue
*dummy
)
1697 struct zebra_vrf
*zvrf
;
1699 /* Evaluate nexthops for those VRFs which underwent route processing.
1701 * should limit the evaluation to the necessary VRFs in most common
1704 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1706 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
1709 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
1710 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET
, 0, RNH_NEXTHOP_TYPE
,
1712 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET
, 0,
1713 RNH_IMPORT_CHECK_TYPE
, NULL
);
1714 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET6
, 0, RNH_NEXTHOP_TYPE
,
1716 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET6
, 0,
1717 RNH_IMPORT_CHECK_TYPE
, NULL
);
1720 /* Schedule LSPs for processing, if needed. */
1721 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1722 if (mpls_should_lsps_be_processed(zvrf
)) {
1723 if (IS_ZEBRA_DEBUG_MPLS
)
1725 "%u: Scheduling all LSPs upon RIB completion",
1727 zebra_mpls_lsp_schedule(zvrf
);
1728 mpls_unmark_lsps_for_processing(zvrf
);
1732 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1733 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
1735 * is pointed to the meta queue structure.
1737 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
1739 struct meta_queue
*mq
= data
;
1742 for (i
= 0; i
< MQ_SIZE
; i
++)
1743 if (process_subq(mq
->subq
[i
], i
)) {
1747 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1751 * Map from rib types to queue type (priority) in meta queue
1753 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1754 [ZEBRA_ROUTE_SYSTEM
] = 4,
1755 [ZEBRA_ROUTE_KERNEL
] = 0,
1756 [ZEBRA_ROUTE_CONNECT
] = 0,
1757 [ZEBRA_ROUTE_STATIC
] = 1,
1758 [ZEBRA_ROUTE_RIP
] = 2,
1759 [ZEBRA_ROUTE_RIPNG
] = 2,
1760 [ZEBRA_ROUTE_OSPF
] = 2,
1761 [ZEBRA_ROUTE_OSPF6
] = 2,
1762 [ZEBRA_ROUTE_ISIS
] = 2,
1763 [ZEBRA_ROUTE_BGP
] = 3,
1764 [ZEBRA_ROUTE_PIM
] = 4, // Shouldn't happen but for safety
1765 [ZEBRA_ROUTE_EIGRP
] = 2,
1766 [ZEBRA_ROUTE_NHRP
] = 2,
1767 [ZEBRA_ROUTE_HSLS
] = 4,
1768 [ZEBRA_ROUTE_OLSR
] = 4,
1769 [ZEBRA_ROUTE_TABLE
] = 1,
1770 [ZEBRA_ROUTE_LDP
] = 4,
1771 [ZEBRA_ROUTE_VNC
] = 3,
1772 [ZEBRA_ROUTE_VNC_DIRECT
] = 3,
1773 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = 3,
1774 [ZEBRA_ROUTE_BGP_DIRECT
] = 3,
1775 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = 3,
1776 [ZEBRA_ROUTE_BABEL
] = 2,
1777 [ZEBRA_ROUTE_ALL
] = 4, // Shouldn't happen but for safety
1780 /* Look into the RN and queue it into one or more priority queues,
1781 * increasing the size for each data push done.
1783 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
1785 struct route_entry
*re
;
1787 RNODE_FOREACH_RE (rn
, re
) {
1788 u_char qindex
= meta_queue_map
[re
->type
];
1789 struct zebra_vrf
*zvrf
;
1791 /* Invariant: at this point we always have rn->info set. */
1792 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
1793 RIB_ROUTE_QUEUED(qindex
))) {
1794 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1797 "rn %p is already queued in sub-queue %u",
1798 (void *)rn
, qindex
);
1802 SET_FLAG(rib_dest_from_rnode(rn
)->flags
,
1803 RIB_ROUTE_QUEUED(qindex
));
1804 listnode_add(mq
->subq
[qindex
], rn
);
1805 route_lock_node(rn
);
1808 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1809 rnode_debug(rn
, re
->vrf_id
,
1810 "queued rn %p into sub-queue %u",
1811 (void *)rn
, qindex
);
1813 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
1815 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1819 /* Add route_node to work queue and schedule processing */
1820 void rib_queue_add(struct route_node
*rn
)
1824 /* Pointless to queue a route_node with no RIB entries to add or remove
1826 if (!rnode_to_ribs(rn
)) {
1827 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
1828 __func__
, (void *)rn
, rn
->lock
);
1829 zlog_backtrace(LOG_DEBUG
);
1833 if (zebrad
.ribq
== NULL
) {
1834 zlog_err("%s: work_queue does not exist!", __func__
);
1839 * The RIB queue should normally be either empty or holding the only
1840 * work_queue_item element. In the latter case this element would
1841 * hold a pointer to the meta queue structure, which must be used to
1842 * actually queue the route nodes to process. So create the MQ
1843 * holder, if necessary, then push the work into it in any case.
1844 * This semantics was introduced after 0.99.9 release.
1846 if (work_queue_empty(zebrad
.ribq
))
1847 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
1849 rib_meta_queue_add(zebrad
.mq
, rn
);
1854 /* Create new meta queue.
1855 A destructor function doesn't seem to be necessary here.
1857 static struct meta_queue
*meta_queue_new(void)
1859 struct meta_queue
*new;
1862 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
1865 for (i
= 0; i
< MQ_SIZE
; i
++) {
1866 new->subq
[i
] = list_new();
1867 assert(new->subq
[i
]);
1873 void meta_queue_free(struct meta_queue
*mq
)
1877 for (i
= 0; i
< MQ_SIZE
; i
++)
1878 list_delete_and_null(&mq
->subq
[i
]);
1880 XFREE(MTYPE_WORK_QUEUE
, mq
);
1883 /* initialise zebra rib work queue */
1884 static void rib_queue_init(struct zebra_t
*zebra
)
1889 work_queue_new(zebra
->master
, "route_node processing"))) {
1890 zlog_err("%s: could not initialise work queue!", __func__
);
1894 /* fill in the work queue spec */
1895 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1896 zebra
->ribq
->spec
.errorfunc
= NULL
;
1897 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
1898 /* XXX: TODO: These should be runtime configurable via vty */
1899 zebra
->ribq
->spec
.max_retries
= 3;
1900 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
1902 if (!(zebra
->mq
= meta_queue_new())) {
1903 zlog_err("%s: could not initialise meta queue!", __func__
);
1909 /* RIB updates are processed via a queue of pointers to route_nodes.
1911 * The queue length is bounded by the maximal size of the routing table,
1912 * as a route_node will not be requeued, if already queued.
1914 * REs are submitted via rib_addnode or rib_delnode which set minimal
1915 * state, or static_install_route (when an existing RE is updated)
1916 * and then submit route_node to queue for best-path selection later.
1917 * Order of add/delete state changes are preserved for any given RE.
1919 * Deleted REs are reaped during best-path selection.
1922 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
1923 * |-------->| | best RE, if required
1925 * static_install->|->rib_addqueue...... -> rib_process
1927 * |-------->| |-> rib_unlink
1928 * |-> set ROUTE_ENTRY_REMOVE |
1929 * rib_delnode (RE freed)
1931 * The 'info' pointer of a route_node points to a rib_dest_t
1932 * ('dest'). Queueing state for a route_node is kept on the dest. The
1933 * dest is created on-demand by rib_link() and is kept around at least
1934 * as long as there are ribs hanging off it (@see rib_gc_dest()).
1936 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1938 * - route_nodes: refcounted by:
1939 * - dest attached to route_node:
1940 * - managed by: rib_link/rib_gc_dest
1941 * - route_node processing queue
1942 * - managed by: rib_addqueue, rib_process.
1946 /* Add RE to head of the route node. */
1947 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
1949 struct route_entry
*head
;
1952 const char *rmap_name
;
1956 dest
= rib_dest_from_rnode(rn
);
1958 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1959 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
1961 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
1962 route_lock_node(rn
); /* rn route table reference */
1967 head
= dest
->routes
;
1974 afi
= (rn
->p
.family
== AF_INET
)
1976 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
1977 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
1978 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
1979 zebra_add_import_table_entry(rn
, re
, rmap_name
);
1984 void rib_addnode(struct route_node
*rn
, struct route_entry
*re
, int process
)
1986 /* RE node has been un-removed before route-node is processed.
1987 * route_node must hence already be on the queue for processing..
1989 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1990 if (IS_ZEBRA_DEBUG_RIB
)
1991 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
1992 (void *)rn
, (void *)re
);
1994 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
1997 rib_link(rn
, re
, process
);
2003 * Detach a rib structure from a route_node.
2005 * Note that a call to rib_unlink() should be followed by a call to
2006 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2007 * longer required to be deleted.
2009 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2015 if (IS_ZEBRA_DEBUG_RIB
)
2016 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2019 dest
= rib_dest_from_rnode(rn
);
2022 re
->next
->prev
= re
->prev
;
2025 re
->prev
->next
= re
->next
;
2027 dest
->routes
= re
->next
;
2030 /* free RE and nexthops */
2031 zebra_deregister_rnh_static_nexthops(re
->vrf_id
, re
->nexthop
, rn
);
2032 nexthops_free(re
->nexthop
);
2033 XFREE(MTYPE_RE
, re
);
2036 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2040 if (IS_ZEBRA_DEBUG_RIB
)
2041 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2042 (void *)rn
, (void *)re
);
2043 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2045 afi
= (rn
->p
.family
== AF_INET
)
2047 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2048 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2049 zebra_del_import_table_entry(rn
, re
);
2050 /* Just clean up if non main table */
2051 if (IS_ZEBRA_DEBUG_RIB
) {
2052 char buf
[SRCDEST2STR_BUFFER
];
2053 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2055 "%u:%s: Freeing route rn %p, re %p (type %d)",
2056 re
->vrf_id
, buf
, rn
, re
, re
->type
);
2065 /* This function dumps the contents of a given RE entry into
2066 * standard debug log. Calling function name and IP prefix in
2067 * question are passed as 1st and 2nd arguments.
2070 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2071 union prefixconstptr src_pp
,
2072 const struct route_entry
*re
)
2074 const struct prefix
*p
= pp
.p
;
2075 const struct prefix
*src_p
= src_pp
.p
;
2076 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2077 char straddr
[PREFIX_STRLEN
];
2078 char srcaddr
[PREFIX_STRLEN
];
2079 struct nexthop
*nexthop
;
2081 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2082 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2083 is_srcdst
? " from " : "",
2084 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2087 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2088 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2091 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2092 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2093 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2094 re
->nexthop_num
, re
->nexthop_active_num
);
2096 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
2097 inet_ntop(p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2098 zlog_debug("%s: %s %s[%u] with flags %s%s%s", func
,
2099 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2101 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2104 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
2107 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2111 zlog_debug("%s: dump complete", func
);
2114 /* This is an exported helper to rtm_read() to dump the strange
2115 * RE entry found by rib_lookup_ipv4_route()
2118 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2120 struct route_table
*table
;
2121 struct route_node
*rn
;
2122 struct route_entry
*re
;
2123 char prefix_buf
[INET_ADDRSTRLEN
];
2126 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2128 zlog_err("%s: zebra_vrf_table() returned NULL", __func__
);
2132 /* Scan the RIB table for exactly matching RE entry. */
2133 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2135 /* No route for this prefix. */
2137 zlog_debug("%s: lookup failed for %s", __func__
,
2138 prefix2str((struct prefix
*)p
, prefix_buf
,
2139 sizeof(prefix_buf
)));
2144 route_unlock_node(rn
);
2147 RNODE_FOREACH_RE (rn
, re
) {
2148 zlog_debug("%s: rn %p, re %p: %s, %s", __func__
, (void *)rn
,
2150 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2153 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2156 route_entry_dump(p
, NULL
, re
);
2160 /* Check if requested address assignment will fail due to another
2161 * route being installed by zebra in FIB already. Take necessary
2162 * actions, if needed: remove such a route from FIB and deSELECT
2163 * corresponding RE entry. Then put affected RN into RIBQ head.
2165 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2167 struct route_table
*table
;
2168 struct route_node
*rn
;
2169 struct route_entry
*re
;
2170 unsigned changed
= 0;
2172 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2173 zlog_err("%s: zebra_vrf_table() returned NULL", __func__
);
2177 /* No matches would be the simplest case. */
2178 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2182 route_unlock_node(rn
);
2184 /* Check all RE entries. In case any changes have to be done, requeue
2185 * the RN into RIBQ head. If the routing message about the new connected
2186 * route (generated by the IP address we are going to assign very soon)
2187 * comes before the RIBQ is processed, the new RE entry will join
2188 * RIBQ record already on head. This is necessary for proper
2190 * of the rest of the RE.
2192 RNODE_FOREACH_RE (rn
, re
) {
2193 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
)
2194 && !RIB_SYSTEM_ROUTE(re
)) {
2196 if (IS_ZEBRA_DEBUG_RIB
) {
2197 char buf
[PREFIX_STRLEN
];
2199 "%u:%s: freeing way for connected prefix",
2201 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2202 route_entry_dump(&rn
->p
, NULL
, re
);
2204 rib_uninstall(rn
, re
);
2211 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2212 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2214 struct route_table
*table
;
2215 struct route_node
*rn
;
2216 struct route_entry
*same
;
2217 struct nexthop
*nexthop
;
2223 assert(!src_p
|| afi
== AFI_IP6
);
2226 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2228 XFREE(MTYPE_RE
, re
);
2232 /* Make it sure prefixlen is applied to the prefix. */
2235 apply_mask_ipv6(src_p
);
2237 /* Set default distance by route type. */
2238 if (re
->distance
== 0) {
2239 re
->distance
= route_distance(re
->type
);
2241 /* iBGP distance is 200. */
2242 if (re
->type
== ZEBRA_ROUTE_BGP
2243 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2247 /* Lookup route node.*/
2248 rn
= srcdest_rnode_get(table
, p
, src_p
);
2250 /* If same type of route are installed, treat it as a implicit
2252 RNODE_FOREACH_RE (rn
, same
) {
2253 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2256 if (same
->type
!= re
->type
)
2258 if (same
->instance
!= re
->instance
)
2260 if (same
->type
== ZEBRA_ROUTE_KERNEL
&&
2261 same
->metric
!= re
->metric
)
2264 * We should allow duplicate connected routes because of
2265 * IPv6 link-local routes and unnumbered interfaces on Linux.
2267 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2271 /* If this route is kernel route, set FIB flag to the route. */
2272 if (RIB_SYSTEM_ROUTE(re
))
2273 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2274 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2276 /* Link new re to node.*/
2277 if (IS_ZEBRA_DEBUG_RIB
) {
2280 "Inserting route rn %p, re %p (type %d) existing %p",
2281 (void *)rn
, (void *)re
, re
->type
, (void *)same
);
2283 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2284 route_entry_dump(p
, src_p
, re
);
2286 rib_addnode(rn
, re
, 1);
2289 /* Free implicit route.*/
2291 rib_delnode(rn
, same
);
2295 route_unlock_node(rn
);
2299 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2300 u_short instance
, int flags
, struct prefix
*p
,
2301 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2302 u_int32_t table_id
, u_int32_t metric
, bool fromkernel
)
2304 struct route_table
*table
;
2305 struct route_node
*rn
;
2306 struct route_entry
*re
;
2307 struct route_entry
*fib
= NULL
;
2308 struct route_entry
*same
= NULL
;
2309 struct nexthop
*rtnh
;
2310 char buf2
[INET6_ADDRSTRLEN
];
2312 assert(!src_p
|| afi
== AFI_IP6
);
2315 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2322 apply_mask_ipv6(src_p
);
2324 /* Lookup route node. */
2325 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2327 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2329 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2330 if (src_p
&& src_p
->prefixlen
)
2331 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2335 if (IS_ZEBRA_DEBUG_RIB
)
2336 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2338 (src_buf
[0] != '\0') ? " from " : "",
2343 /* Lookup same type route. */
2344 RNODE_FOREACH_RE (rn
, re
) {
2345 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2348 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
))
2351 if (re
->type
!= type
)
2353 if (re
->instance
!= instance
)
2355 if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
2356 re
->metric
!= metric
)
2358 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->nexthop
)
2359 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2360 if (rtnh
->ifindex
!= nh
->ifindex
)
2365 /* Make sure that the route found has the same gateway. */
2371 for (ALL_NEXTHOPS(re
->nexthop
, rtnh
))
2372 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2380 /* If same type of route can't be found and this message is from
2384 * In the past(HA!) we could get here because
2385 * we were receiving a route delete from the
2386 * kernel and we're not marking the proto
2387 * as coming from it's appropriate originator.
2388 * Now that we are properly noticing the fact
2389 * that the kernel has deleted our route we
2390 * are not going to get called in this path
2391 * I am going to leave this here because
2392 * this might still work this way on non-linux
2393 * platforms as well as some weird state I have
2394 * not properly thought of yet.
2395 * If we can show that this code path is
2396 * dead then we can remove it.
2398 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
2399 && CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2400 if (IS_ZEBRA_DEBUG_RIB
) {
2403 "rn %p, re %p (type %d) was deleted from kernel, adding",
2404 rn
, fib
, fib
->type
);
2408 for (rtnh
= fib
->nexthop
; rtnh
;
2410 UNSET_FLAG(rtnh
->flags
,
2413 UNSET_FLAG(fib
->status
,
2414 ROUTE_ENTRY_SELECTED_FIB
);
2416 /* This means someone else, other than Zebra,
2418 * a Zebra router from the kernel. We will add
2420 rib_install_kernel(rn
, fib
, NULL
);
2423 if (IS_ZEBRA_DEBUG_RIB
) {
2427 "via %s ifindex %d type %d "
2428 "doesn't exist in rib",
2432 INET_ADDRSTRLEN
), /* FIXME
2438 "type %d doesn't exist in rib",
2441 route_unlock_node(rn
);
2448 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
) &&
2450 rib_install_kernel(rn
, same
, NULL
);
2451 route_unlock_node(rn
);
2455 rib_delnode(rn
, same
);
2458 route_unlock_node(rn
);
2463 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
, u_short instance
,
2464 int flags
, struct prefix
*p
, struct prefix_ipv6
*src_p
,
2465 const struct nexthop
*nh
, u_int32_t table_id
, u_int32_t metric
,
2466 u_int32_t mtu
, u_char distance
)
2468 struct route_entry
*re
;
2469 struct nexthop
*nexthop
;
2471 /* Allocate new route_entry structure. */
2472 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2474 re
->instance
= instance
;
2475 re
->distance
= distance
;
2477 re
->metric
= metric
;
2479 re
->table
= table_id
;
2480 re
->vrf_id
= vrf_id
;
2481 re
->nexthop_num
= 0;
2482 re
->uptime
= time(NULL
);
2485 nexthop
= nexthop_new();
2487 route_entry_nexthop_add(re
, nexthop
);
2489 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2492 /* Schedule routes of a particular table (address-family) based on event. */
2493 static void rib_update_table(struct route_table
*table
,
2494 rib_update_event_t event
)
2496 struct route_node
*rn
;
2497 struct route_entry
*re
, *next
;
2499 /* Walk all routes and queue for processing, if appropriate for
2500 * the trigger event.
2502 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2504 * If we are looking at a route node and the node
2505 * has already been queued we don't
2506 * need to queue it up again
2509 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2510 RIB_ROUTE_ANY_QUEUED
))
2513 case RIB_UPDATE_IF_CHANGE
:
2514 /* Examine all routes that won't get processed by the
2516 * triggered by nexthop evaluation (NHT). This would be
2518 * kernel and certain static routes. Note that NHT will
2520 * triggered upon an interface event as connected routes
2522 * get queued for processing.
2524 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2527 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
&&
2528 re
->type
!= ZEBRA_ROUTE_KERNEL
&&
2529 re
->type
!= ZEBRA_ROUTE_CONNECT
&&
2530 re
->type
!= ZEBRA_ROUTE_STATIC
)
2533 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
2538 for (nh
= re
->nexthop
; nh
; nh
= nh
->next
)
2539 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
2540 || nh
->type
== NEXTHOP_TYPE_IPV6
))
2543 /* If we only have nexthops to a
2552 case RIB_UPDATE_RMAP_CHANGE
:
2553 case RIB_UPDATE_OTHER
:
2554 /* Right now, examine all routes. Can restrict to a
2556 * some cases (TODO).
2558 if (rnode_to_ribs(rn
))
2568 /* RIB update function. */
2569 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
2571 struct route_table
*table
;
2573 /* Process routes of interested address-families. */
2574 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2576 rib_update_table(table
, event
);
2578 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2580 rib_update_table(table
, event
);
2583 /* Remove all routes which comes from non main table. */
2584 static void rib_weed_table(struct route_table
*table
)
2586 struct route_node
*rn
;
2587 struct route_entry
*re
;
2588 struct route_entry
*next
;
2591 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2592 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2593 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2596 if (re
->table
!= zebrad
.rtm_table_default
2597 && re
->table
!= RT_TABLE_MAIN
)
2598 rib_delnode(rn
, re
);
2602 /* Delete all routes from non main table. */
2603 void rib_weed_tables(void)
2606 struct zebra_vrf
*zvrf
;
2608 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2609 if ((zvrf
= vrf
->info
) != NULL
) {
2610 rib_weed_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2611 rib_weed_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2615 /* Delete self installed routes after zebra is relaunched. */
2616 static void rib_sweep_table(struct route_table
*table
)
2618 struct route_node
*rn
;
2619 struct route_entry
*re
;
2620 struct route_entry
*next
;
2621 struct nexthop
*nexthop
;
2627 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2628 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2629 if (IS_ZEBRA_DEBUG_RIB
)
2630 route_entry_dump(&rn
->p
, NULL
, re
);
2632 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2635 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
2639 * So we are starting up and have received
2640 * routes from the kernel that we have installed
2641 * from a previous run of zebra but not cleaned
2642 * up ( say a kill -9 )
2643 * But since we haven't actually installed
2644 * them yet( we received them from the kernel )
2645 * we don't think they are active.
2646 * So let's pretend they are active to actually
2648 * In all honesty I'm not sure if we should
2649 * mark them as active when we receive them
2650 * This is startup only so probably ok.
2652 * If we ever decide to move rib_sweep_table
2653 * to a different spot (ie startup )
2654 * this decision needs to be revisited
2656 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
2657 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2659 ret
= rib_uninstall_kernel(rn
, re
);
2661 rib_delnode(rn
, re
);
2666 /* Sweep all RIB tables. */
2667 void rib_sweep_route(void)
2670 struct zebra_vrf
*zvrf
;
2672 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2673 if ((zvrf
= vrf
->info
) == NULL
)
2676 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2677 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2681 /* Remove specific by protocol routes from 'table'. */
2682 static unsigned long rib_score_proto_table(u_char proto
, u_short instance
,
2683 struct route_table
*table
)
2685 struct route_node
*rn
;
2686 struct route_entry
*re
;
2687 struct route_entry
*next
;
2688 unsigned long n
= 0;
2691 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2692 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2693 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2695 if (re
->type
== proto
2696 && re
->instance
== instance
) {
2697 rib_delnode(rn
, re
);
2704 /* Remove specific by protocol routes. */
2705 unsigned long rib_score_proto(u_char proto
, u_short instance
)
2708 struct zebra_vrf
*zvrf
;
2709 unsigned long cnt
= 0;
2711 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2712 if ((zvrf
= vrf
->info
) != NULL
)
2713 cnt
+= rib_score_proto_table(
2715 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
2716 + rib_score_proto_table(
2718 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2723 /* Close RIB and clean up kernel routes. */
2724 void rib_close_table(struct route_table
*table
)
2726 struct route_node
*rn
;
2727 rib_table_info_t
*info
;
2728 struct route_entry
*re
;
2735 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2736 RNODE_FOREACH_RE (rn
, re
) {
2737 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_SELECTED_FIB
))
2740 if (info
->safi
== SAFI_UNICAST
)
2741 hook_call(rib_update
, rn
, NULL
);
2743 if (!RIB_SYSTEM_ROUTE(re
))
2744 rib_uninstall_kernel(rn
, re
);
2748 /* Routing information base initialize. */
2751 rib_queue_init(&zebrad
);
2757 * Get the first vrf id that is greater than the given vrf id if any.
2759 * Returns TRUE if a vrf id was found, FALSE otherwise.
2761 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
2765 vrf
= vrf_lookup_by_id(vrf_id
);
2767 vrf
= RB_NEXT(vrf_id_head
, vrf
);
2769 *next_id_p
= vrf
->vrf_id
;
2778 * rib_tables_iter_next
2780 * Returns the next table in the iteration.
2782 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
2784 struct route_table
*table
;
2787 * Array that helps us go over all AFI/SAFI combinations via one
2794 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
2795 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
2796 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
2801 switch (iter
->state
) {
2803 case RIB_TABLES_ITER_S_INIT
:
2804 iter
->vrf_id
= VRF_DEFAULT
;
2805 iter
->afi_safi_ix
= -1;
2809 case RIB_TABLES_ITER_S_ITERATING
:
2810 iter
->afi_safi_ix
++;
2813 while (iter
->afi_safi_ix
2814 < (int)ZEBRA_NUM_OF(afi_safis
)) {
2815 table
= zebra_vrf_table(
2816 afi_safis
[iter
->afi_safi_ix
].afi
,
2817 afi_safis
[iter
->afi_safi_ix
].safi
,
2822 iter
->afi_safi_ix
++;
2826 * Found another table in this vrf.
2832 * Done with all tables in the current vrf, go to the
2836 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
2839 iter
->afi_safi_ix
= 0;
2844 case RIB_TABLES_ITER_S_DONE
:
2849 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
2851 iter
->state
= RIB_TABLES_ITER_S_DONE
;