1 /*
2 * Copyright (c) 2017 Linaro Limited
3 * Copyright (c) 2021 Nordic Semiconductor
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/logging/log.h>
9 LOG_MODULE_REGISTER(net_test, CONFIG_NET_SOCKETS_LOG_LEVEL);
10
11 #include <stdio.h>
12 #include <zephyr/sys/mutex.h>
13 #include <zephyr/ztest_assert.h>
14
15 #include <zephyr/net/socket.h>
16 #include <zephyr/net/ethernet.h>
17 #include <zephyr/net/net_mgmt.h>
18 #include <zephyr/net/net_event.h>
19
20 #include "ipv6.h"
21 #include "net_private.h"
22 #include "../../socket_helpers.h"
23
24 #if defined(CONFIG_NET_SOCKETS_LOG_LEVEL_DBG)
25 #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
26 #else
27 #define DBG(fmt, ...)
28 #endif
29
30 #define BUF_AND_SIZE(buf) buf, sizeof(buf) - 1
31 #define STRLEN(buf) (sizeof(buf) - 1)
32
33 #define TEST_STR_SMALL "test"
34 /* More than 256 bytes, to use >1 net_buf. */
35 #define TEST_STR2 \
36 "The Zephyr Project, a Linux Foundation hosted Collaboration " \
37 "Project, is an open source collaborative effort uniting leaders " \
38 "from across the industry to build a best-in-breed small, scalable, " \
39 "real-time operating system (RTOS) optimized for resource-" \
40 "constrained devices, across multiple architectures."
41 /* More than available TX buffers */
42 static const char test_str_all_tx_bufs[] =
43 #include <string_all_tx_bufs.inc>
44 "!"
45 ;
46
47 #define MY_IPV4_ADDR "127.0.0.1"
48 #define MY_IPV6_ADDR "::1"
49 #define MY_MCAST_IPV4_ADDR "224.0.0.1"
50 #define MY_MCAST_IPV6_ADDR "ff01::1"
51
52 #define ANY_PORT 0
53 #define SERVER_PORT 4242
54 #define CLIENT_PORT 9898
55
56 static ZTEST_BMEM char rx_buf[NET_ETH_MTU + 1];
57
58 /* Common routine to communicate packets over pair of sockets. */
comm_sendto_recvfrom(int client_sock,struct sockaddr * client_addr,socklen_t client_addrlen,int server_sock,struct sockaddr * server_addr,socklen_t server_addrlen)59 static void comm_sendto_recvfrom(int client_sock,
60 struct sockaddr *client_addr,
61 socklen_t client_addrlen,
62 int server_sock,
63 struct sockaddr *server_addr,
64 socklen_t server_addrlen)
65 {
66 int avail;
67 ssize_t sent = 0;
68 ssize_t recved = 0;
69 struct sockaddr addr;
70 socklen_t addrlen;
71 struct sockaddr addr2;
72 socklen_t addrlen2;
73
74 zassert_not_null(client_addr, "null client addr");
75 zassert_not_null(server_addr, "null server addr");
76
77 /*
78 * Test client -> server sending
79 */
80
81 sent = zsock_sendto(client_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL),
82 0, server_addr, server_addrlen);
83 zassert_equal(sent, strlen(TEST_STR_SMALL), "sendto failed");
84
85 k_msleep(100);
86
87 avail = 42;
88 zassert_ok(zsock_ioctl(server_sock, ZFD_IOCTL_FIONREAD, &avail));
89 zassert_equal(avail, strlen(TEST_STR_SMALL));
90
91 /* Test recvfrom(MSG_PEEK) */
92 addrlen = sizeof(addr);
93 clear_buf(rx_buf);
94 recved = zsock_recvfrom(server_sock, rx_buf, sizeof(rx_buf),
95 ZSOCK_MSG_PEEK, &addr, &addrlen);
96 zassert_true(recved >= 0, "recvfrom fail");
97 zassert_equal(recved, strlen(TEST_STR_SMALL),
98 "unexpected received bytes");
99 zassert_mem_equal(rx_buf, BUF_AND_SIZE(TEST_STR_SMALL), "wrong data");
100 zassert_equal(addrlen, client_addrlen, "unexpected addrlen");
101
102 /* Test normal recvfrom() */
103 addrlen = sizeof(addr);
104 clear_buf(rx_buf);
105 recved = zsock_recvfrom(server_sock, rx_buf, sizeof(rx_buf),
106 0, &addr, &addrlen);
107 zassert_true(recved >= 0, "recvfrom fail");
108 zassert_equal(recved, strlen(TEST_STR_SMALL),
109 "unexpected received bytes");
110 zassert_mem_equal(rx_buf, BUF_AND_SIZE(TEST_STR_SMALL), "wrong data");
111 zassert_equal(addrlen, client_addrlen, "unexpected addrlen");
112
113 /* Check the client port */
114 if (net_sin(client_addr)->sin_port != ANY_PORT) {
115 zassert_equal(net_sin(client_addr)->sin_port,
116 net_sin(&addr)->sin_port,
117 "unexpected client port");
118 }
119
120 /*
121 * Test server -> client sending
122 */
123
124 sent = zsock_sendto(server_sock, TEST_STR2, sizeof(TEST_STR2) - 1,
125 0, &addr, addrlen);
126 zassert_equal(sent, STRLEN(TEST_STR2), "sendto failed");
127
128 /* Test normal recvfrom() */
129 addrlen2 = sizeof(addr);
130 clear_buf(rx_buf);
131 recved = zsock_recvfrom(client_sock, rx_buf, sizeof(rx_buf),
132 0, &addr2, &addrlen2);
133 zassert_true(recved >= 0, "recvfrom fail");
134 zassert_equal(recved, STRLEN(TEST_STR2),
135 "unexpected received bytes");
136 zassert_mem_equal(rx_buf, BUF_AND_SIZE(TEST_STR2), "wrong data");
137 zassert_equal(addrlen2, server_addrlen, "unexpected addrlen");
138
139 /* Check the server port */
140 zassert_equal(net_sin(server_addr)->sin_port,
141 net_sin(&addr2)->sin_port,
142 "unexpected server port");
143
144 /* Test that unleft leftover data from datagram is discarded. */
145
146 /* Send 2 datagrams */
147 sent = zsock_sendto(server_sock, TEST_STR2, sizeof(TEST_STR2) - 1,
148 0, &addr, addrlen);
149 zassert_equal(sent, STRLEN(TEST_STR2), "sendto failed");
150 sent = zsock_sendto(server_sock, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1,
151 0, &addr, addrlen);
152 zassert_equal(sent, STRLEN(TEST_STR_SMALL), "sendto failed");
153
154 /* Receive just beginning of 1st datagram */
155 addrlen2 = sizeof(addr);
156 clear_buf(rx_buf);
157 recved = zsock_recvfrom(client_sock, rx_buf, 16, 0, &addr2, &addrlen2);
158 zassert_true(recved == 16, "recvfrom fail");
159 zassert_mem_equal(rx_buf, TEST_STR2, 16, "wrong data");
160
161 /* Make sure that now we receive 2nd datagram */
162 addrlen2 = sizeof(addr);
163 clear_buf(rx_buf);
164 recved = zsock_recvfrom(client_sock, rx_buf, 16, 0, &addr2, &addrlen2);
165 zassert_true(recved == STRLEN(TEST_STR_SMALL), "recvfrom fail");
166 zassert_mem_equal(rx_buf, BUF_AND_SIZE(TEST_STR_SMALL), "wrong data");
167 }
168
ZTEST(net_socket_udp,test_02_v4_sendto_recvfrom)169 ZTEST(net_socket_udp, test_02_v4_sendto_recvfrom)
170 {
171 int rv;
172 int client_sock;
173 int server_sock;
174 struct sockaddr_in client_addr;
175 struct sockaddr_in server_addr;
176
177 prepare_sock_udp_v4(MY_IPV4_ADDR, ANY_PORT, &client_sock, &client_addr);
178 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock, &server_addr);
179
180 rv = zsock_bind(server_sock,
181 (struct sockaddr *)&server_addr,
182 sizeof(server_addr));
183 zassert_equal(rv, 0, "bind failed");
184
185 comm_sendto_recvfrom(client_sock,
186 (struct sockaddr *)&client_addr,
187 sizeof(client_addr),
188 server_sock,
189 (struct sockaddr *)&server_addr,
190 sizeof(server_addr));
191
192 rv = zsock_close(client_sock);
193 zassert_equal(rv, 0, "close failed");
194 rv = zsock_close(server_sock);
195 zassert_equal(rv, 0, "close failed");
196 }
197
ZTEST(net_socket_udp,test_03_v6_sendto_recvfrom)198 ZTEST(net_socket_udp, test_03_v6_sendto_recvfrom)
199 {
200 int rv;
201 int client_sock;
202 int server_sock;
203 struct sockaddr_in6 client_addr;
204 struct sockaddr_in6 server_addr;
205
206 prepare_sock_udp_v6(MY_IPV6_ADDR, ANY_PORT, &client_sock, &client_addr);
207 prepare_sock_udp_v6(MY_IPV6_ADDR, SERVER_PORT, &server_sock, &server_addr);
208
209 rv = zsock_bind(server_sock,
210 (struct sockaddr *)&server_addr, sizeof(server_addr));
211 zassert_equal(rv, 0, "bind failed");
212
213 comm_sendto_recvfrom(client_sock,
214 (struct sockaddr *)&client_addr,
215 sizeof(client_addr),
216 server_sock,
217 (struct sockaddr *)&server_addr,
218 sizeof(server_addr));
219
220 rv = zsock_close(client_sock);
221 zassert_equal(rv, 0, "close failed");
222 rv = zsock_close(server_sock);
223 zassert_equal(rv, 0, "close failed");
224 }
225
ZTEST(net_socket_udp,test_04_v4_bind_sendto)226 ZTEST(net_socket_udp, test_04_v4_bind_sendto)
227 {
228 int rv;
229 int client_sock;
230 int server_sock;
231 struct sockaddr_in client_addr;
232 struct sockaddr_in server_addr;
233
234 prepare_sock_udp_v4(MY_IPV4_ADDR, CLIENT_PORT, &client_sock, &client_addr);
235 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock, &server_addr);
236
237 rv = zsock_bind(client_sock,
238 (struct sockaddr *)&client_addr, sizeof(client_addr));
239 zassert_equal(rv, 0, "bind failed");
240
241 rv = zsock_bind(server_sock,
242 (struct sockaddr *)&server_addr, sizeof(server_addr));
243 zassert_equal(rv, 0, "bind failed");
244
245 comm_sendto_recvfrom(client_sock,
246 (struct sockaddr *)&client_addr,
247 sizeof(client_addr),
248 server_sock,
249 (struct sockaddr *)&server_addr,
250 sizeof(server_addr));
251
252 rv = zsock_close(client_sock);
253 zassert_equal(rv, 0, "close failed");
254 rv = zsock_close(server_sock);
255 zassert_equal(rv, 0, "close failed");
256 }
257
ZTEST(net_socket_udp,test_05_v6_bind_sendto)258 ZTEST(net_socket_udp, test_05_v6_bind_sendto)
259 {
260 int rv;
261 int client_sock;
262 int server_sock;
263 struct sockaddr_in6 client_addr;
264 struct sockaddr_in6 server_addr;
265
266 prepare_sock_udp_v6(MY_IPV6_ADDR, CLIENT_PORT, &client_sock, &client_addr);
267 prepare_sock_udp_v6(MY_IPV6_ADDR, SERVER_PORT, &server_sock, &server_addr);
268
269 rv = zsock_bind(client_sock,
270 (struct sockaddr *)&client_addr, sizeof(client_addr));
271 zassert_equal(rv, 0, "bind failed");
272
273 rv = zsock_bind(server_sock,
274 (struct sockaddr *)&server_addr, sizeof(server_addr));
275 zassert_equal(rv, 0, "bind failed");
276
277 comm_sendto_recvfrom(client_sock,
278 (struct sockaddr *)&client_addr,
279 sizeof(client_addr),
280 server_sock,
281 (struct sockaddr *)&server_addr,
282 sizeof(server_addr));
283
284 rv = zsock_close(client_sock);
285 zassert_equal(rv, 0, "close failed");
286 rv = zsock_close(server_sock);
287 zassert_equal(rv, 0, "close failed");
288 }
289
ZTEST(net_socket_udp,test_01_send_recv_2_sock)290 ZTEST(net_socket_udp, test_01_send_recv_2_sock)
291 {
292 int sock1, sock2;
293 struct sockaddr_in bind_addr, conn_addr;
294 char buf[10];
295 int len, rv;
296
297 prepare_sock_udp_v4(MY_IPV4_ADDR, 55555, &sock1, &bind_addr);
298 prepare_sock_udp_v4(MY_IPV4_ADDR, 55555, &sock2, &conn_addr);
299
300 rv = zsock_bind(sock1, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
301 zassert_equal(rv, 0, "bind failed");
302
303 rv = zsock_connect(sock2, (struct sockaddr *)&conn_addr, sizeof(conn_addr));
304 zassert_equal(rv, 0, "connect failed");
305
306 len = zsock_send(sock2, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, 0);
307 zassert_equal(len, STRLEN(TEST_STR_SMALL), "invalid send len");
308
309 clear_buf(buf);
310 len = zsock_recv(sock1, buf, sizeof(buf), ZSOCK_MSG_PEEK);
311 zassert_equal(len, STRLEN(TEST_STR_SMALL), "Invalid recv len");
312 zassert_mem_equal(buf, BUF_AND_SIZE(TEST_STR_SMALL), "Wrong data");
313
314 clear_buf(buf);
315 len = zsock_recv(sock1, buf, sizeof(buf), 0);
316 zassert_equal(len, STRLEN(TEST_STR_SMALL), "Invalid recv len");
317 zassert_mem_equal(buf, BUF_AND_SIZE(TEST_STR_SMALL), "Wrong data");
318
319 rv = zsock_close(sock1);
320 zassert_equal(rv, 0, "close failed");
321 rv = zsock_close(sock2);
322 zassert_equal(rv, 0, "close failed");
323 }
324
ZTEST(net_socket_udp,test_07_so_priority)325 ZTEST(net_socket_udp, test_07_so_priority)
326 {
327 struct sockaddr_in bind_addr4;
328 struct sockaddr_in6 bind_addr6;
329 int sock1, sock2, rv;
330 uint8_t optval;
331
332 prepare_sock_udp_v4(MY_IPV4_ADDR, 55555, &sock1, &bind_addr4);
333 prepare_sock_udp_v6(MY_IPV6_ADDR, 55555, &sock2, &bind_addr6);
334
335 rv = zsock_bind(sock1, (struct sockaddr *)&bind_addr4, sizeof(bind_addr4));
336 zassert_equal(rv, 0, "bind failed");
337
338 rv = zsock_bind(sock2, (struct sockaddr *)&bind_addr6, sizeof(bind_addr6));
339 zassert_equal(rv, 0, "bind failed");
340
341 optval = 2;
342 rv = zsock_setsockopt(sock1, SOL_SOCKET, SO_PRIORITY, &optval,
343 sizeof(optval));
344 zassert_equal(rv, 0, "setsockopt failed (%d)", errno);
345
346 optval = 6;
347 rv = zsock_setsockopt(sock2, SOL_SOCKET, SO_PRIORITY, &optval,
348 sizeof(optval));
349 zassert_equal(rv, 0, "setsockopt failed");
350
351 rv = zsock_close(sock1);
352 zassert_equal(rv, 0, "close failed");
353 rv = zsock_close(sock2);
354 zassert_equal(rv, 0, "close failed");
355 }
356
comm_sendmsg_recvfrom(int client_sock,struct sockaddr * client_addr,socklen_t client_addrlen,const struct msghdr * client_msg,int server_sock,struct sockaddr * server_addr,socklen_t server_addrlen)357 static void comm_sendmsg_recvfrom(int client_sock,
358 struct sockaddr *client_addr,
359 socklen_t client_addrlen,
360 const struct msghdr *client_msg,
361 int server_sock,
362 struct sockaddr *server_addr,
363 socklen_t server_addrlen)
364 {
365 ssize_t sent;
366 ssize_t recved;
367 struct sockaddr addr;
368 socklen_t addrlen;
369 int len, i;
370
371 zassert_not_null(client_addr, "null client addr");
372 zassert_not_null(server_addr, "null server addr");
373
374 /*
375 * Test client -> server sending
376 */
377
378 sent = zsock_sendmsg(client_sock, client_msg, 0);
379 zassert_true(sent > 0, "sendmsg failed (%d)", -errno);
380
381 for (i = 0, len = 0; i < client_msg->msg_iovlen; i++) {
382 len += client_msg->msg_iov[i].iov_len;
383 }
384
385 zassert_equal(sent, len, "iovec len (%d) vs sent (%d)", len, sent);
386
387 /* Test recvfrom(MSG_PEEK) */
388 addrlen = sizeof(addr);
389 clear_buf(rx_buf);
390 recved = zsock_recvfrom(server_sock, rx_buf, sizeof(rx_buf),
391 ZSOCK_MSG_PEEK, &addr, &addrlen);
392 zassert_true(recved >= 0, "recvfrom fail");
393 zassert_equal(recved, strlen(TEST_STR_SMALL),
394 "unexpected received bytes");
395 zassert_equal(sent, recved, "sent(%d)/received(%d) mismatch",
396 sent, recved);
397
398 zassert_mem_equal(rx_buf, BUF_AND_SIZE(TEST_STR_SMALL),
399 "wrong data (%s)", rx_buf);
400 zassert_equal(addrlen, client_addrlen, "unexpected addrlen");
401
402 /* Test normal recvfrom() */
403 addrlen = sizeof(addr);
404 clear_buf(rx_buf);
405 recved = zsock_recvfrom(server_sock, rx_buf, sizeof(rx_buf),
406 0, &addr, &addrlen);
407 zassert_true(recved >= 0, "recvfrom fail");
408 zassert_equal(recved, strlen(TEST_STR_SMALL),
409 "unexpected received bytes");
410 zassert_mem_equal(rx_buf, BUF_AND_SIZE(TEST_STR_SMALL), "wrong data");
411 zassert_equal(addrlen, client_addrlen, "unexpected addrlen");
412
413 /* Check the client port */
414 if (net_sin(client_addr)->sin_port != ANY_PORT) {
415 zassert_equal(net_sin(client_addr)->sin_port,
416 net_sin(&addr)->sin_port,
417 "unexpected client port");
418 }
419 }
420
ZTEST_USER(net_socket_udp,test_12_v4_sendmsg_recvfrom)421 ZTEST_USER(net_socket_udp, test_12_v4_sendmsg_recvfrom)
422 {
423 int rv;
424 int client_sock;
425 int server_sock;
426 struct sockaddr_in client_addr;
427 struct sockaddr_in server_addr;
428 struct msghdr msg;
429 struct cmsghdr *cmsg;
430 struct iovec io_vector[1];
431 union {
432 struct cmsghdr hdr;
433 unsigned char buf[CMSG_SPACE(sizeof(int))];
434 } cmsgbuf;
435
436 prepare_sock_udp_v4(MY_IPV4_ADDR, ANY_PORT, &client_sock, &client_addr);
437 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock, &server_addr);
438
439 rv = zsock_bind(server_sock,
440 (struct sockaddr *)&server_addr,
441 sizeof(server_addr));
442 zassert_equal(rv, 0, "server bind failed");
443
444 rv = zsock_bind(client_sock,
445 (struct sockaddr *)&client_addr,
446 sizeof(client_addr));
447 zassert_equal(rv, 0, "client bind failed");
448
449 io_vector[0].iov_base = TEST_STR_SMALL;
450 io_vector[0].iov_len = strlen(TEST_STR_SMALL);
451
452 memset(&msg, 0, sizeof(msg));
453 msg.msg_control = &cmsgbuf.buf;
454 msg.msg_controllen = sizeof(cmsgbuf.buf);
455 msg.msg_iov = io_vector;
456 msg.msg_iovlen = 1;
457 msg.msg_name = &server_addr;
458 msg.msg_namelen = sizeof(server_addr);
459
460 cmsg = CMSG_FIRSTHDR(&msg);
461 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
462 cmsg->cmsg_level = SOL_SOCKET;
463 cmsg->cmsg_type = 1122;
464 *(int *)CMSG_DATA(cmsg) = 42;
465
466 comm_sendmsg_recvfrom(client_sock,
467 (struct sockaddr *)&client_addr,
468 sizeof(client_addr),
469 &msg,
470 server_sock,
471 (struct sockaddr *)&server_addr,
472 sizeof(server_addr));
473
474 rv = zsock_close(client_sock);
475 zassert_equal(rv, 0, "close failed");
476 rv = zsock_close(server_sock);
477 zassert_equal(rv, 0, "close failed");
478 }
479
ZTEST_USER(net_socket_udp,test_13_v4_sendmsg_recvfrom_no_aux_data)480 ZTEST_USER(net_socket_udp, test_13_v4_sendmsg_recvfrom_no_aux_data)
481 {
482 int rv;
483 int client_sock;
484 int server_sock;
485 struct sockaddr_in client_addr;
486 struct sockaddr_in server_addr;
487 struct msghdr msg;
488 struct iovec io_vector[1];
489
490 prepare_sock_udp_v4(MY_IPV4_ADDR, ANY_PORT, &client_sock, &client_addr);
491 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock, &server_addr);
492
493 rv = zsock_bind(server_sock,
494 (struct sockaddr *)&server_addr,
495 sizeof(server_addr));
496 zassert_equal(rv, 0, "server bind failed");
497
498 rv = zsock_bind(client_sock,
499 (struct sockaddr *)&client_addr,
500 sizeof(client_addr));
501 zassert_equal(rv, 0, "client bind failed");
502
503 io_vector[0].iov_base = TEST_STR_SMALL;
504 io_vector[0].iov_len = strlen(TEST_STR_SMALL);
505
506 memset(&msg, 0, sizeof(msg));
507 msg.msg_iov = io_vector;
508 msg.msg_iovlen = 1;
509 msg.msg_name = &server_addr;
510 msg.msg_namelen = sizeof(server_addr);
511
512 comm_sendmsg_recvfrom(client_sock,
513 (struct sockaddr *)&client_addr,
514 sizeof(client_addr),
515 &msg,
516 server_sock,
517 (struct sockaddr *)&server_addr,
518 sizeof(server_addr));
519
520 rv = zsock_close(client_sock);
521 zassert_equal(rv, 0, "close failed");
522 rv = zsock_close(server_sock);
523 zassert_equal(rv, 0, "close failed");
524 }
525
ZTEST_USER(net_socket_udp,test_14_v6_sendmsg_recvfrom)526 ZTEST_USER(net_socket_udp, test_14_v6_sendmsg_recvfrom)
527 {
528 int rv;
529 int client_sock;
530 int server_sock;
531 struct sockaddr_in6 client_addr;
532 struct sockaddr_in6 server_addr;
533 struct msghdr msg;
534 struct cmsghdr *cmsg;
535 struct iovec io_vector[1];
536 union {
537 struct cmsghdr hdr;
538 unsigned char buf[CMSG_SPACE(sizeof(int))];
539 } cmsgbuf;
540
541 prepare_sock_udp_v6(MY_IPV6_ADDR, ANY_PORT, &client_sock, &client_addr);
542 prepare_sock_udp_v6(MY_IPV6_ADDR, SERVER_PORT, &server_sock, &server_addr);
543
544 rv = zsock_bind(server_sock,
545 (struct sockaddr *)&server_addr, sizeof(server_addr));
546 zassert_equal(rv, 0, "server bind failed");
547
548 rv = zsock_bind(client_sock,
549 (struct sockaddr *)&client_addr,
550 sizeof(client_addr));
551 zassert_equal(rv, 0, "client bind failed");
552
553 io_vector[0].iov_base = TEST_STR_SMALL;
554 io_vector[0].iov_len = strlen(TEST_STR_SMALL);
555
556 memset(&msg, 0, sizeof(msg));
557 msg.msg_control = &cmsgbuf.buf;
558 msg.msg_controllen = sizeof(cmsgbuf.buf);
559 msg.msg_iov = io_vector;
560 msg.msg_iovlen = 1;
561 msg.msg_name = &server_addr;
562 msg.msg_namelen = sizeof(server_addr);
563
564 cmsg = CMSG_FIRSTHDR(&msg);
565 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
566 cmsg->cmsg_level = SOL_SOCKET;
567 cmsg->cmsg_type = 1122;
568 *(int *)CMSG_DATA(cmsg) = 42;
569
570 comm_sendmsg_recvfrom(client_sock,
571 (struct sockaddr *)&client_addr,
572 sizeof(client_addr),
573 &msg,
574 server_sock,
575 (struct sockaddr *)&server_addr,
576 sizeof(server_addr));
577
578 rv = zsock_close(client_sock);
579 zassert_equal(rv, 0, "close failed");
580 rv = zsock_close(server_sock);
581 zassert_equal(rv, 0, "close failed");
582 }
583
ZTEST_USER(net_socket_udp,test_15_v4_sendmsg_recvfrom_connected)584 ZTEST_USER(net_socket_udp, test_15_v4_sendmsg_recvfrom_connected)
585 {
586 int rv;
587 int client_sock;
588 int server_sock;
589 struct sockaddr_in client_addr;
590 struct sockaddr_in server_addr;
591 struct msghdr msg;
592 struct cmsghdr *cmsg;
593 struct iovec io_vector[1];
594 union {
595 struct cmsghdr hdr;
596 unsigned char buf[CMSG_SPACE(sizeof(int))];
597 } cmsgbuf;
598
599 prepare_sock_udp_v4(MY_IPV4_ADDR, ANY_PORT, &client_sock, &client_addr);
600 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock, &server_addr);
601
602 rv = zsock_bind(server_sock,
603 (struct sockaddr *)&server_addr,
604 sizeof(server_addr));
605 zassert_equal(rv, 0, "server bind failed");
606
607 rv = zsock_bind(client_sock,
608 (struct sockaddr *)&client_addr,
609 sizeof(client_addr));
610 zassert_equal(rv, 0, "client bind failed");
611
612 rv = zsock_connect(client_sock, (struct sockaddr *)&server_addr,
613 sizeof(server_addr));
614 zassert_equal(rv, 0, "connect failed");
615
616 io_vector[0].iov_base = TEST_STR_SMALL;
617 io_vector[0].iov_len = strlen(TEST_STR_SMALL);
618
619 memset(&msg, 0, sizeof(msg));
620 msg.msg_control = &cmsgbuf.buf;
621 msg.msg_controllen = sizeof(cmsgbuf.buf);
622 msg.msg_iov = io_vector;
623 msg.msg_iovlen = 1;
624
625 cmsg = CMSG_FIRSTHDR(&msg);
626 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
627 cmsg->cmsg_level = SOL_SOCKET;
628 cmsg->cmsg_type = 1122;
629 *(int *)CMSG_DATA(cmsg) = 42;
630
631 comm_sendmsg_recvfrom(client_sock,
632 (struct sockaddr *)&client_addr,
633 sizeof(client_addr),
634 &msg,
635 server_sock,
636 (struct sockaddr *)&server_addr,
637 sizeof(server_addr));
638
639 rv = zsock_close(client_sock);
640 zassert_equal(rv, 0, "close failed");
641 rv = zsock_close(server_sock);
642 zassert_equal(rv, 0, "close failed");
643 }
644
ZTEST_USER(net_socket_udp,test_06_v6_sendmsg_recvfrom_connected)645 ZTEST_USER(net_socket_udp, test_06_v6_sendmsg_recvfrom_connected)
646 {
647 int rv;
648 int client_sock;
649 int server_sock;
650 struct sockaddr_in6 client_addr;
651 struct sockaddr_in6 server_addr;
652 struct msghdr msg;
653 struct cmsghdr *cmsg;
654 struct iovec io_vector[1];
655 union {
656 struct cmsghdr hdr;
657 unsigned char buf[CMSG_SPACE(sizeof(int))];
658 } cmsgbuf;
659
660 prepare_sock_udp_v6(MY_IPV6_ADDR, ANY_PORT, &client_sock, &client_addr);
661 prepare_sock_udp_v6(MY_IPV6_ADDR, SERVER_PORT, &server_sock, &server_addr);
662
663 rv = zsock_bind(server_sock,
664 (struct sockaddr *)&server_addr, sizeof(server_addr));
665 zassert_equal(rv, 0, "server bind failed");
666
667 rv = zsock_bind(client_sock,
668 (struct sockaddr *)&client_addr,
669 sizeof(client_addr));
670 zassert_equal(rv, 0, "client bind failed");
671
672 rv = zsock_connect(client_sock, (struct sockaddr *)&server_addr,
673 sizeof(server_addr));
674 zassert_equal(rv, 0, "connect failed");
675
676 io_vector[0].iov_base = TEST_STR_SMALL;
677 io_vector[0].iov_len = strlen(TEST_STR_SMALL);
678
679 memset(&msg, 0, sizeof(msg));
680 msg.msg_control = &cmsgbuf.buf;
681 msg.msg_controllen = sizeof(cmsgbuf.buf);
682 msg.msg_iov = io_vector;
683 msg.msg_iovlen = 1;
684
685 cmsg = CMSG_FIRSTHDR(&msg);
686 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
687 cmsg->cmsg_level = SOL_SOCKET;
688 cmsg->cmsg_type = 1122;
689 *(int *)CMSG_DATA(cmsg) = 42;
690
691 comm_sendmsg_recvfrom(client_sock,
692 (struct sockaddr *)&client_addr,
693 sizeof(client_addr),
694 &msg,
695 server_sock,
696 (struct sockaddr *)&server_addr,
697 sizeof(server_addr));
698
699 rv = zsock_close(client_sock);
700 zassert_equal(rv, 0, "close failed");
701 rv = zsock_close(server_sock);
702 zassert_equal(rv, 0, "close failed");
703 }
704
ZTEST(net_socket_udp,test_06_so_type)705 ZTEST(net_socket_udp, test_06_so_type)
706 {
707 struct sockaddr_in bind_addr4;
708 struct sockaddr_in6 bind_addr6;
709 int sock1, sock2, rv;
710 int optval;
711 socklen_t optsize = sizeof(optval);
712
713 prepare_sock_udp_v4(MY_IPV4_ADDR, 55555, &sock1, &bind_addr4);
714 prepare_sock_udp_v6(MY_IPV6_ADDR, 55555, &sock2, &bind_addr6);
715
716 rv = zsock_getsockopt(sock1, SOL_SOCKET, SO_TYPE, &optval, &optsize);
717 zassert_equal(rv, 0, "getsockopt failed (%d)", errno);
718 zassert_equal(optval, SOCK_DGRAM, "getsockopt got invalid type");
719 zassert_equal(optsize, sizeof(optval), "getsockopt got invalid size");
720
721 rv = zsock_getsockopt(sock2, SOL_SOCKET, SO_TYPE, &optval, &optsize);
722 zassert_equal(rv, 0, "getsockopt failed (%d)", errno);
723 zassert_equal(optval, SOCK_DGRAM, "getsockopt got invalid type");
724 zassert_equal(optsize, sizeof(optval), "getsockopt got invalid size");
725
726 rv = zsock_close(sock1);
727 zassert_equal(rv, 0, "close failed");
728 rv = zsock_close(sock2);
729 zassert_equal(rv, 0, "close failed");
730 }
731
ZTEST(net_socket_udp,test_08_so_txtime)732 ZTEST(net_socket_udp, test_08_so_txtime)
733 {
734 struct sockaddr_in bind_addr4;
735 struct sockaddr_in6 bind_addr6;
736 int sock1, sock2, rv;
737 socklen_t optlen;
738 int optval;
739
740 prepare_sock_udp_v4(MY_IPV4_ADDR, 55555, &sock1, &bind_addr4);
741 prepare_sock_udp_v6(MY_IPV6_ADDR, 55555, &sock2, &bind_addr6);
742
743 rv = zsock_bind(sock1, (struct sockaddr *)&bind_addr4, sizeof(bind_addr4));
744 zassert_equal(rv, 0, "bind failed");
745
746 rv = zsock_bind(sock2, (struct sockaddr *)&bind_addr6, sizeof(bind_addr6));
747 zassert_equal(rv, 0, "bind failed");
748
749 optval = true;
750 rv = zsock_setsockopt(sock1, SOL_SOCKET, SO_TXTIME, &optval,
751 sizeof(optval));
752 zassert_equal(rv, 0, "setsockopt failed (%d)", errno);
753
754 optval = false;
755 rv = zsock_setsockopt(sock2, SOL_SOCKET, SO_TXTIME, &optval,
756 sizeof(optval));
757 zassert_equal(rv, 0, "setsockopt failed");
758
759 optlen = sizeof(optval);
760 rv = zsock_getsockopt(sock1, SOL_SOCKET, SO_TXTIME, &optval, &optlen);
761 zassert_equal(rv, 0, "getsockopt failed (%d)", errno);
762 zassert_equal(optlen, sizeof(optval), "invalid optlen %d vs %d",
763 optlen, sizeof(optval));
764 zassert_equal(optval, true, "getsockopt txtime");
765
766 optlen = sizeof(optval);
767 rv = zsock_getsockopt(sock2, SOL_SOCKET, SO_TXTIME, &optval, &optlen);
768 zassert_equal(rv, 0, "getsockopt failed (%d)", errno);
769 zassert_equal(optlen, sizeof(optval), "invalid optlen %d vs %d",
770 optlen, sizeof(optval));
771 zassert_equal(optval, false, "getsockopt txtime");
772
773 rv = zsock_close(sock1);
774 zassert_equal(rv, 0, "close failed");
775 rv = zsock_close(sock2);
776 zassert_equal(rv, 0, "close failed");
777 }
778
ZTEST(net_socket_udp,test_09_so_rcvtimeo)779 ZTEST(net_socket_udp, test_09_so_rcvtimeo)
780 {
781 struct sockaddr_in bind_addr4;
782 struct sockaddr_in6 bind_addr6;
783 int sock1, sock2, rv;
784 ssize_t recved = 0;
785 struct sockaddr addr;
786 socklen_t addrlen;
787 uint32_t start_time, time_diff;
788
789 struct timeval optval = {
790 .tv_sec = 0,
791 .tv_usec = 300000,
792 };
793
794 prepare_sock_udp_v4(MY_IPV4_ADDR, 55555, &sock1, &bind_addr4);
795 prepare_sock_udp_v6(MY_IPV6_ADDR, 55555, &sock2, &bind_addr6);
796
797 rv = zsock_bind(sock1, (struct sockaddr *)&bind_addr4, sizeof(bind_addr4));
798 zassert_equal(rv, 0, "bind failed");
799
800 rv = zsock_bind(sock2, (struct sockaddr *)&bind_addr6, sizeof(bind_addr6));
801 zassert_equal(rv, 0, "bind failed");
802
803 rv = zsock_setsockopt(sock1, SOL_SOCKET, SO_RCVTIMEO, &optval,
804 sizeof(optval));
805 zassert_equal(rv, 0, "setsockopt failed (%d)", errno);
806
807 optval.tv_usec = 400000;
808 rv = zsock_setsockopt(sock2, SOL_SOCKET, SO_RCVTIMEO, &optval,
809 sizeof(optval));
810 zassert_equal(rv, 0, "setsockopt failed (%d)", errno);
811
812 addrlen = sizeof(addr);
813 clear_buf(rx_buf);
814 start_time = k_uptime_get_32();
815 recved = zsock_recvfrom(sock1, rx_buf, sizeof(rx_buf),
816 0, &addr, &addrlen);
817 time_diff = k_uptime_get_32() - start_time;
818
819 zassert_equal(recved, -1, "Unexpected return code");
820 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
821 zassert_true(time_diff >= 300, "Expected timeout after 300ms but "
822 "was %dms", time_diff);
823
824 start_time = k_uptime_get_32();
825 recved = zsock_recvfrom(sock2, rx_buf, sizeof(rx_buf),
826 0, &addr, &addrlen);
827 time_diff = k_uptime_get_32() - start_time;
828
829 zassert_equal(recved, -1, "Unexpected return code");
830 zassert_equal(errno, EAGAIN, "Unexpected errno value: %d", errno);
831 zassert_true(time_diff >= 400, "Expected timeout after 400ms but "
832 "was %dms", time_diff);
833
834 rv = zsock_close(sock1);
835 zassert_equal(rv, 0, "close failed");
836 rv = zsock_close(sock2);
837 zassert_equal(rv, 0, "close failed");
838 }
839
ZTEST(net_socket_udp,test_10_so_sndtimeo)840 ZTEST(net_socket_udp, test_10_so_sndtimeo)
841 {
842 struct sockaddr_in bind_addr4;
843 struct sockaddr_in6 bind_addr6;
844 int sock1, sock2, rv;
845
846 struct timeval optval = {
847 .tv_sec = 2,
848 .tv_usec = 500000,
849 };
850
851 prepare_sock_udp_v4(MY_IPV4_ADDR, 55555, &sock1, &bind_addr4);
852 prepare_sock_udp_v6(MY_IPV6_ADDR, 55555, &sock2, &bind_addr6);
853
854 rv = zsock_bind(sock1, (struct sockaddr *)&bind_addr4, sizeof(bind_addr4));
855 zassert_equal(rv, 0, "bind failed");
856
857 rv = zsock_bind(sock2, (struct sockaddr *)&bind_addr6, sizeof(bind_addr6));
858 zassert_equal(rv, 0, "bind failed");
859
860 rv = zsock_setsockopt(sock1, SOL_SOCKET, SO_SNDTIMEO, &optval,
861 sizeof(optval));
862 zassert_equal(rv, 0, "setsockopt failed (%d)", errno);
863
864 optval.tv_usec = 0;
865 rv = zsock_setsockopt(sock2, SOL_SOCKET, SO_SNDTIMEO, &optval,
866 sizeof(optval));
867 zassert_equal(rv, 0, "setsockopt failed");
868
869 rv = zsock_close(sock1);
870 zassert_equal(rv, 0, "close failed");
871 rv = zsock_close(sock2);
872 zassert_equal(rv, 0, "close failed");
873 }
874
ZTEST(net_socket_udp,test_11_so_protocol)875 ZTEST(net_socket_udp, test_11_so_protocol)
876 {
877 struct sockaddr_in bind_addr4;
878 struct sockaddr_in6 bind_addr6;
879 int sock1, sock2, rv;
880 int optval;
881 socklen_t optsize = sizeof(optval);
882
883 prepare_sock_udp_v4(MY_IPV4_ADDR, 55555, &sock1, &bind_addr4);
884 prepare_sock_udp_v6(MY_IPV6_ADDR, 55555, &sock2, &bind_addr6);
885
886 rv = zsock_getsockopt(sock1, SOL_SOCKET, SO_PROTOCOL, &optval, &optsize);
887 zassert_equal(rv, 0, "getsockopt failed (%d)", errno);
888 zassert_equal(optval, IPPROTO_UDP, "getsockopt got invalid protocol");
889 zassert_equal(optsize, sizeof(optval), "getsockopt got invalid size");
890
891 rv = zsock_getsockopt(sock2, SOL_SOCKET, SO_PROTOCOL, &optval, &optsize);
892 zassert_equal(rv, 0, "getsockopt failed (%d)", errno);
893 zassert_equal(optval, IPPROTO_UDP, "getsockopt got invalid protocol");
894 zassert_equal(optsize, sizeof(optval), "getsockopt got invalid size");
895
896 rv = zsock_close(sock1);
897 zassert_equal(rv, 0, "close failed");
898 rv = zsock_close(sock2);
899 zassert_equal(rv, 0, "close failed");
900 }
901
comm_sendmsg_with_txtime(int client_sock,struct sockaddr * client_addr,socklen_t client_addrlen,const struct msghdr * client_msg)902 static void comm_sendmsg_with_txtime(int client_sock,
903 struct sockaddr *client_addr,
904 socklen_t client_addrlen,
905 const struct msghdr *client_msg)
906 {
907 ssize_t sent;
908 int len, i;
909
910 zassert_not_null(client_addr, "null client addr");
911
912 /*
913 * Test client -> server sending
914 */
915
916 sent = zsock_sendmsg(client_sock, client_msg, 0);
917 zassert_true(sent > 0, "sendmsg failed (%d)", -errno);
918
919 for (i = 0, len = 0; i < client_msg->msg_iovlen; i++) {
920 len += client_msg->msg_iov[i].iov_len;
921 }
922
923 zassert_equal(sent, len, "iovec len (%d) vs sent (%d)", len, sent);
924 }
925
926 /* In order to verify that the network device driver is able to receive
927 * the TXTIME option, create a separate network device and catch the packets
928 * we are sending.
929 */
930 struct eth_fake_context {
931 struct net_if *iface;
932 uint8_t mac_address[6];
933 };
934
935 static struct eth_fake_context eth_fake_data;
936 static ZTEST_BMEM struct sockaddr_in6 udp_server_addr;
937
938 /* The semaphore is there to wait the data to be received. */
939 static ZTEST_BMEM SYS_MUTEX_DEFINE(wait_data);
940
941 static struct net_if *eth_iface;
942 static struct net_if *lo0;
943 static ZTEST_BMEM bool test_started;
944 static ZTEST_BMEM bool test_failed;
945 static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
946 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
947 static struct in_addr my_addr2 = { { { 192, 0, 2, 2 } } };
948 static struct in6_addr my_addr3 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
949 0, 0, 0, 0, 0, 0, 0, 0x3 } } };
950 static struct in6_addr my_mcast_addr1 = { { { 0xff, 0x01, 0, 0, 0, 0, 0, 0,
951 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
952 static struct in_addr my_mcast_addr2 = { { { 224, 0, 0, 2 } } };
953 static uint8_t server_lladdr[] = { 0x01, 0x02, 0x03, 0xff, 0xfe,
954 0x04, 0x05, 0x06 };
955 static struct net_linkaddr server_link_addr = {
956 .addr = server_lladdr,
957 .len = sizeof(server_lladdr),
958 };
959 #define MY_IPV6_ADDR_ETH "2001:db8:100::1"
960 #define PEER_IPV6_ADDR_ETH "2001:db8:100::2"
961 #define TEST_TXTIME INT64_MAX
962 #define WAIT_TIME K_MSEC(250)
963 #define WAIT_TIME_LONG K_MSEC(1000)
964
eth_fake_iface_init(struct net_if * iface)965 static void eth_fake_iface_init(struct net_if *iface)
966 {
967 const struct device *dev = net_if_get_device(iface);
968 struct eth_fake_context *ctx = dev->data;
969
970 ctx->iface = iface;
971
972 net_if_set_link_addr(iface, ctx->mac_address,
973 sizeof(ctx->mac_address),
974 NET_LINK_ETHERNET);
975
976 ethernet_init(iface);
977 }
978
eth_fake_send(const struct device * dev,struct net_pkt * pkt)979 static int eth_fake_send(const struct device *dev, struct net_pkt *pkt)
980 {
981 net_time_t txtime;
982
983 ARG_UNUSED(dev);
984 ARG_UNUSED(pkt);
985
986 if (!test_started) {
987 return 0;
988 }
989
990 txtime = net_pkt_timestamp_ns(pkt);
991 if (txtime != TEST_TXTIME) {
992 test_failed = true;
993 } else {
994 test_failed = false;
995 }
996
997 sys_mutex_unlock(&wait_data);
998
999 return 0;
1000 }
1001
1002 static struct ethernet_api eth_fake_api_funcs = {
1003 .iface_api.init = eth_fake_iface_init,
1004 .send = eth_fake_send,
1005 };
1006
1007 ETH_NET_DEVICE_INIT(eth_fake, "eth_fake", NULL, NULL, ð_fake_data, NULL,
1008 CONFIG_ETH_INIT_PRIORITY, ð_fake_api_funcs, NET_ETH_MTU);
1009
iface_cb(struct net_if * iface,void * user_data)1010 static void iface_cb(struct net_if *iface, void *user_data)
1011 {
1012 struct net_if **my_iface = user_data;
1013
1014 if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
1015 if (PART_OF_ARRAY(NET_IF_GET_NAME(eth_fake, 0), iface)) {
1016 *my_iface = iface;
1017 }
1018 }
1019
1020 if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
1021 lo0 = iface;
1022 net_if_set_default(iface);
1023 }
1024 }
1025
ZTEST(net_socket_udp,test_17_setup_eth_for_ipv6)1026 ZTEST(net_socket_udp, test_17_setup_eth_for_ipv6)
1027 {
1028 struct net_if_addr *ifaddr;
1029 int ret;
1030
1031 net_if_foreach(iface_cb, ð_iface);
1032 zassert_not_null(eth_iface, "No ethernet interface found");
1033
1034 ifaddr = net_if_ipv6_addr_add(eth_iface, &my_addr1,
1035 NET_ADDR_MANUAL, 0);
1036 if (!ifaddr) {
1037 DBG("Cannot add IPv6 address %s\n",
1038 net_sprint_ipv6_addr(&my_addr1));
1039 zassert_not_null(ifaddr, "addr1");
1040 }
1041
1042 net_if_up(eth_iface);
1043
1044 (void)memset(&udp_server_addr, 0, sizeof(udp_server_addr));
1045 udp_server_addr.sin6_family = AF_INET6;
1046 udp_server_addr.sin6_port = htons(1234);
1047 ret = zsock_inet_pton(AF_INET6, PEER_IPV6_ADDR_ETH, &udp_server_addr.sin6_addr);
1048 zassert_equal(ret, 1, "inet_pton failed");
1049
1050 /* In order to avoid neighbor discovery, populate neighbor cache */
1051 net_ipv6_nbr_add(eth_iface, &udp_server_addr.sin6_addr, &server_link_addr,
1052 true, NET_IPV6_NBR_STATE_REACHABLE);
1053 }
1054
ZTEST_USER(net_socket_udp,test_18_v6_sendmsg_with_txtime)1055 ZTEST_USER(net_socket_udp, test_18_v6_sendmsg_with_txtime)
1056 {
1057 int rv;
1058 int client_sock;
1059 int optval;
1060 net_time_t txtime;
1061 struct sockaddr_in6 client_addr;
1062 struct msghdr msg;
1063 struct cmsghdr *cmsg;
1064 struct iovec io_vector[1];
1065 union {
1066 struct cmsghdr hdr;
1067 unsigned char buf[CMSG_SPACE(sizeof(uint64_t))];
1068 } cmsgbuf;
1069
1070 prepare_sock_udp_v6(MY_IPV6_ADDR_ETH, ANY_PORT, &client_sock, &client_addr);
1071
1072 rv = zsock_bind(client_sock,
1073 (struct sockaddr *)&client_addr,
1074 sizeof(client_addr));
1075 zassert_equal(rv, 0, "client bind failed");
1076
1077 io_vector[0].iov_base = TEST_STR_SMALL;
1078 io_vector[0].iov_len = strlen(TEST_STR_SMALL);
1079
1080 memset(&msg, 0, sizeof(msg));
1081 msg.msg_control = &cmsgbuf.buf;
1082 msg.msg_controllen = sizeof(cmsgbuf.buf);
1083 msg.msg_iov = io_vector;
1084 msg.msg_iovlen = 1;
1085 msg.msg_name = &udp_server_addr;
1086 msg.msg_namelen = sizeof(udp_server_addr);
1087
1088 txtime = TEST_TXTIME;
1089
1090 cmsg = CMSG_FIRSTHDR(&msg);
1091 cmsg->cmsg_len = CMSG_LEN(sizeof(txtime));
1092 cmsg->cmsg_level = SOL_SOCKET;
1093 cmsg->cmsg_type = SCM_TXTIME;
1094 *(net_time_t *)CMSG_DATA(cmsg) = txtime;
1095
1096 optval = true;
1097 rv = zsock_setsockopt(client_sock, SOL_SOCKET, SO_TXTIME, &optval,
1098 sizeof(optval));
1099
1100 test_started = true;
1101
1102 comm_sendmsg_with_txtime(client_sock,
1103 (struct sockaddr *)&client_addr,
1104 sizeof(client_addr),
1105 &msg);
1106
1107 rv = zsock_close(client_sock);
1108 zassert_equal(rv, 0, "close failed");
1109
1110 if (sys_mutex_lock(&wait_data, WAIT_TIME)) {
1111 zassert_true(false, "Timeout DNS query not received");
1112 }
1113
1114 zassert_false(test_failed, "Invalid txtime received");
1115
1116 test_started = false;
1117 }
1118
test_msg_trunc(int sock_c,int sock_s,struct sockaddr * addr_c,socklen_t addrlen_c,struct sockaddr * addr_s,socklen_t addrlen_s)1119 void test_msg_trunc(int sock_c, int sock_s, struct sockaddr *addr_c,
1120 socklen_t addrlen_c, struct sockaddr *addr_s,
1121 socklen_t addrlen_s)
1122 {
1123 int rv;
1124 uint8_t str_buf[sizeof(TEST_STR_SMALL) - 1];
1125
1126 rv = zsock_bind(sock_s, addr_s, addrlen_s);
1127 zassert_equal(rv, 0, "server bind failed");
1128
1129 rv = zsock_bind(sock_c, addr_c, addrlen_c);
1130 zassert_equal(rv, 0, "client bind failed");
1131
1132 rv = zsock_connect(sock_c, addr_s, addrlen_s);
1133 zassert_equal(rv, 0, "connect failed");
1134
1135 /* MSG_TRUNC */
1136
1137 rv = zsock_send(sock_c, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, 0);
1138 zassert_equal(rv, sizeof(TEST_STR_SMALL) - 1, "send failed");
1139
1140 memset(str_buf, 0, sizeof(str_buf));
1141 rv = zsock_recv(sock_s, str_buf, 2, ZSOCK_MSG_TRUNC);
1142 zassert_equal(rv, sizeof(TEST_STR_SMALL) - 1, "MSG_TRUNC flag failed");
1143 zassert_mem_equal(str_buf, TEST_STR_SMALL, 2, "invalid rx data");
1144 zassert_equal(str_buf[2], 0, "received more than requested");
1145
1146 /* The remaining data should've been discarded */
1147 rv = zsock_recv(sock_s, str_buf, sizeof(str_buf), ZSOCK_MSG_DONTWAIT);
1148 zassert_equal(rv, -1, "consecutive recv should've failed");
1149 zassert_equal(errno, EAGAIN, "incorrect errno value");
1150
1151 /* MSG_TRUNC & MSG_PEEK combo */
1152
1153 rv = zsock_send(sock_c, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, 0);
1154 zassert_equal(rv, sizeof(TEST_STR_SMALL) - 1, "send failed");
1155
1156 memset(str_buf, 0, sizeof(str_buf));
1157 rv = zsock_recv(sock_s, str_buf, 2, ZSOCK_MSG_TRUNC | ZSOCK_MSG_PEEK);
1158 zassert_equal(rv, sizeof(TEST_STR_SMALL) - 1, "MSG_TRUNC flag failed");
1159
1160 /* The packet should still be available due to MSG_PEEK */
1161 rv = zsock_recv(sock_s, str_buf, sizeof(str_buf), ZSOCK_MSG_TRUNC);
1162 zassert_equal(rv, sizeof(TEST_STR_SMALL) - 1,
1163 "recv after MSG_PEEK failed");
1164 zassert_mem_equal(str_buf, BUF_AND_SIZE(TEST_STR_SMALL),
1165 "invalid rx data");
1166
1167 rv = zsock_close(sock_c);
1168 zassert_equal(rv, 0, "close failed");
1169 rv = zsock_close(sock_s);
1170 zassert_equal(rv, 0, "close failed");
1171 }
1172
ZTEST(net_socket_udp,test_19_v4_msg_trunc)1173 ZTEST(net_socket_udp, test_19_v4_msg_trunc)
1174 {
1175 int client_sock;
1176 int server_sock;
1177 struct sockaddr_in client_addr;
1178 struct sockaddr_in server_addr;
1179
1180 prepare_sock_udp_v4(MY_IPV4_ADDR, ANY_PORT, &client_sock, &client_addr);
1181 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock, &server_addr);
1182
1183 test_msg_trunc(client_sock, server_sock,
1184 (struct sockaddr *)&client_addr, sizeof(client_addr),
1185 (struct sockaddr *)&server_addr, sizeof(server_addr));
1186 }
1187
ZTEST(net_socket_udp,test_20_v6_msg_trunc)1188 ZTEST(net_socket_udp, test_20_v6_msg_trunc)
1189 {
1190 int client_sock;
1191 int server_sock;
1192 struct sockaddr_in6 client_addr;
1193 struct sockaddr_in6 server_addr;
1194
1195 prepare_sock_udp_v6(MY_IPV6_ADDR, ANY_PORT, &client_sock, &client_addr);
1196 prepare_sock_udp_v6(MY_IPV6_ADDR, SERVER_PORT, &server_sock, &server_addr);
1197
1198 test_msg_trunc(client_sock, server_sock,
1199 (struct sockaddr *)&client_addr, sizeof(client_addr),
1200 (struct sockaddr *)&server_addr, sizeof(server_addr));
1201 }
1202
test_dgram_overflow(int sock_c,int sock_s,struct sockaddr * addr_c,socklen_t addrlen_c,struct sockaddr * addr_s,socklen_t addrlen_s,const void * buf,size_t buf_size)1203 static void test_dgram_overflow(int sock_c, int sock_s,
1204 struct sockaddr *addr_c, socklen_t addrlen_c,
1205 struct sockaddr *addr_s, socklen_t addrlen_s,
1206 const void *buf, size_t buf_size)
1207 {
1208 int rv;
1209
1210 rv = zsock_bind(sock_s, addr_s, addrlen_s);
1211 zassert_equal(rv, 0, "server bind failed");
1212
1213 rv = zsock_bind(sock_c, addr_c, addrlen_c);
1214 zassert_equal(rv, 0, "client bind failed");
1215
1216 rv = zsock_connect(sock_c, addr_s, addrlen_s);
1217 zassert_equal(rv, 0, "connect failed");
1218
1219 rv = zsock_send(sock_c, buf, buf_size, 0);
1220 zassert_equal(rv, -1, "send succeeded");
1221 zassert_equal(errno, ENOMEM, "incorrect errno value");
1222
1223 rv = zsock_close(sock_c);
1224 zassert_equal(rv, 0, "close failed");
1225 rv = zsock_close(sock_s);
1226 zassert_equal(rv, 0, "close failed");
1227 }
1228
test_dgram_fragmented(int sock_c,int sock_s,struct sockaddr * addr_c,socklen_t addrlen_c,struct sockaddr * addr_s,socklen_t addrlen_s,const void * buf,size_t buf_size)1229 static void test_dgram_fragmented(int sock_c, int sock_s,
1230 struct sockaddr *addr_c, socklen_t addrlen_c,
1231 struct sockaddr *addr_s, socklen_t addrlen_s,
1232 const void *buf, size_t buf_size)
1233 {
1234 int rv;
1235
1236 rv = zsock_bind(sock_s, addr_s, addrlen_s);
1237 zassert_equal(rv, 0, "server bind failed");
1238
1239 rv = zsock_bind(sock_c, addr_c, addrlen_c);
1240 zassert_equal(rv, 0, "client bind failed");
1241
1242 rv = zsock_connect(sock_c, addr_s, addrlen_s);
1243 zassert_equal(rv, 0, "connect failed");
1244
1245 rv = zsock_send(sock_c, buf, buf_size, 0);
1246 zassert_equal(rv, buf_size, "send failed");
1247
1248 memset(rx_buf, 0, sizeof(rx_buf));
1249 rv = zsock_recv(sock_s, rx_buf, sizeof(rx_buf), 0);
1250 zassert_equal(rv, buf_size, "recv failed");
1251 zassert_mem_equal(rx_buf, buf, buf_size, "wrong data");
1252
1253 rv = zsock_close(sock_c);
1254 zassert_equal(rv, 0, "close failed");
1255 rv = zsock_close(sock_s);
1256 zassert_equal(rv, 0, "close failed");
1257 }
1258
ZTEST(net_socket_udp,test_21_v4_dgram_overflow)1259 ZTEST(net_socket_udp, test_21_v4_dgram_overflow)
1260 {
1261 int client_sock;
1262 int server_sock;
1263 struct sockaddr_in client_addr;
1264 struct sockaddr_in server_addr;
1265
1266 prepare_sock_udp_v4(MY_IPV4_ADDR, ANY_PORT, &client_sock, &client_addr);
1267 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock, &server_addr);
1268
1269 test_dgram_overflow(client_sock, server_sock,
1270 (struct sockaddr *)&client_addr, sizeof(client_addr),
1271 (struct sockaddr *)&server_addr, sizeof(server_addr),
1272 test_str_all_tx_bufs, NET_ETH_MTU + 1);
1273 }
1274
ZTEST(net_socket_udp,test_22_v6_dgram_fragmented_or_overflow)1275 ZTEST(net_socket_udp, test_22_v6_dgram_fragmented_or_overflow)
1276 {
1277 int client_sock;
1278 int server_sock;
1279 struct sockaddr_in6 client_addr;
1280 struct sockaddr_in6 server_addr;
1281
1282 prepare_sock_udp_v6(MY_IPV6_ADDR, ANY_PORT, &client_sock, &client_addr);
1283 prepare_sock_udp_v6(MY_IPV6_ADDR, SERVER_PORT, &server_sock, &server_addr);
1284
1285 if (IS_ENABLED(CONFIG_NET_IPV6_FRAGMENT)) {
1286 test_dgram_fragmented(client_sock, server_sock,
1287 (struct sockaddr *)&client_addr, sizeof(client_addr),
1288 (struct sockaddr *)&server_addr, sizeof(server_addr),
1289 test_str_all_tx_bufs, NET_ETH_MTU + 1);
1290 } else {
1291 test_dgram_overflow(client_sock, server_sock,
1292 (struct sockaddr *)&client_addr, sizeof(client_addr),
1293 (struct sockaddr *)&server_addr, sizeof(server_addr),
1294 test_str_all_tx_bufs, NET_ETH_MTU + 1);
1295 }
1296 }
1297
ZTEST(net_socket_udp,test_23_v6_dgram_overflow)1298 ZTEST(net_socket_udp, test_23_v6_dgram_overflow)
1299 {
1300 int client_sock;
1301 int server_sock;
1302 struct sockaddr_in6 client_addr;
1303 struct sockaddr_in6 server_addr;
1304
1305 prepare_sock_udp_v6(MY_IPV6_ADDR, ANY_PORT, &client_sock, &client_addr);
1306 prepare_sock_udp_v6(MY_IPV6_ADDR, SERVER_PORT, &server_sock, &server_addr);
1307
1308 test_dgram_overflow(client_sock, server_sock,
1309 (struct sockaddr *)&client_addr, sizeof(client_addr),
1310 (struct sockaddr *)&server_addr, sizeof(server_addr),
1311 BUF_AND_SIZE(test_str_all_tx_bufs));
1312 }
1313
test_dgram_connected(int sock_c,int sock_s1,int sock_s2,struct sockaddr * addr_c,socklen_t addrlen_c,struct sockaddr * addr_s1,socklen_t addrlen_s1,struct sockaddr * addr_s2,socklen_t addrlen_s2)1314 static void test_dgram_connected(int sock_c, int sock_s1, int sock_s2,
1315 struct sockaddr *addr_c, socklen_t addrlen_c,
1316 struct sockaddr *addr_s1, socklen_t addrlen_s1,
1317 struct sockaddr *addr_s2, socklen_t addrlen_s2)
1318 {
1319 uint8_t tx_buf = 0xab;
1320 uint8_t rx_buf;
1321 int rv;
1322
1323 rv = zsock_bind(sock_c, addr_c, addrlen_c);
1324 zassert_equal(rv, 0, "client bind failed");
1325
1326 rv = zsock_bind(sock_s1, addr_s1, addrlen_s1);
1327 zassert_equal(rv, 0, "server bind failed");
1328
1329 rv = zsock_bind(sock_s2, addr_s2, addrlen_s2);
1330 zassert_equal(rv, 0, "server bind failed");
1331
1332 rv = zsock_connect(sock_c, addr_s1, addrlen_s1);
1333 zassert_equal(rv, 0, "connect failed");
1334
1335 /* Verify that a datagram can be received from the connected address */
1336 rv = zsock_sendto(sock_s1, &tx_buf, sizeof(tx_buf), 0, addr_c, addrlen_c);
1337 zassert_equal(rv, sizeof(tx_buf), "send failed %d", errno);
1338
1339 /* Give the packet a chance to go through the net stack */
1340 k_msleep(10);
1341
1342 rx_buf = 0;
1343 rv = zsock_recv(sock_c, &rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1344 zassert_equal(rv, sizeof(rx_buf), "recv failed");
1345 zassert_equal(rx_buf, tx_buf, "wrong data");
1346
1347 /* Verify that a datagram is not received from other address */
1348 rv = zsock_sendto(sock_s2, &tx_buf, sizeof(tx_buf), 0, addr_c, addrlen_c);
1349 zassert_equal(rv, sizeof(tx_buf), "send failed");
1350
1351 /* Give the packet a chance to go through the net stack */
1352 k_msleep(10);
1353
1354 rv = zsock_recv(sock_c, &rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1355 zassert_equal(rv, -1, "recv should've failed");
1356 zassert_equal(errno, EAGAIN, "incorrect errno");
1357
1358 rv = zsock_close(sock_c);
1359 zassert_equal(rv, 0, "close failed");
1360 rv = zsock_close(sock_s1);
1361 zassert_equal(rv, 0, "close failed");
1362 rv = zsock_close(sock_s2);
1363 zassert_equal(rv, 0, "close failed");
1364 }
1365
ZTEST(net_socket_udp,test_24_v4_dgram_connected)1366 ZTEST(net_socket_udp, test_24_v4_dgram_connected)
1367 {
1368 int client_sock;
1369 int server_sock_1;
1370 int server_sock_2;
1371 struct sockaddr_in client_addr;
1372 struct sockaddr_in server_addr_1;
1373 struct sockaddr_in server_addr_2;
1374
1375 prepare_sock_udp_v4(MY_IPV4_ADDR, CLIENT_PORT, &client_sock, &client_addr);
1376 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock_1, &server_addr_1);
1377 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT + 1, &server_sock_2, &server_addr_2);
1378
1379 test_dgram_connected(client_sock, server_sock_1, server_sock_2,
1380 (struct sockaddr *)&client_addr, sizeof(client_addr),
1381 (struct sockaddr *)&server_addr_1, sizeof(server_addr_1),
1382 (struct sockaddr *)&server_addr_2, sizeof(server_addr_2));
1383 }
1384
ZTEST(net_socket_udp,test_25_v6_dgram_connected)1385 ZTEST(net_socket_udp, test_25_v6_dgram_connected)
1386 {
1387 int client_sock;
1388 int server_sock_1;
1389 int server_sock_2;
1390 struct sockaddr_in6 client_addr;
1391 struct sockaddr_in6 server_addr_1;
1392 struct sockaddr_in6 server_addr_2;
1393
1394 prepare_sock_udp_v6(MY_IPV6_ADDR, CLIENT_PORT, &client_sock, &client_addr);
1395 prepare_sock_udp_v6(MY_IPV6_ADDR, SERVER_PORT, &server_sock_1, &server_addr_1);
1396 prepare_sock_udp_v6(MY_IPV6_ADDR, SERVER_PORT + 1, &server_sock_2, &server_addr_2);
1397
1398 test_dgram_connected(client_sock, server_sock_1, server_sock_2,
1399 (struct sockaddr *)&client_addr, sizeof(client_addr),
1400 (struct sockaddr *)&server_addr_1, sizeof(server_addr_1),
1401 (struct sockaddr *)&server_addr_2, sizeof(server_addr_2));
1402 }
1403
ZTEST_USER(net_socket_udp,test_26_recvmsg_invalid)1404 ZTEST_USER(net_socket_udp, test_26_recvmsg_invalid)
1405 {
1406 struct msghdr msg;
1407 struct sockaddr_in6 server_addr;
1408 struct cmsghdr *cmsg;
1409 struct iovec io_vector[1];
1410 union {
1411 struct cmsghdr hdr;
1412 unsigned char buf[CMSG_SPACE(sizeof(int))];
1413 } cmsgbuf;
1414 int ret;
1415
1416 /* Userspace is needed for this test */
1417 Z_TEST_SKIP_IFNDEF(CONFIG_USERSPACE);
1418
1419 io_vector[0].iov_base = TEST_STR_SMALL;
1420 io_vector[0].iov_len = strlen(TEST_STR_SMALL);
1421
1422 ret = zsock_recvmsg(0, NULL, 0);
1423 zassert_true(ret < 0 && errno == EINVAL, "Wrong errno (%d)", errno);
1424
1425 /* Set various pointers to NULL or invalid value which should cause failure */
1426 memset(&msg, 0, sizeof(msg));
1427 msg.msg_controllen = sizeof(cmsgbuf.buf);
1428
1429 ret = zsock_recvmsg(0, &msg, 0);
1430 zassert_true(ret < 0, "recvmsg() succeed");
1431
1432 msg.msg_control = &cmsgbuf.buf;
1433
1434 ret = zsock_recvmsg(0, &msg, 0);
1435 zassert_true(ret < 0 && errno == ENOMEM, "Wrong errno (%d)", errno);
1436
1437 msg.msg_iov = io_vector;
1438 msg.msg_iovlen = 1;
1439 msg.msg_name = (void *)1;
1440 msg.msg_namelen = sizeof(server_addr);
1441
1442 cmsg = CMSG_FIRSTHDR(&msg);
1443 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
1444 cmsg->cmsg_level = SOL_SOCKET;
1445 cmsg->cmsg_type = 1122;
1446 *(int *)CMSG_DATA(cmsg) = 42;
1447
1448 ret = zsock_recvmsg(0, &msg, 0);
1449 zassert_true(ret < 0, "recvmsg() succeed");
1450 }
1451
comm_sendmsg_recvmsg(int client_sock,struct sockaddr * client_addr,socklen_t client_addrlen,const struct msghdr * client_msg,int server_sock,struct sockaddr * server_addr,socklen_t server_addrlen,struct msghdr * msg,void * cmsgbuf,int cmsgbuf_len,bool expect_control_data)1452 static void comm_sendmsg_recvmsg(int client_sock,
1453 struct sockaddr *client_addr,
1454 socklen_t client_addrlen,
1455 const struct msghdr *client_msg,
1456 int server_sock,
1457 struct sockaddr *server_addr,
1458 socklen_t server_addrlen,
1459 struct msghdr *msg,
1460 void *cmsgbuf, int cmsgbuf_len,
1461 bool expect_control_data)
1462 {
1463 #define MAX_BUF_LEN 64
1464 #define SMALL_BUF_LEN (sizeof(TEST_STR_SMALL) - 1 - 2)
1465 char buf[MAX_BUF_LEN];
1466 char buf2[SMALL_BUF_LEN];
1467 struct iovec io_vector[2];
1468 ssize_t sent;
1469 ssize_t recved;
1470 struct sockaddr addr;
1471 socklen_t addrlen = server_addrlen;
1472 int len, i;
1473
1474 zassert_not_null(client_addr, "null client addr");
1475 zassert_not_null(server_addr, "null server addr");
1476
1477 /*
1478 * Test client -> server sending
1479 */
1480
1481 sent = zsock_sendmsg(client_sock, client_msg, 0);
1482 zassert_true(sent > 0, "sendmsg failed, %s (%d)", strerror(errno), -errno);
1483
1484 /* One negative test with invalid msg_iov */
1485 memset(msg, 0, sizeof(*msg));
1486 recved = zsock_recvmsg(server_sock, msg, 0);
1487 zassert_true(recved < 0 && errno == ENOMEM, "Wrong errno (%d)", errno);
1488
1489 for (i = 0, len = 0; i < client_msg->msg_iovlen; i++) {
1490 len += client_msg->msg_iov[i].iov_len;
1491 }
1492
1493 zassert_equal(sent, len, "iovec len (%d) vs sent (%d)", len, sent);
1494
1495 /* Test first with one iovec */
1496 io_vector[0].iov_base = buf;
1497 io_vector[0].iov_len = sizeof(buf);
1498
1499 memset(msg, 0, sizeof(*msg));
1500 memset(cmsgbuf, 0, cmsgbuf_len);
1501 msg->msg_control = cmsgbuf;
1502 msg->msg_controllen = cmsgbuf_len;
1503 msg->msg_iov = io_vector;
1504 msg->msg_iovlen = 1;
1505 msg->msg_name = &addr;
1506 msg->msg_namelen = addrlen;
1507
1508 /* Test recvmsg(MSG_PEEK) */
1509 recved = zsock_recvmsg(server_sock, msg, ZSOCK_MSG_PEEK);
1510 zassert_true(recved > 0, "recvmsg fail, %s (%d)", strerror(errno), -errno);
1511 zassert_equal(recved, strlen(TEST_STR_SMALL),
1512 "unexpected received bytes (%d vs %d)",
1513 recved, strlen(TEST_STR_SMALL));
1514 zassert_equal(sent, recved, "sent(%d)/received(%d) mismatch",
1515 sent, recved);
1516
1517 zassert_mem_equal(buf, TEST_STR_SMALL, strlen(TEST_STR_SMALL),
1518 "wrong data (%s)", rx_buf);
1519 zassert_equal(addrlen, client_addrlen, "unexpected addrlen");
1520
1521 /* Test normal recvmsg() */
1522 clear_buf(rx_buf);
1523 recved = zsock_recvmsg(server_sock, msg, 0);
1524 zassert_true(recved > 0, "recvfrom fail");
1525 zassert_equal(recved, strlen(TEST_STR_SMALL),
1526 "unexpected received bytes");
1527 zassert_mem_equal(buf, TEST_STR_SMALL, strlen(TEST_STR_SMALL),
1528 "wrong data (%s)", rx_buf);
1529 zassert_equal(addrlen, client_addrlen, "unexpected addrlen");
1530
1531 /* Control data should be empty */
1532 if (!expect_control_data) {
1533 zassert_equal(msg->msg_controllen, 0,
1534 "We received control data (%u vs %zu)",
1535 0U, msg->msg_controllen);
1536 }
1537
1538 /* Check the client port */
1539 if (addr.sa_family == AF_INET) {
1540 if (net_sin(client_addr)->sin_port != ANY_PORT) {
1541 zassert_equal(net_sin(client_addr)->sin_port,
1542 net_sin(&addr)->sin_port,
1543 "unexpected client port");
1544 }
1545 }
1546
1547 if (addr.sa_family == AF_INET6) {
1548 if (net_sin6(client_addr)->sin6_port != ANY_PORT) {
1549 zassert_equal(net_sin6(client_addr)->sin6_port,
1550 net_sin6(&addr)->sin6_port,
1551 "unexpected client port");
1552 }
1553 }
1554
1555 /* Then send the message again and verify that we could receive
1556 * the full message in smaller chunks too.
1557 */
1558 sent = zsock_sendmsg(client_sock, client_msg, 0);
1559 zassert_true(sent > 0, "sendmsg failed (%d)", -errno);
1560
1561 for (i = 0, len = 0; i < client_msg->msg_iovlen; i++) {
1562 len += client_msg->msg_iov[i].iov_len;
1563 }
1564
1565 zassert_equal(sent, len, "iovec len (%d) vs sent (%d)", len, sent);
1566
1567 /* and then test with two iovec */
1568 io_vector[0].iov_base = buf2;
1569 io_vector[0].iov_len = sizeof(buf2);
1570 io_vector[1].iov_base = buf;
1571 io_vector[1].iov_len = sizeof(buf);
1572
1573 memset(msg, 0, sizeof(*msg));
1574 memset(cmsgbuf, 0, cmsgbuf_len);
1575 msg->msg_control = cmsgbuf;
1576 msg->msg_controllen = cmsgbuf_len;
1577 msg->msg_iov = io_vector;
1578 msg->msg_iovlen = 2;
1579 msg->msg_name = &addr;
1580 msg->msg_namelen = addrlen;
1581
1582 /* Test recvmsg(MSG_PEEK) */
1583 recved = zsock_recvmsg(server_sock, msg, ZSOCK_MSG_PEEK);
1584 zassert_true(recved >= 0, "recvfrom fail (errno %d)", errno);
1585 zassert_equal(recved, strlen(TEST_STR_SMALL),
1586 "unexpected received bytes (%d vs %d)", recved, strlen(TEST_STR_SMALL));
1587 zassert_equal(sent, recved, "sent(%d)/received(%d) mismatch",
1588 sent, recved);
1589
1590 zassert_mem_equal(msg->msg_iov[0].iov_base, TEST_STR_SMALL, msg->msg_iov[0].iov_len,
1591 "wrong data in %s", "iov[0]");
1592 zassert_mem_equal(msg->msg_iov[1].iov_base, &TEST_STR_SMALL[msg->msg_iov[0].iov_len],
1593 msg->msg_iov[1].iov_len,
1594 "wrong data in %s", "iov[1]");
1595 zassert_equal(addrlen, client_addrlen, "unexpected addrlen");
1596
1597 /* Test normal recvfrom() */
1598 recved = zsock_recvmsg(server_sock, msg, ZSOCK_MSG_PEEK);
1599 zassert_true(recved >= 0, "recvfrom fail (errno %d)", errno);
1600 zassert_equal(recved, strlen(TEST_STR_SMALL),
1601 "unexpected received bytes (%d vs %d)", recved, strlen(TEST_STR_SMALL));
1602 zassert_equal(sent, recved, "sent(%d)/received(%d) mismatch",
1603 sent, recved);
1604
1605 zassert_mem_equal(msg->msg_iov[0].iov_base, TEST_STR_SMALL, msg->msg_iov[0].iov_len,
1606 "wrong data in %s", "iov[0]");
1607 zassert_mem_equal(msg->msg_iov[1].iov_base, &TEST_STR_SMALL[msg->msg_iov[0].iov_len],
1608 msg->msg_iov[1].iov_len,
1609 "wrong data in %s", "iov[1]");
1610 zassert_equal(addrlen, client_addrlen, "unexpected addrlen");
1611
1612 /* Control data should be empty */
1613 if (!expect_control_data) {
1614 zassert_equal(msg->msg_controllen, 0,
1615 "We received control data (%u vs %zu)",
1616 0U, msg->msg_controllen);
1617 }
1618
1619 /* Then check that the trucation flag is set correctly */
1620 sent = zsock_sendmsg(client_sock, client_msg, 0);
1621 zassert_true(sent > 0, "sendmsg failed (%d)", -errno);
1622
1623 for (i = 0, len = 0; i < client_msg->msg_iovlen; i++) {
1624 len += client_msg->msg_iov[i].iov_len;
1625 }
1626
1627 zassert_equal(sent, len, "iovec len (%d) vs sent (%d)", len, sent);
1628
1629 /* Test first with one iovec */
1630 io_vector[0].iov_base = buf2;
1631 io_vector[0].iov_len = sizeof(buf2);
1632
1633 memset(msg, 0, sizeof(*msg));
1634 memset(cmsgbuf, 0, cmsgbuf_len);
1635 msg->msg_control = cmsgbuf;
1636 msg->msg_controllen = cmsgbuf_len;
1637 msg->msg_iov = io_vector;
1638 msg->msg_iovlen = 1;
1639 msg->msg_name = &addr;
1640 msg->msg_namelen = addrlen;
1641
1642 /* Test recvmsg */
1643 recved = zsock_recvmsg(server_sock, msg, 0);
1644 zassert_true(recved > 0, "recvmsg fail, %s (%d)", strerror(errno), errno);
1645 zassert_equal(recved, sizeof(buf2),
1646 "unexpected received bytes (%d vs %d)",
1647 recved, sizeof(buf2));
1648 zassert_true(msg->msg_flags & ZSOCK_MSG_TRUNC, "Message not truncated");
1649
1650 zassert_mem_equal(buf2, TEST_STR_SMALL, sizeof(buf2),
1651 "wrong data (%s)", buf2);
1652 zassert_equal(addrlen, client_addrlen, "unexpected addrlen");
1653
1654 /* Control data should be empty */
1655 if (!expect_control_data) {
1656 zassert_equal(msg->msg_controllen, 0,
1657 "We received control data (%u vs %zu)",
1658 0U, msg->msg_controllen);
1659 }
1660 }
1661
ZTEST_USER(net_socket_udp,test_27_recvmsg_user)1662 ZTEST_USER(net_socket_udp, test_27_recvmsg_user)
1663 {
1664 int rv;
1665 int client_sock;
1666 int server_sock;
1667 struct sockaddr_in client_addr;
1668 struct sockaddr_in server_addr;
1669 struct msghdr msg, server_msg;
1670 struct iovec io_vector[1];
1671
1672 prepare_sock_udp_v4(MY_IPV4_ADDR, ANY_PORT, &client_sock, &client_addr);
1673 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock, &server_addr);
1674
1675 rv = zsock_bind(server_sock,
1676 (struct sockaddr *)&server_addr,
1677 sizeof(server_addr));
1678 zassert_equal(rv, 0, "server bind failed");
1679
1680 rv = zsock_bind(client_sock,
1681 (struct sockaddr *)&client_addr,
1682 sizeof(client_addr));
1683 zassert_equal(rv, 0, "client bind failed");
1684
1685 io_vector[0].iov_base = TEST_STR_SMALL;
1686 io_vector[0].iov_len = strlen(TEST_STR_SMALL);
1687
1688 memset(&msg, 0, sizeof(msg));
1689 msg.msg_iov = io_vector;
1690 msg.msg_iovlen = 1;
1691 msg.msg_name = &server_addr;
1692 msg.msg_namelen = sizeof(server_addr);
1693
1694 comm_sendmsg_recvmsg(client_sock,
1695 (struct sockaddr *)&client_addr,
1696 sizeof(client_addr),
1697 &msg,
1698 server_sock,
1699 (struct sockaddr *)&server_addr,
1700 sizeof(server_addr),
1701 &server_msg, NULL, 0, false);
1702
1703 rv = zsock_close(client_sock);
1704 zassert_equal(rv, 0, "close failed");
1705 rv = zsock_close(server_sock);
1706 zassert_equal(rv, 0, "close failed");
1707 }
1708
run_ancillary_recvmsg_test(int client_sock,struct sockaddr * client_addr,int client_addr_len,int server_sock,struct sockaddr * server_addr,int server_addr_len)1709 static void run_ancillary_recvmsg_test(int client_sock,
1710 struct sockaddr *client_addr,
1711 int client_addr_len,
1712 int server_sock,
1713 struct sockaddr *server_addr,
1714 int server_addr_len)
1715 {
1716 int rv;
1717 int opt;
1718 int ifindex = 0;
1719 socklen_t optlen;
1720 struct sockaddr addr = { 0 };
1721 struct net_if *iface;
1722 struct msghdr msg;
1723 struct msghdr server_msg;
1724 struct iovec io_vector[1];
1725 struct cmsghdr *cmsg, *prevcmsg;
1726 union {
1727 struct cmsghdr hdr;
1728 unsigned char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
1729 } cmsgbuf;
1730 #define SMALL_BUF_LEN (sizeof(TEST_STR_SMALL) - 1 - 2)
1731 char buf[MAX_BUF_LEN];
1732
1733 Z_TEST_SKIP_IFNDEF(CONFIG_NET_CONTEXT_RECV_PKTINFO);
1734
1735 rv = zsock_bind(server_sock, server_addr, server_addr_len);
1736 zassert_equal(rv, 0, "server bind failed");
1737
1738 rv = zsock_bind(client_sock, client_addr, client_addr_len);
1739 zassert_equal(rv, 0, "client bind failed");
1740
1741 io_vector[0].iov_base = TEST_STR_SMALL;
1742 io_vector[0].iov_len = strlen(TEST_STR_SMALL);
1743
1744 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
1745
1746 memset(&msg, 0, sizeof(msg));
1747 msg.msg_iov = io_vector;
1748 msg.msg_iovlen = 1;
1749 msg.msg_name = server_addr;
1750 msg.msg_namelen = server_addr_len;
1751
1752 comm_sendmsg_recvmsg(client_sock,
1753 client_addr,
1754 client_addr_len,
1755 &msg,
1756 server_sock,
1757 server_addr,
1758 server_addr_len,
1759 &server_msg,
1760 &cmsgbuf.buf,
1761 sizeof(cmsgbuf.buf),
1762 true);
1763
1764 for (prevcmsg = NULL, cmsg = CMSG_FIRSTHDR(&server_msg);
1765 cmsg != NULL && prevcmsg != cmsg;
1766 prevcmsg = cmsg, cmsg = CMSG_NXTHDR(&server_msg, cmsg)) {
1767 if (cmsg->cmsg_level == IPPROTO_IP &&
1768 cmsg->cmsg_type == IP_PKTINFO) {
1769 net_sin(&addr)->sin_addr = ((struct in_pktinfo *)CMSG_DATA(cmsg))->ipi_addr;
1770 break;
1771 }
1772 }
1773
1774 /* As we have not set the socket option, the address should not be set */
1775 if (server_addr->sa_family == AF_INET) {
1776 zassert_equal(net_sin(&addr)->sin_addr.s_addr, INADDR_ANY, "Source address set!");
1777 }
1778
1779 if (server_addr->sa_family == AF_INET6) {
1780 zassert_true(net_sin6(&addr)->sin6_addr.s6_addr32[0] == 0 &&
1781 net_sin6(&addr)->sin6_addr.s6_addr32[1] == 0 &&
1782 net_sin6(&addr)->sin6_addr.s6_addr32[2] == 0 &&
1783 net_sin6(&addr)->sin6_addr.s6_addr32[3] == 0,
1784 "Source address set!");
1785 }
1786
1787 opt = 1;
1788 optlen = sizeof(opt);
1789 rv = zsock_setsockopt(server_sock, IPPROTO_IP, IP_PKTINFO, &opt, optlen);
1790 zassert_equal(rv, 0, "setsockopt failed (%d)", -errno);
1791
1792 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
1793
1794 memset(&msg, 0, sizeof(msg));
1795 msg.msg_iov = io_vector;
1796 msg.msg_iovlen = 1;
1797 msg.msg_name = server_addr;
1798 msg.msg_namelen = server_addr_len;
1799
1800 comm_sendmsg_recvmsg(client_sock,
1801 client_addr,
1802 client_addr_len,
1803 &msg,
1804 server_sock,
1805 server_addr,
1806 server_addr_len,
1807 &server_msg,
1808 &cmsgbuf.buf,
1809 sizeof(cmsgbuf.buf),
1810 true);
1811
1812 for (cmsg = CMSG_FIRSTHDR(&server_msg); cmsg != NULL;
1813 cmsg = CMSG_NXTHDR(&server_msg, cmsg)) {
1814 if (cmsg->cmsg_level == IPPROTO_IP &&
1815 cmsg->cmsg_type == IP_PKTINFO) {
1816 net_sin(&addr)->sin_addr =
1817 ((struct in_pktinfo *)CMSG_DATA(cmsg))->ipi_addr;
1818 ifindex = ((struct in_pktinfo *)CMSG_DATA(cmsg))->ipi_ifindex;
1819 break;
1820 }
1821
1822 if (cmsg->cmsg_level == IPPROTO_IPV6 &&
1823 cmsg->cmsg_type == IPV6_RECVPKTINFO) {
1824 net_ipaddr_copy(&net_sin6(&addr)->sin6_addr,
1825 &((struct in6_pktinfo *)CMSG_DATA(cmsg))->ipi6_addr);
1826 ifindex = ((struct in6_pktinfo *)CMSG_DATA(cmsg))->ipi6_ifindex;
1827 break;
1828 }
1829 }
1830
1831 if (server_addr->sa_family == AF_INET) {
1832 zassert_equal(net_sin(&addr)->sin_addr.s_addr,
1833 net_sin(server_addr)->sin_addr.s_addr,
1834 "Source address not set properly!");
1835 }
1836
1837 if (server_addr->sa_family == AF_INET6) {
1838 zassert_mem_equal(&net_sin6(&addr)->sin6_addr,
1839 &net_sin6(server_addr)->sin6_addr,
1840 sizeof(struct in6_addr),
1841 "Source address not set properly!");
1842 }
1843
1844 if (!k_is_user_context()) {
1845 iface = net_if_get_default();
1846 zassert_equal(ifindex, net_if_get_by_iface(iface));
1847 }
1848
1849 /* Make sure that the recvmsg() fails if control area is too small */
1850 rv = zsock_sendto(client_sock, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, 0,
1851 server_addr, server_addr_len);
1852 zassert_equal(rv, STRLEN(TEST_STR_SMALL), "sendto failed (%d)", -errno);
1853
1854 io_vector[0].iov_base = buf;
1855 io_vector[0].iov_len = sizeof(buf);
1856
1857 memset(&msg, 0, sizeof(msg));
1858 msg.msg_control = &cmsgbuf.buf;
1859 msg.msg_controllen = 1; /* making sure the control buf is always too small */
1860 msg.msg_iov = io_vector;
1861 msg.msg_iovlen = 1;
1862
1863 rv = zsock_recvmsg(server_sock, &msg, 0);
1864 zassert_true(rv, "recvmsg succeed (%d)", rv);
1865
1866 zassert_true(msg.msg_flags & ZSOCK_MSG_CTRUNC, "Control message not truncated");
1867
1868 rv = zsock_close(client_sock);
1869 zassert_equal(rv, 0, "close failed");
1870 rv = zsock_close(server_sock);
1871 zassert_equal(rv, 0, "close failed");
1872 }
1873
ZTEST_USER(net_socket_udp,test_28_recvmsg_ancillary_ipv4_pktinfo_data_user)1874 ZTEST_USER(net_socket_udp, test_28_recvmsg_ancillary_ipv4_pktinfo_data_user)
1875 {
1876 struct sockaddr_in client_addr;
1877 struct sockaddr_in server_addr;
1878 int client_sock;
1879 int server_sock;
1880
1881 prepare_sock_udp_v4(MY_IPV4_ADDR, ANY_PORT, &client_sock, &client_addr);
1882 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock, &server_addr);
1883
1884 run_ancillary_recvmsg_test(client_sock,
1885 (struct sockaddr *)&client_addr,
1886 sizeof(client_addr),
1887 server_sock,
1888 (struct sockaddr *)&server_addr,
1889 sizeof(server_addr));
1890 }
1891
ZTEST_USER(net_socket_udp,test_29_recvmsg_ancillary_ipv6_pktinfo_data_user)1892 ZTEST_USER(net_socket_udp, test_29_recvmsg_ancillary_ipv6_pktinfo_data_user)
1893 {
1894 struct sockaddr_in6 client_addr;
1895 struct sockaddr_in6 server_addr;
1896 int client_sock;
1897 int server_sock;
1898
1899 prepare_sock_udp_v6(MY_IPV6_ADDR, ANY_PORT, &client_sock, &client_addr);
1900 prepare_sock_udp_v6(MY_IPV6_ADDR, SERVER_PORT, &server_sock, &server_addr);
1901
1902 run_ancillary_recvmsg_test(client_sock,
1903 (struct sockaddr *)&client_addr,
1904 sizeof(client_addr),
1905 server_sock,
1906 (struct sockaddr *)&server_addr,
1907 sizeof(server_addr));
1908 }
1909
ZTEST(net_socket_udp,test_30_setup_eth_for_ipv4)1910 ZTEST(net_socket_udp, test_30_setup_eth_for_ipv4)
1911 {
1912 struct net_if_addr *ifaddr;
1913
1914 net_if_foreach(iface_cb, ð_iface);
1915 zassert_not_null(eth_iface, "No ethernet interface found");
1916
1917 net_if_down(eth_iface);
1918
1919 ifaddr = net_if_ipv4_addr_add(eth_iface, &my_addr2,
1920 NET_ADDR_MANUAL, 0);
1921 if (!ifaddr) {
1922 DBG("Cannot add IPv4 address %s\n",
1923 net_sprint_ipv4_addr(&my_addr2));
1924 zassert_not_null(ifaddr, "addr2");
1925 }
1926
1927 net_if_up(eth_iface);
1928 }
1929
bind_socket(int sock,struct net_if * iface)1930 static int bind_socket(int sock, struct net_if *iface)
1931 {
1932 struct sockaddr_ll addr;
1933
1934 memset(&addr, 0, sizeof(addr));
1935
1936 addr.sll_ifindex = net_if_get_by_iface(iface);
1937 addr.sll_family = AF_PACKET;
1938
1939 return zsock_bind(sock, (struct sockaddr *)&addr, sizeof(addr));
1940 }
1941
test_check_ttl(int sock_c,int sock_s,int sock_p,struct sockaddr * addr_c,socklen_t addrlen_c,struct sockaddr * addr_s,socklen_t addrlen_s,struct sockaddr * addr_sendto,socklen_t addrlen_sendto,sa_family_t family,uint8_t expected_ttl,uint8_t expected_mcast_ttl)1942 static void test_check_ttl(int sock_c, int sock_s, int sock_p,
1943 struct sockaddr *addr_c, socklen_t addrlen_c,
1944 struct sockaddr *addr_s, socklen_t addrlen_s,
1945 struct sockaddr *addr_sendto, socklen_t addrlen_sendto,
1946 sa_family_t family, uint8_t expected_ttl,
1947 uint8_t expected_mcast_ttl)
1948 {
1949 uint8_t tx_buf = 0xab;
1950 uint8_t rx_buf;
1951 int ret, count = 10, opt;
1952 #define IPV4_HDR_SIZE sizeof(struct net_ipv4_hdr)
1953 #define IPV6_HDR_SIZE sizeof(struct net_ipv6_hdr)
1954 #define UDP_HDR_SIZE sizeof(struct net_udp_hdr)
1955 #define V4_HDR_SIZE (IPV4_HDR_SIZE + UDP_HDR_SIZE)
1956 #define V6_HDR_SIZE (IPV6_HDR_SIZE + UDP_HDR_SIZE)
1957 #define MAX_HDR_SIZE (IPV6_HDR_SIZE + UDP_HDR_SIZE)
1958 uint8_t data_to_receive[sizeof(tx_buf) + MAX_HDR_SIZE];
1959 struct sockaddr_ll src;
1960 socklen_t addrlen;
1961 char ifname[CONFIG_NET_INTERFACE_NAME_LEN];
1962 struct ifreq ifreq = { 0 };
1963 struct timeval timeo_optval = {
1964 .tv_sec = 0,
1965 .tv_usec = 100000,
1966 };
1967 #if defined(CONFIG_NET_STATISTICS)
1968 struct net_stats_ip ipv4_stats_before, ipv4_stats_after;
1969 struct net_stats_ip ipv6_stats_before, ipv6_stats_after;
1970 #endif
1971
1972 Z_TEST_SKIP_IFNDEF(CONFIG_NET_INTERFACE_NAME);
1973
1974 ret = zsock_bind(sock_c, addr_c, addrlen_c);
1975 zassert_equal(ret, 0, "client bind failed");
1976
1977 ret = net_if_get_name(lo0, ifname, sizeof(ifname));
1978 zassert_true(ret > 0, "cannot get interface name (%d)", ret);
1979
1980 strncpy(ifreq.ifr_name, ifname, sizeof(ifreq.ifr_name));
1981 ret = zsock_setsockopt(sock_c, SOL_SOCKET, SO_BINDTODEVICE, &ifreq,
1982 sizeof(ifreq));
1983 zassert_equal(ret, 0, "SO_BINDTODEVICE failed, %d", -errno);
1984
1985 ret = zsock_connect(sock_c, addr_s, addrlen_s);
1986 zassert_equal(ret, 0, "connect failed");
1987
1988 ret = zsock_setsockopt(sock_s, SOL_SOCKET, SO_RCVTIMEO,
1989 &timeo_optval, sizeof(timeo_optval));
1990 zassert_equal(ret, 0, "Cannot set receive timeout (%d)", -errno);
1991
1992 while (count > 0) {
1993 ret = zsock_sendto(sock_c, &tx_buf, sizeof(tx_buf), 0,
1994 addr_sendto, addrlen_sendto);
1995 zassert_equal(ret, sizeof(tx_buf), "send failed (%d)", -errno);
1996
1997 ret = zsock_recv(sock_s, &rx_buf, sizeof(rx_buf), ZSOCK_MSG_DONTWAIT);
1998 if (ret > 0) {
1999 zassert_equal(ret, sizeof(rx_buf), "recv failed (%d)", ret);
2000 zassert_equal(rx_buf, tx_buf, "wrong data");
2001 }
2002
2003 ret = zsock_recvfrom(sock_p, data_to_receive, sizeof(data_to_receive), 0,
2004 (struct sockaddr *)&src, &addrlen);
2005 if (ret > 0) {
2006 int hdr_size = family == AF_INET ?
2007 V4_HDR_SIZE : V6_HDR_SIZE;
2008 zassert_equal(ret, sizeof(tx_buf) + hdr_size,
2009 "Cannot receive all data (%d vs %zd) (%d)",
2010 ret, sizeof(tx_buf), -errno);
2011 zassert_mem_equal(&data_to_receive[hdr_size], &tx_buf,
2012 sizeof(tx_buf),
2013 "Sent and received buffers do not match");
2014
2015 if (family == AF_INET) {
2016 struct net_ipv4_hdr *ipv4 =
2017 (struct net_ipv4_hdr *)&data_to_receive[0];
2018
2019 if (expected_ttl > 0) {
2020 zassert_equal(ipv4->ttl, expected_ttl,
2021 "Invalid ttl (%d vs %d)",
2022 ipv4->ttl, expected_ttl);
2023 } else if (expected_mcast_ttl > 0) {
2024 zassert_equal(ipv4->ttl, expected_mcast_ttl,
2025 "Invalid mcast ttl (%d vs %d)",
2026 ipv4->ttl, expected_mcast_ttl);
2027 }
2028 } else if (family == AF_INET6) {
2029 struct net_ipv6_hdr *ipv6 =
2030 (struct net_ipv6_hdr *)&data_to_receive[0];
2031
2032 if (expected_ttl > 0) {
2033 zassert_equal(ipv6->hop_limit, expected_ttl,
2034 "Invalid hop limit (%d vs %d)",
2035 ipv6->hop_limit, expected_ttl);
2036 } else if (expected_mcast_ttl > 0) {
2037 zassert_equal(ipv6->hop_limit, expected_mcast_ttl,
2038 "Invalid mcast hop limit (%d vs %d)",
2039 ipv6->hop_limit, expected_mcast_ttl);
2040 }
2041 } else {
2042 zassert_true(false, "Invalid address family (%d)",
2043 family);
2044 }
2045
2046 break;
2047 }
2048
2049 count--;
2050 }
2051
2052 zassert_true(count > 0, "timeout while waiting data");
2053
2054 if (family == AF_INET) {
2055 /* Set TTL to 0 and make sure the packet is dropped and not
2056 * received
2057 */
2058 int option;
2059
2060 if (expected_ttl > 0) {
2061 option = IP_TTL;
2062 } else {
2063 option = IP_MULTICAST_TTL;
2064 }
2065
2066 opt = 0;
2067 ret = zsock_setsockopt(sock_c, IPPROTO_IP, option, &opt, sizeof(opt));
2068 zassert_equal(ret, 0, "Cannot set %s TTL (%d)",
2069 option == IP_TTL ? "unicast" : "multicast",
2070 -errno);
2071
2072 #if defined(CONFIG_NET_STATISTICS)
2073 /* Get IPv4 stats and verify they are updated for dropped
2074 * packets.
2075 */
2076 net_mgmt(NET_REQUEST_STATS_GET_IPV4, lo0,
2077 &ipv4_stats_before, sizeof(ipv4_stats_before));
2078 #endif
2079 ret = zsock_sendto(sock_c, &tx_buf, sizeof(tx_buf), 0,
2080 addr_sendto, addrlen_sendto);
2081 zassert_equal(ret, sizeof(tx_buf), "send failed (%d)", -errno);
2082
2083 #if defined(CONFIG_NET_STATISTICS)
2084 net_mgmt(NET_REQUEST_STATS_GET_IPV4, lo0,
2085 &ipv4_stats_after, sizeof(ipv4_stats_after));
2086
2087 zassert_equal(ipv4_stats_before.drop + 1,
2088 ipv4_stats_after.drop,
2089 "Dropped statistics not updated (%d vs %d)",
2090 ipv4_stats_before.drop + 1,
2091 ipv4_stats_after.drop);
2092 #endif
2093 ret = zsock_recv(sock_s, &rx_buf, sizeof(rx_buf), 0);
2094 zassert_true(ret < 0 && errno == EAGAIN, "recv succeed (%d)", -errno);
2095 }
2096
2097 if (family == AF_INET6) {
2098 /* Set hoplimit to 0 and make sure the packet is dropped and not
2099 * received.
2100 */
2101 int option;
2102
2103 if (expected_ttl > 0) {
2104 option = IPV6_UNICAST_HOPS;
2105 } else {
2106 option = IPV6_MULTICAST_HOPS;
2107 }
2108
2109 opt = 0;
2110 ret = zsock_setsockopt(sock_c, IPPROTO_IPV6, option,
2111 &opt, sizeof(opt));
2112 zassert_equal(ret, 0, "Cannot set %s hops (%d)",
2113 option == IPV6_UNICAST_HOPS ? "unicast" : "multicast",
2114 -errno);
2115
2116 #if defined(CONFIG_NET_STATISTICS)
2117 /* Get IPv6 stats and verify they are updated for dropped
2118 * packets.
2119 */
2120 net_mgmt(NET_REQUEST_STATS_GET_IPV6, lo0,
2121 &ipv6_stats_before, sizeof(ipv6_stats_before));
2122 #endif
2123 ret = zsock_sendto(sock_c, &tx_buf, sizeof(tx_buf), 0,
2124 addr_sendto, addrlen_sendto);
2125 zassert_equal(ret, sizeof(tx_buf), "send failed (%d)", -errno);
2126
2127 #if defined(CONFIG_NET_STATISTICS)
2128 net_mgmt(NET_REQUEST_STATS_GET_IPV6, lo0,
2129 &ipv6_stats_after, sizeof(ipv6_stats_after));
2130
2131 zassert_equal(ipv6_stats_before.drop + 1,
2132 ipv6_stats_after.drop,
2133 "Dropped statistics not updated (%d vs %d)",
2134 ipv6_stats_before.drop + 1,
2135 ipv6_stats_after.drop);
2136 #endif
2137 ret = zsock_recv(sock_s, &rx_buf, sizeof(rx_buf), 0);
2138 zassert_true(ret < 0 && errno == EAGAIN, "recv succeed (%d)", -errno);
2139
2140 }
2141
2142 ret = zsock_close(sock_c);
2143 zassert_equal(ret, 0, "close failed");
2144 ret = zsock_close(sock_s);
2145 zassert_equal(ret, 0, "close failed");
2146 ret = zsock_close(sock_p);
2147 zassert_equal(ret, 0, "close failed");
2148 }
2149
ZTEST(net_socket_udp,test_31_v4_ttl)2150 ZTEST(net_socket_udp, test_31_v4_ttl)
2151 {
2152 int ret;
2153 int client_sock;
2154 int server_sock;
2155 int packet_sock;
2156 int ttl, verify;
2157 socklen_t optlen;
2158 struct sockaddr_in client_addr;
2159 struct sockaddr_in server_addr;
2160
2161 Z_TEST_SKIP_IFNDEF(CONFIG_NET_SOCKETS_PACKET);
2162
2163 prepare_sock_udp_v4(MY_IPV4_ADDR, CLIENT_PORT, &client_sock, &client_addr);
2164 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock, &server_addr);
2165
2166 packet_sock = zsock_socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
2167 zassert_true(packet_sock >= 0, "Cannot create packet socket (%d)", -errno);
2168
2169 ret = bind_socket(packet_sock, lo0);
2170 zassert_equal(ret, 0, "packet socket bind failed");
2171
2172 zassert_not_null(lo0->config.ip.ipv4,
2173 "Interface %d (%p) no IPv4 configured",
2174 net_if_get_by_iface(lo0), lo0);
2175
2176 ttl = 16;
2177 net_if_ipv4_set_ttl(lo0, ttl);
2178 verify = net_if_ipv4_get_ttl(lo0);
2179 zassert_equal(verify, ttl, "Different TTLs (%d vs %d)", ttl, verify);
2180
2181 ttl = 128;
2182 ret = zsock_setsockopt(client_sock, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
2183 zassert_equal(ret, 0, "Cannot set unicast TTL (%d)", -errno);
2184
2185 optlen = sizeof(verify);
2186 ret = zsock_getsockopt(client_sock, IPPROTO_IP, IP_TTL, &verify, &optlen);
2187 zassert_equal(ret, 0, "Cannot get unicast TTL (%d)", -errno);
2188 zassert_equal(verify, ttl, "Different unicast TTL (%d vs %d)",
2189 ttl, verify);
2190
2191 test_check_ttl(client_sock, server_sock, packet_sock,
2192 (struct sockaddr *)&client_addr, sizeof(client_addr),
2193 (struct sockaddr *)&server_addr, sizeof(server_addr),
2194 (struct sockaddr *)&server_addr, sizeof(server_addr),
2195 AF_INET, ttl, 0);
2196 }
2197
ZTEST(net_socket_udp,test_32_v4_mcast_ttl)2198 ZTEST(net_socket_udp, test_32_v4_mcast_ttl)
2199 {
2200 int ret;
2201 int client_sock;
2202 int server_sock;
2203 int packet_sock;
2204 int mcast_ttl, verify;
2205 socklen_t optlen;
2206 struct sockaddr_in client_addr;
2207 struct sockaddr_in server_addr;
2208 struct sockaddr_in sendto_addr;
2209
2210 Z_TEST_SKIP_IFNDEF(CONFIG_NET_SOCKETS_PACKET);
2211
2212 prepare_sock_udp_v4(MY_IPV4_ADDR, CLIENT_PORT, &client_sock, &client_addr);
2213 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock, &server_addr);
2214
2215 packet_sock = zsock_socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
2216 zassert_true(packet_sock >= 0, "Cannot create packet socket (%d)", -errno);
2217
2218 ret = bind_socket(packet_sock, lo0);
2219 zassert_equal(ret, 0, "packet socket bind failed");
2220
2221 zassert_not_null(lo0->config.ip.ipv4,
2222 "Interface %d (%p) no IPv4 configured",
2223 net_if_get_by_iface(lo0), lo0);
2224
2225 mcast_ttl = 8;
2226 ret = zsock_setsockopt(client_sock, IPPROTO_IP, IP_MULTICAST_TTL, &mcast_ttl,
2227 sizeof(mcast_ttl));
2228 zassert_equal(ret, 0, "Cannot set multicast ttl (%d)", -errno);
2229
2230 optlen = sizeof(verify);
2231 ret = zsock_getsockopt(client_sock, IPPROTO_IP, IP_MULTICAST_TTL, &verify,
2232 &optlen);
2233 zassert_equal(ret, 0, "Cannot get multicast ttl (%d)", -errno);
2234 zassert_equal(verify, mcast_ttl, "Different multicast TTLs (%d vs %d)",
2235 mcast_ttl, verify);
2236
2237 ret = net_addr_pton(AF_INET, MY_MCAST_IPV4_ADDR, &sendto_addr.sin_addr);
2238 zassert_equal(ret, 0, "Cannot get IPv4 address (%d)", ret);
2239
2240 test_check_ttl(client_sock, server_sock, packet_sock,
2241 (struct sockaddr *)&client_addr, sizeof(client_addr),
2242 (struct sockaddr *)&server_addr, sizeof(server_addr),
2243 (struct sockaddr *)&sendto_addr, sizeof(sendto_addr),
2244 AF_INET, 0, mcast_ttl);
2245 }
2246
ZTEST(net_socket_udp,test_33_v6_mcast_hops)2247 ZTEST(net_socket_udp, test_33_v6_mcast_hops)
2248 {
2249 int ret;
2250 int client_sock;
2251 int server_sock;
2252 int packet_sock;
2253 int mcast_hops, if_mcast_hops;
2254 int verify, opt;
2255 socklen_t optlen;
2256 struct sockaddr_in6 client_addr;
2257 struct sockaddr_in6 server_addr;
2258 struct sockaddr_in6 sendto_addr;
2259
2260 Z_TEST_SKIP_IFNDEF(CONFIG_NET_SOCKETS_PACKET);
2261
2262 prepare_sock_udp_v6(MY_IPV6_ADDR, CLIENT_PORT, &client_sock, &client_addr);
2263 prepare_sock_udp_v6(MY_IPV6_ADDR, SERVER_PORT, &server_sock, &server_addr);
2264
2265 packet_sock = zsock_socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
2266 zassert_true(packet_sock >= 0, "Cannot create packet socket (%d)", -errno);
2267
2268 ret = bind_socket(packet_sock, lo0);
2269 zassert_equal(ret, 0, "packet socket bind failed");
2270
2271 zassert_not_null(lo0->config.ip.ipv6,
2272 "Interface %d (%p) no IPv6 configured",
2273 net_if_get_by_iface(lo0), lo0);
2274
2275 /* First make sure setting hop limit to -1 works as expected (route default
2276 * value should be used).
2277 */
2278 if_mcast_hops = net_if_ipv6_get_mcast_hop_limit(lo0);
2279
2280 opt = -1;
2281 ret = zsock_setsockopt(client_sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &opt,
2282 sizeof(opt));
2283 zassert_equal(ret, 0, "Cannot set multicast hop limit (%d)", -errno);
2284
2285 optlen = sizeof(verify);
2286 ret = zsock_getsockopt(client_sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &verify,
2287 &optlen);
2288 zassert_equal(ret, 0, "Cannot get multicast hop limit (%d)", -errno);
2289 zassert_equal(verify, if_mcast_hops, "Different multicast hop limit (%d vs %d)",
2290 if_mcast_hops, verify);
2291
2292 /* Then test the normal case where we set the value */
2293 mcast_hops = 8;
2294 ret = zsock_setsockopt(client_sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcast_hops,
2295 sizeof(mcast_hops));
2296 zassert_equal(ret, 0, "Cannot set multicast hop limit (%d)", -errno);
2297
2298 optlen = sizeof(verify);
2299 ret = zsock_getsockopt(client_sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &verify,
2300 &optlen);
2301 zassert_equal(ret, 0, "Cannot get multicast hop limit (%d)", -errno);
2302 zassert_equal(verify, mcast_hops, "Different multicast hop limit (%d vs %d)",
2303 mcast_hops, verify);
2304
2305 ret = net_addr_pton(AF_INET6, MY_MCAST_IPV6_ADDR, &sendto_addr.sin6_addr);
2306 zassert_equal(ret, 0, "Cannot get IPv6 address (%d)", ret);
2307
2308 test_check_ttl(client_sock, server_sock, packet_sock,
2309 (struct sockaddr *)&client_addr, sizeof(client_addr),
2310 (struct sockaddr *)&server_addr, sizeof(server_addr),
2311 (struct sockaddr *)&sendto_addr, sizeof(sendto_addr),
2312 AF_INET6, 0, mcast_hops);
2313 }
2314
ZTEST(net_socket_udp,test_34_v6_hops)2315 ZTEST(net_socket_udp, test_34_v6_hops)
2316 {
2317 int ret;
2318 int client_sock;
2319 int server_sock;
2320 int packet_sock;
2321 int hops, verify;
2322 socklen_t optlen;
2323 struct sockaddr_in6 client_addr;
2324 struct sockaddr_in6 server_addr;
2325
2326 Z_TEST_SKIP_IFNDEF(CONFIG_NET_SOCKETS_PACKET);
2327
2328 prepare_sock_udp_v6(MY_IPV6_ADDR, CLIENT_PORT, &client_sock, &client_addr);
2329 prepare_sock_udp_v6(MY_IPV6_ADDR, SERVER_PORT, &server_sock, &server_addr);
2330
2331 packet_sock = zsock_socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
2332 zassert_true(packet_sock >= 0, "Cannot create packet socket (%d)", -errno);
2333
2334 ret = bind_socket(packet_sock, lo0);
2335 zassert_equal(ret, 0, "packet socket bind failed");
2336
2337 zassert_not_null(lo0->config.ip.ipv6,
2338 "Interface %d (%p) no IPv6 configured",
2339 net_if_get_by_iface(lo0), lo0);
2340
2341 hops = 16;
2342 net_if_ipv6_set_hop_limit(lo0, hops);
2343 verify = net_if_ipv6_get_hop_limit(lo0);
2344 zassert_equal(verify, hops, "Different hop limit (%d vs %d)", hops, verify);
2345
2346 hops = 8;
2347 ret = zsock_setsockopt(client_sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &hops,
2348 sizeof(hops));
2349 zassert_equal(ret, 0, "Cannot set unicast hops (%d)", -errno);
2350
2351 optlen = sizeof(verify);
2352 ret = zsock_getsockopt(client_sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &verify,
2353 &optlen);
2354 zassert_equal(ret, 0, "Cannot get unicast hops (%d)", -errno);
2355 zassert_equal(verify, hops, "Different unicast hops (%d vs %d)",
2356 hops, verify);
2357
2358 test_check_ttl(client_sock, server_sock, packet_sock,
2359 (struct sockaddr *)&client_addr, sizeof(client_addr),
2360 (struct sockaddr *)&server_addr, sizeof(server_addr),
2361 (struct sockaddr *)&server_addr, sizeof(server_addr),
2362 AF_INET6, hops, 0);
2363 }
2364
ZTEST_USER(net_socket_udp,test_35_recvmsg_msg_controllen_update)2365 ZTEST_USER(net_socket_udp, test_35_recvmsg_msg_controllen_update)
2366 {
2367 int rv;
2368 int client_sock;
2369 int server_sock;
2370 struct sockaddr_in client_addr;
2371 struct sockaddr_in server_addr;
2372 struct msghdr msg, server_msg;
2373 struct iovec io_vector[1];
2374 union {
2375 struct cmsghdr hdr;
2376 unsigned char buf[CMSG_SPACE(sizeof(struct in6_pktinfo))];
2377 } cmsgbuf;
2378
2379 prepare_sock_udp_v4(MY_IPV4_ADDR, ANY_PORT, &client_sock, &client_addr);
2380 prepare_sock_udp_v4(MY_IPV4_ADDR, SERVER_PORT, &server_sock, &server_addr);
2381
2382 rv = zsock_bind(server_sock,
2383 (struct sockaddr *)&server_addr,
2384 sizeof(server_addr));
2385 zassert_equal(rv, 0, "server bind failed");
2386
2387 rv = zsock_bind(client_sock,
2388 (struct sockaddr *)&client_addr,
2389 sizeof(client_addr));
2390 zassert_equal(rv, 0, "client bind failed");
2391
2392 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
2393
2394 io_vector[0].iov_base = TEST_STR_SMALL;
2395 io_vector[0].iov_len = strlen(TEST_STR_SMALL);
2396
2397 memset(&msg, 0, sizeof(msg));
2398 msg.msg_iov = io_vector;
2399 msg.msg_iovlen = 1;
2400 msg.msg_name = &server_addr;
2401 msg.msg_namelen = sizeof(server_addr);
2402
2403 comm_sendmsg_recvmsg(client_sock,
2404 (struct sockaddr *)&client_addr,
2405 sizeof(client_addr),
2406 &msg,
2407 server_sock,
2408 (struct sockaddr *)&server_addr,
2409 sizeof(server_addr),
2410 &server_msg,
2411 &cmsgbuf.buf,
2412 sizeof(cmsgbuf.buf),
2413 false);
2414
2415 rv = zsock_close(client_sock);
2416 zassert_equal(rv, 0, "close failed");
2417 rv = zsock_close(server_sock);
2418 zassert_equal(rv, 0, "close failed");
2419 }
2420
ZTEST(net_socket_udp,test_36_v6_address_removal)2421 ZTEST(net_socket_udp, test_36_v6_address_removal)
2422 {
2423 int ret;
2424 bool status;
2425 int client_sock;
2426 struct sockaddr_in6 client_addr;
2427 struct net_if_addr *ifaddr;
2428 struct net_if *iface;
2429
2430 if (!IS_ENABLED(CONFIG_NET_IPV6_PE)) {
2431 return;
2432 }
2433
2434 ifaddr = net_if_ipv6_addr_lookup(&my_addr1, &iface);
2435 zassert_equal(ifaddr->atomic_ref, 1, "Ref count is wrong (%ld vs %ld)",
2436 ifaddr->atomic_ref, 1);
2437
2438 prepare_sock_udp_v6(MY_IPV6_ADDR_ETH, CLIENT_PORT, &client_sock, &client_addr);
2439
2440 ret = zsock_bind(client_sock,
2441 (struct sockaddr *)&client_addr,
2442 sizeof(client_addr));
2443 zassert_equal(ret, 0, "client bind failed");
2444
2445 status = net_if_ipv6_addr_rm(eth_iface, &my_addr1);
2446 zassert_false(status, "Address could be removed");
2447
2448 ifaddr = net_if_ipv6_addr_lookup(&my_addr1, &iface);
2449 zassert_not_null(ifaddr, "Address %s not found",
2450 net_sprint_ipv6_addr(&my_addr1));
2451
2452 ret = zsock_close(client_sock);
2453 zassert_equal(ret, 0, "close failed");
2454
2455 ifaddr = net_if_ipv6_addr_lookup(&my_addr1, &iface);
2456 zassert_equal(iface, eth_iface, "Invalid interface %p vs %p",
2457 iface, eth_iface);
2458 zassert_is_null(ifaddr, "Address %s found",
2459 net_sprint_ipv6_addr(&my_addr1));
2460 }
2461
check_ipv6_address_preferences(struct net_if * iface,uint16_t preference,const struct in6_addr * addr,const struct in6_addr * dest)2462 static void check_ipv6_address_preferences(struct net_if *iface,
2463 uint16_t preference,
2464 const struct in6_addr *addr,
2465 const struct in6_addr *dest)
2466 {
2467 const struct in6_addr *selected;
2468 size_t optlen;
2469 int optval;
2470 int sock;
2471 int ret;
2472
2473 sock = zsock_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
2474 zassert_true(sock >= 0, "Cannot create socket (%d)", -errno);
2475
2476 optval = preference;
2477 ret = zsock_setsockopt(sock, IPPROTO_IPV6, IPV6_ADDR_PREFERENCES,
2478 &optval, sizeof(optval));
2479 zassert_equal(ret, 0, "setsockopt failed (%d)", errno);
2480
2481 optval = 0; optlen = 0U;
2482 ret = zsock_getsockopt(sock, IPPROTO_IPV6, IPV6_ADDR_PREFERENCES,
2483 &optval, &optlen);
2484 zassert_equal(ret, 0, "setsockopt failed (%d)", errno);
2485 zassert_equal(optlen, sizeof(optval), "invalid optlen %d vs %d",
2486 optlen, sizeof(optval));
2487 zassert_equal(optval, preference,
2488 "getsockopt address preferences");
2489
2490 selected = net_if_ipv6_select_src_addr_hint(iface,
2491 dest,
2492 preference);
2493 ret = net_ipv6_addr_cmp(addr, selected);
2494 zassert_true(ret, "Wrong address %s selected, expected %s",
2495 net_sprint_ipv6_addr(selected),
2496 net_sprint_ipv6_addr(addr));
2497
2498 ret = zsock_close(sock);
2499 zassert_equal(sock, 0, "Cannot close socket (%d)", -errno);
2500 }
2501
ZTEST(net_socket_udp,test_37_ipv6_src_addr_select)2502 ZTEST(net_socket_udp, test_37_ipv6_src_addr_select)
2503 {
2504 struct net_if_addr *ifaddr;
2505 const struct in6_addr dest = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
2506 0, 0, 0, 0, 0, 0, 0, 0x2 } } };
2507
2508 net_if_foreach(iface_cb, ð_iface);
2509 zassert_not_null(eth_iface, "No ethernet interface found");
2510
2511 ifaddr = net_if_ipv6_addr_add(eth_iface, &my_addr1,
2512 NET_ADDR_AUTOCONF, 0);
2513 if (!ifaddr) {
2514 DBG("Cannot add IPv6 address %s\n",
2515 net_sprint_ipv6_addr(&my_addr1));
2516 zassert_not_null(ifaddr, "addr1");
2517 }
2518
2519 ifaddr->is_temporary = false;
2520
2521 ifaddr = net_if_ipv6_addr_add(eth_iface, &my_addr3,
2522 NET_ADDR_AUTOCONF, 0);
2523 if (!ifaddr) {
2524 DBG("Cannot add IPv6 address %s\n",
2525 net_sprint_ipv6_addr(&my_addr3));
2526 zassert_not_null(ifaddr, "addr1");
2527 }
2528
2529 ifaddr->is_temporary = true;
2530
2531 net_if_up(eth_iface);
2532
2533 check_ipv6_address_preferences(NULL, IPV6_PREFER_SRC_PUBLIC,
2534 &my_addr1, &dest);
2535 check_ipv6_address_preferences(NULL, IPV6_PREFER_SRC_TMP,
2536 &my_addr3, &dest);
2537 }
2538
ZTEST(net_socket_udp,test_38_ipv6_multicast_ifindex)2539 ZTEST(net_socket_udp, test_38_ipv6_multicast_ifindex)
2540 {
2541 struct sockaddr_in6 saddr6 = {
2542 .sin6_family = AF_INET6,
2543 .sin6_port = htons(SERVER_PORT),
2544 .sin6_addr = my_mcast_addr1,
2545 };
2546 struct net_if_mcast_addr *ifmaddr;
2547 struct net_if_addr *ifaddr;
2548 int server_sock;
2549 size_t addrlen;
2550 size_t optlen;
2551 int ifindex;
2552 int optval;
2553 int sock;
2554 int ret;
2555 int err;
2556
2557 net_if_foreach(iface_cb, ð_iface);
2558 zassert_not_null(eth_iface, "No ethernet interface found");
2559
2560 ifmaddr = net_if_ipv6_maddr_add(eth_iface, &my_mcast_addr1);
2561 if (!ifmaddr) {
2562 DBG("Cannot add IPv6 multicast address %s\n",
2563 net_sprint_ipv6_addr(&my_mcast_addr1));
2564 zassert_not_null(ifmaddr, "mcast_addr1");
2565 }
2566
2567 ifaddr = net_if_ipv6_addr_add(eth_iface, &my_addr3,
2568 NET_ADDR_AUTOCONF, 0);
2569 if (!ifaddr) {
2570 DBG("Cannot add IPv6 address %s\n",
2571 net_sprint_ipv6_addr(&my_addr3));
2572 zassert_not_null(ifaddr, "addr1");
2573 }
2574
2575 net_if_up(eth_iface);
2576
2577 /* Check that we get the default interface */
2578 sock = zsock_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
2579 zassert_true(sock >= 0, "Cannot create socket (%d)", -errno);
2580
2581 optval = 0; optlen = 0U;
2582 ret = zsock_getsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
2583 &optval, &optlen);
2584 zexpect_equal(ret, 0, "setsockopt failed (%d)", errno);
2585 zexpect_equal(optlen, sizeof(optval), "invalid optlen %d vs %d",
2586 optlen, sizeof(optval));
2587 ifindex = net_if_get_by_iface(net_if_get_default());
2588 zexpect_equal(optval, ifindex,
2589 "getsockopt multicast ifindex (expected %d got %d)",
2590 ifindex, optval);
2591
2592 ret = zsock_close(sock);
2593 zassert_equal(sock, 0, "Cannot close socket (%d)", -errno);
2594
2595 /* Check failure for IPv4 socket */
2596 sock = zsock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2597 zassert_true(sock >= 0, "Cannot create socket (%d)", -errno);
2598
2599 optval = 0; optlen = 0U;
2600 ret = zsock_getsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
2601 &optval, &optlen);
2602 err = -errno;
2603 zexpect_equal(ret, -1, "setsockopt failed (%d)", errno);
2604 zexpect_equal(err, -EAFNOSUPPORT, "setsockopt failed (%d)", errno);
2605 zexpect_equal(optlen, 0U, "setsockopt optlen (%d)", optlen);
2606
2607 ret = zsock_close(sock);
2608 zassert_equal(sock, 0, "Cannot close socket (%d)", -errno);
2609
2610 /* Check that we can set the interface */
2611 sock = zsock_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
2612 zassert_true(sock >= 0, "Cannot create socket (%d)", -errno);
2613
2614 /* Clear any existing interface value by setting it to 0 */
2615 optval = 0; optlen = sizeof(int);
2616 ret = zsock_setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
2617 &optval, optlen);
2618 err = -errno;
2619 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
2620 zexpect_equal(optlen, sizeof(optval), "invalid optlen %d vs %d",
2621 optlen, sizeof(optval));
2622
2623 /* Set the output multicast packet interface to the default interface */
2624 optval = net_if_get_by_iface(net_if_get_default()); optlen = sizeof(int);
2625 ret = zsock_setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
2626 &optval, optlen);
2627 zexpect_equal(ret, 0, "setsockopt failed (%d)", errno);
2628 zexpect_equal(optlen, sizeof(optval), "invalid optlen %d vs %d",
2629 optlen, sizeof(optval));
2630
2631 optval = 0; optlen = 0U;
2632 ret = zsock_getsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_IF,
2633 &optval, &optlen);
2634 err = -errno;
2635 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
2636 zexpect_equal(optlen, sizeof(int), "setsockopt optlen (%d)", optlen);
2637 zexpect_equal(optval, net_if_get_by_iface(net_if_get_default()),
2638 "getsockopt multicast ifindex (expected %d got %d)",
2639 net_if_get_by_iface(net_if_get_default()), optval);
2640
2641 server_sock = prepare_listen_sock_udp_v6(&saddr6);
2642 zassert_not_equal(server_sock, -1, "Cannot create server socket (%d)", -errno);
2643
2644 test_started = true;
2645 loopback_enable_address_swap(false);
2646
2647 ret = zsock_sendto(sock, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, 0,
2648 (struct sockaddr *)&saddr6, sizeof(saddr6));
2649 zexpect_equal(ret, STRLEN(TEST_STR_SMALL),
2650 "invalid send len (was %d expected %d) (%d)",
2651 ret, STRLEN(TEST_STR_SMALL), -errno);
2652
2653 /* Test that the sent data is received from default interface and
2654 * not the Ethernet one.
2655 */
2656 addrlen = sizeof(saddr6);
2657 ret = zsock_recvfrom(server_sock, rx_buf, sizeof(rx_buf),
2658 0, (struct sockaddr *)&saddr6, &addrlen);
2659 zexpect_true(ret >= 0, "recvfrom fail");
2660 zexpect_equal(ret, strlen(TEST_STR_SMALL),
2661 "unexpected received bytes");
2662 zexpect_mem_equal(rx_buf, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1,
2663 "wrong data");
2664
2665 ret = zsock_close(sock);
2666 zassert_equal(ret, 0, "Cannot close socket (%d)", -errno);
2667
2668 ret = zsock_close(server_sock);
2669 zassert_equal(ret, 0, "Cannot close socket (%d)", -errno);
2670
2671 test_started = false;
2672 loopback_enable_address_swap(true);
2673 }
2674
ZTEST(net_socket_udp,test_39_ipv4_multicast_ifindex)2675 ZTEST(net_socket_udp, test_39_ipv4_multicast_ifindex)
2676 {
2677 struct sockaddr_in saddr4 = {
2678 .sin_family = AF_INET,
2679 .sin_port = htons(SERVER_PORT),
2680 .sin_addr = my_mcast_addr2,
2681 };
2682 struct sockaddr_in dst_addr = {
2683 .sin_family = AF_INET,
2684 .sin_port = htons(SERVER_PORT),
2685 .sin_addr = my_mcast_addr2,
2686 };
2687 struct net_if_mcast_addr *ifmaddr;
2688 struct net_if_addr *ifaddr;
2689 struct in_addr addr = { 0 };
2690 struct ip_mreqn mreqn;
2691 struct ip_mreq mreq;
2692 struct net_if *iface;
2693 int server_sock;
2694 size_t addrlen;
2695 size_t optlen;
2696 int ifindex;
2697 int sock;
2698 int ret;
2699 int err;
2700
2701 net_if_foreach(iface_cb, ð_iface);
2702 zassert_not_null(eth_iface, "No ethernet interface found");
2703
2704 ifmaddr = net_if_ipv4_maddr_add(eth_iface, &my_mcast_addr2);
2705 if (!ifmaddr) {
2706 DBG("Cannot add IPv4 multicast address %s\n",
2707 net_sprint_ipv4_addr(&my_mcast_addr2));
2708 zassert_not_null(ifmaddr, "mcast_addr2");
2709 }
2710
2711 ifaddr = net_if_ipv4_addr_add(eth_iface, &my_addr2,
2712 NET_ADDR_MANUAL, 0);
2713 if (!ifaddr) {
2714 DBG("Cannot add IPv4 address %s\n",
2715 net_sprint_ipv4_addr(&my_addr2));
2716 zassert_not_null(ifaddr, "addr2");
2717 }
2718
2719 net_if_up(eth_iface);
2720
2721 /* Check that we get the default interface */
2722 sock = zsock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2723 zassert_true(sock >= 0, "Cannot create socket (%d)", -errno);
2724
2725 optlen = sizeof(addr);
2726 ret = zsock_getsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
2727 &addr, &optlen);
2728 zexpect_equal(ret, 0, "setsockopt failed (%d)", errno);
2729 zexpect_equal(optlen, sizeof(addr), "invalid optlen %d vs %d",
2730 optlen, sizeof(addr));
2731 ifindex = net_if_get_by_iface(net_if_get_default());
2732 ret = net_if_ipv4_addr_lookup_by_index(&addr);
2733 zexpect_equal(ret, ifindex,
2734 "getsockopt multicast ifindex (expected %d got %d)",
2735 ifindex, ret);
2736
2737 ret = zsock_close(sock);
2738 zassert_equal(sock, 0, "Cannot close socket (%d)", -errno);
2739
2740 /* Check failure for IPv6 socket */
2741 sock = zsock_socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
2742 zassert_true(sock >= 0, "Cannot create socket (%d)", -errno);
2743
2744 optlen = 0U;
2745 ret = zsock_getsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
2746 &addr, &optlen);
2747 err = -errno;
2748 zexpect_equal(ret, -1, "setsockopt failed (%d)", errno);
2749 zexpect_equal(err, -EAFNOSUPPORT, "setsockopt failed (%d)", errno);
2750 zexpect_equal(optlen, 0U, "setsockopt optlen (%d)", optlen);
2751
2752 ret = zsock_close(sock);
2753 zassert_equal(sock, 0, "Cannot close socket (%d)", -errno);
2754
2755 /* Check that we can set the interface */
2756 sock = zsock_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2757 zassert_true(sock >= 0, "Cannot create socket (%d)", -errno);
2758
2759 /* Clear any existing interface value by setting it to 0 */
2760 optlen = sizeof(mreqn);
2761 mreqn.imr_ifindex = 0;
2762 mreqn.imr_address.s_addr = 0;
2763
2764 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
2765 &mreqn, optlen);
2766 err = -errno;
2767 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
2768
2769 /* Verify that we get the empty value */
2770 optlen = sizeof(addr);
2771 ret = zsock_getsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
2772 &addr, &optlen);
2773 err = -errno;
2774 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
2775 zexpect_equal(optlen, sizeof(addr), "setsockopt optlen (%d)", optlen);
2776
2777 /* Set the output multicast packet interface to the default interface */
2778 optlen = sizeof(mreqn);
2779 mreqn.imr_ifindex = net_if_get_by_iface(net_if_get_default());
2780 mreqn.imr_address.s_addr = 0;
2781
2782 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
2783 &mreqn, optlen);
2784 err = -errno;
2785 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
2786
2787 /* Verify that we get the default interface */
2788 optlen = sizeof(addr);
2789 memset(&addr, 0, sizeof(addr));
2790 ret = zsock_getsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
2791 &addr, &optlen);
2792 err = -errno;
2793 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
2794 zexpect_equal(optlen, sizeof(addr), "setsockopt optlen (%d)", optlen);
2795
2796 ifaddr = net_if_ipv4_addr_lookup(&addr, &iface);
2797 zexpect_not_null(ifaddr, "Address %s not found",
2798 net_sprint_ipv4_addr(&addr));
2799 zexpect_equal(net_if_get_by_iface(iface),
2800 net_if_get_by_iface(net_if_get_default()),
2801 "Invalid interface %d vs %d",
2802 net_if_get_by_iface(iface),
2803 net_if_get_by_iface(net_if_get_default()));
2804
2805 /* Now send a packet and verify that it is sent via the default
2806 * interface instead of the Ethernet interface.
2807 */
2808 server_sock = prepare_listen_sock_udp_v4(&saddr4);
2809 zassert_not_equal(server_sock, -1, "Cannot create server socket (%d)", -errno);
2810
2811 test_started = true;
2812 loopback_enable_address_swap(false);
2813
2814 ret = zsock_sendto(sock, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, 0,
2815 (struct sockaddr *)&dst_addr, sizeof(dst_addr));
2816 zexpect_equal(ret, STRLEN(TEST_STR_SMALL),
2817 "invalid send len (was %d expected %d) (%d)",
2818 ret, STRLEN(TEST_STR_SMALL), -errno);
2819
2820 /* Test that the sent data is received from Ethernet interface. */
2821 addrlen = sizeof(saddr4);
2822 ret = zsock_recvfrom(server_sock, rx_buf, sizeof(rx_buf),
2823 0, (struct sockaddr *)&saddr4, &addrlen);
2824 zexpect_true(ret >= 0, "recvfrom fail");
2825 zexpect_equal(ret, strlen(TEST_STR_SMALL),
2826 "unexpected received bytes");
2827 zexpect_mem_equal(rx_buf, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1,
2828 "wrong data");
2829
2830 /* Clear the old interface value by setting it to 0 */
2831 optlen = sizeof(mreqn);
2832 mreqn.imr_ifindex = 0;
2833 mreqn.imr_address.s_addr = 0;
2834
2835 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
2836 &mreqn, optlen);
2837 err = -errno;
2838 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
2839
2840 /* Then do it the other way around, set the address but leave the
2841 * interface number unassigned.
2842 */
2843 optlen = sizeof(mreqn);
2844 mreqn.imr_ifindex = 0;
2845
2846 /* Get the address of default interface and set it as a target
2847 * interface.
2848 */
2849 ifaddr = net_if_ipv4_addr_get_first_by_index(net_if_get_by_iface(net_if_get_default()));
2850 zexpect_not_null(ifaddr, "No address found for interface %d",
2851 net_if_get_by_iface(net_if_get_default()));
2852 mreqn.imr_address.s_addr = ifaddr->address.in_addr.s_addr;
2853
2854 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
2855 &mreqn, optlen);
2856 zexpect_equal(ret, 0, "setsockopt failed (%d)", errno);
2857
2858 /* Verify that we get the default interface address */
2859 optlen = sizeof(struct in_addr);
2860 ret = zsock_getsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
2861 &addr, &optlen);
2862 err = -errno;
2863 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
2864 zexpect_equal(optlen, sizeof(struct in_addr), "setsockopt optlen (%d)", optlen);
2865 ret = net_if_ipv4_addr_lookup_by_index(&addr);
2866 zexpect_equal(ret, net_if_get_by_iface(net_if_get_default()),
2867 "getsockopt multicast ifindex (expected %d got %d)",
2868 net_if_get_by_iface(net_if_get_default()), ret);
2869 zexpect_equal(ifaddr->address.in_addr.s_addr,
2870 addr.s_addr,
2871 "getsockopt iface address mismatch (expected %s got %s)",
2872 net_sprint_ipv4_addr(&ifaddr->address.in_addr),
2873 net_sprint_ipv4_addr(&addr));
2874
2875 ret = zsock_sendto(sock, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, 0,
2876 (struct sockaddr *)&dst_addr, sizeof(dst_addr));
2877 zexpect_equal(ret, STRLEN(TEST_STR_SMALL),
2878 "invalid send len (was %d expected %d) (%d)",
2879 ret, STRLEN(TEST_STR_SMALL), -errno);
2880
2881 /* Test that the sent data is received from default interface. */
2882 addrlen = sizeof(saddr4);
2883 ret = zsock_recvfrom(server_sock, rx_buf, sizeof(rx_buf),
2884 0, (struct sockaddr *)&saddr4, &addrlen);
2885 zexpect_true(ret >= 0, "recvfrom fail");
2886 zexpect_equal(ret, strlen(TEST_STR_SMALL),
2887 "unexpected received bytes");
2888 zexpect_mem_equal(rx_buf, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1,
2889 "wrong data");
2890
2891 /* Then use mreq structure to set the interface */
2892 optlen = sizeof(mreq);
2893 ifaddr = net_if_ipv4_addr_get_first_by_index(net_if_get_by_iface(net_if_get_default()));
2894 zexpect_not_null(ifaddr, "No address found for interface %d",
2895 net_if_get_by_iface(net_if_get_default()));
2896 mreq.imr_interface.s_addr = ifaddr->address.in_addr.s_addr;
2897
2898 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_MULTICAST_IF,
2899 &mreq, optlen);
2900 zexpect_equal(ret, 0, "setsockopt failed (%d)", errno);
2901
2902 ret = zsock_sendto(sock, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1, 0,
2903 (struct sockaddr *)&dst_addr, sizeof(dst_addr));
2904 zexpect_equal(ret, STRLEN(TEST_STR_SMALL),
2905 "invalid send len (was %d expected %d) (%d)",
2906 ret, STRLEN(TEST_STR_SMALL), -errno);
2907
2908 /* Test that the sent data is received from default interface. */
2909 addrlen = sizeof(saddr4);
2910 ret = zsock_recvfrom(server_sock, rx_buf, sizeof(rx_buf),
2911 0, (struct sockaddr *)&saddr4, &addrlen);
2912 zexpect_true(ret >= 0, "recvfrom fail");
2913 zexpect_equal(ret, strlen(TEST_STR_SMALL),
2914 "unexpected received bytes");
2915 zexpect_mem_equal(rx_buf, TEST_STR_SMALL, sizeof(TEST_STR_SMALL) - 1,
2916 "wrong data");
2917
2918 ret = zsock_close(sock);
2919 zassert_equal(ret, 0, "Cannot close socket (%d)", -errno);
2920
2921 ret = zsock_close(server_sock);
2922 zassert_equal(ret, 0, "Cannot close socket (%d)", -errno);
2923
2924 test_started = false;
2925 loopback_enable_address_swap(true);
2926 }
2927
2928 #if defined(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE)
2929
2930 #define PORT_RANGE(lower, upper) \
2931 (uint32_t)(((uint16_t)(upper) << 16) | (uint16_t)(lower))
2932
check_port_range(struct sockaddr * my_addr,size_t my_addr_len,struct sockaddr * local_addr,size_t local_addr_len)2933 static void check_port_range(struct sockaddr *my_addr,
2934 size_t my_addr_len,
2935 struct sockaddr *local_addr,
2936 size_t local_addr_len)
2937 {
2938 sa_family_t family = AF_UNSPEC;
2939 uint32_t optval;
2940 size_t addr_len;
2941 size_t optlen;
2942 int sock;
2943 int ret, err;
2944
2945 addr_len = local_addr_len;
2946
2947 if (my_addr->sa_family == AF_INET) {
2948 family = AF_INET;
2949 } else if (my_addr->sa_family == AF_INET6) {
2950 family = AF_INET6;
2951 } else {
2952 zassert_true(false, "Invalid address family %d",
2953 my_addr->sa_family);
2954 }
2955
2956 sock = zsock_socket(family, SOCK_DGRAM, IPPROTO_UDP);
2957 zassert_true(sock >= 0, "Cannot create socket (%d)", -errno);
2958
2959 optval = PORT_RANGE(1024, 1500);
2960 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
2961 &optval, sizeof(optval));
2962 err = -errno;
2963 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
2964
2965 optval = 0; optlen = 0U;
2966 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
2967 &optval, optlen);
2968 err = -errno;
2969 zexpect_equal(ret, -1, "setsockopt failed (%d)", err);
2970
2971 optval = 0; optlen = sizeof(uint64_t);
2972 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
2973 &optval, optlen);
2974 err = -errno;
2975 zexpect_equal(ret, -1, "setsockopt failed (%d)", err);
2976
2977 optval = PORT_RANGE(0, 0);
2978 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
2979 &optval, sizeof(optval));
2980 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
2981
2982 /* Linux allows setting the invalid port range but that is not
2983 * then taken into use when we bind the socket.
2984 */
2985 optval = PORT_RANGE(1024, 0);
2986 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
2987 &optval, sizeof(optval));
2988 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
2989
2990 optval = PORT_RANGE(0, 1024);
2991 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
2992 &optval, sizeof(optval));
2993 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
2994
2995 /* Then set a valid range and verify that bound socket is using it */
2996 optval = PORT_RANGE(10000, 10010);
2997 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
2998 &optval, sizeof(optval));
2999 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
3000
3001 optval = 0; optlen = sizeof(optval);
3002 ret = zsock_getsockopt(sock, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
3003 &optval, &optlen);
3004 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
3005 zexpect_equal(optval, PORT_RANGE(10000, 10010), "Invalid port range");
3006
3007 ret = zsock_bind(sock, my_addr, my_addr_len);
3008 err = -errno;
3009 zexpect_equal(ret, 0, "bind failed (%d)", err);
3010
3011 ret = zsock_getsockname(sock, local_addr, &addr_len);
3012 err = -errno;
3013 zexpect_equal(ret, 0, "getsockname failed (%d)", err);
3014
3015 /* The port should be in the range */
3016 zexpect_true(ntohs(net_sin(local_addr)->sin_port) >= 10000 &&
3017 ntohs(net_sin(local_addr)->sin_port) <= 10010,
3018 "Invalid port %d", ntohs(net_sin(local_addr)->sin_port));
3019
3020 (void)zsock_close(sock);
3021
3022 /* Try setting invalid range and verify that we do not net a port from that
3023 * range.
3024 */
3025 sock = zsock_socket(family, SOCK_DGRAM, IPPROTO_UDP);
3026 zassert_true(sock >= 0, "Cannot create socket (%d)", -errno);
3027
3028 optval = PORT_RANGE(1001, 1000);
3029 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
3030 &optval, sizeof(optval));
3031 err = -errno;
3032 zexpect_equal(ret, -1, "setsockopt failed (%d)", err);
3033 zexpect_equal(err, -EINVAL, "Invalid errno (%d)", -err);
3034
3035 /* Port range cannot be just one port */
3036 optval = PORT_RANGE(1001, 1001);
3037 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
3038 &optval, sizeof(optval));
3039 err = -errno;
3040 zexpect_equal(ret, -1, "setsockopt failed (%d)", err);
3041 zexpect_equal(err, -EINVAL, "Invalid errno (%d)", -err);
3042
3043 optval = PORT_RANGE(0, 1000);
3044 ret = zsock_setsockopt(sock, IPPROTO_IP, IP_LOCAL_PORT_RANGE,
3045 &optval, sizeof(optval));
3046 err = -errno;
3047 zexpect_equal(ret, 0, "setsockopt failed (%d)", err);
3048
3049 ret = zsock_bind(sock, my_addr, my_addr_len);
3050 err = -errno;
3051 zexpect_equal(ret, 0, "bind failed (%d)", err);
3052
3053 addr_len = local_addr_len;
3054 ret = zsock_getsockname(sock, local_addr, &addr_len);
3055 err = -errno;
3056 zexpect_equal(ret, 0, "getsockname failed (%d)", err);
3057
3058 /* The port should not be in the range */
3059 zexpect_false(ntohs(net_sin(local_addr)->sin_port) >= 1000 &&
3060 ntohs(net_sin(local_addr)->sin_port) <= 1001,
3061 "Invalid port %d", ntohs(net_sin(local_addr)->sin_port));
3062
3063 (void)zsock_close(sock);
3064 }
3065 #endif
3066
ZTEST(net_socket_udp,test_40_clamp_udp_tcp_port_range)3067 ZTEST(net_socket_udp, test_40_clamp_udp_tcp_port_range)
3068 {
3069 #if defined(CONFIG_NET_CONTEXT_CLAMP_PORT_RANGE)
3070 struct sockaddr_in my_addr4 = {
3071 .sin_family = AF_INET,
3072 .sin_port = 0,
3073 .sin_addr = { { { 192, 0, 2, 2 } } },
3074 };
3075 struct sockaddr_in6 my_addr6 = {
3076 .sin6_family = AF_INET6,
3077 .sin6_port = 0,
3078 .sin6_addr = in6addr_loopback,
3079 };
3080 struct sockaddr_in local_addr4;
3081 struct sockaddr_in6 local_addr6;
3082
3083 /* First try with a IPv4 socket */
3084 check_port_range((struct sockaddr *)&my_addr4, sizeof(my_addr4),
3085 (struct sockaddr *)&local_addr4, sizeof(local_addr4));
3086
3087 /* Finally try with a IPv6 socket */
3088 check_port_range((struct sockaddr *)&my_addr6, sizeof(my_addr6),
3089 (struct sockaddr *)&local_addr6, sizeof(local_addr6));
3090 #else
3091 ztest_test_skip();
3092 #endif
3093 }
3094
after(void * arg)3095 static void after(void *arg)
3096 {
3097 ARG_UNUSED(arg);
3098
3099 for (int i = 0; i < CONFIG_ZVFS_OPEN_MAX; ++i) {
3100 (void)zsock_close(i);
3101 }
3102 }
3103
3104 ZTEST_SUITE(net_socket_udp, NULL, NULL, NULL, after, NULL);
3105