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/zebra_ns.h"
43 #include "zebra/zserv.h"
44 #include "zebra/zebra_vrf.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/zebra_routemap.h"
47 #include "zebra/debug.h"
48 #include "zebra/zebra_fpm.h"
49 #include "zebra/zebra_rnh.h"
50 #include "zebra/interface.h"
51 #include "zebra/connected.h"
53 /* Should we allow non Quagga processes to delete our routes */
54 extern int allow_delete
;
56 /* Hold time for RIB process, should be very minimal.
57 * it is useful to able to set it otherwise for testing, hence exported
58 * as global here for test-rig code.
60 int rib_process_hold_time
= 10;
62 /* Each route type's string and default distance value. */
67 } route_info
[ZEBRA_ROUTE_MAX
] =
69 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
70 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
71 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
72 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
73 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
74 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
75 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
76 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
77 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
78 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
79 /* no entry/default: 150 */
82 /* RPF lookup behaviour */
83 static enum multicast_mode ipv4_multicast_mode
= MCAST_NO_CONFIG
;
86 static void __attribute__((format (printf
, 5, 6)))
87 _rnode_zlog(const char *_func
, vrf_id_t vrf_id
, struct route_node
*rn
, int priority
,
88 const char *msgfmt
, ...)
90 char buf
[PREFIX_STRLEN
+ 8];
95 vsnprintf(msgbuf
, sizeof(msgbuf
), msgfmt
, ap
);
100 rib_table_info_t
*info
= rn
->table
->info
;
102 prefix2str(&rn
->p
, buf
, sizeof(buf
));
103 if (info
->safi
== SAFI_MULTICAST
)
104 strcat(buf
, " (MRIB)");
108 snprintf(buf
, sizeof(buf
), "{(route_node *) NULL}");
111 zlog (NULL
, priority
, "%s: %d:%s: %s", _func
, vrf_id
, buf
, msgbuf
);
114 #define rnode_debug(node, vrf_id, ...) \
115 _rnode_zlog(__func__, vrf_id, node, LOG_DEBUG, __VA_ARGS__)
116 #define rnode_info(node, ...) \
117 _rnode_zlog(__func__, vrf_id, node, LOG_INFO, __VA_ARGS__)
120 is_zebra_valid_kernel_table(u_int32_t table_id
)
122 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
) ||
123 (table_id
== RT_TABLE_UNSPEC
) ||
124 (table_id
== RT_TABLE_LOCAL
) ||
125 (table_id
== RT_TABLE_COMPAT
))
132 is_zebra_main_routing_table(u_int32_t table_id
)
134 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
140 zebra_check_addr (struct prefix
*p
)
142 if (p
->family
== AF_INET
)
146 addr
= p
->u
.prefix4
.s_addr
;
149 if (IPV4_NET127 (addr
)
151 || IPV4_LINKLOCAL(addr
))
154 if (p
->family
== AF_INET6
)
156 if (IN6_IS_ADDR_LOOPBACK (&p
->u
.prefix6
))
158 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
164 /* Add nexthop to the end of a rib node's nexthop list */
166 rib_nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
168 nexthop_add(&rib
->nexthop
, nexthop
);
175 * copy_nexthop - copy a nexthop to the rib structure.
178 rib_copy_nexthops (struct rib
*rib
, struct nexthop
*nh
)
180 struct nexthop
*nexthop
;
182 nexthop
= nexthop_new();
183 nexthop
->flags
= nh
->flags
;
184 nexthop
->type
= nh
->type
;
185 nexthop
->ifindex
= nh
->ifindex
;
186 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
187 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
188 rib_nexthop_add(rib
, nexthop
);
189 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
190 copy_nexthops(&nexthop
->resolved
, nh
->resolved
);
193 /* Delete specified nexthop from the list. */
195 rib_nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
198 nexthop
->next
->prev
= nexthop
->prev
;
200 nexthop
->prev
->next
= nexthop
->next
;
202 rib
->nexthop
= nexthop
->next
;
209 rib_nexthop_ifindex_add (struct rib
*rib
, unsigned int ifindex
)
211 struct nexthop
*nexthop
;
213 nexthop
= nexthop_new();
214 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
215 nexthop
->ifindex
= ifindex
;
217 rib_nexthop_add (rib
, nexthop
);
223 rib_nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
225 struct nexthop
*nexthop
;
227 nexthop
= nexthop_new();
228 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
229 nexthop
->gate
.ipv4
= *ipv4
;
231 nexthop
->src
.ipv4
= *src
;
233 rib_nexthop_add (rib
, nexthop
);
239 rib_nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
240 struct in_addr
*src
, unsigned int ifindex
)
242 struct nexthop
*nexthop
;
243 struct interface
*ifp
;
245 nexthop
= nexthop_new();
246 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
247 nexthop
->gate
.ipv4
= *ipv4
;
249 nexthop
->src
.ipv4
= *src
;
250 nexthop
->ifindex
= ifindex
;
251 ifp
= if_lookup_by_index (nexthop
->ifindex
);
252 /*Pending: need to think if null ifp here is ok during bootup?
253 There was a crash because ifp here was coming to be NULL */
255 if (connected_is_unnumbered(ifp
)) {
256 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
259 rib_nexthop_add (rib
, nexthop
);
265 rib_nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
267 struct nexthop
*nexthop
;
269 nexthop
= nexthop_new();
270 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
271 nexthop
->gate
.ipv6
= *ipv6
;
273 rib_nexthop_add (rib
, nexthop
);
279 rib_nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
280 unsigned int ifindex
)
282 struct nexthop
*nexthop
;
284 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
285 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
286 nexthop
->gate
.ipv6
= *ipv6
;
287 nexthop
->ifindex
= ifindex
;
289 rib_nexthop_add (rib
, nexthop
);
295 rib_nexthop_blackhole_add (struct rib
*rib
)
297 struct nexthop
*nexthop
;
299 nexthop
= nexthop_new();
300 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
301 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
303 rib_nexthop_add (rib
, nexthop
);
308 /* This method checks whether a recursive nexthop has at
309 * least one resolved nexthop in the fib.
312 nexthop_has_fib_child(struct nexthop
*nexthop
)
316 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
319 for (nh
= nexthop
->resolved
; nh
; nh
= nh
->next
)
320 if (CHECK_FLAG (nh
->flags
, NEXTHOP_FLAG_FIB
))
326 /* If force flag is not set, do not modify falgs at all for uninstall
327 the route from FIB. */
329 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
330 struct route_node
*top
)
332 struct prefix_ipv4 p
;
333 struct route_table
*table
;
334 struct route_node
*rn
;
337 struct nexthop
*newhop
, *tnewhop
;
338 struct nexthop
*resolved_hop
;
340 struct interface
*ifp
;
342 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
343 nexthop
->ifindex
= 0;
347 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
348 zebra_deregister_rnh_static_nexthops(rib
->vrf_id
, nexthop
->resolved
, top
);
349 nexthops_free(nexthop
->resolved
);
350 nexthop
->resolved
= NULL
;
353 /* Skip nexthops that have been filtered out due to route-map */
354 /* The nexthops are specific to this route and so the same */
355 /* nexthop for a different route may not have this flag set */
356 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
360 * Check to see if we should trust the passed in information
361 * for UNNUMBERED interfaces as that we won't find the GW
362 * address in the routing table.
364 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
366 ifp
= if_lookup_by_index (nexthop
->ifindex
);
367 if (ifp
&& connected_is_unnumbered(ifp
))
369 if (if_is_operative(ifp
))
378 /* Make lookup prefix. */
379 memset (&p
, 0, sizeof (struct prefix_ipv4
));
381 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
382 p
.prefix
= nexthop
->gate
.ipv4
;
385 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, rib
->vrf_id
);
389 rn
= route_node_match (table
, (struct prefix
*) &p
);
392 route_unlock_node (rn
);
394 /* If lookup self prefix return immediately. */
398 /* Pick up selected route. */
399 /* However, do not resolve over default route unless explicitly allowed. */
400 if (is_default_prefix (&rn
->p
) &&
401 !nh_resolve_via_default (p
.family
))
404 RNODE_FOREACH_RIB (rn
, match
)
406 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
408 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
412 /* If there is no selected route or matched route is EGP, go up
418 } while (rn
&& rn
->info
== NULL
);
420 route_lock_node (rn
);
424 /* If the longest prefix match for the nexthop yields
425 * a blackhole, mark it as inactive. */
426 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
427 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
430 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
432 /* Directly point connected route. */
433 newhop
= match
->nexthop
;
434 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
435 nexthop
->ifindex
= newhop
->ifindex
;
439 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
442 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
443 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
444 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
448 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
449 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
451 resolved_hop
= XCALLOC(MTYPE_NEXTHOP
, sizeof (struct nexthop
));
452 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
453 /* If the resolving route specifies a gateway, use it */
454 if (newhop
->type
== NEXTHOP_TYPE_IPV4
455 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
457 resolved_hop
->type
= newhop
->type
;
458 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
462 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
463 resolved_hop
->ifindex
= newhop
->ifindex
;
464 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
465 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
469 /* If the resolving route is an interface route,
470 * it means the gateway we are looking up is connected
471 * to that interface. (The actual network is _not_ onlink).
472 * Therefore, the resolved route should have the original
473 * gateway as nexthop as it is directly connected.
475 * On Linux, we have to set the onlink netlink flag because
476 * otherwise, the kernel won't accept the route. */
477 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
479 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
480 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
481 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
482 resolved_hop
->ifindex
= newhop
->ifindex
;
485 nexthop_add(&nexthop
->resolved
, resolved_hop
);
491 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
494 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
495 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
499 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
501 resolved_hop
= XCALLOC(MTYPE_NEXTHOP
, sizeof (struct nexthop
));
502 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
503 /* If the resolving route specifies a gateway, use it */
504 if (newhop
->type
== NEXTHOP_TYPE_IPV4
505 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
507 resolved_hop
->type
= newhop
->type
;
508 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
512 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
513 resolved_hop
->ifindex
= newhop
->ifindex
;
514 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
515 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
519 /* If the resolving route is an interface route,
520 * it means the gateway we are looking up is connected
521 * to that interface. (The actual network is _not_ onlink).
522 * Therefore, the resolved route should have the original
523 * gateway as nexthop as it is directly connected.
525 * On Linux, we have to set the onlink netlink flag because
526 * otherwise, the kernel won't accept the route.
528 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
530 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
531 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
532 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
533 resolved_hop
->ifindex
= newhop
->ifindex
;
536 nexthop_add(&nexthop
->resolved
, resolved_hop
);
551 /* If force flag is not set, do not modify falgs at all for uninstall
552 the route from FIB. */
554 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
555 struct route_node
*top
)
557 struct prefix_ipv6 p
;
558 struct route_table
*table
;
559 struct route_node
*rn
;
562 struct nexthop
*newhop
, *tnewhop
;
564 struct nexthop
*resolved_hop
;
566 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
567 nexthop
->ifindex
= 0;
571 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
572 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, nexthop
->resolved
, top
);
573 nexthops_free(nexthop
->resolved
);
574 nexthop
->resolved
= NULL
;
577 /* Skip nexthops that have been filtered out due to route-map */
578 /* The nexthops are specific to this route and so the same */
579 /* nexthop for a different route may not have this flag set */
580 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
583 /* Make lookup prefix. */
584 memset (&p
, 0, sizeof (struct prefix_ipv6
));
586 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
587 p
.prefix
= nexthop
->gate
.ipv6
;
590 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, rib
->vrf_id
);
594 rn
= route_node_match (table
, (struct prefix
*) &p
);
597 route_unlock_node (rn
);
599 /* If lookup self prefix return immediately. */
603 /* Pick up selected route. */
604 /* However, do not resolve over default route unless explicitly allowed. */
605 if (is_default_prefix (&rn
->p
) &&
606 !nh_resolve_via_default (p
.family
))
609 RNODE_FOREACH_RIB (rn
, match
)
611 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
613 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
617 /* If there is no selected route or matched route is EGP, go up
623 } while (rn
&& rn
->info
== NULL
);
625 route_lock_node (rn
);
629 /* If the longest prefix match for the nexthop yields
630 * a blackhole, mark it as inactive. */
631 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
632 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
635 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
637 /* Directly point connected route. */
638 newhop
= match
->nexthop
;
640 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
641 nexthop
->ifindex
= newhop
->ifindex
;
645 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
648 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
649 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
650 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
654 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
655 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
657 resolved_hop
= XCALLOC(MTYPE_NEXTHOP
, sizeof (struct nexthop
));
658 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
659 /* See nexthop_active_ipv4 for a description how the
660 * resolved nexthop is constructed. */
661 if (newhop
->type
== NEXTHOP_TYPE_IPV6
662 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
664 resolved_hop
->type
= newhop
->type
;
665 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
669 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
670 resolved_hop
->ifindex
= newhop
->ifindex
;
674 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
676 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
677 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
678 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
679 resolved_hop
->ifindex
= newhop
->ifindex
;
682 nexthop_add(&nexthop
->resolved
, resolved_hop
);
688 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
691 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
692 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
696 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
698 resolved_hop
= XCALLOC(MTYPE_NEXTHOP
, sizeof (struct nexthop
));
699 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
700 /* See nexthop_active_ipv4 for a description how the
701 * resolved nexthop is constructed. */
702 if (newhop
->type
== NEXTHOP_TYPE_IPV6
703 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
)
705 resolved_hop
->type
= newhop
->type
;
706 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
710 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
711 resolved_hop
->ifindex
= newhop
->ifindex
;
715 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
)
717 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
718 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
719 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
720 resolved_hop
->ifindex
= newhop
->ifindex
;
723 nexthop_add(&nexthop
->resolved
, resolved_hop
);
739 rib_match_ipv4 (struct in_addr addr
, safi_t safi
, vrf_id_t vrf_id
,
740 struct route_node
**rn_out
)
742 struct prefix_ipv4 p
;
743 struct route_table
*table
;
744 struct route_node
*rn
;
746 struct nexthop
*newhop
, *tnewhop
;
750 table
= zebra_vrf_table (AFI_IP
, safi
, vrf_id
);
754 memset (&p
, 0, sizeof (struct prefix_ipv4
));
756 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
759 rn
= route_node_match (table
, (struct prefix
*) &p
);
763 route_unlock_node (rn
);
765 /* Pick up selected route. */
766 RNODE_FOREACH_RIB (rn
, match
)
768 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
770 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
774 /* If there is no selected route or matched route is EGP, go up
780 } while (rn
&& rn
->info
== NULL
);
782 route_lock_node (rn
);
786 if (match
->type
!= ZEBRA_ROUTE_CONNECT
)
789 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
790 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
808 rib_match_ipv4_multicast (struct in_addr addr
, struct route_node
**rn_out
)
810 struct rib
*rib
= NULL
, *mrib
= NULL
, *urib
= NULL
;
811 struct route_node
*m_rn
= NULL
, *u_rn
= NULL
;
812 int skip_bgp
= 0; /* bool */
814 switch (ipv4_multicast_mode
)
816 case MCAST_MRIB_ONLY
:
817 return rib_match_ipv4 (addr
, SAFI_MULTICAST
, skip_bgp
, rn_out
);
818 case MCAST_URIB_ONLY
:
819 return rib_match_ipv4 (addr
, SAFI_UNICAST
, skip_bgp
, rn_out
);
820 case MCAST_NO_CONFIG
:
821 case MCAST_MIX_MRIB_FIRST
:
822 rib
= mrib
= rib_match_ipv4 (addr
, SAFI_MULTICAST
, skip_bgp
, &m_rn
);
824 rib
= urib
= rib_match_ipv4 (addr
, SAFI_UNICAST
, skip_bgp
, &u_rn
);
826 case MCAST_MIX_DISTANCE
:
827 mrib
= rib_match_ipv4 (addr
, SAFI_MULTICAST
, skip_bgp
, &m_rn
);
828 urib
= rib_match_ipv4 (addr
, SAFI_UNICAST
, skip_bgp
, &u_rn
);
830 rib
= urib
->distance
< mrib
->distance
? urib
: mrib
;
836 case MCAST_MIX_PFXLEN
:
837 mrib
= rib_match_ipv4 (addr
, SAFI_MULTICAST
, skip_bgp
, &m_rn
);
838 urib
= rib_match_ipv4 (addr
, SAFI_UNICAST
, skip_bgp
, &u_rn
);
840 rib
= u_rn
->p
.prefixlen
> m_rn
->p
.prefixlen
? urib
: mrib
;
849 *rn_out
= (rib
== mrib
) ? m_rn
: u_rn
;
851 if (IS_ZEBRA_DEBUG_RIB
)
854 inet_ntop (AF_INET
, &addr
, buf
, BUFSIZ
);
856 zlog_debug("%s: %s: found %s, using %s",
858 mrib
? (urib
? "MRIB+URIB" : "MRIB") :
859 urib
? "URIB" : "nothing",
860 rib
== urib
? "URIB" : rib
== mrib
? "MRIB" : "none");
866 multicast_mode_ipv4_set (enum multicast_mode mode
)
868 if (IS_ZEBRA_DEBUG_RIB
)
869 zlog_debug("%s: multicast lookup mode set (%d)", __func__
, mode
);
870 ipv4_multicast_mode
= mode
;
874 multicast_mode_ipv4_get (void)
876 return ipv4_multicast_mode
;
880 rib_lookup_ipv4 (struct prefix_ipv4
*p
, vrf_id_t vrf_id
)
882 struct route_table
*table
;
883 struct route_node
*rn
;
885 struct nexthop
*nexthop
, *tnexthop
;
889 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
893 rn
= route_node_lookup (table
, (struct prefix
*) p
);
895 /* No route for this prefix. */
900 route_unlock_node (rn
);
902 RNODE_FOREACH_RIB (rn
, match
)
904 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
906 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
913 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
916 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
917 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
924 * This clone function, unlike its original rib_lookup_ipv4(), checks
925 * if specified IPv4 route record (prefix/mask -> gate) exists in
926 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
930 * 0: exact match found
931 * 1: a match was found with a different gate
932 * 2: connected route found
933 * 3: no matches found
936 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
,
939 struct route_table
*table
;
940 struct route_node
*rn
;
942 struct nexthop
*nexthop
, *tnexthop
;
947 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
949 return ZEBRA_RIB_LOOKUP_ERROR
;
951 /* Scan the RIB table for exactly matching RIB entry. */
952 rn
= route_node_lookup (table
, (struct prefix
*) p
);
954 /* No route for this prefix. */
956 return ZEBRA_RIB_NOTFOUND
;
959 route_unlock_node (rn
);
961 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
962 RNODE_FOREACH_RIB (rn
, match
)
964 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
966 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
970 /* None such found :( */
972 return ZEBRA_RIB_NOTFOUND
;
974 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
975 return ZEBRA_RIB_FOUND_CONNECTED
;
977 /* Ok, we have a cood candidate, let's check it's nexthop list... */
979 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
980 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
983 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip (qgate
))
984 return ZEBRA_RIB_FOUND_EXACT
;
985 if (IS_ZEBRA_DEBUG_RIB
)
987 char gate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
988 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
989 inet_ntop (AF_INET
, &sockunion2ip(qgate
), qgate_buf
, INET_ADDRSTRLEN
);
990 zlog_debug ("%s: qgate == %s, %s == %s", __func__
,
991 qgate_buf
, recursing
? "rgate" : "gate", gate_buf
);
996 return ZEBRA_RIB_FOUND_NOGATE
;
998 return ZEBRA_RIB_NOTFOUND
;
1002 rib_match_ipv6 (struct in6_addr
*addr
, vrf_id_t vrf_id
)
1004 struct prefix_ipv6 p
;
1005 struct route_table
*table
;
1006 struct route_node
*rn
;
1008 struct nexthop
*newhop
, *tnewhop
;
1012 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
1016 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1017 p
.family
= AF_INET6
;
1018 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
1019 IPV6_ADDR_COPY (&p
.prefix
, addr
);
1021 rn
= route_node_match (table
, (struct prefix
*) &p
);
1025 route_unlock_node (rn
);
1027 /* Pick up selected route. */
1028 RNODE_FOREACH_RIB (rn
, match
)
1030 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
1032 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
1036 /* If there is no selected route or matched route is EGP, go up
1042 } while (rn
&& rn
->info
== NULL
);
1044 route_lock_node (rn
);
1048 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
1049 /* Directly point connected route. */
1053 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
1054 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
1063 #define RIB_SYSTEM_ROUTE(R) \
1064 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
1066 /* This function verifies reachability of one given nexthop, which can be
1067 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
1068 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
1069 * nexthop->ifindex will be updated appropriately as well.
1070 * An existing route map can turn (otherwise active) nexthop into inactive, but
1073 * The return value is the final value of 'ACTIVE' flag.
1077 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
1078 struct nexthop
*nexthop
, int set
)
1080 rib_table_info_t
*info
= rn
->table
->info
;
1081 struct interface
*ifp
;
1082 route_map_result_t ret
= RMAP_MATCH
;
1084 char buf
[INET6_ADDRSTRLEN
+1];
1086 if (rn
->p
.family
== AF_INET
)
1088 else if (rn
->p
.family
== AF_INET6
)
1092 switch (nexthop
->type
)
1094 case NEXTHOP_TYPE_IFINDEX
:
1095 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
1096 if (ifp
&& if_is_operative(ifp
))
1097 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1099 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1101 case NEXTHOP_TYPE_IPV4
:
1102 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1104 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
1105 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1107 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1109 case NEXTHOP_TYPE_IPV6
:
1111 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1112 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1114 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1116 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1117 /* RFC 5549, v4 prefix with v6 NH */
1118 if (rn
->p
.family
!= AF_INET
)
1120 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
1122 ifp
= if_lookup_by_index_vrf (nexthop
->ifindex
, rib
->vrf_id
);
1123 if (ifp
&& if_is_operative(ifp
))
1124 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1126 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1130 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1131 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1133 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1136 case NEXTHOP_TYPE_BLACKHOLE
:
1137 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1142 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1145 /* XXX: What exactly do those checks do? Do we support
1146 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
1147 if (RIB_SYSTEM_ROUTE(rib
) ||
1148 (family
== AFI_IP
&& rn
->p
.family
!= AF_INET
) ||
1149 (family
== AFI_IP6
&& rn
->p
.family
!= AF_INET6
))
1150 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1152 /* The original code didn't determine the family correctly
1153 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1154 * from the rib_table_info in those cases.
1155 * Possibly it may be better to use only the rib_table_info
1161 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
1163 /* It'll get set if required inside */
1164 ret
= zebra_route_map_check(family
, rib
->type
, &rn
->p
, nexthop
, rib
->vrf_id
,
1166 if (ret
== RMAP_DENYMATCH
)
1168 if (IS_ZEBRA_DEBUG_RIB
)
1170 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, sizeof (buf
));
1171 zlog_debug("%u:%s/%d: Filtering out with NH out %s due to route map",
1172 rib
->vrf_id
, buf
, rn
->p
.prefixlen
,
1173 ifindex2ifname_vrf (nexthop
->ifindex
, rib
->vrf_id
));
1175 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1177 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1180 /* Iterate over all nexthops of the given RIB entry and refresh their
1181 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1182 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1183 * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
1184 * transparently passed to nexthop_active_check().
1186 * Return value is the new number of active nexthops.
1190 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
1192 struct nexthop
*nexthop
;
1193 union g_addr prev_src
;
1194 unsigned int prev_active
, prev_index
, new_active
, old_num_nh
;
1196 old_num_nh
= rib
->nexthop_active_num
;
1198 rib
->nexthop_active_num
= 0;
1199 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
1201 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1203 /* No protocol daemon provides src and so we're skipping tracking it */
1204 prev_src
= nexthop
->rmap_src
;
1205 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1206 prev_index
= nexthop
->ifindex
;
1207 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
1208 rib
->nexthop_active_num
++;
1209 /* Don't allow src setting on IPv6 addr for now */
1210 if (prev_active
!= new_active
||
1211 prev_index
!= nexthop
->ifindex
||
1212 ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
&&
1213 nexthop
->type
< NEXTHOP_TYPE_IPV6
) &&
1214 prev_src
.ipv4
.s_addr
!= nexthop
->rmap_src
.ipv4
.s_addr
) ||
1215 ((nexthop
->type
>= NEXTHOP_TYPE_IPV6
&&
1216 nexthop
->type
< NEXTHOP_TYPE_BLACKHOLE
) &&
1217 !(IPV6_ADDR_SAME (&prev_src
.ipv6
, &nexthop
->rmap_src
.ipv6
))))
1219 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
1220 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1224 if (old_num_nh
!= rib
->nexthop_active_num
)
1225 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
1227 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
))
1229 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1232 return rib
->nexthop_active_num
;
1237 /* Update flag indicates whether this is a "replace" or not. Currently, this
1238 * is only used for IPv4.
1241 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
, int update
)
1244 struct nexthop
*nexthop
, *tnexthop
;
1245 rib_table_info_t
*info
= rn
->table
->info
;
1248 if (info
->safi
!= SAFI_UNICAST
)
1250 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1251 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1256 * Make sure we update the FPM any time we send new information to
1259 zfpm_trigger_update (rn
, "installing in kernel");
1260 switch (PREFIX_FAMILY (&rn
->p
))
1264 ret
= kernel_update_ipv4 (&rn
->p
, rib
);
1266 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
1270 ret
= kernel_update_ipv6 (&rn
->p
, rib
);
1272 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
1276 /* If install succeeds, update FIB flag for nexthops. */
1279 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1281 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
1284 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1285 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1287 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1294 /* Uninstall the route from kernel. */
1296 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
1299 struct nexthop
*nexthop
, *tnexthop
;
1300 rib_table_info_t
*info
= rn
->table
->info
;
1303 if (info
->safi
!= SAFI_UNICAST
)
1305 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1306 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1311 * Make sure we update the FPM any time we send new information to
1314 zfpm_trigger_update (rn
, "uninstalling from kernel");
1316 switch (PREFIX_FAMILY (&rn
->p
))
1319 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
1322 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
1326 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1327 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1332 /* Uninstall the route from kernel. */
1334 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
1336 rib_table_info_t
*info
= rn
->table
->info
;
1338 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1340 if (info
->safi
== SAFI_UNICAST
)
1341 zfpm_trigger_update (rn
, "rib_uninstall");
1343 redistribute_delete (&rn
->p
, rib
);
1344 if (! RIB_SYSTEM_ROUTE (rib
))
1345 rib_uninstall_kernel (rn
, rib
);
1346 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
1350 static void rib_unlink (struct route_node
*, struct rib
*);
1353 * rib_can_delete_dest
1355 * Returns TRUE if the given dest can be deleted from the table.
1358 rib_can_delete_dest (rib_dest_t
*dest
)
1366 * Don't delete the dest if we have to update the FPM about this
1369 if (CHECK_FLAG (dest
->flags
, RIB_DEST_UPDATE_FPM
) ||
1370 CHECK_FLAG (dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1379 * Garbage collect the rib dest corresponding to the given route node
1382 * Returns TRUE if the dest was deleted, FALSE otherwise.
1385 rib_gc_dest (struct route_node
*rn
)
1388 struct zebra_vrf
*zvrf
;
1390 dest
= rib_dest_from_rnode (rn
);
1394 if (!rib_can_delete_dest (dest
))
1397 zvrf
= rib_dest_vrf (dest
);
1398 if (IS_ZEBRA_DEBUG_RIB
)
1399 rnode_debug (rn
, zvrf
->vrf_id
, "removing dest from table");
1402 XFREE (MTYPE_RIB_DEST
, dest
);
1406 * Release the one reference that we keep on the route node.
1408 route_unlock_node (rn
);
1413 rib_process_add_route (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1416 char buf
[INET6_ADDRSTRLEN
];
1419 zfpm_trigger_update (rn
, "new route selected");
1421 /* Update real nexthop. This may actually determine if nexthop is active or not. */
1422 if (!nexthop_active_update (rn
, select
, 1))
1424 UNSET_FLAG(select
->flags
, ZEBRA_FLAG_CHANGED
);
1428 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1429 if (IS_ZEBRA_DEBUG_RIB
)
1431 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1432 zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)",
1433 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1436 if (!RIB_SYSTEM_ROUTE (select
))
1438 if (rib_install_kernel (rn
, select
, 0))
1441 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1442 zlog_warn ("%u:%s/%d: Route install failed",
1443 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
);
1447 /* Update for redistribution. */
1449 redistribute_update (&rn
->p
, select
, NULL
);
1450 UNSET_FLAG(select
->flags
, ZEBRA_FLAG_CHANGED
);
1454 rib_process_del_route (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1457 char buf
[INET6_ADDRSTRLEN
];
1459 zfpm_trigger_update (rn
, "removing existing route");
1461 /* Withdraw redistribute and uninstall from kernel. */
1462 if (IS_ZEBRA_DEBUG_RIB
)
1464 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1465 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)",
1466 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1469 redistribute_delete(&rn
->p
, fib
);
1470 if (!RIB_SYSTEM_ROUTE (fib
))
1471 rib_uninstall_kernel (rn
, fib
);
1473 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1475 /* Update nexthop for route, reset changed flag. */
1476 nexthop_active_update (rn
, fib
, 1);
1477 UNSET_FLAG(fib
->flags
, ZEBRA_FLAG_CHANGED
);
1481 rib_process_update_route (struct zebra_vrf
*zvrf
, struct route_node
*rn
,
1482 struct rib
*select
, struct rib
*fib
)
1484 char buf
[INET6_ADDRSTRLEN
];
1485 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1490 if (IS_ZEBRA_DEBUG_RIB
)
1491 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1494 * We have to install or update if a new route has been selected or
1495 * something has changed.
1497 if (select
!= fib
||
1498 CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
1500 zfpm_trigger_update (rn
, "updating existing route");
1502 /* Update the nexthop; we could determine here that nexthop is inactive. */
1503 if (nexthop_active_update (rn
, select
, 1))
1506 /* If nexthop is active, install the selected route, if appropriate. If
1507 * the install succeeds, cleanup flags for prior route, if different from
1512 if (IS_ZEBRA_DEBUG_RIB
)
1515 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) "
1516 "old %p (type %d)", zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
,
1517 rn
, select
, select
->type
, fib
, fib
->type
);
1519 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)",
1520 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1522 /* Non-system route should be installed. */
1523 if (!RIB_SYSTEM_ROUTE (select
))
1525 if (rib_install_kernel (rn
, select
, 1))
1528 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1529 zlog_warn ("%u:%s/%d: Route install failed",
1530 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
);
1534 /* If install succeeded or system route, cleanup flags for prior route. */
1535 if (installed
&& select
!= fib
)
1537 if (RIB_SYSTEM_ROUTE(select
))
1539 if (!RIB_SYSTEM_ROUTE (fib
))
1540 rib_uninstall_kernel (rn
, fib
);
1544 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1545 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1550 /* Update for redistribution. */
1553 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1554 redistribute_update (&rn
->p
, select
, (select
== fib
) ? NULL
: fib
);
1559 * If nexthop for selected route is not active or install failed, we
1560 * may need to uninstall and delete for redistribution.
1562 if (!nh_active
|| !installed
)
1566 if (IS_ZEBRA_DEBUG_RIB
)
1569 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1570 "old %p (type %d) - %s", zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
,
1571 rn
, select
, select
->type
, fib
, fib
->type
,
1572 nh_active
? "install failed" : "nexthop inactive");
1574 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) - %s",
1575 zvrf
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
,
1576 nh_active
? "install failed" : "nexthop inactive");
1579 del
= (select
== fib
) ? select
: fib
;
1581 redistribute_delete(&rn
->p
, del
);
1583 if (!RIB_SYSTEM_ROUTE (del
))
1584 rib_uninstall_kernel (rn
, del
);
1585 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1591 * Same route selected; check if in the FIB and if not, re-install. This
1592 * is housekeeping code to deal with race conditions in kernel with linux
1593 * netlink reporting interface up before IPv4 or IPv6 protocol is ready
1596 if (!RIB_SYSTEM_ROUTE (select
))
1600 for (ALL_NEXTHOPS_RO(select
->nexthop
, nexthop
, tnexthop
, recursing
))
1601 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1607 rib_install_kernel (rn
, select
, 0);
1611 /* Update prior route. */
1614 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1616 /* Set real nexthop. */
1617 nexthop_active_update (rn
, fib
, 1);
1618 UNSET_FLAG(fib
->flags
, ZEBRA_FLAG_CHANGED
);
1621 /* Clear changed flag. */
1622 UNSET_FLAG(select
->flags
, ZEBRA_FLAG_CHANGED
);
1625 /* Core function for processing routing information base. */
1627 rib_process (struct route_node
*rn
)
1631 struct rib
*fib
= NULL
;
1632 struct rib
*select
= NULL
;
1633 struct rib
*del
= NULL
;
1634 char buf
[INET6_ADDRSTRLEN
];
1636 struct zebra_vrf
*zvrf
= NULL
;
1637 vrf_id_t vrf_id
= VRF_UNKNOWN
;
1638 rib_table_info_t
*info
;
1641 info
= rn
->table
->info
;
1643 dest
= rib_dest_from_rnode (rn
);
1646 zvrf
= rib_dest_vrf (dest
);
1647 vrf_id
= zvrf
->vrf_id
;
1650 if (IS_ZEBRA_DEBUG_RIB
)
1651 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1653 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1654 zlog_debug ("%u:%s/%d: Processing rn %p", vrf_id
, buf
, rn
->p
.prefixlen
, rn
);
1656 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1658 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1659 zlog_debug ("%u:%s/%d: Examine rib %p (type %d) status %x flags %x "
1660 "dist %d metric %d",
1661 vrf_id
, buf
, rn
->p
.prefixlen
, rib
, rib
->type
, rib
->status
,
1662 rib
->flags
, rib
->distance
, rib
->metric
);
1664 UNSET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1666 /* Currently installed rib. */
1667 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1669 assert (fib
== NULL
);
1673 /* Unlock removed routes, so they'll be freed, bar the FIB entry,
1674 * which we need to do do further work with below.
1676 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1680 if (IS_ZEBRA_DEBUG_RIB
)
1681 rnode_debug (rn
, vrf_id
, "rn %p, removing rib %p",
1682 (void *)rn
, (void *)rib
);
1683 rib_unlink (rn
, rib
);
1691 /* Skip unreachable nexthop. */
1692 /* This first call to nexthop_active_update is merely to determine if
1693 * there's any change to nexthops associated with this RIB entry. Now,
1694 * rib_process() can be invoked due to an external event such as link
1695 * down or due to next-hop-tracking evaluation. In the latter case,
1696 * a decision has already been made that the NHs have changed. So, no
1697 * need to invoke a potentially expensive call again. Further, since
1698 * the change might be in a recursive NH which is not caught in
1699 * the nexthop_active_update() code. Thus, we might miss changes to
1702 if (!CHECK_FLAG(rib
->flags
, ZEBRA_FLAG_CHANGED
) &&
1703 ! nexthop_active_update (rn
, rib
, 0))
1705 if (rib
->type
== ZEBRA_ROUTE_TABLE
)
1707 /* This entry was denied by the 'ip protocol table' route-map, we
1708 * need to delete it */
1711 if (IS_ZEBRA_DEBUG_RIB
)
1712 zlog_debug ("%s: %s/%d: imported via import-table but denied "
1713 "by the ip protocol table route-map",
1714 __func__
, buf
, rn
->p
.prefixlen
);
1715 rib_unlink (rn
, rib
);
1724 if (info
->safi
== SAFI_MULTICAST
)
1727 /* Infinite distance. */
1728 if (rib
->distance
== DISTANCE_INFINITY
)
1730 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
1734 /* Newly selected rib, the common case. */
1741 /* filter route selection in following order:
1742 * - connected beats other types
1743 * - lower distance beats higher
1744 * - lower metric beats higher for equal distance
1745 * - last, hence oldest, route wins tie break.
1748 /* Connected routes. Pick the last connected
1749 * route of the set of lowest metric connected routes.
1751 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1753 if (select
->type
!= ZEBRA_ROUTE_CONNECT
1754 || rib
->metric
<= select
->metric
)
1756 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
);
1760 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
1763 else if (select
->type
== ZEBRA_ROUTE_CONNECT
)
1765 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
1769 /* higher distance loses */
1770 if (rib
->distance
> select
->distance
)
1772 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
1777 if (rib
->distance
< select
->distance
)
1779 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
);
1784 /* metric tie-breaks equal distance */
1785 if (rib
->metric
<= select
->metric
)
1787 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
);
1790 } /* RNODE_FOREACH_RIB_SAFE */
1792 /* After the cycle is finished, the following pointers will be set:
1793 * select --- the winner RIB entry, if any was found, otherwise NULL
1794 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1795 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1798 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
1799 zlog_debug ("%u:%s/%d: After processing: select %p fib %p del %p",
1800 vrf_id
, buf
, rn
->p
.prefixlen
, select
, fib
, del
);
1802 /* Same RIB entry is selected. Update FIB and finish. */
1803 if (select
&& select
== fib
)
1804 rib_process_update_route (zvrf
, rn
, select
, select
);
1805 else if (select
&& fib
)
1806 rib_process_update_route (zvrf
, rn
, select
, fib
);
1808 rib_process_add_route (zvrf
, rn
, select
);
1810 rib_process_del_route (zvrf
, rn
, fib
);
1813 if (select
&& select
== fib
)
1815 if (IS_ZEBRA_DEBUG_RIB
)
1816 rnode_debug (rn
, vrf_id
, "Updating existing route, select %p, fib %p",
1817 (void *)select
, (void *)fib
);
1818 if (CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
1820 if (info
->safi
== SAFI_UNICAST
)
1821 zfpm_trigger_update (rn
, "updating existing route");
1823 /* Set real nexthop. */
1824 /* Need to check if any NHs are active to clear the
1827 if (nexthop_active_update (rn
, select
, 1))
1829 if (IS_ZEBRA_DEBUG_RIB
)
1830 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d)",
1831 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1832 if (! RIB_SYSTEM_ROUTE (select
))
1834 /* Clear FIB flag if performing a replace, will get set again
1835 * as part of install.
1837 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1838 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1839 rib_install_kernel (rn
, select
, 1);
1842 /* assuming that the receiver knows how to dedup */
1843 redistribute_update (&rn
->p
, select
, NULL
);
1847 if (IS_ZEBRA_DEBUG_RIB
)
1848 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1849 "- nexthop inactive",
1850 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1852 /* Withdraw unreachable redistribute route */
1853 redistribute_delete(&rn
->p
, select
);
1855 /* Do the uninstall here, if not done earlier. */
1856 if (! RIB_SYSTEM_ROUTE (select
))
1857 rib_uninstall_kernel (rn
, select
);
1858 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1860 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
);
1862 else if (! RIB_SYSTEM_ROUTE (select
))
1864 /* Housekeeping code to deal with
1865 race conditions in kernel with linux
1866 netlink reporting interface up before IPv4 or IPv6 protocol
1867 is ready to add routes.
1868 This makes sure the routes are IN the kernel.
1871 for (ALL_NEXTHOPS_RO(select
->nexthop
, nexthop
, tnexthop
, recursing
))
1872 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1878 rib_install_kernel (rn
, select
, 0);
1883 /* At this point we either haven't found the best RIB entry or it is
1884 * different from what we currently intend to flag with SELECTED. In both
1885 * cases, if a RIB block is present in FIB, it should be withdrawn.
1889 if (IS_ZEBRA_DEBUG_RIB
)
1890 rnode_debug (rn
, vrf_id
, "Removing existing route, fib %p", (void *)fib
);
1892 if (info
->safi
== SAFI_UNICAST
)
1893 zfpm_trigger_update (rn
, "removing existing route");
1895 /* If there's no route to replace this with, withdraw redistribute and
1896 * uninstall from kernel.
1900 if (IS_ZEBRA_DEBUG_RIB
)
1901 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d)",
1902 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1904 redistribute_delete(&rn
->p
, fib
);
1905 if (! RIB_SYSTEM_ROUTE (fib
))
1906 rib_uninstall_kernel (rn
, fib
);
1909 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1911 /* Set real nexthop. */
1912 nexthop_active_update (rn
, fib
, 1);
1913 UNSET_FLAG(fib
->flags
, ZEBRA_FLAG_CHANGED
);
1916 /* Regardless of some RIB entry being SELECTED or not before, now we can
1917 * tell, that if a new winner exists, FIB is still not updated with this
1918 * data, but ready to be.
1922 if (IS_ZEBRA_DEBUG_RIB
)
1923 rnode_debug (rn
, "Adding route, select %p", (void *)select
);
1925 if (info
->safi
== SAFI_UNICAST
)
1926 zfpm_trigger_update (rn
, "new route selected");
1928 /* Set real nexthop. */
1929 if (nexthop_active_update (rn
, select
, 1))
1931 if (IS_ZEBRA_DEBUG_RIB
)
1934 zlog_debug ("%u:%s/%d: Updating route rn %p, rib %p (type %d) "
1935 "old %p (type %d)", vrf_id
, buf
, rn
->p
.prefixlen
, rn
,
1936 select
, select
->type
, fib
, fib
->type
);
1938 zlog_debug ("%u:%s/%d: Adding route rn %p, rib %p (type %d)",
1939 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, select
, select
->type
);
1942 if (! RIB_SYSTEM_ROUTE (select
))
1944 /* Clear FIB flag if performing a replace, will get set again
1945 * as part of install.
1949 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1950 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1952 rib_install_kernel (rn
, select
, fib
? 1 : 0);
1956 /* Uninstall prior route here, if needed. */
1957 if (fib
&& !RIB_SYSTEM_ROUTE (fib
))
1958 rib_uninstall_kernel (rn
, fib
);
1961 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1962 /* Unconditionally announce, this part is exercised by new routes */
1963 /* If we cannot add, for example route added is learnt by the */
1964 /* protocol we're trying to redistribute to, delete the redist */
1965 /* This is notified by setting the is_update to 1 */
1966 redistribute_update (&rn
->p
, select
, fib
);
1970 /* Uninstall prior route here and do redist delete, if needed. */
1973 if (IS_ZEBRA_DEBUG_RIB
)
1974 zlog_debug ("%u:%s/%d: Deleting route rn %p, rib %p (type %d) "
1975 "- nexthop inactive",
1976 vrf_id
, buf
, rn
->p
.prefixlen
, rn
, fib
, fib
->type
);
1978 if (!RIB_SYSTEM_ROUTE (fib
))
1979 rib_uninstall_kernel (rn
, fib
);
1980 redistribute_delete(&rn
->p
, fib
);
1983 UNSET_FLAG(select
->flags
, ZEBRA_FLAG_CHANGED
);
1987 /* FIB route was removed, should be deleted */
1990 if (IS_ZEBRA_DEBUG_RIB
)
1991 rnode_debug (rn
, vrf_id
, "Deleting fib %p, rn %p", (void *)del
, (void *)rn
);
1992 rib_unlink (rn
, del
);
1996 * Check if the dest can be deleted now.
2001 /* Take a list of route_node structs and return 1, if there was a record
2002 * picked from it and processed by rib_process(). Don't process more,
2003 * than one RN record; operate only in the specified sub-queue.
2006 process_subq (struct list
* subq
, u_char qindex
)
2008 struct listnode
*lnode
= listhead (subq
);
2009 struct route_node
*rnode
;
2010 char buf
[INET6_ADDRSTRLEN
];
2012 struct zebra_vrf
*zvrf
= NULL
;
2017 rnode
= listgetdata (lnode
);
2018 dest
= rib_dest_from_rnode (rnode
);
2020 zvrf
= rib_dest_vrf (dest
);
2022 rib_process (rnode
);
2024 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2026 inet_ntop (rnode
->p
.family
, &rnode
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2027 zlog_debug ("%u:%s/%d: rn %p dequeued from sub-queue %u",
2028 zvrf
? zvrf
->vrf_id
: 0, buf
, rnode
->p
.prefixlen
, rnode
, qindex
);
2032 UNSET_FLAG (rib_dest_from_rnode (rnode
)->flags
, RIB_ROUTE_QUEUED (qindex
));
2037 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2038 __func__
, rnode
, rnode
->lock
);
2039 zlog_backtrace(LOG_DEBUG
);
2042 route_unlock_node (rnode
);
2043 list_delete_node (subq
, lnode
);
2048 * All meta queues have been processed. Trigger next-hop evaluation.
2051 meta_queue_process_complete (struct work_queue
*dummy
)
2054 struct zebra_vrf
*zvrf
;
2056 /* Evaluate nexthops for those VRFs which underwent route processing. This
2057 * should limit the evaluation to the necessary VRFs in most common
2060 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
2062 if (((zvrf
= vrf_iter2info (iter
)) != NULL
) &&
2063 (zvrf
->flags
& ZEBRA_VRF_RIB_SCHEDULED
))
2065 zvrf
->flags
&= ~ZEBRA_VRF_RIB_SCHEDULED
;
2066 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2067 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
2068 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
2069 zebra_evaluate_rnh(zvrf
->vrf_id
, AF_INET6
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
2074 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
2075 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
2076 * is pointed to the meta queue structure.
2078 static wq_item_status
2079 meta_queue_process (struct work_queue
*dummy
, void *data
)
2081 struct meta_queue
* mq
= data
;
2084 for (i
= 0; i
< MQ_SIZE
; i
++)
2085 if (process_subq (mq
->subq
[i
], i
))
2090 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
2094 * Map from rib types to queue type (priority) in meta queue
2096 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
2097 [ZEBRA_ROUTE_SYSTEM
] = 4,
2098 [ZEBRA_ROUTE_KERNEL
] = 0,
2099 [ZEBRA_ROUTE_CONNECT
] = 0,
2100 [ZEBRA_ROUTE_STATIC
] = 1,
2101 [ZEBRA_ROUTE_RIP
] = 2,
2102 [ZEBRA_ROUTE_RIPNG
] = 2,
2103 [ZEBRA_ROUTE_OSPF
] = 2,
2104 [ZEBRA_ROUTE_OSPF6
] = 2,
2105 [ZEBRA_ROUTE_ISIS
] = 2,
2106 [ZEBRA_ROUTE_BGP
] = 3,
2107 [ZEBRA_ROUTE_HSLS
] = 4,
2108 [ZEBRA_ROUTE_TABLE
] = 1,
2111 /* Look into the RN and queue it into one or more priority queues,
2112 * increasing the size for each data push done.
2115 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
2119 RNODE_FOREACH_RIB (rn
, rib
)
2121 u_char qindex
= meta_queue_map
[rib
->type
];
2122 struct zebra_vrf
*zvrf
;
2124 /* Invariant: at this point we always have rn->info set. */
2125 if (CHECK_FLAG (rib_dest_from_rnode (rn
)->flags
,
2126 RIB_ROUTE_QUEUED (qindex
)))
2128 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2129 rnode_debug (rn
, rib
->vrf_id
, "rn %p is already queued in sub-queue %u",
2130 (void *)rn
, qindex
);
2134 SET_FLAG (rib_dest_from_rnode (rn
)->flags
, RIB_ROUTE_QUEUED (qindex
));
2135 listnode_add (mq
->subq
[qindex
], rn
);
2136 route_lock_node (rn
);
2139 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2140 rnode_debug (rn
, rib
->vrf_id
, "queued rn %p into sub-queue %u",
2141 (void *)rn
, qindex
);
2143 zvrf
= zebra_vrf_lookup (rib
->vrf_id
);
2145 zvrf
->flags
|= ZEBRA_VRF_RIB_SCHEDULED
;
2149 /* Add route_node to work queue and schedule processing */
2151 rib_queue_add (struct route_node
*rn
)
2155 /* Pointless to queue a route_node with no RIB entries to add or remove */
2156 if (!rnode_to_ribs (rn
))
2158 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
2159 __func__
, (void *)rn
, rn
->lock
);
2160 zlog_backtrace(LOG_DEBUG
);
2164 if (zebrad
.ribq
== NULL
)
2166 zlog_err ("%s: work_queue does not exist!", __func__
);
2171 * The RIB queue should normally be either empty or holding the only
2172 * work_queue_item element. In the latter case this element would
2173 * hold a pointer to the meta queue structure, which must be used to
2174 * actually queue the route nodes to process. So create the MQ
2175 * holder, if necessary, then push the work into it in any case.
2176 * This semantics was introduced after 0.99.9 release.
2178 if (!zebrad
.ribq
->items
->count
)
2179 work_queue_add (zebrad
.ribq
, zebrad
.mq
);
2181 rib_meta_queue_add (zebrad
.mq
, rn
);
2186 /* Create new meta queue.
2187 A destructor function doesn't seem to be necessary here.
2189 static struct meta_queue
*
2190 meta_queue_new (void)
2192 struct meta_queue
*new;
2195 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
2198 for (i
= 0; i
< MQ_SIZE
; i
++)
2200 new->subq
[i
] = list_new ();
2201 assert(new->subq
[i
]);
2207 /* initialise zebra rib work queue */
2209 rib_queue_init (struct zebra_t
*zebra
)
2213 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
2214 "route_node processing")))
2216 zlog_err ("%s: could not initialise work queue!", __func__
);
2220 /* fill in the work queue spec */
2221 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
2222 zebra
->ribq
->spec
.errorfunc
= NULL
;
2223 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
2224 /* XXX: TODO: These should be runtime configurable via vty */
2225 zebra
->ribq
->spec
.max_retries
= 3;
2226 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
2228 if (!(zebra
->mq
= meta_queue_new ()))
2230 zlog_err ("%s: could not initialise meta queue!", __func__
);
2236 /* RIB updates are processed via a queue of pointers to route_nodes.
2238 * The queue length is bounded by the maximal size of the routing table,
2239 * as a route_node will not be requeued, if already queued.
2241 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
2242 * state, or static_install_route (when an existing RIB is updated)
2243 * and then submit route_node to queue for best-path selection later.
2244 * Order of add/delete state changes are preserved for any given RIB.
2246 * Deleted RIBs are reaped during best-path selection.
2249 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
2250 * |-------->| | best RIB, if required
2252 * static_install->|->rib_addqueue...... -> rib_process
2254 * |-------->| |-> rib_unlink
2255 * |-> set RIB_ENTRY_REMOVE |
2256 * rib_delnode (RIB freed)
2258 * The 'info' pointer of a route_node points to a rib_dest_t
2259 * ('dest'). Queueing state for a route_node is kept on the dest. The
2260 * dest is created on-demand by rib_link() and is kept around at least
2261 * as long as there are ribs hanging off it (@see rib_gc_dest()).
2263 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
2265 * - route_nodes: refcounted by:
2266 * - dest attached to route_node:
2267 * - managed by: rib_link/rib_gc_dest
2268 * - route_node processing queue
2269 * - managed by: rib_addqueue, rib_process.
2273 /* Add RIB to head of the route node. */
2275 rib_link (struct route_node
*rn
, struct rib
*rib
, int process
)
2280 const char *rmap_name
;
2284 dest
= rib_dest_from_rnode (rn
);
2287 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2288 rnode_debug (rn
, rib
->vrf_id
, "rn %p adding dest", rn
);
2290 dest
= XCALLOC (MTYPE_RIB_DEST
, sizeof (rib_dest_t
));
2291 route_lock_node (rn
); /* rn route table reference */
2296 head
= dest
->routes
;
2304 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2305 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2306 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2308 rmap_name
= zebra_get_import_table_route_map (afi
, rib
->table
);
2309 zebra_add_import_table_entry(rn
, rib
, rmap_name
);
2317 rib_addnode (struct route_node
*rn
, struct rib
*rib
, int process
)
2319 /* RIB node has been un-removed before route-node is processed.
2320 * route_node must hence already be on the queue for processing..
2322 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2324 if (IS_ZEBRA_DEBUG_RIB
)
2325 rnode_debug (rn
, rib
->vrf_id
, "rn %p, un-removed rib %p", (void *)rn
, (void *)rib
);
2327 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2330 rib_link (rn
, rib
, process
);
2336 * Detach a rib structure from a route_node.
2338 * Note that a call to rib_unlink() should be followed by a call to
2339 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2340 * longer required to be deleted.
2343 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
2349 if (IS_ZEBRA_DEBUG_RIB
)
2350 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p", (void *)rn
, (void *)rib
);
2352 dest
= rib_dest_from_rnode (rn
);
2355 rib
->next
->prev
= rib
->prev
;
2358 rib
->prev
->next
= rib
->next
;
2361 dest
->routes
= rib
->next
;
2364 /* free RIB and nexthops */
2365 zebra_deregister_rnh_static_nexthops (rib
->vrf_id
, rib
->nexthop
, rn
);
2366 nexthops_free(rib
->nexthop
);
2367 XFREE (MTYPE_RIB
, rib
);
2372 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
2376 if (IS_ZEBRA_DEBUG_RIB
)
2377 rnode_debug (rn
, rib
->vrf_id
, "rn %p, rib %p, removing", (void *)rn
, (void *)rib
);
2378 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2380 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2381 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2382 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2384 zebra_del_import_table_entry(rn
, rib
);
2385 /* Just clean up if non main table */
2386 if (IS_ZEBRA_DEBUG_RIB
)
2388 char buf
[INET6_ADDRSTRLEN
];
2389 if (IS_ZEBRA_DEBUG_RIB
)
2391 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2392 zlog_debug ("%u:%s/%d: Freeing route rn %p, rib %p (type %d)",
2393 rib
->vrf_id
, buf
, rn
->p
.prefixlen
, rn
, rib
, rib
->type
);
2397 rib_unlink(rn
, rib
);
2406 rib_add_ipv4 (int type
, u_short instance
, int flags
, struct prefix_ipv4
*p
,
2407 struct in_addr
*gate
, struct in_addr
*src
,
2408 unsigned int ifindex
, vrf_id_t vrf_id
, u_int32_t table_id
,
2409 u_int32_t metric
, u_char distance
, safi_t safi
)
2412 struct rib
*same
= NULL
;
2413 struct route_table
*table
;
2414 struct route_node
*rn
;
2415 struct nexthop
*nexthop
;
2418 table
= zebra_vrf_table_with_table_id (AFI_IP
, safi
, vrf_id
, table_id
);
2422 /* Make it sure prefixlen is applied to the prefix. */
2423 apply_mask_ipv4 (p
);
2425 /* Set default distance by route type. */
2428 if ((unsigned)type
>= array_size(route_info
))
2431 distance
= route_info
[type
].distance
;
2433 /* iBGP distance is 200. */
2434 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2438 /* Lookup route node.*/
2439 rn
= route_node_get (table
, (struct prefix
*) p
);
2441 /* If same type of route are installed, treat it as a implicit
2443 RNODE_FOREACH_RIB (rn
, rib
)
2445 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2448 if (rib
->type
!= type
)
2450 if (rib
->instance
!= instance
)
2453 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2458 /* Duplicate connected route comes in. */
2459 else if ((nexthop
= rib
->nexthop
) &&
2460 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2461 nexthop
->ifindex
== ifindex
&&
2462 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2469 /* Allocate new rib structure. */
2470 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2472 rib
->instance
= instance
;
2473 rib
->distance
= distance
;
2475 rib
->metric
= metric
;
2476 rib
->table
= table_id
;
2477 rib
->vrf_id
= vrf_id
;
2478 rib
->nexthop_num
= 0;
2479 rib
->uptime
= time (NULL
);
2481 /* Nexthop settings. */
2485 rib_nexthop_ipv4_ifindex_add (rib
, gate
, src
, ifindex
);
2487 rib_nexthop_ipv4_add (rib
, gate
, src
);
2490 rib_nexthop_ifindex_add (rib
, ifindex
);
2492 /* If this route is kernel route, set FIB flag to the route. */
2493 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2494 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2495 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2497 /* Link new rib to node.*/
2498 if (IS_ZEBRA_DEBUG_RIB
)
2500 char buf
[INET6_ADDRSTRLEN
];
2501 if (IS_ZEBRA_DEBUG_RIB
)
2503 inet_ntop (p
->family
, &p
->prefix
, buf
, INET6_ADDRSTRLEN
);
2504 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2506 vrf_id
, buf
, p
->prefixlen
, (void *)rn
, (void *)rib
, rib
->type
, (void *)same
);
2509 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2510 rib_dump ((struct prefix
*)p
, rib
);
2512 rib_addnode (rn
, rib
, 1);
2514 /* Free implicit route.*/
2516 rib_delnode (rn
, same
);
2518 route_unlock_node (rn
);
2522 /* This function dumps the contents of a given RIB entry into
2523 * standard debug log. Calling function name and IP prefix in
2524 * question are passed as 1st and 2nd arguments.
2527 void _rib_dump (const char * func
,
2528 union prefix46constptr pp
, const struct rib
* rib
)
2530 const struct prefix
*p
= pp
.p
;
2531 char straddr
[PREFIX_STRLEN
];
2532 struct nexthop
*nexthop
, *tnexthop
;
2535 zlog_debug ("%s: dumping RIB entry %p for %s vrf %u", func
, (const void *)rib
,
2536 prefix2str(pp
, straddr
, sizeof(straddr
)), rib
->vrf_id
);
2539 "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d",
2542 (unsigned long) rib
->uptime
,
2549 "%s: metric == %u, distance == %u, flags == %u, status == %u",
2558 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
2561 rib
->nexthop_active_num
,
2562 rib
->nexthop_fib_num
2565 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2567 inet_ntop (p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2570 "%s: %s %s with flags %s%s%s",
2572 (recursing
? " NH" : "NH"),
2574 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
2575 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
2576 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
2579 zlog_debug ("%s: dump complete", func
);
2582 /* This is an exported helper to rtm_read() to dump the strange
2583 * RIB entry found by rib_lookup_ipv4_route()
2586 void rib_lookup_and_dump (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2588 struct route_table
*table
;
2589 struct route_node
*rn
;
2591 char prefix_buf
[INET_ADDRSTRLEN
];
2594 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2597 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2601 /* Scan the RIB table for exactly matching RIB entry. */
2602 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2604 /* No route for this prefix. */
2607 zlog_debug ("%s: lookup failed for %s", __func__
,
2608 prefix2str((struct prefix
*) p
, prefix_buf
, sizeof(prefix_buf
)));
2613 route_unlock_node (rn
);
2616 RNODE_FOREACH_RIB (rn
, rib
)
2620 "%s: rn %p, rib %p: %s, %s",
2624 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
2625 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
2631 /* Check if requested address assignment will fail due to another
2632 * route being installed by zebra in FIB already. Take necessary
2633 * actions, if needed: remove such a route from FIB and deSELECT
2634 * corresponding RIB entry. Then put affected RN into RIBQ head.
2636 void rib_lookup_and_pushup (struct prefix_ipv4
* p
, vrf_id_t vrf_id
)
2638 struct route_table
*table
;
2639 struct route_node
*rn
;
2641 unsigned changed
= 0;
2643 if (NULL
== (table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
)))
2645 zlog_err ("%s: zebra_vrf_table() returned NULL", __func__
);
2649 /* No matches would be the simplest case. */
2650 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
2654 route_unlock_node (rn
);
2656 /* Check all RIB entries. In case any changes have to be done, requeue
2657 * the RN into RIBQ head. If the routing message about the new connected
2658 * route (generated by the IP address we are going to assign very soon)
2659 * comes before the RIBQ is processed, the new RIB entry will join
2660 * RIBQ record already on head. This is necessary for proper revalidation
2661 * of the rest of the RIB.
2663 RNODE_FOREACH_RIB (rn
, rib
)
2665 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) &&
2666 ! RIB_SYSTEM_ROUTE (rib
))
2669 if (IS_ZEBRA_DEBUG_RIB
)
2671 char buf
[PREFIX_STRLEN
];
2672 zlog_debug ("%u:%s: freeing way for connected prefix",
2673 rib
->vrf_id
, prefix2str(&rn
->p
, buf
, sizeof(buf
)));
2674 rib_dump (&rn
->p
, rib
);
2676 rib_uninstall (rn
, rib
);
2684 rib_add_ipv4_multipath (struct prefix_ipv4
*p
, struct rib
*rib
, safi_t safi
)
2686 struct route_table
*table
;
2687 struct route_node
*rn
;
2689 struct nexthop
*nexthop
;
2693 table
= zebra_vrf_table_with_table_id (AFI_IP
, safi
, rib
->vrf_id
, rib
->table
);
2697 /* Make it sure prefixlen is applied to the prefix. */
2698 apply_mask_ipv4 (p
);
2700 /* Set default distance by route type. */
2701 if (rib
->distance
== 0)
2703 rib
->distance
= route_info
[rib
->type
].distance
;
2705 /* iBGP distance is 200. */
2706 if (rib
->type
== ZEBRA_ROUTE_BGP
2707 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
2708 rib
->distance
= 200;
2711 /* Lookup route node.*/
2712 rn
= route_node_get (table
, (struct prefix
*) p
);
2714 /* If same type of route are installed, treat it as a implicit
2716 RNODE_FOREACH_RIB (rn
, same
)
2718 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
2721 if (same
->type
== rib
->type
&& same
->instance
== rib
->instance
2722 && same
->table
== rib
->table
2723 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
2727 /* If this route is kernel route, set FIB flag to the route. */
2728 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
2729 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2730 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2732 /* Link new rib to node.*/
2733 if (IS_ZEBRA_DEBUG_RIB
)
2735 char buf
[INET6_ADDRSTRLEN
];
2736 if (IS_ZEBRA_DEBUG_RIB
)
2738 inet_ntop (p
->family
, &p
->prefix
, buf
, INET6_ADDRSTRLEN
);
2739 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
2741 rib
->vrf_id
, buf
, p
->prefixlen
, (void *)rn
,
2742 (void *)rib
, rib
->type
, (void *)same
);
2745 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
2746 rib_dump ((struct prefix
*)p
, rib
);
2748 rib_addnode (rn
, rib
, 1);
2751 /* Free implicit route.*/
2754 rib_delnode (rn
, same
);
2758 route_unlock_node (rn
);
2762 /* XXX factor with rib_delete_ipv6 */
2764 rib_delete_ipv4 (int type
, u_short instance
, int flags
, struct prefix_ipv4
*p
,
2765 struct in_addr
*gate
, unsigned int ifindex
, vrf_id_t vrf_id
,
2766 u_int32_t table_id
, safi_t safi
)
2768 struct route_table
*table
;
2769 struct route_node
*rn
;
2771 struct rib
*fib
= NULL
;
2772 struct rib
*same
= NULL
;
2773 struct nexthop
*nexthop
, *tnexthop
;
2775 char buf1
[PREFIX_STRLEN
];
2776 char buf2
[INET6_ADDRSTRLEN
];
2779 table
= zebra_vrf_table_with_table_id (AFI_IP
, safi
, vrf_id
, table_id
);
2784 apply_mask_ipv4 (p
);
2786 /* Lookup route node. */
2787 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2790 if (IS_ZEBRA_DEBUG_RIB
)
2791 zlog_debug ("%u:%s: doesn't exist in rib",
2792 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)));
2793 return ZEBRA_ERR_RTNOEXIST
;
2796 /* Lookup same type route. */
2797 RNODE_FOREACH_RIB (rn
, rib
)
2799 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2802 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2805 if (rib
->type
!= type
)
2807 if (rib
->instance
!= instance
)
2809 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2810 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2812 if (nexthop
->ifindex
!= ifindex
)
2817 route_unlock_node (rn
);
2818 route_unlock_node (rn
);
2824 /* Make sure that the route found has the same gateway. */
2832 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2833 if (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
))
2842 /* If same type of route can't be found and this message is from
2846 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
&&
2847 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
))
2849 if (IS_ZEBRA_DEBUG_RIB
)
2851 zlog_debug ("%u:%s/%d: rn %p, rib %p (type %d) was deleted "
2852 "from kernel, adding",
2853 vrf_id
, inet_ntop (p
->family
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2854 p
->prefixlen
, rn
, fib
, fib
->type
);
2859 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2860 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2862 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
2866 /* This means someone else, other than Zebra, has deleted
2867 * a Zebra router from the kernel. We will add it back */
2868 rib_install_kernel(rn
, fib
, 0);
2873 if (IS_ZEBRA_DEBUG_RIB
)
2876 zlog_debug ("%u:%s: via %s ifindex %d type %d "
2877 "doesn't exist in rib",
2878 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)),
2879 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
2883 zlog_debug ("%u:%s: ifindex %d type %d doesn't exist in rib",
2884 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)),
2888 route_unlock_node (rn
);
2889 return ZEBRA_ERR_RTNOEXIST
;
2894 rib_delnode (rn
, same
);
2896 route_unlock_node (rn
);
2900 /* Install static route into rib. */
2902 static_install_route (afi_t afi
, safi_t safi
, struct prefix
*p
, struct static_route
*si
)
2905 struct route_node
*rn
;
2906 struct route_table
*table
;
2910 table
= zebra_vrf_table (afi
, safi
, si
->vrf_id
);
2914 /* Lookup existing route */
2915 rn
= route_node_get (table
, p
);
2916 RNODE_FOREACH_RIB (rn
, rib
)
2918 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2921 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2927 /* if tag value changed , update old value in RIB */
2928 if (rib
->tag
!= si
->tag
)
2931 /* Same distance static route is there. Update it with new
2933 route_unlock_node (rn
);
2936 case STATIC_IPV4_GATEWAY
:
2937 rib_nexthop_ipv4_add (rib
, &si
->addr
.ipv4
, NULL
);
2938 nh_p
.family
= AF_INET
;
2939 nh_p
.prefixlen
= IPV4_MAX_BITLEN
;
2940 nh_p
.u
.prefix4
= si
->addr
.ipv4
;
2941 zebra_register_rnh_static_nh(si
->vrf_id
, &nh_p
, rn
);
2943 case STATIC_IFINDEX
:
2944 rib_nexthop_ifindex_add (rib
, si
->ifindex
);
2946 case STATIC_IPV4_BLACKHOLE
:
2947 rib_nexthop_blackhole_add (rib
);
2949 case STATIC_IPV6_GATEWAY
:
2950 rib_nexthop_ipv6_add (rib
, &si
->addr
.ipv6
);
2951 nh_p
.family
= AF_INET6
;
2952 nh_p
.prefixlen
= IPV6_MAX_BITLEN
;
2953 nh_p
.u
.prefix6
= si
->addr
.ipv6
;
2954 zebra_register_rnh_static_nh(si
->vrf_id
, &nh_p
, rn
);
2956 case STATIC_IPV6_GATEWAY_IFINDEX
:
2957 rib_nexthop_ipv6_ifindex_add (rib
, &si
->addr
.ipv6
, si
->ifindex
);
2961 if (IS_ZEBRA_DEBUG_RIB
)
2963 char buf
[INET6_ADDRSTRLEN
];
2964 if (IS_ZEBRA_DEBUG_RIB
)
2966 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2967 zlog_debug ("%u:%s/%d: Modifying route rn %p, rib %p (type %d)",
2968 si
->vrf_id
, buf
, p
->prefixlen
, rn
, rib
, rib
->type
);
2971 /* Schedule route for processing or invoke NHT, as appropriate. */
2972 if (si
->type
== STATIC_IPV4_GATEWAY
||
2973 si
->type
== STATIC_IPV6_GATEWAY
)
2974 zebra_evaluate_rnh(si
->vrf_id
, nh_p
.family
, 1, RNH_NEXTHOP_TYPE
, &nh_p
);
2980 /* This is new static route. */
2981 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2983 rib
->type
= ZEBRA_ROUTE_STATIC
;
2985 rib
->distance
= si
->distance
;
2987 rib
->vrf_id
= si
->vrf_id
;
2988 rib
->table
= si
->vrf_id
? (zebra_vrf_lookup(si
->vrf_id
))->table_id
: zebrad
.rtm_table_default
;
2989 rib
->nexthop_num
= 0;
2994 case STATIC_IPV4_GATEWAY
:
2995 rib_nexthop_ipv4_add (rib
, &si
->addr
.ipv4
, NULL
);
2996 nh_p
.family
= AF_INET
;
2997 nh_p
.prefixlen
= IPV4_MAX_BITLEN
;
2998 nh_p
.u
.prefix4
= si
->addr
.ipv4
;
2999 zebra_register_rnh_static_nh(si
->vrf_id
, &nh_p
, rn
);
3001 case STATIC_IFINDEX
:
3002 rib_nexthop_ifindex_add (rib
, si
->ifindex
);
3004 case STATIC_IPV4_BLACKHOLE
:
3005 rib_nexthop_blackhole_add (rib
);
3007 case STATIC_IPV6_GATEWAY
:
3008 rib_nexthop_ipv6_add (rib
, &si
->addr
.ipv6
);
3009 nh_p
.family
= AF_INET6
;
3010 nh_p
.prefixlen
= IPV6_MAX_BITLEN
;
3011 nh_p
.u
.prefix6
= si
->addr
.ipv6
;
3012 zebra_register_rnh_static_nh(si
->vrf_id
, &nh_p
, rn
);
3014 case STATIC_IPV6_GATEWAY_IFINDEX
:
3015 rib_nexthop_ipv6_ifindex_add (rib
, &si
->addr
.ipv6
, si
->ifindex
);
3019 /* Save the flags of this static routes (reject, blackhole) */
3020 rib
->flags
= si
->flags
;
3022 if (IS_ZEBRA_DEBUG_RIB
)
3024 char buf
[INET6_ADDRSTRLEN
];
3025 if (IS_ZEBRA_DEBUG_RIB
)
3027 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
3028 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d)",
3029 si
->vrf_id
, buf
, p
->prefixlen
, rn
, rib
, rib
->type
);
3032 /* Link this rib to the tree. Schedule for processing or invoke NHT,
3035 if (si
->type
== STATIC_IPV4_GATEWAY
||
3036 si
->type
== STATIC_IPV6_GATEWAY
)
3038 rib_addnode (rn
, rib
, 0);
3039 zebra_evaluate_rnh(si
->vrf_id
, nh_p
.family
, 1, RNH_NEXTHOP_TYPE
, &nh_p
);
3042 rib_addnode (rn
, rib
, 1);
3047 static_nexthop_same (struct nexthop
*nexthop
, struct static_route
*si
)
3049 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
3050 && si
->type
== STATIC_IPV4_GATEWAY
3051 && IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &si
->addr
.ipv4
))
3053 if (nexthop
->type
== NEXTHOP_TYPE_IFINDEX
3054 && si
->type
== STATIC_IFINDEX
3055 && nexthop
->ifindex
== si
->ifindex
)
3057 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
3058 && si
->type
== STATIC_IPV4_BLACKHOLE
)
3060 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
3061 && si
->type
== STATIC_IPV6_GATEWAY
3062 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->addr
.ipv6
))
3064 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
3065 && si
->type
== STATIC_IPV6_GATEWAY_IFINDEX
3066 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->addr
.ipv6
)
3067 && nexthop
->ifindex
== si
->ifindex
)
3072 /* Uninstall static route from RIB. */
3074 static_uninstall_route (afi_t afi
, safi_t safi
, struct prefix
*p
, struct static_route
*si
)
3076 struct route_node
*rn
;
3078 struct nexthop
*nexthop
;
3079 struct route_table
*table
;
3083 table
= zebra_vrf_table (afi
, safi
, si
->vrf_id
);
3087 /* Lookup existing route with type and distance. */
3088 rn
= route_node_lookup (table
, p
);
3092 RNODE_FOREACH_RIB (rn
, rib
)
3094 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3097 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
&&
3098 rib
->tag
== si
->tag
)
3104 route_unlock_node (rn
);
3108 /* Lookup nexthop. */
3109 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
3110 if (static_nexthop_same (nexthop
, si
))
3113 /* Can't find nexthop. */
3116 route_unlock_node (rn
);
3120 /* Check nexthop. */
3121 if (rib
->nexthop_num
== 1)
3122 rib_delnode (rn
, rib
);
3125 /* Mark this nexthop as inactive and reinstall the route. Then, delete
3126 * the nexthop. There is no need to re-evaluate the route for this
3129 if (IS_ZEBRA_DEBUG_RIB
)
3131 char buf
[INET6_ADDRSTRLEN
];
3132 if (IS_ZEBRA_DEBUG_RIB
)
3134 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
3135 zlog_debug ("%u:%s/%d: Modifying route rn %p, rib %p (type %d)",
3136 si
->vrf_id
, buf
, p
->prefixlen
, rn
, rib
, rib
->type
);
3139 UNSET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
3140 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3142 /* If there are other active nexthops, do an update. */
3143 if (rib
->nexthop_active_num
> 1)
3145 rib_install_kernel (rn
, rib
, 1);
3146 redistribute_update (&rn
->p
, rib
, NULL
);
3150 redistribute_delete (&rn
->p
, rib
);
3151 rib_uninstall_kernel (rn
, rib
);
3157 /* Delete the nexthop and dereg from NHT */
3158 nh_p
.family
= AF_INET
;
3159 nh_p
.prefixlen
= IPV4_MAX_BITLEN
;
3160 nh_p
.u
.prefix4
= nexthop
->gate
.ipv4
;
3164 nh_p
.family
= AF_INET6
;
3165 nh_p
.prefixlen
= IPV6_MAX_BITLEN
;
3166 nh_p
.u
.prefix6
= nexthop
->gate
.ipv6
;
3168 rib_nexthop_delete (rib
, nexthop
);
3169 zebra_deregister_rnh_static_nh(si
->vrf_id
, &nh_p
, rn
);
3170 nexthop_free (nexthop
);
3173 route_unlock_node (rn
);
3177 static_add_ipv4 (safi_t safi
, struct prefix
*p
, struct in_addr
*gate
, unsigned int ifindex
,
3178 const char *ifname
, u_char flags
, u_short tag
,
3179 u_char distance
, struct zebra_vrf
*zvrf
)
3182 struct route_node
*rn
;
3183 struct static_route
*si
;
3184 struct static_route
*pp
;
3185 struct static_route
*cp
;
3186 struct static_route
*update
= NULL
;
3187 struct route_table
*stable
= zvrf
->stable
[AFI_IP
][safi
];
3192 /* Lookup static route prefix. */
3193 rn
= route_node_get (stable
, p
);
3197 type
= STATIC_IPV4_GATEWAY
;
3199 type
= STATIC_IFINDEX
;
3201 type
= STATIC_IPV4_BLACKHOLE
;
3203 /* Do nothing if there is a same static route. */
3204 for (si
= rn
->info
; si
; si
= si
->next
)
3206 if (type
== si
->type
3207 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->addr
.ipv4
))
3208 && (! ifindex
|| ifindex
== si
->ifindex
))
3210 if ((distance
== si
->distance
) && (tag
== si
->tag
))
3212 route_unlock_node (rn
);
3220 /* Distance or tag changed. */
3222 static_delete_ipv4 (safi
, p
, gate
, ifindex
, update
->tag
, update
->distance
, zvrf
);
3224 /* Make new static route structure. */
3225 si
= XCALLOC (MTYPE_STATIC_ROUTE
, sizeof (struct static_route
));
3228 si
->distance
= distance
;
3231 si
->vrf_id
= zvrf
->vrf_id
;
3232 si
->ifindex
= ifindex
;
3234 strcpy(si
->ifname
, ifname
);
3237 si
->addr
.ipv4
= *gate
;
3239 /* Add new static route information to the tree with sort by
3240 distance value and gateway address. */
3241 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
3243 if (si
->distance
< cp
->distance
)
3245 if (si
->distance
> cp
->distance
)
3247 if (si
->type
== STATIC_IPV4_GATEWAY
&& cp
->type
== STATIC_IPV4_GATEWAY
)
3249 if (ntohl (si
->addr
.ipv4
.s_addr
) < ntohl (cp
->addr
.ipv4
.s_addr
))
3251 if (ntohl (si
->addr
.ipv4
.s_addr
) > ntohl (cp
->addr
.ipv4
.s_addr
))
3256 /* Make linked list. */
3266 /* Install into rib. */
3267 static_install_route (AFI_IP
, safi
, p
, si
);
3273 static_delete_ipv4 (safi_t safi
, struct prefix
*p
, struct in_addr
*gate
, unsigned int ifindex
,
3274 u_short tag
, u_char distance
, struct zebra_vrf
*zvrf
)
3277 struct route_node
*rn
;
3278 struct static_route
*si
;
3279 struct route_table
*stable
;
3282 stable
= zebra_vrf_static_table (AFI_IP
, safi
, zvrf
);
3286 /* Lookup static route prefix. */
3287 rn
= route_node_lookup (stable
, p
);
3293 type
= STATIC_IPV4_GATEWAY
;
3295 type
= STATIC_IFINDEX
;
3297 type
= STATIC_IPV4_BLACKHOLE
;
3299 /* Find same static route is the tree */
3300 for (si
= rn
->info
; si
; si
= si
->next
)
3301 if (type
== si
->type
3302 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->addr
.ipv4
))
3303 && (! ifindex
|| ifindex
== si
->ifindex
)
3304 && (! tag
|| (tag
== si
->tag
)))
3307 /* Can't find static route. */
3310 route_unlock_node (rn
);
3314 /* Install into rib. */
3315 static_uninstall_route (AFI_IP
, safi
, p
, si
);
3317 /* Unlink static route from linked list. */
3319 si
->prev
->next
= si
->next
;
3321 rn
->info
= si
->next
;
3323 si
->next
->prev
= si
->prev
;
3324 route_unlock_node (rn
);
3326 /* Free static route configuration. */
3327 XFREE (MTYPE_STATIC_ROUTE
, si
);
3329 route_unlock_node (rn
);
3335 rib_add_ipv6 (int type
, u_short instance
, int flags
, struct prefix_ipv6
*p
,
3336 struct in6_addr
*gate
, unsigned int ifindex
, vrf_id_t vrf_id
,
3337 u_int32_t table_id
, u_int32_t metric
, u_char distance
, safi_t safi
)
3340 struct rib
*same
= NULL
;
3341 struct route_table
*table
;
3342 struct route_node
*rn
;
3343 struct nexthop
*nexthop
;
3346 table
= zebra_vrf_table_with_table_id (AFI_IP6
, safi
, vrf_id
, table_id
);
3350 /* Make sure mask is applied. */
3351 apply_mask_ipv6 (p
);
3353 /* Set default distance by route type. */
3355 distance
= route_info
[type
].distance
;
3357 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
3360 /* Lookup route node.*/
3361 rn
= route_node_get (table
, (struct prefix
*) p
);
3363 /* If same type of route are installed, treat it as a implicit
3365 RNODE_FOREACH_RIB (rn
, rib
)
3367 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3370 if (rib
->type
!= type
)
3372 if (rib
->instance
!= instance
)
3374 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
3379 else if ((nexthop
= rib
->nexthop
) &&
3380 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
3381 nexthop
->ifindex
== ifindex
)
3388 /* Allocate new rib structure. */
3389 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
3392 rib
->instance
= instance
;
3393 rib
->distance
= distance
;
3395 rib
->metric
= metric
;
3396 rib
->table
= table_id
;
3397 rib
->vrf_id
= vrf_id
;
3398 rib
->nexthop_num
= 0;
3399 rib
->uptime
= time (NULL
);
3401 /* Nexthop settings. */
3405 rib_nexthop_ipv6_ifindex_add (rib
, gate
, ifindex
);
3407 rib_nexthop_ipv6_add (rib
, gate
);
3410 rib_nexthop_ifindex_add (rib
, ifindex
);
3412 /* If this route is kernel route, set FIB flag to the route. */
3413 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
3414 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
3415 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3417 /* Link new rib to node.*/
3418 if (IS_ZEBRA_DEBUG_RIB
)
3420 char buf
[INET6_ADDRSTRLEN
];
3421 if (IS_ZEBRA_DEBUG_RIB
)
3423 inet_ntop (p
->family
, &p
->prefix
, buf
, INET6_ADDRSTRLEN
);
3424 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
3426 vrf_id
, buf
, p
->prefixlen
, (void *)rn
,
3427 (void *)rib
, rib
->type
, (void *)same
);
3430 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3431 rib_dump ((struct prefix
*)p
, rib
);
3433 rib_addnode (rn
, rib
, 1);
3435 /* Free implicit route.*/
3437 rib_delnode (rn
, same
);
3439 route_unlock_node (rn
);
3444 rib_add_ipv6_multipath (struct prefix
*p
, struct rib
*rib
, safi_t safi
,
3445 unsigned long ifindex
)
3447 struct route_table
*table
;
3448 struct route_node
*rn
;
3449 struct rib
*same
= NULL
;
3450 struct nexthop
*nexthop
;
3457 if (p
->family
== AF_INET
)
3463 table
= zebra_vrf_table_with_table_id (family
, safi
, rib
->vrf_id
, rib
->table
);
3467 if (p
->family
== AF_INET
)
3469 /* Make it sure prefixlen is applied to the prefix. */
3470 apply_mask_ipv4 ((struct prefix_ipv4
*)p
);
3474 /* Make sure mask is applied. */
3475 apply_mask_ipv6 ((struct prefix_ipv6
*)p
);
3478 /* Set default distance by route type. */
3479 if (rib
->distance
== 0)
3481 rib
->distance
= route_info
[rib
->type
].distance
;
3483 /* iBGP distance is 200. */
3484 if (rib
->type
== ZEBRA_ROUTE_BGP
3485 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
3486 rib
->distance
= 200;
3489 /* Lookup route node.*/
3490 rn
= route_node_get (table
, (struct prefix
*) p
);
3492 /* If same type of route are installed, treat it as a implicit
3494 RNODE_FOREACH_RIB (rn
, same
) {
3495 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
)) {
3498 if (same
->type
!= rib
->type
) {
3502 if (same
->instance
!= rib
->instance
) {
3506 if (same
->table
!= rib
->table
) {
3509 if (same
->type
!= ZEBRA_ROUTE_CONNECT
) {
3512 else if ((nexthop
= same
->nexthop
) &&
3513 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
3514 nexthop
->ifindex
== ifindex
) {
3520 /* If this route is kernel route, set FIB flag to the route. */
3521 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
) {
3522 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
3523 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3527 /* Link new rib to node.*/
3528 if (IS_ZEBRA_DEBUG_RIB
)
3530 char buf
[INET6_ADDRSTRLEN
];
3531 if (IS_ZEBRA_DEBUG_RIB
)
3533 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
3534 zlog_debug ("%u:%s/%d: Inserting route rn %p, rib %p (type %d) "
3536 rib
->vrf_id
, buf
, p
->prefixlen
, rn
, rib
, rib
->type
, same
);
3539 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
3540 rib_dump ((struct prefix
*)p
, rib
);
3542 rib_addnode (rn
, rib
, 1);
3545 /* Free implicit route.*/
3548 rib_delnode (rn
, same
);
3552 route_unlock_node (rn
);
3556 /* XXX factor with rib_delete_ipv6 */
3559 rib_delete_ipv6 (int type
, u_short instance
, int flags
, struct prefix_ipv6
*p
,
3560 struct in6_addr
*gate
, unsigned int ifindex
, vrf_id_t vrf_id
,
3561 u_int32_t table_id
, safi_t safi
)
3563 struct route_table
*table
;
3564 struct route_node
*rn
;
3566 struct rib
*fib
= NULL
;
3567 struct rib
*same
= NULL
;
3568 struct nexthop
*nexthop
, *tnexthop
;
3570 char buf1
[PREFIX_STRLEN
];
3571 char buf2
[INET6_ADDRSTRLEN
];
3574 apply_mask_ipv6 (p
);
3577 table
= zebra_vrf_table_with_table_id (AFI_IP6
, safi
, vrf_id
, table_id
);
3581 /* Lookup route node. */
3582 rn
= route_node_lookup (table
, (struct prefix
*) p
);
3585 if (IS_ZEBRA_DEBUG_RIB
)
3586 zlog_debug ("%u:%s: doesn't exist in rib",
3587 vrf_id
, prefix2str (p
, buf1
, sizeof(buf1
)));
3588 return ZEBRA_ERR_RTNOEXIST
;
3591 /* Lookup same type route. */
3592 RNODE_FOREACH_RIB (rn
, rib
)
3594 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
3597 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
3600 if (rib
->type
!= type
)
3602 if (rib
->instance
!= instance
)
3604 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
3605 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
3607 if (nexthop
->ifindex
!= ifindex
)
3612 route_unlock_node (rn
);
3613 route_unlock_node (rn
);
3619 /* Make sure that the route found has the same gateway. */
3627 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
3628 if (IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
))
3638 /* If same type of route can't be found and this message is from
3642 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
&&
3643 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
))
3645 if (IS_ZEBRA_DEBUG_RIB
)
3646 zlog_debug ("%u:%s/%d: rn %p, rib %p (type %d) was deleted "
3647 "from kernel, adding",
3648 vrf_id
, inet_ntop (p
->family
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
3649 p
->prefixlen
, rn
, fib
, fib
->type
);
3653 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
3654 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3656 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
3660 /* This means someone else, other than Zebra, has deleted a Zebra
3661 * route from the kernel. We will add it back */
3662 rib_install_kernel(rn
, fib
, 0);
3667 if (IS_ZEBRA_DEBUG_KERNEL
)
3670 zlog_debug ("%s: vrf %u via %s ifindex %d type %d "
3671 "doesn't exist in rib",
3672 prefix2str (p
, buf1
, sizeof(buf1
)), vrf_id
,
3673 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
3677 zlog_debug ("%s: vrf %u ifindex %d type %d doesn't exist in rib",
3678 prefix2str (p
, buf1
, sizeof(buf1
)), vrf_id
,
3682 route_unlock_node (rn
);
3683 return ZEBRA_ERR_RTNOEXIST
;
3688 rib_delnode (rn
, same
);
3690 route_unlock_node (rn
);
3694 /* Add static route into static route configuration. */
3696 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
3697 unsigned int ifindex
, const char *ifname
, u_char flags
,
3698 u_short tag
, u_char distance
, struct zebra_vrf
*zvrf
)
3700 struct route_node
*rn
;
3701 struct static_route
*si
;
3702 struct static_route
*pp
;
3703 struct static_route
*cp
;
3704 struct static_route
*update
= NULL
;
3705 struct route_table
*stable
= zvrf
->stable
[AFI_IP6
][SAFI_UNICAST
];
3711 (type
== STATIC_IPV6_GATEWAY
|| type
== STATIC_IPV6_GATEWAY_IFINDEX
))
3715 (type
== STATIC_IPV6_GATEWAY_IFINDEX
|| type
== STATIC_IFINDEX
))
3718 /* Lookup static route prefix. */
3719 rn
= route_node_get (stable
, p
);
3721 /* Do nothing if there is a same static route. */
3722 for (si
= rn
->info
; si
; si
= si
->next
)
3724 if (type
== si
->type
3725 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->addr
.ipv6
))
3726 && (! ifindex
|| ifindex
== si
->ifindex
))
3728 if ((distance
== si
->distance
) && (tag
== si
->tag
))
3730 route_unlock_node (rn
);
3738 /* Distance or tag changed. */
3740 static_delete_ipv6 (p
, type
, gate
, ifindex
, update
->tag
, update
->distance
, zvrf
);
3742 /* Make new static route structure. */
3743 si
= XCALLOC (MTYPE_STATIC_ROUTE
, sizeof (struct static_route
));
3746 si
->distance
= distance
;
3749 si
->vrf_id
= zvrf
->vrf_id
;
3750 si
->ifindex
= ifindex
;
3752 strcpy (si
->ifname
, ifname
);
3756 case STATIC_IPV6_GATEWAY
:
3757 si
->addr
.ipv6
= *gate
;
3759 case STATIC_IPV6_GATEWAY_IFINDEX
:
3760 si
->addr
.ipv6
= *gate
;
3764 /* Add new static route information to the tree with sort by
3765 distance value and gateway address. */
3766 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
3768 if (si
->distance
< cp
->distance
)
3770 if (si
->distance
> cp
->distance
)
3774 /* Make linked list. */
3784 /* Install into rib. */
3785 static_install_route (AFI_IP6
, SAFI_UNICAST
, p
, si
);
3790 /* Delete static route from static route configuration. */
3792 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
3793 unsigned int ifindex
, u_short tag
, u_char distance
,
3794 struct zebra_vrf
*zvrf
)
3796 struct route_node
*rn
;
3797 struct static_route
*si
;
3798 struct route_table
*stable
;
3801 stable
= zebra_vrf_static_table (AFI_IP6
, SAFI_UNICAST
, zvrf
);
3805 /* Lookup static route prefix. */
3806 rn
= route_node_lookup (stable
, p
);
3810 /* Find same static route is the tree */
3811 for (si
= rn
->info
; si
; si
= si
->next
)
3812 if (distance
== si
->distance
3814 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->addr
.ipv6
))
3815 && (! ifindex
|| ifindex
== si
->ifindex
)
3816 && (! tag
|| (tag
== si
->tag
)))
3819 /* Can't find static route. */
3822 route_unlock_node (rn
);
3826 /* Install into rib. */
3827 static_uninstall_route (AFI_IP6
, SAFI_UNICAST
, p
, si
);
3829 /* Unlink static route from linked list. */
3831 si
->prev
->next
= si
->next
;
3833 rn
->info
= si
->next
;
3835 si
->next
->prev
= si
->prev
;
3837 /* Free static route configuration. */
3838 XFREE (MTYPE_STATIC_ROUTE
, si
);
3843 /* Schedule routes of a particular table (address-family) based on event. */
3845 rib_update_table (struct route_table
*table
, rib_update_event_t event
)
3847 struct route_node
*rn
;
3848 struct rib
*rib
, *next
;
3850 /* Walk all routes and queue for processing, if appropriate for
3851 * the trigger event.
3853 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3857 case RIB_UPDATE_IF_CHANGE
:
3858 /* Examine all routes that won't get processed by the protocol or
3859 * triggered by nexthop evaluation (NHT). This would be system,
3860 * kernel and certain static routes. Note that NHT will get
3861 * triggered upon an interface event as connected routes always
3862 * get queued for processing.
3864 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3866 if (rib
->type
== ZEBRA_ROUTE_OSPF
||
3867 rib
->type
== ZEBRA_ROUTE_OSPF6
||
3868 rib
->type
== ZEBRA_ROUTE_BGP
)
3869 continue; /* protocol will handle. */
3870 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
3873 for (nh
= rib
->nexthop
; nh
; nh
= nh
->next
)
3874 if (!(nh
->type
== NEXTHOP_TYPE_IPV4
||
3875 nh
->type
== NEXTHOP_TYPE_IPV6
))
3878 /* If we only have nexthops to a gateway, NHT will
3889 case RIB_UPDATE_RMAP_CHANGE
:
3890 case RIB_UPDATE_OTHER
:
3891 /* Right now, examine all routes. Can restrict to a protocol in
3892 * some cases (TODO).
3894 if (rnode_to_ribs (rn
))
3904 /* RIB update function. */
3906 rib_update (vrf_id_t vrf_id
, rib_update_event_t event
)
3908 struct route_table
*table
;
3910 /* Process routes of interested address-families. */
3911 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
3913 rib_update_table (table
, event
);
3915 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3917 rib_update_table (table
, event
);
3920 /* Remove all routes which comes from non main table. */
3922 rib_weed_table (struct route_table
*table
)
3924 struct route_node
*rn
;
3929 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3930 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3932 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3935 if (rib
->table
!= zebrad
.rtm_table_default
&&
3936 rib
->table
!= RT_TABLE_MAIN
)
3937 rib_delnode (rn
, rib
);
3941 /* Delete all routes from non main table. */
3943 rib_weed_tables (void)
3946 struct zebra_vrf
*zvrf
;
3948 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
3949 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
3951 rib_weed_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3952 rib_weed_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3956 /* Delete self installed routes after zebra is relaunched. */
3958 rib_sweep_table (struct route_table
*table
)
3960 struct route_node
*rn
;
3966 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3967 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3969 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3972 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
3973 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
3975 ret
= rib_uninstall_kernel (rn
, rib
);
3977 rib_delnode (rn
, rib
);
3982 /* Sweep all RIB tables. */
3984 rib_sweep_route (void)
3987 struct zebra_vrf
*zvrf
;
3989 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
3990 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
3992 rib_sweep_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
3993 rib_sweep_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
3997 /* Remove specific by protocol routes from 'table'. */
3998 static unsigned long
3999 rib_score_proto_table (u_char proto
, u_short instance
, struct route_table
*table
)
4001 struct route_node
*rn
;
4004 unsigned long n
= 0;
4007 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
4008 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
4010 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
4012 if (rib
->type
== proto
&& rib
->instance
== instance
)
4014 rib_delnode (rn
, rib
);
4022 /* Remove specific by protocol routes. */
4024 rib_score_proto (u_char proto
, u_short instance
)
4027 struct zebra_vrf
*zvrf
;
4028 unsigned long cnt
= 0;
4030 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
4031 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
4032 cnt
+= rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP
][SAFI_UNICAST
])
4033 +rib_score_proto_table (proto
, instance
, zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4038 /* Close RIB and clean up kernel routes. */
4040 rib_close_table (struct route_table
*table
)
4042 struct route_node
*rn
;
4043 rib_table_info_t
*info
= table
->info
;
4047 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
4048 RNODE_FOREACH_RIB (rn
, rib
)
4050 if (!CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
4053 if (info
->safi
== SAFI_UNICAST
)
4054 zfpm_trigger_update (rn
, NULL
);
4056 if (! RIB_SYSTEM_ROUTE (rib
))
4057 rib_uninstall_kernel (rn
, rib
);
4061 /* Close all RIB tables. */
4066 struct zebra_vrf
*zvrf
;
4067 struct listnode
*node
;
4068 struct interface
*ifp
;
4071 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
4073 if ((zvrf
= vrf_iter2info (iter
)) != NULL
)
4075 rib_close_table (zvrf
->table
[AFI_IP
][SAFI_UNICAST
]);
4076 rib_close_table (zvrf
->table
[AFI_IP6
][SAFI_UNICAST
]);
4078 for (ALL_LIST_ELEMENTS_RO (vrf_iter2iflist (iter
), node
, ifp
))
4079 if_nbr_ipv6ll_to_ipv4ll_neigh_del_all(ifp
);
4082 /* If we do multiple tables per vrf, need to move this to loop above */
4083 zvrf
= vrf_info_lookup (VRF_DEFAULT
);
4085 for (table_id
= 0; table_id
< ZEBRA_KERNEL_TABLE_MAX
; table_id
++)
4087 if (zvrf
->other_table
[AFI_IP
][table_id
])
4088 rib_close_table (zvrf
->other_table
[AFI_IP
][table_id
]);
4090 if (zvrf
->other_table
[AFI_IP6
][table_id
])
4091 rib_close_table (zvrf
->other_table
[AFI_IP6
][table_id
]);
4095 /* Routing information base initialize. */
4099 rib_queue_init (&zebrad
);
4105 * Get the first vrf id that is greater than the given vrf id if any.
4107 * Returns TRUE if a vrf id was found, FALSE otherwise.
4110 vrf_id_get_next (vrf_id_t vrf_id
, vrf_id_t
*next_id_p
)
4112 vrf_iter_t iter
= vrf_iterator (vrf_id
);
4113 struct zebra_vrf
*zvrf
= vrf_iter2info (iter
);
4115 /* The same one ? Then find out the next. */
4116 if (zvrf
&& (zvrf
->vrf_id
== vrf_id
))
4117 zvrf
= vrf_iter2info (vrf_next (iter
));
4121 *next_id_p
= zvrf
->vrf_id
;
4129 * rib_tables_iter_next
4131 * Returns the next table in the iteration.
4133 struct route_table
*
4134 rib_tables_iter_next (rib_tables_iter_t
*iter
)
4136 struct route_table
*table
;
4139 * Array that helps us go over all AFI/SAFI combinations via one
4146 { AFI_IP
, SAFI_UNICAST
},
4147 { AFI_IP
, SAFI_MULTICAST
},
4148 { AFI_IP6
, SAFI_UNICAST
},
4149 { AFI_IP6
, SAFI_MULTICAST
},
4154 switch (iter
->state
)
4157 case RIB_TABLES_ITER_S_INIT
:
4158 iter
->vrf_id
= VRF_DEFAULT
;
4159 iter
->afi_safi_ix
= -1;
4163 case RIB_TABLES_ITER_S_ITERATING
:
4164 iter
->afi_safi_ix
++;
4168 while (iter
->afi_safi_ix
< (int) ZEBRA_NUM_OF (afi_safis
))
4170 table
= zebra_vrf_table (afi_safis
[iter
->afi_safi_ix
].afi
,
4171 afi_safis
[iter
->afi_safi_ix
].safi
,
4176 iter
->afi_safi_ix
++;
4180 * Found another table in this vrf.
4186 * Done with all tables in the current vrf, go to the next
4189 if (!vrf_id_get_next (iter
->vrf_id
, &iter
->vrf_id
))
4192 iter
->afi_safi_ix
= 0;
4197 case RIB_TABLES_ITER_S_DONE
:
4202 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
4204 iter
->state
= RIB_TABLES_ITER_S_DONE
;