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 convert an IPv4 address to binary. It also assumes you know what a routing table is, as covered in the previous chapter. The chapter explains how a router chooses one entry when several entries match a destination address.
Why several entries can match
A routing table entry is a prefix. A prefix covers every address whose first N bits equal the first N bits of the entry. Prefixes can overlap, so one destination address often falls inside more than one entry.
Overlap is intentional. An operator can say "send everything in 10.0.0.0/8 toward router A" and also "send the smaller block 10.1.0.0/16 toward router B". The smaller block is an exception inside the larger one. The router needs a rule for deciding which entry wins, and that rule is longest prefix match.
The rule
- Compare the destination address with every prefix in the table.
- Keep only the entries whose prefix contains the destination.
- From those, pick the entry with the longest prefix length, meaning the largest number after the slash.
- Forward the packet using that entry's next hop and outgoing interface.
A longer prefix covers fewer addresses, so it is more specific. Choosing the most specific match lets a narrow rule override a broad one.
The router does not choose by the order of entries in the table, by the lowest next-hop address, or by the first match. Table order has no effect on which route wins.
A worked example
Take a router with this table:
| 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 |
Now test three destinations.
| Destination | Entries that contain it | Winner |
|---|---|---|
| 10.1.2.3 | /0, /8, /16 | 10.1.0.0/16 via 192.0.2.3 |
| 10.9.9.9 | /0, /8 | 10.0.0.0/8 via 192.0.2.2 |
| 8.8.8.8 | /0 | 0.0.0.0/0 via 192.0.2.1 |
Binary shows why 10.1.2.3 matches the /16. The first octet, 10, is 00001010. The second octet, 1, is 00000001. The /8 entry compares only the first 8 bits, and the /16 entry compares the first 16 bits. Both agree with the destination. The /16 entry agrees on more bits, so it is the more specific match.
The destination 10.9.9.9 has a second octet of 9 (00001001). That differs from 00000001, so the /16 entry fails and the /8 entry wins.
The entry 0.0.0.0/0 has a prefix length of zero. It compares no bits, so it matches every address. It is the default route, and it wins only when nothing more specific matches. The next chapter covers it in detail.
Longest prefix match in code
This Python function does a linear scan. It collects every matching route 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')Reordering the routes list does not change any result, because max looks at prefix length and ignores position. If you delete the 0.0.0.0/0 entry, lookup("8.8.8.8") returns None. This models a router with no default route.
A linear scan is fine for a teaching example but too slow for a real router with hundreds of thousands of routes. Production software stores prefixes in a binary trie or a compressed variant of one. Each bit of the destination address selects a branch, and the router remembers the last node that held a route. Hardware often uses TCAM, a memory that compares an address against all stored prefixes at once. The chapter on the FIB covers these structures.
Asking a real system
On Linux, ip route get runs the lookup for you and shows which entry won.
# Show the table
ip route show
# Ask which route the kernel would use for this destination
ip route get 8.8.8.8The output of ip route get names the next hop (via), the outgoing interface (dev), and the source address the kernel would use. The output reflects longest prefix match with the real table. Use it before you guess which route a packet takes.
Edge cases
Host routes
A /32 prefix in IPv4 covers exactly one address. It is the most specific prefix possible, so it beats every other entry for that address. Operators use /32 routes to steer traffic for a single server. IPv6 uses /128 for the same purpose.
A specific route can hide a broad one
Suppose the table holds 10.0.0.0/8 via router A and 10.1.0.0/16 via router B. If router B fails or its route points to a dead link, packets for 10.1.x.x still follow the /16 entry. They do not fall back to the /8 entry. A router never retries a less specific match because the more specific one is unhealthy. The /16 entry has to be withdrawn from the table before the /8 entry takes over. Routing protocols withdraw routes when a neighbor fails, and static routes stay until someone removes them.
Ties on prefix length
Two different prefixes of the same length cannot both contain the same address, because a given address has exactly one N-bit prefix. A tie therefore arises only when the table holds the same prefix learned more than once, for example one copy from a static route and one from a routing protocol. Longest prefix match does not resolve that case. A separate step uses administrative distance and metric to choose between the copies. A later chapter covers that step.
Lookup happens per packet, on the destination only
The lookup uses only the destination address. The source address, port numbers, and payload play no part in a plain routing lookup. Two packets to the same destination follow the same route unless policy routing, load balancing, or a table change intervenes.
No match
If no entry contains the destination, the router drops the packet. It usually sends an ICMP Destination Unreachable message to the source. A router with a default route never reaches this case, because 0.0.0.0/0 matches everything.
Overlap and route aggregation
Longest prefix match is what makes aggregation safe. An ISP can advertise one large prefix for all its customers, and a customer can still receive a more specific prefix for a block that moved elsewhere. Routers pick the more specific entry, so the exception works without changing the aggregate.
Summary of the decision
| Situation | Result |
|---|---|
| Several prefixes contain the destination | The longest prefix wins |
| Only 0.0.0.0/0 contains it | The default route is used |
| No prefix contains it | The packet is dropped and ICMP unreachable may be sent |
| The same prefix appears from two sources | Administrative distance and metric decide, not prefix length |
Exercise
A router has these routes: 172.16.0.0/12 via R1, 172.16.5.0/24 via R2, and 172.16.5.128/25 via R3. There is no default route. Which next hop does it use for 172.16.5.200, for 172.16.5.100, and for 172.20.1.1? What happens to a packet for 192.168.1.1?
Show answer
172.16.5.200 uses R3. The /25 covers 172.16.5.128 through 172.16.5.255, and it is the longest match. 172.16.5.100 uses R2. It falls inside the /24 but below 172.16.5.128, so the /25 does not contain it. 172.20.1.1 uses R1. The /12 covers 172.16.0.0 through 172.31.255.255, and neither longer prefix contains the address. 192.168.1.1 matches nothing and there is no default route, so the router drops it and may send ICMP Destination Unreachable.
Exercise
A table holds 10.0.0.0/8 via A and 10.1.0.0/16 via B. Link B goes down, but nobody removes the /16 route. Where do packets for 10.1.5.5 go, and why?
Show answer
They are still sent toward B and are lost. Longest prefix match selects the /16 because it is the most specific entry containing the address. The router does not fall back to the /8 entry unless the /16 entry is removed from the table.