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