Card 02/ 09

ConceptDifficulty: Beginner2 min

Encapsulation: Each Layer Wraps the One Above

On the way down every layer adds a header, and on the way up each one is read and removed.

Encapsulation is how the layers of a network stack cooperate: on the sending machine each layer takes the data from the layer above and wraps it in its own header, and on the receiving machine each layer reads and removes its header before passing the rest up. It is the reason one layer can change without the others noticing.

A header is a small block of fields written in front of the data, such as the addresses the data is going to and from. When a browser sends a request, the request is just bytes to the transport layer, which puts a TCP header in front of them. The network layer treats the TCP header and the request together as its data, and puts an IP header in front of both. The data link layer does the same with an Ethernet header, and adds a short trailer at the end that lets the receiver detect damage. The physical layer then sends the whole thing as bits.

One payload wrapped on the way down and unwrapped on the way up

InputAn HTTP request from a browser

  1. Layer 7, sender

    1. HTTP

    The application hands its message down.

  2. Layer 4, sender

    1. TCPadded
    2. HTTP

    TCP adds a header with the two port numbers.

  3. Layer 3, sender

    1. IPadded
    2. TCP
    3. HTTP

    IP adds a header with the two IP addresses.

  4. Layer 2, sender

    1. Ethernetadded
    2. IP
    3. TCP
    4. HTTP
    5. Trailertrailer

    Ethernet adds a header with MAC addresses and a trailer for error checking.

  5. Layer 2, receiver

    1. IP
    2. TCP
    3. HTTP

    The receiving card checks the trailer, then removes the Ethernet header and trailer.

  6. Layer 3, receiver

    1. TCP
    2. HTTP

    IP checks the destination address is its own and removes its header.

  7. Layer 4, receiver

    1. HTTP

    TCP reads the port, finds the program listening on it, and hands the message over.

ResultThe server's program receives exactly the bytes the browser's program sent.

Each layer reads only its own header: the web server never sees the Ethernet header at all. A header is a private conversation between the same layer on two machines, which is what keeps the layers independent.

A letter in nested envelopes is a fair picture. The innermost envelope is addressed to a person in an office, the next to the office's building, the outer one to the postal sorting centre, and each is opened by whoever it is addressed to. The picture breaks at one point that matters later: a posted letter keeps its envelopes until it arrives, but every router on the way removes the outermost header, the frame, and writes a new one for the next link.

So a packet capture taken on any one link shows the full stack of headers, and knowing their order tells you which part of a fault each header can explain.