Sub topic 1
Refresher: how packets find their way
Prerequisites
This chapter assumes you know basic IP addressing and subnets, the difference between TCP and UDP, and how to use a Linux command line. It reviews the routing ideas that every later chapter on tunnels and VPNs builds on. If you can read a routing table and explain what a default route does, you can skim it.
The forwarding decision
Every host and router makes the same decision for each outgoing IP packet. It looks at the destination address and searches its routing table for the best matching entry. The packet then leaves through the interface named in that entry, addressed to a next hop if the destination is not directly attached.
The best match is the entry with the longest prefix. A route for 10.20.0.0/16 is more specific than one for 10.0.0.0/8, so it wins for addresses inside both. The default route, 0.0.0.0/0, matches everything and is used only when nothing more specific matches.
Routers do not know the full path. Each one only chooses the next hop, and the next router repeats the decision. Each router also decrements the packet's TTL (time to live) field, and drops the packet when it reaches zero. This stops packets from looping forever.
Reading a routing table
On Linux, ip route prints the table. Here is a typical host:
ip route show
# default via 192.168.1.1 dev eth0
# 192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.50The first line is the default route. Traffic with no better match goes to the gateway 192.168.1.1 through eth0. The second line is a connected route. Addresses in 192.168.1.0/24 are on the local link, so the host sends to them directly with no gateway.
To ask the kernel which route a specific destination would use, run ip route get:
ip route get 10.20.1.5The output names the chosen interface, next hop and source address. You will use this command often when debugging tunnels.
Source address and interface
The routing decision also chooses the source address of the packet, which is normally the address of the outgoing interface. This matters for tunnels. A tunnel interface has its own address, and a packet that enters it is wrapped in a new packet whose source and destination are the tunnel endpoints. That second routing decision, for the outer packet, is made separately from the first.
The next diagram shows the lookup order.
Why this matters for tunnels
A tunnel works because routing is only a table lookup. If you add a route that points at a tunnel interface, the kernel treats that interface like any other link. Packets that match the route go into the tunnel. Nothing else about the application changes.
This also explains common failures. If the route is missing, packets take the default route and bypass the tunnel. If a route is too broad, such as a default route pointing into the tunnel, it can capture the tunnel's own outer packets unless a more specific route protects them. Later chapters return to both problems.
Summary of the key ideas
- The longest matching prefix wins, and the default route is the last resort.
- A connected route means "deliver directly". A route with a gateway means "hand to the next router".
- Each router decides only the next hop.
ip route get <address>shows which route the kernel would pick.
Exercise
A host has routes default via 192.168.1.1 dev eth0 and 10.20.0.0/16 via 10.10.10.2 dev gre1. Which interface does a packet to 10.20.5.9 use, and which does a packet to 8.8.8.8 use? Why?
Show answer
The packet to 10.20.5.9 uses gre1, because 10.20.0.0/16 is a longer, more specific match than the default route. The packet to 8.8.8.8 matches only the default route 0.0.0.0/0, so it leaves through eth0 toward 192.168.1.1.
Exercise
Which command shows the route the kernel would choose for the destination 10.20.1.5?
Show answer
ip route get 10.20.1.5