IPv4 Datagrams: Headers, TTL, MTU, and Fragmentation
The IPv4 Datagram Format
A network-layer packet is called a datagram. An IPv4 datagram consists of a header and data. The fixed part of the header is 20 bytes long and may be followed by variable-length options; the data is usually a TCP segment or a UDP datagram. The figure lays out the header 32 bits per row. Click a field to see what it does.
TTL: Each router decrements it by 1; at 0 the datagram is discarded.
| Field | Size | Purpose |
|---|---|---|
| Version | 4 bits | 4 for IPv4 |
| Header length | 4 bits | In 4-byte units; 5 (20 bytes) when there are no options |
| Type of service | 8 bits | 6-bit Differentiated Services (DSCP) plus 2-bit Explicit Congestion Notification (ECN) |
| Total length | 16 bits | Header plus data in bytes; at most 65,535 |
| Identification | 16 bits | Shared by every fragment of the same datagram |
| Flags | 3 bits | Reserved, DF (don't fragment), MF (more fragments) |
| Fragment offset | 13 bits | Position of the fragment's data within the original data, in 8-byte units |
| Time to live (TTL) | 8 bits | Decremented by each router; the datagram is discarded at 0 |
| Protocol | 8 bits | Which upper-layer protocol receives the data: 6 = TCP, 17 = UDP, 1 = ICMP |
| Header checksum | 16 bits | Covers the header only |
| Source and destination addresses | 32 bits each | IP addresses of the sender and receiver |
The protocol field plays a role similar to a transport-layer port number. Ports deliver data to the right process; the protocol field delivers a datagram's data to the right upper-layer protocol.
TTL and the Header Checksum
If routing ever forms a loop, a datagram with no safeguard would circle the network forever. The time-to-live (TTL) field prevents this. Each router decrements TTL by 1; when TTL reaches 0, the router discards the datagram and usually returns an ICMP time-exceeded message to the source (see Section 4.6).
The header checksum is computed the same way as the UDP checksum in Chapter 3: treat the header as a sequence of 16-bit words, wrap any overflow around while summing, and complement the result. Because TTL changes at every hop, every router must recompute the header checksum and write it back.
Here is the -th 16-bit word of the header (with the checksum field set to 0 while computing), fold wraps any carry beyond 16 bits back into the low 16 bits, and the right-hand equation is the receiver's check.
IP checks only the header; errors in the data are detected by upper-layer protocols such as TCP and UDP. Routers therefore process only about 20 header bytes, which keeps forwarding fast.
Follow one datagram's header across three routers
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
MTU and Fragmentation
The maximum amount of data a link-layer frame can carry is the maximum transmission unit (MTU). Ethernet's MTU, for example, is 1500 bytes. The links along an end-to-end path may use different link-layer protocols with different MTUs. When a datagram's total length exceeds the MTU of the next link, the router divides it into several smaller datagrams called fragments.
Each fragment carries its own IP header, filled in according to these rules:
- Every fragment copies the original identification field, so the receiver knows which fragments belong together.
- Every fragment except the last carries a multiple of 8 data bytes.
- The fragment offset is the starting byte of the fragment's data within the original data, divided by 8.
- Every fragment except the last has MF = 1.
For example, a datagram with total length 4000 bytes (20-byte header, 3980 data bytes) must cross a link with MTU 1500 bytes. Each fragment can carry at most 1480 data bytes, so the datagram becomes three fragments:
| Fragment | Total length | Data bytes | Offset | MF |
|---|---|---|---|---|
| 1 | 1500 | 1480 | 0 | 1 |
| 2 | 1500 | 1480 | 185 | 1 |
| 3 | 1040 | 1020 | 370 | 0 |
Fragments may take different paths, so only the destination host reassembles them; routers do not. If any fragment is lost, the destination eventually discards all fragments it has received, and the whole datagram must be retransmitted by an upper layer. Fragmentation also adds header overhead and consumes router resources. Modern hosts therefore usually set DF and perform path MTU discovery: a router that finds a datagram too large but with DF = 1 drops it and returns an ICMP "fragmentation needed" message, and the host reduces its datagram size. IPv6 goes further: routers never fragment at all (see Section 4.7).
Cut a datagram to fit a smaller MTU
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
Fragment a 5000-byte datagram
A host sends an IPv4 datagram with total length 5000 bytes, a 20-byte header, and identification 777. The next link has MTU 1500 bytes. List each fragment's total length, data bytes, fragment offset, and MF flag. How many bytes cross the link in total, and what happens if fragment 3 is lost?
Compute an IPv4 header checksum in Python
A captured IPv4 header (checksum field already zeroed) is:
4500 0073 0000 4000 4011 0000 c0a8 0001 c0a8 00c7
Write Python that treats it as ten 16-bit words, computes the Internet checksum, and verifies it. Print four lines: the raw sum, the folded sum, the checksum, and the verification sum. Expected checksum: 0xb861.