Sub topic 10
MTU, fragmentation, and path MTU
This chapter assumes you know IPv4 headers, TTL, and ICMP errors from the earlier chapters. It also assumes you can read ip and ping output on Linux.
What MTU means
The maximum transmission unit (MTU) is the largest IP packet, in bytes, that a link can carry in one frame. It counts the IP header and payload but not the Ethernet header. Standard Ethernet has an MTU of 1500 bytes.
A router receives a packet on one link and sends it out on another. The two links can have different MTUs. If the outgoing MTU is smaller than the packet, the router cannot just forward it. It has to do something else.
What a router does with an oversized packet
The IPv4 header has a Don't Fragment (DF) flag. The router's choice depends on that flag.
- DF = 0: The router may fragment the packet. It splits the payload into pieces that fit the outgoing MTU. Each piece gets its own IP header with the same Identification value. Each header also carries a fragment offset, and a More Fragments flag on every piece except the last. Only the destination host reassembles the pieces.
- DF = 1: The router drops the packet. It sends the source an ICMP Destination Unreachable message with code 4, "Fragmentation needed and DF set". Modern routers put the next-hop MTU in that message.
IPv6 routers never fragment. Only the sending host may fragment, and it uses an extension header to do so. A router that cannot fit a packet drops it and sends ICMPv6 "Packet Too Big".
A worked fragmentation example
Suppose a 4000-byte packet (20-byte header, 3980 bytes of payload) must cross a link with an MTU of 1500. Each fragment can carry at most 1480 bytes of payload, since 1500 minus the 20-byte header leaves 1480. That number is divisible by 8, which matters because the fragment offset is counted in 8-byte units.
def fragment(total_len, mtu, header=20):
payload = total_len - header
per_frag = (mtu - header) // 8 * 8
frags, offset = [], 0
while offset < payload:
size = min(per_frag, payload - offset)
more = offset + size < payload
frags.append((offset // 8, size, more))
offset += size
return frags
for off, size, more in fragment(4000, 1500):
print(f"offset={off} payload={size} MF={int(more)}")The output is three fragments:
- offset 0, 1480 bytes, MF=1
- offset 185, 1480 bytes, MF=1
- offset 370, 1020 bytes, MF=0
Fragmentation has costs. Losing any one fragment forces the sender to retransmit the whole packet. Firewalls often cannot match ports in non-first fragments. Routers also spend extra work splitting packets.
Path MTU discovery
The path MTU is the smallest MTU on any link between two hosts. Hosts avoid fragmentation by finding it with path MTU discovery (PMTUD):
- The sender sets DF on every packet and starts with its local link MTU.
- A router with a smaller next-hop MTU drops the packet and returns the ICMP message with its MTU.
- The sender lowers its estimate for that destination and resends smaller packets.
You can probe the path by hand on Linux:
# 1472 bytes of data + 8 ICMP + 20 IP = 1500 bytes, with DF set
ping -M do -s 1472 192.0.2.10
# Try a smaller size if that fails
ping -M do -s 1400 192.0.2.10
# Show the interface MTU
ip link show eth0Tunnels such as VPNs and VXLAN add header bytes, so their effective MTU is lower than 1500. A common fix is to clamp the TCP maximum segment size (MSS) on the tunnel interface, so TCP never builds packets that need fragmenting.
Exercise
A host sends a 1500-byte IPv4 packet with DF set toward a link with an MTU of 1400. What does the router do, and what does the sender then do?
Show answer
The router drops the packet and sends ICMP Destination Unreachable, code 4 (Fragmentation needed and DF set), normally including the next-hop MTU of 1400. The sender lowers its path MTU estimate for that destination to 1400 and retransmits using smaller packets.
Exercise
Why can a connection complete its TCP handshake but hang when transferring large data across a path whose firewall drops all ICMP?
Show answer
The handshake packets are small and fit every link. Large data packets with DF set exceed some link's MTU and are dropped. The ICMP error that would tell the sender to shrink them is also dropped, so the sender keeps resending packets that are too big.