Sub topic 9
MTU and why IPv4 fragments packets
This chapter assumes you know what a packet, a router and a host are, and that you can convert between binary and decimal. It also assumes you know the four-layer model, where the network layer sits above the link layer. The next chapter covers the full offset and flag calculation. This one explains why fragmentation exists and how it behaves.
What the MTU is
The maximum transmission unit (MTU) is the largest IP datagram, header included, that a link can carry in one frame. The link layer sets this limit, not IP. Ethernet allows 1500 bytes of payload per frame, so its MTU is 1500. Other links differ, and some older or tunneled links have much smaller values.
A datagram can be as large as 65,535 bytes, because the Total Length field is 16 bits. A path often crosses links with different MTUs, so a datagram that fits on the first link may not fit on a later one.
Why IPv4 fragments
IPv4 does not require the sender to know the smallest MTU along the path. Instead, a router that must forward a datagram onto a link with a smaller MTU splits it into fragments. Each fragment is an independent datagram with its own IP header.
Three rules define the behavior:
- Any router on the path may fragment, not only the sender.
- Only the destination host reassembles. Routers never merge fragments.
- Fragments can themselves be fragmented again if they meet a link with an even smaller MTU.
Reassembly happens only at the destination because fragments may take different routes. An intermediate router cannot be sure it sees all of them.
Header fields that make it work
| Field | Size | Role in fragmentation |
|---|---|---|
| Identification | 16 bits | Same value in every fragment of one original datagram. |
| DF flag | 1 bit | Don't Fragment. If set, a router must not fragment the datagram. |
| MF flag | 1 bit | More Fragments. 1 on every fragment except the last. |
| Fragment Offset | 13 bits | Position of this fragment's data in the original, counted in 8-byte units. |
| Total Length | 16 bits | Header plus data of this fragment, in bytes. |
The offset field is only 13 bits, so it cannot hold a byte offset up to 65,535. Counting in 8-byte units fixes that, because 2^13 × 8 = 65,536. As a result, every fragment except the last must carry a data size that is a multiple of 8 bytes.
The basic rule
For a router sending onto a link with MTU M, with an IP header of H bytes (20 if there are no options), the largest data size per fragment is:
floor((M - H) / 8) * 8Take a 4000-byte datagram with a 20-byte header and an MTU of 1500. It holds 3980 data bytes. Each fragment can carry floor(1480 / 8) × 8 = 1480 data bytes. That gives data sizes of 1480, 1480 and 1020, so total lengths are 1500, 1500 and 1040.
def fragment_count(total_length, mtu, header=20):
if total_length <= mtu:
return 1
per_fragment = (mtu - header) // 8 * 8
data = total_length - header
return -(-data // per_fragment) # ceiling division
print(fragment_count(4000, 1500))
print(fragment_count(1500, 1500))
print(fragment_count(1500, 576))This prints 3, 1 and 3. The last call works out as follows: data is 1480, each fragment carries 552 bytes, and ceil(1480 / 552) is 3.
What happens at the router
The DF flag and path MTU discovery
When DF is 1 and the datagram is too big, the router drops it. It then sends an ICMP "Destination Unreachable, fragmentation needed" message (type 3, code 4) back to the sender, and this message includes the next-hop MTU. The sender can lower its datagram size and retry. This process is called path MTU discovery, and modern TCP implementations rely on it.
Fragmentation has real costs, which is why hosts try to avoid it:
- Router work. Splitting packets takes processing time at the router.
- Loss amplification. Losing one fragment makes the destination discard the whole datagram, so the sender must retransmit all of it.
- Firewall trouble. Only the first fragment carries the transport header, so devices that filter on ports cannot classify the other fragments.
Common errors
- Forgetting the header per fragment. Each fragment repeats the 20-byte header. The total of all fragment lengths therefore exceeds the original length by 20 bytes for each extra fragment.
- Using bytes for the offset. The field holds the byte offset divided by 8. A byte offset of 1480 is written as 185.
- Using a data size that is not a multiple of 8. With an MTU of 1500, 1480 is already a multiple of 8. With an MTU of 1000, the data per fragment is floor(980 / 8) × 8 = 976, not 980.
- Setting MF to 0 on a middle fragment. MF is 1 on every fragment except the last. Fragmenting a fragment keeps MF = 1 on the last piece of a non-final fragment.
- Assuming the MTU is the data size. The MTU includes the IP header.
Exercise
A datagram has a total length of 1400 bytes and a 20-byte header. It must cross a link with MTU 576. How many fragments are produced, and what are their total lengths and offsets?
Show answer
The data is 1400 - 20 = 1380 bytes. Each fragment may carry floor((576 - 20) / 8) × 8 = 552 data bytes. The fragments carry 552, 552 and 276 data bytes, so there are 3 fragments with total lengths 572, 572 and 296. The offsets are 0, 552/8 = 69 and 1104/8 = 138. MF is 1, 1 and 0.
Exercise
A router receives a 3000-byte datagram with DF = 1 and must forward it onto a link with MTU 1500. What does the router do?
Show answer
It does not fragment. It drops the datagram and returns an ICMP "fragmentation needed" message (type 3, code 4) to the sender, including the MTU of 1500. The sender can then send smaller datagrams.