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"
39 #include "zebra/rib.h"
41 #include "zebra/zserv.h"
42 #include "zebra/redistribute.h"
43 #include "zebra/debug.h"
44 #include "zebra/zebra_fpm.h"
45 #include "zebra/zebra_rnh.h"
47 /* Default rtm_table for all clients */
48 extern struct zebra_t zebrad
;
50 /* Hold time for RIB process, should be very minimal.
51 * it is useful to able to set it otherwise for testing, hence exported
52 * as global here for test-rig code.
54 int rib_process_hold_time
= 10;
56 /* Each route type's string and default distance value. */
61 } route_info
[ZEBRA_ROUTE_MAX
] =
63 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
64 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
65 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
66 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
67 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
68 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
69 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
70 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
71 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
72 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
73 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 95},
74 /* no entry/default: 150 */
77 /* Vector for routing table. */
78 static vector vrf_vector
;
84 vrf_table_create (struct vrf
*vrf
, afi_t afi
, safi_t safi
)
86 rib_table_info_t
*info
;
87 struct route_table
*table
;
89 assert (!vrf
->table
[afi
][safi
]);
91 table
= route_table_init ();
92 vrf
->table
[afi
][safi
] = table
;
94 info
= XCALLOC (MTYPE_RIB_TABLE_INFO
, sizeof (*info
));
101 /* Allocate new VRF. */
103 vrf_alloc (const char *name
)
107 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
111 vrf
->name
= XSTRDUP (MTYPE_VRF_NAME
, name
);
113 /* Allocate routing table and static table. */
114 vrf_table_create (vrf
, AFI_IP
, SAFI_UNICAST
);
115 vrf_table_create (vrf
, AFI_IP6
, SAFI_UNICAST
);
116 vrf
->stable
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
117 vrf
->stable
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
118 vrf_table_create (vrf
, AFI_IP
, SAFI_MULTICAST
);
119 vrf_table_create (vrf
, AFI_IP6
, SAFI_MULTICAST
);
120 vrf
->stable
[AFI_IP
][SAFI_MULTICAST
] = route_table_init ();
121 vrf
->stable
[AFI_IP6
][SAFI_MULTICAST
] = route_table_init ();
123 vrf
->rnh_table
[AFI_IP
] = route_table_init();
124 vrf
->rnh_table
[AFI_IP6
] = route_table_init();
126 vrf
->import_check_table
[AFI_IP
] = route_table_init();
127 vrf
->import_check_table
[AFI_IP6
] = route_table_init();
132 /* Lookup VRF by identifier. */
134 vrf_lookup (u_int32_t id
)
136 return vector_lookup (vrf_vector
, id
);
139 /* Initialize VRF. */
143 struct vrf
*default_table
;
145 /* Allocate VRF vector. */
146 vrf_vector
= vector_init (1);
148 /* Allocate default main table. */
149 default_table
= vrf_alloc ("Default-IP-Routing-Table");
151 /* Default table index must be 0. */
152 vector_set_index (vrf_vector
, 0, default_table
);
155 /* Lookup route table. */
157 vrf_table (afi_t afi
, safi_t safi
, u_int32_t id
)
161 vrf
= vrf_lookup (id
);
165 if( afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
168 return vrf
->table
[afi
][safi
];
171 /* Lookup static route table. */
173 vrf_static_table (afi_t afi
, safi_t safi
, u_int32_t id
)
177 vrf
= vrf_lookup (id
);
181 if( afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
184 return vrf
->stable
[afi
][safi
];
188 vrf_other_route_table (afi_t afi
, u_int32_t table_id
, u_int32_t vrf_id
)
192 vrf
= vrf_lookup (vrf_id
);
199 if (table_id
>= ZEBRA_KERNEL_TABLE_MAX
)
202 if (vrf
->other_table
[afi
][table_id
] == NULL
)
204 vrf
->other_table
[afi
][table_id
] = route_table_init();
207 return (vrf
->other_table
[afi
][table_id
]);
211 is_zebra_valid_kernel_table(u_int32_t table_id
)
213 if ((table_id
> ZEBRA_KERNEL_TABLE_MAX
) ||
214 (table_id
== RT_TABLE_UNSPEC
) ||
215 (table_id
== RT_TABLE_LOCAL
) ||
216 (table_id
== RT_TABLE_COMPAT
))
223 is_zebra_main_routing_table(u_int32_t table_id
)
225 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
230 /* Add nexthop to the end of a nexthop list. */
232 _nexthop_add (struct nexthop
**target
, struct nexthop
*nexthop
)
234 struct nexthop
*last
;
236 for (last
= *target
; last
&& last
->next
; last
= last
->next
)
239 last
->next
= nexthop
;
242 nexthop
->prev
= last
;
245 /* Add nexthop to the end of a rib node's nexthop list */
247 nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
249 _nexthop_add(&rib
->nexthop
, nexthop
);
254 _copy_nexthops (struct nexthop
**tnh
, struct nexthop
*nh
)
256 struct nexthop
*nexthop
;
259 for (nh1
= nh
; nh1
; nh1
= nh1
->next
)
261 nexthop
= nexthop_new();
262 nexthop
->flags
= nh
->flags
;
263 nexthop
->type
= nh
->type
;
264 nexthop
->ifindex
= nh
->ifindex
;
266 nexthop
->ifname
= XSTRDUP(0, nh
->ifname
);
267 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
268 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
269 _nexthop_add(tnh
, nexthop
);
271 if (CHECK_FLAG(nh1
->flags
, NEXTHOP_FLAG_RECURSIVE
))
272 _copy_nexthops(&nexthop
->resolved
, nh1
->resolved
);
277 * copy_nexthop - copy a nexthop to the rib structure.
280 copy_nexthops (struct rib
*rib
, struct nexthop
*nh
)
282 struct nexthop
*nexthop
;
284 nexthop
= nexthop_new();
285 nexthop
->flags
= nh
->flags
;
286 nexthop
->type
= nh
->type
;
287 nexthop
->ifindex
= nh
->ifindex
;
289 nexthop
->ifname
= XSTRDUP(0, nh
->ifname
);
290 memcpy(&(nexthop
->gate
), &(nh
->gate
), sizeof(union g_addr
));
291 memcpy(&(nexthop
->src
), &(nh
->src
), sizeof(union g_addr
));
292 nexthop_add(rib
, nexthop
);
293 if (CHECK_FLAG(nh
->flags
, NEXTHOP_FLAG_RECURSIVE
))
294 _copy_nexthops(&nexthop
->resolved
, nh
->resolved
);
297 /* Delete specified nexthop from the list. */
299 nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
302 nexthop
->next
->prev
= nexthop
->prev
;
304 nexthop
->prev
->next
= nexthop
->next
;
306 rib
->nexthop
= nexthop
->next
;
312 nexthop_free (struct nexthop
*nexthop
, struct route_node
*rn
)
315 XFREE (0, nexthop
->ifname
);
316 if (nexthop
->resolved
)
317 nexthops_free(nexthop
->resolved
, rn
);
318 XFREE (MTYPE_NEXTHOP
, nexthop
);
321 /* Frees a list of nexthops */
323 nexthops_free (struct nexthop
*nexthop
, struct route_node
*rn
)
325 struct nexthop
*nh
, *next
;
328 for (nh
= nexthop
; nh
; nh
= next
)
331 if (nh
->type
== NEXTHOP_TYPE_IPV4
)
333 nh_p
.family
= AF_INET
;
334 nh_p
.prefixlen
= IPV4_MAX_BITLEN
;
335 nh_p
.u
.prefix4
= nh
->gate
.ipv4
;
336 zebra_deregister_rnh_static_nh(&nh_p
, rn
);
338 else if (nh
->type
== NEXTHOP_TYPE_IPV6
)
340 nh_p
.family
= AF_INET6
;
341 nh_p
.prefixlen
= IPV6_MAX_BITLEN
;
342 nh_p
.u
.prefix6
= nh
->gate
.ipv6
;
343 zebra_deregister_rnh_static_nh(&nh_p
, rn
);
345 nexthop_free (nh
, rn
);
350 nexthop_ifindex_add (struct rib
*rib
, unsigned int ifindex
)
352 struct nexthop
*nexthop
;
354 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
355 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
356 nexthop
->ifindex
= ifindex
;
358 nexthop_add (rib
, nexthop
);
364 nexthop_ifname_add (struct rib
*rib
, char *ifname
)
366 struct nexthop
*nexthop
;
368 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
369 nexthop
->type
= NEXTHOP_TYPE_IFNAME
;
370 nexthop
->ifname
= XSTRDUP (0, ifname
);
372 nexthop_add (rib
, nexthop
);
378 nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
380 struct nexthop
*nexthop
;
382 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
383 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
384 nexthop
->gate
.ipv4
= *ipv4
;
386 nexthop
->src
.ipv4
= *src
;
388 nexthop_add (rib
, nexthop
);
394 nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
395 struct in_addr
*src
, unsigned int ifindex
)
397 struct nexthop
*nexthop
;
399 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
400 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
401 nexthop
->gate
.ipv4
= *ipv4
;
403 nexthop
->src
.ipv4
= *src
;
404 nexthop
->ifindex
= ifindex
;
406 nexthop_add (rib
, nexthop
);
412 nexthop_ipv4_ifindex_ol_add (struct rib
*rib
, const struct in_addr
*ipv4
,
413 const struct in_addr
*src
, const unsigned int ifindex
)
415 struct nexthop
*nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
417 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
418 IPV4_ADDR_COPY (&nexthop
->gate
.ipv4
, ipv4
);
420 IPV4_ADDR_COPY (&nexthop
->src
.ipv4
, src
);
421 nexthop
->ifindex
= ifindex
;
422 SET_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
);
423 nexthop_add (rib
, nexthop
);
429 nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
431 struct nexthop
*nexthop
;
433 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
434 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
435 nexthop
->gate
.ipv6
= *ipv6
;
437 nexthop_add (rib
, nexthop
);
443 nexthop_ipv6_ifname_add (struct rib
*rib
, struct in6_addr
*ipv6
,
446 struct nexthop
*nexthop
;
448 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
449 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFNAME
;
450 nexthop
->gate
.ipv6
= *ipv6
;
451 nexthop
->ifname
= XSTRDUP (0, ifname
);
453 nexthop_add (rib
, nexthop
);
459 nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
460 unsigned int ifindex
)
462 struct nexthop
*nexthop
;
464 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
465 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
466 nexthop
->gate
.ipv6
= *ipv6
;
467 nexthop
->ifindex
= ifindex
;
469 nexthop_add (rib
, nexthop
);
473 #endif /* HAVE_IPV6 */
476 nexthop_blackhole_add (struct rib
*rib
)
478 struct nexthop
*nexthop
;
480 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
481 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
482 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
484 nexthop_add (rib
, nexthop
);
489 /* This method checks whether a recursive nexthop has at
490 * least one resolved nexthop in the fib.
493 nexthop_has_fib_child(struct nexthop
*nexthop
)
497 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
500 for (nh
= nexthop
->resolved
; nh
; nh
= nh
->next
)
501 if (CHECK_FLAG (nh
->flags
, NEXTHOP_FLAG_FIB
))
507 /* If force flag is not set, do not modify falgs at all for uninstall
508 the route from FIB. */
510 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
511 struct route_node
*top
)
513 struct prefix_ipv4 p
;
514 struct route_table
*table
;
515 struct route_node
*rn
;
518 struct nexthop
*newhop
, *tnewhop
;
519 struct nexthop
*resolved_hop
;
521 struct interface
*ifp
;
523 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
524 nexthop
->ifindex
= 0;
528 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
529 nexthops_free(nexthop
->resolved
, top
);
530 nexthop
->resolved
= NULL
;
533 /* Skip nexthops that have been filtered out due to route-map */
534 /* The nexthops are specific to this route and so the same */
535 /* nexthop for a different route may not have this flag set */
536 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
539 /* onlink flag is an indication that we need to only check that
540 * the link is up, we won't find the GW address in the routing
543 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_ONLINK
))
545 ifp
= if_lookup_by_index (nexthop
->ifindex
);
546 if (ifp
&& if_is_operative(ifp
))
552 /* Make lookup prefix. */
553 memset (&p
, 0, sizeof (struct prefix_ipv4
));
555 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
556 p
.prefix
= nexthop
->gate
.ipv4
;
559 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
563 rn
= route_node_match (table
, (struct prefix
*) &p
);
566 route_unlock_node (rn
);
568 /* If lookup self prefix return immediately. */
572 /* Pick up selected route. */
573 RNODE_FOREACH_RIB (rn
, match
)
575 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
577 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
581 /* If there is no selected route or matched route is EGP, go up
587 } while (rn
&& rn
->info
== NULL
);
589 route_lock_node (rn
);
593 /* If the longest prefix match for the nexthop yields
594 * a blackhole, mark it as inactive. */
595 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
596 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
599 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
601 /* Directly point connected route. */
602 newhop
= match
->nexthop
;
603 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
604 nexthop
->ifindex
= newhop
->ifindex
;
608 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
611 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
612 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
613 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
617 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
618 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
620 resolved_hop
= XCALLOC(MTYPE_NEXTHOP
, sizeof (struct nexthop
));
621 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
622 /* If the resolving route specifies a gateway, use it */
623 if (newhop
->type
== NEXTHOP_TYPE_IPV4
624 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
625 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFNAME
)
627 resolved_hop
->type
= newhop
->type
;
628 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
632 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
633 resolved_hop
->ifindex
= newhop
->ifindex
;
634 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
635 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
639 /* If the resolving route is an interface route,
640 * it means the gateway we are looking up is connected
641 * to that interface. (The actual network is _not_ onlink).
642 * Therefore, the resolved route should have the original
643 * gateway as nexthop as it is directly connected.
645 * On Linux, we have to set the onlink netlink flag because
646 * otherwise, the kernel won't accept the route. */
647 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
648 || newhop
->type
== NEXTHOP_TYPE_IFNAME
)
650 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
651 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
652 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
653 resolved_hop
->ifindex
= newhop
->ifindex
;
656 _nexthop_add(&nexthop
->resolved
, resolved_hop
);
662 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
665 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
666 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
670 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
672 resolved_hop
= XCALLOC(MTYPE_NEXTHOP
, sizeof (struct nexthop
));
673 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
674 /* If the resolving route specifies a gateway, use it */
675 if (newhop
->type
== NEXTHOP_TYPE_IPV4
676 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
677 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFNAME
)
679 resolved_hop
->type
= newhop
->type
;
680 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
684 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
685 resolved_hop
->ifindex
= newhop
->ifindex
;
686 if (newhop
->flags
& NEXTHOP_FLAG_ONLINK
)
687 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
691 /* If the resolving route is an interface route,
692 * it means the gateway we are looking up is connected
693 * to that interface. (The actual network is _not_ onlink).
694 * Therefore, the resolved route should have the original
695 * gateway as nexthop as it is directly connected.
697 * On Linux, we have to set the onlink netlink flag because
698 * otherwise, the kernel won't accept the route.
700 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
701 || newhop
->type
== NEXTHOP_TYPE_IFNAME
)
703 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
704 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
705 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
706 resolved_hop
->ifindex
= newhop
->ifindex
;
709 _nexthop_add(&nexthop
->resolved
, resolved_hop
);
725 /* If force flag is not set, do not modify falgs at all for uninstall
726 the route from FIB. */
728 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
729 struct route_node
*top
)
731 struct prefix_ipv6 p
;
732 struct route_table
*table
;
733 struct route_node
*rn
;
736 struct nexthop
*newhop
, *tnewhop
;
738 struct nexthop
*resolved_hop
;
740 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
741 nexthop
->ifindex
= 0;
745 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
746 nexthops_free(nexthop
->resolved
, top
);
747 nexthop
->resolved
= NULL
;
750 /* Skip nexthops that have been filtered out due to route-map */
751 /* The nexthops are specific to this route and so the same */
752 /* nexthop for a different route may not have this flag set */
753 if (CHECK_FLAG(nexthop
->flags
, NEXTHOP_FLAG_FILTERED
))
756 /* Make lookup prefix. */
757 memset (&p
, 0, sizeof (struct prefix_ipv6
));
759 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
760 p
.prefix
= nexthop
->gate
.ipv6
;
763 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
767 rn
= route_node_match (table
, (struct prefix
*) &p
);
770 route_unlock_node (rn
);
772 /* If lookup self prefix return immediately. */
776 /* Pick up selected route. */
777 RNODE_FOREACH_RIB (rn
, match
)
779 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
781 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
785 /* If there is no selected route or matched route is EGP, go up
791 } while (rn
&& rn
->info
== NULL
);
793 route_lock_node (rn
);
797 /* If the longest prefix match for the nexthop yields
798 * a blackhole, mark it as inactive. */
799 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
800 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
803 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
805 /* Directly point connected route. */
806 newhop
= match
->nexthop
;
808 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
809 nexthop
->ifindex
= newhop
->ifindex
;
813 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
816 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
817 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
818 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
822 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
823 SET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
825 resolved_hop
= XCALLOC(MTYPE_NEXTHOP
, sizeof (struct nexthop
));
826 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
827 /* See nexthop_active_ipv4 for a description how the
828 * resolved nexthop is constructed. */
829 if (newhop
->type
== NEXTHOP_TYPE_IPV6
830 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
831 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
833 resolved_hop
->type
= newhop
->type
;
834 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
838 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
839 resolved_hop
->ifindex
= newhop
->ifindex
;
843 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
844 || newhop
->type
== NEXTHOP_TYPE_IFNAME
)
846 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
847 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
848 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
849 resolved_hop
->ifindex
= newhop
->ifindex
;
852 _nexthop_add(&nexthop
->resolved
, resolved_hop
);
858 else if (rib
->type
== ZEBRA_ROUTE_STATIC
)
861 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
862 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
866 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
868 resolved_hop
= XCALLOC(MTYPE_NEXTHOP
, sizeof (struct nexthop
));
869 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
870 /* See nexthop_active_ipv4 for a description how the
871 * resolved nexthop is constructed. */
872 if (newhop
->type
== NEXTHOP_TYPE_IPV6
873 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
874 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
876 resolved_hop
->type
= newhop
->type
;
877 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
881 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
882 resolved_hop
->ifindex
= newhop
->ifindex
;
886 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
887 || newhop
->type
== NEXTHOP_TYPE_IFNAME
)
889 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
890 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
891 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
892 resolved_hop
->ifindex
= newhop
->ifindex
;
895 _nexthop_add(&nexthop
->resolved
, resolved_hop
);
909 #endif /* HAVE_IPV6 */
912 rib_match_ipv4 (struct in_addr addr
)
914 struct prefix_ipv4 p
;
915 struct route_table
*table
;
916 struct route_node
*rn
;
918 struct nexthop
*newhop
, *tnewhop
;
922 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
926 memset (&p
, 0, sizeof (struct prefix_ipv4
));
928 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
931 rn
= route_node_match (table
, (struct prefix
*) &p
);
935 route_unlock_node (rn
);
937 /* Pick up selected route. */
938 RNODE_FOREACH_RIB (rn
, match
)
940 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
942 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
946 /* If there is no selected route or matched route is EGP, go up
952 } while (rn
&& rn
->info
== NULL
);
954 route_lock_node (rn
);
958 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
959 /* Directly point connected route. */
963 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
964 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
974 rib_lookup_ipv4 (struct prefix_ipv4
*p
)
976 struct route_table
*table
;
977 struct route_node
*rn
;
979 struct nexthop
*nexthop
, *tnexthop
;
983 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
987 rn
= route_node_lookup (table
, (struct prefix
*) p
);
989 /* No route for this prefix. */
994 route_unlock_node (rn
);
996 RNODE_FOREACH_RIB (rn
, match
)
998 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
1000 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
1007 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
1010 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
1011 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1018 * This clone function, unlike its original rib_lookup_ipv4(), checks
1019 * if specified IPv4 route record (prefix/mask -> gate) exists in
1020 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
1024 * 0: exact match found
1025 * 1: a match was found with a different gate
1026 * 2: connected route found
1027 * 3: no matches found
1030 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
)
1032 struct route_table
*table
;
1033 struct route_node
*rn
;
1035 struct nexthop
*nexthop
, *tnexthop
;
1037 int nexthops_active
;
1040 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1042 return ZEBRA_RIB_LOOKUP_ERROR
;
1044 /* Scan the RIB table for exactly matching RIB entry. */
1045 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1047 /* No route for this prefix. */
1049 return ZEBRA_RIB_NOTFOUND
;
1052 route_unlock_node (rn
);
1054 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
1055 RNODE_FOREACH_RIB (rn
, match
)
1057 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
1059 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
1063 /* None such found :( */
1065 return ZEBRA_RIB_NOTFOUND
;
1067 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
1068 return ZEBRA_RIB_FOUND_CONNECTED
;
1070 /* Ok, we have a cood candidate, let's check it's nexthop list... */
1071 nexthops_active
= 0;
1072 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
1073 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1075 nexthops_active
= 1;
1076 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip (qgate
))
1077 return ZEBRA_RIB_FOUND_EXACT
;
1078 if (IS_ZEBRA_DEBUG_RIB
)
1080 char gate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
1081 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
1082 inet_ntop (AF_INET
, &sockunion2ip(qgate
), qgate_buf
, INET_ADDRSTRLEN
);
1083 zlog_debug ("%s: qgate == %s, %s == %s", __func__
,
1084 qgate_buf
, recursing
? "rgate" : "gate", gate_buf
);
1088 if (nexthops_active
)
1089 return ZEBRA_RIB_FOUND_NOGATE
;
1091 return ZEBRA_RIB_NOTFOUND
;
1096 rib_match_ipv6 (struct in6_addr
*addr
)
1098 struct prefix_ipv6 p
;
1099 struct route_table
*table
;
1100 struct route_node
*rn
;
1102 struct nexthop
*newhop
, *tnewhop
;
1106 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
1110 memset (&p
, 0, sizeof (struct prefix_ipv6
));
1111 p
.family
= AF_INET6
;
1112 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
1113 IPV6_ADDR_COPY (&p
.prefix
, addr
);
1115 rn
= route_node_match (table
, (struct prefix
*) &p
);
1119 route_unlock_node (rn
);
1121 /* Pick up selected route. */
1122 RNODE_FOREACH_RIB (rn
, match
)
1124 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
1126 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
1130 /* If there is no selected route or matched route is EGP, go up
1136 } while (rn
&& rn
->info
== NULL
);
1138 route_lock_node (rn
);
1142 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
1143 /* Directly point connected route. */
1147 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
1148 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
1156 #endif /* HAVE_IPV6 */
1158 #define RIB_SYSTEM_ROUTE(R) \
1159 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
1161 /* This function verifies reachability of one given nexthop, which can be
1162 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
1163 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
1164 * nexthop->ifindex will be updated appropriately as well.
1165 * An existing route map can turn (otherwise active) nexthop into inactive, but
1168 * The return value is the final value of 'ACTIVE' flag.
1172 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
1173 struct nexthop
*nexthop
, int set
)
1175 rib_table_info_t
*info
= rn
->table
->info
;
1176 struct interface
*ifp
;
1177 route_map_result_t ret
= RMAP_MATCH
;
1179 char buf
[INET6_ADDRSTRLEN
+1];
1181 if (rn
->p
.family
== AF_INET
)
1183 else if (rn
->p
.family
== AF_INET6
)
1187 switch (nexthop
->type
)
1189 case NEXTHOP_TYPE_IFINDEX
:
1190 ifp
= if_lookup_by_index (nexthop
->ifindex
);
1191 if (ifp
&& if_is_operative(ifp
))
1192 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1194 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1196 case NEXTHOP_TYPE_IPV6_IFNAME
:
1198 case NEXTHOP_TYPE_IFNAME
:
1199 ifp
= if_lookup_by_name (nexthop
->ifname
);
1200 if (ifp
&& if_is_operative(ifp
))
1203 nexthop
->ifindex
= ifp
->ifindex
;
1204 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1209 nexthop
->ifindex
= 0;
1210 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1213 case NEXTHOP_TYPE_IPV4
:
1214 case NEXTHOP_TYPE_IPV4_IFINDEX
:
1216 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
1217 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1219 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1222 case NEXTHOP_TYPE_IPV6
:
1224 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1225 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1227 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1229 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1231 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
1233 ifp
= if_lookup_by_index (nexthop
->ifindex
);
1234 if (ifp
&& if_is_operative(ifp
))
1235 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1237 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1241 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1242 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1244 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1247 #endif /* HAVE_IPV6 */
1248 case NEXTHOP_TYPE_BLACKHOLE
:
1249 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1254 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1257 /* XXX: What exactly do those checks do? Do we support
1258 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
1259 if (RIB_SYSTEM_ROUTE(rib
) ||
1260 (family
== AFI_IP
&& rn
->p
.family
!= AF_INET
) ||
1261 (family
== AFI_IP6
&& rn
->p
.family
!= AF_INET6
))
1262 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1264 /* The original code didn't determine the family correctly
1265 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1266 * from the rib_table_info in those cases.
1267 * Possibly it may be better to use only the rib_table_info
1273 memset(&nexthop
->rmap_src
.ipv6
, 0, sizeof(union g_addr
));
1275 /* It'll get set if required inside */
1276 ret
= zebra_route_map_check(family
, rib
->type
, &rn
->p
, nexthop
, rib
->tag
);
1277 if (ret
== RMAP_DENYMATCH
)
1279 if (IS_ZEBRA_DEBUG_RIB
)
1281 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, sizeof (buf
));
1282 zlog_debug("%s: Filtering out %s with NH out %s due to route map",
1283 __FUNCTION__
, buf
, nexthop
->ifname
);
1285 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1288 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1291 /* Iterate over all nexthops of the given RIB entry and refresh their
1292 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1293 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1294 * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
1295 * transparently passed to nexthop_active_check().
1297 * Return value is the new number of active nexthops.
1301 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
1303 struct nexthop
*nexthop
;
1304 union g_addr prev_src
;
1305 unsigned int prev_active
, prev_index
, new_active
, old_num_nh
;
1307 old_num_nh
= rib
->nexthop_active_num
;
1309 rib
->nexthop_active_num
= 0;
1310 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
1312 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1314 /* No protocol daemon provides src and so we're skipping tracking it */
1315 prev_src
= nexthop
->rmap_src
;
1316 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1317 prev_index
= nexthop
->ifindex
;
1318 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
1319 rib
->nexthop_active_num
++;
1320 /* Don't allow src setting on IPv6 addr for now */
1321 if (prev_active
!= new_active
||
1322 prev_index
!= nexthop
->ifindex
||
1323 ((nexthop
->type
>= NEXTHOP_TYPE_IFINDEX
&&
1324 nexthop
->type
< NEXTHOP_TYPE_IPV6
) &&
1325 prev_src
.ipv4
.s_addr
!= nexthop
->rmap_src
.ipv4
.s_addr
))
1327 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
1328 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1332 if (old_num_nh
!= rib
->nexthop_active_num
)
1333 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
1335 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
))
1337 SET_FLAG (rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1340 return rib
->nexthop_active_num
;
1346 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
)
1349 struct nexthop
*nexthop
, *tnexthop
;
1353 * Make sure we update the FPM any time we send new information to
1356 zfpm_trigger_update (rn
, "installing in kernel");
1357 switch (PREFIX_FAMILY (&rn
->p
))
1360 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
1364 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
1366 #endif /* HAVE_IPV6 */
1369 /* This condition is never met, if we are using rt_socket.c */
1372 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1373 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1377 /* Uninstall the route from kernel. */
1379 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
1382 struct nexthop
*nexthop
, *tnexthop
;
1386 * Make sure we update the FPM any time we send new information to
1389 zfpm_trigger_update (rn
, "uninstalling from kernel");
1391 switch (PREFIX_FAMILY (&rn
->p
))
1394 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
1398 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
1400 #endif /* HAVE_IPV6 */
1403 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1404 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1409 /* Uninstall the route from kernel. */
1411 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
1413 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1415 zfpm_trigger_update (rn
, "rib_uninstall");
1417 redistribute_delete (&rn
->p
, rib
);
1418 if (! RIB_SYSTEM_ROUTE (rib
))
1419 rib_uninstall_kernel (rn
, rib
);
1420 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
1424 static void rib_unlink (struct route_node
*, struct rib
*);
1427 * rib_can_delete_dest
1429 * Returns TRUE if the given dest can be deleted from the table.
1432 rib_can_delete_dest (rib_dest_t
*dest
)
1440 * Don't delete the dest if we have to update the FPM about this
1443 if (CHECK_FLAG (dest
->flags
, RIB_DEST_UPDATE_FPM
) ||
1444 CHECK_FLAG (dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1453 * Garbage collect the rib dest corresponding to the given route node
1456 * Returns TRUE if the dest was deleted, FALSE otherwise.
1459 rib_gc_dest (struct route_node
*rn
)
1462 char buf
[INET6_ADDRSTRLEN
];
1464 dest
= rib_dest_from_rnode (rn
);
1468 if (!rib_can_delete_dest (dest
))
1471 if (IS_ZEBRA_DEBUG_RIB
)
1473 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, sizeof (buf
));
1474 zlog_debug ("%s: %s/%d: removing dest from table", __func__
,
1475 buf
, rn
->p
.prefixlen
);
1479 XFREE (MTYPE_RIB_DEST
, dest
);
1483 * Release the one reference that we keep on the route node.
1485 route_unlock_node (rn
);
1489 /* Core function for processing routing information base. */
1491 rib_process (struct route_node
*rn
)
1495 struct rib
*fib
= NULL
;
1496 struct rib
*select
= NULL
;
1497 struct rib
*del
= NULL
;
1499 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1501 char buf
[INET6_ADDRSTRLEN
];
1505 if (IS_ZEBRA_DEBUG_RIB
|| IS_ZEBRA_DEBUG_RIB_Q
)
1506 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1508 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1510 UNSET_FLAG(rib
->status
, RIB_ENTRY_NEXTHOPS_CHANGED
);
1512 /* Currently installed rib. */
1513 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1515 assert (fib
== NULL
);
1519 /* Unlock removed routes, so they'll be freed, bar the FIB entry,
1520 * which we need to do do further work with below.
1522 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1526 if (IS_ZEBRA_DEBUG_RIB
)
1527 zlog_debug ("%s: %s/%d: rn %p, removing rib %p", __func__
,
1528 buf
, rn
->p
.prefixlen
, rn
, rib
);
1529 rib_unlink (rn
, rib
);
1537 /* Skip unreachable nexthop. */
1538 /* This first call to nexthop_active_update is merely to determine if
1539 * there's any change to nexthops associated with this RIB entry. Now,
1540 * rib_process() can be invoked due to an external event such as link
1541 * down or due to next-hop-tracking evaluation. In the latter case,
1542 * a decision has already been made that the NHs have changed. So, no
1543 * need to invoke a potentially expensive call again. Further, since
1544 * the change might be in a recursive NH which is not caught in
1545 * the nexthop_active_update() code. Thus, we might miss changes to
1548 if (!CHECK_FLAG(rib
->flags
, ZEBRA_FLAG_CHANGED
) &&
1549 ! nexthop_active_update (rn
, rib
, 0))
1552 /* Infinit distance. */
1553 if (rib
->distance
== DISTANCE_INFINITY
)
1556 /* Newly selected rib, the common case. */
1563 /* filter route selection in following order:
1564 * - connected beats other types
1565 * - lower distance beats higher
1566 * - lower metric beats higher for equal distance
1567 * - last, hence oldest, route wins tie break.
1570 /* Connected routes. Pick the last connected
1571 * route of the set of lowest metric connected routes.
1573 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1575 if (select
->type
!= ZEBRA_ROUTE_CONNECT
1576 || rib
->metric
<= select
->metric
)
1580 else if (select
->type
== ZEBRA_ROUTE_CONNECT
)
1583 /* higher distance loses */
1584 if (rib
->distance
> select
->distance
)
1588 if (rib
->distance
< select
->distance
)
1594 /* metric tie-breaks equal distance */
1595 if (rib
->metric
<= select
->metric
)
1597 } /* RNODE_FOREACH_RIB_SAFE */
1599 /* After the cycle is finished, the following pointers will be set:
1600 * select --- the winner RIB entry, if any was found, otherwise NULL
1601 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1602 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1606 /* Same RIB entry is selected. Update FIB and finish. */
1607 if (select
&& select
== fib
)
1609 if (IS_ZEBRA_DEBUG_RIB
)
1610 zlog_debug ("%s: %s/%d: Updating existing route, select %p, fib %p",
1611 __func__
, buf
, rn
->p
.prefixlen
, select
, fib
);
1612 if (CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
1614 zfpm_trigger_update (rn
, "updating existing route");
1616 redistribute_delete (&rn
->p
, select
);
1617 if (! RIB_SYSTEM_ROUTE (select
))
1618 rib_uninstall_kernel (rn
, select
);
1620 /* Set real nexthop. */
1621 /* Need to check if any NHs are active to clear the
1624 if (nexthop_active_update (rn
, select
, 1))
1626 if (! RIB_SYSTEM_ROUTE (select
))
1627 rib_install_kernel (rn
, select
);
1628 redistribute_add (&rn
->p
, select
);
1632 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1634 UNSET_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
);
1636 else if (! RIB_SYSTEM_ROUTE (select
))
1638 /* Housekeeping code to deal with
1639 race conditions in kernel with linux
1640 netlink reporting interface up before IPv4 or IPv6 protocol
1641 is ready to add routes.
1642 This makes sure the routes are IN the kernel.
1645 for (ALL_NEXTHOPS_RO(select
->nexthop
, nexthop
, tnexthop
, recursing
))
1646 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1652 rib_install_kernel (rn
, select
);
1657 /* At this point we either haven't found the best RIB entry or it is
1658 * different from what we currently intend to flag with SELECTED. In both
1659 * cases, if a RIB block is present in FIB, it should be withdrawn.
1663 if (IS_ZEBRA_DEBUG_RIB
)
1664 zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__
,
1665 buf
, rn
->p
.prefixlen
, fib
);
1667 zfpm_trigger_update (rn
, "removing existing route");
1669 redistribute_delete (&rn
->p
, fib
);
1670 if (! RIB_SYSTEM_ROUTE (fib
))
1671 rib_uninstall_kernel (rn
, fib
);
1672 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1674 /* Set real nexthop. */
1675 nexthop_active_update (rn
, fib
, 1);
1676 UNSET_FLAG(fib
->flags
, ZEBRA_FLAG_CHANGED
);
1679 /* Regardless of some RIB entry being SELECTED or not before, now we can
1680 * tell, that if a new winner exists, FIB is still not updated with this
1681 * data, but ready to be.
1685 if (IS_ZEBRA_DEBUG_RIB
)
1686 zlog_debug ("%s: %s/%d: Adding route, select %p", __func__
, buf
,
1687 rn
->p
.prefixlen
, select
);
1689 zfpm_trigger_update (rn
, "new route selected");
1691 /* Set real nexthop. */
1692 if (nexthop_active_update (rn
, select
, 1))
1694 if (! RIB_SYSTEM_ROUTE (select
))
1695 rib_install_kernel (rn
, select
);
1696 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1697 redistribute_add (&rn
->p
, select
);
1699 UNSET_FLAG(select
->flags
, ZEBRA_FLAG_CHANGED
);
1702 /* FIB route was removed, should be deleted */
1705 if (IS_ZEBRA_DEBUG_RIB
)
1706 zlog_debug ("%s: %s/%d: Deleting fib %p, rn %p", __func__
, buf
,
1707 rn
->p
.prefixlen
, del
, rn
);
1708 rib_unlink (rn
, del
);
1712 if (IS_ZEBRA_DEBUG_RIB_Q
)
1713 zlog_debug ("%s: %s/%d: rn %p dequeued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1716 * Check if the dest can be deleted now.
1721 /* Take a list of route_node structs and return 1, if there was a record
1722 * picked from it and processed by rib_process(). Don't process more,
1723 * than one RN record; operate only in the specified sub-queue.
1726 process_subq (struct list
* subq
, u_char qindex
)
1728 struct listnode
*lnode
= listhead (subq
);
1729 struct route_node
*rnode
;
1734 rnode
= listgetdata (lnode
);
1735 rib_process (rnode
);
1738 UNSET_FLAG (rib_dest_from_rnode (rnode
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1743 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1744 __func__
, rnode
, rnode
->lock
);
1745 zlog_backtrace(LOG_DEBUG
);
1748 route_unlock_node (rnode
);
1749 list_delete_node (subq
, lnode
);
1754 * All meta queues have been processed. Trigger next-hop evaluation.
1757 meta_queue_process_complete (struct work_queue
*dummy
)
1759 zebra_evaluate_rnh(0, AF_INET
, 0, RNH_NEXTHOP_TYPE
, NULL
);
1760 zebra_evaluate_rnh(0, AF_INET
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
1762 zebra_evaluate_rnh(0, AF_INET6
, 0, RNH_NEXTHOP_TYPE
, NULL
);
1763 zebra_evaluate_rnh(0, AF_INET6
, 0, RNH_IMPORT_CHECK_TYPE
, NULL
);
1764 #endif /* HAVE_IPV6 */
1767 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1768 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1769 * is pointed to the meta queue structure.
1771 static wq_item_status
1772 meta_queue_process (struct work_queue
*dummy
, void *data
)
1774 struct meta_queue
* mq
= data
;
1777 for (i
= 0; i
< MQ_SIZE
; i
++)
1778 if (process_subq (mq
->subq
[i
], i
))
1783 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1787 * Map from rib types to queue type (priority) in meta queue
1789 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1790 [ZEBRA_ROUTE_SYSTEM
] = 4,
1791 [ZEBRA_ROUTE_KERNEL
] = 0,
1792 [ZEBRA_ROUTE_CONNECT
] = 0,
1793 [ZEBRA_ROUTE_STATIC
] = 1,
1794 [ZEBRA_ROUTE_RIP
] = 2,
1795 [ZEBRA_ROUTE_RIPNG
] = 2,
1796 [ZEBRA_ROUTE_OSPF
] = 2,
1797 [ZEBRA_ROUTE_OSPF6
] = 2,
1798 [ZEBRA_ROUTE_ISIS
] = 2,
1799 [ZEBRA_ROUTE_BGP
] = 3,
1800 [ZEBRA_ROUTE_HSLS
] = 4,
1801 [ZEBRA_ROUTE_BABEL
] = 2,
1802 [ZEBRA_ROUTE_TABLE
] = 1,
1805 /* Look into the RN and queue it into one or more priority queues,
1806 * increasing the size for each data push done.
1809 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
1812 char buf
[INET6_ADDRSTRLEN
];
1814 if (IS_ZEBRA_DEBUG_RIB_Q
)
1815 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1817 RNODE_FOREACH_RIB (rn
, rib
)
1819 u_char qindex
= meta_queue_map
[rib
->type
];
1821 /* Invariant: at this point we always have rn->info set. */
1822 if (CHECK_FLAG (rib_dest_from_rnode (rn
)->flags
,
1823 RIB_ROUTE_QUEUED (qindex
)))
1825 if (IS_ZEBRA_DEBUG_RIB_Q
)
1826 zlog_debug ("%s: %s/%d: rn %p is already queued in sub-queue %u",
1827 __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1831 SET_FLAG (rib_dest_from_rnode (rn
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1832 listnode_add (mq
->subq
[qindex
], rn
);
1833 route_lock_node (rn
);
1836 if (IS_ZEBRA_DEBUG_RIB_Q
)
1837 zlog_debug ("%s: %s/%d: queued rn %p into sub-queue %u",
1838 __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1842 /* Add route_node to work queue and schedule processing */
1844 rib_queue_add (struct zebra_t
*zebra
, struct route_node
*rn
)
1846 char buf
[INET_ADDRSTRLEN
];
1847 assert (zebra
&& rn
);
1849 if (IS_ZEBRA_DEBUG_RIB_Q
)
1850 inet_ntop (AF_INET
, &rn
->p
.u
.prefix
, buf
, INET_ADDRSTRLEN
);
1852 /* Pointless to queue a route_node with no RIB entries to add or remove */
1853 if (!rnode_to_ribs (rn
))
1855 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1856 __func__
, rn
, rn
->lock
);
1857 zlog_backtrace(LOG_DEBUG
);
1861 if (IS_ZEBRA_DEBUG_RIB_Q
)
1862 zlog_info ("%s: %s/%d: work queue added", __func__
, buf
, rn
->p
.prefixlen
);
1866 if (zebra
->ribq
== NULL
)
1868 zlog_err ("%s: work_queue does not exist!", __func__
);
1873 * The RIB queue should normally be either empty or holding the only
1874 * work_queue_item element. In the latter case this element would
1875 * hold a pointer to the meta queue structure, which must be used to
1876 * actually queue the route nodes to process. So create the MQ
1877 * holder, if necessary, then push the work into it in any case.
1878 * This semantics was introduced after 0.99.9 release.
1880 if (!zebra
->ribq
->items
->count
)
1881 work_queue_add (zebra
->ribq
, zebra
->mq
);
1883 rib_meta_queue_add (zebra
->mq
, rn
);
1885 if (IS_ZEBRA_DEBUG_RIB_Q
)
1886 zlog_debug ("%s: %s/%d: rn %p queued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1891 /* Create new meta queue.
1892 A destructor function doesn't seem to be necessary here.
1894 static struct meta_queue
*
1895 meta_queue_new (void)
1897 struct meta_queue
*new;
1900 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
1903 for (i
= 0; i
< MQ_SIZE
; i
++)
1905 new->subq
[i
] = list_new ();
1906 assert(new->subq
[i
]);
1912 /* initialise zebra rib work queue */
1914 rib_queue_init (struct zebra_t
*zebra
)
1918 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1919 "route_node processing")))
1921 zlog_err ("%s: could not initialise work queue!", __func__
);
1925 /* fill in the work queue spec */
1926 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1927 zebra
->ribq
->spec
.errorfunc
= NULL
;
1928 zebra
->ribq
->spec
.completion_func
= &meta_queue_process_complete
;
1929 /* XXX: TODO: These should be runtime configurable via vty */
1930 zebra
->ribq
->spec
.max_retries
= 3;
1931 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
1933 if (!(zebra
->mq
= meta_queue_new ()))
1935 zlog_err ("%s: could not initialise meta queue!", __func__
);
1941 /* RIB updates are processed via a queue of pointers to route_nodes.
1943 * The queue length is bounded by the maximal size of the routing table,
1944 * as a route_node will not be requeued, if already queued.
1946 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
1947 * state, or static_install_ipv{4,6} (when an existing RIB is updated)
1948 * and then submit route_node to queue for best-path selection later.
1949 * Order of add/delete state changes are preserved for any given RIB.
1951 * Deleted RIBs are reaped during best-path selection.
1954 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
1955 * |-------->| | best RIB, if required
1957 * static_install->|->rib_addqueue...... -> rib_process
1959 * |-------->| |-> rib_unlink
1960 * |-> set RIB_ENTRY_REMOVE |
1961 * rib_delnode (RIB freed)
1963 * The 'info' pointer of a route_node points to a rib_dest_t
1964 * ('dest'). Queueing state for a route_node is kept on the dest. The
1965 * dest is created on-demand by rib_link() and is kept around at least
1966 * as long as there are ribs hanging off it (@see rib_gc_dest()).
1968 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1970 * - route_nodes: refcounted by:
1971 * - dest attached to route_node:
1972 * - managed by: rib_link/rib_gc_dest
1973 * - route_node processing queue
1974 * - managed by: rib_addqueue, rib_process.
1978 /* Add RIB to head of the route node. */
1980 rib_link (struct route_node
*rn
, struct rib
*rib
)
1984 char buf
[INET6_ADDRSTRLEN
];
1989 if (IS_ZEBRA_DEBUG_RIB
)
1991 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1992 zlog_debug ("%s: %s/%d: rn %p, rib %p", __func__
,
1993 buf
, rn
->p
.prefixlen
, rn
, rib
);
1996 dest
= rib_dest_from_rnode (rn
);
1999 if (IS_ZEBRA_DEBUG_RIB
)
2001 zlog_debug ("%s: %s/%d: adding dest to table", __func__
,
2002 buf
, rn
->p
.prefixlen
);
2005 dest
= XCALLOC (MTYPE_RIB_DEST
, sizeof (rib_dest_t
));
2006 route_lock_node (rn
); /* rn route table reference */
2011 head
= dest
->routes
;
2019 /* Further processing only if entry is in main table */
2020 if ((rib
->table
== RT_TABLE_MAIN
) || (rib
->table
== zebrad
.rtm_table_default
))
2021 rib_queue_add (&zebrad
, rn
);
2024 if (IS_ZEBRA_DEBUG_RIB
)
2026 zlog_debug ("%s: %s/%d: Skipping further RIB processing of non-main table",
2027 __func__
, buf
, rn
->p
.prefixlen
);
2029 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2030 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2031 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2032 zebra_add_import_table_entry(rn
, rib
);
2037 rib_addnode (struct route_node
*rn
, struct rib
*rib
)
2039 /* RIB node has been un-removed before route-node is processed.
2040 * route_node must hence already be on the queue for processing..
2042 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2044 if (IS_ZEBRA_DEBUG_RIB
)
2046 char buf
[INET6_ADDRSTRLEN
];
2047 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2048 zlog_debug ("%s: %s/%d: rn %p, un-removed rib %p",
2049 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
2051 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2060 * Detach a rib structure from a route_node.
2062 * Note that a call to rib_unlink() should be followed by a call to
2063 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
2064 * longer required to be deleted.
2067 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
2069 char buf
[INET6_ADDRSTRLEN
];
2074 if (IS_ZEBRA_DEBUG_RIB
)
2076 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2077 zlog_debug ("%s: %s/%d: rn %p, rib %p",
2078 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
2081 dest
= rib_dest_from_rnode (rn
);
2084 rib
->next
->prev
= rib
->prev
;
2087 rib
->prev
->next
= rib
->next
;
2090 dest
->routes
= rib
->next
;
2093 /* free RIB and nexthops */
2094 nexthops_free(rib
->nexthop
, rn
);
2095 XFREE (MTYPE_RIB
, rib
);
2100 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
2104 if (IS_ZEBRA_DEBUG_RIB
)
2106 char buf
[INET6_ADDRSTRLEN
];
2107 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
2108 zlog_debug ("%s: %s/%d: rn %p, rib %p, removing", __func__
,
2109 buf
, rn
->p
.prefixlen
, rn
, rib
);
2111 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
2113 if ((rib
->table
== RT_TABLE_MAIN
) || (rib
->table
== zebrad
.rtm_table_default
))
2114 rib_queue_add (&zebrad
, rn
);
2117 afi
= (rn
->p
.family
== AF_INET
) ? AFI_IP
:
2118 (rn
->p
.family
== AF_INET6
) ? AFI_IP6
: AFI_MAX
;
2119 if (is_zebra_import_table_enabled (afi
, rib
->table
))
2120 zebra_del_import_table_entry(rn
, rib
);
2121 /* Just clean up if non main table */
2122 rib_unlink(rn
, rib
);
2127 rib_add_ipv4 (int type
, u_short instance
, int flags
, struct prefix_ipv4
*p
,
2128 struct in_addr
*gate
, struct in_addr
*src
,
2129 unsigned int ifindex
, u_int32_t table_id
,
2130 u_int32_t metric
, u_char distance
, safi_t safi
)
2133 struct rib
*same
= NULL
;
2134 struct route_table
*table
;
2135 struct route_node
*rn
;
2136 struct nexthop
*nexthop
;
2139 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
2141 table
= vrf_table (AFI_IP
, safi
, 0);
2145 table
= vrf_other_route_table (AFI_IP
, table_id
, 0);
2150 /* Make it sure prefixlen is applied to the prefix. */
2151 apply_mask_ipv4 (p
);
2153 /* Set default distance by route type. */
2156 if ((unsigned)type
>= array_size(route_info
))
2159 distance
= route_info
[type
].distance
;
2161 /* iBGP distance is 200. */
2162 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2166 /* Lookup route node.*/
2167 rn
= route_node_get (table
, (struct prefix
*) p
);
2169 /* If same type of route are installed, treat it as a implicit
2171 RNODE_FOREACH_RIB (rn
, rib
)
2173 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2176 if (rib
->type
!= type
)
2178 if (rib
->instance
!= instance
)
2181 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2186 /* Duplicate connected route comes in. */
2187 else if ((nexthop
= rib
->nexthop
) &&
2188 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2189 nexthop
->ifindex
== ifindex
&&
2190 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2197 /* Allocate new rib structure. */
2198 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2200 rib
->instance
= instance
;
2201 rib
->distance
= distance
;
2203 rib
->metric
= metric
;
2204 rib
->table
= table_id
;
2205 rib
->nexthop_num
= 0;
2206 rib
->uptime
= time (NULL
);
2208 /* Nexthop settings. */
2212 nexthop_ipv4_ifindex_add (rib
, gate
, src
, ifindex
);
2214 nexthop_ipv4_add (rib
, gate
, src
);
2217 nexthop_ifindex_add (rib
, ifindex
);
2219 /* If this route is kernel route, set FIB flag to the route. */
2220 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2221 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2222 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2224 /* Link new rib to node.*/
2225 if (IS_ZEBRA_DEBUG_RIB
)
2227 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
2229 rib_dump ((struct prefix
*)p
, rib
);
2231 rib_addnode (rn
, rib
);
2233 /* Free implicit route.*/
2236 if (IS_ZEBRA_DEBUG_RIB
)
2237 zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__
, rn
, rib
);
2238 rib_delnode (rn
, same
);
2241 route_unlock_node (rn
);
2245 /* This function dumps the contents of a given RIB entry into
2246 * standard debug log. Calling function name and IP prefix in
2247 * question are passed as 1st and 2nd arguments.
2250 void _rib_dump (const char * func
,
2251 union prefix46constptr pp
, const struct rib
* rib
)
2253 const struct prefix
*p
= pp
.p
;
2254 char straddr
[INET6_ADDRSTRLEN
];
2255 struct nexthop
*nexthop
, *tnexthop
;
2258 inet_ntop (p
->family
, &p
->u
.prefix
, straddr
, INET6_ADDRSTRLEN
);
2259 zlog_debug ("%s: dumping RIB entry %p for %s/%d", func
, rib
, straddr
, p
->prefixlen
);
2262 "%s: refcnt == %lu, uptime == %lu, type == %u, instance == %d, table == %d",
2265 (unsigned long) rib
->uptime
,
2272 "%s: metric == %u, distance == %u, flags == %u, status == %u",
2281 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
2284 rib
->nexthop_active_num
,
2285 rib
->nexthop_fib_num
2288 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2290 inet_ntop (p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
2293 "%s: %s %s with flags %s%s%s",
2295 (recursing
? " NH" : "NH"),
2297 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
2298 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
2299 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
2302 zlog_debug ("%s: dump complete", func
);
2305 /* This is an exported helper to rtm_read() to dump the strange
2306 * RIB entry found by rib_lookup_ipv4_route()
2309 void rib_lookup_and_dump (struct prefix_ipv4
* p
)
2311 struct route_table
*table
;
2312 struct route_node
*rn
;
2314 char prefix_buf
[INET_ADDRSTRLEN
];
2317 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2320 zlog_err ("%s: vrf_table() returned NULL", __func__
);
2324 inet_ntop (AF_INET
, &p
->prefix
.s_addr
, prefix_buf
, INET_ADDRSTRLEN
);
2325 /* Scan the RIB table for exactly matching RIB entry. */
2326 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2328 /* No route for this prefix. */
2331 zlog_debug ("%s: lookup failed for %s/%d", __func__
, prefix_buf
, p
->prefixlen
);
2336 route_unlock_node (rn
);
2339 RNODE_FOREACH_RIB (rn
, rib
)
2343 "%s: rn %p, rib %p: %s, %s",
2347 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
2348 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
2354 /* Check if requested address assignment will fail due to another
2355 * route being installed by zebra in FIB already. Take necessary
2356 * actions, if needed: remove such a route from FIB and deSELECT
2357 * corresponding RIB entry. Then put affected RN into RIBQ head.
2359 void rib_lookup_and_pushup (struct prefix_ipv4
* p
)
2361 struct route_table
*table
;
2362 struct route_node
*rn
;
2364 unsigned changed
= 0;
2366 if (NULL
== (table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0)))
2368 zlog_err ("%s: vrf_table() returned NULL", __func__
);
2372 /* No matches would be the simplest case. */
2373 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
2377 route_unlock_node (rn
);
2379 /* Check all RIB entries. In case any changes have to be done, requeue
2380 * the RN into RIBQ head. If the routing message about the new connected
2381 * route (generated by the IP address we are going to assign very soon)
2382 * comes before the RIBQ is processed, the new RIB entry will join
2383 * RIBQ record already on head. This is necessary for proper revalidation
2384 * of the rest of the RIB.
2386 RNODE_FOREACH_RIB (rn
, rib
)
2388 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) &&
2389 ! RIB_SYSTEM_ROUTE (rib
))
2392 if (IS_ZEBRA_DEBUG_RIB
)
2394 char buf
[INET_ADDRSTRLEN
];
2395 inet_ntop (rn
->p
.family
, &p
->prefix
, buf
, INET_ADDRSTRLEN
);
2396 zlog_debug ("%s: freeing way for connected prefix %s/%d", __func__
, buf
, p
->prefixlen
);
2397 rib_dump (&rn
->p
, rib
);
2399 rib_uninstall (rn
, rib
);
2403 rib_queue_add (&zebrad
, rn
);
2407 rib_add_ipv4_multipath (struct prefix_ipv4
*p
, struct rib
*rib
, safi_t safi
)
2409 struct route_table
*table
;
2410 struct route_node
*rn
;
2412 struct nexthop
*nexthop
;
2416 if ((rib
->table
== zebrad
.rtm_table_default
) || (rib
->table
== RT_TABLE_MAIN
))
2418 table
= vrf_table (AFI_IP
, safi
, 0);
2422 table
= vrf_other_route_table (AFI_IP
, rib
->table
, 0);
2427 /* Make it sure prefixlen is applied to the prefix. */
2428 apply_mask_ipv4 (p
);
2430 /* Set default distance by route type. */
2431 if (rib
->distance
== 0)
2433 rib
->distance
= route_info
[rib
->type
].distance
;
2435 /* iBGP distance is 200. */
2436 if (rib
->type
== ZEBRA_ROUTE_BGP
2437 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
2438 rib
->distance
= 200;
2441 /* Lookup route node.*/
2442 rn
= route_node_get (table
, (struct prefix
*) p
);
2444 /* If same type of route are installed, treat it as a implicit
2446 RNODE_FOREACH_RIB (rn
, same
)
2448 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
2451 if (same
->type
== rib
->type
&& same
->instance
== rib
->instance
2452 && same
->table
== rib
->table
2453 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
2457 /* If this route is kernel route, set FIB flag to the route. */
2458 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
2459 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2460 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2462 /* Link new rib to node.*/
2463 rib_addnode (rn
, rib
);
2465 if (IS_ZEBRA_DEBUG_RIB
)
2467 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
2472 /* Free implicit route.*/
2475 if (IS_ZEBRA_DEBUG_RIB
)
2477 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
2478 __func__
, rn
, same
);
2481 rib_delnode (rn
, same
);
2485 route_unlock_node (rn
);
2489 /* XXX factor with rib_delete_ipv6 */
2491 rib_delete_ipv4 (int type
, u_short instance
, int flags
, struct prefix_ipv4
*p
,
2492 struct in_addr
*gate
, unsigned int ifindex
,
2493 u_int32_t table_id
, safi_t safi
)
2495 struct route_table
*table
;
2496 struct route_node
*rn
;
2498 struct rib
*fib
= NULL
;
2499 struct rib
*same
= NULL
;
2500 struct nexthop
*nexthop
, *tnexthop
;
2502 char buf1
[INET_ADDRSTRLEN
];
2503 char buf2
[INET_ADDRSTRLEN
];
2506 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
2508 table
= vrf_table (AFI_IP
, safi
, 0);
2512 table
= vrf_other_route_table(AFI_IP
, table_id
, 0);
2518 apply_mask_ipv4 (p
);
2520 if (IS_ZEBRA_DEBUG_KERNEL
)
2523 zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
2524 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2529 zlog_debug ("rib_delete_ipv4(): route delete %s/%d ifindex %d",
2530 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2535 /* Lookup route node. */
2536 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2539 if (IS_ZEBRA_DEBUG_KERNEL
)
2542 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
2543 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2545 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
2548 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
2549 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2553 return ZEBRA_ERR_RTNOEXIST
;
2556 /* Lookup same type route. */
2557 RNODE_FOREACH_RIB (rn
, rib
)
2559 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2562 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2565 if (rib
->type
!= type
)
2567 if (rib
->instance
!= instance
)
2569 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2570 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2572 if (nexthop
->ifindex
!= ifindex
)
2577 route_unlock_node (rn
);
2578 route_unlock_node (rn
);
2584 /* Make sure that the route found has the same gateway. */
2592 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2593 if (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
))
2602 /* If same type of route can't be found and this message is from
2606 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
&&
2607 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
))
2609 if (IS_ZEBRA_DEBUG_KERNEL
)
2611 zlog_debug ("Zebra route %s/%d was deleted by others from kernel",
2612 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2615 /* This means someone else, other than Zebra, has deleted
2616 * a Zebra router from the kernel. We will add it back */
2617 rib_install_kernel(rn
, fib
);
2621 if (IS_ZEBRA_DEBUG_KERNEL
)
2624 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
2625 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2627 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
2631 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
2632 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2637 route_unlock_node (rn
);
2638 return ZEBRA_ERR_RTNOEXIST
;
2643 rib_delnode (rn
, same
);
2645 route_unlock_node (rn
);
2649 /* Install static route into rib. */
2651 static_install_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
2654 struct route_node
*rn
;
2655 struct route_table
*table
;
2659 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2663 /* Lookup existing route */
2664 rn
= route_node_get (table
, p
);
2665 RNODE_FOREACH_RIB (rn
, rib
)
2667 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2670 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2676 /* if tag value changed , update old value in RIB */
2677 if (rib
->tag
!= si
->tag
)
2680 /* Same distance static route is there. Update it with new
2682 route_unlock_node (rn
);
2685 case STATIC_IPV4_GATEWAY
:
2686 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2687 nh_p
.family
= AF_INET
;
2688 nh_p
.prefixlen
= IPV4_MAX_BITLEN
;
2689 nh_p
.u
.prefix4
= si
->gate
.ipv4
;
2690 zebra_register_rnh_static_nh(&nh_p
, rn
);
2692 case STATIC_IPV4_IFNAME
:
2693 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2695 case STATIC_IPV4_BLACKHOLE
:
2696 nexthop_blackhole_add (rib
);
2699 rib_queue_add (&zebrad
, rn
);
2703 /* This is new static route. */
2704 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2706 rib
->type
= ZEBRA_ROUTE_STATIC
;
2708 rib
->distance
= si
->distance
;
2710 rib
->table
= zebrad
.rtm_table_default
;
2711 rib
->nexthop_num
= 0;
2716 case STATIC_IPV4_GATEWAY
:
2717 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2718 nh_p
.family
= AF_INET
;
2719 nh_p
.prefixlen
= IPV4_MAX_BITLEN
;
2720 nh_p
.u
.prefix4
= si
->gate
.ipv4
;
2721 zebra_register_rnh_static_nh(&nh_p
, rn
);
2723 case STATIC_IPV4_IFNAME
:
2724 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2726 case STATIC_IPV4_BLACKHOLE
:
2727 nexthop_blackhole_add (rib
);
2731 /* Save the flags of this static routes (reject, blackhole) */
2732 rib
->flags
= si
->flags
;
2734 /* Link this rib to the tree. */
2735 rib_addnode (rn
, rib
);
2740 static_ipv4_nexthop_same (struct nexthop
*nexthop
, struct static_ipv4
*si
)
2742 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
2743 && si
->type
== STATIC_IPV4_GATEWAY
2744 && IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &si
->gate
.ipv4
))
2746 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2747 && si
->type
== STATIC_IPV4_IFNAME
2748 && strcmp (nexthop
->ifname
, si
->gate
.ifname
) == 0)
2750 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
2751 && si
->type
== STATIC_IPV4_BLACKHOLE
)
2756 /* Uninstall static route from RIB. */
2758 static_uninstall_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
2760 struct route_node
*rn
;
2762 struct nexthop
*nexthop
;
2763 struct route_table
*table
;
2767 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2771 /* Lookup existing route with type and distance. */
2772 rn
= route_node_lookup (table
, p
);
2776 RNODE_FOREACH_RIB (rn
, rib
)
2778 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2781 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
&&
2782 rib
->tag
== si
->tag
)
2788 route_unlock_node (rn
);
2792 /* Lookup nexthop. */
2793 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2794 if (static_ipv4_nexthop_same (nexthop
, si
))
2797 /* Can't find nexthop. */
2800 route_unlock_node (rn
);
2804 /* Check nexthop. */
2805 if (rib
->nexthop_num
== 1)
2806 rib_delnode (rn
, rib
);
2809 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2810 rib_uninstall (rn
, rib
);
2812 nh_p
.family
= AF_INET
;
2813 nh_p
.prefixlen
= IPV4_MAX_BITLEN
;
2814 nh_p
.u
.prefix4
= nexthop
->gate
.ipv4
;
2815 nexthop_delete (rib
, nexthop
);
2816 zebra_deregister_rnh_static_nh(&nh_p
, rn
);
2817 nexthop_free (nexthop
, rn
);
2818 rib_queue_add (&zebrad
, rn
);
2821 route_unlock_node (rn
);
2824 /* Add static route into static route configuration. */
2826 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2827 u_char flags
, u_short tag
, u_char distance
, u_int32_t vrf_id
)
2830 struct route_node
*rn
;
2831 struct static_ipv4
*si
;
2832 struct static_ipv4
*pp
;
2833 struct static_ipv4
*cp
;
2834 struct static_ipv4
*update
= NULL
;
2835 struct route_table
*stable
;
2838 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2842 /* Lookup static route prefix. */
2843 rn
= route_node_get (stable
, p
);
2847 type
= STATIC_IPV4_GATEWAY
;
2849 type
= STATIC_IPV4_IFNAME
;
2851 type
= STATIC_IPV4_BLACKHOLE
;
2853 /* Do nothing if there is a same static route. */
2854 for (si
= rn
->info
; si
; si
= si
->next
)
2856 if (type
== si
->type
2857 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2858 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2860 if ((distance
== si
->distance
) && (tag
== si
->tag
))
2862 route_unlock_node (rn
);
2870 /* Distance or tag changed. */
2872 static_delete_ipv4 (p
, gate
, ifname
, update
->tag
, update
->distance
, vrf_id
);
2874 /* Make new static route structure. */
2875 si
= XCALLOC (MTYPE_STATIC_IPV4
, sizeof (struct static_ipv4
));
2878 si
->distance
= distance
;
2883 si
->gate
.ipv4
= *gate
;
2885 si
->gate
.ifname
= XSTRDUP (0, ifname
);
2887 /* Add new static route information to the tree with sort by
2888 distance value and gateway address. */
2889 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2891 if (si
->distance
< cp
->distance
)
2893 if (si
->distance
> cp
->distance
)
2895 if (si
->type
== STATIC_IPV4_GATEWAY
&& cp
->type
== STATIC_IPV4_GATEWAY
)
2897 if (ntohl (si
->gate
.ipv4
.s_addr
) < ntohl (cp
->gate
.ipv4
.s_addr
))
2899 if (ntohl (si
->gate
.ipv4
.s_addr
) > ntohl (cp
->gate
.ipv4
.s_addr
))
2904 /* Make linked list. */
2914 /* Install into rib. */
2915 static_install_ipv4 (p
, si
);
2920 /* Delete static route from static route configuration. */
2922 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2923 u_short tag
, u_char distance
, u_int32_t vrf_id
)
2926 struct route_node
*rn
;
2927 struct static_ipv4
*si
;
2928 struct route_table
*stable
;
2931 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2935 /* Lookup static route prefix. */
2936 rn
= route_node_lookup (stable
, p
);
2942 type
= STATIC_IPV4_GATEWAY
;
2944 type
= STATIC_IPV4_IFNAME
;
2946 type
= STATIC_IPV4_BLACKHOLE
;
2948 /* Find same static route is the tree */
2949 for (si
= rn
->info
; si
; si
= si
->next
)
2950 if (type
== si
->type
2951 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2952 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0)
2953 && (! tag
|| (tag
== si
->tag
)))
2956 /* Can't find static route. */
2959 route_unlock_node (rn
);
2963 /* Install into rib. */
2964 static_uninstall_ipv4 (p
, si
);
2966 /* Unlink static route from linked list. */
2968 si
->prev
->next
= si
->next
;
2970 rn
->info
= si
->next
;
2972 si
->next
->prev
= si
->prev
;
2973 route_unlock_node (rn
);
2975 /* Free static route configuration. */
2977 XFREE (0, si
->gate
.ifname
);
2978 XFREE (MTYPE_STATIC_IPV4
, si
);
2980 route_unlock_node (rn
);
2988 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
2989 struct in6_addr
*gate
, unsigned int ifindex
, int table
)
2991 if (type
== ZEBRA_ROUTE_CONNECT
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)) {
2992 #if defined (MUSICA) || defined (LINUX)
2993 /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
2994 if (p
->prefixlen
== 96)
2999 if (type
== ZEBRA_ROUTE_KERNEL
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)
3000 && p
->prefixlen
== 96 && gate
&& IN6_IS_ADDR_UNSPECIFIED (gate
))
3002 kernel_delete_ipv6_old (p
, gate
, ifindex
, 0, table
);
3009 rib_add_ipv6 (int type
, u_short instance
, int flags
, struct prefix_ipv6
*p
,
3010 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t table_id
,
3011 u_int32_t metric
, u_char distance
, safi_t safi
)
3014 struct rib
*same
= NULL
;
3015 struct route_table
*table
;
3016 struct route_node
*rn
;
3017 struct nexthop
*nexthop
;
3020 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
3022 table
= vrf_table (AFI_IP6
, safi
, 0);
3026 table
= vrf_other_route_table(AFI_IP6
, table_id
, 0);
3031 /* Make sure mask is applied. */
3032 apply_mask_ipv6 (p
);
3034 /* Set default distance by route type. */
3036 distance
= route_info
[type
].distance
;
3038 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
3041 /* Filter bogus route. */
3042 if (rib_bogus_ipv6 (type
, p
, gate
, ifindex
, 0))
3045 /* Lookup route node.*/
3046 rn
= route_node_get (table
, (struct prefix
*) p
);
3048 /* If same type of route are installed, treat it as a implicit
3050 RNODE_FOREACH_RIB (rn
, rib
)
3052 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3055 if (rib
->type
!= type
)
3057 if (rib
->instance
!= instance
)
3059 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
3064 else if ((nexthop
= rib
->nexthop
) &&
3065 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
3066 nexthop
->ifindex
== ifindex
)
3073 /* Allocate new rib structure. */
3074 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
3077 rib
->instance
= instance
;
3078 rib
->distance
= distance
;
3080 rib
->metric
= metric
;
3081 rib
->table
= table_id
;
3082 rib
->nexthop_num
= 0;
3083 rib
->uptime
= time (NULL
);
3085 /* Nexthop settings. */
3089 nexthop_ipv6_ifindex_add (rib
, gate
, ifindex
);
3091 nexthop_ipv6_add (rib
, gate
);
3094 nexthop_ifindex_add (rib
, ifindex
);
3096 /* If this route is kernel route, set FIB flag to the route. */
3097 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
3098 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
3099 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3101 /* Link new rib to node.*/
3102 rib_addnode (rn
, rib
);
3103 if (IS_ZEBRA_DEBUG_RIB
)
3105 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
3110 /* Free implicit route.*/
3113 if (IS_ZEBRA_DEBUG_RIB
)
3115 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
3116 __func__
, rn
, same
);
3119 rib_delnode (rn
, same
);
3122 route_unlock_node (rn
);
3127 rib_add_ipv6_multipath (struct prefix_ipv6
*p
, struct rib
*rib
, safi_t safi
,
3128 unsigned long ifindex
)
3130 struct route_table
*table
;
3131 struct route_node
*rn
;
3132 struct rib
*same
= NULL
;
3133 struct nexthop
*nexthop
;
3138 table_id
= rib
->table
;
3140 return 0; /* why are we getting called with NULL rib */
3143 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
3145 table
= vrf_table (AFI_IP6
, safi
, 0);
3149 table
= vrf_other_route_table(AFI_IP6
, table_id
, 0);
3155 /* Make sure mask is applied. */
3156 apply_mask_ipv6 (p
);
3158 /* Set default distance by route type. */
3159 if (rib
->distance
== 0)
3161 rib
->distance
= route_info
[rib
->type
].distance
;
3163 /* iBGP distance is 200. */
3164 if (rib
->type
== ZEBRA_ROUTE_BGP
3165 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
3166 rib
->distance
= 200;
3169 /* Lookup route node.*/
3170 rn
= route_node_get (table
, (struct prefix
*) p
);
3172 /* If same type of route are installed, treat it as a implicit
3174 RNODE_FOREACH_RIB (rn
, same
) {
3175 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
)) {
3178 if (same
->type
!= rib
->type
) {
3182 if (same
->instance
!= rib
->instance
) {
3186 if (same
->table
!= rib
->table
) {
3189 if (same
->type
!= ZEBRA_ROUTE_CONNECT
) {
3192 else if ((nexthop
= same
->nexthop
) &&
3193 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
3194 nexthop
->ifindex
== ifindex
) {
3200 /* If this route is kernel route, set FIB flag to the route. */
3201 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
) {
3202 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
3203 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
3207 /* Link new rib to node.*/
3208 rib_addnode (rn
, rib
);
3210 /* Free implicit route.*/
3213 if (IS_ZEBRA_DEBUG_RIB
)
3215 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
3216 __func__
, rn
, same
);
3217 rib_dump ((struct prefix
*)p
, same
);
3219 rib_delnode (rn
, same
);
3223 route_unlock_node (rn
);
3227 /* XXX factor with rib_delete_ipv6 */
3229 rib_delete_ipv6 (int type
, u_short instance
, int flags
, struct prefix_ipv6
*p
,
3230 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t table_id
, safi_t safi
)
3232 struct route_table
*table
;
3233 struct route_node
*rn
;
3235 struct rib
*fib
= NULL
;
3236 struct rib
*same
= NULL
;
3237 struct nexthop
*nexthop
, *tnexthop
;
3239 char buf1
[INET6_ADDRSTRLEN
];
3240 char buf2
[INET6_ADDRSTRLEN
];
3243 apply_mask_ipv6 (p
);
3246 if ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
))
3248 table
= vrf_table (AFI_IP6
, safi
, 0);
3252 table
= vrf_other_route_table(AFI_IP6
, table_id
, 0);
3257 /* Lookup route node. */
3258 rn
= route_node_lookup (table
, (struct prefix
*) p
);
3261 if (IS_ZEBRA_DEBUG_KERNEL
)
3264 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
3265 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
3267 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
3270 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
3271 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
3275 return ZEBRA_ERR_RTNOEXIST
;
3278 /* Lookup same type route. */
3279 RNODE_FOREACH_RIB (rn
, rib
)
3281 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
3284 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
3287 if (rib
->type
!= type
)
3289 if (rib
->instance
!= instance
)
3291 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
3292 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
3294 if (nexthop
->ifindex
!= ifindex
)
3299 route_unlock_node (rn
);
3300 route_unlock_node (rn
);
3306 /* Make sure that the route found has the same gateway. */
3314 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
3315 if (IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
))
3325 /* If same type of route can't be found and this message is from
3329 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
&&
3330 CHECK_FLAG(flags
, ZEBRA_FLAG_SELFROUTE
))
3332 if (IS_ZEBRA_DEBUG_KERNEL
)
3334 zlog_debug ("Zebra route %s/%d was deleted by others from kernel",
3335 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
3338 /* This means someone else, other than Zebra, has deleted a Zebra
3339 * route from the kernel. We will add it back */
3340 rib_install_kernel(rn
, fib
);
3344 if (IS_ZEBRA_DEBUG_KERNEL
)
3347 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
3348 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
3350 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
3354 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
3355 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
3360 route_unlock_node (rn
);
3361 return ZEBRA_ERR_RTNOEXIST
;
3366 rib_delnode (rn
, same
);
3368 route_unlock_node (rn
);
3372 /* Install static route into rib. */
3374 static_install_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
3377 struct route_table
*table
;
3378 struct route_node
*rn
;
3382 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
3386 /* Lookup existing route */
3387 rn
= route_node_get (table
, p
);
3388 RNODE_FOREACH_RIB (rn
, rib
)
3390 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
3393 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
3399 /* if tag value changed , update old value in RIB */
3400 if (rib
->tag
!= si
->tag
)
3403 /* Same distance static route is there. Update it with new
3405 route_unlock_node (rn
);
3409 case STATIC_IPV6_GATEWAY
:
3410 nexthop_ipv6_add (rib
, &si
->ipv6
);
3411 nh_p
.family
= AF_INET6
;
3412 nh_p
.prefixlen
= IPV6_MAX_BITLEN
;
3413 nh_p
.u
.prefix6
= si
->ipv6
;
3414 zebra_register_rnh_static_nh(&nh_p
, rn
);
3416 case STATIC_IPV6_IFNAME
:
3417 nexthop_ifname_add (rib
, si
->ifname
);
3419 case STATIC_IPV6_GATEWAY_IFNAME
:
3420 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
3423 rib_queue_add (&zebrad
, rn
);
3427 /* This is new static route. */
3428 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
3430 rib
->type
= ZEBRA_ROUTE_STATIC
;
3432 rib
->distance
= si
->distance
;
3434 rib
->table
= zebrad
.rtm_table_default
;
3435 rib
->nexthop_num
= 0;
3440 case STATIC_IPV6_GATEWAY
:
3441 nexthop_ipv6_add (rib
, &si
->ipv6
);
3442 nh_p
.family
= AF_INET6
;
3443 nh_p
.prefixlen
= IPV6_MAX_BITLEN
;
3444 nh_p
.u
.prefix6
= si
->ipv6
;
3445 zebra_register_rnh_static_nh(&nh_p
, rn
);
3447 case STATIC_IPV6_IFNAME
:
3448 nexthop_ifname_add (rib
, si
->ifname
);
3450 case STATIC_IPV6_GATEWAY_IFNAME
:
3451 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
3455 /* Save the flags of this static routes (reject, blackhole) */
3456 rib
->flags
= si
->flags
;
3458 /* Link this rib to the tree. */
3459 rib_addnode (rn
, rib
);
3464 static_ipv6_nexthop_same (struct nexthop
*nexthop
, struct static_ipv6
*si
)
3466 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
3467 && si
->type
== STATIC_IPV6_GATEWAY
3468 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
))
3470 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
3471 && si
->type
== STATIC_IPV6_IFNAME
3472 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
3474 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
3475 && si
->type
== STATIC_IPV6_GATEWAY_IFNAME
3476 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
)
3477 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
3483 static_uninstall_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
3485 struct route_table
*table
;
3486 struct route_node
*rn
;
3488 struct nexthop
*nexthop
;
3492 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
3496 /* Lookup existing route with type and distance. */
3497 rn
= route_node_lookup (table
, (struct prefix
*) p
);
3501 RNODE_FOREACH_RIB (rn
, rib
)
3503 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3506 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
&&
3507 rib
->tag
== si
->tag
)
3513 route_unlock_node (rn
);
3517 /* Lookup nexthop. */
3518 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
3519 if (static_ipv6_nexthop_same (nexthop
, si
))
3522 /* Can't find nexthop. */
3525 route_unlock_node (rn
);
3529 /* Check nexthop. */
3530 if (rib
->nexthop_num
== 1)
3532 rib_delnode (rn
, rib
);
3536 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3537 rib_uninstall (rn
, rib
);
3539 nh_p
.family
= AF_INET6
;
3540 nh_p
.prefixlen
= IPV6_MAX_BITLEN
;
3541 nh_p
.u
.prefix6
= nexthop
->gate
.ipv6
;
3542 nexthop_delete (rib
, nexthop
);
3543 zebra_deregister_rnh_static_nh(&nh_p
, rn
);
3544 nexthop_free (nexthop
, rn
);
3545 rib_queue_add (&zebrad
, rn
);
3548 route_unlock_node (rn
);
3551 /* Add static route into static route configuration. */
3553 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
3554 const char *ifname
, u_char flags
, u_short tag
,
3555 u_char distance
, u_int32_t vrf_id
)
3557 struct route_node
*rn
;
3558 struct static_ipv6
*si
;
3559 struct static_ipv6
*pp
;
3560 struct static_ipv6
*cp
;
3561 struct route_table
*stable
;
3564 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3569 (type
== STATIC_IPV6_GATEWAY
|| type
== STATIC_IPV6_GATEWAY_IFNAME
))
3573 (type
== STATIC_IPV6_GATEWAY_IFNAME
|| type
== STATIC_IPV6_IFNAME
))
3576 /* Lookup static route prefix. */
3577 rn
= route_node_get (stable
, p
);
3579 /* Do nothing if there is a same static route. */
3580 for (si
= rn
->info
; si
; si
= si
->next
)
3582 if (distance
== si
->distance
3585 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
3586 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
3588 route_unlock_node (rn
);
3593 /* Make new static route structure. */
3594 si
= XCALLOC (MTYPE_STATIC_IPV6
, sizeof (struct static_ipv6
));
3597 si
->distance
= distance
;
3603 case STATIC_IPV6_GATEWAY
:
3606 case STATIC_IPV6_IFNAME
:
3607 si
->ifname
= XSTRDUP (0, ifname
);
3609 case STATIC_IPV6_GATEWAY_IFNAME
:
3611 si
->ifname
= XSTRDUP (0, ifname
);
3615 /* Add new static route information to the tree with sort by
3616 distance value and gateway address. */
3617 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
3619 if (si
->distance
< cp
->distance
)
3621 if (si
->distance
> cp
->distance
)
3625 /* Make linked list. */
3635 /* Install into rib. */
3636 static_install_ipv6 (p
, si
);
3641 /* Delete static route from static route configuration. */
3643 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
3644 const char *ifname
, u_short tag
, u_char distance
,
3647 struct route_node
*rn
;
3648 struct static_ipv6
*si
;
3649 struct route_table
*stable
;
3652 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3656 /* Lookup static route prefix. */
3657 rn
= route_node_lookup (stable
, p
);
3661 /* Find same static route is the tree */
3662 for (si
= rn
->info
; si
; si
= si
->next
)
3663 if (distance
== si
->distance
3665 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
3666 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0)
3667 && (! tag
|| (tag
== si
->tag
)))
3670 /* Can't find static route. */
3673 route_unlock_node (rn
);
3677 /* Install into rib. */
3678 static_uninstall_ipv6 (p
, si
);
3680 /* Unlink static route from linked list. */
3682 si
->prev
->next
= si
->next
;
3684 rn
->info
= si
->next
;
3686 si
->next
->prev
= si
->prev
;
3688 /* Free static route configuration. */
3690 XFREE (0, si
->ifname
);
3691 XFREE (MTYPE_STATIC_IPV6
, si
);
3695 #endif /* HAVE_IPV6 */
3697 /* RIB update function. */
3701 struct route_node
*rn
;
3702 struct route_table
*table
;
3704 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
3706 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3707 if (rnode_to_ribs (rn
))
3708 rib_queue_add (&zebrad
, rn
);
3710 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
3712 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3713 if (rnode_to_ribs (rn
))
3714 rib_queue_add (&zebrad
, rn
);
3718 /* Remove all routes which comes from non main table. */
3720 rib_weed_table (struct route_table
*table
)
3722 struct route_node
*rn
;
3727 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3728 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3730 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3733 if (rib
->table
!= zebrad
.rtm_table_default
&&
3734 rib
->table
!= RT_TABLE_MAIN
)
3735 rib_delnode (rn
, rib
);
3739 /* Delete all routes from non main table. */
3741 rib_weed_tables (void)
3743 rib_weed_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
3744 rib_weed_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3747 /* Delete self installed routes after zebra is relaunched. */
3749 rib_sweep_table (struct route_table
*table
)
3751 struct route_node
*rn
;
3757 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3758 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3760 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3763 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
3764 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
3766 ret
= rib_uninstall_kernel (rn
, rib
);
3768 rib_delnode (rn
, rib
);
3773 /* Sweep all RIB tables. */
3775 rib_sweep_route (void)
3777 rib_sweep_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
3778 rib_sweep_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3781 /* Remove specific by protocol routes from 'table'. */
3782 static unsigned long
3783 rib_score_proto_table (u_char proto
, u_short instance
, struct route_table
*table
)
3785 struct route_node
*rn
;
3788 unsigned long n
= 0;
3791 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3792 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3794 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3796 if (rib
->type
== proto
&& rib
->instance
== instance
)
3798 rib_delnode (rn
, rib
);
3806 /* Remove specific by protocol routes. */
3808 rib_score_proto (u_char proto
, u_short instance
)
3810 return rib_score_proto_table (proto
, instance
, vrf_table (AFI_IP
, SAFI_UNICAST
, 0))
3811 +rib_score_proto_table (proto
, instance
, vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3814 /* Close RIB and clean up kernel routes. */
3816 rib_close_table (struct route_table
*table
)
3818 struct route_node
*rn
;
3822 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3823 RNODE_FOREACH_RIB (rn
, rib
)
3825 if (!CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
3828 zfpm_trigger_update (rn
, NULL
);
3830 if (! RIB_SYSTEM_ROUTE (rib
))
3831 rib_uninstall_kernel (rn
, rib
);
3835 /* Close all RIB tables. */
3839 rib_close_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
3840 rib_close_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3843 /* Routing information base initialize. */
3847 rib_queue_init (&zebrad
);
3848 /* VRF initialization. */
3855 * Get the first vrf id that is greater than the given vrf id if any.
3857 * Returns TRUE if a vrf id was found, FALSE otherwise.
3860 vrf_id_get_next (uint32_t id
, uint32_t *next_id_p
)
3862 while (++id
< vector_active (vrf_vector
))
3864 if (vrf_lookup (id
))
3875 * rib_tables_iter_next
3877 * Returns the next table in the iteration.
3879 struct route_table
*
3880 rib_tables_iter_next (rib_tables_iter_t
*iter
)
3882 struct route_table
*table
;
3885 * Array that helps us go over all AFI/SAFI combinations via one
3892 { AFI_IP
, SAFI_UNICAST
},
3893 { AFI_IP
, SAFI_MULTICAST
},
3894 { AFI_IP6
, SAFI_UNICAST
},
3895 { AFI_IP6
, SAFI_MULTICAST
},
3900 switch (iter
->state
)
3903 case RIB_TABLES_ITER_S_INIT
:
3905 iter
->afi_safi_ix
= -1;
3909 case RIB_TABLES_ITER_S_ITERATING
:
3910 iter
->afi_safi_ix
++;
3914 while (iter
->afi_safi_ix
< (int) ZEBRA_NUM_OF (afi_safis
))
3916 table
= vrf_table (afi_safis
[iter
->afi_safi_ix
].afi
,
3917 afi_safis
[iter
->afi_safi_ix
].safi
,
3922 iter
->afi_safi_ix
++;
3926 * Found another table in this vrf.
3932 * Done with all tables in the current vrf, go to the next
3935 if (!vrf_id_get_next (iter
->vrf_id
, &iter
->vrf_id
))
3938 iter
->afi_safi_ix
= 0;
3943 case RIB_TABLES_ITER_S_DONE
:
3948 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3950 iter
->state
= RIB_TABLES_ITER_S_DONE
;