Sub topic 14
Tunneling: a packet inside a packet
This chapter assumes you know encapsulation, the IPv4 header, and MTU from the earlier chapters. You should also be able to read a short Python script.
What a tunnel is
Normal encapsulation puts a transport segment inside an IP packet. Tunneling applies the same idea again: a complete IP packet, header included, becomes the payload of a new outer packet. The outer header gets the packet across a stretch of network. At the far end of the tunnel, the outer header is removed and the inner packet continues on its way.
Routers along the tunnel read only the outer header. They never see the inner addresses, so the inner packet can use addresses that the carrying network does not route.
Why use tunnels
- Private addressing across the internet. A VPN carries private addresses such as 10.0.0.0/8 over public routes.
- Protocol bridging. IPv6 packets can cross an IPv4-only network inside IPv4 packets.
- Confidentiality. IPsec and WireGuard encrypt the inner packet before wrapping it.
- Overlay networks. Data centers and cloud platforms use VXLAN and GRE to build virtual networks on top of physical ones.
Building a tunneled packet
IP-in-IP is the simplest tunnel. The outer header uses protocol number 4, which tells the receiver that the payload is another IPv4 packet. This script builds one and then decapsulates it. As in the earlier examples, the checksums are left as 0.
import struct
def ipv4(src, dst, proto, payload):
header = struct.pack(
"!BBHHHBBH4s4s",
0x45, 0, 20 + len(payload), 0, 0, 64, proto, 0,
bytes(src), bytes(dst),
)
return header + payload
def addr(raw):
return ".".join(str(b) for b in raw)
# Inner packet: private addresses, protocol 253 (reserved for experiments)
inner = ipv4([10, 0, 0, 1], [10, 0, 1, 1], 253, b"hello")
# Outer packet: public tunnel endpoints, protocol 4 means IPv4 inside IPv4
outer = ipv4([198, 51, 100, 1], [203, 0, 113, 9], 4, inner)
# Decapsulate at the far endpoint
ihl = (outer[0] & 0x0F) * 4
if outer[9] == 4:
pkt = outer[ihl:]
print("outer:", addr(outer[12:16]), "->", addr(outer[16:20]))
print("inner:", addr(pkt[12:16]), "->", addr(pkt[16:20]))
print("payload:", pkt[20:].decode())The output shows two address pairs, one for each header. Both are present in the same byte string.
The cost: smaller usable MTU
The outer header takes space from every packet. IP-in-IP adds 20 bytes. GRE adds about 24, and WireGuard and IPsec add more. On a link with a 1500-byte MTU, the inner packet can be at most 1480 bytes for IP-in-IP.
If the inner packet is larger, one of two things happens:
- The tunnel endpoint fragments the outer packet, which costs extra work and extra packets.
- If the inner packet has the Don't Fragment bit set, the endpoint drops it and should send back an ICMP "fragmentation needed" message. When firewalls block that message, connections stall on large transfers while small requests still work.
Common fixes are to lower the tunnel interface's MTU, for example to 1400, or to clamp the TCP MSS so that hosts send smaller segments in the first place.
Capturing a tunnel
In a capture, a tunneled packet shows two IP headers in a row. With tcpdump -nn -v, the output often prints the outer addresses first and the inner ones after, for example IP 198.51.100.1 > 203.0.113.9: IP 10.0.0.1 > 10.0.1.1. Encrypted tunnels such as WireGuard show only the outer UDP packet, because the inner packet is unreadable ciphertext.
Exercise
A tunnel uses GRE, which adds about 24 bytes, over a path with a 1500-byte MTU. What is the largest inner packet that fits without fragmentation, and which header do the routers on the path read?
Show answer
The largest inner packet is 1500 - 24 = 1476 bytes. The routers on the path read only the outer IP header, so they route by the tunnel endpoints' addresses and never look at the inner addresses.