1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2021 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define NET_LOG_LEVEL CONFIG_NET_L2_VIRTUAL_LOG_LEVEL
10 
11 #include <zephyr/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 <zephyr/sys/printk.h>
20 #include <zephyr/random/random.h>
21 
22 #include <zephyr/ztest.h>
23 
24 #include <zephyr/net/dummy.h>
25 #include <zephyr/net_buf.h>
26 #include <zephyr/net/net_ip.h>
27 #include <zephyr/net/virtual.h>
28 #include <zephyr/net/virtual_mgmt.h>
29 #include <zephyr/net/ethernet.h>
30 #include <zephyr/net/net_l2.h>
31 
32 #include "ipv4.h"
33 #include "ipv6.h"
34 #include "udp_internal.h"
35 
36 bool arp_add(struct net_if *iface, struct in_addr *src,
37 	     struct net_eth_addr *hwaddr);
38 
39 #define NET_LOG_ENABLED 1
40 #include "net_private.h"
41 
42 #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
43 #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
44 #else
45 #define DBG(fmt, ...)
46 #endif
47 
48 #define PKT_ALLOC_TIME K_MSEC(50)
49 #define TEST_PORT 9999
50 
51 static char *test_data = "Test data to be sent";
52 
53 /* Interface 1 addresses */
54 static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
55 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
56 static struct in_addr my_addr = { { { 192, 0, 2, 1 } } };
57 
58 /* Interface 2 addresses */
59 static struct in6_addr my_addr2 = { { { 0x20, 0x01, 0x0d, 0xb8, 2, 0, 0, 0,
60 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
61 
62 /* Interface 3 addresses */
63 static struct in6_addr my_addr3 = { { { 0x20, 0x01, 0x0d, 0xb8, 3, 0, 0, 0,
64 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
65 
66 /* Extra address is assigned to ll_addr */
67 static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
68 				       0, 0, 0, 0xf2, 0xaa, 0x29, 0x02,
69 				       0x04 } } };
70 
71 struct sockaddr virtual_addr;
72 struct sockaddr peer_addr;
73 
74 #define MTU 1024
75 
76 /* Keep track of all virtual interfaces */
77 static struct net_if *virtual_interfaces[1];
78 static struct net_if *eth_interfaces[2];
79 static struct net_if *dummy_interfaces[2];
80 
81 static struct net_context *udp_ctx;
82 
83 static bool test_failed;
84 static bool test_started;
85 static bool data_received;
86 
87 static K_SEM_DEFINE(wait_data, 0, UINT_MAX);
88 
89 #define WAIT_TIME K_SECONDS(1)
90 
91 struct eth_context {
92 	struct net_if *iface;
93 	uint8_t mac_addr[6];
94 };
95 
96 static struct eth_context eth_context;
97 static uint8_t expecting_outer;
98 static uint8_t expecting_inner;
99 static int header_len;
100 
eth_iface_init(struct net_if * iface)101 static void eth_iface_init(struct net_if *iface)
102 {
103 	const struct device *dev = net_if_get_device(iface);
104 	struct eth_context *context = dev->data;
105 
106 	net_if_set_link_addr(iface, context->mac_addr,
107 			     sizeof(context->mac_addr),
108 			     NET_LINK_ETHERNET);
109 
110 	ethernet_init(iface);
111 }
112 
eth_tx(const struct device * dev,struct net_pkt * pkt)113 static int eth_tx(const struct device *dev, struct net_pkt *pkt)
114 {
115 	struct eth_context *context = dev->data;
116 
117 	zassert_equal_ptr(&eth_context, context,
118 			  "Context pointers do not match (%p vs %p)",
119 			  eth_context, context);
120 
121 	if (!pkt->buffer) {
122 		DBG("No data to send!\n");
123 		return -ENODATA;
124 	}
125 
126 	if (test_started) {
127 		uint8_t outer, inner;
128 		int ret;
129 
130 		net_pkt_set_overwrite(pkt, true);
131 
132 		net_pkt_hexdump(pkt, "pkt");
133 		net_pkt_skip(pkt, sizeof(struct net_eth_hdr));
134 
135 		ret = net_pkt_read_u8(pkt, &outer);
136 		zassert_equal(ret, 0, "Cannot read outer protocol type");
137 		zassert_equal(outer, expecting_outer,
138 			      "Unexpected outer protocol 0x%02x, "
139 			      "expecting 0x%02x",
140 			      outer, expecting_outer);
141 		net_pkt_skip(pkt, header_len - 1);
142 
143 		ret = net_pkt_read_u8(pkt, &inner);
144 		zassert_equal(ret, 0, "Cannot read inner protocol type");
145 		zassert_equal(inner, expecting_inner,
146 			      "Unexpected inner protocol 0x%02x, "
147 			      "expecting 0x%02x",
148 			      inner, expecting_inner);
149 
150 		k_sem_give(&wait_data);
151 	}
152 
153 	net_pkt_unref(pkt);
154 
155 	return 0;
156 }
157 
eth_capabilities(const struct device * dev)158 static enum ethernet_hw_caps eth_capabilities(const struct device *dev)
159 {
160 	return 0;
161 }
162 
163 static struct ethernet_api api_funcs = {
164 	.iface_api.init = eth_iface_init,
165 
166 	.get_capabilities = eth_capabilities,
167 	.send = eth_tx,
168 };
169 
generate_mac(uint8_t * mac_addr)170 static void generate_mac(uint8_t *mac_addr)
171 {
172 	/* 00-00-5E-00-53-xx Documentation RFC 7042 */
173 	mac_addr[0] = 0x00;
174 	mac_addr[1] = 0x00;
175 	mac_addr[2] = 0x5E;
176 	mac_addr[3] = 0x00;
177 	mac_addr[4] = 0x53;
178 	mac_addr[5] = sys_rand8_get();
179 }
180 
eth_init(const struct device * dev)181 static int eth_init(const struct device *dev)
182 {
183 	struct eth_context *context = dev->data;
184 
185 	generate_mac(context->mac_addr);
186 
187 	return 0;
188 }
189 
190 ETH_NET_DEVICE_INIT(eth_test, "eth_test",
191 		    eth_init, NULL,
192 		    &eth_context, NULL, CONFIG_ETH_INIT_PRIORITY,
193 		    &api_funcs, NET_ETH_MTU);
194 
195 struct net_if_test {
196 	uint8_t idx; /* not used for anything, just a dummy value */
197 	uint8_t mac_addr[sizeof(struct net_eth_addr)];
198 	struct net_linkaddr ll_addr;
199 };
200 
net_iface_get_mac(const struct device * dev)201 static uint8_t *net_iface_get_mac(const struct device *dev)
202 {
203 	struct net_if_test *data = dev->data;
204 
205 	if (data->mac_addr[2] == 0x00) {
206 		/* 00-00-5E-00-53-xx Documentation RFC 7042 */
207 		data->mac_addr[0] = 0x00;
208 		data->mac_addr[1] = 0x00;
209 		data->mac_addr[2] = 0x5E;
210 		data->mac_addr[3] = 0x00;
211 		data->mac_addr[4] = 0x53;
212 		data->mac_addr[5] = sys_rand8_get();
213 	}
214 
215 	data->ll_addr.addr = data->mac_addr;
216 	data->ll_addr.len = 6U;
217 
218 	return data->mac_addr;
219 }
220 
net_iface_init(struct net_if * iface)221 static void net_iface_init(struct net_if *iface)
222 {
223 	uint8_t *mac = net_iface_get_mac(net_if_get_device(iface));
224 
225 	net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr),
226 			     NET_LINK_ETHERNET);
227 }
228 
sender_iface(const struct device * dev,struct net_pkt * pkt)229 static int sender_iface(const struct device *dev, struct net_pkt *pkt)
230 {
231 	return 0;
232 }
233 
234 struct net_if_test net_iface1_data;
235 struct net_if_test net_iface2_data;
236 
237 static struct dummy_api net_iface_api = {
238 	.iface_api.init = net_iface_init,
239 	.send = sender_iface,
240 };
241 
242 /* For testing purposes, create two dummy network interfaces so we can check
243  * that attaching virtual interface work ok.
244  */
245 NET_DEVICE_INIT_INSTANCE(eth_test_dummy1,
246 			 "iface1",
247 			 iface1,
248 			 NULL,
249 			 NULL,
250 			 &net_iface1_data,
251 			 NULL,
252 			 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
253 			 &net_iface_api,
254 			 DUMMY_L2,
255 			 NET_L2_GET_CTX_TYPE(DUMMY_L2),
256 			 127);
257 
258 NET_DEVICE_INIT_INSTANCE(eth_test_dummy2,
259 			 "iface2",
260 			 iface2,
261 			 NULL,
262 			 NULL,
263 			 &net_iface2_data,
264 			 NULL,
265 			 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
266 			 &net_iface_api,
267 			 DUMMY_L2,
268 			 NET_L2_GET_CTX_TYPE(DUMMY_L2),
269 			 127);
270 
271 struct user_data {
272 	int eth_if_count;
273 	int dummy_if_count;
274 	int virtual_if_count;
275 	int total_if_count;
276 };
277 
278 #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
iface2str(struct net_if * iface)279 static const char *iface2str(struct net_if *iface)
280 {
281 	if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
282 		return "Ethernet";
283 	}
284 
285 	if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
286 		return "Dummy";
287 	}
288 
289 	if (net_if_l2(iface) == &NET_L2_GET_NAME(VIRTUAL)) {
290 		return "Virtual";
291 	}
292 
293 	return "<unknown type>";
294 }
295 #endif
296 
iface_cb(struct net_if * iface,void * user_data)297 static void iface_cb(struct net_if *iface, void *user_data)
298 {
299 	struct user_data *ud = user_data;
300 	static int starting_eth_idx = 1;
301 
302 	/*
303 	 * The below code is to only use struct net_if devices defined in this
304 	 * test as board on which it is run can have its own set of interfaces.
305 	 *
306 	 * As a result one will not rely on linker's specific 'net_if_area'
307 	 * placement.
308 	 */
309 	if ((iface != net_if_lookup_by_dev(DEVICE_GET(eth_test_dummy1))) &&
310 	    (iface != net_if_lookup_by_dev(DEVICE_GET(eth_test_dummy2))) &&
311 	    (iface != net_if_lookup_by_dev(DEVICE_GET(eth_test))) &&
312 	    (net_if_l2(iface) != &NET_L2_GET_NAME(VIRTUAL))) {
313 		return;
314 	}
315 
316 	DBG("Interface %p (%s) [%d]\n", iface, iface2str(iface),
317 	    net_if_get_by_iface(iface));
318 
319 	if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
320 		if (PART_OF_ARRAY(NET_IF_GET_NAME(eth_test, 0), iface)) {
321 			if (!eth_interfaces[0]) {
322 				/* Just use the first interface */
323 				eth_interfaces[0] = iface;
324 				ud->eth_if_count++;
325 			}
326 		} else {
327 			if (ud->eth_if_count > ARRAY_SIZE(eth_interfaces)) {
328 				goto out;
329 			}
330 
331 			eth_interfaces[starting_eth_idx++] = iface;
332 			ud->eth_if_count++;
333 		}
334 	}
335 
336 	if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
337 		dummy_interfaces[ud->dummy_if_count++] = iface;
338 
339 		zassert_true(ud->dummy_if_count <= 2,
340 			     "Too many dummy interfaces");
341 	}
342 
343 	if (net_if_l2(iface) == &NET_L2_GET_NAME(VIRTUAL)) {
344 		virtual_interfaces[ud->virtual_if_count++] = iface;
345 
346 		zassert_true(ud->virtual_if_count <= 3,
347 			     "Too many virtual interfaces");
348 	} else {
349 		/* By default all interfaces are down initially */
350 		/* Virtual interfaces are down initially */
351 		net_if_down(iface);
352 	}
353 
354 out:
355 	ud->total_if_count++;
356 }
357 
test_virtual_setup(void)358 static void test_virtual_setup(void)
359 {
360 	struct user_data ud = { 0 };
361 
362 	/* Make sure we have enough virtual interfaces */
363 	net_if_foreach(iface_cb, &ud);
364 
365 	zassert_equal(ud.virtual_if_count, ARRAY_SIZE(virtual_interfaces),
366 		      "Invalid number of virtual interfaces, "
367 		      "was %d should be %zu",
368 		      ud.virtual_if_count, ARRAY_SIZE(virtual_interfaces));
369 
370 	zassert_true(ud.eth_if_count <= ARRAY_SIZE(eth_interfaces),
371 		      "Invalid number of eth interfaces, "
372 		      "was %d should be %zu",
373 		      ud.eth_if_count, ARRAY_SIZE(eth_interfaces));
374 
375 	zassert_equal(ud.dummy_if_count, ARRAY_SIZE(dummy_interfaces),
376 		      "Invalid number of dummy interfaces, "
377 		      "was %d should be %zu",
378 		      ud.dummy_if_count, ARRAY_SIZE(dummy_interfaces));
379 }
380 
test_address_setup(void)381 static void test_address_setup(void)
382 {
383 	struct in_addr netmask = {{{ 255, 255, 255, 0 }}};
384 	struct net_if_addr *ifaddr;
385 	struct net_if *eth, *virt, *dummy1, *dummy2;
386 	int ret;
387 
388 	eth = eth_interfaces[0];
389 	virt = virtual_interfaces[0];
390 	dummy1 = dummy_interfaces[0];
391 	dummy2 = dummy_interfaces[1];
392 
393 	zassert_not_null(eth, "Eth Interface");
394 	zassert_not_null(virt, "Virtual Interface");
395 	zassert_not_null(dummy1, "Dummy Interface 1");
396 	zassert_not_null(dummy2, "Dummy Interface 2");
397 
398 	ifaddr = net_if_ipv6_addr_add(eth, &my_addr1, NET_ADDR_MANUAL, 0);
399 	if (!ifaddr) {
400 		DBG("Cannot add IPv6 address %s\n",
401 		    net_sprint_ipv6_addr(&my_addr1));
402 		zassert_not_null(ifaddr, "eth addr");
403 	}
404 
405 	/* For testing purposes we need to set the addresses preferred */
406 	ifaddr->addr_state = NET_ADDR_PREFERRED;
407 
408 	ifaddr = net_if_ipv4_addr_add(eth, &my_addr, NET_ADDR_MANUAL, 0);
409 	if (!ifaddr) {
410 		DBG("Cannot add IPv4 address %s\n",
411 		    net_sprint_ipv4_addr(&my_addr));
412 		zassert_not_null(ifaddr, "eth addr");
413 	}
414 
415 	ifaddr->addr_state = NET_ADDR_PREFERRED;
416 
417 	net_if_ipv4_set_netmask_by_addr(eth, &my_addr, &netmask);
418 
419 	ifaddr = net_if_ipv6_addr_add(eth, &ll_addr, NET_ADDR_MANUAL, 0);
420 	if (!ifaddr) {
421 		DBG("Cannot add IPv6 address %s\n",
422 		    net_sprint_ipv6_addr(&ll_addr));
423 		zassert_not_null(ifaddr, "ll_addr");
424 	}
425 
426 	ifaddr->addr_state = NET_ADDR_PREFERRED;
427 
428 	ifaddr = net_if_ipv6_addr_add(virt, &my_addr2, NET_ADDR_MANUAL, 0);
429 	if (!ifaddr) {
430 		DBG("Cannot add IPv6 address %s\n",
431 		    net_sprint_ipv6_addr(&my_addr2));
432 		zassert_not_null(ifaddr, "virt addr");
433 	}
434 
435 	ifaddr->addr_state = NET_ADDR_PREFERRED;
436 
437 	ifaddr = net_if_ipv6_addr_add(dummy1, &my_addr3, NET_ADDR_MANUAL, 0);
438 	if (!ifaddr) {
439 		DBG("Cannot add IPv6 address %s\n",
440 		       net_sprint_ipv6_addr(&my_addr3));
441 		zassert_not_null(ifaddr, "dummy1 addr");
442 	}
443 
444 	net_if_up(eth);
445 	net_if_up(dummy1);
446 	net_if_up(dummy2);
447 
448 	/* Set the virtual interface addresses */
449 	ret = net_ipaddr_parse(CONFIG_NET_TEST_TUNNEL_MY_ADDR,
450 			       strlen(CONFIG_NET_TEST_TUNNEL_MY_ADDR),
451 			       &virtual_addr);
452 	zassert_equal(ret, 1, "Cannot parse \"%s\"",
453 		      CONFIG_NET_TEST_TUNNEL_MY_ADDR);
454 
455 	if (virtual_addr.sa_family == AF_INET) {
456 		ifaddr = net_if_ipv4_addr_add(virt,
457 					&net_sin(&virtual_addr)->sin_addr,
458 					NET_ADDR_MANUAL, 0);
459 		if (!ifaddr) {
460 			DBG("Cannot add IPv4 address %s\n",
461 			    net_sprint_ipv4_addr(
462 				    &net_sin(&virtual_addr)->sin_addr));
463 			zassert_not_null(ifaddr, "virt addr");
464 		}
465 
466 		net_sin(&virtual_addr)->sin_port = htons(4242);
467 
468 		net_if_ipv4_set_netmask_by_addr(virt,
469 						&net_sin(&virtual_addr)->sin_addr,
470 						&netmask);
471 
472 	} else if (virtual_addr.sa_family == AF_INET6) {
473 		ifaddr = net_if_ipv6_addr_add(virt,
474 					&net_sin6(&virtual_addr)->sin6_addr,
475 					NET_ADDR_MANUAL, 0);
476 		if (!ifaddr) {
477 			DBG("Cannot add IPv6 address %s\n",
478 			    net_sprint_ipv6_addr(
479 				    &net_sin6(&virtual_addr)->sin6_addr));
480 			zassert_not_null(ifaddr, "virt addr");
481 		}
482 
483 		net_sin6(&virtual_addr)->sin6_port = htons(4242);
484 	} else {
485 		zassert_not_null(NULL, "Invalid address family (%d)",
486 				 virtual_addr.sa_family);
487 	}
488 
489 	ifaddr->addr_state = NET_ADDR_PREFERRED;
490 
491 	ret = net_ipaddr_parse(CONFIG_NET_TEST_TUNNEL_PEER_ADDR,
492 			       strlen(CONFIG_NET_TEST_TUNNEL_PEER_ADDR),
493 			       &peer_addr);
494 	zassert_equal(ret, 1, "Cannot parse \"%s\"",
495 		      CONFIG_NET_TEST_TUNNEL_PEER_ADDR);
496 
497 	/* The interface might receive data which might fail the checks
498 	 * in the iface sending function, so we need to reset the failure
499 	 * flag.
500 	 */
501 	test_failed = false;
502 }
503 
add_neighbor(struct net_if * iface,struct in6_addr * addr)504 static bool add_neighbor(struct net_if *iface, struct in6_addr *addr)
505 {
506 	struct net_linkaddr_storage llstorage;
507 	struct net_linkaddr lladdr;
508 	struct net_nbr *nbr;
509 
510 	llstorage.addr[0] = 0x01;
511 	llstorage.addr[1] = 0x02;
512 	llstorage.addr[2] = 0x33;
513 	llstorage.addr[3] = 0x44;
514 	llstorage.addr[4] = 0x05;
515 	llstorage.addr[5] = 0x06;
516 
517 	lladdr.len = 6U;
518 	lladdr.addr = llstorage.addr;
519 	lladdr.type = NET_LINK_ETHERNET;
520 
521 	nbr = net_ipv6_nbr_add(iface, addr, &lladdr, false,
522 			       NET_IPV6_NBR_STATE_REACHABLE);
523 	if (!nbr) {
524 		DBG("Cannot add dst %s to neighbor cache\n",
525 		    net_sprint_ipv6_addr(addr));
526 		return false;
527 	}
528 
529 	return true;
530 }
531 
add_to_arp(struct net_if * iface,struct in_addr * addr)532 static bool add_to_arp(struct net_if *iface, struct in_addr *addr)
533 {
534 #if defined(CONFIG_NET_ARP)
535 	struct net_eth_addr lladdr;
536 
537 	lladdr.addr[0] = sys_rand8_get();
538 	lladdr.addr[1] = 0x08;
539 	lladdr.addr[2] = 0x09;
540 	lladdr.addr[3] = 0x10;
541 	lladdr.addr[4] = 0x11;
542 	lladdr.addr[5] = sys_rand8_get();
543 
544 	return arp_add(iface, addr, &lladdr);
545 #else
546 	ARG_UNUSED(iface);
547 	ARG_UNUSED(addr);
548 
549 	return true;
550 #endif
551 }
552 
ZTEST(net_virtual,test_virtual_01_attach_and_detach)553 ZTEST(net_virtual, test_virtual_01_attach_and_detach)
554 {
555 	struct net_if *iface = virtual_interfaces[0];
556 	int ret;
557 
558 	/* Attach virtual interface on top of Ethernet */
559 
560 	ret = net_virtual_interface_attach(iface, eth_interfaces[0]);
561 	zassert_equal(ret, 0, "Cannot attach %d on top of %d (%d)",
562 		      net_if_get_by_iface(iface),
563 		      net_if_get_by_iface(eth_interfaces[0]),
564 		      ret);
565 
566 	zassert_false(net_if_is_up(iface),
567 		      "Virtual interface %d should be down",
568 		      net_if_get_by_iface(iface));
569 
570 	ret = net_if_up(iface);
571 	zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
572 		      net_if_get_by_iface(iface), ret);
573 
574 	ret = net_virtual_interface_attach(iface,
575 					   NULL);
576 	zassert_equal(ret, 0, "Cannot deattach %d from %d (%d)",
577 		      net_if_get_by_iface(iface),
578 		      net_if_get_by_iface(eth_interfaces[0]),
579 		      ret);
580 
581 	zassert_false(net_if_is_up(iface), "Virtual interface %d is still up",
582 		      net_if_get_by_iface(iface));
583 }
584 
ZTEST(net_virtual,test_virtual_02_real_iface_down)585 ZTEST(net_virtual, test_virtual_02_real_iface_down)
586 {
587 	struct net_if *iface = virtual_interfaces[0];
588 	int ret;
589 
590 	/* Attach virtual interface on top of Ethernet */
591 
592 	ret = net_virtual_interface_attach(iface, eth_interfaces[0]);
593 	zassert_equal(ret, 0, "Cannot attach %d on top of %d (%d)",
594 		      net_if_get_by_iface(iface),
595 		      net_if_get_by_iface(eth_interfaces[0]),
596 		      ret);
597 
598 	zassert_false(net_if_is_up(iface),
599 		      "Virtual interface %d should be down",
600 		      net_if_get_by_iface(iface));
601 
602 	ret = net_if_up(iface);
603 	zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
604 		      net_if_get_by_iface(iface), ret);
605 
606 	zassert_true(net_if_is_up(iface),
607 		     "Virtual interface %d should be up",
608 		     net_if_get_by_iface(iface));
609 	zassert_true(net_if_is_up(eth_interfaces[0]),
610 		     "Real interface %d should be up",
611 		     net_if_get_by_iface(iface));
612 
613 	/* Virtual interface should go down if the underlying iface is down */
614 	ret = net_if_down(eth_interfaces[0]);
615 	zassert_equal(ret, 0, "Cannot take real interface %d down (%d)",
616 		      net_if_get_by_iface(eth_interfaces[0]), ret);
617 
618 	zassert_false(net_if_is_up(iface),
619 		      "Virtual interface %d should be down",
620 		      net_if_get_by_iface(iface));
621 	zassert_false(net_if_is_carrier_ok(iface),
622 		      "Virtual interface %d should be in carrier off",
623 		      net_if_get_by_iface(iface));
624 	zassert_equal(net_if_oper_state(iface), NET_IF_OPER_LOWERLAYERDOWN,
625 		      "Wrong operational state on %d (%d)",
626 		      net_if_get_by_iface(iface), net_if_oper_state(iface));
627 
628 	/* Virtual interface should be brought up if the underlying iface is
629 	 * back up
630 	 */
631 	ret = net_if_up(eth_interfaces[0]);
632 	zassert_equal(ret, 0, "Cannot take real interface %d u (%d)",
633 		      net_if_get_by_iface(eth_interfaces[0]), ret);
634 
635 	zassert_true(net_if_is_up(iface),
636 		     "Virtual interface %d should be up",
637 		     net_if_get_by_iface(iface));
638 	zassert_true(net_if_is_carrier_ok(iface),
639 		     "Virtual interface %d should be in carrier on",
640 		     net_if_get_by_iface(iface));
641 
642 	ret = net_virtual_interface_attach(iface,
643 					   NULL);
644 	zassert_equal(ret, 0, "Cannot deattach %d from %d (%d)",
645 		      net_if_get_by_iface(iface),
646 		      net_if_get_by_iface(eth_interfaces[0]),
647 		      ret);
648 
649 	zassert_false(net_if_is_up(iface), "Virtual interface %d is still up",
650 		      net_if_get_by_iface(iface));
651 }
652 
653 
ZTEST(net_virtual,test_virtual_03_set_mtu)654 ZTEST(net_virtual, test_virtual_03_set_mtu)
655 {
656 	struct virtual_interface_req_params params = { 0 };
657 	struct net_if *iface = virtual_interfaces[0];
658 	int ret;
659 
660 	ret = net_if_up(iface);
661 	zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
662 		      net_if_get_by_iface(iface), ret);
663 
664 	params.mtu = MTU;
665 
666 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_MTU,
667 		       iface, &params, sizeof(params));
668 	zassert_equal(ret, -EACCES, "Could set interface %d MTU to %d (%d)",
669 		      net_if_get_by_iface(iface), params.mtu, ret);
670 
671 	ret = net_if_down(iface);
672 	zassert_equal(ret, 0, "Cannot take virtual interface %d down (%d)",
673 		      net_if_get_by_iface(iface), ret);
674 
675 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_MTU,
676 		       iface, &params, sizeof(params));
677 	zassert_equal(ret, 0, "Cannot set interface %d MTU to %d (%d)",
678 		      net_if_get_by_iface(iface), params.mtu, ret);
679 }
680 
ZTEST(net_virtual,test_virtual_04_get_mtu)681 ZTEST(net_virtual, test_virtual_04_get_mtu)
682 {
683 	struct virtual_interface_req_params params = { 0 };
684 	struct net_if *iface = virtual_interfaces[0];
685 	int ret;
686 
687 	params.mtu = 0;
688 
689 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_GET_MTU,
690 		       iface, &params, sizeof(params));
691 	zassert_equal(ret, 0, "Cannot get interface %d MTU (%d)",
692 		      net_if_get_by_iface(iface), params.mtu, ret);
693 
694 	zassert_equal(params.mtu, MTU,
695 		      "MTU mismatch from interface %d, got %d should be %d",
696 		      net_if_get_by_iface(iface), params.mtu, MTU);
697 }
698 
ZTEST(net_virtual,test_virtual_05_set_peer)699 ZTEST(net_virtual, test_virtual_05_set_peer)
700 {
701 	struct virtual_interface_req_params params = { 0 };
702 	struct net_if *iface = virtual_interfaces[0];
703 	int ret;
704 
705 	ret = net_if_up(iface);
706 	zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
707 		      net_if_get_by_iface(iface), ret);
708 
709 	params.family = peer_addr.sa_family;
710 	if (params.family == AF_INET) {
711 		net_ipaddr_copy(&params.peer4addr,
712 				&net_sin(&peer_addr)->sin_addr);
713 	} else if (params.family == AF_INET6) {
714 		net_ipaddr_copy(&params.peer6addr,
715 				&net_sin6(&peer_addr)->sin6_addr);
716 	} else {
717 		zassert_true(false, "Invalid family (%d)", params.family);
718 	}
719 
720 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_PEER_ADDRESS,
721 		       iface, &params, sizeof(params));
722 	zassert_equal(ret, -EACCES, "Could set interface %d peer to %s (%d)",
723 		      net_if_get_by_iface(iface),
724 		      CONFIG_NET_TEST_TUNNEL_PEER_ADDR, ret);
725 
726 	ret = net_if_down(iface);
727 	zassert_equal(ret, 0, "Cannot take virtual interface %d down (%d)",
728 		      net_if_get_by_iface(iface), ret);
729 
730 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_PEER_ADDRESS,
731 		       iface, &params, sizeof(params));
732 	zassert_equal(ret, 0, "Cannot set interface %d peer to %s (%d)",
733 		      net_if_get_by_iface(iface),
734 		      CONFIG_NET_TEST_TUNNEL_PEER_ADDR, ret);
735 
736 	/* We should be attached now */
737 	ret = net_virtual_interface_attach(iface, dummy_interfaces[0]);
738 	zassert_equal(ret, -EALREADY, "Could attach %d on top of %d (%d)",
739 		      net_if_get_by_iface(iface),
740 		      net_if_get_by_iface(dummy_interfaces[0]),
741 		      ret);
742 
743 	ret = net_virtual_interface_attach(iface, NULL);
744 	zassert_equal(ret, 0, "Cannot deattach %d from %d (%d)",
745 		      net_if_get_by_iface(iface),
746 		      net_if_get_by_iface(eth_interfaces[0]),
747 		      ret);
748 }
749 
ZTEST(net_virtual,test_virtual_06_get_peer)750 ZTEST(net_virtual, test_virtual_06_get_peer)
751 {
752 	struct virtual_interface_req_params params = { 0 };
753 	struct net_if *iface = virtual_interfaces[0];
754 	int ret;
755 
756 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_GET_PEER_ADDRESS,
757 		       iface, &params, sizeof(params));
758 	zassert_equal(ret, 0, "Cannot get interface %d peer (%d)",
759 		      net_if_get_by_iface(iface), ret);
760 
761 	zassert_equal(params.family, peer_addr.sa_family,
762 		      "Invalid family, should be %d was %d",
763 		      peer_addr.sa_family, params.family);
764 	if (params.family == AF_INET) {
765 		zassert_mem_equal(&params.peer4addr,
766 				  &net_sin(&peer_addr)->sin_addr,
767 				  sizeof(struct in_addr),
768 				  "Peer IPv4 address invalid");
769 	} else if (params.family == AF_INET6) {
770 		zassert_mem_equal(&params.peer6addr,
771 				  &net_sin6(&peer_addr)->sin6_addr,
772 				  sizeof(struct in6_addr),
773 				  "Peer IPv6 address invalid");
774 	} else {
775 		zassert_true(false, "Invalid family (%d)", params.family);
776 	}
777 }
778 
ZTEST(net_virtual,test_virtual_07_verify_name)779 ZTEST(net_virtual, test_virtual_07_verify_name)
780 {
781 #define NAME "foobar"
782 #define NAME2 "123456789"
783 	struct net_if *iface = virtual_interfaces[0];
784 	char *tmp = NAME;
785 	char buf[sizeof(NAME2)];
786 	char *name;
787 
788 	net_virtual_set_name(iface, NAME);
789 	name = net_virtual_get_name(iface, buf, sizeof(buf));
790 	zassert_mem_equal(name, tmp, strlen(name), "Cannot get name");
791 
792 	/* Check that the string is truncated */
793 	tmp = NAME2;
794 	net_virtual_set_name(iface, tmp);
795 	name = net_virtual_get_name(iface, buf, sizeof(buf));
796 	zassert_mem_equal(name, tmp, strlen(name), "Cannot get name");
797 	zassert_mem_equal(name, tmp, strlen(tmp) -
798 			  (sizeof(NAME2) - CONFIG_NET_L2_VIRTUAL_MAX_NAME_LEN),
799 			  "Cannot get name");
800 }
801 
ZTEST(net_virtual,test_virtual_08_detach)802 ZTEST(net_virtual, test_virtual_08_detach)
803 {
804 	struct net_if *iface = virtual_interfaces[0];
805 	int ret;
806 
807 	ret = net_virtual_interface_attach(iface, NULL);
808 	zassert_true((ret == 0) || (ret == -EALREADY),
809 		     "Cannot deattach %d from %d (%d)",
810 		     net_if_get_by_iface(iface),
811 		     net_if_get_by_iface(eth_interfaces[0]),
812 		     ret);
813 }
814 
ZTEST(net_virtual,test_virtual_08_send_data_to_tunnel)815 ZTEST(net_virtual, test_virtual_08_send_data_to_tunnel)
816 {
817 	struct virtual_interface_req_params params = { 0 };
818 	struct net_if *iface = virtual_interfaces[0];
819 	struct net_if *attached = eth_interfaces[0];
820 	struct sockaddr dst_addr, src_addr;
821 	void *addr;
822 	int addrlen;
823 	int ret;
824 
825 	params.family = peer_addr.sa_family;
826 	if (params.family == AF_INET) {
827 		net_ipaddr_copy(&params.peer4addr,
828 				&net_sin(&peer_addr)->sin_addr);
829 		expecting_outer = 0x45;
830 		header_len = sizeof(struct net_ipv4_hdr);
831 
832 		ret = add_to_arp(eth_interfaces[0],
833 				 &net_sin(&peer_addr)->sin_addr);
834 		zassert_true(ret, "Cannot add to arp");
835 	} else if (params.family == AF_INET6) {
836 		net_ipaddr_copy(&params.peer6addr,
837 				&net_sin6(&peer_addr)->sin6_addr);
838 		expecting_outer = 0x60;
839 		header_len = sizeof(struct net_ipv6_hdr);
840 
841 		ret = add_neighbor(eth_interfaces[0],
842 				   &net_sin6(&peer_addr)->sin6_addr);
843 		zassert_true(ret, "Cannot add neighbor");
844 	} else {
845 		zassert_true(false, "Invalid family (%d)", params.family);
846 	}
847 
848 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_PEER_ADDRESS,
849 		       iface, &params, sizeof(params));
850 	zassert_equal(ret, 0, "Cannot set interface %d peer to %s (%d)",
851 		      net_if_get_by_iface(iface),
852 		      CONFIG_NET_TEST_TUNNEL_PEER_ADDR, ret);
853 
854 	net_virtual_set_name(iface, CONFIG_NET_TEST_TUNNEL_NAME);
855 
856 	attached = net_virtual_get_iface(iface);
857 	zassert_equal(eth_interfaces[0], attached,
858 		      "Not attached to Ethernet interface");
859 
860 	ret = net_if_up(iface);
861 	zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
862 		      net_if_get_by_iface(iface), ret);
863 
864 	memcpy(&dst_addr, &virtual_addr, sizeof(dst_addr));
865 	memcpy(&src_addr, &virtual_addr, sizeof(src_addr));
866 
867 	if (dst_addr.sa_family == AF_INET) {
868 		net_sin(&dst_addr)->sin_addr.s4_addr[3] = 2;
869 
870 		addr = &src_addr;
871 		addrlen = sizeof(struct sockaddr_in);
872 
873 		expecting_inner = 0x45; /* IPv4 */
874 
875 	} else if (dst_addr.sa_family == AF_INET6) {
876 		net_sin6(&dst_addr)->sin6_addr.s6_addr[15] = 2;
877 
878 		addr = &src_addr;
879 		addrlen = sizeof(struct sockaddr_in6);
880 
881 		expecting_inner = 0x60; /* IPv6 */
882 	} else {
883 		zassert_true(false, "Invalid family (%d)", dst_addr.sa_family);
884 		addrlen = 0;
885 	}
886 
887 	ret = net_context_get(virtual_addr.sa_family, SOCK_DGRAM, IPPROTO_UDP,
888 			      &udp_ctx);
889 	zassert_equal(ret, 0, "Create IP UDP context failed");
890 
891 	ret = net_context_bind(udp_ctx, (struct sockaddr *)addr, addrlen);
892 	zassert_equal(ret, 0, "Context bind failure test failed");
893 
894 	test_started = true;
895 
896 	ret = net_context_sendto(udp_ctx, test_data, strlen(test_data),
897 				 &dst_addr, addrlen,
898 				 NULL, K_NO_WAIT, NULL);
899 	zassert_true(ret > 0, "Send UDP pkt failed");
900 
901 	if (k_sem_take(&wait_data, WAIT_TIME)) {
902 		DBG("Timeout while waiting interface data\n");
903 		zassert_false(true, "Timeout");
904 	}
905 
906 	net_context_unref(udp_ctx);
907 }
908 
create_outer(struct net_if * iface,sa_family_t family,enum net_ip_protocol proto,size_t inner_len,size_t outer_len)909 static struct net_pkt *create_outer(struct net_if *iface,
910 				    sa_family_t family,
911 				    enum net_ip_protocol proto,
912 				    size_t inner_len,
913 				    size_t outer_len)
914 {
915 	return net_pkt_alloc_with_buffer(iface, inner_len + outer_len,
916 					 family, proto, PKT_ALLOC_TIME);
917 }
918 
create_inner(struct net_if * iface,sa_family_t family,enum net_ip_protocol proto,size_t inner_len,size_t data_len)919 static struct net_pkt *create_inner(struct net_if *iface,
920 				    sa_family_t family,
921 				    enum net_ip_protocol proto,
922 				    size_t inner_len,
923 				    size_t data_len)
924 {
925 	return net_pkt_alloc_with_buffer(iface, inner_len + data_len,
926 					 family, proto, PKT_ALLOC_TIME);
927 }
928 
recv_data(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)929 static void recv_data(struct net_context *context,
930 		      struct net_pkt *pkt,
931 		      union net_ip_header *ip_hdr,
932 		      union net_proto_header *proto_hdr,
933 		      int status,
934 		      void *user_data)
935 {
936 	data_received = true;
937 }
938 
test_virtual_recv_data_from_tunnel(int remote_ip,bool expected_ok)939 static void test_virtual_recv_data_from_tunnel(int remote_ip,
940 					       bool expected_ok)
941 {
942 	struct net_if *iface = virtual_interfaces[0];
943 	struct net_if *attached = eth_interfaces[0];
944 	struct sockaddr dst_addr, src_addr, inner_src;
945 	struct in_addr *outerv4, *innerv4;
946 	struct in6_addr *outerv6, *innerv6;
947 	size_t inner_len = sizeof(struct net_udp_hdr) +
948 		strlen(test_data);
949 	struct net_pkt *outer, *inner;
950 	enum net_verdict verdict;
951 	uint16_t src_port = 4242, dst_port = 4242;
952 	uint8_t next_header;
953 	size_t addrlen;
954 	int ret;
955 
956 	memcpy(&dst_addr, &peer_addr, sizeof(dst_addr));
957 	memcpy(&src_addr, &peer_addr, sizeof(src_addr));
958 	memcpy(&inner_src, &virtual_addr, sizeof(inner_src));
959 
960 	if (peer_addr.sa_family == AF_INET) {
961 		net_sin(&dst_addr)->sin_addr.s4_addr[3] = 1;
962 		net_sin(&src_addr)->sin_addr.s4_addr[3] = remote_ip;
963 		outerv4 = &net_sin(&peer_addr)->sin_addr;
964 	} else {
965 		net_sin6(&dst_addr)->sin6_addr.s6_addr[15] = 1;
966 		net_sin6(&src_addr)->sin6_addr.s6_addr[15] = remote_ip;
967 		outerv6 = &net_sin6(&peer_addr)->sin6_addr;
968 	}
969 
970 	if (virtual_addr.sa_family == AF_INET) {
971 		net_sin(&inner_src)->sin_addr.s4_addr[3] = 2;
972 		innerv4 = &net_sin(&virtual_addr)->sin_addr;
973 		inner_len += sizeof(struct net_ipv4_hdr);
974 	} else {
975 		net_sin6(&inner_src)->sin6_addr.s6_addr[15] = 2;
976 		innerv6 = &net_sin6(&virtual_addr)->sin6_addr;
977 		inner_len += sizeof(struct net_ipv6_hdr);
978 	}
979 
980 	if (peer_addr.sa_family == AF_INET) {
981 		outer = create_outer(attached, AF_INET, IPPROTO_IP,
982 				     sizeof(struct net_ipv4_hdr), 0);
983 		zassert_not_null(outer, "Cannot allocate %s pkt", outer);
984 
985 		ret = net_ipv4_create(outer, &net_sin(&src_addr)->sin_addr,
986 				      &net_sin(&dst_addr)->sin_addr);
987 		zassert_equal(ret, 0, "Cannot create %s packet (%d)", "IPv4",
988 			      ret);
989 	} else {
990 		outer = create_outer(attached, AF_INET6, IPPROTO_IPV6,
991 				     sizeof(struct net_ipv6_hdr), 0);
992 		zassert_not_null(outer, "Cannot allocate %s pkt", outer);
993 
994 		ret = net_ipv6_create(outer, &net_sin6(&src_addr)->sin6_addr,
995 				      &net_sin6(&dst_addr)->sin6_addr);
996 		zassert_equal(ret, 0, "Cannot create %s packet (%d)", "IPv6",
997 			      ret);
998 	}
999 
1000 	if (virtual_addr.sa_family == AF_INET) {
1001 		inner = create_inner(iface, AF_INET, IPPROTO_IP,
1002 				     sizeof(struct net_ipv4_hdr),
1003 				     sizeof(struct net_udp_hdr) +
1004 				     strlen(test_data));
1005 		zassert_not_null(inner, "Cannot allocate %s pkt", inner);
1006 
1007 		ret = net_ipv4_create(inner, &net_sin(&inner_src)->sin_addr,
1008 				      innerv4);
1009 		zassert_equal(ret, 0, "Cannot create outer %s (%d)", "IPv4",
1010 			      ret);
1011 		next_header = IPPROTO_IPIP;
1012 		addrlen = sizeof(struct sockaddr_in);
1013 	} else {
1014 		inner = create_inner(iface, AF_INET6, IPPROTO_IPV6,
1015 				     sizeof(struct net_ipv6_hdr),
1016 				     sizeof(struct net_udp_hdr) +
1017 				     strlen(test_data));
1018 		zassert_not_null(inner, "Cannot allocate %s pkt", inner);
1019 
1020 		ret = net_ipv6_create(inner, &net_sin6(&inner_src)->sin6_addr,
1021 				      innerv6);
1022 		zassert_equal(ret, 0, "Cannot create outer %s (%d)", "IPv6",
1023 			      ret);
1024 		next_header = IPPROTO_IPV6;
1025 		addrlen = sizeof(struct sockaddr_in6);
1026 	}
1027 
1028 	ret = net_udp_create(inner, htons(src_port), htons(dst_port));
1029 	zassert_equal(ret, 0, "Cannot create UDP (%d)", ret);
1030 
1031 	net_pkt_write(inner, test_data, strlen(test_data));
1032 
1033 	net_pkt_cursor_init(inner);
1034 
1035 	if (virtual_addr.sa_family == AF_INET) {
1036 		net_ipv4_finalize(inner, IPPROTO_UDP);
1037 	} else {
1038 		net_ipv6_finalize(inner, IPPROTO_UDP);
1039 	}
1040 
1041 	net_buf_frag_add(outer->buffer, inner->buffer);
1042 	inner->buffer = NULL;
1043 	net_pkt_unref(inner);
1044 
1045 	net_pkt_cursor_init(outer);
1046 
1047 	if (peer_addr.sa_family == AF_INET) {
1048 		net_ipv4_finalize(outer, next_header);
1049 	} else {
1050 		net_ipv6_finalize(outer, next_header);
1051 	}
1052 
1053 	ret = net_context_get(virtual_addr.sa_family, SOCK_DGRAM, IPPROTO_UDP,
1054 			      &udp_ctx);
1055 	zassert_equal(ret, 0, "Create IP UDP context failed");
1056 
1057 	net_context_set_iface(udp_ctx, iface);
1058 
1059 	ret = net_context_bind(udp_ctx, (struct sockaddr *)&virtual_addr,
1060 			       addrlen);
1061 	zassert_equal(ret, 0, "Context bind failure test failed");
1062 
1063 	test_started = true;
1064 	data_received = false;
1065 
1066 	ret = net_context_recv(udp_ctx, recv_data, K_NO_WAIT, &wait_data);
1067 	zassert_equal(ret, 0, "UDP recv failed");
1068 
1069 	net_pkt_cursor_init(outer);
1070 
1071 	if (peer_addr.sa_family == AF_INET) {
1072 		verdict = net_ipv4_input(outer, false);
1073 	} else {
1074 		verdict = net_ipv6_input(outer, false);
1075 	}
1076 
1077 	if (expected_ok) {
1078 		zassert_equal(verdict, NET_OK,
1079 			      "Packet not accepted (%d)",
1080 			      verdict);
1081 	} else {
1082 		zassert_equal(verdict, NET_DROP,
1083 			      "Packet not dropped (%d)",
1084 			      verdict);
1085 	}
1086 
1087 	net_context_put(udp_ctx);
1088 }
1089 
ZTEST(net_virtual,test_virtual_09_recv_data_from_tunnel_ok)1090 ZTEST(net_virtual, test_virtual_09_recv_data_from_tunnel_ok)
1091 {
1092 	test_virtual_recv_data_from_tunnel(2, true);
1093 }
1094 
ZTEST(net_virtual,test_virtual_10_recv_data_from_tunnel_fail)1095 ZTEST(net_virtual, test_virtual_10_recv_data_from_tunnel_fail)
1096 {
1097 	test_virtual_recv_data_from_tunnel(3, false);
1098 }
1099 
setup(void)1100 static void *setup(void)
1101 {
1102 	test_virtual_setup();
1103 	test_address_setup();
1104 	return NULL;
1105 }
1106 
1107 ZTEST_SUITE(net_virtual, NULL, setup, NULL, NULL, NULL);
1108