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.A complete Ethernet frame
- Data · application
- Segment · transport
- Packet · network
- 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.
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 layer | OSI layer | PDU | Added | Key fields | Typical size |
|---|---|---|---|---|---|
| Application | 5–7 | Data | The message itself | e.g. an HTTP request | Any |
| Transport | 4 | Segment (TCP) / datagram (UDP) | TCP or UDP header | Source and destination ports | TCP 20–60 bytes, UDP 8 bytes |
| Internet | 3 | Packet | IP header | Source and destination IP, TTL, protocol | IPv4 20–60 bytes, IPv6 40 bytes |
| Network Access | 2 | Frame | Ethernet header + FCS trailer | Destination and source MAC, EtherType | 14 + 4 bytes |
| Network Access | 1 | Bits | Signalling (plus preamble on Ethernet) | None: just signals | 8 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:
| Device | Unwraps down to | What it does with the headers |
|---|---|---|
| Sending host | All layers | Builds every header, from data to bits |
| Hub | Bits | Repeats the signal; reads no header at all |
| Switch | Ethernet header | Reads the destination MAC, forwards the frame unchanged |
| Router | IP header | Removes the frame, reads the destination IP, updates the TTL, builds a new frame |
| Firewall | IP, TCP/UDP, sometimes the data | Checks addresses and ports against its rules |
| Receiving host | All layers | Removes 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.)
| Device | IP address | MAC address | Notes |
|---|---|---|---|
| PC-A | 192.168.10.20/24 | 02:00:00:00:00:aa | Default gateway 192.168.10.1 |
| R1, LAN A side | 192.168.10.1 | 02:00:00:00:00:01 | PC-A's default gateway |
| R1, LAN B side | 192.168.20.1 | 02:00:00:00:00:02 | The server's default gateway |
| Web server | 192.168.20.80/24 | 02:00:00:00:00:bb | Listening 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).
- 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
- 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
- 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
- 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
- Application: the browser creates the HTTP request
GET / HTTP/1.1withHost: intranet.example. It hands this data to TCP through the operating system. - Transport: TCP puts a header in front with source port
51524and destination port80, plus sequence and acknowledgement numbers. Data + TCP header = a segment. If the data were too big, TCP would split it across several segments first. - Internet: IP adds a header with source
192.168.10.20and destination192.168.20.80. The Protocol field is set to6so the receiver knows TCP is inside, and the TTL (time to live) starts at64. Segment + IP header = a packet. - Next-hop decision: PC-A compares the destination with its own subnet.
192.168.20.80is not in192.168.10.0/24, so the packet must go to the default gateway192.168.10.1. ARP tells PC-A that the gateway's MAC is02:00:00:00:00:01. - Network Access: Ethernet adds a header with destination MAC
02:00:00:00:00:01(R1, not the server!), source MAC02:00:00:00:00:aaand EtherType0x0800(IPv4), then a 4-byte FCS trailer. Packet + Ethernet header + FCS = a frame. - 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:
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.
- 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. 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. 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. The server de-encapsulates. Ethernet → IP → TCP port 80 → the HTTP request reaches the web server program.
- 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.
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
Frame 2: on LAN B
R1 → web server
| Field | Changed by the router? | Why |
|---|---|---|
| Destination MAC | Yes | Now the next device on the new link (the server) |
| Source MAC | Yes | Now the router's own outgoing interface |
| FCS | Yes | It is a checksum over a new frame, so it is recalculated |
| TTL | Yes, minus 1 | Stops packets looping forever; at 0 the packet is dropped |
| IP header checksum | Yes | The TTL changed, so the IPv4 header checksum must be recalculated |
| Source and destination IP | No | They name the two end hosts for the whole journey |
| TCP ports and data | No | The 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.
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.
- 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
- 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)
- 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
- 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
- Bits to frame: the NIC turns the electrical signals back into bits and finds the start of the frame.
- Check the frame: it recalculates the FCS. If it doesn't match, the frame was damaged and is dropped silently.
- Is it for me? The destination MAC
02:00:00:00:00:bbis the server's own, so it keeps the frame. (Frames for other MACs are ignored, except broadcasts.) - Which protocol is inside? EtherType
0x0800says IPv4, so the packet goes to IP. - IP: the destination IP
192.168.20.80is the server's own address. Protocol6says pass the payload to TCP. - TCP: destination port
80belongs to the web server program. TCP checks the sequence number, sends an acknowledgement and hands the data up. - Application: the web server reads
GET / HTTP/1.1and 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:
| Header | Field | Common values |
|---|---|---|
| Ethernet | EtherType | 0x0800 IPv4 · 0x86DD IPv6 · 0x0806 ARP |
| IPv4 | Protocol (IPv6: Next Header) | 6 TCP · 17 UDP · 1 ICMP |
| TCP / UDP | Destination port | 80 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:
| Part | Bytes | Running total |
|---|---|---|
| Application data (the MSS, maximum segment size) | 1460 | 1460 |
| + TCP header (no options) | 20 | 1480 (segment) |
| + IPv4 header (no options) | 20 | 1500 (packet = MTU) |
| + Ethernet header and FCS | 14 + 4 | 1518 (frame) |
| + Preamble/SFD and the gap between frames, on the wire | 8 + 12 | 1538 |
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.
$ 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
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).$ 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
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 fails | Layer | What happens |
|---|---|---|
| FCS doesn't match (damaged bits) | Network Access | Frame dropped silently; an input error counter goes up. TCP later resends the lost data. |
| Destination MAC isn't mine | Network Access | Frame ignored (normal on a hub or with flooding) |
| Wrong MAC used (stale ARP entry) | Network Access | Frame goes to the wrong device or nowhere; the packet never arrives |
| TTL reaches 0 at a router | Internet | Packet dropped; the router sends an ICMP "time exceeded" message back |
| Packet bigger than the next link's MTU | Internet | Split into fragments, or dropped with an ICMP "fragmentation needed" message if fragmenting isn't allowed |
| Nothing listening on the destination port | Transport | TCP 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 -aon Windows,ip neighon 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 -tlnon the server). - Still unsure? Capture packets on both sides and compare the headers, as above.
$ 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.
- 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
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?
A router forwards a packet from one Ethernet LAN to another (no NAT). Which of these stays the same?
What is the PDU called after the IP header has been added to a TCP segment?
A server receives a frame whose FCS does not match its own calculation. What happens?
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.