From patchwork Mon Jun 26 23:02:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 113146 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp7808617vqr; Mon, 26 Jun 2023 16:03:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5Kl+azM86AD31FCGhO1UAmo0iII6R4LL0Kbf0dYSIq+Dk8XiKolSpxbBHkgrhsmXRZEMfq X-Received: by 2002:a05:6e02:5c5:b0:341:e5f0:91e3 with SMTP id l5-20020a056e0205c500b00341e5f091e3mr25393413ils.4.1687820627224; Mon, 26 Jun 2023 16:03:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687820627; cv=none; d=google.com; s=arc-20160816; b=wgxeKtPtMsI8XxGTpgJlTNGj6CpR30MIXLYr8tsneoEHfJwErZJJjPFCOumpUur38f lDulBwKwNAzw+yCHzvQAe98PSUWBoZtdiY/b31n3cyc2VoZ/udtsGHgp5Xw019vdBRh3 sHqT7FecbMBKl/xMcTyjyxoULjcT1S7hakgnPkY4y/6Mm6EsKbe/DlyBVG9y9sO73lE0 nYshl5p0IYBVb1xv4uPQrZ/kvGRNLj0xRg3Js76V9Q/bTH1nhvWEs0wv53ABPNipIYvR NXF81nxHm0101wZ7ylGq6sOdlcoYN+jAPQMY81NYMXdmrRj5KbxiunWHW8wWG9bDnwHG LLTA== 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=LRmuab6R6ClXxsIAWdYJTaRQBWQe0tQBAwVSwfhEFfo=; fh=n2s39s+OMKsF2nIFrddV2rynWJqSqU+j7UGYiRpmkQE=; b=j7bClspT1j/WiKjKfB9S07kSmETcDcue3T661ZbFBUmNunquVRMjNpWZ40gbmZZCdo cvv2W76UmFVNWNP3vA1Aqtvoq467FRLGLhSLivcjgyXHoGgGaoBfHJLQjbc64wZxOj2U ehPKSECb3suqvveqHnPe3NINnL5R2lmwaRaZut/6nVUk3O4d3yOePDo+B4E/2kJ+BDyq AAM1ZVEXFRN0Nq5fC6Z/6dIEhMBYkmBgfgGKXl+zgFIdGKXq/D2+KVQpLZYBE9NOja/T xpJ0sx8vVAp/ttHLnZwryBeX93X7LPpESp3gF2n5HiKozV57iZja40hTSCwqxsLsGL6K 5bzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm1 header.b="WvGr/IAG"; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=DNdRYjuJ; 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 n38-20020a635c66000000b005303a26dbf8si5938194pgm.408.2023.06.26.16.03.34; Mon, 26 Jun 2023 16:03:47 -0700 (PDT) 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=fm1 header.b="WvGr/IAG"; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=DNdRYjuJ; 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 S229880AbjFZXCd (ORCPT + 99 others); Mon, 26 Jun 2023 19:02:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229670AbjFZXCZ (ORCPT ); Mon, 26 Jun 2023 19:02:25 -0400 Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB8C310E4; Mon, 26 Jun 2023 16:02:23 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id DC6B75C00D1; Mon, 26 Jun 2023 19:02:21 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 26 Jun 2023 19:02:21 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1687820541; x= 1687906941; bh=LRmuab6R6ClXxsIAWdYJTaRQBWQe0tQBAwVSwfhEFfo=; b=W vGr/IAGBJ68pi0ITGDMOThI/yMAX6bfzgCui+WGs/mNqYRCF5msAqPcRY8BA4Bxz VswhWZdEFDv3gz9ZfHHhe831FsjnSgPPhf4w4wrLENtxlzEQUJydvO7/GzJ+B4Dd FXYdQ3ujPExgosN0qlvN4STM1HMaRkEyrwTGSjou8O9C46+4CPNHxXYMP6ri0brf iTlUOm5eoacHdGzz+8D4o2jNWiiWz9y5+Mf5hX2D7wNpVgGFxVUi1IVmBieud/8W mX0eExhyktgWpYgQ6PNHM9/jmlGW/5ce5hTWkSldFrOo6nkx+FfDq5zeFI2Hpnri 8doXxtygnTC3rly7jHyMg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type: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=fm2; t=1687820541; x= 1687906941; bh=LRmuab6R6ClXxsIAWdYJTaRQBWQe0tQBAwVSwfhEFfo=; b=D NdRYjuJs3JQlLviXVmGUlBxSoRC1pfg2ysGH1jy6WVgxBB4giDyW/fnNETWVBx6b 7lMaKzMWaYs8J2yCZhRLnB/5wJWPaZe3yVhNBl4C55n6Hq55mScoRheN5zELF871 twNI2qitcM/glGYECOer1WHFo0Vf7lkOac2O2OseazGn08omZmZH1HHoCVFnguLf MX99aqi516JTx3Fnj/c1mONYcBQ7l0tPoGTJsfPntVMR83NEgjel2DBFGXhp0019 x1ZmRJub4qNVWrGbC3XcEnOBv3FilO6JjPO4VlAtddeQSomXGU6U1IvayG2K6kyB hzBF6EU0+izhyXlfKEMwQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrgeehgedgudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdlvdefmdenucfjughrpefhvfevufffkffojghfggfgsedtkeertder tddtnecuhfhrohhmpeffrghnihgvlhcuighuuceougiguhesugiguhhuuhdrgiihiieqne cuggftrfgrthhtvghrnhepjeegveeljeehvdevudduffffleelveejueegjedvhedvhedv heethfejgedtieeinecuffhomhgrihhnpehnvghtfhhilhhtvghrrdhpfhenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhu rdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 26 Jun 2023 19:02:19 -0400 (EDT) From: Daniel Xu To: daniel@iogearbox.net, ast@kernel.org, andrii@kernel.org, fw@strlen.de Cc: martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netfilter-devel@vger.kernel.org, dsahern@kernel.org, Andrii Nakryiko Subject: [PATCH bpf-next 1/7] tools: libbpf: add netfilter link attach helper Date: Mon, 26 Jun 2023 17:02:08 -0600 Message-Id: X-Mailer: git-send-email 2.40.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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE 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?1769808201773751730?= X-GMAIL-MSGID: =?utf-8?q?1769808201773751730?= Add new api function: bpf_program__attach_netfilter. It takes a bpf program (netfilter type), and a pointer to a option struct that contains the desired attachment (protocol family, priority, hook location, ...). It returns a pointer to a 'bpf_link' structure or NULL on error. Next patch adds new netfilter_basic test that uses this function to attach a program to a few pf/hook/priority combinations. Co-developed-by: Florian Westphal Signed-off-by: Florian Westphal Suggested-by: Andrii Nakryiko Signed-off-by: Daniel Xu --- tools/lib/bpf/bpf.c | 8 +++++++ tools/lib/bpf/bpf.h | 6 +++++ tools/lib/bpf/libbpf.c | 47 ++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/libbpf.h | 15 +++++++++++++ tools/lib/bpf/libbpf.map | 1 + 5 files changed, 77 insertions(+) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index ed86b37d8024..3b0da19715e1 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -741,6 +741,14 @@ int bpf_link_create(int prog_fd, int target_fd, if (!OPTS_ZEROED(opts, tracing)) return libbpf_err(-EINVAL); break; + case BPF_NETFILTER: + attr.link_create.netfilter.pf = OPTS_GET(opts, netfilter.pf, 0); + attr.link_create.netfilter.hooknum = OPTS_GET(opts, netfilter.hooknum, 0); + attr.link_create.netfilter.priority = OPTS_GET(opts, netfilter.priority, 0); + attr.link_create.netfilter.flags = OPTS_GET(opts, netfilter.flags, 0); + if (!OPTS_ZEROED(opts, netfilter)) + return libbpf_err(-EINVAL); + break; default: if (!OPTS_ZEROED(opts, flags)) return libbpf_err(-EINVAL); diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 9aa0ee473754..c676295ab9bf 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -349,6 +349,12 @@ struct bpf_link_create_opts { struct { __u64 cookie; } tracing; + struct { + __u32 pf; + __u32 hooknum; + __s32 priority; + __u32 flags; + } netfilter; }; size_t :0; }; diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 214f828ece6b..a8b9d5abb55f 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -11811,6 +11811,53 @@ static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_l return libbpf_get_error(*link); } +struct bpf_link *bpf_program__attach_netfilter(const struct bpf_program *prog, + const struct bpf_netfilter_opts *opts) +{ + DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); + struct bpf_link *link; + int prog_fd, link_fd; + + if (!OPTS_VALID(opts, bpf_netfilter_opts)) + return libbpf_err_ptr(-EINVAL); + + link_create_opts.netfilter.pf = OPTS_GET(opts, pf, 0); + link_create_opts.netfilter.hooknum = OPTS_GET(opts, hooknum, 0); + link_create_opts.netfilter.priority = OPTS_GET(opts, priority, 0); + link_create_opts.netfilter.flags = OPTS_GET(opts, flags, 0); + + prog_fd = bpf_program__fd(prog); + if (prog_fd < 0) { + pr_warn("prog '%s': can't attach before loaded\n", prog->name); + return libbpf_err_ptr(-EINVAL); + } + + link = calloc(1, sizeof(*link)); + if (!link) + return libbpf_err_ptr(-ENOMEM); + link->detach = &bpf_link__detach_fd; + + link_fd = bpf_link_create(prog_fd, 0, BPF_NETFILTER, &link_create_opts); + + link->fd = ensure_good_fd(link_fd); + + if (link->fd < 0) { + char errmsg[STRERR_BUFSIZE]; + + link_fd = -errno; + free(link); + pr_warn("prog '%s': failed to attach to pf:%d,hooknum:%d:prio:%d: %s\n", + prog->name, + OPTS_GET(opts, pf, 0), + OPTS_GET(opts, hooknum, 0), + OPTS_GET(opts, priority, 0), + libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg))); + return libbpf_err_ptr(link_fd); + } + + return link; +} + struct bpf_link *bpf_program__attach(const struct bpf_program *prog) { struct bpf_link *link = NULL; diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 754da73c643b..10642ad69d76 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -718,6 +718,21 @@ LIBBPF_API struct bpf_link * bpf_program__attach_freplace(const struct bpf_program *prog, int target_fd, const char *attach_func_name); +struct bpf_netfilter_opts { + /* size of this struct, for forward/backward compatibility */ + size_t sz; + + __u32 pf; + __u32 hooknum; + __s32 priority; + __u32 flags; +}; +#define bpf_netfilter_opts__last_field flags + +LIBBPF_API struct bpf_link * +bpf_program__attach_netfilter(const struct bpf_program *prog, + const struct bpf_netfilter_opts *opts); + struct bpf_map; LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index 7521a2fb7626..d9ec4407befa 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -395,4 +395,5 @@ LIBBPF_1.2.0 { LIBBPF_1.3.0 { global: bpf_obj_pin_opts; + bpf_program__attach_netfilter; } LIBBPF_1.2.0; From patchwork Mon Jun 26 23:02:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 113147 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp7808622vqr; Mon, 26 Jun 2023 16:03:47 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ50nOtW1AbxLeENh12IaIFAXXIxO2jjDdb55Es3kFudvHxfb+WD+fZr0Vc5Tj842Dw+CyYb X-Received: by 2002:a17:90a:8d86:b0:262:fe4b:b45 with SMTP id d6-20020a17090a8d8600b00262fe4b0b45mr3950933pjo.19.1687820627467; Mon, 26 Jun 2023 16:03:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687820627; cv=none; d=google.com; s=arc-20160816; b=vmgRjNKmMQWfjofHDuysZBabgEC/oRpwquvuUH02ewnmfR/A+jTnBq8bl2zhGFnlC3 OvV4UPyope18hhtW7FCgEzf5FX5RJzGjf5QZIt9sIrJImpxtF3bGr5mq85yG4szZPp9a tAaXf3+lh1EeiLA09D726Khrj5mTakjH4w76lpjlH3/jIyfz9GFjfF90MuAWnUHbNPOk bXy5lA/CLUd3kR/okFkwM3bLRS3F1IjpIroVpcmPMDk0QBLtHd6CG3eqpE0XaBMATCod u5oBIBPsxediHbOK0zoJyF2gW60jExKKfeOA7wC9ZNN06PqSETqUnh33a/zUaoewlGPh NrWg== 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=zvVh1fx8mJsJ0llz/fWqNzEDpjhcK5ZKaZSEnotm8Qs=; fh=xU12ScKdWpAwl78AKEPlO7kx/+mKjccw6EfcvhgA87U=; b=e38IqKANa4bAKFRMvNgOH/mPtzVDdTEhMTenUk2CF+YsGAdLZoHTBL8Luezj+Pl9B9 IpFn8zWV8fX1bt/ozL3XTUm3AdQp7cRQ+GECwmRsWQBCW2MXUf3pckWarGq0YTTBxKjM NNPvjgE4nwFRvm26ZjVYq04u6Of+V++OpweJFJwboJw+jfGPc40qWcJ2FSP/NlV8+uc1 lrpwyFShdxX3pFaZknj9zVjdJkxDkx4WJigrDfrZX+2mwOJ5QaKdRo8MxYf7U0PEA5pM 6/rs2+2doKlpg0Oxe9rkUe3IwLitW/EE4v/rspHEbznM9D7GJRpbEjumWsFoq+9hNsu5 yFew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm1 header.b=E1LWtVo4; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=cC6W5nK6; 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 q22-20020a631f56000000b00550e8b3614asi5261457pgm.901.2023.06.26.16.03.34; Mon, 26 Jun 2023 16:03:47 -0700 (PDT) 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=fm1 header.b=E1LWtVo4; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=cC6W5nK6; 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 S229825AbjFZXCh (ORCPT + 99 others); Mon, 26 Jun 2023 19:02:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229653AbjFZXCZ (ORCPT ); Mon, 26 Jun 2023 19:02:25 -0400 Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9EBE10EC; Mon, 26 Jun 2023 16:02:24 -0700 (PDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 1354E5C0164; Mon, 26 Jun 2023 19:02:24 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 26 Jun 2023 19:02:24 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1687820544; x= 1687906944; bh=zvVh1fx8mJsJ0llz/fWqNzEDpjhcK5ZKaZSEnotm8Qs=; b=E 1LWtVo45Hh+d3qwE5X1bscpQFeb/tW56uDPpDf0QptYqJDs82siTLhgUUW6qFKJK LmpoEhPHhJ2HQZvbv83S+LItjnWoBMPyfqbhrsiDNl9HS413XSr/TGQClEJhwlPc O0xxaQFwNoakv1VnWlVjTulrOdTI/JpVt8lIKMEsk0kh4SK+512yFcyGykvVZdqR PJknvlrm5mGMM3dW1V1ETTYyPTnX4zjKXzjwTCOtfAMhMKZsoPmfdr1+RQRvNME/ 40ZpF1IXYQ+QICdVC2Qs67mEKUM+h8M8e/26IeWpTDam9FVSEkSTF2HE1Uj0/N98 UgXegVEkKVLgClHl0pP0w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type: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=fm2; t=1687820544; x= 1687906944; bh=zvVh1fx8mJsJ0llz/fWqNzEDpjhcK5ZKaZSEnotm8Qs=; b=c C6W5nK61tARnvjVDjbqtQivkIarPHJPPcEuw0ev9T/d4YzDkHHVlp1aX4iLaCEq0 3/3VZuqEOoUGMqceSewGMaWhm9AESiNxfImxO+QHvwXuozEmYV83vJG8PRgP+hVG y8gwHCgZwWxSaW9Acp1nEw+clFcsvokPkW5O6OupiCXRJb+5Kd0So0r3Xcq7OJdM 1DW/tAO/LEMCMm/c+jnMWQh4eI+AK06cW/q1WB4p4UPoHAwB6HaDthkjj072JUP4 6CZxnlMAWH2H2E6LPdZmJ5kf7mtkkj3k98XvXg5RVE3mieAxOwCk26TXLyrKNOlS ihrADuo7eZCtEbuUYrlDA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrgeehgedgudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdlfeehmdenucfjughrpefhvf evufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghnihgvlhcuighuuceo ugiguhesugiguhhuuhdrgiihiieqnecuggftrfgrthhtvghrnhepgfefgfegjefhudeike dvueetffelieefuedvhfehjeeljeejkefgffeghfdttdetnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhuuhdrgiihii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 26 Jun 2023 19:02:21 -0400 (EDT) From: Daniel Xu To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, shuah@kernel.org, fw@strlen.de Cc: mykolal@fb.com, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, netfilter-devel@vger.kernel.org, dsahern@kernel.org Subject: [PATCH bpf-next 2/7] selftests/bpf: Add bpf_program__attach_netfilter helper test Date: Mon, 26 Jun 2023 17:02:09 -0600 Message-Id: <96bb65cbdf760b6a593ab85104a95b373d1d822b.1687819413.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.40.1 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-2.6 required=5.0 tests=ANY_BOUNCE_MESSAGE,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE,VBOUNCE_MESSAGE 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?1769808201823925469?= X-GMAIL-MSGID: =?utf-8?q?1769808201823925469?= Call bpf_program__attach_netfilter() with different protocol/hook/priority combinations. Test fails if supposedly-illegal attachments work (e.g., bogus protocol family, illegal priority and so on) or if a should-work attachment fails. Co-developed-by: Florian Westphal Signed-off-by: Florian Westphal Signed-off-by: Daniel Xu --- .../bpf/prog_tests/netfilter_basic.c | 78 +++++++++++++++++++ .../bpf/progs/test_netfilter_link_attach.c | 14 ++++ 2 files changed, 92 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/netfilter_basic.c create mode 100644 tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c diff --git a/tools/testing/selftests/bpf/prog_tests/netfilter_basic.c b/tools/testing/selftests/bpf/prog_tests/netfilter_basic.c new file mode 100644 index 000000000000..357353fee19d --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/netfilter_basic.c @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include + +#include "test_progs.h" +#include "test_netfilter_link_attach.skel.h" + +struct nf_hook_options { + __u32 pf; + __u32 hooknum; + __s32 priority; + __u32 flags; + + bool expect_success; +}; + +struct nf_hook_options nf_hook_attach_tests[] = { + { }, + { .pf = NFPROTO_NUMPROTO, }, + { .pf = NFPROTO_IPV4, .hooknum = 42, }, + { .pf = NFPROTO_IPV4, .priority = INT_MIN }, + { .pf = NFPROTO_IPV4, .priority = INT_MAX }, + { .pf = NFPROTO_IPV4, .flags = UINT_MAX }, + + { .pf = NFPROTO_INET, .priority = 1, }, + + { .pf = NFPROTO_IPV4, .priority = -10000, .expect_success = true }, + { .pf = NFPROTO_IPV6, .priority = 10001, .expect_success = true }, +}; + +void test_netfilter_basic(void) +{ + struct test_netfilter_link_attach *skel; + LIBBPF_OPTS(bpf_netfilter_opts, opts); + struct bpf_program *prog; + int i; + + skel = test_netfilter_link_attach__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_netfilter_link_attach__open_and_load")) + goto out; + + prog = skel->progs.nf_link_attach_test; + + for (i = 0; i < ARRAY_SIZE(nf_hook_attach_tests); i++) { + struct bpf_link *link; + +#define X(opts, m, i) opts.m = nf_hook_attach_tests[(i)].m + X(opts, pf, i); + X(opts, hooknum, i); + X(opts, priority, i); + X(opts, flags, i); +#undef X + link = bpf_program__attach_netfilter(prog, &opts); + if (nf_hook_attach_tests[i].expect_success) { + struct bpf_link *link2; + + if (!ASSERT_OK_PTR(link, "program attach successful")) + continue; + + link2 = bpf_program__attach_netfilter(prog, &opts); + ASSERT_ERR_PTR(link2, "attach program with same pf/hook/priority"); + + if (!ASSERT_OK(bpf_link__destroy(link), "link destroy")) + break; + + link2 = bpf_program__attach_netfilter(prog, &opts); + if (!ASSERT_OK_PTR(link2, "program reattach successful")) + continue; + if (!ASSERT_OK(bpf_link__destroy(link2), "link destroy")) + break; + } else { + ASSERT_ERR_PTR(link, "program load failure"); + } + } +out: + test_netfilter_link_attach__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c b/tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c new file mode 100644 index 000000000000..03a475160abe --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_netfilter_link_attach.c @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "vmlinux.h" +#include + +#define NF_ACCEPT 1 + +SEC("netfilter") +int nf_link_attach_test(struct bpf_nf_ctx *ctx) +{ + return NF_ACCEPT; +} + +char _license[] SEC("license") = "GPL"; From patchwork Mon Jun 26 23:02:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 113148 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp7811831vqr; Mon, 26 Jun 2023 16:09:02 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ48aSOI8uDhQ9u8Cne18nlzAtXTLAu4F6qAy+h4yhmHSqHNxvDn8BGkXWeUfA7R2BLk+WAf X-Received: by 2002:a05:6a20:7f99:b0:10b:4539:fa0a with SMTP id d25-20020a056a207f9900b0010b4539fa0amr37991087pzj.1.1687820941762; Mon, 26 Jun 2023 16:09:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687820941; cv=none; d=google.com; s=arc-20160816; b=q7nrwApJbVpDynzRuVdu4RzdoKn0Z/IV2eQW/pnDwDQWwUljyR024TYifxciqTJprp F/3PYl3ryqlQcfqEBLVCDOtC2xgrROKPZqJrE1SK6yaDQWbX8uVWFAdHtcuRacut5v/C PSfI1cQnKuMtBp4a327Ykihebq7Gl4vbOvJIy/XkF+QSdq65OsS11+2qK04Z4IBzp9ST wGlKfqlOyaisyvM1PDhVCXwzYaosEEB8HR4NztIl7d5CmGIyfNvDi+2NdEVzTLFEns+Z nhyErmOkABfLkJtFmthRI9eIPzdB5JzsqGGHaFFZTs/yMaCem4mk4dJ9ICYP24q6fRJM oefg== 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=fumgg/v3msTCwAU1PpJaH0UyuDxgI4vRhDREddSfnvY=; fh=VGdqhwnS7tCsxh/5np2re55mXjZYh3Tc680/G6sIFR8=; b=LkRXucbPtvTYgs/RSjBy09+d5bQs/uaYeAhPjqMKrgSAoE9A3fSLn8WsWo/NNVGMim okded+HYTTaqgFFKQOt2WPfWsi4v/6WQdWG6VAU7rE2RXQ9YPJ2GfuBiBAGZY7M/fId1 y0ah3H33WgozpJ+88cckMVzs1rYvAUrECdlXY5BIBq1YfFJiq+LaodbhcV/i+F52s+pb 0QRzLk/zZwR0RnuyC+FRO9R598lDObtuWDC+6AJuVwrUjv5R7JSmLr0IihlmZKo1hyM4 BM79uZsZXg4wNfDmP/oBVFyWvO4F1Hx7vVg5baimsUpfuKGc9zGeG+daqTwolFj+BsjH SahQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm1 header.b=K1o9yLE1; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=B3rMd6pX; 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 fb22-20020a056a002d9600b0067af445c0f0si1240285pfb.291.2023.06.26.16.08.47; Mon, 26 Jun 2023 16:09:01 -0700 (PDT) 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=fm1 header.b=K1o9yLE1; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=B3rMd6pX; 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 S230019AbjFZXCn (ORCPT + 99 others); Mon, 26 Jun 2023 19:02:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229783AbjFZXC1 (ORCPT ); Mon, 26 Jun 2023 19:02:27 -0400 Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78AC810DB; Mon, 26 Jun 2023 16:02:26 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id D1DB75C0064; Mon, 26 Jun 2023 19:02:25 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Mon, 26 Jun 2023 19:02:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1687820545; x= 1687906945; bh=fumgg/v3msTCwAU1PpJaH0UyuDxgI4vRhDREddSfnvY=; b=K 1o9yLE1vMQ/yES3xb/eg5kwHneMEghoEspxjKHVXcAyBq2YaRNLK84pagE2H1KAc QuSZ1bevKIQZW2uCHC/4aXU18v+MohuSxvKSNFF4ujokvYhSF3e/fSRyKDn2mUTu T1g63avDkAAruz/4R0qgHSZyVlsokpu8J2VCne7gsyi26hER9Wy9HADgc+nRV+Sh 6qJPpJi4lMOePqBJGMDeRg5XEJxBvpsVeGyXTSfJ5Ws5sfu7WPaWWelSB9i3I9xd fGFSF/+VkFhVmypIuvLFhTdIDyti5hV1Q07/1WDsSqGwPdggsvWlaA7doZaNJxY2 j2EWTX3YBjYAnJh/Ld4rw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type: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=fm2; t=1687820545; x= 1687906945; bh=fumgg/v3msTCwAU1PpJaH0UyuDxgI4vRhDREddSfnvY=; b=B 3rMd6pXubnBB3zPpbHRA09t0f6kPTj5kbSXUKy68bGjV29/7hf9OMIzCpXVBC94y IkZoYzOUuAisvSIRLLBsIkAJNaIHTsucJWre8Q4F4IwI3pI5hKtKCuUcqW2RP5PW Jjv1B9ZKhMmgg/0ALgBXtiZ00GIE+4bzSwOqsLjai0p3iDTdkcBOSiYXAwcDWSyI jpfky/43XzJh8Tkr6ZZkw1SSh2Z8U+Cu263yb3Vy8O8XQEApvayanyrUashmOmOj k3IVRHZkWfDYUU3/098rqW3CUan2guStAxtuo90kl6y0S0LYn8YInLsxU7aDBihj 9vLfZz7imvfqOgo3IA88Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrgeehgedgudeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdljedtmdenucfjughrpefhvf evufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghnihgvlhcuighuuceo ugiguhesugiguhhuuhdrgiihiieqnecuggftrfgrthhtvghrnhepgfefgfegjefhudeike dvueetffelieefuedvhfehjeeljeejkefgffeghfdttdetnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhuuhdrgiihii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 26 Jun 2023 19:02:24 -0400 (EDT) From: Daniel Xu To: edumazet@google.com, dsahern@kernel.org, kuba@kernel.org, fw@strlen.de, pabeni@redhat.com, pablo@netfilter.org, davem@davemloft.net, kadlec@netfilter.org, daniel@iogearbox.net Cc: netfilter-devel@vger.kernel.org, coreteam@netfilter.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, bpf@vger.kernel.org Subject: [PATCH bpf-next 3/7] netfilter: defrag: Add glue hooks for enabling/disabling defrag Date: Mon, 26 Jun 2023 17:02:10 -0600 Message-Id: <66d1eaeb17721b1274eb4c1991a3725e47c912c5.1687819413.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.40.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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE 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?1769808531677868058?= X-GMAIL-MSGID: =?utf-8?q?1769808531677868058?= We want to be able to enable/disable IP packet defrag from core bpf/netfilter code. In other words, execute code from core that could possibly be built as a module. To help avoid symbol resolution errors, use glue hooks that the modules will register callbacks with during module init. Signed-off-by: Daniel Xu --- include/linux/netfilter.h | 12 ++++++++++++ net/ipv4/netfilter/nf_defrag_ipv4.c | 8 ++++++++ net/ipv6/netfilter/nf_defrag_ipv6_hooks.c | 10 ++++++++++ net/netfilter/core.c | 6 ++++++ 4 files changed, 36 insertions(+) diff --git a/include/linux/netfilter.h b/include/linux/netfilter.h index 0762444e3767..1d68499de03e 100644 --- a/include/linux/netfilter.h +++ b/include/linux/netfilter.h @@ -481,6 +481,18 @@ struct nfnl_ct_hook { }; extern const struct nfnl_ct_hook __rcu *nfnl_ct_hook; +struct nf_defrag_v4_hook { + int (*enable)(struct net *net); + void (*disable)(struct net *net); +}; +extern const struct nf_defrag_v4_hook __rcu *nf_defrag_v4_hook; + +struct nf_defrag_v6_hook { + int (*enable)(struct net *net); + void (*disable)(struct net *net); +}; +extern const struct nf_defrag_v6_hook __rcu *nf_defrag_v6_hook; + /** * nf_skb_duplicated - TEE target has sent a packet * diff --git a/net/ipv4/netfilter/nf_defrag_ipv4.c b/net/ipv4/netfilter/nf_defrag_ipv4.c index e61ea428ea18..436e629b0969 100644 --- a/net/ipv4/netfilter/nf_defrag_ipv4.c +++ b/net/ipv4/netfilter/nf_defrag_ipv4.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -113,17 +114,24 @@ static void __net_exit defrag4_net_exit(struct net *net) } } +static struct nf_defrag_v4_hook defrag_hook = { + .enable = nf_defrag_ipv4_enable, + .disable = nf_defrag_ipv4_disable, +}; + static struct pernet_operations defrag4_net_ops = { .exit = defrag4_net_exit, }; static int __init nf_defrag_init(void) { + rcu_assign_pointer(nf_defrag_v4_hook, &defrag_hook); return register_pernet_subsys(&defrag4_net_ops); } static void __exit nf_defrag_fini(void) { + rcu_assign_pointer(nf_defrag_v4_hook, NULL); unregister_pernet_subsys(&defrag4_net_ops); } diff --git a/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c b/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c index cb4eb1d2c620..205fb692f524 100644 --- a/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c +++ b/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -96,6 +97,11 @@ static void __net_exit defrag6_net_exit(struct net *net) } } +static struct nf_defrag_v6_hook defrag_hook = { + .enable = nf_defrag_ipv6_enable, + .disable = nf_defrag_ipv6_disable, +}; + static struct pernet_operations defrag6_net_ops = { .exit = defrag6_net_exit, }; @@ -114,6 +120,9 @@ static int __init nf_defrag_init(void) pr_err("nf_defrag_ipv6: can't register pernet ops\n"); goto cleanup_frag6; } + + rcu_assign_pointer(nf_defrag_v6_hook, &defrag_hook); + return ret; cleanup_frag6: @@ -124,6 +133,7 @@ static int __init nf_defrag_init(void) static void __exit nf_defrag_fini(void) { + rcu_assign_pointer(nf_defrag_v6_hook, NULL); unregister_pernet_subsys(&defrag6_net_ops); nf_ct_frag6_cleanup(); } diff --git a/net/netfilter/core.c b/net/netfilter/core.c index 5f76ae86a656..34845155bb85 100644 --- a/net/netfilter/core.c +++ b/net/netfilter/core.c @@ -680,6 +680,12 @@ EXPORT_SYMBOL_GPL(nfnl_ct_hook); const struct nf_ct_hook __rcu *nf_ct_hook __read_mostly; EXPORT_SYMBOL_GPL(nf_ct_hook); +const struct nf_defrag_v4_hook __rcu *nf_defrag_v4_hook __read_mostly; +EXPORT_SYMBOL_GPL(nf_defrag_v4_hook); + +const struct nf_defrag_v6_hook __rcu *nf_defrag_v6_hook __read_mostly; +EXPORT_SYMBOL_GPL(nf_defrag_v6_hook); + #if IS_ENABLED(CONFIG_NF_CONNTRACK) u8 nf_ctnetlink_has_listener; EXPORT_SYMBOL_GPL(nf_ctnetlink_has_listener); From patchwork Mon Jun 26 23:02:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 113149 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp7812092vqr; Mon, 26 Jun 2023 16:09:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6GTeNj+OP56G9MGnSHNU/3ArVfvYCCJgwKJluk3bsFlOLFX88AXkK7OypHT5aOusbRdXqL X-Received: by 2002:a05:6808:2a63:b0:3a1:dac8:9c1f with SMTP id fu3-20020a0568082a6300b003a1dac89c1fmr4283148oib.29.1687820974897; Mon, 26 Jun 2023 16:09:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687820974; cv=none; d=google.com; s=arc-20160816; b=hniyuKk0KXP4iJ2bZNVcKF0rA5rWfgMkZSAkM5Dffo6VDs+mT1vIf5AeHssfyq4TGX DCw9o56PxwbAyOBZ+0SrtwfDbQU8/Bv13f4xShnZhitCCIneB3ocRsUfO7Lf94jtDo6S 6CAU8veYIroMT2oRAjCok5Nw6iDUXdSVW0H4pfJwDDGAAlRK0zUaTIchIQSop4SOTD/1 57ksWzABi3bUw8c2qYKtxcJ/pC9PEYPNDN+fW2udyboU2fi2Rs0eJkicfxF+eT6xKNX9 bF4nFWf5PARvKMOb2o/9P7AiTSjhyraKsV7LvC0bOJJLyai9q+e7tX3Q9ynSGGqz8d7o c+dw== 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=6b8JNzZ9nHf7gmJe2DR3IE4e9XIkJuXbimCGs/vSd/M=; fh=X4BkhRPAI3LmvXZRXYHMNdGLcN/9blnffB8mpBf/KLA=; b=UmclUC0sAOuROsximKbKJj4GsbrmPxTZT+Fx8PnA1Sq9gIw+ChAuQCRYsQ/ecTI8ws mEdB2TGmoJSHYDalLSKaJ1+2oMVlPRDMEbCUTu5pQ4jQwo/Fc3W9ufb4zsXHPCT01Dk+ 9gCSNr56Nb/cl7jLWHOC5+yDgL+qZqqqE04YWSOCBdUHjj6cdfqDMP1G3IbtuhOvUzcU 4Wqu4J3rw2x8ysD28t5so036s/G4UkXsS/Ee9NWGV0MxNjTRYGA45/hJZUgypkSAhNLo cg29v8hk/N0syOnkFWINAjZbqfJ7KB7rW5yoq3R4XLiXEhi2ENba78hU4+lPlS9E6L6H 1kJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm1 header.b=hUlRbSOv; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=GWge2Zbp; 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 s15-20020a63dc0f000000b005575a1d0e2csi5814851pgg.269.2023.06.26.16.09.22; Mon, 26 Jun 2023 16:09:34 -0700 (PDT) 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=fm1 header.b=hUlRbSOv; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=GWge2Zbp; 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 S229823AbjFZXCu (ORCPT + 99 others); Mon, 26 Jun 2023 19:02:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229704AbjFZXCa (ORCPT ); Mon, 26 Jun 2023 19:02:30 -0400 Received: from new3-smtp.messagingengine.com (new3-smtp.messagingengine.com [66.111.4.229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D8C110DB; Mon, 26 Jun 2023 16:02:29 -0700 (PDT) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailnew.nyi.internal (Postfix) with ESMTP id 63445582914; Mon, 26 Jun 2023 19:02:28 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 26 Jun 2023 19:02:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1687820548; x= 1687827748; bh=6b8JNzZ9nHf7gmJe2DR3IE4e9XIkJuXbimCGs/vSd/M=; b=h UlRbSOvbGdVTP9mq/hwOXnTs4E0qglcpDth6gzoSTd+vcXWRZxKrvVPjAIBuhIQ7 HqfmDdZTNy3VHPVcJJN/3cNp/cjy1aoBCRIz8emdGW2WNnPPe58ehW3l9B1sWink 44TuFHn6YAgM7gHCRYa8nUzBT6x0oCXnI9M9p7sqoOCRcUqnRWQqKMmjGUPIduXs NSAaWmVdhsZjsldHm8a1JhUXIUBwH3SDdIQ8ONFyULEasv1ii0+5lQFLnWwPyUMY fbOsqy/qYyIaW9V0696iTndrHYvVpfNAniQcjKLbUtCyISZ3cLNni0PBL1wa1gyE eCywaJhT6CHvic7M3x8Ew== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type: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=fm2; t=1687820548; x= 1687827748; bh=6b8JNzZ9nHf7gmJe2DR3IE4e9XIkJuXbimCGs/vSd/M=; b=G Wge2ZbpLNg+JUeRkVe2Cp14zoqrHUQ0lGt+tgbhb0ul+PjPEc4ih6F01Vi0DnKM2 e0Q6ghnJCBl3upU1OHC6XVu0EpxJDG5aHSAP9snAALj0vxjmxRgaip+rtzp62aZq 5plzTsTiBnMUzsIFakmFB/iNmMSARpMRnds/UV+JyPPvx8ysSXp3/OuQrcbMIWIl kf3gqaQnM9tKlFAyzvGf0uixTg17KzkqW23NTcJj64guV7E1gorVJ0T+5umye7Vl BXp0RAne132qqvLgBSIw6cY8gYHq39h+KDd+43/jLTQRuKZRjy5uLPZ3wiI6q7UQ ioLtS32RyLb1/tGicxgDQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrgeehgedgudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdljedtmdenucfjughrpefhvf evufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghnihgvlhcuighuuceo ugiguhesugiguhhuuhdrgiihiieqnecuggftrfgrthhtvghrnhepjeegveeljeehvdevud duffffleelveejueegjedvhedvhedvheethfejgedtieeinecuffhomhgrihhnpehnvght fhhilhhtvghrrdhpfhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 26 Jun 2023 19:02:25 -0400 (EDT) From: Daniel Xu To: daniel@iogearbox.net, edumazet@google.com, kuba@kernel.org, fw@strlen.de, pabeni@redhat.com, pablo@netfilter.org, andrii@kernel.org, davem@davemloft.net, ast@kernel.org, kadlec@netfilter.org Cc: martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org, dsahern@kernel.org Subject: [PATCH bpf-next 4/7] netfilter: bpf: Support BPF_F_NETFILTER_IP_DEFRAG in netfilter link Date: Mon, 26 Jun 2023 17:02:11 -0600 Message-Id: <242c66138bf4ec8aa26b29d736fb48242b4164ce.1687819413.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.40.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,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE 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?1769808566372937896?= X-GMAIL-MSGID: =?utf-8?q?1769808566372937896?= This commit adds support for enabling IP defrag using pre-existing netfilter defrag support. Basically all the flag does is bump a refcnt while the link the active. Checks are also added to ensure the prog requesting defrag support is run _after_ netfilter defrag hooks. Signed-off-by: Daniel Xu --- include/uapi/linux/bpf.h | 5 ++ net/netfilter/nf_bpf_link.c | 108 +++++++++++++++++++++++++++++---- tools/include/uapi/linux/bpf.h | 5 ++ 3 files changed, 107 insertions(+), 11 deletions(-) diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 60a9d59beeab..04ac77481583 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -1170,6 +1170,11 @@ enum bpf_link_type { */ #define BPF_F_KPROBE_MULTI_RETURN (1U << 0) +/* link_create.netfilter.flags used in LINK_CREATE command for + * BPF_PROG_TYPE_NETFILTER to enable IP packet defragmentation. + */ +#define BPF_F_NETFILTER_IP_DEFRAG (1U << 0) + /* When BPF ldimm64's insn[0].src_reg != 0 then this can have * the following extensions: * diff --git a/net/netfilter/nf_bpf_link.c b/net/netfilter/nf_bpf_link.c index c36da56d756f..a8015dbce12a 100644 --- a/net/netfilter/nf_bpf_link.c +++ b/net/netfilter/nf_bpf_link.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include #include #include @@ -23,8 +24,77 @@ struct bpf_nf_link { struct nf_hook_ops hook_ops; struct net *net; u32 dead; + bool defrag; }; +static int bpf_nf_enable_defrag(struct bpf_nf_link *link) +{ + int err; + + switch (link->hook_ops.pf) { +#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) + case NFPROTO_IPV4: + const struct nf_defrag_v4_hook *v4_hook; + + err = request_module("nf_defrag_ipv4"); + if (err) + return err; + + rcu_read_lock(); + v4_hook = rcu_dereference(nf_defrag_v4_hook); + err = v4_hook->enable(link->net); + rcu_read_unlock(); + + return err; +#endif +#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) + case NFPROTO_IPV6: + const struct nf_defrag_v6_hook *v6_hook; + + err = request_module("nf_defrag_ipv6_hooks"); + if (err) + return err; + + rcu_read_lock(); + v6_hook = rcu_dereference(nf_defrag_v6_hook); + err = v6_hook->enable(link->net); + rcu_read_unlock(); + + return err; +#endif + default: + return -EAFNOSUPPORT; + } +} + +static void bpf_nf_disable_defrag(struct bpf_nf_link *link) +{ + switch (link->hook_ops.pf) { +#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) + case NFPROTO_IPV4: + const struct nf_defrag_v4_hook *v4_hook; + + rcu_read_lock(); + v4_hook = rcu_dereference(nf_defrag_v4_hook); + v4_hook->disable(link->net); + rcu_read_unlock(); + + break; +#endif +#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6) + case NFPROTO_IPV6: + const struct nf_defrag_v6_hook *v6_hook; + + rcu_read_lock(); + v6_hook = rcu_dereference(nf_defrag_v6_hook); + v6_hook->disable(link->net); + rcu_read_unlock(); + + break; + } +#endif +} + static void bpf_nf_link_release(struct bpf_link *link) { struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link); @@ -37,6 +107,9 @@ static void bpf_nf_link_release(struct bpf_link *link) */ if (!cmpxchg(&nf_link->dead, 0, 1)) nf_unregister_net_hook(nf_link->net, &nf_link->hook_ops); + + if (nf_link->defrag) + bpf_nf_disable_defrag(nf_link); } static void bpf_nf_link_dealloc(struct bpf_link *link) @@ -92,6 +165,8 @@ static const struct bpf_link_ops bpf_nf_link_lops = { static int bpf_nf_check_pf_and_hooks(const union bpf_attr *attr) { + int prio; + switch (attr->link_create.netfilter.pf) { case NFPROTO_IPV4: case NFPROTO_IPV6: @@ -102,19 +177,18 @@ static int bpf_nf_check_pf_and_hooks(const union bpf_attr *attr) return -EAFNOSUPPORT; } - if (attr->link_create.netfilter.flags) + if (attr->link_create.netfilter.flags & ~BPF_F_NETFILTER_IP_DEFRAG) return -EOPNOTSUPP; - /* make sure conntrack confirm is always last. - * - * In the future, if userspace can e.g. request defrag, then - * "defrag_requested && prio before NF_IP_PRI_CONNTRACK_DEFRAG" - * should fail. - */ - switch (attr->link_create.netfilter.priority) { - case NF_IP_PRI_FIRST: return -ERANGE; /* sabotage_in and other warts */ - case NF_IP_PRI_LAST: return -ERANGE; /* e.g. conntrack confirm */ - } + /* make sure conntrack confirm is always last */ + prio = attr->link_create.netfilter.priority; + if (prio == NF_IP_PRI_FIRST) + return -ERANGE; /* sabotage_in and other warts */ + else if (prio == NF_IP_PRI_LAST) + return -ERANGE; /* e.g. conntrack confirm */ + else if ((attr->link_create.netfilter.flags & BPF_F_NETFILTER_IP_DEFRAG) && + (prio > NF_IP_PRI_FIRST && prio <= NF_IP_PRI_CONNTRACK_DEFRAG)) + return -ERANGE; /* cannot use defrag if prog runs before nf_defrag */ return 0; } @@ -156,6 +230,18 @@ int bpf_nf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) return err; } + if (attr->link_create.netfilter.flags & BPF_F_NETFILTER_IP_DEFRAG) { + err = bpf_nf_enable_defrag(link); + if (err) { + bpf_link_cleanup(&link_primer); + return err; + } + /* only mark defrag enabled if enabling succeeds so cleanup path + * doesn't disable without a corresponding enable + */ + link->defrag = true; + } + err = nf_register_net_hook(net, &link->hook_ops); if (err) { bpf_link_cleanup(&link_primer); diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 60a9d59beeab..04ac77481583 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -1170,6 +1170,11 @@ enum bpf_link_type { */ #define BPF_F_KPROBE_MULTI_RETURN (1U << 0) +/* link_create.netfilter.flags used in LINK_CREATE command for + * BPF_PROG_TYPE_NETFILTER to enable IP packet defragmentation. + */ +#define BPF_F_NETFILTER_IP_DEFRAG (1U << 0) + /* When BPF ldimm64's insn[0].src_reg != 0 then this can have * the following extensions: * From patchwork Mon Jun 26 23:02:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 113152 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp7813633vqr; Mon, 26 Jun 2023 16:12:25 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7Z2LDQjMYvs+cSITCRBXZKCZ1cUzDH0zhWr34Zn6t2gVmiPJJWijwC4kGtSLPtYw2o4DYt X-Received: by 2002:a05:6512:239b:b0:4f7:6685:2c5f with SMTP id c27-20020a056512239b00b004f766852c5fmr20879989lfv.61.1687821145222; Mon, 26 Jun 2023 16:12:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687821145; cv=none; d=google.com; s=arc-20160816; b=DA0J/gEItd5kA7FQX1WIpNDYV3v+W579X1IP3EF7GZE9Xwmtd+kfDILu4QV9PVUhK0 TCkqdUYs6ogRvzChM/46pkUH4YY6UjT2uCxfQUvmp9ioEL1hhadBhp7bc+vavzEbdHmH fFmWV4WWbJQ0WXBdmplEXXg1OshxnMu1xM/IMPLTVbTmUpnQRBfyLnwxrq3jWEz3Jrt4 nGmO78IJaip3yPdRK5seU+y3ULSM4tywVUbLKK/ZUT6o1OGsPd+dOi3A1fTFJZ25jxly dj61XUj74l5XeHUMsmdqZJSILUn8YIHwxUoXYtb5zSktdL2JIJLt3/vfc9nf/a/HxIA+ w81A== 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=7tQw2BRMSXqer2U0h2RL5L+2q6EBjYovWny1JwaX3dM=; fh=OuswnlhvTXfWuHRnv1ZFfRE4jJ8Cx0fxssBKnvD5HNw=; b=fqHnbLo4Ve+aMxaMIuD0Z/NjBrztsaEaF5s7eIgD0HrNaPIgRXRR58tAjGYoAo5pq9 e16V/aw4WR4LFXrW27ltvZvgQELSq5THzxxLobRjLwfcEhH6x0nEzcKZejppNdn51T4w R/KAeJFoH+mOPLtGAQcVcn3TC/UKup+BXs1HyXKHnthHsm+Mj9khY/79AjJ25UTjI2lt a04M+C8NCdTiDGKQi5nv+Ng2WrdVHI3f2Np8ddIocmarKSav8C3qDmyHHVbxZVUiEM7i NHCQ1ZGzJNpMHFytypWPYOV+ynXk0b4V83ik9DwRoQaq85xlRLEiBjd/okajNstGo+li xozw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm1 header.b=iO5gWfm2; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=SgntrF0H; 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 c11-20020aa7df0b000000b0051d885b835dsi2707446edy.442.2023.06.26.16.12.00; Mon, 26 Jun 2023 16:12:25 -0700 (PDT) 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=fm1 header.b=iO5gWfm2; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=SgntrF0H; 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 S229566AbjFZXCz (ORCPT + 99 others); Mon, 26 Jun 2023 19:02:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229478AbjFZXCc (ORCPT ); Mon, 26 Jun 2023 19:02:32 -0400 Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28B1C10E4; Mon, 26 Jun 2023 16:02:31 -0700 (PDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 8706E5C015B; Mon, 26 Jun 2023 19:02:30 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 26 Jun 2023 19:02:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1687820550; x= 1687906950; bh=7tQw2BRMSXqer2U0h2RL5L+2q6EBjYovWny1JwaX3dM=; b=i O5gWfm2dzI7yqK96A4KCyQtw2/F3ABwN1y3NYiR0ZPk1vePPbF52RPyKUSKLKuNQ 1c8oX8EzX0GI6QJEHy5nzcXAlJsFF9/cIDHqsZ8PfSeaHWXqfqMdz4ncbkVIVkWp hv2s4aO0sIYT1FIX0Wve8lBiVpOmP7RBCg38SBRrjj8eoYDWVKjhXh418lMFmydo LbBelMEq3lHh50jQj1Ujy9C9MgLa44qlaHoWLcrt1Q+XkN6gvMe+nB9Qzn4id6mI UPb86Uc3Z2jq1E3K9WyRzepBEZMw711pdz3XAzQ13hbVEJ/ufa7AkdC2twAWue8V q74ZznTP0U7RvRhGaG4jw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type: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=fm2; t=1687820550; x= 1687906950; bh=7tQw2BRMSXqer2U0h2RL5L+2q6EBjYovWny1JwaX3dM=; b=S gntrF0HK33owWr4re6Z4QYIJ6qmSUAeI5BjtaoDkCEM8521klbFGvNzlVrjTZKHn rZMEiixKhvi6nOh1OP9I8CyRdjtgmC+xBxmVpgF6Rn0SMD/L8IZ75M9RVFjtV+cj U0B28l3udu6s+jnlvWNJ3BHXbNEchpL96S7ZPPI+g7OlhrMEto7rUJDTMLOnP60/ +NhovERQKLMsxnNxOwtxKNSwKeKB8IX2aGL3+CwmVndKfa9WDAWFkSu9Jk5YaAht 3y74H2xRKtaHjYnGAj2SFA48jESAVxkCapS53DQJsHZxSJG46xU3wXafW4MW02Dn QjwEOsNB/HvaU1QS2MDMA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrgeehgedgudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdljedtmdenucfjughrpefhvf evufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghnihgvlhcuighuuceo ugiguhesugiguhhuuhdrgiihiieqnecuggftrfgrthhtvghrnhepgfefgfegjefhudeike dvueetffelieefuedvhfehjeeljeejkefgffeghfdttdetnecuvehluhhsthgvrhfuihii vgepudenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhuuhdrgiihii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 26 Jun 2023 19:02:28 -0400 (EDT) From: Daniel Xu To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, shuah@kernel.org, fw@strlen.de Cc: mykolal@fb.com, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netfilter-devel@vger.kernel.org, dsahern@kernel.org Subject: [PATCH bpf-next 5/7] bpf: selftests: Support not connecting client socket Date: Mon, 26 Jun 2023 17:02:12 -0600 Message-Id: <53ff90b203c955fd46a3cc7c0c5fdab7b1584595.1687819413.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.40.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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE 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?1769808745381737853?= X-GMAIL-MSGID: =?utf-8?q?1769808745381737853?= For connectionless protocols or raw sockets we do not want to actually connect() to the server. Signed-off-by: Daniel Xu --- tools/testing/selftests/bpf/network_helpers.c | 5 +++-- tools/testing/selftests/bpf/network_helpers.h | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index a105c0cd008a..d5c78c08903b 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -301,8 +301,9 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts) strlen(opts->cc) + 1)) goto error_close; - if (connect_fd_to_addr(fd, &addr, addrlen, opts->must_fail)) - goto error_close; + if (!opts->noconnect) + if (connect_fd_to_addr(fd, &addr, addrlen, opts->must_fail)) + goto error_close; return fd; diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 694185644da6..87894dc984dd 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -21,6 +21,7 @@ struct network_helper_opts { const char *cc; int timeout_ms; bool must_fail; + bool noconnect; }; /* ipv4 test vector */ From patchwork Mon Jun 26 23:02:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 113150 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp7812538vqr; Mon, 26 Jun 2023 16:10:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6g60deVOnVB1kJHh6KJ+GdIbKuOCXZWEW67CqX+TuI4Z8iaqdoY/ltCmeTObuQS+dQEK0P X-Received: by 2002:a17:907:7ea1:b0:98e:886:f27f with SMTP id qb33-20020a1709077ea100b0098e0886f27fmr6799157ejc.20.1687821026973; Mon, 26 Jun 2023 16:10:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687821026; cv=none; d=google.com; s=arc-20160816; b=vflSc4NFVBnL4EBY8RLhoT0G5bn26rE++LHOPi1bZQCkQ6g9CZypVy3Ru9ZH8maovl ltoV4q10bs+MXm9lh1vtbnm4alNgtGOBu5KhZiidhGX4EBEoUmue5nrEtvlMQig0B+42 RlLmwBzgeqd8TlTwyo6VobbZlEGqcMzsfcWsDG2KO8LbaiiG77ojCjbxgAXCJi38TzRQ IkjkATc6EhLGTI8SMrQMefxV86ixYHiWrWRqANBHqD2eYnbPrO5flewqopbqt0A+8i47 fzDz18OX2V/WAdUM5woammMJ9iFsgfZ4oLmpRP2bZsQTkYH5a/arzUEYpLLQId9+ajI4 Y0Dw== 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=n503uRHlm7PSoiNPrcOUsA2hVWcbKqQi9TGbsXoSZ2w=; fh=X+QgxHa6Gldj6BPMGFLx8JolK8iAzuSrlUwBrEuzxNA=; b=wrvWaWeaaJqf1Ic/vwvsuxc9V4VJ9WEpi116Vg3iyuWEfpl7Tn0AkXb0iA6fDbur+z PyIjCQg47bl4LhgrlWLojvWFlgHZk+xJiTT5XV5EA7ofFbs0mBZzUTbb5qb+ltE3dmYr WC1a9DCImhu7RoMrP7XpnYRfI2ZGpVv7V1ciZzCmvtHgtD9c0dYG0KtmEBy0r4y8TMBL XCLtJYbqxlySdNx3Z2yTKjY0nSCbI/8/tl1+pxtE9UwU5swGMM6gzsRwqixG2NGYB0B2 GsSj7k/bHG68yr6iRuXhwwR8H+KB+6mkGaQMMBUILC67NSowtzEYCC8kuN32uwbpmBUQ 565g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm1 header.b=AM2xFjMF; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=aIgs5mBn; 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 ga8-20020a170906b84800b00982cbf93b87si3582453ejb.1014.2023.06.26.16.10.02; Mon, 26 Jun 2023 16:10:26 -0700 (PDT) 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=fm1 header.b=AM2xFjMF; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=aIgs5mBn; 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 S230089AbjFZXC7 (ORCPT + 99 others); Mon, 26 Jun 2023 19:02:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51386 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229935AbjFZXCj (ORCPT ); Mon, 26 Jun 2023 19:02:39 -0400 Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5CE411702; Mon, 26 Jun 2023 16:02:33 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 97D9E5C011D; Mon, 26 Jun 2023 19:02:32 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 26 Jun 2023 19:02:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1687820552; x= 1687906952; bh=n503uRHlm7PSoiNPrcOUsA2hVWcbKqQi9TGbsXoSZ2w=; b=A M2xFjMFCHi0+cn+Ont6P5Xx/0zfrXSozDZPxpFSbgE9pLHZ1A9+MOMVcSmhPBF6m bvfKfPFxaeIVgdiuELhpmNr0DIjw0mTo85fWKE21eBf2QQbFqVVMbQ58D5FIS+jn 2H43dLcRNScKASbQpyp6Zxwyao24miuLA/xtnfvL+MnXuUjiOTQyI9TGgGv73qSd /ubXwfyXW+QzDxjXKcBBTFonUqxgkCDSj+/ZE5m7ZZU1QlBQi9Vi0owIX068LLZd 6ofWNdHD5PGYMyAWCZru6e/t/JSU4cXJVTpGXC3Z2l86YMzV3rcXE7UpEM6o0KF3 0rXTJQyd0Uaeqhn6oVJ7g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type: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=fm2; t=1687820552; x= 1687906952; bh=n503uRHlm7PSoiNPrcOUsA2hVWcbKqQi9TGbsXoSZ2w=; b=a Igs5mBnvWzzmmvZ2NAc8WDkZJN2kP4/Ud0pu+UrjvkPt/bl/5NpZjM/pe8pAnEp3 5lF0pw06YMCzuWOCYLTTJNHb/h3z9Wj8qyoLnJ7UcoMYPLnranQ28WJZ9WtZclEi 7FYm6yE41TJkJPae0Cyzgwi7Kst5T6QPxQ3nyHHZJUN7LYC7/JX4PqEbhoNaqjX5 SqY9Yl4Jq8LDaxHbu72ZDIYEDy/gmLyc38ERU+XZVYFd5QRCmbkn/HvVx1+IbDTp 6MVn9toKEMfelcebY1x1UlNPafLqVJKRWLS+wDUVdGU5YFxcJbigzatjdpTbEQlY Xs85/BnO7wARFiuUoF1DA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrgeehgedgudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdljedtmdenucfjughrpefhvf evufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghnihgvlhcuighuuceo ugiguhesugiguhhuuhdrgiihiieqnecuggftrfgrthhtvghrnhepgfefgfegjefhudeike dvueetffelieefuedvhfehjeeljeejkefgffeghfdttdetnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhuuhdrgiihii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 26 Jun 2023 19:02:30 -0400 (EDT) From: Daniel Xu To: daniel@iogearbox.net, ast@kernel.org, shuah@kernel.org, andrii@kernel.org, fw@strlen.de Cc: martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, mykolal@fb.com, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, netfilter-devel@vger.kernel.org, dsahern@kernel.org Subject: [PATCH bpf-next 6/7] bpf: selftests: Support custom type and proto for client sockets Date: Mon, 26 Jun 2023 17:02:13 -0600 Message-Id: <548b1c471bf0993a68ffc0fe29fd283825a361b6.1687819413.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.40.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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE 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?1769808621124501198?= X-GMAIL-MSGID: =?utf-8?q?1769808621124501198?= Extend connect_to_fd_opts() to take optional type and protocol parameters for the client socket. These parameters are useful when opening a raw socket to send IP fragments. Signed-off-by: Daniel Xu --- tools/testing/selftests/bpf/network_helpers.c | 21 +++++++++++++------ tools/testing/selftests/bpf/network_helpers.h | 2 ++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index d5c78c08903b..910d5d0470e6 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -270,14 +270,23 @@ int connect_to_fd_opts(int server_fd, const struct network_helper_opts *opts) opts = &default_opts; optlen = sizeof(type); - if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) { - log_err("getsockopt(SOL_TYPE)"); - return -1; + + if (opts->type) { + type = opts->type; + } else { + if (getsockopt(server_fd, SOL_SOCKET, SO_TYPE, &type, &optlen)) { + log_err("getsockopt(SOL_TYPE)"); + return -1; + } } - if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) { - log_err("getsockopt(SOL_PROTOCOL)"); - return -1; + if (opts->proto) { + protocol = opts->proto; + } else { + if (getsockopt(server_fd, SOL_SOCKET, SO_PROTOCOL, &protocol, &optlen)) { + log_err("getsockopt(SOL_PROTOCOL)"); + return -1; + } } addrlen = sizeof(addr); diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index 87894dc984dd..5eccc67d1a99 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -22,6 +22,8 @@ struct network_helper_opts { int timeout_ms; bool must_fail; bool noconnect; + int type; + int proto; }; /* ipv4 test vector */ From patchwork Mon Jun 26 23:02:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 113151 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp7813379vqr; Mon, 26 Jun 2023 16:12:00 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6x9bVKmhA4qhY/7ANCJgF5D6n0hjhRQD54JvNty9Jks0rsgt2ypeggwHrewSdN7PuUpB6h X-Received: by 2002:a17:907:31c1:b0:988:882b:9a3 with SMTP id xf1-20020a17090731c100b00988882b09a3mr21283671ejb.36.1687821120533; Mon, 26 Jun 2023 16:12:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687821120; cv=none; d=google.com; s=arc-20160816; b=VC29omNdy6iigpHYOG03Ilp59nNVyc86IGxxow/9AE5RWBs6SvIS0bS4PteEnm/PyZ FkM2NCPWLgixZUdBNqjD1FhC/ae2Z/WWkAy/34IWGbDpduh+5XgKd2nRbQj6YFE1c8Xl ko92e9cl3w39tAQ7X3ieZ/azaLH/e7TpA6hkFe+1t6zybU9GMBby18jtYslOMWBJmCkp CTbM7/SLvlrZQv3+UMQF2dY0+Ok0/jXdljpxBhJ3PZCmu/xtnqOmDmwedd0ED39x2+Hn 1DnH5rikAr3KsJBFQWaljvEhjkkZJYbn9JNANGDRxJUIlBt81QkPBXHSV5HIPa4IpmA5 0rcw== 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=vEy7BCncCrDo0zMmxH8ZwmdBs9Bdxfo/kNUuhdT+M3c=; fh=yiZSFIoqxHYKipmay2Eh8F/gv0lSKSmn6bDNjRSn+qA=; b=EmvZrEmTxsNsBrRQbdP8U6u+oEN1pi6Ctf1E8jHwV0eU9/cByJbUA+R5Zt87FXfPFO RDrze8dtIIdIX/Q1v6aJgjPL/SfHZwK976nW1YF5P1O0cO2+FPlwwSE7kAKROeeb8Heq uv5t9X+c9CBge+5BX5o6KH2GZ3vYQxLqxKIpKDtordpXH+CuOJyfitTMcZIvzfItx/Hu IKitNJ/KndtuvBsBaMyRh9BjF1/HwRzUZOwFh+CppeKwpGNSqNEPdI9XyyCuFpCmuJnD kAvzQ3hNNtwOt/+nil9D5POrLq2SW6nGd7+hmocz+FiNLJo5mypWC6uA8S9GQrBg6uXX ZCXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm1 header.b=gkYhNlmZ; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b="LhV/4TGp"; 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 q16-20020a170906361000b0098ce5bd2ed0si3411665ejb.257.2023.06.26.16.11.35; Mon, 26 Jun 2023 16:12:00 -0700 (PDT) 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=fm1 header.b=gkYhNlmZ; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b="LhV/4TGp"; 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 S229952AbjFZXDI (ORCPT + 99 others); Mon, 26 Jun 2023 19:03:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229777AbjFZXCq (ORCPT ); Mon, 26 Jun 2023 19:02:46 -0400 Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F5D31734; Mon, 26 Jun 2023 16:02:35 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id C22B15C00D1; Mon, 26 Jun 2023 19:02:34 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 26 Jun 2023 19:02:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm1; t=1687820554; x= 1687906954; bh=vEy7BCncCrDo0zMmxH8ZwmdBs9Bdxfo/kNUuhdT+M3c=; b=g kYhNlmZg4GuZncG2oH6lVIycjcvF8+va9ZiAqb9EuR4k8TxO3sVl6ZeRD+9nAYfk NMakorbW3GLFlL9VQ57zw+GXf1Xjs83t7UTsJAIJr1p1EPIXQNFyrW6ByQNGT7O2 NMHKvr5bEJ30qZvuuWin5h0Ywf5AcoBbeob216avI/ZyoAbxuUxW5VWV7sSP8bL4 OFkMTiS7oK2jn0lhq/AOg5yTch5AZt3munMu9wJyHW+lQXM3qyiVyXg80scZD3+g n+KVTBvDizXCSFuYbmPr/8KtVxxmuDF17x0Pj9sD/dNeMwyUXuw+44FzJS/Rshhs 6DpcexbPtGuRtyj7yly2Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type: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=fm2; t=1687820554; x= 1687906954; bh=vEy7BCncCrDo0zMmxH8ZwmdBs9Bdxfo/kNUuhdT+M3c=; b=L hV/4TGpt31AA7vpdVxEq5yiyz2ObOWsgkuqVFNLSp9t3+r9/G8YEXVVE6gS4/xsO vRsrrafXO796XlrMbVx0BXBX+U0cZzGg5GHSQPYoF7NuCDGBimgOFFR3o9Hdh3ca uHl5i3v4C3klZH5DNJHpYpZlPGi1RzNkQfHLIWtyH6EsV1T1dGYutPVHetqzg9jx tjogYENkqUWMhYoIRK5hW9UwmoDkKIujeMh1w+haO0dKJtxob6Agpr8NHu+ieUOL PbOkgeEmgy2p8qEi4kmy5Sp9ErJCcnP6zfiS03iHkkMt5Mp6UjuoWCOEiIMrVNjZ chn9kY4A16fxDa2n9SM7A== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrgeehgedgudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdljedtmdenucfjughrpefhvf evufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpeffrghnihgvlhcuighuuceo ugiguhesugiguhhuuhdrgiihiieqnecuggftrfgrthhtvghrnhepgfefgfegjefhudeike dvueetffelieefuedvhfehjeeljeejkefgffeghfdttdetnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhuuhdrgiihii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 26 Jun 2023 19:02:32 -0400 (EDT) From: Daniel Xu To: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, shuah@kernel.org, fw@strlen.de Cc: martin.lau@linux.dev, song@kernel.org, yhs@fb.com, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, mykolal@fb.com, linux-kernel@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, netfilter-devel@vger.kernel.org, dsahern@kernel.org Subject: [PATCH bpf-next 7/7] bpf: selftests: Add defrag selftests Date: Mon, 26 Jun 2023 17:02:14 -0600 Message-Id: <16bc6545404f87cb852b4498fdbda4d6073d828d.1687819413.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.40.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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE 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?1769808719400092736?= X-GMAIL-MSGID: =?utf-8?q?1769808719400092736?= These selftests tests 2 major scenarios: the BPF based defragmentation can successfully be done and that packet pointers are invalidated after calls to the kfunc. The logic is similar for both ipv4 and ipv6. In the first scenario, we create a UDP client and UDP echo server. The the server side is fairly straightforward: we attach the prog and simply echo back the message. The on the client side, we send fragmented packets to and expect the reassembled message back from the server. Signed-off-by: Daniel Xu --- tools/testing/selftests/bpf/Makefile | 4 +- .../selftests/bpf/generate_udp_fragments.py | 90 ++++++ .../selftests/bpf/ip_check_defrag_frags.h | 57 ++++ .../bpf/prog_tests/ip_check_defrag.c | 282 ++++++++++++++++++ .../selftests/bpf/progs/ip_check_defrag.c | 104 +++++++ 5 files changed, 535 insertions(+), 2 deletions(-) create mode 100755 tools/testing/selftests/bpf/generate_udp_fragments.py create mode 100644 tools/testing/selftests/bpf/ip_check_defrag_frags.h create mode 100644 tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c create mode 100644 tools/testing/selftests/bpf/progs/ip_check_defrag.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index 538df8fb8c42..b47f20381d56 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -561,8 +561,8 @@ TRUNNER_EXTRA_SOURCES := test_progs.c cgroup_helpers.c trace_helpers.c \ network_helpers.c testing_helpers.c \ btf_helpers.c flow_dissector_load.h \ cap_helpers.c test_loader.c xsk.c disasm.c \ - json_writer.c unpriv_helpers.c - + json_writer.c unpriv_helpers.c \ + ip_check_defrag_frags.h TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \ $(OUTPUT)/liburandom_read.so \ $(OUTPUT)/xdp_synproxy \ diff --git a/tools/testing/selftests/bpf/generate_udp_fragments.py b/tools/testing/selftests/bpf/generate_udp_fragments.py new file mode 100755 index 000000000000..2b8a1187991c --- /dev/null +++ b/tools/testing/selftests/bpf/generate_udp_fragments.py @@ -0,0 +1,90 @@ +#!/bin/env python3 +# SPDX-License-Identifier: GPL-2.0 + +""" +This script helps generate fragmented UDP packets. + +While it is technically possible to dynamically generate +fragmented packets in C, it is much harder to read and write +said code. `scapy` is relatively industry standard and really +easy to read / write. + +So we choose to write this script that generates a valid C +header. Rerun script and commit generated file after any +modifications. +""" + +import argparse +import os + +from scapy.all import * + + +# These constants must stay in sync with `ip_check_defrag.c` +VETH1_ADDR = "172.16.1.200" +VETH0_ADDR6 = "fc00::100" +VETH1_ADDR6 = "fc00::200" +CLIENT_PORT = 48878 +SERVER_PORT = 48879 +MAGIC_MESSAGE = "THIS IS THE ORIGINAL MESSAGE, PLEASE REASSEMBLE ME" + + +def print_header(f): + f.write("// SPDX-License-Identifier: GPL-2.0\n") + f.write("/* DO NOT EDIT -- this file is generated */\n") + f.write("\n") + f.write("#ifndef _IP_CHECK_DEFRAG_FRAGS_H\n") + f.write("#define _IP_CHECK_DEFRAG_FRAGS_H\n") + f.write("\n") + f.write("#include \n") + f.write("\n") + + +def print_frags(f, frags, v6): + for idx, frag in enumerate(frags): + # 10 bytes per line to keep width in check + chunks = [frag[i : i + 10] for i in range(0, len(frag), 10)] + chunks_fmted = [", ".join([str(hex(b)) for b in chunk]) for chunk in chunks] + suffix = "6" if v6 else "" + + f.write(f"static uint8_t frag{suffix}_{idx}[] = {{\n") + for chunk in chunks_fmted: + f.write(f"\t{chunk},\n") + f.write(f"}};\n") + + +def print_trailer(f): + f.write("\n") + f.write("#endif /* _IP_CHECK_DEFRAG_FRAGS_H */\n") + + +def main(f): + # srcip of 0 is filled in by IP_HDRINCL + sip = "0.0.0.0" + sip6 = VETH0_ADDR6 + dip = VETH1_ADDR + dip6 = VETH1_ADDR6 + sport = CLIENT_PORT + dport = SERVER_PORT + payload = MAGIC_MESSAGE.encode() + + # Disable UDPv4 checksums to keep code simpler + pkt = IP(src=sip,dst=dip) / UDP(sport=sport,dport=dport,chksum=0) / Raw(load=payload) + # UDPv6 requires a checksum + # Also pin the ipv6 fragment header ID, otherwise it's a random value + pkt6 = IPv6(src=sip6,dst=dip6) / IPv6ExtHdrFragment(id=0xBEEF) / UDP(sport=sport,dport=dport) / Raw(load=payload) + + frags = [f.build() for f in pkt.fragment(24)] + frags6 = [f.build() for f in fragment6(pkt6, 72)] + + print_header(f) + print_frags(f, frags, False) + print_frags(f, frags6, True) + print_trailer(f) + + +if __name__ == "__main__": + dir = os.path.dirname(os.path.realpath(__file__)) + header = f"{dir}/ip_check_defrag_frags.h" + with open(header, "w") as f: + main(f) diff --git a/tools/testing/selftests/bpf/ip_check_defrag_frags.h b/tools/testing/selftests/bpf/ip_check_defrag_frags.h new file mode 100644 index 000000000000..70ab7e9fa22b --- /dev/null +++ b/tools/testing/selftests/bpf/ip_check_defrag_frags.h @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0 +/* DO NOT EDIT -- this file is generated */ + +#ifndef _IP_CHECK_DEFRAG_FRAGS_H +#define _IP_CHECK_DEFRAG_FRAGS_H + +#include + +static uint8_t frag_0[] = { + 0x45, 0x0, 0x0, 0x2c, 0x0, 0x1, 0x20, 0x0, 0x40, 0x11, + 0xac, 0xe8, 0x0, 0x0, 0x0, 0x0, 0xac, 0x10, 0x1, 0xc8, + 0xbe, 0xee, 0xbe, 0xef, 0x0, 0x3a, 0x0, 0x0, 0x54, 0x48, + 0x49, 0x53, 0x20, 0x49, 0x53, 0x20, 0x54, 0x48, 0x45, 0x20, + 0x4f, 0x52, 0x49, 0x47, +}; +static uint8_t frag_1[] = { + 0x45, 0x0, 0x0, 0x2c, 0x0, 0x1, 0x20, 0x3, 0x40, 0x11, + 0xac, 0xe5, 0x0, 0x0, 0x0, 0x0, 0xac, 0x10, 0x1, 0xc8, + 0x49, 0x4e, 0x41, 0x4c, 0x20, 0x4d, 0x45, 0x53, 0x53, 0x41, + 0x47, 0x45, 0x2c, 0x20, 0x50, 0x4c, 0x45, 0x41, 0x53, 0x45, + 0x20, 0x52, 0x45, 0x41, +}; +static uint8_t frag_2[] = { + 0x45, 0x0, 0x0, 0x1e, 0x0, 0x1, 0x0, 0x6, 0x40, 0x11, + 0xcc, 0xf0, 0x0, 0x0, 0x0, 0x0, 0xac, 0x10, 0x1, 0xc8, + 0x53, 0x53, 0x45, 0x4d, 0x42, 0x4c, 0x45, 0x20, 0x4d, 0x45, +}; +static uint8_t frag6_0[] = { + 0x60, 0x0, 0x0, 0x0, 0x0, 0x20, 0x2c, 0x40, 0xfc, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x1, 0x0, 0xfc, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, + 0x11, 0x0, 0x0, 0x1, 0x0, 0x0, 0xbe, 0xef, 0xbe, 0xee, + 0xbe, 0xef, 0x0, 0x3a, 0xd0, 0xf8, 0x54, 0x48, 0x49, 0x53, + 0x20, 0x49, 0x53, 0x20, 0x54, 0x48, 0x45, 0x20, 0x4f, 0x52, + 0x49, 0x47, +}; +static uint8_t frag6_1[] = { + 0x60, 0x0, 0x0, 0x0, 0x0, 0x20, 0x2c, 0x40, 0xfc, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x1, 0x0, 0xfc, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, + 0x11, 0x0, 0x0, 0x19, 0x0, 0x0, 0xbe, 0xef, 0x49, 0x4e, + 0x41, 0x4c, 0x20, 0x4d, 0x45, 0x53, 0x53, 0x41, 0x47, 0x45, + 0x2c, 0x20, 0x50, 0x4c, 0x45, 0x41, 0x53, 0x45, 0x20, 0x52, + 0x45, 0x41, +}; +static uint8_t frag6_2[] = { + 0x60, 0x0, 0x0, 0x0, 0x0, 0x12, 0x2c, 0x40, 0xfc, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x1, 0x0, 0xfc, 0x0, 0x0, 0x0, 0x0, 0x0, + 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, + 0x11, 0x0, 0x0, 0x30, 0x0, 0x0, 0xbe, 0xef, 0x53, 0x53, + 0x45, 0x4d, 0x42, 0x4c, 0x45, 0x20, 0x4d, 0x45, +}; + +#endif /* _IP_CHECK_DEFRAG_FRAGS_H */ diff --git a/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c b/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c new file mode 100644 index 000000000000..5cd08d6e0ebc --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/ip_check_defrag.c @@ -0,0 +1,282 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include "ip_check_defrag.skel.h" +#include "ip_check_defrag_frags.h" + +/* + * This selftest spins up a client and an echo server, each in their own + * network namespace. The client will send a fragmented message to the server. + * The prog attached to the server will shoot down any fragments. Thus, if + * the server is able to correctly echo back the message to the client, we will + * have verified that netfilter is reassembling packets for us. + * + * Topology: + * ========= + * NS0 | NS1 + * | + * client | server + * ---------- | ---------- + * | veth0 | --------- | veth1 | + * ---------- peer ---------- + * | + * | with bpf + */ + +#define NS0 "defrag_ns0" +#define NS1 "defrag_ns1" +#define VETH0 "veth0" +#define VETH1 "veth1" +#define VETH0_ADDR "172.16.1.100" +#define VETH0_ADDR6 "fc00::100" +/* The following constants must stay in sync with `generate_udp_fragments.py` */ +#define VETH1_ADDR "172.16.1.200" +#define VETH1_ADDR6 "fc00::200" +#define CLIENT_PORT 48878 +#define SERVER_PORT 48879 +#define MAGIC_MESSAGE "THIS IS THE ORIGINAL MESSAGE, PLEASE REASSEMBLE ME" + +static int setup_topology(bool ipv6) +{ + bool up; + int i; + + SYS(fail, "ip netns add " NS0); + SYS(fail, "ip netns add " NS1); + SYS(fail, "ip link add " VETH0 " netns " NS0 " type veth peer name " VETH1 " netns " NS1); + if (ipv6) { + SYS(fail, "ip -6 -net " NS0 " addr add " VETH0_ADDR6 "/64 dev " VETH0 " nodad"); + SYS(fail, "ip -6 -net " NS1 " addr add " VETH1_ADDR6 "/64 dev " VETH1 " nodad"); + } else { + SYS(fail, "ip -net " NS0 " addr add " VETH0_ADDR "/24 dev " VETH0); + SYS(fail, "ip -net " NS1 " addr add " VETH1_ADDR "/24 dev " VETH1); + } + SYS(fail, "ip -net " NS0 " link set dev " VETH0 " up"); + SYS(fail, "ip -net " NS1 " link set dev " VETH1 " up"); + + /* Wait for up to 5s for links to come up */ + for (i = 0; i < 5; ++i) { + if (ipv6) + up = !system("ip netns exec " NS0 " ping -6 -c 1 -W 1 " VETH1_ADDR6 " &>/dev/null"); + else + up = !system("ip netns exec " NS0 " ping -c 1 -W 1 " VETH1_ADDR " &>/dev/null"); + + if (up) + break; + } + + return 0; +fail: + return -1; +} + +static void cleanup_topology(void) +{ + SYS_NOFAIL("test -f /var/run/netns/" NS0 " && ip netns delete " NS0); + SYS_NOFAIL("test -f /var/run/netns/" NS1 " && ip netns delete " NS1); +} + +static int attach(struct ip_check_defrag *skel, bool ipv6) +{ + LIBBPF_OPTS(bpf_netfilter_opts, opts, + .pf = ipv6 ? NFPROTO_IPV6 : NFPROTO_IPV4, + .priority = 42, + .flags = BPF_F_NETFILTER_IP_DEFRAG); + struct nstoken *nstoken; + int err = -1; + + nstoken = open_netns(NS1); + + skel->links.defrag = bpf_program__attach_netfilter(skel->progs.defrag, &opts); + if (!ASSERT_OK_PTR(skel->links.defrag, "program attach")) + goto out; + + err = 0; +out: + close_netns(nstoken); + return err; +} + +static int send_frags(int client) +{ + struct sockaddr_storage saddr; + struct sockaddr *saddr_p; + socklen_t saddr_len; + int err; + + saddr_p = (struct sockaddr *)&saddr; + err = make_sockaddr(AF_INET, VETH1_ADDR, SERVER_PORT, &saddr, &saddr_len); + if (!ASSERT_OK(err, "make_sockaddr")) + return -1; + + err = sendto(client, frag_0, sizeof(frag_0), 0, saddr_p, saddr_len); + if (!ASSERT_GE(err, 0, "sendto frag_0")) + return -1; + + err = sendto(client, frag_1, sizeof(frag_1), 0, saddr_p, saddr_len); + if (!ASSERT_GE(err, 0, "sendto frag_1")) + return -1; + + err = sendto(client, frag_2, sizeof(frag_2), 0, saddr_p, saddr_len); + if (!ASSERT_GE(err, 0, "sendto frag_2")) + return -1; + + return 0; +} + +static int send_frags6(int client) +{ + struct sockaddr_storage saddr; + struct sockaddr *saddr_p; + socklen_t saddr_len; + int err; + + saddr_p = (struct sockaddr *)&saddr; + /* Port needs to be set to 0 for raw ipv6 socket for some reason */ + err = make_sockaddr(AF_INET6, VETH1_ADDR6, 0, &saddr, &saddr_len); + if (!ASSERT_OK(err, "make_sockaddr")) + return -1; + + err = sendto(client, frag6_0, sizeof(frag6_0), 0, saddr_p, saddr_len); + if (!ASSERT_GE(err, 0, "sendto frag6_0")) + return -1; + + err = sendto(client, frag6_1, sizeof(frag6_1), 0, saddr_p, saddr_len); + if (!ASSERT_GE(err, 0, "sendto frag6_1")) + return -1; + + err = sendto(client, frag6_2, sizeof(frag6_2), 0, saddr_p, saddr_len); + if (!ASSERT_GE(err, 0, "sendto frag6_2")) + return -1; + + return 0; +} + +void test_bpf_ip_check_defrag_ok(bool ipv6) +{ + struct network_helper_opts rx_opts = { + .timeout_ms = 1000, + .noconnect = true, + }; + struct network_helper_opts tx_ops = { + .timeout_ms = 1000, + .type = SOCK_RAW, + .proto = IPPROTO_RAW, + .noconnect = true, + }; + struct sockaddr_storage caddr; + struct ip_check_defrag *skel; + struct nstoken *nstoken; + int client_tx_fd = -1; + int client_rx_fd = -1; + socklen_t caddr_len; + int srv_fd = -1; + char buf[1024]; + int len, err; + + skel = ip_check_defrag__open_and_load(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + if (!ASSERT_OK(setup_topology(ipv6), "setup_topology")) + goto out; + + if (!ASSERT_OK(attach(skel, ipv6), "attach")) + goto out; + + /* Start server in ns1 */ + nstoken = open_netns(NS1); + if (!ASSERT_OK_PTR(nstoken, "setns ns1")) + goto out; + srv_fd = start_server(ipv6 ? AF_INET6 : AF_INET, SOCK_DGRAM, NULL, SERVER_PORT, 0); + close_netns(nstoken); + if (!ASSERT_GE(srv_fd, 0, "start_server")) + goto out; + + /* Open tx raw socket in ns0 */ + nstoken = open_netns(NS0); + if (!ASSERT_OK_PTR(nstoken, "setns ns0")) + goto out; + client_tx_fd = connect_to_fd_opts(srv_fd, &tx_ops); + close_netns(nstoken); + if (!ASSERT_GE(client_tx_fd, 0, "connect_to_fd_opts")) + goto out; + + /* Open rx socket in ns0 */ + nstoken = open_netns(NS0); + if (!ASSERT_OK_PTR(nstoken, "setns ns0")) + goto out; + client_rx_fd = connect_to_fd_opts(srv_fd, &rx_opts); + close_netns(nstoken); + if (!ASSERT_GE(client_rx_fd, 0, "connect_to_fd_opts")) + goto out; + + /* Bind rx socket to a premeditated port */ + memset(&caddr, 0, sizeof(caddr)); + nstoken = open_netns(NS0); + if (!ASSERT_OK_PTR(nstoken, "setns ns0")) + goto out; + if (ipv6) { + struct sockaddr_in6 *c = (struct sockaddr_in6 *)&caddr; + + c->sin6_family = AF_INET6; + inet_pton(AF_INET6, VETH0_ADDR6, &c->sin6_addr); + c->sin6_port = htons(CLIENT_PORT); + err = bind(client_rx_fd, (struct sockaddr *)c, sizeof(*c)); + } else { + struct sockaddr_in *c = (struct sockaddr_in *)&caddr; + + c->sin_family = AF_INET; + inet_pton(AF_INET, VETH0_ADDR, &c->sin_addr); + c->sin_port = htons(CLIENT_PORT); + err = bind(client_rx_fd, (struct sockaddr *)c, sizeof(*c)); + } + close_netns(nstoken); + if (!ASSERT_OK(err, "bind")) + goto out; + + /* Send message in fragments */ + if (ipv6) { + if (!ASSERT_OK(send_frags6(client_tx_fd), "send_frags6")) + goto out; + } else { + if (!ASSERT_OK(send_frags(client_tx_fd), "send_frags")) + goto out; + } + + if (!ASSERT_EQ(skel->bss->shootdowns, 0, "shootdowns")) + goto out; + + /* Receive reassembled msg on server and echo back to client */ + len = recvfrom(srv_fd, buf, sizeof(buf), 0, (struct sockaddr *)&caddr, &caddr_len); + if (!ASSERT_GE(len, 0, "server recvfrom")) + goto out; + len = sendto(srv_fd, buf, len, 0, (struct sockaddr *)&caddr, caddr_len); + if (!ASSERT_GE(len, 0, "server sendto")) + goto out; + + /* Expect reassembed message to be echoed back */ + len = recvfrom(client_rx_fd, buf, sizeof(buf), 0, NULL, NULL); + if (!ASSERT_EQ(len, sizeof(MAGIC_MESSAGE) - 1, "client short read")) + goto out; + +out: + if (client_rx_fd != -1) + close(client_rx_fd); + if (client_tx_fd != -1) + close(client_tx_fd); + if (srv_fd != -1) + close(srv_fd); + cleanup_topology(); + ip_check_defrag__destroy(skel); +} + +void test_bpf_ip_check_defrag(void) +{ + if (test__start_subtest("v4")) + test_bpf_ip_check_defrag_ok(false); + if (test__start_subtest("v6")) + test_bpf_ip_check_defrag_ok(true); +} diff --git a/tools/testing/selftests/bpf/progs/ip_check_defrag.c b/tools/testing/selftests/bpf/progs/ip_check_defrag.c new file mode 100644 index 000000000000..4259c6d59968 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/ip_check_defrag.c @@ -0,0 +1,104 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "vmlinux.h" +#include +#include +#include "bpf_tracing_net.h" + +#define NF_DROP 0 +#define NF_ACCEPT 1 +#define ETH_P_IP 0x0800 +#define ETH_P_IPV6 0x86DD +#define IP_MF 0x2000 +#define IP_OFFSET 0x1FFF +#define NEXTHDR_FRAGMENT 44 + +extern int bpf_dynptr_from_skb(struct sk_buff *skb, __u64 flags, + struct bpf_dynptr *ptr__uninit) __ksym; +extern void *bpf_dynptr_slice(const struct bpf_dynptr *ptr, uint32_t offset, + void *buffer, uint32_t buffer__sz) __ksym; + +volatile int shootdowns = 0; + +static bool is_frag_v4(struct iphdr *iph) +{ + int offset; + int flags; + + offset = bpf_ntohs(iph->frag_off); + flags = offset & ~IP_OFFSET; + offset &= IP_OFFSET; + offset <<= 3; + + return (flags & IP_MF) || offset; +} + +static bool is_frag_v6(struct ipv6hdr *ip6h) +{ + /* Simplifying assumption that there are no extension headers + * between fixed header and fragmentation header. This assumption + * is only valid in this test case. It saves us the hassle of + * searching all potential extension headers. + */ + return ip6h->nexthdr == NEXTHDR_FRAGMENT; +} + +static int handle_v4(struct sk_buff *skb) +{ + struct bpf_dynptr ptr; + u8 iph_buf[20] = {}; + struct iphdr *iph; + + if (bpf_dynptr_from_skb(skb, 0, &ptr)) + return NF_DROP; + + iph = bpf_dynptr_slice(&ptr, 0, iph_buf, sizeof(iph_buf)); + if (!iph) + return NF_DROP; + + /* Shootdown any frags */ + if (is_frag_v4(iph)) { + shootdowns++; + return NF_DROP; + } + + return NF_ACCEPT; +} + +static int handle_v6(struct sk_buff *skb) +{ + struct bpf_dynptr ptr; + struct ipv6hdr *ip6h; + u8 ip6h_buf[40] = {}; + + if (bpf_dynptr_from_skb(skb, 0, &ptr)) + return NF_DROP; + + ip6h = bpf_dynptr_slice(&ptr, 0, ip6h_buf, sizeof(ip6h_buf)); + if (!ip6h) + return NF_DROP; + + /* Shootdown any frags */ + if (is_frag_v6(ip6h)) { + shootdowns++; + return NF_DROP; + } + + return NF_ACCEPT; +} + +SEC("netfilter") +int defrag(struct bpf_nf_ctx *ctx) +{ + struct sk_buff *skb = ctx->skb; + + switch (bpf_ntohs(skb->protocol)) { + case ETH_P_IP: + return handle_v4(skb); + case ETH_P_IPV6: + return handle_v6(skb); + default: + return NF_ACCEPT; + } +} + +char _license[] SEC("license") = "GPL";