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