Sub topic 2
Longest-prefix match: the most specific route wins
This chapter assumes you can read CIDR prefixes such as 10.2.0.0/16, know the difference between a router, a switch and a host, and can run commands on a Linux host or router.
The rule
A routing table often holds several routes that match one destination address. The router does not take the first match or the lowest-numbered one. It picks the matching route with the longest prefix length, meaning the most bits in the mask. A /24 beats a /16, and a /16 beats a /0.
A route matches when the destination address agrees with the route's network address on every bit covered by the prefix length. Among all matches, the router forwards using the longest.
A worked example
Suppose a router has these routes:
| Prefix | Next hop |
|---|---|
| 0.0.0.0/0 | 192.168.1.1 |
| 10.0.0.0/8 | 192.168.1.3 |
| 10.2.0.0/16 | 192.168.1.2 |
| 10.2.5.0/24 | 192.168.1.4 |
Now look up three destinations:
| Destination | Matching prefixes | Winner |
|---|---|---|
| 10.2.5.7 | /0, /8, /16, /24 | 10.2.5.0/24 via 192.168.1.4 |
| 10.2.9.1 | /0, /8, /16 | 10.2.0.0/16 via 192.168.1.2 |
| 172.16.0.9 | /0 only | 0.0.0.0/0 via 192.168.1.1 |
For 10.2.5.7, all four routes match. The /24 wins because it is the most specific.
Seeing it on Linux
You can build the same table on a Linux host and ask the kernel which route it would use.
# Add routes of different specificity
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 would be chosen for one destination
ip route get 10.2.5.7The ip route get output names the next hop of the /24 route, even though the /16 route also matches. Asking for 10.2.9.1 instead returns the /16 route.
Why this matters
Longest-prefix match is what lets one table hold both broad and narrow routes. A few consequences follow:
- A specific route overrides a broader one for part of its range. You can send 10.2.5.0/24 to a different next hop than the rest of 10.2.0.0/16.
- The default route 0.0.0.0/0 matches everything, but it has the shortest possible prefix. It is used only when nothing more specific matches.
- Prefix length is compared before anything else. Metric and administrative distance, covered in later chapters, only choose between routes for the same prefix.
Common errors
- Assuming metric decides first. A /24 with a poor metric still beats a /16 with a good one. The router never compares them by metric.
- A stray specific route. A forgotten /32 or /24 can pull traffic away from the path you intended. If traffic to one host behaves differently from the rest of its subnet, look for a more specific route.
- Wrong mask on a static route. Entering 10.2.0.0/24 when you meant /16 leaves most of the range unmatched. That traffic then falls to the default route or is dropped.
Exercise
A router has 0.0.0.0/0 via A, 10.0.0.0/8 via B, and 10.1.0.0/16 via C. Which next hop is used for 10.1.4.20, and which for 10.9.0.1?
Show answer
10.1.4.20 matches all three routes and the /16 is longest, so it goes via C. 10.9.0.1 matches the default route and the /8 but not the /16, so the /8 is longest and it goes via B.
Exercise
A /16 route has metric 1 and a /24 route covering part of it has metric 100. A packet's destination falls inside the /24. Which route is used, and why?
Show answer
The /24 route is used. Longest-prefix match is applied first, and metric is not compared between routes with different prefix lengths.