Routelearn.net
Course menu

Course 8: RoutingLesson 1.3 (2 of 12 in this course)51 of 91 in the CCNA series

Longest prefix match

When several routes match a packet, the most specific one wins. Here is how to work it out.

Beginner · 7 min read

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.

In simple terms: If several routes fit a destination, the router picks the most detailed one. It's like choosing directions to a street address over directions to just the city.

A real-life situation

A letter is addressed to "Flat 4, 12 Station Road, Leeds". The national sorting office only reads "Leeds". The Leeds office reads the street. The postman reads the flat number. The more detail someone has, the more exact their delivery. Routers work the same way: when several routes match, the one with the most detail wins.

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.

R1 now has a backup link to R3 and this table:

RouteNext hopCovers
0.0.0.0/0203.0.113.1 (ISP)Every address (the default route)
192.168.0.0/1610.0.12.2 (R2)192.168.0.0 – 192.168.255.255
192.168.3.0/2410.0.13.2 (R3)192.168.3.0 – 192.168.3.255
192.168.3.128/2510.0.12.2 (R2)192.168.3.128 – 192.168.3.255

Work it out

10.0.12.0/3010.0.23.0/3010.0.13.0/30 (backup)PC1192.168.1.10R1R2R3Server192.168.3.10ISP
  1. 1. 192.168.3.10: matches /0, /16 and /24, but not /25 (10 is below 128). Longest is /24, so it goes direct to R3.
  2. 2. 192.168.3.200: matches all four routes. /25 is the longest, so R1 sends it through R2.
  3. 3. 192.168.2.10: matches only /0 and /16. The /16 wins: next hop R2.
  4. 4. 198.51.100.7: matches only 0.0.0.0/0. The default route is the last resort, so it goes to the ISP.
DestinationMatching routesWinner
192.168.3.10/0, /16, /24/24 → R3
192.168.3.200/0, /16, /24, /25/25 → R2
192.168.2.10/0, /16/16 → R2
198.51.100.7/0/0 → ISP

Why it works this way

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.

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):

AddressLast octet in binaryMatches /25?
192.168.3.128/25 (route)10000000-
192.168.3.20011001000Yes
192.168.3.1000001010No

💡 Prefix length decides first. Administrative distance and metric (next lesson) only matter when two routes have the same prefix and length.

How to verify it

Give show ip route an address, and the router tells you which route it would use. This output is based on Cisco documentation, not run on a lab device.

Example output · based on Cisco documentation; exact format varies by platform and software version
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.

Check yourself

Predict · scenario 1

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

Predict · scenario 2

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?