In part 1, the small container had the address 172.17.0.2. This part explains where that address came from, and why a server on the internet sees a different one. The address was lent to the machine, and a router rewrote it on the way out.
At the post office, a new courier has no street number. Bub walks up to the front desk and asks for one. The desk lends a number for a while. It also names the depot, which is the gateway, and the address book, which is the resolver. Later, a letter leaves the city. A clerk replaces its return address with the post office’s own address and notes who the letter was for.
An address is lent
A machine with no address does not know whom to ask. It sends a message to every machine on the local network, and a DHCP server answers. We can build both sides with network namespaces: a server that owns 192.168.50.0/24, and a machine in its own namespace with no address yet.
docker run --rm --privileged alpine sh -c '
apk add -q iproute2 dnsmasq
ip netns add lab
ip link add veth-s type veth peer name veth-c
ip link set veth-c netns lab
ip addr add 192.168.50.1/24 dev veth-s
ip link set veth-s up
ip netns exec lab ip link set veth-c up
dnsmasq -C /dev/null -k --port=0 --interface=veth-s --bind-interfaces \
--dhcp-range=192.168.50.100,192.168.50.150,2m \
--dhcp-option=option:router,192.168.50.1 \
--log-dhcp --log-facility=- 2>/tmp/log &
sleep 1
ip netns exec lab udhcpc -i veth-c -n -q -f 2>&1 | grep lease
grep -oE "DHCP(DISCOVER|OFFER|REQUEST|ACK)" /tmp/log | awk "!s[\$0]++"
ip netns exec lab ip -br addr show veth-c
ip netns exec lab ip route'udhcpc: lease of 192.168.50.147 obtained from 192.168.50.1, lease time 120 DHCPDISCOVER DHCPOFFER DHCPREQUEST DHCPACK veth-c@if13 UP 192.168.50.147/24 fe80::5c7f:86ff:fe97:d0de/64 default via 192.168.50.1 dev veth-c metric 212 192.168.50.0/24 dev veth-c proto kernel scope link src 192.168.50.147
Here is what each marked part does:
- 1
ip netns add labcreates a network namespace: a separate set of network settings, like a second machine. - 2
ip link add veth-s type veth peer name veth-cmakes a virtual cable with two ends. The next line moves one end intolab. - 3
ip addr add 192.168.50.1/24 dev veth-sgives the server end its address. - 4
dnsmasq ... --dhcp-range=192.168.50.100,192.168.50.150,2mstarts a DHCP server that lends addresses from.100to.150for two minutes. - 5
ip netns exec lab udhcpc -i veth-cruns the DHCP client insidelab. It asks for an address onveth-c.
The machine asks, the server offers, the machine requests the offered address, and the server confirms. The awk filter removed repeated lines. The answer has three parts. The address 192.168.50.147/24 is the address and prefix of part 1. The default via 192.168.50.1 line is the gateway of part 1. A DHCP server can also name the resolver of part 2, but this small server does not. The number 120 is the lease time in seconds. The machine asks to renew the lease before the time ends. If it does not, the server may lend the address to another machine.
DISCOVER: a machine with no address asks everyone on the network for one.
An address is rewritten
The addresses 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 are private. Routers on the internet do not forward packets for them, so a machine with a private address cannot talk to the internet directly. The router in front of it uses NAT. We can build a client, a router and a server in three namespaces. One rule on the router rewrites the source address of the packets that leave the client network:
Which address will the server see: 10.0.1.2 or something else?
docker run --rm --privileged alpine sh -c '
apk add -q iproute2 iptables netcat-openbsd
for n in client router server; do ip netns add $n; done
ip link add c0 type veth peer name r1
ip link set c0 netns client; ip link set r1 netns router
ip link add r2 type veth peer name s0
ip link set r2 netns router; ip link set s0 netns server
ip -n client addr add 10.0.1.2/24 dev c0; ip -n client link set c0 up
ip -n router addr add 10.0.1.1/24 dev r1; ip -n router addr add 10.0.2.1/24 dev r2
ip -n router link set r1 up; ip -n router link set r2 up
ip -n server addr add 10.0.2.2/24 dev s0; ip -n server link set s0 up
ip -n client route add default via 10.0.1.1
ip -n server route add default via 10.0.2.1
ip netns exec router sysctl -q -w net.ipv4.ip_forward=1
ip netns exec router iptables -t nat -A POSTROUTING -s 10.0.1.0/24 -o r2 -j MASQUERADE
ip netns exec server nc -l -p 9000 >/dev/null &
sleep 1
ip netns exec client nc -w 2 10.0.2.2 9000 </dev/null &
sleep 1
ip netns exec server ss -tn | grep 9000
ip netns exec router grep 9000 /proc/net/nf_conntrack'ESTAB 0 0 10.0.2.2:9000 10.0.2.1:46322 ipv4 2 tcp 6 431998 ESTABLISHED src=10.0.1.2 dst=10.0.2.2 sport=46322 dport=9000 src=10.0.2.2 dst=10.0.2.1 sport=9000 dport=46322 [ASSURED] mark=0 zone=0 use=2
Here is what each marked part does:
- 1We make three namespaces,
client,routerandserver, and join them with virtual cables.ip -n client ...runsipinside the namespaceclient. - 2
sysctl net.ipv4.ip_forward=1lets the router pass packets between its two cards. - 3
iptables -t nat -A POSTROUTING -s 10.0.1.0/24 -o r2 -j MASQUERADEis the NAT rule. For packets from the client network that leave throughr2, write the router's own address as the source. - 4
nc -l -p 9000is the server, andnc -w 2 10.0.2.2 9000is the client. - 5
ss -tnon the server lists its connections./proc/net/nf_conntrackis the router's table of connections.
The server sees a connection from 10.0.2.1, the router. It does not see 10.0.1.2. The second line is the router’s memory. The first half, src=10.0.1.2 dst=10.0.2.2, is what the client really sent. The second half, src=10.0.2.2 dst=10.0.2.1, is the reply the router expects, addressed to itself. When it arrives, the router looks up the table, writes 10.0.1.2 back on it, and sends it into the private network. The rule target that does this is MASQUERADE.
What follows from a rewritten address
Many machines can share one public address, because the table tells their connections apart. A log on the server holds the router’s address, not the machine that made the request. An address in a log is a clue and not a name. Without a port forward, a machine behind NAT does not receive a new connection from outside, because the table has no entry for it yet. This protects a little, but NAT is not a firewall, and a private address does not make a machine safe. Anything on the same network, or on the far side of a VPN, can reach it directly.
Key points
- A DHCP server lends a machine an address, a gateway and often a resolver, for a limited time called a lease.
- A DHCP exchange has four messages: DISCOVER, OFFER, REQUEST and ACK. The machine does not check who sent the offer.
- Private ranges are not routed on the internet. A router with NAT rewrites the source address of the packets that leave a private network.
- The router keeps a table of connections to rewrite the replies back.
- An address in a server log can be the address of a router that serves many machines.
Next, in part 7: a container sits behind this kind of router, and -p makes Docker write a NAT rule for you.