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