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"
53 #include "zebra/zebra_vxlan.h"
55 DEFINE_HOOK(rib_update
, (struct route_node
* rn
, const char *reason
),
58 /* Should we allow non Quagga processes to delete our routes */
59 extern int allow_delete
;
61 /* Each route type's string and default distance value. */
65 } route_info
[ZEBRA_ROUTE_MAX
] = {
66 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
67 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
68 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
69 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
70 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
71 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
72 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
73 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
74 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
75 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
76 [ZEBRA_ROUTE_PIM
] = {ZEBRA_ROUTE_PIM
, 255},
77 [ZEBRA_ROUTE_EIGRP
] = {ZEBRA_ROUTE_EIGRP
, 90},
78 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10},
79 [ZEBRA_ROUTE_HSLS
] = {ZEBRA_ROUTE_HSLS
, 255},
80 [ZEBRA_ROUTE_OLSR
] = {ZEBRA_ROUTE_OLSR
, 255},
81 [ZEBRA_ROUTE_TABLE
] = {ZEBRA_ROUTE_TABLE
, 150},
82 [ZEBRA_ROUTE_LDP
] = {ZEBRA_ROUTE_LDP
, 150},
83 [ZEBRA_ROUTE_VNC
] = {ZEBRA_ROUTE_VNC
, 20},
84 [ZEBRA_ROUTE_VNC_DIRECT
] = {ZEBRA_ROUTE_VNC_DIRECT
, 20},
85 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = {ZEBRA_ROUTE_VNC_DIRECT_RH
, 20},
86 [ZEBRA_ROUTE_BGP_DIRECT
] = {ZEBRA_ROUTE_BGP_DIRECT
, 20},
87 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = {ZEBRA_ROUTE_BGP_DIRECT_EXT
, 20},
88 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 100},
90 /* no entry/default: 150 */
93 /* RPF lookup behaviour */
94 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
97 static void __attribute__((format(printf
, 5, 6)))
98 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
,
99 int priority
, const char *msgfmt
, ...)
101 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
105 va_start(ap
, msgfmt
);
106 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
110 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
111 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
113 if (info
->safi
== SAFI_MULTICAST
)
114 strcat(buf
, " (MRIB)");
116 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
119 zlog(priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
122 #define rnode_debug(node, vrf_id, ...) \
123 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
124 #define rnode_info(node, ...) \
125 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
127 uint8_t route_distance(int type
)
131 if ((unsigned)type
>= array_size(route_info
))
134 distance
= route_info
[type
].distance
;
139 int is_zebra_valid_kernel_table(u_int32_t table_id
)
141 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
))
145 if ((table_id
== RT_TABLE_UNSPEC
) || (table_id
== RT_TABLE_LOCAL
)
146 || (table_id
== RT_TABLE_COMPAT
))
153 int is_zebra_main_routing_table(u_int32_t table_id
)
155 if ((table_id
== RT_TABLE_MAIN
)
156 || (table_id
== zebrad
.rtm_table_default
))
161 int zebra_check_addr(struct prefix
*p
)
163 if (p
->family
== AF_INET
) {
166 addr
= p
->u
.prefix4
.s_addr
;
169 if (IPV4_NET127(addr
) || IN_CLASSD(addr
)
170 || IPV4_LINKLOCAL(addr
))
173 if (p
->family
== AF_INET6
) {
174 if (IN6_IS_ADDR_LOOPBACK(&p
->u
.prefix6
))
176 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
182 /* Add nexthop to the end of a rib node's nexthop list */
183 void route_entry_nexthop_add(struct route_entry
*re
, struct nexthop
*nexthop
)
185 nexthop_add(&re
->nexthop
, nexthop
);
191 * copy_nexthop - copy a nexthop to the rib structure.
193 void route_entry_copy_nexthops(struct route_entry
*re
, struct nexthop
*nh
)
195 assert(!re
->nexthop
);
196 copy_nexthops(&re
->nexthop
, nh
, NULL
);
197 for (struct nexthop
*nexthop
= nh
; nexthop
; nexthop
= nexthop
->next
)
201 /* Delete specified nexthop from the list. */
202 void route_entry_nexthop_delete(struct route_entry
*re
, struct nexthop
*nexthop
)
205 nexthop
->next
->prev
= nexthop
->prev
;
207 nexthop
->prev
->next
= nexthop
->next
;
209 re
->nexthop
= nexthop
->next
;
214 struct nexthop
*route_entry_nexthop_ifindex_add(struct route_entry
*re
,
217 struct nexthop
*nexthop
;
219 nexthop
= nexthop_new();
220 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
221 nexthop
->ifindex
= ifindex
;
223 route_entry_nexthop_add(re
, nexthop
);
228 struct nexthop
*route_entry_nexthop_ipv4_add(struct route_entry
*re
,
229 struct in_addr
*ipv4
,
232 struct nexthop
*nexthop
;
234 nexthop
= nexthop_new();
235 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
236 nexthop
->gate
.ipv4
= *ipv4
;
238 nexthop
->src
.ipv4
= *src
;
240 route_entry_nexthop_add(re
, nexthop
);
245 struct nexthop
*route_entry_nexthop_ipv4_ifindex_add(struct route_entry
*re
,
246 struct in_addr
*ipv4
,
250 struct nexthop
*nexthop
;
251 struct interface
*ifp
;
253 nexthop
= nexthop_new();
254 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
255 nexthop
->gate
.ipv4
= *ipv4
;
257 nexthop
->src
.ipv4
= *src
;
258 nexthop
->ifindex
= ifindex
;
259 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->nh_vrf_id
);
260 /*Pending: need to think if null ifp here is ok during bootup?
261 There was a crash because ifp here was coming to be NULL */
263 if (connected_is_unnumbered(ifp
) ||
264 CHECK_FLAG(re
->flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
265 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
268 route_entry_nexthop_add(re
, nexthop
);
273 struct nexthop
*route_entry_nexthop_ipv6_add(struct route_entry
*re
,
274 struct in6_addr
*ipv6
)
276 struct nexthop
*nexthop
;
278 nexthop
= nexthop_new();
279 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
280 nexthop
->gate
.ipv6
= *ipv6
;
282 route_entry_nexthop_add(re
, nexthop
);
287 struct nexthop
*route_entry_nexthop_ipv6_ifindex_add(struct route_entry
*re
,
288 struct in6_addr
*ipv6
,
291 struct nexthop
*nexthop
;
293 nexthop
= nexthop_new();
294 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
295 nexthop
->gate
.ipv6
= *ipv6
;
296 nexthop
->ifindex
= ifindex
;
298 route_entry_nexthop_add(re
, nexthop
);
303 struct nexthop
*route_entry_nexthop_blackhole_add(struct route_entry
*re
,
304 enum blackhole_type bh_type
)
306 struct nexthop
*nexthop
;
308 nexthop
= nexthop_new();
309 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
310 nexthop
->bh_type
= bh_type
;
312 route_entry_nexthop_add(re
, nexthop
);
317 static void nexthop_set_resolved(afi_t afi
, struct nexthop
*newhop
,
318 struct nexthop
*nexthop
)
320 struct nexthop
*resolved_hop
;
322 resolved_hop
= nexthop_new();
323 SET_FLAG(resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
325 switch (newhop
->type
) {
326 case NEXTHOP_TYPE_IPV4
:
327 case NEXTHOP_TYPE_IPV4_IFINDEX
:
328 /* If the resolving route specifies a gateway, use it */
329 resolved_hop
->type
= newhop
->type
;
330 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
332 if (newhop
->ifindex
) {
333 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
334 resolved_hop
->ifindex
= newhop
->ifindex
;
335 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
336 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
339 case NEXTHOP_TYPE_IPV6
:
340 case NEXTHOP_TYPE_IPV6_IFINDEX
:
341 resolved_hop
->type
= newhop
->type
;
342 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
344 if (newhop
->ifindex
) {
345 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
346 resolved_hop
->ifindex
= newhop
->ifindex
;
349 case NEXTHOP_TYPE_IFINDEX
:
350 /* If the resolving route is an interface route,
351 * it means the gateway we are looking up is connected
352 * to that interface. (The actual network is _not_ onlink).
353 * Therefore, the resolved route should have the original
354 * gateway as nexthop as it is directly connected.
356 * On Linux, we have to set the onlink netlink flag because
357 * otherwise, the kernel won't accept the route.
359 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
361 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
362 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
363 } else if (afi
== AFI_IP6
) {
364 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
365 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
367 resolved_hop
->ifindex
= newhop
->ifindex
;
369 case NEXTHOP_TYPE_BLACKHOLE
:
370 resolved_hop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
371 resolved_hop
->bh_type
= nexthop
->bh_type
;
375 /* Copy labels of the resolved route */
376 if (newhop
->nh_label
)
377 nexthop_add_labels(resolved_hop
, newhop
->nh_label_type
,
378 newhop
->nh_label
->num_labels
,
379 &newhop
->nh_label
->label
[0]);
381 resolved_hop
->rparent
= nexthop
;
382 nexthop_add(&nexthop
->resolved
, resolved_hop
);
385 /* If force flag is not set, do not modify falgs at all for uninstall
386 the route from FIB. */
387 static int nexthop_active(afi_t afi
, struct route_entry
*re
,
388 struct nexthop
*nexthop
, int set
,
389 struct route_node
*top
)
392 struct route_table
*table
;
393 struct route_node
*rn
;
394 struct route_entry
*match
= NULL
;
396 struct nexthop
*newhop
;
397 struct interface
*ifp
;
400 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
)
401 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
402 nexthop
->ifindex
= 0;
405 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
406 zebra_deregister_rnh_static_nexthops(re
->nh_vrf_id
,
407 nexthop
->resolved
, top
);
408 nexthops_free(nexthop
->resolved
);
409 nexthop
->resolved
= NULL
;
413 /* Skip nexthops that have been filtered out due to route-map */
414 /* The nexthops are specific to this route and so the same */
415 /* nexthop for a different route may not have this flag set */
416 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
420 * Check to see if we should trust the passed in information
421 * for UNNUMBERED interfaces as that we won't find the GW
422 * address in the routing table.
424 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
)) {
425 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->nh_vrf_id
);
426 if (ifp
&& connected_is_unnumbered(ifp
)) {
427 if (if_is_operative(ifp
))
435 /* Make lookup prefix. */
436 memset(&p
, 0, sizeof(struct prefix
));
440 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
441 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
445 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
446 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
449 assert(afi
!= AFI_IP
&& afi
!= AFI_IP6
);
453 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, re
->nh_vrf_id
);
457 rn
= route_node_match(table
, (struct prefix
*)&p
);
459 route_unlock_node(rn
);
461 /* Lookup should halt if we've matched against ourselves ('top',
462 * if specified) - i.e., we cannot have a nexthop NH1 is
463 * resolved by a route NH1. The exception is if the route is a
466 if (top
&& rn
== top
)
467 if (((afi
== AFI_IP
) && (rn
->p
.prefixlen
!= 32)) ||
468 ((afi
== AFI_IP6
) && (rn
->p
.prefixlen
!= 128)))
471 /* Pick up selected route. */
472 /* However, do not resolve over default route unless explicitly
474 if (is_default_prefix(&rn
->p
)
475 && !nh_resolve_via_default(p
.family
))
478 dest
= rib_dest_from_rnode(rn
);
479 if (dest
&& dest
->selected_fib
&&
480 !CHECK_FLAG(dest
->selected_fib
->status
,
481 ROUTE_ENTRY_REMOVED
) &&
482 dest
->selected_fib
->type
!= ZEBRA_ROUTE_TABLE
)
483 match
= dest
->selected_fib
;
485 /* If there is no selected route or matched route is EGP, go up
490 } while (rn
&& rn
->info
== NULL
);
497 if (match
->type
== ZEBRA_ROUTE_CONNECT
) {
498 /* Directly point connected route. */
499 newhop
= match
->nexthop
;
501 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
502 || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
503 nexthop
->ifindex
= newhop
->ifindex
;
506 } else if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_INTERNAL
)) {
508 for (ALL_NEXTHOPS(match
->nexthop
, newhop
)) {
509 if (!CHECK_FLAG(newhop
->flags
,
512 if (CHECK_FLAG(newhop
->flags
,
513 NEXTHOP_FLAG_RECURSIVE
))
517 SET_FLAG(nexthop
->flags
,
518 NEXTHOP_FLAG_RECURSIVE
);
520 ROUTE_ENTRY_NEXTHOPS_CHANGED
);
521 nexthop_set_resolved(afi
, newhop
,
527 re
->nexthop_mtu
= match
->mtu
;
529 } else if (re
->type
== ZEBRA_ROUTE_STATIC
) {
531 for (ALL_NEXTHOPS(match
->nexthop
, newhop
)) {
532 if (!CHECK_FLAG(newhop
->flags
,
537 SET_FLAG(nexthop
->flags
,
538 NEXTHOP_FLAG_RECURSIVE
);
539 nexthop_set_resolved(afi
, newhop
,
545 re
->nexthop_mtu
= match
->mtu
;
554 struct route_entry
*rib_match(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
555 union g_addr
*addr
, struct route_node
**rn_out
)
558 struct route_table
*table
;
559 struct route_node
*rn
;
560 struct route_entry
*match
= NULL
;
561 struct nexthop
*newhop
;
564 table
= zebra_vrf_table(afi
, safi
, vrf_id
);
568 memset(&p
, 0, sizeof(struct prefix
));
571 p
.u
.prefix4
= addr
->ipv4
;
572 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
574 p
.u
.prefix6
= addr
->ipv6
;
575 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
578 rn
= route_node_match(table
, (struct prefix
*)&p
);
583 route_unlock_node(rn
);
585 dest
= rib_dest_from_rnode(rn
);
586 if (dest
&& dest
->selected_fib
&&
587 !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
588 match
= dest
->selected_fib
;
590 /* If there is no selected route or matched route is EGP, go up
595 } while (rn
&& rn
->info
== NULL
);
599 if (match
->type
!= ZEBRA_ROUTE_CONNECT
) {
601 for (ALL_NEXTHOPS(match
->nexthop
, newhop
))
602 if (CHECK_FLAG(newhop
->flags
,
619 struct route_entry
*rib_match_ipv4_multicast(vrf_id_t vrf_id
,
621 struct route_node
**rn_out
)
623 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
624 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
625 union g_addr gaddr
= {.ipv4
= addr
};
627 switch (ipv4_multicast_mode
) {
628 case MCAST_MRIB_ONLY
:
629 return rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
631 case MCAST_URIB_ONLY
:
632 return rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
633 case MCAST_NO_CONFIG
:
634 case MCAST_MIX_MRIB_FIRST
:
635 re
= mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
,
638 re
= ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
,
641 case MCAST_MIX_DISTANCE
:
642 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
643 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
645 re
= ure
->distance
< mre
->distance
? ure
: mre
;
651 case MCAST_MIX_PFXLEN
:
652 mre
= rib_match(AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
653 ure
= rib_match(AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
655 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
664 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
666 if (IS_ZEBRA_DEBUG_RIB
) {
668 inet_ntop(AF_INET
, &addr
, buf
, BUFSIZ
);
670 zlog_debug("%s: %s: found %s, using %s", __func__
, buf
,
671 mre
? (ure
? "MRIB+URIB" : "MRIB")
672 : ure
? "URIB" : "nothing",
673 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
678 void multicast_mode_ipv4_set(enum multicast_mode mode
)
680 if (IS_ZEBRA_DEBUG_RIB
)
681 zlog_debug("%s: multicast lookup mode set (%d)", __func__
,
683 ipv4_multicast_mode
= mode
;
686 enum multicast_mode
multicast_mode_ipv4_get(void)
688 return ipv4_multicast_mode
;
691 struct route_entry
*rib_lookup_ipv4(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
693 struct route_table
*table
;
694 struct route_node
*rn
;
695 struct route_entry
*match
= NULL
;
696 struct nexthop
*nexthop
;
700 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
704 rn
= route_node_lookup(table
, (struct prefix
*)p
);
706 /* No route for this prefix. */
711 route_unlock_node(rn
);
712 dest
= rib_dest_from_rnode(rn
);
714 if (dest
&& dest
->selected_fib
&&
715 !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
716 match
= dest
->selected_fib
;
721 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
724 for (ALL_NEXTHOPS(match
->nexthop
, nexthop
))
725 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
732 * This clone function, unlike its original rib_lookup_ipv4(), checks
733 * if specified IPv4 route record (prefix/mask -> gate) exists in
734 * the whole RIB and has ROUTE_ENTRY_SELECTED_FIB set.
738 * 0: exact match found
739 * 1: a match was found with a different gate
740 * 2: connected route found
741 * 3: no matches found
743 int rib_lookup_ipv4_route(struct prefix_ipv4
*p
, union sockunion
*qgate
,
746 struct route_table
*table
;
747 struct route_node
*rn
;
748 struct route_entry
*match
= NULL
;
749 struct nexthop
*nexthop
;
754 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
756 return ZEBRA_RIB_LOOKUP_ERROR
;
758 /* Scan the RIB table for exactly matching RIB entry. */
759 rn
= route_node_lookup(table
, (struct prefix
*)p
);
761 /* No route for this prefix. */
763 return ZEBRA_RIB_NOTFOUND
;
766 route_unlock_node(rn
);
767 dest
= rib_dest_from_rnode(rn
);
769 /* Find out if a "selected" RR for the discovered RIB entry exists ever.
771 if (dest
&& dest
->selected_fib
&&
772 !CHECK_FLAG(dest
->selected_fib
->status
, ROUTE_ENTRY_REMOVED
))
773 match
= dest
->selected_fib
;
775 /* None such found :( */
777 return ZEBRA_RIB_NOTFOUND
;
779 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
780 return ZEBRA_RIB_FOUND_CONNECTED
;
782 /* Ok, we have a cood candidate, let's check it's nexthop list... */
784 for (ALL_NEXTHOPS(match
->nexthop
, nexthop
))
785 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
787 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip(qgate
))
788 return ZEBRA_RIB_FOUND_EXACT
;
789 if (IS_ZEBRA_DEBUG_RIB
) {
790 char gate_buf
[INET_ADDRSTRLEN
],
791 qgate_buf
[INET_ADDRSTRLEN
];
792 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
.s_addr
,
793 gate_buf
, INET_ADDRSTRLEN
);
794 inet_ntop(AF_INET
, &sockunion2ip(qgate
),
795 qgate_buf
, INET_ADDRSTRLEN
);
796 zlog_debug("%s: qgate == %s, %s == %s",
798 nexthop
->rparent
? "rgate" : "gate",
804 return ZEBRA_RIB_FOUND_NOGATE
;
806 return ZEBRA_RIB_NOTFOUND
;
809 #define RIB_SYSTEM_ROUTE(R) \
810 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
812 /* This function verifies reachability of one given nexthop, which can be
813 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
814 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
815 * nexthop->ifindex will be updated appropriately as well.
816 * An existing route map can turn (otherwise active) nexthop into inactive, but
819 * The return value is the final value of 'ACTIVE' flag.
822 static unsigned nexthop_active_check(struct route_node
*rn
,
823 struct route_entry
*re
,
824 struct nexthop
*nexthop
, int set
)
826 struct interface
*ifp
;
827 route_map_result_t ret
= RMAP_MATCH
;
829 char buf
[SRCDEST2STR_BUFFER
];
830 struct prefix
*p
, *src_p
;
831 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
833 if (rn
->p
.family
== AF_INET
)
835 else if (rn
->p
.family
== AF_INET6
)
839 switch (nexthop
->type
) {
840 case NEXTHOP_TYPE_IFINDEX
:
841 ifp
= if_lookup_by_index(nexthop
->ifindex
, re
->nh_vrf_id
);
842 if (ifp
&& if_is_operative(ifp
))
843 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
845 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
847 case NEXTHOP_TYPE_IPV4
:
848 case NEXTHOP_TYPE_IPV4_IFINDEX
:
850 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_EVPN_RVTEP
))
851 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
852 else if (nexthop_active(AFI_IP
, re
, nexthop
, set
, rn
))
853 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
855 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
857 case NEXTHOP_TYPE_IPV6
:
859 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
860 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
862 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
864 case NEXTHOP_TYPE_IPV6_IFINDEX
:
865 /* RFC 5549, v4 prefix with v6 NH */
866 if (rn
->p
.family
!= AF_INET
)
868 if (IN6_IS_ADDR_LINKLOCAL(&nexthop
->gate
.ipv6
)) {
869 ifp
= if_lookup_by_index(nexthop
->ifindex
,
871 if (ifp
&& if_is_operative(ifp
))
872 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
874 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
876 if (nexthop_active(AFI_IP6
, re
, nexthop
, set
, rn
))
877 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
879 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
882 case NEXTHOP_TYPE_BLACKHOLE
:
883 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
888 if (!CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
891 /* XXX: What exactly do those checks do? Do we support
892 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
893 if (RIB_SYSTEM_ROUTE(re
) || (family
== AFI_IP
&& p
->family
!= AF_INET
)
894 || (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
895 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
897 /* The original code didn't determine the family correctly
898 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
899 * from the rib_table_info in those cases.
900 * Possibly it may be better to use only the rib_table_info
904 rib_table_info_t
*info
;
906 info
= srcdest_rnode_table_info(rn
);
910 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
912 /* It'll get set if required inside */
913 ret
= zebra_route_map_check(family
, re
->type
, p
, nexthop
, re
->nh_vrf_id
,
915 if (ret
== RMAP_DENYMATCH
) {
916 if (IS_ZEBRA_DEBUG_RIB
) {
917 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
919 "%u:%s: Filtering out with NH out %s due to route map",
921 ifindex2ifname(nexthop
->ifindex
,
924 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
926 return CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
929 /* Iterate over all nexthops of the given RIB entry and refresh their
930 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
931 * nexthop is found to toggle the ACTIVE flag, the whole re structure
932 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
933 * transparently passed to nexthop_active_check().
935 * Return value is the new number of active nexthops.
938 static int nexthop_active_update(struct route_node
*rn
, struct route_entry
*re
,
941 struct nexthop
*nexthop
;
942 union g_addr prev_src
;
943 unsigned int prev_active
, new_active
, old_num_nh
;
944 ifindex_t prev_index
;
945 old_num_nh
= re
->nexthop_active_num
;
947 re
->nexthop_active_num
= 0;
948 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
950 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
951 /* No protocol daemon provides src and so we're skipping
953 prev_src
= nexthop
->rmap_src
;
954 prev_active
= CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
955 prev_index
= nexthop
->ifindex
;
956 if ((new_active
= nexthop_active_check(rn
, re
, nexthop
, set
)))
957 re
->nexthop_active_num
++;
958 /* Don't allow src setting on IPv6 addr for now */
959 if (prev_active
!= new_active
|| prev_index
!= nexthop
->ifindex
960 || ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
961 && nexthop
->type
< NEXTHOP_TYPE_IPV6
)
962 && prev_src
.ipv4
.s_addr
963 != nexthop
->rmap_src
.ipv4
.s_addr
)
964 || ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
965 && nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
)
966 && !(IPV6_ADDR_SAME(&prev_src
.ipv6
,
967 &nexthop
->rmap_src
.ipv6
)))) {
968 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
969 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
973 if (old_num_nh
!= re
->nexthop_active_num
)
974 SET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
976 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)) {
977 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
980 return re
->nexthop_active_num
;
984 * Is this RIB labeled-unicast? It must be of type BGP and all paths
985 * (nexthops) must have a label.
987 int zebra_rib_labeled_unicast(struct route_entry
*re
)
989 struct nexthop
*nexthop
= NULL
;
991 if (re
->type
!= ZEBRA_ROUTE_BGP
)
994 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
995 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1001 void kernel_route_rib_pass_fail(struct route_node
*rn
, struct prefix
*p
,
1002 struct route_entry
*re
,
1003 enum southbound_results res
)
1005 struct nexthop
*nexthop
;
1006 char buf
[PREFIX_STRLEN
];
1009 case SOUTHBOUND_INSTALL_SUCCESS
:
1010 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1011 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1014 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1015 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1017 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1019 zsend_route_notify_owner(re
->type
, re
->instance
, re
->vrf_id
,
1020 p
, ZAPI_ROUTE_INSTALLED
);
1022 case SOUTHBOUND_INSTALL_FAILURE
:
1023 zsend_route_notify_owner(re
->type
, re
->instance
, re
->vrf_id
,
1024 p
, ZAPI_ROUTE_FAIL_INSTALL
);
1025 zlog_warn("%u:%s: Route install failed", re
->vrf_id
,
1026 prefix2str(p
, buf
, sizeof(buf
)));
1028 case SOUTHBOUND_DELETE_SUCCESS
:
1029 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
1030 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1032 case SOUTHBOUND_DELETE_FAILURE
:
1033 zlog_warn("%u:%s: Route Deletion failure", re
->vrf_id
,
1034 prefix2str(p
, buf
, sizeof(buf
)));
1039 /* Update flag indicates whether this is a "replace" or not. Currently, this
1040 * is only used for IPv4.
1042 void rib_install_kernel(struct route_node
*rn
, struct route_entry
*re
,
1043 struct route_entry
*old
)
1045 struct nexthop
*nexthop
;
1046 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1047 struct prefix
*p
, *src_p
;
1048 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1050 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1052 if (info
->safi
!= SAFI_UNICAST
) {
1053 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
1054 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1057 struct nexthop
*prev
;
1059 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
1060 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1061 for (ALL_NEXTHOPS(re
->nexthop
, prev
)) {
1062 if (prev
== nexthop
)
1064 if (nexthop_same_firsthop (nexthop
, prev
))
1066 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_DUPLICATE
);
1074 * If this is a replace to a new RE let the originator of the RE
1075 * know that they've lost
1077 if (old
&& old
!= re
)
1078 zsend_route_notify_owner(old
->type
, old
->instance
,
1080 ZAPI_ROUTE_BETTER_ADMIN_WON
);
1083 * Make sure we update the FPM any time we send new information to
1086 hook_call(rib_update
, rn
, "installing in kernel");
1087 kernel_route_rib(rn
, p
, src_p
, old
, re
);
1093 /* Uninstall the route from kernel. */
1094 void rib_uninstall_kernel(struct route_node
*rn
, struct route_entry
*re
)
1096 struct nexthop
*nexthop
;
1097 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1098 struct prefix
*p
, *src_p
;
1099 struct zebra_vrf
*zvrf
= vrf_info_lookup(re
->vrf_id
);
1101 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1103 if (info
->safi
!= SAFI_UNICAST
) {
1104 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
1105 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1110 * Make sure we update the FPM any time we send new information to
1113 hook_call(rib_update
, rn
, "uninstalling from kernel");
1114 kernel_route_rib(rn
, p
, src_p
, re
, NULL
);
1120 /* Uninstall the route from kernel. */
1121 static void rib_uninstall(struct route_node
*rn
, struct route_entry
*re
)
1123 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1124 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1126 if (dest
&& dest
->selected_fib
== re
) {
1127 if (info
->safi
== SAFI_UNICAST
)
1128 hook_call(rib_update
, rn
, "rib_uninstall");
1130 if (!RIB_SYSTEM_ROUTE(re
))
1131 rib_uninstall_kernel(rn
, re
);
1133 /* If labeled-unicast route, uninstall transit LSP. */
1134 if (zebra_rib_labeled_unicast(re
))
1135 zebra_mpls_lsp_uninstall(info
->zvrf
, rn
, re
);
1137 dest
->selected_fib
= NULL
;
1140 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1141 struct prefix
*p
, *src_p
;
1142 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1144 redistribute_delete(p
, src_p
, re
);
1145 UNSET_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
);
1150 * rib_can_delete_dest
1152 * Returns TRUE if the given dest can be deleted from the table.
1154 static int rib_can_delete_dest(rib_dest_t
*dest
)
1161 * Don't delete the dest if we have to update the FPM about this
1164 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)
1165 || CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1174 * Garbage collect the rib dest corresponding to the given route node
1177 * Returns TRUE if the dest was deleted, FALSE otherwise.
1179 int rib_gc_dest(struct route_node
*rn
)
1183 dest
= rib_dest_from_rnode(rn
);
1187 if (!rib_can_delete_dest(dest
))
1190 if (IS_ZEBRA_DEBUG_RIB
) {
1191 struct zebra_vrf
*zvrf
;
1193 zvrf
= rib_dest_vrf(dest
);
1194 rnode_debug(rn
, zvrf_id(zvrf
), "removing dest from table");
1198 XFREE(MTYPE_RIB_DEST
, dest
);
1202 * Release the one reference that we keep on the route node.
1204 route_unlock_node(rn
);
1208 static void rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1209 struct route_entry
*new)
1211 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1213 hook_call(rib_update
, rn
, "new route selected");
1215 /* Update real nexthop. This may actually determine if nexthop is active
1217 if (!nexthop_active_update(rn
, new, 1)) {
1218 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1222 dest
->selected_fib
= new;
1223 if (IS_ZEBRA_DEBUG_RIB
) {
1224 char buf
[SRCDEST2STR_BUFFER
];
1225 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1226 zlog_debug("%u:%s: Adding route rn %p, re %p (type %d)",
1227 zvrf_id(zvrf
), buf
, rn
, new, new->type
);
1230 /* If labeled-unicast route, install transit LSP. */
1231 if (zebra_rib_labeled_unicast(new))
1232 zebra_mpls_lsp_install(zvrf
, rn
, new);
1234 if (!RIB_SYSTEM_ROUTE(new))
1235 rib_install_kernel(rn
, new, NULL
);
1237 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1240 static void rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1241 struct route_entry
*old
)
1243 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1244 hook_call(rib_update
, rn
, "removing existing route");
1246 /* Uninstall from kernel. */
1247 if (IS_ZEBRA_DEBUG_RIB
) {
1248 char buf
[SRCDEST2STR_BUFFER
];
1249 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1250 zlog_debug("%u:%s: Deleting route rn %p, re %p (type %d)",
1251 zvrf_id(zvrf
), buf
, rn
, old
, old
->type
);
1254 /* If labeled-unicast route, uninstall transit LSP. */
1255 if (zebra_rib_labeled_unicast(old
))
1256 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1258 if (!RIB_SYSTEM_ROUTE(old
))
1259 rib_uninstall_kernel(rn
, old
);
1261 dest
->selected_fib
= NULL
;
1263 /* Update nexthop for route, reset changed flag. */
1264 nexthop_active_update(rn
, old
, 1);
1265 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1268 static void rib_process_update_fib(struct zebra_vrf
*zvrf
,
1269 struct route_node
*rn
,
1270 struct route_entry
*old
,
1271 struct route_entry
*new)
1273 struct nexthop
*nexthop
= NULL
;
1276 rib_dest_t
*dest
= rib_dest_from_rnode(rn
);
1279 * We have to install or update if a new route has been selected or
1280 * something has changed.
1282 if (new != old
|| CHECK_FLAG(new->status
, ROUTE_ENTRY_CHANGED
)) {
1283 hook_call(rib_update
, rn
, "updating existing route");
1285 /* Update the nexthop; we could determine here that nexthop is
1287 if (nexthop_active_update(rn
, new, 1))
1290 /* If nexthop is active, install the selected route, if
1292 * the install succeeds, cleanup flags for prior route, if
1297 if (IS_ZEBRA_DEBUG_RIB
) {
1298 char buf
[SRCDEST2STR_BUFFER
];
1299 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1302 "%u:%s: Updating route rn %p, re %p (type %d) "
1304 zvrf_id(zvrf
), buf
, rn
, new,
1305 new->type
, old
, old
->type
);
1308 "%u:%s: Updating route rn %p, re %p (type %d)",
1309 zvrf_id(zvrf
), buf
, rn
, new,
1313 /* If labeled-unicast route, uninstall transit LSP. */
1314 if (zebra_rib_labeled_unicast(old
))
1315 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1317 /* Non-system route should be installed. */
1318 if (!RIB_SYSTEM_ROUTE(new)) {
1319 /* If labeled-unicast route, install transit
1321 if (zebra_rib_labeled_unicast(new))
1322 zebra_mpls_lsp_install(zvrf
, rn
, new);
1324 rib_install_kernel(rn
, new, old
);
1327 /* If install succeeded or system route, cleanup flags
1328 * for prior route. */
1329 if (installed
&& new != old
) {
1330 if (RIB_SYSTEM_ROUTE(new)) {
1331 if (!RIB_SYSTEM_ROUTE(old
))
1332 rib_uninstall_kernel(rn
, old
);
1334 for (nexthop
= old
->nexthop
; nexthop
;
1335 nexthop
= nexthop
->next
)
1336 UNSET_FLAG(nexthop
->flags
,
1341 /* Update for redistribution. */
1343 dest
->selected_fib
= new;
1347 * If nexthop for selected route is not active or install
1349 * may need to uninstall and delete for redistribution.
1351 if (!nh_active
|| !installed
) {
1352 if (IS_ZEBRA_DEBUG_RIB
) {
1353 char buf
[SRCDEST2STR_BUFFER
];
1354 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1357 "%u:%s: Deleting route rn %p, re %p (type %d) "
1358 "old %p (type %d) - %s",
1359 zvrf_id(zvrf
), buf
, rn
, new,
1360 new->type
, old
, old
->type
,
1361 nh_active
? "install failed"
1362 : "nexthop inactive");
1365 "%u:%s: Deleting route rn %p, re %p (type %d) - %s",
1366 zvrf_id(zvrf
), buf
, rn
, new,
1368 nh_active
? "install failed"
1369 : "nexthop inactive");
1372 /* If labeled-unicast route, uninstall transit LSP. */
1373 if (zebra_rib_labeled_unicast(old
))
1374 zebra_mpls_lsp_uninstall(zvrf
, rn
, old
);
1376 if (!RIB_SYSTEM_ROUTE(old
))
1377 rib_uninstall_kernel(rn
, old
);
1378 dest
->selected_fib
= NULL
;
1382 * Same route selected; check if in the FIB and if not,
1384 * is housekeeping code to deal with race conditions in kernel
1386 * netlink reporting interface up before IPv4 or IPv6 protocol
1390 if (!RIB_SYSTEM_ROUTE(new)) {
1393 for (ALL_NEXTHOPS(new->nexthop
, nexthop
))
1394 if (CHECK_FLAG(nexthop
->flags
,
1395 NEXTHOP_FLAG_FIB
)) {
1400 rib_install_kernel(rn
, new, NULL
);
1404 /* Update prior route. */
1406 /* Set real nexthop. */
1407 nexthop_active_update(rn
, old
, 1);
1408 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1411 /* Clear changed flag. */
1412 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1415 /* Check if 'alternate' RIB entry is better than 'current'. */
1416 static struct route_entry
*rib_choose_best(struct route_entry
*current
,
1417 struct route_entry
*alternate
)
1419 if (current
== NULL
)
1422 /* filter route selection in following order:
1423 * - connected beats other types
1424 * - lower distance beats higher
1425 * - lower metric beats higher for equal distance
1426 * - last, hence oldest, route wins tie break.
1429 /* Connected routes. Pick the last connected
1430 * route of the set of lowest metric connected routes.
1432 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
) {
1433 if (current
->type
!= ZEBRA_ROUTE_CONNECT
1434 || alternate
->metric
<= current
->metric
)
1440 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1443 /* higher distance loses */
1444 if (alternate
->distance
< current
->distance
)
1446 if (current
->distance
< alternate
->distance
)
1449 /* metric tie-breaks equal distance */
1450 if (alternate
->metric
<= current
->metric
)
1456 /* Core function for processing routing information base. */
1457 static void rib_process(struct route_node
*rn
)
1459 struct route_entry
*re
;
1460 struct route_entry
*next
;
1461 struct route_entry
*old_selected
= NULL
;
1462 struct route_entry
*new_selected
= NULL
;
1463 struct route_entry
*old_fib
= NULL
;
1464 struct route_entry
*new_fib
= NULL
;
1465 struct route_entry
*best
= NULL
;
1466 char buf
[SRCDEST2STR_BUFFER
];
1468 struct zebra_vrf
*zvrf
= NULL
;
1469 struct prefix
*p
, *src_p
;
1470 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1471 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1475 dest
= rib_dest_from_rnode(rn
);
1477 zvrf
= rib_dest_vrf(dest
);
1478 vrf_id
= zvrf_id(zvrf
);
1481 if (IS_ZEBRA_DEBUG_RIB
)
1482 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1484 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1485 zlog_debug("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1488 * we can have rn's that have a NULL info pointer
1489 * (dest). As such let's not let the deref happen
1490 * additionally we know RNODE_FOREACH_RE_SAFE
1491 * will not iterate so we are ok.
1494 old_fib
= dest
->selected_fib
;
1496 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1497 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1499 "%u:%s: Examine re %p (type %d) status %x flags %x "
1500 "dist %d metric %d",
1501 vrf_id
, buf
, re
, re
->type
, re
->status
,
1502 re
->flags
, re
->distance
, re
->metric
);
1504 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1506 /* Currently selected re. */
1507 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)) {
1508 assert(old_selected
== NULL
);
1512 /* Skip deleted entries from selection */
1513 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
1516 /* Skip unreachable nexthop. */
1517 /* This first call to nexthop_active_update is merely to
1519 * there's any change to nexthops associated with this RIB
1521 * rib_process() can be invoked due to an external event such as
1523 * down or due to next-hop-tracking evaluation. In the latter
1525 * a decision has already been made that the NHs have changed.
1527 * need to invoke a potentially expensive call again. Further,
1529 * the change might be in a recursive NH which is not caught in
1530 * the nexthop_active_update() code. Thus, we might miss changes
1534 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
)
1535 && !nexthop_active_update(rn
, re
, 0)) {
1536 if (re
->type
== ZEBRA_ROUTE_TABLE
) {
1537 /* XXX: HERE BE DRAGONS!!!!!
1538 * In all honesty, I have not yet figured out
1540 * does or why the ROUTE_ENTRY_CHANGED test
1542 * or why we need to delete a route here, and
1544 * this concerns both selected and fib route, or
1547 /* This entry was denied by the 'ip protocol
1548 * table' route-map, we
1549 * need to delete it */
1550 if (re
!= old_selected
) {
1551 if (IS_ZEBRA_DEBUG_RIB
)
1553 "%s: %s: imported via import-table but denied "
1554 "by the ip protocol table route-map",
1558 SET_FLAG(re
->status
,
1559 ROUTE_ENTRY_REMOVED
);
1565 /* Infinite distance. */
1566 if (re
->distance
== DISTANCE_INFINITY
) {
1567 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1571 if (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
)) {
1572 best
= rib_choose_best(new_fib
, re
);
1573 if (new_fib
&& best
!= new_fib
)
1574 UNSET_FLAG(new_fib
->status
,
1575 ROUTE_ENTRY_CHANGED
);
1578 best
= rib_choose_best(new_selected
, re
);
1579 if (new_selected
&& best
!= new_selected
)
1580 UNSET_FLAG(new_selected
->status
,
1581 ROUTE_ENTRY_CHANGED
);
1582 new_selected
= best
;
1585 UNSET_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
);
1586 } /* RNODE_FOREACH_RE */
1588 /* If no FIB override route, use the selected route also for FIB */
1589 if (new_fib
== NULL
)
1590 new_fib
= new_selected
;
1592 /* After the cycle is finished, the following pointers will be set:
1593 * old_selected --- RE entry currently having SELECTED
1594 * new_selected --- RE entry that is newly SELECTED
1595 * old_fib --- RE entry currently in kernel FIB
1596 * new_fib --- RE entry that is newly to be in kernel FIB
1598 * new_selected will get SELECTED flag, and is going to be redistributed
1599 * the zclients. new_fib (which can be new_selected) will be installed
1603 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1605 "%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1606 vrf_id
, buf
, (void *)old_selected
, (void *)new_selected
,
1607 (void *)old_fib
, (void *)new_fib
);
1610 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1611 * fib == selected */
1612 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1613 ROUTE_ENTRY_CHANGED
);
1615 /* Update fib according to selection results */
1616 if (new_fib
&& old_fib
)
1617 rib_process_update_fib(zvrf
, rn
, old_fib
, new_fib
);
1619 rib_process_add_fib(zvrf
, rn
, new_fib
);
1621 rib_process_del_fib(zvrf
, rn
, old_fib
);
1623 /* Redistribute SELECTED entry */
1624 if (old_selected
!= new_selected
|| selected_changed
) {
1625 struct nexthop
*nexthop
;
1627 /* Check if we have a FIB route for the destination, otherwise,
1628 * don't redistribute it */
1629 for (ALL_NEXTHOPS(new_fib
? new_fib
->nexthop
: NULL
, nexthop
)) {
1630 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)) {
1635 new_selected
= NULL
;
1637 if (new_selected
&& new_selected
!= new_fib
) {
1638 nexthop_active_update(rn
, new_selected
, 1);
1639 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1644 redistribute_delete(p
, src_p
, old_selected
);
1645 if (old_selected
!= new_selected
)
1646 UNSET_FLAG(old_selected
->flags
,
1647 ZEBRA_FLAG_SELECTED
);
1651 /* Install new or replace existing redistributed entry
1653 SET_FLAG(new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1654 redistribute_update(p
, src_p
, new_selected
,
1659 /* Remove all RE entries queued for removal */
1660 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
1661 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
1662 if (IS_ZEBRA_DEBUG_RIB
) {
1663 rnode_debug(rn
, vrf_id
, "rn %p, removing re %p",
1664 (void *)rn
, (void *)re
);
1671 * Check if the dest can be deleted now.
1676 /* Take a list of route_node structs and return 1, if there was a record
1677 * picked from it and processed by rib_process(). Don't process more,
1678 * than one RN record; operate only in the specified sub-queue.
1680 static unsigned int process_subq(struct list
*subq
, u_char qindex
)
1682 struct listnode
*lnode
= listhead(subq
);
1683 struct route_node
*rnode
;
1685 struct zebra_vrf
*zvrf
= NULL
;
1690 rnode
= listgetdata(lnode
);
1691 dest
= rib_dest_from_rnode(rnode
);
1693 zvrf
= rib_dest_vrf(dest
);
1697 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
1698 char buf
[SRCDEST2STR_BUFFER
];
1699 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
1700 zlog_debug("%u:%s: rn %p dequeued from sub-queue %u",
1701 zvrf
? zvrf_id(zvrf
) : 0, buf
, rnode
, qindex
);
1705 UNSET_FLAG(rib_dest_from_rnode(rnode
)->flags
,
1706 RIB_ROUTE_QUEUED(qindex
));
1711 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1712 __func__
, rnode
, rnode
->lock
);
1713 zlog_backtrace(LOG_DEBUG
);
1716 route_unlock_node(rnode
);
1717 list_delete_node(subq
, lnode
);
1722 * All meta queues have been processed. Trigger next-hop evaluation.
1724 static void meta_queue_process_complete(struct work_queue
*dummy
)
1727 struct zebra_vrf
*zvrf
;
1729 /* Evaluate nexthops for those VRFs which underwent route processing.
1731 * should limit the evaluation to the necessary VRFs in most common
1734 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
1736 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
1739 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
1740 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET
, 0, RNH_NEXTHOP_TYPE
,
1742 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET
, 0,
1743 RNH_IMPORT_CHECK_TYPE
, NULL
);
1744 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET6
, 0, RNH_NEXTHOP_TYPE
,
1746 zebra_evaluate_rnh(zvrf_id(zvrf
), AF_INET6
, 0,
1747 RNH_IMPORT_CHECK_TYPE
, NULL
);
1750 /* Schedule LSPs for processing, if needed. */
1751 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1752 if (mpls_should_lsps_be_processed(zvrf
)) {
1753 if (IS_ZEBRA_DEBUG_MPLS
)
1755 "%u: Scheduling all LSPs upon RIB completion",
1757 zebra_mpls_lsp_schedule(zvrf
);
1758 mpls_unmark_lsps_for_processing(zvrf
);
1762 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1763 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and
1765 * is pointed to the meta queue structure.
1767 static wq_item_status
meta_queue_process(struct work_queue
*dummy
, void *data
)
1769 struct meta_queue
*mq
= data
;
1772 for (i
= 0; i
< MQ_SIZE
; i
++)
1773 if (process_subq(mq
->subq
[i
], i
)) {
1777 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1781 * Map from rib types to queue type (priority) in meta queue
1783 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1784 [ZEBRA_ROUTE_SYSTEM
] = 4,
1785 [ZEBRA_ROUTE_KERNEL
] = 0,
1786 [ZEBRA_ROUTE_CONNECT
] = 0,
1787 [ZEBRA_ROUTE_STATIC
] = 1,
1788 [ZEBRA_ROUTE_RIP
] = 2,
1789 [ZEBRA_ROUTE_RIPNG
] = 2,
1790 [ZEBRA_ROUTE_OSPF
] = 2,
1791 [ZEBRA_ROUTE_OSPF6
] = 2,
1792 [ZEBRA_ROUTE_ISIS
] = 2,
1793 [ZEBRA_ROUTE_BGP
] = 3,
1794 [ZEBRA_ROUTE_PIM
] = 4, // Shouldn't happen but for safety
1795 [ZEBRA_ROUTE_EIGRP
] = 2,
1796 [ZEBRA_ROUTE_NHRP
] = 2,
1797 [ZEBRA_ROUTE_HSLS
] = 4,
1798 [ZEBRA_ROUTE_OLSR
] = 4,
1799 [ZEBRA_ROUTE_TABLE
] = 1,
1800 [ZEBRA_ROUTE_LDP
] = 4,
1801 [ZEBRA_ROUTE_VNC
] = 3,
1802 [ZEBRA_ROUTE_VNC_DIRECT
] = 3,
1803 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = 3,
1804 [ZEBRA_ROUTE_BGP_DIRECT
] = 3,
1805 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = 3,
1806 [ZEBRA_ROUTE_BABEL
] = 2,
1807 [ZEBRA_ROUTE_ALL
] = 4, // Shouldn't happen but for safety
1810 /* Look into the RN and queue it into one or more priority queues,
1811 * increasing the size for each data push done.
1813 static void rib_meta_queue_add(struct meta_queue
*mq
, struct route_node
*rn
)
1815 struct route_entry
*re
;
1817 RNODE_FOREACH_RE (rn
, re
) {
1818 u_char qindex
= meta_queue_map
[re
->type
];
1819 struct zebra_vrf
*zvrf
;
1821 /* Invariant: at this point we always have rn->info set. */
1822 if (CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
1823 RIB_ROUTE_QUEUED(qindex
))) {
1824 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1827 "rn %p is already queued in sub-queue %u",
1828 (void *)rn
, qindex
);
1832 SET_FLAG(rib_dest_from_rnode(rn
)->flags
,
1833 RIB_ROUTE_QUEUED(qindex
));
1834 listnode_add(mq
->subq
[qindex
], rn
);
1835 route_lock_node(rn
);
1838 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1839 rnode_debug(rn
, re
->vrf_id
,
1840 "queued rn %p into sub-queue %u",
1841 (void *)rn
, qindex
);
1843 zvrf
= zebra_vrf_lookup_by_id(re
->vrf_id
);
1845 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1849 /* Add route_node to work queue and schedule processing */
1850 void rib_queue_add(struct route_node
*rn
)
1854 /* Pointless to queue a route_node with no RIB entries to add or remove
1856 if (!rnode_to_ribs(rn
)) {
1857 zlog_debug("%s: called for route_node (%p, %d) with no ribs",
1858 __func__
, (void *)rn
, rn
->lock
);
1859 zlog_backtrace(LOG_DEBUG
);
1863 if (zebrad
.ribq
== NULL
) {
1864 zlog_err("%s: work_queue does not exist!", __func__
);
1869 * The RIB queue should normally be either empty or holding the only
1870 * work_queue_item element. In the latter case this element would
1871 * hold a pointer to the meta queue structure, which must be used to
1872 * actually queue the route nodes to process. So create the MQ
1873 * holder, if necessary, then push the work into it in any case.
1874 * This semantics was introduced after 0.99.9 release.
1876 if (work_queue_empty(zebrad
.ribq
))
1877 work_queue_add(zebrad
.ribq
, zebrad
.mq
);
1879 rib_meta_queue_add(zebrad
.mq
, rn
);
1884 /* Create new meta queue.
1885 A destructor function doesn't seem to be necessary here.
1887 static struct meta_queue
*meta_queue_new(void)
1889 struct meta_queue
*new;
1892 new = XCALLOC(MTYPE_WORK_QUEUE
, sizeof(struct meta_queue
));
1895 for (i
= 0; i
< MQ_SIZE
; i
++) {
1896 new->subq
[i
] = list_new();
1897 assert(new->subq
[i
]);
1903 void meta_queue_free(struct meta_queue
*mq
)
1907 for (i
= 0; i
< MQ_SIZE
; i
++)
1908 list_delete_and_null(&mq
->subq
[i
]);
1910 XFREE(MTYPE_WORK_QUEUE
, mq
);
1913 /* initialise zebra rib work queue */
1914 static void rib_queue_init(struct zebra_t
*zebra
)
1919 work_queue_new(zebra
->master
, "route_node processing"))) {
1920 zlog_err("%s: could not initialise work queue!", __func__
);
1924 /* fill in the work queue spec */
1925 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1926 zebra
->ribq
->spec
.errorfunc
= NULL
;
1927 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
1928 /* XXX: TODO: These should be runtime configurable via vty */
1929 zebra
->ribq
->spec
.max_retries
= 3;
1930 zebra
->ribq
->spec
.hold
= ZEBRA_RIB_PROCESS_HOLD_TIME
;
1932 if (!(zebra
->mq
= meta_queue_new())) {
1933 zlog_err("%s: could not initialise meta queue!", __func__
);
1939 /* RIB updates are processed via a queue of pointers to route_nodes.
1941 * The queue length is bounded by the maximal size of the routing table,
1942 * as a route_node will not be requeued, if already queued.
1944 * REs are submitted via rib_addnode or rib_delnode which set minimal
1945 * state, or static_install_route (when an existing RE is updated)
1946 * and then submit route_node to queue for best-path selection later.
1947 * Order of add/delete state changes are preserved for any given RE.
1949 * Deleted REs are reaped during best-path selection.
1952 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
1953 * |-------->| | best RE, if required
1955 * static_install->|->rib_addqueue...... -> rib_process
1957 * |-------->| |-> rib_unlink
1958 * |-> set ROUTE_ENTRY_REMOVE |
1959 * rib_delnode (RE freed)
1961 * The 'info' pointer of a route_node points to a rib_dest_t
1962 * ('dest'). Queueing state for a route_node is kept on the dest. The
1963 * dest is created on-demand by rib_link() and is kept around at least
1964 * as long as there are ribs hanging off it (@see rib_gc_dest()).
1966 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1968 * - route_nodes: refcounted by:
1969 * - dest attached to route_node:
1970 * - managed by: rib_link/rib_gc_dest
1971 * - route_node processing queue
1972 * - managed by: rib_addqueue, rib_process.
1976 /* Add RE to head of the route node. */
1977 static void rib_link(struct route_node
*rn
, struct route_entry
*re
, int process
)
1979 struct route_entry
*head
;
1982 const char *rmap_name
;
1986 dest
= rib_dest_from_rnode(rn
);
1988 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1989 rnode_debug(rn
, re
->vrf_id
, "rn %p adding dest", rn
);
1991 dest
= XCALLOC(MTYPE_RIB_DEST
, sizeof(rib_dest_t
));
1992 route_lock_node(rn
); /* rn route table reference */
1997 head
= dest
->routes
;
2004 afi
= (rn
->p
.family
== AF_INET
)
2006 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2007 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2008 rmap_name
= zebra_get_import_table_route_map(afi
, re
->table
);
2009 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2014 void rib_addnode(struct route_node
*rn
, struct route_entry
*re
, int process
)
2016 /* RE node has been un-removed before route-node is processed.
2017 * route_node must hence already be on the queue for processing..
2019 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)) {
2020 if (IS_ZEBRA_DEBUG_RIB
)
2021 rnode_debug(rn
, re
->vrf_id
, "rn %p, un-removed re %p",
2022 (void *)rn
, (void *)re
);
2024 UNSET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2027 rib_link(rn
, re
, process
);
2033 * Detach a rib structure from a route_node.
2035 * Note that a call to rib_unlink() should be followed by a call to
2036 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2037 * longer required to be deleted.
2039 void rib_unlink(struct route_node
*rn
, struct route_entry
*re
)
2045 if (IS_ZEBRA_DEBUG_RIB
)
2046 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
,
2049 dest
= rib_dest_from_rnode(rn
);
2052 re
->next
->prev
= re
->prev
;
2055 re
->prev
->next
= re
->next
;
2057 dest
->routes
= re
->next
;
2060 /* free RE and nexthops */
2061 zebra_deregister_rnh_static_nexthops(re
->vrf_id
, re
->nexthop
, rn
);
2062 nexthops_free(re
->nexthop
);
2063 XFREE(MTYPE_RE
, re
);
2066 void rib_delnode(struct route_node
*rn
, struct route_entry
*re
)
2070 if (IS_ZEBRA_DEBUG_RIB
)
2071 rnode_debug(rn
, re
->vrf_id
, "rn %p, re %p, removing",
2072 (void *)rn
, (void *)re
);
2073 SET_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
);
2075 afi
= (rn
->p
.family
== AF_INET
)
2077 : (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2078 if (is_zebra_import_table_enabled(afi
, re
->table
)) {
2079 zebra_del_import_table_entry(rn
, re
);
2080 /* Just clean up if non main table */
2081 if (IS_ZEBRA_DEBUG_RIB
) {
2082 char buf
[SRCDEST2STR_BUFFER
];
2083 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2085 "%u:%s: Freeing route rn %p, re %p (type %d)",
2086 re
->vrf_id
, buf
, rn
, re
, re
->type
);
2095 /* This function dumps the contents of a given RE entry into
2096 * standard debug log. Calling function name and IP prefix in
2097 * question are passed as 1st and 2nd arguments.
2100 void _route_entry_dump(const char *func
, union prefixconstptr pp
,
2101 union prefixconstptr src_pp
,
2102 const struct route_entry
*re
)
2104 const struct prefix
*p
= pp
.p
;
2105 const struct prefix
*src_p
= src_pp
.p
;
2106 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2107 char straddr
[PREFIX_STRLEN
];
2108 char srcaddr
[PREFIX_STRLEN
];
2109 struct nexthop
*nexthop
;
2111 zlog_debug("%s: dumping RE entry %p for %s%s%s vrf %u", func
,
2112 (const void *)re
, prefix2str(pp
, straddr
, sizeof(straddr
)),
2113 is_srcdst
? " from " : "",
2114 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
))
2117 zlog_debug("%s: uptime == %lu, type == %u, instance == %d, table == %d",
2118 func
, (unsigned long)re
->uptime
, re
->type
, re
->instance
,
2121 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2122 func
, re
->metric
, re
->mtu
, re
->distance
, re
->flags
, re
->status
);
2123 zlog_debug("%s: nexthop_num == %u, nexthop_active_num == %u", func
,
2124 re
->nexthop_num
, re
->nexthop_active_num
);
2126 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
)) {
2127 inet_ntop(p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2128 zlog_debug("%s: %s %s[%u] with flags %s%s%s", func
,
2129 (nexthop
->rparent
? " NH" : "NH"), straddr
,
2131 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
)
2134 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
)
2137 (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
)
2141 zlog_debug("%s: dump complete", func
);
2144 /* This is an exported helper to rtm_read() to dump the strange
2145 * RE entry found by rib_lookup_ipv4_route()
2148 void rib_lookup_and_dump(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2150 struct route_table
*table
;
2151 struct route_node
*rn
;
2152 struct route_entry
*re
;
2153 char prefix_buf
[INET_ADDRSTRLEN
];
2156 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2158 zlog_err("%s: zebra_vrf_table() returned NULL", __func__
);
2162 /* Scan the RIB table for exactly matching RE entry. */
2163 rn
= route_node_lookup(table
, (struct prefix
*)p
);
2165 /* No route for this prefix. */
2167 zlog_debug("%s: lookup failed for %s", __func__
,
2168 prefix2str((struct prefix
*)p
, prefix_buf
,
2169 sizeof(prefix_buf
)));
2174 route_unlock_node(rn
);
2177 RNODE_FOREACH_RE (rn
, re
) {
2178 zlog_debug("%s: rn %p, re %p: %s, %s", __func__
, (void *)rn
,
2180 (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
)
2183 (CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELECTED
)
2186 route_entry_dump(p
, NULL
, re
);
2190 /* Check if requested address assignment will fail due to another
2191 * route being installed by zebra in FIB already. Take necessary
2192 * actions, if needed: remove such a route from FIB and deSELECT
2193 * corresponding RE entry. Then put affected RN into RIBQ head.
2195 void rib_lookup_and_pushup(struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
2197 struct route_table
*table
;
2198 struct route_node
*rn
;
2199 unsigned changed
= 0;
2202 if (NULL
== (table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
))) {
2203 zlog_err("%s: zebra_vrf_table() returned NULL", __func__
);
2207 /* No matches would be the simplest case. */
2208 if (NULL
== (rn
= route_node_lookup(table
, (struct prefix
*)p
)))
2212 route_unlock_node(rn
);
2214 dest
= rib_dest_from_rnode(rn
);
2215 /* Check all RE entries. In case any changes have to be done, requeue
2216 * the RN into RIBQ head. If the routing message about the new connected
2217 * route (generated by the IP address we are going to assign very soon)
2218 * comes before the RIBQ is processed, the new RE entry will join
2219 * RIBQ record already on head. This is necessary for proper
2221 * of the rest of the RE.
2223 if (dest
->selected_fib
&& !RIB_SYSTEM_ROUTE(dest
->selected_fib
)) {
2225 if (IS_ZEBRA_DEBUG_RIB
) {
2226 char buf
[PREFIX_STRLEN
];
2228 zlog_debug("%u:%s: freeing way for connected prefix",
2229 dest
->selected_fib
->vrf_id
,
2230 prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2231 route_entry_dump(&rn
->p
, NULL
, dest
->selected_fib
);
2233 rib_uninstall(rn
, dest
->selected_fib
);
2239 int rib_add_multipath(afi_t afi
, safi_t safi
, struct prefix
*p
,
2240 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2242 struct route_table
*table
;
2243 struct route_node
*rn
;
2244 struct route_entry
*same
;
2245 struct nexthop
*nexthop
;
2251 assert(!src_p
|| afi
== AFI_IP6
);
2254 table
= zebra_vrf_table_with_table_id(afi
, safi
, re
->vrf_id
, re
->table
);
2256 XFREE(MTYPE_RE
, re
);
2260 /* Make it sure prefixlen is applied to the prefix. */
2263 apply_mask_ipv6(src_p
);
2265 /* Set default distance by route type. */
2266 if (re
->distance
== 0) {
2267 re
->distance
= route_distance(re
->type
);
2269 /* iBGP distance is 200. */
2270 if (re
->type
== ZEBRA_ROUTE_BGP
2271 && CHECK_FLAG(re
->flags
, ZEBRA_FLAG_IBGP
))
2275 /* Lookup route node.*/
2276 rn
= srcdest_rnode_get(table
, p
, src_p
);
2278 /* If same type of route are installed, treat it as a implicit
2280 RNODE_FOREACH_RE (rn
, same
) {
2281 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
2284 if (same
->type
!= re
->type
)
2286 if (same
->instance
!= re
->instance
)
2288 if (same
->type
== ZEBRA_ROUTE_KERNEL
&&
2289 same
->metric
!= re
->metric
)
2292 * We should allow duplicate connected routes because of
2293 * IPv6 link-local routes and unnumbered interfaces on Linux.
2295 if (same
->type
!= ZEBRA_ROUTE_CONNECT
)
2299 /* If this route is kernel route, set FIB flag to the route. */
2300 if (RIB_SYSTEM_ROUTE(re
))
2301 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2302 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2304 /* Link new re to node.*/
2305 if (IS_ZEBRA_DEBUG_RIB
) {
2308 "Inserting route rn %p, re %p (type %d) existing %p",
2309 (void *)rn
, (void *)re
, re
->type
, (void *)same
);
2311 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2312 route_entry_dump(p
, src_p
, re
);
2314 rib_addnode(rn
, re
, 1);
2317 /* Free implicit route.*/
2319 rib_delnode(rn
, same
);
2323 route_unlock_node(rn
);
2327 void rib_delete(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2328 u_short instance
, int flags
, struct prefix
*p
,
2329 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2330 u_int32_t table_id
, u_int32_t metric
, bool fromkernel
,
2331 struct ethaddr
*rmac
)
2333 struct route_table
*table
;
2334 struct route_node
*rn
;
2335 struct route_entry
*re
;
2336 struct route_entry
*fib
= NULL
;
2337 struct route_entry
*same
= NULL
;
2338 struct nexthop
*rtnh
;
2339 char buf2
[INET6_ADDRSTRLEN
];
2342 assert(!src_p
|| afi
== AFI_IP6
);
2345 table
= zebra_vrf_table_with_table_id(afi
, safi
, vrf_id
, table_id
);
2352 apply_mask_ipv6(src_p
);
2354 /* Lookup route node. */
2355 rn
= srcdest_rnode_lookup(table
, p
, src_p
);
2357 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2359 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2360 if (src_p
&& src_p
->prefixlen
)
2361 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2365 if (IS_ZEBRA_DEBUG_RIB
)
2366 zlog_debug("%u:%s%s%s doesn't exist in rib", vrf_id
,
2368 (src_buf
[0] != '\0') ? " from " : "",
2373 dest
= rib_dest_from_rnode(rn
);
2374 fib
= dest
->selected_fib
;
2376 /* Lookup same type route. */
2377 RNODE_FOREACH_RE (rn
, re
) {
2378 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2381 if (re
->type
!= type
)
2383 if (re
->instance
!= instance
)
2385 if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
2386 re
->metric
!= metric
)
2388 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (rtnh
= re
->nexthop
)
2389 && rtnh
->type
== NEXTHOP_TYPE_IFINDEX
&& nh
) {
2390 if (rtnh
->ifindex
!= nh
->ifindex
)
2395 /* Make sure that the route found has the same gateway. */
2401 for (ALL_NEXTHOPS(re
->nexthop
, rtnh
))
2402 if (nexthop_same_no_recurse(rtnh
, nh
)) {
2410 /* If same type of route can't be found and this message is from
2414 * In the past(HA!) we could get here because
2415 * we were receiving a route delete from the
2416 * kernel and we're not marking the proto
2417 * as coming from it's appropriate originator.
2418 * Now that we are properly noticing the fact
2419 * that the kernel has deleted our route we
2420 * are not going to get called in this path
2421 * I am going to leave this here because
2422 * this might still work this way on non-linux
2423 * platforms as well as some weird state I have
2424 * not properly thought of yet.
2425 * If we can show that this code path is
2426 * dead then we can remove it.
2428 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
2429 && CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
)) {
2430 if (IS_ZEBRA_DEBUG_RIB
) {
2433 "rn %p, re %p (type %d) was deleted from kernel, adding",
2434 rn
, fib
, fib
->type
);
2438 for (rtnh
= fib
->nexthop
; rtnh
;
2440 UNSET_FLAG(rtnh
->flags
,
2443 dest
->selected_fib
= NULL
;
2445 /* This means someone else, other than Zebra,
2447 * a Zebra router from the kernel. We will add
2449 rib_install_kernel(rn
, fib
, NULL
);
2452 if (IS_ZEBRA_DEBUG_RIB
) {
2456 "via %s ifindex %d type %d "
2457 "doesn't exist in rib",
2461 INET_ADDRSTRLEN
), /* FIXME
2467 "type %d doesn't exist in rib",
2470 route_unlock_node(rn
);
2477 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
) &&
2479 rib_install_kernel(rn
, same
, NULL
);
2480 route_unlock_node(rn
);
2485 if (CHECK_FLAG(flags
, ZEBRA_FLAG_EVPN_ROUTE
)) {
2486 struct nexthop
*tmp_nh
;
2488 for (ALL_NEXTHOPS(re
->nexthop
, tmp_nh
)) {
2489 struct ipaddr vtep_ip
;
2491 memset(&vtep_ip
, 0, sizeof(struct ipaddr
));
2492 vtep_ip
.ipa_type
= IPADDR_V4
;
2493 memcpy(&(vtep_ip
.ipaddr_v4
),
2494 &(tmp_nh
->gate
.ipv4
),
2495 sizeof(struct in_addr
));
2496 zebra_vxlan_evpn_vrf_route_del(re
->vrf_id
, rmac
,
2500 rib_delnode(rn
, same
);
2503 route_unlock_node(rn
);
2508 int rib_add(afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, vrf_id_t nh_vrf_id
,
2509 int type
, u_short instance
, int flags
, struct prefix
*p
,
2510 struct prefix_ipv6
*src_p
, const struct nexthop
*nh
,
2511 u_int32_t table_id
, u_int32_t metric
,
2512 u_int32_t mtu
, uint8_t distance
, route_tag_t tag
)
2514 struct route_entry
*re
;
2515 struct nexthop
*nexthop
;
2517 /* Allocate new route_entry structure. */
2518 re
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
2520 re
->instance
= instance
;
2521 re
->distance
= distance
;
2523 re
->metric
= metric
;
2525 re
->table
= table_id
;
2526 re
->vrf_id
= vrf_id
;
2527 re
->nh_vrf_id
= nh_vrf_id
;
2528 re
->nexthop_num
= 0;
2529 re
->uptime
= time(NULL
);
2533 nexthop
= nexthop_new();
2535 route_entry_nexthop_add(re
, nexthop
);
2537 return rib_add_multipath(afi
, safi
, p
, src_p
, re
);
2540 /* Schedule routes of a particular table (address-family) based on event. */
2541 static void rib_update_table(struct route_table
*table
,
2542 rib_update_event_t event
)
2544 struct route_node
*rn
;
2545 struct route_entry
*re
, *next
;
2547 /* Walk all routes and queue for processing, if appropriate for
2548 * the trigger event.
2550 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2552 * If we are looking at a route node and the node
2553 * has already been queued we don't
2554 * need to queue it up again
2557 && CHECK_FLAG(rib_dest_from_rnode(rn
)->flags
,
2558 RIB_ROUTE_ANY_QUEUED
))
2561 case RIB_UPDATE_IF_CHANGE
:
2562 /* Examine all routes that won't get processed by the
2564 * triggered by nexthop evaluation (NHT). This would be
2566 * kernel and certain static routes. Note that NHT will
2568 * triggered upon an interface event as connected routes
2570 * get queued for processing.
2572 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2575 if (re
->type
!= ZEBRA_ROUTE_SYSTEM
&&
2576 re
->type
!= ZEBRA_ROUTE_KERNEL
&&
2577 re
->type
!= ZEBRA_ROUTE_CONNECT
&&
2578 re
->type
!= ZEBRA_ROUTE_STATIC
)
2581 if (re
->type
!= ZEBRA_ROUTE_STATIC
) {
2586 for (nh
= re
->nexthop
; nh
; nh
= nh
->next
)
2587 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
2588 || nh
->type
== NEXTHOP_TYPE_IPV6
))
2591 /* If we only have nexthops to a
2600 case RIB_UPDATE_RMAP_CHANGE
:
2601 case RIB_UPDATE_OTHER
:
2602 /* Right now, examine all routes. Can restrict to a
2604 * some cases (TODO).
2606 if (rnode_to_ribs(rn
))
2616 /* RIB update function. */
2617 void rib_update(vrf_id_t vrf_id
, rib_update_event_t event
)
2619 struct route_table
*table
;
2621 /* Process routes of interested address-families. */
2622 table
= zebra_vrf_table(AFI_IP
, SAFI_UNICAST
, vrf_id
);
2624 rib_update_table(table
, event
);
2626 table
= zebra_vrf_table(AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2628 rib_update_table(table
, event
);
2631 /* Remove all routes which comes from non main table. */
2632 static void rib_weed_table(struct route_table
*table
)
2634 struct route_node
*rn
;
2635 struct route_entry
*re
;
2636 struct route_entry
*next
;
2639 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2640 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2641 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2644 if (re
->table
!= zebrad
.rtm_table_default
2645 && re
->table
!= RT_TABLE_MAIN
)
2646 rib_delnode(rn
, re
);
2650 /* Delete all routes from non main table. */
2651 void rib_weed_tables(void)
2654 struct zebra_vrf
*zvrf
;
2656 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2657 if ((zvrf
= vrf
->info
) != NULL
) {
2658 rib_weed_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2659 rib_weed_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2663 /* Delete self installed routes after zebra is relaunched. */
2664 static void rib_sweep_table(struct route_table
*table
)
2666 struct route_node
*rn
;
2667 struct route_entry
*re
;
2668 struct route_entry
*next
;
2669 struct nexthop
*nexthop
;
2674 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2675 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2676 if (IS_ZEBRA_DEBUG_RIB
)
2677 route_entry_dump(&rn
->p
, NULL
, re
);
2679 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2682 if (!CHECK_FLAG(re
->flags
, ZEBRA_FLAG_SELFROUTE
))
2686 * So we are starting up and have received
2687 * routes from the kernel that we have installed
2688 * from a previous run of zebra but not cleaned
2689 * up ( say a kill -9 )
2690 * But since we haven't actually installed
2691 * them yet( we received them from the kernel )
2692 * we don't think they are active.
2693 * So let's pretend they are active to actually
2695 * In all honesty I'm not sure if we should
2696 * mark them as active when we receive them
2697 * This is startup only so probably ok.
2699 * If we ever decide to move rib_sweep_table
2700 * to a different spot (ie startup )
2701 * this decision needs to be revisited
2703 for (ALL_NEXTHOPS(re
->nexthop
, nexthop
))
2704 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2706 rib_uninstall_kernel(rn
, re
);
2707 rib_delnode(rn
, re
);
2712 /* Sweep all RIB tables. */
2713 void rib_sweep_route(void)
2716 struct zebra_vrf
*zvrf
;
2718 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
2719 if ((zvrf
= vrf
->info
) == NULL
)
2722 rib_sweep_table(zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2723 rib_sweep_table(zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2727 /* Remove specific by protocol routes from 'table'. */
2728 static unsigned long rib_score_proto_table(u_char proto
, u_short instance
,
2729 struct route_table
*table
)
2731 struct route_node
*rn
;
2732 struct route_entry
*re
;
2733 struct route_entry
*next
;
2734 unsigned long n
= 0;
2737 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
2738 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
2739 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
2741 if (re
->type
== proto
2742 && re
->instance
== instance
) {
2743 rib_delnode(rn
, re
);
2750 /* Remove specific by protocol routes. */
2751 unsigned long rib_score_proto(u_char proto
, u_short instance
)
2754 struct zebra_vrf
*zvrf
;
2755 unsigned long cnt
= 0;
2757 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2758 if ((zvrf
= vrf
->info
) != NULL
)
2759 cnt
+= rib_score_proto_table(
2761 zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
2762 + rib_score_proto_table(
2764 zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2769 /* Close RIB and clean up kernel routes. */
2770 void rib_close_table(struct route_table
*table
)
2772 struct route_node
*rn
;
2773 rib_table_info_t
*info
;
2781 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
)) {
2782 dest
= rib_dest_from_rnode(rn
);
2784 if (dest
&& dest
->selected_fib
) {
2785 if (info
->safi
== SAFI_UNICAST
)
2786 hook_call(rib_update
, rn
, NULL
);
2788 if (!RIB_SYSTEM_ROUTE(dest
->selected_fib
))
2789 rib_uninstall_kernel(rn
, dest
->selected_fib
);
2794 /* Routing information base initialize. */
2797 rib_queue_init(&zebrad
);
2803 * Get the first vrf id that is greater than the given vrf id if any.
2805 * Returns TRUE if a vrf id was found, FALSE otherwise.
2807 static inline int vrf_id_get_next(vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
2811 vrf
= vrf_lookup_by_id(vrf_id
);
2813 vrf
= RB_NEXT(vrf_id_head
, vrf
);
2815 *next_id_p
= vrf
->vrf_id
;
2824 * rib_tables_iter_next
2826 * Returns the next table in the iteration.
2828 struct route_table
*rib_tables_iter_next(rib_tables_iter_t
*iter
)
2830 struct route_table
*table
;
2833 * Array that helps us go over all AFI/SAFI combinations via one
2840 {AFI_IP
, SAFI_UNICAST
}, {AFI_IP
, SAFI_MULTICAST
},
2841 {AFI_IP
, SAFI_LABELED_UNICAST
}, {AFI_IP6
, SAFI_UNICAST
},
2842 {AFI_IP6
, SAFI_MULTICAST
}, {AFI_IP6
, SAFI_LABELED_UNICAST
},
2847 switch (iter
->state
) {
2849 case RIB_TABLES_ITER_S_INIT
:
2850 iter
->vrf_id
= VRF_DEFAULT
;
2851 iter
->afi_safi_ix
= -1;
2855 case RIB_TABLES_ITER_S_ITERATING
:
2856 iter
->afi_safi_ix
++;
2859 while (iter
->afi_safi_ix
2860 < (int)ZEBRA_NUM_OF(afi_safis
)) {
2861 table
= zebra_vrf_table(
2862 afi_safis
[iter
->afi_safi_ix
].afi
,
2863 afi_safis
[iter
->afi_safi_ix
].safi
,
2868 iter
->afi_safi_ix
++;
2872 * Found another table in this vrf.
2878 * Done with all tables in the current vrf, go to the
2882 if (!vrf_id_get_next(iter
->vrf_id
, &iter
->vrf_id
))
2885 iter
->afi_safi_ix
= 0;
2890 case RIB_TABLES_ITER_S_DONE
:
2895 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
2897 iter
->state
= RIB_TABLES_ITER_S_DONE
;