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"
33 #include "zebra/rib.h"
35 #include "zebra/zserv.h"
36 #include "zebra/redistribute.h"
37 #include "zebra/debug.h"
39 /* Default rtm_table for all clients */
40 extern struct zebra_t zebrad
;
42 /* Each route type's string and default distance value. */
49 {ZEBRA_ROUTE_SYSTEM
, 0},
50 {ZEBRA_ROUTE_KERNEL
, 0},
51 {ZEBRA_ROUTE_CONNECT
, 0},
52 {ZEBRA_ROUTE_STATIC
, 1},
53 {ZEBRA_ROUTE_RIP
, 120},
54 {ZEBRA_ROUTE_RIPNG
, 120},
55 {ZEBRA_ROUTE_OSPF
, 110},
56 {ZEBRA_ROUTE_OSPF6
, 110},
57 {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */}
60 /* Vector for routing table. */
63 /* Allocate new VRF. */
65 vrf_alloc (char *name
)
69 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
73 vrf
->name
= XSTRDUP (MTYPE_VRF_NAME
, name
);
75 /* Allocate routing table and static table. */
76 vrf
->table
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
77 vrf
->table
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
78 vrf
->stable
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
79 vrf
->stable
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
86 vrf_free (struct vrf
*vrf
)
89 XFREE (MTYPE_VRF_NAME
, vrf
->name
);
90 XFREE (MTYPE_VRF
, vrf
);
93 /* Lookup VRF by identifier. */
95 vrf_lookup (u_int32_t id
)
97 return vector_lookup (vrf_vector
, id
);
100 /* Lookup VRF by name. */
102 vrf_lookup_by_name (char *name
)
107 for (i
= 0; i
< vector_max (vrf_vector
); i
++)
108 if ((vrf
= vector_slot (vrf_vector
, i
)) != NULL
)
109 if (vrf
->name
&& name
&& strcmp (vrf
->name
, name
) == 0)
114 /* Initialize VRF. */
118 struct vrf
*default_table
;
120 /* Allocate VRF vector. */
121 vrf_vector
= vector_init (1);
123 /* Allocate default main table. */
124 default_table
= vrf_alloc ("Default-IP-Routing-Table");
126 /* Default table index must be 0. */
127 vector_set_index (vrf_vector
, 0, default_table
);
130 /* Lookup route table. */
132 vrf_table (afi_t afi
, safi_t safi
, u_int32_t id
)
136 vrf
= vrf_lookup (id
);
140 return vrf
->table
[afi
][safi
];
143 /* Lookup static route table. */
145 vrf_static_table (afi_t afi
, safi_t safi
, u_int32_t id
)
149 vrf
= vrf_lookup (id
);
153 return vrf
->stable
[afi
][safi
];
156 /* Add nexthop to the end of the list. */
158 nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
160 struct nexthop
*last
;
162 for (last
= rib
->nexthop
; last
&& last
->next
; last
= last
->next
)
165 last
->next
= nexthop
;
167 rib
->nexthop
= nexthop
;
168 nexthop
->prev
= last
;
173 /* Delete specified nexthop from the list. */
175 nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
178 nexthop
->next
->prev
= nexthop
->prev
;
180 nexthop
->prev
->next
= nexthop
->next
;
182 rib
->nexthop
= nexthop
->next
;
188 nexthop_free (struct nexthop
*nexthop
)
191 XFREE (0, nexthop
->ifname
);
192 XFREE (MTYPE_NEXTHOP
, nexthop
);
196 nexthop_ifindex_add (struct rib
*rib
, unsigned int ifindex
)
198 struct nexthop
*nexthop
;
200 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
201 memset (nexthop
, 0, sizeof (struct nexthop
));
202 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
203 nexthop
->ifindex
= ifindex
;
205 nexthop_add (rib
, nexthop
);
211 nexthop_ifname_add (struct rib
*rib
, char *ifname
)
213 struct nexthop
*nexthop
;
215 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
216 memset (nexthop
, 0, sizeof (struct nexthop
));
217 nexthop
->type
= NEXTHOP_TYPE_IFNAME
;
218 nexthop
->ifname
= XSTRDUP (0, ifname
);
220 nexthop_add (rib
, nexthop
);
226 nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
)
228 struct nexthop
*nexthop
;
230 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
231 memset (nexthop
, 0, sizeof (struct nexthop
));
232 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
233 nexthop
->gate
.ipv4
= *ipv4
;
235 nexthop_add (rib
, nexthop
);
241 nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
242 unsigned int ifindex
)
244 struct nexthop
*nexthop
;
246 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
247 memset (nexthop
, 0, sizeof (struct nexthop
));
248 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
249 nexthop
->gate
.ipv4
= *ipv4
;
250 nexthop
->ifindex
= ifindex
;
252 nexthop_add (rib
, nexthop
);
259 nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
261 struct nexthop
*nexthop
;
263 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
264 memset (nexthop
, 0, sizeof (struct nexthop
));
265 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
266 nexthop
->gate
.ipv6
= *ipv6
;
268 nexthop_add (rib
, nexthop
);
274 nexthop_ipv6_ifname_add (struct rib
*rib
, struct in6_addr
*ipv6
,
277 struct nexthop
*nexthop
;
279 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
280 memset (nexthop
, 0, sizeof (struct nexthop
));
281 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFNAME
;
282 nexthop
->gate
.ipv6
= *ipv6
;
283 nexthop
->ifname
= XSTRDUP (0, ifname
);
285 nexthop_add (rib
, nexthop
);
291 nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
292 unsigned int ifindex
)
294 struct nexthop
*nexthop
;
296 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
297 memset (nexthop
, 0, sizeof (struct nexthop
));
298 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
299 nexthop
->gate
.ipv6
= *ipv6
;
300 nexthop
->ifindex
= ifindex
;
302 nexthop_add (rib
, nexthop
);
306 #endif /* HAVE_IPV6 */
309 nexthop_blackhole_add (struct rib
*rib
)
311 struct nexthop
*nexthop
;
313 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
314 memset (nexthop
, 0, sizeof (struct nexthop
));
315 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
316 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
318 nexthop_add (rib
, nexthop
);
323 /* If force flag is not set, do not modify falgs at all for uninstall
324 the route from FIB. */
326 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
327 struct route_node
*top
)
329 struct prefix_ipv4 p
;
330 struct route_table
*table
;
331 struct route_node
*rn
;
333 struct nexthop
*newhop
;
335 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
336 nexthop
->ifindex
= 0;
339 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
341 /* Make lookup prefix. */
342 memset (&p
, 0, sizeof (struct prefix_ipv4
));
344 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
345 p
.prefix
= nexthop
->gate
.ipv4
;
348 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
352 rn
= route_node_match (table
, (struct prefix
*) &p
);
355 route_unlock_node (rn
);
357 /* If lookup self prefix return immidiately. */
361 /* Pick up selected route. */
362 for (match
= rn
->info
; match
; match
= match
->next
)
363 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
366 /* If there is no selected route or matched route is EGP, go up
369 || match
->type
== ZEBRA_ROUTE_BGP
)
373 } while (rn
&& rn
->info
== NULL
);
375 route_lock_node (rn
);
379 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
381 /* Directly point connected route. */
382 newhop
= match
->nexthop
;
383 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
384 nexthop
->ifindex
= newhop
->ifindex
;
388 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
390 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
391 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
392 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
396 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
397 nexthop
->rtype
= newhop
->type
;
398 if (newhop
->type
== NEXTHOP_TYPE_IPV4
||
399 newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
400 nexthop
->rgate
.ipv4
= newhop
->gate
.ipv4
;
401 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
402 || newhop
->type
== NEXTHOP_TYPE_IFNAME
403 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
404 nexthop
->rifindex
= newhop
->ifindex
;
420 /* If force flag is not set, do not modify falgs at all for uninstall
421 the route from FIB. */
423 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
424 struct route_node
*top
)
426 struct prefix_ipv6 p
;
427 struct route_table
*table
;
428 struct route_node
*rn
;
430 struct nexthop
*newhop
;
432 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
433 nexthop
->ifindex
= 0;
436 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
438 /* Make lookup prefix. */
439 memset (&p
, 0, sizeof (struct prefix_ipv6
));
441 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
442 p
.prefix
= nexthop
->gate
.ipv6
;
445 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
449 rn
= route_node_match (table
, (struct prefix
*) &p
);
452 route_unlock_node (rn
);
454 /* If lookup self prefix return immidiately. */
458 /* Pick up selected route. */
459 for (match
= rn
->info
; match
; match
= match
->next
)
460 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
463 /* If there is no selected route or matched route is EGP, go up
466 || match
->type
== ZEBRA_ROUTE_BGP
)
470 } while (rn
&& rn
->info
== NULL
);
472 route_lock_node (rn
);
476 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
478 /* Directly point connected route. */
479 newhop
= match
->nexthop
;
481 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
482 nexthop
->ifindex
= newhop
->ifindex
;
486 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
488 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
489 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
490 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
494 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
495 nexthop
->rtype
= newhop
->type
;
496 if (newhop
->type
== NEXTHOP_TYPE_IPV6
497 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
498 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
499 nexthop
->rgate
.ipv6
= newhop
->gate
.ipv6
;
500 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
501 || newhop
->type
== NEXTHOP_TYPE_IFNAME
502 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
503 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
504 nexthop
->rifindex
= newhop
->ifindex
;
518 #endif /* HAVE_IPV6 */
521 rib_match_ipv4 (struct in_addr addr
)
523 struct prefix_ipv4 p
;
524 struct route_table
*table
;
525 struct route_node
*rn
;
527 struct nexthop
*newhop
;
530 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
534 memset (&p
, 0, sizeof (struct prefix_ipv4
));
536 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
539 rn
= route_node_match (table
, (struct prefix
*) &p
);
543 route_unlock_node (rn
);
545 /* Pick up selected route. */
546 for (match
= rn
->info
; match
; match
= match
->next
)
547 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
550 /* If there is no selected route or matched route is EGP, go up
553 || match
->type
== ZEBRA_ROUTE_BGP
)
557 } while (rn
&& rn
->info
== NULL
);
559 route_lock_node (rn
);
563 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
564 /* Directly point connected route. */
568 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
569 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
579 rib_lookup_ipv4 (struct prefix_ipv4
*p
)
581 struct route_table
*table
;
582 struct route_node
*rn
;
584 struct nexthop
*nexthop
;
587 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
591 rn
= route_node_lookup (table
, (struct prefix
*) p
);
593 /* No route for this prefix. */
598 route_unlock_node (rn
);
600 /* Pick up selected route. */
601 for (match
= rn
->info
; match
; match
= match
->next
)
602 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
))
620 rib_match_ipv6 (struct in6_addr
*addr
)
622 struct prefix_ipv6 p
;
623 struct route_table
*table
;
624 struct route_node
*rn
;
626 struct nexthop
*newhop
;
629 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
633 memset (&p
, 0, sizeof (struct prefix_ipv6
));
635 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
636 IPV6_ADDR_COPY (&p
.prefix
, addr
);
638 rn
= route_node_match (table
, (struct prefix
*) &p
);
642 route_unlock_node (rn
);
644 /* Pick up selected route. */
645 for (match
= rn
->info
; match
; match
= match
->next
)
646 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
649 /* If there is no selected route or matched route is EGP, go up
652 || match
->type
== ZEBRA_ROUTE_BGP
)
656 } while (rn
&& rn
->info
== NULL
);
658 route_lock_node (rn
);
662 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
663 /* Directly point connected route. */
667 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
668 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
676 #endif /* HAVE_IPV6 */
679 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
680 struct nexthop
*nexthop
, int set
)
682 struct interface
*ifp
;
684 switch (nexthop
->type
)
686 case NEXTHOP_TYPE_IFINDEX
:
687 ifp
= if_lookup_by_index (nexthop
->ifindex
);
688 if (ifp
&& if_is_up (ifp
))
689 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
691 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
693 case NEXTHOP_TYPE_IFNAME
:
694 case NEXTHOP_TYPE_IPV6_IFNAME
:
695 ifp
= if_lookup_by_name (nexthop
->ifname
);
696 if (ifp
&& if_is_up (ifp
))
699 nexthop
->ifindex
= ifp
->ifindex
;
700 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
705 nexthop
->ifindex
= 0;
706 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
709 case NEXTHOP_TYPE_IPV4
:
710 case NEXTHOP_TYPE_IPV4_IFINDEX
:
711 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
712 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
714 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
717 case NEXTHOP_TYPE_IPV6
:
718 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
719 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
721 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
723 case NEXTHOP_TYPE_IPV6_IFINDEX
:
724 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
726 ifp
= if_lookup_by_index (nexthop
->ifindex
);
727 if (ifp
&& if_is_up (ifp
))
728 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
730 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
734 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
735 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
737 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
740 #endif /* HAVE_IPV6 */
741 case NEXTHOP_TYPE_BLACKHOLE
:
742 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
747 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
751 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
753 struct nexthop
*nexthop
;
756 rib
->nexthop_active_num
= 0;
757 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
759 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
761 active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
763 nexthop_active_check (rn
, rib
, nexthop
, set
);
764 if ((MULTIPATH_NUM
== 0 || rib
->nexthop_active_num
< MULTIPATH_NUM
)
765 && active
!= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
766 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
768 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
769 rib
->nexthop_active_num
++;
771 return rib
->nexthop_active_num
;
775 #define RIB_SYSTEM_ROUTE(R) \
776 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
779 newrib_free (struct rib
*rib
)
781 struct nexthop
*nexthop
;
782 struct nexthop
*next
;
784 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= next
)
786 next
= nexthop
->next
;
787 nexthop_free (nexthop
);
789 XFREE (MTYPE_RIB
, rib
);
793 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
)
796 struct nexthop
*nexthop
;
798 switch (PREFIX_FAMILY (&rn
->p
))
801 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
805 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
807 #endif /* HAVE_IPV6 */
812 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
813 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
817 /* Uninstall the route from kernel. */
819 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
822 struct nexthop
*nexthop
;
824 switch (PREFIX_FAMILY (&rn
->p
))
827 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
831 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
833 #endif /* HAVE_IPV6 */
836 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
837 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
842 /* Uninstall the route from kernel. */
844 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
846 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
848 redistribute_delete (&rn
->p
, rib
);
849 if (! RIB_SYSTEM_ROUTE (rib
))
850 rib_uninstall_kernel (rn
, rib
);
851 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
855 /* Core function for processing routing information base. */
857 rib_process (struct route_node
*rn
, struct rib
*del
)
861 struct rib
*fib
= NULL
;
862 struct rib
*select
= NULL
;
864 struct nexthop
*nexthop
= NULL
;
866 for (rib
= rn
->info
; rib
; rib
= next
)
870 /* Currently installed rib. */
871 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
874 /* Skip unreachable nexthop. */
875 if (! nexthop_active_update (rn
, rib
, 0))
878 /* Infinit distance. */
879 if (rib
->distance
== DISTANCE_INFINITY
)
882 /* Newly selected rib. */
883 if (! select
|| rib
->distance
< select
->distance
884 || rib
->type
== ZEBRA_ROUTE_CONNECT
)
888 /* Deleted route check. */
889 if (del
&& CHECK_FLAG (del
->flags
, ZEBRA_FLAG_SELECTED
))
892 /* Same route is selected. */
893 if (select
&& select
== fib
)
895 if (CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
897 redistribute_delete (&rn
->p
, select
);
898 if (! RIB_SYSTEM_ROUTE (select
))
899 rib_uninstall_kernel (rn
, select
);
901 /* Set real nexthop. */
902 nexthop_active_update (rn
, select
, 1);
904 if (! RIB_SYSTEM_ROUTE (select
))
905 rib_install_kernel (rn
, select
);
906 redistribute_add (&rn
->p
, select
);
908 else if (! RIB_SYSTEM_ROUTE (select
))
910 /* Housekeeping code to deal with
911 race conditions in kernel with linux
912 netlink reporting interface up before IPv4 or IPv6 protocol
913 is ready to add routes.
914 This makes sure the routes are IN the kernel.
917 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
919 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
922 if (! installed
) rib_install_kernel (rn
, select
);
927 /* Uninstall old rib from forwarding table. */
930 redistribute_delete (&rn
->p
, fib
);
931 if (! RIB_SYSTEM_ROUTE (fib
))
932 rib_uninstall_kernel (rn
, fib
);
933 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
935 /* Set real nexthop. */
936 nexthop_active_update (rn
, fib
, 1);
939 /* Install new rib into forwarding table. */
942 /* Set real nexthop. */
943 nexthop_active_update (rn
, select
, 1);
945 if (! RIB_SYSTEM_ROUTE (select
))
946 rib_install_kernel (rn
, select
);
947 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
948 redistribute_add (&rn
->p
, select
);
952 /* Add RIB to head of the route node. */
954 rib_addnode (struct route_node
*rn
, struct rib
*rib
)
966 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
969 rib
->next
->prev
= rib
->prev
;
971 rib
->prev
->next
= rib
->next
;
973 rn
->info
= rib
->next
;
977 rib_add_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
978 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
,
979 u_int32_t metric
, u_char distance
)
982 struct rib
*same
= NULL
;
983 struct route_table
*table
;
984 struct route_node
*rn
;
985 struct nexthop
*nexthop
;
988 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
992 /* Make it sure prefixlen is applied to the prefix. */
995 /* Set default distance by route type. */
998 distance
= route_info
[type
].distance
;
1000 /* iBGP distance is 200. */
1001 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1005 /* Lookup route node.*/
1006 rn
= route_node_get (table
, (struct prefix
*) p
);
1008 /* If same type of route are installed, treat it as a implicit
1010 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1012 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1014 nexthop
= rib
->nexthop
;
1016 /* Duplicate connected route comes in. */
1017 if (rib
->type
== type
1018 && nexthop
&& nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1019 && nexthop
->ifindex
== ifindex
)
1025 else if (rib
->type
== type
)
1028 rib_delnode (rn
, same
);
1029 route_unlock_node (rn
);
1034 /* Allocate new rib structure. */
1035 rib
= XMALLOC (MTYPE_RIB
, sizeof (struct rib
));
1036 memset (rib
, 0, sizeof (struct rib
));
1038 rib
->distance
= distance
;
1040 rib
->metric
= metric
;
1041 rib
->table
= vrf_id
;
1042 rib
->nexthop_num
= 0;
1043 rib
->uptime
= time (NULL
);
1045 /* Nexthop settings. */
1049 nexthop_ipv4_ifindex_add (rib
, gate
, ifindex
);
1051 nexthop_ipv4_add (rib
, gate
);
1054 nexthop_ifindex_add (rib
, ifindex
);
1056 /* If this route is kernel route, set FIB flag to the route. */
1057 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1058 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1059 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1061 /* Link new rib to node.*/
1062 rib_addnode (rn
, rib
);
1064 /* Process this route node. */
1065 rib_process (rn
, same
);
1067 /* Free implicit route.*/
1075 rib_add_ipv4_multipath (struct prefix_ipv4
*p
, struct rib
*rib
)
1077 struct route_table
*table
;
1078 struct route_node
*rn
;
1080 struct nexthop
*nexthop
;
1083 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1087 /* Make it sure prefixlen is applied to the prefix. */
1088 apply_mask_ipv4 (p
);
1090 /* Set default distance by route type. */
1091 if (rib
->distance
== 0)
1093 rib
->distance
= route_info
[rib
->type
].distance
;
1095 /* iBGP distance is 200. */
1096 if (rib
->type
== ZEBRA_ROUTE_BGP
1097 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
1098 rib
->distance
= 200;
1101 /* Lookup route node.*/
1102 rn
= route_node_get (table
, (struct prefix
*) p
);
1104 /* If same type of route are installed, treat it as a implicit
1106 for (same
= rn
->info
; same
; same
= same
->next
)
1108 if (same
->type
== rib
->type
&& same
->table
== rib
->table
1109 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
1111 rib_delnode (rn
, same
);
1112 route_unlock_node (rn
);
1117 /* If this route is kernel route, set FIB flag to the route. */
1118 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
1119 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1120 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1122 /* Link new rib to node.*/
1123 rib_addnode (rn
, rib
);
1125 /* Process this route node. */
1126 rib_process (rn
, same
);
1128 /* Free implicit route.*/
1136 rib_delete_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1137 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1139 struct route_table
*table
;
1140 struct route_node
*rn
;
1142 struct rib
*fib
= NULL
;
1143 struct rib
*same
= NULL
;
1144 struct nexthop
*nexthop
;
1149 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1154 apply_mask_ipv4 (p
);
1156 if (IS_ZEBRA_DEBUG_KERNEL
&& gate
)
1157 zlog_info ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
1158 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1163 /* Lookup route node. */
1164 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1167 if (IS_ZEBRA_DEBUG_KERNEL
)
1170 zlog_info ("route %s/%d via %s ifindex %d doesn't exist in rib",
1171 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1173 inet_ntop (AF_INET
, gate
, buf2
, BUFSIZ
),
1176 zlog_info ("route %s/%d ifindex %d doesn't exist in rib",
1177 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1181 return ZEBRA_ERR_RTNOEXIST
;
1184 /* Lookup same type route. */
1185 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1187 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1190 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1192 nexthop
= rib
->nexthop
;
1194 if (rib
->type
== type
1195 && nexthop
&& nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1196 && nexthop
->ifindex
== ifindex
)
1201 route_unlock_node (rn
);
1202 route_unlock_node (rn
);
1211 nexthop
= rib
->nexthop
;
1213 /* Make sure that the route found has the same gateway. */
1214 if (rib
->type
== type
1216 (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
1217 IPV4_ADDR_SAME (&nexthop
->rgate
.ipv4
, gate
)) )
1225 if (rib
->type
== type
)
1233 /* If same type of route can't be found and this message is from
1237 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
1240 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1241 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1243 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1247 if (IS_ZEBRA_DEBUG_KERNEL
)
1250 zlog_info ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
1251 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1253 inet_ntop (AF_INET
, gate
, buf2
, BUFSIZ
),
1257 zlog_info ("route %s/%d ifindex %d type %d doesn't exist in rib",
1258 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1263 route_unlock_node (rn
);
1264 return ZEBRA_ERR_RTNOEXIST
;
1269 rib_delnode (rn
, same
);
1271 /* Process changes. */
1272 rib_process (rn
, same
);
1277 route_unlock_node (rn
);
1280 route_unlock_node (rn
);
1285 /* Install static route into rib. */
1287 static_install_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
1290 struct route_node
*rn
;
1291 struct route_table
*table
;
1294 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1298 /* Lookup existing route */
1299 rn
= route_node_get (table
, p
);
1300 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1301 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1306 /* Same distance static route is there. Update it with new
1308 route_unlock_node (rn
);
1311 case STATIC_IPV4_GATEWAY
:
1312 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
);
1314 case STATIC_IPV4_IFNAME
:
1315 nexthop_ifname_add (rib
, si
->gate
.ifname
);
1317 case STATIC_IPV4_BLACKHOLE
:
1318 nexthop_blackhole_add (rib
);
1321 rib_process (rn
, NULL
);
1325 /* This is new static route. */
1326 rib
= XMALLOC (MTYPE_RIB
, sizeof (struct rib
));
1327 memset (rib
, 0, sizeof (struct rib
));
1329 rib
->type
= ZEBRA_ROUTE_STATIC
;
1330 rib
->distance
= si
->distance
;
1332 rib
->nexthop_num
= 0;
1336 case STATIC_IPV4_GATEWAY
:
1337 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
);
1339 case STATIC_IPV4_IFNAME
:
1340 nexthop_ifname_add (rib
, si
->gate
.ifname
);
1342 case STATIC_IPV4_BLACKHOLE
:
1343 nexthop_blackhole_add (rib
);
1347 /* Save the flags of this static routes (reject, blackhole) */
1348 rib
->flags
= si
->flags
;
1350 /* Link this rib to the tree. */
1351 rib_addnode (rn
, rib
);
1353 /* Process this prefix. */
1354 rib_process (rn
, NULL
);
1359 static_ipv4_nexthop_same (struct nexthop
*nexthop
, struct static_ipv4
*si
)
1361 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1362 && si
->type
== STATIC_IPV4_GATEWAY
1363 && IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &si
->gate
.ipv4
))
1365 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
1366 && si
->type
== STATIC_IPV4_IFNAME
1367 && strcmp (nexthop
->ifname
, si
->gate
.ifname
) == 0)
1369 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
1370 && si
->type
== STATIC_IPV4_BLACKHOLE
)
1375 /* Uninstall static route from RIB. */
1377 static_uninstall_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
1379 struct route_node
*rn
;
1381 struct nexthop
*nexthop
;
1382 struct route_table
*table
;
1385 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1389 /* Lookup existing route with type and distance. */
1390 rn
= route_node_lookup (table
, p
);
1394 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1395 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1400 route_unlock_node (rn
);
1404 /* Lookup nexthop. */
1405 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1406 if (static_ipv4_nexthop_same (nexthop
, si
))
1409 /* Can't find nexthop. */
1412 route_unlock_node (rn
);
1416 /* Check nexthop. */
1417 if (rib
->nexthop_num
== 1)
1419 rib_delnode (rn
, rib
);
1420 rib_process (rn
, rib
);
1422 route_unlock_node (rn
);
1426 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1427 rib_uninstall (rn
, rib
);
1428 nexthop_delete (rib
, nexthop
);
1429 nexthop_free (nexthop
);
1430 rib_process (rn
, rib
);
1434 route_unlock_node (rn
);
1437 /* Add static route into static route configuration. */
1439 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, char *ifname
,
1440 u_char flags
, u_char distance
, u_int32_t vrf_id
)
1443 struct route_node
*rn
;
1444 struct static_ipv4
*si
;
1445 struct static_ipv4
*pp
;
1446 struct static_ipv4
*cp
;
1447 struct static_ipv4
*update
= NULL
;
1448 struct route_table
*stable
;
1451 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
1455 /* Lookup static route prefix. */
1456 rn
= route_node_get (stable
, p
);
1460 type
= STATIC_IPV4_GATEWAY
;
1462 type
= STATIC_IPV4_IFNAME
;
1464 type
= STATIC_IPV4_BLACKHOLE
;
1466 /* Do nothing if there is a same static route. */
1467 for (si
= rn
->info
; si
; si
= si
->next
)
1469 if (type
== si
->type
1470 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
1471 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
1473 if (distance
== si
->distance
)
1475 route_unlock_node (rn
);
1483 /* Distance chaged. */
1485 static_delete_ipv4 (p
, gate
, ifname
, update
->distance
, vrf_id
);
1487 /* Make new static route structure. */
1488 si
= XMALLOC (MTYPE_STATIC_IPV4
, sizeof (struct static_ipv4
));
1489 memset (si
, 0, sizeof (struct static_ipv4
));
1492 si
->distance
= distance
;
1496 si
->gate
.ipv4
= *gate
;
1498 si
->gate
.ifname
= XSTRDUP (0, ifname
);
1500 /* Add new static route information to the tree with sort by
1501 distance value and gateway address. */
1502 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
1504 if (si
->distance
< cp
->distance
)
1506 if (si
->distance
> cp
->distance
)
1508 if (si
->type
== STATIC_IPV4_GATEWAY
&& cp
->type
== STATIC_IPV4_GATEWAY
)
1510 if (ntohl (si
->gate
.ipv4
.s_addr
) < ntohl (cp
->gate
.ipv4
.s_addr
))
1512 if (ntohl (si
->gate
.ipv4
.s_addr
) > ntohl (cp
->gate
.ipv4
.s_addr
))
1517 /* Make linked list. */
1527 /* Install into rib. */
1528 static_install_ipv4 (p
, si
);
1533 /* Delete static route from static route configuration. */
1535 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, char *ifname
,
1536 u_char distance
, u_int32_t vrf_id
)
1539 struct route_node
*rn
;
1540 struct static_ipv4
*si
;
1541 struct route_table
*stable
;
1544 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
1548 /* Lookup static route prefix. */
1549 rn
= route_node_lookup (stable
, p
);
1555 type
= STATIC_IPV4_GATEWAY
;
1557 type
= STATIC_IPV4_IFNAME
;
1559 type
= STATIC_IPV4_BLACKHOLE
;
1561 /* Find same static route is the tree */
1562 for (si
= rn
->info
; si
; si
= si
->next
)
1563 if (type
== si
->type
1564 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
1565 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
1568 /* Can't find static route. */
1571 route_unlock_node (rn
);
1575 /* Install into rib. */
1576 static_uninstall_ipv4 (p
, si
);
1578 /* Unlink static route from linked list. */
1580 si
->prev
->next
= si
->next
;
1582 rn
->info
= si
->next
;
1584 si
->next
->prev
= si
->prev
;
1585 route_unlock_node (rn
);
1587 /* Free static route configuration. */
1589 XFREE (0, si
->gate
.ifname
);
1590 XFREE (MTYPE_STATIC_IPV4
, si
);
1592 route_unlock_node (rn
);
1600 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
1601 struct in6_addr
*gate
, unsigned int ifindex
, int table
)
1603 if (type
== ZEBRA_ROUTE_CONNECT
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)) {
1604 #if defined (MUSICA) || defined (LINUX)
1605 /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
1606 if (p
->prefixlen
== 96)
1611 if (type
== ZEBRA_ROUTE_KERNEL
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)
1612 && p
->prefixlen
== 96 && gate
&& IN6_IS_ADDR_UNSPECIFIED (gate
))
1614 kernel_delete_ipv6_old (p
, gate
, ifindex
, 0, table
);
1621 rib_add_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
1622 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1625 struct rib
*same
= NULL
;
1626 struct route_table
*table
;
1627 struct route_node
*rn
;
1628 struct nexthop
*nexthop
;
1631 u_int32_t metric
= 0;
1634 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
1638 /* Make sure mask is applied. */
1639 apply_mask_ipv6 (p
);
1641 /* Set default distance by route type. */
1642 distance
= route_info
[type
].distance
;
1644 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1647 /* Filter bogus route. */
1648 if (rib_bogus_ipv6 (type
, p
, gate
, ifindex
, 0))
1651 /* Lookup route node.*/
1652 rn
= route_node_get (table
, (struct prefix
*) p
);
1654 /* If same type of route are installed, treat it as a implicit
1656 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1658 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1660 nexthop
= rib
->nexthop
;
1662 if (rib
->type
== type
1663 && nexthop
&& nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1664 && nexthop
->ifindex
== ifindex
)
1670 else if (rib
->type
== type
)
1673 rib_delnode (rn
, same
);
1674 route_unlock_node (rn
);
1679 /* Allocate new rib structure. */
1680 rib
= XMALLOC (MTYPE_RIB
, sizeof (struct rib
));
1681 memset (rib
, 0, sizeof (struct rib
));
1683 rib
->distance
= distance
;
1685 rib
->metric
= metric
;
1686 rib
->table
= vrf_id
;
1687 rib
->nexthop_num
= 0;
1688 rib
->uptime
= time (NULL
);
1690 /* Nexthop settings. */
1694 nexthop_ipv6_ifindex_add (rib
, gate
, ifindex
);
1696 nexthop_ipv6_add (rib
, gate
);
1699 nexthop_ifindex_add (rib
, ifindex
);
1701 /* If this route is kernel route, set FIB flag to the route. */
1702 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1703 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1704 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1706 /* Link new rib to node.*/
1707 rib_addnode (rn
, rib
);
1709 /* Process this route node. */
1710 rib_process (rn
, same
);
1712 /* Free implicit route.*/
1720 rib_delete_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
1721 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1723 struct route_table
*table
;
1724 struct route_node
*rn
;
1726 struct rib
*fib
= NULL
;
1727 struct rib
*same
= NULL
;
1728 struct nexthop
*nexthop
;
1733 apply_mask_ipv6 (p
);
1736 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
1740 /* Lookup route node. */
1741 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1744 if (IS_ZEBRA_DEBUG_KERNEL
)
1747 zlog_info ("route %s/%d via %s ifindex %d doesn't exist in rib",
1748 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
1750 inet_ntop (AF_INET6
, gate
, buf2
, BUFSIZ
),
1753 zlog_info ("route %s/%d ifindex %d doesn't exist in rib",
1754 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
1758 return ZEBRA_ERR_RTNOEXIST
;
1761 /* Lookup same type route. */
1762 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1764 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1767 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1769 nexthop
= rib
->nexthop
;
1771 if (rib
->type
== type
1772 && nexthop
&& nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1773 && nexthop
->ifindex
== ifindex
)
1778 route_unlock_node (rn
);
1779 route_unlock_node (rn
);
1788 if (rib
->type
== type
)
1796 /* If same type of route can't be found and this message is from
1800 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
1803 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1804 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1806 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1810 if (IS_ZEBRA_DEBUG_KERNEL
)
1813 zlog_info ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
1814 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
1816 inet_ntop (AF_INET6
, gate
, buf2
, BUFSIZ
),
1820 zlog_info ("route %s/%d ifindex %d type %d doesn't exist in rib",
1821 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
1826 route_unlock_node (rn
);
1827 return ZEBRA_ERR_RTNOEXIST
;
1832 rib_delnode (rn
, same
);
1834 /* Process changes. */
1835 rib_process (rn
, same
);
1840 route_unlock_node (rn
);
1843 route_unlock_node (rn
);
1848 /* Install static route into rib. */
1850 static_install_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
1853 struct route_table
*table
;
1854 struct route_node
*rn
;
1857 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
1861 /* Lookup existing route */
1862 rn
= route_node_get (table
, p
);
1863 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1864 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1869 /* Same distance static route is there. Update it with new
1871 route_unlock_node (rn
);
1875 case STATIC_IPV6_GATEWAY
:
1876 nexthop_ipv6_add (rib
, &si
->ipv6
);
1878 case STATIC_IPV6_IFNAME
:
1879 nexthop_ifname_add (rib
, si
->ifname
);
1881 case STATIC_IPV6_GATEWAY_IFNAME
:
1882 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
1885 rib_process (rn
, NULL
);
1889 /* This is new static route. */
1890 rib
= XMALLOC (MTYPE_RIB
, sizeof (struct rib
));
1891 memset (rib
, 0, sizeof (struct rib
));
1893 rib
->type
= ZEBRA_ROUTE_STATIC
;
1894 rib
->distance
= si
->distance
;
1896 rib
->nexthop_num
= 0;
1900 case STATIC_IPV6_GATEWAY
:
1901 nexthop_ipv6_add (rib
, &si
->ipv6
);
1903 case STATIC_IPV6_IFNAME
:
1904 nexthop_ifname_add (rib
, si
->ifname
);
1906 case STATIC_IPV6_GATEWAY_IFNAME
:
1907 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
1911 /* Save the flags of this static routes (reject, blackhole) */
1912 rib
->flags
= si
->flags
;
1914 /* Link this rib to the tree. */
1915 rib_addnode (rn
, rib
);
1917 /* Process this prefix. */
1918 rib_process (rn
, NULL
);
1923 static_ipv6_nexthop_same (struct nexthop
*nexthop
, struct static_ipv6
*si
)
1925 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1926 && si
->type
== STATIC_IPV6_GATEWAY
1927 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
))
1929 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
1930 && si
->type
== STATIC_IPV6_IFNAME
1931 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
1933 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
1934 && si
->type
== STATIC_IPV6_GATEWAY_IFNAME
1935 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
)
1936 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
1942 static_uninstall_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
1944 struct route_table
*table
;
1945 struct route_node
*rn
;
1947 struct nexthop
*nexthop
;
1950 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
1954 /* Lookup existing route with type and distance. */
1955 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1959 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1960 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1964 route_unlock_node (rn
);
1968 /* Lookup nexthop. */
1969 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1970 if (static_ipv6_nexthop_same (nexthop
, si
))
1973 /* Can't find nexthop. */
1976 route_unlock_node (rn
);
1980 /* Check nexthop. */
1981 if (rib
->nexthop_num
== 1)
1983 rib_delnode (rn
, rib
);
1984 rib_process (rn
, rib
);
1986 route_unlock_node (rn
);
1990 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1991 rib_uninstall (rn
, rib
);
1992 nexthop_delete (rib
, nexthop
);
1993 nexthop_free (nexthop
);
1994 rib_process (rn
, rib
);
1998 route_unlock_node (rn
);
2001 /* Add static route into static route configuration. */
2003 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2004 char *ifname
, u_char flags
, u_char distance
, u_int32_t vrf_id
)
2006 struct route_node
*rn
;
2007 struct static_ipv6
*si
;
2008 struct static_ipv6
*pp
;
2009 struct static_ipv6
*cp
;
2010 struct route_table
*stable
;
2013 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2017 /* Lookup static route prefix. */
2018 rn
= route_node_get (stable
, p
);
2020 /* Do nothing if there is a same static route. */
2021 for (si
= rn
->info
; si
; si
= si
->next
)
2023 if (distance
== si
->distance
2025 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2026 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2028 route_unlock_node (rn
);
2033 /* Make new static route structure. */
2034 si
= XMALLOC (MTYPE_STATIC_IPV6
, sizeof (struct static_ipv6
));
2035 memset (si
, 0, sizeof (struct static_ipv6
));
2038 si
->distance
= distance
;
2043 case STATIC_IPV6_GATEWAY
:
2046 case STATIC_IPV6_IFNAME
:
2047 si
->ifname
= XSTRDUP (0, ifname
);
2049 case STATIC_IPV6_GATEWAY_IFNAME
:
2051 si
->ifname
= XSTRDUP (0, ifname
);
2055 /* Add new static route information to the tree with sort by
2056 distance value and gateway address. */
2057 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2059 if (si
->distance
< cp
->distance
)
2061 if (si
->distance
> cp
->distance
)
2065 /* Make linked list. */
2075 /* Install into rib. */
2076 static_install_ipv6 (p
, si
);
2081 /* Delete static route from static route configuration. */
2083 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2084 char *ifname
, u_char distance
, u_int32_t vrf_id
)
2086 struct route_node
*rn
;
2087 struct static_ipv6
*si
;
2088 struct route_table
*stable
;
2091 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2095 /* Lookup static route prefix. */
2096 rn
= route_node_lookup (stable
, p
);
2100 /* Find same static route is the tree */
2101 for (si
= rn
->info
; si
; si
= si
->next
)
2102 if (distance
== si
->distance
2104 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2105 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2108 /* Can't find static route. */
2111 route_unlock_node (rn
);
2115 /* Install into rib. */
2116 static_uninstall_ipv6 (p
, si
);
2118 /* Unlink static route from linked list. */
2120 si
->prev
->next
= si
->next
;
2122 rn
->info
= si
->next
;
2124 si
->next
->prev
= si
->prev
;
2126 /* Free static route configuration. */
2128 XFREE (0, si
->ifname
);
2129 XFREE (MTYPE_STATIC_IPV6
, si
);
2133 #endif /* HAVE_IPV6 */
2135 /* RIB update function. */
2139 struct route_node
*rn
;
2140 struct route_table
*table
;
2142 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2144 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2145 rib_process (rn
, NULL
);
2147 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2149 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2150 rib_process (rn
, NULL
);
2153 /* Interface goes up. */
2155 rib_if_up (struct interface
*ifp
)
2160 /* Interface goes down. */
2162 rib_if_down (struct interface
*ifp
)
2167 /* Remove all routes which comes from non main table. */
2169 rib_weed_table (struct route_table
*table
)
2171 struct route_node
*rn
;
2176 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2177 for (rib
= rn
->info
; rib
; rib
= next
)
2181 if (rib
->table
!= zebrad
.rtm_table_default
&&
2182 rib
->table
!= RT_TABLE_MAIN
)
2184 rib_delnode (rn
, rib
);
2186 route_unlock_node (rn
);
2191 /* Delete all routes from non main table. */
2195 rib_weed_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2196 rib_weed_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2199 /* Delete self installed routes after zebra is relaunched. */
2201 rib_sweep_table (struct route_table
*table
)
2203 struct route_node
*rn
;
2209 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2210 for (rib
= rn
->info
; rib
; rib
= next
)
2214 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
2215 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
2217 ret
= rib_uninstall_kernel (rn
, rib
);
2220 rib_delnode (rn
, rib
);
2222 route_unlock_node (rn
);
2228 /* Sweep all RIB tables. */
2232 rib_sweep_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2233 rib_sweep_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2236 /* Close RIB and clean up kernel routes. */
2238 rib_close_table (struct route_table
*table
)
2240 struct route_node
*rn
;
2244 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2245 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2246 if (! RIB_SYSTEM_ROUTE (rib
)
2247 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2248 rib_uninstall_kernel (rn
, rib
);
2251 /* Close all RIB tables. */
2255 rib_close_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2256 rib_close_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2259 /* Routing information base initialize. */
2263 /* VRF initialization. */