1 /* Routing Information Base.
2 * Copyright (C) 1997, 98, 99, 2001 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
31 #include "sockunion.h"
34 #include "workqueue.h"
38 #include "zebra/rib.h"
40 #include "zebra/zserv.h"
41 #include "zebra/redistribute.h"
42 #include "zebra/debug.h"
44 /* Default rtm_table for all clients */
45 extern struct zebra_t zebrad
;
47 /* Hold time for RIB process, should be very minimal.
48 * it is useful to able to set it otherwise for testing, hence exported
49 * as global here for test-rig code.
51 int rib_process_hold_time
= 10;
53 /* Each route type's string and default distance value. */
60 {ZEBRA_ROUTE_SYSTEM
, 0},
61 {ZEBRA_ROUTE_KERNEL
, 0},
62 {ZEBRA_ROUTE_CONNECT
, 0},
63 {ZEBRA_ROUTE_STATIC
, 1},
64 {ZEBRA_ROUTE_RIP
, 120},
65 {ZEBRA_ROUTE_RIPNG
, 120},
66 {ZEBRA_ROUTE_OSPF
, 110},
67 {ZEBRA_ROUTE_OSPF6
, 110},
68 {ZEBRA_ROUTE_ISIS
, 115},
69 {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */}
72 /* Vector for routing table. */
75 /* Allocate new VRF. */
77 vrf_alloc (const char *name
)
81 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
85 vrf
->name
= XSTRDUP (MTYPE_VRF_NAME
, name
);
87 /* Allocate routing table and static table. */
88 vrf
->table
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
89 vrf
->table
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
90 vrf
->stable
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
91 vrf
->stable
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
98 vrf_free (struct vrf
*vrf
)
101 XFREE (MTYPE_VRF_NAME
, vrf
->name
);
102 XFREE (MTYPE_VRF
, vrf
);
105 /* Lookup VRF by identifier. */
107 vrf_lookup (u_int32_t id
)
109 return vector_lookup (vrf_vector
, id
);
112 /* Lookup VRF by name. */
114 vrf_lookup_by_name (char *name
)
119 for (i
= 0; i
< vector_active (vrf_vector
); i
++)
120 if ((vrf
= vector_slot (vrf_vector
, i
)) != NULL
)
121 if (vrf
->name
&& name
&& strcmp (vrf
->name
, name
) == 0)
126 /* Initialize VRF. */
130 struct vrf
*default_table
;
132 /* Allocate VRF vector. */
133 vrf_vector
= vector_init (1);
135 /* Allocate default main table. */
136 default_table
= vrf_alloc ("Default-IP-Routing-Table");
138 /* Default table index must be 0. */
139 vector_set_index (vrf_vector
, 0, default_table
);
142 /* Lookup route table. */
144 vrf_table (afi_t afi
, safi_t safi
, u_int32_t id
)
148 vrf
= vrf_lookup (id
);
152 return vrf
->table
[afi
][safi
];
155 /* Lookup static route table. */
157 vrf_static_table (afi_t afi
, safi_t safi
, u_int32_t id
)
161 vrf
= vrf_lookup (id
);
165 return vrf
->stable
[afi
][safi
];
168 /* Add nexthop to the end of the list. */
170 nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
172 struct nexthop
*last
;
174 for (last
= rib
->nexthop
; last
&& last
->next
; last
= last
->next
)
177 last
->next
= nexthop
;
179 rib
->nexthop
= nexthop
;
180 nexthop
->prev
= last
;
185 /* Delete specified nexthop from the list. */
187 nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
190 nexthop
->next
->prev
= nexthop
->prev
;
192 nexthop
->prev
->next
= nexthop
->next
;
194 rib
->nexthop
= nexthop
->next
;
200 nexthop_free (struct nexthop
*nexthop
)
203 XFREE (0, nexthop
->ifname
);
204 XFREE (MTYPE_NEXTHOP
, nexthop
);
208 nexthop_ifindex_add (struct rib
*rib
, unsigned int ifindex
)
210 struct nexthop
*nexthop
;
212 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
213 memset (nexthop
, 0, sizeof (struct nexthop
));
214 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
215 nexthop
->ifindex
= ifindex
;
217 nexthop_add (rib
, nexthop
);
223 nexthop_ifname_add (struct rib
*rib
, char *ifname
)
225 struct nexthop
*nexthop
;
227 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
228 memset (nexthop
, 0, sizeof (struct nexthop
));
229 nexthop
->type
= NEXTHOP_TYPE_IFNAME
;
230 nexthop
->ifname
= XSTRDUP (0, ifname
);
232 nexthop_add (rib
, nexthop
);
238 nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
240 struct nexthop
*nexthop
;
242 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
243 memset (nexthop
, 0, sizeof (struct nexthop
));
244 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
245 nexthop
->gate
.ipv4
= *ipv4
;
247 nexthop
->src
.ipv4
= *src
;
249 nexthop_add (rib
, nexthop
);
254 static struct nexthop
*
255 nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
256 struct in_addr
*src
, unsigned int ifindex
)
258 struct nexthop
*nexthop
;
260 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
261 memset (nexthop
, 0, sizeof (struct nexthop
));
262 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
263 nexthop
->gate
.ipv4
= *ipv4
;
265 nexthop
->src
.ipv4
= *src
;
266 nexthop
->ifindex
= ifindex
;
268 nexthop_add (rib
, nexthop
);
275 nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
277 struct nexthop
*nexthop
;
279 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
280 memset (nexthop
, 0, sizeof (struct nexthop
));
281 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
282 nexthop
->gate
.ipv6
= *ipv6
;
284 nexthop_add (rib
, nexthop
);
289 static struct nexthop
*
290 nexthop_ipv6_ifname_add (struct rib
*rib
, struct in6_addr
*ipv6
,
293 struct nexthop
*nexthop
;
295 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
296 memset (nexthop
, 0, sizeof (struct nexthop
));
297 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFNAME
;
298 nexthop
->gate
.ipv6
= *ipv6
;
299 nexthop
->ifname
= XSTRDUP (0, ifname
);
301 nexthop_add (rib
, nexthop
);
306 static struct nexthop
*
307 nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
308 unsigned int ifindex
)
310 struct nexthop
*nexthop
;
312 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
313 memset (nexthop
, 0, sizeof (struct nexthop
));
314 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
315 nexthop
->gate
.ipv6
= *ipv6
;
316 nexthop
->ifindex
= ifindex
;
318 nexthop_add (rib
, nexthop
);
322 #endif /* HAVE_IPV6 */
325 nexthop_blackhole_add (struct rib
*rib
)
327 struct nexthop
*nexthop
;
329 nexthop
= XMALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
330 memset (nexthop
, 0, sizeof (struct nexthop
));
331 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
332 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
334 nexthop_add (rib
, nexthop
);
339 /* If force flag is not set, do not modify falgs at all for uninstall
340 the route from FIB. */
342 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
343 struct route_node
*top
)
345 struct prefix_ipv4 p
;
346 struct route_table
*table
;
347 struct route_node
*rn
;
349 struct nexthop
*newhop
;
351 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
352 nexthop
->ifindex
= 0;
355 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
357 /* Make lookup prefix. */
358 memset (&p
, 0, sizeof (struct prefix_ipv4
));
360 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
361 p
.prefix
= nexthop
->gate
.ipv4
;
364 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
368 rn
= route_node_match (table
, (struct prefix
*) &p
);
371 route_unlock_node (rn
);
373 /* If lookup self prefix return immidiately. */
377 /* Pick up selected route. */
378 for (match
= rn
->info
; match
; match
= match
->next
)
379 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
382 /* If there is no selected route or matched route is EGP, go up
385 || match
->type
== ZEBRA_ROUTE_BGP
)
389 } while (rn
&& rn
->info
== NULL
);
391 route_lock_node (rn
);
395 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
397 /* Directly point connected route. */
398 newhop
= match
->nexthop
;
399 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
400 nexthop
->ifindex
= newhop
->ifindex
;
404 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
406 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
407 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
408 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
412 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
413 nexthop
->rtype
= newhop
->type
;
414 if (newhop
->type
== NEXTHOP_TYPE_IPV4
||
415 newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
416 nexthop
->rgate
.ipv4
= newhop
->gate
.ipv4
;
417 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
418 || newhop
->type
== NEXTHOP_TYPE_IFNAME
419 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
420 nexthop
->rifindex
= newhop
->ifindex
;
436 /* If force flag is not set, do not modify falgs at all for uninstall
437 the route from FIB. */
439 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
440 struct route_node
*top
)
442 struct prefix_ipv6 p
;
443 struct route_table
*table
;
444 struct route_node
*rn
;
446 struct nexthop
*newhop
;
448 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
449 nexthop
->ifindex
= 0;
452 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
454 /* Make lookup prefix. */
455 memset (&p
, 0, sizeof (struct prefix_ipv6
));
457 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
458 p
.prefix
= nexthop
->gate
.ipv6
;
461 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
465 rn
= route_node_match (table
, (struct prefix
*) &p
);
468 route_unlock_node (rn
);
470 /* If lookup self prefix return immidiately. */
474 /* Pick up selected route. */
475 for (match
= rn
->info
; match
; match
= match
->next
)
476 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
479 /* If there is no selected route or matched route is EGP, go up
482 || match
->type
== ZEBRA_ROUTE_BGP
)
486 } while (rn
&& rn
->info
== NULL
);
488 route_lock_node (rn
);
492 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
494 /* Directly point connected route. */
495 newhop
= match
->nexthop
;
497 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
498 nexthop
->ifindex
= newhop
->ifindex
;
502 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
504 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
505 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
506 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
510 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
511 nexthop
->rtype
= newhop
->type
;
512 if (newhop
->type
== NEXTHOP_TYPE_IPV6
513 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
514 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
515 nexthop
->rgate
.ipv6
= newhop
->gate
.ipv6
;
516 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
517 || newhop
->type
== NEXTHOP_TYPE_IFNAME
518 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
519 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
520 nexthop
->rifindex
= newhop
->ifindex
;
534 #endif /* HAVE_IPV6 */
537 rib_match_ipv4 (struct in_addr addr
)
539 struct prefix_ipv4 p
;
540 struct route_table
*table
;
541 struct route_node
*rn
;
543 struct nexthop
*newhop
;
546 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
550 memset (&p
, 0, sizeof (struct prefix_ipv4
));
552 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
555 rn
= route_node_match (table
, (struct prefix
*) &p
);
559 route_unlock_node (rn
);
561 /* Pick up selected route. */
562 for (match
= rn
->info
; match
; match
= match
->next
)
563 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
566 /* If there is no selected route or matched route is EGP, go up
569 || match
->type
== ZEBRA_ROUTE_BGP
)
573 } while (rn
&& rn
->info
== NULL
);
575 route_lock_node (rn
);
579 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
580 /* Directly point connected route. */
584 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
585 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
595 rib_lookup_ipv4 (struct prefix_ipv4
*p
)
597 struct route_table
*table
;
598 struct route_node
*rn
;
600 struct nexthop
*nexthop
;
603 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
607 rn
= route_node_lookup (table
, (struct prefix
*) p
);
609 /* No route for this prefix. */
614 route_unlock_node (rn
);
616 /* Pick up selected route. */
617 for (match
= rn
->info
; match
; match
= match
->next
)
618 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
621 if (! match
|| match
->type
== ZEBRA_ROUTE_BGP
)
624 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
627 for (nexthop
= match
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
628 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
635 * This clone function, unlike its original rib_lookup_ipv4(), checks
636 * if specified IPv4 route record (prefix/mask -> gate) exists in
637 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
641 * 0: exact match found
642 * 1: a match was found with a different gate
643 * 2: connected route found
644 * 3: no matches found
647 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
)
649 struct route_table
*table
;
650 struct route_node
*rn
;
652 struct nexthop
*nexthop
;
655 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
657 return ZEBRA_RIB_LOOKUP_ERROR
;
659 /* Scan the RIB table for exactly matching RIB entry. */
660 rn
= route_node_lookup (table
, (struct prefix
*) p
);
662 /* No route for this prefix. */
664 return ZEBRA_RIB_NOTFOUND
;
667 route_unlock_node (rn
);
669 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
670 for (match
= rn
->info
; match
; match
= match
->next
)
672 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
674 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
678 /* None such found :( */
680 return ZEBRA_RIB_NOTFOUND
;
682 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
683 return ZEBRA_RIB_FOUND_CONNECTED
;
685 /* Ok, we have a cood candidate, let's check it's nexthop list... */
686 for (nexthop
= match
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
687 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
689 /* We are happy with either direct or recursive hexthop */
690 if (nexthop
->gate
.ipv4
.s_addr
== qgate
->sin
.sin_addr
.s_addr
||
691 nexthop
->rgate
.ipv4
.s_addr
== qgate
->sin
.sin_addr
.s_addr
)
692 return ZEBRA_RIB_FOUND_EXACT
;
695 if (IS_ZEBRA_DEBUG_RIB
)
697 char gate_buf
[INET_ADDRSTRLEN
], rgate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
698 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
699 inet_ntop (AF_INET
, &nexthop
->rgate
.ipv4
.s_addr
, rgate_buf
, INET_ADDRSTRLEN
);
700 inet_ntop (AF_INET
, &qgate
->sin
.sin_addr
.s_addr
, qgate_buf
, INET_ADDRSTRLEN
);
701 zlog_debug ("%s: qgate == %s, gate == %s, rgate == %s", __func__
, qgate_buf
, gate_buf
, rgate_buf
);
703 return ZEBRA_RIB_FOUND_NOGATE
;
707 return ZEBRA_RIB_NOTFOUND
;
712 rib_match_ipv6 (struct in6_addr
*addr
)
714 struct prefix_ipv6 p
;
715 struct route_table
*table
;
716 struct route_node
*rn
;
718 struct nexthop
*newhop
;
721 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
725 memset (&p
, 0, sizeof (struct prefix_ipv6
));
727 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
728 IPV6_ADDR_COPY (&p
.prefix
, addr
);
730 rn
= route_node_match (table
, (struct prefix
*) &p
);
734 route_unlock_node (rn
);
736 /* Pick up selected route. */
737 for (match
= rn
->info
; match
; match
= match
->next
)
738 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
741 /* If there is no selected route or matched route is EGP, go up
744 || match
->type
== ZEBRA_ROUTE_BGP
)
748 } while (rn
&& rn
->info
== NULL
);
750 route_lock_node (rn
);
754 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
755 /* Directly point connected route. */
759 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
760 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
768 #endif /* HAVE_IPV6 */
770 #define RIB_SYSTEM_ROUTE(R) \
771 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
773 /* This function verifies reachability of one given nexthop, which can be
774 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
775 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
776 * nexthop->ifindex will be updated appropriately as well.
777 * An existing route map can turn (otherwise active) nexthop into inactive, but
780 * The return value is the final value of 'ACTIVE' flag.
784 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
785 struct nexthop
*nexthop
, int set
)
787 struct interface
*ifp
;
788 route_map_result_t ret
= RMAP_MATCH
;
789 extern char *proto_rm
[AFI_MAX
][ZEBRA_ROUTE_MAX
+1];
790 struct route_map
*rmap
;
794 switch (nexthop
->type
)
796 case NEXTHOP_TYPE_IFINDEX
:
797 ifp
= if_lookup_by_index (nexthop
->ifindex
);
798 if (ifp
&& if_is_up (ifp
))
799 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
801 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
803 case NEXTHOP_TYPE_IPV6_IFNAME
:
805 case NEXTHOP_TYPE_IFNAME
:
806 ifp
= if_lookup_by_name (nexthop
->ifname
);
807 if (ifp
&& if_is_up (ifp
))
810 nexthop
->ifindex
= ifp
->ifindex
;
811 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
816 nexthop
->ifindex
= 0;
817 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
820 case NEXTHOP_TYPE_IPV4
:
821 case NEXTHOP_TYPE_IPV4_IFINDEX
:
823 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
824 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
826 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
829 case NEXTHOP_TYPE_IPV6
:
831 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
832 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
834 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
836 case NEXTHOP_TYPE_IPV6_IFINDEX
:
838 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
840 ifp
= if_lookup_by_index (nexthop
->ifindex
);
841 if (ifp
&& if_is_up (ifp
))
842 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
844 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
848 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
849 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
851 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
854 #endif /* HAVE_IPV6 */
855 case NEXTHOP_TYPE_BLACKHOLE
:
856 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
861 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
864 if (RIB_SYSTEM_ROUTE(rib
) ||
865 (family
== AFI_IP
&& rn
->p
.family
!= AF_INET
) ||
866 (family
== AFI_IP6
&& rn
->p
.family
!= AF_INET6
))
867 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
870 if (rib
->type
>= 0 && rib
->type
< ZEBRA_ROUTE_MAX
&&
871 proto_rm
[family
][rib
->type
])
872 rmap
= route_map_lookup_by_name (proto_rm
[family
][rib
->type
]);
873 if (!rmap
&& proto_rm
[family
][ZEBRA_ROUTE_MAX
])
874 rmap
= route_map_lookup_by_name (proto_rm
[family
][ZEBRA_ROUTE_MAX
]);
876 ret
= route_map_apply(rmap
, &rn
->p
, RMAP_ZEBRA
, nexthop
);
879 if (ret
== RMAP_DENYMATCH
)
880 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
881 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
884 /* Iterate over all nexthops of the given RIB entry and refresh their
885 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
886 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
887 * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
888 * transparently passed to nexthop_active_check().
890 * Return value is the new number of active nexthops.
894 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
896 struct nexthop
*nexthop
;
897 int prev_active
, new_active
;
899 rib
->nexthop_active_num
= 0;
900 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
902 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
904 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
905 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
906 rib
->nexthop_active_num
++;
907 if (prev_active
!= new_active
)
908 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
910 return rib
->nexthop_active_num
;
916 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
)
919 struct nexthop
*nexthop
;
921 switch (PREFIX_FAMILY (&rn
->p
))
924 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
928 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
930 #endif /* HAVE_IPV6 */
933 /* This condition is never met, if we are using rt_socket.c */
936 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
937 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
941 /* Uninstall the route from kernel. */
943 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
946 struct nexthop
*nexthop
;
948 switch (PREFIX_FAMILY (&rn
->p
))
951 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
955 if (IS_ZEBRA_DEBUG_RIB
)
956 zlog_debug ("%s: calling kernel_delete_ipv4 (%p, %p)", __func__
, rn
, rib
);
957 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
959 #endif /* HAVE_IPV6 */
962 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
963 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
968 /* Uninstall the route from kernel. */
970 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
972 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
974 redistribute_delete (&rn
->p
, rib
);
975 if (! RIB_SYSTEM_ROUTE (rib
))
976 rib_uninstall_kernel (rn
, rib
);
977 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
981 static void rib_unlink (struct route_node
*, struct rib
*);
983 /* Core function for processing routing information base. */
984 static wq_item_status
985 rib_process (struct work_queue
*wq
, void *data
)
989 struct rib
*fib
= NULL
;
990 struct rib
*select
= NULL
;
991 struct rib
*del
= NULL
;
992 struct route_node
*rn
= data
;
994 struct nexthop
*nexthop
= NULL
;
995 char buf
[INET6_ADDRSTRLEN
];
999 if (IS_ZEBRA_DEBUG_RIB
|| IS_ZEBRA_DEBUG_RIB_Q
)
1000 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1002 for (rib
= rn
->info
; rib
; rib
= next
)
1004 /* The next pointer is saved, because current pointer
1005 * may be passed to rib_unlink() in the middle of iteration.
1009 /* Currently installed rib. */
1010 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1012 assert (fib
== NULL
);
1016 /* Unlock removed routes, so they'll be freed, bar the FIB entry,
1017 * which we need to do do further work with below.
1019 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1023 if (IS_ZEBRA_DEBUG_RIB
)
1024 zlog_debug ("%s: %s/%d: rn %p, removing rib %p", __func__
,
1025 buf
, rn
->p
.prefixlen
, rn
, rib
);
1026 rib_unlink (rn
, rib
);
1034 /* Skip unreachable nexthop. */
1035 if (! nexthop_active_update (rn
, rib
, 0))
1038 /* Infinit distance. */
1039 if (rib
->distance
== DISTANCE_INFINITY
)
1042 /* Newly selected rib, the common case. */
1049 /* filter route selection in following order:
1050 * - connected beats other types
1051 * - lower distance beats higher
1052 * - lower metric beats higher for equal distance
1053 * - last, hence oldest, route wins tie break.
1056 /* Connected routes. Pick the last connected
1057 * route of the set of lowest metric connected routes.
1059 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1061 if (select
->type
!= ZEBRA_ROUTE_CONNECT
1062 || rib
->metric
<= select
->metric
)
1066 else if (select
->type
== ZEBRA_ROUTE_CONNECT
)
1069 /* higher distance loses */
1070 if (rib
->distance
> select
->distance
)
1074 if (rib
->distance
< select
->distance
)
1080 /* metric tie-breaks equal distance */
1081 if (rib
->metric
<= select
->metric
)
1083 } /* for (rib = rn->info; rib; rib = next) */
1085 /* After the cycle is finished, the following pointers will be set:
1086 * select --- the winner RIB entry, if any was found, otherwise NULL
1087 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1088 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1092 /* Same RIB entry is selected. Update FIB and finish. */
1093 if (select
&& select
== fib
)
1095 if (IS_ZEBRA_DEBUG_RIB
)
1096 zlog_debug ("%s: %s/%d: Updating existing route, select %p, fib %p",
1097 __func__
, buf
, rn
->p
.prefixlen
, select
, fib
);
1098 if (CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
1100 redistribute_delete (&rn
->p
, select
);
1101 if (! RIB_SYSTEM_ROUTE (select
))
1102 rib_uninstall_kernel (rn
, select
);
1104 /* Set real nexthop. */
1105 nexthop_active_update (rn
, select
, 1);
1107 if (! RIB_SYSTEM_ROUTE (select
))
1108 rib_install_kernel (rn
, select
);
1109 redistribute_add (&rn
->p
, select
);
1111 else if (! RIB_SYSTEM_ROUTE (select
))
1113 /* Housekeeping code to deal with
1114 race conditions in kernel with linux
1115 netlink reporting interface up before IPv4 or IPv6 protocol
1116 is ready to add routes.
1117 This makes sure the routes are IN the kernel.
1120 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1121 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1127 rib_install_kernel (rn
, select
);
1132 /* At this point we either haven't found the best RIB entry or it is
1133 * different from what we currently intend to flag with SELECTED. In both
1134 * cases, if a RIB block is present in FIB, it should be withdrawn.
1138 if (IS_ZEBRA_DEBUG_RIB
)
1139 zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__
,
1140 buf
, rn
->p
.prefixlen
, fib
);
1141 redistribute_delete (&rn
->p
, fib
);
1142 if (! RIB_SYSTEM_ROUTE (fib
))
1143 rib_uninstall_kernel (rn
, fib
);
1144 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1146 /* Set real nexthop. */
1147 nexthop_active_update (rn
, fib
, 1);
1150 /* Regardless of some RIB entry being SELECTED or not before, now we can
1151 * tell, that if a new winner exists, FIB is still not updated with this
1152 * data, but ready to be.
1156 if (IS_ZEBRA_DEBUG_RIB
)
1157 zlog_debug ("%s: %s/%d: Adding route, select %p", __func__
, buf
,
1158 rn
->p
.prefixlen
, select
);
1159 /* Set real nexthop. */
1160 nexthop_active_update (rn
, select
, 1);
1162 if (! RIB_SYSTEM_ROUTE (select
))
1163 rib_install_kernel (rn
, select
);
1164 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1165 redistribute_add (&rn
->p
, select
);
1168 /* FIB route was removed, should be deleted */
1171 if (IS_ZEBRA_DEBUG_RIB
)
1172 zlog_debug ("%s: %s/%d: Deleting fib %p, rn %p", __func__
, buf
,
1173 rn
->p
.prefixlen
, del
, rn
);
1174 rib_unlink (rn
, del
);
1178 if (IS_ZEBRA_DEBUG_RIB_Q
)
1179 zlog_debug ("%s: %s/%d: rn %p dequeued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1181 UNSET_FLAG (((struct rib
*)rn
->info
)->rn_status
, RIB_ROUTE_QUEUED
);
1182 route_unlock_node (rn
); /* rib queue lock */
1186 /* Add route_node to work queue and schedule processing */
1188 rib_queue_add (struct zebra_t
*zebra
, struct route_node
*rn
)
1190 char buf
[INET_ADDRSTRLEN
];
1191 assert (zebra
&& rn
);
1193 if (IS_ZEBRA_DEBUG_RIB_Q
)
1194 inet_ntop (AF_INET
, &rn
->p
.u
.prefix
, buf
, INET_ADDRSTRLEN
);
1196 /* Pointless to queue a route_node with no RIB entries to add or remove */
1199 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1200 __func__
, rn
, rn
->lock
);
1201 zlog_backtrace(LOG_DEBUG
);
1205 /* Route-table node already queued, so nothing to do */
1206 if (CHECK_FLAG (((struct rib
*)rn
->info
)->rn_status
, RIB_ROUTE_QUEUED
))
1208 if (IS_ZEBRA_DEBUG_RIB_Q
)
1209 zlog_debug ("%s: %s/%d: rn %p already queued", __func__
, buf
,
1210 rn
->p
.prefixlen
, rn
);
1214 route_lock_node (rn
); /* rib queue lock */
1216 if (IS_ZEBRA_DEBUG_RIB_Q
)
1217 zlog_info ("%s: %s/%d: work queue added", __func__
, buf
, rn
->p
.prefixlen
);
1221 if (zebra
->ribq
== NULL
)
1223 zlog_err ("%s: work_queue does not exist!", __func__
);
1224 route_unlock_node (rn
);
1228 work_queue_add (zebra
->ribq
, rn
);
1230 SET_FLAG (((struct rib
*)rn
->info
)->rn_status
, RIB_ROUTE_QUEUED
);
1232 if (IS_ZEBRA_DEBUG_RIB_Q
)
1233 zlog_debug ("%s: %s/%d: rn %p queued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1238 /* initialise zebra rib work queue */
1240 rib_queue_init (struct zebra_t
*zebra
)
1244 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1245 "route_node processing")))
1247 zlog_err ("%s: could not initialise work queue!", __func__
);
1251 /* fill in the work queue spec */
1252 zebra
->ribq
->spec
.workfunc
= &rib_process
;
1253 zebra
->ribq
->spec
.errorfunc
= NULL
;
1254 /* XXX: TODO: These should be runtime configurable via vty */
1255 zebra
->ribq
->spec
.max_retries
= 3;
1256 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
1261 /* RIB updates are processed via a queue of pointers to route_nodes.
1263 * The queue length is bounded by the maximal size of the routing table,
1264 * as a route_node will not be requeued, if already queued.
1266 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
1267 * state, or static_install_ipv{4,6} (when an existing RIB is updated)
1268 * and then submit route_node to queue for best-path selection later.
1269 * Order of add/delete state changes are preserved for any given RIB.
1271 * Deleted RIBs are reaped during best-path selection.
1274 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
1275 * |-------->| | best RIB, if required
1277 * static_install->|->rib_addqueue...... -> rib_process
1279 * |-------->| |-> rib_unlink
1280 * |-> set RIB_ENTRY_REMOVE |
1281 * rib_delnode (RIB freed)
1284 * Queueing state for a route_node is kept in the head RIB entry, this
1285 * state must be preserved as and when the head RIB entry of a
1286 * route_node is changed by rib_unlink / rib_link. A small complication,
1287 * but saves having to allocate a dedicated object for this.
1289 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1291 * - route_nodes: refcounted by:
1292 * - RIBs attached to route_node:
1293 * - managed by: rib_link/unlink
1294 * - route_node processing queue
1295 * - managed by: rib_addqueue, rib_process.
1299 /* Add RIB to head of the route node. */
1301 rib_link (struct route_node
*rn
, struct rib
*rib
)
1304 char buf
[INET6_ADDRSTRLEN
];
1308 route_lock_node (rn
); /* rn route table reference */
1310 if (IS_ZEBRA_DEBUG_RIB
)
1312 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1313 zlog_debug ("%s: %s/%d: rn %p, rib %p", __func__
,
1314 buf
, rn
->p
.prefixlen
, rn
, rib
);
1320 if (IS_ZEBRA_DEBUG_RIB
)
1321 zlog_debug ("%s: %s/%d: new head, rn_status copied over", __func__
,
1322 buf
, rn
->p
.prefixlen
);
1324 /* Transfer the rn status flags to the new head RIB */
1325 rib
->rn_status
= head
->rn_status
;
1329 rib_queue_add (&zebrad
, rn
);
1333 rib_addnode (struct route_node
*rn
, struct rib
*rib
)
1335 /* RIB node has been un-removed before route-node is processed.
1336 * route_node must hence already be on the queue for processing..
1338 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1340 if (IS_ZEBRA_DEBUG_RIB
)
1342 char buf
[INET6_ADDRSTRLEN
];
1343 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1344 zlog_debug ("%s: %s/%d: rn %p, un-removed rib %p",
1345 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1347 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1354 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
1356 struct nexthop
*nexthop
, *next
;
1357 char buf
[INET6_ADDRSTRLEN
];
1361 if (IS_ZEBRA_DEBUG_RIB
)
1363 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1364 zlog_debug ("%s: %s/%d: rn %p, rib %p",
1365 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1369 rib
->next
->prev
= rib
->prev
;
1372 rib
->prev
->next
= rib
->next
;
1375 rn
->info
= rib
->next
;
1379 if (IS_ZEBRA_DEBUG_RIB
)
1380 zlog_debug ("%s: %s/%d: rn %p, rib %p, new head copy",
1381 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1382 rib
->next
->rn_status
= rib
->rn_status
;
1386 /* free RIB and nexthops */
1387 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= next
)
1389 next
= nexthop
->next
;
1390 nexthop_free (nexthop
);
1392 XFREE (MTYPE_RIB
, rib
);
1394 route_unlock_node (rn
); /* rn route table reference */
1398 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
1400 if (IS_ZEBRA_DEBUG_RIB
)
1402 char buf
[INET6_ADDRSTRLEN
];
1403 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1404 zlog_debug ("%s: %s/%d: rn %p, rib %p, removing", __func__
,
1405 buf
, rn
->p
.prefixlen
, rn
, rib
);
1407 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1408 rib_queue_add (&zebrad
, rn
);
1412 rib_add_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1413 struct in_addr
*gate
, struct in_addr
*src
,
1414 unsigned int ifindex
, u_int32_t vrf_id
,
1415 u_int32_t metric
, u_char distance
)
1418 struct rib
*same
= NULL
;
1419 struct route_table
*table
;
1420 struct route_node
*rn
;
1421 struct nexthop
*nexthop
;
1424 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1428 /* Make it sure prefixlen is applied to the prefix. */
1429 apply_mask_ipv4 (p
);
1431 /* Set default distance by route type. */
1434 distance
= route_info
[type
].distance
;
1436 /* iBGP distance is 200. */
1437 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1441 /* Lookup route node.*/
1442 rn
= route_node_get (table
, (struct prefix
*) p
);
1444 /* If same type of route are installed, treat it as a implicit
1446 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1448 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1451 if (rib
->type
!= type
)
1453 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
1458 /* Duplicate connected route comes in. */
1459 else if ((nexthop
= rib
->nexthop
) &&
1460 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
1461 nexthop
->ifindex
== ifindex
&&
1462 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1469 /* Allocate new rib structure. */
1470 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1472 rib
->distance
= distance
;
1474 rib
->metric
= metric
;
1475 rib
->table
= vrf_id
;
1476 rib
->nexthop_num
= 0;
1477 rib
->uptime
= time (NULL
);
1479 /* Nexthop settings. */
1483 nexthop_ipv4_ifindex_add (rib
, gate
, src
, ifindex
);
1485 nexthop_ipv4_add (rib
, gate
, src
);
1488 nexthop_ifindex_add (rib
, ifindex
);
1490 /* If this route is kernel route, set FIB flag to the route. */
1491 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1492 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1493 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1495 /* Link new rib to node.*/
1496 if (IS_ZEBRA_DEBUG_RIB
)
1497 zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__
, rn
, rib
);
1498 rib_addnode (rn
, rib
);
1500 /* Free implicit route.*/
1503 if (IS_ZEBRA_DEBUG_RIB
)
1504 zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__
, rn
, rib
);
1505 rib_delnode (rn
, same
);
1508 route_unlock_node (rn
);
1512 /* This function dumps the contents of a given RIB entry into
1513 * standard debug log. Calling function name and IP prefix in
1514 * question are passed as 1st and 2nd arguments.
1517 void rib_dump (const char * func
, const struct prefix_ipv4
* p
, const struct rib
* rib
)
1519 char straddr1
[INET_ADDRSTRLEN
], straddr2
[INET_ADDRSTRLEN
];
1520 struct nexthop
*nexthop
;
1522 inet_ntop (AF_INET
, &p
->prefix
, straddr1
, INET_ADDRSTRLEN
);
1523 zlog_debug ("%s: dumping RIB entry %p for %s/%d", func
, rib
, straddr1
, p
->prefixlen
);
1526 "%s: refcnt == %lu, uptime == %u, type == %u, table == %d",
1535 "%s: metric == %u, distance == %u, flags == %u, status == %u",
1544 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
1547 rib
->nexthop_active_num
,
1548 rib
->nexthop_fib_num
1550 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1552 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, straddr1
, INET_ADDRSTRLEN
);
1553 inet_ntop (AF_INET
, &nexthop
->rgate
.ipv4
.s_addr
, straddr2
, INET_ADDRSTRLEN
);
1556 "%s: NH %s (%s) with flags %s%s%s",
1560 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
1561 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
1562 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
1565 zlog_debug ("%s: dump complete", func
);
1568 /* This is an exported helper to rtm_read() to dump the strange
1569 * RIB entry found by rib_lookup_ipv4_route()
1572 void rib_lookup_and_dump (struct prefix_ipv4
* p
)
1574 struct route_table
*table
;
1575 struct route_node
*rn
;
1577 char prefix_buf
[INET_ADDRSTRLEN
];
1580 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1583 zlog_err ("%s: vrf_table() returned NULL", __func__
);
1587 inet_ntop (AF_INET
, &p
->prefix
.s_addr
, prefix_buf
, INET_ADDRSTRLEN
);
1588 /* Scan the RIB table for exactly matching RIB entry. */
1589 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1591 /* No route for this prefix. */
1594 zlog_debug ("%s: lookup failed for %s/%d", __func__
, prefix_buf
, p
->prefixlen
);
1599 route_unlock_node (rn
);
1602 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1606 "%s: rn %p, rib %p: %s, %s",
1610 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
1611 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
1613 rib_dump (__func__
, p
, rib
);
1618 rib_add_ipv4_multipath (struct prefix_ipv4
*p
, struct rib
*rib
)
1620 struct route_table
*table
;
1621 struct route_node
*rn
;
1623 struct nexthop
*nexthop
;
1626 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1629 /* Make it sure prefixlen is applied to the prefix. */
1630 apply_mask_ipv4 (p
);
1632 /* Set default distance by route type. */
1633 if (rib
->distance
== 0)
1635 rib
->distance
= route_info
[rib
->type
].distance
;
1637 /* iBGP distance is 200. */
1638 if (rib
->type
== ZEBRA_ROUTE_BGP
1639 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
1640 rib
->distance
= 200;
1643 /* Lookup route node.*/
1644 rn
= route_node_get (table
, (struct prefix
*) p
);
1646 /* If same type of route are installed, treat it as a implicit
1648 for (same
= rn
->info
; same
; same
= same
->next
)
1650 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
1653 if (same
->type
== rib
->type
&& same
->table
== rib
->table
1654 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
1658 /* If this route is kernel route, set FIB flag to the route. */
1659 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
1660 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1661 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1663 /* Link new rib to node.*/
1664 rib_addnode (rn
, rib
);
1665 if (IS_ZEBRA_DEBUG_RIB
)
1667 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
1669 rib_dump (__func__
, p
, rib
);
1672 /* Free implicit route.*/
1675 if (IS_ZEBRA_DEBUG_RIB
)
1677 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
1678 __func__
, rn
, same
);
1679 rib_dump (__func__
, p
, same
);
1681 rib_delnode (rn
, same
);
1684 route_unlock_node (rn
);
1688 /* XXX factor with rib_delete_ipv6 */
1690 rib_delete_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1691 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1693 struct route_table
*table
;
1694 struct route_node
*rn
;
1696 struct rib
*fib
= NULL
;
1697 struct rib
*same
= NULL
;
1698 struct nexthop
*nexthop
;
1703 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1708 apply_mask_ipv4 (p
);
1710 if (IS_ZEBRA_DEBUG_KERNEL
&& gate
)
1711 zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
1712 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1717 /* Lookup route node. */
1718 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1721 if (IS_ZEBRA_DEBUG_KERNEL
)
1724 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
1725 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1727 inet_ntop (AF_INET
, gate
, buf2
, BUFSIZ
),
1730 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
1731 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1735 return ZEBRA_ERR_RTNOEXIST
;
1738 /* Lookup same type route. */
1739 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1741 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1744 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1747 if (rib
->type
!= type
)
1749 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
1750 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&& nexthop
->ifindex
== ifindex
)
1755 route_unlock_node (rn
);
1756 route_unlock_node (rn
);
1762 /* Make sure that the route found has the same gateway. */
1763 else if (gate
== NULL
||
1764 ((nexthop
= rib
->nexthop
) &&
1765 (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
1766 IPV4_ADDR_SAME (&nexthop
->rgate
.ipv4
, gate
))))
1773 /* If same type of route can't be found and this message is from
1777 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
1780 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1781 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1783 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1787 if (IS_ZEBRA_DEBUG_KERNEL
)
1790 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
1791 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1793 inet_ntop (AF_INET
, gate
, buf2
, BUFSIZ
),
1797 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
1798 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1803 route_unlock_node (rn
);
1804 return ZEBRA_ERR_RTNOEXIST
;
1809 rib_delnode (rn
, same
);
1811 route_unlock_node (rn
);
1815 /* Install static route into rib. */
1817 static_install_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
1820 struct route_node
*rn
;
1821 struct route_table
*table
;
1824 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1828 /* Lookup existing route */
1829 rn
= route_node_get (table
, p
);
1830 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1832 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1835 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1841 /* Same distance static route is there. Update it with new
1843 route_unlock_node (rn
);
1846 case STATIC_IPV4_GATEWAY
:
1847 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
1849 case STATIC_IPV4_IFNAME
:
1850 nexthop_ifname_add (rib
, si
->gate
.ifname
);
1852 case STATIC_IPV4_BLACKHOLE
:
1853 nexthop_blackhole_add (rib
);
1856 rib_queue_add (&zebrad
, rn
);
1860 /* This is new static route. */
1861 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1863 rib
->type
= ZEBRA_ROUTE_STATIC
;
1864 rib
->distance
= si
->distance
;
1866 rib
->nexthop_num
= 0;
1870 case STATIC_IPV4_GATEWAY
:
1871 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
1873 case STATIC_IPV4_IFNAME
:
1874 nexthop_ifname_add (rib
, si
->gate
.ifname
);
1876 case STATIC_IPV4_BLACKHOLE
:
1877 nexthop_blackhole_add (rib
);
1881 /* Save the flags of this static routes (reject, blackhole) */
1882 rib
->flags
= si
->flags
;
1884 /* Link this rib to the tree. */
1885 rib_addnode (rn
, rib
);
1890 static_ipv4_nexthop_same (struct nexthop
*nexthop
, struct static_ipv4
*si
)
1892 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
1893 && si
->type
== STATIC_IPV4_GATEWAY
1894 && IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &si
->gate
.ipv4
))
1896 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
1897 && si
->type
== STATIC_IPV4_IFNAME
1898 && strcmp (nexthop
->ifname
, si
->gate
.ifname
) == 0)
1900 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
1901 && si
->type
== STATIC_IPV4_BLACKHOLE
)
1906 /* Uninstall static route from RIB. */
1908 static_uninstall_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
1910 struct route_node
*rn
;
1912 struct nexthop
*nexthop
;
1913 struct route_table
*table
;
1916 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1920 /* Lookup existing route with type and distance. */
1921 rn
= route_node_lookup (table
, p
);
1925 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1927 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1930 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
1936 route_unlock_node (rn
);
1940 /* Lookup nexthop. */
1941 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1942 if (static_ipv4_nexthop_same (nexthop
, si
))
1945 /* Can't find nexthop. */
1948 route_unlock_node (rn
);
1952 /* Check nexthop. */
1953 if (rib
->nexthop_num
== 1)
1954 rib_delnode (rn
, rib
);
1957 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1958 rib_uninstall (rn
, rib
);
1959 nexthop_delete (rib
, nexthop
);
1960 nexthop_free (nexthop
);
1961 rib_queue_add (&zebrad
, rn
);
1964 route_unlock_node (rn
);
1967 /* Add static route into static route configuration. */
1969 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
1970 u_char flags
, u_char distance
, u_int32_t vrf_id
)
1973 struct route_node
*rn
;
1974 struct static_ipv4
*si
;
1975 struct static_ipv4
*pp
;
1976 struct static_ipv4
*cp
;
1977 struct static_ipv4
*update
= NULL
;
1978 struct route_table
*stable
;
1981 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
1985 /* Lookup static route prefix. */
1986 rn
= route_node_get (stable
, p
);
1990 type
= STATIC_IPV4_GATEWAY
;
1992 type
= STATIC_IPV4_IFNAME
;
1994 type
= STATIC_IPV4_BLACKHOLE
;
1996 /* Do nothing if there is a same static route. */
1997 for (si
= rn
->info
; si
; si
= si
->next
)
1999 if (type
== si
->type
2000 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2001 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2003 if (distance
== si
->distance
)
2005 route_unlock_node (rn
);
2013 /* Distance changed. */
2015 static_delete_ipv4 (p
, gate
, ifname
, update
->distance
, vrf_id
);
2017 /* Make new static route structure. */
2018 si
= XMALLOC (MTYPE_STATIC_IPV4
, sizeof (struct static_ipv4
));
2019 memset (si
, 0, sizeof (struct static_ipv4
));
2022 si
->distance
= distance
;
2026 si
->gate
.ipv4
= *gate
;
2028 si
->gate
.ifname
= XSTRDUP (0, ifname
);
2030 /* Add new static route information to the tree with sort by
2031 distance value and gateway address. */
2032 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2034 if (si
->distance
< cp
->distance
)
2036 if (si
->distance
> cp
->distance
)
2038 if (si
->type
== STATIC_IPV4_GATEWAY
&& cp
->type
== STATIC_IPV4_GATEWAY
)
2040 if (ntohl (si
->gate
.ipv4
.s_addr
) < ntohl (cp
->gate
.ipv4
.s_addr
))
2042 if (ntohl (si
->gate
.ipv4
.s_addr
) > ntohl (cp
->gate
.ipv4
.s_addr
))
2047 /* Make linked list. */
2057 /* Install into rib. */
2058 static_install_ipv4 (p
, si
);
2063 /* Delete static route from static route configuration. */
2065 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2066 u_char distance
, u_int32_t vrf_id
)
2069 struct route_node
*rn
;
2070 struct static_ipv4
*si
;
2071 struct route_table
*stable
;
2074 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2078 /* Lookup static route prefix. */
2079 rn
= route_node_lookup (stable
, p
);
2085 type
= STATIC_IPV4_GATEWAY
;
2087 type
= STATIC_IPV4_IFNAME
;
2089 type
= STATIC_IPV4_BLACKHOLE
;
2091 /* Find same static route is the tree */
2092 for (si
= rn
->info
; si
; si
= si
->next
)
2093 if (type
== si
->type
2094 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2095 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2098 /* Can't find static route. */
2101 route_unlock_node (rn
);
2105 /* Install into rib. */
2106 static_uninstall_ipv4 (p
, si
);
2108 /* Unlink static route from linked list. */
2110 si
->prev
->next
= si
->next
;
2112 rn
->info
= si
->next
;
2114 si
->next
->prev
= si
->prev
;
2115 route_unlock_node (rn
);
2117 /* Free static route configuration. */
2119 XFREE (0, si
->gate
.ifname
);
2120 XFREE (MTYPE_STATIC_IPV4
, si
);
2122 route_unlock_node (rn
);
2130 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
2131 struct in6_addr
*gate
, unsigned int ifindex
, int table
)
2133 if (type
== ZEBRA_ROUTE_CONNECT
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)) {
2134 #if defined (MUSICA) || defined (LINUX)
2135 /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
2136 if (p
->prefixlen
== 96)
2141 if (type
== ZEBRA_ROUTE_KERNEL
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)
2142 && p
->prefixlen
== 96 && gate
&& IN6_IS_ADDR_UNSPECIFIED (gate
))
2144 kernel_delete_ipv6_old (p
, gate
, ifindex
, 0, table
);
2151 rib_add_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2152 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
,
2153 u_int32_t metric
, u_char distance
)
2156 struct rib
*same
= NULL
;
2157 struct route_table
*table
;
2158 struct route_node
*rn
;
2159 struct nexthop
*nexthop
;
2162 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2166 /* Make sure mask is applied. */
2167 apply_mask_ipv6 (p
);
2169 /* Set default distance by route type. */
2171 distance
= route_info
[type
].distance
;
2173 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2176 /* Filter bogus route. */
2177 if (rib_bogus_ipv6 (type
, p
, gate
, ifindex
, 0))
2180 /* Lookup route node.*/
2181 rn
= route_node_get (table
, (struct prefix
*) p
);
2183 /* If same type of route are installed, treat it as a implicit
2185 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2187 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2190 if (rib
->type
!= type
)
2192 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2197 else if ((nexthop
= rib
->nexthop
) &&
2198 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2199 nexthop
->ifindex
== ifindex
)
2206 /* Allocate new rib structure. */
2207 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2210 rib
->distance
= distance
;
2212 rib
->metric
= metric
;
2213 rib
->table
= vrf_id
;
2214 rib
->nexthop_num
= 0;
2215 rib
->uptime
= time (NULL
);
2217 /* Nexthop settings. */
2221 nexthop_ipv6_ifindex_add (rib
, gate
, ifindex
);
2223 nexthop_ipv6_add (rib
, gate
);
2226 nexthop_ifindex_add (rib
, ifindex
);
2228 /* If this route is kernel route, set FIB flag to the route. */
2229 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2230 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2231 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2233 /* Link new rib to node.*/
2234 rib_addnode (rn
, rib
);
2236 /* Free implicit route.*/
2238 rib_delnode (rn
, same
);
2240 route_unlock_node (rn
);
2244 /* XXX factor with rib_delete_ipv6 */
2246 rib_delete_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2247 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
2249 struct route_table
*table
;
2250 struct route_node
*rn
;
2252 struct rib
*fib
= NULL
;
2253 struct rib
*same
= NULL
;
2254 struct nexthop
*nexthop
;
2259 apply_mask_ipv6 (p
);
2262 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2266 /* Lookup route node. */
2267 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2270 if (IS_ZEBRA_DEBUG_KERNEL
)
2273 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
2274 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
2276 inet_ntop (AF_INET6
, gate
, buf2
, BUFSIZ
),
2279 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
2280 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
2284 return ZEBRA_ERR_RTNOEXIST
;
2287 /* Lookup same type route. */
2288 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2290 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2293 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2296 if (rib
->type
!= type
)
2298 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2299 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&& nexthop
->ifindex
== ifindex
)
2304 route_unlock_node (rn
);
2305 route_unlock_node (rn
);
2311 /* Make sure that the route found has the same gateway. */
2312 else if (gate
== NULL
||
2313 ((nexthop
= rib
->nexthop
) &&
2314 (IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
) ||
2315 IPV6_ADDR_SAME (&nexthop
->rgate
.ipv6
, gate
))))
2322 /* If same type of route can't be found and this message is from
2326 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
2329 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2330 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2332 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
2336 if (IS_ZEBRA_DEBUG_KERNEL
)
2339 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
2340 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
2342 inet_ntop (AF_INET6
, gate
, buf2
, BUFSIZ
),
2346 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
2347 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
2352 route_unlock_node (rn
);
2353 return ZEBRA_ERR_RTNOEXIST
;
2358 rib_delnode (rn
, same
);
2360 route_unlock_node (rn
);
2364 /* Install static route into rib. */
2366 static_install_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2369 struct route_table
*table
;
2370 struct route_node
*rn
;
2373 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2377 /* Lookup existing route */
2378 rn
= route_node_get (table
, p
);
2379 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2381 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2384 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2390 /* Same distance static route is there. Update it with new
2392 route_unlock_node (rn
);
2396 case STATIC_IPV6_GATEWAY
:
2397 nexthop_ipv6_add (rib
, &si
->ipv6
);
2399 case STATIC_IPV6_IFNAME
:
2400 nexthop_ifname_add (rib
, si
->ifname
);
2402 case STATIC_IPV6_GATEWAY_IFNAME
:
2403 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2406 rib_queue_add (&zebrad
, rn
);
2410 /* This is new static route. */
2411 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2413 rib
->type
= ZEBRA_ROUTE_STATIC
;
2414 rib
->distance
= si
->distance
;
2416 rib
->nexthop_num
= 0;
2420 case STATIC_IPV6_GATEWAY
:
2421 nexthop_ipv6_add (rib
, &si
->ipv6
);
2423 case STATIC_IPV6_IFNAME
:
2424 nexthop_ifname_add (rib
, si
->ifname
);
2426 case STATIC_IPV6_GATEWAY_IFNAME
:
2427 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2431 /* Save the flags of this static routes (reject, blackhole) */
2432 rib
->flags
= si
->flags
;
2434 /* Link this rib to the tree. */
2435 rib_addnode (rn
, rib
);
2440 static_ipv6_nexthop_same (struct nexthop
*nexthop
, struct static_ipv6
*si
)
2442 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
2443 && si
->type
== STATIC_IPV6_GATEWAY
2444 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
))
2446 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2447 && si
->type
== STATIC_IPV6_IFNAME
2448 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2450 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
2451 && si
->type
== STATIC_IPV6_GATEWAY_IFNAME
2452 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
)
2453 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2459 static_uninstall_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2461 struct route_table
*table
;
2462 struct route_node
*rn
;
2464 struct nexthop
*nexthop
;
2467 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2471 /* Lookup existing route with type and distance. */
2472 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2476 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2478 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2481 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2487 route_unlock_node (rn
);
2491 /* Lookup nexthop. */
2492 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2493 if (static_ipv6_nexthop_same (nexthop
, si
))
2496 /* Can't find nexthop. */
2499 route_unlock_node (rn
);
2503 /* Check nexthop. */
2504 if (rib
->nexthop_num
== 1)
2506 rib_delnode (rn
, rib
);
2510 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2511 rib_uninstall (rn
, rib
);
2512 nexthop_delete (rib
, nexthop
);
2513 nexthop_free (nexthop
);
2514 rib_queue_add (&zebrad
, rn
);
2517 route_unlock_node (rn
);
2520 /* Add static route into static route configuration. */
2522 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2523 const char *ifname
, u_char flags
, u_char distance
,
2526 struct route_node
*rn
;
2527 struct static_ipv6
*si
;
2528 struct static_ipv6
*pp
;
2529 struct static_ipv6
*cp
;
2530 struct route_table
*stable
;
2533 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2538 (type
== STATIC_IPV6_GATEWAY
|| type
== STATIC_IPV6_GATEWAY_IFNAME
))
2542 (type
== STATIC_IPV6_GATEWAY_IFNAME
|| type
== STATIC_IPV6_IFNAME
))
2545 /* Lookup static route prefix. */
2546 rn
= route_node_get (stable
, p
);
2548 /* Do nothing if there is a same static route. */
2549 for (si
= rn
->info
; si
; si
= si
->next
)
2551 if (distance
== si
->distance
2553 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2554 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2556 route_unlock_node (rn
);
2561 /* Make new static route structure. */
2562 si
= XMALLOC (MTYPE_STATIC_IPV6
, sizeof (struct static_ipv6
));
2563 memset (si
, 0, sizeof (struct static_ipv6
));
2566 si
->distance
= distance
;
2571 case STATIC_IPV6_GATEWAY
:
2574 case STATIC_IPV6_IFNAME
:
2575 si
->ifname
= XSTRDUP (0, ifname
);
2577 case STATIC_IPV6_GATEWAY_IFNAME
:
2579 si
->ifname
= XSTRDUP (0, ifname
);
2583 /* Add new static route information to the tree with sort by
2584 distance value and gateway address. */
2585 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2587 if (si
->distance
< cp
->distance
)
2589 if (si
->distance
> cp
->distance
)
2593 /* Make linked list. */
2603 /* Install into rib. */
2604 static_install_ipv6 (p
, si
);
2609 /* Delete static route from static route configuration. */
2611 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2612 const char *ifname
, u_char distance
, u_int32_t vrf_id
)
2614 struct route_node
*rn
;
2615 struct static_ipv6
*si
;
2616 struct route_table
*stable
;
2619 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2623 /* Lookup static route prefix. */
2624 rn
= route_node_lookup (stable
, p
);
2628 /* Find same static route is the tree */
2629 for (si
= rn
->info
; si
; si
= si
->next
)
2630 if (distance
== si
->distance
2632 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2633 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2636 /* Can't find static route. */
2639 route_unlock_node (rn
);
2643 /* Install into rib. */
2644 static_uninstall_ipv6 (p
, si
);
2646 /* Unlink static route from linked list. */
2648 si
->prev
->next
= si
->next
;
2650 rn
->info
= si
->next
;
2652 si
->next
->prev
= si
->prev
;
2654 /* Free static route configuration. */
2656 XFREE (0, si
->ifname
);
2657 XFREE (MTYPE_STATIC_IPV6
, si
);
2661 #endif /* HAVE_IPV6 */
2663 /* RIB update function. */
2667 struct route_node
*rn
;
2668 struct route_table
*table
;
2670 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2672 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2674 rib_queue_add (&zebrad
, rn
);
2676 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2678 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2680 rib_queue_add (&zebrad
, rn
);
2683 /* Interface goes up. */
2685 rib_if_up (struct interface
*ifp
)
2690 /* Interface goes down. */
2692 rib_if_down (struct interface
*ifp
)
2697 /* Remove all routes which comes from non main table. */
2699 rib_weed_table (struct route_table
*table
)
2701 struct route_node
*rn
;
2706 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2707 for (rib
= rn
->info
; rib
; rib
= next
)
2711 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2714 if (rib
->table
!= zebrad
.rtm_table_default
&&
2715 rib
->table
!= RT_TABLE_MAIN
)
2716 rib_delnode (rn
, rib
);
2720 /* Delete all routes from non main table. */
2722 rib_weed_tables (void)
2724 rib_weed_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2725 rib_weed_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2728 /* Delete self installed routes after zebra is relaunched. */
2730 rib_sweep_table (struct route_table
*table
)
2732 struct route_node
*rn
;
2738 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2739 for (rib
= rn
->info
; rib
; rib
= next
)
2743 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2746 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
2747 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
2749 ret
= rib_uninstall_kernel (rn
, rib
);
2751 rib_delnode (rn
, rib
);
2756 /* Sweep all RIB tables. */
2758 rib_sweep_route (void)
2760 rib_sweep_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2761 rib_sweep_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2764 /* Close RIB and clean up kernel routes. */
2766 rib_close_table (struct route_table
*table
)
2768 struct route_node
*rn
;
2772 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2773 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2775 if (! RIB_SYSTEM_ROUTE (rib
)
2776 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2777 rib_uninstall_kernel (rn
, rib
);
2781 /* Close all RIB tables. */
2785 rib_close_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2786 rib_close_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2789 /* Routing information base initialize. */
2793 rib_queue_init (&zebrad
);
2794 /* VRF initialization. */