TCP Connection Establishment (3-Way Handshake)
TCP opens every connection with a three-message ritual: SYN, SYN+ACK, ACK. This lesson fixes the canonical flags, the exact sequence and acknowledgment arithmetic, why three steps (and not two), and attacks that abuse the ritual on web servers.
The control bits that matter
The TCP header’s flag byte holds six control bits. For connection management you need four of them now (the other two — PSH for push and URG for urgent — are data-related and rarely tested for establishment):
| Flag | Full name | Meaning |
|---|---|---|
| SYN | Synchronise | “Here is my initial sequence number; synchronise with me.” Starts a connection. |
| ACK | Acknowledge | “The acknowledgement field is valid.” Confirms receipt. |
| FIN | Finish | “No more data from my side.” Starts a graceful close (Lesson 5). |
| RST | Reset | Abort the connection immediately; also a “no such connection” reply. |
The three-way handshake, precisely
Before any application data flows, the client and server exchange exactly three segments. Learn the canonical version (RFC 793) with symbols:
Step 1 Client --> Server SYN
Flags: SYN = 1, ACK = 0 (no ACK yet)
SEQ = X (X = client's ISN, the byte number the
client's first data byte will carry)
ACK field: not meaningful
Step 2 Server --> Client SYN + ACK
Flags: SYN = 1, ACK = 1
SEQ = Y (Y = server's OWN ISN)
ACK = X + 1 (acknowledges step 1; confirms the
client's SYN occupies one SYN byte)
Step 3 Client --> Server ACK
Flags: SYN = 0, ACK = 1
SEQ = X + 1 (client's next byte)
ACK = Y + 1 (acknowledges step 2; confirms the
server's SYN byte)
The arithmetic is the whole exam point, so do it slowly with real numbers. Let X = 2000 (client’s ISN) and Y = 7121 (server’s ISN):
- Step 1: client sends
SEQ=2000, SYN set, no ACK. - Step 2: server replies
SEQ=7121,ACK=2001, SYN+ACK both set. - Step 3: client replies
SEQ=2001,ACK=7122, ACK set.
Check the two rules you already learned in Lesson 3: (a) each SYN consumes one byte of sequence space, so the ACK that answers a SYN is always ISN + 1; (b) an ACK number names the next expected byte. The client’s first real data byte is 2001, hence step 3’s SEQ is 2001; the server’s first real data byte is 7122, hence step 3’s ACK is 7122. If asked “after the handshake, what is the client’s next sequence number?” the answer is X+1 — the SYN byte was already numbered X but carries no application data.
ISN+1. SYN+ACK → ACK ISN+1. The two ACK values are X+1 and Y+1; never subtract. A common trap is writing the client’s final SEQ as X+1 but the server’s ACK for it as X+1 too — check which stack you are in: the client’s SEQ mirrors X+1; the server’s ACK field is X+1.
Why three steps and not two
A classic exam question: why can’t the client-to-server reachability be proved in two messages? Think about what each party knows after each step:
- After step 1, only the server knows something: “someone sent me a SYN.” It cannot even trust that the client is reachable — the SYN may be a spoofed source.
- After step 2, the client is the only one who knows both directions work: it sent a SYN and received the SYN+ACK. The server still does not know whether its SYN+ACK reached the client.
- After step 3, the server receives the ACK and knows: the client got the SYN+ACK. Now both sides know both directions work, both sides have synchronised their sequence numbers, and both have exchanged their initial parameters.
ISN randomisation and parameter exchange
The ISN is not zero and not predictable. A fixed, guessable starting sequence would let an attacker hijack a connection by injecting segments whose sequence numbers it can predict (the old sequence-number-guessing attack). Modern stacks pick an initial sequence number that is hard to predict (RFC 6528 / RFC 1948: derived from a hash of the connection and a secret key). The ISN still counts bytes like any other sequence number — randomisation only changes where counting starts, never how it works. (UDP has no sequence numbers at all, which is one reason it is not connection-oriented.)
SYNs also carry the options both sides must agree on before data flows:
- MSS (Maximum Segment Size) — the largest TCP payload this side will accept (typically the interface MTU minus IP and TCP headers, commonly 1460 bytes on Ethernet).
- Window size (and window scale) — the advertised buffer for flow control, optionally multiplied by a negotiated scale factor.
- Also negotiated: SACK (selective acknowledgements), timestamps.
Because both SYNs carry options, steps 2 and 3 do double duty: they synchronise numbers and settle the connection’s parameters before a single data byte moves.
The timing diagram
CLIENT SERVER
| |
| 1. SYN SYN=1,ACK=0 |
|----> SEQ=2000 ------------------------->| client ISN X = 2000
| | server transitions:
| | LISTEN --> SYN-RECEIVED
| 2. SYN+ACK SYN=1,ACK=1 |
|<--- SEQ=7121, ACK=2001 -----------------| server ISN Y = 7121
| |
| 3. ACK SYN=0,ACK=1 |
|----> SEQ=2001, ACK=7122 -------------->| client is now ESTABLISHED
| | server: SYN-RECEIVED --> ESTABLISHED
|=====================================================================>
| Both directions verified; |
| data may now flow (full duplex) |
seq=X…X+1 and ack=…Y+1 on the server’s side — if a filter question shows you these two values, you should name the phase instantly.
The SYN flood attack on web servers
Here is the security tie-in (a future week deepens it): the handshake is expensive for the server. An attacker sends thousands of step-1 SYNs using spoofed source IP addresses that will never reply. The server dutifully answers each with SYN+ACK and allocates a half-open connection (the SYN-RECEIVED state) in its connection backlog, then waits for the ACK that never comes, holding the entry until a timeout cleans it up.
- Effect on a web server: the backlog fills with bogus half-open connections; new legitimate SYNs are dropped. Because the attack never completes step 3, the server also spends its CPU and memory maintaining the phantom entries — a denial of service against the real visitors.
- Why spoofing matters: step 2 goes to the forged source address, so the attacker never needs to receive it. A real client would complete the handshake and free the entry; the spoofed half-connections persist.
- Mitigations: SYN cookies (the server encodes the connection state into the SYN+ACK’s sequence number and statelessly validates the client’s ACK), aggressive half-open timeouts, rate limiting of SYNs, firewalls that filter obviously spoofed sources (BCP 38 ingress filtering), and TCP stack tuning.
Review
Check what you have learned with these review questions.
Sources & references
- Postel, J. RFC 793: Transmission Control Protocol (1981), Section 3.4 (establishing a connection), the state diagram (LISTEN, SYN-RECEIVED, ESTABLISHED). rfc-editor.org/rfc/rfc793.
- Bellovin, S. RFC 1948: Defending Against Sequence Number Attacks (1996); RFC 6528 updates the ISN generation guidance. rfc-editor.org/rfc/rfc1948.
- Bernstein, D. J. SYN cookies (technical note on the defence). cr.yp.to/syncookies.html.
- IANA. Service Name and Transport Protocol Port Number Registry (the ports the web server listens on). iana.org.
- Cisco. CCNA (200-301) Exam Topics, Network Fundamentals. cisco.com.
Target exams: CCNA 200-301 v1.1 · CyberOps Associate 200-201. Last content verification: 2026-10-11.