Sub topic 9
Decapsulation: unwrapping at the receiver
This chapter assumes you know that a sender wraps data in a TCP header, an IP header and an Ethernet header. It also assumes you can read hexadecimal bytes and run Python 3.
The reverse of encapsulation
Decapsulation is the receiver's side of the process. Each layer reads its own header, uses the fields to decide what to do, removes the header, and passes the remaining payload up to the next layer. The sender adds headers from the top down. The receiver removes them from the bottom up.
The order is fixed because the outermost header is the first one on the wire. The link layer sees the frame first, so only it can find where the IP packet begins.
What each layer checks
Each layer asks two questions. Is this meant for me? Who gets the payload next?
| Layer | Header field used to accept | Header field used to pass up |
|---|---|---|
| Link | Destination MAC is mine or broadcast | EtherType (0x0800 means IPv4) |
| Network | Destination IP is one of my addresses | Protocol (6 means TCP, 17 means UDP) |
| Transport | Destination port has a listening socket | The socket itself |
The "pass up" field is how a layer knows which protocol's header comes next. Without EtherType, the card could not tell an IPv4 packet from an ARP message. Without the protocol number, IP could not tell TCP from UDP.
The header's own length matters too. The IPv4 header length (IHL) field counts 4-byte words, so a value of 5 means 20 bytes. The TCP data offset field works the same way. A receiver must read these fields instead of assuming 20 bytes, because options make headers longer.
Decapsulating in code
This script builds a frame by hand, then unwraps it in the order a receiver would. Checksums are left as 0 to keep the demo short, so it does not verify them. A real stack also verifies the IP header checksum and the TCP checksum, and drops packets that fail.
import struct
http = b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
tcp = struct.pack("!HHIIHHHH", 50000, 80, 1, 0, (5 << 12) | 0x018, 65535, 0, 0) + http
ip = struct.pack("!BBHHHBBH4s4s", 0x45, 0, 20 + len(tcp), 0, 0, 64, 6, 0,
bytes([192, 168, 1, 10]), bytes([203, 0, 113, 5])) + tcp
frame = struct.pack("!6s6sH", bytes.fromhex("aabbccddeeff"),
bytes.fromhex("112233445566"), 0x0800) + ip
def decapsulate(frame):
_, _, ethertype = struct.unpack("!6s6sH", frame[:14])
if ethertype != 0x0800:
raise ValueError("not IPv4")
packet = frame[14:]
ihl = (packet[0] & 0x0F) * 4
(total_len,) = struct.unpack("!H", packet[2:4])
if packet[9] != 6:
raise ValueError("not TCP")
segment = packet[ihl:total_len]
sport, dport = struct.unpack("!HH", segment[:4])
offset = (segment[12] >> 4) * 4
return sport, dport, segment[offset:]
sport, dport, data = decapsulate(frame)
print(sport, dport)
print(data.decode())The script slices the packet with total_len from the IP header. This matters because Ethernet pads short frames to a minimum size. Without trimming to the IP total length, the padding would be treated as TCP data.
Common errors
- Dropped at the link layer. The destination MAC is neither the host's own address nor broadcast. The packet never reaches IP. This is normal on a switched network when frames are flooded.
- Dropped at the network layer. The destination IP does not belong to the host, and the host is not set to forward packets.
- Rejected at the transport layer. No socket is listening on the destination port. TCP answers with a reset (RST), and UDP typically triggers an ICMP "port unreachable" message.
- Wrong header length. Code that assumes a 20-byte IP header reads garbage when options are present.
Exercise
A frame arrives with EtherType 0x0800, protocol number 17 and destination port 53. Which header does the receiver parse after the IP header, and what does it use to choose the application?
Show answer
Protocol 17 means UDP, so it parses the UDP header. The destination port, 53, selects the socket bound to that port, which is usually a DNS server.