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