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