1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define NET_LOG_LEVEL CONFIG_NET_UDP_LOG_LEVEL
10 
11 #include <zephyr/logging/log.h>
12 LOG_MODULE_REGISTER(net_test, NET_LOG_LEVEL);
13 
14 #include <zephyr/kernel.h>
15 #include <zephyr/linker/sections.h>
16 
17 #include <zephyr/types.h>
18 #include <stddef.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <zephyr/device.h>
23 #include <zephyr/init.h>
24 #include <zephyr/sys/printk.h>
25 #include <zephyr/net_buf.h>
26 #include <zephyr/net/net_core.h>
27 #include <zephyr/net/net_pkt.h>
28 #include <zephyr/net/net_ip.h>
29 #include <zephyr/net/ethernet.h>
30 #include <zephyr/net/dummy.h>
31 #include <zephyr/net/udp.h>
32 #include <zephyr/random/random.h>
33 
34 #include "ipv4.h"
35 #include "ipv6.h"
36 
37 #include <zephyr/ztest.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 #include "udp_internal.h"
46 
47 #if NET_LOG_LEVEL >= LOG_LEVEL_DBG
48 #define NET_LOG_ENABLED 1
49 #endif
50 #include "net_private.h"
51 #include "ipv4.h"
52 
53 static bool test_failed;
54 static struct k_sem recv_lock;
55 
56 struct net_udp_context {
57 	uint8_t mac_addr[sizeof(struct net_eth_addr)];
58 	struct net_linkaddr ll_addr;
59 };
60 
net_udp_dev_init(const struct device * dev)61 int net_udp_dev_init(const struct device *dev)
62 {
63 	struct net_udp_context *net_udp_context = dev->data;
64 
65 	net_udp_context = net_udp_context;
66 
67 	return 0;
68 }
69 
net_udp_get_mac(const struct device * dev)70 static uint8_t *net_udp_get_mac(const struct device *dev)
71 {
72 	struct net_udp_context *context = dev->data;
73 
74 	if (context->mac_addr[2] == 0x00) {
75 		/* 00-00-5E-00-53-xx Documentation RFC 7042 */
76 		context->mac_addr[0] = 0x00;
77 		context->mac_addr[1] = 0x00;
78 		context->mac_addr[2] = 0x5E;
79 		context->mac_addr[3] = 0x00;
80 		context->mac_addr[4] = 0x53;
81 		context->mac_addr[5] = sys_rand8_get();
82 	}
83 
84 	return context->mac_addr;
85 }
86 
net_udp_iface_init(struct net_if * iface)87 static void net_udp_iface_init(struct net_if *iface)
88 {
89 	uint8_t *mac = net_udp_get_mac(net_if_get_device(iface));
90 
91 	net_if_set_link_addr(iface, mac, 6, NET_LINK_ETHERNET);
92 }
93 
94 static int send_status = -EINVAL;
95 
tester_send(const struct device * dev,struct net_pkt * pkt)96 static int tester_send(const struct device *dev, struct net_pkt *pkt)
97 {
98 	if (!pkt->frags) {
99 		DBG("No data to send!\n");
100 		return -ENODATA;
101 	}
102 
103 	DBG("Data was sent successfully\n");
104 
105 	send_status = 0;
106 
107 	return 0;
108 }
109 
110 struct net_udp_context net_udp_context_data;
111 
112 static struct dummy_api net_udp_if_api = {
113 	.iface_api.init = net_udp_iface_init,
114 	.send = tester_send,
115 };
116 
117 #define _ETH_L2_LAYER DUMMY_L2
118 #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2)
119 
120 NET_DEVICE_INIT(net_udp_test, "net_udp_test",
121 		net_udp_dev_init, NULL,
122 		&net_udp_context_data, NULL,
123 		CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
124 		&net_udp_if_api, _ETH_L2_LAYER, _ETH_L2_CTX_TYPE, 127);
125 
test_setup(void)126 static void *test_setup(void)
127 {
128 	struct net_if *iface;
129 	struct net_if_addr *ifaddr;
130 
131 	struct sockaddr_in6 any_addr6;
132 	const struct in6_addr in6addr_anyaddr = IN6ADDR_ANY_INIT;
133 
134 	struct sockaddr_in6 my_addr6;
135 	struct in6_addr in6addr_my = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
136 					   0, 0, 0, 0, 0, 0, 0, 0x1 } } };
137 
138 	struct sockaddr_in6 peer_addr6;
139 	struct in6_addr in6addr_peer = { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0,
140 					  0, 0, 0, 0x4e, 0x11, 0, 0, 0x2 } } };
141 
142 	struct sockaddr_in any_addr4;
143 	const struct in_addr in4addr_any = { { { 0 } } };
144 
145 	struct sockaddr_in my_addr4;
146 	struct in_addr in4addr_my = { { { 192, 0, 2, 1 } } };
147 
148 	struct sockaddr_in peer_addr4;
149 	struct in_addr in4addr_peer = { { { 192, 0, 2, 9 } } };
150 
151 	iface = net_if_get_first_by_type(&NET_L2_GET_NAME(DUMMY));
152 	test_failed = false;
153 
154 	net_ipaddr_copy(&any_addr6.sin6_addr, &in6addr_anyaddr);
155 	any_addr6.sin6_family = AF_INET6;
156 
157 	net_ipaddr_copy(&my_addr6.sin6_addr, &in6addr_my);
158 	my_addr6.sin6_family = AF_INET6;
159 
160 	net_ipaddr_copy(&peer_addr6.sin6_addr, &in6addr_peer);
161 	peer_addr6.sin6_family = AF_INET6;
162 
163 	net_ipaddr_copy(&any_addr4.sin_addr, &in4addr_any);
164 	any_addr4.sin_family = AF_INET;
165 
166 	net_ipaddr_copy(&my_addr4.sin_addr, &in4addr_my);
167 	my_addr4.sin_family = AF_INET;
168 
169 	net_ipaddr_copy(&peer_addr4.sin_addr, &in4addr_peer);
170 	peer_addr4.sin_family = AF_INET;
171 
172 	k_sem_init(&recv_lock, 0, UINT_MAX);
173 
174 	ifaddr = net_if_ipv6_addr_add(iface, &in6addr_my, NET_ADDR_MANUAL, 0);
175 	if (!ifaddr) {
176 		printk("Cannot add %s to interface %p\n",
177 		       net_sprint_ipv6_addr(&in6addr_my), iface);
178 		zassert_true(0, "exiting");
179 	}
180 
181 	ifaddr = net_if_ipv4_addr_add(iface, &in4addr_my, NET_ADDR_MANUAL, 0);
182 	if (!ifaddr) {
183 		printk("Cannot add %s to interface %p\n",
184 		       net_sprint_ipv4_addr(&in4addr_my), iface);
185 		zassert_true(0, "exiting");
186 	}
187 
188 	return NULL;
189 }
190 
ZTEST(net_shell_test_suite,test_net_shell)191 ZTEST(net_shell_test_suite, test_net_shell)
192 {
193 	int ret;
194 
195 	/* Test that command exists */
196 	ret = shell_execute_cmd(NULL, "net iface");
197 	zassert_equal(ret, 0, "");
198 
199 	/* There is no foobar command */
200 	ret = shell_execute_cmd(NULL, "net foobar");
201 	zassert_equal(ret, 1, "");
202 }
203 
204 ZTEST_SUITE(net_shell_test_suite, NULL, test_setup, NULL, NULL, NULL);
205