2021-09-14 01:07:59 +02:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
|
|
|
|
#include <test_progs.h>
|
|
|
|
|
|
|
|
#include "connect4_dropper.skel.h"
|
|
|
|
|
|
|
|
#include "cgroup_helpers.h"
|
|
|
|
#include "network_helpers.h"
|
|
|
|
|
|
|
|
static int run_test(int cgroup_fd, int server_fd, bool classid)
|
|
|
|
{
|
|
|
|
struct connect4_dropper *skel;
|
2025-02-27 22:26:44 +08:00
|
|
|
int fd, err = 0, port;
|
2021-09-14 01:07:59 +02:00
|
|
|
|
|
|
|
skel = connect4_dropper__open_and_load();
|
|
|
|
if (!ASSERT_OK_PTR(skel, "skel_open"))
|
|
|
|
return -1;
|
|
|
|
|
2025-02-27 22:26:44 +08:00
|
|
|
port = get_socket_local_port(server_fd);
|
|
|
|
if (!ASSERT_GE(port, 0, "get_socket_local_port"))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
skel->bss->port = ntohs(port);
|
|
|
|
|
2021-09-14 01:07:59 +02:00
|
|
|
skel->links.connect_v4_dropper =
|
|
|
|
bpf_program__attach_cgroup(skel->progs.connect_v4_dropper,
|
|
|
|
cgroup_fd);
|
|
|
|
if (!ASSERT_OK_PTR(skel->links.connect_v4_dropper, "prog_attach")) {
|
|
|
|
err = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (classid && !ASSERT_OK(join_classid(), "join_classid")) {
|
|
|
|
err = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2024-07-18 14:22:30 +08:00
|
|
|
errno = 0;
|
|
|
|
fd = connect_to_fd_opts(server_fd, NULL);
|
|
|
|
if (fd >= 0) {
|
|
|
|
log_err("Unexpected success to connect to server");
|
2021-09-14 01:07:59 +02:00
|
|
|
err = -1;
|
|
|
|
close(fd);
|
2024-07-18 14:22:30 +08:00
|
|
|
} else if (errno != EPERM) {
|
|
|
|
log_err("Unexpected errno from connect to server");
|
|
|
|
err = -1;
|
|
|
|
}
|
2021-09-14 01:07:59 +02:00
|
|
|
out:
|
|
|
|
connect4_dropper__destroy(skel);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_cgroup_v1v2(void)
|
|
|
|
{
|
|
|
|
struct network_helper_opts opts = {};
|
|
|
|
int server_fd, client_fd, cgroup_fd;
|
|
|
|
|
|
|
|
/* Step 1: Check base connectivity works without any BPF. */
|
2025-02-27 22:26:44 +08:00
|
|
|
server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
|
2021-09-14 01:07:59 +02:00
|
|
|
if (!ASSERT_GE(server_fd, 0, "server_fd"))
|
|
|
|
return;
|
2024-07-18 14:22:29 +08:00
|
|
|
client_fd = connect_to_fd_opts(server_fd, &opts);
|
2021-09-14 01:07:59 +02:00
|
|
|
if (!ASSERT_GE(client_fd, 0, "client_fd")) {
|
|
|
|
close(server_fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
close(client_fd);
|
|
|
|
close(server_fd);
|
|
|
|
|
|
|
|
/* Step 2: Check BPF policy prog attached to cgroups drops connectivity. */
|
|
|
|
cgroup_fd = test__join_cgroup("/connect_dropper");
|
|
|
|
if (!ASSERT_GE(cgroup_fd, 0, "cgroup_fd"))
|
|
|
|
return;
|
2025-02-27 22:26:44 +08:00
|
|
|
server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
|
2021-09-14 01:07:59 +02:00
|
|
|
if (!ASSERT_GE(server_fd, 0, "server_fd")) {
|
|
|
|
close(cgroup_fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ASSERT_OK(run_test(cgroup_fd, server_fd, false), "cgroup-v2-only");
|
|
|
|
setup_classid_environment();
|
2023-11-11 09:00:31 +00:00
|
|
|
set_classid();
|
2021-09-14 01:07:59 +02:00
|
|
|
ASSERT_OK(run_test(cgroup_fd, server_fd, true), "cgroup-v1v2");
|
|
|
|
cleanup_classid_environment();
|
|
|
|
close(server_fd);
|
|
|
|
close(cgroup_fd);
|
|
|
|
}
|