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.
- Client sends
SYN, Sequence Num = x. - Server responds with
SYN + ACK, Sequence Num = y, Acknowledgment = x + 1. - Client sends
ACK, Acknowledgment = y + 1.
- Client sends
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 ACKto 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:
- LastByteRead < NextByteExpected \leq LastByteRcvd + 1
- Receive window:
MaxRcvBuffer - Transmit window:
advertisedWindow
Flow Control in TCP
- Receiver advertises window no larger than its buffer size:
- Example:
- Sender adheres to the AdvertisedWindow:
- Example:
- Maximum number of outstanding packets/bytes.
Sequence Number Wraparound
AdvertisedWindow: 16 bits long, up to .- Sequence number field: 32 bits long, up to .
- SN >> AdvertisedWindow, so there would be no problem in ordering.
- How fast are sequence numbers exhausted:
- T1 (1.5Mbps):
- 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.