BPF List
 help / color / mirror / Atom feed
* [RFC nf-next 0/3] bpf, netfilter: minimal support for bpf progs
@ 2023-02-08 16:03 Florian Westphal
  2023-02-08 16:03 ` [RFC nf-next 1/3] bpf: add bpf_link support for BPF_NETFILTER programs Florian Westphal
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Florian Westphal @ 2023-02-08 16:03 UTC (permalink / raw)
  To: bpf; +Cc: netfilter-devel, Florian Westphal

Add minimal support to hook bpf programs to netfilter hooks,
e.g. PREROUTING or FORWARD.

Hooking is currently possible for all supprted protocols, i.e.
arp, bridge, ip, ip6 and inet (both ipv4/ipv6) pseudo-family.

For this the most relevant parts for registering a netfilter
hook via the in-kernel api are exposed to userspace via bpf_link.

With this its possible to build a small test program such as:

SEC("netfilter")
int nf_test(struct __sk_buff *skb)
{
	const struct iphdr *iph = (void *)skb->data;

	if (iph + 1 < skb->data_end)
		bpf_printk("%x,%x\n", iph->saddr, iph->daddr);
	else
		return NF_DROP;

        return NF_ACCEPT;
}

(output can be observed via /sys/kernel/tracing/trace_pipe).

The prototype is wrong, future versions will instead expose
'struct bpf_nf_ctx' via btf, so programs will have to use

SEC("netfilter")
int nf_test(struct bpf_nf_ctx *ctx)
{
	struct sk_buff *skb = ctx->skb; ...


I will work on this next but thought it was better to send
a current snapshot.

This series no longer depends on the proposed in-kernel
nf_hook_slow bpf translator.

Feedback welcome.

Florian Westphal (3):
  bpf: add bpf_link support for BPF_NETFILTER programs
  libbpf: sync header file, add nf prog section name
  bpf: minimal support for programs hooked into netfilter framework

 include/linux/bpf_types.h           |   4 +
 include/linux/netfilter.h           |   1 +
 include/net/netfilter/nf_hook_bpf.h |  10 ++
 include/uapi/linux/bpf.h            |  13 ++
 kernel/bpf/btf.c                    |   3 +
 kernel/bpf/syscall.c                |   7 +
 kernel/bpf/verifier.c               |   3 +
 net/netfilter/Kconfig               |   3 +
 net/netfilter/Makefile              |   1 +
 net/netfilter/nf_bpf_link.c         | 242 ++++++++++++++++++++++++++++
 tools/include/uapi/linux/bpf.h      |  13 ++
 tools/lib/bpf/libbpf.c              |   1 +
 12 files changed, 301 insertions(+)
 create mode 100644 include/net/netfilter/nf_hook_bpf.h
 create mode 100644 net/netfilter/nf_bpf_link.c

-- 
2.39.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-02-08 16:03 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-08 16:03 [RFC nf-next 0/3] bpf, netfilter: minimal support for bpf progs Florian Westphal
2023-02-08 16:03 ` [RFC nf-next 1/3] bpf: add bpf_link support for BPF_NETFILTER programs Florian Westphal
2023-02-08 16:03 ` [RFC nf-next 2/3] libbpf: sync header file, add nf prog section name Florian Westphal
2023-02-08 16:03 ` [RFC nf-next 3/3] bpf: minimal support for programs hooked into netfilter framework Florian Westphal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox