]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_rnh.c
zebra: Fix some memory leaks on shutdown
[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
70 int zebra_rnh_ip_default_route = 0;
71 int zebra_rnh_ipv6_default_route = 0;
72
73 static inline struct route_table *get_rnh_table(vrf_id_t vrfid, int family,
74 rnh_type_t type)
75 {
76 struct zebra_vrf *zvrf;
77 struct route_table *t = NULL;
78
79 zvrf = zebra_vrf_lookup_by_id(vrfid);
80 if (zvrf)
81 switch (type) {
82 case RNH_NEXTHOP_TYPE:
83 t = zvrf->rnh_table[family2afi(family)];
84 break;
85 case RNH_IMPORT_CHECK_TYPE:
86 t = zvrf->import_check_table[family2afi(family)];
87 break;
88 }
89
90 return t;
91 }
92
93 char *rnh_str(struct rnh *rnh, char *buf, int size)
94 {
95 prefix2str(&(rnh->node->p), buf, size);
96 return buf;
97 }
98
99 struct rnh *zebra_add_rnh(struct prefix *p, vrf_id_t vrfid, rnh_type_t type)
100 {
101 struct route_table *table;
102 struct route_node *rn;
103 struct rnh *rnh = NULL;
104 char buf[PREFIX2STR_BUFFER];
105
106 if (IS_ZEBRA_DEBUG_NHT) {
107 prefix2str(p, buf, sizeof(buf));
108 zlog_debug("%u: Add RNH %s type %d", vrfid, buf, type);
109 }
110 table = get_rnh_table(vrfid, PREFIX_FAMILY(p), type);
111 if (!table) {
112 prefix2str(p, buf, sizeof(buf));
113 zlog_warn("%u: Add RNH %s type %d - table not found", vrfid,
114 buf, type);
115 return NULL;
116 }
117
118 /* Make it sure prefixlen is applied to the prefix. */
119 apply_mask(p);
120
121 /* Lookup (or add) route node.*/
122 rn = route_node_get(table, p);
123
124 if (!rn->info) {
125 rnh = XCALLOC(MTYPE_RNH, sizeof(struct rnh));
126 rnh->client_list = list_new();
127 rnh->vrf_id = vrfid;
128 rnh->zebra_static_route_list = list_new();
129 route_lock_node(rn);
130 rn->info = rnh;
131 rnh->node = rn;
132 }
133
134 route_unlock_node(rn);
135 return (rn->info);
136 }
137
138 struct rnh *zebra_lookup_rnh(struct prefix *p, vrf_id_t vrfid, rnh_type_t type)
139 {
140 struct route_table *table;
141 struct route_node *rn;
142
143 table = get_rnh_table(vrfid, PREFIX_FAMILY(p), type);
144 if (!table)
145 return NULL;
146
147 /* Make it sure prefixlen is applied to the prefix. */
148 apply_mask(p);
149
150 /* Lookup route node.*/
151 rn = route_node_lookup(table, p);
152 if (!rn)
153 return NULL;
154
155 route_unlock_node(rn);
156 return (rn->info);
157 }
158
159 void zebra_free_rnh(struct rnh *rnh)
160 {
161 rnh->flags |= ZEBRA_NHT_DELETED;
162 list_free(rnh->client_list);
163 list_free(rnh->zebra_static_route_list);
164 free_state(rnh->vrf_id, rnh->state, rnh->node);
165 XFREE(MTYPE_RNH, rnh);
166 }
167
168 void zebra_delete_rnh(struct rnh *rnh, rnh_type_t type)
169 {
170 struct route_node *rn;
171
172 if (!rnh || (rnh->flags & ZEBRA_NHT_DELETED) || !(rn = rnh->node))
173 return;
174
175 if (IS_ZEBRA_DEBUG_NHT) {
176 char buf[PREFIX2STR_BUFFER];
177 zlog_debug("%u: Del RNH %s type %d", rnh->vrf_id,
178 rnh_str(rnh, buf, sizeof(buf)), type);
179 }
180
181 zebra_free_rnh(rnh);
182 rn->info = NULL;
183 route_unlock_node(rn);
184 }
185
186 void zebra_add_rnh_client(struct rnh *rnh, struct zserv *client,
187 rnh_type_t type, vrf_id_t vrf_id)
188 {
189 if (IS_ZEBRA_DEBUG_NHT) {
190 char buf[PREFIX2STR_BUFFER];
191 zlog_debug("%u: Client %s registers for RNH %s type %d", vrf_id,
192 zebra_route_string(client->proto),
193 rnh_str(rnh, buf, sizeof(buf)), type);
194 }
195 if (!listnode_lookup(rnh->client_list, client)) {
196 listnode_add(rnh->client_list, client);
197 send_client(rnh, client, type,
198 vrf_id); // Pending: check if its needed
199 }
200 }
201
202 void zebra_remove_rnh_client(struct rnh *rnh, struct zserv *client,
203 rnh_type_t type)
204 {
205 if (IS_ZEBRA_DEBUG_NHT) {
206 char buf[PREFIX2STR_BUFFER];
207 zlog_debug("Client %s unregisters for RNH %s type %d",
208 zebra_route_string(client->proto),
209 rnh_str(rnh, buf, sizeof(buf)), type);
210 }
211 listnode_delete(rnh->client_list, client);
212 if (list_isempty(rnh->client_list)
213 && list_isempty(rnh->zebra_static_route_list))
214 zebra_delete_rnh(rnh, type);
215 }
216
217 void zebra_register_rnh_static_nh(vrf_id_t vrf_id, struct prefix *nh,
218 struct route_node *static_rn)
219 {
220 struct rnh *rnh;
221
222 rnh = zebra_add_rnh(nh, vrf_id, RNH_NEXTHOP_TYPE);
223 if (rnh && !listnode_lookup(rnh->zebra_static_route_list, static_rn)) {
224 listnode_add(rnh->zebra_static_route_list, static_rn);
225 }
226 }
227
228 void zebra_deregister_rnh_static_nh(vrf_id_t vrf_id, struct prefix *nh,
229 struct route_node *static_rn)
230 {
231 struct rnh *rnh;
232
233 rnh = zebra_lookup_rnh(nh, vrf_id, RNH_NEXTHOP_TYPE);
234 if (!rnh || (rnh->flags & ZEBRA_NHT_DELETED))
235 return;
236
237 listnode_delete(rnh->zebra_static_route_list, static_rn);
238
239 if (list_isempty(rnh->client_list)
240 && list_isempty(rnh->zebra_static_route_list))
241 zebra_delete_rnh(rnh, RNH_NEXTHOP_TYPE);
242 }
243
244 void zebra_deregister_rnh_static_nexthops(vrf_id_t vrf_id,
245 struct nexthop *nexthop,
246 struct route_node *rn)
247 {
248 struct nexthop *nh;
249 struct prefix nh_p;
250
251 for (nh = nexthop; nh; nh = nh->next) {
252 switch (nh->type) {
253 case NEXTHOP_TYPE_IPV4:
254 case NEXTHOP_TYPE_IPV4_IFINDEX:
255 nh_p.family = AF_INET;
256 nh_p.prefixlen = IPV4_MAX_BITLEN;
257 nh_p.u.prefix4 = nh->gate.ipv4;
258 break;
259 case NEXTHOP_TYPE_IPV6:
260 case NEXTHOP_TYPE_IPV6_IFINDEX:
261 nh_p.family = AF_INET6;
262 nh_p.prefixlen = IPV6_MAX_BITLEN;
263 nh_p.u.prefix6 = nh->gate.ipv6;
264 break;
265 /*
266 * Not sure what really to do here, we are not
267 * supposed to have either of these for NHT
268 * and the code has no way to know what prefix
269 * to use. So I'm going to just continue
270 * for the moment, which is preferable to
271 * what is currently happening which is a
272 * CRASH and BURN.
273 * Some simple testing shows that we
274 * are not leaving slag around for these
275 * skipped static routes. Since
276 * they don't appear to be installed
277 */
278 case NEXTHOP_TYPE_IFINDEX:
279 case NEXTHOP_TYPE_BLACKHOLE:
280 continue;
281 break;
282 }
283 zebra_deregister_rnh_static_nh(vrf_id, &nh_p, rn);
284 }
285 }
286
287 /* Apply the NHT route-map for a client to the route (and nexthops)
288 * resolving a NH.
289 */
290 static int zebra_rnh_apply_nht_rmap(int family, struct route_node *prn,
291 struct route_entry *re, int proto)
292 {
293 int at_least_one = 0;
294 int rmap_family; /* Route map has diff AF family enum */
295 struct nexthop *nexthop;
296 int ret;
297
298 rmap_family = (family == AF_INET) ? AFI_IP : AFI_IP6;
299
300 if (prn && re) {
301 for (nexthop = re->nexthop; nexthop; nexthop = nexthop->next) {
302 ret = zebra_nht_route_map_check(rmap_family, proto,
303 &prn->p, re, nexthop);
304 if (ret != RMAP_DENYMATCH) {
305 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
306 at_least_one++; /* at least one valid NH */
307 } else {
308 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
309 }
310 }
311 }
312 return (at_least_one);
313 }
314
315 /*
316 * Determine appropriate route (RE entry) resolving a tracked entry
317 * (nexthop or BGP route for import).
318 */
319 static struct route_entry *zebra_rnh_resolve_entry(vrf_id_t vrfid, int family,
320 rnh_type_t type,
321 struct route_node *nrn,
322 struct rnh *rnh,
323 struct route_node **prn)
324 {
325 struct route_table *route_table;
326 struct route_node *rn;
327 struct route_entry *re;
328
329 *prn = NULL;
330
331 route_table = zebra_vrf_table(family2afi(family), SAFI_UNICAST, vrfid);
332 if (!route_table) // unexpected
333 return NULL;
334
335 rn = route_node_match(route_table, &nrn->p);
336 if (!rn)
337 return NULL;
338
339 /* When resolving nexthops, do not resolve via the default route unless
340 * 'ip nht resolve-via-default' is configured.
341 */
342 if ((type == RNH_NEXTHOP_TYPE)
343 && (is_default_prefix(&rn->p)
344 && !nh_resolve_via_default(rn->p.family)))
345 re = NULL;
346 else if ((type == RNH_IMPORT_CHECK_TYPE)
347 && CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH)
348 && !prefix_same(&nrn->p, &rn->p))
349 re = NULL;
350 else {
351 /* Identify appropriate route entry. */
352 RNODE_FOREACH_RE(rn, re)
353 {
354 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
355 continue;
356 if (!CHECK_FLAG(re->status, ROUTE_ENTRY_SELECTED_FIB))
357 continue;
358
359 if (CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) {
360 if (re->type == ZEBRA_ROUTE_CONNECT)
361 break;
362 if (re->type == ZEBRA_ROUTE_NHRP) {
363 struct nexthop *nexthop;
364 for (nexthop = re->nexthop; nexthop;
365 nexthop = nexthop->next)
366 if (nexthop->type
367 == NEXTHOP_TYPE_IFINDEX)
368 break;
369 if (nexthop)
370 break;
371 }
372 } else if ((type == RNH_IMPORT_CHECK_TYPE)
373 && (re->type == ZEBRA_ROUTE_BGP))
374 continue;
375 else
376 break;
377 }
378 }
379
380 /* Need to unlock route node */
381 route_unlock_node(rn);
382 if (re)
383 *prn = rn;
384 return re;
385 }
386
387 /*
388 * See if a tracked route entry for import (by BGP) has undergone any
389 * change, and if so, notify the client.
390 */
391 static void zebra_rnh_eval_import_check_entry(vrf_id_t vrfid, int family,
392 int force, struct route_node *nrn,
393 struct rnh *rnh,
394 struct route_entry *re)
395 {
396 int state_changed = 0;
397 struct zserv *client;
398 char bufn[INET6_ADDRSTRLEN];
399 struct listnode *node;
400 struct nexthop *nexthop;
401
402 if (re && (rnh->state == NULL)) {
403 for (ALL_NEXTHOPS(re->nexthop, nexthop))
404 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
405 state_changed = 1;
406 break;
407 }
408 } else if (!re && (rnh->state != NULL))
409 state_changed = 1;
410
411 if (compare_state(re, rnh->state))
412 copy_state(rnh, re, nrn);
413
414 if (state_changed || force) {
415 if (IS_ZEBRA_DEBUG_NHT) {
416 prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN);
417 zlog_debug("%u:%s: Route import check %s %s\n", vrfid,
418 bufn, rnh->state ? "passed" : "failed",
419 state_changed ? "(state changed)" : "");
420 }
421 /* state changed, notify clients */
422 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) {
423 send_client(rnh, client, RNH_IMPORT_CHECK_TYPE, vrfid);
424 }
425 }
426 }
427
428 /*
429 * Notify clients registered for this nexthop about a change.
430 */
431 static void zebra_rnh_notify_protocol_clients(vrf_id_t vrfid, int family,
432 struct route_node *nrn,
433 struct rnh *rnh,
434 struct route_node *prn,
435 struct route_entry *re)
436 {
437 struct listnode *node;
438 struct zserv *client;
439 char bufn[INET6_ADDRSTRLEN];
440 char bufp[INET6_ADDRSTRLEN];
441 int num_resolving_nh;
442
443 if (IS_ZEBRA_DEBUG_NHT) {
444 prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN);
445 if (prn && re) {
446 prefix2str(&prn->p, bufp, INET6_ADDRSTRLEN);
447 zlog_debug("%u:%s: NH resolved over route %s", vrfid,
448 bufn, bufp);
449 } else
450 zlog_debug("%u:%s: NH has become unresolved", vrfid,
451 bufn);
452 }
453
454 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) {
455 if (prn && re) {
456 /* Apply route-map for this client to route resolving
457 * this
458 * nexthop to see if it is filtered or not.
459 */
460 num_resolving_nh = zebra_rnh_apply_nht_rmap(
461 family, prn, re, client->proto);
462 if (num_resolving_nh)
463 rnh->filtered[client->proto] = 0;
464 else
465 rnh->filtered[client->proto] = 1;
466
467 if (IS_ZEBRA_DEBUG_NHT)
468 zlog_debug(
469 "%u:%s: Notifying client %s about NH %s",
470 vrfid, bufn,
471 zebra_route_string(client->proto),
472 num_resolving_nh
473 ? ""
474 : "(filtered by route-map)");
475 } else {
476 rnh->filtered[client->proto] = 0;
477 if (IS_ZEBRA_DEBUG_NHT)
478 zlog_debug(
479 "%u:%s: Notifying client %s about NH (unreachable)",
480 vrfid, bufn,
481 zebra_route_string(client->proto));
482 }
483
484 send_client(rnh, client, RNH_NEXTHOP_TYPE, vrfid);
485 }
486 }
487
488 static void zebra_rnh_process_static_routes(vrf_id_t vrfid, int family,
489 struct route_node *nrn,
490 struct rnh *rnh,
491 struct route_node *prn,
492 struct route_entry *re)
493 {
494 struct listnode *node;
495 int num_resolving_nh = 0;
496 struct route_node *static_rn;
497 struct route_entry *sre;
498 struct nexthop *nexthop;
499 char bufn[INET6_ADDRSTRLEN];
500 char bufp[INET6_ADDRSTRLEN];
501 char bufs[INET6_ADDRSTRLEN];
502
503 if (IS_ZEBRA_DEBUG_NHT) {
504 prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN);
505 if (prn)
506 prefix2str(&prn->p, bufp, INET6_ADDRSTRLEN);
507 }
508
509 if (prn && re) {
510 /* Apply route-map for "static" to route resolving this
511 * nexthop to see if it is filtered or not.
512 */
513 num_resolving_nh = zebra_rnh_apply_nht_rmap(family, prn, re,
514 ZEBRA_ROUTE_STATIC);
515 if (num_resolving_nh)
516 rnh->filtered[ZEBRA_ROUTE_STATIC] = 0;
517 else
518 rnh->filtered[ZEBRA_ROUTE_STATIC] = 1;
519 } else
520 rnh->filtered[ZEBRA_ROUTE_STATIC] = 0;
521
522 /* Evaluate each static route associated with this nexthop. */
523 for (ALL_LIST_ELEMENTS_RO(rnh->zebra_static_route_list, node,
524 static_rn)) {
525 RNODE_FOREACH_RE(static_rn, sre)
526 {
527 if (sre->type != ZEBRA_ROUTE_STATIC)
528 continue;
529
530 /* Set the filter flag for the correct nexthop - static
531 * route may
532 * be having multiple. We care here only about
533 * registered nexthops.
534 */
535 for (nexthop = sre->nexthop; nexthop;
536 nexthop = nexthop->next) {
537 switch (nexthop->type) {
538 case NEXTHOP_TYPE_IPV4:
539 case NEXTHOP_TYPE_IPV4_IFINDEX:
540 if (nexthop->gate.ipv4.s_addr
541 == nrn->p.u.prefix4.s_addr) {
542 if (num_resolving_nh)
543 UNSET_FLAG(
544 nexthop->flags,
545 NEXTHOP_FLAG_FILTERED);
546 else
547 SET_FLAG(
548 nexthop->flags,
549 NEXTHOP_FLAG_FILTERED);
550 }
551 break;
552 case NEXTHOP_TYPE_IPV6:
553 case NEXTHOP_TYPE_IPV6_IFINDEX:
554
555 if (memcmp(&nexthop->gate.ipv6,
556 &nrn->p.u.prefix6, 16)
557 == 0) {
558 if (num_resolving_nh)
559 UNSET_FLAG(
560 nexthop->flags,
561 NEXTHOP_FLAG_FILTERED);
562 else
563 SET_FLAG(
564 nexthop->flags,
565 NEXTHOP_FLAG_FILTERED);
566 }
567 break;
568 default:
569 break;
570 }
571 }
572
573 if (IS_ZEBRA_DEBUG_NHT) {
574 prefix2str(&static_rn->p, bufs,
575 INET6_ADDRSTRLEN);
576 if (prn && re)
577 zlog_debug(
578 "%u:%s: NH change %s, scheduling static route %s",
579 vrfid, bufn,
580 num_resolving_nh
581 ? ""
582 : "(filtered by route-map)",
583 bufs);
584 else
585 zlog_debug(
586 "%u:%s: NH unreachable, scheduling static route %s",
587 vrfid, bufn, bufs);
588 }
589
590 SET_FLAG(sre->status, ROUTE_ENTRY_CHANGED);
591 SET_FLAG(sre->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
592 }
593
594 rib_queue_add(static_rn);
595 }
596 }
597
598 /*
599 * See if a tracked nexthop entry has undergone any change, and if so,
600 * take appropriate action; this involves notifying any clients and/or
601 * scheduling dependent static routes for processing.
602 */
603 static void zebra_rnh_eval_nexthop_entry(vrf_id_t vrfid, int family, int force,
604 struct route_node *nrn,
605 struct rnh *rnh,
606 struct route_node *prn,
607 struct route_entry *re)
608 {
609 int state_changed = 0;
610
611 /* If we're resolving over a different route, resolution has changed or
612 * the resolving route has some change (e.g., metric), there is a state
613 * change.
614 */
615 if (!prefix_same(&rnh->resolved_route, &prn->p)) {
616 if (prn)
617 prefix_copy(&rnh->resolved_route, &prn->p);
618 else
619 memset(&rnh->resolved_route, 0, sizeof(struct prefix));
620
621 copy_state(rnh, re, nrn);
622 state_changed = 1;
623 } else if (compare_state(re, rnh->state)) {
624 copy_state(rnh, re, nrn);
625 state_changed = 1;
626 }
627
628 if (state_changed || force) {
629 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
630 * rnh->state.
631 */
632 /* Notify registered protocol clients. */
633 zebra_rnh_notify_protocol_clients(vrfid, family, nrn, rnh, prn,
634 rnh->state);
635
636 /* Process static routes attached to this nexthop */
637 zebra_rnh_process_static_routes(vrfid, family, nrn, rnh, prn,
638 rnh->state);
639 }
640 }
641
642 /* Evaluate one tracked entry */
643 static void zebra_rnh_evaluate_entry(vrf_id_t vrfid, int family, int force,
644 rnh_type_t type, struct route_node *nrn)
645 {
646 struct rnh *rnh;
647 struct route_entry *re;
648 struct route_node *prn;
649 char bufn[INET6_ADDRSTRLEN];
650
651 if (IS_ZEBRA_DEBUG_NHT) {
652 prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN);
653 zlog_debug("%u:%s: Evaluate RNH, type %d %s", vrfid, bufn, type,
654 force ? "(force)" : "");
655 }
656
657 rnh = nrn->info;
658
659 /* Identify route entry (RE) resolving this tracked entry. */
660 re = zebra_rnh_resolve_entry(vrfid, family, type, nrn, rnh, &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 re = zebra_rnh_resolve_entry(vrfid, family, type, nrn, rnh, &prn);
696
697 if (re)
698 UNSET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
699 }
700
701 /* Evaluate all tracked entries (nexthops or routes for import into BGP)
702 * of a particular VRF and address-family or a specific prefix.
703 */
704 void zebra_evaluate_rnh(vrf_id_t vrfid, int family, int force, rnh_type_t type,
705 struct prefix *p)
706 {
707 struct route_table *rnh_table;
708 struct route_node *nrn;
709
710 rnh_table = get_rnh_table(vrfid, family, type);
711 if (!rnh_table) // unexpected
712 return;
713
714 if (p) {
715 /* Evaluating a specific entry, make sure it exists. */
716 nrn = route_node_lookup(rnh_table, p);
717 if (nrn && nrn->info)
718 zebra_rnh_evaluate_entry(vrfid, family, force, type,
719 nrn);
720
721 if (nrn)
722 route_unlock_node(nrn);
723 } else {
724 /* Evaluate entire table. */
725 nrn = route_top(rnh_table);
726 while (nrn) {
727 if (nrn->info)
728 zebra_rnh_evaluate_entry(vrfid, family, force,
729 type, nrn);
730 nrn = route_next(nrn); /* this will also unlock nrn */
731 }
732 nrn = route_top(rnh_table);
733 while (nrn) {
734 if (nrn->info)
735 zebra_rnh_clear_nhc_flag(vrfid, family, type,
736 nrn);
737 nrn = route_next(nrn); /* this will also unlock nrn */
738 }
739 }
740 }
741
742 void zebra_print_rnh_table(vrf_id_t vrfid, int af, struct vty *vty,
743 rnh_type_t type)
744 {
745 struct route_table *table;
746 struct route_node *rn;
747
748 table = get_rnh_table(vrfid, af, type);
749 if (!table) {
750 zlog_debug("print_rnhs: rnh table not found\n");
751 return;
752 }
753
754 for (rn = route_top(table); rn; rn = route_next(rn))
755 if (rn->info)
756 print_rnh(rn, vty);
757 }
758
759 int zebra_cleanup_rnh_client(vrf_id_t vrf_id, int family, struct zserv *client,
760 rnh_type_t type)
761 {
762 struct route_table *ntable;
763 struct route_node *nrn;
764 struct rnh *rnh;
765
766 if (IS_ZEBRA_DEBUG_NHT)
767 zlog_debug("%u: Client %s RNH cleanup for family %d type %d",
768 vrf_id, zebra_route_string(client->proto), family,
769 type);
770
771 ntable = get_rnh_table(vrf_id, family, type);
772 if (!ntable) {
773 zlog_debug("cleanup_rnh_client: rnh table not found\n");
774 return -1;
775 }
776
777 for (nrn = route_top(ntable); nrn; nrn = route_next(nrn)) {
778 if (!nrn->info)
779 continue;
780
781 rnh = nrn->info;
782 zebra_remove_rnh_client(rnh, client, type);
783 }
784 return 1;
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 zebra_deregister_rnh_static_nexthops(vrf_id, re->nexthop, rn);
799 nexthops_free(re->nexthop);
800 XFREE(MTYPE_RE, re);
801 }
802
803 static void copy_state(struct rnh *rnh, struct route_entry *re,
804 struct route_node *rn)
805 {
806 struct route_entry *state;
807
808 if (rnh->state) {
809 free_state(rnh->vrf_id, rnh->state, rn);
810 rnh->state = NULL;
811 }
812
813 if (!re)
814 return;
815
816 state = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
817 state->type = re->type;
818 state->distance = re->distance;
819 state->metric = re->metric;
820
821 route_entry_copy_nexthops(state, re->nexthop);
822 rnh->state = state;
823 }
824
825 static int compare_state(struct route_entry *r1, struct route_entry *r2)
826 {
827
828 if (!r1 && !r2)
829 return 0;
830
831 if ((!r1 && r2) || (r1 && !r2))
832 return 1;
833
834 if (r1->distance != r2->distance)
835 return 1;
836
837 if (r1->metric != r2->metric)
838 return 1;
839
840 if (r1->nexthop_num != r2->nexthop_num)
841 return 1;
842
843 if (CHECK_FLAG(r1->status, ROUTE_ENTRY_NEXTHOPS_CHANGED))
844 return 1;
845
846 return 0;
847 }
848
849 static int send_client(struct rnh *rnh, struct zserv *client, rnh_type_t type,
850 vrf_id_t vrf_id)
851 {
852 struct stream *s;
853 struct route_entry *re;
854 unsigned long nump;
855 u_char num;
856 struct nexthop *nexthop;
857 struct route_node *rn;
858 int cmd = (type == RNH_IMPORT_CHECK_TYPE) ? ZEBRA_IMPORT_CHECK_UPDATE
859 : ZEBRA_NEXTHOP_UPDATE;
860
861 rn = rnh->node;
862 re = rnh->state;
863
864 /* Get output stream. */
865 s = client->obuf;
866 stream_reset(s);
867
868 zserv_create_header(s, cmd, vrf_id);
869
870 stream_putw(s, rn->p.family);
871 switch (rn->p.family) {
872 case AF_INET:
873 stream_putc(s, rn->p.prefixlen);
874 stream_put_in_addr(s, &rn->p.u.prefix4);
875 break;
876 case AF_INET6:
877 stream_putc(s, rn->p.prefixlen);
878 stream_put(s, &rn->p.u.prefix6, IPV6_MAX_BYTELEN);
879 break;
880 default:
881 zlog_err("%s: Unknown family (%d) notification attempted\n",
882 __FUNCTION__, rn->p.family);
883 break;
884 }
885 if (re) {
886 stream_putc(s, re->distance);
887 stream_putl(s, re->metric);
888 num = 0;
889 nump = stream_get_endp(s);
890 stream_putc(s, 0);
891 for (nexthop = re->nexthop; nexthop; nexthop = nexthop->next)
892 if ((CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
893 || CHECK_FLAG(nexthop->flags,
894 NEXTHOP_FLAG_RECURSIVE))
895 && CHECK_FLAG(nexthop->flags,
896 NEXTHOP_FLAG_ACTIVE)) {
897 stream_putc(s, nexthop->type);
898 switch (nexthop->type) {
899 case NEXTHOP_TYPE_IPV4:
900 stream_put_in_addr(s,
901 &nexthop->gate.ipv4);
902 stream_putl(s, nexthop->ifindex);
903 break;
904 case NEXTHOP_TYPE_IFINDEX:
905 stream_putl(s, nexthop->ifindex);
906 break;
907 case NEXTHOP_TYPE_IPV4_IFINDEX:
908 stream_put_in_addr(s,
909 &nexthop->gate.ipv4);
910 stream_putl(s, nexthop->ifindex);
911 break;
912 case NEXTHOP_TYPE_IPV6:
913 stream_put(s, &nexthop->gate.ipv6, 16);
914 stream_putl(s, nexthop->ifindex);
915 break;
916 case NEXTHOP_TYPE_IPV6_IFINDEX:
917 stream_put(s, &nexthop->gate.ipv6, 16);
918 stream_putl(s, nexthop->ifindex);
919 break;
920 default:
921 /* do nothing */
922 break;
923 }
924 num++;
925 }
926 stream_putc_at(s, nump, num);
927 } else {
928 stream_putc(s, 0); // distance
929 stream_putl(s, 0); // metric
930 stream_putc(s, 0); // nexthops
931 }
932 stream_putw_at(s, 0, stream_get_endp(s));
933
934 client->nh_last_upd_time = monotime(NULL);
935 client->last_write_cmd = cmd;
936 return zebra_server_send_message(client);
937 }
938
939 static void print_nh(struct nexthop *nexthop, struct vty *vty)
940 {
941 char buf[BUFSIZ];
942 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
943
944 switch (nexthop->type) {
945 case NEXTHOP_TYPE_IPV4:
946 case NEXTHOP_TYPE_IPV4_IFINDEX:
947 vty_out(vty, " via %s", inet_ntoa(nexthop->gate.ipv4));
948 if (nexthop->ifindex)
949 vty_out(vty, ", %s",
950 ifindex2ifname_per_ns(zns, nexthop->ifindex));
951 break;
952 case NEXTHOP_TYPE_IPV6:
953 case NEXTHOP_TYPE_IPV6_IFINDEX:
954 vty_out(vty, " %s",
955 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));
956 if (nexthop->ifindex)
957 vty_out(vty, ", via %s",
958 ifindex2ifname_per_ns(zns, nexthop->ifindex));
959 break;
960 case NEXTHOP_TYPE_IFINDEX:
961 vty_out(vty, " is directly connected, %s",
962 ifindex2ifname_per_ns(zns, nexthop->ifindex));
963 break;
964 case NEXTHOP_TYPE_BLACKHOLE:
965 vty_out(vty, " is directly connected, Null0");
966 break;
967 default:
968 break;
969 }
970 vty_out(vty, "\n");
971 }
972
973 static void print_rnh(struct route_node *rn, struct vty *vty)
974 {
975 struct rnh *rnh;
976 struct nexthop *nexthop;
977 struct listnode *node;
978 struct zserv *client;
979 char buf[BUFSIZ];
980
981 rnh = rn->info;
982 vty_out(vty, "%s%s\n",
983 inet_ntop(rn->p.family, &rn->p.u.prefix, buf, BUFSIZ),
984 CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED) ? "(Connected)"
985 : "");
986 if (rnh->state) {
987 vty_out(vty, " resolved via %s\n",
988 zebra_route_string(rnh->state->type));
989 for (nexthop = rnh->state->nexthop; nexthop;
990 nexthop = nexthop->next)
991 print_nh(nexthop, vty);
992 } else
993 vty_out(vty, " unresolved%s\n",
994 CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)
995 ? "(Connected)"
996 : "");
997
998 vty_out(vty, " Client list:");
999 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client))
1000 vty_out(vty, " %s(fd %d)%s", zebra_route_string(client->proto),
1001 client->sock,
1002 rnh->filtered[client->proto] ? "(filtered)" : "");
1003 if (!list_isempty(rnh->zebra_static_route_list))
1004 vty_out(vty, " zebra%s",
1005 rnh->filtered[ZEBRA_ROUTE_STATIC] ? "(filtered)" : "");
1006 vty_out(vty, "\n");
1007 }