Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp1380611rdb; Mon, 4 Sep 2023 11:49:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGVTJw6Gytd6/EAKBdgoyhDmjnoAlEYxUr5G5G4f1EYgzH2tB/h7ItAVqbN/aj50uEWkiFs X-Received: by 2002:a19:6402:0:b0:500:b74b:e53 with SMTP id y2-20020a196402000000b00500b74b0e53mr6105993lfb.46.1693853348356; Mon, 04 Sep 2023 11:49:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693853348; cv=none; d=google.com; s=arc-20160816; b=ktj2D4nZmNsczEZdlOt/eBU+KnhlCrLAL6fYCBFvj1mAXXj41Eq8ICn9jD5U7nzt3C nxWvgseKMIh4KfXpem6QZ1kiW8uKyVoQ+C6YU4KhUzU0P86/At4GIShj/DhiLOEUjJ0l l4lvSUmkTKzLHf4wrPQUQ+Z0n6Vta6xetGIk+r4F8Mti7K0+k6X7Mw6aYR2TkUengXJr AGTWmQSgpxhd3Grw090ES8ZaCZmg866ZjcmzzCbQ0nX4fq9bWhMsb2KBJbqiRv1tWzWg XrKyEsokDf3OSrZ+i7LGn/Mn2WWahVovjnCWgjaTani89yoXZvleFJdKXXTF++tCPGLZ Nbig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=Ij0mQ5KJCH8NDxN1o5+Va6lMnt+ordkrGczVNVSC17s=; fh=xPUo8Tpx3Z8/3jGwPQJgKbrPpvwcRhjtirsBmOs8QN0=; b=ikSH8lvJYImVD90TB6fk1JVjSv3RYJ6mq3KSHrc29Om/zWn6fHqzSVWHBmtZS894QQ XxAHtxExtqy6SzK5gYi+ynxgby+xYvq8xGJ1zMBxwFCS09NPMEWW0xB3B2eIIhmlurwN 6V+IOBlwNWFshdug207eXTJqV/HBdnvBqyOnxqmjJ9nRypyfw+0oF2incVKn4cqves/k em8jg4KEOGIoBigNSfnDTfAlwU/vzG7FeMjx8UlnahMxC36ODPzZQ4H3aLYrXFI0ETPj gEXWHN7WFF8CuGh/pw92LLpusbMcN8pMDPVZWTpFFAt6idl1lsVNgSsUHx4CEAxCQ21E 4L4Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s14-20020a056402164e00b0052a0fd4885bsi6673417edx.639.2023.09.04.11.48.41; Mon, 04 Sep 2023 11:49:08 -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; 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 S233006AbjIDKtQ (ORCPT + 99 others); Mon, 4 Sep 2023 06:49:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230431AbjIDKtQ (ORCPT ); Mon, 4 Sep 2023 06:49:16 -0400 Received: from Chamillionaire.breakpoint.cc (Chamillionaire.breakpoint.cc [IPv6:2a0a:51c0:0:237:300::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 655ED19B; Mon, 4 Sep 2023 03:49:12 -0700 (PDT) Received: from fw by Chamillionaire.breakpoint.cc with local (Exim 4.92) (envelope-from ) id 1qd78a-0000sx-EP; Mon, 04 Sep 2023 12:48:56 +0200 Date: Mon, 4 Sep 2023 12:48:56 +0200 From: Florian Westphal To: David Wang <00107082@163.com> Cc: fw@strlen.de, Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , linux-kernel@vger.kernel.org, bpf@vger.kernel.org Subject: Re: [PATCH] samples/bpf: Add sample usage for BPF_PROG_TYPE_NETFILTER Message-ID: <20230904104856.GE11802@breakpoint.cc> References: <20230904102128.11476-1-00107082@163.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230904102128.11476-1-00107082@163.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,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 David Wang <00107082@163.com> wrote: > This sample code implements a simple ipv4 > blacklist via the new bpf type BPF_PROG_TYPE_NETFILTER, > which was introduced in 6.4. > > The bpf program drops package if destination ip address > hits a match in the map of type BPF_MAP_TYPE_LPM_TRIE, > > The userspace code would load the bpf program, > attach it to netfilter's FORWARD/OUTPUT hook, > and then write ip patterns into the bpf map. Thanks, I think its good to have this. > diff --git a/samples/bpf/netfilter_ip4_blacklist.bpf.c b/samples/bpf/netfilter_ip4_blacklist.bpf.c > new file mode 100644 > index 000000000000..d315d64fda7f > --- /dev/null > +++ b/samples/bpf/netfilter_ip4_blacklist.bpf.c > @@ -0,0 +1,62 @@ > +// SPDX-License-Identifier: GPL-2.0 > + > +#include "vmlinux.h" > +#include > + > + > +#define NF_DROP 0 > +#define NF_ACCEPT 1 If you are interested, you could send a patch for nf-next that makes the uapi headers expose this as enum, AFAIU that would make the verdict nanes available via vmlinux.h. > + /* search p->daddr in trie */ > + key.prefixlen = 32; > + key.data = p->daddr; > + pvalue = bpf_map_lookup_elem(&ipv4_lpm_map, &key); > + if (pvalue) { > + /* cat /sys/kernel/debug/tracing/trace_pipe */ > + bpf_printk("rule matched with %d...\n", *pvalue); If you are interested you could send a patch that adds a kfunc to nf_bpf_link that exposes nf_log_packet() to bpf. nf_log_packet has a terrible api, I suggest to have the kfunc take 'struct nf_hook_state *' instead of 6+ members of that struct as argument. Thanks for the example.