]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_rnh.c
Merge pull request #7460 from pguibert6WIND/remove_bgp_constraint
[mirror_frr.git] / zebra / zebra_rnh.c
1 /* Zebra next hop tracking code
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 "prefix.h"
24 #include "table.h"
25 #include "memory.h"
26 #include "command.h"
27 #include "if.h"
28 #include "log.h"
29 #include "sockunion.h"
30 #include "linklist.h"
31 #include "thread.h"
32 #include "workqueue.h"
33 #include "prefix.h"
34 #include "routemap.h"
35 #include "stream.h"
36 #include "nexthop.h"
37 #include "vrf.h"
38
39 #include "zebra/zebra_router.h"
40 #include "zebra/rib.h"
41 #include "zebra/rt.h"
42 #include "zebra/zserv.h"
43 #include "zebra/zebra_ns.h"
44 #include "zebra/zebra_vrf.h"
45 #include "zebra/redistribute.h"
46 #include "zebra/debug.h"
47 #include "zebra/zebra_rnh.h"
48 #include "zebra/zebra_routemap.h"
49 #include "zebra/zebra_srte.h"
50 #include "zebra/interface.h"
51 #include "zebra/zebra_memory.h"
52 #include "zebra/zebra_errors.h"
53
54 DEFINE_MTYPE_STATIC(ZEBRA, RNH, "Nexthop tracking object")
55
56 static void free_state(vrf_id_t vrf_id, struct route_entry *re,
57 struct route_node *rn);
58 static void copy_state(struct rnh *rnh, const struct route_entry *re,
59 struct route_node *rn);
60 static int compare_state(struct route_entry *r1, struct route_entry *r2);
61 static void print_rnh(struct route_node *rn, struct vty *vty);
62 static int zebra_client_cleanup_rnh(struct zserv *client);
63
64 void zebra_rnh_init(void)
65 {
66 hook_register(zserv_client_close, zebra_client_cleanup_rnh);
67 }
68
69 static inline struct route_table *get_rnh_table(vrf_id_t vrfid, afi_t afi,
70 enum rnh_type type)
71 {
72 struct zebra_vrf *zvrf;
73 struct route_table *t = NULL;
74
75 zvrf = zebra_vrf_lookup_by_id(vrfid);
76 if (zvrf)
77 switch (type) {
78 case RNH_NEXTHOP_TYPE:
79 t = zvrf->rnh_table[afi];
80 break;
81 case RNH_IMPORT_CHECK_TYPE:
82 t = zvrf->import_check_table[afi];
83 break;
84 }
85
86 return t;
87 }
88
89 static void zebra_rnh_remove_from_routing_table(struct rnh *rnh)
90 {
91 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(rnh->vrf_id);
92 struct route_table *table = zvrf->table[rnh->afi][SAFI_UNICAST];
93 struct route_node *rn;
94 rib_dest_t *dest;
95
96 if (!table)
97 return;
98
99 rn = route_node_match(table, &rnh->resolved_route);
100 if (!rn)
101 return;
102
103 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
104 zlog_debug("%s: %s(%u):%pRN removed from tracking on %pRN",
105 __func__, VRF_LOGNAME(zvrf->vrf), rnh->vrf_id,
106 rnh->node, rn);
107
108 dest = rib_dest_from_rnode(rn);
109 rnh_list_del(&dest->nht, rnh);
110 route_unlock_node(rn);
111 }
112
113 static void zebra_rnh_store_in_routing_table(struct rnh *rnh)
114 {
115 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(rnh->vrf_id);
116 struct route_table *table = zvrf->table[rnh->afi][SAFI_UNICAST];
117 struct route_node *rn;
118 rib_dest_t *dest;
119
120 rn = route_node_match(table, &rnh->resolved_route);
121 if (!rn)
122 return;
123
124 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
125 zlog_debug("%s: %s(%u):%pRN added for tracking on %pRN",
126 __func__, VRF_LOGNAME(zvrf->vrf), rnh->vrf_id,
127 rnh->node, rn);
128
129 dest = rib_dest_from_rnode(rn);
130 rnh_list_add_tail(&dest->nht, rnh);
131 route_unlock_node(rn);
132 }
133
134 struct rnh *zebra_add_rnh(struct prefix *p, vrf_id_t vrfid, enum rnh_type type,
135 bool *exists)
136 {
137 struct route_table *table;
138 struct route_node *rn;
139 struct rnh *rnh = NULL;
140 afi_t afi = family2afi(p->family);
141
142 if (IS_ZEBRA_DEBUG_NHT) {
143 struct vrf *vrf = vrf_lookup_by_id(vrfid);
144
145 zlog_debug("%s(%u): Add RNH %pFX type %s", VRF_LOGNAME(vrf),
146 vrfid, p, rnh_type2str(type));
147 }
148 table = get_rnh_table(vrfid, afi, type);
149 if (!table) {
150 struct vrf *vrf = vrf_lookup_by_id(vrfid);
151
152 flog_warn(EC_ZEBRA_RNH_NO_TABLE,
153 "%s(%u): Add RNH %pFX type %s - table not found",
154 VRF_LOGNAME(vrf), vrfid, p, rnh_type2str(type));
155 *exists = false;
156 return NULL;
157 }
158
159 /* Make it sure prefixlen is applied to the prefix. */
160 apply_mask(p);
161
162 /* Lookup (or add) route node.*/
163 rn = route_node_get(table, p);
164
165 if (!rn->info) {
166 rnh = XCALLOC(MTYPE_RNH, sizeof(struct rnh));
167
168 /*
169 * The resolved route is already 0.0.0.0/0 or
170 * 0::0/0 due to the calloc right above, but
171 * we should set the family so that future
172 * comparisons can just be done
173 */
174 rnh->resolved_route.family = p->family;
175 rnh->client_list = list_new();
176 rnh->vrf_id = vrfid;
177 rnh->type = type;
178 rnh->seqno = 0;
179 rnh->afi = afi;
180 rnh->zebra_pseudowire_list = list_new();
181 route_lock_node(rn);
182 rn->info = rnh;
183 rnh->node = rn;
184 *exists = false;
185
186 zebra_rnh_store_in_routing_table(rnh);
187 } else
188 *exists = true;
189
190 route_unlock_node(rn);
191 return (rn->info);
192 }
193
194 struct rnh *zebra_lookup_rnh(struct prefix *p, vrf_id_t vrfid,
195 enum rnh_type type)
196 {
197 struct route_table *table;
198 struct route_node *rn;
199
200 table = get_rnh_table(vrfid, family2afi(PREFIX_FAMILY(p)), type);
201 if (!table)
202 return NULL;
203
204 /* Make it sure prefixlen is applied to the prefix. */
205 apply_mask(p);
206
207 /* Lookup route node.*/
208 rn = route_node_lookup(table, p);
209 if (!rn)
210 return NULL;
211
212 route_unlock_node(rn);
213 return (rn->info);
214 }
215
216 void zebra_free_rnh(struct rnh *rnh)
217 {
218 struct zebra_vrf *zvrf;
219 struct route_table *table;
220
221 zebra_rnh_remove_from_routing_table(rnh);
222 rnh->flags |= ZEBRA_NHT_DELETED;
223 list_delete(&rnh->client_list);
224 list_delete(&rnh->zebra_pseudowire_list);
225
226 zvrf = zebra_vrf_lookup_by_id(rnh->vrf_id);
227 table = zvrf->table[family2afi(rnh->resolved_route.family)][SAFI_UNICAST];
228
229 if (table) {
230 struct route_node *rern;
231
232 rern = route_node_match(table, &rnh->resolved_route);
233 if (rern) {
234 rib_dest_t *dest;
235
236 route_unlock_node(rern);
237
238 dest = rib_dest_from_rnode(rern);
239 rnh_list_del(&dest->nht, rnh);
240 }
241 }
242 free_state(rnh->vrf_id, rnh->state, rnh->node);
243 XFREE(MTYPE_RNH, rnh);
244 }
245
246 static void zebra_delete_rnh(struct rnh *rnh, enum rnh_type type)
247 {
248 struct route_node *rn;
249
250 if (!list_isempty(rnh->client_list)
251 || !list_isempty(rnh->zebra_pseudowire_list))
252 return;
253
254 if ((rnh->flags & ZEBRA_NHT_DELETED) || !(rn = rnh->node))
255 return;
256
257 if (IS_ZEBRA_DEBUG_NHT) {
258 struct vrf *vrf = vrf_lookup_by_id(rnh->vrf_id);
259
260 zlog_debug("%s(%u): Del RNH %pRN type %s", VRF_LOGNAME(vrf),
261 rnh->vrf_id, rnh->node, rnh_type2str(type));
262 }
263
264 zebra_free_rnh(rnh);
265 rn->info = NULL;
266 route_unlock_node(rn);
267 }
268
269 /*
270 * This code will send to the registering client
271 * the looked up rnh.
272 * For a rnh that was created, there is no data
273 * so it will send an empty nexthop group
274 * If rnh exists then we know it has been evaluated
275 * and as such it will have a resolved rnh.
276 */
277 void zebra_add_rnh_client(struct rnh *rnh, struct zserv *client,
278 enum rnh_type type, vrf_id_t vrf_id)
279 {
280 if (IS_ZEBRA_DEBUG_NHT) {
281 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
282
283 zlog_debug("%s(%u): Client %s registers for RNH %pRN type %s",
284 VRF_LOGNAME(vrf), vrf_id,
285 zebra_route_string(client->proto), rnh->node,
286 rnh_type2str(type));
287 }
288 if (!listnode_lookup(rnh->client_list, client))
289 listnode_add(rnh->client_list, client);
290
291 /*
292 * We always need to respond with known information,
293 * currently multiple daemons expect this behavior
294 */
295 zebra_send_rnh_update(rnh, client, type, vrf_id, 0);
296 }
297
298 void zebra_remove_rnh_client(struct rnh *rnh, struct zserv *client,
299 enum rnh_type type)
300 {
301 if (IS_ZEBRA_DEBUG_NHT) {
302 struct vrf *vrf = vrf_lookup_by_id(rnh->vrf_id);
303
304 zlog_debug("Client %s unregisters for RNH %s(%u)%pRN type %s",
305 zebra_route_string(client->proto), VRF_LOGNAME(vrf),
306 vrf->vrf_id, rnh->node, rnh_type2str(type));
307 }
308 listnode_delete(rnh->client_list, client);
309 zebra_delete_rnh(rnh, type);
310 }
311
312 /* XXX move this utility function elsewhere? */
313 static void addr2hostprefix(int af, const union g_addr *addr,
314 struct prefix *prefix)
315 {
316 switch (af) {
317 case AF_INET:
318 prefix->family = AF_INET;
319 prefix->prefixlen = IPV4_MAX_BITLEN;
320 prefix->u.prefix4 = addr->ipv4;
321 break;
322 case AF_INET6:
323 prefix->family = AF_INET6;
324 prefix->prefixlen = IPV6_MAX_BITLEN;
325 prefix->u.prefix6 = addr->ipv6;
326 break;
327 default:
328 memset(prefix, 0, sizeof(*prefix));
329 zlog_warn("%s: unknown address family %d", __func__, af);
330 break;
331 }
332 }
333
334 void zebra_register_rnh_pseudowire(vrf_id_t vrf_id, struct zebra_pw *pw,
335 bool *nht_exists)
336 {
337 struct prefix nh;
338 struct rnh *rnh;
339 bool exists;
340 struct zebra_vrf *zvrf;
341
342 *nht_exists = false;
343
344 zvrf = vrf_info_lookup(vrf_id);
345 if (!zvrf)
346 return;
347
348 addr2hostprefix(pw->af, &pw->nexthop, &nh);
349 rnh = zebra_add_rnh(&nh, vrf_id, RNH_NEXTHOP_TYPE, &exists);
350 if (!rnh)
351 return;
352
353 if (!listnode_lookup(rnh->zebra_pseudowire_list, pw)) {
354 listnode_add(rnh->zebra_pseudowire_list, pw);
355 pw->rnh = rnh;
356 zebra_evaluate_rnh(zvrf, family2afi(pw->af), 1,
357 RNH_NEXTHOP_TYPE, &nh);
358 } else
359 *nht_exists = true;
360 }
361
362 void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id, struct zebra_pw *pw)
363 {
364 struct rnh *rnh;
365
366 rnh = pw->rnh;
367 if (!rnh)
368 return;
369
370 listnode_delete(rnh->zebra_pseudowire_list, pw);
371 pw->rnh = NULL;
372
373 zebra_delete_rnh(rnh, RNH_NEXTHOP_TYPE);
374 }
375
376 /* Clear the NEXTHOP_FLAG_RNH_FILTERED flags on all nexthops
377 */
378 static void zebra_rnh_clear_nexthop_rnh_filters(struct route_entry *re)
379 {
380 struct nexthop *nexthop;
381
382 if (re) {
383 for (nexthop = re->nhe->nhg.nexthop; nexthop;
384 nexthop = nexthop->next) {
385 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_RNH_FILTERED);
386 }
387 }
388 }
389
390 /* Apply the NHT route-map for a client to the route (and nexthops)
391 * resolving a NH.
392 */
393 static int zebra_rnh_apply_nht_rmap(afi_t afi, struct zebra_vrf *zvrf,
394 struct route_node *prn,
395 struct route_entry *re, int proto)
396 {
397 int at_least_one = 0;
398 struct nexthop *nexthop;
399 route_map_result_t ret;
400
401 if (prn && re) {
402 for (nexthop = re->nhe->nhg.nexthop; nexthop;
403 nexthop = nexthop->next) {
404 ret = zebra_nht_route_map_check(
405 afi, proto, &prn->p, zvrf, re, nexthop);
406 if (ret != RMAP_DENYMATCH)
407 at_least_one++; /* at least one valid NH */
408 else {
409 SET_FLAG(nexthop->flags,
410 NEXTHOP_FLAG_RNH_FILTERED);
411 }
412 }
413 }
414 return (at_least_one);
415 }
416
417 /*
418 * Determine appropriate route (RE entry) resolving a tracked BGP route
419 * for BGP route for import.
420 */
421 static struct route_entry *
422 zebra_rnh_resolve_import_entry(struct zebra_vrf *zvrf, afi_t afi,
423 struct route_node *nrn, struct rnh *rnh,
424 struct route_node **prn)
425 {
426 struct route_table *route_table;
427 struct route_node *rn;
428 struct route_entry *re;
429
430 *prn = NULL;
431
432 route_table = zvrf->table[afi][SAFI_UNICAST];
433 if (!route_table) // unexpected
434 return NULL;
435
436 rn = route_node_match(route_table, &nrn->p);
437 if (!rn)
438 return NULL;
439
440 /* Unlock route node - we don't need to lock when walking the tree. */
441 route_unlock_node(rn);
442
443 if (CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH)
444 && !prefix_same(&nrn->p, &rn->p))
445 return NULL;
446
447 if (IS_ZEBRA_DEBUG_NHT_DETAILED) {
448 zlog_debug("%s: %s(%u):%pRN Resolved Import Entry to %pRN",
449 __func__, VRF_LOGNAME(zvrf->vrf), rnh->vrf_id,
450 rnh->node, rn);
451 }
452
453 /* Identify appropriate route entry. */
454 RNODE_FOREACH_RE (rn, re) {
455 if (!CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)
456 && CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED)
457 && !CHECK_FLAG(re->status, ROUTE_ENTRY_QUEUED)
458 && (re->type != ZEBRA_ROUTE_BGP))
459 break;
460 }
461
462 if (re)
463 *prn = rn;
464
465 if (!re && IS_ZEBRA_DEBUG_NHT_DETAILED)
466 zlog_debug(" Rejected due to removed or is a bgp route");
467
468 return re;
469 }
470
471 /*
472 * See if a tracked route entry for import (by BGP) has undergone any
473 * change, and if so, notify the client.
474 */
475 static void zebra_rnh_eval_import_check_entry(struct zebra_vrf *zvrf, afi_t afi,
476 int force, struct route_node *nrn,
477 struct rnh *rnh,
478 struct route_node *prn,
479 struct route_entry *re)
480 {
481 int state_changed = 0;
482 struct zserv *client;
483 struct listnode *node;
484
485 zebra_rnh_remove_from_routing_table(rnh);
486 if (prn) {
487 prefix_copy(&rnh->resolved_route, &prn->p);
488 } else {
489 int family = rnh->resolved_route.family;
490
491 memset(&rnh->resolved_route.family, 0, sizeof(struct prefix));
492 rnh->resolved_route.family = family;
493 }
494 zebra_rnh_store_in_routing_table(rnh);
495
496 if (re && (rnh->state == NULL)) {
497 if (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED))
498 state_changed = 1;
499 } else if (!re && (rnh->state != NULL))
500 state_changed = 1;
501
502 if (compare_state(re, rnh->state)) {
503 copy_state(rnh, re, nrn);
504 state_changed = 1;
505 }
506
507 if (state_changed || force) {
508 if (IS_ZEBRA_DEBUG_NHT)
509 zlog_debug("%s(%u):%pRN: Route import check %s %s",
510 VRF_LOGNAME(zvrf->vrf), zvrf->vrf->vrf_id,
511 nrn, rnh->state ? "passed" : "failed",
512 state_changed ? "(state changed)" : "");
513 /* state changed, notify clients */
514 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) {
515 zebra_send_rnh_update(rnh, client,
516 RNH_IMPORT_CHECK_TYPE,
517 zvrf->vrf->vrf_id, 0);
518 }
519 }
520 }
521
522 /*
523 * Notify clients registered for this nexthop about a change.
524 */
525 static void zebra_rnh_notify_protocol_clients(struct zebra_vrf *zvrf, afi_t afi,
526 struct route_node *nrn,
527 struct rnh *rnh,
528 struct route_node *prn,
529 struct route_entry *re)
530 {
531 struct listnode *node;
532 struct zserv *client;
533 int num_resolving_nh;
534
535 if (IS_ZEBRA_DEBUG_NHT) {
536 if (prn && re) {
537 zlog_debug("%s(%u):%pRN: NH resolved over route %pRN",
538 VRF_LOGNAME(zvrf->vrf), zvrf->vrf->vrf_id,
539 nrn, prn);
540 } else
541 zlog_debug("%s(%u):%pRN: NH has become unresolved",
542 VRF_LOGNAME(zvrf->vrf), zvrf->vrf->vrf_id,
543 nrn);
544 }
545
546 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) {
547 if (prn && re) {
548 /* Apply route-map for this client to route resolving
549 * this
550 * nexthop to see if it is filtered or not.
551 */
552 zebra_rnh_clear_nexthop_rnh_filters(re);
553 num_resolving_nh = zebra_rnh_apply_nht_rmap(
554 afi, zvrf, prn, re, client->proto);
555 if (num_resolving_nh)
556 rnh->filtered[client->proto] = 0;
557 else
558 rnh->filtered[client->proto] = 1;
559
560 if (IS_ZEBRA_DEBUG_NHT)
561 zlog_debug(
562 "%s(%u):%pRN: Notifying client %s about NH %s",
563 VRF_LOGNAME(zvrf->vrf),
564 zvrf->vrf->vrf_id, nrn,
565 zebra_route_string(client->proto),
566 num_resolving_nh
567 ? ""
568 : "(filtered by route-map)");
569 } else {
570 rnh->filtered[client->proto] = 0;
571 if (IS_ZEBRA_DEBUG_NHT)
572 zlog_debug(
573 "%s(%u):%pRN: Notifying client %s about NH (unreachable)",
574 VRF_LOGNAME(zvrf->vrf),
575 zvrf->vrf->vrf_id, nrn,
576 zebra_route_string(client->proto));
577 }
578
579 zebra_send_rnh_update(rnh, client, RNH_NEXTHOP_TYPE,
580 zvrf->vrf->vrf_id, 0);
581 }
582
583 if (re)
584 zebra_rnh_clear_nexthop_rnh_filters(re);
585 }
586
587 /*
588 * Utility to determine whether a candidate nexthop is useable. We make this
589 * check in a couple of places, so this is a single home for the logic we
590 * use.
591 */
592 static bool rnh_nexthop_valid(const struct route_entry *re,
593 const struct nexthop *nh)
594 {
595 return (CHECK_FLAG(re->status, ROUTE_ENTRY_INSTALLED)
596 && CHECK_FLAG(nh->flags, NEXTHOP_FLAG_ACTIVE)
597 && !CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RECURSIVE)
598 && !CHECK_FLAG(nh->flags, NEXTHOP_FLAG_DUPLICATE)
599 && !CHECK_FLAG(nh->flags, NEXTHOP_FLAG_RNH_FILTERED));
600 }
601
602 /*
603 * Determine appropriate route (route entry) resolving a tracked
604 * nexthop.
605 */
606 static struct route_entry *
607 zebra_rnh_resolve_nexthop_entry(struct zebra_vrf *zvrf, afi_t afi,
608 struct route_node *nrn, struct rnh *rnh,
609 struct route_node **prn)
610 {
611 struct route_table *route_table;
612 struct route_node *rn;
613 struct route_entry *re;
614 struct nexthop *nexthop;
615
616 *prn = NULL;
617
618 route_table = zvrf->table[afi][SAFI_UNICAST];
619 if (!route_table)
620 return NULL;
621
622 rn = route_node_match(route_table, &nrn->p);
623 if (!rn)
624 return NULL;
625
626 /* Unlock route node - we don't need to lock when walking the tree. */
627 route_unlock_node(rn);
628
629 /* While resolving nexthops, we may need to walk up the tree from the
630 * most-specific match. Do similar logic as in zebra_rib.c
631 */
632 while (rn) {
633 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
634 zlog_debug("%s: %s(%u):%pRN Possible Match to %pRN",
635 __func__, VRF_LOGNAME(zvrf->vrf),
636 rnh->vrf_id, rnh->node, rn);
637
638 /* Do not resolve over default route unless allowed &&
639 * match route to be exact if so specified
640 */
641 if (is_default_prefix(&rn->p)
642 && !rnh_resolve_via_default(zvrf, rn->p.family)) {
643 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
644 zlog_debug(
645 " Not allowed to resolve through default prefix");
646 return NULL;
647 }
648
649 /* Identify appropriate route entry. */
650 RNODE_FOREACH_RE (rn, re) {
651 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED)) {
652 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
653 zlog_debug(
654 " Route Entry %s removed",
655 zebra_route_string(re->type));
656 continue;
657 }
658 if (!CHECK_FLAG(re->flags, ZEBRA_FLAG_SELECTED) &&
659 !CHECK_FLAG(re->flags, ZEBRA_FLAG_FIB_OVERRIDE)) {
660 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
661 zlog_debug(
662 " Route Entry %s !selected",
663 zebra_route_string(re->type));
664 continue;
665 }
666
667 if (CHECK_FLAG(re->status, ROUTE_ENTRY_QUEUED)) {
668 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
669 zlog_debug(
670 " Route Entry %s queued",
671 zebra_route_string(re->type));
672 continue;
673 }
674
675 /* Just being SELECTED isn't quite enough - must
676 * have an installed nexthop to be useful.
677 */
678 for (ALL_NEXTHOPS(re->nhe->nhg, nexthop)) {
679 if (rnh_nexthop_valid(re, nexthop))
680 break;
681 }
682
683 if (nexthop == NULL) {
684 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
685 zlog_debug(
686 " Route Entry %s no nexthops",
687 zebra_route_string(re->type));
688 continue;
689 }
690
691 if (CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) {
692 if ((re->type == ZEBRA_ROUTE_CONNECT)
693 || (re->type == ZEBRA_ROUTE_STATIC))
694 break;
695 if (re->type == ZEBRA_ROUTE_NHRP) {
696
697 for (nexthop = re->nhe->nhg.nexthop;
698 nexthop;
699 nexthop = nexthop->next)
700 if (nexthop->type
701 == NEXTHOP_TYPE_IFINDEX)
702 break;
703 if (nexthop)
704 break;
705 }
706 } else
707 break;
708 }
709
710 /* Route entry found, we're done; else, walk up the tree. */
711 if (re) {
712 *prn = rn;
713 return re;
714 }
715
716 if (!CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
717 rn = rn->parent;
718 else {
719 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
720 zlog_debug(
721 " Nexthop must be connected, cannot recurse up");
722 return NULL;
723 }
724 }
725
726 return NULL;
727 }
728
729 static void zebra_rnh_process_pseudowires(vrf_id_t vrfid, struct rnh *rnh)
730 {
731 struct zebra_pw *pw;
732 struct listnode *node;
733
734 for (ALL_LIST_ELEMENTS_RO(rnh->zebra_pseudowire_list, node, pw))
735 zebra_pw_update(pw);
736 }
737
738 /*
739 * See if a tracked nexthop entry has undergone any change, and if so,
740 * take appropriate action; this involves notifying any clients and/or
741 * scheduling dependent static routes for processing.
742 */
743 static void zebra_rnh_eval_nexthop_entry(struct zebra_vrf *zvrf, afi_t afi,
744 int force, struct route_node *nrn,
745 struct rnh *rnh,
746 struct route_node *prn,
747 struct route_entry *re)
748 {
749 int state_changed = 0;
750
751 /* If we're resolving over a different route, resolution has changed or
752 * the resolving route has some change (e.g., metric), there is a state
753 * change.
754 */
755 zebra_rnh_remove_from_routing_table(rnh);
756 if (!prefix_same(&rnh->resolved_route, prn ? &prn->p : NULL)) {
757 if (prn)
758 prefix_copy(&rnh->resolved_route, &prn->p);
759 else {
760 /*
761 * Just quickly store the family of the resolved
762 * route so that we can reset it in a second here
763 */
764 int family = rnh->resolved_route.family;
765
766 memset(&rnh->resolved_route, 0, sizeof(struct prefix));
767 rnh->resolved_route.family = family;
768 }
769
770 copy_state(rnh, re, nrn);
771 state_changed = 1;
772 } else if (compare_state(re, rnh->state)) {
773 copy_state(rnh, re, nrn);
774 state_changed = 1;
775 }
776 zebra_rnh_store_in_routing_table(rnh);
777
778 if (state_changed || force) {
779 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
780 * rnh->state.
781 */
782 /* Notify registered protocol clients. */
783 zebra_rnh_notify_protocol_clients(zvrf, afi, nrn, rnh, prn,
784 rnh->state);
785
786 /* Process pseudowires attached to this nexthop */
787 zebra_rnh_process_pseudowires(zvrf->vrf->vrf_id, rnh);
788 }
789 }
790
791 /* Evaluate one tracked entry */
792 static void zebra_rnh_evaluate_entry(struct zebra_vrf *zvrf, afi_t afi,
793 int force, enum rnh_type type,
794 struct route_node *nrn)
795 {
796 struct rnh *rnh;
797 struct route_entry *re;
798 struct route_node *prn;
799
800 if (IS_ZEBRA_DEBUG_NHT) {
801 zlog_debug("%s(%u):%pRN: Evaluate RNH, type %s %s",
802 VRF_LOGNAME(zvrf->vrf), zvrf->vrf->vrf_id, nrn,
803 rnh_type2str(type), force ? "(force)" : "");
804 }
805
806 rnh = nrn->info;
807
808 /* Identify route entry (RE) resolving this tracked entry. */
809 if (type == RNH_IMPORT_CHECK_TYPE)
810 re = zebra_rnh_resolve_import_entry(zvrf, afi, nrn, rnh, &prn);
811 else
812 re = zebra_rnh_resolve_nexthop_entry(zvrf, afi, nrn, rnh, &prn);
813
814 /* If the entry cannot be resolved and that is also the existing state,
815 * there is nothing further to do.
816 */
817 if (!re && rnh->state == NULL && !force)
818 return;
819
820 /* Process based on type of entry. */
821 if (type == RNH_IMPORT_CHECK_TYPE)
822 zebra_rnh_eval_import_check_entry(zvrf, afi, force, nrn, rnh,
823 prn, re);
824 else
825 zebra_rnh_eval_nexthop_entry(zvrf, afi, force, nrn, rnh, prn,
826 re);
827 }
828
829 /*
830 * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag
831 * from the re entries.
832 *
833 * Please note we are doing this *after* we have
834 * notified the world about each nexthop as that
835 * we can have a situation where one re entry
836 * covers multiple nexthops we are interested in.
837 */
838 static void zebra_rnh_clear_nhc_flag(struct zebra_vrf *zvrf, afi_t afi,
839 enum rnh_type type, struct route_node *nrn)
840 {
841 struct rnh *rnh;
842 struct route_entry *re;
843 struct route_node *prn;
844
845 rnh = nrn->info;
846
847 /* Identify route entry (RIB) resolving this tracked entry. */
848 if (type == RNH_IMPORT_CHECK_TYPE)
849 re = zebra_rnh_resolve_import_entry(zvrf, afi, nrn, rnh,
850 &prn);
851 else
852 re = zebra_rnh_resolve_nexthop_entry(zvrf, afi, nrn, rnh,
853 &prn);
854
855 if (re)
856 UNSET_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED);
857 }
858
859 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
860 * of a particular VRF and address-family or a specific prefix.
861 */
862 void zebra_evaluate_rnh(struct zebra_vrf *zvrf, afi_t afi, int force,
863 enum rnh_type type, struct prefix *p)
864 {
865 struct route_table *rnh_table;
866 struct route_node *nrn;
867
868 rnh_table = get_rnh_table(zvrf->vrf->vrf_id, afi, type);
869 if (!rnh_table) // unexpected
870 return;
871
872 if (p) {
873 /* Evaluating a specific entry, make sure it exists. */
874 nrn = route_node_lookup(rnh_table, p);
875 if (nrn && nrn->info)
876 zebra_rnh_evaluate_entry(zvrf, afi, force, type, nrn);
877
878 if (nrn)
879 route_unlock_node(nrn);
880 } else {
881 /* Evaluate entire table. */
882 nrn = route_top(rnh_table);
883 while (nrn) {
884 if (nrn->info)
885 zebra_rnh_evaluate_entry(zvrf, afi, force, type,
886 nrn);
887 nrn = route_next(nrn); /* this will also unlock nrn */
888 }
889 nrn = route_top(rnh_table);
890 while (nrn) {
891 if (nrn->info)
892 zebra_rnh_clear_nhc_flag(zvrf, afi, type, nrn);
893 nrn = route_next(nrn); /* this will also unlock nrn */
894 }
895 }
896 }
897
898 void zebra_print_rnh_table(vrf_id_t vrfid, afi_t afi, struct vty *vty,
899 enum rnh_type type, struct prefix *p)
900 {
901 struct route_table *table;
902 struct route_node *rn;
903
904 table = get_rnh_table(vrfid, afi, type);
905 if (!table) {
906 if (IS_ZEBRA_DEBUG_NHT)
907 zlog_debug("print_rnhs: rnh table not found");
908 return;
909 }
910
911 for (rn = route_top(table); rn; rn = route_next(rn)) {
912 if (p && !prefix_match(&rn->p, p))
913 continue;
914
915 if (rn->info)
916 print_rnh(rn, vty);
917 }
918 }
919
920 /**
921 * free_state - free up the re structure associated with the rnh.
922 */
923 static void free_state(vrf_id_t vrf_id, struct route_entry *re,
924 struct route_node *rn)
925 {
926 if (!re)
927 return;
928
929 /* free RE and nexthops */
930 zebra_nhg_free(re->nhe);
931 XFREE(MTYPE_RE, re);
932 }
933
934 static void copy_state(struct rnh *rnh, const struct route_entry *re,
935 struct route_node *rn)
936 {
937 struct route_entry *state;
938
939 if (rnh->state) {
940 free_state(rnh->vrf_id, rnh->state, rn);
941 rnh->state = NULL;
942 }
943
944 if (!re)
945 return;
946
947 state = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
948 state->type = re->type;
949 state->distance = re->distance;
950 state->metric = re->metric;
951 state->vrf_id = re->vrf_id;
952 state->status = re->status;
953
954 state->nhe = zebra_nhe_copy(re->nhe, 0);
955
956 /* Copy the 'fib' nexthops also, if present - we want to capture
957 * the true installed nexthops.
958 */
959 if (re->fib_ng.nexthop)
960 nexthop_group_copy(&state->fib_ng, &re->fib_ng);
961 if (re->fib_backup_ng.nexthop)
962 nexthop_group_copy(&state->fib_backup_ng, &re->fib_backup_ng);
963
964 rnh->state = state;
965 }
966
967 /*
968 * Compare two route_entries' nexthops.
969 */
970 static bool compare_valid_nexthops(struct route_entry *r1,
971 struct route_entry *r2)
972 {
973 bool matched_p = false;
974 struct nexthop_group *nhg1, *nhg2;
975 struct nexthop *nh1, *nh2;
976
977 /* Account for backup nexthops and for the 'fib' nexthop lists,
978 * if present.
979 */
980 nhg1 = rib_get_fib_nhg(r1);
981 nhg2 = rib_get_fib_nhg(r2);
982
983 nh1 = nhg1->nexthop;
984 nh2 = nhg2->nexthop;
985
986 while (1) {
987 /* Find each list's next valid nexthop */
988 while ((nh1 != NULL) && !rnh_nexthop_valid(r1, nh1))
989 nh1 = nexthop_next(nh1);
990
991 while ((nh2 != NULL) && !rnh_nexthop_valid(r2, nh2))
992 nh2 = nexthop_next(nh2);
993
994 if (nh1 && nh2) {
995 /* Any difference is a no-match */
996 if (nexthop_cmp(nh1, nh2) != 0) {
997 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
998 zlog_debug("%s: nh1, nh2 differ",
999 __func__);
1000 goto done;
1001 }
1002
1003 nh1 = nexthop_next(nh1);
1004 nh2 = nexthop_next(nh2);
1005 } else if (nh1 || nh2) {
1006 /* One list has more valid nexthops than the other */
1007 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
1008 zlog_debug("%s: nh1 %s, nh2 %s", __func__,
1009 nh1 ? "non-NULL" : "NULL",
1010 nh2 ? "non-NULL" : "NULL");
1011 goto done;
1012 } else
1013 break; /* Done with both lists */
1014 }
1015
1016 /* The test for the backups is slightly different: the only installed
1017 * backups will be in the 'fib' list.
1018 */
1019 nhg1 = rib_get_fib_backup_nhg(r1);
1020 nhg2 = rib_get_fib_backup_nhg(r2);
1021
1022 nh1 = nhg1->nexthop;
1023 nh2 = nhg2->nexthop;
1024
1025 while (1) {
1026 /* Find each backup list's next valid nexthop */
1027 while ((nh1 != NULL) && !rnh_nexthop_valid(r1, nh1))
1028 nh1 = nexthop_next(nh1);
1029
1030 while ((nh2 != NULL) && !rnh_nexthop_valid(r2, nh2))
1031 nh2 = nexthop_next(nh2);
1032
1033 if (nh1 && nh2) {
1034 /* Any difference is a no-match */
1035 if (nexthop_cmp(nh1, nh2) != 0) {
1036 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
1037 zlog_debug("%s: backup nh1, nh2 differ",
1038 __func__);
1039 goto done;
1040 }
1041
1042 nh1 = nexthop_next(nh1);
1043 nh2 = nexthop_next(nh2);
1044 } else if (nh1 || nh2) {
1045 /* One list has more valid nexthops than the other */
1046 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
1047 zlog_debug("%s: backup nh1 %s, nh2 %s",
1048 __func__,
1049 nh1 ? "non-NULL" : "NULL",
1050 nh2 ? "non-NULL" : "NULL");
1051 goto done;
1052 } else
1053 break; /* Done with both lists */
1054 }
1055
1056 /* Well, it's a match */
1057 if (IS_ZEBRA_DEBUG_NHT_DETAILED)
1058 zlog_debug("%s: matched", __func__);
1059
1060 matched_p = true;
1061
1062 done:
1063
1064 return matched_p;
1065 }
1066
1067 static int compare_state(struct route_entry *r1, struct route_entry *r2)
1068 {
1069 if (!r1 && !r2)
1070 return 0;
1071
1072 if ((!r1 && r2) || (r1 && !r2))
1073 return 1;
1074
1075 if (r1->distance != r2->distance)
1076 return 1;
1077
1078 if (r1->metric != r2->metric)
1079 return 1;
1080
1081 if (!compare_valid_nexthops(r1, r2))
1082 return 1;
1083
1084 return 0;
1085 }
1086
1087 int zebra_send_rnh_update(struct rnh *rnh, struct zserv *client,
1088 enum rnh_type type, vrf_id_t vrf_id,
1089 uint32_t srte_color)
1090 {
1091 struct stream *s = NULL;
1092 struct route_entry *re;
1093 unsigned long nump;
1094 uint8_t num;
1095 struct nexthop *nh;
1096 struct route_node *rn;
1097 int ret;
1098 uint32_t message = 0;
1099 int cmd = (type == RNH_IMPORT_CHECK_TYPE) ? ZEBRA_IMPORT_CHECK_UPDATE
1100 : ZEBRA_NEXTHOP_UPDATE;
1101
1102 rn = rnh->node;
1103 re = rnh->state;
1104
1105 /* Get output stream. */
1106 s = stream_new(ZEBRA_MAX_PACKET_SIZ);
1107
1108 zclient_create_header(s, cmd, vrf_id);
1109
1110 /* Message flags. */
1111 if (srte_color)
1112 SET_FLAG(message, ZAPI_MESSAGE_SRTE);
1113 stream_putl(s, message);
1114
1115 stream_putw(s, rn->p.family);
1116 switch (rn->p.family) {
1117 case AF_INET:
1118 stream_putc(s, rn->p.prefixlen);
1119 stream_put_in_addr(s, &rn->p.u.prefix4);
1120 break;
1121 case AF_INET6:
1122 stream_putc(s, rn->p.prefixlen);
1123 stream_put(s, &rn->p.u.prefix6, IPV6_MAX_BYTELEN);
1124 break;
1125 default:
1126 flog_err(EC_ZEBRA_RNH_UNKNOWN_FAMILY,
1127 "%s: Unknown family (%d) notification attempted",
1128 __func__, rn->p.family);
1129 goto failure;
1130 }
1131 if (srte_color)
1132 stream_putl(s, srte_color);
1133
1134 if (re) {
1135 struct zapi_nexthop znh;
1136 struct nexthop_group *nhg;
1137
1138 stream_putc(s, re->type);
1139 stream_putw(s, re->instance);
1140 stream_putc(s, re->distance);
1141 stream_putl(s, re->metric);
1142 num = 0;
1143 nump = stream_get_endp(s);
1144 stream_putc(s, 0);
1145
1146 nhg = rib_get_fib_nhg(re);
1147 for (ALL_NEXTHOPS_PTR(nhg, nh))
1148 if (rnh_nexthop_valid(re, nh)) {
1149 zapi_nexthop_from_nexthop(&znh, nh);
1150 ret = zapi_nexthop_encode(s, &znh, 0, message);
1151 if (ret < 0)
1152 goto failure;
1153
1154 num++;
1155 }
1156
1157 nhg = rib_get_fib_backup_nhg(re);
1158 if (nhg) {
1159 for (ALL_NEXTHOPS_PTR(nhg, nh))
1160 if (rnh_nexthop_valid(re, nh)) {
1161 zapi_nexthop_from_nexthop(&znh, nh);
1162 ret = zapi_nexthop_encode(
1163 s, &znh, 0 /* flags */,
1164 0 /* message */);
1165 if (ret < 0)
1166 goto failure;
1167
1168 num++;
1169 }
1170 }
1171
1172 stream_putc_at(s, nump, num);
1173 } else {
1174 stream_putc(s, 0); // type
1175 stream_putw(s, 0); // instance
1176 stream_putc(s, 0); // distance
1177 stream_putl(s, 0); // metric
1178 stream_putc(s, 0); // nexthops
1179 }
1180 stream_putw_at(s, 0, stream_get_endp(s));
1181
1182 client->nh_last_upd_time = monotime(NULL);
1183 client->last_write_cmd = cmd;
1184 return zserv_send_message(client, s);
1185
1186 failure:
1187
1188 stream_free(s);
1189 return -1;
1190 }
1191
1192 static void print_nh(struct nexthop *nexthop, struct vty *vty)
1193 {
1194 char buf[BUFSIZ];
1195 struct zebra_ns *zns = zebra_ns_lookup(nexthop->vrf_id);
1196
1197 switch (nexthop->type) {
1198 case NEXTHOP_TYPE_IPV4:
1199 case NEXTHOP_TYPE_IPV4_IFINDEX:
1200 vty_out(vty, " via %pI4", &nexthop->gate.ipv4);
1201 if (nexthop->ifindex)
1202 vty_out(vty, ", %s",
1203 ifindex2ifname_per_ns(zns, nexthop->ifindex));
1204 break;
1205 case NEXTHOP_TYPE_IPV6:
1206 case NEXTHOP_TYPE_IPV6_IFINDEX:
1207 vty_out(vty, " %s",
1208 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));
1209 if (nexthop->ifindex)
1210 vty_out(vty, ", via %s",
1211 ifindex2ifname_per_ns(zns, nexthop->ifindex));
1212 break;
1213 case NEXTHOP_TYPE_IFINDEX:
1214 vty_out(vty, " is directly connected, %s",
1215 ifindex2ifname_per_ns(zns, nexthop->ifindex));
1216 break;
1217 case NEXTHOP_TYPE_BLACKHOLE:
1218 vty_out(vty, " is directly connected, Null0");
1219 break;
1220 default:
1221 break;
1222 }
1223 vty_out(vty, "\n");
1224 }
1225
1226 static void print_rnh(struct route_node *rn, struct vty *vty)
1227 {
1228 struct rnh *rnh;
1229 struct nexthop *nexthop;
1230 struct listnode *node;
1231 struct zserv *client;
1232 char buf[BUFSIZ];
1233
1234 rnh = rn->info;
1235 vty_out(vty, "%s%s\n",
1236 inet_ntop(rn->p.family, &rn->p.u.prefix, buf, BUFSIZ),
1237 CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED) ? "(Connected)"
1238 : "");
1239 if (rnh->state) {
1240 vty_out(vty, " resolved via %s\n",
1241 zebra_route_string(rnh->state->type));
1242 for (nexthop = rnh->state->nhe->nhg.nexthop; nexthop;
1243 nexthop = nexthop->next)
1244 print_nh(nexthop, vty);
1245 } else
1246 vty_out(vty, " unresolved%s\n",
1247 CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)
1248 ? "(Connected)"
1249 : "");
1250
1251 vty_out(vty, " Client list:");
1252 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client))
1253 vty_out(vty, " %s(fd %d)%s", zebra_route_string(client->proto),
1254 client->sock,
1255 rnh->filtered[client->proto] ? "(filtered)" : "");
1256 if (!list_isempty(rnh->zebra_pseudowire_list))
1257 vty_out(vty, " zebra[pseudowires]");
1258 vty_out(vty, "\n");
1259 }
1260
1261 static int zebra_cleanup_rnh_client(vrf_id_t vrf_id, afi_t afi,
1262 struct zserv *client, enum rnh_type type)
1263 {
1264 struct route_table *ntable;
1265 struct route_node *nrn;
1266 struct rnh *rnh;
1267
1268 if (IS_ZEBRA_DEBUG_NHT) {
1269 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
1270
1271 zlog_debug(
1272 "%s(%u): Client %s RNH cleanup for family %s type %s",
1273 VRF_LOGNAME(vrf), vrf_id,
1274 zebra_route_string(client->proto), afi2str(afi),
1275 rnh_type2str(type));
1276 }
1277
1278 ntable = get_rnh_table(vrf_id, afi, type);
1279 if (!ntable) {
1280 zlog_debug("cleanup_rnh_client: rnh table not found");
1281 return -1;
1282 }
1283
1284 for (nrn = route_top(ntable); nrn; nrn = route_next(nrn)) {
1285 if (!nrn->info)
1286 continue;
1287
1288 rnh = nrn->info;
1289 zebra_remove_rnh_client(rnh, client, type);
1290 }
1291 return 1;
1292 }
1293
1294 /* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1295 static int zebra_client_cleanup_rnh(struct zserv *client)
1296 {
1297 struct vrf *vrf;
1298 struct zebra_vrf *zvrf;
1299
1300 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) {
1301 zvrf = vrf->info;
1302 if (zvrf) {
1303 zebra_cleanup_rnh_client(zvrf_id(zvrf), AFI_IP, client,
1304 RNH_NEXTHOP_TYPE);
1305 zebra_cleanup_rnh_client(zvrf_id(zvrf), AFI_IP6, client,
1306 RNH_NEXTHOP_TYPE);
1307 zebra_cleanup_rnh_client(zvrf_id(zvrf), AFI_IP, client,
1308 RNH_IMPORT_CHECK_TYPE);
1309 zebra_cleanup_rnh_client(zvrf_id(zvrf), AFI_IP6, client,
1310 RNH_IMPORT_CHECK_TYPE);
1311 }
1312 }
1313
1314 return 0;
1315 }
1316
1317 int rnh_resolve_via_default(struct zebra_vrf *zvrf, int family)
1318 {
1319 if (((family == AF_INET) && zvrf->zebra_rnh_ip_default_route)
1320 || ((family == AF_INET6) && zvrf->zebra_rnh_ipv6_default_route))
1321 return 1;
1322 else
1323 return 0;
1324 }