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 
10 static 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 
34 out:
35 	close(fd);
36 	return -1;
37 }
38 
39 static 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 
62 static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
63 static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
64 
65 static 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 (!ASSERT_GE(err, 0, "listed on socket"))
80 		return NULL;
81 
82 	err += verify_sockopt(fd, CUSTOM_INHERIT1, "listen", 1);
83 	err += verify_sockopt(fd, CUSTOM_INHERIT2, "listen", 1);
84 	err += verify_sockopt(fd, CUSTOM_LISTENER, "listen", 1);
85 
86 	client_fd = accept(fd, (struct sockaddr *)&addr, &len);
87 	if (!ASSERT_GE(client_fd, 0, "accept client"))
88 		return NULL;
89 
90 	err += verify_sockopt(client_fd, CUSTOM_INHERIT1, "accept", 1);
91 	err += verify_sockopt(client_fd, CUSTOM_INHERIT2, "accept", 1);
92 	err += verify_sockopt(client_fd, CUSTOM_LISTENER, "accept", 0);
93 
94 	close(client_fd);
95 
96 	return (void *)(long)err;
97 }
98 
99 static int start_server(void)
100 {
101 	struct sockaddr_in addr = {
102 		.sin_family = AF_INET,
103 		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
104 	};
105 	char buf;
106 	int err;
107 	int fd;
108 	int i;
109 
110 	fd = socket(AF_INET, SOCK_STREAM, 0);
111 	if (fd < 0) {
112 		log_err("Failed to create server socket");
113 		return -1;
114 	}
115 
116 	for (i = CUSTOM_INHERIT1; i <= CUSTOM_LISTENER; i++) {
117 		buf = 0x01;
118 		err = setsockopt(fd, SOL_CUSTOM, i, &buf, 1);
119 		if (err) {
120 			log_err("Failed to call setsockopt(%d)", i);
121 			close(fd);
122 			return -1;
123 		}
124 	}
125 
126 	if (bind(fd, (const struct sockaddr *)&addr, sizeof(addr)) < 0) {
127 		log_err("Failed to bind socket");
128 		close(fd);
129 		return -1;
130 	}
131 
132 	return fd;
133 }
134 
135 static int prog_attach(struct bpf_object *obj, int cgroup_fd, const char *title,
136 		       const char *prog_name)
137 {
138 	enum bpf_attach_type attach_type;
139 	enum bpf_prog_type prog_type;
140 	struct bpf_program *prog;
141 	int err;
142 
143 	err = libbpf_prog_type_by_name(title, &prog_type, &attach_type);
144 	if (err) {
145 		log_err("Failed to deduct types for %s BPF program", prog_name);
146 		return -1;
147 	}
148 
149 	prog = bpf_object__find_program_by_name(obj, prog_name);
150 	if (!prog) {
151 		log_err("Failed to find %s BPF program", prog_name);
152 		return -1;
153 	}
154 
155 	err = bpf_prog_attach(bpf_program__fd(prog), cgroup_fd,
156 			      attach_type, 0);
157 	if (err) {
158 		log_err("Failed to attach %s BPF program", prog_name);
159 		return -1;
160 	}
161 
162 	return 0;
163 }
164 
165 static void run_test(int cgroup_fd)
166 {
167 	int server_fd = -1, client_fd;
168 	struct bpf_object *obj;
169 	void *server_err;
170 	pthread_t tid;
171 	int err;
172 
173 	obj = bpf_object__open_file("sockopt_inherit.bpf.o", NULL);
174 	if (!ASSERT_OK_PTR(obj, "obj_open"))
175 		return;
176 
177 	err = bpf_object__load(obj);
178 	if (!ASSERT_OK(err, "obj_load"))
179 		goto close_bpf_object;
180 
181 	err = prog_attach(obj, cgroup_fd, "cgroup/getsockopt", "_getsockopt");
182 	if (!ASSERT_OK(err, "prog_attach _getsockopt"))
183 		goto close_bpf_object;
184 
185 	err = prog_attach(obj, cgroup_fd, "cgroup/setsockopt", "_setsockopt");
186 	if (!ASSERT_OK(err, "prog_attach _setsockopt"))
187 		goto close_bpf_object;
188 
189 	server_fd = start_server();
190 	if (!ASSERT_GE(server_fd, 0, "start_server"))
191 		goto close_bpf_object;
192 
193 	pthread_mutex_lock(&server_started_mtx);
194 	if (!ASSERT_OK(pthread_create(&tid, NULL, server_thread,
195 				      (void *)&server_fd), "pthread_create")) {
196 		pthread_mutex_unlock(&server_started_mtx);
197 		goto close_server_fd;
198 	}
199 	pthread_cond_wait(&server_started, &server_started_mtx);
200 	pthread_mutex_unlock(&server_started_mtx);
201 
202 	client_fd = connect_to_server(server_fd);
203 	if (!ASSERT_GE(client_fd, 0, "connect_to_server"))
204 		goto close_server_fd;
205 
206 	ASSERT_OK(verify_sockopt(client_fd, CUSTOM_INHERIT1, "connect", 0), "verify_sockopt1");
207 	ASSERT_OK(verify_sockopt(client_fd, CUSTOM_INHERIT2, "connect", 0), "verify_sockopt2");
208 	ASSERT_OK(verify_sockopt(client_fd, CUSTOM_LISTENER, "connect", 0), "verify_sockopt ener");
209 
210 	pthread_join(tid, &server_err);
211 
212 	err = (int)(long)server_err;
213 	ASSERT_OK(err, "pthread_join retval");
214 
215 	close(client_fd);
216 
217 close_server_fd:
218 	close(server_fd);
219 close_bpf_object:
220 	bpf_object__close(obj);
221 }
222 
223 void test_sockopt_inherit(void)
224 {
225 	int cgroup_fd;
226 
227 	cgroup_fd = test__join_cgroup("/sockopt_inherit");
228 	if (!ASSERT_GE(cgroup_fd, 0, "join_cgroup"))
229 		return;
230 
231 	run_test(cgroup_fd);
232 	close(cgroup_fd);
233 }
234