|
|
a4b897 |
commit 99ea60996425f1baa6bcb07d01323b80129c2141
|
|
|
a4b897 |
Author: Pavel Šimerda <psimerda@redhat.com>
|
|
|
a4b897 |
Date: Mon Jun 1 18:56:09 2015 +0200
|
|
|
a4b897 |
|
|
|
a4b897 |
backport selected ip-netns features
|
|
|
a4b897 |
|
|
|
a4b897 |
diff --git a/ip/ipnetns.c b/ip/ipnetns.c
|
|
|
a4b897 |
index 794a498..24df167 100644
|
|
|
a4b897 |
--- a/ip/ipnetns.c
|
|
|
a4b897 |
+++ b/ip/ipnetns.c
|
|
|
a4b897 |
@@ -15,180 +15,151 @@
|
|
|
a4b897 |
#include <unistd.h>
|
|
|
a4b897 |
#include <ctype.h>
|
|
|
a4b897 |
|
|
|
a4b897 |
+#include <linux/net_namespace.h>
|
|
|
a4b897 |
+
|
|
|
a4b897 |
#include "utils.h"
|
|
|
a4b897 |
#include "ip_common.h"
|
|
|
a4b897 |
-
|
|
|
a4b897 |
-#define NETNS_RUN_DIR "/var/run/netns"
|
|
|
a4b897 |
-#define NETNS_ETC_DIR "/etc/netns"
|
|
|
a4b897 |
-
|
|
|
a4b897 |
-#ifndef CLONE_NEWNET
|
|
|
a4b897 |
-#define CLONE_NEWNET 0x40000000 /* New network namespace (lo, device, names sockets, etc) */
|
|
|
a4b897 |
-#endif
|
|
|
a4b897 |
-
|
|
|
a4b897 |
-#ifndef MNT_DETACH
|
|
|
a4b897 |
-#define MNT_DETACH 0x00000002 /* Just detach from the tree */
|
|
|
a4b897 |
-#endif /* MNT_DETACH */
|
|
|
a4b897 |
-
|
|
|
a4b897 |
-/* sys/mount.h may be out too old to have these */
|
|
|
a4b897 |
-#ifndef MS_REC
|
|
|
a4b897 |
-#define MS_REC 16384
|
|
|
a4b897 |
-#endif
|
|
|
a4b897 |
-
|
|
|
a4b897 |
-#ifndef MS_SLAVE
|
|
|
a4b897 |
-#define MS_SLAVE (1 << 19)
|
|
|
a4b897 |
-#endif
|
|
|
a4b897 |
-
|
|
|
a4b897 |
-#ifndef MS_SHARED
|
|
|
a4b897 |
-#define MS_SHARED (1 << 20)
|
|
|
a4b897 |
-#endif
|
|
|
a4b897 |
-
|
|
|
a4b897 |
-#ifndef HAVE_SETNS
|
|
|
a4b897 |
-static int setns(int fd, int nstype)
|
|
|
a4b897 |
-{
|
|
|
a4b897 |
-#ifdef __NR_setns
|
|
|
a4b897 |
- return syscall(__NR_setns, fd, nstype);
|
|
|
a4b897 |
-#else
|
|
|
a4b897 |
- errno = ENOSYS;
|
|
|
a4b897 |
- return -1;
|
|
|
a4b897 |
-#endif
|
|
|
a4b897 |
-}
|
|
|
a4b897 |
-#endif /* HAVE_SETNS */
|
|
|
a4b897 |
+#include "namespace.h"
|
|
|
a4b897 |
|
|
|
a4b897 |
static int usage(void)
|
|
|
a4b897 |
{
|
|
|
a4b897 |
fprintf(stderr, "Usage: ip netns list\n");
|
|
|
a4b897 |
fprintf(stderr, " ip netns add NAME\n");
|
|
|
a4b897 |
- fprintf(stderr, " ip netns delete NAME\n");
|
|
|
a4b897 |
- fprintf(stderr, " ip netns identify PID\n");
|
|
|
a4b897 |
+ fprintf(stderr, " ip netns set NAME NETNSID\n");
|
|
|
a4b897 |
+ fprintf(stderr, " ip [-all] netns delete [NAME]\n");
|
|
|
a4b897 |
+ fprintf(stderr, " ip netns identify [PID]\n");
|
|
|
a4b897 |
fprintf(stderr, " ip netns pids NAME\n");
|
|
|
a4b897 |
- fprintf(stderr, " ip netns exec NAME cmd ...\n");
|
|
|
a4b897 |
+ fprintf(stderr, " ip [-all] netns exec [NAME] cmd ...\n");
|
|
|
a4b897 |
fprintf(stderr, " ip netns monitor\n");
|
|
|
a4b897 |
exit(-1);
|
|
|
a4b897 |
}
|
|
|
a4b897 |
|
|
|
a4b897 |
-int get_netns_fd(const char *name)
|
|
|
a4b897 |
+static int have_rtnl_getnsid = -1;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+static int ipnetns_accept_msg(const struct sockaddr_nl *who,
|
|
|
a4b897 |
+ struct nlmsghdr *n, void *arg)
|
|
|
a4b897 |
{
|
|
|
a4b897 |
- char pathbuf[MAXPATHLEN];
|
|
|
a4b897 |
- const char *path, *ptr;
|
|
|
a4b897 |
+ struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(n);
|
|
|
a4b897 |
|
|
|
a4b897 |
- path = name;
|
|
|
a4b897 |
- ptr = strchr(name, '/');
|
|
|
a4b897 |
- if (!ptr) {
|
|
|
a4b897 |
- snprintf(pathbuf, sizeof(pathbuf), "%s/%s",
|
|
|
a4b897 |
- NETNS_RUN_DIR, name );
|
|
|
a4b897 |
- path = pathbuf;
|
|
|
a4b897 |
- }
|
|
|
a4b897 |
- return open(path, O_RDONLY);
|
|
|
a4b897 |
+ if (n->nlmsg_type == NLMSG_ERROR &&
|
|
|
a4b897 |
+ (err->error == -EOPNOTSUPP || err->error == -EINVAL))
|
|
|
a4b897 |
+ have_rtnl_getnsid = 0;
|
|
|
a4b897 |
+ else
|
|
|
a4b897 |
+ have_rtnl_getnsid = 1;
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
}
|
|
|
a4b897 |
|
|
|
a4b897 |
-static int netns_list(int argc, char **argv)
|
|
|
a4b897 |
+static int ipnetns_have_nsid(void)
|
|
|
a4b897 |
{
|
|
|
a4b897 |
- struct dirent *entry;
|
|
|
a4b897 |
- DIR *dir;
|
|
|
a4b897 |
+ struct {
|
|
|
a4b897 |
+ struct nlmsghdr n;
|
|
|
a4b897 |
+ struct rtgenmsg g;
|
|
|
a4b897 |
+ char buf[1024];
|
|
|
a4b897 |
+ } req;
|
|
|
a4b897 |
+ int fd;
|
|
|
a4b897 |
|
|
|
a4b897 |
- dir = opendir(NETNS_RUN_DIR);
|
|
|
a4b897 |
- if (!dir)
|
|
|
a4b897 |
- return 0;
|
|
|
a4b897 |
+ if (have_rtnl_getnsid < 0) {
|
|
|
a4b897 |
+ memset(&req, 0, sizeof(req));
|
|
|
a4b897 |
+ req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
|
|
|
a4b897 |
+ req.n.nlmsg_flags = NLM_F_REQUEST;
|
|
|
a4b897 |
+ req.n.nlmsg_type = RTM_GETNSID;
|
|
|
a4b897 |
+ req.g.rtgen_family = AF_UNSPEC;
|
|
|
a4b897 |
|
|
|
a4b897 |
- while ((entry = readdir(dir)) != NULL) {
|
|
|
a4b897 |
- if (strcmp(entry->d_name, ".") == 0)
|
|
|
a4b897 |
- continue;
|
|
|
a4b897 |
- if (strcmp(entry->d_name, "..") == 0)
|
|
|
a4b897 |
- continue;
|
|
|
a4b897 |
- printf("%s\n", entry->d_name);
|
|
|
a4b897 |
+ fd = open("/proc/self/ns/net", O_RDONLY);
|
|
|
a4b897 |
+ if (fd < 0) {
|
|
|
a4b897 |
+ perror("open(\"/proc/self/ns/net\")");
|
|
|
a4b897 |
+ exit(1);
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ addattr32(&req.n, 1024, NETNSA_FD, fd);
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ if (rtnl_send(&rth, &req.n, req.n.nlmsg_len) < 0) {
|
|
|
a4b897 |
+ perror("request send failed");
|
|
|
a4b897 |
+ exit(1);
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
+ rtnl_listen(&rth, ipnetns_accept_msg, NULL);
|
|
|
a4b897 |
+ close(fd);
|
|
|
a4b897 |
}
|
|
|
a4b897 |
- closedir(dir);
|
|
|
a4b897 |
- return 0;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ return have_rtnl_getnsid;
|
|
|
a4b897 |
}
|
|
|
a4b897 |
|
|
|
a4b897 |
-static void bind_etc(const char *name)
|
|
|
a4b897 |
+static int get_netnsid_from_name(const char *name)
|
|
|
a4b897 |
+{
|
|
|
a4b897 |
+ struct {
|
|
|
a4b897 |
+ struct nlmsghdr n;
|
|
|
a4b897 |
+ struct rtgenmsg g;
|
|
|
a4b897 |
+ char buf[1024];
|
|
|
a4b897 |
+ } req, answer;
|
|
|
a4b897 |
+ struct rtattr *tb[NETNSA_MAX + 1];
|
|
|
a4b897 |
+ struct rtgenmsg *rthdr;
|
|
|
a4b897 |
+ int len, fd;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ memset(&req, 0, sizeof(req));
|
|
|
a4b897 |
+ req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
|
|
|
a4b897 |
+ req.n.nlmsg_flags = NLM_F_REQUEST;
|
|
|
a4b897 |
+ req.n.nlmsg_type = RTM_GETNSID;
|
|
|
a4b897 |
+ req.g.rtgen_family = AF_UNSPEC;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ fd = netns_get_fd(name);
|
|
|
a4b897 |
+ if (fd < 0)
|
|
|
a4b897 |
+ return fd;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ addattr32(&req.n, 1024, NETNSA_FD, fd);
|
|
|
a4b897 |
+ if (rtnl_talk(&rth, &req.n, 0, 0, &answer.n) < 0) {
|
|
|
a4b897 |
+ close(fd);
|
|
|
a4b897 |
+ return -2;
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
+ close(fd);
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ /* Validate message and parse attributes */
|
|
|
a4b897 |
+ if (answer.n.nlmsg_type == NLMSG_ERROR)
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ rthdr = NLMSG_DATA(&answer.n);
|
|
|
a4b897 |
+ len = answer.n.nlmsg_len - NLMSG_SPACE(sizeof(*rthdr));
|
|
|
a4b897 |
+ if (len < 0)
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ parse_rtattr(tb, NETNSA_MAX, NETNS_RTA(rthdr), len);
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ if (tb[NETNSA_NSID])
|
|
|
a4b897 |
+ return rta_getattr_u32(tb[NETNSA_NSID]);
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+}
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+static int netns_list(int argc, char **argv)
|
|
|
a4b897 |
{
|
|
|
a4b897 |
- char etc_netns_path[MAXPATHLEN];
|
|
|
a4b897 |
- char netns_name[MAXPATHLEN];
|
|
|
a4b897 |
- char etc_name[MAXPATHLEN];
|
|
|
a4b897 |
struct dirent *entry;
|
|
|
a4b897 |
DIR *dir;
|
|
|
a4b897 |
+ int id;
|
|
|
a4b897 |
|
|
|
a4b897 |
- snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", NETNS_ETC_DIR, name);
|
|
|
a4b897 |
- dir = opendir(etc_netns_path);
|
|
|
a4b897 |
+ dir = opendir(NETNS_RUN_DIR);
|
|
|
a4b897 |
if (!dir)
|
|
|
a4b897 |
- return;
|
|
|
a4b897 |
+ return 0;
|
|
|
a4b897 |
|
|
|
a4b897 |
while ((entry = readdir(dir)) != NULL) {
|
|
|
a4b897 |
if (strcmp(entry->d_name, ".") == 0)
|
|
|
a4b897 |
continue;
|
|
|
a4b897 |
if (strcmp(entry->d_name, "..") == 0)
|
|
|
a4b897 |
continue;
|
|
|
a4b897 |
- snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path, entry->d_name);
|
|
|
a4b897 |
- snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name);
|
|
|
a4b897 |
- if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) {
|
|
|
a4b897 |
- fprintf(stderr, "Bind %s -> %s failed: %s\n",
|
|
|
a4b897 |
- netns_name, etc_name, strerror(errno));
|
|
|
a4b897 |
+ printf("%s", entry->d_name);
|
|
|
a4b897 |
+ if (ipnetns_have_nsid()) {
|
|
|
a4b897 |
+ id = get_netnsid_from_name(entry->d_name);
|
|
|
a4b897 |
+ if (id >= 0)
|
|
|
a4b897 |
+ printf(" (id: %d)", id);
|
|
|
a4b897 |
}
|
|
|
a4b897 |
+ printf("\n");
|
|
|
a4b897 |
}
|
|
|
a4b897 |
closedir(dir);
|
|
|
a4b897 |
+ return 0;
|
|
|
a4b897 |
}
|
|
|
a4b897 |
|
|
|
a4b897 |
-static int netns_exec(int argc, char **argv)
|
|
|
a4b897 |
+static int cmd_exec(const char *cmd, char **argv, bool do_fork)
|
|
|
a4b897 |
{
|
|
|
a4b897 |
- /* Setup the proper environment for apps that are not netns
|
|
|
a4b897 |
- * aware, and execute a program in that environment.
|
|
|
a4b897 |
- */
|
|
|
a4b897 |
- const char *name, *cmd;
|
|
|
a4b897 |
- char net_path[MAXPATHLEN];
|
|
|
a4b897 |
- int netns;
|
|
|
a4b897 |
-
|
|
|
a4b897 |
- if (argc < 1) {
|
|
|
a4b897 |
- fprintf(stderr, "No netns name specified\n");
|
|
|
a4b897 |
- return -1;
|
|
|
a4b897 |
- }
|
|
|
a4b897 |
- if (argc < 2) {
|
|
|
a4b897 |
- fprintf(stderr, "No command specified\n");
|
|
|
a4b897 |
- return -1;
|
|
|
a4b897 |
- }
|
|
|
a4b897 |
-
|
|
|
a4b897 |
- name = argv[0];
|
|
|
a4b897 |
- cmd = argv[1];
|
|
|
a4b897 |
- snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name);
|
|
|
a4b897 |
- netns = open(net_path, O_RDONLY | O_CLOEXEC);
|
|
|
a4b897 |
- if (netns < 0) {
|
|
|
a4b897 |
- fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
|
|
|
a4b897 |
- name, strerror(errno));
|
|
|
a4b897 |
- return -1;
|
|
|
a4b897 |
- }
|
|
|
a4b897 |
-
|
|
|
a4b897 |
- if (setns(netns, CLONE_NEWNET) < 0) {
|
|
|
a4b897 |
- fprintf(stderr, "seting the network namespace \"%s\" failed: %s\n",
|
|
|
a4b897 |
- name, strerror(errno));
|
|
|
a4b897 |
- return -1;
|
|
|
a4b897 |
- }
|
|
|
a4b897 |
-
|
|
|
a4b897 |
- if (unshare(CLONE_NEWNS) < 0) {
|
|
|
a4b897 |
- fprintf(stderr, "unshare failed: %s\n", strerror(errno));
|
|
|
a4b897 |
- return -1;
|
|
|
a4b897 |
- }
|
|
|
a4b897 |
- /* Don't let any mounts propogate back to the parent */
|
|
|
a4b897 |
- if (mount("", "/", "none", MS_SLAVE | MS_REC, NULL)) {
|
|
|
a4b897 |
- fprintf(stderr, "\"mount --make-rslave /\" failed: %s\n",
|
|
|
a4b897 |
- strerror(errno));
|
|
|
a4b897 |
- return -1;
|
|
|
a4b897 |
- }
|
|
|
a4b897 |
- /* Mount a version of /sys that describes the network namespace */
|
|
|
a4b897 |
- if (umount2("/sys", MNT_DETACH) < 0) {
|
|
|
a4b897 |
- fprintf(stderr, "umount of /sys failed: %s\n", strerror(errno));
|
|
|
a4b897 |
- return -1;
|
|
|
a4b897 |
- }
|
|
|
a4b897 |
- if (mount(name, "/sys", "sysfs", 0, NULL) < 0) {
|
|
|
a4b897 |
- fprintf(stderr, "mount of /sys failed: %s\n",strerror(errno));
|
|
|
a4b897 |
- return -1;
|
|
|
a4b897 |
- }
|
|
|
a4b897 |
-
|
|
|
a4b897 |
- /* Setup bind mounts for config files in /etc */
|
|
|
a4b897 |
- bind_etc(name);
|
|
|
a4b897 |
-
|
|
|
a4b897 |
fflush(stdout);
|
|
|
a4b897 |
-
|
|
|
a4b897 |
- if (batch_mode) {
|
|
|
a4b897 |
+ if (do_fork) {
|
|
|
a4b897 |
int status;
|
|
|
a4b897 |
pid_t pid;
|
|
|
a4b897 |
|
|
|
a4b897 |
@@ -205,20 +176,57 @@ static int netns_exec(int argc, char **argv)
|
|
|
a4b897 |
exit(1);
|
|
|
a4b897 |
}
|
|
|
a4b897 |
|
|
|
a4b897 |
- /* If child failed, propogate status */
|
|
|
a4b897 |
- if (WIFEXITED(status))
|
|
|
a4b897 |
- exit(WEXITSTATUS(status));
|
|
|
a4b897 |
+ if (WIFEXITED(status)) {
|
|
|
a4b897 |
+ return WEXITSTATUS(status);
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
|
|
|
a4b897 |
- return 0;
|
|
|
a4b897 |
+ exit(1);
|
|
|
a4b897 |
}
|
|
|
a4b897 |
}
|
|
|
a4b897 |
|
|
|
a4b897 |
- if (execvp(cmd, argv + 1) < 0)
|
|
|
a4b897 |
+ if (execvp(cmd, argv) < 0)
|
|
|
a4b897 |
fprintf(stderr, "exec of \"%s\" failed: %s\n",
|
|
|
a4b897 |
- cmd, strerror(errno));
|
|
|
a4b897 |
+ cmd, strerror(errno));
|
|
|
a4b897 |
_exit(1);
|
|
|
a4b897 |
}
|
|
|
a4b897 |
|
|
|
a4b897 |
+static int on_netns_exec(char *nsname, void *arg)
|
|
|
a4b897 |
+{
|
|
|
a4b897 |
+ char **argv = arg;
|
|
|
a4b897 |
+ cmd_exec(argv[1], argv + 1, true);
|
|
|
a4b897 |
+ return 0;
|
|
|
a4b897 |
+}
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+static int netns_exec(int argc, char **argv)
|
|
|
a4b897 |
+{
|
|
|
a4b897 |
+ /* Setup the proper environment for apps that are not netns
|
|
|
a4b897 |
+ * aware, and execute a program in that environment.
|
|
|
a4b897 |
+ */
|
|
|
a4b897 |
+ const char *cmd;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ if (argc < 1 && !do_all) {
|
|
|
a4b897 |
+ fprintf(stderr, "No netns name specified\n");
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
+ if ((argc < 2 && !do_all) || (argc < 1 && do_all)) {
|
|
|
a4b897 |
+ fprintf(stderr, "No command specified\n");
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ if (do_all)
|
|
|
a4b897 |
+ return do_each_netns(on_netns_exec, --argv, 1);
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ if (netns_switch(argv[0]))
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ /* ip must return the status of the child,
|
|
|
a4b897 |
+ * but do_cmd() will add a minus to this,
|
|
|
a4b897 |
+ * so let's add another one here to cancel it.
|
|
|
a4b897 |
+ */
|
|
|
a4b897 |
+ cmd = argv[1];
|
|
|
a4b897 |
+ return -cmd_exec(cmd, argv + 1, !!batch_mode);
|
|
|
a4b897 |
+}
|
|
|
a4b897 |
+
|
|
|
a4b897 |
static int is_pid(const char *str)
|
|
|
a4b897 |
{
|
|
|
a4b897 |
int ch;
|
|
|
a4b897 |
@@ -282,7 +290,7 @@ static int netns_pids(int argc, char **argv)
|
|
|
a4b897 |
}
|
|
|
a4b897 |
closedir(dir);
|
|
|
a4b897 |
return 0;
|
|
|
a4b897 |
-
|
|
|
a4b897 |
+
|
|
|
a4b897 |
}
|
|
|
a4b897 |
|
|
|
a4b897 |
static int netns_identify(int argc, char **argv)
|
|
|
a4b897 |
@@ -295,19 +303,17 @@ static int netns_identify(int argc, char **argv)
|
|
|
a4b897 |
struct dirent *entry;
|
|
|
a4b897 |
|
|
|
a4b897 |
if (argc < 1) {
|
|
|
a4b897 |
- fprintf(stderr, "No pid specified\n");
|
|
|
a4b897 |
- return -1;
|
|
|
a4b897 |
- }
|
|
|
a4b897 |
- if (argc > 1) {
|
|
|
a4b897 |
+ pidstr = "self";
|
|
|
a4b897 |
+ } else if (argc > 1) {
|
|
|
a4b897 |
fprintf(stderr, "extra arguments specified\n");
|
|
|
a4b897 |
return -1;
|
|
|
a4b897 |
- }
|
|
|
a4b897 |
- pidstr = argv[0];
|
|
|
a4b897 |
-
|
|
|
a4b897 |
- if (!is_pid(pidstr)) {
|
|
|
a4b897 |
- fprintf(stderr, "Specified string '%s' is not a pid\n",
|
|
|
a4b897 |
- pidstr);
|
|
|
a4b897 |
- return -1;
|
|
|
a4b897 |
+ } else {
|
|
|
a4b897 |
+ pidstr = argv[0];
|
|
|
a4b897 |
+ if (!is_pid(pidstr)) {
|
|
|
a4b897 |
+ fprintf(stderr, "Specified string '%s' is not a pid\n",
|
|
|
a4b897 |
+ pidstr);
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
}
|
|
|
a4b897 |
|
|
|
a4b897 |
snprintf(net_path, sizeof(net_path), "/proc/%s/ns/net", pidstr);
|
|
|
a4b897 |
@@ -355,21 +361,14 @@ static int netns_identify(int argc, char **argv)
|
|
|
a4b897 |
}
|
|
|
a4b897 |
closedir(dir);
|
|
|
a4b897 |
return 0;
|
|
|
a4b897 |
-
|
|
|
a4b897 |
+
|
|
|
a4b897 |
}
|
|
|
a4b897 |
|
|
|
a4b897 |
-static int netns_delete(int argc, char **argv)
|
|
|
a4b897 |
+static int on_netns_del(char *nsname, void *arg)
|
|
|
a4b897 |
{
|
|
|
a4b897 |
- const char *name;
|
|
|
a4b897 |
char netns_path[MAXPATHLEN];
|
|
|
a4b897 |
|
|
|
a4b897 |
- if (argc < 1) {
|
|
|
a4b897 |
- fprintf(stderr, "No netns name specified\n");
|
|
|
a4b897 |
- return -1;
|
|
|
a4b897 |
- }
|
|
|
a4b897 |
-
|
|
|
a4b897 |
- name = argv[0];
|
|
|
a4b897 |
- snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
|
|
|
a4b897 |
+ snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, nsname);
|
|
|
a4b897 |
umount2(netns_path, MNT_DETACH);
|
|
|
a4b897 |
if (unlink(netns_path) < 0) {
|
|
|
a4b897 |
fprintf(stderr, "Cannot remove namespace file \"%s\": %s\n",
|
|
|
a4b897 |
@@ -379,6 +378,33 @@ static int netns_delete(int argc, char **argv)
|
|
|
a4b897 |
return 0;
|
|
|
a4b897 |
}
|
|
|
a4b897 |
|
|
|
a4b897 |
+static int netns_delete(int argc, char **argv)
|
|
|
a4b897 |
+{
|
|
|
a4b897 |
+ if (argc < 1 && !do_all) {
|
|
|
a4b897 |
+ fprintf(stderr, "No netns name specified\n");
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ if (do_all)
|
|
|
a4b897 |
+ return netns_foreach(on_netns_del, NULL);
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ return on_netns_del(argv[0], NULL);
|
|
|
a4b897 |
+}
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+static int create_netns_dir(void)
|
|
|
a4b897 |
+{
|
|
|
a4b897 |
+ /* Create the base netns directory if it doesn't exist */
|
|
|
a4b897 |
+ if (mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)) {
|
|
|
a4b897 |
+ if (errno != EEXIST) {
|
|
|
a4b897 |
+ fprintf(stderr, "mkdir %s failed: %s\n",
|
|
|
a4b897 |
+ NETNS_RUN_DIR, strerror(errno));
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ return 0;
|
|
|
a4b897 |
+}
|
|
|
a4b897 |
+
|
|
|
a4b897 |
static int netns_add(int argc, char **argv)
|
|
|
a4b897 |
{
|
|
|
a4b897 |
/* This function creates a new network namespace and
|
|
|
a4b897 |
@@ -402,10 +428,10 @@ static int netns_add(int argc, char **argv)
|
|
|
a4b897 |
|
|
|
a4b897 |
snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
|
|
|
a4b897 |
|
|
|
a4b897 |
- /* Create the base netns directory if it doesn't exist */
|
|
|
a4b897 |
- mkdir(NETNS_RUN_DIR, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
|
|
|
a4b897 |
+ if (create_netns_dir())
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
|
|
|
a4b897 |
- /* Make it possible for network namespace mounts to propogate between
|
|
|
a4b897 |
+ /* Make it possible for network namespace mounts to propagate between
|
|
|
a4b897 |
* mount namespaces. This makes it likely that a unmounting a network
|
|
|
a4b897 |
* namespace file in one namespace will unmount the network namespace
|
|
|
a4b897 |
* file in all namespaces allowing the network namespace to be freed
|
|
|
a4b897 |
@@ -431,7 +457,7 @@ static int netns_add(int argc, char **argv)
|
|
|
a4b897 |
/* Create the filesystem state */
|
|
|
a4b897 |
fd = open(netns_path, O_RDONLY|O_CREAT|O_EXCL, 0);
|
|
|
a4b897 |
if (fd < 0) {
|
|
|
a4b897 |
- fprintf(stderr, "Cannot not create namespace file \"%s\": %s\n",
|
|
|
a4b897 |
+ fprintf(stderr, "Cannot create namespace file \"%s\": %s\n",
|
|
|
a4b897 |
netns_path, strerror(errno));
|
|
|
a4b897 |
return -1;
|
|
|
a4b897 |
}
|
|
|
a4b897 |
@@ -454,6 +480,61 @@ out_delete:
|
|
|
a4b897 |
return -1;
|
|
|
a4b897 |
}
|
|
|
a4b897 |
|
|
|
a4b897 |
+static int set_netnsid_from_name(const char *name, int nsid)
|
|
|
a4b897 |
+{
|
|
|
a4b897 |
+ struct {
|
|
|
a4b897 |
+ struct nlmsghdr n;
|
|
|
a4b897 |
+ struct rtgenmsg g;
|
|
|
a4b897 |
+ char buf[1024];
|
|
|
a4b897 |
+ } req;
|
|
|
a4b897 |
+ int fd, err = 0;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ memset(&req, 0, sizeof(req));
|
|
|
a4b897 |
+ req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg));
|
|
|
a4b897 |
+ req.n.nlmsg_flags = NLM_F_REQUEST;
|
|
|
a4b897 |
+ req.n.nlmsg_type = RTM_NEWNSID;
|
|
|
a4b897 |
+ req.g.rtgen_family = AF_UNSPEC;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ fd = netns_get_fd(name);
|
|
|
a4b897 |
+ if (fd < 0)
|
|
|
a4b897 |
+ return fd;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ addattr32(&req.n, 1024, NETNSA_FD, fd);
|
|
|
a4b897 |
+ addattr32(&req.n, 1024, NETNSA_NSID, nsid);
|
|
|
a4b897 |
+ if (rtnl_talk(&rth, &req.n, 0, 0, NULL) < 0)
|
|
|
a4b897 |
+ err = -2;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ close(fd);
|
|
|
a4b897 |
+ return err;
|
|
|
a4b897 |
+}
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+static int netns_set(int argc, char **argv)
|
|
|
a4b897 |
+{
|
|
|
a4b897 |
+ char netns_path[MAXPATHLEN];
|
|
|
a4b897 |
+ const char *name;
|
|
|
a4b897 |
+ int netns, nsid;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ if (argc < 1) {
|
|
|
a4b897 |
+ fprintf(stderr, "No netns name specified\n");
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
+ if (argc < 2) {
|
|
|
a4b897 |
+ fprintf(stderr, "No nsid specified\n");
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
+ name = argv[0];
|
|
|
a4b897 |
+ nsid = atoi(argv[1]);
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ snprintf(netns_path, sizeof(netns_path), "%s/%s", NETNS_RUN_DIR, name);
|
|
|
a4b897 |
+ netns = open(netns_path, O_RDONLY | O_CLOEXEC);
|
|
|
a4b897 |
+ if (netns < 0) {
|
|
|
a4b897 |
+ fprintf(stderr, "Cannot open network namespace \"%s\": %s\n",
|
|
|
a4b897 |
+ name, strerror(errno));
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+ }
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ return set_netnsid_from_name(name, nsid);
|
|
|
a4b897 |
+}
|
|
|
a4b897 |
|
|
|
a4b897 |
static int netns_monitor(int argc, char **argv)
|
|
|
a4b897 |
{
|
|
|
a4b897 |
@@ -466,6 +547,10 @@ static int netns_monitor(int argc, char **argv)
|
|
|
a4b897 |
strerror(errno));
|
|
|
a4b897 |
return -1;
|
|
|
a4b897 |
}
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+ if (create_netns_dir())
|
|
|
a4b897 |
+ return -1;
|
|
|
a4b897 |
+
|
|
|
a4b897 |
if (inotify_add_watch(fd, NETNS_RUN_DIR, IN_CREATE | IN_DELETE) < 0) {
|
|
|
a4b897 |
fprintf(stderr, "inotify_add_watch failed: %s\n",
|
|
|
a4b897 |
strerror(errno));
|
|
|
a4b897 |
@@ -505,6 +590,9 @@ int do_netns(int argc, char **argv)
|
|
|
a4b897 |
if (matches(*argv, "add") == 0)
|
|
|
a4b897 |
return netns_add(argc-1, argv+1);
|
|
|
a4b897 |
|
|
|
a4b897 |
+ if (matches(*argv, "set") == 0)
|
|
|
a4b897 |
+ return netns_set(argc-1, argv+1);
|
|
|
a4b897 |
+
|
|
|
a4b897 |
if (matches(*argv, "delete") == 0)
|
|
|
a4b897 |
return netns_delete(argc-1, argv+1);
|
|
|
a4b897 |
|
|
|
a4b897 |
diff --git a/man/man8/ip-netns.8 b/man/man8/ip-netns.8
|
|
|
a4b897 |
index 6aa6e93..80a4ad1 100644
|
|
|
a4b897 |
--- a/man/man8/ip-netns.8
|
|
|
a4b897 |
+++ b/man/man8/ip-netns.8
|
|
|
a4b897 |
@@ -16,20 +16,28 @@ ip-netns \- process network namespace management
|
|
|
a4b897 |
.BR "ip netns" " { " list " } "
|
|
|
a4b897 |
|
|
|
a4b897 |
.ti -8
|
|
|
a4b897 |
-.BR "ip netns" " { " add " | " delete " } "
|
|
|
a4b897 |
+.B ip netns add
|
|
|
a4b897 |
.I NETNSNAME
|
|
|
a4b897 |
|
|
|
a4b897 |
.ti -8
|
|
|
a4b897 |
+.B ip [-all] netns del
|
|
|
a4b897 |
+.RI "[ " NETNSNAME " ]"
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+.ti -8
|
|
|
a4b897 |
+.BR "ip netns" " { " set " } "
|
|
|
a4b897 |
+.I NETNSNAME NETNSID
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+.ti -8
|
|
|
a4b897 |
.BR "ip netns identify"
|
|
|
a4b897 |
-.I PID
|
|
|
a4b897 |
+.RI "[ " PID " ]"
|
|
|
a4b897 |
|
|
|
a4b897 |
.ti -8
|
|
|
a4b897 |
.BR "ip netns pids"
|
|
|
a4b897 |
.I NETNSNAME
|
|
|
a4b897 |
|
|
|
a4b897 |
.ti -8
|
|
|
a4b897 |
-.BR "ip netns exec "
|
|
|
a4b897 |
-.I NETNSNAME command ...
|
|
|
a4b897 |
+.BR "ip [-all] netns exec "
|
|
|
a4b897 |
+.RI "[ " NETNSNAME " ] " command ...
|
|
|
a4b897 |
|
|
|
a4b897 |
.ti -8
|
|
|
a4b897 |
.BR "ip netns monitor"
|
|
|
a4b897 |
@@ -38,12 +46,15 @@ ip-netns \- process network namespace management
|
|
|
a4b897 |
A network namespace is logically another copy of the network stack,
|
|
|
a4b897 |
with its own routes, firewall rules, and network devices.
|
|
|
a4b897 |
|
|
|
a4b897 |
+By default a process inherits its network namespace from its parent. Initially all
|
|
|
a4b897 |
+the processes share the same default network namespace from the init process.
|
|
|
a4b897 |
+
|
|
|
a4b897 |
By convention a named network namespace is an object at
|
|
|
a4b897 |
.BR "/var/run/netns/" NAME
|
|
|
a4b897 |
-that can be opened. The file descriptor resulting from opening
|
|
|
a4b897 |
+that can be opened. The file descriptor resulting from opening
|
|
|
a4b897 |
.BR "/var/run/netns/" NAME
|
|
|
a4b897 |
-refers to the specified network namespace. Holding that file
|
|
|
a4b897 |
-descriptor open keeps the network namespace alive. The file
|
|
|
a4b897 |
+refers to the specified network namespace. Holding that file
|
|
|
a4b897 |
+descriptor open keeps the network namespace alive. The file
|
|
|
a4b897 |
descriptor can be used with the
|
|
|
a4b897 |
.B setns(2)
|
|
|
a4b897 |
system call to change the network namespace associated with a task.
|
|
|
a4b897 |
@@ -76,19 +87,64 @@ If NAME is available in /var/run/netns/ this command creates a new
|
|
|
a4b897 |
network namespace and assigns NAME.
|
|
|
a4b897 |
|
|
|
a4b897 |
.TP
|
|
|
a4b897 |
-.B ip netns delete NAME - delete the name of a network namespace
|
|
|
a4b897 |
+.B ip [-all] netns delete [ NAME ] - delete the name of a network namespace(s)
|
|
|
a4b897 |
.sp
|
|
|
a4b897 |
If NAME is present in /var/run/netns it is umounted and the mount
|
|
|
a4b897 |
-point is removed. If this is the last user of the network namespace the
|
|
|
a4b897 |
-network namespace will be freed, otherwise the network namespace
|
|
|
a4b897 |
-persists until it has no more users. ip netns delete may fail if
|
|
|
a4b897 |
-the mount point is in use in another mount namespace.
|
|
|
a4b897 |
+point is removed. If this is the last user of the network namespace the
|
|
|
a4b897 |
+network namespace will be freed and all physical devices will be moved to the
|
|
|
a4b897 |
+default one, otherwise the network namespace persists until it has no more
|
|
|
a4b897 |
+users. ip netns delete may fail if the mount point is in use in another mount
|
|
|
a4b897 |
+namespace.
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+If
|
|
|
a4b897 |
+.B -all
|
|
|
a4b897 |
+option was specified then all the network namespace names will be removed.
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+It is possible to lose the physical device when it was moved to netns and
|
|
|
a4b897 |
+then this netns was deleted with a running process:
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+.RS 10
|
|
|
a4b897 |
+$ ip netns add net0
|
|
|
a4b897 |
+.RE
|
|
|
a4b897 |
+.RS 10
|
|
|
a4b897 |
+$ ip link set dev eth0 netns net0
|
|
|
a4b897 |
+.RE
|
|
|
a4b897 |
+.RS 10
|
|
|
a4b897 |
+$ ip netns exec net0 SOME_PROCESS_IN_BACKGROUND
|
|
|
a4b897 |
+.RE
|
|
|
a4b897 |
+.RS 10
|
|
|
a4b897 |
+$ ip netns del net0
|
|
|
a4b897 |
+.RE
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+.RS
|
|
|
a4b897 |
+and eth0 will appear in the default netns only after SOME_PROCESS_IN_BACKGROUND
|
|
|
a4b897 |
+will exit or will be killed. To prevent this the processes running in net0
|
|
|
a4b897 |
+should be killed before deleting the netns:
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+.RE
|
|
|
a4b897 |
+.RS 10
|
|
|
a4b897 |
+$ ip netns pids net0 | xargs kill
|
|
|
a4b897 |
+.RE
|
|
|
a4b897 |
+.RS 10
|
|
|
a4b897 |
+$ ip netns del net0
|
|
|
a4b897 |
+.RE
|
|
|
a4b897 |
|
|
|
a4b897 |
.TP
|
|
|
a4b897 |
-.B ip netns identify PID - Report network namespaces names for process
|
|
|
a4b897 |
+.B ip netns set NAME NETNSID - assign an id to a peer network namespace
|
|
|
a4b897 |
+.sp
|
|
|
a4b897 |
+This command assigns a id to a peer network namespace. This id is valid
|
|
|
a4b897 |
+only in the current network namespace.
|
|
|
a4b897 |
+This id will be used by the kernel in some netlink messages. If no id is
|
|
|
a4b897 |
+assigned when the kernel needs it, it will be automatically assigned by
|
|
|
a4b897 |
+the kernel.
|
|
|
a4b897 |
+Once it is assigned, it's not possible to change it.
|
|
|
a4b897 |
+
|
|
|
a4b897 |
+.TP
|
|
|
a4b897 |
+.B ip netns identify [PID] - Report network namespaces names for process
|
|
|
a4b897 |
.sp
|
|
|
a4b897 |
This command walks through /var/run/netns and finds all the network
|
|
|
a4b897 |
-namespace names for network namespace of the specified process.
|
|
|
a4b897 |
+namespace names for network namespace of the specified process, if PID is
|
|
|
a4b897 |
+not specified then the current process will be used.
|
|
|
a4b897 |
|
|
|
a4b897 |
.TP
|
|
|
a4b897 |
.B ip netns pids NAME - Report processes in the named network namespace
|
|
|
a4b897 |
@@ -97,15 +153,25 @@ This command walks through proc and finds all of the process who have
|
|
|
a4b897 |
the named network namespace as their primary network namespace.
|
|
|
a4b897 |
|
|
|
a4b897 |
.TP
|
|
|
a4b897 |
-.B ip netns exec NAME cmd ... - Run cmd in the named network namespace
|
|
|
a4b897 |
+.B ip [-all] netns exec [ NAME ] cmd ... - Run cmd in the named network namespace
|
|
|
a4b897 |
.sp
|
|
|
a4b897 |
This command allows applications that are network namespace unaware
|
|
|
a4b897 |
to be run in something other than the default network namespace with
|
|
|
a4b897 |
all of the configuration for the specified network namespace appearing
|
|
|
a4b897 |
-in the customary global locations. A network namespace and bind mounts
|
|
|
a4b897 |
+in the customary global locations. A network namespace and bind mounts
|
|
|
a4b897 |
are used to move files from their network namespace specific location
|
|
|
a4b897 |
to their default locations without affecting other processes.
|
|
|
a4b897 |
|
|
|
a4b897 |
+If
|
|
|
a4b897 |
+.B -all
|
|
|
a4b897 |
+option was specified then
|
|
|
a4b897 |
+.B cmd
|
|
|
a4b897 |
+will be executed synchronously on the each named network namespace even if
|
|
|
a4b897 |
+.B cmd
|
|
|
a4b897 |
+fails on some of them. Network namespace name is printed on each
|
|
|
a4b897 |
+.B cmd
|
|
|
a4b897 |
+executing.
|
|
|
a4b897 |
+
|
|
|
a4b897 |
.TP
|
|
|
a4b897 |
.B ip netns monitor - Report as network namespace names are added and deleted
|
|
|
a4b897 |
.sp
|
|
|
a4b897 |
--- iproute2-3.10.0/man/man8/ip.8.orig 2015-07-08 19:15:58.468148060 +0200
|
|
|
a4b897 |
+++ iproute2-3.10.0/man/man8/ip.8 2015-07-08 19:16:38.078147665 +0200
|
|
|
a4b897 |
@@ -132,7 +132,7 @@
|
|
|
a4b897 |
host addresses.
|
|
|
a4b897 |
|
|
|
a4b897 |
.TP
|
|
|
a4b897 |
-.BR "\-n" , " \-net" , " \-netns " <NETNS>
|
|
|
a4b897 |
+.BR "\-n" , " \-netns " <NETNS>
|
|
|
a4b897 |
switches
|
|
|
a4b897 |
.B ip
|
|
|
a4b897 |
to the specified network namespace
|