1[![Run Status](https://api.shippable.com/projects/58ffb2b81fb3ec0700e1602f/badge?branch=master)](https://app.shippable.com/github/zephyrproject-rtos/net-tools) 2 3# Networking Tools 4 5The comments and instructions below are for the new IP stack in Zephyr. 6 7In a Zephyr default setup, the network tools are pre-installed under the 8`tools` directory. 9 10Here are instructions how to communicate between Zephyr that is running 11inside QEMU, and host device that is running Linux. 12 13For setting up routing and NAT rules to allow access to external networks, please see 14the [README NAT.md](README%20NAT.md) 15 16You need to run *socat* and *tunslip* to create a minimally working 17network setup. 18 19There are convenience scripts (_loop-socat.sh_ and _loop-slip-tap.sh_) for 20running socat and tunslip6 processes. For running these, you need two 21terminals. 22 23Terminal 1: 24``` 25$ ./loop-socat.sh 26``` 27 28Terminal 2: 29``` 30$ sudo ./loop-slip-tap.sh 31``` 32 33After running these scripts you do not need to manually restart 34them when qemu process stops. 35 36In the Qemu side, you need to compile the kernel with proper config. 37Minimally you need these settings active in your project config file. 38``` 39CONFIG_NETWORKING=y 40CONFIG_NET_IPV6=y 41CONFIG_NET_IPV4=y 42CONFIG_NET_YAIP=y 43CONFIG_NET_UDP=y 44CONFIG_NET_LOG=y 45CONFIG_NET_SLIP=y 46CONFIG_SLIP_TAP=y 47CONFIG_SYS_LOG=y 48CONFIG_SYS_LOG_SHOW_COLOR=y 49CONFIG_NANO_TIMEOUTS=y 50CONFIG_TEST_RANDOM_GENERATOR=y 51``` 52 53After you have the loop scripts and Qemu running running you can communicate 54with the Zephyr. 55 56If your have echo-server running in the Qemu, then you can use the echo-client 57tool in net-tools directory to communicate with it. 58``` 59# ./echo-client -i tap0 2001:db8::1 60``` 61The IP stack responds to ping requests if properly configured. 62``` 63$ ping6 -I tap0 -c 1 2001:db8::1 64``` 65You can attach wireshark to tap0 interface to see what data is being 66transferred. 67 68If building with CONFIG_NET_TCP=y in your project config file, it's possible 69to run the echo-server sample in Zephyr, and then test the TCP stack using 70the supplied tcptest.py script: 71``` 72$ ./tcptest.py tap0 2001:db8::1 73``` 74This script will send numbers to the echo-server program, read them back, 75and compare if it got the exact bytes back. Transmission errors, timeouts, 76and time to get the response are all recorded and printed to the standard 77output. 78 79Be sure to use Python 3, as it requires a function from the socket module 80that's only available in this version (wrapper around if_nametoindex(3)). 81 82 83## Using net-setup.sh script to setup host side ethernet interface 84 85The net-setup.sh script can setup an ethernet interface to the host. 86User is able to setup a configuration file that will contain 87commands to setup IP addresses and routes to the host interface. 88This net-setup.sh script will need to be run as a root user. 89 90If no parameters are given, then "zeth" network interface and "zeth.conf" 91configuration file are used. The script waits until user presses CTRL-c 92and then removes the network interface. 93``` 94$ net-setup.sh 95``` 96``` 97$ net-setup.sh --config zeth-vlan.conf 98``` 99``` 100$ net-setup.sh --config my-own-config.conf --iface foobar 101``` 102 103It is also possible to let the script return and then stop the network 104interface later. Is can be done by first creating the interface with 105"start" or "up" command, and then later remove the interface with 106"stop" or "down" command. 107``` 108$ net-setup.sh start 109do your things here 110$ net-setup.sh stop 111``` 112``` 113$ net-setup.sh --config my-own-config.conf up 114do your things here 115$ net-setup.sh --config my-own-config.conf down 116``` 117 118Any extra parameters that the script does not know, are passed directly 119to "ip" command. 120``` 121$ net-setup.sh --config my-own-config.conf --iface foo user bar 122``` 123 124## Using encrypted SSL link with echo-* programs 125 126Install stunnel 127 128Fedora: 129``` 130$ dnf install stunnel 131``` 132Ubuntu: 133``` 134$ apt-get install stunnel4 -y 135``` 136Finally run the stunnel script in Linux 137``` 138$ ./stunnel.sh 139``` 140And connect echo-client to this SSL tunnel (note that the IP address 141is the address of Linux host where the tunnel end point is located). 142``` 143$ ./echo-client -p 4243 2001:db8::2 -t 144``` 145If you are running echo-client in Zephyr QEMU, then run echo-server like 146this: 147``` 148$ ./echo-server -p 4244 -i tap0 149``` 150 151If you want to re-create the certificates in echo-server and echo-client in 152Zephyr net samples, then they can be created like this (note that you do not 153need to do this as the certs have been prepared already in echo-server and 154echo-client sample sources): 155``` 156$ openssl genrsa -out echo-apps-key.pem 2048 157$ openssl req -new -x509 -key echo-apps-key.pem -out echo-apps-cert.pem \ 158 -days 10000 -subj '/CN=localhost' 159``` 160The cert that is to be embedded into test_certs.h in echo-server and 161echo-client, can be generated like this: 162``` 163$ openssl x509 -in echo-apps-cert.pem -outform DER | \ 164 hexdump -e '8/1 "0x%02x, " "\n"' | sed 's/0x ,//g' 165``` 166The private key to be embedded into test_certs.h in echo-server can be 167generated like this: 168``` 169$ openssl pkcs8 -topk8 -inform PEM -outform DER -nocrypt \ 170 -in echo-apps-key.pem | hexdump -e '8/1 "0x%02x, " "\n"' | \ 171 sed 's/0x ,//g' 172``` 173 174If you want to re-create the signed certificates in echo-server in Zephyr 175net samples and echo-client in net-tools, then they can be created like this 176(note that you do not need to do this as the certs have been prepared already 177in echo-server and echo-client sources): 178``` 179CA 180-- 181$ openssl genrsa -out ca_privkey.pem 2048 182$ openssl req -new -x509 -days 36500 -key ca_privkey.pem -out ca.crt -subj "/CN=exampleCA" 183 184Convert to DER format 185$ openssl x509 -in ca.crt -outform DER -out ca.der 186``` 187 188``` 189Client 190------ 191$ openssl genrsa -out client_privkey.pem 2048 192$ openssl req -new -key client_privkey.pem -out client.csr -subj "/CN=exampleClient" 193$ openssl x509 -req -CA ca.crt -CAkey ca_privkey.pem -days 36500 -in client.csr -CAcreateserial -out client.crt 194``` 195 196``` 197Server 198------ 199$ openssl genrsa -out server_privkey.pem 2048 200$ openssl req -new -key server_privkey.pem -out server.csr -subj "/CN=localhost" 201$ openssl x509 -req -CA ca.crt -CAkey ca_privkey.pem -days 36500 -in server.csr -CAcreateserial -out server.crt 202 203Convert to DER format 204$ openssl x509 -in server.crt -outform DER -out server.der 205$ openssl pkcs8 -topk8 -inform PEM -outform DER -nocrypt -in server_privkey.pem -out server_privkey.der 206``` 207 208Copy ca.crt, client.crt and client_privkey.pem to net-tools. 209Copy ca.der, server.der and server_privkey.der to samples/net/sockets/echo-server/src/. 210 211Enable NET_SAMPLE_CERTS_WITH_SC in samples/net/sockets/echo-server and build the sample. 212Use stunnel_sc.conf in stunnel.sh to run echo-client with signed certificates. 213 214## Using DTLS link with echo-* programs 215 216For DTLS client functionality, you can do this 217 218``` 219$ ./dtls-client -c echo-apps-cert.pem 2001:db8::1 220``` 221or 222``` 223$ ./dtls-client -c echo-apps-cert.pem 192.0.2.1 224``` 225For DTLS server functionality, you can do this 226 227``` 228$ ./dtls-server 229``` 230 231## TLS connecitivity errors 232 233If you see this error print in zephyr console 234 235[net/app] [ERR] _net_app_ssl_mainloop: Closing connection -0x7180 (SSL - Verification of the message MAC failed) 236 237Then increasing the mbedtls heap size might help. So you can set the option 238CONFIG_MBEDTLS_HEAP_SIZE to some higher value. 239 240Example: 241``` 242CONFIG_MBEDTLS_HEAP_SIZE=30000 243``` 244 245## PPP Connectivity 246 247You can test the PPP connectivity running in Qemu in Zephyr using pppd that is 248running in Linux host. You need to run *socat* and *pppd* to create 249a minimally working network setup. 250 251There are convenience scripts (_loop-ppp-dev.sh_ and _loop-pppd.sh_) for 252running socat and pppd processes. For running these, you need two 253terminals. 254 255Terminal 1: 256``` 257$ ./loop-ppp-dev.sh 258``` 259 260Terminal 2: 261``` 262$ sudo ./loop-pppd.sh 263``` 264 265After this, start PPP enabled Zephyr application. For example Zephyr 266*echo-server* sample in samples/net/sockets/echo_server has _overlay-ppp.conf_ 267file that enables PPP support. 268