Sub topic 2
Tunnelling: packets inside packets
This chapter assumes you know IP addressing and subnets, how a routing table and default route work, the difference between TCP and UDP, and basic Linux command-line use.
The idea
Encapsulation means putting one packet inside the payload of another. A tunnel uses this to carry packets across a network that would not route them on its own. The network in the middle only sees the outer packet. The inner packet is just data until it reaches the far end of the tunnel.
Two things make this useful:
- The inner packet can use addresses the middle network does not know, such as private 10.x.x.x ranges.
- The inner packet can be a different protocol from the outer one, for example IPv6 inside IPv4.
What the packet looks like
A packet sent through a tunnel has two sets of headers. The outer IP header uses the public addresses of the two tunnel endpoints. The inner IP header uses the addresses of the real source and destination.
| Layer | Source | Destination |
|---|---|---|
| Outer IP header | 192.0.2.1 (host A) | 198.51.100.1 (host B) |
| Tunnel header | protocol-specific | protocol-specific |
| Inner IP header | 10.10.10.1 | 10.20.0.5 |
| Inner payload | TCP, UDP or ICMP data |
Routers on the internet look only at the outer header. Host B removes the outer header and tunnel header (decapsulation), then routes the inner packet as normal.
A tunnel is an interface
On Linux, a tunnel appears as an ordinary network interface. You give it an address and add routes that point at it. Any packet the routing table sends to that interface gets wrapped and sent to the other endpoint.
This example creates a GRE tunnel. GRE is a simple tunnelling protocol, covered in a later chapter.
# On host A (public address 192.0.2.1); host B mirrors this with local/remote swapped
sudo ip tunnel add gre1 mode gre local 192.0.2.1 remote 198.51.100.1 ttl 255
sudo ip addr add 10.10.10.1/30 dev gre1
sudo ip link set gre1 up
sudo ip route add 10.20.0.0/16 via 10.10.10.2 dev gre1Each line does one job:
ip tunnel addcreates the interface and sets the outer addresses,localandremote.ip addr addgives the tunnel its inner address. The /30 gives two usable addresses, one per end.ip link set gre1 upenables the interface.ip route addsends traffic for 10.20.0.0/16 into the tunnel, with 10.10.10.2 (host B's inner address) as the next hop.
Host B needs the mirrored configuration. It also needs a route back to host A's side, or replies will never return.
Following one packet
Suppose a process on host A sends a packet to 10.20.0.5.
- The routing table matches 10.20.0.0/16 and selects
gre1. - The GRE driver adds a GRE header and a new outer IP header. The outer source is 192.0.2.1 and the outer destination is 198.51.100.1.
- The kernel routes the outer packet using the normal routing table, probably through the default route.
- Host B receives the outer packet, sees GRE as the protocol and passes it to its
gre1interface. - The inner packet comes out and is routed to 10.20.0.5.
Two routing decisions happen: one for the inner packet, which picks the tunnel, and one for the outer packet, which picks the physical path.
Common errors
- Routing loop. If a route for the remote endpoint's public address points into the tunnel, the outer packets try to enter the tunnel themselves. The tunnel never comes up. Keep the route to the far endpoint on the physical interface.
- Missing return route. The tunnel works one way only. Check routes on both hosts.
- Overhead. Every outer header makes the packet bigger. Large packets may then fail to cross the path. The next chapters cover this.
Exercise
Host A sends a packet to 10.20.0.5 through a GRE tunnel. Which addresses appear in the outer IP header, and which in the inner IP header? Using the example above, who routes the outer packet?
Show answer
The outer header carries the tunnel endpoints' public addresses: source 192.0.2.1 and destination 198.51.100.1. The inner header carries the original addresses: the sending host's inner address (for example 10.10.10.1) and destination 10.20.0.5. Routers between A and B route the outer packet using only the outer destination. They never use the inner addresses.
Exercise
You add a route for 198.51.100.0/24 via gre1 on host A, and the tunnel to 198.51.100.1 stops working. Why?
Show answer
The outer packets are addressed to 198.51.100.1, which now matches a route pointing into the tunnel. The kernel tries to encapsulate the outer packets in the tunnel itself, which creates a loop. The route to the remote endpoint must use the physical interface.