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