Sub topic 3
Encapsulation: wrapping data layer by layer
This chapter assumes you know what an IP address is, can read bytes in hexadecimal, and can run commands in a terminal. It covers how a sender wraps application data in headers before it reaches the wire. Later chapters cover each header in detail.
The idea
Encapsulation is the process of placing the data from one layer inside the payload of the next layer down. Each layer adds its own header in front of what it received. The layer below does not look inside that payload. It treats the whole thing as opaque bytes and adds its own header.
Take an HTTP request as the starting point:
- The application produces data:
GET / HTTP/1.1plus headers. - The transport layer prepends a TCP header. It holds the source and destination ports, sequence numbers and flags. The result is a segment.
- The network layer prepends an IPv4 header. It holds the source and destination IP addresses, a TTL and a protocol number. The result is a packet.
- The link layer prepends an Ethernet header. It holds the destination and source MAC addresses and an EtherType. The result is a frame.
The network card adds a frame check sequence (FCS) at the end of the frame. Most tools never show it, because the card strips it on receipt.
Why each header exists
Each header holds the information its own layer needs, and nothing else. This keeps the layers independent.
| Layer | Header field the layer uses | Question it answers |
|---|---|---|
| Transport | Destination port | Which program on the host gets this? |
| Network | Destination IP address | Which host is this for? |
| Link | Destination MAC address | Which device on this local link receives it next? |
The EtherType in the Ethernet header and the protocol number in the IP header tell the receiver which header comes next. Without them, the receiver would not know whether the bytes after an IP header are TCP, UDP or something else.
Building a frame by hand
The following Python program builds the frame from the steps above and then unpacks it. The checksums are left at 0 to keep the demo short. A real stack computes them.
import struct
http = b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
# Transport: TCP header (20 bytes, no options) + application data
# Flags 0x018 = PSH + ACK. The checksum is left as 0 in this demo.
tcp = struct.pack("!HHIIHHHH", 50000, 80, 1, 0, (5 << 12) | 0x018, 65535, 0, 0) + http
# Network: IPv4 header (20 bytes, no options) + TCP segment
# Protocol 6 = TCP. The header checksum is left as 0 in this demo.
src_ip = bytes([192, 168, 1, 10])
dst_ip = bytes([203, 0, 113, 5])
ip = struct.pack("!BBHHHBBH4s4s", 0x45, 0, 20 + len(tcp), 0, 0, 64, 6, 0, src_ip, dst_ip) + tcp
# Link: Ethernet II header (14 bytes) + IP packet. EtherType 0x0800 = IPv4.
# The frame check sequence is added by the network card, so it is omitted.
dst_mac = bytes.fromhex("aabbccddeeff")
src_mac = bytes.fromhex("112233445566")
frame = struct.pack("!6s6sH", dst_mac, src_mac, 0x0800) + ip
# Decapsulate: peel the headers off in the reverse order
_, _, ethertype = struct.unpack("!6s6sH", frame[:14])
packet = frame[14:]
ihl = (packet[0] & 0x0F) * 4
(total_len,) = struct.unpack("!H", packet[2:4])
ttl, proto = packet[8], packet[9]
segment = packet[ihl:total_len]
sport, dport = struct.unpack("!HH", segment[:4])
offset = (segment[12] >> 4) * 4
data = segment[offset:]
print(hex(ethertype), ttl, proto, sport, dport)
print(data.decode())The first line of output is 0x800 64 6 50000 80. The program recovers the EtherType, TTL, protocol number and both ports. The second line is the original HTTP request.
The frame is 14 + 20 + 20 + 38 = 92 bytes long. The HTTP request is 38 bytes, and the three headers add 54 bytes of overhead.
Common errors
- Thinking a layer reads the payload. The IP layer never parses the TCP header. Only the transport layer at the receiver does.
- Mixing up the names. A segment is TCP data plus its header. A packet is a segment plus an IP header. A frame is a packet plus an Ethernet header.
- Forgetting header length. The IPv4 IHL field and the TCP data offset field give header lengths in 4-byte words. Code that assumes 20 bytes breaks when options are present.
- Forgetting byte order. Network headers use big-endian order. The
!instructformat strings selects it.
Exercise
A host sends 1000 bytes of application data over TCP and IPv4, with no options, on Ethernet. How many bytes are in the frame, not counting the FCS, and which header does the receiver read first?
Show answer
The frame is 1000 + 20 (TCP) + 20 (IPv4) + 14 (Ethernet) = 1054 bytes. The receiver reads the Ethernet header first, because the link layer is the outermost wrapper.