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