Octet

Walk me through the TCP header. What does each field do?

Show the answer

Say this first

The TCP header is 20 bytes without options and up to 60 with them. It carries source and destination ports, a 32-bit sequence number, a 32-bit acknowledgment number, the data offset (header length in 4-byte words), the flags, a 16-bit receive window, a checksum, the urgent pointer, and then options such as MSS, window scale, SACK and timestamps.

Go deeper

Fields in order

FieldBitsWhat it does
Source port16Sender's port, usually ephemeral on the client
Destination port16Receiver's port, for example 443
Sequence number32Byte number of the first data byte in this segment (or the ISN in a SYN)
Acknowledgment number32Next byte the sender of this segment expects to receive, valid when ACK is set
Data offset4Header length in 32-bit words, 5 to 15, so 20 to 60 bytes
Reserved4Must be zero
Flags8CWR, ECE, URG, ACK, PSH, RST, SYN, FIN
Window16Receive window in bytes, shifted left by the window scale factor if one was agreed
Checksum16Covers a pseudo-header (source IP, destination IP, protocol, TCP length), the header and the data
Urgent pointer16Only meaningful with URG, almost never used
Options0 to 320Up to 40 bytes

Options worth knowing by number

KindOptionWhere
0End of option listPadding
1NOPAlignment padding
2MSS (4 bytes)SYN and SYN-ACK only
3Window scale (3 bytes)SYN and SYN-ACK only
4SACK permitted (2 bytes)SYN and SYN-ACK only
5SACK blocks (variable)Any segment after the handshake
8Timestamps (10 bytes)SYN, then every segment if both agreed

TCP has no length field of its own: the segment length is the IP total length minus the IP header minus the TCP data offset.

Traps

  • Calling the window field "the congestion window". It is the receive window. The congestion window is never sent on the wire.
  • Forgetting that the header length field counts 4-byte words, so a value of 5 means 20 bytes.
How did you do?Book 5.2 Reliable Byte Stream (TCP)