IP fundamentals

Longest prefix match in IP routing

Routers generally choose a matching route with the longest prefix; other routing policies determine choices among competing routes.

Updated

Find matching routes

Consider 192.0.2.0/24 and 192.0.2.32/27. The address 192.0.2.37 matches both. /27 fixes more leading bits than /24, so it is the more specific route.

Default route

0.0.0.0/0 matches any IPv4 address, but it is less specific than every other matching IPv4 prefix. The IPv6 counterpart is ::/0.

Distinguish selection stages

Routing protocols and local preferences determine which routes are installed. Longest prefix match then applies to destination forwarding among available routes. It does not imply that the most specific route has the lowest latency.

Compare nested prefixes

For 192.0.2.77, a /24 route to 192.0.2.0 and a /26 route to 192.0.2.64 both match. Under ordinary longest-prefix forwarding, the /26 is selected over the /24. Policy routing and route availability can affect the actual outcome, so check the active table.

Step-by-step example

Use three overlapping route entries to decide which route matches 192.0.2.70.

  1. 01

    List the candidate routes

    Imagine a routing table with a default route, 192.0.2.0/24 and 192.0.2.64/26. All three include the example destination, which lies in .64–.127.

    /0
    Default
    192.0.2.0/24
    .0–.255
    192.0.2.64/26
    .64–.127
    Illustration · example, not a live result
  2. 02

    Choose the most specific match

    The /26 fixes more leading bits than /24 or /0. It wins longest-prefix matching among eligible routes in this illustrative table.

    Destination
    192.0.2.70
    Winning prefix
    192.0.2.64/26
    Illustration · example, not a live result
  3. 03

    Compare another destination

    192.0.2.150 is inside /24 but outside the /26, so /24 is selected here. Real routing tables also account for policies, route availability and equal-prefix selection.

    192.0.2.70
    /26
    192.0.2.150
    /24
    Illustration · example, not a live result

Check your understanding

A more-specific matching route is preferred over a less-specific one in the routing table; registry ownership is a different question.

Try this tool →

Try this

Use the overlap checker with the /24 and /27 example; confirm the containment and resulting interval.

CIDR overlap checker →

Technical references