Network Bytes · Episode 43 · CCNA 3.2
Longest prefix match
When two routes both contain a packet's destination, the router uses the more specific one, the route with the longest prefix. A route only counts if it contains the address, and the default route 0.0.0.0/0 is the last resort.
Sorting mail
Imagine sorting mail. One rule says anything for Canada goes in bin A. Another says anything for Toronto goes in bin B. A letter addressed to Toronto, Canada matches both rules, and you'd naturally pick the more specific one: bin B. Routers do the same thing with routes, and it's called longest prefix match.
What the prefix length means
The number after the slash is the prefix length: how many leading bits of the address must match the route's network. A /8 only checks the first 8 bits, so 10.0.0.0/8 covers everything from 10.0.0.0 to 10.255.255.255 ("ten dot anything"). A /24 checks the first 24 bits, so 10.1.1.0/24 covers only 10.1.1.0 to 10.1.1.255. A longer prefix means a smaller, more specific range.
/8 or /24?
Say a packet is going to 10.1.1.5, and the table has both 10.0.0.0/8 and 10.1.1.0/24. Both contain 10.1.1.5, but /24 is longer, so it wins and the packet follows that route.
Two rules keep this straight:
- A route only counts if it contains the address. A long prefix that doesn't include the destination is simply not a match, however specific it is.
- The default route is the last resort.
0.0.0.0/0has a prefix length of zero: it checks no bits, so it matches every address. Because every other matching route is longer, it's only used when nothing else matches.
Try it yourself
Look at your own computer's routing table. On Windows, open Command Prompt and run route print; on macOS, run netstat -rn; on Linux, run ip route. You'll usually see a default route (0.0.0.0/0, shown as default or 0.0.0.0 with mask 0.0.0.0) pointing at your router, plus a more specific route for your local network. Then practise: a table has 192.168.0.0/16 and 192.168.5.0/24. Which one does a packet for 192.168.5.20 use, and which one does a packet for 192.168.7.20 use?
Good to know
- Longest prefix match is applied to the routes that are already installed in the routing table. Administrative distance and metric only compare routes for the exact same prefix and length, to decide which one gets installed (that's EP44). A longer prefix always wins over a shorter one, whatever their administrative distance.
- "Ten dot anything" in the video is shorthand for
10.0.0.0/8, the range10.0.0.0to10.255.255.255. The mail bins are an analogy. - The same rule works for IPv6: routers match on prefixes of any length and use the longest one that contains the destination.
- With no matching route and no default route, the packet is dropped (EP41).
For CCNA learners
This lines up with CCNA 200-301 exam topic 3.2.a, determine how a router makes a forwarding decision by default: longest prefix match. Be ready to look at a routing table, find every route that contains a given address, and pick the one with the longest prefix, with 0.0.0.0/0 as the fallback.
Answer to the comment question
10.1.0.0/16. The /16 covers 10.1.0.0 to 10.1.255.255, so it contains 10.1.2.5. The /24 only covers 10.1.1.0 to 10.1.1.255, so it doesn't contain 10.1.2.5 at all. A route only counts if it contains the address, so the longer /24 never enters the race, and the /16 is the longest match.
Test your knowledge
Quick quiz: Longest prefix match
4 multiple-choice questions. Pick one answer for each, then check how you did.
Sources
- Cisco, "Configure Route Selection for Routers": the longest prefix match always wins among installed routes; administrative distance and metric decide what gets installed
- IETF, RFC 4632, "Classless Inter-domain Routing (CIDR)": forwarding in the Internet is done on a longest-match basis
- IETF, RFC 7608, "IPv6 Prefix Length Recommendation for Forwarding": implement longest-match-first on prefixes of any length
- Cisco, "Configure a Gateway of Last Resort that Uses IP Commands": the 0.0.0.0/0 default route is used when no more specific route matches
- Cisco, CCNA 200-301 v1.1 exam topics: 3.2.a determine how a router makes a forwarding decision by default: longest prefix match
Video transcript
When two routes both match, which one does the router pick?
Imagine sorting mail. One rule: anything for Canada goes in bin A. Another: anything for Toronto goes in bin B. A Toronto letter matches both.
You'd pick the more specific rule. Routers do the same. It's called longest prefix match.
Say a packet is going to ten dot one dot one dot five. The table has a slash eight route for ten dot anything, and a slash twenty-four route for ten dot one dot one.
Both contain it, but slash twenty-four is longer, so it wins. A route only counts if it contains the address. The default route matches everything, so it's the last resort.
Routes: ten dot one dot zero dot zero slash sixteen, and ten dot one dot one dot zero slash twenty-four. A packet heads to ten dot one dot two dot five. Which route wins? Comment your answer below!
That's your byte.
More from Network Bytes
- EP41What a router actually does
- EP42Reading a routing table
- EP44Administrative distance
- EP45Static and default routes