TCP - Transport Layer Protocol ================================== .. toctree:: :maxdepth: 1 :hidden: :includehidden: TCP/tcp TCP/Connection_Oriented TCP/Reliable_Data_Transfer TCP/Error_Detection TCP/Flow_Control TCP/Port_Addressing TCP/Graceful_Connection_Termination TCP/Congestion_Control TCP/Stream_Oriented TCP/Ordered_Delivery TCP/Header TCP/Header_Options TCP/Common_Level_Options TCP/Socket_Level_Options TCP/Linux_Settings TCP (Transmission Control Protocol) is a connection-oriented transport layer protocol that ensures reliable, ordered, and error-checked delivery of data between applications over a network. .. list-table:: :widths: 25 55 20 :header-rows: 1 * - Category - Description - Use Case * - Connection Oriented - TCP establishes a reliable, connection-oriented communication channel using a three-way handshake before data exchange begins. - Web browsing, file transfers, and remote login * - Reliable Data Transfer - Ensures complete and accurate delivery of data using acknowledgments, retransmissions, and sequencing. - Email, file sharing, database transactions * - Error Detection - Uses checksums to detect corruption in headers and payloads during transmission. - Reliable communication in noisy or error-prone networks * - Flow Control - Uses a sliding window mechanism to match sender rate with receiver capability, avoiding overwhelming the receiver. - Prevent buffer overflow in receiving systems * - Port Addressing - Identifies sending and receiving applications using 16-bit source and destination ports. - Supports multiple simultaneous networked applications * - Graceful Connection Termination - Uses a four-step FIN/ACK process to properly close a connection and release resources on both sides. - Ensures clean session closure without data loss * - Congestion Control - Dynamically adjusts sending rate based on perceived network congestion using algorithms like AIMD. - Stabilizes network load and prevents congestion collapse * - Stream Oriented - Treats transmitted data as a continuous stream of bytes rather than discrete packets. - Ideal for long-lived, persistent connections like SSH or HTTP * - Ordered Delivery - Guarantees data is delivered in the exact sequence it was sent by reordering out-of-order segments. - Application-level data consistency and correctness * - Header - Describes fields in the TCP header such as sequence number, ACK number, window size, flags, and checksum. - Packet inspection, firewall rules, and protocol analysis * - Header Options - TCP headers may include optional fields like MSS, Window Scale, Timestamps, and SACK for enhanced performance. - Improve performance and compatibility in diverse environments * - Common Level Options - Common configuration options include timeout settings, keepalive, and Nagle’s algorithm. - Customizing TCP behavior for specific applications * - Socket Level Options - OS-level socket APIs allow control over TCP behavior via options like `SO_REUSEADDR`, `TCP_NODELAY`, etc. - Fine-tune networking behavior in applications * - Linux Settings - System-wide TCP behavior can be configured in Linux via `/proc/sys/net/ipv4/tcp_*` settings. - Performance tuning and security hardening in Linux systems .. tab-set:: .. tab-item:: Connection Oriented **RFC:** RFC 793 (TCP Specification) **Main Features:** - Establishes a reliable connection using a three-way handshake (`SYN`, `SYN-ACK`, `ACK`) - Operates at OSI Layer 4 (Transport Layer) - Guarantees ordered, lossless, and duplicate-free data delivery - Maintains session state throughout the communication - Supports full-duplex communication - Gracefully terminates connection using a four-way FIN handshake **Use Cases:** - Reliable data transfer in web applications (HTTP/HTTPS) - File transfers (FTP, SFTP) - Remote sessions (SSH, Telnet) - Email protocols (SMTP, IMAP, POP3) **Alternative Protocols:** - UDP – Connectionless transport protocol (less overhead) - SCTP – Message-oriented, reliable protocol (used in telecom) - QUIC – Modern alternative using UDP with TCP-like reliability .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about Connection-Oriented TCP:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`Connection Oriented Basic Setup on Ubuntu using IPv4 ` * :ref:`TCP Feature : Connection Oriented ` * :ref:`Reference links ` .. button-link:: ./TCP/Connection_Oriented.html :color: primary :shadow: :expand: Jump to "Connection Oriented" .. tab-set:: .. tab-item:: Reliable Data Transfer **RFC:** RFC 793 (Core TCP Reliability Features) **Main Features:** - Ensures error-free, ordered, and complete data delivery - Uses **acknowledgments (ACKs)** and **sequence numbers** to track data - Automatically **retransmits lost or corrupted packets** - Implements **sliding window** for efficient transmission - Detects and avoids duplicate data at the receiver - Guarantees delivery through timeout and retry mechanisms **Use Cases:** - Downloading files (FTP, HTTP, HTTPS) - Email services (IMAP, POP3, SMTP) - Database synchronization - Remote access tools (SSH, Telnet) **Alternative Protocols:** - UDP – Fast but unreliable, no retransmissions or guarantees - QUIC – Uses UDP but adds reliability and congestion control - SCTP – Alternative transport with built-in reliability for messages .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about Reliable Data Transfer in TCP:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`Reliable Data Transfer Basic Setup on Ubuntu using IPv4 ` * :ref:`TCP Feature : Reliable Data Transfer ` * :ref:`Reference links ` .. button-link:: ./TCP/Reliable_Data_Transfer.html :color: primary :shadow: :expand: Jump to "Reliable Data Transfer" .. tab-set:: .. tab-item:: Error_Detection **RFC:** RFC 793 (TCP Protocol Specification) **Main Features:** - Uses a **16-bit checksum** field in the TCP header to detect data corruption - Verifies the **entire TCP segment**, including header and payload - Protects against accidental errors introduced during transmission - Receiver discards packets with checksum mismatches - Works with IPv4 and IPv6 pseudo-headers for end-to-end verification **Use Cases:** - Ensuring data integrity over unreliable networks - Detecting corruption in transmitted files or web content - Verifying segment correctness in email, SSH, or VoIP streams - Supporting high-integrity transactions in financial systems **Alternative Protocols:** - UDP – Optional checksum, no guaranteed error detection - SCTP – Stronger CRC-based error detection - QUIC – Relies on cryptographic integrity checks (TLS) .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about Error Detection in TCP:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`Error_Detection Basic Setup on Ubuntu using IPv4 ` * :ref:`TCP Feature : Error Detection ` * :ref:`Reference links ` .. button-link:: ./TCP/Error_Detection.html :color: primary :shadow: :expand: Jump to " Error Detection" .. tab-set:: .. tab-item:: Flow Control **RFC:** RFC 793 (TCP Protocol Specification) **Main Features:** - Prevents sender from overwhelming the receiver by controlling data flow - Uses a **window size** field to advertise how much data the receiver can handle - Implements **sliding window** mechanism for efficient data transmission - Adjusts flow dynamically based on network and receiver conditions - Helps avoid packet loss and ensures smooth communication **Use Cases:** - Maintaining reliable data transmission in varying network conditions - Supporting applications like web browsing, file transfer, and streaming - Preventing buffer overflow on slower receivers or congested networks - Optimizing throughput and minimizing retransmissions **Alternative Protocols:** - UDP – No built-in flow control, relies on application layer - SCTP – Enhanced flow control with multistreaming support - QUIC – Flow control integrated with encryption and multiplexing .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about Flow Control in TCP:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`Flow_Control Basic Setup on Ubuntu using IPv6 ` * :ref:`TCP Feature : Flow Control ` * :ref:`Reference links ` .. button-link:: ./TCP/Flow_Control.html :color: primary :shadow: :expand: Jump to "Flow Control" .. tab-set:: .. tab-item:: Port Addressing **RFC:** RFC 793 (TCP Protocol Specification) **Main Features:** - Uses **16-bit port numbers** to identify sending and receiving applications - Enables **multiplexing** of multiple connections on a single IP address - Ports range from **0 to 65535**, with well-known ports (0-1023) assigned to common services - Distinguishes between **source port** and **destination port** in TCP headers - Supports simultaneous communication sessions between different applications or services **Use Cases:** - Running multiple services (e.g., HTTP on port 80, SSH on port 22) on one device - Enabling client-server communication by specifying destination port on server - Allowing operating systems to manage multiple TCP connections concurrently - Supporting network firewalls and filters based on port numbers **Alternative Protocols:** - UDP – Also uses port addressing but without connection-oriented guarantees - SCTP – Uses port addressing with multihoming support - QUIC – Uses UDP ports but adds multiplexed streams on top .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about TCP Port Addressing:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`Port_Addressing Basic Setup on Ubuntu using IPv4 ` * :ref:`TCP Feature : Port Addressing ` * :ref:`Reference links ` .. button-link:: ./TCP/Port_Addressing.html :color: primary :shadow: :expand: Jump to "Port Addressing" .. tab-set:: .. tab-item:: Graceful Connection Termination **RFC:** RFC 793 (TCP Protocol Specification) **Main Features:** - Uses a **four-step handshake** (FIN, ACK, FIN, ACK) to close a connection gracefully - Ensures all pending data is transmitted before the connection is fully terminated - Allows both sender and receiver to acknowledge connection closure separately - Prevents data loss by orderly shutdown of communication channels - Supports **half-close** where one side can stop sending data while still receiving **Use Cases:** - Closing web connections after data transfer completion (HTTP) - Properly ending FTP or SSH sessions without abrupt termination - Avoiding data truncation in file transfers or streaming services - Ensuring clean session teardown in distributed applications **Alternative Protocols:** - UDP – Connectionless, does not support graceful termination - SCTP – Supports graceful shutdown with multi-stream capabilities - QUIC – Uses different mechanisms for connection closure but ensures data integrity .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about TCP Graceful Connection Termination:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`Graceful_Connection_Termination Basic Setup on Ubuntu using IPv4 ` * :ref:`TCP Feature : Graceful Connection Termination ` * :ref:`Reference links ` .. button-link:: ./TCP/Graceful_Connection_Termination.html :color: primary :shadow: :expand: Jump to "Graceful Connection Termination" .. tab-set:: .. tab-item:: Congestion Control **RFC:** RFC 2581 (TCP Congestion Control) **Main Features:** - Mechanisms to prevent network congestion collapse by controlling the rate of data transmission - Uses algorithms like **Slow Start**, **Congestion Avoidance**, **Fast Retransmit**, and **Fast Recovery** - Adjusts the congestion window (cwnd) dynamically based on network feedback - Detects packet loss as a sign of congestion and reduces transmission rate accordingly - Helps maintain fairness and efficiency in shared network environments **Use Cases:** - Avoiding network overload in internet backbone and enterprise networks - Ensuring stable performance in web browsing, streaming, and file transfers - Improving throughput and reducing packet loss during peak traffic periods - Managing traffic in data centers and cloud networks **Alternative Protocols:** - QUIC – Implements congestion control at the user level with improved performance - SCTP – Uses similar congestion control techniques as TCP with multi-streaming - DCCP – Designed specifically for congestion-controlled datagram transport .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about TCP Congestion Control:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`Congestion_Control Basic Setup on Ubuntu using IPv4 ` * :ref:`TCP Feature : Congestion Control ` * :ref:`Reference links ` .. button-link:: ./TCP/Congestion_Control.html :color: primary :shadow: :expand: Jump to "Congestion Control" .. tab-set:: .. tab-item:: Stream Oriented **RFC:** RFC 793 (Transmission Control Protocol) **Main Features:** - Provides a continuous, reliable byte-stream abstraction between applications - Data is delivered in order without message boundaries, unlike datagram protocols - Enables applications to send and receive data as a seamless flow of bytes - Supports full-duplex communication allowing simultaneous send and receive - Handles segmentation and reassembly of data transparently to the application **Use Cases:** - Web browsing (HTTP/HTTPS), file transfers (FTP) - Remote login and command execution (SSH, Telnet) - Streaming services requiring reliable data flow - Any application requiring reliable, ordered delivery of data streams **Alternative Protocols:** - UDP – Connectionless and message-oriented, without guaranteed delivery - SCTP – Supports message-oriented multi-streaming with reliability - QUIC – User-space protocol providing stream multiplexing over UDP .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about TCP Stream-Oriented Communication:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`Stream_Oriented Basic Setup on Ubuntu using IPv4 ` * :ref:`TCP Feature : Stream Oriented ` * :ref:`Reference links ` .. button-link:: ./TCP/Stream_Oriented.html :color: primary :shadow: :expand: Jump to "Stream Oriented" .. tab-set:: .. tab-item:: Ordered Delivery **RFC:** RFC 793 (Transmission Control Protocol) **Main Features:** - Ensures that data segments are delivered to the application in the exact order they were sent - Uses sequence numbers and acknowledgments to track and reorder out-of-sequence packets - Retransmits lost or corrupted packets to maintain data integrity and order - Critical for applications where order of data impacts functionality and correctness **Use Cases:** - File transfers (FTP) requiring exact data sequence - Web browsing (HTTP/HTTPS) for proper page rendering - Email protocols (SMTP, IMAP) for message integrity - Remote terminal sessions (SSH, Telnet) for command execution accuracy **Alternative Protocols:** - UDP – Does not guarantee ordering, suitable for real-time or loss-tolerant apps - SCTP – Supports ordered and unordered delivery with multiple streams - QUIC – Provides ordered delivery with improved latency over UDP .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about TCP Ordered Delivery:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`Ordered_Delivery Basic Setup on Ubuntu using IPv4 ` * :ref:`TCP Feature : Ordered Delivery ` * :ref:`Reference links ` .. button-link:: ./TCP/Ordered_Delivery.html :color: primary :shadow: :expand: Jump to " Ordered Delivery" .. tab-set:: .. tab-item:: Header **RFC:** RFC 793 (Transmission Control Protocol) **Main Features:** - Fixed 20-byte header (without options) containing essential control information - Includes fields such as Source Port, Destination Port, Sequence Number, Acknowledgment Number - Contains Control Flags (SYN, ACK, FIN, RST, PSH, URG) for connection management - Window Size for flow control, Checksum for error checking, and Urgent Pointer **Use Cases:** - Establishing and managing TCP connections - Ensuring reliable and ordered data transfer - Flow control and congestion management during communication - Error detection and recovery **Alternative Protocols:** - UDP Header – simpler, smaller header without reliability features - SCTP Header – supports multi-streaming and multi-homing features - QUIC Header – built on UDP, optimized for low latency and security .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about the TCP Header:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`TCP Header Basic Setup on Ubuntu using IPv4 ` * :ref:`Reference links ` .. button-link:: ./TCP/Header.html :color: primary :shadow: :expand: Jump to "TCP Header" .. tab-set:: .. tab-item:: Header Options **RFC:** RFC 793 (Transmission Control Protocol), RFC 7323 (TCP Extensions for High Performance) **Main Features:** - Optional fields extending the basic TCP header, length varies up to 40 bytes total header size - Common options include Maximum Segment Size (MSS), Window Scale, Selective Acknowledgment (SACK), and Timestamps - Enhance performance, reliability, and support for high-speed networks - Options enable better flow control, congestion management, and improved error handling **Use Cases:** - Negotiating maximum segment size during connection setup - Scaling window sizes for high bandwidth-delay product networks - Selective retransmission of lost packets using SACK - Round-trip time measurement and PAWS (Protection Against Wrapped Sequence numbers) using timestamps **Alternative Protocols:** - TCP without options (minimum 20-byte header) - Other transport protocols (UDP, SCTP) with their own option fields - QUIC with built-in optimization features over UDP .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about TCP Header Options:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`TCP Header Options Basic Setup on Ubuntu using IPv4 ` * :ref:`Reference links ` .. button-link:: ./TCP/Header_Options.html :color: primary :shadow: :expand: Jump to "TCP Header Options" .. tab-set:: .. tab-item:: Common Level Options **RFC:** RFC 1122, RFC 793, and various OS-level socket APIs **Main Features:** - Configurable parameters that control TCP behavior during communication - Common options include **TCP_NODELAY**, **SO_KEEPALIVE**, **SO_LINGER**, **TCP_MAXSEG**, and **TCP_CORK** - Exposed via socket programming interfaces (e.g., setsockopt in C, Python's socket module) - Improve performance, optimize behavior for specific applications or workloads **Use Cases:** - **TCP_NODELAY**: Disables Nagle’s algorithm for low-latency applications like games or real-time chat - **SO_KEEPALIVE**: Detects dead peer connections in long-lived sessions - **SO_LINGER**: Controls how TCP handles socket close (graceful vs. abrupt) - **TCP_CORK**: Delays packet transmission until data is ready (used in HTTP servers) - **TCP_MAXSEG**: Sets the maximum segment size to control packet size **Alternative Protocols:** - UDP (no connection-level options, minimal overhead) - QUIC (modern transport protocol with built-in performance tuning) - SCTP (includes both stream and message-oriented options) .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about Common TCP-Level Options:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`Common Level Options Basic Setup on Ubuntu using IPv4 ` * :ref:`Reference links ` .. button-link:: ./TCP/Common_Level_Options.html :color: primary :shadow: :expand: Jump to "TCP Common Level Options" .. tab-set:: .. tab-item:: Socket Level Options **RFC:** RFC 1122, POSIX Socket API Specification **Main Features:** - Socket-level options control behavior of network sockets at the OS interface level - Applied using system calls like `setsockopt()` and `getsockopt()` - Include options for buffer size, reuse of addresses, timeout behavior, and more - Affects both performance and reliability of TCP applications **Use Cases:** - **SO_REUSEADDR**: Allows a socket to forcibly bind to a port in `TIME_WAIT` - **SO_RCVBUF / SO_SNDBUF**: Tune buffer sizes for high-throughput or low-latency applications - **SO_RCVTIMEO / SO_SNDTIMEO**: Set socket timeouts for blocking calls - **SO_LINGER**: Determine behavior on `close()` — wait for unsent data or discard it - **SO_BROADCAST**: Enable sending datagrams to broadcast addresses (UDP) **Alternative Protocols:** - UDP – Shares many socket options but lacks connection state - SCTP – Provides extended socket-level control with multiple streams - QUIC – Bypasses traditional sockets via user-space libraries .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about TCP Socket-Level Options:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`Socket Level Options Basic Setup on Ubuntu using IPv4 ` * :ref:`Reference links ` .. button-link:: ./TCP/Socket_Level_Options.html :color: primary :shadow: :expand: Jump to "TCP Socket Level Options" .. tab-set:: .. tab-item:: Linux Settings **RFC:** Not governed by a specific RFC – based on Linux kernel and sysctl configuration **Main Features:** - TCP behavior in Linux is customizable using `sysctl`, `/proc`, and socket options - Fine-tuning parameters affects performance, security, congestion handling, and buffering - Commonly configured via `/etc/sysctl.conf` or runtime with `sysctl -w` - Allows optimization for high-performance servers, embedded systems, and networks **Use Cases:** - **`net.ipv4.tcp_syncookies`**: Protects against SYN flood attacks - **`net.ipv4.tcp_tw_reuse`**: Allows reuse of sockets in `TIME_WAIT` state - **`net.ipv4.tcp_fin_timeout`**: Adjusts timeout for closed connections - **`net.ipv4.tcp_window_scaling`**: Enables support for large TCP windows - **`net.core.rmem_max / wmem_max`**: Set max socket receive/send buffer size - **`net.ipv4.tcp_congestion_control`**: Selects the congestion control algorithm (e.g., `cubic`, `bbr`) **Alternative Protocols:** - BSD TCP stack – Alternative behavior and defaults in FreeBSD, macOS - Windows TCP/IP stack – Tuning through Registry settings - QUIC – User-space protocol not subject to kernel TCP settings .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: What You Will Learn in This Section **Let us learn more about TCP Linux Settings:** * :ref:`Learnings in this section ` * :ref:`Terminology ` * :ref:`Version Info ` * :ref:`TCP linux Settings Basic Setup on Ubuntu using IPv4 ` * :ref:`Reference links ` .. button-link:: ./TCP/Linux_Settings.html :color: primary :shadow: :expand: Jump to "TCP Linux Settings"