Routelearn.net
Course menu

Unit 3: Network ModelsLesson 3.3 (3 of 3 in this unit)15 of 84 in the Network Fundamentals course

Encapsulation and de-encapsulation

How data is wrapped in a header at each layer before it is sent, unwrapped layer by layer when it arrives, and what a router changes (and doesn't change) at every hop in between. One web request, followed with real ports, IP addresses and MAC addresses.

Beginner · 18 min read · Before this: The OSI model, The TCP/IP model

Encapsulation is the process in which each layer of the sender’s network stack, from the transport layer down, adds its own header (and, at the data link layer, a trailer) to the data passed down from the layer above, producing a segment, then a packet, then a frame. De-encapsulation is the reverse: each layer at the receiver reads and removes its own header before passing the data up.

In simple terms: Before data is sent, each layer wraps it in its own envelope with the information that layer needs. The receiver opens the envelopes one by one, in reverse order.

In The OSI model and The TCP/IP model you learned that networking is split into layers. This lesson shows what that means for real data: each layer wraps the data in its own label before it leaves, and the receiver unwraps those labels in reverse. By the end you should be able to:

  • Name the PDU at each layer: data, segment, packet, frame, bits.
  • Say which addresses each header carries: ports, IP addresses, MAC addresses.
  • Walk through encapsulation on the sender and de-encapsulation on the receiver.
  • Explain exactly what a router changes when it forwards a packet, and what it leaves alone.

💡 In simple terms: think of Russian nesting dolls, or a letter in an envelope, inside a parcel, inside a delivery van. Each wrapper has its own label for its own job. The van driver only reads the van's route sheet; nobody along the way opens the letter.

What is encapsulation?

Encapsulation is the process of adding control information to data as it moves down the layers on the sending device. Each layer takes what the layer above gave it, treats it as its payload (the contents it carries), and puts a header in front of it. A header is a small block of fields, such as addresses and numbers, that the same layer on the other device will read. Ethernet also adds a trailer at the end.

De-encapsulation (also spelled decapsulation) is the reverse, on the receiving device: each layer reads its own header, acts on it, removes it and passes the rest up to the next layer.

Data: The application produces data, such as an HTTPS request.

  1. Data · application
  2. Segment · transport
  3. Packet · network
  4. Frame · data link

Data: The application produces data, such as an HTTPS request. Segment: TCP adds a header with the source and destination ports. Packet: IP adds a header with the source and destination IP addresses. Frame: Ethernet adds MAC addresses in front and an error check (FCS) at the end.

On the way out each layer wraps the one above it; the receiver unwraps them in reverse (decapsulation).

Why encapsulation exists

Encapsulation is what makes the layered model work in practice. It solves several problems at once:

Each layer stays independent

TCP doesn't care if the frame travels over Wi-Fi or fibre. It hands its segment down and trusts the layers below.

Different addresses for different jobs

Ports find the program, IP addresses find the host, MAC addresses find the next device. Each sits in its own header.

Devices only open what they need

A switch reads only the Ethernet header; a router goes one layer deeper. Neither has to understand the application.

Links can be swapped hop by hop

The outer frame can be thrown away and rebuilt for each link, while the packet inside travels unchanged end to end.

The PDU at each layer

A PDU (protocol data unit) is what a piece of data is called at a given layer. The name changes as headers are added:

TCP/IP layerOSI layerPDUAddedKey fieldsTypical size
Application5–7DataThe message itselfe.g. an HTTP requestAny
Transport4Segment (TCP) / datagram (UDP)TCP or UDP headerSource and destination portsTCP 20–60 bytes, UDP 8 bytes
Internet3PacketIP headerSource and destination IP, TTL, protocolIPv4 20–60 bytes, IPv6 40 bytes
Network Access2FrameEthernet header + FCS trailerDestination and source MAC, EtherType14 + 4 bytes
Network Access1BitsSignalling (plus preamble on Ethernet)None: just signals8 bytes preamble + SFD

💡 Memory trick for the PDUs from the top: Do Some People Fear Birthdays? Data, Segment, Packet, Frame, Bits.

Where encapsulation happens

Full encapsulation and de-encapsulation, through every layer, happen only on the two end hosts: the device that creates the data and the device it is for. Devices in the middle only unwrap as far as they need to:

DeviceUnwraps down toWhat it does with the headers
Sending hostAll layersBuilds every header, from data to bits
HubBitsRepeats the signal; reads no header at all
SwitchEthernet headerReads the destination MAC, forwards the frame unchanged
RouterIP headerRemoves the frame, reads the destination IP, updates the TTL, builds a new frame
FirewallIP, TCP/UDP, sometimes the dataChecks addresses and ports against its rules
Receiving hostAll layersRemoves every header, from bits to data

The example network

Every diagram below follows the same request. PC-A opens the company intranet page at http://intranet.example. The web server is on a different subnet, so the traffic must pass through router R1. There is no NAT here, which keeps the example clear. (The example uses plain HTTP so you can see the request; with HTTPS the data would be encrypted by TLS, but the headers below it would look the same.)

DeviceIP addressMAC addressNotes
PC-A192.168.10.20/2402:00:00:00:00:aaDefault gateway 192.168.10.1
R1, LAN A side192.168.10.102:00:00:00:00:01PC-A's default gateway
R1, LAN B side192.168.20.102:00:00:00:00:02The server's default gateway
Web server192.168.20.80/2402:00:00:00:00:bbListening on TCP port 80

Assume the TCP connection is already open (the three-way handshake is done) and that PC-A and R1 already know the MAC addresses they need from ARP.

Encapsulation on the sender, step by step

Read from the top. Each row is one layer on PC-A, and the strip shows the whole PDU at that point: the newest header is always on the outside (the left).

  1. 1Application (HTTP)PDU: Data

    The browser writes the HTTP request. This is the data the user actually cares about.

    Data
    Request line
    GET / HTTP/1.1
    Host header
    intranet.example
  2. 2Transport (TCP)PDU: Segment

    TCP adds a header with ports, so the server knows which program the data is for, and sequence numbers, so lost data can be resent.

    TCP headerData
    Source port
    51524 (temporary)
    Destination port
    80 (HTTP)
    Sequence / ack numbers
    track every byte
    Flags
    ACK, PSH
  3. 3Internet (IPv4)PDU: Packet

    IP adds the end-to-end addresses. The server is on another subnet, so PC-A will send the packet to its default gateway, R1.

    IP headerTCP headerData
    Source IP
    192.168.10.20 (PC-A)
    Destination IP
    192.168.20.80 (web server)
    Protocol
    6 = TCP
    TTL
    64
  4. 4Network Access (Ethernet)PDU: Frame

    Ethernet adds MAC addresses for this one link (PC-A to R1) and an error check at the end. The NIC then sends the frame as bits.

    Ethernet headerIP headerTCP headerDataFCS
    Destination MAC
    02:00:00:00:00:01 (R1)
    Source MAC
    02:00:00:00:00:aa (PC-A)
    EtherType
    0x0800 = IPv4 inside
    FCS (trailer)
    CRC-32 error check
PC-A builds the frame. Data becomes a segment, the segment becomes a packet, the packet becomes a frame.
  1. Application: the browser creates the HTTP request GET / HTTP/1.1 with Host: intranet.example. It hands this data to TCP through the operating system.
  2. Transport: TCP puts a header in front with source port 51524 and destination port 80, plus sequence and acknowledgement numbers. Data + TCP header = a segment. If the data were too big, TCP would split it across several segments first.
  3. Internet: IP adds a header with source 192.168.10.20 and destination 192.168.20.80. The Protocol field is set to 6 so the receiver knows TCP is inside, and the TTL (time to live) starts at 64. Segment + IP header = a packet.
  4. Next-hop decision: PC-A compares the destination with its own subnet. 192.168.20.80 is not in 192.168.10.0/24, so the packet must go to the default gateway 192.168.10.1. ARP tells PC-A that the gateway's MAC is 02:00:00:00:00:01.
  5. Network Access: Ethernet adds a header with destination MAC 02:00:00:00:00:01 (R1, not the server!), source MAC 02:00:00:00:00:aa and EtherType 0x0800 (IPv4), then a 4-byte FCS trailer. Packet + Ethernet header + FCS = a frame.
  6. Physical: the NIC sends a preamble so the receiver can lock on, then the frame as electrical signals on the cable: bits.

Here is the finished frame, field by field:

Preamble7 BSync pattern
SFD1 BStart of frame
Destination MAC6 BWho it's for
Source MAC6 BWho sent it
EtherType2 Be.g. 0x0800 = IPv4
Payload46–1500 BIP packet (+ padding)
FCS4 BError check (CRC)
On the wire only (8 B)
Ethernet header: 14 B
Frame: 64–1518 bytes (destination MAC → FCS)
Ethernet II frame. The preamble and SFD are transmitted before the frame but aren't counted in its size. Payloads under 46 bytes are padded; 802.1Q VLAN tags add 4 bytes.

The key idea: the IP header says where the packet is ultimately going (the server). The Ethernet header only says who should receive it on this link (R1). See ARP for local and remote destinations for why.

Watch the frame travel

Now follow the frame across the network. Pay attention to the router step: that is where the outer wrapper changes.

:01:02PC-A192.168.10.20SW1LAN AR1.10.1 | .20.1SW2LAN BWeb server192.168.20.80
  1. 1. PC-A encapsulates. HTTP data → TCP (51524 → 80) → IP (192.168.10.20 → 192.168.20.80) → Ethernet (02:00:00:00:00:aa → 02:00:00:00:00:01).
  2. 2. SW1 reads only the Ethernet header. Destination MAC 02:00:00:00:00:01 is on the port toward R1. The frame goes out unchanged.
  3. 3. R1 de-encapsulates and re-encapsulates. It checks the FCS, strips the frame, reads destination IP 192.168.20.80, lowers the TTL to 63, then builds a new frame: 02:00:00:00:00:02 → 02:00:00:00:00:bb.
  4. 4. The server de-encapsulates. Ethernet → IP → TCP port 80 → the HTTP request reaches the web server program.
  5. 5. The reply does the same in reverse. The server encapsulates its response (port 80 → 51524, 192.168.20.80 → 192.168.10.20), and R1 rebuilds the frame again on the way back.
The IP addresses stay the same from PC-A to the server. The MAC addresses change at the router.

What changes at a router hop

This is one of the most important ideas in networking. When R1 receives the frame, it de-encapsulates up to the IP header, makes its routing decision, then encapsulates the same packet in a brand-new frame for the next link. Compare the two frames:

Frame 1: on LAN A

PC-A → R1

Layer 2 · Ethernet header (this hop)
Destination MAC
02:00:00:00:00:01
R1, LAN A side
Source MAC
02:00:00:00:00:aa
PC-A
EtherType
0x0800
IPv4
Layer 3 · IP header (end to end)
Source IP
192.168.10.20
PC-A
Destination IP
192.168.20.80
web server
TCP 51524 → 80 · HTTP GET · TTL 64

Frame 2: on LAN B

R1 → web server

Layer 2 · Ethernet header (this hop)
Destination MAC
02:00:00:00:00:bb
web server
Source MAC
02:00:00:00:00:02
R1, LAN B side
EtherType
0x0800
IPv4
Layer 3 · IP header (end to end)
Source IP
192.168.10.20
unchanged
Destination IP
192.168.20.80
unchanged
TCP 51524 → 80 · HTTP GET · TTL 63
FieldChanged by the router?Why
Destination MACYesNow the next device on the new link (the server)
Source MACYesNow the router's own outgoing interface
FCSYesIt is a checksum over a new frame, so it is recalculated
TTLYes, minus 1Stops packets looping forever; at 0 the packet is dropped
IP header checksumYesThe TTL changed, so the IPv4 header checksum must be recalculated
Source and destination IPNoThey name the two end hosts for the whole journey
TCP ports and dataNoThe router doesn't even need to read them

In the IPv4 header below, the highlighted fields are the ones a router rewrites on every hop. The source and destination addresses stay the same.

Version4 bits
IHL4 bitsheader length
DSCP / ECN8 bits
Total length16 bits
Identification16 bits
Flags3 bits
Fragment offset13 bits
TTL8 bits64 → 63
Protocol8 bits6 = TCP
Header checksum16 bitsrecalculated
Source IP address32 bits192.168.10.20 · unchanged
Destination IP address32 bits192.168.20.80 · unchanged
The 20-byte IPv4 header (no options). A router changes the TTL and the header checksum, never the addresses (unless it is doing NAT).

The NAT exception. A home router usually does NAT: it replaces the private source IP (and often the source port) with its own public address before the packet goes to the internet. That is an extra job on top of routing, not part of normal forwarding. See Why NAT exists and How NAT and PAT work.

If the path had five routers, this would happen five times: five different frames on five links, but one packet with the same IP addresses inside all of them. The full journey is in Different-subnet communication.

De-encapsulation on the receiver, step by step

The web server unwraps the frame from the outside in. Read from the top: the full frame arrives first, and one header disappears per row. Notice the MAC addresses are the ones R1 wrote, and the TTL is 63.

  1. 1Network Access (Ethernet)PDU: Frame

    The NIC turns the bits back into a frame, checks the FCS, sees its own MAC as the destination and reads the EtherType. Ethernet removes its header and trailer.

    Ethernet headerIP headerTCP headerDataFCS
    Destination MAC
    02:00:00:00:00:bb (the server)
    Source MAC
    02:00:00:00:00:02 (R1, not PC-A)
    EtherType
    0x0800, so pass it up to IPv4
    FCS (trailer)
    recalculated: matches, frame is good
  2. 2Internet (IPv4)PDU: Packet

    The destination IP is the server's own address, so the packet is for it. Protocol 6 says the payload is TCP. IP removes its header.

    IP headerTCP headerData
    Source IP
    192.168.10.20 (PC-A, unchanged)
    Destination IP
    192.168.20.80 (that's me)
    Protocol
    6 = TCP, so pass it up to TCP
    TTL
    63 (R1 took one off)
  3. 3Transport (TCP)PDU: Segment

    Destination port 80 belongs to the web server program. TCP checks the sequence number, acknowledges the data and removes its header.

    TCP headerData
    Source port
    51524 (temporary)
    Destination port
    80 (HTTP)
    Sequence / ack numbers
    track every byte
    Flags
    ACK, PSH
  4. 4Application (HTTP)PDU: Data

    Only the HTTP request is left. The web server program reads it and prepares the page.

    Data
    Request line
    GET / HTTP/1.1
    Host header
    intranet.example
The web server unwraps the frame: frame → packet → segment → data.
  1. Bits to frame: the NIC turns the electrical signals back into bits and finds the start of the frame.
  2. Check the frame: it recalculates the FCS. If it doesn't match, the frame was damaged and is dropped silently.
  3. Is it for me? The destination MAC 02:00:00:00:00:bb is the server's own, so it keeps the frame. (Frames for other MACs are ignored, except broadcasts.)
  4. Which protocol is inside? EtherType 0x0800 says IPv4, so the packet goes to IP.
  5. IP: the destination IP 192.168.20.80 is the server's own address. Protocol 6 says pass the payload to TCP.
  6. TCP: destination port 80 belongs to the web server program. TCP checks the sequence number, sends an acknowledgement and hands the data up.
  7. Application: the web server reads GET / HTTP/1.1 and starts building its reply, which will be encapsulated the same way, in the other direction.

How each layer knows what's inside

A receiver never has to guess. Every header has a field that names the protocol in the next layer up. Using these to pass data to the right place is called demultiplexing:

HeaderFieldCommon values
EthernetEtherType0x0800 IPv4 · 0x86DD IPv6 · 0x0806 ARP
IPv4Protocol (IPv6: Next Header)6 TCP · 17 UDP · 1 ICMP
TCP / UDPDestination port80 HTTP · 443 HTTPS · 53 DNS · 22 SSH

On the sending side the opposite happens: many programs share one network card, and their data is mixed onto the same link. That is multiplexing. Ports are what keep the conversations apart; see Port numbers and sockets.

The cost of all those headers

Every header takes space that could have carried data. This is called overhead. On standard Ethernet, the largest packet a frame can carry is 1500 bytes: the MTU (maximum transmission unit). Here is how a full-size TCP segment fills it:

PartBytesRunning total
Application data (the MSS, maximum segment size)14601460
+ TCP header (no options)201480 (segment)
+ IPv4 header (no options)201500 (packet = MTU)
+ Ethernet header and FCS14 + 41518 (frame)
+ Preamble/SFD and the gap between frames, on the wire8 + 121538

So about 1460 ÷ 1538 ≈ 95% of the bits on the wire are your data at best. Small packets are much less efficient: a minimum-size 64-byte frame carrying a bare TCP acknowledgement holds no user data at all, only headers and padding. If you add more headers, for example a VPN, the space left for data shrinks further; see VPN basics.

A real capture: seeing the headers

Packet capture tools such as Wireshark and tcpdump show every header. Here is the first segment of the connection (the TCP SYN) captured on both LANs. Compare the MAC addresses and the TTL.

Example output on PC-A (LAN A) from a Linux PC, written for this lesson
$ sudo tcpdump -e -n -v -i eth0 tcp port 80
tcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
10:15:02.114233 02:00:00:00:00:aa > 02:00:00:00:00:01, ethertype IPv4 (0x0800), length 74: (tos 0x0, ttl 64, id 4821, offset 0, flags [DF], proto TCP (6), length 60)
    192.168.10.20.51524 > 192.168.20.80.80: Flags [S], cksum 0x8c3e (correct), seq 2814375120, win 64240, options [mss 1460,sackOK,TS val 1593001 ecr 0,nop,wscale 7], length 0
Read it from left to right, outside in. Ethernet: 02:00:00:00:00:aa > 02:00:00:00:00:01 and EtherType IPv4. IP: ttl 64, proto TCP (6), and the IP addresses on the next line. TCP: the port numbers come after the IP address (.51524 and .80) and Flags [S] is a SYN. The frame length is 74 bytes: 14 for Ethernet + 60 for the IP packet (tcpdump doesn't show the FCS).
Example output on the web server (LAN B) from a Linux server, written for this lesson
$ sudo tcpdump -e -n -v -i eth0 tcp port 80
tcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
10:15:02.114871 02:00:00:00:00:02 > 02:00:00:00:00:bb, ethertype IPv4 (0x0800), length 74: (tos 0x0, ttl 63, id 4821, offset 0, flags [DF], proto TCP (6), length 60)
    192.168.10.20.51524 > 192.168.20.80.80: Flags [S], cksum 0x8c3e (correct), seq 2814375120, win 64240, options [mss 1460,sackOK,TS val 1593001 ecr 0,nop,wscale 7], length 0
Same packet, different frame. The MAC addresses changed (now R1's LAN B interface to the server) and the TTL dropped to 63. The IP addresses, ports, IP ID 4821 and TCP sequence number are exactly the same.

What happens when it goes wrong

Because every layer checks its own header, a problem is usually caught at one specific layer, and the PDU is dropped there:

Check that failsLayerWhat happens
FCS doesn't match (damaged bits)Network AccessFrame dropped silently; an input error counter goes up. TCP later resends the lost data.
Destination MAC isn't mineNetwork AccessFrame ignored (normal on a hub or with flooding)
Wrong MAC used (stale ARP entry)Network AccessFrame goes to the wrong device or nowhere; the packet never arrives
TTL reaches 0 at a routerInternetPacket dropped; the router sends an ICMP "time exceeded" message back
Packet bigger than the next link's MTUInternetSplit into fragments, or dropped with an ICMP "fragmentation needed" message if fragmenting isn't allowed
Nothing listening on the destination portTransportTCP answers with a reset (RST): "connection refused". UDP triggers ICMP "port unreachable".

The ICMP lesson covers those error messages, and Traceroute uses the TTL rule on purpose to map the routers on a path.

Troubleshooting with encapsulation in mind

Knowing which header holds which address tells you where to look:

  • Frames damaged? Look at interface error counters (Layer 1 and 2).
  • Wrong next hop? Check the ARP cache: does the gateway's IP map to the right MAC? (arp -a on Windows, ip neigh on Linux)
  • Packets going the wrong way? Check IP settings and routes (ipconfig, ip route).
  • Reaching the host but not the service? Check the port (Test-NetConnection -Port, ss -tln on the server).
  • Still unsure? Capture packets on both sides and compare the headers, as above.
Example output from a Linux PC, written for this lesson
$ ip -s link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 02:00:00:00:00:aa brd ff:ff:ff:ff:ff:ff
    RX:  bytes packets errors dropped  missed   mcast
      48213907   61822    214       0       0     312
    TX:  bytes packets errors dropped carrier collsns
       9921842   40217      0       0       0       0
mtu 1500 is the largest packet this interface will put in a frame. The RX errors count of 214 means 214 frames arrived damaged (usually failed FCS checks) and were dropped before IP ever saw them. A number that keeps rising points to a cable, connector or duplex problem: see Layer 1 and 2 problems.

Common mistakes

  • Thinking the destination MAC is the final host's MAC. For a remote destination it is the default gateway's MAC. Only the IP header names the final host.
  • Thinking routers change IP addresses. Normal routing keeps both IP addresses. Only NAT rewrites them.
  • Thinking switches rebuild frames. A Layer 2 switch forwards the frame unchanged; only routers build new frames.
  • Mixing up the PDU names. Segment = layer 4, packet = layer 3, frame = layer 2. A "frame" has MAC addresses; a "packet" has IP addresses.
  • Forgetting the trailer. Ethernet is the only common layer that adds something at the end: the FCS.
  • Thinking every layer adds a header in every packet. There is no separate session or presentation header. TLS, when used, adds its own record header as part of the application data.
✅ Key takeaways
  • Encapsulation adds a header at each layer going down; de-encapsulation removes them going up.
  • PDUs: data → segment (TCP) or datagram (UDP) → packet → frame → bits.
  • Ports are in the TCP/UDP header, IP addresses in the IP header, MAC addresses in the Ethernet header.
  • Ethernet also adds a trailer, the FCS, to detect damaged frames.
  • At each router hop the frame is rebuilt (new MACs, new FCS) and the TTL drops by 1; the IP addresses and ports stay the same (unless NAT is used).
  • EtherType, the IP Protocol field and port numbers tell each layer where to pass the data next.

Knowledge check

Predict · scenario 1

PC-A (192.168.10.20) sends a packet to a server on another subnet. What is the destination MAC address in the frame PC-A sends?

Predict · scenario 2

A router forwards a packet from one Ethernet LAN to another (no NAT). Which of these stays the same?

Predict · scenario 3

What is the PDU called after the IP header has been added to a TCP segment?

Predict · scenario 4

A server receives a frame whose FCS does not match its own calculation. What happens?

Predict · scenario 5

On the receiver, how does IP know that the payload should go to TCP rather than UDP?

Where to go next

You have now finished the Network Models unit. Next, look closely at the outer wrapper in Ethernet fundamentals, The Ethernet frame and MAC addresses. The TCP header is covered field by field in The TCP header, and the complete hop-by-hop story, including ARP, is in Different-subnet communication.

FAQ

Is it decapsulation or de-encapsulation?
Both words are used and mean the same thing: removing headers (and trailers) layer by layer on the way up the receiver's stack. Cisco material often says de-encapsulation; many textbooks say decapsulation.
Does a switch de-encapsulate frames?
A normal (Layer 2) switch reads the Ethernet header to choose an output port, but it does not remove it or look inside the IP packet. The frame leaves the switch exactly as it arrived, MAC addresses included.
Do IP addresses ever change on the way?
Not in normal routing: routers keep the source and destination IP addresses and only rebuild the frame. The common exception is NAT, for example on a home router, which rewrites the private source IP (and often the port) to a public one.
Why does Ethernet add a trailer when other layers don't?
The FCS (frame check sequence) is a checksum over the whole frame. Putting it at the end means the NIC can calculate it while the bits are being sent and append the result last, and the receiver can check it as the bits arrive.