Sub topic 1
Refresher: how packets find their way
Prerequisites
This chapter assumes you already know the following:
- Basic IPv4 addressing and CIDR notation, such as
10.0.0.0/24. - The difference between TCP and UDP.
- How to run commands in a Linux shell.
It is a refresher, not an introduction. Later chapters build tunnels on the ideas here, so each section ends with the point that matters for tunnelling.
Addresses, subnets and the local decision
Every IP packet carries a source address and a destination address. When a host sends a packet, its first question is whether the destination is on the same subnet as one of its own interfaces.
A subnet is a block of addresses that share a prefix. The prefix length (the number after the slash) says how many leading bits are fixed. For 192.168.1.10/24, the first 24 bits (192.168.1) identify the network, and the last 8 bits identify the host.
The host compares the destination against each interface's subnet:
- If the destination is inside a directly connected subnet, the host sends the packet straight to that destination. It uses ARP (IPv4) or Neighbor Discovery (IPv6) to find the destination's link-layer (MAC) address.
- If it is not, the host sends the packet to a router, called the next hop or gateway. The packet keeps its original destination IP address, but the link-layer destination becomes the router's MAC address.
The IP addresses stay the same from end to end. The link-layer addresses change at every hop. This separation is what makes tunnelling possible, because a tunnel only has to deliver a packet to a different IP address than the one it ultimately targets.
The routing table
A routing table is a list of rules. Each rule maps a destination prefix to a way of reaching it: a next hop, an outgoing interface, or both. View the table on Linux with:
ip route showTypical output on a laptop:
default via 192.168.1.1 dev wlan0 proto dhcp metric 600
192.168.1.0/24 dev wlan0 proto kernel scope link src 192.168.1.10 metric 600The two lines mean:
192.168.1.0/24 dev wlan0 ... scope link: this subnet is directly attached towlan0. Send packets for it straight out of that interface.default via 192.168.1.1 dev wlan0: for anything else, send the packet to the router at192.168.1.1.
The default route is the rule for 0.0.0.0/0 (and ::/0 in IPv6). Its prefix length is zero, so it matches every destination. It is the rule of last resort.
Longest prefix match
Several rules can match the same destination. The kernel picks the rule with the longest prefix, meaning the most specific one. Take this table:
| Destination prefix | Next hop | Interface |
|---|---|---|
| `0.0.0.0/0` | `192.168.1.1` | `wlan0` |
| `10.0.0.0/8` | `10.8.0.1` | `wg0` |
| `10.20.0.0/16` | `10.8.0.5` | `wg0` |
The next table shows which rule wins for some destinations.
| Destination | Matching rules | Winner |
|---|---|---|
| `8.8.8.8` | `/0` only | default via `192.168.1.1` |
| `10.1.2.3` | `/0`, `/8` | `10.0.0.0/8` via `10.8.0.1` |
| `10.20.5.9` | `/0`, `/8`, `/16` | `10.20.0.0/16` via `10.8.0.5` |
You can ask the kernel which rule it would use for a given destination:
ip route get 10.20.5.9This command is the most useful single tool in this tutorial. When a tunnel does not carry traffic, the first question is nearly always "which route does this destination actually use?", and ip route get answers it.
The point for tunnelling: a VPN client works mostly by adding routes. A route that points at a tunnel interface sends matching packets into the tunnel. A route that is missing, or is less specific than another one, sends them somewhere else.
Routers forward, hosts usually do not
A router receives a packet that is not addressed to itself and forwards it. It does this by looking up the destination in its routing table, decrementing the TTL (time to live, called hop limit in IPv6) by one, and sending the packet out of the chosen interface. If the TTL reaches zero, the router drops the packet and sends back an ICMP "time exceeded" message. This is how traceroute works, and it also stops packets from circling forever in a routing loop.
A Linux machine acts as a router only if forwarding is enabled:
sysctl net.ipv4.ip_forward
sudo sysctl -w net.ipv4.ip_forward=1The default is 0 on most distributions, so the kernel silently drops transit packets. A VPN server that relays traffic between clients and a network must have forwarding enabled. Forgetting this is one of the most common reasons a freshly built VPN connects but passes no traffic.
Private addresses and NAT
Some IPv4 ranges are reserved for private use and are not routed on the public internet (RFC 1918):
| Range | Prefix |
|---|---|
| `10.0.0.0` to `10.255.255.255` | `10.0.0.0/8` |
| `172.16.0.0` to `172.31.255.255` | `172.16.0.0/12` |
| `192.168.0.0` to `192.168.255.255` | `192.168.0.0/16` |
Many networks reuse these ranges. Network Address Translation (NAT) lets hosts with private addresses reach the internet. The usual form is source NAT with port translation. The NAT device rewrites the source address and port of each outgoing packet to its own public address and a port it chooses. It records the mapping in a state table, and it uses the table to rewrite replies back.
Two consequences matter later:
- A NAT device only creates a mapping when a host inside sends something first. Unsolicited inbound packets have no mapping and are dropped.
- Mappings expire after a period of inactivity. For UDP the timeout is often short, from about 30 seconds to a few minutes.
Because private ranges are reused so widely, two networks you want to join may use the same subnet. If your home network is 192.168.1.0/24 and the office network is also 192.168.1.0/24, a route to "the office" is ambiguous. Choosing non-overlapping ranges is a planning step for every VPN design.
Worked example: following one packet
Host A (192.168.1.10) wants to reach a web server at 203.0.113.7. The home router at 192.168.1.1 has public address 198.51.100.20.
- Host A looks up
203.0.113.7. Only the default route matches, so it sends the packet to192.168.1.1. The packet has source192.168.1.10, destination203.0.113.7, and the router's MAC address as link-layer destination. - The router checks its table, finds its own default route toward the ISP, and decrements the TTL.
- Before sending, the router applies NAT. The source becomes
198.51.100.20with a new port, and the router stores the mapping. - Routers across the internet each pick a next hop by longest prefix match until the packet reaches
203.0.113.7. - The server replies to
198.51.100.20. The home router matches the reply to its stored mapping, rewrites the destination back to192.168.1.10, and forwards it.
The diagram shows the same path.
Why a private network cannot be reached directly
Now suppose a laptop at a cafe needs to reach 10.20.5.9 in an office network. Two things stop it:
- Internet routers do not carry private ranges. A packet to
10.20.5.9sent out the laptop's default route would be dropped or sent to an unrelated private network. - Even if the packet arrived at the office edge, the office's NAT or firewall would not accept unsolicited traffic.
The packet needs a way to travel across the internet to a public address that belongs to the office, and then be released inside the office network. The next chapter shows how: wrap the private packet inside another packet whose addresses the internet can route. That wrapping is encapsulation, and a route is what decides which packets get wrapped.
Checking your own system
Run these commands before the later chapters so you know your baseline:
ip addr show
ip route show
ip route get 1.1.1.1
sysctl net.ipv4.ip_forwardNote your default gateway, your interface names and your subnet. Later, after you bring up a tunnel, compare the output and see which routes appeared.
Common mistakes
- Assuming the packet's destination address changes at each router. Only the link-layer address changes. NAT is the exception, and only at the NAT device.
- Forgetting that the most specific prefix wins, not the first listed or the lowest metric. Metric only breaks ties between routes of the same prefix length.
- Choosing overlapping subnets at the two ends of a planned VPN.
- Forgetting that return traffic needs a route too. The remote side must know how to reach your source address, or replies go elsewhere.
Exercise
A host has these routes: default via 192.168.1.1 dev eth0, 10.0.0.0/8 dev tun0, and 10.5.0.0/16 via 192.168.1.1 dev eth0. Which route and interface does a packet to 10.5.3.4 use, and which does a packet to 10.9.3.4 use? Why?
Show answer
The packet to 10.5.3.4 matches all three routes, and the /16 is the longest prefix, so it goes via 192.168.1.1 on eth0. The packet to 10.9.3.4 matches the default and the /8, and the /8 is longer, so it goes out tun0. Longest prefix match decides, regardless of the order of the rules.
Exercise
Your home network is 192.168.1.0/24. You plan to connect it to an office network that is also 192.168.1.0/24. Why is this a problem, and what is the usual fix?
Show answer
A host in either network cannot tell whether 192.168.1.50 is local or remote, because the directly connected subnet route always wins and the packet is never sent into the tunnel. The usual fix is to renumber one network to a non-overlapping range, such as 10.20.0.0/16. NAT between the two sides is a workaround but adds complexity.
Exercise
A Linux VPN server accepts connections from clients, but clients cannot reach hosts behind it. Name one setting on the server you should check first.
Show answer
Check that IP forwarding is enabled with sysctl net.ipv4.ip_forward. If it is 0, the kernel drops packets that are not addressed to the server itself, so it cannot relay traffic between the tunnel and the internal network.