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