Sub topic 7
Longest-prefix match in a forwarding table
This chapter assumes you can convert between binary and decimal, read CIDR prefix notation such as 10.1.0.0/16, and know that a router moves packets between networks. It covers how a router chooses one route when several entries match a destination address.
Why more than one entry can match
A forwarding table maps destination prefixes to next hops. With CIDR, prefixes have different lengths, and a short prefix contains every longer prefix that falls inside its address range. An address inside 10.1.0.0/16 is also inside 10.0.0.0/8, so both entries match it.
The router resolves this with one rule: among all matching entries, use the one with the longest prefix length. The longest prefix is the most specific description of where the destination lives.
A prefix matches a destination when the first n bits of the destination equal the first n bits of the prefix, where n is the prefix length. Equivalently, destination AND mask == network address.
The algorithm
- For each entry, apply its mask to the destination address.
- Keep the entry if the result equals the entry's network address.
- From the kept entries, choose the one with the largest prefix length.
- Send the packet to that entry's next hop.
The default route 0.0.0.0/0 has length 0. Every address matches it, and it wins only when nothing more specific matches.
A worked example
Take this table:
| Prefix | Next hop |
|---|---|
| 10.0.0.0/8 | A |
| 10.1.0.0/16 | B |
| 0.0.0.0/0 | C |
- Destination
10.1.2.3matches all three entries. The longest is/16, so the packet goes to B. - Destination
10.2.0.1matches/8and/0. The longest is/8, so it goes to A. - Destination
8.8.8.8matches only/0, so it goes to C.
The following program runs this lookup, then merges two adjacent blocks into one aggregate:
import ipaddress
table = [("10.0.0.0/8", "A"), ("10.1.0.0/16", "B"), ("0.0.0.0/0", "C")]
def lookup(dst):
ip = ipaddress.ip_address(dst)
best = None
for prefix, hop in table:
net = ipaddress.ip_network(prefix)
if ip in net and (best is None or net.prefixlen > best[0].prefixlen):
best = (net, hop)
return best[1]
print(lookup("10.1.2.3"))
print(lookup("10.2.0.1"))
print(lookup("8.8.8.8"))
print(list(ipaddress.collapse_addresses([
ipaddress.ip_network("192.168.0.0/24"),
ipaddress.ip_network("192.168.1.0/24"),
])))It prints B, A, C, and then [IPv4Network('192.168.0.0/23')].
Working it by hand
For exam problems, compare bits instead of running code. Write the destination in binary and compare it with each prefix's leading bits.
Suppose the destination is 135.46.57.14 and the table has 135.46.56.0/22, 135.46.60.0/22 and 135.46.0.0/16.
- Third octet 57 is
00111001. The/22prefix covers the first 6 bits of that octet. - For
135.46.56.0/22, the third octet 56 is00111000. The first 6 bits are001110, and 57 also starts with001110. This entry matches. - For
135.46.60.0/22, 60 is00111100. Its first 6 bits are001111, which differ from001110. No match. - The
/16entry matches because the first two octets agree. - Two entries match,
/22and/16. The/22is longer and wins.
A quicker check for a /22: its block size in the third octet is 4, so 135.46.56.0/22 covers third octets 56 to 59.
Edge cases and common errors
- Length wins, not table order. The position of an entry in the table does not matter. A
/24listed last still beats a/8listed first. - Longest does not mean numerically largest. Compare prefix lengths, not address values.
- Equal length cannot both match. Two different prefixes of the same length cover disjoint ranges, so at most one of them contains a given address. Duplicate identical prefixes are a configuration error.
- No match and no default route. The router drops the packet and normally sends an ICMP destination-unreachable message.
- Aggregation can hide holes. If a router advertises
192.168.0.0/23but only owns half of it, a more specific route elsewhere for the other half still wins by longest-prefix match. This is how an organization that moves to a new provider keeps its addresses. - Mask must be contiguous. Longest-prefix match assumes masks of the form 1s followed by 0s, which prefix notation guarantees.
Implementation notes
Scanning every entry works for a small table but is too slow for a core router with hundreds of thousands of routes. Routers store prefixes in a binary trie, where each bit of the address chooses a left or right branch. The lookup walks down the trie and remembers the last node that held a route. When the walk ends, that remembered node is the longest match. The lookup cost depends on the address length (at most 32 steps), not on the number of routes. Hardware routers often use TCAM, which compares the address against all entries in parallel and returns the highest-priority match, with entries ordered by prefix length.
Longest-prefix match and the rest of forwarding
Forwarding is the per-packet action of looking up the table and sending the packet on. Longest-prefix match is the lookup step. Routing is the separate process, covered in later chapters, that builds the table. The same lookup rule applies whether the entries came from RIP, OSPF, BGP or static configuration.
Exercise
A router has these entries: 192.168.0.0/16 to R1, 192.168.16.0/20 to R2, 192.168.16.0/24 to R3, and 0.0.0.0/0 to R4. Which next hop is chosen for destinations 192.168.16.9, 192.168.20.5, 192.168.200.1 and 172.16.0.1?
Show answer
192.168.16.9 matches /16, /20, /24 and /0, so the /24 wins and the next hop is R3. 192.168.20.5 is outside 192.168.16.0/24 (third octet 20, not 16). The /20 covers third octets 16 to 31, so it matches along with /16 and /0, and the /20 wins: R2. 192.168.200.1 matches only /16 and /0, so R1. 172.16.0.1 matches only /0, so R4.
Exercise
A table holds 200.1.0.0/17 to X and 200.1.0.0/16 to Y. To which entry does a packet for 200.1.130.5 go, and why?
Show answer
It goes to Y. The /17 covers third octets 0 to 127, and 130 lies outside that range. Only the /16 matches, so the next hop is Y.