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_operative(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_operative(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_operative(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. */
985 rib_process (struct route_node
*rn
)
989 struct rib
*fib
= NULL
;
990 struct rib
*select
= NULL
;
991 struct rib
*del
= NULL
;
993 struct nexthop
*nexthop
= NULL
;
994 char buf
[INET6_ADDRSTRLEN
];
998 if (IS_ZEBRA_DEBUG_RIB
|| IS_ZEBRA_DEBUG_RIB_Q
)
999 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1001 for (rib
= rn
->info
; rib
; rib
= next
)
1003 /* The next pointer is saved, because current pointer
1004 * may be passed to rib_unlink() in the middle of iteration.
1008 /* Currently installed rib. */
1009 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1011 assert (fib
== NULL
);
1015 /* Unlock removed routes, so they'll be freed, bar the FIB entry,
1016 * which we need to do do further work with below.
1018 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1022 if (IS_ZEBRA_DEBUG_RIB
)
1023 zlog_debug ("%s: %s/%d: rn %p, removing rib %p", __func__
,
1024 buf
, rn
->p
.prefixlen
, rn
, rib
);
1025 rib_unlink (rn
, rib
);
1033 /* Skip unreachable nexthop. */
1034 if (! nexthop_active_update (rn
, rib
, 0))
1037 /* Infinit distance. */
1038 if (rib
->distance
== DISTANCE_INFINITY
)
1041 /* Newly selected rib, the common case. */
1048 /* filter route selection in following order:
1049 * - connected beats other types
1050 * - lower distance beats higher
1051 * - lower metric beats higher for equal distance
1052 * - last, hence oldest, route wins tie break.
1055 /* Connected routes. Pick the last connected
1056 * route of the set of lowest metric connected routes.
1058 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1060 if (select
->type
!= ZEBRA_ROUTE_CONNECT
1061 || rib
->metric
<= select
->metric
)
1065 else if (select
->type
== ZEBRA_ROUTE_CONNECT
)
1068 /* higher distance loses */
1069 if (rib
->distance
> select
->distance
)
1073 if (rib
->distance
< select
->distance
)
1079 /* metric tie-breaks equal distance */
1080 if (rib
->metric
<= select
->metric
)
1082 } /* for (rib = rn->info; rib; rib = next) */
1084 /* After the cycle is finished, the following pointers will be set:
1085 * select --- the winner RIB entry, if any was found, otherwise NULL
1086 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1087 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1091 /* Same RIB entry is selected. Update FIB and finish. */
1092 if (select
&& select
== fib
)
1094 if (IS_ZEBRA_DEBUG_RIB
)
1095 zlog_debug ("%s: %s/%d: Updating existing route, select %p, fib %p",
1096 __func__
, buf
, rn
->p
.prefixlen
, select
, fib
);
1097 if (CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
1099 redistribute_delete (&rn
->p
, select
);
1100 if (! RIB_SYSTEM_ROUTE (select
))
1101 rib_uninstall_kernel (rn
, select
);
1103 /* Set real nexthop. */
1104 nexthop_active_update (rn
, select
, 1);
1106 if (! RIB_SYSTEM_ROUTE (select
))
1107 rib_install_kernel (rn
, select
);
1108 redistribute_add (&rn
->p
, select
);
1110 else if (! RIB_SYSTEM_ROUTE (select
))
1112 /* Housekeeping code to deal with
1113 race conditions in kernel with linux
1114 netlink reporting interface up before IPv4 or IPv6 protocol
1115 is ready to add routes.
1116 This makes sure the routes are IN the kernel.
1119 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1120 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1126 rib_install_kernel (rn
, select
);
1131 /* At this point we either haven't found the best RIB entry or it is
1132 * different from what we currently intend to flag with SELECTED. In both
1133 * cases, if a RIB block is present in FIB, it should be withdrawn.
1137 if (IS_ZEBRA_DEBUG_RIB
)
1138 zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__
,
1139 buf
, rn
->p
.prefixlen
, fib
);
1140 redistribute_delete (&rn
->p
, fib
);
1141 if (! RIB_SYSTEM_ROUTE (fib
))
1142 rib_uninstall_kernel (rn
, fib
);
1143 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1145 /* Set real nexthop. */
1146 nexthop_active_update (rn
, fib
, 1);
1149 /* Regardless of some RIB entry being SELECTED or not before, now we can
1150 * tell, that if a new winner exists, FIB is still not updated with this
1151 * data, but ready to be.
1155 if (IS_ZEBRA_DEBUG_RIB
)
1156 zlog_debug ("%s: %s/%d: Adding route, select %p", __func__
, buf
,
1157 rn
->p
.prefixlen
, select
);
1158 /* Set real nexthop. */
1159 nexthop_active_update (rn
, select
, 1);
1161 if (! RIB_SYSTEM_ROUTE (select
))
1162 rib_install_kernel (rn
, select
);
1163 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1164 redistribute_add (&rn
->p
, select
);
1167 /* FIB route was removed, should be deleted */
1170 if (IS_ZEBRA_DEBUG_RIB
)
1171 zlog_debug ("%s: %s/%d: Deleting fib %p, rn %p", __func__
, buf
,
1172 rn
->p
.prefixlen
, del
, rn
);
1173 rib_unlink (rn
, del
);
1177 if (IS_ZEBRA_DEBUG_RIB_Q
)
1178 zlog_debug ("%s: %s/%d: rn %p dequeued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1181 /* Take a list of route_node structs and return 1, if there was a record picked from
1182 * it and processed by rib_process(). Don't process more, than one RN record; operate
1183 * only in the specified sub-queue.
1186 process_subq (struct list
* subq
, u_char qindex
)
1188 struct listnode
*lnode
;
1189 struct route_node
*rnode
;
1190 if (!(lnode
= listhead (subq
)))
1192 rnode
= listgetdata (lnode
);
1193 rib_process (rnode
);
1194 if (rnode
->info
) /* The first RIB record is holding the flags bitmask. */
1195 UNSET_FLAG (((struct rib
*)rnode
->info
)->rn_status
, RIB_ROUTE_QUEUED(qindex
));
1196 route_unlock_node (rnode
);
1197 list_delete_node (subq
, lnode
);
1201 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1202 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1203 * is pointed to the meta queue structure.
1205 static wq_item_status
1206 meta_queue_process (struct work_queue
*dummy
, void *data
)
1208 struct meta_queue
* mq
= data
;
1210 for (i
= 0; i
< MQ_SIZE
; i
++)
1211 if (process_subq (mq
->subq
[i
], i
))
1216 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1219 /* Look into the RN and queue it into one or more priority queues, increasing the size
1220 * for each data push done.
1222 void rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
1226 char buf
[INET6_ADDRSTRLEN
];
1227 if (IS_ZEBRA_DEBUG_RIB_Q
)
1228 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1229 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1233 case ZEBRA_ROUTE_KERNEL
:
1234 case ZEBRA_ROUTE_CONNECT
:
1237 case ZEBRA_ROUTE_STATIC
:
1240 case ZEBRA_ROUTE_RIP
:
1241 case ZEBRA_ROUTE_RIPNG
:
1242 case ZEBRA_ROUTE_OSPF
:
1243 case ZEBRA_ROUTE_OSPF6
:
1244 case ZEBRA_ROUTE_ISIS
:
1247 case ZEBRA_ROUTE_BGP
:
1254 /* Invariant: at this point we always have rn->info set. */
1255 if (CHECK_FLAG (((struct rib
*)rn
->info
)->rn_status
, RIB_ROUTE_QUEUED(qindex
)))
1257 if (IS_ZEBRA_DEBUG_RIB_Q
)
1258 zlog_debug ("%s: %s/%d: rn %p is already queued in sub-queue %u", __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1261 SET_FLAG (((struct rib
*)rn
->info
)->rn_status
, RIB_ROUTE_QUEUED(qindex
));
1262 listnode_add (mq
->subq
[qindex
], rn
);
1263 route_lock_node (rn
);
1265 if (IS_ZEBRA_DEBUG_RIB_Q
)
1266 zlog_debug ("%s: %s/%d: queued rn %p into sub-queue %u", __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1270 /* Add route_node to work queue and schedule processing */
1272 rib_queue_add (struct zebra_t
*zebra
, struct route_node
*rn
)
1274 char buf
[INET_ADDRSTRLEN
];
1275 assert (zebra
&& rn
);
1277 if (IS_ZEBRA_DEBUG_RIB_Q
)
1278 inet_ntop (AF_INET
, &rn
->p
.u
.prefix
, buf
, INET_ADDRSTRLEN
);
1280 /* Pointless to queue a route_node with no RIB entries to add or remove */
1283 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1284 __func__
, rn
, rn
->lock
);
1285 zlog_backtrace(LOG_DEBUG
);
1289 if (IS_ZEBRA_DEBUG_RIB_Q
)
1290 zlog_info ("%s: %s/%d: work queue added", __func__
, buf
, rn
->p
.prefixlen
);
1294 if (zebra
->ribq
== NULL
)
1296 zlog_err ("%s: work_queue does not exist!", __func__
);
1300 /* The RIB queue should normally be either empty or holding the only work_queue_item
1301 * element. In the latter case this element would hold a pointer to the meta queue
1302 * structure, which must be used to actually queue the route nodes to process. So
1303 * create the MQ holder, if necessary, then push the work into it in any case.
1304 * This semantics was introduced after 0.99.9 release.
1307 /* Should I invent work_queue_empty() and use it, or it's Ok to do as follows? */
1308 if (!zebra
->ribq
->items
->count
)
1309 work_queue_add (zebra
->ribq
, zebra
->mq
);
1311 rib_meta_queue_add (zebra
->mq
, rn
);
1313 if (IS_ZEBRA_DEBUG_RIB_Q
)
1314 zlog_debug ("%s: %s/%d: rn %p queued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1319 /* Create new meta queue. A destructor function doesn't seem to be necessary here. */
1323 struct meta_queue
*new;
1324 unsigned i
, failed
= 0;
1326 if ((new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
))) == NULL
)
1328 for (i
= 0; i
< MQ_SIZE
; i
++)
1329 if ((new->subq
[i
] = list_new ()) == NULL
)
1333 for (i
= 0; i
< MQ_SIZE
; i
++)
1335 list_delete (new->subq
[i
]);
1336 XFREE (MTYPE_WORK_QUEUE
, new);
1343 /* initialise zebra rib work queue */
1345 rib_queue_init (struct zebra_t
*zebra
)
1349 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1350 "route_node processing")))
1352 zlog_err ("%s: could not initialise work queue!", __func__
);
1356 /* fill in the work queue spec */
1357 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1358 zebra
->ribq
->spec
.errorfunc
= NULL
;
1359 /* XXX: TODO: These should be runtime configurable via vty */
1360 zebra
->ribq
->spec
.max_retries
= 3;
1361 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
1363 if (!(zebra
->mq
= meta_queue_new ()))
1365 zlog_err ("%s: could not initialise meta queue!", __func__
);
1371 /* RIB updates are processed via a queue of pointers to route_nodes.
1373 * The queue length is bounded by the maximal size of the routing table,
1374 * as a route_node will not be requeued, if already queued.
1376 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
1377 * state, or static_install_ipv{4,6} (when an existing RIB is updated)
1378 * and then submit route_node to queue for best-path selection later.
1379 * Order of add/delete state changes are preserved for any given RIB.
1381 * Deleted RIBs are reaped during best-path selection.
1384 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
1385 * |-------->| | best RIB, if required
1387 * static_install->|->rib_addqueue...... -> rib_process
1389 * |-------->| |-> rib_unlink
1390 * |-> set RIB_ENTRY_REMOVE |
1391 * rib_delnode (RIB freed)
1394 * Queueing state for a route_node is kept in the head RIB entry, this
1395 * state must be preserved as and when the head RIB entry of a
1396 * route_node is changed by rib_unlink / rib_link. A small complication,
1397 * but saves having to allocate a dedicated object for this.
1399 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1401 * - route_nodes: refcounted by:
1402 * - RIBs attached to route_node:
1403 * - managed by: rib_link/unlink
1404 * - route_node processing queue
1405 * - managed by: rib_addqueue, rib_process.
1409 /* Add RIB to head of the route node. */
1411 rib_link (struct route_node
*rn
, struct rib
*rib
)
1414 char buf
[INET6_ADDRSTRLEN
];
1418 route_lock_node (rn
); /* rn route table reference */
1420 if (IS_ZEBRA_DEBUG_RIB
)
1422 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1423 zlog_debug ("%s: %s/%d: rn %p, rib %p", __func__
,
1424 buf
, rn
->p
.prefixlen
, rn
, rib
);
1430 if (IS_ZEBRA_DEBUG_RIB
)
1431 zlog_debug ("%s: %s/%d: new head, rn_status copied over", __func__
,
1432 buf
, rn
->p
.prefixlen
);
1434 /* Transfer the rn status flags to the new head RIB */
1435 rib
->rn_status
= head
->rn_status
;
1439 rib_queue_add (&zebrad
, rn
);
1443 rib_addnode (struct route_node
*rn
, struct rib
*rib
)
1445 /* RIB node has been un-removed before route-node is processed.
1446 * route_node must hence already be on the queue for processing..
1448 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1450 if (IS_ZEBRA_DEBUG_RIB
)
1452 char buf
[INET6_ADDRSTRLEN
];
1453 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1454 zlog_debug ("%s: %s/%d: rn %p, un-removed rib %p",
1455 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1457 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1464 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
1466 struct nexthop
*nexthop
, *next
;
1467 char buf
[INET6_ADDRSTRLEN
];
1471 if (IS_ZEBRA_DEBUG_RIB
)
1473 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1474 zlog_debug ("%s: %s/%d: rn %p, rib %p",
1475 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1479 rib
->next
->prev
= rib
->prev
;
1482 rib
->prev
->next
= rib
->next
;
1485 rn
->info
= rib
->next
;
1489 if (IS_ZEBRA_DEBUG_RIB
)
1490 zlog_debug ("%s: %s/%d: rn %p, rib %p, new head copy",
1491 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1492 rib
->next
->rn_status
= rib
->rn_status
;
1496 /* free RIB and nexthops */
1497 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= next
)
1499 next
= nexthop
->next
;
1500 nexthop_free (nexthop
);
1502 XFREE (MTYPE_RIB
, rib
);
1504 route_unlock_node (rn
); /* rn route table reference */
1508 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
1510 if (IS_ZEBRA_DEBUG_RIB
)
1512 char buf
[INET6_ADDRSTRLEN
];
1513 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1514 zlog_debug ("%s: %s/%d: rn %p, rib %p, removing", __func__
,
1515 buf
, rn
->p
.prefixlen
, rn
, rib
);
1517 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1518 rib_queue_add (&zebrad
, rn
);
1522 rib_add_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1523 struct in_addr
*gate
, struct in_addr
*src
,
1524 unsigned int ifindex
, u_int32_t vrf_id
,
1525 u_int32_t metric
, u_char distance
)
1528 struct rib
*same
= NULL
;
1529 struct route_table
*table
;
1530 struct route_node
*rn
;
1531 struct nexthop
*nexthop
;
1534 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1538 /* Make it sure prefixlen is applied to the prefix. */
1539 apply_mask_ipv4 (p
);
1541 /* Set default distance by route type. */
1544 distance
= route_info
[type
].distance
;
1546 /* iBGP distance is 200. */
1547 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1551 /* Lookup route node.*/
1552 rn
= route_node_get (table
, (struct prefix
*) p
);
1554 /* If same type of route are installed, treat it as a implicit
1556 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1558 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1561 if (rib
->type
!= type
)
1563 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
1568 /* Duplicate connected route comes in. */
1569 else if ((nexthop
= rib
->nexthop
) &&
1570 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
1571 nexthop
->ifindex
== ifindex
&&
1572 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1579 /* Allocate new rib structure. */
1580 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1582 rib
->distance
= distance
;
1584 rib
->metric
= metric
;
1585 rib
->table
= vrf_id
;
1586 rib
->nexthop_num
= 0;
1587 rib
->uptime
= time (NULL
);
1589 /* Nexthop settings. */
1593 nexthop_ipv4_ifindex_add (rib
, gate
, src
, ifindex
);
1595 nexthop_ipv4_add (rib
, gate
, src
);
1598 nexthop_ifindex_add (rib
, ifindex
);
1600 /* If this route is kernel route, set FIB flag to the route. */
1601 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1602 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1603 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1605 /* Link new rib to node.*/
1606 if (IS_ZEBRA_DEBUG_RIB
)
1607 zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__
, rn
, rib
);
1608 rib_addnode (rn
, rib
);
1610 /* Free implicit route.*/
1613 if (IS_ZEBRA_DEBUG_RIB
)
1614 zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__
, rn
, rib
);
1615 rib_delnode (rn
, same
);
1618 route_unlock_node (rn
);
1622 /* This function dumps the contents of a given RIB entry into
1623 * standard debug log. Calling function name and IP prefix in
1624 * question are passed as 1st and 2nd arguments.
1627 void rib_dump (const char * func
, const struct prefix_ipv4
* p
, const struct rib
* rib
)
1629 char straddr1
[INET_ADDRSTRLEN
], straddr2
[INET_ADDRSTRLEN
];
1630 struct nexthop
*nexthop
;
1632 inet_ntop (AF_INET
, &p
->prefix
, straddr1
, INET_ADDRSTRLEN
);
1633 zlog_debug ("%s: dumping RIB entry %p for %s/%d", func
, rib
, straddr1
, p
->prefixlen
);
1636 "%s: refcnt == %lu, uptime == %u, type == %u, table == %d",
1645 "%s: metric == %u, distance == %u, flags == %u, status == %u",
1654 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
1657 rib
->nexthop_active_num
,
1658 rib
->nexthop_fib_num
1660 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1662 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, straddr1
, INET_ADDRSTRLEN
);
1663 inet_ntop (AF_INET
, &nexthop
->rgate
.ipv4
.s_addr
, straddr2
, INET_ADDRSTRLEN
);
1666 "%s: NH %s (%s) with flags %s%s%s",
1670 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
1671 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
1672 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
1675 zlog_debug ("%s: dump complete", func
);
1678 /* This is an exported helper to rtm_read() to dump the strange
1679 * RIB entry found by rib_lookup_ipv4_route()
1682 void rib_lookup_and_dump (struct prefix_ipv4
* p
)
1684 struct route_table
*table
;
1685 struct route_node
*rn
;
1687 char prefix_buf
[INET_ADDRSTRLEN
];
1690 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1693 zlog_err ("%s: vrf_table() returned NULL", __func__
);
1697 inet_ntop (AF_INET
, &p
->prefix
.s_addr
, prefix_buf
, INET_ADDRSTRLEN
);
1698 /* Scan the RIB table for exactly matching RIB entry. */
1699 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1701 /* No route for this prefix. */
1704 zlog_debug ("%s: lookup failed for %s/%d", __func__
, prefix_buf
, p
->prefixlen
);
1709 route_unlock_node (rn
);
1712 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1716 "%s: rn %p, rib %p: %s, %s",
1720 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
1721 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
1723 rib_dump (__func__
, p
, rib
);
1727 /* Check if requested address assignment will fail due to another
1728 * route being installed by zebra in FIB already. Take necessary
1729 * actions, if needed: remove such a route from FIB and deSELECT
1730 * corresponding RIB entry. Then put affected RN into RIBQ head.
1732 void rib_lookup_and_pushup (struct prefix_ipv4
* p
)
1734 struct route_table
*table
;
1735 struct route_node
*rn
;
1737 unsigned changed
= 0;
1739 if (NULL
== (table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0)))
1741 zlog_err ("%s: vrf_table() returned NULL", __func__
);
1745 /* No matches would be the simplest case. */
1746 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
1750 route_unlock_node (rn
);
1752 /* Check all RIB entries. In case any changes have to be done, requeue
1753 * the RN into RIBQ head. If the routing message about the new connected
1754 * route (generated by the IP address we are going to assign very soon)
1755 * comes before the RIBQ is processed, the new RIB entry will join
1756 * RIBQ record already on head. This is necessary for proper revalidation
1757 * of the rest of the RIB.
1759 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1761 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) &&
1762 ! RIB_SYSTEM_ROUTE (rib
))
1765 if (IS_ZEBRA_DEBUG_RIB
)
1767 char buf
[INET_ADDRSTRLEN
];
1768 inet_ntop (rn
->p
.family
, &p
->prefix
, buf
, INET_ADDRSTRLEN
);
1769 zlog_debug ("%s: freeing way for connected prefix %s/%d", __func__
, buf
, p
->prefixlen
);
1770 rib_dump (__func__
, (struct prefix_ipv4
*)&rn
->p
, rib
);
1772 rib_uninstall (rn
, rib
);
1776 rib_queue_add (&zebrad
, rn
);
1780 rib_add_ipv4_multipath (struct prefix_ipv4
*p
, struct rib
*rib
)
1782 struct route_table
*table
;
1783 struct route_node
*rn
;
1785 struct nexthop
*nexthop
;
1788 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1791 /* Make it sure prefixlen is applied to the prefix. */
1792 apply_mask_ipv4 (p
);
1794 /* Set default distance by route type. */
1795 if (rib
->distance
== 0)
1797 rib
->distance
= route_info
[rib
->type
].distance
;
1799 /* iBGP distance is 200. */
1800 if (rib
->type
== ZEBRA_ROUTE_BGP
1801 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
1802 rib
->distance
= 200;
1805 /* Lookup route node.*/
1806 rn
= route_node_get (table
, (struct prefix
*) p
);
1808 /* If same type of route are installed, treat it as a implicit
1810 for (same
= rn
->info
; same
; same
= same
->next
)
1812 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
1815 if (same
->type
== rib
->type
&& same
->table
== rib
->table
1816 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
1820 /* If this route is kernel route, set FIB flag to the route. */
1821 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
1822 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1823 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1825 /* Link new rib to node.*/
1826 rib_addnode (rn
, rib
);
1827 if (IS_ZEBRA_DEBUG_RIB
)
1829 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
1831 rib_dump (__func__
, p
, rib
);
1834 /* Free implicit route.*/
1837 if (IS_ZEBRA_DEBUG_RIB
)
1839 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
1840 __func__
, rn
, same
);
1841 rib_dump (__func__
, p
, same
);
1843 rib_delnode (rn
, same
);
1846 route_unlock_node (rn
);
1850 /* XXX factor with rib_delete_ipv6 */
1852 rib_delete_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1853 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1855 struct route_table
*table
;
1856 struct route_node
*rn
;
1858 struct rib
*fib
= NULL
;
1859 struct rib
*same
= NULL
;
1860 struct nexthop
*nexthop
;
1865 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1870 apply_mask_ipv4 (p
);
1872 if (IS_ZEBRA_DEBUG_KERNEL
&& gate
)
1873 zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
1874 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1879 /* Lookup route node. */
1880 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1883 if (IS_ZEBRA_DEBUG_KERNEL
)
1886 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
1887 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1889 inet_ntop (AF_INET
, gate
, buf2
, BUFSIZ
),
1892 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
1893 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1897 return ZEBRA_ERR_RTNOEXIST
;
1900 /* Lookup same type route. */
1901 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1903 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1906 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1909 if (rib
->type
!= type
)
1911 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
1912 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&& nexthop
->ifindex
== ifindex
)
1917 route_unlock_node (rn
);
1918 route_unlock_node (rn
);
1924 /* Make sure that the route found has the same gateway. */
1925 else if (gate
== NULL
||
1926 ((nexthop
= rib
->nexthop
) &&
1927 (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
1928 IPV4_ADDR_SAME (&nexthop
->rgate
.ipv4
, gate
))))
1935 /* If same type of route can't be found and this message is from
1939 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
1942 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1943 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1945 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1949 if (IS_ZEBRA_DEBUG_KERNEL
)
1952 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
1953 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1955 inet_ntop (AF_INET
, gate
, buf2
, BUFSIZ
),
1959 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
1960 inet_ntop (AF_INET
, &p
->prefix
, buf1
, BUFSIZ
),
1965 route_unlock_node (rn
);
1966 return ZEBRA_ERR_RTNOEXIST
;
1971 rib_delnode (rn
, same
);
1973 route_unlock_node (rn
);
1977 /* Install static route into rib. */
1979 static_install_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
1982 struct route_node
*rn
;
1983 struct route_table
*table
;
1986 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1990 /* Lookup existing route */
1991 rn
= route_node_get (table
, p
);
1992 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1994 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1997 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2003 /* Same distance static route is there. Update it with new
2005 route_unlock_node (rn
);
2008 case STATIC_IPV4_GATEWAY
:
2009 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2011 case STATIC_IPV4_IFNAME
:
2012 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2014 case STATIC_IPV4_BLACKHOLE
:
2015 nexthop_blackhole_add (rib
);
2018 rib_queue_add (&zebrad
, rn
);
2022 /* This is new static route. */
2023 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2025 rib
->type
= ZEBRA_ROUTE_STATIC
;
2026 rib
->distance
= si
->distance
;
2028 rib
->nexthop_num
= 0;
2032 case STATIC_IPV4_GATEWAY
:
2033 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2035 case STATIC_IPV4_IFNAME
:
2036 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2038 case STATIC_IPV4_BLACKHOLE
:
2039 nexthop_blackhole_add (rib
);
2043 /* Save the flags of this static routes (reject, blackhole) */
2044 rib
->flags
= si
->flags
;
2046 /* Link this rib to the tree. */
2047 rib_addnode (rn
, rib
);
2052 static_ipv4_nexthop_same (struct nexthop
*nexthop
, struct static_ipv4
*si
)
2054 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
2055 && si
->type
== STATIC_IPV4_GATEWAY
2056 && IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &si
->gate
.ipv4
))
2058 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2059 && si
->type
== STATIC_IPV4_IFNAME
2060 && strcmp (nexthop
->ifname
, si
->gate
.ifname
) == 0)
2062 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
2063 && si
->type
== STATIC_IPV4_BLACKHOLE
)
2068 /* Uninstall static route from RIB. */
2070 static_uninstall_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
2072 struct route_node
*rn
;
2074 struct nexthop
*nexthop
;
2075 struct route_table
*table
;
2078 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2082 /* Lookup existing route with type and distance. */
2083 rn
= route_node_lookup (table
, p
);
2087 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2089 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2092 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2098 route_unlock_node (rn
);
2102 /* Lookup nexthop. */
2103 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2104 if (static_ipv4_nexthop_same (nexthop
, si
))
2107 /* Can't find nexthop. */
2110 route_unlock_node (rn
);
2114 /* Check nexthop. */
2115 if (rib
->nexthop_num
== 1)
2116 rib_delnode (rn
, rib
);
2119 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2120 rib_uninstall (rn
, rib
);
2121 nexthop_delete (rib
, nexthop
);
2122 nexthop_free (nexthop
);
2123 rib_queue_add (&zebrad
, rn
);
2126 route_unlock_node (rn
);
2129 /* Add static route into static route configuration. */
2131 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2132 u_char flags
, u_char distance
, u_int32_t vrf_id
)
2135 struct route_node
*rn
;
2136 struct static_ipv4
*si
;
2137 struct static_ipv4
*pp
;
2138 struct static_ipv4
*cp
;
2139 struct static_ipv4
*update
= NULL
;
2140 struct route_table
*stable
;
2143 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2147 /* Lookup static route prefix. */
2148 rn
= route_node_get (stable
, p
);
2152 type
= STATIC_IPV4_GATEWAY
;
2154 type
= STATIC_IPV4_IFNAME
;
2156 type
= STATIC_IPV4_BLACKHOLE
;
2158 /* Do nothing if there is a same static route. */
2159 for (si
= rn
->info
; si
; si
= si
->next
)
2161 if (type
== si
->type
2162 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2163 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2165 if (distance
== si
->distance
)
2167 route_unlock_node (rn
);
2175 /* Distance changed. */
2177 static_delete_ipv4 (p
, gate
, ifname
, update
->distance
, vrf_id
);
2179 /* Make new static route structure. */
2180 si
= XMALLOC (MTYPE_STATIC_IPV4
, sizeof (struct static_ipv4
));
2181 memset (si
, 0, sizeof (struct static_ipv4
));
2184 si
->distance
= distance
;
2188 si
->gate
.ipv4
= *gate
;
2190 si
->gate
.ifname
= XSTRDUP (0, ifname
);
2192 /* Add new static route information to the tree with sort by
2193 distance value and gateway address. */
2194 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2196 if (si
->distance
< cp
->distance
)
2198 if (si
->distance
> cp
->distance
)
2200 if (si
->type
== STATIC_IPV4_GATEWAY
&& cp
->type
== STATIC_IPV4_GATEWAY
)
2202 if (ntohl (si
->gate
.ipv4
.s_addr
) < ntohl (cp
->gate
.ipv4
.s_addr
))
2204 if (ntohl (si
->gate
.ipv4
.s_addr
) > ntohl (cp
->gate
.ipv4
.s_addr
))
2209 /* Make linked list. */
2219 /* Install into rib. */
2220 static_install_ipv4 (p
, si
);
2225 /* Delete static route from static route configuration. */
2227 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2228 u_char distance
, u_int32_t vrf_id
)
2231 struct route_node
*rn
;
2232 struct static_ipv4
*si
;
2233 struct route_table
*stable
;
2236 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2240 /* Lookup static route prefix. */
2241 rn
= route_node_lookup (stable
, p
);
2247 type
= STATIC_IPV4_GATEWAY
;
2249 type
= STATIC_IPV4_IFNAME
;
2251 type
= STATIC_IPV4_BLACKHOLE
;
2253 /* Find same static route is the tree */
2254 for (si
= rn
->info
; si
; si
= si
->next
)
2255 if (type
== si
->type
2256 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2257 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2260 /* Can't find static route. */
2263 route_unlock_node (rn
);
2267 /* Install into rib. */
2268 static_uninstall_ipv4 (p
, si
);
2270 /* Unlink static route from linked list. */
2272 si
->prev
->next
= si
->next
;
2274 rn
->info
= si
->next
;
2276 si
->next
->prev
= si
->prev
;
2277 route_unlock_node (rn
);
2279 /* Free static route configuration. */
2281 XFREE (0, si
->gate
.ifname
);
2282 XFREE (MTYPE_STATIC_IPV4
, si
);
2284 route_unlock_node (rn
);
2292 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
2293 struct in6_addr
*gate
, unsigned int ifindex
, int table
)
2295 if (type
== ZEBRA_ROUTE_CONNECT
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)) {
2296 #if defined (MUSICA) || defined (LINUX)
2297 /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
2298 if (p
->prefixlen
== 96)
2303 if (type
== ZEBRA_ROUTE_KERNEL
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)
2304 && p
->prefixlen
== 96 && gate
&& IN6_IS_ADDR_UNSPECIFIED (gate
))
2306 kernel_delete_ipv6_old (p
, gate
, ifindex
, 0, table
);
2313 rib_add_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2314 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
,
2315 u_int32_t metric
, u_char distance
)
2318 struct rib
*same
= NULL
;
2319 struct route_table
*table
;
2320 struct route_node
*rn
;
2321 struct nexthop
*nexthop
;
2324 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2328 /* Make sure mask is applied. */
2329 apply_mask_ipv6 (p
);
2331 /* Set default distance by route type. */
2333 distance
= route_info
[type
].distance
;
2335 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2338 /* Filter bogus route. */
2339 if (rib_bogus_ipv6 (type
, p
, gate
, ifindex
, 0))
2342 /* Lookup route node.*/
2343 rn
= route_node_get (table
, (struct prefix
*) p
);
2345 /* If same type of route are installed, treat it as a implicit
2347 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2349 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2352 if (rib
->type
!= type
)
2354 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2359 else if ((nexthop
= rib
->nexthop
) &&
2360 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2361 nexthop
->ifindex
== ifindex
)
2368 /* Allocate new rib structure. */
2369 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2372 rib
->distance
= distance
;
2374 rib
->metric
= metric
;
2375 rib
->table
= vrf_id
;
2376 rib
->nexthop_num
= 0;
2377 rib
->uptime
= time (NULL
);
2379 /* Nexthop settings. */
2383 nexthop_ipv6_ifindex_add (rib
, gate
, ifindex
);
2385 nexthop_ipv6_add (rib
, gate
);
2388 nexthop_ifindex_add (rib
, ifindex
);
2390 /* If this route is kernel route, set FIB flag to the route. */
2391 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2392 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2393 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2395 /* Link new rib to node.*/
2396 rib_addnode (rn
, rib
);
2398 /* Free implicit route.*/
2400 rib_delnode (rn
, same
);
2402 route_unlock_node (rn
);
2406 /* XXX factor with rib_delete_ipv6 */
2408 rib_delete_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2409 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
2411 struct route_table
*table
;
2412 struct route_node
*rn
;
2414 struct rib
*fib
= NULL
;
2415 struct rib
*same
= NULL
;
2416 struct nexthop
*nexthop
;
2421 apply_mask_ipv6 (p
);
2424 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2428 /* Lookup route node. */
2429 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2432 if (IS_ZEBRA_DEBUG_KERNEL
)
2435 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
2436 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
2438 inet_ntop (AF_INET6
, gate
, buf2
, BUFSIZ
),
2441 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
2442 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
2446 return ZEBRA_ERR_RTNOEXIST
;
2449 /* Lookup same type route. */
2450 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2452 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2455 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2458 if (rib
->type
!= type
)
2460 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2461 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&& nexthop
->ifindex
== ifindex
)
2466 route_unlock_node (rn
);
2467 route_unlock_node (rn
);
2473 /* Make sure that the route found has the same gateway. */
2474 else if (gate
== NULL
||
2475 ((nexthop
= rib
->nexthop
) &&
2476 (IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
) ||
2477 IPV6_ADDR_SAME (&nexthop
->rgate
.ipv6
, gate
))))
2484 /* If same type of route can't be found and this message is from
2488 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
2491 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2492 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2494 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
2498 if (IS_ZEBRA_DEBUG_KERNEL
)
2501 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
2502 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
2504 inet_ntop (AF_INET6
, gate
, buf2
, BUFSIZ
),
2508 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
2509 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, BUFSIZ
),
2514 route_unlock_node (rn
);
2515 return ZEBRA_ERR_RTNOEXIST
;
2520 rib_delnode (rn
, same
);
2522 route_unlock_node (rn
);
2526 /* Install static route into rib. */
2528 static_install_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2531 struct route_table
*table
;
2532 struct route_node
*rn
;
2535 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2539 /* Lookup existing route */
2540 rn
= route_node_get (table
, p
);
2541 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2543 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2546 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2552 /* Same distance static route is there. Update it with new
2554 route_unlock_node (rn
);
2558 case STATIC_IPV6_GATEWAY
:
2559 nexthop_ipv6_add (rib
, &si
->ipv6
);
2561 case STATIC_IPV6_IFNAME
:
2562 nexthop_ifname_add (rib
, si
->ifname
);
2564 case STATIC_IPV6_GATEWAY_IFNAME
:
2565 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2568 rib_queue_add (&zebrad
, rn
);
2572 /* This is new static route. */
2573 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2575 rib
->type
= ZEBRA_ROUTE_STATIC
;
2576 rib
->distance
= si
->distance
;
2578 rib
->nexthop_num
= 0;
2582 case STATIC_IPV6_GATEWAY
:
2583 nexthop_ipv6_add (rib
, &si
->ipv6
);
2585 case STATIC_IPV6_IFNAME
:
2586 nexthop_ifname_add (rib
, si
->ifname
);
2588 case STATIC_IPV6_GATEWAY_IFNAME
:
2589 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2593 /* Save the flags of this static routes (reject, blackhole) */
2594 rib
->flags
= si
->flags
;
2596 /* Link this rib to the tree. */
2597 rib_addnode (rn
, rib
);
2602 static_ipv6_nexthop_same (struct nexthop
*nexthop
, struct static_ipv6
*si
)
2604 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
2605 && si
->type
== STATIC_IPV6_GATEWAY
2606 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
))
2608 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2609 && si
->type
== STATIC_IPV6_IFNAME
2610 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2612 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
2613 && si
->type
== STATIC_IPV6_GATEWAY_IFNAME
2614 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
)
2615 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2621 static_uninstall_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2623 struct route_table
*table
;
2624 struct route_node
*rn
;
2626 struct nexthop
*nexthop
;
2629 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2633 /* Lookup existing route with type and distance. */
2634 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2638 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2640 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2643 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2649 route_unlock_node (rn
);
2653 /* Lookup nexthop. */
2654 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2655 if (static_ipv6_nexthop_same (nexthop
, si
))
2658 /* Can't find nexthop. */
2661 route_unlock_node (rn
);
2665 /* Check nexthop. */
2666 if (rib
->nexthop_num
== 1)
2668 rib_delnode (rn
, rib
);
2672 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2673 rib_uninstall (rn
, rib
);
2674 nexthop_delete (rib
, nexthop
);
2675 nexthop_free (nexthop
);
2676 rib_queue_add (&zebrad
, rn
);
2679 route_unlock_node (rn
);
2682 /* Add static route into static route configuration. */
2684 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2685 const char *ifname
, u_char flags
, u_char distance
,
2688 struct route_node
*rn
;
2689 struct static_ipv6
*si
;
2690 struct static_ipv6
*pp
;
2691 struct static_ipv6
*cp
;
2692 struct route_table
*stable
;
2695 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2700 (type
== STATIC_IPV6_GATEWAY
|| type
== STATIC_IPV6_GATEWAY_IFNAME
))
2704 (type
== STATIC_IPV6_GATEWAY_IFNAME
|| type
== STATIC_IPV6_IFNAME
))
2707 /* Lookup static route prefix. */
2708 rn
= route_node_get (stable
, p
);
2710 /* Do nothing if there is a same static route. */
2711 for (si
= rn
->info
; si
; si
= si
->next
)
2713 if (distance
== si
->distance
2715 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2716 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2718 route_unlock_node (rn
);
2723 /* Make new static route structure. */
2724 si
= XMALLOC (MTYPE_STATIC_IPV6
, sizeof (struct static_ipv6
));
2725 memset (si
, 0, sizeof (struct static_ipv6
));
2728 si
->distance
= distance
;
2733 case STATIC_IPV6_GATEWAY
:
2736 case STATIC_IPV6_IFNAME
:
2737 si
->ifname
= XSTRDUP (0, ifname
);
2739 case STATIC_IPV6_GATEWAY_IFNAME
:
2741 si
->ifname
= XSTRDUP (0, ifname
);
2745 /* Add new static route information to the tree with sort by
2746 distance value and gateway address. */
2747 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2749 if (si
->distance
< cp
->distance
)
2751 if (si
->distance
> cp
->distance
)
2755 /* Make linked list. */
2765 /* Install into rib. */
2766 static_install_ipv6 (p
, si
);
2771 /* Delete static route from static route configuration. */
2773 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2774 const char *ifname
, u_char distance
, u_int32_t vrf_id
)
2776 struct route_node
*rn
;
2777 struct static_ipv6
*si
;
2778 struct route_table
*stable
;
2781 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2785 /* Lookup static route prefix. */
2786 rn
= route_node_lookup (stable
, p
);
2790 /* Find same static route is the tree */
2791 for (si
= rn
->info
; si
; si
= si
->next
)
2792 if (distance
== si
->distance
2794 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2795 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2798 /* Can't find static route. */
2801 route_unlock_node (rn
);
2805 /* Install into rib. */
2806 static_uninstall_ipv6 (p
, si
);
2808 /* Unlink static route from linked list. */
2810 si
->prev
->next
= si
->next
;
2812 rn
->info
= si
->next
;
2814 si
->next
->prev
= si
->prev
;
2816 /* Free static route configuration. */
2818 XFREE (0, si
->ifname
);
2819 XFREE (MTYPE_STATIC_IPV6
, si
);
2823 #endif /* HAVE_IPV6 */
2825 /* RIB update function. */
2829 struct route_node
*rn
;
2830 struct route_table
*table
;
2832 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2834 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2836 rib_queue_add (&zebrad
, rn
);
2838 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2840 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2842 rib_queue_add (&zebrad
, rn
);
2845 /* Interface goes up. */
2847 rib_if_up (struct interface
*ifp
)
2852 /* Interface goes down. */
2854 rib_if_down (struct interface
*ifp
)
2859 /* Remove all routes which comes from non main table. */
2861 rib_weed_table (struct route_table
*table
)
2863 struct route_node
*rn
;
2868 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2869 for (rib
= rn
->info
; rib
; rib
= next
)
2873 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2876 if (rib
->table
!= zebrad
.rtm_table_default
&&
2877 rib
->table
!= RT_TABLE_MAIN
)
2878 rib_delnode (rn
, rib
);
2882 /* Delete all routes from non main table. */
2884 rib_weed_tables (void)
2886 rib_weed_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2887 rib_weed_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2890 /* Delete self installed routes after zebra is relaunched. */
2892 rib_sweep_table (struct route_table
*table
)
2894 struct route_node
*rn
;
2900 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2901 for (rib
= rn
->info
; rib
; rib
= next
)
2905 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2908 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
2909 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
2911 ret
= rib_uninstall_kernel (rn
, rib
);
2913 rib_delnode (rn
, rib
);
2918 /* Sweep all RIB tables. */
2920 rib_sweep_route (void)
2922 rib_sweep_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2923 rib_sweep_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2926 /* Close RIB and clean up kernel routes. */
2928 rib_close_table (struct route_table
*table
)
2930 struct route_node
*rn
;
2934 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2935 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2937 if (! RIB_SYSTEM_ROUTE (rib
)
2938 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2939 rib_uninstall_kernel (rn
, rib
);
2943 /* Close all RIB tables. */
2947 rib_close_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2948 rib_close_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2951 /* Routing information base initialize. */
2955 rib_queue_init (&zebrad
);
2956 /* VRF initialization. */