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(ð_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_rand32_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 ð_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_rand32_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 DBG("Interface %p (%s) [%d]\n", iface, iface2str(iface),
316 net_if_get_by_iface(iface));
317
318 if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
319 if (PART_OF_ARRAY(NET_IF_GET_NAME(eth_test, 0), iface)) {
320 if (!eth_interfaces[0]) {
321 /* Just use the first interface */
322 eth_interfaces[0] = iface;
323 ud->eth_if_count++;
324 }
325 } else {
326 if (ud->eth_if_count > ARRAY_SIZE(eth_interfaces)) {
327 goto out;
328 }
329
330 eth_interfaces[starting_eth_idx++] = iface;
331 ud->eth_if_count++;
332 }
333 }
334
335 if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
336 dummy_interfaces[ud->dummy_if_count++] = iface;
337
338 zassert_true(ud->dummy_if_count <= 2,
339 "Too many dummy interfaces");
340 }
341
342 if (net_if_l2(iface) == &NET_L2_GET_NAME(VIRTUAL)) {
343 virtual_interfaces[ud->virtual_if_count++] = iface;
344
345 zassert_true(ud->virtual_if_count <= 3,
346 "Too many virtual interfaces");
347 } else {
348 /* By default all interfaces are down initially */
349 /* Virtual interfaces are down initially */
350 net_if_down(iface);
351 }
352
353 out:
354 ud->total_if_count++;
355 }
356
test_virtual_setup(void)357 static void test_virtual_setup(void)
358 {
359 struct user_data ud = { 0 };
360
361 /* Make sure we have enough virtual interfaces */
362 net_if_foreach(iface_cb, &ud);
363
364 zassert_equal(ud.virtual_if_count, ARRAY_SIZE(virtual_interfaces),
365 "Invalid number of virtual interfaces, "
366 "was %d should be %zu",
367 ud.virtual_if_count, ARRAY_SIZE(virtual_interfaces));
368
369 zassert_true(ud.eth_if_count <= ARRAY_SIZE(eth_interfaces),
370 "Invalid number of eth interfaces, "
371 "was %d should be %zu",
372 ud.eth_if_count, ARRAY_SIZE(eth_interfaces));
373
374 zassert_equal(ud.dummy_if_count, ARRAY_SIZE(dummy_interfaces),
375 "Invalid number of dummy interfaces, "
376 "was %d should be %zu",
377 ud.dummy_if_count, ARRAY_SIZE(dummy_interfaces));
378 }
379
test_address_setup(void)380 static void test_address_setup(void)
381 {
382 struct net_if_addr *ifaddr;
383 struct net_if *eth, *virt, *dummy1, *dummy2;
384 int ret;
385
386 eth = eth_interfaces[0];
387 virt = virtual_interfaces[0];
388 dummy1 = dummy_interfaces[0];
389 dummy2 = dummy_interfaces[1];
390
391 zassert_not_null(eth, "Eth Interface");
392 zassert_not_null(virt, "Virtual Interface");
393 zassert_not_null(dummy1, "Dummy Interface 1");
394 zassert_not_null(dummy2, "Dummy Interface 2");
395
396 ifaddr = net_if_ipv6_addr_add(eth, &my_addr1, NET_ADDR_MANUAL, 0);
397 if (!ifaddr) {
398 DBG("Cannot add IPv6 address %s\n",
399 net_sprint_ipv6_addr(&my_addr1));
400 zassert_not_null(ifaddr, "eth addr");
401 }
402
403 /* For testing purposes we need to set the addresses preferred */
404 ifaddr->addr_state = NET_ADDR_PREFERRED;
405
406 ifaddr = net_if_ipv4_addr_add(eth, &my_addr, NET_ADDR_MANUAL, 0);
407 if (!ifaddr) {
408 DBG("Cannot add IPv4 address %s\n",
409 net_sprint_ipv4_addr(&my_addr));
410 zassert_not_null(ifaddr, "eth addr");
411 }
412
413 ifaddr->addr_state = NET_ADDR_PREFERRED;
414
415 ifaddr = net_if_ipv6_addr_add(eth, &ll_addr, NET_ADDR_MANUAL, 0);
416 if (!ifaddr) {
417 DBG("Cannot add IPv6 address %s\n",
418 net_sprint_ipv6_addr(&ll_addr));
419 zassert_not_null(ifaddr, "ll_addr");
420 }
421
422 ifaddr->addr_state = NET_ADDR_PREFERRED;
423
424 ifaddr = net_if_ipv6_addr_add(virt, &my_addr2, NET_ADDR_MANUAL, 0);
425 if (!ifaddr) {
426 DBG("Cannot add IPv6 address %s\n",
427 net_sprint_ipv6_addr(&my_addr2));
428 zassert_not_null(ifaddr, "virt addr");
429 }
430
431 ifaddr->addr_state = NET_ADDR_PREFERRED;
432
433 ifaddr = net_if_ipv6_addr_add(dummy1, &my_addr3, NET_ADDR_MANUAL, 0);
434 if (!ifaddr) {
435 DBG("Cannot add IPv6 address %s\n",
436 net_sprint_ipv6_addr(&my_addr3));
437 zassert_not_null(ifaddr, "dummy1 addr");
438 }
439
440 net_if_up(eth);
441 net_if_up(dummy1);
442 net_if_up(dummy2);
443
444 /* Set the virtual interface addresses */
445 ret = net_ipaddr_parse(CONFIG_NET_TEST_TUNNEL_MY_ADDR,
446 strlen(CONFIG_NET_TEST_TUNNEL_MY_ADDR),
447 &virtual_addr);
448 zassert_equal(ret, 1, "Cannot parse \"%s\"",
449 CONFIG_NET_TEST_TUNNEL_MY_ADDR);
450
451 if (virtual_addr.sa_family == AF_INET) {
452 ifaddr = net_if_ipv4_addr_add(virt,
453 &net_sin(&virtual_addr)->sin_addr,
454 NET_ADDR_MANUAL, 0);
455 if (!ifaddr) {
456 DBG("Cannot add IPv4 address %s\n",
457 net_sprint_ipv4_addr(
458 &net_sin(&virtual_addr)->sin_addr));
459 zassert_not_null(ifaddr, "virt addr");
460 }
461
462 net_sin(&virtual_addr)->sin_port = htons(4242);
463 } else if (virtual_addr.sa_family == AF_INET6) {
464 ifaddr = net_if_ipv6_addr_add(virt,
465 &net_sin6(&virtual_addr)->sin6_addr,
466 NET_ADDR_MANUAL, 0);
467 if (!ifaddr) {
468 DBG("Cannot add IPv6 address %s\n",
469 net_sprint_ipv6_addr(
470 &net_sin6(&virtual_addr)->sin6_addr));
471 zassert_not_null(ifaddr, "virt addr");
472 }
473
474 net_sin6(&virtual_addr)->sin6_port = htons(4242);
475 } else {
476 zassert_not_null(NULL, "Invalid address family (%d)",
477 virtual_addr.sa_family);
478 }
479
480 ifaddr->addr_state = NET_ADDR_PREFERRED;
481
482 ret = net_ipaddr_parse(CONFIG_NET_TEST_TUNNEL_PEER_ADDR,
483 strlen(CONFIG_NET_TEST_TUNNEL_PEER_ADDR),
484 &peer_addr);
485 zassert_equal(ret, 1, "Cannot parse \"%s\"",
486 CONFIG_NET_TEST_TUNNEL_PEER_ADDR);
487
488 /* The interface might receive data which might fail the checks
489 * in the iface sending function, so we need to reset the failure
490 * flag.
491 */
492 test_failed = false;
493 }
494
add_neighbor(struct net_if * iface,struct in6_addr * addr)495 static bool add_neighbor(struct net_if *iface, struct in6_addr *addr)
496 {
497 struct net_linkaddr_storage llstorage;
498 struct net_linkaddr lladdr;
499 struct net_nbr *nbr;
500
501 llstorage.addr[0] = 0x01;
502 llstorage.addr[1] = 0x02;
503 llstorage.addr[2] = 0x33;
504 llstorage.addr[3] = 0x44;
505 llstorage.addr[4] = 0x05;
506 llstorage.addr[5] = 0x06;
507
508 lladdr.len = 6U;
509 lladdr.addr = llstorage.addr;
510 lladdr.type = NET_LINK_ETHERNET;
511
512 nbr = net_ipv6_nbr_add(iface, addr, &lladdr, false,
513 NET_IPV6_NBR_STATE_REACHABLE);
514 if (!nbr) {
515 DBG("Cannot add dst %s to neighbor cache\n",
516 net_sprint_ipv6_addr(addr));
517 return false;
518 }
519
520 return true;
521 }
522
add_to_arp(struct net_if * iface,struct in_addr * addr)523 static bool add_to_arp(struct net_if *iface, struct in_addr *addr)
524 {
525 #if defined(CONFIG_NET_ARP)
526 struct net_eth_addr lladdr;
527
528 lladdr.addr[0] = sys_rand32_get();
529 lladdr.addr[1] = 0x08;
530 lladdr.addr[2] = 0x09;
531 lladdr.addr[3] = 0x10;
532 lladdr.addr[4] = 0x11;
533 lladdr.addr[5] = sys_rand32_get();
534
535 return arp_add(iface, addr, &lladdr);
536 #else
537 ARG_UNUSED(iface);
538 ARG_UNUSED(addr);
539
540 return true;
541 #endif
542 }
543
ZTEST(net_virtual,test_virtual_01_attach_and_detach)544 ZTEST(net_virtual, test_virtual_01_attach_and_detach)
545 {
546 struct net_if *iface = virtual_interfaces[0];
547 int ret;
548
549 /* Attach virtual interface on top of Ethernet */
550
551 ret = net_virtual_interface_attach(iface, eth_interfaces[0]);
552 zassert_equal(ret, 0, "Cannot attach %d on top of %d (%d)",
553 net_if_get_by_iface(iface),
554 net_if_get_by_iface(eth_interfaces[0]),
555 ret);
556
557 zassert_false(net_if_is_up(iface),
558 "Virtual interface %d should be down",
559 net_if_get_by_iface(iface));
560
561 ret = net_if_up(iface);
562 zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
563 net_if_get_by_iface(iface), ret);
564
565 ret = net_virtual_interface_attach(iface,
566 NULL);
567 zassert_equal(ret, 0, "Cannot deattach %d from %d (%d)",
568 net_if_get_by_iface(iface),
569 net_if_get_by_iface(eth_interfaces[0]),
570 ret);
571
572 zassert_false(net_if_is_up(iface), "Virtual interface %d is still up",
573 net_if_get_by_iface(iface));
574 }
575
ZTEST(net_virtual,test_virtual_02_real_iface_down)576 ZTEST(net_virtual, test_virtual_02_real_iface_down)
577 {
578 struct net_if *iface = virtual_interfaces[0];
579 int ret;
580
581 /* Attach virtual interface on top of Ethernet */
582
583 ret = net_virtual_interface_attach(iface, eth_interfaces[0]);
584 zassert_equal(ret, 0, "Cannot attach %d on top of %d (%d)",
585 net_if_get_by_iface(iface),
586 net_if_get_by_iface(eth_interfaces[0]),
587 ret);
588
589 zassert_false(net_if_is_up(iface),
590 "Virtual interface %d should be down",
591 net_if_get_by_iface(iface));
592
593 ret = net_if_up(iface);
594 zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
595 net_if_get_by_iface(iface), ret);
596
597 zassert_true(net_if_is_up(iface),
598 "Virtual interface %d should be up",
599 net_if_get_by_iface(iface));
600 zassert_true(net_if_is_up(eth_interfaces[0]),
601 "Real interface %d should be up",
602 net_if_get_by_iface(iface));
603
604 /* Virtual interface should go down if the underlying iface is down */
605 ret = net_if_down(eth_interfaces[0]);
606 zassert_equal(ret, 0, "Cannot take real interface %d down (%d)",
607 net_if_get_by_iface(eth_interfaces[0]), ret);
608
609 zassert_false(net_if_is_up(iface),
610 "Virtual interface %d should be down",
611 net_if_get_by_iface(iface));
612 zassert_false(net_if_is_carrier_ok(iface),
613 "Virtual interface %d should be in carrier off",
614 net_if_get_by_iface(iface));
615 zassert_equal(net_if_oper_state(iface), NET_IF_OPER_LOWERLAYERDOWN,
616 "Wrong operational state on %d (%d)",
617 net_if_get_by_iface(iface), net_if_oper_state(iface));
618
619 /* Virtual interface should be brought up if the underlying iface is
620 * back up
621 */
622 ret = net_if_up(eth_interfaces[0]);
623 zassert_equal(ret, 0, "Cannot take real interface %d u (%d)",
624 net_if_get_by_iface(eth_interfaces[0]), ret);
625
626 zassert_true(net_if_is_up(iface),
627 "Virtual interface %d should be up",
628 net_if_get_by_iface(iface));
629 zassert_true(net_if_is_carrier_ok(iface),
630 "Virtual interface %d should be in carrier on",
631 net_if_get_by_iface(iface));
632
633 ret = net_virtual_interface_attach(iface,
634 NULL);
635 zassert_equal(ret, 0, "Cannot deattach %d from %d (%d)",
636 net_if_get_by_iface(iface),
637 net_if_get_by_iface(eth_interfaces[0]),
638 ret);
639
640 zassert_false(net_if_is_up(iface), "Virtual interface %d is still up",
641 net_if_get_by_iface(iface));
642 }
643
644
ZTEST(net_virtual,test_virtual_03_set_mtu)645 ZTEST(net_virtual, test_virtual_03_set_mtu)
646 {
647 struct virtual_interface_req_params params = { 0 };
648 struct net_if *iface = virtual_interfaces[0];
649 int ret;
650
651 ret = net_if_up(iface);
652 zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
653 net_if_get_by_iface(iface), ret);
654
655 params.mtu = MTU;
656
657 ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_MTU,
658 iface, ¶ms, sizeof(params));
659 zassert_equal(ret, -EACCES, "Could set interface %d MTU to %d (%d)",
660 net_if_get_by_iface(iface), params.mtu, ret);
661
662 ret = net_if_down(iface);
663 zassert_equal(ret, 0, "Cannot take virtual interface %d down (%d)",
664 net_if_get_by_iface(iface), ret);
665
666 ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_MTU,
667 iface, ¶ms, sizeof(params));
668 zassert_equal(ret, 0, "Cannot set interface %d MTU to %d (%d)",
669 net_if_get_by_iface(iface), params.mtu, ret);
670 }
671
ZTEST(net_virtual,test_virtual_04_get_mtu)672 ZTEST(net_virtual, test_virtual_04_get_mtu)
673 {
674 struct virtual_interface_req_params params = { 0 };
675 struct net_if *iface = virtual_interfaces[0];
676 int ret;
677
678 params.mtu = 0;
679
680 ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_GET_MTU,
681 iface, ¶ms, sizeof(params));
682 zassert_equal(ret, 0, "Cannot get interface %d MTU (%d)",
683 net_if_get_by_iface(iface), params.mtu, ret);
684
685 zassert_equal(params.mtu, MTU,
686 "MTU mismatch from interface %d, got %d should be %d",
687 net_if_get_by_iface(iface), params.mtu, MTU);
688 }
689
ZTEST(net_virtual,test_virtual_05_set_peer)690 ZTEST(net_virtual, test_virtual_05_set_peer)
691 {
692 struct virtual_interface_req_params params = { 0 };
693 struct net_if *iface = virtual_interfaces[0];
694 int ret;
695
696 ret = net_if_up(iface);
697 zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
698 net_if_get_by_iface(iface), ret);
699
700 params.family = peer_addr.sa_family;
701 if (params.family == AF_INET) {
702 net_ipaddr_copy(¶ms.peer4addr,
703 &net_sin(&peer_addr)->sin_addr);
704 } else if (params.family == AF_INET6) {
705 net_ipaddr_copy(¶ms.peer6addr,
706 &net_sin6(&peer_addr)->sin6_addr);
707 } else {
708 zassert_true(false, "Invalid family (%d)", params.family);
709 }
710
711 ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_PEER_ADDRESS,
712 iface, ¶ms, sizeof(params));
713 zassert_equal(ret, -EACCES, "Could set interface %d peer to %s (%d)",
714 net_if_get_by_iface(iface),
715 CONFIG_NET_TEST_TUNNEL_PEER_ADDR, ret);
716
717 ret = net_if_down(iface);
718 zassert_equal(ret, 0, "Cannot take virtual interface %d down (%d)",
719 net_if_get_by_iface(iface), ret);
720
721 ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_PEER_ADDRESS,
722 iface, ¶ms, sizeof(params));
723 zassert_equal(ret, 0, "Cannot set interface %d peer to %s (%d)",
724 net_if_get_by_iface(iface),
725 CONFIG_NET_TEST_TUNNEL_PEER_ADDR, ret);
726
727 /* We should be attached now */
728 ret = net_virtual_interface_attach(iface, dummy_interfaces[0]);
729 zassert_equal(ret, -EALREADY, "Could attach %d on top of %d (%d)",
730 net_if_get_by_iface(iface),
731 net_if_get_by_iface(dummy_interfaces[0]),
732 ret);
733 }
734
ZTEST(net_virtual,test_virtual_06_get_peer)735 ZTEST(net_virtual, test_virtual_06_get_peer)
736 {
737 struct virtual_interface_req_params params = { 0 };
738 struct net_if *iface = virtual_interfaces[0];
739 int ret;
740
741 ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_GET_PEER_ADDRESS,
742 iface, ¶ms, sizeof(params));
743 zassert_equal(ret, 0, "Cannot get interface %d peer (%d)",
744 net_if_get_by_iface(iface), ret);
745
746 zassert_equal(params.family, peer_addr.sa_family,
747 "Invalid family, should be %d was %d",
748 peer_addr.sa_family, params.family);
749 if (params.family == AF_INET) {
750 zassert_mem_equal(¶ms.peer4addr,
751 &net_sin(&peer_addr)->sin_addr,
752 sizeof(struct in_addr),
753 "Peer IPv4 address invalid");
754 } else if (params.family == AF_INET6) {
755 zassert_mem_equal(¶ms.peer6addr,
756 &net_sin6(&peer_addr)->sin6_addr,
757 sizeof(struct in6_addr),
758 "Peer IPv6 address invalid");
759 } else {
760 zassert_true(false, "Invalid family (%d)", params.family);
761 }
762 }
763
ZTEST(net_virtual,test_virtual_07_verify_name)764 ZTEST(net_virtual, test_virtual_07_verify_name)
765 {
766 #define NAME "foobar"
767 #define NAME2 "123456789"
768 struct net_if *iface = virtual_interfaces[0];
769 char *tmp = NAME;
770 char buf[sizeof(NAME2)];
771 char *name;
772
773 net_virtual_set_name(iface, NAME);
774 name = net_virtual_get_name(iface, buf, sizeof(buf));
775 zassert_mem_equal(name, tmp, strlen(name), "Cannot get name");
776
777 /* Check that the string is truncated */
778 tmp = NAME2;
779 net_virtual_set_name(iface, tmp);
780 name = net_virtual_get_name(iface, buf, sizeof(buf));
781 zassert_mem_equal(name, tmp, strlen(name), "Cannot get name");
782 zassert_mem_equal(name, tmp, strlen(tmp) -
783 (sizeof(NAME2) - CONFIG_NET_L2_VIRTUAL_MAX_NAME_LEN),
784 "Cannot get name");
785 }
786
ZTEST(net_virtual,test_virtual_08_send_data_to_tunnel)787 ZTEST(net_virtual, test_virtual_08_send_data_to_tunnel)
788 {
789 struct virtual_interface_req_params params = { 0 };
790 struct net_if *iface = virtual_interfaces[0];
791 struct net_if *attached;
792 struct sockaddr dst_addr, src_addr;
793 struct in_addr netmask = {{{ 255, 255, 255, 0 }}};
794 void *addr;
795 int addrlen;
796 int ret;
797
798 params.family = peer_addr.sa_family;
799 if (params.family == AF_INET) {
800 net_ipaddr_copy(¶ms.peer4addr,
801 &net_sin(&peer_addr)->sin_addr);
802 expecting_outer = 0x45;
803 header_len = sizeof(struct net_ipv4_hdr);
804
805 ret = add_to_arp(eth_interfaces[0],
806 &net_sin(&peer_addr)->sin_addr);
807 zassert_true(ret, "Cannot add to arp");
808 } else if (params.family == AF_INET6) {
809 net_ipaddr_copy(¶ms.peer6addr,
810 &net_sin6(&peer_addr)->sin6_addr);
811 expecting_outer = 0x60;
812 header_len = sizeof(struct net_ipv6_hdr);
813
814 ret = add_neighbor(eth_interfaces[0],
815 &net_sin6(&peer_addr)->sin6_addr);
816 zassert_true(ret, "Cannot add neighbor");
817 } else {
818 zassert_true(false, "Invalid family (%d)", params.family);
819 }
820
821 net_if_ipv4_set_netmask(iface, &netmask);
822 net_if_ipv4_set_netmask(eth_interfaces[0], &netmask);
823
824 ret = net_mgmt(NET_REQUEST_VIRTUAL_INTERFACE_SET_PEER_ADDRESS,
825 iface, ¶ms, sizeof(params));
826 zassert_equal(ret, 0, "Cannot set interface %d peer to %s (%d)",
827 net_if_get_by_iface(iface),
828 CONFIG_NET_TEST_TUNNEL_PEER_ADDR, ret);
829
830 net_virtual_set_name(iface, CONFIG_NET_TEST_TUNNEL_NAME);
831
832 attached = net_virtual_get_iface(iface);
833 zassert_equal(eth_interfaces[0], attached,
834 "Not attached to Ethernet interface");
835
836 ret = net_if_up(iface);
837 zassert_equal(ret, 0, "Cannot take virtual interface %d up (%d)",
838 net_if_get_by_iface(iface), ret);
839
840 memcpy(&dst_addr, &virtual_addr, sizeof(dst_addr));
841 memcpy(&src_addr, &virtual_addr, sizeof(src_addr));
842
843 if (dst_addr.sa_family == AF_INET) {
844 net_sin(&dst_addr)->sin_addr.s4_addr[3] = 2;
845
846 addr = &src_addr;
847 addrlen = sizeof(struct sockaddr_in);
848
849 expecting_inner = 0x45; /* IPv4 */
850
851 } else if (dst_addr.sa_family == AF_INET6) {
852 net_sin6(&dst_addr)->sin6_addr.s6_addr[15] = 2;
853
854 addr = &src_addr;
855 addrlen = sizeof(struct sockaddr_in6);
856
857 expecting_inner = 0x60; /* IPv6 */
858 } else {
859 zassert_true(false, "Invalid family (%d)", dst_addr.sa_family);
860 addrlen = 0;
861 }
862
863 ret = net_context_get(virtual_addr.sa_family, SOCK_DGRAM, IPPROTO_UDP,
864 &udp_ctx);
865 zassert_equal(ret, 0, "Create IP UDP context failed");
866
867 ret = net_context_bind(udp_ctx, (struct sockaddr *)addr, addrlen);
868 zassert_equal(ret, 0, "Context bind failure test failed");
869
870 test_started = true;
871
872 ret = net_context_sendto(udp_ctx, test_data, strlen(test_data),
873 &dst_addr, addrlen,
874 NULL, K_NO_WAIT, NULL);
875 zassert_true(ret > 0, "Send UDP pkt failed");
876
877 if (k_sem_take(&wait_data, WAIT_TIME)) {
878 DBG("Timeout while waiting interface data\n");
879 zassert_false(true, "Timeout");
880 }
881
882 net_context_unref(udp_ctx);
883 }
884
create_outer(struct net_if * iface,sa_family_t family,enum net_ip_protocol proto,size_t inner_len,size_t outer_len)885 static struct net_pkt *create_outer(struct net_if *iface,
886 sa_family_t family,
887 enum net_ip_protocol proto,
888 size_t inner_len,
889 size_t outer_len)
890 {
891 return net_pkt_alloc_with_buffer(iface, inner_len + outer_len,
892 family, proto, PKT_ALLOC_TIME);
893 }
894
create_inner(struct net_if * iface,sa_family_t family,enum net_ip_protocol proto,size_t inner_len,size_t data_len)895 static struct net_pkt *create_inner(struct net_if *iface,
896 sa_family_t family,
897 enum net_ip_protocol proto,
898 size_t inner_len,
899 size_t data_len)
900 {
901 return net_pkt_alloc_with_buffer(iface, inner_len + data_len,
902 family, proto, PKT_ALLOC_TIME);
903 }
904
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)905 static void recv_data(struct net_context *context,
906 struct net_pkt *pkt,
907 union net_ip_header *ip_hdr,
908 union net_proto_header *proto_hdr,
909 int status,
910 void *user_data)
911 {
912 data_received = true;
913 }
914
test_virtual_recv_data_from_tunnel(int remote_ip,bool expected_ok)915 static void test_virtual_recv_data_from_tunnel(int remote_ip,
916 bool expected_ok)
917 {
918 struct net_if *iface = virtual_interfaces[0];
919 struct net_if *attached = eth_interfaces[0];
920 struct sockaddr dst_addr, src_addr, inner_src;
921 struct in_addr *outerv4, *innerv4;
922 struct in6_addr *outerv6, *innerv6;
923 size_t inner_len = sizeof(struct net_udp_hdr) +
924 strlen(test_data);
925 struct net_pkt *outer, *inner;
926 enum net_verdict verdict;
927 uint16_t src_port = 4242, dst_port = 4242;
928 uint8_t next_header;
929 size_t addrlen;
930 int ret;
931
932 memcpy(&dst_addr, &peer_addr, sizeof(dst_addr));
933 memcpy(&src_addr, &peer_addr, sizeof(src_addr));
934 memcpy(&inner_src, &virtual_addr, sizeof(inner_src));
935
936 if (peer_addr.sa_family == AF_INET) {
937 net_sin(&dst_addr)->sin_addr.s4_addr[3] = 1;
938 net_sin(&src_addr)->sin_addr.s4_addr[3] = remote_ip;
939 outerv4 = &net_sin(&peer_addr)->sin_addr;
940 } else {
941 net_sin6(&dst_addr)->sin6_addr.s6_addr[15] = 1;
942 net_sin6(&src_addr)->sin6_addr.s6_addr[15] = remote_ip;
943 outerv6 = &net_sin6(&peer_addr)->sin6_addr;
944 }
945
946 if (virtual_addr.sa_family == AF_INET) {
947 net_sin(&inner_src)->sin_addr.s4_addr[3] = 2;
948 innerv4 = &net_sin(&virtual_addr)->sin_addr;
949 inner_len += sizeof(struct net_ipv4_hdr);
950 } else {
951 net_sin6(&inner_src)->sin6_addr.s6_addr[15] = 2;
952 innerv6 = &net_sin6(&virtual_addr)->sin6_addr;
953 inner_len += sizeof(struct net_ipv6_hdr);
954 }
955
956 if (peer_addr.sa_family == AF_INET) {
957 outer = create_outer(attached, AF_INET, IPPROTO_IP,
958 sizeof(struct net_ipv4_hdr), 0);
959 zassert_not_null(outer, "Cannot allocate %s pkt", outer);
960
961 ret = net_ipv4_create(outer, &net_sin(&src_addr)->sin_addr,
962 &net_sin(&dst_addr)->sin_addr);
963 zassert_equal(ret, 0, "Cannot create %s packet (%d)", "IPv4",
964 ret);
965 } else {
966 outer = create_outer(attached, AF_INET6, IPPROTO_IPV6,
967 sizeof(struct net_ipv6_hdr), 0);
968 zassert_not_null(outer, "Cannot allocate %s pkt", outer);
969
970 ret = net_ipv6_create(outer, &net_sin6(&src_addr)->sin6_addr,
971 &net_sin6(&dst_addr)->sin6_addr);
972 zassert_equal(ret, 0, "Cannot create %s packet (%d)", "IPv6",
973 ret);
974 }
975
976 if (virtual_addr.sa_family == AF_INET) {
977 inner = create_inner(iface, AF_INET, IPPROTO_IP,
978 sizeof(struct net_ipv4_hdr),
979 sizeof(struct net_udp_hdr) +
980 strlen(test_data));
981 zassert_not_null(inner, "Cannot allocate %s pkt", inner);
982
983 ret = net_ipv4_create(inner, &net_sin(&inner_src)->sin_addr,
984 innerv4);
985 zassert_equal(ret, 0, "Cannot create outer %s (%d)", "IPv4",
986 ret);
987 next_header = IPPROTO_IPIP;
988 addrlen = sizeof(struct sockaddr_in);
989 } else {
990 inner = create_inner(iface, AF_INET6, IPPROTO_IPV6,
991 sizeof(struct net_ipv6_hdr),
992 sizeof(struct net_udp_hdr) +
993 strlen(test_data));
994 zassert_not_null(inner, "Cannot allocate %s pkt", inner);
995
996 ret = net_ipv6_create(inner, &net_sin6(&inner_src)->sin6_addr,
997 innerv6);
998 zassert_equal(ret, 0, "Cannot create outer %s (%d)", "IPv6",
999 ret);
1000 next_header = IPPROTO_IPV6;
1001 addrlen = sizeof(struct sockaddr_in6);
1002 }
1003
1004 ret = net_udp_create(inner, htons(src_port), htons(dst_port));
1005 zassert_equal(ret, 0, "Cannot create UDP (%d)", ret);
1006
1007 net_pkt_write(inner, test_data, strlen(test_data));
1008
1009 net_pkt_cursor_init(inner);
1010 net_ipv4_finalize(inner, IPPROTO_UDP);
1011
1012 net_buf_frag_add(outer->buffer, inner->buffer);
1013 inner->buffer = NULL;
1014 net_pkt_unref(inner);
1015
1016 net_pkt_cursor_init(outer);
1017
1018 if (peer_addr.sa_family == AF_INET) {
1019 net_ipv4_finalize(outer, next_header);
1020 } else {
1021 net_ipv6_finalize(outer, next_header);
1022 }
1023
1024 ret = net_context_get(virtual_addr.sa_family, SOCK_DGRAM, IPPROTO_UDP,
1025 &udp_ctx);
1026 zassert_equal(ret, 0, "Create IP UDP context failed");
1027
1028 net_context_set_iface(udp_ctx, iface);
1029
1030 ret = net_context_bind(udp_ctx, (struct sockaddr *)&virtual_addr,
1031 addrlen);
1032 zassert_equal(ret, 0, "Context bind failure test failed");
1033
1034 test_started = true;
1035 data_received = false;
1036
1037 ret = net_context_recv(udp_ctx, recv_data, K_NO_WAIT, &wait_data);
1038 zassert_equal(ret, 0, "UDP recv failed");
1039
1040 net_pkt_cursor_init(outer);
1041
1042 if (peer_addr.sa_family == AF_INET) {
1043 verdict = net_ipv4_input(outer, false);
1044 } else {
1045 verdict = net_ipv6_input(outer, false);
1046 }
1047
1048 if (expected_ok) {
1049 zassert_equal(verdict, NET_CONTINUE,
1050 "Packet not accepted (%d)",
1051 verdict);
1052 } else {
1053 zassert_equal(verdict, NET_DROP,
1054 "Packet not dropped (%d)",
1055 verdict);
1056 }
1057
1058 net_context_put(udp_ctx);
1059 }
1060
ZTEST(net_virtual,test_virtual_09_recv_data_from_tunnel_ok)1061 ZTEST(net_virtual, test_virtual_09_recv_data_from_tunnel_ok)
1062 {
1063 test_virtual_recv_data_from_tunnel(2, true);
1064 }
1065
ZTEST(net_virtual,test_virtual_10_recv_data_from_tunnel_fail)1066 ZTEST(net_virtual, test_virtual_10_recv_data_from_tunnel_fail)
1067 {
1068 test_virtual_recv_data_from_tunnel(3, false);
1069 }
1070
setup(void)1071 static void *setup(void)
1072 {
1073 test_virtual_setup();
1074 test_address_setup();
1075 return NULL;
1076 }
1077
1078 ZTEST_SUITE(net_virtual, NULL, setup, NULL, NULL, NULL);
1079