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