1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define NET_LOG_LEVEL CONFIG_NET_IF_LOG_LEVEL
10 
11 #include <logging/log.h>
12 LOG_MODULE_REGISTER(net_test, NET_LOG_LEVEL);
13 
14 #include <zephyr/types.h>
15 #include <stdbool.h>
16 #include <stddef.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <sys/printk.h>
20 #include <linker/sections.h>
21 #include <random/rand32.h>
22 
23 #include <ztest.h>
24 
25 #include <net/ethernet.h>
26 #include <net/dummy.h>
27 #include <net/buf.h>
28 #include <net/net_ip.h>
29 #include <net/net_if.h>
30 
31 #define NET_LOG_ENABLED 1
32 #include "net_private.h"
33 
34 #if defined(CONFIG_NET_IF_LOG_LEVEL_DBG)
35 #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
36 #else
37 #define DBG(fmt, ...)
38 #endif
39 
40 /* Interface 1 addresses */
41 static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
42 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
43 static struct in_addr my_ipv4_addr1 = { { { 192, 0, 2, 1 } } };
44 
45 /* Interface 2 addresses */
46 static struct in6_addr my_addr2 = { { { 0x20, 0x01, 0x0d, 0xb8, 2, 0, 0, 0,
47 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
48 
49 /* Interface 3 addresses */
50 static struct in6_addr my_addr3 = { { { 0x20, 0x01, 0x0d, 0xb8, 3, 0, 0, 0,
51 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
52 
53 /* Extra address is assigned to ll_addr */
54 static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
55 				       0, 0, 0, 0xf2, 0xaa, 0x29, 0x02,
56 				       0x04 } } };
57 
58 static struct in6_addr in6addr_mcast = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
59 					     0, 0, 0, 0, 0, 0, 0, 0x1 } } };
60 
61 static struct net_if *iface1;
62 static struct net_if *iface2;
63 static struct net_if *iface3;
64 static struct net_if *iface4;
65 
66 static bool test_failed;
67 static bool test_started;
68 static struct k_sem wait_data;
69 
70 #define WAIT_TIME 250
71 
72 struct net_if_test {
73 	uint8_t idx;
74 	uint8_t mac_addr[sizeof(struct net_eth_addr)];
75 	struct net_linkaddr ll_addr;
76 };
77 
net_iface_dev_init(const struct device * dev)78 static int net_iface_dev_init(const struct device *dev)
79 {
80 	return 0;
81 }
82 
net_iface_get_mac(const struct device * dev)83 static uint8_t *net_iface_get_mac(const struct device *dev)
84 {
85 	struct net_if_test *data = dev->data;
86 
87 	if (data->mac_addr[2] == 0x00) {
88 		/* 00-00-5E-00-53-xx Documentation RFC 7042 */
89 		data->mac_addr[0] = 0x00;
90 		data->mac_addr[1] = 0x00;
91 		data->mac_addr[2] = 0x5E;
92 		data->mac_addr[3] = 0x00;
93 		data->mac_addr[4] = 0x53;
94 		data->mac_addr[5] = sys_rand32_get();
95 	}
96 
97 	data->ll_addr.addr = data->mac_addr;
98 	data->ll_addr.len = 6U;
99 
100 	return data->mac_addr;
101 }
102 
net_iface_init(struct net_if * iface)103 static void net_iface_init(struct net_if *iface)
104 {
105 	uint8_t *mac = net_iface_get_mac(net_if_get_device(iface));
106 
107 	net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr),
108 			     NET_LINK_ETHERNET);
109 }
110 
sender_iface(const struct device * dev,struct net_pkt * pkt)111 static int sender_iface(const struct device *dev, struct net_pkt *pkt)
112 {
113 	if (!pkt->buffer) {
114 		DBG("No data to send!\n");
115 		return -ENODATA;
116 	}
117 
118 	if (test_started) {
119 		struct net_if_test *data = dev->data;
120 
121 		DBG("Sending at iface %d %p\n",
122 		    net_if_get_by_iface(net_pkt_iface(pkt)),
123 		    net_pkt_iface(pkt));
124 
125 		if (net_if_get_by_iface(net_pkt_iface(pkt)) != data->idx) {
126 			DBG("Invalid interface %d index, expecting %d\n",
127 			    data->idx, net_if_get_by_iface(net_pkt_iface(pkt)));
128 			test_failed = true;
129 		}
130 	}
131 
132 	k_sem_give(&wait_data);
133 
134 	return 0;
135 }
136 
137 struct net_if_test net_iface1_data;
138 struct net_if_test net_iface2_data;
139 struct net_if_test net_iface3_data;
140 
141 static struct dummy_api net_iface_api = {
142 	.iface_api.init = net_iface_init,
143 	.send = sender_iface,
144 };
145 
146 #define _ETH_L2_LAYER DUMMY_L2
147 #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2)
148 
149 NET_DEVICE_INIT_INSTANCE(net_iface1_test,
150 			 "iface1",
151 			 iface1,
152 			 net_iface_dev_init,
153 			 NULL,
154 			 &net_iface1_data,
155 			 NULL,
156 			 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
157 			 &net_iface_api,
158 			 _ETH_L2_LAYER,
159 			 _ETH_L2_CTX_TYPE,
160 			 127);
161 
162 NET_DEVICE_INIT_INSTANCE(net_iface2_test,
163 			 "iface2",
164 			 iface2,
165 			 net_iface_dev_init,
166 			 NULL,
167 			 &net_iface2_data,
168 			 NULL,
169 			 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
170 			 &net_iface_api,
171 			 _ETH_L2_LAYER,
172 			 _ETH_L2_CTX_TYPE,
173 			 127);
174 
175 NET_DEVICE_INIT_INSTANCE(net_iface3_test,
176 			 "iface3",
177 			 iface3,
178 			 net_iface_dev_init,
179 			 NULL,
180 			 &net_iface3_data,
181 			 NULL,
182 			 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
183 			 &net_iface_api,
184 			 _ETH_L2_LAYER,
185 			 _ETH_L2_CTX_TYPE,
186 			 127);
187 
188 struct eth_fake_context {
189 	struct net_if *iface;
190 	uint8_t mac_address[6];
191 	bool promisc_mode;
192 };
193 
194 static struct eth_fake_context eth_fake_data;
195 
eth_fake_iface_init(struct net_if * iface)196 static void eth_fake_iface_init(struct net_if *iface)
197 {
198 	const struct device *dev = net_if_get_device(iface);
199 	struct eth_fake_context *ctx = dev->data;
200 
201 	ctx->iface = iface;
202 
203 	net_if_set_link_addr(iface, ctx->mac_address,
204 			     sizeof(ctx->mac_address),
205 			     NET_LINK_ETHERNET);
206 
207 	ethernet_init(iface);
208 }
209 
eth_fake_send(const struct device * dev,struct net_pkt * pkt)210 static int eth_fake_send(const struct device *dev,
211 			 struct net_pkt *pkt)
212 {
213 	ARG_UNUSED(dev);
214 	ARG_UNUSED(pkt);
215 
216 	return 0;
217 }
218 
eth_fake_get_capabilities(const struct device * dev)219 static enum ethernet_hw_caps eth_fake_get_capabilities(const struct device *dev)
220 {
221 	return ETHERNET_PROMISC_MODE;
222 }
223 
eth_fake_set_config(const struct device * dev,enum ethernet_config_type type,const struct ethernet_config * config)224 static int eth_fake_set_config(const struct device *dev,
225 			       enum ethernet_config_type type,
226 			       const struct ethernet_config *config)
227 {
228 	struct eth_fake_context *ctx = dev->data;
229 
230 	switch (type) {
231 	case ETHERNET_CONFIG_TYPE_PROMISC_MODE:
232 		if (config->promisc_mode == ctx->promisc_mode) {
233 			return -EALREADY;
234 		}
235 
236 		ctx->promisc_mode = config->promisc_mode;
237 
238 		break;
239 
240 	default:
241 		return -EINVAL;
242 	}
243 
244 	return 0;
245 }
246 
247 static struct ethernet_api eth_fake_api_funcs = {
248 	.iface_api.init = eth_fake_iface_init,
249 
250 	.get_capabilities = eth_fake_get_capabilities,
251 	.set_config = eth_fake_set_config,
252 	.send = eth_fake_send,
253 };
254 
eth_fake_init(const struct device * dev)255 static int eth_fake_init(const struct device *dev)
256 {
257 	struct eth_fake_context *ctx = dev->data;
258 
259 	ctx->promisc_mode = false;
260 
261 	return 0;
262 }
263 
264 ETH_NET_DEVICE_INIT(eth_fake, "eth_fake", eth_fake_init, NULL,
265 		    &eth_fake_data, NULL, CONFIG_ETH_INIT_PRIORITY,
266 		    &eth_fake_api_funcs, NET_ETH_MTU);
267 
268 #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
iface2str(struct net_if * iface)269 static const char *iface2str(struct net_if *iface)
270 {
271 	if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
272 		return "Ethernet";
273 	}
274 
275 	if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
276 		return "Dummy";
277 	}
278 
279 	return "<unknown type>";
280 }
281 #endif
282 
iface_cb(struct net_if * iface,void * user_data)283 static void iface_cb(struct net_if *iface, void *user_data)
284 {
285 	static int if_count;
286 
287 	DBG("Interface %p (%s) [%d]\n", iface, iface2str(iface),
288 	    net_if_get_by_iface(iface));
289 
290 	if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
291 		const struct ethernet_api *api =
292 			net_if_get_device(iface)->api;
293 
294 		/* As native_posix board will introduce another ethernet
295 		 * interface, make sure that we only use our own in this test.
296 		 */
297 		if (api->get_capabilities ==
298 		    eth_fake_api_funcs.get_capabilities) {
299 			iface4 = iface;
300 		}
301 	} else {
302 		switch (if_count) {
303 		case 0:
304 			iface1 = iface;
305 			break;
306 		case 1:
307 			iface2 = iface;
308 			break;
309 		case 2:
310 			iface3 = iface;
311 			break;
312 		}
313 
314 		if_count++;
315 	}
316 }
317 
test_iface_setup(void)318 static void test_iface_setup(void)
319 {
320 	struct net_if_mcast_addr *maddr;
321 	struct net_if_addr *ifaddr;
322 	int idx;
323 
324 	/* The semaphore is there to wait the data to be received. */
325 	k_sem_init(&wait_data, 0, UINT_MAX);
326 
327 	net_if_foreach(iface_cb, NULL);
328 
329 	idx = net_if_get_by_iface(iface1);
330 	((struct net_if_test *)
331 	 net_if_get_device(iface1)->data)->idx = idx;
332 
333 	idx = net_if_get_by_iface(iface2);
334 	((struct net_if_test *)
335 	 net_if_get_device(iface2)->data)->idx = idx;
336 
337 	idx = net_if_get_by_iface(iface3);
338 	((struct net_if_test *)
339 	 net_if_get_device(iface3)->data)->idx = idx;
340 
341 	DBG("Interfaces: [%d] iface1 %p, [%d] iface2 %p, [%d] iface3 %p\n",
342 	    net_if_get_by_iface(iface1), iface1,
343 	    net_if_get_by_iface(iface2), iface2,
344 	    net_if_get_by_iface(iface3), iface3);
345 
346 	zassert_not_null(iface1, "Interface 1");
347 	zassert_not_null(iface2, "Interface 2");
348 	zassert_not_null(iface3, "Interface 3");
349 
350 	ifaddr = net_if_ipv6_addr_add(iface1, &my_addr1,
351 				      NET_ADDR_MANUAL, 0);
352 	if (!ifaddr) {
353 		DBG("Cannot add IPv6 address %s\n",
354 		       net_sprint_ipv6_addr(&my_addr1));
355 		zassert_not_null(ifaddr, "addr1");
356 	}
357 
358 	ifaddr = net_if_ipv4_addr_add(iface1, &my_ipv4_addr1,
359 				      NET_ADDR_MANUAL, 0);
360 	if (!ifaddr) {
361 		DBG("Cannot add IPv4 address %s\n",
362 		       net_sprint_ipv4_addr(&my_ipv4_addr1));
363 		zassert_not_null(ifaddr, "ipv4 addr1");
364 	}
365 
366 	/* For testing purposes we need to set the adddresses preferred */
367 	ifaddr->addr_state = NET_ADDR_PREFERRED;
368 
369 	ifaddr = net_if_ipv6_addr_add(iface1, &ll_addr,
370 				      NET_ADDR_MANUAL, 0);
371 	if (!ifaddr) {
372 		DBG("Cannot add IPv6 address %s\n",
373 		       net_sprint_ipv6_addr(&ll_addr));
374 		zassert_not_null(ifaddr, "ll_addr");
375 	}
376 
377 	ifaddr->addr_state = NET_ADDR_PREFERRED;
378 
379 	ifaddr = net_if_ipv6_addr_add(iface2, &my_addr2,
380 				      NET_ADDR_MANUAL, 0);
381 	if (!ifaddr) {
382 		DBG("Cannot add IPv6 address %s\n",
383 		       net_sprint_ipv6_addr(&my_addr2));
384 		zassert_not_null(ifaddr, "addr2");
385 	}
386 
387 	ifaddr->addr_state = NET_ADDR_PREFERRED;
388 
389 	ifaddr = net_if_ipv6_addr_add(iface2, &my_addr3,
390 				      NET_ADDR_MANUAL, 0);
391 	if (!ifaddr) {
392 		DBG("Cannot add IPv6 address %s\n",
393 		       net_sprint_ipv6_addr(&my_addr3));
394 		zassert_not_null(ifaddr, "addr3");
395 	}
396 
397 	ifaddr->addr_state = NET_ADDR_PREFERRED;
398 
399 	net_ipv6_addr_create(&in6addr_mcast, 0xff02, 0, 0, 0, 0, 0, 0, 0x0001);
400 
401 	maddr = net_if_ipv6_maddr_add(iface1, &in6addr_mcast);
402 	if (!maddr) {
403 		DBG("Cannot add multicast IPv6 address %s\n",
404 		       net_sprint_ipv6_addr(&in6addr_mcast));
405 		zassert_not_null(maddr, "mcast");
406 	}
407 
408 	net_if_up(iface1);
409 	net_if_up(iface2);
410 	net_if_up(iface3);
411 	net_if_up(iface4);
412 
413 	/* The interface might receive data which might fail the checks
414 	 * in the iface sending function, so we need to reset the failure
415 	 * flag.
416 	 */
417 	test_failed = false;
418 
419 	test_started = true;
420 }
421 
send_iface(struct net_if * iface,int val,bool expect_fail)422 static bool send_iface(struct net_if *iface, int val, bool expect_fail)
423 {
424 	static uint8_t data[] = { 't', 'e', 's', 't', '\0' };
425 	struct net_pkt *pkt;
426 	int ret;
427 
428 	pkt = net_pkt_alloc_with_buffer(iface, sizeof(data),
429 					AF_UNSPEC, 0, K_FOREVER);
430 	if (!pkt) {
431 		DBG("Cannot allocate pkt\n");
432 		return false;
433 	}
434 
435 	net_pkt_write(pkt, data, sizeof(data));
436 	net_pkt_cursor_init(pkt);
437 
438 	ret = net_send_data(pkt);
439 	if (!expect_fail && ret < 0) {
440 		DBG("Cannot send test packet (%d)\n", ret);
441 		return false;
442 	}
443 
444 	if (!expect_fail && k_sem_take(&wait_data, K_MSEC(WAIT_TIME))) {
445 		DBG("Timeout while waiting interface %d data\n", val);
446 		return false;
447 	}
448 
449 	return true;
450 }
451 
test_send_iface1(void)452 static void test_send_iface1(void)
453 {
454 	bool ret;
455 
456 	DBG("Sending data to iface 1 %p\n", iface1);
457 
458 	ret = send_iface(iface1, 1, false);
459 
460 	zassert_true(ret, "iface 1");
461 }
462 
test_send_iface2(void)463 static void test_send_iface2(void)
464 {
465 	bool ret;
466 
467 	DBG("Sending data to iface 2 %p\n", iface2);
468 
469 	ret = send_iface(iface2, 2, false);
470 
471 	zassert_true(ret, "iface 2");
472 }
473 
test_send_iface3(void)474 static void test_send_iface3(void)
475 {
476 	bool ret;
477 
478 	DBG("Sending data to iface 3 %p\n", iface3);
479 
480 	ret = send_iface(iface3, 3, false);
481 
482 	zassert_true(ret, "iface 3");
483 }
484 
test_send_iface1_down(void)485 static void test_send_iface1_down(void)
486 {
487 	bool ret;
488 
489 	DBG("Sending data to iface 1 %p while down\n", iface1);
490 
491 	net_if_down(iface1);
492 
493 	ret = send_iface(iface1, 1, true);
494 
495 	zassert_true(ret, "iface 1 down");
496 }
497 
test_send_iface1_up(void)498 static void test_send_iface1_up(void)
499 {
500 	bool ret;
501 
502 	DBG("Sending data to iface 1 %p again\n", iface1);
503 
504 	net_if_up(iface1);
505 
506 	ret = send_iface(iface1, 1, false);
507 
508 	zassert_true(ret, "iface 1 up again");
509 }
510 
test_select_src_iface(void)511 static void test_select_src_iface(void)
512 {
513 	struct in6_addr dst_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
514 					  0, 0, 0, 0, 0, 0, 0, 0x2 } } };
515 	struct in6_addr ll_addr1 = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
516 					 0, 0, 0x09, 0x12, 0xaa, 0x29, 0x02,
517 					 0x88 } } };
518 	struct in6_addr dst_addr3 = { { { 0x20, 0x01, 0x0d, 0xb8, 3, 0, 0, 0,
519 					0, 0, 0, 0, 0, 0, 0, 0x99 } } };
520 	struct in6_addr in6addr_mcast1 = { { { 0x00 } } };
521 	struct in_addr dst_addr_2 = { { { 192, 0, 2, 2 } } };
522 
523 	struct net_if_addr *ifaddr;
524 	struct net_if *iface;
525 	struct sockaddr_in ipv4;
526 	struct sockaddr_in6 ipv6;
527 
528 	iface = net_if_ipv6_select_src_iface(&dst_addr1);
529 	zassert_equal_ptr(iface, iface1, "Invalid interface %p vs %p selected",
530 			  iface, iface1);
531 
532 	iface = net_if_ipv6_select_src_iface(&ll_addr1);
533 	zassert_equal_ptr(iface, iface1, "Invalid interface %p vs %p selected",
534 			  iface, iface1);
535 
536 	net_ipv6_addr_create(&in6addr_mcast1, 0xff02, 0, 0, 0, 0, 0, 0, 0x0002);
537 
538 	iface = net_if_ipv6_select_src_iface(&in6addr_mcast1);
539 	zassert_equal_ptr(iface, iface1, "Invalid interface %p vs %p selected",
540 			  iface, iface1);
541 
542 	iface = net_if_ipv6_select_src_iface(&dst_addr3);
543 	zassert_equal_ptr(iface, iface2, "Invalid interface %p vs %p selected",
544 			  iface, iface2);
545 
546 	ifaddr = net_if_ipv6_addr_lookup(&ll_addr, NULL);
547 	zassert_not_null(ifaddr, "No such ll_addr found");
548 
549 	ifaddr->addr_state = NET_ADDR_TENTATIVE;
550 
551 	/* We should now get default interface */
552 	iface = net_if_ipv6_select_src_iface(&ll_addr1);
553 	zassert_equal_ptr(iface, net_if_get_default(),
554 			  "Invalid interface %p vs %p selected",
555 			  iface, net_if_get_default());
556 
557 	net_ipaddr_copy(&ipv4.sin_addr, &dst_addr_2);
558 	ipv4.sin_family = AF_INET;
559 	ipv4.sin_port = 0U;
560 
561 	iface = net_if_select_src_iface((struct sockaddr *)&ipv4);
562 	zassert_equal_ptr(iface, iface1, "Invalid interface %p vs %p selected",
563 			  iface, iface1);
564 
565 	net_ipaddr_copy(&ipv6.sin6_addr, &dst_addr1);
566 	ipv6.sin6_family = AF_INET6;
567 	ipv6.sin6_port = 0U;
568 
569 	iface = net_if_select_src_iface((struct sockaddr *)&ipv6);
570 	zassert_equal_ptr(iface, iface1, "Invalid interface %p vs %p selected",
571 			  iface, iface1);
572 }
573 
test_check_promisc_mode_off(void)574 static void test_check_promisc_mode_off(void)
575 {
576 	bool ret;
577 
578 	DBG("Make sure promiscuous mode is OFF (%p)\n", iface4);
579 
580 	ret = net_if_is_promisc(iface4);
581 
582 	zassert_false(ret, "iface 1 promiscuous mode ON");
583 }
584 
test_check_promisc_mode_on(void)585 static void test_check_promisc_mode_on(void)
586 {
587 	bool ret;
588 
589 	DBG("Make sure promiscuous mode is ON (%p)\n", iface4);
590 
591 	ret = net_if_is_promisc(iface4);
592 
593 	zassert_true(ret, "iface 1 promiscuous mode OFF");
594 }
595 
test_set_promisc_mode_on_again(void)596 static void test_set_promisc_mode_on_again(void)
597 {
598 	int ret;
599 
600 	DBG("Make sure promiscuous mode is ON (%p)\n", iface4);
601 
602 	ret = net_if_set_promisc(iface4);
603 
604 	zassert_equal(ret, -EALREADY, "iface 1 promiscuous mode OFF");
605 }
606 
test_set_promisc_mode_on(void)607 static void test_set_promisc_mode_on(void)
608 {
609 	bool ret;
610 
611 	DBG("Setting promiscuous mode ON (%p)\n", iface4);
612 
613 	ret = net_if_set_promisc(iface4);
614 
615 	zassert_equal(ret, 0, "iface 1 promiscuous mode set failed");
616 }
617 
test_set_promisc_mode_off(void)618 static void test_set_promisc_mode_off(void)
619 {
620 	DBG("Setting promiscuous mode OFF (%p)\n", iface4);
621 
622 	net_if_unset_promisc(iface4);
623 }
624 
625 static struct in_addr my_ipv4_addr_test = { { { 10, 0, 0, 1 } } };
626 static struct in_addr my_ipv4_addr_not_found = { { { 1, 2, 3, 4 } } };
627 
test_v4_addr_add(void)628 static void test_v4_addr_add(void)
629 {
630 	bool ret;
631 
632 	ret = net_if_ipv4_addr_add_by_index(1, &my_ipv4_addr_test,
633 					    NET_ADDR_MANUAL, 0);
634 	zassert_true(ret, "Cannot add IPv4 address");
635 }
636 
test_v4_addr_lookup(void)637 static void test_v4_addr_lookup(void)
638 {
639 	int ret;
640 
641 	ret = net_if_ipv4_addr_lookup_by_index(&my_ipv4_addr_test);
642 	zassert_equal(ret, 1, "IPv4 address not found");
643 
644 	ret = net_if_ipv4_addr_lookup_by_index(&my_ipv4_addr_not_found);
645 	zassert_not_equal(ret, 1, "IPv4 address found");
646 }
647 
test_v4_addr_rm(void)648 static void test_v4_addr_rm(void)
649 {
650 	bool ret;
651 
652 	ret = net_if_ipv4_addr_rm_by_index(1, &my_ipv4_addr_test);
653 	zassert_true(ret, "Cannot remove IPv4 address");
654 }
655 
656 #define MY_ADDR_V4_USER      { { { 10, 0, 0, 2 } } }
657 #define UNKNOWN_ADDR_V4_USER { { { 5, 6, 7, 8 } } }
658 
test_v4_addr_add_user(void)659 static void test_v4_addr_add_user(void)
660 {
661 	struct in_addr my_addr = MY_ADDR_V4_USER;
662 	bool ret;
663 
664 	ret = net_if_ipv4_addr_add_by_index(1, &my_addr, NET_ADDR_MANUAL, 0);
665 	zassert_true(ret, "Could not add IPv4 address");
666 }
667 
test_v4_addr_add_user_from_userspace(void)668 static void test_v4_addr_add_user_from_userspace(void)
669 {
670 	k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
671 	k_thread_user_mode_enter((k_thread_entry_t)test_v4_addr_add_user, NULL,
672 				 NULL, NULL);
673 }
674 
test_v4_addr_lookup_user(void)675 static void test_v4_addr_lookup_user(void)
676 {
677 	struct in_addr my_addr = MY_ADDR_V4_USER;
678 	struct in_addr unknown_addr = UNKNOWN_ADDR_V4_USER;
679 	int ret;
680 
681 	ret = net_if_ipv4_addr_lookup_by_index(&my_addr);
682 	zassert_equal(ret, 1, "IPv4 address not found (%d)", ret);
683 
684 	ret = net_if_ipv4_addr_lookup_by_index(&unknown_addr);
685 	zassert_equal(ret, 0, "IPv4 address found");
686 }
687 
test_v4_addr_rm_user(void)688 static void test_v4_addr_rm_user(void)
689 {
690 	struct in_addr my_addr = MY_ADDR_V4_USER;
691 	bool ret;
692 
693 	ret = net_if_ipv4_addr_rm_by_index(1, &my_addr);
694 	zassert_true(ret, "Cannot remove IPv4 address");
695 }
696 
test_v4_addr_rm_user_from_userspace(void)697 static void test_v4_addr_rm_user_from_userspace(void)
698 {
699 	k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
700 	k_thread_user_mode_enter((k_thread_entry_t)test_v4_addr_rm_user, NULL,
701 				 NULL, NULL);
702 }
703 
704 static
705 struct in6_addr my_ipv6_addr_test = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
706 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
707 
708 static
709 struct in6_addr my_ipv6_addr_not_found = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0,
710 					    0, 0, 0, 0, 0, 0, 0, 0, 0x64 } } };
711 
test_v6_addr_add(void)712 static void test_v6_addr_add(void)
713 {
714 	bool ret;
715 
716 	ret = net_if_ipv6_addr_add_by_index(1, &my_ipv6_addr_test,
717 					    NET_ADDR_MANUAL, 0);
718 	zassert_true(ret, "Cannot add IPv6 address");
719 }
720 
test_v6_addr_add_mcast_twice(void)721 static void test_v6_addr_add_mcast_twice(void)
722 {
723 	struct net_if_mcast_addr *maddr;
724 
725 	maddr = net_if_ipv6_maddr_add(iface1, &in6addr_mcast);
726 	zassert_equal(maddr, NULL, "Address was added twice");
727 }
728 
test_v6_addr_lookup(void)729 static void test_v6_addr_lookup(void)
730 {
731 	int ret;
732 
733 	ret = net_if_ipv6_addr_lookup_by_index(&my_ipv6_addr_test);
734 	zassert_equal(ret, 1, "IPv6 address not found");
735 
736 	ret = net_if_ipv6_addr_lookup_by_index(&my_ipv6_addr_not_found);
737 	zassert_not_equal(ret, 1, "IPv6 address found");
738 }
739 
test_v6_addr_rm(void)740 static void test_v6_addr_rm(void)
741 {
742 	bool ret;
743 
744 	ret = net_if_ipv6_addr_rm_by_index(1, &my_ipv6_addr_test);
745 	zassert_true(ret, "Cannot remove IPv6 address");
746 }
747 
748 #define MY_ADDR_V6_USER { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, \
749 			      0, 0, 0, 0, 0, 0, 0, 0x65 } } }
750 
751 #define UNKNOWN_ADDR_V6_USER { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, \
752 			      0, 0, 0, 0, 0, 0, 0, 0x66 } } }
753 
test_v6_addr_add_user(void)754 static void test_v6_addr_add_user(void)
755 {
756 	struct in6_addr my_addr = MY_ADDR_V6_USER;
757 	bool ret;
758 
759 	ret = net_if_ipv6_addr_add_by_index(1, &my_addr, NET_ADDR_MANUAL, 0);
760 	zassert_true(ret, "Could not add IPv6 address");
761 }
762 
test_v6_addr_add_user_from_userspace(void)763 static void test_v6_addr_add_user_from_userspace(void)
764 {
765 	k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
766 	k_thread_user_mode_enter((k_thread_entry_t)test_v6_addr_add_user, NULL,
767 				 NULL, NULL);
768 }
769 
test_v6_addr_lookup_user(void)770 static void test_v6_addr_lookup_user(void)
771 {
772 	struct in6_addr my_addr = MY_ADDR_V6_USER;
773 	struct in6_addr unknown_addr = UNKNOWN_ADDR_V6_USER;
774 	int ret;
775 
776 	ret = net_if_ipv6_addr_lookup_by_index(&my_addr);
777 	zassert_equal(ret, 1, "IPv6 address not found (%d)", ret);
778 
779 	ret = net_if_ipv6_addr_lookup_by_index(&unknown_addr);
780 	zassert_equal(ret, 0, "IPv6 address found");
781 }
782 
test_v6_addr_rm_user(void)783 static void test_v6_addr_rm_user(void)
784 {
785 	struct in6_addr my_addr = MY_ADDR_V6_USER;
786 	bool ret;
787 
788 	/* Check also that add is enabled so that we can remove something
789 	 * that was already added.
790 	 */
791 	ret = net_if_ipv6_addr_rm_by_index(1, &my_addr);
792 	zassert_true(ret, "Cannot remove IPv6 address");
793 }
794 
test_v6_addr_rm_user_from_userspace(void)795 static void test_v6_addr_rm_user_from_userspace(void)
796 {
797 	k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
798 	k_thread_user_mode_enter((k_thread_entry_t)test_v6_addr_rm_user, NULL,
799 				 NULL, NULL);
800 }
801 
test_netmask_addr_add(void)802 static void test_netmask_addr_add(void)
803 {
804 	struct in_addr my_netmask = { { { 255, 255, 255, 0 } } };
805 	bool ret;
806 
807 	ret = net_if_ipv4_set_netmask_by_index(1, &my_netmask);
808 	zassert_true(ret, "Cannot add IPv4 netmask");
809 }
810 
test_netmask_addr_add_from_userspace(void)811 static void test_netmask_addr_add_from_userspace(void)
812 {
813 	k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
814 	k_thread_user_mode_enter((k_thread_entry_t)test_netmask_addr_add, NULL,
815 				 NULL, NULL);
816 }
817 
test_gw_addr_add(void)818 static void test_gw_addr_add(void)
819 {
820 	struct in_addr my_gw = { { { 192, 0, 2, 254 } } };
821 	bool ret;
822 
823 	ret = net_if_ipv4_set_gw_by_index(1, &my_gw);
824 	zassert_true(ret, "Cannot add IPv4 gateway");
825 }
826 
test_gw_addr_add_from_userspace(void)827 static void test_gw_addr_add_from_userspace(void)
828 {
829 	k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
830 	k_thread_user_mode_enter((k_thread_entry_t)test_gw_addr_add, NULL,
831 				 NULL, NULL);
832 }
833 
test_get_by_index(void)834 static void test_get_by_index(void)
835 {
836 	zassert_not_null(net_if_get_by_index(1),
837 			 "Cannot get interface at index 1");
838 }
839 
test_get_by_index_from_userspace(void)840 static void test_get_by_index_from_userspace(void)
841 {
842 	k_thread_access_grant(k_current_get(), net_if_get_by_index(1));
843 	k_thread_user_mode_enter((k_thread_entry_t)test_get_by_index, NULL,
844 				 NULL, NULL);
845 }
846 
test_main(void)847 void test_main(void)
848 {
849 	ztest_test_suite(net_iface_test,
850 			 ztest_unit_test(test_iface_setup),
851 			 ztest_unit_test(test_send_iface1),
852 			 ztest_unit_test(test_send_iface2),
853 			 ztest_unit_test(test_send_iface3),
854 			 ztest_unit_test(test_send_iface1_down),
855 			 ztest_unit_test(test_send_iface1_up),
856 			 ztest_unit_test(test_select_src_iface),
857 			 ztest_unit_test(test_check_promisc_mode_off),
858 			 ztest_unit_test(test_set_promisc_mode_on),
859 			 ztest_unit_test(test_check_promisc_mode_on),
860 			 ztest_unit_test(test_set_promisc_mode_on_again),
861 			 ztest_unit_test(test_set_promisc_mode_off),
862 			 ztest_unit_test(test_check_promisc_mode_off),
863 			 ztest_unit_test(test_v4_addr_add),
864 			 ztest_unit_test(test_v4_addr_lookup),
865 			 ztest_unit_test(test_v4_addr_rm),
866 			 ztest_unit_test(test_v4_addr_add_user_from_userspace),
867 			 ztest_user_unit_test(test_v4_addr_lookup_user),
868 			 ztest_unit_test(test_v4_addr_rm_user_from_userspace),
869 			 ztest_unit_test(test_v6_addr_add),
870 			 ztest_unit_test(test_v6_addr_add_mcast_twice),
871 			 ztest_unit_test(test_v6_addr_lookup),
872 			 ztest_unit_test(test_v6_addr_rm),
873 			 ztest_unit_test(test_v6_addr_add_user_from_userspace),
874 			 ztest_user_unit_test(test_v6_addr_lookup_user),
875 			 ztest_unit_test(test_v6_addr_rm_user_from_userspace),
876 			 ztest_unit_test(test_netmask_addr_add),
877 			 ztest_unit_test(test_netmask_addr_add_from_userspace),
878 			 ztest_unit_test(test_gw_addr_add),
879 			 ztest_unit_test(test_gw_addr_add_from_userspace),
880 			 ztest_unit_test(test_get_by_index),
881 			 ztest_unit_test(test_get_by_index_from_userspace)
882 		);
883 
884 	ztest_run_test_suite(net_iface_test);
885 }
886