# Encapsulation and Decapsulation
## What is Encapsulation
Encapsulation is the process by which data is wrapped with the necessary protocol information at each layer of the OSI model as it travels from the Application layer down to the Physical layer on the sending device. Each layer treats the data it receives from the layer above as a payload and adds its own header, and sometimes a trailer, to that payload before passing it down to the next layer. The result is that by the time data reaches the Physical layer, it has been progressively wrapped inside multiple layers of protocol information.
## Encapsulation at Each Layer
At the Application layer, the user's data, for example the content of an email, is the starting point. This is sometimes called the application protocol data unit.
At the Transport layer, the application data is divided into segments if it is too large to send at once. Each segment gets a Transport layer header. In the case of TCP, this header includes the source port number identifying the application on the sender, the destination port number identifying the application on the receiver, a sequence number identifying the position of this segment in the overall data stream, and an acknowledgment number if data is being acknowledged in this segment.
At the Network layer, each segment is placed inside a packet. The Network layer adds an IP header containing the source IP address of the sending device and the destination IP address of the receiving device. The IP header also contains information about the protocol being used in the Transport layer, allowing the receiver to pass the payload to the correct protocol.
At the Data Link layer, each packet is placed inside a frame. The Data Link layer adds a frame header containing the source MAC address and destination MAC address. The MAC addresses used here identify the immediately adjacent devices, not the original sender and final destination of the packet. A frame trailer containing a CRC value for error detection is also added at this layer.
At the Physical layer, the frame is converted into a stream of individual bits and transmitted as physical signals over the network medium.
## Decapsulation at Each Layer
Decapsulation is the reverse process that occurs on the receiving device. As data arrives at the Physical layer and passes upward through the layers, each layer reads and removes the header that was added by the corresponding layer on the sending device.
At the Physical layer, individual bits are received and assembled into a frame.
At the Data Link layer, the frame header is read to verify the destination MAC address. The CRC in the trailer is recalculated and compared to verify the frame was not corrupted. The header and trailer are removed and the remaining packet is passed to the Network layer.
At the Network layer, the IP header is read to verify the destination IP address and determine which Transport layer protocol should receive the data. The IP header is removed and the segment is passed to the appropriate Transport layer protocol.
At the Transport layer, the header is read to determine the destination port and sequence number. TCP uses the sequence number to reassemble segments in the correct order. Acknowledgments are sent back to the sender. The header is removed and the data is passed to the appropriate application.
At the Application, Presentation, and Session layers, the remaining data processing occurs and the original application data is delivered to the receiving application.
## Why Encapsulation Matters
Encapsulation enables the modularity of the OSI model. Each layer does not need to know anything about the protocols used by the layers above or below it. A TCP segment simply becomes the payload of an IP packet, and a TCP implementation does not need to know whether it is running over Ethernet or WiFi. An IP packet simply becomes the payload of a frame, and an IP implementation does not need to know what type of physical network carries the frame. This separation of concerns allows networking technologies to be developed and improved independently at each layer.Back to Subject