Routing: how packets find their way
No router knows the whole path. Each one looks up a single table and passes the packet one hop closer. Learn how routing tables work, why the most specific route wins, and how networks heal around failures.
In this lecture you will
- Read a routing table and explain each of its columns
- Apply the longest-prefix-match rule to choose a route
- Explain what TTL protects against and how traceroute uses it
- Distinguish static routes, interior routing protocols and BGP
In lecture 1, packets crossed the internet “hop by hop”. That phrase hides one of networking’s most elegant ideas: no router knows the full path. Each one makes a single local decision — which neighbor should get this packet next? — and trusts the next router to do the same.
The routing table
Every router, and in fact every computer, keeps a routing table: a list of destination networks and where to send traffic for each. A typical entry has:
- Destination
- A network prefix such as 172.20.8.0/24. The special prefix 0.0.0.0/0 matches every address and is called the default route.
- Next hop
- The neighboring router to hand the packet to — or “directly connected” when the destination network is attached to this router.
- Interface
- Which port or link to send the packet out of.
- Metric
- A cost used to choose between routes to the same destination (for example, fewer hops or faster links).
You can see your own machine’s table with ip route on Linux, netstat -rn on macOS or route print on Windows. On a laptop it is usually short: the local subnet, and a default route pointing at the router.
Longest prefix wins
Often several entries match the same destination. A packet for 172.20.8.10 matches 172.20.8.0/24, 172.20.0.0/16 and the default route 0.0.0.0/0. The rule is simple: use the most specific match — the longest prefix. A /24 fixes 24 bits of the address, so it beats a /16, which beats the default /0.
This rule lets networks describe the world compactly. A router can say “everything in 10.0.0.0/8 goes left” and then add a single exception for one subnet, without listing millions of addresses.
Follow a packet across four routers
Step through the model. Watch the table of the router currently holding the packet, which rows match, which one wins, and how the TTL falls at each hop. In the last step a link fails and traffic takes another path.
- The laptop sends to its gatewayThe destination 172.20.8.10 is not on the laptop’s own subnet (10.1.0.0/16), so the only matching route is the default: send it to R1, the default gateway.
- R1 looks up the destinationThree of R1’s routes match 172.20.8.10. The /24 route via R2 is the most specific, so it wins over the /16 route and the default route.
- Forward to R2, lower the TTLR1 sends the packet out of eth1 to R2 and reduces the Time To Live from 64 to 63. The IP addresses in the packet do not change.
- R2 makes its own decisionR2 knows nothing about R1’s choice. It looks up its own table, finds 172.20.8.0/24 via R4, and forwards again. TTL is now 62.
- R4 delivers locallyR4 is directly connected to 172.20.8.0/24, so it delivers the packet on that network to the server, using the server’s hardware (MAC) address.
- A link fails — the network healsThe R1–R2 link goes down. The routing protocol withdraws the /24 route through R2, so R1’s best match becomes 172.20.0.0/16 via R3. Traffic flows around the failure.
TTL: protection against loops
Misconfigurations happen: router A sends to B, B sends back to A. Without protection, a packet would circle forever, consuming bandwidth. The Time To Live field prevents that. Every router lowers it by one; at zero, the packet is discarded, and the router usually sends an ICMP Time Exceeded message back to the source.
traceroute turns this into a diagnostic tool. It sends packets with TTL 1, then 2, then 3, and so on. Each router along the path discards the packet when its TTL runs out and reports back — revealing itself, hop by hop.
- The pathA request from Bengaluru to a server in New York crosses several networks: an ISP backbone in Mumbai, an undersea cable landing in Singapore and a trans-Pacific cable to Los Angeles.
- TTL 1: the first routerThe first probe has a TTL of 1. The home router lowers it to 0, discards the packet and reports back with an ICMP Time Exceeded message: 2 ms away.
- TTL 2 and 3The next probes die at the Mumbai backbone (21 ms) and in Singapore (64 ms). Each report reveals one more hop and its round-trip time.
- Across the PacificLos Angeles answers after 228 ms. Crossing an ocean adds more than 160 ms: light in fiber covers about 200 km per millisecond, and the cable is long.
- The destinationThe server itself answers after 291 ms. No software upgrade can beat the physics of distance — which is why content delivery networks put copies of content near users.
Where routes come from
Routers learn routes in three ways:
- Directly connected — networks attached to the router’s own interfaces.
- Static routes — entered by an administrator. Simple and predictable, but they don’t react to failures.
- Dynamic routing protocols — routers tell each other what they can reach and update automatically when links change. OSPF and IS-IS are common inside an organization. BGP connects the tens of thousands of independent networks — autonomous systems — that make up the internet.
OSPF and IS-IS are link-state protocols: every router floods information about its own links to all the others, so they all share the same map, and each one calculates the cheapest paths from its own position with Dijkstra’s algorithm. Cut some links and watch the routes adapt.
The cheapest route from A to B goes via C, because 5 + 3 is less than 10. Cut the D–F link and traffic to F reroutes through C and E at a higher cost; cut both of A’s links and everything becomes unreachable.
Because BGP decides how traffic flows between networks, a single mistaken announcement can send traffic the wrong way or make large services unreachable. That is why internet-scale outages are so often traced back to routing changes.
Routing gets packets to the right machine. In the final lecture of this module, we look at how TCP and UDP get the data to the right program — and what happens when packets go missing.
Key takeaways
- A router forwards each packet independently, using only the destination address and its routing table.
- When several routes match, the most specific one — the longest prefix — wins; 0.0.0.0/0 is the catch-all default.
- TTL is lowered at every hop so looping packets eventually die; traceroute exploits this to reveal the path.
- Routing protocols such as OSPF and BGP keep tables up to date, which is how networks route around failures.
Check your understanding
4 questions · answers explainedQ1A router has routes for 10.0.0.0/8, 10.4.0.0/16 and 0.0.0.0/0. Which one is used for a packet to 10.4.9.1?
Q2What does a router do when it lowers a packet’s TTL to zero?
Q3Which protocol exchanges routes between independently run networks, such as ISPs and cloud providers?
Q4In a cloud virtual network, a subnet’s route table sends 0.0.0.0/0 to a NAT gateway. What does that achieve?