Pcornel
Networking · Routing › RIB / FIB / CEF

Longest Prefix Match

Cheat sheet · https://pcornel.dk/topic.php?topic=2603

Back to the Topic

Answer

When several routes match a destination, the router uses the one with the longest prefix — the most specific. A /32 beats a /24, which beats a default route /0. Administrative distance and metric only decide between routes for the same prefix.

Remember

Specificity first, then administrative distance, then metric. A more specific route from any source wins over a less specific one from a "better" protocol. Summary and default routes catch only what nothing more specific matches.

First checks

  • show ip route <dst> — the route actually chosen for that address.
  • show ip route <prefix> <mask> longer-prefixes — every more specific route under a block.
  • show ip cef <dst> — the forwarding entry used.
  • show ipv6 route <dst> — the same for IPv6.

Verify in this order

destination address → all routes that contain it → the longest prefix selected → its next hop resolved → CEF entry matches

Where it usually breaks

underlay/interface reachability → protocol adjacency/session → received/originated state → policy → best path/RIB → FIB/adjacency → traffic path

Route lookup decision order

StageDecisionExample
1Longest matching prefix/32 beats /24 beats /0
2Route-source preference for same prefixlower Cisco AD wins between sources
3Protocol-specific path choiceOSPF cost, EIGRP DUAL/metric, BGP best-path logic
4Multipath/install constraintsequal/eligible paths may coexist
5FIB/CEF programmingrecursive next hop and adjacency become forwarding state

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=2603

Confirmation

Confirm action

Continue?