Longest prefix match is the forwarding rule by which a router selects, among all routing table entries that contain a packet’s destination address, the one with the longest subnet mask, meaning the most specific prefix. In routing, it is the decisive step that turns a table of overlapping routes into a single next hop for every packet.
The rule exists because classless addressing (CIDR) allows prefixes of any length from /0 to /32 in IPv4 and /0 to /128 in IPv6, so one address often matches several routes at once. A default route, a summary route and a host route can all cover the same destination, and longest prefix match guarantees that the most precise information wins.
How longest prefix match selects a route
A router performs longest prefix match by comparing the leading bits of the destination address with each stored prefix and keeping the matching entry that has the greatest number of fixed bits. Bits beyond the prefix length are ignored, so 10.1.0.0/16 matches every address whose first 16 bits are 00001010.00000001.
The following table shows how a router with four overlapping routes forwards a packet destined to 10.1.2.77.
| Prefix | Matches 10.1.2.77 | Prefix length | Result |
|---|---|---|---|
| 0.0.0.0/0 | Yes | 0 bits | Default route, least specific |
| 10.0.0.0/8 | Yes | 8 bits | Matches but less specific |
| 10.1.2.0/24 | Yes | 24 bits | Selected, longest match |
| 10.1.3.0/24 | No | 24 bits | Third octet differs |
Only matching entries are considered, and the comparison is purely about specificity, not about metrics. A /24 learned with a poor metric still beats a /16 with an excellent metric, because the two routes describe different sets of destinations.
Where longest prefix match fits in route selection
Longest prefix match operates on the forwarding table after the router has already decided which routes to install. Route installation and packet forwarding are separate decisions, and confusing them is one of the most common errors in routing troubleshooting.
When several sources offer the same prefix with the same length, the router first compares administrative distance to choose between protocols, then the protocol’s own metric to choose between paths. Only the winner, or several equal-cost winners with ECMP, enters the table. Longest prefix match then picks among the installed prefixes of different lengths for every packet.
| Stage | Question answered | Deciding factor |
|---|---|---|
| Protocol selection | Which source provides this exact prefix | Administrative distance |
| Path selection | Which path within one protocol is best | Protocol metric such as cost or AS path |
| Multipath | Can traffic be shared across paths | Equal-cost multipath rules |
| Forwarding lookup | Which installed prefix applies to this packet | Longest prefix match |
Policy-based routing and virtual routing and forwarding instances can intervene before this lookup. With VRF, the router performs longest prefix match inside a separate table per instance, so identical prefixes in different VRFs never compete.
How routers perform the lookup at line rate
Finding the longest match among hundreds of thousands of prefixes for every packet is computationally demanding. The full IPv4 Internet table exceeds 900,000 prefixes, and a 100 Gbit/s port can receive roughly 148 million minimum-size packets per second.
Implementations therefore rely on specialized data structures and hardware. The table compares the main approaches.
| Method | Principle | Strength | Limitation |
|---|---|---|---|
| Binary trie | One tree level per address bit | Simple and exact | Up to 32 or 128 memory accesses |
| Multibit or compressed trie | Several bits per step, paths compressed | Few memory accesses | Complex updates |
| TCAM | Parallel ternary comparison of all entries | One lookup per clock cycle | Expensive, power-hungry, limited capacity |
| Hash tables per length | Binary search across prefix lengths | Good software performance | Needs marker entries |
In ternary content-addressable memory (TCAM), entries are stored in order of decreasing prefix length so that the first hit is automatically the longest match. Software routers and virtual switches typically use compressed tries, such as the Poptrie or DIR-24-8 designs, which trade memory for speed.
Practical uses in network design
Network engineers use longest prefix match intentionally to steer traffic. Because a more specific route always wins, adding a narrow prefix overrides a broader one without removing it.
- Default routes at 0.0.0.0/0 catch all traffic that no specific route covers, so branch routers need only a few local entries.
- Route aggregation advertises one short summary prefix while longer internal prefixes keep precise forwarding inside the domain.
- Traffic engineering in BGP announces two /24 halves of a /23 through different providers, so inbound traffic prefers the more specific path.
- Host routes at /32 or /128 pin a single address, for example a loopback or anycast service, to a particular path.
- Black hole routes for an attacked prefix discard traffic early because they are more specific than the normal route.
Most providers filter BGP announcements longer than /24 in IPv4 and longer than /48 in IPv6, so traffic engineering with more-specific prefixes works only within those practical limits.
Risks, pitfalls and misconceptions
The strength of longest prefix match is also its main security risk: whoever announces the most specific prefix attracts the traffic. In a BGP prefix hijack, an unauthorized network announces a more specific route than the legitimate owner, and routers worldwide follow it. Resource Public Key Infrastructure (RPKI) route origin validation reduces this risk by checking the authorized origin and maximum length.
Operational mistakes follow a recognizable pattern. A forgotten static /32 can silently override a dynamic route, and a summary route pointing to a null interface can discard traffic for unused subnets by design or by accident. Engineers who test reachability should always inspect which specific entry the router actually selected rather than assume the expected route is in use.
The idea in brief
Imagine a mail sorting center with several rules: one for an entire country, one for a city, and one for a single street. A letter addressed to that street matches all three rules, but the most detailed rule decides where it goes.
Routers work the same way. When several entries in their table fit a destination, they follow the most specific one. Broad entries such as the default route are only a fallback for everything that has no better match.
A practical scenario
A company holds the address block 198.51.100.0/23 and connects to two Internet providers. Its web servers sit in the lower half, and the team wants inbound web traffic to arrive mainly through the faster provider while keeping both links usable.
The engineers announce the full /23 through both providers and additionally announce 198.51.100.0/24 only through the faster one. Remote routers see both the /23 and the /24 for any address in the lower half and, following longest prefix match, send that traffic over the faster provider. The upper half reaches the company through whichever path BGP prefers for the /23.
When the faster link fails, the /24 disappears from the Internet within seconds, and traffic falls back to the /23 through the second provider. The design delivers predictable inbound load balancing and automatic failover without any special agreement between the providers.