3 / 6 · Basic
How packets find their way
Each router lowers a packet's TTL by one. When it reaches 0 the packet is thrown away, so a packet caught in a loop cannot circle forever.
Jump to the 10 practice questionsYou will learn
- Follow the TTL countdown and what traceroute does with it
- Pick the next hop by the longest matching prefix
- Explain where lost packets come from
Before this: IP addresses
1Explore
Try the simulator first
Try the simulator before reading anything else. Set TTL to 2 and press Send, then set it to 64 and send again. Watch where the packets stop the first time.
Drag to turn. Scroll or pinch to zoom.
2Explain
What you just saw: a countdown in every packet
With TTL 2, every packet died at the second router it met and a red message came back. With 64, the packets reached the server with 60 left after four routers. The IPv4 header has an 8-bit field called Time to Live (TTL), so its largest value is 255. RFC 791 says every router must lower it by at least one, and many computers start it at 64. When it reaches 0 the router drops the packet and sends an ICMP "Time Exceeded" message (RFC 792) back to the sender.
3Problem
TTL after four routers
A packet leaves with TTL 64 and passes 4 routers. What is its TTL when it arrives?
Hint
Each router takes off one.
Solution
Answer: 60
Each router lowers TTL by 1: 64 − 4 = 60.
4Problem
The largest TTL
TTL is an 8-bit field. What is the largest value it can hold?
Hint
An 8-bit field.
Solution
Answer: 255
Because 0 is one of the patterns, the largest value is one below the count: 2⁸ − 1 = 255. The number of patterns and the largest label are different.
5Problem
TTL reaches 0
What does a router do when a packet's TTL reaches 0?
Hint
The red message in the simulator.
Solution
Answer: Drops it and sends ICMP Time Exceeded to the sender
RFC 791 and RFC 792: the packet is discarded and an ICMP message reports it.
6Explain
Why a countdown
Why have a countdown at all? A mistake in the routing tables could send a packet round in a circle, and without a limit it would use links forever. The name says "time", and on paper RFC 791 measures it in seconds. But every router takes off at least one, so in practice it works as a count of hops. IPv6 gave the same field a plainer name, Hop Limit.
7Problem
Why TTL exists
Why does TTL exist?
Hint
A loop in the tables.
Solution
Answer: So a packet caught in a routing loop cannot circle forever
Without a countdown a looping packet would use links forever.
8Explain
How traceroute draws a map
The traceroute button uses this rule to draw a map. It sends a probe with TTL 1: the first router drops it and replies, which reveals that router's address. TTL 2 reveals the second router, and so on until the destination answers. Press it and read the list it builds. Then cut R1 to R3 and send again to see the packets take another way.
9Problem
A probe with TTL 1
traceroute sends a probe with TTL 1. Who answers?
Hint
The first router lowers it to 0.
Solution
Answer: The first router on the path
The first router lowers TTL to 0, drops the probe and replies with ICMP Time Exceeded.
10Explain
Each router looks up one line in a table
No router knows the whole route from your laptop to a server on another continent. Each one makes a single decision: for this destination address, which neighbour gets the packet next? The packet moves one hop, and the next router decides again. A route is a chain of these local choices.
11Explain
The longest prefix wins
The decision comes from a forwarding table, a list of prefixes, each with the neighbour to use. The router picks the entry that matches the destination with the longest prefix, because a longer prefix is a more exact match. Say the table sends 203.0.0.0/16 to neighbour B and 203.0.113.0/24 to neighbour A. A packet for 203.0.113.9 matches both, the /24 wins, and it goes to A. A packet for 203.0.7.1 matches only the /16, so it goes to B. Most tables also have a default entry for everything else; in a home router it points to the ISP.
| Packet for | Entries that match | Sent to |
|---|---|---|
| 203.0.113.9 | 203.0.113.0/24 (A), 203.0.0.0/16 (B), default (ISP) | A, longest (24 bits) |
| 203.0.7.1 | 203.0.0.0/16 (B), default (ISP) | B, longest (16 bits) |
| 8.8.8.8 | default 0.0.0.0/0 (ISP) | ISP |
12Problem
203.0.113.9
A table has 203.0.113.0/24 → A and 203.0.0.0/16 → B. Where does a packet for 203.0.113.9 go?
Hint
Both entries match; which prefix is longer?
Solution
Answer: A
Both match; the longest prefix (/24) wins.
13Problem
203.0.7.1
Same table. Where does a packet for 203.0.7.1 go?
Hint
Only the /16 matches.
Solution
Answer: B
203.0.7.1 does not start with 203.0.113, so only the /16 matches.
14Explain
Who writes the tables
Inside one company, routers share what they can reach using routing protocols and work out new paths when a link fails. Between the tens of thousands of separate networks that make up the internet, the Border Gateway Protocol (BGP, RFC 4271) carries announcements such as "I can reach 198.51.100.0/24". When a cable is cut, routes through it are withdrawn and traffic shifts to whatever path is left. In the simulator, that is what happened when you cut R1 to R3.
15Problem
Between networks
Which protocol carries route announcements between the separate networks of the internet?
Hint
Border Gateway Protocol.
Solution
Answer: BGP
The clue is announcements between separate networks: BGP-4, defined in RFC 4271, tells other networks which prefixes are reachable. This is routing information, not a web page transfer.
16Problem
Cut R5 to the server
In the simulator you cut R5 to the server. What happens?
Hint
How many links does the server have?
Solution
Answer: Nothing gets through: the server has only one link
Rerouting needs a second path. A single link is a single point of failure.
17Explain
Most lost packets fall out of a full queue
Routers also make packets wait. At busy moments packets arrive faster than an outgoing link can carry them, so each router keeps a queue in memory. When the queue is full, the next packet to arrive is simply dropped. Nobody is told; the sender can only work it out later. This is the most common way packets go missing on the internet, more common than a cut cable. A weak Wi-Fi signal can also spoil a frame so that it has to be thrown away. IP itself promises nothing.
18Explore
A 3,000,000-byte photo is 2055 packets
Links limit how big one packet may be. On Ethernet one frame carries at most 1500 bytes of payload (RFC 894). Take away 20 bytes of IPv4 header and 20 bytes of TCP header, and about 1460 bytes of your data fit in each packet. A 3,000,000-byte photo therefore needs 3,000,000 ÷ 1460 ≈ 2054.8, rounded up to 2055 packets, and each one is routed on its own.
Math.ceil rounds up: a part-filled last packet still counts as a packet.
const bytes = 3000000;
console.log(Math.ceil(bytes / 1460));205519Problem
Packets for 3,000,000 bytes
About 1460 bytes of data fit in one packet. How many packets does a 3,000,000-byte file need?
packets
Hint
3,000,000 ÷ 1460, rounded up.
Solution
Answer: 2055 packets
3,000,000 ÷ 1460 ≈ 2054.8, so round up to 2055. A partially filled final packet still counts; rounding down would leave some file data unsent.
20Practice
Longest prefix match
The table, num and matches are given. Finish nextHop(ip): among the entries that match the address, return the "to" of the one with the longest prefix.
A
B
ISPconst table = [
{ prefix: "203.0.113.0", len: 24, to: "A" },
{ prefix: "203.0.0.0", len: 16, to: "B" },
{ prefix: "0.0.0.0", len: 0, to: "ISP" },
];
const num = (ip) => ip.split(".").reduce((n, x) => n * 256 + Number(x), 0);
const matches = (ip, e) => Math.floor(num(ip) / 2 ** (32 - e.len)) === Math.floor(num(e.prefix) / 2 ** (32 - e.len));
function nextHop(ip) {
for (const e of table) {
// keep the matching entry with the largest len
}
return "ISP";
}
console.log(nextHop("203.0.113.9"));
console.log(nextHop("203.0.7.1"));
console.log(nextHop("8.8.8.8"));
Hint
Keep the best entry so far and replace it only by a longer match.
A solution
const table = [
{ prefix: "203.0.113.0", len: 24, to: "A" },
{ prefix: "203.0.0.0", len: 16, to: "B" },
{ prefix: "0.0.0.0", len: 0, to: "ISP" },
];
const num = (ip) => ip.split(".").reduce((n, x) => n * 256 + Number(x), 0);
const matches = (ip, e) => Math.floor(num(ip) / 2 ** (32 - e.len)) === Math.floor(num(e.prefix) / 2 ** (32 - e.len));
function nextHop(ip) {
let best = null;
for (const e of table) {
if (matches(ip, e) && (best === null || e.len > best.len)) best = e;
}
return best.to;
}
console.log(nextHop("203.0.113.9"));
console.log(nextHop("203.0.7.1"));
console.log(nextHop("8.8.8.8"));21Explain
Common mistakes
"Each router knows the whole path to the server."
Each router only picks the next hop from its forwarding table. The route is whatever chain those single choices make, and packets of the same message can take different routes.
"TTL is how many seconds a packet has left."
RFC 791 does describe it in seconds, but every router must take off at least one, so in practice it counts hops. IPv6 renamed it Hop Limit. The TTL on a DNS record is a different value with the same name, and that one really is in seconds.
"Most lost packets come from broken cables."
Most are dropped by busy routers whose queues are full, and a weak Wi-Fi signal spoils frames too. A cut cable is rarer, and the routers can often send traffic around it.
"IP makes sure every packet arrives."
RFC 791 says IP does not provide a reliable communication facility: no acknowledgements, no retransmissions. Recovery is done by the two end computers, usually with TCP.
22
Summary
- Routers decide one hop at a time using a forwarding table; the longest matching prefix wins.
- BGP spreads route announcements between networks and withdraws routes when links fail.
- TTL (IPv6: Hop Limit) drops by 1 per router; at 0 the packet is dropped and ICMP reports it.
- traceroute maps a path by sending TTL 1, 2, 3 and so on.