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