Sub topic 2
Longest-prefix match: the most specific route wins
This chapter assumes you can read CIDR notation such as 10.2.0.0/16, convert a prefix length to a subnet mask, and tell a router from a host. It covers how a router picks one route from a table that may hold several matching entries.
Why more than one route can match
A routing table is a list of prefixes. Each entry says: "packets whose destination falls inside this prefix go to this next hop." Prefixes overlap all the time, because a short prefix such as 10.0.0.0/8 contains every longer prefix that starts with 10.
A router must therefore answer two questions for every packet:
- Which entries contain the destination address?
- If several do, which one is used?
The answer to the second question is longest-prefix match: among all matching entries, the router uses the one with the longest prefix length (the largest number after the slash). A longer prefix describes a smaller block of addresses, so it is a more specific statement about where the destination lives.
The matching test
An entry matches a destination when the first N bits of the destination equal the first N bits of the entry's network address, where N is the prefix length. Using the mask is equivalent: the entry matches if destination AND mask equals the network address.
Take the destination 10.2.5.7 and the prefix 10.2.0.0/16:
- The mask
255.255.0.0keeps the first two octets. 10.2.5.7 AND 255.255.0.0gives10.2.0.0.- That equals the entry's network address, so the entry matches.
For 10.3.0.0/16 the result 10.2.0.0 does not equal 10.3.0.0, so that entry does not match.
A worked example
Suppose a router has this table:
| Prefix | Next hop | Prefix length |
|---|---|---|
| 0.0.0.0/0 | 192.168.1.1 | 0 |
| 10.0.0.0/8 | 192.168.1.3 | 8 |
| 10.2.0.0/16 | 192.168.1.2 | 16 |
| 10.2.5.0/24 | 192.168.1.4 | 24 |
Now trace four destinations.
| Destination | Entries that match | Winner | Next hop |
|---|---|---|---|
| 10.2.5.7 | /0, /8, /16, /24 | 10.2.5.0/24 | 192.168.1.4 |
| 10.2.9.1 | /0, /8, /16 | 10.2.0.0/16 | 192.168.1.2 |
| 10.200.0.1 | /0, /8 | 10.0.0.0/8 | 192.168.1.3 |
| 172.16.0.1 | /0 | 0.0.0.0/0 | 192.168.1.1 |
The destination 10.2.5.7 matches all four entries. The /24 is the longest, so it wins, even though the /16 and /8 are also correct matches. The default route 0.0.0.0/0 matches everything and has length 0, so it only wins when nothing else matches. That is why it works as a catch-all.
Reproducing it on Linux
You can build this table on a Linux host or router and ask the kernel what it would do. The next hops must be reachable on a directly connected network, so this example assumes eth0 has an address in 192.168.1.0/24.
# Build the example table (next hops must be on a connected subnet)
sudo ip route add 10.0.0.0/8 via 192.168.1.3 dev eth0
sudo ip route add 10.2.0.0/16 via 192.168.1.2 dev eth0
sudo ip route add 10.2.5.0/24 via 192.168.1.4 dev eth0
# Show the table
ip route show
# Ask which route is chosen for specific destinations
ip route get 10.2.5.7
ip route get 10.2.9.1
ip route get 10.200.0.1ip route get reports the entry the kernel selected, including the next hop and the outgoing interface. On a Cisco IOS router the equivalent is show ip route 10.2.5.7, which prints the longest matching entry and how it was learned.
! Cisco IOS: show the route chosen for one destination
show ip route 10.2.5.7The order of the decision
The lookup follows a fixed order. Mixing these steps up is a common source of confusion.
- Prefix length decides first. The router considers only the longest matching prefix.
- Administrative distance decides between sources for the same prefix. If two sources, such as a static route and an OSPF route, both offer exactly
10.2.0.0/16, the lower administrative distance wins. - Metric decides between paths from the same source. If one protocol offers two paths to the same prefix, the lower metric is preferred, or the router uses both if the metrics tie and equal-cost load sharing is enabled.
Administrative distance and metric never override prefix length. A route with a terrible metric and a long prefix beats a route with a perfect metric and a shorter prefix. The later chapters on administrative distance and metrics build on this ordering.
Edge cases and common errors
A more specific route can hijack traffic
Because the longest prefix wins, adding a narrow route silently redirects part of a larger block. If you add 10.2.5.0/24 toward the wrong next hop, hosts in that /24 lose connectivity while the rest of 10.2.0.0/16 keeps working. This is also the reason an attacker or a misconfigured neighbor that announces a more specific prefix can attract traffic that belongs elsewhere.
A specific route to a dead next hop still wins
Longest-prefix match does not check whether the path works. If 10.2.5.0/24 is present in the table but its next hop is down and the route has not been removed, packets for that /24 are still sent to the dead next hop. They are not rerouted via the /16. The route must actually leave the table, through a failed interface, a withdrawn protocol route, or tracking, before the less specific entry takes over.
A missing route is not an error until nothing matches
If no entry matches and there is no default route, the router drops the packet and normally sends an ICMP "destination unreachable" message to the sender. With a default route present, the packet is forwarded toward the upstream router instead, which may then drop it. A missing return route is a common cause of one-way connectivity.
Equal prefixes are not a tie on length
Two routes with the same prefix and length are tied on step 1. They are separated by administrative distance and then metric, as described above. Do not assume the one added last or first wins.
Mask mistakes change which route matches
A typo such as 10.2.0.0/6 instead of /16 produces a very short prefix that matches a huge range of addresses. It will rarely win against specific routes, but it can capture traffic that would otherwise fall to the default route. Check the prefix length on every static route you add.
Host bits set in the network address
Entering 10.2.5.7/16 as a route destination has host bits set. Linux rejects some of these, and some platforms silently mask them to 10.2.0.0/16. Write the true network address so the table says what you mean.
Why routers can do this quickly
A naive lookup compares the destination with every entry, which is slow for large tables. Routers instead store prefixes in a structure such as a binary trie (a tree that branches one bit at a time). The lookup walks down the tree following the destination's bits and remembers the last entry it passed. When the walk ends, that remembered entry is the longest match. The cost depends on the address length (32 bits for IPv4), not on the number of routes. Hardware routers often use specialized memory (TCAM) to perform the same search in a single step.
Choosing prefix lengths in practice
- Use the shortest prefix that accurately describes the destination, so tables stay small.
- Add a longer prefix only when part of a block must take a different path, such as an exception for one subnet.
- Summarizing several contiguous subnets into one shorter prefix reduces table size, but a summary can attract traffic for addresses that are not actually reachable behind it.
Exercise
A router has these entries: 0.0.0.0/0 via A, 172.16.0.0/12 via B, 172.16.8.0/22 via C, 172.16.10.0/24 via D. Which next hop is used for the destination 172.16.9.40, and why?
Show answer
Next hop C. The /22 entry 172.16.8.0/22 covers 172.16.8.0 to 172.16.11.255, so it contains 172.16.9.40. The /24 entry covers only 172.16.10.0 to 172.16.10.255 and does not match. The /12 and /0 entries also match, but the /22 is the longest matching prefix, so it wins.
Exercise
The router has 10.2.0.0/16 via a working next hop and 10.2.5.0/24 via a next hop that has failed, but the /24 route is still in the table. Where do packets for 10.2.5.7 go, and what must happen for them to use the /16 route?
Show answer
They go to the failed next hop, because the /24 is the longest match and longest-prefix match does not test whether the path works. They use the /16 only after the /24 route is removed from the table, for example because its interface goes down, a protocol withdraws it, or route tracking removes a static route.