Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

3 · IP addresses and subnets

In one sentence: the /24 after an address says how many of its first bits name the network; every address in that network is on the link and reached directly, and every other address needs a router.

The problem it solves

In Practical 0 you gave red 10.0.0.1/24 and blue 10.0.0.2/24, and they could talk. Now keep the same cable and give blue 10.0.1.2/24 instead. Red’s ping fails before a single packet leaves:

ping: connect: Network is unreachable

The cable is fine, both ends are up. What changed is one number in the address, and the /24 decides whether that number matters. Misread prefixes cause some of the most confusing failures there are: “it’s plugged in, it pings one way, but not the other”.

The idea: a street and a house number

An IPv4 address is 32 bits, written as four numbers from 0 to 255 (10.0.0.1). The prefix length, the number after the /, splits it in two:

  • the first part, /24 bits long, is the network: the street;
  • the rest names one machine on it: the house number.
 10   .  0   .  0   .  1      /24
 [------ network ------][host]
 10.0.0.x : 256 addresses on this network

 10   .  0   .  0   .  1      /16
 [-- network --][-- host ---]
 10.0.x.x : 65,536 addresses on this network

Two addresses are on the same network when their network parts are equal. 10.0.1.2 is in 10.0.0.0/16 (10.0 matches) but not in 10.0.0.0/24 (10.0.0 doesn’t).

PrefixNetwork partAddressesTypical use
/810.x.x.x16,777,216a whole private range
/16172.17.x.x65,536Docker’s default network
/2410.0.0.x256a home or office network
/304 addresses4a link between two routers
/32the whole address1one address only

When you add 10.0.0.1/24 to an interface, the kernel adds a route for the whole network, with the host part set to zero:

10.0.0.0/24 dev veth-red proto kernel scope link src 10.0.0.1 

scope link is the point: every address in 10.0.0.0/24 is on this link, so the kernel sends to it directly. It doesn’t check that anyone is there. Ask about an address nobody has:

ip -n red route get 10.0.0.77
10.0.0.77 dev veth-red src 10.0.0.1 uid 0 

An address outside every network you’re on needs a router, a machine that passes packets between networks (lesson 4). Red has no route at all for 10.0.1.2, which is what Network is unreachable means: no packet is sent, the error comes from red’s own routing table.

Change both prefixes to /16 on the same cable, and red’s route becomes 10.0.0.0/16. Now 10.0.1.2 is on the link and the ping works:

3 packets transmitted, 3 received, 0% packet loss, time 2078ms

Both sides have to agree

Now give red /16 and leave blue at /24. Red thinks blue is on its link; blue thinks red isn’t on its own. Red pings: 100% packet loss. tcpdump in blue shows red asking for blue’s hardware address (an ARP request, lesson 5), three times, a second apart, and never getting an answer:

15:42:49.085989 ARP, Request who-has 10.0.1.2 tell 10.0.0.1, length 28
15:42:50.107065 ARP, Request who-has 10.0.1.2 tell 10.0.0.1, length 28
15:42:51.131062 ARP, Request who-has 10.0.1.2 tell 10.0.0.1, length 28

Blue doesn’t answer because it has no route to 10.0.0.1, and its kernel checks that before it replies: reverse-path filtering, rp_filter, here 2 (“drop what I couldn’t answer by any route”). A new namespace copies that setting from your computer’s own network settings. With rp_filter at 0, blue answers the ARP and only the ping replies are lost: blue has no way to send them.

Give blue a route (ip -n blue route add 10.0.0.0/16 dev veth-blue) and it answers the ARP at once and all three pings come back. A conversation needs a route in both directions, and with wrong prefixes one side may never even reply to “who are you?”.

Try it on your computer

Lesson 4 adds the router, and Practical 1 builds it. In your Practical 0 lab you can already ask the kernel about any address without changing anything:

ip -n red route get 10.0.0.200
ip -n red route get 10.0.1.2

The first is on red’s link; the second is Network is unreachable.

Go deeper: the bits, the mask and the two reserved addresses

10.0.0.1 is 00001010.00000000.00000000.00000001 in binary. A /24 means the first 24 bits are the network. Older tools write the same thing as a netmask, 24 ones then zeros: 255.255.255.0 (/16 is 255.255.0.0). An address is in the network when address AND mask equals the network address (RFC 950).

The kernel builds the route from the address with the host bits cleared: add 10.0.7.77/24 and the route is 10.0.7.0/24. In each network the first address (host bits all 0) names the network and the last (all 1) is the broadcast address, so a /24 has 256 addresses and 254 for machines (RFC 919).

A /32 is a network of one: add 10.0.5.1/32 to red and the address appears in ip -br addr, but no new route does. Nothing else is on that “link”.

Go deeper: private ranges, and Docker's /16

Three ranges are set aside for private networks and never routed on the internet: 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 (RFC 1918). Every address in this course is in one of them. Docker’s default network is one too; read it on your server (read-only):

docker network inspect bridge --format '{{json .IPAM.Config}}'
[{"Subnet":"172.17.0.0/16","IPRange":"","Gateway":"172.17.0.1"}]

That’s why your lab container’s eth0 has a 172.17.x.x/16 address and a route 172.17.0.0/16 dev eth0: every container on that network is on its link, and anything else goes to the gateway 172.17.0.1.

Common misunderstandings

  • “Same cable, so they can talk.” The cable only carries frames. Each side decides from its own routes whether the other is on its link; with 10.0.0.1/24 and 10.0.1.2/24, red won’t even try.
  • “The prefix is just part of the address.” It is a claim about the network. Both ends must make the same claim, or one of them won’t answer.
  • “10.0.0.0/24 in the route table is a machine.” It’s the network: the host part is zero. The route covers all 256 addresses.

Check yourself

1. A machine has 192.168.1.10/24. Which of 192.168.1.200, 192.168.2.10 and 192.168.1.255 does it send to directly? 192.168.1.200 (same first three numbers). 192.168.1.255 is on the link too, but it's the network's broadcast address. 192.168.2.10 is another network: it needs a route through a router.
2. Red pings an address and gets Network is unreachable instantly. Did a packet leave red? No. The error comes from red's own routing table: no route matches. ip route get <address> says the same thing without pinging.
3. Red (/16) pings blue (/24) on the same cable and gets no reply. tcpdump in blue shows ARP requests from red. What's missing? Blue's way back: blue has no route to red's address, so it doesn't answer. Make the prefixes match (or give blue a route).

Next: lesson 4 · Routing, then Practical 1 · A router between two networks.