Sub topic 8
CIDR: why classes were replaced
This chapter assumes you can convert between dotted decimal and binary, and that you know what a subnet mask and a prefix length are. It also assumes you know the default masks of classes A, B, and C.
The problem with classes
Classful addressing offered only three network sizes for ordinary hosts:
| Class | Default prefix | Addresses per network |
|---|---|---|
| A | /8 | 16,777,216 |
| B | /16 | 65,536 |
| C | /24 | 256 |
An organization with 300 hosts did not fit in a class C network, which holds 254 usable hosts. It received a class B network with about 65,000 usable addresses, and most of them went unused. By the early 1990s, class B space was running out while many class C networks sat unallocated.
Class C had a second problem. Every class C network needed its own entry in the routing tables of internet routers. Hundreds of thousands of small networks threatened to overwhelm those tables.
The CIDR idea
Classless Inter-Domain Routing (CIDR) removes the link between the first bits of an address and the prefix length. The prefix can be any length from /0 to /32. The prefix length is always written explicitly, as in 203.0.113.0/26. The address itself no longer tells you where the network part ends.
CIDR solves both problems:
- Right-sized allocation. An organization with 300 hosts can receive a /23, which holds 510 usable hosts.
- Route aggregation. A provider can advertise one short prefix that covers many customer networks.
Sizing a block
A /n prefix leaves 32 − n host bits, so the block holds 2^(32−n) addresses. Subtract 2 for the network and broadcast addresses to get the usable count.
import ipaddress
for text in ["203.0.113.0/26", "10.4.0.0/23", "172.16.0.0/20"]:
net = ipaddress.ip_network(text)
print(text, net.netmask, net.num_addresses - 2)This prints:
203.0.113.0/26 255.255.255.192 62
10.4.0.0/23 255.255.254.0 510
172.16.0.0/20 255.255.240.0 4094A block must start on a boundary that is a multiple of its size. 10.4.1.0/23 is invalid, because the third octet of a /23 must be even. The ipaddress module raises a ValueError for it unless you pass strict=False.
Aggregation
Suppose a provider owns 198.51.100.0/22 and gives four /24 customer networks out of it. Outside the provider, routers need only one route.
import ipaddress
customers = [
ipaddress.ip_network("198.51.100.0/24"),
ipaddress.ip_network("198.51.101.0/24"),
ipaddress.ip_network("198.51.102.0/24"),
ipaddress.ip_network("198.51.103.0/24"),
]
print(list(ipaddress.collapse_addresses(customers)))
# [IPv4Network('198.51.100.0/22')]Four contiguous, aligned /24 networks collapse into one /22. This is called supernetting. Aggregation only works when the blocks are contiguous and aligned. Non-contiguous blocks stay as separate routes.
Longest-prefix match
With CIDR, two routes can overlap. A router then picks the matching route with the longest prefix. If a table holds 10.0.0.0/8 and 10.1.0.0/16, a packet for 10.1.5.9 uses the /16 route. A packet for 10.9.9.9 uses the /8 route. Classful routing never needed this rule, because the class fixed the prefix length.
Common errors
- Assuming the class gives the mask.
192.168.1.0/22is valid under CIDR even though 192.x.x.x was class C space. - Misaligned blocks. A prefix whose host bits are not all zero is not a network address.
- Aggregating non-adjacent blocks.
198.51.101.0/24and198.51.102.0/24are adjacent but do not form a /23, because the /23 boundary falls between .100 and .101 or between .102 and .103. - Forgetting the two reserved addresses. A /31 and /32 are special cases. A /31 is used on point-to-point links, and a /32 names a single host.
Exercise
A company needs 900 usable host addresses. What is the smallest CIDR prefix length that fits, and how many usable hosts does that block hold?
Show answer
A /22 is the longest prefix that fits. A /23 holds only 510 usable hosts. A /22 has 10 host bits, so 2^10 = 1024 addresses, and 1024 − 2 = 1022 usable hosts.
Exercise
Can 198.51.101.0/24 and 198.51.102.0/24 be combined into a single /23 route? Explain.
Show answer
No. A /23 must start on an even third octet, so 198.51.100.0/23 covers .100 and .101, and 198.51.102.0/23 covers .102 and .103. The two blocks sit in different /23 boundaries and stay as two routes.