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