Data Encapsulation and Decapsulation
In this post, I will summarize the data packaging process used to send data from one device to another. Encapsulation is the process of putting a header in front of data by the sending device. The headers include information that describes the following:
source of the data (source address)
destination of the data (destination address)
the service that the data needs to be delivered to once it reaches its destination
The header information added to the data will differ based on the layer at which it is added. The reverse process happens on the receiving device. The headers are stripped away as the data moves from the network, to the NIC, to the application.
The entire process of packaging the data is called data encapsulation. Here is a quick explanation of the process of user data passing down the theoretical OSI model:
The user data starts at the application layer of the OSI model.
As the data moves down to the presentation layer, it can be encoded, encrypted, or compressed into a standard format.
At the session layer, a session ID can be attached to the data.
At the transport layer, the data could be broken up into multiple blocks based on MTU. A header will be added containing the source port, destination port, and sequence information. This is known as a segment or datagram.
The segment/datagram flows down to the network layer, where a new IP header containing the source IP address and destination IP address is added. This is known as a packet.
The packet flows down to the Data Link layer, where a new header containing the source MAC address and destination MAC address is added. A trailer is also added at the Data Link Layer called the Frame Check Sequence (FCS). This is known as a frame.
The frame flows down to the physical layer, where it is translated to a signal depending on the physical medium used to transmit the data.
Once the data reaches the destination device, it will begin decapsulating the data. The process is reversed as the transmitted signal is passed to the NIC and then up the layers of the destination device, where it goes from signal to frame, frame to packet, and packet to segment.
Here is a diagram of the process and the Protocol Data Unit at each layer:
Here is a real-world example:
Client Device A needs to upload a file to Server Device B. I will keep this high level for now to explain the encapsulation process. I’ll do a detailed walk-through of the life of a packet in a later post once I get through the fundamentals.
Encapsulation Process:
Client Device A creates a file that needs to be sent to Server Device B.
The application on Client A encapsulates the data with a header with the source and destination ports.
Client A’s network stack will determine what interface it will use to send the data and the destination for Server B. Client A will add a new header that includes the source and destination IP addresses. Client A will also add a protocol number to the header to let Server B know what process should handle the data when it is received.
Client A’s network stack will send the encapsulated data down to the physical interface that it will use to reach Server B. The physical interface will add another header to the data which has the physical address of the network card of Client A as the source address and the physical address of the network card of Server B as the destination.
The file is sent across the wire from Client A to Server B. The data is contained inside a segment, which is inside a packet, which is inside a frame that can be sent across an ethernet segment.
Decapsulation Process:
Server B receives the frame. Server B will see its physical address listed as the destination address in the outermost header and know the frame is destined for its network card. Server B will process the packet and strip the outer header after determining where to send the packet on the local host using the protocol number.
Server B’s network stack will receive the packet and use the socket number to determine the appropriate application on the local host that should receive the data. Server B will then strip the outer header, leaving the application data.
The application Server B receives the packet, strips the final header, and processes the file.
Hopefully this provides a good overview of the encapsulation/decapsulation process of sending data between devices.