mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-05-02 05:07:46 -04:00
Merge branch 'libbpf: add netfilter link attach helper'
Florian Westphal says: ==================== v4: address comment from Daniel Xu: - use human-readable test names in 2/2 v3: address comments from Andrii: - prune verbose error message in 1/2 - use bpf_link_create internally in 1/2 - use subtests in patch 2/2 When initial netfilter bpf program type support got added one suggestion was to extend libbpf with a helper to ease attachment of nf programs to the hook locations. Add such a helper and a demo test case that attaches a dummy program to various combinations. I tested that the selftest fails when changing the expected outcome (i.e., set 'success' when it should fail and v.v.). ==================== Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
This commit is contained in:
@@ -741,6 +741,14 @@ int bpf_link_create(int prog_fd, int target_fd,
|
||||
if (!OPTS_ZEROED(opts, tracing))
|
||||
return libbpf_err(-EINVAL);
|
||||
break;
|
||||
case BPF_NETFILTER:
|
||||
attr.link_create.netfilter.pf = OPTS_GET(opts, netfilter.pf, 0);
|
||||
attr.link_create.netfilter.hooknum = OPTS_GET(opts, netfilter.hooknum, 0);
|
||||
attr.link_create.netfilter.priority = OPTS_GET(opts, netfilter.priority, 0);
|
||||
attr.link_create.netfilter.flags = OPTS_GET(opts, netfilter.flags, 0);
|
||||
if (!OPTS_ZEROED(opts, netfilter))
|
||||
return libbpf_err(-EINVAL);
|
||||
break;
|
||||
default:
|
||||
if (!OPTS_ZEROED(opts, flags))
|
||||
return libbpf_err(-EINVAL);
|
||||
|
||||
@@ -349,6 +349,12 @@ struct bpf_link_create_opts {
|
||||
struct {
|
||||
__u64 cookie;
|
||||
} tracing;
|
||||
struct {
|
||||
__u32 pf;
|
||||
__u32 hooknum;
|
||||
__s32 priority;
|
||||
__u32 flags;
|
||||
} netfilter;
|
||||
};
|
||||
size_t :0;
|
||||
};
|
||||
|
||||
@@ -11815,6 +11815,48 @@ static int attach_iter(const struct bpf_program *prog, long cookie, struct bpf_l
|
||||
return libbpf_get_error(*link);
|
||||
}
|
||||
|
||||
struct bpf_link *bpf_program__attach_netfilter(const struct bpf_program *prog,
|
||||
const struct bpf_netfilter_opts *opts)
|
||||
{
|
||||
LIBBPF_OPTS(bpf_link_create_opts, lopts);
|
||||
struct bpf_link *link;
|
||||
int prog_fd, link_fd;
|
||||
|
||||
if (!OPTS_VALID(opts, bpf_netfilter_opts))
|
||||
return libbpf_err_ptr(-EINVAL);
|
||||
|
||||
prog_fd = bpf_program__fd(prog);
|
||||
if (prog_fd < 0) {
|
||||
pr_warn("prog '%s': can't attach before loaded\n", prog->name);
|
||||
return libbpf_err_ptr(-EINVAL);
|
||||
}
|
||||
|
||||
link = calloc(1, sizeof(*link));
|
||||
if (!link)
|
||||
return libbpf_err_ptr(-ENOMEM);
|
||||
|
||||
link->detach = &bpf_link__detach_fd;
|
||||
|
||||
lopts.netfilter.pf = OPTS_GET(opts, pf, 0);
|
||||
lopts.netfilter.hooknum = OPTS_GET(opts, hooknum, 0);
|
||||
lopts.netfilter.priority = OPTS_GET(opts, priority, 0);
|
||||
lopts.netfilter.flags = OPTS_GET(opts, flags, 0);
|
||||
|
||||
link_fd = bpf_link_create(prog_fd, 0, BPF_NETFILTER, &lopts);
|
||||
if (link_fd < 0) {
|
||||
char errmsg[STRERR_BUFSIZE];
|
||||
|
||||
link_fd = -errno;
|
||||
free(link);
|
||||
pr_warn("prog '%s': failed to attach to netfilter: %s\n",
|
||||
prog->name, libbpf_strerror_r(link_fd, errmsg, sizeof(errmsg)));
|
||||
return libbpf_err_ptr(link_fd);
|
||||
}
|
||||
link->fd = link_fd;
|
||||
|
||||
return link;
|
||||
}
|
||||
|
||||
struct bpf_link *bpf_program__attach(const struct bpf_program *prog)
|
||||
{
|
||||
struct bpf_link *link = NULL;
|
||||
|
||||
@@ -718,6 +718,21 @@ LIBBPF_API struct bpf_link *
|
||||
bpf_program__attach_freplace(const struct bpf_program *prog,
|
||||
int target_fd, const char *attach_func_name);
|
||||
|
||||
struct bpf_netfilter_opts {
|
||||
/* size of this struct, for forward/backward compatibility */
|
||||
size_t sz;
|
||||
|
||||
__u32 pf;
|
||||
__u32 hooknum;
|
||||
__s32 priority;
|
||||
__u32 flags;
|
||||
};
|
||||
#define bpf_netfilter_opts__last_field flags
|
||||
|
||||
LIBBPF_API struct bpf_link *
|
||||
bpf_program__attach_netfilter(const struct bpf_program *prog,
|
||||
const struct bpf_netfilter_opts *opts);
|
||||
|
||||
struct bpf_map;
|
||||
|
||||
LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);
|
||||
|
||||
@@ -395,4 +395,5 @@ LIBBPF_1.2.0 {
|
||||
LIBBPF_1.3.0 {
|
||||
global:
|
||||
bpf_obj_pin_opts;
|
||||
bpf_program__attach_netfilter;
|
||||
} LIBBPF_1.2.0;
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <netinet/in.h>
|
||||
#include <linux/netfilter.h>
|
||||
|
||||
#include "test_progs.h"
|
||||
#include "test_netfilter_link_attach.skel.h"
|
||||
|
||||
struct nf_link_test {
|
||||
__u32 pf;
|
||||
__u32 hooknum;
|
||||
__s32 priority;
|
||||
__u32 flags;
|
||||
|
||||
bool expect_success;
|
||||
const char * const name;
|
||||
};
|
||||
|
||||
static const struct nf_link_test nf_hook_link_tests[] = {
|
||||
{ .name = "allzero", },
|
||||
{ .pf = NFPROTO_NUMPROTO, .name = "invalid-pf", },
|
||||
{ .pf = NFPROTO_IPV4, .hooknum = 42, .name = "invalid-hooknum", },
|
||||
{ .pf = NFPROTO_IPV4, .priority = INT_MIN, .name = "invalid-priority-min", },
|
||||
{ .pf = NFPROTO_IPV4, .priority = INT_MAX, .name = "invalid-priority-max", },
|
||||
{ .pf = NFPROTO_IPV4, .flags = UINT_MAX, .name = "invalid-flags", },
|
||||
|
||||
{ .pf = NFPROTO_INET, .priority = 1, .name = "invalid-inet-not-supported", },
|
||||
|
||||
{ .pf = NFPROTO_IPV4, .priority = -10000, .expect_success = true, .name = "attach ipv4", },
|
||||
{ .pf = NFPROTO_IPV6, .priority = 10001, .expect_success = true, .name = "attach ipv6", },
|
||||
};
|
||||
|
||||
void test_netfilter_link_attach(void)
|
||||
{
|
||||
struct test_netfilter_link_attach *skel;
|
||||
struct bpf_program *prog;
|
||||
LIBBPF_OPTS(bpf_netfilter_opts, opts);
|
||||
int i;
|
||||
|
||||
skel = test_netfilter_link_attach__open_and_load();
|
||||
if (!ASSERT_OK_PTR(skel, "test_netfilter_link_attach__open_and_load"))
|
||||
goto out;
|
||||
|
||||
prog = skel->progs.nf_link_attach_test;
|
||||
if (!ASSERT_OK_PTR(prog, "attach program"))
|
||||
goto out;
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(nf_hook_link_tests); i++) {
|
||||
struct bpf_link *link;
|
||||
|
||||
if (!test__start_subtest(nf_hook_link_tests[i].name))
|
||||
continue;
|
||||
|
||||
#define X(opts, m, i) opts.m = nf_hook_link_tests[(i)].m
|
||||
X(opts, pf, i);
|
||||
X(opts, hooknum, i);
|
||||
X(opts, priority, i);
|
||||
X(opts, flags, i);
|
||||
#undef X
|
||||
link = bpf_program__attach_netfilter(prog, &opts);
|
||||
if (nf_hook_link_tests[i].expect_success) {
|
||||
struct bpf_link *link2;
|
||||
|
||||
if (!ASSERT_OK_PTR(link, "program attach successful"))
|
||||
continue;
|
||||
|
||||
link2 = bpf_program__attach_netfilter(prog, &opts);
|
||||
ASSERT_ERR_PTR(link2, "attach program with same pf/hook/priority");
|
||||
|
||||
if (!ASSERT_OK(bpf_link__destroy(link), "link destroy"))
|
||||
break;
|
||||
|
||||
link2 = bpf_program__attach_netfilter(prog, &opts);
|
||||
if (!ASSERT_OK_PTR(link2, "program reattach successful"))
|
||||
continue;
|
||||
if (!ASSERT_OK(bpf_link__destroy(link2), "link destroy"))
|
||||
break;
|
||||
} else {
|
||||
ASSERT_ERR_PTR(link, "program load failure");
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
test_netfilter_link_attach__destroy(skel);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "vmlinux.h"
|
||||
#include <bpf/bpf_helpers.h>
|
||||
|
||||
#define NF_ACCEPT 1
|
||||
|
||||
SEC("netfilter")
|
||||
int nf_link_attach_test(struct bpf_nf_ctx *ctx)
|
||||
{
|
||||
return NF_ACCEPT;
|
||||
}
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
||||
Reference in New Issue
Block a user