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