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
41 #include "lib_errors.h"
43 DEFINE_MTYPE_STATIC(LIB
, NS
, "NetNS Context")
44 DEFINE_MTYPE_STATIC(LIB
, NS_NAME
, "NetNS Name")
46 /* default NS ID value used when VRF backend is not NETNS */
47 #define NS_DEFAULT_INTERNAL 0
49 static inline int ns_compare(const struct ns
*ns
, const struct ns
*ns2
);
50 static struct ns
*ns_lookup_name_internal(const char *name
);
52 RB_GENERATE(ns_head
, ns
, entry
, ns_compare
)
54 static struct ns_head ns_tree
= RB_INITIALIZER(&ns_tree
);
56 static struct ns
*default_ns
;
57 static int ns_current_ns_fd
;
58 static int ns_default_ns_fd
;
63 RB_ENTRY(ns_map_nsid
) id_entry
;
64 ns_id_t ns_id_external
;
68 static inline int ns_map_compare(const struct ns_map_nsid
*a
,
69 const struct ns_map_nsid
*b
)
71 return (a
->ns_id
- b
->ns_id
);
74 RB_HEAD(ns_map_nsid_head
, ns_map_nsid
);
75 RB_PROTOTYPE(ns_map_nsid_head
, ns_map_nsid
, id_entry
, ns_map_compare
);
76 RB_GENERATE(ns_map_nsid_head
, ns_map_nsid
, id_entry
, ns_map_compare
);
77 static struct ns_map_nsid_head ns_map_nsid_list
=
78 RB_INITIALIZER(&ns_map_nsid_list
);
80 static ns_id_t ns_id_external_numbering
;
84 #define CLONE_NEWNET 0x40000000
85 /* New network namespace (lo, device, names sockets, etc) */
89 static inline int setns(int fd
, int nstype
)
92 return syscall(__NR_setns
, fd
, nstype
);
98 #endif /* !HAVE_SETNS */
101 static int have_netns_enabled
= -1;
102 #endif /* HAVE_NETNS */
104 /* default NS ID value used when VRF backend is not NETNS */
105 #define NS_DEFAULT_INTERNAL 0
107 static int have_netns(void)
110 if (have_netns_enabled
< 0) {
111 int fd
= open(NS_DEFAULT_NAME
, O_RDONLY
);
114 have_netns_enabled
= 0;
116 have_netns_enabled
= 1;
120 return have_netns_enabled
;
126 /* Holding NS hooks */
127 static struct ns_master
{
128 int (*ns_new_hook
)(struct ns
*ns
);
129 int (*ns_delete_hook
)(struct ns
*ns
);
130 int (*ns_enable_hook
)(struct ns
*ns
);
131 int (*ns_disable_hook
)(struct ns
*ns
);
136 static int ns_is_enabled(struct ns
*ns
);
138 static inline int ns_compare(const struct ns
*a
, const struct ns
*b
)
140 return (a
->ns_id
- b
->ns_id
);
143 /* Look up a NS by identifier. */
144 static struct ns
*ns_lookup_internal(ns_id_t ns_id
)
149 return RB_FIND(ns_head
, &ns_tree
, &ns
);
152 /* Look up a NS by name */
153 static struct ns
*ns_lookup_name_internal(const char *name
)
155 struct ns
*ns
= NULL
;
157 RB_FOREACH (ns
, ns_head
, &ns_tree
) {
158 if (ns
->name
!= NULL
) {
159 if (strcmp(name
, ns
->name
) == 0)
166 static struct ns
*ns_get_created_internal(struct ns
*ns
, char *name
,
171 * Initialize interfaces.
173 if (!ns
&& !name
&& ns_id
!= NS_UNKNOWN
)
174 ns
= ns_lookup_internal(ns_id
);
176 ns
= ns_lookup_name_internal(name
);
178 ns
= XCALLOC(MTYPE_NS
, sizeof(struct ns
));
181 ns
->name
= XSTRDUP(MTYPE_NS_NAME
, name
);
183 RB_INSERT(ns_head
, &ns_tree
, ns
);
186 if (ns_id
!= ns
->ns_id
) {
187 RB_REMOVE(ns_head
, &ns_tree
, ns
);
189 RB_INSERT(ns_head
, &ns_tree
, ns
);
194 if (ns
->ns_id
!= NS_UNKNOWN
)
195 zlog_info("NS %u is created.", ns
->ns_id
);
197 zlog_info("NS %s is created.", ns
->name
);
199 if (ns_master
.ns_new_hook
)
200 (*ns_master
.ns_new_hook
)(ns
);
205 * Enable a NS - that is, let the NS be ready to use.
206 * The NS_ENABLE_HOOK callback will be called to inform
207 * that they can allocate resources in this NS.
209 * RETURN: 1 - enabled successfully; otherwise, 0.
211 static int ns_enable_internal(struct ns
*ns
, void (*func
)(ns_id_t
, void *))
213 if (!ns_is_enabled(ns
)) {
215 ns
->fd
= open(ns
->name
, O_RDONLY
);
218 /* Remember ns_enable_hook has been called */
222 if (!ns_is_enabled(ns
)) {
223 flog_err_sys(EC_LIB_SYSTEM_CALL
,
224 "Can not enable NS %u: %s!", ns
->ns_id
,
225 safe_strerror(errno
));
229 /* Non default NS. leave */
230 if (ns
->ns_id
== NS_UNKNOWN
) {
232 "Can not enable NS %s %u: Invalid NSID",
233 ns
->name
, ns
->ns_id
);
237 func(ns
->ns_id
, (void *)ns
->vrf_ctxt
);
240 zlog_info("NS %u is associated with NETNS %s.",
241 ns
->ns_id
, ns
->name
);
242 zlog_info("NS %u is enabled.", ns
->ns_id
);
244 /* zebra first receives NS enable event,
245 * then VRF enable event
247 if (ns_master
.ns_enable_hook
)
248 (*ns_master
.ns_enable_hook
)(ns
);
255 * Check whether the NS is enabled - that is, whether the NS
256 * is ready to allocate resources. Currently there's only one
257 * type of resource: socket.
259 static int ns_is_enabled(struct ns
*ns
)
262 return ns
&& ns
->fd
>= 0;
264 return ns
&& ns
->fd
== -2 && ns
->ns_id
== NS_DEFAULT
;
268 * Disable a NS - that is, let the NS be unusable.
269 * The NS_DELETE_HOOK callback will be called to inform
270 * that they must release the resources in the NS.
272 static void ns_disable_internal(struct ns
*ns
)
274 if (ns_is_enabled(ns
)) {
276 zlog_info("NS %u is to be disabled.", ns
->ns_id
);
278 if (ns_master
.ns_disable_hook
)
279 (*ns_master
.ns_disable_hook
)(ns
);
288 /* VRF list existance check by name. */
289 static struct ns_map_nsid
*ns_map_nsid_lookup_by_nsid(ns_id_t ns_id
)
291 struct ns_map_nsid ns_map
;
293 ns_map
.ns_id
= ns_id
;
294 return RB_FIND(ns_map_nsid_head
, &ns_map_nsid_list
, &ns_map
);
297 ns_id_t
ns_map_nsid_with_external(ns_id_t ns_id
, bool map
)
299 struct ns_map_nsid
*ns_map
;
300 vrf_id_t ns_id_external
;
302 ns_map
= ns_map_nsid_lookup_by_nsid(ns_id
);
303 if (ns_map
&& !map
) {
304 ns_id_external
= ns_map
->ns_id_external
;
305 RB_REMOVE(ns_map_nsid_head
, &ns_map_nsid_list
, ns_map
);
306 return ns_id_external
;
309 return ns_map
->ns_id_external
;
310 ns_map
= XCALLOC(MTYPE_NS
, sizeof(struct ns_map_nsid
));
312 * default vrf is the first one : 0
314 ns_map
->ns_id_external
= ns_id_external_numbering
++;
315 ns_map
->ns_id
= ns_id
;
316 RB_INSERT(ns_map_nsid_head
, &ns_map_nsid_list
, ns_map
);
317 return ns_map
->ns_id_external
;
320 struct ns
*ns_get_created(struct ns
*ns
, char *name
, ns_id_t ns_id
)
322 return ns_get_created_internal(ns
, name
, ns_id
);
325 int ns_have_netns(void)
330 /* Delete a NS. This is called in ns_terminate(). */
331 void ns_delete(struct ns
*ns
)
334 zlog_info("NS %u is to be deleted.", ns
->ns_id
);
338 if (ns_master
.ns_delete_hook
)
339 (*ns_master
.ns_delete_hook
)(ns
);
342 * I'm not entirely sure if the vrf->iflist
343 * needs to be moved into here or not.
345 // if_terminate (&ns->iflist);
347 RB_REMOVE(ns_head
, &ns_tree
, ns
);
348 XFREE(MTYPE_NS_NAME
, ns
->name
);
353 /* Look up the data pointer of the specified VRF. */
354 void *ns_info_lookup(ns_id_t ns_id
)
356 struct ns
*ns
= ns_lookup_internal(ns_id
);
358 return ns
? ns
->info
: NULL
;
361 /* Look up a NS by name */
362 struct ns
*ns_lookup_name(const char *name
)
364 return ns_lookup_name_internal(name
);
367 int ns_enable(struct ns
*ns
, void (*func
)(ns_id_t
, void *))
369 return ns_enable_internal(ns
, func
);
372 void ns_disable(struct ns
*ns
)
374 ns_disable_internal(ns
);
377 struct ns
*ns_lookup(ns_id_t ns_id
)
379 return ns_lookup_internal(ns_id
);
382 void ns_walk_func(int (*func
)(struct ns
*))
384 struct ns
*ns
= NULL
;
386 RB_FOREACH (ns
, ns_head
, &ns_tree
)
390 const char *ns_get_name(struct ns
*ns
)
397 /* Add a NS hook. Please add hooks before calling ns_init(). */
398 void ns_add_hook(int type
, int (*func
)(struct ns
*))
402 ns_master
.ns_new_hook
= func
;
405 ns_master
.ns_delete_hook
= func
;
408 ns_master
.ns_enable_hook
= func
;
410 case NS_DISABLE_HOOK
:
411 ns_master
.ns_disable_hook
= func
;
419 * NS realization with NETNS
422 char *ns_netns_pathname(struct vty
*vty
, const char *name
)
424 static char pathname
[PATH_MAX
];
428 if (name
[0] == '/') /* absolute pathname */
429 result
= realpath(name
, pathname
);
431 /* relevant pathname */
432 char tmp_name
[PATH_MAX
];
434 snprintf(tmp_name
, PATH_MAX
, "%s/%s", NS_RUN_DIR
, name
);
435 result
= realpath(tmp_name
, pathname
);
440 vty_out(vty
, "Invalid pathname for %s: %s\n",
442 safe_strerror(errno
));
444 flog_warn(EC_LIB_LINUX_NS
,
445 "Invalid pathname for %s: %s", pathname
,
446 safe_strerror(errno
));
449 check_base
= basename(pathname
);
450 if (check_base
!= NULL
&& strlen(check_base
) + 1 > NS_NAMSIZ
) {
452 vty_out(vty
, "NS name (%s) invalid: too long (>%d)\n",
453 check_base
, NS_NAMSIZ
- 1);
455 flog_warn(EC_LIB_LINUX_NS
,
456 "NS name (%s) invalid: too long (>%d)",
457 check_base
, NS_NAMSIZ
- 1);
465 static int ns_initialised
;
468 /* silently return as initialisation done */
469 if (ns_initialised
== 1)
473 ns_default_ns_fd
= open(NS_DEFAULT_NAME
, O_RDONLY
);
475 ns_default_ns_fd
= -1;
478 ns_current_ns_fd
= -1;
482 /* Initialize NS module. */
483 void ns_init_management(ns_id_t default_ns_id
, ns_id_t internal_ns
)
488 default_ns
= ns_get_created_internal(NULL
, NULL
, default_ns_id
);
490 flog_err(EC_LIB_NS
, "%s: failed to create the default NS!",
495 fd
= open(NS_DEFAULT_NAME
, O_RDONLY
);
498 default_ns
->internal_ns_id
= internal_ns
;
500 /* Set the default NS name. */
501 default_ns
->name
= XSTRDUP(MTYPE_NS_NAME
, NS_DEFAULT_NAME
);
503 zlog_info("%s: default NSID is %u", __func__
,
506 /* Enable the default NS. */
507 if (!ns_enable(default_ns
, NULL
)) {
508 flog_err(EC_LIB_NS
, "%s: failed to enable the default NS!",
514 /* Terminate NS module. */
515 void ns_terminate(void)
519 while (!RB_EMPTY(ns_head
, &ns_tree
)) {
520 ns
= RB_ROOT(ns_head
, &ns_tree
);
526 int ns_switch_to_netns(const char *name
)
533 if (ns_default_ns_fd
== -1)
535 fd
= open(name
, O_RDONLY
);
540 ret
= setns(fd
, CLONE_NEWNET
);
541 ns_current_ns_fd
= fd
;
546 /* returns 1 if switch() was not called before
547 * return status of setns() otherwise
549 int ns_switchback_to_initial(void)
551 if (ns_current_ns_fd
!= -1 && ns_default_ns_fd
!= -1) {
554 ret
= setns(ns_default_ns_fd
, CLONE_NEWNET
);
555 ns_current_ns_fd
= -1;
558 /* silently ignore if setns() is not called */
562 /* Create a socket for the NS. */
563 int ns_socket(int domain
, int type
, int protocol
, ns_id_t ns_id
)
565 struct ns
*ns
= ns_lookup(ns_id
);
568 if (!ns
|| !ns_is_enabled(ns
)) {
573 ret
= (ns_id
!= NS_DEFAULT
) ? setns(ns
->fd
, CLONE_NEWNET
) : 0;
575 ret
= socket(domain
, type
, protocol
);
576 if (ns_id
!= NS_DEFAULT
) {
577 setns(ns_lookup(NS_DEFAULT
)->fd
, CLONE_NEWNET
);
578 ns_current_ns_fd
= ns_id
;
582 ret
= socket(domain
, type
, protocol
);
587 ns_id_t
ns_get_default_id(void)
590 return default_ns
->ns_id
;
591 return NS_DEFAULT_INTERNAL
;