Sub topic 8
The IPv4 header fields that matter
This chapter assumes you can convert between binary and decimal, and that you know a packet, a router and a host are. Later chapters on fragmentation, NAT and routing all read or rewrite a few header fields, so this chapter covers those fields.
Header layout
An IPv4 header is 20 bytes long when it has no options. It is built from 32-bit words.
| Field | Size (bits) | Purpose |
|---|---|---|
| Version | 4 | Always 4 |
| IHL | 4 | Header length in 32-bit words (5 to 15) |
| DSCP/ECN | 8 | Traffic class and congestion marking |
| Total Length | 16 | Header plus data, in bytes |
| Identification | 16 | Groups the fragments of one datagram |
| Flags | 3 | Reserved, DF, MF |
| Fragment Offset | 13 | Position of the data, in 8-byte units |
| TTL | 8 | Remaining hops |
| Protocol | 8 | Upper-layer protocol (6 TCP, 17 UDP, 1 ICMP) |
| Header Checksum | 16 | Detects errors in the header only |
| Source Address | 32 | Sender |
| Destination Address | 32 | Receiver |
| Options and padding | 0 to 320 | Rarely used |
Adding the fixed fields gives 160 bits, which is 20 bytes.
Fields that set sizes
IHL counts 4-byte words, so a value of 5 means 20 bytes. The largest value, 15, gives 60 bytes, so options take at most 40 bytes. To convert IHL to bytes, multiply by 4.
Total Length covers the header and the data. Its maximum is 65535 bytes, so the largest data payload with a 20-byte header is 65515 bytes. The data size is always Total Length minus IHL × 4.
Fields used for fragmentation
The next chapters rely on these three fields.
- Identification is the same in every fragment of one original datagram. The destination uses it, together with source, destination and protocol, to group fragments.
- Flags has three bits. The first is reserved. The second is DF (Don't Fragment). The third is MF (More Fragments), which is 1 on every fragment except the last.
- Fragment Offset is the position of this fragment's data in the original data. It counts 8-byte units, so a stored value of 185 means byte 1480.
Because the offset field is only 13 bits wide, it can only express multiples of 8 bytes. For that reason every fragment except the last must carry a multiple of 8 data bytes.
Fields that change at every hop
TTL (Time To Live) prevents a packet from circling forever during a routing loop. Each router decrements it by 1. A router that makes it 0 discards the packet and sends an ICMP Time Exceeded message to the source. The traceroute tool uses this by sending packets with TTL 1, 2, 3 and so on.
Header Checksum is a 16-bit ones'-complement sum of the header's 16-bit words. It protects only the header, not the data. Because TTL changes at each hop, every router recomputes the checksum. A receiver that sums the whole header, checksum included, gets zero (in ones'-complement terms, 0xFFFF).
Source and Destination Address stay constant end to end, except where a NAT device rewrites them. A NAT device also has to fix the checksum.
Building and parsing a header in Python
The following program builds a header, checks the checksum and decodes the fields.
import socket
import struct
FORMAT = "!BBHHHBBH4s4s"
def checksum(data):
total = 0
for i in range(0, len(data), 2):
total += (data[i] << 8) | data[i + 1]
while total >> 16:
total = (total & 0xFFFF) + (total >> 16)
return ~total & 0xFFFF
def build_header(total_length, ident, mf, offset, ttl, proto, src, dst):
ver_ihl = (4 << 4) | 5
flags_offset = (mf << 13) | offset
raw = struct.pack(FORMAT, ver_ihl, 0, total_length, ident,
flags_offset, ttl, proto, 0,
socket.inet_aton(src), socket.inet_aton(dst))
return raw[:10] + struct.pack("!H", checksum(raw)) + raw[12:]
def parse_header(h):
ver_ihl, _, total, ident, fo, ttl, proto, _, src, dst = struct.unpack(FORMAT, h[:20])
return {
"version": ver_ihl >> 4,
"header_bytes": (ver_ihl & 0xF) * 4,
"total_length": total,
"id": ident,
"DF": (fo >> 14) & 1,
"MF": (fo >> 13) & 1,
"offset_bytes": (fo & 0x1FFF) * 8,
"ttl": ttl,
"protocol": proto,
"src": socket.inet_ntoa(src),
"dst": socket.inet_ntoa(dst),
}
header = build_header(1500, 0x1234, 1, 185, 64, 6, "192.168.1.10", "10.1.2.3")
print(parse_header(header))
print("checksum over full header:", hex(checksum(header)))The final line prints 0x0, which shows the checksum is valid. The parsed offset is 1480 bytes, since the stored value 185 is multiplied by 8.
Common errors
- Treating IHL as a byte count. It counts words, so multiply by 4.
- Forgetting that Total Length includes the header. Subtract the header before computing data sizes.
- Writing a byte offset in the Fragment Offset field. Divide by 8 first.
- Setting MF on the last fragment. MF is 0 there, but the last fragment still has a non-zero offset.
- Assuming the checksum covers the data. Integrity of the payload is left to TCP, UDP or the link layer.
Exercise
A datagram has IHL = 8 and Total Length = 1000. How many bytes are the header, the options and the data? A later fragment of it shows Fragment Offset = 125. At which byte of the original data does that fragment's data start?
Show answer
The header is 8 × 4 = 32 bytes. The options take 32 − 20 = 12 bytes. The data is 1000 − 32 = 968 bytes. An offset of 125 means 125 × 8 = 1000, so the fragment's data starts at byte 1000 of the original data.
Exercise
Why must a router recompute the header checksum even when it does not fragment the datagram?
Show answer
The router decrements TTL, which changes the header, so the old checksum no longer matches the header contents.