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