Sub topic 2
How data is wrapped on the way down and unwrapped on the way up
This chapter assumes you know what an IP address, a router, and a web server are, and that you can run simple commands in a terminal. It covers encapsulation and decapsulation, the mechanism that makes layering work. The chapters that follow look at each layer in detail.
The core idea
Each layer adds its own control information to the data it receives from the layer above. That information is a header. Some layers, such as Ethernet, also add a trailer at the end. The result becomes the payload for the layer below. This wrapping is called encapsulation.
The receiving host reverses the process. Each layer reads and removes the header meant for it, then passes the remaining payload upward. This is decapsulation.
A layer on the receiver only reads what its peer layer on the sender wrote. The Layer 4 header is written by the sender's transport layer and read by the receiver's transport layer. Layers in between treat it as opaque payload.
Walking down the stack
Suppose a browser sends an HTTP request. Using the TCP/IP-style view of what happens in practice:
- Application data. The browser produces the HTTP request text.
- Segment. TCP adds a header with source and destination ports, sequence numbers, and flags. The result is a segment.
- Packet. IP adds a header with source and destination IP addresses and a protocol field (6 for TCP, 17 for UDP). The result is a packet.
- Frame. Ethernet adds a header with source and destination MAC addresses and an EtherType. It also adds a trailer holding a frame check sequence (FCS) for error detection. The result is a frame.
- Bits. The network card turns the frame into signals on the medium.
Walking up the stack
The receiver's network card detects the signal and rebuilds the frame. The data link layer then does the following:
- It checks the destination MAC address and the FCS. A frame that fails the check is discarded.
- It reads the EtherType, for example
0x0800for IPv4, and removes the Ethernet header and trailer. - The IP layer checks the destination IP address, reads the protocol field, and removes the IP header.
- The TCP layer uses the destination port to find the right socket, reorders data using sequence numbers, and removes the TCP header.
- The application receives the HTTP request bytes.
Each header contains a field that names the next protocol up: EtherType in Ethernet, protocol number in IP, and port number in TCP and UDP. These fields tell the receiver which module should handle the payload.
Seeing a header in code
This Python snippet parses a 14-byte Ethernet II header. It shows the three fields that Layer 2 adds in front of the packet.
import struct
def mac(b):
return ":".join(f"{x:02x}" for x in b)
# 14-byte Ethernet II header: destination MAC, source MAC, EtherType
header = bytes.fromhex("ffffffffffff" "001122334455" "0806")
dst, src, ethertype = struct.unpack("!6s6sH", header)
print(mac(dst), mac(src), hex(ethertype))
# ff:ff:ff:ff:ff:ff 00:11:22:33:44:55 0x806The destination ff:ff:ff:ff:ff:ff is the broadcast address, so every host on the local network accepts the frame. The EtherType 0x0806 tells the receiver to hand the payload to ARP, not IP.
What changes along the path
Not every header stays the same from sender to receiver.
| Header | Changes at each router? | Why |
|---|---|---|
| Ethernet (MAC addresses) | Yes | The frame is rebuilt for each link. The router strips the old header and adds a new one for the next hop. |
| IP (addresses) | No, apart from the TTL (time to live) field | Source and destination IP addresses identify the end hosts. NAT is an exception. |
| TCP (ports, sequence numbers) | No | Only the two endpoints process it. |
A router decapsulates only as far as Layer 3. It reads the IP header, chooses a next hop, and encapsulates the packet in a new frame. It never looks at the TCP header during normal forwarding.
Common mistakes
- Thinking MAC addresses travel end to end. They only identify the two ends of one link. IP addresses identify the ends of the whole path.
- Thinking each layer talks to the layer beneath it as a peer. Peer layers on the two hosts communicate logically. The data physically moves down, across, and up.
- Mixing up unit names. Segment, packet, and frame are specific to Layers 4, 3, and 2. People often say "packet" for all of them, but the distinction matters when you read a capture.
Exercise
A host sends data to a web server three routers away. Which headers are rewritten at each router, and which stay the same (ignoring NAT and TTL)?
Show answer
The Ethernet header is rewritten at every router, because each link needs its own source and destination MAC addresses. The IP header (apart from the TTL, which decreases by one) and the TCP header stay the same from sender to receiver.
Exercise
A frame arrives with the EtherType 0x0800. What does the receiver do after removing the Ethernet header and trailer?
Show answer
It passes the payload to the IPv4 module, because 0x0800 identifies IPv4. IP then reads its own header and uses the protocol field to pick TCP, UDP, or another protocol.