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 static void *bgp_address_hash_alloc(void *p
)
176 const struct in_addr
*val
= (const struct in_addr
*)p
;
177 struct bgp_addr
*addr
;
179 addr
= XMALLOC(MTYPE_BGP_ADDR
, sizeof(struct bgp_addr
));
181 addr
->addr
.s_addr
= val
->s_addr
;
186 static void bgp_address_hash_free(void *addr
)
188 XFREE(MTYPE_BGP_ADDR
, addr
);
191 static unsigned int bgp_address_hash_key_make(void *p
)
193 const struct bgp_addr
*addr
= p
;
195 return jhash_1word(addr
->addr
.s_addr
, 0);
198 static int bgp_address_hash_cmp(const void *p1
, const void *p2
)
200 const struct bgp_addr
*addr1
= p1
;
201 const struct bgp_addr
*addr2
= p2
;
203 return addr1
->addr
.s_addr
== addr2
->addr
.s_addr
;
206 void bgp_address_init(struct bgp
*bgp
)
209 hash_create(bgp_address_hash_key_make
, bgp_address_hash_cmp
,
213 void bgp_address_destroy(struct bgp
*bgp
)
215 if (bgp
->address_hash
== NULL
)
217 hash_clean(bgp
->address_hash
, bgp_address_hash_free
);
218 hash_free(bgp
->address_hash
);
219 bgp
->address_hash
= NULL
;
222 static void bgp_address_add(struct bgp
*bgp
, struct prefix
*p
)
225 struct bgp_addr
*addr
;
227 tmp
.addr
= p
->u
.prefix4
;
229 addr
= hash_get(bgp
->address_hash
, &tmp
, bgp_address_hash_alloc
);
236 static void bgp_address_del(struct bgp
*bgp
, struct prefix
*p
)
239 struct bgp_addr
*addr
;
241 tmp
.addr
= p
->u
.prefix4
;
243 addr
= hash_lookup(bgp
->address_hash
, &tmp
);
244 /* may have been deleted earlier by bgp_interface_down() */
250 if (addr
->refcnt
== 0) {
251 hash_release(bgp
->address_hash
, addr
);
252 XFREE(MTYPE_BGP_ADDR
, addr
);
257 struct bgp_connected_ref
{
261 void bgp_connected_add(struct bgp
*bgp
, struct connected
*ifc
)
266 struct bgp_connected_ref
*bc
;
267 struct listnode
*node
, *nnode
;
272 p
= *(CONNECTED_PREFIX(ifc
));
273 if (addr
->family
== AF_INET
) {
274 apply_mask_ipv4((struct prefix_ipv4
*)&p
);
276 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
279 bgp_address_add(bgp
, addr
);
281 rn
= bgp_node_get(bgp
->connected_table
[AFI_IP
],
282 (struct prefix
*)&p
);
283 bc
= bgp_connected_get_node_info(rn
);
287 bc
= XCALLOC(MTYPE_BGP_CONN
,
288 sizeof(struct bgp_connected_ref
));
290 bgp_connected_set_node_info(rn
, bc
);
293 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
295 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)
296 && peer
->status
!= Established
297 && !CHECK_FLAG(peer
->flags
,
298 PEER_FLAG_IFPEER_V6ONLY
)) {
299 if (peer_active(peer
))
300 BGP_EVENT_ADD(peer
, BGP_Stop
);
301 BGP_EVENT_ADD(peer
, BGP_Start
);
304 } else if (addr
->family
== AF_INET6
) {
305 apply_mask_ipv6((struct prefix_ipv6
*)&p
);
307 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
310 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
313 rn
= bgp_node_get(bgp
->connected_table
[AFI_IP6
],
314 (struct prefix
*)&p
);
316 bc
= bgp_connected_get_node_info(rn
);
320 bc
= XCALLOC(MTYPE_BGP_CONN
,
321 sizeof(struct bgp_connected_ref
));
323 bgp_connected_set_node_info(rn
, bc
);
328 void bgp_connected_delete(struct bgp
*bgp
, struct connected
*ifc
)
332 struct bgp_node
*rn
= NULL
;
333 struct bgp_connected_ref
*bc
;
337 p
= *(CONNECTED_PREFIX(ifc
));
339 if (addr
->family
== AF_INET
) {
340 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
343 bgp_address_del(bgp
, addr
);
345 rn
= bgp_node_lookup(bgp
->connected_table
[AFI_IP
], &p
);
346 } else if (addr
->family
== AF_INET6
) {
347 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
350 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
353 rn
= bgp_node_lookup(bgp
->connected_table
[AFI_IP6
],
354 (struct prefix
*)&p
);
360 bc
= bgp_connected_get_node_info(rn
);
362 if (bc
->refcnt
== 0) {
363 XFREE(MTYPE_BGP_CONN
, bc
);
364 bgp_connected_set_node_info(rn
, NULL
);
370 static void bgp_connected_cleanup(struct route_table
*table
,
371 struct route_node
*rn
)
373 struct bgp_connected_ref
*bc
;
374 struct bgp_node
*bn
= bgp_node_from_rnode(rn
);
376 bc
= bgp_connected_get_node_info(bn
);
381 if (bc
->refcnt
== 0) {
382 XFREE(MTYPE_BGP_CONN
, bc
);
383 bgp_connected_set_node_info(bn
, NULL
);
387 int bgp_nexthop_self(struct bgp
*bgp
, struct in_addr nh_addr
)
389 struct bgp_addr tmp
, *addr
;
390 struct tip_addr tmp_tip
, *tip
;
394 addr
= hash_lookup(bgp
->address_hash
, &tmp
);
398 tmp_tip
.addr
= nh_addr
;
399 tip
= hash_lookup(bgp
->tip_hash
, &tmp_tip
);
406 int bgp_multiaccess_check_v4(struct in_addr nexthop
, struct peer
*peer
)
408 struct bgp_node
*rn1
;
409 struct bgp_node
*rn2
;
414 p
.prefixlen
= IPV4_MAX_BITLEN
;
415 p
.u
.prefix4
= nexthop
;
417 rn1
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP
], &p
);
422 p
.prefixlen
= IPV4_MAX_BITLEN
;
423 p
.u
.prefix4
= peer
->su
.sin
.sin_addr
;
425 rn2
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP
], &p
);
427 bgp_unlock_node(rn1
);
431 ret
= (rn1
== rn2
) ? 1 : 0;
433 bgp_unlock_node(rn1
);
434 bgp_unlock_node(rn2
);
439 int bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop
,
440 struct update_subgroup
*subgrp
)
442 struct bgp_node
*rn1
, *rn2
;
448 np
.prefixlen
= IPV4_MAX_BITLEN
;
449 np
.u
.prefix4
= nexthop
;
452 p
.prefixlen
= IPV4_MAX_BITLEN
;
454 bgp
= SUBGRP_INST(subgrp
);
455 rn1
= bgp_node_match(bgp
->connected_table
[AFI_IP
], &np
);
459 SUBGRP_FOREACH_PEER (subgrp
, paf
) {
460 p
.u
.prefix4
= paf
->peer
->su
.sin
.sin_addr
;
462 rn2
= bgp_node_match(bgp
->connected_table
[AFI_IP
], &p
);
464 bgp_unlock_node(rn1
);
465 bgp_unlock_node(rn2
);
470 bgp_unlock_node(rn2
);
473 bgp_unlock_node(rn1
);
477 static void bgp_show_nexthops_detail(struct vty
*vty
, struct bgp
*bgp
,
478 struct bgp_nexthop_cache
*bnc
)
480 char buf
[PREFIX2STR_BUFFER
];
481 struct nexthop
*nexthop
;
483 for (nexthop
= bnc
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
484 switch (nexthop
->type
) {
485 case NEXTHOP_TYPE_IPV6
:
486 vty_out(vty
, " gate %s\n",
487 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
490 case NEXTHOP_TYPE_IPV6_IFINDEX
:
491 vty_out(vty
, " gate %s, if %s\n",
492 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
494 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
496 case NEXTHOP_TYPE_IPV4
:
497 vty_out(vty
, " gate %s\n",
498 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
501 case NEXTHOP_TYPE_IFINDEX
:
502 vty_out(vty
, " if %s\n",
503 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
505 case NEXTHOP_TYPE_IPV4_IFINDEX
:
506 vty_out(vty
, " gate %s, if %s\n",
507 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
509 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
511 case NEXTHOP_TYPE_BLACKHOLE
:
512 vty_out(vty
, " blackhole\n");
515 vty_out(vty
, " invalid nexthop type %u\n",
520 static void bgp_show_nexthops(struct vty
*vty
, struct bgp
*bgp
, int detail
)
523 struct bgp_nexthop_cache
*bnc
;
524 char buf
[PREFIX2STR_BUFFER
];
528 vty_out(vty
, "Current BGP nexthop cache:\n");
529 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
530 if (!bgp
->nexthop_cache_table
[afi
])
533 for (rn
= bgp_table_top(bgp
->nexthop_cache_table
[afi
]); rn
;
534 rn
= bgp_route_next(rn
)) {
535 bnc
= bgp_nexthop_get_node_info(rn
);
539 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
)) {
541 " %s valid [IGP metric %d], #paths %d\n",
542 inet_ntop(rn
->p
.family
,
543 &rn
->p
.u
.prefix
, buf
,
545 bnc
->metric
, bnc
->path_count
);
550 bgp_show_nexthops_detail(vty
, bgp
, bnc
);
553 vty_out(vty
, " %s invalid\n",
554 inet_ntop(rn
->p
.family
,
555 &rn
->p
.u
.prefix
, buf
,
557 if (CHECK_FLAG(bnc
->flags
,
558 BGP_NEXTHOP_CONNECTED
))
559 vty_out(vty
, " Must be Connected\n");
561 tbuf
= time(NULL
) - (bgp_clock() - bnc
->last_update
);
562 vty_out(vty
, " Last update: %s", ctime(&tbuf
));
568 static int show_ip_bgp_nexthop_table(struct vty
*vty
, const char *name
,
574 bgp
= bgp_lookup_by_name(name
);
576 bgp
= bgp_get_default();
578 vty_out(vty
, "%% No such BGP instance exist\n");
582 bgp_show_nexthops(vty
, bgp
, detail
);
587 static void bgp_show_all_instances_nexthops_vty(struct vty
*vty
)
589 struct listnode
*node
, *nnode
;
592 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
593 vty_out(vty
, "\nInstance %s:\n",
594 (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
597 bgp_show_nexthops(vty
, bgp
, 0);
601 DEFUN (show_ip_bgp_nexthop
,
602 show_ip_bgp_nexthop_cmd
,
603 "show [ip] bgp [<view|vrf> VIEWVRFNAME] nexthop [detail]",
607 BGP_INSTANCE_HELP_STR
608 "BGP nexthop table\n"
609 "Show detailed information\n")
614 if (argv_find(argv
, argc
, "view", &idx
)
615 || argv_find(argv
, argc
, "vrf", &idx
))
616 vrf
= argv
[++idx
]->arg
;
617 int detail
= argv_find(argv
, argc
, "detail", &idx
) ? 1 : 0;
618 return show_ip_bgp_nexthop_table(vty
, vrf
, detail
);
621 DEFUN (show_ip_bgp_instance_all_nexthop
,
622 show_ip_bgp_instance_all_nexthop_cmd
,
623 "show [ip] bgp <view|vrf> all nexthop",
627 BGP_INSTANCE_ALL_HELP_STR
628 "BGP nexthop table\n")
630 bgp_show_all_instances_nexthops_vty(vty
);
634 void bgp_scan_init(struct bgp
*bgp
)
638 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
639 bgp
->nexthop_cache_table
[afi
] =
640 bgp_table_init(bgp
, afi
, SAFI_UNICAST
);
641 bgp
->connected_table
[afi
] = bgp_table_init(bgp
, afi
,
643 bgp
->import_check_table
[afi
] =
644 bgp_table_init(bgp
, afi
, SAFI_UNICAST
);
648 void bgp_scan_vty_init(void)
650 install_element(VIEW_NODE
, &show_ip_bgp_nexthop_cmd
);
651 install_element(VIEW_NODE
, &show_ip_bgp_instance_all_nexthop_cmd
);
654 void bgp_scan_finish(struct bgp
*bgp
)
658 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
659 /* Only the current one needs to be reset. */
660 bgp_nexthop_cache_reset(bgp
->nexthop_cache_table
[afi
]);
661 bgp_table_unlock(bgp
->nexthop_cache_table
[afi
]);
662 bgp
->nexthop_cache_table
[afi
] = NULL
;
664 bgp
->connected_table
[afi
]->route_table
->cleanup
=
665 bgp_connected_cleanup
;
666 bgp_table_unlock(bgp
->connected_table
[afi
]);
667 bgp
->connected_table
[afi
] = NULL
;
669 bgp_table_unlock(bgp
->import_check_table
[afi
]);
670 bgp
->import_check_table
[afi
] = NULL
;