Skip to content
BoKSA

01: Reliable Data Transfer and TCP

01: Reliable Data Transfer and TCP

Course: Networking - Security Advanced · Updated: October 2026 Literature: K&R chapter 3 (Transport Layer): reliable data transfer, pipelined protocols, TCP, congestion control Related lab: Lab: Reliable data transfer and TCP

Learning objectives

After studying this article you can:

  • explain why a reliable transport protocol is needed on top of an unreliable network layer
  • explain the mechanisms used for reliability: checksums, acknowledgements, sequence numbers, timers and retransmission
  • explain pipelining and compare Go-Back-N with Selective Repeat
  • describe the TCP segment, the three-way handshake, connection teardown and the TCP state machine
  • explain TCP flow control and TCP congestion control (slow start, congestion avoidance, fast recovery)
  • describe how TCP has evolved (CUBIC, BBR, ECN) and why QUIC exists

1. The problem: an unreliable network

The Internet's network layer (IP) is best effort. A packet can be:

  • corrupted: bits flipped by noise or faulty hardware
  • lost: dropped by a router whose queue is full
  • reordered: different packets take different paths
  • duplicated: for example by a retransmission

Applications such as web browsing, e-mail and file transfer still need every byte delivered correctly and in order. The transport layer bridges that gap. The reliability mechanisms live in the end hosts, not in the network. This is the end-to-end principle.

2. Building a reliable protocol step by step

K&R build a reliable data transfer protocol (rdt) step by step. Each version solves one more problem:

Version Assumed channel New mechanism
rdt1.0 Perfectly reliable None needed
rdt2.0 Bit errors Checksum to detect errors, ACK/NAK feedback, retransmission
rdt2.1 / 2.2 Bit errors, and ACKs can also be corrupted Sequence numbers (0/1) to detect duplicates. rdt2.2 uses duplicate ACKs instead of NAKs.
rdt3.0 Bit errors and loss Countdown timer: retransmit if no ACK arrives in time

rdt3.0 is a correct stop-and-wait protocol (also called the alternating-bit protocol), but it is slow.

Why stop-and-wait is slow

Take a 1 Gbit/s link with a round-trip time (RTT) of 30 ms and 1,000-byte (8,000-bit) packets:

1
2
transmission time  d_trans = L / R = 8000 bit / 10^9 bit/s = 8 µs
utilisation        U = d_trans / (RTT + d_trans) = 0.008 / 30.008 ≈ 0.00027

The sender is busy only 0.027% of the time. The link is idle while the sender waits for an ACK.

3. Pipelining

The fix is to let the sender have multiple unacknowledged packets in flight. This is pipelining. It requires:

  • a larger range of sequence numbers
  • buffering at the sender (and possibly at the receiver)

With a window of N packets, the utilisation is about N times higher. The two classic pipelined protocols handle errors differently.

Go-Back-N (GBN)

  • The sender may have up to N unacknowledged packets. This is the window.
  • The receiver sends cumulative ACKs. "ACK n" means "everything up to and including n has arrived".
  • The receiver discards out-of-order packets. It has no receive buffer.
  • There is a single timer for the oldest unacknowledged packet. On timeout, the sender resends all packets in the window.
1
2
3
4
5
Sender window (N = 4)
 [0][1][2][3] 4  5  6 ...
  |  |  X  |            packet 2 is lost
  ACK0 ACK1  ACK1 ACK1   receiver discards 3, keeps sending ACK1
  timeout for 2 → resend 2, 3 (everything from 2 onwards)

GBN is simple, but one loss can cause many unnecessary retransmissions.

Selective Repeat (SR)

  • The receiver acknowledges each packet individually and buffers out-of-order packets.
  • The sender keeps a timer per packet and retransmits only the packet that timed out.
  • The window size must be at most half the sequence-number space. Otherwise the receiver can't tell a new packet from a retransmission of an old one.
Go-Back-N Selective Repeat
ACKs Cumulative Individual
Receiver buffer No (out-of-order packets are discarded) Yes
Timers One (oldest packet) One per packet
Retransmission after a loss The whole window from the lost packet onwards Only the lost packet
Complexity Low Higher

Try it: the K&R website has interactive GBN and SR animations: https://gaia.cs.umass.edu/kurose_ross/interactive/

4. TCP: the Internet's reliable transport

TCP (RFC 9293, which in 2022 replaced the original RFC 793 from 1981) combines ideas from GBN and SR:

  • Cumulative ACKs, as in GBN
  • Buffering of out-of-order segments at the receiver, as in SR
  • Selective Acknowledgements (SACK, RFC 2018), an option that tells the sender exactly which blocks arrived. SACK is enabled by default on all modern operating systems.
  • Byte-oriented sequence numbers: the sequence number is the number of the first byte in the segment.

4.1 The TCP segment

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 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                      |
+-------+-------+-+-+-+-+-+-+-+-+-------------------------------+
| Offset| Rsrvd |C|E|U|A|P|R|S|F|            Window             |
|       |       |W|C|R|C|S|S|Y|I|                               |
|       |       |R|E|G|K|H|T|N|N|                               |
+-------+-------+-+-+-+-+-+-+-+-+-------------------------------+
|           Checksum            |         Urgent Pointer        |
+-------------------------------+-------------------------------+
|                    Options (MSS, SACK, Window scale, Timestamps)
+---------------------------------------------------------------+

The important fields are:

  • Sequence / Acknowledgement number: byte positions in the stream
  • Flags: SYN (set up), ACK, FIN (close gracefully), RST (abort), PSH, URG, and ECE/CWR for Explicit Congestion Notification
  • Window: how many bytes the receiver can still accept (flow control)
  • Options: Maximum Segment Size (MSS), window scaling (RFC 7323), SACK, timestamps

4.2 Connection management

Three-way handshake:

1
2
3
4
Client                              Server
  | ---- SYN, seq=x --------------->  |   (LISTEN → SYN-RCVD)
  | <--- SYN+ACK, seq=y, ack=x+1 ---  |
  | ---- ACK, ack=y+1 ------------->  |   (ESTABLISHED)

Both sides choose a random Initial Sequence Number (ISN). A predictable ISN lets an attacker inject or hijack connections (see the Cyber Security section).

Teardown: each side sends a FIN and receives an ACK, so there are usually four segments. The side that closes first waits in TIME_WAIT for 2 × MSL (Maximum Segment Lifetime). This makes sure old duplicate segments have died out.

4.3 Round-trip time and timeout

TCP keeps an exponentially weighted moving average of the measured RTT (RFC 6298):

1
2
3
EstimatedRTT = (1 − α) · EstimatedRTT + α · SampleRTT        (α = 0.125)
DevRTT       = (1 − β) · DevRTT + β · |SampleRTT − EstimatedRTT|   (β = 0.25)
TimeoutInterval = EstimatedRTT + 4 · DevRTT

Fast retransmit: when the sender receives three duplicate ACKs, it retransmits the missing segment straight away, without waiting for the timeout.

4.4 Flow control

Flow control stops the sender from overflowing the receiver's buffer. The receiver advertises its free buffer space in the rwnd (Window) field. The sender makes sure that:

1
LastByteSent − LastByteAcked ≤ rwnd

The window field is only 16 bits (max 64 KB), so modern TCP uses the window scale option to allow windows of up to 1 GB.

4.5 Congestion control

Congestion control stops the sender from overflowing the network. The sender keeps a congestion window cwnd, and the amount of data in flight is min(cwnd, rwnd).

Classic TCP (Reno, RFC 5681) uses AIMD, Additive Increase / Multiplicative Decrease:

Phase Behaviour
Slow start cwnd starts small (modern initial window = 10 MSS, RFC 6928) and doubles every RTT until it reaches ssthresh
Congestion avoidance cwnd grows linearly, by 1 MSS per RTT
Loss detected by 3 duplicate ACKs ssthresh = cwnd/2, cwnd = ssthresh + 3, then fast recovery
Loss detected by timeout ssthresh = cwnd/2, cwnd = 1 MSS, back to slow start
1
2
3
4
5
6
7
8
9
cwnd
 ^          x  (loss: 3 dup ACKs)
 |         /|        /|
 |        / |       / |      "saw-tooth" of TCP Reno
 |       /  |      /  |
 |      /   |_____/   |____
 |     /
 |   _/  slow start (exponential)
 +-------------------------------------> time

4.6 TCP fairness

When K TCP connections share a bottleneck link of rate R, AIMD tends to give each connection about R/K. Applications can get around this by opening many parallel connections, which is one reason why newer approaches look beyond per-flow fairness.

5. How TCP evolved after 2018

The original course said you should "be aware that TCP evolves". This is how it has changed:

Development What it is Status in 2026
CUBIC (RFC 9438, 2023) cwnd grows as a cubic function of the time since the last loss. It works well on links with high bandwidth and high latency. Default in Linux, Windows and macOS
BBR (Google) Bottleneck Bandwidth and RTT. It models the path instead of reacting only to loss. BBRv3 is used by Google services. Widely deployed at Google/YouTube and on many CDNs
ECN (RFC 3168) Routers mark packets instead of dropping them, and the sender slows down on a mark Widely supported
L4S (RFC 9330–9332) Low Latency, Low Loss, Scalable throughput. Very low queueing delay for interactive applications. Being rolled out by ISPs and in cable/5G networks
QUIC (RFC 9000, 2021) A transport protocol on top of UDP with encryption (TLS 1.3) built in, multiple streams without head-of-line blocking, and 0-RTT/1-RTT connection setup Carries HTTP/3 (RFC 9114). A large share of web traffic (Google, Meta, Cloudflare) now uses QUIC.
Multipath TCP (RFC 8684) One TCP connection over several paths (e.g. Wi-Fi + 5G) Used by Apple (Siri, Maps) and supported in Linux

Why QUIC? TCP is implemented in operating-system kernels and inspected by middleboxes, which makes it very hard to change. QUIC runs in user space over UDP and is encrypted end to end, so it can evolve quickly. It still uses the same principles you learned here: sequence numbers (packet numbers), ACKs (with ranges, similar to SACK), retransmission timers, flow control and congestion control.

6. Summary

  • Reliability over an unreliable channel needs checksums, ACKs, sequence numbers, timers and retransmission.
  • Pipelining is essential for performance. GBN is simple but wasteful. SR is efficient but more complex.
  • TCP combines cumulative ACKs, SACK, adaptive timeouts, fast retransmit, flow control (rwnd) and congestion control (cwnd).
  • Congestion control has moved from Reno to CUBIC and BBR. QUIC/HTTP/3 reuses the same principles in a new, encrypted protocol.

References

Videos

Articles and interactive material

Standards

  • RFC 9293: Transmission Control Protocol (2022)
  • RFC 5681: TCP Congestion Control
  • RFC 6298: Computing TCP's Retransmission Timer
  • RFC 2018: TCP Selective Acknowledgment
  • RFC 7323: TCP Extensions for High Performance (window scaling, timestamps)
  • RFC 9438: CUBIC for Fast and Long-Distance Networks
  • RFC 9000: QUIC, and RFC 9114: HTTP/3

All RFCs: https://www.rfc-editor.org/

Tools

  • Wireshark, Statistics → TCP Stream Graphs (Stevens graph, throughput, window scaling): https://www.wireshark.org/
  • ss -ti (Linux) shows per-connection cwnd, RTT and the congestion algorithm
  • iperf3, for throughput measurements: https://iperf.fr/

Cyber Security

Why it matters

TCP is the transport for most of the services you will defend or attack: web, mail, SSH, databases and remote desktop. Many classic and modern attacks abuse the state that TCP keeps:

Attack How it abuses TCP
SYN flood The attacker sends huge numbers of SYNs, often from spoofed addresses, and never completes the handshake. The server fills its backlog of half-open connections and can no longer accept legitimate clients. This is a classic Denial-of-Service (DoS) attack.
TCP reset (RST) injection An attacker on the path, or one who can guess sequence numbers, sends a forged RST that tears down a connection. ISPs and national firewalls have used this for censorship, and it can be used against BGP sessions.
Session hijacking / sequence prediction With predictable ISNs (the Kevin Mitnick attack, 1994), an attacker can inject data into a connection. Modern ISNs are random (RFC 6528).
Port scanning Tools such as nmap use the handshake (SYN scan, FIN/XMAS/NULL scans) and the responses to find open ports, operating systems and firewall rules. This is usually the first step of an attack.
Slowloris / slow-read attacks Many connections are kept open with very slow traffic, which exhausts the server's connection limit.
Amplification over UDP UDP-based protocols without a handshake (DNS, NTP, memcached, but also badly configured QUIC servers) can be abused to reflect and amplify traffic towards a victim.

Knowing how TCP works is also essential for network forensics. Retransmissions, resets, odd flag combinations and window sizes reveal scanning, tunnelling and the operating system that sent the traffic (passive OS fingerprinting, see p0f and JA4T).

How to make it safe

  1. Enable SYN cookies (Linux: net.ipv4.tcp_syncookies = 1, on by default). The server encodes the connection state in the ISN, so it doesn't need to store half-open connections.
  2. Use DDoS protection upstream (your ISP, or a cloud scrubbing service such as Cloudflare, Akamai or AWS Shield) for internet-facing services. One server can't absorb a volumetric attack.
  3. Firewall and rate-limit: allow only the ports you need (default deny), limit new connections per source, and drop invalid flag combinations. Use stateful filtering (nftables/iptables ct state).
  4. Protect long-lived sessions: use TCP-AO (RFC 5925; it replaces the TCP MD5 option) for BGP sessions, and always run sensitive protocols over TLS/QUIC so that an attacker can't inject or read data.
  5. Tune timeouts on web servers and load balancers to defend against Slowloris (e.g. client_header_timeout in nginx, or use a reverse proxy).
  6. Keep stacks patched. TCP/IP stack vulnerabilities still appear, for example Ripple20 and Amnesia:33 (2020) in IoT stacks, and Windows TCP/IP remote-code-execution bugs.
  7. Monitor: an IDS (see article 16) can alert on port scans, SYN floods and unusual RST rates. NetFlow/IPFIX shows anomalies at the connection level.
  8. Plan for QUIC visibility: QUIC encrypts almost all transport headers, so firewalls and IDS can see less. Decide on a policy, such as allowing QUIC only to known services or inspecting it at an endpoint or proxy.

Extra references