]> git.proxmox.com Git - mirror_frr.git/blob - lib/netns_linux.c
Merge pull request #5163 from ton31337/fix/do_not_reconnect_if_prefix_overflow_7.1
[mirror_frr.git] / lib / netns_linux.c
1 /*
2 * NS 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 #ifdef HAVE_NETNS
25 #undef _GNU_SOURCE
26 #define _GNU_SOURCE
27
28 #include <sched.h>
29 #endif
30
31 /* for basename */
32 #include <libgen.h>
33
34 #include "if.h"
35 #include "ns.h"
36 #include "log.h"
37 #include "memory.h"
38 #include "command.h"
39 #include "vty.h"
40 #include "vrf.h"
41 #include "lib_errors.h"
42
43 DEFINE_MTYPE_STATIC(LIB, NS, "NetNS Context")
44 DEFINE_MTYPE_STATIC(LIB, NS_NAME, "NetNS Name")
45
46 /* default NS ID value used when VRF backend is not NETNS */
47 #define NS_DEFAULT_INTERNAL 0
48
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);
51
52 RB_GENERATE(ns_head, ns, entry, ns_compare)
53
54 struct ns_head ns_tree = RB_INITIALIZER(&ns_tree);
55
56 static struct ns *default_ns;
57 static int ns_current_ns_fd;
58 static int ns_default_ns_fd;
59
60 static int ns_debug;
61
62 struct ns_map_nsid {
63 RB_ENTRY(ns_map_nsid) id_entry;
64 ns_id_t ns_id_external;
65 ns_id_t ns_id;
66 };
67
68 static inline int ns_map_compare(const struct ns_map_nsid *a,
69 const struct ns_map_nsid *b)
70 {
71 return (a->ns_id - b->ns_id);
72 }
73
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);
78
79 static ns_id_t ns_id_external_numbering;
80
81
82 #ifndef CLONE_NEWNET
83 #define CLONE_NEWNET 0x40000000
84 /* New network namespace (lo, device, names sockets, etc) */
85 #endif
86
87 #ifndef HAVE_SETNS
88 static inline int setns(int fd, int nstype)
89 {
90 #ifdef __NR_setns
91 return syscall(__NR_setns, fd, nstype);
92 #else
93 errno = EINVAL;
94 return -1;
95 #endif
96 }
97 #endif /* !HAVE_SETNS */
98
99 #ifdef HAVE_NETNS
100 static int have_netns_enabled = -1;
101 #endif /* HAVE_NETNS */
102
103 /* default NS ID value used when VRF backend is not NETNS */
104 #define NS_DEFAULT_INTERNAL 0
105
106 static int have_netns(void)
107 {
108 #ifdef HAVE_NETNS
109 if (have_netns_enabled < 0) {
110 int fd = open(NS_DEFAULT_NAME, O_RDONLY);
111
112 if (fd < 0)
113 have_netns_enabled = 0;
114 else {
115 have_netns_enabled = 1;
116 close(fd);
117 }
118 }
119 return have_netns_enabled;
120 #else
121 return 0;
122 #endif
123 }
124
125 /* Holding NS hooks */
126 struct ns_master {
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);
131 } ns_master = {
132 0,
133 };
134
135 static int ns_is_enabled(struct ns *ns);
136
137 static inline int ns_compare(const struct ns *a, const struct ns *b)
138 {
139 return (a->ns_id - b->ns_id);
140 }
141
142 /* Look up a NS by identifier. */
143 static struct ns *ns_lookup_internal(ns_id_t ns_id)
144 {
145 struct ns ns;
146
147 ns.ns_id = ns_id;
148 return RB_FIND(ns_head, &ns_tree, &ns);
149 }
150
151 /* Look up a NS by name */
152 static struct ns *ns_lookup_name_internal(const char *name)
153 {
154 struct ns *ns = NULL;
155
156 RB_FOREACH (ns, ns_head, &ns_tree) {
157 if (ns->name != NULL) {
158 if (strcmp(name, ns->name) == 0)
159 return ns;
160 }
161 }
162 return NULL;
163 }
164
165 static struct ns *ns_get_created_internal(struct ns *ns, char *name,
166 ns_id_t ns_id)
167 {
168 int created = 0;
169 /*
170 * Initialize interfaces.
171 */
172 if (!ns && !name && ns_id != NS_UNKNOWN)
173 ns = ns_lookup_internal(ns_id);
174 if (!ns && name)
175 ns = ns_lookup_name_internal(name);
176 if (!ns) {
177 ns = XCALLOC(MTYPE_NS, sizeof(struct ns));
178 ns->ns_id = ns_id;
179 if (name)
180 ns->name = XSTRDUP(MTYPE_NS_NAME, name);
181 ns->fd = -1;
182 RB_INSERT(ns_head, &ns_tree, ns);
183 created = 1;
184 }
185 if (ns_id != ns->ns_id) {
186 RB_REMOVE(ns_head, &ns_tree, ns);
187 ns->ns_id = ns_id;
188 RB_INSERT(ns_head, &ns_tree, ns);
189 }
190 if (!created)
191 return ns;
192 if (ns_debug) {
193 if (ns->ns_id != NS_UNKNOWN)
194 zlog_info("NS %u is created.", ns->ns_id);
195 else
196 zlog_info("NS %s is created.", ns->name);
197 }
198 if (ns_master.ns_new_hook)
199 (*ns_master.ns_new_hook)(ns);
200 return ns;
201 }
202
203 /*
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.
207 *
208 * RETURN: 1 - enabled successfully; otherwise, 0.
209 */
210 static int ns_enable_internal(struct ns *ns, void (*func)(ns_id_t, void *))
211 {
212 if (!ns_is_enabled(ns)) {
213 if (have_netns()) {
214 ns->fd = open(ns->name, O_RDONLY);
215 } else {
216 ns->fd = -2;
217 /* Remember ns_enable_hook has been called */
218 errno = -ENOTSUP;
219 }
220
221 if (!ns_is_enabled(ns)) {
222 flog_err_sys(EC_LIB_SYSTEM_CALL,
223 "Can not enable NS %u: %s!", ns->ns_id,
224 safe_strerror(errno));
225 return 0;
226 }
227
228 /* Non default NS. leave */
229 if (ns->ns_id == NS_UNKNOWN) {
230 flog_err(EC_LIB_NS,
231 "Can not enable NS %s %u: Invalid NSID",
232 ns->name, ns->ns_id);
233 return 0;
234 }
235 if (func)
236 func(ns->ns_id, (void *)ns->vrf_ctxt);
237 if (ns_debug) {
238 if (have_netns())
239 zlog_info("NS %u is associated with NETNS %s.",
240 ns->ns_id, ns->name);
241 zlog_info("NS %u is enabled.", ns->ns_id);
242 }
243 /* zebra first receives NS enable event,
244 * then VRF enable event
245 */
246 if (ns_master.ns_enable_hook)
247 (*ns_master.ns_enable_hook)(ns);
248 }
249
250 return 1;
251 }
252
253 /*
254 * Check whether the NS is enabled - that is, whether the NS
255 * is ready to allocate resources. Currently there's only one
256 * type of resource: socket.
257 */
258 static int ns_is_enabled(struct ns *ns)
259 {
260 if (have_netns())
261 return ns && ns->fd >= 0;
262 else
263 return ns && ns->fd == -2 && ns->ns_id == NS_DEFAULT;
264 }
265
266 /*
267 * Disable a NS - that is, let the NS be unusable.
268 * The NS_DELETE_HOOK callback will be called to inform
269 * that they must release the resources in the NS.
270 */
271 static void ns_disable_internal(struct ns *ns)
272 {
273 if (ns_is_enabled(ns)) {
274 if (ns_debug)
275 zlog_info("NS %u is to be disabled.", ns->ns_id);
276
277 if (ns_master.ns_disable_hook)
278 (*ns_master.ns_disable_hook)(ns);
279
280 if (have_netns())
281 close(ns->fd);
282
283 ns->fd = -1;
284 }
285 }
286
287 /* VRF list existance check by name. */
288 static struct ns_map_nsid *ns_map_nsid_lookup_by_nsid(ns_id_t ns_id)
289 {
290 struct ns_map_nsid ns_map;
291
292 ns_map.ns_id = ns_id;
293 return RB_FIND(ns_map_nsid_head, &ns_map_nsid_list, &ns_map);
294 }
295
296 ns_id_t ns_map_nsid_with_external(ns_id_t ns_id, bool map)
297 {
298 struct ns_map_nsid *ns_map;
299 vrf_id_t ns_id_external;
300
301 ns_map = ns_map_nsid_lookup_by_nsid(ns_id);
302 if (ns_map && !map) {
303 ns_id_external = ns_map->ns_id_external;
304 RB_REMOVE(ns_map_nsid_head, &ns_map_nsid_list, ns_map);
305 return ns_id_external;
306 }
307 if (ns_map)
308 return ns_map->ns_id_external;
309 ns_map = XCALLOC(MTYPE_NS, sizeof(struct ns_map_nsid));
310 /* increase vrf_id
311 * default vrf is the first one : 0
312 */
313 ns_map->ns_id_external = ns_id_external_numbering++;
314 ns_map->ns_id = ns_id;
315 RB_INSERT(ns_map_nsid_head, &ns_map_nsid_list, ns_map);
316 return ns_map->ns_id_external;
317 }
318
319 struct ns *ns_get_created(struct ns *ns, char *name, ns_id_t ns_id)
320 {
321 return ns_get_created_internal(ns, name, ns_id);
322 }
323
324 int ns_have_netns(void)
325 {
326 return have_netns();
327 }
328
329 /* Delete a NS. This is called in ns_terminate(). */
330 void ns_delete(struct ns *ns)
331 {
332 if (ns_debug)
333 zlog_info("NS %u is to be deleted.", ns->ns_id);
334
335 ns_disable(ns);
336
337 if (ns_master.ns_delete_hook)
338 (*ns_master.ns_delete_hook)(ns);
339
340 /*
341 * I'm not entirely sure if the vrf->iflist
342 * needs to be moved into here or not.
343 */
344 // if_terminate (&ns->iflist);
345
346 RB_REMOVE(ns_head, &ns_tree, ns);
347 XFREE(MTYPE_NS_NAME, ns->name);
348
349 XFREE(MTYPE_NS, ns);
350 }
351
352 /* Look up the data pointer of the specified VRF. */
353 void *ns_info_lookup(ns_id_t ns_id)
354 {
355 struct ns *ns = ns_lookup_internal(ns_id);
356
357 return ns ? ns->info : NULL;
358 }
359
360 /* Look up a NS by name */
361 struct ns *ns_lookup_name(const char *name)
362 {
363 return ns_lookup_name_internal(name);
364 }
365
366 int ns_enable(struct ns *ns, void (*func)(ns_id_t, void *))
367 {
368 return ns_enable_internal(ns, func);
369 }
370
371 void ns_disable(struct ns *ns)
372 {
373 return ns_disable_internal(ns);
374 }
375
376 struct ns *ns_lookup(ns_id_t ns_id)
377 {
378 return ns_lookup_internal(ns_id);
379 }
380
381 void ns_walk_func(int (*func)(struct ns *))
382 {
383 struct ns *ns = NULL;
384
385 RB_FOREACH (ns, ns_head, &ns_tree)
386 func(ns);
387 }
388
389 const char *ns_get_name(struct ns *ns)
390 {
391 if (!ns)
392 return NULL;
393 return ns->name;
394 }
395
396 /* Add a NS hook. Please add hooks before calling ns_init(). */
397 void ns_add_hook(int type, int (*func)(struct ns *))
398 {
399 switch (type) {
400 case NS_NEW_HOOK:
401 ns_master.ns_new_hook = func;
402 break;
403 case NS_DELETE_HOOK:
404 ns_master.ns_delete_hook = func;
405 break;
406 case NS_ENABLE_HOOK:
407 ns_master.ns_enable_hook = func;
408 break;
409 case NS_DISABLE_HOOK:
410 ns_master.ns_disable_hook = func;
411 break;
412 default:
413 break;
414 }
415 }
416
417 /*
418 * NS realization with NETNS
419 */
420
421 char *ns_netns_pathname(struct vty *vty, const char *name)
422 {
423 static char pathname[PATH_MAX];
424 char *result;
425 char *check_base;
426
427 if (name[0] == '/') /* absolute pathname */
428 result = realpath(name, pathname);
429 else {
430 /* relevant pathname */
431 char tmp_name[PATH_MAX];
432
433 snprintf(tmp_name, PATH_MAX, "%s/%s", NS_RUN_DIR, name);
434 result = realpath(tmp_name, pathname);
435 }
436
437 if (!result) {
438 if (vty)
439 vty_out(vty, "Invalid pathname for %s: %s\n",
440 pathname,
441 safe_strerror(errno));
442 else
443 flog_warn(EC_LIB_LINUX_NS,
444 "Invalid pathname for %s: %s", pathname,
445 safe_strerror(errno));
446 return NULL;
447 }
448 check_base = basename(pathname);
449 if (check_base != NULL && strlen(check_base) + 1 > NS_NAMSIZ) {
450 if (vty)
451 vty_out(vty, "NS name (%s) invalid: too long (>%d)\n",
452 check_base, NS_NAMSIZ - 1);
453 else
454 flog_warn(EC_LIB_LINUX_NS,
455 "NS name (%s) invalid: too long (>%d)",
456 check_base, NS_NAMSIZ - 1);
457 return NULL;
458 }
459 return pathname;
460 }
461
462 void ns_init(void)
463 {
464 static int ns_initialised;
465
466 ns_debug = 0;
467 /* silently return as initialisation done */
468 if (ns_initialised == 1)
469 return;
470 errno = 0;
471 if (have_netns())
472 ns_default_ns_fd = open(NS_DEFAULT_NAME, O_RDONLY);
473 else {
474 ns_default_ns_fd = -1;
475 default_ns = NULL;
476 }
477 ns_current_ns_fd = -1;
478 ns_initialised = 1;
479 }
480
481 /* Initialize NS module. */
482 void ns_init_management(ns_id_t default_ns_id, ns_id_t internal_ns)
483 {
484 int fd;
485
486 ns_init();
487 default_ns = ns_get_created_internal(NULL, NULL, default_ns_id);
488 if (!default_ns) {
489 flog_err(EC_LIB_NS, "%s: failed to create the default NS!",
490 __func__);
491 exit(1);
492 }
493 if (have_netns()) {
494 fd = open(NS_DEFAULT_NAME, O_RDONLY);
495 default_ns->fd = fd;
496 }
497 default_ns->internal_ns_id = internal_ns;
498
499 /* Set the default NS name. */
500 default_ns->name = XSTRDUP(MTYPE_NS_NAME, NS_DEFAULT_NAME);
501 if (ns_debug)
502 zlog_info("%s: default NSID is %u", __func__,
503 default_ns->ns_id);
504
505 /* Enable the default NS. */
506 if (!ns_enable(default_ns, NULL)) {
507 flog_err(EC_LIB_NS, "%s: failed to enable the default NS!",
508 __func__);
509 exit(1);
510 }
511 }
512
513 /* Terminate NS module. */
514 void ns_terminate(void)
515 {
516 struct ns *ns;
517
518 while (!RB_EMPTY(ns_head, &ns_tree)) {
519 ns = RB_ROOT(ns_head, &ns_tree);
520
521 ns_delete(ns);
522 }
523 }
524
525 int ns_switch_to_netns(const char *name)
526 {
527 int ret;
528 int fd;
529
530 if (name == NULL)
531 return -1;
532 if (ns_default_ns_fd == -1)
533 return -1;
534 fd = open(name, O_RDONLY);
535 if (fd == -1) {
536 errno = EINVAL;
537 return -1;
538 }
539 ret = setns(fd, CLONE_NEWNET);
540 ns_current_ns_fd = fd;
541 close(fd);
542 return ret;
543 }
544
545 /* returns 1 if switch() was not called before
546 * return status of setns() otherwise
547 */
548 int ns_switchback_to_initial(void)
549 {
550 if (ns_current_ns_fd != -1 && ns_default_ns_fd != -1) {
551 int ret;
552
553 ret = setns(ns_default_ns_fd, CLONE_NEWNET);
554 ns_current_ns_fd = -1;
555 return ret;
556 }
557 /* silently ignore if setns() is not called */
558 return 1;
559 }
560
561 /* Create a socket for the NS. */
562 int ns_socket(int domain, int type, int protocol, ns_id_t ns_id)
563 {
564 struct ns *ns = ns_lookup(ns_id);
565 int ret;
566
567 if (!ns || !ns_is_enabled(ns)) {
568 errno = EINVAL;
569 return -1;
570 }
571 if (have_netns()) {
572 ret = (ns_id != NS_DEFAULT) ? setns(ns->fd, CLONE_NEWNET) : 0;
573 if (ret >= 0) {
574 ret = socket(domain, type, protocol);
575 if (ns_id != NS_DEFAULT) {
576 setns(ns_lookup(NS_DEFAULT)->fd, CLONE_NEWNET);
577 ns_current_ns_fd = ns_id;
578 }
579 }
580 } else
581 ret = socket(domain, type, protocol);
582
583 return ret;
584 }
585
586 ns_id_t ns_get_default_id(void)
587 {
588 if (default_ns)
589 return default_ns->ns_id;
590 return NS_DEFAULT_INTERNAL;
591 }