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:
| Route | Next hop | Covers |
|---|---|---|
0.0.0.0/0 | 203.0.113.1 (ISP) | Every address (the default route) |
192.168.0.0/16 | 10.0.12.2 (R2) | 192.168.0.0 – 192.168.255.255 |
192.168.3.0/24 | 10.0.13.2 (R3) | 192.168.3.0 – 192.168.3.255 |
192.168.3.128/25 | 10.0.12.2 (R2) | 192.168.3.128 – 192.168.3.255 |
Work it out
- 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. 192.168.3.200: matches all four routes. /25 is the longest, so R1 sends it through R2.
- 3. 192.168.2.10: matches only /0 and /16. The /16 wins: next hop R2.
- 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.
| Destination | Matching routes | Winner |
|---|---|---|
| 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):
| Address | Last octet in binary | Matches /25? |
|---|---|---|
| 192.168.3.128/25 (route) | 10000000 | - |
| 192.168.3.200 | 11001000 | Yes |
| 192.168.3.10 | 00001010 | No |
💡 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.
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
Check yourself
Using the table above, where does R1 send a packet for 192.168.3.127?
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?