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