Sub topic 8
CIDR: Why Classes Were Replaced
This chapter assumes you can convert between dotted decimal and binary, and that you know a subnet mask marks which bits of an address are the network part. It also assumes you know the class A, B, and C default masks (/8, /16, /24). The chapter explains the problems that made classful addressing unworkable. It then shows how Classless Inter-Domain Routing (CIDR) fixed them, and finishes with the errors people commonly make.
The problem with fixed class boundaries
In the classful scheme, the first octet fixed the size of a network. A network could hold roughly 16.7 million hosts (class A), 65,534 hosts (class B), or 254 hosts (class C). There was nothing in between.
An organization that needed 2,000 addresses had two bad options:
- A class C gave it only 254 addresses, which was too few.
- A class B gave it 65,534 addresses, so about 63,500 were wasted.
Most organizations chose the class B, because a class C was too small. Class B space ran out quickly, even though most of it sat unused. This was address exhaustion, and it was the first problem.
The second problem was routing table growth. If an organization held eight class C networks instead of one class B, every router on the internet needed eight separate entries to reach it. A routing table is the list of destination networks a router consults to decide where to send each packet. By the early 1990s, the tables in the core routers were growing faster than the hardware could keep up.
| Problem | Cause | Effect |
|---|---|---|
| Wasted addresses | Only three network sizes (/8, /16, /24) | Class B space was nearly used up |
| Large routing tables | Many small class C blocks per organization | Core routers ran out of memory and CPU |
What CIDR changes
CIDR was introduced in 1993 (RFC 1518 and RFC 1519, later updated by RFC 4632). It makes two changes.
- The class is ignored. The network part can be any length from 0 to 32 bits. The first octet no longer determines it.
- The prefix length travels with the address. Every network is written as an address, a slash, and the number of network bits. This is prefix notation. For example,
192.168.1.0/24has 24 network bits and 8 host bits.
Because the prefix length can be any value, block sizes are powers of two, and an organization can receive a block close to what it needs. The 2,000-address organization now gets a /21, which is 2,048 addresses, instead of a class B.
The table shows how block size depends on prefix length.
| Prefix | Mask | Host bits | Total addresses | Usable hosts |
|---|---|---|---|---|
| /20 | 255.255.240.0 | 12 | 4,096 | 4,094 |
| /21 | 255.255.248.0 | 11 | 2,048 | 2,046 |
| /22 | 255.255.252.0 | 10 | 1,024 | 1,022 |
| /23 | 255.255.254.0 | 9 | 512 | 510 |
| /24 | 255.255.255.0 | 8 | 256 | 254 |
The "usable hosts" column subtracts the network address and the broadcast address, as covered in the previous chapter.
You can check the /21 case with the standard library.
import ipaddress
net = ipaddress.ip_network("10.4.8.0/21")
print(net.netmask) # 255.255.248.0
print(net.network_address) # 10.4.8.0
print(net.broadcast_address) # 10.4.15.255
print(net.num_addresses - 2) # 2046Notice that the network spans 10.4.8.0 to 10.4.15.255. The third octet covers eight values (8 through 15), because 3 of its 8 bits are host bits. A /21 does not line up with an octet boundary, which is the main skill CIDR asks of you.
Finding the block boundary
A valid CIDR block must start at an address that is a multiple of its size. A /21 block holds 2,048 addresses, so in the third octet it must start at a multiple of 8 (0, 8, 16, 24, and so on).
To find which block an address belongs to, take these steps:
- Find the "interesting" octet, the one where the prefix ends partway through.
- Compute the block size in that octet: 256 minus the mask value in that octet.
- Round the octet down to the nearest multiple of the block size.
Take 10.4.13.77/21. The mask is 255.255.248.0, so the interesting octet is the third. The block size is 256 − 248 = 8. Rounding 13 down to a multiple of 8 gives 8. The network is 10.4.8.0/21, and the broadcast address is 10.4.15.255.
import ipaddress
iface = ipaddress.ip_interface("10.4.13.77/21")
print(iface.network) # 10.4.8.0/21Route aggregation
The second benefit of CIDR is route aggregation, also called supernetting. Several contiguous blocks can be advertised as one shorter prefix. A supernet is a block made by merging adjacent networks.
Suppose an ISP holds four consecutive /24 networks:
- 203.0.112.0/24
- 203.0.113.0/24
- 203.0.114.0/24
- 203.0.115.0/24
In binary, the first 22 bits are identical across all four. The ISP can advertise one route, 203.0.112.0/22, to the rest of the internet. Four table entries become one.
import ipaddress
blocks = [
ipaddress.ip_network("203.0.112.0/24"),
ipaddress.ip_network("203.0.113.0/24"),
ipaddress.ip_network("203.0.114.0/24"),
ipaddress.ip_network("203.0.115.0/24"),
]
print(list(ipaddress.collapse_addresses(blocks)))
# [IPv4Network('203.0.112.0/22')]Aggregation works only when the blocks are contiguous and the combined block is correctly aligned. Three blocks, or four blocks that do not start on a multiple of 4, cannot collapse into one prefix without including addresses that belong to someone else.
Longest-prefix match
With classless routes, one destination can match several table entries. A router resolves this with the longest-prefix match rule: it uses the matching route with the most network bits.
Consider this table:
| Route | Next hop |
|---|---|
| 0.0.0.0/0 | ISP A |
| 203.0.112.0/22 | ISP B |
| 203.0.113.0/24 | ISP C |
A packet for 203.0.113.9 matches all three routes. The /24 is the longest, so the packet goes to ISP C. A packet for 203.0.114.9 matches the /0 and the /22, so it goes to ISP B. The default route 0.0.0.0/0 matches everything and is used only when nothing more specific exists.
Longest-prefix match is what allows exceptions inside an aggregate. A customer who moves to another provider can keep its /24, and the new provider advertises that /24 as a more specific route.
import ipaddress
routes = {
"0.0.0.0/0": "ISP A",
"203.0.112.0/22": "ISP B",
"203.0.113.0/24": "ISP C",
}
def lookup(destination):
address = ipaddress.ip_address(destination)
best = None
for prefix, hop in routes.items():
net = ipaddress.ip_network(prefix)
if address in net and (best is None or net.prefixlen > best[0].prefixlen):
best = (net, hop)
return best[1]
print(lookup("203.0.113.9")) # ISP C
print(lookup("203.0.114.9")) # ISP B
print(lookup("8.8.8.8")) # ISP AClasses after CIDR
Classes did not disappear from vocabulary. People still say "class C" to mean a /24, and old equipment or documentation may assume classful defaults. Modern routing protocols and operating systems do not use the first octet to choose a mask. The mask is always configured or advertised explicitly.
Two things remain from the classful era:
- Class D (224.0.0.0/4) is still used for multicast.
- Class E (240.0.0.0/4) is still reserved.
Both are now simply prefixes, not classes in the routing sense.
Common errors
- Assuming the mask from the first octet.
10.1.1.0/24is a /24 network even though 10.x.x.x was once class A. Always read the prefix. - Misaligned blocks.
10.4.4.0/21is not a valid network, because 4 is not a multiple of 8. The Python module rejects it unless you passstrict=False, which silently rounds down to10.4.0.0/21. - Counting usable hosts on /31 and /32. The "subtract 2" rule does not apply to /31 point-to-point links (RFC 3021), which use both addresses, or to a /32, which names a single host.
- Aggregating non-adjacent blocks. Merging
203.0.112.0/24and203.0.114.0/24into a /22 would also claim the /24 between them. - Forgetting the default route.
0.0.0.0/0is a valid prefix with zero network bits. It matches every address.
import ipaddress
try:
ipaddress.ip_network("10.4.4.0/21")
except ValueError as error:
print(error) # 10.4.4.0/21 has host bits set
print(ipaddress.ip_network("10.4.4.0/21", strict=False)) # 10.4.0.0/21Exercise
An organization needs 900 hosts on one subnet and is given the block 172.20.8.0. Which prefix length fits best, what are the network and broadcast addresses, and is 172.20.8.0/22 a valid block?
Show answer
900 hosts need at least 10 host bits, because 2^10 = 1,024 gives 1,022 usable addresses (2^9 = 512 is too small). The prefix is 32 − 10 = /22. For a /22 the block size in the third octet is 4, and 8 is a multiple of 4, so 172.20.8.0/22 is a valid block. It runs from the network address 172.20.8.0 to the broadcast address 172.20.11.255.
Exercise
Can 192.168.4.0/24, 192.168.5.0/24, 192.168.6.0/24, and 192.168.7.0/24 be aggregated into one route? Can 192.168.5.0/24 through 192.168.8.0/24?
Show answer
The first set can. The four blocks are contiguous and 4 is a multiple of 4, so they collapse to 192.168.4.0/22. The second set cannot become a single prefix. It starts at 5, which is not a multiple of 4, so the smallest single block covering it would be much larger and would include other addresses. It needs several routes, such as 192.168.5.0/24, 192.168.6.0/23, and 192.168.8.0/24.