]> git.proxmox.com Git - mirror_frr.git/blob - lib/vrf.c
Merge remote-tracking branch 'upstream/master' into evpn-extended-mobility
[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
43 DEFINE_MTYPE_STATIC(LIB, VRF, "VRF")
44 DEFINE_MTYPE_STATIC(LIB, VRF_BITMAP, "VRF bit-map")
45
46 DEFINE_QOBJ_TYPE(vrf)
47
48 static __inline int vrf_id_compare(const struct vrf *, const struct vrf *);
49 static __inline int vrf_name_compare(const struct vrf *, const struct vrf *);
50
51 RB_GENERATE(vrf_id_head, vrf, id_entry, vrf_id_compare);
52 RB_GENERATE(vrf_name_head, vrf, name_entry, vrf_name_compare);
53
54 struct vrf_id_head vrfs_by_id = RB_INITIALIZER(&vrfs_by_id);
55 struct vrf_name_head vrfs_by_name = RB_INITIALIZER(&vrfs_by_name);
56
57 static int vrf_backend;
58 static struct zebra_privs_t *vrf_daemon_privs;
59
60 /*
61 * Turn on/off debug code
62 * for vrf.
63 */
64 int debug_vrf = 0;
65
66 /* Holding VRF hooks */
67 struct vrf_master {
68 int (*vrf_new_hook)(struct vrf *);
69 int (*vrf_delete_hook)(struct vrf *);
70 int (*vrf_enable_hook)(struct vrf *);
71 int (*vrf_disable_hook)(struct vrf *);
72 } vrf_master = {
73 0,
74 };
75
76 static int vrf_is_enabled(struct vrf *vrf);
77
78 /* VRF list existance check by name. */
79 struct vrf *vrf_lookup_by_name(const char *name)
80 {
81 struct vrf vrf;
82 strlcpy(vrf.name, name, sizeof(vrf.name));
83 return (RB_FIND(vrf_name_head, &vrfs_by_name, &vrf));
84 }
85
86 static __inline int vrf_id_compare(const struct vrf *a, const struct vrf *b)
87 {
88 return (a->vrf_id - b->vrf_id);
89 }
90
91 static int vrf_name_compare(const struct vrf *a, const struct vrf *b)
92 {
93 return strcmp(a->name, b->name);
94 }
95
96 /* if ns_id is different and not VRF_UNKNOWN,
97 * then update vrf identifier, and enable VRF
98 */
99 static void vrf_update_vrf_id(ns_id_t ns_id, void *opaqueptr)
100 {
101 ns_id_t vrf_id = (vrf_id_t)ns_id;
102 vrf_id_t old_vrf_id;
103 struct vrf *vrf = (struct vrf *)opaqueptr;
104
105 if (!vrf)
106 return;
107 old_vrf_id = vrf->vrf_id;
108 if (vrf_id == vrf->vrf_id)
109 return;
110 if (vrf->vrf_id != VRF_UNKNOWN)
111 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
112 vrf->vrf_id = vrf_id;
113 RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
114 if (old_vrf_id == VRF_UNKNOWN)
115 vrf_enable((struct vrf *)vrf);
116 }
117
118 int vrf_switch_to_netns(vrf_id_t vrf_id)
119 {
120 char *name;
121 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
122
123 /* VRF is default VRF. silently ignore */
124 if (!vrf || vrf->vrf_id == VRF_DEFAULT)
125 return 1; /* 1 = default */
126 /* VRF has no NETNS backend. silently ignore */
127 if (vrf->data.l.netns_name[0] == '\0')
128 return 2; /* 2 = no netns */
129 name = ns_netns_pathname(NULL, vrf->data.l.netns_name);
130 if (debug_vrf)
131 zlog_debug("VRF_SWITCH: %s(%u)", name, vrf->vrf_id);
132 return ns_switch_to_netns(name);
133 }
134
135 int vrf_switchback_to_initial(void)
136 {
137 int ret = ns_switchback_to_initial();
138
139 if (ret == 0 && debug_vrf)
140 zlog_debug("VRF_SWITCHBACK");
141 return ret;
142 }
143
144 /* Get a VRF. If not found, create one.
145 * Arg:
146 * name - The name of the vrf. May be NULL if unknown.
147 * vrf_id - The vrf_id of the vrf. May be VRF_UNKNOWN if unknown
148 * Description: Please note that this routine can be called with just the name
149 * and 0 vrf-id
150 */
151 struct vrf *vrf_get(vrf_id_t vrf_id, const char *name)
152 {
153 struct vrf *vrf = NULL;
154 int new = 0;
155
156 if (debug_vrf)
157 zlog_debug("VRF_GET: %s(%u)", name == NULL ? "(NULL)" : name,
158 vrf_id);
159
160 /* Nothing to see, move along here */
161 if (!name && vrf_id == VRF_UNKNOWN)
162 return NULL;
163
164 /* attempt to find already available VRF
165 */
166 if (name)
167 vrf = vrf_lookup_by_name(name);
168 /* Try to find VRF both by ID and name */
169 if (!vrf && vrf_id != VRF_UNKNOWN)
170 vrf = vrf_lookup_by_id(vrf_id);
171
172 if (vrf == NULL) {
173 vrf = XCALLOC(MTYPE_VRF, sizeof(struct vrf));
174 vrf->vrf_id = VRF_UNKNOWN;
175 QOBJ_REG(vrf, vrf);
176 new = 1;
177
178 if (debug_vrf)
179 zlog_debug("VRF(%u) %s is created.", vrf_id,
180 (name) ? name : "(NULL)");
181 }
182
183 /* Set identifier */
184 if (vrf_id != VRF_UNKNOWN && vrf->vrf_id == VRF_UNKNOWN) {
185 vrf->vrf_id = vrf_id;
186 RB_INSERT(vrf_id_head, &vrfs_by_id, vrf);
187 }
188
189 /* Set name */
190 if (name && vrf->name[0] != '\0' && strcmp(name, vrf->name)) {
191 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
192 strlcpy(vrf->name, name, sizeof(vrf->name));
193 RB_INSERT(vrf_name_head, &vrfs_by_name, vrf);
194 } else if (name && vrf->name[0] == '\0') {
195 strlcpy(vrf->name, name, sizeof(vrf->name));
196 RB_INSERT(vrf_name_head, &vrfs_by_name, vrf);
197 }
198 if (new &&vrf_master.vrf_new_hook)
199 (*vrf_master.vrf_new_hook)(vrf);
200
201 return vrf;
202 }
203
204 /* Delete a VRF. This is called when the underlying VRF goes away, a
205 * pre-configured VRF is deleted or when shutting down (vrf_terminate()).
206 */
207 void vrf_delete(struct vrf *vrf)
208 {
209 if (debug_vrf)
210 zlog_debug("VRF %u is to be deleted.", vrf->vrf_id);
211
212 if (vrf_is_enabled(vrf))
213 vrf_disable(vrf);
214
215 /* If the VRF is user configured, it'll stick around, just remove
216 * the ID mapping. Interfaces assigned to this VRF should've been
217 * removed already as part of the VRF going down.
218 */
219 if (vrf_is_user_cfged(vrf)) {
220 if (vrf->vrf_id != VRF_UNKNOWN) {
221 /* Delete any VRF interfaces - should be only
222 * the VRF itself, other interfaces should've
223 * been moved out of the VRF.
224 */
225 if_terminate(vrf);
226 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
227 vrf->vrf_id = VRF_UNKNOWN;
228 }
229 return;
230 }
231
232 if (vrf_master.vrf_delete_hook)
233 (*vrf_master.vrf_delete_hook)(vrf);
234
235 QOBJ_UNREG(vrf);
236 if_terminate(vrf);
237
238 if (vrf->vrf_id != VRF_UNKNOWN)
239 RB_REMOVE(vrf_id_head, &vrfs_by_id, vrf);
240 if (vrf->name[0] != '\0')
241 RB_REMOVE(vrf_name_head, &vrfs_by_name, vrf);
242
243 XFREE(MTYPE_VRF, vrf);
244 }
245
246 /* Look up a VRF by identifier. */
247 struct vrf *vrf_lookup_by_id(vrf_id_t vrf_id)
248 {
249 struct vrf vrf;
250 vrf.vrf_id = vrf_id;
251 return (RB_FIND(vrf_id_head, &vrfs_by_id, &vrf));
252 }
253
254 /*
255 * Enable a VRF - that is, let the VRF be ready to use.
256 * The VRF_ENABLE_HOOK callback will be called to inform
257 * that they can allocate resources in this VRF.
258 *
259 * RETURN: 1 - enabled successfully; otherwise, 0.
260 */
261 int vrf_enable(struct vrf *vrf)
262 {
263 if (vrf_is_enabled(vrf))
264 return 1;
265
266 if (debug_vrf)
267 zlog_debug("VRF %u is enabled.", vrf->vrf_id);
268
269 SET_FLAG(vrf->status, VRF_ACTIVE);
270
271 if (vrf_master.vrf_enable_hook)
272 (*vrf_master.vrf_enable_hook)(vrf);
273
274 /*
275 * If we have any nexthop group entries that
276 * are awaiting vrf initialization then
277 * let's let people know about it
278 */
279 nexthop_group_enable_vrf(vrf);
280
281 return 1;
282 }
283
284 /*
285 * Disable a VRF - that is, let the VRF be unusable.
286 * The VRF_DELETE_HOOK callback will be called to inform
287 * that they must release the resources in the VRF.
288 */
289 void vrf_disable(struct vrf *vrf)
290 {
291 if (!vrf_is_enabled(vrf))
292 return;
293
294 UNSET_FLAG(vrf->status, VRF_ACTIVE);
295
296 if (debug_vrf)
297 zlog_debug("VRF %u is to be disabled.", vrf->vrf_id);
298
299 /* Till now, nothing to be done for the default VRF. */
300 // Pending: see why this statement.
301
302 if (vrf_master.vrf_disable_hook)
303 (*vrf_master.vrf_disable_hook)(vrf);
304 }
305
306 const char *vrf_id_to_name(vrf_id_t vrf_id)
307 {
308 struct vrf *vrf;
309
310 vrf = vrf_lookup_by_id(vrf_id);
311 if (vrf)
312 return vrf->name;
313
314 return "n/a";
315 }
316
317 vrf_id_t vrf_name_to_id(const char *name)
318 {
319 struct vrf *vrf;
320 vrf_id_t vrf_id = VRF_DEFAULT; // Pending: need a way to return invalid
321 // id/ routine not used.
322
323 vrf = vrf_lookup_by_name(name);
324 if (vrf)
325 vrf_id = vrf->vrf_id;
326
327 return vrf_id;
328 }
329
330 /* Get the data pointer of the specified VRF. If not found, create one. */
331 void *vrf_info_get(vrf_id_t vrf_id)
332 {
333 struct vrf *vrf = vrf_get(vrf_id, NULL);
334 return vrf->info;
335 }
336
337 /* Look up the data pointer of the specified VRF. */
338 void *vrf_info_lookup(vrf_id_t vrf_id)
339 {
340 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
341 return vrf ? vrf->info : NULL;
342 }
343
344 /*
345 * VRF hash for storing set or not.
346 */
347 struct vrf_bit_set {
348 vrf_id_t vrf_id;
349 bool set;
350 };
351
352 static unsigned int vrf_hash_bitmap_key(void *data)
353 {
354 struct vrf_bit_set *bit = data;
355
356 return bit->vrf_id;
357 }
358
359 static int vrf_hash_bitmap_cmp(const void *a, const void *b)
360 {
361 const struct vrf_bit_set *bit1 = a;
362 const struct vrf_bit_set *bit2 = b;
363
364 return bit1->vrf_id == bit2->vrf_id;
365 }
366
367 static void *vrf_hash_bitmap_alloc(void *data)
368 {
369 struct vrf_bit_set *copy = data;
370 struct vrf_bit_set *bit;
371
372 bit = XMALLOC(MTYPE_VRF_BITMAP, sizeof(*bit));
373 bit->vrf_id = copy->vrf_id;
374
375 return bit;
376 }
377
378 static void vrf_hash_bitmap_free(void *data)
379 {
380 struct vrf_bit_set *bit = data;
381
382 XFREE(MTYPE_VRF_BITMAP, bit);
383 }
384
385 vrf_bitmap_t vrf_bitmap_init(void)
386 {
387 return hash_create_size(32, vrf_hash_bitmap_key, vrf_hash_bitmap_cmp,
388 "VRF BIT HASH");
389 }
390
391 void vrf_bitmap_free(vrf_bitmap_t bmap)
392 {
393 struct hash *vrf_hash = bmap;
394
395 if (vrf_hash == NULL)
396 return;
397
398 hash_clean(vrf_hash, vrf_hash_bitmap_free);
399 hash_free(vrf_hash);
400 }
401
402 void vrf_bitmap_set(vrf_bitmap_t bmap, vrf_id_t vrf_id)
403 {
404 struct vrf_bit_set lookup = { .vrf_id = vrf_id };
405 struct hash *vrf_hash = bmap;
406 struct vrf_bit_set *bit;
407
408 if (vrf_hash == NULL || vrf_id == VRF_UNKNOWN)
409 return;
410
411 bit = hash_get(vrf_hash, &lookup, vrf_hash_bitmap_alloc);
412 bit->set = true;
413 }
414
415 void vrf_bitmap_unset(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 = false;
426 }
427
428 int vrf_bitmap_check(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 0;
436
437 bit = hash_lookup(vrf_hash, &lookup);
438 if (bit)
439 return bit->set;
440
441 return 0;
442 }
443
444 static void vrf_autocomplete(vector comps, struct cmd_token *token)
445 {
446 struct vrf *vrf = NULL;
447
448 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
449 if (vrf->vrf_id != VRF_DEFAULT)
450 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, vrf->name));
451 }
452 }
453
454 static const struct cmd_variable_handler vrf_var_handlers[] = {
455 {
456 .varname = "vrf",
457 .completions = vrf_autocomplete,
458 },
459 {.completions = NULL},
460 };
461
462 /* Initialize VRF module. */
463 void vrf_init(int (*create)(struct vrf *), int (*enable)(struct vrf *),
464 int (*disable)(struct vrf *), int (*delete)(struct vrf *))
465 {
466 struct vrf *default_vrf;
467
468 /* initialise NS, in case VRF backend if NETNS */
469 ns_init();
470 if (debug_vrf)
471 zlog_debug("%s: Initializing VRF subsystem",
472 __PRETTY_FUNCTION__);
473
474 vrf_master.vrf_new_hook = create;
475 vrf_master.vrf_enable_hook = enable;
476 vrf_master.vrf_disable_hook = disable;
477 vrf_master.vrf_delete_hook = delete;
478
479 /* The default VRF always exists. */
480 default_vrf = vrf_get(VRF_DEFAULT, VRF_DEFAULT_NAME);
481 if (!default_vrf) {
482 flog_err(LIB_ERR_VRF_START,
483 "vrf_init: failed to create the default VRF!");
484 exit(1);
485 }
486
487 /* Enable the default VRF. */
488 if (!vrf_enable(default_vrf)) {
489 flog_err(LIB_ERR_VRF_START,
490 "vrf_init: failed to enable the default VRF!");
491 exit(1);
492 }
493
494 cmd_variable_handler_register(vrf_var_handlers);
495 }
496
497 /* Terminate VRF module. */
498 void vrf_terminate(void)
499 {
500 struct vrf *vrf;
501
502 if (debug_vrf)
503 zlog_debug("%s: Shutting down vrf subsystem",
504 __PRETTY_FUNCTION__);
505
506 while (!RB_EMPTY(vrf_id_head, &vrfs_by_id)) {
507 vrf = RB_ROOT(vrf_id_head, &vrfs_by_id);
508
509 /* Clear configured flag and invoke delete. */
510 UNSET_FLAG(vrf->status, VRF_CONFIGURED);
511 vrf_delete(vrf);
512 }
513
514 while (!RB_EMPTY(vrf_name_head, &vrfs_by_name)) {
515 vrf = RB_ROOT(vrf_name_head, &vrfs_by_name);
516
517 /* Clear configured flag and invoke delete. */
518 UNSET_FLAG(vrf->status, VRF_CONFIGURED);
519 vrf_delete(vrf);
520 }
521 }
522
523 static int vrf_default_accepts_vrf(int type)
524 {
525 const char *fname = NULL;
526 char buf[32] = {0x0};
527 int ret = 0;
528 FILE *fd = NULL;
529
530 /*
531 * TCP & UDP services running in the default VRF context (ie., not bound
532 * to any VRF device) can work across all VRF domains by enabling the
533 * tcp_l3mdev_accept and udp_l3mdev_accept sysctl options:
534 * sysctl -w net.ipv4.tcp_l3mdev_accept=1
535 * sysctl -w net.ipv4.udp_l3mdev_accept=1
536 */
537 if (type == SOCK_STREAM)
538 fname = "/proc/sys/net/ipv4/tcp_l3mdev_accept";
539 else if (type == SOCK_DGRAM)
540 fname = "/proc/sys/net/ipv4/udp_l3mdev_accept";
541 else
542 return ret;
543 fd = fopen(fname, "r");
544 if (fd == NULL)
545 return ret;
546 fgets(buf, 32, fd);
547 ret = atoi(buf);
548 fclose(fd);
549 return ret;
550 }
551
552 /* Create a socket for the VRF. */
553 int vrf_socket(int domain, int type, int protocol, vrf_id_t vrf_id,
554 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(LIB_ERR_SOCKET, "%s: Can't switch to VRF %u (%s)",
561 __func__, vrf_id, safe_strerror(errno));
562
563 if (ret > 0 && interfacename && vrf_default_accepts_vrf(type)) {
564 zlog_err("VRF socket not used since net.ipv4.%s_l3mdev_accept != 0",
565 (type == SOCK_STREAM ? "tcp" : "udp"));
566 errno = EEXIST; /* not sure if this is the best error... */
567 return -2;
568 }
569
570 ret = socket(domain, type, protocol);
571 save_errno = errno;
572 ret2 = vrf_switchback_to_initial();
573 if (ret2 < 0)
574 flog_err_sys(LIB_ERR_SOCKET,
575 "%s: Can't switchback from VRF %u (%s)", __func__,
576 vrf_id, safe_strerror(errno));
577 errno = save_errno;
578 if (ret <= 0)
579 return ret;
580 ret2 = vrf_bind(vrf_id, ret, interfacename);
581 if (ret2 < 0) {
582 close(ret);
583 ret = ret2;
584 }
585 return ret;
586 }
587
588 int vrf_is_backend_netns(void)
589 {
590 return (vrf_backend == VRF_BACKEND_NETNS);
591 }
592
593 int vrf_get_backend(void)
594 {
595 return vrf_backend;
596 }
597
598 void vrf_configure_backend(int vrf_backend_netns)
599 {
600 vrf_backend = vrf_backend_netns;
601 }
602
603 int vrf_handler_create(struct vty *vty, const char *vrfname,
604 struct vrf **vrf)
605 {
606 struct vrf *vrfp;
607
608 if (strlen(vrfname) > VRF_NAMSIZ) {
609 if (vty)
610 vty_out(vty,
611 "%% VRF name %s invalid: length exceeds %d bytes\n",
612 vrfname, VRF_NAMSIZ);
613 else
614 zlog_warn(
615 "%% VRF name %s invalid: length exceeds %d bytes\n",
616 vrfname, VRF_NAMSIZ);
617 return CMD_WARNING_CONFIG_FAILED;
618 }
619
620 vrfp = vrf_get(VRF_UNKNOWN, vrfname);
621
622 if (vty)
623 VTY_PUSH_CONTEXT(VRF_NODE, vrfp);
624
625 if (vrf)
626 *vrf = vrfp;
627 return CMD_SUCCESS;
628 }
629
630 int vrf_netns_handler_create(struct vty *vty, struct vrf *vrf, char *pathname,
631 ns_id_t ns_id, ns_id_t internal_ns_id)
632 {
633 struct ns *ns = NULL;
634
635 if (!vrf)
636 return CMD_WARNING_CONFIG_FAILED;
637 if (vrf->vrf_id != VRF_UNKNOWN && vrf->ns_ctxt == NULL) {
638 if (vty)
639 vty_out(vty,
640 "VRF %u is already configured with VRF %s\n",
641 vrf->vrf_id, vrf->name);
642 else
643 zlog_warn("VRF %u is already configured with VRF %s\n",
644 vrf->vrf_id, vrf->name);
645 return CMD_WARNING_CONFIG_FAILED;
646 }
647 if (vrf->ns_ctxt != NULL) {
648 ns = (struct ns *)vrf->ns_ctxt;
649 if (!strcmp(ns->name, pathname)) {
650 if (vty)
651 vty_out(vty,
652 "VRF %u already configured with NETNS %s\n",
653 vrf->vrf_id, ns->name);
654 else
655 zlog_warn(
656 "VRF %u already configured with NETNS %s",
657 vrf->vrf_id, ns->name);
658 return CMD_WARNING_CONFIG_FAILED;
659 }
660 }
661 ns = ns_lookup_name(pathname);
662 if (ns && ns->vrf_ctxt) {
663 struct vrf *vrf2 = (struct vrf *)ns->vrf_ctxt;
664
665 if (vrf2 == vrf)
666 return CMD_SUCCESS;
667 if (vty)
668 vty_out(vty,
669 "NS %s is already configured"
670 " with VRF %u(%s)\n",
671 ns->name, vrf2->vrf_id, vrf2->name);
672 else
673 zlog_warn("NS %s is already configured with VRF %u(%s)",
674 ns->name, vrf2->vrf_id, vrf2->name);
675 return CMD_WARNING_CONFIG_FAILED;
676 }
677 ns = ns_get_created(ns, pathname, ns_id);
678 ns->internal_ns_id = internal_ns_id;
679 ns->vrf_ctxt = (void *)vrf;
680 vrf->ns_ctxt = (void *)ns;
681 /* update VRF netns NAME */
682 strlcpy(vrf->data.l.netns_name, basename(pathname), NS_NAMSIZ);
683
684 if (!ns_enable(ns, vrf_update_vrf_id)) {
685 if (vty)
686 vty_out(vty, "Can not associate NS %u with NETNS %s\n",
687 ns->ns_id, ns->name);
688 else
689 zlog_warn("Can not associate NS %u with NETNS %s",
690 ns->ns_id, ns->name);
691 return CMD_WARNING_CONFIG_FAILED;
692 }
693
694 return CMD_SUCCESS;
695 }
696
697 int vrf_is_mapped_on_netns(struct vrf *vrf)
698 {
699 if (!vrf || vrf->data.l.netns_name[0] == '\0')
700 return 0;
701 if (vrf->vrf_id == VRF_DEFAULT)
702 return 0;
703 return 1;
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 struct cmd_node vrf_node = {VRF_NODE, "%s(config-vrf)# ", 1};
762
763 DEFUN_NOSH (vrf_netns,
764 vrf_netns_cmd,
765 "netns NAME",
766 "Attach VRF to a Namespace\n"
767 "The file name in " NS_RUN_DIR ", or a full pathname\n")
768 {
769 int idx_name = 1, ret;
770 char *pathname = ns_netns_pathname(vty, argv[idx_name]->arg);
771
772 VTY_DECLVAR_CONTEXT(vrf, vrf);
773
774 if (!pathname)
775 return CMD_WARNING_CONFIG_FAILED;
776
777 frr_elevate_privs(vrf_daemon_privs) {
778 ret = vrf_netns_handler_create(vty, vrf, pathname,
779 NS_UNKNOWN, NS_UNKNOWN);
780 }
781 return ret;
782 }
783
784 DEFUN_NOSH (no_vrf_netns,
785 no_vrf_netns_cmd,
786 "no netns [NAME]",
787 NO_STR
788 "Detach VRF from a Namespace\n"
789 "The file name in " NS_RUN_DIR ", or a full pathname\n")
790 {
791 struct ns *ns = NULL;
792
793 VTY_DECLVAR_CONTEXT(vrf, vrf);
794
795 if (!vrf_is_backend_netns()) {
796 vty_out(vty, "VRF backend is not Netns. Aborting\n");
797 return CMD_WARNING_CONFIG_FAILED;
798 }
799 if (!vrf->ns_ctxt) {
800 vty_out(vty, "VRF %s(%u) is not configured with NetNS\n",
801 vrf->name, vrf->vrf_id);
802 return CMD_WARNING_CONFIG_FAILED;
803 }
804
805 ns = (struct ns *)vrf->ns_ctxt;
806
807 ns->vrf_ctxt = NULL;
808 vrf_disable(vrf);
809 /* vrf ID from VRF is necessary for Zebra
810 * so that propagate to other clients is done
811 */
812 ns_delete(ns);
813 vrf->ns_ctxt = NULL;
814 return CMD_SUCCESS;
815 }
816
817 /*
818 * Debug CLI for vrf's
819 */
820 DEFUN (vrf_debug,
821 vrf_debug_cmd,
822 "debug vrf",
823 DEBUG_STR
824 "VRF Debugging\n")
825 {
826 debug_vrf = 1;
827
828 return CMD_SUCCESS;
829 }
830
831 DEFUN (no_vrf_debug,
832 no_vrf_debug_cmd,
833 "no debug vrf",
834 NO_STR
835 DEBUG_STR
836 "VRF Debugging\n")
837 {
838 debug_vrf = 0;
839
840 return CMD_SUCCESS;
841 }
842
843 static int vrf_write_host(struct vty *vty)
844 {
845 if (debug_vrf)
846 vty_out(vty, "debug vrf\n");
847
848 return 1;
849 }
850
851 static struct cmd_node vrf_debug_node = {VRF_DEBUG_NODE, "", 1};
852
853 void vrf_install_commands(void)
854 {
855 install_node(&vrf_debug_node, vrf_write_host);
856
857 install_element(CONFIG_NODE, &vrf_debug_cmd);
858 install_element(ENABLE_NODE, &vrf_debug_cmd);
859 install_element(CONFIG_NODE, &no_vrf_debug_cmd);
860 install_element(ENABLE_NODE, &no_vrf_debug_cmd);
861 }
862
863 void vrf_cmd_init(int (*writefunc)(struct vty *vty),
864 struct zebra_privs_t *daemon_privs)
865 {
866 install_element(CONFIG_NODE, &vrf_cmd);
867 install_element(CONFIG_NODE, &no_vrf_cmd);
868 install_node(&vrf_node, writefunc);
869 install_default(VRF_NODE);
870 install_element(VRF_NODE, &vrf_exit_cmd);
871 if (vrf_is_backend_netns() && ns_have_netns()) {
872 /* Install NS commands. */
873 vrf_daemon_privs = daemon_privs;
874 install_element(VRF_NODE, &vrf_netns_cmd);
875 install_element(VRF_NODE, &no_vrf_netns_cmd);
876 }
877 }
878
879 vrf_id_t vrf_get_default_id(void)
880 {
881 struct vrf *vrf = vrf_lookup_by_name(VRF_DEFAULT_NAME);
882
883 if (vrf)
884 return vrf->vrf_id;
885 /* backend netns is only known by zebra
886 * for other daemons, we return VRF_DEFAULT_INTERNAL
887 */
888 if (vrf_is_backend_netns())
889 return ns_get_default_id();
890 else
891 return VRF_DEFAULT_INTERNAL;
892 }
893
894 int vrf_bind(vrf_id_t vrf_id, int fd, char *name)
895 {
896 int ret = 0;
897
898 if (fd < 0 || name == NULL)
899 return fd;
900 if (vrf_is_mapped_on_netns(vrf_lookup_by_id(vrf_id)))
901 return fd;
902 #ifdef SO_BINDTODEVICE
903 ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, name, strlen(name)+1);
904 if (ret < 0)
905 zlog_debug("bind to interface %s failed, errno=%d", name,
906 errno);
907 #endif /* SO_BINDTODEVICE */
908 return ret;
909 }
910 int vrf_getaddrinfo(const char *node, const char *service,
911 const struct addrinfo *hints, struct addrinfo **res,
912 vrf_id_t vrf_id)
913 {
914 int ret, ret2, save_errno;
915
916 ret = vrf_switch_to_netns(vrf_id);
917 if (ret < 0)
918 flog_err_sys(LIB_ERR_SOCKET, "%s: Can't switch to VRF %u (%s)",
919 __func__, vrf_id, safe_strerror(errno));
920 ret = getaddrinfo(node, service, hints, res);
921 save_errno = errno;
922 ret2 = vrf_switchback_to_initial();
923 if (ret2 < 0)
924 flog_err_sys(LIB_ERR_SOCKET,
925 "%s: Can't switchback from VRF %u (%s)", __func__,
926 vrf_id, safe_strerror(errno));
927 errno = save_errno;
928 return ret;
929 }
930
931 int vrf_ioctl(vrf_id_t vrf_id, int d, unsigned long request, char *params)
932 {
933 int ret, saved_errno, rc;
934
935 ret = vrf_switch_to_netns(vrf_id);
936 if (ret < 0) {
937 flog_err_sys(LIB_ERR_SOCKET, "%s: Can't switch to VRF %u (%s)",
938 __func__, vrf_id, safe_strerror(errno));
939 return 0;
940 }
941 rc = ioctl(d, request, params);
942 saved_errno = errno;
943 ret = vrf_switchback_to_initial();
944 if (ret < 0)
945 flog_err_sys(LIB_ERR_SOCKET,
946 "%s: Can't switchback from VRF %u (%s)", __func__,
947 vrf_id, safe_strerror(errno));
948 errno = saved_errno;
949 return rc;
950 }
951
952 int vrf_sockunion_socket(const union sockunion *su, vrf_id_t vrf_id,
953 char *interfacename)
954 {
955 int ret, save_errno, ret2;
956
957 ret = vrf_switch_to_netns(vrf_id);
958 if (ret < 0)
959 flog_err_sys(LIB_ERR_SOCKET, "%s: Can't switch to VRF %u (%s)",
960 __func__, vrf_id, safe_strerror(errno));
961 ret = sockunion_socket(su);
962 save_errno = errno;
963 ret2 = vrf_switchback_to_initial();
964 if (ret2 < 0)
965 flog_err_sys(LIB_ERR_SOCKET,
966 "%s: Can't switchback from VRF %u (%s)", __func__,
967 vrf_id, safe_strerror(errno));
968 errno = save_errno;
969
970 if (ret <= 0)
971 return ret;
972 ret2 = vrf_bind(vrf_id, ret, interfacename);
973 if (ret2 < 0) {
974 close(ret);
975 ret = ret2;
976 }
977 return ret;
978 }