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