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