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