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