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