Sub topic 4
Longest prefix match: picking the most specific route
This chapter assumes you can read CIDR notation such as 10.1.0.0/16, and that you know a routing table maps destination prefixes to next hops. Here you will see how a router chooses when several entries match one destination.
The problem: more than one entry matches
A destination address often falls inside several prefixes at once. Take a table with three routes:
| Prefix | Next hop |
|---|---|
| 0.0.0.0/0 | 192.0.2.1 |
| 10.0.0.0/8 | 192.0.2.2 |
| 10.1.0.0/16 | 192.0.2.3 |
The address 10.1.2.3 matches all three. The /0 prefix matches every address. The /8 matches because the first 8 bits are 10. The /16 matches because the first 16 bits are 10.1.
The rule
The router picks the matching entry with the longest prefix length, meaning the most fixed bits. This is called longest prefix match (LPM). A longer prefix describes a smaller set of addresses, so it is the most specific description of where the destination lives.
This lets an operator send a whole range one way and carve out an exception for a part of it. Traffic for 10.0.0.0/8 can go to one router while 10.1.0.0/16 goes to another.
A simple implementation
The following code scans every route, keeps the ones that contain the destination, and returns the one with the largest prefix length.
import ipaddress
routes = [
(ipaddress.ip_network("0.0.0.0/0"), "192.0.2.1"),
(ipaddress.ip_network("10.0.0.0/8"), "192.0.2.2"),
(ipaddress.ip_network("10.1.0.0/16"), "192.0.2.3"),
]
def lookup(destination):
addr = ipaddress.ip_address(destination)
matches = [(net, hop) for net, hop in routes if addr in net]
if not matches:
return None
return max(matches, key=lambda m: m[0].prefixlen)
print(lookup("10.1.2.3")) # (IPv4Network('10.1.0.0/16'), '192.0.2.3')
print(lookup("10.9.9.9")) # (IPv4Network('10.0.0.0/8'), '192.0.2.2')
print(lookup("8.8.8.8")) # (IPv4Network('0.0.0.0/0'), '192.0.2.1')Walk through the three calls:
- 10.1.2.3 matches /0, /8 and /16. The /16 is longest, so the next hop is 192.0.2.3.
- 10.9.9.9 matches /0 and /8 only. It is outside 10.1.0.0/16, so the /8 wins.
- 8.8.8.8 matches only the /0, which is the default route.
The order of entries does not matter. Only prefix length decides.
Common errors
- Assuming first match wins. Firewall rule lists often work that way. Routing tables do not. A broad route listed first does not hide a narrower one.
- Comparing addresses instead of prefix lengths. A larger network address is not more specific. Only the mask length counts.
- Forgetting that /0 always matches. If no other route matches, the default route catches the packet. Without one, the router drops it and normally sends an ICMP destination unreachable message.
- Expecting a more specific route to be ignored when its next hop is down. Specificity is checked first. If the chosen route is unusable, the router does not fall back to a shorter match unless the route itself is withdrawn.
Checking it on a real system
On Linux, ip route get shows which route the kernel would select for a destination.
ip route show
ip route get 8.8.8.8The output of ip route get names the next hop and outgoing interface, which is the result of the same longest prefix match.
Exercise
A router has routes for 172.16.0.0/12 via A, 172.16.5.0/24 via B, 172.16.5.128/25 via C, and 0.0.0.0/0 via D. Which next hop is used for a packet to 172.16.5.200, and which for 172.16.5.20?
Show answer
172.16.5.200 matches all four routes. The /25 covers 172.16.5.128 to 172.16.5.255, so it is the longest and the packet goes via C. 172.16.5.20 is outside the /25 but inside the /24, /12 and /0, so the /24 wins and the packet goes via B.