Transmission Control Protocol

A comprehensive, interactive lesson covering TCP architecture, header anatomy, connection management, flow control, and network administrator skills.

๐Ÿ“š RFC 793 / RFC 9293 ๐ŸŽ“ CompTIA Network+ ๐Ÿ” CISSP Domain 4 โš™๏ธ Transport Layer (OSI Layer 4) ๐Ÿ› ๏ธ Wireshark Analysis

1 ยท What is TCP?

TCP (Transmission Control Protocol) is a connection-oriented, reliable transport-layer protocol defined in RFC 793 (1981) and updated by RFC 9293 (2022).

๐Ÿ”—

Connection-Oriented

TCP requires a logical connection to be established between endpoints before any data transfer begins โ€” via the three-way handshake (SYN โ†’ SYN-ACK โ†’ ACK).

โœ…

Reliable Delivery

Every segment is acknowledged. Unacknowledged segments are retransmitted. Sequence numbers ensure in-order delivery and detect lost or duplicate packets.

๐Ÿšฆ

Flow Control

The receiver advertises a window size, telling the sender how much data can be buffered at any one time โ€” preventing buffer overflow at the receiver.

๐ŸŒ

Congestion Control

TCP dynamically adjusts the sending rate using algorithms like CUBIC, BBR, and Reno to avoid overwhelming intermediate network devices.

๐Ÿ“ฆ

Full-Duplex

Data can flow in both directions simultaneously. Each direction has its own sequence number space and acknowledgment mechanism.

๐Ÿ”ข

Byte-Stream Service

TCP treats data as a continuous byte stream, not as individual messages. Application boundaries are not preserved โ€” the application must delimit its own messages.

CharacteristicTCP BehaviorRelevance
Protocol NumberIP Protocol 6Identifies TCP within IP packets
Connection Setup3-Way Handshake (SYN, SYN-ACK, ACK)Adds ~1 RTT before data transfer
Teardown4-Way Handshake (FIN, ACK, FIN, ACK) or RSTGraceful vs abrupt close
Header Size20 bytes minimum, up to 60 bytes with optionsMore overhead than UDP (8 bytes)
Sequencing32-bit sequence & acknowledgment numbersOrder and reliability assurance
Error Detection16-bit checksum (header + data + pseudo-header)Detects corruption in transit
Window Size16-bit field (up to 65,535 bytes; scaled with options)Enables flow control
Common PortsHTTP:80, HTTPS:443, SSH:22, FTP:21, SMTP:25Port-based multiplexing
MSSMaximum Segment Size negotiated in SYN optionsAvoids IP fragmentation
TIME_WAIT2 ร— MSL (typically 60โ€“240 sec) after closePrevents stale packets entering new connections
๐ŸŒ

Web Browsing (HTTP/HTTPS)

Every webpage request uses TCP. HTTP/1.1 uses persistent connections; HTTP/2 multiplexes streams over a single TCP connection.

๐Ÿ“ง

Email (SMTP / IMAP / POP3)

All major email protocols rely on TCP for guaranteed delivery of messages between mail servers and clients.

๐Ÿ–ฅ๏ธ

Remote Administration (SSH / RDP)

TCP ensures that remote shell commands are delivered in order โ€” critical when sequence matters for configuration changes.

๐Ÿ“

File Transfers (FTP / SFTP / SCP)

File integrity depends on TCP's reliability โ€” a single bit error in a database file could be catastrophic.

๐Ÿ—„๏ธ

Databases (MySQL / PostgreSQL)

Database queries and transactions require ordered, reliable delivery. Most RDBMS clients use TCP natively.

๐Ÿ“ก

VPN Tunnels (OpenVPN / WireGuard)

Many VPN implementations encapsulate tunneled traffic over TCP for firewall traversal, though UDP is often preferred for performance.

Network administrators use Wireshark to capture, dissect, and troubleshoot TCP sessions. Key filters include:

tcp.flags.syn == 1 โ€” Show SYN packets only
tcp.flags.reset == 1 โ€” Find RST resets (abnormal closes)
tcp.analysis.retransmission โ€” Identify retransmissions
tcp.stream eq 0 โ€” Follow a specific TCP stream
tcp.port == 443 โ€” Filter HTTPS traffic

The "TCP Stream" follow feature reconstructs the raw byte stream for deeper analysis of application data.

Stateful firewalls track TCP connection state. Admins must understand:

โ€ข Established sessions โ€” allow return traffic without explicit inbound rules
โ€ข SYN floods โ€” DoS attack using half-open connections (SYN_RCVD state)
โ€ข RST injection attacks โ€” adversarial TCP resets disrupting legitimate sessions
โ€ข Port-based ACLs โ€” matching on source/destination port, flags
โ€ข TCP state inspection โ€” only permit packets matching known-good connection state

Linux/Windows admins tune TCP kernel parameters for high-throughput networks:

net.core.rmem_max โ€” Maximum receive socket buffer
net.ipv4.tcp_window_scaling โ€” Enable Window Scaling option
net.ipv4.tcp_congestion_control โ€” Algorithm: cubic, bbr, reno
net.ipv4.tcp_tw_reuse โ€” Reuse TIME_WAIT sockets
net.ipv4.tcp_syn_backlog โ€” Queue size for half-open connections

For high-bandwidth, long-delay links (BDP = Bandwidth ร— RTT), increasing the window size is critical to fill the "pipe."

SYN Flood: Attacker sends many SYN packets with spoofed IPs. Server fills its backlog with SYN_RCVD entries. Mitigation: SYN cookies, rate limiting.

Session Hijacking: Attacker predicts sequence numbers and injects segments into an established session.

RST Attack: Injecting forged RST packets to terminate legitimate connections (used in censorship and DoS).

Land Attack: SYN packet with source IP = destination IP, causing OS confusion.

Teardrop Attack: Overlapping IP/TCP fragments that crash vulnerable stacks on reassembly.

ss -tnp โ€” Show all TCP sockets, numeric, with process info
ss -tnlp โ€” Show listening TCP ports
ss state established โ€” Show only ESTABLISHED connections
netstat -an | grep TIME_WAIT โ€” Count TIME_WAIT sockets
ss -s โ€” Socket summary statistics
tcpdump -i eth0 tcp port 80 โ€” Capture HTTP TCP traffic

Monitoring TCP connection counts and states is critical for diagnosing server overload, SYN flood attacks, or application bugs causing connection leaks.

3 ยท TCP Connection Lifecycle

TCP connections go through a structured setup, data transfer, and teardown process.

๐Ÿ’ป CLIENT
192.168.1.10:54321
๐Ÿ–ฅ๏ธ SERVER
10.0.0.1:443
1
SYN โ†’

Client โ†’ Server: SYN=1, Seq=100 (ISN). "I want to connect. My starting sequence number is 100."

2
โ† SYN-ACK

Server โ†’ Client: SYN=1, ACK=1, Seq=300 (server ISN), Ack=101. "Got it. My starting sequence is 300. I acknowledge byte 101."

3
ACK โ†’

Client โ†’ Server: ACK=1, Seq=101, Ack=301. "Acknowledged! Connection established. Data transfer may now begin."

๐Ÿ“ Admin Note: A high ratio of SYN packets to SYN-ACK packets (half-open connections) may indicate a SYN flood attack. Monitor with ss -s and look for large numbers of connections in the SYN_RCVD state.
๐Ÿ’ป CLIENT
๐Ÿ–ฅ๏ธ SERVER
1
FIN โ†’

Client โ†’ Server: FIN=1, Seq=X. "I have no more data to send. Initiating graceful close."

2
โ† ACK

Server โ†’ Client: ACK=1, Ack=X+1. "Got your FIN. I may still have data to send โ€” half-close state."

3
โ† FIN

Server โ†’ Client: FIN=1, Seq=Y. "Now I'm done too. Closing my side."

4
ACK โ†’

Client โ†’ Server: ACK=1, Ack=Y+1. "Received. Client enters TIME_WAIT for 2ร—MSL before fully closing."

๐Ÿ• TIME_WAIT State: After sending the final ACK, the active closer waits 2 ร— MSL (Maximum Segment Lifetime, typically 60s) before closing. This ensures the final ACK reaches the server (in case it was lost and the server resent its FIN). Admins may see many TIME_WAIT sockets on busy web servers โ€” this is normal but can exhaust port ranges under high connection rates.

Simulated Wireshark packet list showing a complete TCP connection to port 80:

No. Time Src IP Dst IP Proto Info
1 0.000000 192.168.1.10 10.0.0.1 TCP 54321 โ†’ 80 [SYN] Seq=0 Win=65535 Len=0 MSS=1460 WS=128 SACK_PERM
2 0.012341 10.0.0.1 192.168.1.10 TCP 80 โ†’ 54321 [SYN, ACK] Seq=0 Ack=1 Win=28960 Len=0 MSS=1460
3 0.012890 192.168.1.10 10.0.0.1 TCP 54321 โ†’ 80 [ACK] Seq=1 Ack=1 Win=65535 Len=0
4 0.013001 192.168.1.10 10.0.0.1 HTTP GET / HTTP/1.1
5 0.025441 10.0.0.1 192.168.1.10 TCP 80 โ†’ 54321 [ACK] Seq=1 Ack=78 Win=29056 Len=0
6 0.026100 10.0.0.1 192.168.1.10 HTTP HTTP/1.1 200 OK (text/html) [TCP segment 1]
7 0.026200 10.0.0.1 192.168.1.10 TCP [TCP segment 2] Seq=1461 Len=1460
8 0.026900 192.168.1.10 10.0.0.1 TCP [ACK] Seq=78 Ack=2921 Win=64576 Len=0
9 0.140000 192.168.1.10 10.0.0.1 TCP 54321 โ†’ 80 [FIN, ACK] Seq=78 Ack=4381 Win=64576 Len=0
10 0.152341 10.0.0.1 192.168.1.10 TCP 80 โ†’ 54321 [FIN, ACK] Seq=4381 Ack=79 Win=29056 Len=0
11 0.152900 192.168.1.10 10.0.0.1 TCP 54321 โ†’ 80 [ACK] Seq=79 Ack=4382 Win=64575 Len=0
๐Ÿ”ต Client โ†’ Server ๐ŸŸข Server โ†’ Client ๐ŸŸก HTTP Data ๐ŸŸ  Response Segments ๐ŸŸฃ Connection Close

4 ยท TCP State Machine

TCP maintains connection state using a finite state machine. Click any state to learn more.

CLOSED

No connection exists

LISTEN

Waiting for connection

SYN_SENT

SYN sent, awaiting SYN-ACK

SYN_RECEIVED

SYN received, sent SYN-ACK

ESTABLISHED

Connection open, data flows

FIN_WAIT_1

FIN sent, awaiting ACK

FIN_WAIT_2

Our FIN ACKed, wait for peer FIN

CLOSE_WAIT

Peer FIN received, waiting to close

CLOSING

Both sides sent FIN simultaneously

LAST_ACK

Waiting for our FIN to be ACKed

TIME_WAIT

Waiting 2ร—MSL before CLOSED

5 ยท Flow Control & Congestion Control

TCP dynamically adapts to receiver capacity and network conditions to maximize throughput without causing harm.

Sliding Window Simulator

4 segments
2 acked
Acknowledged Sent, not yet ACKed Window (can send now) Future (outside window)
๐Ÿš€

Slow Start

Begins with a congestion window (cwnd) of 1 MSS. cwnd doubles each RTT until it reaches the slow-start threshold (ssthresh) or packet loss occurs. Despite the name, exponential growth is fast.

๐Ÿ“ˆ

Congestion Avoidance

Once cwnd reaches ssthresh, it grows linearly (additive increase) โ€” by 1 MSS per RTT โ€” until congestion is detected. This is the AIMD (Additive Increase, Multiplicative Decrease) phase.

๐Ÿ“‰

Multiplicative Decrease

On packet loss (timeout): ssthresh = cwnd/2, cwnd = 1 MSS. On triple duplicate ACK (fast retransmit): ssthresh = cwnd/2, cwnd = ssthresh. Less aggressive than timeout response.

โšก

Fast Retransmit

When 3 duplicate ACKs are received for the same sequence number, TCP retransmits the missing segment immediately โ€” without waiting for the retransmission timer to expire.

๐Ÿง 

CUBIC (Linux Default)

Grows cwnd as a cubic function of time since last congestion event. Better for high-bandwidth, high-latency networks (long fat networks). Less aggressive on short RTT paths.

๐ŸŒŠ

BBR (Google)

Bottleneck Bandwidth and Round-trip propagation time. Models the network path to estimate available bandwidth โ€” doesn't rely on packet loss as signal. Widely used in YouTube/Google services.

Bandwidth-Delay Product Calculator

The BDP tells you how many bytes need to be "in flight" to saturate a link. Your TCP window must be at least this large.

100 Mbps
50 ms
BDP Analysis

6 ยท TCP Header Builder

Construct a TCP segment header. As you modify fields, the hex output and visual representation update in real time.

๐Ÿ› ๏ธ TCP Header Constructor All values update in real time

Common flag presets:

๐Ÿ“ฆ Generated TCP Header โ€” Hexadecimal Representation
๐Ÿ“‹ Field Summary
๐Ÿšฉ Flags Bit Map (CWR ECE URG ACK PSH RST SYN FIN)

7 ยท TCP vs UDP

Understanding when to use TCP versus UDP is a critical skill for network administrators and application developers.

TCP โ€” Transmission Control Protocol
ConnectionConnection-oriented
ReliabilityGuaranteed delivery
OrderingIn-order delivery
Header20โ€“60 bytes
SpeedLower (overhead)
Flow ControlYes (sliding window)
Congestion ControlYes (CUBIC, BBR)
Use CasesHTTP, SSH, FTP, SMTP, DB
UDP โ€” User Datagram Protocol
ConnectionConnectionless
ReliabilityBest-effort
OrderingNot guaranteed
Header8 bytes only
SpeedHigher (minimal overhead)
Flow ControlNo
Congestion ControlNo (app responsibility)
Use CasesDNS, VoIP, Gaming, QUIC, Video
โšก Modern Note: HTTP/3 uses QUIC โ€” a UDP-based protocol developed by Google that implements reliability, ordering, and multiplexing at the application layer. It avoids TCP's head-of-line blocking and achieves faster connection setup (0-RTT). TCP is still dominant but QUIC is rapidly growing.

8 ยท Knowledge Check

Test your understanding of TCP concepts. Click an answer to see instant feedback.