2 * Copyright (C) 2000 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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 #include "bgpd/bgpd.h"
39 #include "bgpd/bgp_route.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_nexthop.h"
42 #include "bgpd/bgp_nht.h"
43 #include "bgpd/bgp_debug.h"
44 #include "bgpd/bgp_damp.h"
45 #include "bgpd/bgp_fsm.h"
46 #include "bgpd/bgp_vty.h"
47 #include "bgpd/bgp_rd.h"
49 DEFINE_MTYPE_STATIC(BGPD
, MARTIAN_STRING
, "BGP Martian Addr Intf String");
51 int bgp_nexthop_cache_compare(const struct bgp_nexthop_cache
*a
,
52 const struct bgp_nexthop_cache
*b
)
54 if (a
->srte_color
< b
->srte_color
)
56 if (a
->srte_color
> b
->srte_color
)
59 if (a
->ifindex
< b
->ifindex
)
61 if (a
->ifindex
> b
->ifindex
)
64 return prefix_cmp(&a
->prefix
, &b
->prefix
);
67 void bnc_nexthop_free(struct bgp_nexthop_cache
*bnc
)
69 nexthops_free(bnc
->nexthop
);
72 struct bgp_nexthop_cache
*bnc_new(struct bgp_nexthop_cache_head
*tree
,
73 struct prefix
*prefix
, uint32_t srte_color
,
76 struct bgp_nexthop_cache
*bnc
;
78 bnc
= XCALLOC(MTYPE_BGP_NEXTHOP_CACHE
,
79 sizeof(struct bgp_nexthop_cache
));
80 bnc
->prefix
= *prefix
;
81 bnc
->ifindex
= ifindex
;
82 bnc
->srte_color
= srte_color
;
84 LIST_INIT(&(bnc
->paths
));
85 bgp_nexthop_cache_add(tree
, bnc
);
90 bool bnc_existing_for_prefix(struct bgp_nexthop_cache
*bnc
)
92 struct bgp_nexthop_cache
*bnc_tmp
;
94 frr_each (bgp_nexthop_cache
, bnc
->tree
, bnc_tmp
) {
97 if (prefix_cmp(&bnc
->prefix
, &bnc_tmp
->prefix
) == 0)
103 void bnc_free(struct bgp_nexthop_cache
*bnc
)
105 bnc_nexthop_free(bnc
);
106 bgp_nexthop_cache_del(bnc
->tree
, bnc
);
107 XFREE(MTYPE_BGP_NEXTHOP_CACHE
, bnc
);
110 struct bgp_nexthop_cache
*bnc_find(struct bgp_nexthop_cache_head
*tree
,
111 struct prefix
*prefix
, uint32_t srte_color
,
114 struct bgp_nexthop_cache bnc
= {};
119 bnc
.prefix
= *prefix
;
120 bnc
.srte_color
= srte_color
;
121 bnc
.ifindex
= ifindex
;
122 return bgp_nexthop_cache_find(tree
, &bnc
);
125 /* Reset and free all BGP nexthop cache. */
126 static void bgp_nexthop_cache_reset(struct bgp_nexthop_cache_head
*tree
)
128 struct bgp_nexthop_cache
*bnc
;
130 while (bgp_nexthop_cache_count(tree
) > 0) {
131 bnc
= bgp_nexthop_cache_first(tree
);
133 while (!LIST_EMPTY(&(bnc
->paths
))) {
134 struct bgp_path_info
*path
= LIST_FIRST(&(bnc
->paths
));
136 path_nh_map(path
, bnc
, false);
143 static void *bgp_tip_hash_alloc(void *p
)
145 const struct in_addr
*val
= (const struct in_addr
*)p
;
146 struct tip_addr
*addr
;
148 addr
= XMALLOC(MTYPE_TIP_ADDR
, sizeof(struct tip_addr
));
150 addr
->addr
.s_addr
= val
->s_addr
;
155 static void bgp_tip_hash_free(void *addr
)
157 XFREE(MTYPE_TIP_ADDR
, addr
);
160 static unsigned int bgp_tip_hash_key_make(const void *p
)
162 const struct tip_addr
*addr
= p
;
164 return jhash_1word(addr
->addr
.s_addr
, 0);
167 static bool bgp_tip_hash_cmp(const void *p1
, const void *p2
)
169 const struct tip_addr
*addr1
= p1
;
170 const struct tip_addr
*addr2
= p2
;
172 return addr1
->addr
.s_addr
== addr2
->addr
.s_addr
;
175 void bgp_tip_hash_init(struct bgp
*bgp
)
177 bgp
->tip_hash
= hash_create(bgp_tip_hash_key_make
, bgp_tip_hash_cmp
,
181 void bgp_tip_hash_destroy(struct bgp
*bgp
)
183 if (bgp
->tip_hash
== NULL
)
185 hash_clean(bgp
->tip_hash
, bgp_tip_hash_free
);
186 hash_free(bgp
->tip_hash
);
187 bgp
->tip_hash
= NULL
;
190 void bgp_tip_add(struct bgp
*bgp
, struct in_addr
*tip
)
193 struct tip_addr
*addr
;
197 addr
= hash_get(bgp
->tip_hash
, &tmp
, bgp_tip_hash_alloc
);
201 void bgp_tip_del(struct bgp
*bgp
, struct in_addr
*tip
)
204 struct tip_addr
*addr
;
208 addr
= hash_lookup(bgp
->tip_hash
, &tmp
);
209 /* may have been deleted earlier by bgp_interface_down() */
215 if (addr
->refcnt
== 0) {
216 hash_release(bgp
->tip_hash
, addr
);
217 XFREE(MTYPE_TIP_ADDR
, addr
);
221 /* BGP own address structure */
224 struct list
*ifp_name_list
;
227 static void show_address_entry(struct hash_bucket
*bucket
, void *args
)
229 struct vty
*vty
= (struct vty
*)args
;
230 struct bgp_addr
*addr
= (struct bgp_addr
*)bucket
->data
;
232 struct listnode
*node
;
233 char str
[INET6_ADDRSTRLEN
] = {0};
235 vty_out(vty
, "addr: %s, count: %d : ",
236 inet_ntop(addr
->p
.family
, &(addr
->p
.u
.prefix
),
237 str
, INET6_ADDRSTRLEN
),
238 addr
->ifp_name_list
->count
);
240 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
241 vty_out(vty
, " %s,", name
);
247 void bgp_nexthop_show_address_hash(struct vty
*vty
, struct bgp
*bgp
)
249 hash_iterate(bgp
->address_hash
,
250 (void (*)(struct hash_bucket
*, void *))show_address_entry
,
254 static void bgp_address_hash_string_del(void *val
)
258 XFREE(MTYPE_MARTIAN_STRING
, data
);
261 static void *bgp_address_hash_alloc(void *p
)
263 struct bgp_addr
*copy_addr
= p
;
264 struct bgp_addr
*addr
= NULL
;
266 addr
= XMALLOC(MTYPE_BGP_ADDR
, sizeof(struct bgp_addr
));
267 prefix_copy(&addr
->p
, ©_addr
->p
);
269 addr
->ifp_name_list
= list_new();
270 addr
->ifp_name_list
->del
= bgp_address_hash_string_del
;
275 static void bgp_address_hash_free(void *data
)
277 struct bgp_addr
*addr
= data
;
279 list_delete(&addr
->ifp_name_list
);
280 XFREE(MTYPE_BGP_ADDR
, addr
);
283 static unsigned int bgp_address_hash_key_make(const void *p
)
285 const struct bgp_addr
*addr
= p
;
287 return prefix_hash_key(&addr
->p
);
290 static bool bgp_address_hash_cmp(const void *p1
, const void *p2
)
292 const struct bgp_addr
*addr1
= p1
;
293 const struct bgp_addr
*addr2
= p2
;
295 return prefix_same(&addr1
->p
, &addr2
->p
);
298 void bgp_address_init(struct bgp
*bgp
)
301 hash_create(bgp_address_hash_key_make
, bgp_address_hash_cmp
,
302 "BGP Connected Address Hash");
305 void bgp_address_destroy(struct bgp
*bgp
)
307 if (bgp
->address_hash
== NULL
)
309 hash_clean(bgp
->address_hash
, bgp_address_hash_free
);
310 hash_free(bgp
->address_hash
);
311 bgp
->address_hash
= NULL
;
314 static void bgp_address_add(struct bgp
*bgp
, struct connected
*ifc
,
318 struct bgp_addr
*addr
;
319 struct listnode
*node
;
324 if (tmp
.p
.family
== AF_INET
)
325 tmp
.p
.prefixlen
= IPV4_MAX_BITLEN
;
326 else if (tmp
.p
.family
== AF_INET6
)
327 tmp
.p
.prefixlen
= IPV6_MAX_BITLEN
;
329 addr
= hash_get(bgp
->address_hash
, &tmp
, bgp_address_hash_alloc
);
331 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
332 if (strcmp(ifc
->ifp
->name
, name
) == 0)
336 name
= XSTRDUP(MTYPE_MARTIAN_STRING
, ifc
->ifp
->name
);
337 listnode_add(addr
->ifp_name_list
, name
);
341 static void bgp_address_del(struct bgp
*bgp
, struct connected
*ifc
,
345 struct bgp_addr
*addr
;
346 struct listnode
*node
;
351 if (tmp
.p
.family
== AF_INET
)
352 tmp
.p
.prefixlen
= IPV4_MAX_BITLEN
;
353 else if (tmp
.p
.family
== AF_INET6
)
354 tmp
.p
.prefixlen
= IPV6_MAX_BITLEN
;
356 addr
= hash_lookup(bgp
->address_hash
, &tmp
);
357 /* may have been deleted earlier by bgp_interface_down() */
361 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
362 if (strcmp(ifc
->ifp
->name
, name
) == 0)
367 list_delete_node(addr
->ifp_name_list
, node
);
368 XFREE(MTYPE_MARTIAN_STRING
, name
);
371 if (addr
->ifp_name_list
->count
== 0) {
372 hash_release(bgp
->address_hash
, addr
);
373 list_delete(&addr
->ifp_name_list
);
374 XFREE(MTYPE_BGP_ADDR
, addr
);
379 struct bgp_connected_ref
{
383 void bgp_connected_add(struct bgp
*bgp
, struct connected
*ifc
)
387 struct bgp_dest
*dest
;
388 struct bgp_connected_ref
*bc
;
389 struct listnode
*node
, *nnode
;
394 p
= *(CONNECTED_PREFIX(ifc
));
395 if (addr
->family
== AF_INET
) {
396 apply_mask_ipv4((struct prefix_ipv4
*)&p
);
398 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
401 bgp_address_add(bgp
, ifc
, addr
);
403 dest
= bgp_node_get(bgp
->connected_table
[AFI_IP
], &p
);
404 bc
= bgp_dest_get_bgp_connected_ref_info(dest
);
408 bc
= XCALLOC(MTYPE_BGP_CONN
,
409 sizeof(struct bgp_connected_ref
));
411 bgp_dest_set_bgp_connected_ref_info(dest
, bc
);
414 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
416 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)
417 && !peer_established(peer
)
418 && !CHECK_FLAG(peer
->flags
,
419 PEER_FLAG_IFPEER_V6ONLY
)) {
420 if (peer_active(peer
))
421 BGP_EVENT_ADD(peer
, BGP_Stop
);
422 BGP_EVENT_ADD(peer
, BGP_Start
);
425 } else if (addr
->family
== AF_INET6
) {
426 apply_mask_ipv6((struct prefix_ipv6
*)&p
);
428 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
431 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
434 bgp_address_add(bgp
, ifc
, addr
);
436 dest
= bgp_node_get(bgp
->connected_table
[AFI_IP6
], &p
);
438 bc
= bgp_dest_get_bgp_connected_ref_info(dest
);
442 bc
= XCALLOC(MTYPE_BGP_CONN
,
443 sizeof(struct bgp_connected_ref
));
445 bgp_dest_set_bgp_connected_ref_info(dest
, bc
);
450 void bgp_connected_delete(struct bgp
*bgp
, struct connected
*ifc
)
454 struct bgp_dest
*dest
= NULL
;
455 struct bgp_connected_ref
*bc
;
459 p
= *(CONNECTED_PREFIX(ifc
));
461 if (addr
->family
== AF_INET
) {
462 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
465 bgp_address_del(bgp
, ifc
, addr
);
467 dest
= bgp_node_lookup(bgp
->connected_table
[AFI_IP
], &p
);
468 } else if (addr
->family
== AF_INET6
) {
469 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
472 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
475 bgp_address_del(bgp
, ifc
, addr
);
477 dest
= bgp_node_lookup(bgp
->connected_table
[AFI_IP6
], &p
);
483 bc
= bgp_dest_get_bgp_connected_ref_info(dest
);
485 if (bc
->refcnt
== 0) {
486 XFREE(MTYPE_BGP_CONN
, bc
);
487 bgp_dest_set_bgp_connected_ref_info(dest
, NULL
);
489 bgp_dest_unlock_node(dest
);
490 bgp_dest_unlock_node(dest
);
493 static void bgp_connected_cleanup(struct route_table
*table
,
494 struct route_node
*rn
)
496 struct bgp_connected_ref
*bc
;
497 struct bgp_dest
*bn
= bgp_dest_from_rnode(rn
);
499 bc
= bgp_dest_get_bgp_connected_ref_info(bn
);
504 if (bc
->refcnt
== 0) {
505 XFREE(MTYPE_BGP_CONN
, bc
);
506 bgp_dest_set_bgp_connected_ref_info(bn
, NULL
);
510 bool bgp_nexthop_self(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
511 uint8_t sub_type
, struct attr
*attr
,
512 struct bgp_dest
*dest
)
514 uint8_t new_afi
= afi
== AFI_IP
? AF_INET
: AF_INET6
;
515 struct bgp_addr tmp_addr
= {{0}}, *addr
= NULL
;
516 struct tip_addr tmp_tip
, *tip
= NULL
;
517 const struct prefix
*p
= bgp_dest_get_prefix(dest
);
518 bool is_bgp_static_route
=
519 ((type
== ZEBRA_ROUTE_BGP
) && (sub_type
== BGP_ROUTE_STATIC
))
523 if (!is_bgp_static_route
)
524 new_afi
= BGP_ATTR_NEXTHOP_AFI_IP6(attr
) ? AF_INET6
: AF_INET
;
526 tmp_addr
.p
.family
= new_afi
;
529 if (is_bgp_static_route
) {
530 tmp_addr
.p
.u
.prefix4
= p
->u
.prefix4
;
531 tmp_addr
.p
.prefixlen
= p
->prefixlen
;
533 /* Here we need to find out which nexthop to be used*/
534 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
)) {
535 tmp_addr
.p
.u
.prefix4
= attr
->nexthop
;
536 tmp_addr
.p
.prefixlen
= IPV4_MAX_BITLEN
;
537 } else if ((attr
->mp_nexthop_len
)
538 && ((attr
->mp_nexthop_len
539 == BGP_ATTR_NHLEN_IPV4
)
540 || (attr
->mp_nexthop_len
541 == BGP_ATTR_NHLEN_VPNV4
))) {
542 tmp_addr
.p
.u
.prefix4
=
543 attr
->mp_nexthop_global_in
;
544 tmp_addr
.p
.prefixlen
= IPV4_MAX_BITLEN
;
550 if (is_bgp_static_route
) {
551 tmp_addr
.p
.u
.prefix6
= p
->u
.prefix6
;
552 tmp_addr
.p
.prefixlen
= p
->prefixlen
;
554 tmp_addr
.p
.u
.prefix6
= attr
->mp_nexthop_global
;
555 tmp_addr
.p
.prefixlen
= IPV6_MAX_BITLEN
;
562 addr
= hash_lookup(bgp
->address_hash
, &tmp_addr
);
566 if (new_afi
== AF_INET
&& hashcount(bgp
->tip_hash
)) {
567 memset(&tmp_tip
, 0, sizeof(tmp_tip
));
568 tmp_tip
.addr
= attr
->nexthop
;
570 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
)) {
571 tmp_tip
.addr
= attr
->nexthop
;
572 } else if ((attr
->mp_nexthop_len
) &&
573 ((attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV4
)
574 || (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV4
))) {
575 tmp_tip
.addr
= attr
->mp_nexthop_global_in
;
578 tip
= hash_lookup(bgp
->tip_hash
, &tmp_tip
);
586 bool bgp_multiaccess_check_v4(struct in_addr nexthop
, struct peer
*peer
)
588 struct bgp_dest
*dest1
;
589 struct bgp_dest
*dest2
;
594 p
.prefixlen
= IPV4_MAX_BITLEN
;
595 p
.u
.prefix4
= nexthop
;
597 dest1
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP
], &p
);
602 p
.prefixlen
= IPV4_MAX_BITLEN
;
603 p
.u
.prefix4
= peer
->su
.sin
.sin_addr
;
605 dest2
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP
], &p
);
607 bgp_dest_unlock_node(dest1
);
611 ret
= (dest1
== dest2
);
613 bgp_dest_unlock_node(dest1
);
614 bgp_dest_unlock_node(dest2
);
619 bool bgp_multiaccess_check_v6(struct in6_addr nexthop
, struct peer
*peer
)
621 struct bgp_dest
*dest1
;
622 struct bgp_dest
*dest2
;
627 p
.prefixlen
= IPV6_MAX_BITLEN
;
628 p
.u
.prefix6
= nexthop
;
630 dest1
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP6
], &p
);
635 p
.prefixlen
= IPV6_MAX_BITLEN
;
636 p
.u
.prefix6
= peer
->su
.sin6
.sin6_addr
;
638 dest2
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP6
], &p
);
640 bgp_dest_unlock_node(dest1
);
644 ret
= (dest1
== dest2
);
646 bgp_dest_unlock_node(dest1
);
647 bgp_dest_unlock_node(dest2
);
652 bool bgp_subgrp_multiaccess_check_v6(struct in6_addr nexthop
,
653 struct update_subgroup
*subgrp
,
654 struct peer
*exclude
)
656 struct bgp_dest
*dest1
= NULL
, *dest2
= NULL
;
657 struct peer_af
*paf
= NULL
;
658 struct prefix p
= {0}, np
= {0};
659 struct bgp
*bgp
= NULL
;
661 np
.family
= AF_INET6
;
662 np
.prefixlen
= IPV6_MAX_BITLEN
;
663 np
.u
.prefix6
= nexthop
;
666 p
.prefixlen
= IPV6_MAX_BITLEN
;
668 bgp
= SUBGRP_INST(subgrp
);
669 dest1
= bgp_node_match(bgp
->connected_table
[AFI_IP6
], &np
);
673 SUBGRP_FOREACH_PEER (subgrp
, paf
) {
674 /* Skip peer we're told to exclude - e.g., source of route. */
675 if (paf
->peer
== exclude
)
678 p
.u
.prefix6
= paf
->peer
->su
.sin6
.sin6_addr
;
679 dest2
= bgp_node_match(bgp
->connected_table
[AFI_IP6
], &p
);
680 if (dest1
== dest2
) {
681 bgp_dest_unlock_node(dest1
);
682 bgp_dest_unlock_node(dest2
);
687 bgp_dest_unlock_node(dest2
);
690 bgp_dest_unlock_node(dest1
);
694 bool bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop
,
695 struct update_subgroup
*subgrp
,
696 struct peer
*exclude
)
698 struct bgp_dest
*dest1
, *dest2
;
704 np
.prefixlen
= IPV4_MAX_BITLEN
;
705 np
.u
.prefix4
= nexthop
;
708 p
.prefixlen
= IPV4_MAX_BITLEN
;
710 bgp
= SUBGRP_INST(subgrp
);
711 dest1
= bgp_node_match(bgp
->connected_table
[AFI_IP
], &np
);
715 SUBGRP_FOREACH_PEER (subgrp
, paf
) {
716 /* Skip peer we're told to exclude - e.g., source of route. */
717 if (paf
->peer
== exclude
)
720 p
.u
.prefix4
= paf
->peer
->su
.sin
.sin_addr
;
722 dest2
= bgp_node_match(bgp
->connected_table
[AFI_IP
], &p
);
723 if (dest1
== dest2
) {
724 bgp_dest_unlock_node(dest1
);
725 bgp_dest_unlock_node(dest2
);
730 bgp_dest_unlock_node(dest2
);
733 bgp_dest_unlock_node(dest1
);
737 static void bgp_show_nexthop_paths(struct vty
*vty
, struct bgp
*bgp
,
738 struct bgp_nexthop_cache
*bnc
)
740 struct bgp_dest
*dest
;
741 struct bgp_path_info
*path
;
744 struct bgp_table
*table
;
745 struct bgp
*bgp_path
;
747 vty_out(vty
, " Paths:\n");
748 LIST_FOREACH (path
, &(bnc
->paths
), nh_thread
) {
750 assert(dest
&& bgp_dest_table(dest
));
751 afi
= family2afi(bgp_dest_get_prefix(dest
)->family
);
752 table
= bgp_dest_table(dest
);
754 bgp_path
= table
->bgp
;
757 vty_out(vty
, " %d/%d %pBD RD %pRD %s flags 0x%x\n",
759 (struct prefix_rd
*)bgp_dest_get_prefix(
761 bgp_path
->name_pretty
, path
->flags
);
763 vty_out(vty
, " %d/%d %pBD %s flags 0x%x\n",
764 afi
, safi
, dest
, bgp_path
->name_pretty
, path
->flags
);
768 static void bgp_show_nexthops_detail(struct vty
*vty
, struct bgp
*bgp
,
769 struct bgp_nexthop_cache
*bnc
)
771 struct nexthop
*nexthop
;
773 for (nexthop
= bnc
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
774 switch (nexthop
->type
) {
775 case NEXTHOP_TYPE_IPV6
:
776 vty_out(vty
, " gate %pI6\n", &nexthop
->gate
.ipv6
);
778 case NEXTHOP_TYPE_IPV6_IFINDEX
:
779 vty_out(vty
, " gate %pI6, if %s\n",
781 ifindex2ifname(bnc
->ifindex
? bnc
->ifindex
785 case NEXTHOP_TYPE_IPV4
:
786 vty_out(vty
, " gate %pI4\n", &nexthop
->gate
.ipv4
);
788 case NEXTHOP_TYPE_IFINDEX
:
789 vty_out(vty
, " if %s\n",
790 ifindex2ifname(bnc
->ifindex
? bnc
->ifindex
794 case NEXTHOP_TYPE_IPV4_IFINDEX
:
795 vty_out(vty
, " gate %pI4, if %s\n",
797 ifindex2ifname(bnc
->ifindex
? bnc
->ifindex
801 case NEXTHOP_TYPE_BLACKHOLE
:
802 vty_out(vty
, " blackhole\n");
805 vty_out(vty
, " invalid nexthop type %u\n",
811 static void bgp_show_nexthop(struct vty
*vty
, struct bgp
*bgp
,
812 struct bgp_nexthop_cache
*bnc
,
815 char buf
[PREFIX2STR_BUFFER
];
819 peer
= (struct peer
*)bnc
->nht_info
;
822 vty_out(vty
, " SR-TE color %u -", bnc
->srte_color
);
823 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
)) {
824 vty_out(vty
, " %s valid [IGP metric %d], #paths %d",
825 inet_ntop(bnc
->prefix
.family
, &bnc
->prefix
.u
.prefix
,
827 bnc
->metric
, bnc
->path_count
);
829 vty_out(vty
, ", peer %s", peer
->host
);
830 if (bnc
->is_evpn_gwip_nexthop
)
831 vty_out(vty
, " EVPN Gateway IP");
833 bgp_show_nexthops_detail(vty
, bgp
, bnc
);
834 } else if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_EVPN_INCOMPLETE
)) {
836 " %s overlay index unresolved [IGP metric %d], #paths %d",
837 inet_ntop(bnc
->prefix
.family
, &bnc
->prefix
.u
.prefix
,
839 bnc
->metric
, bnc
->path_count
);
840 if (bnc
->is_evpn_gwip_nexthop
)
841 vty_out(vty
, " EVPN Gateway IP");
843 bgp_show_nexthops_detail(vty
, bgp
, bnc
);
845 vty_out(vty
, " %s invalid, #paths %d",
846 inet_ntop(bnc
->prefix
.family
, &bnc
->prefix
.u
.prefix
,
850 vty_out(vty
, ", peer %s", peer
->host
);
851 if (bnc
->is_evpn_gwip_nexthop
)
852 vty_out(vty
, " EVPN Gateway IP");
854 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
))
855 vty_out(vty
, " Must be Connected\n");
856 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
))
857 vty_out(vty
, " Is not Registered\n");
859 tbuf
= time(NULL
) - (monotime(NULL
) - bnc
->last_update
);
860 vty_out(vty
, " Last update: %s", ctime(&tbuf
));
862 /* show paths dependent on nexthop, if needed. */
864 bgp_show_nexthop_paths(vty
, bgp
, bnc
);
867 static void bgp_show_nexthops(struct vty
*vty
, struct bgp
*bgp
,
870 struct bgp_nexthop_cache
*bnc
;
872 struct bgp_nexthop_cache_head(*tree
)[AFI_MAX
];
875 vty_out(vty
, "Current BGP import check cache:\n");
877 vty_out(vty
, "Current BGP nexthop cache:\n");
879 tree
= &bgp
->import_check_table
;
881 tree
= &bgp
->nexthop_cache_table
;
882 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
883 frr_each (bgp_nexthop_cache
, &(*tree
)[afi
], bnc
)
884 bgp_show_nexthop(vty
, bgp
, bnc
, false);
888 static int show_ip_bgp_nexthop_table(struct vty
*vty
, const char *name
,
889 const char *nhopip_str
,
895 bgp
= bgp_lookup_by_name(name
);
897 bgp
= bgp_get_default();
899 vty_out(vty
, "%% No such BGP instance exist\n");
905 struct bgp_nexthop_cache_head (*tree
)[AFI_MAX
];
906 struct bgp_nexthop_cache
*bnc
;
909 if (!str2prefix(nhopip_str
, &nhop
)) {
910 vty_out(vty
, "nexthop address is malformed\n");
913 tree
= import_table
? &bgp
->import_check_table
914 : &bgp
->nexthop_cache_table
;
915 frr_each (bgp_nexthop_cache
, &(*tree
)[family2afi(nhop
.family
)],
917 if (prefix_cmp(&bnc
->prefix
, &nhop
))
919 bgp_show_nexthop(vty
, bgp
, bnc
, true);
923 vty_out(vty
, "nexthop %s does not have entry\n",
926 bgp_show_nexthops(vty
, bgp
, import_table
);
931 static void bgp_show_all_instances_nexthops_vty(struct vty
*vty
)
933 struct listnode
*node
, *nnode
;
936 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
937 vty_out(vty
, "\nInstance %s:\n",
938 (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
941 bgp_show_nexthops(vty
, bgp
, false);
945 DEFUN (show_ip_bgp_nexthop
,
946 show_ip_bgp_nexthop_cmd
,
947 "show [ip] bgp [<view|vrf> VIEWVRFNAME] nexthop [<A.B.C.D|X:X::X:X>] [detail]",
951 BGP_INSTANCE_HELP_STR
952 "BGP nexthop table\n"
953 "IPv4 nexthop address\n"
954 "IPv6 nexthop address\n"
955 "Show detailed information\n")
960 char *nhop_ip
= NULL
;
962 if (argv_find(argv
, argc
, "view", &idx
)
963 || argv_find(argv
, argc
, "vrf", &idx
))
964 vrf
= argv
[++idx
]->arg
;
966 if (argv_find(argv
, argc
, "A.B.C.D", &nh_idx
)
967 || argv_find(argv
, argc
, "X:X::X:X", &nh_idx
))
968 nhop_ip
= argv
[nh_idx
]->arg
;
970 return show_ip_bgp_nexthop_table(vty
, vrf
, nhop_ip
, false);
973 DEFUN (show_ip_bgp_import_check
,
974 show_ip_bgp_import_check_cmd
,
975 "show [ip] bgp [<view|vrf> VIEWVRFNAME] import-check-table [detail]",
979 BGP_INSTANCE_HELP_STR
980 "BGP import check table\n"
981 "Show detailed information\n")
986 if (argv_find(argv
, argc
, "view", &idx
)
987 || argv_find(argv
, argc
, "vrf", &idx
))
988 vrf
= argv
[++idx
]->arg
;
990 return show_ip_bgp_nexthop_table(vty
, vrf
, NULL
, true);
993 DEFUN (show_ip_bgp_instance_all_nexthop
,
994 show_ip_bgp_instance_all_nexthop_cmd
,
995 "show [ip] bgp <view|vrf> all nexthop",
999 BGP_INSTANCE_ALL_HELP_STR
1000 "BGP nexthop table\n")
1002 bgp_show_all_instances_nexthops_vty(vty
);
1006 void bgp_scan_init(struct bgp
*bgp
)
1010 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1011 bgp_nexthop_cache_init(&bgp
->nexthop_cache_table
[afi
]);
1012 bgp_nexthop_cache_init(&bgp
->import_check_table
[afi
]);
1013 bgp
->connected_table
[afi
] = bgp_table_init(bgp
, afi
,
1018 void bgp_scan_vty_init(void)
1020 install_element(VIEW_NODE
, &show_ip_bgp_nexthop_cmd
);
1021 install_element(VIEW_NODE
, &show_ip_bgp_import_check_cmd
);
1022 install_element(VIEW_NODE
, &show_ip_bgp_instance_all_nexthop_cmd
);
1025 void bgp_scan_finish(struct bgp
*bgp
)
1029 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1030 /* Only the current one needs to be reset. */
1031 bgp_nexthop_cache_reset(&bgp
->nexthop_cache_table
[afi
]);
1032 bgp_nexthop_cache_reset(&bgp
->import_check_table
[afi
]);
1034 bgp
->connected_table
[afi
]->route_table
->cleanup
=
1035 bgp_connected_cleanup
;
1036 bgp_table_unlock(bgp
->connected_table
[afi
]);
1037 bgp
->connected_table
[afi
] = NULL
;
1041 char *bgp_nexthop_dump_bnc_flags(struct bgp_nexthop_cache
*bnc
, char *buf
,
1044 if (bnc
->flags
== 0) {
1045 snprintfrr(buf
, len
, "None ");
1049 snprintfrr(buf
, len
, "%s%s%s%s%s%s%s",
1050 CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
) ? "Valid " : "",
1051 CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
) ? "Reg " : "",
1052 CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
) ? "Conn " : "",
1053 CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
) ? "Notify "
1055 CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
) ? "Static " : "",
1056 CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
)
1059 CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_LABELED_VALID
)
1066 char *bgp_nexthop_dump_bnc_change_flags(struct bgp_nexthop_cache
*bnc
,
1067 char *buf
, size_t len
)
1069 if (bnc
->flags
== 0) {
1070 snprintfrr(buf
, len
, "None ");
1074 snprintfrr(buf
, len
, "%s%s%s",
1075 CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
)
1078 CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_METRIC_CHANGED
)
1081 CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CONNECTED_CHANGED
)