]> git.proxmox.com Git - mirror_frr.git/blame - staticd/static_routes.c
snapcraft: Add FabricD to Snap package
[mirror_frr.git] / staticd / static_routes.c
CommitLineData
7e24fdf3 1/*
8d5cbee9 2 * STATICd - route code
7e24fdf3
DS
3 * Copyright (C) 2018 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
8d5cbee9
DS
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.
7e24fdf3 10 *
8d5cbee9
DS
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.
7e24fdf3
DS
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. */
8bc8de2c
DS
36static void static_install_route(struct route_node *rn,
37 struct static_route *si_changed, safi_t safi)
7e24fdf3
DS
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)
8bc8de2c 46 static_zebra_route_add(rn, si_changed, si->vrf_id, safi, true);
7e24fdf3
DS
47
48}
49
50/* Uninstall static route from RIB. */
51static void static_uninstall_route(vrf_id_t vrf_id, safi_t safi,
8bc8de2c
DS
52 struct route_node *rn,
53 struct static_route *si_changed)
7e24fdf3
DS
54{
55
56 if (rn->info)
8bc8de2c 57 static_zebra_route_add(rn, si_changed, vrf_id, safi, true);
7e24fdf3 58 else
8bc8de2c 59 static_zebra_route_add(rn, si_changed, vrf_id, safi, false);
7e24fdf3
DS
60}
61
62int 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,
02dc8ba3
QY
67 struct static_nh_label *snh_label, uint32_t table_id,
68 bool onlink)
7e24fdf3
DS
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))
02dc8ba3 107 && si->bh_type == bh_type && si->onlink == onlink) {
7e24fdf3
DS
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;
02dc8ba3 132 si->onlink = onlink;
7e24fdf3
DS
133
134 if (ifname)
135 strlcpy(si->ifname, ifname, sizeof(si->ifname));
136 si->ifindex = IFINDEX_INTERNAL;
137
138 switch (type) {
139 case STATIC_IPV4_GATEWAY:
140 case STATIC_IPV4_GATEWAY_IFNAME:
141 si->addr.ipv4 = gate->ipv4;
142 break;
143 case STATIC_IPV6_GATEWAY:
144 case STATIC_IPV6_GATEWAY_IFNAME:
145 si->addr.ipv6 = gate->ipv6;
146 break;
147 case STATIC_IFNAME:
148 break;
149 }
150
151 /* Save labels, if any. */
152 memcpy(&si->snh_label, snh_label, sizeof(struct static_nh_label));
153
154 /*
155 * Add new static route information to the tree with sort by
156 * distance value and gateway address.
157 */
158 for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) {
159 if (si->distance < cp->distance)
160 break;
161 if (si->distance > cp->distance)
162 continue;
163 if (si->type == STATIC_IPV4_GATEWAY
164 && cp->type == STATIC_IPV4_GATEWAY) {
165 if (ntohl(si->addr.ipv4.s_addr)
166 < ntohl(cp->addr.ipv4.s_addr))
167 break;
168 if (ntohl(si->addr.ipv4.s_addr)
169 > ntohl(cp->addr.ipv4.s_addr))
170 continue;
171 }
172 }
173
174 /* Make linked list. */
175 if (pp)
176 pp->next = si;
177 else
178 rn->info = si;
179 if (cp)
180 cp->prev = si;
181 si->prev = pp;
182 si->next = cp;
183
184 /* check whether interface exists in system & install if it does */
185 if (!ifname)
8bc8de2c 186 static_install_route(rn, si, safi);
7e24fdf3
DS
187 else {
188 struct interface *ifp;
189
190 ifp = if_lookup_by_name(ifname, nh_svrf->vrf->vrf_id);
191 if (ifp && ifp->ifindex != IFINDEX_INTERNAL) {
192 si->ifindex = ifp->ifindex;
8bc8de2c 193 static_install_route(rn, si, safi);
7e24fdf3
DS
194 } else
195 zlog_warn("Static Route using %s interface not installed because the interface does not exist in specified vrf",
196 ifname);
197 }
198
199 return 1;
200}
201
202int static_delete_route(afi_t afi, safi_t safi, uint8_t type, struct prefix *p,
203 struct prefix_ipv6 *src_p, union g_addr *gate,
204 const char *ifname, route_tag_t tag, uint8_t distance,
205 struct static_vrf *svrf,
206 struct static_nh_label *snh_label,
207 uint32_t table_id)
208{
209 struct route_node *rn;
210 struct static_route *si;
211 struct route_table *stable;
212
213 /* Lookup table. */
214 stable = static_vrf_static_table(afi, safi, svrf);
215 if (!stable)
216 return -1;
217
218 /* Lookup static route prefix. */
219 rn = srcdest_rnode_lookup(stable, p, src_p);
220 if (!rn)
221 return 0;
222
223 /* Find same static route is the tree */
224 for (si = rn->info; si; si = si->next)
225 if (type == si->type
226 && (!gate
227 || ((afi == AFI_IP
228 && IPV4_ADDR_SAME(&gate->ipv4, &si->addr.ipv4))
229 || (afi == AFI_IP6
230 && IPV6_ADDR_SAME(gate, &si->addr.ipv6))))
231 && (!strcmp(ifname ? ifname : "", si->ifname))
232 && (!tag || (tag == si->tag))
233 && (table_id == si->table_id)
234 && (!snh_label->num_labels
235 || !memcmp(&si->snh_label, snh_label,
236 sizeof(struct static_nh_label))))
237 break;
238
239 /* Can't find static route. */
240 if (!si) {
241 route_unlock_node(rn);
242 return 0;
243 }
244
245 static_zebra_nht_register(si, false);
246
247 /* Unlink static route from linked list. */
248 if (si->prev)
249 si->prev->next = si->next;
250 else
251 rn->info = si->next;
252 if (si->next)
253 si->next->prev = si->prev;
254
255 /*
256 * If we have other si nodes then route replace
257 * else delete the route
258 */
8bc8de2c 259 static_uninstall_route(si->vrf_id, safi, rn, si);
7e24fdf3
DS
260 route_unlock_node(rn);
261
262 /* Free static route configuration. */
263 XFREE(MTYPE_STATIC_ROUTE, si);
264
265 route_unlock_node(rn);
266
267 return 1;
268}
269
270static void static_ifindex_update_af(struct interface *ifp, bool up, afi_t afi,
271 safi_t safi)
272{
273 struct route_table *stable;
274 struct route_node *rn;
275 struct static_route *si;
276 struct vrf *vrf;
277
278 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
279 struct static_vrf *svrf;
280
281 svrf = vrf->info;
282
283 stable = static_vrf_static_table(afi, safi, svrf);
284 if (!stable)
285 continue;
286
287 for (rn = route_top(stable); rn; rn = srcdest_route_next(rn)) {
288 for (si = rn->info; si; si = si->next) {
289 if (!si->ifname[0])
290 continue;
291 if (up) {
292 if (strcmp(si->ifname, ifp->name))
293 continue;
294 si->ifindex = ifp->ifindex;
295 } else {
296 if (si->ifindex != ifp->ifindex)
297 continue;
298 si->ifindex = IFINDEX_INTERNAL;
299 }
7e24fdf3 300
0b70cb10
DS
301 static_install_route(rn, si, safi);
302 }
7e24fdf3
DS
303 }
304 }
305}
306
307/*
308 * This function looks at a svrf's stable and notices if any of the
309 * nexthops we are using are part of the vrf coming up.
310 * If we are using them then cleanup the nexthop vrf id
311 * to be the new value and then re-installs them
312 *
313 *
314 * stable -> The table we are looking at.
315 * svrf -> The newly changed vrf.
316 * afi -> The afi to look at
317 * safi -> the safi to look at
318 */
319static void static_fixup_vrf(struct static_vrf *svrf,
320 struct route_table *stable, afi_t afi, safi_t safi)
321{
322 struct route_node *rn;
323 struct static_route *si;
324 struct interface *ifp;
7e24fdf3
DS
325
326 for (rn = route_top(stable); rn; rn = route_next(rn)) {
7e24fdf3
DS
327 for (si = rn->info; si; si = si->next) {
328 if (strcmp(svrf->vrf->name, si->nh_vrfname) != 0)
329 continue;
330
331 si->nh_vrf_id = svrf->vrf->vrf_id;
6d2a4f37 332 si->nh_registered = false;
7e24fdf3
DS
333 if (si->ifindex) {
334 ifp = if_lookup_by_name(si->ifname,
335 si->nh_vrf_id);
336 if (ifp)
337 si->ifindex = ifp->ifindex;
338 else
339 continue;
340 }
7e24fdf3 341
8bc8de2c 342 static_install_route(rn, si, safi);
0b70cb10 343 }
7e24fdf3
DS
344 }
345}
346
347/*
348 * This function enables static routes in a svrf as it
349 * is coming up. It sets the new vrf_id as appropriate.
350 *
351 * svrf -> The svrf that is being brought up and enabled by the kernel
352 * stable -> The stable we are looking at.
353 * afi -> the afi in question
354 * safi -> the safi in question
355 */
356static void static_enable_vrf(struct static_vrf *svrf,
357 struct route_table *stable,
358 afi_t afi, safi_t safi)
359{
360 struct route_node *rn;
361 struct static_route *si;
362 struct interface *ifp;
363 struct vrf *vrf = svrf->vrf;
7e24fdf3
DS
364
365 for (rn = route_top(stable); rn; rn = route_next(rn)) {
7e24fdf3
DS
366 for (si = rn->info; si; si = si->next) {
367 si->vrf_id = vrf->vrf_id;
368 if (si->ifindex) {
369 ifp = if_lookup_by_name(si->ifname,
370 si->nh_vrf_id);
371 if (ifp)
372 si->ifindex = ifp->ifindex;
373 else
374 continue;
375 }
8bc8de2c 376 static_install_route(rn, si, safi);
0b70cb10 377 }
7e24fdf3
DS
378 }
379}
380
381/*
382 * When a vrf is being enabled by the kernel, go through all the
383 * static routes in the system that use this vrf (both nexthops vrfs
384 * and the routes vrf )
385 *
386 * enable_svrf -> the vrf being enabled
387 */
388void static_fixup_vrf_ids(struct static_vrf *enable_svrf)
389{
390 struct route_table *stable;
391 struct vrf *vrf;
392 afi_t afi;
393 safi_t safi;
394
395 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
396 struct static_vrf *svrf;
397
398 svrf = vrf->info;
399 /* Install any static routes configured for this VRF. */
400 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
401 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
402 stable = svrf->stable[afi][safi];
403 if (!stable)
404 continue;
405
406 static_fixup_vrf(enable_svrf, stable,
407 afi, safi);
408
409 if (enable_svrf == svrf)
410 static_enable_vrf(svrf, stable,
411 afi, safi);
412 }
413 }
414 }
415}
416
417/*
418 * Look at the specified stable and if any of the routes in
419 * this table are using the svrf as the nexthop, uninstall
420 * those routes.
421 *
422 * svrf -> the vrf being disabled
423 * stable -> the table we need to look at.
424 * afi -> the afi in question
425 * safi -> the safi in question
426 */
427static void static_cleanup_vrf(struct static_vrf *svrf,
428 struct route_table *stable,
429 afi_t afi, safi_t safi)
430{
431 struct route_node *rn;
432 struct static_route *si;
433
434 for (rn = route_top(stable); rn; rn = route_next(rn)) {
435 for (si = rn->info; si; si = si->next) {
436 if (strcmp(svrf->vrf->name, si->nh_vrfname) != 0)
437 continue;
438
8bc8de2c 439 static_uninstall_route(si->vrf_id, safi, rn, si);
7e24fdf3
DS
440 }
441 }
442}
443
444/*
445 * Look at all static routes in this table and uninstall
446 * them.
447 *
448 * stable -> The table to uninstall from
449 * afi -> The afi in question
450 * safi -> the safi in question
451 */
452static void static_disable_vrf(struct route_table *stable,
453 afi_t afi, safi_t safi)
454{
455 struct route_node *rn;
456 struct static_route *si;
457
458 for (rn = route_top(stable); rn; rn = route_next(rn))
459 for (si = rn->info; si; si = si->next)
8bc8de2c 460 static_uninstall_route(si->vrf_id, safi, rn, si);
7e24fdf3
DS
461}
462
463/*
464 * When the disable_svrf is shutdown by the kernel, we call
465 * this function and it cleans up all static routes using
466 * this vrf as a nexthop as well as all static routes
467 * in it's stables.
468 *
469 * disable_svrf - The vrf being disabled
470 */
471void static_cleanup_vrf_ids(struct static_vrf *disable_svrf)
472{
473 struct vrf *vrf;
474 afi_t afi;
475 safi_t safi;
476
477 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
478 struct static_vrf *svrf;
479
480 svrf = vrf->info;
481
482 /* Uninstall any static routes configured for this VRF. */
483 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
484 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
485 struct route_table *stable;
486
487 stable = svrf->stable[afi][safi];
488 if (!stable)
489 continue;
490
491 static_cleanup_vrf(disable_svrf, stable,
492 afi, safi);
493
494 if (disable_svrf == svrf)
495 static_disable_vrf(stable, afi, safi);
496 }
497 }
498 }
499}
500
a9be49bc
DS
501/*
502 * This function enables static routes when an interface it relies
503 * on in a different vrf is coming up.
504 *
a9be49bc
DS
505 * stable -> The stable we are looking at.
506 * ifp -> interface coming up
507 * afi -> the afi in question
508 * safi -> the safi in question
509 */
510static void static_fixup_intf_nh(struct route_table *stable,
511 struct interface *ifp,
512 afi_t afi, safi_t safi)
513{
514 struct route_node *rn;
515 struct static_route *si;
516
517 for (rn = route_top(stable); rn; rn = route_next(rn)) {
518 for (si = rn->info; si; si = si->next) {
519 if (si->nh_vrf_id != ifp->vrf_id)
520 continue;
521
522 if (si->ifindex != ifp->ifindex)
523 continue;
524
525 static_install_route(rn, si, safi);
526 }
527 }
528}
529
530/*
531 * This function enables static routes that rely on an interface in
532 * a different vrf when that interface comes up.
533 */
534void static_install_intf_nh(struct interface *ifp)
535{
536 struct route_table *stable;
537 struct vrf *vrf;
538 afi_t afi;
539 safi_t safi;
540
541 RB_FOREACH(vrf, vrf_name_head, &vrfs_by_name) {
542 struct static_vrf *svrf = vrf->info;
543
544 /* Not needed if same vrf since happens naturally */
545 if (vrf->vrf_id == ifp->vrf_id)
546 continue;
547
548 /* Install any static routes configured for this interface. */
549 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
550 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
551 stable = svrf->stable[afi][safi];
552 if (!stable)
553 continue;
554
555 static_fixup_intf_nh(stable, ifp, afi, safi);
556 }
557 }
558 }
559}
560
7e24fdf3
DS
561/* called from if_{add,delete}_update, i.e. when ifindex becomes [in]valid */
562void static_ifindex_update(struct interface *ifp, bool up)
563{
564 static_ifindex_update_af(ifp, up, AFI_IP, SAFI_UNICAST);
565 static_ifindex_update_af(ifp, up, AFI_IP, SAFI_MULTICAST);
566 static_ifindex_update_af(ifp, up, AFI_IP6, SAFI_UNICAST);
567 static_ifindex_update_af(ifp, up, AFI_IP6, SAFI_MULTICAST);
568}