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
37 #include "bgpd/bgpd.h"
38 #include "bgpd/bgp_route.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_nexthop.h"
41 #include "bgpd/bgp_nht.h"
42 #include "bgpd/bgp_debug.h"
43 #include "bgpd/bgp_damp.h"
44 #include "bgpd/bgp_fsm.h"
45 #include "bgpd/bgp_vty.h"
46 #include "bgpd/bgp_rd.h"
48 DEFINE_MTYPE_STATIC(BGPD
, MARTIAN_STRING
, "BGP Martian Address Intf String");
50 int bgp_nexthop_cache_compare(const struct bgp_nexthop_cache
*a
,
51 const struct bgp_nexthop_cache
*b
)
53 if (a
->srte_color
< b
->srte_color
)
55 if (a
->srte_color
> b
->srte_color
)
58 return prefix_cmp(&a
->prefix
, &b
->prefix
);
61 const char *bnc_str(struct bgp_nexthop_cache
*bnc
, char *buf
, int size
)
63 return prefix2str(&bnc
->prefix
, buf
, size
);
66 void bnc_nexthop_free(struct bgp_nexthop_cache
*bnc
)
68 nexthops_free(bnc
->nexthop
);
71 struct bgp_nexthop_cache
*bnc_new(struct bgp_nexthop_cache_head
*tree
,
72 struct prefix
*prefix
, uint32_t srte_color
)
74 struct bgp_nexthop_cache
*bnc
;
76 bnc
= XCALLOC(MTYPE_BGP_NEXTHOP_CACHE
,
77 sizeof(struct bgp_nexthop_cache
));
78 bnc
->prefix
= *prefix
;
79 bnc
->srte_color
= srte_color
;
81 LIST_INIT(&(bnc
->paths
));
82 bgp_nexthop_cache_add(tree
, bnc
);
87 bool bnc_existing_for_prefix(struct bgp_nexthop_cache
*bnc
)
89 struct bgp_nexthop_cache
*bnc_tmp
;
91 frr_each (bgp_nexthop_cache
, bnc
->tree
, bnc_tmp
) {
94 if (prefix_cmp(&bnc
->prefix
, &bnc_tmp
->prefix
) == 0)
100 void bnc_free(struct bgp_nexthop_cache
*bnc
)
102 bnc_nexthop_free(bnc
);
103 bgp_nexthop_cache_del(bnc
->tree
, bnc
);
104 XFREE(MTYPE_BGP_NEXTHOP_CACHE
, bnc
);
107 struct bgp_nexthop_cache
*bnc_find(struct bgp_nexthop_cache_head
*tree
,
108 struct prefix
*prefix
, uint32_t srte_color
)
110 struct bgp_nexthop_cache bnc
= {};
115 bnc
.prefix
= *prefix
;
116 bnc
.srte_color
= srte_color
;
117 return bgp_nexthop_cache_find(tree
, &bnc
);
120 /* Reset and free all BGP nexthop cache. */
121 static void bgp_nexthop_cache_reset(struct bgp_nexthop_cache_head
*tree
)
123 struct bgp_nexthop_cache
*bnc
;
125 while (bgp_nexthop_cache_count(tree
) > 0) {
126 bnc
= bgp_nexthop_cache_first(tree
);
128 while (!LIST_EMPTY(&(bnc
->paths
))) {
129 struct bgp_path_info
*path
= LIST_FIRST(&(bnc
->paths
));
131 path_nh_map(path
, bnc
, false);
138 static void *bgp_tip_hash_alloc(void *p
)
140 const struct in_addr
*val
= (const struct in_addr
*)p
;
141 struct tip_addr
*addr
;
143 addr
= XMALLOC(MTYPE_TIP_ADDR
, sizeof(struct tip_addr
));
145 addr
->addr
.s_addr
= val
->s_addr
;
150 static void bgp_tip_hash_free(void *addr
)
152 XFREE(MTYPE_TIP_ADDR
, addr
);
155 static unsigned int bgp_tip_hash_key_make(const void *p
)
157 const struct tip_addr
*addr
= p
;
159 return jhash_1word(addr
->addr
.s_addr
, 0);
162 static bool bgp_tip_hash_cmp(const void *p1
, const void *p2
)
164 const struct tip_addr
*addr1
= p1
;
165 const struct tip_addr
*addr2
= p2
;
167 return addr1
->addr
.s_addr
== addr2
->addr
.s_addr
;
170 void bgp_tip_hash_init(struct bgp
*bgp
)
172 bgp
->tip_hash
= hash_create(bgp_tip_hash_key_make
, bgp_tip_hash_cmp
,
176 void bgp_tip_hash_destroy(struct bgp
*bgp
)
178 if (bgp
->tip_hash
== NULL
)
180 hash_clean(bgp
->tip_hash
, bgp_tip_hash_free
);
181 hash_free(bgp
->tip_hash
);
182 bgp
->tip_hash
= NULL
;
185 void bgp_tip_add(struct bgp
*bgp
, struct in_addr
*tip
)
188 struct tip_addr
*addr
;
192 addr
= hash_get(bgp
->tip_hash
, &tmp
, bgp_tip_hash_alloc
);
199 void bgp_tip_del(struct bgp
*bgp
, struct in_addr
*tip
)
202 struct tip_addr
*addr
;
206 addr
= hash_lookup(bgp
->tip_hash
, &tmp
);
207 /* may have been deleted earlier by bgp_interface_down() */
213 if (addr
->refcnt
== 0) {
214 hash_release(bgp
->tip_hash
, addr
);
215 XFREE(MTYPE_TIP_ADDR
, addr
);
219 /* BGP own address structure */
222 struct list
*ifp_name_list
;
225 static void show_address_entry(struct hash_bucket
*bucket
, void *args
)
227 struct vty
*vty
= (struct vty
*)args
;
228 struct bgp_addr
*addr
= (struct bgp_addr
*)bucket
->data
;
230 struct listnode
*node
;
231 char str
[INET6_ADDRSTRLEN
] = {0};
233 vty_out(vty
, "addr: %s, count: %d : ",
234 inet_ntop(addr
->p
.family
, &(addr
->p
.u
.prefix
),
235 str
, INET6_ADDRSTRLEN
),
236 addr
->ifp_name_list
->count
);
238 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
239 vty_out(vty
, " %s,", name
);
245 void bgp_nexthop_show_address_hash(struct vty
*vty
, struct bgp
*bgp
)
247 hash_iterate(bgp
->address_hash
,
248 (void (*)(struct hash_bucket
*, void *))show_address_entry
,
252 static void bgp_address_hash_string_del(void *val
)
256 XFREE(MTYPE_MARTIAN_STRING
, data
);
259 static void *bgp_address_hash_alloc(void *p
)
261 struct bgp_addr
*copy_addr
= p
;
262 struct bgp_addr
*addr
= NULL
;
264 addr
= XMALLOC(MTYPE_BGP_ADDR
, sizeof(struct bgp_addr
));
265 prefix_copy(&addr
->p
, ©_addr
->p
);
267 addr
->ifp_name_list
= list_new();
268 addr
->ifp_name_list
->del
= bgp_address_hash_string_del
;
273 static void bgp_address_hash_free(void *data
)
275 struct bgp_addr
*addr
= data
;
277 list_delete(&addr
->ifp_name_list
);
278 XFREE(MTYPE_BGP_ADDR
, addr
);
281 static unsigned int bgp_address_hash_key_make(const void *p
)
283 const struct bgp_addr
*addr
= p
;
285 return prefix_hash_key(&addr
->p
);
288 static bool bgp_address_hash_cmp(const void *p1
, const void *p2
)
290 const struct bgp_addr
*addr1
= p1
;
291 const struct bgp_addr
*addr2
= p2
;
293 return prefix_same(&addr1
->p
, &addr2
->p
);
296 void bgp_address_init(struct bgp
*bgp
)
299 hash_create(bgp_address_hash_key_make
, bgp_address_hash_cmp
,
300 "BGP Connected Address Hash");
303 void bgp_address_destroy(struct bgp
*bgp
)
305 if (bgp
->address_hash
== NULL
)
307 hash_clean(bgp
->address_hash
, bgp_address_hash_free
);
308 hash_free(bgp
->address_hash
);
309 bgp
->address_hash
= NULL
;
312 static void bgp_address_add(struct bgp
*bgp
, struct connected
*ifc
,
316 struct bgp_addr
*addr
;
317 struct listnode
*node
;
322 if (tmp
.p
.family
== AF_INET
)
323 tmp
.p
.prefixlen
= IPV4_MAX_BITLEN
;
324 else if (tmp
.p
.family
== AF_INET6
)
325 tmp
.p
.prefixlen
= IPV6_MAX_BITLEN
;
327 addr
= hash_get(bgp
->address_hash
, &tmp
, bgp_address_hash_alloc
);
329 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
330 if (strcmp(ifc
->ifp
->name
, name
) == 0)
334 name
= XSTRDUP(MTYPE_MARTIAN_STRING
, ifc
->ifp
->name
);
335 listnode_add(addr
->ifp_name_list
, name
);
339 static void bgp_address_del(struct bgp
*bgp
, struct connected
*ifc
,
343 struct bgp_addr
*addr
;
344 struct listnode
*node
;
349 if (tmp
.p
.family
== AF_INET
)
350 tmp
.p
.prefixlen
= IPV4_MAX_BITLEN
;
351 else if (tmp
.p
.family
== AF_INET6
)
352 tmp
.p
.prefixlen
= IPV6_MAX_BITLEN
;
354 addr
= hash_lookup(bgp
->address_hash
, &tmp
);
355 /* may have been deleted earlier by bgp_interface_down() */
359 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
360 if (strcmp(ifc
->ifp
->name
, name
) == 0)
365 list_delete_node(addr
->ifp_name_list
, node
);
366 XFREE(MTYPE_MARTIAN_STRING
, name
);
369 if (addr
->ifp_name_list
->count
== 0) {
370 hash_release(bgp
->address_hash
, addr
);
371 list_delete(&addr
->ifp_name_list
);
372 XFREE(MTYPE_BGP_ADDR
, addr
);
377 struct bgp_connected_ref
{
381 void bgp_connected_add(struct bgp
*bgp
, struct connected
*ifc
)
385 struct bgp_dest
*dest
;
386 struct bgp_connected_ref
*bc
;
387 struct listnode
*node
, *nnode
;
392 p
= *(CONNECTED_PREFIX(ifc
));
393 if (addr
->family
== AF_INET
) {
394 apply_mask_ipv4((struct prefix_ipv4
*)&p
);
396 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
399 bgp_address_add(bgp
, ifc
, addr
);
401 dest
= bgp_node_get(bgp
->connected_table
[AFI_IP
],
402 (struct prefix
*)&p
);
403 bc
= bgp_dest_get_bgp_connected_ref_info(dest
);
407 bc
= XCALLOC(MTYPE_BGP_CONN
,
408 sizeof(struct bgp_connected_ref
));
410 bgp_dest_set_bgp_connected_ref_info(dest
, bc
);
413 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
415 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)
416 && peer
->status
!= Established
417 && !CHECK_FLAG(peer
->flags
,
418 PEER_FLAG_IFPEER_V6ONLY
)) {
419 if (peer_active(peer
))
420 BGP_EVENT_ADD(peer
, BGP_Stop
);
421 BGP_EVENT_ADD(peer
, BGP_Start
);
424 } else if (addr
->family
== AF_INET6
) {
425 apply_mask_ipv6((struct prefix_ipv6
*)&p
);
427 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
430 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
433 bgp_address_add(bgp
, ifc
, addr
);
435 dest
= bgp_node_get(bgp
->connected_table
[AFI_IP6
],
436 (struct prefix
*)&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(struct tip_addr
));
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
;
748 vty_out(vty
, " Paths:\n");
749 LIST_FOREACH (path
, &(bnc
->paths
), nh_thread
) {
751 assert(dest
&& bgp_dest_table(dest
));
752 afi
= family2afi(bgp_dest_get_prefix(dest
)->family
);
753 table
= bgp_dest_table(dest
);
755 bgp_path
= table
->bgp
;
758 prefix_rd2str((struct prefix_rd
*)bgp_dest_get_prefix(dest
->pdest
),
760 vty_out(vty
, " %d/%d %pBD RD %s %s flags 0x%x\n",
761 afi
, safi
, dest
, buf1
, 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 char buf
[PREFIX2STR_BUFFER
];
772 struct nexthop
*nexthop
;
774 for (nexthop
= bnc
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
775 switch (nexthop
->type
) {
776 case NEXTHOP_TYPE_IPV6
:
777 vty_out(vty
, " gate %s\n",
778 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
781 case NEXTHOP_TYPE_IPV6_IFINDEX
:
782 vty_out(vty
, " gate %s, if %s\n",
783 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
785 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
787 case NEXTHOP_TYPE_IPV4
:
788 vty_out(vty
, " gate %s\n",
789 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
792 case NEXTHOP_TYPE_IFINDEX
:
793 vty_out(vty
, " if %s\n",
794 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
796 case NEXTHOP_TYPE_IPV4_IFINDEX
:
797 vty_out(vty
, " gate %s, if %s\n",
798 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
800 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
802 case NEXTHOP_TYPE_BLACKHOLE
:
803 vty_out(vty
, " blackhole\n");
806 vty_out(vty
, " invalid nexthop type %u\n",
812 static void bgp_show_nexthop(struct vty
*vty
, struct bgp
*bgp
,
813 struct bgp_nexthop_cache
*bnc
,
816 char buf
[PREFIX2STR_BUFFER
];
820 peer
= (struct peer
*)bnc
->nht_info
;
823 vty_out(vty
, " SR-TE color %u -", bnc
->srte_color
);
824 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
)) {
825 vty_out(vty
, " %s valid [IGP metric %d], #paths %d",
826 inet_ntop(bnc
->prefix
.family
, &bnc
->prefix
.u
.prefix
,
828 bnc
->metric
, bnc
->path_count
);
830 vty_out(vty
, ", peer %s", peer
->host
);
832 bgp_show_nexthops_detail(vty
, bgp
, bnc
);
834 vty_out(vty
, " %s invalid, #paths %d",
835 inet_ntop(bnc
->prefix
.family
, &bnc
->prefix
.u
.prefix
,
839 vty_out(vty
, ", peer %s", peer
->host
);
841 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
))
842 vty_out(vty
, " Must be Connected\n");
843 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
))
844 vty_out(vty
, " Is not Registered\n");
846 tbuf
= time(NULL
) - (bgp_clock() - bnc
->last_update
);
847 vty_out(vty
, " Last update: %s", ctime(&tbuf
));
850 /* show paths dependent on nexthop, if needed. */
852 bgp_show_nexthop_paths(vty
, bgp
, bnc
);
855 static void bgp_show_nexthops(struct vty
*vty
, struct bgp
*bgp
,
858 struct bgp_nexthop_cache
*bnc
;
860 struct bgp_nexthop_cache_head(*tree
)[AFI_MAX
];
863 vty_out(vty
, "Current BGP import check cache:\n");
865 vty_out(vty
, "Current BGP nexthop cache:\n");
867 tree
= &bgp
->import_check_table
;
869 tree
= &bgp
->nexthop_cache_table
;
870 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
871 frr_each (bgp_nexthop_cache
, &(*tree
)[afi
], bnc
)
872 bgp_show_nexthop(vty
, bgp
, bnc
, false);
876 static int show_ip_bgp_nexthop_table(struct vty
*vty
, const char *name
,
877 const char *nhopip_str
,
883 bgp
= bgp_lookup_by_name(name
);
885 bgp
= bgp_get_default();
887 vty_out(vty
, "%% No such BGP instance exist\n");
893 struct bgp_nexthop_cache_head (*tree
)[AFI_MAX
];
894 struct bgp_nexthop_cache
*bnc
;
896 if (!str2prefix(nhopip_str
, &nhop
)) {
897 vty_out(vty
, "nexthop address is malformed\n");
900 tree
= import_table
? &bgp
->import_check_table
901 : &bgp
->nexthop_cache_table
;
902 bnc
= bnc_find(tree
[family2afi(nhop
.family
)], &nhop
, 0);
904 vty_out(vty
, "specified nexthop does not have entry\n");
907 bgp_show_nexthop(vty
, bgp
, bnc
, true);
909 bgp_show_nexthops(vty
, bgp
, import_table
);
914 static void bgp_show_all_instances_nexthops_vty(struct vty
*vty
)
916 struct listnode
*node
, *nnode
;
919 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
920 vty_out(vty
, "\nInstance %s:\n",
921 (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
924 bgp_show_nexthops(vty
, bgp
, false);
928 DEFUN (show_ip_bgp_nexthop
,
929 show_ip_bgp_nexthop_cmd
,
930 "show [ip] bgp [<view|vrf> VIEWVRFNAME] nexthop [<A.B.C.D|X:X::X:X>] [detail]",
934 BGP_INSTANCE_HELP_STR
935 "BGP nexthop table\n"
936 "IPv4 nexthop address\n"
937 "IPv6 nexthop address\n"
938 "Show detailed information\n")
943 char *nhop_ip
= NULL
;
945 if (argv_find(argv
, argc
, "view", &idx
)
946 || argv_find(argv
, argc
, "vrf", &idx
))
947 vrf
= argv
[++idx
]->arg
;
949 if (argv_find(argv
, argc
, "A.B.C.D", &nh_idx
)
950 || argv_find(argv
, argc
, "X:X::X:X", &nh_idx
))
951 nhop_ip
= argv
[nh_idx
]->arg
;
953 return show_ip_bgp_nexthop_table(vty
, vrf
, nhop_ip
, false);
956 DEFUN (show_ip_bgp_import_check
,
957 show_ip_bgp_import_check_cmd
,
958 "show [ip] bgp [<view|vrf> VIEWVRFNAME] import-check-table [detail]",
962 BGP_INSTANCE_HELP_STR
963 "BGP import check table\n"
964 "Show detailed information\n")
969 if (argv_find(argv
, argc
, "view", &idx
)
970 || argv_find(argv
, argc
, "vrf", &idx
))
971 vrf
= argv
[++idx
]->arg
;
973 return show_ip_bgp_nexthop_table(vty
, vrf
, NULL
, true);
976 DEFUN (show_ip_bgp_instance_all_nexthop
,
977 show_ip_bgp_instance_all_nexthop_cmd
,
978 "show [ip] bgp <view|vrf> all nexthop",
982 BGP_INSTANCE_ALL_HELP_STR
983 "BGP nexthop table\n")
985 bgp_show_all_instances_nexthops_vty(vty
);
989 void bgp_scan_init(struct bgp
*bgp
)
993 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
994 bgp_nexthop_cache_init(&bgp
->nexthop_cache_table
[afi
]);
995 bgp_nexthop_cache_init(&bgp
->import_check_table
[afi
]);
996 bgp
->connected_table
[afi
] = bgp_table_init(bgp
, afi
,
1001 void bgp_scan_vty_init(void)
1003 install_element(VIEW_NODE
, &show_ip_bgp_nexthop_cmd
);
1004 install_element(VIEW_NODE
, &show_ip_bgp_import_check_cmd
);
1005 install_element(VIEW_NODE
, &show_ip_bgp_instance_all_nexthop_cmd
);
1008 void bgp_scan_finish(struct bgp
*bgp
)
1012 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1013 /* Only the current one needs to be reset. */
1014 bgp_nexthop_cache_reset(&bgp
->nexthop_cache_table
[afi
]);
1015 bgp_nexthop_cache_reset(&bgp
->import_check_table
[afi
]);
1017 bgp
->connected_table
[afi
]->route_table
->cleanup
=
1018 bgp_connected_cleanup
;
1019 bgp_table_unlock(bgp
->connected_table
[afi
]);
1020 bgp
->connected_table
[afi
] = NULL
;