Sub topic 6
Route aggregation and supernetting
This chapter assumes you can convert between binary and dotted-decimal, read a prefix such as /22, and compute a network address from an address and a mask. It also assumes you know that a router forwards by matching a destination against prefixes.
What aggregation does
Route aggregation (also called supernetting) replaces several adjacent prefixes in a routing table with one shorter prefix that covers them all. Subnetting borrows host bits to make blocks smaller. Supernetting gives bits back to make a block larger.
Aggregation has two benefits:
- The routing table gets smaller, so lookups are faster and less memory is used.
- Routing updates shrink. An ISP can advertise one prefix to the rest of the Internet instead of one prefix per customer.
When blocks can be merged
Several blocks merge into one prefix exactly when three conditions hold:
- The blocks are contiguous, with no gaps between them.
- They have the same size, 2^k addresses each. (Blocks of mixed sizes can be merged in steps, as the next section shows.)
- The number of blocks, n, is a power of 2, and the first block's address is a multiple of n × 2^k.
Merging n blocks removes log2(n) bits from the prefix length. Two /24 blocks become a /23. Four become a /22. Eight become a /21.
Condition 3 is the one that trips people up. The block of size n × 2^k must start on its own size boundary. For /24 blocks this means the third octet of the first block must be divisible by n.
| Blocks | n | First third octet | Divisible? | Result |
|---|---|---|---|---|
| 192.168.0.0/24, 192.168.1.0/24 | 2 | 0 | Yes | 192.168.0.0/23 |
| 192.168.1.0/24, 192.168.2.0/24 | 2 | 1 | No | Cannot merge exactly |
| 192.168.4.0/24 to 192.168.7.0/24 | 4 | 4 | Yes | 192.168.4.0/22 |
| 192.168.2.0/24 to 192.168.5.0/24 | 4 | 2 | No | Cannot merge exactly |
The second row is contiguous (1.255 is followed by 2.0), yet it fails. The only /23 containing 192.168.1.0 starts at 192.168.0.0, and the only /23 containing 192.168.2.0 starts at 192.168.2.0. Those are different blocks.
Aggregating mixed sizes
Work from the largest block downward and merge in steps. Suppose a router holds 10.0.0.0/24, 10.0.1.0/24 and 10.0.2.0/23.
- The first two merge into 10.0.0.0/23.
- That /23 is now adjacent to 10.0.2.0/23, and 10.0.0.0 is a multiple of 1024 (a /22 holds 1024 addresses; the third octet 0 is divisible by 4). They merge into 10.0.0.0/22.
Python's standard library does this exact merging for you:
import ipaddress
exact = ipaddress.collapse_addresses([
ipaddress.ip_network("10.0.0.0/24"),
ipaddress.ip_network("10.0.1.0/24"),
ipaddress.ip_network("10.0.2.0/23"),
])
print(list(exact))
misaligned = ipaddress.collapse_addresses([
ipaddress.ip_network("192.168.1.0/24"),
ipaddress.ip_network("192.168.2.0/24"),
])
print(list(misaligned))The first call prints [IPv4Network('10.0.0.0/22')]. The second prints both /24 blocks unchanged, because no exact merge exists.
Smallest covering block
Exam questions often ask for the smallest single prefix that covers a set of networks, even when the merge is not exact. Take the lowest address and the highest address, then find the longest prefix whose block contains both.
import ipaddress
def supernet(blocks):
nets = [ipaddress.ip_network(b) for b in blocks]
low = min(n.network_address for n in nets)
high = max(n.broadcast_address for n in nets)
for prefix in range(32, -1, -1):
candidate = ipaddress.ip_network((str(low), prefix), strict=False)
if high in candidate:
return candidate
for group in (["192.168.4.0/24", "192.168.7.0/24"],
["192.168.1.0/24", "192.168.2.0/24"]):
s = supernet(group)
print(s, s.num_addresses)This prints 192.168.4.0/22 1024 and 192.168.0.0/22 1024. In the second case the two /24 blocks total 512 addresses, but the covering block holds 1024. The extra addresses belong to 192.168.0.0/24 and 192.168.3.0/24, which the advertiser does not own.
Aggregation and longest-prefix match
Aggregation and longest-prefix match work together. A router can hold a broad aggregate and a more specific route that carves out an exception. The more specific route wins for addresses it covers, and the aggregate handles the rest.
Example: an ISP advertises 200.10.0.0/16. One customer takes a block, 200.10.4.0/24, and moves to a different provider. The new provider advertises 200.10.4.0/24. A router that sees both sends 200.10.4.x to the new provider and everything else in 200.10.0.0/16 to the ISP. This is why exceptions do not break aggregation, though they do add entries.
Common mistakes
- Checking only contiguity. The merged block must start on a boundary equal to its own size.
- Miscounting prefix bits. Merging four blocks changes the prefix by 2 bits, not 4.
- Merging blocks of different sizes in one step. Merge the equal-size neighbours first, then repeat.
- Forgetting that the network address changes. In 192.168.5.0/24 plus 192.168.4.0/24, the aggregate is written from the lower address, 192.168.4.0/23.
Exercise
A router has routes for 200.10.8.0/24, 200.10.9.0/24, 200.10.10.0/24 and 200.10.11.0/24, all with the same next hop. Give the single aggregate prefix. Then say whether 200.10.9.0/24 through 200.10.12.0/24 could be aggregated the same way.
Show answer
The third octets 8, 9, 10, 11 are four contiguous blocks, and 8 is divisible by 4, so they merge into 200.10.8.0/22 (the prefix shrinks by log2(4) = 2 bits). The second set starts at 9, which is not divisible by 4, so no exact aggregate exists. The smallest covering block would be 200.10.8.0/21, which covers 2048 addresses for only 1024 owned.