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
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 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 struct ns_map_nsid_head ns_map_nsid_list
= RB_INITIALIZER(&ns_map_nsid_list
);
79 static ns_id_t ns_id_external_numbering
;
83 #define CLONE_NEWNET 0x40000000
84 /* New network namespace (lo, device, names sockets, etc) */
88 static inline int setns(int fd
, int nstype
)
91 return syscall(__NR_setns
, fd
, nstype
);
97 #endif /* !HAVE_SETNS */
100 static int have_netns_enabled
= -1;
101 #endif /* HAVE_NETNS */
103 /* default NS ID value used when VRF backend is not NETNS */
104 #define NS_DEFAULT_INTERNAL 0
106 static int have_netns(void)
109 if (have_netns_enabled
< 0) {
110 int fd
= open(NS_DEFAULT_NAME
, O_RDONLY
);
113 have_netns_enabled
= 0;
115 have_netns_enabled
= 1;
119 return have_netns_enabled
;
125 /* Holding NS hooks */
127 int (*ns_new_hook
)(struct ns
*ns
);
128 int (*ns_delete_hook
)(struct ns
*ns
);
129 int (*ns_enable_hook
)(struct ns
*ns
);
130 int (*ns_disable_hook
)(struct ns
*ns
);
135 static int ns_is_enabled(struct ns
*ns
);
137 static inline int ns_compare(const struct ns
*a
, const struct ns
*b
)
139 return (a
->ns_id
- b
->ns_id
);
142 /* Look up a NS by identifier. */
143 static struct ns
*ns_lookup_internal(ns_id_t ns_id
)
148 return RB_FIND(ns_head
, &ns_tree
, &ns
);
151 /* Look up a NS by name */
152 static struct ns
*ns_lookup_name_internal(const char *name
)
154 struct ns
*ns
= NULL
;
156 RB_FOREACH (ns
, ns_head
, &ns_tree
) {
157 if (ns
->name
!= NULL
) {
158 if (strcmp(name
, ns
->name
) == 0)
165 static struct ns
*ns_get_created_internal(struct ns
*ns
, char *name
,
170 * Initialize interfaces.
172 if (!ns
&& !name
&& ns_id
!= NS_UNKNOWN
)
173 ns
= ns_lookup_internal(ns_id
);
175 ns
= ns_lookup_name_internal(name
);
177 ns
= XCALLOC(MTYPE_NS
, sizeof(struct ns
));
180 ns
->name
= XSTRDUP(MTYPE_NS_NAME
, name
);
182 RB_INSERT(ns_head
, &ns_tree
, ns
);
185 if (ns_id
!= ns
->ns_id
) {
186 RB_REMOVE(ns_head
, &ns_tree
, ns
);
188 RB_INSERT(ns_head
, &ns_tree
, ns
);
193 if (ns
->ns_id
!= NS_UNKNOWN
)
194 zlog_info("NS %u is created.", ns
->ns_id
);
196 zlog_info("NS %s is created.", ns
->name
);
198 if (ns_master
.ns_new_hook
)
199 (*ns_master
.ns_new_hook
)(ns
);
204 * Enable a NS - that is, let the NS be ready to use.
205 * The NS_ENABLE_HOOK callback will be called to inform
206 * that they can allocate resources in this NS.
208 * RETURN: 1 - enabled successfully; otherwise, 0.
210 static int ns_enable_internal(struct ns
*ns
, void (*func
)(ns_id_t
, void *))
212 if (!ns_is_enabled(ns
)) {
214 ns
->fd
= open(ns
->name
, O_RDONLY
);
217 /* Remember ns_enable_hook has been called */
221 if (!ns_is_enabled(ns
)) {
222 zlog_err("Can not enable NS %u: %s!", ns
->ns_id
,
223 safe_strerror(errno
));
227 /* Non default NS. leave */
228 if (ns
->ns_id
== NS_UNKNOWN
) {
229 zlog_err("Can not enable NS %s %u: Invalid NSID",
230 ns
->name
, ns
->ns_id
);
234 func(ns
->ns_id
, (void *)ns
->vrf_ctxt
);
237 zlog_info("NS %u is associated with NETNS %s.",
238 ns
->ns_id
, ns
->name
);
239 zlog_info("NS %u is enabled.", ns
->ns_id
);
241 /* zebra first receives NS enable event,
242 * then VRF enable event
244 if (ns_master
.ns_enable_hook
)
245 (*ns_master
.ns_enable_hook
)(ns
);
252 * Check whether the NS is enabled - that is, whether the NS
253 * is ready to allocate resources. Currently there's only one
254 * type of resource: socket.
256 static int ns_is_enabled(struct ns
*ns
)
259 return ns
&& ns
->fd
>= 0;
261 return ns
&& ns
->fd
== -2 && ns
->ns_id
== NS_DEFAULT
;
265 * Disable a NS - that is, let the NS be unusable.
266 * The NS_DELETE_HOOK callback will be called to inform
267 * that they must release the resources in the NS.
269 static void ns_disable_internal(struct ns
*ns
)
271 if (ns_is_enabled(ns
)) {
273 zlog_info("NS %u is to be disabled.", ns
->ns_id
);
275 if (ns_master
.ns_disable_hook
)
276 (*ns_master
.ns_disable_hook
)(ns
);
285 /* VRF list existance check by name. */
286 static struct ns_map_nsid
*ns_map_nsid_lookup_by_nsid(ns_id_t ns_id
)
288 struct ns_map_nsid ns_map
;
290 ns_map
.ns_id
= ns_id
;
291 return RB_FIND(ns_map_nsid_head
, &ns_map_nsid_list
, &ns_map
);
294 ns_id_t
ns_map_nsid_with_external(ns_id_t ns_id
, bool map
)
296 struct ns_map_nsid
*ns_map
;
297 vrf_id_t ns_id_external
;
299 ns_map
= ns_map_nsid_lookup_by_nsid(ns_id
);
300 if (ns_map
&& !map
) {
301 ns_id_external
= ns_map
->ns_id_external
;
302 RB_REMOVE(ns_map_nsid_head
, &ns_map_nsid_list
, ns_map
);
303 return ns_id_external
;
306 return ns_map
->ns_id_external
;
307 ns_map
= XCALLOC(MTYPE_NS
, sizeof(struct ns_map_nsid
));
309 * default vrf is the first one : 0
311 ns_map
->ns_id_external
= ns_id_external_numbering
++;
312 ns_map
->ns_id
= ns_id
;
313 RB_INSERT(ns_map_nsid_head
, &ns_map_nsid_list
, ns_map
);
314 return ns_map
->ns_id_external
;
317 struct ns
*ns_get_created(struct ns
*ns
, char *name
, ns_id_t ns_id
)
319 return ns_get_created_internal(ns
, name
, ns_id
);
322 int ns_have_netns(void)
327 /* Delete a NS. This is called in ns_terminate(). */
328 void ns_delete(struct ns
*ns
)
331 zlog_info("NS %u is to be deleted.", ns
->ns_id
);
335 if (ns_master
.ns_delete_hook
)
336 (*ns_master
.ns_delete_hook
)(ns
);
339 * I'm not entirely sure if the vrf->iflist
340 * needs to be moved into here or not.
342 // if_terminate (&ns->iflist);
344 RB_REMOVE(ns_head
, &ns_tree
, ns
);
346 XFREE(MTYPE_NS_NAME
, ns
->name
);
351 /* Look up the data pointer of the specified VRF. */
352 void *ns_info_lookup(ns_id_t ns_id
)
354 struct ns
*ns
= ns_lookup_internal(ns_id
);
356 return ns
? ns
->info
: NULL
;
359 /* Look up a NS by name */
360 struct ns
*ns_lookup_name(const char *name
)
362 return ns_lookup_name_internal(name
);
365 int ns_enable(struct ns
*ns
, void (*func
)(ns_id_t
, void *))
367 return ns_enable_internal(ns
, func
);
370 void ns_disable(struct ns
*ns
)
372 return ns_disable_internal(ns
);
375 struct ns
*ns_lookup(ns_id_t ns_id
)
377 return ns_lookup_internal(ns_id
);
380 void ns_walk_func(int (*func
)(struct ns
*))
382 struct ns
*ns
= NULL
;
384 RB_FOREACH (ns
, ns_head
, &ns_tree
)
388 const char *ns_get_name(struct ns
*ns
)
395 /* Add a NS hook. Please add hooks before calling ns_init(). */
396 void ns_add_hook(int type
, int (*func
)(struct ns
*))
400 ns_master
.ns_new_hook
= func
;
403 ns_master
.ns_delete_hook
= func
;
406 ns_master
.ns_enable_hook
= func
;
408 case NS_DISABLE_HOOK
:
409 ns_master
.ns_disable_hook
= func
;
417 * NS realization with NETNS
420 char *ns_netns_pathname(struct vty
*vty
, const char *name
)
422 static char pathname
[PATH_MAX
];
426 if (name
[0] == '/') /* absolute pathname */
427 result
= realpath(name
, pathname
);
429 /* relevant pathname */
430 char tmp_name
[PATH_MAX
];
432 snprintf(tmp_name
, PATH_MAX
, "%s/%s", NS_RUN_DIR
, name
);
433 result
= realpath(tmp_name
, pathname
);
438 vty_out(vty
, "Invalid pathname for %s: %s\n",
440 safe_strerror(errno
));
442 zlog_warn("Invalid pathname for %s: %s",
444 safe_strerror(errno
));
447 check_base
= basename(pathname
);
448 if (check_base
!= NULL
&& strlen(check_base
) + 1 > NS_NAMSIZ
) {
450 vty_out(vty
, "NS name (%s) invalid: too long (>%d)\n",
451 check_base
, NS_NAMSIZ
- 1);
453 zlog_warn("NS name (%s) invalid: too long (>%d)",
454 check_base
, NS_NAMSIZ
- 1);
462 static int ns_initialised
;
465 /* silently return as initialisation done */
466 if (ns_initialised
== 1)
470 if (have_netns_enabled
< 0) {
471 ns_default_ns_fd
= open(NS_DEFAULT_NAME
, O_RDONLY
);
472 if (ns_default_ns_fd
== -1)
473 zlog_err("NS initialization failure %d(%s)",
474 errno
, safe_strerror(errno
));
476 ns_default_ns_fd
= -1;
480 ns_default_ns_fd
= -1;
482 #endif /* HAVE_NETNS */
483 ns_current_ns_fd
= -1;
487 /* Initialize NS module. */
488 void ns_init_management(ns_id_t default_ns_id
, ns_id_t internal_ns
)
493 default_ns
= ns_get_created_internal(NULL
, NULL
, default_ns_id
);
495 zlog_err("%s: failed to create the default NS!", __func__
);
499 fd
= open(NS_DEFAULT_NAME
, O_RDONLY
);
502 default_ns
->internal_ns_id
= internal_ns
;
504 /* Set the default NS name. */
505 default_ns
->name
= XSTRDUP(MTYPE_NS_NAME
, NS_DEFAULT_NAME
);
507 zlog_info("%s: default NSID is %u", __func__
,
510 /* Enable the default NS. */
511 if (!ns_enable(default_ns
, NULL
)) {
512 zlog_err("%s: failed to enable the default NS!", __func__
);
517 /* Terminate NS module. */
518 void ns_terminate(void)
522 while (!RB_EMPTY(ns_head
, &ns_tree
)) {
523 ns
= RB_ROOT(ns_head
, &ns_tree
);
529 int ns_switch_to_netns(const char *name
)
536 if (ns_default_ns_fd
== -1)
538 fd
= open(name
, O_RDONLY
);
543 ret
= setns(fd
, CLONE_NEWNET
);
544 ns_current_ns_fd
= fd
;
549 /* returns 1 if switch() was not called before
550 * return status of setns() otherwise
552 int ns_switchback_to_initial(void)
554 if (ns_current_ns_fd
!= -1 && ns_default_ns_fd
!= -1) {
557 ret
= setns(ns_default_ns_fd
, CLONE_NEWNET
);
558 ns_current_ns_fd
= -1;
561 /* silently ignore if setns() is not called */
565 /* Create a socket for the NS. */
566 int ns_socket(int domain
, int type
, int protocol
, ns_id_t ns_id
)
568 struct ns
*ns
= ns_lookup(ns_id
);
571 if (!ns
|| !ns_is_enabled(ns
)) {
576 ret
= (ns_id
!= NS_DEFAULT
) ? setns(ns
->fd
, CLONE_NEWNET
) : 0;
578 ret
= socket(domain
, type
, protocol
);
579 if (ns_id
!= NS_DEFAULT
) {
580 setns(ns_lookup(NS_DEFAULT
)->fd
, CLONE_NEWNET
);
581 ns_current_ns_fd
= ns_id
;
585 ret
= socket(domain
, type
, protocol
);
590 ns_id_t
ns_get_default_id(void)
593 return default_ns
->ns_id
;
594 return NS_DEFAULT_INTERNAL
;