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