]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_nht.c
Merge pull request #6011 from patrasar/pim-no-msdp-group-cmd
[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
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_table.h"
37 #include "bgpd/bgp_route.h"
38 #include "bgpd/bgp_attr.h"
39 #include "bgpd/bgp_nexthop.h"
40 #include "bgpd/bgp_debug.h"
41 #include "bgpd/bgp_errors.h"
42 #include "bgpd/bgp_nht.h"
43 #include "bgpd/bgp_fsm.h"
44 #include "bgpd/bgp_zebra.h"
45 #include "bgpd/bgp_flowspec_util.h"
46 #include "bgpd/bgp_evpn.h"
47
48 extern struct zclient *zclient;
49
50 static void register_zebra_rnh(struct bgp_nexthop_cache *bnc,
51 int is_bgp_static_route);
52 static void unregister_zebra_rnh(struct bgp_nexthop_cache *bnc,
53 int is_bgp_static_route);
54 static void evaluate_paths(struct bgp_nexthop_cache *bnc);
55 static int make_prefix(int afi, struct bgp_path_info *pi, struct prefix *p);
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 }
62
63 static int bgp_isvalid_labeled_nexthop(struct bgp_nexthop_cache *bnc)
64 {
65 return (bgp_zebra_num_connects() == 0
66 || (bnc && CHECK_FLAG(bnc->flags, BGP_NEXTHOP_LABELED_VALID)));
67 }
68
69 static void bgp_unlink_nexthop_check(struct bgp_nexthop_cache *bnc)
70 {
71 if (LIST_EMPTY(&(bnc->paths)) && !bnc->nht_info) {
72 if (BGP_DEBUG(nht, NHT)) {
73 char buf[PREFIX2STR_BUFFER];
74 zlog_debug("bgp_unlink_nexthop: freeing bnc %s(%s)",
75 bnc_str(bnc, buf, PREFIX2STR_BUFFER),
76 bnc->bgp->name_pretty);
77 }
78 unregister_zebra_rnh(bnc,
79 CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE));
80 bgp_node_set_bgp_nexthop_info(bnc->node, NULL);
81 bgp_unlock_node(bnc->node);
82 bnc->node = NULL;
83 bnc_free(bnc);
84 }
85 }
86
87 void bgp_unlink_nexthop(struct bgp_path_info *path)
88 {
89 struct bgp_nexthop_cache *bnc = path->nexthop;
90
91 if (!bnc)
92 return;
93
94 path_nh_map(path, NULL, false);
95
96 bgp_unlink_nexthop_check(bnc);
97 }
98
99 void bgp_unlink_nexthop_by_peer(struct peer *peer)
100 {
101 struct prefix p;
102 struct bgp_node *rn;
103 struct bgp_nexthop_cache *bnc;
104 afi_t afi = family2afi(peer->su.sa.sa_family);
105
106 if (!sockunion2hostprefix(&peer->su, &p))
107 return;
108
109 rn = bgp_node_get(peer->bgp->nexthop_cache_table[afi], &p);
110
111 bnc = bgp_node_get_bgp_nexthop_info(rn);
112 if (!bnc)
113 return;
114
115 /* cleanup the peer reference */
116 bnc->nht_info = NULL;
117
118 bgp_unlink_nexthop_check(bnc);
119 }
120
121 /*
122 * A route and its nexthop might belong to different VRFs. Therefore,
123 * we need both the bgp_route and bgp_nexthop pointers.
124 */
125 int bgp_find_or_add_nexthop(struct bgp *bgp_route, struct bgp *bgp_nexthop,
126 afi_t afi, struct bgp_path_info *pi,
127 struct peer *peer, int connected)
128 {
129 struct bgp_node *rn;
130 struct bgp_nexthop_cache *bnc;
131 struct prefix p;
132 int is_bgp_static_route = 0;
133 const struct prefix *bnc_p;
134
135 if (pi) {
136 is_bgp_static_route = ((pi->type == ZEBRA_ROUTE_BGP)
137 && (pi->sub_type == BGP_ROUTE_STATIC))
138 ? 1
139 : 0;
140
141 /* Since Extended Next-hop Encoding (RFC5549) support, we want
142 to derive
143 address-family from the next-hop. */
144 if (!is_bgp_static_route)
145 afi = BGP_ATTR_NEXTHOP_AFI_IP6(pi->attr) ? AFI_IP6
146 : AFI_IP;
147
148 /* This will return true if the global IPv6 NH is a link local
149 * addr */
150 if (make_prefix(afi, pi, &p) < 0)
151 return 1;
152 } else if (peer) {
153 if (!sockunion2hostprefix(&peer->su, &p)) {
154 if (BGP_DEBUG(nht, NHT)) {
155 zlog_debug(
156 "%s: Attempting to register with unknown AFI %d (not %d or %d)",
157 __func__, afi, AFI_IP, AFI_IP6);
158 }
159 return 0;
160 }
161 } else
162 return 0;
163
164 if (is_bgp_static_route)
165 rn = bgp_node_get(bgp_nexthop->import_check_table[afi], &p);
166 else
167 rn = bgp_node_get(bgp_nexthop->nexthop_cache_table[afi], &p);
168
169 bnc = bgp_node_get_bgp_nexthop_info(rn);
170 if (!bnc) {
171 bnc = bnc_new();
172 bgp_node_set_bgp_nexthop_info(rn, bnc);
173 bnc->node = rn;
174 bnc->bgp = bgp_nexthop;
175 bgp_lock_node(rn);
176 if (BGP_DEBUG(nht, NHT)) {
177 char buf[PREFIX2STR_BUFFER];
178
179 zlog_debug("Allocated bnc %s(%s) peer %p",
180 bnc_str(bnc, buf, PREFIX2STR_BUFFER),
181 bnc->bgp->name_pretty, peer);
182 }
183 }
184
185 bnc_p = bgp_node_get_prefix(bnc->node);
186
187 bgp_unlock_node(rn);
188 if (is_bgp_static_route) {
189 SET_FLAG(bnc->flags, BGP_STATIC_ROUTE);
190
191 /* If we're toggling the type, re-register */
192 if ((CHECK_FLAG(bgp_route->flags, BGP_FLAG_IMPORT_CHECK))
193 && !CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH)) {
194 SET_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH);
195 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
196 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
197 } else if ((!CHECK_FLAG(bgp_route->flags,
198 BGP_FLAG_IMPORT_CHECK))
199 && CHECK_FLAG(bnc->flags,
200 BGP_STATIC_ROUTE_EXACT_MATCH)) {
201 UNSET_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH);
202 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
203 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
204 }
205 }
206 /* When nexthop is already known, but now requires 'connected'
207 * resolution,
208 * re-register it. The reverse scenario where the nexthop currently
209 * requires
210 * 'connected' resolution does not need a re-register (i.e., we treat
211 * 'connected-required' as an override) except in the scenario where
212 * this
213 * is actually a case of tracking a peer for connectivity (e.g., after
214 * disable connected-check).
215 * NOTE: We don't track the number of paths separately for 'connected-
216 * required' vs 'connected-not-required' as this change is not a common
217 * scenario.
218 */
219 else if (connected && !CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) {
220 SET_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED);
221 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
222 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
223 } else if (peer && !connected
224 && CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)) {
225 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED);
226 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
227 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
228 }
229 if (bgp_route->inst_type == BGP_INSTANCE_TYPE_VIEW) {
230 SET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
231 SET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
232 } else if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED)
233 && !is_default_host_route(bnc_p))
234 register_zebra_rnh(bnc, is_bgp_static_route);
235
236 if (pi && pi->nexthop != bnc) {
237 /* Unlink from existing nexthop cache, if any. This will also
238 * free
239 * the nexthop cache entry, if appropriate.
240 */
241 bgp_unlink_nexthop(pi);
242
243 /* updates NHT pi list reference */
244 path_nh_map(pi, bnc, true);
245
246 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID) && bnc->metric)
247 (bgp_path_info_extra_get(pi))->igpmetric = bnc->metric;
248 else if (pi->extra)
249 pi->extra->igpmetric = 0;
250 } else if (peer)
251 bnc->nht_info = (void *)peer; /* NHT peer reference */
252
253 /*
254 * We are cheating here. Views have no associated underlying
255 * ability to detect nexthops. So when we have a view
256 * just tell everyone the nexthop is valid
257 */
258 if (bgp_route->inst_type == BGP_INSTANCE_TYPE_VIEW)
259 return 1;
260 else
261 return (bgp_isvalid_nexthop(bnc));
262 }
263
264 void bgp_delete_connected_nexthop(afi_t afi, struct peer *peer)
265 {
266 struct bgp_node *rn;
267 struct bgp_nexthop_cache *bnc;
268 struct prefix p;
269
270 if (!peer)
271 return;
272
273 if (!sockunion2hostprefix(&peer->su, &p))
274 return;
275
276 rn = bgp_node_lookup(
277 peer->bgp->nexthop_cache_table[family2afi(p.family)], &p);
278 if (!rn) {
279 if (BGP_DEBUG(nht, NHT))
280 zlog_debug(
281 "Cannot find connected NHT node for peer %s(%s)",
282 peer->host, peer->bgp->name_pretty);
283 return;
284 }
285
286 bnc = bgp_node_get_bgp_nexthop_info(rn);
287 if (!bnc) {
288 if (BGP_DEBUG(nht, NHT))
289 zlog_debug(
290 "Cannot find connected NHT node for peer %s(%s) on route_node as expected",
291 peer->host, peer->bgp->name_pretty);
292 bgp_unlock_node(rn);
293 return;
294 }
295 bgp_unlock_node(rn);
296
297 if (bnc->nht_info != peer) {
298 if (BGP_DEBUG(nht, NHT))
299 zlog_debug(
300 "Connected NHT %p node for peer %s(%s) points to %p",
301 bnc, peer->host, bnc->bgp->name_pretty,
302 bnc->nht_info);
303 return;
304 }
305
306 bnc->nht_info = NULL;
307
308 if (LIST_EMPTY(&(bnc->paths))) {
309 if (BGP_DEBUG(nht, NHT))
310 zlog_debug(
311 "Freeing connected NHT node %p for peer %s(%s)",
312 bnc, peer->host, bnc->bgp->name_pretty);
313 unregister_zebra_rnh(bnc, 0);
314 bgp_node_set_bgp_nexthop_info(bnc->node, NULL);
315 bgp_unlock_node(bnc->node);
316 bnc_free(bnc);
317 }
318 }
319
320 void bgp_parse_nexthop_update(int command, vrf_id_t vrf_id)
321 {
322 struct bgp_node *rn = NULL;
323 struct bgp_nexthop_cache *bnc;
324 struct nexthop *nexthop;
325 struct nexthop *oldnh;
326 struct nexthop *nhlist_head = NULL;
327 struct nexthop *nhlist_tail = NULL;
328 int i;
329 struct bgp *bgp;
330 struct zapi_route nhr;
331
332 bgp = bgp_lookup_by_vrf_id(vrf_id);
333 if (!bgp) {
334 flog_err(
335 EC_BGP_NH_UPD,
336 "parse nexthop update: instance not found for vrf_id %u",
337 vrf_id);
338 return;
339 }
340
341 if (!zapi_nexthop_update_decode(zclient->ibuf, &nhr)) {
342 if (BGP_DEBUG(nht, NHT))
343 zlog_debug("%s[%s]: Failure to decode nexthop update",
344 __func__, bgp->name_pretty);
345 return;
346 }
347
348 if (command == ZEBRA_NEXTHOP_UPDATE)
349 rn = bgp_node_lookup(
350 bgp->nexthop_cache_table[family2afi(nhr.prefix.family)],
351 &nhr.prefix);
352 else if (command == ZEBRA_IMPORT_CHECK_UPDATE)
353 rn = bgp_node_lookup(
354 bgp->import_check_table[family2afi(nhr.prefix.family)],
355 &nhr.prefix);
356
357 if (!rn) {
358 if (BGP_DEBUG(nht, NHT)) {
359 char buf[PREFIX2STR_BUFFER];
360 prefix2str(&nhr.prefix, buf, sizeof(buf));
361 zlog_debug("parse nexthop update(%s(%s)): rn not found",
362 buf, bgp->name_pretty);
363 }
364 return;
365 }
366
367 bnc = bgp_node_get_bgp_nexthop_info(rn);
368 if (!bnc) {
369 if (BGP_DEBUG(nht, NHT)) {
370 char buf[PREFIX2STR_BUFFER];
371
372 prefix2str(&nhr.prefix, buf, sizeof(buf));
373 zlog_debug(
374 "parse nexthop update(%s(%s)): bnc node info not found",
375 buf, bgp->name_pretty);
376 }
377 bgp_unlock_node(rn);
378 return;
379 }
380
381 bgp_unlock_node(rn);
382 bnc->last_update = bgp_clock();
383 bnc->change_flags = 0;
384
385 /* debug print the input */
386 if (BGP_DEBUG(nht, NHT)) {
387 char buf[PREFIX2STR_BUFFER];
388 prefix2str(&nhr.prefix, buf, sizeof(buf));
389 zlog_debug(
390 "%s(%u): Rcvd NH update %s - metric %d/%d #nhops %d/%d flags 0x%x",
391 bnc->bgp->name_pretty, vrf_id, buf, nhr.metric,
392 bnc->metric, nhr.nexthop_num, bnc->nexthop_num,
393 bnc->flags);
394 }
395
396 if (nhr.metric != bnc->metric)
397 bnc->change_flags |= BGP_NEXTHOP_METRIC_CHANGED;
398
399 if (nhr.nexthop_num != bnc->nexthop_num)
400 bnc->change_flags |= BGP_NEXTHOP_CHANGED;
401
402 if (nhr.nexthop_num) {
403 struct peer *peer = bnc->nht_info;
404
405 /* notify bgp fsm if nbr ip goes from invalid->valid */
406 if (!bnc->nexthop_num)
407 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
408
409 bnc->flags |= BGP_NEXTHOP_VALID;
410 bnc->metric = nhr.metric;
411 bnc->nexthop_num = nhr.nexthop_num;
412
413 bnc->flags &= ~BGP_NEXTHOP_LABELED_VALID; /* check below */
414
415 for (i = 0; i < nhr.nexthop_num; i++) {
416 int num_labels = 0;
417
418 nexthop = nexthop_from_zapi_nexthop(&nhr.nexthops[i]);
419
420 /*
421 * Turn on RA for the v6 nexthops
422 * we receive from bgp. This is to allow us
423 * to work with v4 routing over v6 nexthops
424 */
425 if (peer && !peer->ifp
426 && CHECK_FLAG(peer->flags,
427 PEER_FLAG_CAPABILITY_ENHE)
428 && nhr.prefix.family == AF_INET6
429 && nexthop->type != NEXTHOP_TYPE_BLACKHOLE) {
430 struct interface *ifp;
431
432 ifp = if_lookup_by_index(nexthop->ifindex,
433 nexthop->vrf_id);
434 if (ifp)
435 zclient_send_interface_radv_req(
436 zclient, nexthop->vrf_id, ifp,
437 true,
438 BGP_UNNUM_DEFAULT_RA_INTERVAL);
439 }
440 /* There is at least one label-switched path */
441 if (nexthop->nh_label &&
442 nexthop->nh_label->num_labels) {
443
444 bnc->flags |= BGP_NEXTHOP_LABELED_VALID;
445 num_labels = nexthop->nh_label->num_labels;
446 }
447
448 if (BGP_DEBUG(nht, NHT)) {
449 char buf[NEXTHOP_STRLEN];
450 zlog_debug(
451 " nhop via %s (%d labels)",
452 nexthop2str(nexthop, buf, sizeof(buf)),
453 num_labels);
454 }
455
456 if (nhlist_tail) {
457 nhlist_tail->next = nexthop;
458 nhlist_tail = nexthop;
459 } else {
460 nhlist_tail = nexthop;
461 nhlist_head = nexthop;
462 }
463
464 /* No need to evaluate the nexthop if we have already
465 * determined
466 * that there has been a change.
467 */
468 if (bnc->change_flags & BGP_NEXTHOP_CHANGED)
469 continue;
470
471 for (oldnh = bnc->nexthop; oldnh; oldnh = oldnh->next)
472 if (nexthop_same(oldnh, nexthop))
473 break;
474
475 if (!oldnh)
476 bnc->change_flags |= BGP_NEXTHOP_CHANGED;
477 }
478 bnc_nexthop_free(bnc);
479 bnc->nexthop = nhlist_head;
480 } else {
481 bnc->flags &= ~BGP_NEXTHOP_VALID;
482 bnc->nexthop_num = nhr.nexthop_num;
483
484 /* notify bgp fsm if nbr ip goes from valid->invalid */
485 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
486
487 bnc_nexthop_free(bnc);
488 bnc->nexthop = NULL;
489 }
490
491 evaluate_paths(bnc);
492 }
493
494 /*
495 * Cleanup nexthop registration and status information for BGP nexthops
496 * pertaining to this VRF. This is invoked upon VRF deletion.
497 */
498 void bgp_cleanup_nexthops(struct bgp *bgp)
499 {
500 afi_t afi;
501 struct bgp_node *rn;
502 struct bgp_nexthop_cache *bnc;
503
504 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
505 if (!bgp->nexthop_cache_table[afi])
506 continue;
507
508 for (rn = bgp_table_top(bgp->nexthop_cache_table[afi]); rn;
509 rn = bgp_route_next(rn)) {
510 bnc = bgp_node_get_bgp_nexthop_info(rn);
511 if (!bnc)
512 continue;
513
514 /* Clear relevant flags. */
515 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_VALID);
516 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
517 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
518 }
519 }
520 }
521
522 /**
523 * make_prefix - make a prefix structure from the path (essentially
524 * path's node.
525 */
526 static int make_prefix(int afi, struct bgp_path_info *pi, struct prefix *p)
527 {
528
529 int is_bgp_static = ((pi->type == ZEBRA_ROUTE_BGP)
530 && (pi->sub_type == BGP_ROUTE_STATIC))
531 ? 1
532 : 0;
533 struct bgp_node *net = pi->net;
534 const struct prefix *p_orig = bgp_node_get_prefix(net);
535
536 if (p_orig->family == AF_FLOWSPEC) {
537 if (!pi->peer)
538 return -1;
539 return bgp_flowspec_get_first_nh(pi->peer->bgp,
540 pi, p);
541 }
542 memset(p, 0, sizeof(struct prefix));
543 switch (afi) {
544 case AFI_IP:
545 p->family = AF_INET;
546 if (is_bgp_static) {
547 p->u.prefix4 = p_orig->u.prefix4;
548 p->prefixlen = p_orig->prefixlen;
549 } else {
550 p->u.prefix4 = pi->attr->nexthop;
551 p->prefixlen = IPV4_MAX_BITLEN;
552 }
553 break;
554 case AFI_IP6:
555 p->family = AF_INET6;
556
557 if (is_bgp_static) {
558 p->u.prefix6 = p_orig->u.prefix6;
559 p->prefixlen = p_orig->prefixlen;
560 } else {
561 p->u.prefix6 = pi->attr->mp_nexthop_global;
562 p->prefixlen = IPV6_MAX_BITLEN;
563 }
564 break;
565 default:
566 if (BGP_DEBUG(nht, NHT)) {
567 zlog_debug(
568 "%s: Attempting to make prefix with unknown AFI %d (not %d or %d)",
569 __func__, afi, AFI_IP, AFI_IP6);
570 }
571 break;
572 }
573 return 0;
574 }
575
576 /**
577 * sendmsg_zebra_rnh -- Format and send a nexthop register/Unregister
578 * command to Zebra.
579 * ARGUMENTS:
580 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
581 * int command -- command to send to zebra
582 * RETURNS:
583 * void.
584 */
585 static void sendmsg_zebra_rnh(struct bgp_nexthop_cache *bnc, int command)
586 {
587 const struct prefix *p;
588 bool exact_match = false;
589 int ret;
590
591 if (!zclient)
592 return;
593
594 /* Don't try to register if Zebra doesn't know of this instance. */
595 if (!IS_BGP_INST_KNOWN_TO_ZEBRA(bnc->bgp)) {
596 if (BGP_DEBUG(zebra, ZEBRA))
597 zlog_debug(
598 "%s: No zebra instance to talk to, not installing NHT entry",
599 __func__);
600 return;
601 }
602
603 if (!bgp_zebra_num_connects()) {
604 if (BGP_DEBUG(zebra, ZEBRA))
605 zlog_debug(
606 "%s: We have not connected yet, cannot send nexthops",
607 __func__);
608 }
609 p = bgp_node_get_prefix(bnc->node);
610 if ((command == ZEBRA_NEXTHOP_REGISTER
611 || command == ZEBRA_IMPORT_ROUTE_REGISTER)
612 && (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_CONNECTED)
613 || CHECK_FLAG(bnc->flags, BGP_STATIC_ROUTE_EXACT_MATCH)))
614 exact_match = true;
615
616 if (BGP_DEBUG(zebra, ZEBRA)) {
617 char buf[PREFIX2STR_BUFFER];
618
619 prefix2str(p, buf, PREFIX2STR_BUFFER);
620 zlog_debug("%s: sending cmd %s for %s (vrf %s)",
621 __func__, zserv_command_string(command), buf,
622 bnc->bgp->name_pretty);
623 }
624
625 ret = zclient_send_rnh(zclient, command, p, exact_match,
626 bnc->bgp->vrf_id);
627 /* TBD: handle the failure */
628 if (ret < 0)
629 flog_warn(EC_BGP_ZEBRA_SEND,
630 "sendmsg_nexthop: zclient_send_message() failed");
631
632 if ((command == ZEBRA_NEXTHOP_REGISTER)
633 || (command == ZEBRA_IMPORT_ROUTE_REGISTER))
634 SET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
635 else if ((command == ZEBRA_NEXTHOP_UNREGISTER)
636 || (command == ZEBRA_IMPORT_ROUTE_UNREGISTER))
637 UNSET_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED);
638 return;
639 }
640
641 /**
642 * register_zebra_rnh - register a NH/route with Zebra for notification
643 * when the route or the route to the nexthop changes.
644 * ARGUMENTS:
645 * struct bgp_nexthop_cache *bnc
646 * RETURNS:
647 * void.
648 */
649 static void register_zebra_rnh(struct bgp_nexthop_cache *bnc,
650 int is_bgp_import_route)
651 {
652 /* Check if we have already registered */
653 if (bnc->flags & BGP_NEXTHOP_REGISTERED)
654 return;
655 if (is_bgp_import_route)
656 sendmsg_zebra_rnh(bnc, ZEBRA_IMPORT_ROUTE_REGISTER);
657 else
658 sendmsg_zebra_rnh(bnc, ZEBRA_NEXTHOP_REGISTER);
659 }
660
661 /**
662 * unregister_zebra_rnh -- Unregister the route/nexthop from Zebra.
663 * ARGUMENTS:
664 * struct bgp_nexthop_cache *bnc
665 * RETURNS:
666 * void.
667 */
668 static void unregister_zebra_rnh(struct bgp_nexthop_cache *bnc,
669 int is_bgp_import_route)
670 {
671 /* Check if we have already registered */
672 if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_REGISTERED))
673 return;
674
675 if (is_bgp_import_route)
676 sendmsg_zebra_rnh(bnc, ZEBRA_IMPORT_ROUTE_UNREGISTER);
677 else
678 sendmsg_zebra_rnh(bnc, ZEBRA_NEXTHOP_UNREGISTER);
679 }
680
681 /**
682 * evaluate_paths - Evaluate the paths/nets associated with a nexthop.
683 * ARGUMENTS:
684 * struct bgp_nexthop_cache *bnc -- the nexthop structure.
685 * RETURNS:
686 * void.
687 */
688 static void evaluate_paths(struct bgp_nexthop_cache *bnc)
689 {
690 struct bgp_node *rn;
691 struct bgp_path_info *path;
692 int afi;
693 struct peer *peer = (struct peer *)bnc->nht_info;
694 struct bgp_table *table;
695 safi_t safi;
696 struct bgp *bgp_path;
697 const struct prefix *p;
698
699 if (BGP_DEBUG(nht, NHT)) {
700 char buf[PREFIX2STR_BUFFER];
701 bnc_str(bnc, buf, PREFIX2STR_BUFFER);
702 zlog_debug(
703 "NH update for %s(%s) - flags 0x%x chgflags 0x%x - evaluate paths",
704 buf, bnc->bgp->name_pretty, bnc->flags,
705 bnc->change_flags);
706 }
707
708 LIST_FOREACH (path, &(bnc->paths), nh_thread) {
709 if (!(path->type == ZEBRA_ROUTE_BGP
710 && ((path->sub_type == BGP_ROUTE_NORMAL)
711 || (path->sub_type == BGP_ROUTE_STATIC)
712 || (path->sub_type == BGP_ROUTE_IMPORTED))))
713 continue;
714
715 rn = path->net;
716 assert(rn && bgp_node_table(rn));
717 p = bgp_node_get_prefix(rn);
718 afi = family2afi(p->family);
719 table = bgp_node_table(rn);
720 safi = table->safi;
721
722 /*
723 * handle routes from other VRFs (they can have a
724 * nexthop in THIS VRF). bgp_path is the bgp instance
725 * that owns the route referencing this nexthop.
726 */
727 bgp_path = table->bgp;
728
729 /*
730 * Path becomes valid/invalid depending on whether the nexthop
731 * reachable/unreachable.
732 *
733 * In case of unicast routes that were imported from vpn
734 * and that have labels, they are valid only if there are
735 * nexthops with labels
736 */
737
738 int bnc_is_valid_nexthop = 0;
739
740 if (safi == SAFI_UNICAST &&
741 path->sub_type == BGP_ROUTE_IMPORTED &&
742 path->extra &&
743 path->extra->num_labels) {
744
745 bnc_is_valid_nexthop =
746 bgp_isvalid_labeled_nexthop(bnc) ? 1 : 0;
747 } else {
748 bnc_is_valid_nexthop =
749 bgp_isvalid_nexthop(bnc) ? 1 : 0;
750 }
751
752 if (BGP_DEBUG(nht, NHT))
753 zlog_debug("%s: prefix %pRN (vrf %s) %svalid", __func__,
754 rn, bgp_path->name,
755 (bnc_is_valid_nexthop ? "" : "not "));
756
757 if ((CHECK_FLAG(path->flags, BGP_PATH_VALID) ? 1 : 0)
758 != bnc_is_valid_nexthop) {
759 if (CHECK_FLAG(path->flags, BGP_PATH_VALID)) {
760 bgp_aggregate_decrement(bgp_path, p, path, afi,
761 safi);
762 bgp_path_info_unset_flag(rn, path,
763 BGP_PATH_VALID);
764 } else {
765 bgp_path_info_set_flag(rn, path,
766 BGP_PATH_VALID);
767 bgp_aggregate_increment(bgp_path, p, path, afi,
768 safi);
769 }
770 }
771
772 /* Copy the metric to the path. Will be used for bestpath
773 * computation */
774 if (bgp_isvalid_nexthop(bnc) && bnc->metric)
775 (bgp_path_info_extra_get(path))->igpmetric =
776 bnc->metric;
777 else if (path->extra)
778 path->extra->igpmetric = 0;
779
780 if (CHECK_FLAG(bnc->change_flags, BGP_NEXTHOP_METRIC_CHANGED)
781 || CHECK_FLAG(bnc->change_flags, BGP_NEXTHOP_CHANGED))
782 SET_FLAG(path->flags, BGP_PATH_IGP_CHANGED);
783
784 if (safi == SAFI_EVPN && bgp_evpn_is_prefix_nht_supported(p)) {
785 if (CHECK_FLAG(path->flags, BGP_PATH_VALID))
786 bgp_evpn_import_route(bgp_path, afi, safi, p,
787 path);
788 else
789 bgp_evpn_unimport_route(bgp_path, afi, safi, p,
790 path);
791 }
792
793 bgp_process(bgp_path, rn, afi, safi);
794 }
795
796 if (peer) {
797 int valid_nexthops = bgp_isvalid_nexthop(bnc);
798
799 if (valid_nexthops)
800 peer->last_reset = PEER_DOWN_WAITING_OPEN;
801 else
802 peer->last_reset = PEER_DOWN_WAITING_NHT;
803
804 if (!CHECK_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED)) {
805 if (BGP_DEBUG(nht, NHT))
806 zlog_debug(
807 "%s: Updating peer (%s(%s)) status with NHT",
808 __func__, peer->host,
809 peer->bgp->name_pretty);
810 bgp_fsm_event_update(peer, valid_nexthops);
811 SET_FLAG(bnc->flags, BGP_NEXTHOP_PEER_NOTIFIED);
812 }
813 }
814
815 RESET_FLAG(bnc->change_flags);
816 }
817
818 /**
819 * path_nh_map - make or break path-to-nexthop association.
820 * ARGUMENTS:
821 * path - pointer to the path structure
822 * bnc - pointer to the nexthop structure
823 * make - if set, make the association. if unset, just break the existing
824 * association.
825 */
826 void path_nh_map(struct bgp_path_info *path, struct bgp_nexthop_cache *bnc,
827 bool make)
828 {
829 if (path->nexthop) {
830 LIST_REMOVE(path, nh_thread);
831 path->nexthop->path_count--;
832 path->nexthop = NULL;
833 }
834 if (make) {
835 LIST_INSERT_HEAD(&(bnc->paths), path, nh_thread);
836 path->nexthop = bnc;
837 path->nexthop->path_count++;
838 }
839 }
840
841 /*
842 * This function is called to register nexthops to zebra
843 * as that we may have tried to install the nexthops
844 * before we actually have a zebra connection
845 */
846 void bgp_nht_register_nexthops(struct bgp *bgp)
847 {
848 struct bgp_node *rn;
849 struct bgp_nexthop_cache *bnc;
850 afi_t afi;
851
852 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
853 if (!bgp->nexthop_cache_table[afi])
854 continue;
855
856 for (rn = bgp_table_top(bgp->nexthop_cache_table[afi]); rn;
857 rn = bgp_route_next(rn)) {
858 bnc = bgp_node_get_bgp_nexthop_info(rn);
859
860 if (!bnc)
861 continue;
862
863 register_zebra_rnh(bnc, 0);
864 }
865 }
866 }
867
868 void bgp_nht_register_enhe_capability_interfaces(struct peer *peer)
869 {
870 struct bgp *bgp;
871 struct bgp_node *rn;
872 struct bgp_nexthop_cache *bnc;
873 struct nexthop *nhop;
874 struct interface *ifp;
875 struct prefix p;
876
877 if (peer->ifp)
878 return;
879
880 bgp = peer->bgp;
881
882 if (!bgp->nexthop_cache_table[AFI_IP6])
883 return;
884
885 if (!sockunion2hostprefix(&peer->su, &p)) {
886 if (BGP_DEBUG(nht, NHT))
887 zlog_debug("%s: Unable to convert prefix to sockunion",
888 __func__);
889 return;
890 }
891
892 if (p.family != AF_INET6)
893 return;
894 rn = bgp_node_lookup(bgp->nexthop_cache_table[AFI_IP6], &p);
895 if (!rn)
896 return;
897
898 bnc = bgp_node_get_bgp_nexthop_info(rn);
899 if (!bnc)
900 return;
901
902 if (peer != bnc->nht_info)
903 return;
904
905 for (nhop = bnc->nexthop; nhop; nhop = nhop->next) {
906 ifp = if_lookup_by_index(nhop->ifindex, nhop->vrf_id);
907
908 if (!ifp)
909 continue;
910
911 zclient_send_interface_radv_req(zclient,
912 nhop->vrf_id,
913 ifp, true,
914 BGP_UNNUM_DEFAULT_RA_INTERVAL);
915 }
916 }