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 return prefix_cmp(&a
->prefix
, &b
->prefix
);
62 const char *bnc_str(struct bgp_nexthop_cache
*bnc
, char *buf
, int size
)
64 return prefix2str(&bnc
->prefix
, buf
, size
);
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
)
75 struct bgp_nexthop_cache
*bnc
;
77 bnc
= XCALLOC(MTYPE_BGP_NEXTHOP_CACHE
,
78 sizeof(struct bgp_nexthop_cache
));
79 bnc
->prefix
= *prefix
;
80 bnc
->srte_color
= srte_color
;
82 LIST_INIT(&(bnc
->paths
));
83 bgp_nexthop_cache_add(tree
, bnc
);
88 bool bnc_existing_for_prefix(struct bgp_nexthop_cache
*bnc
)
90 struct bgp_nexthop_cache
*bnc_tmp
;
92 frr_each (bgp_nexthop_cache
, bnc
->tree
, bnc_tmp
) {
95 if (prefix_cmp(&bnc
->prefix
, &bnc_tmp
->prefix
) == 0)
101 void bnc_free(struct bgp_nexthop_cache
*bnc
)
103 bnc_nexthop_free(bnc
);
104 bgp_nexthop_cache_del(bnc
->tree
, bnc
);
105 XFREE(MTYPE_BGP_NEXTHOP_CACHE
, bnc
);
108 struct bgp_nexthop_cache
*bnc_find(struct bgp_nexthop_cache_head
*tree
,
109 struct prefix
*prefix
, uint32_t srte_color
)
111 struct bgp_nexthop_cache bnc
= {};
116 bnc
.prefix
= *prefix
;
117 bnc
.srte_color
= srte_color
;
118 return bgp_nexthop_cache_find(tree
, &bnc
);
121 /* Reset and free all BGP nexthop cache. */
122 static void bgp_nexthop_cache_reset(struct bgp_nexthop_cache_head
*tree
)
124 struct bgp_nexthop_cache
*bnc
;
126 while (bgp_nexthop_cache_count(tree
) > 0) {
127 bnc
= bgp_nexthop_cache_first(tree
);
129 while (!LIST_EMPTY(&(bnc
->paths
))) {
130 struct bgp_path_info
*path
= LIST_FIRST(&(bnc
->paths
));
132 path_nh_map(path
, bnc
, false);
139 static void *bgp_tip_hash_alloc(void *p
)
141 const struct in_addr
*val
= (const struct in_addr
*)p
;
142 struct tip_addr
*addr
;
144 addr
= XMALLOC(MTYPE_TIP_ADDR
, sizeof(struct tip_addr
));
146 addr
->addr
.s_addr
= val
->s_addr
;
151 static void bgp_tip_hash_free(void *addr
)
153 XFREE(MTYPE_TIP_ADDR
, addr
);
156 static unsigned int bgp_tip_hash_key_make(const void *p
)
158 const struct tip_addr
*addr
= p
;
160 return jhash_1word(addr
->addr
.s_addr
, 0);
163 static bool bgp_tip_hash_cmp(const void *p1
, const void *p2
)
165 const struct tip_addr
*addr1
= p1
;
166 const struct tip_addr
*addr2
= p2
;
168 return addr1
->addr
.s_addr
== addr2
->addr
.s_addr
;
171 void bgp_tip_hash_init(struct bgp
*bgp
)
173 bgp
->tip_hash
= hash_create(bgp_tip_hash_key_make
, bgp_tip_hash_cmp
,
177 void bgp_tip_hash_destroy(struct bgp
*bgp
)
179 if (bgp
->tip_hash
== NULL
)
181 hash_clean(bgp
->tip_hash
, bgp_tip_hash_free
);
182 hash_free(bgp
->tip_hash
);
183 bgp
->tip_hash
= NULL
;
186 void bgp_tip_add(struct bgp
*bgp
, struct in_addr
*tip
)
189 struct tip_addr
*addr
;
193 addr
= hash_get(bgp
->tip_hash
, &tmp
, bgp_tip_hash_alloc
);
200 void bgp_tip_del(struct bgp
*bgp
, struct in_addr
*tip
)
203 struct tip_addr
*addr
;
207 addr
= hash_lookup(bgp
->tip_hash
, &tmp
);
208 /* may have been deleted earlier by bgp_interface_down() */
214 if (addr
->refcnt
== 0) {
215 hash_release(bgp
->tip_hash
, addr
);
216 XFREE(MTYPE_TIP_ADDR
, addr
);
220 /* BGP own address structure */
223 struct list
*ifp_name_list
;
226 static void show_address_entry(struct hash_bucket
*bucket
, void *args
)
228 struct vty
*vty
= (struct vty
*)args
;
229 struct bgp_addr
*addr
= (struct bgp_addr
*)bucket
->data
;
231 struct listnode
*node
;
232 char str
[INET6_ADDRSTRLEN
] = {0};
234 vty_out(vty
, "addr: %s, count: %d : ",
235 inet_ntop(addr
->p
.family
, &(addr
->p
.u
.prefix
),
236 str
, INET6_ADDRSTRLEN
),
237 addr
->ifp_name_list
->count
);
239 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
240 vty_out(vty
, " %s,", name
);
246 void bgp_nexthop_show_address_hash(struct vty
*vty
, struct bgp
*bgp
)
248 hash_iterate(bgp
->address_hash
,
249 (void (*)(struct hash_bucket
*, void *))show_address_entry
,
253 static void bgp_address_hash_string_del(void *val
)
257 XFREE(MTYPE_MARTIAN_STRING
, data
);
260 static void *bgp_address_hash_alloc(void *p
)
262 struct bgp_addr
*copy_addr
= p
;
263 struct bgp_addr
*addr
= NULL
;
265 addr
= XMALLOC(MTYPE_BGP_ADDR
, sizeof(struct bgp_addr
));
266 prefix_copy(&addr
->p
, ©_addr
->p
);
268 addr
->ifp_name_list
= list_new();
269 addr
->ifp_name_list
->del
= bgp_address_hash_string_del
;
274 static void bgp_address_hash_free(void *data
)
276 struct bgp_addr
*addr
= data
;
278 list_delete(&addr
->ifp_name_list
);
279 XFREE(MTYPE_BGP_ADDR
, addr
);
282 static unsigned int bgp_address_hash_key_make(const void *p
)
284 const struct bgp_addr
*addr
= p
;
286 return prefix_hash_key(&addr
->p
);
289 static bool bgp_address_hash_cmp(const void *p1
, const void *p2
)
291 const struct bgp_addr
*addr1
= p1
;
292 const struct bgp_addr
*addr2
= p2
;
294 return prefix_same(&addr1
->p
, &addr2
->p
);
297 void bgp_address_init(struct bgp
*bgp
)
300 hash_create(bgp_address_hash_key_make
, bgp_address_hash_cmp
,
301 "BGP Connected Address Hash");
304 void bgp_address_destroy(struct bgp
*bgp
)
306 if (bgp
->address_hash
== NULL
)
308 hash_clean(bgp
->address_hash
, bgp_address_hash_free
);
309 hash_free(bgp
->address_hash
);
310 bgp
->address_hash
= NULL
;
313 static void bgp_address_add(struct bgp
*bgp
, struct connected
*ifc
,
317 struct bgp_addr
*addr
;
318 struct listnode
*node
;
323 if (tmp
.p
.family
== AF_INET
)
324 tmp
.p
.prefixlen
= IPV4_MAX_BITLEN
;
325 else if (tmp
.p
.family
== AF_INET6
)
326 tmp
.p
.prefixlen
= IPV6_MAX_BITLEN
;
328 addr
= hash_get(bgp
->address_hash
, &tmp
, bgp_address_hash_alloc
);
330 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
331 if (strcmp(ifc
->ifp
->name
, name
) == 0)
335 name
= XSTRDUP(MTYPE_MARTIAN_STRING
, ifc
->ifp
->name
);
336 listnode_add(addr
->ifp_name_list
, name
);
340 static void bgp_address_del(struct bgp
*bgp
, struct connected
*ifc
,
344 struct bgp_addr
*addr
;
345 struct listnode
*node
;
350 if (tmp
.p
.family
== AF_INET
)
351 tmp
.p
.prefixlen
= IPV4_MAX_BITLEN
;
352 else if (tmp
.p
.family
== AF_INET6
)
353 tmp
.p
.prefixlen
= IPV6_MAX_BITLEN
;
355 addr
= hash_lookup(bgp
->address_hash
, &tmp
);
356 /* may have been deleted earlier by bgp_interface_down() */
360 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
361 if (strcmp(ifc
->ifp
->name
, name
) == 0)
366 list_delete_node(addr
->ifp_name_list
, node
);
367 XFREE(MTYPE_MARTIAN_STRING
, name
);
370 if (addr
->ifp_name_list
->count
== 0) {
371 hash_release(bgp
->address_hash
, addr
);
372 list_delete(&addr
->ifp_name_list
);
373 XFREE(MTYPE_BGP_ADDR
, addr
);
378 struct bgp_connected_ref
{
382 void bgp_connected_add(struct bgp
*bgp
, struct connected
*ifc
)
386 struct bgp_dest
*dest
;
387 struct bgp_connected_ref
*bc
;
388 struct listnode
*node
, *nnode
;
393 p
= *(CONNECTED_PREFIX(ifc
));
394 if (addr
->family
== AF_INET
) {
395 apply_mask_ipv4((struct prefix_ipv4
*)&p
);
397 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
400 bgp_address_add(bgp
, ifc
, addr
);
402 dest
= bgp_node_get(bgp
->connected_table
[AFI_IP
],
403 (struct prefix
*)&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
->status
!= Established
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
],
437 (struct prefix
*)&p
);
439 bc
= bgp_dest_get_bgp_connected_ref_info(dest
);
443 bc
= XCALLOC(MTYPE_BGP_CONN
,
444 sizeof(struct bgp_connected_ref
));
446 bgp_dest_set_bgp_connected_ref_info(dest
, bc
);
451 void bgp_connected_delete(struct bgp
*bgp
, struct connected
*ifc
)
455 struct bgp_dest
*dest
= NULL
;
456 struct bgp_connected_ref
*bc
;
460 p
= *(CONNECTED_PREFIX(ifc
));
462 if (addr
->family
== AF_INET
) {
463 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
466 bgp_address_del(bgp
, ifc
, addr
);
468 dest
= bgp_node_lookup(bgp
->connected_table
[AFI_IP
], &p
);
469 } else if (addr
->family
== AF_INET6
) {
470 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
473 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
476 bgp_address_del(bgp
, ifc
, addr
);
478 dest
= bgp_node_lookup(bgp
->connected_table
[AFI_IP6
], &p
);
484 bc
= bgp_dest_get_bgp_connected_ref_info(dest
);
486 if (bc
->refcnt
== 0) {
487 XFREE(MTYPE_BGP_CONN
, bc
);
488 bgp_dest_set_bgp_connected_ref_info(dest
, NULL
);
490 bgp_dest_unlock_node(dest
);
491 bgp_dest_unlock_node(dest
);
494 static void bgp_connected_cleanup(struct route_table
*table
,
495 struct route_node
*rn
)
497 struct bgp_connected_ref
*bc
;
498 struct bgp_dest
*bn
= bgp_dest_from_rnode(rn
);
500 bc
= bgp_dest_get_bgp_connected_ref_info(bn
);
505 if (bc
->refcnt
== 0) {
506 XFREE(MTYPE_BGP_CONN
, bc
);
507 bgp_dest_set_bgp_connected_ref_info(bn
, NULL
);
511 bool bgp_nexthop_self(struct bgp
*bgp
, afi_t afi
, uint8_t type
,
512 uint8_t sub_type
, struct attr
*attr
,
513 struct bgp_dest
*dest
)
515 uint8_t new_afi
= afi
== AFI_IP
? AF_INET
: AF_INET6
;
516 struct bgp_addr tmp_addr
= {{0}}, *addr
= NULL
;
517 struct tip_addr tmp_tip
, *tip
= NULL
;
518 const struct prefix
*p
= bgp_dest_get_prefix(dest
);
519 bool is_bgp_static_route
=
520 ((type
== ZEBRA_ROUTE_BGP
) && (sub_type
== BGP_ROUTE_STATIC
))
524 if (!is_bgp_static_route
)
525 new_afi
= BGP_ATTR_NEXTHOP_AFI_IP6(attr
) ? AF_INET6
: AF_INET
;
527 tmp_addr
.p
.family
= new_afi
;
530 if (is_bgp_static_route
) {
531 tmp_addr
.p
.u
.prefix4
= p
->u
.prefix4
;
532 tmp_addr
.p
.prefixlen
= p
->prefixlen
;
534 /* Here we need to find out which nexthop to be used*/
535 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
)) {
536 tmp_addr
.p
.u
.prefix4
= attr
->nexthop
;
537 tmp_addr
.p
.prefixlen
= IPV4_MAX_BITLEN
;
538 } else if ((attr
->mp_nexthop_len
)
539 && ((attr
->mp_nexthop_len
540 == BGP_ATTR_NHLEN_IPV4
)
541 || (attr
->mp_nexthop_len
542 == BGP_ATTR_NHLEN_VPNV4
))) {
543 tmp_addr
.p
.u
.prefix4
=
544 attr
->mp_nexthop_global_in
;
545 tmp_addr
.p
.prefixlen
= IPV4_MAX_BITLEN
;
551 if (is_bgp_static_route
) {
552 tmp_addr
.p
.u
.prefix6
= p
->u
.prefix6
;
553 tmp_addr
.p
.prefixlen
= p
->prefixlen
;
555 tmp_addr
.p
.u
.prefix6
= attr
->mp_nexthop_global
;
556 tmp_addr
.p
.prefixlen
= IPV6_MAX_BITLEN
;
563 addr
= hash_lookup(bgp
->address_hash
, &tmp_addr
);
567 if (new_afi
== AF_INET
&& hashcount(bgp
->tip_hash
)) {
568 memset(&tmp_tip
, 0, sizeof(struct tip_addr
));
569 tmp_tip
.addr
= attr
->nexthop
;
571 if (attr
->flag
& ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP
)) {
572 tmp_tip
.addr
= attr
->nexthop
;
573 } else if ((attr
->mp_nexthop_len
) &&
574 ((attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_IPV4
)
575 || (attr
->mp_nexthop_len
== BGP_ATTR_NHLEN_VPNV4
))) {
576 tmp_tip
.addr
= attr
->mp_nexthop_global_in
;
579 tip
= hash_lookup(bgp
->tip_hash
, &tmp_tip
);
587 bool bgp_multiaccess_check_v4(struct in_addr nexthop
, struct peer
*peer
)
589 struct bgp_dest
*dest1
;
590 struct bgp_dest
*dest2
;
595 p
.prefixlen
= IPV4_MAX_BITLEN
;
596 p
.u
.prefix4
= nexthop
;
598 dest1
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP
], &p
);
603 p
.prefixlen
= IPV4_MAX_BITLEN
;
604 p
.u
.prefix4
= peer
->su
.sin
.sin_addr
;
606 dest2
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP
], &p
);
608 bgp_dest_unlock_node(dest1
);
612 ret
= (dest1
== dest2
);
614 bgp_dest_unlock_node(dest1
);
615 bgp_dest_unlock_node(dest2
);
620 bool bgp_multiaccess_check_v6(struct in6_addr nexthop
, struct peer
*peer
)
622 struct bgp_dest
*dest1
;
623 struct bgp_dest
*dest2
;
628 p
.prefixlen
= IPV6_MAX_BITLEN
;
629 p
.u
.prefix6
= nexthop
;
631 dest1
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP6
], &p
);
636 p
.prefixlen
= IPV6_MAX_BITLEN
;
637 p
.u
.prefix6
= peer
->su
.sin6
.sin6_addr
;
639 dest2
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP6
], &p
);
641 bgp_dest_unlock_node(dest1
);
645 ret
= (dest1
== dest2
);
647 bgp_dest_unlock_node(dest1
);
648 bgp_dest_unlock_node(dest2
);
653 bool bgp_subgrp_multiaccess_check_v6(struct in6_addr nexthop
,
654 struct update_subgroup
*subgrp
,
655 struct peer
*exclude
)
657 struct bgp_dest
*dest1
= NULL
, *dest2
= NULL
;
658 struct peer_af
*paf
= NULL
;
659 struct prefix p
= {0}, np
= {0};
660 struct bgp
*bgp
= NULL
;
662 np
.family
= AF_INET6
;
663 np
.prefixlen
= IPV6_MAX_BITLEN
;
664 np
.u
.prefix6
= nexthop
;
667 p
.prefixlen
= IPV6_MAX_BITLEN
;
669 bgp
= SUBGRP_INST(subgrp
);
670 dest1
= bgp_node_match(bgp
->connected_table
[AFI_IP6
], &np
);
674 SUBGRP_FOREACH_PEER (subgrp
, paf
) {
675 /* Skip peer we're told to exclude - e.g., source of route. */
676 if (paf
->peer
== exclude
)
679 p
.u
.prefix6
= paf
->peer
->su
.sin6
.sin6_addr
;
680 dest2
= bgp_node_match(bgp
->connected_table
[AFI_IP6
], &p
);
681 if (dest1
== dest2
) {
682 bgp_dest_unlock_node(dest1
);
683 bgp_dest_unlock_node(dest2
);
688 bgp_dest_unlock_node(dest2
);
691 bgp_dest_unlock_node(dest1
);
695 bool bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop
,
696 struct update_subgroup
*subgrp
,
697 struct peer
*exclude
)
699 struct bgp_dest
*dest1
, *dest2
;
705 np
.prefixlen
= IPV4_MAX_BITLEN
;
706 np
.u
.prefix4
= nexthop
;
709 p
.prefixlen
= IPV4_MAX_BITLEN
;
711 bgp
= SUBGRP_INST(subgrp
);
712 dest1
= bgp_node_match(bgp
->connected_table
[AFI_IP
], &np
);
716 SUBGRP_FOREACH_PEER (subgrp
, paf
) {
717 /* Skip peer we're told to exclude - e.g., source of route. */
718 if (paf
->peer
== exclude
)
721 p
.u
.prefix4
= paf
->peer
->su
.sin
.sin_addr
;
723 dest2
= bgp_node_match(bgp
->connected_table
[AFI_IP
], &p
);
724 if (dest1
== dest2
) {
725 bgp_dest_unlock_node(dest1
);
726 bgp_dest_unlock_node(dest2
);
731 bgp_dest_unlock_node(dest2
);
734 bgp_dest_unlock_node(dest1
);
738 static void bgp_show_nexthop_paths(struct vty
*vty
, struct bgp
*bgp
,
739 struct bgp_nexthop_cache
*bnc
)
741 struct bgp_dest
*dest
;
742 struct bgp_path_info
*path
;
745 struct bgp_table
*table
;
746 struct bgp
*bgp_path
;
749 vty_out(vty
, " Paths:\n");
750 LIST_FOREACH (path
, &(bnc
->paths
), nh_thread
) {
752 assert(dest
&& bgp_dest_table(dest
));
753 afi
= family2afi(bgp_dest_get_prefix(dest
)->family
);
754 table
= bgp_dest_table(dest
);
756 bgp_path
= table
->bgp
;
759 prefix_rd2str((struct prefix_rd
*)bgp_dest_get_prefix(dest
->pdest
),
761 vty_out(vty
, " %d/%d %pBD RD %s %s flags 0x%x\n",
762 afi
, safi
, dest
, buf1
, bgp_path
->name_pretty
, path
->flags
);
764 vty_out(vty
, " %d/%d %pBD %s flags 0x%x\n",
765 afi
, safi
, dest
, bgp_path
->name_pretty
, path
->flags
);
769 static void bgp_show_nexthops_detail(struct vty
*vty
, struct bgp
*bgp
,
770 struct bgp_nexthop_cache
*bnc
)
772 char buf
[PREFIX2STR_BUFFER
];
773 struct nexthop
*nexthop
;
775 for (nexthop
= bnc
->nexthop
; nexthop
; nexthop
= nexthop
->next
) {
776 switch (nexthop
->type
) {
777 case NEXTHOP_TYPE_IPV6
:
778 vty_out(vty
, " gate %s\n",
779 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
782 case NEXTHOP_TYPE_IPV6_IFINDEX
:
783 vty_out(vty
, " gate %s, if %s\n",
784 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
786 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
788 case NEXTHOP_TYPE_IPV4
:
789 vty_out(vty
, " gate %s\n",
790 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
793 case NEXTHOP_TYPE_IFINDEX
:
794 vty_out(vty
, " if %s\n",
795 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
797 case NEXTHOP_TYPE_IPV4_IFINDEX
:
798 vty_out(vty
, " gate %s, if %s\n",
799 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
801 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
803 case NEXTHOP_TYPE_BLACKHOLE
:
804 vty_out(vty
, " blackhole\n");
807 vty_out(vty
, " invalid nexthop type %u\n",
813 static void bgp_show_nexthop(struct vty
*vty
, struct bgp
*bgp
,
814 struct bgp_nexthop_cache
*bnc
,
817 char buf
[PREFIX2STR_BUFFER
];
821 peer
= (struct peer
*)bnc
->nht_info
;
824 vty_out(vty
, " SR-TE color %u -", bnc
->srte_color
);
825 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
)) {
826 vty_out(vty
, " %s valid [IGP metric %d], #paths %d",
827 inet_ntop(bnc
->prefix
.family
, &bnc
->prefix
.u
.prefix
,
829 bnc
->metric
, bnc
->path_count
);
831 vty_out(vty
, ", peer %s", peer
->host
);
833 bgp_show_nexthops_detail(vty
, bgp
, bnc
);
835 vty_out(vty
, " %s invalid, #paths %d",
836 inet_ntop(bnc
->prefix
.family
, &bnc
->prefix
.u
.prefix
,
840 vty_out(vty
, ", peer %s", peer
->host
);
842 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
))
843 vty_out(vty
, " Must be Connected\n");
844 if (!CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
))
845 vty_out(vty
, " Is not Registered\n");
847 tbuf
= time(NULL
) - (bgp_clock() - bnc
->last_update
);
848 vty_out(vty
, " Last update: %s", ctime(&tbuf
));
851 /* show paths dependent on nexthop, if needed. */
853 bgp_show_nexthop_paths(vty
, bgp
, bnc
);
856 static void bgp_show_nexthops(struct vty
*vty
, struct bgp
*bgp
,
859 struct bgp_nexthop_cache
*bnc
;
861 struct bgp_nexthop_cache_head(*tree
)[AFI_MAX
];
864 vty_out(vty
, "Current BGP import check cache:\n");
866 vty_out(vty
, "Current BGP nexthop cache:\n");
868 tree
= &bgp
->import_check_table
;
870 tree
= &bgp
->nexthop_cache_table
;
871 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
872 frr_each (bgp_nexthop_cache
, &(*tree
)[afi
], bnc
)
873 bgp_show_nexthop(vty
, bgp
, bnc
, false);
877 static int show_ip_bgp_nexthop_table(struct vty
*vty
, const char *name
,
878 const char *nhopip_str
,
884 bgp
= bgp_lookup_by_name(name
);
886 bgp
= bgp_get_default();
888 vty_out(vty
, "%% No such BGP instance exist\n");
894 struct bgp_nexthop_cache_head (*tree
)[AFI_MAX
];
895 struct bgp_nexthop_cache
*bnc
;
897 if (!str2prefix(nhopip_str
, &nhop
)) {
898 vty_out(vty
, "nexthop address is malformed\n");
901 tree
= import_table
? &bgp
->import_check_table
902 : &bgp
->nexthop_cache_table
;
903 bnc
= bnc_find(tree
[family2afi(nhop
.family
)], &nhop
, 0);
905 vty_out(vty
, "specified nexthop does not have entry\n");
908 bgp_show_nexthop(vty
, bgp
, bnc
, true);
910 bgp_show_nexthops(vty
, bgp
, import_table
);
915 static void bgp_show_all_instances_nexthops_vty(struct vty
*vty
)
917 struct listnode
*node
, *nnode
;
920 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
921 vty_out(vty
, "\nInstance %s:\n",
922 (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
925 bgp_show_nexthops(vty
, bgp
, false);
929 DEFUN (show_ip_bgp_nexthop
,
930 show_ip_bgp_nexthop_cmd
,
931 "show [ip] bgp [<view|vrf> VIEWVRFNAME] nexthop [<A.B.C.D|X:X::X:X>] [detail]",
935 BGP_INSTANCE_HELP_STR
936 "BGP nexthop table\n"
937 "IPv4 nexthop address\n"
938 "IPv6 nexthop address\n"
939 "Show detailed information\n")
944 char *nhop_ip
= NULL
;
946 if (argv_find(argv
, argc
, "view", &idx
)
947 || argv_find(argv
, argc
, "vrf", &idx
))
948 vrf
= argv
[++idx
]->arg
;
950 if (argv_find(argv
, argc
, "A.B.C.D", &nh_idx
)
951 || argv_find(argv
, argc
, "X:X::X:X", &nh_idx
))
952 nhop_ip
= argv
[nh_idx
]->arg
;
954 return show_ip_bgp_nexthop_table(vty
, vrf
, nhop_ip
, false);
957 DEFUN (show_ip_bgp_import_check
,
958 show_ip_bgp_import_check_cmd
,
959 "show [ip] bgp [<view|vrf> VIEWVRFNAME] import-check-table [detail]",
963 BGP_INSTANCE_HELP_STR
964 "BGP import check table\n"
965 "Show detailed information\n")
970 if (argv_find(argv
, argc
, "view", &idx
)
971 || argv_find(argv
, argc
, "vrf", &idx
))
972 vrf
= argv
[++idx
]->arg
;
974 return show_ip_bgp_nexthop_table(vty
, vrf
, NULL
, true);
977 DEFUN (show_ip_bgp_instance_all_nexthop
,
978 show_ip_bgp_instance_all_nexthop_cmd
,
979 "show [ip] bgp <view|vrf> all nexthop",
983 BGP_INSTANCE_ALL_HELP_STR
984 "BGP nexthop table\n")
986 bgp_show_all_instances_nexthops_vty(vty
);
990 void bgp_scan_init(struct bgp
*bgp
)
994 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
995 bgp_nexthop_cache_init(&bgp
->nexthop_cache_table
[afi
]);
996 bgp_nexthop_cache_init(&bgp
->import_check_table
[afi
]);
997 bgp
->connected_table
[afi
] = bgp_table_init(bgp
, afi
,
1002 void bgp_scan_vty_init(void)
1004 install_element(VIEW_NODE
, &show_ip_bgp_nexthop_cmd
);
1005 install_element(VIEW_NODE
, &show_ip_bgp_import_check_cmd
);
1006 install_element(VIEW_NODE
, &show_ip_bgp_instance_all_nexthop_cmd
);
1009 void bgp_scan_finish(struct bgp
*bgp
)
1013 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
1014 /* Only the current one needs to be reset. */
1015 bgp_nexthop_cache_reset(&bgp
->nexthop_cache_table
[afi
]);
1016 bgp_nexthop_cache_reset(&bgp
->import_check_table
[afi
]);
1018 bgp
->connected_table
[afi
]->route_table
->cleanup
=
1019 bgp_connected_cleanup
;
1020 bgp_table_unlock(bgp
->connected_table
[afi
]);
1021 bgp
->connected_table
[afi
] = NULL
;
1025 char *bgp_nexthop_dump_bnc_flags(struct bgp_nexthop_cache
*bnc
, char *buf
,
1028 if (bnc
->flags
== 0) {
1029 snprintfrr(buf
, len
, "None ");
1033 snprintfrr(buf
, len
, "%s%s%s%s%s%s%s",
1034 CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
) ? "Valid " : "",
1035 CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_REGISTERED
) ? "Reg " : "",
1036 CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_CONNECTED
) ? "Conn " : "",
1037 CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_PEER_NOTIFIED
) ? "Notify "
1039 CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE
) ? "Static " : "",
1040 CHECK_FLAG(bnc
->flags
, BGP_STATIC_ROUTE_EXACT_MATCH
)
1043 CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_LABELED_VALID
)
1050 char *bgp_nexthop_dump_bnc_change_flags(struct bgp_nexthop_cache
*bnc
,
1051 char *buf
, size_t len
)
1053 if (bnc
->flags
== 0) {
1054 snprintfrr(buf
, len
, "None ");
1058 snprintfrr(buf
, len
, "%s%s%s",
1059 CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CHANGED
)
1062 CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_METRIC_CHANGED
)
1065 CHECK_FLAG(bnc
->change_flags
, BGP_NEXTHOP_CONNECTED_CHANGED
)