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 
connect_to_server(int server_fd)12 static 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 
36 out:
37 	close(fd);
38 	return -1;
39 }
40 
verify_sockopt(int fd,int optname,const char * msg,char expected)41 static 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 
64 static pthread_mutex_t server_started_mtx = PTHREAD_MUTEX_INITIALIZER;
65 static pthread_cond_t server_started = PTHREAD_COND_INITIALIZER;
66 
server_thread(void * arg)67 static 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 
start_server(void)101 static 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 
run_test(int cgroup_fd)137 static 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 
191 close_server_fd:
192 	close(server_fd);
193 close_bpf_object:
194 	bpf_link__destroy(link_getsockopt);
195 	bpf_link__destroy(link_setsockopt);
196 
197 	sockopt_inherit__destroy(obj);
198 }
199 
test_sockopt_inherit(void)200 void 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 }
211