Longest prefix match

📘 CCNA 200-301 v1.1

3.2 Determine how a router makes a forwarding decision by default

3.2.a Longest prefix match

How a Router Makes a Forwarding Decision (Default Behavior)

Subtopic: Longest Prefix Match


1. What happens when a router receives a packet

When a router receives an IP packet on one of its interfaces, it must decide where to send it next.
To make this decision, the router looks at the destination IP address inside the packet and checks its routing table to find the best route.


2. What is the Routing Table

The routing table is like a database that contains:

  • Destination networks (where traffic can go)
  • Subnet masks (or prefix lengths) for those networks
  • Next-hop IP addresses (the next router or device to send the packet to)
  • Outgoing interfaces (which interface to send the packet out)
  • Administrative distance and metric (used to choose between multiple routes, if needed)

A simplified routing table might look like this:

Destination NetworkSubnet MaskNext HopInterface
192.168.1.0255.255.255.0 (/24)10.1.1.2G0/0
192.168.0.0255.255.0.0 (/16)10.2.2.2G0/1
0.0.0.00.0.0.0 (/0)10.3.3.3G0/2

3. How the Router Chooses the Best Route

The router checks each entry in the routing table to see which one matches the destination IP address in the packet.
There might be more than one match because networks can overlap.

For example, both:

  • 192.168.0.0/16
  • 192.168.1.0/24

can match the same IP address, such as 192.168.1.10.

So, which one should the router choose?


4. The Rule: Longest Prefix Match

The router will always choose the route with the longest prefix match.

That means the route with the most specific subnet mask — the largest number of bits in the prefix — will be selected.


5. What “Longest Prefix” Means

The prefix length refers to how many bits of the network portion are fixed (shown after the slash, like /24, /16, etc.)

  • /8 → Network portion = 8 bits
  • /16 → Network portion = 16 bits
  • /24 → Network portion = 24 bits

The longer the prefix, the smaller the network, and the more specific the route.

So /24 is more specific than /16, and /16 is more specific than /8.


6. Example: Step-by-Step Longest Prefix Match

Let’s say the router receives a packet with destination IP:
192.168.1.10

And the routing table has:

Destination NetworkPrefixNext Hop
192.168.0.0/1610.1.1.1
192.168.1.0/2410.2.2.2
0.0.0.0/010.3.3.3

Now the router compares the destination IP (192.168.1.10) with each network:

  1. 192.168.0.0/16 — Matches (first 16 bits are the same)
  2. 192.168.1.0/24 — Matches (first 24 bits are the same)
  3. 0.0.0.0/0 — Matches everything (default route)

Since all three match, the router uses the longest prefix match rule.

  • /24 is longer (more specific) than /16 or /0.

The router will choose 192.168.1.0/24 as the best route.


7. Why Longest Prefix Match is Important

It ensures that:

  • Traffic takes the most accurate route possible.
  • Local or specific network routes are preferred over general ones.
  • Packets don’t accidentally take a longer or less efficient path.

In real IT networks, you might have general routes for large networks (like corporate WANs) and specific routes for particular subnets (like branch offices). The router uses longest prefix match to pick the right one.


8. If There is No Match

If the router finds no matching route in the routing table, it checks if there is a default route (0.0.0.0/0).

  • If a default route exists, the packet is forwarded using that route.
  • If there is no default route, the router drops the packet and sends an ICMP Destination Unreachable message back to the source.

9. Exam Tip (CCNA Focus)

✅ You must understand that:

  • Routers always perform longest prefix match.
  • The prefix length determines how specific a route is.
  • Even if multiple routing protocols (like OSPF, EIGRP, RIP) advertise routes, after selecting the best route per protocol, the router still applies longest prefix match before forwarding.
  • The administrative distance and metric only matter when there are multiple routes to the same exact network (same prefix).
    Longest prefix match comes before administrative distance comparison.

10. Quick Summary

ConceptDescription
Routing TableList of known networks and how to reach them
Prefix LengthNumber of bits defining the network portion
Longest Prefix MatchRouter selects the most specific (longest) match
Default Route (0.0.0.0/0)Used when no specific route exists
If no match and no default routePacket is dropped

11. Key Commands to Verify (For Lab/Exam)

On Cisco routers:

  • show ip route → Displays the routing table
  • show ip route [IP-address] → Shows which route matches that IP and which interface it will use

Example:

Router# show ip route 192.168.1.10
Routing entry for 192.168.1.0/24
  Known via "ospf", distance 110, metric 20
  Redistributing via ospf
  Last update from 10.2.2.2 on GigabitEthernet0/1, 00:00:22 ago

This output shows that the packet to 192.168.1.10 will use the 192.168.1.0/24 route.


Final Exam Key Point:

A router always forwards packets based on the route with the longest (most specific) prefix match in the routing table.
If no matching route exists, it uses the default route or drops the packet.


Leave a Reply

Your email address will not be published. Required fields are marked *

Buy Me a Coffee