]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_rnh.c
Merge pull request #1178 from donaldsharp/pim_obfuscation
[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;
163 list_free(rnh->client_list);
164 list_free(rnh->zebra_static_route_list);
731a75fe 165 list_free(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:
307 zlog_warn("%s: unknown address family %d", __func__, af);
308 break;
309 }
310}
311
312void zebra_register_rnh_pseudowire(vrf_id_t vrf_id, struct zebra_pw *pw)
313{
314 struct prefix nh;
315 struct rnh *rnh;
316
317 addr2hostprefix(pw->af, &pw->nexthop, &nh);
318 rnh = zebra_add_rnh(&nh, vrf_id, RNH_NEXTHOP_TYPE);
319 if (rnh && !listnode_lookup(rnh->zebra_pseudowire_list, pw)) {
320 listnode_add(rnh->zebra_pseudowire_list, pw);
321 pw->rnh = rnh;
322 zebra_evaluate_rnh(vrf_id, pw->af, 1, RNH_NEXTHOP_TYPE, &nh);
323 }
324}
325
326void zebra_deregister_rnh_pseudowire(vrf_id_t vrf_id, struct zebra_pw *pw)
327{
328 struct rnh *rnh;
329
330 rnh = pw->rnh;
331 if (!rnh)
332 return;
333
334 listnode_delete(rnh->zebra_pseudowire_list, pw);
335 pw->rnh = NULL;
336
337 if (list_isempty(rnh->client_list)
338 && list_isempty(rnh->zebra_static_route_list)
339 && list_isempty(rnh->zebra_pseudowire_list))
340 zebra_delete_rnh(rnh, RNH_NEXTHOP_TYPE);
341}
342
d50b5bdd 343/* Apply the NHT route-map for a client to the route (and nexthops)
344 * resolving a NH.
345 */
d62a17ae 346static int zebra_rnh_apply_nht_rmap(int family, struct route_node *prn,
347 struct route_entry *re, int proto)
6e26278c 348{
d62a17ae 349 int at_least_one = 0;
350 int rmap_family; /* Route map has diff AF family enum */
351 struct nexthop *nexthop;
352 int ret;
353
354 rmap_family = (family == AF_INET) ? AFI_IP : AFI_IP6;
355
356 if (prn && re) {
357 for (nexthop = re->nexthop; nexthop; nexthop = nexthop->next) {
358 ret = zebra_nht_route_map_check(rmap_family, proto,
359 &prn->p, re, nexthop);
360 if (ret != RMAP_DENYMATCH) {
361 SET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
362 at_least_one++; /* at least one valid NH */
363 } else {
364 UNSET_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE);
365 }
366 }
6e26278c 367 }
d62a17ae 368 return (at_least_one);
6e26278c
DS
369}
370
d50b5bdd 371/*
f0f77c9a 372 * Determine appropriate route (RE entry) resolving a tracked entry
d50b5bdd 373 * (nexthop or BGP route for import).
374 */
d62a17ae 375static struct route_entry *zebra_rnh_resolve_entry(vrf_id_t vrfid, int family,
376 rnh_type_t type,
377 struct route_node *nrn,
378 struct rnh *rnh,
379 struct route_node **prn)
fb018d25 380{
d62a17ae 381 struct route_table *route_table;
382 struct route_node *rn;
383 struct route_entry *re;
384
385 *prn = NULL;
386
387 route_table = zebra_vrf_table(family2afi(family), SAFI_UNICAST, vrfid);
388 if (!route_table) // unexpected
389 return NULL;
390
391 rn = route_node_match(route_table, &nrn->p);
392 if (!rn)
393 return NULL;
394
395 /* When resolving nexthops, do not resolve via the default route unless
396 * 'ip nht resolve-via-default' is configured.
397 */
398 if ((type == RNH_NEXTHOP_TYPE)
399 && (is_default_prefix(&rn->p)
400 && !nh_resolve_via_default(rn->p.family)))
401 re = NULL;
402 else if ((type == RNH_IMPORT_CHECK_TYPE)
403 && CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH)
404 && !prefix_same(&nrn->p, &rn->p))
405 re = NULL;
406 else {
407 /* Identify appropriate route entry. */
408 RNODE_FOREACH_RE(rn, re)
409 {
410 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
411 continue;
412 if (!CHECK_FLAG(re->status, ROUTE_ENTRY_SELECTED_FIB))
413 continue;
414
415 if (CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) {
416 if (re->type == ZEBRA_ROUTE_CONNECT)
417 break;
418 if (re->type == ZEBRA_ROUTE_NHRP) {
419 struct nexthop *nexthop;
420 for (nexthop = re->nexthop; nexthop;
421 nexthop = nexthop->next)
422 if (nexthop->type
423 == NEXTHOP_TYPE_IFINDEX)
424 break;
425 if (nexthop)
426 break;
427 }
428 } else if ((type == RNH_IMPORT_CHECK_TYPE)
429 && (re->type == ZEBRA_ROUTE_BGP))
430 continue;
431 else
432 break;
433 }
434 }
435
436 /* Need to unlock route node */
437 route_unlock_node(rn);
438 if (re)
439 *prn = rn;
440 return re;
d50b5bdd 441}
442
443/*
444 * See if a tracked route entry for import (by BGP) has undergone any
445 * change, and if so, notify the client.
446 */
d62a17ae 447static void zebra_rnh_eval_import_check_entry(vrf_id_t vrfid, int family,
448 int force, struct route_node *nrn,
449 struct rnh *rnh,
450 struct route_entry *re)
d50b5bdd 451{
d62a17ae 452 int state_changed = 0;
453 struct zserv *client;
454 char bufn[INET6_ADDRSTRLEN];
455 struct listnode *node;
456 struct nexthop *nexthop;
457
458 if (re && (rnh->state == NULL)) {
459 for (ALL_NEXTHOPS(re->nexthop, nexthop))
460 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)) {
461 state_changed = 1;
462 break;
463 }
464 } else if (!re && (rnh->state != NULL))
465 state_changed = 1;
466
467 if (compare_state(re, rnh->state))
468 copy_state(rnh, re, nrn);
469
470 if (state_changed || force) {
471 if (IS_ZEBRA_DEBUG_NHT) {
472 prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN);
473 zlog_debug("%u:%s: Route import check %s %s\n", vrfid,
474 bufn, rnh->state ? "passed" : "failed",
475 state_changed ? "(state changed)" : "");
476 }
477 /* state changed, notify clients */
478 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) {
479 send_client(rnh, client, RNH_IMPORT_CHECK_TYPE, vrfid);
480 }
481 }
d50b5bdd 482}
fb018d25 483
d50b5bdd 484/*
485 * Notify clients registered for this nexthop about a change.
486 */
d62a17ae 487static void zebra_rnh_notify_protocol_clients(vrf_id_t vrfid, int family,
488 struct route_node *nrn,
489 struct rnh *rnh,
490 struct route_node *prn,
491 struct route_entry *re)
d50b5bdd 492{
d62a17ae 493 struct listnode *node;
494 struct zserv *client;
495 char bufn[INET6_ADDRSTRLEN];
496 char bufp[INET6_ADDRSTRLEN];
497 int num_resolving_nh;
498
499 if (IS_ZEBRA_DEBUG_NHT) {
500 prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN);
501 if (prn && re) {
502 prefix2str(&prn->p, bufp, INET6_ADDRSTRLEN);
503 zlog_debug("%u:%s: NH resolved over route %s", vrfid,
504 bufn, bufp);
505 } else
506 zlog_debug("%u:%s: NH has become unresolved", vrfid,
507 bufn);
508 }
509
510 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client)) {
511 if (prn && re) {
512 /* Apply route-map for this client to route resolving
513 * this
514 * nexthop to see if it is filtered or not.
515 */
516 num_resolving_nh = zebra_rnh_apply_nht_rmap(
517 family, prn, re, client->proto);
518 if (num_resolving_nh)
519 rnh->filtered[client->proto] = 0;
520 else
521 rnh->filtered[client->proto] = 1;
522
523 if (IS_ZEBRA_DEBUG_NHT)
524 zlog_debug(
525 "%u:%s: Notifying client %s about NH %s",
526 vrfid, bufn,
527 zebra_route_string(client->proto),
528 num_resolving_nh
529 ? ""
530 : "(filtered by route-map)");
531 } else {
532 rnh->filtered[client->proto] = 0;
533 if (IS_ZEBRA_DEBUG_NHT)
534 zlog_debug(
535 "%u:%s: Notifying client %s about NH (unreachable)",
536 vrfid, bufn,
537 zebra_route_string(client->proto));
538 }
539
540 send_client(rnh, client, RNH_NEXTHOP_TYPE, vrfid);
541 }
d50b5bdd 542}
078430f6 543
d62a17ae 544static void zebra_rnh_process_static_routes(vrf_id_t vrfid, int family,
545 struct route_node *nrn,
546 struct rnh *rnh,
547 struct route_node *prn,
548 struct route_entry *re)
d50b5bdd 549{
d62a17ae 550 struct listnode *node;
551 int num_resolving_nh = 0;
552 struct route_node *static_rn;
553 struct route_entry *sre;
554 struct nexthop *nexthop;
555 char bufn[INET6_ADDRSTRLEN];
556 char bufp[INET6_ADDRSTRLEN];
557 char bufs[INET6_ADDRSTRLEN];
558
559 if (IS_ZEBRA_DEBUG_NHT) {
560 prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN);
561 if (prn)
562 prefix2str(&prn->p, bufp, INET6_ADDRSTRLEN);
563 }
078430f6 564
d62a17ae 565 if (prn && re) {
566 /* Apply route-map for "static" to route resolving this
567 * nexthop to see if it is filtered or not.
568 */
569 num_resolving_nh = zebra_rnh_apply_nht_rmap(family, prn, re,
570 ZEBRA_ROUTE_STATIC);
571 if (num_resolving_nh)
572 rnh->filtered[ZEBRA_ROUTE_STATIC] = 0;
573 else
574 rnh->filtered[ZEBRA_ROUTE_STATIC] = 1;
575 } else
576 rnh->filtered[ZEBRA_ROUTE_STATIC] = 0;
577
578 /* Evaluate each static route associated with this nexthop. */
579 for (ALL_LIST_ELEMENTS_RO(rnh->zebra_static_route_list, node,
580 static_rn)) {
581 RNODE_FOREACH_RE(static_rn, sre)
582 {
583 if (sre->type != ZEBRA_ROUTE_STATIC)
584 continue;
585
586 /* Set the filter flag for the correct nexthop - static
587 * route may
588 * be having multiple. We care here only about
589 * registered nexthops.
590 */
591 for (nexthop = sre->nexthop; nexthop;
592 nexthop = nexthop->next) {
593 switch (nexthop->type) {
594 case NEXTHOP_TYPE_IPV4:
595 case NEXTHOP_TYPE_IPV4_IFINDEX:
596 if (nexthop->gate.ipv4.s_addr
597 == nrn->p.u.prefix4.s_addr) {
598 if (num_resolving_nh)
599 UNSET_FLAG(
600 nexthop->flags,
601 NEXTHOP_FLAG_FILTERED);
602 else
603 SET_FLAG(
604 nexthop->flags,
605 NEXTHOP_FLAG_FILTERED);
606 }
607 break;
608 case NEXTHOP_TYPE_IPV6:
609 case NEXTHOP_TYPE_IPV6_IFINDEX:
610
611 if (memcmp(&nexthop->gate.ipv6,
612 &nrn->p.u.prefix6, 16)
613 == 0) {
614 if (num_resolving_nh)
615 UNSET_FLAG(
616 nexthop->flags,
617 NEXTHOP_FLAG_FILTERED);
618 else
619 SET_FLAG(
620 nexthop->flags,
621 NEXTHOP_FLAG_FILTERED);
622 }
623 break;
624 default:
625 break;
626 }
627 }
628
629 if (IS_ZEBRA_DEBUG_NHT) {
630 prefix2str(&static_rn->p, bufs,
631 INET6_ADDRSTRLEN);
632 if (prn && re)
633 zlog_debug(
634 "%u:%s: NH change %s, scheduling static route %s",
635 vrfid, bufn,
636 num_resolving_nh
637 ? ""
638 : "(filtered by route-map)",
639 bufs);
640 else
641 zlog_debug(
642 "%u:%s: NH unreachable, scheduling static route %s",
643 vrfid, bufn, bufs);
644 }
645
646 SET_FLAG(sre->status, ROUTE_ENTRY_CHANGED);
647 SET_FLAG(sre->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
648 }
649
650 rib_queue_add(static_rn);
651 }
d50b5bdd 652}
078430f6 653
731a75fe
RW
654static void zebra_rnh_process_pseudowires(vrf_id_t vrfid, struct rnh *rnh)
655{
656 struct zebra_pw *pw;
657 struct listnode *node;
658
659 for (ALL_LIST_ELEMENTS_RO(rnh->zebra_pseudowire_list, node, pw))
660 zebra_pw_update(pw);
661}
662
d50b5bdd 663/*
664 * See if a tracked nexthop entry has undergone any change, and if so,
665 * take appropriate action; this involves notifying any clients and/or
666 * scheduling dependent static routes for processing.
667 */
d62a17ae 668static void zebra_rnh_eval_nexthop_entry(vrf_id_t vrfid, int family, int force,
669 struct route_node *nrn,
670 struct rnh *rnh,
671 struct route_node *prn,
672 struct route_entry *re)
d50b5bdd 673{
d62a17ae 674 int state_changed = 0;
675
676 /* If we're resolving over a different route, resolution has changed or
677 * the resolving route has some change (e.g., metric), there is a state
678 * change.
679 */
680 if (!prefix_same(&rnh->resolved_route, &prn->p)) {
681 if (prn)
682 prefix_copy(&rnh->resolved_route, &prn->p);
683 else
684 memset(&rnh->resolved_route, 0, sizeof(struct prefix));
685
686 copy_state(rnh, re, nrn);
687 state_changed = 1;
688 } else if (compare_state(re, rnh->state)) {
689 copy_state(rnh, re, nrn);
690 state_changed = 1;
691 }
692
693 if (state_changed || force) {
694 /* NOTE: Use the "copy" of resolving route stored in 'rnh' i.e.,
695 * rnh->state.
696 */
697 /* Notify registered protocol clients. */
698 zebra_rnh_notify_protocol_clients(vrfid, family, nrn, rnh, prn,
699 rnh->state);
700
701 /* Process static routes attached to this nexthop */
702 zebra_rnh_process_static_routes(vrfid, family, nrn, rnh, prn,
703 rnh->state);
731a75fe
RW
704
705 /* Process pseudowires attached to this nexthop */
706 zebra_rnh_process_pseudowires(vrfid, rnh);
d62a17ae 707 }
d50b5bdd 708}
6e26278c 709
d50b5bdd 710/* Evaluate one tracked entry */
d62a17ae 711static void zebra_rnh_evaluate_entry(vrf_id_t vrfid, int family, int force,
712 rnh_type_t type, struct route_node *nrn)
d50b5bdd 713{
d62a17ae 714 struct rnh *rnh;
715 struct route_entry *re;
716 struct route_node *prn;
717 char bufn[INET6_ADDRSTRLEN];
718
719 if (IS_ZEBRA_DEBUG_NHT) {
720 prefix2str(&nrn->p, bufn, INET6_ADDRSTRLEN);
721 zlog_debug("%u:%s: Evaluate RNH, type %d %s", vrfid, bufn, type,
722 force ? "(force)" : "");
723 }
724
725 rnh = nrn->info;
726
727 /* Identify route entry (RE) resolving this tracked entry. */
728 re = zebra_rnh_resolve_entry(vrfid, family, type, nrn, rnh, &prn);
729
730 /* If the entry cannot be resolved and that is also the existing state,
731 * there is nothing further to do.
732 */
733 if (!re && rnh->state == NULL && !force)
734 return;
735
736 /* Process based on type of entry. */
737 if (type == RNH_IMPORT_CHECK_TYPE)
738 zebra_rnh_eval_import_check_entry(vrfid, family, force, nrn,
739 rnh, re);
740 else
741 zebra_rnh_eval_nexthop_entry(vrfid, family, force, nrn, rnh,
742 prn, re);
d50b5bdd 743}
744
8ce5e6a3 745/*
f0f77c9a
DS
746 * Clear the ROUTE_ENTRY_NEXTHOPS_CHANGED flag
747 * from the re entries.
8ce5e6a3
DS
748 *
749 * Please note we are doing this *after* we have
750 * notified the world about each nexthop as that
f0f77c9a 751 * we can have a situation where one re entry
8ce5e6a3
DS
752 * covers multiple nexthops we are interested in.
753 */
d62a17ae 754static void zebra_rnh_clear_nhc_flag(vrf_id_t vrfid, int family,
755 rnh_type_t type, struct route_node *nrn)
8ce5e6a3 756{
d62a17ae 757 struct rnh *rnh;
758 struct route_entry *re;
759 struct route_node *prn;
8ce5e6a3 760
d62a17ae 761 rnh = nrn->info;
8ce5e6a3 762
d62a17ae 763 re = zebra_rnh_resolve_entry(vrfid, family, type, nrn, rnh, &prn);
8ce5e6a3 764
332ad713 765 if (re) {
d62a17ae 766 UNSET_FLAG(re->status, ROUTE_ENTRY_NEXTHOPS_CHANGED);
332ad713
RW
767 UNSET_FLAG(re->status, ROUTE_ENTRY_LABELS_CHANGED);
768 }
8ce5e6a3 769}
d50b5bdd 770
771/* Evaluate all tracked entries (nexthops or routes for import into BGP)
772 * of a particular VRF and address-family or a specific prefix.
773 */
d62a17ae 774void zebra_evaluate_rnh(vrf_id_t vrfid, int family, int force, rnh_type_t type,
775 struct prefix *p)
d50b5bdd 776{
d62a17ae 777 struct route_table *rnh_table;
778 struct route_node *nrn;
779
780 rnh_table = get_rnh_table(vrfid, family, type);
781 if (!rnh_table) // unexpected
782 return;
783
784 if (p) {
785 /* Evaluating a specific entry, make sure it exists. */
786 nrn = route_node_lookup(rnh_table, p);
787 if (nrn && nrn->info)
788 zebra_rnh_evaluate_entry(vrfid, family, force, type,
789 nrn);
790
791 if (nrn)
792 route_unlock_node(nrn);
793 } else {
794 /* Evaluate entire table. */
795 nrn = route_top(rnh_table);
796 while (nrn) {
797 if (nrn->info)
798 zebra_rnh_evaluate_entry(vrfid, family, force,
799 type, nrn);
800 nrn = route_next(nrn); /* this will also unlock nrn */
801 }
802 nrn = route_top(rnh_table);
803 while (nrn) {
804 if (nrn->info)
805 zebra_rnh_clear_nhc_flag(vrfid, family, type,
806 nrn);
807 nrn = route_next(nrn); /* this will also unlock nrn */
808 }
809 }
fb018d25
DS
810}
811
d62a17ae 812void zebra_print_rnh_table(vrf_id_t vrfid, int af, struct vty *vty,
813 rnh_type_t type)
fb018d25 814{
d62a17ae 815 struct route_table *table;
816 struct route_node *rn;
817
818 table = get_rnh_table(vrfid, af, type);
819 if (!table) {
820 zlog_debug("print_rnhs: rnh table not found\n");
821 return;
822 }
823
824 for (rn = route_top(table); rn; rn = route_next(rn))
825 if (rn->info)
826 print_rnh(rn, vty);
fb018d25
DS
827}
828
d62a17ae 829int zebra_cleanup_rnh_client(vrf_id_t vrf_id, int family, struct zserv *client,
830 rnh_type_t type)
fb018d25 831{
d62a17ae 832 struct route_table *ntable;
833 struct route_node *nrn;
834 struct rnh *rnh;
835
836 if (IS_ZEBRA_DEBUG_NHT)
837 zlog_debug("%u: Client %s RNH cleanup for family %d type %d",
838 vrf_id, zebra_route_string(client->proto), family,
839 type);
840
841 ntable = get_rnh_table(vrf_id, family, type);
842 if (!ntable) {
843 zlog_debug("cleanup_rnh_client: rnh table not found\n");
844 return -1;
845 }
846
847 for (nrn = route_top(ntable); nrn; nrn = route_next(nrn)) {
848 if (!nrn->info)
849 continue;
850
851 rnh = nrn->info;
852 zebra_remove_rnh_client(rnh, client, type);
853 }
854 return 1;
fb018d25
DS
855}
856
857/**
f0f77c9a 858 * free_state - free up the re structure associated with the rnh.
fb018d25 859 */
d62a17ae 860static void free_state(vrf_id_t vrf_id, struct route_entry *re,
861 struct route_node *rn)
fb018d25 862{
fb018d25 863
d62a17ae 864 if (!re)
865 return;
fb018d25 866
d62a17ae 867 /* free RE and nexthops */
868 zebra_deregister_rnh_static_nexthops(vrf_id, re->nexthop, rn);
869 nexthops_free(re->nexthop);
870 XFREE(MTYPE_RE, re);
fb018d25
DS
871}
872
d62a17ae 873static void copy_state(struct rnh *rnh, struct route_entry *re,
874 struct route_node *rn)
fb018d25 875{
d62a17ae 876 struct route_entry *state;
fb018d25 877
d62a17ae 878 if (rnh->state) {
879 free_state(rnh->vrf_id, rnh->state, rn);
880 rnh->state = NULL;
881 }
fb018d25 882
d62a17ae 883 if (!re)
884 return;
fb018d25 885
d62a17ae 886 state = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
887 state->type = re->type;
7733c6c4 888 state->distance = re->distance;
d62a17ae 889 state->metric = re->metric;
fb018d25 890
d62a17ae 891 route_entry_copy_nexthops(state, re->nexthop);
892 rnh->state = state;
fb018d25
DS
893}
894
d62a17ae 895static int compare_state(struct route_entry *r1, struct route_entry *r2)
fb018d25 896{
fb018d25 897
d62a17ae 898 if (!r1 && !r2)
899 return 0;
fb018d25 900
d62a17ae 901 if ((!r1 && r2) || (r1 && !r2))
902 return 1;
fb018d25 903
7733c6c4
JB
904 if (r1->distance != r2->distance)
905 return 1;
906
d62a17ae 907 if (r1->metric != r2->metric)
908 return 1;
fb018d25 909
d62a17ae 910 if (r1->nexthop_num != r2->nexthop_num)
911 return 1;
fb018d25 912
332ad713
RW
913 if (CHECK_FLAG(r1->status, ROUTE_ENTRY_NEXTHOPS_CHANGED)
914 || CHECK_FLAG(r1->status, ROUTE_ENTRY_LABELS_CHANGED))
d62a17ae 915 return 1;
fb018d25 916
d62a17ae 917 return 0;
fb018d25
DS
918}
919
d62a17ae 920static int send_client(struct rnh *rnh, struct zserv *client, rnh_type_t type,
921 vrf_id_t vrf_id)
fb018d25 922{
d62a17ae 923 struct stream *s;
924 struct route_entry *re;
925 unsigned long nump;
926 u_char num;
927 struct nexthop *nexthop;
928 struct route_node *rn;
929 int cmd = (type == RNH_IMPORT_CHECK_TYPE) ? ZEBRA_IMPORT_CHECK_UPDATE
930 : ZEBRA_NEXTHOP_UPDATE;
931
932 rn = rnh->node;
933 re = rnh->state;
934
935 /* Get output stream. */
936 s = client->obuf;
937 stream_reset(s);
938
939 zserv_create_header(s, cmd, vrf_id);
940
941 stream_putw(s, rn->p.family);
942 switch (rn->p.family) {
943 case AF_INET:
944 stream_putc(s, rn->p.prefixlen);
945 stream_put_in_addr(s, &rn->p.u.prefix4);
fb018d25 946 break;
d62a17ae 947 case AF_INET6:
948 stream_putc(s, rn->p.prefixlen);
949 stream_put(s, &rn->p.u.prefix6, IPV6_MAX_BYTELEN);
fb018d25 950 break;
d62a17ae 951 default:
952 zlog_err("%s: Unknown family (%d) notification attempted\n",
953 __FUNCTION__, rn->p.family);
fb018d25 954 break;
d62a17ae 955 }
956 if (re) {
957 stream_putc(s, re->distance);
958 stream_putl(s, re->metric);
959 num = 0;
960 nump = stream_get_endp(s);
961 stream_putc(s, 0);
962 for (nexthop = re->nexthop; nexthop; nexthop = nexthop->next)
963 if ((CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
964 || CHECK_FLAG(nexthop->flags,
965 NEXTHOP_FLAG_RECURSIVE))
966 && CHECK_FLAG(nexthop->flags,
967 NEXTHOP_FLAG_ACTIVE)) {
968 stream_putc(s, nexthop->type);
969 switch (nexthop->type) {
970 case NEXTHOP_TYPE_IPV4:
971 stream_put_in_addr(s,
972 &nexthop->gate.ipv4);
973 stream_putl(s, nexthop->ifindex);
974 break;
975 case NEXTHOP_TYPE_IFINDEX:
976 stream_putl(s, nexthop->ifindex);
977 break;
978 case NEXTHOP_TYPE_IPV4_IFINDEX:
979 stream_put_in_addr(s,
980 &nexthop->gate.ipv4);
981 stream_putl(s, nexthop->ifindex);
982 break;
983 case NEXTHOP_TYPE_IPV6:
984 stream_put(s, &nexthop->gate.ipv6, 16);
985 stream_putl(s, nexthop->ifindex);
986 break;
987 case NEXTHOP_TYPE_IPV6_IFINDEX:
988 stream_put(s, &nexthop->gate.ipv6, 16);
989 stream_putl(s, nexthop->ifindex);
990 break;
991 default:
992 /* do nothing */
993 break;
994 }
995 num++;
996 }
997 stream_putc_at(s, nump, num);
998 } else {
999 stream_putc(s, 0); // distance
1000 stream_putl(s, 0); // metric
1001 stream_putc(s, 0); // nexthops
1002 }
1003 stream_putw_at(s, 0, stream_get_endp(s));
1004
1005 client->nh_last_upd_time = monotime(NULL);
1006 client->last_write_cmd = cmd;
1007 return zebra_server_send_message(client);
fb018d25
DS
1008}
1009
d62a17ae 1010static void print_nh(struct nexthop *nexthop, struct vty *vty)
fb018d25 1011{
d62a17ae 1012 char buf[BUFSIZ];
1013 struct zebra_ns *zns = zebra_ns_lookup(NS_DEFAULT);
1014
1015 switch (nexthop->type) {
1016 case NEXTHOP_TYPE_IPV4:
1017 case NEXTHOP_TYPE_IPV4_IFINDEX:
1018 vty_out(vty, " via %s", inet_ntoa(nexthop->gate.ipv4));
1019 if (nexthop->ifindex)
1020 vty_out(vty, ", %s",
1021 ifindex2ifname_per_ns(zns, nexthop->ifindex));
1022 break;
1023 case NEXTHOP_TYPE_IPV6:
1024 case NEXTHOP_TYPE_IPV6_IFINDEX:
1025 vty_out(vty, " %s",
1026 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));
1027 if (nexthop->ifindex)
1028 vty_out(vty, ", via %s",
1029 ifindex2ifname_per_ns(zns, nexthop->ifindex));
1030 break;
1031 case NEXTHOP_TYPE_IFINDEX:
1032 vty_out(vty, " is directly connected, %s",
1033 ifindex2ifname_per_ns(zns, nexthop->ifindex));
1034 break;
1035 case NEXTHOP_TYPE_BLACKHOLE:
1036 vty_out(vty, " is directly connected, Null0");
1037 break;
1038 default:
1039 break;
1040 }
1041 vty_out(vty, "\n");
fb018d25
DS
1042}
1043
d62a17ae 1044static void print_rnh(struct route_node *rn, struct vty *vty)
fb018d25 1045{
d62a17ae 1046 struct rnh *rnh;
1047 struct nexthop *nexthop;
1048 struct listnode *node;
1049 struct zserv *client;
1050 char buf[BUFSIZ];
1051
1052 rnh = rn->info;
1053 vty_out(vty, "%s%s\n",
1054 inet_ntop(rn->p.family, &rn->p.u.prefix, buf, BUFSIZ),
1055 CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED) ? "(Connected)"
1056 : "");
1057 if (rnh->state) {
1058 vty_out(vty, " resolved via %s\n",
1059 zebra_route_string(rnh->state->type));
1060 for (nexthop = rnh->state->nexthop; nexthop;
1061 nexthop = nexthop->next)
1062 print_nh(nexthop, vty);
1063 } else
1064 vty_out(vty, " unresolved%s\n",
1065 CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)
1066 ? "(Connected)"
1067 : "");
1068
1069 vty_out(vty, " Client list:");
1070 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client))
1071 vty_out(vty, " %s(fd %d)%s", zebra_route_string(client->proto),
1072 client->sock,
1073 rnh->filtered[client->proto] ? "(filtered)" : "");
1074 if (!list_isempty(rnh->zebra_static_route_list))
1075 vty_out(vty, " zebra%s",
1076 rnh->filtered[ZEBRA_ROUTE_STATIC] ? "(filtered)" : "");
731a75fe
RW
1077 if (!list_isempty(rnh->zebra_pseudowire_list))
1078 vty_out(vty, " zebra[pseudowires]");
d62a17ae 1079 vty_out(vty, "\n");
fb018d25 1080}