Skip to content
Sign inGet all access
Chapter 6·8 minFree

The IP Layer

TTL as a hop counter, how traceroute works, and MTU.

IP is the envelope. It reaches a machine and guarantees nothing else.

The IP header

tcpdump -v prints it before the TCP line:

cap "-i lo -n -v -c 1 tcp port 9000" \
    "echo hi | nc -w 1 127.0.0.1 9000" | head -1
OutputIP (tos 0x0, ttl 64, id N, offset 0, flags [DF], proto TCP (6), length 60)
Field Here Means
ttl 64 hops remaining
id a number identifies fragments of one packet
flags [DF] do not fragment
proto TCP (6) what is inside
length 60 the whole packet, headers included

proto

One number says what the payload is. TCP is 6:

cap "-i lo -n -v -c 1 udp port 9500" \
    "echo hi | nc -u -w 1 127.0.0.1 9500" | head -1
OutputIP (tos 0x0, ttl 64, id N, offset 0, flags [DF], proto UDP (17), length 31)

UDP is 17, ICMP is 1. The same envelope carries all three, and the kernel reads this field to decide where to hand it.

cap "-i eth0 -n -v -c 1 icmp" "ping -c 1 -W 2 1.1.1.1" | head -1
OutputIP (tos 0x0, ttl 64, id N, offset 0, flags [DF], proto ICMP (1), length 84)

TTL

Time to live, measured in hops. Every router decrements it; a packet reaching zero is dropped.

terminal
ping -c 1 -W 2 1.1.1.1 | sed -n 2p | grep -oE 'ttl=[0-9]+'
Outputttl=63

It left with 64 and arrived with 63. The gateway, one hop closer, returns the full 64:

terminal
ping -c 1 -W 2 172.17.0.1 | sed -n 2p | grep -oE 'ttl=[0-9]+'
Outputttl=64

Counting back from 64 is how you estimate the distance to a host.

Watching it expire

Send a packet that can only travel one hop:

terminal
ping -c 1 -t 1 -W 2 1.1.1.1 | sed -n 2p
OutputFrom 172.17.0.1 icmp_seq=1 Time to live exceeded

172.17.0.1 is the first router, reporting the drop. Allow two hops:

terminal
ping -c 1 -t 2 -W 2 1.1.1.1 | sed -n 2p
Output64 bytes from 1.1.1.1: icmp_seq=1 ttl=63 time=N ms

The router did not have to tell you. ICMP “time exceeded” is a courtesy, and the next section depends on routers sending it.

How traceroute works

That loop, with the TTL incrementing. Each router that drops a packet names itself:

terminal
traceroute -m 3 -w 1 -q 1 1.1.1.1
Outputtraceroute to 1.1.1.1 (1.1.1.1), 3 hops max, 46 byte packets 1 172.17.0.1 (172.17.0.1) N ms 2 * 3 *

MTU

The largest packet a link carries. Ethernet is 1500, which is where chapter 4’s MSS of 1460 comes from: 1500 minus 20 of IP and 20 of TCP.

terminal
ip link show eth0 | grep -oE 'mtu [0-9]+'
Outputmtu 65535

Docker’s bridge is virtual, so it reports 65535 and nothing fragments here. On a real link it is 1500.

[DF] says “do not fragment, send an error instead”.

A router that cannot forward it replies with the size it can take, and the sender retries smaller. That is path MTU discovery.

IPv6, briefly

A longer address, a simpler header. What changes:

IPv4 IPv6
Address 172.17.0.4 2001:db8::1
Loopback 127.0.0.1 ::1
In a URL http://1.2.3.4:80 http://[2001:db8::1]:80
Fragmentation routers may senders only
NAT usual unnecessary

::1 is loopback, and it answers like any other address:

terminal
ping6 -c 1 -W 2 ::1 | sed -n 2p
Output64 bytes from ::1: icmp_seq=1 ttl=64 time=N ms

Ports, TCP, UDP and chapters 3 to 5 are identical. The brackets are what you will hit.