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
31 #include "sockunion.h"
34 #include "workqueue.h"
40 #include "zebra/rib.h"
42 #include "zebra/zserv.h"
43 #include "zebra/redistribute.h"
44 #include "zebra/debug.h"
45 #include "zebra/zebra_fpm.h"
46 #include "zebra/zebra_rnh.h"
48 /* Default rtm_table for all clients */
49 extern struct zebra_t zebrad;
51 /* Hold time for RIB process, should be very minimal.
52 * it is useful to able to set it otherwise for testing, hence exported
53 * as global here for test-rig code.
55 int rib_process_hold_time = 10;
57 /* Each route type's string and default distance value. */
62 } route_info[ZEBRA_ROUTE_MAX] =
64 [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0},
65 [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0},
66 [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0},
67 [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1},
68 [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120},
69 [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120},
70 [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110},
71 [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110},
72 [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115},
73 [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */},
74 [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, 95},
75 [ZEBRA_ROUTE_NHRP] = {ZEBRA_ROUTE_NHRP, 10},
76 /* no entry/default: 150 */
79 /* RPF lookup behaviour */
80 static enum multicast_mode ipv4_multicast_mode = MCAST_NO_CONFIG;
82 static void __attribute__((format (printf, 4, 5)))
83 _rnode_zlog(const char *_func, struct route_node *rn, int priority,
84 const char *msgfmt, ...)
86 char prefix[PREFIX_STRLEN], buf[256];
91 vsnprintf(msgbuf, sizeof(msgbuf), msgfmt, ap);
96 rib_table_info_t *info = rn->table->info;
98 snprintf(buf, sizeof(buf), "%s%s vrf %u",
99 prefix2str(&rn->p, prefix, sizeof(prefix)),
100 info->safi == SAFI_MULTICAST ? " (MRIB)" : "",
105 snprintf(buf, sizeof(buf), "{(route_node *) NULL}");
108 zlog (NULL, priority, "%s: %s: %s", _func, buf, msgbuf);
111 #define rnode_debug(node, ...) \
112 _rnode_zlog(__func__, node, LOG_DEBUG, __VA_ARGS__)
113 #define rnode_info(node, ...) \
114 _rnode_zlog(__func__, node, LOG_INFO, __VA_ARGS__)
116 /* Add nexthop to the end of a rib node's nexthop list */
118 rib_nexthop_add (struct rib *rib, struct nexthop *nexthop)
120 nexthop_add(&rib->nexthop, nexthop);
124 /* Delete specified nexthop from the list. */
126 rib_nexthop_delete (struct rib *rib, struct nexthop *nexthop)
129 nexthop->next->prev = nexthop->prev;
131 nexthop->prev->next = nexthop->next;
133 rib->nexthop = nexthop->next;
138 rib_nexthop_ifindex_add (struct rib *rib, ifindex_t ifindex)
140 struct nexthop *nexthop;
142 nexthop = nexthop_new ();
143 nexthop->type = NEXTHOP_TYPE_IFINDEX;
144 nexthop->ifindex = ifindex;
146 rib_nexthop_add (rib, nexthop);
152 rib_nexthop_ifname_add (struct rib *rib, char *ifname)
154 struct nexthop *nexthop;
156 nexthop = nexthop_new ();
157 nexthop->type = NEXTHOP_TYPE_IFNAME;
158 nexthop->ifname = XSTRDUP (MTYPE_TMP, ifname);
160 rib_nexthop_add (rib, nexthop);
166 rib_nexthop_ipv4_add (struct rib *rib, struct in_addr *ipv4, struct in_addr *src)
168 struct nexthop *nexthop;
170 nexthop = nexthop_new ();
171 nexthop->type = NEXTHOP_TYPE_IPV4;
172 nexthop->gate.ipv4 = *ipv4;
174 nexthop->src.ipv4 = *src;
176 rib_nexthop_add (rib, nexthop);
182 rib_nexthop_ipv4_ifindex_add (struct rib *rib, struct in_addr *ipv4,
183 struct in_addr *src, ifindex_t ifindex)
185 struct nexthop *nexthop;
187 nexthop = nexthop_new ();
188 nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
189 nexthop->gate.ipv4 = *ipv4;
191 nexthop->src.ipv4 = *src;
192 nexthop->ifindex = ifindex;
194 rib_nexthop_add (rib, nexthop);
200 rib_nexthop_ipv6_add (struct rib *rib, struct in6_addr *ipv6)
202 struct nexthop *nexthop;
204 nexthop = nexthop_new ();
205 nexthop->type = NEXTHOP_TYPE_IPV6;
206 nexthop->gate.ipv6 = *ipv6;
208 rib_nexthop_add (rib, nexthop);
213 static struct nexthop *
214 rib_nexthop_ipv6_ifname_add (struct rib *rib, struct in6_addr *ipv6,
217 struct nexthop *nexthop;
219 nexthop = nexthop_new ();
220 nexthop->type = NEXTHOP_TYPE_IPV6_IFNAME;
221 nexthop->gate.ipv6 = *ipv6;
222 nexthop->ifname = XSTRDUP (MTYPE_TMP, ifname);
224 rib_nexthop_add (rib, nexthop);
230 rib_nexthop_ipv6_ifindex_add (struct rib *rib, struct in6_addr *ipv6,
233 struct nexthop *nexthop;
235 nexthop = nexthop_new ();
236 nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
237 nexthop->gate.ipv6 = *ipv6;
238 nexthop->ifindex = ifindex;
240 rib_nexthop_add (rib, nexthop);
246 rib_nexthop_blackhole_add (struct rib *rib)
248 struct nexthop *nexthop;
250 nexthop = nexthop_new ();
251 nexthop->type = NEXTHOP_TYPE_BLACKHOLE;
252 SET_FLAG (rib->flags, ZEBRA_FLAG_BLACKHOLE);
254 rib_nexthop_add (rib, nexthop);
259 /* This method checks whether a recursive nexthop has at
260 * least one resolved nexthop in the fib.
263 nexthop_has_fib_child(struct nexthop *nexthop)
267 if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE))
270 for (nh = nexthop->resolved; nh; nh = nh->next)
271 if (CHECK_FLAG (nh->flags, NEXTHOP_FLAG_FIB))
277 /* If force flag is not set, do not modify falgs at all for uninstall
278 the route from FIB. */
280 nexthop_active_ipv4 (struct rib *rib, struct nexthop *nexthop, int set,
281 struct route_node *top)
283 struct prefix_ipv4 p;
284 struct route_table *table;
285 struct route_node *rn;
288 struct nexthop *newhop;
289 struct nexthop *resolved_hop;
291 if (nexthop->type == NEXTHOP_TYPE_IPV4)
292 nexthop->ifindex = 0;
296 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
297 nexthops_free(nexthop->resolved);
298 nexthop->resolved = NULL;
299 rib->nexthop_mtu = 0;
302 /* Make lookup prefix. */
303 memset (&p, 0, sizeof (struct prefix_ipv4));
305 p.prefixlen = IPV4_MAX_PREFIXLEN;
306 p.prefix = nexthop->gate.ipv4;
309 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, rib->vrf_id);
313 rn = route_node_match (table, (struct prefix *) &p);
316 route_unlock_node (rn);
318 /* If lookup self prefix return immediately. */
322 /* Pick up selected route. */
323 RNODE_FOREACH_RIB (rn, match)
325 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
327 if (CHECK_FLAG (match->status, RIB_ENTRY_SELECTED_FIB))
331 /* If there is no selected route or matched route is EGP, go up
334 || match->type == ZEBRA_ROUTE_BGP)
338 } while (rn && rn->info == NULL);
340 route_lock_node (rn);
344 /* If the longest prefix match for the nexthop yields
345 * a blackhole, mark it as inactive. */
346 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE)
347 || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT))
350 if (match->type == ZEBRA_ROUTE_CONNECT)
352 /* Directly point connected route. */
353 newhop = match->nexthop;
354 if (newhop && nexthop->type == NEXTHOP_TYPE_IPV4)
355 nexthop->ifindex = newhop->ifindex;
359 else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL))
362 for (newhop = match->nexthop; newhop; newhop = newhop->next)
363 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)
364 && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE))
368 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
370 resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
371 SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
372 /* If the resolving route specifies a gateway, use it */
373 if (newhop->type == NEXTHOP_TYPE_IPV4
374 || newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX
375 || newhop->type == NEXTHOP_TYPE_IPV4_IFNAME)
377 resolved_hop->type = newhop->type;
378 resolved_hop->gate.ipv4 = newhop->gate.ipv4;
379 resolved_hop->ifindex = newhop->ifindex;
382 /* If the resolving route is an interface route, it
383 * means the gateway we are looking up is connected
384 * to that interface. Therefore, the resolved route
385 * should have the original gateway as nexthop as it
386 * is directly connected. */
387 if (newhop->type == NEXTHOP_TYPE_IFINDEX
388 || newhop->type == NEXTHOP_TYPE_IFNAME)
390 resolved_hop->type = NEXTHOP_TYPE_IPV4_IFINDEX;
391 resolved_hop->gate.ipv4 = nexthop->gate.ipv4;
392 resolved_hop->ifindex = newhop->ifindex;
395 nexthop_add(&nexthop->resolved, resolved_hop);
400 rib->nexthop_mtu = match->mtu;
412 /* If force flag is not set, do not modify falgs at all for uninstall
413 the route from FIB. */
415 nexthop_active_ipv6 (struct rib *rib, struct nexthop *nexthop, int set,
416 struct route_node *top)
418 struct prefix_ipv6 p;
419 struct route_table *table;
420 struct route_node *rn;
423 struct nexthop *newhop;
424 struct nexthop *resolved_hop;
426 if (nexthop->type == NEXTHOP_TYPE_IPV6)
427 nexthop->ifindex = 0;
431 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
432 nexthops_free(nexthop->resolved);
433 nexthop->resolved = NULL;
436 /* Make lookup prefix. */
437 memset (&p, 0, sizeof (struct prefix_ipv6));
439 p.prefixlen = IPV6_MAX_PREFIXLEN;
440 p.prefix = nexthop->gate.ipv6;
443 table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, rib->vrf_id);
447 rn = route_node_match (table, (struct prefix *) &p);
450 route_unlock_node (rn);
452 /* If lookup self prefix return immediately. */
456 /* Pick up selected route. */
457 RNODE_FOREACH_RIB (rn, match)
459 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
461 if (CHECK_FLAG (match->status, RIB_ENTRY_SELECTED_FIB))
465 /* If there is no selected route or matched route is EGP, go up
468 || match->type == ZEBRA_ROUTE_BGP)
472 } while (rn && rn->info == NULL);
474 route_lock_node (rn);
478 /* If the longest prefix match for the nexthop yields
479 * a blackhole, mark it as inactive. */
480 if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE)
481 || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT))
484 if (match->type == ZEBRA_ROUTE_CONNECT)
486 /* Directly point connected route. */
487 newhop = match->nexthop;
489 if (newhop && nexthop->type == NEXTHOP_TYPE_IPV6)
490 nexthop->ifindex = newhop->ifindex;
494 else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL))
497 for (newhop = match->nexthop; newhop; newhop = newhop->next)
498 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)
499 && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE))
503 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE);
505 resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop));
506 SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE);
507 /* See nexthop_active_ipv4 for a description how the
508 * resolved nexthop is constructed. */
509 if (newhop->type == NEXTHOP_TYPE_IPV6
510 || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX
511 || newhop->type == NEXTHOP_TYPE_IPV6_IFNAME)
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 (newhop->type == NEXTHOP_TYPE_IFINDEX
524 || newhop->type == NEXTHOP_TYPE_IFNAME)
526 resolved_hop->flags |= NEXTHOP_FLAG_ONLINK;
527 resolved_hop->type = NEXTHOP_TYPE_IPV6_IFINDEX;
528 resolved_hop->gate.ipv6 = nexthop->gate.ipv6;
529 resolved_hop->ifindex = newhop->ifindex;
532 nexthop_add(&nexthop->resolved, resolved_hop);
548 rib_match_ipv4_safi (struct in_addr addr, safi_t safi, int skip_bgp,
549 struct route_node **rn_out, vrf_id_t vrf_id)
551 struct route_table *table;
552 struct route_node *rn;
554 struct nexthop *newhop, *tnewhop;
558 table = zebra_vrf_table (AFI_IP, safi, vrf_id);
562 rn = route_node_match_ipv4 (table, &addr);
566 route_unlock_node (rn);
568 /* Pick up selected route. */
569 RNODE_FOREACH_RIB (rn, match)
571 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
573 if (CHECK_FLAG (match->status, RIB_ENTRY_SELECTED_FIB))
577 /* If there is no selected route or matched route is EGP, go up
579 if (!match || (skip_bgp && (match->type == ZEBRA_ROUTE_BGP)))
583 } while (rn && rn->info == NULL);
585 route_lock_node (rn);
589 if (match->type != ZEBRA_ROUTE_CONNECT)
592 for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
593 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
611 rib_match_ipv4_multicast (struct in_addr addr, struct route_node **rn_out,
614 struct rib *rib = NULL, *mrib = NULL, *urib = NULL;
615 struct route_node *m_rn = NULL, *u_rn = NULL;
616 int skip_bgp = 0; /* bool */
618 switch (ipv4_multicast_mode)
620 case MCAST_MRIB_ONLY:
621 return rib_match_ipv4_safi (addr, SAFI_MULTICAST, skip_bgp, rn_out,
623 case MCAST_URIB_ONLY:
624 return rib_match_ipv4_safi (addr, SAFI_UNICAST, skip_bgp, rn_out,
626 case MCAST_NO_CONFIG:
627 case MCAST_MIX_MRIB_FIRST:
628 rib = mrib = rib_match_ipv4_safi (addr, SAFI_MULTICAST, skip_bgp, &m_rn,
631 rib = urib = rib_match_ipv4_safi (addr, SAFI_UNICAST, skip_bgp, &u_rn,
634 case MCAST_MIX_DISTANCE:
635 mrib = rib_match_ipv4_safi (addr, SAFI_MULTICAST, skip_bgp, &m_rn,
637 urib = rib_match_ipv4_safi (addr, SAFI_UNICAST, skip_bgp, &u_rn,
640 rib = urib->distance < mrib->distance ? urib : mrib;
646 case MCAST_MIX_PFXLEN:
647 mrib = rib_match_ipv4_safi (addr, SAFI_MULTICAST, skip_bgp, &m_rn,
649 urib = rib_match_ipv4_safi (addr, SAFI_UNICAST, skip_bgp, &u_rn,
652 rib = u_rn->p.prefixlen > m_rn->p.prefixlen ? urib : mrib;
661 *rn_out = (rib == mrib) ? m_rn : u_rn;
663 if (IS_ZEBRA_DEBUG_RIB)
666 inet_ntop (AF_INET, &addr, buf, BUFSIZ);
668 zlog_debug("%s: %s vrf %u: found %s, using %s",
669 __func__, buf, vrf_id,
670 mrib ? (urib ? "MRIB+URIB" : "MRIB") :
671 urib ? "URIB" : "nothing",
672 rib == urib ? "URIB" : rib == mrib ? "MRIB" : "none");
678 multicast_mode_ipv4_set (enum multicast_mode mode)
680 if (IS_ZEBRA_DEBUG_RIB)
681 zlog_debug("%s: multicast lookup mode set (%d)", __func__, mode);
682 ipv4_multicast_mode = mode;
686 multicast_mode_ipv4_get (void)
688 return ipv4_multicast_mode;
692 rib_lookup_ipv4 (struct prefix_ipv4 *p, vrf_id_t vrf_id)
694 struct route_table *table;
695 struct route_node *rn;
697 struct nexthop *nexthop, *tnexthop;
701 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
705 rn = route_node_lookup (table, (struct prefix *) p);
707 /* No route for this prefix. */
712 route_unlock_node (rn);
714 RNODE_FOREACH_RIB (rn, match)
716 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
718 if (CHECK_FLAG (match->status, RIB_ENTRY_SELECTED_FIB))
722 if (! match || match->type == ZEBRA_ROUTE_BGP)
725 if (match->type == ZEBRA_ROUTE_CONNECT)
728 for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing))
729 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
736 * This clone function, unlike its original rib_lookup_ipv4(), checks
737 * if specified IPv4 route record (prefix/mask -> gate) exists in
738 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
742 * 0: exact match found
743 * 1: a match was found with a different gate
744 * 2: connected route found
745 * 3: no matches found
748 rib_lookup_ipv4_route (struct prefix_ipv4 *p, union sockunion * qgate,
751 struct route_table *table;
752 struct route_node *rn;
754 struct nexthop *nexthop, *tnexthop;
759 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
761 return ZEBRA_RIB_LOOKUP_ERROR;
763 /* Scan the RIB table for exactly matching RIB entry. */
764 rn = route_node_lookup (table, (struct prefix *) p);
766 /* No route for this prefix. */
768 return ZEBRA_RIB_NOTFOUND;
771 route_unlock_node (rn);
773 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
774 RNODE_FOREACH_RIB (rn, match)
776 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
778 if (CHECK_FLAG (match->status, RIB_ENTRY_SELECTED_FIB))
782 /* None such found :( */
784 return ZEBRA_RIB_NOTFOUND;
786 if (match->type == ZEBRA_ROUTE_CONNECT)
787 return ZEBRA_RIB_FOUND_CONNECTED;
789 /* Ok, we have a cood candidate, let's check it's nexthop list... */
791 for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing))
792 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
795 if (nexthop->gate.ipv4.s_addr == sockunion2ip (qgate))
796 return ZEBRA_RIB_FOUND_EXACT;
797 if (IS_ZEBRA_DEBUG_RIB)
799 char gate_buf[INET_ADDRSTRLEN], qgate_buf[INET_ADDRSTRLEN];
800 inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, gate_buf, INET_ADDRSTRLEN);
801 inet_ntop (AF_INET, &sockunion2ip(qgate), qgate_buf, INET_ADDRSTRLEN);
802 zlog_debug ("%s: qgate == %s, %s == %s", __func__,
803 qgate_buf, recursing ? "rgate" : "gate", gate_buf);
808 return ZEBRA_RIB_FOUND_NOGATE;
810 return ZEBRA_RIB_NOTFOUND;
814 rib_match_ipv6 (struct in6_addr *addr, vrf_id_t vrf_id)
816 struct prefix_ipv6 p;
817 struct route_table *table;
818 struct route_node *rn;
820 struct nexthop *newhop, *tnewhop;
824 table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id);
828 memset (&p, 0, sizeof (struct prefix_ipv6));
830 p.prefixlen = IPV6_MAX_PREFIXLEN;
831 IPV6_ADDR_COPY (&p.prefix, addr);
833 rn = route_node_match (table, (struct prefix *) &p);
837 route_unlock_node (rn);
839 /* Pick up selected route. */
840 RNODE_FOREACH_RIB (rn, match)
842 if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED))
844 if (CHECK_FLAG (match->status, RIB_ENTRY_SELECTED_FIB))
848 /* If there is no selected route or matched route is EGP, go up
851 || match->type == ZEBRA_ROUTE_BGP)
855 } while (rn && rn->info == NULL);
857 route_lock_node (rn);
861 if (match->type == ZEBRA_ROUTE_CONNECT)
862 /* Directly point connected route. */
866 for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing))
867 if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB))
876 #define RIB_SYSTEM_ROUTE(R) \
877 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
879 /* This function verifies reachability of one given nexthop, which can be
880 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
881 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
882 * nexthop->ifindex will be updated appropriately as well.
883 * An existing route map can turn (otherwise active) nexthop into inactive, but
886 * The return value is the final value of 'ACTIVE' flag.
890 nexthop_active_check (struct route_node *rn, struct rib *rib,
891 struct nexthop *nexthop, int set)
893 rib_table_info_t *info = rn->table->info;
894 struct interface *ifp;
895 route_map_result_t ret = RMAP_MATCH;
896 extern char *proto_rm[AFI_MAX][ZEBRA_ROUTE_MAX+1];
897 struct route_map *rmap;
901 switch (nexthop->type)
903 case NEXTHOP_TYPE_IFINDEX:
904 ifp = if_lookup_by_index_vrf (nexthop->ifindex, rib->vrf_id);
905 if (ifp && if_is_operative(ifp))
906 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
908 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
910 case NEXTHOP_TYPE_IPV6_IFNAME:
912 case NEXTHOP_TYPE_IFNAME:
913 ifp = if_lookup_by_name_vrf (nexthop->ifname, rib->vrf_id);
914 if (ifp && if_is_operative(ifp))
917 nexthop->ifindex = ifp->ifindex;
918 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
923 nexthop->ifindex = 0;
924 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
927 case NEXTHOP_TYPE_IPV4:
928 case NEXTHOP_TYPE_IPV4_IFINDEX:
930 if (nexthop_active_ipv4 (rib, nexthop, set, rn))
931 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
933 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
935 case NEXTHOP_TYPE_IPV6:
937 if (nexthop_active_ipv6 (rib, nexthop, set, rn))
938 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
940 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
942 case NEXTHOP_TYPE_IPV6_IFINDEX:
944 if (IN6_IS_ADDR_LINKLOCAL (&nexthop->gate.ipv6))
946 ifp = if_lookup_by_index_vrf (nexthop->ifindex, rib->vrf_id);
947 if (ifp && if_is_operative(ifp))
948 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
950 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
954 if (nexthop_active_ipv6 (rib, nexthop, set, rn))
955 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
957 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
960 case NEXTHOP_TYPE_BLACKHOLE:
961 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
966 if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
969 /* XXX: What exactly do those checks do? Do we support
970 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
971 if (RIB_SYSTEM_ROUTE(rib) ||
972 (family == AFI_IP && rn->p.family != AF_INET) ||
973 (family == AFI_IP6 && rn->p.family != AF_INET6))
974 return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
976 /* The original code didn't determine the family correctly
977 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
978 * from the rib_table_info in those cases.
979 * Possibly it may be better to use only the rib_table_info
986 if (rib->type >= 0 && rib->type < ZEBRA_ROUTE_MAX &&
987 proto_rm[family][rib->type])
988 rmap = route_map_lookup_by_name (proto_rm[family][rib->type]);
989 if (!rmap && proto_rm[family][ZEBRA_ROUTE_MAX])
990 rmap = route_map_lookup_by_name (proto_rm[family][ZEBRA_ROUTE_MAX]);
992 struct nexthop_vrfid nh_vrf = {nexthop, rib->vrf_id};
993 ret = route_map_apply(rmap, &rn->p, RMAP_ZEBRA, &nh_vrf);
996 if (ret == RMAP_DENYMATCH)
997 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
998 return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1001 /* Iterate over all nexthops of the given RIB entry and refresh their
1002 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1003 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1004 * is flagged with RIB_ENTRY_CHANGED. The 4th 'set' argument is
1005 * transparently passed to nexthop_active_check().
1007 * Return value is the new number of active nexthops.
1011 nexthop_active_update (struct route_node *rn, struct rib *rib, int set)
1013 struct nexthop *nexthop;
1014 unsigned int prev_active, new_active;
1015 ifindex_t prev_index;
1017 rib->nexthop_active_num = 0;
1019 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
1021 prev_active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE);
1022 prev_index = nexthop->ifindex;
1023 if ((new_active = nexthop_active_check (rn, rib, nexthop, set)))
1024 rib->nexthop_active_num++;
1025 if (prev_active != new_active ||
1026 prev_index != nexthop->ifindex)
1027 SET_FLAG (rib->status, RIB_ENTRY_CHANGED);
1029 return rib->nexthop_active_num;
1035 rib_update_kernel (struct route_node *rn, struct rib *old, struct rib *new)
1038 struct nexthop *nexthop, *tnexthop;
1039 rib_table_info_t *info = rn->table->info;
1042 if (info->safi != SAFI_UNICAST)
1045 for (ALL_NEXTHOPS_RO(new->nexthop, nexthop, tnexthop, recursing))
1046 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1048 for (ALL_NEXTHOPS_RO(old->nexthop, nexthop, tnexthop, recursing))
1049 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1054 * Make sure we update the FPM any time we send new information to
1057 zfpm_trigger_update (rn, "updating in kernel");
1059 ret = kernel_route_rib (&rn->p, old, new);
1061 /* This condition is never met, if we are using rt_socket.c */
1064 for (ALL_NEXTHOPS_RO(new->nexthop, nexthop, tnexthop, recursing))
1065 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1067 else if (old && old != new)
1069 for (ALL_NEXTHOPS_RO(old->nexthop, nexthop, tnexthop, recursing))
1070 UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1076 /* Uninstall the route from kernel. */
1078 rib_uninstall (struct route_node *rn, struct rib *rib)
1080 rib_table_info_t *info = rn->table->info;
1082 if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB))
1084 if (info->safi == SAFI_UNICAST)
1085 zfpm_trigger_update (rn, "rib_uninstall");
1087 redistribute_delete (&rn->p, rib);
1088 if (! RIB_SYSTEM_ROUTE (rib))
1089 rib_update_kernel (rn, rib, NULL);
1090 UNSET_FLAG (rib->flags, ZEBRA_FLAG_SELECTED);
1094 static void rib_unlink (struct route_node *, struct rib *);
1097 * rib_can_delete_dest
1099 * Returns TRUE if the given dest can be deleted from the table.
1102 rib_can_delete_dest (rib_dest_t *dest)
1110 * Don't delete the dest if we have to update the FPM about this
1113 if (CHECK_FLAG (dest->flags, RIB_DEST_UPDATE_FPM) ||
1114 CHECK_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM))
1123 * Garbage collect the rib dest corresponding to the given route node
1126 * Returns TRUE if the dest was deleted, FALSE otherwise.
1129 rib_gc_dest (struct route_node *rn)
1133 dest = rib_dest_from_rnode (rn);
1137 if (!rib_can_delete_dest (dest))
1140 if (IS_ZEBRA_DEBUG_RIB)
1141 rnode_debug (rn, "removing dest from table");
1144 XFREE (MTYPE_RIB_DEST, dest);
1148 * Release the one reference that we keep on the route node.
1150 route_unlock_node (rn);
1154 /* Check if 'alternate' RIB entry is better than 'current'. */
1156 rib_choose_best (struct rib *current, struct rib *alternate)
1158 if (current == NULL)
1161 /* filter route selection in following order:
1162 * - connected beats other types
1163 * - lower distance beats higher
1164 * - lower metric beats higher for equal distance
1165 * - last, hence oldest, route wins tie break.
1168 /* Connected routes. Pick the last connected
1169 * route of the set of lowest metric connected routes.
1171 if (alternate->type == ZEBRA_ROUTE_CONNECT)
1173 if (current->type != ZEBRA_ROUTE_CONNECT
1174 || alternate->metric <= current->metric)
1180 if (current->type == ZEBRA_ROUTE_CONNECT)
1183 /* higher distance loses */
1184 if (alternate->distance < current->distance)
1186 if (current->distance < alternate->distance)
1189 /* metric tie-breaks equal distance */
1190 if (alternate->metric <= current->metric)
1196 /* Core function for processing routing information base. */
1198 rib_process (struct route_node *rn)
1202 struct rib *old_selected = NULL;
1203 struct rib *new_selected = NULL;
1204 struct rib *old_fib = NULL;
1205 struct rib *new_fib = NULL;
1207 struct nexthop *nexthop = NULL, *tnexthop;
1209 rib_table_info_t *info;
1213 info = rn->table->info;
1215 RNODE_FOREACH_RIB (rn, rib)
1217 UNSET_FLAG (rib->status, RIB_ENTRY_CHANGED);
1219 /* Currently installed rib. */
1220 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
1222 assert (old_selected == NULL);
1225 if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB))
1227 assert (old_fib == NULL);
1231 /* Skip deleted entries from selection */
1232 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
1235 /* Skip unreachable nexthop. */
1236 if (! nexthop_active_update (rn, rib, 0))
1239 /* Infinit distance. */
1240 if (rib->distance == DISTANCE_INFINITY)
1243 if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_FIB_OVERRIDE))
1244 new_fib = rib_choose_best(new_fib, rib);
1246 new_selected = rib_choose_best(new_selected, rib);
1247 } /* RNODE_FOREACH_RIB_SAFE */
1249 /* If no FIB override route, use the selected route also for FIB */
1250 if (new_fib == NULL)
1251 new_fib = new_selected;
1253 /* After the cycle is finished, the following pointers will be set:
1254 * old_selected --- RIB entry currently having SELECTED
1255 * new_selected --- RIB entry that is newly SELECTED
1256 * old_fib --- RIB entry currently in kernel FIB
1257 * new_fib --- RIB entry that is newly to be in kernel FIB
1259 * new_selected will get SELECTED flag, and is going to be redistributed
1260 * the zclients. new_fib (which can be new_selected) will be installed in kernel.
1263 /* Set real nexthops. */
1265 nexthop_active_update (rn, new_fib, 1);
1266 if (new_selected && new_selected != new_fib)
1267 nexthop_active_update (rn, new_selected, 1);
1269 /* Update kernel if FIB entry has changed */
1270 if (old_fib != new_fib
1271 || (new_fib && CHECK_FLAG (new_fib->status, RIB_ENTRY_CHANGED)))
1273 if (old_fib && old_fib != new_fib)
1275 if (! RIB_SYSTEM_ROUTE (old_fib) && (! new_fib || RIB_SYSTEM_ROUTE (new_fib)))
1276 rib_update_kernel (rn, old_fib, NULL);
1277 UNSET_FLAG (old_fib->status, RIB_ENTRY_SELECTED_FIB);
1282 /* Install new or replace existing FIB entry */
1283 SET_FLAG (new_fib->status, RIB_ENTRY_SELECTED_FIB);
1284 if (! RIB_SYSTEM_ROUTE (new_fib))
1285 rib_update_kernel (rn, old_fib, new_fib);
1288 if (info->safi == SAFI_UNICAST)
1289 zfpm_trigger_update (rn, "updating existing route");
1291 else if (old_fib == new_fib && new_fib && ! RIB_SYSTEM_ROUTE (new_fib))
1293 /* Housekeeping code to deal with race conditions in kernel with
1294 * linux netlink reporting interface up before IPv4 or IPv6 protocol
1295 * is ready to add routes. This makes sure routes are IN the kernel.
1297 for (ALL_NEXTHOPS_RO(new_fib->nexthop, nexthop, tnexthop, recursing))
1298 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
1304 rib_update_kernel (rn, NULL, new_fib);
1307 /* Redistribute SELECTED entry */
1308 if (old_selected != new_selected
1309 || (new_selected && CHECK_FLAG (new_selected->status, RIB_ENTRY_CHANGED)))
1314 redistribute_delete (&rn->p, old_selected);
1315 if (old_selected != new_selected)
1316 UNSET_FLAG (old_selected->flags, ZEBRA_FLAG_SELECTED);
1321 /* Install new or replace existing redistributed entry */
1322 SET_FLAG (new_selected->flags, ZEBRA_FLAG_SELECTED);
1323 redistribute_add (&rn->p, new_selected);
1327 /* Remove all RIB entries queued for removal */
1328 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
1330 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
1332 if (IS_ZEBRA_DEBUG_RIB)
1333 rnode_debug (rn, "rn %p, removing rib %p",
1334 (void *)rn, (void *)rib);
1335 rib_unlink (rn, rib);
1339 if (IS_ZEBRA_DEBUG_RIB_Q)
1340 rnode_debug (rn, "rn %p dequeued", (void *)rn);
1343 * Check if the dest can be deleted now.
1348 /* Take a list of route_node structs and return 1, if there was a record
1349 * picked from it and processed by rib_process(). Don't process more,
1350 * than one RN record; operate only in the specified sub-queue.
1353 process_subq (struct list * subq, u_char qindex)
1355 struct listnode *lnode = listhead (subq);
1356 struct route_node *rnode;
1361 rnode = listgetdata (lnode);
1362 rib_process (rnode);
1365 UNSET_FLAG (rib_dest_from_rnode (rnode)->flags, RIB_ROUTE_QUEUED (qindex));
1370 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1371 __func__, rnode, rnode->lock);
1372 zlog_backtrace(LOG_DEBUG);
1375 route_unlock_node (rnode);
1376 list_delete_node (subq, lnode);
1381 * All meta queues have been processed. Trigger next-hop evaluation.
1384 meta_queue_process_complete (struct work_queue *dummy)
1386 zebra_evaluate_rnh_table(0, AF_INET);
1388 zebra_evaluate_rnh_table(0, AF_INET6);
1389 #endif /* HAVE_IPV6 */
1392 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1393 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1394 * is pointed to the meta queue structure.
1396 static wq_item_status
1397 meta_queue_process (struct work_queue *dummy, void *data)
1399 struct meta_queue * mq = data;
1402 for (i = 0; i < MQ_SIZE; i++)
1403 if (process_subq (mq->subq[i], i))
1408 return mq->size ? WQ_REQUEUE : WQ_SUCCESS;
1412 * Map from rib types to queue type (priority) in meta queue
1414 static const u_char meta_queue_map[ZEBRA_ROUTE_MAX] = {
1415 [ZEBRA_ROUTE_SYSTEM] = 4,
1416 [ZEBRA_ROUTE_KERNEL] = 0,
1417 [ZEBRA_ROUTE_CONNECT] = 0,
1418 [ZEBRA_ROUTE_STATIC] = 1,
1419 [ZEBRA_ROUTE_RIP] = 2,
1420 [ZEBRA_ROUTE_RIPNG] = 2,
1421 [ZEBRA_ROUTE_OSPF] = 2,
1422 [ZEBRA_ROUTE_OSPF6] = 2,
1423 [ZEBRA_ROUTE_ISIS] = 2,
1424 [ZEBRA_ROUTE_BGP] = 3,
1425 [ZEBRA_ROUTE_HSLS] = 4,
1426 [ZEBRA_ROUTE_BABEL] = 2,
1427 [ZEBRA_ROUTE_NHRP] = 2,
1430 /* Look into the RN and queue it into one or more priority queues,
1431 * increasing the size for each data push done.
1434 rib_meta_queue_add (struct meta_queue *mq, struct route_node *rn)
1438 RNODE_FOREACH_RIB (rn, rib)
1440 u_char qindex = meta_queue_map[rib->type];
1442 /* Invariant: at this point we always have rn->info set. */
1443 if (CHECK_FLAG (rib_dest_from_rnode (rn)->flags,
1444 RIB_ROUTE_QUEUED (qindex)))
1446 if (IS_ZEBRA_DEBUG_RIB_Q)
1447 rnode_debug (rn, "rn %p is already queued in sub-queue %u",
1448 (void *)rn, qindex);
1452 SET_FLAG (rib_dest_from_rnode (rn)->flags, RIB_ROUTE_QUEUED (qindex));
1453 listnode_add (mq->subq[qindex], rn);
1454 route_lock_node (rn);
1457 if (IS_ZEBRA_DEBUG_RIB_Q)
1458 rnode_debug (rn, "queued rn %p into sub-queue %u",
1459 (void *)rn, qindex);
1463 /* Add route_node to work queue and schedule processing */
1465 rib_queue_add (struct zebra_t *zebra, struct route_node *rn)
1467 assert (zebra && rn);
1469 /* Pointless to queue a route_node with no RIB entries to add or remove */
1470 if (!rnode_to_ribs (rn))
1472 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1473 __func__, (void *)rn, rn->lock);
1474 zlog_backtrace(LOG_DEBUG);
1478 if (IS_ZEBRA_DEBUG_RIB_Q)
1479 rnode_info (rn, "work queue added");
1483 if (zebra->ribq == NULL)
1485 zlog_err ("%s: work_queue does not exist!", __func__);
1490 * The RIB queue should normally be either empty or holding the only
1491 * work_queue_item element. In the latter case this element would
1492 * hold a pointer to the meta queue structure, which must be used to
1493 * actually queue the route nodes to process. So create the MQ
1494 * holder, if necessary, then push the work into it in any case.
1495 * This semantics was introduced after 0.99.9 release.
1497 if (!zebra->ribq->items->count)
1498 work_queue_add (zebra->ribq, zebra->mq);
1500 rib_meta_queue_add (zebra->mq, rn);
1502 if (IS_ZEBRA_DEBUG_RIB_Q)
1503 rnode_debug (rn, "rn %p queued", (void *)rn);
1508 /* Create new meta queue.
1509 A destructor function doesn't seem to be necessary here.
1511 static struct meta_queue *
1512 meta_queue_new (void)
1514 struct meta_queue *new;
1517 new = XCALLOC (MTYPE_WORK_QUEUE, sizeof (struct meta_queue));
1520 for (i = 0; i < MQ_SIZE; i++)
1522 new->subq[i] = list_new ();
1523 assert(new->subq[i]);
1529 /* initialise zebra rib work queue */
1531 rib_queue_init (struct zebra_t *zebra)
1535 if (! (zebra->ribq = work_queue_new (zebra->master,
1536 "route_node processing")))
1538 zlog_err ("%s: could not initialise work queue!", __func__);
1542 /* fill in the work queue spec */
1543 zebra->ribq->spec.workfunc = &meta_queue_process;
1544 zebra->ribq->spec.errorfunc = NULL;
1545 zebra->ribq->spec.completion_func = &meta_queue_process_complete;
1546 /* XXX: TODO: These should be runtime configurable via vty */
1547 zebra->ribq->spec.max_retries = 3;
1548 zebra->ribq->spec.hold = rib_process_hold_time;
1550 if (!(zebra->mq = meta_queue_new ()))
1552 zlog_err ("%s: could not initialise meta queue!", __func__);
1558 /* RIB updates are processed via a queue of pointers to route_nodes.
1560 * The queue length is bounded by the maximal size of the routing table,
1561 * as a route_node will not be requeued, if already queued.
1563 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
1564 * state, or static_install_route (when an existing RIB is updated)
1565 * and then submit route_node to queue for best-path selection later.
1566 * Order of add/delete state changes are preserved for any given RIB.
1568 * Deleted RIBs are reaped during best-path selection.
1571 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
1572 * |-------->| | best RIB, if required
1574 * static_install->|->rib_addqueue...... -> rib_process
1576 * |-------->| |-> rib_unlink
1577 * |-> set RIB_ENTRY_REMOVE |
1578 * rib_delnode (RIB freed)
1580 * The 'info' pointer of a route_node points to a rib_dest_t
1581 * ('dest'). Queueing state for a route_node is kept on the dest. The
1582 * dest is created on-demand by rib_link() and is kept around at least
1583 * as long as there are ribs hanging off it (@see rib_gc_dest()).
1585 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1587 * - route_nodes: refcounted by:
1588 * - dest attached to route_node:
1589 * - managed by: rib_link/rib_gc_dest
1590 * - route_node processing queue
1591 * - managed by: rib_addqueue, rib_process.
1595 /* Add RIB to head of the route node. */
1597 rib_link (struct route_node *rn, struct rib *rib)
1604 if (IS_ZEBRA_DEBUG_RIB)
1605 rnode_debug (rn, "rn %p, rib %p", (void *)rn, (void *)rib);
1607 dest = rib_dest_from_rnode (rn);
1610 if (IS_ZEBRA_DEBUG_RIB)
1611 rnode_debug (rn, "adding dest to table");
1613 dest = XCALLOC (MTYPE_RIB_DEST, sizeof (rib_dest_t));
1614 route_lock_node (rn); /* rn route table reference */
1619 head = dest->routes;
1626 rib_queue_add (&zebrad, rn);
1630 rib_addnode (struct route_node *rn, struct rib *rib)
1632 /* RIB node has been un-removed before route-node is processed.
1633 * route_node must hence already be on the queue for processing..
1635 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
1637 if (IS_ZEBRA_DEBUG_RIB)
1638 rnode_debug (rn, "rn %p, un-removed rib %p", (void *)rn, (void *)rib);
1640 UNSET_FLAG (rib->status, RIB_ENTRY_REMOVED);
1649 * Detach a rib structure from a route_node.
1651 * Note that a call to rib_unlink() should be followed by a call to
1652 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
1653 * longer required to be deleted.
1656 rib_unlink (struct route_node *rn, struct rib *rib)
1662 if (IS_ZEBRA_DEBUG_RIB)
1663 rnode_debug (rn, "rn %p, rib %p", (void *)rn, (void *)rib);
1665 dest = rib_dest_from_rnode (rn);
1668 rib->next->prev = rib->prev;
1671 rib->prev->next = rib->next;
1674 dest->routes = rib->next;
1677 /* free RIB and nexthops */
1678 nexthops_free(rib->nexthop);
1679 XFREE (MTYPE_RIB, rib);
1684 rib_delnode (struct route_node *rn, struct rib *rib)
1686 if (IS_ZEBRA_DEBUG_RIB)
1687 rnode_debug (rn, "rn %p, rib %p, removing", (void *)rn, (void *)rib);
1688 SET_FLAG (rib->status, RIB_ENTRY_REMOVED);
1689 rib_queue_add (&zebrad, rn);
1693 rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p,
1694 struct in_addr *gate, struct in_addr *src,
1695 ifindex_t ifindex, vrf_id_t vrf_id, int table_id,
1696 u_int32_t metric, u_int32_t mtu, u_char distance, safi_t safi)
1699 struct rib *same = NULL;
1700 struct route_table *table;
1701 struct route_node *rn;
1702 struct nexthop *nexthop;
1705 table = zebra_vrf_table (AFI_IP, safi, vrf_id);
1709 /* Make it sure prefixlen is applied to the prefix. */
1710 apply_mask_ipv4 (p);
1712 /* Set default distance by route type. */
1715 if ((unsigned)type >= array_size(route_info))
1718 distance = route_info[type].distance;
1720 /* iBGP distance is 200. */
1721 if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
1725 /* Lookup route node.*/
1726 rn = route_node_get (table, (struct prefix *) p);
1728 /* If same type of route are installed, treat it as a implicit
1730 RNODE_FOREACH_RIB (rn, rib)
1732 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
1735 if (rib->type != type)
1737 if (rib->type != ZEBRA_ROUTE_CONNECT)
1742 /* Duplicate connected route comes in. */
1743 else if ((nexthop = rib->nexthop) &&
1744 nexthop->type == NEXTHOP_TYPE_IFINDEX &&
1745 nexthop->ifindex == ifindex &&
1746 !CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
1753 /* Allocate new rib structure. */
1754 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
1756 rib->distance = distance;
1758 rib->metric = metric;
1760 rib->vrf_id = vrf_id;
1761 rib->table = table_id;
1762 rib->nexthop_num = 0;
1763 rib->uptime = time (NULL);
1765 /* Nexthop settings. */
1769 rib_nexthop_ipv4_ifindex_add (rib, gate, src, ifindex);
1771 rib_nexthop_ipv4_add (rib, gate, src);
1774 rib_nexthop_ifindex_add (rib, ifindex);
1776 /* If this route is kernel route, set FIB flag to the route. */
1777 if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
1778 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
1779 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1781 /* Link new rib to node.*/
1782 if (IS_ZEBRA_DEBUG_RIB)
1783 zlog_debug ("%s: calling rib_addnode (%p, %p)",
1784 __func__, (void *)rn, (void *)rib);
1785 rib_addnode (rn, rib);
1787 /* Free implicit route.*/
1790 if (IS_ZEBRA_DEBUG_RIB)
1791 zlog_debug ("%s: calling rib_delnode (%p, %p)",
1792 __func__, (void *)rn, (void *)rib);
1793 rib_delnode (rn, same);
1796 route_unlock_node (rn);
1800 /* This function dumps the contents of a given RIB entry into
1801 * standard debug log. Calling function name and IP prefix in
1802 * question are passed as 1st and 2nd arguments.
1805 void _rib_dump (const char * func,
1806 union prefix46constptr pp, const struct rib * rib)
1808 const struct prefix *p = pp.p;
1809 char straddr[PREFIX_STRLEN];
1810 struct nexthop *nexthop, *tnexthop;
1813 zlog_debug ("%s: dumping RIB entry %p for %s vrf %u", func, (void *)rib,
1814 prefix2str(p, straddr, sizeof(straddr)), rib->vrf_id);
1817 "%s: refcnt == %lu, uptime == %lu, type == %u, table == %d",
1820 (unsigned long) rib->uptime,
1826 "%s: metric == %u, distance == %u, flags == %u, status == %u",
1835 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
1838 rib->nexthop_active_num,
1839 rib->nexthop_fib_num
1842 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
1844 inet_ntop (p->family, &nexthop->gate, straddr, INET6_ADDRSTRLEN);
1847 "%s: %s %s with flags %s%s%s",
1849 (recursing ? " NH" : "NH"),
1851 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE) ? "ACTIVE " : ""),
1852 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? "FIB " : ""),
1853 (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ? "RECURSIVE" : "")
1856 zlog_debug ("%s: dump complete", func);
1859 /* This is an exported helper to rtm_read() to dump the strange
1860 * RIB entry found by rib_lookup_ipv4_route()
1863 void rib_lookup_and_dump (struct prefix_ipv4 * p)
1865 struct route_table *table;
1866 struct route_node *rn;
1868 char prefix_buf[INET_ADDRSTRLEN];
1871 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, VRF_DEFAULT);
1874 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__);
1878 /* Scan the RIB table for exactly matching RIB entry. */
1879 rn = route_node_lookup (table, (struct prefix *) p);
1881 /* No route for this prefix. */
1884 zlog_debug ("%s: lookup failed for %s", __func__,
1885 prefix2str((struct prefix*) p, prefix_buf, sizeof(prefix_buf)));
1890 route_unlock_node (rn);
1893 RNODE_FOREACH_RIB (rn, rib)
1897 "%s: rn %p, rib %p: %s, %s",
1901 (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED) ? "removed" : "NOT removed"),
1902 (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? "selected" : "NOT selected")
1909 rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib, safi_t safi)
1911 struct route_table *table;
1912 struct route_node *rn;
1914 struct nexthop *nexthop;
1918 table = zebra_vrf_table (AFI_IP, safi, rib->vrf_id);
1922 /* Make it sure prefixlen is applied to the prefix. */
1923 apply_mask_ipv4 (p);
1925 /* Set default distance by route type. */
1926 if (rib->distance == 0)
1928 rib->distance = route_info[rib->type].distance;
1930 /* iBGP distance is 200. */
1931 if (rib->type == ZEBRA_ROUTE_BGP
1932 && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP))
1933 rib->distance = 200;
1936 /* Lookup route node.*/
1937 rn = route_node_get (table, (struct prefix *) p);
1939 /* If same type of route are installed, treat it as a implicit
1941 RNODE_FOREACH_RIB (rn, same)
1943 if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED))
1946 if (same->type == rib->type && same->table == rib->table
1947 && same->type != ZEBRA_ROUTE_CONNECT)
1951 /* If this route is kernel route, set FIB flag to the route. */
1952 if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT)
1953 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
1954 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
1956 /* Link new rib to node.*/
1957 rib_addnode (rn, rib);
1959 if (IS_ZEBRA_DEBUG_RIB)
1961 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
1962 __func__, (void *)rn, (void *)rib);
1966 /* Free implicit route.*/
1969 if (IS_ZEBRA_DEBUG_RIB)
1971 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
1972 __func__, (void *)rn, (void *)same);
1975 rib_delnode (rn, same);
1979 route_unlock_node (rn);
1983 /* XXX factor with rib_delete_ipv6 */
1985 rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p,
1986 struct in_addr *gate, ifindex_t ifindex,
1987 vrf_id_t vrf_id, safi_t safi)
1989 struct route_table *table;
1990 struct route_node *rn;
1992 struct rib *fib = NULL;
1993 struct rib *same = NULL;
1994 struct nexthop *nexthop, *tnexthop;
1996 char buf1[PREFIX_STRLEN];
1997 char buf2[INET_ADDRSTRLEN];
2000 table = zebra_vrf_table (AFI_IP, safi, vrf_id);
2005 apply_mask_ipv4 (p);
2007 if (IS_ZEBRA_DEBUG_KERNEL)
2010 zlog_debug ("rib_delete_ipv4(): route delete %s vrf %u via %s ifindex %d",
2011 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
2015 zlog_debug ("rib_delete_ipv4(): route delete %s vrf %u ifindex %d",
2016 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
2020 /* Lookup route node. */
2021 rn = route_node_lookup (table, (struct prefix *) p);
2024 if (IS_ZEBRA_DEBUG_KERNEL)
2027 zlog_debug ("route %s vrf %u via %s ifindex %d doesn't exist in rib",
2028 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
2029 inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN),
2032 zlog_debug ("route %s vrf %u ifindex %d doesn't exist in rib",
2033 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
2036 return ZEBRA_ERR_RTNOEXIST;
2039 /* Lookup same type route. */
2040 RNODE_FOREACH_RIB (rn, rib)
2042 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2045 if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB))
2048 if (rib->type != type)
2050 if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
2051 nexthop->type == NEXTHOP_TYPE_IFINDEX)
2053 if (nexthop->ifindex != ifindex)
2058 route_unlock_node (rn);
2059 route_unlock_node (rn);
2065 /* Make sure that the route found has the same gateway. */
2073 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
2074 if (IPV4_ADDR_SAME (&nexthop->gate.ipv4, gate))
2083 /* If same type of route can't be found and this message is from
2087 if (fib && type == ZEBRA_ROUTE_KERNEL &&
2088 CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE))
2090 if (IS_ZEBRA_DEBUG_KERNEL)
2092 zlog_debug ("Zebra route %s/%d was deleted by others from kernel",
2093 inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
2096 /* This means someone else, other than Zebra, has deleted
2097 * a Zebra router from the kernel. We will add it back */
2098 rib_update_kernel(rn, NULL, fib);
2102 if (IS_ZEBRA_DEBUG_KERNEL)
2105 zlog_debug ("route %s vrf %u via %s ifindex %d type %d "
2106 "doesn't exist in rib",
2107 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
2108 inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN),
2112 zlog_debug ("route %s vrf %u ifindex %d type %d doesn't exist in rib",
2113 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
2117 route_unlock_node (rn);
2118 return ZEBRA_ERR_RTNOEXIST;
2123 rib_delnode (rn, same);
2125 route_unlock_node (rn);
2129 /* Install static route into rib. */
2131 static_install_route (afi_t afi, safi_t safi, struct prefix *p, struct static_route *si)
2134 struct route_node *rn;
2135 struct route_table *table;
2138 table = zebra_vrf_table (afi, safi, si->vrf_id);
2142 /* Lookup existing route */
2143 rn = route_node_get (table, p);
2144 RNODE_FOREACH_RIB (rn, rib)
2146 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2149 if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance)
2155 /* if tag value changed , update old value in RIB */
2156 if (rib->tag != si->tag)
2159 /* Same distance static route is there. Update it with new
2161 route_unlock_node (rn);
2164 case STATIC_IPV4_GATEWAY:
2165 rib_nexthop_ipv4_add (rib, &si->addr.ipv4, NULL);
2167 case STATIC_IPV4_IFNAME:
2168 rib_nexthop_ifname_add (rib, si->ifname);
2170 case STATIC_IPV4_BLACKHOLE:
2171 rib_nexthop_blackhole_add (rib);
2173 case STATIC_IPV6_GATEWAY:
2174 rib_nexthop_ipv6_add (rib, &si->addr.ipv6);
2176 case STATIC_IPV6_IFNAME:
2177 rib_nexthop_ifname_add (rib, si->ifname);
2179 case STATIC_IPV6_GATEWAY_IFNAME:
2180 rib_nexthop_ipv6_ifname_add (rib, &si->addr.ipv6, si->ifname);
2183 rib_queue_add (&zebrad, rn);
2187 /* This is new static route. */
2188 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
2190 rib->type = ZEBRA_ROUTE_STATIC;
2191 rib->distance = si->distance;
2193 rib->vrf_id = si->vrf_id;
2194 rib->table = zebrad.rtm_table_default;
2195 rib->nexthop_num = 0;
2200 case STATIC_IPV4_GATEWAY:
2201 rib_nexthop_ipv4_add (rib, &si->addr.ipv4, NULL);
2203 case STATIC_IPV4_IFNAME:
2204 rib_nexthop_ifname_add (rib, si->ifname);
2206 case STATIC_IPV4_BLACKHOLE:
2207 rib_nexthop_blackhole_add (rib);
2209 case STATIC_IPV6_GATEWAY:
2210 rib_nexthop_ipv6_add (rib, &si->addr.ipv6);
2212 case STATIC_IPV6_IFNAME:
2213 rib_nexthop_ifname_add (rib, si->ifname);
2215 case STATIC_IPV6_GATEWAY_IFNAME:
2216 rib_nexthop_ipv6_ifname_add (rib, &si->addr.ipv6, si->ifname);
2220 /* Save the flags of this static routes (reject, blackhole) */
2221 rib->flags = si->flags;
2223 /* Link this rib to the tree. */
2224 rib_addnode (rn, rib);
2229 static_nexthop_same (struct nexthop *nexthop, struct static_route *si)
2231 if (nexthop->type == NEXTHOP_TYPE_IPV4
2232 && si->type == STATIC_IPV4_GATEWAY
2233 && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->addr.ipv4))
2235 if (nexthop->type == NEXTHOP_TYPE_IFNAME
2236 && si->type == STATIC_IPV4_IFNAME
2237 && strcmp (nexthop->ifname, si->ifname) == 0)
2239 if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE
2240 && si->type == STATIC_IPV4_BLACKHOLE)
2242 if (nexthop->type == NEXTHOP_TYPE_IPV6
2243 && si->type == STATIC_IPV6_GATEWAY
2244 && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6))
2246 if (nexthop->type == NEXTHOP_TYPE_IFNAME
2247 && si->type == STATIC_IPV6_IFNAME
2248 && strcmp (nexthop->ifname, si->ifname) == 0)
2250 if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME
2251 && si->type == STATIC_IPV6_GATEWAY_IFNAME
2252 && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->addr.ipv6)
2253 && strcmp (nexthop->ifname, si->ifname) == 0)
2258 /* Uninstall static route from RIB. */
2260 static_uninstall_route (afi_t afi, safi_t safi, struct prefix *p, struct static_route *si)
2262 struct route_node *rn;
2264 struct nexthop *nexthop;
2265 struct route_table *table;
2268 table = zebra_vrf_table (afi, safi, si->vrf_id);
2272 /* Lookup existing route with type and distance. */
2273 rn = route_node_lookup (table, p);
2277 RNODE_FOREACH_RIB (rn, rib)
2279 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2282 if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance &&
2283 rib->tag == si->tag)
2289 route_unlock_node (rn);
2293 /* Lookup nexthop. */
2294 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
2295 if (static_nexthop_same (nexthop, si))
2298 /* Can't find nexthop. */
2301 route_unlock_node (rn);
2305 /* Check nexthop. */
2306 if (rib->nexthop_num == 1)
2307 rib_delnode (rn, rib);
2310 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
2311 rib_uninstall (rn, rib);
2312 rib_nexthop_delete (rib, nexthop);
2313 nexthop_free (nexthop);
2314 rib_queue_add (&zebrad, rn);
2317 route_unlock_node (rn);
2321 static_add_ipv4_safi (safi_t safi, struct prefix *p, struct in_addr *gate,
2322 const char *ifname, u_char flags, route_tag_t tag,
2323 u_char distance, vrf_id_t vrf_id)
2326 struct route_node *rn;
2327 struct static_route *si;
2328 struct static_route *pp;
2329 struct static_route *cp;
2330 struct static_route *update = NULL;
2331 struct zebra_vrf *zvrf = vrf_info_get (vrf_id);
2332 struct route_table *stable = zvrf->stable[AFI_IP][safi];
2337 /* Lookup static route prefix. */
2338 rn = route_node_get (stable, p);
2342 type = STATIC_IPV4_GATEWAY;
2344 type = STATIC_IPV4_IFNAME;
2346 type = STATIC_IPV4_BLACKHOLE;
2348 /* Do nothing if there is a same static route. */
2349 for (si = rn->info; si; si = si->next)
2351 if (type == si->type
2352 && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4))
2353 && (! ifname || strcmp (ifname, si->ifname) == 0))
2355 if (distance == si->distance &&
2358 route_unlock_node (rn);
2366 /* Distance or tag changed. */
2368 static_delete_ipv4_safi (safi, p, gate, ifname, update->tag, update->distance, vrf_id);
2370 /* Make new static route structure. */
2371 si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route));
2374 si->distance = distance;
2377 si->vrf_id = vrf_id;
2380 si->addr.ipv4 = *gate;
2382 si->ifname = XSTRDUP (MTYPE_TMP, ifname);
2384 /* Add new static route information to the tree with sort by
2385 distance value and gateway address. */
2386 for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next)
2388 if (si->distance < cp->distance)
2390 if (si->distance > cp->distance)
2392 if (si->type == STATIC_IPV4_GATEWAY && cp->type == STATIC_IPV4_GATEWAY)
2394 if (ntohl (si->addr.ipv4.s_addr) < ntohl (cp->addr.ipv4.s_addr))
2396 if (ntohl (si->addr.ipv4.s_addr) > ntohl (cp->addr.ipv4.s_addr))
2401 /* Make linked list. */
2411 /* Install into rib. */
2412 static_install_route (AFI_IP, safi, p, si);
2418 static_delete_ipv4_safi (safi_t safi, struct prefix *p, struct in_addr *gate,
2419 const char *ifname, route_tag_t tag, u_char distance,
2423 struct route_node *rn;
2424 struct static_route *si;
2425 struct route_table *stable;
2428 stable = zebra_vrf_static_table (AFI_IP, safi, vrf_id);
2432 /* Lookup static route prefix. */
2433 rn = route_node_lookup (stable, p);
2439 type = STATIC_IPV4_GATEWAY;
2441 type = STATIC_IPV4_IFNAME;
2443 type = STATIC_IPV4_BLACKHOLE;
2445 /* Find same static route is the tree */
2446 for (si = rn->info; si; si = si->next)
2447 if (type == si->type
2448 && (! gate || IPV4_ADDR_SAME (gate, &si->addr.ipv4))
2449 && (! ifname || strcmp (ifname, si->ifname) == 0)
2450 && (! tag || (tag == si->tag)))
2453 /* Can't find static route. */
2456 route_unlock_node (rn);
2460 /* Install into rib. */
2461 static_uninstall_route (AFI_IP, safi, p, si);
2463 /* Unlink static route from linked list. */
2465 si->prev->next = si->next;
2467 rn->info = si->next;
2469 si->next->prev = si->prev;
2470 route_unlock_node (rn);
2472 /* Free static route configuration. */
2474 XFREE (0, si->ifname);
2475 XFREE (MTYPE_STATIC_ROUTE, si);
2477 route_unlock_node (rn);
2483 rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p,
2484 struct in6_addr *gate, ifindex_t ifindex,
2485 vrf_id_t vrf_id, int table_id,
2486 u_int32_t metric, u_int32_t mtu, u_char distance, safi_t safi)
2489 struct rib *same = NULL;
2490 struct route_table *table;
2491 struct route_node *rn;
2492 struct nexthop *nexthop;
2495 table = zebra_vrf_table (AFI_IP6, safi, vrf_id);
2499 /* Make sure mask is applied. */
2500 apply_mask_ipv6 (p);
2502 /* Set default distance by route type. */
2504 distance = route_info[type].distance;
2506 if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP))
2509 /* Lookup route node.*/
2510 rn = route_node_get (table, (struct prefix *) p);
2512 /* If same type of route are installed, treat it as a implicit
2514 RNODE_FOREACH_RIB (rn, rib)
2516 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2519 if (rib->type != type)
2521 if (rib->type != ZEBRA_ROUTE_CONNECT)
2526 else if ((nexthop = rib->nexthop) &&
2527 nexthop->type == NEXTHOP_TYPE_IFINDEX &&
2528 nexthop->ifindex == ifindex)
2535 /* Allocate new rib structure. */
2536 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
2539 rib->distance = distance;
2541 rib->metric = metric;
2543 rib->vrf_id = vrf_id;
2544 rib->table = table_id;
2545 rib->nexthop_num = 0;
2546 rib->uptime = time (NULL);
2548 /* Nexthop settings. */
2552 rib_nexthop_ipv6_ifindex_add (rib, gate, ifindex);
2554 rib_nexthop_ipv6_add (rib, gate);
2557 rib_nexthop_ifindex_add (rib, ifindex);
2559 /* If this route is kernel route, set FIB flag to the route. */
2560 if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT)
2561 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
2562 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2564 /* Link new rib to node.*/
2565 rib_addnode (rn, rib);
2566 if (IS_ZEBRA_DEBUG_RIB)
2568 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
2569 __func__, (void *)rn, (void *)rib);
2573 /* Free implicit route.*/
2576 if (IS_ZEBRA_DEBUG_RIB)
2578 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
2579 __func__, (void *)rn, (void *)same);
2582 rib_delnode (rn, same);
2585 route_unlock_node (rn);
2590 rib_add_ipv6_multipath (struct prefix_ipv6 *p, struct rib *rib, safi_t safi)
2592 struct route_table *table;
2593 struct route_node *rn;
2594 struct rib *same = NULL;
2595 struct nexthop *nexthop;
2599 return 0; /* why are we getting called with NULL rib */
2602 table = zebra_vrf_table (AFI_IP6, safi, rib->vrf_id);
2607 /* Make sure mask is applied. */
2608 apply_mask_ipv6 (p);
2610 /* Set default distance by route type. */
2611 if (rib->distance == 0)
2613 rib->distance = route_info[rib->type].distance;
2615 /* iBGP distance is 200. */
2616 if (rib->type == ZEBRA_ROUTE_BGP
2617 && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP))
2618 rib->distance = 200;
2621 /* Lookup route node.*/
2622 rn = route_node_get (table, (struct prefix *) p);
2624 /* If same type of route are installed, treat it as a implicit
2626 RNODE_FOREACH_RIB (rn, same) {
2627 if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED)) {
2630 if (same->type != rib->type) {
2634 if (same->table != rib->table) {
2637 if (same->type != ZEBRA_ROUTE_CONNECT) {
2642 /* If this route is kernel route, set FIB flag to the route. */
2643 if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT) {
2644 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) {
2645 SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB);
2649 /* Link new rib to node.*/
2650 rib_addnode (rn, rib);
2652 /* Free implicit route.*/
2655 if (IS_ZEBRA_DEBUG_RIB)
2657 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
2658 __func__, rn, same);
2659 rib_dump ((struct prefix *)p, same);
2661 rib_delnode (rn, same);
2665 route_unlock_node (rn);
2669 /* XXX factor with rib_delete_ipv6 */
2671 rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p,
2672 struct in6_addr *gate, ifindex_t ifindex,
2673 vrf_id_t vrf_id, safi_t safi)
2675 struct route_table *table;
2676 struct route_node *rn;
2678 struct rib *fib = NULL;
2679 struct rib *same = NULL;
2680 struct nexthop *nexthop, *tnexthop;
2682 char buf1[PREFIX_STRLEN];
2683 char buf2[INET6_ADDRSTRLEN];
2686 apply_mask_ipv6 (p);
2689 table = zebra_vrf_table (AFI_IP6, safi, vrf_id);
2693 /* Lookup route node. */
2694 rn = route_node_lookup (table, (struct prefix *) p);
2697 if (IS_ZEBRA_DEBUG_KERNEL)
2700 zlog_debug ("route %s vrf %u via %s ifindex %d doesn't exist in rib",
2701 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
2702 inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN),
2705 zlog_debug ("route %s vrf %u ifindex %d doesn't exist in rib",
2706 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
2709 return ZEBRA_ERR_RTNOEXIST;
2712 /* Lookup same type route. */
2713 RNODE_FOREACH_RIB (rn, rib)
2715 if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED))
2718 if (CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB))
2721 if (rib->type != type)
2723 if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) &&
2724 nexthop->type == NEXTHOP_TYPE_IFINDEX)
2726 if (nexthop->ifindex != ifindex)
2731 route_unlock_node (rn);
2732 route_unlock_node (rn);
2738 /* Make sure that the route found has the same gateway. */
2746 for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing))
2747 if (IPV6_ADDR_SAME (&nexthop->gate.ipv6, gate))
2757 /* If same type of route can't be found and this message is from
2761 if (fib && type == ZEBRA_ROUTE_KERNEL &&
2762 CHECK_FLAG(flags, ZEBRA_FLAG_SELFROUTE))
2764 if (IS_ZEBRA_DEBUG_KERNEL)
2766 zlog_debug ("Zebra route %s/%d was deleted by others from kernel",
2767 inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN),
2770 /* This means someone else, other than Zebra, has deleted a Zebra
2771 * route from the kernel. We will add it back */
2772 rib_update_kernel(rn, NULL, fib);
2776 if (IS_ZEBRA_DEBUG_KERNEL)
2779 zlog_debug ("route %s vrf %u via %s ifindex %d type %d "
2780 "doesn't exist in rib",
2781 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
2782 inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN),
2786 zlog_debug ("route %s vrf %u ifindex %d type %d doesn't exist in rib",
2787 prefix2str (p, buf1, sizeof(buf1)), vrf_id,
2791 route_unlock_node (rn);
2792 return ZEBRA_ERR_RTNOEXIST;
2797 rib_delnode (rn, same);
2799 route_unlock_node (rn);
2803 /* Add static route into static route configuration. */
2805 static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
2806 const char *ifname, u_char flags, route_tag_t tag,
2807 u_char distance, vrf_id_t vrf_id)
2809 struct route_node *rn;
2810 struct static_route *si;
2811 struct static_route *pp;
2812 struct static_route *cp;
2813 struct static_route *update = NULL;
2814 struct zebra_vrf *zvrf = vrf_info_get (vrf_id);
2815 struct route_table *stable = zvrf->stable[AFI_IP6][SAFI_UNICAST];
2821 (type == STATIC_IPV6_GATEWAY || type == STATIC_IPV6_GATEWAY_IFNAME))
2825 (type == STATIC_IPV6_GATEWAY_IFNAME || type == STATIC_IPV6_IFNAME))
2828 /* Lookup static route prefix. */
2829 rn = route_node_get (stable, p);
2831 /* Do nothing if there is a same static route. */
2832 for (si = rn->info; si; si = si->next)
2834 if (type == si->type
2836 && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6))
2837 && (! ifname || strcmp (ifname, si->ifname) == 0))
2839 if (distance == si->distance)
2841 route_unlock_node (rn);
2850 static_delete_ipv6(p, type, gate, ifname, tag, update->distance, vrf_id);
2852 /* Make new static route structure. */
2853 si = XCALLOC (MTYPE_STATIC_ROUTE, sizeof (struct static_route));
2856 si->distance = distance;
2859 si->vrf_id = vrf_id;
2863 case STATIC_IPV6_GATEWAY:
2864 si->addr.ipv6 = *gate;
2866 case STATIC_IPV6_IFNAME:
2867 si->ifname = XSTRDUP (MTYPE_TMP, ifname);
2869 case STATIC_IPV6_GATEWAY_IFNAME:
2870 si->addr.ipv6 = *gate;
2871 si->ifname = XSTRDUP (MTYPE_TMP, ifname);
2875 /* Add new static route information to the tree with sort by
2876 distance value and gateway address. */
2877 for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next)
2879 if (si->distance < cp->distance)
2881 if (si->distance > cp->distance)
2885 /* Make linked list. */
2895 /* Install into rib. */
2896 static_install_route (AFI_IP6, SAFI_UNICAST, p, si);
2901 /* Delete static route from static route configuration. */
2903 static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate,
2904 const char *ifname, route_tag_t tag, u_char distance,
2907 struct route_node *rn;
2908 struct static_route *si;
2909 struct route_table *stable;
2912 stable = zebra_vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id);
2916 /* Lookup static route prefix. */
2917 rn = route_node_lookup (stable, p);
2921 /* Find same static route is the tree */
2922 for (si = rn->info; si; si = si->next)
2923 if (distance == si->distance
2925 && (! gate || IPV6_ADDR_SAME (gate, &si->addr.ipv6))
2926 && (! ifname || strcmp (ifname, si->ifname) == 0)
2927 && (! tag || (tag == si->tag)))
2930 /* Can't find static route. */
2933 route_unlock_node (rn);
2937 /* Install into rib. */
2938 static_uninstall_route (AFI_IP6, SAFI_UNICAST, p, si);
2940 /* Unlink static route from linked list. */
2942 si->prev->next = si->next;
2944 rn->info = si->next;
2946 si->next->prev = si->prev;
2948 /* Free static route configuration. */
2950 XFREE (0, si->ifname);
2951 XFREE (MTYPE_STATIC_ROUTE, si);
2956 /* RIB update function. */
2958 rib_update (vrf_id_t vrf_id)
2960 struct route_node *rn;
2961 struct route_table *table;
2963 table = zebra_vrf_table (AFI_IP, SAFI_UNICAST, vrf_id);
2965 for (rn = route_top (table); rn; rn = route_next (rn))
2966 if (rnode_to_ribs (rn))
2967 rib_queue_add (&zebrad, rn);
2969 table = zebra_vrf_table (AFI_IP6, SAFI_UNICAST, vrf_id);
2971 for (rn = route_top (table); rn; rn = route_next (rn))
2972 if (rnode_to_ribs (rn))
2973 rib_queue_add (&zebrad, rn);
2977 /* Remove all routes which comes from non main table. */
2979 rib_weed_table (struct route_table *table)
2981 struct route_node *rn;
2986 for (rn = route_top (table); rn; rn = route_next (rn))
2987 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
2989 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
2992 if (rib->table != zebrad.rtm_table_default &&
2993 rib->table != RT_TABLE_MAIN)
2994 rib_delnode (rn, rib);
2998 /* Delete all routes from non main table. */
3000 rib_weed_tables (void)
3003 struct zebra_vrf *zvrf;
3005 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
3006 if ((zvrf = vrf_iter2info (iter)) != NULL)
3008 rib_weed_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
3009 rib_weed_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
3014 /* Delete self installed routes after zebra is relaunched. */
3016 rib_sweep_table (struct route_table *table)
3018 struct route_node *rn;
3024 for (rn = route_top (table); rn; rn = route_next (rn))
3025 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
3027 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3030 if (rib->type == ZEBRA_ROUTE_KERNEL &&
3031 CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE))
3033 ret = rib_update_kernel (rn, rib, NULL);
3035 rib_delnode (rn, rib);
3041 /* Sweep all RIB tables. */
3043 rib_sweep_route (void)
3046 struct zebra_vrf *zvrf;
3048 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
3049 if ((zvrf = vrf_iter2info (iter)) != NULL)
3051 rib_weed_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
3052 rib_weed_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
3056 /* Remove specific by protocol routes from 'table'. */
3057 static unsigned long
3058 rib_score_proto_table (u_char proto, struct route_table *table)
3060 struct route_node *rn;
3063 unsigned long n = 0;
3066 for (rn = route_top (table); rn; rn = route_next (rn))
3067 RNODE_FOREACH_RIB_SAFE (rn, rib, next)
3069 if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
3071 if (rib->type == proto)
3073 rib_delnode (rn, rib);
3081 /* Remove specific by protocol routes. */
3083 rib_score_proto (u_char proto)
3086 struct zebra_vrf *zvrf;
3087 unsigned long cnt = 0;
3089 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
3090 if ((zvrf = vrf_iter2info (iter)) != NULL)
3091 cnt += rib_score_proto_table (proto, zvrf->table[AFI_IP][SAFI_UNICAST])
3092 +rib_score_proto_table (proto, zvrf->table[AFI_IP6][SAFI_UNICAST]);
3097 /* Close RIB and clean up kernel routes. */
3099 rib_close_table (struct route_table *table)
3101 struct route_node *rn;
3102 rib_table_info_t *info = table->info;
3106 for (rn = route_top (table); rn; rn = route_next (rn))
3107 RNODE_FOREACH_RIB (rn, rib)
3109 if (!CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB))
3112 if (info->safi == SAFI_UNICAST)
3113 zfpm_trigger_update (rn, NULL);
3115 if (! RIB_SYSTEM_ROUTE (rib))
3116 rib_update_kernel (rn, rib, NULL);
3120 /* Close all RIB tables. */
3125 struct zebra_vrf *zvrf;
3127 for (iter = vrf_first (); iter != VRF_ITER_INVALID; iter = vrf_next (iter))
3128 if ((zvrf = vrf_iter2info (iter)) != NULL)
3130 rib_close_table (zvrf->table[AFI_IP][SAFI_UNICAST]);
3131 rib_close_table (zvrf->table[AFI_IP6][SAFI_UNICAST]);
3135 /* Routing information base initialize. */
3139 rib_queue_init (&zebrad);
3145 * Get the first vrf id that is greater than the given vrf id if any.
3147 * Returns TRUE if a vrf id was found, FALSE otherwise.
3150 vrf_id_get_next (vrf_id_t vrf_id, vrf_id_t *next_id_p)
3152 vrf_iter_t iter = vrf_iterator (vrf_id);
3153 struct zebra_vrf *zvrf = vrf_iter2info (iter);
3155 /* The same one ? Then find out the next. */
3156 if (zvrf && (zvrf->vrf_id == vrf_id))
3157 zvrf = vrf_iter2info (vrf_next (iter));
3161 *next_id_p = zvrf->vrf_id;
3169 * rib_tables_iter_next
3171 * Returns the next table in the iteration.
3173 struct route_table *
3174 rib_tables_iter_next (rib_tables_iter_t *iter)
3176 struct route_table *table;
3179 * Array that helps us go over all AFI/SAFI combinations via one
3186 { AFI_IP, SAFI_UNICAST },
3187 { AFI_IP, SAFI_MULTICAST },
3188 { AFI_IP6, SAFI_UNICAST },
3189 { AFI_IP6, SAFI_MULTICAST },
3194 switch (iter->state)
3197 case RIB_TABLES_ITER_S_INIT:
3198 iter->vrf_id = VRF_DEFAULT;
3199 iter->afi_safi_ix = -1;
3203 case RIB_TABLES_ITER_S_ITERATING:
3204 iter->afi_safi_ix++;
3208 while (iter->afi_safi_ix < (int) ZEBRA_NUM_OF (afi_safis))
3210 table = zebra_vrf_table (afi_safis[iter->afi_safi_ix].afi,
3211 afi_safis[iter->afi_safi_ix].safi,
3216 iter->afi_safi_ix++;
3220 * Found another table in this vrf.
3226 * Done with all tables in the current vrf, go to the next
3229 if (!vrf_id_get_next (iter->vrf_id, &iter->vrf_id))
3232 iter->afi_safi_ix = 0;
3237 case RIB_TABLES_ITER_S_DONE:
3242 iter->state = RIB_TABLES_ITER_S_ITERATING;
3244 iter->state = RIB_TABLES_ITER_S_DONE;
3249 /* Lookup VRF by identifier. */
3251 zebra_vrf_lookup (vrf_id_t vrf_id)
3253 return vrf_info_lookup (vrf_id);
3257 * Create a routing table for the specific AFI/SAFI in the given VRF.
3260 zebra_vrf_table_create (struct zebra_vrf *zvrf, afi_t afi, safi_t safi)
3262 rib_table_info_t *info;
3263 struct route_table *table;
3265 assert (!zvrf->table[afi][safi]);
3267 table = route_table_init ();
3268 zvrf->table[afi][safi] = table;
3270 info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info));
3277 /* Allocate new zebra VRF. */
3279 zebra_vrf_alloc (vrf_id_t vrf_id)
3281 struct zebra_vrf *zvrf;
3286 zvrf = XCALLOC (MTYPE_ZEBRA_VRF, sizeof (struct zebra_vrf));
3288 /* Allocate routing table and static table. */
3289 zebra_vrf_table_create (zvrf, AFI_IP, SAFI_UNICAST);
3290 zebra_vrf_table_create (zvrf, AFI_IP6, SAFI_UNICAST);
3291 zvrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init ();
3292 zvrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init ();
3293 zebra_vrf_table_create (zvrf, AFI_IP, SAFI_MULTICAST);
3294 zebra_vrf_table_create (zvrf, AFI_IP6, SAFI_MULTICAST);
3295 zvrf->stable[AFI_IP][SAFI_MULTICAST] = route_table_init ();
3296 zvrf->stable[AFI_IP6][SAFI_MULTICAST] = route_table_init ();
3298 zvrf->rnh_table[AFI_IP] = route_table_init();
3299 zvrf->rnh_table[AFI_IP6] = route_table_init();
3302 zvrf->vrf_id = vrf_id;
3305 /* Initialize netlink sockets */
3306 snprintf (nl_name, 64, "netlink-listen (vrf %u)", vrf_id);
3307 zvrf->netlink.sock = -1;
3308 zvrf->netlink.name = XSTRDUP (MTYPE_NETLINK_NAME, nl_name);
3310 snprintf (nl_name, 64, "netlink-cmd (vrf %u)", vrf_id);
3311 zvrf->netlink_cmd.sock = -1;
3312 zvrf->netlink_cmd.name = XSTRDUP (MTYPE_NETLINK_NAME, nl_name);
3318 /* Lookup the routing table in an enabled VRF. */
3319 struct route_table *
3320 zebra_vrf_table (afi_t afi, safi_t safi, vrf_id_t vrf_id)
3322 struct zebra_vrf *zvrf = vrf_info_lookup (vrf_id);
3327 if (afi >= AFI_MAX || safi >= SAFI_MAX)
3330 return zvrf->table[afi][safi];
3333 /* Lookup the static routing table in a VRF. */
3334 struct route_table *
3335 zebra_vrf_static_table (afi_t afi, safi_t safi, vrf_id_t vrf_id)
3337 struct zebra_vrf *zvrf = vrf_info_lookup (vrf_id);
3342 if (afi >= AFI_MAX || safi >= SAFI_MAX)
3345 return zvrf->stable[afi][safi];