Course contents

Chapter 8: RoutingLesson 2 of 16

Longest prefix match

R1 has a summary route and a more specific exception for the same destination. Which next hop should it use? Change the destination below and watch where the matching prefix changes.

Beginner · 7 min read

Technical definition

Longest prefix match is the rule a router uses to choose a route when a destination address matches several entries in its routing table: it uses the matching route with the longest prefix length, which is the most specific one.

What longest prefix match is

The prefix length is the number after the slash, like /24. It says how many bits at the start of the address must match. A bigger number means a smaller, more specific network.

A packet's destination can match several routes at once. Longest prefix match is the rule: the router uses the matching route with the longest prefix. Other details, like which protocol the route came from, don't matter at this stage.

Change the destination; watch the winning prefix
R1 looks up destination 192.168.3.10The longest match is 192.168.3.0/24, via 10.0.13.2, R3. Matching prefixes are highlighted; the green selected row is the longest. This is a route lookup view, not a cabling diagram.Packet destination: 192.168.3.10R1Route lookup0.0.0.0/0Every IPv4 address203.0.113.1 · ISPMatches, but shorter192.168.0.0/16192.168.0.0 – 192.168.255.25510.0.12.2 · R2Matches, but shorter192.168.3.0/24192.168.3.0 – 192.168.3.25510.0.13.2 · R3Selected: longest match192.168.3.128/25192.168.3.128 – 192.168.3.25510.0.12.2 · R2Does not match
192.168.3.0/24 → R3

3 installed prefixes match. /24 is the longest match, so R1 uses next hop 10.0.13.2.

All listed routes are assumed installed and usable in this example. Administrative distance is not re-compared during forwarding. Try .127 and .128 to see the /25 boundary; the .128 example is a lookup demonstration, not a claim that this /25 network address is a usable host.

Why the most specific route wins

A more specific route is usually a deliberate exception. Someone summarised all company LANs as 192.168.0.0/16, then added a /24 because one site has a better path. If the broad route won, the exception would never be used.

The flip side shows up in troubleshooting. When traffic takes a path nobody expects, the cause is often a more specific route left over from a test or a migration, quietly beating the summary everyone is looking at.

To check a match, compare only the first n bits. For the /25, the first three octets must be 192.168.3 and the first bit of the last octet must be 1 (values 128–255):

Diagram
The /25 boundary is one bit in the last octet
The /25 boundary is one bit in the last octet
The /25 boundary is one bit in the last octetFor 192.168.3.128/25, the first 24 bits identify 192.168.3 and the next bit must be one. Last-octet value 127 is binary 01111111 and does not match this /25; value 128 is 10000000 and does match. Both are still in 192.168.3.0/24.192.168.3.128/25 = 192.168.3 + first bit 1192.168.3.1270 1111111First bit 0: not in this /25192.168.3.1281 0000000First bit 1: matches this /25Both match /24; only the right address matches 192.168.3.128/25.
This compares destination bit patterns, not host usability. 192.168.3.128 is the /25 network address, while .255 is that /25's directed-broadcast address.

Forwarding chooses among routes already installed. Administrative distance and protocol path selection determine installation; they are not extra comparisons made for each packet.

Asking the router which route it would use

Give show ip route an address, and the router tells you which matching route is present in the routing table. This Cisco IOS XE 17.x-style privileged EXEC example is authored from documentation, not run on a lab device. Output details depend on the image. Forwarding uses the programmed forwarding table; a route lookup alone does not prove that a next hop or application is working.

Example output
R1#show ip route 192.168.3.200
Routing entry for 192.168.3.128/25
  Known via "static", distance 1, metric 0
  Routing Descriptor Blocks:
  * 10.0.12.2
      Route metric is 0, traffic share count is 1
The first line names the winning route. The /24 also matches, but the router reports the /25 because it is longer.

Try the boundary, then remove the fallback

Use the explorer as a small route-lookup lab. Select 192.168.3.127, then 192.168.3.128. The first destination chooses R3 through the /24; the second chooses R2 through the /25. The /25 covers values 128–255 in the last octet. This is a bit-match demonstration: the network and broadcast addresses in that range are not ordinary host addresses.

Select 198.51.100.7 and note the default route. Clear Include the installed default route: none of this model’s remaining prefixes matches, so R1 drops the packet. Select 192.168.3.200 while the default is absent; the /25 still works. Restore the checkbox and verify that 198.51.100.7 uses the default again.

Removing a fallback does not remove a more specific matching route. This explorer assumes its listed routes are installed and usable; it does not emulate next-hop resolution, interfaces, return paths or a real router. On a device, inspect the destination lookup and forwarding/next-hop state before changing any route.

Test yourself on longest prefix match

Predict · scenario 1

The model’s default route is removed. What happens to destination 198.51.100.7?

Predict · scenario 2

Using the table above, where does R1 send a packet for 192.168.3.127?

Predict · scenario 3

A router has 10.0.0.0/8 learned by OSPF and 10.1.1.0/24 as a static route. Which is used for 10.1.1.5?

Cisco reference: Route installation, administrative distance and forwarding.