Sub topic 2
Tunnelling: packets inside packets
This chapter assumes you know IP addressing and subnets, how a routing table and default route work, and the difference between TCP and UDP. It also assumes you can use a Linux shell. The rest of the tutorial builds on the mechanism described here.
The problem a tunnel solves
Two offices each use private addresses: 10.1.0.0/16 in one and 10.2.0.0/16 in the other. Routers on the public internet do not route private ranges, so a packet from 10.1.0.5 to 10.2.0.9 is dropped at the first provider router. A tunnel solves this without changing the internet. It carries the private packet as the payload of a second packet that the internet can route.
The same idea carries IPv6 across an IPv4-only network, or a protocol the middle network does not support across one that does. In each case the tunnel hides the inner packet from the networks in between.
Encapsulation
Encapsulation means placing one packet inside another as data. You already rely on it in the normal stack. A TCP segment is the payload of an IP packet, and that packet is the payload of an Ethernet frame. A tunnel applies the same idea at one layer: an IP packet becomes the payload of another IP packet.
The packet that carries the other is the outer packet. The packet being carried is the inner packet.
| Part | Role | Example values |
|---|---|---|
| Outer IP header | Gets the packet across the underlay | `203.0.113.1` to `198.51.100.1` |
| Tunnel header | Says what is inside, and may add fields such as keys or sequence numbers | GRE, ESP, UDP plus a VPN header |
| Inner IP header | Original addresses, used after the tunnel | `10.1.0.5` to `10.2.0.9` |
| Inner payload | The original TCP or UDP segment and data | HTTPS request |
The network the outer packet crosses is the underlay. The network formed by the inner addresses is the overlay. Only the two tunnel endpoints understand both.
Following one packet
Host A (10.1.0.5) sends to Host B (10.2.0.9). Gateway R1 has public address 203.0.113.1 and R2 has 198.51.100.1. The two gateways are the tunnel endpoints.
The steps are:
- Host A sends the packet to its default gateway, R1, as usual.
- R1 looks up
10.2.0.9in its routing table. A route for10.2.0.0/16points at a tunnel interface instead of a physical one. - The tunnel interface adds the tunnel header and a new outer IP header. The outer source is R1's public address and the outer destination is R2's. The inner packet is untouched.
- Internet routers forward the packet using the outer header alone. They never read the inner addresses.
- R2 sees that the outer destination is itself and that the protocol field names a tunnel. It strips the outer header and tunnel header.
- R2 treats the recovered inner packet as a new packet to route. It finds
10.2.0.9on a connected network and delivers it.
Two routing decisions
Every tunnelled packet goes through two route lookups at the sender. The first uses the inner destination, and its result is "send via the tunnel interface." The second uses the outer destination, which is the remote endpoint address, and its result is an ordinary physical interface and next hop.
This produces the most common tunnel misconfiguration, recursive routing. Suppose you add a route that sends all traffic, including traffic to the remote endpoint's public address, into the tunnel. The outer packet then matches the tunnel route, gets encapsulated again, and never leaves. Tunnel software avoids this by keeping a specific host route to the remote endpoint through the physical interface. The chapter on split and full tunnelling returns to this.
A minimal example: IP-in-IP
IP-in-IP is the simplest tunnel. It adds only an outer IP header, with protocol number 4 in the outer header. Host A's gateway (public address 192.0.2.1) and host B's gateway (198.51.100.1) can be configured like this. Run it on the first gateway. The second one mirrors it with local and remote swapped and the 10.10.10.x addresses exchanged.
sudo ip tunnel add tun0 mode ipip local 192.0.2.1 remote 198.51.100.1
sudo ip addr add 10.10.10.1/30 dev tun0
sudo ip link set tun0 up
sudo ip route add 10.20.0.0/16 via 10.10.10.2 dev tun0The first command creates a virtual interface named tun0. Any packet sent out of it is wrapped in an outer IP header with those source and destination addresses. The 10.10.10.0/30 addresses form a small link network that exists only inside the tunnel. The last command is the route from the previous section: traffic for the remote subnet goes to the far end of the tunnel.
To see encapsulation on the wire, capture on the physical interface while pinging through the tunnel:
sudo tcpdump -ni eth0 -v 'ip proto 4'Each line shows an outer header between the two public addresses and, nested inside it, the inner header with the private addresses. If you capture on tun0 instead, you see only the inner packets, because that interface sits above the encapsulation step.
What identifies the payload
The outer header must tell the receiver what is inside, or it cannot decapsulate correctly. Tunnels do this in one of two ways. Some use the IP protocol field, and others sit on top of UDP and use a port number.
| Tunnel | Carried how | Identifier |
|---|---|---|
| IP-in-IP | Directly over IP | IP protocol 4 |
| 6in4 (IPv6 over IPv4) | Directly over IP | IP protocol 41 |
| GRE | Directly over IP | IP protocol 47 |
| IPsec ESP | Directly over IP | IP protocol 50 |
| WireGuard, VXLAN, many TLS-style VPNs | Over UDP (some over TCP) | UDP port |
The choice matters in practice. Firewalls and NAT devices often pass TCP and UDP freely but block or mishandle unusual IP protocols. A tunnel that rides on a UDP port is more likely to cross a hotel network or a home router. Later chapters return to this under NAT traversal.
What encapsulation does not do
A tunnel on its own has three limits.
- No secrecy. The inner packet is copied into the outer payload as is. Anyone who can capture the outer packets can read the inner ones. The capture above shows this. Encryption is a separate feature.
- No authentication. A plain tunnel endpoint accepts any packet addressed to it with the right protocol number. An attacker who can send packets to the endpoint can inject inner packets with forged source addresses.
- Extra size. The outer and tunnel headers add bytes. IP-in-IP adds 20, and other tunnels add more. If the inner packet was already near the link's maximum size, the result no longer fits. The overhead and MTU chapter covers that failure.
There is also a smaller behaviour to know. The inner packet's TTL is not decremented while it crosses the underlay, because routers there see only the outer TTL. To traceroute run inside the overlay, the whole tunnel looks like one hop.
Quick checks when a tunnel does not work
- Capture on the physical interface. If you see no outer packets leaving, the route to the overlay subnet is missing or points at the wrong interface.
- If outer packets leave but nothing returns, check that the far end has the matching
localandremotevalues and a return route for the inner source subnet. - If outer packets arrive at the far end but nothing comes out of the tunnel interface, a firewall may be dropping the tunnel's protocol number or port.
Exercise
A gateway has the route 10.2.0.0/16 dev tun0, and its tunnel remote endpoint is 198.51.100.1. A packet for 10.2.0.9 arrives. Which address does the first routing lookup use, which does the second use, and what would go wrong if the gateway also had a route 0.0.0.0/0 dev tun0 and no specific route to 198.51.100.1?
Show answer
The first lookup uses the inner destination, 10.2.0.9, and selects tun0. The second lookup uses the outer destination, 198.51.100.1, and must select a physical interface. With only a default route into tun0, the outer packet would match that route, be encapsulated again, and loop without ever leaving the machine. The fix is a specific host route to 198.51.100.1 through the physical interface.