TCP HANDSHAKE
Three-way setup, one-way flood
A client opens a TCP connection by sending a SYN packet. An attacker sends 4 SYNs per second from fake source addresses and never completes the handshake. The server has a 1 Gbit/s link, 64 GB of RAM and a backlog of 128. What stops new clients from connecting?
TCP opens a connection in three packets. The client sends SYN with its sequence number x. The server answers SYN-ACK with its own y and ack x+1. The client sends ACK y+1. Between the second and third packet the server holds a half-open entry in its SYN queue, sized by the listen backlog.
An attacker sends SYNs from spoofed addresses. The server’s SYN-ACKs go to hosts that never asked, no ACK comes back, and each entry holds its slot until it times out. Once the queue is full, the server drops every SYN. SYN cookies keep no entry: the server puts the connection into y and checks it when the ACK arrives.
// listen(fd, 6): on every SYN
entry = { src, isn: x, y: random() } // TCB-lite, held up to ~60 s
if (synQueue.length >= backlog) drop(syn) // legit clients too
synQueue.push(entry)
send(SYN_ACK, { seq: y, ack: x + 1 })
// on ACK
e = synQueue.find(src, ack == e.y + 1)
if (e) { synQueue.remove(e); accept() }// SYN cookies (net.ipv4.tcp_syncookies = 1)
// on SYN: store nothing
y = hash(secret, src, sport, dst, dport, t >> 6) + x // + MSS bits in Linux
send(SYN_ACK, { seq: y, ack: x + 1 })
// on ACK seq = x + 1, ack = y + 1
x = seq - 1
ok = ack - 1 == hash(secret, 4-tuple, t_now) + x
|| ack - 1 == hash(secret, 4-tuple, t_now - 1) + x
if (ok) accept() // rebuild the connection from the ACK Watch the SERVER box and the invariant strip. 3-WAY HANDSHAKE shows the queue holding the client’s entry from SYN-ACK to ACK. In SYN FLOOD the queue reaches its limit at 1000 ms and every client SYN after that is dropped. In SYN COOKIES the same flood leaves the queue at 0, the client connects on its first SYN, and a forged ACK is rejected. Drag SYN BACKLOG and ATTACK SYNs / 500 ms to see where the flood stops working.
A client connects normally against a backlog of SYN BACKLOG half-open slots (default 6). Inject one burst of spoofed SYNs at a point where the client ends up refused. Half-open entries live 3 s.
GOAL: the legitimate client SYN is dropped
Implement stateless SYN cookies. encode returns the server ISN for a SYN; validate checks the final ACK (seq = x+1, ack = y+1). The cookie must bind the full 4-tuple and the client ISN, expire after the current and previous 64 s slot, and work with no stored state: the grader validates on a fresh instance. Keep arithmetic mod 2^32 (>>> 0).
Linux net.ipv4.tcp_syncookies and tcp_max_syn_backlog, the backlog argument to listen(), the “possible SYN flooding on port 443” kernel log line, and load balancers and DDoS scrubbers that terminate the handshake on your behalf.
“Walk me through the TCP handshake” and “what is a SYN flood and how do you defend against it?” Give the sequence numbers, say which state the server holds between SYN-ACK and ACK, explain why spoofed sources never free it, and describe SYN cookies: a keyed hash of the 4-tuple, a time counter and the client ISN, checked on the ACK.
USED IN
Chapters that build on this lesson.
- Satellite, direct-to-device and coverage for the unservedHow Mobile Networks Work
- Troubleshooting a live faultNetwork Engineer