]>
Commit | Line | Data |
---|---|---|
b72ede27 FL |
1 | /* |
2 | * VRF functions. | |
3 | * Copyright (C) 2014 6WIND S.A. | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
7 | * GNU Zebra is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation; either version 2, or (at your | |
10 | * option) any later version. | |
11 | * | |
12 | * GNU Zebra is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
896014f4 DL |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
b72ede27 FL |
20 | */ |
21 | ||
22 | #include <zebra.h> | |
23 | ||
4691b65a PG |
24 | /* for basename */ |
25 | #include <libgen.h> | |
26 | ||
6a69b354 | 27 | #include "if.h" |
b72ede27 | 28 | #include "vrf.h" |
7922fc65 | 29 | #include "vrf_int.h" |
b72ede27 FL |
30 | #include "prefix.h" |
31 | #include "table.h" | |
32 | #include "log.h" | |
33 | #include "memory.h" | |
19dc275e | 34 | #include "command.h" |
b95c1883 | 35 | #include "ns.h" |
3bc34908 | 36 | #include "privs.h" |
98cbbaea | 37 | #include "nexthop_group.h" |
b66d022e | 38 | #include "lib_errors.h" |
19dc275e | 39 | |
ec31f30d PG |
40 | /* default VRF ID value used when VRF backend is not NETNS */ |
41 | #define VRF_DEFAULT_INTERNAL 0 | |
dd114702 | 42 | #define VRF_DEFAULT_NAME_INTERNAL "default" |
ec31f30d | 43 | |
d62a17ae | 44 | DEFINE_MTYPE_STATIC(LIB, VRF, "VRF") |
4a1ab8e4 DL |
45 | DEFINE_MTYPE_STATIC(LIB, VRF_BITMAP, "VRF bit-map") |
46 | ||
e80e7cce DL |
47 | DEFINE_QOBJ_TYPE(vrf) |
48 | ||
d62a17ae | 49 | static __inline int vrf_id_compare(const struct vrf *, const struct vrf *); |
50 | static __inline int vrf_name_compare(const struct vrf *, const struct vrf *); | |
1a1a7065 | 51 | |
d62a17ae | 52 | RB_GENERATE(vrf_id_head, vrf, id_entry, vrf_id_compare); |
53 | RB_GENERATE(vrf_name_head, vrf, name_entry, vrf_name_compare); | |
1a1a7065 | 54 | |
d62a17ae | 55 | struct vrf_id_head vrfs_by_id = RB_INITIALIZER(&vrfs_by_id); |
56 | struct vrf_name_head vrfs_by_name = RB_INITIALIZER(&vrfs_by_name); | |
1a1a7065 | 57 | |
78dd30b2 | 58 | static int vrf_backend; |
72261ecd | 59 | static int vrf_backend_configured; |
3bc34908 | 60 | static struct zebra_privs_t *vrf_daemon_privs; |
c200f5e1 | 61 | static char vrf_default_name[VRF_NAMSIZ] = VRF_DEFAULT_NAME_INTERNAL; |
78dd30b2 | 62 | |
19dc275e DS |
63 | /* |
64 | * Turn on/off debug code | |
65 | * for vrf. | |
66 | */ | |
c17faa4b | 67 | static int debug_vrf = 0; |
b72ede27 | 68 | |
b72ede27 | 69 | /* Holding VRF hooks */ |
d62a17ae | 70 | struct vrf_master { |
71 | int (*vrf_new_hook)(struct vrf *); | |
72 | int (*vrf_delete_hook)(struct vrf *); | |
73 | int (*vrf_enable_hook)(struct vrf *); | |
74 | int (*vrf_disable_hook)(struct vrf *); | |
ecbc5a37 | 75 | int (*vrf_update_name_hook)(struct vrf *vrf); |
d62a17ae | 76 | } vrf_master = { |
77 | 0, | |
78 | }; | |
b72ede27 | 79 | |
d62a17ae | 80 | static int vrf_is_enabled(struct vrf *vrf); |
e5bf3e1e | 81 | |
216b18ef | 82 | /* VRF list existance check by name. */ |
d62a17ae | 83 | struct vrf *vrf_lookup_by_name(const char *name) |
216b18ef | 84 | { |
d62a17ae | 85 | struct vrf vrf; |
86 | strlcpy(vrf.name, name, sizeof(vrf.name)); | |
87 | return (RB_FIND(vrf_name_head, &vrfs_by_name, &vrf)); | |
216b18ef | 88 | } |
216b18ef | 89 | |
d62a17ae | 90 | static __inline int vrf_id_compare(const struct vrf *a, const struct vrf *b) |
b72ede27 | 91 | { |
d62a17ae | 92 | return (a->vrf_id - b->vrf_id); |
216b18ef DS |
93 | } |
94 | ||
d62a17ae | 95 | static int vrf_name_compare(const struct vrf *a, const struct vrf *b) |
b72ede27 | 96 | { |
d62a17ae | 97 | return strcmp(a->name, b->name); |
b72ede27 FL |
98 | } |
99 | ||
e26aedbe PG |
100 | /* if ns_id is different and not VRF_UNKNOWN, |
101 | * then update vrf identifier, and enable VRF | |
102 | */ | |
103 | static void vrf_update_vrf_id(ns_id_t ns_id, void *opaqueptr) | |
104 | { | |
105 | ns_id_t vrf_id = (vrf_id_t)ns_id; | |
106 | vrf_id_t old_vrf_id; | |
107 | struct vrf *vrf = (struct vrf *)opaqueptr; | |
108 | ||
109 | if (!vrf) | |
110 | return; | |
111 | old_vrf_id = vrf->vrf_id; | |
112 | if (vrf_id == vrf->vrf_id) | |
113 | return; | |
114 | if (vrf->vrf_id != VRF_UNKNOWN) | |
115 | RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf); | |
116 | vrf->vrf_id = vrf_id; | |
117 | RB_INSERT(vrf_id_head, &vrfs_by_id, vrf); | |
118 | if (old_vrf_id == VRF_UNKNOWN) | |
119 | vrf_enable((struct vrf *)vrf); | |
120 | } | |
121 | ||
ce1be369 PG |
122 | int vrf_switch_to_netns(vrf_id_t vrf_id) |
123 | { | |
124 | char *name; | |
125 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); | |
126 | ||
ce1be369 | 127 | /* VRF is default VRF. silently ignore */ |
e26aedbe | 128 | if (!vrf || vrf->vrf_id == VRF_DEFAULT) |
9dff1132 | 129 | return 1; /* 1 = default */ |
e26aedbe PG |
130 | /* VRF has no NETNS backend. silently ignore */ |
131 | if (vrf->data.l.netns_name[0] == '\0') | |
9dff1132 | 132 | return 2; /* 2 = no netns */ |
ce1be369 PG |
133 | name = ns_netns_pathname(NULL, vrf->data.l.netns_name); |
134 | if (debug_vrf) | |
135 | zlog_debug("VRF_SWITCH: %s(%u)", name, vrf->vrf_id); | |
136 | return ns_switch_to_netns(name); | |
137 | } | |
138 | ||
139 | int vrf_switchback_to_initial(void) | |
140 | { | |
141 | int ret = ns_switchback_to_initial(); | |
142 | ||
143 | if (ret == 0 && debug_vrf) | |
144 | zlog_debug("VRF_SWITCHBACK"); | |
145 | return ret; | |
146 | } | |
147 | ||
216b18ef | 148 | /* Get a VRF. If not found, create one. |
34f8e6af DS |
149 | * Arg: |
150 | * name - The name of the vrf. May be NULL if unknown. | |
151 | * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown | |
216b18ef | 152 | * Description: Please note that this routine can be called with just the name |
34f8e6af DS |
153 | * and 0 vrf-id |
154 | */ | |
d62a17ae | 155 | struct vrf *vrf_get(vrf_id_t vrf_id, const char *name) |
156 | { | |
157 | struct vrf *vrf = NULL; | |
158 | int new = 0; | |
159 | ||
160 | if (debug_vrf) | |
996c9314 LB |
161 | zlog_debug("VRF_GET: %s(%u)", name == NULL ? "(NULL)" : name, |
162 | vrf_id); | |
d62a17ae | 163 | |
164 | /* Nothing to see, move along here */ | |
165 | if (!name && vrf_id == VRF_UNKNOWN) | |
166 | return NULL; | |
167 | ||
0c2bac38 PG |
168 | /* attempt to find already available VRF |
169 | */ | |
170 | if (name) | |
171 | vrf = vrf_lookup_by_name(name); | |
dd114702 PG |
172 | if (vrf && vrf_id != VRF_UNKNOWN |
173 | && vrf->vrf_id != VRF_UNKNOWN | |
174 | && vrf->vrf_id != vrf_id) { | |
175 | zlog_debug("VRF_GET: avoid %s creation(%u), same name exists (%u)", | |
176 | name, vrf_id, vrf->vrf_id); | |
177 | return NULL; | |
178 | } | |
d62a17ae | 179 | /* Try to find VRF both by ID and name */ |
0c2bac38 | 180 | if (!vrf && vrf_id != VRF_UNKNOWN) |
d62a17ae | 181 | vrf = vrf_lookup_by_id(vrf_id); |
d62a17ae | 182 | |
183 | if (vrf == NULL) { | |
184 | vrf = XCALLOC(MTYPE_VRF, sizeof(struct vrf)); | |
185 | vrf->vrf_id = VRF_UNKNOWN; | |
d62a17ae | 186 | QOBJ_REG(vrf, vrf); |
187 | new = 1; | |
188 | ||
189 | if (debug_vrf) | |
190 | zlog_debug("VRF(%u) %s is created.", vrf_id, | |
191 | (name) ? name : "(NULL)"); | |
192 | } | |
193 | ||
194 | /* Set identifier */ | |
195 | if (vrf_id != VRF_UNKNOWN && vrf->vrf_id == VRF_UNKNOWN) { | |
196 | vrf->vrf_id = vrf_id; | |
197 | RB_INSERT(vrf_id_head, &vrfs_by_id, vrf); | |
198 | } | |
199 | ||
200 | /* Set name */ | |
201 | if (name && vrf->name[0] != '\0' && strcmp(name, vrf->name)) { | |
202 | RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf); | |
203 | strlcpy(vrf->name, name, sizeof(vrf->name)); | |
204 | RB_INSERT(vrf_name_head, &vrfs_by_name, vrf); | |
205 | } else if (name && vrf->name[0] == '\0') { | |
206 | strlcpy(vrf->name, name, sizeof(vrf->name)); | |
207 | RB_INSERT(vrf_name_head, &vrfs_by_name, vrf); | |
208 | } | |
d62a17ae | 209 | if (new &&vrf_master.vrf_new_hook) |
210 | (*vrf_master.vrf_new_hook)(vrf); | |
211 | ||
212 | return vrf; | |
b72ede27 FL |
213 | } |
214 | ||
84915b0a | 215 | /* Delete a VRF. This is called when the underlying VRF goes away, a |
216 | * pre-configured VRF is deleted or when shutting down (vrf_terminate()). | |
217 | */ | |
d62a17ae | 218 | void vrf_delete(struct vrf *vrf) |
b72ede27 | 219 | { |
d62a17ae | 220 | if (debug_vrf) |
221 | zlog_debug("VRF %u is to be deleted.", vrf->vrf_id); | |
b72ede27 | 222 | |
d62a17ae | 223 | if (vrf_is_enabled(vrf)) |
224 | vrf_disable(vrf); | |
e5bf3e1e | 225 | |
84915b0a | 226 | /* If the VRF is user configured, it'll stick around, just remove |
227 | * the ID mapping. Interfaces assigned to this VRF should've been | |
228 | * removed already as part of the VRF going down. | |
229 | */ | |
230 | if (vrf_is_user_cfged(vrf)) { | |
231 | if (vrf->vrf_id != VRF_UNKNOWN) { | |
232 | /* Delete any VRF interfaces - should be only | |
233 | * the VRF itself, other interfaces should've | |
234 | * been moved out of the VRF. | |
235 | */ | |
236 | if_terminate(vrf); | |
237 | RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf); | |
238 | vrf->vrf_id = VRF_UNKNOWN; | |
239 | } | |
240 | return; | |
241 | } | |
242 | ||
d62a17ae | 243 | if (vrf_master.vrf_delete_hook) |
244 | (*vrf_master.vrf_delete_hook)(vrf); | |
216b18ef | 245 | |
d62a17ae | 246 | QOBJ_UNREG(vrf); |
f4e14fdb | 247 | if_terminate(vrf); |
b72ede27 | 248 | |
d62a17ae | 249 | if (vrf->vrf_id != VRF_UNKNOWN) |
250 | RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf); | |
251 | if (vrf->name[0] != '\0') | |
252 | RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf); | |
b72ede27 | 253 | |
d62a17ae | 254 | XFREE(MTYPE_VRF, vrf); |
b72ede27 FL |
255 | } |
256 | ||
257 | /* Look up a VRF by identifier. */ | |
d62a17ae | 258 | struct vrf *vrf_lookup_by_id(vrf_id_t vrf_id) |
b72ede27 | 259 | { |
d62a17ae | 260 | struct vrf vrf; |
261 | vrf.vrf_id = vrf_id; | |
262 | return (RB_FIND(vrf_id_head, &vrfs_by_id, &vrf)); | |
b72ede27 FL |
263 | } |
264 | ||
e5bf3e1e FL |
265 | /* |
266 | * Enable a VRF - that is, let the VRF be ready to use. | |
267 | * The VRF_ENABLE_HOOK callback will be called to inform | |
268 | * that they can allocate resources in this VRF. | |
269 | * | |
270 | * RETURN: 1 - enabled successfully; otherwise, 0. | |
271 | */ | |
d62a17ae | 272 | int vrf_enable(struct vrf *vrf) |
e5bf3e1e | 273 | { |
d62a17ae | 274 | if (vrf_is_enabled(vrf)) |
275 | return 1; | |
05e8e11e | 276 | |
d62a17ae | 277 | if (debug_vrf) |
278 | zlog_debug("VRF %u is enabled.", vrf->vrf_id); | |
e5bf3e1e | 279 | |
d62a17ae | 280 | SET_FLAG(vrf->status, VRF_ACTIVE); |
e5bf3e1e | 281 | |
d62a17ae | 282 | if (vrf_master.vrf_enable_hook) |
283 | (*vrf_master.vrf_enable_hook)(vrf); | |
e5bf3e1e | 284 | |
98cbbaea DS |
285 | /* |
286 | * If we have any nexthop group entries that | |
287 | * are awaiting vrf initialization then | |
288 | * let's let people know about it | |
289 | */ | |
290 | nexthop_group_enable_vrf(vrf); | |
291 | ||
d62a17ae | 292 | return 1; |
e5bf3e1e FL |
293 | } |
294 | ||
295 | /* | |
296 | * Disable a VRF - that is, let the VRF be unusable. | |
297 | * The VRF_DELETE_HOOK callback will be called to inform | |
298 | * that they must release the resources in the VRF. | |
299 | */ | |
697d3ec7 | 300 | void vrf_disable(struct vrf *vrf) |
e5bf3e1e | 301 | { |
d62a17ae | 302 | if (!vrf_is_enabled(vrf)) |
303 | return; | |
a647bfa8 | 304 | |
d62a17ae | 305 | UNSET_FLAG(vrf->status, VRF_ACTIVE); |
e5bf3e1e | 306 | |
d62a17ae | 307 | if (debug_vrf) |
308 | zlog_debug("VRF %u is to be disabled.", vrf->vrf_id); | |
e5bf3e1e | 309 | |
d62a17ae | 310 | /* Till now, nothing to be done for the default VRF. */ |
311 | // Pending: see why this statement. | |
e74f14fc | 312 | |
d62a17ae | 313 | if (vrf_master.vrf_disable_hook) |
314 | (*vrf_master.vrf_disable_hook)(vrf); | |
e5bf3e1e FL |
315 | } |
316 | ||
b7cfce93 MK |
317 | const char *vrf_id_to_name(vrf_id_t vrf_id) |
318 | { | |
319 | struct vrf *vrf; | |
320 | ||
321 | vrf = vrf_lookup_by_id(vrf_id); | |
322 | if (vrf) | |
323 | return vrf->name; | |
324 | ||
181c08c6 | 325 | return "n/a"; |
b7cfce93 MK |
326 | } |
327 | ||
d62a17ae | 328 | vrf_id_t vrf_name_to_id(const char *name) |
216b18ef | 329 | { |
d62a17ae | 330 | struct vrf *vrf; |
331 | vrf_id_t vrf_id = VRF_DEFAULT; // Pending: need a way to return invalid | |
332 | // id/ routine not used. | |
216b18ef | 333 | |
2569910b PG |
334 | if (!name) |
335 | return vrf_id; | |
d62a17ae | 336 | vrf = vrf_lookup_by_name(name); |
337 | if (vrf) | |
338 | vrf_id = vrf->vrf_id; | |
216b18ef | 339 | |
d62a17ae | 340 | return vrf_id; |
216b18ef DS |
341 | } |
342 | ||
b72ede27 | 343 | /* Get the data pointer of the specified VRF. If not found, create one. */ |
d62a17ae | 344 | void *vrf_info_get(vrf_id_t vrf_id) |
b72ede27 | 345 | { |
d62a17ae | 346 | struct vrf *vrf = vrf_get(vrf_id, NULL); |
347 | return vrf->info; | |
b72ede27 FL |
348 | } |
349 | ||
350 | /* Look up the data pointer of the specified VRF. */ | |
d62a17ae | 351 | void *vrf_info_lookup(vrf_id_t vrf_id) |
b72ede27 | 352 | { |
d62a17ae | 353 | struct vrf *vrf = vrf_lookup_by_id(vrf_id); |
354 | return vrf ? vrf->info : NULL; | |
b72ede27 FL |
355 | } |
356 | ||
7076bb2f | 357 | /* |
4a8bf858 | 358 | * VRF hash for storing set or not. |
7076bb2f | 359 | */ |
4a8bf858 DS |
360 | struct vrf_bit_set { |
361 | vrf_id_t vrf_id; | |
362 | bool set; | |
363 | }; | |
7076bb2f | 364 | |
d8b87afe | 365 | static unsigned int vrf_hash_bitmap_key(const void *data) |
4a8bf858 | 366 | { |
d8b87afe | 367 | const struct vrf_bit_set *bit = data; |
d62a17ae | 368 | |
4a8bf858 DS |
369 | return bit->vrf_id; |
370 | } | |
d62a17ae | 371 | |
74df8d6d | 372 | static bool vrf_hash_bitmap_cmp(const void *a, const void *b) |
4a8bf858 DS |
373 | { |
374 | const struct vrf_bit_set *bit1 = a; | |
375 | const struct vrf_bit_set *bit2 = b; | |
d62a17ae | 376 | |
4a8bf858 DS |
377 | return bit1->vrf_id == bit2->vrf_id; |
378 | } | |
379 | ||
380 | static void *vrf_hash_bitmap_alloc(void *data) | |
381 | { | |
382 | struct vrf_bit_set *copy = data; | |
383 | struct vrf_bit_set *bit; | |
384 | ||
385 | bit = XMALLOC(MTYPE_VRF_BITMAP, sizeof(*bit)); | |
386 | bit->vrf_id = copy->vrf_id; | |
387 | ||
388 | return bit; | |
389 | } | |
390 | ||
391 | static void vrf_hash_bitmap_free(void *data) | |
392 | { | |
393 | struct vrf_bit_set *bit = data; | |
394 | ||
395 | XFREE(MTYPE_VRF_BITMAP, bit); | |
396 | } | |
7076bb2f | 397 | |
d62a17ae | 398 | vrf_bitmap_t vrf_bitmap_init(void) |
7076bb2f | 399 | { |
4a8bf858 DS |
400 | return hash_create_size(32, vrf_hash_bitmap_key, vrf_hash_bitmap_cmp, |
401 | "VRF BIT HASH"); | |
7076bb2f FL |
402 | } |
403 | ||
d62a17ae | 404 | void vrf_bitmap_free(vrf_bitmap_t bmap) |
7076bb2f | 405 | { |
4a8bf858 | 406 | struct hash *vrf_hash = bmap; |
7076bb2f | 407 | |
4a8bf858 | 408 | if (vrf_hash == NULL) |
d62a17ae | 409 | return; |
7076bb2f | 410 | |
4a8bf858 DS |
411 | hash_clean(vrf_hash, vrf_hash_bitmap_free); |
412 | hash_free(vrf_hash); | |
7076bb2f FL |
413 | } |
414 | ||
d62a17ae | 415 | void vrf_bitmap_set(vrf_bitmap_t bmap, vrf_id_t vrf_id) |
7076bb2f | 416 | { |
4a8bf858 DS |
417 | struct vrf_bit_set lookup = { .vrf_id = vrf_id }; |
418 | struct hash *vrf_hash = bmap; | |
419 | struct vrf_bit_set *bit; | |
7076bb2f | 420 | |
4a8bf858 | 421 | if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN) |
d62a17ae | 422 | return; |
7076bb2f | 423 | |
4a8bf858 DS |
424 | bit = hash_get(vrf_hash, &lookup, vrf_hash_bitmap_alloc); |
425 | bit->set = true; | |
7076bb2f FL |
426 | } |
427 | ||
d62a17ae | 428 | void vrf_bitmap_unset(vrf_bitmap_t bmap, vrf_id_t vrf_id) |
7076bb2f | 429 | { |
4a8bf858 DS |
430 | struct vrf_bit_set lookup = { .vrf_id = vrf_id }; |
431 | struct hash *vrf_hash = bmap; | |
432 | struct vrf_bit_set *bit; | |
7076bb2f | 433 | |
4a8bf858 | 434 | if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN) |
d62a17ae | 435 | return; |
7076bb2f | 436 | |
4a8bf858 DS |
437 | bit = hash_get(vrf_hash, &lookup, vrf_hash_bitmap_alloc); |
438 | bit->set = false; | |
7076bb2f FL |
439 | } |
440 | ||
d62a17ae | 441 | int vrf_bitmap_check(vrf_bitmap_t bmap, vrf_id_t vrf_id) |
7076bb2f | 442 | { |
4a8bf858 DS |
443 | struct vrf_bit_set lookup = { .vrf_id = vrf_id }; |
444 | struct hash *vrf_hash = bmap; | |
445 | struct vrf_bit_set *bit; | |
7076bb2f | 446 | |
4a8bf858 | 447 | if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN) |
d62a17ae | 448 | return 0; |
7076bb2f | 449 | |
4a8bf858 DS |
450 | bit = hash_lookup(vrf_hash, &lookup); |
451 | if (bit) | |
452 | return bit->set; | |
453 | ||
454 | return 0; | |
7076bb2f FL |
455 | } |
456 | ||
d62a17ae | 457 | static void vrf_autocomplete(vector comps, struct cmd_token *token) |
d617d5fe | 458 | { |
d62a17ae | 459 | struct vrf *vrf = NULL; |
d617d5fe | 460 | |
723001fc PG |
461 | RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) |
462 | vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name)); | |
d617d5fe DS |
463 | } |
464 | ||
465 | static const struct cmd_variable_handler vrf_var_handlers[] = { | |
d62a17ae | 466 | { |
467 | .varname = "vrf", | |
468 | .completions = vrf_autocomplete, | |
469 | }, | |
470 | {.completions = NULL}, | |
d617d5fe DS |
471 | }; |
472 | ||
b72ede27 | 473 | /* Initialize VRF module. */ |
d62a17ae | 474 | void vrf_init(int (*create)(struct vrf *), int (*enable)(struct vrf *), |
d01b92fd | 475 | int (*disable)(struct vrf *), int (*destroy)(struct vrf *), |
ecbc5a37 | 476 | int ((*update)(struct vrf *))) |
d62a17ae | 477 | { |
478 | struct vrf *default_vrf; | |
479 | ||
e26aedbe PG |
480 | /* initialise NS, in case VRF backend if NETNS */ |
481 | ns_init(); | |
d62a17ae | 482 | if (debug_vrf) |
483 | zlog_debug("%s: Initializing VRF subsystem", | |
484 | __PRETTY_FUNCTION__); | |
485 | ||
486 | vrf_master.vrf_new_hook = create; | |
487 | vrf_master.vrf_enable_hook = enable; | |
488 | vrf_master.vrf_disable_hook = disable; | |
d01b92fd | 489 | vrf_master.vrf_delete_hook = destroy; |
ecbc5a37 | 490 | vrf_master.vrf_update_name_hook = update; |
d62a17ae | 491 | |
492 | /* The default VRF always exists. */ | |
eb6934d5 | 493 | default_vrf = vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME); |
d62a17ae | 494 | if (!default_vrf) { |
450971aa | 495 | flog_err(EC_LIB_VRF_START, |
1c50c1c0 | 496 | "vrf_init: failed to create the default VRF!"); |
d62a17ae | 497 | exit(1); |
498 | } | |
20c87e98 TC |
499 | if (vrf_is_backend_netns()) { |
500 | struct ns *ns; | |
501 | ||
fdafe17e | 502 | strlcpy(default_vrf->data.l.netns_name, |
eb6934d5 | 503 | VRF_DEFAULT_NAME, NS_NAMSIZ); |
20c87e98 | 504 | ns = ns_lookup(ns_get_default_id()); |
c3568c4d TC |
505 | ns->vrf_ctxt = default_vrf; |
506 | default_vrf->ns_ctxt = ns; | |
20c87e98 | 507 | } |
d62a17ae | 508 | |
509 | /* Enable the default VRF. */ | |
510 | if (!vrf_enable(default_vrf)) { | |
450971aa | 511 | flog_err(EC_LIB_VRF_START, |
1c50c1c0 | 512 | "vrf_init: failed to enable the default VRF!"); |
d62a17ae | 513 | exit(1); |
514 | } | |
515 | ||
516 | cmd_variable_handler_register(vrf_var_handlers); | |
b72ede27 FL |
517 | } |
518 | ||
519 | /* Terminate VRF module. */ | |
d62a17ae | 520 | void vrf_terminate(void) |
b72ede27 | 521 | { |
d62a17ae | 522 | struct vrf *vrf; |
b72ede27 | 523 | |
d62a17ae | 524 | if (debug_vrf) |
525 | zlog_debug("%s: Shutting down vrf subsystem", | |
526 | __PRETTY_FUNCTION__); | |
19dc275e | 527 | |
55cd0f61 DS |
528 | while (!RB_EMPTY(vrf_id_head, &vrfs_by_id)) { |
529 | vrf = RB_ROOT(vrf_id_head, &vrfs_by_id); | |
530 | ||
65c3a7c4 | 531 | /* Clear configured flag and invoke delete. */ |
532 | UNSET_FLAG(vrf->status, VRF_CONFIGURED); | |
d62a17ae | 533 | vrf_delete(vrf); |
65c3a7c4 | 534 | } |
55cd0f61 DS |
535 | |
536 | while (!RB_EMPTY(vrf_name_head, &vrfs_by_name)) { | |
537 | vrf = RB_ROOT(vrf_name_head, &vrfs_by_name); | |
538 | ||
65c3a7c4 | 539 | /* Clear configured flag and invoke delete. */ |
540 | UNSET_FLAG(vrf->status, VRF_CONFIGURED); | |
d62a17ae | 541 | vrf_delete(vrf); |
65c3a7c4 | 542 | } |
b72ede27 FL |
543 | } |
544 | ||
e5bf3e1e | 545 | /* Create a socket for the VRF. */ |
0f4977c6 | 546 | int vrf_socket(int domain, int type, int protocol, vrf_id_t vrf_id, |
02fe07c7 | 547 | const char *interfacename) |
e5bf3e1e | 548 | { |
2e0d2b3d | 549 | int ret, save_errno, ret2; |
e5bf3e1e | 550 | |
2e0d2b3d PG |
551 | ret = vrf_switch_to_netns(vrf_id); |
552 | if (ret < 0) | |
450971aa | 553 | flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)", |
09c866e3 | 554 | __func__, vrf_id, safe_strerror(errno)); |
b66d022e | 555 | |
d62a17ae | 556 | ret = socket(domain, type, protocol); |
2e0d2b3d PG |
557 | save_errno = errno; |
558 | ret2 = vrf_switchback_to_initial(); | |
559 | if (ret2 < 0) | |
450971aa | 560 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
561 | "%s: Can't switchback from VRF %u (%s)", __func__, |
562 | vrf_id, safe_strerror(errno)); | |
2e0d2b3d | 563 | errno = save_errno; |
0f4977c6 PG |
564 | if (ret <= 0) |
565 | return ret; | |
566 | ret2 = vrf_bind(vrf_id, ret, interfacename); | |
567 | if (ret2 < 0) { | |
568 | close(ret); | |
569 | ret = ret2; | |
570 | } | |
d62a17ae | 571 | return ret; |
e5bf3e1e FL |
572 | } |
573 | ||
78dd30b2 PG |
574 | int vrf_is_backend_netns(void) |
575 | { | |
576 | return (vrf_backend == VRF_BACKEND_NETNS); | |
577 | } | |
578 | ||
579 | int vrf_get_backend(void) | |
580 | { | |
72261ecd PG |
581 | if (!vrf_backend_configured) |
582 | return VRF_BACKEND_UNKNOWN; | |
78dd30b2 PG |
583 | return vrf_backend; |
584 | } | |
585 | ||
586 | void vrf_configure_backend(int vrf_backend_netns) | |
587 | { | |
588 | vrf_backend = vrf_backend_netns; | |
72261ecd | 589 | vrf_backend_configured = 1; |
78dd30b2 PG |
590 | } |
591 | ||
03aff2d8 PG |
592 | int vrf_handler_create(struct vty *vty, const char *vrfname, |
593 | struct vrf **vrf) | |
f30c50b9 | 594 | { |
d62a17ae | 595 | struct vrf *vrfp; |
f30c50b9 | 596 | |
d62a17ae | 597 | if (strlen(vrfname) > VRF_NAMSIZ) { |
697d3ec7 PG |
598 | if (vty) |
599 | vty_out(vty, | |
996c9314 LB |
600 | "%% VRF name %s invalid: length exceeds %d bytes\n", |
601 | vrfname, VRF_NAMSIZ); | |
697d3ec7 | 602 | else |
0351a28f | 603 | flog_warn( |
450971aa | 604 | EC_LIB_VRF_LENGTH, |
996c9314 LB |
605 | "%% VRF name %s invalid: length exceeds %d bytes\n", |
606 | vrfname, VRF_NAMSIZ); | |
d62a17ae | 607 | return CMD_WARNING_CONFIG_FAILED; |
608 | } | |
f30c50b9 | 609 | |
d62a17ae | 610 | vrfp = vrf_get(VRF_UNKNOWN, vrfname); |
f30c50b9 | 611 | |
697d3ec7 PG |
612 | if (vty) |
613 | VTY_PUSH_CONTEXT(VRF_NODE, vrfp); | |
f30c50b9 | 614 | |
697d3ec7 PG |
615 | if (vrf) |
616 | *vrf = vrfp; | |
d62a17ae | 617 | return CMD_SUCCESS; |
f30c50b9 RW |
618 | } |
619 | ||
996c9314 | 620 | int vrf_netns_handler_create(struct vty *vty, struct vrf *vrf, char *pathname, |
03aff2d8 | 621 | ns_id_t ns_id, ns_id_t internal_ns_id) |
e26aedbe PG |
622 | { |
623 | struct ns *ns = NULL; | |
624 | ||
625 | if (!vrf) | |
626 | return CMD_WARNING_CONFIG_FAILED; | |
627 | if (vrf->vrf_id != VRF_UNKNOWN && vrf->ns_ctxt == NULL) { | |
628 | if (vty) | |
629 | vty_out(vty, | |
630 | "VRF %u is already configured with VRF %s\n", | |
631 | vrf->vrf_id, vrf->name); | |
632 | else | |
9165c5f5 | 633 | zlog_info("VRF %u is already configured with VRF %s", |
e26aedbe PG |
634 | vrf->vrf_id, vrf->name); |
635 | return CMD_WARNING_CONFIG_FAILED; | |
636 | } | |
637 | if (vrf->ns_ctxt != NULL) { | |
996c9314 | 638 | ns = (struct ns *)vrf->ns_ctxt; |
2e1cc436 | 639 | if (!strcmp(ns->name, pathname)) { |
e26aedbe PG |
640 | if (vty) |
641 | vty_out(vty, | |
996c9314 LB |
642 | "VRF %u already configured with NETNS %s\n", |
643 | vrf->vrf_id, ns->name); | |
e26aedbe | 644 | else |
0351a28f | 645 | zlog_info( |
ade6974d QY |
646 | "VRF %u already configured with NETNS %s", |
647 | vrf->vrf_id, ns->name); | |
e26aedbe PG |
648 | return CMD_WARNING_CONFIG_FAILED; |
649 | } | |
650 | } | |
651 | ns = ns_lookup_name(pathname); | |
652 | if (ns && ns->vrf_ctxt) { | |
653 | struct vrf *vrf2 = (struct vrf *)ns->vrf_ctxt; | |
654 | ||
655 | if (vrf2 == vrf) | |
656 | return CMD_SUCCESS; | |
657 | if (vty) | |
996c9314 LB |
658 | vty_out(vty, |
659 | "NS %s is already configured" | |
e26aedbe | 660 | " with VRF %u(%s)\n", |
996c9314 | 661 | ns->name, vrf2->vrf_id, vrf2->name); |
e26aedbe | 662 | else |
0351a28f | 663 | zlog_info("NS %s is already configured with VRF %u(%s)", |
e26aedbe PG |
664 | ns->name, vrf2->vrf_id, vrf2->name); |
665 | return CMD_WARNING_CONFIG_FAILED; | |
666 | } | |
667 | ns = ns_get_created(ns, pathname, ns_id); | |
03aff2d8 | 668 | ns->internal_ns_id = internal_ns_id; |
e26aedbe PG |
669 | ns->vrf_ctxt = (void *)vrf; |
670 | vrf->ns_ctxt = (void *)ns; | |
671 | /* update VRF netns NAME */ | |
2e1cc436 | 672 | strlcpy(vrf->data.l.netns_name, basename(pathname), NS_NAMSIZ); |
e26aedbe PG |
673 | |
674 | if (!ns_enable(ns, vrf_update_vrf_id)) { | |
675 | if (vty) | |
676 | vty_out(vty, "Can not associate NS %u with NETNS %s\n", | |
996c9314 | 677 | ns->ns_id, ns->name); |
e26aedbe | 678 | else |
0351a28f | 679 | zlog_info("Can not associate NS %u with NETNS %s", |
e26aedbe PG |
680 | ns->ns_id, ns->name); |
681 | return CMD_WARNING_CONFIG_FAILED; | |
682 | } | |
683 | ||
684 | return CMD_SUCCESS; | |
685 | } | |
686 | ||
697d3ec7 | 687 | /* vrf CLI commands */ |
16d6ea59 QY |
688 | DEFUN_NOSH(vrf_exit, |
689 | vrf_exit_cmd, | |
690 | "exit-vrf", | |
691 | "Exit current mode and down to previous mode\n") | |
692 | { | |
693 | /* We have to set vrf context to default vrf */ | |
694 | VTY_PUSH_CONTEXT(VRF_NODE, vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME)); | |
695 | vty->node = CONFIG_NODE; | |
696 | return CMD_SUCCESS; | |
697 | } | |
698 | ||
697d3ec7 PG |
699 | DEFUN_NOSH (vrf, |
700 | vrf_cmd, | |
701 | "vrf NAME", | |
702 | "Select a VRF to configure\n" | |
703 | "VRF's name\n") | |
704 | { | |
705 | int idx_name = 1; | |
706 | const char *vrfname = argv[idx_name]->arg; | |
707 | ||
708 | return vrf_handler_create(vty, vrfname, NULL); | |
709 | } | |
710 | ||
34c46274 RW |
711 | DEFUN (no_vrf, |
712 | no_vrf_cmd, | |
713 | "no vrf NAME", | |
714 | NO_STR | |
715 | "Delete a pseudo VRF's configuration\n" | |
716 | "VRF's name\n") | |
f30c50b9 | 717 | { |
d62a17ae | 718 | const char *vrfname = argv[2]->arg; |
53dc2b05 | 719 | |
d62a17ae | 720 | struct vrf *vrfp; |
f30c50b9 | 721 | |
d62a17ae | 722 | vrfp = vrf_lookup_by_name(vrfname); |
f30c50b9 | 723 | |
d62a17ae | 724 | if (vrfp == NULL) { |
725 | vty_out(vty, "%% VRF %s does not exist\n", vrfname); | |
726 | return CMD_WARNING_CONFIG_FAILED; | |
727 | } | |
f30c50b9 | 728 | |
d62a17ae | 729 | if (CHECK_FLAG(vrfp->status, VRF_ACTIVE)) { |
730 | vty_out(vty, "%% Only inactive VRFs can be deleted\n"); | |
731 | return CMD_WARNING_CONFIG_FAILED; | |
732 | } | |
f30c50b9 | 733 | |
84915b0a | 734 | /* Clear configured flag and invoke delete. */ |
735 | UNSET_FLAG(vrfp->status, VRF_CONFIGURED); | |
d62a17ae | 736 | vrf_delete(vrfp); |
f30c50b9 | 737 | |
d62a17ae | 738 | return CMD_SUCCESS; |
f30c50b9 RW |
739 | } |
740 | ||
53dc2b05 | 741 | |
d62a17ae | 742 | struct cmd_node vrf_node = {VRF_NODE, "%s(config-vrf)# ", 1}; |
7ddcfca4 | 743 | |
34c46274 | 744 | DEFUN_NOSH (vrf_netns, |
4a541e8c PG |
745 | vrf_netns_cmd, |
746 | "netns NAME", | |
747 | "Attach VRF to a Namespace\n" | |
748 | "The file name in " NS_RUN_DIR ", or a full pathname\n") | |
e26aedbe | 749 | { |
3bc34908 | 750 | int idx_name = 1, ret; |
e26aedbe PG |
751 | char *pathname = ns_netns_pathname(vty, argv[idx_name]->arg); |
752 | ||
753 | VTY_DECLVAR_CONTEXT(vrf, vrf); | |
754 | ||
755 | if (!pathname) | |
756 | return CMD_WARNING_CONFIG_FAILED; | |
3bc34908 | 757 | |
6bb30c2c DL |
758 | frr_elevate_privs(vrf_daemon_privs) { |
759 | ret = vrf_netns_handler_create(vty, vrf, pathname, | |
760 | NS_UNKNOWN, NS_UNKNOWN); | |
761 | } | |
3bc34908 | 762 | return ret; |
e26aedbe PG |
763 | } |
764 | ||
34c46274 | 765 | DEFUN_NOSH (no_vrf_netns, |
e26aedbe PG |
766 | no_vrf_netns_cmd, |
767 | "no netns [NAME]", | |
768 | NO_STR | |
769 | "Detach VRF from a Namespace\n" | |
770 | "The file name in " NS_RUN_DIR ", or a full pathname\n") | |
771 | { | |
772 | struct ns *ns = NULL; | |
773 | ||
774 | VTY_DECLVAR_CONTEXT(vrf, vrf); | |
775 | ||
776 | if (!vrf_is_backend_netns()) { | |
777 | vty_out(vty, "VRF backend is not Netns. Aborting\n"); | |
778 | return CMD_WARNING_CONFIG_FAILED; | |
779 | } | |
780 | if (!vrf->ns_ctxt) { | |
781 | vty_out(vty, "VRF %s(%u) is not configured with NetNS\n", | |
782 | vrf->name, vrf->vrf_id); | |
783 | return CMD_WARNING_CONFIG_FAILED; | |
784 | } | |
785 | ||
786 | ns = (struct ns *)vrf->ns_ctxt; | |
787 | ||
788 | ns->vrf_ctxt = NULL; | |
789 | vrf_disable(vrf); | |
790 | /* vrf ID from VRF is necessary for Zebra | |
791 | * so that propagate to other clients is done | |
792 | */ | |
793 | ns_delete(ns); | |
794 | vrf->ns_ctxt = NULL; | |
795 | return CMD_SUCCESS; | |
796 | } | |
797 | ||
19dc275e DS |
798 | /* |
799 | * Debug CLI for vrf's | |
800 | */ | |
801 | DEFUN (vrf_debug, | |
802 | vrf_debug_cmd, | |
803 | "debug vrf", | |
804 | DEBUG_STR | |
805 | "VRF Debugging\n") | |
806 | { | |
d62a17ae | 807 | debug_vrf = 1; |
19dc275e | 808 | |
d62a17ae | 809 | return CMD_SUCCESS; |
19dc275e DS |
810 | } |
811 | ||
812 | DEFUN (no_vrf_debug, | |
813 | no_vrf_debug_cmd, | |
814 | "no debug vrf", | |
815 | NO_STR | |
816 | DEBUG_STR | |
817 | "VRF Debugging\n") | |
818 | { | |
d62a17ae | 819 | debug_vrf = 0; |
19dc275e | 820 | |
d62a17ae | 821 | return CMD_SUCCESS; |
19dc275e DS |
822 | } |
823 | ||
d62a17ae | 824 | static int vrf_write_host(struct vty *vty) |
19dc275e | 825 | { |
d62a17ae | 826 | if (debug_vrf) |
827 | vty_out(vty, "debug vrf\n"); | |
19dc275e | 828 | |
d62a17ae | 829 | return 1; |
19dc275e DS |
830 | } |
831 | ||
d62a17ae | 832 | static struct cmd_node vrf_debug_node = {VRF_DEBUG_NODE, "", 1}; |
19dc275e | 833 | |
d62a17ae | 834 | void vrf_install_commands(void) |
19dc275e | 835 | { |
d62a17ae | 836 | install_node(&vrf_debug_node, vrf_write_host); |
19dc275e | 837 | |
d62a17ae | 838 | install_element(CONFIG_NODE, &vrf_debug_cmd); |
839 | install_element(ENABLE_NODE, &vrf_debug_cmd); | |
840 | install_element(CONFIG_NODE, &no_vrf_debug_cmd); | |
841 | install_element(ENABLE_NODE, &no_vrf_debug_cmd); | |
19dc275e | 842 | } |
53dc2b05 | 843 | |
3bc34908 PG |
844 | void vrf_cmd_init(int (*writefunc)(struct vty *vty), |
845 | struct zebra_privs_t *daemon_privs) | |
7ddcfca4 | 846 | { |
d62a17ae | 847 | install_element(CONFIG_NODE, &vrf_cmd); |
848 | install_element(CONFIG_NODE, &no_vrf_cmd); | |
849 | install_node(&vrf_node, writefunc); | |
850 | install_default(VRF_NODE); | |
16d6ea59 | 851 | install_element(VRF_NODE, &vrf_exit_cmd); |
e26aedbe PG |
852 | if (vrf_is_backend_netns() && ns_have_netns()) { |
853 | /* Install NS commands. */ | |
3bc34908 | 854 | vrf_daemon_privs = daemon_privs; |
e26aedbe PG |
855 | install_element(VRF_NODE, &vrf_netns_cmd); |
856 | install_element(VRF_NODE, &no_vrf_netns_cmd); | |
857 | } | |
19dc275e | 858 | } |
ec31f30d | 859 | |
4fe52e76 | 860 | void vrf_set_default_name(const char *default_name, bool force) |
ec31f30d | 861 | { |
c200f5e1 | 862 | struct vrf *def_vrf; |
4fe52e76 | 863 | static bool def_vrf_forced; |
ec31f30d | 864 | |
c200f5e1 PG |
865 | def_vrf = vrf_lookup_by_id(VRF_DEFAULT); |
866 | assert(default_name); | |
4fe52e76 PG |
867 | if (def_vrf && !force && def_vrf_forced) { |
868 | zlog_debug("VRF: %s, avoid changing name to %s, previously forced (%u)", | |
869 | def_vrf->name, default_name, | |
870 | def_vrf->vrf_id); | |
871 | return; | |
872 | } | |
48e5512c | 873 | |
c200f5e1 PG |
874 | snprintf(vrf_default_name, VRF_NAMSIZ, "%s", default_name); |
875 | if (def_vrf) { | |
4fe52e76 PG |
876 | if (force) |
877 | def_vrf_forced = true; | |
c200f5e1 PG |
878 | RB_REMOVE(vrf_name_head, &vrfs_by_name, def_vrf); |
879 | strlcpy(def_vrf->data.l.netns_name, | |
880 | vrf_default_name, NS_NAMSIZ); | |
881 | strlcpy(def_vrf->name, vrf_default_name, sizeof(def_vrf->name)); | |
882 | RB_INSERT(vrf_name_head, &vrfs_by_name, def_vrf); | |
ecbc5a37 PG |
883 | if (vrf_master.vrf_update_name_hook) |
884 | (*vrf_master.vrf_update_name_hook)(def_vrf); | |
c200f5e1 PG |
885 | } |
886 | } | |
887 | ||
888 | const char *vrf_get_default_name(void) | |
889 | { | |
890 | return vrf_default_name; | |
891 | } | |
892 | ||
893 | vrf_id_t vrf_get_default_id(void) | |
894 | { | |
03aff2d8 PG |
895 | /* backend netns is only known by zebra |
896 | * for other daemons, we return VRF_DEFAULT_INTERNAL | |
897 | */ | |
ec31f30d PG |
898 | if (vrf_is_backend_netns()) |
899 | return ns_get_default_id(); | |
900 | else | |
901 | return VRF_DEFAULT_INTERNAL; | |
902 | } | |
2e0d2b3d | 903 | |
02fe07c7 | 904 | int vrf_bind(vrf_id_t vrf_id, int fd, const char *name) |
0f4977c6 PG |
905 | { |
906 | int ret = 0; | |
91f854f6 | 907 | struct interface *ifp; |
0f4977c6 | 908 | |
a36898e7 | 909 | if (fd < 0 || name == NULL) |
0f4977c6 | 910 | return fd; |
91f854f6 PG |
911 | /* the device should exist |
912 | * otherwise we should return | |
913 | * case ifname = vrf in netns mode => return | |
914 | */ | |
a36898e7 | 915 | ifp = if_lookup_by_name(name, vrf_id); |
91f854f6 | 916 | if (!ifp) |
0f4977c6 PG |
917 | return fd; |
918 | #ifdef SO_BINDTODEVICE | |
c9c70dd1 | 919 | ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name)+1); |
0f4977c6 | 920 | if (ret < 0) |
996c9314 LB |
921 | zlog_debug("bind to interface %s failed, errno=%d", name, |
922 | errno); | |
0f4977c6 PG |
923 | #endif /* SO_BINDTODEVICE */ |
924 | return ret; | |
925 | } | |
2e0d2b3d | 926 | int vrf_getaddrinfo(const char *node, const char *service, |
996c9314 LB |
927 | const struct addrinfo *hints, struct addrinfo **res, |
928 | vrf_id_t vrf_id) | |
2e0d2b3d PG |
929 | { |
930 | int ret, ret2, save_errno; | |
931 | ||
932 | ret = vrf_switch_to_netns(vrf_id); | |
933 | if (ret < 0) | |
450971aa | 934 | flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)", |
09c866e3 | 935 | __func__, vrf_id, safe_strerror(errno)); |
2e0d2b3d PG |
936 | ret = getaddrinfo(node, service, hints, res); |
937 | save_errno = errno; | |
938 | ret2 = vrf_switchback_to_initial(); | |
939 | if (ret2 < 0) | |
450971aa | 940 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
941 | "%s: Can't switchback from VRF %u (%s)", __func__, |
942 | vrf_id, safe_strerror(errno)); | |
2e0d2b3d PG |
943 | errno = save_errno; |
944 | return ret; | |
945 | } | |
946 | ||
516d7591 PG |
947 | int vrf_ioctl(vrf_id_t vrf_id, int d, unsigned long request, char *params) |
948 | { | |
949 | int ret, saved_errno, rc; | |
950 | ||
951 | ret = vrf_switch_to_netns(vrf_id); | |
952 | if (ret < 0) { | |
450971aa | 953 | flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)", |
09c866e3 | 954 | __func__, vrf_id, safe_strerror(errno)); |
516d7591 PG |
955 | return 0; |
956 | } | |
957 | rc = ioctl(d, request, params); | |
958 | saved_errno = errno; | |
959 | ret = vrf_switchback_to_initial(); | |
960 | if (ret < 0) | |
450971aa | 961 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
962 | "%s: Can't switchback from VRF %u (%s)", __func__, |
963 | vrf_id, safe_strerror(errno)); | |
516d7591 PG |
964 | errno = saved_errno; |
965 | return rc; | |
966 | } | |
967 | ||
0f4977c6 | 968 | int vrf_sockunion_socket(const union sockunion *su, vrf_id_t vrf_id, |
02fe07c7 | 969 | const char *interfacename) |
2e0d2b3d PG |
970 | { |
971 | int ret, save_errno, ret2; | |
972 | ||
973 | ret = vrf_switch_to_netns(vrf_id); | |
974 | if (ret < 0) | |
450971aa | 975 | flog_err_sys(EC_LIB_SOCKET, "%s: Can't switch to VRF %u (%s)", |
09c866e3 | 976 | __func__, vrf_id, safe_strerror(errno)); |
2e0d2b3d PG |
977 | ret = sockunion_socket(su); |
978 | save_errno = errno; | |
979 | ret2 = vrf_switchback_to_initial(); | |
980 | if (ret2 < 0) | |
450971aa | 981 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
982 | "%s: Can't switchback from VRF %u (%s)", __func__, |
983 | vrf_id, safe_strerror(errno)); | |
2e0d2b3d | 984 | errno = save_errno; |
0f4977c6 PG |
985 | |
986 | if (ret <= 0) | |
987 | return ret; | |
988 | ret2 = vrf_bind(vrf_id, ret, interfacename); | |
989 | if (ret2 < 0) { | |
990 | close(ret); | |
991 | ret = ret2; | |
992 | } | |
2e0d2b3d PG |
993 | return ret; |
994 | } | |
0b014ea6 PG |
995 | |
996 | vrf_id_t vrf_generate_id(void) | |
997 | { | |
998 | static int vrf_id_local; | |
999 | ||
1000 | return ++vrf_id_local; | |
1001 | } |