Q&A-22How does data encapsulation and decapsulation work across the TCP/IP and OSI layers, including the specific PDU names, headers, and layer dispatch keys?
Answer
Encapsulation wraps protocol data units (PDUs) by prepending control headers (and sometimes appending trailers) as payload travels down the stack. Decapsulation reverses this on receipt by parsing the outer envelope, verifying integrity, stripping the header, and using a demultiplexing key to dispatch the inner PDU to the correct layer handler.
PDU and Layer Mapping
| OSI Layer | TCP/IP Layer (RFC 1122) | PDU Name | Core Headers / Metadata | Dispatch / Demux Key |
|---|---|---|---|---|
| 7, 6, 5 (App, Pres, Sess) | Application | Data / Message | HTTP/TLS/DNS framing (e.g., TLS ContentType 23) | Application port / URI / ALPN |
| 4 (Transport) | Transport | Segment (TCP) / Datagram (UDP) | TCP (RFC 9293) / UDP (RFC 768) header (Source/Dest Port) | Destination Port (RFC 6335) |
| 3 (Network) | Internet | Packet / Datagram | IPv4 (RFC 791) / IPv6 (RFC 8200) header (IPs, TTL/Hop Limit) | IPv4 Protocol / IPv6 Next Header |
| 2 (Data Link) | Link | Frame | Ethernet II header + FCS trailer (Src/Dst MAC) | EtherType (e.g., 0x0800 IPv4, 0x86DD IPv6) |
| 1 (Physical) | Link / Physical | Bits / Symbols | Preamble, SFD, line coding, signal transitions | Media signal / PHY clock |
Outbound Encapsulation Flow
- Application Layer: A program writes a stream or message into a socket (e.g., HTTP payload wrapped in TLS 1.3 records via RFC 8446). The PDU is Application Data.
- Transport Layer: The kernel buffers the data and segments it according to the Maximum Segment Size (MSS). It adds a 20-byte TCP header (RFC 9293) or 8-byte UDP header (RFC 768), including source and destination ports. The resulting PDU is a TCP Segment or UDP Datagram.
- Internet Layer: The kernel routes the segment and attaches an IP header: a 20-byte IPv4 header (RFC 791) or a 40-byte fixed IPv6 header (RFC 8200). The protocol field is set (
6for TCP,17for UDP). The PDU is an IP Packet. - Link Layer: The host resolves the next-hop L2 address via ARP (RFC 826) or IPv6 Neighbor Discovery (RFC 4861). It wraps the packet into an Ethernet frame: 14-byte header (preamble excluded, 6-byte dest MAC, 6-byte src MAC, 2-byte EtherType
0x0800or0x86DD) and 4-byte Frame Check Sequence (FCS/CRC32) trailer. - Physical Layer: The network interface card (NIC) encodes the frame into physical line signals or bit streams onto the physical medium.
Inbound Decapsulation and Demultiplexing Flow
[NIC Rx] -> Frame (CRC check)
└─> EtherType=0x0800 ──> [IPv4 Layer] (Checksum, TTL-1 if routing)
└─> Protocol=6 ──> [TCP Stack] (Seq/Ack, Ports)
└─> DstPort=443 ──> [TLS/App Socket]- NIC: Validates FCS; discards corrupt frames without interrupting the CPU. Strips frame delimiters.
- Link Demux: Inspects
EtherType.0x0800dispatches payload to IPv4;0x86DDto IPv6;0x0806to ARP. - Network Demux: Verifies IPv4 checksum, validates destination IP, and checks fragmentation fields. Inspects the IPv4
Protocol(or IPv6Next Header) field:6dispatches to TCP,17to UDP,1to ICMP (RFC 792). - Transport Demux: TCP matches the 4-tuple
(src_ip, src_port, dst_ip, dst_port)against open sockets in the connection table, handles sequence ordering, acknowledges data, strips the TCP header, and enqueues bytes into the socket receive buffer. - Application: The user-space application reads raw bytes from the file descriptor, unrolls TLS records (RFC 8446), and parses the application protocol.
Common Failure Modes
- MTU/MSS Mismatch: Outer headers expand packet size beyond path MTU. If the
DF(Don't Fragment) bit is set and an intermediate router cannot forward, an ICMP Type 3 Code 4 (Fragmentation Needed) is sent (RFC 1191). If firewalls drop ICMP, this creates a Path MTU black hole. - Demux Key Drop: Mismatched EtherType or unrecognized IP Protocol numbers cause the receiving stack to drop the PDU silently or emit ICMP Protocol Unreachable.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.