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