Sub topic 8
Computing IPv4 Fragment Sizes, Offsets and Flags
Assume you know IPv4 header fields, binary arithmetic, and the meaning of an MTU (maximum transmission unit). An IPv4 datagram may be fragmented when its total size exceeds the outgoing link’s MTU.
Fragmentation rules
The IPv4 total length includes both the header and payload. With a normal 20-byte header:
- Maximum data per fragment is
MTU - header length. - Every fragment except the last must carry a data size divisible by 8 bytes.
- The fragment offset identifies where its data belongs in the original payload.
- The offset is measured in 8-byte units, not bytes.
- The MF (More Fragments) flag is 1 for every fragment except the last.
- The DF (Don’t Fragment) flag prevents fragmentation. If DF is 1 and the datagram is too large, the router discards it and reports an error.
For a fragment with data beginning at byte start, calculate:
fragment offset = start / 8
total length = header length + fragment data lengthWorked example
Suppose a 4000-byte datagram has a 20-byte header and crosses a link with MTU 1500.
The largest fragment data size is:
(1500 - 20) rounded down to a multiple of 8 = 1480 bytesThe original payload is 4000 - 20 = 3980 bytes. The fragments are:
| Fragment | Data bytes | Start byte | Total length | Offset | MF |
|---|---|---|---|---|---|
| 1 | 1480 | 0 | 1500 | 0 | 1 |
| 2 | 1480 | 1480 | 1500 | 185 | 1 |
| 3 | 1020 | 2960 | 1040 | 370 | 0 |
The second offset is 1480 / 8 = 185. The third is 2960 / 8 = 370. The last fragment may have a data size that is not divisible by 8.
def fragment(total_length, mtu, header=20):
data = total_length - header
per_fragment = (mtu - header) // 8 * 8
fragments = []
offset = 0
while data > 0:
size = min(per_fragment, data)
data -= size
more_fragments = 1 if data > 0 else 0
fragments.append((header + size, offset // 8, more_fragments))
offset += size
return fragments
print(fragment(4000, 1500))The output is:
[(1500, 0, 1), (1500, 185, 1), (1040, 370, 0)]Important edge cases
The Identification field has the same value in all fragments of one original datagram. The destination uses this value, the source address, and the protocol to group fragments for reassembly.
A fragment can itself be fragmented by a later router if DF is zero and the next MTU is smaller. Each resulting fragment receives a new offset relative to the original payload, while the Identification value remains associated with the original datagram.
Exercise
A 2500-byte IPv4 datagram with a 20-byte header crosses an MTU of 1000. What are the total lengths, offsets, and MF flags?
Show answer
The data capacity is 976 bytes, because 980 rounds down to 976. The fragments are (996, 0, 1), (996, 122, 1), and (548, 244, 0).