From: Lorenz Bauer <lmb@cloudflare.com>
To: eric.dumazet@gmail.com, daniel@iogearbox.net, ast@kernel.org,
andrii@kernel.org
Cc: bpf@vger.kernel.org, netdev@vger.kernel.org,
kernel-team@cloudflare.com, Lorenz Bauer <lmb@cloudflare.com>
Subject: [PATCH bpf-next v3 2/4] nsfs: add an ioctl to discover the network namespace cookie
Date: Fri, 19 Feb 2021 15:43:28 +0000 [thread overview]
Message-ID: <20210219154330.93615-3-lmb@cloudflare.com> (raw)
In-Reply-To: <20210219154330.93615-1-lmb@cloudflare.com>
Network namespaces have a globally unique non-zero identifier aka a
cookie, in line with socket cookies. Add an ioctl to retrieve the
cookie from user space without going via BPF.
Signed-off-by: Lorenz Bauer <lmb@cloudflare.com>
---
fs/nsfs.c | 7 +++++++
include/uapi/linux/nsfs.h | 2 ++
2 files changed, 9 insertions(+)
diff --git a/fs/nsfs.c b/fs/nsfs.c
index 800c1d0eb0d0..48198a1b1685 100644
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -11,6 +11,7 @@
#include <linux/user_namespace.h>
#include <linux/nsfs.h>
#include <linux/uaccess.h>
+#include <net/net_namespace.h>
#include "internal.h"
@@ -191,6 +192,7 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
struct user_namespace *user_ns;
struct ns_common *ns = get_proc_ns(file_inode(filp));
uid_t __user *argp;
+ struct net *net_ns;
uid_t uid;
switch (ioctl) {
@@ -209,6 +211,11 @@ static long ns_ioctl(struct file *filp, unsigned int ioctl,
argp = (uid_t __user *) arg;
uid = from_kuid_munged(current_user_ns(), user_ns->owner);
return put_user(uid, argp);
+ case NS_GET_COOKIE:
+ if (ns->ops->type != CLONE_NEWNET)
+ return -EINVAL;
+ net_ns = container_of(ns, struct net, ns);
+ return put_user(net_ns->net_cookie, (u64 __user *)arg);
default:
return -ENOTTY;
}
diff --git a/include/uapi/linux/nsfs.h b/include/uapi/linux/nsfs.h
index a0c8552b64ee..86611c2cf908 100644
--- a/include/uapi/linux/nsfs.h
+++ b/include/uapi/linux/nsfs.h
@@ -15,5 +15,7 @@
#define NS_GET_NSTYPE _IO(NSIO, 0x3)
/* Get owner UID (in the caller's user namespace) for a user namespace */
#define NS_GET_OWNER_UID _IO(NSIO, 0x4)
+/* Returns a unique non-zero identifier for a network namespace */
+#define NS_GET_COOKIE _IO(NSIO, 0x5)
#endif /* __LINUX_NSFS_H */
--
2.27.0
next prev parent reply other threads:[~2021-02-19 15:45 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-02-19 15:43 [PATCH bpf-next v3 0/4] Expose network namespace cookies to user space Lorenz Bauer
2021-02-19 15:43 ` [PATCH bpf-next v3 1/4] net: add SO_NETNS_COOKIE socket option Lorenz Bauer
2021-02-19 15:43 ` Lorenz Bauer [this message]
2021-02-19 15:43 ` [PATCH bpf-next v3 3/4] tools/testing: add test for NS_GET_COOKIE Lorenz Bauer
2021-02-19 15:43 ` [PATCH bpf-next v3 4/4] tools/testing: add a selftest for SO_NETNS_COOKIE Lorenz Bauer
2021-03-12 9:57 ` [PATCH bpf-next v3 0/4] Expose network namespace cookies to user space Tony Lu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210219154330.93615-3-lmb@cloudflare.com \
--to=lmb@cloudflare.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=eric.dumazet@gmail.com \
--cc=kernel-team@cloudflare.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).