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 if ((bnc
= rn
->info
) != NULL
) {
91 static void *bgp_tip_hash_alloc(void *p
)
93 const struct in_addr
*val
= (const struct in_addr
*)p
;
94 struct tip_addr
*addr
;
96 addr
= XMALLOC(MTYPE_TIP_ADDR
, sizeof(struct tip_addr
));
98 addr
->addr
.s_addr
= val
->s_addr
;
103 static void bgp_tip_hash_free(void *addr
)
105 XFREE(MTYPE_TIP_ADDR
, addr
);
108 static unsigned int bgp_tip_hash_key_make(void *p
)
110 const struct tip_addr
*addr
= p
;
112 return jhash_1word(addr
->addr
.s_addr
, 0);
115 static int bgp_tip_hash_cmp(const void *p1
, const void *p2
)
117 const struct tip_addr
*addr1
= p1
;
118 const struct tip_addr
*addr2
= p2
;
120 return addr1
->addr
.s_addr
== addr2
->addr
.s_addr
;
123 void bgp_tip_hash_init(struct bgp
*bgp
)
125 bgp
->tip_hash
= hash_create(bgp_tip_hash_key_make
, bgp_tip_hash_cmp
,
129 void bgp_tip_hash_destroy(struct bgp
*bgp
)
131 if (bgp
->tip_hash
== NULL
)
133 hash_clean(bgp
->tip_hash
, bgp_tip_hash_free
);
134 hash_free(bgp
->tip_hash
);
135 bgp
->tip_hash
= NULL
;
138 void bgp_tip_add(struct bgp
*bgp
, struct in_addr
*tip
)
141 struct tip_addr
*addr
;
145 addr
= hash_get(bgp
->tip_hash
, &tmp
, bgp_tip_hash_alloc
);
152 void bgp_tip_del(struct bgp
*bgp
, struct in_addr
*tip
)
155 struct tip_addr
*addr
;
159 addr
= hash_lookup(bgp
->tip_hash
, &tmp
);
160 /* may have been deleted earlier by bgp_interface_down() */
166 if (addr
->refcnt
== 0) {
167 hash_release(bgp
->tip_hash
, addr
);
168 XFREE(MTYPE_TIP_ADDR
, addr
);
172 static void *bgp_address_hash_alloc(void *p
)
174 const struct in_addr
*val
= (const struct in_addr
*)p
;
175 struct bgp_addr
*addr
;
177 addr
= XMALLOC(MTYPE_BGP_ADDR
, sizeof(struct bgp_addr
));
179 addr
->addr
.s_addr
= val
->s_addr
;
184 static void bgp_address_hash_free(void *addr
)
186 XFREE(MTYPE_BGP_ADDR
, addr
);
189 static unsigned int bgp_address_hash_key_make(void *p
)
191 const struct bgp_addr
*addr
= p
;
193 return jhash_1word(addr
->addr
.s_addr
, 0);
196 static int bgp_address_hash_cmp(const void *p1
, const void *p2
)
198 const struct bgp_addr
*addr1
= p1
;
199 const struct bgp_addr
*addr2
= p2
;
201 return addr1
->addr
.s_addr
== addr2
->addr
.s_addr
;
204 void bgp_address_init(struct bgp
*bgp
)
207 hash_create(bgp_address_hash_key_make
, bgp_address_hash_cmp
,
211 void bgp_address_destroy(struct bgp
*bgp
)
213 if (bgp
->address_hash
== NULL
)
215 hash_clean(bgp
->address_hash
, bgp_address_hash_free
);
216 hash_free(bgp
->address_hash
);
217 bgp
->address_hash
= NULL
;
220 static void bgp_address_add(struct bgp
*bgp
, struct prefix
*p
)
223 struct bgp_addr
*addr
;
225 tmp
.addr
= p
->u
.prefix4
;
227 addr
= hash_get(bgp
->address_hash
, &tmp
, bgp_address_hash_alloc
);
234 static void bgp_address_del(struct bgp
*bgp
, struct prefix
*p
)
237 struct bgp_addr
*addr
;
239 tmp
.addr
= p
->u
.prefix4
;
241 addr
= hash_lookup(bgp
->address_hash
, &tmp
);
242 /* may have been deleted earlier by bgp_interface_down() */
248 if (addr
->refcnt
== 0) {
249 hash_release(bgp
->address_hash
, addr
);
250 XFREE(MTYPE_BGP_ADDR
, addr
);
255 struct bgp_connected_ref
{
259 void bgp_connected_add(struct bgp
*bgp
, struct connected
*ifc
)
264 struct bgp_connected_ref
*bc
;
265 struct listnode
*node
, *nnode
;
270 p
= *(CONNECTED_PREFIX(ifc
));
271 if (addr
->family
== AF_INET
) {
272 apply_mask_ipv4((struct prefix_ipv4
*)&p
);
274 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
277 bgp_address_add(bgp
, addr
);
279 rn
= bgp_node_get(bgp
->connected_table
[AFI_IP
],
280 (struct prefix
*)&p
);
285 bc
= XCALLOC(MTYPE_BGP_CONN
,
286 sizeof(struct bgp_connected_ref
));
291 for (ALL_LIST_ELEMENTS(bgp
->peer
, node
, nnode
, peer
)) {
293 && (strcmp(peer
->conf_if
, ifc
->ifp
->name
) == 0)
294 && peer
->status
!= Established
295 && !CHECK_FLAG(peer
->flags
,
296 PEER_FLAG_IFPEER_V6ONLY
)) {
297 if (peer_active(peer
))
298 BGP_EVENT_ADD(peer
, BGP_Stop
);
299 BGP_EVENT_ADD(peer
, BGP_Start
);
302 } else if (addr
->family
== AF_INET6
) {
303 apply_mask_ipv6((struct prefix_ipv6
*)&p
);
305 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
308 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
311 rn
= bgp_node_get(bgp
->connected_table
[AFI_IP6
],
312 (struct prefix
*)&p
);
317 bc
= XCALLOC(MTYPE_BGP_CONN
,
318 sizeof(struct bgp_connected_ref
));
325 void bgp_connected_delete(struct bgp
*bgp
, struct connected
*ifc
)
329 struct bgp_node
*rn
= NULL
;
330 struct bgp_connected_ref
*bc
;
334 p
= *(CONNECTED_PREFIX(ifc
));
336 if (addr
->family
== AF_INET
) {
337 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
340 bgp_address_del(bgp
, addr
);
342 rn
= bgp_node_lookup(bgp
->connected_table
[AFI_IP
], &p
);
343 } else if (addr
->family
== AF_INET6
) {
344 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
347 if (IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
350 rn
= bgp_node_lookup(bgp
->connected_table
[AFI_IP6
],
351 (struct prefix
*)&p
);
359 if (bc
->refcnt
== 0) {
360 XFREE(MTYPE_BGP_CONN
, bc
);
367 static void bgp_connected_cleanup(struct route_table
*table
,
368 struct route_node
*rn
)
370 struct bgp_connected_ref
*bc
;
377 if (bc
->refcnt
== 0) {
378 XFREE(MTYPE_BGP_CONN
, bc
);
383 int bgp_nexthop_self(struct bgp
*bgp
, struct in_addr nh_addr
)
385 struct bgp_addr tmp
, *addr
;
386 struct tip_addr tmp_tip
, *tip
;
390 addr
= hash_lookup(bgp
->address_hash
, &tmp
);
394 tmp_tip
.addr
= nh_addr
;
395 tip
= hash_lookup(bgp
->tip_hash
, &tmp_tip
);
402 int bgp_multiaccess_check_v4(struct in_addr nexthop
, struct peer
*peer
)
404 struct bgp_node
*rn1
;
405 struct bgp_node
*rn2
;
410 p
.prefixlen
= IPV4_MAX_BITLEN
;
411 p
.u
.prefix4
= nexthop
;
413 rn1
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP
], &p
);
418 p
.prefixlen
= IPV4_MAX_BITLEN
;
419 p
.u
.prefix4
= peer
->su
.sin
.sin_addr
;
421 rn2
= bgp_node_match(peer
->bgp
->connected_table
[AFI_IP
], &p
);
423 bgp_unlock_node(rn1
);
427 ret
= (rn1
== rn2
) ? 1 : 0;
429 bgp_unlock_node(rn1
);
430 bgp_unlock_node(rn2
);
435 int bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop
,
436 struct update_subgroup
*subgrp
)
438 struct bgp_node
*rn1
, *rn2
;
444 np
.prefixlen
= IPV4_MAX_BITLEN
;
445 np
.u
.prefix4
= nexthop
;
448 p
.prefixlen
= IPV4_MAX_BITLEN
;
450 bgp
= SUBGRP_INST(subgrp
);
451 rn1
= bgp_node_match(bgp
->connected_table
[AFI_IP
], &np
);
455 SUBGRP_FOREACH_PEER (subgrp
, paf
) {
456 p
.u
.prefix4
= paf
->peer
->su
.sin
.sin_addr
;
458 rn2
= bgp_node_match(bgp
->connected_table
[AFI_IP
], &p
);
460 bgp_unlock_node(rn1
);
461 bgp_unlock_node(rn2
);
466 bgp_unlock_node(rn2
);
469 bgp_unlock_node(rn1
);
473 static void bgp_show_nexthops_detail(struct vty
*vty
, struct bgp
*bgp
,
474 struct bgp_nexthop_cache
*bnc
)
476 char buf
[PREFIX2STR_BUFFER
];
477 struct nexthop
*nexthop
;
479 for (nexthop
= bnc
->nexthop
; nexthop
; nexthop
= nexthop
->next
)
480 switch (nexthop
->type
) {
481 case NEXTHOP_TYPE_IPV6
:
482 vty_out(vty
, " gate %s\n",
483 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
486 case NEXTHOP_TYPE_IPV6_IFINDEX
:
487 vty_out(vty
, " gate %s, if %s\n",
488 inet_ntop(AF_INET6
, &nexthop
->gate
.ipv6
, buf
,
490 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
492 case NEXTHOP_TYPE_IPV4
:
493 vty_out(vty
, " gate %s\n",
494 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
497 case NEXTHOP_TYPE_IFINDEX
:
498 vty_out(vty
, " if %s\n",
499 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
501 case NEXTHOP_TYPE_IPV4_IFINDEX
:
502 vty_out(vty
, " gate %s, if %s\n",
503 inet_ntop(AF_INET
, &nexthop
->gate
.ipv4
, buf
,
505 ifindex2ifname(nexthop
->ifindex
, bgp
->vrf_id
));
507 case NEXTHOP_TYPE_BLACKHOLE
:
508 vty_out(vty
, " blackhole\n");
511 vty_out(vty
, " invalid nexthop type %u\n",
516 static void bgp_show_nexthops(struct vty
*vty
, struct bgp
*bgp
, int detail
)
519 struct bgp_nexthop_cache
*bnc
;
520 char buf
[PREFIX2STR_BUFFER
];
524 vty_out(vty
, "Current BGP nexthop cache:\n");
525 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
526 if (!bgp
->nexthop_cache_table
[afi
])
529 for (rn
= bgp_table_top(bgp
->nexthop_cache_table
[afi
]); rn
;
530 rn
= bgp_route_next(rn
)) {
531 if ((bnc
= rn
->info
) != NULL
) {
532 if (CHECK_FLAG(bnc
->flags
, BGP_NEXTHOP_VALID
)) {
534 " %s valid [IGP metric %d], #paths %d\n",
535 inet_ntop(rn
->p
.family
,
536 &rn
->p
.u
.prefix
, buf
,
538 bnc
->metric
, bnc
->path_count
);
543 bgp_show_nexthops_detail(vty
, bgp
, bnc
);
546 vty_out(vty
, " %s invalid\n",
547 inet_ntop(rn
->p
.family
,
548 &rn
->p
.u
.prefix
, buf
,
550 if (CHECK_FLAG(bnc
->flags
,
551 BGP_NEXTHOP_CONNECTED
))
553 " Must be Connected\n");
556 - (bgp_clock() - bnc
->last_update
);
557 vty_out(vty
, " Last update: %s", ctime(&tbuf
));
564 static int show_ip_bgp_nexthop_table(struct vty
*vty
, const char *name
,
570 bgp
= bgp_lookup_by_name(name
);
572 bgp
= bgp_get_default();
574 vty_out(vty
, "%% No such BGP instance exist\n");
578 bgp_show_nexthops(vty
, bgp
, detail
);
583 static void bgp_show_all_instances_nexthops_vty(struct vty
*vty
)
585 struct listnode
*node
, *nnode
;
588 for (ALL_LIST_ELEMENTS(bm
->bgp
, node
, nnode
, bgp
)) {
589 vty_out(vty
, "\nInstance %s:\n",
590 (bgp
->inst_type
== BGP_INSTANCE_TYPE_DEFAULT
)
593 bgp_show_nexthops(vty
, bgp
, 0);
597 DEFUN (show_ip_bgp_nexthop
,
598 show_ip_bgp_nexthop_cmd
,
599 "show [ip] bgp [<view|vrf> VIEWVRFNAME] nexthop [detail]",
603 BGP_INSTANCE_HELP_STR
604 "BGP nexthop table\n"
605 "Show detailed information\n")
610 if (argv_find(argv
, argc
, "view", &idx
)
611 || argv_find(argv
, argc
, "vrf", &idx
))
612 vrf
= argv
[++idx
]->arg
;
613 int detail
= argv_find(argv
, argc
, "detail", &idx
) ? 1 : 0;
614 return show_ip_bgp_nexthop_table(vty
, vrf
, detail
);
617 DEFUN (show_ip_bgp_instance_all_nexthop
,
618 show_ip_bgp_instance_all_nexthop_cmd
,
619 "show [ip] bgp <view|vrf> all nexthop",
623 BGP_INSTANCE_ALL_HELP_STR
624 "BGP nexthop table\n")
626 bgp_show_all_instances_nexthops_vty(vty
);
630 void bgp_scan_init(struct bgp
*bgp
)
634 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
635 bgp
->nexthop_cache_table
[afi
] =
636 bgp_table_init(bgp
, afi
, SAFI_UNICAST
);
637 bgp
->connected_table
[afi
] = bgp_table_init(bgp
, afi
,
639 bgp
->import_check_table
[afi
] =
640 bgp_table_init(bgp
, afi
, SAFI_UNICAST
);
644 void bgp_scan_vty_init(void)
646 install_element(VIEW_NODE
, &show_ip_bgp_nexthop_cmd
);
647 install_element(VIEW_NODE
, &show_ip_bgp_instance_all_nexthop_cmd
);
650 void bgp_scan_finish(struct bgp
*bgp
)
654 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
655 /* Only the current one needs to be reset. */
656 bgp_nexthop_cache_reset(bgp
->nexthop_cache_table
[afi
]);
657 bgp_table_unlock(bgp
->nexthop_cache_table
[afi
]);
658 bgp
->nexthop_cache_table
[afi
] = NULL
;
660 bgp
->connected_table
[afi
]->route_table
->cleanup
=
661 bgp_connected_cleanup
;
662 bgp_table_unlock(bgp
->connected_table
[afi
]);
663 bgp
->connected_table
[afi
] = NULL
;
665 bgp_table_unlock(bgp
->import_check_table
[afi
]);
666 bgp
->import_check_table
[afi
] = NULL
;