]>
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)
47 static int setns(int fd
, int nstype
)
50 return syscall(__NR_setns
, fd
, nstype
);
56 #endif /* HAVE_SETNS */
58 static int usage(void)
60 fprintf(stderr
, "Usage: ip netns list\n");
61 fprintf(stderr
, " ip netns add NAME\n");
62 fprintf(stderr
, " ip netns delete NAME\n");
63 fprintf(stderr
, " ip netns identify PID\n");
64 fprintf(stderr
, " ip netns pids NAME\n");
65 fprintf(stderr
, " ip netns exec NAME cmd ...\n");
66 fprintf(stderr
, " ip netns monitor\n");
70 int get_netns_fd(const char *name
)
72 char pathbuf
[MAXPATHLEN
];
73 const char *path
, *ptr
;
76 ptr
= strchr(name
, '/');
78 snprintf(pathbuf
, sizeof(pathbuf
), "%s/%s",
79 NETNS_RUN_DIR
, name
);
82 return open(path
, O_RDONLY
);
85 static int netns_list(int argc
, char **argv
)
90 dir
= opendir(NETNS_RUN_DIR
);
94 while ((entry
= readdir(dir
)) != NULL
) {
95 if (strcmp(entry
->d_name
, ".") == 0)
97 if (strcmp(entry
->d_name
, "..") == 0)
99 printf("%s\n", entry
->d_name
);
105 static void bind_etc(const char *name
)
107 char etc_netns_path
[MAXPATHLEN
];
108 char netns_name
[MAXPATHLEN
];
109 char etc_name
[MAXPATHLEN
];
110 struct dirent
*entry
;
113 snprintf(etc_netns_path
, sizeof(etc_netns_path
), "%s/%s", NETNS_ETC_DIR
, name
);
114 dir
= opendir(etc_netns_path
);
118 while ((entry
= readdir(dir
)) != NULL
) {
119 if (strcmp(entry
->d_name
, ".") == 0)
121 if (strcmp(entry
->d_name
, "..") == 0)
123 snprintf(netns_name
, sizeof(netns_name
), "%s/%s", etc_netns_path
, entry
->d_name
);
124 snprintf(etc_name
, sizeof(etc_name
), "/etc/%s", entry
->d_name
);
125 if (mount(netns_name
, etc_name
, "none", MS_BIND
, NULL
) < 0) {
126 fprintf(stderr
, "Bind %s -> %s failed: %s\n",
127 netns_name
, etc_name
, strerror(errno
));
133 static int netns_exec(int argc
, char **argv
)
135 /* Setup the proper environment for apps that are not netns
136 * aware, and execute a program in that environment.
138 const char *name
, *cmd
;
139 char net_path
[MAXPATHLEN
];
143 fprintf(stderr
, "No netns name specified\n");
147 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
));
159 if (setns(netns
, CLONE_NEWNET
) < 0) {
160 fprintf(stderr
, "seting the network namespace \"%s\" failed: %s\n",
161 name
, strerror(errno
));
165 if (unshare(CLONE_NEWNS
) < 0) {
166 fprintf(stderr
, "unshare failed: %s\n", strerror(errno
));
169 /* Don't let any mounts propogate back to the parent */
170 if (mount("", "/", "none", MS_SLAVE
| MS_REC
, NULL
)) {
171 fprintf(stderr
, "\"mount --make-rslave /\" failed: %s\n",
175 /* Mount a version of /sys that describes the network namespace */
176 if (umount2("/sys", MNT_DETACH
) < 0) {
177 fprintf(stderr
, "umount of /sys failed: %s\n", strerror(errno
));
180 if (mount(name
, "/sys", "sysfs", 0, NULL
) < 0) {
181 fprintf(stderr
, "mount of /sys failed: %s\n",strerror(errno
));
185 /* Setup bind mounts for config files in /etc */
188 if (execvp(cmd
, argv
+ 1) < 0)
189 fprintf(stderr
, "exec of \"%s\" failed: %s\n",
190 cmd
, strerror(errno
));
194 static int is_pid(const char *str
)
197 for (; (ch
= *str
); str
++) {
204 static int netns_pids(int argc
, char **argv
)
207 char net_path
[MAXPATHLEN
];
211 struct dirent
*entry
;
214 fprintf(stderr
, "No netns name specified\n");
218 fprintf(stderr
, "extra arguments specified\n");
223 snprintf(net_path
, sizeof(net_path
), "%s/%s", NETNS_RUN_DIR
, name
);
224 netns
= open(net_path
, O_RDONLY
);
226 fprintf(stderr
, "Cannot open network namespace: %s\n",
230 if (fstat(netns
, &netst
) < 0) {
231 fprintf(stderr
, "Stat of netns failed: %s\n",
235 dir
= opendir("/proc/");
237 fprintf(stderr
, "Open of /proc failed: %s\n",
241 while((entry
= readdir(dir
))) {
242 char pid_net_path
[MAXPATHLEN
];
244 if (!is_pid(entry
->d_name
))
246 snprintf(pid_net_path
, sizeof(pid_net_path
), "/proc/%s/ns/net",
248 if (stat(pid_net_path
, &st
) != 0)
250 if ((st
.st_dev
== netst
.st_dev
) &&
251 (st
.st_ino
== netst
.st_ino
)) {
252 printf("%s\n", entry
->d_name
);
260 static int netns_identify(int argc
, char **argv
)
263 char net_path
[MAXPATHLEN
];
267 struct dirent
*entry
;
270 fprintf(stderr
, "No pid specified\n");
274 fprintf(stderr
, "extra arguments specified\n");
279 if (!is_pid(pidstr
)) {
280 fprintf(stderr
, "Specified string '%s' is not a pid\n",
285 snprintf(net_path
, sizeof(net_path
), "/proc/%s/ns/net", pidstr
);
286 netns
= open(net_path
, O_RDONLY
);
288 fprintf(stderr
, "Cannot open network namespace: %s\n",
292 if (fstat(netns
, &netst
) < 0) {
293 fprintf(stderr
, "Stat of netns failed: %s\n",
297 dir
= opendir(NETNS_RUN_DIR
);
299 /* Succeed treat a missing directory as an empty directory */
303 fprintf(stderr
, "Failed to open directory %s:%s\n",
304 NETNS_RUN_DIR
, strerror(errno
));
308 while((entry
= readdir(dir
))) {
309 char name_path
[MAXPATHLEN
];
312 if (strcmp(entry
->d_name
, ".") == 0)
314 if (strcmp(entry
->d_name
, "..") == 0)
317 snprintf(name_path
, sizeof(name_path
), "%s/%s", NETNS_RUN_DIR
,
320 if (stat(name_path
, &st
) != 0)
323 if ((st
.st_dev
== netst
.st_dev
) &&
324 (st
.st_ino
== netst
.st_ino
)) {
325 printf("%s\n", entry
->d_name
);
333 static int netns_delete(int argc
, char **argv
)
336 char netns_path
[MAXPATHLEN
];
339 fprintf(stderr
, "No netns name specified\n");
344 snprintf(netns_path
, sizeof(netns_path
), "%s/%s", NETNS_RUN_DIR
, name
);
345 umount2(netns_path
, MNT_DETACH
);
346 if (unlink(netns_path
) < 0) {
347 fprintf(stderr
, "Cannot remove namespace file \"%s\": %s\n",
348 netns_path
, strerror(errno
));
354 static int netns_add(int argc
, char **argv
)
356 /* This function creates a new network namespace and
357 * a new mount namespace and bind them into a well known
358 * location in the filesystem based on the name provided.
360 * The mount namespace is created so that any necessary
361 * userspace tweaks like remounting /sys, or bind mounting
362 * a new /etc/resolv.conf can be shared between uers.
364 char netns_path
[MAXPATHLEN
];
367 int made_netns_run_dir_mount
= 0;
370 fprintf(stderr
, "No netns name specified\n");
375 snprintf(netns_path
, sizeof(netns_path
), "%s/%s", NETNS_RUN_DIR
, name
);
377 /* Create the base netns directory if it doesn't exist */
378 mkdir(NETNS_RUN_DIR
, S_IRWXU
|S_IRGRP
|S_IXGRP
|S_IROTH
|S_IXOTH
);
380 /* Make it possible for network namespace mounts to propogate between
381 * mount namespaces. This makes it likely that a unmounting a network
382 * namespace file in one namespace will unmount the network namespace
383 * file in all namespaces allowing the network namespace to be freed
386 while (mount("", NETNS_RUN_DIR
, "none", MS_SHARED
| MS_REC
, NULL
)) {
387 /* Fail unless we need to make the mount point */
388 if (errno
!= EINVAL
|| made_netns_run_dir_mount
) {
389 fprintf(stderr
, "mount --make-shared %s failed: %s\n",
390 NETNS_RUN_DIR
, strerror(errno
));
394 /* Upgrade NETNS_RUN_DIR to a mount point */
395 if (mount(NETNS_RUN_DIR
, NETNS_RUN_DIR
, "none", MS_BIND
, NULL
)) {
396 fprintf(stderr
, "mount --bind %s %s failed: %s\n",
397 NETNS_RUN_DIR
, NETNS_RUN_DIR
, strerror(errno
));
400 made_netns_run_dir_mount
= 1;
403 /* Create the filesystem state */
404 fd
= open(netns_path
, O_RDONLY
|O_CREAT
|O_EXCL
, 0);
406 fprintf(stderr
, "Cannot not create namespace file \"%s\": %s\n",
407 netns_path
, strerror(errno
));
411 if (unshare(CLONE_NEWNET
) < 0) {
412 fprintf(stderr
, "Failed to create a new network namespace \"%s\": %s\n",
413 name
, strerror(errno
));
417 /* Bind the netns last so I can watch for it */
418 if (mount("/proc/self/ns/net", netns_path
, "none", MS_BIND
, NULL
) < 0) {
419 fprintf(stderr
, "Bind /proc/self/ns/net -> %s failed: %s\n",
420 netns_path
, strerror(errno
));
425 netns_delete(argc
, argv
);
430 static int netns_monitor(int argc
, char **argv
)
433 struct inotify_event
*event
;
437 fprintf(stderr
, "inotify_init failed: %s\n",
441 if (inotify_add_watch(fd
, NETNS_RUN_DIR
, IN_CREATE
| IN_DELETE
) < 0) {
442 fprintf(stderr
, "inotify_add_watch failed: %s\n",
447 ssize_t len
= read(fd
, buf
, sizeof(buf
));
449 fprintf(stderr
, "read failed: %s\n",
453 for (event
= (struct inotify_event
*)buf
;
454 (char *)event
< &buf
[len
];
455 event
= (struct inotify_event
*)((char *)event
+ sizeof(*event
) + event
->len
)) {
456 if (event
->mask
& IN_CREATE
)
457 printf("add %s\n", event
->name
);
458 if (event
->mask
& IN_DELETE
)
459 printf("delete %s\n", event
->name
);
465 int do_netns(int argc
, char **argv
)
468 return netns_list(0, NULL
);
470 if ((matches(*argv
, "list") == 0) || (matches(*argv
, "show") == 0) ||
471 (matches(*argv
, "lst") == 0))
472 return netns_list(argc
-1, argv
+1);
474 if (matches(*argv
, "help") == 0)
477 if (matches(*argv
, "add") == 0)
478 return netns_add(argc
-1, argv
+1);
480 if (matches(*argv
, "delete") == 0)
481 return netns_delete(argc
-1, argv
+1);
483 if (matches(*argv
, "identify") == 0)
484 return netns_identify(argc
-1, argv
+1);
486 if (matches(*argv
, "pids") == 0)
487 return netns_pids(argc
-1, argv
+1);
489 if (matches(*argv
, "exec") == 0)
490 return netns_exec(argc
-1, argv
+1);
492 if (matches(*argv
, "monitor") == 0)
493 return netns_monitor(argc
-1, argv
+1);
495 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip netns help\".\n", *argv
);