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