summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/bpf/prog_tests/netns_cookie.c
blob: 71d8f3ba7d6ba64a0c28ce5e675c0ada28d29cfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// SPDX-License-Identifier: GPL-2.0

#include <test_progs.h>
#include "netns_cookie_prog.skel.h"
#include "network_helpers.h"

#ifndef SO_NETNS_COOKIE
#define SO_NETNS_COOKIE 71
#endif

static int duration;

void test_netns_cookie(void)
{
	int server_fd = -1, client_fd = -1, cgroup_fd = -1;
	int err, val, ret, map, verdict;
	struct netns_cookie_prog *skel;
	uint64_t cookie_expected_value;
	socklen_t vallen = sizeof(cookie_expected_value);
	static const char send_msg[] = "message";

	skel = netns_cookie_prog__open_and_load();
	if (!ASSERT_OK_PTR(skel, "skel_open"))
		return;

	cgroup_fd = test__join_cgroup("/netns_cookie");
	if (CHECK(cgroup_fd < 0, "join_cgroup", "cgroup creation failed\n"))
		goto done;

	skel->links.get_netns_cookie_sockops = bpf_program__attach_cgroup(
		skel->progs.get_netns_cookie_sockops, cgroup_fd);
	if (!ASSERT_OK_PTR(skel->links.get_netns_cookie_sockops, "prog_attach"))
		goto done;

	verdict = bpf_program__fd(skel->progs.get_netns_cookie_sk_msg);
	map = bpf_map__fd(skel->maps.sock_map);
	err = bpf_prog_attach(verdict, map, BPF_SK_MSG_VERDICT, 0);
	if (!ASSERT_OK(err, "prog_attach"))
		goto done;

	server_fd = start_server(AF_INET6, SOCK_STREAM, "::1", 0, 0);
	if (CHECK(server_fd < 0, "start_server", "errno %d\n", errno))
		goto done;

	client_fd = connect_to_fd(server_fd, 0);
	if (CHECK(client_fd < 0, "connect_to_fd", "errno %d\n", errno))
		goto done;

	ret = send(client_fd, send_msg, sizeof(send_msg), 0);
	if (CHECK(ret != sizeof(send_msg), "send(msg)", "ret:%d\n", ret))
		goto done;

	err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sockops_netns_cookies),
				  &client_fd, &val);
	if (!ASSERT_OK(err, "map_lookup(sockops_netns_cookies)"))
		goto done;

	err = getsockopt(client_fd, SOL_SOCKET, SO_NETNS_COOKIE,
			 &cookie_expected_value, &vallen);
	if (!ASSERT_OK(err, "getsockopt"))
		goto done;

	ASSERT_EQ(val, cookie_expected_value, "cookie_value");

	err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.sk_msg_netns_cookies),
				  &client_fd, &val);
	if (!ASSERT_OK(err, "map_lookup(sk_msg_netns_cookies)"))
		goto done;

	ASSERT_EQ(val, cookie_expected_value, "cookie_value");

done:
	if (server_fd != -1)
		close(server_fd);
	if (client_fd != -1)
		close(client_fd);
	if (cgroup_fd != -1)
		close(cgroup_fd);
	netns_cookie_prog__destroy(skel);
}