Part I: Network Theory Chapter 5

Transport Layer Protocols

TCP connections, UDP datagrams, QUIC, ports, sockets, congestion control, and reliability mechanisms

Chapter 5: Transport Layer Protocols

The Protocol That Changed Streaming Forever

In 2012, Google engineers faced a problem: despite decades of optimization, web page loads still felt sluggish. The culprit wasn’t bandwidthβ€”it was TCP’s fundamental design. Every new connection required a three-way handshake (one round trip). TLS added another two round trips. A single lost packet could stall the entire connection due to head-of-line blocking.

Their solution was radical: abandon TCP entirely. Build a new protocol from scratch, optimizing for modern internet conditions. They called it QUIC (Quick UDP Internet Connections).

By 2023, QUIC powered over 25% of all internet traffic. Google, Facebook, and Cloudflare use it extensively. HTTP/3 is built on QUIC. It reduces connection establishment from 2-3 round trips to zero (with session resumption) and eliminates head-of-line blocking entirely.

This chapter explores the Transport layerβ€”TCP, UDP, and the revolutionary QUIC protocol that’s reshaping the internet.


End-to-End Communication

The Network layer (IP) handles getting packets from one host to another across the internet. But a host typically runs many applications simultaneouslyβ€”your web browser, email client, music streaming service, and more. How does incoming data reach the right application? And how do we ensure data arrives reliably and in order?

The Transport layer answers these questions. It provides end-to-end communication between applications, not just between hosts. Through port numbers, it multiplexes multiple application conversations over a single IP connection. Through mechanisms like acknowledgments and retransmission, it can provide reliability over an inherently unreliable network.

Three protocols dominate this layer:

  • TCP - Reliable, ordered delivery
  • UDP - Fast, connectionless communication
  • QUIC - Modern encrypted transport (TCP replacement for HTTP/3)

Ports and Sockets

Port Numbers

A port number is a 16-bit integer (0-65535) that identifies a specific application or service on a host. When data arrives at a host, the Transport layer uses the destination port to determine which application should receive it.

Think of IP addresses as street addresses and port numbers as apartment numbers. The IP gets the packet to the building; the port gets it to the right unit.

Transport Layer Addressing

Transport Layer Addressing:
═══════════════════════════════════════════════════════════════════

    Your Computer                        Web Server
    192.168.1.100                       93.184.216.34
    
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”             β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ Browser      :52431 │────────────►│ Web Server     :443 β”‚
    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€             β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
    β”‚ Email Client :52432 │────────────►│ Mail Server     :25 β”‚
    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€             β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
    β”‚ SSH Client   :52433 │────────────►│ SSH Server      :22 β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜             β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
    
    Client uses ephemeral ports         Server uses well-known ports
    (49152-65535, random)               (0-1023, fixed)

Port ranges:

RangeNameDescription
0-1023Well-KnownReserved for standard services (requires root/admin)
1024-49151RegisteredAssigned by IANA for specific applications
49152-65535Dynamic/EphemeralUsed for client-side connections

Common well-known ports (memorize these):

PortProtocolServiceSecurity Notes
20, 21TCPFTPUnencrypted, use SFTP instead
22TCPSSHSecure remote access
23TCPTelnetUnencrypted, deprecated
25TCPSMTPEmail transfer (often filtered)
53TCP/UDPDNSCritical infrastructure target
67, 68UDPDHCPRogue server attacks
80TCPHTTPUnencrypted web
110TCPPOP3Legacy email retrieval
143TCPIMAPEmail access
443TCP/UDPHTTPS/QUICEncrypted web
445TCPSMBWindows file sharing (common attack target)
3306TCPMySQLShould never be exposed
3389TCPRDPRemote desktop (brute force target)
5432TCPPostgreSQLDatabase
8080TCPHTTP AltCommon for proxies/alt web

Sockets

A socket is the combination of an IP address and port number, uniquely identifying an endpoint in a network conversation. A connection between two applications is identified by four values: source IP, source port, destination IP, and destination port.

Socket and Connection Identification

Socket and Connection Identification:
═══════════════════════════════════════════════════════════════════

Socket = IP Address : Port Number

Client Socket: 192.168.1.100:52431
Server Socket: 93.184.216.34:443

Connection (5-tuple with protocol):
(TCP, 192.168.1.100, 52431, 93.184.216.34, 443)

Multiple connections to same server use different source ports:
Browser Tab 1: (TCP, 192.168.1.100, 52431, 93.184.216.34, 443)
Browser Tab 2: (TCP, 192.168.1.100, 52432, 93.184.216.34, 443)
Browser Tab 3: (TCP, 192.168.1.100, 52433, 93.184.216.34, 443)

Each is a unique connection despite same destination!

Security Note: Port scanning is a fundamental reconnaissance technique. By probing ports, attackers discover which services are running on a target. Open ports represent potential entry points. We’ll explore scanning techniques in Part II, Chapters 4 and 9.


TCP: Reliable Transport

Why Reliability Matters

IP is an unreliable protocolβ€”packets can be lost, duplicated, corrupted, or arrive out of order. For many applications, this is unacceptable. When you download a file or load a web page, you need all the data, in the correct order, without errors.

TCP (Transmission Control Protocol) provides:

  • Reliable delivery: Lost packets are detected and retransmitted
  • Ordered delivery: Data arrives in the sequence it was sent
  • Error detection: Corrupt data is detected and rejected
  • Flow control: Prevents overwhelming slow receivers
  • Congestion control: Prevents overwhelming the network

The TCP Three-Way Handshake

TCP is connection-orientedβ€”before sending data, client and server establish a connection through the three-way handshake. This synchronizes sequence numbers and confirms both parties are ready.

TCP ThreeWay Handshake

TCP Three-Way Handshake:
═══════════════════════════════════════════════════════════════════

    Client                                            Server
       β”‚                                                 β”‚
       β”‚  1. SYN                                         β”‚
       β”‚     Seq=100, SYN flag set                       β”‚
       β”‚     "I want to connect, my ISN is 100"          β”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
       β”‚                                                 β”‚
       β”‚  2. SYN-ACK                                     β”‚
       β”‚     Seq=300, Ack=101, SYN+ACK flags             β”‚
       β”‚     "OK, my ISN is 300, expecting your 101"     β”‚
       │◄─────────────────────────────────────────────────
       β”‚                                                 β”‚
       β”‚  3. ACK                                         β”‚
       β”‚     Seq=101, Ack=301, ACK flag                  β”‚
       β”‚     "Got it, expecting your 301"                β”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
       β”‚                                                 β”‚
       β”‚         Connection Established!                 β”‚
       │◄═══════════════════════════════════════════════►│
       β”‚                                                 β”‚

Why this matters:
- Synchronizes initial sequence numbers (ISN)
- Confirms both sides can send and receive
- ISNs should be random (security against prediction attacks)

What each step does:

  1. SYN (Synchronize): Client sends a segment with the SYN flag set and its Initial Sequence Number (ISN). This ISN should be random to prevent sequence number prediction attacks.

  2. SYN-ACK: Server responds with SYN and ACK flags, its own ISN, and an acknowledgment of the client’s ISN+1.

  3. ACK: Client acknowledges the server’s ISN+1. Connection is now established.

Security Note: The three-way handshake is vulnerable to SYN flood attacks. An attacker sends many SYN packets with spoofed source addresses. The server allocates resources for each half-open connection and waits for the final ACK that never comes, eventually exhausting memory. See Part II, Chapter 4.

TCP Header Structure

TCP Header Format (20 bytes minimum, up to 60 with options)

TCP Header Format (20 bytes minimum, up to 60 with options):
═══════════════════════════════════════════════════════════════════

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
β”œβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”€
β”‚          Source Port          β”‚       Destination Port        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                       Sequence Number                         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                    Acknowledgment Number                      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”¬β”€β”¬β”€β”¬β”€β”¬β”€β”¬β”€β”¬β”€β”¬β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚Data   β”‚       β”‚Cβ”‚Eβ”‚Uβ”‚Aβ”‚Pβ”‚Rβ”‚Sβ”‚Fβ”‚                               β”‚
β”‚Offset β”‚ Res   β”‚Wβ”‚Cβ”‚Rβ”‚Cβ”‚Sβ”‚Sβ”‚Yβ”‚Iβ”‚          Window               β”‚
β”‚4 bits β”‚ 4 bitsβ”‚Rβ”‚Eβ”‚Gβ”‚Kβ”‚Hβ”‚Tβ”‚Nβ”‚Nβ”‚          16 bits              β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”΄β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚          Checksum             β”‚        Urgent Pointer         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                    Options (if Data Offset > 5)               β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                             Data                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key fields:

FieldSizeDescription
Source Port16 bitsSending application’s port
Destination Port16 bitsReceiving application’s port
Sequence Number32 bitsPosition of first data byte in this segment
Acknowledgment Number32 bitsNext expected byte from sender
Data Offset4 bitsHeader length in 32-bit words
Flags9 bitsControl bits (see below)
Window16 bitsReceive buffer space available
Checksum16 bitsError detection
Urgent Pointer16 bitsUrgent data offset (rarely used)

TCP Flags and their meanings:

FlagNamePurposeSecurity Relevance
SYNSynchronizeInitiate connectionSYN floods, scanning
ACKAcknowledgeAcknowledgment field validSession hijacking
FINFinishSender finished sendingConnection teardown
RSTResetAbort connectionRST injection attacks
PSHPushDeliver data immediately-
URGUrgentUrgent pointer validRarely used, some evasion
ECEECN EchoCongestion notification-
CWRCongestion Window ReducedSender slowing down-

Sequence Numbers and Acknowledgments

TCP tracks data using byte sequence numbers. Each byte of data has a sequence number, and acknowledgments indicate the next expected byte.

TCP Data Transfer with Sequence Numbers

TCP Data Transfer with Sequence Numbers:
═══════════════════════════════════════════════════════════════════

Client sends 1000 bytes:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ SEQ=100 β”‚ Data: 1000 bytes                                     β”‚
β”‚         β”‚ (Bytes 100-1099)                                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                                    β–Ό
Server acknowledges:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ ACK=1100 β”‚ "I received up to byte 1099"                        β”‚
β”‚          β”‚ "Send me byte 1100 next"                            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                                    β–Ό
Client sends more:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ SEQ=1100 β”‚ Data: 500 bytes                                     β”‚
β”‚          β”‚ (Bytes 1100-1599)                                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                    β”‚
                                    β–Ό
Server acknowledges:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ ACK=1600 β”‚ "Got it, send byte 1600 next"                       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

This mechanism enables:

  • Loss detection: If an ACK doesn’t arrive in time, retransmit
  • Duplicate detection: Receiving same sequence number twice indicates duplicate
  • Ordering: Reassemble out-of-order segments using sequence numbers

Flow Control: Sliding Window

TCP’s sliding window mechanism prevents a fast sender from overwhelming a slow receiver. The receiver advertises its available buffer space in the Window field of each ACK.

TCP Sliding Window

TCP Sliding Window:
═══════════════════════════════════════════════════════════════════

Sender's view of sequence number space:

    ◄─── Sent & ACKed ──►◄─── Sent, not ACKed ──►◄── Can send ──►
    β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”
    β”‚ 1  β”‚ 2  β”‚ 3  β”‚ 4  β”‚ 5  β”‚ 6  β”‚ 7  β”‚ 8  β”‚ 9  β”‚ 10 β”‚ 11 β”‚ 12 β”‚
    β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”˜
                        β–²                        β–²
                        β”‚                        β”‚
                   Window start             Window end
                   (Last ACK received)    (Start + Window size)

Window shrinks when receiver buffer fills:
Receiver: "Window = 0"  (Stop sending, I'm full!)

Window grows when receiver processes data:
Receiver: "Window = 8000"  (OK, you can send more)

Congestion Control

Beyond flow control (protecting the receiver), TCP implements congestion control to protect the network. The internet would collapse without itβ€”every sender would blast traffic until packets dropped everywhere.

TCP Congestion Control Phases

TCP Congestion Control Phases:
═══════════════════════════════════════════════════════════════════

cwnd                                   Packet loss!
(KB)                                   cwnd cut in half
  β”‚                                         β”‚
64β”œ                              β—β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  β”‚                             β•±β”‚β•²  
32β”œ ─ ─ ─ ─ ─ ─ ─ ─ ─●─ ─ ─ ─ ╱─ β”‚ β•² ─ ─ ─ ─ ─ ssthresh
  β”‚                 β•±β”‚       β•±   β”‚  β•²
16β”œ               β•±  β”‚      β•±    β–Ό   β•²
  β”‚              β•±   β”‚     β•±          β•²
 8β”œ            β•±     β”‚    β•±    Slow    β•²
  β”‚           β•±      β”‚   β•±     Start    β•²        β•±
 4β”œ         β•±        β”‚  β•±     (again)    ●──────╱
  β”‚        β•±         β”‚ β•±                       β•±
 2β”œ      β•±           β”‚β•±                       β•±
  β”‚     β•±            β•±                       β•±
 1β”œβ”€β”€β”€β”€β—            β•±                       β•±
  β”‚    β”‚           β•±                       β•±
  └────┴───────────┴───────────────────────┴────────────► Time
       β”‚           β”‚                       β”‚
       β”‚           β”‚                       β”‚
   Slow Start   Congestion            Congestion
  (exponential) Avoidance             Avoidance
   1β†’2β†’4β†’8β†’16   (linear +1/RTT)       (resumes)

Phase 1: SLOW START        - cwnd doubles each RTT (exponential)
Phase 2: CONGESTION AVOID  - cwnd += 1 MSS per RTT (linear) 
Phase 3: LOSS DETECTED     - cwnd cut to half (or to 1 for timeout)
Phase 4: RECOVERY          - Fast recovery or slow start again

Algorithms:
- Slow Start: cwnd doubles every RTT (exponential growth)
- Congestion Avoidance: cwnd increases by 1 MSS per RTT (linear)
- Fast Retransmit: Retransmit on 3 duplicate ACKs
- Fast Recovery: Don't start over after fast retransmit

Modern congestion control algorithms:

AlgorithmDescriptionUse Case
RenoClassic, conservativeLegacy
CUBICDefault Linux, aggressiveGeneral internet
BBRGoogle, bandwidth-basedHigh bandwidth, lossy
DCTCPData center optimizedLow latency DC

THINK ABOUT IT

Why is congestion control a β€œtragedy of the commons” problem? What would happen if some senders ignored congestion control while others implemented it faithfully?

TCP Connection Termination

Connections are terminated gracefully using a four-way process:

TCP Connection Termination (4way)

TCP Connection Termination (4-way):
═══════════════════════════════════════════════════════════════════

    Client                                            Server
       β”‚                                                 β”‚
       β”‚  1. FIN                                         β”‚
       β”‚     "I'm done sending"                          β”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
       β”‚                                                 β”‚
       β”‚  2. ACK                                         β”‚
       β”‚     "OK, got your FIN"                          β”‚
       │◄─────────────────────────────────────────────────
       β”‚                                                 β”‚
       β”‚         (Server may still send data)            β”‚
       β”‚                                                 β”‚
       β”‚  3. FIN                                         β”‚
       β”‚     "I'm done sending too"                      β”‚
       │◄─────────────────────────────────────────────────
       β”‚                                                 β”‚
       β”‚  4. ACK                                         β”‚
       β”‚     "OK, goodbye"                               β”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
       β”‚                                                 β”‚
       β”‚  TIME_WAIT (2*MSL)                              β”‚
       β”‚  Client waits before fully closing              β”‚
       β”‚  (handles delayed/duplicate packets)            β”‚
       β”‚                                                 β”‚

       /                                                 /

Or abrupt termination:
       β”‚  RST                                            β”‚
       β”‚     "Connection killed immediately"             β”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚

Security Note: RST packets can be injected by attackers who can guess sequence numbers, causing connection disruption. This is the basis of TCP Reset attacks. See Part II, Chapter 4.


UDP: Speed Over Reliability

When Reliability Isn’t Needed

Not all applications need TCP’s guarantees. Video streaming can tolerate occasional lost framesβ€”retransmitting would cause delays worse than the loss. DNS queries are simple request-response pairs that can be easily retried. Real-time games need low latency more than perfect reliability.

UDP (User Datagram Protocol) provides minimal transport services:

  • Port-based multiplexing
  • Optional checksum
  • That’s it.

No connections, no acknowledgments, no retransmission, no ordering, no flow control. Applications that use UDP must handle these concerns themselves if needed.

UDP Header Structure

The UDP header is remarkably simpleβ€”just 8 bytes:

UDP Header Format (8 bytes)

UDP Header Format (8 bytes):
═══════════════════════════════════════════════════════════════════

 0                   1                   2                   3
 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
β”œβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”Όβ”€β”€
β”‚          Source Port          β”‚       Destination Port        β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚            Length             β”‚           Checksum            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                             Data                              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Compare to TCP's 20+ bytes header!
UDP prioritizes speed over features.
FieldSizeDescription
Source Port16 bitsSending application’s port
Destination Port16 bitsReceiving application’s port
Length16 bitsTotal datagram size (header + data)
Checksum16 bitsOptional in IPv4, mandatory in IPv6

UDP Use Cases

ApplicationWhy UDP?
DNSSimple queries; easy to retry if lost
DHCPBootstrap protocol; TCP handshake impractical
VoIP/Video callingReal-time; latency more important than perfection
Video StreamingLoss tolerable; retransmission would cause buffering
Online GamingLow latency critical; application handles reliability
NTPTime synchronization; simple request-response
QUICBuilds own reliability on top of UDP
IoT/SensorsResource-constrained devices

UDP Security Considerations

UDP’s connectionless nature creates security challenges:

Source Address Spoofing: UDP has no handshake to verify the source address. Attackers can easily send UDP packets with forged source IPs.

Amplification Attacks: Some UDP services respond with much more data than the request. Attackers send small requests with spoofed source IPs; the large responses flood the victim.

UDP Amplification Attack

UDP Amplification Attack:
═══════════════════════════════════════════════════════════════════

                         Attacker
                            β”‚
    1. Small UDP request    β”‚   Source IP: Victim (spoofed!)
       to vulnerable server β”‚   "Get me all your DNS records"
       (e.g., DNS query)    β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚  DNS Server   β”‚
                    β”‚  (Reflector)  β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚
    2. Large UDP response   β”‚   "Here's 50x more data than
       to spoofed source    β”‚    you asked for!"
       (the victim)         β–Ό
                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚    Victim     β”‚  ← Flooded with responses
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Amplification factors:
DNS:      28-54x
NTP:      556x (monlist)
Memcached: 51,000x (!!)
SSDP:     30x

Security Note: UDP-based DDoS attacks are a major threat. DNS amplification, NTP amplification, and memcached amplification have powered some of the largest attacks in history (over 2 Tbps). See Part II, Chapter 7.


QUIC: The Modern Transport

Why QUIC Was Created

TCP served the internet well for decades, but its design assumptions don’t match modern reality:

TCP LimitationImpactQUIC Solution
Handshake latency1-3 RTT to start0-1 RTT
Head-of-line blockingOne lost packet stalls all dataIndependent streams
OssificationMiddleboxes interfere with changesEncrypted transport
No built-in encryptionTLS added separatelyTLS 1.3 integrated
Connection tied to IPMobile networks break connectionsConnection IDs

QUIC Architecture

QUIC vs Traditional Stack

QUIC vs Traditional Stack:
═══════════════════════════════════════════════════════════════════

Traditional HTTPS:                   QUIC/HTTP3:
──────────────────                  ────────────

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     HTTP/2      β”‚                 β”‚     HTTP/3      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€                 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚      TLS        β”‚                 β”‚                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€                 β”‚      QUIC       β”‚
β”‚      TCP        β”‚                 β”‚                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€                 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚       IP        β”‚                 β”‚      UDP        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                 β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
                                    β”‚       IP        β”‚
                                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

QUIC combines:
- Transport (reliability, flow control)
- Encryption (TLS 1.3)
- Multiplexing (streams)
- Connection migration

All encrypted, no middlebox interference!

QUIC Key Features

1. Faster Connection Establishment

TCP TLS 1.3 QUIC

TCP + TLS 1.3:                      QUIC:
══════════════                      ═════

Client          Server              Client          Server
   β”‚    SYN        β”‚                   β”‚               β”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚                   β”‚   Initial     β”‚
   β”‚   SYN-ACK     β”‚                   β”‚   (crypto)    β”‚
   │◄───────────────                   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
   β”‚    ACK        β”‚                   β”‚   Handshake   β”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚                   │◄───────────────
   β”‚               β”‚                   β”‚   ACK+Data    β”‚
   β”‚  ClientHello  β”‚                   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚                   β”‚               β”‚
   β”‚  ServerHello  β”‚
   β”‚  + cert + etc β”‚                   1 RTT (or 0 with resumption!)
   │◄───────────────
   β”‚  Finished     β”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
   β”‚               β”‚
   
   3 RTT before data

2. Stream Multiplexing Without Head-of-Line Blocking

HTTP/2 over TCP (Headofline blocking)

HTTP/2 over TCP (Head-of-line blocking):
════════════════════════════════════════

Stream 1: β–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘  (waiting for lost packet)
Stream 2: β–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘  (blocked even though its data arrived!)
Stream 3: β–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘  (blocked even though its data arrived!)
                β”‚
                └── One lost packet blocks ALL streams

QUIC (Independent streams):
═══════════════════════════

Stream 1: β–ˆβ–ˆβ–ˆβ–ˆβ–‘β–‘β–‘β–‘β–‘β–‘  (waiting for lost packet)
Stream 2: β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ    (continues normally!)
Stream 3: β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ  (continues normally!)
                β”‚
                └── Loss only affects the specific stream

3. Connection Migration

Mobile scenario

Mobile scenario:
════════════════

With TCP:
Phone on WiFi ────────────► Server
   (IP: 192.168.1.50)         β”‚
                              β”‚
Phone moves to cellular       β”‚
   (IP: 10.0.0.50)            β”‚
                              β”‚
   Connection BROKEN!  βœ—      β”‚
   New 3-way handshake needed β”‚

With QUIC:
Phone on WiFi ────────────► Server
   (Connection ID: ABC123)    β”‚
                              β”‚
Phone moves to cellular       β”‚
   (Same Connection ID)       β”‚
                              β”‚
   Connection CONTINUES!      β”‚
   Zero interruption          β”‚

QUIC Security Implications

Advantages:

  • All packets encrypted (except first byte)
  • No cleartext metadata for middleboxes to inspect
  • TLS 1.3 integration = modern cryptography
  • Connection IDs prevent tracking by IP

Challenges:

  • Encrypted traffic harder to inspect for security
  • Traditional firewalls/IDS less effective
  • Network troubleshooting more difficult
  • New protocol = new potential vulnerabilities

PRO TIP

QUIC uses UDP port 443. If you’re blocking QUIC, block UDP 443. However, browsers will fall back to TCP, so this may not achieve your goal. Better to use application-layer controls.


TCP vs UDP vs QUIC Comparison

FeatureTCPUDPQUIC
ConnectionYes (3-way)NoYes (0-1 RTT)
ReliabilityGuaranteedBest effortGuaranteed
OrderingPreservedNot guaranteedPer-stream
Flow ControlYesNoYes
Congestion ControlYesNoYes
EncryptionOptional (TLS)NoBuilt-in (TLS 1.3)
Header Size20+ bytes8 bytesVariable (encrypted)
Head-of-line BlockingYesN/ANo
Connection MigrationNoN/AYes
Use CasesWeb, email, file transferStreaming, DNS, gamingHTTP/3, modern apps

Port Scanning: Reconnaissance Primer

Port scanning probes a target to discover which services are running. Different scan types use TCP and UDP behavior in clever ways.

TCP Connect Scan

The simplest scanβ€”complete the three-way handshake. If it succeeds, the port is open.

TCP Connect Scan

TCP Connect Scan:
═══════════════════════════════════════════════════════════════════

Port OPEN:                          Port CLOSED:
──────────                         ────────────

Scanner         Target              Scanner         Target
   β”‚    SYN        β”‚                   β”‚    SYN        β”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚                   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
   β”‚   SYN-ACK     β”‚                   β”‚    RST        β”‚
   │◄───────────────  OPEN             │◄─────────────── βœ— CLOSED
   β”‚    ACK        β”‚                   β”‚               β”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
   β”‚    RST        β”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
   (Close connection)

Pros: Reliable, works without special privileges Cons: Easily logged (complete connections are suspicious)

TCP SYN Scan (Half-Open)

Send SYN, but don’t complete the handshake. Less likely to be logged.

TCP SYN Scan (Stealthy)

TCP SYN Scan (Stealthy):
═══════════════════════════════════════════════════════════════════

Port OPEN:                          Port CLOSED:
──────────                         ────────────

Scanner         Target              Scanner         Target
   β”‚    SYN        β”‚                   β”‚    SYN        β”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚                   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
   β”‚   SYN-ACK     β”‚                   β”‚    RST        β”‚
   │◄───────────────  OPEN             │◄─────────────── βœ— CLOSED
   β”‚    RST        β”‚                   β”‚               β”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
   (Never complete handshake)

No full connection = less likely to be logged
(But modern IDS will still detect this)

Pros: Stealthier, faster Cons: Requires raw socket privileges (root/admin)

Other TCP Scan Types

Scan TypePacket SentOpen Port ResponseClosed Port Response
FINFIN flag onlyNo responseRST
XMASFIN+PSH+URGNo responseRST
NULLNo flagsNo responseRST
ACKACK flag onlyRSTRST (but different)
WindowACK flagDifferent window values-

These β€œstealth” scans exploit RFC-defined behavior: closed ports should respond to invalid packets with RST, while open ports should silently ignore them. Modern firewalls often detect these.

UDP Scanning

UDP scanning is trickierβ€”there’s no handshake to detect open ports:

UDP Scan

UDP Scan:
═══════════════════════════════════════════════════════════════════

Port OPEN:                          Port CLOSED:
──────────                         ────────────

Scanner         Target              Scanner         Target
   β”‚  UDP packet   β”‚                   β”‚  UDP packet   β”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚                   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
   β”‚   (silence)   β”‚                   β”‚  ICMP Port    β”‚
   β”‚               β”‚ ? OPEN/FILTERED   β”‚  Unreachable  β”‚
   β”‚               β”‚                   │◄─────────────── βœ— CLOSED

No response = open OR filtered (can't tell!)
ICMP response = definitely closed
UDP scans are SLOW (ICMP rate limiting)

Security Note: Port scanning is covered extensively in Part II, Chapter 9. Tools like Nmap automate these techniques and add OS fingerprinting, service version detection, and more.


Practical Commands

# View listening ports and connections
ss -tuln                   # Linux (modern, faster)
netstat -tuln              # Linux (legacy)
netstat -an                # macOS/Windows

# View established connections
ss -t state established    # Linux
netstat -an | grep ESTABLISHED

# Check specific port
nc -zv host port           # Test TCP port
nc -zuv host port          # Test UDP port

# Capture TCP traffic
tcpdump -i eth0 tcp port 80
sudo tcpdump -i any 'tcp[tcpflags] & (tcp-syn) != 0'  # SYN packets only

# Watch TCP connection states
watch -n 1 'ss -t'

# View per-connection statistics
ss -ti                     # Detailed TCP info with RTT, cwnd, etc.

# Check for QUIC traffic
tcpdump -i eth0 'udp port 443'

# Nmap port scanning (covered in Part II)
nmap -sT target            # TCP connect scan
nmap -sS target            # SYN scan (requires root)
nmap -sU target            # UDP scan (slow)
nmap -sV target            # Service version detection

TRY IT YOURSELF

Compare TCP states during a web request:

# Terminal 1: Watch connections
watch -n 0.5 'ss -t state all | grep :443'

# Terminal 2: Make a request
curl https://example.com

You’ll see the connection go through SYN-SENT, ESTABLISHED, and TIME-WAIT states.


Key Takeaways

  1. Ports identify applications; combined with IPs, sockets identify connection endpoints

  2. TCP provides reliable, ordered, connection-oriented communication through handshakes, acknowledgments, and retransmissionβ€”but has latency costs

  3. UDP provides fast, connectionless communication with minimal overheadβ€”but no reliability guarantees

  4. QUIC combines the best of both: reliability like TCP, speed improvements, built-in encryption, and stream multiplexing without head-of-line blocking

  5. Congestion control prevents network collapse; different algorithms optimize for different scenarios

  6. Port scanning exploits TCP/UDP behavior to discover servicesβ€”understanding the protocols helps understand the scans


Self-Assessment

  1. Comprehension: Why does TCP need a three-way handshake while UDP doesn’t need any handshake?

  2. Application: A video streaming service experiences buffering when packets are lost. Should they use TCP, UDP, or QUIC? Why?

  3. What if: If an attacker can observe (but not modify) your TCP traffic, what information can they learn? What if the traffic is QUIC?


Review Questions

  1. How does TCP’s three-way handshake work, and why is it necessary?
  2. What advantages does UDP offer over TCP, and when would you choose UDP?
  3. How do sequence numbers and acknowledgments enable reliability in TCP?
  4. Why are SYN floods effective, and what resource do they exhaust?
  5. What problems does QUIC solve that TCP couldn’t?
  6. How does a TCP SYN scan differ from a TCP connect scan?

Key RFCs

  • RFC 9293 - Transmission Control Protocol (TCP) - Latest consolidation
  • RFC 768 - User Datagram Protocol (UDP)
  • RFC 9000 - QUIC: A UDP-Based Multiplexed and Secure Transport
  • RFC 9001 - Using TLS to Secure QUIC
  • RFC 9114 - HTTP/3
  • RFC 6335 - IANA Procedures for Port Assignment