Sub topic 14
Tunneling: a packet inside a packet
This chapter assumes you know the layers from earlier chapters: how a header and payload make up each protocol data unit (PDU), how IPv4 and Ethernet headers are laid out, and how MTU limits packet size. You should also be comfortable reading tcpdump output and running commands on Linux.
What a tunnel is
Normal encapsulation follows the layer order: application data, then a TCP or UDP header, then an IP header, then an Ethernet header. A tunnel breaks that order on purpose. It takes a complete packet, with its own IP header, and treats it as the payload of another packet. That makes it a packet inside a packet.
The inner packet is the passenger. The outer packet is the carrier. Routers between the tunnel endpoints read only the carrier's header and forward by its destination address. They never look at the passenger.
The outer header is added by the tunnel entry and removed by the tunnel exit. The two endpoints are ordinary hosts or routers that agree to wrap and unwrap traffic. Nothing else on the path needs to know the tunnel exists.
Why tunnels exist
- Crossing a network that cannot carry your traffic. IPv6 packets can travel across an IPv4-only network if they are wrapped in IPv4 packets.
- Joining private networks. A branch office with addresses such as
10.1.0.0/16can reach headquarters over the internet. Private addresses are not routable there, so the tunnel carries them inside packets with public addresses. - Encryption. VPN protocols such as IPsec and WireGuard encrypt the inner packet and then wrap it, so the path sees only the outer header.
- Virtual networks. Data centers use VXLAN to carry Ethernet frames across an IP network, so virtual machines on different hosts appear to share one Layer 2 segment.
Layering inside the outer packet
The outer header names the inner protocol, using the same fields as any other IP packet. In IPv4 this is the Protocol field. In a tunnel it holds a value that means "the payload is another IP packet" or "the payload is a tunnel header".
| Tunnel type | Outer protocol or port | What the payload holds | Added bytes (IPv4 outer) |
|---|---|---|---|
| IP-in-IP | IP protocol 4 | An IPv4 packet | 20 |
| IPv6-in-IPv4 (6in4) | IP protocol 41 | An IPv6 packet | 20 |
| GRE (basic) | IP protocol 47 | GRE header, then any network-layer packet | 24 |
| VXLAN | UDP port 4789 | VXLAN header, then an Ethernet frame | 36 over the inner frame |
| WireGuard | UDP (port is configurable) | Encrypted inner IP packet | 60 |
The "added bytes" column is the extra space the outer headers take. It matters in the size section below.
Following one packet through a tunnel
Host A at 10.1.0.5 sends to host B at 10.2.0.9. Both sit behind tunnel routers R1 and R2. R1 has public address 198.51.100.1 and R2 has 198.51.100.2.
Step by step:
- A builds a normal packet with source
10.1.0.5and destination10.2.0.9and sends it to its default gateway, R1. - R1's routing table says that
10.2.0.0/16is reached through the tunnel. R1 adds an outer IP header with source198.51.100.1, destination198.51.100.2and the tunnel's protocol number. - Internet routers forward the packet by the outer destination. Each one decrements the outer TTL. The inner TTL is untouched, because no router on the path reads it.
- R2 receives the packet because the outer destination is its own address. It sees the tunnel protocol number, removes the outer header, and processes the inner packet as if it had just arrived.
- R2 routes the inner packet to B using the inner destination.
This is the same decapsulation you learned earlier, applied twice. The first pass removes the Ethernet and outer IP headers. The second removes nothing new at R2, but R2 now has a fresh IP packet to route.
Building and unwrapping an IP-in-IP packet
The following Python program builds an inner IPv4 packet carrying a UDP datagram, wraps it in an outer IPv4 header with protocol 4, and then unwraps it. It computes real header checksums, so the result is a valid packet. The UDP checksum is left at 0, which IPv4 permits.
import socket
import struct
def checksum(header: bytes) -> int:
"""Internet checksum: ones' complement sum of 16-bit words."""
total = sum(struct.unpack("!%dH" % (len(header) // 2), header))
while total >> 16:
total = (total & 0xFFFF) + (total >> 16)
return ~total & 0xFFFF
def ipv4(src: str, dst: str, proto: int, payload: bytes, ttl: int = 64) -> bytes:
header = struct.pack(
"!BBHHHBBH4s4s",
0x45, 0, 20 + len(payload), 0, 0, ttl, proto, 0,
socket.inet_aton(src), socket.inet_aton(dst),
)
csum = checksum(header)
header = header[:10] + struct.pack("!H", csum) + header[12:]
return header + payload
# Inner packet: host A to host B, UDP port 5000 to 53, TTL 64
udp = struct.pack("!HHHH", 5000, 53, 8 + 5, 0) + b"hello"
inner = ipv4("10.1.0.5", "10.2.0.9", 17, udp)
# Outer packet: R1 to R2, protocol 4 means "an IPv4 packet follows"
outer = ipv4("198.51.100.1", "198.51.100.2", 4, inner, ttl=60)
print("inner:", len(inner), "bytes, outer:", len(outer), "bytes")
# Decapsulate at R2
ihl = (outer[0] & 0x0F) * 4
assert checksum(outer[:ihl]) == 0 # a valid header sums to zero
assert outer[9] == 4 # protocol 4: payload is an IPv4 packet
recovered = outer[ihl:]
src = socket.inet_ntoa(recovered[12:16])
dst = socket.inet_ntoa(recovered[16:20])
print("inner source:", src, "inner destination:", dst, "inner TTL:", recovered[8])
print("same as original:", recovered == inner)The output shows the outer packet is exactly 20 bytes longer than the inner one, and the recovered packet matches the original byte for byte. The inner TTL is still 64, even though the outer TTL started at 60 and would drop at every hop.
Which fields belong to which header
In a tunnel, "what stays the same end to end" has two answers, one per header level.
- Inner header. Source and destination addresses stay the same from A to B. The tunnel hides this packet from the routers it crosses.
- Outer header. The source and destination are the tunnel endpoints. The TTL changes at every hop between R1 and R2.
- Ethernet header. It is rebuilt at every link, as in normal forwarding. Outer encapsulation does not change that.
A common mistake is to expect a traceroute from A to B to show the internet routers between R1 and R2. It does not. The inner TTL is decremented at R1 (as a normal router) and at R2, but not at the routers in between. The whole tunnel looks like a single hop.
Size limits
A tunnel adds bytes, but the links on the path still have the same MTU. If the underlay MTU is 1500 and the tunnel adds 20 bytes, the largest inner packet that fits is 1480 bytes. Linux sets the tunnel interface MTU to match, so a typical IP-in-IP interface reports 1480.
The effects reach TCP too. A TCP connection through the tunnel negotiates MSS from the MTU it sees. If the sender believes the path MTU is 1500, it will send segments that, once wrapped, exceed 1500 and must be dropped or fragmented.
Failure modes to know:
- Fragmentation after encapsulation. If the inner packet allows fragmentation, the tunnel entry can fragment the outer packet. The exit must reassemble it, which costs CPU and fails if any fragment is lost.
- Don't Fragment set on the inner packet. The tunnel entry should drop it and send an ICMP "fragmentation needed" message to the source. If firewalls block that ICMP, the connection stalls. Small packets such as the TCP handshake work, but large transfers hang. This is the classic "VPN connects but pages won't load" symptom.
- MSS clamping. Many tunnel endpoints rewrite the MSS option in TCP SYN packets so senders choose smaller segments from the start. This avoids relying on ICMP.
Seeing a tunnel on Linux
You can create an IP-in-IP tunnel between two Linux hosts. Run this on the first host (with its mirror image on the second, swapping local and remote), then capture on the physical interface:
sudo ip tunnel add ipip0 mode ipip local 198.51.100.1 remote 198.51.100.2
sudo ip addr add 10.10.0.1/30 dev ipip0
sudo ip link set ipip0 up
ping -c 2 10.10.0.2
sudo tcpdump -i eth0 -nn -v ip proto 4Replace the addresses and eth0 with values from your own hosts. In the capture, each line shows two IP headers. The first is the outer header with proto IPIP (4), and the second is the inner header between 10.10.0.1 and 10.10.0.2. Capturing on ipip0 instead shows only the inner packets, because the tunnel interface sits above the encapsulation step.
Summary of the model
Tunneling uses the same two rules you have already seen: add a header to send, remove a header to receive. The difference is that the thing being wrapped is a whole packet and the wrapper is another IP header. Routers on the path forward only by the outermost header. The inner packet's addresses are meaningful only at the endpoints. Every tunnel costs header bytes, so you must account for them in MTU and MSS.
Exercise
A tunnel uses GRE over IPv4 (20 bytes of outer IP header plus 4 bytes of GRE header). The physical links have an MTU of 1500. (a) What is the largest inner IP packet that fits without fragmenting the outer packet? (b) A host behind the tunnel sends a TCP segment using 20-byte IP and 20-byte TCP headers. What MSS keeps its packets within that limit?
Show answer
(a) 1500 - 20 - 4 = 1476 bytes, so the tunnel interface MTU should be 1476. (b) The inner packet may be 1476 bytes, and 20 + 20 of that is headers, so the MSS is 1476 - 40 = 1436 bytes.
Exercise
Host A traceroutes to host B through an IP-in-IP tunnel that crosses six internet routers. Why does the traceroute show far fewer hops than the real path, and which TTL does each internet router decrement?
Show answer
Each internet router forwards by the outer header and decrements only the outer TTL. The inner TTL is decremented at the tunnel endpoints when they route the inner packet, not at the routers in between. The six routers inside the tunnel are therefore invisible to the inner traceroute, and the tunnel counts as one hop.