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 	memcpy(data->ll_addr.addr, data->mac_addr, sizeof(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 lladdr;
507 	struct net_nbr *nbr;
508 
509 	lladdr.addr[0] = 0x01;
510 	lladdr.addr[1] = 0x02;
511 	lladdr.addr[2] = 0x33;
512 	lladdr.addr[3] = 0x44;
513 	lladdr.addr[4] = 0x05;
514 	lladdr.addr[5] = 0x06;
515 
516 	lladdr.len = 6U;
517 	lladdr.type = NET_LINK_ETHERNET;
518 
519 	nbr = net_ipv6_nbr_add(iface, addr, &lladdr, false,
520 			       NET_IPV6_NBR_STATE_REACHABLE);
521 	if (!nbr) {
522 		DBG("Cannot add dst %s to neighbor cache\n",
523 		    net_sprint_ipv6_addr(addr));
524 		return false;
525 	}
526 
527 	return true;
528 }
529 
add_to_arp(struct net_if * iface,struct in_addr * addr)530 static bool add_to_arp(struct net_if *iface, struct in_addr *addr)
531 {
532 #if defined(CONFIG_NET_ARP)
533 	struct net_eth_addr lladdr;
534 
535 	lladdr.addr[0] = sys_rand8_get();
536 	lladdr.addr[1] = 0x08;
537 	lladdr.addr[2] = 0x09;
538 	lladdr.addr[3] = 0x10;
539 	lladdr.addr[4] = 0x11;
540 	lladdr.addr[5] = sys_rand8_get();
541 
542 	return arp_add(iface, addr, &lladdr);
543 #else
544 	ARG_UNUSED(iface);
545 	ARG_UNUSED(addr);
546 
547 	return true;
548 #endif
549 }
550 
ZTEST(net_virtual,test_virtual_01_attach_and_detach)551 ZTEST(net_virtual, test_virtual_01_attach_and_detach)
552 {
553 	struct net_if *iface = virtual_interfaces[0];
554 	int ret;
555 
556 	/* Attach virtual interface on top of Ethernet */
557 
558 	ret = net_virtual_interface_attach(iface, eth_interfaces[0]);
559 	zassert_equal(ret, 0, "Cannot attach %d on top of %d (%d)",
560 		      net_if_get_by_iface(iface),
561 		      net_if_get_by_iface(eth_interfaces[0]),
562 		      ret);
563 
564 	zassert_false(net_if_is_up(iface),
565 		      "Virtual interface %d should be down",
566 		      net_if_get_by_iface(iface));
567 
568 	ret = net_if_up(iface);
569 	zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
570 		      net_if_get_by_iface(iface), ret);
571 
572 	ret = net_virtual_interface_attach(iface,
573 					   NULL);
574 	zassert_equal(ret, 0, "Cannot deattach %d from %d (%d)",
575 		      net_if_get_by_iface(iface),
576 		      net_if_get_by_iface(eth_interfaces[0]),
577 		      ret);
578 
579 	zassert_false(net_if_is_up(iface), "Virtual interface %d is still up",
580 		      net_if_get_by_iface(iface));
581 }
582 
ZTEST(net_virtual,test_virtual_02_real_iface_down)583 ZTEST(net_virtual, test_virtual_02_real_iface_down)
584 {
585 	struct net_if *iface = virtual_interfaces[0];
586 	int ret;
587 
588 	/* Attach virtual interface on top of Ethernet */
589 
590 	ret = net_virtual_interface_attach(iface, eth_interfaces[0]);
591 	zassert_equal(ret, 0, "Cannot attach %d on top of %d (%d)",
592 		      net_if_get_by_iface(iface),
593 		      net_if_get_by_iface(eth_interfaces[0]),
594 		      ret);
595 
596 	zassert_false(net_if_is_up(iface),
597 		      "Virtual interface %d should be down",
598 		      net_if_get_by_iface(iface));
599 
600 	ret = net_if_up(iface);
601 	zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
602 		      net_if_get_by_iface(iface), ret);
603 
604 	zassert_true(net_if_is_up(iface),
605 		     "Virtual interface %d should be up",
606 		     net_if_get_by_iface(iface));
607 	zassert_true(net_if_is_up(eth_interfaces[0]),
608 		     "Real interface %d should be up",
609 		     net_if_get_by_iface(iface));
610 
611 	/* Virtual interface should go down if the underlying iface is down */
612 	ret = net_if_down(eth_interfaces[0]);
613 	zassert_equal(ret, 0, "Cannot take real interface %d down (%d)",
614 		      net_if_get_by_iface(eth_interfaces[0]), ret);
615 
616 	zassert_false(net_if_is_up(iface),
617 		      "Virtual interface %d should be down",
618 		      net_if_get_by_iface(iface));
619 	zassert_false(net_if_is_carrier_ok(iface),
620 		      "Virtual interface %d should be in carrier off",
621 		      net_if_get_by_iface(iface));
622 	zassert_equal(net_if_oper_state(iface), NET_IF_OPER_LOWERLAYERDOWN,
623 		      "Wrong operational state on %d (%d)",
624 		      net_if_get_by_iface(iface), net_if_oper_state(iface));
625 
626 	/* Virtual interface should be brought up if the underlying iface is
627 	 * back up
628 	 */
629 	ret = net_if_up(eth_interfaces[0]);
630 	zassert_equal(ret, 0, "Cannot take real interface %d u (%d)",
631 		      net_if_get_by_iface(eth_interfaces[0]), ret);
632 
633 	zassert_true(net_if_is_up(iface),
634 		     "Virtual interface %d should be up",
635 		     net_if_get_by_iface(iface));
636 	zassert_true(net_if_is_carrier_ok(iface),
637 		     "Virtual interface %d should be in carrier on",
638 		     net_if_get_by_iface(iface));
639 
640 	ret = net_virtual_interface_attach(iface,
641 					   NULL);
642 	zassert_equal(ret, 0, "Cannot deattach %d from %d (%d)",
643 		      net_if_get_by_iface(iface),
644 		      net_if_get_by_iface(eth_interfaces[0]),
645 		      ret);
646 
647 	zassert_false(net_if_is_up(iface), "Virtual interface %d is still up",
648 		      net_if_get_by_iface(iface));
649 }
650 
651 
ZTEST(net_virtual,test_virtual_03_set_mtu)652 ZTEST(net_virtual, test_virtual_03_set_mtu)
653 {
654 	struct virtual_interface_req_params params = { 0 };
655 	struct net_if *iface = virtual_interfaces[0];
656 	int ret;
657 
658 	ret = net_if_up(iface);
659 	zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
660 		      net_if_get_by_iface(iface), ret);
661 
662 	params.mtu = MTU;
663 
664 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_MTU,
665 		       iface, &params, sizeof(params));
666 	zassert_equal(ret, -EACCES, "Could set interface %d MTU to %d (%d)",
667 		      net_if_get_by_iface(iface), params.mtu, ret);
668 
669 	ret = net_if_down(iface);
670 	zassert_equal(ret, 0, "Cannot take virtual interface %d down (%d)",
671 		      net_if_get_by_iface(iface), ret);
672 
673 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_MTU,
674 		       iface, &params, sizeof(params));
675 	zassert_equal(ret, 0, "Cannot set interface %d MTU to %d (%d)",
676 		      net_if_get_by_iface(iface), params.mtu, ret);
677 }
678 
ZTEST(net_virtual,test_virtual_04_get_mtu)679 ZTEST(net_virtual, test_virtual_04_get_mtu)
680 {
681 	struct virtual_interface_req_params params = { 0 };
682 	struct net_if *iface = virtual_interfaces[0];
683 	int ret;
684 
685 	params.mtu = 0;
686 
687 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_GET_MTU,
688 		       iface, &params, sizeof(params));
689 	zassert_equal(ret, 0, "Cannot get interface %d MTU (%d)",
690 		      net_if_get_by_iface(iface), params.mtu, ret);
691 
692 	zassert_equal(params.mtu, MTU,
693 		      "MTU mismatch from interface %d, got %d should be %d",
694 		      net_if_get_by_iface(iface), params.mtu, MTU);
695 }
696 
ZTEST(net_virtual,test_virtual_05_set_peer)697 ZTEST(net_virtual, test_virtual_05_set_peer)
698 {
699 	struct virtual_interface_req_params params = { 0 };
700 	struct net_if *iface = virtual_interfaces[0];
701 	int ret;
702 
703 	ret = net_if_up(iface);
704 	zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
705 		      net_if_get_by_iface(iface), ret);
706 
707 	params.family = peer_addr.sa_family;
708 	if (params.family == AF_INET) {
709 		net_ipaddr_copy(&params.peer4addr,
710 				&net_sin(&peer_addr)->sin_addr);
711 	} else if (params.family == AF_INET6) {
712 		net_ipaddr_copy(&params.peer6addr,
713 				&net_sin6(&peer_addr)->sin6_addr);
714 	} else {
715 		zassert_true(false, "Invalid family (%d)", params.family);
716 	}
717 
718 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_PEER_ADDRESS,
719 		       iface, &params, sizeof(params));
720 	zassert_equal(ret, -EACCES, "Could set interface %d peer to %s (%d)",
721 		      net_if_get_by_iface(iface),
722 		      CONFIG_NET_TEST_TUNNEL_PEER_ADDR, ret);
723 
724 	ret = net_if_down(iface);
725 	zassert_equal(ret, 0, "Cannot take virtual interface %d down (%d)",
726 		      net_if_get_by_iface(iface), ret);
727 
728 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_PEER_ADDRESS,
729 		       iface, &params, sizeof(params));
730 	zassert_equal(ret, 0, "Cannot set interface %d peer to %s (%d)",
731 		      net_if_get_by_iface(iface),
732 		      CONFIG_NET_TEST_TUNNEL_PEER_ADDR, ret);
733 
734 	/* We should be attached now */
735 	ret = net_virtual_interface_attach(iface, dummy_interfaces[0]);
736 	zassert_equal(ret, -EALREADY, "Could attach %d on top of %d (%d)",
737 		      net_if_get_by_iface(iface),
738 		      net_if_get_by_iface(dummy_interfaces[0]),
739 		      ret);
740 
741 	ret = net_virtual_interface_attach(iface, NULL);
742 	zassert_equal(ret, 0, "Cannot deattach %d from %d (%d)",
743 		      net_if_get_by_iface(iface),
744 		      net_if_get_by_iface(eth_interfaces[0]),
745 		      ret);
746 }
747 
ZTEST(net_virtual,test_virtual_06_get_peer)748 ZTEST(net_virtual, test_virtual_06_get_peer)
749 {
750 	struct virtual_interface_req_params params = { 0 };
751 	struct net_if *iface = virtual_interfaces[0];
752 	int ret;
753 
754 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_GET_PEER_ADDRESS,
755 		       iface, &params, sizeof(params));
756 	zassert_equal(ret, 0, "Cannot get interface %d peer (%d)",
757 		      net_if_get_by_iface(iface), ret);
758 
759 	zassert_equal(params.family, peer_addr.sa_family,
760 		      "Invalid family, should be %d was %d",
761 		      peer_addr.sa_family, params.family);
762 	if (params.family == AF_INET) {
763 		zassert_mem_equal(&params.peer4addr,
764 				  &net_sin(&peer_addr)->sin_addr,
765 				  sizeof(struct in_addr),
766 				  "Peer IPv4 address invalid");
767 	} else if (params.family == AF_INET6) {
768 		zassert_mem_equal(&params.peer6addr,
769 				  &net_sin6(&peer_addr)->sin6_addr,
770 				  sizeof(struct in6_addr),
771 				  "Peer IPv6 address invalid");
772 	} else {
773 		zassert_true(false, "Invalid family (%d)", params.family);
774 	}
775 }
776 
ZTEST(net_virtual,test_virtual_07_verify_name)777 ZTEST(net_virtual, test_virtual_07_verify_name)
778 {
779 #define NAME "foobar"
780 #define NAME2 "123456789"
781 	struct net_if *iface = virtual_interfaces[0];
782 	char *tmp = NAME;
783 	char buf[sizeof(NAME2)];
784 	char *name;
785 
786 	net_virtual_set_name(iface, NAME);
787 	name = net_virtual_get_name(iface, buf, sizeof(buf));
788 	zassert_mem_equal(name, tmp, strlen(name), "Cannot get name");
789 
790 	/* Check that the string is truncated */
791 	tmp = NAME2;
792 	net_virtual_set_name(iface, tmp);
793 	name = net_virtual_get_name(iface, buf, sizeof(buf));
794 	zassert_mem_equal(name, tmp, strlen(name), "Cannot get name");
795 	zassert_mem_equal(name, tmp, strlen(tmp) -
796 			  (sizeof(NAME2) - CONFIG_NET_L2_VIRTUAL_MAX_NAME_LEN),
797 			  "Cannot get name");
798 }
799 
ZTEST(net_virtual,test_virtual_08_detach)800 ZTEST(net_virtual, test_virtual_08_detach)
801 {
802 	struct net_if *iface = virtual_interfaces[0];
803 	int ret;
804 
805 	ret = net_virtual_interface_attach(iface, NULL);
806 	zassert_true((ret == 0) || (ret == -EALREADY),
807 		     "Cannot deattach %d from %d (%d)",
808 		     net_if_get_by_iface(iface),
809 		     net_if_get_by_iface(eth_interfaces[0]),
810 		     ret);
811 }
812 
ZTEST(net_virtual,test_virtual_08_send_data_to_tunnel)813 ZTEST(net_virtual, test_virtual_08_send_data_to_tunnel)
814 {
815 	struct virtual_interface_req_params params = { 0 };
816 	struct net_if *iface = virtual_interfaces[0];
817 	struct net_if *attached = eth_interfaces[0];
818 	struct sockaddr dst_addr, src_addr;
819 	void *addr;
820 	int addrlen;
821 	int ret;
822 
823 	params.family = peer_addr.sa_family;
824 	if (params.family == AF_INET) {
825 		net_ipaddr_copy(&params.peer4addr,
826 				&net_sin(&peer_addr)->sin_addr);
827 		expecting_outer = 0x45;
828 		header_len = sizeof(struct net_ipv4_hdr);
829 
830 		ret = add_to_arp(eth_interfaces[0],
831 				 &net_sin(&peer_addr)->sin_addr);
832 		zassert_true(ret, "Cannot add to arp");
833 	} else if (params.family == AF_INET6) {
834 		net_ipaddr_copy(&params.peer6addr,
835 				&net_sin6(&peer_addr)->sin6_addr);
836 		expecting_outer = 0x60;
837 		header_len = sizeof(struct net_ipv6_hdr);
838 
839 		ret = add_neighbor(eth_interfaces[0],
840 				   &net_sin6(&peer_addr)->sin6_addr);
841 		zassert_true(ret, "Cannot add neighbor");
842 	} else {
843 		zassert_true(false, "Invalid family (%d)", params.family);
844 	}
845 
846 	ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_PEER_ADDRESS,
847 		       iface, &params, sizeof(params));
848 	zassert_equal(ret, 0, "Cannot set interface %d peer to %s (%d)",
849 		      net_if_get_by_iface(iface),
850 		      CONFIG_NET_TEST_TUNNEL_PEER_ADDR, ret);
851 
852 	net_virtual_set_name(iface, CONFIG_NET_TEST_TUNNEL_NAME);
853 
854 	attached = net_virtual_get_iface(iface);
855 	zassert_equal(eth_interfaces[0], attached,
856 		      "Not attached to Ethernet interface");
857 
858 	ret = net_if_up(iface);
859 	zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
860 		      net_if_get_by_iface(iface), ret);
861 
862 	memcpy(&dst_addr, &virtual_addr, sizeof(dst_addr));
863 	memcpy(&src_addr, &virtual_addr, sizeof(src_addr));
864 
865 	if (dst_addr.sa_family == AF_INET) {
866 		net_sin(&dst_addr)->sin_addr.s4_addr[3] = 2;
867 
868 		addr = &src_addr;
869 		addrlen = sizeof(struct sockaddr_in);
870 
871 		expecting_inner = 0x45; /* IPv4 */
872 
873 	} else if (dst_addr.sa_family == AF_INET6) {
874 		net_sin6(&dst_addr)->sin6_addr.s6_addr[15] = 2;
875 
876 		addr = &src_addr;
877 		addrlen = sizeof(struct sockaddr_in6);
878 
879 		expecting_inner = 0x60; /* IPv6 */
880 	} else {
881 		zassert_true(false, "Invalid family (%d)", dst_addr.sa_family);
882 		addrlen = 0;
883 	}
884 
885 	ret = net_context_get(virtual_addr.sa_family, SOCK_DGRAM, IPPROTO_UDP,
886 			      &udp_ctx);
887 	zassert_equal(ret, 0, "Create IP UDP context failed");
888 
889 	ret = net_context_bind(udp_ctx, (struct sockaddr *)addr, addrlen);
890 	zassert_equal(ret, 0, "Context bind failure test failed");
891 
892 	test_started = true;
893 
894 	ret = net_context_sendto(udp_ctx, test_data, strlen(test_data),
895 				 &dst_addr, addrlen,
896 				 NULL, K_NO_WAIT, NULL);
897 	zassert_true(ret > 0, "Send UDP pkt failed");
898 
899 	if (k_sem_take(&wait_data, WAIT_TIME)) {
900 		DBG("Timeout while waiting interface data\n");
901 		zassert_false(true, "Timeout");
902 	}
903 
904 	net_context_unref(udp_ctx);
905 }
906 
create_outer(struct net_if * iface,sa_family_t family,enum net_ip_protocol proto,size_t inner_len,size_t outer_len)907 static struct net_pkt *create_outer(struct net_if *iface,
908 				    sa_family_t family,
909 				    enum net_ip_protocol proto,
910 				    size_t inner_len,
911 				    size_t outer_len)
912 {
913 	return net_pkt_alloc_with_buffer(iface, inner_len + outer_len,
914 					 family, proto, PKT_ALLOC_TIME);
915 }
916 
create_inner(struct net_if * iface,sa_family_t family,enum net_ip_protocol proto,size_t inner_len,size_t data_len)917 static struct net_pkt *create_inner(struct net_if *iface,
918 				    sa_family_t family,
919 				    enum net_ip_protocol proto,
920 				    size_t inner_len,
921 				    size_t data_len)
922 {
923 	return net_pkt_alloc_with_buffer(iface, inner_len + data_len,
924 					 family, proto, PKT_ALLOC_TIME);
925 }
926 
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)927 static void recv_data(struct net_context *context,
928 		      struct net_pkt *pkt,
929 		      union net_ip_header *ip_hdr,
930 		      union net_proto_header *proto_hdr,
931 		      int status,
932 		      void *user_data)
933 {
934 	data_received = true;
935 }
936 
test_virtual_recv_data_from_tunnel(int remote_ip,bool expected_ok)937 static void test_virtual_recv_data_from_tunnel(int remote_ip,
938 					       bool expected_ok)
939 {
940 	struct net_if *iface = virtual_interfaces[0];
941 	struct net_if *attached = eth_interfaces[0];
942 	struct sockaddr dst_addr, src_addr, inner_src;
943 	struct in_addr *outerv4, *innerv4;
944 	struct in6_addr *outerv6, *innerv6;
945 	size_t inner_len = sizeof(struct net_udp_hdr) +
946 		strlen(test_data);
947 	struct net_pkt *outer, *inner;
948 	enum net_verdict verdict;
949 	uint16_t src_port = 4242, dst_port = 4242;
950 	uint8_t next_header;
951 	size_t addrlen;
952 	int ret;
953 
954 	memcpy(&dst_addr, &peer_addr, sizeof(dst_addr));
955 	memcpy(&src_addr, &peer_addr, sizeof(src_addr));
956 	memcpy(&inner_src, &virtual_addr, sizeof(inner_src));
957 
958 	if (peer_addr.sa_family == AF_INET) {
959 		net_sin(&dst_addr)->sin_addr.s4_addr[3] = 1;
960 		net_sin(&src_addr)->sin_addr.s4_addr[3] = remote_ip;
961 		outerv4 = &net_sin(&peer_addr)->sin_addr;
962 	} else {
963 		net_sin6(&dst_addr)->sin6_addr.s6_addr[15] = 1;
964 		net_sin6(&src_addr)->sin6_addr.s6_addr[15] = remote_ip;
965 		outerv6 = &net_sin6(&peer_addr)->sin6_addr;
966 	}
967 
968 	if (virtual_addr.sa_family == AF_INET) {
969 		net_sin(&inner_src)->sin_addr.s4_addr[3] = 2;
970 		innerv4 = &net_sin(&virtual_addr)->sin_addr;
971 		inner_len += sizeof(struct net_ipv4_hdr);
972 	} else {
973 		net_sin6(&inner_src)->sin6_addr.s6_addr[15] = 2;
974 		innerv6 = &net_sin6(&virtual_addr)->sin6_addr;
975 		inner_len += sizeof(struct net_ipv6_hdr);
976 	}
977 
978 	if (peer_addr.sa_family == AF_INET) {
979 		outer = create_outer(attached, AF_INET, IPPROTO_IP,
980 				     sizeof(struct net_ipv4_hdr), 0);
981 		zassert_not_null(outer, "Cannot allocate %s pkt", outer);
982 
983 		ret = net_ipv4_create(outer, &net_sin(&src_addr)->sin_addr,
984 				      &net_sin(&dst_addr)->sin_addr);
985 		zassert_equal(ret, 0, "Cannot create %s packet (%d)", "IPv4",
986 			      ret);
987 	} else {
988 		outer = create_outer(attached, AF_INET6, IPPROTO_IPV6,
989 				     sizeof(struct net_ipv6_hdr), 0);
990 		zassert_not_null(outer, "Cannot allocate %s pkt", outer);
991 
992 		ret = net_ipv6_create(outer, &net_sin6(&src_addr)->sin6_addr,
993 				      &net_sin6(&dst_addr)->sin6_addr);
994 		zassert_equal(ret, 0, "Cannot create %s packet (%d)", "IPv6",
995 			      ret);
996 	}
997 
998 	if (virtual_addr.sa_family == AF_INET) {
999 		inner = create_inner(iface, AF_INET, IPPROTO_IP,
1000 				     sizeof(struct net_ipv4_hdr),
1001 				     sizeof(struct net_udp_hdr) +
1002 				     strlen(test_data));
1003 		zassert_not_null(inner, "Cannot allocate %s pkt", inner);
1004 
1005 		ret = net_ipv4_create(inner, &net_sin(&inner_src)->sin_addr,
1006 				      innerv4);
1007 		zassert_equal(ret, 0, "Cannot create outer %s (%d)", "IPv4",
1008 			      ret);
1009 		next_header = IPPROTO_IPIP;
1010 		addrlen = sizeof(struct sockaddr_in);
1011 	} else {
1012 		inner = create_inner(iface, AF_INET6, IPPROTO_IPV6,
1013 				     sizeof(struct net_ipv6_hdr),
1014 				     sizeof(struct net_udp_hdr) +
1015 				     strlen(test_data));
1016 		zassert_not_null(inner, "Cannot allocate %s pkt", inner);
1017 
1018 		ret = net_ipv6_create(inner, &net_sin6(&inner_src)->sin6_addr,
1019 				      innerv6);
1020 		zassert_equal(ret, 0, "Cannot create outer %s (%d)", "IPv6",
1021 			      ret);
1022 		next_header = IPPROTO_IPV6;
1023 		addrlen = sizeof(struct sockaddr_in6);
1024 	}
1025 
1026 	ret = net_udp_create(inner, htons(src_port), htons(dst_port));
1027 	zassert_equal(ret, 0, "Cannot create UDP (%d)", ret);
1028 
1029 	net_pkt_write(inner, test_data, strlen(test_data));
1030 
1031 	net_pkt_cursor_init(inner);
1032 
1033 	if (virtual_addr.sa_family == AF_INET) {
1034 		net_ipv4_finalize(inner, IPPROTO_UDP);
1035 	} else {
1036 		net_ipv6_finalize(inner, IPPROTO_UDP);
1037 	}
1038 
1039 	net_buf_frag_add(outer->buffer, inner->buffer);
1040 	inner->buffer = NULL;
1041 	net_pkt_unref(inner);
1042 
1043 	net_pkt_cursor_init(outer);
1044 
1045 	if (peer_addr.sa_family == AF_INET) {
1046 		net_ipv4_finalize(outer, next_header);
1047 	} else {
1048 		net_ipv6_finalize(outer, next_header);
1049 	}
1050 
1051 	ret = net_context_get(virtual_addr.sa_family, SOCK_DGRAM, IPPROTO_UDP,
1052 			      &udp_ctx);
1053 	zassert_equal(ret, 0, "Create IP UDP context failed");
1054 
1055 	net_context_set_iface(udp_ctx, iface);
1056 
1057 	ret = net_context_bind(udp_ctx, (struct sockaddr *)&virtual_addr,
1058 			       addrlen);
1059 	zassert_equal(ret, 0, "Context bind failure test failed");
1060 
1061 	test_started = true;
1062 	data_received = false;
1063 
1064 	ret = net_context_recv(udp_ctx, recv_data, K_NO_WAIT, &wait_data);
1065 	zassert_equal(ret, 0, "UDP recv failed");
1066 
1067 	net_pkt_cursor_init(outer);
1068 
1069 	if (peer_addr.sa_family == AF_INET) {
1070 		verdict = net_ipv4_input(outer, false);
1071 	} else {
1072 		verdict = net_ipv6_input(outer, false);
1073 	}
1074 
1075 	if (expected_ok) {
1076 		zassert_equal(verdict, NET_OK,
1077 			      "Packet not accepted (%d)",
1078 			      verdict);
1079 	} else {
1080 		zassert_equal(verdict, NET_DROP,
1081 			      "Packet not dropped (%d)",
1082 			      verdict);
1083 	}
1084 
1085 	net_context_put(udp_ctx);
1086 }
1087 
ZTEST(net_virtual,test_virtual_09_recv_data_from_tunnel_ok)1088 ZTEST(net_virtual, test_virtual_09_recv_data_from_tunnel_ok)
1089 {
1090 	test_virtual_recv_data_from_tunnel(2, true);
1091 }
1092 
ZTEST(net_virtual,test_virtual_10_recv_data_from_tunnel_fail)1093 ZTEST(net_virtual, test_virtual_10_recv_data_from_tunnel_fail)
1094 {
1095 	test_virtual_recv_data_from_tunnel(3, false);
1096 }
1097 
setup(void)1098 static void *setup(void)
1099 {
1100 	test_virtual_setup();
1101 	test_address_setup();
1102 	return NULL;
1103 }
1104 
1105 ZTEST_SUITE(net_virtual, NULL, setup, NULL, NULL, NULL);
1106