Skip to content

Lesson 2 of 9 · 24m

Handshake and Connection Lifecycle: SYN to CLOSE

The three-way handshake, simultaneous state on both endpoints, retransmitted SYNs, half-open connections, RST behavior, and the close states that confuse operators.

TCP’s handshake exists to synchronize state, not to exchange pleasantries. Each side needs to know the other’s initial sequence number and confirm that packets can flow in both directions. The three packets are small, but the operational consequences are enormous: firewalls key state off them, load balancers enforce them, and a one-way routing fault often looks like a handshake problem even though routing is the real culprit.

The three packets that matter

client -> server  SYN        seq=1000
server -> client  SYN, ACK   seq=5000, ack=1001
client -> server  ACK        seq=1001, ack=5001

After that, the connection is established. The sequence numbers are randomized to reduce blind spoofing and stale-segment confusion. If you see a third handshake packet never arriving, verify return routing before blaming the server.

SYN retransmission tells you where to look

A Linux client retrying SYNs commonly backs off at increasing intervals. On a capture taken at the client, repeated outbound SYNs with no SYN-ACK usually mean one of four things: the server is not listening, a firewall drops the SYN, the SYN-ACK is lost on the return path, or asymmetric routing sends it elsewhere.

ss -tan state syn-sent
ss -tan state established '( dport = :443 )'

Capture on both ends if possible. A SYN seen arriving at the server but no SYN-ACK leaving is very different from no SYN arriving at all.

RST is not always an attack

A reset means some endpoint or middlebox wants the byte stream gone now. Common legitimate causes include connecting to a closed port, writing to a connection after the peer closed it, and idle state expiring in a NAT or firewall. In capture work, compare the RST’s sequence and acknowledgment numbers with the existing stream; a wildly mismatched RST often came from an observer or stale state rather than the peer.

Closing is a separate lifecycle

TCP close is normally four packets because each direction closes independently:

A -> B  FIN, ACK
B -> A  ACK
B -> A  FIN, ACK
A -> B  ACK

TIME_WAIT sits on the side that sends the final ACK. It protects the internet from old duplicate segments in a later incarnation of the same four-tuple. High TIME_WAIT counts are often normal for short-lived outbound connections and are not automatically a leak.

States worth recognizing

SYN-SENT       client sent SYN, waiting for SYN-ACK
SYN-RECEIVED   server replied, waiting for final ACK
ESTABLISHED    data can flow
FIN-WAIT-1/2   local side initiated close
CLOSE-WAIT     peer closed; local application has not
LAST-ACK       final FIN is waiting for its ACK
TIME_WAIT      connection is closed but remembered

CLOSE-WAIT accumulation usually indicts the local application: the peer finished, but the process never closed its socket.

Field notes

  • The handshake does not authenticate identity; it only synchronizes transport state.
  • A completed TCP handshake says nothing about TLS trust or application health.
  • Middleboxes can complete handshakes on behalf of backends, especially load balancers and proxies.
  • Do not “fix” TIME_WAIT by lowering kernel timers unless you can explain which invariant you weaken; record capture side because packet absence is ambiguous.

Was this lesson helpful?

Sign in to rate