A packet only knows the next step

A machine has no map of the internet. For every packet it asks who should get it next. The most specific route answers, and a wrong route changes only the addresses in its range.

Part 1 of 9 · Networking5 min read
10.15.5networkhostnetwork + hostthe /16 marks the split10.0.0.0/810.1.0.0/1610.1.2.3/32defaultnarrowest winsorder is ignoredgatewayno sign matches?hand it to the gatewayip route get 10.1.5.5ask the kernelthe real decision, not a guessone wrong linebreaks only its own range

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:

Shell
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. 1docker run --rm starts a small Linux container from the image alpine, and --rm removes it when the command ends.
  2. 2sh -c '...' runs the text in quotes as a shell command inside the container.
  3. 3apk add -q iproute2 installs the ip tool. The option -q hides the messages of the installer.
  4. 4ip -4 -br addr lists the addresses. -4 shows IPv4 only, and -br prints 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.

10155/16network: first 16 bitshost: last 16 bitsa longer prefix is a smaller group/8: 16 million/16: 65,536/24: 256/32: one
An address has two parts: the network it belongs to, and the host inside that network. The number after the slash says where the split is.

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:

Shell
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. 1--cap-add NET_ADMIN allows the container to change its own network settings.
  2. 2&& runs the next command only if the one before it worked.
  3. 3ip route prints the routing table: the list of routes that the machine knows.
  4. 4ip route get 1.1.1.1 asks which route the kernel would use for that address.
  5. 5| head -1 keeps 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?

Shell
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. 1ip route add 10.0.0.0/8 via 172.17.0.254 adds a route. For addresses in 10.0.0.0/8, the next hop is 172.17.0.254.
  2. 2for d in ...; do ...; done repeats the command for each address in the list.
  3. 3ip route del 10.1.0.0/16 removes 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.910.0.0.0/810.1.0.0/16/32via 172.17.0.25410.1.5.510.0.0.0/810.1.0.0/16/32via 172.17.0.110.1.2.310.0.0.0/810.1.0.0/16/32via 172.17.0.77

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.1 is 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.

New posts by email

One email when a post ships, about twice a month. Nothing else, and one click to leave.

Latest: Finding where a request breaks · RSS