[CRIU] [PATCH 08/10] net: set a proper network namespace to create a socket

Andrei Vagin avagin at openvz.org
Wed Aug 31 15:55:31 PDT 2016


From: Andrei Vagin <avagin at virtuozzo.com>

Each socket has to be restored from a proper network namespaces
where it was created.

Signed-off-by: Andrei Vagin <avagin at virtuozzo.com>
---
 criu/files.c           |  2 ++
 criu/include/sockets.h |  1 +
 criu/sk-inet.c         |  3 +++
 criu/sk-netlink.c      |  3 +++
 criu/sk-packet.c       |  3 +++
 criu/sk-unix.c         |  5 +++++
 criu/sockets.c         | 35 +++++++++++++++++++++++++++++++++++
 7 files changed, 52 insertions(+)

diff --git a/criu/files.c b/criu/files.c
index 1aaee70..691c3bb 100644
--- a/criu/files.c
+++ b/criu/files.c
@@ -979,6 +979,8 @@ static int open_transport_fd(int pid, struct fdinfo_list_entry *fle)
 
 	pr_info("\t\tCreate transport fd %s\n", saddr.sun_path + 1);
 
+	if (root_item->ids && set_netns(root_item->ids->net_ns_id))
+		return -1;
 
 	sock = socket(PF_UNIX, SOCK_DGRAM, 0);
 	if (sock < 0) {
diff --git a/criu/include/sockets.h b/criu/include/sockets.h
index fc64ac2..78a41b1 100644
--- a/criu/include/sockets.h
+++ b/criu/include/sockets.h
@@ -94,6 +94,7 @@ static inline int sk_decode_shutdown(int val)
 #define SIOCGSKNS      0x894C          /* get socket network namespace */
 #endif
 
+extern int set_netns(uint32_t ns_id);
 extern int kerndat_socket_netns(void);
 
 #endif /* __CR_SOCKETS_H__ */
diff --git a/criu/sk-inet.c b/criu/sk-inet.c
index 8083c05..05623b5 100644
--- a/criu/sk-inet.c
+++ b/criu/sk-inet.c
@@ -584,6 +584,9 @@ static int open_inet_sk(struct file_desc *d)
 	if (inet_validate_address(ie))
 		return -1;
 
+	if (set_netns(ie->ns_id))
+		return -1;
+
 	sk = socket(ie->family, ie->type, ie->proto);
 	if (sk < 0) {
 		pr_perror("Can't create inet socket");
diff --git a/criu/sk-netlink.c b/criu/sk-netlink.c
index 252a484..ab06024 100644
--- a/criu/sk-netlink.c
+++ b/criu/sk-netlink.c
@@ -180,6 +180,9 @@ static int open_netlink_sk(struct file_desc *d)
 
 	pr_info("Opening netlink socket id %#x\n", nse->id);
 
+	if (set_netns(nse->ns_id))
+		return -1;
+
 	sk = socket(PF_NETLINK, SOCK_RAW, nse->protocol);
 	if (sk < 0) {
 		pr_perror("Can't create netlink sock");
diff --git a/criu/sk-packet.c b/criu/sk-packet.c
index d064f36..0d0ffd5 100644
--- a/criu/sk-packet.c
+++ b/criu/sk-packet.c
@@ -419,6 +419,9 @@ static int open_packet_sk(struct file_desc *d)
 
 	pr_info("Opening packet socket id %#x\n", pse->id);
 
+	if (set_netns(pse->ns_id))
+		return -1;
+
 	sk = socket(PF_PACKET, pse->type, pse->protocol);
 	if (sk < 0) {
 		pr_perror("Can't create packet sock");
diff --git a/criu/sk-unix.c b/criu/sk-unix.c
index 1b5d2aa..27097c0 100644
--- a/criu/sk-unix.c
+++ b/criu/sk-unix.c
@@ -1061,6 +1061,9 @@ static int open_unixsk_pair_master(struct unix_sk_info *ui)
 	pr_info("Opening pair master (id %#x ino %#x peer %#x)\n",
 			ui->ue->id, ui->ue->ino, ui->ue->peer);
 
+	if (set_netns(ui->ue->ns_id))
+		return -1;
+
 	if (socketpair(PF_UNIX, ui->ue->type, 0, sk) < 0) {
 		pr_perror("Can't make socketpair");
 		return -1;
@@ -1238,6 +1241,8 @@ static int open_unixsk_standalone(struct unix_sk_info *ui)
 			return -1;
 		}
 
+		if (set_netns(ui->ue->ns_id))
+			return -1;
 
 		sk = socket(PF_UNIX, ui->ue->type, 0);
 		if (sk < 0) {
diff --git a/criu/sockets.c b/criu/sockets.c
index edd459f..13987d6 100644
--- a/criu/sockets.c
+++ b/criu/sockets.c
@@ -1,3 +1,4 @@
+#include <sched.h>
 #include <unistd.h>
 #include <sys/socket.h>
 #include <linux/netlink.h>
@@ -21,6 +22,7 @@
 #include "namespaces.h"
 #include "net.h"
 #include "fs-magic.h"
+#include "pstree.h"
 
 #ifndef SOCK_DIAG_BY_FAMILY
 #define SOCK_DIAG_BY_FAMILY 20
@@ -740,3 +742,36 @@ int collect_sockets(struct ns_id *ns)
 
 	return err;
 }
+
+static uint32_t last_ns_id = 0;
+
+int set_netns(uint32_t ns_id)
+{
+	struct ns_id *ns;
+	int nsfd;
+
+	if (!(root_ns_mask & CLONE_NEWNET))
+		return 0;
+
+	if (ns_id == last_ns_id)
+		return 0;
+
+	ns = lookup_ns_by_id(ns_id, &net_ns_desc);
+	if (ns == NULL) {
+		pr_err("Unable to find a network namespace");
+		return -1;
+	}
+	nsfd = open_proc(root_item->pid.virt, "fd/%d", ns->net.ns_fd);
+	if (nsfd < 0)
+		return -1;
+	if (setns(nsfd, CLONE_NEWNET)) {
+		pr_perror("Unable to switch a network namespace");
+		close(nsfd);
+		return -1;
+	}
+	last_ns_id = ns_id;
+	close(nsfd);
+	close_pid_proc();
+
+	return 0;
+}
-- 
2.7.4



More information about the CRIU mailing list