How does a router use its routing table, and what is longest prefix match?
MediumConceptRouting Protocols
Answer
A routing table lists destination networks (prefixes) with the next hop and outgoing interface for each. For every packet, the router compares the destination IP address with the entries and applies longest prefix match: if several entries match, the one with the longest prefix, meaning the most specific network, wins.
- Routes are either static, typed in by an administrator (simple and predictable, but they do not adapt to failures), or dynamic, learned from routing protocols such as RIP, OSPF and BGP, which react to topology changes automatically.
- The default route
0.0.0.0/0matches every address, so it is used only when nothing more specific matches; home routers send all internet traffic this way. - Forwarding (looking up the table for each packet) happens in fast hardware, while routing (building the table) is done by protocols in the background.
Example: A router has these entries:
Prefix Next hop
10.0.0.0/8 R1
10.1.0.0/16 R2
10.1.2.0/24 R3
0.0.0.0/0 ISP
10.1.2.3 matches all four entries, and /24 is the longest, so it goes to R3. 10.1.9.9 matches /8, /16 and /0, so it goes to R2. 8.8.8.8 matches only the default route, so it goes to the ISP.