TCP Protocol Notes

Stream of Bytes and Sequence Numbers

  • Host A (source) communicates with Host B (destination) using TCP.
  • TCP data is transmitted with a TCP header.
  • ISN (Initial Sequence Number): The initial sequence number for the connection.
  • Sequence number: Sequence number of the first byte.
  • ACK sequence number: The next expected byte.

TCP Byte Service

  • Application processes communicate using TCP.
  • TCP uses send and receive buffers.
  • Write: Bytes are written to the send buffer.
  • Read: Bytes are read from the receive buffer.
  • Data is transmitted in segments.
  • TCP is a full-duplex protocol, meaning data and acknowledgements flow in both directions.

TCP Segment

  • TCP segment encapsulated within an IP packet.
  • The size of the IP packet must not be bigger than the Maximum Transmission Unit (MTU).
  • Example: Up to 1500 bytes on an Ethernet network.
  • The TCP header is typically 20 bytes long.

TCP Segment Header Format

  • The TCP segment header includes the following fields:
    • SrcPort: Source port.
    • DstPort: Destination port.
    • SequenceNum: Sequence number.
    • Acknowledgment: Acknowledgment number.
    • HdrLen: Header length.
    • Flags: Control flags.
    • AdvertisedWindow: Advertised window size.
    • Checksum: Checksum for error detection.
    • UrgPtr: Urgent pointer.
    • Options: Variable options.
    • Data: The actual data.

Header Fields in a Segment

  • <SrcPort, SrcIPAddr, DestPort, DestIPAddr>: Defines a TCP connection.
  • SequenceNum: Denotes the location of the first byte in the segment.
  • Flags: A 6-bit field containing control flags:
    • SYN: Synchronize, used to establish a connection.
    • FIN: Finish, used to terminate a connection.
    • ACK: Acknowledgment, signifies an acknowledgement.
    • URG: Urgent data, indicates urgent data.
    • UrgPtr: Points to where the urgent data ends.
    • PUSH: Notifies the application to deliver data immediately.
    • RESET: Indicates an error occurred and the connection needs to be aborted.

Simplified TCP Flow

  • Data flows toward the receiver (full duplex, with flow in the opposite direction).
  • Acknowledgements (Acks) and window size flow in the opposite direction.

Connection Establishment: Three-Way Handshake

  • Involves the exchange of three messages.
  • Active participant (client) initiates the connection.
  • Passive participant (server) listens for connection requests.
    1. Client sends SYN, Sequence Num = x.
    2. Server responds with SYN + ACK, Sequence Num = y, Acknowledgment = x + 1.
    3. Client sends ACK, Acknowledgment = y + 1.

Client's SYN Packet

  • Contains the following fields:
    • Client's Port
    • Server's Port
    • Client's ISN (Initial Sequence Number)
    • Acknowledgment
    • SYN flag is set.
    • Other header fields: AdvertisedWindow, Checksum, UrgPtr, Options, Data.

Server's Reply Packet

  • Contains the following fields:
    • Server's Port
    • Client's Port
    • Server's ISN
    • Acknowledgment = Client's ISN + 1
    • SYN and ACK flags are set.
    • Other header fields: AdvertisedWindow, Checksum, UrgPtr, Options, Data.

Client's ACK Packet

  • Contains the following fields:
    • Client's Port
    • Server's Port
    • Sequence Num
    • Acknowledgment = Server's ISN + 1
    • ACK flag is set.
    • Other header fields: AdvertisedWindow, Checksum, UrgPtr, Options, Data.

Initial Sequence Number (ISN)

  • Sequence number for the very first byte of the session.
  • Why not a default ISN of 0?
    • Practical issue: IP addresses and port numbers uniquely identify a connection. The same port numbers may get used again, and an old packet might still be in flight, potentially associated with the new connection.
    • Security reasons: To prevent TCP hijacking.
  • TCP requires changing the ISN over time.

Tearing Down the Connection

  • Closing the connection:
    • FIN (Finish) is sent to close and receive remaining bytes.
    • The other host sends a FIN ACK to acknowledge.
    • RST (Reset) is sent to close and not receive remaining bytes.

Flow Control: Sliding Window in TCP

  • Goals of sliding window:
    • Guarantee end-to-end reliable data delivery and in-order delivery.
    • Flow control between sender and receiver to avoid receiver buffer overflow.
  • Key variables:
    • LastByteAcked: Last byte acknowledged.
    • LastByteSent: Last byte sent.
    • LastByteWritten: Last byte written.
    • LastByteRead: Last byte read.
    • NextByteExpected: Next byte expected.
    • LastByteRcvd: Last byte received.
  • Relationships:
    • LastByteAckedLastByteSentLastByteWrittenLastByteAcked \leq LastByteSent \leq LastByteWritten
    • LastByteRead < NextByteExpected \leq LastByteRcvd + 1
  • Receive window: MaxRcvBuffer
  • Transmit window: advertisedWindow

Flow Control in TCP

  • Receiver advertises window no larger than its buffer size:
    • LastByteRcvdLastByteReadMaxRcvBufferLastByteRcvd – LastByteRead \leq MaxRcvBuffer
    • AdvertisedWindow=MaxRcvBuffer((NextByteExpected1)LastByteRead)AdvertisedWindow = MaxRcvBuffer – ((NextByteExpected – 1) – LastByteRead)
  • Example:
    • LastByteRcvd=5,LastByteRead=1,NextByteExpected=6,MaxRcvBuffer=10LastByteRcvd = 5, LastByteRead = 1, NextByteExpected = 6, MaxRcvBuffer = 10
    • 51105 – 1 \leq 10
    • AdvertisedWindow=10((61)1)=104=6AdvertisedWindow = 10 - ((6 – 1) – 1) = 10 – 4 = 6
  • Sender adheres to the AdvertisedWindow:
    • LastByteSentLastByteAckedAdvertisedWindowLastByteSent – LastByteAcked \leq AdvertisedWindow
    • EffectiveWindow=AdvertisedWindow(LastByteSentLastByteAcked)EffectiveWindow = AdvertisedWindow – (LastByteSent – LastByteAcked)
  • Example:
    • sent=7,ACK=5,EffectiveWindow=6(75)=4sent = 7, ACK = 5, EffectiveWindow = 6 – (7 - 5) = 4
  • Maximum number of outstanding packets/bytes.

Sequence Number Wraparound

  • AdvertisedWindow: 16 bits long, up to 2162^{16}.
  • Sequence number field: 32 bits long, up to 2322^{32}.
  • SN >> AdvertisedWindow, so there would be no problem in ordering.
  • How fast are sequence numbers exhausted:
    • T1 (1.5Mbps): 232x8/1.5x1066.4hrs2^{32} x 8 / 1.5 x 10^6 \approx 6.4hrs
    • Ethernet (10 Mbps): 57 mins
    • T3 (45Mbps): 13 mins
    • Fast Ethernet (100 Mbps): 6 mins
    • OC-3 (155Mbps): 4 mins
    • OC-12 (622 Mbps): 55 sec
    • OC-48 (2.5 Gbps): 14 sec

Failure due to Seq # Wraparound

  • Sequence number wrap-around on the current connection.
  • A sequence number may be "wrapped" (cycled) within the time that a segment is delayed in queues.
  • Earlier incarnation of the connection.
  • A delayed segment from the terminated connection could fall within the current window for the new incarnation and be accepted as valid.
  • Solution: Add a timestamp field on every packet.