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. */
60 {ZEBRA_ROUTE_SYSTEM
, 0},
61 {ZEBRA_ROUTE_KERNEL
, 0},
62 {ZEBRA_ROUTE_CONNECT
, 0},
63 {ZEBRA_ROUTE_STATIC
, 1},
64 {ZEBRA_ROUTE_RIP
, 120},
65 {ZEBRA_ROUTE_RIPNG
, 120},
66 {ZEBRA_ROUTE_OSPF
, 110},
67 {ZEBRA_ROUTE_OSPF6
, 110},
68 {ZEBRA_ROUTE_ISIS
, 115},
69 {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */}
72 /* Vector for routing table. */
73 static vector vrf_vector
;
75 /* Allocate new VRF. */
77 vrf_alloc (const char *name
)
81 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
85 vrf
->name
= XSTRDUP (MTYPE_VRF_NAME
, name
);
87 /* Allocate routing table and static table. */
88 vrf
->table
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
89 vrf
->table
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
90 vrf
->stable
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
91 vrf
->stable
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
96 /* Lookup VRF by identifier. */
98 vrf_lookup (u_int32_t id
)
100 return vector_lookup (vrf_vector
, id
);
103 /* Initialize VRF. */
107 struct vrf
*default_table
;
109 /* Allocate VRF vector. */
110 vrf_vector
= vector_init (1);
112 /* Allocate default main table. */
113 default_table
= vrf_alloc ("Default-IP-Routing-Table");
115 /* Default table index must be 0. */
116 vector_set_index (vrf_vector
, 0, default_table
);
119 /* Lookup route table. */
121 vrf_table (afi_t afi
, safi_t safi
, u_int32_t id
)
125 vrf
= vrf_lookup (id
);
129 return vrf
->table
[afi
][safi
];
132 /* Lookup static route table. */
134 vrf_static_table (afi_t afi
, safi_t safi
, u_int32_t id
)
138 vrf
= vrf_lookup (id
);
142 return vrf
->stable
[afi
][safi
];
145 /* Add nexthop to the end of the list. */
147 nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
149 struct nexthop
*last
;
151 for (last
= rib
->nexthop
; last
&& last
->next
; last
= last
->next
)
154 last
->next
= nexthop
;
156 rib
->nexthop
= nexthop
;
157 nexthop
->prev
= last
;
162 /* Delete specified nexthop from the list. */
164 nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
167 nexthop
->next
->prev
= nexthop
->prev
;
169 nexthop
->prev
->next
= nexthop
->next
;
171 rib
->nexthop
= nexthop
->next
;
177 nexthop_free (struct nexthop
*nexthop
)
180 XFREE (0, nexthop
->ifname
);
181 XFREE (MTYPE_NEXTHOP
, nexthop
);
185 nexthop_ifindex_add (struct rib
*rib
, unsigned int ifindex
)
187 struct nexthop
*nexthop
;
189 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
190 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
191 nexthop
->ifindex
= ifindex
;
193 nexthop_add (rib
, nexthop
);
199 nexthop_ifname_add (struct rib
*rib
, char *ifname
)
201 struct nexthop
*nexthop
;
203 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
204 nexthop
->type
= NEXTHOP_TYPE_IFNAME
;
205 nexthop
->ifname
= XSTRDUP (0, ifname
);
207 nexthop_add (rib
, nexthop
);
213 nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
215 struct nexthop
*nexthop
;
217 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
218 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
219 nexthop
->gate
.ipv4
= *ipv4
;
221 nexthop
->src
.ipv4
= *src
;
223 nexthop_add (rib
, nexthop
);
228 static struct nexthop
*
229 nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
230 struct in_addr
*src
, unsigned int ifindex
)
232 struct nexthop
*nexthop
;
234 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
235 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
236 nexthop
->gate
.ipv4
= *ipv4
;
238 nexthop
->src
.ipv4
= *src
;
239 nexthop
->ifindex
= ifindex
;
241 nexthop_add (rib
, nexthop
);
248 nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
250 struct nexthop
*nexthop
;
252 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
253 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
254 nexthop
->gate
.ipv6
= *ipv6
;
256 nexthop_add (rib
, nexthop
);
261 static struct nexthop
*
262 nexthop_ipv6_ifname_add (struct rib
*rib
, struct in6_addr
*ipv6
,
265 struct nexthop
*nexthop
;
267 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
268 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFNAME
;
269 nexthop
->gate
.ipv6
= *ipv6
;
270 nexthop
->ifname
= XSTRDUP (0, ifname
);
272 nexthop_add (rib
, nexthop
);
277 static struct nexthop
*
278 nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
279 unsigned int ifindex
)
281 struct nexthop
*nexthop
;
283 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
284 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
285 nexthop
->gate
.ipv6
= *ipv6
;
286 nexthop
->ifindex
= ifindex
;
288 nexthop_add (rib
, nexthop
);
292 #endif /* HAVE_IPV6 */
295 nexthop_blackhole_add (struct rib
*rib
)
297 struct nexthop
*nexthop
;
299 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
300 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
301 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
303 nexthop_add (rib
, nexthop
);
308 /* If force flag is not set, do not modify falgs at all for uninstall
309 the route from FIB. */
311 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
312 struct route_node
*top
)
314 struct prefix_ipv4 p
;
315 struct route_table
*table
;
316 struct route_node
*rn
;
318 struct nexthop
*newhop
;
320 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
321 nexthop
->ifindex
= 0;
324 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
326 /* Make lookup prefix. */
327 memset (&p
, 0, sizeof (struct prefix_ipv4
));
329 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
330 p
.prefix
= nexthop
->gate
.ipv4
;
333 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
337 rn
= route_node_match (table
, (struct prefix
*) &p
);
340 route_unlock_node (rn
);
342 /* If lookup self prefix return immediately. */
346 /* Pick up selected route. */
347 for (match
= rn
->info
; match
; match
= match
->next
)
349 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
351 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
355 /* If there is no selected route or matched route is EGP, go up
358 || match
->type
== ZEBRA_ROUTE_BGP
)
362 } while (rn
&& rn
->info
== NULL
);
364 route_lock_node (rn
);
368 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
370 /* Directly point connected route. */
371 newhop
= match
->nexthop
;
372 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
373 nexthop
->ifindex
= newhop
->ifindex
;
377 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
379 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
380 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
381 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
385 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
386 nexthop
->rtype
= newhop
->type
;
387 if (newhop
->type
== NEXTHOP_TYPE_IPV4
||
388 newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
389 nexthop
->rgate
.ipv4
= newhop
->gate
.ipv4
;
390 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
391 || newhop
->type
== NEXTHOP_TYPE_IFNAME
392 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
393 nexthop
->rifindex
= newhop
->ifindex
;
409 /* If force flag is not set, do not modify falgs at all for uninstall
410 the route from FIB. */
412 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
413 struct route_node
*top
)
415 struct prefix_ipv6 p
;
416 struct route_table
*table
;
417 struct route_node
*rn
;
419 struct nexthop
*newhop
;
421 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
422 nexthop
->ifindex
= 0;
425 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
427 /* Make lookup prefix. */
428 memset (&p
, 0, sizeof (struct prefix_ipv6
));
430 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
431 p
.prefix
= nexthop
->gate
.ipv6
;
434 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
438 rn
= route_node_match (table
, (struct prefix
*) &p
);
441 route_unlock_node (rn
);
443 /* If lookup self prefix return immediately. */
447 /* Pick up selected route. */
448 for (match
= rn
->info
; match
; match
= match
->next
)
450 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
452 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
456 /* If there is no selected route or matched route is EGP, go up
459 || match
->type
== ZEBRA_ROUTE_BGP
)
463 } while (rn
&& rn
->info
== NULL
);
465 route_lock_node (rn
);
469 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
471 /* Directly point connected route. */
472 newhop
= match
->nexthop
;
474 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
475 nexthop
->ifindex
= newhop
->ifindex
;
479 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
481 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
482 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
483 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
487 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
488 nexthop
->rtype
= newhop
->type
;
489 if (newhop
->type
== NEXTHOP_TYPE_IPV6
490 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
491 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
492 nexthop
->rgate
.ipv6
= newhop
->gate
.ipv6
;
493 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
494 || newhop
->type
== NEXTHOP_TYPE_IFNAME
495 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
496 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
497 nexthop
->rifindex
= newhop
->ifindex
;
511 #endif /* HAVE_IPV6 */
514 rib_match_ipv4 (struct in_addr addr
)
516 struct prefix_ipv4 p
;
517 struct route_table
*table
;
518 struct route_node
*rn
;
520 struct nexthop
*newhop
;
523 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
527 memset (&p
, 0, sizeof (struct prefix_ipv4
));
529 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
532 rn
= route_node_match (table
, (struct prefix
*) &p
);
536 route_unlock_node (rn
);
538 /* Pick up selected route. */
539 for (match
= rn
->info
; match
; match
= match
->next
)
541 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
543 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
547 /* If there is no selected route or matched route is EGP, go up
550 || match
->type
== ZEBRA_ROUTE_BGP
)
554 } while (rn
&& rn
->info
== NULL
);
556 route_lock_node (rn
);
560 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
561 /* Directly point connected route. */
565 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
566 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
576 rib_lookup_ipv4 (struct prefix_ipv4
*p
)
578 struct route_table
*table
;
579 struct route_node
*rn
;
581 struct nexthop
*nexthop
;
584 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
588 rn
= route_node_lookup (table
, (struct prefix
*) p
);
590 /* No route for this prefix. */
595 route_unlock_node (rn
);
597 for (match
= rn
->info
; match
; match
= match
->next
)
599 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
601 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
605 if (! match
|| match
->type
== ZEBRA_ROUTE_BGP
)
608 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
611 for (nexthop
= match
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
612 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
619 * This clone function, unlike its original rib_lookup_ipv4(), checks
620 * if specified IPv4 route record (prefix/mask -> gate) exists in
621 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
625 * 0: exact match found
626 * 1: a match was found with a different gate
627 * 2: connected route found
628 * 3: no matches found
631 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
)
633 struct route_table
*table
;
634 struct route_node
*rn
;
636 struct nexthop
*nexthop
;
639 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
641 return ZEBRA_RIB_LOOKUP_ERROR
;
643 /* Scan the RIB table for exactly matching RIB entry. */
644 rn
= route_node_lookup (table
, (struct prefix
*) p
);
646 /* No route for this prefix. */
648 return ZEBRA_RIB_NOTFOUND
;
651 route_unlock_node (rn
);
653 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
654 for (match
= rn
->info
; match
; match
= match
->next
)
656 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
658 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
662 /* None such found :( */
664 return ZEBRA_RIB_NOTFOUND
;
666 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
667 return ZEBRA_RIB_FOUND_CONNECTED
;
669 /* Ok, we have a cood candidate, let's check it's nexthop list... */
670 for (nexthop
= match
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
671 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
673 /* We are happy with either direct or recursive hexthop */
674 if (nexthop
->gate
.ipv4
.s_addr
== qgate
->sin
.sin_addr
.s_addr
||
675 nexthop
->rgate
.ipv4
.s_addr
== qgate
->sin
.sin_addr
.s_addr
)
676 return ZEBRA_RIB_FOUND_EXACT
;
679 if (IS_ZEBRA_DEBUG_RIB
)
681 char gate_buf
[INET_ADDRSTRLEN
], rgate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
682 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
683 inet_ntop (AF_INET
, &nexthop
->rgate
.ipv4
.s_addr
, rgate_buf
, INET_ADDRSTRLEN
);
684 inet_ntop (AF_INET
, &qgate
->sin
.sin_addr
.s_addr
, qgate_buf
, INET_ADDRSTRLEN
);
685 zlog_debug ("%s: qgate == %s, gate == %s, rgate == %s", __func__
, qgate_buf
, gate_buf
, rgate_buf
);
687 return ZEBRA_RIB_FOUND_NOGATE
;
691 return ZEBRA_RIB_NOTFOUND
;
696 rib_match_ipv6 (struct in6_addr
*addr
)
698 struct prefix_ipv6 p
;
699 struct route_table
*table
;
700 struct route_node
*rn
;
702 struct nexthop
*newhop
;
705 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
709 memset (&p
, 0, sizeof (struct prefix_ipv6
));
711 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
712 IPV6_ADDR_COPY (&p
.prefix
, addr
);
714 rn
= route_node_match (table
, (struct prefix
*) &p
);
718 route_unlock_node (rn
);
720 /* Pick up selected route. */
721 for (match
= rn
->info
; match
; match
= match
->next
)
723 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
725 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
729 /* If there is no selected route or matched route is EGP, go up
732 || match
->type
== ZEBRA_ROUTE_BGP
)
736 } while (rn
&& rn
->info
== NULL
);
738 route_lock_node (rn
);
742 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
743 /* Directly point connected route. */
747 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
748 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
756 #endif /* HAVE_IPV6 */
758 #define RIB_SYSTEM_ROUTE(R) \
759 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
761 /* This function verifies reachability of one given nexthop, which can be
762 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
763 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
764 * nexthop->ifindex will be updated appropriately as well.
765 * An existing route map can turn (otherwise active) nexthop into inactive, but
768 * The return value is the final value of 'ACTIVE' flag.
772 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
773 struct nexthop
*nexthop
, int set
)
775 struct interface
*ifp
;
776 route_map_result_t ret
= RMAP_MATCH
;
777 extern char *proto_rm
[AFI_MAX
][ZEBRA_ROUTE_MAX
+1];
778 struct route_map
*rmap
;
782 switch (nexthop
->type
)
784 case NEXTHOP_TYPE_IFINDEX
:
785 ifp
= if_lookup_by_index (nexthop
->ifindex
);
786 if (ifp
&& if_is_operative(ifp
))
787 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
789 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
791 case NEXTHOP_TYPE_IPV6_IFNAME
:
793 case NEXTHOP_TYPE_IFNAME
:
794 ifp
= if_lookup_by_name (nexthop
->ifname
);
795 if (ifp
&& if_is_operative(ifp
))
798 nexthop
->ifindex
= ifp
->ifindex
;
799 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
804 nexthop
->ifindex
= 0;
805 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
808 case NEXTHOP_TYPE_IPV4
:
809 case NEXTHOP_TYPE_IPV4_IFINDEX
:
811 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
812 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
814 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
817 case NEXTHOP_TYPE_IPV6
:
819 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
820 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
822 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
824 case NEXTHOP_TYPE_IPV6_IFINDEX
:
826 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
828 ifp
= if_lookup_by_index (nexthop
->ifindex
);
829 if (ifp
&& if_is_operative(ifp
))
830 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
832 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
836 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
837 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
839 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
842 #endif /* HAVE_IPV6 */
843 case NEXTHOP_TYPE_BLACKHOLE
:
844 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
849 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
852 if (RIB_SYSTEM_ROUTE(rib
) ||
853 (family
== AFI_IP
&& rn
->p
.family
!= AF_INET
) ||
854 (family
== AFI_IP6
&& rn
->p
.family
!= AF_INET6
))
855 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
858 if (rib
->type
>= 0 && rib
->type
< ZEBRA_ROUTE_MAX
&&
859 proto_rm
[family
][rib
->type
])
860 rmap
= route_map_lookup_by_name (proto_rm
[family
][rib
->type
]);
861 if (!rmap
&& proto_rm
[family
][ZEBRA_ROUTE_MAX
])
862 rmap
= route_map_lookup_by_name (proto_rm
[family
][ZEBRA_ROUTE_MAX
]);
864 ret
= route_map_apply(rmap
, &rn
->p
, RMAP_ZEBRA
, nexthop
);
867 if (ret
== RMAP_DENYMATCH
)
868 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
869 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
872 /* Iterate over all nexthops of the given RIB entry and refresh their
873 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
874 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
875 * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
876 * transparently passed to nexthop_active_check().
878 * Return value is the new number of active nexthops.
882 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
884 struct nexthop
*nexthop
;
885 unsigned int prev_active
, prev_index
, new_active
;
887 rib
->nexthop_active_num
= 0;
888 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
890 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
892 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
893 prev_index
= nexthop
->ifindex
;
894 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
895 rib
->nexthop_active_num
++;
896 if (prev_active
!= new_active
||
897 prev_index
!= nexthop
->ifindex
)
898 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
900 return rib
->nexthop_active_num
;
906 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
)
909 struct nexthop
*nexthop
;
911 switch (PREFIX_FAMILY (&rn
->p
))
914 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
918 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
920 #endif /* HAVE_IPV6 */
923 /* This condition is never met, if we are using rt_socket.c */
926 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
927 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
931 /* Uninstall the route from kernel. */
933 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
936 struct nexthop
*nexthop
;
938 switch (PREFIX_FAMILY (&rn
->p
))
941 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
945 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
947 #endif /* HAVE_IPV6 */
950 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
951 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
956 /* Uninstall the route from kernel. */
958 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
960 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
962 redistribute_delete (&rn
->p
, rib
);
963 if (! RIB_SYSTEM_ROUTE (rib
))
964 rib_uninstall_kernel (rn
, rib
);
965 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
969 static void rib_unlink (struct route_node
*, struct rib
*);
971 /* Core function for processing routing information base. */
973 rib_process (struct route_node
*rn
)
977 struct rib
*fib
= NULL
;
978 struct rib
*select
= NULL
;
979 struct rib
*del
= NULL
;
981 struct nexthop
*nexthop
= NULL
;
982 char buf
[INET6_ADDRSTRLEN
];
986 if (IS_ZEBRA_DEBUG_RIB
|| IS_ZEBRA_DEBUG_RIB_Q
)
987 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
989 for (rib
= rn
->info
; rib
; rib
= next
)
991 /* The next pointer is saved, because current pointer
992 * may be passed to rib_unlink() in the middle of iteration.
996 /* Currently installed rib. */
997 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
999 assert (fib
== NULL
);
1003 /* Unlock removed routes, so they'll be freed, bar the FIB entry,
1004 * which we need to do do further work with below.
1006 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1010 if (IS_ZEBRA_DEBUG_RIB
)
1011 zlog_debug ("%s: %s/%d: rn %p, removing rib %p", __func__
,
1012 buf
, rn
->p
.prefixlen
, rn
, rib
);
1013 rib_unlink (rn
, rib
);
1021 /* Skip unreachable nexthop. */
1022 if (! nexthop_active_update (rn
, rib
, 0))
1025 /* Infinit distance. */
1026 if (rib
->distance
== DISTANCE_INFINITY
)
1029 /* Newly selected rib, the common case. */
1036 /* filter route selection in following order:
1037 * - connected beats other types
1038 * - lower distance beats higher
1039 * - lower metric beats higher for equal distance
1040 * - last, hence oldest, route wins tie break.
1043 /* Connected routes. Pick the last connected
1044 * route of the set of lowest metric connected routes.
1046 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1048 if (select
->type
!= ZEBRA_ROUTE_CONNECT
1049 || rib
->metric
<= select
->metric
)
1053 else if (select
->type
== ZEBRA_ROUTE_CONNECT
)
1056 /* higher distance loses */
1057 if (rib
->distance
> select
->distance
)
1061 if (rib
->distance
< select
->distance
)
1067 /* metric tie-breaks equal distance */
1068 if (rib
->metric
<= select
->metric
)
1070 } /* for (rib = rn->info; rib; rib = next) */
1072 /* After the cycle is finished, the following pointers will be set:
1073 * select --- the winner RIB entry, if any was found, otherwise NULL
1074 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1075 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1079 /* Same RIB entry is selected. Update FIB and finish. */
1080 if (select
&& select
== fib
)
1082 if (IS_ZEBRA_DEBUG_RIB
)
1083 zlog_debug ("%s: %s/%d: Updating existing route, select %p, fib %p",
1084 __func__
, buf
, rn
->p
.prefixlen
, select
, fib
);
1085 if (CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
1087 redistribute_delete (&rn
->p
, select
);
1088 if (! RIB_SYSTEM_ROUTE (select
))
1089 rib_uninstall_kernel (rn
, select
);
1091 /* Set real nexthop. */
1092 nexthop_active_update (rn
, select
, 1);
1094 if (! RIB_SYSTEM_ROUTE (select
))
1095 rib_install_kernel (rn
, select
);
1096 redistribute_add (&rn
->p
, select
);
1098 else if (! RIB_SYSTEM_ROUTE (select
))
1100 /* Housekeeping code to deal with
1101 race conditions in kernel with linux
1102 netlink reporting interface up before IPv4 or IPv6 protocol
1103 is ready to add routes.
1104 This makes sure the routes are IN the kernel.
1107 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1108 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1114 rib_install_kernel (rn
, select
);
1119 /* At this point we either haven't found the best RIB entry or it is
1120 * different from what we currently intend to flag with SELECTED. In both
1121 * cases, if a RIB block is present in FIB, it should be withdrawn.
1125 if (IS_ZEBRA_DEBUG_RIB
)
1126 zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__
,
1127 buf
, rn
->p
.prefixlen
, fib
);
1128 redistribute_delete (&rn
->p
, fib
);
1129 if (! RIB_SYSTEM_ROUTE (fib
))
1130 rib_uninstall_kernel (rn
, fib
);
1131 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1133 /* Set real nexthop. */
1134 nexthop_active_update (rn
, fib
, 1);
1137 /* Regardless of some RIB entry being SELECTED or not before, now we can
1138 * tell, that if a new winner exists, FIB is still not updated with this
1139 * data, but ready to be.
1143 if (IS_ZEBRA_DEBUG_RIB
)
1144 zlog_debug ("%s: %s/%d: Adding route, select %p", __func__
, buf
,
1145 rn
->p
.prefixlen
, select
);
1146 /* Set real nexthop. */
1147 nexthop_active_update (rn
, select
, 1);
1149 if (! RIB_SYSTEM_ROUTE (select
))
1150 rib_install_kernel (rn
, select
);
1151 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1152 redistribute_add (&rn
->p
, select
);
1155 /* FIB route was removed, should be deleted */
1158 if (IS_ZEBRA_DEBUG_RIB
)
1159 zlog_debug ("%s: %s/%d: Deleting fib %p, rn %p", __func__
, buf
,
1160 rn
->p
.prefixlen
, del
, rn
);
1161 rib_unlink (rn
, del
);
1165 if (IS_ZEBRA_DEBUG_RIB_Q
)
1166 zlog_debug ("%s: %s/%d: rn %p dequeued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1169 /* Take a list of route_node structs and return 1, if there was a record
1170 * picked from it and processed by rib_process(). Don't process more,
1171 * than one RN record; operate only in the specified sub-queue.
1174 process_subq (struct list
* subq
, u_char qindex
)
1176 struct listnode
*lnode
= listhead (subq
);
1177 struct route_node
*rnode
;
1182 rnode
= listgetdata (lnode
);
1183 rib_process (rnode
);
1185 if (rnode
->info
) /* The first RIB record is holding the flags bitmask. */
1186 UNSET_FLAG (((struct rib
*)rnode
->info
)->rn_status
, RIB_ROUTE_QUEUED(qindex
));
1190 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1191 __func__
, rnode
, rnode
->lock
);
1192 zlog_backtrace(LOG_DEBUG
);
1195 route_unlock_node (rnode
);
1196 list_delete_node (subq
, lnode
);
1200 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1201 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1202 * is pointed to the meta queue structure.
1204 static wq_item_status
1205 meta_queue_process (struct work_queue
*dummy
, void *data
)
1207 struct meta_queue
* mq
= data
;
1210 for (i
= 0; i
< MQ_SIZE
; i
++)
1211 if (process_subq (mq
->subq
[i
], i
))
1216 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1219 /* Map from rib types to queue type (priority) in meta queue */
1220 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1221 [ZEBRA_ROUTE_SYSTEM
] = 4,
1222 [ZEBRA_ROUTE_KERNEL
] = 0,
1223 [ZEBRA_ROUTE_CONNECT
] = 0,
1224 [ZEBRA_ROUTE_STATIC
] = 1,
1225 [ZEBRA_ROUTE_RIP
] = 2,
1226 [ZEBRA_ROUTE_RIPNG
] = 2,
1227 [ZEBRA_ROUTE_OSPF
] = 2,
1228 [ZEBRA_ROUTE_OSPF6
] = 2,
1229 [ZEBRA_ROUTE_ISIS
] = 2,
1230 [ZEBRA_ROUTE_BGP
] = 3,
1231 [ZEBRA_ROUTE_HSLS
] = 4,
1234 /* Look into the RN and queue it into one or more priority queues,
1235 * increasing the size for each data push done.
1238 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
1241 char buf
[INET6_ADDRSTRLEN
];
1243 if (IS_ZEBRA_DEBUG_RIB_Q
)
1244 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1246 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1248 u_char qindex
= meta_queue_map
[rib
->type
];
1250 /* Invariant: at this point we always have rn->info set. */
1251 if (CHECK_FLAG (((struct rib
*)rn
->info
)->rn_status
, RIB_ROUTE_QUEUED(qindex
)))
1253 if (IS_ZEBRA_DEBUG_RIB_Q
)
1254 zlog_debug ("%s: %s/%d: rn %p is already queued in sub-queue %u",
1255 __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1259 SET_FLAG (((struct rib
*)rn
->info
)->rn_status
, RIB_ROUTE_QUEUED(qindex
));
1260 listnode_add (mq
->subq
[qindex
], rn
);
1261 route_lock_node (rn
);
1264 if (IS_ZEBRA_DEBUG_RIB_Q
)
1265 zlog_debug ("%s: %s/%d: queued rn %p into sub-queue %u",
1266 __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1270 /* Add route_node to work queue and schedule processing */
1272 rib_queue_add (struct zebra_t
*zebra
, struct route_node
*rn
)
1275 if (IS_ZEBRA_DEBUG_RIB_Q
)
1277 char buf
[INET6_ADDRSTRLEN
];
1279 zlog_info ("%s: %s/%d: work queue added", __func__
,
1280 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
),
1285 * The RIB queue should normally be either empty or holding the only
1286 * work_queue_item element. In the latter case this element would
1287 * hold a pointer to the meta queue structure, which must be used to
1288 * actually queue the route nodes to process. So create the MQ
1289 * holder, if necessary, then push the work into it in any case.
1290 * This semantics was introduced after 0.99.9 release.
1292 if (!zebra
->ribq
->items
->count
)
1293 work_queue_add (zebra
->ribq
, zebra
->mq
);
1295 rib_meta_queue_add (zebra
->mq
, rn
);
1298 /* Create new meta queue.
1299 A destructor function doesn't seem to be necessary here.
1301 static struct meta_queue
*
1302 meta_queue_new (void)
1304 struct meta_queue
*new;
1307 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
1310 for (i
= 0; i
< MQ_SIZE
; i
++)
1312 new->subq
[i
] = list_new ();
1313 assert(new->subq
[i
]);
1319 /* initialise zebra rib work queue */
1321 rib_queue_init (struct zebra_t
*zebra
)
1323 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1324 "route_node processing")))
1326 zlog_err ("%s: could not initialise work queue!", __func__
);
1330 /* fill in the work queue spec */
1331 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1332 zebra
->ribq
->spec
.errorfunc
= NULL
;
1333 /* XXX: TODO: These should be runtime configurable via vty */
1334 zebra
->ribq
->spec
.max_retries
= 3;
1335 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
1337 if (!(zebra
->mq
= meta_queue_new ()))
1338 zlog_err ("%s: could not initialise meta queue!", __func__
);
1341 /* RIB updates are processed via a queue of pointers to route_nodes.
1343 * The queue length is bounded by the maximal size of the routing table,
1344 * as a route_node will not be requeued, if already queued.
1346 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
1347 * state, or static_install_ipv{4,6} (when an existing RIB is updated)
1348 * and then submit route_node to queue for best-path selection later.
1349 * Order of add/delete state changes are preserved for any given RIB.
1351 * Deleted RIBs are reaped during best-path selection.
1354 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
1355 * |-------->| | best RIB, if required
1357 * static_install->|->rib_addqueue...... -> rib_process
1359 * |-------->| |-> rib_unlink
1360 * |-> set RIB_ENTRY_REMOVE |
1361 * rib_delnode (RIB freed)
1364 * Queueing state for a route_node is kept in the head RIB entry, this
1365 * state must be preserved as and when the head RIB entry of a
1366 * route_node is changed by rib_unlink / rib_link. A small complication,
1367 * but saves having to allocate a dedicated object for this.
1369 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1371 * - route_nodes: refcounted by:
1372 * - RIBs attached to route_node:
1373 * - managed by: rib_link/unlink
1374 * - route_node processing queue
1375 * - managed by: rib_addqueue, rib_process.
1379 /* Add RIB to head of the route node. */
1381 rib_link (struct route_node
*rn
, struct rib
*rib
)
1384 char buf
[INET6_ADDRSTRLEN
];
1388 route_lock_node (rn
); /* rn route table reference */
1390 if (IS_ZEBRA_DEBUG_RIB
)
1392 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1393 zlog_debug ("%s: %s/%d: rn %p, rib %p", __func__
,
1394 buf
, rn
->p
.prefixlen
, rn
, rib
);
1400 if (IS_ZEBRA_DEBUG_RIB
)
1401 zlog_debug ("%s: %s/%d: new head, rn_status copied over", __func__
,
1402 buf
, rn
->p
.prefixlen
);
1404 /* Transfer the rn status flags to the new head RIB */
1405 rib
->rn_status
= head
->rn_status
;
1409 rib_queue_add (&zebrad
, rn
);
1413 rib_addnode (struct route_node
*rn
, struct rib
*rib
)
1415 /* RIB node has been un-removed before route-node is processed.
1416 * route_node must hence already be on the queue for processing..
1418 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1420 if (IS_ZEBRA_DEBUG_RIB
)
1422 char buf
[INET6_ADDRSTRLEN
];
1423 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1424 zlog_debug ("%s: %s/%d: rn %p, un-removed rib %p",
1425 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1427 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1434 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
1436 struct nexthop
*nexthop
, *next
;
1437 char buf
[INET6_ADDRSTRLEN
];
1441 if (IS_ZEBRA_DEBUG_RIB
)
1443 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1444 zlog_debug ("%s: %s/%d: rn %p, rib %p",
1445 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1449 rib
->next
->prev
= rib
->prev
;
1452 rib
->prev
->next
= rib
->next
;
1455 rn
->info
= rib
->next
;
1459 if (IS_ZEBRA_DEBUG_RIB
)
1460 zlog_debug ("%s: %s/%d: rn %p, rib %p, new head copy",
1461 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1462 rib
->next
->rn_status
= rib
->rn_status
;
1466 /* free RIB and nexthops */
1467 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= next
)
1469 next
= nexthop
->next
;
1470 nexthop_free (nexthop
);
1472 XFREE (MTYPE_RIB
, rib
);
1474 route_unlock_node (rn
); /* rn route table reference */
1478 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
1480 if (IS_ZEBRA_DEBUG_RIB
)
1482 char buf
[INET6_ADDRSTRLEN
];
1483 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1484 zlog_debug ("%s: %s/%d: rn %p, rib %p, removing", __func__
,
1485 buf
, rn
->p
.prefixlen
, rn
, rib
);
1487 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1488 rib_queue_add (&zebrad
, rn
);
1492 rib_add_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1493 struct in_addr
*gate
, struct in_addr
*src
,
1494 unsigned int ifindex
, u_int32_t vrf_id
,
1495 u_int32_t metric
, u_char distance
)
1498 struct rib
*same
= NULL
;
1499 struct route_table
*table
;
1500 struct route_node
*rn
;
1501 struct nexthop
*nexthop
;
1504 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1508 /* Make it sure prefixlen is applied to the prefix. */
1509 apply_mask_ipv4 (p
);
1511 /* Set default distance by route type. */
1514 distance
= route_info
[type
].distance
;
1516 /* iBGP distance is 200. */
1517 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1521 /* Lookup route node.*/
1522 rn
= route_node_get (table
, (struct prefix
*) p
);
1524 /* If same type of route are installed, treat it as a implicit
1526 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1528 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1531 if (rib
->type
!= type
)
1533 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
1538 /* Duplicate connected route comes in. */
1539 else if ((nexthop
= rib
->nexthop
) &&
1540 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
1541 nexthop
->ifindex
== ifindex
&&
1542 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1549 /* Allocate new rib structure. */
1550 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1552 rib
->distance
= distance
;
1554 rib
->metric
= metric
;
1555 rib
->table
= vrf_id
;
1556 rib
->nexthop_num
= 0;
1557 rib
->uptime
= time (NULL
);
1559 /* Nexthop settings. */
1563 nexthop_ipv4_ifindex_add (rib
, gate
, src
, ifindex
);
1565 nexthop_ipv4_add (rib
, gate
, src
);
1568 nexthop_ifindex_add (rib
, ifindex
);
1570 /* If this route is kernel route, set FIB flag to the route. */
1571 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1572 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1573 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1575 /* Link new rib to node.*/
1576 if (IS_ZEBRA_DEBUG_RIB
)
1577 zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__
, rn
, rib
);
1578 rib_addnode (rn
, rib
);
1580 /* Free implicit route.*/
1583 if (IS_ZEBRA_DEBUG_RIB
)
1584 zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__
, rn
, rib
);
1585 rib_delnode (rn
, same
);
1588 route_unlock_node (rn
);
1592 /* This function dumps the contents of a given RIB entry into
1593 * standard debug log. Calling function name and IP prefix in
1594 * question are passed as 1st and 2nd arguments.
1597 void rib_dump (const char * func
, const struct prefix_ipv4
* p
, const struct rib
* rib
)
1599 char straddr1
[INET_ADDRSTRLEN
], straddr2
[INET_ADDRSTRLEN
];
1600 struct nexthop
*nexthop
;
1602 inet_ntop (AF_INET
, &p
->prefix
, straddr1
, INET_ADDRSTRLEN
);
1603 zlog_debug ("%s: dumping RIB entry %p for %s/%d", func
, rib
, straddr1
, p
->prefixlen
);
1606 "%s: refcnt == %lu, uptime == %lu, type == %u, table == %d",
1609 (unsigned long) rib
->uptime
,
1615 "%s: metric == %u, distance == %u, flags == %u, status == %u",
1624 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
1627 rib
->nexthop_active_num
,
1628 rib
->nexthop_fib_num
1630 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1632 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, straddr1
, INET_ADDRSTRLEN
);
1633 inet_ntop (AF_INET
, &nexthop
->rgate
.ipv4
.s_addr
, straddr2
, INET_ADDRSTRLEN
);
1636 "%s: NH %s (%s) with flags %s%s%s",
1640 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
1641 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
1642 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
1645 zlog_debug ("%s: dump complete", func
);
1648 /* This is an exported helper to rtm_read() to dump the strange
1649 * RIB entry found by rib_lookup_ipv4_route()
1652 void rib_lookup_and_dump (struct prefix_ipv4
* p
)
1654 struct route_table
*table
;
1655 struct route_node
*rn
;
1657 char prefix_buf
[INET_ADDRSTRLEN
];
1660 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1663 zlog_err ("%s: vrf_table() returned NULL", __func__
);
1667 inet_ntop (AF_INET
, &p
->prefix
.s_addr
, prefix_buf
, INET_ADDRSTRLEN
);
1668 /* Scan the RIB table for exactly matching RIB entry. */
1669 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1671 /* No route for this prefix. */
1674 zlog_debug ("%s: lookup failed for %s/%d", __func__
, prefix_buf
, p
->prefixlen
);
1679 route_unlock_node (rn
);
1682 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1686 "%s: rn %p, rib %p: %s, %s",
1690 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
1691 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
1693 rib_dump (__func__
, p
, rib
);
1697 /* Check if requested address assignment will fail due to another
1698 * route being installed by zebra in FIB already. Take necessary
1699 * actions, if needed: remove such a route from FIB and deSELECT
1700 * corresponding RIB entry. Then put affected RN into RIBQ head.
1702 void rib_lookup_and_pushup (struct prefix_ipv4
* p
)
1704 struct route_table
*table
;
1705 struct route_node
*rn
;
1707 unsigned changed
= 0;
1709 if (NULL
== (table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0)))
1711 zlog_err ("%s: vrf_table() returned NULL", __func__
);
1715 /* No matches would be the simplest case. */
1716 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
1720 route_unlock_node (rn
);
1722 /* Check all RIB entries. In case any changes have to be done, requeue
1723 * the RN into RIBQ head. If the routing message about the new connected
1724 * route (generated by the IP address we are going to assign very soon)
1725 * comes before the RIBQ is processed, the new RIB entry will join
1726 * RIBQ record already on head. This is necessary for proper revalidation
1727 * of the rest of the RIB.
1729 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1731 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) &&
1732 ! RIB_SYSTEM_ROUTE (rib
))
1735 if (IS_ZEBRA_DEBUG_RIB
)
1737 char buf
[INET_ADDRSTRLEN
];
1738 inet_ntop (rn
->p
.family
, &p
->prefix
, buf
, INET_ADDRSTRLEN
);
1739 zlog_debug ("%s: freeing way for connected prefix %s/%d", __func__
, buf
, p
->prefixlen
);
1740 rib_dump (__func__
, (struct prefix_ipv4
*)&rn
->p
, rib
);
1742 rib_uninstall (rn
, rib
);
1746 rib_queue_add (&zebrad
, rn
);
1750 rib_add_ipv4_multipath (struct prefix_ipv4
*p
, struct rib
*rib
)
1752 struct route_table
*table
;
1753 struct route_node
*rn
;
1755 struct nexthop
*nexthop
;
1758 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1761 /* Make it sure prefixlen is applied to the prefix. */
1762 apply_mask_ipv4 (p
);
1764 /* Set default distance by route type. */
1765 if (rib
->distance
== 0)
1767 rib
->distance
= route_info
[rib
->type
].distance
;
1769 /* iBGP distance is 200. */
1770 if (rib
->type
== ZEBRA_ROUTE_BGP
1771 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
1772 rib
->distance
= 200;
1775 /* Lookup route node.*/
1776 rn
= route_node_get (table
, (struct prefix
*) p
);
1778 /* If same type of route are installed, treat it as a implicit
1780 for (same
= rn
->info
; same
; same
= same
->next
)
1782 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
1785 if (same
->type
== rib
->type
&& same
->table
== rib
->table
1786 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
1790 /* If this route is kernel route, set FIB flag to the route. */
1791 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
1792 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1793 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1795 /* Link new rib to node.*/
1796 rib_addnode (rn
, rib
);
1797 if (IS_ZEBRA_DEBUG_RIB
)
1799 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
1801 rib_dump (__func__
, p
, rib
);
1804 /* Free implicit route.*/
1807 if (IS_ZEBRA_DEBUG_RIB
)
1809 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
1810 __func__
, rn
, same
);
1811 rib_dump (__func__
, p
, same
);
1813 rib_delnode (rn
, same
);
1816 route_unlock_node (rn
);
1820 /* XXX factor with rib_delete_ipv6 */
1822 rib_delete_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1823 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1825 struct route_table
*table
;
1826 struct route_node
*rn
;
1828 struct rib
*fib
= NULL
;
1829 struct rib
*same
= NULL
;
1830 struct nexthop
*nexthop
;
1831 char buf1
[INET_ADDRSTRLEN
];
1832 char buf2
[INET_ADDRSTRLEN
];
1835 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1840 apply_mask_ipv4 (p
);
1842 if (IS_ZEBRA_DEBUG_KERNEL
&& gate
)
1843 zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
1844 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1849 /* Lookup route node. */
1850 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1853 if (IS_ZEBRA_DEBUG_KERNEL
)
1856 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
1857 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1859 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
1862 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
1863 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1867 return ZEBRA_ERR_RTNOEXIST
;
1870 /* Lookup same type route. */
1871 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1873 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1876 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1879 if (rib
->type
!= type
)
1881 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
1882 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&& nexthop
->ifindex
== ifindex
)
1887 route_unlock_node (rn
);
1888 route_unlock_node (rn
);
1894 /* Make sure that the route found has the same gateway. */
1895 else if (gate
== NULL
||
1896 ((nexthop
= rib
->nexthop
) &&
1897 (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
1898 IPV4_ADDR_SAME (&nexthop
->rgate
.ipv4
, gate
))))
1905 /* If same type of route can't be found and this message is from
1909 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
1912 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1913 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1915 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1919 if (IS_ZEBRA_DEBUG_KERNEL
)
1922 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
1923 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1925 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
1929 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
1930 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1935 route_unlock_node (rn
);
1936 return ZEBRA_ERR_RTNOEXIST
;
1941 rib_delnode (rn
, same
);
1943 route_unlock_node (rn
);
1947 /* Install static route into rib. */
1949 static_install_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
1952 struct route_node
*rn
;
1953 struct route_table
*table
;
1956 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1960 /* Lookup existing route */
1961 rn
= route_node_get (table
, p
);
1962 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1964 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1967 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1973 /* Same distance static route is there. Update it with new
1975 route_unlock_node (rn
);
1978 case STATIC_IPV4_GATEWAY
:
1979 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
1981 case STATIC_IPV4_IFNAME
:
1982 nexthop_ifname_add (rib
, si
->gate
.ifname
);
1984 case STATIC_IPV4_BLACKHOLE
:
1985 nexthop_blackhole_add (rib
);
1988 rib_queue_add (&zebrad
, rn
);
1992 /* This is new static route. */
1993 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1995 rib
->type
= ZEBRA_ROUTE_STATIC
;
1996 rib
->distance
= si
->distance
;
1998 rib
->nexthop_num
= 0;
2002 case STATIC_IPV4_GATEWAY
:
2003 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2005 case STATIC_IPV4_IFNAME
:
2006 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2008 case STATIC_IPV4_BLACKHOLE
:
2009 nexthop_blackhole_add (rib
);
2013 /* Save the flags of this static routes (reject, blackhole) */
2014 rib
->flags
= si
->flags
;
2016 /* Link this rib to the tree. */
2017 rib_addnode (rn
, rib
);
2022 static_ipv4_nexthop_same (struct nexthop
*nexthop
, struct static_ipv4
*si
)
2024 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
2025 && si
->type
== STATIC_IPV4_GATEWAY
2026 && IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &si
->gate
.ipv4
))
2028 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2029 && si
->type
== STATIC_IPV4_IFNAME
2030 && strcmp (nexthop
->ifname
, si
->gate
.ifname
) == 0)
2032 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
2033 && si
->type
== STATIC_IPV4_BLACKHOLE
)
2038 /* Uninstall static route from RIB. */
2040 static_uninstall_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
2042 struct route_node
*rn
;
2044 struct nexthop
*nexthop
;
2045 struct route_table
*table
;
2048 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2052 /* Lookup existing route with type and distance. */
2053 rn
= route_node_lookup (table
, p
);
2057 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2059 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2062 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2068 route_unlock_node (rn
);
2072 /* Lookup nexthop. */
2073 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2074 if (static_ipv4_nexthop_same (nexthop
, si
))
2077 /* Can't find nexthop. */
2080 route_unlock_node (rn
);
2084 /* Check nexthop. */
2085 if (rib
->nexthop_num
== 1)
2086 rib_delnode (rn
, rib
);
2089 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2090 rib_uninstall (rn
, rib
);
2091 nexthop_delete (rib
, nexthop
);
2092 nexthop_free (nexthop
);
2093 rib_queue_add (&zebrad
, rn
);
2096 route_unlock_node (rn
);
2099 /* Add static route into static route configuration. */
2101 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2102 u_char flags
, u_char distance
, u_int32_t vrf_id
)
2105 struct route_node
*rn
;
2106 struct static_ipv4
*si
;
2107 struct static_ipv4
*pp
;
2108 struct static_ipv4
*cp
;
2109 struct static_ipv4
*update
= NULL
;
2110 struct route_table
*stable
;
2113 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2117 /* Lookup static route prefix. */
2118 rn
= route_node_get (stable
, p
);
2122 type
= STATIC_IPV4_GATEWAY
;
2124 type
= STATIC_IPV4_IFNAME
;
2126 type
= STATIC_IPV4_BLACKHOLE
;
2128 /* Do nothing if there is a same static route. */
2129 for (si
= rn
->info
; si
; si
= si
->next
)
2131 if (type
== si
->type
2132 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2133 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2135 if (distance
== si
->distance
)
2137 route_unlock_node (rn
);
2145 /* Distance changed. */
2147 static_delete_ipv4 (p
, gate
, ifname
, update
->distance
, vrf_id
);
2149 /* Make new static route structure. */
2150 si
= XCALLOC (MTYPE_STATIC_IPV4
, sizeof (struct static_ipv4
));
2153 si
->distance
= distance
;
2157 si
->gate
.ipv4
= *gate
;
2159 si
->gate
.ifname
= XSTRDUP (0, ifname
);
2161 /* Add new static route information to the tree with sort by
2162 distance value and gateway address. */
2163 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2165 if (si
->distance
< cp
->distance
)
2167 if (si
->distance
> cp
->distance
)
2169 if (si
->type
== STATIC_IPV4_GATEWAY
&& cp
->type
== STATIC_IPV4_GATEWAY
)
2171 if (ntohl (si
->gate
.ipv4
.s_addr
) < ntohl (cp
->gate
.ipv4
.s_addr
))
2173 if (ntohl (si
->gate
.ipv4
.s_addr
) > ntohl (cp
->gate
.ipv4
.s_addr
))
2178 /* Make linked list. */
2188 /* Install into rib. */
2189 static_install_ipv4 (p
, si
);
2194 /* Delete static route from static route configuration. */
2196 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2197 u_char distance
, u_int32_t vrf_id
)
2200 struct route_node
*rn
;
2201 struct static_ipv4
*si
;
2202 struct route_table
*stable
;
2205 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2209 /* Lookup static route prefix. */
2210 rn
= route_node_lookup (stable
, p
);
2216 type
= STATIC_IPV4_GATEWAY
;
2218 type
= STATIC_IPV4_IFNAME
;
2220 type
= STATIC_IPV4_BLACKHOLE
;
2222 /* Find same static route is the tree */
2223 for (si
= rn
->info
; si
; si
= si
->next
)
2224 if (type
== si
->type
2225 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2226 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2229 /* Can't find static route. */
2232 route_unlock_node (rn
);
2236 /* Install into rib. */
2237 static_uninstall_ipv4 (p
, si
);
2239 /* Unlink static route from linked list. */
2241 si
->prev
->next
= si
->next
;
2243 rn
->info
= si
->next
;
2245 si
->next
->prev
= si
->prev
;
2246 route_unlock_node (rn
);
2248 /* Free static route configuration. */
2250 XFREE (0, si
->gate
.ifname
);
2251 XFREE (MTYPE_STATIC_IPV4
, si
);
2253 route_unlock_node (rn
);
2261 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
2262 struct in6_addr
*gate
, unsigned int ifindex
, int table
)
2264 if (type
== ZEBRA_ROUTE_CONNECT
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)) {
2265 #if defined (MUSICA) || defined (LINUX)
2266 /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
2267 if (p
->prefixlen
== 96)
2272 if (type
== ZEBRA_ROUTE_KERNEL
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)
2273 && p
->prefixlen
== 96 && gate
&& IN6_IS_ADDR_UNSPECIFIED (gate
))
2275 kernel_delete_ipv6_old (p
, gate
, ifindex
, 0, table
);
2282 rib_add_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2283 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
,
2284 u_int32_t metric
, u_char distance
)
2287 struct rib
*same
= NULL
;
2288 struct route_table
*table
;
2289 struct route_node
*rn
;
2290 struct nexthop
*nexthop
;
2293 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2297 /* Make sure mask is applied. */
2298 apply_mask_ipv6 (p
);
2300 /* Set default distance by route type. */
2302 distance
= route_info
[type
].distance
;
2304 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2307 /* Filter bogus route. */
2308 if (rib_bogus_ipv6 (type
, p
, gate
, ifindex
, 0))
2311 /* Lookup route node.*/
2312 rn
= route_node_get (table
, (struct prefix
*) p
);
2314 /* If same type of route are installed, treat it as a implicit
2316 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2318 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2321 if (rib
->type
!= type
)
2323 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2328 else if ((nexthop
= rib
->nexthop
) &&
2329 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2330 nexthop
->ifindex
== ifindex
)
2337 /* Allocate new rib structure. */
2338 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2341 rib
->distance
= distance
;
2343 rib
->metric
= metric
;
2344 rib
->table
= vrf_id
;
2345 rib
->nexthop_num
= 0;
2346 rib
->uptime
= time (NULL
);
2348 /* Nexthop settings. */
2352 nexthop_ipv6_ifindex_add (rib
, gate
, ifindex
);
2354 nexthop_ipv6_add (rib
, gate
);
2357 nexthop_ifindex_add (rib
, ifindex
);
2359 /* If this route is kernel route, set FIB flag to the route. */
2360 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2361 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2362 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2364 /* Link new rib to node.*/
2365 rib_addnode (rn
, rib
);
2367 /* Free implicit route.*/
2369 rib_delnode (rn
, same
);
2371 route_unlock_node (rn
);
2375 /* XXX factor with rib_delete_ipv6 */
2377 rib_delete_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2378 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
2380 struct route_table
*table
;
2381 struct route_node
*rn
;
2383 struct rib
*fib
= NULL
;
2384 struct rib
*same
= NULL
;
2385 struct nexthop
*nexthop
;
2386 char buf1
[INET6_ADDRSTRLEN
];
2387 char buf2
[INET6_ADDRSTRLEN
];
2390 apply_mask_ipv6 (p
);
2393 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2397 /* Lookup route node. */
2398 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2401 if (IS_ZEBRA_DEBUG_KERNEL
)
2404 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
2405 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2407 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
2410 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
2411 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2415 return ZEBRA_ERR_RTNOEXIST
;
2418 /* Lookup same type route. */
2419 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2421 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2424 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2427 if (rib
->type
!= type
)
2429 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2430 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&& nexthop
->ifindex
== ifindex
)
2435 route_unlock_node (rn
);
2436 route_unlock_node (rn
);
2442 /* Make sure that the route found has the same gateway. */
2443 else if (gate
== NULL
||
2444 ((nexthop
= rib
->nexthop
) &&
2445 (IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
) ||
2446 IPV6_ADDR_SAME (&nexthop
->rgate
.ipv6
, gate
))))
2453 /* If same type of route can't be found and this message is from
2457 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
2460 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2461 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2463 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
2467 if (IS_ZEBRA_DEBUG_KERNEL
)
2470 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
2471 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2473 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
2477 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
2478 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2483 route_unlock_node (rn
);
2484 return ZEBRA_ERR_RTNOEXIST
;
2489 rib_delnode (rn
, same
);
2491 route_unlock_node (rn
);
2495 /* Install static route into rib. */
2497 static_install_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2500 struct route_table
*table
;
2501 struct route_node
*rn
;
2504 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2508 /* Lookup existing route */
2509 rn
= route_node_get (table
, p
);
2510 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2512 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2515 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2521 /* Same distance static route is there. Update it with new
2523 route_unlock_node (rn
);
2527 case STATIC_IPV6_GATEWAY
:
2528 nexthop_ipv6_add (rib
, &si
->ipv6
);
2530 case STATIC_IPV6_IFNAME
:
2531 nexthop_ifname_add (rib
, si
->ifname
);
2533 case STATIC_IPV6_GATEWAY_IFNAME
:
2534 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2537 rib_queue_add (&zebrad
, rn
);
2541 /* This is new static route. */
2542 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2544 rib
->type
= ZEBRA_ROUTE_STATIC
;
2545 rib
->distance
= si
->distance
;
2547 rib
->nexthop_num
= 0;
2551 case STATIC_IPV6_GATEWAY
:
2552 nexthop_ipv6_add (rib
, &si
->ipv6
);
2554 case STATIC_IPV6_IFNAME
:
2555 nexthop_ifname_add (rib
, si
->ifname
);
2557 case STATIC_IPV6_GATEWAY_IFNAME
:
2558 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2562 /* Save the flags of this static routes (reject, blackhole) */
2563 rib
->flags
= si
->flags
;
2565 /* Link this rib to the tree. */
2566 rib_addnode (rn
, rib
);
2571 static_ipv6_nexthop_same (struct nexthop
*nexthop
, struct static_ipv6
*si
)
2573 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
2574 && si
->type
== STATIC_IPV6_GATEWAY
2575 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
))
2577 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2578 && si
->type
== STATIC_IPV6_IFNAME
2579 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2581 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
2582 && si
->type
== STATIC_IPV6_GATEWAY_IFNAME
2583 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
)
2584 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2590 static_uninstall_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2592 struct route_table
*table
;
2593 struct route_node
*rn
;
2595 struct nexthop
*nexthop
;
2598 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2602 /* Lookup existing route with type and distance. */
2603 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2607 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2609 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2612 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2618 route_unlock_node (rn
);
2622 /* Lookup nexthop. */
2623 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2624 if (static_ipv6_nexthop_same (nexthop
, si
))
2627 /* Can't find nexthop. */
2630 route_unlock_node (rn
);
2634 /* Check nexthop. */
2635 if (rib
->nexthop_num
== 1)
2637 rib_delnode (rn
, rib
);
2641 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2642 rib_uninstall (rn
, rib
);
2643 nexthop_delete (rib
, nexthop
);
2644 nexthop_free (nexthop
);
2645 rib_queue_add (&zebrad
, rn
);
2648 route_unlock_node (rn
);
2651 /* Add static route into static route configuration. */
2653 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2654 const char *ifname
, u_char flags
, u_char distance
,
2657 struct route_node
*rn
;
2658 struct static_ipv6
*si
;
2659 struct static_ipv6
*pp
;
2660 struct static_ipv6
*cp
;
2661 struct route_table
*stable
;
2664 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2669 (type
== STATIC_IPV6_GATEWAY
|| type
== STATIC_IPV6_GATEWAY_IFNAME
))
2673 (type
== STATIC_IPV6_GATEWAY_IFNAME
|| type
== STATIC_IPV6_IFNAME
))
2676 /* Lookup static route prefix. */
2677 rn
= route_node_get (stable
, p
);
2679 /* Do nothing if there is a same static route. */
2680 for (si
= rn
->info
; si
; si
= si
->next
)
2682 if (distance
== si
->distance
2684 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2685 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2687 route_unlock_node (rn
);
2692 /* Make new static route structure. */
2693 si
= XCALLOC (MTYPE_STATIC_IPV6
, sizeof (struct static_ipv6
));
2696 si
->distance
= distance
;
2701 case STATIC_IPV6_GATEWAY
:
2704 case STATIC_IPV6_IFNAME
:
2705 si
->ifname
= XSTRDUP (0, ifname
);
2707 case STATIC_IPV6_GATEWAY_IFNAME
:
2709 si
->ifname
= XSTRDUP (0, ifname
);
2713 /* Add new static route information to the tree with sort by
2714 distance value and gateway address. */
2715 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2717 if (si
->distance
< cp
->distance
)
2719 if (si
->distance
> cp
->distance
)
2723 /* Make linked list. */
2733 /* Install into rib. */
2734 static_install_ipv6 (p
, si
);
2739 /* Delete static route from static route configuration. */
2741 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2742 const char *ifname
, u_char distance
, u_int32_t vrf_id
)
2744 struct route_node
*rn
;
2745 struct static_ipv6
*si
;
2746 struct route_table
*stable
;
2749 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2753 /* Lookup static route prefix. */
2754 rn
= route_node_lookup (stable
, p
);
2758 /* Find same static route is the tree */
2759 for (si
= rn
->info
; si
; si
= si
->next
)
2760 if (distance
== si
->distance
2762 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2763 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2766 /* Can't find static route. */
2769 route_unlock_node (rn
);
2773 /* Install into rib. */
2774 static_uninstall_ipv6 (p
, si
);
2776 /* Unlink static route from linked list. */
2778 si
->prev
->next
= si
->next
;
2780 rn
->info
= si
->next
;
2782 si
->next
->prev
= si
->prev
;
2784 /* Free static route configuration. */
2786 XFREE (0, si
->ifname
);
2787 XFREE (MTYPE_STATIC_IPV6
, si
);
2791 #endif /* HAVE_IPV6 */
2793 /* RIB update function. */
2797 struct route_node
*rn
;
2798 struct route_table
*table
;
2800 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2802 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2804 rib_queue_add (&zebrad
, rn
);
2806 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2808 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2810 rib_queue_add (&zebrad
, rn
);
2814 /* Remove all routes which comes from non main table. */
2816 rib_weed_table (struct route_table
*table
)
2818 struct route_node
*rn
;
2823 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2824 for (rib
= rn
->info
; rib
; rib
= next
)
2828 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2831 if (rib
->table
!= zebrad
.rtm_table_default
&&
2832 rib
->table
!= RT_TABLE_MAIN
)
2833 rib_delnode (rn
, rib
);
2837 /* Delete all routes from non main table. */
2839 rib_weed_tables (void)
2841 rib_weed_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2842 rib_weed_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2845 /* Delete self installed routes after zebra is relaunched. */
2847 rib_sweep_table (struct route_table
*table
)
2849 struct route_node
*rn
;
2855 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2856 for (rib
= rn
->info
; rib
; rib
= next
)
2860 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2863 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
2864 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
2866 ret
= rib_uninstall_kernel (rn
, rib
);
2868 rib_delnode (rn
, rib
);
2873 /* Sweep all RIB tables. */
2875 rib_sweep_route (void)
2877 rib_sweep_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2878 rib_sweep_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2881 /* Close RIB and clean up kernel routes. */
2883 rib_close_table (struct route_table
*table
)
2885 struct route_node
*rn
;
2889 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2890 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2892 if (! RIB_SYSTEM_ROUTE (rib
)
2893 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2894 rib_uninstall_kernel (rn
, rib
);
2898 /* Close all RIB tables. */
2902 rib_close_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2903 rib_close_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2906 /* Routing information base initialize. */
2910 rib_queue_init (&zebrad
);
2911 /* VRF initialization. */