]> git.proxmox.com Git - mirror_frr.git/blob - staticd/static_vrf.c
Merge pull request #5919 from qlyoung/fix-vrrp-mvl-uaf
[mirror_frr.git] / staticd / static_vrf.c
1 /*
2 * STATICd - vrf 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 "vrf.h"
23 #include "nexthop.h"
24 #include "table.h"
25 #include "srcdest_table.h"
26
27 #include "static_memory.h"
28 #include "static_vrf.h"
29 #include "static_routes.h"
30 #include "static_zebra.h"
31 #include "static_vty.h"
32
33 static void zebra_stable_node_cleanup(struct route_table *table,
34 struct route_node *node)
35 {
36 struct static_route *si, *next;
37
38 if (node->info)
39 for (si = node->info; si; si = next) {
40 next = si->next;
41 XFREE(MTYPE_STATIC_ROUTE, si);
42 }
43 }
44
45 static struct static_vrf *static_vrf_alloc(void)
46 {
47 struct route_table *table;
48 struct static_vrf *svrf;
49 safi_t safi;
50 afi_t afi;
51
52 svrf = XCALLOC(MTYPE_TMP, sizeof(struct static_vrf));
53
54 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
55 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
56 if (afi == AFI_IP6)
57 table = srcdest_table_init();
58 else
59 table = route_table_init();
60 table->cleanup = zebra_stable_node_cleanup;
61 svrf->stable[afi][safi] = table;
62 }
63 }
64 return svrf;
65 }
66
67 static int static_vrf_new(struct vrf *vrf)
68 {
69 struct static_vrf *svrf;
70
71 svrf = static_vrf_alloc();
72 vrf->info = svrf;
73 svrf->vrf = vrf;
74
75 return 0;
76 }
77
78 static int static_vrf_enable(struct vrf *vrf)
79 {
80 static_zebra_vrf_register(vrf);
81
82 static_fixup_vrf_ids(vrf->info);
83
84 /*
85 * We may have static routes that are now possible to
86 * insert into the appropriate tables
87 */
88 static_config_install_delayed_routes(vrf->info);
89
90 return 0;
91 }
92
93 static int static_vrf_disable(struct vrf *vrf)
94 {
95 static_zebra_vrf_unregister(vrf);
96 return 0;
97 }
98
99 static int static_vrf_delete(struct vrf *vrf)
100 {
101 struct route_table *table;
102 struct static_vrf *svrf;
103 safi_t safi;
104 afi_t afi;
105
106 svrf = vrf->info;
107 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
108 for (safi = SAFI_UNICAST; safi <= SAFI_MULTICAST; safi++) {
109 table = svrf->stable[afi][safi];
110 route_table_finish(table);
111 svrf->stable[afi][safi] = NULL;
112 }
113 }
114 XFREE(MTYPE_TMP, svrf);
115 return 0;
116 }
117
118 /* Lookup the static routing table in a VRF. */
119 struct route_table *static_vrf_static_table(afi_t afi, safi_t safi,
120 struct static_vrf *svrf)
121 {
122 if (!svrf)
123 return NULL;
124
125 if (afi >= AFI_MAX || safi >= SAFI_MAX)
126 return NULL;
127
128 return svrf->stable[afi][safi];
129 }
130
131 struct static_vrf *static_vrf_lookup_by_id(vrf_id_t vrf_id)
132 {
133 struct vrf *vrf;
134
135 vrf = vrf_lookup_by_id(vrf_id);
136 if (vrf)
137 return ((struct static_vrf *)vrf->info);
138
139 return NULL;
140 }
141
142 struct static_vrf *static_vrf_lookup_by_name(const char *name)
143 {
144 struct vrf *vrf;
145
146 if (!name)
147 name = VRF_DEFAULT_NAME;
148
149 vrf = vrf_lookup_by_name(name);
150 if (vrf)
151 return ((struct static_vrf *)vrf->info);
152
153 return NULL;
154 }
155
156 static int static_vrf_config_write(struct vty *vty)
157 {
158 struct vrf *vrf;
159
160 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
161 if (vrf->vrf_id != VRF_DEFAULT)
162 vty_frame(vty, "vrf %s\n", vrf->name);
163
164 static_config(vty, vrf->info, AFI_IP,
165 SAFI_UNICAST, "ip route");
166 static_config(vty, vrf->info, AFI_IP,
167 SAFI_MULTICAST, "ip mroute");
168 static_config(vty, vrf->info, AFI_IP6,
169 SAFI_UNICAST, "ipv6 route");
170
171 if (vrf->vrf_id != VRF_DEFAULT)
172 vty_endframe(vty, " exit-vrf\n!\n");
173 }
174
175 return 0;
176 }
177
178 int static_vrf_has_config(struct static_vrf *svrf)
179 {
180 struct route_table *table;
181 safi_t safi;
182 afi_t afi;
183
184 /*
185 * NOTE: This is a don't care for the default VRF, but we go through
186 * the motions to keep things consistent.
187 */
188 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
189 for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) {
190 table = svrf->stable[afi][safi];
191 if (!table)
192 continue;
193 if (route_table_count(table))
194 return 1;
195 }
196 }
197
198 return 0;
199 }
200
201 void static_vrf_init(void)
202 {
203 vrf_init(static_vrf_new, static_vrf_enable,
204 static_vrf_disable, static_vrf_delete, NULL);
205
206 vrf_cmd_init(static_vrf_config_write, &static_privs);
207 }
208
209 void static_vrf_terminate(void)
210 {
211 vrf_terminate();
212 }