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 = zsock_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 = zsock_inet_pton(AF_INET, "127.0.0.1", buf);
38 	zassert_equal(res, 1, "");
39 
40 	res = zsock_inet_pton(AF_INET, "127.0.0.1a", buf);
41 	zassert_equal(res, 0, "");
42 
43 	res = zsock_inet_pton(AF_INET6, "a:b:c:d:0:1:2:3", buf);
44 	zassert_equal(res, 1, "");
45 
46 	res = zsock_inet_pton(AF_INET6, "::1", buf);
47 	zassert_equal(res, 1, "");
48 
49 	res = zsock_inet_pton(AF_INET6, "1::", buf);
50 	zassert_equal(res, 1, "");
51 
52 	res = zsock_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.1.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, 1, 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_1,struct sockaddr * peer_addr_2,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_1,
170 			  struct sockaddr *peer_addr_2, socklen_t peer_addrlen,
171 			  struct sockaddr *bind_addr, 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 = zsock_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 = zsock_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 = zsock_setsockopt(sock_c, SOL_SOCKET, SO_BINDTODEVICE, &ifreq,
202 			       sizeof(ifreq));
203 	zassert_equal(ret, 0, "SO_BINDTODEVICE failed, %d", errno);
204 
205 	ret = zsock_sendto(sock_c, send_buf, strlen(send_buf) + 1, 0,
206 			   peer_addr_1, 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 = zsock_setsockopt(sock_c, SOL_SOCKET, SO_BINDTODEVICE, &ifreq,
227 			       sizeof(ifreq));
228 	zassert_equal(ret, 0, "SO_BINDTODEVICE failed, %d", errno);
229 
230 	ret = zsock_sendto(sock_c, send_buf, strlen(send_buf) + 1, 0,
231 			   peer_addr_2, 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 = zsock_recv(sock_s, recv_buf, sizeof(recv_buf), ZSOCK_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 = zsock_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 = zsock_setsockopt(sock_c, SOL_SOCKET, SO_BINDTODEVICE, &ifreq,
265 			       sizeof(ifreq));
266 	zassert_equal(ret, 0, "SO_BINDTODEVICE failed, %d", errno);
267 
268 	ret = zsock_sendto(sock_c, send_buf, strlen(send_buf) + 1, 0,
269 			   peer_addr_1, 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 = zsock_recv(sock_s, recv_buf, sizeof(recv_buf), ZSOCK_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 = zsock_close(sock_c);
289 	zassert_equal(ret, 0, "close failed, %d", errno);
290 	ret = zsock_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 sock_c;
299 	int sock_s;
300 	struct sockaddr_in peer_addr_1 = {
301 		.sin_family = AF_INET,
302 		.sin_port = htons(DST_PORT),
303 		.sin_addr = { { { 192, 0, 1, 1 } } },
304 	};
305 	struct sockaddr_in peer_addr_2 = {
306 		.sin_family = AF_INET,
307 		.sin_port = htons(DST_PORT),
308 		.sin_addr = { { { 192, 0, 2, 2 } } },
309 	};
310 	struct sockaddr_in bind_addr = {
311 		.sin_family = AF_INET,
312 		.sin_port = htons(DST_PORT),
313 		.sin_addr = INADDR_ANY_INIT,
314 	};
315 
316 	sock_c = zsock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
317 	zassert_true(sock_c >= 0, "socket open failed");
318 	sock_s = zsock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
319 	zassert_true(sock_s >= 0, "socket open failed");
320 
321 	test_so_bindtodevice(sock_c, sock_s, (struct sockaddr *)&peer_addr_1,
322 			     (struct sockaddr *)&peer_addr_2, sizeof(peer_addr_1),
323 			     (struct sockaddr *)&bind_addr, sizeof(bind_addr));
324 }
325 
test_ipv6_so_bindtodevice(void)326 void test_ipv6_so_bindtodevice(void)
327 {
328 	int sock_c;
329 	int sock_s;
330 	struct sockaddr_in6 peer_addr_1 = {
331 		.sin6_family = AF_INET6,
332 		.sin6_port = htons(DST_PORT),
333 		.sin6_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
334 					0, 0, 0, 0, 0, 0, 0, 0x1 } } },
335 	};
336 	struct sockaddr_in6 peer_addr_2 = {
337 		.sin6_family = AF_INET6,
338 		.sin6_port = htons(DST_PORT),
339 		.sin6_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
340 					0, 0, 0, 0, 0, 0, 0, 0x2 } } },
341 	};
342 	struct sockaddr_in6 bind_addr = {
343 		.sin6_family = AF_INET6,
344 		.sin6_port = htons(DST_PORT),
345 		.sin6_addr = IN6ADDR_ANY_INIT,
346 	};
347 
348 	sock_c = zsock_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
349 	zassert_true(sock_c >= 0, "socket open failed");
350 	sock_s = zsock_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
351 	zassert_true(sock_s >= 0, "socket open failed");
352 
353 	test_so_bindtodevice(sock_c, sock_s, (struct sockaddr *)&peer_addr_1,
354 			     (struct sockaddr *)&peer_addr_2, sizeof(peer_addr_1),
355 			     (struct sockaddr *)&bind_addr, sizeof(bind_addr));
356 }
357 
358 #define ADDR_SIZE(family) ((family == AF_INET) ? \
359 			   sizeof(struct sockaddr_in) : \
360 			   sizeof(struct sockaddr_in6))
361 
test_getpeername(int family)362 void test_getpeername(int family)
363 {
364 	int ret;
365 	int sock_c;
366 	int sock_s;
367 	struct sockaddr peer_addr;
368 	socklen_t peer_addr_len;
369 	struct sockaddr srv_addr = { 0 };
370 
371 	srv_addr.sa_family = family;
372 	if (family == AF_INET) {
373 		net_sin(&srv_addr)->sin_port = htons(DST_PORT);
374 		ret = zsock_inet_pton(AF_INET, TEST_MY_IPV4_ADDR,
375 				      &net_sin(&srv_addr)->sin_addr);
376 	} else {
377 		net_sin6(&srv_addr)->sin6_port = htons(DST_PORT);
378 		ret = zsock_inet_pton(AF_INET6, TEST_MY_IPV6_ADDR,
379 				      &net_sin6(&srv_addr)->sin6_addr);
380 	}
381 	zassert_equal(ret, 1, "inet_pton failed");
382 
383 	/* UDP socket */
384 	sock_c = zsock_socket(family, SOCK_DGRAM, IPPROTO_UDP);
385 	zassert_true(sock_c >= 0, "socket open failed");
386 
387 	peer_addr_len = ADDR_SIZE(family);
388 	ret = zsock_getpeername(sock_c, &peer_addr, &peer_addr_len);
389 	zassert_equal(ret, -1, "getpeername shouldn've failed");
390 	zassert_equal(errno, ENOTCONN, "getpeername returned invalid error");
391 
392 	ret = zsock_connect(sock_c, &srv_addr, ADDR_SIZE(family));
393 	zassert_equal(ret, 0, "connect failed");
394 
395 	memset(&peer_addr, 0, sizeof(peer_addr));
396 	peer_addr_len = ADDR_SIZE(family);
397 	ret = zsock_getpeername(sock_c, &peer_addr, &peer_addr_len);
398 	zassert_equal(ret, 0, "getpeername failed");
399 	zassert_mem_equal(&peer_addr, &srv_addr, ADDR_SIZE(family),
400 			 "obtained wrong address");
401 
402 	ret = zsock_close(sock_c);
403 	zassert_equal(ret, 0, "close failed, %d", errno);
404 
405 	/* TCP socket */
406 	sock_c = zsock_socket(family, SOCK_STREAM, IPPROTO_TCP);
407 	zassert_true(sock_c >= 0, "socket open failed");
408 	sock_s = zsock_socket(family, SOCK_STREAM, IPPROTO_TCP);
409 	zassert_true(sock_s >= 0, "socket open failed");
410 
411 	ret = zsock_bind(sock_s, &srv_addr, ADDR_SIZE(family));
412 	zassert_equal(ret, 0, "bind failed, %d", errno);
413 
414 	ret = zsock_listen(sock_s, 1);
415 	zassert_equal(ret, 0, "listen failed, %d", errno);
416 
417 	peer_addr_len = ADDR_SIZE(family);
418 	ret = zsock_getpeername(sock_c, &peer_addr, &peer_addr_len);
419 	zassert_equal(ret, -1, "getpeername shouldn've failed");
420 	zassert_equal(errno, ENOTCONN, "getpeername returned invalid error");
421 
422 	ret = zsock_connect(sock_c, &srv_addr, ADDR_SIZE(family));
423 	zassert_equal(ret, 0, "connect failed");
424 
425 	memset(&peer_addr, 0, sizeof(peer_addr));
426 	peer_addr_len = ADDR_SIZE(family);
427 	ret = zsock_getpeername(sock_c, &peer_addr, &peer_addr_len);
428 	zassert_equal(ret, 0, "getpeername failed");
429 	zassert_mem_equal(&peer_addr, &srv_addr, ADDR_SIZE(family),
430 			 "obtained wrong address");
431 
432 	ret = zsock_close(sock_c);
433 	zassert_equal(ret, 0, "close failed, %d", errno);
434 	ret = zsock_close(sock_s);
435 	zassert_equal(ret, 0, "close failed, %d", errno);
436 
437 	k_sleep(K_MSEC(2 * CONFIG_NET_TCP_TIME_WAIT_DELAY));
438 }
439 
440 
test_ipv4_getpeername(void)441 void test_ipv4_getpeername(void)
442 {
443 	test_getpeername(AF_INET);
444 }
445 
test_ipv6_getpeername(void)446 void test_ipv6_getpeername(void)
447 {
448 	test_getpeername(AF_INET6);
449 }
450 
test_getsockname_tcp(int family)451 void test_getsockname_tcp(int family)
452 {
453 	int ret;
454 	int sock_c;
455 	int sock_s;
456 	struct sockaddr local_addr = { 0 };
457 	socklen_t local_addr_len = ADDR_SIZE(family);
458 	struct sockaddr srv_addr = { 0 };
459 
460 	srv_addr.sa_family = family;
461 	if (family == AF_INET) {
462 		net_sin(&srv_addr)->sin_port = htons(DST_PORT);
463 		ret = zsock_inet_pton(AF_INET, TEST_MY_IPV4_ADDR,
464 				      &net_sin(&srv_addr)->sin_addr);
465 	} else {
466 		net_sin6(&srv_addr)->sin6_port = htons(DST_PORT);
467 		ret = zsock_inet_pton(AF_INET6, TEST_MY_IPV6_ADDR,
468 				      &net_sin6(&srv_addr)->sin6_addr);
469 	}
470 	zassert_equal(ret, 1, "inet_pton failed");
471 
472 	sock_c = zsock_socket(family, SOCK_STREAM, IPPROTO_TCP);
473 	zassert_true(sock_c >= 0, "socket open failed");
474 	sock_s = zsock_socket(family, SOCK_STREAM, IPPROTO_TCP);
475 	zassert_true(sock_s >= 0, "socket open failed");
476 
477 	/* Verify that unbound/unconnected socket has no local address set */
478 	ret = zsock_getsockname(sock_c, &local_addr, &local_addr_len);
479 	zassert_equal(ret, -1, "getsockname shouldn've failed");
480 	zassert_equal(errno, EINVAL, "getsockname returned invalid error");
481 	ret = zsock_getsockname(sock_s, &local_addr, &local_addr_len);
482 	zassert_equal(ret, -1, "getsockname shouldn've failed");
483 	zassert_equal(errno, EINVAL, "getsockname returned invalid error");
484 
485 	/* Verify that getsockname() can read local address of a bound socket */
486 	ret = zsock_bind(sock_s, &srv_addr, ADDR_SIZE(family));
487 	zassert_equal(ret, 0, "bind failed, %d", errno);
488 
489 	memset(&local_addr, 0, sizeof(local_addr));
490 	local_addr_len = ADDR_SIZE(family);
491 	ret = zsock_getsockname(sock_s, &local_addr, &local_addr_len);
492 	zassert_equal(ret, 0, "getsockname failed");
493 	zassert_mem_equal(&local_addr, &srv_addr, ADDR_SIZE(family),
494 			 "obtained wrong address");
495 
496 	ret = zsock_listen(sock_s, 1);
497 	zassert_equal(ret, 0, "listen failed, %d", errno);
498 
499 	/* Verify that getsockname() can read local address of a connected socket */
500 	ret = zsock_connect(sock_c, &srv_addr, ADDR_SIZE(family));
501 	zassert_equal(ret, 0, "connect failed");
502 
503 	memset(&local_addr, 0, sizeof(local_addr));
504 	local_addr_len = ADDR_SIZE(family);
505 	ret = zsock_getsockname(sock_c, &local_addr, &local_addr_len);
506 	zassert_equal(ret, 0, "getsockname failed");
507 	zassert_equal(local_addr.sa_family, family, "wrong family");
508 	/* Can't verify address/port of client socket here reliably as they're
509 	 * chosen by net stack
510 	 */
511 
512 	ret = zsock_close(sock_c);
513 	zassert_equal(ret, 0, "close failed, %d", errno);
514 	ret = zsock_close(sock_s);
515 	zassert_equal(ret, 0, "close failed, %d", errno);
516 
517 	k_sleep(K_MSEC(CONFIG_NET_TCP_TIME_WAIT_DELAY));
518 }
519 
test_getsockname_udp(int family)520 void test_getsockname_udp(int family)
521 {
522 	int ret;
523 	int sock_c;
524 	int sock_s;
525 	struct sockaddr local_addr = { 0 };
526 	socklen_t local_addr_len = ADDR_SIZE(family);
527 	struct sockaddr srv_addr = { 0 };
528 
529 	srv_addr.sa_family = family;
530 	if (family == AF_INET) {
531 		net_sin(&srv_addr)->sin_port = htons(DST_PORT);
532 		ret = zsock_inet_pton(AF_INET, TEST_MY_IPV4_ADDR,
533 				      &net_sin(&srv_addr)->sin_addr);
534 	} else {
535 		net_sin6(&srv_addr)->sin6_port = htons(DST_PORT);
536 		ret = zsock_inet_pton(AF_INET6, TEST_MY_IPV6_ADDR,
537 				      &net_sin6(&srv_addr)->sin6_addr);
538 	}
539 	zassert_equal(ret, 1, "inet_pton failed");
540 
541 	sock_c = zsock_socket(family, SOCK_DGRAM, IPPROTO_UDP);
542 	zassert_true(sock_c >= 0, "socket open failed");
543 	sock_s = zsock_socket(family, SOCK_DGRAM, IPPROTO_UDP);
544 	zassert_true(sock_s >= 0, "socket open failed");
545 
546 	/* Verify that unbound/unconnected socket has no local address set */
547 	ret = zsock_getsockname(sock_c, &local_addr, &local_addr_len);
548 	zassert_equal(ret, -1, "getsockname shouldn've failed");
549 	zassert_equal(errno, EINVAL, "getsockname returned invalid error");
550 	ret = zsock_getsockname(sock_s, &local_addr, &local_addr_len);
551 	zassert_equal(ret, -1, "getsockname shouldn've failed");
552 	zassert_equal(errno, EINVAL, "getsockname returned invalid error");
553 
554 	/* Verify that getsockname() can read local address of a bound socket */
555 	ret = zsock_bind(sock_s, &srv_addr, ADDR_SIZE(family));
556 	zassert_equal(ret, 0, "bind failed, %d", errno);
557 
558 	memset(&local_addr, 0, sizeof(local_addr));
559 	local_addr_len = ADDR_SIZE(family);
560 	ret = zsock_getsockname(sock_s, &local_addr, &local_addr_len);
561 	zassert_equal(ret, 0, "getsockname failed");
562 	zassert_mem_equal(&local_addr, &srv_addr, ADDR_SIZE(family),
563 			 "obtained wrong address");
564 
565 	/* Verify that getsockname() can read local address of a connected socket */
566 	ret = zsock_connect(sock_c, &srv_addr, ADDR_SIZE(family));
567 	zassert_equal(ret, 0, "connect failed");
568 
569 	memset(&local_addr, 0, sizeof(local_addr));
570 	local_addr_len = ADDR_SIZE(family);
571 	ret = zsock_getsockname(sock_c, &local_addr, &local_addr_len);
572 	zassert_equal(ret, 0, "getsockname failed");
573 	zassert_equal(local_addr.sa_family, family, "wrong family");
574 	/* Can't verify address/port of client socket here reliably as they're
575 	 * chosen by net stack
576 	 */
577 
578 	ret = zsock_close(sock_c);
579 	zassert_equal(ret, 0, "close failed, %d", errno);
580 	ret = zsock_close(sock_s);
581 	zassert_equal(ret, 0, "close failed, %d", errno);
582 }
583 
584 #define MAPPING_PORT 4244
585 
test_ipv4_mapped_to_ipv6_disabled(void)586 void test_ipv4_mapped_to_ipv6_disabled(void)
587 {
588 	int ret;
589 	int sock_s4, sock_s6;
590 	struct sockaddr srv_addr4 = { 0 };
591 	struct sockaddr srv_addr6 = { 0 };
592 
593 	if (IS_ENABLED(CONFIG_NET_IPV4_MAPPING_TO_IPV6)) {
594 		return;
595 	}
596 
597 	srv_addr4.sa_family = AF_INET;
598 	net_sin(&srv_addr4)->sin_port = htons(MAPPING_PORT);
599 	ret = zsock_inet_pton(AF_INET, TEST_MY_IPV4_ADDR,
600 			      &net_sin(&srv_addr4)->sin_addr);
601 	zassert_equal(ret, 1, "inet_pton failed");
602 
603 	srv_addr6.sa_family = AF_INET6;
604 	net_sin6(&srv_addr6)->sin6_port = htons(MAPPING_PORT);
605 	ret = zsock_inet_pton(AF_INET6, TEST_MY_IPV6_ADDR,
606 			      &net_sin6(&srv_addr6)->sin6_addr);
607 	zassert_equal(ret, 1, "inet_pton failed");
608 
609 	sock_s4 = zsock_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
610 	zassert_true(sock_s4 >= 0, "socket open failed");
611 
612 	ret = zsock_bind(sock_s4, &srv_addr4, ADDR_SIZE(AF_INET));
613 	zassert_equal(ret, 0, "bind failed, %d", errno);
614 
615 	ret = zsock_listen(sock_s4, 1);
616 	zassert_equal(ret, 0, "listen failed, %d", errno);
617 
618 	sock_s6 = zsock_socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
619 	zassert_true(sock_s6 >= 0, "socket open failed");
620 
621 	ret = zsock_bind(sock_s6, &srv_addr6, ADDR_SIZE(AF_INET6));
622 	zassert_equal(ret, 0, "bind failed, %d", errno);
623 
624 	ret = zsock_close(sock_s4);
625 	zassert_equal(ret, 0, "close failed, %d", errno);
626 	ret = zsock_close(sock_s6);
627 	zassert_equal(ret, 0, "close failed, %d", errno);
628 }
629 
test_ipv4_mapped_to_ipv6_enabled(void)630 void test_ipv4_mapped_to_ipv6_enabled(void)
631 {
632 	socklen_t optlen = sizeof(int);
633 	int off = 0;
634 	int ret;
635 	int sock_s4, sock_s6;
636 	struct sockaddr srv_addr4 = { 0 };
637 	struct sockaddr srv_addr6 = { 0 };
638 
639 	if (!IS_ENABLED(CONFIG_NET_IPV4_MAPPING_TO_IPV6)) {
640 		return;
641 	}
642 
643 	/* We must have bound to ANY address so that the
644 	 * v4-mapping-to-v6 works as expected.
645 	 */
646 	srv_addr4.sa_family = AF_INET;
647 	net_sin(&srv_addr4)->sin_port = htons(MAPPING_PORT);
648 	ret = zsock_inet_pton(AF_INET, "0.0.0.0",
649 			      &net_sin(&srv_addr4)->sin_addr);
650 	zassert_equal(ret, 1, "inet_pton failed");
651 
652 	srv_addr6.sa_family = AF_INET6;
653 	net_sin6(&srv_addr6)->sin6_port = htons(MAPPING_PORT);
654 	ret = zsock_inet_pton(AF_INET6, "::",
655 			      &net_sin6(&srv_addr6)->sin6_addr);
656 	zassert_equal(ret, 1, "inet_pton failed");
657 
658 	/* First create IPv6 socket */
659 	sock_s6 = zsock_socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
660 	zassert_true(sock_s6 >= 0, "socket open failed");
661 
662 	ret = zsock_bind(sock_s6, &srv_addr6, ADDR_SIZE(AF_INET6));
663 	zassert_equal(ret, 0, "bind failed, %d", errno);
664 
665 	ret = zsock_listen(sock_s6, 1);
666 	zassert_equal(ret, 0, "listen failed, %d", errno);
667 
668 	/* Then try to create IPv4 socket to the same port */
669 	sock_s4 = zsock_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
670 	zassert_true(sock_s4 >= 0, "socket open failed");
671 
672 	/* Initially the IPV6_V6ONLY is set so the next bind is ok */
673 	ret = zsock_bind(sock_s4, &srv_addr4, ADDR_SIZE(AF_INET));
674 	zassert_equal(ret, 0, "bind failed, %d", errno);
675 
676 	ret = zsock_close(sock_s4);
677 	zassert_equal(ret, 0, "close failed, %d", errno);
678 
679 	/* Then we turn off IPV6_V6ONLY which means that IPv4 and IPv6
680 	 * will have same port space and the next bind should fail.
681 	 */
682 	ret = zsock_setsockopt(sock_s6, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
683 	zassert_equal(ret, 0, "setsockopt failed, %d", errno);
684 
685 	ret = zsock_getsockopt(sock_s6, IPPROTO_IPV6, IPV6_V6ONLY, &off, &optlen);
686 	zassert_equal(ret, 0, "getsockopt failed, %d", errno);
687 	zassert_equal(off, 0, "IPV6_V6ONLY option setting failed");
688 
689 	sock_s4 = zsock_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
690 	zassert_true(sock_s4 >= 0, "socket open failed");
691 
692 	/* Now v4 bind should fail */
693 	ret = zsock_bind(sock_s4, &srv_addr4, ADDR_SIZE(AF_INET));
694 	zassert_equal(ret, -1, "bind failed, %d", errno);
695 	zassert_equal(errno, EADDRINUSE, "bind failed");
696 
697 	ret = zsock_close(sock_s4);
698 	zassert_equal(ret, 0, "close failed, %d", errno);
699 	ret = zsock_close(sock_s6);
700 	zassert_equal(ret, 0, "close failed, %d", errno);
701 }
702 
test_ipv4_mapped_to_ipv6_server(void)703 void test_ipv4_mapped_to_ipv6_server(void)
704 {
705 	socklen_t optlen = sizeof(int);
706 	int off;
707 	int ret, len;
708 	int sock_c4, sock_c6, sock_s6, new_sock;
709 	struct sockaddr srv_addr6 = { 0 };
710 	struct sockaddr srv_addr = { 0 };
711 	struct sockaddr connect_addr6 = { 0 };
712 	struct sockaddr addr = { 0 };
713 	socklen_t addrlen = sizeof(addr);
714 	struct sockaddr_in6 mapped;
715 
716 #define MAX_BUF_SIZE 16
717 	char buf[MAX_BUF_SIZE];
718 
719 	if (!IS_ENABLED(CONFIG_NET_IPV4_MAPPING_TO_IPV6)) {
720 		return;
721 	}
722 
723 	len = strlen("foobar");
724 
725 	/* Create a server socket and try to connect IPv4
726 	 * socket to it. If v4-mapping-to-v6 is enabled,
727 	 * this will work and we should be bound internally
728 	 * to ::ffff:a.b.c.d IPv6 address.
729 	 */
730 	srv_addr.sa_family = AF_INET;
731 	net_sin(&srv_addr)->sin_port = htons(MAPPING_PORT);
732 	ret = zsock_inet_pton(AF_INET, "192.0.1.1",
733 			      &net_sin(&srv_addr)->sin_addr);
734 	zassert_equal(ret, 1, "inet_pton failed");
735 
736 	connect_addr6.sa_family = AF_INET6;
737 	net_sin6(&connect_addr6)->sin6_port = htons(MAPPING_PORT);
738 	ret = zsock_inet_pton(AF_INET6, TEST_PEER_IPV6_ADDR,
739 			      &net_sin6(&connect_addr6)->sin6_addr);
740 	zassert_equal(ret, 1, "inet_pton failed");
741 
742 	net_ipv6_addr_create_v4_mapped(&net_sin(&srv_addr)->sin_addr,
743 				       &mapped.sin6_addr);
744 
745 	/* We must have bound to ANY address so that the
746 	 * v4-mapping-to-v6 works as expected.
747 	 */
748 	srv_addr6.sa_family = AF_INET6;
749 	net_sin6(&srv_addr6)->sin6_port = htons(MAPPING_PORT);
750 	ret = zsock_inet_pton(AF_INET6, "::",
751 			      &net_sin6(&srv_addr6)->sin6_addr);
752 	zassert_equal(ret, 1, "inet_pton failed");
753 
754 	/* First create IPv6 socket */
755 	sock_s6 = zsock_socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
756 	zassert_true(sock_s6 >= 0, "socket open failed");
757 
758 	/* Verify that by default the IPV6_V6ONLY option is set */
759 	ret = zsock_getsockopt(sock_s6, IPPROTO_IPV6, IPV6_V6ONLY, &off, &optlen);
760 	zassert_equal(ret, 0, "getsockopt failed, %d", errno);
761 	zassert_not_equal(off, 0, "IPV6_V6ONLY option setting failed");
762 
763 	/* Then we turn off IPV6_V6ONLY which means that IPv4 and IPv6
764 	 * will have same port space.
765 	 */
766 	off = 0;
767 	ret = zsock_setsockopt(sock_s6, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof(off));
768 	zassert_equal(ret, 0, "setsockopt failed, %d", errno);
769 
770 	ret = zsock_getsockopt(sock_s6, IPPROTO_IPV6, IPV6_V6ONLY, &off, &optlen);
771 	zassert_equal(ret, 0, "getsockopt failed, %d", errno);
772 	zassert_equal(off, 0, "IPV6_V6ONLY option setting failed, %d", off);
773 
774 	ret = zsock_bind(sock_s6, &srv_addr6, ADDR_SIZE(AF_INET6));
775 	zassert_equal(ret, 0, "bind failed, %d", errno);
776 
777 	ret = zsock_listen(sock_s6, 1);
778 	zassert_equal(ret, 0, "listen failed, %d", errno);
779 
780 	/* Then create IPv4 socket and connect to the IPv6 port */
781 	sock_c4 = zsock_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
782 	zassert_true(sock_c4 >= 0, "socket open failed");
783 
784 	ret = zsock_connect(sock_c4, &srv_addr, ADDR_SIZE(AF_INET));
785 	zassert_equal(ret, 0, "connect failed");
786 
787 	new_sock = zsock_accept(sock_s6, &addr, &addrlen);
788 	zassert_true(new_sock >= 0, "accept failed, %d", errno);
789 
790 	/* Note that we should get IPv6 address here (mapped from IPv4) */
791 	zassert_equal(addr.sa_family, AF_INET6, "wrong family");
792 	zassert_equal(addrlen, sizeof(struct sockaddr_in6),
793 		      "wrong addrlen (%d, expecting %d)", addrlen,
794 		      sizeof(struct sockaddr_in6));
795 	zassert_mem_equal(&mapped.sin6_addr, &net_sin6(&addr)->sin6_addr,
796 			  sizeof(struct in6_addr),
797 			  "wrong address (%s, expecting %s)",
798 			  net_sprint_ipv6_addr(&mapped.sin6_addr),
799 			  net_sprint_ipv6_addr(&net_sin6(&addr)->sin6_addr));
800 
801 	/* Send data back to IPv4 client from IPv6 socket */
802 	ret = zsock_send(new_sock, "foobar", len, 0);
803 	zassert_equal(ret, len, "cannot send (%d vs %d), errno %d", ret, len, errno);
804 
805 	addrlen = sizeof(struct sockaddr_in);
806 	ret = zsock_recv(sock_c4, buf, sizeof(buf), 0);
807 	zassert_equal(ret, strlen("foobar"), "cannot recv");
808 
809 	ret = zsock_close(sock_c4);
810 	zassert_equal(ret, 0, "close failed, %d", errno);
811 
812 	(void)zsock_close(new_sock);
813 
814 	/* Let the system stabilize and cleanup itself */
815 	k_sleep(K_MSEC(200));
816 
817 	/* Then create IPv6 socket and make sure it works ok too */
818 	sock_c6 = zsock_socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
819 	zassert_true(sock_c6 >= 0, "socket open failed");
820 
821 	ret = zsock_connect(sock_c6, &connect_addr6, ADDR_SIZE(AF_INET6));
822 	zassert_equal(ret, 0, "connect failed, %d", errno);
823 
824 	new_sock = zsock_accept(sock_s6, &addr, &addrlen);
825 	zassert_true(new_sock >= 0, "accept failed, %d", errno);
826 
827 	ret = zsock_send(new_sock, "foobar", len, 0);
828 	zassert_equal(ret, len, "cannot send (%d vs %d), errno %d", ret, len, errno);
829 
830 	addrlen = sizeof(struct sockaddr_in);
831 	ret = zsock_recv(sock_c6, buf, sizeof(buf), 0);
832 	zassert_equal(ret, strlen("foobar"), "cannot recv");
833 
834 	ret = zsock_close(sock_c6);
835 	zassert_equal(ret, 0, "close failed, %d", errno);
836 
837 	ret = zsock_close(sock_s6);
838 	zassert_equal(ret, 0, "close failed, %d", errno);
839 	ret = zsock_close(new_sock);
840 	zassert_equal(ret, 0, "close failed, %d", errno);
841 }
842 
ZTEST_USER(socket_misc_test_suite,test_ipv4_getsockname_tcp)843 ZTEST_USER(socket_misc_test_suite, test_ipv4_getsockname_tcp)
844 {
845 	test_getsockname_tcp(AF_INET);
846 }
847 
ZTEST_USER(socket_misc_test_suite,test_ipv4_getsockname_udp)848 ZTEST_USER(socket_misc_test_suite, test_ipv4_getsockname_udp)
849 {
850 	test_getsockname_udp(AF_INET);
851 }
852 
ZTEST_USER(socket_misc_test_suite,test_ipv6_getsockname_tcp)853 ZTEST_USER(socket_misc_test_suite, test_ipv6_getsockname_tcp)
854 {
855 	test_getsockname_tcp(AF_INET6);
856 }
857 
ZTEST_USER(socket_misc_test_suite,test_ipv6_getsockname_udp)858 ZTEST_USER(socket_misc_test_suite, test_ipv6_getsockname_udp)
859 {
860 	test_getsockname_udp(AF_INET6);
861 }
862 
setup(void)863 static void *setup(void)
864 {
865 	k_thread_system_pool_assign(k_current_get());
866 	return NULL;
867 }
868 
ZTEST_USER(socket_misc_test_suite,test_ipv4)869 ZTEST_USER(socket_misc_test_suite, test_ipv4)
870 {
871 	test_ipv4_so_bindtodevice();
872 	test_ipv4_getpeername();
873 }
874 
ZTEST_USER(socket_misc_test_suite,test_ipv6)875 ZTEST_USER(socket_misc_test_suite, test_ipv6)
876 {
877 	test_ipv6_so_bindtodevice();
878 	test_ipv6_getpeername();
879 }
880 
ZTEST_USER(socket_misc_test_suite,test_ipv4_mapped_to_ipv6_disabled)881 ZTEST_USER(socket_misc_test_suite, test_ipv4_mapped_to_ipv6_disabled)
882 {
883 	test_ipv4_mapped_to_ipv6_disabled();
884 }
885 
ZTEST_USER(socket_misc_test_suite,test_ipv4_mapped_to_ipv6_enabled)886 ZTEST_USER(socket_misc_test_suite, test_ipv4_mapped_to_ipv6_enabled)
887 {
888 	test_ipv4_mapped_to_ipv6_enabled();
889 }
890 
ZTEST(socket_misc_test_suite,test_ipv4_mapped_to_ipv6_server)891 ZTEST(socket_misc_test_suite, test_ipv4_mapped_to_ipv6_server)
892 {
893 	test_ipv4_mapped_to_ipv6_server();
894 }
895 
ZTEST(socket_misc_test_suite,test_so_domain_socket_option)896 ZTEST(socket_misc_test_suite, test_so_domain_socket_option)
897 {
898 	int ret;
899 	int sock_u;
900 	int sock_t;
901 	socklen_t optlen = sizeof(int);
902 	int domain;
903 
904 	sock_t = zsock_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
905 	zassert_true(sock_t >= 0, "TCP socket open failed");
906 	sock_u = zsock_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
907 	zassert_true(sock_u >= 0, "UDP socket open failed");
908 
909 	ret = zsock_getsockopt(sock_t, SOL_SOCKET, SO_DOMAIN, &domain, &optlen);
910 	zassert_equal(ret, 0, "getsockopt failed, %d", -errno);
911 	zassert_equal(domain, AF_INET, "Mismatch domain value %d vs %d",
912 		      AF_INET, domain);
913 
914 	ret = zsock_getsockopt(sock_u, SOL_SOCKET, SO_DOMAIN, &domain, &optlen);
915 	zassert_equal(ret, 0, "getsockopt failed, %d", -errno);
916 	zassert_equal(domain, AF_INET6, "Mismatch domain value %d vs %d",
917 		      AF_INET6, domain);
918 
919 	/* setsockopt() is not supported for this option */
920 	domain = AF_INET;
921 	ret = zsock_setsockopt(sock_u, SOL_SOCKET, SO_DOMAIN, &domain, optlen);
922 	zassert_equal(ret, -1, "setsockopt succeed");
923 	zassert_equal(errno, ENOPROTOOPT, "Invalid errno %d", errno);
924 
925 	ret = zsock_close(sock_t);
926 	zassert_equal(ret, 0, "close failed, %d", -errno);
927 	ret = zsock_close(sock_u);
928 	zassert_equal(ret, 0, "close failed, %d", -errno);
929 }
930 
931 ZTEST_SUITE(socket_misc_test_suite, NULL, setup, NULL, NULL, NULL);
932