Sub topic 4
Encapsulation and decapsulation: how data moves through the stack
This chapter assumes you know what a client and a server are, can run commands in a terminal, and have seen the idea of layered networking from the earlier chapters. Here you follow one message as each layer wraps it on the way out and unwraps it on the way in.
The core idea
Each layer adds its own header to the data it receives from the layer above. A header is a small block of control fields placed in front of the data. Adding a header is called encapsulation. Removing it at the receiver is called decapsulation.
The layer below does not look inside what it receives. It treats the whole thing as an opaque payload and puts its own header in front. The Ethernet layer never reads the TCP header, and the application never sees the Ethernet header.
The link layer also adds a trailer, a short check value at the end of the frame (the Ethernet frame check sequence). The upper layers add only headers.
Going down: encapsulation
Suppose a browser sends an HTTP request. The steps on the sending host are:
- The application produces data: the HTTP request text.
- The transport layer adds a TCP header with source and destination ports, sequence numbers, and flags. Data plus header is a segment.
- The internet layer adds an IP header with source and destination IP addresses and a protocol number. The result is a packet.
- The link layer adds an Ethernet header with source and destination MAC addresses, plus a trailer. The result is a frame.
- The network interface sends the frame as bits on the medium.
Going up: decapsulation
The receiver reverses the process. Each layer reads only its own header, uses the fields to decide what to do, removes the header, and hands the payload upward.
- The link layer checks that the destination MAC matches this interface and that the check value is valid. It then reads the EtherType field to learn that the payload is IP.
- The internet layer checks the destination IP address and reads the protocol field to learn that the payload is TCP.
- The transport layer uses the destination port to find the right socket and delivers the data to that application.
These "next protocol" fields (EtherType, IP protocol number, port) are how each layer knows which module should receive the payload.
Seeing it with Scapy
Scapy builds a packet by stacking headers with the / operator. The order matches encapsulation: the outermost header comes first.
from scapy.all import Ether, IP, TCP
pkt = Ether() / IP(dst="192.0.2.10") / TCP(dport=80, flags="S")
pkt.show()The output lists an Ether section, an IP section, and a TCP section, each with its own fields. Fields you did not set, such as the source addresses, take default values. Nothing is sent by this script. It only builds and prints the packet.
Letting the operating system do it
Most programs never build headers themselves. This program supplies only data, an address, and a UDP port.
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.sendto(b"hello", ("127.0.0.1", 9999))The operating system adds the UDP header, the IP header, and, on a real interface, the link-layer header. This is the point of layering: the application depends only on the socket interface.
Overhead and size limits
Every header costs bytes. With typical sizes, an Ethernet header is 14 bytes, an IPv4 header is 20, and a TCP header is 20, with options adding more. The Ethernet payload is limited by the maximum transmission unit (MTU), commonly 1500 bytes. That leaves 1460 bytes of application data per TCP segment, called the maximum segment size (MSS).
| Layer | Header added | Typical size |
|---|---|---|
| Link | Ethernet | 14 bytes (plus 4-byte trailer) |
| Internet | IPv4 | 20 bytes |
| Transport | TCP | 20 bytes |
Common errors in the mental model
- Thinking a router reads the TCP header. A router decapsulates only to the IP header, chooses the next hop, and builds a new link-layer header for the next link. The IP packet stays the same apart from fields such as the TTL.
- Thinking MAC addresses travel end to end. They change at every hop, while IP addresses stay the same.
- Expecting the capture to show clean boundaries. A capture tool shows the frame from the Ethernet header onward, already assembled.
Exercise
A frame arrives at a host. Which fields tell the host, in order, which protocol handles the payload at each step, and what does the host do with the TCP destination port at the end?
Show answer
The EtherType field in the Ethernet header says the payload is IP. The protocol field in the IP header says the payload is TCP. The TCP destination port is then used to find the socket of the receiving application, and the application data is delivered to it.
Exercise
An application sends 1000 bytes over TCP on Ethernet with no IP or TCP options. How many bytes are in the full frame, including the 4-byte Ethernet trailer?
Show answer
1000 + 20 (TCP) + 20 (IP) + 14 (Ethernet header) + 4 (trailer) = 1058 bytes.