Skip to content

Commit 132328e

Browse files
Florian Westphalanakryiko
authored andcommitted
bpf: netfilter: Add BPF_NETFILTER bpf_attach_type
Andrii Nakryiko writes: And we currently don't have an attach type for NETLINK BPF link. Thankfully it's not too late to add it. I see that link_create() in kernel/bpf/syscall.c just bypasses attach_type check. We shouldn't have done that. Instead we need to add BPF_NETLINK attach type to enum bpf_attach_type. And wire all that properly throughout the kernel and libbpf itself. This adds BPF_NETFILTER and uses it. This breaks uabi but this wasn't in any non-rc release yet, so it should be fine. v2: check link_attack prog type in link_create too Fixes: 84601d6 ("bpf: add bpf_link support for BPF_NETFILTER programs") Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/CAEf4BzZ69YgrQW7DHCJUT_X+GqMq_ZQQPBwopaJJVGFD5=d5Vg@mail.gmail.com/ Link: https://lore.kernel.org/bpf/[email protected]
1 parent 23509e9 commit 132328e

File tree

5 files changed

+15
-1
lines changed

5 files changed

+15
-1
lines changed

include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ enum bpf_attach_type {
10351035
BPF_TRACE_KPROBE_MULTI,
10361036
BPF_LSM_CGROUP,
10371037
BPF_STRUCT_OPS,
1038+
BPF_NETFILTER,
10381039
__MAX_BPF_ATTACH_TYPE
10391040
};
10401041

kernel/bpf/syscall.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,6 +2433,10 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
24332433
default:
24342434
return -EINVAL;
24352435
}
2436+
case BPF_PROG_TYPE_NETFILTER:
2437+
if (expected_attach_type == BPF_NETFILTER)
2438+
return 0;
2439+
return -EINVAL;
24362440
case BPF_PROG_TYPE_SYSCALL:
24372441
case BPF_PROG_TYPE_EXT:
24382442
if (expected_attach_type)
@@ -4590,7 +4594,12 @@ static int link_create(union bpf_attr *attr, bpfptr_t uattr)
45904594

45914595
switch (prog->type) {
45924596
case BPF_PROG_TYPE_EXT:
4597+
break;
45934598
case BPF_PROG_TYPE_NETFILTER:
4599+
if (attr->link_create.attach_type != BPF_NETFILTER) {
4600+
ret = -EINVAL;
4601+
goto out;
4602+
}
45944603
break;
45954604
case BPF_PROG_TYPE_PERF_EVENT:
45964605
case BPF_PROG_TYPE_TRACEPOINT:

tools/include/uapi/linux/bpf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ enum bpf_attach_type {
10351035
BPF_TRACE_KPROBE_MULTI,
10361036
BPF_LSM_CGROUP,
10371037
BPF_STRUCT_OPS,
1038+
BPF_NETFILTER,
10381039
__MAX_BPF_ATTACH_TYPE
10391040
};
10401041

tools/lib/bpf/libbpf.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ static const char * const attach_type_name[] = {
117117
[BPF_PERF_EVENT] = "perf_event",
118118
[BPF_TRACE_KPROBE_MULTI] = "trace_kprobe_multi",
119119
[BPF_STRUCT_OPS] = "struct_ops",
120+
[BPF_NETFILTER] = "netfilter",
120121
};
121122

122123
static const char * const link_type_name[] = {
@@ -8712,7 +8713,7 @@ static const struct bpf_sec_def section_defs[] = {
87128713
SEC_DEF("struct_ops+", STRUCT_OPS, 0, SEC_NONE),
87138714
SEC_DEF("struct_ops.s+", STRUCT_OPS, 0, SEC_SLEEPABLE),
87148715
SEC_DEF("sk_lookup", SK_LOOKUP, BPF_SK_LOOKUP, SEC_ATTACHABLE),
8715-
SEC_DEF("netfilter", NETFILTER, 0, SEC_NONE),
8716+
SEC_DEF("netfilter", NETFILTER, BPF_NETFILTER, SEC_NONE),
87168717
};
87178718

87188719
static size_t custom_sec_def_cnt;

tools/lib/bpf/libbpf_probes.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,9 @@ static int probe_prog_load(enum bpf_prog_type prog_type,
180180
case BPF_PROG_TYPE_SK_REUSEPORT:
181181
case BPF_PROG_TYPE_FLOW_DISSECTOR:
182182
case BPF_PROG_TYPE_CGROUP_SYSCTL:
183+
break;
183184
case BPF_PROG_TYPE_NETFILTER:
185+
opts.expected_attach_type = BPF_NETFILTER;
184186
break;
185187
default:
186188
return -EOPNOTSUPP;

0 commit comments

Comments
 (0)