Routing tables and longest prefix match
A router decides where every packet goes by reading its routing table: a list of destination prefixes, each with a port and a next hop. Watch four packets arrive at one router. For each, several rows match, and the router always picks the most specific one: the longest prefix. 0.0.0.0/0, the default route, catches everything else.
5 quick questions
Pick an answer to see why. Your score saves with your progress.
01A router's table holds 0.0.0.0/0, 10.0.0.0/8, 10.1.0.0/16 and 10.1.2.0/24. Which row does a packet for 10.1.2.200 use?
All four rows match 10.1.2.200. The router picks the longest prefix, /24, because it is the most specific.
02Same table. Which row does a packet for 10.7.0.1 use?
10.7.0.1 starts with 10, so 10.0.0.0/8 matches. Its second number is 7, not 1, so the /16 and /24 rows do not. The longest match is /8.
03What does the /16 in 10.1.0.0/16 mean?
The prefix length counts the leading bits that must match. /16 fixes 10.1 and leaves the last 16 bits free: 65,536 addresses.
04Why does 0.0.0.0/0 match every destination?
With a prefix length of 0 the mask is 0.0.0.0, so any address AND the mask gives 0.0.0.0. It matches everything, but it is also the least specific, so it only wins when nothing else matches.
05A static route and an OSPF route both offer 10.0.0.0/8. Which one goes in the table (Cisco defaults)?
Administrative distance only compares routes for the same prefix; lower is more trusted. Longest prefix match then works on the rows that made it into the table.
Glossary
- Routing table
- A router's list of destinations (prefixes), each with where to send matching packets: a port and, if needed, a next-hop router.
- Prefix
- A network written as an address and a length, like 10.1.0.0/16: the first 16 bits are fixed, the rest are free.
- Longest prefix match
- When several rows match a destination, the router uses the one with the longest prefix, because it is the most specific.
- Default route
- The row 0.0.0.0/0. It matches every address, so it catches packets that no more specific row fits.
- Next hop
- The neighbouring router a packet is handed to. A connected row has none: the destination is on that port's own network.
- Port (interface)
- A socket on the router where a network cable plugs in. Each row says which port a packet leaves by.
- Administrative distance
- A trust score for where a route came from (Cisco defaults: connected 0, static 1, OSPF 110). Lower wins, for the same prefix only.
- OSPF
- Open Shortest Path First, a routing protocol routers use to teach each other routes inside one organisation.
Notes and sources
- The networks inside the organisation use private addresses (RFC 1918). The ISP link and the far host use documentation addresses from RFC 5737; real examples often use 8.8.8.8, which is a real public server.
- A real table also lists the three small /30 link networks to routers B and C and to the ISP as connected rows. The figure leaves them off the card; the lookup still checks them, and none of the four packets matches them.
- Administrative distance numbers are vendor defaults (Cisco: connected 0, static 1, OSPF 110). They only choose between routes for the same prefix; a longer prefix always beats a shorter one.
- An office LAN with 192.168.x.x addresses also needs NAT on the way to the internet, which rewrites the source address. That is a separate lesson.
- RFC 1812: Requirements for IP Version 4 Routers (§5.2.4.3 longest match, §5.3.1 TTL)
- RFC 4632: Classless Inter-domain Routing (CIDR)
- RFC 791: Internet Protocol (IPv4)
- RFC 2328: OSPF Version 2
- RFC 1918: Address Allocation for Private Internets
- RFC 5737: IPv4 Address Blocks Reserved for Documentation
- Cisco: What Is Administrative Distance?
Quiz Unlocks after the last step.