]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipnetns.c
5 #include <sys/inotify.h>
8 #include <sys/syscall.h>
19 #include "ip_common.h"
21 #define NETNS_RUN_DIR "/var/run/netns"
22 #define NETNS_ETC_DIR "/etc/netns"
25 #define CLONE_NEWNET 0x40000000 /* New network namespace (lo, device, names sockets, etc) */
29 #define MNT_DETACH 0x00000002 /* Just detach from the tree */
30 #endif /* MNT_DETACH */
32 /* sys/mount.h may be out too old to have these */
38 #define MS_SLAVE (1 << 19)
42 #define MS_SHARED (1 << 20)
46 static int setns(int fd
, int nstype
)
49 return syscall(__NR_setns
, fd
, nstype
);
55 #endif /* HAVE_SETNS */
57 static int usage(void)
59 fprintf(stderr
, "Usage: ip netns list\n");
60 fprintf(stderr
, " ip netns add NAME\n");
61 fprintf(stderr
, " ip netns delete NAME\n");
62 fprintf(stderr
, " ip netns identify PID\n");
63 fprintf(stderr
, " ip netns pids NAME\n");
64 fprintf(stderr
, " ip netns exec NAME cmd ...\n");
65 fprintf(stderr
, " ip netns monitor\n");
69 int get_netns_fd(const char *name
)
71 char pathbuf
[MAXPATHLEN
];
72 const char *path
, *ptr
;
75 ptr
= strchr(name
, '/');
77 snprintf(pathbuf
, sizeof(pathbuf
), "%s/%s",
78 NETNS_RUN_DIR
, name
);
81 return open(path
, O_RDONLY
);
84 static int netns_list(int argc
, char **argv
)
89 dir
= opendir(NETNS_RUN_DIR
);
93 while ((entry
= readdir(dir
)) != NULL
) {
94 if (strcmp(entry
->d_name
, ".") == 0)
96 if (strcmp(entry
->d_name
, "..") == 0)
98 printf("%s\n", entry
->d_name
);
104 static void bind_etc(const char *name
)
106 char etc_netns_path
[MAXPATHLEN
];
107 char netns_name
[MAXPATHLEN
];
108 char etc_name
[MAXPATHLEN
];
109 struct dirent
*entry
;
112 snprintf(etc_netns_path
, sizeof(etc_netns_path
), "%s/%s", NETNS_ETC_DIR
, name
);
113 dir
= opendir(etc_netns_path
);
117 while ((entry
= readdir(dir
)) != NULL
) {
118 if (strcmp(entry
->d_name
, ".") == 0)
120 if (strcmp(entry
->d_name
, "..") == 0)
122 snprintf(netns_name
, sizeof(netns_name
), "%s/%s", etc_netns_path
, entry
->d_name
);
123 snprintf(etc_name
, sizeof(etc_name
), "/etc/%s", entry
->d_name
);
124 if (mount(netns_name
, etc_name
, "none", MS_BIND
, NULL
) < 0) {
125 fprintf(stderr
, "Bind %s -> %s failed: %s\n",
126 netns_name
, etc_name
, strerror(errno
));
132 static int netns_exec(int argc
, char **argv
)
134 /* Setup the proper environment for apps that are not netns
135 * aware, and execute a program in that environment.
137 const char *name
, *cmd
;
138 char net_path
[MAXPATHLEN
];
142 fprintf(stderr
, "No netns name specified\n");
146 fprintf(stderr
, "No command specified\n");
152 snprintf(net_path
, sizeof(net_path
), "%s/%s", NETNS_RUN_DIR
, name
);
153 netns
= open(net_path
, O_RDONLY
| O_CLOEXEC
);
155 fprintf(stderr
, "Cannot open network namespace \"%s\": %s\n",
156 name
, strerror(errno
));
160 if (setns(netns
, CLONE_NEWNET
) < 0) {
161 fprintf(stderr
, "seting the network namespace \"%s\" failed: %s\n",
162 name
, strerror(errno
));
166 if (unshare(CLONE_NEWNS
) < 0) {
167 fprintf(stderr
, "unshare failed: %s\n", strerror(errno
));
170 /* Don't let any mounts propagate back to the parent */
171 if (mount("", "/", "none", MS_SLAVE
| MS_REC
, NULL
)) {
172 fprintf(stderr
, "\"mount --make-rslave /\" failed: %s\n",
176 /* Mount a version of /sys that describes the network namespace */
177 if (umount2("/sys", MNT_DETACH
) < 0) {
178 fprintf(stderr
, "umount of /sys failed: %s\n", strerror(errno
));
181 if (mount(name
, "/sys", "sysfs", 0, NULL
) < 0) {
182 fprintf(stderr
, "mount of /sys failed: %s\n",strerror(errno
));
186 /* Setup bind mounts for config files in /etc */
203 if (waitpid(pid
, &status
, 0) < 0) {
208 if (WIFEXITED(status
)) {
209 /* ip must return the status of the child,
210 * but do_cmd() will add a minus to this,
211 * so let's add another one here to cancel it.
213 return -WEXITSTATUS(status
);
220 if (execvp(cmd
, argv
+ 1) < 0)
221 fprintf(stderr
, "exec of \"%s\" failed: %s\n",
222 cmd
, strerror(errno
));
226 static int is_pid(const char *str
)
229 for (; (ch
= *str
); str
++) {
236 static int netns_pids(int argc
, char **argv
)
239 char net_path
[MAXPATHLEN
];
243 struct dirent
*entry
;
246 fprintf(stderr
, "No netns name specified\n");
250 fprintf(stderr
, "extra arguments specified\n");
255 snprintf(net_path
, sizeof(net_path
), "%s/%s", NETNS_RUN_DIR
, name
);
256 netns
= open(net_path
, O_RDONLY
);
258 fprintf(stderr
, "Cannot open network namespace: %s\n",
262 if (fstat(netns
, &netst
) < 0) {
263 fprintf(stderr
, "Stat of netns failed: %s\n",
267 dir
= opendir("/proc/");
269 fprintf(stderr
, "Open of /proc failed: %s\n",
273 while((entry
= readdir(dir
))) {
274 char pid_net_path
[MAXPATHLEN
];
276 if (!is_pid(entry
->d_name
))
278 snprintf(pid_net_path
, sizeof(pid_net_path
), "/proc/%s/ns/net",
280 if (stat(pid_net_path
, &st
) != 0)
282 if ((st
.st_dev
== netst
.st_dev
) &&
283 (st
.st_ino
== netst
.st_ino
)) {
284 printf("%s\n", entry
->d_name
);
292 static int netns_identify(int argc
, char **argv
)
295 char net_path
[MAXPATHLEN
];
299 struct dirent
*entry
;
302 fprintf(stderr
, "No pid specified\n");
306 fprintf(stderr
, "extra arguments specified\n");
311 if (!is_pid(pidstr
)) {
312 fprintf(stderr
, "Specified string '%s' is not a pid\n",
317 snprintf(net_path
, sizeof(net_path
), "/proc/%s/ns/net", pidstr
);
318 netns
= open(net_path
, O_RDONLY
);
320 fprintf(stderr
, "Cannot open network namespace: %s\n",
324 if (fstat(netns
, &netst
) < 0) {
325 fprintf(stderr
, "Stat of netns failed: %s\n",
329 dir
= opendir(NETNS_RUN_DIR
);
331 /* Succeed treat a missing directory as an empty directory */
335 fprintf(stderr
, "Failed to open directory %s:%s\n",
336 NETNS_RUN_DIR
, strerror(errno
));
340 while((entry
= readdir(dir
))) {
341 char name_path
[MAXPATHLEN
];
344 if (strcmp(entry
->d_name
, ".") == 0)
346 if (strcmp(entry
->d_name
, "..") == 0)
349 snprintf(name_path
, sizeof(name_path
), "%s/%s", NETNS_RUN_DIR
,
352 if (stat(name_path
, &st
) != 0)
355 if ((st
.st_dev
== netst
.st_dev
) &&
356 (st
.st_ino
== netst
.st_ino
)) {
357 printf("%s\n", entry
->d_name
);
365 static int netns_delete(int argc
, char **argv
)
368 char netns_path
[MAXPATHLEN
];
371 fprintf(stderr
, "No netns name specified\n");
376 snprintf(netns_path
, sizeof(netns_path
), "%s/%s", NETNS_RUN_DIR
, name
);
377 umount2(netns_path
, MNT_DETACH
);
378 if (unlink(netns_path
) < 0) {
379 fprintf(stderr
, "Cannot remove namespace file \"%s\": %s\n",
380 netns_path
, strerror(errno
));
386 static int netns_add(int argc
, char **argv
)
388 /* This function creates a new network namespace and
389 * a new mount namespace and bind them into a well known
390 * location in the filesystem based on the name provided.
392 * The mount namespace is created so that any necessary
393 * userspace tweaks like remounting /sys, or bind mounting
394 * a new /etc/resolv.conf can be shared between uers.
396 char netns_path
[MAXPATHLEN
];
399 int made_netns_run_dir_mount
= 0;
402 fprintf(stderr
, "No netns name specified\n");
407 snprintf(netns_path
, sizeof(netns_path
), "%s/%s", NETNS_RUN_DIR
, name
);
409 /* Create the base netns directory if it doesn't exist */
410 mkdir(NETNS_RUN_DIR
, S_IRWXU
|S_IRGRP
|S_IXGRP
|S_IROTH
|S_IXOTH
);
412 /* Make it possible for network namespace mounts to propagate between
413 * mount namespaces. This makes it likely that a unmounting a network
414 * namespace file in one namespace will unmount the network namespace
415 * file in all namespaces allowing the network namespace to be freed
418 while (mount("", NETNS_RUN_DIR
, "none", MS_SHARED
| MS_REC
, NULL
)) {
419 /* Fail unless we need to make the mount point */
420 if (errno
!= EINVAL
|| made_netns_run_dir_mount
) {
421 fprintf(stderr
, "mount --make-shared %s failed: %s\n",
422 NETNS_RUN_DIR
, strerror(errno
));
426 /* Upgrade NETNS_RUN_DIR to a mount point */
427 if (mount(NETNS_RUN_DIR
, NETNS_RUN_DIR
, "none", MS_BIND
, NULL
)) {
428 fprintf(stderr
, "mount --bind %s %s failed: %s\n",
429 NETNS_RUN_DIR
, NETNS_RUN_DIR
, strerror(errno
));
432 made_netns_run_dir_mount
= 1;
435 /* Create the filesystem state */
436 fd
= open(netns_path
, O_RDONLY
|O_CREAT
|O_EXCL
, 0);
438 fprintf(stderr
, "Cannot not create namespace file \"%s\": %s\n",
439 netns_path
, strerror(errno
));
443 if (unshare(CLONE_NEWNET
) < 0) {
444 fprintf(stderr
, "Failed to create a new network namespace \"%s\": %s\n",
445 name
, strerror(errno
));
449 /* Bind the netns last so I can watch for it */
450 if (mount("/proc/self/ns/net", netns_path
, "none", MS_BIND
, NULL
) < 0) {
451 fprintf(stderr
, "Bind /proc/self/ns/net -> %s failed: %s\n",
452 netns_path
, strerror(errno
));
457 netns_delete(argc
, argv
);
462 static int netns_monitor(int argc
, char **argv
)
465 struct inotify_event
*event
;
469 fprintf(stderr
, "inotify_init failed: %s\n",
473 if (inotify_add_watch(fd
, NETNS_RUN_DIR
, IN_CREATE
| IN_DELETE
) < 0) {
474 fprintf(stderr
, "inotify_add_watch failed: %s\n",
479 ssize_t len
= read(fd
, buf
, sizeof(buf
));
481 fprintf(stderr
, "read failed: %s\n",
485 for (event
= (struct inotify_event
*)buf
;
486 (char *)event
< &buf
[len
];
487 event
= (struct inotify_event
*)((char *)event
+ sizeof(*event
) + event
->len
)) {
488 if (event
->mask
& IN_CREATE
)
489 printf("add %s\n", event
->name
);
490 if (event
->mask
& IN_DELETE
)
491 printf("delete %s\n", event
->name
);
497 int do_netns(int argc
, char **argv
)
500 return netns_list(0, NULL
);
502 if ((matches(*argv
, "list") == 0) || (matches(*argv
, "show") == 0) ||
503 (matches(*argv
, "lst") == 0))
504 return netns_list(argc
-1, argv
+1);
506 if (matches(*argv
, "help") == 0)
509 if (matches(*argv
, "add") == 0)
510 return netns_add(argc
-1, argv
+1);
512 if (matches(*argv
, "delete") == 0)
513 return netns_delete(argc
-1, argv
+1);
515 if (matches(*argv
, "identify") == 0)
516 return netns_identify(argc
-1, argv
+1);
518 if (matches(*argv
, "pids") == 0)
519 return netns_pids(argc
-1, argv
+1);
521 if (matches(*argv
, "exec") == 0)
522 return netns_exec(argc
-1, argv
+1);
524 if (matches(*argv
, "monitor") == 0)
525 return netns_monitor(argc
-1, argv
+1);
527 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv
);