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