1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2018 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define NET_LOG_LEVEL CONFIG_NET_L2_ETHERNET_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/linker/sections.h>
21 #include <zephyr/random/random.h>
22 
23 #include <zephyr/ztest.h>
24 
25 #include <zephyr/net/socket.h>
26 #include <zephyr/net/ethernet.h>
27 #include <zephyr/net/dummy.h>
28 #include <zephyr/net_buf.h>
29 #include <zephyr/net/net_ip.h>
30 #include <zephyr/net/ethernet_vlan.h>
31 #include <zephyr/net/net_l2.h>
32 
33 #include "ipv6.h"
34 #include "../../socket/socket_helpers.h"
35 
36 #define NET_LOG_ENABLED 1
37 #include "net_private.h"
38 
39 #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
40 #define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
41 #else
42 #define DBG(fmt, ...)
43 #endif
44 
45 #define ANY_PORT 0
46 #define SERVER_PORT 4242
47 #define CLIENT_PORT 9898
48 
49 #define VLAN_TAG_1 100
50 #define VLAN_TAG_2 200
51 #define VLAN_TAG_3 300
52 #define VLAN_TAG_4 400
53 #define VLAN_TAG_5 500
54 #define VLAN_TAG_6 600
55 
56 #define NET_ETH_MAX_COUNT 2
57 
58 #define MY_IPV6_ADDR "2001:db8:200::2"
59 #define MY_IPV6_ADDR_SRV "2001:db8:200::1"
60 
61 /* Interface 1 addresses */
62 static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
63 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
64 
65 /* Interface 2 addresses */
66 static struct in6_addr my_addr2 = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
67 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
68 
69 /* VLAN Interface 3 addresses */
70 static struct in6_addr my_addr3 = { { { 0x20, 0x01, 0x0d, 0xb8, 2, 0, 0, 0,
71 					0, 0, 0, 0, 0, 0, 0, 0x1 } } };
72 
73 /* Extra address is assigned to ll_addr */
74 static struct in6_addr ll_addr = { { { 0xfe, 0x80, 0x43, 0xb8, 0, 0, 0, 0,
75 				       0, 0, 0, 0xf2, 0xaa, 0x29, 0x02,
76 				       0x04 } } };
77 
78 /* Keep track of all ethernet interfaces */
79 static struct net_if *eth_interfaces[NET_ETH_MAX_COUNT];
80 static struct net_if *vlan_interfaces[NET_VLAN_MAX_COUNT];
81 static struct net_if *dummy_interfaces[2];
82 static struct net_if *embed_ll_interface;
83 
84 static bool test_failed;
85 static bool test_started;
86 
87 static K_SEM_DEFINE(wait_data, 0, UINT_MAX);
88 
89 #define BUF_AND_SIZE(buf) buf, sizeof(buf) - 1
90 #define STRLEN(buf) (sizeof(buf) - 1)
91 
92 #define TEST_STR_SMALL "test"
93 
94 /* More than 256 bytes, to use >1 net_buf. */
95 #define TEST_STR2 \
96 	"The Zephyr Project, a Linux Foundation hosted Collaboration " \
97 	"Project, is an open source collaborative effort uniting leaders " \
98 	"from across the industry to build a best-in-breed small, scalable, " \
99 	"real-time operating system (RTOS) optimized for resource-" \
100 	"constrained devices, across multiple architectures."
101 
102 #define WAIT_TIME K_MSEC(500)
103 
104 struct eth_context {
105 	struct net_if *iface;
106 	uint8_t mac_addr[6];
107 
108 	uint16_t expecting_tag;
109 };
110 
111 static struct eth_context eth_vlan_context;
112 
eth_vlan_iface_init(struct net_if * iface)113 static void eth_vlan_iface_init(struct net_if *iface)
114 {
115 	const struct device *dev = net_if_get_device(iface);
116 	struct eth_context *context = dev->data;
117 
118 	net_if_set_link_addr(iface, context->mac_addr,
119 			     sizeof(context->mac_addr),
120 			     NET_LINK_ETHERNET);
121 
122 	ethernet_init(iface);
123 }
124 
eth_tx(const struct device * dev,struct net_pkt * pkt)125 static int eth_tx(const struct device *dev, struct net_pkt *pkt)
126 {
127 	struct eth_context *context = dev->data;
128 	int ret;
129 
130 	if (!IS_ENABLED(CONFIG_NET_L2_ETHERNET_RESERVE_HEADER)) {
131 		/* There should be at least two net_buf. The first one should contain
132 		 * the link layer header.
133 		 */
134 		zassert_not_equal(pkt->buffer->frags, NULL, "Only one net_buf in chain!");
135 	}
136 
137 	zassert_equal_ptr(&eth_vlan_context, context,
138 			  "Context pointers do not match (%p vs %p)",
139 			  eth_vlan_context, context);
140 
141 	if (!pkt->buffer) {
142 		DBG("No data to send!\n");
143 		return -ENODATA;
144 	}
145 
146 	if (test_started) {
147 		struct net_eth_vlan_hdr *hdr =
148 			(struct net_eth_vlan_hdr *)NET_ETH_HDR(pkt);
149 
150 		zassert_equal(context->expecting_tag,
151 			      net_pkt_vlan_tag(pkt),
152 			      "Invalid VLAN tag (%d vs %d) in TX pkt",
153 			      net_pkt_vlan_tag(pkt),
154 			      context->expecting_tag);
155 
156 		zassert_equal(context->expecting_tag,
157 			      net_eth_vlan_get_vid(ntohs(hdr->vlan.tci)),
158 			      "Invalid VLAN tag in ethernet header");
159 
160 		k_sleep(K_MSEC(10));
161 		k_sem_give(&wait_data);
162 	}
163 
164 	ret = net_recv_data(net_pkt_iface(pkt),
165 			    net_pkt_clone(pkt, K_NO_WAIT));
166 	zassert_false(ret < 0, "Cannot receive data (%d)", ret);
167 
168 	return ret;
169 }
170 
eth_vlan_capabilities(const struct device * dev)171 static enum ethernet_hw_caps eth_vlan_capabilities(const struct device *dev)
172 {
173 	return ETHERNET_HW_VLAN;
174 }
175 
176 static struct ethernet_api api_funcs = {
177 	.iface_api.init = eth_vlan_iface_init,
178 
179 	.get_capabilities = eth_vlan_capabilities,
180 	.send = eth_tx,
181 };
182 
generate_mac(uint8_t * mac_addr)183 static void generate_mac(uint8_t *mac_addr)
184 {
185 	/* 00-00-5E-00-53-xx Documentation RFC 7042 */
186 	mac_addr[0] = 0x00;
187 	mac_addr[1] = 0x00;
188 	mac_addr[2] = 0x5E;
189 	mac_addr[3] = 0x00;
190 	mac_addr[4] = 0x53;
191 	mac_addr[5] = sys_rand8_get();
192 }
193 
eth_vlan_init(const struct device * dev)194 static int eth_vlan_init(const struct device *dev)
195 {
196 	struct eth_context *context = dev->data;
197 
198 	generate_mac(context->mac_addr);
199 
200 	return 0;
201 }
202 
203 ETH_NET_DEVICE_INIT(eth_vlan_test, "eth_vlan_test",
204 		    eth_vlan_init, NULL,
205 		    &eth_vlan_context, NULL, CONFIG_ETH_INIT_PRIORITY,
206 		    &api_funcs, NET_ETH_MTU);
207 
eth_init(const struct device * dev)208 static int eth_init(const struct device *dev)
209 {
210 	struct eth_context *context = dev->data;
211 
212 	generate_mac(context->mac_addr);
213 
214 	return 0;
215 }
216 
217 ETH_NET_DEVICE_INIT(eth_test, "eth_test", eth_init, NULL,
218 		    &eth_vlan_context, NULL, CONFIG_ETH_INIT_PRIORITY,
219 		    &api_funcs, NET_ETH_MTU);
220 
eth_tx_embed_ll_hdr(const struct device * dev,struct net_pkt * pkt)221 static int eth_tx_embed_ll_hdr(const struct device *dev, struct net_pkt *pkt)
222 {
223 	if (IS_ENABLED(CONFIG_NET_L2_ETHERNET_RESERVE_HEADER)) {
224 		/* There should be only one net_buf */
225 		zassert_equal(pkt->buffer->frags, NULL, "More than one net_buf in chain!");
226 	}
227 
228 	if (test_started) {
229 		k_sleep(K_MSEC(10));
230 		k_sem_give(&wait_data);
231 	}
232 
233 	return 0;
234 }
235 
eth_vlan_embed_ll_hdr_capabilities(const struct device * dev)236 static enum ethernet_hw_caps eth_vlan_embed_ll_hdr_capabilities(const struct device *dev)
237 {
238 	return ETHERNET_HW_VLAN;
239 }
240 
241 static struct ethernet_api api_vlan_embed_ll_hdr_funcs = {
242 	.iface_api.init = eth_vlan_iface_init,
243 
244 	.get_capabilities = eth_vlan_embed_ll_hdr_capabilities,
245 	.send = eth_tx_embed_ll_hdr,
246 };
247 
248 ETH_NET_DEVICE_INIT(eth_embed_ll_hdr_test, "eth_embed_ll_hdr_test", eth_init, NULL,
249 		    &eth_vlan_context, NULL, CONFIG_ETH_INIT_PRIORITY,
250 		    &api_vlan_embed_ll_hdr_funcs, NET_ETH_MTU);
251 
252 struct net_if_test {
253 	uint8_t idx; /* not used for anything, just a dummy value */
254 	uint8_t mac_addr[sizeof(struct net_eth_addr)];
255 	struct net_linkaddr ll_addr;
256 };
257 
net_iface_get_mac(const struct device * dev)258 static uint8_t *net_iface_get_mac(const struct device *dev)
259 {
260 	struct net_if_test *data = dev->data;
261 
262 	/* 00-00-5E-00-53-xx Documentation RFC 7042 */
263 	data->mac_addr[0] = 0x00;
264 	data->mac_addr[1] = 0x00;
265 	data->mac_addr[2] = 0x5E;
266 	data->mac_addr[3] = 0x00;
267 	data->mac_addr[4] = 0x53;
268 	data->mac_addr[5] = sys_rand8_get();
269 
270 	data->ll_addr.addr = data->mac_addr;
271 	data->ll_addr.len = 6U;
272 
273 	return data->mac_addr;
274 }
275 
net_iface_init(struct net_if * iface)276 static void net_iface_init(struct net_if *iface)
277 {
278 	uint8_t *mac = net_iface_get_mac(net_if_get_device(iface));
279 
280 	net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr),
281 			     NET_LINK_ETHERNET);
282 }
283 
sender_iface(const struct device * dev,struct net_pkt * pkt)284 static int sender_iface(const struct device *dev, struct net_pkt *pkt)
285 {
286 	return 0;
287 }
288 
289 struct net_if_test net_iface1_data;
290 struct net_if_test net_iface2_data;
291 
292 static struct dummy_api net_iface_api = {
293 	.iface_api.init = net_iface_init,
294 	.send = sender_iface,
295 };
296 
297 /* For testing purposes, create two dummy network interfaces so we can check
298  * that no VLANs are created for it.
299  */
300 NET_DEVICE_INIT_INSTANCE(net_iface1_test,
301 			 "iface1",
302 			 iface1,
303 			 NULL,
304 			 NULL,
305 			 &net_iface1_data,
306 			 NULL,
307 			 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
308 			 &net_iface_api,
309 			 DUMMY_L2,
310 			 NET_L2_GET_CTX_TYPE(DUMMY_L2),
311 			 127);
312 
313 NET_DEVICE_INIT_INSTANCE(net_iface2_test,
314 			 "iface2",
315 			 iface2,
316 			 NULL,
317 			 NULL,
318 			 &net_iface2_data,
319 			 NULL,
320 			 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
321 			 &net_iface_api,
322 			 DUMMY_L2,
323 			 NET_L2_GET_CTX_TYPE(DUMMY_L2),
324 			 127);
325 
326 struct user_data {
327 	int eth_if_count;
328 	int vlan_if_count;
329 	int dummy_if_count;
330 	int total_if_count;
331 };
332 
333 #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
iface2str(struct net_if * iface)334 static const char *iface2str(struct net_if *iface)
335 {
336 #ifdef CONFIG_NET_L2_ETHERNET
337 	if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
338 		return "Ethernet";
339 	}
340 #endif
341 
342 #ifdef CONFIG_NET_L2_DUMMY
343 	if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
344 		return "Dummy";
345 	}
346 #endif
347 
348 	return "<unknown type>";
349 }
350 #endif
351 
iface_cb(struct net_if * iface,void * user_data)352 static void iface_cb(struct net_if *iface, void *user_data)
353 {
354 	struct user_data *ud = user_data;
355 
356 	DBG("Interface %p (%s) [%d]\n", iface, iface2str(iface),
357 	    net_if_get_by_iface(iface));
358 
359 	if (net_if_l2(iface) == &NET_L2_GET_NAME(ETHERNET)) {
360 		/* Ignore one special interface that is used to validate
361 		 * ll header embedding.
362 		 */
363 		if (strncmp(net_if_get_device(iface)->name,
364 			    "eth_embed_ll_hdr_test",
365 			    sizeof("eth_embed_ll_hdr_test") - 1) == 0) {
366 			embed_ll_interface = iface;
367 			return;
368 		}
369 
370 		eth_interfaces[ud->eth_if_count++] = iface;
371 	}
372 
373 	if (net_if_l2(iface) == &NET_L2_GET_NAME(VIRTUAL)) {
374 		vlan_interfaces[ud->vlan_if_count++] = iface;
375 
376 		zassert_true(ud->vlan_if_count <= NET_VLAN_MAX_COUNT,
377 			     "Too many VLAN interfaces");
378 	}
379 
380 	if (net_if_l2(iface) == &NET_L2_GET_NAME(DUMMY)) {
381 		dummy_interfaces[ud->dummy_if_count++] = iface;
382 
383 		zassert_true(ud->dummy_if_count <= 2,
384 			     "Too many dummy interfaces");
385 	}
386 
387 	/* By default all interfaces are down initially */
388 	net_if_down(iface);
389 
390 	ud->total_if_count++;
391 }
392 
test_vlan_setup(void)393 static void test_vlan_setup(void)
394 {
395 	struct user_data ud = { 0 };
396 	int remaining;
397 
398 	/* Make sure we have enough virtual interfaces */
399 	net_if_foreach(iface_cb, &ud);
400 
401 	/* One extra eth interface without vlan support */
402 	zassert_equal(ud.vlan_if_count, NET_VLAN_MAX_COUNT,
403 		      "Invalid number of VLANs %d vs %d",
404 		      ud.vlan_if_count, NET_VLAN_MAX_COUNT);
405 
406 	remaining = ud.total_if_count - NET_VLAN_MAX_COUNT -
407 		ud.eth_if_count - ud.dummy_if_count;
408 	zassert_equal(remaining, 0,
409 		      "Invalid number of interfaces expecting %d got %d+%d+%d",
410 		      ud.total_if_count, NET_VLAN_MAX_COUNT,
411 		      ud.eth_if_count, ud.dummy_if_count);
412 }
413 
test_address_setup(void)414 static void test_address_setup(void)
415 {
416 	struct net_if_addr *ifaddr;
417 	struct net_if *iface1, *iface2, *iface3;
418 
419 	iface1 = eth_interfaces[1]; /* This has VLAN enabled */
420 	iface2 = eth_interfaces[0]; /* and this one not */
421 	iface3 = vlan_interfaces[0]; /* and this is the virtual VLAN interface */
422 
423 	zassert_not_null(iface1, "Interface 1");
424 	zassert_not_null(iface2, "Interface 2");
425 	zassert_not_null(iface3, "Interface 3");
426 
427 	ifaddr = net_if_ipv6_addr_add(iface1, &my_addr1,
428 				      NET_ADDR_MANUAL, 0);
429 	if (!ifaddr) {
430 		DBG("Cannot add IPv6 address %s\n",
431 		       net_sprint_ipv6_addr(&my_addr1));
432 		zassert_not_null(ifaddr, "addr1");
433 	}
434 
435 	/* For testing purposes we need to set the addresses preferred */
436 	ifaddr->addr_state = NET_ADDR_PREFERRED;
437 
438 	ifaddr = net_if_ipv6_addr_add(iface1, &ll_addr,
439 				      NET_ADDR_MANUAL, 0);
440 	if (!ifaddr) {
441 		DBG("Cannot add IPv6 address %s\n",
442 		       net_sprint_ipv6_addr(&ll_addr));
443 		zassert_not_null(ifaddr, "ll_addr");
444 	}
445 
446 	ifaddr->addr_state = NET_ADDR_PREFERRED;
447 
448 	ifaddr = net_if_ipv6_addr_add(iface2, &my_addr2,
449 				      NET_ADDR_MANUAL, 0);
450 	if (!ifaddr) {
451 		DBG("Cannot add IPv6 address %s\n",
452 		       net_sprint_ipv6_addr(&my_addr2));
453 		zassert_not_null(ifaddr, "addr2");
454 	}
455 
456 	ifaddr->addr_state = NET_ADDR_PREFERRED;
457 
458 	ifaddr = net_if_ipv6_addr_add(iface3, &my_addr3,
459 				      NET_ADDR_MANUAL, 0);
460 	if (!ifaddr) {
461 		DBG("Cannot add IPv6 address %s\n",
462 		       net_sprint_ipv6_addr(&my_addr3));
463 		zassert_not_null(ifaddr, "addr3");
464 	}
465 
466 	ifaddr->addr_state = NET_ADDR_PREFERRED;
467 
468 	net_if_up(iface1);
469 	net_if_up(iface2);
470 
471 	/* The interface might receive data which might fail the checks
472 	 * in the iface sending function, so we need to reset the failure
473 	 * flag.
474 	 */
475 	test_failed = false;
476 }
477 
ZTEST(net_vlan,test_vlan_tci)478 ZTEST(net_vlan, test_vlan_tci)
479 {
480 	struct net_pkt *pkt;
481 	uint16_t tci;
482 	uint16_t tag;
483 	uint8_t priority;
484 	bool dei;
485 
486 	pkt = net_pkt_alloc(K_FOREVER);
487 
488 	tag = NET_VLAN_TAG_UNSPEC;
489 	net_pkt_set_vlan_tag(pkt, tag);
490 
491 	priority = 0U;
492 	net_pkt_set_vlan_priority(pkt, priority);
493 
494 	zassert_equal(net_pkt_vlan_tag(pkt), NET_VLAN_TAG_UNSPEC,
495 		      "invalid VLAN tag unspec");
496 	zassert_equal(net_pkt_vlan_priority(pkt), priority,
497 		      "invalid VLAN priority");
498 
499 	net_pkt_set_vlan_tag(pkt, 0);
500 	zassert_equal(net_pkt_vlan_tag(pkt), 0, "invalid VLAN tag");
501 
502 	/* TCI should be zero now */
503 	zassert_equal(net_pkt_vlan_tci(pkt), 0, "invalid VLAN TCI");
504 
505 	priority = 1U;
506 	net_pkt_set_vlan_priority(pkt, priority);
507 
508 	zassert_equal(net_pkt_vlan_priority(pkt), priority,
509 		      "invalid VLAN priority");
510 
511 	net_pkt_set_vlan_tag(pkt, tag);
512 
513 	zassert_equal(net_pkt_vlan_tag(pkt), NET_VLAN_TAG_UNSPEC,
514 		      "invalid VLAN tag unspec");
515 
516 	zassert_equal(net_pkt_vlan_priority(pkt), priority,
517 		      "invalid VLAN priority");
518 
519 	net_pkt_set_vlan_tag(pkt, 0);
520 	zassert_equal(net_pkt_vlan_priority(pkt), priority,
521 		      "invalid VLAN priority");
522 
523 	dei = true;
524 	net_pkt_set_vlan_dei(pkt, dei);
525 
526 	zassert_equal(net_pkt_vlan_dei(pkt), dei, "invalid VLAN DEI");
527 	zassert_equal(net_pkt_vlan_priority(pkt), priority,
528 		      "invalid VLAN priority");
529 	zassert_equal(net_pkt_vlan_tag(pkt), 0, "invalid VLAN tag");
530 
531 	net_pkt_set_vlan_tag(pkt, tag);
532 	zassert_equal(net_pkt_vlan_tag(pkt), tag, "invalid VLAN tag");
533 	zassert_equal(net_pkt_vlan_dei(pkt), dei, "invalid VLAN DEI");
534 	zassert_equal(net_pkt_vlan_priority(pkt), priority,
535 		      "invalid VLAN priority");
536 
537 	dei = false;
538 	net_pkt_set_vlan_dei(pkt, dei);
539 	zassert_equal(net_pkt_vlan_tag(pkt), tag, "invalid VLAN tag");
540 	zassert_equal(net_pkt_vlan_dei(pkt), dei, "invalid VLAN DEI");
541 	zassert_equal(net_pkt_vlan_priority(pkt), priority,
542 		      "invalid VLAN priority");
543 
544 	tag = 0U;
545 	net_pkt_set_vlan_tag(pkt, tag);
546 	zassert_equal(net_pkt_vlan_tag(pkt), tag, "invalid VLAN tag");
547 	zassert_equal(net_pkt_vlan_dei(pkt), dei, "invalid VLAN DEI");
548 	zassert_equal(net_pkt_vlan_priority(pkt), priority,
549 		      "invalid VLAN priority");
550 
551 	priority = 0U;
552 	net_pkt_set_vlan_priority(pkt, priority);
553 	zassert_equal(net_pkt_vlan_tag(pkt), tag, "invalid VLAN tag");
554 	zassert_equal(net_pkt_vlan_dei(pkt), dei, "invalid VLAN DEI");
555 	zassert_equal(net_pkt_vlan_priority(pkt), priority,
556 		      "invalid VLAN priority");
557 
558 	zassert_equal(net_pkt_vlan_tci(pkt), 0, "invalid VLAN TCI");
559 
560 	tci = 0U;
561 	tag = 100U;
562 	priority = 3U;
563 
564 	tci = net_eth_vlan_set_vid(tci, tag);
565 	tci = net_eth_vlan_set_pcp(tci, priority);
566 
567 	zassert_equal(tag, net_eth_vlan_get_vid(tci), "Invalid VLAN tag");
568 	zassert_equal(priority, net_eth_vlan_get_pcp(tci),
569 		      "Invalid VLAN priority");
570 
571 	net_pkt_unref(pkt);
572 }
573 
574 /* Enable two VLAN tags and verity that proper interfaces are enabled.
575  */
test_vlan_enable(void)576 static void test_vlan_enable(void)
577 {
578 	struct ethernet_context *eth_ctx;
579 	struct net_if *iface;
580 	int ret;
581 
582 	ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_1);
583 	zassert_equal(ret, 0, "Cannot enable %d (%d)", VLAN_TAG_1, ret);
584 	ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_2);
585 	zassert_equal(ret, 0, "Cannot enable %d (%d)", VLAN_TAG_2, ret);
586 	ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_3);
587 	zassert_equal(ret, 0, "Cannot enable %d (%d)", VLAN_TAG_3, ret);
588 	ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_4);
589 	zassert_equal(ret, 0, "Cannot enable %d (%d)", VLAN_TAG_4, ret);
590 
591 	eth_ctx = net_if_l2_data(eth_interfaces[0]);
592 
593 	iface = net_eth_get_vlan_iface(eth_interfaces[0], VLAN_TAG_1);
594 	zassert_equal_ptr(iface, vlan_interfaces[0],
595 			  "Invalid interface for tag %d (%p vs %p)",
596 			  VLAN_TAG_1, iface, vlan_interfaces[0]);
597 
598 	iface = net_eth_get_vlan_iface(eth_interfaces[0], VLAN_TAG_2);
599 	zassert_equal_ptr(iface, vlan_interfaces[1],
600 			  "Invalid interface for tag %d (%p vs %p)",
601 			  VLAN_TAG_2, iface, vlan_interfaces[1]);
602 
603 	iface = net_eth_get_vlan_iface(eth_interfaces[0], VLAN_TAG_3);
604 	zassert_equal_ptr(iface, vlan_interfaces[2],
605 			  "Invalid interface for tag %d (%p vs %p)",
606 			  VLAN_TAG_3, iface, vlan_interfaces[2]);
607 
608 	iface = net_eth_get_vlan_iface(eth_interfaces[0], VLAN_TAG_4);
609 	zassert_equal_ptr(iface, vlan_interfaces[3],
610 			  "Invalid interface for tag %d (%p vs %p)",
611 			  VLAN_TAG_4, iface, vlan_interfaces[3]);
612 
613 	ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[0]);
614 	zassert_equal(ret, true, "VLAN enabled for interface 0");
615 
616 	ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[1]);
617 	zassert_equal(ret, false, "VLAN enabled for interface 1");
618 
619 	iface = eth_interfaces[0];
620 	ret = net_eth_vlan_enable(iface, NET_VLAN_TAG_UNSPEC);
621 	zassert_equal(ret, -EBADF, "Invalid VLAN tag value %d", ret);
622 
623 	iface = eth_interfaces[1];
624 	ret = net_eth_vlan_enable(iface, VLAN_TAG_1);
625 	zassert_equal(ret, -EALREADY, "VLAN tag %d enabled for iface 1 (%d)",
626 		      VLAN_TAG_1, ret);
627 
628 	for (int i = VLAN_TAG_1; i <= VLAN_TAG_5; i += 100) {
629 		iface = net_eth_get_vlan_iface(NULL, i);
630 
631 		ARRAY_FOR_EACH_PTR(vlan_interfaces, vlan_iface) {
632 			uint16_t tag;
633 
634 			ret = net_eth_is_vlan_interface(*vlan_iface);
635 			zassert_equal(ret, true,
636 				      "Not identified as VLAN interface %d",
637 				      net_if_get_by_iface(*vlan_iface));
638 
639 			if (*vlan_iface == iface) {
640 				tag = net_eth_get_vlan_tag(*vlan_iface);
641 
642 				zassert_equal(tag, i,
643 					      "Could not get the VLAN interface (%d)",
644 					      net_if_get_by_iface(*vlan_iface));
645 				break;
646 			}
647 		}
648 	}
649 }
650 
test_vlan_disable(void)651 static void test_vlan_disable(void)
652 {
653 	struct ethernet_context *eth_ctx;
654 	struct net_if *iface;
655 	int ret;
656 
657 	ret = net_eth_vlan_disable(eth_interfaces[1], VLAN_TAG_1);
658 	zassert_not_equal(ret, 0, "Could disable %d (%d)", VLAN_TAG_1, ret);
659 
660 	ret = net_eth_vlan_disable(eth_interfaces[0], VLAN_TAG_1);
661 	zassert_equal(ret, 0, "Cannot disable %d (%d)", VLAN_TAG_1, ret);
662 
663 	ret = net_eth_vlan_disable(eth_interfaces[0], VLAN_TAG_2);
664 	zassert_equal(ret, 0, "Cannot disable %d (%d)", VLAN_TAG_2, ret);
665 
666 	ret = net_eth_vlan_disable(eth_interfaces[0], VLAN_TAG_3);
667 	zassert_equal(ret, 0, "Cannot disable %d (%d)", VLAN_TAG_2, ret);
668 
669 	ret = net_eth_vlan_disable(vlan_interfaces[3], VLAN_TAG_4);
670 	zassert_equal(ret, 0, "Cannot disable %d (%d)", VLAN_TAG_2, ret);
671 
672 	eth_ctx = net_if_l2_data(eth_interfaces[0]);
673 
674 	iface = net_eth_get_vlan_iface(eth_interfaces[0], VLAN_TAG_1);
675 	zassert_equal_ptr(iface, NULL, "Valid interface for tag %d", VLAN_TAG_1);
676 
677 	iface = net_eth_get_vlan_iface(eth_interfaces[0], VLAN_TAG_2);
678 	zassert_equal_ptr(iface, NULL, "Valid interface for tag %d", VLAN_TAG_2);
679 
680 	ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[0]);
681 	zassert_equal(ret, false, "VLAN enabled for interface 0");
682 
683 	ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[1]);
684 	zassert_equal(ret, false, "VLAN enabled for interface 1");
685 
686 	iface = eth_interfaces[0];
687 	ret = net_eth_vlan_disable(iface, NET_VLAN_TAG_UNSPEC);
688 	zassert_equal(ret, -EBADF, "Invalid VLAN tag value %d", ret);
689 
690 	iface = eth_interfaces[1];
691 	ret = net_eth_vlan_disable(iface, VLAN_TAG_1);
692 	zassert_equal(ret, -ESRCH, "VLAN tag %d disabled for iface 1",
693 		      VLAN_TAG_1);
694 }
695 
test_vlan_enable_all(void)696 static void test_vlan_enable_all(void)
697 {
698 	struct ethernet_context *eth_ctx;
699 	struct net_if *iface;
700 	int ret;
701 
702 	ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_1);
703 	zassert_true(ret == 0 || ret == -EALREADY, "Cannot enable %d", VLAN_TAG_1);
704 	ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_2);
705 	zassert_true(ret == 0 || ret == -EALREADY, "Cannot enable %d", VLAN_TAG_2);
706 	ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_3);
707 	zassert_true(ret == 0 || ret == -EALREADY, "Cannot enable %d", VLAN_TAG_3);
708 	ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_4);
709 	zassert_true(ret == 0 || ret == -EALREADY, "Cannot enable %d", VLAN_TAG_4);
710 
711 	eth_ctx = net_if_l2_data(eth_interfaces[0]);
712 
713 	ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[0]);
714 	zassert_equal(ret, true, "VLAN disabled for interface 0");
715 
716 	ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[0]);
717 	zassert_equal(ret, true, "VLAN disabled for interface 0");
718 
719 	ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[0]);
720 	zassert_equal(ret, true, "VLAN disabled for interface 0");
721 
722 	ret = net_eth_is_vlan_enabled(eth_ctx, eth_interfaces[0]);
723 	zassert_equal(ret, true, "VLAN disabled for interface 0");
724 
725 	iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY));
726 	zassert_not_null(iface, "No dummy iface found");
727 
728 	zassert_equal(net_if_l2(iface), &NET_L2_GET_NAME(DUMMY),
729 		      "Not a dummy interface");
730 
731 	ret = net_eth_vlan_enable(iface, VLAN_TAG_5);
732 	zassert_equal(ret, -EINVAL, "Wrong iface type (%d)", ret);
733 }
734 
test_vlan_disable_all(void)735 static void test_vlan_disable_all(void)
736 {
737 	struct ethernet_context *eth_ctx;
738 	struct net_if *iface;
739 	int ret;
740 
741 	ret = net_eth_vlan_disable(eth_interfaces[0], VLAN_TAG_1);
742 	zassert_equal(ret, 0, "Cannot disable %d", VLAN_TAG_1);
743 	ret = net_eth_vlan_disable(eth_interfaces[0], VLAN_TAG_2);
744 	zassert_equal(ret, 0, "Cannot disable %d", VLAN_TAG_2);
745 	ret = net_eth_vlan_disable(eth_interfaces[0], VLAN_TAG_3);
746 	zassert_equal(ret, 0, "Cannot disable %d", VLAN_TAG_3);
747 	ret = net_eth_vlan_disable(eth_interfaces[0], VLAN_TAG_4);
748 	zassert_equal(ret, 0, "Cannot disable %d", VLAN_TAG_4);
749 
750 	eth_ctx = net_if_l2_data(eth_interfaces[0]);
751 
752 	ret = net_eth_is_vlan_enabled(eth_ctx, vlan_interfaces[0]);
753 	zassert_equal(ret, false, "VLAN enabled for interface 0");
754 
755 	ret = net_eth_is_vlan_enabled(eth_ctx, vlan_interfaces[1]);
756 	zassert_equal(ret, false, "VLAN enabled for interface 1");
757 
758 	ret = net_eth_is_vlan_enabled(eth_ctx, vlan_interfaces[2]);
759 	zassert_equal(ret, false, "VLAN enabled for interface 2");
760 
761 	ret = net_eth_is_vlan_enabled(eth_ctx, vlan_interfaces[3]);
762 	zassert_equal(ret, false, "VLAN enabled for interface 3");
763 
764 	iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY));
765 	zassert_not_null(iface, "No dummy iface found");
766 
767 	zassert_equal(net_if_l2(iface), &NET_L2_GET_NAME(DUMMY),
768 		      "Not a dummy interface");
769 
770 	ret = net_eth_vlan_disable(iface, VLAN_TAG_5);
771 	zassert_equal(ret, -EINVAL, "Wrong iface type (%d)", ret);
772 }
773 
add_neighbor(struct net_if * iface,struct in6_addr * addr)774 static bool add_neighbor(struct net_if *iface, struct in6_addr *addr)
775 {
776 	struct net_linkaddr *ll_addr;
777 	struct net_nbr *nbr;
778 
779 	ll_addr = net_if_get_link_addr(iface);
780 
781 	nbr = net_ipv6_nbr_add(iface, addr, ll_addr, false,
782 			       NET_IPV6_NBR_STATE_REACHABLE);
783 	if (!nbr) {
784 		DBG("Cannot add dst %s to neighbor cache\n",
785 		    net_sprint_ipv6_addr(addr));
786 		return false;
787 	}
788 
789 	DBG("Adding dst %s as [%s] to nbr cache\n",
790 	    net_sprint_ipv6_addr(addr),
791 	    net_sprint_ll_addr(ll_addr->addr, 6));
792 
793 	return true;
794 }
795 
comm_sendto_recvfrom(int client_sock,struct sockaddr * client_addr,socklen_t client_addrlen,int server_sock,struct sockaddr * server_addr,socklen_t server_addrlen)796 static void comm_sendto_recvfrom(int client_sock,
797 				 struct sockaddr *client_addr,
798 				 socklen_t client_addrlen,
799 				 int server_sock,
800 				 struct sockaddr *server_addr,
801 				 socklen_t server_addrlen)
802 {
803 	ssize_t sent = 0;
804 
805 	ARG_UNUSED(server_sock);
806 	ARG_UNUSED(client_addr);
807 	ARG_UNUSED(client_addrlen);
808 
809 	zassert_not_null(client_addr, "null client addr");
810 	zassert_not_null(server_addr, "null server addr");
811 
812 	/*
813 	 * Test client -> server sending
814 	 */
815 
816 	sent = zsock_sendto(client_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL),
817 			    0, server_addr, server_addrlen);
818 	zassert_equal(sent, strlen(TEST_STR_SMALL), "sendto failed (%d vs %d)",
819 		      sent, strlen(TEST_STR_SMALL));
820 
821 	if (k_sem_take(&wait_data, WAIT_TIME)) {
822 		DBG("Timeout while waiting interface data\n");
823 		zassert_false(true, "Timeout");
824 	}
825 
826 	/* The data verification cannot be done easily because we are in the
827 	 * same link and our send/recv MAC addresses are the same and the
828 	 * packet is dropped by core stack.
829 	 * We cannot use loopback as then VLAN would not be used.
830 	 * One option would be to make a fully functional network but that is
831 	 * out of scope for this test application (at least for now).
832 	 * So do not try to verify data, just check the received tag value.
833 	 */
834 }
835 
ZTEST(net_vlan,test_vlan_ipv6_sendto_recvfrom)836 ZTEST(net_vlan, test_vlan_ipv6_sendto_recvfrom)
837 {
838 	struct net_if *iface;
839 	int ret;
840 	int client_sock;
841 	int server_sock;
842 	struct sockaddr_in6 client_addr;
843 	struct sockaddr_in6 server_addr;
844 	struct eth_context *ctx;
845 
846 	/* Setup the interfaces */
847 	test_vlan_enable();
848 	test_vlan_disable_all();
849 
850 	ret = net_eth_vlan_enable(eth_interfaces[0], VLAN_TAG_1);
851 	zassert_equal(ret, 0, "Could not enable %d (%d)", VLAN_TAG_1, ret);
852 
853 	iface = net_eth_get_vlan_iface(eth_interfaces[0], VLAN_TAG_1);
854 	ret = net_eth_is_vlan_enabled(NULL, eth_interfaces[0]);
855 	zassert_equal(ret, true, "VLAN not enabled for interface 1");
856 
857 	ctx = net_if_get_device(eth_interfaces[0])->data;
858 	ctx->expecting_tag = VLAN_TAG_1;
859 
860 	net_if_up(eth_interfaces[0]);
861 	net_if_up(vlan_interfaces[0]);
862 
863 	prepare_sock_udp_v6(MY_IPV6_ADDR, ANY_PORT, &client_sock, &client_addr);
864 	prepare_sock_udp_v6(MY_IPV6_ADDR_SRV, SERVER_PORT, &server_sock, &server_addr);
865 
866 	ret = add_neighbor(iface, &server_addr.sin6_addr);
867 	zassert_true(ret, "Cannot add neighbor");
868 
869 	ret = zsock_bind(server_sock,
870 			 (struct sockaddr *)&server_addr,
871 			 sizeof(server_addr));
872 	zassert_equal(ret, 0, "bind failed");
873 
874 	test_started = true;
875 
876 	comm_sendto_recvfrom(client_sock,
877 			     (struct sockaddr *)&client_addr,
878 			     sizeof(client_addr),
879 			     server_sock,
880 			     (struct sockaddr *)&server_addr,
881 			     sizeof(server_addr));
882 
883 	ret = zsock_close(client_sock);
884 	zassert_equal(ret, 0, "close failed");
885 	ret = zsock_close(server_sock);
886 	zassert_equal(ret, 0, "close failed");
887 }
888 
889 /* This should be the last test to be run so add "zz" to the name */
ZTEST(net_vlan,test_zz_vlan_embed_ll_hdr)890 ZTEST(net_vlan, test_zz_vlan_embed_ll_hdr)
891 {
892 	struct net_if *iface;
893 	int ret;
894 	int client_sock;
895 	struct sockaddr_in6 client_addr;
896 	struct sockaddr_in6 dest_addr;
897 	struct net_if_addr *ifaddr;
898 	ssize_t sent = 0;
899 	struct ifreq ifreq = { 0 };
900 	char ifname[CONFIG_NET_INTERFACE_NAME_LEN];
901 
902 	/* embed ll interface addresses */
903 	static struct in6_addr my_vlan_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0x90, 0, 0, 0,
904 						    0, 0, 0, 0, 0, 0, 0, 0x2 } } };
905 
906 	static struct in6_addr peer_vlan_addr = { { { 0x20, 0x01, 0x0d, 0xb8, 0x90, 0, 0, 0,
907 						    0, 0, 0, 0, 0, 0, 0, 0x1 } } };
908 
909 	ret = net_eth_vlan_enable(embed_ll_interface, VLAN_TAG_6);
910 	zassert_equal(ret, 0, "Could not enable %d (%d)", VLAN_TAG_6, ret);
911 
912 	iface = net_eth_get_vlan_iface(embed_ll_interface, VLAN_TAG_6);
913 	ret = net_eth_is_vlan_enabled(NULL, embed_ll_interface);
914 	zassert_equal(ret, true, "VLAN not enabled for interface");
915 
916 	ifaddr = net_if_ipv6_addr_add(iface,
917 				      &my_vlan_addr,
918 				      NET_ADDR_MANUAL, 0);
919 	if (!ifaddr) {
920 		DBG("Cannot add IPv6 address %s\n",
921 		       net_sprint_ipv6_addr(&my_vlan_addr));
922 		zassert_not_null(ifaddr, "vlan addr");
923 	}
924 
925 	net_if_up(embed_ll_interface);
926 	net_if_up(iface);
927 
928 	test_started = true;
929 
930 	prepare_sock_udp_v6("2001:db8:90::2", ANY_PORT, &client_sock, &client_addr);
931 
932 	ret = net_if_get_name(iface, ifname, sizeof(ifname));
933 	zassert_true(ret > 0, "cannot get interface name (%d/%s)", ret, strerror(-ret));
934 
935 	strncpy(ifreq.ifr_name, ifname, sizeof(ifreq.ifr_name));
936 	ret = zsock_setsockopt(client_sock, SOL_SOCKET, SO_BINDTODEVICE, &ifreq,
937 			       sizeof(ifreq));
938 	zassert_equal(ret, 0, "SO_BINDTODEVICE failed, %d", -errno);
939 
940 	ret = add_neighbor(iface, &peer_vlan_addr);
941 	zassert_true(ret, "Cannot add neighbor");
942 
943 	net_ipaddr_copy(&dest_addr.sin6_addr, &peer_vlan_addr);
944 
945 	sent = zsock_sendto(client_sock, TEST_STR_SMALL, strlen(TEST_STR_SMALL),
946 			    0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
947 	zassert_equal(sent, strlen(TEST_STR_SMALL), "send (%d) failed %d/%s",
948 		      sent, -errno, strerror(errno));
949 
950 	if (k_sem_take(&wait_data, WAIT_TIME)) {
951 		DBG("Timeout while waiting interface data\n");
952 		zassert_false(true, "Timeout");
953 	}
954 
955 	ret = zsock_close(client_sock);
956 	zassert_equal(ret, 0, "close failed");
957 }
958 
setup(void)959 static void *setup(void)
960 {
961 	test_vlan_setup();
962 	test_address_setup();
963 	return NULL;
964 }
965 
ZTEST(net_vlan,test_vlan_enable_disable)966 ZTEST(net_vlan, test_vlan_enable_disable)
967 {
968 	test_vlan_enable();
969 	test_vlan_disable();
970 }
971 
ZTEST(net_vlan,test_vlan_enable_disable_all)972 ZTEST(net_vlan, test_vlan_enable_disable_all)
973 {
974 	test_vlan_enable_all();
975 	test_vlan_disable_all();
976 }
977 
978 ZTEST_SUITE(net_vlan, NULL, setup, NULL, NULL, NULL);
979