Longest-prefix match explained with one diagram

In production now
Subscribe and you'll get it the day it goes live. A new episode every week.
Subscribe on YouTubeAbout this episode
Four routes match the same packet. Which one does the router use? This episode explains longest-prefix match with a single nested-box diagram, works through four examples on our ISP's addresses (pause and guess two of them yourself), and shows why this one rule drives more-specific announcements, /32 loopbacks, aggregation, and blackholes in service-provider networks, plus why MPLS swaps it for an exact-match label lookup in the core. Part of Ping Academy's free course "IP/MPLS: Zero to Service Provider".
What you'll learn
- Read a prefix and its length (or mask) as "fixed bits + free bits" and say how many addresses it covers.
- Apply longest-prefix match: of all routes that match a destination, the one with the longest prefix length wins.
- Explain why administrative distance and metric don't compete with longest-prefix match (different question, different stage).
- Use the default route correctly as the match of last resort.
- Explain why longest-prefix match matters in service-provider networks: more-specific announcements and hijacks, /32 loopbacks, aggregation, discard routes and blackholes.
- Describe, at a teaser level, how MPLS replaces per-hop longest-prefix match in the core with an exact-match label lookup.
Cheat sheet
- Prefix length = number of fixed bits. A /n covers 2^(32−n) addresses: /24 = 256, /28 = 16, /32 = 1, /0 = all.
- Mask = 1s for fixed bits, 0s for free bits: /24 = 255.255.255.0, /28 = 255.255.255.240.
- Longest-prefix match: of all FIB entries that match the destination, the one with the longest prefix length wins.
- AD and metric pick between routes for the same prefix (into the RIB). LPM picks between different prefixes (per packet, in the FIB). A /32 from a less-trusted source still beats a /24.
- Default route
0.0.0.0/0matches everything and wins only when nothing more specific matches. - Mental model: routes are nested boxes; a destination uses the smallest box it lands in.
- More-specifics always win: useful for traffic steering, dangerous in leaks and hijacks (e.g., the 2008 YouTube incident).
- Loopbacks are /32: BGP next hops and (later) MPLS label paths depend on the exact /32. If it disappears, traffic silently falls back to a less specific route.
- Summarize + discard: when announcing an aggregate, install
aggregate → Null0so unused addresses in the block are dropped instead of looping. - Check it in the lab: FRR
show ip route 10.255.0.14(longest match for an address) · Linuxip route get 10.255.0.14. MPLS teaser: LPM once at the ingress PE, exact-match label lookups in the core.