Sub topic 10
MTU, Fragmentation, and Path MTU
This chapter assumes you know what an IP address and subnet are, how Ethernet frames carry IP packets, and how a router picks a next hop and rewrites the Layer 2 header. It also assumes you can read terminal output. Here we look at what happens when a packet is too big for the next link.
What MTU means
The maximum transmission unit (MTU) is the largest IP packet, in bytes, that a link can carry in one frame. The MTU counts the IP header and payload. It does not count the Layer 2 header. Standard Ethernet has an MTU of 1500 bytes. The frame on the wire is larger, because it also carries the 14-byte Ethernet header and a 4-byte frame check sequence.
Different links have different MTUs. Some examples:
| Link type | Typical MTU (bytes) |
|---|---|
| Ethernet | 1500 |
| Ethernet with jumbo frames | about 9000 |
| PPPoE (DSL) | 1492 |
| IPv4-in-IPv4 or GRE tunnel | often 1476 to 1480 |
| IPsec or VPN tunnel | often 1400 to 1450 |
A router connects links, so it can receive a packet on a 9000-byte link and need to send it out on a 1500-byte link. The router cannot send a packet through an interface if the packet is larger than that interface's MTU. It has to do something else with the packet.
What a router can do with an oversized packet
When a packet is larger than the outgoing MTU, an IPv4 router has two options, and the Don't Fragment bit in the IPv4 header decides which one it takes.
- If the DF (Don't Fragment) bit is 0, the router may fragment the packet. It splits the packet into smaller pieces that each fit.
- If the DF bit is 1, the router must not fragment. It drops the packet and sends an ICMP error back to the source.
IPv6 works differently. IPv6 routers never fragment packets in transit. Only the sending host may fragment, and it does so using a Fragment extension header. A router that cannot forward an IPv6 packet because it is too big drops it and sends an ICMPv6 Packet Too Big message.
How IPv4 fragmentation works
IPv4 fragmentation uses three header fields.
- Identification is a 16-bit value the sender sets. All fragments of one original packet share it.
- Flags include DF and MF (More Fragments). MF is 1 on every fragment except the last.
- Fragment offset gives the position of the fragment's data in the original payload, in units of 8 bytes.
Because the offset is counted in 8-byte units, every fragment except the last must carry a payload that is a multiple of 8 bytes.
Take a 4000-byte IPv4 packet (20-byte header and 3980 bytes of data) sent onto a link with an MTU of 1500. Each fragment gets its own 20-byte header, so each can carry at most 1480 bytes of data. 1480 is divisible by 8, so it works.
| Fragment | Total length | Data bytes | MF | Offset (8-byte units) |
|---|---|---|---|---|
| 1 | 1500 | 1480 | 1 | 0 |
| 2 | 1500 | 1480 | 1 | 185 |
| 3 | 1040 | 1020 | 0 | 370 |
The offset of fragment 2 is 1480 / 8 = 185. The offset of fragment 3 is 2960 / 8 = 370. The data adds up: 1480 + 1480 + 1020 = 3980.
The following Python script computes this split for any packet size and MTU.
def fragment(total_length, mtu, header_len=20):
"""Return (data_bytes, offset_units, more_fragments) for each fragment."""
data = total_length - header_len
max_data = (mtu - header_len) // 8 * 8 # round down to a multiple of 8
if max_data <= 0:
raise ValueError("MTU too small for this header")
fragments = []
offset = 0
while offset < data:
size = min(max_data, data - offset)
more = (offset + size) < data
fragments.append((size, offset // 8, int(more)))
offset += size
return fragments
for frag in fragment(4000, 1500):
print(frag)
# (1480, 0, 1)
# (1480, 185, 1)
# (1020, 370, 0)Routers do not reassemble fragments. Only the destination host does, using the source address, destination address, protocol, and Identification value to group fragments. Later routers on the path can fragment the pieces again if they meet an even smaller MTU.
Why fragmentation is avoided
Fragmentation works, but it has real costs.
- Loss multiplies. If any one fragment is lost, the destination cannot rebuild the packet, and the sender must retransmit the whole thing. A 3-fragment packet is lost if any of three packets is lost.
- Extra work. The router must build new headers, and the destination must buffer and reassemble. This is slow on hardware designed to forward at line rate, and it is often handled on a slower path.
- Firewalls and load balancers struggle. Only the first fragment carries the TCP or UDP header with port numbers. A device that filters on ports cannot classify the other fragments. Many devices drop fragments or reassemble them first.
- Security issues. Overlapping or malformed fragments have been used to evade filters and to attack reassembly code.
For these reasons modern TCP stacks avoid fragmentation by setting the DF bit and adapting their packet size.
Path MTU discovery
The path MTU is the smallest MTU on any link between a source and a destination. A sender wants its packets to be no larger than that value. Path MTU Discovery (PMTUD) is the process of finding it.
PMTUD works like this:
- The sender sends packets with the DF bit set, sized to its own interface MTU.
- If a router on the path cannot forward a packet because the next link's MTU is smaller, it drops the packet. It sends back an ICMP message: Destination Unreachable, code 4, "Fragmentation Needed and DF Set" (ICMPv6 uses Packet Too Big). The message includes the MTU of the next link.
- The sender lowers its estimate for that destination, resends smaller packets, and repeats until packets get through.
TCP also negotiates a maximum segment size (MSS) in the SYN packets. The MSS is the largest amount of TCP data in one segment. For IPv4 on Ethernet it is usually 1460, which is 1500 minus 20 bytes of IP header and 20 bytes of TCP header.
The PMTUD black hole
PMTUD depends on ICMP messages getting back to the sender. Some firewalls block all ICMP. When they do, the sender never learns that its packets are too big. The symptom is distinctive: small packets work, large packets silently vanish.
A typical case: a TCP handshake succeeds, because SYN packets are tiny. A small request goes through. Then the connection stalls as soon as the server sends a full-size response. This is called a PMTUD black hole.
Common fixes:
- Allow the required ICMP messages through firewalls.
- Use MSS clamping on a router at the edge of a tunnel or PPPoE link, so the router rewrites the MSS in SYN packets to a value that fits the smaller MTU.
- Lower the MTU on the affected interface.
- Enable packetization layer PMTUD (RFC 4821), which probes using the transport protocol itself and does not depend on ICMP.
Inspecting and testing MTU on Linux
You can check interface MTUs and probe the path MTU with these commands.
# Show the MTU of each interface
ip link show
# Send 1472 bytes of ping data with DF set.
# 1472 + 8 (ICMP header) + 20 (IP header) = 1500 bytes total.
ping -M do -s 1472 -c 3 8.8.8.8
# Try a larger size; if the path MTU is 1500 this should fail
ping -M do -s 1473 -c 3 8.8.8.8
# Ask the kernel what path MTU it has cached for a destination
ip route get 8.8.8.8
# Lower the MTU of an interface (for example for a tunnel)
sudo ip link set dev eth0 mtu 1400
# Clamp the TCP MSS on forwarded traffic to the path MTU
sudo iptables -t mangle -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtuThe -M do option sets the DF bit. When a packet is too big, ping reports "Message too long" or "Frag needed and DF set (mtu = N)", and the N is the next-hop MTU from the ICMP message. Using ping with different -s values and binary search quickly finds the path MTU.
Where this fits in forwarding
In the per-packet steps from earlier chapters, the MTU check happens after the router has chosen the outgoing interface and before it writes the new Layer 2 frame. The router needs the outgoing interface first, because the MTU belongs to the link the packet is about to use. Decrementing the TTL still happens as usual, and fragmenting or dropping happens at this stage.
Exercise
A host sends a 1500-byte IPv4 packet with DF set toward a server. A router on the path has a next link with an MTU of 1400. What does the router do, and what must the host do next?
Show answer
The router drops the packet because it exceeds 1400 bytes and DF forbids fragmenting. It sends an ICMP Destination Unreachable, code 4 (Fragmentation Needed and DF Set), containing the next-hop MTU of 1400. The host must lower its path MTU for that destination to 1400 and resend using packets of at most 1400 bytes. If a firewall blocks the ICMP message, the host never learns this and the connection stalls on large packets.
Exercise
A 3020-byte IPv4 packet (20-byte header, 3000 bytes of data) with DF clear must cross a link with an MTU of 1000. How many fragments are produced, and what are each fragment's data size and offset?
Show answer
Each fragment can carry at most 980 bytes of data after its 20-byte header, and the largest multiple of 8 not above 980 is 976. The data splits into 976, 976, 976, and 72 bytes, so four fragments. Their offsets in 8-byte units are 0, 122, 244, and 366. MF is 1 on the first three and 0 on the last.