Commit 14731c52 authored by Andrei Vagin's avatar Andrei Vagin

net: add a way to get a network namespace for a socket

Each sockets belongs to one network namespace and operates
in this network namespace.

socket_diag reports informations about sockets from
one network namespace, but it doesn't report sockets which
are not bound or connected to somewhere. So we need to have
a way to get network namespaces for such sockets.
Acked-by: 's avatarPavel Emelyanov <xemul@virtuozzo.com>
Signed-off-by: 's avatarAndrei Vagin <avagin@virtuozzo.com>
parent 37ea6ed0
......@@ -187,5 +187,6 @@ extern int __userns_call(const char *func_name, uns_call_t call, int flags,
extern int add_ns_shared_cb(int (*actor)(void *data), void *data);
extern struct ns_id *get_socket_ns(int lfd);
extern struct ns_id *lookup_ns_by_kid(unsigned int kid, struct ns_desc *nd);
#endif /* __CR_NS_H__ */
......@@ -324,7 +324,7 @@ int rst_add_ns_id(unsigned int id, struct pstree_item *i, struct ns_desc *nd)
return 0;
}
static struct ns_id *lookup_ns_by_kid(unsigned int kid, struct ns_desc *nd)
struct ns_id *lookup_ns_by_kid(unsigned int kid, struct ns_desc *nd)
{
struct ns_id *nsid;
......
......@@ -2290,11 +2290,31 @@ static struct ns_id *get_root_netns()
*/
struct ns_id *get_socket_ns(int lfd)
{
if (netns_nr == 1)
return get_root_netns();
struct ns_id *ns;
struct stat st;
int ns_fd;
pr_perror("Unable to get a socket net namespace");
return NULL;
ns_fd = ioctl(lfd, SIOCGSKNS);
if (ns_fd < 0) {
/* backward compatiblity with old kernels */
if (netns_nr == 1)
return get_root_netns();
pr_perror("Unable to get a socket net namespace");
return NULL;
}
if (fstat(ns_fd, &st)) {
pr_perror("Unable to stat a network namespace");
return NULL;
}
ns = lookup_ns_by_kid(st.st_ino, &net_ns_desc);
if (ns == NULL) {
pr_err("Unable to dump a socket from an external network namespace\n");
return NULL;
}
return ns;
}
int kerndat_socket_netns(void)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment