bpf: add netfilter program type
Subsystems (10)
Florian Westphal
maintainerv2 cover bpf: add netfilter program type
Add minimal support to hook bpf programs to netfilter hooks, e.g. PREROUTING or FORWARD.
For this the most relevant parts for registering a netfilter hook via the in-kernel api are exposed to userspace via bpf_link.
The new program type is 'tracing style', i.e. there is no context access rewrite done by verifier, the function argument (struct bpf_nf_ctx) isn't stable.
There is no support for direct packet access, dynptr api should be used instead.
With this its possible to build a small test program such as:
#include "vmlinux.h"
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;
SEC("netfilter")
int nf_test(struct bpf_nf_ctx *ctx)
{
struct nf_hook_state *state = ctx->state;
struct sk_buff *skb = ctx->skb;
const struct iphdr *iph, _iph;
const struct tcphdr *th, _th;
struct bpf_dynptr ptr;
if (bpf_dynptr_from_skb(skb, 0, &ptr))
return NF_DROP;
iph = bpf_dynptr_slice(&ptr, 0, &_iph, sizeof(_iph));
if (!iph)
return NF_DROP;
th = bpf_dynptr_slice(&ptr, iph->ihl << 2, &_th, sizeof(_th));
if (!th)
return NF_DROP;
bpf_printk("accept %x:%d->%x:%d, hook %d ifin %d\n", iph->saddr, bpf_ntohs(th->source), iph->daddr, bpf_ntohs(th->dest), state->hook, state->in->ifindex);
return NF_ACCEPT;
}Then, tail /sys/kernel/tracing/trace_pipe.
Changes since v1:
1. Don't fail to link when CONFIG_NETFILTER=n (build bot)
2. Use test_progs instead of test_verifier (Alexei)
Changes since last RFC version:
1. extend 'bpftool link show' to print prio/hooknum etc
2. extend 'nft list hooks' so it can print the bpf program id
3. Add an extra patch to artificially restrict bpf progs with same priority. Its fine from a technical pov but it will cause ordering issues (most recent one comes first). Can be removed later.
4. Add test_run support for netfilter prog type and a small extension to verifier tests to make sure we can't return verdicts like NF_STOLEN.
5. Alter the netfilter part of the bpf_link uapi struct:
- add flags/reserved members.
Not used here except returning errors when they are nonzero. Plan is to allow the bpf_link users to enable netfilter defrag or conntrack engine by setting feature flags at link create time in the future.
Let me know if there is anything missing that has to be addressed before this can be merged.
Thanks!
Florian Westphal (6):
bpf: add bpf_link support for BPF_NETFILTER programs
bpf: minimal support for programs hooked into netfilter framework
netfilter: nfnetlink hook: dump bpf prog id
netfilter: disallow bpf hook attachment at same priority
tools: bpftool: print netfilter link info
bpf: add test_run support for netfilter program type
include/linux/bpf.h | 3 + include/linux/bpf_types.h | 4 + include/linux/netfilter.h | 1 + include/net/netfilter/nf_bpf_link.h | 15 ++ include/uapi/linux/bpf.h | 15 ++ include/uapi/linux/netfilter/nfnetlink_hook.h | 20 +- kernel/bpf/btf.c | 6 + kernel/bpf/syscall.c | 6 + kernel/bpf/verifier.c | 3 + net/bpf/test_run.c | 140 +++++++++++++ net/core/filter.c | 1 + net/netfilter/Kconfig | 3 + net/netfilter/Makefile | 1 + net/netfilter/core.c | 12 ++ net/netfilter/nf_bpf_link.c | 190 ++++++++++++++++++ net/netfilter/nfnetlink_hook.c | 81 ++++++-- tools/bpf/bpftool/link.c | 24 +++ tools/include/uapi/linux/bpf.h | 15 ++ tools/lib/bpf/libbpf.c | 1 + .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_netfilter_retcode.c | 49 +++++ 21 files changed, 578 insertions(+), 14 deletions(-) create mode 100644 include/net/netfilter/nf_bpf_link.h create mode 100644 net/netfilter/nf_bpf_link.c create mode 100644 tools/testing/selftests/bpf/progs/verifier_netfilter_retcode.c
Signature · 1 line
2.39.2
Florian Westphal
maintainerPATCH v2 1/6 bpf: add bpf_link support for BPF_NETFILTER programs
Add bpf_link support skeleton. To keep this reviewable, no bpf program can be invoked yet, if a program is attached only a c-stub is called and not the actual bpf program.
Defaults to 'y' if both netfilter and bpf syscall are enabled in kconfig.
Uapi example usage:
union bpf_attr attr = { };
attr.link_create.prog_fd = progfd;
attr.link_create.attach_type = 0; /* unused */
attr.link_create.netfilter.pf = PF_INET;
attr.link_create.netfilter.hooknum = NF_INET_LOCAL_IN;
attr.link_create.netfilter.priority = -128;
err = bpf(BPF_LINK_CREATE, &attr, sizeof(attr));... this would attach progfd to ipv4:input hook.
Such hook gets removed automatically if the calling program exits.
BPF_NETFILTER program invocation is added in followup change.
NF_HOOK_OP_BPF enum will eventually be read from nfnetlink_hook, it allows to tell userspace which program is attached at the given hook when user runs 'nft hook list' command rather than just the priority and not-very-helpful 'this hook runs a bpf prog but I can't tell which one'.
Will also be used to disallow registration of two bpf programs with same priority in a followup patch.
---
v2: add stub so CONFIG_NETFILTER=n won't fail to link
include/linux/netfilter.h | 1 + include/net/netfilter/nf_bpf_link.h | 10 +++ include/uapi/linux/bpf.h | 15 ++++ kernel/bpf/syscall.c | 6 ++ net/netfilter/Kconfig | 3 + net/netfilter/Makefile | 1 + net/netfilter/nf_bpf_link.c | 121 ++++++++++++++++++++++++++++ 7 files changed, 157 insertions(+) create mode 100644 include/net/netfilter/nf_bpf_link.h create mode 100644 net/netfilter/nf_bpf_link.c
include/linux/netfilter.h +1 -0
@@ -80,6 +80,7 @@ typedef unsigned int nf_hookfn(void *priv,enum nf_hook_ops_type {NF_HOOK_OP_UNDEFINED ,NF_HOOK_OP_NF_TABLES , +NF_HOOK_OP_BPF , };struct nf_hook_ops {
include/net/netfilter/nf_bpf_link.h new file +10 -0
@@ -0,0 +1,10 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#if IS_ENABLED (CONFIG_NETFILTER_BPF_LINK ) +int bpf_nf_link_attach (const union bpf_attr *attr,struct bpf_prog *prog); +#else +static inline int bpf_nf_link_attach (const union bpf_attr *attr,struct bpf_prog *prog) +{ +return -EOPNOTSUPP ; +} +#endif
include/uapi/linux/bpf.h +15 -0
@@ -986,6 +986,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_LSM ,BPF_PROG_TYPE_SK_LOOKUP ,BPF_PROG_TYPE_SYSCALL ,/* a program that can execute syscalls */ + BPF_PROG_TYPE_NETFILTER, }; enum bpf_attach_type {
@@ -1050,6 +1051,7 @@ enum bpf_link_type {BPF_LINK_TYPE_PERF_EVENT =7 ,BPF_LINK_TYPE_KPROBE_MULTI =8 ,BPF_LINK_TYPE_STRUCT_OPS =9 , +BPF_LINK_TYPE_NETFILTER =10 , MAX_BPF_LINK_TYPE, };
@@ -1560,6 +1562,13 @@ union bpf_attr {*/ __u64 cookie; } tracing; +struct { +__u32 pf; +__u32 hooknum; +__s32 prio; +__u32 flags; +__u64 reserved[2 ]; + } netfilter; }; } link_create;
@@ -6410,6 +6419,12 @@ struct bpf_link_info {struct {__u32 map_id; } struct_ops; +struct { +__u32 pf; +__u32 hooknum; +__s32 priority; +__u32 flags; + } netfilter; }; } __attribute__((aligned(8)));
kernel/bpf/syscall.c +6 -0
@@ -35,6 +35,7 @@#include <linux/rcupdate_trace.h> #include <linux/memcontrol.h> #include <linux/trace_events.h> +#include <net/netfilter/nf_bpf_link.h> #define IS_FD_ARRAY (map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \ (map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
@@ -2472,6 +2473,7 @@ static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)case BPF_PROG_TYPE_CGROUP_SYSCTL :case BPF_PROG_TYPE_SOCK_OPS :case BPF_PROG_TYPE_EXT :/* extends any prog */ +case BPF_PROG_TYPE_NETFILTER :return true;case BPF_PROG_TYPE_CGROUP_SKB :/* always unpriv */
@@ -4598,6 +4600,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)switch (prog->type) {case BPF_PROG_TYPE_EXT : +case BPF_PROG_TYPE_NETFILTER :break ;case BPF_PROG_TYPE_PERF_EVENT :case BPF_PROG_TYPE_TRACEPOINT :
@@ -4664,6 +4667,9 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)case BPF_PROG_TYPE_XDP : ret =bpf_xdp_link_attach (attr, prog);break ; +case BPF_PROG_TYPE_NETFILTER : + ret =bpf_nf_link_attach (attr, prog); +break ;#endif case BPF_PROG_TYPE_PERF_EVENT :case BPF_PROG_TYPE_TRACEPOINT :
net/netfilter/Kconfig +3 -0
@@ -30,6 +30,9 @@ config NETFILTER_FAMILY_BRIDGE config NETFILTER_FAMILY_ARP bool +config NETFILTER_BPF_LINK + def_bool BPF_SYSCALL + config NETFILTER_NETLINK_HOOK tristate "Netfilter base hook dump support" depends on NETFILTER_ADVANCED
net/netfilter/Makefile +1 -0
@@ -22,6 +22,7 @@ nf_conntrack-$(CONFIG_DEBUG_INFO_BTF) += nf_conntrack_bpf.o endif obj-$(CONFIG_NETFILTER) = netfilter.o +obj-$(CONFIG_NETFILTER_BPF_LINK) += nf_bpf_link.o obj-$(CONFIG_NETFILTER_NETLINK) += nfnetlink.o obj-$(CONFIG_NETFILTER_NETLINK_ACCT) += nfnetlink_acct.o
net/netfilter/nf_bpf_link.c new file +121 -0
121 lines not on this page · raw message
Signature · 1 line
2.39.2
Florian Westphal
maintainerPATCH v2 2/6 bpf: minimal support for programs hooked into netfilter framework
In reply to Florian WestphalThis adds minimal support for BPF_PROG_TYPE_NETFILTER bpf programs that will be invoked via the NF_HOOK() points in the ip stack.
Invocation incurs an indirect call. This is not a necessity: Its possible to add 'DEFINE_BPF_DISPATCHER(nf_progs)' and handle the program invocation with the same method already done for xdp progs.
This isn't done here to keep the size of this chunk down.
Verifier restricts verdicts to either DROP or ACCEPT.
--- include/linux/bpf_types.h | 4 ++ include/net/netfilter/nf_bpf_link.h | 5 +++ kernel/bpf/btf.c | 6 +++ kernel/bpf/verifier.c | 3 ++ net/core/filter.c | 1 + net/netfilter/nf_bpf_link.c | 70 ++++++++++++++++++++++++++++- 6 files changed, 88 insertions(+), 1 deletion(-)
include/linux/bpf_types.h +4 -0
@@ -79,6 +79,10 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_LSM, lsm,#endif BPF_PROG_TYPE (BPF_PROG_TYPE_SYSCALL , bpf_syscall, void *, void *) +#ifdef CONFIG_NETFILTER +BPF_PROG_TYPE (BPF_PROG_TYPE_NETFILTER , netfilter, + struct bpf_nf_ctx, struct bpf_nf_ctx) +#endif BPF_MAP_TYPE(BPF_MAP_TYPE_ARRAY, array_map_ops)BPF_MAP_TYPE (BPF_MAP_TYPE_PERCPU_ARRAY , percpu_array_map_ops)
include/net/netfilter/nf_bpf_link.h +5 -0
@@ -1,5 +1,10 @@/* SPDX-License-Identifier: GPL-2.0 */ +struct bpf_nf_ctx { +const struct nf_hook_state *state; +struct sk_buff *skb; +}; +#if IS_ENABLED (CONFIG_NETFILTER_BPF_LINK )int bpf_nf_link_attach (const union bpf_attr *attr,struct bpf_prog *prog);#else
kernel/bpf/btf.c +6 -0
@@ -25,6 +25,9 @@#include <linux/bsearch.h> #include <linux/kobject.h> #include <linux/sysfs.h> + +#include <net/netfilter/nf_bpf_link.h> +#include <net/sock.h> #include "../tools/lib/bpf/relo_core.h"
@@ -212,6 +215,7 @@ enum btf_kfunc_hook {BTF_KFUNC_HOOK_SK_SKB ,BTF_KFUNC_HOOK_SOCKET_FILTER ,BTF_KFUNC_HOOK_LWT , +BTF_KFUNC_HOOK_NETFILTER , BTF_KFUNC_HOOK_MAX, };
@@ -7857,6 +7861,8 @@ static int bpf_prog_type_to_kfunc_hook(enum bpf_prog_type prog_type)case BPF_PROG_TYPE_LWT_XMIT :case BPF_PROG_TYPE_LWT_SEG6LOCAL :return BTF_KFUNC_HOOK_LWT ; +case BPF_PROG_TYPE_NETFILTER : +return BTF_KFUNC_HOOK_NETFILTER ;default :return BTF_KFUNC_HOOK_MAX ; }
kernel/bpf/verifier.c +3 -0
@@ -13799,6 +13799,9 @@ static int check_return_code(struct bpf_verifier_env *env) }break ; +case BPF_PROG_TYPE_NETFILTER : + range =tnum_range (NF_DROP ,NF_ACCEPT ); +break ;case BPF_PROG_TYPE_EXT :/* freplace program can return anything as its return value * depends on the to-be-replaced kernel func or bpf program.
net/core/filter.c +1 -0
@@ -11712,6 +11712,7 @@ static int __init bpf_kfunc_init(void) ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_LWT_IN , &bpf_kfunc_set_skb); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_LWT_XMIT , &bpf_kfunc_set_skb); ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_LWT_SEG6LOCAL , &bpf_kfunc_set_skb); + ret = ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_NETFILTER , &bpf_kfunc_set_skb);return ret ?:register_btf_kfunc_id_set (BPF_PROG_TYPE_XDP , &bpf_kfunc_set_xdp); }late_initcall (bpf_kfunc_init);
net/netfilter/nf_bpf_link.c +69 -1
84 lines not on this page · raw message
Signature · 1 line
2.39.2
Florian Westphal
maintainerPATCH v2 3/6 netfilter: nfnetlink hook: dump bpf prog id
In reply to Florian WestphalThis allows userspace ("nft list hooks") to show which bpf program is attached to which hook.
Without this, user only knows bpf prog is attached at prio x, y, z at INPUT and FORWARD, but can't tell which program is where.
--- include/uapi/linux/netfilter/nfnetlink_hook.h | 20 ++++- net/netfilter/nfnetlink_hook.c | 81 ++++++++++++++++--- 2 files changed, 87 insertions(+), 14 deletions(-)
include/uapi/linux/netfilter/nfnetlink_hook.h +19 -1
@@ -32,8 +32,12 @@ enum nfnl_hook_attributes {/** * enum nfnl_hook_chain_info_attributes - chain description * -* NFNLA_HOOK_INFO_DESC: nft chain and table name (enum nft_table_attributes) (NLA_NESTED) +* NFNLA_HOOK_INFO_DESC: nft chain and table name (NLA_NESTED) * NFNLA_HOOK_INFO_TYPE: chain type (enum nfnl_hook_chaintype) (NLA_U32) +* +* NFNLA_HOOK_INFO_DESC depends on NFNLA_HOOK_INFO_TYPE value: +* NFNL_HOOK_TYPE_NFTABLES: enum nft_table_attributes +* NFNL_HOOK_TYPE_BPF: enum nfnl_hook_bpf_info_attributes */ enum nfnl_hook_chain_info_attributes {NFNLA_HOOK_INFO_UNSPEC ,
@@ -56,9 +60,23 @@ enum nfnl_hook_chain_desc_attributes {* enum nfnl_hook_chaintype - chain type * * @NFNL_HOOK_TYPE_NFTABLES nf_tables base chain +* @NFNL_HOOK_TYPE_BPF bpf program */ enum nfnl_hook_chaintype {NFNL_HOOK_TYPE_NFTABLES =0x1 , +NFNL_HOOK_TYPE_BPF , +}; + +/** +* enum nfnl_hook_bpf_info_attributes - bpf prog description +* +* NFNLA_BPF_INFO_ID: bpf program id (NLA_U32) +*/ +enum nfnl_hook_bpf_attributes { +NFNLA_HOOK_BPF_UNSPEC , +NFNLA_HOOK_BPF_ID , + __NFNLA_HOOK_BPF_MAX, }; +#define NFNLA_HOOK_BPF_MAX (__NFNLA_HOOK_BPF_MAX - 1)#endif /* _NFNL_HOOK_H */
net/netfilter/nfnetlink_hook.c +68 -13
116 lines not on this page · raw message
Signature · 1 line
2.39.2
Florian Westphal
maintainerPATCH v2 4/6 netfilter: disallow bpf hook attachment at same priority
In reply to Florian WestphalThis is just to avoid ordering issues between multiple bpf programs, this could be removed later in case it turns out to be too cautious.
bpf prog could still be shared with non-bpf hook, otherwise we'd have to make conntrack hook registration fail just because a bpf program has same priority.
--- net/netfilter/core.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
net/netfilter/core.c +12 -0
@@ -119,6 +119,18 @@ nf_hook_entries_grow(const struct nf_hook_entries *old,for (i =0 ; i < old_entries; i++) {if (orig_ops[i] != &dummy_ops) alloc_entries++; + +/* Restrict BPF hook type to force a unique priority, not +* shared at attach time. +* +* This is mainly to avoid ordering issues between two +* different bpf programs, this doesn't prevent a normal +* hook at same priority as a bpf one (we don't want to +* prevent defrag, conntrack, iptables etc from attaching). +*/ +if (reg->priority == orig_ops[i]->priority && + reg->hook_ops_type ==NF_HOOK_OP_BPF ) +return ERR_PTR (-EBUSY ); } }
Signature · 1 line
2.39.2
Florian Westphal
maintainerPATCH v2 5/6 tools: bpftool: print netfilter link info
In reply to Florian WestphalDump protocol family, hook and priority value:
$ bpftool link
2: type 10 prog 20
pf: 2, hook 1, prio -128--- tools/bpf/bpftool/link.c | 24 ++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 15 +++++++++++++++ tools/lib/bpf/libbpf.c | 1 + 3 files changed, 40 insertions(+)
tools/bpf/bpftool/link.c +24 -0
@@ -135,6 +135,18 @@ static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr) } } +static void show_netfilter_json (const struct bpf_link_info *info,json_writer_t *wtr) +{ +jsonw_uint_field (json_wtr,"pf" , + info->netfilter.pf); +jsonw_uint_field (json_wtr,"hook" , + info->netfilter.hooknum); +jsonw_int_field (json_wtr,"prio" , + info->netfilter.priority); +jsonw_uint_field (json_wtr,"flags" , + info->netfilter.flags); +} +static int get_prog_info (int prog_id,struct bpf_prog_info *info) {__u32 len =sizeof (*info);
@@ -195,6 +207,10 @@ static int show_link_close_json(int fd, struct bpf_link_info *info) info->netns.netns_ino);show_link_attach_type_json (info->netns.attach_type, json_wtr);break ; +case BPF_LINK_TYPE_NETFILTER : +show_netfilter_json (info, json_wtr); +break ; +default :break ; }
@@ -301,6 +317,14 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)printf ("\n\tnetns_ino %u " , info->netns.netns_ino);show_link_attach_type_plain (info->netns.attach_type);break ; +case BPF_LINK_TYPE_NETFILTER : +printf ("\n\tpf: %d, hook %u, prio %d" , + info->netfilter.pf, + info->netfilter.hooknum, + info->netfilter.priority); +if (info->netfilter.flags) +printf (" flags 0x%x" , info->netfilter.flags); +break ;default :break ; }
tools/include/uapi/linux/bpf.h +15 -0
@@ -986,6 +986,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_LSM ,BPF_PROG_TYPE_SK_LOOKUP ,BPF_PROG_TYPE_SYSCALL ,/* a program that can execute syscalls */ + BPF_PROG_TYPE_NETFILTER, }; enum bpf_attach_type {
@@ -1050,6 +1051,7 @@ enum bpf_link_type {BPF_LINK_TYPE_PERF_EVENT =7 ,BPF_LINK_TYPE_KPROBE_MULTI =8 ,BPF_LINK_TYPE_STRUCT_OPS =9 , +BPF_LINK_TYPE_NETFILTER =10 , MAX_BPF_LINK_TYPE, };
@@ -1560,6 +1562,13 @@ union bpf_attr {*/ __u64 cookie; } tracing; +struct { +__u32 pf; +__u32 hooknum; +__s32 prio; +__u32 flags; +__u64 reserved[2 ]; + } netfilter; }; } link_create;
@@ -6410,6 +6419,12 @@ struct bpf_link_info {struct {__u32 map_id; } struct_ops; +struct { +__u32 pf; +__u32 hooknum; +__s32 priority; +__u32 flags; + } netfilter; }; } __attribute__((aligned(8)));
tools/lib/bpf/libbpf.c +1 -0
@@ -8641,6 +8641,7 @@ static const struct bpf_sec_def section_defs[] = {SEC_DEF ("struct_ops+" ,STRUCT_OPS ,0 ,SEC_NONE ),SEC_DEF ("struct_ops.s+" ,STRUCT_OPS ,0 ,SEC_SLEEPABLE ),SEC_DEF ("sk_lookup" ,SK_LOOKUP ,BPF_SK_LOOKUP ,SEC_ATTACHABLE ), + SEC_DEF("netfilter", NETFILTER, 0, SEC_NONE), };static size_t custom_sec_def_cnt;
Signature · 1 line
2.39.2
Re: [PATCH bpf-next v2 5/6] tools: bpftool: print netfilter link info
Hi Florian,
Quoted 4 lines · Florian Westphal
Dump protocol family, hook and priority value:
$ bpftool link 2: type 10 prog 20
Could you please update link_type_name in libbpf (libbpf.c) so that we display "netfilter" here instead of "type 10"?
Quoted 63 lines
pf: 2, hook 1, prio -128Signed-off-by Florian Westphal <[email protected]>--- tools/bpf/bpftool/link.c | 24 ++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 15 +++++++++++++++ tools/lib/bpf/libbpf.c | 1 + 3 files changed, 40 insertions(+)tools/bpf/bpftool/link.c +24
-0@@ -135,6 +135,18 @@ static void show_iter_json(struct bpf_link_info *info, json_writer_t *wtr) } } +static void show_netfilter_json (const struct bpf_link_info *info,json_writer_t *wtr) +{ +jsonw_uint_field (json_wtr,"pf" , + info->netfilter.pf); +jsonw_uint_field (json_wtr,"hook" , + info->netfilter.hooknum); +jsonw_int_field (json_wtr,"prio" , + info->netfilter.priority); +jsonw_uint_field (json_wtr,"flags" , + info->netfilter.flags); +} +static int get_prog_info (int prog_id,struct bpf_prog_info *info) {__u32 len =sizeof (*info);@@ -195,6 +207,10 @@ static int show_link_close_json(int fd, struct bpf_link_info *info) info->netns.netns_ino);show_link_attach_type_json (info->netns.attach_type, json_wtr);break ; +case BPF_LINK_TYPE_NETFILTER : +show_netfilter_json (info, json_wtr); +break ; +default :break ; }@@ -301,6 +317,14 @@ static int show_link_close_plain(int fd, struct bpf_link_info *info)printf ("\n\tnetns_ino %u " , info->netns.netns_ino);show_link_attach_type_plain (info->netns.attach_type);break ; +case BPF_LINK_TYPE_NETFILTER : +printf ("\n\tpf: %d, hook %u, prio %d" , + info->netfilter.pf, + info->netfilter.hooknum, + info->netfilter.priority); +if (info->netfilter.flags) +printf (" flags 0x%x" , info->netfilter.flags); +break ;default :break ; }tools/include/uapi/linux/bpf.h +0
-0
tools/include/uapi/linux/bpf.h enum bpf_prog_type { · line 989
Quoted 4 lines
tools/include/uapi/linux/bpf.h +1
-0@@ -986,6 +986,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_LSM ,BPF_PROG_TYPE_SK_LOOKUP ,BPF_PROG_TYPE_SYSCALL ,/* a program that can execute syscalls */ + BPF_PROG_TYPE_NETFILTER,
If netfilter programs could be loaded with bpftool, we'd need to update bpftool's docs. But I don't think this is the case, right? We don't currently have a way to pass the pf, hooknum, priority and flags necessary to load the program with "bpftool prog load" so it would fail?
Have you considered listing netfilter programs in the output of "bpftool net" as well? Given that they're related to networking, it would maybe make sense to have them listed alongside XDP, TC, and flow dissector programs?
The patch looks good to me otherwise.
Thanks,
Quentin
Florian Westphal
maintainerRe: [PATCH bpf-next v2 5/6] tools: bpftool: print netfilter link info
Quoted 8 lines · Quentin Monnet
Quoted 4 lines · Florian Westphal
Dump protocol family, hook and priority value:
$ bpftool link 2: type 10 prog 20Could you please update
link_type_namein libbpf (libbpf.c) so that we display "netfilter" here instead of "type 10"?
Done.
Quoted 4 lines
Quoted 4 lines
tools/include/uapi/linux/bpf.h +0
-0
Quoted 8 lines
tools/include/uapi/linux/bpf.h enum bpf_prog_type { · line 989
Quoted 5 lines
tools/include/uapi/linux/bpf.h +1
-0@@ -986,6 +986,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_LSM ,BPF_PROG_TYPE_SK_LOOKUP ,BPF_PROG_TYPE_SYSCALL ,/* a program that can execute syscalls */ + BPF_PROG_TYPE_NETFILTER,If netfilter programs could be loaded with bpftool, we'd need to update bpftool's docs. But I don't think this is the case, right?
bpftool prog load nftest.o /sys/fs/bpf/nftest
will work, but the program isn't attached anywhere.
Quoted 3 lines
don't currently have a way to pass the pf, hooknum, priority and flags necessary to load the program with "bpftool prog load" so it would fail?
I don't know how to make it work to actually attach it, because the hook is unregistered when the link fd is closed.
So either bpftool would have to fork and auto-daemon (maybe unexpected...) or wait/block until CTRL-C.
This also needs new libbpf api AFAICS because existing bpf_link are specific to the program type, so I'd have to add something like:
struct bpf_link *
bpf_program__attach_netfilter(const struct bpf_program *prog,
const struct bpf_netfilter_opts *opts)
Advice welcome.
Quoted 4 lines
Have you considered listing netfilter programs in the output of "bpftool net" as well? Given that they're related to networking, it would maybe make sense to have them listed alongside XDP, TC, and flow dissector programs?
I could print the same output that 'bpf link' already shows.
Not sure on the real distinction between those two here.
When should I use 'bpftool link' and when 'bpftool net', and what info and features should either of these provide for netfilter programs?
Re: [PATCH bpf-next v2 5/6] tools: bpftool: print netfilter link info
2023-04-14 12:41 UTC+0200 ~ Florian Westphal <[email protected]>
Quoted 11 lines
Quoted 8 lines · Quentin Monnet
Quoted 4 lines · Florian Westphal
Dump protocol family, hook and priority value:
$ bpftool link 2: type 10 prog 20Could you please update
link_type_namein libbpf (libbpf.c) so that we display "netfilter" here instead of "type 10"?Done.
Thanks!
I'm just thinking we could also maybe print something nicer for the pf and the hook, "NF_INET_LOCAL_IN" would be more user-friendly than "hook 1"?
Quoted 14 lines
Quoted 13 lines
tools/include/uapi/linux/bpf.h enum bpf_prog_type { · line 989
Quoted 10 lines
tools/include/uapi/linux/bpf.h +1
-0@@ -986,6 +986,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_LSM ,BPF_PROG_TYPE_SK_LOOKUP ,BPF_PROG_TYPE_SYSCALL ,/* a program that can execute syscalls */ + BPF_PROG_TYPE_NETFILTER,If netfilter programs could be loaded with bpftool, we'd need to update bpftool's docs. But I don't think this is the case, right?
Quoted 3 lines
bpftool prog load nftest.o
/sys/fs/bpf/nftestwill work, but the program isn't attached anywhere.
Let's maybe not document it, then. It may still be useful to check whether a program load, but users would definitely expect the program to remain loaded after bpftool invocation has completed. Or alternatively, we could document, but print a warning.
Quoted 11 lines
Quoted 4 lines
don't currently have a way to pass the pf, hooknum, priority and flags necessary to load the program with "bpftool prog load" so it would fail?
I don't know how to make it work to actually attach it, because the hook is unregistered when the link fd is closed.
So either bpftool would have to fork and auto-daemon (maybe unexpected...) or wait/block until CTRL-C.
Quoted 8 lines
This also needs new libbpf api AFAICS because existing
bpf_linkare specific to the program type, so I'd have to add something like:struct
bpf_link*
bpf_program__attach_netfilter(const structbpf_program*prog,
const structbpf_netfilter_opts*opts)Advice welcome.
OK, yes we'd need something like this if we wanted to load and attach from bpftool. If you already have the tooling elsewhere, it's maybe not necessary to add it here. Depends if you want users to be able to attach netfilter programs with bpftool or even libbpf.
There are other program types that are not supported for loading/attaching with bpftool (the bpftool-prog man page is not always correct in that regard, I think).
I'd say let's keep this out of the current patchset anyway. If we have a use case for attaching via libbpf/bpftool we can do this as a follow-up.
Quoted 9 lines
Quoted 5 lines
Have you considered listing netfilter programs in the output of "bpftool net" as well? Given that they're related to networking, it would maybe make sense to have them listed alongside XDP, TC, and flow dissector programs?
I could print the same output that 'bpf link' already shows.
Not sure on the real distinction between those two here.
There would probably be some overlap (to say the least), yes.
Quoted 3 lines
When should I use 'bpftool link' and when 'bpftool net', and what info and features should either of these provide for netfilter programs?
That's a good question. I thought I'd check how we handle it for XDP for "bpftool net" vs. "bpftool link", but I realised this link type (and some others) were never added to the switch/case you update in bpftool/link.c, and we're not printing any particular information about them beyond type and associated program id. Conversely, I'd have to check whether we print XDP programs using links in "bpftool net". Maybe some things to improve here. Anyway.
The way I see it, "bpftool net" should provide a more structured overview of the different programs affecting networking, in particular for JSON. The idea would be to display all BPF programs that can affect packet processing. See what we have for XDP for example:
# bpftool net -p
[{
"xdp": [{
"devname": "eni88np1",
"ifindex": 12,
"multi_attachments": [{
"mode": "driver",
"id": 1238
},{
"mode": "offload",
"id": 1239
}
]
}
],
"tc": [{
"devname": "eni88np1",
"ifindex": 12,
"kind": "clsact/ingress",
"name": "sample_ret0.o:[.text]",
"id": 1241
},{
"devname": "eni88np1",
"ifindex": 12,
"kind": "clsact/ingress",
"name": "sample_ret0.o:[.text]",
"id": 1240
}
],
"flow_dissector": [
"id": 1434
]
}
]This gives us all the info about XDP programs at once, grouped by device when relevant. By contrast, listing them in "bpftool link" would likely only show one at a time, in an uncorrelated manner. Similarly, we could have netfilter sorted by pf then hook in "bpftool net". If there's more relevant info that we get from program info and not from the netfilter link, this would also be a good place to have it (but not sure there's any info we're missing from "bpftool link"?).
But given that the info will be close, or identical, if not for the JSON structure, I don't mean to impose this to you - it's also OK to just skip "bpftool net" for now if you prefer.
Quentin
Florian Westphal
maintainerRe: [PATCH bpf-next v2 5/6] tools: bpftool: print netfilter link info
Quoted 11 lines · Quentin Monnet
2023-04-14 12:41 UTC+0200 ~ Florian Westphal <[email protected]>
Quoted 10 lines
Quoted 8 lines · Quentin Monnet
Quoted 4 lines · Florian Westphal
Dump protocol family, hook and priority value:
$ bpftool link 2: type 10 prog 20Could you please update
link_type_namein libbpf (libbpf.c) so that we display "netfilter" here instead of "type 10"?
Quoted 6 lines
Quoted 1 line
Done.
Thanks!
I'm just thinking we could also maybe print something nicer for the pf and the hook, "
NF_INET_LOCAL_IN" would be more user-friendly than "hook 1"?
Done. I've also made the first patch more restrictive wrt. allowed attachment points and priorities.
Better safe than sorry, we can be more liberal later if there are use-cases.
v3 will be coming next week.
Quoted 19 lines
Quoted 6 lines
I don't know how to make it work to actually attach it, because the hook is unregistered when the link fd is closed.
So either bpftool would have to fork and auto-daemon (maybe unexpected...) or wait/block until CTRL-C.
Quoted 8 lines
This also needs new libbpf api AFAICS because existing
bpf_linkare specific to the program type, so I'd have to add something like:struct
bpf_link*
bpf_program__attach_netfilter(const structbpf_program*prog,
const structbpf_netfilter_opts*opts)Advice welcome.
OK, yes we'd need something like this if we wanted to load and attach from bpftool. If you already have the tooling elsewhere, it's maybe not necessary to add it here. Depends if you want users to be able to attach netfilter programs with bpftool or even libbpf.
[..]
Quoted 2 lines
I'd say let's keep this out of the current patchset anyway. If we have a use case for attaching via libbpf/bpftool we can do this as a follow-up.
Sounds good to me.
Quentin Deslandes or Daniel Xu might want/need libbpf support for their projects.
Quoted 41 lines
The way I see it, "bpftool net" should provide a more structured overview of the different programs affecting networking, in particular for JSON. The idea would be to display all BPF programs that can affect packet processing. See what we have for XDP for example:
# bpftool net -p [{ "xdp": [{ "devname": "eni88np1", "ifindex": 12, "multi_attachments": [{ "mode": "driver", "id": 1238 },{ "mode": "offload", "id": 1239 } ] } ], "tc": [{ "devname": "eni88np1", "ifindex": 12, "kind": "clsact/ingress", "name": "sample_ret0.o:[.text]", "id": 1241 },{ "devname": "eni88np1", "ifindex": 12, "kind": "clsact/ingress", "name": "sample_ret0.o:[.text]", "id": 1240 } ], "flow_dissector": [ "id": 1434 ] } ]
Quoted 7 lines
This gives us all the info about XDP programs at once, grouped by device when relevant. By contrast, listing them in "bpftool link" would likely only show one at a time, in an uncorrelated manner. Similarly, we could have netfilter sorted by pf then hook in "bpftool net". If there's more relevant info that we get from program info and not from the netfilter link, this would also be a good place to have it (but not sure there's any info we're missing from "bpftool link"?).
Currently 'bpftool link' shows everything wrt. netfilter bpf programs.
Quoted 3 lines
But given that the info will be close, or identical, if not for the JSON structure, I don't mean to impose this to you - it's also OK to just skip "bpftool net" for now if you prefer.
I'll probably make 'bpftool net' and 'bpftool link' print identical netfilter output, I'll check this on Monday (to make sure the formatting doesn't seem out of place).
Its kinda silly to not have anything netfilter related in 'bpftool net', this thing isn't named 'linkfilter' after all 8-)
Re: [PATCH bpf-next v2 5/6] tools: bpftool: print netfilter link info
2023-04-14 16:49 UTC+0200 ~ Florian Westphal <[email protected]>
Quoted 112 lines
Quoted 11 lines · Quentin Monnet
2023-04-14 12:41 UTC+0200 ~ Florian Westphal <[email protected]>
Quoted 10 lines
Quoted 8 lines · Quentin Monnet
Quoted 4 lines · Florian Westphal
Dump protocol family, hook and priority value:
$ bpftool link 2: type 10 prog 20Could you please update
link_type_namein libbpf (libbpf.c) so that we display "netfilter" here instead of "type 10"?Quoted 6 lines
Quoted 1 line
Done.
Thanks!
I'm just thinking we could also maybe print something nicer for the pf and the hook, "
NF_INET_LOCAL_IN" would be more user-friendly than "hook 1"?Done. I've also made the first patch more restrictive wrt. allowed attachment points and priorities.
Better safe than sorry, we can be more liberal later if there are use-cases.
v3 will be coming next week.
Quoted 19 lines
Quoted 6 lines
I don't know how to make it work to actually attach it, because the hook is unregistered when the link fd is closed.
So either bpftool would have to fork and auto-daemon (maybe unexpected...) or wait/block until CTRL-C.
Quoted 8 lines
This also needs new libbpf api AFAICS because existing
bpf_linkare specific to the program type, so I'd have to add something like:struct
bpf_link*
bpf_program__attach_netfilter(const structbpf_program*prog,
const structbpf_netfilter_opts*opts)Advice welcome.
OK, yes we'd need something like this if we wanted to load and attach from bpftool. If you already have the tooling elsewhere, it's maybe not necessary to add it here. Depends if you want users to be able to attach netfilter programs with bpftool or even libbpf.
[..]
Quoted 2 lines
I'd say let's keep this out of the current patchset anyway. If we have a use case for attaching via libbpf/bpftool we can do this as a follow-up.
Sounds good to me.
Quentin Deslandes or Daniel Xu might want/need libbpf support for their projects.
Quoted 41 lines
The way I see it, "bpftool net" should provide a more structured overview of the different programs affecting networking, in particular for JSON. The idea would be to display all BPF programs that can affect packet processing. See what we have for XDP for example:
# bpftool net -p [{ "xdp": [{ "devname": "eni88np1", "ifindex": 12, "multi_attachments": [{ "mode": "driver", "id": 1238 },{ "mode": "offload", "id": 1239 } ] } ], "tc": [{ "devname": "eni88np1", "ifindex": 12, "kind": "clsact/ingress", "name": "sample_ret0.o:[.text]", "id": 1241 },{ "devname": "eni88np1", "ifindex": 12, "kind": "clsact/ingress", "name": "sample_ret0.o:[.text]", "id": 1240 } ], "flow_dissector": [ "id": 1434 ] } ]Quoted 7 lines
This gives us all the info about XDP programs at once, grouped by device when relevant. By contrast, listing them in "bpftool link" would likely only show one at a time, in an uncorrelated manner. Similarly, we could have netfilter sorted by pf then hook in "bpftool net". If there's more relevant info that we get from program info and not from the netfilter link, this would also be a good place to have it (but not sure there's any info we're missing from "bpftool link"?).
Currently 'bpftool link' shows everything wrt. netfilter bpf programs.
Quoted 3 lines
But given that the info will be close, or identical, if not for the JSON structure, I don't mean to impose this to you - it's also OK to just skip "bpftool net" for now if you prefer.
Quoted 6 lines
I'll probably make 'bpftool net' and 'bpftool link' print identical netfilter output, I'll check this on Monday (to make sure the formatting doesn't seem out of place).
Its kinda silly to not have anything netfilter related in 'bpftool net', this thing isn't named 'linkfilter' after all 8-)
Sounds all good to me :) Thanks!
Florian Westphal
maintainerPATCH v2 6/6 bpf: add test_run support for netfilter program type
In reply to Florian Westphalalso extend prog_tests with a small retval test: values other than accept or drop (0, 1) will cause issues.
NF_QUEUE could be implemented later if we can guarantee that attachment of such programs can be rejected if they get attached to a pf/hook that doesn't support async reinjection.
NF_STOLEN could be implemented via trusted helpers that can guarantee that the skb will eventually be free'd.
$ ./test_progs --allow=verifier_netfilter_retcode #278/1 verifier_netfilter_retcode/bpf_exit with invalid return code. test1:OK #278/2 verifier_netfilter_retcode/bpf_exit with valid return code. test2:OK #278/3 verifier_netfilter_retcode/bpf_exit with valid return code. test3:OK #278/4 verifier_netfilter_retcode/bpf_exit with invalid return code. test4:OK #278 verifier_netfilter_retcode:OK
---
v2: use progs/, not old verifier tests
avoid unused variable warning
include/linux/bpf.h | 3 + net/bpf/test_run.c | 140 ++++++++++++++++++ net/netfilter/nf_bpf_link.c | 1 + .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_netfilter_retcode.c | 49 ++++++ 5 files changed, 195 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_netfilter_retcode.c
include/linux/bpf.h +3 -0
@@ -2234,6 +2234,9 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,int bpf_prog_test_run_sk_lookup (struct bpf_prog *prog,const union bpf_attr *kattr,union bpf_attr __user *uattr); +int bpf_prog_test_run_nf (struct bpf_prog *prog, +const union bpf_attr *kattr, +union bpf_attr __user *uattr);bool btf_ctx_access (int off,int size,enum bpf_access_type type,const struct bpf_prog *prog,struct bpf_insn_access_aux *info);
net/bpf/test_run.c +140 -0
153 lines not on this page · raw message
net/netfilter/nf_bpf_link.c +1 -0
@@ -128,6 +128,7 @@ int bpf_nf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog) }const struct bpf_prog_ops netfilter_prog_ops = { + .test_run = bpf_prog_test_run_nf, };static bool nf_ptr_to_btf_id (struct bpf_insn_access_aux *info,const char *name)
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -29,6 +29,7 @@#include "verifier_map_ret_val.skel.h" #include "verifier_masking.skel.h" #include "verifier_meta_access.skel.h" +#include "verifier_netfilter_retcode.skel.h" #include "verifier_raw_stack.skel.h" #include "verifier_raw_tp_writable.skel.h" #include "verifier_ringbuf.skel.h"
@@ -93,6 +94,7 @@ void test_verifier_map_ptr(void) { RUN(verifier_map_ptr); }void test_verifier_map_ret_val (void ) {RUN (verifier_map_ret_val); }void test_verifier_masking (void ) {RUN (verifier_masking); }void test_verifier_meta_access (void ) {RUN (verifier_meta_access); } +void test_verifier_netfilter_retcode (void ) {RUN (verifier_netfilter_retcode); }void test_verifier_raw_stack (void ) {RUN (verifier_raw_stack); }void test_verifier_raw_tp_writable (void ) {RUN (verifier_raw_tp_writable); }void test_verifier_ringbuf (void ) {RUN (verifier_ringbuf); }
tools/testing/selftests/bpf/progs/verifier_netfilter_retcode.c new file +49 -0
49 lines not on this page · raw message
Signature · 1 line
2.39.2