From patchwork Thu Aug 3 13:41:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geliang Tang X-Patchwork-Id: 13340068 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 20673EB64DD for ; Thu, 3 Aug 2023 13:44:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236413AbjHCNot (ORCPT ); Thu, 3 Aug 2023 09:44:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236416AbjHCNob (ORCPT ); Thu, 3 Aug 2023 09:44:31 -0400 Received: from EUR05-AM6-obe.outbound.protection.outlook.com (mail-am6eur05on2045.outbound.protection.outlook.com [40.107.22.45]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3FA8D44A1; Thu, 3 Aug 2023 06:41:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bXUNnsCQV0iSqJ75Jq7K+suRfbUtSktLrNAO8xNwLXO+ygGOmbuztXvq5r6xAoCp2KmF+LiSGXe7aOU7lsAvixGu0x+v8RdGcswELFIGhdgKwz6T5O1grNcmE+fttYGHmtn1QzhEV/WJqsdPwRI8HCeBR/ab26rHAVlE0nlfSVeIOu0w8zZbuSSlTTOm+klRZG4zNPSRFfz6h3GnqGx70t7ep/L+51Jfnrtr3T6WvN/2ZgszE5ZGbznftq8GjZOx8a7tzl8ORL3V9wNKm9Xg5EX6G44ZaSuOpKltmsXTRuc6zrKinMSzVhjUdggUhuFLKoT8NfQrPm2UAp5jNnYVCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=DD0kw2+0/ffqwVvqYQuyKv0x7CGp4nGi8nyUCwahJ5M=; b=OXdWovl0KgbMFS4s4HInC2ZBSLix5kOt5zG8ovVtCk8xzl6BCsBCUrg8jWqlk3ONyqY3hdD3rH6i6s6Eb/j8NzBNlzanijm71gD+PzBX6WEqVenOG21Ot8jiXstnT4dwMU5PoOS+rDEbV0ASJYA0p26gSjku8YXQDxMnQxTOxiBMaYztXi0xOIg0BIFa4mUqH3/YOpqmqZ+G/grqDGie2G2iiRrOzrw1mSEHMspKdbdFgYxm4VBJBqbObAYEMh8/pBgPjBj3vD8L8n6RXVbhM17Zn3Xd0amyn4+c9PKNYsLbUHym5XM0aljQ6QXAmokGbF1QtIf7+5N3YIWLHKELCA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=suse.com; dmarc=pass action=none header.from=suse.com; dkim=pass header.d=suse.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DD0kw2+0/ffqwVvqYQuyKv0x7CGp4nGi8nyUCwahJ5M=; b=2RJriGX74msZ353XAvRx+ytouBF/p+81nbcl05QQ++/gZ2YEVbql289i8GWVl+7J2VyXZi1xONvizVgfbguH7cfDiknLNCVnqFu6rwnHm9VnxuCqAhE+1sH3BYhjZxWwDwyjWUVhAiroSQQSX2pOsmgiEzQizlSjmEEk7mfv/IAAt30mzWwWdHOfcIWUg8uR72jAy0PZk613FtT7vEwUB9f8+1IPsMcB0fSJnh0lR/D7eydCrO0TVk4oEp9owKB+LP7zR9vBuqefNIrnudlnlnH0OYoRWjcQoAQgZFcXJdHZHUe4h78YWgq135izTkNZb++VT2mxbSPasXOLZHCE5g== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=suse.com; Received: from VI1PR0402MB3503.eurprd04.prod.outlook.com (2603:10a6:803:d::26) by AS8PR04MB8580.eurprd04.prod.outlook.com (2603:10a6:20b:427::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6631.47; Thu, 3 Aug 2023 13:41:47 +0000 Received: from VI1PR0402MB3503.eurprd04.prod.outlook.com ([fe80::6480:1775:c36d:b797]) by VI1PR0402MB3503.eurprd04.prod.outlook.com ([fe80::6480:1775:c36d:b797%5]) with mapi id 15.20.6631.046; Thu, 3 Aug 2023 13:41:47 +0000 From: Geliang Tang To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Florent Revest , Brendan Jackman , Matthieu Baerts , Mat Martineau , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , John Johansen , Paul Moore , James Morris , "Serge E. Hallyn" , Stephen Smalley , Eric Paris , Mykola Lysenko , Shuah Khan , Simon Horman Cc: Geliang Tang , bpf@vger.kernel.org, netdev@vger.kernel.org, mptcp@lists.linux.dev, apparmor@lists.ubuntu.com, linux-security-module@vger.kernel.org, selinux@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH bpf-next v9 1/4] bpf: Add update_socket_protocol hook Date: Thu, 3 Aug 2023 21:41:43 +0800 Message-Id: X-Mailer: git-send-email 2.35.3 In-Reply-To: References: X-ClientProxiedBy: SG2PR03CA0099.apcprd03.prod.outlook.com (2603:1096:4:7c::27) To VI1PR0402MB3503.eurprd04.prod.outlook.com (2603:10a6:803:d::26) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VI1PR0402MB3503:EE_|AS8PR04MB8580:EE_ X-MS-Office365-Filtering-Correlation-Id: 569c0b44-6c0f-4114-5db7-08db94276238 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: r6p/IXflHlT4gD7oJp13einpP78p25kze6EgC37WDaFYH8ilpl3x2o4FF6utntCZwQFoGeON/zopDCbW/E/9Th1Vj9Gi1L82UpWpAQkanrpsVpW0gdJo0yepha/kI74TjvwpTsk6fNwee85DTLJkFN6MIPNrASi7Bj4J3A0Onw02rPp0NzHD+VKlVz+NLydl1zosgc3h/ZtdJ+vHv7nw+nb8+E6s/43lP2TDmbLjGrodqZDqVJ4XrXmkdIBo/LdlQtzBrOs8LYdv23UH1fal6Mwk0ocbJ63gTXBAjApjw2DkrgqyFbVQfll9OSVqTF4+1+MlUHYbRM4Pba2fqhK3LD6kuq45LiI/q1e3Cglk1WcM3odgg00hDdfoeyMEbgbOZh8FM71R18JmMAmfURbe3OpYwxcLyp847oOp8n6AQTdgasuge26D/miHNyH4odpGItZskyog7QHnyLrwTeP0vFttyABvXgqPfQPKmmxwRc7K/KUgyx90Oi4yGuNSRerXUjn9ebbaEBfHyRXvQwIyhpDMcjwLg/LSjvUYSJH+cOXhqP1qQz+9/86LmWC2AVuj/GB+FlZ82GV+zuThcXZh4QGYtKc0T16DEkZ4L02QlKI+iym44Ytniwnu64Kl/7Bx X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VI1PR0402MB3503.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(39860400002)(136003)(346002)(396003)(366004)(376002)(451199021)(2616005)(83380400001)(6506007)(26005)(186003)(41300700001)(2906002)(66946007)(4326008)(66476007)(66556008)(5660300002)(316002)(44832011)(7416002)(8676002)(8936002)(6666004)(6486002)(15650500001)(966005)(6512007)(7406005)(478600001)(110136005)(38100700002)(921005)(86362001)(36756003)(13296009);DIR:OUT;SF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UR+PsZGpZAMq6OLNUYjOVX0xQimiycXUVnWpPDM1xiE+7zgsVMQ0y28R7UErNzTIA59Oks+XQXlXTpGcWixXqLRjrtGd6u60hR2yqHA1z5HBoTGk27t8mW7xH/mbNG6Rz1vnlRKBafSf1gFa/NEdohO61NwWM55Qdg4gokkTR3oxrtDEQHPXFniO9WOmfGR4Xemz7BwcJ30jE7zLpOf+vY5IenRFoRKKc8VdoCuKE2UOdrgsMwcvO67JqlxWj+OKxBFv8LmOuIpkfI4kYrQt8XGZM8nl2Egcdakh0iwWmZeCFd3f3QlPIyIYT/RaSkMyDRVCRw6bIfghD5fLbiRypeGAqpIyp+lnIpRas3ix4VGjuR30i3G7Mynh09lv1TA54hodlXTEzKDPQQ46SucK/qVmbjH9tRqF7Rwr9g8XrHsaw7vU7v0BoFLWWuW7wapnLO5yTnHGPJ6LI1zeQdB58yqf5C1KJsBFcByZYDT78O8MNeEszjxEv8CKHFJDtVvCet20vGuDVi1ylHkZUDdQNUa/jZDBPMeihP2iMC9kmaGb9KJ/nMBHyKSr9JazE9cDiZy6zh27 X-OriginatorOrg: suse.com X-MS-Exchange-CrossTenant-Network-Message-Id: 569c0b44-6c0f-4114-5db7-08db94276238 X-MS-Exchange-CrossTenant-AuthSource: VI1PR0402MB3503.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Aug 2023 13:41:47.8092 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: f7a17af6-1c5c-4a36-aa8b-f5be247aa4ba X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: lwJ8ovNZW1Z19b5fZYDs4twvOT9dIEIE0luDZM5MY6r9XbyCJsSzhMgOTLKnJQ/U5NbL/6XTx1Bb1Mo8q97mIQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR04MB8580 Precedence: bulk List-ID: Add a hook named update_socket_protocol in __sys_socket(), for bpf progs to attach to and update socket protocol. One user case is to force legacy TCP apps to create and use MPTCP sockets instead of TCP ones. Define a mod_ret set named bpf_mptcp_fmodret_ids, add the hook update_socket_protocol into this set, and register it in bpf_mptcp_kfunc_init(). Closes: https://github.com/multipath-tcp/mptcp_net-next/issues/79 Acked-by: Matthieu Baerts Signed-off-by: Geliang Tang --- net/mptcp/bpf.c | 17 +++++++++++++++++ net/socket.c | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/net/mptcp/bpf.c b/net/mptcp/bpf.c index 5a0a84ad94af..c43aee31014d 100644 --- a/net/mptcp/bpf.c +++ b/net/mptcp/bpf.c @@ -12,6 +12,23 @@ #include #include "protocol.h" +#ifdef CONFIG_BPF_JIT +BTF_SET8_START(bpf_mptcp_fmodret_ids) +BTF_ID_FLAGS(func, update_socket_protocol) +BTF_SET8_END(bpf_mptcp_fmodret_ids) + +static const struct btf_kfunc_id_set bpf_mptcp_fmodret_set = { + .owner = THIS_MODULE, + .set = &bpf_mptcp_fmodret_ids, +}; + +static int __init bpf_mptcp_kfunc_init(void) +{ + return register_btf_fmodret_id_set(&bpf_mptcp_fmodret_set); +} +late_initcall(bpf_mptcp_kfunc_init); +#endif /* CONFIG_BPF_JIT */ + struct mptcp_sock *bpf_mptcp_sock_from_subflow(struct sock *sk) { if (sk && sk_fullsock(sk) && sk->sk_protocol == IPPROTO_TCP && sk_is_mptcp(sk)) diff --git a/net/socket.c b/net/socket.c index 2b0e54b2405c..9f98ced88ac5 100644 --- a/net/socket.c +++ b/net/socket.c @@ -1644,11 +1644,35 @@ struct file *__sys_socket_file(int family, int type, int protocol) return sock_alloc_file(sock, flags, NULL); } +/* A hook for bpf progs to attach to and update socket protocol. + * + * A static noinline declaration here could cause the compiler to + * optimize away the function. A global noinline declaration will + * keep the definition, but may optimize away the callsite. + * Therefore, __weak is needed to ensure that the call is still + * emitted, by telling the compiler that we don't know what the + * function might eventually be. + * + * __diag_* below are needed to dismiss the missing prototype warning. + */ + +__diag_push(); +__diag_ignore_all("-Wmissing-prototypes", + "kfuncs which will be used in BPF programs"); + +__weak noinline int update_socket_protocol(int family, int type, int protocol) +{ + return protocol; +} + +__diag_pop(); + int __sys_socket(int family, int type, int protocol) { struct socket *sock; int flags; + protocol = update_socket_protocol(family, type, protocol); sock = __sys_socket_create(family, type, protocol); if (IS_ERR(sock)) return PTR_ERR(sock);