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