]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_nexthop.c
Merge pull request #7953 from mjstapp/fix_more_ntoa
[mirror_frr.git] / bgpd / bgp_nexthop.c
CommitLineData
718e3744 1/* BGP nexthop scan
896014f4
DL
2 * Copyright (C) 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
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
9 * later version.
10 *
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.
15 *
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
19 */
718e3744 20
21#include <zebra.h>
22
23#include "command.h"
24#include "thread.h"
25#include "prefix.h"
26#include "zclient.h"
27#include "stream.h"
28#include "network.h"
29#include "log.h"
30#include "memory.h"
10f9bf3f
JBD
31#include "hash.h"
32#include "jhash.h"
fb018d25 33#include "nexthop.h"
3f9c7369 34#include "queue.h"
039f3a34 35#include "filter.h"
718e3744 36
37#include "bgpd/bgpd.h"
718e3744 38#include "bgpd/bgp_route.h"
39#include "bgpd/bgp_attr.h"
40#include "bgpd/bgp_nexthop.h"
fb018d25 41#include "bgpd/bgp_nht.h"
718e3744 42#include "bgpd/bgp_debug.h"
43#include "bgpd/bgp_damp.h"
8ffedcea 44#include "bgpd/bgp_fsm.h"
8386ac43 45#include "bgpd/bgp_vty.h"
5d76a53d 46#include "bgpd/bgp_rd.h"
718e3744 47
2a99175f 48DEFINE_MTYPE_STATIC(BGPD, MARTIAN_STRING, "BGP Martian Addr Intf String");
330cec3d 49
f663c581
RW
50int bgp_nexthop_cache_compare(const struct bgp_nexthop_cache *a,
51 const struct bgp_nexthop_cache *b)
fb018d25 52{
545aeef1
RW
53 if (a->srte_color < b->srte_color)
54 return -1;
55 if (a->srte_color > b->srte_color)
56 return 1;
57
f663c581
RW
58 return prefix_cmp(&a->prefix, &b->prefix);
59}
60
61const char *bnc_str(struct bgp_nexthop_cache *bnc, char *buf, int size)
62{
63 return prefix2str(&bnc->prefix, buf, size);
fb018d25
DS
64}
65
d62a17ae 66void bnc_nexthop_free(struct bgp_nexthop_cache *bnc)
718e3744 67{
d62a17ae 68 nexthops_free(bnc->nexthop);
718e3744 69}
70
f663c581 71struct bgp_nexthop_cache *bnc_new(struct bgp_nexthop_cache_head *tree,
545aeef1 72 struct prefix *prefix, uint32_t srte_color)
718e3744 73{
d62a17ae 74 struct bgp_nexthop_cache *bnc;
fb018d25 75
d62a17ae 76 bnc = XCALLOC(MTYPE_BGP_NEXTHOP_CACHE,
77 sizeof(struct bgp_nexthop_cache));
f663c581 78 bnc->prefix = *prefix;
545aeef1 79 bnc->srte_color = srte_color;
f663c581 80 bnc->tree = tree;
d62a17ae 81 LIST_INIT(&(bnc->paths));
f663c581
RW
82 bgp_nexthop_cache_add(tree, bnc);
83
d62a17ae 84 return bnc;
718e3744 85}
86
e37e1e27
PR
87bool bnc_existing_for_prefix(struct bgp_nexthop_cache *bnc)
88{
89 struct bgp_nexthop_cache *bnc_tmp;
90
91 frr_each (bgp_nexthop_cache, bnc->tree, bnc_tmp) {
92 if (bnc_tmp == bnc)
93 continue;
94 if (prefix_cmp(&bnc->prefix, &bnc_tmp->prefix) == 0)
95 return true;
96 }
97 return false;
98}
99
d62a17ae 100void bnc_free(struct bgp_nexthop_cache *bnc)
718e3744 101{
d62a17ae 102 bnc_nexthop_free(bnc);
f663c581 103 bgp_nexthop_cache_del(bnc->tree, bnc);
d62a17ae 104 XFREE(MTYPE_BGP_NEXTHOP_CACHE, bnc);
718e3744 105}
6b0655a2 106
f663c581 107struct bgp_nexthop_cache *bnc_find(struct bgp_nexthop_cache_head *tree,
545aeef1 108 struct prefix *prefix, uint32_t srte_color)
f663c581
RW
109{
110 struct bgp_nexthop_cache bnc = {};
111
112 if (!tree)
113 return NULL;
114
115 bnc.prefix = *prefix;
545aeef1 116 bnc.srte_color = srte_color;
f663c581
RW
117 return bgp_nexthop_cache_find(tree, &bnc);
118}
119
718e3744 120/* Reset and free all BGP nexthop cache. */
f663c581 121static void bgp_nexthop_cache_reset(struct bgp_nexthop_cache_head *tree)
718e3744 122{
d62a17ae 123 struct bgp_nexthop_cache *bnc;
124
f663c581
RW
125 while (bgp_nexthop_cache_count(tree) > 0) {
126 bnc = bgp_nexthop_cache_first(tree);
7f040da1 127
3d111939
DS
128 while (!LIST_EMPTY(&(bnc->paths))) {
129 struct bgp_path_info *path = LIST_FIRST(&(bnc->paths));
7f040da1 130
3d111939 131 path_nh_map(path, bnc, false);
d62a17ae 132 }
3d111939
DS
133
134 bnc_free(bnc);
14315f2d 135 }
718e3744 136}
137
db0e1937
MK
138static void *bgp_tip_hash_alloc(void *p)
139{
0291c246
MK
140 const struct in_addr *val = (const struct in_addr *)p;
141 struct tip_addr *addr;
db0e1937
MK
142
143 addr = XMALLOC(MTYPE_TIP_ADDR, sizeof(struct tip_addr));
144 addr->refcnt = 0;
145 addr->addr.s_addr = val->s_addr;
146
147 return addr;
148}
149
150static void bgp_tip_hash_free(void *addr)
151{
152 XFREE(MTYPE_TIP_ADDR, addr);
153}
154
d8b87afe 155static unsigned int bgp_tip_hash_key_make(const void *p)
db0e1937 156{
0291c246 157 const struct tip_addr *addr = p;
db0e1937
MK
158
159 return jhash_1word(addr->addr.s_addr, 0);
160}
161
74df8d6d 162static bool bgp_tip_hash_cmp(const void *p1, const void *p2)
db0e1937 163{
0291c246
MK
164 const struct tip_addr *addr1 = p1;
165 const struct tip_addr *addr2 = p2;
db0e1937
MK
166
167 return addr1->addr.s_addr == addr2->addr.s_addr;
168}
169
170void bgp_tip_hash_init(struct bgp *bgp)
171{
996c9314 172 bgp->tip_hash = hash_create(bgp_tip_hash_key_make, bgp_tip_hash_cmp,
3f65c5b1 173 "BGP TIP hash");
db0e1937
MK
174}
175
176void bgp_tip_hash_destroy(struct bgp *bgp)
177{
178 if (bgp->tip_hash == NULL)
179 return;
180 hash_clean(bgp->tip_hash, bgp_tip_hash_free);
181 hash_free(bgp->tip_hash);
182 bgp->tip_hash = NULL;
183}
184
185void bgp_tip_add(struct bgp *bgp, struct in_addr *tip)
186{
0291c246
MK
187 struct tip_addr tmp;
188 struct tip_addr *addr;
db0e1937
MK
189
190 tmp.addr = *tip;
191
192 addr = hash_get(bgp->tip_hash, &tmp, bgp_tip_hash_alloc);
193 if (!addr)
194 return;
195
196 addr->refcnt++;
197}
198
199void bgp_tip_del(struct bgp *bgp, struct in_addr *tip)
200{
0291c246
MK
201 struct tip_addr tmp;
202 struct tip_addr *addr;
db0e1937
MK
203
204 tmp.addr = *tip;
205
206 addr = hash_lookup(bgp->tip_hash, &tmp);
207 /* may have been deleted earlier by bgp_interface_down() */
208 if (addr == NULL)
209 return;
210
211 addr->refcnt--;
212
213 if (addr->refcnt == 0) {
214 hash_release(bgp->tip_hash, addr);
215 XFREE(MTYPE_TIP_ADDR, addr);
216 }
217}
10f9bf3f 218
af97a18b
DS
219/* BGP own address structure */
220struct bgp_addr {
2ec802d1 221 struct prefix p;
f4c2fb93 222 struct list *ifp_name_list;
af97a18b
DS
223};
224
e3b78da8 225static void show_address_entry(struct hash_bucket *bucket, void *args)
af97a18b
DS
226{
227 struct vty *vty = (struct vty *)args;
e3b78da8 228 struct bgp_addr *addr = (struct bgp_addr *)bucket->data;
f4c2fb93
DS
229 char *name;
230 struct listnode *node;
949b0f24 231 char str[INET6_ADDRSTRLEN] = {0};
232
e61f7c0a 233 vty_out(vty, "addr: %s, count: %d : ",
2ec802d1 234 inet_ntop(addr->p.family, &(addr->p.u.prefix),
e61f7c0a
DS
235 str, INET6_ADDRSTRLEN),
236 addr->ifp_name_list->count);
f4c2fb93
DS
237
238 for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
239 vty_out(vty, " %s,", name);
240 }
241
242 vty_out(vty, "\n");
af97a18b
DS
243}
244
245void bgp_nexthop_show_address_hash(struct vty *vty, struct bgp *bgp)
246{
247 hash_iterate(bgp->address_hash,
e3b78da8 248 (void (*)(struct hash_bucket *, void *))show_address_entry,
af97a18b
DS
249 vty);
250}
251
f4c2fb93
DS
252static void bgp_address_hash_string_del(void *val)
253{
254 char *data = val;
255
330cec3d 256 XFREE(MTYPE_MARTIAN_STRING, data);
f4c2fb93
DS
257}
258
d62a17ae 259static void *bgp_address_hash_alloc(void *p)
10f9bf3f 260{
949b0f24 261 struct bgp_addr *copy_addr = p;
262 struct bgp_addr *addr = NULL;
10f9bf3f 263
d62a17ae 264 addr = XMALLOC(MTYPE_BGP_ADDR, sizeof(struct bgp_addr));
2ec802d1 265 prefix_copy(&addr->p, &copy_addr->p);
10f9bf3f 266
f4c2fb93
DS
267 addr->ifp_name_list = list_new();
268 addr->ifp_name_list->del = bgp_address_hash_string_del;
269
d62a17ae 270 return addr;
10f9bf3f
JBD
271}
272
f4c2fb93 273static void bgp_address_hash_free(void *data)
37d361e7 274{
f4c2fb93
DS
275 struct bgp_addr *addr = data;
276
6a154c88 277 list_delete(&addr->ifp_name_list);
d62a17ae 278 XFREE(MTYPE_BGP_ADDR, addr);
37d361e7
RW
279}
280
d8b87afe 281static unsigned int bgp_address_hash_key_make(const void *p)
10f9bf3f 282{
d62a17ae 283 const struct bgp_addr *addr = p;
10f9bf3f 284
2ec802d1 285 return prefix_hash_key(&addr->p);
10f9bf3f
JBD
286}
287
74df8d6d 288static bool bgp_address_hash_cmp(const void *p1, const void *p2)
10f9bf3f 289{
d62a17ae 290 const struct bgp_addr *addr1 = p1;
291 const struct bgp_addr *addr2 = p2;
10f9bf3f 292
2ec802d1 293 return prefix_same(&addr1->p, &addr2->p);
10f9bf3f
JBD
294}
295
d62a17ae 296void bgp_address_init(struct bgp *bgp)
10f9bf3f 297{
996c9314
LB
298 bgp->address_hash =
299 hash_create(bgp_address_hash_key_make, bgp_address_hash_cmp,
949b0f24 300 "BGP Connected Address Hash");
10f9bf3f
JBD
301}
302
d62a17ae 303void bgp_address_destroy(struct bgp *bgp)
bb86c601 304{
d62a17ae 305 if (bgp->address_hash == NULL)
306 return;
307 hash_clean(bgp->address_hash, bgp_address_hash_free);
308 hash_free(bgp->address_hash);
309 bgp->address_hash = NULL;
bb86c601
LB
310}
311
f4c2fb93
DS
312static void bgp_address_add(struct bgp *bgp, struct connected *ifc,
313 struct prefix *p)
10f9bf3f 314{
d62a17ae 315 struct bgp_addr tmp;
316 struct bgp_addr *addr;
f4c2fb93
DS
317 struct listnode *node;
318 char *name;
10f9bf3f 319
2ec802d1 320 tmp.p = *p;
949b0f24 321
2ec802d1
DS
322 if (tmp.p.family == AF_INET)
323 tmp.p.prefixlen = IPV4_MAX_BITLEN;
324 else if (tmp.p.family == AF_INET6)
325 tmp.p.prefixlen = IPV6_MAX_BITLEN;
10f9bf3f 326
d62a17ae 327 addr = hash_get(bgp->address_hash, &tmp, bgp_address_hash_alloc);
5ce10e92 328
f4c2fb93
DS
329 for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
330 if (strcmp(ifc->ifp->name, name) == 0)
331 break;
332 }
333 if (!node) {
330cec3d 334 name = XSTRDUP(MTYPE_MARTIAN_STRING, ifc->ifp->name);
f4c2fb93
DS
335 listnode_add(addr->ifp_name_list, name);
336 }
10f9bf3f
JBD
337}
338
f4c2fb93
DS
339static void bgp_address_del(struct bgp *bgp, struct connected *ifc,
340 struct prefix *p)
10f9bf3f 341{
d62a17ae 342 struct bgp_addr tmp;
343 struct bgp_addr *addr;
f4c2fb93
DS
344 struct listnode *node;
345 char *name;
10f9bf3f 346
2ec802d1 347 tmp.p = *p;
949b0f24 348
2ec802d1
DS
349 if (tmp.p.family == AF_INET)
350 tmp.p.prefixlen = IPV4_MAX_BITLEN;
351 else if (tmp.p.family == AF_INET6)
352 tmp.p.prefixlen = IPV6_MAX_BITLEN;
10f9bf3f 353
d62a17ae 354 addr = hash_lookup(bgp->address_hash, &tmp);
355 /* may have been deleted earlier by bgp_interface_down() */
356 if (addr == NULL)
357 return;
9e47abd8 358
f4c2fb93
DS
359 for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
360 if (strcmp(ifc->ifp->name, name) == 0)
361 break;
362 }
10f9bf3f 363
b9129915 364 if (node) {
f4c2fb93 365 list_delete_node(addr->ifp_name_list, node);
b9129915
DS
366 XFREE(MTYPE_MARTIAN_STRING, name);
367 }
f4c2fb93
DS
368
369 if (addr->ifp_name_list->count == 0) {
d62a17ae 370 hash_release(bgp->address_hash, addr);
6a154c88 371 list_delete(&addr->ifp_name_list);
d62a17ae 372 XFREE(MTYPE_BGP_ADDR, addr);
373 }
10f9bf3f
JBD
374}
375
6b0655a2 376
d62a17ae 377struct bgp_connected_ref {
378 unsigned int refcnt;
718e3744 379};
380
d62a17ae 381void bgp_connected_add(struct bgp *bgp, struct connected *ifc)
718e3744 382{
d62a17ae 383 struct prefix p;
384 struct prefix *addr;
9bcb3eef 385 struct bgp_dest *dest;
d62a17ae 386 struct bgp_connected_ref *bc;
387 struct listnode *node, *nnode;
388 struct peer *peer;
389
390 addr = ifc->address;
391
392 p = *(CONNECTED_PREFIX(ifc));
393 if (addr->family == AF_INET) {
394 apply_mask_ipv4((struct prefix_ipv4 *)&p);
395
396 if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
397 return;
398
f4c2fb93 399 bgp_address_add(bgp, ifc, addr);
d62a17ae 400
9bcb3eef
DS
401 dest = bgp_node_get(bgp->connected_table[AFI_IP],
402 (struct prefix *)&p);
403 bc = bgp_dest_get_bgp_connected_ref_info(dest);
3d9dbdbe 404 if (bc)
d62a17ae 405 bc->refcnt++;
3d9dbdbe 406 else {
d62a17ae 407 bc = XCALLOC(MTYPE_BGP_CONN,
408 sizeof(struct bgp_connected_ref));
409 bc->refcnt = 1;
9bcb3eef 410 bgp_dest_set_bgp_connected_ref_info(dest, bc);
d62a17ae 411 }
8ffedcea 412
d62a17ae 413 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
414 if (peer->conf_if
415 && (strcmp(peer->conf_if, ifc->ifp->name) == 0)
416 && peer->status != Established
417 && !CHECK_FLAG(peer->flags,
418 PEER_FLAG_IFPEER_V6ONLY)) {
419 if (peer_active(peer))
420 BGP_EVENT_ADD(peer, BGP_Stop);
421 BGP_EVENT_ADD(peer, BGP_Start);
422 }
423 }
424 } else if (addr->family == AF_INET6) {
425 apply_mask_ipv6((struct prefix_ipv6 *)&p);
426
427 if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
428 return;
429
430 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
431 return;
432
949b0f24 433 bgp_address_add(bgp, ifc, addr);
434
9bcb3eef
DS
435 dest = bgp_node_get(bgp->connected_table[AFI_IP6],
436 (struct prefix *)&p);
3d9dbdbe 437
9bcb3eef 438 bc = bgp_dest_get_bgp_connected_ref_info(dest);
3d9dbdbe 439 if (bc)
d62a17ae 440 bc->refcnt++;
3d9dbdbe 441 else {
d62a17ae 442 bc = XCALLOC(MTYPE_BGP_CONN,
443 sizeof(struct bgp_connected_ref));
444 bc->refcnt = 1;
9bcb3eef 445 bgp_dest_set_bgp_connected_ref_info(dest, bc);
d62a17ae 446 }
718e3744 447 }
718e3744 448}
449
d62a17ae 450void bgp_connected_delete(struct bgp *bgp, struct connected *ifc)
718e3744 451{
d62a17ae 452 struct prefix p;
453 struct prefix *addr;
9bcb3eef 454 struct bgp_dest *dest = NULL;
d62a17ae 455 struct bgp_connected_ref *bc;
718e3744 456
d62a17ae 457 addr = ifc->address;
718e3744 458
d62a17ae 459 p = *(CONNECTED_PREFIX(ifc));
f6bdc080 460 apply_mask(&p);
d62a17ae 461 if (addr->family == AF_INET) {
d62a17ae 462 if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
463 return;
718e3744 464
f4c2fb93 465 bgp_address_del(bgp, ifc, addr);
10f9bf3f 466
9bcb3eef 467 dest = bgp_node_lookup(bgp->connected_table[AFI_IP], &p);
d62a17ae 468 } else if (addr->family == AF_INET6) {
d62a17ae 469 if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
470 return;
471
472 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
473 return;
474
949b0f24 475 bgp_address_del(bgp, ifc, addr);
476
9bcb3eef 477 dest = bgp_node_lookup(bgp->connected_table[AFI_IP6], &p);
f6bdc080 478 }
d62a17ae 479
9bcb3eef 480 if (!dest)
f6bdc080
DS
481 return;
482
9bcb3eef 483 bc = bgp_dest_get_bgp_connected_ref_info(dest);
f6bdc080
DS
484 bc->refcnt--;
485 if (bc->refcnt == 0) {
486 XFREE(MTYPE_BGP_CONN, bc);
9bcb3eef 487 bgp_dest_set_bgp_connected_ref_info(dest, NULL);
718e3744 488 }
9bcb3eef
DS
489 bgp_dest_unlock_node(dest);
490 bgp_dest_unlock_node(dest);
718e3744 491}
492
3292693b
DS
493static void bgp_connected_cleanup(struct route_table *table,
494 struct route_node *rn)
495{
496 struct bgp_connected_ref *bc;
9bcb3eef 497 struct bgp_dest *bn = bgp_dest_from_rnode(rn);
3292693b 498
9bcb3eef 499 bc = bgp_dest_get_bgp_connected_ref_info(bn);
3292693b
DS
500 if (!bc)
501 return;
502
503 bc->refcnt--;
504 if (bc->refcnt == 0) {
505 XFREE(MTYPE_BGP_CONN, bc);
9bcb3eef 506 bgp_dest_set_bgp_connected_ref_info(bn, NULL);
3292693b
DS
507 }
508}
509
3dc339cd 510bool bgp_nexthop_self(struct bgp *bgp, afi_t afi, uint8_t type,
9bcb3eef
DS
511 uint8_t sub_type, struct attr *attr,
512 struct bgp_dest *dest)
718e3744 513{
af34d2da 514 uint8_t new_afi = afi == AFI_IP ? AF_INET : AF_INET6;
8c5c49ac 515 struct bgp_addr tmp_addr = {{0}}, *addr = NULL;
949b0f24 516 struct tip_addr tmp_tip, *tip = NULL;
9bcb3eef 517 const struct prefix *p = bgp_dest_get_prefix(dest);
c4fb2504
DS
518 bool is_bgp_static_route =
519 ((type == ZEBRA_ROUTE_BGP) && (sub_type == BGP_ROUTE_STATIC))
949b0f24 520 ? true
521 : false;
522
523 if (!is_bgp_static_route)
af34d2da 524 new_afi = BGP_ATTR_NEXTHOP_AFI_IP6(attr) ? AF_INET6 : AF_INET;
949b0f24 525
e26c3055 526 tmp_addr.p.family = new_afi;
949b0f24 527 switch (new_afi) {
af34d2da 528 case AF_INET:
949b0f24 529 if (is_bgp_static_route) {
b54892e0
DS
530 tmp_addr.p.u.prefix4 = p->u.prefix4;
531 tmp_addr.p.prefixlen = p->prefixlen;
949b0f24 532 } else {
533 /* Here we need to find out which nexthop to be used*/
c4fb2504 534 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP)) {
e26c3055
DS
535 tmp_addr.p.u.prefix4 = attr->nexthop;
536 tmp_addr.p.prefixlen = IPV4_MAX_BITLEN;
c4fb2504
DS
537 } else if ((attr->mp_nexthop_len)
538 && ((attr->mp_nexthop_len
539 == BGP_ATTR_NHLEN_IPV4)
540 || (attr->mp_nexthop_len
541 == BGP_ATTR_NHLEN_VPNV4))) {
e26c3055 542 tmp_addr.p.u.prefix4 =
949b0f24 543 attr->mp_nexthop_global_in;
e26c3055 544 tmp_addr.p.prefixlen = IPV4_MAX_BITLEN;
949b0f24 545 } else
3dc339cd 546 return false;
949b0f24 547 }
548 break;
af34d2da 549 case AF_INET6:
949b0f24 550 if (is_bgp_static_route) {
b54892e0
DS
551 tmp_addr.p.u.prefix6 = p->u.prefix6;
552 tmp_addr.p.prefixlen = p->prefixlen;
949b0f24 553 } else {
e26c3055
DS
554 tmp_addr.p.u.prefix6 = attr->mp_nexthop_global;
555 tmp_addr.p.prefixlen = IPV6_MAX_BITLEN;
949b0f24 556 }
557 break;
558 default:
559 break;
560 }
10f9bf3f 561
949b0f24 562 addr = hash_lookup(bgp->address_hash, &tmp_addr);
d62a17ae 563 if (addr)
3dc339cd 564 return true;
718e3744 565
3584c85e 566 if (new_afi == AF_INET && hashcount(bgp->tip_hash)) {
949b0f24 567 memset(&tmp_tip, 0, sizeof(struct tip_addr));
568 tmp_tip.addr = attr->nexthop;
569
570 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP)) {
571 tmp_tip.addr = attr->nexthop;
572 } else if ((attr->mp_nexthop_len) &&
2ec802d1
DS
573 ((attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV4)
574 || (attr->mp_nexthop_len == BGP_ATTR_NHLEN_VPNV4))) {
949b0f24 575 tmp_tip.addr = attr->mp_nexthop_global_in;
576 }
577
578 tip = hash_lookup(bgp->tip_hash, &tmp_tip);
579 if (tip)
3dc339cd 580 return true;
949b0f24 581 }
db0e1937 582
3dc339cd 583 return false;
718e3744 584}
6b0655a2 585
3dc339cd 586bool bgp_multiaccess_check_v4(struct in_addr nexthop, struct peer *peer)
718e3744 587{
9bcb3eef
DS
588 struct bgp_dest *dest1;
589 struct bgp_dest *dest2;
d62a17ae 590 struct prefix p;
591 int ret;
592
593 p.family = AF_INET;
594 p.prefixlen = IPV4_MAX_BITLEN;
595 p.u.prefix4 = nexthop;
596
9bcb3eef
DS
597 dest1 = bgp_node_match(peer->bgp->connected_table[AFI_IP], &p);
598 if (!dest1)
3dc339cd 599 return false;
d62a17ae 600
601 p.family = AF_INET;
602 p.prefixlen = IPV4_MAX_BITLEN;
603 p.u.prefix4 = peer->su.sin.sin_addr;
604
9bcb3eef
DS
605 dest2 = bgp_node_match(peer->bgp->connected_table[AFI_IP], &p);
606 if (!dest2) {
607 bgp_dest_unlock_node(dest1);
3dc339cd 608 return false;
d62a17ae 609 }
718e3744 610
9bcb3eef 611 ret = (dest1 == dest2);
718e3744 612
9bcb3eef
DS
613 bgp_dest_unlock_node(dest1);
614 bgp_dest_unlock_node(dest2);
718e3744 615
3dc339cd 616 return ret;
718e3744 617}
618
3dc339cd 619bool bgp_multiaccess_check_v6(struct in6_addr nexthop, struct peer *peer)
737af885 620{
9bcb3eef
DS
621 struct bgp_dest *dest1;
622 struct bgp_dest *dest2;
737af885
BS
623 struct prefix p;
624 int ret;
625
626 p.family = AF_INET6;
627 p.prefixlen = IPV6_MAX_BITLEN;
628 p.u.prefix6 = nexthop;
629
9bcb3eef
DS
630 dest1 = bgp_node_match(peer->bgp->connected_table[AFI_IP6], &p);
631 if (!dest1)
3dc339cd 632 return false;
737af885
BS
633
634 p.family = AF_INET6;
635 p.prefixlen = IPV6_MAX_BITLEN;
636 p.u.prefix6 = peer->su.sin6.sin6_addr;
637
9bcb3eef
DS
638 dest2 = bgp_node_match(peer->bgp->connected_table[AFI_IP6], &p);
639 if (!dest2) {
640 bgp_dest_unlock_node(dest1);
3dc339cd 641 return false;
737af885
BS
642 }
643
9bcb3eef 644 ret = (dest1 == dest2);
737af885 645
9bcb3eef
DS
646 bgp_dest_unlock_node(dest1);
647 bgp_dest_unlock_node(dest2);
737af885
BS
648
649 return ret;
650}
651
3dc339cd
DA
652bool bgp_subgrp_multiaccess_check_v6(struct in6_addr nexthop,
653 struct update_subgroup *subgrp,
654 struct peer *exclude)
737af885 655{
9bcb3eef 656 struct bgp_dest *dest1 = NULL, *dest2 = NULL;
737af885
BS
657 struct peer_af *paf = NULL;
658 struct prefix p = {0}, np = {0};
659 struct bgp *bgp = NULL;
660
661 np.family = AF_INET6;
662 np.prefixlen = IPV6_MAX_BITLEN;
663 np.u.prefix6 = nexthop;
664
665 p.family = AF_INET;
666 p.prefixlen = IPV6_MAX_BITLEN;
667
668 bgp = SUBGRP_INST(subgrp);
9bcb3eef
DS
669 dest1 = bgp_node_match(bgp->connected_table[AFI_IP6], &np);
670 if (!dest1)
3dc339cd 671 return false;
737af885
BS
672
673 SUBGRP_FOREACH_PEER (subgrp, paf) {
a3b72539 674 /* Skip peer we're told to exclude - e.g., source of route. */
675 if (paf->peer == exclude)
676 continue;
737af885
BS
677
678 p.u.prefix6 = paf->peer->su.sin6.sin6_addr;
9bcb3eef
DS
679 dest2 = bgp_node_match(bgp->connected_table[AFI_IP6], &p);
680 if (dest1 == dest2) {
681 bgp_dest_unlock_node(dest1);
682 bgp_dest_unlock_node(dest2);
3dc339cd 683 return true;
737af885
BS
684 }
685
9bcb3eef
DS
686 if (dest2)
687 bgp_dest_unlock_node(dest2);
737af885
BS
688 }
689
9bcb3eef 690 bgp_dest_unlock_node(dest1);
3dc339cd 691 return false;
737af885
BS
692}
693
3dc339cd
DA
694bool bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop,
695 struct update_subgroup *subgrp,
696 struct peer *exclude)
65d4e0c6 697{
9bcb3eef 698 struct bgp_dest *dest1, *dest2;
65d4e0c6
DS
699 struct peer_af *paf;
700 struct prefix p, np;
a2b6e694 701 struct bgp *bgp;
65d4e0c6
DS
702
703 np.family = AF_INET;
704 np.prefixlen = IPV4_MAX_BITLEN;
705 np.u.prefix4 = nexthop;
706
707 p.family = AF_INET;
708 p.prefixlen = IPV4_MAX_BITLEN;
709
65d4e0c6 710 bgp = SUBGRP_INST(subgrp);
9bcb3eef
DS
711 dest1 = bgp_node_match(bgp->connected_table[AFI_IP], &np);
712 if (!dest1)
3dc339cd 713 return false;
65d4e0c6 714
996c9314 715 SUBGRP_FOREACH_PEER (subgrp, paf) {
a3b72539 716 /* Skip peer we're told to exclude - e.g., source of route. */
717 if (paf->peer == exclude)
718 continue;
719
65d4e0c6
DS
720 p.u.prefix4 = paf->peer->su.sin.sin_addr;
721
9bcb3eef
DS
722 dest2 = bgp_node_match(bgp->connected_table[AFI_IP], &p);
723 if (dest1 == dest2) {
724 bgp_dest_unlock_node(dest1);
725 bgp_dest_unlock_node(dest2);
3dc339cd 726 return true;
65d4e0c6
DS
727 }
728
9bcb3eef
DS
729 if (dest2)
730 bgp_dest_unlock_node(dest2);
65d4e0c6
DS
731 }
732
9bcb3eef 733 bgp_dest_unlock_node(dest1);
3dc339cd 734 return false;
65d4e0c6
DS
735}
736
5d76a53d 737static void bgp_show_nexthop_paths(struct vty *vty, struct bgp *bgp,
738 struct bgp_nexthop_cache *bnc)
739{
9bcb3eef 740 struct bgp_dest *dest;
5d76a53d 741 struct bgp_path_info *path;
742 int afi;
743 safi_t safi;
744 struct bgp_table *table;
745 struct bgp *bgp_path;
746 char buf1[BUFSIZ];
747
748 vty_out(vty, " Paths:\n");
749 LIST_FOREACH (path, &(bnc->paths), nh_thread) {
9bcb3eef
DS
750 dest = path->net;
751 assert(dest && bgp_dest_table(dest));
752 afi = family2afi(bgp_dest_get_prefix(dest)->family);
753 table = bgp_dest_table(dest);
5d76a53d 754 safi = table->safi;
755 bgp_path = table->bgp;
756
9bcb3eef
DS
757 if (dest->pdest) {
758 prefix_rd2str((struct prefix_rd *)bgp_dest_get_prefix(dest->pdest),
5d76a53d 759 buf1, sizeof(buf1));
56ca3b5b 760 vty_out(vty, " %d/%d %pBD RD %s %s flags 0x%x\n",
9bcb3eef 761 afi, safi, dest, buf1, bgp_path->name_pretty, path->flags);
5d76a53d 762 } else
56ca3b5b 763 vty_out(vty, " %d/%d %pBD %s flags 0x%x\n",
9bcb3eef 764 afi, safi, dest, bgp_path->name_pretty, path->flags);
5d76a53d 765 }
766}
767
996c9314 768static void bgp_show_nexthops_detail(struct vty *vty, struct bgp *bgp,
e22ac3ee
DS
769 struct bgp_nexthop_cache *bnc)
770{
771 char buf[PREFIX2STR_BUFFER];
772 struct nexthop *nexthop;
773
5d76a53d 774 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next) {
e22ac3ee
DS
775 switch (nexthop->type) {
776 case NEXTHOP_TYPE_IPV6:
777 vty_out(vty, " gate %s\n",
996c9314
LB
778 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
779 sizeof(buf)));
e22ac3ee
DS
780 break;
781 case NEXTHOP_TYPE_IPV6_IFINDEX:
782 vty_out(vty, " gate %s, if %s\n",
996c9314
LB
783 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
784 sizeof(buf)),
785 ifindex2ifname(nexthop->ifindex, bgp->vrf_id));
e22ac3ee
DS
786 break;
787 case NEXTHOP_TYPE_IPV4:
788 vty_out(vty, " gate %s\n",
996c9314
LB
789 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf,
790 sizeof(buf)));
e22ac3ee
DS
791 break;
792 case NEXTHOP_TYPE_IFINDEX:
793 vty_out(vty, " if %s\n",
996c9314 794 ifindex2ifname(nexthop->ifindex, bgp->vrf_id));
e22ac3ee
DS
795 break;
796 case NEXTHOP_TYPE_IPV4_IFINDEX:
797 vty_out(vty, " gate %s, if %s\n",
996c9314
LB
798 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf,
799 sizeof(buf)),
800 ifindex2ifname(nexthop->ifindex, bgp->vrf_id));
e22ac3ee
DS
801 break;
802 case NEXTHOP_TYPE_BLACKHOLE:
803 vty_out(vty, " blackhole\n");
804 break;
805 default:
996c9314 806 vty_out(vty, " invalid nexthop type %u\n",
e22ac3ee
DS
807 nexthop->type);
808 }
5d76a53d 809 }
e22ac3ee
DS
810}
811
5d76a53d 812static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp,
5d76a53d 813 struct bgp_nexthop_cache *bnc,
814 bool specific)
fb018d25 815{
d62a17ae 816 char buf[PREFIX2STR_BUFFER];
d62a17ae 817 time_t tbuf;
5d76a53d 818 struct peer *peer;
5d76a53d 819
820 peer = (struct peer *)bnc->nht_info;
821
545aeef1
RW
822 if (bnc->srte_color)
823 vty_out(vty, " SR-TE color %u -", bnc->srte_color);
5d76a53d 824 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) {
825 vty_out(vty, " %s valid [IGP metric %d], #paths %d",
f663c581
RW
826 inet_ntop(bnc->prefix.family, &bnc->prefix.u.prefix,
827 buf, sizeof(buf)),
5d76a53d 828 bnc->metric, bnc->path_count);
829 if (peer)
830 vty_out(vty, ", peer %s", peer->host);
831 vty_out(vty, "\n");
832 bgp_show_nexthops_detail(vty, bgp, bnc);
833 } else {
834 vty_out(vty, " %s invalid, #paths %d",
f663c581
RW
835 inet_ntop(bnc->prefix.family, &bnc->prefix.u.prefix,
836 buf, sizeof(buf)),
5d76a53d 837 bnc->path_count);
838 if (peer)
839 vty_out(vty, ", peer %s", peer->host);
840 vty_out(vty, "\n");
841 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED))
842 vty_out(vty, " Must be Connected\n");
843 if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED))
844 vty_out(vty, " Is not Registered\n");
845 }
846 tbuf = time(NULL) - (bgp_clock() - bnc->last_update);
847 vty_out(vty, " Last update: %s", ctime(&tbuf));
848 vty_out(vty, "\n");
849
850 /* show paths dependent on nexthop, if needed. */
851 if (specific)
852 bgp_show_nexthop_paths(vty, bgp, bnc);
853}
854
855static void bgp_show_nexthops(struct vty *vty, struct bgp *bgp,
856 bool import_table)
857{
5d76a53d 858 struct bgp_nexthop_cache *bnc;
d62a17ae 859 afi_t afi;
f663c581 860 struct bgp_nexthop_cache_head(*tree)[AFI_MAX];
d62a17ae 861
05e47722
PG
862 if (import_table)
863 vty_out(vty, "Current BGP import check cache:\n");
864 else
865 vty_out(vty, "Current BGP nexthop cache:\n");
866 if (import_table)
f663c581 867 tree = &bgp->import_check_table;
05e47722 868 else
f663c581 869 tree = &bgp->nexthop_cache_table;
d62a17ae 870 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
f663c581
RW
871 frr_each (bgp_nexthop_cache, &(*tree)[afi], bnc)
872 bgp_show_nexthop(vty, bgp, bnc, false);
fb018d25 873 }
f186de26 874}
875
d62a17ae 876static int show_ip_bgp_nexthop_table(struct vty *vty, const char *name,
5d76a53d 877 const char *nhopip_str,
878 bool import_table)
f186de26 879{
d62a17ae 880 struct bgp *bgp;
881
882 if (name)
883 bgp = bgp_lookup_by_name(name);
884 else
885 bgp = bgp_get_default();
886 if (!bgp) {
887 vty_out(vty, "%% No such BGP instance exist\n");
888 return CMD_WARNING;
889 }
f186de26 890
5d76a53d 891 if (nhopip_str) {
892 struct prefix nhop;
f663c581 893 struct bgp_nexthop_cache_head (*tree)[AFI_MAX];
5d76a53d 894 struct bgp_nexthop_cache *bnc;
895
896 if (!str2prefix(nhopip_str, &nhop)) {
897 vty_out(vty, "nexthop address is malformed\n");
898 return CMD_WARNING;
899 }
f663c581
RW
900 tree = import_table ? &bgp->import_check_table
901 : &bgp->nexthop_cache_table;
902 bnc = bnc_find(tree[family2afi(nhop.family)], &nhop, 0);
5d76a53d 903 if (!bnc) {
904 vty_out(vty, "specified nexthop does not have entry\n");
905 return CMD_SUCCESS;
906 }
f663c581 907 bgp_show_nexthop(vty, bgp, bnc, true);
5d76a53d 908 } else
909 bgp_show_nexthops(vty, bgp, import_table);
f186de26 910
d62a17ae 911 return CMD_SUCCESS;
fb018d25
DS
912}
913
d62a17ae 914static void bgp_show_all_instances_nexthops_vty(struct vty *vty)
f186de26 915{
d62a17ae 916 struct listnode *node, *nnode;
917 struct bgp *bgp;
918
919 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
920 vty_out(vty, "\nInstance %s:\n",
921 (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
5742e42b 922 ? VRF_DEFAULT_NAME
d62a17ae 923 : bgp->name);
5d76a53d 924 bgp_show_nexthops(vty, bgp, false);
d62a17ae 925 }
f186de26 926}
927
fb018d25
DS
928DEFUN (show_ip_bgp_nexthop,
929 show_ip_bgp_nexthop_cmd,
5d76a53d 930 "show [ip] bgp [<view|vrf> VIEWVRFNAME] nexthop [<A.B.C.D|X:X::X:X>] [detail]",
50ef26d4 931 SHOW_STR
932 IP_STR
933 BGP_STR
8386ac43 934 BGP_INSTANCE_HELP_STR
3a2d747c 935 "BGP nexthop table\n"
5d76a53d 936 "IPv4 nexthop address\n"
937 "IPv6 nexthop address\n"
3a2d747c 938 "Show detailed information\n")
50ef26d4 939{
d62a17ae 940 int idx = 0;
5d76a53d 941 int nh_idx = 0;
b7ada628 942 char *vrf = NULL;
5d76a53d 943 char *nhop_ip = NULL;
b7ada628
DS
944
945 if (argv_find(argv, argc, "view", &idx)
946 || argv_find(argv, argc, "vrf", &idx))
947 vrf = argv[++idx]->arg;
05e47722 948
5d76a53d 949 if (argv_find(argv, argc, "A.B.C.D", &nh_idx)
950 || argv_find(argv, argc, "X:X::X:X", &nh_idx))
951 nhop_ip = argv[nh_idx]->arg;
952
953 return show_ip_bgp_nexthop_table(vty, vrf, nhop_ip, false);
05e47722
PG
954}
955
956DEFUN (show_ip_bgp_import_check,
957 show_ip_bgp_import_check_cmd,
958 "show [ip] bgp [<view|vrf> VIEWVRFNAME] import-check-table [detail]",
959 SHOW_STR
960 IP_STR
961 BGP_STR
962 BGP_INSTANCE_HELP_STR
963 "BGP import check table\n"
964 "Show detailed information\n")
965{
966 int idx = 0;
967 char *vrf = NULL;
968
969 if (argv_find(argv, argc, "view", &idx)
970 || argv_find(argv, argc, "vrf", &idx))
971 vrf = argv[++idx]->arg;
5d76a53d 972
973 return show_ip_bgp_nexthop_table(vty, vrf, NULL, true);
50ef26d4 974}
975
f186de26 976DEFUN (show_ip_bgp_instance_all_nexthop,
977 show_ip_bgp_instance_all_nexthop_cmd,
bec37ba5 978 "show [ip] bgp <view|vrf> all nexthop",
f186de26 979 SHOW_STR
980 IP_STR
981 BGP_STR
982 BGP_INSTANCE_ALL_HELP_STR
983 "BGP nexthop table\n")
984{
d62a17ae 985 bgp_show_all_instances_nexthops_vty(vty);
986 return CMD_SUCCESS;
f186de26 987}
988
d62a17ae 989void bgp_scan_init(struct bgp *bgp)
718e3744 990{
d62a17ae 991 afi_t afi;
992
993 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
f663c581
RW
994 bgp_nexthop_cache_init(&bgp->nexthop_cache_table[afi]);
995 bgp_nexthop_cache_init(&bgp->import_check_table[afi]);
960035b2
PZ
996 bgp->connected_table[afi] = bgp_table_init(bgp, afi,
997 SAFI_UNICAST);
d62a17ae 998 }
fc9a856f
DS
999}
1000
d62a17ae 1001void bgp_scan_vty_init(void)
fc9a856f 1002{
d62a17ae 1003 install_element(VIEW_NODE, &show_ip_bgp_nexthop_cmd);
05e47722 1004 install_element(VIEW_NODE, &show_ip_bgp_import_check_cmd);
d62a17ae 1005 install_element(VIEW_NODE, &show_ip_bgp_instance_all_nexthop_cmd);
718e3744 1006}
228da428 1007
d62a17ae 1008void bgp_scan_finish(struct bgp *bgp)
228da428 1009{
d62a17ae 1010 afi_t afi;
6c88b44d 1011
d62a17ae 1012 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
1013 /* Only the current one needs to be reset. */
f663c581
RW
1014 bgp_nexthop_cache_reset(&bgp->nexthop_cache_table[afi]);
1015 bgp_nexthop_cache_reset(&bgp->import_check_table[afi]);
228da428 1016
3292693b
DS
1017 bgp->connected_table[afi]->route_table->cleanup =
1018 bgp_connected_cleanup;
d62a17ae 1019 bgp_table_unlock(bgp->connected_table[afi]);
1020 bgp->connected_table[afi] = NULL;
d62a17ae 1021 }
228da428 1022}