1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2015 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/logging/log.h>
10 LOG_MODULE_REGISTER(net_test, CONFIG_NET_IPV6_LOG_LEVEL);
11 
12 #include <zephyr/types.h>
13 #include <stdbool.h>
14 #include <stddef.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <zephyr/linker/sections.h>
18 #include <zephyr/random/random.h>
19 
20 #include <zephyr/ztest.h>
21 
22 #include <zephyr/net/mld.h>
23 #include <zephyr/net/net_core.h>
24 #include <zephyr/net/net_pkt.h>
25 #include <zephyr/net/net_ip.h>
26 #include <zephyr/net/ethernet.h>
27 #include <zephyr/net/dummy.h>
28 #include <zephyr/net/udp.h>
29 #include <zephyr/net/dns_resolve.h>
30 
31 #include "icmpv6.h"
32 #include "ipv6.h"
33 #include "route.h"
34 
35 #include "udp_internal.h"
36 
37 #define NET_LOG_ENABLED 1
38 #include "net_private.h"
39 
40 #if defined(CONFIG_NET_IPV6_PE)
41 #define NET_IPV6_PE_FILTER_PREFIX_COUNT CONFIG_NET_IPV6_PE_FILTER_PREFIX_COUNT
42 #else
43 #define NET_IPV6_PE_FILTER_PREFIX_COUNT 0
44 #endif
45 
46 #define TEST_NET_IF net_if_lookup_by_dev(DEVICE_GET(eth_ipv6_net))
47 #define TEST_MSG_SIZE 128
48 
49 static struct net_in6_addr my_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
50 				       0, 0, 0, 0, 0, 0, 0, 0x1 } } };
51 static struct net_in6_addr peer_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
52 					 0, 0, 0, 0, 0, 0, 0, 0x2 } } };
53 static struct net_in6_addr multicast_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
54 					      0, 0, 0, 0, 0, 0, 0, 0x1 } } };
55 static struct net_in6_addr all_nodes_mcast = { { { 0xff, 0x02, 0, 0, 0, 0, 0, 0,
56 					       0, 0, 0, 0, 0, 0, 0, 0x1 } } };
57 
58 /* Below should match prefix/addr distributed in RA message. */
59 static struct net_in6_addr test_router_addr = { { {
60 	0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0x2, 0x60,
61 	0x97, 0xff, 0xfe, 0x07, 0x69, 0xea
62 } } };
63 static struct net_in6_addr test_ra_prefix = { { { 0x3f, 0xfe, 0x05, 0x07, 0, 0, 0, 1,
64 					      0, 0, 0, 0, 0, 0, 0, 0 } } };
65 static struct net_in6_addr test_ra_autoconf_addr = { { {
66 	0x3f, 0xfe, 0x05, 0x07, 0x00, 0x00, 0x00, 0x01,
67 	0x02, 0x00, 0x5e, 0xff, 0xfe, 0x00, 0x53, 0x00
68 } } };
69 
70 /* ICMPv6 NS frame (74 bytes) */
71 static const unsigned char icmpv6_ns_invalid[] = {
72 /* IPv6 header starts here */
73 	0x60, 0x00, 0x00, 0x00, 0x00, 0x20, 0x3A, 0xFF,
74 	0x20, 0x01, 0x0D, 0xB8, 0x00, 0x00, 0x00, 0x00,
75 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
76 	0x20, 0x01, 0x0D, 0xB8, 0x00, 0x00, 0x00, 0x00,
77 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
78 /* ICMPv6 NS header starts here */
79 	0x87, 0x00, 0x7B, 0x9C, 0x60, 0x00, 0x00, 0x00,
80 /* Target Address */
81 	0x20, 0x01, 0x0D, 0xB8, 0x00, 0x00, 0x00, 0x00,
82 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
83 /* Source link layer address */
84 	0x01, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0xD8,
85 /* Target link layer address */
86 	0x02, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0xD7,
87 /* Source link layer address */
88 	0x01, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0xD6,
89 /* MTU option */
90 	0x05, 0x01, 0x10, 0x00, 0x00, 0x00, 0x00, 0xD5,
91 };
92 
93 /* ICMPv6 NS frame (64 bytes) */
94 static const unsigned char icmpv6_ns_no_sllao[] = {
95 /* IPv6 header starts here */
96 	0x60, 0x00, 0x00, 0x00, 0x00, 0x18, 0x3A, 0xFF,
97 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
98 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
99 	0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
100 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
101 /* ICMPv6 NS header starts here */
102 	0x87, 0x00, 0x7B, 0x9C, 0x60, 0x00, 0x00, 0x00,
103 /* Target Address */
104 	0x20, 0x01, 0x0D, 0xB8, 0x00, 0x00, 0x00, 0x00,
105 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
106 };
107 
108 /* */
109 static const unsigned char icmpv6_ra[] = {
110 /* IPv6 header starts here */
111 	0x60, 0x00, 0x00, 0x00, 0x00, 0x70, 0x3a, 0xff,
112 	0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
113 	0x02, 0x60, 0x97, 0xff, 0xfe, 0x07, 0x69, 0xea,
114 	0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
116 /* ICMPv6 RA header starts here */
117 	0x86, 0x00, 0xbf, 0x01, 0x40, 0x00, 0x07, 0x08,
118 	0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
119 /* SLLAO */
120 	0x01, 0x01, 0x00, 0x60, 0x97, 0x07, 0x69, 0xea,
121 /* MTU */
122 	0x05, 0x01, 0x00, 0x00, 0x00, 0x00, 0x05, 0xdc,
123 /* Prefix info*/
124 	0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0xFF, 0xFF,
125 	0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
126 	0x3f, 0xfe, 0x05, 0x07, 0x00, 0x00, 0x00, 0x01,
127 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128 /* Route info */
129 	0x18, 0x03, 0x30, 0x08, 0xff, 0xff, 0xff, 0xff,
130 	0x20, 0x01, 0x0d, 0xb0, 0x0f, 0xff, 0x00, 0x00,
131 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132 /* Recursive DNS Server */
133 	0x19, 0x03, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
134 	0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
135 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
136 };
137 
138 /* IPv6 hop-by-hop option in the message */
139 static const unsigned char ipv6_hbho[] = {
140 /* IPv6 header starts here (IPv6 addresses are wrong) */
141 	0x60, 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x3f, /* `....6.? */
142 	0x20, 0x01, 0x0D, 0xB8, 0x00, 0x00, 0x00, 0x00,
143 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
144 	0x20, 0x01, 0x0D, 0xB8, 0x00, 0x00, 0x00, 0x00,
145 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
146 /* Hop-by-hop option starts here */
147 	0x11, 0x00,
148 /* RPL sub-option starts here */
149 	0x63, 0x04, 0x80, 0x1e, 0x01, 0x00,             /* ..c..... */
150 /* UDP header starts here (checksum is "fixed" in this example) */
151 	0xaa, 0xdc, 0xbf, 0xd7, 0x00, 0x2e, 0xa2, 0x55, /* ......M. */
152 /* User data starts here (38 bytes) */
153 	0x10, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, /* ........ */
154 	0x00, 0x00, 0x01, 0x00, 0x00, 0x02, 0x00, 0x02, /* ........ */
155 	0x00, 0x00, 0x03, 0x00, 0x00, 0x02, 0x00, 0x03, /* ........ */
156 	0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xc9, /* ........ */
157 	0x00, 0x00, 0x01, 0x00, 0x00, 0x00,             /* ...... */
158 };
159 
160 static int send_msg(struct net_in6_addr *src, struct net_in6_addr *dst);
161 
162 typedef void (*ns_callback)(struct net_pkt *pkt, void *user_data);
163 
164 struct test_ns_handler {
165 	ns_callback fn;
166 	void *user_data;
167 };
168 
169 static bool expecting_ra;
170 static uint32_t dad_time[3];
171 static bool test_failed;
172 static struct k_sem wait_data;
173 static bool recv_cb_called;
174 struct net_if_addr *ifaddr_record;
175 static struct test_ns_handler *ns_handler;
176 static int pkt_num;
177 
178 #define WAIT_TIME 250
179 #define WAIT_TIME_LONG CONFIG_NET_IPV6_NS_TIMEOUT
180 #define WAIT_TIME_NS_TIMEOUT (WAIT_TIME_LONG + WAIT_TIME)
181 #define SENDING 93244
182 #define MY_PORT 1969
183 #define PEER_PORT 16233
184 
185 struct net_test_ipv6 {
186 	struct ethernet_context ctx;
187 	uint8_t mac_addr[sizeof(struct net_eth_addr)];
188 	struct net_linkaddr ll_addr;
189 };
190 
net_test_dev_init(const struct device * dev)191 int net_test_dev_init(const struct device *dev)
192 {
193 	return 0;
194 }
195 
net_test_get_mac(const struct device * dev)196 static uint8_t *net_test_get_mac(const struct device *dev)
197 {
198 	struct net_test_ipv6 *context = dev->data;
199 
200 	if (context->mac_addr[2] == 0x00) {
201 		/* 00-00-5E-00-53-xx Documentation RFC 7042 */
202 		context->mac_addr[0] = 0x00;
203 		context->mac_addr[1] = 0x00;
204 		context->mac_addr[2] = 0x5E;
205 		context->mac_addr[3] = 0x00;
206 		context->mac_addr[4] = 0x53;
207 		context->mac_addr[5] = sys_rand8_get();
208 	}
209 
210 	return context->mac_addr;
211 }
212 
net_test_iface_init(struct net_if * iface)213 static void net_test_iface_init(struct net_if *iface)
214 {
215 	uint8_t *mac = net_test_get_mac(net_if_get_device(iface));
216 
217 	net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr),
218 			     NET_LINK_ETHERNET);
219 
220 	if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
221 		ethernet_init(iface);
222 	}
223 }
224 
225 /**
226  * @brief IPv6 handle RA message
227  */
prepare_ra_message(struct net_pkt * pkt)228 static void prepare_ra_message(struct net_pkt *pkt)
229 {
230 	struct net_eth_hdr hdr;
231 
232 	net_buf_unref(pkt->buffer);
233 	pkt->buffer = NULL;
234 
235 	net_pkt_alloc_buffer(pkt, sizeof(struct net_eth_hdr) +
236 			     sizeof(icmpv6_ra), NET_AF_UNSPEC, K_NO_WAIT);
237 	net_pkt_cursor_init(pkt);
238 
239 	hdr.type = net_htons(NET_ETH_PTYPE_IPV6);
240 	memset(&hdr.src, 0, sizeof(struct net_eth_addr));
241 	memcpy(&hdr.dst, net_pkt_iface(pkt)->if_dev->link_addr.addr,
242 	       sizeof(struct net_eth_addr));
243 
244 	net_pkt_set_overwrite(pkt, false);
245 
246 	net_pkt_write(pkt, &hdr, sizeof(struct net_eth_hdr));
247 	net_pkt_write(pkt, icmpv6_ra, sizeof(icmpv6_ra));
248 
249 	net_pkt_cursor_init(pkt);
250 }
251 
inject_na_message(struct net_if * iface,struct net_in6_addr * src,struct net_in6_addr * dst,struct net_in6_addr * target,uint8_t flags)252 static void inject_na_message(struct net_if *iface, struct net_in6_addr *src,
253 			      struct net_in6_addr *dst, struct net_in6_addr *target,
254 			      uint8_t flags)
255 {
256 	struct net_eth_hdr hdr;
257 	struct net_pkt *pkt;
258 	uint8_t na_flags[] = { flags, 0, 0, 0 };
259 	uint8_t na_tlla_opt[] = { 0x02, 0x01, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
260 
261 	pkt = net_pkt_alloc_with_buffer(iface, TEST_MSG_SIZE, NET_AF_INET6,
262 					NET_IPPROTO_ICMPV6, K_NO_WAIT);
263 	zassert_not_null(pkt, "Failed to allocate packet");
264 
265 	net_pkt_set_ipv6_hop_limit(pkt, NET_IPV6_ND_HOP_LIMIT);
266 
267 	hdr.type = net_htons(NET_ETH_PTYPE_IPV6);
268 	memset(&hdr.src, 0xaa, sizeof(struct net_eth_addr));
269 	memcpy(&hdr.dst, net_pkt_iface(pkt)->if_dev->link_addr.addr,
270 	       sizeof(struct net_eth_addr));
271 
272 	/* Reserve space for the L2 header. */
273 	net_buf_reserve(pkt->frags, sizeof(struct net_eth_hdr));
274 	net_pkt_cursor_init(pkt);
275 	net_pkt_set_overwrite(pkt, false);
276 
277 	zassert_ok(net_ipv6_create(pkt, src, dst));
278 	zassert_ok(net_icmpv6_create(pkt, NET_ICMPV6_NA, 0));
279 	zassert_ok(net_pkt_write(pkt, na_flags, sizeof(na_flags)));
280 	zassert_ok(net_pkt_write(pkt, target, sizeof(struct net_in6_addr)));
281 	zassert_ok(net_pkt_write(pkt, na_tlla_opt, sizeof(na_tlla_opt)));
282 
283 	net_pkt_cursor_init(pkt);
284 	net_ipv6_finalize(pkt, NET_IPPROTO_ICMPV6);
285 
286 	/* Fill L2 header. */
287 	net_buf_push_mem(pkt->frags, &hdr, sizeof(struct net_eth_hdr));
288 
289 	net_pkt_cursor_init(pkt);
290 	zassert_ok((net_recv_data(iface, pkt)), "Data receive for NA failed.");
291 }
292 
skip_headers(struct net_pkt * pkt)293 static void skip_headers(struct net_pkt *pkt)
294 {
295 	net_pkt_cursor_init(pkt);
296 	net_pkt_skip(pkt, sizeof(struct net_eth_hdr));
297 	net_pkt_skip(pkt, net_pkt_ip_hdr_len(pkt) + net_pkt_ipv6_ext_len(pkt));
298 	net_pkt_skip(pkt, sizeof(struct net_icmp_hdr));
299 }
300 
get_icmp_hdr(struct net_pkt * pkt)301 static struct net_icmp_hdr *get_icmp_hdr(struct net_pkt *pkt)
302 {
303 	NET_PKT_DATA_ACCESS_CONTIGUOUS_DEFINE(icmp_access, struct net_icmp_hdr);
304 	/* First frag is the ll header */
305 	struct net_buf *bak = pkt->buffer;
306 	struct net_pkt_cursor backup;
307 	struct net_icmp_hdr *hdr;
308 
309 	pkt->buffer = bak->frags;
310 
311 	net_pkt_cursor_backup(pkt, &backup);
312 	net_pkt_cursor_init(pkt);
313 
314 	if (net_pkt_skip(pkt, net_pkt_ip_hdr_len(pkt) +
315 			 net_pkt_ipv6_ext_len(pkt))) {
316 		hdr = NULL;
317 		goto out;
318 	}
319 
320 	hdr = (struct net_icmp_hdr *)net_pkt_get_data(pkt, &icmp_access);
321 
322 out:
323 	pkt->buffer = bak;
324 
325 	net_pkt_cursor_restore(pkt, &backup);
326 
327 	return hdr;
328 }
329 
330 
tester_send(const struct device * dev,struct net_pkt * pkt)331 static int tester_send(const struct device *dev, struct net_pkt *pkt)
332 {
333 	struct net_icmp_hdr *icmp;
334 
335 	if (!pkt->buffer) {
336 		TC_ERROR("No data to send!\n");
337 		return -ENODATA;
338 	}
339 
340 	icmp = get_icmp_hdr(pkt);
341 
342 	pkt_num++;
343 
344 	/* Reply with RA message */
345 	if (icmp->type == NET_ICMPV6_RS) {
346 		if (expecting_ra) {
347 			prepare_ra_message(pkt);
348 		} else {
349 			goto out;
350 		}
351 	}
352 
353 	if (icmp->type == NET_ICMPV6_NS) {
354 		if (ns_handler != NULL) {
355 			ns_handler->fn(pkt, ns_handler->user_data);
356 		}
357 
358 		if (dad_time[0] == 0U) {
359 			dad_time[0] = k_uptime_get_32();
360 		} else if (dad_time[1] == 0U) {
361 			dad_time[1] = k_uptime_get_32();
362 		} else if (dad_time[2] == 0U) {
363 			dad_time[2] = k_uptime_get_32();
364 		}
365 
366 		goto out;
367 	}
368 
369 	/* Feed this data back to us */
370 	if (net_recv_data(net_pkt_iface(pkt),
371 			  net_pkt_clone(pkt, K_NO_WAIT)) < 0) {
372 		TC_ERROR("Data receive failed.");
373 		goto out;
374 	}
375 
376 	return 0;
377 
378 out:
379 	test_failed = true;
380 
381 	return 0;
382 }
383 
384 /* Ethernet interface (interface under test) */
385 struct net_test_ipv6 net_test_data;
386 
387 static const struct ethernet_api net_test_if_api = {
388 	.iface_api.init = net_test_iface_init,
389 	.send = tester_send,
390 };
391 
392 #define _ETH_L2_LAYER ETHERNET_L2
393 #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(ETHERNET_L2)
394 
395 NET_DEVICE_INIT(eth_ipv6_net, "eth_ipv6_net",
396 		net_test_dev_init, NULL, &net_test_data, NULL,
397 		CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
398 		&net_test_if_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE,
399 		NET_ETH_MTU);
400 
test_iface_down_up(void)401 static void test_iface_down_up(void)
402 {
403 	zassert_ok(net_if_down(TEST_NET_IF), "Failed to bring iface down");
404 	k_msleep(10);
405 	zassert_ok(net_if_up(TEST_NET_IF), "Failed to bring iface up");
406 }
407 
test_iface_down_up_delayed_carrier(void)408 static void test_iface_down_up_delayed_carrier(void)
409 {
410 	zassert_ok(net_if_down(TEST_NET_IF), "Failed to bring iface down");
411 	k_msleep(10);
412 	net_if_carrier_off(TEST_NET_IF);
413 	zassert_ok(net_if_up(TEST_NET_IF), "Failed to bring iface up");
414 	k_msleep(10);
415 	net_if_carrier_on(TEST_NET_IF);
416 }
417 
test_iface_carrier_off_on(void)418 static void test_iface_carrier_off_on(void)
419 {
420 	net_if_carrier_off(TEST_NET_IF);
421 	k_msleep(10);
422 	net_if_carrier_on(TEST_NET_IF);
423 }
424 
425 /* dummy interface for multi-interface tests */
dummy_send(const struct device * dev,struct net_pkt * pkt)426 static int dummy_send(const struct device *dev, struct net_pkt *pkt)
427 {
428 	ARG_UNUSED(dev);
429 	ARG_UNUSED(pkt);
430 
431 	return 0;
432 }
433 
434 struct net_test_ipv6 net_dummy_data;
435 
436 static const struct dummy_api net_dummy_if_api = {
437 		.iface_api.init = net_test_iface_init,
438 		.send = dummy_send,
439 };
440 
441 #define _DUMMY_L2_LAYER DUMMY_L2
442 #define _DUMMY_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2)
443 
444 NET_DEVICE_INIT(eth_ipv6_net_dummy, "eth_ipv6_net_dummy",
445 		net_test_dev_init, NULL, &net_dummy_data,
446 		NULL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
447 		&net_dummy_if_api, _DUMMY_L2_LAYER, _DUMMY_L2_CTX_TYPE,
448 		127);
449 
450 /**
451  * @brief IPv6 add neighbor
452  */
add_neighbor(void)453 static void add_neighbor(void)
454 {
455 	struct net_nbr *nbr;
456 	struct net_linkaddr lladdr;
457 
458 	lladdr.addr[0] = 0x01;
459 	lladdr.addr[1] = 0x02;
460 	lladdr.addr[2] = 0x33;
461 	lladdr.addr[3] = 0x44;
462 	lladdr.addr[4] = 0x05;
463 	lladdr.addr[5] = 0x06;
464 
465 	lladdr.len = 6U;
466 	lladdr.type = NET_LINK_ETHERNET;
467 
468 	nbr = net_ipv6_nbr_add(TEST_NET_IF, &peer_addr, &lladdr,
469 			       false, NET_IPV6_NBR_STATE_REACHABLE);
470 	zassert_not_null(nbr, "Cannot add peer %s to neighbor cache\n",
471 			 net_sprint_ipv6_addr(&peer_addr));
472 }
473 
rm_neighbor(void)474 static void rm_neighbor(void)
475 {
476 	struct net_linkaddr lladdr;
477 
478 	lladdr.addr[0] = 0x01;
479 	lladdr.addr[1] = 0x02;
480 	lladdr.addr[2] = 0x33;
481 	lladdr.addr[3] = 0x44;
482 	lladdr.addr[4] = 0x05;
483 	lladdr.addr[5] = 0x06;
484 
485 	lladdr.len = 6U;
486 	lladdr.type = NET_LINK_ETHERNET;
487 
488 	net_ipv6_nbr_rm(TEST_NET_IF, &peer_addr);
489 }
490 
491 /**
492  * @brief IPv6 add more than max neighbors
493  */
add_max_neighbors(void)494 static void add_max_neighbors(void)
495 {
496 	struct net_in6_addr dst_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
497 					 0, 0, 0, 0, 0, 0, 0, 0x3 } } };
498 	struct net_nbr *nbr;
499 	struct net_linkaddr lladdr;
500 	uint8_t i;
501 
502 	lladdr.addr[0] = 0x01;
503 	lladdr.addr[1] = 0x02;
504 	lladdr.addr[2] = 0x33;
505 	lladdr.addr[3] = 0x44;
506 	lladdr.addr[4] = 0x05;
507 	lladdr.addr[5] = 0x07;
508 
509 	lladdr.len = 6U;
510 	lladdr.type = NET_LINK_ETHERNET;
511 
512 	for (i = 0U; i < CONFIG_NET_IPV6_MAX_NEIGHBORS + 1; i++) {
513 		lladdr.addr[5] += i;
514 		dst_addr.s6_addr[15] += i;
515 		nbr = net_ipv6_nbr_add(TEST_NET_IF, &dst_addr,
516 				       &lladdr, false,
517 				       NET_IPV6_NBR_STATE_STALE);
518 		zassert_not_null(nbr, "Cannot add peer %s to neighbor cache\n",
519 				 net_sprint_ipv6_addr(&dst_addr));
520 	}
521 }
522 
rm_max_neighbors(void)523 static void rm_max_neighbors(void)
524 {
525 	struct net_in6_addr dst_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
526 					 0, 0, 0, 0, 0, 0, 0, 0x3 } } };
527 	struct net_linkaddr lladdr;
528 	uint8_t i;
529 
530 	lladdr.addr[0] = 0x01;
531 	lladdr.addr[1] = 0x02;
532 	lladdr.addr[2] = 0x33;
533 	lladdr.addr[3] = 0x44;
534 	lladdr.addr[4] = 0x05;
535 	lladdr.addr[5] = 0x07;
536 
537 	lladdr.len = 6U;
538 	lladdr.type = NET_LINK_ETHERNET;
539 
540 	for (i = 0U; i < CONFIG_NET_IPV6_MAX_NEIGHBORS + 1; i++) {
541 		lladdr.addr[5] += i;
542 		dst_addr.s6_addr[15] += i;
543 		net_ipv6_nbr_rm(TEST_NET_IF, &dst_addr);
544 	}
545 }
546 
547 /**
548  * @brief IPv6 neighbor lookup fail
549  */
nbr_lookup_fail(void)550 static void nbr_lookup_fail(void)
551 {
552 	struct net_nbr *nbr;
553 
554 	nbr = net_ipv6_nbr_lookup(TEST_NET_IF, &peer_addr);
555 	zassert_is_null(nbr, "Neighbor %s found in cache\n",
556 			net_sprint_ipv6_addr(&peer_addr));
557 
558 }
559 
560 /**
561  * @brief IPv6 neighbor lookup ok
562  */
nbr_lookup_ok(void)563 static void nbr_lookup_ok(void)
564 {
565 	struct net_nbr *nbr;
566 
567 	nbr = net_ipv6_nbr_lookup(TEST_NET_IF, &peer_addr);
568 	zassert_not_null(nbr, "Neighbor %s not found in cache\n",
569 			 net_sprint_ipv6_addr(&peer_addr));
570 }
571 
572 /**
573  * @brief IPv6 setup
574  */
ipv6_setup(void)575 static void *ipv6_setup(void)
576 {
577 	struct net_if_addr *ifaddr = NULL, *ifaddr2;
578 	struct net_if *iface = TEST_NET_IF;
579 	struct net_if *iface2 = NULL;
580 	struct net_if_ipv6 *ipv6;
581 	int i;
582 
583 	zassert_not_null(iface, "Interface is NULL");
584 
585 	/* We cannot use net_if_ipv6_addr_add() to add the address to
586 	 * network interface in this case as that would trigger DAD which
587 	 * we are not prepared to handle here. So instead add the address
588 	 * manually in this special case so that subsequent tests can
589 	 * pass.
590 	 */
591 	zassert_false(net_if_config_ipv6_get(iface, &ipv6) < 0,
592 			"IPv6 config is not valid");
593 
594 	for (i = 0; i < NET_IF_MAX_IPV6_ADDR; i++) {
595 		if (iface->config.ip.ipv6->unicast[i].is_used) {
596 			continue;
597 		}
598 
599 		ifaddr = &iface->config.ip.ipv6->unicast[i];
600 
601 		ifaddr->is_used = true;
602 		ifaddr->address.family = NET_AF_INET6;
603 		ifaddr->addr_type = NET_ADDR_MANUAL;
604 		ifaddr->addr_state = NET_ADDR_PREFERRED;
605 		ifaddr_record = ifaddr;
606 		net_ipaddr_copy(&ifaddr->address.in6_addr, &my_addr);
607 		break;
608 	}
609 
610 	ifaddr2 = net_if_ipv6_addr_lookup(&my_addr, &iface2);
611 	zassert_true(ifaddr2 == ifaddr, "Invalid ifaddr (%p vs %p)\n", ifaddr, ifaddr2);
612 
613 	/* The semaphore is there to wait the data to be received. */
614 	k_sem_init(&wait_data, 0, UINT_MAX);
615 
616 	nbr_lookup_fail();
617 	add_neighbor();
618 	add_max_neighbors();
619 	nbr_lookup_ok();
620 	k_sleep(K_MSEC(50));
621 
622 	/* Last, randomized MAC byte needs to be copied to the expected autoconf
623 	 * address.
624 	 */
625 	test_ra_autoconf_addr.s6_addr[15] = net_if_get_link_addr(iface)->addr[5];
626 
627 	return NULL;
628 }
629 
ipv6_before(void * fixture)630 static void ipv6_before(void *fixture)
631 {
632 	ARG_UNUSED(fixture);
633 
634 	ns_handler = NULL;
635 }
636 
ipv6_teardown(void * dummy)637 static void ipv6_teardown(void *dummy)
638 {
639 	ARG_UNUSED(dummy);
640 	struct net_if *iface = TEST_NET_IF;
641 
642 	rm_max_neighbors();
643 	rm_neighbor();
644 
645 	net_ipv6_addr_create(&multicast_addr, 0xff02, 0, 0, 0, 0, 0, 0, 0x0001);
646 	net_if_ipv6_maddr_rm(iface, &multicast_addr);
647 	ifaddr_record->is_used = false;
648 }
649 
650 /**
651  * @brief IPv6 compare prefix
652  *
653  */
ZTEST(net_ipv6,test_cmp_prefix)654 ZTEST(net_ipv6, test_cmp_prefix)
655 {
656 	bool st;
657 
658 	struct net_in6_addr prefix1 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
659 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
660 	struct net_in6_addr prefix2 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
661 					0, 0, 0, 0, 0, 0, 0, 0x2 } } };
662 
663 	st = net_ipv6_is_prefix((uint8_t *)&prefix1, (uint8_t *)&prefix2, 64);
664 	zassert_true(st, "Prefix /64  compare failed");
665 
666 	st = net_ipv6_is_prefix((uint8_t *)&prefix1, (uint8_t *)&prefix2, 65);
667 	zassert_true(st, "Prefix /65 compare failed");
668 
669 	/* Set one extra bit in the other prefix for testing /65 */
670 	prefix1.s6_addr[8] = 0x80;
671 
672 	st = net_ipv6_is_prefix((uint8_t *)&prefix1, (uint8_t *)&prefix2, 65);
673 	zassert_false(st, "Prefix /65 compare should have failed");
674 
675 	/* Set two bits in prefix2, it is now /66 */
676 	prefix2.s6_addr[8] = 0xc0;
677 
678 	st = net_ipv6_is_prefix((uint8_t *)&prefix1, (uint8_t *)&prefix2, 65);
679 	zassert_true(st, "Prefix /65 compare failed");
680 
681 	/* Set all remaining bits in prefix2, it is now /128 */
682 	(void)memset(&prefix2.s6_addr[8], 0xff, 8);
683 
684 	st = net_ipv6_is_prefix((uint8_t *)&prefix1, (uint8_t *)&prefix2, 65);
685 	zassert_true(st, "Prefix /65 compare failed");
686 
687 	/* Comparing /64 should be still ok */
688 	st = net_ipv6_is_prefix((uint8_t *)&prefix1, (uint8_t *)&prefix2, 64);
689 	zassert_true(st, "Prefix /64 compare failed");
690 
691 	/* But comparing /66 should should fail */
692 	st = net_ipv6_is_prefix((uint8_t *)&prefix1, (uint8_t *)&prefix2, 66);
693 	zassert_false(st, "Prefix /66 compare should have failed");
694 
695 }
696 
697 /**
698  * @brief IPv6 send NS extra options
699  */
ZTEST(net_ipv6,test_send_ns_extra_options)700 ZTEST(net_ipv6, test_send_ns_extra_options)
701 {
702 	struct net_pkt *pkt;
703 	struct net_if *iface;
704 
705 	iface = TEST_NET_IF;
706 
707 	pkt = net_pkt_alloc_with_buffer(iface, sizeof(icmpv6_ns_invalid),
708 					NET_AF_UNSPEC, 0, K_FOREVER);
709 
710 	NET_ASSERT(pkt, "Out of TX packets");
711 
712 	net_pkt_write(pkt, icmpv6_ns_invalid, sizeof(icmpv6_ns_invalid));
713 	net_pkt_lladdr_clear(pkt);
714 
715 	zassert_false((net_recv_data(iface, pkt) < 0),
716 		      "Data receive for invalid NS failed.");
717 
718 }
719 
720 /**
721  * @brief IPv6 send NS no option
722  */
ZTEST(net_ipv6,test_send_ns_no_options)723 ZTEST(net_ipv6, test_send_ns_no_options)
724 {
725 	struct net_pkt *pkt;
726 	struct net_if *iface;
727 
728 	iface = TEST_NET_IF;
729 
730 	pkt = net_pkt_alloc_with_buffer(iface, sizeof(icmpv6_ns_no_sllao),
731 					NET_AF_UNSPEC, 0, K_FOREVER);
732 
733 	NET_ASSERT(pkt, "Out of TX packets");
734 
735 	net_pkt_write(pkt, icmpv6_ns_no_sllao, sizeof(icmpv6_ns_no_sllao));
736 	net_pkt_lladdr_clear(pkt);
737 
738 	zassert_false((net_recv_data(iface, pkt) < 0),
739 		      "Data receive for invalid NS failed.");
740 }
741 
742 struct test_nd_context {
743 	struct k_sem wait_ns;
744 	struct net_in6_addr *exp_ns_addr;
745 	bool reply;
746 };
747 
expect_nd_ns(struct net_pkt * pkt,void * user_data)748 static void expect_nd_ns(struct net_pkt *pkt, void *user_data)
749 {
750 	uint32_t res_bytes;
751 	struct net_in6_addr target;
752 	struct test_nd_context *ctx = user_data;
753 
754 	skip_headers(pkt);
755 
756 	zassert_ok(net_pkt_read_be32(pkt, &res_bytes), "Failed to read reserved bytes");
757 	zassert_equal(0, res_bytes, "Reserved bytes must be zeroed");
758 	zassert_ok(net_pkt_read(pkt, &target, sizeof(struct net_in6_addr)),
759 		   "Failed to read target address");
760 
761 	if (net_ipv6_addr_cmp(ctx->exp_ns_addr, &target)) {
762 		if (ctx->reply) {
763 			inject_na_message(net_pkt_iface(pkt), &target, &my_addr,
764 					  &target, NET_ICMPV6_NA_FLAG_SOLICITED);
765 		}
766 
767 		k_sem_give(&ctx->wait_ns);
768 	}
769 }
770 
771 extern int net_ipv6_nbr_test_cancel(void);
772 
ZTEST(net_ipv6,test_send_neighbor_discovery)773 ZTEST(net_ipv6, test_send_neighbor_discovery)
774 {
775 	static struct test_nd_context ctx = {
776 		.exp_ns_addr = &test_router_addr,
777 		.reply = true
778 	};
779 	static struct test_ns_handler handler = {
780 		.fn = expect_nd_ns,
781 		.user_data = &ctx
782 	};
783 	enum net_verdict verdict;
784 	struct net_nbr *nbr;
785 	struct k_mem_slab *tx;
786 	struct net_buf_pool *tx_data;
787 	int avail_buf_count;
788 	int avail_pkt_count;
789 	int ret;
790 
791 	net_pkt_get_info(NULL, &tx, NULL, &tx_data);
792 
793 	k_sem_init(&ctx.wait_ns, 0, 1);
794 	ns_handler = &handler;
795 
796 	(void)net_ipv6_nbr_rm(TEST_NET_IF, &test_router_addr);
797 
798 	/* Make sure we can queue two packets */
799 	pkt_num = 0;
800 
801 	avail_buf_count = atomic_get(&tx_data->avail_count);
802 	avail_pkt_count = k_mem_slab_num_free_get(tx);
803 
804 	verdict = send_msg(&my_addr, &test_router_addr);
805 	zassert_equal(verdict, NET_OK, "Packet was dropped (%d)", verdict);
806 
807 	/* Second attempt should be queued and give no NS. */
808 	verdict = send_msg(&my_addr, &test_router_addr);
809 	zassert_equal(verdict, NET_OK, "Packet was dropped (%d)", verdict);
810 
811 	/* At this point we should have sent one NS and queued one packet. */
812 	zassert_equal(pkt_num, 1, "Unexpected number of packets sent (%d)", pkt_num);
813 
814 	zassert_ok(k_sem_take(&ctx.wait_ns, K_MSEC(WAIT_TIME)),
815 		   "Timeout while waiting for expected NS");
816 	k_sleep(K_MSEC(10));
817 
818 	/* Neighbor should be here now. */
819 	nbr = net_ipv6_nbr_lookup(TEST_NET_IF, &test_router_addr);
820 	zassert_not_null(nbr, "Neighbor not found.");
821 	zassert_equal(net_ipv6_nbr_data(nbr)->state, NET_IPV6_NBR_STATE_REACHABLE,
822 		      "Neighbor should be reachable at this point.");
823 
824 	/* Packet count should now be 3, one for the first NS and two
825 	 * for the queued packets.
826 	 */
827 	zassert_equal(pkt_num, 3, "Unexpected number of packets sent (%d)", pkt_num);
828 
829 	/* Third attempt (neighbor valid) should give no NS. */
830 	verdict = send_msg(&my_addr, &test_router_addr);
831 	zassert_equal(verdict, NET_OK, "Packet was dropped (%d)", verdict);
832 	zassert_equal(k_sem_take(&ctx.wait_ns, K_MSEC(10)), -EAGAIN,
833 		      "Should not get NS");
834 
835 	/* Packet count should be 4 as we sent one more packet. */
836 	zassert_equal(pkt_num, 4, "Unexpected number of packets sent (%d)", pkt_num);
837 
838 	/* If there are anything pending by the NS reply timer, then
839 	 * then 1 is returned and we can update the buffer and packet
840 	 * counts.
841 	 */
842 	ret = net_ipv6_nbr_test_cancel();
843 	avail_pkt_count -= ret;
844 	avail_buf_count -= ret;
845 
846 	zassert_equal(k_mem_slab_num_free_get(tx), avail_pkt_count,
847 		      "Unexpected tx packet pool free count (%d vs %d)",
848 		      k_mem_slab_num_free_get(tx), avail_pkt_count);
849 
850 	zassert_equal(atomic_get(&tx_data->avail_count), avail_buf_count,
851 		      "Unexpected tx data pool available count (%ld vs %d)",
852 		      atomic_get(&tx_data->avail_count), avail_buf_count);
853 }
854 
ZTEST(net_ipv6,test_send_neighbor_discovery_timeout)855 ZTEST(net_ipv6, test_send_neighbor_discovery_timeout)
856 {
857 	static struct test_nd_context ctx = {
858 		.exp_ns_addr = &test_router_addr,
859 		.reply = true
860 	};
861 	enum net_verdict verdict;
862 	struct net_nbr *nbr;
863 
864 	k_sem_init(&ctx.wait_ns, 0, 1);
865 
866 	(void)net_ipv6_nbr_rm(TEST_NET_IF, &test_router_addr);
867 
868 	/* Make sure we can queue two packets */
869 	pkt_num = 0;
870 
871 	verdict = send_msg(&my_addr, &test_router_addr);
872 	zassert_equal(verdict, NET_OK, "Packet was dropped (%d)", verdict);
873 
874 	/* Second attempt should be queued and give no NS. */
875 	verdict = send_msg(&my_addr, &test_router_addr);
876 	zassert_equal(verdict, NET_OK, "Packet was dropped (%d)", verdict);
877 
878 	/* At this point we should have sent one NS and queued one packet. */
879 	zassert_equal(pkt_num, 1, "Unexpected number of packets sent (%d)", pkt_num);
880 
881 	k_sleep(K_MSEC(10));
882 
883 	zassert_not_ok(k_sem_take(&ctx.wait_ns, K_MSEC(WAIT_TIME_NS_TIMEOUT)),
884 		       "Timeout while waiting for expected NS");
885 
886 	nbr = net_ipv6_nbr_lookup(TEST_NET_IF, &test_router_addr);
887 	zassert_not_null(nbr, "Neighbor not found.");
888 
889 	/* Packet count should be 2, one for the first NS and second for the
890 	 * timeouted NS packet.
891 	 */
892 	zassert_equal(pkt_num, 2, "Unexpected number of packets sent (%d)", pkt_num);
893 
894 	(void)net_ipv6_nbr_rm(TEST_NET_IF, &test_router_addr);
895 }
896 
897 /**
898  * @brief IPv6 prefix timeout
899  */
ZTEST(net_ipv6,test_prefix_timeout)900 ZTEST(net_ipv6, test_prefix_timeout)
901 {
902 	struct net_if_ipv6_prefix *prefix;
903 	struct net_in6_addr addr = { { { 0x20, 1, 0x0d, 0xb8, 42, 0, 0, 0,
904 				     0, 0, 0, 0, 0, 0, 0, 0 } } };
905 	uint32_t lifetime = 1U;
906 	int len = 64;
907 
908 	prefix = net_if_ipv6_prefix_add(TEST_NET_IF, &addr, len, lifetime);
909 	zassert_not_null(prefix, "Cannot get prefix");
910 
911 	net_if_ipv6_prefix_set_lf(prefix, false);
912 	net_if_ipv6_prefix_set_timer(prefix, lifetime);
913 
914 	k_sleep(K_SECONDS(lifetime * 2U));
915 
916 	prefix = net_if_ipv6_prefix_lookup(TEST_NET_IF, &addr, len);
917 	zassert_is_null(prefix, "Prefix %s/%d should have expired",
918 			net_sprint_ipv6_addr(&addr), len);
919 }
920 
ZTEST(net_ipv6,test_prefix_timeout_long)921 ZTEST(net_ipv6, test_prefix_timeout_long)
922 {
923 	struct net_if_ipv6_prefix *ifprefix;
924 	struct net_in6_addr prefix = { { { 0x20, 1, 0x0d, 0xb8, 43, 0, 0, 0,
925 				     0, 0, 0, 0, 0, 0, 0, 0 } } };
926 	uint32_t lifetime = 0xfffffffe;
927 	int len = 64;
928 	uint64_t remaining;
929 	int ret;
930 
931 	ifprefix = net_if_ipv6_prefix_add(TEST_NET_IF, &prefix, len, lifetime);
932 
933 	net_if_ipv6_prefix_set_lf(ifprefix, false);
934 	net_if_ipv6_prefix_set_timer(ifprefix, lifetime);
935 
936 	zassert_equal(ifprefix->lifetime.wrap_counter, 1999,
937 		      "Wrap counter wrong (%d)",
938 		      ifprefix->lifetime.wrap_counter);
939 	remaining = MSEC_PER_SEC * (uint64_t)lifetime -
940 		NET_TIMEOUT_MAX_VALUE * (uint64_t)ifprefix->lifetime.wrap_counter;
941 
942 	zassert_equal(remaining, ifprefix->lifetime.timer_timeout,
943 		     "Remaining time wrong (%llu vs %d)", remaining,
944 		      ifprefix->lifetime.timer_timeout);
945 
946 	ret = net_if_ipv6_prefix_rm(TEST_NET_IF, &prefix, len);
947 	zassert_equal(ret, true, "Prefix %s/%d should have been removed",
948 		      net_sprint_ipv6_addr(&prefix), len);
949 }
950 
rs_message(void)951 static void rs_message(void)
952 {
953 	struct net_if *iface;
954 	int ret;
955 
956 	iface = TEST_NET_IF;
957 
958 	expecting_ra = true;
959 
960 	ret = net_ipv6_send_rs(iface);
961 
962 	zassert_equal(ret, 0, "RS sending failed (%d)", ret);
963 
964 	k_yield();
965 }
966 
ra_message(void)967 static void ra_message(void)
968 {
969 	struct net_in6_addr route_prefix = { { { 0x20, 0x01, 0x0d, 0xb0, 0x0f, 0xff } } };
970 	struct net_sockaddr_in6 dns_addr = {
971 		.sin6_family = NET_AF_INET6,
972 		.sin6_port = net_htons(53),
973 		.sin6_addr = { { {  0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
974 				    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } } },
975 	};
976 	struct net_route_entry *route;
977 	struct dns_resolve_context *ctx;
978 	struct net_sockaddr_in6 *dns_server;
979 	struct net_if_addr *ifaddr;
980 
981 	/* We received RA message earlier, make sure that the information
982 	 * in that message is placed to proper prefix and lookup info.
983 	 */
984 
985 	expecting_ra = false;
986 
987 	zassert_false(!net_if_ipv6_prefix_lookup(TEST_NET_IF, &test_ra_prefix, 64),
988 		      "Prefix %s should be here\n",
989 		      net_sprint_ipv6_addr(&test_ra_prefix));
990 
991 	zassert_false(!net_if_ipv6_router_lookup(TEST_NET_IF, &test_router_addr),
992 		      "Router %s should be here\n",
993 		      net_sprint_ipv6_addr(&test_router_addr));
994 
995 	/* Check if autoconf address was added correctly. */
996 	ifaddr = net_if_ipv6_addr_lookup_by_iface(TEST_NET_IF,
997 						  &test_ra_autoconf_addr);
998 	zassert_not_null(ifaddr, "Autoconf address %s missing",
999 			 net_sprint_ipv6_addr(&test_ra_autoconf_addr));
1000 	zassert_equal(ifaddr->addr_type, NET_ADDR_AUTOCONF,
1001 		      "Address type should be autoconf");
1002 
1003 	/* Check if route was added correctly. */
1004 	route = net_route_lookup(TEST_NET_IF, &route_prefix);
1005 	zassert_not_null(route, "Route not found");
1006 	zassert_equal(route->prefix_len, 48, "Wrong prefix length set");
1007 	zassert_mem_equal(&route->addr, &route_prefix, sizeof(route_prefix),
1008 			  "Wrong prefix set");
1009 	zassert_true(route->is_infinite, "Wrong lifetime set");
1010 	zassert_equal(route->preference, NET_ROUTE_PREFERENCE_HIGH,
1011 		      "Wrong preference set");
1012 
1013 	/* Check if RDNSS was added correctly. */
1014 	ctx = dns_resolve_get_default();
1015 	zassert_equal(ctx->state, DNS_RESOLVE_CONTEXT_ACTIVE);
1016 	dns_server = (struct net_sockaddr_in6 *)&ctx->servers[0].dns_server;
1017 	zassert_equal(dns_server->sin6_family, dns_addr.sin6_family);
1018 	zassert_equal(dns_server->sin6_port, dns_addr.sin6_port);
1019 	zassert_mem_equal(&dns_server->sin6_addr, &dns_addr.sin6_addr,
1020 			  sizeof(dns_addr.sin6_addr), "Wrong DNS address set");
1021 	zassert_equal(dns_server->sin6_scope_id, dns_addr.sin6_scope_id);
1022 }
1023 
ZTEST(net_ipv6,test_rs_ra_message)1024 ZTEST(net_ipv6, test_rs_ra_message)
1025 {
1026 	rs_message();
1027 	/* Small delay to let the net stack process the generated RA message. */
1028 	k_sleep(K_MSEC(10));
1029 	ra_message();
1030 }
1031 
1032 struct test_dad_context {
1033 	struct k_sem wait_dad;
1034 	struct net_in6_addr *exp_dad_addr;
1035 	bool reply;
1036 };
1037 
expect_dad_ns(struct net_pkt * pkt,void * user_data)1038 static void expect_dad_ns(struct net_pkt *pkt, void *user_data)
1039 {
1040 	uint32_t res_bytes;
1041 	struct net_in6_addr target;
1042 	struct test_dad_context *ctx = user_data;
1043 
1044 	skip_headers(pkt);
1045 
1046 	zassert_ok(net_pkt_read_be32(pkt, &res_bytes), "Failed to read reserved bytes");
1047 	zassert_equal(0, res_bytes, "Reserved bytes must be zeroed");
1048 	zassert_ok(net_pkt_read(pkt, &target, sizeof(struct net_in6_addr)),
1049 		   "Failed to read target address");
1050 
1051 	if (net_ipv6_addr_cmp(ctx->exp_dad_addr, &target)) {
1052 		if (ctx->reply) {
1053 			inject_na_message(net_pkt_iface(pkt), &target,
1054 					  &all_nodes_mcast, &target, 0);
1055 		}
1056 
1057 		k_sem_give(&ctx->wait_dad);
1058 	}
1059 }
1060 
1061 /* Verify that RS is sent after interface state change, RA processed,
1062  * prefix added and autoconf address configured.
1063  */
verify_rs_on_iface_event(void (* action)(void))1064 static void verify_rs_on_iface_event(void (*action)(void))
1065 {
1066 	struct net_if_router *router;
1067 	static struct test_dad_context ctx = {
1068 		.exp_dad_addr = &test_ra_autoconf_addr
1069 	};
1070 	static struct test_ns_handler handler = {
1071 		.fn = expect_dad_ns,
1072 		.user_data = &ctx
1073 	};
1074 
1075 	(void)net_if_ipv6_prefix_rm(TEST_NET_IF, &test_ra_prefix, 64);
1076 
1077 	router = net_if_ipv6_router_lookup(TEST_NET_IF, &test_router_addr);
1078 	if (router) {
1079 		(void)net_if_ipv6_router_rm(router);
1080 	}
1081 
1082 	k_sem_init(&ctx.wait_dad, 0, 1);
1083 
1084 	ns_handler = &handler;
1085 	expecting_ra = true;
1086 
1087 	action();
1088 
1089 	k_sleep(K_MSEC(10));
1090 
1091 	ra_message();
1092 
1093 	zassert_ok(k_sem_take(&ctx.wait_dad, K_MSEC(WAIT_TIME)),
1094 		   "Timeout while waiting for DAD NS");
1095 }
1096 
ZTEST(net_ipv6,test_rs_after_iface_up)1097 ZTEST(net_ipv6, test_rs_after_iface_up)
1098 {
1099 	verify_rs_on_iface_event(test_iface_down_up);
1100 }
1101 
ZTEST(net_ipv6,test_rs_after_iface_up_carrier_delayed)1102 ZTEST(net_ipv6, test_rs_after_iface_up_carrier_delayed)
1103 {
1104 	verify_rs_on_iface_event(test_iface_down_up_delayed_carrier);
1105 }
1106 
ZTEST(net_ipv6,test_rs_after_carrier_toggle)1107 ZTEST(net_ipv6, test_rs_after_carrier_toggle)
1108 {
1109 	verify_rs_on_iface_event(test_iface_carrier_off_on);
1110 }
1111 
1112 /**
1113  * @brief IPv6 parse Hop-By-Hop Option
1114  */
ZTEST(net_ipv6,test_hbho_message)1115 ZTEST(net_ipv6, test_hbho_message)
1116 {
1117 	struct net_pkt *pkt;
1118 	struct net_if *iface;
1119 
1120 	iface = TEST_NET_IF;
1121 
1122 	pkt = net_pkt_alloc_with_buffer(iface, sizeof(ipv6_hbho),
1123 					NET_AF_UNSPEC, 0, K_FOREVER);
1124 
1125 	NET_ASSERT(pkt, "Out of TX packets");
1126 
1127 	net_pkt_write(pkt, ipv6_hbho, sizeof(ipv6_hbho));
1128 	net_pkt_lladdr_clear(pkt);
1129 
1130 	zassert_false(net_recv_data(iface, pkt) < 0,
1131 		      "Data receive for HBHO failed.");
1132 }
1133 
1134 /* IPv6 hop-by-hop option in the message HBHO (72 Bytes) */
1135 static const unsigned char ipv6_hbho_1[] = {
1136 /* IPv6 header starts here */
1137 0x60, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x40,
1138 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1139 0x08, 0xc0, 0xde, 0xff, 0xfe, 0x9b, 0xb4, 0x47,
1140 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
1141 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1142 /* Hop-by-hop option starts here */
1143 0x11, 0x08,
1144 /* Padding */
1145 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1146 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1147 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1148 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1150 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1151 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1154 /* UDP header starts here (8 bytes) */
1155 0x4e, 0x20, 0x10, 0x92, 0x00, 0x30, 0xa1, 0xc5,
1156 /* User data starts here (40 bytes) */
1157 0x30, 0x26, 0x02, 0x01, 0x00, 0x04, 0x06, 0x70,
1158 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0, 0x19, 0x02,
1159 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00,
1160 0x30, 0x0e, 0x30, 0x0c, 0x06, 0x08, 0x2b, 0x06,
1161 0x01, 0x02, 0x01, 0x01, 0x05, 0x00, 0x05, 0x00 };
1162 
1163 /**
1164  * @brief IPv6 parse Hop-By-Hop Option
1165  */
ZTEST(net_ipv6,test_hbho_message_1)1166 ZTEST(net_ipv6, test_hbho_message_1)
1167 {
1168 	struct net_pkt *pkt;
1169 	struct net_if *iface;
1170 
1171 	iface = TEST_NET_IF;
1172 
1173 	pkt = net_pkt_alloc_with_buffer(iface, sizeof(ipv6_hbho_1),
1174 					NET_AF_UNSPEC, 0, K_FOREVER);
1175 
1176 	NET_ASSERT(pkt, "Out of TX packets");
1177 
1178 	net_pkt_write(pkt, ipv6_hbho_1, sizeof(ipv6_hbho_1));
1179 
1180 	net_pkt_lladdr_clear(pkt);
1181 
1182 	zassert_false(net_recv_data(iface, pkt) < 0,
1183 		      "Data receive for HBHO failed.");
1184 
1185 	/* Verify IPv6 Ext hdr length */
1186 	zassert_false(net_pkt_ipv6_ext_len(pkt) == 72U,
1187 		      "IPv6 mismatch ext hdr length");
1188 }
1189 
1190 /* IPv6 hop-by-hop option in the message HBHO (104 Bytes) */
1191 static const unsigned char ipv6_hbho_2[] = {
1192 /* IPv6 header starts here */
1193 0x60, 0x00, 0x00, 0x00, 0x00, 0x98, 0x00, 0x40,
1194 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1195 0x08, 0xc0, 0xde, 0xff, 0xfe, 0x9b, 0xb4, 0x47,
1196 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
1197 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1198 /* Hop-by-hop option starts here */
1199 0x11, 0x0c,
1200 /* padding */
1201 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1203 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1204 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1205 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1206 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1207 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1208 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1209 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1210 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1211 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1212 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1213 0x01, 0x04, 0x00, 0x00, 0x00, 0x00,
1214 /* udp header starts here (8 bytes) */
1215 0x4e, 0x20, 0x10, 0x92, 0x00, 0x30, 0xa1, 0xc5,
1216 /* User data starts here (40 bytes) */
1217 0x30, 0x26, 0x02, 0x01, 0x00, 0x04, 0x06, 0x70,
1218 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0, 0x19, 0x02,
1219 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00,
1220 0x30, 0x0e, 0x30, 0x0c, 0x06, 0x08, 0x2b, 0x06,
1221 0x01, 0x02, 0x01, 0x01, 0x05, 0x00, 0x05, 0x00 };
1222 
1223 /**
1224  * @brief IPv6 parse Hop-By-Hop Option
1225  */
ZTEST(net_ipv6,test_hbho_message_2)1226 ZTEST(net_ipv6, test_hbho_message_2)
1227 {
1228 	struct net_pkt *pkt;
1229 	struct net_if *iface;
1230 
1231 	iface = TEST_NET_IF;
1232 
1233 	pkt = net_pkt_alloc_with_buffer(iface, sizeof(ipv6_hbho_2),
1234 					NET_AF_UNSPEC, 0, K_FOREVER);
1235 
1236 	NET_ASSERT(pkt, "Out of TX packets");
1237 
1238 
1239 	net_pkt_write(pkt, ipv6_hbho_2, sizeof(ipv6_hbho_2));
1240 	net_pkt_lladdr_clear(pkt);
1241 
1242 	zassert_false(net_recv_data(iface, pkt) < 0,
1243 		      "Data receive for HBHO failed.");
1244 
1245 	/* Verify IPv6 Ext hdr length */
1246 	zassert_false(net_pkt_ipv6_ext_len(pkt) == 104U,
1247 		      "IPv6 mismatch ext hdr length");
1248 }
1249 
1250 /* IPv6 hop-by-hop option in the message HBHO (920 bytes) */
1251 static const unsigned char ipv6_hbho_3[] = {
1252 /* IPv6 header starts here */
1253 0x60, 0x00, 0x00, 0x00, 0x03, 0xc8, 0x00, 0x40,
1254 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1255 0x08, 0xc0, 0xde, 0xff, 0xfe, 0x9b, 0xb4, 0x47,
1256 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
1257 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1258 /* Hop-by-hop option starts here */
1259 0x11, 0x72,
1260 /* padding */
1261 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1262 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1263 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1264 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1265 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1266 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1267 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1268 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1269 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1270 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1271 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1272 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1273 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1274 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1275 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1276 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1277 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1278 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1279 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1280 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1281 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1282 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1283 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1284 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1285 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1286 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1287 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1288 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1289 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1290 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1291 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1292 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1293 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1294 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1295 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1296 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1297 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1298 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1299 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1300 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1301 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1302 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1303 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1304 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1305 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1306 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1307 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1308 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1309 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1310 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1311 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1312 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1313 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1314 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1315 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1316 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1317 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1318 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1319 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1320 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1321 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1322 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1323 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1324 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1325 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1326 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1327 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1328 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1329 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1330 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1331 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1332 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1333 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1334 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1335 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1336 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1337 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1338 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1339 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1340 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1341 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1342 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1343 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1345 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1346 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1347 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1348 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1349 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1350 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1351 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1352 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1353 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1354 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1355 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1356 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1357 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1358 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1359 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1360 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1361 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1362 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1363 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1364 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1365 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1366 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1367 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1368 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1369 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1370 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1371 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1372 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1373 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1374 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1375 0x01, 0x04, 0x00, 0x00, 0x00, 0x00,
1376 /* udp header starts here (8 bytes) */
1377 0x4e, 0x20, 0x10, 0x92, 0x00, 0x30, 0xa1, 0xc5,
1378 /* User data starts here (40 bytes) */
1379 0x30, 0x26, 0x02, 0x01, 0x00, 0x04, 0x06, 0x70,
1380 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0, 0x19, 0x02,
1381 0x01, 0x00, 0x02, 0x01, 0x00, 0x02, 0x01, 0x00,
1382 0x30, 0x0e, 0x30, 0x0c, 0x06, 0x08, 0x2b, 0x06,
1383 0x01, 0x02, 0x01, 0x01, 0x05, 0x00, 0x05, 0x00
1384 };
1385 
1386 /**
1387  * @brief IPv6 parse Hop-By-Hop Option
1388  */
ZTEST(net_ipv6,test_hbho_message_3)1389 ZTEST(net_ipv6, test_hbho_message_3)
1390 {
1391 	struct net_pkt *pkt;
1392 	struct net_if *iface;
1393 
1394 	iface = TEST_NET_IF;
1395 
1396 	pkt = net_pkt_alloc_with_buffer(iface, sizeof(ipv6_hbho_3),
1397 					NET_AF_UNSPEC, 0, K_FOREVER);
1398 
1399 	NET_ASSERT(pkt, "Out of TX packets");
1400 
1401 	net_pkt_write(pkt, ipv6_hbho_3, sizeof(ipv6_hbho_3));
1402 	net_pkt_lladdr_clear(pkt);
1403 
1404 	zassert_false(net_recv_data(iface, pkt) < 0,
1405 		      "Data receive for HBHO failed.");
1406 
1407 	/* Verify IPv6 Ext hdr length */
1408 	zassert_false(net_pkt_ipv6_ext_len(pkt) == 920U,
1409 		      "IPv6 mismatch ext hdr length");
1410 }
1411 
1412 #define FIFTY_DAYS (60 * 60 * 24 * 50)
1413 
1414 /* Implemented in subsys/net/ip/net_if.c */
1415 extern void net_address_lifetime_timeout(void);
1416 
ZTEST(net_ipv6,test_address_lifetime)1417 ZTEST(net_ipv6, test_address_lifetime)
1418 {
1419 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1420 				     0, 0, 0, 0, 0, 0, 0x20, 0x1 } } };
1421 	struct net_if *iface = TEST_NET_IF;
1422 	uint32_t vlifetime = 0xffff;
1423 	uint64_t timeout = (uint64_t)vlifetime * MSEC_PER_SEC;
1424 	struct net_if_addr *ifaddr;
1425 	uint64_t remaining;
1426 	bool ret;
1427 
1428 	ifaddr = net_if_ipv6_addr_add(iface, &addr, NET_ADDR_AUTOCONF,
1429 				      vlifetime);
1430 	zassert_not_null(ifaddr, "Address with lifetime cannot be added");
1431 
1432 	/* Make sure DAD gets some time to run */
1433 	k_sleep(K_MSEC(200));
1434 
1435 	/* Then check that the timeout values in net_if_addr are set correctly.
1436 	 * Start first with smaller timeout values.
1437 	 */
1438 	zassert_equal(ifaddr->lifetime.timer_timeout, timeout,
1439 		      "Timer timeout set wrong (%d vs %llu)",
1440 		      ifaddr->lifetime.timer_timeout, timeout);
1441 	zassert_equal(ifaddr->lifetime.wrap_counter, 0,
1442 		      "Wrap counter wrong (%d)", ifaddr->lifetime.wrap_counter);
1443 
1444 	/* Then update the lifetime and check that timeout values are correct
1445 	 */
1446 	vlifetime = FIFTY_DAYS;
1447 	net_if_ipv6_addr_update_lifetime(ifaddr, vlifetime);
1448 
1449 	zassert_equal(ifaddr->lifetime.wrap_counter, 2,
1450 		      "Wrap counter wrong (%d)", ifaddr->lifetime.wrap_counter);
1451 	remaining = MSEC_PER_SEC * (uint64_t)vlifetime -
1452 		NET_TIMEOUT_MAX_VALUE * (uint64_t)ifaddr->lifetime.wrap_counter;
1453 
1454 	zassert_equal(remaining, ifaddr->lifetime.timer_timeout,
1455 		     "Remaining time wrong (%llu vs %d)", remaining,
1456 		      ifaddr->lifetime.timer_timeout);
1457 
1458 	/* The address should not expire */
1459 	net_address_lifetime_timeout();
1460 
1461 	zassert_equal(ifaddr->lifetime.wrap_counter, 2,
1462 		      "Wrap counter wrong (%d)", ifaddr->lifetime.wrap_counter);
1463 
1464 	ifaddr->lifetime.timer_timeout = 10;
1465 	ifaddr->lifetime.timer_start = k_uptime_get_32() - 10;
1466 	ifaddr->lifetime.wrap_counter = 0;
1467 
1468 	net_address_lifetime_timeout();
1469 
1470 	/* The address should be expired now */
1471 	zassert_equal(ifaddr->lifetime.timer_timeout, 0,
1472 		      "Timer timeout set wrong (%u vs %u)",
1473 		      ifaddr->lifetime.timer_timeout, 0);
1474 	zassert_equal(ifaddr->lifetime.wrap_counter, 0,
1475 		      "Wrap counter wrong (%d)", ifaddr->lifetime.wrap_counter);
1476 
1477 	ret = net_if_ipv6_addr_rm(iface, &addr);
1478 	zassert_true(ret, "Address with lifetime cannot be removed");
1479 }
1480 
1481 /**
1482  * @brief IPv6 change ll address
1483  */
ZTEST(net_ipv6,test_change_ll_addr)1484 ZTEST(net_ipv6, test_change_ll_addr)
1485 {
1486 	static uint8_t new_mac[] = { 00, 01, 02, 03, 04, 05 };
1487 	struct net_linkaddr *ll;
1488 	struct net_linkaddr *ll_iface;
1489 	struct net_if *iface;
1490 	struct net_nbr *nbr;
1491 	uint32_t flags;
1492 	int ret;
1493 
1494 	iface = TEST_NET_IF;
1495 
1496 	flags = NET_ICMPV6_NA_FLAG_ROUTER |
1497 		NET_ICMPV6_NA_FLAG_OVERRIDE;
1498 
1499 	ret = net_ipv6_send_na(iface, &peer_addr, &all_nodes_mcast,
1500 			       &peer_addr, flags);
1501 	zassert_false(ret < 0, "Cannot send NA 1");
1502 
1503 	nbr = net_ipv6_nbr_lookup(iface, &peer_addr);
1504 	zassert_not_null(nbr, "Neighbor %s not found in cache\n",
1505 			 net_sprint_ipv6_addr(&peer_addr));
1506 	ll = net_nbr_get_lladdr(nbr->idx);
1507 
1508 	ll_iface = net_if_get_link_addr(iface);
1509 
1510 	zassert_true(memcmp(ll->addr, ll_iface->addr, ll->len) != 0,
1511 		     "Wrong link address 1");
1512 
1513 	/* As the net_ipv6_send_na() uses interface link address to
1514 	 * greate tllao, change the interface ll address here.
1515 	 */
1516 	memcpy(ll_iface->addr, new_mac, sizeof(new_mac));
1517 
1518 	ret = net_ipv6_send_na(iface, &peer_addr, &all_nodes_mcast,
1519 			       &peer_addr, flags);
1520 	zassert_false(ret < 0, "Cannot send NA 2");
1521 
1522 	nbr = net_ipv6_nbr_lookup(iface, &peer_addr);
1523 	zassert_not_null(nbr, "Neighbor %s not found in cache\n",
1524 			 net_sprint_ipv6_addr(&peer_addr));
1525 	ll = net_nbr_get_lladdr(nbr->idx);
1526 
1527 	zassert_true(memcmp(ll->addr, ll_iface->addr, ll->len) != 0,
1528 		     "Wrong link address 2");
1529 
1530 	memcpy(ll_iface->addr, net_test_data.mac_addr,
1531 	       sizeof(net_test_data.mac_addr));
1532 }
1533 
ZTEST(net_ipv6,test_dad_timeout)1534 ZTEST(net_ipv6, test_dad_timeout)
1535 {
1536 #if defined(CONFIG_NET_IPV6_DAD)
1537 	struct net_in6_addr addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1538 				      0, 0, 0, 0, 0, 0, 0x99, 0x1 } } };
1539 	struct net_in6_addr addr2 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1540 				      0, 0, 0, 0, 0, 0, 0x99, 0x2 } } };
1541 	struct net_in6_addr addr3 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1542 				      0, 0, 0, 0, 0, 0, 0x99, 0x3 } } };
1543 	struct net_if *iface = TEST_NET_IF;
1544 
1545 	struct net_if_addr *ifaddr;
1546 
1547 	dad_time[0] = dad_time[1] = dad_time[2] = 0U;
1548 
1549 	ifaddr = net_if_ipv6_addr_add(iface, &addr1, NET_ADDR_AUTOCONF, 0xffff);
1550 	zassert_not_null(ifaddr, "Address 1 cannot be added");
1551 
1552 	k_sleep(K_MSEC(10));
1553 
1554 	ifaddr = net_if_ipv6_addr_add(iface, &addr2, NET_ADDR_AUTOCONF, 0xffff);
1555 	zassert_not_null(ifaddr, "Address 2 cannot be added");
1556 
1557 	k_sleep(K_MSEC(10));
1558 
1559 	ifaddr = net_if_ipv6_addr_add(iface, &addr3, NET_ADDR_AUTOCONF, 0xffff);
1560 	zassert_not_null(ifaddr, "Address 3 cannot be added");
1561 
1562 	k_sleep(K_MSEC(200));
1563 
1564 	/* Check we have received three DAD queries */
1565 	zassert_true((dad_time[0] != 0U) && (dad_time[1] != 0U) &&
1566 			(dad_time[2] != 0U), "Did not get DAD reply");
1567 
1568 	net_if_ipv6_addr_rm(iface, &addr1);
1569 	net_if_ipv6_addr_rm(iface, &addr2);
1570 	net_if_ipv6_addr_rm(iface, &addr3);
1571 #endif
1572 }
1573 
1574 
1575 /* Verify that DAD NS is sent after interface state change, for static address
1576  * (added to the interface in ipv6_setup()).
1577  */
verify_dad_on_static_addr_on_iface_event(void (* action)(void))1578 static void verify_dad_on_static_addr_on_iface_event(void (*action)(void))
1579 {
1580 	static struct test_dad_context ctx = {
1581 		.exp_dad_addr = &my_addr
1582 	};
1583 
1584 	static struct test_ns_handler handler = {
1585 		.fn = expect_dad_ns,
1586 		.user_data = &ctx
1587 	};
1588 
1589 	k_sem_init(&ctx.wait_dad, 0, 1);
1590 
1591 	ns_handler = &handler;
1592 
1593 	action();
1594 
1595 	zassert_ok(k_sem_take(&ctx.wait_dad, K_MSEC(WAIT_TIME)),
1596 		   "Timeout while waiting for DAD NS");
1597 }
1598 
ZTEST(net_ipv6,test_dad_on_static_addr_after_iface_up)1599 ZTEST(net_ipv6, test_dad_on_static_addr_after_iface_up)
1600 {
1601 	verify_dad_on_static_addr_on_iface_event(test_iface_down_up);
1602 }
1603 
ZTEST(net_ipv6,test_dad_on_static_addr_after_iface_up_carrier_delayed)1604 ZTEST(net_ipv6, test_dad_on_static_addr_after_iface_up_carrier_delayed)
1605 {
1606 	verify_dad_on_static_addr_on_iface_event(test_iface_down_up_delayed_carrier);
1607 }
1608 
ZTEST(net_ipv6,test_dad_on_static_addr_after_carrier_toggle)1609 ZTEST(net_ipv6, test_dad_on_static_addr_after_carrier_toggle)
1610 {
1611 	verify_dad_on_static_addr_on_iface_event(test_iface_carrier_off_on);
1612 }
1613 
1614 /* Verify that DAD NS is sent after interface state change, for link-local
1615  * address.
1616  */
verify_dad_on_ll_addr_on_iface_event(void (* action)(void))1617 static void verify_dad_on_ll_addr_on_iface_event(void (*action)(void))
1618 {
1619 	static struct net_in6_addr link_local_addr;
1620 	static struct test_dad_context ctx = {
1621 		.exp_dad_addr = &link_local_addr
1622 	};
1623 	static struct test_ns_handler handler = {
1624 		.fn = expect_dad_ns,
1625 		.user_data = &ctx
1626 	};
1627 
1628 	net_ipv6_addr_create_iid(&link_local_addr,
1629 				 net_if_get_link_addr(TEST_NET_IF));
1630 	k_sem_init(&ctx.wait_dad, 0, 1);
1631 
1632 	ns_handler = &handler;
1633 
1634 	action();
1635 
1636 	zassert_ok(k_sem_take(&ctx.wait_dad, K_MSEC(WAIT_TIME)),
1637 		   "Timeout while waiting for DAD NS");
1638 }
1639 
ZTEST(net_ipv6,test_dad_on_ll_addr_after_iface_up)1640 ZTEST(net_ipv6, test_dad_on_ll_addr_after_iface_up)
1641 {
1642 	verify_dad_on_ll_addr_on_iface_event(test_iface_down_up);
1643 }
1644 
ZTEST(net_ipv6,test_dad_on_ll_addr_after_iface_up_carrier_delayed)1645 ZTEST(net_ipv6, test_dad_on_ll_addr_after_iface_up_carrier_delayed)
1646 {
1647 	verify_dad_on_ll_addr_on_iface_event(test_iface_down_up_delayed_carrier);
1648 }
1649 
ZTEST(net_ipv6,test_dad_on_ll_addr_after_carrier_toggle)1650 ZTEST(net_ipv6, test_dad_on_ll_addr_after_carrier_toggle)
1651 {
1652 	verify_dad_on_ll_addr_on_iface_event(test_iface_carrier_off_on);
1653 }
1654 
1655 /* Verify that in case of DAD conflict, address is not used on the interface. */
ZTEST(net_ipv6,test_dad_conflict)1656 ZTEST(net_ipv6, test_dad_conflict)
1657 {
1658 	static struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1659 				     0, 0, 0, 0, 0, 0, 0x99, 0x4 } } };
1660 	static struct test_dad_context ctx = {
1661 		.exp_dad_addr = &addr,
1662 		.reply = true
1663 	};
1664 	static struct test_ns_handler handler = {
1665 		.fn = expect_dad_ns,
1666 		.user_data = &ctx
1667 	};
1668 	struct net_if_addr *ifaddr;
1669 
1670 	k_sem_init(&ctx.wait_dad, 0, 1);
1671 
1672 	ns_handler = &handler;
1673 
1674 	ifaddr = net_if_ipv6_addr_add(TEST_NET_IF, &addr, NET_ADDR_AUTOCONF, 0xffff);
1675 	zassert_not_null(ifaddr, "Address cannot be added");
1676 
1677 	zassert_ok(k_sem_take(&ctx.wait_dad, K_MSEC(WAIT_TIME)),
1678 		   "Timeout while waiting for DAD NS");
1679 
1680 	/* Small delay to let the stack process NA response. */
1681 	k_sleep(K_MSEC(100));
1682 
1683 	ifaddr = net_if_ipv6_addr_lookup_by_iface(TEST_NET_IF, &addr);
1684 	zassert_is_null(ifaddr, "Address should not be present on the interface");
1685 }
1686 
1687 #define NET_UDP_HDR(pkt)  ((struct net_udp_hdr *)(net_udp_get_hdr(pkt, NULL)))
1688 
setup_ipv6_udp(struct net_if * iface,struct net_in6_addr * local_addr,struct net_in6_addr * remote_addr,uint16_t local_port,uint16_t remote_port)1689 static struct net_pkt *setup_ipv6_udp(struct net_if *iface,
1690 				      struct net_in6_addr *local_addr,
1691 				      struct net_in6_addr *remote_addr,
1692 				      uint16_t local_port,
1693 				      uint16_t remote_port)
1694 {
1695 	static const char payload[] = "foobar";
1696 	struct net_pkt *pkt;
1697 
1698 	pkt = net_pkt_alloc_with_buffer(iface, strlen(payload),
1699 					NET_AF_INET6, NET_IPPROTO_UDP, K_FOREVER);
1700 	if (!pkt) {
1701 		return NULL;
1702 	}
1703 
1704 	if (net_ipv6_create(pkt, local_addr, remote_addr)) {
1705 		printk("Cannot create IPv6  pkt %p", pkt);
1706 		zassert_true(0, "exiting");
1707 	}
1708 
1709 	if (net_udp_create(pkt, net_htons(local_port), net_htons(remote_port))) {
1710 		printk("Cannot create IPv6  pkt %p", pkt);
1711 		zassert_true(0, "exiting");
1712 	}
1713 
1714 	if (net_pkt_write(pkt, (uint8_t *)payload, strlen(payload))) {
1715 		printk("Cannot write IPv6 ext header pkt %p", pkt);
1716 		zassert_true(0, "exiting");
1717 	}
1718 
1719 	net_pkt_cursor_init(pkt);
1720 	net_ipv6_finalize(pkt, NET_IPPROTO_UDP);
1721 	net_pkt_cursor_init(pkt);
1722 
1723 	return pkt;
1724 }
1725 
recv_msg(struct net_in6_addr * src,struct net_in6_addr * dst)1726 static enum net_verdict recv_msg(struct net_in6_addr *src, struct net_in6_addr *dst)
1727 {
1728 	struct net_pkt *pkt;
1729 	struct net_if *iface;
1730 
1731 	iface = TEST_NET_IF;
1732 
1733 	pkt = setup_ipv6_udp(iface, src, dst, 4242, 4321);
1734 
1735 	/* We by-pass the normal packet receiving flow in this case in order
1736 	 * to simplify the testing.
1737 	 */
1738 	return net_ipv6_input(pkt);
1739 }
1740 
send_msg(struct net_in6_addr * src,struct net_in6_addr * dst)1741 static int send_msg(struct net_in6_addr *src, struct net_in6_addr *dst)
1742 {
1743 	struct net_pkt *pkt;
1744 	struct net_if *iface;
1745 
1746 	iface = TEST_NET_IF;
1747 
1748 	pkt = setup_ipv6_udp(iface, src, dst, 4242, 4321);
1749 
1750 	return net_send_data(pkt);
1751 }
1752 
ZTEST(net_ipv6,test_src_localaddr_recv)1753 ZTEST(net_ipv6, test_src_localaddr_recv)
1754 {
1755 	struct net_in6_addr localaddr = { { { 0, 0, 0, 0, 0, 0, 0, 0,
1756 					  0, 0, 0, 0, 0, 0, 0, 0x1 } } };
1757 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1758 				     0, 0, 0, 0, 0, 0, 0, 0x10 } } };
1759 	enum net_verdict verdict;
1760 
1761 	verdict = recv_msg(&localaddr, &addr);
1762 	zassert_equal(verdict, NET_DROP,
1763 		      "Local address packet was not dropped");
1764 }
1765 
ZTEST(net_ipv6,test_dst_localaddr_recv)1766 ZTEST(net_ipv6, test_dst_localaddr_recv)
1767 {
1768 	struct net_in6_addr localaddr = { { { 0, 0, 0, 0, 0, 0, 0, 0,
1769 					  0, 0, 0, 0, 0, 0, 0, 0x1 } } };
1770 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1771 				     0, 0, 0, 0, 0, 0, 0, 0x10 } } };
1772 	enum net_verdict verdict;
1773 
1774 	verdict = recv_msg(&addr, &localaddr);
1775 	zassert_equal(verdict, NET_DROP,
1776 		      "Local address packet was not dropped");
1777 }
1778 
ZTEST(net_ipv6,test_dst_iface_scope_mcast_recv)1779 ZTEST(net_ipv6, test_dst_iface_scope_mcast_recv)
1780 {
1781 	struct net_in6_addr mcast_iface = { { { 0xff, 0x01, 0, 0, 0, 0, 0, 0,
1782 					    0, 0, 0, 0, 0, 0, 0, 0 } } };
1783 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1784 				     0, 0, 0, 0, 0, 0, 0, 0x10 } } };
1785 	enum net_verdict verdict;
1786 
1787 	verdict = recv_msg(&addr, &mcast_iface);
1788 	zassert_equal(verdict, NET_DROP,
1789 		      "Interface scope multicast packet was not dropped");
1790 }
1791 
ZTEST(net_ipv6,test_dst_zero_scope_mcast_recv)1792 ZTEST(net_ipv6, test_dst_zero_scope_mcast_recv)
1793 {
1794 	struct net_in6_addr mcast_zero = { { { 0xff, 0x00, 0, 0, 0, 0, 0, 0,
1795 					   0, 0, 0, 0, 0, 0, 0, 0 } } };
1796 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1797 				     0, 0, 0, 0, 0, 0, 0, 0x10 } } };
1798 	enum net_verdict verdict;
1799 
1800 	verdict = recv_msg(&addr, &mcast_zero);
1801 	zassert_equal(verdict, NET_DROP,
1802 		      "Zero scope multicast packet was not dropped");
1803 }
1804 
ZTEST(net_ipv6,test_dst_site_scope_mcast_recv_drop)1805 ZTEST(net_ipv6, test_dst_site_scope_mcast_recv_drop)
1806 {
1807 	struct net_in6_addr mcast_site = { { { 0xff, 0x05, 0, 0, 0, 0, 0, 0,
1808 					   0, 0, 0, 0, 0, 0, 0, 0 } } };
1809 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1810 				     0, 0, 0, 0, 0, 0, 0, 0x10 } } };
1811 	enum net_verdict verdict;
1812 
1813 	verdict = recv_msg(&addr, &mcast_site);
1814 	zassert_equal(verdict, NET_DROP,
1815 		      "Site scope multicast packet was not dropped");
1816 }
1817 
net_ctx_create(struct net_context ** ctx)1818 static void net_ctx_create(struct net_context **ctx)
1819 {
1820 	int ret;
1821 
1822 	ret = net_context_get(NET_AF_INET6, NET_SOCK_DGRAM, NET_IPPROTO_UDP, ctx);
1823 	zassert_equal(ret, 0,
1824 		      "Context create IPv6 UDP test failed");
1825 }
1826 
net_ctx_bind_mcast(struct net_context * ctx,struct net_in6_addr * maddr)1827 static void net_ctx_bind_mcast(struct net_context *ctx, struct net_in6_addr *maddr)
1828 {
1829 	struct net_sockaddr_in6 addr = {
1830 		.sin6_family = NET_AF_INET6,
1831 		.sin6_port = net_htons(4321),
1832 		.sin6_addr = { { { 0 } } },
1833 	};
1834 	int ret;
1835 
1836 	net_ipaddr_copy(&addr.sin6_addr, maddr);
1837 
1838 	ret = net_context_bind(ctx, (struct net_sockaddr *)&addr,
1839 			       sizeof(struct net_sockaddr_in6));
1840 	zassert_equal(ret, 0, "Context bind test failed (%d)", ret);
1841 }
1842 
net_ctx_listen(struct net_context * ctx)1843 static void net_ctx_listen(struct net_context *ctx)
1844 {
1845 	zassert_true(net_context_listen(ctx, 0),
1846 		     "Context listen IPv6 UDP test failed");
1847 }
1848 
recv_cb(struct net_context * context,struct net_pkt * pkt,union net_ip_header * ip_hdr,union net_proto_header * proto_hdr,int status,void * user_data)1849 static void recv_cb(struct net_context *context,
1850 		    struct net_pkt *pkt,
1851 		    union net_ip_header *ip_hdr,
1852 		    union net_proto_header *proto_hdr,
1853 		    int status,
1854 		    void *user_data)
1855 {
1856 	ARG_UNUSED(context);
1857 	ARG_UNUSED(pkt);
1858 	ARG_UNUSED(ip_hdr);
1859 	ARG_UNUSED(proto_hdr);
1860 	ARG_UNUSED(status);
1861 	ARG_UNUSED(user_data);
1862 
1863 	recv_cb_called = true;
1864 
1865 	net_pkt_unref(pkt);
1866 
1867 	k_sem_give(&wait_data);
1868 }
1869 
net_ctx_recv(struct net_context * ctx)1870 static void net_ctx_recv(struct net_context *ctx)
1871 {
1872 	int ret;
1873 
1874 	ret = net_context_recv(ctx, recv_cb, K_NO_WAIT, NULL);
1875 	zassert_equal(ret, 0, "Context recv IPv6 UDP failed");
1876 }
1877 
join_group(struct net_in6_addr * mcast_addr)1878 static void join_group(struct net_in6_addr *mcast_addr)
1879 {
1880 	int ret;
1881 
1882 	ret = net_ipv6_mld_join(TEST_NET_IF, mcast_addr);
1883 	zassert_equal(ret, 0, "Cannot join IPv6 multicast group");
1884 }
1885 
leave_group(struct net_in6_addr * mcast_addr)1886 static void leave_group(struct net_in6_addr *mcast_addr)
1887 {
1888 	int ret;
1889 
1890 	ret = net_ipv6_mld_leave(TEST_NET_IF, mcast_addr);
1891 	zassert_equal(ret, 0, "Cannot leave IPv6 multicast group");
1892 }
1893 
ZTEST(net_ipv6,test_dst_site_scope_mcast_recv_ok)1894 ZTEST(net_ipv6, test_dst_site_scope_mcast_recv_ok)
1895 {
1896 	struct net_in6_addr mcast_all_dhcp = { { { 0xff, 0x05, 0, 0, 0, 0, 0, 0,
1897 					    0, 0, 0, 0x01, 0, 0, 0, 0x03 } } };
1898 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1899 				     0, 0, 0, 0, 0, 0, 0, 0x10 } } };
1900 	enum net_verdict verdict;
1901 	struct net_context *ctx;
1902 
1903 	/* The packet will be dropped unless we have a listener and joined the
1904 	 * group.
1905 	 */
1906 	join_group(&mcast_all_dhcp);
1907 
1908 	net_ctx_create(&ctx);
1909 	net_ctx_bind_mcast(ctx, &mcast_all_dhcp);
1910 	net_ctx_listen(ctx);
1911 	net_ctx_recv(ctx);
1912 
1913 	verdict = recv_msg(&addr, &mcast_all_dhcp);
1914 	zassert_equal(verdict, NET_OK,
1915 		      "All DHCP site scope multicast packet was dropped (%d)",
1916 		      verdict);
1917 
1918 	net_context_put(ctx);
1919 
1920 	leave_group(&mcast_all_dhcp);
1921 }
1922 
ZTEST(net_ipv6,test_dst_org_scope_mcast_recv)1923 ZTEST(net_ipv6, test_dst_org_scope_mcast_recv)
1924 {
1925 	struct net_in6_addr mcast_org = { { { 0xff, 0x08, 0, 0, 0, 0, 0, 0,
1926 					  0, 0, 0, 0, 0, 0, 0, 0 } } };
1927 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1928 				     0, 0, 0, 0, 0, 0, 0, 0x10 } } };
1929 	enum net_verdict verdict;
1930 
1931 	verdict = recv_msg(&addr, &mcast_org);
1932 	zassert_equal(verdict, NET_DROP,
1933 		      "Organisation scope multicast packet was not dropped");
1934 }
1935 
ZTEST(net_ipv6,test_dst_iface_scope_mcast_send)1936 ZTEST(net_ipv6, test_dst_iface_scope_mcast_send)
1937 {
1938 	struct net_in6_addr mcast_iface = { { { 0xff, 0x01, 0, 0, 0, 0, 0, 0,
1939 					    0, 0, 0, 0, 0, 0, 0, 0 } } };
1940 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
1941 				     0, 0, 0, 0, 0, 0, 0, 0x10 } } };
1942 	struct net_if_mcast_addr *maddr;
1943 	struct net_context *ctx;
1944 	int ret;
1945 
1946 	/* Note that there is no need to join the multicast group as the
1947 	 * interface local scope multicast address packet will not leave the
1948 	 * device. But we will still need to add proper multicast address to
1949 	 * the network interface.
1950 	 */
1951 	maddr = net_if_ipv6_maddr_add(TEST_NET_IF, &mcast_iface);
1952 	zassert_not_null(maddr, "Cannot add multicast address to interface");
1953 
1954 	net_ctx_create(&ctx);
1955 	net_ctx_bind_mcast(ctx, &mcast_iface);
1956 	net_ctx_listen(ctx);
1957 	net_ctx_recv(ctx);
1958 
1959 	ret = send_msg(&addr, &mcast_iface);
1960 	zassert_equal(ret, 0,
1961 		      "Interface local scope multicast packet was dropped (%d)",
1962 		      ret);
1963 
1964 	k_sem_take(&wait_data, K_MSEC(WAIT_TIME));
1965 
1966 	zassert_true(recv_cb_called, "No data received on time, "
1967 		     "IPv6 recv test failed");
1968 	recv_cb_called = false;
1969 
1970 	net_context_put(ctx);
1971 
1972 	net_if_ipv6_maddr_rm(TEST_NET_IF, &mcast_iface);
1973 }
1974 
ZTEST(net_ipv6,test_dst_unknown_group_mcast_recv)1975 ZTEST(net_ipv6, test_dst_unknown_group_mcast_recv)
1976 {
1977 	struct net_in6_addr mcast_unknown_group = {
1978 		{ { 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0x01, 0x02, 0x03, 0x04, 0x05,
1979 		    0x06, 0x07, 0x08 } }
1980 	};
1981 	struct net_in6_addr in6_addr_any = NET_IN6ADDR_ANY_INIT;
1982 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0,
1983 				     0, 0, 0, 0, 0, 0x10 } } };
1984 	struct net_context *ctx;
1985 	enum net_verdict verdict;
1986 
1987 	/* Create listening socket that is bound to all incoming traffic. */
1988 	net_ctx_create(&ctx);
1989 	net_ctx_bind_mcast(ctx, &in6_addr_any);
1990 	net_ctx_listen(ctx);
1991 	net_ctx_recv(ctx);
1992 
1993 	/* Don't join multicast group before receiving packet.
1994 	 * Expectation: packet should be dropped by receiving interface on IP
1995 	 * Layer and not be received in listening socket.
1996 	 */
1997 	verdict = recv_msg(&addr, &mcast_unknown_group);
1998 
1999 	zassert_equal(verdict, NET_DROP,
2000 		      "Packet sent to unknown multicast group was not dropped");
2001 
2002 	net_context_put(ctx);
2003 }
2004 
ZTEST(net_ipv6,test_y_dst_unjoined_group_mcast_recv)2005 ZTEST(net_ipv6, test_y_dst_unjoined_group_mcast_recv)
2006 {
2007 	struct net_in6_addr mcast_unjoined_group = {
2008 		{ { 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0x42, 0x42, 0x42, 0x42, 0x42,
2009 		    0x42, 0x42, 0x42 } }
2010 	};
2011 	struct net_in6_addr in6_addr_any = NET_IN6ADDR_ANY_INIT;
2012 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0,
2013 				     0, 0, 0, 0, 0, 0x10 } } };
2014 	struct net_if_mcast_addr *maddr;
2015 	struct net_context *ctx;
2016 	enum net_verdict verdict;
2017 
2018 	/* Create listening socket that is bound to all incoming traffic. */
2019 	net_ctx_create(&ctx);
2020 	net_ctx_bind_mcast(ctx, &in6_addr_any);
2021 	net_ctx_listen(ctx);
2022 	net_ctx_recv(ctx);
2023 
2024 	/* add multicast address to interface but do not join the group yet */
2025 	maddr = net_if_ipv6_maddr_add(TEST_NET_IF, &mcast_unjoined_group);
2026 
2027 	net_if_ipv6_maddr_leave(TEST_NET_IF, maddr);
2028 
2029 	/* receive multicast on interface that did not join the group yet.
2030 	 * Expectation: packet should be dropped by first interface on IP
2031 	 * Layer and not be received in listening socket.
2032 	 */
2033 	verdict = recv_msg(&addr, &mcast_unjoined_group);
2034 
2035 	zassert_equal(verdict, NET_DROP,
2036 		      "Packet sent to unjoined multicast group was not "
2037 		      "dropped.");
2038 
2039 	/* now join the multicast group and attempt to receive again */
2040 	net_if_ipv6_maddr_join(TEST_NET_IF, maddr);
2041 	verdict = recv_msg(&addr, &mcast_unjoined_group);
2042 
2043 	zassert_equal(verdict, NET_OK,
2044 		      "Packet sent to joined multicast group was not "
2045 		      "received.");
2046 
2047 	net_if_ipv6_maddr_rm(TEST_NET_IF, &mcast_unjoined_group);
2048 
2049 	net_context_put(ctx);
2050 }
2051 
ZTEST(net_ipv6,test_dst_is_other_iface_mcast_recv)2052 ZTEST(net_ipv6, test_dst_is_other_iface_mcast_recv)
2053 {
2054 	struct net_in6_addr mcast_iface2 = { { { 0xff, 0x02, 0, 0, 0, 0, 0, 0, 0x01,
2055 					     0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
2056 					     0x08 } } };
2057 	struct net_in6_addr in6_addr_any = NET_IN6ADDR_ANY_INIT;
2058 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0,
2059 				     0, 0, 0, 0, 0, 0x10 } } };
2060 	struct net_if *test_iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY));
2061 	struct net_if_mcast_addr *maddr;
2062 	struct net_context *ctx;
2063 	enum net_verdict verdict;
2064 
2065 	/* Create listening socket that is bound to all incoming traffic. */
2066 	net_ctx_create(&ctx);
2067 	net_ctx_bind_mcast(ctx, &in6_addr_any);
2068 	net_ctx_listen(ctx);
2069 	net_ctx_recv(ctx);
2070 
2071 	/* Join multicast group on second interface. */
2072 	maddr = net_if_ipv6_maddr_add(test_iface, &mcast_iface2);
2073 	zassert_not_null(maddr, "Cannot add multicast address to interface");
2074 	net_if_ipv6_maddr_join(test_iface, maddr);
2075 
2076 	/* Receive multicast on first interface that did not join the group.
2077 	 * Expectation: packet should be dropped by first interface on IP
2078 	 * Layer and not be received in listening socket.
2079 	 *
2080 	 * Furthermore, multicast scope is link-local thus it should not cross
2081 	 * interface boundaries.
2082 	 */
2083 	verdict = recv_msg(&addr, &mcast_iface2);
2084 
2085 	zassert_equal(verdict, NET_DROP,
2086 		      "Packet sent to multicast group joined by second "
2087 		      "interface not dropped");
2088 
2089 	net_if_ipv6_maddr_leave(test_iface, maddr);
2090 
2091 	net_if_ipv6_maddr_rm(test_iface, &mcast_iface2);
2092 
2093 	net_context_put(ctx);
2094 }
2095 
2096 /* Verify that after interface state change it's possible to transmit mcast
2097  * packets to theoretically joined groups.
2098  */
verify_iface_mcast_send_on_iface_event(void (* action)(void))2099 static void verify_iface_mcast_send_on_iface_event(void (*action)(void))
2100 {
2101 	enum net_verdict verdict;
2102 	struct net_context *ctx;
2103 	struct net_in6_addr solicited_node_mcast;
2104 
2105 	action();
2106 
2107 	/* All nodes */
2108 	net_ctx_create(&ctx);
2109 	net_ctx_bind_mcast(ctx, &all_nodes_mcast);
2110 	net_ctx_listen(ctx);
2111 	net_ctx_recv(ctx);
2112 
2113 	verdict = send_msg(&my_addr, &all_nodes_mcast);
2114 	zassert_equal(verdict, NET_OK,
2115 		      "All nodes multicast packet was dropped (%d)",
2116 		      verdict);
2117 
2118 	net_context_put(ctx);
2119 
2120 	/* Solicited node */
2121 	net_ipv6_addr_create_solicited_node(&my_addr, &solicited_node_mcast);
2122 
2123 	net_ctx_create(&ctx);
2124 	net_ctx_bind_mcast(ctx, &solicited_node_mcast);
2125 	net_ctx_listen(ctx);
2126 	net_ctx_recv(ctx);
2127 
2128 	verdict = send_msg(&my_addr, &solicited_node_mcast);
2129 	zassert_equal(verdict, NET_OK,
2130 		      "Solicited node multicast packet was dropped (%d)",
2131 		      verdict);
2132 
2133 	net_context_put(ctx);
2134 }
2135 
ZTEST(net_ipv6,test_iface_mcast_send_after_iface_up)2136 ZTEST(net_ipv6, test_iface_mcast_send_after_iface_up)
2137 {
2138 	verify_iface_mcast_send_on_iface_event(test_iface_down_up);
2139 }
2140 
ZTEST(net_ipv6,test_iface_mcast_send_after_iface_up_carrier_delayed)2141 ZTEST(net_ipv6, test_iface_mcast_send_after_iface_up_carrier_delayed)
2142 {
2143 	verify_iface_mcast_send_on_iface_event(test_iface_down_up_delayed_carrier);
2144 }
2145 
ZTEST(net_ipv6,test_iface_mcast_send_after_carrier_toggle)2146 ZTEST(net_ipv6, test_iface_mcast_send_after_carrier_toggle)
2147 {
2148 	verify_iface_mcast_send_on_iface_event(test_iface_carrier_off_on);
2149 }
2150 
2151 /* Verify that after interface state change it's possible to receive mcast
2152  * packets on theoretically joined groups.
2153  */
verify_iface_mcast_recv_on_iface_event(void (* action)(void))2154 static void verify_iface_mcast_recv_on_iface_event(void (*action)(void))
2155 {
2156 	enum net_verdict verdict;
2157 	struct net_context *ctx;
2158 	struct net_in6_addr solicited_node_mcast;
2159 
2160 	action();
2161 
2162 	k_sem_reset(&wait_data);
2163 
2164 	/* All nodes */
2165 	net_ctx_create(&ctx);
2166 	net_ctx_bind_mcast(ctx, &all_nodes_mcast);
2167 	net_ctx_listen(ctx);
2168 	net_ctx_recv(ctx);
2169 
2170 	verdict = recv_msg(&peer_addr, &all_nodes_mcast);
2171 	zassert_equal(verdict, NET_OK,
2172 		      "All nodes multicast packet was dropped (%d)",
2173 		      verdict);
2174 	zassert_ok(k_sem_take(&wait_data, K_MSEC(WAIT_TIME)),
2175 		   "Timeout while waiting for mcast packet");
2176 
2177 	net_context_put(ctx);
2178 
2179 	/* Solicited node */
2180 	net_ipv6_addr_create_solicited_node(&my_addr, &solicited_node_mcast);
2181 
2182 	net_ctx_create(&ctx);
2183 	net_ctx_bind_mcast(ctx, &solicited_node_mcast);
2184 	net_ctx_listen(ctx);
2185 	net_ctx_recv(ctx);
2186 
2187 	verdict = recv_msg(&peer_addr, &solicited_node_mcast);
2188 	zassert_equal(verdict, NET_OK,
2189 		      "Solicited node multicast packet was dropped (%d)",
2190 		      verdict);
2191 	zassert_ok(k_sem_take(&wait_data, K_MSEC(WAIT_TIME)),
2192 		   "Timeout while waiting for mcast packet");
2193 
2194 	net_context_put(ctx);
2195 }
2196 
ZTEST(net_ipv6,test_iface_mcast_recv_after_iface_up)2197 ZTEST(net_ipv6, test_iface_mcast_recv_after_iface_up)
2198 {
2199 	verify_iface_mcast_recv_on_iface_event(test_iface_down_up);
2200 }
2201 
ZTEST(net_ipv6,test_iface_mcast_recv_after_iface_up_carrier_delayed)2202 ZTEST(net_ipv6, test_iface_mcast_recv_after_iface_up_carrier_delayed)
2203 {
2204 	verify_iface_mcast_recv_on_iface_event(test_iface_down_up_delayed_carrier);
2205 }
2206 
ZTEST(net_ipv6,test_iface_mcast_recv_after_carrier_toggle)2207 ZTEST(net_ipv6, test_iface_mcast_recv_after_carrier_toggle)
2208 {
2209 	verify_iface_mcast_recv_on_iface_event(test_iface_carrier_off_on);
2210 }
2211 
ZTEST(net_ipv6,test_no_nd_flag)2212 ZTEST(net_ipv6, test_no_nd_flag)
2213 {
2214 	bool ret;
2215 	struct net_in6_addr addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
2216 				     0, 0, 0, 0, 0, 0, 0x99, 0x10 } } };
2217 	struct net_if *iface = TEST_NET_IF;
2218 	struct net_if_addr *ifaddr;
2219 
2220 	dad_time[0] = 0;
2221 
2222 	net_if_flag_set(iface, NET_IF_IPV6_NO_ND);
2223 
2224 	ifaddr = net_if_ipv6_addr_add(iface, &addr, NET_ADDR_AUTOCONF, 0xffff);
2225 	zassert_not_null(ifaddr, "Address cannot be added");
2226 
2227 	/* Let the network stack to proceed */
2228 	k_sleep(K_MSEC(10));
2229 
2230 	zassert_equal(dad_time[0], 0, "Received ND message when not expected");
2231 	zassert_equal(ifaddr->addr_state, NET_ADDR_PREFERRED,
2232 		      "Address should've been set to preferred");
2233 
2234 	ret = net_if_ipv6_addr_rm(iface, &addr);
2235 	zassert_true(ret, "Failed to remove address");
2236 
2237 	net_if_flag_clear(iface, NET_IF_IPV6_NO_ND);
2238 }
2239 
ZTEST(net_ipv6,test_nd_reachability_hint)2240 ZTEST(net_ipv6, test_nd_reachability_hint)
2241 {
2242 	struct net_nbr *nbr;
2243 
2244 	nbr = net_ipv6_nbr_lookup(TEST_NET_IF, &peer_addr);
2245 	zassert_not_null(nbr, "Neighbor %s not found in cache\n",
2246 			 net_sprint_ipv6_addr(&peer_addr));
2247 
2248 	/* Configure neighbor's state to STALE. */
2249 	net_ipv6_nbr_data(nbr)->state = NET_IPV6_NBR_STATE_STALE;
2250 
2251 	net_ipv6_nbr_reachability_hint(TEST_NET_IF, &peer_addr);
2252 	zassert_equal(net_ipv6_nbr_data(nbr)->state, NET_IPV6_NBR_STATE_REACHABLE);
2253 
2254 	/* Configure neighbor's state to PROBE. */
2255 	net_ipv6_nbr_data(nbr)->state = NET_IPV6_NBR_STATE_PROBE;
2256 
2257 	/* Additionally ensure that state is not changed for different interface ID. */
2258 	net_ipv6_nbr_reachability_hint(TEST_NET_IF + 1, &peer_addr);
2259 	zassert_equal(net_ipv6_nbr_data(nbr)->state, NET_IPV6_NBR_STATE_PROBE);
2260 
2261 	net_ipv6_nbr_reachability_hint(TEST_NET_IF, &peer_addr);
2262 	zassert_equal(net_ipv6_nbr_data(nbr)->state, NET_IPV6_NBR_STATE_REACHABLE);
2263 }
2264 
is_pe_address_found(struct net_if * iface,struct net_in6_addr * prefix)2265 static bool is_pe_address_found(struct net_if *iface, struct net_in6_addr *prefix)
2266 {
2267 	struct net_if_ipv6 *ipv6;
2268 
2269 	ipv6 = iface->config.ip.ipv6;
2270 
2271 	zassert_not_null(ipv6, "IPv6 configuration is wrong for iface %p",
2272 			 iface);
2273 
2274 	ARRAY_FOR_EACH(ipv6->unicast, i) {
2275 		if (!ipv6->unicast[i].is_used ||
2276 		    ipv6->unicast[i].address.family != NET_AF_INET6 ||
2277 		    !ipv6->unicast[i].is_temporary) {
2278 			continue;
2279 		}
2280 
2281 		if (net_ipv6_is_prefix(
2282 			    (uint8_t *)&ipv6->unicast[i].address.in6_addr,
2283 			    (uint8_t *)prefix, 64)) {
2284 			return true;
2285 		}
2286 	}
2287 
2288 	return false;
2289 }
2290 
get_pe_addresses(struct net_if * iface,struct net_in6_addr ** public_addr,struct net_in6_addr ** temp_addr)2291 static void get_pe_addresses(struct net_if *iface,
2292 			     struct net_in6_addr **public_addr,
2293 			     struct net_in6_addr **temp_addr)
2294 {
2295 	struct net_in6_addr prefix = { { { 0x3f, 0xfe, 0x05, 0x07, 0, 0, 0, 1,
2296 				       0, 0, 0, 0, 0, 0, 0, 0 } } };
2297 	struct net_if_ipv6 *ipv6;
2298 
2299 	ipv6 = iface->config.ip.ipv6;
2300 
2301 	zassert_not_null(ipv6, "IPv6 configuration is wrong for iface %p",
2302 			 iface);
2303 
2304 	ARRAY_FOR_EACH(ipv6->unicast, i) {
2305 		if (!ipv6->unicast[i].is_used ||
2306 		    ipv6->unicast[i].address.family != NET_AF_INET6) {
2307 			continue;
2308 		}
2309 
2310 		if (net_ipv6_is_prefix(
2311 			    (uint8_t *)&ipv6->unicast[i].address.in6_addr,
2312 			    (uint8_t *)&prefix, 64)) {
2313 			if (ipv6->unicast[i].is_temporary) {
2314 				*temp_addr =
2315 					&ipv6->unicast[i].address.in6_addr;
2316 			} else {
2317 				*public_addr =
2318 					&ipv6->unicast[i].address.in6_addr;
2319 			}
2320 		}
2321 	}
2322 }
2323 
2324 /* The privacy extension tests need to be run after the RA tests so name
2325  * the tests like this.
2326  */
ZTEST(net_ipv6,test_z_privacy_extension_01)2327 ZTEST(net_ipv6, test_z_privacy_extension_01)
2328 {
2329 	struct net_in6_addr prefix = { { { 0x3f, 0xfe, 0x05, 0x07, 0, 0, 0, 1,
2330 				       0, 0, 0, 0, 0, 0, 0, 0 } } };
2331 	struct net_if *iface = net_if_get_default();
2332 	bool found;
2333 
2334 	if (!IS_ENABLED(CONFIG_NET_IPV6_PE)) {
2335 		return;
2336 	}
2337 
2338 	zassert_true(iface->pe_enabled,
2339 		     "Privacy extension not enabled for iface %d",
2340 		     net_if_get_by_iface(iface));
2341 
2342 	if (IS_ENABLED(CONFIG_NET_IPV6_PE_PREFER_PUBLIC_ADDRESSES)) {
2343 		zassert_true(iface->pe_prefer_public,
2344 			     "Prefer public flag not set correctly for iface %d",
2345 			     net_if_get_by_iface(iface));
2346 	}
2347 
2348 	/* We received RA message earlier, make sure that temporary address
2349 	 * is created because of that message.
2350 	 */
2351 
2352 	found = is_pe_address_found(iface, &prefix);
2353 	zassert_true(found, "Temporary address not found for iface %d",
2354 		     net_if_get_by_iface(iface));
2355 }
2356 
ZTEST(net_ipv6,test_z_privacy_extension_02_filters)2357 ZTEST(net_ipv6, test_z_privacy_extension_02_filters)
2358 {
2359 	struct net_in6_addr prefix1 = { { { 0x3f, 0xfe, 0x05, 0x07, 0, 0, 0, 1,
2360 					0, 0, 0, 0, 0, 0, 0, 0 } } };
2361 	struct net_in6_addr prefix2 = { { { 0x3f, 0xfe, 0x04, 0x07, 0, 0, 0, 1,
2362 					0, 0, 0, 0, 0, 0, 0, 0 } } };
2363 	struct net_in6_addr prefix3 = { { { 0x3f, 0xfe, 0x03, 0x07, 0, 0, 0, 1,
2364 					0, 0, 0, 0, 0, 0, 0, 0 } } };
2365 	struct net_if *iface = net_if_get_default();
2366 	bool found;
2367 	int ret;
2368 
2369 	if (!IS_ENABLED(CONFIG_NET_IPV6_PE) || NET_IPV6_PE_FILTER_PREFIX_COUNT == 0) {
2370 		return;
2371 	}
2372 
2373 	/* First add denylist filters */
2374 	ret = net_ipv6_pe_add_filter(&prefix1, true);
2375 	zassert_equal(ret, 0, "Filter cannot be added (%d)", ret);
2376 
2377 	ret = net_ipv6_pe_add_filter(&prefix2, true);
2378 	zassert_equal(ret, 0, "Filter cannot be added (%d)", ret);
2379 
2380 	ret = net_ipv6_pe_add_filter(&prefix3, true);
2381 	zassert_true(ret < 0, "Filter could be added");
2382 
2383 	/* Then delete them */
2384 	ret = net_ipv6_pe_del_filter(&prefix1);
2385 	zassert_equal(ret, 0, "Filter cannot be deleted (%d)", ret);
2386 
2387 	ret = net_ipv6_pe_del_filter(&prefix2);
2388 	zassert_equal(ret, 0, "Filter cannot be deleted (%d)", ret);
2389 
2390 	ret = net_ipv6_pe_del_filter(&prefix2);
2391 	zassert_true(ret < 0, "Filter found (%d)", ret);
2392 
2393 	/* Then add allowlist filter */
2394 	ret = net_ipv6_pe_add_filter(&prefix1, false);
2395 	zassert_equal(ret, 0, "Filter cannot be added (%d)", ret);
2396 
2397 	/* Send RS again as we have now PE allowlist filter in place */
2398 	rs_message();
2399 
2400 	/* IP stack needs to process the packet */
2401 	k_sleep(K_MSEC(150));
2402 
2403 	found = is_pe_address_found(iface, &prefix1);
2404 	zassert_true(found, "Temporary address not found for iface %p", iface);
2405 
2406 	/* Then try with denylisted filter */
2407 	ret = net_ipv6_pe_del_filter(&prefix1);
2408 	zassert_equal(ret, 0, "Filter cannot be deleted (%d)", ret);
2409 
2410 	ret = net_ipv6_pe_add_filter(&prefix1, true);
2411 	zassert_equal(ret, 0, "Filter cannot be added (%d)", ret);
2412 
2413 	k_sleep(K_MSEC(10));
2414 
2415 	/* Send RS again as we have now PE denylist filter in place */
2416 	rs_message();
2417 
2418 	k_sleep(K_MSEC(150));
2419 
2420 	found = is_pe_address_found(iface, &prefix1);
2421 	zassert_false(found, "Temporary address found for iface %p", iface);
2422 
2423 	ret = net_ipv6_pe_del_filter(&prefix1);
2424 	zassert_equal(ret, 0, "Filter cannot be deleted (%d)", ret);
2425 
2426 	/* Add the temp address back for the next tests */
2427 	ret = net_ipv6_pe_add_filter(&prefix1, false);
2428 	zassert_equal(ret, 0, "Filter cannot be added (%d)", ret);
2429 
2430 	k_sleep(K_MSEC(50));
2431 
2432 	/* Send RS again as we have now PE allowlist filter in place */
2433 	rs_message();
2434 
2435 	k_sleep(K_MSEC(150));
2436 
2437 	found = is_pe_address_found(iface, &prefix1);
2438 	zassert_true(found, "Temporary address not found for iface %p", iface);
2439 }
2440 
ZTEST(net_ipv6,test_z_privacy_extension_03_get_addr)2441 ZTEST(net_ipv6, test_z_privacy_extension_03_get_addr)
2442 {
2443 	struct net_in6_addr dst_addr = { { { 0x3f, 0xfe, 0x05, 0x07, 0, 0, 0, 1,
2444 				       0, 0, 2, 3, 4, 5, 6, 7 } } };
2445 	struct net_if *iface = net_if_get_default();
2446 	struct net_in6_addr *public_addr = NULL;
2447 	struct net_in6_addr *temp_addr = NULL;
2448 	const struct net_in6_addr *src_addr;
2449 
2450 	if (!IS_ENABLED(CONFIG_NET_IPV6_PE)) {
2451 		return;
2452 	}
2453 
2454 	get_pe_addresses(iface, &public_addr, &temp_addr);
2455 
2456 	zassert_not_null(public_addr, "No public address found");
2457 	zassert_not_null(temp_addr, "No temporary address found");
2458 
2459 	src_addr = net_if_ipv6_select_src_addr(iface, &dst_addr);
2460 	zassert_not_null(src_addr, "No suitable source address found");
2461 
2462 	if (iface->pe_prefer_public) {
2463 		zassert_true(net_ipv6_addr_cmp(src_addr, public_addr),
2464 			     "Non public address selected");
2465 	} else {
2466 		zassert_true(net_ipv6_addr_cmp(src_addr, temp_addr),
2467 			     "Non temporary address selected");
2468 	}
2469 }
2470 
2471 ZTEST_SUITE(net_ipv6, NULL, ipv6_setup, ipv6_before, NULL, ipv6_teardown);
2472