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_table.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 "zebra/rib.h"
48 #include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */
50 char *bnc_str(struct bgp_nexthop_cache
*bnc
, char *buf
, int size
)
52 prefix2str(&(bnc
->node
->p
), buf
, size
);
56 void bnc_nexthop_free(struct bgp_nexthop_cache
*bnc
)
58 nexthops_free(bnc
->nexthop
);
61 struct bgp_nexthop_cache
*bnc_new(void)
63 struct bgp_nexthop_cache
*bnc
;
65 bnc
= XCALLOC(MTYPE_BGP_NEXTHOP_CACHE
,
66 sizeof(struct bgp_nexthop_cache
));
67 LIST_INIT(&(bnc
->paths
));
71 void bnc_free(struct bgp_nexthop_cache
*bnc
)
73 bnc_nexthop_free(bnc
);
74 XFREE(MTYPE_BGP_NEXTHOP_CACHE
, bnc
);
77 /* Reset and free all BGP nexthop cache. */
78 static void bgp_nexthop_cache_reset(struct bgp_table
*table
)
81 struct bgp_nexthop_cache
*bnc
;
83 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
84 bnc
= bgp_nexthop_get_node_info(rn
);
87 bgp_nexthop_set_node_info(rn
, NULL
);
93 static void *bgp_tip_hash_alloc(void *p
)
95 const struct in_addr
*val
= (const struct in_addr
*)p
;
96 struct tip_addr
*addr
;
98 addr
= XMALLOC(MTYPE_TIP_ADDR
, sizeof(struct tip_addr
));
100 addr
->addr
.s_addr
= val
->s_addr
;
105 static void bgp_tip_hash_free(void *addr
)
107 XFREE(MTYPE_TIP_ADDR
, addr
);
110 static unsigned int bgp_tip_hash_key_make(void *p
)
112 const struct tip_addr
*addr
= p
;
114 return jhash_1word(addr
->addr
.s_addr
, 0);
117 static int bgp_tip_hash_cmp(const void *p1
, const void *p2
)
119 const struct tip_addr
*addr1
= p1
;
120 const struct tip_addr
*addr2
= p2
;
122 return addr1
->addr
.s_addr
== addr2
->addr
.s_addr
;
125 void bgp_tip_hash_init(struct bgp
*bgp
)
127 bgp
->tip_hash
= hash_create(bgp_tip_hash_key_make
, bgp_tip_hash_cmp
,
131 void bgp_tip_hash_destroy(struct bgp
*bgp
)
133 if (bgp
->tip_hash
== NULL
)
135 hash_clean(bgp
->tip_hash
, bgp_tip_hash_free
);
136 hash_free(bgp
->tip_hash
);
137 bgp
->tip_hash
= NULL
;
140 void bgp_tip_add(struct bgp
*bgp
, struct in_addr
*tip
)
143 struct tip_addr
*addr
;
147 addr
= hash_get(bgp
->tip_hash
, &tmp
, bgp_tip_hash_alloc
);
154 void bgp_tip_del(struct bgp
*bgp
, struct in_addr
*tip
)
157 struct tip_addr
*addr
;
161 addr
= hash_lookup(bgp
->tip_hash
, &tmp
);
162 /* may have been deleted earlier by bgp_interface_down() */
168 if (addr
->refcnt
== 0) {
169 hash_release(bgp
->tip_hash
, addr
);
170 XFREE(MTYPE_TIP_ADDR
, addr
);
174 /* BGP own address structure */
177 struct list
*ifp_name_list
;
180 static void show_address_entry(struct hash_backet
*backet
, void *args
)
182 struct vty
*vty
= (struct vty
*)args
;
183 struct bgp_addr
*addr
= (struct bgp_addr
*)backet
->data
;
185 struct listnode
*node
;
187 vty_out(vty
, "addr: %s, count: %d : ", inet_ntoa(addr
->addr
),
188 addr
->ifp_name_list
->count
);
190 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
191 vty_out(vty
, " %s,", name
);
197 void bgp_nexthop_show_address_hash(struct vty
*vty
, struct bgp
*bgp
)
199 hash_iterate(bgp
->address_hash
,
200 (void (*)(struct hash_backet
*, void *))show_address_entry
,
204 static void bgp_address_hash_string_del(void *val
)
208 XFREE(MTYPE_TMP
, data
);
211 static void *bgp_address_hash_alloc(void *p
)
213 const struct in_addr
*val
= (const struct in_addr
*)p
;
214 struct bgp_addr
*addr
;
216 addr
= XMALLOC(MTYPE_BGP_ADDR
, sizeof(struct bgp_addr
));
217 addr
->addr
.s_addr
= val
->s_addr
;
219 addr
->ifp_name_list
= list_new();
220 addr
->ifp_name_list
->del
= bgp_address_hash_string_del
;
225 static void bgp_address_hash_free(void *data
)
227 struct bgp_addr
*addr
= data
;
229 list_delete(&addr
->ifp_name_list
);
230 XFREE(MTYPE_BGP_ADDR
, addr
);
233 static unsigned int bgp_address_hash_key_make(void *p
)
235 const struct bgp_addr
*addr
= p
;
237 return jhash_1word(addr
->addr
.s_addr
, 0);
240 static int bgp_address_hash_cmp(const void *p1
, const void *p2
)
242 const struct bgp_addr
*addr1
= p1
;
243 const struct bgp_addr
*addr2
= p2
;
245 return addr1
->addr
.s_addr
== addr2
->addr
.s_addr
;
248 void bgp_address_init(struct bgp
*bgp
)
251 hash_create(bgp_address_hash_key_make
, bgp_address_hash_cmp
,
255 void bgp_address_destroy(struct bgp
*bgp
)
257 if (bgp
->address_hash
== NULL
)
259 hash_clean(bgp
->address_hash
, bgp_address_hash_free
);
260 hash_free(bgp
->address_hash
);
261 bgp
->address_hash
= NULL
;
264 static void bgp_address_add(struct bgp
*bgp
, struct connected
*ifc
,
268 struct bgp_addr
*addr
;
269 struct listnode
*node
;
272 tmp
.addr
= p
->u
.prefix4
;
274 addr
= hash_get(bgp
->address_hash
, &tmp
, bgp_address_hash_alloc
);
276 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
277 if (strcmp(ifc
->ifp
->name
, name
) == 0)
281 name
= XSTRDUP(MTYPE_TMP
, ifc
->ifp
->name
);
282 listnode_add(addr
->ifp_name_list
, name
);
286 static void bgp_address_del(struct bgp
*bgp
, struct connected
*ifc
,
290 struct bgp_addr
*addr
;
291 struct listnode
*node
;
294 tmp
.addr
= p
->u
.prefix4
;
296 addr
= hash_lookup(bgp
->address_hash
, &tmp
);
297 /* may have been deleted earlier by bgp_interface_down() */
301 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
302 if (strcmp(ifc
->ifp
->name
, name
) == 0)
307 list_delete_node(addr
->ifp_name_list
, node
);
309 if (addr
->ifp_name_list
->count
== 0) {
310 hash_release(bgp
->address_hash
, addr
);
311 list_delete(&addr
->ifp_name_list
);
312 XFREE(MTYPE_BGP_ADDR
, addr
);
317 struct bgp_connected_ref
{
321 void bgp_connected_add(struct bgp
*bgp
, struct connected
*ifc
)
326 struct bgp_connected_ref
*bc
;
327 struct listnode
*node
, *nnode
;
332 p
= *(CONNECTED_PREFIX(ifc
));
333 if (addr
->family
== AF_INET
) {
334 apply_mask_ipv4((struct prefix_ipv4
*)&p
);
336 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
339 bgp_address_add(bgp
, ifc
, addr
);
341 rn
= bgp_node_get(bgp
->connected_table
[AFI_IP
],
342 (struct prefix
*)&p
);
343 bc
= bgp_connected_get_node_info(rn
);
347 bc
= XCALLOC(MTYPE_BGP_CONN
,
348 sizeof(struct bgp_connected_ref
));
350 bgp_connected_set_node_info(rn
, bc
);
353 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
355 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)
356 && peer
->status
!= Established
357 && !CHECK_FLAG(peer
->flags
,
358 PEER_FLAG_IFPEER_V6ONLY
)) {
359 if (peer_active(peer
))
360 BGP_EVENT_ADD(peer
, BGP_Stop
);
361 BGP_EVENT_ADD(peer
, BGP_Start
);
364 } else if (addr
->family
== AF_INET6
) {
365 apply_mask_ipv6((struct prefix_ipv6
*)&p
);
367 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
370 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
373 rn
= bgp_node_get(bgp
->connected_table
[AFI_IP6
],
374 (struct prefix
*)&p
);
376 bc
= bgp_connected_get_node_info(rn
);
380 bc
= XCALLOC(MTYPE_BGP_CONN
,
381 sizeof(struct bgp_connected_ref
));
383 bgp_connected_set_node_info(rn
, bc
);
388 void bgp_connected_delete(struct bgp
*bgp
, struct connected
*ifc
)
392 struct bgp_node
*rn
= NULL
;
393 struct bgp_connected_ref
*bc
;
397 p
= *(CONNECTED_PREFIX(ifc
));
399 if (addr
->family
== AF_INET
) {
400 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
403 bgp_address_del(bgp
, ifc
, addr
);
405 rn
= bgp_node_lookup(bgp
->connected_table
[AFI_IP
], &p
);
406 } else if (addr
->family
== AF_INET6
) {
407 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
410 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
413 rn
= bgp_node_lookup(bgp
->connected_table
[AFI_IP6
],
414 (struct prefix
*)&p
);
420 bc
= bgp_connected_get_node_info(rn
);
422 if (bc
->refcnt
== 0) {
423 XFREE(MTYPE_BGP_CONN
, bc
);
424 bgp_connected_set_node_info(rn
, NULL
);
430 static void bgp_connected_cleanup(struct route_table
*table
,
431 struct route_node
*rn
)
433 struct bgp_connected_ref
*bc
;
434 struct bgp_node
*bn
= bgp_node_from_rnode(rn
);
436 bc
= bgp_connected_get_node_info(bn
);
441 if (bc
->refcnt
== 0) {
442 XFREE(MTYPE_BGP_CONN
, bc
);
443 bgp_connected_set_node_info(bn
, NULL
);
447 int bgp_nexthop_self(struct bgp
*bgp
, struct in_addr nh_addr
)
449 struct bgp_addr tmp
, *addr
;
450 struct tip_addr tmp_tip
, *tip
;
454 addr
= hash_lookup(bgp
->address_hash
, &tmp
);
458 tmp_tip
.addr
= nh_addr
;
459 tip
= hash_lookup(bgp
->tip_hash
, &tmp_tip
);
466 int bgp_multiaccess_check_v4(struct in_addr nexthop
, struct peer
*peer
)
468 struct bgp_node
*rn1
;
469 struct bgp_node
*rn2
;
474 p
.prefixlen
= IPV4_MAX_BITLEN
;
475 p
.u
.prefix4
= nexthop
;
477 rn1
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP
], &p
);
482 p
.prefixlen
= IPV4_MAX_BITLEN
;
483 p
.u
.prefix4
= peer
->su
.sin
.sin_addr
;
485 rn2
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP
], &p
);
487 bgp_unlock_node(rn1
);
491 ret
= (rn1
== rn2
) ? 1 : 0;
493 bgp_unlock_node(rn1
);
494 bgp_unlock_node(rn2
);
499 int bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop
,
500 struct update_subgroup
*subgrp
)
502 struct bgp_node
*rn1
, *rn2
;
508 np
.prefixlen
= IPV4_MAX_BITLEN
;
509 np
.u
.prefix4
= nexthop
;
512 p
.prefixlen
= IPV4_MAX_BITLEN
;
514 bgp
= SUBGRP_INST(subgrp
);
515 rn1
= bgp_node_match(bgp
->connected_table
[AFI_IP
], &np
);
519 SUBGRP_FOREACH_PEER (subgrp
, paf
) {
520 p
.u
.prefix4
= paf
->peer
->su
.sin
.sin_addr
;
522 rn2
= bgp_node_match(bgp
->connected_table
[AFI_IP
], &p
);
524 bgp_unlock_node(rn1
);
525 bgp_unlock_node(rn2
);
530 bgp_unlock_node(rn2
);
533 bgp_unlock_node(rn1
);
537 static void bgp_show_nexthops_detail(struct vty
*vty
, struct bgp
*bgp
,
538 struct bgp_nexthop_cache
*bnc
)
540 char buf
[PREFIX2STR_BUFFER
];
541 struct nexthop
*nexthop
;
543 for (nexthop
= bnc
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
544 switch (nexthop
->type
) {
545 case NEXTHOP_TYPE_IPV6
:
546 vty_out(vty
, " gate %s\n",
547 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
550 case NEXTHOP_TYPE_IPV6_IFINDEX
:
551 vty_out(vty
, " gate %s, if %s\n",
552 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
554 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
556 case NEXTHOP_TYPE_IPV4
:
557 vty_out(vty
, " gate %s\n",
558 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
561 case NEXTHOP_TYPE_IFINDEX
:
562 vty_out(vty
, " if %s\n",
563 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
565 case NEXTHOP_TYPE_IPV4_IFINDEX
:
566 vty_out(vty
, " gate %s, if %s\n",
567 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
569 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
571 case NEXTHOP_TYPE_BLACKHOLE
:
572 vty_out(vty
, " blackhole\n");
575 vty_out(vty
, " invalid nexthop type %u\n",
580 static void bgp_show_nexthops(struct vty
*vty
, struct bgp
*bgp
, int detail
)
583 struct bgp_nexthop_cache
*bnc
;
584 char buf
[PREFIX2STR_BUFFER
];
588 vty_out(vty
, "Current BGP nexthop cache:\n");
589 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
590 if (!bgp
->nexthop_cache_table
[afi
])
593 for (rn
= bgp_table_top(bgp
->nexthop_cache_table
[afi
]); rn
;
594 rn
= bgp_route_next(rn
)) {
595 bnc
= bgp_nexthop_get_node_info(rn
);
599 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
)) {
601 " %s valid [IGP metric %d], #paths %d\n",
602 inet_ntop(rn
->p
.family
,
603 &rn
->p
.u
.prefix
, buf
,
605 bnc
->metric
, bnc
->path_count
);
610 bgp_show_nexthops_detail(vty
, bgp
, bnc
);
613 vty_out(vty
, " %s invalid\n",
614 inet_ntop(rn
->p
.family
,
615 &rn
->p
.u
.prefix
, buf
,
617 if (CHECK_FLAG(bnc
->flags
,
618 BGP_NEXTHOP_CONNECTED
))
619 vty_out(vty
, " Must be Connected\n");
621 tbuf
= time(NULL
) - (bgp_clock() - bnc
->last_update
);
622 vty_out(vty
, " Last update: %s", ctime(&tbuf
));
628 static int show_ip_bgp_nexthop_table(struct vty
*vty
, const char *name
,
634 bgp
= bgp_lookup_by_name(name
);
636 bgp
= bgp_get_default();
638 vty_out(vty
, "%% No such BGP instance exist\n");
642 bgp_show_nexthops(vty
, bgp
, detail
);
647 static void bgp_show_all_instances_nexthops_vty(struct vty
*vty
)
649 struct listnode
*node
, *nnode
;
652 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
653 vty_out(vty
, "\nInstance %s:\n",
654 (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
657 bgp_show_nexthops(vty
, bgp
, 0);
661 DEFUN (show_ip_bgp_nexthop
,
662 show_ip_bgp_nexthop_cmd
,
663 "show [ip] bgp [<view|vrf> VIEWVRFNAME] nexthop [detail]",
667 BGP_INSTANCE_HELP_STR
668 "BGP nexthop table\n"
669 "Show detailed information\n")
674 if (argv_find(argv
, argc
, "view", &idx
)
675 || argv_find(argv
, argc
, "vrf", &idx
))
676 vrf
= argv
[++idx
]->arg
;
677 int detail
= argv_find(argv
, argc
, "detail", &idx
) ? 1 : 0;
678 return show_ip_bgp_nexthop_table(vty
, vrf
, detail
);
681 DEFUN (show_ip_bgp_instance_all_nexthop
,
682 show_ip_bgp_instance_all_nexthop_cmd
,
683 "show [ip] bgp <view|vrf> all nexthop",
687 BGP_INSTANCE_ALL_HELP_STR
688 "BGP nexthop table\n")
690 bgp_show_all_instances_nexthops_vty(vty
);
694 void bgp_scan_init(struct bgp
*bgp
)
698 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
699 bgp
->nexthop_cache_table
[afi
] =
700 bgp_table_init(bgp
, afi
, SAFI_UNICAST
);
701 bgp
->connected_table
[afi
] = bgp_table_init(bgp
, afi
,
703 bgp
->import_check_table
[afi
] =
704 bgp_table_init(bgp
, afi
, SAFI_UNICAST
);
708 void bgp_scan_vty_init(void)
710 install_element(VIEW_NODE
, &show_ip_bgp_nexthop_cmd
);
711 install_element(VIEW_NODE
, &show_ip_bgp_instance_all_nexthop_cmd
);
714 void bgp_scan_finish(struct bgp
*bgp
)
718 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
719 /* Only the current one needs to be reset. */
720 bgp_nexthop_cache_reset(bgp
->nexthop_cache_table
[afi
]);
721 bgp_table_unlock(bgp
->nexthop_cache_table
[afi
]);
722 bgp
->nexthop_cache_table
[afi
] = NULL
;
724 bgp
->connected_table
[afi
]->route_table
->cleanup
=
725 bgp_connected_cleanup
;
726 bgp_table_unlock(bgp
->connected_table
[afi
]);
727 bgp
->connected_table
[afi
] = NULL
;
729 bgp_table_unlock(bgp
->import_check_table
[afi
]);
730 bgp
->import_check_table
[afi
] = NULL
;