Let’s Get Certified
Module 25 / 54 · IP connectivity

Select the longest matching route

A packet goes to 10.8.4.77. Installed routes include 10.0.0.0/8 through A, 10.8.0.0/16 through B and 10.8.4.64/26 through C. Which router receives it?

Select the longest matching route

The routing fundamentals lesson compares matching entries and selects the most specific prefix for the packet.

Work through the decision

A packet goes to 10.8.4.77. Installed routes include 10.0.0.0/8 through A, 10.8.0.0/16 through B and 10.8.4.64/26 through C. Which router receives it?

Check your reasoning

The /26 spans 10.8.4.64 through .127, so all three match and C wins on prefix length. At .128 the /26 no longer matches and B wins. Administrative distance helps choose routes for the same prefix when building the table.

Jeremy’s IT Lab · 24:22 · Read the synchronized transcript

Interpret the evidence

Longest prefix
Among matching installed routes the one with the greatest prefix length is the most specific.
Lower administrative distance
For the same prefix, the router normally prefers the candidate with the lower administrative distance across sources.
Lower OSPF cost
Within OSPF, compare eligible routes of the same type by accumulated cost.
Equal-cost multipath
A supported routing process can install several eligible paths when cost and preference match.
No matching route
A router with no matching prefix and no default cannot forward the packet normally.

Practice the guide skills

  1. Make two decisions in order: choose which candidates enter the table for each prefix, then use the destination for a longest-prefix lookup.
  2. Change one address across a /26 boundary, remove one route and compare both results with the offline forwarding lab.

Official objectives for this lesson

Lab · replay a forwarding-table lookup

The list below contains installed routes, so source preference has already been resolved. Use Python 3 to predict the egress for three packet destinations before running the lookup. Only the standard library is needed.

from ipaddress import ip_address, ip_network

routes = {
    ip_network("0.0.0.0/0"): "transit",
    ip_network("10.8.0.0/16"): "campus",
    ip_network("10.8.4.64/26"): "lab",
}

def lookup(address):
    matches = [prefix for prefix in routes if ip_address(address) in prefix]
    if not matches:
        return "no route"
    selected = max(matches, key=lambda prefix: prefix.prefixlen)
    return f"{selected} via {routes[selected]}"

for dest in ["10.8.4.77", "10.8.4.128", "192.0.2.9"]:
    print(dest, lookup(dest))
del routes[ip_network("10.8.4.64/26")]
print("after withdrawal:", lookup("10.8.4.77"))
Check the result
10.8.4.77 10.8.4.64/26 via lab
10.8.4.128 10.8.0.0/16 via campus
192.0.2.9 0.0.0.0/0 via transit
after withdrawal: 10.8.0.0/16 via campus

Add a 10.8.4.77/32 route toward diagnostics and repeat. Only the first destination changes, and its host route survives withdrawal of the /26. Remove the default route to verify how an unmatched destination is handled.

Related lesson · Read the most specific matching route before comparing metrics

Knowledge check

Select the longest matching route quiz

Answer 3 questions. Check each answer for an explanation.

Worked example · Select the longest matching route