Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <test_progs.h>
3#include "cgroup_helpers.h"
4
5#include "sockopt_inherit.skel.h"
6
7#define SOL_CUSTOM 0xdeadbeef
8#define CUSTOM_INHERIT1 0
9#define CUSTOM_INHERIT2 1
10#define CUSTOM_LISTENER 2
11
12static int connect_to_server(int server_fd)
13{
14 struct sockaddr_storage addr;
15 socklen_t len = sizeof(addr);
16 int fd;
17
18 fd = socket(AF_INET, SOCK_STREAM, 0);
19 if (fd < 0) {
20 log_err("Failed to create client socket");
21 return -1;
22 }
23
24 if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) {
25 log_err("Failed to get server addr");
26 goto out;
27 }
28
29 if (connect(fd, (const struct sockaddr *)&addr, len) < 0) {
30 log_err("Fail to connect to server");
31 goto out;
32 }
33
34 return fd;
35
36out:
37 close(fd);
38 return -1;
39}
40
41static int verify_sockopt(int fd, int optname, const char *msg, char expected)
42{
43 socklen_t optlen = 1;
44 char buf = 0;
45 int err;
46
47 err = getsockopt(fd, SOL_CUSTOM, optname, &buf, &optlen);
48 if (err) {
49 log_err("%s: failed to call getsockopt", msg);
50 return 1;
51 }
52
53 printf("%s %d: got=0x%x ? expected=0x%x\n", msg, optname, buf, expected);
54
55 if (buf != expected) {
56 log_err("%s: unexpected getsockopt value %d != %d", msg,
57 buf, expected);
58 return 1;
59 }
60
61 return 0;
62}
63
64static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
65static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
66
67static void *server_thread(void *arg)
68{
69 struct sockaddr_storage addr;
70 socklen_t len = sizeof(addr);
71 int fd = *(int *)arg;
72 int client_fd;
73 int err = 0;
74
75 err = listen(fd, 1);
76
77 pthread_mutex_lock(&server_started_mtx);
78 pthread_cond_signal(&server_started);
79 pthread_mutex_unlock(&server_started_mtx);
80
81 if (!ASSERT_GE(err, 0, "listed on socket"))
82 return NULL;
83
84 err += verify_sockopt(fd, CUSTOM_INHERIT1, "listen", 1);
85 err += verify_sockopt(fd, CUSTOM_INHERIT2, "listen", 1);
86 err += verify_sockopt(fd, CUSTOM_LISTENER, "listen", 1);
87
88 client_fd = accept(fd, (struct sockaddr *)&addr, &len);
89 if (!ASSERT_GE(client_fd, 0, "accept client"))
90 return NULL;
91
92 err += verify_sockopt(client_fd, CUSTOM_INHERIT1, "accept", 1);
93 err += verify_sockopt(client_fd, CUSTOM_INHERIT2, "accept", 1);
94 err += verify_sockopt(client_fd, CUSTOM_LISTENER, "accept", 0);
95
96 close(client_fd);
97
98 return (void *)(long)err;
99}
100
101static int start_server(void)
102{
103 struct sockaddr_in addr = {
104 .sin_family = AF_INET,
105 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
106 };
107 char buf;
108 int err;
109 int fd;
110 int i;
111
112 fd = socket(AF_INET, SOCK_STREAM, 0);
113 if (fd < 0) {
114 log_err("Failed to create server socket");
115 return -1;
116 }
117
118 for (i = CUSTOM_INHERIT1; i <= CUSTOM_LISTENER; i++) {
119 buf = 0x01;
120 err = setsockopt(fd, SOL_CUSTOM, i, &buf, 1);
121 if (err) {
122 log_err("Failed to call setsockopt(%d)", i);
123 close(fd);
124 return -1;
125 }
126 }
127
128 if (bind(fd, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
129 log_err("Failed to bind socket");
130 close(fd);
131 return -1;
132 }
133
134 return fd;
135}
136
137static void run_test(int cgroup_fd)
138{
139 struct bpf_link *link_getsockopt = NULL;
140 struct bpf_link *link_setsockopt = NULL;
141 int server_fd = -1, client_fd;
142 struct sockopt_inherit *obj;
143 void *server_err;
144 pthread_t tid;
145 int err;
146
147 obj = sockopt_inherit__open_and_load();
148 if (!ASSERT_OK_PTR(obj, "skel-load"))
149 return;
150
151 obj->bss->page_size = sysconf(_SC_PAGESIZE);
152
153 link_getsockopt = bpf_program__attach_cgroup(obj->progs._getsockopt,
154 cgroup_fd);
155 if (!ASSERT_OK_PTR(link_getsockopt, "cg-attach-getsockopt"))
156 goto close_bpf_object;
157
158 link_setsockopt = bpf_program__attach_cgroup(obj->progs._setsockopt,
159 cgroup_fd);
160 if (!ASSERT_OK_PTR(link_setsockopt, "cg-attach-setsockopt"))
161 goto close_bpf_object;
162
163 server_fd = start_server();
164 if (!ASSERT_GE(server_fd, 0, "start_server"))
165 goto close_bpf_object;
166
167 pthread_mutex_lock(&server_started_mtx);
168 if (!ASSERT_OK(pthread_create(&tid, NULL, server_thread,
169 (void *)&server_fd), "pthread_create")) {
170 pthread_mutex_unlock(&server_started_mtx);
171 goto close_server_fd;
172 }
173 pthread_cond_wait(&server_started, &server_started_mtx);
174 pthread_mutex_unlock(&server_started_mtx);
175
176 client_fd = connect_to_server(server_fd);
177 if (!ASSERT_GE(client_fd, 0, "connect_to_server"))
178 goto close_server_fd;
179
180 ASSERT_OK(verify_sockopt(client_fd, CUSTOM_INHERIT1, "connect", 0), "verify_sockopt1");
181 ASSERT_OK(verify_sockopt(client_fd, CUSTOM_INHERIT2, "connect", 0), "verify_sockopt2");
182 ASSERT_OK(verify_sockopt(client_fd, CUSTOM_LISTENER, "connect", 0), "verify_sockopt ener");
183
184 pthread_join(tid, &server_err);
185
186 err = (int)(long)server_err;
187 ASSERT_OK(err, "pthread_join retval");
188
189 close(client_fd);
190
191close_server_fd:
192 close(server_fd);
193close_bpf_object:
194 bpf_link__destroy(link_getsockopt);
195 bpf_link__destroy(link_setsockopt);
196
197 sockopt_inherit__destroy(obj);
198}
199
200void test_sockopt_inherit(void)
201{
202 int cgroup_fd;
203
204 cgroup_fd = test__join_cgroup("/sockopt_inherit");
205 if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
206 return;
207
208 run_test(cgroup_fd);
209 close(cgroup_fd);
210}
1// SPDX-License-Identifier: GPL-2.0
2#include <test_progs.h>
3#include "cgroup_helpers.h"
4
5#define SOL_CUSTOM 0xdeadbeef
6#define CUSTOM_INHERIT1 0
7#define CUSTOM_INHERIT2 1
8#define CUSTOM_LISTENER 2
9
10static int connect_to_server(int server_fd)
11{
12 struct sockaddr_storage addr;
13 socklen_t len = sizeof(addr);
14 int fd;
15
16 fd = socket(AF_INET, SOCK_STREAM, 0);
17 if (fd < 0) {
18 log_err("Failed to create client socket");
19 return -1;
20 }
21
22 if (getsockname(server_fd, (struct sockaddr *)&addr, &len)) {
23 log_err("Failed to get server addr");
24 goto out;
25 }
26
27 if (connect(fd, (const struct sockaddr *)&addr, len) < 0) {
28 log_err("Fail to connect to server");
29 goto out;
30 }
31
32 return fd;
33
34out:
35 close(fd);
36 return -1;
37}
38
39static int verify_sockopt(int fd, int optname, const char *msg, char expected)
40{
41 socklen_t optlen = 1;
42 char buf = 0;
43 int err;
44
45 err = getsockopt(fd, SOL_CUSTOM, optname, &buf, &optlen);
46 if (err) {
47 log_err("%s: failed to call getsockopt", msg);
48 return 1;
49 }
50
51 printf("%s %d: got=0x%x ? expected=0x%x\n", msg, optname, buf, expected);
52
53 if (buf != expected) {
54 log_err("%s: unexpected getsockopt value %d != %d", msg,
55 buf, expected);
56 return 1;
57 }
58
59 return 0;
60}
61
62static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
63static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
64
65static void *server_thread(void *arg)
66{
67 struct sockaddr_storage addr;
68 socklen_t len = sizeof(addr);
69 int fd = *(int *)arg;
70 int client_fd;
71 int err = 0;
72
73 err = listen(fd, 1);
74
75 pthread_mutex_lock(&server_started_mtx);
76 pthread_cond_signal(&server_started);
77 pthread_mutex_unlock(&server_started_mtx);
78
79 if (CHECK_FAIL(err < 0)) {
80 perror("Failed to listed on socket");
81 return NULL;
82 }
83
84 err += verify_sockopt(fd, CUSTOM_INHERIT1, "listen", 1);
85 err += verify_sockopt(fd, CUSTOM_INHERIT2, "listen", 1);
86 err += verify_sockopt(fd, CUSTOM_LISTENER, "listen", 1);
87
88 client_fd = accept(fd, (struct sockaddr *)&addr, &len);
89 if (CHECK_FAIL(client_fd < 0)) {
90 perror("Failed to accept client");
91 return NULL;
92 }
93
94 err += verify_sockopt(client_fd, CUSTOM_INHERIT1, "accept", 1);
95 err += verify_sockopt(client_fd, CUSTOM_INHERIT2, "accept", 1);
96 err += verify_sockopt(client_fd, CUSTOM_LISTENER, "accept", 0);
97
98 close(client_fd);
99
100 return (void *)(long)err;
101}
102
103static int start_server(void)
104{
105 struct sockaddr_in addr = {
106 .sin_family = AF_INET,
107 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
108 };
109 char buf;
110 int err;
111 int fd;
112 int i;
113
114 fd = socket(AF_INET, SOCK_STREAM, 0);
115 if (fd < 0) {
116 log_err("Failed to create server socket");
117 return -1;
118 }
119
120 for (i = CUSTOM_INHERIT1; i <= CUSTOM_LISTENER; i++) {
121 buf = 0x01;
122 err = setsockopt(fd, SOL_CUSTOM, i, &buf, 1);
123 if (err) {
124 log_err("Failed to call setsockopt(%d)", i);
125 close(fd);
126 return -1;
127 }
128 }
129
130 if (bind(fd, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
131 log_err("Failed to bind socket");
132 close(fd);
133 return -1;
134 }
135
136 return fd;
137}
138
139static int prog_attach(struct bpf_object *obj, int cgroup_fd, const char *title)
140{
141 enum bpf_attach_type attach_type;
142 enum bpf_prog_type prog_type;
143 struct bpf_program *prog;
144 int err;
145
146 err = libbpf_prog_type_by_name(title, &prog_type, &attach_type);
147 if (err) {
148 log_err("Failed to deduct types for %s BPF program", title);
149 return -1;
150 }
151
152 prog = bpf_object__find_program_by_title(obj, title);
153 if (!prog) {
154 log_err("Failed to find %s BPF program", title);
155 return -1;
156 }
157
158 err = bpf_prog_attach(bpf_program__fd(prog), cgroup_fd,
159 attach_type, 0);
160 if (err) {
161 log_err("Failed to attach %s BPF program", title);
162 return -1;
163 }
164
165 return 0;
166}
167
168static void run_test(int cgroup_fd)
169{
170 struct bpf_prog_load_attr attr = {
171 .file = "./sockopt_inherit.o",
172 };
173 int server_fd = -1, client_fd;
174 struct bpf_object *obj;
175 void *server_err;
176 pthread_t tid;
177 int ignored;
178 int err;
179
180 err = bpf_prog_load_xattr(&attr, &obj, &ignored);
181 if (CHECK_FAIL(err))
182 return;
183
184 err = prog_attach(obj, cgroup_fd, "cgroup/getsockopt");
185 if (CHECK_FAIL(err))
186 goto close_bpf_object;
187
188 err = prog_attach(obj, cgroup_fd, "cgroup/setsockopt");
189 if (CHECK_FAIL(err))
190 goto close_bpf_object;
191
192 server_fd = start_server();
193 if (CHECK_FAIL(server_fd < 0))
194 goto close_bpf_object;
195
196 pthread_mutex_lock(&server_started_mtx);
197 if (CHECK_FAIL(pthread_create(&tid, NULL, server_thread,
198 (void *)&server_fd)))
199 goto close_server_fd;
200 pthread_cond_wait(&server_started, &server_started_mtx);
201 pthread_mutex_unlock(&server_started_mtx);
202
203 client_fd = connect_to_server(server_fd);
204 if (CHECK_FAIL(client_fd < 0))
205 goto close_server_fd;
206
207 CHECK_FAIL(verify_sockopt(client_fd, CUSTOM_INHERIT1, "connect", 0));
208 CHECK_FAIL(verify_sockopt(client_fd, CUSTOM_INHERIT2, "connect", 0));
209 CHECK_FAIL(verify_sockopt(client_fd, CUSTOM_LISTENER, "connect", 0));
210
211 pthread_join(tid, &server_err);
212
213 err = (int)(long)server_err;
214 CHECK_FAIL(err);
215
216 close(client_fd);
217
218close_server_fd:
219 close(server_fd);
220close_bpf_object:
221 bpf_object__close(obj);
222}
223
224void test_sockopt_inherit(void)
225{
226 int cgroup_fd;
227
228 cgroup_fd = test__join_cgroup("/sockopt_inherit");
229 if (CHECK_FAIL(cgroup_fd < 0))
230 return;
231
232 run_test(cgroup_fd);
233 close(cgroup_fd);
234}