From patchwork Mon Feb 27 19:51:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 62123 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2617307wrd; Mon, 27 Feb 2023 12:02:47 -0800 (PST) X-Google-Smtp-Source: AK7set+9U2JazGJFmbbRJKs8J0lJ4IkSSBVcZUebCvObJI6rnnoLbqkuq6P2xlNzqS2677qJ6EH5 X-Received: by 2002:a17:906:6a24:b0:8ee:72c0:6c7a with SMTP id qw36-20020a1709066a2400b008ee72c06c7amr17600303ejc.58.1677528167193; Mon, 27 Feb 2023 12:02:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677528167; cv=none; d=google.com; s=arc-20160816; b=f1jm1v26DW/56E5yxhrpwnK6FdBRFIpIYOZazAiT1FswxosQMOlc/IRSGgfIx/YnLg TUkryYcoEboEK4mNtyha7f5ELI7uFq3N6xz6kKZ1uTAtUvL+kdifmKrE4E6pwNANgN+A gH9RymrtU88iIzOoBA9JzwKUt0/7J0Uqj2zgaT/3qOgEoznYCF9QB0hlteEBz81HVX3w eQKE+br45hSGlQT012yYW3Y//U8WNoNjNGDLqs4WYbjbjL0jEiOv2oU1yk3LexGGCD1L FHqiSeK56GzCvS7DlBYO0G4fX7HUYu2lN+bDCBU/d2iUfefdeuv1BorNDJOCiLC0gCrZ Q3FQ== 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 :feedback-id:dkim-signature:dkim-signature; bh=vqOPrU2USs/DBPvfuObiynBOOBEeIGTPg/9yydmeqQo=; b=V2BNtKa3TPihmjsNn1DQpnY4x8mHc3RMgyXNZ0M2emmvTAwWVaYs+BXRpSm/28Jyvt uoZ86eSd+TVt8kvVYsmiutyM3i/dqFgEr3WVpT4lxXQwOhkM8KIO9pyWG8uQRGY3Tm6c rgzEjosz8IOk928sJoFZ/LD/7JI1dG66sP9ugVeHTFgZDOZjTkFnSJwVljklw7gDP412 YLnnwSlIXwq/x71QXMLjtydC+Cn8rSPj63CKUbah3ZHk7lVSUQKK/glwzg0bIFuFcUgy FCKJ50u+K/mUJtKQg+X4ZG05CPg4OgjvqZuzp4iCq4pqpF8r7KhYtqui8KMNiSOxQN50 /MoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=Z4tJwqYA; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=AkgaaiB3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a11-20020aa7d74b000000b004ad7265c2easi8916740eds.200.2023.02.27.12.02.24; Mon, 27 Feb 2023 12:02:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=Z4tJwqYA; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=AkgaaiB3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230405AbjB0Twn (ORCPT + 99 others); Mon, 27 Feb 2023 14:52:43 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36896 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230371AbjB0Twi (ORCPT ); Mon, 27 Feb 2023 14:52:38 -0500 Received: from wout1-smtp.messagingengine.com (wout1-smtp.messagingengine.com [64.147.123.24]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46B6528862; Mon, 27 Feb 2023 11:52:08 -0800 (PST) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 3A8BB32000CC; Mon, 27 Feb 2023 14:52:07 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Mon, 27 Feb 2023 14:52:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to; s=fm3; t=1677527526; x=1677613926; bh=vq OPrU2USs/DBPvfuObiynBOOBEeIGTPg/9yydmeqQo=; b=Z4tJwqYA3DJV7CLlHF 7zSwAocfWfQRFbAqGuQoL5985PQMS5/0JARZfs3EXUuR9aFTrdRXq1Oo/NG0TQ0h X6eiWPj4ZSjaDnO2Ki7LDkbpTOjP8iezX6lptw/qMQ9l9UxVj82lzaLTubx+cIGS bCxCqpNoDAoTzwhJNXOCZK8L1i1BbGA16brgEWyvtimafxgXqqqLVYwqMjssjyef KJ27IJld+I/yiF61lRbOvSG18Xi/ZBZJO4kgLMVr9j821R9peqyuEwipqQda5XjY LHw8MxV7ra+m048qp/aNJcrAmk/yZY0GUa1H05nza1/EvNy6vtPuYdMK61DhaQev NKsA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; t=1677527526; x=1677613926; bh=vqOPrU2USs/DB PvfuObiynBOOBEeIGTPg/9yydmeqQo=; b=AkgaaiB3NcAsUn39bFP5FEK1KYKtC 0Srry3tuLsNTT5VGRt1UTw4FCSDipnYaCv5P5amMz8xm/2OvxBy+FsFk49V4roT+ 1O+yYA+1wq56WMAt15gQtE4nsLQOliDgft2FArQBC0L/hGnsaIDgTBliSKUOlPou Odw+LxCoze55Yk5df+46TxmmsnRrdJ8Jc/5gqgsKWDS4q3PNxl8esSGhEIVvxWq+ TpWfAawj/osjSm3h2378povMNUjEIL3JBg/kuDx/QL64FpkDdUYESdR9nZYaedKc 39G2JmkkOwICZWEGigQ3mJiSCrMmXplQg827LBJDP32Sg++p/fACDnxZQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudeltddguddvhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdejtddmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 27 Feb 2023 14:52:05 -0500 (EST) From: Daniel Xu To: kuba@kernel.org, edumazet@google.com, davem@davemloft.net, dsahern@kernel.org, pabeni@redhat.com Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH bpf-next v2 5/8] bpf: net: ipv6: Add bpf_ipv6_frag_rcv() kfunc Date: Mon, 27 Feb 2023 12:51:07 -0700 Message-Id: X-Mailer: git-send-email 2.39.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759015775336639712?= X-GMAIL-MSGID: =?utf-8?q?1759015775336639712?= This helper is used to defragment IPv6 packets. Similar to the previous bpf_ip_check_defrag() kfunc, this kfunc: * Returns 0 on defrag + skb update success * Returns < 0 on error * Takes care to ensure ctx (skb) remains valid no matter what the underlying call to _ipv6_frag_rcv() does * Is only callable from TC clsact progs Please see bpf_ip_check_defrag() commit for more details / suggestions. Signed-off-by: Daniel Xu --- include/net/ipv6_frag.h | 1 + include/net/transp_v6.h | 1 + net/ipv6/Makefile | 1 + net/ipv6/af_inet6.c | 4 ++ net/ipv6/reassembly_bpf.c | 143 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100644 net/ipv6/reassembly_bpf.c diff --git a/include/net/ipv6_frag.h b/include/net/ipv6_frag.h index 7321ffe3a108..cf4763cd3886 100644 --- a/include/net/ipv6_frag.h +++ b/include/net/ipv6_frag.h @@ -15,6 +15,7 @@ enum ip6_defrag_users { __IP6_DEFRAG_CONNTRACK_OUT = IP6_DEFRAG_CONNTRACK_OUT + USHRT_MAX, IP6_DEFRAG_CONNTRACK_BRIDGE_IN, __IP6_DEFRAG_CONNTRACK_BRIDGE_IN = IP6_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX, + IP6_DEFRAG_BPF, }; /* diff --git a/include/net/transp_v6.h b/include/net/transp_v6.h index d27b1caf3753..244123a74349 100644 --- a/include/net/transp_v6.h +++ b/include/net/transp_v6.h @@ -20,6 +20,7 @@ int ipv6_exthdrs_init(void); void ipv6_exthdrs_exit(void); int ipv6_frag_init(void); void ipv6_frag_exit(void); +int register_ipv6_reassembly_bpf(void); /* transport protocols */ int pingv6_init(void); diff --git a/net/ipv6/Makefile b/net/ipv6/Makefile index 3036a45e8a1e..6e90ff1d20c0 100644 --- a/net/ipv6/Makefile +++ b/net/ipv6/Makefile @@ -26,6 +26,7 @@ ipv6-$(CONFIG_IPV6_SEG6_LWTUNNEL) += seg6_iptunnel.o seg6_local.o ipv6-$(CONFIG_IPV6_SEG6_HMAC) += seg6_hmac.o ipv6-$(CONFIG_IPV6_RPL_LWTUNNEL) += rpl_iptunnel.o ipv6-$(CONFIG_IPV6_IOAM6_LWTUNNEL) += ioam6_iptunnel.o +ipv6-$(CONFIG_DEBUG_INFO_BTF) += reassembly_bpf.o obj-$(CONFIG_INET6_AH) += ah6.o obj-$(CONFIG_INET6_ESP) += esp6.o diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c index 38689bedfce7..39663de75fbd 100644 --- a/net/ipv6/af_inet6.c +++ b/net/ipv6/af_inet6.c @@ -1174,6 +1174,10 @@ static int __init inet6_init(void) if (err) goto ipv6_frag_fail; + err = register_ipv6_reassembly_bpf(); + if (err) + goto ipv6_frag_fail; + /* Init v6 transport protocols. */ err = udpv6_init(); if (err) diff --git a/net/ipv6/reassembly_bpf.c b/net/ipv6/reassembly_bpf.c new file mode 100644 index 000000000000..c6c804d4f636 --- /dev/null +++ b/net/ipv6/reassembly_bpf.c @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Unstable ipv6 fragmentation helpers for TC-BPF hook + * + * These are called from SCHED_CLS BPF programs. Note that it is allowed to + * break compatibility for these functions since the interface they are exposed + * through to BPF programs is explicitly unstable. + */ + +#include +#include +#include +#include +#include +#include +#include + +static int set_dst(struct sk_buff *skb, struct net *net) +{ + const struct ipv6hdr *ip6h = ipv6_hdr(skb); + struct dst_entry *dst; + + struct flowi6 fl6 = { + .flowi6_flags = FLOWI_FLAG_ANYSRC, + .flowi6_mark = skb->mark, + .flowlabel = ip6_flowinfo(ip6h), + .flowi6_iif = skb->skb_iif, + .flowi6_proto = ip6h->nexthdr, + .daddr = ip6h->daddr, + .saddr = ip6h->saddr, + }; + + dst = ipv6_stub->ipv6_dst_lookup_flow(net, NULL, &fl6, NULL); + if (IS_ERR(dst)) + return PTR_ERR(dst); + + skb_dst_set(skb, dst); + + return 0; +} + +__diag_push(); +__diag_ignore_all("-Wmissing-prototypes", + "Global functions as their definitions will be in reassembly BTF"); + +/* bpf_ipv6_frag_rcv - Defragment an ipv6 packet + * + * This helper takes an skb as input. If this skb successfully reassembles + * the original packet, the skb is updated to contain the original, reassembled + * packet. + * + * Otherwise (on error or incomplete reassembly), the input skb remains + * unmodified. + * + * Parameters: + * @ctx - Pointer to program context (skb) + * @netns - Child network namespace id. If value is a negative signed + * 32-bit integer, the netns of the device in the skb is used. + * + * Return: + * 0 on successfully reassembly or non-fragmented packet. Negative value on + * error or incomplete reassembly. + */ +int bpf_ipv6_frag_rcv(struct __sk_buff *ctx, u64 netns) +{ + struct sk_buff *skb = (struct sk_buff *)ctx; + struct sk_buff *skb_cpy; + struct net *caller_net; + unsigned int foff; + struct net *net; + int mac_len; + void *mac; + int err; + + if (unlikely(!((s32)netns < 0 || netns <= S32_MAX))) + return -EINVAL; + + caller_net = skb->dev ? dev_net(skb->dev) : sock_net(skb->sk); + if ((s32)netns < 0) { + net = caller_net; + } else { + net = get_net_ns_by_id(caller_net, netns); + if (unlikely(!net)) + return -EINVAL; + } + + err = set_dst(skb, net); + if (err < 0) + return err; + + mac_len = skb->mac_len; + skb_cpy = skb_copy(skb, GFP_ATOMIC); + if (!skb_cpy) + return -ENOMEM; + + /* _ipv6_frag_rcv() expects skb->transport_header to be set to start of + * the frag header and nhoff to be set. + */ + err = ipv6_find_hdr(skb_cpy, &foff, NEXTHDR_FRAGMENT, NULL, NULL); + if (err < 0) + return err; + skb_set_transport_header(skb_cpy, foff); + IP6CB(skb_cpy)->nhoff = offsetof(struct ipv6hdr, nexthdr); + + /* inet6_protocol handlers return >0 on success, 0 on out of band + * consumption, <0 on error. We never expect to see 0 here. + */ + err = _ipv6_frag_rcv(net, skb_cpy, IP6_DEFRAG_BPF); + if (err < 0) + return err; + else if (err == 0) + return -EINVAL; + + skb_morph(skb, skb_cpy); + kfree_skb(skb_cpy); + + /* _ipv6_frag_rcv() does not maintain mac header, so push empty header + * in so prog sees the correct layout. The empty mac header will be + * later pulled from cls_bpf. + */ + skb->mac_len = mac_len; + mac = skb_push(skb, mac_len); + memset(mac, 0, mac_len); + bpf_compute_data_pointers(skb); + + return 0; +} + +__diag_pop() + +BTF_SET8_START(ipv6_reassembly_kfunc_set) +BTF_ID_FLAGS(func, bpf_ipv6_frag_rcv, KF_CHANGES_PKT) +BTF_SET8_END(ipv6_reassembly_kfunc_set) + +static const struct btf_kfunc_id_set ipv6_reassembly_bpf_kfunc_set = { + .owner = THIS_MODULE, + .set = &ipv6_reassembly_kfunc_set, +}; + +int register_ipv6_reassembly_bpf(void) +{ + return register_btf_kfunc_id_set(BPF_PROG_TYPE_SCHED_CLS, + &ipv6_reassembly_bpf_kfunc_set); +}