What is the "longest prefix match" rule, and why does a /28 route always beat a /24 route for the same destination?
Longest prefix match means the route with the most matching leading bits wins. A /28 mask matches 28 bits (more specific) while a /24 matches only 24 bits (less specific) — the /28 gives a more precise direction to the destination.
* Prefix length and route summarization. — Michel Bakni, CC BY-SA 4.0, via Wikimedia Commons. *
How longest match works:
For destination 10.1.1.55:
| Route | Prefix Length | Matches? | Result |
|---|---|---|---|
| 10.0.0.0/8 | 8 bits | Yes (first 8 bits match) | Less specific |
| 10.1.1.0/24 | 24 bits | Yes (first 24 bits match) | More specific |
| 10.1.1.48/28 | 28 bits | Yes (first 28 bits match) | Most specific — wins! |
| 10.1.2.0/24 | 24 bits | No (third octet doesn't match) | Not a match |
IPv6 example:
For destination 2001:db8:c000::99/48:
| Route Entry | Prefix | Match? |
|---|---|---|
| 2001:db8:c000::/40 | 40 bits match | Yes, but shorter prefix |
| 2001:db8:c000::/48 | 48 bits match | Yes — longest match! |
| 2001:db8:c000:5555::/64 | 64 bits required | No — bits 49-64 don't match |
Why this matters:
- Without longest match, all traffic to 10.1.1.x would take the same path
- With longest match, traffic to 10.1.1.48-63 can take a different (more optimal) path than traffic to 10.1.1.0-47
- This enables route summarization — a broad route catches most traffic, while specific routes provide exceptions
Tip: Think of it like postal sorting: "Switzerland" matches broadly, "Zurich, Switzerland" is more specific, and "Bahnhofstrasse 1, Zurich" is the most specific. The mail carrier always uses the most specific address they know.
Go deeper:
Longest prefix match (Wikipedia) — a worked lookup example showing the more-specific prefix always wins.