When a router gets a packet, it has to pick a route from its routing table to decide how to forward it. The routing table can hold many routes, sometimes dozens or even hundreds. Each route stands for a different network.
Sometimes, a router discovers multiple routes that match the same destination IP address but with different subnet masks. This is when the Longest Prefix Match (LPM) rule is important. This rule helps the router choose the best path for the destination network. It picks the most accurate route instead of a broader, less specific option.
Example scenario
Let’s look at an example together.
A packet with the destination IP address 192.168.1.130 arrives at router R1.Figure 1 – Router with multiple matching routes
When we check the routing table, the router finds three possible matches for this destination:
192.168.0.0/16
192.168.1.0/24
192.168.1.128/25
At first glance, all three entries seem valid because they all include the destination IP address 192.168.1.130. However, the router must select the most specific route according to the Longest Prefix Match (LPM) principle.
The router picks the route with the longest prefix. This means it chooses the network that has the most bits in common with the destination address. This method ensures that traffic is always forwarded through the most precise and efficient path available.
Let’s explore the LPM process step by step in the next section.
Answer the question below
The concept of the Longest Prefix Match (LPM) is simple. Each route in the routing table has a prefix length that tells the router how much of the IP address identifies the network part.
To visualize this, let’s take the same routing table and look at the range of addresses covered by each prefix:192.168.0.0/16 → 192.168.0.0 – 192.168.255.255
192.168.1.0/24 → 192.168.1.0 – 192.168.1.255
192.168.1.128/25 → 192.168.1.128 – 192.168.1.255
Figure 2 – Routing table with different prefix lengths for the Longest Prefix Match
A shorter prefix covers a larger network, while a longer prefix is more specific.
When a router forwards a packet, it checks the destination IP address. It compares it to all prefixes in its table. The router selects the prefix with the most matching bits. This is known as the Longest Prefix Match rule.Finding the best route
Let’s see how it works using the destination 192.168.1.130.
By converting the networks into binary, we can see which prefix matches the destination for the longest bit sequence:Network Prefix
Binary Representation
Matching Bits
192.168.0.0/16
11000000.10101000.00000000.00000000
16
192.168.1.0/24
11000000.10101000.00000001.00000000
24
192.168.1.128/25
11000000.10101000.00000001.10000000
25
Destination 192.168.1.130
11000000.10101000.00000001.10000010
—
Table 1 - Longest Prefix Match Process
As shown above, the /25 prefix shares the longest sequence of identical bits with the destination address. Therefore, the router selects 192.168.1.128/25 as the best route to forward the packet.
Figure 3 – Longest Prefix Match selection of the most specific /25 route
Answer the question below
Which prefix has the most specific match for 192.168.1.130?
You can verify how routers apply the Longest Prefix Match (LPM) rule with the command
show ip route
This command shows which route the router selects for a specific destination.
Let’s test it with the destination 192.168.1.130:Figure 4 – CLI confirmation of Longest Prefix Match selection with show ip route
The output confirms that R1 uses the 192.168.1.128/25 route to reach 192.168.1.130, exactly as predicted by the Longest Prefix Match process.
This validation step is important in labs because it helps confirm which route is used by the router, not just what you expect theoretically.
Answer the question below
The Longest Prefix Match (LPM) rule is applied only when routes have different prefix lengths.
But sometimes, two or more routes share the same prefix length. In that case, the router cannot rely on LPM alone and must follow an additional decision process.When multiple routes have equal prefix lengths, the router uses the following order of preference:
Administrative Distance (AD)
Metric (if AD is the same)
Equal-Cost Multi-Path (ECMP)
1. Check Administrative Distance (AD)
If multiple routes point to the same prefix, the router first compares their Administrative Distance (AD).
The route with the lowest AD is installed in the routing table.
Figure 5 – Route selected by the lowest Administrative Distance (AD).This decision is straightforward. For example, if one route is static (AD 1) and another is OSPF (AD 110), the router keeps only the static route since it is considered more reliable.
2. Compare Metrics (if AD is the same)
If two routes have the same AD, the router then compares their metrics, also called path cost.
The metric helps determine which route offers a better path to reach the destination.Figure 6 – The route with the lower metric is selected.
For instance, in OSPF, both routes may exist in the Link-State Database (LSDB), but only the route with the lowest cost is installed in the routing table (RIB). The second one remains in the LSDB but is not used for forwarding traffic.
In this example, the router selects the route via 10.0.0.2 because it has the lowest metric (2) compared to 50 on the other route.3. Equal Cost Multi-Path (ECMP)
If the Administrative Distance and the metric are the same, the router adds multiple routes to the routing table.
This mechanism is called Equal-Cost Multi-Path (ECMP).Figure 7 – Both routes used with Equal-Cost Multi-Path (ECMP).
In this situation, the router uses both routes for forwarding. Traffic is load-balanced across the two equal-cost paths (via 10.0.0.2 and 10.0.0.3), depending on the platform and configuration. We’ll explore ECMP in detail in a dedicated lesson.
Answer the question below
If two routes have the same prefix length, what criterion is checked first?
Routers always choose the most specific route using the Longest Prefix Match (LPM) rule.
This applies when multiple routes lead to the same destination network but have different subnet masks.
If routes have the same prefix length, the router compares their Administrative Distance, then their metric.
If both values are identical, ECMP can be used to share traffic across multiple paths.In summary:
LPM applies when routes have the same destination network but different subnet masks.
Administrative Distance decides between routes with the same prefix.
The decision order is always: LPM → AD → Metric → ECMP.
This simple logic defines exactly how routers make their routing decisions.
Answer the question below