Detailed Explanation of TCP Segment Structure

Detailed Explanation of TCP Segment Structure

A TCP segment is the fundamental unit of data transmission in the TCP protocol. Understanding its structure is essential for grasping how TCP works. A TCP segment is divided into two main parts: the Header and the Data. The data part is optional; for example, segments in the three-way handshake for connection establishment carry no data. However, the header is always present.

The fixed portion of the TCP header is 20 bytes. In addition, there is an optional Options section, making the header length variable—minimum 20 bytes, maximum 60 bytes. Its specific structure is typically represented with each row being 4 bytes (32 bits), as shown in the diagram below:

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Source Port          |       Destination Port        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                        Sequence Number                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Acknowledgment Number                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Data  |           |U|A|P|R|S|F|                               |
| Offset | Reserved  |R|C|S|S|Y|I|            Window             |
|        |           |G|K|H|T|N|N|                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|           Checksum            |         Urgent Pointer        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options (variable length, optional)         |
|                                               +-+-+-+-+-+-+-+-+
|                                               |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Let's break down the meaning and function of each field one by one.


Step 1: The Endpoints of the Connection – Port Numbers

  1. Source Port (16 bits):

    • Function: Identifies the sending application process.
    • Details: Think of it as the "door" from which the data packet exits. Typically, client programs use randomly assigned ephemeral ports.
  2. Destination Port (16 bits):

    • Function: Identifies the receiving application process.
    • Details: Think of it as the "door" the data packet is intended to enter. Well-known server services use fixed ports, e.g., HTTP uses port 80, HTTPS uses port 443.
    • Combination: The four-tuple—Source IP + Source Port + Destination IP + Destination Port—uniquely identifies a TCP connection.

Step 2: Ensuring Data Order – Sequence and Acknowledgment Numbers

These are the core fields for TCP's reliable delivery.

  1. Sequence Number (32 bits):

    • Function: Identifies the sequence number of the first data byte in this segment.
    • Details: TCP is byte-oriented and assigns a sequence number to each byte. For example, assuming an Initial Sequence Number (ISN) of 1000, if this segment carries 1000 bytes of data, its sequence number is 1000, and the next segment's sequence number would be 1000 + 1000 = 2000.
    • Significance: The receiver uses sequence numbers to reorder out-of-order packets and to detect duplicate packets.
  2. Acknowledgment Number (32 bits):

    • Function: The sequence number of the next byte the sender of this segment expects to receive. It also cumulatively acknowledges all data up to (acknowledgment number - 1).
    • Details: Acknowledgments are cumulative. If the last byte received has sequence number 1999, the acknowledgment number sent back will be 2000, meaning "I have correctly received all data before 2000; I now expect you to start sending from 2000."
    • Prerequisite: This field is only valid when the ACK flag bit (see below) in the segment is set to 1.

Step 3: Controlling the Segment Itself – Header Length, Flags, and Window

These fields control the segment's behavior and flow.

  1. Data Offset (4 bits):

    • Function: Specifies the length of the TCP header, indicating where the data portion begins.
    • Details: The unit for this field is 4-byte words (32-bit words). Because the header has a variable-length Options section, this field is needed to locate the end of the header. The minimum value is 5 (5 * 4 bytes = 20 bytes, the standard header), and the maximum is 15 (15 * 4 bytes = 60 bytes).
  2. Reserved (6 bits):

    • Function: Reserved for future use; must be set to 0.
  3. Flags (6 bits, each representing a control function):

    • Function: Indicates special properties and purposes of the segment.
    • Details:
      • URG (Urgent): When set to 1, it indicates the segment contains urgent data that should be delivered promptly. The Urgent Pointer field is valid in this case.
      • ACK (Acknowledgment): When set to 1, the Acknowledgment Number field is valid. After connection establishment, the ACK flag is typically set to 1 in all segments.
      • PSH (Push): When set to 1, it prompts the receiver to deliver the data to the upper-layer application immediately, rather than waiting for the buffer to fill.
      • RST (Reset): When set to 1, it signals a severe error with the connection, which must be released and re-established.
      • SYN (Synchronize): When set to 1, it indicates this is a connection request or connection acceptance segment. Used in the three-way handshake.
      • FIN (Finish): When set to 1, it indicates the sender has finished sending data and wishes to terminate the connection. Used in the connection termination handshake.
  4. Window Size (16 bits):

    • Function: Used for flow control. Indicates how many bytes of data, starting from the acknowledgment number in this segment, the receiver is willing to accept.
    • Details: This is the basis for TCP's sliding window flow control mechanism. The sender adjusts its transmission rate based on the window size advertised by the receiver to avoid overflowing the receiver's buffer.

Step 4: Ensuring Segment Integrity and Special Functions

  1. Checksum (16 bits):

    • Function: Used to verify the integrity of the TCP header, data, and a pseudo-header (containing source IP, destination IP, protocol type, etc.) during transmission.
    • Details: Calculated by the sender and verified by the receiver. If the checksum fails, the receiver silently discards the segment, which triggers retransmission by the sender upon timeout.
  2. Urgent Pointer (16 bits):

    • Function: When the URG flag is 1, this pointer indicates the position of the last byte of urgent data within the segment's data portion.
    • Details: Urgent data needs "priority" handling. The value of the urgent pointer, when added to the sequence number, gives the position of the last byte of urgent data.

Step 5: The Extensible Part – Options and Padding

  1. Options (Variable length):

    • Function: Used to extend TCP functionality and is the reason for the variable header length.
    • Common Options:
      • Maximum Segment Size (MSS): Advertised during the three-way handshake, it indicates the maximum data length this end can accept in a segment, to avoid IP-layer fragmentation.
      • Window Scale Factor: Used to scale the 16-bit window size for support in high-speed networks.
      • Selective Acknowledgments (SACK): Allows the receiver to inform the sender about non-contiguous blocks of data that have been received, improving retransmission efficiency.
      • Timestamps: Used for Round-Trip Time (RTT) calculation and Protection Against Wrapped Sequence numbers (PAWS).
  2. Padding:

    • Function: Ensures the TCP header length is a multiple of 4 bytes.
    • Details: Because the Options field is variable in length, it may cause the header length not to be a multiple of 4 bytes. Padding with zeros is used to satisfy the requirement of the Data Offset field.

Summary

Through the breakdown above, we can see the ingenious design of the TCP segment structure, with each field serving a crucial purpose:

  • Port Numbers define the communication endpoints.
  • Sequence and Acknowledgment Numbers form the cornerstone of reliable delivery.
  • Flag Bits control the connection lifecycle (SYN, FIN, RST) and data handling (URG, PSH).
  • Window and Checksum handle flow control and error detection, respectively.
  • Options provide extensibility to the protocol.

Understanding the TCP segment structure is the first step towards a deeper mastery of advanced TCP features like connection management, reliable data transfer, flow control, and congestion control.