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. */
58 } route_info
[ZEBRA_ROUTE_MAX
] =
60 [ZEBRA_ROUTE_SYSTEM
] = {ZEBRA_ROUTE_SYSTEM
, 0},
61 [ZEBRA_ROUTE_KERNEL
] = {ZEBRA_ROUTE_KERNEL
, 0},
62 [ZEBRA_ROUTE_CONNECT
] = {ZEBRA_ROUTE_CONNECT
, 0},
63 [ZEBRA_ROUTE_STATIC
] = {ZEBRA_ROUTE_STATIC
, 1},
64 [ZEBRA_ROUTE_RIP
] = {ZEBRA_ROUTE_RIP
, 120},
65 [ZEBRA_ROUTE_RIPNG
] = {ZEBRA_ROUTE_RIPNG
, 120},
66 [ZEBRA_ROUTE_OSPF
] = {ZEBRA_ROUTE_OSPF
, 110},
67 [ZEBRA_ROUTE_OSPF6
] = {ZEBRA_ROUTE_OSPF6
, 110},
68 [ZEBRA_ROUTE_ISIS
] = {ZEBRA_ROUTE_ISIS
, 115},
69 [ZEBRA_ROUTE_BGP
] = {ZEBRA_ROUTE_BGP
, 20 /* IBGP is 200. */},
70 [ZEBRA_ROUTE_BABEL
] = {ZEBRA_ROUTE_BABEL
, 95},
71 /* no entry/default: 150 */
74 /* Vector for routing table. */
75 static vector vrf_vector
;
77 /* Allocate new VRF. */
79 vrf_alloc (const char *name
)
83 vrf
= XCALLOC (MTYPE_VRF
, sizeof (struct vrf
));
87 vrf
->name
= XSTRDUP (MTYPE_VRF_NAME
, name
);
89 /* Allocate routing table and static table. */
90 vrf
->table
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
91 vrf
->table
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
92 vrf
->stable
[AFI_IP
][SAFI_UNICAST
] = route_table_init ();
93 vrf
->stable
[AFI_IP6
][SAFI_UNICAST
] = route_table_init ();
94 vrf
->table
[AFI_IP
][SAFI_MULTICAST
] = route_table_init ();
95 vrf
->table
[AFI_IP6
][SAFI_MULTICAST
] = route_table_init ();
96 vrf
->stable
[AFI_IP
][SAFI_MULTICAST
] = route_table_init ();
97 vrf
->stable
[AFI_IP6
][SAFI_MULTICAST
] = route_table_init ();
103 /* Lookup VRF by identifier. */
105 vrf_lookup (u_int32_t id
)
107 return vector_lookup (vrf_vector
, id
);
110 /* Initialize VRF. */
114 struct vrf
*default_table
;
116 /* Allocate VRF vector. */
117 vrf_vector
= vector_init (1);
119 /* Allocate default main table. */
120 default_table
= vrf_alloc ("Default-IP-Routing-Table");
122 /* Default table index must be 0. */
123 vector_set_index (vrf_vector
, 0, default_table
);
126 /* Lookup route table. */
128 vrf_table (afi_t afi
, safi_t safi
, u_int32_t id
)
132 vrf
= vrf_lookup (id
);
136 return vrf
->table
[afi
][safi
];
139 /* Lookup static route table. */
141 vrf_static_table (afi_t afi
, safi_t safi
, u_int32_t id
)
145 vrf
= vrf_lookup (id
);
149 return vrf
->stable
[afi
][safi
];
152 /* Add nexthop to the end of the list. */
154 nexthop_add (struct rib
*rib
, struct nexthop
*nexthop
)
156 struct nexthop
*last
;
158 for (last
= rib
->nexthop
; last
&& last
->next
; last
= last
->next
)
161 last
->next
= nexthop
;
163 rib
->nexthop
= nexthop
;
164 nexthop
->prev
= last
;
169 /* Delete specified nexthop from the list. */
171 nexthop_delete (struct rib
*rib
, struct nexthop
*nexthop
)
174 nexthop
->next
->prev
= nexthop
->prev
;
176 nexthop
->prev
->next
= nexthop
->next
;
178 rib
->nexthop
= nexthop
->next
;
184 nexthop_free (struct nexthop
*nexthop
)
187 XFREE (0, nexthop
->ifname
);
188 XFREE (MTYPE_NEXTHOP
, nexthop
);
192 nexthop_ifindex_add (struct rib
*rib
, unsigned int ifindex
)
194 struct nexthop
*nexthop
;
196 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
197 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
198 nexthop
->ifindex
= ifindex
;
200 nexthop_add (rib
, nexthop
);
206 nexthop_ifname_add (struct rib
*rib
, char *ifname
)
208 struct nexthop
*nexthop
;
210 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
211 nexthop
->type
= NEXTHOP_TYPE_IFNAME
;
212 nexthop
->ifname
= XSTRDUP (0, ifname
);
214 nexthop_add (rib
, nexthop
);
220 nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
222 struct nexthop
*nexthop
;
224 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
225 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
226 nexthop
->gate
.ipv4
= *ipv4
;
228 nexthop
->src
.ipv4
= *src
;
230 nexthop_add (rib
, nexthop
);
236 nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
237 struct in_addr
*src
, unsigned int ifindex
)
239 struct nexthop
*nexthop
;
241 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
242 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
243 nexthop
->gate
.ipv4
= *ipv4
;
245 nexthop
->src
.ipv4
= *src
;
246 nexthop
->ifindex
= ifindex
;
248 nexthop_add (rib
, nexthop
);
255 nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
257 struct nexthop
*nexthop
;
259 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
260 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
261 nexthop
->gate
.ipv6
= *ipv6
;
263 nexthop_add (rib
, nexthop
);
268 static struct nexthop
*
269 nexthop_ipv6_ifname_add (struct rib
*rib
, struct in6_addr
*ipv6
,
272 struct nexthop
*nexthop
;
274 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
275 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFNAME
;
276 nexthop
->gate
.ipv6
= *ipv6
;
277 nexthop
->ifname
= XSTRDUP (0, ifname
);
279 nexthop_add (rib
, nexthop
);
284 static struct nexthop
*
285 nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
286 unsigned int ifindex
)
288 struct nexthop
*nexthop
;
290 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
291 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
292 nexthop
->gate
.ipv6
= *ipv6
;
293 nexthop
->ifindex
= ifindex
;
295 nexthop_add (rib
, nexthop
);
299 #endif /* HAVE_IPV6 */
302 nexthop_blackhole_add (struct rib
*rib
)
304 struct nexthop
*nexthop
;
306 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
307 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
308 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
310 nexthop_add (rib
, nexthop
);
315 /* If force flag is not set, do not modify falgs at all for uninstall
316 the route from FIB. */
318 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
319 struct route_node
*top
)
321 struct prefix_ipv4 p
;
322 struct route_table
*table
;
323 struct route_node
*rn
;
325 struct nexthop
*newhop
;
327 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
328 nexthop
->ifindex
= 0;
331 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
333 /* Make lookup prefix. */
334 memset (&p
, 0, sizeof (struct prefix_ipv4
));
336 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
337 p
.prefix
= nexthop
->gate
.ipv4
;
340 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
344 rn
= route_node_match (table
, (struct prefix
*) &p
);
347 route_unlock_node (rn
);
349 /* If lookup self prefix return immediately. */
353 /* Pick up selected route. */
354 for (match
= rn
->info
; match
; match
= match
->next
)
356 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
358 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
362 /* If there is no selected route or matched route is EGP, go up
365 || match
->type
== ZEBRA_ROUTE_BGP
)
369 } while (rn
&& rn
->info
== NULL
);
371 route_lock_node (rn
);
375 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
377 /* Directly point connected route. */
378 newhop
= match
->nexthop
;
379 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
380 nexthop
->ifindex
= newhop
->ifindex
;
384 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
386 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
387 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
388 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
392 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
393 nexthop
->rtype
= newhop
->type
;
394 if (newhop
->type
== NEXTHOP_TYPE_IPV4
||
395 newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
396 nexthop
->rgate
.ipv4
= newhop
->gate
.ipv4
;
397 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
398 || newhop
->type
== NEXTHOP_TYPE_IFNAME
399 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
400 nexthop
->rifindex
= newhop
->ifindex
;
416 /* If force flag is not set, do not modify falgs at all for uninstall
417 the route from FIB. */
419 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
420 struct route_node
*top
)
422 struct prefix_ipv6 p
;
423 struct route_table
*table
;
424 struct route_node
*rn
;
426 struct nexthop
*newhop
;
428 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
429 nexthop
->ifindex
= 0;
432 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
434 /* Make lookup prefix. */
435 memset (&p
, 0, sizeof (struct prefix_ipv6
));
437 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
438 p
.prefix
= nexthop
->gate
.ipv6
;
441 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
445 rn
= route_node_match (table
, (struct prefix
*) &p
);
448 route_unlock_node (rn
);
450 /* If lookup self prefix return immediately. */
454 /* Pick up selected route. */
455 for (match
= rn
->info
; match
; match
= match
->next
)
457 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
459 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
463 /* If there is no selected route or matched route is EGP, go up
466 || match
->type
== ZEBRA_ROUTE_BGP
)
470 } while (rn
&& rn
->info
== NULL
);
472 route_lock_node (rn
);
476 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
478 /* Directly point connected route. */
479 newhop
= match
->nexthop
;
481 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV6
)
482 nexthop
->ifindex
= newhop
->ifindex
;
486 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
488 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
489 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
490 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
494 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
495 nexthop
->rtype
= newhop
->type
;
496 if (newhop
->type
== NEXTHOP_TYPE_IPV6
497 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
498 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
499 nexthop
->rgate
.ipv6
= newhop
->gate
.ipv6
;
500 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
501 || newhop
->type
== NEXTHOP_TYPE_IFNAME
502 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFINDEX
503 || newhop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
)
504 nexthop
->rifindex
= newhop
->ifindex
;
518 #endif /* HAVE_IPV6 */
521 rib_match_ipv4 (struct in_addr addr
)
523 struct prefix_ipv4 p
;
524 struct route_table
*table
;
525 struct route_node
*rn
;
527 struct nexthop
*newhop
;
530 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
534 memset (&p
, 0, sizeof (struct prefix_ipv4
));
536 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
539 rn
= route_node_match (table
, (struct prefix
*) &p
);
543 route_unlock_node (rn
);
545 /* Pick up selected route. */
546 for (match
= rn
->info
; match
; match
= match
->next
)
548 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
550 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
554 /* If there is no selected route or matched route is EGP, go up
557 || match
->type
== ZEBRA_ROUTE_BGP
)
561 } while (rn
&& rn
->info
== NULL
);
563 route_lock_node (rn
);
567 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
568 /* Directly point connected route. */
572 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
573 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
583 rib_lookup_ipv4 (struct prefix_ipv4
*p
)
585 struct route_table
*table
;
586 struct route_node
*rn
;
588 struct nexthop
*nexthop
;
591 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
595 rn
= route_node_lookup (table
, (struct prefix
*) p
);
597 /* No route for this prefix. */
602 route_unlock_node (rn
);
604 for (match
= rn
->info
; match
; match
= match
->next
)
606 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
608 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
612 if (! match
|| match
->type
== ZEBRA_ROUTE_BGP
)
615 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
618 for (nexthop
= match
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
619 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
626 * This clone function, unlike its original rib_lookup_ipv4(), checks
627 * if specified IPv4 route record (prefix/mask -> gate) exists in
628 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
632 * 0: exact match found
633 * 1: a match was found with a different gate
634 * 2: connected route found
635 * 3: no matches found
638 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
)
640 struct route_table
*table
;
641 struct route_node
*rn
;
643 struct nexthop
*nexthop
;
646 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
648 return ZEBRA_RIB_LOOKUP_ERROR
;
650 /* Scan the RIB table for exactly matching RIB entry. */
651 rn
= route_node_lookup (table
, (struct prefix
*) p
);
653 /* No route for this prefix. */
655 return ZEBRA_RIB_NOTFOUND
;
658 route_unlock_node (rn
);
660 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
661 for (match
= rn
->info
; match
; match
= match
->next
)
663 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
665 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
669 /* None such found :( */
671 return ZEBRA_RIB_NOTFOUND
;
673 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
674 return ZEBRA_RIB_FOUND_CONNECTED
;
676 /* Ok, we have a cood candidate, let's check it's nexthop list... */
677 for (nexthop
= match
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
678 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
680 /* We are happy with either direct or recursive hexthop */
681 if (nexthop
->gate
.ipv4
.s_addr
== qgate
->sin
.sin_addr
.s_addr
||
682 nexthop
->rgate
.ipv4
.s_addr
== qgate
->sin
.sin_addr
.s_addr
)
683 return ZEBRA_RIB_FOUND_EXACT
;
686 if (IS_ZEBRA_DEBUG_RIB
)
688 char gate_buf
[INET_ADDRSTRLEN
], rgate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
689 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
690 inet_ntop (AF_INET
, &nexthop
->rgate
.ipv4
.s_addr
, rgate_buf
, INET_ADDRSTRLEN
);
691 inet_ntop (AF_INET
, &qgate
->sin
.sin_addr
.s_addr
, qgate_buf
, INET_ADDRSTRLEN
);
692 zlog_debug ("%s: qgate == %s, gate == %s, rgate == %s", __func__
, qgate_buf
, gate_buf
, rgate_buf
);
694 return ZEBRA_RIB_FOUND_NOGATE
;
698 return ZEBRA_RIB_NOTFOUND
;
703 rib_match_ipv6 (struct in6_addr
*addr
)
705 struct prefix_ipv6 p
;
706 struct route_table
*table
;
707 struct route_node
*rn
;
709 struct nexthop
*newhop
;
712 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
716 memset (&p
, 0, sizeof (struct prefix_ipv6
));
718 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
719 IPV6_ADDR_COPY (&p
.prefix
, addr
);
721 rn
= route_node_match (table
, (struct prefix
*) &p
);
725 route_unlock_node (rn
);
727 /* Pick up selected route. */
728 for (match
= rn
->info
; match
; match
= match
->next
)
730 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
732 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
736 /* If there is no selected route or matched route is EGP, go up
739 || match
->type
== ZEBRA_ROUTE_BGP
)
743 } while (rn
&& rn
->info
== NULL
);
745 route_lock_node (rn
);
749 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
750 /* Directly point connected route. */
754 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
755 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
763 #endif /* HAVE_IPV6 */
765 #define RIB_SYSTEM_ROUTE(R) \
766 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
768 /* This function verifies reachability of one given nexthop, which can be
769 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
770 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
771 * nexthop->ifindex will be updated appropriately as well.
772 * An existing route map can turn (otherwise active) nexthop into inactive, but
775 * The return value is the final value of 'ACTIVE' flag.
779 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
780 struct nexthop
*nexthop
, int set
)
782 struct interface
*ifp
;
783 route_map_result_t ret
= RMAP_MATCH
;
784 extern char *proto_rm
[AFI_MAX
][ZEBRA_ROUTE_MAX
+1];
785 struct route_map
*rmap
;
789 switch (nexthop
->type
)
791 case NEXTHOP_TYPE_IFINDEX
:
792 ifp
= if_lookup_by_index (nexthop
->ifindex
);
793 if (ifp
&& if_is_operative(ifp
))
794 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
796 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
798 case NEXTHOP_TYPE_IPV6_IFNAME
:
800 case NEXTHOP_TYPE_IFNAME
:
801 ifp
= if_lookup_by_name (nexthop
->ifname
);
802 if (ifp
&& if_is_operative(ifp
))
805 nexthop
->ifindex
= ifp
->ifindex
;
806 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
811 nexthop
->ifindex
= 0;
812 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
815 case NEXTHOP_TYPE_IPV4
:
816 case NEXTHOP_TYPE_IPV4_IFINDEX
:
818 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
819 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
821 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
824 case NEXTHOP_TYPE_IPV6
:
826 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
827 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
829 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
831 case NEXTHOP_TYPE_IPV6_IFINDEX
:
833 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
835 ifp
= if_lookup_by_index (nexthop
->ifindex
);
836 if (ifp
&& if_is_operative(ifp
))
837 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
839 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
843 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
844 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
846 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
849 #endif /* HAVE_IPV6 */
850 case NEXTHOP_TYPE_BLACKHOLE
:
851 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
856 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
859 if (RIB_SYSTEM_ROUTE(rib
) ||
860 (family
== AFI_IP
&& rn
->p
.family
!= AF_INET
) ||
861 (family
== AFI_IP6
&& rn
->p
.family
!= AF_INET6
))
862 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
865 if (rib
->type
>= 0 && rib
->type
< ZEBRA_ROUTE_MAX
&&
866 proto_rm
[family
][rib
->type
])
867 rmap
= route_map_lookup_by_name (proto_rm
[family
][rib
->type
]);
868 if (!rmap
&& proto_rm
[family
][ZEBRA_ROUTE_MAX
])
869 rmap
= route_map_lookup_by_name (proto_rm
[family
][ZEBRA_ROUTE_MAX
]);
871 ret
= route_map_apply(rmap
, &rn
->p
, RMAP_ZEBRA
, nexthop
);
874 if (ret
== RMAP_DENYMATCH
)
875 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
876 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
879 /* Iterate over all nexthops of the given RIB entry and refresh their
880 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
881 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
882 * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
883 * transparently passed to nexthop_active_check().
885 * Return value is the new number of active nexthops.
889 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
891 struct nexthop
*nexthop
;
892 unsigned int prev_active
, prev_index
, new_active
;
894 rib
->nexthop_active_num
= 0;
895 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
897 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
899 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
900 prev_index
= nexthop
->ifindex
;
901 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
902 rib
->nexthop_active_num
++;
903 if (prev_active
!= new_active
||
904 prev_index
!= nexthop
->ifindex
)
905 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
907 return rib
->nexthop_active_num
;
913 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
)
916 struct nexthop
*nexthop
;
918 switch (PREFIX_FAMILY (&rn
->p
))
921 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
925 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
927 #endif /* HAVE_IPV6 */
930 /* This condition is never met, if we are using rt_socket.c */
933 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
934 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
938 /* Uninstall the route from kernel. */
940 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
943 struct nexthop
*nexthop
;
945 switch (PREFIX_FAMILY (&rn
->p
))
948 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
952 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
954 #endif /* HAVE_IPV6 */
957 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
958 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
963 /* Uninstall the route from kernel. */
965 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
967 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
969 redistribute_delete (&rn
->p
, rib
);
970 if (! RIB_SYSTEM_ROUTE (rib
))
971 rib_uninstall_kernel (rn
, rib
);
972 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
976 static void rib_unlink (struct route_node
*, struct rib
*);
978 /* Core function for processing routing information base. */
980 rib_process (struct route_node
*rn
)
984 struct rib
*fib
= NULL
;
985 struct rib
*select
= NULL
;
986 struct rib
*del
= NULL
;
988 struct nexthop
*nexthop
= NULL
;
989 char buf
[INET6_ADDRSTRLEN
];
993 if (IS_ZEBRA_DEBUG_RIB
|| IS_ZEBRA_DEBUG_RIB_Q
)
994 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
996 for (rib
= rn
->info
; rib
; rib
= next
)
998 /* The next pointer is saved, because current pointer
999 * may be passed to rib_unlink() in the middle of iteration.
1003 /* Currently installed rib. */
1004 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1006 assert (fib
== NULL
);
1010 /* Unlock removed routes, so they'll be freed, bar the FIB entry,
1011 * which we need to do do further work with below.
1013 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1017 if (IS_ZEBRA_DEBUG_RIB
)
1018 zlog_debug ("%s: %s/%d: rn %p, removing rib %p", __func__
,
1019 buf
, rn
->p
.prefixlen
, rn
, rib
);
1020 rib_unlink (rn
, rib
);
1028 /* Skip unreachable nexthop. */
1029 if (! nexthop_active_update (rn
, rib
, 0))
1032 /* Infinit distance. */
1033 if (rib
->distance
== DISTANCE_INFINITY
)
1036 /* Newly selected rib, the common case. */
1043 /* filter route selection in following order:
1044 * - connected beats other types
1045 * - lower distance beats higher
1046 * - lower metric beats higher for equal distance
1047 * - last, hence oldest, route wins tie break.
1050 /* Connected routes. Pick the last connected
1051 * route of the set of lowest metric connected routes.
1053 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1055 if (select
->type
!= ZEBRA_ROUTE_CONNECT
1056 || rib
->metric
<= select
->metric
)
1060 else if (select
->type
== ZEBRA_ROUTE_CONNECT
)
1063 /* higher distance loses */
1064 if (rib
->distance
> select
->distance
)
1068 if (rib
->distance
< select
->distance
)
1074 /* metric tie-breaks equal distance */
1075 if (rib
->metric
<= select
->metric
)
1077 } /* for (rib = rn->info; rib; rib = next) */
1079 /* After the cycle is finished, the following pointers will be set:
1080 * select --- the winner RIB entry, if any was found, otherwise NULL
1081 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1082 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1086 /* Same RIB entry is selected. Update FIB and finish. */
1087 if (select
&& select
== fib
)
1089 if (IS_ZEBRA_DEBUG_RIB
)
1090 zlog_debug ("%s: %s/%d: Updating existing route, select %p, fib %p",
1091 __func__
, buf
, rn
->p
.prefixlen
, select
, fib
);
1092 if (CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
1094 redistribute_delete (&rn
->p
, select
);
1095 if (! RIB_SYSTEM_ROUTE (select
))
1096 rib_uninstall_kernel (rn
, select
);
1098 /* Set real nexthop. */
1099 nexthop_active_update (rn
, select
, 1);
1101 if (! RIB_SYSTEM_ROUTE (select
))
1102 rib_install_kernel (rn
, select
);
1103 redistribute_add (&rn
->p
, select
);
1105 else if (! RIB_SYSTEM_ROUTE (select
))
1107 /* Housekeeping code to deal with
1108 race conditions in kernel with linux
1109 netlink reporting interface up before IPv4 or IPv6 protocol
1110 is ready to add routes.
1111 This makes sure the routes are IN the kernel.
1114 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1115 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1121 rib_install_kernel (rn
, select
);
1126 /* At this point we either haven't found the best RIB entry or it is
1127 * different from what we currently intend to flag with SELECTED. In both
1128 * cases, if a RIB block is present in FIB, it should be withdrawn.
1132 if (IS_ZEBRA_DEBUG_RIB
)
1133 zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__
,
1134 buf
, rn
->p
.prefixlen
, fib
);
1135 redistribute_delete (&rn
->p
, fib
);
1136 if (! RIB_SYSTEM_ROUTE (fib
))
1137 rib_uninstall_kernel (rn
, fib
);
1138 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1140 /* Set real nexthop. */
1141 nexthop_active_update (rn
, fib
, 1);
1144 /* Regardless of some RIB entry being SELECTED or not before, now we can
1145 * tell, that if a new winner exists, FIB is still not updated with this
1146 * data, but ready to be.
1150 if (IS_ZEBRA_DEBUG_RIB
)
1151 zlog_debug ("%s: %s/%d: Adding route, select %p", __func__
, buf
,
1152 rn
->p
.prefixlen
, select
);
1153 /* Set real nexthop. */
1154 nexthop_active_update (rn
, select
, 1);
1156 if (! RIB_SYSTEM_ROUTE (select
))
1157 rib_install_kernel (rn
, select
);
1158 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1159 redistribute_add (&rn
->p
, select
);
1162 /* FIB route was removed, should be deleted */
1165 if (IS_ZEBRA_DEBUG_RIB
)
1166 zlog_debug ("%s: %s/%d: Deleting fib %p, rn %p", __func__
, buf
,
1167 rn
->p
.prefixlen
, del
, rn
);
1168 rib_unlink (rn
, del
);
1172 if (IS_ZEBRA_DEBUG_RIB_Q
)
1173 zlog_debug ("%s: %s/%d: rn %p dequeued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1176 /* Take a list of route_node structs and return 1, if there was a record
1177 * picked from it and processed by rib_process(). Don't process more,
1178 * than one RN record; operate only in the specified sub-queue.
1181 process_subq (struct list
* subq
, u_char qindex
)
1183 struct listnode
*lnode
= listhead (subq
);
1184 struct route_node
*rnode
;
1189 rnode
= listgetdata (lnode
);
1190 rib_process (rnode
);
1192 if (rnode
->info
) /* The first RIB record is holding the flags bitmask. */
1193 UNSET_FLAG (((struct rib
*)rnode
->info
)->rn_status
, RIB_ROUTE_QUEUED(qindex
));
1197 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1198 __func__
, rnode
, rnode
->lock
);
1199 zlog_backtrace(LOG_DEBUG
);
1202 route_unlock_node (rnode
);
1203 list_delete_node (subq
, lnode
);
1207 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1208 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1209 * is pointed to the meta queue structure.
1211 static wq_item_status
1212 meta_queue_process (struct work_queue
*dummy
, void *data
)
1214 struct meta_queue
* mq
= data
;
1217 for (i
= 0; i
< MQ_SIZE
; i
++)
1218 if (process_subq (mq
->subq
[i
], i
))
1223 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1226 /* Map from rib types to queue type (priority) in meta queue */
1227 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1228 [ZEBRA_ROUTE_SYSTEM
] = 4,
1229 [ZEBRA_ROUTE_KERNEL
] = 0,
1230 [ZEBRA_ROUTE_CONNECT
] = 0,
1231 [ZEBRA_ROUTE_STATIC
] = 1,
1232 [ZEBRA_ROUTE_RIP
] = 2,
1233 [ZEBRA_ROUTE_RIPNG
] = 2,
1234 [ZEBRA_ROUTE_OSPF
] = 2,
1235 [ZEBRA_ROUTE_OSPF6
] = 2,
1236 [ZEBRA_ROUTE_ISIS
] = 2,
1237 [ZEBRA_ROUTE_BGP
] = 3,
1238 [ZEBRA_ROUTE_HSLS
] = 4,
1239 [ZEBRA_ROUTE_BABEL
] = 2,
1242 /* Look into the RN and queue it into one or more priority queues,
1243 * increasing the size for each data push done.
1246 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
1249 char buf
[INET6_ADDRSTRLEN
];
1251 if (IS_ZEBRA_DEBUG_RIB_Q
)
1252 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1254 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1256 u_char qindex
= meta_queue_map
[rib
->type
];
1258 /* Invariant: at this point we always have rn->info set. */
1259 if (CHECK_FLAG (((struct rib
*)rn
->info
)->rn_status
, RIB_ROUTE_QUEUED(qindex
)))
1261 if (IS_ZEBRA_DEBUG_RIB_Q
)
1262 zlog_debug ("%s: %s/%d: rn %p is already queued in sub-queue %u",
1263 __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1267 SET_FLAG (((struct rib
*)rn
->info
)->rn_status
, RIB_ROUTE_QUEUED(qindex
));
1268 listnode_add (mq
->subq
[qindex
], rn
);
1269 route_lock_node (rn
);
1272 if (IS_ZEBRA_DEBUG_RIB_Q
)
1273 zlog_debug ("%s: %s/%d: queued rn %p into sub-queue %u",
1274 __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1278 /* Add route_node to work queue and schedule processing */
1280 rib_queue_add (struct zebra_t
*zebra
, struct route_node
*rn
)
1282 char buf
[INET_ADDRSTRLEN
];
1283 assert (zebra
&& rn
);
1285 if (IS_ZEBRA_DEBUG_RIB_Q
)
1286 inet_ntop (AF_INET
, &rn
->p
.u
.prefix
, buf
, INET_ADDRSTRLEN
);
1288 /* Pointless to queue a route_node with no RIB entries to add or remove */
1291 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1292 __func__
, rn
, rn
->lock
);
1293 zlog_backtrace(LOG_DEBUG
);
1297 if (IS_ZEBRA_DEBUG_RIB_Q
)
1298 zlog_info ("%s: %s/%d: work queue added", __func__
, buf
, rn
->p
.prefixlen
);
1302 if (zebra
->ribq
== NULL
)
1304 zlog_err ("%s: work_queue does not exist!", __func__
);
1309 * The RIB queue should normally be either empty or holding the only
1310 * work_queue_item element. In the latter case this element would
1311 * hold a pointer to the meta queue structure, which must be used to
1312 * actually queue the route nodes to process. So create the MQ
1313 * holder, if necessary, then push the work into it in any case.
1314 * This semantics was introduced after 0.99.9 release.
1316 if (!zebra
->ribq
->items
->count
)
1317 work_queue_add (zebra
->ribq
, zebra
->mq
);
1319 rib_meta_queue_add (zebra
->mq
, rn
);
1321 if (IS_ZEBRA_DEBUG_RIB_Q
)
1322 zlog_debug ("%s: %s/%d: rn %p queued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1327 /* Create new meta queue.
1328 A destructor function doesn't seem to be necessary here.
1330 static struct meta_queue
*
1331 meta_queue_new (void)
1333 struct meta_queue
*new;
1336 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
1339 for (i
= 0; i
< MQ_SIZE
; i
++)
1341 new->subq
[i
] = list_new ();
1342 assert(new->subq
[i
]);
1348 /* initialise zebra rib work queue */
1350 rib_queue_init (struct zebra_t
*zebra
)
1354 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1355 "route_node processing")))
1357 zlog_err ("%s: could not initialise work queue!", __func__
);
1361 /* fill in the work queue spec */
1362 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1363 zebra
->ribq
->spec
.errorfunc
= NULL
;
1364 /* XXX: TODO: These should be runtime configurable via vty */
1365 zebra
->ribq
->spec
.max_retries
= 3;
1366 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
1368 if (!(zebra
->mq
= meta_queue_new ()))
1370 zlog_err ("%s: could not initialise meta queue!", __func__
);
1376 /* RIB updates are processed via a queue of pointers to route_nodes.
1378 * The queue length is bounded by the maximal size of the routing table,
1379 * as a route_node will not be requeued, if already queued.
1381 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
1382 * state, or static_install_ipv{4,6} (when an existing RIB is updated)
1383 * and then submit route_node to queue for best-path selection later.
1384 * Order of add/delete state changes are preserved for any given RIB.
1386 * Deleted RIBs are reaped during best-path selection.
1389 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
1390 * |-------->| | best RIB, if required
1392 * static_install->|->rib_addqueue...... -> rib_process
1394 * |-------->| |-> rib_unlink
1395 * |-> set RIB_ENTRY_REMOVE |
1396 * rib_delnode (RIB freed)
1399 * Queueing state for a route_node is kept in the head RIB entry, this
1400 * state must be preserved as and when the head RIB entry of a
1401 * route_node is changed by rib_unlink / rib_link. A small complication,
1402 * but saves having to allocate a dedicated object for this.
1404 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1406 * - route_nodes: refcounted by:
1407 * - RIBs attached to route_node:
1408 * - managed by: rib_link/unlink
1409 * - route_node processing queue
1410 * - managed by: rib_addqueue, rib_process.
1414 /* Add RIB to head of the route node. */
1416 rib_link (struct route_node
*rn
, struct rib
*rib
)
1419 char buf
[INET6_ADDRSTRLEN
];
1423 route_lock_node (rn
); /* rn route table reference */
1425 if (IS_ZEBRA_DEBUG_RIB
)
1427 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1428 zlog_debug ("%s: %s/%d: rn %p, rib %p", __func__
,
1429 buf
, rn
->p
.prefixlen
, rn
, rib
);
1435 if (IS_ZEBRA_DEBUG_RIB
)
1436 zlog_debug ("%s: %s/%d: new head, rn_status copied over", __func__
,
1437 buf
, rn
->p
.prefixlen
);
1439 /* Transfer the rn status flags to the new head RIB */
1440 rib
->rn_status
= head
->rn_status
;
1444 rib_queue_add (&zebrad
, rn
);
1448 rib_addnode (struct route_node
*rn
, struct rib
*rib
)
1450 /* RIB node has been un-removed before route-node is processed.
1451 * route_node must hence already be on the queue for processing..
1453 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1455 if (IS_ZEBRA_DEBUG_RIB
)
1457 char buf
[INET6_ADDRSTRLEN
];
1458 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1459 zlog_debug ("%s: %s/%d: rn %p, un-removed rib %p",
1460 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1462 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1469 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
1471 struct nexthop
*nexthop
, *next
;
1472 char buf
[INET6_ADDRSTRLEN
];
1476 if (IS_ZEBRA_DEBUG_RIB
)
1478 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1479 zlog_debug ("%s: %s/%d: rn %p, rib %p",
1480 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1484 rib
->next
->prev
= rib
->prev
;
1487 rib
->prev
->next
= rib
->next
;
1490 rn
->info
= rib
->next
;
1494 if (IS_ZEBRA_DEBUG_RIB
)
1495 zlog_debug ("%s: %s/%d: rn %p, rib %p, new head copy",
1496 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1497 rib
->next
->rn_status
= rib
->rn_status
;
1501 /* free RIB and nexthops */
1502 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= next
)
1504 next
= nexthop
->next
;
1505 nexthop_free (nexthop
);
1507 XFREE (MTYPE_RIB
, rib
);
1509 route_unlock_node (rn
); /* rn route table reference */
1513 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
1515 if (IS_ZEBRA_DEBUG_RIB
)
1517 char buf
[INET6_ADDRSTRLEN
];
1518 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1519 zlog_debug ("%s: %s/%d: rn %p, rib %p, removing", __func__
,
1520 buf
, rn
->p
.prefixlen
, rn
, rib
);
1522 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1523 rib_queue_add (&zebrad
, rn
);
1527 rib_add_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1528 struct in_addr
*gate
, struct in_addr
*src
,
1529 unsigned int ifindex
, u_int32_t vrf_id
,
1530 u_int32_t metric
, u_char distance
, safi_t safi
)
1533 struct rib
*same
= NULL
;
1534 struct route_table
*table
;
1535 struct route_node
*rn
;
1536 struct nexthop
*nexthop
;
1539 table
= vrf_table (AFI_IP
, safi
, 0);
1543 /* Make it sure prefixlen is applied to the prefix. */
1544 apply_mask_ipv4 (p
);
1546 /* Set default distance by route type. */
1549 if ((unsigned)type
>= sizeof(route_info
) / sizeof(route_info
[0]))
1552 distance
= route_info
[type
].distance
;
1554 /* iBGP distance is 200. */
1555 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1559 /* Lookup route node.*/
1560 rn
= route_node_get (table
, (struct prefix
*) p
);
1562 /* If same type of route are installed, treat it as a implicit
1564 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1566 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1569 if (rib
->type
!= type
)
1571 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
1576 /* Duplicate connected route comes in. */
1577 else if ((nexthop
= rib
->nexthop
) &&
1578 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
1579 nexthop
->ifindex
== ifindex
&&
1580 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1587 /* Allocate new rib structure. */
1588 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1590 rib
->distance
= distance
;
1592 rib
->metric
= metric
;
1593 rib
->table
= vrf_id
;
1594 rib
->nexthop_num
= 0;
1595 rib
->uptime
= time (NULL
);
1597 /* Nexthop settings. */
1601 nexthop_ipv4_ifindex_add (rib
, gate
, src
, ifindex
);
1603 nexthop_ipv4_add (rib
, gate
, src
);
1606 nexthop_ifindex_add (rib
, ifindex
);
1608 /* If this route is kernel route, set FIB flag to the route. */
1609 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1610 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1611 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1613 /* Link new rib to node.*/
1614 if (IS_ZEBRA_DEBUG_RIB
)
1615 zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__
, rn
, rib
);
1616 rib_addnode (rn
, rib
);
1618 /* Free implicit route.*/
1621 if (IS_ZEBRA_DEBUG_RIB
)
1622 zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__
, rn
, rib
);
1623 rib_delnode (rn
, same
);
1626 route_unlock_node (rn
);
1630 /* This function dumps the contents of a given RIB entry into
1631 * standard debug log. Calling function name and IP prefix in
1632 * question are passed as 1st and 2nd arguments.
1635 void rib_dump (const char * func
, const struct prefix_ipv4
* p
, const struct rib
* rib
)
1637 char straddr1
[INET_ADDRSTRLEN
], straddr2
[INET_ADDRSTRLEN
];
1638 struct nexthop
*nexthop
;
1640 inet_ntop (AF_INET
, &p
->prefix
, straddr1
, INET_ADDRSTRLEN
);
1641 zlog_debug ("%s: dumping RIB entry %p for %s/%d", func
, rib
, straddr1
, p
->prefixlen
);
1644 "%s: refcnt == %lu, uptime == %lu, type == %u, table == %d",
1647 (unsigned long) rib
->uptime
,
1653 "%s: metric == %u, distance == %u, flags == %u, status == %u",
1662 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
1665 rib
->nexthop_active_num
,
1666 rib
->nexthop_fib_num
1668 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1670 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, straddr1
, INET_ADDRSTRLEN
);
1671 inet_ntop (AF_INET
, &nexthop
->rgate
.ipv4
.s_addr
, straddr2
, INET_ADDRSTRLEN
);
1674 "%s: NH %s (%s) with flags %s%s%s",
1678 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
1679 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
1680 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
1683 zlog_debug ("%s: dump complete", func
);
1686 /* This is an exported helper to rtm_read() to dump the strange
1687 * RIB entry found by rib_lookup_ipv4_route()
1690 void rib_lookup_and_dump (struct prefix_ipv4
* p
)
1692 struct route_table
*table
;
1693 struct route_node
*rn
;
1695 char prefix_buf
[INET_ADDRSTRLEN
];
1698 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1701 zlog_err ("%s: vrf_table() returned NULL", __func__
);
1705 inet_ntop (AF_INET
, &p
->prefix
.s_addr
, prefix_buf
, INET_ADDRSTRLEN
);
1706 /* Scan the RIB table for exactly matching RIB entry. */
1707 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1709 /* No route for this prefix. */
1712 zlog_debug ("%s: lookup failed for %s/%d", __func__
, prefix_buf
, p
->prefixlen
);
1717 route_unlock_node (rn
);
1720 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1724 "%s: rn %p, rib %p: %s, %s",
1728 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
1729 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
1731 rib_dump (__func__
, p
, rib
);
1735 /* Check if requested address assignment will fail due to another
1736 * route being installed by zebra in FIB already. Take necessary
1737 * actions, if needed: remove such a route from FIB and deSELECT
1738 * corresponding RIB entry. Then put affected RN into RIBQ head.
1740 void rib_lookup_and_pushup (struct prefix_ipv4
* p
)
1742 struct route_table
*table
;
1743 struct route_node
*rn
;
1745 unsigned changed
= 0;
1747 if (NULL
== (table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0)))
1749 zlog_err ("%s: vrf_table() returned NULL", __func__
);
1753 /* No matches would be the simplest case. */
1754 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
1758 route_unlock_node (rn
);
1760 /* Check all RIB entries. In case any changes have to be done, requeue
1761 * the RN into RIBQ head. If the routing message about the new connected
1762 * route (generated by the IP address we are going to assign very soon)
1763 * comes before the RIBQ is processed, the new RIB entry will join
1764 * RIBQ record already on head. This is necessary for proper revalidation
1765 * of the rest of the RIB.
1767 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1769 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) &&
1770 ! RIB_SYSTEM_ROUTE (rib
))
1773 if (IS_ZEBRA_DEBUG_RIB
)
1775 char buf
[INET_ADDRSTRLEN
];
1776 inet_ntop (rn
->p
.family
, &p
->prefix
, buf
, INET_ADDRSTRLEN
);
1777 zlog_debug ("%s: freeing way for connected prefix %s/%d", __func__
, buf
, p
->prefixlen
);
1778 rib_dump (__func__
, (struct prefix_ipv4
*)&rn
->p
, rib
);
1780 rib_uninstall (rn
, rib
);
1784 rib_queue_add (&zebrad
, rn
);
1788 rib_add_ipv4_multipath (struct prefix_ipv4
*p
, struct rib
*rib
, safi_t safi
)
1790 struct route_table
*table
;
1791 struct route_node
*rn
;
1793 struct nexthop
*nexthop
;
1796 table
= vrf_table (AFI_IP
, safi
, 0);
1800 /* Make it sure prefixlen is applied to the prefix. */
1801 apply_mask_ipv4 (p
);
1803 /* Set default distance by route type. */
1804 if (rib
->distance
== 0)
1806 rib
->distance
= route_info
[rib
->type
].distance
;
1808 /* iBGP distance is 200. */
1809 if (rib
->type
== ZEBRA_ROUTE_BGP
1810 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
1811 rib
->distance
= 200;
1814 /* Lookup route node.*/
1815 rn
= route_node_get (table
, (struct prefix
*) p
);
1817 /* If same type of route are installed, treat it as a implicit
1819 for (same
= rn
->info
; same
; same
= same
->next
)
1821 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
1824 if (same
->type
== rib
->type
&& same
->table
== rib
->table
1825 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
1829 /* If this route is kernel route, set FIB flag to the route. */
1830 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
1831 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1832 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1834 /* Link new rib to node.*/
1835 rib_addnode (rn
, rib
);
1836 if (IS_ZEBRA_DEBUG_RIB
)
1838 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
1840 rib_dump (__func__
, p
, rib
);
1843 /* Free implicit route.*/
1846 if (IS_ZEBRA_DEBUG_RIB
)
1848 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
1849 __func__
, rn
, same
);
1850 rib_dump (__func__
, p
, same
);
1852 rib_delnode (rn
, same
);
1855 route_unlock_node (rn
);
1859 /* XXX factor with rib_delete_ipv6 */
1861 rib_delete_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1862 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
, safi_t safi
)
1864 struct route_table
*table
;
1865 struct route_node
*rn
;
1867 struct rib
*fib
= NULL
;
1868 struct rib
*same
= NULL
;
1869 struct nexthop
*nexthop
;
1870 char buf1
[INET_ADDRSTRLEN
];
1871 char buf2
[INET_ADDRSTRLEN
];
1874 table
= vrf_table (AFI_IP
, safi
, 0);
1879 apply_mask_ipv4 (p
);
1881 if (IS_ZEBRA_DEBUG_KERNEL
&& gate
)
1882 zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
1883 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1888 /* Lookup route node. */
1889 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1892 if (IS_ZEBRA_DEBUG_KERNEL
)
1895 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
1896 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1898 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
1901 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
1902 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1906 return ZEBRA_ERR_RTNOEXIST
;
1909 /* Lookup same type route. */
1910 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1912 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1915 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1918 if (rib
->type
!= type
)
1920 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
1921 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
1923 if (nexthop
->ifindex
!= ifindex
)
1928 route_unlock_node (rn
);
1929 route_unlock_node (rn
);
1935 /* Make sure that the route found has the same gateway. */
1936 else if (gate
== NULL
||
1937 ((nexthop
= rib
->nexthop
) &&
1938 (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
1939 IPV4_ADDR_SAME (&nexthop
->rgate
.ipv4
, gate
))))
1946 /* If same type of route can't be found and this message is from
1950 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
1953 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1954 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1956 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1960 if (IS_ZEBRA_DEBUG_KERNEL
)
1963 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
1964 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1966 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
1970 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
1971 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1976 route_unlock_node (rn
);
1977 return ZEBRA_ERR_RTNOEXIST
;
1982 rib_delnode (rn
, same
);
1984 route_unlock_node (rn
);
1988 /* Install static route into rib. */
1990 static_install_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
1993 struct route_node
*rn
;
1994 struct route_table
*table
;
1997 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2001 /* Lookup existing route */
2002 rn
= route_node_get (table
, p
);
2003 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2005 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2008 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2014 /* Same distance static route is there. Update it with new
2016 route_unlock_node (rn
);
2019 case STATIC_IPV4_GATEWAY
:
2020 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2022 case STATIC_IPV4_IFNAME
:
2023 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2025 case STATIC_IPV4_BLACKHOLE
:
2026 nexthop_blackhole_add (rib
);
2029 rib_queue_add (&zebrad
, rn
);
2033 /* This is new static route. */
2034 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2036 rib
->type
= ZEBRA_ROUTE_STATIC
;
2037 rib
->distance
= si
->distance
;
2039 rib
->nexthop_num
= 0;
2043 case STATIC_IPV4_GATEWAY
:
2044 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2046 case STATIC_IPV4_IFNAME
:
2047 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2049 case STATIC_IPV4_BLACKHOLE
:
2050 nexthop_blackhole_add (rib
);
2054 /* Save the flags of this static routes (reject, blackhole) */
2055 rib
->flags
= si
->flags
;
2057 /* Link this rib to the tree. */
2058 rib_addnode (rn
, rib
);
2063 static_ipv4_nexthop_same (struct nexthop
*nexthop
, struct static_ipv4
*si
)
2065 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
2066 && si
->type
== STATIC_IPV4_GATEWAY
2067 && IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &si
->gate
.ipv4
))
2069 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2070 && si
->type
== STATIC_IPV4_IFNAME
2071 && strcmp (nexthop
->ifname
, si
->gate
.ifname
) == 0)
2073 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
2074 && si
->type
== STATIC_IPV4_BLACKHOLE
)
2079 /* Uninstall static route from RIB. */
2081 static_uninstall_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
2083 struct route_node
*rn
;
2085 struct nexthop
*nexthop
;
2086 struct route_table
*table
;
2089 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2093 /* Lookup existing route with type and distance. */
2094 rn
= route_node_lookup (table
, p
);
2098 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2100 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2103 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2109 route_unlock_node (rn
);
2113 /* Lookup nexthop. */
2114 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2115 if (static_ipv4_nexthop_same (nexthop
, si
))
2118 /* Can't find nexthop. */
2121 route_unlock_node (rn
);
2125 /* Check nexthop. */
2126 if (rib
->nexthop_num
== 1)
2127 rib_delnode (rn
, rib
);
2130 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2131 rib_uninstall (rn
, rib
);
2132 nexthop_delete (rib
, nexthop
);
2133 nexthop_free (nexthop
);
2134 rib_queue_add (&zebrad
, rn
);
2137 route_unlock_node (rn
);
2140 /* Add static route into static route configuration. */
2142 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2143 u_char flags
, u_char distance
, u_int32_t vrf_id
)
2146 struct route_node
*rn
;
2147 struct static_ipv4
*si
;
2148 struct static_ipv4
*pp
;
2149 struct static_ipv4
*cp
;
2150 struct static_ipv4
*update
= NULL
;
2151 struct route_table
*stable
;
2154 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2158 /* Lookup static route prefix. */
2159 rn
= route_node_get (stable
, p
);
2163 type
= STATIC_IPV4_GATEWAY
;
2165 type
= STATIC_IPV4_IFNAME
;
2167 type
= STATIC_IPV4_BLACKHOLE
;
2169 /* Do nothing if there is a same static route. */
2170 for (si
= rn
->info
; si
; si
= si
->next
)
2172 if (type
== si
->type
2173 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2174 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2176 if (distance
== si
->distance
)
2178 route_unlock_node (rn
);
2186 /* Distance changed. */
2188 static_delete_ipv4 (p
, gate
, ifname
, update
->distance
, vrf_id
);
2190 /* Make new static route structure. */
2191 si
= XCALLOC (MTYPE_STATIC_IPV4
, sizeof (struct static_ipv4
));
2194 si
->distance
= distance
;
2198 si
->gate
.ipv4
= *gate
;
2200 si
->gate
.ifname
= XSTRDUP (0, ifname
);
2202 /* Add new static route information to the tree with sort by
2203 distance value and gateway address. */
2204 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2206 if (si
->distance
< cp
->distance
)
2208 if (si
->distance
> cp
->distance
)
2210 if (si
->type
== STATIC_IPV4_GATEWAY
&& cp
->type
== STATIC_IPV4_GATEWAY
)
2212 if (ntohl (si
->gate
.ipv4
.s_addr
) < ntohl (cp
->gate
.ipv4
.s_addr
))
2214 if (ntohl (si
->gate
.ipv4
.s_addr
) > ntohl (cp
->gate
.ipv4
.s_addr
))
2219 /* Make linked list. */
2229 /* Install into rib. */
2230 static_install_ipv4 (p
, si
);
2235 /* Delete static route from static route configuration. */
2237 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2238 u_char distance
, u_int32_t vrf_id
)
2241 struct route_node
*rn
;
2242 struct static_ipv4
*si
;
2243 struct route_table
*stable
;
2246 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2250 /* Lookup static route prefix. */
2251 rn
= route_node_lookup (stable
, p
);
2257 type
= STATIC_IPV4_GATEWAY
;
2259 type
= STATIC_IPV4_IFNAME
;
2261 type
= STATIC_IPV4_BLACKHOLE
;
2263 /* Find same static route is the tree */
2264 for (si
= rn
->info
; si
; si
= si
->next
)
2265 if (type
== si
->type
2266 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2267 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2270 /* Can't find static route. */
2273 route_unlock_node (rn
);
2277 /* Install into rib. */
2278 static_uninstall_ipv4 (p
, si
);
2280 /* Unlink static route from linked list. */
2282 si
->prev
->next
= si
->next
;
2284 rn
->info
= si
->next
;
2286 si
->next
->prev
= si
->prev
;
2287 route_unlock_node (rn
);
2289 /* Free static route configuration. */
2291 XFREE (0, si
->gate
.ifname
);
2292 XFREE (MTYPE_STATIC_IPV4
, si
);
2294 route_unlock_node (rn
);
2302 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
2303 struct in6_addr
*gate
, unsigned int ifindex
, int table
)
2305 if (type
== ZEBRA_ROUTE_CONNECT
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)) {
2306 #if defined (MUSICA) || defined (LINUX)
2307 /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
2308 if (p
->prefixlen
== 96)
2313 if (type
== ZEBRA_ROUTE_KERNEL
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)
2314 && p
->prefixlen
== 96 && gate
&& IN6_IS_ADDR_UNSPECIFIED (gate
))
2316 kernel_delete_ipv6_old (p
, gate
, ifindex
, 0, table
);
2323 rib_add_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2324 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
,
2325 u_int32_t metric
, u_char distance
, safi_t safi
)
2328 struct rib
*same
= NULL
;
2329 struct route_table
*table
;
2330 struct route_node
*rn
;
2331 struct nexthop
*nexthop
;
2334 table
= vrf_table (AFI_IP6
, safi
, 0);
2338 /* Make sure mask is applied. */
2339 apply_mask_ipv6 (p
);
2341 /* Set default distance by route type. */
2343 distance
= route_info
[type
].distance
;
2345 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2348 /* Filter bogus route. */
2349 if (rib_bogus_ipv6 (type
, p
, gate
, ifindex
, 0))
2352 /* Lookup route node.*/
2353 rn
= route_node_get (table
, (struct prefix
*) p
);
2355 /* If same type of route are installed, treat it as a implicit
2357 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2359 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2362 if (rib
->type
!= type
)
2364 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2369 else if ((nexthop
= rib
->nexthop
) &&
2370 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2371 nexthop
->ifindex
== ifindex
)
2378 /* Allocate new rib structure. */
2379 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2382 rib
->distance
= distance
;
2384 rib
->metric
= metric
;
2385 rib
->table
= vrf_id
;
2386 rib
->nexthop_num
= 0;
2387 rib
->uptime
= time (NULL
);
2389 /* Nexthop settings. */
2393 nexthop_ipv6_ifindex_add (rib
, gate
, ifindex
);
2395 nexthop_ipv6_add (rib
, gate
);
2398 nexthop_ifindex_add (rib
, ifindex
);
2400 /* If this route is kernel route, set FIB flag to the route. */
2401 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2402 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2403 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2405 /* Link new rib to node.*/
2406 rib_addnode (rn
, rib
);
2408 /* Free implicit route.*/
2410 rib_delnode (rn
, same
);
2412 route_unlock_node (rn
);
2416 /* XXX factor with rib_delete_ipv6 */
2418 rib_delete_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2419 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
, safi_t safi
)
2421 struct route_table
*table
;
2422 struct route_node
*rn
;
2424 struct rib
*fib
= NULL
;
2425 struct rib
*same
= NULL
;
2426 struct nexthop
*nexthop
;
2427 char buf1
[INET6_ADDRSTRLEN
];
2428 char buf2
[INET6_ADDRSTRLEN
];
2431 apply_mask_ipv6 (p
);
2434 table
= vrf_table (AFI_IP6
, safi
, 0);
2438 /* Lookup route node. */
2439 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2442 if (IS_ZEBRA_DEBUG_KERNEL
)
2445 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
2446 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2448 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
2451 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
2452 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2456 return ZEBRA_ERR_RTNOEXIST
;
2459 /* Lookup same type route. */
2460 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2462 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2465 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2468 if (rib
->type
!= type
)
2470 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2471 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
)
2473 if (nexthop
->ifindex
!= ifindex
)
2478 route_unlock_node (rn
);
2479 route_unlock_node (rn
);
2485 /* Make sure that the route found has the same gateway. */
2486 else if (gate
== NULL
||
2487 ((nexthop
= rib
->nexthop
) &&
2488 (IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
) ||
2489 IPV6_ADDR_SAME (&nexthop
->rgate
.ipv6
, gate
))))
2496 /* If same type of route can't be found and this message is from
2500 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
2503 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2504 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2506 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
2510 if (IS_ZEBRA_DEBUG_KERNEL
)
2513 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
2514 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2516 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
2520 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
2521 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2526 route_unlock_node (rn
);
2527 return ZEBRA_ERR_RTNOEXIST
;
2532 rib_delnode (rn
, same
);
2534 route_unlock_node (rn
);
2538 /* Install static route into rib. */
2540 static_install_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2543 struct route_table
*table
;
2544 struct route_node
*rn
;
2547 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2551 /* Lookup existing route */
2552 rn
= route_node_get (table
, p
);
2553 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2555 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2558 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2564 /* Same distance static route is there. Update it with new
2566 route_unlock_node (rn
);
2570 case STATIC_IPV6_GATEWAY
:
2571 nexthop_ipv6_add (rib
, &si
->ipv6
);
2573 case STATIC_IPV6_IFNAME
:
2574 nexthop_ifname_add (rib
, si
->ifname
);
2576 case STATIC_IPV6_GATEWAY_IFNAME
:
2577 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2580 rib_queue_add (&zebrad
, rn
);
2584 /* This is new static route. */
2585 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2587 rib
->type
= ZEBRA_ROUTE_STATIC
;
2588 rib
->distance
= si
->distance
;
2590 rib
->nexthop_num
= 0;
2594 case STATIC_IPV6_GATEWAY
:
2595 nexthop_ipv6_add (rib
, &si
->ipv6
);
2597 case STATIC_IPV6_IFNAME
:
2598 nexthop_ifname_add (rib
, si
->ifname
);
2600 case STATIC_IPV6_GATEWAY_IFNAME
:
2601 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2605 /* Save the flags of this static routes (reject, blackhole) */
2606 rib
->flags
= si
->flags
;
2608 /* Link this rib to the tree. */
2609 rib_addnode (rn
, rib
);
2614 static_ipv6_nexthop_same (struct nexthop
*nexthop
, struct static_ipv6
*si
)
2616 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
2617 && si
->type
== STATIC_IPV6_GATEWAY
2618 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
))
2620 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2621 && si
->type
== STATIC_IPV6_IFNAME
2622 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2624 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
2625 && si
->type
== STATIC_IPV6_GATEWAY_IFNAME
2626 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
)
2627 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2633 static_uninstall_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2635 struct route_table
*table
;
2636 struct route_node
*rn
;
2638 struct nexthop
*nexthop
;
2641 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2645 /* Lookup existing route with type and distance. */
2646 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2650 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2652 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2655 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2661 route_unlock_node (rn
);
2665 /* Lookup nexthop. */
2666 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2667 if (static_ipv6_nexthop_same (nexthop
, si
))
2670 /* Can't find nexthop. */
2673 route_unlock_node (rn
);
2677 /* Check nexthop. */
2678 if (rib
->nexthop_num
== 1)
2680 rib_delnode (rn
, rib
);
2684 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2685 rib_uninstall (rn
, rib
);
2686 nexthop_delete (rib
, nexthop
);
2687 nexthop_free (nexthop
);
2688 rib_queue_add (&zebrad
, rn
);
2691 route_unlock_node (rn
);
2694 /* Add static route into static route configuration. */
2696 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2697 const char *ifname
, u_char flags
, u_char distance
,
2700 struct route_node
*rn
;
2701 struct static_ipv6
*si
;
2702 struct static_ipv6
*pp
;
2703 struct static_ipv6
*cp
;
2704 struct route_table
*stable
;
2707 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2712 (type
== STATIC_IPV6_GATEWAY
|| type
== STATIC_IPV6_GATEWAY_IFNAME
))
2716 (type
== STATIC_IPV6_GATEWAY_IFNAME
|| type
== STATIC_IPV6_IFNAME
))
2719 /* Lookup static route prefix. */
2720 rn
= route_node_get (stable
, p
);
2722 /* Do nothing if there is a same static route. */
2723 for (si
= rn
->info
; si
; si
= si
->next
)
2725 if (distance
== si
->distance
2727 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2728 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2730 route_unlock_node (rn
);
2735 /* Make new static route structure. */
2736 si
= XCALLOC (MTYPE_STATIC_IPV6
, sizeof (struct static_ipv6
));
2739 si
->distance
= distance
;
2744 case STATIC_IPV6_GATEWAY
:
2747 case STATIC_IPV6_IFNAME
:
2748 si
->ifname
= XSTRDUP (0, ifname
);
2750 case STATIC_IPV6_GATEWAY_IFNAME
:
2752 si
->ifname
= XSTRDUP (0, ifname
);
2756 /* Add new static route information to the tree with sort by
2757 distance value and gateway address. */
2758 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2760 if (si
->distance
< cp
->distance
)
2762 if (si
->distance
> cp
->distance
)
2766 /* Make linked list. */
2776 /* Install into rib. */
2777 static_install_ipv6 (p
, si
);
2782 /* Delete static route from static route configuration. */
2784 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2785 const char *ifname
, u_char distance
, u_int32_t vrf_id
)
2787 struct route_node
*rn
;
2788 struct static_ipv6
*si
;
2789 struct route_table
*stable
;
2792 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2796 /* Lookup static route prefix. */
2797 rn
= route_node_lookup (stable
, p
);
2801 /* Find same static route is the tree */
2802 for (si
= rn
->info
; si
; si
= si
->next
)
2803 if (distance
== si
->distance
2805 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2806 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2809 /* Can't find static route. */
2812 route_unlock_node (rn
);
2816 /* Install into rib. */
2817 static_uninstall_ipv6 (p
, si
);
2819 /* Unlink static route from linked list. */
2821 si
->prev
->next
= si
->next
;
2823 rn
->info
= si
->next
;
2825 si
->next
->prev
= si
->prev
;
2827 /* Free static route configuration. */
2829 XFREE (0, si
->ifname
);
2830 XFREE (MTYPE_STATIC_IPV6
, si
);
2834 #endif /* HAVE_IPV6 */
2836 /* RIB update function. */
2840 struct route_node
*rn
;
2841 struct route_table
*table
;
2843 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2845 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2847 rib_queue_add (&zebrad
, rn
);
2849 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2851 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2853 rib_queue_add (&zebrad
, rn
);
2857 /* Remove all routes which comes from non main table. */
2859 rib_weed_table (struct route_table
*table
)
2861 struct route_node
*rn
;
2866 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2867 for (rib
= rn
->info
; rib
; rib
= next
)
2871 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2874 if (rib
->table
!= zebrad
.rtm_table_default
&&
2875 rib
->table
!= RT_TABLE_MAIN
)
2876 rib_delnode (rn
, rib
);
2880 /* Delete all routes from non main table. */
2882 rib_weed_tables (void)
2884 rib_weed_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2885 rib_weed_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2888 /* Delete self installed routes after zebra is relaunched. */
2890 rib_sweep_table (struct route_table
*table
)
2892 struct route_node
*rn
;
2898 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2899 for (rib
= rn
->info
; rib
; rib
= next
)
2903 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2906 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
2907 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
2909 ret
= rib_uninstall_kernel (rn
, rib
);
2911 rib_delnode (rn
, rib
);
2916 /* Sweep all RIB tables. */
2918 rib_sweep_route (void)
2920 rib_sweep_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2921 rib_sweep_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2924 /* Delete routes learned from a given client. */
2925 /* TODO(wsun) May need to split the sweep process into multiple batches,
2926 * so that the process won't take too long if the table is large. */
2928 rib_sweep_client_table (struct route_table
*table
, int rib_type
)
2930 struct route_node
*rn
;
2936 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2937 for (rib
= rn
->info
; rib
; rib
= next
)
2941 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2944 if (rib
->type
== rib_type
)
2945 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2947 /* TODO(wsun) Is this mandatory? What about graceful restart/
2948 * non-stop forwarding */
2949 ret
= rib_uninstall_kernel (rn
, rib
);
2951 rib_delnode (rn
, rib
);
2953 zlog_err ("%s: could not delete routes from kernel!",
2958 /* Always delete the node. */
2959 rib_delnode (rn
, rib
);
2964 /* Sweep all routes learned from a given client from RIB tables. */
2966 rib_sweep_client_route (struct zserv
*client
)
2969 int route_type
= client
->route_type
;
2970 if (route_type
!= ZEBRA_ROUTE_MAX
)
2972 zlog_debug ("%s: Removing existing routes from client type %d",
2973 __func__
, route_type
);
2974 rib_sweep_client_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0), route_type
);
2975 rib_sweep_client_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0), route_type
);
2980 /* Remove specific by protocol routes from 'table'. */
2981 static unsigned long
2982 rib_score_proto_table (u_char proto
, struct route_table
*table
)
2984 struct route_node
*rn
;
2987 unsigned long n
= 0;
2990 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2991 for (rib
= rn
->info
; rib
; rib
= next
)
2994 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2996 if (rib
->type
== proto
)
2998 rib_delnode (rn
, rib
);
3006 /* Remove specific by protocol routes. */
3008 rib_score_proto (u_char proto
)
3010 return rib_score_proto_table (proto
, vrf_table (AFI_IP
, SAFI_UNICAST
, 0))
3011 +rib_score_proto_table (proto
, vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3014 /* Close RIB and clean up kernel routes. */
3016 rib_close_table (struct route_table
*table
)
3018 struct route_node
*rn
;
3022 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
3023 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
3025 if (! RIB_SYSTEM_ROUTE (rib
)
3026 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
3027 rib_uninstall_kernel (rn
, rib
);
3031 /* Close all RIB tables. */
3035 rib_close_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
3036 rib_close_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
3039 /* Routing information base initialize. */
3043 rib_queue_init (&zebrad
);
3044 /* VRF initialization. */