Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1257568pxb; Wed, 10 Feb 2021 04:16:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJwe0xAMQaIPA78eNxF4OQ08SANHugfbavxwtaYnYv+mYQb52sKFGUgKW00uDzgXpSFT9iK9 X-Received: by 2002:aa7:c895:: with SMTP id p21mr2872526eds.165.1612959371425; Wed, 10 Feb 2021 04:16:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1612959371; cv=none; d=google.com; s=arc-20160816; b=RNJ8Hrg4u8RcGzYsOYkYtmmNikbe9q7sXhoZRqlZWHBhGvWN0Moy8kQlJrwQuXIQof QPU1rtzHUEKbWdYNYS6pXCE+znazDHYLLvyiFdPjhJyvqBDrhxw/gMMvCle2mg+4kT4k dDxvbZkRhlAjt/BD6uvjeBITlN7hQfgsERvgFHN7LTkJ4i3CdP6oKHDeP5jQp1pju6YC 7namEEek/OMw+TgmjrxCwPS7Z9KIrjD/tabOvi5DPFSDrcB1+dPd0Vre2uuB8yfcu+CX AE2FhiWghitx9XEiBLSfzzwLqYkkg3+9ZFi21Pm1tie9/30Xq2lTIlrJV1aWvVPSfm9P vkBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7YlFkcwEzuK8xj1bqWFb4J+pbM9jNy7GJX0y3deB8As=; b=MHGJwPuNQUHTwj2A0xASc5H5CsN7jzhxjsCQjyWf960KVRPBJ8iyfuehskNqzOSNSj flmzH7m5DLq8GqPqHC3jtNf3rPOYYMa14DdU5eyKltz1aFDIdMPKErvICsVJGvg4LZ/T gt1x/Xuvo5Xy12ord+eR9hKWGp2xEqqKuwZEgJFFWG7eEetXm/LtexQnWhyDWwPh/8l+ CzpWxwVddabKHfgdCcZn2VxHWQJ35Pkfa44Okhqj4bIKgCEkgh85yZO7tEvA7pwv/n1O BHx4yQKBhNIGz7HB5FZiPS+cWj8m5DnxRl7zuP3dcwPa7LifxLu8HMW1oioIJSiEsJHa iQSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=syPwiZlF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h22si1133062eje.389.2021.02.10.04.15.47; Wed, 10 Feb 2021 04:16:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@cloudflare.com header.s=google header.b=syPwiZlF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=cloudflare.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231271AbhBJMNN (ORCPT + 99 others); Wed, 10 Feb 2021 07:13:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231666AbhBJMKb (ORCPT ); Wed, 10 Feb 2021 07:10:31 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAECDC0611C3 for ; Wed, 10 Feb 2021 04:04:39 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id j21so1637079wmj.0 for ; Wed, 10 Feb 2021 04:04:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cloudflare.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7YlFkcwEzuK8xj1bqWFb4J+pbM9jNy7GJX0y3deB8As=; b=syPwiZlFCfO6MHc40aH33LzcMM6KfDmzRv+D2pLSH5QhqhfJeAI8pcSdpGXIuefrhT QANrLqzWqVKdKDRMI9LkGu7/nHBiH+rC1ieNTWfFHSmkspO5VBaxfKVXb2X2Squ8dPwc gqbcZAWKlW0ioCr6UjiF2eRNBO1Q7kMUruhv0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7YlFkcwEzuK8xj1bqWFb4J+pbM9jNy7GJX0y3deB8As=; b=aTBqvzAM3Vbd0gEvoxOVleiowZTvbGvAZ0lao7wlJXvEd+95PwyPVyXkizbgkLFLZl rRoCRNOeaQBYMFssJLZVeaDGqq0rHg4lTFmGe1pRVNWfVqO5Bhk7a8C0SWCeoKHgCX5X cG7heap47+OQCM8u1bX6ODCu7RskmSRjabg2+LWd0Uvg4GTxaP85YOqgMOL9934SakR9 YNUFkTN9F34lZ7xA5cBwZHO7J6dzQC3/Oi/YikQfqKgvb/Z7GRVMaUevjPd698tK0cNC RB3U+CiUfJBcAGaJavuNNT0VVST3CBXwoTFl6lKHT+nEWZ1/T3nvwGVGiuU8pb60tkdR KOgA== X-Gm-Message-State: AOAM533wmBnNEuVkJAkEA1dmMAX0tCCn5Gjrjh6cjhPw8ozSHQj0gXmM CWcjisjV8BIlSGR1gRk9B8tZAw== X-Received: by 2002:a1c:f70c:: with SMTP id v12mr2614677wmh.77.1612958678358; Wed, 10 Feb 2021 04:04:38 -0800 (PST) Received: from antares.lan (c.3.c.9.d.d.c.e.0.a.6.8.a.9.e.c.f.f.6.2.a.5.a.7.0.b.8.0.1.0.0.2.ip6.arpa. [2001:8b0:7a5a:26ff:ce9a:86a0:ecdd:9c3c]) by smtp.gmail.com with ESMTPSA id j7sm2837854wrp.72.2021.02.10.04.04.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Feb 2021 04:04:38 -0800 (PST) From: Lorenz Bauer To: Richard Henderson , Ivan Kokshaysky , Matt Turner , Thomas Bogendoerfer , "James E.J. Bottomley" , Helge Deller , "David S. Miller" , Arnd Bergmann , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Jakub Kicinski Cc: kernel-team@cloudflare.com, Lorenz Bauer , linux-api@vger.kernel.org, linux-alpha@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, sparclinux@vger.kernel.org, linux-arch@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH bpf 1/4] net: add SO_NETNS_COOKIE socket option Date: Wed, 10 Feb 2021 12:04:22 +0000 Message-Id: <20210210120425.53438-2-lmb@cloudflare.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210210120425.53438-1-lmb@cloudflare.com> References: <20210210120425.53438-1-lmb@cloudflare.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We need to distinguish which network namespace a socket belongs to. BPF has the useful bpf_get_netns_cookie helper for this, but accessing it from user space isn't possible. Add a read-only socket option that returns the netns cookie, similar to SO_COOKIE. If network namespaces are disabled, SO_NETNS_COOKIE returns the cookie of init_net. The BPF helpers change slightly: instead of returning 0 when network namespaces are disabled we return the init_net cookie as for the socket option. Cc: linux-api@vger.kernel.org Signed-off-by: Lorenz Bauer --- arch/alpha/include/uapi/asm/socket.h | 2 ++ arch/mips/include/uapi/asm/socket.h | 2 ++ arch/parisc/include/uapi/asm/socket.h | 2 ++ arch/sparc/include/uapi/asm/socket.h | 2 ++ include/linux/sock_diag.h | 20 ++++++++++++++++++++ include/uapi/asm-generic/socket.h | 2 ++ net/core/filter.c | 9 ++++----- net/core/sock.c | 7 +++++++ 8 files changed, 41 insertions(+), 5 deletions(-) diff --git a/arch/alpha/include/uapi/asm/socket.h b/arch/alpha/include/uapi/asm/socket.h index 57420356ce4c..6b3daba60987 100644 --- a/arch/alpha/include/uapi/asm/socket.h +++ b/arch/alpha/include/uapi/asm/socket.h @@ -127,6 +127,8 @@ #define SO_PREFER_BUSY_POLL 69 #define SO_BUSY_POLL_BUDGET 70 +#define SO_NETNS_COOKIE 71 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 diff --git a/arch/mips/include/uapi/asm/socket.h b/arch/mips/include/uapi/asm/socket.h index 2d949969313b..cdf404a831b2 100644 --- a/arch/mips/include/uapi/asm/socket.h +++ b/arch/mips/include/uapi/asm/socket.h @@ -138,6 +138,8 @@ #define SO_PREFER_BUSY_POLL 69 #define SO_BUSY_POLL_BUDGET 70 +#define SO_NETNS_COOKIE 71 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 diff --git a/arch/parisc/include/uapi/asm/socket.h b/arch/parisc/include/uapi/asm/socket.h index f60904329bbc..5b5351cdcb33 100644 --- a/arch/parisc/include/uapi/asm/socket.h +++ b/arch/parisc/include/uapi/asm/socket.h @@ -119,6 +119,8 @@ #define SO_PREFER_BUSY_POLL 0x4043 #define SO_BUSY_POLL_BUDGET 0x4044 +#define SO_NETNS_COOKIE 0x4045 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 diff --git a/arch/sparc/include/uapi/asm/socket.h b/arch/sparc/include/uapi/asm/socket.h index 848a22fbac20..ff79db753dce 100644 --- a/arch/sparc/include/uapi/asm/socket.h +++ b/arch/sparc/include/uapi/asm/socket.h @@ -120,6 +120,8 @@ #define SO_PREFER_BUSY_POLL 0x0048 #define SO_BUSY_POLL_BUDGET 0x0049 +#define SO_NETNS_COOKIE 0x004a + #if !defined(__KERNEL__) diff --git a/include/linux/sock_diag.h b/include/linux/sock_diag.h index 0b9ecd8cf979..6e88436097b1 100644 --- a/include/linux/sock_diag.h +++ b/include/linux/sock_diag.h @@ -38,6 +38,26 @@ static inline u64 sock_gen_cookie(struct sock *sk) return cookie; } +static inline u64 __sock_gen_netns_cookie(struct sock *sk) +{ +#ifdef CONFIG_NET_NS + return __net_gen_cookie(sk->sk_net.net); +#else + return __net_gen_cookie(&init_net); +#endif +} + +static inline u64 sock_gen_netns_cookie(struct sock *sk) +{ + u64 cookie; + + preempt_disable(); + cookie = __sock_gen_netns_cookie(sk); + preempt_enable(); + + return cookie; +} + int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie); void sock_diag_save_cookie(struct sock *sk, __u32 *cookie); diff --git a/include/uapi/asm-generic/socket.h b/include/uapi/asm-generic/socket.h index 4dcd13d097a9..d588c244ec2f 100644 --- a/include/uapi/asm-generic/socket.h +++ b/include/uapi/asm-generic/socket.h @@ -122,6 +122,8 @@ #define SO_PREFER_BUSY_POLL 69 #define SO_BUSY_POLL_BUDGET 70 +#define SO_NETNS_COOKIE 71 + #if !defined(__KERNEL__) #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__)) diff --git a/net/core/filter.c b/net/core/filter.c index e15d4741719a..51f47b6913f1 100644 --- a/net/core/filter.c +++ b/net/core/filter.c @@ -4645,11 +4645,10 @@ static const struct bpf_func_proto bpf_get_socket_cookie_sock_ops_proto = { static u64 __bpf_get_netns_cookie(struct sock *sk) { -#ifdef CONFIG_NET_NS - return __net_gen_cookie(sk ? sk->sk_net.net : &init_net); -#else - return 0; -#endif + if (sk) + return __sock_gen_netns_cookie(sk); + + return __net_gen_cookie(&init_net); } BPF_CALL_1(bpf_get_netns_cookie_sock, struct sock *, ctx) diff --git a/net/core/sock.c b/net/core/sock.c index bbcd4b97eddd..2db201c210ca 100644 --- a/net/core/sock.c +++ b/net/core/sock.c @@ -1606,6 +1606,13 @@ int sock_getsockopt(struct socket *sock, int level, int optname, v.val = sk->sk_bound_dev_if; break; + case SO_NETNS_COOKIE: + lv = sizeof(u64); + if (len < lv) + return -EINVAL; + v.val64 = sock_gen_netns_cookie(sk); + break; + default: /* We implement the SO_SNDLOWAT etc to not be settable * (1003.1g 7). -- 2.27.0