1 /*
2  * Copyright (c) 2019 Linaro Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(net_test, CONFIG_NET_SOCKETS_LOG_LEVEL);
9 
10 #include <stdio.h>
11 #include <zephyr/ztest_assert.h>
12 #include <zephyr/sys/sem.h>
13 
14 #include <zephyr/net/socket.h>
15 #include <zephyr/net/dummy.h>
16 
17 #include "net_private.h"
18 
19 #include "../../socket_helpers.h"
20 
ZTEST_USER(socket_misc_test_suite,test_gethostname)21 ZTEST_USER(socket_misc_test_suite, test_gethostname)
22 {
23 	static ZTEST_BMEM char buf[80];
24 	int res;
25 
26 	res = gethostname(buf, sizeof(buf));
27 	zassert_equal(res, 0, "");
28 	printk("%s\n", buf);
29 	zassert_equal(strcmp(buf, "ztest_hostname"), 0, "");
30 }
31 
ZTEST_USER(socket_misc_test_suite,test_inet_pton)32 ZTEST_USER(socket_misc_test_suite, test_inet_pton)
33 {
34 	int res;
35 	uint8_t buf[32];
36 
37 	res = inet_pton(AF_INET, "127.0.0.1", buf);
38 	zassert_equal(res, 1, "");
39 
40 	res = inet_pton(AF_INET, "127.0.0.1a", buf);
41 	zassert_equal(res, 0, "");
42 
43 	res = inet_pton(AF_INET6, "a:b:c:d:0:1:2:3", buf);
44 	zassert_equal(res, 1, "");
45 
46 	res = inet_pton(AF_INET6, "::1", buf);
47 	zassert_equal(res, 1, "");
48 
49 	res = inet_pton(AF_INET6, "1::", buf);
50 	zassert_equal(res, 1, "");
51 
52 	res = inet_pton(AF_INET6, "a:b:c:d:0:1:2:3z", buf);
53 	zassert_equal(res, 0, "");
54 }
55 
56 #define TEST_MY_IPV4_ADDR "192.0.2.1"
57 #define TEST_PEER_IPV4_ADDR "192.0.2.2"
58 #define TEST_MY_IPV6_ADDR "2001:db8::1"
59 #define TEST_PEER_IPV6_ADDR "2001:db8::2"
60 
61 static struct in6_addr my_ipv6_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
62 					     0, 0, 0, 0, 0, 0, 0, 0x1 } } };
63 static struct in_addr my_ipv4_addr1 = { { { 192, 0, 2, 1 } } };
64 
65 static struct in6_addr my_ipv6_addr2 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
66 					     0, 0, 0, 0, 0, 0, 0, 0x2 } } };
67 static struct in_addr my_ipv4_addr2 = { { { 192, 0, 2, 2 } } };
68 
69 static uint8_t lladdr1[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
70 static uint8_t lladdr2[] = { 0x02, 0x02, 0x02, 0x02, 0x02, 0x02 };
71 
72 struct dummy_context {
73 	struct net_if *iface;
74 	uint8_t *mac_address;
75 	struct in6_addr *ipv6_addr;
76 	struct in_addr *ipv4_addr;
77 };
78 
79 static struct dummy_context dummy_data1 = {
80 	.mac_address = lladdr1,
81 	.ipv6_addr = &my_ipv6_addr1,
82 	.ipv4_addr = &my_ipv4_addr1
83 };
84 static struct dummy_context dummy_data2 = {
85 	.mac_address = lladdr2,
86 	.ipv6_addr = &my_ipv6_addr2,
87 	.ipv4_addr = &my_ipv4_addr2
88 };
89 
90 static ZTEST_BMEM const struct device *current_dev;
91 static ZTEST_BMEM struct sys_sem send_sem;
92 
dummy_send(const struct device * dev,struct net_pkt * pkt)93 static int dummy_send(const struct device *dev, struct net_pkt *pkt)
94 {
95 	struct net_pkt *recv_pkt;
96 	int ret;
97 
98 	ARG_UNUSED(dev);
99 
100 	NET_DBG("Sending data (%zd bytes) to iface %d",
101 		net_pkt_get_len(pkt), net_if_get_by_iface(net_pkt_iface(pkt)));
102 
103 	current_dev = dev;
104 
105 	sys_sem_give(&send_sem);
106 
107 	/* Report it back to the interface. */
108 	recv_pkt = net_pkt_clone(pkt, K_NO_WAIT);
109 	if (recv_pkt == NULL) {
110 		return -ENOMEM;
111 	}
112 
113 	ret = net_recv_data(net_pkt_iface(recv_pkt), recv_pkt);
114 	zassert_equal(ret, 0, "Cannot receive data (%d)", ret);
115 
116 	net_pkt_unref(pkt);
117 
118 	return 0;
119 }
120 
dummy_iface_init(struct net_if * iface)121 static void dummy_iface_init(struct net_if *iface)
122 {
123 	const struct device *dev = net_if_get_device(iface);
124 	struct dummy_context *ctx = dev->data;
125 	struct net_if_addr *ifaddr;
126 
127 	ctx->iface = iface;
128 
129 	net_if_set_link_addr(iface, ctx->mac_address, 6, NET_LINK_DUMMY);
130 
131 	ifaddr = net_if_ipv6_addr_add(net_if_lookup_by_dev(dev), ctx->ipv6_addr,
132 				      NET_ADDR_MANUAL, 0);
133 	if (!ifaddr) {
134 		zassert_not_null(ifaddr, "ipv6 addr");
135 	}
136 
137 	ifaddr = net_if_ipv4_addr_add(net_if_lookup_by_dev(dev), ctx->ipv4_addr,
138 				      NET_ADDR_MANUAL, 0);
139 	if (!ifaddr) {
140 		zassert_not_null(ifaddr, "ipv4 addr");
141 	}
142 }
143 
144 static struct dummy_api dummy_api_funcs = {
145 	.iface_api.init = dummy_iface_init,
146 	.send = dummy_send,
147 };
148 
149 #if defined(CONFIG_NET_INTERFACE_NAME)
150 #define DEV1_NAME "dummy0"
151 #define DEV2_NAME "dummy1"
152 #else
153 #define DEV1_NAME "dummy_1"
154 #define DEV2_NAME "dummy_2"
155 #endif
156 
157 NET_DEVICE_INIT(dummy_1, DEV1_NAME, NULL, NULL, &dummy_data1, NULL,
158 		CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &dummy_api_funcs,
159 		DUMMY_L2, NET_L2_GET_CTX_TYPE(DUMMY_L2), 127);
160 
161 
162 NET_DEVICE_INIT(dummy_2, DEV2_NAME, NULL, NULL, &dummy_data2, NULL,
163 		CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &dummy_api_funcs,
164 		DUMMY_L2, NET_L2_GET_CTX_TYPE(DUMMY_L2), 127);
165 
166 #define DST_PORT 4242
167 #define BIND_PORT 4240
168 
test_so_bindtodevice(int sock_c,int sock_s,struct sockaddr * peer_addr,socklen_t peer_addrlen,struct sockaddr * bind_addr,socklen_t bind_addrlen)169 void test_so_bindtodevice(int sock_c, int sock_s, struct sockaddr *peer_addr,
170 			  socklen_t peer_addrlen, struct sockaddr *bind_addr,
171 			  socklen_t bind_addrlen)
172 {
173 	int ret;
174 	struct ifreq ifreq = { 0 };
175 
176 #if !defined(CONFIG_NET_INTERFACE_NAME)
177 	const struct device *dev1 = device_get_binding(DEV1_NAME);
178 	const struct device *dev2 = device_get_binding(DEV2_NAME);
179 #endif
180 
181 	uint8_t send_buf[32];
182 	uint8_t recv_buf[sizeof(send_buf)] = { 0 };
183 
184 	ret = bind(sock_s, bind_addr, bind_addrlen);
185 	zassert_equal(ret, 0, "bind failed, %d", errno);
186 
187 	/* Bind server socket with interface 2. */
188 
189 	strcpy(ifreq.ifr_name, DEV2_NAME);
190 	ret = setsockopt(sock_s, SOL_SOCKET, SO_BINDTODEVICE, &ifreq,
191 			 sizeof(ifreq));
192 	zassert_equal(ret, 0, "SO_BINDTODEVICE failed, %d", errno);
193 
194 	/* Bind client socket with interface 1 and send a packet. */
195 
196 	current_dev = NULL;
197 	sys_sem_init(&send_sem, 0, 1);
198 	strcpy(ifreq.ifr_name, DEV1_NAME);
199 	strcpy(send_buf, DEV1_NAME);
200 
201 	ret = setsockopt(sock_c, SOL_SOCKET, SO_BINDTODEVICE, &ifreq,
202 			 sizeof(ifreq));
203 	zassert_equal(ret, 0, "SO_BINDTODEVICE failed, %d", errno);
204 
205 	ret = sendto(sock_c, send_buf, strlen(send_buf) + 1, 0,
206 		     peer_addr, peer_addrlen);
207 	zassert_equal(ret, strlen(send_buf) + 1, "sendto failed, %d", errno);
208 
209 	ret = sys_sem_take(&send_sem, K_MSEC(100));
210 	zassert_equal(ret, 0, "iface did not receive packet");
211 
212 #if !defined(CONFIG_NET_INTERFACE_NAME)
213 	zassert_equal_ptr(dev1, current_dev, "invalid interface used (%p vs %p)",
214 			  dev1, current_dev);
215 #endif
216 
217 	k_msleep(10);
218 
219 	/* Bind client socket with interface 2 and send a packet. */
220 
221 	current_dev = NULL;
222 	sys_sem_init(&send_sem, 0, 1);
223 	strcpy(ifreq.ifr_name, DEV2_NAME);
224 	strcpy(send_buf, DEV2_NAME);
225 
226 	ret = setsockopt(sock_c, SOL_SOCKET, SO_BINDTODEVICE, &ifreq,
227 			 sizeof(ifreq));
228 	zassert_equal(ret, 0, "SO_BINDTODEVICE failed, %d", errno);
229 
230 	ret = sendto(sock_c, send_buf, strlen(send_buf) + 1, 0,
231 		     peer_addr, peer_addrlen);
232 	zassert_equal(ret, strlen(send_buf) + 1, "sendto failed, %d", errno);
233 
234 	ret = sys_sem_take(&send_sem, K_MSEC(100));
235 	zassert_equal(ret, 0, "iface did not receive packet");
236 
237 #if !defined(CONFIG_NET_INTERFACE_NAME)
238 	zassert_equal_ptr(dev2, current_dev, "invalid interface used (%p vs %p)",
239 			  dev2, current_dev);
240 #endif
241 
242 	/* Server socket should only receive data from the bound interface. */
243 
244 	k_msleep(10);
245 
246 	ret = recv(sock_s, recv_buf, sizeof(recv_buf), MSG_DONTWAIT);
247 	zassert_true(ret > 0, "recv failed, %d", errno);
248 	zassert_mem_equal(recv_buf, DEV2_NAME, strlen(DEV2_NAME),
249 			 "received datagram from invalid interface");
250 
251 	/* Remove the binding from the server socket. */
252 
253 	strcpy(ifreq.ifr_name, "");
254 	ret = setsockopt(sock_s, SOL_SOCKET, SO_BINDTODEVICE, &ifreq,
255 			 sizeof(ifreq));
256 	zassert_equal(ret, 0, "SO_BINDTODEVICE failed, %d", errno);
257 
258 	/* Bind client socket with interface 1 again. */
259 
260 	sys_sem_init(&send_sem, 0, 1);
261 	strcpy(ifreq.ifr_name, DEV1_NAME);
262 	strcpy(send_buf, DEV1_NAME);
263 
264 	ret = setsockopt(sock_c, SOL_SOCKET, SO_BINDTODEVICE, &ifreq,
265 			 sizeof(ifreq));
266 	zassert_equal(ret, 0, "SO_BINDTODEVICE failed, %d", errno);
267 
268 	ret = sendto(sock_c, send_buf, strlen(send_buf) + 1, 0,
269 		     peer_addr, peer_addrlen);
270 	zassert_equal(ret, strlen(send_buf) + 1, "sendto failed, %d", errno);
271 
272 	ret = sys_sem_take(&send_sem, K_MSEC(100));
273 	zassert_equal(ret, 0, "iface did not receive packet");
274 
275 #if !defined(CONFIG_NET_INTERFACE_NAME)
276 	zassert_equal_ptr(dev1, current_dev, "invalid interface used");
277 #endif
278 
279 	/* Server socket should now receive data from interface 1 as well. */
280 
281 	k_msleep(10);
282 
283 	ret = recv(sock_s, recv_buf, sizeof(recv_buf), MSG_DONTWAIT);
284 	zassert_true(ret > 0, "recv failed, %d", errno);
285 	zassert_mem_equal(recv_buf, DEV1_NAME, strlen(DEV1_NAME),
286 			 "received datagram from invalid interface");
287 
288 	ret = close(sock_c);
289 	zassert_equal(ret, 0, "close failed, %d", errno);
290 	ret = close(sock_s);
291 	zassert_equal(ret, 0, "close failed, %d", errno);
292 
293 	k_sleep(K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY));
294 }
295 
test_ipv4_so_bindtodevice(void)296 void test_ipv4_so_bindtodevice(void)
297 {
298 	int ret;
299 	int sock_c;
300 	int sock_s;
301 	struct sockaddr_in peer_addr;
302 	struct sockaddr_in bind_addr = {
303 		.sin_family = AF_INET,
304 		.sin_port = htons(DST_PORT),
305 		.sin_addr = INADDR_ANY_INIT,
306 	};
307 
308 	sock_c = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
309 	zassert_true(sock_c >= 0, "socket open failed");
310 	sock_s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
311 	zassert_true(sock_s >= 0, "socket open failed");
312 
313 	peer_addr.sin_family = AF_INET;
314 	peer_addr.sin_port = htons(DST_PORT);
315 	ret = inet_pton(AF_INET, TEST_PEER_IPV4_ADDR,
316 			&peer_addr.sin_addr);
317 	zassert_equal(ret, 1, "inet_pton failed");
318 
319 	test_so_bindtodevice(sock_c, sock_s, (struct sockaddr *)&peer_addr,
320 			     sizeof(peer_addr), (struct sockaddr *)&bind_addr,
321 			     sizeof(bind_addr));
322 }
323 
test_ipv6_so_bindtodevice(void)324 void test_ipv6_so_bindtodevice(void)
325 {
326 	int ret;
327 	int sock_c;
328 	int sock_s;
329 	struct sockaddr_in6 peer_addr;
330 	struct sockaddr_in6 bind_addr = {
331 		.sin6_family = AF_INET6,
332 		.sin6_port = htons(DST_PORT),
333 		.sin6_addr = IN6ADDR_ANY_INIT,
334 	};
335 
336 	sock_c = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
337 	zassert_true(sock_c >= 0, "socket open failed");
338 	sock_s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
339 	zassert_true(sock_s >= 0, "socket open failed");
340 
341 	peer_addr.sin6_family = AF_INET6;
342 	peer_addr.sin6_port = htons(DST_PORT);
343 	ret = inet_pton(AF_INET6, TEST_PEER_IPV6_ADDR,
344 			&peer_addr.sin6_addr);
345 	zassert_equal(ret, 1, "inet_pton failed");
346 
347 	test_so_bindtodevice(sock_c, sock_s, (struct sockaddr *)&peer_addr,
348 			     sizeof(peer_addr), (struct sockaddr *)&bind_addr,
349 			     sizeof(bind_addr));
350 }
351 
352 #define ADDR_SIZE(family) ((family == AF_INET) ? \
353 			   sizeof(struct sockaddr_in) : \
354 			   sizeof(struct sockaddr_in6))
355 
test_getpeername(int family)356 void test_getpeername(int family)
357 {
358 	int ret;
359 	int sock_c;
360 	int sock_s;
361 	struct sockaddr peer_addr;
362 	socklen_t peer_addr_len;
363 	struct sockaddr srv_addr = { 0 };
364 
365 	srv_addr.sa_family = family;
366 	if (family == AF_INET) {
367 		net_sin(&srv_addr)->sin_port = htons(DST_PORT);
368 		ret = inet_pton(AF_INET, TEST_MY_IPV4_ADDR,
369 				&net_sin(&srv_addr)->sin_addr);
370 	} else {
371 		net_sin6(&srv_addr)->sin6_port = htons(DST_PORT);
372 		ret = inet_pton(AF_INET6, TEST_MY_IPV6_ADDR,
373 				&net_sin6(&srv_addr)->sin6_addr);
374 	}
375 	zassert_equal(ret, 1, "inet_pton failed");
376 
377 	/* UDP socket */
378 	sock_c = socket(family, SOCK_DGRAM, IPPROTO_UDP);
379 	zassert_true(sock_c >= 0, "socket open failed");
380 
381 	peer_addr_len = ADDR_SIZE(family);
382 	ret = getpeername(sock_c, &peer_addr, &peer_addr_len);
383 	zassert_equal(ret, -1, "getpeername shouldn've failed");
384 	zassert_equal(errno, ENOTCONN, "getpeername returned invalid error");
385 
386 	ret = connect(sock_c, &srv_addr, ADDR_SIZE(family));
387 	zassert_equal(ret, 0, "connect failed");
388 
389 	memset(&peer_addr, 0, sizeof(peer_addr));
390 	peer_addr_len = ADDR_SIZE(family);
391 	ret = getpeername(sock_c, &peer_addr, &peer_addr_len);
392 	zassert_equal(ret, 0, "getpeername failed");
393 	zassert_mem_equal(&peer_addr, &srv_addr, ADDR_SIZE(family),
394 			 "obtained wrong address");
395 
396 	ret = close(sock_c);
397 	zassert_equal(ret, 0, "close failed, %d", errno);
398 
399 	/* TCP socket */
400 	sock_c = socket(family, SOCK_STREAM, IPPROTO_TCP);
401 	zassert_true(sock_c >= 0, "socket open failed");
402 	sock_s = socket(family, SOCK_STREAM, IPPROTO_TCP);
403 	zassert_true(sock_s >= 0, "socket open failed");
404 
405 	ret = bind(sock_s, &srv_addr, ADDR_SIZE(family));
406 	zassert_equal(ret, 0, "bind failed, %d", errno);
407 
408 	ret = listen(sock_s, 1);
409 	zassert_equal(ret, 0, "listen failed, %d", errno);
410 
411 	peer_addr_len = ADDR_SIZE(family);
412 	ret = getpeername(sock_c, &peer_addr, &peer_addr_len);
413 	zassert_equal(ret, -1, "getpeername shouldn've failed");
414 	zassert_equal(errno, ENOTCONN, "getpeername returned invalid error");
415 
416 	ret = connect(sock_c, &srv_addr, ADDR_SIZE(family));
417 	zassert_equal(ret, 0, "connect failed");
418 
419 	memset(&peer_addr, 0, sizeof(peer_addr));
420 	peer_addr_len = ADDR_SIZE(family);
421 	ret = getpeername(sock_c, &peer_addr, &peer_addr_len);
422 	zassert_equal(ret, 0, "getpeername failed");
423 	zassert_mem_equal(&peer_addr, &srv_addr, ADDR_SIZE(family),
424 			 "obtained wrong address");
425 
426 	ret = close(sock_c);
427 	zassert_equal(ret, 0, "close failed, %d", errno);
428 	ret = close(sock_s);
429 	zassert_equal(ret, 0, "close failed, %d", errno);
430 
431 	k_sleep(K_MSEC(2 * CONFIG_NET_TCP_TIME_WAIT_DELAY));
432 }
433 
434 
test_ipv4_getpeername(void)435 void test_ipv4_getpeername(void)
436 {
437 	test_getpeername(AF_INET);
438 }
439 
test_ipv6_getpeername(void)440 void test_ipv6_getpeername(void)
441 {
442 	test_getpeername(AF_INET6);
443 }
444 
test_getsockname_tcp(int family)445 void test_getsockname_tcp(int family)
446 {
447 	int ret;
448 	int sock_c;
449 	int sock_s;
450 	struct sockaddr local_addr = { 0 };
451 	socklen_t local_addr_len = ADDR_SIZE(family);
452 	struct sockaddr srv_addr = { 0 };
453 
454 	srv_addr.sa_family = family;
455 	if (family == AF_INET) {
456 		net_sin(&srv_addr)->sin_port = htons(DST_PORT);
457 		ret = inet_pton(AF_INET, TEST_MY_IPV4_ADDR,
458 				&net_sin(&srv_addr)->sin_addr);
459 	} else {
460 		net_sin6(&srv_addr)->sin6_port = htons(DST_PORT);
461 		ret = inet_pton(AF_INET6, TEST_MY_IPV6_ADDR,
462 				&net_sin6(&srv_addr)->sin6_addr);
463 	}
464 	zassert_equal(ret, 1, "inet_pton failed");
465 
466 	sock_c = socket(family, SOCK_STREAM, IPPROTO_TCP);
467 	zassert_true(sock_c >= 0, "socket open failed");
468 	sock_s = socket(family, SOCK_STREAM, IPPROTO_TCP);
469 	zassert_true(sock_s >= 0, "socket open failed");
470 
471 	/* Verify that unbound/unconnected socket has no local address set */
472 	ret = getsockname(sock_c, &local_addr, &local_addr_len);
473 	zassert_equal(ret, -1, "getsockname shouldn've failed");
474 	zassert_equal(errno, EINVAL, "getsockname returned invalid error");
475 	ret = getsockname(sock_s, &local_addr, &local_addr_len);
476 	zassert_equal(ret, -1, "getsockname shouldn've failed");
477 	zassert_equal(errno, EINVAL, "getsockname returned invalid error");
478 
479 	/* Verify that getsockname() can read local address of a bound socket */
480 	ret = bind(sock_s, &srv_addr, ADDR_SIZE(family));
481 	zassert_equal(ret, 0, "bind failed, %d", errno);
482 
483 	memset(&local_addr, 0, sizeof(local_addr));
484 	local_addr_len = ADDR_SIZE(family);
485 	ret = getsockname(sock_s, &local_addr, &local_addr_len);
486 	zassert_equal(ret, 0, "getsockname failed");
487 	zassert_mem_equal(&local_addr, &srv_addr, ADDR_SIZE(family),
488 			 "obtained wrong address");
489 
490 	ret = listen(sock_s, 1);
491 	zassert_equal(ret, 0, "listen failed, %d", errno);
492 
493 	/* Verify that getsockname() can read local address of a connected socket */
494 	ret = connect(sock_c, &srv_addr, ADDR_SIZE(family));
495 	zassert_equal(ret, 0, "connect failed");
496 
497 	memset(&local_addr, 0, sizeof(local_addr));
498 	local_addr_len = ADDR_SIZE(family);
499 	ret = getsockname(sock_c, &local_addr, &local_addr_len);
500 	zassert_equal(ret, 0, "getsockname failed");
501 	zassert_equal(local_addr.sa_family, family, "wrong family");
502 	/* Can't verify address/port of client socket here reliably as they're
503 	 * chosen by net stack
504 	 */
505 
506 	ret = close(sock_c);
507 	zassert_equal(ret, 0, "close failed, %d", errno);
508 	ret = close(sock_s);
509 	zassert_equal(ret, 0, "close failed, %d", errno);
510 
511 	k_sleep(K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY));
512 }
513 
test_getsockname_udp(int family)514 void test_getsockname_udp(int family)
515 {
516 	int ret;
517 	int sock_c;
518 	int sock_s;
519 	struct sockaddr local_addr = { 0 };
520 	socklen_t local_addr_len = ADDR_SIZE(family);
521 	struct sockaddr srv_addr = { 0 };
522 
523 	srv_addr.sa_family = family;
524 	if (family == AF_INET) {
525 		net_sin(&srv_addr)->sin_port = htons(DST_PORT);
526 		ret = inet_pton(AF_INET, TEST_MY_IPV4_ADDR,
527 				&net_sin(&srv_addr)->sin_addr);
528 	} else {
529 		net_sin6(&srv_addr)->sin6_port = htons(DST_PORT);
530 		ret = inet_pton(AF_INET6, TEST_MY_IPV6_ADDR,
531 				&net_sin6(&srv_addr)->sin6_addr);
532 	}
533 	zassert_equal(ret, 1, "inet_pton failed");
534 
535 	sock_c = socket(family, SOCK_DGRAM, IPPROTO_UDP);
536 	zassert_true(sock_c >= 0, "socket open failed");
537 	sock_s = socket(family, SOCK_DGRAM, IPPROTO_UDP);
538 	zassert_true(sock_s >= 0, "socket open failed");
539 
540 	/* Verify that unbound/unconnected socket has no local address set */
541 	ret = getsockname(sock_c, &local_addr, &local_addr_len);
542 	zassert_equal(ret, -1, "getsockname shouldn've failed");
543 	zassert_equal(errno, EINVAL, "getsockname returned invalid error");
544 	ret = getsockname(sock_s, &local_addr, &local_addr_len);
545 	zassert_equal(ret, -1, "getsockname shouldn've failed");
546 	zassert_equal(errno, EINVAL, "getsockname returned invalid error");
547 
548 	/* Verify that getsockname() can read local address of a bound socket */
549 	ret = bind(sock_s, &srv_addr, ADDR_SIZE(family));
550 	zassert_equal(ret, 0, "bind failed, %d", errno);
551 
552 	memset(&local_addr, 0, sizeof(local_addr));
553 	local_addr_len = ADDR_SIZE(family);
554 	ret = getsockname(sock_s, &local_addr, &local_addr_len);
555 	zassert_equal(ret, 0, "getsockname failed");
556 	zassert_mem_equal(&local_addr, &srv_addr, ADDR_SIZE(family),
557 			 "obtained wrong address");
558 
559 	/* Verify that getsockname() can read local address of a connected socket */
560 	ret = connect(sock_c, &srv_addr, ADDR_SIZE(family));
561 	zassert_equal(ret, 0, "connect failed");
562 
563 	memset(&local_addr, 0, sizeof(local_addr));
564 	local_addr_len = ADDR_SIZE(family);
565 	ret = getsockname(sock_c, &local_addr, &local_addr_len);
566 	zassert_equal(ret, 0, "getsockname failed");
567 	zassert_equal(local_addr.sa_family, family, "wrong family");
568 	/* Can't verify address/port of client socket here reliably as they're
569 	 * chosen by net stack
570 	 */
571 
572 	ret = close(sock_c);
573 	zassert_equal(ret, 0, "close failed, %d", errno);
574 	ret = close(sock_s);
575 	zassert_equal(ret, 0, "close failed, %d", errno);
576 }
577 
578 #define MAPPING_PORT 4244
579 
test_ipv4_mapped_to_ipv6_disabled(void)580 void test_ipv4_mapped_to_ipv6_disabled(void)
581 {
582 	int ret;
583 	int sock_s4, sock_s6;
584 	struct sockaddr srv_addr4 = { 0 };
585 	struct sockaddr srv_addr6 = { 0 };
586 
587 	if (IS_ENABLED(CONFIG_NET_IPV4_MAPPING_TO_IPV6)) {
588 		return;
589 	}
590 
591 	srv_addr4.sa_family = AF_INET;
592 	net_sin(&srv_addr4)->sin_port = htons(MAPPING_PORT);
593 	ret = inet_pton(AF_INET, TEST_MY_IPV4_ADDR,
594 			&net_sin(&srv_addr4)->sin_addr);
595 	zassert_equal(ret, 1, "inet_pton failed");
596 
597 	srv_addr6.sa_family = AF_INET6;
598 	net_sin6(&srv_addr6)->sin6_port = htons(MAPPING_PORT);
599 	ret = inet_pton(AF_INET6, TEST_MY_IPV6_ADDR,
600 			&net_sin6(&srv_addr6)->sin6_addr);
601 	zassert_equal(ret, 1, "inet_pton failed");
602 
603 	sock_s4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
604 	zassert_true(sock_s4 >= 0, "socket open failed");
605 
606 	ret = bind(sock_s4, &srv_addr4, ADDR_SIZE(AF_INET));
607 	zassert_equal(ret, 0, "bind failed, %d", errno);
608 
609 	ret = listen(sock_s4, 1);
610 	zassert_equal(ret, 0, "listen failed, %d", errno);
611 
612 	sock_s6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
613 	zassert_true(sock_s6 >= 0, "socket open failed");
614 
615 	ret = bind(sock_s6, &srv_addr6, ADDR_SIZE(AF_INET6));
616 	zassert_equal(ret, 0, "bind failed, %d", errno);
617 
618 	ret = close(sock_s4);
619 	zassert_equal(ret, 0, "close failed, %d", errno);
620 	ret = close(sock_s6);
621 	zassert_equal(ret, 0, "close failed, %d", errno);
622 }
623 
test_ipv4_mapped_to_ipv6_enabled(void)624 void test_ipv4_mapped_to_ipv6_enabled(void)
625 {
626 	socklen_t optlen = sizeof(int);
627 	int off = 0;
628 	int ret;
629 	int sock_s4, sock_s6;
630 	struct sockaddr srv_addr4 = { 0 };
631 	struct sockaddr srv_addr6 = { 0 };
632 
633 	if (!IS_ENABLED(CONFIG_NET_IPV4_MAPPING_TO_IPV6)) {
634 		return;
635 	}
636 
637 	/* We must have bound to ANY address so that the
638 	 * v4-mapping-to-v6 works as expected.
639 	 */
640 	srv_addr4.sa_family = AF_INET;
641 	net_sin(&srv_addr4)->sin_port = htons(MAPPING_PORT);
642 	ret = inet_pton(AF_INET, "0.0.0.0",
643 			&net_sin(&srv_addr4)->sin_addr);
644 	zassert_equal(ret, 1, "inet_pton failed");
645 
646 	srv_addr6.sa_family = AF_INET6;
647 	net_sin6(&srv_addr6)->sin6_port = htons(MAPPING_PORT);
648 	ret = inet_pton(AF_INET6, "::",
649 			&net_sin6(&srv_addr6)->sin6_addr);
650 	zassert_equal(ret, 1, "inet_pton failed");
651 
652 	/* First create IPv6 socket */
653 	sock_s6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
654 	zassert_true(sock_s6 >= 0, "socket open failed");
655 
656 	ret = bind(sock_s6, &srv_addr6, ADDR_SIZE(AF_INET6));
657 	zassert_equal(ret, 0, "bind failed, %d", errno);
658 
659 	ret = listen(sock_s6, 1);
660 	zassert_equal(ret, 0, "listen failed, %d", errno);
661 
662 	/* Then try to create IPv4 socket to the same port */
663 	sock_s4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
664 	zassert_true(sock_s4 >= 0, "socket open failed");
665 
666 	/* Initially the IPV6_V6ONLY is set so the next bind is ok */
667 	ret = bind(sock_s4, &srv_addr4, ADDR_SIZE(AF_INET));
668 	zassert_equal(ret, 0, "bind failed, %d", errno);
669 
670 	ret = close(sock_s4);
671 	zassert_equal(ret, 0, "close failed, %d", errno);
672 
673 	/* Then we turn off IPV6_V6ONLY which means that IPv4 and IPv6
674 	 * will have same port space and the next bind should fail.
675 	 */
676 	ret = setsockopt(sock_s6, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
677 	zassert_equal(ret, 0, "setsockopt failed, %d", errno);
678 
679 	ret = getsockopt(sock_s6, IPPROTO_IPV6, IPV6_V6ONLY, &off, &optlen);
680 	zassert_equal(ret, 0, "getsockopt failed, %d", errno);
681 	zassert_equal(off, 0, "IPV6_V6ONLY option setting failed");
682 
683 	sock_s4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
684 	zassert_true(sock_s4 >= 0, "socket open failed");
685 
686 	/* Now v4 bind should fail */
687 	ret = bind(sock_s4, &srv_addr4, ADDR_SIZE(AF_INET));
688 	zassert_equal(ret, -1, "bind failed, %d", errno);
689 	zassert_equal(errno, EADDRINUSE, "bind failed");
690 
691 	ret = close(sock_s4);
692 	zassert_equal(ret, 0, "close failed, %d", errno);
693 	ret = close(sock_s6);
694 	zassert_equal(ret, 0, "close failed, %d", errno);
695 }
696 
test_ipv4_mapped_to_ipv6_server(void)697 void test_ipv4_mapped_to_ipv6_server(void)
698 {
699 	socklen_t optlen = sizeof(int);
700 	int off;
701 	int ret, len;
702 	int sock_c4, sock_c6, sock_s6, new_sock;
703 	struct sockaddr srv_addr6 = { 0 };
704 	struct sockaddr srv_addr = { 0 };
705 	struct sockaddr connect_addr6 = { 0 };
706 	struct sockaddr addr = { 0 };
707 	socklen_t addrlen = sizeof(addr);
708 	struct sockaddr_in6 mapped;
709 
710 #define MAX_BUF_SIZE 16
711 	char buf[MAX_BUF_SIZE];
712 
713 	if (!IS_ENABLED(CONFIG_NET_IPV4_MAPPING_TO_IPV6)) {
714 		return;
715 	}
716 
717 	len = strlen("foobar");
718 
719 	/* Create a server socket and try to connect IPv4
720 	 * socket to it. If v4-mapping-to-v6 is enabled,
721 	 * this will work and we should be bound internally
722 	 * to ::ffff:a.b.c.d IPv6 address.
723 	 */
724 	srv_addr.sa_family = AF_INET;
725 	net_sin(&srv_addr)->sin_port = htons(MAPPING_PORT);
726 	ret = inet_pton(AF_INET, "192.0.2.1",
727 				&net_sin(&srv_addr)->sin_addr);
728 	zassert_equal(ret, 1, "inet_pton failed");
729 
730 	connect_addr6.sa_family = AF_INET6;
731 	net_sin6(&connect_addr6)->sin6_port = htons(MAPPING_PORT);
732 	ret = inet_pton(AF_INET6, TEST_PEER_IPV6_ADDR,
733 				&net_sin6(&connect_addr6)->sin6_addr);
734 	zassert_equal(ret, 1, "inet_pton failed");
735 
736 	net_ipv6_addr_create_v4_mapped(&net_sin(&srv_addr)->sin_addr,
737 				       &mapped.sin6_addr);
738 
739 	/* We must have bound to ANY address so that the
740 	 * v4-mapping-to-v6 works as expected.
741 	 */
742 	srv_addr6.sa_family = AF_INET6;
743 	net_sin6(&srv_addr6)->sin6_port = htons(MAPPING_PORT);
744 	ret = inet_pton(AF_INET6, "::",
745 			&net_sin6(&srv_addr6)->sin6_addr);
746 	zassert_equal(ret, 1, "inet_pton failed");
747 
748 	/* First create IPv6 socket */
749 	sock_s6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
750 	zassert_true(sock_s6 >= 0, "socket open failed");
751 
752 	/* Verify that by default the IPV6_V6ONLY option is set */
753 	ret = getsockopt(sock_s6, IPPROTO_IPV6, IPV6_V6ONLY, &off, &optlen);
754 	zassert_equal(ret, 0, "getsockopt failed, %d", errno);
755 	zassert_not_equal(off, 0, "IPV6_V6ONLY option setting failed");
756 
757 	/* Then we turn off IPV6_V6ONLY which means that IPv4 and IPv6
758 	 * will have same port space.
759 	 */
760 	off = 0;
761 	ret = setsockopt(sock_s6, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
762 	zassert_equal(ret, 0, "setsockopt failed, %d", errno);
763 
764 	ret = getsockopt(sock_s6, IPPROTO_IPV6, IPV6_V6ONLY, &off, &optlen);
765 	zassert_equal(ret, 0, "getsockopt failed, %d", errno);
766 	zassert_equal(off, 0, "IPV6_V6ONLY option setting failed, %d", off);
767 
768 	ret = bind(sock_s6, &srv_addr6, ADDR_SIZE(AF_INET6));
769 	zassert_equal(ret, 0, "bind failed, %d", errno);
770 
771 	ret = listen(sock_s6, 1);
772 	zassert_equal(ret, 0, "listen failed, %d", errno);
773 
774 	/* Then create IPv4 socket and connect to the IPv6 port */
775 	sock_c4 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
776 	zassert_true(sock_c4 >= 0, "socket open failed");
777 
778 	ret = connect(sock_c4, &srv_addr, ADDR_SIZE(AF_INET));
779 	zassert_equal(ret, 0, "connect failed");
780 
781 	new_sock = accept(sock_s6, &addr, &addrlen);
782 	zassert_true(new_sock >= 0, "accept failed, %d", errno);
783 
784 	/* Note that we should get IPv6 address here (mapped from IPv4) */
785 	zassert_equal(addr.sa_family, AF_INET6, "wrong family");
786 	zassert_equal(addrlen, sizeof(struct sockaddr_in6),
787 		      "wrong addrlen (%d, expecting %d)", addrlen,
788 		      sizeof(struct sockaddr_in6));
789 	zassert_mem_equal(&mapped.sin6_addr, &net_sin6(&addr)->sin6_addr,
790 			  sizeof(struct in6_addr),
791 			  "wrong address (%s, expecting %s)",
792 			  net_sprint_ipv6_addr(&mapped.sin6_addr),
793 			  net_sprint_ipv6_addr(&net_sin6(&addr)->sin6_addr));
794 
795 	/* Send data back to IPv4 client from IPv6 socket */
796 	ret = send(new_sock, "foobar", len, 0);
797 	zassert_equal(ret, len, "cannot send (%d vs %d), errno %d", ret, len, errno);
798 
799 	addrlen = sizeof(struct sockaddr_in);
800 	ret = recv(sock_c4, buf, sizeof(buf), 0);
801 	zassert_equal(ret, strlen("foobar"), "cannot recv");
802 
803 	ret = close(sock_c4);
804 	zassert_equal(ret, 0, "close failed, %d", errno);
805 
806 	(void)close(new_sock);
807 
808 	/* Let the system stabilize and cleanup itself */
809 	k_sleep(K_MSEC(200));
810 
811 	/* Then create IPv6 socket and make sure it works ok too */
812 	sock_c6 = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
813 	zassert_true(sock_c6 >= 0, "socket open failed");
814 
815 	ret = connect(sock_c6, &connect_addr6, ADDR_SIZE(AF_INET6));
816 	zassert_equal(ret, 0, "connect failed, %d", errno);
817 
818 	new_sock = accept(sock_s6, &addr, &addrlen);
819 	zassert_true(new_sock >= 0, "accept failed, %d", errno);
820 
821 	ret = send(new_sock, "foobar", len, 0);
822 	zassert_equal(ret, len, "cannot send (%d vs %d), errno %d", ret, len, errno);
823 
824 	addrlen = sizeof(struct sockaddr_in);
825 	ret = recv(sock_c6, buf, sizeof(buf), 0);
826 	zassert_equal(ret, strlen("foobar"), "cannot recv");
827 
828 	ret = close(sock_c6);
829 	zassert_equal(ret, 0, "close failed, %d", errno);
830 
831 	ret = close(sock_s6);
832 	zassert_equal(ret, 0, "close failed, %d", errno);
833 	ret = close(new_sock);
834 	zassert_equal(ret, 0, "close failed, %d", errno);
835 }
836 
ZTEST_USER(socket_misc_test_suite,test_ipv4_getsockname_tcp)837 ZTEST_USER(socket_misc_test_suite, test_ipv4_getsockname_tcp)
838 {
839 	test_getsockname_tcp(AF_INET);
840 }
841 
ZTEST_USER(socket_misc_test_suite,test_ipv4_getsockname_udp)842 ZTEST_USER(socket_misc_test_suite, test_ipv4_getsockname_udp)
843 {
844 	test_getsockname_udp(AF_INET);
845 }
846 
ZTEST_USER(socket_misc_test_suite,test_ipv6_getsockname_tcp)847 ZTEST_USER(socket_misc_test_suite, test_ipv6_getsockname_tcp)
848 {
849 	test_getsockname_tcp(AF_INET6);
850 }
851 
ZTEST_USER(socket_misc_test_suite,test_ipv6_getsockname_udp)852 ZTEST_USER(socket_misc_test_suite, test_ipv6_getsockname_udp)
853 {
854 	test_getsockname_udp(AF_INET6);
855 }
856 
setup(void)857 static void *setup(void)
858 {
859 	k_thread_system_pool_assign(k_current_get());
860 	return NULL;
861 }
862 
ZTEST_USER(socket_misc_test_suite,test_ipv4)863 ZTEST_USER(socket_misc_test_suite, test_ipv4)
864 {
865 	test_ipv4_so_bindtodevice();
866 	test_ipv4_getpeername();
867 }
868 
ZTEST_USER(socket_misc_test_suite,test_ipv6)869 ZTEST_USER(socket_misc_test_suite, test_ipv6)
870 {
871 	test_ipv6_so_bindtodevice();
872 	test_ipv6_getpeername();
873 }
874 
ZTEST_USER(socket_misc_test_suite,test_ipv4_mapped_to_ipv6_disabled)875 ZTEST_USER(socket_misc_test_suite, test_ipv4_mapped_to_ipv6_disabled)
876 {
877 	test_ipv4_mapped_to_ipv6_disabled();
878 }
879 
ZTEST_USER(socket_misc_test_suite,test_ipv4_mapped_to_ipv6_enabled)880 ZTEST_USER(socket_misc_test_suite, test_ipv4_mapped_to_ipv6_enabled)
881 {
882 	test_ipv4_mapped_to_ipv6_enabled();
883 }
884 
ZTEST(socket_misc_test_suite,test_ipv4_mapped_to_ipv6_server)885 ZTEST(socket_misc_test_suite, test_ipv4_mapped_to_ipv6_server)
886 {
887 	test_ipv4_mapped_to_ipv6_server();
888 }
889 
ZTEST(socket_misc_test_suite,test_so_domain_socket_option)890 ZTEST(socket_misc_test_suite, test_so_domain_socket_option)
891 {
892 	int ret;
893 	int sock_u;
894 	int sock_t;
895 	socklen_t optlen = sizeof(int);
896 	int domain;
897 
898 	sock_t = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
899 	zassert_true(sock_t >= 0, "TCP socket open failed");
900 	sock_u = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
901 	zassert_true(sock_u >= 0, "UDP socket open failed");
902 
903 	ret = getsockopt(sock_t, SOL_SOCKET, SO_DOMAIN, &domain, &optlen);
904 	zassert_equal(ret, 0, "getsockopt failed, %d", -errno);
905 	zassert_equal(domain, AF_INET, "Mismatch domain value %d vs %d",
906 		      AF_INET, domain);
907 
908 	ret = getsockopt(sock_u, SOL_SOCKET, SO_DOMAIN, &domain, &optlen);
909 	zassert_equal(ret, 0, "getsockopt failed, %d", -errno);
910 	zassert_equal(domain, AF_INET6, "Mismatch domain value %d vs %d",
911 		      AF_INET6, domain);
912 
913 	/* setsockopt() is not supported for this option */
914 	domain = AF_INET;
915 	ret = setsockopt(sock_u, SOL_SOCKET, SO_DOMAIN, &domain, optlen);
916 	zassert_equal(ret, -1, "setsockopt succeed");
917 	zassert_equal(errno, ENOPROTOOPT, "Invalid errno %d", errno);
918 
919 	ret = close(sock_t);
920 	zassert_equal(ret, 0, "close failed, %d", -errno);
921 	ret = close(sock_u);
922 	zassert_equal(ret, 0, "close failed, %d", -errno);
923 }
924 
925 ZTEST_SUITE(socket_misc_test_suite, NULL, setup, NULL, NULL, NULL);
926