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