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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #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_fpm.h"
51 #include "zebra/zebra_rnh.h"
52 #include "zebra/interface.h"
53 #include "zebra/connected.h"
55 /* Should we allow non Quagga processes to delete our routes */
56 extern int allow_delete
;
58 /* Hold time for RIB process, should be very minimal.
59 * it is useful to able to set it otherwise for testing, hence exported
60 * as global here for test-rig code.
62 int rib_process_hold_time
= 10;
64 /* Each route type's string and default distance value. */
69 } route_info
[ZEBRA_ROUTE_MAX
] =
71 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
72 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
73 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
74 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
75 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
76 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
77 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
78 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
79 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
80 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
81 /* no entry/default: 150 */
84 /* RPF lookup behaviour */
85 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
88 static void __attribute__((format (printf
, 5, 6)))
89 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
, int priority
,
90 const char *msgfmt
, ...)
92 char buf
[SRCDEST2STR_BUFFER
+ sizeof(" (MRIB)")];
97 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
102 rib_table_info_t
*info
= srcdest_rnode_table_info (rn
);
103 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
105 if (info
->safi
== SAFI_MULTICAST
)
106 strcat(buf
, " (MRIB)");
110 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
113 zlog (NULL
, priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
116 #define rnode_debug(node, vrf_id, ...) \
117 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
118 #define rnode_info(node, ...) \
119 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
122 route_distance (int type
)
126 if ((unsigned)type
>= array_size(route_info
))
129 distance
= route_info
[type
].distance
;
135 is_zebra_valid_kernel_table(u_int32_t table_id
)
137 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
))
141 if ((table_id
== RT_TABLE_UNSPEC
) ||
142 (table_id
== RT_TABLE_LOCAL
) ||
143 (table_id
== RT_TABLE_COMPAT
))
151 is_zebra_main_routing_table(u_int32_t table_id
)
153 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
159 zebra_check_addr (struct prefix
*p
)
161 if (p
->family
== AF_INET
)
165 addr
= p
->u
.prefix4
.s_addr
;
168 if (IPV4_NET127 (addr
)
170 || IPV4_LINKLOCAL(addr
))
173 if (p
->family
== AF_INET6
)
175 if (IN6_IS_ADDR_LOOPBACK (&p
->u
.prefix6
))
177 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
183 /* Add nexthop to the end of a rib node's nexthop list */
185 rib_nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
187 nexthop_add(&rib
->nexthop
, nexthop
);
194 * copy_nexthop - copy a nexthop to the rib structure.
197 rib_copy_nexthops (struct rib
*rib
, struct nexthop
*nh
)
199 struct nexthop
*nexthop
;
201 nexthop
= nexthop_new();
202 nexthop
->flags
= nh
->flags
;
203 nexthop
->type
= nh
->type
;
204 nexthop
->ifindex
= nh
->ifindex
;
205 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
206 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
208 nexthop_add_labels (nexthop
, nh
->nh_label_type
, nh
->nh_label
->num_labels
,
209 &nh
->nh_label
->label
[0]);
210 rib_nexthop_add(rib
, nexthop
);
211 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
212 copy_nexthops(&nexthop
->resolved
, nh
->resolved
);
215 /* Delete specified nexthop from the list. */
217 rib_nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
220 nexthop
->next
->prev
= nexthop
->prev
;
222 nexthop
->prev
->next
= nexthop
->next
;
224 rib
->nexthop
= nexthop
->next
;
231 rib_nexthop_ifindex_add (struct rib
*rib
, ifindex_t ifindex
)
233 struct nexthop
*nexthop
;
235 nexthop
= nexthop_new();
236 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
237 nexthop
->ifindex
= ifindex
;
239 rib_nexthop_add (rib
, nexthop
);
245 rib_nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
247 struct nexthop
*nexthop
;
249 nexthop
= nexthop_new();
250 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
251 nexthop
->gate
.ipv4
= *ipv4
;
253 nexthop
->src
.ipv4
= *src
;
255 rib_nexthop_add (rib
, nexthop
);
261 rib_nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
262 struct in_addr
*src
, ifindex_t ifindex
)
264 struct nexthop
*nexthop
;
265 struct interface
*ifp
;
267 nexthop
= nexthop_new();
268 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
269 nexthop
->gate
.ipv4
= *ipv4
;
271 nexthop
->src
.ipv4
= *src
;
272 nexthop
->ifindex
= ifindex
;
273 ifp
= if_lookup_by_index (nexthop
->ifindex
);
274 /*Pending: need to think if null ifp here is ok during bootup?
275 There was a crash because ifp here was coming to be NULL */
277 if (connected_is_unnumbered(ifp
)) {
278 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
281 rib_nexthop_add (rib
, nexthop
);
287 rib_nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
289 struct nexthop
*nexthop
;
291 nexthop
= nexthop_new();
292 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
293 nexthop
->gate
.ipv6
= *ipv6
;
295 rib_nexthop_add (rib
, nexthop
);
301 rib_nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
304 struct nexthop
*nexthop
;
306 nexthop
= nexthop_new();
307 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
308 nexthop
->gate
.ipv6
= *ipv6
;
309 nexthop
->ifindex
= ifindex
;
311 rib_nexthop_add (rib
, nexthop
);
317 rib_nexthop_blackhole_add (struct rib
*rib
)
319 struct nexthop
*nexthop
;
321 nexthop
= nexthop_new();
322 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
323 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
325 rib_nexthop_add (rib
, nexthop
);
330 /* This method checks whether a recursive nexthop has at
331 * least one resolved nexthop in the fib.
334 nexthop_has_fib_child(struct nexthop
*nexthop
)
338 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
341 for (nh
= nexthop
->resolved
; nh
; nh
= nh
->next
)
342 if (CHECK_FLAG (nh
->flags
, NEXTHOP_FLAG_FIB
))
348 /* If force flag is not set, do not modify falgs at all for uninstall
349 the route from FIB. */
351 nexthop_active (afi_t afi
, struct rib
*rib
, struct nexthop
*nexthop
, int set
,
352 struct route_node
*top
)
355 struct route_table
*table
;
356 struct route_node
*rn
;
359 struct nexthop
*newhop
, *tnewhop
;
360 struct nexthop
*resolved_hop
;
362 struct interface
*ifp
;
364 if ((nexthop
->type
== NEXTHOP_TYPE_IPV4
) || nexthop
->type
== NEXTHOP_TYPE_IPV6
)
365 nexthop
->ifindex
= 0;
369 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
370 zebra_deregister_rnh_static_nexthops(rib
->vrf_id
, nexthop
->resolved
, top
);
371 nexthops_free(nexthop
->resolved
);
372 nexthop
->resolved
= NULL
;
373 rib
->nexthop_mtu
= 0;
376 /* Skip nexthops that have been filtered out due to route-map */
377 /* The nexthops are specific to this route and so the same */
378 /* nexthop for a different route may not have this flag set */
379 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
383 * Check to see if we should trust the passed in information
384 * for UNNUMBERED interfaces as that we won't find the GW
385 * address in the routing table.
387 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
389 ifp
= if_lookup_by_index (nexthop
->ifindex
);
390 if (ifp
&& connected_is_unnumbered(ifp
))
392 if (if_is_operative(ifp
))
401 /* Make lookup prefix. */
402 memset (&p
, 0, sizeof (struct prefix
));
407 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
408 p
.u
.prefix4
= nexthop
->gate
.ipv4
;
412 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
413 p
.u
.prefix6
= nexthop
->gate
.ipv6
;
416 assert (afi
!= AFI_IP
&& afi
!= AFI_IP6
);
420 table
= zebra_vrf_table (afi
, SAFI_UNICAST
, rib
->vrf_id
);
424 rn
= route_node_match (table
, (struct prefix
*) &p
);
427 route_unlock_node (rn
);
429 /* If lookup self prefix return immediately. */
433 /* Pick up selected route. */
434 /* However, do not resolve over default route unless explicitly allowed. */
435 if (is_default_prefix (&rn
->p
) &&
436 !nh_resolve_via_default (p
.family
))
439 RNODE_FOREACH_RIB (rn
, match
)
441 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
444 /* if the next hop is imported from another table, skip it */
445 if (match
->type
== ZEBRA_ROUTE_TABLE
)
447 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
451 /* If there is no selected route or matched route is EGP, go up
457 } while (rn
&& rn
->info
== NULL
);
459 route_lock_node (rn
);
463 /* If the longest prefix match for the nexthop yields
464 * a blackhole, mark it as inactive. */
465 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
466 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
469 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
471 /* Directly point connected route. */
472 newhop
= match
->nexthop
;
475 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
||
476 nexthop
->type
== NEXTHOP_TYPE_IPV6
)
477 nexthop
->ifindex
= newhop
->ifindex
;
481 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
484 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
485 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
486 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
490 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
491 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
493 resolved_hop
= nexthop_new();
494 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
495 /* If the resolving route specifies a gateway, use it */
496 if (newhop
->type
== NEXTHOP_TYPE_IPV4
497 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
499 resolved_hop
->type
= newhop
->type
;
500 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
504 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
505 resolved_hop
->ifindex
= newhop
->ifindex
;
506 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
507 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
510 if (newhop
->type
== NEXTHOP_TYPE_IPV6
511 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
513 resolved_hop
->type
= newhop
->type
;
514 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
518 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
519 resolved_hop
->ifindex
= newhop
->ifindex
;
523 /* If the resolving route is an interface route,
524 * it means the gateway we are looking up is connected
525 * to that interface. (The actual network is _not_ onlink).
526 * Therefore, the resolved route should have the original
527 * gateway as nexthop as it is directly connected.
529 * On Linux, we have to set the onlink netlink flag because
530 * otherwise, the kernel won't accept the route. */
531 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
533 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
536 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
537 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
539 else if (afi
== AFI_IP6
)
541 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
542 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
544 resolved_hop
->ifindex
= newhop
->ifindex
;
547 nexthop_add(&nexthop
->resolved
, resolved_hop
);
553 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
556 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
557 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
561 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
563 resolved_hop
= nexthop_new();
564 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
565 /* If the resolving route specifies a gateway, use it */
566 if (newhop
->type
== NEXTHOP_TYPE_IPV4
567 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
569 resolved_hop
->type
= newhop
->type
;
570 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
574 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
575 resolved_hop
->ifindex
= newhop
->ifindex
;
576 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
577 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
580 if (newhop
->type
== NEXTHOP_TYPE_IPV6
581 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
583 resolved_hop
->type
= newhop
->type
;
584 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
588 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
589 resolved_hop
->ifindex
= newhop
->ifindex
;
593 /* If the resolving route is an interface route,
594 * it means the gateway we are looking up is connected
595 * to that interface. (The actual network is _not_ onlink).
596 * Therefore, the resolved route should have the original
597 * gateway as nexthop as it is directly connected.
599 * On Linux, we have to set the onlink netlink flag because
600 * otherwise, the kernel won't accept the route.
602 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
604 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
607 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
608 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
610 else if (afi
== AFI_IP6
)
612 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
613 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
615 resolved_hop
->ifindex
= newhop
->ifindex
;
618 nexthop_add(&nexthop
->resolved
, resolved_hop
);
623 rib
->nexthop_mtu
= match
->mtu
;
636 rib_match (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
,
637 union g_addr
*addr
, struct route_node
**rn_out
)
640 struct route_table
*table
;
641 struct route_node
*rn
;
643 struct nexthop
*newhop
, *tnewhop
;
647 table
= zebra_vrf_table (afi
, safi
, vrf_id
);
651 memset (&p
, 0, sizeof (struct prefix
));
655 p
.u
.prefix4
= addr
->ipv4
;
656 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
660 p
.u
.prefix6
= addr
->ipv6
;
661 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
664 rn
= route_node_match (table
, (struct prefix
*) &p
);
668 route_unlock_node (rn
);
670 /* Pick up selected route. */
671 RNODE_FOREACH_RIB (rn
, match
)
673 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
675 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
679 /* If there is no selected route or matched route is EGP, go up
685 } while (rn
&& rn
->info
== NULL
);
687 route_lock_node (rn
);
691 if (match
->type
!= ZEBRA_ROUTE_CONNECT
)
694 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
695 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
713 rib_match_ipv4_multicast (vrf_id_t vrf_id
, struct in_addr addr
, struct route_node
**rn_out
)
715 struct rib
*rib
= NULL
, *mrib
= NULL
, *urib
= NULL
;
716 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
717 union g_addr gaddr
= { .ipv4
= addr
};
719 switch (ipv4_multicast_mode
)
721 case MCAST_MRIB_ONLY
:
722 return rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, rn_out
);
723 case MCAST_URIB_ONLY
:
724 return rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, rn_out
);
725 case MCAST_NO_CONFIG
:
726 case MCAST_MIX_MRIB_FIRST
:
727 rib
= mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
729 rib
= urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
731 case MCAST_MIX_DISTANCE
:
732 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
733 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
735 rib
= urib
->distance
< mrib
->distance
? urib
: mrib
;
741 case MCAST_MIX_PFXLEN
:
742 mrib
= rib_match (AFI_IP
, SAFI_MULTICAST
, vrf_id
, &gaddr
, &m_rn
);
743 urib
= rib_match (AFI_IP
, SAFI_UNICAST
, vrf_id
, &gaddr
, &u_rn
);
745 rib
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? urib
: mrib
;
754 *rn_out
= (rib
== mrib
) ? m_rn
: u_rn
;
756 if (IS_ZEBRA_DEBUG_RIB
)
759 inet_ntop (AF_INET
, &addr
, buf
, BUFSIZ
);
761 zlog_debug("%s: %s: found %s, using %s",
763 mrib
? (urib
? "MRIB+URIB" : "MRIB") :
764 urib
? "URIB" : "nothing",
765 rib
== urib
? "URIB" : rib
== mrib
? "MRIB" : "none");
771 multicast_mode_ipv4_set (enum multicast_mode mode
)
773 if (IS_ZEBRA_DEBUG_RIB
)
774 zlog_debug("%s: multicast lookup mode set (%d)", __func__
, mode
);
775 ipv4_multicast_mode
= mode
;
779 multicast_mode_ipv4_get (void)
781 return ipv4_multicast_mode
;
785 rib_lookup_ipv4 (struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
787 struct route_table
*table
;
788 struct route_node
*rn
;
790 struct nexthop
*nexthop
, *tnexthop
;
794 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
798 rn
= route_node_lookup (table
, (struct prefix
*) p
);
800 /* No route for this prefix. */
805 route_unlock_node (rn
);
807 RNODE_FOREACH_RIB (rn
, match
)
809 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
811 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
818 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
821 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
822 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
829 * This clone function, unlike its original rib_lookup_ipv4(), checks
830 * if specified IPv4 route record (prefix/mask -> gate) exists in
831 * the whole RIB and has RIB_ENTRY_SELECTED_FIB set.
835 * 0: exact match found
836 * 1: a match was found with a different gate
837 * 2: connected route found
838 * 3: no matches found
841 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
,
844 struct route_table
*table
;
845 struct route_node
*rn
;
847 struct nexthop
*nexthop
, *tnexthop
;
852 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
854 return ZEBRA_RIB_LOOKUP_ERROR
;
856 /* Scan the RIB table for exactly matching RIB entry. */
857 rn
= route_node_lookup (table
, (struct prefix
*) p
);
859 /* No route for this prefix. */
861 return ZEBRA_RIB_NOTFOUND
;
864 route_unlock_node (rn
);
866 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
867 RNODE_FOREACH_RIB (rn
, match
)
869 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
871 if (CHECK_FLAG (match
->status
, RIB_ENTRY_SELECTED_FIB
))
875 /* None such found :( */
877 return ZEBRA_RIB_NOTFOUND
;
879 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
880 return ZEBRA_RIB_FOUND_CONNECTED
;
882 /* Ok, we have a cood candidate, let's check it's nexthop list... */
884 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
885 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
888 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip (qgate
))
889 return ZEBRA_RIB_FOUND_EXACT
;
890 if (IS_ZEBRA_DEBUG_RIB
)
892 char gate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
893 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
894 inet_ntop (AF_INET
, &sockunion2ip(qgate
), qgate_buf
, INET_ADDRSTRLEN
);
895 zlog_debug ("%s: qgate == %s, %s == %s", __func__
,
896 qgate_buf
, recursing
? "rgate" : "gate", gate_buf
);
901 return ZEBRA_RIB_FOUND_NOGATE
;
903 return ZEBRA_RIB_NOTFOUND
;
906 #define RIB_SYSTEM_ROUTE(R) \
907 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
909 /* This function verifies reachability of one given nexthop, which can be
910 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
911 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
912 * nexthop->ifindex will be updated appropriately as well.
913 * An existing route map can turn (otherwise active) nexthop into inactive, but
916 * The return value is the final value of 'ACTIVE' flag.
920 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
921 struct nexthop
*nexthop
, int set
)
923 struct interface
*ifp
;
924 route_map_result_t ret
= RMAP_MATCH
;
926 char buf
[SRCDEST2STR_BUFFER
];
927 struct prefix
*p
, *src_p
;
928 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
930 if (rn
->p
.family
== AF_INET
)
932 else if (rn
->p
.family
== AF_INET6
)
936 switch (nexthop
->type
)
938 case NEXTHOP_TYPE_IFINDEX
:
939 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
940 if (ifp
&& if_is_operative(ifp
))
941 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
943 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
945 case NEXTHOP_TYPE_IPV4
:
946 case NEXTHOP_TYPE_IPV4_IFINDEX
:
948 if (nexthop_active (AFI_IP
, rib
, nexthop
, set
, rn
))
949 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
951 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
953 case NEXTHOP_TYPE_IPV6
:
955 if (nexthop_active (AFI_IP6
, rib
, nexthop
, set
, rn
))
956 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
958 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
960 case NEXTHOP_TYPE_IPV6_IFINDEX
:
961 /* RFC 5549, v4 prefix with v6 NH */
962 if (rn
->p
.family
!= AF_INET
)
964 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
966 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
967 if (ifp
&& if_is_operative(ifp
))
968 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
970 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
974 if (nexthop_active (AFI_IP6
, rib
, nexthop
, set
, rn
))
975 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
977 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
980 case NEXTHOP_TYPE_BLACKHOLE
:
981 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
986 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
989 /* XXX: What exactly do those checks do? Do we support
990 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
991 if (RIB_SYSTEM_ROUTE(rib
) ||
992 (family
== AFI_IP
&& p
->family
!= AF_INET
) ||
993 (family
== AFI_IP6
&& p
->family
!= AF_INET6
))
994 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
996 /* The original code didn't determine the family correctly
997 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
998 * from the rib_table_info in those cases.
999 * Possibly it may be better to use only the rib_table_info
1004 rib_table_info_t
*info
;
1006 info
= srcdest_rnode_table_info(rn
);
1010 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
1012 /* It'll get set if required inside */
1013 ret
= zebra_route_map_check(family
, rib
->type
, p
, nexthop
, rib
->vrf_id
,
1015 if (ret
== RMAP_DENYMATCH
)
1017 if (IS_ZEBRA_DEBUG_RIB
)
1019 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1020 zlog_debug("%u:%s: Filtering out with NH out %s due to route map",
1022 ifindex2ifname_vrf (nexthop
->ifindex
, rib
->vrf_id
));
1024 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1026 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1029 /* Iterate over all nexthops of the given RIB entry and refresh their
1030 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1031 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1032 * is flagged with RIB_ENTRY_CHANGED. The 4th 'set' argument is
1033 * transparently passed to nexthop_active_check().
1035 * Return value is the new number of active nexthops.
1039 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
1041 struct nexthop
*nexthop
;
1042 union g_addr prev_src
;
1043 unsigned int prev_active
, new_active
, old_num_nh
;
1044 ifindex_t prev_index
;
1045 old_num_nh
= rib
->nexthop_active_num
;
1047 rib
->nexthop_active_num
= 0;
1048 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1050 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1052 /* No protocol daemon provides src and so we're skipping tracking it */
1053 prev_src
= nexthop
->rmap_src
;
1054 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1055 prev_index
= nexthop
->ifindex
;
1056 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
1057 rib
->nexthop_active_num
++;
1058 /* Don't allow src setting on IPv6 addr for now */
1059 if (prev_active
!= new_active
||
1060 prev_index
!= nexthop
->ifindex
||
1061 ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
&&
1062 nexthop
->type
< NEXTHOP_TYPE_IPV6
) &&
1063 prev_src
.ipv4
.s_addr
!= nexthop
->rmap_src
.ipv4
.s_addr
) ||
1064 ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
&&
1065 nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
) &&
1066 !(IPV6_ADDR_SAME (&prev_src
.ipv6
, &nexthop
->rmap_src
.ipv6
))))
1068 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1069 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1073 if (old_num_nh
!= rib
->nexthop_active_num
)
1074 SET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1076 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_CHANGED
))
1078 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1081 return rib
->nexthop_active_num
;
1086 /* Update flag indicates whether this is a "replace" or not. Currently, this
1087 * is only used for IPv4.
1090 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
, struct rib
*old
)
1093 struct nexthop
*nexthop
, *tnexthop
;
1094 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1096 struct prefix
*p
, *src_p
;
1098 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
1100 if (info
->safi
!= SAFI_UNICAST
)
1102 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1103 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1108 * Make sure we update the FPM any time we send new information to
1111 zfpm_trigger_update (rn
, "installing in kernel");
1112 ret
= kernel_route_rib (p
, src_p
, old
, rib
);
1114 /* If install succeeds, update FIB flag for nexthops. */
1117 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1119 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1122 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1123 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1125 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1132 /* Uninstall the route from kernel. */
1134 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
1137 struct nexthop
*nexthop
, *tnexthop
;
1138 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1140 struct prefix
*p
, *src_p
;
1142 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
1144 if (info
->safi
!= SAFI_UNICAST
)
1146 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1147 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1152 * Make sure we update the FPM any time we send new information to
1155 zfpm_trigger_update (rn
, "uninstalling from kernel");
1156 ret
= kernel_route_rib (p
, src_p
, rib
, NULL
);
1158 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1159 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1164 /* Uninstall the route from kernel. */
1166 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
1168 rib_table_info_t
*info
= srcdest_rnode_table_info(rn
);
1170 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
1172 if (info
->safi
== SAFI_UNICAST
)
1173 zfpm_trigger_update (rn
, "rib_uninstall");
1175 if (! RIB_SYSTEM_ROUTE (rib
))
1176 rib_uninstall_kernel (rn
, rib
);
1177 UNSET_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
);
1180 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1182 struct prefix
*p
, *src_p
;
1183 srcdest_rnode_prefixes (rn
, &p
, &src_p
);
1185 redistribute_delete (p
, src_p
, rib
);
1186 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
1191 * rib_can_delete_dest
1193 * Returns TRUE if the given dest can be deleted from the table.
1196 rib_can_delete_dest (rib_dest_t
*dest
)
1204 * Don't delete the dest if we have to update the FPM about this
1207 if (CHECK_FLAG (dest
->flags
, RIB_DEST_UPDATE_FPM
) ||
1208 CHECK_FLAG (dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1217 * Garbage collect the rib dest corresponding to the given route node
1220 * Returns TRUE if the dest was deleted, FALSE otherwise.
1223 rib_gc_dest (struct route_node
*rn
)
1226 struct zebra_vrf
*zvrf
;
1228 dest
= rib_dest_from_rnode (rn
);
1232 if (!rib_can_delete_dest (dest
))
1235 zvrf
= rib_dest_vrf (dest
);
1236 if (IS_ZEBRA_DEBUG_RIB
)
1237 rnode_debug (rn
, zvrf_id (zvrf
), "removing dest from table");
1240 XFREE (MTYPE_RIB_DEST
, dest
);
1244 * Release the one reference that we keep on the route node.
1246 route_unlock_node (rn
);
1251 rib_process_add_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1254 zfpm_trigger_update (rn
, "new route selected");
1256 /* Update real nexthop. This may actually determine if nexthop is active or not. */
1257 if (!nexthop_active_update (rn
, new, 1))
1259 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1263 SET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1264 if (IS_ZEBRA_DEBUG_RIB
)
1266 char buf
[SRCDEST2STR_BUFFER
];
1267 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1268 zlog_debug ("%u:%s: Adding route rn %p, rib %p (type %d)",
1269 zvrf_id (zvrf
), buf
, rn
, new, new->type
);
1272 if (!RIB_SYSTEM_ROUTE (new))
1274 if (rib_install_kernel (rn
, new, NULL
))
1276 char buf
[SRCDEST2STR_BUFFER
];
1277 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1278 zlog_warn ("%u:%s: Route install failed",
1279 zvrf_id (zvrf
), buf
);
1283 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1287 rib_process_del_fib(struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1290 zfpm_trigger_update (rn
, "removing existing route");
1292 /* Uninstall from kernel. */
1293 if (IS_ZEBRA_DEBUG_RIB
)
1295 char buf
[SRCDEST2STR_BUFFER
];
1296 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1297 zlog_debug ("%u:%s: Deleting route rn %p, rib %p (type %d)",
1298 zvrf_id (zvrf
), buf
, rn
, old
, old
->type
);
1301 if (!RIB_SYSTEM_ROUTE (old
))
1302 rib_uninstall_kernel (rn
, old
);
1304 UNSET_FLAG (old
->status
, RIB_ENTRY_SELECTED_FIB
);
1306 /* Update nexthop for route, reset changed flag. */
1307 nexthop_active_update (rn
, old
, 1);
1308 UNSET_FLAG(old
->status
, RIB_ENTRY_CHANGED
);
1312 rib_process_update_fib (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1313 struct rib
*old
, struct rib
*new)
1315 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1321 * We have to install or update if a new route has been selected or
1322 * something has changed.
1325 CHECK_FLAG (new->status
, RIB_ENTRY_CHANGED
))
1327 zfpm_trigger_update (rn
, "updating existing route");
1329 /* Update the nexthop; we could determine here that nexthop is inactive. */
1330 if (nexthop_active_update (rn
, new, 1))
1333 /* If nexthop is active, install the selected route, if appropriate. If
1334 * the install succeeds, cleanup flags for prior route, if different from
1339 if (IS_ZEBRA_DEBUG_RIB
)
1341 char buf
[SRCDEST2STR_BUFFER
];
1342 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1344 zlog_debug ("%u:%s: Updating route rn %p, rib %p (type %d) "
1345 "old %p (type %d)", zvrf_id (zvrf
), buf
,
1346 rn
, new, new->type
, old
, old
->type
);
1348 zlog_debug ("%u:%s: Updating route rn %p, rib %p (type %d)",
1349 zvrf_id (zvrf
), buf
, rn
, new, new->type
);
1351 /* Non-system route should be installed. */
1352 if (!RIB_SYSTEM_ROUTE (new))
1354 if (rib_install_kernel (rn
, new, old
))
1356 char buf
[SRCDEST2STR_BUFFER
];
1357 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1359 zlog_warn ("%u:%s: Route install failed", zvrf_id (zvrf
), buf
);
1363 /* If install succeeded or system route, cleanup flags for prior route. */
1364 if (installed
&& new != old
)
1366 if (RIB_SYSTEM_ROUTE(new))
1368 if (!RIB_SYSTEM_ROUTE (old
))
1369 rib_uninstall_kernel (rn
, old
);
1373 for (nexthop
= old
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1374 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1378 /* Update for redistribution. */
1380 SET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1384 * If nexthop for selected route is not active or install failed, we
1385 * may need to uninstall and delete for redistribution.
1387 if (!nh_active
|| !installed
)
1389 if (IS_ZEBRA_DEBUG_RIB
)
1391 char buf
[SRCDEST2STR_BUFFER
];
1392 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1394 zlog_debug ("%u:%s: Deleting route rn %p, rib %p (type %d) "
1395 "old %p (type %d) - %s", zvrf_id (zvrf
), buf
,
1396 rn
, new, new->type
, old
, old
->type
,
1397 nh_active
? "install failed" : "nexthop inactive");
1399 zlog_debug ("%u:%s: Deleting route rn %p, rib %p (type %d) - %s",
1400 zvrf_id (zvrf
), buf
, rn
, new, new->type
,
1401 nh_active
? "install failed" : "nexthop inactive");
1404 if (!RIB_SYSTEM_ROUTE (old
))
1405 rib_uninstall_kernel (rn
, old
);
1406 UNSET_FLAG (new->status
, RIB_ENTRY_SELECTED_FIB
);
1412 * Same route selected; check if in the FIB and if not, re-install. This
1413 * is housekeeping code to deal with race conditions in kernel with linux
1414 * netlink reporting interface up before IPv4 or IPv6 protocol is ready
1417 if (!RIB_SYSTEM_ROUTE (new))
1421 for (ALL_NEXTHOPS_RO(new->nexthop
, nexthop
, tnexthop
, recursing
))
1422 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1428 rib_install_kernel (rn
, new, NULL
);
1432 /* Update prior route. */
1435 UNSET_FLAG (old
->status
, RIB_ENTRY_SELECTED_FIB
);
1437 /* Set real nexthop. */
1438 nexthop_active_update (rn
, old
, 1);
1439 UNSET_FLAG(old
->status
, RIB_ENTRY_CHANGED
);
1442 /* Clear changed flag. */
1443 UNSET_FLAG(new->status
, RIB_ENTRY_CHANGED
);
1446 /* Check if 'alternate' RIB entry is better than 'current'. */
1448 rib_choose_best (struct rib
*current
, struct rib
*alternate
)
1450 if (current
== NULL
)
1453 /* filter route selection in following order:
1454 * - connected beats other types
1455 * - lower distance beats higher
1456 * - lower metric beats higher for equal distance
1457 * - last, hence oldest, route wins tie break.
1460 /* Connected routes. Pick the last connected
1461 * route of the set of lowest metric connected routes.
1463 if (alternate
->type
== ZEBRA_ROUTE_CONNECT
)
1465 if (current
->type
!= ZEBRA_ROUTE_CONNECT
1466 || alternate
->metric
<= current
->metric
)
1472 if (current
->type
== ZEBRA_ROUTE_CONNECT
)
1475 /* higher distance loses */
1476 if (alternate
->distance
< current
->distance
)
1478 if (current
->distance
< alternate
->distance
)
1481 /* metric tie-breaks equal distance */
1482 if (alternate
->metric
<= current
->metric
)
1488 /* Core function for processing routing information base. */
1490 rib_process (struct route_node
*rn
)
1494 struct rib
*old_selected
= NULL
;
1495 struct rib
*new_selected
= NULL
;
1496 struct rib
*old_fib
= NULL
;
1497 struct rib
*new_fib
= NULL
;
1498 struct rib
*best
= NULL
;
1499 char buf
[SRCDEST2STR_BUFFER
];
1501 struct zebra_vrf
*zvrf
= NULL
;
1502 struct prefix
*p
, *src_p
;
1503 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
1504 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1508 dest
= rib_dest_from_rnode (rn
);
1511 zvrf
= rib_dest_vrf (dest
);
1512 vrf_id
= zvrf_id (zvrf
);
1515 if (IS_ZEBRA_DEBUG_RIB
)
1516 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
1518 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1519 zlog_debug ("%u:%s: Processing rn %p", vrf_id
, buf
, rn
);
1521 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1523 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1524 zlog_debug ("%u:%s: Examine rib %p (type %d) status %x flags %x "
1525 "dist %d metric %d",
1526 vrf_id
, buf
, rib
, rib
->type
, rib
->status
,
1527 rib
->flags
, rib
->distance
, rib
->metric
);
1529 UNSET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1531 /* Currently selected rib. */
1532 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1534 assert (old_selected
== NULL
);
1537 /* Currently in fib */
1538 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
1540 assert (old_fib
== NULL
);
1544 /* Skip deleted entries from selection */
1545 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1548 /* Skip unreachable nexthop. */
1549 /* This first call to nexthop_active_update is merely to determine if
1550 * there's any change to nexthops associated with this RIB entry. Now,
1551 * rib_process() can be invoked due to an external event such as link
1552 * down or due to next-hop-tracking evaluation. In the latter case,
1553 * a decision has already been made that the NHs have changed. So, no
1554 * need to invoke a potentially expensive call again. Further, since
1555 * the change might be in a recursive NH which is not caught in
1556 * the nexthop_active_update() code. Thus, we might miss changes to
1559 if (!CHECK_FLAG(rib
->status
, RIB_ENTRY_CHANGED
) &&
1560 ! nexthop_active_update (rn
, rib
, 0))
1562 if (rib
->type
== ZEBRA_ROUTE_TABLE
)
1564 /* XXX: HERE BE DRAGONS!!!!!
1565 * In all honesty, I have not yet figured out what this part
1566 * does or why the RIB_ENTRY_CHANGED test above is correct
1567 * or why we need to delete a route here, and also not whether
1568 * this concerns both selected and fib route, or only selected
1570 /* This entry was denied by the 'ip protocol table' route-map, we
1571 * need to delete it */
1572 if (rib
!= old_selected
)
1574 if (IS_ZEBRA_DEBUG_RIB
)
1575 zlog_debug ("%s: %s: imported via import-table but denied "
1576 "by the ip protocol table route-map",
1578 rib_unlink (rn
, rib
);
1581 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1587 /* Infinite distance. */
1588 if (rib
->distance
== DISTANCE_INFINITY
)
1590 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1594 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_FIB_OVERRIDE
))
1596 best
= rib_choose_best(new_fib
, rib
);
1597 if (new_fib
&& best
!= new_fib
)
1598 UNSET_FLAG (new_fib
->status
, RIB_ENTRY_CHANGED
);
1603 best
= rib_choose_best(new_selected
, rib
);
1604 if (new_selected
&& best
!= new_selected
)
1605 UNSET_FLAG (new_selected
->status
, RIB_ENTRY_CHANGED
);
1606 new_selected
= best
;
1609 UNSET_FLAG (rib
->status
, RIB_ENTRY_CHANGED
);
1610 } /* RNODE_FOREACH_RIB */
1612 /* If no FIB override route, use the selected route also for FIB */
1613 if (new_fib
== NULL
)
1614 new_fib
= new_selected
;
1616 /* After the cycle is finished, the following pointers will be set:
1617 * old_selected --- RIB entry currently having SELECTED
1618 * new_selected --- RIB entry that is newly SELECTED
1619 * old_fib --- RIB entry currently in kernel FIB
1620 * new_fib --- RIB entry that is newly to be in kernel FIB
1622 * new_selected will get SELECTED flag, and is going to be redistributed
1623 * the zclients. new_fib (which can be new_selected) will be installed in kernel.
1626 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1628 zlog_debug ("%u:%s: After processing: old_selected %p new_selected %p old_fib %p new_fib %p",
1630 (void *)old_selected
,
1631 (void *)new_selected
,
1636 /* Buffer RIB_ENTRY_CHANGED here, because it will get cleared if
1637 * fib == selected */
1638 bool selected_changed
= new_selected
&& CHECK_FLAG(new_selected
->status
,
1641 /* Update fib according to selection results */
1642 if (new_fib
&& old_fib
)
1643 rib_process_update_fib (zvrf
, rn
, old_fib
, new_fib
);
1645 rib_process_add_fib (zvrf
, rn
, new_fib
);
1647 rib_process_del_fib (zvrf
, rn
, old_fib
);
1649 /* Redistribute SELECTED entry */
1650 if (old_selected
!= new_selected
|| selected_changed
)
1652 struct nexthop
*nexthop
, *tnexthop
;
1655 /* Check if we have a FIB route for the destination, otherwise,
1656 * don't redistribute it */
1657 for (ALL_NEXTHOPS_RO(new_fib
? new_fib
->nexthop
: NULL
, nexthop
,
1658 tnexthop
, recursing
))
1660 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1666 new_selected
= NULL
;
1668 if (new_selected
&& new_selected
!= new_fib
)
1670 nexthop_active_update(rn
, new_selected
, 1);
1671 UNSET_FLAG(new_selected
->status
, RIB_ENTRY_CHANGED
);
1677 redistribute_delete(p
, src_p
, old_selected
);
1678 if (old_selected
!= new_selected
)
1679 UNSET_FLAG (old_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1684 /* Install new or replace existing redistributed entry */
1685 SET_FLAG (new_selected
->flags
, ZEBRA_FLAG_SELECTED
);
1686 redistribute_update (p
, src_p
, new_selected
, old_selected
);
1690 /* Remove all RIB entries queued for removal */
1691 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1693 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1695 if (IS_ZEBRA_DEBUG_RIB
)
1697 rnode_debug (rn
, vrf_id
, "rn %p, removing rib %p",
1698 (void *)rn
, (void *)rib
);
1700 rib_unlink(rn
, rib
);
1705 * Check if the dest can be deleted now.
1710 /* Take a list of route_node structs and return 1, if there was a record
1711 * picked from it and processed by rib_process(). Don't process more,
1712 * than one RN record; operate only in the specified sub-queue.
1715 process_subq (struct list
* subq
, u_char qindex
)
1717 struct listnode
*lnode
= listhead (subq
);
1718 struct route_node
*rnode
;
1720 struct zebra_vrf
*zvrf
= NULL
;
1725 rnode
= listgetdata (lnode
);
1726 dest
= rib_dest_from_rnode (rnode
);
1728 zvrf
= rib_dest_vrf (dest
);
1730 rib_process (rnode
);
1732 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1734 char buf
[SRCDEST2STR_BUFFER
];
1735 srcdest_rnode2str(rnode
, buf
, sizeof(buf
));
1736 zlog_debug ("%u:%s: rn %p dequeued from sub-queue %u",
1737 zvrf
? zvrf_id (zvrf
) : 0, buf
, rnode
, qindex
);
1741 UNSET_FLAG (rib_dest_from_rnode (rnode
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1746 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1747 __func__
, rnode
, rnode
->lock
);
1748 zlog_backtrace(LOG_DEBUG
);
1751 route_unlock_node (rnode
);
1752 list_delete_node (subq
, lnode
);
1757 * All meta queues have been processed. Trigger next-hop evaluation.
1760 meta_queue_process_complete (struct work_queue
*dummy
)
1763 struct zebra_vrf
*zvrf
;
1765 /* Evaluate nexthops for those VRFs which underwent route processing. This
1766 * should limit the evaluation to the necessary VRFs in most common
1769 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
1772 if (zvrf
== NULL
|| !(zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
1775 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
1776 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET
, 0, RNH_NEXTHOP_TYPE
, NULL
);
1777 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
1778 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
1779 zebra_evaluate_rnh(zvrf_id (zvrf
), AF_INET6
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
1782 /* Schedule LSPs for processing, if needed. */
1783 zvrf
= vrf_info_lookup(VRF_DEFAULT
);
1784 if (mpls_should_lsps_be_processed(zvrf
))
1786 if (IS_ZEBRA_DEBUG_MPLS
)
1787 zlog_debug ("%u: Scheduling all LSPs upon RIB completion", zvrf_id (zvrf
));
1788 zebra_mpls_lsp_schedule (zvrf
);
1789 mpls_unmark_lsps_for_processing(zvrf
);
1793 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1794 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1795 * is pointed to the meta queue structure.
1797 static wq_item_status
1798 meta_queue_process (struct work_queue
*dummy
, void *data
)
1800 struct meta_queue
* mq
= data
;
1803 for (i
= 0; i
< MQ_SIZE
; i
++)
1804 if (process_subq (mq
->subq
[i
], i
))
1809 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1813 * Map from rib types to queue type (priority) in meta queue
1815 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1816 [ZEBRA_ROUTE_SYSTEM
] = 4,
1817 [ZEBRA_ROUTE_KERNEL
] = 0,
1818 [ZEBRA_ROUTE_CONNECT
] = 0,
1819 [ZEBRA_ROUTE_STATIC
] = 1,
1820 [ZEBRA_ROUTE_RIP
] = 2,
1821 [ZEBRA_ROUTE_RIPNG
] = 2,
1822 [ZEBRA_ROUTE_OSPF
] = 2,
1823 [ZEBRA_ROUTE_OSPF6
] = 2,
1824 [ZEBRA_ROUTE_ISIS
] = 2,
1825 [ZEBRA_ROUTE_BGP
] = 3,
1826 [ZEBRA_ROUTE_HSLS
] = 4,
1827 [ZEBRA_ROUTE_TABLE
] = 1,
1830 /* Look into the RN and queue it into one or more priority queues,
1831 * increasing the size for each data push done.
1834 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
1838 RNODE_FOREACH_RIB (rn
, rib
)
1840 u_char qindex
= meta_queue_map
[rib
->type
];
1841 struct zebra_vrf
*zvrf
;
1843 /* Invariant: at this point we always have rn->info set. */
1844 if (CHECK_FLAG (rib_dest_from_rnode (rn
)->flags
,
1845 RIB_ROUTE_QUEUED (qindex
)))
1847 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1848 rnode_debug (rn
, rib
->vrf_id
, "rn %p is already queued in sub-queue %u",
1849 (void *)rn
, qindex
);
1853 SET_FLAG (rib_dest_from_rnode (rn
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1854 listnode_add (mq
->subq
[qindex
], rn
);
1855 route_lock_node (rn
);
1858 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1859 rnode_debug (rn
, rib
->vrf_id
, "queued rn %p into sub-queue %u",
1860 (void *)rn
, qindex
);
1862 zvrf
= zebra_vrf_lookup_by_id (rib
->vrf_id
);
1864 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
1868 /* Add route_node to work queue and schedule processing */
1870 rib_queue_add (struct route_node
*rn
)
1874 /* Pointless to queue a route_node with no RIB entries to add or remove */
1875 if (!rnode_to_ribs (rn
))
1877 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1878 __func__
, (void *)rn
, rn
->lock
);
1879 zlog_backtrace(LOG_DEBUG
);
1883 if (zebrad
.ribq
== NULL
)
1885 zlog_err ("%s: work_queue does not exist!", __func__
);
1890 * The RIB queue should normally be either empty or holding the only
1891 * work_queue_item element. In the latter case this element would
1892 * hold a pointer to the meta queue structure, which must be used to
1893 * actually queue the route nodes to process. So create the MQ
1894 * holder, if necessary, then push the work into it in any case.
1895 * This semantics was introduced after 0.99.9 release.
1897 if (!zebrad
.ribq
->items
->count
)
1898 work_queue_add (zebrad
.ribq
, zebrad
.mq
);
1900 rib_meta_queue_add (zebrad
.mq
, rn
);
1905 /* Create new meta queue.
1906 A destructor function doesn't seem to be necessary here.
1908 static struct meta_queue
*
1909 meta_queue_new (void)
1911 struct meta_queue
*new;
1914 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
1917 for (i
= 0; i
< MQ_SIZE
; i
++)
1919 new->subq
[i
] = list_new ();
1920 assert(new->subq
[i
]);
1927 meta_queue_free (struct meta_queue
*mq
)
1931 for (i
= 0; i
< MQ_SIZE
; i
++)
1932 list_delete (mq
->subq
[i
]);
1934 XFREE (MTYPE_WORK_QUEUE
, mq
);
1937 /* initialise zebra rib work queue */
1939 rib_queue_init (struct zebra_t
*zebra
)
1943 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1944 "route_node processing")))
1946 zlog_err ("%s: could not initialise work queue!", __func__
);
1950 /* fill in the work queue spec */
1951 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1952 zebra
->ribq
->spec
.errorfunc
= NULL
;
1953 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
1954 /* XXX: TODO: These should be runtime configurable via vty */
1955 zebra
->ribq
->spec
.max_retries
= 3;
1956 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
1958 if (!(zebra
->mq
= meta_queue_new ()))
1960 zlog_err ("%s: could not initialise meta queue!", __func__
);
1966 /* RIB updates are processed via a queue of pointers to route_nodes.
1968 * The queue length is bounded by the maximal size of the routing table,
1969 * as a route_node will not be requeued, if already queued.
1971 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
1972 * state, or static_install_route (when an existing RIB is updated)
1973 * and then submit route_node to queue for best-path selection later.
1974 * Order of add/delete state changes are preserved for any given RIB.
1976 * Deleted RIBs are reaped during best-path selection.
1979 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
1980 * |-------->| | best RIB, if required
1982 * static_install->|->rib_addqueue...... -> rib_process
1984 * |-------->| |-> rib_unlink
1985 * |-> set RIB_ENTRY_REMOVE |
1986 * rib_delnode (RIB freed)
1988 * The 'info' pointer of a route_node points to a rib_dest_t
1989 * ('dest'). Queueing state for a route_node is kept on the dest. The
1990 * dest is created on-demand by rib_link() and is kept around at least
1991 * as long as there are ribs hanging off it (@see rib_gc_dest()).
1993 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1995 * - route_nodes: refcounted by:
1996 * - dest attached to route_node:
1997 * - managed by: rib_link/rib_gc_dest
1998 * - route_node processing queue
1999 * - managed by: rib_addqueue, rib_process.
2003 /* Add RIB to head of the route node. */
2005 rib_link (struct route_node
*rn
, struct rib
*rib
, int process
)
2010 const char *rmap_name
;
2014 dest
= rib_dest_from_rnode (rn
);
2017 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2018 rnode_debug (rn
, rib
->vrf_id
, "rn %p adding dest", rn
);
2020 dest
= XCALLOC (MTYPE_RIB_DEST
, sizeof (rib_dest_t
));
2021 route_lock_node (rn
); /* rn route table reference */
2026 head
= dest
->routes
;
2034 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2035 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2036 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2038 rmap_name
= zebra_get_import_table_route_map (afi
, rib
->table
);
2039 zebra_add_import_table_entry(rn
, rib
, rmap_name
);
2047 rib_addnode (struct route_node
*rn
, struct rib
*rib
, int process
)
2049 /* RIB node has been un-removed before route-node is processed.
2050 * route_node must hence already be on the queue for processing..
2052 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2054 if (IS_ZEBRA_DEBUG_RIB
)
2055 rnode_debug (rn
, rib
->vrf_id
, "rn %p, un-removed rib %p", (void *)rn
, (void *)rib
);
2057 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2060 rib_link (rn
, rib
, process
);
2066 * Detach a rib structure from a route_node.
2068 * Note that a call to rib_unlink() should be followed by a call to
2069 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2070 * longer required to be deleted.
2073 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
2079 if (IS_ZEBRA_DEBUG_RIB
)
2080 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p", (void *)rn
, (void *)rib
);
2082 dest
= rib_dest_from_rnode (rn
);
2085 rib
->next
->prev
= rib
->prev
;
2088 rib
->prev
->next
= rib
->next
;
2091 dest
->routes
= rib
->next
;
2094 /* free RIB and nexthops */
2095 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, rib
->nexthop
, rn
);
2096 nexthops_free(rib
->nexthop
);
2097 XFREE (MTYPE_RIB
, rib
);
2102 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
2106 if (IS_ZEBRA_DEBUG_RIB
)
2107 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p, removing", (void *)rn
, (void *)rib
);
2108 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2110 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2111 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2112 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2114 zebra_del_import_table_entry(rn
, rib
);
2115 /* Just clean up if non main table */
2116 if (IS_ZEBRA_DEBUG_RIB
)
2118 char buf
[SRCDEST2STR_BUFFER
];
2119 srcdest_rnode2str(rn
, buf
, sizeof(buf
));
2120 zlog_debug ("%u:%s: Freeing route rn %p, rib %p (type %d)",
2121 rib
->vrf_id
, buf
, rn
, rib
, rib
->type
);
2124 rib_unlink(rn
, rib
);
2132 /* This function dumps the contents of a given RIB entry into
2133 * standard debug log. Calling function name and IP prefix in
2134 * question are passed as 1st and 2nd arguments.
2137 void _rib_dump (const char * func
,
2138 union prefixconstptr pp
,
2139 union prefixconstptr src_pp
,
2140 const struct rib
* rib
)
2142 const struct prefix
*p
= pp
.p
;
2143 const struct prefix
*src_p
= src_pp
.p
;
2144 bool is_srcdst
= src_p
&& src_p
->prefixlen
;
2145 char straddr
[PREFIX_STRLEN
];
2146 char srcaddr
[PREFIX_STRLEN
];
2147 struct nexthop
*nexthop
, *tnexthop
;
2150 zlog_debug ("%s: dumping RIB entry %p for %s%s%s vrf %u", func
, (const void *)rib
,
2151 prefix2str(pp
, straddr
, sizeof(straddr
)),
2152 is_srcdst
? " from " : "",
2153 is_srcdst
? prefix2str(src_pp
, srcaddr
, sizeof(srcaddr
)) : "",
2157 "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d",
2160 (unsigned long) rib
->uptime
,
2167 "%s: metric == %u, mtu == %u, distance == %u, flags == %u, status == %u",
2177 "%s: nexthop_num == %u, nexthop_active_num == %u",
2180 rib
->nexthop_active_num
2183 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2185 inet_ntop (p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2188 "%s: %s %s with flags %s%s%s",
2190 (recursing
? " NH" : "NH"),
2192 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
2193 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
2194 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
2197 zlog_debug ("%s: dump complete", func
);
2200 /* This is an exported helper to rtm_read() to dump the strange
2201 * RIB entry found by rib_lookup_ipv4_route()
2204 void rib_lookup_and_dump (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2206 struct route_table
*table
;
2207 struct route_node
*rn
;
2209 char prefix_buf
[INET_ADDRSTRLEN
];
2212 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2215 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2219 /* Scan the RIB table for exactly matching RIB entry. */
2220 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2222 /* No route for this prefix. */
2225 zlog_debug ("%s: lookup failed for %s", __func__
,
2226 prefix2str((struct prefix
*) p
, prefix_buf
, sizeof(prefix_buf
)));
2231 route_unlock_node (rn
);
2234 RNODE_FOREACH_RIB (rn
, rib
)
2238 "%s: rn %p, rib %p: %s, %s",
2242 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
2243 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
2245 rib_dump (p
, NULL
, rib
);
2249 /* Check if requested address assignment will fail due to another
2250 * route being installed by zebra in FIB already. Take necessary
2251 * actions, if needed: remove such a route from FIB and deSELECT
2252 * corresponding RIB entry. Then put affected RN into RIBQ head.
2254 void rib_lookup_and_pushup (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2256 struct route_table
*table
;
2257 struct route_node
*rn
;
2259 unsigned changed
= 0;
2261 if (NULL
== (table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
)))
2263 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2267 /* No matches would be the simplest case. */
2268 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
2272 route_unlock_node (rn
);
2274 /* Check all RIB entries. In case any changes have to be done, requeue
2275 * the RN into RIBQ head. If the routing message about the new connected
2276 * route (generated by the IP address we are going to assign very soon)
2277 * comes before the RIBQ is processed, the new RIB entry will join
2278 * RIBQ record already on head. This is necessary for proper revalidation
2279 * of the rest of the RIB.
2281 RNODE_FOREACH_RIB (rn
, rib
)
2283 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
) &&
2284 ! RIB_SYSTEM_ROUTE (rib
))
2287 if (IS_ZEBRA_DEBUG_RIB
)
2289 char buf
[PREFIX_STRLEN
];
2290 zlog_debug ("%u:%s: freeing way for connected prefix",
2291 rib
->vrf_id
, prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2292 rib_dump (&rn
->p
, NULL
, rib
);
2294 rib_uninstall (rn
, rib
);
2302 rib_add_multipath (afi_t afi
, safi_t safi
, struct prefix
*p
,
2303 struct prefix_ipv6
*src_p
, struct rib
*rib
)
2305 struct route_table
*table
;
2306 struct route_node
*rn
;
2308 struct nexthop
*nexthop
;
2315 if (p
->family
== AF_INET
)
2320 assert(!src_p
|| family
== AFI_IP6
);
2323 table
= zebra_vrf_table_with_table_id (family
, safi
, rib
->vrf_id
, rib
->table
);
2327 /* Make it sure prefixlen is applied to the prefix. */
2330 apply_mask_ipv6 (src_p
);
2332 /* Set default distance by route type. */
2333 if (rib
->distance
== 0)
2335 rib
->distance
= route_info
[rib
->type
].distance
;
2337 /* iBGP distance is 200. */
2338 if (rib
->type
== ZEBRA_ROUTE_BGP
2339 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
2340 rib
->distance
= 200;
2343 /* Lookup route node.*/
2344 rn
= srcdest_rnode_get (table
, p
, src_p
);
2346 /* If same type of route are installed, treat it as a implicit
2348 RNODE_FOREACH_RIB (rn
, same
)
2350 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
2353 if (same
->type
== rib
->type
&& same
->instance
== rib
->instance
2354 && same
->table
== rib
->table
2355 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
2359 /* If this route is kernel route, set FIB flag to the route. */
2360 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
2361 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2362 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2364 /* Link new rib to node.*/
2365 if (IS_ZEBRA_DEBUG_RIB
)
2367 rnode_debug(rn
, rib
->vrf_id
, "Inserting route rn %p, rib %p (type %d) existing %p",
2368 (void *)rn
, (void *)rib
, rib
->type
, (void *)same
);
2370 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2371 rib_dump (p
, src_p
, rib
);
2373 rib_addnode (rn
, rib
, 1);
2376 /* Free implicit route.*/
2379 rib_delnode (rn
, same
);
2383 route_unlock_node (rn
);
2388 rib_delete (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
, u_short instance
,
2389 int flags
, struct prefix
*p
, struct prefix_ipv6
*src_p
,
2390 union g_addr
*gate
, ifindex_t ifindex
, u_int32_t table_id
)
2392 struct route_table
*table
;
2393 struct route_node
*rn
;
2395 struct rib
*fib
= NULL
;
2396 struct rib
*same
= NULL
;
2397 struct nexthop
*nexthop
, *tnexthop
;
2399 char buf2
[INET6_ADDRSTRLEN
];
2401 assert(!src_p
|| afi
== AFI_IP6
);
2404 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2411 apply_mask_ipv6 (src_p
);
2413 /* Lookup route node. */
2414 rn
= srcdest_rnode_lookup (table
, p
, src_p
);
2417 char dst_buf
[PREFIX_STRLEN
], src_buf
[PREFIX_STRLEN
];
2419 prefix2str(p
, dst_buf
, sizeof(dst_buf
));
2420 if (src_p
&& src_p
->prefixlen
)
2421 prefix2str(src_p
, src_buf
, sizeof(src_buf
));
2425 if (IS_ZEBRA_DEBUG_RIB
)
2426 zlog_debug ("%u:%s%s%s doesn't exist in rib",
2428 (src_buf
[0] != '\0') ? " from " : "",
2433 /* Lookup same type route. */
2434 RNODE_FOREACH_RIB (rn
, rib
)
2436 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2439 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
2442 if (rib
->type
!= type
)
2444 if (rib
->instance
!= instance
)
2446 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2447 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2449 if (nexthop
->ifindex
!= ifindex
)
2454 route_unlock_node (rn
);
2455 route_unlock_node (rn
);
2461 /* Make sure that the route found has the same gateway. */
2469 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2470 if (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
2471 IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
))
2480 /* If same type of route can't be found and this message is from
2484 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
&&
2485 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
))
2487 if (IS_ZEBRA_DEBUG_RIB
)
2489 rnode_debug (rn
, vrf_id
, "rn %p, rib %p (type %d) was deleted from kernel, adding",
2490 rn
, fib
, fib
->type
);
2495 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2496 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2498 UNSET_FLAG (fib
->status
, RIB_ENTRY_SELECTED_FIB
);
2502 /* This means someone else, other than Zebra, has deleted
2503 * a Zebra router from the kernel. We will add it back */
2504 rib_install_kernel(rn
, fib
, NULL
);
2509 if (IS_ZEBRA_DEBUG_RIB
)
2512 rnode_debug(rn
, vrf_id
, "via %s ifindex %d type %d "
2513 "doesn't exist in rib",
2514 inet_ntop (family2afi(afi
), gate
, buf2
, INET_ADDRSTRLEN
), /* FIXME */
2518 rnode_debug (rn
, vrf_id
, "ifindex %d type %d doesn't exist in rib",
2522 route_unlock_node (rn
);
2528 rib_delnode (rn
, same
);
2530 route_unlock_node (rn
);
2537 rib_add (afi_t afi
, safi_t safi
, vrf_id_t vrf_id
, int type
,
2538 u_short instance
, int flags
, struct prefix
*p
,
2539 struct prefix_ipv6
*src_p
, union g_addr
*gate
,
2540 union g_addr
*src
, ifindex_t ifindex
,
2541 u_int32_t table_id
, u_int32_t metric
, u_int32_t mtu
,
2545 struct rib
*same
= NULL
;
2546 struct route_table
*table
;
2547 struct route_node
*rn
;
2548 struct nexthop
*nexthop
;
2550 assert(!src_p
|| afi
== AFI_IP6
);
2553 table
= zebra_vrf_table_with_table_id (afi
, safi
, vrf_id
, table_id
);
2557 /* Make sure mask is applied. */
2560 apply_mask_ipv6 (src_p
);
2562 /* Set default distance by route type. */
2565 if ((unsigned)type
>= array_size(route_info
))
2568 distance
= route_info
[type
].distance
;
2570 /* iBGP distance is 200. */
2571 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2575 /* Lookup route node.*/
2576 rn
= srcdest_rnode_get (table
, p
, src_p
);
2578 /* If same type of route are installed, treat it as a implicit
2580 RNODE_FOREACH_RIB (rn
, rib
)
2582 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2585 if (rib
->type
!= type
)
2587 if (rib
->instance
!= instance
)
2589 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2594 /* Duplicate connected route comes in. */
2595 else if ((nexthop
= rib
->nexthop
) &&
2596 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2597 nexthop
->ifindex
== ifindex
&&
2598 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2605 /* Allocate new rib structure. */
2606 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2609 rib
->instance
= instance
;
2610 rib
->distance
= distance
;
2612 rib
->metric
= metric
;
2614 rib
->table
= table_id
;
2615 rib
->vrf_id
= vrf_id
;
2616 rib
->nexthop_num
= 0;
2617 rib
->uptime
= time (NULL
);
2619 /* Nexthop settings. */
2625 rib_nexthop_ipv6_ifindex_add (rib
, &gate
->ipv6
, ifindex
);
2627 rib_nexthop_ipv6_add (rib
, &gate
->ipv6
);
2632 rib_nexthop_ipv4_ifindex_add (rib
, &gate
->ipv4
, &src
->ipv4
, ifindex
);
2634 rib_nexthop_ipv4_add (rib
, &gate
->ipv4
, &src
->ipv4
);
2638 rib_nexthop_ifindex_add (rib
, ifindex
);
2640 /* If this route is kernel route, set FIB flag to the route. */
2641 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2642 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2643 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2645 /* Link new rib to node.*/
2646 if (IS_ZEBRA_DEBUG_RIB
)
2648 rnode_debug (rn
, vrf_id
, "Inserting route rn %p, rib %p (type %d) existing %p",
2649 (void *)rn
, (void *)rib
, rib
->type
, (void *)same
);
2651 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2652 rib_dump (p
, src_p
, rib
);
2654 rib_addnode (rn
, rib
, 1);
2656 /* Free implicit route.*/
2658 rib_delnode (rn
, same
);
2660 route_unlock_node (rn
);
2664 /* Schedule routes of a particular table (address-family) based on event. */
2666 rib_update_table (struct route_table
*table
, rib_update_event_t event
)
2668 struct route_node
*rn
;
2669 struct rib
*rib
, *next
;
2671 /* Walk all routes and queue for processing, if appropriate for
2672 * the trigger event.
2674 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2678 case RIB_UPDATE_IF_CHANGE
:
2679 /* Examine all routes that won't get processed by the protocol or
2680 * triggered by nexthop evaluation (NHT). This would be system,
2681 * kernel and certain static routes. Note that NHT will get
2682 * triggered upon an interface event as connected routes always
2683 * get queued for processing.
2685 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2687 if (rib
->type
== ZEBRA_ROUTE_OSPF
||
2688 rib
->type
== ZEBRA_ROUTE_OSPF6
||
2689 rib
->type
== ZEBRA_ROUTE_BGP
)
2690 continue; /* protocol will handle. */
2691 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
2694 for (nh
= rib
->nexthop
; nh
; nh
= nh
->next
)
2695 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
||
2696 nh
->type
== NEXTHOP_TYPE_IPV6
))
2699 /* If we only have nexthops to a gateway, NHT will
2710 case RIB_UPDATE_RMAP_CHANGE
:
2711 case RIB_UPDATE_OTHER
:
2712 /* Right now, examine all routes. Can restrict to a protocol in
2713 * some cases (TODO).
2715 if (rnode_to_ribs (rn
))
2725 /* RIB update function. */
2727 rib_update (vrf_id_t vrf_id
, rib_update_event_t event
)
2729 struct route_table
*table
;
2731 /* Process routes of interested address-families. */
2732 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2734 rib_update_table (table
, event
);
2736 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2738 rib_update_table (table
, event
);
2741 /* Remove all routes which comes from non main table. */
2743 rib_weed_table (struct route_table
*table
)
2745 struct route_node
*rn
;
2750 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2751 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2753 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2756 if (rib
->table
!= zebrad
.rtm_table_default
&&
2757 rib
->table
!= RT_TABLE_MAIN
)
2758 rib_delnode (rn
, rib
);
2762 /* Delete all routes from non main table. */
2764 rib_weed_tables (void)
2767 struct zebra_vrf
*zvrf
;
2769 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2770 if ((zvrf
= vrf
->info
) != NULL
)
2772 rib_weed_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2773 rib_weed_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2777 /* Delete self installed routes after zebra is relaunched. */
2779 rib_sweep_table (struct route_table
*table
)
2781 struct route_node
*rn
;
2787 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2788 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2790 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2793 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
2794 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
2796 ret
= rib_uninstall_kernel (rn
, rib
);
2798 rib_delnode (rn
, rib
);
2803 /* Sweep all RIB tables. */
2805 rib_sweep_route (void)
2808 struct zebra_vrf
*zvrf
;
2810 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2811 if ((zvrf
= vrf
->info
) != NULL
)
2813 rib_sweep_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
2814 rib_sweep_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2818 /* Remove specific by protocol routes from 'table'. */
2819 static unsigned long
2820 rib_score_proto_table (u_char proto
, u_short instance
, struct route_table
*table
)
2822 struct route_node
*rn
;
2825 unsigned long n
= 0;
2828 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2829 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2831 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2833 if (rib
->type
== proto
&& rib
->instance
== instance
)
2835 rib_delnode (rn
, rib
);
2842 /* Remove specific by protocol routes. */
2844 rib_score_proto (u_char proto
, u_short instance
)
2847 struct zebra_vrf
*zvrf
;
2848 unsigned long cnt
= 0;
2850 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
)
2851 if ((zvrf
= vrf
->info
) != NULL
)
2852 cnt
+= rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
2853 +rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
2858 /* Close RIB and clean up kernel routes. */
2860 rib_close_table (struct route_table
*table
)
2862 struct route_node
*rn
;
2863 rib_table_info_t
*info
= table
->info
;
2867 for (rn
= route_top (table
); rn
; rn
= srcdest_route_next (rn
))
2868 RNODE_FOREACH_RIB (rn
, rib
)
2870 if (!CHECK_FLAG (rib
->status
, RIB_ENTRY_SELECTED_FIB
))
2873 if (info
->safi
== SAFI_UNICAST
)
2874 zfpm_trigger_update (rn
, NULL
);
2876 if (! RIB_SYSTEM_ROUTE (rib
))
2877 rib_uninstall_kernel (rn
, rib
);
2881 /* Routing information base initialize. */
2885 rib_queue_init (&zebrad
);
2891 * Get the first vrf id that is greater than the given vrf id if any.
2893 * Returns TRUE if a vrf id was found, FALSE otherwise.
2896 vrf_id_get_next (vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
2900 vrf
= vrf_lookup_by_id (vrf_id
);
2903 vrf
= RB_NEXT (vrf_id_head
, &vrfs_by_id
, vrf
);
2905 *next_id_p
= vrf
->vrf_id
;
2914 * rib_tables_iter_next
2916 * Returns the next table in the iteration.
2918 struct route_table
*
2919 rib_tables_iter_next (rib_tables_iter_t
*iter
)
2921 struct route_table
*table
;
2924 * Array that helps us go over all AFI/SAFI combinations via one
2931 { AFI_IP
, SAFI_UNICAST
},
2932 { AFI_IP
, SAFI_MULTICAST
},
2933 { AFI_IP6
, SAFI_UNICAST
},
2934 { AFI_IP6
, SAFI_MULTICAST
},
2939 switch (iter
->state
)
2942 case RIB_TABLES_ITER_S_INIT
:
2943 iter
->vrf_id
= VRF_DEFAULT
;
2944 iter
->afi_safi_ix
= -1;
2948 case RIB_TABLES_ITER_S_ITERATING
:
2949 iter
->afi_safi_ix
++;
2953 while (iter
->afi_safi_ix
< (int) ZEBRA_NUM_OF (afi_safis
))
2955 table
= zebra_vrf_table (afi_safis
[iter
->afi_safi_ix
].afi
,
2956 afi_safis
[iter
->afi_safi_ix
].safi
,
2961 iter
->afi_safi_ix
++;
2965 * Found another table in this vrf.
2971 * Done with all tables in the current vrf, go to the next
2974 if (!vrf_id_get_next (iter
->vrf_id
, &iter
->vrf_id
))
2977 iter
->afi_safi_ix
= 0;
2982 case RIB_TABLES_ITER_S_DONE
:
2987 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
2989 iter
->state
= RIB_TABLES_ITER_S_DONE
;