Chapters

Subnet masks

CIDR says where the network part ends. The subnet mask is how a machine actually uses that split, many times a second, to decide where each packet goes.

Addressing and local delivery · chapter 4 of 34 · 5 min read

The question before every send

Before sending anything, a device must decide how to send it. If the target is on the same network, it sends directly: no router, no internet. If not, it hands the packet to the router (the default gateway). The subnet mask is the tool that makes this decision.

Packet to sendto 34.87.120.15Apply the maskmy IP AND masktarget AND masksamenetwork?yesSame networksend directlyARP for the targetnoDifferentsend to the gatewayARP for the router

Wide by design - scroll it sideways.

What a mask looks like

A mask is written like an IP address. For a /24 network it is 255.255.255.0. The laptop got this value from DHCP (chapter 3). But a mask is not an address: you cannot give it to a device. It exists only to be applied to addresses.

Its purpose is clear in binary. 255 is 11111111 (eight 1s) and 0 is 00000000 (eight 0s). So 255.255.255.0 is twenty-four 1s followed by eight 0s, the exact /24 split from chapter 3:

  • 1s = network bits (24)
  • 0s = host bits (8)

255.255.255.0 is 11111111 11111111 11111111 00000000 in binary. The first 24 bits are the network part and the last 8 the host part.

Bitwise AND

To use the mask, the device runs a bitwise AND between an IP and the mask: bit 1 of the IP with bit 1 of the mask, bit 2 with bit 2, and so on for all 32 bits. The rule for each pair:

IP bit AND mask bitResultIn words
1 AND 11AND with 1 keeps the original bit
0 AND 10AND with 1 keeps the original bit
1 AND 00AND with 0 always gives 0
0 AND 00AND with 0 always gives 0
So the mask keeps the network part of an IP and wipes the host part to zeros. What is left is the network address.

Worked example: the laptop

laptop: 192.168.1.23 AND 255.255.255.0

IP
192.168.1.23
192.168.1.23
mask /24
255.255.255.0
255.255.255.0
AND result
network address
192.168.1.0

192.168.1.23 AND 255.255.255.0 gives the network address 192.168.1.0.

Octet by octet: 192 AND 255 = 192, 168 AND 255 = 168, 1 AND 255 = 1, and 23 AND 0 = 0. The result, 192.168.1.0, is the network the laptop is on. This is the same address CIDR wrote as 192.168.1.0/24.

Same network or not?

Now the laptop wants to send to two places: the phone (192.168.1.40) and Kadé's API (34.87.120.15). It uses its own mask for both, because its own mask is the only one it knows.

phone: 192.168.1.40 AND 255.255.255.0

IP
192.168.1.40
192.168.1.40
mask /24
255.255.255.0
255.255.255.0
AND result
network address
192.168.1.0

192.168.1.40 AND 255.255.255.0 gives the network address 192.168.1.0.

kade-api: 34.87.120.15 AND 255.255.255.0

IP
34.87.120.15
34.87.120.15
mask /24
255.255.255.0
255.255.255.0
AND result
network address
34.87.120.0

34.87.120.15 AND 255.255.255.0 gives the network address 34.87.120.0.

TargetTarget AND maskLaptop's networkResult
Phone192.168.1.0192.168.1.0equal → send directly
kade-api34.87.120.0192.168.1.0different → gateway

What happens next (back to chapter 1)

The answer decides which MAC address goes into the frame, and so what the laptop asks ARP for:

TargetSame?Laptop ARPs forFrame's destination MAC
Printer192.168.1.60yes192.168.1.6000:1b:a9:44:7c:21the printer itself
kade-api34.87.120.15no192.168.1.13c:84:6a:10:ee:01the router, which passes it on

This is why the frame to Kadé's API in chapter 1 carried the router's MAC: the mask check had already said "different network".

Masks of other sizes

A /16 mask is 255.255.0.0: two octets of 1s, two of 0s. Masks do not have to end on an octet boundary. The 1s always fill from the left with no gaps, so a partly-filled octet can only be one of a few values: 128, 192, 224, 240, 248, 252 or 254.

CIDRMaskLast non-255 octet in binaryAddresses
/8255.0.0.00000000016,777,216
/16255.255.0.00000000065,536
/20255.255.240.0111100004,096
/24255.255.255.000000000256
/25255.255.255.12810000000128
/26255.255.255.1921100000064
/27255.255.255.2241110000032
/28255.255.255.2401111000016
/30255.255.255.252111111004
/32255.255.255.255111111111

The mask also gives the last address of a range (the broadcast). Flip the mask so 1s become 0s and 0s become 1s (0.0.0.255 for a /24; this flipped form is called a wildcard mask). Then OR it with the network address (OR gives 1 if either bit is 1): 192.168.1.0 OR 0.0.0.255 = 192.168.1.255.

In the cloud: kade-api and kade-db

The same check runs inside GCP. kade-api (10.10.1.10/24, in sn-app) wants to query kade-db (10.10.2.5, in sn-data). kade-api's network is 10.10.1.0. kade-db AND the mask is 10.10.2.0. Different, so kade-api sends to its gateway, 10.10.1.1. Change the values below to test other pairs:

Different networks (10.10.1.0 vs 10.10.2.0). Send to the default gateway (in GCP this is 10.10.1.1).

Summary

#IdeaIn one lineNew terms
1The decisionSame network → direct. Different → default gateway.subnet mask
2Mask formatLooks like an IP; 1s = network bits, 0s = host bits; /24 = 255.255.255.0
3ANDAND with 1 keeps the bit, AND with 0 clears itbitwise AND
4Network addressIP AND mask = the network the IP is on
5ComparingEqual network addresses = same network
6ResultSame → ARP for the target. Different → ARP for the router.
7Other sizes1s fill from the left; broadcast = network OR wildcardwildcard mask, OR
8CloudSame logic in a VPC; latency depends on zone and region, not subnet

Try it yourself

Your mask

  • What to look for: 255.255.255.0, or on Linux the /24 after your IP.
  • Runs on: macOS · Windows: ipconfig · Linux: ip -4 addr
  • From section: 2. What a mask looks like
ipconfig getoption en0 subnet_mask

How a LAN target is reached

ip route get 192.168.1.60

How an outside target is reached

ip route get 34.87.120.15

Same check on macOS

route -n get 34.87.120.15

Same check on Windows

Find-NetRoute -RemoteIPAddress 34.87.120.15

Is an IP inside a range?

python3 -c "import ipaddress as i; print(i.ip_address('10.10.2.5') in i.ip_network('10.10.1.0/24'))"

Spotted a mistake, or something unclear? Suggest an edit on GitHub