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_ISIS
, 115},
58 {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */}
61 /* Vector for routing table. */
64 /* Allocate new VRF. */
66 vrf_alloc (char *name
)
70 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
74 vrf
->name
= XSTRDUP (MTYPE_VRF_NAME
, name
);
76 /* Allocate routing table and static table. */
77 vrf
->table
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
78 vrf
->table
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
79 vrf
->stable
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
80 vrf
->stable
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
87 vrf_free (struct vrf
*vrf
)
90 XFREE (MTYPE_VRF_NAME
, vrf
->name
);
91 XFREE (MTYPE_VRF
, vrf
);
94 /* Lookup VRF by identifier. */
96 vrf_lookup (u_int32_t id
)
98 return vector_lookup (vrf_vector
, id
);
101 /* Lookup VRF by name. */
103 vrf_lookup_by_name (char *name
)
108 for (i
= 0; i
< vector_max (vrf_vector
); i
++)
109 if ((vrf
= vector_slot (vrf_vector
, i
)) != NULL
)
110 if (vrf
->name
&& name
&& strcmp (vrf
->name
, name
) == 0)
115 /* Initialize VRF. */
119 struct vrf
*default_table
;
121 /* Allocate VRF vector. */
122 vrf_vector
= vector_init (1);
124 /* Allocate default main table. */
125 default_table
= vrf_alloc ("Default-IP-Routing-Table");
127 /* Default table index must be 0. */
128 vector_set_index (vrf_vector
, 0, default_table
);
131 /* Lookup route table. */
133 vrf_table (afi_t afi
, safi_t safi
, u_int32_t id
)
137 vrf
= vrf_lookup (id
);
141 return vrf
->table
[afi
][safi
];
144 /* Lookup static route table. */
146 vrf_static_table (afi_t afi
, safi_t safi
, u_int32_t id
)
150 vrf
= vrf_lookup (id
);
154 return vrf
->stable
[afi
][safi
];
157 /* Add nexthop to the end of the list. */
159 nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
161 struct nexthop
*last
;
163 for (last
= rib
->nexthop
; last
&& last
->next
; last
= last
->next
)
166 last
->next
= nexthop
;
168 rib
->nexthop
= nexthop
;
169 nexthop
->prev
= last
;
174 /* Delete specified nexthop from the list. */
176 nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
179 nexthop
->next
->prev
= nexthop
->prev
;
181 nexthop
->prev
->next
= nexthop
->next
;
183 rib
->nexthop
= nexthop
->next
;
189 nexthop_free (struct nexthop
*nexthop
)
192 XFREE (0, nexthop
->ifname
);
193 XFREE (MTYPE_NEXTHOP
, nexthop
);
197 nexthop_ifindex_add (struct rib
*rib
, unsigned int ifindex
)
199 struct nexthop
*nexthop
;
201 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
202 memset (nexthop
, 0, sizeof (struct nexthop
));
203 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
204 nexthop
->ifindex
= ifindex
;
206 nexthop_add (rib
, nexthop
);
212 nexthop_ifname_add (struct rib
*rib
, char *ifname
)
214 struct nexthop
*nexthop
;
216 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
217 memset (nexthop
, 0, sizeof (struct nexthop
));
218 nexthop
->type
= NEXTHOP_TYPE_IFNAME
;
219 nexthop
->ifname
= XSTRDUP (0, ifname
);
221 nexthop_add (rib
, nexthop
);
227 nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
)
229 struct nexthop
*nexthop
;
231 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
232 memset (nexthop
, 0, sizeof (struct nexthop
));
233 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
234 nexthop
->gate
.ipv4
= *ipv4
;
236 nexthop_add (rib
, nexthop
);
242 nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
243 unsigned int ifindex
)
245 struct nexthop
*nexthop
;
247 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
248 memset (nexthop
, 0, sizeof (struct nexthop
));
249 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
250 nexthop
->gate
.ipv4
= *ipv4
;
251 nexthop
->ifindex
= ifindex
;
253 nexthop_add (rib
, nexthop
);
260 nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
262 struct nexthop
*nexthop
;
264 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
265 memset (nexthop
, 0, sizeof (struct nexthop
));
266 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
267 nexthop
->gate
.ipv6
= *ipv6
;
269 nexthop_add (rib
, nexthop
);
275 nexthop_ipv6_ifname_add (struct rib
*rib
, struct in6_addr
*ipv6
,
278 struct nexthop
*nexthop
;
280 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
281 memset (nexthop
, 0, sizeof (struct nexthop
));
282 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFNAME
;
283 nexthop
->gate
.ipv6
= *ipv6
;
284 nexthop
->ifname
= XSTRDUP (0, ifname
);
286 nexthop_add (rib
, nexthop
);
292 nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
293 unsigned int ifindex
)
295 struct nexthop
*nexthop
;
297 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
298 memset (nexthop
, 0, sizeof (struct nexthop
));
299 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
300 nexthop
->gate
.ipv6
= *ipv6
;
301 nexthop
->ifindex
= ifindex
;
303 nexthop_add (rib
, nexthop
);
307 #endif /* HAVE_IPV6 */
310 nexthop_blackhole_add (struct rib
*rib
)
312 struct nexthop
*nexthop
;
314 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
315 memset (nexthop
, 0, sizeof (struct nexthop
));
316 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
317 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
319 nexthop_add (rib
, nexthop
);
324 /* If force flag is not set, do not modify falgs at all for uninstall
325 the route from FIB. */
327 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
328 struct route_node
*top
)
330 struct prefix_ipv4 p
;
331 struct route_table
*table
;
332 struct route_node
*rn
;
334 struct nexthop
*newhop
;
336 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
337 nexthop
->ifindex
= 0;
340 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
342 /* Make lookup prefix. */
343 memset (&p
, 0, sizeof (struct prefix_ipv4
));
345 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
346 p
.prefix
= nexthop
->gate
.ipv4
;
349 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
353 rn
= route_node_match (table
, (struct prefix
*) &p
);
356 route_unlock_node (rn
);
358 /* If lookup self prefix return immidiately. */
362 /* Pick up selected route. */
363 for (match
= rn
->info
; match
; match
= match
->next
)
364 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
367 /* If there is no selected route or matched route is EGP, go up
370 || match
->type
== ZEBRA_ROUTE_BGP
)
374 } while (rn
&& rn
->info
== NULL
);
376 route_lock_node (rn
);
380 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
382 /* Directly point connected route. */
383 newhop
= match
->nexthop
;
384 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
385 nexthop
->ifindex
= newhop
->ifindex
;
389 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
391 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
392 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
393 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
397 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
398 nexthop
->rtype
= newhop
->type
;
399 if (newhop
->type
== NEXTHOP_TYPE_IPV4
||
400 newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
401 nexthop
->rgate
.ipv4
= newhop
->gate
.ipv4
;
402 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
403 || newhop
->type
== NEXTHOP_TYPE_IFNAME
404 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
405 nexthop
->rifindex
= newhop
->ifindex
;
421 /* If force flag is not set, do not modify falgs at all for uninstall
422 the route from FIB. */
424 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
425 struct route_node
*top
)
427 struct prefix_ipv6 p
;
428 struct route_table
*table
;
429 struct route_node
*rn
;
431 struct nexthop
*newhop
;
433 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
434 nexthop
->ifindex
= 0;
437 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
439 /* Make lookup prefix. */
440 memset (&p
, 0, sizeof (struct prefix_ipv6
));
442 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
443 p
.prefix
= nexthop
->gate
.ipv6
;
446 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
450 rn
= route_node_match (table
, (struct prefix
*) &p
);
453 route_unlock_node (rn
);
455 /* If lookup self prefix return immidiately. */
459 /* Pick up selected route. */
460 for (match
= rn
->info
; match
; match
= match
->next
)
461 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
464 /* If there is no selected route or matched route is EGP, go up
467 || match
->type
== ZEBRA_ROUTE_BGP
)
471 } while (rn
&& rn
->info
== NULL
);
473 route_lock_node (rn
);
477 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
479 /* Directly point connected route. */
480 newhop
= match
->nexthop
;
482 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
483 nexthop
->ifindex
= newhop
->ifindex
;
487 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
489 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
490 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
491 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
495 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
496 nexthop
->rtype
= newhop
->type
;
497 if (newhop
->type
== NEXTHOP_TYPE_IPV6
498 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
499 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
500 nexthop
->rgate
.ipv6
= newhop
->gate
.ipv6
;
501 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
502 || newhop
->type
== NEXTHOP_TYPE_IFNAME
503 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
504 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
505 nexthop
->rifindex
= newhop
->ifindex
;
519 #endif /* HAVE_IPV6 */
522 rib_match_ipv4 (struct in_addr addr
)
524 struct prefix_ipv4 p
;
525 struct route_table
*table
;
526 struct route_node
*rn
;
528 struct nexthop
*newhop
;
531 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
535 memset (&p
, 0, sizeof (struct prefix_ipv4
));
537 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
540 rn
= route_node_match (table
, (struct prefix
*) &p
);
544 route_unlock_node (rn
);
546 /* Pick up selected route. */
547 for (match
= rn
->info
; match
; match
= match
->next
)
548 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
551 /* If there is no selected route or matched route is EGP, go up
554 || match
->type
== ZEBRA_ROUTE_BGP
)
558 } while (rn
&& rn
->info
== NULL
);
560 route_lock_node (rn
);
564 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
565 /* Directly point connected route. */
569 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
570 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
580 rib_lookup_ipv4 (struct prefix_ipv4
*p
)
582 struct route_table
*table
;
583 struct route_node
*rn
;
585 struct nexthop
*nexthop
;
588 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
592 rn
= route_node_lookup (table
, (struct prefix
*) p
);
594 /* No route for this prefix. */
599 route_unlock_node (rn
);
601 /* Pick up selected route. */
602 for (match
= rn
->info
; match
; match
= match
->next
)
603 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
606 if (! match
|| match
->type
== ZEBRA_ROUTE_BGP
)
609 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
612 for (nexthop
= match
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
613 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
621 rib_match_ipv6 (struct in6_addr
*addr
)
623 struct prefix_ipv6 p
;
624 struct route_table
*table
;
625 struct route_node
*rn
;
627 struct nexthop
*newhop
;
630 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
634 memset (&p
, 0, sizeof (struct prefix_ipv6
));
636 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
637 IPV6_ADDR_COPY (&p
.prefix
, addr
);
639 rn
= route_node_match (table
, (struct prefix
*) &p
);
643 route_unlock_node (rn
);
645 /* Pick up selected route. */
646 for (match
= rn
->info
; match
; match
= match
->next
)
647 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
650 /* If there is no selected route or matched route is EGP, go up
653 || match
->type
== ZEBRA_ROUTE_BGP
)
657 } while (rn
&& rn
->info
== NULL
);
659 route_lock_node (rn
);
663 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
664 /* Directly point connected route. */
668 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
669 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
677 #endif /* HAVE_IPV6 */
680 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
681 struct nexthop
*nexthop
, int set
)
683 struct interface
*ifp
;
685 switch (nexthop
->type
)
687 case NEXTHOP_TYPE_IFINDEX
:
688 ifp
= if_lookup_by_index (nexthop
->ifindex
);
689 if (ifp
&& if_is_up (ifp
))
690 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
692 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
694 case NEXTHOP_TYPE_IFNAME
:
695 case NEXTHOP_TYPE_IPV6_IFNAME
:
696 ifp
= if_lookup_by_name (nexthop
->ifname
);
697 if (ifp
&& if_is_up (ifp
))
700 nexthop
->ifindex
= ifp
->ifindex
;
701 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
706 nexthop
->ifindex
= 0;
707 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
710 case NEXTHOP_TYPE_IPV4
:
711 case NEXTHOP_TYPE_IPV4_IFINDEX
:
712 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
713 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
715 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
718 case NEXTHOP_TYPE_IPV6
:
719 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
720 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
722 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
724 case NEXTHOP_TYPE_IPV6_IFINDEX
:
725 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
727 ifp
= if_lookup_by_index (nexthop
->ifindex
);
728 if (ifp
&& if_is_up (ifp
))
729 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
731 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
735 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
736 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
738 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
741 #endif /* HAVE_IPV6 */
742 case NEXTHOP_TYPE_BLACKHOLE
:
743 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
748 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
752 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
754 struct nexthop
*nexthop
;
757 rib
->nexthop_active_num
= 0;
758 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
760 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
762 active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
764 nexthop_active_check (rn
, rib
, nexthop
, set
);
765 if ((MULTIPATH_NUM
== 0 || rib
->nexthop_active_num
< MULTIPATH_NUM
)
766 && active
!= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
767 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
769 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
770 rib
->nexthop_active_num
++;
772 return rib
->nexthop_active_num
;
776 #define RIB_SYSTEM_ROUTE(R) \
777 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
780 newrib_free (struct rib
*rib
)
782 struct nexthop
*nexthop
;
783 struct nexthop
*next
;
785 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= next
)
787 next
= nexthop
->next
;
788 nexthop_free (nexthop
);
790 XFREE (MTYPE_RIB
, rib
);
794 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
)
797 struct nexthop
*nexthop
;
799 switch (PREFIX_FAMILY (&rn
->p
))
802 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
806 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
808 #endif /* HAVE_IPV6 */
813 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
814 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
818 /* Uninstall the route from kernel. */
820 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
823 struct nexthop
*nexthop
;
825 switch (PREFIX_FAMILY (&rn
->p
))
828 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
832 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
834 #endif /* HAVE_IPV6 */
837 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
838 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
843 /* Uninstall the route from kernel. */
845 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
847 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
849 redistribute_delete (&rn
->p
, rib
);
850 if (! RIB_SYSTEM_ROUTE (rib
))
851 rib_uninstall_kernel (rn
, rib
);
852 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
856 /* Core function for processing routing information base. */
858 rib_process (struct route_node
*rn
, struct rib
*del
)
862 struct rib
*fib
= NULL
;
863 struct rib
*select
= NULL
;
865 struct nexthop
*nexthop
= NULL
;
867 for (rib
= rn
->info
; rib
; rib
= next
)
871 /* Currently installed rib. */
872 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
875 /* Skip unreachable nexthop. */
876 if (! nexthop_active_update (rn
, rib
, 0))
879 /* Infinit distance. */
880 if (rib
->distance
== DISTANCE_INFINITY
)
883 /* Newly selected rib. */
884 if (! select
|| rib
->distance
< select
->distance
885 || rib
->type
== ZEBRA_ROUTE_CONNECT
)
889 /* Deleted route check. */
890 if (del
&& CHECK_FLAG (del
->flags
, ZEBRA_FLAG_SELECTED
))
893 /* Same route is selected. */
894 if (select
&& select
== fib
)
896 if (CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
898 redistribute_delete (&rn
->p
, select
);
899 if (! RIB_SYSTEM_ROUTE (select
))
900 rib_uninstall_kernel (rn
, select
);
902 /* Set real nexthop. */
903 nexthop_active_update (rn
, select
, 1);
905 if (! RIB_SYSTEM_ROUTE (select
))
906 rib_install_kernel (rn
, select
);
907 redistribute_add (&rn
->p
, select
);
909 else if (! RIB_SYSTEM_ROUTE (select
))
911 /* Housekeeping code to deal with
912 race conditions in kernel with linux
913 netlink reporting interface up before IPv4 or IPv6 protocol
914 is ready to add routes.
915 This makes sure the routes are IN the kernel.
918 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
920 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
923 if (! installed
) rib_install_kernel (rn
, select
);
928 /* Uninstall old rib from forwarding table. */
931 redistribute_delete (&rn
->p
, fib
);
932 if (! RIB_SYSTEM_ROUTE (fib
))
933 rib_uninstall_kernel (rn
, fib
);
934 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
936 /* Set real nexthop. */
937 nexthop_active_update (rn
, fib
, 1);
940 /* Install new rib into forwarding table. */
943 /* Set real nexthop. */
944 nexthop_active_update (rn
, select
, 1);
946 if (! RIB_SYSTEM_ROUTE (select
))
947 rib_install_kernel (rn
, select
);
948 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
949 redistribute_add (&rn
->p
, select
);
953 /* Add RIB to head of the route node. */
955 rib_addnode (struct route_node
*rn
, struct rib
*rib
)
967 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
970 rib
->next
->prev
= rib
->prev
;
972 rib
->prev
->next
= rib
->next
;
974 rn
->info
= rib
->next
;
978 rib_add_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
979 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
,
980 u_int32_t metric
, u_char distance
)
983 struct rib
*same
= NULL
;
984 struct route_table
*table
;
985 struct route_node
*rn
;
986 struct nexthop
*nexthop
;
989 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
993 /* Make it sure prefixlen is applied to the prefix. */
996 /* Set default distance by route type. */
999 distance
= route_info
[type
].distance
;
1001 /* iBGP distance is 200. */
1002 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1006 /* Lookup route node.*/
1007 rn
= route_node_get (table
, (struct prefix
*) p
);
1009 /* If same type of route are installed, treat it as a implicit
1011 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1013 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1015 nexthop
= rib
->nexthop
;
1017 /* Duplicate connected route comes in. */
1018 if (rib
->type
== type
1019 && nexthop
&& nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1020 && nexthop
->ifindex
== ifindex
)
1026 else if (rib
->type
== type
)
1029 rib_delnode (rn
, same
);
1030 route_unlock_node (rn
);
1035 /* Allocate new rib structure. */
1036 rib
= XMALLOC (MTYPE_RIB
, sizeof (struct rib
));
1037 memset (rib
, 0, sizeof (struct rib
));
1039 rib
->distance
= distance
;
1041 rib
->metric
= metric
;
1042 rib
->table
= vrf_id
;
1043 rib
->nexthop_num
= 0;
1044 rib
->uptime
= time (NULL
);
1046 /* Nexthop settings. */
1050 nexthop_ipv4_ifindex_add (rib
, gate
, ifindex
);
1052 nexthop_ipv4_add (rib
, gate
);
1055 nexthop_ifindex_add (rib
, ifindex
);
1057 /* If this route is kernel route, set FIB flag to the route. */
1058 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1059 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1060 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1062 /* Link new rib to node.*/
1063 rib_addnode (rn
, rib
);
1065 /* Process this route node. */
1066 rib_process (rn
, same
);
1068 /* Free implicit route.*/
1076 rib_add_ipv4_multipath (struct prefix_ipv4
*p
, struct rib
*rib
)
1078 struct route_table
*table
;
1079 struct route_node
*rn
;
1081 struct nexthop
*nexthop
;
1084 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1088 /* Make it sure prefixlen is applied to the prefix. */
1089 apply_mask_ipv4 (p
);
1091 /* Set default distance by route type. */
1092 if (rib
->distance
== 0)
1094 rib
->distance
= route_info
[rib
->type
].distance
;
1096 /* iBGP distance is 200. */
1097 if (rib
->type
== ZEBRA_ROUTE_BGP
1098 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
1099 rib
->distance
= 200;
1102 /* Lookup route node.*/
1103 rn
= route_node_get (table
, (struct prefix
*) p
);
1105 /* If same type of route are installed, treat it as a implicit
1107 for (same
= rn
->info
; same
; same
= same
->next
)
1109 if (same
->type
== rib
->type
&& same
->table
== rib
->table
1110 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
1112 rib_delnode (rn
, same
);
1113 route_unlock_node (rn
);
1118 /* If this route is kernel route, set FIB flag to the route. */
1119 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
1120 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1121 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1123 /* Link new rib to node.*/
1124 rib_addnode (rn
, rib
);
1126 /* Process this route node. */
1127 rib_process (rn
, same
);
1129 /* Free implicit route.*/
1137 rib_delete_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1138 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1140 struct route_table
*table
;
1141 struct route_node
*rn
;
1143 struct rib
*fib
= NULL
;
1144 struct rib
*same
= NULL
;
1145 struct nexthop
*nexthop
;
1150 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1155 apply_mask_ipv4 (p
);
1157 if (IS_ZEBRA_DEBUG_KERNEL
&& gate
)
1158 zlog_info ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
1159 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1164 /* Lookup route node. */
1165 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1168 if (IS_ZEBRA_DEBUG_KERNEL
)
1171 zlog_info ("route %s/%d via %s ifindex %d doesn't exist in rib",
1172 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1174 inet_ntop (AF_INET
, gate
, buf2
, BUFSIZ
),
1177 zlog_info ("route %s/%d ifindex %d doesn't exist in rib",
1178 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1182 return ZEBRA_ERR_RTNOEXIST
;
1185 /* Lookup same type route. */
1186 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1188 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1191 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1193 nexthop
= rib
->nexthop
;
1195 if (rib
->type
== type
1196 && nexthop
&& nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1197 && nexthop
->ifindex
== ifindex
)
1202 route_unlock_node (rn
);
1203 route_unlock_node (rn
);
1212 nexthop
= rib
->nexthop
;
1214 /* Make sure that the route found has the same gateway. */
1215 if (rib
->type
== type
1217 (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
1218 IPV4_ADDR_SAME (&nexthop
->rgate
.ipv4
, gate
)) )
1226 if (rib
->type
== type
)
1234 /* If same type of route can't be found and this message is from
1238 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
1241 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1242 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1244 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1248 if (IS_ZEBRA_DEBUG_KERNEL
)
1251 zlog_info ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
1252 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1254 inet_ntop (AF_INET
, gate
, buf2
, BUFSIZ
),
1258 zlog_info ("route %s/%d ifindex %d type %d doesn't exist in rib",
1259 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1264 route_unlock_node (rn
);
1265 return ZEBRA_ERR_RTNOEXIST
;
1270 rib_delnode (rn
, same
);
1272 /* Process changes. */
1273 rib_process (rn
, same
);
1278 route_unlock_node (rn
);
1281 route_unlock_node (rn
);
1286 /* Install static route into rib. */
1288 static_install_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
1291 struct route_node
*rn
;
1292 struct route_table
*table
;
1295 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1299 /* Lookup existing route */
1300 rn
= route_node_get (table
, p
);
1301 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1302 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1307 /* Same distance static route is there. Update it with new
1309 route_unlock_node (rn
);
1312 case STATIC_IPV4_GATEWAY
:
1313 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
);
1315 case STATIC_IPV4_IFNAME
:
1316 nexthop_ifname_add (rib
, si
->gate
.ifname
);
1318 case STATIC_IPV4_BLACKHOLE
:
1319 nexthop_blackhole_add (rib
);
1322 rib_process (rn
, NULL
);
1326 /* This is new static route. */
1327 rib
= XMALLOC (MTYPE_RIB
, sizeof (struct rib
));
1328 memset (rib
, 0, sizeof (struct rib
));
1330 rib
->type
= ZEBRA_ROUTE_STATIC
;
1331 rib
->distance
= si
->distance
;
1333 rib
->nexthop_num
= 0;
1337 case STATIC_IPV4_GATEWAY
:
1338 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
);
1340 case STATIC_IPV4_IFNAME
:
1341 nexthop_ifname_add (rib
, si
->gate
.ifname
);
1343 case STATIC_IPV4_BLACKHOLE
:
1344 nexthop_blackhole_add (rib
);
1348 /* Save the flags of this static routes (reject, blackhole) */
1349 rib
->flags
= si
->flags
;
1351 /* Link this rib to the tree. */
1352 rib_addnode (rn
, rib
);
1354 /* Process this prefix. */
1355 rib_process (rn
, NULL
);
1360 static_ipv4_nexthop_same (struct nexthop
*nexthop
, struct static_ipv4
*si
)
1362 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1363 && si
->type
== STATIC_IPV4_GATEWAY
1364 && IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &si
->gate
.ipv4
))
1366 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
1367 && si
->type
== STATIC_IPV4_IFNAME
1368 && strcmp (nexthop
->ifname
, si
->gate
.ifname
) == 0)
1370 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
1371 && si
->type
== STATIC_IPV4_BLACKHOLE
)
1376 /* Uninstall static route from RIB. */
1378 static_uninstall_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
1380 struct route_node
*rn
;
1382 struct nexthop
*nexthop
;
1383 struct route_table
*table
;
1386 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1390 /* Lookup existing route with type and distance. */
1391 rn
= route_node_lookup (table
, p
);
1395 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1396 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1401 route_unlock_node (rn
);
1405 /* Lookup nexthop. */
1406 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1407 if (static_ipv4_nexthop_same (nexthop
, si
))
1410 /* Can't find nexthop. */
1413 route_unlock_node (rn
);
1417 /* Check nexthop. */
1418 if (rib
->nexthop_num
== 1)
1420 rib_delnode (rn
, rib
);
1421 rib_process (rn
, rib
);
1423 route_unlock_node (rn
);
1427 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1428 rib_uninstall (rn
, rib
);
1429 nexthop_delete (rib
, nexthop
);
1430 nexthop_free (nexthop
);
1431 rib_process (rn
, rib
);
1435 route_unlock_node (rn
);
1438 /* Add static route into static route configuration. */
1440 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, char *ifname
,
1441 u_char flags
, u_char distance
, u_int32_t vrf_id
)
1444 struct route_node
*rn
;
1445 struct static_ipv4
*si
;
1446 struct static_ipv4
*pp
;
1447 struct static_ipv4
*cp
;
1448 struct static_ipv4
*update
= NULL
;
1449 struct route_table
*stable
;
1452 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
1456 /* Lookup static route prefix. */
1457 rn
= route_node_get (stable
, p
);
1461 type
= STATIC_IPV4_GATEWAY
;
1463 type
= STATIC_IPV4_IFNAME
;
1465 type
= STATIC_IPV4_BLACKHOLE
;
1467 /* Do nothing if there is a same static route. */
1468 for (si
= rn
->info
; si
; si
= si
->next
)
1470 if (type
== si
->type
1471 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
1472 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
1474 if (distance
== si
->distance
)
1476 route_unlock_node (rn
);
1484 /* Distance chaged. */
1486 static_delete_ipv4 (p
, gate
, ifname
, update
->distance
, vrf_id
);
1488 /* Make new static route structure. */
1489 si
= XMALLOC (MTYPE_STATIC_IPV4
, sizeof (struct static_ipv4
));
1490 memset (si
, 0, sizeof (struct static_ipv4
));
1493 si
->distance
= distance
;
1497 si
->gate
.ipv4
= *gate
;
1499 si
->gate
.ifname
= XSTRDUP (0, ifname
);
1501 /* Add new static route information to the tree with sort by
1502 distance value and gateway address. */
1503 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
1505 if (si
->distance
< cp
->distance
)
1507 if (si
->distance
> cp
->distance
)
1509 if (si
->type
== STATIC_IPV4_GATEWAY
&& cp
->type
== STATIC_IPV4_GATEWAY
)
1511 if (ntohl (si
->gate
.ipv4
.s_addr
) < ntohl (cp
->gate
.ipv4
.s_addr
))
1513 if (ntohl (si
->gate
.ipv4
.s_addr
) > ntohl (cp
->gate
.ipv4
.s_addr
))
1518 /* Make linked list. */
1528 /* Install into rib. */
1529 static_install_ipv4 (p
, si
);
1534 /* Delete static route from static route configuration. */
1536 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, char *ifname
,
1537 u_char distance
, u_int32_t vrf_id
)
1540 struct route_node
*rn
;
1541 struct static_ipv4
*si
;
1542 struct route_table
*stable
;
1545 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
1549 /* Lookup static route prefix. */
1550 rn
= route_node_lookup (stable
, p
);
1556 type
= STATIC_IPV4_GATEWAY
;
1558 type
= STATIC_IPV4_IFNAME
;
1560 type
= STATIC_IPV4_BLACKHOLE
;
1562 /* Find same static route is the tree */
1563 for (si
= rn
->info
; si
; si
= si
->next
)
1564 if (type
== si
->type
1565 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
1566 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
1569 /* Can't find static route. */
1572 route_unlock_node (rn
);
1576 /* Install into rib. */
1577 static_uninstall_ipv4 (p
, si
);
1579 /* Unlink static route from linked list. */
1581 si
->prev
->next
= si
->next
;
1583 rn
->info
= si
->next
;
1585 si
->next
->prev
= si
->prev
;
1586 route_unlock_node (rn
);
1588 /* Free static route configuration. */
1590 XFREE (0, si
->gate
.ifname
);
1591 XFREE (MTYPE_STATIC_IPV4
, si
);
1593 route_unlock_node (rn
);
1601 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
1602 struct in6_addr
*gate
, unsigned int ifindex
, int table
)
1604 if (type
== ZEBRA_ROUTE_CONNECT
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)) {
1605 #if defined (MUSICA) || defined (LINUX)
1606 /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
1607 if (p
->prefixlen
== 96)
1612 if (type
== ZEBRA_ROUTE_KERNEL
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)
1613 && p
->prefixlen
== 96 && gate
&& IN6_IS_ADDR_UNSPECIFIED (gate
))
1615 kernel_delete_ipv6_old (p
, gate
, ifindex
, 0, table
);
1622 rib_add_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
1623 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1626 struct rib
*same
= NULL
;
1627 struct route_table
*table
;
1628 struct route_node
*rn
;
1629 struct nexthop
*nexthop
;
1632 u_int32_t metric
= 0;
1635 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
1639 /* Make sure mask is applied. */
1640 apply_mask_ipv6 (p
);
1642 /* Set default distance by route type. */
1643 distance
= route_info
[type
].distance
;
1645 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1648 /* Filter bogus route. */
1649 if (rib_bogus_ipv6 (type
, p
, gate
, ifindex
, 0))
1652 /* Lookup route node.*/
1653 rn
= route_node_get (table
, (struct prefix
*) p
);
1655 /* If same type of route are installed, treat it as a implicit
1657 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1659 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1661 nexthop
= rib
->nexthop
;
1663 if (rib
->type
== type
1664 && nexthop
&& nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1665 && nexthop
->ifindex
== ifindex
)
1671 else if (rib
->type
== type
)
1674 rib_delnode (rn
, same
);
1675 route_unlock_node (rn
);
1680 /* Allocate new rib structure. */
1681 rib
= XMALLOC (MTYPE_RIB
, sizeof (struct rib
));
1682 memset (rib
, 0, sizeof (struct rib
));
1684 rib
->distance
= distance
;
1686 rib
->metric
= metric
;
1687 rib
->table
= vrf_id
;
1688 rib
->nexthop_num
= 0;
1689 rib
->uptime
= time (NULL
);
1691 /* Nexthop settings. */
1695 nexthop_ipv6_ifindex_add (rib
, gate
, ifindex
);
1697 nexthop_ipv6_add (rib
, gate
);
1700 nexthop_ifindex_add (rib
, ifindex
);
1702 /* If this route is kernel route, set FIB flag to the route. */
1703 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1704 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1705 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1707 /* Link new rib to node.*/
1708 rib_addnode (rn
, rib
);
1710 /* Process this route node. */
1711 rib_process (rn
, same
);
1713 /* Free implicit route.*/
1721 rib_delete_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
1722 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1724 struct route_table
*table
;
1725 struct route_node
*rn
;
1727 struct rib
*fib
= NULL
;
1728 struct rib
*same
= NULL
;
1729 struct nexthop
*nexthop
;
1734 apply_mask_ipv6 (p
);
1737 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
1741 /* Lookup route node. */
1742 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1745 if (IS_ZEBRA_DEBUG_KERNEL
)
1748 zlog_info ("route %s/%d via %s ifindex %d doesn't exist in rib",
1749 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
1751 inet_ntop (AF_INET6
, gate
, buf2
, BUFSIZ
),
1754 zlog_info ("route %s/%d ifindex %d doesn't exist in rib",
1755 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
1759 return ZEBRA_ERR_RTNOEXIST
;
1762 /* Lookup same type route. */
1763 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1765 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1768 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1770 nexthop
= rib
->nexthop
;
1772 if (rib
->type
== type
1773 && nexthop
&& nexthop
->type
== NEXTHOP_TYPE_IFINDEX
1774 && nexthop
->ifindex
== ifindex
)
1779 route_unlock_node (rn
);
1780 route_unlock_node (rn
);
1789 if (rib
->type
== type
)
1797 /* If same type of route can't be found and this message is from
1801 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
1804 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1805 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1807 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1811 if (IS_ZEBRA_DEBUG_KERNEL
)
1814 zlog_info ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
1815 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
1817 inet_ntop (AF_INET6
, gate
, buf2
, BUFSIZ
),
1821 zlog_info ("route %s/%d ifindex %d type %d doesn't exist in rib",
1822 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
1827 route_unlock_node (rn
);
1828 return ZEBRA_ERR_RTNOEXIST
;
1833 rib_delnode (rn
, same
);
1835 /* Process changes. */
1836 rib_process (rn
, same
);
1841 route_unlock_node (rn
);
1844 route_unlock_node (rn
);
1849 /* Install static route into rib. */
1851 static_install_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
1854 struct route_table
*table
;
1855 struct route_node
*rn
;
1858 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
1862 /* Lookup existing route */
1863 rn
= route_node_get (table
, p
);
1864 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1865 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1870 /* Same distance static route is there. Update it with new
1872 route_unlock_node (rn
);
1876 case STATIC_IPV6_GATEWAY
:
1877 nexthop_ipv6_add (rib
, &si
->ipv6
);
1879 case STATIC_IPV6_IFNAME
:
1880 nexthop_ifname_add (rib
, si
->ifname
);
1882 case STATIC_IPV6_GATEWAY_IFNAME
:
1883 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
1886 rib_process (rn
, NULL
);
1890 /* This is new static route. */
1891 rib
= XMALLOC (MTYPE_RIB
, sizeof (struct rib
));
1892 memset (rib
, 0, sizeof (struct rib
));
1894 rib
->type
= ZEBRA_ROUTE_STATIC
;
1895 rib
->distance
= si
->distance
;
1897 rib
->nexthop_num
= 0;
1901 case STATIC_IPV6_GATEWAY
:
1902 nexthop_ipv6_add (rib
, &si
->ipv6
);
1904 case STATIC_IPV6_IFNAME
:
1905 nexthop_ifname_add (rib
, si
->ifname
);
1907 case STATIC_IPV6_GATEWAY_IFNAME
:
1908 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
1912 /* Save the flags of this static routes (reject, blackhole) */
1913 rib
->flags
= si
->flags
;
1915 /* Link this rib to the tree. */
1916 rib_addnode (rn
, rib
);
1918 /* Process this prefix. */
1919 rib_process (rn
, NULL
);
1924 static_ipv6_nexthop_same (struct nexthop
*nexthop
, struct static_ipv6
*si
)
1926 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
1927 && si
->type
== STATIC_IPV6_GATEWAY
1928 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
))
1930 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
1931 && si
->type
== STATIC_IPV6_IFNAME
1932 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
1934 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
1935 && si
->type
== STATIC_IPV6_GATEWAY_IFNAME
1936 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
)
1937 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
1943 static_uninstall_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
1945 struct route_table
*table
;
1946 struct route_node
*rn
;
1948 struct nexthop
*nexthop
;
1951 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
1955 /* Lookup existing route with type and distance. */
1956 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1960 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1961 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1965 route_unlock_node (rn
);
1969 /* Lookup nexthop. */
1970 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1971 if (static_ipv6_nexthop_same (nexthop
, si
))
1974 /* Can't find nexthop. */
1977 route_unlock_node (rn
);
1981 /* Check nexthop. */
1982 if (rib
->nexthop_num
== 1)
1984 rib_delnode (rn
, rib
);
1985 rib_process (rn
, rib
);
1987 route_unlock_node (rn
);
1991 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1992 rib_uninstall (rn
, rib
);
1993 nexthop_delete (rib
, nexthop
);
1994 nexthop_free (nexthop
);
1995 rib_process (rn
, rib
);
1999 route_unlock_node (rn
);
2002 /* Add static route into static route configuration. */
2004 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2005 char *ifname
, u_char flags
, u_char distance
, u_int32_t vrf_id
)
2007 struct route_node
*rn
;
2008 struct static_ipv6
*si
;
2009 struct static_ipv6
*pp
;
2010 struct static_ipv6
*cp
;
2011 struct route_table
*stable
;
2014 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2018 /* Lookup static route prefix. */
2019 rn
= route_node_get (stable
, p
);
2021 /* Do nothing if there is a same static route. */
2022 for (si
= rn
->info
; si
; si
= si
->next
)
2024 if (distance
== si
->distance
2026 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2027 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2029 route_unlock_node (rn
);
2034 /* Make new static route structure. */
2035 si
= XMALLOC (MTYPE_STATIC_IPV6
, sizeof (struct static_ipv6
));
2036 memset (si
, 0, sizeof (struct static_ipv6
));
2039 si
->distance
= distance
;
2044 case STATIC_IPV6_GATEWAY
:
2047 case STATIC_IPV6_IFNAME
:
2048 si
->ifname
= XSTRDUP (0, ifname
);
2050 case STATIC_IPV6_GATEWAY_IFNAME
:
2052 si
->ifname
= XSTRDUP (0, ifname
);
2056 /* Add new static route information to the tree with sort by
2057 distance value and gateway address. */
2058 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2060 if (si
->distance
< cp
->distance
)
2062 if (si
->distance
> cp
->distance
)
2066 /* Make linked list. */
2076 /* Install into rib. */
2077 static_install_ipv6 (p
, si
);
2082 /* Delete static route from static route configuration. */
2084 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2085 char *ifname
, u_char distance
, u_int32_t vrf_id
)
2087 struct route_node
*rn
;
2088 struct static_ipv6
*si
;
2089 struct route_table
*stable
;
2092 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2096 /* Lookup static route prefix. */
2097 rn
= route_node_lookup (stable
, p
);
2101 /* Find same static route is the tree */
2102 for (si
= rn
->info
; si
; si
= si
->next
)
2103 if (distance
== si
->distance
2105 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2106 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2109 /* Can't find static route. */
2112 route_unlock_node (rn
);
2116 /* Install into rib. */
2117 static_uninstall_ipv6 (p
, si
);
2119 /* Unlink static route from linked list. */
2121 si
->prev
->next
= si
->next
;
2123 rn
->info
= si
->next
;
2125 si
->next
->prev
= si
->prev
;
2127 /* Free static route configuration. */
2129 XFREE (0, si
->ifname
);
2130 XFREE (MTYPE_STATIC_IPV6
, si
);
2134 #endif /* HAVE_IPV6 */
2136 /* RIB update function. */
2140 struct route_node
*rn
;
2141 struct route_table
*table
;
2143 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2145 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2146 rib_process (rn
, NULL
);
2148 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2150 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2151 rib_process (rn
, NULL
);
2154 /* Interface goes up. */
2156 rib_if_up (struct interface
*ifp
)
2161 /* Interface goes down. */
2163 rib_if_down (struct interface
*ifp
)
2168 /* Remove all routes which comes from non main table. */
2170 rib_weed_table (struct route_table
*table
)
2172 struct route_node
*rn
;
2177 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2178 for (rib
= rn
->info
; rib
; rib
= next
)
2182 if (rib
->table
!= zebrad
.rtm_table_default
&&
2183 rib
->table
!= RT_TABLE_MAIN
)
2185 rib_delnode (rn
, rib
);
2187 route_unlock_node (rn
);
2192 /* Delete all routes from non main table. */
2196 rib_weed_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2197 rib_weed_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2200 /* Delete self installed routes after zebra is relaunched. */
2202 rib_sweep_table (struct route_table
*table
)
2204 struct route_node
*rn
;
2210 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2211 for (rib
= rn
->info
; rib
; rib
= next
)
2215 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
2216 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
2218 ret
= rib_uninstall_kernel (rn
, rib
);
2221 rib_delnode (rn
, rib
);
2223 route_unlock_node (rn
);
2229 /* Sweep all RIB tables. */
2233 rib_sweep_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2234 rib_sweep_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2237 /* Close RIB and clean up kernel routes. */
2239 rib_close_table (struct route_table
*table
)
2241 struct route_node
*rn
;
2245 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2246 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2247 if (! RIB_SYSTEM_ROUTE (rib
)
2248 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2249 rib_uninstall_kernel (rn
, rib
);
2252 /* Close all RIB tables. */
2256 rib_close_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2257 rib_close_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2260 /* Routing information base initialize. */
2264 /* VRF initialization. */