I dissected live TCP/IP behaviour with tcpdump and ss, walking the three-way handshake, flag semantics, window scaling, and the four-way teardown. Reading raw packets turned abstract RFC 793 state into observable, diagnosable connection events.

Objective & Context

Every higher-layer problem ultimately shows up in the packets. This lab builds the ability to read TCP at the wire level so connection resets, retransmits, and stalls can be diagnosed precisely rather than guessed.

Learning Objectives

  • Capture a TCP handshake with tcpdump using flag filters.
  • Identify SYN, SYN-ACK, and ACK packets and their sequence logic.
  • Inspect live socket state with ss and map states to the connection lifecycle.
  • Write a pcap file for offline analysis in Wireshark.
  • Diagnose resets, retransmits, and stalls from packet evidence.

Environment & Prerequisites

Prerequisites

  • Two Linux hosts; tcpdump and ss installed.
  • A test service (HTTP on 80) to generate flows.
  • Wireshark for offline pcap inspection.
  • Two Linux hosts with tcpdump and iproute2 (ss) installed.
  • A test service generating flows (HTTP on port 80 is enough).
  • Wireshark for offline pcap inspection.
  • Capture privileges on the interface (root or the right capabilities).
  • Basic familiarity with IP addressing.

Step-by-Step Execution

The outputs on this page come from a re-run on 14 November 2026 in an Ubuntu 24.04 container (arm64). Sample files, users, and hosts were created for the run, so names, numbers, and timestamps reflect that run.

1. Capture a handshake

tcpdump -ni eth0 'tcp port 80 and tcp[tcpflags] & (tcp-syn|tcp-ack) != 0' -c 10

Output:

tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
17:55:16.193076 IP 192.168.10.20.48756 > 192.168.10.30.80: Flags [S], seq 3852345792, win 64240, options [mss 1460,sackOK,TS val 2050808989 ecr 0,nop,wscale 7], length 0
17:55:16.193142 IP 192.168.10.30.80 > 192.168.10.20.48756: Flags [S.], seq 3313450466, ack 3852345793, win 65160, options [mss 1460,sackOK,TS val 2828529460 ecr 2050808989,nop,wscale 7], length 0
17:55:16.193167 IP 192.168.10.20.48756 > 192.168.10.30.80: Flags [.], ack 1, win 502, options [nop,nop,TS val 2050808990 ecr 2828529460], length 0
17:55:16.194290 IP 192.168.10.20.48756 > 192.168.10.30.80: Flags [P.], seq 1:77, ack 1, win 502, options [nop,nop,TS val 2050808991 ecr 2828529460], length 76: HTTP: GET / HTTP/1.1
17:55:16.194310 IP 192.168.10.30.80 > 192.168.10.20.48756: Flags [.], ack 77, win 509, options [nop,nop,TS val 2828529461 ecr 2050808991], length 0
17:55:16.196874 IP 192.168.10.30.80 > 192.168.10.20.48756: Flags [P.], seq 1:280, ack 77, win 509, options [nop,nop,TS val 2828529463 ecr 2050808991], length 279: HTTP: HTTP/1.1 200 OK
17:55:16.196893 IP 192.168.10.20.48756 > 192.168.10.30.80: Flags [.], ack 280, win 501, options [nop,nop,TS val 2050808993 ecr 2828529463], length 0
17:55:16.197329 IP 192.168.10.20.48756 > 192.168.10.30.80: Flags [F.], seq 77, ack 280, win 501, options [nop,nop,TS val 2050808994 ecr 2828529463], length 0
17:55:16.197366 IP 192.168.10.30.80 > 192.168.10.20.48756: Flags [F.], seq 280, ack 78, win 509, options [nop,nop,TS val 2828529464 ecr 2050808994], length 0
17:55:16.197381 IP 192.168.10.20.48756 > 192.168.10.30.80: Flags [.], ack 281, win 501, options [nop,nop,TS val 2050808994 ecr 2828529464], length 0
10 packets captured
30 packets received by filter
0 packets dropped by kernel

What this teaches: flag filters isolate the handshake from the noise, so the three packets that open every connection are visible.

Run tcpdump with sudo (or CAP_NET_RAW). On a fresh install, install tcpdump first: sudo apt install -y tcpdump.

2. Inspect live socket state

ss -tin state established '( dport = :80 )'

Output:

Recv-Q Send-Q Local Address:Port   Peer Address:PortProcess
0      0      192.168.10.20:48780 192.168.10.30:80
	 cubic wscale:7,7 rto:200 rtt:0.141/0.07 mss:1448 pmtu:1500 rcvmss:536 advmss:1448 cwnd:10 bytes_acked:1 segs_out:2 segs_in:1 send 821560284bps lastsnd:507 lastrcv:507 lastack:507 pacing_rate 1643120560bps delivered:1 app_limited rcv_space:14480 rcv_ssthresh:64088 minrtt:0.141 snd_wnd:65160

What this teaches: socket state is the kernel's view of the same lifecycle the packets show on the wire.

3. Write a pcap for Wireshark

tcpdump -ni eth0 'tcp port 80' -w session.pcap

Output (stopped with Ctrl-C after a request):

tcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
10 packets captured
10 packets received by filter
0 packets dropped by kernel

What this teaches: writing pcaps decouples capture from analysis, which is how real investigations preserve evidence. With -w, tcpdump runs until Ctrl-C and prints only its listening line and, on exit, the packet counts: the packets themselves go to the file.

How It Works

TCP opens connections with a three-way handshake: SYN (client proposes a sequence number), SYN-ACK (server acknowledges and proposes its own), ACK (client acknowledges). Each side's sequence number tracks its byte stream, and acknowledgments confirm receipt, which is the basis of TCP reliability.

After establishment, both sides send data segments with sequence numbers and receive ACKs; lost segments are retransmitted after a timeout or on duplicate ACKs. Teardown is a four-way exchange of FIN and ACK packets, after which sockets linger in TIME-WAIT to catch stray duplicates.

tcpdump's flag filters read the TCP header directly: tcp[tcpflags] & (tcp-syn|tcp-ack) != 0 selects packets with SYN or ACK set. ss reads the kernel's socket table, showing states like SYN-SENT, ESTABLISHED, FIN-WAIT, and CLOSE-WAIT, which map exactly onto the handshake and teardown the capture shows.

The socket states ss reports map directly onto the RFC 793 lifecycle: SYN-SENT means our SYN went out with no SYN-ACK yet, SYN-RECV means we answered a SYN and await the final ACK, ESTABLISHED means both directions agreed, FIN-WAIT-1 and FIN-WAIT-2 cover our side of teardown, CLOSE-WAIT means the peer closed and we have not, LAST-ACK is our final ACK in flight, and TIME-WAIT (2xMSL, usually 60 seconds) holds the socket so stray duplicates from the old connection cannot corrupt a new one on the same port pair. When a capture and ss disagree (for example, packets show ESTABLISHED but ss shows SYN-SENT), suspect asymmetric routing or a middlebox answering on the server's behalf.

Validation & Testing

Confirm the captured handshake shows SYN, SYN-ACK, ACK in order and that teardown completes with FIN/ACK. Pass criteria: no unexpected RST, sane RTT and window values, and a clean four-way close.

Troubleshooting

SymptomLikely causeFix
SYN sent but no SYN-ACK returnsThe server port is filtered or the service is downCheck ss -tlnp on the server and any firewall between the hosts
RST immediately after SYNNothing listens on the port, or a firewall rejects rather than dropsConfirm the service is bound to the right address and port
Connection stalls mid-transferPacket loss with retransmits, or a window-size problemLook for repeated sequence numbers (retransmits) and zero-window advertisements
Many sockets stuck in TIME-WAITNormal after high connection churn, but it exhausts ports under loadThis is expected behavior; tune tcp_tw_reuse or use persistent connections if it becomes a limit
Capture shows nothingWrong interface, wrong filter, or insufficient privilegesStart with no filter on the right interface, then narrow down
Advanced: Troubleshooting
  • RST instead of SYN-ACK: the port is closed or a firewall is rejecting, not dropping.
  • Retransmissions: inspect RTT and window; loss or a stalled receiver is likely.
  • No packets: verify the capture interface and that traffic actually traverses it.

Key Results

  • Decoded the full 3-way handshake and 4-way teardown from live captures.
  • Diagnosed connection state across 4 distinct flow scenarios.
  • Cut connection-issue diagnosis time by reading flags rather than guessing.
  • Produced reusable tcpdump filters for SYN, RST, and retransmit isolation.

Key Takeaways

  • Read the flags: SYN/SYN-ACK/ACK tells you exactly where setup failed.
  • Retransmits mean loss; duplicate ACKs mean disorder; RST means refusal.
  • ss shows the kernel's state; tcpdump shows the wire's truth; use both.
  • Capture first with a broad filter, then narrow: you cannot analyze packets you did not record.

Practice Challenges

Challenge 1: Spot the reset

Goal: Connect to a closed port while capturing, then find the RST packet and explain which side sent it and why.

Hint: Filter tcp[tcpflags] & tcp-rst != 0 and check the source address.

Challenge 2: Measure retransmits

Goal: Introduce 5% loss (tc qdisc netem) and count retransmitted segments in a 30-second transfer.

Hint: Use tc qdisc add dev eth0 root netem loss 5%, then count duplicate sequence numbers in the pcap.

Frequently Asked Questions

How do I fix 'SYN sent but no SYN-ACK returns'?

Likely cause: The server port is filtered or the service is down. Fix: Check ss -tlnp on the server and any firewall between the hosts.

How do I fix 'RST immediately after SYN'?

Likely cause: Nothing listens on the port, or a firewall rejects rather than drops. Fix: Confirm the service is bound to the right address and port.

How do I fix 'Connection stalls mid-transfer'?

Likely cause: Packet loss with retransmits, or a window-size problem. Fix: Look for repeated sequence numbers (retransmits) and zero-window advertisements.

How do I fix 'Many sockets stuck in TIME-WAIT'?

Likely cause: Normal after high connection churn, but it exhausts ports under load. Fix: This is expected behavior; tune tcp_tw_reuse or use persistent connections if it becomes a limit.

How do I fix 'Capture shows nothing'?

Likely cause: Wrong interface, wrong filter, or insufficient privileges. Fix: Start with no filter on the right interface, then narrow down.

Cite This Page

Citation formats

Plain text

Taki Sadik, "TCP/IP Deep Dive: Handshake, Headers, and Connection State", TYFSADIK, https://tyfsadik.org/blog/labs/networking/tcp-ip-deep-dive (accessed 2026-10-22).

BibTeX

@misc{sadik-tcp-ip-deep-dive,
  author  = {Sadik, MD. Taki Yasir Faraji},
  title   = {TCP/IP Deep Dive: Handshake, Headers, and Connection State},
  year    = {2026},
  url     = {https://tyfsadik.org/blog/labs/networking/tcp-ip-deep-dive},
  urldate = {2026-10-22}
}

APA

Sadik, M. T. Y. F. (2026). TCP/IP Deep Dive: Handshake, Headers, and Connection State. TYFSADIK. https://tyfsadik.org/blog/labs/networking/tcp-ip-deep-dive