Skip to content

Commit 84601d6

Browse files
Florian WestphalAlexei Starovoitov
authored andcommitted
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. v4: arm32 cmpxchg only supports 32bit operand s/prio/priority/ v3: restrict prog attachment to ip/ip6 for now, lets lift restrictions if more use cases pop up (arptables, ebtables, netdev ingress/egress etc). Signed-off-by: Florian Westphal <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 45cea72 commit 84601d6

File tree

7 files changed

+194
-0
lines changed

7 files changed

+194
-0
lines changed

include/linux/netfilter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ typedef unsigned int nf_hookfn(void *priv,
8080
enum nf_hook_ops_type {
8181
NF_HOOK_OP_UNDEFINED,
8282
NF_HOOK_OP_NF_TABLES,
83+
NF_HOOK_OP_BPF,
8384
};
8485

8586
struct nf_hook_ops {

include/net/netfilter/nf_bpf_link.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
3+
#if IS_ENABLED(CONFIG_NETFILTER_BPF_LINK)
4+
int bpf_nf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
5+
#else
6+
static inline int bpf_nf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
7+
{
8+
return -EOPNOTSUPP;
9+
}
10+
#endif

include/uapi/linux/bpf.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ enum bpf_prog_type {
986986
BPF_PROG_TYPE_LSM,
987987
BPF_PROG_TYPE_SK_LOOKUP,
988988
BPF_PROG_TYPE_SYSCALL, /* a program that can execute syscalls */
989+
BPF_PROG_TYPE_NETFILTER,
989990
};
990991

991992
enum bpf_attach_type {
@@ -1050,6 +1051,7 @@ enum bpf_link_type {
10501051
BPF_LINK_TYPE_PERF_EVENT = 7,
10511052
BPF_LINK_TYPE_KPROBE_MULTI = 8,
10521053
BPF_LINK_TYPE_STRUCT_OPS = 9,
1054+
BPF_LINK_TYPE_NETFILTER = 10,
10531055

10541056
MAX_BPF_LINK_TYPE,
10551057
};
@@ -1560,6 +1562,12 @@ union bpf_attr {
15601562
*/
15611563
__u64 cookie;
15621564
} tracing;
1565+
struct {
1566+
__u32 pf;
1567+
__u32 hooknum;
1568+
__s32 priority;
1569+
__u32 flags;
1570+
} netfilter;
15631571
};
15641572
} link_create;
15651573

@@ -6410,6 +6418,12 @@ struct bpf_link_info {
64106418
struct {
64116419
__u32 map_id;
64126420
} struct_ops;
6421+
struct {
6422+
__u32 pf;
6423+
__u32 hooknum;
6424+
__s32 priority;
6425+
__u32 flags;
6426+
} netfilter;
64136427
};
64146428
} __attribute__((aligned(8)));
64156429

kernel/bpf/syscall.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <linux/rcupdate_trace.h>
3636
#include <linux/memcontrol.h>
3737
#include <linux/trace_events.h>
38+
#include <net/netfilter/nf_bpf_link.h>
3839

3940
#define IS_FD_ARRAY(map) ((map)->map_type == BPF_MAP_TYPE_PERF_EVENT_ARRAY || \
4041
(map)->map_type == BPF_MAP_TYPE_CGROUP_ARRAY || \
@@ -2462,6 +2463,7 @@ static bool is_net_admin_prog_type(enum bpf_prog_type prog_type)
24622463
case BPF_PROG_TYPE_CGROUP_SYSCTL:
24632464
case BPF_PROG_TYPE_SOCK_OPS:
24642465
case BPF_PROG_TYPE_EXT: /* extends any prog */
2466+
case BPF_PROG_TYPE_NETFILTER:
24652467
return true;
24662468
case BPF_PROG_TYPE_CGROUP_SKB:
24672469
/* always unpriv */
@@ -4588,6 +4590,7 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
45884590

45894591
switch (prog->type) {
45904592
case BPF_PROG_TYPE_EXT:
4593+
case BPF_PROG_TYPE_NETFILTER:
45914594
break;
45924595
case BPF_PROG_TYPE_PERF_EVENT:
45934596
case BPF_PROG_TYPE_TRACEPOINT:
@@ -4654,6 +4657,9 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
46544657
case BPF_PROG_TYPE_XDP:
46554658
ret = bpf_xdp_link_attach(attr, prog);
46564659
break;
4660+
case BPF_PROG_TYPE_NETFILTER:
4661+
ret = bpf_nf_link_attach(attr, prog);
4662+
break;
46574663
#endif
46584664
case BPF_PROG_TYPE_PERF_EVENT:
46594665
case BPF_PROG_TYPE_TRACEPOINT:

net/netfilter/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ config NETFILTER_FAMILY_BRIDGE
3030
config NETFILTER_FAMILY_ARP
3131
bool
3232

33+
config NETFILTER_BPF_LINK
34+
def_bool BPF_SYSCALL
35+
3336
config NETFILTER_NETLINK_HOOK
3437
tristate "Netfilter base hook dump support"
3538
depends on NETFILTER_ADVANCED

net/netfilter/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ nf_conntrack-$(CONFIG_DEBUG_INFO_BTF) += nf_conntrack_bpf.o
2222
endif
2323

2424
obj-$(CONFIG_NETFILTER) = netfilter.o
25+
obj-$(CONFIG_NETFILTER_BPF_LINK) += nf_bpf_link.o
2526

2627
obj-$(CONFIG_NETFILTER_NETLINK) += nfnetlink.o
2728
obj-$(CONFIG_NETFILTER_NETLINK_ACCT) += nfnetlink_acct.o

net/netfilter/nf_bpf_link.c

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <linux/bpf.h>
3+
#include <linux/netfilter.h>
4+
5+
#include <net/netfilter/nf_bpf_link.h>
6+
#include <uapi/linux/netfilter_ipv4.h>
7+
8+
static unsigned int nf_hook_run_bpf(void *bpf_prog, struct sk_buff *skb,
9+
const struct nf_hook_state *s)
10+
{
11+
return NF_ACCEPT;
12+
}
13+
14+
struct bpf_nf_link {
15+
struct bpf_link link;
16+
struct nf_hook_ops hook_ops;
17+
struct net *net;
18+
u32 dead;
19+
};
20+
21+
static void bpf_nf_link_release(struct bpf_link *link)
22+
{
23+
struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link);
24+
25+
if (nf_link->dead)
26+
return;
27+
28+
/* prevent hook-not-found warning splat from netfilter core when
29+
* .detach was already called
30+
*/
31+
if (!cmpxchg(&nf_link->dead, 0, 1))
32+
nf_unregister_net_hook(nf_link->net, &nf_link->hook_ops);
33+
}
34+
35+
static void bpf_nf_link_dealloc(struct bpf_link *link)
36+
{
37+
struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link);
38+
39+
kfree(nf_link);
40+
}
41+
42+
static int bpf_nf_link_detach(struct bpf_link *link)
43+
{
44+
bpf_nf_link_release(link);
45+
return 0;
46+
}
47+
48+
static void bpf_nf_link_show_info(const struct bpf_link *link,
49+
struct seq_file *seq)
50+
{
51+
struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link);
52+
53+
seq_printf(seq, "pf:\t%u\thooknum:\t%u\tprio:\t%d\n",
54+
nf_link->hook_ops.pf, nf_link->hook_ops.hooknum,
55+
nf_link->hook_ops.priority);
56+
}
57+
58+
static int bpf_nf_link_fill_link_info(const struct bpf_link *link,
59+
struct bpf_link_info *info)
60+
{
61+
struct bpf_nf_link *nf_link = container_of(link, struct bpf_nf_link, link);
62+
63+
info->netfilter.pf = nf_link->hook_ops.pf;
64+
info->netfilter.hooknum = nf_link->hook_ops.hooknum;
65+
info->netfilter.priority = nf_link->hook_ops.priority;
66+
info->netfilter.flags = 0;
67+
68+
return 0;
69+
}
70+
71+
static int bpf_nf_link_update(struct bpf_link *link, struct bpf_prog *new_prog,
72+
struct bpf_prog *old_prog)
73+
{
74+
return -EOPNOTSUPP;
75+
}
76+
77+
static const struct bpf_link_ops bpf_nf_link_lops = {
78+
.release = bpf_nf_link_release,
79+
.dealloc = bpf_nf_link_dealloc,
80+
.detach = bpf_nf_link_detach,
81+
.show_fdinfo = bpf_nf_link_show_info,
82+
.fill_link_info = bpf_nf_link_fill_link_info,
83+
.update_prog = bpf_nf_link_update,
84+
};
85+
86+
static int bpf_nf_check_pf_and_hooks(const union bpf_attr *attr)
87+
{
88+
switch (attr->link_create.netfilter.pf) {
89+
case NFPROTO_IPV4:
90+
case NFPROTO_IPV6:
91+
if (attr->link_create.netfilter.hooknum >= NF_INET_NUMHOOKS)
92+
return -EPROTO;
93+
break;
94+
default:
95+
return -EAFNOSUPPORT;
96+
}
97+
98+
if (attr->link_create.netfilter.flags)
99+
return -EOPNOTSUPP;
100+
101+
/* make sure conntrack confirm is always last.
102+
*
103+
* In the future, if userspace can e.g. request defrag, then
104+
* "defrag_requested && prio before NF_IP_PRI_CONNTRACK_DEFRAG"
105+
* should fail.
106+
*/
107+
switch (attr->link_create.netfilter.priority) {
108+
case NF_IP_PRI_FIRST: return -ERANGE; /* sabotage_in and other warts */
109+
case NF_IP_PRI_LAST: return -ERANGE; /* e.g. conntrack confirm */
110+
}
111+
112+
return 0;
113+
}
114+
115+
int bpf_nf_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
116+
{
117+
struct net *net = current->nsproxy->net_ns;
118+
struct bpf_link_primer link_primer;
119+
struct bpf_nf_link *link;
120+
int err;
121+
122+
if (attr->link_create.flags)
123+
return -EINVAL;
124+
125+
err = bpf_nf_check_pf_and_hooks(attr);
126+
if (err)
127+
return err;
128+
129+
link = kzalloc(sizeof(*link), GFP_USER);
130+
if (!link)
131+
return -ENOMEM;
132+
133+
bpf_link_init(&link->link, BPF_LINK_TYPE_NETFILTER, &bpf_nf_link_lops, prog);
134+
135+
link->hook_ops.hook = nf_hook_run_bpf;
136+
link->hook_ops.hook_ops_type = NF_HOOK_OP_BPF;
137+
link->hook_ops.priv = prog;
138+
139+
link->hook_ops.pf = attr->link_create.netfilter.pf;
140+
link->hook_ops.priority = attr->link_create.netfilter.priority;
141+
link->hook_ops.hooknum = attr->link_create.netfilter.hooknum;
142+
143+
link->net = net;
144+
link->dead = false;
145+
146+
err = bpf_link_prime(&link->link, &link_primer);
147+
if (err) {
148+
kfree(link);
149+
return err;
150+
}
151+
152+
err = nf_register_net_hook(net, &link->hook_ops);
153+
if (err) {
154+
bpf_link_cleanup(&link_primer);
155+
return err;
156+
}
157+
158+
return bpf_link_settle(&link_primer);
159+
}

0 commit comments

Comments
 (0)