]> git.proxmox.com Git - mirror_frr.git/blob - staticd/static_routes.c
staticd: Fix crash with NULL si
[mirror_frr.git] / staticd / static_routes.c
1 /*
2 * STATICd - route code
3 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
6 * This program 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 Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * 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 #include <zebra.h>
21
22 #include <lib/nexthop.h>
23 #include <lib/memory.h>
24 #include <lib/srcdest_table.h>
25 #include <lib/if.h>
26 #include <lib/vty.h>
27 #include <lib/vrf.h>
28 #include <lib/memory.h>
29
30 #include "static_vrf.h"
31 #include "static_routes.h"
32 #include "static_memory.h"
33 #include "static_zebra.h"
34
35 /* Install static route into rib. */
36 static void static_install_route(struct route_node *rn,
37 struct static_route *si_changed, safi_t safi)
38 {
39 struct static_route *si;
40
41 for (si = rn->info; si; si = si->next)
42 static_zebra_nht_register(si, true);
43
44 si = rn->info;
45 if (si)
46 static_zebra_route_add(rn, si_changed, si->vrf_id, safi, true);
47
48 }
49
50 /* Uninstall static route from RIB. */
51 static void static_uninstall_route(vrf_id_t vrf_id, safi_t safi,
52 struct route_node *rn,
53 struct static_route *si_changed)
54 {
55
56 if (rn->info)
57 static_zebra_route_add(rn, si_changed, vrf_id, safi, true);
58 else
59 static_zebra_route_add(rn, si_changed, vrf_id, safi, false);
60 }
61
62 int static_add_route(afi_t afi, safi_t safi, uint8_t type, struct prefix *p,
63 struct prefix_ipv6 *src_p, union g_addr *gate,
64 const char *ifname, enum static_blackhole_type bh_type,
65 route_tag_t tag, uint8_t distance, struct static_vrf *svrf,
66 struct static_vrf *nh_svrf,
67 struct static_nh_label *snh_label,
68 uint32_t table_id)
69 {
70 struct route_node *rn;
71 struct static_route *si;
72 struct static_route *pp;
73 struct static_route *cp;
74 struct static_route *update = NULL;
75 struct route_table *stable = svrf->stable[afi][safi];
76
77 if (!stable)
78 return -1;
79
80 if (!gate && (type == STATIC_IPV4_GATEWAY
81 || type == STATIC_IPV4_GATEWAY_IFNAME
82 || type == STATIC_IPV6_GATEWAY
83 || type == STATIC_IPV6_GATEWAY_IFNAME))
84 return -1;
85
86 if (!ifname
87 && (type == STATIC_IFNAME || type == STATIC_IPV4_GATEWAY_IFNAME
88 || type == STATIC_IPV6_GATEWAY_IFNAME))
89 return -1;
90
91 /* Lookup static route prefix. */
92 rn = srcdest_rnode_get(stable, p, src_p);
93
94 /* Do nothing if there is a same static route. */
95 for (si = rn->info; si; si = si->next) {
96 if (type == si->type
97 && (!gate
98 || ((afi == AFI_IP
99 && IPV4_ADDR_SAME(&gate->ipv4, &si->addr.ipv4))
100 || (afi == AFI_IP6
101 && IPV6_ADDR_SAME(gate, &si->addr.ipv6))))
102 && (!strcmp(ifname ? ifname : "", si->ifname))) {
103 if ((distance == si->distance) && (tag == si->tag)
104 && (table_id == si->table_id)
105 && !memcmp(&si->snh_label, snh_label,
106 sizeof(struct static_nh_label))
107 && si->bh_type == bh_type) {
108 route_unlock_node(rn);
109 return 0;
110 }
111 update = si;
112 }
113 }
114
115 /* Distance or tag or label changed, delete existing first. */
116 if (update)
117 static_delete_route(afi, safi, type, p, src_p, gate, ifname,
118 update->tag, update->distance, svrf,
119 &update->snh_label, table_id);
120
121 /* Make new static route structure. */
122 si = XCALLOC(MTYPE_STATIC_ROUTE, sizeof(struct static_route));
123
124 si->type = type;
125 si->distance = distance;
126 si->bh_type = bh_type;
127 si->tag = tag;
128 si->vrf_id = svrf->vrf->vrf_id;
129 si->nh_vrf_id = nh_svrf->vrf->vrf_id;
130 strcpy(si->nh_vrfname, nh_svrf->vrf->name);
131 si->table_id = table_id;
132
133 if (ifname)
134 strlcpy(si->ifname, ifname, sizeof(si->ifname));
135 si->ifindex = IFINDEX_INTERNAL;
136
137 switch (type) {
138 case STATIC_IPV4_GATEWAY:
139 case STATIC_IPV4_GATEWAY_IFNAME:
140 si->addr.ipv4 = gate->ipv4;
141 break;
142 case STATIC_IPV6_GATEWAY:
143 case STATIC_IPV6_GATEWAY_IFNAME:
144 si->addr.ipv6 = gate->ipv6;
145 break;
146 case STATIC_IFNAME:
147 break;
148 }
149
150 /* Save labels, if any. */
151 memcpy(&si->snh_label, snh_label, sizeof(struct static_nh_label));
152
153 /*
154 * Add new static route information to the tree with sort by
155 * distance value and gateway address.
156 */
157 for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) {
158 if (si->distance < cp->distance)
159 break;
160 if (si->distance > cp->distance)
161 continue;
162 if (si->type == STATIC_IPV4_GATEWAY
163 && cp->type == STATIC_IPV4_GATEWAY) {
164 if (ntohl(si->addr.ipv4.s_addr)
165 < ntohl(cp->addr.ipv4.s_addr))
166 break;
167 if (ntohl(si->addr.ipv4.s_addr)
168 > ntohl(cp->addr.ipv4.s_addr))
169 continue;
170 }
171 }
172
173 /* Make linked list. */
174 if (pp)
175 pp->next = si;
176 else
177 rn->info = si;
178 if (cp)
179 cp->prev = si;
180 si->prev = pp;
181 si->next = cp;
182
183 /* check whether interface exists in system & install if it does */
184 if (!ifname)
185 static_install_route(rn, si, safi);
186 else {
187 struct interface *ifp;
188
189 ifp = if_lookup_by_name(ifname, nh_svrf->vrf->vrf_id);
190 if (ifp && ifp->ifindex != IFINDEX_INTERNAL) {
191 si->ifindex = ifp->ifindex;
192 static_install_route(rn, si, safi);
193 } else
194 zlog_warn("Static Route using %s interface not installed because the interface does not exist in specified vrf",
195 ifname);
196 }
197
198 return 1;
199 }
200
201 int static_delete_route(afi_t afi, safi_t safi, uint8_t type, struct prefix *p,
202 struct prefix_ipv6 *src_p, union g_addr *gate,
203 const char *ifname, route_tag_t tag, uint8_t distance,
204 struct static_vrf *svrf,
205 struct static_nh_label *snh_label,
206 uint32_t table_id)
207 {
208 struct route_node *rn;
209 struct static_route *si;
210 struct route_table *stable;
211
212 /* Lookup table. */
213 stable = static_vrf_static_table(afi, safi, svrf);
214 if (!stable)
215 return -1;
216
217 /* Lookup static route prefix. */
218 rn = srcdest_rnode_lookup(stable, p, src_p);
219 if (!rn)
220 return 0;
221
222 /* Find same static route is the tree */
223 for (si = rn->info; si; si = si->next)
224 if (type == si->type
225 && (!gate
226 || ((afi == AFI_IP
227 && IPV4_ADDR_SAME(&gate->ipv4, &si->addr.ipv4))
228 || (afi == AFI_IP6
229 && IPV6_ADDR_SAME(gate, &si->addr.ipv6))))
230 && (!strcmp(ifname ? ifname : "", si->ifname))
231 && (!tag || (tag == si->tag))
232 && (table_id == si->table_id)
233 && (!snh_label->num_labels
234 || !memcmp(&si->snh_label, snh_label,
235 sizeof(struct static_nh_label))))
236 break;
237
238 /* Can't find static route. */
239 if (!si) {
240 route_unlock_node(rn);
241 return 0;
242 }
243
244 static_zebra_nht_register(si, false);
245
246 /* Unlink static route from linked list. */
247 if (si->prev)
248 si->prev->next = si->next;
249 else
250 rn->info = si->next;
251 if (si->next)
252 si->next->prev = si->prev;
253
254 /*
255 * If we have other si nodes then route replace
256 * else delete the route
257 */
258 static_uninstall_route(si->vrf_id, safi, rn, si);
259 route_unlock_node(rn);
260
261 /* Free static route configuration. */
262 XFREE(MTYPE_STATIC_ROUTE, si);
263
264 route_unlock_node(rn);
265
266 return 1;
267 }
268
269 static void static_ifindex_update_af(struct interface *ifp, bool up, afi_t afi,
270 safi_t safi)
271 {
272 struct route_table *stable;
273 struct route_node *rn;
274 struct static_route *si;
275 struct vrf *vrf;
276
277 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
278 struct static_vrf *svrf;
279
280 svrf = vrf->info;
281
282 stable = static_vrf_static_table(afi, safi, svrf);
283 if (!stable)
284 continue;
285
286 for (rn = route_top(stable); rn; rn = srcdest_route_next(rn)) {
287 for (si = rn->info; si; si = si->next) {
288 if (!si->ifname[0])
289 continue;
290 if (up) {
291 if (strcmp(si->ifname, ifp->name))
292 continue;
293 si->ifindex = ifp->ifindex;
294 } else {
295 if (si->ifindex != ifp->ifindex)
296 continue;
297 si->ifindex = IFINDEX_INTERNAL;
298 }
299
300 static_install_route(rn, si, safi);
301 }
302 }
303 }
304 }
305
306 /*
307 * This function looks at a svrf's stable and notices if any of the
308 * nexthops we are using are part of the vrf coming up.
309 * If we are using them then cleanup the nexthop vrf id
310 * to be the new value and then re-installs them
311 *
312 *
313 * stable -> The table we are looking at.
314 * svrf -> The newly changed vrf.
315 * afi -> The afi to look at
316 * safi -> the safi to look at
317 */
318 static void static_fixup_vrf(struct static_vrf *svrf,
319 struct route_table *stable, afi_t afi, safi_t safi)
320 {
321 struct route_node *rn;
322 struct static_route *si;
323 struct interface *ifp;
324
325 for (rn = route_top(stable); rn; rn = route_next(rn)) {
326 for (si = rn->info; si; si = si->next) {
327 if (strcmp(svrf->vrf->name, si->nh_vrfname) != 0)
328 continue;
329
330 si->nh_vrf_id = svrf->vrf->vrf_id;
331 if (si->ifindex) {
332 ifp = if_lookup_by_name(si->ifname,
333 si->nh_vrf_id);
334 if (ifp)
335 si->ifindex = ifp->ifindex;
336 else
337 continue;
338 }
339
340 static_install_route(rn, si, safi);
341 }
342 }
343 }
344
345 /*
346 * This function enables static routes in a svrf as it
347 * is coming up. It sets the new vrf_id as appropriate.
348 *
349 * svrf -> The svrf that is being brought up and enabled by the kernel
350 * stable -> The stable we are looking at.
351 * afi -> the afi in question
352 * safi -> the safi in question
353 */
354 static void static_enable_vrf(struct static_vrf *svrf,
355 struct route_table *stable,
356 afi_t afi, safi_t safi)
357 {
358 struct route_node *rn;
359 struct static_route *si;
360 struct interface *ifp;
361 struct vrf *vrf = svrf->vrf;
362
363 for (rn = route_top(stable); rn; rn = route_next(rn)) {
364 for (si = rn->info; si; si = si->next) {
365 si->vrf_id = vrf->vrf_id;
366 if (si->ifindex) {
367 ifp = if_lookup_by_name(si->ifname,
368 si->nh_vrf_id);
369 if (ifp)
370 si->ifindex = ifp->ifindex;
371 else
372 continue;
373 }
374 static_install_route(rn, si, safi);
375 }
376 }
377 }
378
379 /*
380 * When a vrf is being enabled by the kernel, go through all the
381 * static routes in the system that use this vrf (both nexthops vrfs
382 * and the routes vrf )
383 *
384 * enable_svrf -> the vrf being enabled
385 */
386 void static_fixup_vrf_ids(struct static_vrf *enable_svrf)
387 {
388 struct route_table *stable;
389 struct vrf *vrf;
390 afi_t afi;
391 safi_t safi;
392
393 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
394 struct static_vrf *svrf;
395
396 svrf = vrf->info;
397 /* Install any static routes configured for this VRF. */
398 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
399 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
400 stable = svrf->stable[afi][safi];
401 if (!stable)
402 continue;
403
404 static_fixup_vrf(enable_svrf, stable,
405 afi, safi);
406
407 if (enable_svrf == svrf)
408 static_enable_vrf(svrf, stable,
409 afi, safi);
410 }
411 }
412 }
413 }
414
415 /*
416 * Look at the specified stable and if any of the routes in
417 * this table are using the svrf as the nexthop, uninstall
418 * those routes.
419 *
420 * svrf -> the vrf being disabled
421 * stable -> the table we need to look at.
422 * afi -> the afi in question
423 * safi -> the safi in question
424 */
425 static void static_cleanup_vrf(struct static_vrf *svrf,
426 struct route_table *stable,
427 afi_t afi, safi_t safi)
428 {
429 struct route_node *rn;
430 struct static_route *si;
431
432 for (rn = route_top(stable); rn; rn = route_next(rn)) {
433 for (si = rn->info; si; si = si->next) {
434 if (strcmp(svrf->vrf->name, si->nh_vrfname) != 0)
435 continue;
436
437 static_uninstall_route(si->vrf_id, safi, rn, si);
438 }
439 }
440 }
441
442 /*
443 * Look at all static routes in this table and uninstall
444 * them.
445 *
446 * stable -> The table to uninstall from
447 * afi -> The afi in question
448 * safi -> the safi in question
449 */
450 static void static_disable_vrf(struct route_table *stable,
451 afi_t afi, safi_t safi)
452 {
453 struct route_node *rn;
454 struct static_route *si;
455
456 for (rn = route_top(stable); rn; rn = route_next(rn))
457 for (si = rn->info; si; si = si->next)
458 static_uninstall_route(si->vrf_id, safi, rn, si);
459 }
460
461 /*
462 * When the disable_svrf is shutdown by the kernel, we call
463 * this function and it cleans up all static routes using
464 * this vrf as a nexthop as well as all static routes
465 * in it's stables.
466 *
467 * disable_svrf - The vrf being disabled
468 */
469 void static_cleanup_vrf_ids(struct static_vrf *disable_svrf)
470 {
471 struct vrf *vrf;
472 afi_t afi;
473 safi_t safi;
474
475 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
476 struct static_vrf *svrf;
477
478 svrf = vrf->info;
479
480 /* Uninstall any static routes configured for this VRF. */
481 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
482 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
483 struct route_table *stable;
484
485 stable = svrf->stable[afi][safi];
486 if (!stable)
487 continue;
488
489 static_cleanup_vrf(disable_svrf, stable,
490 afi, safi);
491
492 if (disable_svrf == svrf)
493 static_disable_vrf(stable, afi, safi);
494 }
495 }
496 }
497 }
498
499 /* called from if_{add,delete}_update, i.e. when ifindex becomes [in]valid */
500 void static_ifindex_update(struct interface *ifp, bool up)
501 {
502 static_ifindex_update_af(ifp, up, AFI_IP, SAFI_UNICAST);
503 static_ifindex_update_af(ifp, up, AFI_IP, SAFI_MULTICAST);
504 static_ifindex_update_af(ifp, up, AFI_IP6, SAFI_UNICAST);
505 static_ifindex_update_af(ifp, up, AFI_IP6, SAFI_MULTICAST);
506 }