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"
43 #include "zebra/zebra_fpm.h"
45 /* Default rtm_table for all clients */
46 extern struct zebra_t zebrad
;
48 /* Hold time for RIB process, should be very minimal.
49 * it is useful to able to set it otherwise for testing, hence exported
50 * as global here for test-rig code.
52 int rib_process_hold_time
= 10;
54 /* Each route type's string and default distance value. */
59 } route_info
[ZEBRA_ROUTE_MAX
] =
61 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
62 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
63 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
64 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
65 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
66 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
67 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
68 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
69 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
70 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
71 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 95},
72 /* no entry/default: 150 */
75 /* Vector for routing table. */
76 static vector vrf_vector
;
82 vrf_table_create (struct vrf
*vrf
, afi_t afi
, safi_t safi
)
84 rib_table_info_t
*info
;
85 struct route_table
*table
;
87 assert (!vrf
->table
[afi
][safi
]);
89 table
= route_table_init ();
90 vrf
->table
[afi
][safi
] = table
;
92 info
= XCALLOC (MTYPE_RIB_TABLE_INFO
, sizeof (*info
));
99 /* Allocate new VRF. */
101 vrf_alloc (const char *name
)
105 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
109 vrf
->name
= XSTRDUP (MTYPE_VRF_NAME
, name
);
111 /* Allocate routing table and static table. */
112 vrf_table_create (vrf
, AFI_IP
, SAFI_UNICAST
);
113 vrf_table_create (vrf
, AFI_IP6
, SAFI_UNICAST
);
114 vrf
->stable
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
115 vrf
->stable
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
116 vrf_table_create (vrf
, AFI_IP
, SAFI_MULTICAST
);
117 vrf_table_create (vrf
, AFI_IP6
, SAFI_MULTICAST
);
118 vrf
->stable
[AFI_IP
][SAFI_MULTICAST
] = route_table_init ();
119 vrf
->stable
[AFI_IP6
][SAFI_MULTICAST
] = route_table_init ();
125 /* Lookup VRF by identifier. */
127 vrf_lookup (u_int32_t id
)
129 return vector_lookup (vrf_vector
, id
);
132 /* Initialize VRF. */
136 struct vrf
*default_table
;
138 /* Allocate VRF vector. */
139 vrf_vector
= vector_init (1);
141 /* Allocate default main table. */
142 default_table
= vrf_alloc ("Default-IP-Routing-Table");
144 /* Default table index must be 0. */
145 vector_set_index (vrf_vector
, 0, default_table
);
148 /* Lookup route table. */
150 vrf_table (afi_t afi
, safi_t safi
, u_int32_t id
)
154 vrf
= vrf_lookup (id
);
158 if( afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
161 return vrf
->table
[afi
][safi
];
164 /* Lookup static route table. */
166 vrf_static_table (afi_t afi
, safi_t safi
, u_int32_t id
)
170 vrf
= vrf_lookup (id
);
174 if( afi
>= AFI_MAX
|| safi
>= SAFI_MAX
)
177 return vrf
->stable
[afi
][safi
];
181 * nexthop_type_to_str
184 nexthop_type_to_str (enum nexthop_types_t nh_type
)
186 static const char *desc
[] = {
188 "Directly connected",
191 "IPv4 nexthop with ifindex",
192 "IPv4 nexthop with ifname",
194 "IPv6 nexthop with ifindex",
195 "IPv6 nexthop with ifname",
199 if (nh_type
>= ZEBRA_NUM_OF (desc
))
200 return "<Invalid nh type>";
202 return desc
[nh_type
];
205 /* Add nexthop to the end of a nexthop list. */
207 _nexthop_add (struct nexthop
**target
, struct nexthop
*nexthop
)
209 struct nexthop
*last
;
211 for (last
= *target
; last
&& last
->next
; last
= last
->next
)
214 last
->next
= nexthop
;
217 nexthop
->prev
= last
;
220 /* Add nexthop to the end of a rib node's nexthop list */
222 nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
224 _nexthop_add(&rib
->nexthop
, nexthop
);
228 /* Delete specified nexthop from the list. */
230 nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
233 nexthop
->next
->prev
= nexthop
->prev
;
235 nexthop
->prev
->next
= nexthop
->next
;
237 rib
->nexthop
= nexthop
->next
;
241 static void nexthops_free(struct nexthop
*nexthop
);
245 nexthop_free (struct nexthop
*nexthop
)
248 XFREE (0, nexthop
->ifname
);
249 if (nexthop
->resolved
)
250 nexthops_free(nexthop
->resolved
);
251 XFREE (MTYPE_NEXTHOP
, nexthop
);
254 /* Frees a list of nexthops */
256 nexthops_free (struct nexthop
*nexthop
)
258 struct nexthop
*nh
, *next
;
260 for (nh
= nexthop
; nh
; nh
= next
)
268 nexthop_ifindex_add (struct rib
*rib
, unsigned int ifindex
)
270 struct nexthop
*nexthop
;
272 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
273 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
274 nexthop
->ifindex
= ifindex
;
276 nexthop_add (rib
, nexthop
);
282 nexthop_ifname_add (struct rib
*rib
, char *ifname
)
284 struct nexthop
*nexthop
;
286 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
287 nexthop
->type
= NEXTHOP_TYPE_IFNAME
;
288 nexthop
->ifname
= XSTRDUP (0, ifname
);
290 nexthop_add (rib
, nexthop
);
296 nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
298 struct nexthop
*nexthop
;
300 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
301 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
302 nexthop
->gate
.ipv4
= *ipv4
;
304 nexthop
->src
.ipv4
= *src
;
306 nexthop_add (rib
, nexthop
);
312 nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
313 struct in_addr
*src
, unsigned int ifindex
)
315 struct nexthop
*nexthop
;
317 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
318 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
319 nexthop
->gate
.ipv4
= *ipv4
;
321 nexthop
->src
.ipv4
= *src
;
322 nexthop
->ifindex
= ifindex
;
324 nexthop_add (rib
, nexthop
);
331 nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
333 struct nexthop
*nexthop
;
335 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
336 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
337 nexthop
->gate
.ipv6
= *ipv6
;
339 nexthop_add (rib
, nexthop
);
344 static struct nexthop
*
345 nexthop_ipv6_ifname_add (struct rib
*rib
, struct in6_addr
*ipv6
,
348 struct nexthop
*nexthop
;
350 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
351 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFNAME
;
352 nexthop
->gate
.ipv6
= *ipv6
;
353 nexthop
->ifname
= XSTRDUP (0, ifname
);
355 nexthop_add (rib
, nexthop
);
360 static struct nexthop
*
361 nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
362 unsigned int ifindex
)
364 struct nexthop
*nexthop
;
366 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
367 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
368 nexthop
->gate
.ipv6
= *ipv6
;
369 nexthop
->ifindex
= ifindex
;
371 nexthop_add (rib
, nexthop
);
375 #endif /* HAVE_IPV6 */
378 nexthop_blackhole_add (struct rib
*rib
)
380 struct nexthop
*nexthop
;
382 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
383 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
384 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
386 nexthop_add (rib
, nexthop
);
391 /* This method checks whether a recursive nexthop has at
392 * least one resolved nexthop in the fib.
395 nexthop_has_fib_child(struct nexthop
*nexthop
)
399 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
402 for (nh
= nexthop
->resolved
; nh
; nh
= nh
->next
)
403 if (CHECK_FLAG (nh
->flags
, NEXTHOP_FLAG_FIB
))
409 /* If force flag is not set, do not modify falgs at all for uninstall
410 the route from FIB. */
412 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
413 struct route_node
*top
)
415 struct prefix_ipv4 p
;
416 struct route_table
*table
;
417 struct route_node
*rn
;
420 struct nexthop
*newhop
;
421 struct nexthop
*resolved_hop
;
423 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
424 nexthop
->ifindex
= 0;
428 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
429 nexthops_free(nexthop
->resolved
);
430 nexthop
->resolved
= NULL
;
433 /* Make lookup prefix. */
434 memset (&p
, 0, sizeof (struct prefix_ipv4
));
436 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
437 p
.prefix
= nexthop
->gate
.ipv4
;
440 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
444 rn
= route_node_match (table
, (struct prefix
*) &p
);
447 route_unlock_node (rn
);
449 /* If lookup self prefix return immediately. */
453 /* Pick up selected route. */
454 RNODE_FOREACH_RIB (rn
, match
)
456 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
458 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
462 /* If there is no selected route or matched route is EGP, go up
465 || match
->type
== ZEBRA_ROUTE_BGP
)
469 } while (rn
&& rn
->info
== NULL
);
471 route_lock_node (rn
);
475 /* If the longest prefix match for the nexthop yields
476 * a blackhole, mark it as inactive. */
477 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
478 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
481 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
483 /* Directly point connected route. */
484 newhop
= match
->nexthop
;
485 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
486 nexthop
->ifindex
= newhop
->ifindex
;
490 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
493 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
494 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
495 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
499 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
501 resolved_hop
= XCALLOC(MTYPE_NEXTHOP
, sizeof (struct nexthop
));
502 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
503 /* If the resolving route specifies a gateway, use it */
504 if (newhop
->type
== NEXTHOP_TYPE_IPV4
505 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
506 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFNAME
)
508 resolved_hop
->type
= newhop
->type
;
509 resolved_hop
->gate
.ipv4
= newhop
->gate
.ipv4
;
513 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
514 resolved_hop
->ifindex
= newhop
->ifindex
;
518 /* If the resolving route is an interface route,
519 * it means the gateway we are looking up is connected
520 * to that interface. (The actual network is _not_ onlink).
521 * Therefore, the resolved route should have the original
522 * gateway as nexthop as it is directly connected.
524 * On Linux, we have to set the onlink netlink flag because
525 * otherwise, the kernel won't accept the route. */
526 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
527 || newhop
->type
== NEXTHOP_TYPE_IFNAME
)
529 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
530 resolved_hop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
531 resolved_hop
->gate
.ipv4
= nexthop
->gate
.ipv4
;
532 resolved_hop
->ifindex
= newhop
->ifindex
;
535 _nexthop_add(&nexthop
->resolved
, resolved_hop
);
551 /* If force flag is not set, do not modify falgs at all for uninstall
552 the route from FIB. */
554 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
555 struct route_node
*top
)
557 struct prefix_ipv6 p
;
558 struct route_table
*table
;
559 struct route_node
*rn
;
562 struct nexthop
*newhop
;
563 struct nexthop
*resolved_hop
;
565 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
566 nexthop
->ifindex
= 0;
570 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
571 nexthops_free(nexthop
->resolved
);
572 nexthop
->resolved
= NULL
;
575 /* Make lookup prefix. */
576 memset (&p
, 0, sizeof (struct prefix_ipv6
));
578 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
579 p
.prefix
= nexthop
->gate
.ipv6
;
582 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
586 rn
= route_node_match (table
, (struct prefix
*) &p
);
589 route_unlock_node (rn
);
591 /* If lookup self prefix return immediately. */
595 /* Pick up selected route. */
596 RNODE_FOREACH_RIB (rn
, match
)
598 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
600 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
604 /* If there is no selected route or matched route is EGP, go up
607 || match
->type
== ZEBRA_ROUTE_BGP
)
611 } while (rn
&& rn
->info
== NULL
);
613 route_lock_node (rn
);
617 /* If the longest prefix match for the nexthop yields
618 * a blackhole, mark it as inactive. */
619 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_BLACKHOLE
)
620 || CHECK_FLAG (match
->flags
, ZEBRA_FLAG_REJECT
))
623 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
625 /* Directly point connected route. */
626 newhop
= match
->nexthop
;
628 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
629 nexthop
->ifindex
= newhop
->ifindex
;
633 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
636 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
637 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
638 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
642 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
644 resolved_hop
= XCALLOC(MTYPE_NEXTHOP
, sizeof (struct nexthop
));
645 SET_FLAG (resolved_hop
->flags
, NEXTHOP_FLAG_ACTIVE
);
646 /* See nexthop_active_ipv4 for a description how the
647 * resolved nexthop is constructed. */
648 if (newhop
->type
== NEXTHOP_TYPE_IPV6
649 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
650 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
652 resolved_hop
->type
= newhop
->type
;
653 resolved_hop
->gate
.ipv6
= newhop
->gate
.ipv6
;
657 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
658 resolved_hop
->ifindex
= newhop
->ifindex
;
662 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
663 || newhop
->type
== NEXTHOP_TYPE_IFNAME
)
665 resolved_hop
->flags
|= NEXTHOP_FLAG_ONLINK
;
666 resolved_hop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
667 resolved_hop
->gate
.ipv6
= nexthop
->gate
.ipv6
;
668 resolved_hop
->ifindex
= newhop
->ifindex
;
671 _nexthop_add(&nexthop
->resolved
, resolved_hop
);
685 #endif /* HAVE_IPV6 */
688 rib_match_ipv4 (struct in_addr addr
)
690 struct prefix_ipv4 p
;
691 struct route_table
*table
;
692 struct route_node
*rn
;
694 struct nexthop
*newhop
, *tnewhop
;
698 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
702 memset (&p
, 0, sizeof (struct prefix_ipv4
));
704 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
707 rn
= route_node_match (table
, (struct prefix
*) &p
);
711 route_unlock_node (rn
);
713 /* Pick up selected route. */
714 RNODE_FOREACH_RIB (rn
, match
)
716 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
718 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
722 /* If there is no selected route or matched route is EGP, go up
725 || match
->type
== ZEBRA_ROUTE_BGP
)
729 } while (rn
&& rn
->info
== NULL
);
731 route_lock_node (rn
);
735 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
736 /* Directly point connected route. */
740 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
741 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
751 rib_lookup_ipv4 (struct prefix_ipv4
*p
)
753 struct route_table
*table
;
754 struct route_node
*rn
;
756 struct nexthop
*nexthop
, *tnexthop
;
760 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
764 rn
= route_node_lookup (table
, (struct prefix
*) p
);
766 /* No route for this prefix. */
771 route_unlock_node (rn
);
773 RNODE_FOREACH_RIB (rn
, match
)
775 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
777 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
781 if (! match
|| match
->type
== ZEBRA_ROUTE_BGP
)
784 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
787 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
788 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
795 * This clone function, unlike its original rib_lookup_ipv4(), checks
796 * if specified IPv4 route record (prefix/mask -> gate) exists in
797 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
801 * 0: exact match found
802 * 1: a match was found with a different gate
803 * 2: connected route found
804 * 3: no matches found
807 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
)
809 struct route_table
*table
;
810 struct route_node
*rn
;
812 struct nexthop
*nexthop
, *tnexthop
;
817 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
819 return ZEBRA_RIB_LOOKUP_ERROR
;
821 /* Scan the RIB table for exactly matching RIB entry. */
822 rn
= route_node_lookup (table
, (struct prefix
*) p
);
824 /* No route for this prefix. */
826 return ZEBRA_RIB_NOTFOUND
;
829 route_unlock_node (rn
);
831 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
832 RNODE_FOREACH_RIB (rn
, match
)
834 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
836 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
840 /* None such found :( */
842 return ZEBRA_RIB_NOTFOUND
;
844 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
845 return ZEBRA_RIB_FOUND_CONNECTED
;
847 /* Ok, we have a cood candidate, let's check it's nexthop list... */
849 for (ALL_NEXTHOPS_RO(match
->nexthop
, nexthop
, tnexthop
, recursing
))
850 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
853 if (nexthop
->gate
.ipv4
.s_addr
== sockunion2ip (qgate
))
854 return ZEBRA_RIB_FOUND_EXACT
;
855 if (IS_ZEBRA_DEBUG_RIB
)
857 char gate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
858 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
859 inet_ntop (AF_INET
, &sockunion2ip(qgate
), qgate_buf
, INET_ADDRSTRLEN
);
860 zlog_debug ("%s: qgate == %s, %s == %s", __func__
,
861 qgate_buf
, recursing
? "rgate" : "gate", gate_buf
);
866 return ZEBRA_RIB_FOUND_NOGATE
;
868 return ZEBRA_RIB_NOTFOUND
;
873 rib_match_ipv6 (struct in6_addr
*addr
)
875 struct prefix_ipv6 p
;
876 struct route_table
*table
;
877 struct route_node
*rn
;
879 struct nexthop
*newhop
, *tnewhop
;
883 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
887 memset (&p
, 0, sizeof (struct prefix_ipv6
));
889 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
890 IPV6_ADDR_COPY (&p
.prefix
, addr
);
892 rn
= route_node_match (table
, (struct prefix
*) &p
);
896 route_unlock_node (rn
);
898 /* Pick up selected route. */
899 RNODE_FOREACH_RIB (rn
, match
)
901 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
903 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
907 /* If there is no selected route or matched route is EGP, go up
910 || match
->type
== ZEBRA_ROUTE_BGP
)
914 } while (rn
&& rn
->info
== NULL
);
916 route_lock_node (rn
);
920 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
921 /* Directly point connected route. */
925 for (ALL_NEXTHOPS_RO(match
->nexthop
, newhop
, tnewhop
, recursing
))
926 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
934 #endif /* HAVE_IPV6 */
936 #define RIB_SYSTEM_ROUTE(R) \
937 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
939 /* This function verifies reachability of one given nexthop, which can be
940 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
941 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
942 * nexthop->ifindex will be updated appropriately as well.
943 * An existing route map can turn (otherwise active) nexthop into inactive, but
946 * The return value is the final value of 'ACTIVE' flag.
950 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
951 struct nexthop
*nexthop
, int set
)
953 rib_table_info_t
*info
= rn
->table
->info
;
954 struct interface
*ifp
;
955 route_map_result_t ret
= RMAP_MATCH
;
956 extern char *proto_rm
[AFI_MAX
][ZEBRA_ROUTE_MAX
+1];
957 struct route_map
*rmap
;
961 switch (nexthop
->type
)
963 case NEXTHOP_TYPE_IFINDEX
:
964 ifp
= if_lookup_by_index (nexthop
->ifindex
);
965 if (ifp
&& if_is_operative(ifp
))
966 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
968 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
970 case NEXTHOP_TYPE_IPV6_IFNAME
:
972 case NEXTHOP_TYPE_IFNAME
:
973 ifp
= if_lookup_by_name (nexthop
->ifname
);
974 if (ifp
&& if_is_operative(ifp
))
977 nexthop
->ifindex
= ifp
->ifindex
;
978 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
983 nexthop
->ifindex
= 0;
984 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
987 case NEXTHOP_TYPE_IPV4
:
988 case NEXTHOP_TYPE_IPV4_IFINDEX
:
990 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
991 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
993 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
996 case NEXTHOP_TYPE_IPV6
:
998 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
999 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1001 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1003 case NEXTHOP_TYPE_IPV6_IFINDEX
:
1005 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
1007 ifp
= if_lookup_by_index (nexthop
->ifindex
);
1008 if (ifp
&& if_is_operative(ifp
))
1009 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1011 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1015 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
1016 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1018 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1021 #endif /* HAVE_IPV6 */
1022 case NEXTHOP_TYPE_BLACKHOLE
:
1023 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1028 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
1031 /* XXX: What exactly do those checks do? Do we support
1032 * e.g. IPv4 routes with IPv6 nexthops or vice versa? */
1033 if (RIB_SYSTEM_ROUTE(rib
) ||
1034 (family
== AFI_IP
&& rn
->p
.family
!= AF_INET
) ||
1035 (family
== AFI_IP6
&& rn
->p
.family
!= AF_INET6
))
1036 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1038 /* The original code didn't determine the family correctly
1039 * e.g. for NEXTHOP_TYPE_IFINDEX. Retrieve the correct afi
1040 * from the rib_table_info in those cases.
1041 * Possibly it may be better to use only the rib_table_info
1048 if (rib
->type
>= 0 && rib
->type
< ZEBRA_ROUTE_MAX
&&
1049 proto_rm
[family
][rib
->type
])
1050 rmap
= route_map_lookup_by_name (proto_rm
[family
][rib
->type
]);
1051 if (!rmap
&& proto_rm
[family
][ZEBRA_ROUTE_MAX
])
1052 rmap
= route_map_lookup_by_name (proto_rm
[family
][ZEBRA_ROUTE_MAX
]);
1054 ret
= route_map_apply(rmap
, &rn
->p
, RMAP_ZEBRA
, nexthop
);
1057 if (ret
== RMAP_DENYMATCH
)
1058 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1059 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1062 /* Iterate over all nexthops of the given RIB entry and refresh their
1063 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
1064 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
1065 * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
1066 * transparently passed to nexthop_active_check().
1068 * Return value is the new number of active nexthops.
1072 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
1074 struct nexthop
*nexthop
;
1075 unsigned int prev_active
, prev_index
, new_active
;
1077 rib
->nexthop_active_num
= 0;
1078 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
1080 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1082 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
1083 prev_index
= nexthop
->ifindex
;
1084 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
1085 rib
->nexthop_active_num
++;
1086 if (prev_active
!= new_active
||
1087 prev_index
!= nexthop
->ifindex
)
1088 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
1090 return rib
->nexthop_active_num
;
1096 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
)
1099 struct nexthop
*nexthop
, *tnexthop
;
1103 * Make sure we update the FPM any time we send new information to
1106 zfpm_trigger_update (rn
, "installing in kernel");
1107 switch (PREFIX_FAMILY (&rn
->p
))
1110 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
1114 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
1116 #endif /* HAVE_IPV6 */
1119 /* This condition is never met, if we are using rt_socket.c */
1122 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1123 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1127 /* Uninstall the route from kernel. */
1129 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
1132 struct nexthop
*nexthop
, *tnexthop
;
1136 * Make sure we update the FPM any time we send new information to
1139 zfpm_trigger_update (rn
, "uninstalling from kernel");
1141 switch (PREFIX_FAMILY (&rn
->p
))
1144 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
1148 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
1150 #endif /* HAVE_IPV6 */
1153 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1154 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1159 /* Uninstall the route from kernel. */
1161 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
1163 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1165 zfpm_trigger_update (rn
, "rib_uninstall");
1167 redistribute_delete (&rn
->p
, rib
);
1168 if (! RIB_SYSTEM_ROUTE (rib
))
1169 rib_uninstall_kernel (rn
, rib
);
1170 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
1174 static void rib_unlink (struct route_node
*, struct rib
*);
1177 * rib_can_delete_dest
1179 * Returns TRUE if the given dest can be deleted from the table.
1182 rib_can_delete_dest (rib_dest_t
*dest
)
1190 * Don't delete the dest if we have to update the FPM about this
1193 if (CHECK_FLAG (dest
->flags
, RIB_DEST_UPDATE_FPM
) ||
1194 CHECK_FLAG (dest
->flags
, RIB_DEST_SENT_TO_FPM
))
1203 * Garbage collect the rib dest corresponding to the given route node
1206 * Returns TRUE if the dest was deleted, FALSE otherwise.
1209 rib_gc_dest (struct route_node
*rn
)
1212 char buf
[INET6_ADDRSTRLEN
];
1214 dest
= rib_dest_from_rnode (rn
);
1218 if (!rib_can_delete_dest (dest
))
1221 if (IS_ZEBRA_DEBUG_RIB
)
1223 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, sizeof (buf
));
1224 zlog_debug ("%s: %s/%d: removing dest from table", __func__
,
1225 buf
, rn
->p
.prefixlen
);
1229 XFREE (MTYPE_RIB_DEST
, dest
);
1233 * Release the one reference that we keep on the route node.
1235 route_unlock_node (rn
);
1239 /* Core function for processing routing information base. */
1241 rib_process (struct route_node
*rn
)
1245 struct rib
*fib
= NULL
;
1246 struct rib
*select
= NULL
;
1247 struct rib
*del
= NULL
;
1249 struct nexthop
*nexthop
= NULL
, *tnexthop
;
1251 char buf
[INET6_ADDRSTRLEN
];
1255 if (IS_ZEBRA_DEBUG_RIB
|| IS_ZEBRA_DEBUG_RIB_Q
)
1256 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1258 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
1260 /* Currently installed rib. */
1261 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1263 assert (fib
== NULL
);
1267 /* Unlock removed routes, so they'll be freed, bar the FIB entry,
1268 * which we need to do do further work with below.
1270 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1274 if (IS_ZEBRA_DEBUG_RIB
)
1275 zlog_debug ("%s: %s/%d: rn %p, removing rib %p", __func__
,
1276 buf
, rn
->p
.prefixlen
, rn
, rib
);
1277 rib_unlink (rn
, rib
);
1285 /* Skip unreachable nexthop. */
1286 if (! nexthop_active_update (rn
, rib
, 0))
1289 /* Infinit distance. */
1290 if (rib
->distance
== DISTANCE_INFINITY
)
1293 /* Newly selected rib, the common case. */
1300 /* filter route selection in following order:
1301 * - connected beats other types
1302 * - lower distance beats higher
1303 * - lower metric beats higher for equal distance
1304 * - last, hence oldest, route wins tie break.
1307 /* Connected routes. Pick the last connected
1308 * route of the set of lowest metric connected routes.
1310 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1312 if (select
->type
!= ZEBRA_ROUTE_CONNECT
1313 || rib
->metric
<= select
->metric
)
1317 else if (select
->type
== ZEBRA_ROUTE_CONNECT
)
1320 /* higher distance loses */
1321 if (rib
->distance
> select
->distance
)
1325 if (rib
->distance
< select
->distance
)
1331 /* metric tie-breaks equal distance */
1332 if (rib
->metric
<= select
->metric
)
1334 } /* RNODE_FOREACH_RIB_SAFE */
1336 /* After the cycle is finished, the following pointers will be set:
1337 * select --- the winner RIB entry, if any was found, otherwise NULL
1338 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1339 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1343 /* Same RIB entry is selected. Update FIB and finish. */
1344 if (select
&& select
== fib
)
1346 if (IS_ZEBRA_DEBUG_RIB
)
1347 zlog_debug ("%s: %s/%d: Updating existing route, select %p, fib %p",
1348 __func__
, buf
, rn
->p
.prefixlen
, select
, fib
);
1349 if (CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
1351 zfpm_trigger_update (rn
, "updating existing route");
1353 redistribute_delete (&rn
->p
, select
);
1354 if (! RIB_SYSTEM_ROUTE (select
))
1355 rib_uninstall_kernel (rn
, select
);
1357 /* Set real nexthop. */
1358 nexthop_active_update (rn
, select
, 1);
1360 if (! RIB_SYSTEM_ROUTE (select
))
1361 rib_install_kernel (rn
, select
);
1362 redistribute_add (&rn
->p
, select
);
1364 else if (! RIB_SYSTEM_ROUTE (select
))
1366 /* Housekeeping code to deal with
1367 race conditions in kernel with linux
1368 netlink reporting interface up before IPv4 or IPv6 protocol
1369 is ready to add routes.
1370 This makes sure the routes are IN the kernel.
1373 for (ALL_NEXTHOPS_RO(select
->nexthop
, nexthop
, tnexthop
, recursing
))
1374 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1380 rib_install_kernel (rn
, select
);
1385 /* At this point we either haven't found the best RIB entry or it is
1386 * different from what we currently intend to flag with SELECTED. In both
1387 * cases, if a RIB block is present in FIB, it should be withdrawn.
1391 if (IS_ZEBRA_DEBUG_RIB
)
1392 zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__
,
1393 buf
, rn
->p
.prefixlen
, fib
);
1395 zfpm_trigger_update (rn
, "removing existing route");
1397 redistribute_delete (&rn
->p
, fib
);
1398 if (! RIB_SYSTEM_ROUTE (fib
))
1399 rib_uninstall_kernel (rn
, fib
);
1400 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1402 /* Set real nexthop. */
1403 nexthop_active_update (rn
, fib
, 1);
1406 /* Regardless of some RIB entry being SELECTED or not before, now we can
1407 * tell, that if a new winner exists, FIB is still not updated with this
1408 * data, but ready to be.
1412 if (IS_ZEBRA_DEBUG_RIB
)
1413 zlog_debug ("%s: %s/%d: Adding route, select %p", __func__
, buf
,
1414 rn
->p
.prefixlen
, select
);
1416 zfpm_trigger_update (rn
, "new route selected");
1418 /* Set real nexthop. */
1419 nexthop_active_update (rn
, select
, 1);
1421 if (! RIB_SYSTEM_ROUTE (select
))
1422 rib_install_kernel (rn
, select
);
1423 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1424 redistribute_add (&rn
->p
, select
);
1427 /* FIB route was removed, should be deleted */
1430 if (IS_ZEBRA_DEBUG_RIB
)
1431 zlog_debug ("%s: %s/%d: Deleting fib %p, rn %p", __func__
, buf
,
1432 rn
->p
.prefixlen
, del
, rn
);
1433 rib_unlink (rn
, del
);
1437 if (IS_ZEBRA_DEBUG_RIB_Q
)
1438 zlog_debug ("%s: %s/%d: rn %p dequeued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1441 * Check if the dest can be deleted now.
1446 /* Take a list of route_node structs and return 1, if there was a record
1447 * picked from it and processed by rib_process(). Don't process more,
1448 * than one RN record; operate only in the specified sub-queue.
1451 process_subq (struct list
* subq
, u_char qindex
)
1453 struct listnode
*lnode
= listhead (subq
);
1454 struct route_node
*rnode
;
1459 rnode
= listgetdata (lnode
);
1460 rib_process (rnode
);
1463 UNSET_FLAG (rib_dest_from_rnode (rnode
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1468 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1469 __func__
, rnode
, rnode
->lock
);
1470 zlog_backtrace(LOG_DEBUG
);
1473 route_unlock_node (rnode
);
1474 list_delete_node (subq
, lnode
);
1478 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1479 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1480 * is pointed to the meta queue structure.
1482 static wq_item_status
1483 meta_queue_process (struct work_queue
*dummy
, void *data
)
1485 struct meta_queue
* mq
= data
;
1488 for (i
= 0; i
< MQ_SIZE
; i
++)
1489 if (process_subq (mq
->subq
[i
], i
))
1494 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1498 * Map from rib types to queue type (priority) in meta queue
1500 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1501 [ZEBRA_ROUTE_SYSTEM
] = 4,
1502 [ZEBRA_ROUTE_KERNEL
] = 0,
1503 [ZEBRA_ROUTE_CONNECT
] = 0,
1504 [ZEBRA_ROUTE_STATIC
] = 1,
1505 [ZEBRA_ROUTE_RIP
] = 2,
1506 [ZEBRA_ROUTE_RIPNG
] = 2,
1507 [ZEBRA_ROUTE_OSPF
] = 2,
1508 [ZEBRA_ROUTE_OSPF6
] = 2,
1509 [ZEBRA_ROUTE_ISIS
] = 2,
1510 [ZEBRA_ROUTE_BGP
] = 3,
1511 [ZEBRA_ROUTE_HSLS
] = 4,
1512 [ZEBRA_ROUTE_BABEL
] = 2,
1515 /* Look into the RN and queue it into one or more priority queues,
1516 * increasing the size for each data push done.
1519 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
1522 char buf
[INET6_ADDRSTRLEN
];
1524 if (IS_ZEBRA_DEBUG_RIB_Q
)
1525 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1527 RNODE_FOREACH_RIB (rn
, rib
)
1529 u_char qindex
= meta_queue_map
[rib
->type
];
1531 /* Invariant: at this point we always have rn->info set. */
1532 if (CHECK_FLAG (rib_dest_from_rnode (rn
)->flags
,
1533 RIB_ROUTE_QUEUED (qindex
)))
1535 if (IS_ZEBRA_DEBUG_RIB_Q
)
1536 zlog_debug ("%s: %s/%d: rn %p is already queued in sub-queue %u",
1537 __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1541 SET_FLAG (rib_dest_from_rnode (rn
)->flags
, RIB_ROUTE_QUEUED (qindex
));
1542 listnode_add (mq
->subq
[qindex
], rn
);
1543 route_lock_node (rn
);
1546 if (IS_ZEBRA_DEBUG_RIB_Q
)
1547 zlog_debug ("%s: %s/%d: queued rn %p into sub-queue %u",
1548 __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1552 /* Add route_node to work queue and schedule processing */
1554 rib_queue_add (struct zebra_t
*zebra
, struct route_node
*rn
)
1556 char buf
[INET_ADDRSTRLEN
];
1557 assert (zebra
&& rn
);
1559 if (IS_ZEBRA_DEBUG_RIB_Q
)
1560 inet_ntop (AF_INET
, &rn
->p
.u
.prefix
, buf
, INET_ADDRSTRLEN
);
1562 /* Pointless to queue a route_node with no RIB entries to add or remove */
1563 if (!rnode_to_ribs (rn
))
1565 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1566 __func__
, rn
, rn
->lock
);
1567 zlog_backtrace(LOG_DEBUG
);
1571 if (IS_ZEBRA_DEBUG_RIB_Q
)
1572 zlog_info ("%s: %s/%d: work queue added", __func__
, buf
, rn
->p
.prefixlen
);
1576 if (zebra
->ribq
== NULL
)
1578 zlog_err ("%s: work_queue does not exist!", __func__
);
1583 * The RIB queue should normally be either empty or holding the only
1584 * work_queue_item element. In the latter case this element would
1585 * hold a pointer to the meta queue structure, which must be used to
1586 * actually queue the route nodes to process. So create the MQ
1587 * holder, if necessary, then push the work into it in any case.
1588 * This semantics was introduced after 0.99.9 release.
1590 if (!zebra
->ribq
->items
->count
)
1591 work_queue_add (zebra
->ribq
, zebra
->mq
);
1593 rib_meta_queue_add (zebra
->mq
, rn
);
1595 if (IS_ZEBRA_DEBUG_RIB_Q
)
1596 zlog_debug ("%s: %s/%d: rn %p queued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1601 /* Create new meta queue.
1602 A destructor function doesn't seem to be necessary here.
1604 static struct meta_queue
*
1605 meta_queue_new (void)
1607 struct meta_queue
*new;
1610 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
1613 for (i
= 0; i
< MQ_SIZE
; i
++)
1615 new->subq
[i
] = list_new ();
1616 assert(new->subq
[i
]);
1622 /* initialise zebra rib work queue */
1624 rib_queue_init (struct zebra_t
*zebra
)
1628 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1629 "route_node processing")))
1631 zlog_err ("%s: could not initialise work queue!", __func__
);
1635 /* fill in the work queue spec */
1636 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1637 zebra
->ribq
->spec
.errorfunc
= NULL
;
1638 /* XXX: TODO: These should be runtime configurable via vty */
1639 zebra
->ribq
->spec
.max_retries
= 3;
1640 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
1642 if (!(zebra
->mq
= meta_queue_new ()))
1644 zlog_err ("%s: could not initialise meta queue!", __func__
);
1650 /* RIB updates are processed via a queue of pointers to route_nodes.
1652 * The queue length is bounded by the maximal size of the routing table,
1653 * as a route_node will not be requeued, if already queued.
1655 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
1656 * state, or static_install_ipv{4,6} (when an existing RIB is updated)
1657 * and then submit route_node to queue for best-path selection later.
1658 * Order of add/delete state changes are preserved for any given RIB.
1660 * Deleted RIBs are reaped during best-path selection.
1663 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
1664 * |-------->| | best RIB, if required
1666 * static_install->|->rib_addqueue...... -> rib_process
1668 * |-------->| |-> rib_unlink
1669 * |-> set RIB_ENTRY_REMOVE |
1670 * rib_delnode (RIB freed)
1672 * The 'info' pointer of a route_node points to a rib_dest_t
1673 * ('dest'). Queueing state for a route_node is kept on the dest. The
1674 * dest is created on-demand by rib_link() and is kept around at least
1675 * as long as there are ribs hanging off it (@see rib_gc_dest()).
1677 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1679 * - route_nodes: refcounted by:
1680 * - dest attached to route_node:
1681 * - managed by: rib_link/rib_gc_dest
1682 * - route_node processing queue
1683 * - managed by: rib_addqueue, rib_process.
1687 /* Add RIB to head of the route node. */
1689 rib_link (struct route_node
*rn
, struct rib
*rib
)
1693 char buf
[INET6_ADDRSTRLEN
];
1697 if (IS_ZEBRA_DEBUG_RIB
)
1699 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1700 zlog_debug ("%s: %s/%d: rn %p, rib %p", __func__
,
1701 buf
, rn
->p
.prefixlen
, rn
, rib
);
1704 dest
= rib_dest_from_rnode (rn
);
1707 if (IS_ZEBRA_DEBUG_RIB
)
1709 zlog_debug ("%s: %s/%d: adding dest to table", __func__
,
1710 buf
, rn
->p
.prefixlen
);
1713 dest
= XCALLOC (MTYPE_RIB_DEST
, sizeof (rib_dest_t
));
1714 route_lock_node (rn
); /* rn route table reference */
1719 head
= dest
->routes
;
1726 rib_queue_add (&zebrad
, rn
);
1730 rib_addnode (struct route_node
*rn
, struct rib
*rib
)
1732 /* RIB node has been un-removed before route-node is processed.
1733 * route_node must hence already be on the queue for processing..
1735 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1737 if (IS_ZEBRA_DEBUG_RIB
)
1739 char buf
[INET6_ADDRSTRLEN
];
1740 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1741 zlog_debug ("%s: %s/%d: rn %p, un-removed rib %p",
1742 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1744 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1753 * Detach a rib structure from a route_node.
1755 * Note that a call to rib_unlink() should be followed by a call to
1756 * rib_gc_dest() at some point. This allows a rib_dest_t that is no
1757 * longer required to be deleted.
1760 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
1762 char buf
[INET6_ADDRSTRLEN
];
1767 if (IS_ZEBRA_DEBUG_RIB
)
1769 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1770 zlog_debug ("%s: %s/%d: rn %p, rib %p",
1771 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1774 dest
= rib_dest_from_rnode (rn
);
1777 rib
->next
->prev
= rib
->prev
;
1780 rib
->prev
->next
= rib
->next
;
1783 dest
->routes
= rib
->next
;
1786 /* free RIB and nexthops */
1787 nexthops_free(rib
->nexthop
);
1788 XFREE (MTYPE_RIB
, rib
);
1793 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
1795 if (IS_ZEBRA_DEBUG_RIB
)
1797 char buf
[INET6_ADDRSTRLEN
];
1798 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1799 zlog_debug ("%s: %s/%d: rn %p, rib %p, removing", __func__
,
1800 buf
, rn
->p
.prefixlen
, rn
, rib
);
1802 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1803 rib_queue_add (&zebrad
, rn
);
1807 rib_add_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1808 struct in_addr
*gate
, struct in_addr
*src
,
1809 unsigned int ifindex
, u_int32_t vrf_id
,
1810 u_int32_t metric
, u_char distance
, safi_t safi
)
1813 struct rib
*same
= NULL
;
1814 struct route_table
*table
;
1815 struct route_node
*rn
;
1816 struct nexthop
*nexthop
;
1819 table
= vrf_table (AFI_IP
, safi
, 0);
1823 /* Make it sure prefixlen is applied to the prefix. */
1824 apply_mask_ipv4 (p
);
1826 /* Set default distance by route type. */
1829 if ((unsigned)type
>= array_size(route_info
))
1832 distance
= route_info
[type
].distance
;
1834 /* iBGP distance is 200. */
1835 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1839 /* Lookup route node.*/
1840 rn
= route_node_get (table
, (struct prefix
*) p
);
1842 /* If same type of route are installed, treat it as a implicit
1844 RNODE_FOREACH_RIB (rn
, rib
)
1846 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1849 if (rib
->type
!= type
)
1851 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
1856 /* Duplicate connected route comes in. */
1857 else if ((nexthop
= rib
->nexthop
) &&
1858 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
1859 nexthop
->ifindex
== ifindex
&&
1860 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1867 /* Allocate new rib structure. */
1868 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1870 rib
->distance
= distance
;
1872 rib
->metric
= metric
;
1873 rib
->table
= vrf_id
;
1874 rib
->nexthop_num
= 0;
1875 rib
->uptime
= time (NULL
);
1877 /* Nexthop settings. */
1881 nexthop_ipv4_ifindex_add (rib
, gate
, src
, ifindex
);
1883 nexthop_ipv4_add (rib
, gate
, src
);
1886 nexthop_ifindex_add (rib
, ifindex
);
1888 /* If this route is kernel route, set FIB flag to the route. */
1889 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1890 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1891 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1893 /* Link new rib to node.*/
1894 if (IS_ZEBRA_DEBUG_RIB
)
1895 zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__
, rn
, rib
);
1896 rib_addnode (rn
, rib
);
1898 /* Free implicit route.*/
1901 if (IS_ZEBRA_DEBUG_RIB
)
1902 zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__
, rn
, rib
);
1903 rib_delnode (rn
, same
);
1906 route_unlock_node (rn
);
1910 /* This function dumps the contents of a given RIB entry into
1911 * standard debug log. Calling function name and IP prefix in
1912 * question are passed as 1st and 2nd arguments.
1915 void rib_dump (const char * func
, const struct prefix
* p
, const struct rib
* rib
)
1917 char straddr
[INET6_ADDRSTRLEN
];
1918 struct nexthop
*nexthop
, *tnexthop
;
1921 inet_ntop (p
->family
, &p
->u
.prefix
, straddr
, INET6_ADDRSTRLEN
);
1922 zlog_debug ("%s: dumping RIB entry %p for %s/%d", func
, rib
, straddr
, p
->prefixlen
);
1925 "%s: refcnt == %lu, uptime == %lu, type == %u, table == %d",
1928 (unsigned long) rib
->uptime
,
1934 "%s: metric == %u, distance == %u, flags == %u, status == %u",
1943 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
1946 rib
->nexthop_active_num
,
1947 rib
->nexthop_fib_num
1950 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
1952 inet_ntop (p
->family
, &nexthop
->gate
, straddr
, INET6_ADDRSTRLEN
);
1955 "%s: %s %s with flags %s%s%s",
1957 (recursing
? " NH" : "NH"),
1959 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
1960 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
1961 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
1964 zlog_debug ("%s: dump complete", func
);
1967 /* This is an exported helper to rtm_read() to dump the strange
1968 * RIB entry found by rib_lookup_ipv4_route()
1971 void rib_lookup_and_dump (struct prefix_ipv4
* p
)
1973 struct route_table
*table
;
1974 struct route_node
*rn
;
1976 char prefix_buf
[INET_ADDRSTRLEN
];
1979 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1982 zlog_err ("%s: vrf_table() returned NULL", __func__
);
1986 inet_ntop (AF_INET
, &p
->prefix
.s_addr
, prefix_buf
, INET_ADDRSTRLEN
);
1987 /* Scan the RIB table for exactly matching RIB entry. */
1988 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1990 /* No route for this prefix. */
1993 zlog_debug ("%s: lookup failed for %s/%d", __func__
, prefix_buf
, p
->prefixlen
);
1998 route_unlock_node (rn
);
2001 RNODE_FOREACH_RIB (rn
, rib
)
2005 "%s: rn %p, rib %p: %s, %s",
2009 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
2010 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
2012 rib_dump (__func__
, (struct prefix
*) p
, rib
);
2016 /* Check if requested address assignment will fail due to another
2017 * route being installed by zebra in FIB already. Take necessary
2018 * actions, if needed: remove such a route from FIB and deSELECT
2019 * corresponding RIB entry. Then put affected RN into RIBQ head.
2021 void rib_lookup_and_pushup (struct prefix_ipv4
* p
)
2023 struct route_table
*table
;
2024 struct route_node
*rn
;
2026 unsigned changed
= 0;
2028 if (NULL
== (table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0)))
2030 zlog_err ("%s: vrf_table() returned NULL", __func__
);
2034 /* No matches would be the simplest case. */
2035 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
2039 route_unlock_node (rn
);
2041 /* Check all RIB entries. In case any changes have to be done, requeue
2042 * the RN into RIBQ head. If the routing message about the new connected
2043 * route (generated by the IP address we are going to assign very soon)
2044 * comes before the RIBQ is processed, the new RIB entry will join
2045 * RIBQ record already on head. This is necessary for proper revalidation
2046 * of the rest of the RIB.
2048 RNODE_FOREACH_RIB (rn
, rib
)
2050 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) &&
2051 ! RIB_SYSTEM_ROUTE (rib
))
2054 if (IS_ZEBRA_DEBUG_RIB
)
2056 char buf
[INET_ADDRSTRLEN
];
2057 inet_ntop (rn
->p
.family
, &p
->prefix
, buf
, INET_ADDRSTRLEN
);
2058 zlog_debug ("%s: freeing way for connected prefix %s/%d", __func__
, buf
, p
->prefixlen
);
2059 rib_dump (__func__
, &rn
->p
, rib
);
2061 rib_uninstall (rn
, rib
);
2065 rib_queue_add (&zebrad
, rn
);
2069 rib_add_ipv4_multipath (struct prefix_ipv4
*p
, struct rib
*rib
, safi_t safi
)
2071 struct route_table
*table
;
2072 struct route_node
*rn
;
2074 struct nexthop
*nexthop
;
2077 table
= vrf_table (AFI_IP
, safi
, 0);
2081 /* Make it sure prefixlen is applied to the prefix. */
2082 apply_mask_ipv4 (p
);
2084 /* Set default distance by route type. */
2085 if (rib
->distance
== 0)
2087 rib
->distance
= route_info
[rib
->type
].distance
;
2089 /* iBGP distance is 200. */
2090 if (rib
->type
== ZEBRA_ROUTE_BGP
2091 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
2092 rib
->distance
= 200;
2095 /* Lookup route node.*/
2096 rn
= route_node_get (table
, (struct prefix
*) p
);
2098 /* If same type of route are installed, treat it as a implicit
2100 RNODE_FOREACH_RIB (rn
, same
)
2102 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
2105 if (same
->type
== rib
->type
&& same
->table
== rib
->table
2106 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
2110 /* If this route is kernel route, set FIB flag to the route. */
2111 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
2112 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2113 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2115 /* Link new rib to node.*/
2116 rib_addnode (rn
, rib
);
2117 if (IS_ZEBRA_DEBUG_RIB
)
2119 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
2121 rib_dump (__func__
, (struct prefix
*) p
, rib
);
2124 /* Free implicit route.*/
2127 if (IS_ZEBRA_DEBUG_RIB
)
2129 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
2130 __func__
, rn
, same
);
2131 rib_dump (__func__
, (struct prefix
*) p
, same
);
2133 rib_delnode (rn
, same
);
2136 route_unlock_node (rn
);
2140 /* XXX factor with rib_delete_ipv6 */
2142 rib_delete_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
2143 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
, safi_t safi
)
2145 struct route_table
*table
;
2146 struct route_node
*rn
;
2148 struct rib
*fib
= NULL
;
2149 struct rib
*same
= NULL
;
2150 struct nexthop
*nexthop
, *tnexthop
;
2152 char buf1
[INET_ADDRSTRLEN
];
2153 char buf2
[INET_ADDRSTRLEN
];
2156 table
= vrf_table (AFI_IP
, safi
, 0);
2161 apply_mask_ipv4 (p
);
2163 if (IS_ZEBRA_DEBUG_KERNEL
)
2166 zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
2167 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2172 zlog_debug ("rib_delete_ipv4(): route delete %s/%d ifindex %d",
2173 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2178 /* Lookup route node. */
2179 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2182 if (IS_ZEBRA_DEBUG_KERNEL
)
2185 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
2186 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2188 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
2191 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
2192 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2196 return ZEBRA_ERR_RTNOEXIST
;
2199 /* Lookup same type route. */
2200 RNODE_FOREACH_RIB (rn
, rib
)
2202 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2205 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2208 if (rib
->type
!= type
)
2210 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2211 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2213 if (nexthop
->ifindex
!= ifindex
)
2218 route_unlock_node (rn
);
2219 route_unlock_node (rn
);
2225 /* Make sure that the route found has the same gateway. */
2233 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2234 if (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
))
2243 /* If same type of route can't be found and this message is from
2247 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
2250 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2251 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2253 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
2257 if (IS_ZEBRA_DEBUG_KERNEL
)
2260 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
2261 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2263 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
2267 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
2268 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
2273 route_unlock_node (rn
);
2274 return ZEBRA_ERR_RTNOEXIST
;
2279 rib_delnode (rn
, same
);
2281 route_unlock_node (rn
);
2285 /* Install static route into rib. */
2287 static_install_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
2290 struct route_node
*rn
;
2291 struct route_table
*table
;
2294 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2298 /* Lookup existing route */
2299 rn
= route_node_get (table
, p
);
2300 RNODE_FOREACH_RIB (rn
, rib
)
2302 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2305 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2311 /* Same distance static route is there. Update it with new
2313 route_unlock_node (rn
);
2316 case STATIC_IPV4_GATEWAY
:
2317 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2319 case STATIC_IPV4_IFNAME
:
2320 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2322 case STATIC_IPV4_BLACKHOLE
:
2323 nexthop_blackhole_add (rib
);
2326 rib_queue_add (&zebrad
, rn
);
2330 /* This is new static route. */
2331 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2333 rib
->type
= ZEBRA_ROUTE_STATIC
;
2334 rib
->distance
= si
->distance
;
2336 rib
->table
= zebrad
.rtm_table_default
;
2337 rib
->nexthop_num
= 0;
2341 case STATIC_IPV4_GATEWAY
:
2342 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2344 case STATIC_IPV4_IFNAME
:
2345 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2347 case STATIC_IPV4_BLACKHOLE
:
2348 nexthop_blackhole_add (rib
);
2352 /* Save the flags of this static routes (reject, blackhole) */
2353 rib
->flags
= si
->flags
;
2355 /* Link this rib to the tree. */
2356 rib_addnode (rn
, rib
);
2361 static_ipv4_nexthop_same (struct nexthop
*nexthop
, struct static_ipv4
*si
)
2363 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
2364 && si
->type
== STATIC_IPV4_GATEWAY
2365 && IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &si
->gate
.ipv4
))
2367 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2368 && si
->type
== STATIC_IPV4_IFNAME
2369 && strcmp (nexthop
->ifname
, si
->gate
.ifname
) == 0)
2371 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
2372 && si
->type
== STATIC_IPV4_BLACKHOLE
)
2377 /* Uninstall static route from RIB. */
2379 static_uninstall_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
2381 struct route_node
*rn
;
2383 struct nexthop
*nexthop
;
2384 struct route_table
*table
;
2387 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2391 /* Lookup existing route with type and distance. */
2392 rn
= route_node_lookup (table
, p
);
2396 RNODE_FOREACH_RIB (rn
, rib
)
2398 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2401 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2407 route_unlock_node (rn
);
2411 /* Lookup nexthop. */
2412 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2413 if (static_ipv4_nexthop_same (nexthop
, si
))
2416 /* Can't find nexthop. */
2419 route_unlock_node (rn
);
2423 /* Check nexthop. */
2424 if (rib
->nexthop_num
== 1)
2425 rib_delnode (rn
, rib
);
2428 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2429 rib_uninstall (rn
, rib
);
2430 nexthop_delete (rib
, nexthop
);
2431 nexthop_free (nexthop
);
2432 rib_queue_add (&zebrad
, rn
);
2435 route_unlock_node (rn
);
2438 /* Add static route into static route configuration. */
2440 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2441 u_char flags
, u_char distance
, u_int32_t vrf_id
)
2444 struct route_node
*rn
;
2445 struct static_ipv4
*si
;
2446 struct static_ipv4
*pp
;
2447 struct static_ipv4
*cp
;
2448 struct static_ipv4
*update
= NULL
;
2449 struct route_table
*stable
;
2452 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2456 /* Lookup static route prefix. */
2457 rn
= route_node_get (stable
, p
);
2461 type
= STATIC_IPV4_GATEWAY
;
2463 type
= STATIC_IPV4_IFNAME
;
2465 type
= STATIC_IPV4_BLACKHOLE
;
2467 /* Do nothing if there is a same static route. */
2468 for (si
= rn
->info
; si
; si
= si
->next
)
2470 if (type
== si
->type
2471 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2472 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2474 if (distance
== si
->distance
)
2476 route_unlock_node (rn
);
2484 /* Distance changed. */
2486 static_delete_ipv4 (p
, gate
, ifname
, update
->distance
, vrf_id
);
2488 /* Make new static route structure. */
2489 si
= XCALLOC (MTYPE_STATIC_IPV4
, sizeof (struct static_ipv4
));
2492 si
->distance
= distance
;
2496 si
->gate
.ipv4
= *gate
;
2498 si
->gate
.ifname
= XSTRDUP (0, ifname
);
2500 /* Add new static route information to the tree with sort by
2501 distance value and gateway address. */
2502 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2504 if (si
->distance
< cp
->distance
)
2506 if (si
->distance
> cp
->distance
)
2508 if (si
->type
== STATIC_IPV4_GATEWAY
&& cp
->type
== STATIC_IPV4_GATEWAY
)
2510 if (ntohl (si
->gate
.ipv4
.s_addr
) < ntohl (cp
->gate
.ipv4
.s_addr
))
2512 if (ntohl (si
->gate
.ipv4
.s_addr
) > ntohl (cp
->gate
.ipv4
.s_addr
))
2517 /* Make linked list. */
2527 /* Install into rib. */
2528 static_install_ipv4 (p
, si
);
2533 /* Delete static route from static route configuration. */
2535 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2536 u_char distance
, u_int32_t vrf_id
)
2539 struct route_node
*rn
;
2540 struct static_ipv4
*si
;
2541 struct route_table
*stable
;
2544 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2548 /* Lookup static route prefix. */
2549 rn
= route_node_lookup (stable
, p
);
2555 type
= STATIC_IPV4_GATEWAY
;
2557 type
= STATIC_IPV4_IFNAME
;
2559 type
= STATIC_IPV4_BLACKHOLE
;
2561 /* Find same static route is the tree */
2562 for (si
= rn
->info
; si
; si
= si
->next
)
2563 if (type
== si
->type
2564 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2565 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2568 /* Can't find static route. */
2571 route_unlock_node (rn
);
2575 /* Install into rib. */
2576 static_uninstall_ipv4 (p
, si
);
2578 /* Unlink static route from linked list. */
2580 si
->prev
->next
= si
->next
;
2582 rn
->info
= si
->next
;
2584 si
->next
->prev
= si
->prev
;
2585 route_unlock_node (rn
);
2587 /* Free static route configuration. */
2589 XFREE (0, si
->gate
.ifname
);
2590 XFREE (MTYPE_STATIC_IPV4
, si
);
2592 route_unlock_node (rn
);
2600 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
2601 struct in6_addr
*gate
, unsigned int ifindex
, int table
)
2603 if (type
== ZEBRA_ROUTE_CONNECT
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)) {
2604 #if defined (MUSICA) || defined (LINUX)
2605 /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
2606 if (p
->prefixlen
== 96)
2611 if (type
== ZEBRA_ROUTE_KERNEL
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)
2612 && p
->prefixlen
== 96 && gate
&& IN6_IS_ADDR_UNSPECIFIED (gate
))
2614 kernel_delete_ipv6_old (p
, gate
, ifindex
, 0, table
);
2621 rib_add_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2622 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
,
2623 u_int32_t metric
, u_char distance
, safi_t safi
)
2626 struct rib
*same
= NULL
;
2627 struct route_table
*table
;
2628 struct route_node
*rn
;
2629 struct nexthop
*nexthop
;
2632 table
= vrf_table (AFI_IP6
, safi
, 0);
2636 /* Make sure mask is applied. */
2637 apply_mask_ipv6 (p
);
2639 /* Set default distance by route type. */
2641 distance
= route_info
[type
].distance
;
2643 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2646 /* Filter bogus route. */
2647 if (rib_bogus_ipv6 (type
, p
, gate
, ifindex
, 0))
2650 /* Lookup route node.*/
2651 rn
= route_node_get (table
, (struct prefix
*) p
);
2653 /* If same type of route are installed, treat it as a implicit
2655 RNODE_FOREACH_RIB (rn
, rib
)
2657 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2660 if (rib
->type
!= type
)
2662 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2667 else if ((nexthop
= rib
->nexthop
) &&
2668 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2669 nexthop
->ifindex
== ifindex
)
2676 /* Allocate new rib structure. */
2677 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2680 rib
->distance
= distance
;
2682 rib
->metric
= metric
;
2683 rib
->table
= vrf_id
;
2684 rib
->nexthop_num
= 0;
2685 rib
->uptime
= time (NULL
);
2687 /* Nexthop settings. */
2691 nexthop_ipv6_ifindex_add (rib
, gate
, ifindex
);
2693 nexthop_ipv6_add (rib
, gate
);
2696 nexthop_ifindex_add (rib
, ifindex
);
2698 /* If this route is kernel route, set FIB flag to the route. */
2699 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2700 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2701 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2703 /* Link new rib to node.*/
2704 rib_addnode (rn
, rib
);
2705 if (IS_ZEBRA_DEBUG_RIB
)
2707 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
2709 rib_dump (__func__
, (struct prefix
*) p
, rib
);
2712 /* Free implicit route.*/
2715 if (IS_ZEBRA_DEBUG_RIB
)
2717 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
2718 __func__
, rn
, same
);
2719 rib_dump (__func__
, (struct prefix
*) p
, same
);
2721 rib_delnode (rn
, same
);
2724 route_unlock_node (rn
);
2728 /* XXX factor with rib_delete_ipv6 */
2730 rib_delete_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2731 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
, safi_t safi
)
2733 struct route_table
*table
;
2734 struct route_node
*rn
;
2736 struct rib
*fib
= NULL
;
2737 struct rib
*same
= NULL
;
2738 struct nexthop
*nexthop
, *tnexthop
;
2740 char buf1
[INET6_ADDRSTRLEN
];
2741 char buf2
[INET6_ADDRSTRLEN
];
2744 apply_mask_ipv6 (p
);
2747 table
= vrf_table (AFI_IP6
, safi
, 0);
2751 /* Lookup route node. */
2752 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2755 if (IS_ZEBRA_DEBUG_KERNEL
)
2758 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
2759 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2761 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
2764 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
2765 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2769 return ZEBRA_ERR_RTNOEXIST
;
2772 /* Lookup same type route. */
2773 RNODE_FOREACH_RIB (rn
, rib
)
2775 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2778 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2781 if (rib
->type
!= type
)
2783 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2784 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2786 if (nexthop
->ifindex
!= ifindex
)
2791 route_unlock_node (rn
);
2792 route_unlock_node (rn
);
2798 /* Make sure that the route found has the same gateway. */
2806 for (ALL_NEXTHOPS_RO(rib
->nexthop
, nexthop
, tnexthop
, recursing
))
2807 if (IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
))
2817 /* If same type of route can't be found and this message is from
2821 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
2824 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2825 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2827 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
2831 if (IS_ZEBRA_DEBUG_KERNEL
)
2834 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
2835 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2837 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
2841 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
2842 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2847 route_unlock_node (rn
);
2848 return ZEBRA_ERR_RTNOEXIST
;
2853 rib_delnode (rn
, same
);
2855 route_unlock_node (rn
);
2859 /* Install static route into rib. */
2861 static_install_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2864 struct route_table
*table
;
2865 struct route_node
*rn
;
2868 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2872 /* Lookup existing route */
2873 rn
= route_node_get (table
, p
);
2874 RNODE_FOREACH_RIB (rn
, rib
)
2876 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2879 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2885 /* Same distance static route is there. Update it with new
2887 route_unlock_node (rn
);
2891 case STATIC_IPV6_GATEWAY
:
2892 nexthop_ipv6_add (rib
, &si
->ipv6
);
2894 case STATIC_IPV6_IFNAME
:
2895 nexthop_ifname_add (rib
, si
->ifname
);
2897 case STATIC_IPV6_GATEWAY_IFNAME
:
2898 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2901 rib_queue_add (&zebrad
, rn
);
2905 /* This is new static route. */
2906 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2908 rib
->type
= ZEBRA_ROUTE_STATIC
;
2909 rib
->distance
= si
->distance
;
2911 rib
->nexthop_num
= 0;
2915 case STATIC_IPV6_GATEWAY
:
2916 nexthop_ipv6_add (rib
, &si
->ipv6
);
2918 case STATIC_IPV6_IFNAME
:
2919 nexthop_ifname_add (rib
, si
->ifname
);
2921 case STATIC_IPV6_GATEWAY_IFNAME
:
2922 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2926 /* Save the flags of this static routes (reject, blackhole) */
2927 rib
->flags
= si
->flags
;
2929 /* Link this rib to the tree. */
2930 rib_addnode (rn
, rib
);
2935 static_ipv6_nexthop_same (struct nexthop
*nexthop
, struct static_ipv6
*si
)
2937 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
2938 && si
->type
== STATIC_IPV6_GATEWAY
2939 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
))
2941 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2942 && si
->type
== STATIC_IPV6_IFNAME
2943 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2945 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
2946 && si
->type
== STATIC_IPV6_GATEWAY_IFNAME
2947 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
)
2948 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2954 static_uninstall_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2956 struct route_table
*table
;
2957 struct route_node
*rn
;
2959 struct nexthop
*nexthop
;
2962 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2966 /* Lookup existing route with type and distance. */
2967 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2971 RNODE_FOREACH_RIB (rn
, rib
)
2973 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2976 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2982 route_unlock_node (rn
);
2986 /* Lookup nexthop. */
2987 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2988 if (static_ipv6_nexthop_same (nexthop
, si
))
2991 /* Can't find nexthop. */
2994 route_unlock_node (rn
);
2998 /* Check nexthop. */
2999 if (rib
->nexthop_num
== 1)
3001 rib_delnode (rn
, rib
);
3005 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
3006 rib_uninstall (rn
, rib
);
3007 nexthop_delete (rib
, nexthop
);
3008 nexthop_free (nexthop
);
3009 rib_queue_add (&zebrad
, rn
);
3012 route_unlock_node (rn
);
3015 /* Add static route into static route configuration. */
3017 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
3018 const char *ifname
, u_char flags
, u_char distance
,
3021 struct route_node
*rn
;
3022 struct static_ipv6
*si
;
3023 struct static_ipv6
*pp
;
3024 struct static_ipv6
*cp
;
3025 struct route_table
*stable
;
3028 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3033 (type
== STATIC_IPV6_GATEWAY
|| type
== STATIC_IPV6_GATEWAY_IFNAME
))
3037 (type
== STATIC_IPV6_GATEWAY_IFNAME
|| type
== STATIC_IPV6_IFNAME
))
3040 /* Lookup static route prefix. */
3041 rn
= route_node_get (stable
, p
);
3043 /* Do nothing if there is a same static route. */
3044 for (si
= rn
->info
; si
; si
= si
->next
)
3046 if (distance
== si
->distance
3048 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
3049 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
3051 route_unlock_node (rn
);
3056 /* Make new static route structure. */
3057 si
= XCALLOC (MTYPE_STATIC_IPV6
, sizeof (struct static_ipv6
));
3060 si
->distance
= distance
;
3065 case STATIC_IPV6_GATEWAY
:
3068 case STATIC_IPV6_IFNAME
:
3069 si
->ifname
= XSTRDUP (0, ifname
);
3071 case STATIC_IPV6_GATEWAY_IFNAME
:
3073 si
->ifname
= XSTRDUP (0, ifname
);
3077 /* Add new static route information to the tree with sort by
3078 distance value and gateway address. */
3079 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
3081 if (si
->distance
< cp
->distance
)
3083 if (si
->distance
> cp
->distance
)
3087 /* Make linked list. */
3097 /* Install into rib. */
3098 static_install_ipv6 (p
, si
);
3103 /* Delete static route from static route configuration. */
3105 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
3106 const char *ifname
, u_char distance
, u_int32_t vrf_id
)
3108 struct route_node
*rn
;
3109 struct static_ipv6
*si
;
3110 struct route_table
*stable
;
3113 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
3117 /* Lookup static route prefix. */
3118 rn
= route_node_lookup (stable
, p
);
3122 /* Find same static route is the tree */
3123 for (si
= rn
->info
; si
; si
= si
->next
)
3124 if (distance
== si
->distance
3126 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
3127 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
3130 /* Can't find static route. */
3133 route_unlock_node (rn
);
3137 /* Install into rib. */
3138 static_uninstall_ipv6 (p
, si
);
3140 /* Unlink static route from linked list. */
3142 si
->prev
->next
= si
->next
;
3144 rn
->info
= si
->next
;
3146 si
->next
->prev
= si
->prev
;
3148 /* Free static route configuration. */
3150 XFREE (0, si
->ifname
);
3151 XFREE (MTYPE_STATIC_IPV6
, si
);
3155 #endif /* HAVE_IPV6 */
3157 /* RIB update function. */
3161 struct route_node
*rn
;
3162 struct route_table
*table
;
3164 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
3166 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3167 if (rnode_to_ribs (rn
))
3168 rib_queue_add (&zebrad
, rn
);
3170 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
3172 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3173 if (rnode_to_ribs (rn
))
3174 rib_queue_add (&zebrad
, rn
);
3178 /* Remove all routes which comes from non main table. */
3180 rib_weed_table (struct route_table
*table
)
3182 struct route_node
*rn
;
3187 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3188 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3190 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3193 if (rib
->table
!= zebrad
.rtm_table_default
&&
3194 rib
->table
!= RT_TABLE_MAIN
)
3195 rib_delnode (rn
, rib
);
3199 /* Delete all routes from non main table. */
3201 rib_weed_tables (void)
3203 rib_weed_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
3204 rib_weed_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3207 /* Delete self installed routes after zebra is relaunched. */
3209 rib_sweep_table (struct route_table
*table
)
3211 struct route_node
*rn
;
3217 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3218 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3220 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3223 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
3224 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
3226 ret
= rib_uninstall_kernel (rn
, rib
);
3228 rib_delnode (rn
, rib
);
3233 /* Sweep all RIB tables. */
3235 rib_sweep_route (void)
3237 rib_sweep_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
3238 rib_sweep_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3241 /* Remove specific by protocol routes from 'table'. */
3242 static unsigned long
3243 rib_score_proto_table (u_char proto
, struct route_table
*table
)
3245 struct route_node
*rn
;
3248 unsigned long n
= 0;
3251 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3252 RNODE_FOREACH_RIB_SAFE (rn
, rib
, next
)
3254 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
3256 if (rib
->type
== proto
)
3258 rib_delnode (rn
, rib
);
3266 /* Remove specific by protocol routes. */
3268 rib_score_proto (u_char proto
)
3270 return rib_score_proto_table (proto
, vrf_table (AFI_IP
, SAFI_UNICAST
, 0))
3271 +rib_score_proto_table (proto
, vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3274 /* Close RIB and clean up kernel routes. */
3276 rib_close_table (struct route_table
*table
)
3278 struct route_node
*rn
;
3282 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3283 RNODE_FOREACH_RIB (rn
, rib
)
3285 if (!CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
3288 zfpm_trigger_update (rn
, NULL
);
3290 if (! RIB_SYSTEM_ROUTE (rib
))
3291 rib_uninstall_kernel (rn
, rib
);
3295 /* Close all RIB tables. */
3299 rib_close_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
3300 rib_close_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3303 /* Routing information base initialize. */
3307 rib_queue_init (&zebrad
);
3308 /* VRF initialization. */
3315 * Get the first vrf id that is greater than the given vrf id if any.
3317 * Returns TRUE if a vrf id was found, FALSE otherwise.
3320 vrf_id_get_next (uint32_t id
, uint32_t *next_id_p
)
3322 while (++id
< vector_active (vrf_vector
))
3324 if (vrf_lookup (id
))
3335 * rib_tables_iter_next
3337 * Returns the next table in the iteration.
3339 struct route_table
*
3340 rib_tables_iter_next (rib_tables_iter_t
*iter
)
3342 struct route_table
*table
;
3345 * Array that helps us go over all AFI/SAFI combinations via one
3352 { AFI_IP
, SAFI_UNICAST
},
3353 { AFI_IP
, SAFI_MULTICAST
},
3354 { AFI_IP6
, SAFI_UNICAST
},
3355 { AFI_IP6
, SAFI_MULTICAST
},
3360 switch (iter
->state
)
3363 case RIB_TABLES_ITER_S_INIT
:
3365 iter
->afi_safi_ix
= -1;
3369 case RIB_TABLES_ITER_S_ITERATING
:
3370 iter
->afi_safi_ix
++;
3374 while (iter
->afi_safi_ix
< (int) ZEBRA_NUM_OF (afi_safis
))
3376 table
= vrf_table (afi_safis
[iter
->afi_safi_ix
].afi
,
3377 afi_safis
[iter
->afi_safi_ix
].safi
,
3382 iter
->afi_safi_ix
++;
3386 * Found another table in this vrf.
3392 * Done with all tables in the current vrf, go to the next
3395 if (!vrf_id_get_next (iter
->vrf_id
, &iter
->vrf_id
))
3398 iter
->afi_safi_ix
= 0;
3403 case RIB_TABLES_ITER_S_DONE
:
3408 iter
->state
= RIB_TABLES_ITER_S_ITERATING
;
3410 iter
->state
= RIB_TABLES_ITER_S_DONE
;