← Module 03 · Layer 3: Network

Routing Tables & Longest Prefix Match

A router's whole job comes down to one lookup, repeated for every packet: given this destination address, which entry in my table actually applies?

What's in a routing table entry

Every entry describes: a destination network (as an address and prefix, like 10.0.2.0/24), the next-hop address to forward toward, the outgoing interface, and a metric/administrative distance (covered in the next topic) used to break ties between competing sources of routes.

When more than one entry matches

Routing tables are built with prefixes of different lengths, and a single destination IP (Internet Protocol) address can legitimately match several entries at once: a broad 10.0.0.0/16 entry and a more specific 10.0.2.0/24 entry could both technically cover the same address. The router always picks the entry with the longest matching prefix, meaning the most specific one, regardless of which entry appears first in the table or which one has a "better" metric.

This is exactly how postal sorting works: a rule for "Springfield, Illinois" is more specific than a rule for "the state of Illinois," and a sorter routes mail using whichever rule matches most precisely, not whichever rule was written first.

Destination to reach: 10.0.2.55 0.0.0.0/0 → default route 10.0.0.0/16 → also matches, less specific 10.0.2.0/24 → longest prefix match, wins used /24 beats /16, and /16 beats the /0 default: the most specific match always wins, no exceptions.
Three entries all technically cover 10.0.2.55. Only the most specific one is ever actually used.

The default route

A 0.0.0.0/0 entry, the default route (sometimes called the "route of last resort"), matches literally every address with the least specificity possible: a prefix length of zero. It only gets used when nothing more specific matches anything at all, which is exactly what makes it the right entry for "everywhere else on the internet I don't have an explicit route for."

METRIC AND AD ONLY MATTER AFTER THIS

Longest prefix match is evaluated first, before administrative distance or metric ever come into play. Those only break ties between multiple routes to the exact same prefix. They never override a more specific match. That distinction is the whole subject of the next topic.