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"
38 #include "zebra/rib.h"
40 #include "zebra/zserv.h"
41 #include "zebra/redistribute.h"
42 #include "zebra/debug.h"
44 /* Default rtm_table for all clients */
45 extern struct zebra_t zebrad
;
47 /* Hold time for RIB process, should be very minimal.
48 * it is useful to able to set it otherwise for testing, hence exported
49 * as global here for test-rig code.
51 int rib_process_hold_time
= 10;
53 /* Each route type's string and default distance value. */
58 } route_info
[ZEBRA_ROUTE_MAX
] =
60 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
61 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
62 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
63 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
64 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
65 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
66 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
67 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
68 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
69 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
70 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 95},
71 /* no entry/default: 150 */
74 /* Vector for routing table. */
75 static vector vrf_vector
;
81 vrf_table_create (struct vrf
*vrf
, afi_t afi
, safi_t safi
)
83 rib_table_info_t
*info
;
84 struct route_table
*table
;
86 assert (!vrf
->table
[afi
][safi
]);
88 table
= route_table_init ();
89 vrf
->table
[afi
][safi
] = table
;
91 info
= XCALLOC (MTYPE_RIB_TABLE_INFO
, sizeof (*info
));
98 /* Allocate new VRF. */
100 vrf_alloc (const char *name
)
104 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
108 vrf
->name
= XSTRDUP (MTYPE_VRF_NAME
, name
);
110 /* Allocate routing table and static table. */
111 vrf_table_create (vrf
, AFI_IP
, SAFI_UNICAST
);
112 vrf_table_create (vrf
, AFI_IP6
, SAFI_UNICAST
);
113 vrf
->stable
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
114 vrf
->stable
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
115 vrf_table_create (vrf
, AFI_IP
, SAFI_MULTICAST
);
116 vrf_table_create (vrf
, AFI_IP6
, SAFI_MULTICAST
);
117 vrf
->stable
[AFI_IP
][SAFI_MULTICAST
] = route_table_init ();
118 vrf
->stable
[AFI_IP6
][SAFI_MULTICAST
] = route_table_init ();
124 /* Lookup VRF by identifier. */
126 vrf_lookup (u_int32_t id
)
128 return vector_lookup (vrf_vector
, id
);
131 /* Initialize VRF. */
135 struct vrf
*default_table
;
137 /* Allocate VRF vector. */
138 vrf_vector
= vector_init (1);
140 /* Allocate default main table. */
141 default_table
= vrf_alloc ("Default-IP-Routing-Table");
143 /* Default table index must be 0. */
144 vector_set_index (vrf_vector
, 0, default_table
);
147 /* Lookup route table. */
149 vrf_table (afi_t afi
, safi_t safi
, u_int32_t id
)
153 vrf
= vrf_lookup (id
);
157 return vrf
->table
[afi
][safi
];
160 /* Lookup static route table. */
162 vrf_static_table (afi_t afi
, safi_t safi
, u_int32_t id
)
166 vrf
= vrf_lookup (id
);
170 return vrf
->stable
[afi
][safi
];
173 /* Add nexthop to the end of the list. */
175 nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
177 struct nexthop
*last
;
179 for (last
= rib
->nexthop
; last
&& last
->next
; last
= last
->next
)
182 last
->next
= nexthop
;
184 rib
->nexthop
= nexthop
;
185 nexthop
->prev
= last
;
190 /* Delete specified nexthop from the list. */
192 nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
195 nexthop
->next
->prev
= nexthop
->prev
;
197 nexthop
->prev
->next
= nexthop
->next
;
199 rib
->nexthop
= nexthop
->next
;
205 nexthop_free (struct nexthop
*nexthop
)
208 XFREE (0, nexthop
->ifname
);
209 XFREE (MTYPE_NEXTHOP
, nexthop
);
213 nexthop_ifindex_add (struct rib
*rib
, unsigned int ifindex
)
215 struct nexthop
*nexthop
;
217 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
218 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
219 nexthop
->ifindex
= ifindex
;
221 nexthop_add (rib
, nexthop
);
227 nexthop_ifname_add (struct rib
*rib
, char *ifname
)
229 struct nexthop
*nexthop
;
231 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
232 nexthop
->type
= NEXTHOP_TYPE_IFNAME
;
233 nexthop
->ifname
= XSTRDUP (0, ifname
);
235 nexthop_add (rib
, nexthop
);
241 nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
243 struct nexthop
*nexthop
;
245 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
246 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
247 nexthop
->gate
.ipv4
= *ipv4
;
249 nexthop
->src
.ipv4
= *src
;
251 nexthop_add (rib
, nexthop
);
257 nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
258 struct in_addr
*src
, unsigned int ifindex
)
260 struct nexthop
*nexthop
;
262 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
263 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
264 nexthop
->gate
.ipv4
= *ipv4
;
266 nexthop
->src
.ipv4
= *src
;
267 nexthop
->ifindex
= ifindex
;
269 nexthop_add (rib
, nexthop
);
276 nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
278 struct nexthop
*nexthop
;
280 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
281 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
282 nexthop
->gate
.ipv6
= *ipv6
;
284 nexthop_add (rib
, nexthop
);
289 static struct nexthop
*
290 nexthop_ipv6_ifname_add (struct rib
*rib
, struct in6_addr
*ipv6
,
293 struct nexthop
*nexthop
;
295 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
296 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFNAME
;
297 nexthop
->gate
.ipv6
= *ipv6
;
298 nexthop
->ifname
= XSTRDUP (0, ifname
);
300 nexthop_add (rib
, nexthop
);
305 static struct nexthop
*
306 nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
307 unsigned int ifindex
)
309 struct nexthop
*nexthop
;
311 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
312 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
313 nexthop
->gate
.ipv6
= *ipv6
;
314 nexthop
->ifindex
= ifindex
;
316 nexthop_add (rib
, nexthop
);
320 #endif /* HAVE_IPV6 */
323 nexthop_blackhole_add (struct rib
*rib
)
325 struct nexthop
*nexthop
;
327 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
328 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
329 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
331 nexthop_add (rib
, nexthop
);
336 /* If force flag is not set, do not modify falgs at all for uninstall
337 the route from FIB. */
339 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
340 struct route_node
*top
)
342 struct prefix_ipv4 p
;
343 struct route_table
*table
;
344 struct route_node
*rn
;
346 struct nexthop
*newhop
;
348 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
349 nexthop
->ifindex
= 0;
352 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
354 /* Make lookup prefix. */
355 memset (&p
, 0, sizeof (struct prefix_ipv4
));
357 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
358 p
.prefix
= nexthop
->gate
.ipv4
;
361 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
365 rn
= route_node_match (table
, (struct prefix
*) &p
);
368 route_unlock_node (rn
);
370 /* If lookup self prefix return immediately. */
374 /* Pick up selected route. */
375 RNODE_FOREACH_RIB (rn
, match
)
377 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
379 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
383 /* If there is no selected route or matched route is EGP, go up
386 || match
->type
== ZEBRA_ROUTE_BGP
)
390 } while (rn
&& rn
->info
== NULL
);
392 route_lock_node (rn
);
396 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
398 /* Directly point connected route. */
399 newhop
= match
->nexthop
;
400 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
401 nexthop
->ifindex
= newhop
->ifindex
;
405 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
407 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
408 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
409 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
413 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
414 nexthop
->rtype
= newhop
->type
;
415 if (newhop
->type
== NEXTHOP_TYPE_IPV4
||
416 newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
417 nexthop
->rgate
.ipv4
= newhop
->gate
.ipv4
;
418 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
419 || newhop
->type
== NEXTHOP_TYPE_IFNAME
420 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
421 nexthop
->rifindex
= newhop
->ifindex
;
437 /* If force flag is not set, do not modify falgs at all for uninstall
438 the route from FIB. */
440 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
441 struct route_node
*top
)
443 struct prefix_ipv6 p
;
444 struct route_table
*table
;
445 struct route_node
*rn
;
447 struct nexthop
*newhop
;
449 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
450 nexthop
->ifindex
= 0;
453 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
455 /* Make lookup prefix. */
456 memset (&p
, 0, sizeof (struct prefix_ipv6
));
458 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
459 p
.prefix
= nexthop
->gate
.ipv6
;
462 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
466 rn
= route_node_match (table
, (struct prefix
*) &p
);
469 route_unlock_node (rn
);
471 /* If lookup self prefix return immediately. */
475 /* Pick up selected route. */
476 RNODE_FOREACH_RIB (rn
, match
)
478 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
480 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
484 /* If there is no selected route or matched route is EGP, go up
487 || match
->type
== ZEBRA_ROUTE_BGP
)
491 } while (rn
&& rn
->info
== NULL
);
493 route_lock_node (rn
);
497 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
499 /* Directly point connected route. */
500 newhop
= match
->nexthop
;
502 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
503 nexthop
->ifindex
= newhop
->ifindex
;
507 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
509 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
510 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
511 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
515 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
516 nexthop
->rtype
= newhop
->type
;
517 if (newhop
->type
== NEXTHOP_TYPE_IPV6
518 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
519 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
520 nexthop
->rgate
.ipv6
= newhop
->gate
.ipv6
;
521 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
522 || newhop
->type
== NEXTHOP_TYPE_IFNAME
523 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
524 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
525 nexthop
->rifindex
= newhop
->ifindex
;
539 #endif /* HAVE_IPV6 */
542 rib_match_ipv4 (struct in_addr addr
)
544 struct prefix_ipv4 p
;
545 struct route_table
*table
;
546 struct route_node
*rn
;
548 struct nexthop
*newhop
;
551 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
555 memset (&p
, 0, sizeof (struct prefix_ipv4
));
557 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
560 rn
= route_node_match (table
, (struct prefix
*) &p
);
564 route_unlock_node (rn
);
566 /* Pick up selected route. */
567 RNODE_FOREACH_RIB (rn
, match
)
569 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
571 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
575 /* If there is no selected route or matched route is EGP, go up
578 || match
->type
== ZEBRA_ROUTE_BGP
)
582 } while (rn
&& rn
->info
== NULL
);
584 route_lock_node (rn
);
588 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
589 /* Directly point connected route. */
593 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
594 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
604 rib_lookup_ipv4 (struct prefix_ipv4
*p
)
606 struct route_table
*table
;
607 struct route_node
*rn
;
609 struct nexthop
*nexthop
;
612 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
616 rn
= route_node_lookup (table
, (struct prefix
*) p
);
618 /* No route for this prefix. */
623 route_unlock_node (rn
);
625 RNODE_FOREACH_RIB (rn
, match
)
627 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
629 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
633 if (! match
|| match
->type
== ZEBRA_ROUTE_BGP
)
636 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
639 for (nexthop
= match
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
640 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
647 * This clone function, unlike its original rib_lookup_ipv4(), checks
648 * if specified IPv4 route record (prefix/mask -> gate) exists in
649 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
653 * 0: exact match found
654 * 1: a match was found with a different gate
655 * 2: connected route found
656 * 3: no matches found
659 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
)
661 struct route_table
*table
;
662 struct route_node
*rn
;
664 struct nexthop
*nexthop
;
667 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
669 return ZEBRA_RIB_LOOKUP_ERROR
;
671 /* Scan the RIB table for exactly matching RIB entry. */
672 rn
= route_node_lookup (table
, (struct prefix
*) p
);
674 /* No route for this prefix. */
676 return ZEBRA_RIB_NOTFOUND
;
679 route_unlock_node (rn
);
681 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
682 RNODE_FOREACH_RIB (rn
, match
)
684 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
686 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
690 /* None such found :( */
692 return ZEBRA_RIB_NOTFOUND
;
694 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
695 return ZEBRA_RIB_FOUND_CONNECTED
;
697 /* Ok, we have a cood candidate, let's check it's nexthop list... */
698 for (nexthop
= match
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
699 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
701 /* We are happy with either direct or recursive hexthop */
702 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip (qgate
) ||
703 nexthop
->rgate
.ipv4
.s_addr
== sockunion2ip (qgate
))
704 return ZEBRA_RIB_FOUND_EXACT
;
707 if (IS_ZEBRA_DEBUG_RIB
)
709 char gate_buf
[INET_ADDRSTRLEN
], rgate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
710 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
711 inet_ntop (AF_INET
, &nexthop
->rgate
.ipv4
.s_addr
, rgate_buf
, INET_ADDRSTRLEN
);
712 inet_ntop (AF_INET
, &sockunion2ip (qgate
), qgate_buf
, INET_ADDRSTRLEN
);
713 zlog_debug ("%s: qgate == %s, gate == %s, rgate == %s", __func__
, qgate_buf
, gate_buf
, rgate_buf
);
715 return ZEBRA_RIB_FOUND_NOGATE
;
719 return ZEBRA_RIB_NOTFOUND
;
724 rib_match_ipv6 (struct in6_addr
*addr
)
726 struct prefix_ipv6 p
;
727 struct route_table
*table
;
728 struct route_node
*rn
;
730 struct nexthop
*newhop
;
733 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
737 memset (&p
, 0, sizeof (struct prefix_ipv6
));
739 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
740 IPV6_ADDR_COPY (&p
.prefix
, addr
);
742 rn
= route_node_match (table
, (struct prefix
*) &p
);
746 route_unlock_node (rn
);
748 /* Pick up selected route. */
749 RNODE_FOREACH_RIB (rn
, match
)
751 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
753 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
757 /* If there is no selected route or matched route is EGP, go up
760 || match
->type
== ZEBRA_ROUTE_BGP
)
764 } while (rn
&& rn
->info
== NULL
);
766 route_lock_node (rn
);
770 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
771 /* Directly point connected route. */
775 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
776 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
784 #endif /* HAVE_IPV6 */
786 #define RIB_SYSTEM_ROUTE(R) \
787 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
789 /* This function verifies reachability of one given nexthop, which can be
790 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
791 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
792 * nexthop->ifindex will be updated appropriately as well.
793 * An existing route map can turn (otherwise active) nexthop into inactive, but
796 * The return value is the final value of 'ACTIVE' flag.
800 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
801 struct nexthop
*nexthop
, int set
)
803 struct interface
*ifp
;
804 route_map_result_t ret
= RMAP_MATCH
;
805 extern char *proto_rm
[AFI_MAX
][ZEBRA_ROUTE_MAX
+1];
806 struct route_map
*rmap
;
810 switch (nexthop
->type
)
812 case NEXTHOP_TYPE_IFINDEX
:
813 ifp
= if_lookup_by_index (nexthop
->ifindex
);
814 if (ifp
&& if_is_operative(ifp
))
815 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
817 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
819 case NEXTHOP_TYPE_IPV6_IFNAME
:
821 case NEXTHOP_TYPE_IFNAME
:
822 ifp
= if_lookup_by_name (nexthop
->ifname
);
823 if (ifp
&& if_is_operative(ifp
))
826 nexthop
->ifindex
= ifp
->ifindex
;
827 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
832 nexthop
->ifindex
= 0;
833 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
836 case NEXTHOP_TYPE_IPV4
:
837 case NEXTHOP_TYPE_IPV4_IFINDEX
:
839 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
840 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
842 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
845 case NEXTHOP_TYPE_IPV6
:
847 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
848 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
850 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
852 case NEXTHOP_TYPE_IPV6_IFINDEX
:
854 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
856 ifp
= if_lookup_by_index (nexthop
->ifindex
);
857 if (ifp
&& if_is_operative(ifp
))
858 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
860 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
864 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
865 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
867 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
870 #endif /* HAVE_IPV6 */
871 case NEXTHOP_TYPE_BLACKHOLE
:
872 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
877 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
880 if (RIB_SYSTEM_ROUTE(rib
) ||
881 (family
== AFI_IP
&& rn
->p
.family
!= AF_INET
) ||
882 (family
== AFI_IP6
&& rn
->p
.family
!= AF_INET6
))
883 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
886 if (rib
->type
>= 0 && rib
->type
< ZEBRA_ROUTE_MAX
&&
887 proto_rm
[family
][rib
->type
])
888 rmap
= route_map_lookup_by_name (proto_rm
[family
][rib
->type
]);
889 if (!rmap
&& proto_rm
[family
][ZEBRA_ROUTE_MAX
])
890 rmap
= route_map_lookup_by_name (proto_rm
[family
][ZEBRA_ROUTE_MAX
]);
892 ret
= route_map_apply(rmap
, &rn
->p
, RMAP_ZEBRA
, nexthop
);
895 if (ret
== RMAP_DENYMATCH
)
896 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
897 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
900 /* Iterate over all nexthops of the given RIB entry and refresh their
901 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
902 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
903 * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
904 * transparently passed to nexthop_active_check().
906 * Return value is the new number of active nexthops.
910 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
912 struct nexthop
*nexthop
;
913 unsigned int prev_active
, prev_index
, new_active
;
915 rib
->nexthop_active_num
= 0;
916 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
918 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
920 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
921 prev_index
= nexthop
->ifindex
;
922 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
923 rib
->nexthop_active_num
++;
924 if (prev_active
!= new_active
||
925 prev_index
!= nexthop
->ifindex
)
926 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
928 return rib
->nexthop_active_num
;
934 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
)
937 struct nexthop
*nexthop
;
939 switch (PREFIX_FAMILY (&rn
->p
))
942 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
946 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
948 #endif /* HAVE_IPV6 */
951 /* This condition is never met, if we are using rt_socket.c */
954 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
955 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
959 /* Uninstall the route from kernel. */
961 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
964 struct nexthop
*nexthop
;
966 switch (PREFIX_FAMILY (&rn
->p
))
969 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
973 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
975 #endif /* HAVE_IPV6 */
978 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
979 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
984 /* Uninstall the route from kernel. */
986 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
988 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
990 redistribute_delete (&rn
->p
, rib
);
991 if (! RIB_SYSTEM_ROUTE (rib
))
992 rib_uninstall_kernel (rn
, rib
);
993 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
997 static void rib_unlink (struct route_node
*, struct rib
*);
1000 * rib_can_delete_dest
1002 * Returns TRUE if the given dest can be deleted from the table.
1005 rib_can_delete_dest (rib_dest_t
*dest
)
1018 * Garbage collect the rib dest corresponding to the given route node
1021 * Returns TRUE if the dest was deleted, FALSE otherwise.
1024 rib_gc_dest (struct route_node
*rn
)
1027 char buf
[INET6_ADDRSTRLEN
];
1029 dest
= rib_dest_from_rnode (rn
);
1033 if (!rib_can_delete_dest (dest
))
1036 if (IS_ZEBRA_DEBUG_RIB
)
1038 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, sizeof (buf
));
1039 zlog_debug ("%s: %s/%d: removing dest from table", __func__
,
1040 buf
, rn
->p
.prefixlen
);
1044 XFREE (MTYPE_RIB_DEST
, dest
);
1048 * Release the one reference that we keep on the route node.
1050 route_unlock_node (rn
);
1054 /* Core function for processing routing information base. */
1056 rib_process (struct route_node
*rn
)
1060 struct rib
*fib
= NULL
;
1061 struct rib
*select
= NULL
;
1062 struct rib
*del
= NULL
;
1064 struct nexthop
*nexthop
= NULL
;
1065 char buf
[INET6_ADDRSTRLEN
];
1069 if (IS_ZEBRA_DEBUG_RIB
|| IS_ZEBRA_DEBUG_RIB_Q
)
1070 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1072 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1074 /* Currently installed rib. */
1075 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1077 assert (fib
== NULL
);
1081 /* Unlock removed routes, so they'll be freed, bar the FIB entry,
1082 * which we need to do do further work with below.
1084 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1088 if (IS_ZEBRA_DEBUG_RIB
)
1089 zlog_debug ("%s: %s/%d: rn %p, removing rib %p", __func__
,
1090 buf
, rn
->p
.prefixlen
, rn
, rib
);
1091 rib_unlink (rn
, rib
);
1099 /* Skip unreachable nexthop. */
1100 if (! nexthop_active_update (rn
, rib
, 0))
1103 /* Infinit distance. */
1104 if (rib
->distance
== DISTANCE_INFINITY
)
1107 /* Newly selected rib, the common case. */
1114 /* filter route selection in following order:
1115 * - connected beats other types
1116 * - lower distance beats higher
1117 * - lower metric beats higher for equal distance
1118 * - last, hence oldest, route wins tie break.
1121 /* Connected routes. Pick the last connected
1122 * route of the set of lowest metric connected routes.
1124 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1126 if (select
->type
!= ZEBRA_ROUTE_CONNECT
1127 || rib
->metric
<= select
->metric
)
1131 else if (select
->type
== ZEBRA_ROUTE_CONNECT
)
1134 /* higher distance loses */
1135 if (rib
->distance
> select
->distance
)
1139 if (rib
->distance
< select
->distance
)
1145 /* metric tie-breaks equal distance */
1146 if (rib
->metric
<= select
->metric
)
1148 } /* RNODE_FOREACH_RIB_SAFE */
1150 /* After the cycle is finished, the following pointers will be set:
1151 * select --- the winner RIB entry, if any was found, otherwise NULL
1152 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1153 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1157 /* Same RIB entry is selected. Update FIB and finish. */
1158 if (select
&& select
== fib
)
1160 if (IS_ZEBRA_DEBUG_RIB
)
1161 zlog_debug ("%s: %s/%d: Updating existing route, select %p, fib %p",
1162 __func__
, buf
, rn
->p
.prefixlen
, select
, fib
);
1163 if (CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
1165 redistribute_delete (&rn
->p
, select
);
1166 if (! RIB_SYSTEM_ROUTE (select
))
1167 rib_uninstall_kernel (rn
, select
);
1169 /* Set real nexthop. */
1170 nexthop_active_update (rn
, select
, 1);
1172 if (! RIB_SYSTEM_ROUTE (select
))
1173 rib_install_kernel (rn
, select
);
1174 redistribute_add (&rn
->p
, select
);
1176 else if (! RIB_SYSTEM_ROUTE (select
))
1178 /* Housekeeping code to deal with
1179 race conditions in kernel with linux
1180 netlink reporting interface up before IPv4 or IPv6 protocol
1181 is ready to add routes.
1182 This makes sure the routes are IN the kernel.
1185 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1186 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1192 rib_install_kernel (rn
, select
);
1197 /* At this point we either haven't found the best RIB entry or it is
1198 * different from what we currently intend to flag with SELECTED. In both
1199 * cases, if a RIB block is present in FIB, it should be withdrawn.
1203 if (IS_ZEBRA_DEBUG_RIB
)
1204 zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__
,
1205 buf
, rn
->p
.prefixlen
, fib
);
1206 redistribute_delete (&rn
->p
, fib
);
1207 if (! RIB_SYSTEM_ROUTE (fib
))
1208 rib_uninstall_kernel (rn
, fib
);
1209 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1211 /* Set real nexthop. */
1212 nexthop_active_update (rn
, fib
, 1);
1215 /* Regardless of some RIB entry being SELECTED or not before, now we can
1216 * tell, that if a new winner exists, FIB is still not updated with this
1217 * data, but ready to be.
1221 if (IS_ZEBRA_DEBUG_RIB
)
1222 zlog_debug ("%s: %s/%d: Adding route, select %p", __func__
, buf
,
1223 rn
->p
.prefixlen
, select
);
1224 /* Set real nexthop. */
1225 nexthop_active_update (rn
, select
, 1);
1227 if (! RIB_SYSTEM_ROUTE (select
))
1228 rib_install_kernel (rn
, select
);
1229 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1230 redistribute_add (&rn
->p
, select
);
1233 /* FIB route was removed, should be deleted */
1236 if (IS_ZEBRA_DEBUG_RIB
)
1237 zlog_debug ("%s: %s/%d: Deleting fib %p, rn %p", __func__
, buf
,
1238 rn
->p
.prefixlen
, del
, rn
);
1239 rib_unlink (rn
, del
);
1243 if (IS_ZEBRA_DEBUG_RIB_Q
)
1244 zlog_debug ("%s: %s/%d: rn %p dequeued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1247 * Check if the dest can be deleted now.
1252 /* Take a list of route_node structs and return 1, if there was a record
1253 * picked from it and processed by rib_process(). Don't process more,
1254 * than one RN record; operate only in the specified sub-queue.
1257 process_subq (struct list
* subq
, u_char qindex
)
1259 struct listnode
*lnode
= listhead (subq
);
1260 struct route_node
*rnode
;
1265 rnode
= listgetdata (lnode
);
1266 rib_process (rnode
);
1269 UNSET_FLAG (rib_dest_from_rnode (rnode
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1274 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1275 __func__
, rnode
, rnode
->lock
);
1276 zlog_backtrace(LOG_DEBUG
);
1279 route_unlock_node (rnode
);
1280 list_delete_node (subq
, lnode
);
1284 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1285 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1286 * is pointed to the meta queue structure.
1288 static wq_item_status
1289 meta_queue_process (struct work_queue
*dummy
, void *data
)
1291 struct meta_queue
* mq
= data
;
1294 for (i
= 0; i
< MQ_SIZE
; i
++)
1295 if (process_subq (mq
->subq
[i
], i
))
1300 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1304 * Map from rib types to queue type (priority) in meta queue
1306 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1307 [ZEBRA_ROUTE_SYSTEM
] = 4,
1308 [ZEBRA_ROUTE_KERNEL
] = 0,
1309 [ZEBRA_ROUTE_CONNECT
] = 0,
1310 [ZEBRA_ROUTE_STATIC
] = 1,
1311 [ZEBRA_ROUTE_RIP
] = 2,
1312 [ZEBRA_ROUTE_RIPNG
] = 2,
1313 [ZEBRA_ROUTE_OSPF
] = 2,
1314 [ZEBRA_ROUTE_OSPF6
] = 2,
1315 [ZEBRA_ROUTE_ISIS
] = 2,
1316 [ZEBRA_ROUTE_BGP
] = 3,
1317 [ZEBRA_ROUTE_HSLS
] = 4,
1318 [ZEBRA_ROUTE_BABEL
] = 2,
1321 /* Look into the RN and queue it into one or more priority queues,
1322 * increasing the size for each data push done.
1325 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
1328 char buf
[INET6_ADDRSTRLEN
];
1330 if (IS_ZEBRA_DEBUG_RIB_Q
)
1331 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1333 RNODE_FOREACH_RIB (rn
, rib
)
1335 u_char qindex
= meta_queue_map
[rib
->type
];
1337 /* Invariant: at this point we always have rn->info set. */
1338 if (CHECK_FLAG (rib_dest_from_rnode (rn
)->flags
,
1339 RIB_ROUTE_QUEUED (qindex
)))
1341 if (IS_ZEBRA_DEBUG_RIB_Q
)
1342 zlog_debug ("%s: %s/%d: rn %p is already queued in sub-queue %u",
1343 __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1347 SET_FLAG (rib_dest_from_rnode (rn
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1348 listnode_add (mq
->subq
[qindex
], rn
);
1349 route_lock_node (rn
);
1352 if (IS_ZEBRA_DEBUG_RIB_Q
)
1353 zlog_debug ("%s: %s/%d: queued rn %p into sub-queue %u",
1354 __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1358 /* Add route_node to work queue and schedule processing */
1360 rib_queue_add (struct zebra_t
*zebra
, struct route_node
*rn
)
1362 char buf
[INET_ADDRSTRLEN
];
1363 assert (zebra
&& rn
);
1365 if (IS_ZEBRA_DEBUG_RIB_Q
)
1366 inet_ntop (AF_INET
, &rn
->p
.u
.prefix
, buf
, INET_ADDRSTRLEN
);
1368 /* Pointless to queue a route_node with no RIB entries to add or remove */
1369 if (!rnode_to_ribs (rn
))
1371 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1372 __func__
, rn
, rn
->lock
);
1373 zlog_backtrace(LOG_DEBUG
);
1377 if (IS_ZEBRA_DEBUG_RIB_Q
)
1378 zlog_info ("%s: %s/%d: work queue added", __func__
, buf
, rn
->p
.prefixlen
);
1382 if (zebra
->ribq
== NULL
)
1384 zlog_err ("%s: work_queue does not exist!", __func__
);
1389 * The RIB queue should normally be either empty or holding the only
1390 * work_queue_item element. In the latter case this element would
1391 * hold a pointer to the meta queue structure, which must be used to
1392 * actually queue the route nodes to process. So create the MQ
1393 * holder, if necessary, then push the work into it in any case.
1394 * This semantics was introduced after 0.99.9 release.
1396 if (!zebra
->ribq
->items
->count
)
1397 work_queue_add (zebra
->ribq
, zebra
->mq
);
1399 rib_meta_queue_add (zebra
->mq
, rn
);
1401 if (IS_ZEBRA_DEBUG_RIB_Q
)
1402 zlog_debug ("%s: %s/%d: rn %p queued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1407 /* Create new meta queue.
1408 A destructor function doesn't seem to be necessary here.
1410 static struct meta_queue
*
1411 meta_queue_new (void)
1413 struct meta_queue
*new;
1416 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
1419 for (i
= 0; i
< MQ_SIZE
; i
++)
1421 new->subq
[i
] = list_new ();
1422 assert(new->subq
[i
]);
1428 /* initialise zebra rib work queue */
1430 rib_queue_init (struct zebra_t
*zebra
)
1434 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1435 "route_node processing")))
1437 zlog_err ("%s: could not initialise work queue!", __func__
);
1441 /* fill in the work queue spec */
1442 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1443 zebra
->ribq
->spec
.errorfunc
= NULL
;
1444 /* XXX: TODO: These should be runtime configurable via vty */
1445 zebra
->ribq
->spec
.max_retries
= 3;
1446 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
1448 if (!(zebra
->mq
= meta_queue_new ()))
1450 zlog_err ("%s: could not initialise meta queue!", __func__
);
1456 /* RIB updates are processed via a queue of pointers to route_nodes.
1458 * The queue length is bounded by the maximal size of the routing table,
1459 * as a route_node will not be requeued, if already queued.
1461 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
1462 * state, or static_install_ipv{4,6} (when an existing RIB is updated)
1463 * and then submit route_node to queue for best-path selection later.
1464 * Order of add/delete state changes are preserved for any given RIB.
1466 * Deleted RIBs are reaped during best-path selection.
1469 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
1470 * |-------->| | best RIB, if required
1472 * static_install->|->rib_addqueue...... -> rib_process
1474 * |-------->| |-> rib_unlink
1475 * |-> set RIB_ENTRY_REMOVE |
1476 * rib_delnode (RIB freed)
1478 * The 'info' pointer of a route_node points to a rib_dest_t
1479 * ('dest'). Queueing state for a route_node is kept on the dest. The
1480 * dest is created on-demand by rib_link() and is kept around at least
1481 * as long as there are ribs hanging off it (@see rib_gc_dest()).
1483 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1485 * - route_nodes: refcounted by:
1486 * - dest attached to route_node:
1487 * - managed by: rib_link/rib_gc_dest
1488 * - route_node processing queue
1489 * - managed by: rib_addqueue, rib_process.
1493 /* Add RIB to head of the route node. */
1495 rib_link (struct route_node
*rn
, struct rib
*rib
)
1499 char buf
[INET6_ADDRSTRLEN
];
1503 if (IS_ZEBRA_DEBUG_RIB
)
1505 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1506 zlog_debug ("%s: %s/%d: rn %p, rib %p", __func__
,
1507 buf
, rn
->p
.prefixlen
, rn
, rib
);
1510 dest
= rib_dest_from_rnode (rn
);
1513 if (IS_ZEBRA_DEBUG_RIB
)
1515 zlog_debug ("%s: %s/%d: adding dest to table", __func__
,
1516 buf
, rn
->p
.prefixlen
);
1519 dest
= XCALLOC (MTYPE_RIB_DEST
, sizeof (rib_dest_t
));
1520 route_lock_node (rn
); /* rn route table reference */
1525 head
= dest
->routes
;
1532 rib_queue_add (&zebrad
, rn
);
1536 rib_addnode (struct route_node
*rn
, struct rib
*rib
)
1538 /* RIB node has been un-removed before route-node is processed.
1539 * route_node must hence already be on the queue for processing..
1541 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1543 if (IS_ZEBRA_DEBUG_RIB
)
1545 char buf
[INET6_ADDRSTRLEN
];
1546 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1547 zlog_debug ("%s: %s/%d: rn %p, un-removed rib %p",
1548 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1550 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1559 * Detach a rib structure from a route_node.
1561 * Note that a call to rib_unlink() should be followed by a call to
1562 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
1563 * longer required to be deleted.
1566 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
1568 struct nexthop
*nexthop
, *next
;
1569 char buf
[INET6_ADDRSTRLEN
];
1574 if (IS_ZEBRA_DEBUG_RIB
)
1576 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1577 zlog_debug ("%s: %s/%d: rn %p, rib %p",
1578 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1581 dest
= rib_dest_from_rnode (rn
);
1584 rib
->next
->prev
= rib
->prev
;
1587 rib
->prev
->next
= rib
->next
;
1590 dest
->routes
= rib
->next
;
1593 /* free RIB and nexthops */
1594 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= next
)
1596 next
= nexthop
->next
;
1597 nexthop_free (nexthop
);
1599 XFREE (MTYPE_RIB
, rib
);
1604 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
1606 if (IS_ZEBRA_DEBUG_RIB
)
1608 char buf
[INET6_ADDRSTRLEN
];
1609 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1610 zlog_debug ("%s: %s/%d: rn %p, rib %p, removing", __func__
,
1611 buf
, rn
->p
.prefixlen
, rn
, rib
);
1613 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1614 rib_queue_add (&zebrad
, rn
);
1618 rib_add_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1619 struct in_addr
*gate
, struct in_addr
*src
,
1620 unsigned int ifindex
, u_int32_t vrf_id
,
1621 u_int32_t metric
, u_char distance
, safi_t safi
)
1624 struct rib
*same
= NULL
;
1625 struct route_table
*table
;
1626 struct route_node
*rn
;
1627 struct nexthop
*nexthop
;
1630 table
= vrf_table (AFI_IP
, safi
, 0);
1634 /* Make it sure prefixlen is applied to the prefix. */
1635 apply_mask_ipv4 (p
);
1637 /* Set default distance by route type. */
1640 if ((unsigned)type
>= array_size(route_info
))
1643 distance
= route_info
[type
].distance
;
1645 /* iBGP distance is 200. */
1646 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1650 /* Lookup route node.*/
1651 rn
= route_node_get (table
, (struct prefix
*) p
);
1653 /* If same type of route are installed, treat it as a implicit
1655 RNODE_FOREACH_RIB (rn
, rib
)
1657 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1660 if (rib
->type
!= type
)
1662 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
1667 /* Duplicate connected route comes in. */
1668 else if ((nexthop
= rib
->nexthop
) &&
1669 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
1670 nexthop
->ifindex
== ifindex
&&
1671 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1678 /* Allocate new rib structure. */
1679 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1681 rib
->distance
= distance
;
1683 rib
->metric
= metric
;
1684 rib
->table
= vrf_id
;
1685 rib
->nexthop_num
= 0;
1686 rib
->uptime
= time (NULL
);
1688 /* Nexthop settings. */
1692 nexthop_ipv4_ifindex_add (rib
, gate
, src
, ifindex
);
1694 nexthop_ipv4_add (rib
, gate
, src
);
1697 nexthop_ifindex_add (rib
, ifindex
);
1699 /* If this route is kernel route, set FIB flag to the route. */
1700 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1701 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1702 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1704 /* Link new rib to node.*/
1705 if (IS_ZEBRA_DEBUG_RIB
)
1706 zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__
, rn
, rib
);
1707 rib_addnode (rn
, rib
);
1709 /* Free implicit route.*/
1712 if (IS_ZEBRA_DEBUG_RIB
)
1713 zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__
, rn
, rib
);
1714 rib_delnode (rn
, same
);
1717 route_unlock_node (rn
);
1721 /* This function dumps the contents of a given RIB entry into
1722 * standard debug log. Calling function name and IP prefix in
1723 * question are passed as 1st and 2nd arguments.
1726 void rib_dump (const char * func
, const struct prefix_ipv4
* p
, const struct rib
* rib
)
1728 char straddr1
[INET_ADDRSTRLEN
], straddr2
[INET_ADDRSTRLEN
];
1729 struct nexthop
*nexthop
;
1731 inet_ntop (AF_INET
, &p
->prefix
, straddr1
, INET_ADDRSTRLEN
);
1732 zlog_debug ("%s: dumping RIB entry %p for %s/%d", func
, rib
, straddr1
, p
->prefixlen
);
1735 "%s: refcnt == %lu, uptime == %lu, type == %u, table == %d",
1738 (unsigned long) rib
->uptime
,
1744 "%s: metric == %u, distance == %u, flags == %u, status == %u",
1753 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
1756 rib
->nexthop_active_num
,
1757 rib
->nexthop_fib_num
1759 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1761 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, straddr1
, INET_ADDRSTRLEN
);
1762 inet_ntop (AF_INET
, &nexthop
->rgate
.ipv4
.s_addr
, straddr2
, INET_ADDRSTRLEN
);
1765 "%s: NH %s (%s) with flags %s%s%s",
1769 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
1770 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
1771 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
1774 zlog_debug ("%s: dump complete", func
);
1777 /* This is an exported helper to rtm_read() to dump the strange
1778 * RIB entry found by rib_lookup_ipv4_route()
1781 void rib_lookup_and_dump (struct prefix_ipv4
* p
)
1783 struct route_table
*table
;
1784 struct route_node
*rn
;
1786 char prefix_buf
[INET_ADDRSTRLEN
];
1789 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1792 zlog_err ("%s: vrf_table() returned NULL", __func__
);
1796 inet_ntop (AF_INET
, &p
->prefix
.s_addr
, prefix_buf
, INET_ADDRSTRLEN
);
1797 /* Scan the RIB table for exactly matching RIB entry. */
1798 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1800 /* No route for this prefix. */
1803 zlog_debug ("%s: lookup failed for %s/%d", __func__
, prefix_buf
, p
->prefixlen
);
1808 route_unlock_node (rn
);
1811 RNODE_FOREACH_RIB (rn
, rib
)
1815 "%s: rn %p, rib %p: %s, %s",
1819 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
1820 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
1822 rib_dump (__func__
, p
, rib
);
1826 /* Check if requested address assignment will fail due to another
1827 * route being installed by zebra in FIB already. Take necessary
1828 * actions, if needed: remove such a route from FIB and deSELECT
1829 * corresponding RIB entry. Then put affected RN into RIBQ head.
1831 void rib_lookup_and_pushup (struct prefix_ipv4
* p
)
1833 struct route_table
*table
;
1834 struct route_node
*rn
;
1836 unsigned changed
= 0;
1838 if (NULL
== (table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0)))
1840 zlog_err ("%s: vrf_table() returned NULL", __func__
);
1844 /* No matches would be the simplest case. */
1845 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
1849 route_unlock_node (rn
);
1851 /* Check all RIB entries. In case any changes have to be done, requeue
1852 * the RN into RIBQ head. If the routing message about the new connected
1853 * route (generated by the IP address we are going to assign very soon)
1854 * comes before the RIBQ is processed, the new RIB entry will join
1855 * RIBQ record already on head. This is necessary for proper revalidation
1856 * of the rest of the RIB.
1858 RNODE_FOREACH_RIB (rn
, rib
)
1860 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) &&
1861 ! RIB_SYSTEM_ROUTE (rib
))
1864 if (IS_ZEBRA_DEBUG_RIB
)
1866 char buf
[INET_ADDRSTRLEN
];
1867 inet_ntop (rn
->p
.family
, &p
->prefix
, buf
, INET_ADDRSTRLEN
);
1868 zlog_debug ("%s: freeing way for connected prefix %s/%d", __func__
, buf
, p
->prefixlen
);
1869 rib_dump (__func__
, (struct prefix_ipv4
*)&rn
->p
, rib
);
1871 rib_uninstall (rn
, rib
);
1875 rib_queue_add (&zebrad
, rn
);
1879 rib_add_ipv4_multipath (struct prefix_ipv4
*p
, struct rib
*rib
, safi_t safi
)
1881 struct route_table
*table
;
1882 struct route_node
*rn
;
1884 struct nexthop
*nexthop
;
1887 table
= vrf_table (AFI_IP
, safi
, 0);
1891 /* Make it sure prefixlen is applied to the prefix. */
1892 apply_mask_ipv4 (p
);
1894 /* Set default distance by route type. */
1895 if (rib
->distance
== 0)
1897 rib
->distance
= route_info
[rib
->type
].distance
;
1899 /* iBGP distance is 200. */
1900 if (rib
->type
== ZEBRA_ROUTE_BGP
1901 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
1902 rib
->distance
= 200;
1905 /* Lookup route node.*/
1906 rn
= route_node_get (table
, (struct prefix
*) p
);
1908 /* If same type of route are installed, treat it as a implicit
1910 RNODE_FOREACH_RIB (rn
, same
)
1912 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
1915 if (same
->type
== rib
->type
&& same
->table
== rib
->table
1916 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
1920 /* If this route is kernel route, set FIB flag to the route. */
1921 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
1922 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1923 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1925 /* Link new rib to node.*/
1926 rib_addnode (rn
, rib
);
1927 if (IS_ZEBRA_DEBUG_RIB
)
1929 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
1931 rib_dump (__func__
, p
, rib
);
1934 /* Free implicit route.*/
1937 if (IS_ZEBRA_DEBUG_RIB
)
1939 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
1940 __func__
, rn
, same
);
1941 rib_dump (__func__
, p
, same
);
1943 rib_delnode (rn
, same
);
1946 route_unlock_node (rn
);
1950 /* XXX factor with rib_delete_ipv6 */
1952 rib_delete_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1953 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
, safi_t safi
)
1955 struct route_table
*table
;
1956 struct route_node
*rn
;
1958 struct rib
*fib
= NULL
;
1959 struct rib
*same
= NULL
;
1960 struct nexthop
*nexthop
;
1961 char buf1
[INET_ADDRSTRLEN
];
1962 char buf2
[INET_ADDRSTRLEN
];
1965 table
= vrf_table (AFI_IP
, safi
, 0);
1970 apply_mask_ipv4 (p
);
1972 if (IS_ZEBRA_DEBUG_KERNEL
&& gate
)
1973 zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
1974 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1979 /* Lookup route node. */
1980 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1983 if (IS_ZEBRA_DEBUG_KERNEL
)
1986 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
1987 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1989 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
1992 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
1993 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1997 return ZEBRA_ERR_RTNOEXIST
;
2000 /* Lookup same type route. */
2001 RNODE_FOREACH_RIB (rn
, rib
)
2003 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2006 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2009 if (rib
->type
!= type
)
2011 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2012 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2014 if (nexthop
->ifindex
!= ifindex
)
2019 route_unlock_node (rn
);
2020 route_unlock_node (rn
);
2026 /* Make sure that the route found has the same gateway. */
2027 else if (gate
== NULL
||
2028 ((nexthop
= rib
->nexthop
) &&
2029 (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
2030 IPV4_ADDR_SAME (&nexthop
->rgate
.ipv4
, gate
))))
2037 /* If same type of route can't be found and this message is from
2041 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
2044 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2045 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2047 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
2051 if (IS_ZEBRA_DEBUG_KERNEL
)
2054 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
2055 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2057 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
2061 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
2062 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2067 route_unlock_node (rn
);
2068 return ZEBRA_ERR_RTNOEXIST
;
2073 rib_delnode (rn
, same
);
2075 route_unlock_node (rn
);
2079 /* Install static route into rib. */
2081 static_install_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
2084 struct route_node
*rn
;
2085 struct route_table
*table
;
2088 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2092 /* Lookup existing route */
2093 rn
= route_node_get (table
, p
);
2094 RNODE_FOREACH_RIB (rn
, rib
)
2096 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2099 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2105 /* Same distance static route is there. Update it with new
2107 route_unlock_node (rn
);
2110 case STATIC_IPV4_GATEWAY
:
2111 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2113 case STATIC_IPV4_IFNAME
:
2114 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2116 case STATIC_IPV4_BLACKHOLE
:
2117 nexthop_blackhole_add (rib
);
2120 rib_queue_add (&zebrad
, rn
);
2124 /* This is new static route. */
2125 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2127 rib
->type
= ZEBRA_ROUTE_STATIC
;
2128 rib
->distance
= si
->distance
;
2130 rib
->table
= zebrad
.rtm_table_default
;
2131 rib
->nexthop_num
= 0;
2135 case STATIC_IPV4_GATEWAY
:
2136 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2138 case STATIC_IPV4_IFNAME
:
2139 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2141 case STATIC_IPV4_BLACKHOLE
:
2142 nexthop_blackhole_add (rib
);
2146 /* Save the flags of this static routes (reject, blackhole) */
2147 rib
->flags
= si
->flags
;
2149 /* Link this rib to the tree. */
2150 rib_addnode (rn
, rib
);
2155 static_ipv4_nexthop_same (struct nexthop
*nexthop
, struct static_ipv4
*si
)
2157 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
2158 && si
->type
== STATIC_IPV4_GATEWAY
2159 && IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &si
->gate
.ipv4
))
2161 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2162 && si
->type
== STATIC_IPV4_IFNAME
2163 && strcmp (nexthop
->ifname
, si
->gate
.ifname
) == 0)
2165 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
2166 && si
->type
== STATIC_IPV4_BLACKHOLE
)
2171 /* Uninstall static route from RIB. */
2173 static_uninstall_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
2175 struct route_node
*rn
;
2177 struct nexthop
*nexthop
;
2178 struct route_table
*table
;
2181 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2185 /* Lookup existing route with type and distance. */
2186 rn
= route_node_lookup (table
, p
);
2190 RNODE_FOREACH_RIB (rn
, rib
)
2192 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2195 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2201 route_unlock_node (rn
);
2205 /* Lookup nexthop. */
2206 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2207 if (static_ipv4_nexthop_same (nexthop
, si
))
2210 /* Can't find nexthop. */
2213 route_unlock_node (rn
);
2217 /* Check nexthop. */
2218 if (rib
->nexthop_num
== 1)
2219 rib_delnode (rn
, rib
);
2222 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2223 rib_uninstall (rn
, rib
);
2224 nexthop_delete (rib
, nexthop
);
2225 nexthop_free (nexthop
);
2226 rib_queue_add (&zebrad
, rn
);
2229 route_unlock_node (rn
);
2232 /* Add static route into static route configuration. */
2234 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2235 u_char flags
, u_char distance
, u_int32_t vrf_id
)
2238 struct route_node
*rn
;
2239 struct static_ipv4
*si
;
2240 struct static_ipv4
*pp
;
2241 struct static_ipv4
*cp
;
2242 struct static_ipv4
*update
= NULL
;
2243 struct route_table
*stable
;
2246 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2250 /* Lookup static route prefix. */
2251 rn
= route_node_get (stable
, p
);
2255 type
= STATIC_IPV4_GATEWAY
;
2257 type
= STATIC_IPV4_IFNAME
;
2259 type
= STATIC_IPV4_BLACKHOLE
;
2261 /* Do nothing if there is a same static route. */
2262 for (si
= rn
->info
; si
; si
= si
->next
)
2264 if (type
== si
->type
2265 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2266 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2268 if (distance
== si
->distance
)
2270 route_unlock_node (rn
);
2278 /* Distance changed. */
2280 static_delete_ipv4 (p
, gate
, ifname
, update
->distance
, vrf_id
);
2282 /* Make new static route structure. */
2283 si
= XCALLOC (MTYPE_STATIC_IPV4
, sizeof (struct static_ipv4
));
2286 si
->distance
= distance
;
2290 si
->gate
.ipv4
= *gate
;
2292 si
->gate
.ifname
= XSTRDUP (0, ifname
);
2294 /* Add new static route information to the tree with sort by
2295 distance value and gateway address. */
2296 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2298 if (si
->distance
< cp
->distance
)
2300 if (si
->distance
> cp
->distance
)
2302 if (si
->type
== STATIC_IPV4_GATEWAY
&& cp
->type
== STATIC_IPV4_GATEWAY
)
2304 if (ntohl (si
->gate
.ipv4
.s_addr
) < ntohl (cp
->gate
.ipv4
.s_addr
))
2306 if (ntohl (si
->gate
.ipv4
.s_addr
) > ntohl (cp
->gate
.ipv4
.s_addr
))
2311 /* Make linked list. */
2321 /* Install into rib. */
2322 static_install_ipv4 (p
, si
);
2327 /* Delete static route from static route configuration. */
2329 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2330 u_char distance
, u_int32_t vrf_id
)
2333 struct route_node
*rn
;
2334 struct static_ipv4
*si
;
2335 struct route_table
*stable
;
2338 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2342 /* Lookup static route prefix. */
2343 rn
= route_node_lookup (stable
, p
);
2349 type
= STATIC_IPV4_GATEWAY
;
2351 type
= STATIC_IPV4_IFNAME
;
2353 type
= STATIC_IPV4_BLACKHOLE
;
2355 /* Find same static route is the tree */
2356 for (si
= rn
->info
; si
; si
= si
->next
)
2357 if (type
== si
->type
2358 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2359 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2362 /* Can't find static route. */
2365 route_unlock_node (rn
);
2369 /* Install into rib. */
2370 static_uninstall_ipv4 (p
, si
);
2372 /* Unlink static route from linked list. */
2374 si
->prev
->next
= si
->next
;
2376 rn
->info
= si
->next
;
2378 si
->next
->prev
= si
->prev
;
2379 route_unlock_node (rn
);
2381 /* Free static route configuration. */
2383 XFREE (0, si
->gate
.ifname
);
2384 XFREE (MTYPE_STATIC_IPV4
, si
);
2386 route_unlock_node (rn
);
2394 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
2395 struct in6_addr
*gate
, unsigned int ifindex
, int table
)
2397 if (type
== ZEBRA_ROUTE_CONNECT
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)) {
2398 #if defined (MUSICA) || defined (LINUX)
2399 /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
2400 if (p
->prefixlen
== 96)
2405 if (type
== ZEBRA_ROUTE_KERNEL
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)
2406 && p
->prefixlen
== 96 && gate
&& IN6_IS_ADDR_UNSPECIFIED (gate
))
2408 kernel_delete_ipv6_old (p
, gate
, ifindex
, 0, table
);
2415 rib_add_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2416 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
,
2417 u_int32_t metric
, u_char distance
, safi_t safi
)
2420 struct rib
*same
= NULL
;
2421 struct route_table
*table
;
2422 struct route_node
*rn
;
2423 struct nexthop
*nexthop
;
2426 table
= vrf_table (AFI_IP6
, safi
, 0);
2430 /* Make sure mask is applied. */
2431 apply_mask_ipv6 (p
);
2433 /* Set default distance by route type. */
2435 distance
= route_info
[type
].distance
;
2437 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2440 /* Filter bogus route. */
2441 if (rib_bogus_ipv6 (type
, p
, gate
, ifindex
, 0))
2444 /* Lookup route node.*/
2445 rn
= route_node_get (table
, (struct prefix
*) p
);
2447 /* If same type of route are installed, treat it as a implicit
2449 RNODE_FOREACH_RIB (rn
, rib
)
2451 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2454 if (rib
->type
!= type
)
2456 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2461 else if ((nexthop
= rib
->nexthop
) &&
2462 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2463 nexthop
->ifindex
== ifindex
)
2470 /* Allocate new rib structure. */
2471 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2474 rib
->distance
= distance
;
2476 rib
->metric
= metric
;
2477 rib
->table
= vrf_id
;
2478 rib
->nexthop_num
= 0;
2479 rib
->uptime
= time (NULL
);
2481 /* Nexthop settings. */
2485 nexthop_ipv6_ifindex_add (rib
, gate
, ifindex
);
2487 nexthop_ipv6_add (rib
, gate
);
2490 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 rib_addnode (rn
, rib
);
2500 /* Free implicit route.*/
2502 rib_delnode (rn
, same
);
2504 route_unlock_node (rn
);
2508 /* XXX factor with rib_delete_ipv6 */
2510 rib_delete_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2511 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
, safi_t safi
)
2513 struct route_table
*table
;
2514 struct route_node
*rn
;
2516 struct rib
*fib
= NULL
;
2517 struct rib
*same
= NULL
;
2518 struct nexthop
*nexthop
;
2519 char buf1
[INET6_ADDRSTRLEN
];
2520 char buf2
[INET6_ADDRSTRLEN
];
2523 apply_mask_ipv6 (p
);
2526 table
= vrf_table (AFI_IP6
, safi
, 0);
2530 /* Lookup route node. */
2531 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2534 if (IS_ZEBRA_DEBUG_KERNEL
)
2537 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
2538 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2540 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
2543 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
2544 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2548 return ZEBRA_ERR_RTNOEXIST
;
2551 /* Lookup same type route. */
2552 RNODE_FOREACH_RIB (rn
, rib
)
2554 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2557 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2560 if (rib
->type
!= type
)
2562 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2563 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2565 if (nexthop
->ifindex
!= ifindex
)
2570 route_unlock_node (rn
);
2571 route_unlock_node (rn
);
2577 /* Make sure that the route found has the same gateway. */
2578 else if (gate
== NULL
||
2579 ((nexthop
= rib
->nexthop
) &&
2580 (IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
) ||
2581 IPV6_ADDR_SAME (&nexthop
->rgate
.ipv6
, gate
))))
2588 /* If same type of route can't be found and this message is from
2592 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
2595 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2596 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2598 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
2602 if (IS_ZEBRA_DEBUG_KERNEL
)
2605 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
2606 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2608 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
2612 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
2613 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2618 route_unlock_node (rn
);
2619 return ZEBRA_ERR_RTNOEXIST
;
2624 rib_delnode (rn
, same
);
2626 route_unlock_node (rn
);
2630 /* Install static route into rib. */
2632 static_install_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2635 struct route_table
*table
;
2636 struct route_node
*rn
;
2639 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2643 /* Lookup existing route */
2644 rn
= route_node_get (table
, p
);
2645 RNODE_FOREACH_RIB (rn
, rib
)
2647 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2650 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2656 /* Same distance static route is there. Update it with new
2658 route_unlock_node (rn
);
2662 case STATIC_IPV6_GATEWAY
:
2663 nexthop_ipv6_add (rib
, &si
->ipv6
);
2665 case STATIC_IPV6_IFNAME
:
2666 nexthop_ifname_add (rib
, si
->ifname
);
2668 case STATIC_IPV6_GATEWAY_IFNAME
:
2669 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2672 rib_queue_add (&zebrad
, rn
);
2676 /* This is new static route. */
2677 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2679 rib
->type
= ZEBRA_ROUTE_STATIC
;
2680 rib
->distance
= si
->distance
;
2682 rib
->nexthop_num
= 0;
2686 case STATIC_IPV6_GATEWAY
:
2687 nexthop_ipv6_add (rib
, &si
->ipv6
);
2689 case STATIC_IPV6_IFNAME
:
2690 nexthop_ifname_add (rib
, si
->ifname
);
2692 case STATIC_IPV6_GATEWAY_IFNAME
:
2693 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2697 /* Save the flags of this static routes (reject, blackhole) */
2698 rib
->flags
= si
->flags
;
2700 /* Link this rib to the tree. */
2701 rib_addnode (rn
, rib
);
2706 static_ipv6_nexthop_same (struct nexthop
*nexthop
, struct static_ipv6
*si
)
2708 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
2709 && si
->type
== STATIC_IPV6_GATEWAY
2710 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
))
2712 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2713 && si
->type
== STATIC_IPV6_IFNAME
2714 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2716 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
2717 && si
->type
== STATIC_IPV6_GATEWAY_IFNAME
2718 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
)
2719 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2725 static_uninstall_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2727 struct route_table
*table
;
2728 struct route_node
*rn
;
2730 struct nexthop
*nexthop
;
2733 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2737 /* Lookup existing route with type and distance. */
2738 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2742 RNODE_FOREACH_RIB (rn
, rib
)
2744 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2747 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2753 route_unlock_node (rn
);
2757 /* Lookup nexthop. */
2758 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2759 if (static_ipv6_nexthop_same (nexthop
, si
))
2762 /* Can't find nexthop. */
2765 route_unlock_node (rn
);
2769 /* Check nexthop. */
2770 if (rib
->nexthop_num
== 1)
2772 rib_delnode (rn
, rib
);
2776 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2777 rib_uninstall (rn
, rib
);
2778 nexthop_delete (rib
, nexthop
);
2779 nexthop_free (nexthop
);
2780 rib_queue_add (&zebrad
, rn
);
2783 route_unlock_node (rn
);
2786 /* Add static route into static route configuration. */
2788 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2789 const char *ifname
, u_char flags
, u_char distance
,
2792 struct route_node
*rn
;
2793 struct static_ipv6
*si
;
2794 struct static_ipv6
*pp
;
2795 struct static_ipv6
*cp
;
2796 struct route_table
*stable
;
2799 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2804 (type
== STATIC_IPV6_GATEWAY
|| type
== STATIC_IPV6_GATEWAY_IFNAME
))
2808 (type
== STATIC_IPV6_GATEWAY_IFNAME
|| type
== STATIC_IPV6_IFNAME
))
2811 /* Lookup static route prefix. */
2812 rn
= route_node_get (stable
, p
);
2814 /* Do nothing if there is a same static route. */
2815 for (si
= rn
->info
; si
; si
= si
->next
)
2817 if (distance
== si
->distance
2819 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2820 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2822 route_unlock_node (rn
);
2827 /* Make new static route structure. */
2828 si
= XCALLOC (MTYPE_STATIC_IPV6
, sizeof (struct static_ipv6
));
2831 si
->distance
= distance
;
2836 case STATIC_IPV6_GATEWAY
:
2839 case STATIC_IPV6_IFNAME
:
2840 si
->ifname
= XSTRDUP (0, ifname
);
2842 case STATIC_IPV6_GATEWAY_IFNAME
:
2844 si
->ifname
= XSTRDUP (0, ifname
);
2848 /* Add new static route information to the tree with sort by
2849 distance value and gateway address. */
2850 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2852 if (si
->distance
< cp
->distance
)
2854 if (si
->distance
> cp
->distance
)
2858 /* Make linked list. */
2868 /* Install into rib. */
2869 static_install_ipv6 (p
, si
);
2874 /* Delete static route from static route configuration. */
2876 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2877 const char *ifname
, u_char distance
, u_int32_t vrf_id
)
2879 struct route_node
*rn
;
2880 struct static_ipv6
*si
;
2881 struct route_table
*stable
;
2884 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2888 /* Lookup static route prefix. */
2889 rn
= route_node_lookup (stable
, p
);
2893 /* Find same static route is the tree */
2894 for (si
= rn
->info
; si
; si
= si
->next
)
2895 if (distance
== si
->distance
2897 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2898 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2901 /* Can't find static route. */
2904 route_unlock_node (rn
);
2908 /* Install into rib. */
2909 static_uninstall_ipv6 (p
, si
);
2911 /* Unlink static route from linked list. */
2913 si
->prev
->next
= si
->next
;
2915 rn
->info
= si
->next
;
2917 si
->next
->prev
= si
->prev
;
2919 /* Free static route configuration. */
2921 XFREE (0, si
->ifname
);
2922 XFREE (MTYPE_STATIC_IPV6
, si
);
2926 #endif /* HAVE_IPV6 */
2928 /* RIB update function. */
2932 struct route_node
*rn
;
2933 struct route_table
*table
;
2935 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2937 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2938 if (rnode_to_ribs (rn
))
2939 rib_queue_add (&zebrad
, rn
);
2941 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2943 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2944 if (rnode_to_ribs (rn
))
2945 rib_queue_add (&zebrad
, rn
);
2949 /* Remove all routes which comes from non main table. */
2951 rib_weed_table (struct route_table
*table
)
2953 struct route_node
*rn
;
2958 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2959 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2961 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2964 if (rib
->table
!= zebrad
.rtm_table_default
&&
2965 rib
->table
!= RT_TABLE_MAIN
)
2966 rib_delnode (rn
, rib
);
2970 /* Delete all routes from non main table. */
2972 rib_weed_tables (void)
2974 rib_weed_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2975 rib_weed_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2978 /* Delete self installed routes after zebra is relaunched. */
2980 rib_sweep_table (struct route_table
*table
)
2982 struct route_node
*rn
;
2988 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2989 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
2991 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2994 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
2995 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
2997 ret
= rib_uninstall_kernel (rn
, rib
);
2999 rib_delnode (rn
, rib
);
3004 /* Sweep all RIB tables. */
3006 rib_sweep_route (void)
3008 rib_sweep_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
3009 rib_sweep_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3012 /* Remove specific by protocol routes from 'table'. */
3013 static unsigned long
3014 rib_score_proto_table (u_char proto
, struct route_table
*table
)
3016 struct route_node
*rn
;
3019 unsigned long n
= 0;
3022 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3023 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3025 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3027 if (rib
->type
== proto
)
3029 rib_delnode (rn
, rib
);
3037 /* Remove specific by protocol routes. */
3039 rib_score_proto (u_char proto
)
3041 return rib_score_proto_table (proto
, vrf_table (AFI_IP
, SAFI_UNICAST
, 0))
3042 +rib_score_proto_table (proto
, vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3045 /* Close RIB and clean up kernel routes. */
3047 rib_close_table (struct route_table
*table
)
3049 struct route_node
*rn
;
3053 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3054 RNODE_FOREACH_RIB (rn
, rib
)
3056 if (!CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
3059 if (! RIB_SYSTEM_ROUTE (rib
))
3060 rib_uninstall_kernel (rn
, rib
);
3064 /* Close all RIB tables. */
3068 rib_close_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
3069 rib_close_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3072 /* Routing information base initialize. */
3076 rib_queue_init (&zebrad
);
3077 /* VRF initialization. */
3084 * Get the first vrf id that is greater than the given vrf id if any.
3086 * Returns TRUE if a vrf id was found, FALSE otherwise.
3089 vrf_id_get_next (uint32_t id
, uint32_t *next_id_p
)
3091 while (++id
< vector_active (vrf_vector
))
3093 if (vrf_lookup (id
))
3104 * rib_tables_iter_next
3106 * Returns the next table in the iteration.
3108 struct route_table
*
3109 rib_tables_iter_next (rib_tables_iter_t
*iter
)
3111 struct route_table
*table
;
3114 * Array that helps us go over all AFI/SAFI combinations via one
3121 { AFI_IP
, SAFI_UNICAST
},
3122 { AFI_IP
, SAFI_MULTICAST
},
3123 { AFI_IP6
, SAFI_UNICAST
},
3124 { AFI_IP6
, SAFI_MULTICAST
},
3129 switch (iter
->state
)
3132 case RIB_TABLES_ITER_S_INIT
:
3134 iter
->afi_safi_ix
= -1;
3138 case RIB_TABLES_ITER_S_ITERATING
:
3139 iter
->afi_safi_ix
++;
3143 while (iter
->afi_safi_ix
< (int) ZEBRA_NUM_OF (afi_safis
))
3145 table
= vrf_table (afi_safis
[iter
->afi_safi_ix
].afi
,
3146 afi_safis
[iter
->afi_safi_ix
].safi
,
3151 iter
->afi_safi_ix
++;
3155 * Found another table in this vrf.
3161 * Done with all tables in the current vrf, go to the next
3164 if (!vrf_id_get_next (iter
->vrf_id
, &iter
->vrf_id
))
3167 iter
->afi_safi_ix
= 0;
3172 case RIB_TABLES_ITER_S_DONE
:
3177 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3179 iter
->state
= RIB_TABLES_ITER_S_DONE
;