1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "zebra_memory.h"
31 #include "sockunion.h"
34 #include "workqueue.h"
40 #include "srcdest_table.h"
42 #include "zebra/rib.h"
44 #include "zebra/zebra_ns.h"
45 #include "zebra/zserv.h"
46 #include "zebra/zebra_vrf.h"
47 #include "zebra/redistribute.h"
48 #include "zebra/zebra_routemap.h"
49 #include "zebra/debug.h"
50 #include "zebra/zebra_rnh.h"
51 #include "zebra/interface.h"
52 #include "zebra/connected.h"
54 DEFINE_HOOK(rib_update
, (struct route_node
*rn
, const char *reason
), (rn
, reason
))
56 /* Should we allow non Quagga processes to delete our routes */
57 extern int allow_delete
;
59 /* Hold time for RIB process, should be very minimal.
60 * it is useful to able to set it otherwise for testing, hence exported
61 * as global here for test-rig code.
63 int rib_process_hold_time
= 10;
65 /* Each route type's string and default distance value. */
70 } route_info
[ZEBRA_ROUTE_MAX
] =
72 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
73 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
74 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
75 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
76 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
77 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
78 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
79 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
80 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
81 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
82 [ZEBRA_ROUTE_NHRP
] = {ZEBRA_ROUTE_NHRP
, 10},
83 /* no entry/default: 150 */
86 /* RPF lookup behaviour */
87 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
90 static void __attribute__((format (printf
, 5, 6)))
91 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
, int priority
,
92 const char *msgfmt
, ...)
94 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
99 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
104 rib_table_info_t
*info
= srcdest_rnode_table_info (rn
);
105 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
107 if (info
->safi
== SAFI_MULTICAST
)
108 strcat(buf
, " (MRIB)");
112 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
115 zlog (priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
118 #define rnode_debug(node, vrf_id, ...) \
119 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
120 #define rnode_info(node, ...) \
121 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
124 route_distance (int type
)
128 if ((unsigned)type
>= array_size(route_info
))
131 distance
= route_info
[type
].distance
;
137 is_zebra_valid_kernel_table(u_int32_t table_id
)
139 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
))
143 if ((table_id
== RT_TABLE_UNSPEC
) ||
144 (table_id
== RT_TABLE_LOCAL
) ||
145 (table_id
== RT_TABLE_COMPAT
))
153 is_zebra_main_routing_table(u_int32_t table_id
)
155 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
161 zebra_check_addr (struct prefix
*p
)
163 if (p
->family
== AF_INET
)
167 addr
= p
->u
.prefix4
.s_addr
;
170 if (IPV4_NET127 (addr
)
172 || IPV4_LINKLOCAL(addr
))
175 if (p
->family
== AF_INET6
)
177 if (IN6_IS_ADDR_LOOPBACK (&p
->u
.prefix6
))
179 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
185 /* Add nexthop to the end of a rib node's nexthop list */
187 route_entry_nexthop_add (struct route_entry
*re
, struct nexthop
*nexthop
)
189 nexthop_add(&re
->nexthop
, nexthop
);
196 * copy_nexthop - copy a nexthop to the rib structure.
199 route_entry_copy_nexthops (struct route_entry
*re
, struct nexthop
*nh
)
201 struct nexthop
*nexthop
;
203 nexthop
= nexthop_new();
204 nexthop
->flags
= nh
->flags
;
205 nexthop
->type
= nh
->type
;
206 nexthop
->ifindex
= nh
->ifindex
;
207 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
208 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
210 nexthop_add_labels (nexthop
, nh
->nh_label_type
, nh
->nh_label
->num_labels
,
211 &nh
->nh_label
->label
[0]);
212 route_entry_nexthop_add(re
, nexthop
);
213 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
214 copy_nexthops(&nexthop
->resolved
, nh
->resolved
);
217 /* Delete specified nexthop from the list. */
219 route_entry_nexthop_delete (struct route_entry
*re
, struct nexthop
*nexthop
)
222 nexthop
->next
->prev
= nexthop
->prev
;
224 nexthop
->prev
->next
= nexthop
->next
;
226 re
->nexthop
= nexthop
->next
;
233 route_entry_nexthop_ifindex_add (struct route_entry
*re
, ifindex_t ifindex
)
235 struct nexthop
*nexthop
;
237 nexthop
= nexthop_new();
238 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
239 nexthop
->ifindex
= ifindex
;
241 route_entry_nexthop_add (re
, nexthop
);
247 route_entry_nexthop_ipv4_add (struct route_entry
*re
, struct in_addr
*ipv4
, struct in_addr
*src
)
249 struct nexthop
*nexthop
;
251 nexthop
= nexthop_new();
252 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
253 nexthop
->gate
.ipv4
= *ipv4
;
255 nexthop
->src
.ipv4
= *src
;
257 route_entry_nexthop_add (re
, nexthop
);
263 route_entry_nexthop_ipv4_ifindex_add (struct route_entry
*re
, struct in_addr
*ipv4
,
264 struct in_addr
*src
, ifindex_t ifindex
)
266 struct nexthop
*nexthop
;
267 struct interface
*ifp
;
269 nexthop
= nexthop_new();
270 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
271 nexthop
->gate
.ipv4
= *ipv4
;
273 nexthop
->src
.ipv4
= *src
;
274 nexthop
->ifindex
= ifindex
;
275 ifp
= if_lookup_by_index (nexthop
->ifindex
, VRF_DEFAULT
);
276 /*Pending: need to think if null ifp here is ok during bootup?
277 There was a crash because ifp here was coming to be NULL */
279 if (connected_is_unnumbered(ifp
)) {
280 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
283 route_entry_nexthop_add (re
, nexthop
);
289 route_entry_nexthop_ipv6_add (struct route_entry
*re
, struct in6_addr
*ipv6
)
291 struct nexthop
*nexthop
;
293 nexthop
= nexthop_new();
294 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
295 nexthop
->gate
.ipv6
= *ipv6
;
297 route_entry_nexthop_add (re
, nexthop
);
303 route_entry_nexthop_ipv6_ifindex_add (struct route_entry
*re
, struct in6_addr
*ipv6
,
306 struct nexthop
*nexthop
;
308 nexthop
= nexthop_new();
309 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
310 nexthop
->gate
.ipv6
= *ipv6
;
311 nexthop
->ifindex
= ifindex
;
313 route_entry_nexthop_add (re
, nexthop
);
319 route_entry_nexthop_blackhole_add (struct route_entry
*re
)
321 struct nexthop
*nexthop
;
323 nexthop
= nexthop_new();
324 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
325 SET_FLAG (re
->flags
, ZEBRA_FLAG_BLACKHOLE
);
327 route_entry_nexthop_add (re
, nexthop
);
333 nexthop_set_resolved (afi_t afi
, struct nexthop
*newhop
, struct nexthop
*nexthop
)
335 struct nexthop
*resolved_hop
;
337 resolved_hop
= nexthop_new();
338 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
339 /* If the resolving route specifies a gateway, use it */
340 if (newhop
->type
== NEXTHOP_TYPE_IPV4
341 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
343 resolved_hop
->type
= newhop
->type
;
344 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
348 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
349 resolved_hop
->ifindex
= newhop
->ifindex
;
350 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
351 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
354 if (newhop
->type
== NEXTHOP_TYPE_IPV6
355 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
357 resolved_hop
->type
= newhop
->type
;
358 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
362 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
363 resolved_hop
->ifindex
= newhop
->ifindex
;
367 /* If the resolving route is an interface route,
368 * it means the gateway we are looking up is connected
369 * to that interface. (The actual network is _not_ onlink).
370 * Therefore, the resolved route should have the original
371 * gateway as nexthop as it is directly connected.
373 * On Linux, we have to set the onlink netlink flag because
374 * otherwise, the kernel won't accept the route.
376 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
378 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
381 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
382 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
384 else if (afi
== AFI_IP6
)
386 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
387 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
389 resolved_hop
->ifindex
= newhop
->ifindex
;
392 nexthop_add(&nexthop
->resolved
, resolved_hop
);
395 /* If force flag is not set, do not modify falgs at all for uninstall
396 the route from FIB. */
398 nexthop_active (afi_t afi
, struct route_entry
*re
, struct nexthop
*nexthop
, int set
,
399 struct route_node
*top
)
402 struct route_table
*table
;
403 struct route_node
*rn
;
404 struct route_entry
*match
;
406 struct nexthop
*newhop
, *tnewhop
;
408 struct interface
*ifp
;
410 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
) || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
411 nexthop
->ifindex
= 0;
415 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
416 zebra_deregister_rnh_static_nexthops(re
->vrf_id
, nexthop
->resolved
, top
);
417 nexthops_free(nexthop
->resolved
);
418 nexthop
->resolved
= NULL
;
422 /* Skip nexthops that have been filtered out due to route-map */
423 /* The nexthops are specific to this route and so the same */
424 /* nexthop for a different route may not have this flag set */
425 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
429 * Check to see if we should trust the passed in information
430 * for UNNUMBERED interfaces as that we won't find the GW
431 * address in the routing table.
433 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
435 ifp
= if_lookup_by_index (nexthop
->ifindex
, VRF_DEFAULT
);
436 if (ifp
&& connected_is_unnumbered(ifp
))
438 if (if_is_operative(ifp
))
447 /* Make lookup prefix. */
448 memset (&p
, 0, sizeof (struct prefix
));
453 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
454 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
458 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
459 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
462 assert (afi
!= AFI_IP
&& afi
!= AFI_IP6
);
466 table
= zebra_vrf_table (afi
, SAFI_UNICAST
, re
->vrf_id
);
470 rn
= route_node_match (table
, (struct prefix
*) &p
);
473 route_unlock_node (rn
);
475 /* If lookup self prefix return immediately. */
479 /* Pick up selected route. */
480 /* However, do not resolve over default route unless explicitly allowed. */
481 if (is_default_prefix (&rn
->p
) &&
482 !nh_resolve_via_default (p
.family
))
485 RNODE_FOREACH_RE (rn
, match
)
487 if (CHECK_FLAG (match
->status
, ROUTE_ENTRY_REMOVED
))
490 /* if the next hop is imported from another table, skip it */
491 if (match
->type
== ZEBRA_ROUTE_TABLE
)
493 if (CHECK_FLAG (match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
497 /* If there is no selected route or matched route is EGP, go up
503 } while (rn
&& rn
->info
== NULL
);
505 route_lock_node (rn
);
510 /* If the longest prefix match for the nexthop yields
511 * a blackhole, mark it as inactive. */
512 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
513 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
516 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
518 /* Directly point connected route. */
519 newhop
= match
->nexthop
;
522 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
||
523 nexthop
->type
== NEXTHOP_TYPE_IPV6
)
524 nexthop
->ifindex
= newhop
->ifindex
;
528 else if (CHECK_FLAG (re
->flags
, ZEBRA_FLAG_INTERNAL
))
531 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
532 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
533 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
537 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
538 SET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
540 nexthop_set_resolved(afi
, newhop
, nexthop
);
546 else if (re
->type
== ZEBRA_ROUTE_STATIC
)
549 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
550 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
554 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
556 nexthop_set_resolved(afi
, newhop
, nexthop
);
561 re
->nexthop_mtu
= match
->mtu
;
573 rib_match (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
574 union g_addr
*addr
, struct route_node
**rn_out
)
577 struct route_table
*table
;
578 struct route_node
*rn
;
579 struct route_entry
*match
;
580 struct nexthop
*newhop
, *tnewhop
;
584 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
588 memset (&p
, 0, sizeof (struct prefix
));
592 p
.u
.prefix4
= addr
->ipv4
;
593 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
597 p
.u
.prefix6
= addr
->ipv6
;
598 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
601 rn
= route_node_match (table
, (struct prefix
*) &p
);
605 route_unlock_node (rn
);
607 /* Pick up selected route. */
608 RNODE_FOREACH_RE (rn
, match
)
610 if (CHECK_FLAG (match
->status
, ROUTE_ENTRY_REMOVED
))
612 if (CHECK_FLAG (match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
616 /* If there is no selected route or matched route is EGP, go up
622 } while (rn
&& rn
->info
== NULL
);
624 route_lock_node (rn
);
628 if (match
->type
!= ZEBRA_ROUTE_CONNECT
)
631 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
632 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
650 rib_match_ipv4_multicast (vrf_id_t vrf_id
, struct in_addr addr
, struct route_node
**rn_out
)
652 struct route_entry
*re
= NULL
, *mre
= NULL
, *ure
= NULL
;
653 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
654 union g_addr gaddr
= { .ipv4
= addr
};
656 switch (ipv4_multicast_mode
)
658 case MCAST_MRIB_ONLY
:
659 return rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, rn_out
);
660 case MCAST_URIB_ONLY
:
661 return rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
662 case MCAST_NO_CONFIG
:
663 case MCAST_MIX_MRIB_FIRST
:
664 re
= mre
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
666 re
= ure
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
668 case MCAST_MIX_DISTANCE
:
669 mre
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
670 ure
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
672 re
= ure
->distance
< mre
->distance
? ure
: mre
;
678 case MCAST_MIX_PFXLEN
:
679 mre
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
680 ure
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
682 re
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? ure
: mre
;
691 *rn_out
= (re
== mre
) ? m_rn
: u_rn
;
693 if (IS_ZEBRA_DEBUG_RIB
)
696 inet_ntop (AF_INET
, &addr
, buf
, BUFSIZ
);
698 zlog_debug("%s: %s: found %s, using %s",
700 mre
? (ure
? "MRIB+URIB" : "MRIB") :
701 ure
? "URIB" : "nothing",
702 re
== ure
? "URIB" : re
== mre
? "MRIB" : "none");
708 multicast_mode_ipv4_set (enum multicast_mode mode
)
710 if (IS_ZEBRA_DEBUG_RIB
)
711 zlog_debug("%s: multicast lookup mode set (%d)", __func__
, mode
);
712 ipv4_multicast_mode
= mode
;
716 multicast_mode_ipv4_get (void)
718 return ipv4_multicast_mode
;
722 rib_lookup_ipv4 (struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
724 struct route_table
*table
;
725 struct route_node
*rn
;
726 struct route_entry
*match
;
727 struct nexthop
*nexthop
, *tnexthop
;
731 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
735 rn
= route_node_lookup (table
, (struct prefix
*) p
);
737 /* No route for this prefix. */
742 route_unlock_node (rn
);
744 RNODE_FOREACH_RE (rn
, match
)
746 if (CHECK_FLAG (match
->status
, ROUTE_ENTRY_REMOVED
))
748 if (CHECK_FLAG (match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
755 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
758 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
759 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
766 * This clone function, unlike its original rib_lookup_ipv4(), checks
767 * if specified IPv4 route record (prefix/mask -> gate) exists in
768 * the whole RIB and has ROUTE_ENTRY_SELECTED_FIB set.
772 * 0: exact match found
773 * 1: a match was found with a different gate
774 * 2: connected route found
775 * 3: no matches found
778 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
,
781 struct route_table
*table
;
782 struct route_node
*rn
;
783 struct route_entry
*match
;
784 struct nexthop
*nexthop
, *tnexthop
;
789 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
791 return ZEBRA_RIB_LOOKUP_ERROR
;
793 /* Scan the RIB table for exactly matching RIB entry. */
794 rn
= route_node_lookup (table
, (struct prefix
*) p
);
796 /* No route for this prefix. */
798 return ZEBRA_RIB_NOTFOUND
;
801 route_unlock_node (rn
);
803 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
804 RNODE_FOREACH_RE (rn
, match
)
806 if (CHECK_FLAG (match
->status
, ROUTE_ENTRY_REMOVED
))
808 if (CHECK_FLAG (match
->status
, ROUTE_ENTRY_SELECTED_FIB
))
812 /* None such found :( */
814 return ZEBRA_RIB_NOTFOUND
;
816 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
817 return ZEBRA_RIB_FOUND_CONNECTED
;
819 /* Ok, we have a cood candidate, let's check it's nexthop list... */
821 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
822 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
825 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip (qgate
))
826 return ZEBRA_RIB_FOUND_EXACT
;
827 if (IS_ZEBRA_DEBUG_RIB
)
829 char gate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
830 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
831 inet_ntop (AF_INET
, &sockunion2ip(qgate
), qgate_buf
, INET_ADDRSTRLEN
);
832 zlog_debug ("%s: qgate == %s, %s == %s", __func__
,
833 qgate_buf
, recursing
? "rgate" : "gate", gate_buf
);
838 return ZEBRA_RIB_FOUND_NOGATE
;
840 return ZEBRA_RIB_NOTFOUND
;
843 #define RIB_SYSTEM_ROUTE(R) \
844 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
846 /* This function verifies reachability of one given nexthop, which can be
847 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
848 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
849 * nexthop->ifindex will be updated appropriately as well.
850 * An existing route map can turn (otherwise active) nexthop into inactive, but
853 * The return value is the final value of 'ACTIVE' flag.
857 nexthop_active_check (struct route_node
*rn
, struct route_entry
*re
,
858 struct nexthop
*nexthop
, int set
)
860 struct interface
*ifp
;
861 route_map_result_t ret
= RMAP_MATCH
;
863 char buf
[SRCDEST2STR_BUFFER
];
864 struct prefix
*p
, *src_p
;
865 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
867 if (rn
->p
.family
== AF_INET
)
869 else if (rn
->p
.family
== AF_INET6
)
873 switch (nexthop
->type
)
875 case NEXTHOP_TYPE_IFINDEX
:
876 ifp
= if_lookup_by_index (nexthop
->ifindex
, re
->vrf_id
);
877 if (ifp
&& if_is_operative(ifp
))
878 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
880 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
882 case NEXTHOP_TYPE_IPV4
:
883 case NEXTHOP_TYPE_IPV4_IFINDEX
:
885 if (nexthop_active (AFI_IP
, re
, nexthop
, set
, rn
))
886 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
888 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
890 case NEXTHOP_TYPE_IPV6
:
892 if (nexthop_active (AFI_IP6
, re
, nexthop
, set
, rn
))
893 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
895 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
897 case NEXTHOP_TYPE_IPV6_IFINDEX
:
898 /* RFC 5549, v4 prefix with v6 NH */
899 if (rn
->p
.family
!= AF_INET
)
901 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
903 ifp
= if_lookup_by_index (nexthop
->ifindex
, re
->vrf_id
);
904 if (ifp
&& if_is_operative(ifp
))
905 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
907 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
911 if (nexthop_active (AFI_IP6
, re
, nexthop
, set
, rn
))
912 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
914 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
917 case NEXTHOP_TYPE_BLACKHOLE
:
918 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
923 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
926 /* XXX: What exactly do those checks do? Do we support
927 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
928 if (RIB_SYSTEM_ROUTE(re
) ||
929 (family
== AFI_IP
&& p
->family
!= AF_INET
) ||
930 (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
931 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
933 /* The original code didn't determine the family correctly
934 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
935 * from the rib_table_info in those cases.
936 * Possibly it may be better to use only the rib_table_info
941 rib_table_info_t
*info
;
943 info
= srcdest_rnode_table_info(rn
);
947 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
949 /* It'll get set if required inside */
950 ret
= zebra_route_map_check(family
, re
->type
, p
, nexthop
, re
->vrf_id
,
952 if (ret
== RMAP_DENYMATCH
)
954 if (IS_ZEBRA_DEBUG_RIB
)
956 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
957 zlog_debug("%u:%s: Filtering out with NH out %s due to route map",
959 ifindex2ifname (nexthop
->ifindex
, re
->vrf_id
));
961 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
963 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
966 /* Iterate over all nexthops of the given RIB entry and refresh their
967 * ACTIVE flag. re->nexthop_active_num is updated accordingly. If any
968 * nexthop is found to toggle the ACTIVE flag, the whole re structure
969 * is flagged with ROUTE_ENTRY_CHANGED. The 4th 'set' argument is
970 * transparently passed to nexthop_active_check().
972 * Return value is the new number of active nexthops.
976 nexthop_active_update (struct route_node
*rn
, struct route_entry
*re
, int set
)
978 struct nexthop
*nexthop
;
979 union g_addr prev_src
;
980 unsigned int prev_active
, new_active
, old_num_nh
;
981 ifindex_t prev_index
;
982 old_num_nh
= re
->nexthop_active_num
;
984 re
->nexthop_active_num
= 0;
985 UNSET_FLAG (re
->status
, ROUTE_ENTRY_CHANGED
);
987 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
989 /* No protocol daemon provides src and so we're skipping tracking it */
990 prev_src
= nexthop
->rmap_src
;
991 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
992 prev_index
= nexthop
->ifindex
;
993 if ((new_active
= nexthop_active_check (rn
, re
, nexthop
, set
)))
994 re
->nexthop_active_num
++;
995 /* Don't allow src setting on IPv6 addr for now */
996 if (prev_active
!= new_active
||
997 prev_index
!= nexthop
->ifindex
||
998 ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
&&
999 nexthop
->type
< NEXTHOP_TYPE_IPV6
) &&
1000 prev_src
.ipv4
.s_addr
!= nexthop
->rmap_src
.ipv4
.s_addr
) ||
1001 ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
&&
1002 nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
) &&
1003 !(IPV6_ADDR_SAME (&prev_src
.ipv6
, &nexthop
->rmap_src
.ipv6
))))
1005 SET_FLAG (re
->status
, ROUTE_ENTRY_CHANGED
);
1006 SET_FLAG (re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1010 if (old_num_nh
!= re
->nexthop_active_num
)
1011 SET_FLAG (re
->status
, ROUTE_ENTRY_CHANGED
);
1013 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_CHANGED
))
1015 SET_FLAG (re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1018 return re
->nexthop_active_num
;
1022 * Is this RIB labeled-unicast? It must be of type BGP and all paths
1023 * (nexthops) must have a label.
1026 zebra_rib_labeled_unicast (struct route_entry
*re
)
1028 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1031 if (re
->type
!= ZEBRA_ROUTE_BGP
)
1034 for (ALL_NEXTHOPS_RO(re
->nexthop
, nexthop
, tnexthop
, recursing
))
1035 if (!nexthop
->nh_label
|| !nexthop
->nh_label
->num_labels
)
1041 /* Update flag indicates whether this is a "replace" or not. Currently, this
1042 * is only used for IPv4.
1045 rib_install_kernel (struct route_node
*rn
, struct route_entry
*re
, struct route_entry
*old
)
1048 struct nexthop
*nexthop
, *tnexthop
;
1049 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1051 struct prefix
*p
, *src_p
;
1052 struct zebra_vrf
*zvrf
= vrf_info_lookup (re
->vrf_id
);
1054 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
1056 if (info
->safi
!= SAFI_UNICAST
)
1058 for (ALL_NEXTHOPS_RO(re
->nexthop
, nexthop
, tnexthop
, recursing
))
1059 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1064 * Make sure we update the FPM any time we send new information to
1067 hook_call(rib_update
, rn
, "installing in kernel");
1068 ret
= kernel_route_rib (p
, src_p
, old
, re
);
1071 /* If install succeeds, update FIB flag for nexthops. */
1074 for (ALL_NEXTHOPS_RO(re
->nexthop
, nexthop
, tnexthop
, recursing
))
1076 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1079 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1080 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1082 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1089 /* Uninstall the route from kernel. */
1091 rib_uninstall_kernel (struct route_node
*rn
, struct route_entry
*re
)
1094 struct nexthop
*nexthop
, *tnexthop
;
1095 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1097 struct prefix
*p
, *src_p
;
1098 struct zebra_vrf
*zvrf
= vrf_info_lookup (re
->vrf_id
);
1100 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
1102 if (info
->safi
!= SAFI_UNICAST
)
1104 for (ALL_NEXTHOPS_RO(re
->nexthop
, nexthop
, tnexthop
, recursing
))
1105 SET_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 ret
= kernel_route_rib (p
, src_p
, re
, NULL
);
1117 for (ALL_NEXTHOPS_RO(re
->nexthop
, nexthop
, tnexthop
, recursing
))
1118 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1123 /* Uninstall the route from kernel. */
1125 rib_uninstall (struct route_node
*rn
, struct route_entry
*re
)
1127 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1129 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_SELECTED_FIB
))
1131 if (info
->safi
== SAFI_UNICAST
)
1132 hook_call(rib_update
, rn
, "rib_uninstall");
1134 if (! RIB_SYSTEM_ROUTE (re
))
1135 rib_uninstall_kernel (rn
, re
);
1137 /* If labeled-unicast route, uninstall transit LSP. */
1138 if (zebra_rib_labeled_unicast (re
))
1139 zebra_mpls_lsp_uninstall (info
->zvrf
, rn
, re
);
1141 UNSET_FLAG (re
->status
, ROUTE_ENTRY_SELECTED_FIB
);
1144 if (CHECK_FLAG (re
->flags
, ZEBRA_FLAG_SELECTED
))
1146 struct prefix
*p
, *src_p
;
1147 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
1149 redistribute_delete (p
, src_p
, re
);
1150 UNSET_FLAG (re
->flags
, ZEBRA_FLAG_SELECTED
);
1155 * rib_can_delete_dest
1157 * Returns TRUE if the given dest can be deleted from the table.
1160 rib_can_delete_dest (rib_dest_t
*dest
)
1168 * Don't delete the dest if we have to update the FPM about this
1171 if (CHECK_FLAG (dest
->flags
, RIB_DEST_UPDATE_FPM
) ||
1172 CHECK_FLAG (dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1181 * Garbage collect the rib dest corresponding to the given route node
1184 * Returns TRUE if the dest was deleted, FALSE otherwise.
1187 rib_gc_dest (struct route_node
*rn
)
1190 struct zebra_vrf
*zvrf
;
1192 dest
= rib_dest_from_rnode (rn
);
1196 if (!rib_can_delete_dest (dest
))
1199 zvrf
= rib_dest_vrf (dest
);
1200 if (IS_ZEBRA_DEBUG_RIB
)
1201 rnode_debug (rn
, zvrf_id (zvrf
), "removing dest from table");
1204 XFREE (MTYPE_RIB_DEST
, dest
);
1208 * Release the one reference that we keep on the route node.
1210 route_unlock_node (rn
);
1215 rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1216 struct route_entry
*new)
1218 hook_call(rib_update
, rn
, "new route selected");
1220 /* Update real nexthop. This may actually determine if nexthop is active or not. */
1221 if (!nexthop_active_update (rn
, new, 1))
1223 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1227 SET_FLAG (new->status
, ROUTE_ENTRY_SELECTED_FIB
);
1228 if (IS_ZEBRA_DEBUG_RIB
)
1230 char buf
[SRCDEST2STR_BUFFER
];
1231 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1232 zlog_debug ("%u:%s: Adding route rn %p, re %p (type %d)",
1233 zvrf_id (zvrf
), buf
, rn
, new, new->type
);
1236 /* If labeled-unicast route, install transit LSP. */
1237 if (zebra_rib_labeled_unicast (new))
1238 zebra_mpls_lsp_install (zvrf
, rn
, new);
1240 if (!RIB_SYSTEM_ROUTE (new))
1242 if (rib_install_kernel (rn
, new, NULL
))
1244 char buf
[SRCDEST2STR_BUFFER
];
1245 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1246 zlog_warn ("%u:%s: Route install failed",
1247 zvrf_id (zvrf
), buf
);
1251 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1255 rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1256 struct route_entry
*old
)
1258 hook_call(rib_update
, rn
, "removing existing route");
1260 /* Uninstall from kernel. */
1261 if (IS_ZEBRA_DEBUG_RIB
)
1263 char buf
[SRCDEST2STR_BUFFER
];
1264 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1265 zlog_debug ("%u:%s: Deleting route rn %p, re %p (type %d)",
1266 zvrf_id (zvrf
), buf
, rn
, old
, old
->type
);
1269 /* If labeled-unicast route, uninstall transit LSP. */
1270 if (zebra_rib_labeled_unicast (old
))
1271 zebra_mpls_lsp_uninstall (zvrf
, rn
, old
);
1273 if (!RIB_SYSTEM_ROUTE (old
))
1274 rib_uninstall_kernel (rn
, old
);
1276 UNSET_FLAG (old
->status
, ROUTE_ENTRY_SELECTED_FIB
);
1278 /* Update nexthop for route, reset changed flag. */
1279 nexthop_active_update (rn
, old
, 1);
1280 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1284 rib_process_update_fib (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1285 struct route_entry
*old
, struct route_entry
*new)
1287 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1293 * We have to install or update if a new route has been selected or
1294 * something has changed.
1297 CHECK_FLAG (new->status
, ROUTE_ENTRY_CHANGED
))
1299 hook_call(rib_update
, rn
, "updating existing route");
1301 /* Update the nexthop; we could determine here that nexthop is inactive. */
1302 if (nexthop_active_update (rn
, new, 1))
1305 /* If nexthop is active, install the selected route, if appropriate. If
1306 * the install succeeds, cleanup flags for prior route, if different from
1311 if (IS_ZEBRA_DEBUG_RIB
)
1313 char buf
[SRCDEST2STR_BUFFER
];
1314 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1316 zlog_debug ("%u:%s: Updating route rn %p, re %p (type %d) "
1317 "old %p (type %d)", zvrf_id (zvrf
), buf
,
1318 rn
, new, new->type
, old
, old
->type
);
1320 zlog_debug ("%u:%s: Updating route rn %p, re %p (type %d)",
1321 zvrf_id (zvrf
), buf
, rn
, new, new->type
);
1324 /* If labeled-unicast route, uninstall transit LSP. */
1325 if (zebra_rib_labeled_unicast (old
))
1326 zebra_mpls_lsp_uninstall (zvrf
, rn
, old
);
1328 /* Non-system route should be installed. */
1329 if (!RIB_SYSTEM_ROUTE (new))
1331 /* If labeled-unicast route, install transit LSP. */
1332 if (zebra_rib_labeled_unicast (new))
1333 zebra_mpls_lsp_install (zvrf
, rn
, new);
1335 if (rib_install_kernel (rn
, new, old
))
1337 char buf
[SRCDEST2STR_BUFFER
];
1338 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1340 zlog_warn ("%u:%s: Route install failed", zvrf_id (zvrf
), buf
);
1344 /* If install succeeded or system route, cleanup flags for prior route. */
1345 if (installed
&& new != old
)
1347 if (RIB_SYSTEM_ROUTE(new))
1349 if (!RIB_SYSTEM_ROUTE (old
))
1350 rib_uninstall_kernel (rn
, old
);
1354 for (nexthop
= old
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1355 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1359 /* Update for redistribution. */
1361 SET_FLAG (new->status
, ROUTE_ENTRY_SELECTED_FIB
);
1365 * If nexthop for selected route is not active or install failed, we
1366 * may need to uninstall and delete for redistribution.
1368 if (!nh_active
|| !installed
)
1370 if (IS_ZEBRA_DEBUG_RIB
)
1372 char buf
[SRCDEST2STR_BUFFER
];
1373 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1375 zlog_debug ("%u:%s: Deleting route rn %p, re %p (type %d) "
1376 "old %p (type %d) - %s", zvrf_id (zvrf
), buf
,
1377 rn
, new, new->type
, old
, old
->type
,
1378 nh_active
? "install failed" : "nexthop inactive");
1380 zlog_debug ("%u:%s: Deleting route rn %p, re %p (type %d) - %s",
1381 zvrf_id (zvrf
), buf
, rn
, new, new->type
,
1382 nh_active
? "install failed" : "nexthop inactive");
1385 /* If labeled-unicast route, uninstall transit LSP. */
1386 if (zebra_rib_labeled_unicast (old
))
1387 zebra_mpls_lsp_uninstall (zvrf
, rn
, old
);
1389 if (!RIB_SYSTEM_ROUTE (old
))
1390 rib_uninstall_kernel (rn
, old
);
1391 UNSET_FLAG (new->status
, ROUTE_ENTRY_SELECTED_FIB
);
1397 * Same route selected; check if in the FIB and if not, re-install. This
1398 * is housekeeping code to deal with race conditions in kernel with linux
1399 * netlink reporting interface up before IPv4 or IPv6 protocol is ready
1402 if (!RIB_SYSTEM_ROUTE (new))
1406 for (ALL_NEXTHOPS_RO(new->nexthop
, nexthop
, tnexthop
, recursing
))
1407 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1413 rib_install_kernel (rn
, new, NULL
);
1417 /* Update prior route. */
1420 UNSET_FLAG (old
->status
, ROUTE_ENTRY_SELECTED_FIB
);
1422 /* Set real nexthop. */
1423 nexthop_active_update (rn
, old
, 1);
1424 UNSET_FLAG(old
->status
, ROUTE_ENTRY_CHANGED
);
1427 /* Clear changed flag. */
1428 UNSET_FLAG(new->status
, ROUTE_ENTRY_CHANGED
);
1431 /* Check if 'alternate' RIB entry is better than 'current'. */
1432 static struct route_entry
*
1433 rib_choose_best (struct route_entry
*current
, struct route_entry
*alternate
)
1435 if (current
== NULL
)
1438 /* filter route selection in following order:
1439 * - connected beats other types
1440 * - lower distance beats higher
1441 * - lower metric beats higher for equal distance
1442 * - last, hence oldest, route wins tie break.
1445 /* Connected routes. Pick the last connected
1446 * route of the set of lowest metric connected routes.
1448 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
)
1450 if (current
->type
!= ZEBRA_ROUTE_CONNECT
1451 || alternate
->metric
<= current
->metric
)
1457 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1460 /* higher distance loses */
1461 if (alternate
->distance
< current
->distance
)
1463 if (current
->distance
< alternate
->distance
)
1466 /* metric tie-breaks equal distance */
1467 if (alternate
->metric
<= current
->metric
)
1473 /* Core function for processing routing information base. */
1475 rib_process (struct route_node
*rn
)
1477 struct route_entry
*re
;
1478 struct route_entry
*next
;
1479 struct route_entry
*old_selected
= NULL
;
1480 struct route_entry
*new_selected
= NULL
;
1481 struct route_entry
*old_fib
= NULL
;
1482 struct route_entry
*new_fib
= NULL
;
1483 struct route_entry
*best
= NULL
;
1484 char buf
[SRCDEST2STR_BUFFER
];
1486 struct zebra_vrf
*zvrf
= NULL
;
1487 struct prefix
*p
, *src_p
;
1488 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1489 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1493 dest
= rib_dest_from_rnode (rn
);
1496 zvrf
= rib_dest_vrf (dest
);
1497 vrf_id
= zvrf_id (zvrf
);
1500 if (IS_ZEBRA_DEBUG_RIB
)
1501 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1503 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1504 zlog_debug ("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1506 RNODE_FOREACH_RE_SAFE (rn
, re
, next
)
1508 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1509 zlog_debug ("%u:%s: Examine re %p (type %d) status %x flags %x "
1510 "dist %d metric %d",
1511 vrf_id
, buf
, re
, re
->type
, re
->status
,
1512 re
->flags
, re
->distance
, re
->metric
);
1514 UNSET_FLAG(re
->status
, ROUTE_ENTRY_NEXTHOPS_CHANGED
);
1516 /* Currently selected re. */
1517 if (CHECK_FLAG (re
->flags
, ZEBRA_FLAG_SELECTED
))
1519 assert (old_selected
== NULL
);
1522 /* Currently in fib */
1523 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_SELECTED_FIB
))
1525 assert (old_fib
== NULL
);
1529 /* Skip deleted entries from selection */
1530 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
))
1533 /* Skip unreachable nexthop. */
1534 /* This first call to nexthop_active_update is merely to determine if
1535 * there's any change to nexthops associated with this RIB entry. Now,
1536 * rib_process() can be invoked due to an external event such as link
1537 * down or due to next-hop-tracking evaluation. In the latter case,
1538 * a decision has already been made that the NHs have changed. So, no
1539 * need to invoke a potentially expensive call again. Further, since
1540 * the change might be in a recursive NH which is not caught in
1541 * the nexthop_active_update() code. Thus, we might miss changes to
1544 if (!CHECK_FLAG(re
->status
, ROUTE_ENTRY_CHANGED
) &&
1545 ! nexthop_active_update (rn
, re
, 0))
1547 if (re
->type
== ZEBRA_ROUTE_TABLE
)
1549 /* XXX: HERE BE DRAGONS!!!!!
1550 * In all honesty, I have not yet figured out what this part
1551 * does or why the ROUTE_ENTRY_CHANGED test above is correct
1552 * or why we need to delete a route here, and also not whether
1553 * this concerns both selected and fib route, or only selected
1555 /* This entry was denied by the 'ip protocol table' route-map, we
1556 * need to delete it */
1557 if (re
!= old_selected
)
1559 if (IS_ZEBRA_DEBUG_RIB
)
1560 zlog_debug ("%s: %s: imported via import-table but denied "
1561 "by the ip protocol table route-map",
1563 rib_unlink (rn
, re
);
1566 SET_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
);
1572 /* Infinite distance. */
1573 if (re
->distance
== DISTANCE_INFINITY
)
1575 UNSET_FLAG (re
->status
, ROUTE_ENTRY_CHANGED
);
1579 if (CHECK_FLAG (re
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
))
1581 best
= rib_choose_best(new_fib
, re
);
1582 if (new_fib
&& best
!= new_fib
)
1583 UNSET_FLAG (new_fib
->status
, ROUTE_ENTRY_CHANGED
);
1588 best
= rib_choose_best(new_selected
, re
);
1589 if (new_selected
&& best
!= new_selected
)
1590 UNSET_FLAG (new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1591 new_selected
= best
;
1594 UNSET_FLAG (re
->status
, ROUTE_ENTRY_CHANGED
);
1595 } /* RNODE_FOREACH_RE */
1597 /* If no FIB override route, use the selected route also for FIB */
1598 if (new_fib
== NULL
)
1599 new_fib
= new_selected
;
1601 /* After the cycle is finished, the following pointers will be set:
1602 * old_selected --- RE entry currently having SELECTED
1603 * new_selected --- RE entry that is newly SELECTED
1604 * old_fib --- RE entry currently in kernel FIB
1605 * new_fib --- RE entry that is newly to be in kernel FIB
1607 * new_selected will get SELECTED flag, and is going to be redistributed
1608 * the zclients. new_fib (which can be new_selected) will be installed in kernel.
1611 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1613 zlog_debug ("%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1615 (void *)old_selected
,
1616 (void *)new_selected
,
1621 /* Buffer ROUTE_ENTRY_CHANGED here, because it will get cleared if
1622 * fib == selected */
1623 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1624 ROUTE_ENTRY_CHANGED
);
1626 /* Update fib according to selection results */
1627 if (new_fib
&& old_fib
)
1628 rib_process_update_fib (zvrf
, rn
, old_fib
, new_fib
);
1630 rib_process_add_fib (zvrf
, rn
, new_fib
);
1632 rib_process_del_fib (zvrf
, rn
, old_fib
);
1634 /* Redistribute SELECTED entry */
1635 if (old_selected
!= new_selected
|| selected_changed
)
1637 struct nexthop
*nexthop
, *tnexthop
;
1640 /* Check if we have a FIB route for the destination, otherwise,
1641 * don't redistribute it */
1642 for (ALL_NEXTHOPS_RO(new_fib
? new_fib
->nexthop
: NULL
, nexthop
,
1643 tnexthop
, recursing
))
1645 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1651 new_selected
= NULL
;
1653 if (new_selected
&& new_selected
!= new_fib
)
1655 nexthop_active_update(rn
, new_selected
, 1);
1656 UNSET_FLAG(new_selected
->status
, ROUTE_ENTRY_CHANGED
);
1662 redistribute_delete(p
, src_p
, old_selected
);
1663 if (old_selected
!= new_selected
)
1664 UNSET_FLAG (old_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1669 /* Install new or replace existing redistributed entry */
1670 SET_FLAG (new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1671 redistribute_update (p
, src_p
, new_selected
, old_selected
);
1675 /* Remove all RE entries queued for removal */
1676 RNODE_FOREACH_RE_SAFE (rn
, re
, next
)
1678 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
))
1680 if (IS_ZEBRA_DEBUG_RIB
)
1682 rnode_debug (rn
, vrf_id
, "rn %p, removing re %p",
1683 (void *)rn
, (void *)re
);
1690 * Check if the dest can be deleted now.
1695 /* Take a list of route_node structs and return 1, if there was a record
1696 * picked from it and processed by rib_process(). Don't process more,
1697 * than one RN record; operate only in the specified sub-queue.
1700 process_subq (struct list
* subq
, u_char qindex
)
1702 struct listnode
*lnode
= listhead (subq
);
1703 struct route_node
*rnode
;
1705 struct zebra_vrf
*zvrf
= NULL
;
1710 rnode
= listgetdata (lnode
);
1711 dest
= rib_dest_from_rnode (rnode
);
1713 zvrf
= rib_dest_vrf (dest
);
1715 rib_process (rnode
);
1717 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1719 char buf
[SRCDEST2STR_BUFFER
];
1720 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
1721 zlog_debug ("%u:%s: rn %p dequeued from sub-queue %u",
1722 zvrf
? zvrf_id (zvrf
) : 0, buf
, rnode
, qindex
);
1726 UNSET_FLAG (rib_dest_from_rnode (rnode
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1731 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1732 __func__
, rnode
, rnode
->lock
);
1733 zlog_backtrace(LOG_DEBUG
);
1736 route_unlock_node (rnode
);
1737 list_delete_node (subq
, lnode
);
1742 * All meta queues have been processed. Trigger next-hop evaluation.
1745 meta_queue_process_complete (struct work_queue
*dummy
)
1748 struct zebra_vrf
*zvrf
;
1750 /* Evaluate nexthops for those VRFs which underwent route processing. This
1751 * should limit the evaluation to the necessary VRFs in most common
1754 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1757 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
1760 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
1761 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET
, 0, RNH_NEXTHOP_TYPE
, NULL
);
1762 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
1763 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
1764 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET6
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
1767 /* Schedule LSPs for processing, if needed. */
1768 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1769 if (mpls_should_lsps_be_processed(zvrf
))
1771 if (IS_ZEBRA_DEBUG_MPLS
)
1772 zlog_debug ("%u: Scheduling all LSPs upon RIB completion", zvrf_id (zvrf
));
1773 zebra_mpls_lsp_schedule (zvrf
);
1774 mpls_unmark_lsps_for_processing(zvrf
);
1778 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1779 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1780 * is pointed to the meta queue structure.
1782 static wq_item_status
1783 meta_queue_process (struct work_queue
*dummy
, void *data
)
1785 struct meta_queue
* mq
= data
;
1788 for (i
= 0; i
< MQ_SIZE
; i
++)
1789 if (process_subq (mq
->subq
[i
], i
))
1794 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1798 * Map from rib types to queue type (priority) in meta queue
1800 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1801 [ZEBRA_ROUTE_SYSTEM
] = 4,
1802 [ZEBRA_ROUTE_KERNEL
] = 0,
1803 [ZEBRA_ROUTE_CONNECT
] = 0,
1804 [ZEBRA_ROUTE_STATIC
] = 1,
1805 [ZEBRA_ROUTE_RIP
] = 2,
1806 [ZEBRA_ROUTE_RIPNG
] = 2,
1807 [ZEBRA_ROUTE_OSPF
] = 2,
1808 [ZEBRA_ROUTE_OSPF6
] = 2,
1809 [ZEBRA_ROUTE_ISIS
] = 2,
1810 [ZEBRA_ROUTE_BGP
] = 3,
1811 [ZEBRA_ROUTE_PIM
] = 4, // Shouldn't happen but for safety
1812 [ZEBRA_ROUTE_EIGRP
] = 2,
1813 [ZEBRA_ROUTE_NHRP
] = 2,
1814 [ZEBRA_ROUTE_HSLS
] = 4,
1815 [ZEBRA_ROUTE_OLSR
] = 4,
1816 [ZEBRA_ROUTE_TABLE
] = 1,
1817 [ZEBRA_ROUTE_LDP
] = 4,
1818 [ZEBRA_ROUTE_VNC
] = 3,
1819 [ZEBRA_ROUTE_VNC_DIRECT
] = 3,
1820 [ZEBRA_ROUTE_VNC_DIRECT_RH
] = 3,
1821 [ZEBRA_ROUTE_BGP_DIRECT
] = 3,
1822 [ZEBRA_ROUTE_BGP_DIRECT_EXT
] = 3,
1823 [ZEBRA_ROUTE_BABEL
] = 2,
1824 [ZEBRA_ROUTE_ALL
] = 4, // Shouldn't happen but for safety
1827 /* Look into the RN and queue it into one or more priority queues,
1828 * increasing the size for each data push done.
1831 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
1833 struct route_entry
*re
;
1835 RNODE_FOREACH_RE (rn
, re
)
1837 u_char qindex
= meta_queue_map
[re
->type
];
1838 struct zebra_vrf
*zvrf
;
1840 /* Invariant: at this point we always have rn->info set. */
1841 if (CHECK_FLAG (rib_dest_from_rnode (rn
)->flags
,
1842 RIB_ROUTE_QUEUED (qindex
)))
1844 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1845 rnode_debug (rn
, re
->vrf_id
, "rn %p is already queued in sub-queue %u",
1846 (void *)rn
, qindex
);
1850 SET_FLAG (rib_dest_from_rnode (rn
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1851 listnode_add (mq
->subq
[qindex
], rn
);
1852 route_lock_node (rn
);
1855 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1856 rnode_debug (rn
, re
->vrf_id
, "queued rn %p into sub-queue %u",
1857 (void *)rn
, qindex
);
1859 zvrf
= zebra_vrf_lookup_by_id (re
->vrf_id
);
1861 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1865 /* Add route_node to work queue and schedule processing */
1867 rib_queue_add (struct route_node
*rn
)
1871 /* Pointless to queue a route_node with no RIB entries to add or remove */
1872 if (!rnode_to_ribs (rn
))
1874 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1875 __func__
, (void *)rn
, rn
->lock
);
1876 zlog_backtrace(LOG_DEBUG
);
1880 if (zebrad
.ribq
== NULL
)
1882 zlog_err ("%s: work_queue does not exist!", __func__
);
1887 * The RIB queue should normally be either empty or holding the only
1888 * work_queue_item element. In the latter case this element would
1889 * hold a pointer to the meta queue structure, which must be used to
1890 * actually queue the route nodes to process. So create the MQ
1891 * holder, if necessary, then push the work into it in any case.
1892 * This semantics was introduced after 0.99.9 release.
1894 if (!zebrad
.ribq
->items
->count
)
1895 work_queue_add (zebrad
.ribq
, zebrad
.mq
);
1897 rib_meta_queue_add (zebrad
.mq
, rn
);
1902 /* Create new meta queue.
1903 A destructor function doesn't seem to be necessary here.
1905 static struct meta_queue
*
1906 meta_queue_new (void)
1908 struct meta_queue
*new;
1911 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
1914 for (i
= 0; i
< MQ_SIZE
; i
++)
1916 new->subq
[i
] = list_new ();
1917 assert(new->subq
[i
]);
1924 meta_queue_free (struct meta_queue
*mq
)
1928 for (i
= 0; i
< MQ_SIZE
; i
++)
1929 list_delete (mq
->subq
[i
]);
1931 XFREE (MTYPE_WORK_QUEUE
, mq
);
1934 /* initialise zebra rib work queue */
1936 rib_queue_init (struct zebra_t
*zebra
)
1940 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1941 "route_node processing")))
1943 zlog_err ("%s: could not initialise work queue!", __func__
);
1947 /* fill in the work queue spec */
1948 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1949 zebra
->ribq
->spec
.errorfunc
= NULL
;
1950 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
1951 /* XXX: TODO: These should be runtime configurable via vty */
1952 zebra
->ribq
->spec
.max_retries
= 3;
1953 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
1955 if (!(zebra
->mq
= meta_queue_new ()))
1957 zlog_err ("%s: could not initialise meta queue!", __func__
);
1963 /* RIB updates are processed via a queue of pointers to route_nodes.
1965 * The queue length is bounded by the maximal size of the routing table,
1966 * as a route_node will not be requeued, if already queued.
1968 * REs are submitted via rib_addnode or rib_delnode which set minimal
1969 * state, or static_install_route (when an existing RE is updated)
1970 * and then submit route_node to queue for best-path selection later.
1971 * Order of add/delete state changes are preserved for any given RE.
1973 * Deleted REs are reaped during best-path selection.
1976 * |-> rib_link or unset ROUTE_ENTRY_REMOVE |->Update kernel with
1977 * |-------->| | best RE, if required
1979 * static_install->|->rib_addqueue...... -> rib_process
1981 * |-------->| |-> rib_unlink
1982 * |-> set ROUTE_ENTRY_REMOVE |
1983 * rib_delnode (RE freed)
1985 * The 'info' pointer of a route_node points to a rib_dest_t
1986 * ('dest'). Queueing state for a route_node is kept on the dest. The
1987 * dest is created on-demand by rib_link() and is kept around at least
1988 * as long as there are ribs hanging off it (@see rib_gc_dest()).
1990 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1992 * - route_nodes: refcounted by:
1993 * - dest attached to route_node:
1994 * - managed by: rib_link/rib_gc_dest
1995 * - route_node processing queue
1996 * - managed by: rib_addqueue, rib_process.
2000 /* Add RE to head of the route node. */
2002 rib_link (struct route_node
*rn
, struct route_entry
*re
, int process
)
2004 struct route_entry
*head
;
2007 const char *rmap_name
;
2011 dest
= rib_dest_from_rnode (rn
);
2014 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2015 rnode_debug (rn
, re
->vrf_id
, "rn %p adding dest", rn
);
2017 dest
= XCALLOC (MTYPE_RIB_DEST
, sizeof (rib_dest_t
));
2018 route_lock_node (rn
); /* rn route table reference */
2023 head
= dest
->routes
;
2031 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2032 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2033 if (is_zebra_import_table_enabled (afi
, re
->table
))
2035 rmap_name
= zebra_get_import_table_route_map (afi
, re
->table
);
2036 zebra_add_import_table_entry(rn
, re
, rmap_name
);
2044 rib_addnode (struct route_node
*rn
, struct route_entry
*re
, int process
)
2046 /* RE node has been un-removed before route-node is processed.
2047 * route_node must hence already be on the queue for processing..
2049 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
))
2051 if (IS_ZEBRA_DEBUG_RIB
)
2052 rnode_debug (rn
, re
->vrf_id
, "rn %p, un-removed re %p", (void *)rn
, (void *)re
);
2054 UNSET_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
);
2057 rib_link (rn
, re
, process
);
2063 * Detach a rib structure from a route_node.
2065 * Note that a call to rib_unlink() should be followed by a call to
2066 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2067 * longer required to be deleted.
2070 rib_unlink (struct route_node
*rn
, struct route_entry
*re
)
2076 if (IS_ZEBRA_DEBUG_RIB
)
2077 rnode_debug (rn
, re
->vrf_id
, "rn %p, re %p", (void *)rn
, (void *)re
);
2079 dest
= rib_dest_from_rnode (rn
);
2082 re
->next
->prev
= re
->prev
;
2085 re
->prev
->next
= re
->next
;
2088 dest
->routes
= re
->next
;
2091 /* free RE and nexthops */
2092 zebra_deregister_rnh_static_nexthops (re
->vrf_id
, re
->nexthop
, rn
);
2093 nexthops_free(re
->nexthop
);
2094 XFREE (MTYPE_RE
, re
);
2099 rib_delnode (struct route_node
*rn
, struct route_entry
*re
)
2103 if (IS_ZEBRA_DEBUG_RIB
)
2104 rnode_debug (rn
, re
->vrf_id
, "rn %p, re %p, removing", (void *)rn
, (void *)re
);
2105 SET_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
);
2107 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2108 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2109 if (is_zebra_import_table_enabled (afi
, re
->table
))
2111 zebra_del_import_table_entry(rn
, re
);
2112 /* Just clean up if non main table */
2113 if (IS_ZEBRA_DEBUG_RIB
)
2115 char buf
[SRCDEST2STR_BUFFER
];
2116 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2117 zlog_debug ("%u:%s: Freeing route rn %p, re %p (type %d)",
2118 re
->vrf_id
, buf
, rn
, re
, re
->type
);
2129 /* This function dumps the contents of a given RE entry into
2130 * standard debug log. Calling function name and IP prefix in
2131 * question are passed as 1st and 2nd arguments.
2134 void _route_entry_dump (const char * func
,
2135 union prefixconstptr pp
,
2136 union prefixconstptr src_pp
,
2137 const struct route_entry
* re
)
2139 const struct prefix
*p
= pp
.p
;
2140 const struct prefix
*src_p
= src_pp
.p
;
2141 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2142 char straddr
[PREFIX_STRLEN
];
2143 char srcaddr
[PREFIX_STRLEN
];
2144 struct nexthop
*nexthop
, *tnexthop
;
2147 zlog_debug ("%s: dumping RE entry %p for %s%s%s vrf %u", func
, (const void *)re
,
2148 prefix2str(pp
, straddr
, sizeof(straddr
)),
2149 is_srcdst
? " from " : "",
2150 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
)) : "",
2154 "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d",
2157 (unsigned long) re
->uptime
,
2164 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2174 "%s: nexthop_num == %u, nexthop_active_num == %u",
2177 re
->nexthop_active_num
2180 for (ALL_NEXTHOPS_RO(re
->nexthop
, nexthop
, tnexthop
, recursing
))
2182 inet_ntop (p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2185 "%s: %s %s with flags %s%s%s",
2187 (recursing
? " NH" : "NH"),
2189 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
2190 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
2191 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
2194 zlog_debug ("%s: dump complete", func
);
2197 /* This is an exported helper to rtm_read() to dump the strange
2198 * RE entry found by rib_lookup_ipv4_route()
2201 void rib_lookup_and_dump (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2203 struct route_table
*table
;
2204 struct route_node
*rn
;
2205 struct route_entry
*re
;
2206 char prefix_buf
[INET_ADDRSTRLEN
];
2209 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2212 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2216 /* Scan the RIB table for exactly matching RE entry. */
2217 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2219 /* No route for this prefix. */
2222 zlog_debug ("%s: lookup failed for %s", __func__
,
2223 prefix2str((struct prefix
*) p
, prefix_buf
, sizeof(prefix_buf
)));
2228 route_unlock_node (rn
);
2231 RNODE_FOREACH_RE (rn
, re
)
2235 "%s: rn %p, re %p: %s, %s",
2239 (CHECK_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
2240 (CHECK_FLAG (re
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
2242 route_entry_dump (p
, NULL
, re
);
2246 /* Check if requested address assignment will fail due to another
2247 * route being installed by zebra in FIB already. Take necessary
2248 * actions, if needed: remove such a route from FIB and deSELECT
2249 * corresponding RE entry. Then put affected RN into RIBQ head.
2251 void rib_lookup_and_pushup (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2253 struct route_table
*table
;
2254 struct route_node
*rn
;
2255 struct route_entry
*re
;
2256 unsigned changed
= 0;
2258 if (NULL
== (table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
)))
2260 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2264 /* No matches would be the simplest case. */
2265 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
2269 route_unlock_node (rn
);
2271 /* Check all RE entries. In case any changes have to be done, requeue
2272 * the RN into RIBQ head. If the routing message about the new connected
2273 * route (generated by the IP address we are going to assign very soon)
2274 * comes before the RIBQ is processed, the new RE entry will join
2275 * RIBQ record already on head. This is necessary for proper revalidation
2276 * of the rest of the RE.
2278 RNODE_FOREACH_RE (rn
, re
)
2280 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_SELECTED_FIB
) &&
2281 ! RIB_SYSTEM_ROUTE (re
))
2284 if (IS_ZEBRA_DEBUG_RIB
)
2286 char buf
[PREFIX_STRLEN
];
2287 zlog_debug ("%u:%s: freeing way for connected prefix",
2288 re
->vrf_id
, prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2289 route_entry_dump (&rn
->p
, NULL
, re
);
2291 rib_uninstall (rn
, re
);
2299 rib_add_multipath (afi_t afi
, safi_t safi
, struct prefix
*p
,
2300 struct prefix_ipv6
*src_p
, struct route_entry
*re
)
2302 struct route_table
*table
;
2303 struct route_node
*rn
;
2304 struct route_entry
*same
;
2305 struct nexthop
*nexthop
;
2312 if (p
->family
== AF_INET
)
2317 assert(!src_p
|| family
== AFI_IP6
);
2320 table
= zebra_vrf_table_with_table_id (family
, safi
, re
->vrf_id
, re
->table
);
2324 /* Make it sure prefixlen is applied to the prefix. */
2327 apply_mask_ipv6 (src_p
);
2329 /* Set default distance by route type. */
2330 if (re
->distance
== 0)
2332 re
->distance
= route_info
[re
->type
].distance
;
2334 /* iBGP distance is 200. */
2335 if (re
->type
== ZEBRA_ROUTE_BGP
2336 && CHECK_FLAG (re
->flags
, ZEBRA_FLAG_IBGP
))
2340 /* Lookup route node.*/
2341 rn
= srcdest_rnode_get (table
, p
, src_p
);
2343 /* If same type of route are installed, treat it as a implicit
2345 RNODE_FOREACH_RE (rn
, same
)
2347 if (CHECK_FLAG (same
->status
, ROUTE_ENTRY_REMOVED
))
2350 if (same
->type
== re
->type
&& same
->instance
== re
->instance
2351 && same
->table
== re
->table
2352 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
2356 /* If this route is kernel route, set FIB flag to the route. */
2357 if (re
->type
== ZEBRA_ROUTE_KERNEL
|| re
->type
== ZEBRA_ROUTE_CONNECT
)
2358 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2359 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2361 /* Link new re to node.*/
2362 if (IS_ZEBRA_DEBUG_RIB
)
2364 rnode_debug(rn
, re
->vrf_id
, "Inserting route rn %p, re %p (type %d) existing %p",
2365 (void *)rn
, (void *)re
, re
->type
, (void *)same
);
2367 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2368 route_entry_dump (p
, src_p
, re
);
2370 rib_addnode (rn
, re
, 1);
2373 /* Free implicit route.*/
2376 rib_delnode (rn
, same
);
2380 route_unlock_node (rn
);
2385 rib_delete (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
, u_short instance
,
2386 int flags
, struct prefix
*p
, struct prefix_ipv6
*src_p
,
2387 union g_addr
*gate
, ifindex_t ifindex
, u_int32_t table_id
)
2389 struct route_table
*table
;
2390 struct route_node
*rn
;
2391 struct route_entry
*re
;
2392 struct route_entry
*fib
= NULL
;
2393 struct route_entry
*same
= NULL
;
2394 struct nexthop
*nexthop
, *tnexthop
;
2396 char buf2
[INET6_ADDRSTRLEN
];
2398 assert(!src_p
|| afi
== AFI_IP6
);
2401 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2408 apply_mask_ipv6 (src_p
);
2410 /* Lookup route node. */
2411 rn
= srcdest_rnode_lookup (table
, p
, src_p
);
2414 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2416 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2417 if (src_p
&& src_p
->prefixlen
)
2418 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2422 if (IS_ZEBRA_DEBUG_RIB
)
2423 zlog_debug ("%u:%s%s%s doesn't exist in rib",
2425 (src_buf
[0] != '\0') ? " from " : "",
2430 /* Lookup same type route. */
2431 RNODE_FOREACH_RE (rn
, re
)
2433 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
))
2436 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_SELECTED_FIB
))
2439 if (re
->type
!= type
)
2441 if (re
->instance
!= instance
)
2443 if (re
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= re
->nexthop
) &&
2444 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2446 if (nexthop
->ifindex
!= ifindex
)
2451 route_unlock_node (rn
);
2452 route_unlock_node (rn
);
2458 /* Make sure that the route found has the same gateway. */
2466 for (ALL_NEXTHOPS_RO(re
->nexthop
, nexthop
, tnexthop
, recursing
))
2467 if (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
2468 IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
))
2477 /* If same type of route can't be found and this message is from
2481 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
&&
2482 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
))
2484 if (IS_ZEBRA_DEBUG_RIB
)
2486 rnode_debug (rn
, vrf_id
, "rn %p, re %p (type %d) was deleted from kernel, adding",
2487 rn
, fib
, fib
->type
);
2492 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2493 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2495 UNSET_FLAG (fib
->status
, ROUTE_ENTRY_SELECTED_FIB
);
2499 /* This means someone else, other than Zebra, has deleted
2500 * a Zebra router from the kernel. We will add it back */
2501 rib_install_kernel(rn
, fib
, NULL
);
2506 if (IS_ZEBRA_DEBUG_RIB
)
2509 rnode_debug(rn
, vrf_id
, "via %s ifindex %d type %d "
2510 "doesn't exist in rib",
2511 inet_ntop (family2afi(afi
), gate
, buf2
, INET_ADDRSTRLEN
), /* FIXME */
2515 rnode_debug (rn
, vrf_id
, "ifindex %d type %d doesn't exist in rib",
2519 route_unlock_node (rn
);
2525 rib_delnode (rn
, same
);
2527 route_unlock_node (rn
);
2534 rib_add (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2535 u_short instance
, int flags
, struct prefix
*p
,
2536 struct prefix_ipv6
*src_p
, union g_addr
*gate
,
2537 union g_addr
*src
, ifindex_t ifindex
,
2538 u_int32_t table_id
, u_int32_t metric
, u_int32_t mtu
,
2541 struct route_entry
*re
;
2542 struct route_entry
*same
= NULL
;
2543 struct route_table
*table
;
2544 struct route_node
*rn
;
2545 struct nexthop
*nexthop
;
2547 assert(!src_p
|| afi
== AFI_IP6
);
2550 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2554 /* Make sure mask is applied. */
2557 apply_mask_ipv6 (src_p
);
2559 /* Set default distance by route type. */
2562 if ((unsigned)type
>= array_size(route_info
))
2565 distance
= route_info
[type
].distance
;
2567 /* iBGP distance is 200. */
2568 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2572 /* Lookup route node.*/
2573 rn
= srcdest_rnode_get (table
, p
, src_p
);
2575 /* If same type of route are installed, treat it as a implicit
2577 RNODE_FOREACH_RE (rn
, re
)
2579 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
))
2582 if (re
->type
!= type
)
2584 if (re
->instance
!= instance
)
2586 if (re
->type
!= ZEBRA_ROUTE_CONNECT
)
2591 /* Duplicate connected route comes in. */
2592 else if ((nexthop
= re
->nexthop
) &&
2593 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2594 nexthop
->ifindex
== ifindex
&&
2595 !CHECK_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
))
2602 /* Allocate new re structure. */
2603 re
= XCALLOC (MTYPE_RE
, sizeof (struct route_entry
));
2606 re
->instance
= instance
;
2607 re
->distance
= distance
;
2609 re
->metric
= metric
;
2611 re
->table
= table_id
;
2612 re
->vrf_id
= vrf_id
;
2613 re
->nexthop_num
= 0;
2614 re
->uptime
= time (NULL
);
2616 /* Nexthop settings. */
2622 route_entry_nexthop_ipv6_ifindex_add (re
, &gate
->ipv6
, ifindex
);
2624 route_entry_nexthop_ipv6_add (re
, &gate
->ipv6
);
2629 route_entry_nexthop_ipv4_ifindex_add (re
, &gate
->ipv4
, &src
->ipv4
, ifindex
);
2631 route_entry_nexthop_ipv4_add (re
, &gate
->ipv4
, &src
->ipv4
);
2635 route_entry_nexthop_ifindex_add (re
, ifindex
);
2637 /* If this route is kernel route, set FIB flag to the route. */
2638 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2639 for (nexthop
= re
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2640 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2642 /* Link new rib to node.*/
2643 if (IS_ZEBRA_DEBUG_RIB
)
2645 rnode_debug (rn
, vrf_id
, "Inserting route rn %p, re %p (type %d) existing %p",
2646 (void *)rn
, (void *)re
, re
->type
, (void *)same
);
2648 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2649 route_entry_dump (p
, src_p
, re
);
2651 rib_addnode (rn
, re
, 1);
2653 /* Free implicit route.*/
2655 rib_delnode (rn
, same
);
2657 route_unlock_node (rn
);
2661 /* Schedule routes of a particular table (address-family) based on event. */
2663 rib_update_table (struct route_table
*table
, rib_update_event_t event
)
2665 struct route_node
*rn
;
2666 struct route_entry
*re
, *next
;
2668 /* Walk all routes and queue for processing, if appropriate for
2669 * the trigger event.
2671 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2675 case RIB_UPDATE_IF_CHANGE
:
2676 /* Examine all routes that won't get processed by the protocol or
2677 * triggered by nexthop evaluation (NHT). This would be system,
2678 * kernel and certain static routes. Note that NHT will get
2679 * triggered upon an interface event as connected routes always
2680 * get queued for processing.
2682 RNODE_FOREACH_RE_SAFE (rn
, re
, next
)
2684 if (re
->type
== ZEBRA_ROUTE_OSPF
||
2685 re
->type
== ZEBRA_ROUTE_OSPF6
||
2686 re
->type
== ZEBRA_ROUTE_BGP
)
2687 continue; /* protocol will handle. */
2688 else if (re
->type
== ZEBRA_ROUTE_STATIC
)
2691 for (nh
= re
->nexthop
; nh
; nh
= nh
->next
)
2692 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
||
2693 nh
->type
== NEXTHOP_TYPE_IPV6
))
2696 /* If we only have nexthops to a gateway, NHT will
2707 case RIB_UPDATE_RMAP_CHANGE
:
2708 case RIB_UPDATE_OTHER
:
2709 /* Right now, examine all routes. Can restrict to a protocol in
2710 * some cases (TODO).
2712 if (rnode_to_ribs (rn
))
2722 /* RIB update function. */
2724 rib_update (vrf_id_t vrf_id
, rib_update_event_t event
)
2726 struct route_table
*table
;
2728 /* Process routes of interested address-families. */
2729 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2731 rib_update_table (table
, event
);
2733 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2735 rib_update_table (table
, event
);
2738 /* Remove all routes which comes from non main table. */
2740 rib_weed_table (struct route_table
*table
)
2742 struct route_node
*rn
;
2743 struct route_entry
*re
;
2744 struct route_entry
*next
;
2747 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2748 RNODE_FOREACH_RE_SAFE (rn
, re
, next
)
2750 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
))
2753 if (re
->table
!= zebrad
.rtm_table_default
&&
2754 re
->table
!= RT_TABLE_MAIN
)
2755 rib_delnode (rn
, re
);
2759 /* Delete all routes from non main table. */
2761 rib_weed_tables (void)
2764 struct zebra_vrf
*zvrf
;
2766 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2767 if ((zvrf
= vrf
->info
) != NULL
)
2769 rib_weed_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2770 rib_weed_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2774 /* Delete self installed routes after zebra is relaunched. */
2776 rib_sweep_table (struct route_table
*table
)
2778 struct route_node
*rn
;
2779 struct route_entry
*re
;
2780 struct route_entry
*next
;
2784 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2785 RNODE_FOREACH_RE_SAFE (rn
, re
, next
)
2787 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
))
2790 if (re
->type
== ZEBRA_ROUTE_KERNEL
&&
2791 CHECK_FLAG (re
->flags
, ZEBRA_FLAG_SELFROUTE
))
2793 ret
= rib_uninstall_kernel (rn
, re
);
2795 rib_delnode (rn
, re
);
2800 /* Sweep all RIB tables. */
2802 rib_sweep_route (void)
2805 struct zebra_vrf
*zvrf
;
2807 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2808 if ((zvrf
= vrf
->info
) != NULL
)
2810 rib_sweep_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2811 rib_sweep_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2815 /* Remove specific by protocol routes from 'table'. */
2816 static unsigned long
2817 rib_score_proto_table (u_char proto
, u_short instance
, struct route_table
*table
)
2819 struct route_node
*rn
;
2820 struct route_entry
*re
;
2821 struct route_entry
*next
;
2822 unsigned long n
= 0;
2825 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2826 RNODE_FOREACH_RE_SAFE (rn
, re
, next
)
2828 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
))
2830 if (re
->type
== proto
&& re
->instance
== instance
)
2832 rib_delnode (rn
, re
);
2839 /* Remove specific by protocol routes. */
2841 rib_score_proto (u_char proto
, u_short instance
)
2844 struct zebra_vrf
*zvrf
;
2845 unsigned long cnt
= 0;
2847 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2848 if ((zvrf
= vrf
->info
) != NULL
)
2849 cnt
+= rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
2850 +rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2855 /* Close RIB and clean up kernel routes. */
2857 rib_close_table (struct route_table
*table
)
2859 struct route_node
*rn
;
2860 rib_table_info_t
*info
= table
->info
;
2861 struct route_entry
*re
;
2864 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2865 RNODE_FOREACH_RE (rn
, re
)
2867 if (!CHECK_FLAG (re
->status
, ROUTE_ENTRY_SELECTED_FIB
))
2870 if (info
->safi
== SAFI_UNICAST
)
2871 hook_call(rib_update
, rn
, NULL
);
2873 if (! RIB_SYSTEM_ROUTE (re
))
2874 rib_uninstall_kernel (rn
, re
);
2878 /* Routing information base initialize. */
2882 rib_queue_init (&zebrad
);
2888 * Get the first vrf id that is greater than the given vrf id if any.
2890 * Returns TRUE if a vrf id was found, FALSE otherwise.
2893 vrf_id_get_next (vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
2897 vrf
= vrf_lookup_by_id (vrf_id
);
2900 vrf
= RB_NEXT (vrf_id_head
, &vrfs_by_id
, vrf
);
2902 *next_id_p
= vrf
->vrf_id
;
2911 * rib_tables_iter_next
2913 * Returns the next table in the iteration.
2915 struct route_table
*
2916 rib_tables_iter_next (rib_tables_iter_t
*iter
)
2918 struct route_table
*table
;
2921 * Array that helps us go over all AFI/SAFI combinations via one
2928 { AFI_IP
, SAFI_UNICAST
},
2929 { AFI_IP
, SAFI_MULTICAST
},
2930 { AFI_IP
, SAFI_LABELED_UNICAST
},
2931 { AFI_IP6
, SAFI_UNICAST
},
2932 { AFI_IP6
, SAFI_MULTICAST
},
2933 { AFI_IP6
, SAFI_LABELED_UNICAST
},
2938 switch (iter
->state
)
2941 case RIB_TABLES_ITER_S_INIT
:
2942 iter
->vrf_id
= VRF_DEFAULT
;
2943 iter
->afi_safi_ix
= -1;
2947 case RIB_TABLES_ITER_S_ITERATING
:
2948 iter
->afi_safi_ix
++;
2952 while (iter
->afi_safi_ix
< (int) ZEBRA_NUM_OF (afi_safis
))
2954 table
= zebra_vrf_table (afi_safis
[iter
->afi_safi_ix
].afi
,
2955 afi_safis
[iter
->afi_safi_ix
].safi
,
2960 iter
->afi_safi_ix
++;
2964 * Found another table in this vrf.
2970 * Done with all tables in the current vrf, go to the next
2973 if (!vrf_id_get_next (iter
->vrf_id
, &iter
->vrf_id
))
2976 iter
->afi_safi_ix
= 0;
2981 case RIB_TABLES_ITER_S_DONE
:
2986 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
2988 iter
->state
= RIB_TABLES_ITER_S_DONE
;