Your server sends a request to 1.1.1.1. Another request goes to a database at 10.0.0.5. The machine sees only the one network it is plugged into, and it has no map of the internet. Every packet still has to leave towards one place. How does the machine choose, and how do we find out which choice was wrong?
Think of a post office. A packet is a letter, and the machine is the post office with one courier, Bub. Bub does not know the way to every street. Bub reads the address and asks one question. Do I deliver this myself, or do I hand it to the next depot? The next depot is the next router.
What an IP address is
An IP address is the number of a machine on a network, like the number of a house on a street. A letter finds a house by its number, and a packet finds a machine by its IP address.
The address has four numbers from 0 to 255, separated by dots, for example 172.17.0.2. Inside the machine, the address is 32 bits, and each of the four numbers uses 8 of them, so the biggest number is 255. Two machines on the same network must not have the same address, or packets for one would reach the other.
A machine can have more than one address, one for each network card. Ask a small container for its addresses:
docker run --rm \
alpine sh -c \
'apk add -q iproute2 &&
ip -4 -br addr 'lo UNKNOWN 127.0.0.1/8 eth0@if553 UP 172.17.0.2/16
Here is what each marked part does:
- 1
docker run --rmstarts a small Linux container from the imagealpine, and--rmremoves it when the command ends. - 2
sh -c '...'runs the text in quotes as a shell command inside the container. - 3
apk add -q iproute2installs theiptool. The option-qhides the messages of the installer. - 4
ip -4 -br addrlists the addresses.-4shows IPv4 only, and-brprints one short line for each card.
There are two cards. lo is the loopback card, and its address 127.0.0.1 is how a machine talks to itself. It is the address behind the name localhost. eth0 is the card connected to a network, and its address is 172.17.0.2. The part after the slash, /16, is explained next. Your addresses will differ.
An address has two parts
An address is the network and the host written together. A CIDR prefix says where one ends and the other begins. In 10.1.5.5/16, the first 16 bits name the network and the rest name the host.
A longer prefix means a smaller group. The size is 2 to the power of (32 minus the prefix). A /24 leaves 8 bits for the host, and 2 to the power of 8 is 256. A /8 holds about 16 million addresses, a /16 holds 65,536, a /24 holds 256, and a /32 is exactly one address. So 10.0.0.0/8 and 10.0.0.0/24 look almost the same, but one holds 16 million addresses and the other 256.
The one question before every packet
Before it sends anything, the machine asks: is the destination on my own network? If yes, it delivers the packet directly. If not, it gives the packet to a router, which carries it towards the next network. The router for “everything else” is the default gateway. Start a small container and ask it. The option --cap-add NET_ADMIN lets the container change its own routes:
docker run --rm --cap-add NET_ADMIN \
alpine sh -c 'apk add -q iproute2 &&
ip route &&
ip route get 1.1.1.1 | head -1 &&
ip route get 172.17.0.9 | head -1'default via 172.17.0.1 dev eth0 172.17.0.0/16 dev eth0 proto kernel scope link src 172.17.0.2 1.1.1.1 via 172.17.0.1 dev eth0 src 172.17.0.2 uid 0 172.17.0.9 dev eth0 src 172.17.0.2 uid 0
Here is what each marked part does:
- 1
--cap-add NET_ADMINallows the container to change its own network settings. - 2
&&runs the next command only if the one before it worked. - 3
ip routeprints the routing table: the list of routes that the machine knows. - 4
ip route get 1.1.1.1asks which route the kernel would use for that address. - 5
| head -1keeps only the first line of the answer.
The machine does not plan the whole path: the next router makes its own choice from its own table. This routing table has two lines. The second says that everything in 172.17.0.0/16 is on the local network, so the machine delivers directly. The first is the default route: for any other address, hand the packet to 172.17.0.1.
ip route get shows what the kernel would really do. The line for 1.1.1.1 has a via, so the packet goes to the gateway. The line for 172.17.0.9 has none, so it goes straight to that machine. The src is the return address the kernel writes on the packet.
The most specific route wins
A real table has many lines, and several can match one address. The kernel picks the line with the longest matching prefix, the most specific claim. Add three routes with different prefixes, each pointing to a different next hop, and ask about three addresses:
Which next hop will 10.1.5.5 use?
docker run --rm --cap-add NET_ADMIN alpine sh -c 'apk add -q iproute2 &&
ip route add 10.0.0.0/8 via 172.17.0.254 &&
ip route add 10.1.0.0/16 via 172.17.0.1 &&
ip route add 10.1.2.3/32 via 172.17.0.77 &&
for d in 10.9.9.9 10.1.5.5 10.1.2.3; do ip route get $d | head -1; done &&
ip route del 10.1.0.0/16 &&
ip route get 10.1.5.5 | head -1'10.9.9.9 via 172.17.0.254 dev eth0 src 172.17.0.2 uid 0 10.1.5.5 via 172.17.0.1 dev eth0 src 172.17.0.2 uid 0 10.1.2.3 via 172.17.0.77 dev eth0 src 172.17.0.2 uid 0 10.1.5.5 via 172.17.0.254 dev eth0 src 172.17.0.2 uid 0
Here is what each marked part does:
- 1
ip route add 10.0.0.0/8 via 172.17.0.254adds a route. For addresses in10.0.0.0/8, the next hop is172.17.0.254. - 2
for d in ...; do ...; donerepeats the command for each address in the list. - 3
ip route del 10.1.0.0/16removes one route again.
All three addresses match the /8. Two of them also match the /16, and one matches the /32. Each address takes the narrowest line that matches. The order of the lines does not matter. Many firewalls work differently: the first rule that matches wins.
10.9.9.9 matches only the /8, so it goes to 172.17.0.254.
The last two commands remove the /16 line and ask again for 10.1.5.5. The answer changes to 172.17.0.254, because the /8 is now the narrowest claim left. A wrong route works the same way: it changes the path only for the addresses in its range. The uid 0 at the end of each line is the user that asked.
Key points
- An IP address is the number of a machine on a network, like a house number. A machine has one for each network card, and
127.0.0.1is its address for itself. - An address is a network part and a host part. The prefix, such as
/16, says where the split is. - Before every packet the machine asks: is the destination on my own network? If not, the packet goes to the default gateway.
- The kernel picks the route with the longest matching prefix. The order of the lines does not decide.
ip route get <address>shows the real decision: the next hop, the interface and the return address.- A wrong route changes the path only for the addresses in its range.
Next, in part 2: how a name such as example.com becomes an address.