]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_nht.c
bgpd: add resolution for l3vpn traffic over gre interfaces
[mirror_frr.git] / bgpd / bgp_nht.c
1 /* BGP Nexthop tracking
2 * Copyright (C) 2013 Cumulus Networks, Inc.
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 "nexthop.h"
32 #include "vrf.h"
33 #include "filter.h"
34 #include "nexthop_group.h"
35
36 #include "bgpd/bgpd.h"
37 #include "bgpd/bgp_table.h"
38 #include "bgpd/bgp_route.h"
39 #include "bgpd/bgp_attr.h"
40 #include "bgpd/bgp_nexthop.h"
41 #include "bgpd/bgp_debug.h"
42 #include "bgpd/bgp_errors.h"
43 #include "bgpd/bgp_nht.h"
44 #include "bgpd/bgp_fsm.h"
45 #include "bgpd/bgp_zebra.h"
46 #include "bgpd/bgp_flowspec_util.h"
47 #include "bgpd/bgp_evpn.h"
48 #include "bgpd/bgp_rd.h"
49
50 extern struct zclient *zclient;
51
52 static void register_zebra_rnh(struct bgp_nexthop_cache *bnc);
53 static void unregister_zebra_rnh(struct bgp_nexthop_cache *bnc);
54 static int make_prefix(int afi, struct bgp_path_info *pi, struct prefix *p);
55 static void bgp_nht_ifp_initial(struct thread *thread);
56
57 static int bgp_isvalid_nexthop(struct bgp_nexthop_cache *bnc)
58 {
59 return (bgp_zebra_num_connects() == 0
60 || (bnc && CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)
61 && bnc->nexthop_num > 0));
62 }
63
64 static int bgp_isvalid_nexthop_for_mplsovergre(struct bgp_nexthop_cache *bnc,
65 struct bgp_path_info *path)
66 {
67 struct interface *ifp = NULL;
68 struct nexthop *nexthop;
69
70 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next) {
71 if (nexthop->type != NEXTHOP_TYPE_BLACKHOLE) {
72 ifp = if_lookup_by_index(
73 bnc->ifindex ? bnc->ifindex : nexthop->ifindex,
74 bnc->bgp->vrf_id);
75 if (ifp && (ifp->ll_type == ZEBRA_LLT_IPGRE ||
76 ifp->ll_type == ZEBRA_LLT_IP6GRE))
77 break;
78 }
79 }
80 if (!ifp)
81 return false;
82
83 if (CHECK_FLAG(path->attr->rmap_change_flags,
84 BATTR_RMAP_L3VPN_ACCEPT_GRE))
85 return true;
86
87 return false;
88 }
89
90 static int bgp_isvalid_nexthop_for_mpls(struct bgp_nexthop_cache *bnc,
91 struct bgp_path_info *path)
92 {
93 /*
94 * - In the case of MPLS-VPN, the label is learned from LDP or other
95 * protocols, and nexthop tracking is enabled for the label.
96 * The value is recorded as BGP_NEXTHOP_LABELED_VALID.
97 * - In the case of SRv6-VPN, we need to track the reachability to the
98 * SID (in other words, IPv6 address). As in MPLS, we need to record
99 * the value as BGP_NEXTHOP_SID_VALID. However, this function is
100 * currently not implemented, and this function assumes that all
101 * Transit routes for SRv6-VPN are valid.
102 * - Otherwise check for mpls-gre acceptance
103 */
104 return (bgp_zebra_num_connects() == 0 ||
105 (bnc && (bnc->nexthop_num > 0 &&
106 (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_LABELED_VALID) ||
107 bnc->bgp->srv6_enabled ||
108 bgp_isvalid_nexthop_for_mplsovergre(bnc, path)))));
109 }
110
111 static void bgp_unlink_nexthop_check(struct bgp_nexthop_cache *bnc)
112 {
113 if (LIST_EMPTY(&(bnc->paths)) && !bnc->nht_info) {
114 if (BGP_DEBUG(nht, NHT))
115 zlog_debug("%s: freeing bnc %pFX(%d)(%u)(%s)", __func__,
116 &bnc->prefix, bnc->ifindex, bnc->srte_color,
117 bnc->bgp->name_pretty);
118 /* only unregister if this is the last nh for this prefix*/
119 if (!bnc_existing_for_prefix(bnc))
120 unregister_zebra_rnh(bnc);
121 bnc_free(bnc);
122 }
123 }
124
125 void bgp_unlink_nexthop(struct bgp_path_info *path)
126 {
127 struct bgp_nexthop_cache *bnc = path->nexthop;
128
129 if (!bnc)
130 return;
131
132 path_nh_map(path, NULL, false);
133
134 bgp_unlink_nexthop_check(bnc);
135 }
136
137 void bgp_replace_nexthop_by_peer(struct peer *from, struct peer *to)
138 {
139 struct prefix pp;
140 struct prefix pt;
141 struct bgp_nexthop_cache *bncp, *bnct;
142 afi_t afi;
143 ifindex_t ifindex = 0;
144
145 if (!sockunion2hostprefix(&from->su, &pp))
146 return;
147
148 /*
149 * Gather the ifindex for if up/down events to be
150 * tagged into this fun
151 */
152 if (from->conf_if && IN6_IS_ADDR_LINKLOCAL(&from->su.sin6.sin6_addr))
153 ifindex = from->su.sin6.sin6_scope_id;
154
155 afi = family2afi(pp.family);
156 bncp = bnc_find(&from->bgp->nexthop_cache_table[afi], &pp, 0, ifindex);
157
158 if (!sockunion2hostprefix(&to->su, &pt))
159 return;
160
161 /*
162 * Gather the ifindex for if up/down events to be
163 * tagged into this fun
164 */
165 ifindex = 0;
166 if (to->conf_if && IN6_IS_ADDR_LINKLOCAL(&to->su.sin6.sin6_addr))
167 ifindex = to->su.sin6.sin6_scope_id;
168 bnct = bnc_find(&to->bgp->nexthop_cache_table[afi], &pt, 0, ifindex);
169
170 if (bnct != bncp)
171 return;
172
173 if (bnct)
174 bnct->nht_info = to;
175 }
176
177 void bgp_unlink_nexthop_by_peer(struct peer *peer)
178 {
179 struct prefix p;
180 struct bgp_nexthop_cache *bnc;
181 afi_t afi = family2afi(peer->su.sa.sa_family);
182 ifindex_t ifindex = 0;
183
184 if (!sockunion2hostprefix(&peer->su, &p))
185 return;
186 /*
187 * Gather the ifindex for if up/down events to be
188 * tagged into this fun
189 */
190 if (afi == AFI_IP6 && IN6_IS_ADDR_LINKLOCAL(&peer->su.sin6.sin6_addr))
191 ifindex = peer->su.sin6.sin6_scope_id;
192 bnc = bnc_find(&peer->bgp->nexthop_cache_table[afi], &p, 0, ifindex);
193 if (!bnc)
194 return;
195
196 /* cleanup the peer reference */
197 bnc->nht_info = NULL;
198
199 bgp_unlink_nexthop_check(bnc);
200 }
201
202 /*
203 * A route and its nexthop might belong to different VRFs. Therefore,
204 * we need both the bgp_route and bgp_nexthop pointers.
205 */
206 int bgp_find_or_add_nexthop(struct bgp *bgp_route, struct bgp *bgp_nexthop,
207 afi_t afi, safi_t safi, struct bgp_path_info *pi,
208 struct peer *peer, int connected,
209 const struct prefix *orig_prefix)
210 {
211 struct bgp_nexthop_cache_head *tree = NULL;
212 struct bgp_nexthop_cache *bnc;
213 struct prefix p;
214 uint32_t srte_color = 0;
215 int is_bgp_static_route = 0;
216 ifindex_t ifindex = 0;
217
218 if (pi) {
219 is_bgp_static_route = ((pi->type == ZEBRA_ROUTE_BGP)
220 && (pi->sub_type == BGP_ROUTE_STATIC))
221 ? 1
222 : 0;
223
224 /* Since Extended Next-hop Encoding (RFC5549) support, we want
225 to derive
226 address-family from the next-hop. */
227 if (!is_bgp_static_route)
228 afi = BGP_ATTR_MP_NEXTHOP_LEN_IP6(pi->attr) ? AFI_IP6
229 : AFI_IP;
230
231 /* Validation for the ipv4 mapped ipv6 nexthop. */
232 if (IS_MAPPED_IPV6(&pi->attr->mp_nexthop_global)) {
233 afi = AFI_IP;
234 }
235
236 /* This will return true if the global IPv6 NH is a link local
237 * addr */
238 if (make_prefix(afi, pi, &p) < 0)
239 return 1;
240
241 if (!is_bgp_static_route && orig_prefix
242 && prefix_same(&p, orig_prefix)) {
243 if (BGP_DEBUG(nht, NHT)) {
244 zlog_debug(
245 "%s(%pFX): prefix loops through itself",
246 __func__, &p);
247 }
248 return 0;
249 }
250
251 srte_color = pi->attr->srte_color;
252 } else if (peer) {
253 /*
254 * Gather the ifindex for if up/down events to be
255 * tagged into this fun
256 */
257 if (afi == AFI_IP6 &&
258 IN6_IS_ADDR_LINKLOCAL(&peer->su.sin6.sin6_addr)) {
259 ifindex = peer->su.sin6.sin6_scope_id;
260 if (ifindex == 0) {
261 if (BGP_DEBUG(nht, NHT)) {
262 zlog_debug(
263 "%s: Unable to locate ifindex, waiting till we have one",
264 peer->conf_if);
265 }
266 return 0;
267 }
268 }
269
270 if (!sockunion2hostprefix(&peer->su, &p)) {
271 if (BGP_DEBUG(nht, NHT)) {
272 zlog_debug(
273 "%s: Attempting to register with unknown AFI %d (not %d or %d)",
274 __func__, afi, AFI_IP, AFI_IP6);
275 }
276 return 0;
277 }
278 } else
279 return 0;
280
281 if (is_bgp_static_route)
282 tree = &bgp_nexthop->import_check_table[afi];
283 else
284 tree = &bgp_nexthop->nexthop_cache_table[afi];
285
286 bnc = bnc_find(tree, &p, srte_color, ifindex);
287 if (!bnc) {
288 bnc = bnc_new(tree, &p, srte_color, ifindex);
289 bnc->bgp = bgp_nexthop;
290 if (BGP_DEBUG(nht, NHT))
291 zlog_debug("Allocated bnc %pFX(%d)(%u)(%s) peer %p",
292 &bnc->prefix, bnc->ifindex, bnc->srte_color,
293 bnc->bgp->name_pretty, peer);
294 } else {
295 if (BGP_DEBUG(nht, NHT))
296 zlog_debug(
297 "Found existing bnc %pFX(%d)(%s) flags 0x%x ifindex %d #paths %d peer %p",
298 &bnc->prefix, bnc->ifindex,
299 bnc->bgp->name_pretty, bnc->flags, bnc->ifindex,
300 bnc->path_count, bnc->nht_info);
301 }
302
303 if (pi && is_route_parent_evpn(pi))
304 bnc->is_evpn_gwip_nexthop = true;
305
306 if (is_bgp_static_route) {
307 SET_FLAG(bnc->flags, BGP_STATIC_ROUTE);
308
309 /* If we're toggling the type, re-register */
310 if ((CHECK_FLAG(bgp_route->flags, BGP_FLAG_IMPORT_CHECK))
311 && !CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH)) {
312 SET_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH);
313 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
314 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
315 } else if ((!CHECK_FLAG(bgp_route->flags,
316 BGP_FLAG_IMPORT_CHECK))
317 && CHECK_FLAG(bnc->flags,
318 BGP_STATIC_ROUTE_EXACT_MATCH)) {
319 UNSET_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH);
320 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
321 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
322 }
323 }
324 /* When nexthop is already known, but now requires 'connected'
325 * resolution,
326 * re-register it. The reverse scenario where the nexthop currently
327 * requires
328 * 'connected' resolution does not need a re-register (i.e., we treat
329 * 'connected-required' as an override) except in the scenario where
330 * this
331 * is actually a case of tracking a peer for connectivity (e.g., after
332 * disable connected-check).
333 * NOTE: We don't track the number of paths separately for 'connected-
334 * required' vs 'connected-not-required' as this change is not a common
335 * scenario.
336 */
337 else if (connected && !CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) {
338 SET_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED);
339 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
340 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
341 } else if (peer && !connected
342 && CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) {
343 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED);
344 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
345 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
346 }
347 if (peer && (bnc->ifindex != ifindex)) {
348 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
349 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
350 bnc->ifindex = ifindex;
351 }
352 if (bgp_route->inst_type == BGP_INSTANCE_TYPE_VIEW) {
353 SET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
354 SET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
355 } else if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED)
356 && !is_default_host_route(&bnc->prefix))
357 register_zebra_rnh(bnc);
358
359 if (pi && pi->nexthop != bnc) {
360 /* Unlink from existing nexthop cache, if any. This will also
361 * free
362 * the nexthop cache entry, if appropriate.
363 */
364 bgp_unlink_nexthop(pi);
365
366 /* updates NHT pi list reference */
367 path_nh_map(pi, bnc, true);
368
369 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID) && bnc->metric)
370 (bgp_path_info_extra_get(pi))->igpmetric = bnc->metric;
371 else if (pi->extra)
372 pi->extra->igpmetric = 0;
373 } else if (peer) {
374 /*
375 * Let's not accidentally save the peer data for a peer
376 * we are going to throw away in a second or so.
377 * When we come back around we'll fix up this
378 * data properly in replace_nexthop_by_peer
379 */
380 if (CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE))
381 bnc->nht_info = (void *)peer; /* NHT peer reference */
382 }
383
384 /*
385 * We are cheating here. Views have no associated underlying
386 * ability to detect nexthops. So when we have a view
387 * just tell everyone the nexthop is valid
388 */
389 if (bgp_route->inst_type == BGP_INSTANCE_TYPE_VIEW)
390 return 1;
391 else if (safi == SAFI_UNICAST && pi &&
392 pi->sub_type == BGP_ROUTE_IMPORTED && pi->extra &&
393 pi->extra->num_labels && !bnc->is_evpn_gwip_nexthop)
394 return bgp_isvalid_nexthop_for_mpls(bnc, pi);
395 else
396 return (bgp_isvalid_nexthop(bnc));
397 }
398
399 void bgp_delete_connected_nexthop(afi_t afi, struct peer *peer)
400 {
401 struct bgp_nexthop_cache *bnc;
402 struct prefix p;
403 ifindex_t ifindex = 0;
404
405 if (!peer)
406 return;
407
408 if (!sockunion2hostprefix(&peer->su, &p))
409 return;
410 /*
411 * Gather the ifindex for if up/down events to be
412 * tagged into this fun
413 */
414 if (afi == AFI_IP6 && IN6_IS_ADDR_LINKLOCAL(&peer->su.sin6.sin6_addr))
415 ifindex = peer->su.sin6.sin6_scope_id;
416 bnc = bnc_find(&peer->bgp->nexthop_cache_table[family2afi(p.family)],
417 &p, 0, ifindex);
418 if (!bnc) {
419 if (BGP_DEBUG(nht, NHT))
420 zlog_debug(
421 "Cannot find connected NHT node for peer %s(%s)",
422 peer->host, peer->bgp->name_pretty);
423 return;
424 }
425
426 if (bnc->nht_info != peer) {
427 if (BGP_DEBUG(nht, NHT))
428 zlog_debug(
429 "Connected NHT %p node for peer %s(%s) points to %p",
430 bnc, peer->host, bnc->bgp->name_pretty,
431 bnc->nht_info);
432 return;
433 }
434
435 bnc->nht_info = NULL;
436
437 if (LIST_EMPTY(&(bnc->paths))) {
438 if (BGP_DEBUG(nht, NHT))
439 zlog_debug(
440 "Freeing connected NHT node %p for peer %s(%s)",
441 bnc, peer->host, bnc->bgp->name_pretty);
442 unregister_zebra_rnh(bnc);
443 bnc_free(bnc);
444 }
445 }
446
447 static void bgp_process_nexthop_update(struct bgp_nexthop_cache *bnc,
448 struct zapi_route *nhr,
449 bool import_check)
450 {
451 struct nexthop *nexthop;
452 struct nexthop *oldnh;
453 struct nexthop *nhlist_head = NULL;
454 struct nexthop *nhlist_tail = NULL;
455 int i;
456 bool evpn_resolved = false;
457
458 bnc->last_update = monotime(NULL);
459 bnc->change_flags = 0;
460
461 /* debug print the input */
462 if (BGP_DEBUG(nht, NHT)) {
463 char bnc_buf[BNC_FLAG_DUMP_SIZE];
464
465 zlog_debug(
466 "%s(%u): Rcvd NH update %pFX(%u)%u) - metric %d/%d #nhops %d/%d flags %s",
467 bnc->bgp->name_pretty, bnc->bgp->vrf_id, &nhr->prefix,
468 bnc->ifindex, bnc->srte_color, nhr->metric, bnc->metric,
469 nhr->nexthop_num, bnc->nexthop_num,
470 bgp_nexthop_dump_bnc_flags(bnc, bnc_buf,
471 sizeof(bnc_buf)));
472 }
473
474 if (nhr->metric != bnc->metric)
475 bnc->change_flags |= BGP_NEXTHOP_METRIC_CHANGED;
476
477 if (nhr->nexthop_num != bnc->nexthop_num)
478 bnc->change_flags |= BGP_NEXTHOP_CHANGED;
479
480 if (import_check && (nhr->type == ZEBRA_ROUTE_BGP ||
481 !prefix_same(&bnc->prefix, &nhr->prefix))) {
482 SET_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED);
483 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
484 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_LABELED_VALID);
485 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_EVPN_INCOMPLETE);
486
487 bnc_nexthop_free(bnc);
488 bnc->nexthop = NULL;
489
490 if (BGP_DEBUG(nht, NHT))
491 zlog_debug(
492 "%s: Import Check does not resolve to the same prefix for %pFX received %pFX or matching route is BGP",
493 __func__, &bnc->prefix, &nhr->prefix);
494 } else if (nhr->nexthop_num) {
495 struct peer *peer = bnc->nht_info;
496
497 /* notify bgp fsm if nbr ip goes from invalid->valid */
498 if (!bnc->nexthop_num)
499 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
500
501 if (!bnc->is_evpn_gwip_nexthop)
502 bnc->flags |= BGP_NEXTHOP_VALID;
503 bnc->metric = nhr->metric;
504 bnc->nexthop_num = nhr->nexthop_num;
505
506 bnc->flags &= ~BGP_NEXTHOP_LABELED_VALID; /* check below */
507
508 for (i = 0; i < nhr->nexthop_num; i++) {
509 int num_labels = 0;
510
511 nexthop = nexthop_from_zapi_nexthop(&nhr->nexthops[i]);
512
513 /*
514 * Turn on RA for the v6 nexthops
515 * we receive from bgp. This is to allow us
516 * to work with v4 routing over v6 nexthops
517 */
518 if (peer && !peer->ifp
519 && CHECK_FLAG(peer->flags,
520 PEER_FLAG_CAPABILITY_ENHE)
521 && nhr->prefix.family == AF_INET6
522 && nexthop->type != NEXTHOP_TYPE_BLACKHOLE) {
523 struct interface *ifp;
524
525 ifp = if_lookup_by_index(nexthop->ifindex,
526 nexthop->vrf_id);
527 if (ifp)
528 zclient_send_interface_radv_req(
529 zclient, nexthop->vrf_id, ifp,
530 true,
531 BGP_UNNUM_DEFAULT_RA_INTERVAL);
532 }
533 /* There is at least one label-switched path */
534 if (nexthop->nh_label &&
535 nexthop->nh_label->num_labels) {
536
537 bnc->flags |= BGP_NEXTHOP_LABELED_VALID;
538 num_labels = nexthop->nh_label->num_labels;
539 }
540
541 if (BGP_DEBUG(nht, NHT)) {
542 char buf[NEXTHOP_STRLEN];
543 zlog_debug(
544 " nhop via %s (%d labels)",
545 nexthop2str(nexthop, buf, sizeof(buf)),
546 num_labels);
547 }
548
549 if (nhlist_tail) {
550 nhlist_tail->next = nexthop;
551 nhlist_tail = nexthop;
552 } else {
553 nhlist_tail = nexthop;
554 nhlist_head = nexthop;
555 }
556
557 /* No need to evaluate the nexthop if we have already
558 * determined
559 * that there has been a change.
560 */
561 if (bnc->change_flags & BGP_NEXTHOP_CHANGED)
562 continue;
563
564 for (oldnh = bnc->nexthop; oldnh; oldnh = oldnh->next)
565 if (nexthop_same(oldnh, nexthop))
566 break;
567
568 if (!oldnh)
569 bnc->change_flags |= BGP_NEXTHOP_CHANGED;
570 }
571 bnc_nexthop_free(bnc);
572 bnc->nexthop = nhlist_head;
573
574 /*
575 * Gateway IP nexthop is L3 reachable. Mark it as
576 * BGP_NEXTHOP_VALID only if it is recursively resolved with a
577 * remote EVPN RT-2.
578 * Else, mark it as BGP_NEXTHOP_EVPN_INCOMPLETE.
579 * When its mapping with EVPN RT-2 is established, unset
580 * BGP_NEXTHOP_EVPN_INCOMPLETE and set BGP_NEXTHOP_VALID.
581 */
582 if (bnc->is_evpn_gwip_nexthop) {
583 evpn_resolved = bgp_evpn_is_gateway_ip_resolved(bnc);
584
585 if (BGP_DEBUG(nht, NHT))
586 zlog_debug(
587 "EVPN gateway IP %pFX recursive MAC/IP lookup %s",
588 &bnc->prefix,
589 (evpn_resolved ? "successful"
590 : "failed"));
591
592 if (evpn_resolved) {
593 bnc->flags |= BGP_NEXTHOP_VALID;
594 bnc->flags &= ~BGP_NEXTHOP_EVPN_INCOMPLETE;
595 bnc->change_flags |= BGP_NEXTHOP_MACIP_CHANGED;
596 } else {
597 bnc->flags |= BGP_NEXTHOP_EVPN_INCOMPLETE;
598 bnc->flags &= ~BGP_NEXTHOP_VALID;
599 }
600 }
601 } else {
602 bnc->flags &= ~BGP_NEXTHOP_EVPN_INCOMPLETE;
603 bnc->flags &= ~BGP_NEXTHOP_VALID;
604 bnc->flags &= ~BGP_NEXTHOP_LABELED_VALID;
605 bnc->nexthop_num = nhr->nexthop_num;
606
607 /* notify bgp fsm if nbr ip goes from valid->invalid */
608 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
609
610 bnc_nexthop_free(bnc);
611 bnc->nexthop = NULL;
612 }
613
614 evaluate_paths(bnc);
615 }
616
617 static void bgp_nht_ifp_table_handle(struct bgp *bgp,
618 struct bgp_nexthop_cache_head *table,
619 struct interface *ifp, bool up)
620 {
621 struct bgp_nexthop_cache *bnc;
622
623 frr_each (bgp_nexthop_cache, table, bnc) {
624 if (bnc->ifindex != ifp->ifindex)
625 continue;
626
627 bnc->last_update = monotime(NULL);
628 bnc->change_flags = 0;
629
630 /*
631 * For interface based routes ( ala the v6 LL routes
632 * that this was written for ) the metric received
633 * for the connected route is 0 not 1.
634 */
635 bnc->metric = 0;
636 if (up) {
637 SET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
638 SET_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED);
639 bnc->nexthop_num = 1;
640 } else {
641 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
642 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
643 SET_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED);
644 bnc->nexthop_num = 0;
645 }
646
647 evaluate_paths(bnc);
648 }
649 }
650 static void bgp_nht_ifp_handle(struct interface *ifp, bool up)
651 {
652 struct bgp *bgp;
653
654 bgp = ifp->vrf->info;
655 if (!bgp)
656 return;
657
658 bgp_nht_ifp_table_handle(bgp, &bgp->nexthop_cache_table[AFI_IP], ifp,
659 up);
660 bgp_nht_ifp_table_handle(bgp, &bgp->import_check_table[AFI_IP], ifp,
661 up);
662 bgp_nht_ifp_table_handle(bgp, &bgp->nexthop_cache_table[AFI_IP6], ifp,
663 up);
664 bgp_nht_ifp_table_handle(bgp, &bgp->import_check_table[AFI_IP6], ifp,
665 up);
666 }
667
668 void bgp_nht_ifp_up(struct interface *ifp)
669 {
670 bgp_nht_ifp_handle(ifp, true);
671 }
672
673 void bgp_nht_ifp_down(struct interface *ifp)
674 {
675 bgp_nht_ifp_handle(ifp, false);
676 }
677
678 static void bgp_nht_ifp_initial(struct thread *thread)
679 {
680 ifindex_t ifindex = THREAD_VAL(thread);
681 struct bgp *bgp = THREAD_ARG(thread);
682 struct interface *ifp = if_lookup_by_index(ifindex, bgp->vrf_id);
683
684 if (!ifp)
685 return;
686
687 if (BGP_DEBUG(nht, NHT))
688 zlog_debug(
689 "Handle NHT initial update for Intf %s(%d) status %s",
690 ifp->name, ifp->ifindex, if_is_up(ifp) ? "up" : "down");
691
692 if (if_is_up(ifp))
693 bgp_nht_ifp_up(ifp);
694 else
695 bgp_nht_ifp_down(ifp);
696 }
697
698 /*
699 * So the bnc code has the ability to handle interface up/down
700 * events to properly handle v6 LL peering.
701 * What is happening here:
702 * The event system for peering expects the nht code to
703 * report on the tracking events after we move to active
704 * So let's give the system a chance to report on that event
705 * in a manner that is expected.
706 */
707 void bgp_nht_interface_events(struct peer *peer)
708 {
709 struct bgp *bgp = peer->bgp;
710 struct bgp_nexthop_cache_head *table;
711 struct bgp_nexthop_cache *bnc;
712 struct prefix p;
713 ifindex_t ifindex = 0;
714
715 if (!IN6_IS_ADDR_LINKLOCAL(&peer->su.sin6.sin6_addr))
716 return;
717
718 if (!sockunion2hostprefix(&peer->su, &p))
719 return;
720 /*
721 * Gather the ifindex for if up/down events to be
722 * tagged into this fun
723 */
724 if (peer->conf_if && IN6_IS_ADDR_LINKLOCAL(&peer->su.sin6.sin6_addr))
725 ifindex = peer->su.sin6.sin6_scope_id;
726
727 table = &bgp->nexthop_cache_table[AFI_IP6];
728 bnc = bnc_find(table, &p, 0, ifindex);
729 if (!bnc)
730 return;
731
732 if (bnc->ifindex)
733 thread_add_event(bm->master, bgp_nht_ifp_initial, bnc->bgp,
734 bnc->ifindex, NULL);
735 }
736
737 void bgp_parse_nexthop_update(int command, vrf_id_t vrf_id)
738 {
739 struct bgp_nexthop_cache_head *tree = NULL;
740 struct bgp_nexthop_cache *bnc_nhc, *bnc_import;
741 struct bgp *bgp;
742 struct prefix match;
743 struct zapi_route nhr;
744 afi_t afi;
745
746 bgp = bgp_lookup_by_vrf_id(vrf_id);
747 if (!bgp) {
748 flog_err(
749 EC_BGP_NH_UPD,
750 "parse nexthop update: instance not found for vrf_id %u",
751 vrf_id);
752 return;
753 }
754
755 if (!zapi_nexthop_update_decode(zclient->ibuf, &match, &nhr)) {
756 zlog_err("%s[%s]: Failure to decode nexthop update", __func__,
757 bgp->name_pretty);
758 return;
759 }
760
761 afi = family2afi(match.family);
762 tree = &bgp->nexthop_cache_table[afi];
763
764 bnc_nhc = bnc_find(tree, &match, nhr.srte_color, 0);
765 if (!bnc_nhc) {
766 if (BGP_DEBUG(nht, NHT))
767 zlog_debug(
768 "parse nexthop update(%pFX(%u)(%s)): bnc info not found for nexthop cache",
769 &nhr.prefix, nhr.srte_color, bgp->name_pretty);
770 } else
771 bgp_process_nexthop_update(bnc_nhc, &nhr, false);
772
773 tree = &bgp->import_check_table[afi];
774
775 bnc_import = bnc_find(tree, &match, nhr.srte_color, 0);
776 if (!bnc_import) {
777 if (BGP_DEBUG(nht, NHT))
778 zlog_debug(
779 "parse nexthop update(%pFX(%u)(%s)): bnc info not found for import check",
780 &nhr.prefix, nhr.srte_color, bgp->name_pretty);
781 } else
782 bgp_process_nexthop_update(bnc_import, &nhr, true);
783
784 /*
785 * HACK: if any BGP route is dependant on an SR-policy that doesn't
786 * exist, zebra will never send NH updates relative to that policy. In
787 * that case, whenever we receive an update about a colorless NH, update
788 * the corresponding colorful NHs that share the same endpoint but that
789 * are inactive. This ugly hack should work around the problem at the
790 * cost of a performance pernalty. Long term, what should be done is to
791 * make zebra's RNH subsystem aware of SR-TE colors (like bgpd is),
792 * which should provide a better infrastructure to solve this issue in
793 * a more efficient and elegant way.
794 */
795 if (nhr.srte_color == 0 && bnc_nhc) {
796 struct bgp_nexthop_cache *bnc_iter;
797
798 frr_each (bgp_nexthop_cache, &bgp->nexthop_cache_table[afi],
799 bnc_iter) {
800 if (!prefix_same(&bnc_nhc->prefix, &bnc_iter->prefix) ||
801 bnc_iter->srte_color == 0 ||
802 CHECK_FLAG(bnc_iter->flags, BGP_NEXTHOP_VALID))
803 continue;
804
805 bgp_process_nexthop_update(bnc_iter, &nhr, false);
806 }
807 }
808 }
809
810 /*
811 * Cleanup nexthop registration and status information for BGP nexthops
812 * pertaining to this VRF. This is invoked upon VRF deletion.
813 */
814 void bgp_cleanup_nexthops(struct bgp *bgp)
815 {
816 for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++) {
817 struct bgp_nexthop_cache *bnc;
818
819 frr_each (bgp_nexthop_cache, &bgp->nexthop_cache_table[afi],
820 bnc) {
821 /* Clear relevant flags. */
822 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
823 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
824 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
825 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_EVPN_INCOMPLETE);
826 }
827 }
828 }
829
830 /**
831 * make_prefix - make a prefix structure from the path (essentially
832 * path's node.
833 */
834 static int make_prefix(int afi, struct bgp_path_info *pi, struct prefix *p)
835 {
836
837 int is_bgp_static = ((pi->type == ZEBRA_ROUTE_BGP)
838 && (pi->sub_type == BGP_ROUTE_STATIC))
839 ? 1
840 : 0;
841 struct bgp_dest *net = pi->net;
842 const struct prefix *p_orig = bgp_dest_get_prefix(net);
843 struct in_addr ipv4;
844
845 if (p_orig->family == AF_FLOWSPEC) {
846 if (!pi->peer)
847 return -1;
848 return bgp_flowspec_get_first_nh(pi->peer->bgp,
849 pi, p, afi);
850 }
851 memset(p, 0, sizeof(struct prefix));
852 switch (afi) {
853 case AFI_IP:
854 p->family = AF_INET;
855 if (is_bgp_static) {
856 p->u.prefix4 = p_orig->u.prefix4;
857 p->prefixlen = p_orig->prefixlen;
858 } else {
859 if (IS_MAPPED_IPV6(&pi->attr->mp_nexthop_global)) {
860 ipv4_mapped_ipv6_to_ipv4(
861 &pi->attr->mp_nexthop_global, &ipv4);
862 p->u.prefix4 = ipv4;
863 p->prefixlen = IPV4_MAX_BITLEN;
864 } else {
865 if (p_orig->family == AF_EVPN)
866 p->u.prefix4 =
867 pi->attr->mp_nexthop_global_in;
868 else
869 p->u.prefix4 = pi->attr->nexthop;
870 p->prefixlen = IPV4_MAX_BITLEN;
871 }
872 }
873 break;
874 case AFI_IP6:
875 p->family = AF_INET6;
876
877 if (is_bgp_static) {
878 p->u.prefix6 = p_orig->u.prefix6;
879 p->prefixlen = p_orig->prefixlen;
880 } else {
881 /* If we receive MP_REACH nexthop with ::(LL)
882 * or LL(LL), use LL address as nexthop cache.
883 */
884 if (pi->attr->mp_nexthop_len
885 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
886 && (IN6_IS_ADDR_UNSPECIFIED(
887 &pi->attr->mp_nexthop_global)
888 || IN6_IS_ADDR_LINKLOCAL(
889 &pi->attr->mp_nexthop_global)))
890 p->u.prefix6 = pi->attr->mp_nexthop_local;
891 /* If we receive MR_REACH with (GA)::(LL)
892 * then check for route-map to choose GA or LL
893 */
894 else if (pi->attr->mp_nexthop_len
895 == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
896 if (pi->attr->mp_nexthop_prefer_global)
897 p->u.prefix6 =
898 pi->attr->mp_nexthop_global;
899 else
900 p->u.prefix6 =
901 pi->attr->mp_nexthop_local;
902 } else
903 p->u.prefix6 = pi->attr->mp_nexthop_global;
904 p->prefixlen = IPV6_MAX_BITLEN;
905 }
906 break;
907 default:
908 if (BGP_DEBUG(nht, NHT)) {
909 zlog_debug(
910 "%s: Attempting to make prefix with unknown AFI %d (not %d or %d)",
911 __func__, afi, AFI_IP, AFI_IP6);
912 }
913 break;
914 }
915 return 0;
916 }
917
918 /**
919 * sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
920 * command to Zebra.
921 * ARGUMENTS:
922 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
923 * int command -- command to send to zebra
924 * RETURNS:
925 * void.
926 */
927 static void sendmsg_zebra_rnh(struct bgp_nexthop_cache *bnc, int command)
928 {
929 bool exact_match = false;
930 bool resolve_via_default = false;
931 int ret;
932
933 if (!zclient)
934 return;
935
936 /* Don't try to register if Zebra doesn't know of this instance. */
937 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bnc->bgp)) {
938 if (BGP_DEBUG(zebra, ZEBRA))
939 zlog_debug(
940 "%s: No zebra instance to talk to, not installing NHT entry",
941 __func__);
942 return;
943 }
944
945 if (!bgp_zebra_num_connects()) {
946 if (BGP_DEBUG(zebra, ZEBRA))
947 zlog_debug(
948 "%s: We have not connected yet, cannot send nexthops",
949 __func__);
950 }
951 if (command == ZEBRA_NEXTHOP_REGISTER) {
952 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED))
953 exact_match = true;
954 if (CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH))
955 resolve_via_default = true;
956 }
957
958 if (BGP_DEBUG(zebra, ZEBRA))
959 zlog_debug("%s: sending cmd %s for %pFX (vrf %s)", __func__,
960 zserv_command_string(command), &bnc->prefix,
961 bnc->bgp->name_pretty);
962
963 ret = zclient_send_rnh(zclient, command, &bnc->prefix, SAFI_UNICAST,
964 exact_match, resolve_via_default,
965 bnc->bgp->vrf_id);
966 if (ret == ZCLIENT_SEND_FAILURE) {
967 flog_warn(EC_BGP_ZEBRA_SEND,
968 "sendmsg_nexthop: zclient_send_message() failed");
969 return;
970 }
971
972 if (command == ZEBRA_NEXTHOP_REGISTER)
973 SET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
974 else if (command == ZEBRA_NEXTHOP_UNREGISTER)
975 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
976 return;
977 }
978
979 /**
980 * register_zebra_rnh - register a NH/route with Zebra for notification
981 * when the route or the route to the nexthop changes.
982 * ARGUMENTS:
983 * struct bgp_nexthop_cache *bnc
984 * RETURNS:
985 * void.
986 */
987 static void register_zebra_rnh(struct bgp_nexthop_cache *bnc)
988 {
989 /* Check if we have already registered */
990 if (bnc->flags & BGP_NEXTHOP_REGISTERED)
991 return;
992
993 if (bnc->ifindex) {
994 SET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
995 return;
996 }
997
998 sendmsg_zebra_rnh(bnc, ZEBRA_NEXTHOP_REGISTER);
999 }
1000
1001 /**
1002 * unregister_zebra_rnh -- Unregister the route/nexthop from Zebra.
1003 * ARGUMENTS:
1004 * struct bgp_nexthop_cache *bnc
1005 * RETURNS:
1006 * void.
1007 */
1008 static void unregister_zebra_rnh(struct bgp_nexthop_cache *bnc)
1009 {
1010 /* Check if we have already registered */
1011 if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED))
1012 return;
1013
1014 if (bnc->ifindex) {
1015 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
1016 return;
1017 }
1018
1019 sendmsg_zebra_rnh(bnc, ZEBRA_NEXTHOP_UNREGISTER);
1020 }
1021
1022 /**
1023 * evaluate_paths - Evaluate the paths/nets associated with a nexthop.
1024 * ARGUMENTS:
1025 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
1026 * RETURNS:
1027 * void.
1028 */
1029 void evaluate_paths(struct bgp_nexthop_cache *bnc)
1030 {
1031 struct bgp_dest *dest;
1032 struct bgp_path_info *path;
1033 int afi;
1034 struct peer *peer = (struct peer *)bnc->nht_info;
1035 struct bgp_table *table;
1036 safi_t safi;
1037 struct bgp *bgp_path;
1038 const struct prefix *p;
1039
1040 if (BGP_DEBUG(nht, NHT)) {
1041 char bnc_buf[BNC_FLAG_DUMP_SIZE];
1042 char chg_buf[BNC_FLAG_DUMP_SIZE];
1043
1044 zlog_debug(
1045 "NH update for %pFX(%d)(%u)(%s) - flags %s chgflags %s- evaluate paths",
1046 &bnc->prefix, bnc->ifindex, bnc->srte_color,
1047 bnc->bgp->name_pretty,
1048 bgp_nexthop_dump_bnc_flags(bnc, bnc_buf,
1049 sizeof(bnc_buf)),
1050 bgp_nexthop_dump_bnc_change_flags(bnc, chg_buf,
1051 sizeof(bnc_buf)));
1052 }
1053
1054 LIST_FOREACH (path, &(bnc->paths), nh_thread) {
1055 if (!(path->type == ZEBRA_ROUTE_BGP
1056 && ((path->sub_type == BGP_ROUTE_NORMAL)
1057 || (path->sub_type == BGP_ROUTE_STATIC)
1058 || (path->sub_type == BGP_ROUTE_IMPORTED))))
1059 continue;
1060
1061 dest = path->net;
1062 assert(dest && bgp_dest_table(dest));
1063 p = bgp_dest_get_prefix(dest);
1064 afi = family2afi(p->family);
1065 table = bgp_dest_table(dest);
1066 safi = table->safi;
1067
1068 /*
1069 * handle routes from other VRFs (they can have a
1070 * nexthop in THIS VRF). bgp_path is the bgp instance
1071 * that owns the route referencing this nexthop.
1072 */
1073 bgp_path = table->bgp;
1074
1075 /*
1076 * Path becomes valid/invalid depending on whether the nexthop
1077 * reachable/unreachable.
1078 *
1079 * In case of unicast routes that were imported from vpn
1080 * and that have labels, they are valid only if there are
1081 * nexthops with labels
1082 *
1083 * If the nexthop is EVPN gateway-IP,
1084 * do not check for a valid label.
1085 */
1086
1087 bool bnc_is_valid_nexthop = false;
1088 bool path_valid = false;
1089
1090 if (safi == SAFI_UNICAST && path->sub_type == BGP_ROUTE_IMPORTED
1091 && path->extra && path->extra->num_labels
1092 && (path->attr->evpn_overlay.type
1093 != OVERLAY_INDEX_GATEWAY_IP)) {
1094 bnc_is_valid_nexthop =
1095 bgp_isvalid_nexthop_for_mpls(bnc, path) ? true
1096 : false;
1097 } else {
1098 if (bgp_update_martian_nexthop(
1099 bnc->bgp, afi, safi, path->type,
1100 path->sub_type, path->attr, dest)) {
1101 if (BGP_DEBUG(nht, NHT))
1102 zlog_debug(
1103 "%s: prefix %pBD (vrf %s), ignoring path due to martian or self-next-hop",
1104 __func__, dest, bgp_path->name);
1105 } else
1106 bnc_is_valid_nexthop =
1107 bgp_isvalid_nexthop(bnc) ? true : false;
1108 }
1109
1110 if (BGP_DEBUG(nht, NHT)) {
1111 char buf1[RD_ADDRSTRLEN];
1112
1113 if (dest->pdest) {
1114 prefix_rd2str((struct prefix_rd *)bgp_dest_get_prefix(dest->pdest),
1115 buf1, sizeof(buf1));
1116 zlog_debug(
1117 "... eval path %d/%d %pBD RD %s %s flags 0x%x",
1118 afi, safi, dest, buf1,
1119 bgp_path->name_pretty, path->flags);
1120 } else
1121 zlog_debug(
1122 "... eval path %d/%d %pBD %s flags 0x%x",
1123 afi, safi, dest, bgp_path->name_pretty,
1124 path->flags);
1125 }
1126
1127 /* Skip paths marked for removal or as history. */
1128 if (CHECK_FLAG(path->flags, BGP_PATH_REMOVED)
1129 || CHECK_FLAG(path->flags, BGP_PATH_HISTORY))
1130 continue;
1131
1132 /* Copy the metric to the path. Will be used for bestpath
1133 * computation */
1134 if (bgp_isvalid_nexthop(bnc) && bnc->metric)
1135 (bgp_path_info_extra_get(path))->igpmetric =
1136 bnc->metric;
1137 else if (path->extra)
1138 path->extra->igpmetric = 0;
1139
1140 if (CHECK_FLAG(bnc->change_flags, BGP_NEXTHOP_METRIC_CHANGED)
1141 || CHECK_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED)
1142 || path->attr->srte_color != 0)
1143 SET_FLAG(path->flags, BGP_PATH_IGP_CHANGED);
1144
1145 path_valid = CHECK_FLAG(path->flags, BGP_PATH_VALID);
1146 if (path_valid != bnc_is_valid_nexthop) {
1147 if (path_valid) {
1148 /* No longer valid, clear flag; also for EVPN
1149 * routes, unimport from VRFs if needed.
1150 */
1151 bgp_aggregate_decrement(bgp_path, p, path, afi,
1152 safi);
1153 bgp_path_info_unset_flag(dest, path,
1154 BGP_PATH_VALID);
1155 if (safi == SAFI_EVPN &&
1156 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest)))
1157 bgp_evpn_unimport_route(bgp_path,
1158 afi, safi, bgp_dest_get_prefix(dest), path);
1159 } else {
1160 /* Path becomes valid, set flag; also for EVPN
1161 * routes, import from VRFs if needed.
1162 */
1163 bgp_path_info_set_flag(dest, path,
1164 BGP_PATH_VALID);
1165 bgp_aggregate_increment(bgp_path, p, path, afi,
1166 safi);
1167 if (safi == SAFI_EVPN &&
1168 bgp_evpn_is_prefix_nht_supported(bgp_dest_get_prefix(dest)))
1169 bgp_evpn_import_route(bgp_path,
1170 afi, safi, bgp_dest_get_prefix(dest), path);
1171 }
1172 }
1173
1174 bgp_process(bgp_path, dest, afi, safi);
1175 }
1176
1177 if (peer) {
1178 int valid_nexthops = bgp_isvalid_nexthop(bnc);
1179
1180 if (valid_nexthops) {
1181 /*
1182 * Peering cannot occur across a blackhole nexthop
1183 */
1184 if (bnc->nexthop_num == 1 && bnc->nexthop
1185 && bnc->nexthop->type == NEXTHOP_TYPE_BLACKHOLE) {
1186 peer->last_reset = PEER_DOWN_WAITING_NHT;
1187 valid_nexthops = 0;
1188 } else
1189 peer->last_reset = PEER_DOWN_WAITING_OPEN;
1190 } else
1191 peer->last_reset = PEER_DOWN_WAITING_NHT;
1192
1193 if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED)) {
1194 if (BGP_DEBUG(nht, NHT))
1195 zlog_debug(
1196 "%s: Updating peer (%s(%s)) status with NHT nexthops %d",
1197 __func__, peer->host,
1198 peer->bgp->name_pretty,
1199 !!valid_nexthops);
1200 bgp_fsm_nht_update(peer, !!valid_nexthops);
1201 SET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
1202 }
1203 }
1204
1205 RESET_FLAG(bnc->change_flags);
1206 }
1207
1208 /**
1209 * path_nh_map - make or break path-to-nexthop association.
1210 * ARGUMENTS:
1211 * path - pointer to the path structure
1212 * bnc - pointer to the nexthop structure
1213 * make - if set, make the association. if unset, just break the existing
1214 * association.
1215 */
1216 void path_nh_map(struct bgp_path_info *path, struct bgp_nexthop_cache *bnc,
1217 bool make)
1218 {
1219 if (path->nexthop) {
1220 LIST_REMOVE(path, nh_thread);
1221 path->nexthop->path_count--;
1222 path->nexthop = NULL;
1223 }
1224 if (make) {
1225 LIST_INSERT_HEAD(&(bnc->paths), path, nh_thread);
1226 path->nexthop = bnc;
1227 path->nexthop->path_count++;
1228 }
1229 }
1230
1231 /*
1232 * This function is called to register nexthops to zebra
1233 * as that we may have tried to install the nexthops
1234 * before we actually have a zebra connection
1235 */
1236 void bgp_nht_register_nexthops(struct bgp *bgp)
1237 {
1238 for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++) {
1239 struct bgp_nexthop_cache *bnc;
1240
1241 frr_each (bgp_nexthop_cache, &bgp->nexthop_cache_table[afi],
1242 bnc) {
1243 register_zebra_rnh(bnc);
1244 }
1245 }
1246 }
1247
1248 void bgp_nht_reg_enhe_cap_intfs(struct peer *peer)
1249 {
1250 struct bgp *bgp;
1251 struct bgp_nexthop_cache *bnc;
1252 struct nexthop *nhop;
1253 struct interface *ifp;
1254 struct prefix p;
1255 ifindex_t ifindex = 0;
1256
1257 if (peer->ifp)
1258 return;
1259
1260 bgp = peer->bgp;
1261 if (!sockunion2hostprefix(&peer->su, &p)) {
1262 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1263 __func__, peer->host);
1264 return;
1265 }
1266
1267 if (p.family != AF_INET6)
1268 return;
1269 /*
1270 * Gather the ifindex for if up/down events to be
1271 * tagged into this fun
1272 */
1273 if (peer->conf_if && IN6_IS_ADDR_LINKLOCAL(&peer->su.sin6.sin6_addr))
1274 ifindex = peer->su.sin6.sin6_scope_id;
1275
1276 bnc = bnc_find(&bgp->nexthop_cache_table[AFI_IP6], &p, 0, ifindex);
1277 if (!bnc)
1278 return;
1279
1280 if (peer != bnc->nht_info)
1281 return;
1282
1283 for (nhop = bnc->nexthop; nhop; nhop = nhop->next) {
1284 ifp = if_lookup_by_index(nhop->ifindex, nhop->vrf_id);
1285
1286 if (!ifp)
1287 continue;
1288
1289 zclient_send_interface_radv_req(zclient,
1290 nhop->vrf_id,
1291 ifp, true,
1292 BGP_UNNUM_DEFAULT_RA_INTERVAL);
1293 }
1294 }
1295
1296 void bgp_nht_dereg_enhe_cap_intfs(struct peer *peer)
1297 {
1298 struct bgp *bgp;
1299 struct bgp_nexthop_cache *bnc;
1300 struct nexthop *nhop;
1301 struct interface *ifp;
1302 struct prefix p;
1303 ifindex_t ifindex = 0;
1304
1305 if (peer->ifp)
1306 return;
1307
1308 bgp = peer->bgp;
1309
1310 if (!sockunion2hostprefix(&peer->su, &p)) {
1311 zlog_warn("%s: Unable to convert sockunion to prefix for %s",
1312 __func__, peer->host);
1313 return;
1314 }
1315
1316 if (p.family != AF_INET6)
1317 return;
1318 /*
1319 * Gather the ifindex for if up/down events to be
1320 * tagged into this fun
1321 */
1322 if (peer->conf_if && IN6_IS_ADDR_LINKLOCAL(&peer->su.sin6.sin6_addr))
1323 ifindex = peer->su.sin6.sin6_scope_id;
1324
1325 bnc = bnc_find(&bgp->nexthop_cache_table[AFI_IP6], &p, 0, ifindex);
1326 if (!bnc)
1327 return;
1328
1329 if (peer != bnc->nht_info)
1330 return;
1331
1332 for (nhop = bnc->nexthop; nhop; nhop = nhop->next) {
1333 ifp = if_lookup_by_index(nhop->ifindex, nhop->vrf_id);
1334
1335 if (!ifp)
1336 continue;
1337
1338 zclient_send_interface_radv_req(zclient, nhop->vrf_id, ifp, 0,
1339 0);
1340 }
1341 }
1342
1343 /****************************************************************************
1344 * L3 NHGs are used for fast failover of nexthops in the dplane. These are
1345 * the APIs for allocating L3 NHG ids. Management of the L3 NHG itself is
1346 * left to the application using it.
1347 * PS: Currently EVPN host routes is the only app using L3 NHG for fast
1348 * failover of remote ES links.
1349 ***************************************************************************/
1350 static bitfield_t bgp_nh_id_bitmap;
1351 static uint32_t bgp_l3nhg_start;
1352
1353 /* XXX - currently we do nothing on the callbacks */
1354 static void bgp_l3nhg_add_cb(const char *name)
1355 {
1356 }
1357 static void bgp_l3nhg_add_nexthop_cb(const struct nexthop_group_cmd *nhgc,
1358 const struct nexthop *nhop)
1359 {
1360 }
1361 static void bgp_l3nhg_del_nexthop_cb(const struct nexthop_group_cmd *nhgc,
1362 const struct nexthop *nhop)
1363 {
1364 }
1365 static void bgp_l3nhg_del_cb(const char *name)
1366 {
1367 }
1368
1369 static void bgp_l3nhg_zebra_init(void)
1370 {
1371 static bool bgp_l3nhg_zebra_inited;
1372 if (bgp_l3nhg_zebra_inited)
1373 return;
1374
1375 bgp_l3nhg_zebra_inited = true;
1376 bgp_l3nhg_start = zclient_get_nhg_start(ZEBRA_ROUTE_BGP);
1377 nexthop_group_init(bgp_l3nhg_add_cb, bgp_l3nhg_add_nexthop_cb,
1378 bgp_l3nhg_del_nexthop_cb, bgp_l3nhg_del_cb);
1379 }
1380
1381
1382 void bgp_l3nhg_init(void)
1383 {
1384 uint32_t id_max;
1385
1386 id_max = MIN(ZEBRA_NHG_PROTO_SPACING - 1, 16 * 1024);
1387 bf_init(bgp_nh_id_bitmap, id_max);
1388 bf_assign_zero_index(bgp_nh_id_bitmap);
1389
1390 if (BGP_DEBUG(nht, NHT) || BGP_DEBUG(evpn_mh, EVPN_MH_ES))
1391 zlog_debug("bgp l3_nhg range %u - %u", bgp_l3nhg_start + 1,
1392 bgp_l3nhg_start + id_max);
1393 }
1394
1395 void bgp_l3nhg_finish(void)
1396 {
1397 bf_free(bgp_nh_id_bitmap);
1398 }
1399
1400 uint32_t bgp_l3nhg_id_alloc(void)
1401 {
1402 uint32_t nhg_id = 0;
1403
1404 bgp_l3nhg_zebra_init();
1405 bf_assign_index(bgp_nh_id_bitmap, nhg_id);
1406 if (nhg_id)
1407 nhg_id += bgp_l3nhg_start;
1408
1409 return nhg_id;
1410 }
1411
1412 void bgp_l3nhg_id_free(uint32_t nhg_id)
1413 {
1414 if (!nhg_id || (nhg_id <= bgp_l3nhg_start))
1415 return;
1416
1417 nhg_id -= bgp_l3nhg_start;
1418
1419 bf_release_index(bgp_nh_id_bitmap, nhg_id);
1420 }