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