advanced21 min studyLesson 192 in Engineering

Forwarding chooses the most specific matching prefix

Apply longest-prefix matching to overlapping routes and explain why a more specific blackhole defeats an otherwise healthy aggregate.

On this page

What you’ll understand

  • Determine which installed route matches a destination most specifically.
  • Separate route selection for one prefix from forwarding across prefixes.
  • Diagnose how aggregates and discard routes interact.

A router can hold several routes whose address ranges contain the same destination. Forwarding does not simply use the cheapest route anywhere in that collection. It first identifies matching prefixes and chooses the longest one in the forwarding table. This rule lets a broad summary coexist with exceptions, but it also allows a mistaken specific route to capture traffic that would otherwise follow a healthy default. Work through the bits before changing a metric.

Matching is a relation between bits

A destination matches a prefix when its first p bits equal that prefix's p fixed bits. The default IPv4 route 0.0.0.0/0 fixes no bits and therefore matches every IPv4 destination. A /32 fixes all 32 bits and matches exactly one address. Between them, overlapping routes form a hierarchy. Prefix length measures specificity; the numerical value of an address and the order in which routes appear in a printed listing do not define the winner.

Suppose the installed table contains a default via A, 198.51.100.0/24 via B, and 198.51.100.128/25 via C. Destination 198.51.100.170 matches all three, so it follows C. Destination 198.51.100.70 matches the default and /24, so it follows B. This conclusion assumes ordinary destination-based unicast forwarding in the relevant table. Policy routing can first select another table or rule context; record that context before applying the same lookup reasoning.

r∗(d)∈arg max⁡r: d∈Prprr^*(d)\in\operatorname*{arg\,max}_{r:\ d\in P_r}p_r
d is the destination address, P_r the address set matched by installed route r, and p_r its prefix length in bits. This expresses longest-prefix matching within a selected forwarding table; equal-prefix next-hop choices are a separate decision.↔ Scroll wide equations horizontally.

Control-plane preference comes before this lookup

Routing protocols and local configuration can offer several candidate paths for the same prefix. Their selection rules determine which paths become usable forwarding entries, possibly including equal-cost next hops. Longest-prefix matching then operates across the installed prefixes for a packet's destination. A lower metric attached to a /16 does not override an installed matching /24 merely because its number is smaller. Comparing those metrics across specificity levels confuses two different stages.

A next hop may itself require recursive route resolution and a reachable adjacency. A control-plane route shown as selected is therefore insufficient evidence that a packet leaves the expected interface. Check the actual forwarding entry, its resolved next hop, and the neighbor state. Hardware and software forwarding views can also differ during updates. Diagnosing a reported route without checking its installation and resolution can miss the exact layer where forwarding stopped.

∣P/24∣=256,∣P/25∣=128,∣P/25∣∣P/24∣=12|P_{/24}|=256,\qquad |P_{/25}|=128,\qquad \frac{|P_{/25}|}{|P_{/24}|}=\frac12
For aligned IPv4 prefixes, a /25 child covers half the raw addresses of its /24 parent. Counts include every bit pattern; these are destination-set sizes, not counts of usable hosts or traffic volumes.↔ Scroll wide equations horizontally.

Summaries need a plan for their gaps

An aggregate advertisement claims reachability for its whole prefix, although the advertiser may have only some more-specific destinations active. A discard route for the aggregate can deliberately stop unmatched traffic from looping back toward a default route. The discard is safe only in the sense of preventing that forwarding loop; it does not make missing destinations reachable. An accidentally installed discard with a longer matching prefix will also win over a valid less-specific route, so inspect both successful and failing addresses.

The probability lab illustrates a testing hazard rather than performing route lookup. Interpret p as the fraction of independently, uniformly sampled destination addresses that fall in a failing child prefix, and n as the number of tests. A failed /25 inside a /24 gives p=0.5 under that synthetic sampling rule. Predict the zero-failure bar as n increases. Production traffic is rarely uniform or independent, so explicitly probe each advertised exception and aggregate gap instead of treating random success as proof of a complete route table.

Find a blackhole hidden by an aggregate

A forwarding table contains 0.0.0.0/0 via A, 203.0.113.0/24 via B, and a discard for 203.0.113.192/26. Where do packets to .190 and .200 go?

  1. A /26 covers 64 addresses. The 203.0.113.192/26 block spans .192 through .255, so .190 does not match it while .200 does.

  2. Destination .190 matches the /24 and default; the /24 wins and forwards toward B. Destination .200 matches all three entries; the /26 wins and discards the packet.

  3. Changing only the default route's metric cannot repair .200. Verify why the more-specific discard exists and what valid route or withdrawal should replace it before applying a scoped correction.

The result

The two nearby addresses take different forwarding actions because prefix specificity, rather than address proximity or the default metric, decides the match.

Explore the model

Interactive lab

Interactive lab · probability

Will your probes hit the failing child?

Set p to the assumed independent probability that a probe targets a failing child prefix, then vary n. Predict the zero-failure probability. The binomial bars model a sampling assumption, not a router's lookup algorithm or real traffic distribution.

Will your probes hit the failing child?These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Current readings: Mean · np: 5; Variance · np(1−p): 2.5; Total probability: 1. All parameters are adjustable with the labeled controls below.03581000.070.150.220.3Successes kP(X = k)k = 0 · P = 9.77e-4
Exact probabilityHighlighted outcomeMean np
Mean · np
5
Variance · np(1−p)
2.5
Total probability
1
t = 0 s

These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forTargeted tests of prefix boundaries reveal forwarding exceptions that a small random sample can miss.

Check your understanding

A matching /24 discard and a healthy /16 route are both installed in the same destination-based table. What happens?

Try it yourself

An installed table has 10.0.0.0/8 via A, 10.20.0.0/16 via B, and 10.20.16.0/20 via C. Determine next hops for 10.20.31.9 and 10.20.32.9.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

A /20 fixes the high four bits of the third octet. Find the aligned sixteen-value range beginning at 16.

Compare with the solution

The /20 spans third-octet values 16 through 31, so 10.20.31.9 matches it and uses C. Third octet 32 lies outside that range but inside 10.20.0.0/16, so 10.20.32.9 uses B. Both also match the /8, which is less specific. These answers assume the entries are actually installed and no earlier policy rule selects a different table.

Take it with you

  • Longest-prefix matching compares installed matching prefixes within a selected table.
  • Protocol preference for the same prefix is a separate control-plane decision.
  • A more-specific discard can defeat healthy aggregate and default routes.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.