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