1 /*
2  * Copyright (c) 2018 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_DECLARE(net_echo_client_sample, LOG_LEVEL_DBG);
9 
10 #include <zephyr/kernel.h>
11 
12 #include <zephyr/net/ethernet.h>
13 
14 /* User data for the interface callback */
15 struct ud {
16 	struct net_if *first;
17 	struct net_if *second;
18 };
19 
iface_cb(struct net_if * iface,void * user_data)20 static void iface_cb(struct net_if *iface, void *user_data)
21 {
22 	struct ud *ud = user_data;
23 
24 	if (net_if_l2(iface) != &NET_L2_GET_NAME(VIRTUAL)) {
25 		return;
26 	}
27 
28 	if (!ud->first) {
29 		ud->first = iface;
30 		return;
31 	}
32 
33 	if (!ud->second) {
34 		ud->second = iface;
35 		return;
36 	}
37 }
38 
setup_iface(struct net_if * eth_iface,struct net_if * iface,const char * ipv6_addr,const char * ipv4_addr,uint16_t vlan_tag)39 static int setup_iface(struct net_if *eth_iface,
40 		       struct net_if *iface,
41 		       const char *ipv6_addr,
42 		       const char *ipv4_addr,
43 		       uint16_t vlan_tag)
44 {
45 	struct net_if_addr *ifaddr;
46 	struct in_addr addr4;
47 	struct in6_addr addr6;
48 	int ret;
49 
50 	ret = net_eth_vlan_enable(eth_iface, vlan_tag);
51 	if (ret < 0) {
52 		LOG_ERR("Cannot enable VLAN for tag %d (%d)", vlan_tag, ret);
53 	}
54 
55 	if (net_addr_pton(AF_INET6, ipv6_addr, &addr6)) {
56 		LOG_ERR("Invalid address: %s", ipv6_addr);
57 		return -EINVAL;
58 	}
59 
60 	ifaddr = net_if_ipv6_addr_add(iface, &addr6, NET_ADDR_MANUAL, 0);
61 	if (!ifaddr) {
62 		LOG_ERR("Cannot add %s to interface %p", ipv6_addr, iface);
63 		return -EINVAL;
64 	}
65 
66 	if (net_addr_pton(AF_INET, ipv4_addr, &addr4)) {
67 		LOG_ERR("Invalid address: %s", ipv4_addr);
68 		return -EINVAL;
69 	}
70 
71 	ifaddr = net_if_ipv4_addr_add(iface, &addr4, NET_ADDR_MANUAL, 0);
72 	if (!ifaddr) {
73 		LOG_ERR("Cannot add %s to interface %p", ipv4_addr, iface);
74 		return -EINVAL;
75 	}
76 
77 	LOG_DBG("Interface %p VLAN tag %d setup done.", iface, vlan_tag);
78 
79 	return 0;
80 }
81 
init_vlan(void)82 int init_vlan(void)
83 {
84 	struct net_if *iface;
85 	struct ud ud;
86 	int ret;
87 
88 	if (CONFIG_NET_VLAN_COUNT == 0) {
89 		LOG_DBG("No VLAN interfaces defined.");
90 		return 0;
91 	}
92 
93 	iface = net_if_get_first_by_type(&NET_L2_GET_NAME(ETHERNET));
94 	if (!iface) {
95 		LOG_ERR("No ethernet interfaces found.");
96 		return -ENOENT;
97 	}
98 
99 	memset(&ud, 0, sizeof(ud));
100 
101 	net_if_foreach(iface_cb, &ud);
102 
103 	ret = setup_iface(iface, ud.first,
104 			  CONFIG_NET_SAMPLE_IFACE2_MY_IPV6_ADDR,
105 			  CONFIG_NET_SAMPLE_IFACE2_MY_IPV4_ADDR,
106 			  CONFIG_NET_SAMPLE_IFACE2_VLAN_TAG);
107 	if (ret < 0) {
108 		return ret;
109 	}
110 
111 	ret = setup_iface(iface, ud.second,
112 			  CONFIG_NET_SAMPLE_IFACE3_MY_IPV6_ADDR,
113 			  CONFIG_NET_SAMPLE_IFACE3_MY_IPV4_ADDR,
114 			  CONFIG_NET_SAMPLE_IFACE3_VLAN_TAG);
115 	if (ret < 0) {
116 		return ret;
117 	}
118 
119 	/* Bring up the VLAN interface automatically */
120 	net_if_up(ud.first);
121 	net_if_up(ud.second);
122 
123 	return 0;
124 }
125