Sub topic 11
Reassembly and loss of a single fragment
This chapter assumes you can compute fragment sizes, offsets and flags from the previous chapter, and that you know the IPv4 header fields Identification, Flags (DF and MF) and Fragment Offset.
Who reassembles, and where
Only the destination host reassembles a fragmented datagram. Routers on the path never put fragments back together. A router may split a fragment again if the next link has a smaller MTU, but it always forwards the pieces independently.
There are two reasons for this design.
- Fragments of one datagram can take different paths, so no single router is guaranteed to see all of them.
- A router that reassembled would need buffer memory and state per datagram, which is expensive at line rate.
How the destination matches fragments
The receiver groups fragments that share the same four values: source address, destination address, protocol, and Identification. The sender gives every fragment of one datagram the same Identification value. Fragments from different datagrams have different values and are kept apart.
For each group the receiver does the following:
- Allocates a reassembly buffer and starts a reassembly timer when the first fragment arrives. Arrival order does not matter.
- Places each fragment's data at byte position
offset × 8. - Learns the total payload length when the fragment with MF = 0 arrives. Its offset × 8 plus its data length is the end of the payload.
- Declares the datagram complete when the buffer has no gaps from byte 0 up to that end.
A datagram is complete only if two conditions hold. The last fragment (MF = 0) has arrived, and every byte before it is covered.
What happens when one fragment is lost
IPv4 has no mechanism to request one missing fragment. If any fragment is lost, the receiver cannot complete the datagram. When the reassembly timer expires, it discards all fragments it holds for that group. Common implementations use a timeout of about 30 to 60 seconds. The receiver may also send an ICMP "Time Exceeded, fragment reassembly time exceeded" message (type 11, code 1). It can only do this if it received fragment 0, because the ICMP message must quote the start of the original datagram.
Recovery is the job of a higher layer. TCP times out and resends the segment, and the sender fragments it again. UDP does nothing, so the application sees a lost datagram.
Detecting a gap in code
This function takes the fragment tuples (total length, offset, MF) that arrived and reports whether reassembly can finish. It reuses the fragments from the previous chapter's 4000-byte example.
def reassembly_status(fragments, header=20):
ordered = sorted(fragments, key=lambda f: f[1])
covered = 0
last_seen = False
for total_length, offset, more_fragments in ordered:
start = offset * 8
if start > covered:
return f"gap: bytes {covered} to {start - 1} missing"
covered = max(covered, start + total_length - header)
if more_fragments == 0:
last_seen = True
if not last_seen:
return "gap: last fragment missing"
return f"complete: {covered} payload bytes"
f1 = (1500, 0, 1)
f2 = (1500, 185, 1)
f3 = (1040, 370, 0)
print(reassembly_status([f3, f1, f2]))
print(reassembly_status([f1, f3]))
print(reassembly_status([f1, f2]))
print(reassembly_status([f2, f3]))The output is:
complete: 3980 payload bytes
gap: bytes 1480 to 2959 missing
gap: last fragment missing
gap: bytes 0 to 1479 missingThe first call passes the fragments out of order and still succeeds. This shows that only offsets matter, not arrival order.
Why fragmentation makes loss worse
Suppose each fragment is lost independently with probability p. A datagram split into n fragments survives only if all n arrive, so it is lost with probability 1 − (1 − p)^n. With p = 1%:
| Fragments (n) | Datagram loss probability |
|---|---|
| 1 | 1.00% |
| 3 | 2.97% |
| 10 | 9.56% |
The cost of a loss is also larger. One lost fragment forces the sender to retransmit the entire datagram, all 3980 payload bytes in our example, not just the missing 1480. This is why TCP picks a Maximum Segment Size (MSS) that avoids fragmentation, and why path MTU discovery sets DF = 1 so oversized packets are dropped rather than fragmented.
Edge cases
- Duplicates. A fragment may arrive twice. The receiver writes the same bytes to the same position, so nothing changes.
- Overlap. Fragments whose byte ranges overlap are a known attack technique (teardrop-style). Modern stacks drop the whole datagram.
- Identification reuse. If the sender reuses an Identification value while old fragments are still buffered, the receiver can mix data from two datagrams. The result fails the transport checksum, but only after reassembly.
- DF = 1. A router that cannot forward a DF = 1 packet without fragmenting drops it and sends ICMP "Fragmentation Needed". Such packets are never fragmented, so reassembly never applies to them.
- Refragmentation. A fragment that is fragmented again keeps its MF = 1 value for every piece except possibly the last. If the fragment being split already had MF = 0, only its last piece keeps MF = 0, and offsets stay relative to the original datagram.
Exercise
A datagram with a 20-byte header and 2980 bytes of data crosses a link with MTU 1000. Fragments are numbered 1, 2, 3, 4 in order. (a) Give (total length, offset, MF) for each fragment. (b) Fragment 3 is lost. Which byte range is missing at the receiver, and how many bytes must the sender retransmit if the upper layer resends the datagram? (c) If each fragment is lost independently with probability 0.02, what is the probability the datagram is lost?
Show answer
(a) Data per fragment is (1000 − 20) // 8 × 8 = 976 bytes. Data splits as 976, 976, 976 and 52, since 2980 − 2928 = 52. The fragments are (996, 0, 1), (996, 122, 1), (996, 244, 1) and (72, 366, 0). (b) Fragment 3 covers bytes 1952 to 2927, so that range is missing. The sender retransmits the full 2980 bytes of data, not only the lost 976. (c) 1 − 0.98^4 = 1 − 0.92237 = 0.0776, about 7.76%.
Exercise
A receiver holds fragments with (offset, MF) = (0, 1), (100, 1) and (200, 0). Each fragment carries 800 data bytes. Can it reassemble the datagram?
Show answer
No. The offsets are in 8-byte units, so the fragments start at bytes 0, 800 and 1600. Each fragment covers 800 bytes, so they cover bytes 0 to 799, 800 to 1599 and 1600 to 2399, with no gap. The last fragment has MF = 0, so the payload is 2400 bytes and reassembly succeeds. If the fragment at offset 100 were missing, bytes 800 to 1599 would be a gap and the datagram would be discarded when the timer expired.