Course contentsShow
← Computer Science
Lesson 1469 of 2,87234. Internet Protocol SuiteFree lesson

Longest Prefix Match Routing

How routers select the most specific matching route using longest prefix matching algorithm.

Longest Prefix Match Routing

What you'll learn: How routers use the longest prefix match algorithm to pick the most specific route from multiple matching options in their routing table.

The Problem: Multiple Matching Routes

When a router receives a packet destined for an IP address, it looks up its routing table to decide where to forward it. But here's the twist: multiple routes might match the same destination address. For example, both 192.168.0.0/16 and 192.168.5.0/24 match the address 192.168.5.42. Which one should the router use?

The Solution: Most Specific Wins

Longest prefix matching solves this by choosing the route with the longest matching prefix (the most specific subnet mask). Think of it like GPS directions: if you're looking for "123 Main Street, Springfield, Illinois," you'd prefer directions to that exact address over general directions to "Illinois" or even just "Springfield."

How It Works

The router:

  1. Converts the destination IP to binary
  2. Compares it against each routing table entry's network prefix
  3. Finds all matches
  4. Selects the one with the most bits in the prefix (highest /XX CIDR notation)

Example

For destination 192.168.5.42, if the routing table contains:

  • 0.0.0.0/0 (default route) — matches
  • 192.168.0.0/16 (65,536 addresses) — matches
  • 192.168.5.0/24 (256 addresses) — matches with longest prefix, WINS

The /24 route is chosen because 24 bits > 16 bits > 0 bits.

Why This Matters

Longest prefix matching enables hierarchical routing and efficient traffic engineering. More specific routes override general ones, letting network administrators create exceptions and optimize paths without breaking the entire routing system.

Key Takeaway: Longest prefix match routing selects the most specific (longest subnet mask) matching route, ensuring packets take the most precise path available in the routing table.