Sub topic 13
Longest-prefix match in routing
This chapter assumes you can read CIDR notation, compute a subnet's network and broadcast addresses, and know what a summary route is. It explains how a router chooses among routes when more than one matches a destination.
Why several routes can match
A routing table is a list of prefixes, each paired with a next hop. A destination address matches a prefix when the address falls inside that prefix's block. Summarization makes overlapping matches common. A router may hold 10.0.0.0/8 as a broad route and 10.1.5.0/24 as a specific one, and the address 10.1.5.9 fits both.
The router resolves this with one rule: it uses the matching route with the longest prefix length. A longer prefix means more fixed bits, so a smaller and more specific block.
A worked example
Take this table:
| Prefix | Next hop |
|---|---|
| 0.0.0.0/0 | ISP |
| 10.0.0.0/8 | core |
| 10.1.0.0/16 | campus |
| 10.1.5.0/24 | lab |
Now look up 10.1.5.9. It matches all four rows, because 0.0.0.0/0 matches every address. The longest match is /24, so the packet goes to lab.
For 10.1.9.9, the /24 fails. The third octet is 9, not 5. The longest remaining match is /16, so the packet goes to campus.
How a router tests a match
A prefix matches when the destination and the prefix's network address agree on the first n bits. Equivalently, apply the prefix's mask to the destination with a bitwise AND and compare the result to the network address. This is the same AND operation you used to find a network address earlier.
For 10.1.9.9 against 10.1.0.0/16, the mask is 255.255.0.0. The AND gives 10.1.0.0, which equals the route's network address, so the route matches.
Real routers do not test every row in turn. They store prefixes in a trie, which is a tree that branches on one bit at a time, or in specialized hardware memory. The result is the same as the longest match found by scanning the whole table.
The same logic in Python
import ipaddress
routes = {
ipaddress.ip_network("0.0.0.0/0"): "ISP",
ipaddress.ip_network("10.0.0.0/8"): "core",
ipaddress.ip_network("10.1.0.0/16"): "campus",
ipaddress.ip_network("10.1.5.0/24"): "lab",
}
def lookup(addr):
ip = ipaddress.ip_address(addr)
matches = [net for net in routes if ip in net]
best = max(matches, key=lambda n: n.prefixlen)
return best, routes[best]
for a in ["10.1.5.9", "10.1.9.9", "10.200.0.1", "8.8.8.8"]:
print(a, *lookup(a))
# 10.1.5.9 10.1.5.0/24 lab
# 10.1.9.9 10.1.0.0/16 campus
# 10.200.0.1 10.0.0.0/8 core
# 8.8.8.8 0.0.0.0/0 ISPThe default route 0.0.0.0/0 has prefix length 0. It matches everything and always loses to any other match. It is chosen only when nothing more specific exists.
Common errors
- Choosing the first match in the list. Table order does not decide the winner. Prefix length does.
- Choosing the smallest address or the lowest network number. Only the number of matching bits counts.
- Forgetting that a hole in a summary stays routable. If you summarize
10.1.0.0/16toward one router but a different router owns10.1.5.0/24, the/24still wins for its addresses. This is useful for exceptions, but it also means a stale specific route can silently override your summary. - Treating a
/32as special. A/32is just the longest possible prefix. It matches exactly one host, so it beats every other route for that address.
Exercise
A router has these routes: 10.0.0.0/8, 10.1.0.0/16, and 10.1.8.0/22. Which route does it use for destination 10.1.9.9, and why?
Show answer
It uses 10.1.8.0/22. This block covers 10.1.8.0 to 10.1.11.255, so it contains 10.1.9.9. All three routes match, and the /22 has the longest prefix length.