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. */
73 static vector vrf_vector
;
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
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
213 nexthop
->type
= NEXTHOP_TYPE_IFINDEX
;
214 nexthop
->ifindex
= ifindex
;
216 nexthop_add (rib
, nexthop
);
222 nexthop_ifname_add (struct rib
*rib
, char *ifname
)
224 struct nexthop
*nexthop
;
226 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
227 nexthop
->type
= NEXTHOP_TYPE_IFNAME
;
228 nexthop
->ifname
= XSTRDUP (0, ifname
);
230 nexthop_add (rib
, nexthop
);
236 nexthop_ipv4_add (struct rib
*rib
, struct in_addr
*ipv4
, struct in_addr
*src
)
238 struct nexthop
*nexthop
;
240 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
241 nexthop
->type
= NEXTHOP_TYPE_IPV4
;
242 nexthop
->gate
.ipv4
= *ipv4
;
244 nexthop
->src
.ipv4
= *src
;
246 nexthop_add (rib
, nexthop
);
251 static struct nexthop
*
252 nexthop_ipv4_ifindex_add (struct rib
*rib
, struct in_addr
*ipv4
,
253 struct in_addr
*src
, unsigned int ifindex
)
255 struct nexthop
*nexthop
;
257 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
258 nexthop
->type
= NEXTHOP_TYPE_IPV4_IFINDEX
;
259 nexthop
->gate
.ipv4
= *ipv4
;
261 nexthop
->src
.ipv4
= *src
;
262 nexthop
->ifindex
= ifindex
;
264 nexthop_add (rib
, nexthop
);
271 nexthop_ipv6_add (struct rib
*rib
, struct in6_addr
*ipv6
)
273 struct nexthop
*nexthop
;
275 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
276 nexthop
->type
= NEXTHOP_TYPE_IPV6
;
277 nexthop
->gate
.ipv6
= *ipv6
;
279 nexthop_add (rib
, nexthop
);
284 static struct nexthop
*
285 nexthop_ipv6_ifname_add (struct rib
*rib
, struct in6_addr
*ipv6
,
288 struct nexthop
*nexthop
;
290 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
291 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFNAME
;
292 nexthop
->gate
.ipv6
= *ipv6
;
293 nexthop
->ifname
= XSTRDUP (0, ifname
);
295 nexthop_add (rib
, nexthop
);
300 static struct nexthop
*
301 nexthop_ipv6_ifindex_add (struct rib
*rib
, struct in6_addr
*ipv6
,
302 unsigned int ifindex
)
304 struct nexthop
*nexthop
;
306 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
307 nexthop
->type
= NEXTHOP_TYPE_IPV6_IFINDEX
;
308 nexthop
->gate
.ipv6
= *ipv6
;
309 nexthop
->ifindex
= ifindex
;
311 nexthop_add (rib
, nexthop
);
315 #endif /* HAVE_IPV6 */
318 nexthop_blackhole_add (struct rib
*rib
)
320 struct nexthop
*nexthop
;
322 nexthop
= XCALLOC (MTYPE_NEXTHOP
, sizeof (struct nexthop
));
323 nexthop
->type
= NEXTHOP_TYPE_BLACKHOLE
;
324 SET_FLAG (rib
->flags
, ZEBRA_FLAG_BLACKHOLE
);
326 nexthop_add (rib
, nexthop
);
331 /* If force flag is not set, do not modify falgs at all for uninstall
332 the route from FIB. */
334 nexthop_active_ipv4 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
335 struct route_node
*top
)
337 struct prefix_ipv4 p
;
338 struct route_table
*table
;
339 struct route_node
*rn
;
341 struct nexthop
*newhop
;
343 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
)
344 nexthop
->ifindex
= 0;
347 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
349 /* Make lookup prefix. */
350 memset (&p
, 0, sizeof (struct prefix_ipv4
));
352 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
353 p
.prefix
= nexthop
->gate
.ipv4
;
356 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
360 rn
= route_node_match (table
, (struct prefix
*) &p
);
363 route_unlock_node (rn
);
365 /* If lookup self prefix return immidiately. */
369 /* Pick up selected route. */
370 for (match
= rn
->info
; match
; match
= match
->next
)
372 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
374 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
378 /* If there is no selected route or matched route is EGP, go up
381 || match
->type
== ZEBRA_ROUTE_BGP
)
385 } while (rn
&& rn
->info
== NULL
);
387 route_lock_node (rn
);
391 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
393 /* Directly point connected route. */
394 newhop
= match
->nexthop
;
395 if (newhop
&& nexthop
->type
== NEXTHOP_TYPE_IPV4
)
396 nexthop
->ifindex
= newhop
->ifindex
;
400 else if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_INTERNAL
))
402 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
403 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
)
404 && ! CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_RECURSIVE
))
408 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
409 nexthop
->rtype
= newhop
->type
;
410 if (newhop
->type
== NEXTHOP_TYPE_IPV4
||
411 newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
412 nexthop
->rgate
.ipv4
= newhop
->gate
.ipv4
;
413 if (newhop
->type
== NEXTHOP_TYPE_IFINDEX
414 || newhop
->type
== NEXTHOP_TYPE_IFNAME
415 || newhop
->type
== NEXTHOP_TYPE_IPV4_IFINDEX
)
416 nexthop
->rifindex
= newhop
->ifindex
;
432 /* If force flag is not set, do not modify falgs at all for uninstall
433 the route from FIB. */
435 nexthop_active_ipv6 (struct rib
*rib
, struct nexthop
*nexthop
, int set
,
436 struct route_node
*top
)
438 struct prefix_ipv6 p
;
439 struct route_table
*table
;
440 struct route_node
*rn
;
442 struct nexthop
*newhop
;
444 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
)
445 nexthop
->ifindex
= 0;
448 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
);
450 /* Make lookup prefix. */
451 memset (&p
, 0, sizeof (struct prefix_ipv6
));
453 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
454 p
.prefix
= nexthop
->gate
.ipv6
;
457 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
461 rn
= route_node_match (table
, (struct prefix
*) &p
);
464 route_unlock_node (rn
);
466 /* If lookup self prefix return immidiately. */
470 /* Pick up selected route. */
471 for (match
= rn
->info
; match
; match
= match
->next
)
473 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
475 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
)
564 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
566 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
570 /* If there is no selected route or matched route is EGP, go up
573 || match
->type
== ZEBRA_ROUTE_BGP
)
577 } while (rn
&& rn
->info
== NULL
);
579 route_lock_node (rn
);
583 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
584 /* Directly point connected route. */
588 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
589 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
599 rib_lookup_ipv4 (struct prefix_ipv4
*p
)
601 struct route_table
*table
;
602 struct route_node
*rn
;
604 struct nexthop
*nexthop
;
607 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
611 rn
= route_node_lookup (table
, (struct prefix
*) p
);
613 /* No route for this prefix. */
618 route_unlock_node (rn
);
620 for (match
= rn
->info
; match
; match
= match
->next
)
622 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
624 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
628 if (! match
|| match
->type
== ZEBRA_ROUTE_BGP
)
631 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
634 for (nexthop
= match
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
635 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
642 * This clone function, unlike its original rib_lookup_ipv4(), checks
643 * if specified IPv4 route record (prefix/mask -> gate) exists in
644 * the whole RIB and has ZEBRA_FLAG_SELECTED set.
648 * 0: exact match found
649 * 1: a match was found with a different gate
650 * 2: connected route found
651 * 3: no matches found
654 rib_lookup_ipv4_route (struct prefix_ipv4
*p
, union sockunion
* qgate
)
656 struct route_table
*table
;
657 struct route_node
*rn
;
659 struct nexthop
*nexthop
;
662 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
664 return ZEBRA_RIB_LOOKUP_ERROR
;
666 /* Scan the RIB table for exactly matching RIB entry. */
667 rn
= route_node_lookup (table
, (struct prefix
*) p
);
669 /* No route for this prefix. */
671 return ZEBRA_RIB_NOTFOUND
;
674 route_unlock_node (rn
);
676 /* Find out if a "selected" RR for the discovered RIB entry exists ever. */
677 for (match
= rn
->info
; match
; match
= match
->next
)
679 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
681 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
685 /* None such found :( */
687 return ZEBRA_RIB_NOTFOUND
;
689 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
690 return ZEBRA_RIB_FOUND_CONNECTED
;
692 /* Ok, we have a cood candidate, let's check it's nexthop list... */
693 for (nexthop
= match
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
694 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
696 /* We are happy with either direct or recursive hexthop */
697 if (nexthop
->gate
.ipv4
.s_addr
== qgate
->sin
.sin_addr
.s_addr
||
698 nexthop
->rgate
.ipv4
.s_addr
== qgate
->sin
.sin_addr
.s_addr
)
699 return ZEBRA_RIB_FOUND_EXACT
;
702 if (IS_ZEBRA_DEBUG_RIB
)
704 char gate_buf
[INET_ADDRSTRLEN
], rgate_buf
[INET_ADDRSTRLEN
], qgate_buf
[INET_ADDRSTRLEN
];
705 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, gate_buf
, INET_ADDRSTRLEN
);
706 inet_ntop (AF_INET
, &nexthop
->rgate
.ipv4
.s_addr
, rgate_buf
, INET_ADDRSTRLEN
);
707 inet_ntop (AF_INET
, &qgate
->sin
.sin_addr
.s_addr
, qgate_buf
, INET_ADDRSTRLEN
);
708 zlog_debug ("%s: qgate == %s, gate == %s, rgate == %s", __func__
, qgate_buf
, gate_buf
, rgate_buf
);
710 return ZEBRA_RIB_FOUND_NOGATE
;
714 return ZEBRA_RIB_NOTFOUND
;
719 rib_match_ipv6 (struct in6_addr
*addr
)
721 struct prefix_ipv6 p
;
722 struct route_table
*table
;
723 struct route_node
*rn
;
725 struct nexthop
*newhop
;
728 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
732 memset (&p
, 0, sizeof (struct prefix_ipv6
));
734 p
.prefixlen
= IPV6_MAX_PREFIXLEN
;
735 IPV6_ADDR_COPY (&p
.prefix
, addr
);
737 rn
= route_node_match (table
, (struct prefix
*) &p
);
741 route_unlock_node (rn
);
743 /* Pick up selected route. */
744 for (match
= rn
->info
; match
; match
= match
->next
)
746 if (CHECK_FLAG (match
->status
, RIB_ENTRY_REMOVED
))
748 if (CHECK_FLAG (match
->flags
, ZEBRA_FLAG_SELECTED
))
752 /* If there is no selected route or matched route is EGP, go up
755 || match
->type
== ZEBRA_ROUTE_BGP
)
759 } while (rn
&& rn
->info
== NULL
);
761 route_lock_node (rn
);
765 if (match
->type
== ZEBRA_ROUTE_CONNECT
)
766 /* Directly point connected route. */
770 for (newhop
= match
->nexthop
; newhop
; newhop
= newhop
->next
)
771 if (CHECK_FLAG (newhop
->flags
, NEXTHOP_FLAG_FIB
))
779 #endif /* HAVE_IPV6 */
781 #define RIB_SYSTEM_ROUTE(R) \
782 ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT)
784 /* This function verifies reachability of one given nexthop, which can be
785 * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored
786 * in nexthop->flags field. If the 4th parameter, 'set', is non-zero,
787 * nexthop->ifindex will be updated appropriately as well.
788 * An existing route map can turn (otherwise active) nexthop into inactive, but
791 * The return value is the final value of 'ACTIVE' flag.
795 nexthop_active_check (struct route_node
*rn
, struct rib
*rib
,
796 struct nexthop
*nexthop
, int set
)
798 struct interface
*ifp
;
799 route_map_result_t ret
= RMAP_MATCH
;
800 extern char *proto_rm
[AFI_MAX
][ZEBRA_ROUTE_MAX
+1];
801 struct route_map
*rmap
;
805 switch (nexthop
->type
)
807 case NEXTHOP_TYPE_IFINDEX
:
808 ifp
= if_lookup_by_index (nexthop
->ifindex
);
809 if (ifp
&& if_is_operative(ifp
))
810 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
812 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
814 case NEXTHOP_TYPE_IPV6_IFNAME
:
816 case NEXTHOP_TYPE_IFNAME
:
817 ifp
= if_lookup_by_name (nexthop
->ifname
);
818 if (ifp
&& if_is_operative(ifp
))
821 nexthop
->ifindex
= ifp
->ifindex
;
822 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
827 nexthop
->ifindex
= 0;
828 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
831 case NEXTHOP_TYPE_IPV4
:
832 case NEXTHOP_TYPE_IPV4_IFINDEX
:
834 if (nexthop_active_ipv4 (rib
, nexthop
, set
, rn
))
835 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
837 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
840 case NEXTHOP_TYPE_IPV6
:
842 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
843 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
845 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
847 case NEXTHOP_TYPE_IPV6_IFINDEX
:
849 if (IN6_IS_ADDR_LINKLOCAL (&nexthop
->gate
.ipv6
))
851 ifp
= if_lookup_by_index (nexthop
->ifindex
);
852 if (ifp
&& if_is_operative(ifp
))
853 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
855 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
859 if (nexthop_active_ipv6 (rib
, nexthop
, set
, rn
))
860 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
862 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
865 #endif /* HAVE_IPV6 */
866 case NEXTHOP_TYPE_BLACKHOLE
:
867 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
872 if (! CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
))
875 if (RIB_SYSTEM_ROUTE(rib
) ||
876 (family
== AFI_IP
&& rn
->p
.family
!= AF_INET
) ||
877 (family
== AFI_IP6
&& rn
->p
.family
!= AF_INET6
))
878 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
881 if (rib
->type
>= 0 && rib
->type
< ZEBRA_ROUTE_MAX
&&
882 proto_rm
[family
][rib
->type
])
883 rmap
= route_map_lookup_by_name (proto_rm
[family
][rib
->type
]);
884 if (!rmap
&& proto_rm
[family
][ZEBRA_ROUTE_MAX
])
885 rmap
= route_map_lookup_by_name (proto_rm
[family
][ZEBRA_ROUTE_MAX
]);
887 ret
= route_map_apply(rmap
, &rn
->p
, RMAP_ZEBRA
, nexthop
);
890 if (ret
== RMAP_DENYMATCH
)
891 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
892 return CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
895 /* Iterate over all nexthops of the given RIB entry and refresh their
896 * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any
897 * nexthop is found to toggle the ACTIVE flag, the whole rib structure
898 * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is
899 * transparently passed to nexthop_active_check().
901 * Return value is the new number of active nexthops.
905 nexthop_active_update (struct route_node
*rn
, struct rib
*rib
, int set
)
907 struct nexthop
*nexthop
;
908 int prev_active
, prev_index
, new_active
;
910 rib
->nexthop_active_num
= 0;
911 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
913 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
915 prev_active
= CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
);
916 prev_index
= nexthop
->ifindex
;
917 if ((new_active
= nexthop_active_check (rn
, rib
, nexthop
, set
)))
918 rib
->nexthop_active_num
++;
919 if (prev_active
!= new_active
||
920 prev_index
!= nexthop
->ifindex
)
921 SET_FLAG (rib
->flags
, ZEBRA_FLAG_CHANGED
);
923 return rib
->nexthop_active_num
;
929 rib_install_kernel (struct route_node
*rn
, struct rib
*rib
)
932 struct nexthop
*nexthop
;
934 switch (PREFIX_FAMILY (&rn
->p
))
937 ret
= kernel_add_ipv4 (&rn
->p
, rib
);
941 ret
= kernel_add_ipv6 (&rn
->p
, rib
);
943 #endif /* HAVE_IPV6 */
946 /* This condition is never met, if we are using rt_socket.c */
949 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
950 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
954 /* Uninstall the route from kernel. */
956 rib_uninstall_kernel (struct route_node
*rn
, struct rib
*rib
)
959 struct nexthop
*nexthop
;
961 switch (PREFIX_FAMILY (&rn
->p
))
964 ret
= kernel_delete_ipv4 (&rn
->p
, rib
);
968 ret
= kernel_delete_ipv6 (&rn
->p
, rib
);
970 #endif /* HAVE_IPV6 */
973 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
974 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
979 /* Uninstall the route from kernel. */
981 rib_uninstall (struct route_node
*rn
, struct rib
*rib
)
983 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
985 redistribute_delete (&rn
->p
, rib
);
986 if (! RIB_SYSTEM_ROUTE (rib
))
987 rib_uninstall_kernel (rn
, rib
);
988 UNSET_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
);
992 static void rib_unlink (struct route_node
*, struct rib
*);
994 /* Core function for processing routing information base. */
996 rib_process (struct route_node
*rn
)
1000 struct rib
*fib
= NULL
;
1001 struct rib
*select
= NULL
;
1002 struct rib
*del
= NULL
;
1004 struct nexthop
*nexthop
= NULL
;
1005 char buf
[INET6_ADDRSTRLEN
];
1009 if (IS_ZEBRA_DEBUG_RIB
|| IS_ZEBRA_DEBUG_RIB_Q
)
1010 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1012 for (rib
= rn
->info
; rib
; rib
= next
)
1014 /* The next pointer is saved, because current pointer
1015 * may be passed to rib_unlink() in the middle of iteration.
1019 /* Currently installed rib. */
1020 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1022 assert (fib
== NULL
);
1026 /* Unlock removed routes, so they'll be freed, bar the FIB entry,
1027 * which we need to do do further work with below.
1029 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1033 if (IS_ZEBRA_DEBUG_RIB
)
1034 zlog_debug ("%s: %s/%d: rn %p, removing rib %p", __func__
,
1035 buf
, rn
->p
.prefixlen
, rn
, rib
);
1036 rib_unlink (rn
, rib
);
1044 /* Skip unreachable nexthop. */
1045 if (! nexthop_active_update (rn
, rib
, 0))
1048 /* Infinit distance. */
1049 if (rib
->distance
== DISTANCE_INFINITY
)
1052 /* Newly selected rib, the common case. */
1059 /* filter route selection in following order:
1060 * - connected beats other types
1061 * - lower distance beats higher
1062 * - lower metric beats higher for equal distance
1063 * - last, hence oldest, route wins tie break.
1066 /* Connected routes. Pick the last connected
1067 * route of the set of lowest metric connected routes.
1069 if (rib
->type
== ZEBRA_ROUTE_CONNECT
)
1071 if (select
->type
!= ZEBRA_ROUTE_CONNECT
1072 || rib
->metric
<= select
->metric
)
1076 else if (select
->type
== ZEBRA_ROUTE_CONNECT
)
1079 /* higher distance loses */
1080 if (rib
->distance
> select
->distance
)
1084 if (rib
->distance
< select
->distance
)
1090 /* metric tie-breaks equal distance */
1091 if (rib
->metric
<= select
->metric
)
1093 } /* for (rib = rn->info; rib; rib = next) */
1095 /* After the cycle is finished, the following pointers will be set:
1096 * select --- the winner RIB entry, if any was found, otherwise NULL
1097 * fib --- the SELECTED RIB entry, if any, otherwise NULL
1098 * del --- equal to fib, if fib is queued for deletion, NULL otherwise
1102 /* Same RIB entry is selected. Update FIB and finish. */
1103 if (select
&& select
== fib
)
1105 if (IS_ZEBRA_DEBUG_RIB
)
1106 zlog_debug ("%s: %s/%d: Updating existing route, select %p, fib %p",
1107 __func__
, buf
, rn
->p
.prefixlen
, select
, fib
);
1108 if (CHECK_FLAG (select
->flags
, ZEBRA_FLAG_CHANGED
))
1110 redistribute_delete (&rn
->p
, select
);
1111 if (! RIB_SYSTEM_ROUTE (select
))
1112 rib_uninstall_kernel (rn
, select
);
1114 /* Set real nexthop. */
1115 nexthop_active_update (rn
, select
, 1);
1117 if (! RIB_SYSTEM_ROUTE (select
))
1118 rib_install_kernel (rn
, select
);
1119 redistribute_add (&rn
->p
, select
);
1121 else if (! RIB_SYSTEM_ROUTE (select
))
1123 /* Housekeeping code to deal with
1124 race conditions in kernel with linux
1125 netlink reporting interface up before IPv4 or IPv6 protocol
1126 is ready to add routes.
1127 This makes sure the routes are IN the kernel.
1130 for (nexthop
= select
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1131 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
1137 rib_install_kernel (rn
, select
);
1142 /* At this point we either haven't found the best RIB entry or it is
1143 * different from what we currently intend to flag with SELECTED. In both
1144 * cases, if a RIB block is present in FIB, it should be withdrawn.
1148 if (IS_ZEBRA_DEBUG_RIB
)
1149 zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__
,
1150 buf
, rn
->p
.prefixlen
, fib
);
1151 redistribute_delete (&rn
->p
, fib
);
1152 if (! RIB_SYSTEM_ROUTE (fib
))
1153 rib_uninstall_kernel (rn
, fib
);
1154 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1156 /* Set real nexthop. */
1157 nexthop_active_update (rn
, fib
, 1);
1160 /* Regardless of some RIB entry being SELECTED or not before, now we can
1161 * tell, that if a new winner exists, FIB is still not updated with this
1162 * data, but ready to be.
1166 if (IS_ZEBRA_DEBUG_RIB
)
1167 zlog_debug ("%s: %s/%d: Adding route, select %p", __func__
, buf
,
1168 rn
->p
.prefixlen
, select
);
1169 /* Set real nexthop. */
1170 nexthop_active_update (rn
, select
, 1);
1172 if (! RIB_SYSTEM_ROUTE (select
))
1173 rib_install_kernel (rn
, select
);
1174 SET_FLAG (select
->flags
, ZEBRA_FLAG_SELECTED
);
1175 redistribute_add (&rn
->p
, select
);
1178 /* FIB route was removed, should be deleted */
1181 if (IS_ZEBRA_DEBUG_RIB
)
1182 zlog_debug ("%s: %s/%d: Deleting fib %p, rn %p", __func__
, buf
,
1183 rn
->p
.prefixlen
, del
, rn
);
1184 rib_unlink (rn
, del
);
1188 if (IS_ZEBRA_DEBUG_RIB_Q
)
1189 zlog_debug ("%s: %s/%d: rn %p dequeued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1192 /* Take a list of route_node structs and return 1, if there was a record
1193 * picked from it and processed by rib_process(). Don't process more,
1194 * than one RN record; operate only in the specified sub-queue.
1197 process_subq (struct list
* subq
, u_char qindex
)
1199 struct listnode
*lnode
= listhead (subq
);
1200 struct route_node
*rnode
;
1205 rnode
= listgetdata (lnode
);
1206 rib_process (rnode
);
1208 if (rnode
->info
) /* The first RIB record is holding the flags bitmask. */
1209 UNSET_FLAG (((struct rib
*)rnode
->info
)->rn_status
, RIB_ROUTE_QUEUED(qindex
));
1213 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1214 __func__
, rnode
, rnode
->lock
);
1215 zlog_backtrace(LOG_DEBUG
);
1218 route_unlock_node (rnode
);
1219 list_delete_node (subq
, lnode
);
1223 /* Dispatch the meta queue by picking, processing and unlocking the next RN from
1224 * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data
1225 * is pointed to the meta queue structure.
1227 static wq_item_status
1228 meta_queue_process (struct work_queue
*dummy
, void *data
)
1230 struct meta_queue
* mq
= data
;
1233 for (i
= 0; i
< MQ_SIZE
; i
++)
1234 if (process_subq (mq
->subq
[i
], i
))
1239 return mq
->size
? WQ_REQUEUE
: WQ_SUCCESS
;
1242 /* Map from rib types to queue type (priority) in meta queue */
1243 static const u_char meta_queue_map
[ZEBRA_ROUTE_MAX
] = {
1244 [ZEBRA_ROUTE_SYSTEM
] = 4,
1245 [ZEBRA_ROUTE_KERNEL
] = 0,
1246 [ZEBRA_ROUTE_CONNECT
] = 0,
1247 [ZEBRA_ROUTE_STATIC
] = 1,
1248 [ZEBRA_ROUTE_RIP
] = 2,
1249 [ZEBRA_ROUTE_RIPNG
] = 2,
1250 [ZEBRA_ROUTE_OSPF
] = 2,
1251 [ZEBRA_ROUTE_OSPF6
] = 2,
1252 [ZEBRA_ROUTE_ISIS
] = 2,
1253 [ZEBRA_ROUTE_BGP
] = 3,
1254 [ZEBRA_ROUTE_HSLS
] = 4,
1257 /* Look into the RN and queue it into one or more priority queues,
1258 * increasing the size for each data push done.
1261 rib_meta_queue_add (struct meta_queue
*mq
, struct route_node
*rn
)
1264 char buf
[INET6_ADDRSTRLEN
];
1266 if (IS_ZEBRA_DEBUG_RIB_Q
)
1267 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1269 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1271 u_char qindex
= meta_queue_map
[rib
->type
];
1273 /* Invariant: at this point we always have rn->info set. */
1274 if (CHECK_FLAG (((struct rib
*)rn
->info
)->rn_status
, RIB_ROUTE_QUEUED(qindex
)))
1276 if (IS_ZEBRA_DEBUG_RIB_Q
)
1277 zlog_debug ("%s: %s/%d: rn %p is already queued in sub-queue %u",
1278 __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1282 SET_FLAG (((struct rib
*)rn
->info
)->rn_status
, RIB_ROUTE_QUEUED(qindex
));
1283 listnode_add (mq
->subq
[qindex
], rn
);
1284 route_lock_node (rn
);
1287 if (IS_ZEBRA_DEBUG_RIB_Q
)
1288 zlog_debug ("%s: %s/%d: queued rn %p into sub-queue %u",
1289 __func__
, buf
, rn
->p
.prefixlen
, rn
, qindex
);
1293 /* Add route_node to work queue and schedule processing */
1295 rib_queue_add (struct zebra_t
*zebra
, struct route_node
*rn
)
1297 char buf
[INET_ADDRSTRLEN
];
1298 assert (zebra
&& rn
);
1300 if (IS_ZEBRA_DEBUG_RIB_Q
)
1301 inet_ntop (AF_INET
, &rn
->p
.u
.prefix
, buf
, INET_ADDRSTRLEN
);
1303 /* Pointless to queue a route_node with no RIB entries to add or remove */
1306 zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
1307 __func__
, rn
, rn
->lock
);
1308 zlog_backtrace(LOG_DEBUG
);
1312 if (IS_ZEBRA_DEBUG_RIB_Q
)
1313 zlog_info ("%s: %s/%d: work queue added", __func__
, buf
, rn
->p
.prefixlen
);
1317 if (zebra
->ribq
== NULL
)
1319 zlog_err ("%s: work_queue does not exist!", __func__
);
1323 /* The RIB queue should normally be either empty or holding the only work_queue_item
1324 * element. In the latter case this element would hold a pointer to the meta queue
1325 * structure, which must be used to actually queue the route nodes to process. So
1326 * create the MQ holder, if necessary, then push the work into it in any case.
1327 * This semantics was introduced after 0.99.9 release.
1330 /* Should I invent work_queue_empty() and use it, or it's Ok to do as follows? */
1331 if (!zebra
->ribq
->items
->count
)
1332 work_queue_add (zebra
->ribq
, zebra
->mq
);
1334 rib_meta_queue_add (zebra
->mq
, rn
);
1336 if (IS_ZEBRA_DEBUG_RIB_Q
)
1337 zlog_debug ("%s: %s/%d: rn %p queued", __func__
, buf
, rn
->p
.prefixlen
, rn
);
1342 /* Create new meta queue.
1343 A destructor function doesn't seem to be necessary here.
1345 static struct meta_queue
*
1346 meta_queue_new (void)
1348 struct meta_queue
*new;
1351 new = XCALLOC (MTYPE_WORK_QUEUE
, sizeof (struct meta_queue
));
1354 for (i
= 0; i
< MQ_SIZE
; i
++)
1356 new->subq
[i
] = list_new ();
1357 assert(new->subq
[i
]);
1363 /* initialise zebra rib work queue */
1365 rib_queue_init (struct zebra_t
*zebra
)
1369 if (! (zebra
->ribq
= work_queue_new (zebra
->master
,
1370 "route_node processing")))
1372 zlog_err ("%s: could not initialise work queue!", __func__
);
1376 /* fill in the work queue spec */
1377 zebra
->ribq
->spec
.workfunc
= &meta_queue_process
;
1378 zebra
->ribq
->spec
.errorfunc
= NULL
;
1379 /* XXX: TODO: These should be runtime configurable via vty */
1380 zebra
->ribq
->spec
.max_retries
= 3;
1381 zebra
->ribq
->spec
.hold
= rib_process_hold_time
;
1383 if (!(zebra
->mq
= meta_queue_new ()))
1385 zlog_err ("%s: could not initialise meta queue!", __func__
);
1391 /* RIB updates are processed via a queue of pointers to route_nodes.
1393 * The queue length is bounded by the maximal size of the routing table,
1394 * as a route_node will not be requeued, if already queued.
1396 * RIBs are submitted via rib_addnode or rib_delnode which set minimal
1397 * state, or static_install_ipv{4,6} (when an existing RIB is updated)
1398 * and then submit route_node to queue for best-path selection later.
1399 * Order of add/delete state changes are preserved for any given RIB.
1401 * Deleted RIBs are reaped during best-path selection.
1404 * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with
1405 * |-------->| | best RIB, if required
1407 * static_install->|->rib_addqueue...... -> rib_process
1409 * |-------->| |-> rib_unlink
1410 * |-> set RIB_ENTRY_REMOVE |
1411 * rib_delnode (RIB freed)
1414 * Queueing state for a route_node is kept in the head RIB entry, this
1415 * state must be preserved as and when the head RIB entry of a
1416 * route_node is changed by rib_unlink / rib_link. A small complication,
1417 * but saves having to allocate a dedicated object for this.
1419 * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code):
1421 * - route_nodes: refcounted by:
1422 * - RIBs attached to route_node:
1423 * - managed by: rib_link/unlink
1424 * - route_node processing queue
1425 * - managed by: rib_addqueue, rib_process.
1429 /* Add RIB to head of the route node. */
1431 rib_link (struct route_node
*rn
, struct rib
*rib
)
1434 char buf
[INET6_ADDRSTRLEN
];
1438 route_lock_node (rn
); /* rn route table reference */
1440 if (IS_ZEBRA_DEBUG_RIB
)
1442 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1443 zlog_debug ("%s: %s/%d: rn %p, rib %p", __func__
,
1444 buf
, rn
->p
.prefixlen
, rn
, rib
);
1450 if (IS_ZEBRA_DEBUG_RIB
)
1451 zlog_debug ("%s: %s/%d: new head, rn_status copied over", __func__
,
1452 buf
, rn
->p
.prefixlen
);
1454 /* Transfer the rn status flags to the new head RIB */
1455 rib
->rn_status
= head
->rn_status
;
1459 rib_queue_add (&zebrad
, rn
);
1463 rib_addnode (struct route_node
*rn
, struct rib
*rib
)
1465 /* RIB node has been un-removed before route-node is processed.
1466 * route_node must hence already be on the queue for processing..
1468 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1470 if (IS_ZEBRA_DEBUG_RIB
)
1472 char buf
[INET6_ADDRSTRLEN
];
1473 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1474 zlog_debug ("%s: %s/%d: rn %p, un-removed rib %p",
1475 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1477 UNSET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1484 rib_unlink (struct route_node
*rn
, struct rib
*rib
)
1486 struct nexthop
*nexthop
, *next
;
1487 char buf
[INET6_ADDRSTRLEN
];
1491 if (IS_ZEBRA_DEBUG_RIB
)
1493 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1494 zlog_debug ("%s: %s/%d: rn %p, rib %p",
1495 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1499 rib
->next
->prev
= rib
->prev
;
1502 rib
->prev
->next
= rib
->next
;
1505 rn
->info
= rib
->next
;
1509 if (IS_ZEBRA_DEBUG_RIB
)
1510 zlog_debug ("%s: %s/%d: rn %p, rib %p, new head copy",
1511 __func__
, buf
, rn
->p
.prefixlen
, rn
, rib
);
1512 rib
->next
->rn_status
= rib
->rn_status
;
1516 /* free RIB and nexthops */
1517 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= next
)
1519 next
= nexthop
->next
;
1520 nexthop_free (nexthop
);
1522 XFREE (MTYPE_RIB
, rib
);
1524 route_unlock_node (rn
); /* rn route table reference */
1528 rib_delnode (struct route_node
*rn
, struct rib
*rib
)
1530 if (IS_ZEBRA_DEBUG_RIB
)
1532 char buf
[INET6_ADDRSTRLEN
];
1533 inet_ntop (rn
->p
.family
, &rn
->p
.u
.prefix
, buf
, INET6_ADDRSTRLEN
);
1534 zlog_debug ("%s: %s/%d: rn %p, rib %p, removing", __func__
,
1535 buf
, rn
->p
.prefixlen
, rn
, rib
);
1537 SET_FLAG (rib
->status
, RIB_ENTRY_REMOVED
);
1538 rib_queue_add (&zebrad
, rn
);
1542 rib_add_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1543 struct in_addr
*gate
, struct in_addr
*src
,
1544 unsigned int ifindex
, u_int32_t vrf_id
,
1545 u_int32_t metric
, u_char distance
)
1548 struct rib
*same
= NULL
;
1549 struct route_table
*table
;
1550 struct route_node
*rn
;
1551 struct nexthop
*nexthop
;
1554 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1558 /* Make it sure prefixlen is applied to the prefix. */
1559 apply_mask_ipv4 (p
);
1561 /* Set default distance by route type. */
1564 distance
= route_info
[type
].distance
;
1566 /* iBGP distance is 200. */
1567 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
1571 /* Lookup route node.*/
1572 rn
= route_node_get (table
, (struct prefix
*) p
);
1574 /* If same type of route are installed, treat it as a implicit
1576 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1578 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1581 if (rib
->type
!= type
)
1583 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
1588 /* Duplicate connected route comes in. */
1589 else if ((nexthop
= rib
->nexthop
) &&
1590 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
1591 nexthop
->ifindex
== ifindex
&&
1592 !CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1599 /* Allocate new rib structure. */
1600 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
1602 rib
->distance
= distance
;
1604 rib
->metric
= metric
;
1605 rib
->table
= vrf_id
;
1606 rib
->nexthop_num
= 0;
1607 rib
->uptime
= time (NULL
);
1609 /* Nexthop settings. */
1613 nexthop_ipv4_ifindex_add (rib
, gate
, src
, ifindex
);
1615 nexthop_ipv4_add (rib
, gate
, src
);
1618 nexthop_ifindex_add (rib
, ifindex
);
1620 /* If this route is kernel route, set FIB flag to the route. */
1621 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
1622 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1623 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1625 /* Link new rib to node.*/
1626 if (IS_ZEBRA_DEBUG_RIB
)
1627 zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__
, rn
, rib
);
1628 rib_addnode (rn
, rib
);
1630 /* Free implicit route.*/
1633 if (IS_ZEBRA_DEBUG_RIB
)
1634 zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__
, rn
, rib
);
1635 rib_delnode (rn
, same
);
1638 route_unlock_node (rn
);
1642 /* This function dumps the contents of a given RIB entry into
1643 * standard debug log. Calling function name and IP prefix in
1644 * question are passed as 1st and 2nd arguments.
1647 void rib_dump (const char * func
, const struct prefix_ipv4
* p
, const struct rib
* rib
)
1649 char straddr1
[INET_ADDRSTRLEN
], straddr2
[INET_ADDRSTRLEN
];
1650 struct nexthop
*nexthop
;
1652 inet_ntop (AF_INET
, &p
->prefix
, straddr1
, INET_ADDRSTRLEN
);
1653 zlog_debug ("%s: dumping RIB entry %p for %s/%d", func
, rib
, straddr1
, p
->prefixlen
);
1656 "%s: refcnt == %lu, uptime == %u, type == %u, table == %d",
1665 "%s: metric == %u, distance == %u, flags == %u, status == %u",
1674 "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u",
1677 rib
->nexthop_active_num
,
1678 rib
->nexthop_fib_num
1680 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1682 inet_ntop (AF_INET
, &nexthop
->gate
.ipv4
.s_addr
, straddr1
, INET_ADDRSTRLEN
);
1683 inet_ntop (AF_INET
, &nexthop
->rgate
.ipv4
.s_addr
, straddr2
, INET_ADDRSTRLEN
);
1686 "%s: NH %s (%s) with flags %s%s%s",
1690 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_ACTIVE
) ? "ACTIVE " : ""),
1691 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
) ? "FIB " : ""),
1692 (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_RECURSIVE
) ? "RECURSIVE" : "")
1695 zlog_debug ("%s: dump complete", func
);
1698 /* This is an exported helper to rtm_read() to dump the strange
1699 * RIB entry found by rib_lookup_ipv4_route()
1702 void rib_lookup_and_dump (struct prefix_ipv4
* p
)
1704 struct route_table
*table
;
1705 struct route_node
*rn
;
1707 char prefix_buf
[INET_ADDRSTRLEN
];
1710 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1713 zlog_err ("%s: vrf_table() returned NULL", __func__
);
1717 inet_ntop (AF_INET
, &p
->prefix
.s_addr
, prefix_buf
, INET_ADDRSTRLEN
);
1718 /* Scan the RIB table for exactly matching RIB entry. */
1719 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1721 /* No route for this prefix. */
1724 zlog_debug ("%s: lookup failed for %s/%d", __func__
, prefix_buf
, p
->prefixlen
);
1729 route_unlock_node (rn
);
1732 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1736 "%s: rn %p, rib %p: %s, %s",
1740 (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
) ? "removed" : "NOT removed"),
1741 (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) ? "selected" : "NOT selected")
1743 rib_dump (__func__
, p
, rib
);
1747 /* Check if requested address assignment will fail due to another
1748 * route being installed by zebra in FIB already. Take necessary
1749 * actions, if needed: remove such a route from FIB and deSELECT
1750 * corresponding RIB entry. Then put affected RN into RIBQ head.
1752 void rib_lookup_and_pushup (struct prefix_ipv4
* p
)
1754 struct route_table
*table
;
1755 struct route_node
*rn
;
1757 unsigned changed
= 0;
1759 if (NULL
== (table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0)))
1761 zlog_err ("%s: vrf_table() returned NULL", __func__
);
1765 /* No matches would be the simplest case. */
1766 if (NULL
== (rn
= route_node_lookup (table
, (struct prefix
*) p
)))
1770 route_unlock_node (rn
);
1772 /* Check all RIB entries. In case any changes have to be done, requeue
1773 * the RN into RIBQ head. If the routing message about the new connected
1774 * route (generated by the IP address we are going to assign very soon)
1775 * comes before the RIBQ is processed, the new RIB entry will join
1776 * RIBQ record already on head. This is necessary for proper revalidation
1777 * of the rest of the RIB.
1779 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1781 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
) &&
1782 ! RIB_SYSTEM_ROUTE (rib
))
1785 if (IS_ZEBRA_DEBUG_RIB
)
1787 char buf
[INET_ADDRSTRLEN
];
1788 inet_ntop (rn
->p
.family
, &p
->prefix
, buf
, INET_ADDRSTRLEN
);
1789 zlog_debug ("%s: freeing way for connected prefix %s/%d", __func__
, buf
, p
->prefixlen
);
1790 rib_dump (__func__
, (struct prefix_ipv4
*)&rn
->p
, rib
);
1792 rib_uninstall (rn
, rib
);
1796 rib_queue_add (&zebrad
, rn
);
1800 rib_add_ipv4_multipath (struct prefix_ipv4
*p
, struct rib
*rib
)
1802 struct route_table
*table
;
1803 struct route_node
*rn
;
1805 struct nexthop
*nexthop
;
1808 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1811 /* Make it sure prefixlen is applied to the prefix. */
1812 apply_mask_ipv4 (p
);
1814 /* Set default distance by route type. */
1815 if (rib
->distance
== 0)
1817 rib
->distance
= route_info
[rib
->type
].distance
;
1819 /* iBGP distance is 200. */
1820 if (rib
->type
== ZEBRA_ROUTE_BGP
1821 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_IBGP
))
1822 rib
->distance
= 200;
1825 /* Lookup route node.*/
1826 rn
= route_node_get (table
, (struct prefix
*) p
);
1828 /* If same type of route are installed, treat it as a implicit
1830 for (same
= rn
->info
; same
; same
= same
->next
)
1832 if (CHECK_FLAG (same
->status
, RIB_ENTRY_REMOVED
))
1835 if (same
->type
== rib
->type
&& same
->table
== rib
->table
1836 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
1840 /* If this route is kernel route, set FIB flag to the route. */
1841 if (rib
->type
== ZEBRA_ROUTE_KERNEL
|| rib
->type
== ZEBRA_ROUTE_CONNECT
)
1842 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1843 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1845 /* Link new rib to node.*/
1846 rib_addnode (rn
, rib
);
1847 if (IS_ZEBRA_DEBUG_RIB
)
1849 zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry",
1851 rib_dump (__func__
, p
, rib
);
1854 /* Free implicit route.*/
1857 if (IS_ZEBRA_DEBUG_RIB
)
1859 zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry",
1860 __func__
, rn
, same
);
1861 rib_dump (__func__
, p
, same
);
1863 rib_delnode (rn
, same
);
1866 route_unlock_node (rn
);
1870 /* XXX factor with rib_delete_ipv6 */
1872 rib_delete_ipv4 (int type
, int flags
, struct prefix_ipv4
*p
,
1873 struct in_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
1875 struct route_table
*table
;
1876 struct route_node
*rn
;
1878 struct rib
*fib
= NULL
;
1879 struct rib
*same
= NULL
;
1880 struct nexthop
*nexthop
;
1881 char buf1
[INET_ADDRSTRLEN
];
1882 char buf2
[INET_ADDRSTRLEN
];
1885 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
1890 apply_mask_ipv4 (p
);
1892 if (IS_ZEBRA_DEBUG_KERNEL
&& gate
)
1893 zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d",
1894 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1899 /* Lookup route node. */
1900 rn
= route_node_lookup (table
, (struct prefix
*) p
);
1903 if (IS_ZEBRA_DEBUG_KERNEL
)
1906 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
1907 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1909 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
1912 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
1913 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1917 return ZEBRA_ERR_RTNOEXIST
;
1920 /* Lookup same type route. */
1921 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
1923 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
1926 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
1929 if (rib
->type
!= type
)
1931 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
1932 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&& nexthop
->ifindex
== ifindex
)
1937 route_unlock_node (rn
);
1938 route_unlock_node (rn
);
1944 /* Make sure that the route found has the same gateway. */
1945 else if (gate
== NULL
||
1946 ((nexthop
= rib
->nexthop
) &&
1947 (IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, gate
) ||
1948 IPV4_ADDR_SAME (&nexthop
->rgate
.ipv4
, gate
))))
1955 /* If same type of route can't be found and this message is from
1959 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
1962 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
1963 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
1965 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
1969 if (IS_ZEBRA_DEBUG_KERNEL
)
1972 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
1973 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1975 inet_ntop (AF_INET
, gate
, buf2
, INET_ADDRSTRLEN
),
1979 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
1980 inet_ntop (AF_INET
, &p
->prefix
, buf1
, INET_ADDRSTRLEN
),
1985 route_unlock_node (rn
);
1986 return ZEBRA_ERR_RTNOEXIST
;
1991 rib_delnode (rn
, same
);
1993 route_unlock_node (rn
);
1997 /* Install static route into rib. */
1999 static_install_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
2002 struct route_node
*rn
;
2003 struct route_table
*table
;
2006 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2010 /* Lookup existing route */
2011 rn
= route_node_get (table
, p
);
2012 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2014 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2017 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2023 /* Same distance static route is there. Update it with new
2025 route_unlock_node (rn
);
2028 case STATIC_IPV4_GATEWAY
:
2029 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2031 case STATIC_IPV4_IFNAME
:
2032 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2034 case STATIC_IPV4_BLACKHOLE
:
2035 nexthop_blackhole_add (rib
);
2038 rib_queue_add (&zebrad
, rn
);
2042 /* This is new static route. */
2043 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2045 rib
->type
= ZEBRA_ROUTE_STATIC
;
2046 rib
->distance
= si
->distance
;
2048 rib
->nexthop_num
= 0;
2052 case STATIC_IPV4_GATEWAY
:
2053 nexthop_ipv4_add (rib
, &si
->gate
.ipv4
, NULL
);
2055 case STATIC_IPV4_IFNAME
:
2056 nexthop_ifname_add (rib
, si
->gate
.ifname
);
2058 case STATIC_IPV4_BLACKHOLE
:
2059 nexthop_blackhole_add (rib
);
2063 /* Save the flags of this static routes (reject, blackhole) */
2064 rib
->flags
= si
->flags
;
2066 /* Link this rib to the tree. */
2067 rib_addnode (rn
, rib
);
2072 static_ipv4_nexthop_same (struct nexthop
*nexthop
, struct static_ipv4
*si
)
2074 if (nexthop
->type
== NEXTHOP_TYPE_IPV4
2075 && si
->type
== STATIC_IPV4_GATEWAY
2076 && IPV4_ADDR_SAME (&nexthop
->gate
.ipv4
, &si
->gate
.ipv4
))
2078 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2079 && si
->type
== STATIC_IPV4_IFNAME
2080 && strcmp (nexthop
->ifname
, si
->gate
.ifname
) == 0)
2082 if (nexthop
->type
== NEXTHOP_TYPE_BLACKHOLE
2083 && si
->type
== STATIC_IPV4_BLACKHOLE
)
2088 /* Uninstall static route from RIB. */
2090 static_uninstall_ipv4 (struct prefix
*p
, struct static_ipv4
*si
)
2092 struct route_node
*rn
;
2094 struct nexthop
*nexthop
;
2095 struct route_table
*table
;
2098 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2102 /* Lookup existing route with type and distance. */
2103 rn
= route_node_lookup (table
, p
);
2107 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2109 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2112 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2118 route_unlock_node (rn
);
2122 /* Lookup nexthop. */
2123 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2124 if (static_ipv4_nexthop_same (nexthop
, si
))
2127 /* Can't find nexthop. */
2130 route_unlock_node (rn
);
2134 /* Check nexthop. */
2135 if (rib
->nexthop_num
== 1)
2136 rib_delnode (rn
, rib
);
2139 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2140 rib_uninstall (rn
, rib
);
2141 nexthop_delete (rib
, nexthop
);
2142 nexthop_free (nexthop
);
2143 rib_queue_add (&zebrad
, rn
);
2146 route_unlock_node (rn
);
2149 /* Add static route into static route configuration. */
2151 static_add_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2152 u_char flags
, u_char distance
, u_int32_t vrf_id
)
2155 struct route_node
*rn
;
2156 struct static_ipv4
*si
;
2157 struct static_ipv4
*pp
;
2158 struct static_ipv4
*cp
;
2159 struct static_ipv4
*update
= NULL
;
2160 struct route_table
*stable
;
2163 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2167 /* Lookup static route prefix. */
2168 rn
= route_node_get (stable
, p
);
2172 type
= STATIC_IPV4_GATEWAY
;
2174 type
= STATIC_IPV4_IFNAME
;
2176 type
= STATIC_IPV4_BLACKHOLE
;
2178 /* Do nothing if there is a same static route. */
2179 for (si
= rn
->info
; si
; si
= si
->next
)
2181 if (type
== si
->type
2182 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2183 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2185 if (distance
== si
->distance
)
2187 route_unlock_node (rn
);
2195 /* Distance changed. */
2197 static_delete_ipv4 (p
, gate
, ifname
, update
->distance
, vrf_id
);
2199 /* Make new static route structure. */
2200 si
= XCALLOC (MTYPE_STATIC_IPV4
, sizeof (struct static_ipv4
));
2203 si
->distance
= distance
;
2207 si
->gate
.ipv4
= *gate
;
2209 si
->gate
.ifname
= XSTRDUP (0, ifname
);
2211 /* Add new static route information to the tree with sort by
2212 distance value and gateway address. */
2213 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2215 if (si
->distance
< cp
->distance
)
2217 if (si
->distance
> cp
->distance
)
2219 if (si
->type
== STATIC_IPV4_GATEWAY
&& cp
->type
== STATIC_IPV4_GATEWAY
)
2221 if (ntohl (si
->gate
.ipv4
.s_addr
) < ntohl (cp
->gate
.ipv4
.s_addr
))
2223 if (ntohl (si
->gate
.ipv4
.s_addr
) > ntohl (cp
->gate
.ipv4
.s_addr
))
2228 /* Make linked list. */
2238 /* Install into rib. */
2239 static_install_ipv4 (p
, si
);
2244 /* Delete static route from static route configuration. */
2246 static_delete_ipv4 (struct prefix
*p
, struct in_addr
*gate
, const char *ifname
,
2247 u_char distance
, u_int32_t vrf_id
)
2250 struct route_node
*rn
;
2251 struct static_ipv4
*si
;
2252 struct route_table
*stable
;
2255 stable
= vrf_static_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
2259 /* Lookup static route prefix. */
2260 rn
= route_node_lookup (stable
, p
);
2266 type
= STATIC_IPV4_GATEWAY
;
2268 type
= STATIC_IPV4_IFNAME
;
2270 type
= STATIC_IPV4_BLACKHOLE
;
2272 /* Find same static route is the tree */
2273 for (si
= rn
->info
; si
; si
= si
->next
)
2274 if (type
== si
->type
2275 && (! gate
|| IPV4_ADDR_SAME (gate
, &si
->gate
.ipv4
))
2276 && (! ifname
|| strcmp (ifname
, si
->gate
.ifname
) == 0))
2279 /* Can't find static route. */
2282 route_unlock_node (rn
);
2286 /* Install into rib. */
2287 static_uninstall_ipv4 (p
, si
);
2289 /* Unlink static route from linked list. */
2291 si
->prev
->next
= si
->next
;
2293 rn
->info
= si
->next
;
2295 si
->next
->prev
= si
->prev
;
2296 route_unlock_node (rn
);
2298 /* Free static route configuration. */
2300 XFREE (0, si
->gate
.ifname
);
2301 XFREE (MTYPE_STATIC_IPV4
, si
);
2303 route_unlock_node (rn
);
2311 rib_bogus_ipv6 (int type
, struct prefix_ipv6
*p
,
2312 struct in6_addr
*gate
, unsigned int ifindex
, int table
)
2314 if (type
== ZEBRA_ROUTE_CONNECT
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)) {
2315 #if defined (MUSICA) || defined (LINUX)
2316 /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */
2317 if (p
->prefixlen
== 96)
2322 if (type
== ZEBRA_ROUTE_KERNEL
&& IN6_IS_ADDR_UNSPECIFIED (&p
->prefix
)
2323 && p
->prefixlen
== 96 && gate
&& IN6_IS_ADDR_UNSPECIFIED (gate
))
2325 kernel_delete_ipv6_old (p
, gate
, ifindex
, 0, table
);
2332 rib_add_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2333 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
,
2334 u_int32_t metric
, u_char distance
)
2337 struct rib
*same
= NULL
;
2338 struct route_table
*table
;
2339 struct route_node
*rn
;
2340 struct nexthop
*nexthop
;
2343 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2347 /* Make sure mask is applied. */
2348 apply_mask_ipv6 (p
);
2350 /* Set default distance by route type. */
2352 distance
= route_info
[type
].distance
;
2354 if (type
== ZEBRA_ROUTE_BGP
&& CHECK_FLAG (flags
, ZEBRA_FLAG_IBGP
))
2357 /* Filter bogus route. */
2358 if (rib_bogus_ipv6 (type
, p
, gate
, ifindex
, 0))
2361 /* Lookup route node.*/
2362 rn
= route_node_get (table
, (struct prefix
*) p
);
2364 /* If same type of route are installed, treat it as a implicit
2366 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2368 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2371 if (rib
->type
!= type
)
2373 if (rib
->type
!= ZEBRA_ROUTE_CONNECT
)
2378 else if ((nexthop
= rib
->nexthop
) &&
2379 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&&
2380 nexthop
->ifindex
== ifindex
)
2387 /* Allocate new rib structure. */
2388 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2391 rib
->distance
= distance
;
2393 rib
->metric
= metric
;
2394 rib
->table
= vrf_id
;
2395 rib
->nexthop_num
= 0;
2396 rib
->uptime
= time (NULL
);
2398 /* Nexthop settings. */
2402 nexthop_ipv6_ifindex_add (rib
, gate
, ifindex
);
2404 nexthop_ipv6_add (rib
, gate
);
2407 nexthop_ifindex_add (rib
, ifindex
);
2409 /* If this route is kernel route, set FIB flag to the route. */
2410 if (type
== ZEBRA_ROUTE_KERNEL
|| type
== ZEBRA_ROUTE_CONNECT
)
2411 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2412 SET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2414 /* Link new rib to node.*/
2415 rib_addnode (rn
, rib
);
2417 /* Free implicit route.*/
2419 rib_delnode (rn
, same
);
2421 route_unlock_node (rn
);
2425 /* XXX factor with rib_delete_ipv6 */
2427 rib_delete_ipv6 (int type
, int flags
, struct prefix_ipv6
*p
,
2428 struct in6_addr
*gate
, unsigned int ifindex
, u_int32_t vrf_id
)
2430 struct route_table
*table
;
2431 struct route_node
*rn
;
2433 struct rib
*fib
= NULL
;
2434 struct rib
*same
= NULL
;
2435 struct nexthop
*nexthop
;
2436 char buf1
[INET6_ADDRSTRLEN
];
2437 char buf2
[INET6_ADDRSTRLEN
];
2440 apply_mask_ipv6 (p
);
2443 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2447 /* Lookup route node. */
2448 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2451 if (IS_ZEBRA_DEBUG_KERNEL
)
2454 zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib",
2455 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2457 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
2460 zlog_debug ("route %s/%d ifindex %d doesn't exist in rib",
2461 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2465 return ZEBRA_ERR_RTNOEXIST
;
2468 /* Lookup same type route. */
2469 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2471 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2474 if (CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2477 if (rib
->type
!= type
)
2479 if (rib
->type
== ZEBRA_ROUTE_CONNECT
&& (nexthop
= rib
->nexthop
) &&
2480 nexthop
->type
== NEXTHOP_TYPE_IFINDEX
&& nexthop
->ifindex
== ifindex
)
2485 route_unlock_node (rn
);
2486 route_unlock_node (rn
);
2492 /* Make sure that the route found has the same gateway. */
2493 else if (gate
== NULL
||
2494 ((nexthop
= rib
->nexthop
) &&
2495 (IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, gate
) ||
2496 IPV6_ADDR_SAME (&nexthop
->rgate
.ipv6
, gate
))))
2503 /* If same type of route can't be found and this message is from
2507 if (fib
&& type
== ZEBRA_ROUTE_KERNEL
)
2510 for (nexthop
= fib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2511 UNSET_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
);
2513 UNSET_FLAG (fib
->flags
, ZEBRA_FLAG_SELECTED
);
2517 if (IS_ZEBRA_DEBUG_KERNEL
)
2520 zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib",
2521 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2523 inet_ntop (AF_INET6
, gate
, buf2
, INET6_ADDRSTRLEN
),
2527 zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib",
2528 inet_ntop (AF_INET6
, &p
->prefix
, buf1
, INET6_ADDRSTRLEN
),
2533 route_unlock_node (rn
);
2534 return ZEBRA_ERR_RTNOEXIST
;
2539 rib_delnode (rn
, same
);
2541 route_unlock_node (rn
);
2545 /* Install static route into rib. */
2547 static_install_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2550 struct route_table
*table
;
2551 struct route_node
*rn
;
2554 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2558 /* Lookup existing route */
2559 rn
= route_node_get (table
, p
);
2560 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2562 if (CHECK_FLAG(rib
->status
, RIB_ENTRY_REMOVED
))
2565 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2571 /* Same distance static route is there. Update it with new
2573 route_unlock_node (rn
);
2577 case STATIC_IPV6_GATEWAY
:
2578 nexthop_ipv6_add (rib
, &si
->ipv6
);
2580 case STATIC_IPV6_IFNAME
:
2581 nexthop_ifname_add (rib
, si
->ifname
);
2583 case STATIC_IPV6_GATEWAY_IFNAME
:
2584 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2587 rib_queue_add (&zebrad
, rn
);
2591 /* This is new static route. */
2592 rib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
2594 rib
->type
= ZEBRA_ROUTE_STATIC
;
2595 rib
->distance
= si
->distance
;
2597 rib
->nexthop_num
= 0;
2601 case STATIC_IPV6_GATEWAY
:
2602 nexthop_ipv6_add (rib
, &si
->ipv6
);
2604 case STATIC_IPV6_IFNAME
:
2605 nexthop_ifname_add (rib
, si
->ifname
);
2607 case STATIC_IPV6_GATEWAY_IFNAME
:
2608 nexthop_ipv6_ifname_add (rib
, &si
->ipv6
, si
->ifname
);
2612 /* Save the flags of this static routes (reject, blackhole) */
2613 rib
->flags
= si
->flags
;
2615 /* Link this rib to the tree. */
2616 rib_addnode (rn
, rib
);
2621 static_ipv6_nexthop_same (struct nexthop
*nexthop
, struct static_ipv6
*si
)
2623 if (nexthop
->type
== NEXTHOP_TYPE_IPV6
2624 && si
->type
== STATIC_IPV6_GATEWAY
2625 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
))
2627 if (nexthop
->type
== NEXTHOP_TYPE_IFNAME
2628 && si
->type
== STATIC_IPV6_IFNAME
2629 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2631 if (nexthop
->type
== NEXTHOP_TYPE_IPV6_IFNAME
2632 && si
->type
== STATIC_IPV6_GATEWAY_IFNAME
2633 && IPV6_ADDR_SAME (&nexthop
->gate
.ipv6
, &si
->ipv6
)
2634 && strcmp (nexthop
->ifname
, si
->ifname
) == 0)
2640 static_uninstall_ipv6 (struct prefix
*p
, struct static_ipv6
*si
)
2642 struct route_table
*table
;
2643 struct route_node
*rn
;
2645 struct nexthop
*nexthop
;
2648 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2652 /* Lookup existing route with type and distance. */
2653 rn
= route_node_lookup (table
, (struct prefix
*) p
);
2657 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2659 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2662 if (rib
->type
== ZEBRA_ROUTE_STATIC
&& rib
->distance
== si
->distance
)
2668 route_unlock_node (rn
);
2672 /* Lookup nexthop. */
2673 for (nexthop
= rib
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
2674 if (static_ipv6_nexthop_same (nexthop
, si
))
2677 /* Can't find nexthop. */
2680 route_unlock_node (rn
);
2684 /* Check nexthop. */
2685 if (rib
->nexthop_num
== 1)
2687 rib_delnode (rn
, rib
);
2691 if (CHECK_FLAG (nexthop
->flags
, NEXTHOP_FLAG_FIB
))
2692 rib_uninstall (rn
, rib
);
2693 nexthop_delete (rib
, nexthop
);
2694 nexthop_free (nexthop
);
2695 rib_queue_add (&zebrad
, rn
);
2698 route_unlock_node (rn
);
2701 /* Add static route into static route configuration. */
2703 static_add_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2704 const char *ifname
, u_char flags
, u_char distance
,
2707 struct route_node
*rn
;
2708 struct static_ipv6
*si
;
2709 struct static_ipv6
*pp
;
2710 struct static_ipv6
*cp
;
2711 struct route_table
*stable
;
2714 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2719 (type
== STATIC_IPV6_GATEWAY
|| type
== STATIC_IPV6_GATEWAY_IFNAME
))
2723 (type
== STATIC_IPV6_GATEWAY_IFNAME
|| type
== STATIC_IPV6_IFNAME
))
2726 /* Lookup static route prefix. */
2727 rn
= route_node_get (stable
, p
);
2729 /* Do nothing if there is a same static route. */
2730 for (si
= rn
->info
; si
; si
= si
->next
)
2732 if (distance
== si
->distance
2734 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2735 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2737 route_unlock_node (rn
);
2742 /* Make new static route structure. */
2743 si
= XCALLOC (MTYPE_STATIC_IPV6
, sizeof (struct static_ipv6
));
2746 si
->distance
= distance
;
2751 case STATIC_IPV6_GATEWAY
:
2754 case STATIC_IPV6_IFNAME
:
2755 si
->ifname
= XSTRDUP (0, ifname
);
2757 case STATIC_IPV6_GATEWAY_IFNAME
:
2759 si
->ifname
= XSTRDUP (0, ifname
);
2763 /* Add new static route information to the tree with sort by
2764 distance value and gateway address. */
2765 for (pp
= NULL
, cp
= rn
->info
; cp
; pp
= cp
, cp
= cp
->next
)
2767 if (si
->distance
< cp
->distance
)
2769 if (si
->distance
> cp
->distance
)
2773 /* Make linked list. */
2783 /* Install into rib. */
2784 static_install_ipv6 (p
, si
);
2789 /* Delete static route from static route configuration. */
2791 static_delete_ipv6 (struct prefix
*p
, u_char type
, struct in6_addr
*gate
,
2792 const char *ifname
, u_char distance
, u_int32_t vrf_id
)
2794 struct route_node
*rn
;
2795 struct static_ipv6
*si
;
2796 struct route_table
*stable
;
2799 stable
= vrf_static_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
2803 /* Lookup static route prefix. */
2804 rn
= route_node_lookup (stable
, p
);
2808 /* Find same static route is the tree */
2809 for (si
= rn
->info
; si
; si
= si
->next
)
2810 if (distance
== si
->distance
2812 && (! gate
|| IPV6_ADDR_SAME (gate
, &si
->ipv6
))
2813 && (! ifname
|| strcmp (ifname
, si
->ifname
) == 0))
2816 /* Can't find static route. */
2819 route_unlock_node (rn
);
2823 /* Install into rib. */
2824 static_uninstall_ipv6 (p
, si
);
2826 /* Unlink static route from linked list. */
2828 si
->prev
->next
= si
->next
;
2830 rn
->info
= si
->next
;
2832 si
->next
->prev
= si
->prev
;
2834 /* Free static route configuration. */
2836 XFREE (0, si
->ifname
);
2837 XFREE (MTYPE_STATIC_IPV6
, si
);
2841 #endif /* HAVE_IPV6 */
2843 /* RIB update function. */
2847 struct route_node
*rn
;
2848 struct route_table
*table
;
2850 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
2852 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2854 rib_queue_add (&zebrad
, rn
);
2856 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
2858 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2860 rib_queue_add (&zebrad
, rn
);
2863 /* Interface goes up. */
2865 rib_if_up (struct interface
*ifp
)
2870 /* Interface goes down. */
2872 rib_if_down (struct interface
*ifp
)
2877 /* Remove all routes which comes from non main table. */
2879 rib_weed_table (struct route_table
*table
)
2881 struct route_node
*rn
;
2886 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2887 for (rib
= rn
->info
; rib
; rib
= next
)
2891 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2894 if (rib
->table
!= zebrad
.rtm_table_default
&&
2895 rib
->table
!= RT_TABLE_MAIN
)
2896 rib_delnode (rn
, rib
);
2900 /* Delete all routes from non main table. */
2902 rib_weed_tables (void)
2904 rib_weed_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2905 rib_weed_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2908 /* Delete self installed routes after zebra is relaunched. */
2910 rib_sweep_table (struct route_table
*table
)
2912 struct route_node
*rn
;
2918 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2919 for (rib
= rn
->info
; rib
; rib
= next
)
2923 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
2926 if (rib
->type
== ZEBRA_ROUTE_KERNEL
&&
2927 CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELFROUTE
))
2929 ret
= rib_uninstall_kernel (rn
, rib
);
2931 rib_delnode (rn
, rib
);
2936 /* Sweep all RIB tables. */
2938 rib_sweep_route (void)
2940 rib_sweep_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2941 rib_sweep_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2944 /* Close RIB and clean up kernel routes. */
2946 rib_close_table (struct route_table
*table
)
2948 struct route_node
*rn
;
2952 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2953 for (rib
= rn
->info
; rib
; rib
= rib
->next
)
2955 if (! RIB_SYSTEM_ROUTE (rib
)
2956 && CHECK_FLAG (rib
->flags
, ZEBRA_FLAG_SELECTED
))
2957 rib_uninstall_kernel (rn
, rib
);
2961 /* Close all RIB tables. */
2965 rib_close_table (vrf_table (AFI_IP
, SAFI_UNICAST
, 0));
2966 rib_close_table (vrf_table (AFI_IP6
, SAFI_UNICAST
, 0));
2969 /* Routing information base initialize. */
2973 rib_queue_init (&zebrad
);
2974 /* VRF initialization. */