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