3 * Copyright (C) 2014 6WIND S.A.
5 * This file is part of GNU Zebra.
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.
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.
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
37 #include "nexthop_group.h"
39 /* default VRF ID value used when VRF backend is not NETNS */
40 #define VRF_DEFAULT_INTERNAL 0
42 DEFINE_MTYPE_STATIC(LIB
, VRF
, "VRF")
43 DEFINE_MTYPE_STATIC(LIB
, VRF_BITMAP
, "VRF bit-map")
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
*);
50 RB_GENERATE(vrf_id_head
, vrf
, id_entry
, vrf_id_compare
);
51 RB_GENERATE(vrf_name_head
, vrf
, name_entry
, vrf_name_compare
);
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
);
56 static int vrf_backend
;
57 static struct zebra_privs_t
*vrf_daemon_privs
;
60 * Turn on/off debug code
65 /* Holding VRF hooks */
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
*);
75 static int vrf_is_enabled(struct vrf
*vrf
);
77 /* VRF list existance check by name. */
78 struct vrf
*vrf_lookup_by_name(const char *name
)
81 strlcpy(vrf
.name
, name
, sizeof(vrf
.name
));
82 return (RB_FIND(vrf_name_head
, &vrfs_by_name
, &vrf
));
85 static __inline
int vrf_id_compare(const struct vrf
*a
, const struct vrf
*b
)
87 return (a
->vrf_id
- b
->vrf_id
);
90 static int vrf_name_compare(const struct vrf
*a
, const struct vrf
*b
)
92 return strcmp(a
->name
, b
->name
);
95 /* if ns_id is different and not VRF_UNKNOWN,
96 * then update vrf identifier, and enable VRF
98 static void vrf_update_vrf_id(ns_id_t ns_id
, void *opaqueptr
)
100 ns_id_t vrf_id
= (vrf_id_t
)ns_id
;
102 struct vrf
*vrf
= (struct vrf
*)opaqueptr
;
106 old_vrf_id
= vrf
->vrf_id
;
107 if (vrf_id
== vrf
->vrf_id
)
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
);
117 int vrf_switch_to_netns(vrf_id_t vrf_id
)
120 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
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
);
130 zlog_debug("VRF_SWITCH: %s(%u)", name
, vrf
->vrf_id
);
131 return ns_switch_to_netns(name
);
134 int vrf_switchback_to_initial(void)
136 int ret
= ns_switchback_to_initial();
138 if (ret
== 0 && debug_vrf
)
139 zlog_debug("VRF_SWITCHBACK");
143 /* Get a VRF. If not found, create one.
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
150 struct vrf
*vrf_get(vrf_id_t vrf_id
, const char *name
)
152 struct vrf
*vrf
= NULL
;
156 zlog_debug("VRF_GET: %s(%u)", name
== NULL
? "(NULL)" : name
,
159 /* Nothing to see, move along here */
160 if (!name
&& vrf_id
== VRF_UNKNOWN
)
163 /* attempt to find already available VRF
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
);
172 vrf
= XCALLOC(MTYPE_VRF
, sizeof(struct vrf
));
173 vrf
->vrf_id
= VRF_UNKNOWN
;
178 zlog_debug("VRF(%u) %s is created.", vrf_id
,
179 (name
) ? name
: "(NULL)");
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
);
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
);
197 if (new &&vrf_master
.vrf_new_hook
)
198 (*vrf_master
.vrf_new_hook
)(vrf
);
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()).
206 void vrf_delete(struct vrf
*vrf
)
209 zlog_debug("VRF %u is to be deleted.", vrf
->vrf_id
);
211 if (vrf_is_enabled(vrf
))
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.
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.
225 RB_REMOVE(vrf_id_head
, &vrfs_by_id
, vrf
);
226 vrf
->vrf_id
= VRF_UNKNOWN
;
231 if (vrf_master
.vrf_delete_hook
)
232 (*vrf_master
.vrf_delete_hook
)(vrf
);
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
);
242 XFREE(MTYPE_VRF
, vrf
);
245 /* Look up a VRF by identifier. */
246 struct vrf
*vrf_lookup_by_id(vrf_id_t vrf_id
)
250 return (RB_FIND(vrf_id_head
, &vrfs_by_id
, &vrf
));
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.
258 * RETURN: 1 - enabled successfully; otherwise, 0.
260 int vrf_enable(struct vrf
*vrf
)
262 if (vrf_is_enabled(vrf
))
266 zlog_debug("VRF %u is enabled.", vrf
->vrf_id
);
268 SET_FLAG(vrf
->status
, VRF_ACTIVE
);
270 if (vrf_master
.vrf_enable_hook
)
271 (*vrf_master
.vrf_enable_hook
)(vrf
);
274 * If we have any nexthop group entries that
275 * are awaiting vrf initialization then
276 * let's let people know about it
278 nexthop_group_enable_vrf(vrf
);
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.
288 void vrf_disable(struct vrf
*vrf
)
290 if (!vrf_is_enabled(vrf
))
293 UNSET_FLAG(vrf
->status
, VRF_ACTIVE
);
296 zlog_debug("VRF %u is to be disabled.", vrf
->vrf_id
);
298 /* Till now, nothing to be done for the default VRF. */
299 // Pending: see why this statement.
301 if (vrf_master
.vrf_disable_hook
)
302 (*vrf_master
.vrf_disable_hook
)(vrf
);
305 const char *vrf_id_to_name(vrf_id_t vrf_id
)
309 vrf
= vrf_lookup_by_id(vrf_id
);
316 vrf_id_t
vrf_name_to_id(const char *name
)
319 vrf_id_t vrf_id
= VRF_DEFAULT
; // Pending: need a way to return invalid
320 // id/ routine not used.
322 vrf
= vrf_lookup_by_name(name
);
324 vrf_id
= vrf
->vrf_id
;
329 /* Get the data pointer of the specified VRF. If not found, create one. */
330 void *vrf_info_get(vrf_id_t vrf_id
)
332 struct vrf
*vrf
= vrf_get(vrf_id
, NULL
);
336 /* Look up the data pointer of the specified VRF. */
337 void *vrf_info_lookup(vrf_id_t vrf_id
)
339 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
340 return vrf
? vrf
->info
: NULL
;
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 */
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)
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))
360 uint8_t *groups
[VRF_BITMAP_NUM_OF_GROUPS
];
363 vrf_bitmap_t
vrf_bitmap_init(void)
365 return (vrf_bitmap_t
)XCALLOC(MTYPE_VRF_BITMAP
,
366 sizeof(struct vrf_bitmap
));
369 void vrf_bitmap_free(vrf_bitmap_t bmap
)
371 struct vrf_bitmap
*bm
= (struct vrf_bitmap
*)bmap
;
374 if (bmap
== VRF_BITMAP_NULL
)
377 for (i
= 0; i
< VRF_BITMAP_NUM_OF_GROUPS
; i
++)
379 XFREE(MTYPE_VRF_BITMAP
, bm
->groups
[i
]);
381 XFREE(MTYPE_VRF_BITMAP
, bm
);
384 void vrf_bitmap_set(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
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
);
390 if (bmap
== VRF_BITMAP_NULL
|| vrf_id
== VRF_UNKNOWN
)
393 if (bm
->groups
[group
] == NULL
)
394 bm
->groups
[group
] = XCALLOC(MTYPE_VRF_BITMAP
,
395 VRF_BITMAP_NUM_OF_BYTES_IN_GROUP
);
397 SET_FLAG(bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP(offset
)],
398 VRF_BITMAP_FLAG(offset
));
401 void vrf_bitmap_unset(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
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
);
407 if (bmap
== VRF_BITMAP_NULL
|| vrf_id
== VRF_UNKNOWN
408 || bm
->groups
[group
] == NULL
)
411 UNSET_FLAG(bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP(offset
)],
412 VRF_BITMAP_FLAG(offset
));
415 int vrf_bitmap_check(vrf_bitmap_t bmap
, vrf_id_t vrf_id
)
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
);
421 if (bmap
== VRF_BITMAP_NULL
|| vrf_id
== VRF_UNKNOWN
422 || bm
->groups
[group
] == NULL
)
425 return CHECK_FLAG(bm
->groups
[group
][VRF_BITMAP_INDEX_IN_GROUP(offset
)],
426 VRF_BITMAP_FLAG(offset
))
431 static void vrf_autocomplete(vector comps
, struct cmd_token
*token
)
433 struct vrf
*vrf
= NULL
;
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
));
441 static const struct cmd_variable_handler vrf_var_handlers
[] = {
444 .completions
= vrf_autocomplete
,
446 {.completions
= NULL
},
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
*))
453 struct vrf
*default_vrf
;
455 /* initialise NS, in case VRF backend if NETNS */
458 zlog_debug("%s: Initializing VRF subsystem",
459 __PRETTY_FUNCTION__
);
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;
466 /* The default VRF always exists. */
467 default_vrf
= vrf_get(VRF_DEFAULT
, VRF_DEFAULT_NAME
);
469 zlog_err("vrf_init: failed to create the default VRF!");
473 /* Enable the default VRF. */
474 if (!vrf_enable(default_vrf
)) {
475 zlog_err("vrf_init: failed to enable the default VRF!");
479 cmd_variable_handler_register(vrf_var_handlers
);
482 /* Terminate VRF module. */
483 void vrf_terminate(void)
488 zlog_debug("%s: Shutting down vrf subsystem",
489 __PRETTY_FUNCTION__
);
491 while (!RB_EMPTY(vrf_id_head
, &vrfs_by_id
)) {
492 vrf
= RB_ROOT(vrf_id_head
, &vrfs_by_id
);
494 /* Clear configured flag and invoke delete. */
495 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
499 while (!RB_EMPTY(vrf_name_head
, &vrfs_by_name
)) {
500 vrf
= RB_ROOT(vrf_name_head
, &vrfs_by_name
);
502 /* Clear configured flag and invoke delete. */
503 UNSET_FLAG(vrf
->status
, VRF_CONFIGURED
);
508 static int vrf_default_accepts_vrf(int type
)
510 const char *fname
= NULL
;
511 char buf
[32] = {0x0};
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
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";
528 fd
= fopen(fname
, "r");
537 /* Create a socket for the VRF. */
538 int vrf_socket(int domain
, int type
, int protocol
, vrf_id_t vrf_id
,
541 int ret
, save_errno
, ret2
;
543 ret
= vrf_switch_to_netns(vrf_id
);
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... */
553 ret
= socket(domain
, type
, protocol
);
555 ret2
= vrf_switchback_to_initial();
557 zlog_err("%s: Can't switchback from VRF %u (%s)", __func__
,
558 vrf_id
, safe_strerror(errno
));
562 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);
570 int vrf_is_backend_netns(void)
572 return (vrf_backend
== VRF_BACKEND_NETNS
);
575 int vrf_get_backend(void)
580 void vrf_configure_backend(int vrf_backend_netns
)
582 vrf_backend
= vrf_backend_netns
;
585 int vrf_handler_create(struct vty
*vty
, const char *vrfname
,
590 if (strlen(vrfname
) > VRF_NAMSIZ
) {
593 "%% VRF name %s invalid: length exceeds %d bytes\n",
594 vrfname
, VRF_NAMSIZ
);
597 "%% VRF name %s invalid: length exceeds %d bytes\n",
598 vrfname
, VRF_NAMSIZ
);
599 return CMD_WARNING_CONFIG_FAILED
;
602 vrfp
= vrf_get(VRF_UNKNOWN
, vrfname
);
605 VTY_PUSH_CONTEXT(VRF_NODE
, vrfp
);
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
)
615 struct ns
*ns
= NULL
;
618 return CMD_WARNING_CONFIG_FAILED
;
619 if (vrf
->vrf_id
!= VRF_UNKNOWN
&& vrf
->ns_ctxt
== NULL
) {
622 "VRF %u is already configured with VRF %s\n",
623 vrf
->vrf_id
, vrf
->name
);
625 zlog_warn("VRF %u is already configured with VRF %s\n",
626 vrf
->vrf_id
, vrf
->name
);
627 return CMD_WARNING_CONFIG_FAILED
;
629 if (vrf
->ns_ctxt
!= NULL
) {
630 ns
= (struct ns
*)vrf
->ns_ctxt
;
631 if (!strcmp(ns
->name
, pathname
)) {
634 "VRF %u already configured with NETNS %s\n",
635 vrf
->vrf_id
, ns
->name
);
638 "VRF %u already configured with NETNS %s",
639 vrf
->vrf_id
, ns
->name
);
640 return CMD_WARNING_CONFIG_FAILED
;
643 ns
= ns_lookup_name(pathname
);
644 if (ns
&& ns
->vrf_ctxt
) {
645 struct vrf
*vrf2
= (struct vrf
*)ns
->vrf_ctxt
;
651 "NS %s is already configured"
652 " with VRF %u(%s)\n",
653 ns
->name
, vrf2
->vrf_id
, vrf2
->name
);
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
;
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
);
666 if (!ns_enable(ns
, vrf_update_vrf_id
)) {
668 vty_out(vty
, "Can not associate NS %u with NETNS %s\n",
669 ns
->ns_id
, ns
->name
);
671 zlog_warn("Can not associate NS %u with NETNS %s",
672 ns
->ns_id
, ns
->name
);
673 return CMD_WARNING_CONFIG_FAILED
;
679 int vrf_is_mapped_on_netns(vrf_id_t vrf_id
)
681 struct vrf
*vrf
= vrf_lookup_by_id(vrf_id
);
683 if (!vrf
|| vrf
->data
.l
.netns_name
[0] == '\0')
685 if (vrf
->vrf_id
== VRF_DEFAULT
)
690 /* vrf CLI commands */
694 "Exit current mode and down to previous mode\n")
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
;
705 "Select a VRF to configure\n"
709 const char *vrfname
= argv
[idx_name
]->arg
;
711 return vrf_handler_create(vty
, vrfname
, NULL
);
718 "Delete a pseudo VRF's configuration\n"
721 const char *vrfname
= argv
[2]->arg
;
725 vrfp
= vrf_lookup_by_name(vrfname
);
728 vty_out(vty
, "%% VRF %s does not exist\n", vrfname
);
729 return CMD_WARNING_CONFIG_FAILED
;
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
;
737 /* Clear configured flag and invoke delete. */
738 UNSET_FLAG(vrfp
->status
, VRF_CONFIGURED
);
745 struct cmd_node vrf_node
= {VRF_NODE
, "%s(config-vrf)# ", 1};
750 "Attach VRF to a Namespace\n"
751 "The file name in " NS_RUN_DIR
", or a full pathname\n")
753 int idx_name
= 1, ret
;
754 char *pathname
= ns_netns_pathname(vty
, argv
[idx_name
]->arg
);
756 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
759 return CMD_WARNING_CONFIG_FAILED
;
761 if (vrf_daemon_privs
&&
762 vrf_daemon_privs
->change(ZPRIVS_RAISE
))
763 zlog_err("%s: Can't raise privileges", __func__
);
765 ret
= vrf_netns_handler_create(vty
, vrf
, pathname
,
766 NS_UNKNOWN
, NS_UNKNOWN
);
768 if (vrf_daemon_privs
&&
769 vrf_daemon_privs
->change(ZPRIVS_LOWER
))
770 zlog_err("%s: Can't lower privileges", __func__
);
778 "Detach VRF from a Namespace\n"
779 "The file name in " NS_RUN_DIR
", or a full pathname\n")
781 struct ns
*ns
= NULL
;
783 VTY_DECLVAR_CONTEXT(vrf
, vrf
);
785 if (!vrf_is_backend_netns()) {
786 vty_out(vty
, "VRF backend is not Netns. Aborting\n");
787 return CMD_WARNING_CONFIG_FAILED
;
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
;
795 ns
= (struct ns
*)vrf
->ns_ctxt
;
799 /* vrf ID from VRF is necessary for Zebra
800 * so that propagate to other clients is done
808 * Debug CLI for vrf's
833 static int vrf_write_host(struct vty
*vty
)
836 vty_out(vty
, "debug vrf\n");
841 static struct cmd_node vrf_debug_node
= {VRF_DEBUG_NODE
, "", 1};
843 void vrf_install_commands(void)
845 install_node(&vrf_debug_node
, vrf_write_host
);
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
);
853 void vrf_cmd_init(int (*writefunc
)(struct vty
*vty
),
854 struct zebra_privs_t
*daemon_privs
)
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
);
869 vrf_id_t
vrf_get_default_id(void)
871 struct vrf
*vrf
= vrf_lookup_by_name(VRF_DEFAULT_NAME
);
875 /* backend netns is only known by zebra
876 * for other daemons, we return VRF_DEFAULT_INTERNAL
878 if (vrf_is_backend_netns())
879 return ns_get_default_id();
881 return VRF_DEFAULT_INTERNAL
;
884 int vrf_bind(vrf_id_t vrf_id
, int fd
, char *name
)
888 if (fd
< 0 || name
== NULL
)
890 if (vrf_is_mapped_on_netns(vrf_id
))
892 #ifdef SO_BINDTODEVICE
893 ret
= setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
, name
, strlen(name
)+1);
895 zlog_debug("bind to interface %s failed, errno=%d", name
,
897 #endif /* SO_BINDTODEVICE */
900 int vrf_getaddrinfo(const char *node
, const char *service
,
901 const struct addrinfo
*hints
, struct addrinfo
**res
,
904 int ret
, ret2
, save_errno
;
906 ret
= vrf_switch_to_netns(vrf_id
);
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
);
912 ret2
= vrf_switchback_to_initial();
914 zlog_err("%s: Can't switchback from VRF %u (%s)", __func__
,
915 vrf_id
, safe_strerror(errno
));
920 int vrf_ioctl(vrf_id_t vrf_id
, int d
, unsigned long request
, char *params
)
922 int ret
, saved_errno
, rc
;
924 ret
= vrf_switch_to_netns(vrf_id
);
926 zlog_err("%s: Can't switch to VRF %u (%s)", __func__
, vrf_id
,
927 safe_strerror(errno
));
930 rc
= ioctl(d
, request
, params
);
932 ret
= vrf_switchback_to_initial();
934 zlog_err("%s: Can't switchback from VRF %u (%s)", __func__
,
935 vrf_id
, safe_strerror(errno
));
940 int vrf_sockunion_socket(const union sockunion
*su
, vrf_id_t vrf_id
,
943 int ret
, save_errno
, ret2
;
945 ret
= vrf_switch_to_netns(vrf_id
);
947 zlog_err("%s: Can't switch to VRF %u (%s)", __func__
, vrf_id
,
948 safe_strerror(errno
));
949 ret
= sockunion_socket(su
);
951 ret2
= vrf_switchback_to_initial();
953 zlog_err("%s: Can't switchback from VRF %u (%s)", __func__
,
954 vrf_id
, safe_strerror(errno
));
959 ret2
= vrf_bind(vrf_id
, ret
, interfacename
);