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