Sub topic 5
CIDR notation and prefix length
This chapter assumes you can read an IPv4 address as four octets, convert values from 0 to 255 between binary and decimal, and understand that an address has a network part and a host part. Here you will learn how CIDR writes down where that split falls.
From classes to prefixes
Classful addressing fixed the split at /8, /16, or /24, based on the first bits of the address. Those three sizes were too coarse. An organization that needed 300 addresses had to take a /16 with 65,534 hosts, and most of those addresses went unused.
Classless Inter-Domain Routing (CIDR) removes the fixed sizes. The split can fall after any bit from 0 to 32. CIDR also lets routers advertise a group of networks as one route, which a later chapter covers.
Reading the notation
A CIDR address has the form address/prefix-length. The prefix length is the number of leading bits that identify the network. The remaining bits identify hosts.
For 192.168.10.37/26:
- The first 26 bits are the network part.
- The last 32 - 26 = 6 bits are the host part.
- 2^6 = 64 addresses fit in this block.
Prefix length and subnet mask
The prefix length and the subnet mask carry the same information. The mask has a 1 for every network bit and a 0 for every host bit. To convert, write that many 1s, then pad with 0s to 32 bits, and read the result as four octets.
For /26, the mask is 11111111.11111111.11111111.11000000, which is 255.255.255.192. The last octet is 128 + 64 = 192.
A prefix that is not a multiple of 8 puts a partial octet in the mask. The partial octet can only take these values:
| Bits set in octet | Octet value |
|---|---|
| 1 | 128 |
| 2 | 192 |
| 3 | 224 |
| 4 | 240 |
| 5 | 248 |
| 6 | 252 |
| 7 | 254 |
| 8 | 255 |
To convert a prefix to a mask, find how many full octets of 255 it contains. Then look up the leftover bits in the table. For /20, there are two full octets (16 bits) and 4 leftover bits, so the mask is 255.255.240.0.
Converting in code
The ipaddress module reports the mask for any prefix. This check uses only the standard library.
import ipaddress
for prefix in (8, 20, 26, 30):
net = ipaddress.ip_network(f"10.0.0.0/{prefix}")
print(f"/{prefix}", net.netmask, net.num_addresses)
# /8 255.0.0.0 16777216
# /20 255.255.240.0 4096
# /26 255.255.255.192 64
# /30 255.255.255.252 4Common errors
- Mask bits must be contiguous.
255.255.0.255is not a valid mask, because the 1s must all come before the 0s. - Each extra prefix bit halves the block. A /25 holds half as many addresses as a /24. People often assume a longer prefix means a bigger network, but the opposite is true.
- An address with a prefix does not have to be the network address.
192.168.10.37/26is a host address inside the192.168.10.0/26network. Python'sip_networkraisesValueErrorfor this input unless you passstrict=False.
Exercise
What is the subnet mask for 172.16.5.9/21, and how many host bits does the address have?
Show answer
21 bits is two full octets (16) plus 5 leftover bits. Five bits give 248 in the third octet, so the mask is 255.255.248.0. The host part is 32 - 21 = 11 bits.
Exercise
Is 255.255.253.0 a valid subnet mask? Explain.
Show answer
No. 253 is 11111101 in binary, which has a 0 before a 1. Mask bits must be contiguous 1s followed by 0s, so 253 cannot appear in a mask.