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