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 DEFINE_MTYPE_STATIC(BGPD
, MARTIAN_STRING
, "BGP Martian Address Intf String");
52 char *bnc_str(struct bgp_nexthop_cache
*bnc
, char *buf
, int size
)
54 prefix2str(&(bnc
->node
->p
), buf
, size
);
58 void bnc_nexthop_free(struct bgp_nexthop_cache
*bnc
)
60 nexthops_free(bnc
->nexthop
);
63 struct bgp_nexthop_cache
*bnc_new(void)
65 struct bgp_nexthop_cache
*bnc
;
67 bnc
= XCALLOC(MTYPE_BGP_NEXTHOP_CACHE
,
68 sizeof(struct bgp_nexthop_cache
));
69 LIST_INIT(&(bnc
->paths
));
73 void bnc_free(struct bgp_nexthop_cache
*bnc
)
75 bnc_nexthop_free(bnc
);
76 XFREE(MTYPE_BGP_NEXTHOP_CACHE
, bnc
);
79 /* Reset and free all BGP nexthop cache. */
80 static void bgp_nexthop_cache_reset(struct bgp_table
*table
)
83 struct bgp_nexthop_cache
*bnc
;
85 for (rn
= bgp_table_top(table
); rn
; rn
= bgp_route_next(rn
)) {
86 bnc
= bgp_nexthop_get_node_info(rn
);
89 bgp_nexthop_set_node_info(rn
, NULL
);
95 static void *bgp_tip_hash_alloc(void *p
)
97 const struct in_addr
*val
= (const struct in_addr
*)p
;
98 struct tip_addr
*addr
;
100 addr
= XMALLOC(MTYPE_TIP_ADDR
, sizeof(struct tip_addr
));
102 addr
->addr
.s_addr
= val
->s_addr
;
107 static void bgp_tip_hash_free(void *addr
)
109 XFREE(MTYPE_TIP_ADDR
, addr
);
112 static unsigned int bgp_tip_hash_key_make(void *p
)
114 const struct tip_addr
*addr
= p
;
116 return jhash_1word(addr
->addr
.s_addr
, 0);
119 static int bgp_tip_hash_cmp(const void *p1
, const void *p2
)
121 const struct tip_addr
*addr1
= p1
;
122 const struct tip_addr
*addr2
= p2
;
124 return addr1
->addr
.s_addr
== addr2
->addr
.s_addr
;
127 void bgp_tip_hash_init(struct bgp
*bgp
)
129 bgp
->tip_hash
= hash_create(bgp_tip_hash_key_make
, bgp_tip_hash_cmp
,
133 void bgp_tip_hash_destroy(struct bgp
*bgp
)
135 if (bgp
->tip_hash
== NULL
)
137 hash_clean(bgp
->tip_hash
, bgp_tip_hash_free
);
138 hash_free(bgp
->tip_hash
);
139 bgp
->tip_hash
= NULL
;
142 void bgp_tip_add(struct bgp
*bgp
, struct in_addr
*tip
)
145 struct tip_addr
*addr
;
149 addr
= hash_get(bgp
->tip_hash
, &tmp
, bgp_tip_hash_alloc
);
156 void bgp_tip_del(struct bgp
*bgp
, struct in_addr
*tip
)
159 struct tip_addr
*addr
;
163 addr
= hash_lookup(bgp
->tip_hash
, &tmp
);
164 /* may have been deleted earlier by bgp_interface_down() */
170 if (addr
->refcnt
== 0) {
171 hash_release(bgp
->tip_hash
, addr
);
172 XFREE(MTYPE_TIP_ADDR
, addr
);
176 /* BGP own address structure */
179 struct list
*ifp_name_list
;
182 static void show_address_entry(struct hash_backet
*backet
, void *args
)
184 struct vty
*vty
= (struct vty
*)args
;
185 struct bgp_addr
*addr
= (struct bgp_addr
*)backet
->data
;
187 struct listnode
*node
;
189 vty_out(vty
, "addr: %s, count: %d : ", inet_ntoa(addr
->addr
),
190 addr
->ifp_name_list
->count
);
192 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
193 vty_out(vty
, " %s,", name
);
199 void bgp_nexthop_show_address_hash(struct vty
*vty
, struct bgp
*bgp
)
201 hash_iterate(bgp
->address_hash
,
202 (void (*)(struct hash_backet
*, void *))show_address_entry
,
206 static void bgp_address_hash_string_del(void *val
)
210 XFREE(MTYPE_MARTIAN_STRING
, data
);
213 static void *bgp_address_hash_alloc(void *p
)
215 const struct in_addr
*val
= (const struct in_addr
*)p
;
216 struct bgp_addr
*addr
;
218 addr
= XMALLOC(MTYPE_BGP_ADDR
, sizeof(struct bgp_addr
));
219 addr
->addr
.s_addr
= val
->s_addr
;
221 addr
->ifp_name_list
= list_new();
222 addr
->ifp_name_list
->del
= bgp_address_hash_string_del
;
227 static void bgp_address_hash_free(void *data
)
229 struct bgp_addr
*addr
= data
;
231 list_delete(&addr
->ifp_name_list
);
232 XFREE(MTYPE_BGP_ADDR
, addr
);
235 static unsigned int bgp_address_hash_key_make(void *p
)
237 const struct bgp_addr
*addr
= p
;
239 return jhash_1word(addr
->addr
.s_addr
, 0);
242 static int bgp_address_hash_cmp(const void *p1
, const void *p2
)
244 const struct bgp_addr
*addr1
= p1
;
245 const struct bgp_addr
*addr2
= p2
;
247 return addr1
->addr
.s_addr
== addr2
->addr
.s_addr
;
250 void bgp_address_init(struct bgp
*bgp
)
253 hash_create(bgp_address_hash_key_make
, bgp_address_hash_cmp
,
257 void bgp_address_destroy(struct bgp
*bgp
)
259 if (bgp
->address_hash
== NULL
)
261 hash_clean(bgp
->address_hash
, bgp_address_hash_free
);
262 hash_free(bgp
->address_hash
);
263 bgp
->address_hash
= NULL
;
266 static void bgp_address_add(struct bgp
*bgp
, struct connected
*ifc
,
270 struct bgp_addr
*addr
;
271 struct listnode
*node
;
274 tmp
.addr
= p
->u
.prefix4
;
276 addr
= hash_get(bgp
->address_hash
, &tmp
, bgp_address_hash_alloc
);
278 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
279 if (strcmp(ifc
->ifp
->name
, name
) == 0)
283 name
= XSTRDUP(MTYPE_MARTIAN_STRING
, ifc
->ifp
->name
);
284 listnode_add(addr
->ifp_name_list
, name
);
288 static void bgp_address_del(struct bgp
*bgp
, struct connected
*ifc
,
292 struct bgp_addr
*addr
;
293 struct listnode
*node
;
296 tmp
.addr
= p
->u
.prefix4
;
298 addr
= hash_lookup(bgp
->address_hash
, &tmp
);
299 /* may have been deleted earlier by bgp_interface_down() */
303 for (ALL_LIST_ELEMENTS_RO(addr
->ifp_name_list
, node
, name
)) {
304 if (strcmp(ifc
->ifp
->name
, name
) == 0)
309 list_delete_node(addr
->ifp_name_list
, node
);
310 XFREE(MTYPE_MARTIAN_STRING
, name
);
313 if (addr
->ifp_name_list
->count
== 0) {
314 hash_release(bgp
->address_hash
, addr
);
315 list_delete(&addr
->ifp_name_list
);
316 XFREE(MTYPE_BGP_ADDR
, addr
);
321 struct bgp_connected_ref
{
325 void bgp_connected_add(struct bgp
*bgp
, struct connected
*ifc
)
330 struct bgp_connected_ref
*bc
;
331 struct listnode
*node
, *nnode
;
336 p
= *(CONNECTED_PREFIX(ifc
));
337 if (addr
->family
== AF_INET
) {
338 apply_mask_ipv4((struct prefix_ipv4
*)&p
);
340 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
343 bgp_address_add(bgp
, ifc
, addr
);
345 rn
= bgp_node_get(bgp
->connected_table
[AFI_IP
],
346 (struct prefix
*)&p
);
347 bc
= bgp_connected_get_node_info(rn
);
351 bc
= XCALLOC(MTYPE_BGP_CONN
,
352 sizeof(struct bgp_connected_ref
));
354 bgp_connected_set_node_info(rn
, bc
);
357 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
359 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)
360 && peer
->status
!= Established
361 && !CHECK_FLAG(peer
->flags
,
362 PEER_FLAG_IFPEER_V6ONLY
)) {
363 if (peer_active(peer
))
364 BGP_EVENT_ADD(peer
, BGP_Stop
);
365 BGP_EVENT_ADD(peer
, BGP_Start
);
368 } else if (addr
->family
== AF_INET6
) {
369 apply_mask_ipv6((struct prefix_ipv6
*)&p
);
371 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
374 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
377 rn
= bgp_node_get(bgp
->connected_table
[AFI_IP6
],
378 (struct prefix
*)&p
);
380 bc
= bgp_connected_get_node_info(rn
);
384 bc
= XCALLOC(MTYPE_BGP_CONN
,
385 sizeof(struct bgp_connected_ref
));
387 bgp_connected_set_node_info(rn
, bc
);
392 void bgp_connected_delete(struct bgp
*bgp
, struct connected
*ifc
)
396 struct bgp_node
*rn
= NULL
;
397 struct bgp_connected_ref
*bc
;
401 p
= *(CONNECTED_PREFIX(ifc
));
403 if (addr
->family
== AF_INET
) {
404 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
407 bgp_address_del(bgp
, ifc
, addr
);
409 rn
= bgp_node_lookup(bgp
->connected_table
[AFI_IP
], &p
);
410 } else if (addr
->family
== AF_INET6
) {
411 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
414 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
417 rn
= bgp_node_lookup(bgp
->connected_table
[AFI_IP6
],
418 (struct prefix
*)&p
);
424 bc
= bgp_connected_get_node_info(rn
);
426 if (bc
->refcnt
== 0) {
427 XFREE(MTYPE_BGP_CONN
, bc
);
428 bgp_connected_set_node_info(rn
, NULL
);
434 static void bgp_connected_cleanup(struct route_table
*table
,
435 struct route_node
*rn
)
437 struct bgp_connected_ref
*bc
;
438 struct bgp_node
*bn
= bgp_node_from_rnode(rn
);
440 bc
= bgp_connected_get_node_info(bn
);
445 if (bc
->refcnt
== 0) {
446 XFREE(MTYPE_BGP_CONN
, bc
);
447 bgp_connected_set_node_info(bn
, NULL
);
451 int bgp_nexthop_self(struct bgp
*bgp
, struct in_addr nh_addr
)
453 struct bgp_addr tmp
, *addr
;
454 struct tip_addr tmp_tip
, *tip
;
458 addr
= hash_lookup(bgp
->address_hash
, &tmp
);
462 tmp_tip
.addr
= nh_addr
;
463 tip
= hash_lookup(bgp
->tip_hash
, &tmp_tip
);
470 int bgp_multiaccess_check_v4(struct in_addr nexthop
, struct peer
*peer
)
472 struct bgp_node
*rn1
;
473 struct bgp_node
*rn2
;
478 p
.prefixlen
= IPV4_MAX_BITLEN
;
479 p
.u
.prefix4
= nexthop
;
481 rn1
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP
], &p
);
486 p
.prefixlen
= IPV4_MAX_BITLEN
;
487 p
.u
.prefix4
= peer
->su
.sin
.sin_addr
;
489 rn2
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP
], &p
);
491 bgp_unlock_node(rn1
);
495 ret
= (rn1
== rn2
) ? 1 : 0;
497 bgp_unlock_node(rn1
);
498 bgp_unlock_node(rn2
);
503 int bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop
,
504 struct update_subgroup
*subgrp
)
506 struct bgp_node
*rn1
, *rn2
;
512 np
.prefixlen
= IPV4_MAX_BITLEN
;
513 np
.u
.prefix4
= nexthop
;
516 p
.prefixlen
= IPV4_MAX_BITLEN
;
518 bgp
= SUBGRP_INST(subgrp
);
519 rn1
= bgp_node_match(bgp
->connected_table
[AFI_IP
], &np
);
523 SUBGRP_FOREACH_PEER (subgrp
, paf
) {
524 p
.u
.prefix4
= paf
->peer
->su
.sin
.sin_addr
;
526 rn2
= bgp_node_match(bgp
->connected_table
[AFI_IP
], &p
);
528 bgp_unlock_node(rn1
);
529 bgp_unlock_node(rn2
);
534 bgp_unlock_node(rn2
);
537 bgp_unlock_node(rn1
);
541 static void bgp_show_nexthops_detail(struct vty
*vty
, struct bgp
*bgp
,
542 struct bgp_nexthop_cache
*bnc
)
544 char buf
[PREFIX2STR_BUFFER
];
545 struct nexthop
*nexthop
;
547 for (nexthop
= bnc
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
548 switch (nexthop
->type
) {
549 case NEXTHOP_TYPE_IPV6
:
550 vty_out(vty
, " gate %s\n",
551 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
554 case NEXTHOP_TYPE_IPV6_IFINDEX
:
555 vty_out(vty
, " gate %s, if %s\n",
556 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
558 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
560 case NEXTHOP_TYPE_IPV4
:
561 vty_out(vty
, " gate %s\n",
562 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
565 case NEXTHOP_TYPE_IFINDEX
:
566 vty_out(vty
, " if %s\n",
567 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
569 case NEXTHOP_TYPE_IPV4_IFINDEX
:
570 vty_out(vty
, " gate %s, if %s\n",
571 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
573 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
575 case NEXTHOP_TYPE_BLACKHOLE
:
576 vty_out(vty
, " blackhole\n");
579 vty_out(vty
, " invalid nexthop type %u\n",
584 static void bgp_show_nexthops(struct vty
*vty
, struct bgp
*bgp
, int detail
)
587 struct bgp_nexthop_cache
*bnc
;
588 char buf
[PREFIX2STR_BUFFER
];
592 vty_out(vty
, "Current BGP nexthop cache:\n");
593 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
594 if (!bgp
->nexthop_cache_table
[afi
])
597 for (rn
= bgp_table_top(bgp
->nexthop_cache_table
[afi
]); rn
;
598 rn
= bgp_route_next(rn
)) {
599 bnc
= bgp_nexthop_get_node_info(rn
);
603 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
)) {
605 " %s valid [IGP metric %d], #paths %d\n",
606 inet_ntop(rn
->p
.family
,
607 &rn
->p
.u
.prefix
, buf
,
609 bnc
->metric
, bnc
->path_count
);
614 bgp_show_nexthops_detail(vty
, bgp
, bnc
);
617 vty_out(vty
, " %s invalid\n",
618 inet_ntop(rn
->p
.family
,
619 &rn
->p
.u
.prefix
, buf
,
621 if (CHECK_FLAG(bnc
->flags
,
622 BGP_NEXTHOP_CONNECTED
))
623 vty_out(vty
, " Must be Connected\n");
625 tbuf
= time(NULL
) - (bgp_clock() - bnc
->last_update
);
626 vty_out(vty
, " Last update: %s", ctime(&tbuf
));
632 static int show_ip_bgp_nexthop_table(struct vty
*vty
, const char *name
,
638 bgp
= bgp_lookup_by_name(name
);
640 bgp
= bgp_get_default();
642 vty_out(vty
, "%% No such BGP instance exist\n");
646 bgp_show_nexthops(vty
, bgp
, detail
);
651 static void bgp_show_all_instances_nexthops_vty(struct vty
*vty
)
653 struct listnode
*node
, *nnode
;
656 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
657 vty_out(vty
, "\nInstance %s:\n",
658 (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
661 bgp_show_nexthops(vty
, bgp
, 0);
665 DEFUN (show_ip_bgp_nexthop
,
666 show_ip_bgp_nexthop_cmd
,
667 "show [ip] bgp [<view|vrf> VIEWVRFNAME] nexthop [detail]",
671 BGP_INSTANCE_HELP_STR
672 "BGP nexthop table\n"
673 "Show detailed information\n")
678 if (argv_find(argv
, argc
, "view", &idx
)
679 || argv_find(argv
, argc
, "vrf", &idx
))
680 vrf
= argv
[++idx
]->arg
;
681 int detail
= argv_find(argv
, argc
, "detail", &idx
) ? 1 : 0;
682 return show_ip_bgp_nexthop_table(vty
, vrf
, detail
);
685 DEFUN (show_ip_bgp_instance_all_nexthop
,
686 show_ip_bgp_instance_all_nexthop_cmd
,
687 "show [ip] bgp <view|vrf> all nexthop",
691 BGP_INSTANCE_ALL_HELP_STR
692 "BGP nexthop table\n")
694 bgp_show_all_instances_nexthops_vty(vty
);
698 void bgp_scan_init(struct bgp
*bgp
)
702 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
703 bgp
->nexthop_cache_table
[afi
] =
704 bgp_table_init(bgp
, afi
, SAFI_UNICAST
);
705 bgp
->connected_table
[afi
] = bgp_table_init(bgp
, afi
,
707 bgp
->import_check_table
[afi
] =
708 bgp_table_init(bgp
, afi
, SAFI_UNICAST
);
712 void bgp_scan_vty_init(void)
714 install_element(VIEW_NODE
, &show_ip_bgp_nexthop_cmd
);
715 install_element(VIEW_NODE
, &show_ip_bgp_instance_all_nexthop_cmd
);
718 void bgp_scan_finish(struct bgp
*bgp
)
722 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
723 /* Only the current one needs to be reset. */
724 bgp_nexthop_cache_reset(bgp
->nexthop_cache_table
[afi
]);
725 bgp_table_unlock(bgp
->nexthop_cache_table
[afi
]);
726 bgp
->nexthop_cache_table
[afi
] = NULL
;
728 bgp
->connected_table
[afi
]->route_table
->cleanup
=
729 bgp_connected_cleanup
;
730 bgp_table_unlock(bgp
->connected_table
[afi
]);
731 bgp
->connected_table
[afi
] = NULL
;
733 bgp_table_unlock(bgp
->import_check_table
[afi
]);
734 bgp
->import_check_table
[afi
] = NULL
;