]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_nexthop.c
Merge pull request #8744 from sworleys/RTADV-Fix-Upstream
[mirror_frr.git] / bgpd / bgp_nexthop.c
1 /* BGP nexthop scan
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 */
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"
31 #include "hash.h"
32 #include "jhash.h"
33 #include "nexthop.h"
34 #include "queue.h"
35 #include "filter.h"
36 #include "printfrr.h"
37
38 #include "bgpd/bgpd.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 "bgpd/bgp_rd.h"
48
49 DEFINE_MTYPE_STATIC(BGPD, MARTIAN_STRING, "BGP Martian Addr Intf String");
50
51 int bgp_nexthop_cache_compare(const struct bgp_nexthop_cache *a,
52 const struct bgp_nexthop_cache *b)
53 {
54 if (a->srte_color < b->srte_color)
55 return -1;
56 if (a->srte_color > b->srte_color)
57 return 1;
58
59 return prefix_cmp(&a->prefix, &b->prefix);
60 }
61
62 const char *bnc_str(struct bgp_nexthop_cache *bnc, char *buf, int size)
63 {
64 return prefix2str(&bnc->prefix, buf, size);
65 }
66
67 void bnc_nexthop_free(struct bgp_nexthop_cache *bnc)
68 {
69 nexthops_free(bnc->nexthop);
70 }
71
72 struct bgp_nexthop_cache *bnc_new(struct bgp_nexthop_cache_head *tree,
73 struct prefix *prefix, uint32_t srte_color)
74 {
75 struct bgp_nexthop_cache *bnc;
76
77 bnc = XCALLOC(MTYPE_BGP_NEXTHOP_CACHE,
78 sizeof(struct bgp_nexthop_cache));
79 bnc->prefix = *prefix;
80 bnc->srte_color = srte_color;
81 bnc->tree = tree;
82 LIST_INIT(&(bnc->paths));
83 bgp_nexthop_cache_add(tree, bnc);
84
85 return bnc;
86 }
87
88 bool 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
101 void bnc_free(struct bgp_nexthop_cache *bnc)
102 {
103 bnc_nexthop_free(bnc);
104 bgp_nexthop_cache_del(bnc->tree, bnc);
105 XFREE(MTYPE_BGP_NEXTHOP_CACHE, bnc);
106 }
107
108 struct bgp_nexthop_cache *bnc_find(struct bgp_nexthop_cache_head *tree,
109 struct prefix *prefix, uint32_t srte_color)
110 {
111 struct bgp_nexthop_cache bnc = {};
112
113 if (!tree)
114 return NULL;
115
116 bnc.prefix = *prefix;
117 bnc.srte_color = srte_color;
118 return bgp_nexthop_cache_find(tree, &bnc);
119 }
120
121 /* Reset and free all BGP nexthop cache. */
122 static void bgp_nexthop_cache_reset(struct bgp_nexthop_cache_head *tree)
123 {
124 struct bgp_nexthop_cache *bnc;
125
126 while (bgp_nexthop_cache_count(tree) > 0) {
127 bnc = bgp_nexthop_cache_first(tree);
128
129 while (!LIST_EMPTY(&(bnc->paths))) {
130 struct bgp_path_info *path = LIST_FIRST(&(bnc->paths));
131
132 path_nh_map(path, bnc, false);
133 }
134
135 bnc_free(bnc);
136 }
137 }
138
139 static void *bgp_tip_hash_alloc(void *p)
140 {
141 const struct in_addr *val = (const struct in_addr *)p;
142 struct tip_addr *addr;
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
151 static void bgp_tip_hash_free(void *addr)
152 {
153 XFREE(MTYPE_TIP_ADDR, addr);
154 }
155
156 static unsigned int bgp_tip_hash_key_make(const void *p)
157 {
158 const struct tip_addr *addr = p;
159
160 return jhash_1word(addr->addr.s_addr, 0);
161 }
162
163 static bool bgp_tip_hash_cmp(const void *p1, const void *p2)
164 {
165 const struct tip_addr *addr1 = p1;
166 const struct tip_addr *addr2 = p2;
167
168 return addr1->addr.s_addr == addr2->addr.s_addr;
169 }
170
171 void bgp_tip_hash_init(struct bgp *bgp)
172 {
173 bgp->tip_hash = hash_create(bgp_tip_hash_key_make, bgp_tip_hash_cmp,
174 "BGP TIP hash");
175 }
176
177 void 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
186 void bgp_tip_add(struct bgp *bgp, struct in_addr *tip)
187 {
188 struct tip_addr tmp;
189 struct tip_addr *addr;
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
200 void bgp_tip_del(struct bgp *bgp, struct in_addr *tip)
201 {
202 struct tip_addr tmp;
203 struct tip_addr *addr;
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 }
219
220 /* BGP own address structure */
221 struct bgp_addr {
222 struct prefix p;
223 struct list *ifp_name_list;
224 };
225
226 static void show_address_entry(struct hash_bucket *bucket, void *args)
227 {
228 struct vty *vty = (struct vty *)args;
229 struct bgp_addr *addr = (struct bgp_addr *)bucket->data;
230 char *name;
231 struct listnode *node;
232 char str[INET6_ADDRSTRLEN] = {0};
233
234 vty_out(vty, "addr: %s, count: %d : ",
235 inet_ntop(addr->p.family, &(addr->p.u.prefix),
236 str, INET6_ADDRSTRLEN),
237 addr->ifp_name_list->count);
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");
244 }
245
246 void bgp_nexthop_show_address_hash(struct vty *vty, struct bgp *bgp)
247 {
248 hash_iterate(bgp->address_hash,
249 (void (*)(struct hash_bucket *, void *))show_address_entry,
250 vty);
251 }
252
253 static void bgp_address_hash_string_del(void *val)
254 {
255 char *data = val;
256
257 XFREE(MTYPE_MARTIAN_STRING, data);
258 }
259
260 static void *bgp_address_hash_alloc(void *p)
261 {
262 struct bgp_addr *copy_addr = p;
263 struct bgp_addr *addr = NULL;
264
265 addr = XMALLOC(MTYPE_BGP_ADDR, sizeof(struct bgp_addr));
266 prefix_copy(&addr->p, &copy_addr->p);
267
268 addr->ifp_name_list = list_new();
269 addr->ifp_name_list->del = bgp_address_hash_string_del;
270
271 return addr;
272 }
273
274 static void bgp_address_hash_free(void *data)
275 {
276 struct bgp_addr *addr = data;
277
278 list_delete(&addr->ifp_name_list);
279 XFREE(MTYPE_BGP_ADDR, addr);
280 }
281
282 static unsigned int bgp_address_hash_key_make(const void *p)
283 {
284 const struct bgp_addr *addr = p;
285
286 return prefix_hash_key(&addr->p);
287 }
288
289 static bool bgp_address_hash_cmp(const void *p1, const void *p2)
290 {
291 const struct bgp_addr *addr1 = p1;
292 const struct bgp_addr *addr2 = p2;
293
294 return prefix_same(&addr1->p, &addr2->p);
295 }
296
297 void bgp_address_init(struct bgp *bgp)
298 {
299 bgp->address_hash =
300 hash_create(bgp_address_hash_key_make, bgp_address_hash_cmp,
301 "BGP Connected Address Hash");
302 }
303
304 void bgp_address_destroy(struct bgp *bgp)
305 {
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;
311 }
312
313 static void bgp_address_add(struct bgp *bgp, struct connected *ifc,
314 struct prefix *p)
315 {
316 struct bgp_addr tmp;
317 struct bgp_addr *addr;
318 struct listnode *node;
319 char *name;
320
321 tmp.p = *p;
322
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;
327
328 addr = hash_get(bgp->address_hash, &tmp, bgp_address_hash_alloc);
329
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) {
335 name = XSTRDUP(MTYPE_MARTIAN_STRING, ifc->ifp->name);
336 listnode_add(addr->ifp_name_list, name);
337 }
338 }
339
340 static void bgp_address_del(struct bgp *bgp, struct connected *ifc,
341 struct prefix *p)
342 {
343 struct bgp_addr tmp;
344 struct bgp_addr *addr;
345 struct listnode *node;
346 char *name;
347
348 tmp.p = *p;
349
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;
354
355 addr = hash_lookup(bgp->address_hash, &tmp);
356 /* may have been deleted earlier by bgp_interface_down() */
357 if (addr == NULL)
358 return;
359
360 for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
361 if (strcmp(ifc->ifp->name, name) == 0)
362 break;
363 }
364
365 if (node) {
366 list_delete_node(addr->ifp_name_list, node);
367 XFREE(MTYPE_MARTIAN_STRING, name);
368 }
369
370 if (addr->ifp_name_list->count == 0) {
371 hash_release(bgp->address_hash, addr);
372 list_delete(&addr->ifp_name_list);
373 XFREE(MTYPE_BGP_ADDR, addr);
374 }
375 }
376
377
378 struct bgp_connected_ref {
379 unsigned int refcnt;
380 };
381
382 void bgp_connected_add(struct bgp *bgp, struct connected *ifc)
383 {
384 struct prefix p;
385 struct prefix *addr;
386 struct bgp_dest *dest;
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
400 bgp_address_add(bgp, ifc, addr);
401
402 dest = bgp_node_get(bgp->connected_table[AFI_IP],
403 (struct prefix *)&p);
404 bc = bgp_dest_get_bgp_connected_ref_info(dest);
405 if (bc)
406 bc->refcnt++;
407 else {
408 bc = XCALLOC(MTYPE_BGP_CONN,
409 sizeof(struct bgp_connected_ref));
410 bc->refcnt = 1;
411 bgp_dest_set_bgp_connected_ref_info(dest, bc);
412 }
413
414 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
415 if (peer->conf_if
416 && (strcmp(peer->conf_if, ifc->ifp->name) == 0)
417 && !peer_established(peer)
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
434 bgp_address_add(bgp, ifc, addr);
435
436 dest = bgp_node_get(bgp->connected_table[AFI_IP6],
437 (struct prefix *)&p);
438
439 bc = bgp_dest_get_bgp_connected_ref_info(dest);
440 if (bc)
441 bc->refcnt++;
442 else {
443 bc = XCALLOC(MTYPE_BGP_CONN,
444 sizeof(struct bgp_connected_ref));
445 bc->refcnt = 1;
446 bgp_dest_set_bgp_connected_ref_info(dest, bc);
447 }
448 }
449 }
450
451 void bgp_connected_delete(struct bgp *bgp, struct connected *ifc)
452 {
453 struct prefix p;
454 struct prefix *addr;
455 struct bgp_dest *dest = NULL;
456 struct bgp_connected_ref *bc;
457
458 addr = ifc->address;
459
460 p = *(CONNECTED_PREFIX(ifc));
461 apply_mask(&p);
462 if (addr->family == AF_INET) {
463 if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
464 return;
465
466 bgp_address_del(bgp, ifc, addr);
467
468 dest = bgp_node_lookup(bgp->connected_table[AFI_IP], &p);
469 } else if (addr->family == AF_INET6) {
470 if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
471 return;
472
473 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
474 return;
475
476 bgp_address_del(bgp, ifc, addr);
477
478 dest = bgp_node_lookup(bgp->connected_table[AFI_IP6], &p);
479 }
480
481 if (!dest)
482 return;
483
484 bc = bgp_dest_get_bgp_connected_ref_info(dest);
485 bc->refcnt--;
486 if (bc->refcnt == 0) {
487 XFREE(MTYPE_BGP_CONN, bc);
488 bgp_dest_set_bgp_connected_ref_info(dest, NULL);
489 }
490 bgp_dest_unlock_node(dest);
491 bgp_dest_unlock_node(dest);
492 }
493
494 static void bgp_connected_cleanup(struct route_table *table,
495 struct route_node *rn)
496 {
497 struct bgp_connected_ref *bc;
498 struct bgp_dest *bn = bgp_dest_from_rnode(rn);
499
500 bc = bgp_dest_get_bgp_connected_ref_info(bn);
501 if (!bc)
502 return;
503
504 bc->refcnt--;
505 if (bc->refcnt == 0) {
506 XFREE(MTYPE_BGP_CONN, bc);
507 bgp_dest_set_bgp_connected_ref_info(bn, NULL);
508 }
509 }
510
511 bool bgp_nexthop_self(struct bgp *bgp, afi_t afi, uint8_t type,
512 uint8_t sub_type, struct attr *attr,
513 struct bgp_dest *dest)
514 {
515 uint8_t new_afi = afi == AFI_IP ? AF_INET : AF_INET6;
516 struct bgp_addr tmp_addr = {{0}}, *addr = NULL;
517 struct tip_addr tmp_tip, *tip = NULL;
518 const struct prefix *p = bgp_dest_get_prefix(dest);
519 bool is_bgp_static_route =
520 ((type == ZEBRA_ROUTE_BGP) && (sub_type == BGP_ROUTE_STATIC))
521 ? true
522 : false;
523
524 if (!is_bgp_static_route)
525 new_afi = BGP_ATTR_NEXTHOP_AFI_IP6(attr) ? AF_INET6 : AF_INET;
526
527 tmp_addr.p.family = new_afi;
528 switch (new_afi) {
529 case AF_INET:
530 if (is_bgp_static_route) {
531 tmp_addr.p.u.prefix4 = p->u.prefix4;
532 tmp_addr.p.prefixlen = p->prefixlen;
533 } else {
534 /* Here we need to find out which nexthop to be used*/
535 if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP)) {
536 tmp_addr.p.u.prefix4 = attr->nexthop;
537 tmp_addr.p.prefixlen = IPV4_MAX_BITLEN;
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))) {
543 tmp_addr.p.u.prefix4 =
544 attr->mp_nexthop_global_in;
545 tmp_addr.p.prefixlen = IPV4_MAX_BITLEN;
546 } else
547 return false;
548 }
549 break;
550 case AF_INET6:
551 if (is_bgp_static_route) {
552 tmp_addr.p.u.prefix6 = p->u.prefix6;
553 tmp_addr.p.prefixlen = p->prefixlen;
554 } else {
555 tmp_addr.p.u.prefix6 = attr->mp_nexthop_global;
556 tmp_addr.p.prefixlen = IPV6_MAX_BITLEN;
557 }
558 break;
559 default:
560 break;
561 }
562
563 addr = hash_lookup(bgp->address_hash, &tmp_addr);
564 if (addr)
565 return true;
566
567 if (new_afi == AF_INET && hashcount(bgp->tip_hash)) {
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) &&
574 ((attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV4)
575 || (attr->mp_nexthop_len == BGP_ATTR_NHLEN_VPNV4))) {
576 tmp_tip.addr = attr->mp_nexthop_global_in;
577 }
578
579 tip = hash_lookup(bgp->tip_hash, &tmp_tip);
580 if (tip)
581 return true;
582 }
583
584 return false;
585 }
586
587 bool bgp_multiaccess_check_v4(struct in_addr nexthop, struct peer *peer)
588 {
589 struct bgp_dest *dest1;
590 struct bgp_dest *dest2;
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
598 dest1 = bgp_node_match(peer->bgp->connected_table[AFI_IP], &p);
599 if (!dest1)
600 return false;
601
602 p.family = AF_INET;
603 p.prefixlen = IPV4_MAX_BITLEN;
604 p.u.prefix4 = peer->su.sin.sin_addr;
605
606 dest2 = bgp_node_match(peer->bgp->connected_table[AFI_IP], &p);
607 if (!dest2) {
608 bgp_dest_unlock_node(dest1);
609 return false;
610 }
611
612 ret = (dest1 == dest2);
613
614 bgp_dest_unlock_node(dest1);
615 bgp_dest_unlock_node(dest2);
616
617 return ret;
618 }
619
620 bool bgp_multiaccess_check_v6(struct in6_addr nexthop, struct peer *peer)
621 {
622 struct bgp_dest *dest1;
623 struct bgp_dest *dest2;
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
631 dest1 = bgp_node_match(peer->bgp->connected_table[AFI_IP6], &p);
632 if (!dest1)
633 return false;
634
635 p.family = AF_INET6;
636 p.prefixlen = IPV6_MAX_BITLEN;
637 p.u.prefix6 = peer->su.sin6.sin6_addr;
638
639 dest2 = bgp_node_match(peer->bgp->connected_table[AFI_IP6], &p);
640 if (!dest2) {
641 bgp_dest_unlock_node(dest1);
642 return false;
643 }
644
645 ret = (dest1 == dest2);
646
647 bgp_dest_unlock_node(dest1);
648 bgp_dest_unlock_node(dest2);
649
650 return ret;
651 }
652
653 bool bgp_subgrp_multiaccess_check_v6(struct in6_addr nexthop,
654 struct update_subgroup *subgrp,
655 struct peer *exclude)
656 {
657 struct bgp_dest *dest1 = NULL, *dest2 = NULL;
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);
670 dest1 = bgp_node_match(bgp->connected_table[AFI_IP6], &np);
671 if (!dest1)
672 return false;
673
674 SUBGRP_FOREACH_PEER (subgrp, paf) {
675 /* Skip peer we're told to exclude - e.g., source of route. */
676 if (paf->peer == exclude)
677 continue;
678
679 p.u.prefix6 = paf->peer->su.sin6.sin6_addr;
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);
684 return true;
685 }
686
687 if (dest2)
688 bgp_dest_unlock_node(dest2);
689 }
690
691 bgp_dest_unlock_node(dest1);
692 return false;
693 }
694
695 bool bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop,
696 struct update_subgroup *subgrp,
697 struct peer *exclude)
698 {
699 struct bgp_dest *dest1, *dest2;
700 struct peer_af *paf;
701 struct prefix p, np;
702 struct bgp *bgp;
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
711 bgp = SUBGRP_INST(subgrp);
712 dest1 = bgp_node_match(bgp->connected_table[AFI_IP], &np);
713 if (!dest1)
714 return false;
715
716 SUBGRP_FOREACH_PEER (subgrp, paf) {
717 /* Skip peer we're told to exclude - e.g., source of route. */
718 if (paf->peer == exclude)
719 continue;
720
721 p.u.prefix4 = paf->peer->su.sin.sin_addr;
722
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);
727 return true;
728 }
729
730 if (dest2)
731 bgp_dest_unlock_node(dest2);
732 }
733
734 bgp_dest_unlock_node(dest1);
735 return false;
736 }
737
738 static void bgp_show_nexthop_paths(struct vty *vty, struct bgp *bgp,
739 struct bgp_nexthop_cache *bnc)
740 {
741 struct bgp_dest *dest;
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) {
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);
755 safi = table->safi;
756 bgp_path = table->bgp;
757
758 if (dest->pdest) {
759 prefix_rd2str((struct prefix_rd *)bgp_dest_get_prefix(dest->pdest),
760 buf1, sizeof(buf1));
761 vty_out(vty, " %d/%d %pBD RD %s %s flags 0x%x\n",
762 afi, safi, dest, buf1, bgp_path->name_pretty, path->flags);
763 } else
764 vty_out(vty, " %d/%d %pBD %s flags 0x%x\n",
765 afi, safi, dest, bgp_path->name_pretty, path->flags);
766 }
767 }
768
769 static void bgp_show_nexthops_detail(struct vty *vty, struct bgp *bgp,
770 struct bgp_nexthop_cache *bnc)
771 {
772 char buf[PREFIX2STR_BUFFER];
773 struct nexthop *nexthop;
774
775 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next) {
776 switch (nexthop->type) {
777 case NEXTHOP_TYPE_IPV6:
778 vty_out(vty, " gate %s\n",
779 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
780 sizeof(buf)));
781 break;
782 case NEXTHOP_TYPE_IPV6_IFINDEX:
783 vty_out(vty, " gate %s, if %s\n",
784 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
785 sizeof(buf)),
786 ifindex2ifname(bnc->ifindex ? bnc->ifindex
787 : nexthop->ifindex,
788 bgp->vrf_id));
789 break;
790 case NEXTHOP_TYPE_IPV4:
791 vty_out(vty, " gate %s\n",
792 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf,
793 sizeof(buf)));
794 break;
795 case NEXTHOP_TYPE_IFINDEX:
796 vty_out(vty, " if %s\n",
797 ifindex2ifname(bnc->ifindex ? bnc->ifindex
798 : nexthop->ifindex,
799 bgp->vrf_id));
800 break;
801 case NEXTHOP_TYPE_IPV4_IFINDEX:
802 vty_out(vty, " gate %s, if %s\n",
803 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf,
804 sizeof(buf)),
805 ifindex2ifname(bnc->ifindex ? bnc->ifindex
806 : nexthop->ifindex,
807 bgp->vrf_id));
808 break;
809 case NEXTHOP_TYPE_BLACKHOLE:
810 vty_out(vty, " blackhole\n");
811 break;
812 default:
813 vty_out(vty, " invalid nexthop type %u\n",
814 nexthop->type);
815 }
816 }
817 }
818
819 static void bgp_show_nexthop(struct vty *vty, struct bgp *bgp,
820 struct bgp_nexthop_cache *bnc,
821 bool specific)
822 {
823 char buf[PREFIX2STR_BUFFER];
824 time_t tbuf;
825 struct peer *peer;
826
827 peer = (struct peer *)bnc->nht_info;
828
829 if (bnc->srte_color)
830 vty_out(vty, " SR-TE color %u -", bnc->srte_color);
831 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) {
832 vty_out(vty, " %s valid [IGP metric %d], #paths %d",
833 inet_ntop(bnc->prefix.family, &bnc->prefix.u.prefix,
834 buf, sizeof(buf)),
835 bnc->metric, bnc->path_count);
836 if (peer)
837 vty_out(vty, ", peer %s", peer->host);
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");
850 vty_out(vty, "\n");
851 bgp_show_nexthops_detail(vty, bgp, bnc);
852 } else {
853 vty_out(vty, " %s invalid, #paths %d",
854 inet_ntop(bnc->prefix.family, &bnc->prefix.u.prefix,
855 buf, sizeof(buf)),
856 bnc->path_count);
857 if (peer)
858 vty_out(vty, ", peer %s", peer->host);
859 if (bnc->is_evpn_gwip_nexthop)
860 vty_out(vty, " EVPN Gateway IP");
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
876 static void bgp_show_nexthops(struct vty *vty, struct bgp *bgp,
877 bool import_table)
878 {
879 struct bgp_nexthop_cache *bnc;
880 afi_t afi;
881 struct bgp_nexthop_cache_head(*tree)[AFI_MAX];
882
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)
888 tree = &bgp->import_check_table;
889 else
890 tree = &bgp->nexthop_cache_table;
891 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
892 frr_each (bgp_nexthop_cache, &(*tree)[afi], bnc)
893 bgp_show_nexthop(vty, bgp, bnc, false);
894 }
895 }
896
897 static int show_ip_bgp_nexthop_table(struct vty *vty, const char *name,
898 const char *nhopip_str,
899 bool import_table)
900 {
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 }
911
912 if (nhopip_str) {
913 struct prefix nhop;
914 struct bgp_nexthop_cache_head (*tree)[AFI_MAX];
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 }
921 tree = import_table ? &bgp->import_check_table
922 : &bgp->nexthop_cache_table;
923 bnc = bnc_find(tree[family2afi(nhop.family)], &nhop, 0);
924 if (!bnc) {
925 vty_out(vty, "specified nexthop does not have entry\n");
926 return CMD_SUCCESS;
927 }
928 bgp_show_nexthop(vty, bgp, bnc, true);
929 } else
930 bgp_show_nexthops(vty, bgp, import_table);
931
932 return CMD_SUCCESS;
933 }
934
935 static void bgp_show_all_instances_nexthops_vty(struct vty *vty)
936 {
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)
943 ? VRF_DEFAULT_NAME
944 : bgp->name);
945 bgp_show_nexthops(vty, bgp, false);
946 }
947 }
948
949 DEFUN (show_ip_bgp_nexthop,
950 show_ip_bgp_nexthop_cmd,
951 "show [ip] bgp [<view|vrf> VIEWVRFNAME] nexthop [<A.B.C.D|X:X::X:X>] [detail]",
952 SHOW_STR
953 IP_STR
954 BGP_STR
955 BGP_INSTANCE_HELP_STR
956 "BGP nexthop table\n"
957 "IPv4 nexthop address\n"
958 "IPv6 nexthop address\n"
959 "Show detailed information\n")
960 {
961 int idx = 0;
962 int nh_idx = 0;
963 char *vrf = NULL;
964 char *nhop_ip = NULL;
965
966 if (argv_find(argv, argc, "view", &idx)
967 || argv_find(argv, argc, "vrf", &idx))
968 vrf = argv[++idx]->arg;
969
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);
975 }
976
977 DEFUN (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;
993
994 return show_ip_bgp_nexthop_table(vty, vrf, NULL, true);
995 }
996
997 DEFUN (show_ip_bgp_instance_all_nexthop,
998 show_ip_bgp_instance_all_nexthop_cmd,
999 "show [ip] bgp <view|vrf> all nexthop",
1000 SHOW_STR
1001 IP_STR
1002 BGP_STR
1003 BGP_INSTANCE_ALL_HELP_STR
1004 "BGP nexthop table\n")
1005 {
1006 bgp_show_all_instances_nexthops_vty(vty);
1007 return CMD_SUCCESS;
1008 }
1009
1010 void bgp_scan_init(struct bgp *bgp)
1011 {
1012 afi_t afi;
1013
1014 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
1015 bgp_nexthop_cache_init(&bgp->nexthop_cache_table[afi]);
1016 bgp_nexthop_cache_init(&bgp->import_check_table[afi]);
1017 bgp->connected_table[afi] = bgp_table_init(bgp, afi,
1018 SAFI_UNICAST);
1019 }
1020 }
1021
1022 void bgp_scan_vty_init(void)
1023 {
1024 install_element(VIEW_NODE, &show_ip_bgp_nexthop_cmd);
1025 install_element(VIEW_NODE, &show_ip_bgp_import_check_cmd);
1026 install_element(VIEW_NODE, &show_ip_bgp_instance_all_nexthop_cmd);
1027 }
1028
1029 void bgp_scan_finish(struct bgp *bgp)
1030 {
1031 afi_t afi;
1032
1033 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
1034 /* Only the current one needs to be reset. */
1035 bgp_nexthop_cache_reset(&bgp->nexthop_cache_table[afi]);
1036 bgp_nexthop_cache_reset(&bgp->import_check_table[afi]);
1037
1038 bgp->connected_table[afi]->route_table->cleanup =
1039 bgp_connected_cleanup;
1040 bgp_table_unlock(bgp->connected_table[afi]);
1041 bgp->connected_table[afi] = NULL;
1042 }
1043 }
1044
1045 char *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 }
1069
1070 char *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 }