]> git.proxmox.com Git - mirror_frr.git/blob - zebra/zebra_rnh.c
Merge pull request #537 from donaldsharp/vrf_stuff
[mirror_frr.git] / zebra / zebra_rnh.c
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 *
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
19 */
20
21 #include <zebra.h>
22
23 #include "prefix.h"
24 #include "table.h"
25 #include "memory.h"
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"
37 #include "vrf.h"
38
39 #include "zebra/rib.h"
40 #include "zebra/rt.h"
41 #include "zebra/zserv.h"
42 #include "zebra/zebra_ns.h"
43 #include "zebra/zebra_vrf.h"
44 #include "zebra/redistribute.h"
45 #include "zebra/debug.h"
46 #include "zebra/zebra_rnh.h"
47 #include "zebra/zebra_routemap.h"
48 #include "zebra/interface.h"
49 #include "zebra/zebra_memory.h"
50
51 static void free_state(vrf_id_t vrf_id, struct rib *rib, struct route_node *rn);
52 static void copy_state(struct rnh *rnh, struct rib *rib,
53 struct route_node *rn);
54 #define lookup_rnh_table(v, f) \
55 ({ \
56 struct zebra_vrf *zvrf; \
57 struct route_table *t = NULL; \
58 zvrf = zebra_vrf_lookup_by_id(v); \
59 if (zvrf) \
60 t = zvrf->rnh_table[family2afi(f)]; \
61 t; \
62 })
63
64 static int compare_state(struct rib *r1, struct rib *r2);
65 static int send_client(struct rnh *rnh, struct zserv *client, rnh_type_t type,
66 vrf_id_t vrf_id);
67 static void print_rnh(struct route_node *rn, struct vty *vty);
68
69 int zebra_rnh_ip_default_route = 0;
70 int zebra_rnh_ipv6_default_route = 0;
71
72 static inline struct route_table *get_rnh_table(vrf_id_t vrfid, int family,
73 rnh_type_t type)
74 {
75 struct zebra_vrf *zvrf;
76 struct route_table *t = NULL;
77
78 zvrf = zebra_vrf_lookup_by_id(vrfid);
79 if (zvrf)
80 switch (type)
81 {
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;
91 }
92
93 char *rnh_str (struct rnh *rnh, char *buf, int size)
94 {
95 prefix2str(&(rnh->node->p), buf, size);
96 return buf;
97 }
98
99 struct rnh *
100 zebra_add_rnh (struct prefix *p, vrf_id_t vrfid, rnh_type_t type)
101 {
102 struct route_table *table;
103 struct route_node *rn;
104 struct rnh *rnh = NULL;
105 char buf[PREFIX2STR_BUFFER];
106
107 if (IS_ZEBRA_DEBUG_NHT)
108 {
109 prefix2str(p, buf, sizeof (buf));
110 zlog_debug("%u: Add RNH %s type %d", vrfid, buf, type);
111 }
112 table = get_rnh_table(vrfid, PREFIX_FAMILY(p), type);
113 if (!table)
114 {
115 prefix2str(p, buf, sizeof (buf));
116 zlog_warn("%u: Add RNH %s type %d - table not found",
117 vrfid, buf, type);
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();
131 rnh->vrf_id = vrfid;
132 rnh->zebra_static_route_list = list_new();
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
142 struct rnh *
143 zebra_lookup_rnh (struct prefix *p, vrf_id_t vrfid, rnh_type_t type)
144 {
145 struct route_table *table;
146 struct route_node *rn;
147
148 table = get_rnh_table(vrfid, PREFIX_FAMILY(p), type);
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
164 void
165 zebra_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
174 void
175 zebra_delete_rnh (struct rnh *rnh, rnh_type_t type)
176 {
177 struct route_node *rn;
178
179 if (!rnh || (rnh->flags & ZEBRA_NHT_DELETED) || !(rn = rnh->node))
180 return;
181
182 if (IS_ZEBRA_DEBUG_NHT)
183 {
184 char buf[PREFIX2STR_BUFFER];
185 zlog_debug("%u: Del RNH %s type %d",
186 rnh->vrf_id, rnh_str(rnh, buf, sizeof (buf)), type);
187 }
188
189 zebra_free_rnh (rnh);
190 rn->info = NULL;
191 route_unlock_node (rn);
192 }
193
194 void
195 zebra_add_rnh_client (struct rnh *rnh, struct zserv *client, rnh_type_t type,
196 vrf_id_t vrf_id)
197 {
198 if (IS_ZEBRA_DEBUG_NHT)
199 {
200 char buf[PREFIX2STR_BUFFER];
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);
204 }
205 if (!listnode_lookup(rnh->client_list, client))
206 {
207 listnode_add(rnh->client_list, client);
208 send_client(rnh, client, type, vrf_id); // Pending: check if its needed
209 }
210 }
211
212 void
213 zebra_remove_rnh_client (struct rnh *rnh, struct zserv *client, rnh_type_t type)
214 {
215 if (IS_ZEBRA_DEBUG_NHT)
216 {
217 char buf[PREFIX2STR_BUFFER];
218 zlog_debug("Client %s unregisters for RNH %s type %d",
219 zebra_route_string(client->proto),
220 rnh_str(rnh, buf, sizeof (buf)), type);
221 }
222 listnode_delete(rnh->client_list, client);
223 if (list_isempty(rnh->client_list) &&
224 list_isempty(rnh->zebra_static_route_list))
225 zebra_delete_rnh(rnh, type);
226 }
227
228 void
229 zebra_register_rnh_static_nh(vrf_id_t vrf_id, struct prefix *nh,
230 struct route_node *static_rn)
231 {
232 struct rnh *rnh;
233
234 rnh = zebra_add_rnh(nh, vrf_id, RNH_NEXTHOP_TYPE);
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
241 void
242 zebra_deregister_rnh_static_nh(vrf_id_t vrf_id, struct prefix *nh,
243 struct route_node *static_rn)
244 {
245 struct rnh *rnh;
246
247 rnh = zebra_lookup_rnh(nh, vrf_id, RNH_NEXTHOP_TYPE);
248 if (!rnh || (rnh->flags & ZEBRA_NHT_DELETED))
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))
255 zebra_delete_rnh(rnh, RNH_NEXTHOP_TYPE);
256 }
257
258 void
259 zebra_deregister_rnh_static_nexthops (vrf_id_t vrf_id, struct nexthop *nexthop,
260 struct route_node *rn)
261 {
262 struct nexthop *nh;
263 struct prefix nh_p;
264
265 for (nh = nexthop; nh ; nh = nh->next)
266 {
267 switch (nh->type)
268 {
269 case NEXTHOP_TYPE_IPV4:
270 case NEXTHOP_TYPE_IPV4_IFINDEX:
271 nh_p.family = AF_INET;
272 nh_p.prefixlen = IPV4_MAX_BITLEN;
273 nh_p.u.prefix4 = nh->gate.ipv4;
274 break;
275 case NEXTHOP_TYPE_IPV6:
276 case NEXTHOP_TYPE_IPV6_IFINDEX:
277 nh_p.family = AF_INET6;
278 nh_p.prefixlen = IPV6_MAX_BITLEN;
279 nh_p.u.prefix6 = nh->gate.ipv6;
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 }
299 zebra_deregister_rnh_static_nh(vrf_id, &nh_p, rn);
300 }
301 }
302
303 /* Apply the NHT route-map for a client to the route (and nexthops)
304 * resolving a NH.
305 */
306 static int
307 zebra_rnh_apply_nht_rmap(int family, struct route_node *prn,
308 struct rib *rib, int proto)
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
337 /*
338 * Determine appropriate route (RIB entry) resolving a tracked entry
339 * (nexthop or BGP route for import).
340 */
341 static struct rib *
342 zebra_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)
345 {
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
360 /* When resolving nexthops, do not resolve via the default route unless
361 * 'ip nht resolve-via-default' is configured.
362 */
363 if ((type == RNH_NEXTHOP_TYPE) &&
364 (is_default_prefix (&rn->p) &&
365 !nh_resolve_via_default(rn->p.family)))
366 rib = NULL;
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;
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;
378 if (! CHECK_FLAG (rib->status, RIB_ENTRY_SELECTED_FIB))
379 continue;
380
381 if (CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
382 {
383 if (rib->type == ZEBRA_ROUTE_CONNECT)
384 break;
385 if (rib->type == ZEBRA_ROUTE_NHRP)
386 {
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)
392 break;
393 }
394 }
395 else if ((type == RNH_IMPORT_CHECK_TYPE) &&
396 (rib->type == ZEBRA_ROUTE_BGP))
397 continue;
398 else
399 break;
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 */
414 static void
415 zebra_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;
420 struct zserv *client;
421 char bufn[INET6_ADDRSTRLEN];
422 struct listnode *node;
423 struct nexthop *nexthop, *tnexthop;
424 int recursing;
425
426 if (rib && (rnh->state == NULL))
427 {
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 }
434 }
435 else if (!rib && (rnh->state != NULL))
436 state_changed = 1;
437
438 if (compare_state(rib, rnh->state))
439 copy_state(rnh, rib, nrn);
440
441 if (state_changed || force)
442 {
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 }
455 }
456 }
457
458 /*
459 * Notify clients registered for this nexthop about a change.
460 */
461 static void
462 zebra_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;
471
472 if (IS_ZEBRA_DEBUG_NHT)
473 {
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 }
483
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 }
510
511 send_client(rnh, client, RNH_NEXTHOP_TYPE, vrfid);
512 }
513 }
514
515 static void
516 zebra_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];
528
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.
540 */
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;
545 else
546 rnh->filtered[ZEBRA_ROUTE_STATIC] = 1;
547 }
548 else
549 rnh->filtered[ZEBRA_ROUTE_STATIC] = 0;
550
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)
558 continue;
559
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:
579
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 }
592
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
605 SET_FLAG(srib->status, RIB_ENTRY_CHANGED);
606 SET_FLAG(srib->status, RIB_ENTRY_NEXTHOPS_CHANGED);
607 }
608
609 rib_queue_add(static_rn);
610 }
611 }
612
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 */
618 static void
619 zebra_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;
624
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 {
631 if (prn)
632 prefix_copy(&rnh->resolved_route, &prn->p);
633 else
634 memset(&rnh->resolved_route, 0, sizeof(struct prefix));
635
636 copy_state(rnh, rib, nrn);
637 state_changed = 1;
638 }
639 else if (compare_state(rib, rnh->state))
640 {
641 copy_state(rnh, rib, nrn);
642 state_changed = 1;
643 }
644
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);
653
654 /* Process static routes attached to this nexthop */
655 zebra_rnh_process_static_routes (vrfid, family, nrn, rnh,
656 prn, rnh->state);
657 }
658 }
659
660 /* Evaluate one tracked entry */
661 static void
662 zebra_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];
669
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 }
676
677 rnh = nrn->info;
678
679 /* Identify route entry (RIB) resolving this tracked entry. */
680 rib = zebra_rnh_resolve_entry (vrfid, family, type, nrn, rnh, &prn);
681
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;
687
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
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 */
706 static void
707 zebra_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 }
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 */
725 void
726 zebra_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;
735
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);
742
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 }
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 }
763 }
764 }
765
766 void
767 zebra_print_rnh_table (vrf_id_t vrfid, int af, struct vty *vty, rnh_type_t type)
768 {
769 struct route_table *table;
770 struct route_node *rn;
771
772 table = get_rnh_table(vrfid, af, type);
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
784 int
785 zebra_cleanup_rnh_client (vrf_id_t vrf_id, int family, struct zserv *client,
786 rnh_type_t type)
787 {
788 struct route_table *ntable;
789 struct route_node *nrn;
790 struct rnh *rnh;
791
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);
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;
809 zebra_remove_rnh_client(rnh, client, type);
810 }
811 return 1;
812 }
813
814 /**
815 * free_state - free up the rib structure associated with the rnh.
816 */
817 static void
818 free_state (vrf_id_t vrf_id, struct rib *rib, struct route_node *rn)
819 {
820
821 if (!rib)
822 return;
823
824 /* free RIB and nexthops */
825 zebra_deregister_rnh_static_nexthops (vrf_id, rib->nexthop, rn);
826 nexthops_free(rib->nexthop);
827 XFREE (MTYPE_RIB, rib);
828 }
829
830 static void
831 copy_state (struct rnh *rnh, struct rib *rib, struct route_node *rn)
832 {
833 struct rib *state;
834 struct nexthop *nh;
835
836 if (rnh->state)
837 {
838 free_state(rnh->vrf_id, rnh->state, rn);
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)
850 rib_copy_nexthops(state, nh);
851 rnh->state = state;
852 }
853
854 static int
855 compare_state (struct rib *r1, struct rib *r2)
856 {
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
870 if (CHECK_FLAG(r1->status, RIB_ENTRY_NEXTHOPS_CHANGED))
871 return 1;
872
873 return 0;
874 }
875
876 static int
877 send_client (struct rnh *rnh, struct zserv *client, rnh_type_t type, vrf_id_t vrf_id)
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;
885 int cmd = (type == RNH_IMPORT_CHECK_TYPE)
886 ? ZEBRA_IMPORT_CHECK_UPDATE : ZEBRA_NEXTHOP_UPDATE;
887
888 rn = rnh->node;
889 rib = rnh->state;
890
891 /* Get output stream. */
892 s = client->obuf;
893 stream_reset (s);
894
895 zserv_create_header (s, cmd, vrf_id);
896
897 stream_putw(s, rn->p.family);
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 }
913 if (rib)
914 {
915 stream_putc (s, rib->distance);
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)
921 if ((CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ||
922 CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) &&
923 CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
924 {
925 stream_putc (s, nexthop->type);
926 switch (nexthop->type)
927 {
928 case NEXTHOP_TYPE_IPV4:
929 stream_put_in_addr (s, &nexthop->gate.ipv4);
930 stream_putl (s, nexthop->ifindex);
931 break;
932 case NEXTHOP_TYPE_IFINDEX:
933 stream_putl (s, nexthop->ifindex);
934 break;
935 case NEXTHOP_TYPE_IPV4_IFINDEX:
936 stream_put_in_addr (s, &nexthop->gate.ipv4);
937 stream_putl (s, nexthop->ifindex);
938 break;
939 case NEXTHOP_TYPE_IPV6:
940 stream_put (s, &nexthop->gate.ipv6, 16);
941 stream_putl (s, nexthop->ifindex);
942 break;
943 case NEXTHOP_TYPE_IPV6_IFINDEX:
944 stream_put (s, &nexthop->gate.ipv6, 16);
945 stream_putl (s, nexthop->ifindex);
946 break;
947 default:
948 /* do nothing */
949 break;
950 }
951 num++;
952 }
953 stream_putc_at (s, nump, num);
954 }
955 else
956 {
957 stream_putc (s, 0); // distance
958 stream_putl (s, 0); // metric
959 stream_putc (s, 0); // nexthops
960 }
961 stream_putw_at (s, 0, stream_get_endp (s));
962
963 client->nh_last_upd_time = monotime(NULL);
964 client->last_write_cmd = cmd;
965 return zebra_server_send_message(client);
966 }
967
968 static void
969 print_nh (struct nexthop *nexthop, struct vty *vty)
970 {
971 char buf[BUFSIZ];
972 struct zebra_ns *zns = zebra_ns_lookup (NS_DEFAULT);
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)
980 vty_out (vty, ", %s", ifindex2ifname_per_ns (zns, nexthop->ifindex));
981 break;
982 case NEXTHOP_TYPE_IPV6:
983 case NEXTHOP_TYPE_IPV6_IFINDEX:
984 vty_out (vty, " %s",
985 inet_ntop (AF_INET6, &nexthop->gate.ipv6, buf, BUFSIZ));
986 if (nexthop->ifindex)
987 vty_out (vty, ", via %s", ifindex2ifname_per_ns (zns, nexthop->ifindex));
988 break;
989 case NEXTHOP_TYPE_IFINDEX:
990 vty_out (vty, " is directly connected, %s",
991 ifindex2ifname_per_ns (zns, nexthop->ifindex));
992 break;
993 case NEXTHOP_TYPE_BLACKHOLE:
994 vty_out (vty, " is directly connected, Null0");
995 break;
996 default:
997 break;
998 }
999 vty_out(vty, "%s", VTY_NEWLINE);
1000 }
1001
1002 static void
1003 print_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;
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)" : "",
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
1023 vty_out(vty, " unresolved%s%s",
1024 CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED) ? "(Connected)" : "",
1025 VTY_NEWLINE);
1026
1027 vty_out(vty, " Client list:");
1028 for (ALL_LIST_ELEMENTS_RO(rnh->client_list, node, client))
1029 vty_out(vty, " %s(fd %d)%s", zebra_route_string(client->proto),
1030 client->sock, rnh->filtered[client->proto] ? "(filtered)" : "");
1031 if (!list_isempty(rnh->zebra_static_route_list))
1032 vty_out(vty, " zebra%s", rnh->filtered[ZEBRA_ROUTE_STATIC] ? "(filtered)" : "");
1033 vty_out(vty, "%s", VTY_NEWLINE);
1034 }