1 /*
2  * Copyright (c) 2020 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(net_test, CONFIG_NET_SOCKETS_LOG_LEVEL);
9 
10 #include <stdio.h>
11 #include <zephyr/sys/mutex.h>
12 #include <zephyr/ztest_assert.h>
13 
14 #include <fcntl.h>
15 #include <zephyr/net/socket.h>
16 #include <zephyr/net/ethernet.h>
17 
18 #if defined(CONFIG_NET_SOCKETS_LOG_LEVEL_DBG)
19 #define DBG(fmt, ...) NET_DBG(fmt, ##__VA_ARGS__)
20 #else
21 #define DBG(fmt, ...)
22 #endif
23 
24 #define IPV4_ADDR "127.0.0.1"
25 
26 static uint8_t lladdr1[] = { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
27 static uint8_t lladdr2[] = { 0x02, 0x02, 0x02, 0x02, 0x02, 0x02 };
28 
29 struct eth_fake_context {
30 	struct net_if *iface;
31 	uint8_t *mac_address;
32 	char *ip_address;
33 };
34 
35 static struct eth_fake_context eth_fake_data1 = {
36 	.mac_address = lladdr1,
37 	.ip_address = IPV4_ADDR,
38 };
39 static struct eth_fake_context eth_fake_data2 = {
40 	.mac_address = lladdr2
41 };
42 
eth_fake_send(const struct device * dev,struct net_pkt * pkt)43 static int eth_fake_send(const struct device *dev, struct net_pkt *pkt)
44 {
45 	struct net_pkt *recv_pkt;
46 	int ret;
47 	struct net_if *target_iface;
48 
49 	ARG_UNUSED(dev);
50 	ARG_UNUSED(pkt);
51 
52 	DBG("Sending data (%d bytes) to iface %d\n",
53 	    net_pkt_get_len(pkt), net_if_get_by_iface(net_pkt_iface(pkt)));
54 
55 	recv_pkt = net_pkt_rx_clone(pkt, K_NO_WAIT);
56 
57 	if (memcmp(pkt->frags->data, lladdr1, sizeof(lladdr1)) == 0) {
58 		target_iface = eth_fake_data1.iface;
59 	} else {
60 		target_iface = eth_fake_data2.iface;
61 	}
62 
63 	net_pkt_set_iface(recv_pkt, target_iface);
64 
65 	k_sleep(K_MSEC(10)); /* Let the receiver run */
66 
67 	ret = net_recv_data(net_pkt_iface(recv_pkt), recv_pkt);
68 	zassert_equal(ret, 0, "Cannot receive data (%d)", ret);
69 
70 	return 0;
71 }
72 
eth_fake_iface_init(struct net_if * iface)73 static void eth_fake_iface_init(struct net_if *iface)
74 {
75 	const struct device *dev = net_if_get_device(iface);
76 	struct eth_fake_context *ctx = dev->data;
77 
78 	ctx->iface = iface;
79 
80 	net_if_set_link_addr(iface, ctx->mac_address, 6, NET_LINK_ETHERNET);
81 
82 	if (ctx->ip_address != NULL) {
83 		struct in_addr addr;
84 
85 		if (net_addr_pton(AF_INET, ctx->ip_address, &addr) == 0) {
86 			net_if_ipv4_addr_add(iface, &addr, NET_ADDR_MANUAL, 0);
87 		}
88 	}
89 
90 	ethernet_init(iface);
91 }
92 
93 static struct ethernet_api eth_fake_api_funcs = {
94 	.iface_api.init = eth_fake_iface_init,
95 	.send = eth_fake_send,
96 };
97 
98 ETH_NET_DEVICE_INIT(eth_fake1, "eth_fake1", NULL, NULL, &eth_fake_data1, NULL,
99 		    CONFIG_ETH_INIT_PRIORITY, &eth_fake_api_funcs,
100 		    NET_ETH_MTU);
101 
102 ETH_NET_DEVICE_INIT(eth_fake2, "eth_fake2", NULL, NULL, &eth_fake_data2, NULL,
103 		    CONFIG_ETH_INIT_PRIORITY, &eth_fake_api_funcs,
104 		    NET_ETH_MTU);
105 
setup_socket(struct net_if * iface,int type,int proto)106 static int setup_socket(struct net_if *iface, int type, int proto)
107 {
108 	int sock;
109 
110 	sock = socket(AF_PACKET, type, proto);
111 	zassert_true(sock >= 0, "Cannot create packet socket (%d)", -errno);
112 
113 	return sock;
114 }
115 
bind_socket(int sock,struct net_if * iface)116 static int bind_socket(int sock, struct net_if *iface)
117 {
118 	struct sockaddr_ll addr;
119 
120 	memset(&addr, 0, sizeof(addr));
121 
122 	addr.sll_ifindex = net_if_get_by_iface(iface);
123 	addr.sll_family = AF_PACKET;
124 
125 	return bind(sock, (struct sockaddr *)&addr, sizeof(addr));
126 }
127 
128 struct user_data {
129 	struct net_if *first;
130 	struct net_if *second;
131 };
132 
iface_cb(struct net_if * iface,void * user_data)133 static void iface_cb(struct net_if *iface, void *user_data)
134 {
135 	struct user_data *ud = user_data;
136 	struct net_linkaddr *link_addr;
137 
138 	if (net_if_l2(iface) != &NET_L2_GET_NAME(ETHERNET)) {
139 		return;
140 	}
141 
142 	link_addr = net_if_get_link_addr(iface);
143 	if (memcmp(link_addr->addr, lladdr1, sizeof(lladdr1)) != 0 &&
144 	    memcmp(link_addr->addr, lladdr2, sizeof(lladdr2)) != 0) {
145 		return;
146 	}
147 
148 	if (ud->first == NULL) {
149 		ud->first = iface;
150 		return;
151 	}
152 
153 	ud->second = iface;
154 }
155 
setblocking(int fd,bool val)156 static void setblocking(int fd, bool val)
157 {
158 	int fl, res;
159 
160 	fl = fcntl(fd, F_GETFL, 0);
161 	zassert_not_equal(fl, -1, "Fail to set fcntl");
162 
163 	if (val) {
164 		fl &= ~O_NONBLOCK;
165 	} else {
166 		fl |= O_NONBLOCK;
167 	}
168 
169 	res = fcntl(fd, F_SETFL, fl);
170 	zassert_not_equal(res, -1, "Fail to set fcntl");
171 }
172 
173 #define SRC_PORT 4240
174 #define DST_PORT 4242
prepare_udp_socket(struct sockaddr_in * sockaddr,uint16_t local_port)175 static int prepare_udp_socket(struct sockaddr_in *sockaddr, uint16_t local_port)
176 {
177 	int sock, ret;
178 
179 	sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
180 	zassert_true(sock >= 0, "Cannot create DGRAM (UDP) socket (%d)", sock);
181 
182 	sockaddr->sin_family = AF_INET;
183 	sockaddr->sin_port = htons(local_port);
184 	ret = inet_pton(AF_INET, IPV4_ADDR, &sockaddr->sin_addr);
185 	zassert_equal(ret, 1, "inet_pton failed");
186 
187 	/* Bind UDP socket to local port */
188 	ret = bind(sock, (struct sockaddr *) sockaddr, sizeof(*sockaddr));
189 	zassert_equal(ret, 0, "Cannot bind DGRAM (UDP) socket (%d)", -errno);
190 
191 	return sock;
192 }
193 
__test_packet_sockets(int * sock1,int * sock2)194 static void __test_packet_sockets(int *sock1, int *sock2)
195 {
196 	struct user_data ud = { 0 };
197 	int ret;
198 
199 	net_if_foreach(iface_cb, &ud);
200 
201 	zassert_not_null(ud.first, "1st Ethernet interface not found");
202 	zassert_not_null(ud.second, "2nd Ethernet interface not found");
203 
204 	*sock1 = setup_socket(ud.first, SOCK_RAW, ETH_P_ALL);
205 	zassert_true(*sock1 >= 0, "Cannot create 1st socket (%d)", *sock1);
206 
207 	*sock2 = setup_socket(ud.second, SOCK_RAW, ETH_P_ALL);
208 	zassert_true(*sock2 >= 0, "Cannot create 2nd socket (%d)", *sock2);
209 
210 	ret = bind_socket(*sock1, ud.first);
211 	zassert_equal(ret, 0, "Cannot bind 1st socket (%d)", -errno);
212 
213 	ret = bind_socket(*sock2, ud.second);
214 	zassert_equal(ret, 0, "Cannot bind 2nd socket (%d)", -errno);
215 }
216 
217 #define IP_HDR_SIZE 20
218 #define UDP_HDR_SIZE 8
219 #define HDR_SIZE (IP_HDR_SIZE + UDP_HDR_SIZE)
ZTEST(socket_packet,test_raw_packet_sockets)220 ZTEST(socket_packet, test_raw_packet_sockets)
221 {
222 	uint8_t data_to_send[] = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 };
223 	uint8_t data_to_receive[sizeof(data_to_send) + HDR_SIZE];
224 	struct sockaddr_ll src;
225 	struct sockaddr_in sockaddr;
226 	int ret, sock1, sock2, sock3, sock4;
227 	socklen_t addrlen;
228 	ssize_t sent = 0;
229 
230 	__test_packet_sockets(&sock1, &sock2);
231 
232 	/* Prepare UDP socket which will read data */
233 	sock3 = prepare_udp_socket(&sockaddr, DST_PORT);
234 
235 	/* Prepare UDP socket from which data are going to be sent */
236 	sock4 = prepare_udp_socket(&sockaddr, SRC_PORT);
237 	/* Properly set destination port for UDP packet */
238 	sockaddr.sin_port = htons(DST_PORT);
239 
240 	/*
241 	 * Send UDP datagram to us - as check_ip_addr() in net_send_data()
242 	 * returns 1 - the packet is processed immediately in the net stack
243 	 */
244 	sent = sendto(sock4, data_to_send, sizeof(data_to_send),
245 		      0, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
246 	zassert_equal(sent, sizeof(data_to_send), "sendto failed");
247 
248 	k_msleep(10); /* Let the packet enter the system */
249 
250 	setblocking(sock3, false);
251 	memset(&data_to_receive, 0, sizeof(data_to_receive));
252 	errno = 0;
253 
254 	/* Check if UDP packets can be read after being sent */
255 	addrlen = sizeof(sockaddr);
256 	ret = recvfrom(sock3, data_to_receive, sizeof(data_to_receive),
257 		       0, (struct sockaddr *)&sockaddr, &addrlen);
258 	zassert_equal(ret, sizeof(data_to_send), "Cannot receive all data (%d)",
259 		      -errno);
260 	zassert_mem_equal(data_to_receive, data_to_send, sizeof(data_to_send),
261 			  "Sent and received buffers do not match");
262 
263 	/* And if the packet has been also passed to RAW socket */
264 	setblocking(sock1, false);
265 	memset(&data_to_receive, 0, sizeof(data_to_receive));
266 	memset(&src, 0, sizeof(src));
267 	addrlen = sizeof(src);
268 	errno = 0;
269 
270 	/* The recvfrom reads the the whole received packet - including its
271 	 * IP (20B) and UDP (8) headers. After those we can expect payload,
272 	 * which have been sent.
273 	 */
274 	ret = recvfrom(sock1, data_to_receive, sizeof(data_to_receive), 0,
275 		       (struct sockaddr *)&src, &addrlen);
276 	zassert_equal(ret, sizeof(data_to_send) + HDR_SIZE,
277 		      "Cannot receive all data (%d vs %zd) (%d)",
278 		      ret, sizeof(data_to_send), -errno);
279 	zassert_mem_equal(&data_to_receive[HDR_SIZE], data_to_send,
280 			  sizeof(data_to_send),
281 			  "Sent and received buffers do not match");
282 
283 	close(sock1);
284 	close(sock2);
285 	close(sock3);
286 	close(sock4);
287 }
288 
ZTEST(socket_packet,test_packet_sockets)289 ZTEST(socket_packet, test_packet_sockets)
290 {
291 	int sock1, sock2;
292 
293 	__test_packet_sockets(&sock1, &sock2);
294 
295 	close(sock1);
296 	close(sock2);
297 }
298 
ZTEST(socket_packet,test_packet_sockets_dgram)299 ZTEST(socket_packet, test_packet_sockets_dgram)
300 {
301 	uint8_t data_to_send[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
302 	uint8_t data_to_receive[32];
303 	socklen_t addrlen = sizeof(struct sockaddr_ll);
304 	struct user_data ud = { 0 };
305 	struct sockaddr_ll dst, src;
306 	int ret, sock1, sock2;
307 	int iter, max_iter = 10;
308 
309 	net_if_foreach(iface_cb, &ud);
310 
311 	zassert_not_null(ud.first, "1st Ethernet interface not found");
312 	zassert_not_null(ud.second, "2nd Ethernet interface not found");
313 
314 	sock1 = setup_socket(ud.first, SOCK_DGRAM, ETH_P_TSN);
315 	zassert_true(sock1 >= 0, "Cannot create 1st socket (%d)", sock1);
316 
317 	sock2 = setup_socket(ud.second, SOCK_DGRAM, ETH_P_TSN);
318 	zassert_true(sock2 >= 0, "Cannot create 2nd socket (%d)", sock2);
319 
320 	ret = bind_socket(sock1, ud.first);
321 	zassert_equal(ret, 0, "Cannot bind 1st socket (%d)", -errno);
322 
323 	ret = bind_socket(sock2, ud.second);
324 	zassert_equal(ret, 0, "Cannot bind 2nd socket (%d)", -errno);
325 
326 	setblocking(sock1, false);
327 	setblocking(sock2, false);
328 
329 	memset(&dst, 0, sizeof(dst));
330 	dst.sll_family = AF_PACKET;
331 	dst.sll_protocol = htons(ETH_P_IP);
332 	memcpy(dst.sll_addr, lladdr1, sizeof(lladdr1));
333 
334 	ret = sendto(sock2, data_to_send, sizeof(data_to_send), 0,
335 		     (const struct sockaddr *)&dst, sizeof(struct sockaddr_ll));
336 	zassert_equal(ret, sizeof(data_to_send), "Cannot send all data (%d)",
337 		      -errno);
338 
339 	k_msleep(10); /* Let the packet enter the system */
340 	memset(&src, 0, sizeof(src));
341 
342 	errno = 0;
343 	iter = 0;
344 	do {
345 		ret = recvfrom(sock1, data_to_receive, sizeof(data_to_receive),
346 			       0, (struct sockaddr *)&src, &addrlen);
347 		k_msleep(10);
348 		iter++;
349 	} while (ret < 0 && errno == EAGAIN && iter < max_iter);
350 
351 	zassert_equal(ret, sizeof(data_to_send),
352 		      "Cannot receive all data (%d vs %zd) (%d)",
353 		      ret, sizeof(data_to_send), -errno);
354 
355 	zassert_mem_equal(data_to_send, data_to_receive, sizeof(data_to_send),
356 			  "Data mismatch");
357 
358 	memcpy(dst.sll_addr, lladdr2, sizeof(lladdr2));
359 
360 	/* Send to socket 2 but read from socket 1. There should not be any
361 	 * data in socket 1
362 	 */
363 	ret = sendto(sock2, data_to_send, sizeof(data_to_send), 0,
364 		     (const struct sockaddr *)&dst, sizeof(struct sockaddr_ll));
365 	zassert_equal(ret, sizeof(data_to_send), "Cannot send all data (%d)",
366 		      -errno);
367 
368 	k_msleep(10);
369 	memset(&src, 0, sizeof(src));
370 
371 	ret = recvfrom(sock1, data_to_receive, sizeof(data_to_receive), 0,
372 		       (struct sockaddr *)&src, &addrlen);
373 	zassert_equal(ret, -1, "Received something (%d)", ret);
374 	zassert_equal(errno, EAGAIN, "Wrong errno (%d)", errno);
375 
376 	memset(&src, 0, sizeof(src));
377 
378 	errno = 0;
379 	iter = 0;
380 	do {
381 		ret = recvfrom(sock2, data_to_receive, sizeof(data_to_receive),
382 			       0, (struct sockaddr *)&src, &addrlen);
383 		k_msleep(10);
384 		iter++;
385 	} while (ret < 0 && errno == EAGAIN && iter < max_iter);
386 
387 	zassert_equal(ret, sizeof(data_to_send), "Cannot receive all data (%d)",
388 		      -errno);
389 	zassert_mem_equal(data_to_send, data_to_receive, sizeof(data_to_send),
390 			  "Data mismatch");
391 
392 	close(sock1);
393 	close(sock2);
394 }
395 
ZTEST(socket_packet,test_raw_and_dgram_socket_exchange)396 ZTEST(socket_packet, test_raw_and_dgram_socket_exchange)
397 {
398 	uint8_t data_to_send[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
399 	uint8_t data_to_receive[32];
400 	socklen_t addrlen = sizeof(struct sockaddr_ll);
401 	struct user_data ud = { 0 };
402 	struct sockaddr_ll dst, src;
403 	int ret, sock1, sock2;
404 	int iter, max_iter = 10;
405 	const uint8_t expected_payload_raw[] = {
406 		0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* Dst ll addr */
407 		0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* Src ll addr */
408 		ETH_P_IP >> 8, ETH_P_IP & 0xFF, /* EtherType */
409 		0, 1, 2, 3, 4, 5, 6, 7, 8, 9 /* Payload */
410 	};
411 	const uint8_t send_payload_raw[] = {
412 		0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* Dst ll addr */
413 		0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* Src ll addr */
414 		ETH_P_IP >> 8, ETH_P_IP & 0xFF, /* EtherType */
415 		0, 1, 2, 3, 4, 5, 6, 7, 8, 9 /* Payload */
416 	};
417 
418 
419 	net_if_foreach(iface_cb, &ud);
420 
421 	zassert_not_null(ud.first, "1st Ethernet interface not found");
422 	zassert_not_null(ud.second, "2nd Ethernet interface not found");
423 
424 	sock1 = setup_socket(ud.first, SOCK_DGRAM, ETH_P_ALL);
425 	zassert_true(sock1 >= 0, "Cannot create 1st socket (%d)", sock1);
426 
427 	sock2 = setup_socket(ud.second, SOCK_RAW, ETH_P_ALL);
428 	zassert_true(sock2 >= 0, "Cannot create 2nd socket (%d)", sock2);
429 
430 	ret = bind_socket(sock1, ud.first);
431 	zassert_equal(ret, 0, "Cannot bind 1st socket (%d)", -errno);
432 
433 	ret = bind_socket(sock2, ud.second);
434 	zassert_equal(ret, 0, "Cannot bind 2nd socket (%d)", -errno);
435 
436 	setblocking(sock1, false);
437 	setblocking(sock2, false);
438 
439 	memset(&dst, 0, sizeof(dst));
440 	dst.sll_family = AF_PACKET;
441 	dst.sll_protocol = htons(ETH_P_IP);
442 	memcpy(dst.sll_addr, lladdr2, sizeof(lladdr1));
443 
444 	/* SOCK_DGRAM to SOCK_RAW */
445 
446 	ret = sendto(sock1, data_to_send, sizeof(data_to_send), 0,
447 		     (const struct sockaddr *)&dst, sizeof(struct sockaddr_ll));
448 	zassert_equal(ret, sizeof(data_to_send), "Cannot send all data (%d)",
449 		      -errno);
450 
451 	k_msleep(10); /* Let the packet enter the system */
452 	memset(&src, 0, sizeof(src));
453 
454 	errno = 0;
455 	iter = 0;
456 	do {
457 		ret = recvfrom(sock2, data_to_receive, sizeof(data_to_receive),
458 			       0, (struct sockaddr *)&src, &addrlen);
459 		k_msleep(10);
460 		iter++;
461 	} while (ret < 0 && errno == EAGAIN && iter < max_iter);
462 
463 	zassert_equal(ret, sizeof(expected_payload_raw),
464 		      "Cannot receive all data (%d vs %zd) (%d)",
465 		      ret, sizeof(expected_payload_raw), -errno);
466 
467 	zassert_mem_equal(expected_payload_raw, data_to_receive,
468 			  sizeof(expected_payload_raw), "Data mismatch");
469 
470 	memset(&dst, 0, sizeof(dst));
471 	dst.sll_family = AF_PACKET;
472 	dst.sll_protocol = htons(ETH_P_IP);
473 
474 	/* SOCK_RAW to SOCK_DGRAM */
475 
476 	ret = sendto(sock2, send_payload_raw, sizeof(send_payload_raw), 0,
477 		     (const struct sockaddr *)&dst, sizeof(struct sockaddr_ll));
478 	zassert_equal(ret, sizeof(send_payload_raw), "Cannot send all data (%d)",
479 		      -errno);
480 
481 	k_msleep(10);
482 	memset(&src, 0, sizeof(src));
483 
484 	errno = 0;
485 	iter = 0;
486 	do {
487 		ret = recvfrom(sock1, data_to_receive, sizeof(data_to_receive),
488 			       0, (struct sockaddr *)&src, &addrlen);
489 		k_msleep(10);
490 		iter++;
491 	} while (ret < 0 && errno == EAGAIN && iter < max_iter);
492 
493 	zassert_equal(ret, sizeof(data_to_send), "Cannot receive all data (%d)",
494 		      -errno);
495 	zassert_mem_equal(data_to_send, data_to_receive, sizeof(data_to_send),
496 			  "Data mismatch");
497 
498 	close(sock1);
499 	close(sock2);
500 }
501 
ZTEST(socket_packet,test_raw_and_dgram_socket_recv)502 ZTEST(socket_packet, test_raw_and_dgram_socket_recv)
503 {
504 	uint8_t data_to_send[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
505 	uint8_t data_to_receive[32];
506 	socklen_t addrlen = sizeof(struct sockaddr_ll);
507 	struct user_data ud = { 0 };
508 	struct sockaddr_ll dst, src;
509 	int ret, sock1, sock2, sock3;
510 	int iter, max_iter = 10;
511 	const uint8_t expected_payload_raw[] = {
512 		0x02, 0x02, 0x02, 0x02, 0x02, 0x02, /* Dst ll addr */
513 		0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* Src ll addr */
514 		ETH_P_IP >> 8, ETH_P_IP & 0xFF, /* EtherType */
515 		0, 1, 2, 3, 4, 5, 6, 7, 8, 9 /* Payload */
516 	};
517 
518 	net_if_foreach(iface_cb, &ud);
519 
520 	zassert_not_null(ud.first, "1st Ethernet interface not found");
521 	zassert_not_null(ud.second, "2nd Ethernet interface not found");
522 
523 	sock1 = setup_socket(ud.first, SOCK_DGRAM, ETH_P_ALL);
524 	zassert_true(sock1 >= 0, "Cannot create 1st socket (%d)", sock1);
525 
526 	sock2 = setup_socket(ud.second, SOCK_RAW, ETH_P_ALL);
527 	zassert_true(sock2 >= 0, "Cannot create 2nd socket (%d)", sock2);
528 
529 	sock3 = setup_socket(ud.second, SOCK_RAW, ETH_P_ALL);
530 	zassert_true(sock3 >= 0, "Cannot create 2nd socket (%d)", sock3);
531 
532 	ret = bind_socket(sock1, ud.first);
533 	zassert_equal(ret, 0, "Cannot bind 1st socket (%d)", -errno);
534 
535 	ret = bind_socket(sock2, ud.second);
536 	zassert_equal(ret, 0, "Cannot bind 2nd socket (%d)", -errno);
537 
538 	ret = bind_socket(sock3, ud.second);
539 	zassert_equal(ret, 0, "Cannot bind 2nd socket (%d)", -errno);
540 
541 	setblocking(sock1, false);
542 	setblocking(sock2, false);
543 	setblocking(sock3, false);
544 
545 	memset(&dst, 0, sizeof(dst));
546 	dst.sll_family = AF_PACKET;
547 	dst.sll_protocol = htons(ETH_P_IP);
548 	memcpy(dst.sll_addr, lladdr2, sizeof(lladdr1));
549 
550 	ret = sendto(sock1, data_to_send, sizeof(data_to_send), 0,
551 		     (const struct sockaddr *)&dst, sizeof(struct sockaddr_ll));
552 	zassert_equal(ret, sizeof(data_to_send), "Cannot send all data (%d)",
553 		      -errno);
554 
555 	k_msleep(10); /* Let the packet enter the system */
556 	memset(&src, 0, sizeof(src));
557 
558 	/* Both SOCK_DGRAM to SOCK_RAW sockets should receive packet. */
559 
560 	errno = 0;
561 	iter = 0;
562 	do {
563 		ret = recvfrom(sock2, data_to_receive, sizeof(data_to_receive),
564 			       0, (struct sockaddr *)&src, &addrlen);
565 		k_msleep(10);
566 		iter++;
567 	} while (ret < 0 && errno == EAGAIN && iter < max_iter);
568 
569 	zassert_equal(ret, sizeof(expected_payload_raw),
570 		      "Cannot receive all data (%d vs %zd) (%d)",
571 		      ret, sizeof(expected_payload_raw), -errno);
572 
573 	zassert_mem_equal(expected_payload_raw, data_to_receive,
574 			  sizeof(expected_payload_raw), "Data mismatch");
575 
576 	memset(&src, 0, sizeof(src));
577 
578 	errno = 0;
579 	iter = 0;
580 	do {
581 		ret = recvfrom(sock3, data_to_receive, sizeof(data_to_receive),
582 			       0, (struct sockaddr *)&src, &addrlen);
583 		k_msleep(10);
584 		iter++;
585 	} while (ret < 0 && errno == EAGAIN && iter < max_iter);
586 
587 	zassert_equal(ret, sizeof(expected_payload_raw),
588 		      "Cannot receive all data (%d)", -errno);
589 	zassert_mem_equal(expected_payload_raw, data_to_receive,
590 			  sizeof(expected_payload_raw), "Data mismatch");
591 
592 	close(sock1);
593 	close(sock2);
594 	close(sock3);
595 }
596 
597 ZTEST_SUITE(socket_packet, NULL, NULL, NULL, NULL, NULL);
598