Skip to content

Commit a94098d

Browse files
Florian Westphalanakryiko
authored andcommitted
selftests/bpf: Add bpf_program__attach_netfilter helper test
Call bpf_program__attach_netfilter() with different protocol/hook/priority combinations. Test fails if supposedly-illegal attachments work (e.g., bogus protocol family, illegal priority and so on) or if a should-work attachment fails. Expected output: ./test_progs -t netfilter_link_attach torvalds#145/1 netfilter_link_attach/allzero:OK torvalds#145/2 netfilter_link_attach/invalid-pf:OK torvalds#145/3 netfilter_link_attach/invalid-hooknum:OK torvalds#145/4 netfilter_link_attach/invalid-priority-min:OK torvalds#145/5 netfilter_link_attach/invalid-priority-max:OK torvalds#145/6 netfilter_link_attach/invalid-flags:OK torvalds#145/7 netfilter_link_attach/invalid-inet-not-supported:OK torvalds#145/8 netfilter_link_attach/attach ipv4:OK torvalds#145/9 netfilter_link_attach/attach ipv6:OK Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Reviewed-by: Toke Høiland-Jørgensen <[email protected]> Acked-by: Daniel Xu <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 52364ab commit a94098d

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#include <netinet/in.h>
4+
#include <linux/netfilter.h>
5+
6+
#include "test_progs.h"
7+
#include "test_netfilter_link_attach.skel.h"
8+
9+
struct nf_link_test {
10+
__u32 pf;
11+
__u32 hooknum;
12+
__s32 priority;
13+
__u32 flags;
14+
15+
bool expect_success;
16+
const char * const name;
17+
};
18+
19+
static const struct nf_link_test nf_hook_link_tests[] = {
20+
{ .name = "allzero", },
21+
{ .pf = NFPROTO_NUMPROTO, .name = "invalid-pf", },
22+
{ .pf = NFPROTO_IPV4, .hooknum = 42, .name = "invalid-hooknum", },
23+
{ .pf = NFPROTO_IPV4, .priority = INT_MIN, .name = "invalid-priority-min", },
24+
{ .pf = NFPROTO_IPV4, .priority = INT_MAX, .name = "invalid-priority-max", },
25+
{ .pf = NFPROTO_IPV4, .flags = UINT_MAX, .name = "invalid-flags", },
26+
27+
{ .pf = NFPROTO_INET, .priority = 1, .name = "invalid-inet-not-supported", },
28+
29+
{ .pf = NFPROTO_IPV4, .priority = -10000, .expect_success = true, .name = "attach ipv4", },
30+
{ .pf = NFPROTO_IPV6, .priority = 10001, .expect_success = true, .name = "attach ipv6", },
31+
};
32+
33+
void test_netfilter_link_attach(void)
34+
{
35+
struct test_netfilter_link_attach *skel;
36+
struct bpf_program *prog;
37+
LIBBPF_OPTS(bpf_netfilter_opts, opts);
38+
int i;
39+
40+
skel = test_netfilter_link_attach__open_and_load();
41+
if (!ASSERT_OK_PTR(skel, "test_netfilter_link_attach__open_and_load"))
42+
goto out;
43+
44+
prog = skel->progs.nf_link_attach_test;
45+
if (!ASSERT_OK_PTR(prog, "attach program"))
46+
goto out;
47+
48+
for (i = 0; i < ARRAY_SIZE(nf_hook_link_tests); i++) {
49+
struct bpf_link *link;
50+
51+
if (!test__start_subtest(nf_hook_link_tests[i].name))
52+
continue;
53+
54+
#define X(opts, m, i) opts.m = nf_hook_link_tests[(i)].m
55+
X(opts, pf, i);
56+
X(opts, hooknum, i);
57+
X(opts, priority, i);
58+
X(opts, flags, i);
59+
#undef X
60+
link = bpf_program__attach_netfilter(prog, &opts);
61+
if (nf_hook_link_tests[i].expect_success) {
62+
struct bpf_link *link2;
63+
64+
if (!ASSERT_OK_PTR(link, "program attach successful"))
65+
continue;
66+
67+
link2 = bpf_program__attach_netfilter(prog, &opts);
68+
ASSERT_ERR_PTR(link2, "attach program with same pf/hook/priority");
69+
70+
if (!ASSERT_OK(bpf_link__destroy(link), "link destroy"))
71+
break;
72+
73+
link2 = bpf_program__attach_netfilter(prog, &opts);
74+
if (!ASSERT_OK_PTR(link2, "program reattach successful"))
75+
continue;
76+
if (!ASSERT_OK(bpf_link__destroy(link2), "link destroy"))
77+
break;
78+
} else {
79+
ASSERT_ERR_PTR(link, "program load failure");
80+
}
81+
}
82+
83+
out:
84+
test_netfilter_link_attach__destroy(skel);
85+
}
86+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#include "vmlinux.h"
4+
#include <bpf/bpf_helpers.h>
5+
6+
#define NF_ACCEPT 1
7+
8+
SEC("netfilter")
9+
int nf_link_attach_test(struct bpf_nf_ctx *ctx)
10+
{
11+
return NF_ACCEPT;
12+
}
13+
14+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)