Octet

Explain the TCP three-way handshake. What is exchanged besides SYN and ACK?

Show the answer

Say this first

The client sends a SYN with a random initial sequence number and its options. The server answers with a SYN-ACK carrying its own ISN and acknowledging the client's ISN plus one. The client sends an ACK for the server's ISN plus one. Along the way both sides announce their MSS, window scale, SACK support, timestamps and ECN support. The client goes SYN_SENT to ESTABLISHED; the server goes LISTEN to SYN_RCVD to ESTABLISHED.

Go deeper

The three segments

Output
Client (SYN_SENT)                              Server (LISTEN)
  SYN  seq=x       MSS 1460, SACK ok, TS, wscale 7       ->
                                                (SYN_RCVD)
  <-   SYN,ACK  seq=y ack=x+1   MSS 1460, SACK ok, TS, wscale 7
(ESTABLISHED)
  ACK  seq=x+1 ack=y+1                                   ->
                                                (ESTABLISHED)

The SYN and the SYN-ACK each use up one sequence number, which is why the acknowledgments are ISN plus one.

Why three and not two

Each side has to learn the other's ISN and know that its own ISN was received. Two ISNs need two acknowledgments; the server's SYN and its ACK share one segment, so three segments are enough. The exchange also defeats old duplicate SYNs: if a stale SYN reaches the server, the client answers the unexpected SYN-ACK with a RST instead of an ACK, and no connection opens.

Why the ISN is random

A predictable ISN lets an attacker who cannot see the traffic forge segments into a connection. RFC 6528 describes generating it from a keyed hash of the four-tuple plus a clock.

Options that only exist in the handshake

MSS, window scale and SACK permitted are only valid in SYN and SYN-ACK. If one side does not offer window scaling or SACK, neither side uses it. The third ACK may already carry data.

Traps

  • Saying the acknowledgment number is the next byte the sender will send. It is the next byte it expects to receive.
  • Reading Wireshark's relative sequence numbers and claiming the ISN is 0.
How did you do?Book 5.2 Reliable Byte Stream (TCP)