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"
36 #include "zebra/rib.h"
38 #include "zebra/zserv.h"
39 #include "zebra/redistribute.h"
40 #include "zebra/debug.h"
42 /* Default rtm_table for all clients */
43 extern struct zebra_t zebrad
;
45 /* Each route type's string and default distance value. */
52 {ZEBRA_ROUTE_SYSTEM
, 0},
53 {ZEBRA_ROUTE_KERNEL
, 0},
54 {ZEBRA_ROUTE_CONNECT
, 0},
55 {ZEBRA_ROUTE_STATIC
, 1},
56 {ZEBRA_ROUTE_RIP
, 120},
57 {ZEBRA_ROUTE_RIPNG
, 120},
58 {ZEBRA_ROUTE_OSPF
, 110},
59 {ZEBRA_ROUTE_OSPF6
, 110},
60 {ZEBRA_ROUTE_ISIS
, 115},
61 {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */}
64 struct zebra_queue_node_t
66 struct route_node
*node
;
70 /* Vector for routing table. */
73 /* Allocate new VRF. */
75 vrf_alloc (const char *name
)
79 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
83 vrf
->name
= XSTRDUP (MTYPE_VRF_NAME
, name
);
85 /* Allocate routing table and static table. */
86 vrf
->table
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
87 vrf
->table
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
88 vrf
->stable
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
89 vrf
->stable
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
96 vrf_free (struct vrf
*vrf
)
99 XFREE (MTYPE_VRF_NAME
, vrf
->name
);
100 XFREE (MTYPE_VRF
, vrf
);
103 /* Lookup VRF by identifier. */
105 vrf_lookup (u_int32_t id
)
107 return vector_lookup (vrf_vector
, id
);
110 /* Lookup VRF by name. */
112 vrf_lookup_by_name (char *name
)
117 for (i
= 0; i
< vector_active (vrf_vector
); i
++)
118 if ((vrf
= vector_slot (vrf_vector
, i
)) != NULL
)
119 if (vrf
->name
&& name
&& strcmp (vrf
->name
, name
) == 0)
124 /* Initialize VRF. */
128 struct vrf
*default_table
;
130 /* Allocate VRF vector. */
131 vrf_vector
= vector_init (1);
133 /* Allocate default main table. */
134 default_table
= vrf_alloc ("Default-IP-Routing-Table");
136 /* Default table index must be 0. */
137 vector_set_index (vrf_vector
, 0, default_table
);
140 /* Lookup route table. */
142 vrf_table (afi_t afi
, safi_t safi
, u_int32_t id
)
146 vrf
= vrf_lookup (id
);
150 return vrf
->table
[afi
][safi
];
153 /* Lookup static route table. */
155 vrf_static_table (afi_t afi
, safi_t safi
, u_int32_t id
)
159 vrf
= vrf_lookup (id
);
163 return vrf
->stable
[afi
][safi
];
166 /* Add nexthop to the end of the list. */
168 nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
170 struct nexthop
*last
;
172 for (last
= rib
->nexthop
; last
&& last
->next
; last
= last
->next
)
175 last
->next
= nexthop
;
177 rib
->nexthop
= nexthop
;
178 nexthop
->prev
= last
;
183 /* Delete specified nexthop from the list. */
185 nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
188 nexthop
->next
->prev
= nexthop
->prev
;
190 nexthop
->prev
->next
= nexthop
->next
;
192 rib
->nexthop
= nexthop
->next
;
198 nexthop_free (struct nexthop
*nexthop
)
201 XFREE (0, nexthop
->ifname
);
202 XFREE (MTYPE_NEXTHOP
, nexthop
);
206 nexthop_ifindex_add (struct rib
*rib
, unsigned int ifindex
)
208 struct nexthop
*nexthop
;
210 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
211 memset (nexthop
, 0, sizeof (struct nexthop
));
212 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
213 nexthop
->ifindex
= ifindex
;
215 nexthop_add (rib
, nexthop
);
221 nexthop_ifname_add (struct rib
*rib
, char *ifname
)
223 struct nexthop
*nexthop
;
225 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
226 memset (nexthop
, 0, sizeof (struct nexthop
));
227 nexthop
->type
= NEXTHOP_TYPE_IFNAME
;
228 nexthop
->ifname
= XSTRDUP (0, ifname
);
230 nexthop_add (rib
, nexthop
);
236 nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
)
238 struct nexthop
*nexthop
;
240 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
241 memset (nexthop
, 0, sizeof (struct nexthop
));
242 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
243 nexthop
->gate
.ipv4
= *ipv4
;
245 nexthop_add (rib
, nexthop
);
250 static struct nexthop
*
251 nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
252 unsigned int ifindex
)
254 struct nexthop
*nexthop
;
256 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
257 memset (nexthop
, 0, sizeof (struct nexthop
));
258 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
259 nexthop
->gate
.ipv4
= *ipv4
;
260 nexthop
->ifindex
= ifindex
;
262 nexthop_add (rib
, nexthop
);
269 nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
271 struct nexthop
*nexthop
;
273 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
274 memset (nexthop
, 0, sizeof (struct nexthop
));
275 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
276 nexthop
->gate
.ipv6
= *ipv6
;
278 nexthop_add (rib
, nexthop
);
283 static struct nexthop
*
284 nexthop_ipv6_ifname_add (struct rib
*rib
, struct in6_addr
*ipv6
,
287 struct nexthop
*nexthop
;
289 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
290 memset (nexthop
, 0, sizeof (struct nexthop
));
291 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFNAME
;
292 nexthop
->gate
.ipv6
= *ipv6
;
293 nexthop
->ifname
= XSTRDUP (0, ifname
);
295 nexthop_add (rib
, nexthop
);
300 static struct nexthop
*
301 nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
302 unsigned int ifindex
)
304 struct nexthop
*nexthop
;
306 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
307 memset (nexthop
, 0, sizeof (struct nexthop
));
308 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
309 nexthop
->gate
.ipv6
= *ipv6
;
310 nexthop
->ifindex
= ifindex
;
312 nexthop_add (rib
, nexthop
);
316 #endif /* HAVE_IPV6 */
319 nexthop_blackhole_add (struct rib
*rib
)
321 struct nexthop
*nexthop
;
323 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
324 memset (nexthop
, 0, sizeof (struct nexthop
));
325 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
326 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
328 nexthop_add (rib
, nexthop
);
333 /* If force flag is not set, do not modify falgs at all for uninstall
334 the route from FIB. */
336 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
337 struct route_node
*top
)
339 struct prefix_ipv4 p
;
340 struct route_table
*table
;
341 struct route_node
*rn
;
343 struct nexthop
*newhop
;
345 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
346 nexthop
->ifindex
= 0;
349 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
351 /* Make lookup prefix. */
352 memset (&p
, 0, sizeof (struct prefix_ipv4
));
354 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
355 p
.prefix
= nexthop
->gate
.ipv4
;
358 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
362 rn
= route_node_match (table
, (struct prefix
*) &p
);
365 route_unlock_node (rn
);
367 /* If lookup self prefix return immidiately. */
371 /* Pick up selected route. */
372 for (match
= rn
->info
; match
; match
= match
->next
)
373 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
376 /* If there is no selected route or matched route is EGP, go up
379 || match
->type
== ZEBRA_ROUTE_BGP
)
383 } while (rn
&& rn
->info
== NULL
);
385 route_lock_node (rn
);
389 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
391 /* Directly point connected route. */
392 newhop
= match
->nexthop
;
393 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
394 nexthop
->ifindex
= newhop
->ifindex
;
398 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
400 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
401 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
402 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
406 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
407 nexthop
->rtype
= newhop
->type
;
408 if (newhop
->type
== NEXTHOP_TYPE_IPV4
||
409 newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
410 nexthop
->rgate
.ipv4
= newhop
->gate
.ipv4
;
411 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
412 || newhop
->type
== NEXTHOP_TYPE_IFNAME
413 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
414 nexthop
->rifindex
= newhop
->ifindex
;
430 /* If force flag is not set, do not modify falgs at all for uninstall
431 the route from FIB. */
433 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
434 struct route_node
*top
)
436 struct prefix_ipv6 p
;
437 struct route_table
*table
;
438 struct route_node
*rn
;
440 struct nexthop
*newhop
;
442 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
443 nexthop
->ifindex
= 0;
446 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
448 /* Make lookup prefix. */
449 memset (&p
, 0, sizeof (struct prefix_ipv6
));
451 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
452 p
.prefix
= nexthop
->gate
.ipv6
;
455 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
459 rn
= route_node_match (table
, (struct prefix
*) &p
);
462 route_unlock_node (rn
);
464 /* If lookup self prefix return immidiately. */
468 /* Pick up selected route. */
469 for (match
= rn
->info
; match
; match
= match
->next
)
470 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
473 /* If there is no selected route or matched route is EGP, go up
476 || match
->type
== ZEBRA_ROUTE_BGP
)
480 } while (rn
&& rn
->info
== NULL
);
482 route_lock_node (rn
);
486 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
488 /* Directly point connected route. */
489 newhop
= match
->nexthop
;
491 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
492 nexthop
->ifindex
= newhop
->ifindex
;
496 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
498 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
499 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
500 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
504 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
505 nexthop
->rtype
= newhop
->type
;
506 if (newhop
->type
== NEXTHOP_TYPE_IPV6
507 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
508 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
509 nexthop
->rgate
.ipv6
= newhop
->gate
.ipv6
;
510 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
511 || newhop
->type
== NEXTHOP_TYPE_IFNAME
512 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
513 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
514 nexthop
->rifindex
= newhop
->ifindex
;
528 #endif /* HAVE_IPV6 */
531 rib_match_ipv4 (struct in_addr addr
)
533 struct prefix_ipv4 p
;
534 struct route_table
*table
;
535 struct route_node
*rn
;
537 struct nexthop
*newhop
;
540 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
544 memset (&p
, 0, sizeof (struct prefix_ipv4
));
546 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
549 rn
= route_node_match (table
, (struct prefix
*) &p
);
553 route_unlock_node (rn
);
555 /* Pick up selected route. */
556 for (match
= rn
->info
; match
; match
= match
->next
)
557 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
560 /* If there is no selected route or matched route is EGP, go up
563 || match
->type
== ZEBRA_ROUTE_BGP
)
567 } while (rn
&& rn
->info
== NULL
);
569 route_lock_node (rn
);
573 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
574 /* Directly point connected route. */
578 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
579 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
589 rib_lookup_ipv4 (struct prefix_ipv4
*p
)
591 struct route_table
*table
;
592 struct route_node
*rn
;
594 struct nexthop
*nexthop
;
597 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
601 rn
= route_node_lookup (table
, (struct prefix
*) p
);
603 /* No route for this prefix. */
608 route_unlock_node (rn
);
610 /* Pick up selected route. */
611 for (match
= rn
->info
; match
; match
= match
->next
)
612 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
615 if (! match
|| match
->type
== ZEBRA_ROUTE_BGP
)
618 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
621 for (nexthop
= match
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
622 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
630 rib_match_ipv6 (struct in6_addr
*addr
)
632 struct prefix_ipv6 p
;
633 struct route_table
*table
;
634 struct route_node
*rn
;
636 struct nexthop
*newhop
;
639 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
643 memset (&p
, 0, sizeof (struct prefix_ipv6
));
645 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
646 IPV6_ADDR_COPY (&p
.prefix
, addr
);
648 rn
= route_node_match (table
, (struct prefix
*) &p
);
652 route_unlock_node (rn
);
654 /* Pick up selected route. */
655 for (match
= rn
->info
; match
; match
= match
->next
)
656 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
659 /* If there is no selected route or matched route is EGP, go up
662 || match
->type
== ZEBRA_ROUTE_BGP
)
666 } while (rn
&& rn
->info
== NULL
);
668 route_lock_node (rn
);
672 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
673 /* Directly point connected route. */
677 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
678 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
686 #endif /* HAVE_IPV6 */
689 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
690 struct nexthop
*nexthop
, int set
)
692 struct interface
*ifp
;
694 switch (nexthop
->type
)
696 case NEXTHOP_TYPE_IFINDEX
:
697 ifp
= if_lookup_by_index (nexthop
->ifindex
);
698 if (ifp
&& if_is_up (ifp
))
699 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
701 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
703 case NEXTHOP_TYPE_IFNAME
:
704 case NEXTHOP_TYPE_IPV6_IFNAME
:
705 ifp
= if_lookup_by_name (nexthop
->ifname
);
706 if (ifp
&& if_is_up (ifp
))
709 nexthop
->ifindex
= ifp
->ifindex
;
710 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
715 nexthop
->ifindex
= 0;
716 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
719 case NEXTHOP_TYPE_IPV4
:
720 case NEXTHOP_TYPE_IPV4_IFINDEX
:
721 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
722 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
724 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
727 case NEXTHOP_TYPE_IPV6
:
728 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
729 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
731 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
733 case NEXTHOP_TYPE_IPV6_IFINDEX
:
734 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
736 ifp
= if_lookup_by_index (nexthop
->ifindex
);
737 if (ifp
&& if_is_up (ifp
))
738 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
740 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
744 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
745 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
747 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
750 #endif /* HAVE_IPV6 */
751 case NEXTHOP_TYPE_BLACKHOLE
:
752 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
757 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
761 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
763 struct nexthop
*nexthop
;
766 rib
->nexthop_active_num
= 0;
767 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
769 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
771 active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
773 nexthop_active_check (rn
, rib
, nexthop
, set
);
774 if ((MULTIPATH_NUM
== 0 || rib
->nexthop_active_num
< MULTIPATH_NUM
)
775 && active
!= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
776 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
778 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
779 rib
->nexthop_active_num
++;
781 return rib
->nexthop_active_num
;
785 #define RIB_SYSTEM_ROUTE(R) \
786 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
789 rib_lock (struct rib
*rib
)
791 assert (rib
->lock
>= 0);
798 rib_unlock (struct rib
*rib
)
800 struct nexthop
*nexthop
;
801 struct nexthop
*next
;
803 assert (rib
->lock
> 0);
808 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= next
)
810 next
= nexthop
->next
;
811 nexthop_free (nexthop
);
813 XFREE (MTYPE_RIB
, rib
);
820 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
)
823 struct nexthop
*nexthop
;
825 switch (PREFIX_FAMILY (&rn
->p
))
828 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
832 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
834 #endif /* HAVE_IPV6 */
839 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
840 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
844 /* Uninstall the route from kernel. */
846 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
849 struct nexthop
*nexthop
;
851 switch (PREFIX_FAMILY (&rn
->p
))
854 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
858 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
860 #endif /* HAVE_IPV6 */
863 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
864 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
869 /* Uninstall the route from kernel. */
871 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
873 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
875 redistribute_delete (&rn
->p
, rib
);
876 if (! RIB_SYSTEM_ROUTE (rib
))
877 rib_uninstall_kernel (rn
, rib
);
878 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
882 /* Core function for processing routing information base. */
883 static wq_item_status
884 rib_process (struct zebra_queue_node_t
*qnode
)
888 struct rib
*fib
= NULL
;
889 struct rib
*select
= NULL
;
890 struct rib
*del
= qnode
->del
;
891 struct route_node
*rn
= qnode
->node
;
893 struct nexthop
*nexthop
= NULL
;
897 /* possibly should lock and unlock rib on each iteration. however, for
898 * now, we assume called functions are synchronous and dont delete RIBs
899 * (as the work-queue deconstructor for this function is supposed to be
900 * the canonical 'delete' path for RIBs). Further if called functions
901 * below were to made asynchronous they should themselves acquire any
902 * locks/refcounts as needed and not depend on this caller to do it for
905 for (rib
= rn
->info
; rib
; rib
= next
)
909 /* Currently installed rib. */
910 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
913 /* Skip unreachable nexthop. */
914 if (! nexthop_active_update (rn
, rib
, 0))
917 /* Infinit distance. */
918 if (rib
->distance
== DISTANCE_INFINITY
)
921 /* Newly selected rib. */
922 if (! select
|| rib
->distance
< select
->distance
923 || rib
->type
== ZEBRA_ROUTE_CONNECT
)
927 /* Deleted route check. */
928 if (del
&& CHECK_FLAG (del
->flags
, ZEBRA_FLAG_SELECTED
))
931 /* We possibly should lock fib and select here However, all functions
932 * below are 'inline' and not asynchronous And if any were to be
933 * converted, they should manage references themselves really.. See
934 * previous comment above.
937 /* Same route is selected. */
938 if (select
&& select
== fib
)
940 if (CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
942 redistribute_delete (&rn
->p
, select
);
943 if (! RIB_SYSTEM_ROUTE (select
))
944 rib_uninstall_kernel (rn
, select
);
946 /* Set real nexthop. */
947 nexthop_active_update (rn
, select
, 1);
949 if (! RIB_SYSTEM_ROUTE (select
))
950 rib_install_kernel (rn
, select
);
951 redistribute_add (&rn
->p
, select
);
953 else if (! RIB_SYSTEM_ROUTE (select
))
955 /* Housekeeping code to deal with
956 race conditions in kernel with linux
957 netlink reporting interface up before IPv4 or IPv6 protocol
958 is ready to add routes.
959 This makes sure the routes are IN the kernel.
962 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
964 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
968 rib_install_kernel (rn
, select
);
973 /* Uninstall old rib from forwarding table. */
976 redistribute_delete (&rn
->p
, fib
);
977 if (! RIB_SYSTEM_ROUTE (fib
))
978 rib_uninstall_kernel (rn
, fib
);
979 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
981 /* Set real nexthop. */
982 nexthop_active_update (rn
, fib
, 1);
985 /* Install new rib into forwarding table. */
988 /* Set real nexthop. */
989 nexthop_active_update (rn
, select
, 1);
991 if (! RIB_SYSTEM_ROUTE (select
))
992 rib_install_kernel (rn
, select
);
993 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
994 redistribute_add (&rn
->p
, select
);
1001 /* Add work queue item to work queue and schedule processing */
1003 rib_queue_add_qnode (struct zebra_t
*zebra
, struct zebra_queue_node_t
*qnode
)
1005 route_lock_node (qnode
->node
);
1007 if (IS_ZEBRA_DEBUG_EVENT
)
1008 zlog_info ("rib_queue_add_qnode: work queue added");
1010 assert (zebra
&& qnode
&& qnode
->node
);
1013 rib_lock (qnode
->del
);
1015 if (zebra
->ribq
== NULL
)
1017 zlog_err ("rib_queue_add_qnode: ribq work_queue does not exist!");
1018 route_unlock_node (qnode
->node
);
1022 work_queue_add (zebra
->ribq
, qnode
);
1027 /* Add route node and rib to work queue and schedule processing */
1029 rib_queue_add (struct zebra_t
*zebra
, struct route_node
*rn
, struct rib
*del
)
1031 struct zebra_queue_node_t
*qnode
;
1033 assert (zebra
&& rn
);
1035 qnode
= (struct zebra_queue_node_t
*)
1036 XCALLOC (MTYPE_RIB_QUEUE
, sizeof (struct zebra_queue_node_t
));
1040 zlog_err ("rib_queue_add: failed to allocate queue node memory, %s",
1048 rib_queue_add_qnode (zebra
, qnode
);
1053 /* free zebra_queue_node_t */
1055 rib_queue_qnode_del (struct zebra_queue_node_t
*qnode
)
1057 route_unlock_node (qnode
->node
);
1060 rib_unlock (qnode
->del
);
1062 XFREE (MTYPE_RIB_QUEUE
, qnode
);
1065 /* initialise zebra rib work queue */
1067 rib_queue_init (struct zebra_t
*zebra
)
1071 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1072 "zebra_rib_work_queue")))
1074 zlog_err ("rib_queue_init: could not initialise work queue!");
1078 /* fill in the work queue spec */
1079 zebra
->ribq
->spec
.workfunc
= (wq_item_status (*) (void *))&rib_process
;
1080 zebra
->ribq
->spec
.errorfunc
= NULL
;
1081 zebra
->ribq
->spec
.del_item_data
= (void (*) (void *)) &rib_queue_qnode_del
;
1082 /* XXX: TODO: These should be runtime configurable via vty */
1083 zebra
->ribq
->spec
.max_retries
= 3;
1084 zebra
->ribq
->spec
.hold
= 500;
1085 zebra
->ribq
->spec
.delay
= 10;
1090 /* Add RIB to head of the route node. */
1092 rib_addnode (struct route_node
*rn
, struct rib
*rib
)
1099 route_lock_node (rn
);
1109 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
1114 rib
->next
->prev
= rib
->prev
;
1116 rib
->prev
->next
= rib
->next
;
1118 rn
->info
= rib
->next
;
1121 route_unlock_node (rn
);
1125 rib_add_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1126 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
,
1127 u_int32_t metric
, u_char distance
)
1130 struct rib
*same
= NULL
;
1131 struct route_table
*table
;
1132 struct route_node
*rn
;
1133 struct nexthop
*nexthop
;
1136 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1140 /* Make it sure prefixlen is applied to the prefix. */
1141 apply_mask_ipv4 (p
);
1143 /* Set default distance by route type. */
1146 distance
= route_info
[type
].distance
;
1148 /* iBGP distance is 200. */
1149 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1153 /* Lookup route node.*/
1154 rn
= route_node_get (table
, (struct prefix
*) p
);
1156 /* If same type of route are installed, treat it as a implicit
1158 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1160 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1162 nexthop
= rib
->nexthop
;
1164 /* Duplicate connected route comes in. */
1165 if (rib
->type
== type
1166 && nexthop
&& nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1167 && nexthop
->ifindex
== ifindex
)
1173 else if (rib
->type
== type
)
1180 /* Allocate new rib structure. */
1181 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1183 rib
->distance
= distance
;
1185 rib
->metric
= metric
;
1186 rib
->table
= vrf_id
;
1187 rib
->nexthop_num
= 0;
1188 rib
->uptime
= time (NULL
);
1190 /* Nexthop settings. */
1194 nexthop_ipv4_ifindex_add (rib
, gate
, ifindex
);
1196 nexthop_ipv4_add (rib
, gate
);
1199 nexthop_ifindex_add (rib
, ifindex
);
1201 /* If this route is kernel route, set FIB flag to the route. */
1202 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1203 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1204 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1206 /* Link new rib to node.*/
1207 rib_addnode (rn
, rib
);
1209 /* Process this route node. */
1210 rib_queue_add (&zebrad
, rn
, same
);
1212 /* Free implicit route.*/
1214 rib_delnode (rn
, same
);
1216 route_unlock_node (rn
);
1221 rib_add_ipv4_multipath (struct prefix_ipv4
*p
, struct rib
*rib
)
1223 struct route_table
*table
;
1224 struct route_node
*rn
;
1226 struct nexthop
*nexthop
;
1229 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1232 /* Make it sure prefixlen is applied to the prefix. */
1233 apply_mask_ipv4 (p
);
1235 /* Set default distance by route type. */
1236 if (rib
->distance
== 0)
1238 rib
->distance
= route_info
[rib
->type
].distance
;
1240 /* iBGP distance is 200. */
1241 if (rib
->type
== ZEBRA_ROUTE_BGP
1242 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
1243 rib
->distance
= 200;
1246 /* Lookup route node.*/
1247 rn
= route_node_get (table
, (struct prefix
*) p
);
1249 /* If same type of route are installed, treat it as a implicit
1251 for (same
= rn
->info
; same
; same
= same
->next
)
1253 if (same
->type
== rib
->type
&& same
->table
== rib
->table
1254 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
1258 /* If this route is kernel route, set FIB flag to the route. */
1259 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
1260 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1261 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1263 /* Link new rib to node.*/
1264 rib_addnode (rn
, rib
);
1266 /* Process this route node. */
1267 rib_queue_add (&zebrad
, rn
, same
);
1269 /* Free implicit route.*/
1271 rib_delnode (rn
, same
);
1273 route_unlock_node (rn
);
1278 rib_delete_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1279 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1281 struct route_table
*table
;
1282 struct route_node
*rn
;
1284 struct rib
*fib
= NULL
;
1285 struct rib
*same
= NULL
;
1286 struct nexthop
*nexthop
;
1291 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1296 apply_mask_ipv4 (p
);
1298 if (IS_ZEBRA_DEBUG_KERNEL
&& gate
)
1299 zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
1300 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1305 /* Lookup route node. */
1306 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1309 if (IS_ZEBRA_DEBUG_KERNEL
)
1312 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
1313 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1315 inet_ntop (AF_INET
, gate
, buf2
, BUFSIZ
),
1318 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
1319 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1323 return ZEBRA_ERR_RTNOEXIST
;
1326 /* Lookup same type route. */
1327 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1329 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1332 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1334 nexthop
= rib
->nexthop
;
1336 if (rib
->type
== type
1337 && nexthop
&& nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1338 && nexthop
->ifindex
== ifindex
)
1343 route_unlock_node (rn
);
1344 route_unlock_node (rn
);
1353 nexthop
= rib
->nexthop
;
1355 /* Make sure that the route found has the same gateway. */
1356 if (rib
->type
== type
1358 (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
1359 IPV4_ADDR_SAME (&nexthop
->rgate
.ipv4
, gate
)) )
1367 if (rib
->type
== type
)
1375 /* If same type of route can't be found and this message is from
1379 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
1382 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1383 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1385 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1389 if (IS_ZEBRA_DEBUG_KERNEL
)
1392 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
1393 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1395 inet_ntop (AF_INET
, gate
, buf2
, BUFSIZ
),
1399 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
1400 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1405 route_unlock_node (rn
);
1406 return ZEBRA_ERR_RTNOEXIST
;
1410 /* Process changes. */
1411 rib_queue_add (&zebrad
, rn
, same
);
1414 rib_delnode (rn
, same
);
1416 route_unlock_node (rn
);
1420 /* Install static route into rib. */
1422 static_install_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
1425 struct route_node
*rn
;
1426 struct route_table
*table
;
1429 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1433 /* Lookup existing route */
1434 rn
= route_node_get (table
, p
);
1435 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1436 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1441 /* Same distance static route is there. Update it with new
1443 route_unlock_node (rn
);
1446 case STATIC_IPV4_GATEWAY
:
1447 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
);
1449 case STATIC_IPV4_IFNAME
:
1450 nexthop_ifname_add (rib
, si
->gate
.ifname
);
1452 case STATIC_IPV4_BLACKHOLE
:
1453 nexthop_blackhole_add (rib
);
1456 rib_queue_add (&zebrad
, rn
, NULL
);
1460 /* This is new static route. */
1461 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1463 rib
->type
= ZEBRA_ROUTE_STATIC
;
1464 rib
->distance
= si
->distance
;
1466 rib
->nexthop_num
= 0;
1470 case STATIC_IPV4_GATEWAY
:
1471 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
);
1473 case STATIC_IPV4_IFNAME
:
1474 nexthop_ifname_add (rib
, si
->gate
.ifname
);
1476 case STATIC_IPV4_BLACKHOLE
:
1477 nexthop_blackhole_add (rib
);
1481 /* Save the flags of this static routes (reject, blackhole) */
1482 rib
->flags
= si
->flags
;
1484 /* Link this rib to the tree. */
1485 rib_addnode (rn
, rib
);
1487 /* Process this prefix. */
1488 rib_queue_add (&zebrad
, rn
, NULL
);
1493 static_ipv4_nexthop_same (struct nexthop
*nexthop
, struct static_ipv4
*si
)
1495 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1496 && si
->type
== STATIC_IPV4_GATEWAY
1497 && IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &si
->gate
.ipv4
))
1499 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
1500 && si
->type
== STATIC_IPV4_IFNAME
1501 && strcmp (nexthop
->ifname
, si
->gate
.ifname
) == 0)
1503 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
1504 && si
->type
== STATIC_IPV4_BLACKHOLE
)
1509 /* Uninstall static route from RIB. */
1511 static_uninstall_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
1513 struct route_node
*rn
;
1515 struct nexthop
*nexthop
;
1516 struct route_table
*table
;
1519 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1523 /* Lookup existing route with type and distance. */
1524 rn
= route_node_lookup (table
, p
);
1528 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1529 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1534 route_unlock_node (rn
);
1538 /* Lookup nexthop. */
1539 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1540 if (static_ipv4_nexthop_same (nexthop
, si
))
1543 /* Can't find nexthop. */
1546 route_unlock_node (rn
);
1550 /* Check nexthop. */
1551 if (rib
->nexthop_num
== 1)
1553 rib_queue_add (&zebrad
, rn
, rib
);
1554 rib_delnode (rn
, rib
);
1558 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1559 rib_uninstall (rn
, rib
);
1560 rib_queue_add (&zebrad
, rn
, rib
);
1563 route_unlock_node (rn
);
1566 /* Add static route into static route configuration. */
1568 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
1569 u_char flags
, u_char distance
, u_int32_t vrf_id
)
1572 struct route_node
*rn
;
1573 struct static_ipv4
*si
;
1574 struct static_ipv4
*pp
;
1575 struct static_ipv4
*cp
;
1576 struct static_ipv4
*update
= NULL
;
1577 struct route_table
*stable
;
1580 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
1584 /* Lookup static route prefix. */
1585 rn
= route_node_get (stable
, p
);
1589 type
= STATIC_IPV4_GATEWAY
;
1591 type
= STATIC_IPV4_IFNAME
;
1593 type
= STATIC_IPV4_BLACKHOLE
;
1595 /* Do nothing if there is a same static route. */
1596 for (si
= rn
->info
; si
; si
= si
->next
)
1598 if (type
== si
->type
1599 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
1600 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
1602 if (distance
== si
->distance
)
1604 route_unlock_node (rn
);
1612 /* Distance chaged. */
1614 static_delete_ipv4 (p
, gate
, ifname
, update
->distance
, vrf_id
);
1616 /* Make new static route structure. */
1617 si
= XMALLOC (MTYPE_STATIC_IPV4
, sizeof (struct static_ipv4
));
1618 memset (si
, 0, sizeof (struct static_ipv4
));
1621 si
->distance
= distance
;
1625 si
->gate
.ipv4
= *gate
;
1627 si
->gate
.ifname
= XSTRDUP (0, ifname
);
1629 /* Add new static route information to the tree with sort by
1630 distance value and gateway address. */
1631 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
1633 if (si
->distance
< cp
->distance
)
1635 if (si
->distance
> cp
->distance
)
1637 if (si
->type
== STATIC_IPV4_GATEWAY
&& cp
->type
== STATIC_IPV4_GATEWAY
)
1639 if (ntohl (si
->gate
.ipv4
.s_addr
) < ntohl (cp
->gate
.ipv4
.s_addr
))
1641 if (ntohl (si
->gate
.ipv4
.s_addr
) > ntohl (cp
->gate
.ipv4
.s_addr
))
1646 /* Make linked list. */
1656 /* Install into rib. */
1657 static_install_ipv4 (p
, si
);
1662 /* Delete static route from static route configuration. */
1664 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
1665 u_char distance
, u_int32_t vrf_id
)
1668 struct route_node
*rn
;
1669 struct static_ipv4
*si
;
1670 struct route_table
*stable
;
1673 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
1677 /* Lookup static route prefix. */
1678 rn
= route_node_lookup (stable
, p
);
1684 type
= STATIC_IPV4_GATEWAY
;
1686 type
= STATIC_IPV4_IFNAME
;
1688 type
= STATIC_IPV4_BLACKHOLE
;
1690 /* Find same static route is the tree */
1691 for (si
= rn
->info
; si
; si
= si
->next
)
1692 if (type
== si
->type
1693 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
1694 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
1697 /* Can't find static route. */
1700 route_unlock_node (rn
);
1704 /* Install into rib. */
1705 static_uninstall_ipv4 (p
, si
);
1707 /* Unlink static route from linked list. */
1709 si
->prev
->next
= si
->next
;
1711 rn
->info
= si
->next
;
1713 si
->next
->prev
= si
->prev
;
1714 route_unlock_node (rn
);
1716 /* Free static route configuration. */
1718 XFREE (0, si
->gate
.ifname
);
1719 XFREE (MTYPE_STATIC_IPV4
, si
);
1721 route_unlock_node (rn
);
1729 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
1730 struct in6_addr
*gate
, unsigned int ifindex
, int table
)
1732 if (type
== ZEBRA_ROUTE_CONNECT
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)) {
1733 #if defined (MUSICA) || defined (LINUX)
1734 /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
1735 if (p
->prefixlen
== 96)
1740 if (type
== ZEBRA_ROUTE_KERNEL
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)
1741 && p
->prefixlen
== 96 && gate
&& IN6_IS_ADDR_UNSPECIFIED (gate
))
1743 kernel_delete_ipv6_old (p
, gate
, ifindex
, 0, table
);
1750 rib_add_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
1751 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1754 struct rib
*same
= NULL
;
1755 struct route_table
*table
;
1756 struct route_node
*rn
;
1757 struct nexthop
*nexthop
;
1760 u_int32_t metric
= 0;
1763 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
1767 /* Make sure mask is applied. */
1768 apply_mask_ipv6 (p
);
1770 /* Set default distance by route type. */
1771 distance
= route_info
[type
].distance
;
1773 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1776 /* Filter bogus route. */
1777 if (rib_bogus_ipv6 (type
, p
, gate
, ifindex
, 0))
1780 /* Lookup route node.*/
1781 rn
= route_node_get (table
, (struct prefix
*) p
);
1783 /* If same type of route are installed, treat it as a implicit
1785 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1787 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1789 nexthop
= rib
->nexthop
;
1791 if (rib
->type
== type
1792 && nexthop
&& nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1793 && nexthop
->ifindex
== ifindex
)
1799 else if (rib
->type
== type
)
1806 /* Allocate new rib structure. */
1807 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1810 rib
->distance
= distance
;
1812 rib
->metric
= metric
;
1813 rib
->table
= vrf_id
;
1814 rib
->nexthop_num
= 0;
1815 rib
->uptime
= time (NULL
);
1817 /* Nexthop settings. */
1821 nexthop_ipv6_ifindex_add (rib
, gate
, ifindex
);
1823 nexthop_ipv6_add (rib
, gate
);
1826 nexthop_ifindex_add (rib
, ifindex
);
1828 /* If this route is kernel route, set FIB flag to the route. */
1829 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1830 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1831 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1833 /* Link new rib to node.*/
1834 rib_addnode (rn
, rib
);
1836 /* Process this route node. */
1837 rib_queue_add (&zebrad
, rn
, same
);
1839 /* Free implicit route.*/
1841 rib_delnode (rn
, same
);
1843 route_unlock_node (rn
);
1848 rib_delete_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
1849 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1851 struct route_table
*table
;
1852 struct route_node
*rn
;
1854 struct rib
*fib
= NULL
;
1855 struct rib
*same
= NULL
;
1856 struct nexthop
*nexthop
;
1861 apply_mask_ipv6 (p
);
1864 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
1868 /* Lookup route node. */
1869 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1872 if (IS_ZEBRA_DEBUG_KERNEL
)
1875 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
1876 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
1878 inet_ntop (AF_INET6
, gate
, buf2
, BUFSIZ
),
1881 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
1882 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
1886 return ZEBRA_ERR_RTNOEXIST
;
1889 /* Lookup same type route. */
1890 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1892 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1895 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1897 nexthop
= rib
->nexthop
;
1899 if (rib
->type
== type
1900 && nexthop
&& nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1901 && nexthop
->ifindex
== ifindex
)
1906 route_unlock_node (rn
);
1907 route_unlock_node (rn
);
1916 if (rib
->type
== type
)
1924 /* If same type of route can't be found and this message is from
1928 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
1931 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1932 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1934 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1938 if (IS_ZEBRA_DEBUG_KERNEL
)
1941 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
1942 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
1944 inet_ntop (AF_INET6
, gate
, buf2
, BUFSIZ
),
1948 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
1949 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
1954 route_unlock_node (rn
);
1955 return ZEBRA_ERR_RTNOEXIST
;
1959 /* Process changes. */
1960 rib_queue_add (&zebrad
, rn
, same
);
1963 rib_delnode (rn
, same
);
1965 route_unlock_node (rn
);
1969 /* Install static route into rib. */
1971 static_install_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
1974 struct route_table
*table
;
1975 struct route_node
*rn
;
1978 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
1982 /* Lookup existing route */
1983 rn
= route_node_get (table
, p
);
1984 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1985 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1990 /* Same distance static route is there. Update it with new
1992 route_unlock_node (rn
);
1996 case STATIC_IPV6_GATEWAY
:
1997 nexthop_ipv6_add (rib
, &si
->ipv6
);
1999 case STATIC_IPV6_IFNAME
:
2000 nexthop_ifname_add (rib
, si
->ifname
);
2002 case STATIC_IPV6_GATEWAY_IFNAME
:
2003 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2006 rib_queue_add (&zebrad
, rn
, NULL
);
2010 /* This is new static route. */
2011 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2013 rib
->type
= ZEBRA_ROUTE_STATIC
;
2014 rib
->distance
= si
->distance
;
2016 rib
->nexthop_num
= 0;
2020 case STATIC_IPV6_GATEWAY
:
2021 nexthop_ipv6_add (rib
, &si
->ipv6
);
2023 case STATIC_IPV6_IFNAME
:
2024 nexthop_ifname_add (rib
, si
->ifname
);
2026 case STATIC_IPV6_GATEWAY_IFNAME
:
2027 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2031 /* Save the flags of this static routes (reject, blackhole) */
2032 rib
->flags
= si
->flags
;
2034 /* Link this rib to the tree. */
2035 rib_addnode (rn
, rib
);
2037 /* Process this prefix. */
2038 rib_queue_add (&zebrad
, rn
, NULL
);
2043 static_ipv6_nexthop_same (struct nexthop
*nexthop
, struct static_ipv6
*si
)
2045 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
2046 && si
->type
== STATIC_IPV6_GATEWAY
2047 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
))
2049 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2050 && si
->type
== STATIC_IPV6_IFNAME
2051 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2053 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
2054 && si
->type
== STATIC_IPV6_GATEWAY_IFNAME
2055 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
)
2056 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2062 static_uninstall_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2064 struct route_table
*table
;
2065 struct route_node
*rn
;
2067 struct nexthop
*nexthop
;
2070 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2074 /* Lookup existing route with type and distance. */
2075 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2079 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2080 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2084 route_unlock_node (rn
);
2088 /* Lookup nexthop. */
2089 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2090 if (static_ipv6_nexthop_same (nexthop
, si
))
2093 /* Can't find nexthop. */
2096 route_unlock_node (rn
);
2100 /* Check nexthop. */
2101 if (rib
->nexthop_num
== 1)
2103 rib_delnode (rn
, rib
);
2104 rib_queue_add (&zebrad
, rn
, rib
);
2108 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2109 rib_uninstall (rn
, rib
);
2110 rib_queue_add (&zebrad
, rn
, rib
);
2113 route_unlock_node (rn
);
2116 /* Add static route into static route configuration. */
2118 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2119 const char *ifname
, u_char flags
, u_char distance
,
2122 struct route_node
*rn
;
2123 struct static_ipv6
*si
;
2124 struct static_ipv6
*pp
;
2125 struct static_ipv6
*cp
;
2126 struct route_table
*stable
;
2129 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2133 /* Lookup static route prefix. */
2134 rn
= route_node_get (stable
, p
);
2136 /* Do nothing if there is a same static route. */
2137 for (si
= rn
->info
; si
; si
= si
->next
)
2139 if (distance
== si
->distance
2141 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2142 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2144 route_unlock_node (rn
);
2149 /* Make new static route structure. */
2150 si
= XMALLOC (MTYPE_STATIC_IPV6
, sizeof (struct static_ipv6
));
2151 memset (si
, 0, sizeof (struct static_ipv6
));
2154 si
->distance
= distance
;
2159 case STATIC_IPV6_GATEWAY
:
2162 case STATIC_IPV6_IFNAME
:
2163 si
->ifname
= XSTRDUP (0, ifname
);
2165 case STATIC_IPV6_GATEWAY_IFNAME
:
2167 si
->ifname
= XSTRDUP (0, ifname
);
2171 /* Add new static route information to the tree with sort by
2172 distance value and gateway address. */
2173 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2175 if (si
->distance
< cp
->distance
)
2177 if (si
->distance
> cp
->distance
)
2181 /* Make linked list. */
2191 /* Install into rib. */
2192 static_install_ipv6 (p
, si
);
2197 /* Delete static route from static route configuration. */
2199 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2200 const char *ifname
, u_char distance
, u_int32_t vrf_id
)
2202 struct route_node
*rn
;
2203 struct static_ipv6
*si
;
2204 struct route_table
*stable
;
2207 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2211 /* Lookup static route prefix. */
2212 rn
= route_node_lookup (stable
, p
);
2216 /* Find same static route is the tree */
2217 for (si
= rn
->info
; si
; si
= si
->next
)
2218 if (distance
== si
->distance
2220 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2221 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2224 /* Can't find static route. */
2227 route_unlock_node (rn
);
2231 /* Install into rib. */
2232 static_uninstall_ipv6 (p
, si
);
2234 /* Unlink static route from linked list. */
2236 si
->prev
->next
= si
->next
;
2238 rn
->info
= si
->next
;
2240 si
->next
->prev
= si
->prev
;
2242 /* Free static route configuration. */
2244 XFREE (0, si
->ifname
);
2245 XFREE (MTYPE_STATIC_IPV6
, si
);
2249 #endif /* HAVE_IPV6 */
2251 /* RIB update function. */
2255 struct route_node
*rn
;
2256 struct route_table
*table
;
2258 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2260 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2262 rib_queue_add (&zebrad
, rn
, NULL
);
2264 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2266 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2268 rib_queue_add (&zebrad
, rn
, NULL
);
2271 /* Interface goes up. */
2273 rib_if_up (struct interface
*ifp
)
2278 /* Interface goes down. */
2280 rib_if_down (struct interface
*ifp
)
2285 /* Remove all routes which comes from non main table. */
2287 rib_weed_table (struct route_table
*table
)
2289 struct route_node
*rn
;
2294 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2295 for (rib
= rn
->info
; rib
; rib
= next
)
2299 if (rib
->table
!= zebrad
.rtm_table_default
&&
2300 rib
->table
!= RT_TABLE_MAIN
)
2301 rib_delnode (rn
, rib
);
2305 /* Delete all routes from non main table. */
2307 rib_weed_tables (void)
2309 rib_weed_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2310 rib_weed_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2313 /* Delete self installed routes after zebra is relaunched. */
2315 rib_sweep_table (struct route_table
*table
)
2317 struct route_node
*rn
;
2323 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2324 for (rib
= rn
->info
; rib
; rib
= next
)
2328 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
2329 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
2331 ret
= rib_uninstall_kernel (rn
, rib
);
2333 rib_delnode (rn
, rib
);
2338 /* Sweep all RIB tables. */
2340 rib_sweep_route (void)
2342 rib_sweep_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2343 rib_sweep_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2346 /* Close RIB and clean up kernel routes. */
2348 rib_close_table (struct route_table
*table
)
2350 struct route_node
*rn
;
2354 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2355 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2356 if (! RIB_SYSTEM_ROUTE (rib
)
2357 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2358 rib_uninstall_kernel (rn
, rib
);
2361 /* Close all RIB tables. */
2365 rib_close_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2366 rib_close_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2369 /* Routing information base initialize. */
2373 rib_queue_init (&zebrad
);
2374 /* VRF initialization. */