1 /* main.c - Application main entry point */
2
3 /*
4 * Copyright (c) 2016 Intel Corporation
5 * Copyright (c) 2023 Nordic Semiconductor ASA
6 *
7 * SPDX-License-Identifier: Apache-2.0
8 */
9
10 #define NET_LOG_LEVEL CONFIG_NET_IF_LOG_LEVEL
11
12 #include <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(net_test, NET_LOG_LEVEL);
14
15 #include <zephyr/types.h>
16 #include <stdbool.h>
17 #include <stddef.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <zephyr/sys/printk.h>
21 #include <zephyr/linker/sections.h>
22 #include <zephyr/random/random.h>
23
24 #include <zephyr/ztest.h>
25
26 #include <zephyr/net/ethernet.h>
27 #include <zephyr/net/dummy.h>
28 #include <zephyr/net_buf.h>
29 #include <zephyr/net/net_ip.h>
30 #include <zephyr/net/net_if.h>
31
32 #define NET_LOG_ENABLED 1
33 #include "net_private.h"
34
35 #if defined(CONFIG_NET_IF_LOG_LEVEL_DBG)
36 #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
37 #else
38 #define DBG(fmt, ...)
39 #endif
40
41 /* Interface 1 addresses */
42 static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
43 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
44 static ZTEST_BMEM struct in_addr my_ipv4_addr1 = { { { 192, 0, 2, 1 } } };
45
46 /* Interface 2 addresses */
47 static struct in6_addr my_addr2 = { { { 0x20, 0x01, 0x0d, 0xb8, 2, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
49
50 /* Interface 3 addresses */
51 static struct in6_addr my_addr3 = { { { 0x20, 0x01, 0x0d, 0xb8, 3, 0, 0, 0,
52 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
53
54 /* Extra address is assigned to ll_addr */
55 static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
56 0, 0, 0, 0xf2, 0xaa, 0x29, 0x02,
57 0x04 } } };
58
59 static struct in_addr inaddr_mcast = { { { 224, 0, 0, 1 } } };
60 static struct in6_addr in6addr_mcast;
61
62 static struct net_if *iface1;
63 static struct net_if *iface2;
64 static struct net_if *iface3;
65 static struct net_if *iface4;
66 static struct net_if *eth_iface;
67
68 static bool test_failed;
69 static bool test_started;
70 static struct k_sem wait_data;
71 static bool device_ok;
72
73 #define WAIT_TIME 250
74
75 struct net_if_test {
76 uint8_t idx;
77 uint8_t mac_addr[sizeof(struct net_eth_addr)];
78 struct net_linkaddr ll_addr;
79 };
80
net_iface_get_mac(const struct device * dev)81 static uint8_t *net_iface_get_mac(const struct device *dev)
82 {
83 struct net_if_test *data = dev->data;
84
85 if (data->mac_addr[2] == 0x00) {
86 /* 00-00-5E-00-53-xx Documentation RFC 7042 */
87 data->mac_addr[0] = 0x00;
88 data->mac_addr[1] = 0x00;
89 data->mac_addr[2] = 0x5E;
90 data->mac_addr[3] = 0x00;
91 data->mac_addr[4] = 0x53;
92 data->mac_addr[5] = sys_rand8_get();
93 }
94
95 data->ll_addr.addr = data->mac_addr;
96 data->ll_addr.len = 6U;
97
98 return data->mac_addr;
99 }
100
net_iface_init(struct net_if * iface)101 static void net_iface_init(struct net_if *iface)
102 {
103 uint8_t *mac = net_iface_get_mac(net_if_get_device(iface));
104
105 net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr),
106 NET_LINK_ETHERNET);
107 }
108
dev_init(const struct device * dev)109 static int dev_init(const struct device *dev)
110 {
111 if (device_ok == false) {
112 return -EAGAIN;
113 }
114
115 return 0;
116 }
117
sender_iface(const struct device * dev,struct net_pkt * pkt)118 static int sender_iface(const struct device *dev, struct net_pkt *pkt)
119 {
120 if (!pkt->buffer) {
121 DBG("No data to send!\n");
122 return -ENODATA;
123 }
124
125 if (test_started) {
126 struct net_if_test *data = dev->data;
127
128 DBG("Sending at iface %d %p\n",
129 net_if_get_by_iface(net_pkt_iface(pkt)),
130 net_pkt_iface(pkt));
131
132 if (net_if_get_by_iface(net_pkt_iface(pkt)) != data->idx) {
133 DBG("Invalid interface %d index, expecting %d\n",
134 data->idx, net_if_get_by_iface(net_pkt_iface(pkt)));
135 test_failed = true;
136 }
137 }
138
139 k_sem_give(&wait_data);
140
141 return 0;
142 }
143
144 struct net_if_test net_iface1_data;
145 struct net_if_test net_iface2_data;
146 struct net_if_test net_iface3_data;
147
148 static struct dummy_api net_iface_api = {
149 .iface_api.init = net_iface_init,
150 .send = sender_iface,
151 };
152
153 #define _ETH_L2_LAYER DUMMY_L2
154 #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2)
155
156 NET_DEVICE_INIT_INSTANCE(net_iface1_test,
157 "iface1",
158 iface1,
159 dev_init,
160 NULL,
161 &net_iface1_data,
162 NULL,
163 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
164 &net_iface_api,
165 _ETH_L2_LAYER,
166 _ETH_L2_CTX_TYPE,
167 127);
168
169 NET_DEVICE_INIT_INSTANCE(net_iface2_test,
170 "iface2",
171 iface2,
172 NULL,
173 NULL,
174 &net_iface2_data,
175 NULL,
176 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
177 &net_iface_api,
178 _ETH_L2_LAYER,
179 _ETH_L2_CTX_TYPE,
180 127);
181
182 NET_DEVICE_INIT_INSTANCE(net_iface3_test,
183 "iface3",
184 iface3,
185 NULL,
186 NULL,
187 &net_iface3_data,
188 NULL,
189 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
190 &net_iface_api,
191 _ETH_L2_LAYER,
192 _ETH_L2_CTX_TYPE,
193 127);
194
195 struct eth_fake_context {
196 struct net_if *iface;
197 uint8_t mac_address[6];
198 bool promisc_mode;
199 };
200
201 static struct eth_fake_context eth_fake_data;
202
eth_fake_iface_init(struct net_if * iface)203 static void eth_fake_iface_init(struct net_if *iface)
204 {
205 const struct device *dev = net_if_get_device(iface);
206 struct eth_fake_context *ctx = dev->data;
207
208 ctx->iface = iface;
209
210 net_if_set_link_addr(iface, ctx->mac_address,
211 sizeof(ctx->mac_address),
212 NET_LINK_ETHERNET);
213
214 ethernet_init(iface);
215 }
216
eth_fake_send(const struct device * dev,struct net_pkt * pkt)217 static int eth_fake_send(const struct device *dev,
218 struct net_pkt *pkt)
219 {
220 ARG_UNUSED(dev);
221 ARG_UNUSED(pkt);
222
223 return 0;
224 }
225
eth_fake_get_capabilities(const struct device * dev)226 static enum ethernet_hw_caps eth_fake_get_capabilities(const struct device *dev)
227 {
228 return ETHERNET_PROMISC_MODE;
229 }
230
eth_fake_set_config(const struct device * dev,enum ethernet_config_type type,const struct ethernet_config * config)231 static int eth_fake_set_config(const struct device *dev,
232 enum ethernet_config_type type,
233 const struct ethernet_config *config)
234 {
235 struct eth_fake_context *ctx = dev->data;
236
237 switch (type) {
238 case ETHERNET_CONFIG_TYPE_PROMISC_MODE:
239 if (config->promisc_mode == ctx->promisc_mode) {
240 return -EALREADY;
241 }
242
243 ctx->promisc_mode = config->promisc_mode;
244
245 break;
246
247 default:
248 return -EINVAL;
249 }
250
251 return 0;
252 }
253
254 static struct ethernet_api eth_fake_api_funcs = {
255 .iface_api.init = eth_fake_iface_init,
256
257 .get_capabilities = eth_fake_get_capabilities,
258 .set_config = eth_fake_set_config,
259 .send = eth_fake_send,
260 };
261
eth_fake_init(const struct device * dev)262 static int eth_fake_init(const struct device *dev)
263 {
264 struct eth_fake_context *ctx = dev->data;
265
266 ctx->promisc_mode = false;
267
268 return 0;
269 }
270
271 ETH_NET_DEVICE_INIT(eth_fake, "eth_fake", eth_fake_init, NULL,
272 ð_fake_data, NULL, CONFIG_ETH_INIT_PRIORITY,
273 ð_fake_api_funcs, NET_ETH_MTU);
274
275 #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
iface2str(struct net_if * iface)276 static const char *iface2str(struct net_if *iface)
277 {
278 if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
279 return "Ethernet";
280 }
281
282 if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
283 return "Dummy";
284 }
285
286 return "<unknown type>";
287 }
288 #endif
289
iface_cb(struct net_if * iface,void * user_data)290 static void iface_cb(struct net_if *iface, void *user_data)
291 {
292 static int if_count;
293
294 DBG("Interface %p (%s) [%d]\n", iface, iface2str(iface),
295 net_if_get_by_iface(iface));
296
297 if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
298 const struct ethernet_api *api =
299 net_if_get_device(iface)->api;
300
301 /* As native_sim board will introduce another ethernet
302 * interface, make sure that we only use our own in this test.
303 */
304 if (api->get_capabilities ==
305 eth_fake_api_funcs.get_capabilities) {
306 iface4 = iface;
307 eth_iface = iface;
308 }
309 } else {
310 switch (if_count) {
311 case 0:
312 iface1 = iface;
313 break;
314 case 1:
315 iface2 = iface;
316 break;
317 case 2:
318 iface3 = iface;
319 break;
320 }
321
322 if_count++;
323 }
324 }
325
iface_setup(void)326 static void *iface_setup(void)
327 {
328 struct net_if_mcast_addr *maddr;
329 struct net_if_addr *ifaddr;
330 const struct device *dev;
331 bool status;
332 int idx, ret;
333
334 /* The semaphore is there to wait the data to be received. */
335 k_sem_init(&wait_data, 0, UINT_MAX);
336
337 net_if_foreach(iface_cb, NULL);
338
339 idx = net_if_get_by_iface(iface1);
340 ((struct net_if_test *)
341 net_if_get_device(iface1)->data)->idx = idx;
342
343 idx = net_if_get_by_iface(iface2);
344 ((struct net_if_test *)
345 net_if_get_device(iface2)->data)->idx = idx;
346
347 idx = net_if_get_by_iface(iface3);
348 ((struct net_if_test *)
349 net_if_get_device(iface3)->data)->idx = idx;
350
351 DBG("Interfaces: [%d] iface1 %p, [%d] iface2 %p, [%d] iface3 %p\n",
352 net_if_get_by_iface(iface1), iface1,
353 net_if_get_by_iface(iface2), iface2,
354 net_if_get_by_iface(iface3), iface3);
355
356 zassert_not_null(iface1, "Interface 1");
357 zassert_not_null(iface2, "Interface 2");
358 zassert_not_null(iface3, "Interface 3");
359
360 /* Make sure that the first interface device is not ready */
361 dev = net_if_get_device(iface1);
362 zassert_not_null(dev, "Device is not set!");
363
364 status = device_is_ready(dev);
365 zassert_equal(status, false, "Device %s (%p) is ready!",
366 dev->name, dev);
367
368 /* Trying to take the interface up will fail */
369 ret = net_if_up(iface1);
370 zassert_equal(ret, -ENXIO, "Interface 1 is up (%d)", ret);
371
372 /* Try to set dormant state */
373 net_if_dormant_on(iface1);
374
375 /* Operational state should be "oper down" */
376 zassert_equal(iface1->if_dev->oper_state, NET_IF_OPER_DOWN,
377 "Invalid operational state (%d)",
378 iface1->if_dev->oper_state);
379
380 /* Mark the device ready and take the interface up */
381 dev->state->init_res = 0;
382 device_ok = true;
383
384 ret = net_if_up(iface1);
385 zassert_equal(ret, 0, "Interface 1 is not up (%d)", ret);
386
387 zassert_equal(iface1->if_dev->oper_state, NET_IF_OPER_DORMANT,
388 "Invalid operational state (%d)",
389 iface1->if_dev->oper_state);
390
391 net_if_dormant_off(iface1);
392 zassert_equal(iface1->if_dev->oper_state, NET_IF_OPER_UP,
393 "Invalid operational state (%d)",
394 iface1->if_dev->oper_state);
395
396 ifaddr = net_if_ipv6_addr_add(iface1, &my_addr1,
397 NET_ADDR_MANUAL, 0);
398 if (!ifaddr) {
399 DBG("Cannot add IPv6 address %s\n",
400 net_sprint_ipv6_addr(&my_addr1));
401 zassert_not_null(ifaddr, "addr1");
402 }
403
404 ifaddr = net_if_ipv4_addr_add(iface1, &my_ipv4_addr1,
405 NET_ADDR_MANUAL, 0);
406 if (!ifaddr) {
407 DBG("Cannot add IPv4 address %s\n",
408 net_sprint_ipv4_addr(&my_ipv4_addr1));
409 zassert_not_null(ifaddr, "ipv4 addr1");
410 }
411
412 /* For testing purposes we need to set the addresses preferred */
413 ifaddr->addr_state = NET_ADDR_PREFERRED;
414
415 ifaddr = net_if_ipv6_addr_add(iface1, &ll_addr,
416 NET_ADDR_MANUAL, 0);
417 if (!ifaddr) {
418 DBG("Cannot add IPv6 address %s\n",
419 net_sprint_ipv6_addr(&ll_addr));
420 zassert_not_null(ifaddr, "ll_addr");
421 }
422
423 ifaddr->addr_state = NET_ADDR_PREFERRED;
424
425 ifaddr = net_if_ipv6_addr_add(iface2, &my_addr2,
426 NET_ADDR_MANUAL, 0);
427 if (!ifaddr) {
428 DBG("Cannot add IPv6 address %s\n",
429 net_sprint_ipv6_addr(&my_addr2));
430 zassert_not_null(ifaddr, "addr2");
431 }
432
433 ifaddr->addr_state = NET_ADDR_PREFERRED;
434
435 ifaddr = net_if_ipv6_addr_add(iface2, &my_addr3,
436 NET_ADDR_MANUAL, 0);
437 if (!ifaddr) {
438 DBG("Cannot add IPv6 address %s\n",
439 net_sprint_ipv6_addr(&my_addr3));
440 zassert_not_null(ifaddr, "addr3");
441 }
442
443 ifaddr->addr_state = NET_ADDR_PREFERRED;
444
445 maddr = net_if_ipv4_maddr_add(iface1, &inaddr_mcast);
446 if (!maddr) {
447 DBG("Cannot add multicast IPv4 address %s\n",
448 net_sprint_ipv4_addr(&inaddr_mcast));
449 zassert_not_null(maddr, "mcast");
450 }
451
452 net_ipv6_addr_create(&in6addr_mcast, 0xff02, 0, 0, 0, 0, 0, 0, 0x0001);
453
454 maddr = net_if_ipv6_maddr_add(iface1, &in6addr_mcast);
455 if (!maddr) {
456 DBG("Cannot add multicast IPv6 address %s\n",
457 net_sprint_ipv6_addr(&in6addr_mcast));
458 zassert_not_null(maddr, "mcast");
459 }
460
461 net_if_up(iface1);
462 net_if_up(iface2);
463 net_if_up(iface3);
464 net_if_up(iface4);
465
466 /* The interface might receive data which might fail the checks
467 * in the iface sending function, so we need to reset the failure
468 * flag.
469 */
470 test_failed = false;
471
472 test_started = true;
473
474 return NULL;
475 }
476
iface_teardown(void * dummy)477 static void iface_teardown(void *dummy)
478 {
479 ARG_UNUSED(dummy);
480 net_if_ipv6_addr_rm(iface1, &my_addr1);
481 net_if_ipv6_addr_rm(iface1, &ll_addr);
482 net_if_ipv6_addr_rm(iface2, &my_addr2);
483 net_if_ipv6_addr_rm(iface2, &my_addr3);
484 net_if_ipv6_maddr_rm(iface1, &in6addr_mcast);
485 net_if_down(iface1);
486 net_if_down(iface2);
487 net_if_down(iface3);
488 net_if_down(iface4);
489 }
490
test_iface_init(struct net_if * iface,bool carrier,bool dormant)491 static void test_iface_init(struct net_if *iface, bool carrier, bool dormant)
492 {
493 net_if_down(iface);
494
495 if (carrier) {
496 net_if_carrier_on(iface);
497 } else {
498 net_if_carrier_off(iface);
499 }
500
501 if (dormant) {
502 net_if_dormant_on(iface);
503 } else {
504 net_if_dormant_off(iface);
505 }
506
507 net_if_up(iface);
508 }
509
ZTEST(net_iface,test_oper_state)510 ZTEST(net_iface, test_oper_state)
511 {
512 /* Carrier OFF, Dormant OFF - interface should remain down */
513 test_iface_init(iface1, false, false);
514 zassert_false(net_if_is_up(iface1), "Interface should be down");
515 zassert_equal(net_if_oper_state(iface1), NET_IF_OPER_DOWN,
516 "Wrong operational state");
517
518 /* Carrier ON transition - interface should go up */
519 net_if_carrier_on(iface1);
520 zassert_true(net_if_is_up(iface1), "Interface should be up");
521 zassert_equal(net_if_oper_state(iface1), NET_IF_OPER_UP,
522 "Wrong operational state");
523
524 /* Carrier ON, Dormant ON - interface should remain down */
525 test_iface_init(iface1, true, true);
526 zassert_false(net_if_is_up(iface1), "Interface should be down");
527 zassert_equal(net_if_oper_state(iface1), NET_IF_OPER_DORMANT,
528 "Wrong operational state");
529
530 /* Dormant OFF transition - interface should go up */
531 net_if_dormant_off(iface1);
532 zassert_true(net_if_is_up(iface1), "Interface should be up");
533 zassert_equal(net_if_oper_state(iface1), NET_IF_OPER_UP,
534 "Wrong operational state");
535
536 /* Carrier ON, Dormant OFF - interface should go up right away */
537 test_iface_init(iface1, true, false);
538 zassert_true(net_if_is_up(iface1), "Interface should be up");
539 zassert_equal(net_if_oper_state(iface1), NET_IF_OPER_UP,
540 "Wrong operational state");
541
542 /* Carrier OFF transition - interface should go down */
543 net_if_carrier_off(iface1);
544 zassert_false(net_if_is_up(iface1), "Interface should be down");
545 zassert_equal(net_if_oper_state(iface1), NET_IF_OPER_DOWN,
546 "Wrong operational state");
547
548 /* Carrier ON, Dormant OFF - interface should go up right away */
549 test_iface_init(iface1, true, false);
550 zassert_true(net_if_is_up(iface1), "Interface should be up");
551 zassert_equal(net_if_oper_state(iface1), NET_IF_OPER_UP,
552 "Wrong operational state");
553
554 /* Dormant ON transition - interface should go down */
555 net_if_dormant_on(iface1);
556 zassert_false(net_if_is_up(iface1), "Interface should be down");
557 zassert_equal(net_if_oper_state(iface1), NET_IF_OPER_DORMANT,
558 "Wrong operational state");
559
560 /* Carrier ON, Dormant OFF - interface should go up right away */
561 test_iface_init(iface1, true, false);
562 zassert_true(net_if_is_up(iface1), "Interface should be up");
563 zassert_equal(net_if_oper_state(iface1), NET_IF_OPER_UP,
564 "Wrong operational state");
565
566 /* Admin down transition - interface should go down */
567 net_if_down(iface1);
568 zassert_false(net_if_is_up(iface1), "Interface should be down");
569 zassert_equal(net_if_oper_state(iface1), NET_IF_OPER_DOWN,
570 "Wrong operational state");
571
572 /* Bring the interface back up */
573 net_if_up(iface1);
574 zassert_true(net_if_is_up(iface1), "Interface should be up");
575 }
576
send_iface(struct net_if * iface,int val,bool expect_fail)577 static bool send_iface(struct net_if *iface, int val, bool expect_fail)
578 {
579 static uint8_t data[] = { 't', 'e', 's', 't', '\0' };
580 struct net_pkt *pkt;
581 int ret;
582
583 pkt = net_pkt_alloc_with_buffer(iface, sizeof(data),
584 AF_UNSPEC, 0, K_FOREVER);
585 if (!pkt) {
586 DBG("Cannot allocate pkt\n");
587 return false;
588 }
589
590 net_pkt_write(pkt, data, sizeof(data));
591 net_pkt_cursor_init(pkt);
592
593 ret = net_send_data(pkt);
594 if (!expect_fail && ret < 0) {
595 DBG("Cannot send test packet (%d)\n", ret);
596 return false;
597 }
598
599 if (!expect_fail && k_sem_take(&wait_data, K_MSEC(WAIT_TIME))) {
600 DBG("Timeout while waiting interface %d data\n", val);
601 return false;
602 }
603
604 return true;
605 }
606
ZTEST(net_iface,test_send_iface1)607 ZTEST(net_iface, test_send_iface1)
608 {
609 bool ret;
610
611 DBG("Sending data to iface 1 %p\n", iface1);
612
613 ret = send_iface(iface1, 1, false);
614
615 zassert_true(ret, "iface 1");
616 }
617
ZTEST(net_iface,test_send_iface2)618 ZTEST(net_iface, test_send_iface2)
619 {
620 bool ret;
621
622 DBG("Sending data to iface 2 %p\n", iface2);
623
624 ret = send_iface(iface2, 2, false);
625
626 zassert_true(ret, "iface 2");
627 }
628
ZTEST(net_iface,test_send_iface3)629 ZTEST(net_iface, test_send_iface3)
630 {
631 bool ret;
632
633 DBG("Sending data to iface 3 %p\n", iface3);
634
635 ret = send_iface(iface3, 3, false);
636
637 zassert_true(ret, "iface 3");
638 }
639
send_iface1_down(void)640 static void send_iface1_down(void)
641 {
642 bool ret;
643
644 DBG("Sending data to iface 1 %p while down\n", iface1);
645
646 net_if_down(iface1);
647
648 ret = send_iface(iface1, 1, true);
649
650 zassert_true(ret, "iface 1 down");
651 }
652
send_iface1_up(void)653 static void send_iface1_up(void)
654 {
655 bool ret;
656
657 DBG("Sending data to iface 1 %p again\n", iface1);
658
659 net_if_up(iface1);
660
661 ret = send_iface(iface1, 1, false);
662
663 zassert_true(ret, "iface 1 up again");
664 }
665
ZTEST(net_iface,test_send_iface1_down_up)666 ZTEST(net_iface, test_send_iface1_down_up)
667 {
668 send_iface1_down();
669 send_iface1_up();
670 }
671
ZTEST(net_iface,test_select_src_iface)672 ZTEST(net_iface, test_select_src_iface)
673 {
674 struct in6_addr dst_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
675 0, 0, 0, 0, 0, 0, 0, 0x2 } } };
676 struct in6_addr ll_addr1 = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
677 0, 0, 0x09, 0x12, 0xaa, 0x29, 0x02,
678 0x88 } } };
679 struct in6_addr dst_addr3 = { { { 0x20, 0x01, 0x0d, 0xb8, 3, 0, 0, 0,
680 0, 0, 0, 0, 0, 0, 0, 0x99 } } };
681 struct in6_addr in6addr_mcast1 = { { { 0x00 } } };
682 struct in_addr dst_addr_2 = { { { 192, 0, 2, 2 } } };
683
684 struct net_if_addr *ifaddr;
685 struct net_if *iface;
686 struct sockaddr_in ipv4;
687 struct sockaddr_in6 ipv6;
688
689 iface = net_if_ipv6_select_src_iface(&dst_addr1);
690 zassert_equal_ptr(iface, iface1, "Invalid interface %p vs %p selected",
691 iface, iface1);
692
693 iface = net_if_ipv6_select_src_iface(&ll_addr1);
694 zassert_equal_ptr(iface, iface1, "Invalid interface %p vs %p selected",
695 iface, iface1);
696
697 net_ipv6_addr_create(&in6addr_mcast1, 0xff02, 0, 0, 0, 0, 0, 0, 0x0002);
698
699 iface = net_if_ipv6_select_src_iface(&in6addr_mcast1);
700 zassert_equal_ptr(iface, iface1, "Invalid interface %p vs %p selected",
701 iface, iface1);
702
703 iface = net_if_ipv6_select_src_iface(&dst_addr3);
704 zassert_equal_ptr(iface, iface2, "Invalid interface %p vs %p selected",
705 iface, iface2);
706
707 ifaddr = net_if_ipv6_addr_lookup(&ll_addr, NULL);
708 zassert_not_null(ifaddr, "No such ll_addr found");
709
710 ifaddr->addr_state = NET_ADDR_TENTATIVE;
711
712 /* We should now get default interface */
713 iface = net_if_ipv6_select_src_iface(&ll_addr1);
714 zassert_equal_ptr(iface, net_if_get_default(),
715 "Invalid interface %p vs %p selected",
716 iface, net_if_get_default());
717
718 net_ipaddr_copy(&ipv4.sin_addr, &dst_addr_2);
719 ipv4.sin_family = AF_INET;
720 ipv4.sin_port = 0U;
721
722 iface = net_if_select_src_iface((struct sockaddr *)&ipv4);
723 zassert_equal_ptr(iface, iface1, "Invalid interface %p vs %p selected",
724 iface, iface1);
725
726 net_ipaddr_copy(&ipv6.sin6_addr, &dst_addr1);
727 ipv6.sin6_family = AF_INET6;
728 ipv6.sin6_port = 0U;
729
730 iface = net_if_select_src_iface((struct sockaddr *)&ipv6);
731 zassert_equal_ptr(iface, iface1, "Invalid interface %p vs %p selected",
732 iface, iface1);
733 }
734
check_promisc_mode_off(void)735 static void check_promisc_mode_off(void)
736 {
737 bool ret;
738
739 DBG("Make sure promiscuous mode is OFF (%p)\n", iface4);
740
741 ret = net_if_is_promisc(iface4);
742
743 zassert_false(ret, "iface 1 promiscuous mode ON");
744 }
745
check_promisc_mode_on(void)746 static void check_promisc_mode_on(void)
747 {
748 bool ret;
749
750 DBG("Make sure promiscuous mode is ON (%p)\n", iface4);
751
752 ret = net_if_is_promisc(iface4);
753
754 zassert_true(ret, "iface 1 promiscuous mode OFF");
755 }
756
set_promisc_mode_on_again(void)757 static void set_promisc_mode_on_again(void)
758 {
759 int ret;
760
761 DBG("Make sure promiscuous mode is ON (%p)\n", iface4);
762
763 ret = net_if_set_promisc(iface4);
764
765 zassert_equal(ret, -EALREADY, "iface 1 promiscuous mode OFF");
766 }
767
set_promisc_mode_on(void)768 static void set_promisc_mode_on(void)
769 {
770 bool ret;
771
772 DBG("Setting promiscuous mode ON (%p)\n", iface4);
773
774 ret = net_if_set_promisc(iface4);
775
776 zassert_equal(ret, 0, "iface 1 promiscuous mode set failed");
777 }
778
set_promisc_mode_off(void)779 static void set_promisc_mode_off(void)
780 {
781 DBG("Setting promiscuous mode OFF (%p)\n", iface4);
782
783 net_if_unset_promisc(iface4);
784 }
785
ZTEST(net_iface,test_promisc_mode)786 ZTEST(net_iface, test_promisc_mode)
787 {
788 check_promisc_mode_off();
789 set_promisc_mode_on();
790 check_promisc_mode_on();
791 set_promisc_mode_on_again();
792 set_promisc_mode_off();
793 check_promisc_mode_off();
794 }
795
796 static struct in_addr my_ipv4_addr_test = { { { 10, 0, 0, 1 } } };
797 static struct in_addr my_ipv4_addr_not_found = { { { 1, 2, 3, 4 } } };
798
v4_addr_add(void)799 static void v4_addr_add(void)
800 {
801 bool ret;
802
803 ret = net_if_ipv4_addr_add_by_index(1, &my_ipv4_addr_test,
804 NET_ADDR_MANUAL, 0);
805 zassert_true(ret, "Cannot add IPv4 address");
806 }
807
v4_addr_lookup(void)808 static void v4_addr_lookup(void)
809 {
810 int ret;
811
812 ret = net_if_ipv4_addr_lookup_by_index(&my_ipv4_addr_test);
813 zassert_equal(ret, 1, "IPv4 address not found");
814
815 ret = net_if_ipv4_addr_lookup_by_index(&my_ipv4_addr_not_found);
816 zassert_not_equal(ret, 1, "IPv4 address found");
817 }
818
v4_addr_rm(void)819 static void v4_addr_rm(void)
820 {
821 bool ret;
822
823 ret = net_if_ipv4_addr_rm_by_index(1, &my_ipv4_addr_test);
824 zassert_true(ret, "Cannot remove IPv4 address");
825 }
826
ZTEST(net_iface,test_v4_addr_add_rm)827 ZTEST(net_iface, test_v4_addr_add_rm)
828 {
829 v4_addr_add();
830 v4_addr_lookup();
831 v4_addr_rm();
832 }
833
834 #define MY_ADDR_V4_USER { { { 10, 0, 0, 2 } } }
835 #define UNKNOWN_ADDR_V4_USER { { { 5, 6, 7, 8 } } }
836
v4_addr_add_user(void * p1,void * p2,void * p3)837 static void v4_addr_add_user(void *p1, void *p2, void *p3)
838 {
839 ARG_UNUSED(p1);
840 ARG_UNUSED(p2);
841 ARG_UNUSED(p3);
842
843 struct in_addr my_addr = MY_ADDR_V4_USER;
844 bool ret;
845
846 ret = net_if_ipv4_addr_add_by_index(1, &my_addr, NET_ADDR_MANUAL, 0);
847 zassert_true(ret, "Could not add IPv4 address");
848 }
849
v4_addr_add_user_from_userspace(void)850 static void v4_addr_add_user_from_userspace(void)
851 {
852 k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
853 k_thread_user_mode_enter(v4_addr_add_user, NULL,
854 NULL, NULL);
855 }
856
v4_addr_lookup_user(void)857 static void v4_addr_lookup_user(void)
858 {
859 struct in_addr my_addr = MY_ADDR_V4_USER;
860 struct in_addr unknown_addr = UNKNOWN_ADDR_V4_USER;
861 int ret;
862
863 ret = net_if_ipv4_addr_lookup_by_index(&my_addr);
864 zassert_equal(ret, 1, "IPv4 address not found (%d)", ret);
865
866 ret = net_if_ipv4_addr_lookup_by_index(&unknown_addr);
867 zassert_equal(ret, 0, "IPv4 address found");
868 }
869
v4_addr_rm_user(void * p1,void * p2,void * p3)870 static void v4_addr_rm_user(void *p1, void *p2, void *p3)
871 {
872 ARG_UNUSED(p1);
873 ARG_UNUSED(p2);
874 ARG_UNUSED(p3);
875
876 struct in_addr my_addr = MY_ADDR_V4_USER;
877 bool ret;
878
879 ret = net_if_ipv4_addr_rm_by_index(1, &my_addr);
880 zassert_true(ret, "Cannot remove IPv4 address");
881 }
882
v4_addr_rm_user_from_userspace(void)883 static void v4_addr_rm_user_from_userspace(void)
884 {
885 k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
886 k_thread_user_mode_enter(v4_addr_rm_user, NULL,
887 NULL, NULL);
888 }
889
ZTEST(net_iface,test_v4_addr_add_rm_user_from_userspace)890 ZTEST(net_iface, test_v4_addr_add_rm_user_from_userspace)
891 {
892 v4_addr_add_user_from_userspace();
893 v4_addr_lookup_user();
894 v4_addr_rm_user_from_userspace();
895 }
896
897 static
898 struct in6_addr my_ipv6_addr_test = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
899 0, 0, 0, 0, 0, 0, 0, 0x1 } } };
900
901 static
902 struct in6_addr my_ipv6_addr_not_found = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0,
903 0, 0, 0, 0, 0, 0, 0, 0, 0x64 } } };
904
v6_addr_add(void)905 static void v6_addr_add(void)
906 {
907 bool ret;
908
909 ret = net_if_ipv6_addr_add_by_index(1, &my_ipv6_addr_test,
910 NET_ADDR_MANUAL, 0);
911 zassert_true(ret, "Cannot add IPv6 address");
912 }
913
v6_addr_add_mcast_twice(void)914 static void v6_addr_add_mcast_twice(void)
915 {
916 struct net_if_mcast_addr *maddr;
917
918 maddr = net_if_ipv6_maddr_add(iface1, &in6addr_mcast);
919 zassert_equal(maddr, NULL, "Address was added twice");
920 }
921
v6_addr_lookup(void)922 static void v6_addr_lookup(void)
923 {
924 int ret;
925
926 ret = net_if_ipv6_addr_lookup_by_index(&my_ipv6_addr_test);
927 zassert_equal(ret, 1, "IPv6 address not found");
928
929 ret = net_if_ipv6_addr_lookup_by_index(&my_ipv6_addr_not_found);
930 zassert_not_equal(ret, 1, "IPv6 address found");
931 }
932
v6_addr_rm(void)933 static void v6_addr_rm(void)
934 {
935 bool ret;
936
937 ret = net_if_ipv6_addr_rm_by_index(1, &my_ipv6_addr_test);
938 zassert_true(ret, "Cannot remove IPv6 address");
939 }
940
ZTEST(net_iface,test_v6_addr_add_rm)941 ZTEST(net_iface, test_v6_addr_add_rm)
942 {
943 v6_addr_add();
944 v6_addr_add_mcast_twice();
945 v6_addr_lookup();
946 v6_addr_rm();
947 }
948
ZTEST(net_iface,test_v6_addr_add_rm_solicited)949 ZTEST(net_iface, test_v6_addr_add_rm_solicited)
950 {
951 const struct in6_addr prefix = { { { 0x20, 0x01, 0x1b, 0x98, 0x24, 0xb8, 0x7e, 0xbb,
952 0, 0, 0, 0, 0, 0, 0, 0 } } };
953 struct in6_addr iid_addr = { };
954 struct in6_addr iid_addr_mcast = { };
955 struct in6_addr unicast_addr = { };
956 struct in6_addr unicast_addr_mcast = { };
957 struct net_if_addr *ifaddr;
958 struct net_if_mcast_addr *maddr;
959 bool ret;
960
961 /* Add a link-local address based on the interface identifier */
962 net_ipv6_addr_create_iid(&iid_addr, net_if_get_link_addr(iface4));
963 ifaddr = net_if_ipv6_addr_add(iface4, &iid_addr,
964 NET_ADDR_AUTOCONF, 0);
965 zassert_not_null(ifaddr, "Cannot add IPv6 link-local address");
966
967 /* Add the corresponding solicited-node multicast address */
968 net_ipv6_addr_create_solicited_node(&iid_addr, &iid_addr_mcast);
969 maddr = net_if_ipv6_maddr_add(iface4, &iid_addr_mcast);
970 zassert_not_null(maddr, "Cannot add solicited-node multicast address");
971
972 /* Add an autoconfigured global unicast address */
973 net_ipv6_addr_create_iid(&unicast_addr, net_if_get_link_addr(iface4));
974 memcpy(&unicast_addr, &prefix, sizeof(prefix) / 2);
975 ifaddr = net_if_ipv6_addr_add(iface4, &unicast_addr,
976 NET_ADDR_AUTOCONF, 0);
977 zassert_not_null(ifaddr, "Cannot add IPv6 global unicast address");
978
979 /* Add the corresponding solicited-node multicast address (should exist) */
980 net_ipv6_addr_create_solicited_node(&unicast_addr, &unicast_addr_mcast);
981 zassert_mem_equal(&unicast_addr_mcast, &iid_addr_mcast,
982 sizeof(struct in6_addr));
983 maddr = net_if_ipv6_maddr_add(iface4, &unicast_addr_mcast);
984 zassert_is_null(maddr, "Solicited-node multicast address was added twice");
985
986 /* Remove the global unicast address */
987 ret = net_if_ipv6_addr_rm(iface4, &unicast_addr);
988 zassert_true(ret, "Cannot remove IPv6 global unicast address");
989
990 /* The solicited-node multicast address should stay */
991 maddr = net_if_ipv6_maddr_lookup(&iid_addr_mcast, &iface4);
992 zassert_not_null(maddr, "Solicited-node multicast address was removed");
993
994 /* Remove the link-local address */
995 ret = net_if_ipv6_addr_rm(iface4, &iid_addr);
996 zassert_true(ret, "Cannot remove IPv6 link-local address");
997
998 /* The solicited-node multicast address should be gone */
999 maddr = net_if_ipv6_maddr_lookup(&iid_addr_mcast, &iface4);
1000 zassert_is_null(maddr, "Solicited-node multicast address was not removed");
1001 }
1002
1003 #define MY_ADDR_V6_USER { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, \
1004 0, 0, 0, 0, 0, 0, 0, 0x65 } } }
1005
1006 #define UNKNOWN_ADDR_V6_USER { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, \
1007 0, 0, 0, 0, 0, 0, 0, 0x66 } } }
1008
v6_addr_add_user(void * p1,void * p2,void * p3)1009 static void v6_addr_add_user(void *p1, void *p2, void *p3)
1010 {
1011 ARG_UNUSED(p1);
1012 ARG_UNUSED(p2);
1013 ARG_UNUSED(p3);
1014
1015 struct in6_addr my_addr = MY_ADDR_V6_USER;
1016 bool ret;
1017
1018 ret = net_if_ipv6_addr_add_by_index(1, &my_addr, NET_ADDR_MANUAL, 0);
1019 zassert_true(ret, "Could not add IPv6 address");
1020 }
1021
v6_addr_add_user_from_userspace(void)1022 static void v6_addr_add_user_from_userspace(void)
1023 {
1024 k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
1025 k_thread_user_mode_enter(v6_addr_add_user, NULL,
1026 NULL, NULL);
1027 }
1028
v6_addr_lookup_user(void)1029 static void v6_addr_lookup_user(void)
1030 {
1031 struct in6_addr my_addr = MY_ADDR_V6_USER;
1032 struct in6_addr unknown_addr = UNKNOWN_ADDR_V6_USER;
1033 int ret;
1034
1035 ret = net_if_ipv6_addr_lookup_by_index(&my_addr);
1036 zassert_equal(ret, 1, "IPv6 address not found (%d)", ret);
1037
1038 ret = net_if_ipv6_addr_lookup_by_index(&unknown_addr);
1039 zassert_equal(ret, 0, "IPv6 address found");
1040 }
1041
v6_addr_rm_user(void * p1,void * p2,void * p3)1042 static void v6_addr_rm_user(void *p1, void *p2, void *p3)
1043 {
1044 ARG_UNUSED(p1);
1045 ARG_UNUSED(p2);
1046 ARG_UNUSED(p3);
1047
1048 struct in6_addr my_addr = MY_ADDR_V6_USER;
1049 bool ret;
1050
1051 /* Check also that add is enabled so that we can remove something
1052 * that was already added.
1053 */
1054 ret = net_if_ipv6_addr_rm_by_index(1, &my_addr);
1055 zassert_true(ret, "Cannot remove IPv6 address");
1056 }
1057
v6_addr_rm_user_from_userspace(void)1058 static void v6_addr_rm_user_from_userspace(void)
1059 {
1060 k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
1061 k_thread_user_mode_enter(v6_addr_rm_user, NULL,
1062 NULL, NULL);
1063 }
1064
ZTEST(net_iface,test_v6_addr_add_rm_user_from_userspace)1065 ZTEST(net_iface, test_v6_addr_add_rm_user_from_userspace)
1066 {
1067 v6_addr_add_user_from_userspace();
1068 v6_addr_lookup_user();
1069 v6_addr_rm_user_from_userspace();
1070 }
1071
netmask_addr_add(void * p1,void * p2,void * p3)1072 static void netmask_addr_add(void *p1, void *p2, void *p3)
1073 {
1074 ARG_UNUSED(p1);
1075 ARG_UNUSED(p2);
1076 ARG_UNUSED(p3);
1077
1078 struct in_addr my_netmask = { { { 255, 255, 255, 0 } } };
1079 bool ret;
1080
1081 ret = net_if_ipv4_set_netmask_by_addr_by_index(1, &my_ipv4_addr1, &my_netmask);
1082 zassert_true(ret, "Cannot add IPv4 netmask");
1083 }
1084
ZTEST(net_iface,test_netmask_addr_add)1085 ZTEST(net_iface, test_netmask_addr_add)
1086 {
1087 netmask_addr_add(NULL, NULL, NULL);
1088 }
1089
netmask_addr_add_from_userspace(void)1090 static void netmask_addr_add_from_userspace(void)
1091 {
1092 k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
1093 k_thread_user_mode_enter(netmask_addr_add, NULL,
1094 NULL, NULL);
1095 }
1096
ZTEST(net_iface,test_netmask_addr_add_from_userspace)1097 ZTEST(net_iface, test_netmask_addr_add_from_userspace)
1098 {
1099 netmask_addr_add_from_userspace();
1100 }
1101
gw_addr_add(void * p1,void * p2,void * p3)1102 static void gw_addr_add(void *p1, void *p2, void *p3)
1103 {
1104 ARG_UNUSED(p1);
1105 ARG_UNUSED(p2);
1106 ARG_UNUSED(p3);
1107
1108 struct in_addr my_gw = { { { 192, 0, 2, 254 } } };
1109 bool ret;
1110
1111 ret = net_if_ipv4_set_gw_by_index(1, &my_gw);
1112 zassert_true(ret, "Cannot add IPv4 gateway");
1113 }
1114
ZTEST(net_iface,test_gw_addr_add)1115 ZTEST(net_iface, test_gw_addr_add)
1116 {
1117 gw_addr_add(NULL, NULL, NULL);
1118 }
1119
gw_addr_add_from_userspace(void)1120 static void gw_addr_add_from_userspace(void)
1121 {
1122 k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
1123 k_thread_user_mode_enter(gw_addr_add, NULL,
1124 NULL, NULL);
1125 }
1126
ZTEST(net_iface,test_gw_addr_add_from_userspace)1127 ZTEST(net_iface, test_gw_addr_add_from_userspace)
1128 {
1129 gw_addr_add_from_userspace();
1130 }
1131
get_by_index(void * p1,void * p2,void * p3)1132 static void get_by_index(void *p1, void *p2, void *p3)
1133 {
1134 ARG_UNUSED(p1);
1135 ARG_UNUSED(p2);
1136 ARG_UNUSED(p3);
1137
1138 zassert_not_null(net_if_get_by_index(1),
1139 "Cannot get interface at index 1");
1140 }
1141
ZTEST(net_iface,test_get_by_index)1142 ZTEST(net_iface, test_get_by_index)
1143 {
1144 get_by_index(NULL, NULL, NULL);
1145 }
1146
get_by_index_from_userspace(void)1147 static void get_by_index_from_userspace(void)
1148 {
1149 k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
1150 k_thread_user_mode_enter(get_by_index, NULL,
1151 NULL, NULL);
1152 }
1153
ZTEST(net_iface,test_get_by_index_from_userspace)1154 ZTEST(net_iface, test_get_by_index_from_userspace)
1155 {
1156 get_by_index_from_userspace();
1157 }
1158
foreach_ipv4_addr_check(struct net_if * iface,struct net_if_addr * if_addr,void * user_data)1159 static void foreach_ipv4_addr_check(struct net_if *iface,
1160 struct net_if_addr *if_addr,
1161 void *user_data)
1162 {
1163 int *count = (int *)user_data;
1164
1165 (*count)++;
1166
1167 zassert_equal_ptr(iface, iface1, "Callback called on wrong interface");
1168 zassert_mem_equal(&if_addr->address.in_addr, &my_ipv4_addr1,
1169 sizeof(struct in_addr), "Wrong IPv4 address");
1170 }
1171
ZTEST(net_iface,test_ipv4_addr_foreach)1172 ZTEST(net_iface, test_ipv4_addr_foreach)
1173 {
1174 int count = 0;
1175
1176 /* iface1 has one IPv4 address configured */
1177 net_if_ipv4_addr_foreach(iface1, foreach_ipv4_addr_check, &count);
1178 zassert_equal(count, 1, "Incorrect number of callback calls");
1179
1180 count = 0;
1181
1182 /* iface4 has no IPv4 address configured */
1183 net_if_ipv4_addr_foreach(iface4, foreach_ipv4_addr_check, &count);
1184 zassert_equal(count, 0, "Incorrect number of callback calls");
1185 }
1186
foreach_ipv4_maddr_check(struct net_if * iface,struct net_if_mcast_addr * if_addr,void * user_data)1187 static void foreach_ipv4_maddr_check(struct net_if *iface,
1188 struct net_if_mcast_addr *if_addr,
1189 void *user_data)
1190 {
1191 int *count = (int *)user_data;
1192
1193 (*count)++;
1194
1195 zassert_equal_ptr(iface, iface1, "Callback called on wrong interface");
1196 zassert_mem_equal(&if_addr->address.in_addr, &inaddr_mcast,
1197 sizeof(struct in_addr), "Wrong IPv4 multicast address");
1198 }
1199
ZTEST(net_iface,test_ipv4_maddr_foreach)1200 ZTEST(net_iface, test_ipv4_maddr_foreach)
1201 {
1202 int count = 0;
1203
1204 /* iface1 has one IPv4 multicast address configured */
1205 net_if_ipv4_maddr_foreach(iface1, foreach_ipv4_maddr_check, &count);
1206 zassert_equal(count, 1, "Incorrect number of callback calls");
1207
1208 count = 0;
1209
1210 /* iface4 has no IPv4 multicast address configured */
1211 net_if_ipv4_maddr_foreach(iface4, foreach_ipv4_maddr_check, &count);
1212 zassert_equal(count, 0, "Incorrect number of callback calls");
1213 }
1214
foreach_ipv6_addr_check(struct net_if * iface,struct net_if_addr * if_addr,void * user_data)1215 static void foreach_ipv6_addr_check(struct net_if *iface,
1216 struct net_if_addr *if_addr,
1217 void *user_data)
1218 {
1219 int *count = (int *)user_data;
1220
1221 (*count)++;
1222
1223 zassert_equal_ptr(iface, iface1, "Callback called on wrong interface");
1224
1225 if (net_ipv6_is_ll_addr(&if_addr->address.in6_addr)) {
1226 zassert_mem_equal(&if_addr->address.in6_addr, &ll_addr,
1227 sizeof(struct in6_addr), "Wrong IPv6 address");
1228 } else {
1229 zassert_mem_equal(&if_addr->address.in6_addr, &my_addr1,
1230 sizeof(struct in6_addr), "Wrong IPv6 address");
1231 }
1232 }
1233
ZTEST(net_iface,test_ipv6_addr_foreach)1234 ZTEST(net_iface, test_ipv6_addr_foreach)
1235 {
1236 int count = 0;
1237
1238 /* iface1 has two IPv6 addresses configured */
1239 net_if_ipv6_addr_foreach(iface1, foreach_ipv6_addr_check, &count);
1240 zassert_equal(count, 2, "Incorrect number of callback calls");
1241
1242 count = 0;
1243
1244 /* iface4 has no IPv6 address configured */
1245 net_if_ipv6_addr_foreach(iface4, foreach_ipv6_addr_check, &count);
1246 zassert_equal(count, 0, "Incorrect number of callback calls");
1247 }
1248
foreach_ipv6_maddr_check(struct net_if * iface,struct net_if_mcast_addr * if_addr,void * user_data)1249 static void foreach_ipv6_maddr_check(struct net_if *iface,
1250 struct net_if_mcast_addr *if_addr,
1251 void *user_data)
1252 {
1253 int *count = (int *)user_data;
1254
1255 (*count)++;
1256
1257 zassert_equal_ptr(iface, iface1, "Callback called on wrong interface");
1258 zassert_mem_equal(&if_addr->address.in6_addr, &in6addr_mcast, sizeof(struct in6_addr),
1259 "Wrong IPv6 multicast address");
1260 }
1261
ZTEST(net_iface,test_ipv6_maddr_foreach)1262 ZTEST(net_iface, test_ipv6_maddr_foreach)
1263 {
1264 int count = 0;
1265
1266 /* iface1 has one IPv6 multicast address configured */
1267 net_if_ipv6_maddr_foreach(iface1, foreach_ipv6_maddr_check, &count);
1268 zassert_equal(count, 1, "Incorrect number of callback calls");
1269
1270 count = 0;
1271
1272 /* iface4 has no IPv6 multicast address configured */
1273 net_if_ipv6_maddr_foreach(iface4, foreach_ipv6_maddr_check, &count);
1274 zassert_equal(count, 0, "Incorrect number of callback calls");
1275 }
1276
ZTEST(net_iface,test_interface_name)1277 ZTEST(net_iface, test_interface_name)
1278 {
1279 int ret;
1280
1281 #if defined(CONFIG_NET_INTERFACE_NAME)
1282 char buf[CONFIG_NET_INTERFACE_NAME_LEN + 1];
1283 struct net_if *iface;
1284 char *name;
1285
1286 iface = net_if_get_default();
1287 memset(buf, 0, sizeof(buf));
1288
1289 ret = net_if_get_name(NULL, NULL, -1);
1290 zassert_equal(ret, -EINVAL, "Unexpected value returned");
1291
1292 ret = net_if_get_name(iface, NULL, -1);
1293 zassert_equal(ret, -EINVAL, "Unexpected value returned");
1294
1295 ret = net_if_get_name(iface, buf, 0);
1296 zassert_equal(ret, -EINVAL, "Unexpected value returned");
1297
1298 name = "mynetworkiface0";
1299 ret = net_if_set_name(iface, name);
1300 zassert_equal(ret, -ENAMETOOLONG, "Unexpected value (%d) returned", ret);
1301
1302 name = "abc0";
1303 ret = net_if_set_name(iface, name);
1304 zassert_equal(ret, 0, "Unexpected value (%d) returned", ret);
1305
1306 name = "abc0";
1307 ret = net_if_set_name(iface2, name);
1308 zassert_equal(ret, -EALREADY, "Unexpected value (%d) returned", ret);
1309
1310 name = "abc";
1311 ret = net_if_set_name(iface2, name);
1312 zassert_equal(ret, 0, "Unexpected value (%d) returned", ret);
1313
1314 name = "abc0";
1315
1316 ret = net_if_get_name(iface, buf, 1);
1317 zassert_equal(ret, -ERANGE, "Unexpected value (%d) returned", ret);
1318
1319 ret = net_if_get_name(iface, buf, strlen(name) - 1);
1320 zassert_equal(ret, -ERANGE, "Unexpected value (%d) returned", ret);
1321
1322 ret = net_if_get_name(iface, buf, sizeof(buf) - 1);
1323 zassert_equal(ret, strlen(name), "Unexpected value (%d) returned, expected %d",
1324 ret, strlen(name));
1325
1326 ret = net_if_get_by_name(name);
1327 zassert_equal(ret, net_if_get_by_iface(iface), "Unexpected value (%d) returned", ret);
1328
1329 ret = net_if_get_by_name("ENOENT");
1330 zassert_equal(ret, -ENOENT, "Unexpected value (%d) returned", ret);
1331 #else
1332 ret = net_if_get_name(NULL, NULL, -1);
1333 zassert_equal(ret, -ENOTSUP, "Invalid value returned");
1334 #endif
1335 }
1336
generate_iid(struct net_if * iface,struct in6_addr * expected_addr,struct in6_addr * iid_addr)1337 static void generate_iid(struct net_if *iface,
1338 struct in6_addr *expected_addr,
1339 struct in6_addr *iid_addr)
1340 {
1341 const struct in6_addr prefix = { { { 0x20, 0x01, 0x1b, 0x98, 0x24, 0xb8, 0x7e, 0xbb,
1342 0, 0, 0, 0, 0, 0, 0, 0 } } };
1343 struct net_linkaddr *lladdr = net_if_get_link_addr(iface);
1344 uint8_t *mac;
1345 int ret;
1346
1347 (void)net_iface_get_mac(net_if_get_device(iface));
1348
1349 lladdr = net_if_get_link_addr(eth_iface);
1350 mac = lladdr->addr;
1351
1352 memcpy(expected_addr, &prefix, sizeof(struct in6_addr));
1353 memcpy(&expected_addr->s6_addr[8], &mac[0], 3);
1354 expected_addr->s6_addr[11] = 0xff;
1355 expected_addr->s6_addr[12] = 0xfe;
1356 memcpy(&expected_addr->s6_addr[13], &mac[3], 3);
1357
1358 expected_addr->s6_addr[8] ^= 0x02; /* Universal bit toggle */
1359
1360 ret = net_ipv6_addr_generate_iid(iface, &prefix, NULL, 0, 0, iid_addr,
1361 net_if_get_link_addr(eth_iface));
1362 zassert_equal(ret, 0, "Unexpected value (%d) returned", ret);
1363 }
1364
ZTEST(net_iface,test_ipv6_iid_eui64)1365 ZTEST(net_iface, test_ipv6_iid_eui64)
1366 {
1367 #if defined(CONFIG_NET_IPV6_IID_EUI_64)
1368 struct in6_addr iid_addr = { };
1369 struct in6_addr expected_addr = { };
1370
1371 generate_iid(eth_iface, &expected_addr, &iid_addr);
1372
1373 zassert_mem_equal(&expected_addr, &iid_addr, sizeof(struct in6_addr));
1374 #else
1375 ztest_test_skip();
1376 #endif
1377 }
1378
ZTEST(net_iface,test_ipv6_iid_stable)1379 ZTEST(net_iface, test_ipv6_iid_stable)
1380 {
1381 #if defined(CONFIG_NET_IPV6_IID_STABLE)
1382 struct in6_addr iid_addr = { };
1383 struct in6_addr expected_addr = { };
1384
1385 generate_iid(eth_iface, &expected_addr, &iid_addr);
1386
1387 /* Make sure that EUI-64 bytes are not there */
1388 zassert_not_equal(iid_addr.s6_addr[11], 0xff);
1389 zassert_not_equal(iid_addr.s6_addr[12], 0xfe);
1390
1391 zassert_true(memcmp(&expected_addr, &iid_addr, sizeof(struct in6_addr)) != 0,
1392 "IID is EUI-64 instead of randomized");
1393 #else
1394 ztest_test_skip();
1395 #endif
1396 }
1397
1398 ZTEST_SUITE(net_iface, NULL, iface_setup, NULL, NULL, iface_teardown);
1399