Sub topic 4
CIDR and prefix notation
This chapter assumes you can convert between binary and decimal, and that you know the classful address classes and fixed subnet masks from the earlier chapters.
Why CIDR replaced classes
Classful addressing only offers three block sizes: 256 (class C), 65,536 (class B) and about 16.7 million (class A). A site that needed 2,000 addresses had to take a class B and waste about 63,000 of them. Class C blocks were too small, so such sites took many of them, and each one added a separate entry to the global routing tables.
Classless Inter-Domain Routing (CIDR) removes the fixed boundaries. The split between the network part and the host part of an address can fall on any bit. A block is written as address/prefix-length, where the prefix length is the number of leading bits that identify the network.
Reading prefix notation
In 200.17.21.77/20, the first 20 bits are the network part and the remaining 12 bits are the host part. The prefix length gives the mask directly: 20 ones followed by 12 zeros, which is 255.255.240.0.
| Prefix | Mask | Total addresses | Usable hosts |
|---|---|---|---|
| /8 | 255.0.0.0 | 16,777,216 | 16,777,214 |
| /16 | 255.255.0.0 | 65,536 | 65,534 |
| /24 | 255.255.255.0 | 256 | 254 |
| /26 | 255.255.255.192 | 64 | 62 |
| /30 | 255.255.255.252 | 4 | 2 |
| /31 | 255.255.255.254 | 2 | 2 (point-to-point links, RFC 3021) |
| /32 | 255.255.255.255 | 1 | 1 (a single host route) |
For a prefix of length p, a block has 2^(32 − p) addresses. The usual formula for usable hosts is 2^(32 − p) − 2, because the all-zeros host part is the network address and the all-ones host part is the broadcast address. GATE questions almost always use this formula. /31 and /32 are the exceptions.
Computing the block
The network address is the address AND the mask. The broadcast address is the network address with every host bit set to 1.
For 200.17.21.77/20, the prefix ends in the third octet. That octet has 4 network bits, so the block size is 2^4 = 16. Rounding 21 down to a multiple of 16 gives 16. The network is 200.17.16.0, the broadcast address is 200.17.31.255, and the usable range is 200.17.16.1 to 200.17.31.254. There are 2^12 − 2 = 4094 usable hosts.
You can check this with Python:
import ipaddress
iface = ipaddress.ip_interface("200.17.21.77/20")
net = iface.network
print(net.network_address)
print(net.broadcast_address)
print(net.netmask)
print(net.num_addresses - 2)The output is 200.17.16.0, 200.17.31.255, 255.255.240.0 and 4094.
Sizing a block for a host requirement
To fit n hosts, find the smallest h such that 2^h − 2 ≥ n. The prefix length is then 32 − h. For 2,000 hosts, 2^11 − 2 = 2046 is enough and 2^10 − 2 = 1022 is not. So h = 11 and the prefix is /21, which wastes far fewer addresses than a class B.
Alignment and invalid masks
Two rules catch many GATE candidates.
- Contiguous mask. A mask must be ones followed by zeros. 255.0.255.0 is not a valid CIDR mask.
- Aligned block. A block of 2^h addresses must start at a multiple of 2^h.
192.168.1.0/23is not a valid block, because a /23 spans two consecutive /24s and must start at an even third octet.
import ipaddress
try:
ipaddress.ip_network("192.168.1.0/23")
except ValueError as err:
print("rejected:", err)
print(ipaddress.ip_network("192.168.1.0/23", strict=False))The first call is rejected because host bits are set. With strict=False, Python rounds down to the real block, 192.168.0.0/23. A router does the same thing when it ANDs an address with a mask.
Membership tests
An address belongs to a prefix if the address AND the mask equals the network address. Equivalently, the first p bits of the address match the first p bits of the prefix. Longest-prefix match and route aggregation, covered in the next chapters, both rely on this test. When two prefixes overlap, the one with the larger p is the more specific.
Common mistakes
- Rounding the interesting octet up instead of down.
- Using the wrong octet when the prefix is a multiple of 8, for example /16, where the whole third octet is host bits.
- Writing the block size as 256 − mask octet but applying it to the wrong octet.
- Forgetting that a /24 has 254 usable hosts, not 256.
Exercise
A host has the address 172.16.45.200/21. Give the network address, the broadcast address and the number of usable hosts.
Show answer
The prefix ends in the third octet with 5 network bits, so the mask is 255.255.248.0 and the block size is 2^3 = 8. Rounding 45 down to a multiple of 8 gives 40. Network: 172.16.40.0. Broadcast: 172.16.47.255. There are 11 host bits, so 2^11 − 2 = 2046 usable hosts.
Exercise
What is the smallest prefix length that gives at least 500 usable host addresses, and what is its mask?
Show answer
2^9 − 2 = 510 is enough and 2^8 − 2 = 254 is not, so h = 9 and the prefix is /23. The mask is 255.255.254.0.