Sub topic 3
Encapsulation and Headers as Data Moves Down the Stack
Prerequisites and the basic idea
This chapter assumes that you know the purpose of a computer network, the difference between bits and bytes, and the broad roles of the OSI or TCP/IP layers. Encapsulation is the process of adding control information to data as it moves down a protocol stack. Decapsulation removes that information as the data moves up the stack at the destination.
Each layer provides a service to the layer above it. A layer usually adds a header, which is a control-information field placed before the data. The data unit produced by a layer is called a protocol data unit (PDU). The PDU has a different name at different layers.
| Layer | Typical PDU | Information commonly added |
|---|---|---|
| Application | Message | Application data |
| Transport | Segment or datagram | Port numbers, sequencing, reliability |
| Network | Packet | Source and destination IP addresses |
| Data link | Frame | MAC addresses, error detection trailer |
| Physical | Bits | Signals on the medium |
The exact names differ between protocols. For example, TCP produces a segment, while UDP produces a datagram.
Encapsulation at the sender
Suppose an application sends a web request. The application creates a message containing the request. The transport layer receives that message and adds a TCP header. The header can include source and destination port numbers, sequence numbers, and control flags. The result is a TCP segment.
The network layer treats the complete segment as its payload. It adds an IP header containing logical source and destination addresses. The result is an IP packet.
The data-link layer then adds a frame header and usually a trailer. The header contains local MAC addresses. The trailer often contains a frame check sequence for detecting transmission errors. The result is a frame.
Finally, the physical layer converts the frame into bits or symbols and sends them over a cable, radio link, or fiber.
Headers are interpreted only by relevant layers
A receiving host processes the frame first. Its data-link layer checks the frame and removes the link-layer header and trailer. The network layer examines the IP header and decides whether the packet belongs to the host. It then removes the IP header.
The transport layer uses its header to identify the destination application through a port number. After checking sequence information or error-control fields, it removes the transport header and delivers the original message to the application.
Intermediate routers do not normally remove every header. A router removes the incoming data-link frame, examines the network-layer packet, selects the next hop, and places the packet inside a new frame for the next link. The IP header remains, although some fields may change. A switch usually examines the data-link frame and forwards it without interpreting the transport payload.
A small model
The following program models headers as nested strings:
def encapsulate(message):
segment = "TCP[" + message + "]"
packet = "IP[" + segment + "]"
frame = "MAC[" + packet + "]"
return frame
def decapsulate(frame):
packet = frame.removeprefix("MAC[").removesuffix("]")
segment = packet.removeprefix("IP[").removesuffix("]")
message = segment.removeprefix("TCP[").removesuffix("]")
return message
frame = encapsulate("GET /index.html")
print(frame)
print(decapsulate(frame))In a real network, fields have binary formats and fixed or variable lengths. The nesting model shows the order without pretending that headers are plain text.
Exercise
A TCP segment is placed inside an IP packet, which is then placed inside an Ethernet frame. Which layer added the Ethernet header, and which layer removes it at the destination?
Show answer
The data-link layer added the Ethernet header. The destination's data-link layer checks and removes it before passing the IP packet to the network layer.