Sub topic 10
Computing fragment sizes, offsets and flags
This chapter assumes you know the IPv4 header layout (header length, total length, identification, flags, fragment offset), what an MTU is, and how to convert between bytes and bits. It covers the arithmetic that GATE questions test: how many fragments a datagram becomes, and what each fragment's total length, offset and More Fragments (MF) flag are.
The four rules
Fragmentation arithmetic follows four rules. Everything else is bookkeeping.
- The MTU limits the whole IP packet, header included. The data in one fragment is at most MTU minus the header length.
- The fragment offset field stores the position of the fragment's data in units of 8 bytes. It is 13 bits wide.
- Every fragment except the last must carry a multiple of 8 data bytes. The data size is therefore the largest multiple of 8 that is at most MTU minus header.
- MF is 1 on every fragment except the last. The last fragment has MF = 0.
The Total Length field of the original datagram includes its header. Subtract the header before you divide the data into fragments. Each fragment then gets its own copy of the header, usually 20 bytes.
Worked example: 4000 bytes over an MTU of 1500
A datagram has total length 4000 bytes and a 20-byte header. The link MTU is 1500.
- Data to send: 4000 − 20 = 3980 bytes.
- Maximum data per fragment: 1500 − 20 = 1480. Since 1480 / 8 = 185 exactly, 1480 is already a multiple of 8.
- Number of fragments: 3980 / 1480 = 2.69, so 3 fragments. The first two carry 1480 bytes each (2960 total). The last carries 3980 − 2960 = 1020 bytes.
- Offsets, in 8-byte units: 0, 1480 / 8 = 185, and 2960 / 8 = 370.
| Fragment | Data bytes | Total length | Offset field | MF |
|---|---|---|---|---|
| 1 | 1480 | 1500 | 0 | 1 |
| 2 | 1480 | 1500 | 185 | 1 |
| 3 | 1020 | 1040 | 370 | 0 |
The offsets are not byte positions. A byte position of 1480 is stored as 185.
The same arithmetic in code
The function below mirrors the steps above. It rounds the per-fragment data down to a multiple of 8 and records each fragment's total length, offset and MF flag.
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))It prints [(1500, 0, 1), (1500, 185, 1), (1040, 370, 0)], matching the table.
When the MTU is not a multiple of 8 plus the header
The round-down in rule 3 matters when MTU − header is not divisible by 8. Take an MTU of 576 and a 20-byte header. MTU − header = 556. The largest multiple of 8 not above 556 is 552, because 556 / 8 = 69.5. Each non-final fragment therefore carries 552 bytes, not 556. A common error is to use 556 and get an offset of 69.5, which cannot be stored.
print(fragment(1500, 576))This prints [(572, 0, 1), (572, 69, 1), (456, 138, 0)]. The data is 1480 bytes: 552 + 552 + 376. The offsets are 0, 552 / 8 = 69 and 1104 / 8 = 138.
Fragmenting a fragment
A router further along the path may have a smaller MTU. It fragments each incoming fragment again. Two things change:
- The new offsets are added to the offset of the fragment being split. If a fragment with offset 185 is split into pieces, the pieces start at 185 and continue from there.
- If the fragment being split already had MF = 1, every piece keeps MF = 1, including the last piece. Only the piece that holds the end of the original data can have MF = 0.
Reassembly happens only at the destination host. Routers do not merge fragments.
Other limits and edge cases
- The offset field is 13 bits, so the largest offset is 8191. Times 8, the data can start at byte 65,528 at most. This bounds the whole datagram to 65,535 bytes, the maximum Total Length.
- The Don't Fragment (DF) flag forbids fragmentation. A router that needs to fragment such a packet drops it and sends back an ICMP error. Path MTU discovery relies on this.
- Header options make the header longer than 20 bytes, up to 60. Use the header length from the question in the formulas. Some options are copied into every fragment and some only into the first, so read the question's assumptions.
- The identification field is the same in all fragments of one datagram. The destination groups fragments by source, destination, protocol and identification.
- If any one fragment is lost, the destination cannot rebuild the datagram. It discards all the fragments it holds after a reassembly timeout, and the sender's higher layer must resend the whole datagram.
Checking your answer
Run these checks on any result:
- The sum of the data bytes in all fragments equals the original total length minus the original header.
- Every non-final fragment's data size is divisible by 8.
- Each offset equals the sum of the earlier fragments' data bytes divided by 8.
- Only the last fragment has MF = 0 (for an original, unfragmented datagram).
Exercise
A datagram with a 20-byte header and total length 2820 bytes crosses a link with MTU 1000. Give the total length, offset field and MF flag of each fragment.
Show answer
Data = 2820 − 20 = 2800 bytes. MTU − header = 980, and 980 / 8 = 122.5, so each non-final fragment carries 976 bytes (122 × 8). Three fragments carry 976 + 976 = 1952 bytes, leaving 2800 − 1952 = 848 bytes for the last. Fragments (total length, offset, MF): (996, 0, 1), (996, 122, 1), (868, 244, 0).
Exercise
A fragment with total length 1500, offset 185 and MF = 1 reaches a link with MTU 620. The header is 20 bytes. Give the offset and MF flag of each resulting fragment.
Show answer
The fragment holds 1480 data bytes starting at offset 185. MTU − header = 600, which is a multiple of 8, so each piece carries 600 bytes. 1480 = 600 + 600 + 280. The offsets are 185, 185 + 75 = 260, and 260 + 75 = 335. MF is 1 on all three, because the original fragment had MF = 1. Total lengths are 620, 620 and 300.