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