← All posts
TCP/IP

The TCP Three-Way Handshake: What Happens Before Any Data Moves

The TCP Three-Way Handshake: What Happens Before Any Data Moves

Every TCP connection you have ever opened, whether a browser loading a page, an SSH session or a BGP peering, began with three packets. The TCP three-way handshake is small, but it decides a lot: which sequence numbers each side will use, which options are enabled, and whether the other side is even listening. In this post we follow the handshake packet by packet, look at what each field means, and then see what it looks like in a capture and when it goes wrong.

Why TCP needs a handshake

TCP, defined in RFC 9293 (which obsoletes RFC 793), provides a reliable, ordered byte stream. To do that, both endpoints must keep state: the next sequence number to send, the next one expected, window sizes and negotiated options. The handshake is how two hosts create that shared state. Each side must announce its own initial sequence number (ISN) and have the other side acknowledge it. Because each direction has its own sequence space, that takes a SYN and an acknowledgement in each direction. The middle two steps are combined into a single packet, which is why it takes three packets instead of four.

The three steps

Assume a client (C) connecting to a server (S) listening on port 443.

Step 1: SYN

The client sends a segment with the SYN flag set and no payload. It chooses an initial sequence number, call it x. The client moves from CLOSED to SYN-SENT. This segment also carries the options the client supports, such as Maximum Segment Size, window scale, SACK-permitted and timestamps.

Step 2: SYN-ACK

The server, which was in LISTEN, replies with both SYN and ACK set. It picks its own ISN y, and acknowledges x + 1. The SYN flag consumes one sequence number, which is why the acknowledgement number is one higher than the ISN. The server moves to SYN-RECEIVED and includes its own options in the reply.

Step 3: ACK

The client sends a plain ACK with sequence number x + 1 and acknowledgement number y + 1. Both sides are now ESTABLISHED, and data can flow. This third segment may already carry data in practice, since the client is allowed to send once it is established.

Client                                   Server
 CLOSED                                   LISTEN
 SYN-SENT     --- SYN, seq=x --------Client                                   Server
 CLOSED                                   LISTEN
 SYN-SENT     --- SYN, seq=x ---------->  SYN-RECEIVED
 ESTABLISHED  <-- SYN+ACK, seq=y, ack=x+1  
 ESTABLISHED  --- ACK, seq=x+1, ack=y+1-> ESTABLISHED

What gets negotiated

The handshake is also the only place where several TCP options can be agreed. They appear in the TCP header options field of the SYN and SYN-ACK:

  • MSS: the largest segment payload each side is willing to receive. It is commonly derived from the interface MTU minus IP and TCP headers.

  • Window scale (RFC 7323): a shift count that lets the 16-bit window field describe windows larger than 64 KB. It is only in effect if both SYN and SYN-ACK carry it. We cover this in detail in TCP receive window and window scaling.

  • SACK-permitted: allows selective acknowledgements later in the connection.

  • Timestamps (RFC 7323): used for RTT measurement and protection against wrapped sequence numbers.

Because these options are only valid in SYN segments, a middlebox that strips or rewrites them silently changes how the connection behaves for its entire lifetime. That is a good reason to look at the handshake first when a connection is slow but not broken.

Why the initial sequence number is not zero

Starting every connection at sequence number 0 would let old, delayed segments from a previous connection with the same four-tuple be mistaken for new data. RFC 9293 therefore requires a clock-driven ISN, and modern stacks add randomness so that attackers cannot easily guess it. Predictable ISNs historically allowed connection spoofing, which is why randomized ISN generation (see RFC 6528) is standard. For the full layout of the header fields themselves, see the TCP header: ports, sequence numbers and flags.

Seeing it in a capture

You can reproduce the handshake yourself on a lab machine. The commands below are illustrative; adjust the interface and host for your environment.

# Terminal 1: capture the first three packets on port 443
sudo tcpdump -nn -S -i eth0 'tcp port 443' -c 3

# Terminal 2: open a connection
curl -sI https://example.com > /dev/null

Illustrative output, abbreviated and with documentation addresses, not a real measurement:

IP 192.0.2.10.51514 > 198.51.100.20.443: Flags [S],  seq 1000000, win 64240, options [mss 1460,sackOK,TS val 1 ecr 0,nop,wscale 7], length 0
IP 198.51.100.20.443 > 192.0.2.10.51514: Flags [S.], seq 2000000, ack 1000001, win 65160, options [mss 1460,sackOK,TS val 2 ecr 1,nop,wscale 7], length 0
IP 192.0.2.10.51514 > 198.51.100.20.443: Flags [.],  ack 2000001, win 502, length 0

In Wireshark, the same thing is easy to isolate with a display filter:

tcp.flags.syn == 1

Wireshark by default shows relative sequence numbers (starting at 0) to keep the display readable. If you want the real values to confirm the ISN behavior, turn off Relative sequence numbers in the TCP protocol preferences. The time between the SYN and the SYN-ACK is also a useful first approximation of the round-trip time to the server, plus any server-side queuing.

When the handshake fails

Most connection problems show up as one of a few handshake patterns:

  • SYN with no reply, retransmitted: the SYN is being dropped, by a firewall, an ACL, a routing problem or a host that is down. Clients retransmit the SYN with increasing delays before giving up.

  • SYN answered by RST: the host is reachable, but nothing is listening on that port, or a device is actively rejecting the connection. This fails fast, which is the clue that distinguishes it from a drop.

  • SYN and SYN-ACK seen, but no final ACK: often an asymmetric path or a stateful device that does not see the return traffic. The server stays in SYN-RECEIVED and will retransmit its SYN-ACK.

  • Handshake completes but large transfers stall: check the MSS values in both SYNs and whether ICMP is permitted. An MSS that does not match the real path MTU leads to blackholed large packets even though the handshake looks fine.

SYN floods and SYN cookies

The server has to allocate some state after it receives a SYN, which makes the half-open state a target. In a SYN flood, an attacker sends many SYNs, often with spoofed source addresses, so the server fills its backlog waiting for ACKs that never arrive. Many stacks defend with SYN cookies: the server encodes the necessary state into the ISN of its SYN-ACK and keeps nothing until a valid ACK comes back. The trade-off is that some options may not be fully preserved when cookies are in use, and the exact behavior depends on the operating system. Check your platform's documentation rather than assuming.

What comes next

The handshake is only the opening move. Once the connection is ESTABLISHED, reliability comes from sequence numbers, acknowledgements and retransmission, which we introduced in what TCP reliability means. The same handshake is what runs underneath every BGP peering before any OPEN message is exchanged, which you can follow in BGP peering: the TCP session and FSM states.

Key takeaways

  • The handshake is SYN, SYN-ACK, ACK. It synchronizes the initial sequence numbers in both directions.

  • SYN consumes one sequence number, so each acknowledgement is ISN + 1.

  • MSS, window scale, SACK and timestamps are negotiated only in the SYN segments.

  • Reading the handshake in a capture tells you quickly whether a failure is a drop, a reject, an asymmetric path or an MTU problem.

Comments (0)

Please sign in to comment.

  • Be the first to comment.