1# Create NAT and routing for Zephyr native network on Linux
2
3This page describes how you can set up a networking rules that allows you to run Zephyr applications
4build to native_posix or QEMU targets and get them to access public Internet.
5
6
7
8![Network overview](nat.png)
9
10Zephyr applications, when build on native_posix or various qemu targets,
11run simulated Ethernet driver that attaches itself to a network interface called zeth.
12This is Linux TAP interface so it can send and receive Ethernet frames.
13
14In order to have networking with Zephyr, you need to configure IP address on linux kernel side,
15set up the routing and have NAT (Network Address Translation / IP masquerading) for outbound
16traffic.
17
18Optionally, having dnsmasq to serve as small stand-alone DHCP and DNS server helps.
19When running dnsmasq, it must bind into the zeth interface, so it does not serve any
20requests coming from outside network.
21
22## Building the Zephyr application for native networking
23
24At minimun, you need following Kconfig settings for your *native_posix* application to have
25working network
26
27```
28# General network requirements
29CONFIG_NETWORKING=y
30CONFIG_NET_IPV4=y
31CONFIG_NET_TCP=y
32CONFIG_NET_UDP=y
33CONFIG_NET_SOCKETS=y
34
35# Native posix requirements
36CONFIG_ENTROPY_GENERATOR=y
37CONFIG_TEST_RANDOM_GENERATOR=y
38
39CONFIG_NET_L2_ETHERNET=y
40CONFIG_ETH_NATIVE_POSIX=y
41CONFIG_ETH_NATIVE_POSIX_RANDOM_MAC=y
42
43# IPv4 config
44CONFIG_NET_CONFIG_SETTINGS=y
45CONFIG_NET_CONFIG_NEED_IPV4=y
46CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
47CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2"
48CONFIG_DNS_RESOLVER=y
49CONFIG_DNS_RESOLVER_MAX_SERVERS=1
50CONFIG_DNS_SERVER_IP_ADDRESSES=y
51CONFIG_DNS_SERVER1="192.0.2.2"
52```
53
54## Running the native_posix app
55
56You can test your configuration for example in `zephyr/samples/net/sockets/http_get` by creating
57a file called `boards/native_posix.conf` with the content from the sample above.
58
59```sh
60cd zephyr/samples/net/sockets/http_get
61# create boards/native_posix.conf
62
63# Building the application
64west build -b native_posix -- -DOVERLAY_CONFIG=overlay-tls.conf
65
66# Run the application
67# Note that you need to run it realtime (-rt) so responses don't timeout,
68# otherwise it would run faster.
69# Also any TLS/DTLS traffic needs proper seed or random generator,
70# $RANDOM is adequate enough for testing purposes, not secure though,
71./build/zephyr/zephyr.elf -rt -seed=$RANDOM
72
73# Run application that use shell, for example lwm2m_client
74# These spawn the shell and logs into a virtual serial port on /dev/pts/X
75# it is easier to open a x-terminal to this serial port when you start the application
76./build/zephyr/zephyr.elf -rt -seed=$RANDOM -attach_uart
77```
78
79You might need to supply also `-attach_uart_cmd` parameter, in case default X-terminal is not
80installed. For example `-attach_uart_cmd='x-terminal-emulator -e screen %s &'` works.
81