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