Layering and Encapsulation: OSI, the TCP/IP Model and How Headers Nest
How network protocols are split into layers, the seven-layer OSI model versus the four-layer TCP/IP model, how each layer wraps the one above in its own header, and how a receiver uses those headers to deliver a packet, traced byte by byte in a real capture.
kvi5hnuCode Sprout team member20 min readOn this page
A web page download involves many separate jobs. Someone has to put bits on the wire, get each frame to the next device, find a route across many networks, make the data reliable, and finally speak HTTP (the Hypertext Transfer Protocol that web browsers and servers use). No single protocol does all of that. Instead, network software is built as a stack of layers, each doing one part of the job and relying on the layer below it for the rest.
This article explains the two layer models you'll hear about, the OSI model and the TCP/IP model, and then follows one real HTTP request down through the layers and back up again, byte by byte.
Why layers?
The idea of building software in layers came from operating-system design in the late 1960s, where a hierarchy of layers made a large system easier to verify. Networking took it further. Each layer:
- offers a service to the layer above it (for example, "deliver this datagram to that address"),
- uses the service of the layer below it, and
- talks to its peer, the same layer on the other machine, using its own protocol.
The big benefit is independence. Ethernet engineers can build 800 Gbit/s links without anyone changing TCP, and browser developers can invent HTTP/3 without anyone changing Ethernet. Different people with different expertise work on different layers, and each layer can change on its own schedule.
The OSI model
The most widely quoted layer model is the Open Systems Interconnection (OSI) reference model from the International Organization for Standardization (ISO). It has seven layers, numbered from the bottom:
| Layer | Name | Job | Examples |
|---|---|---|---|
| 7 | Application | The protocol the user's program speaks | HTTP, SMTP, DNS |
| 6 | Presentation | Data formats and encodings | Character sets, compression, (in theory) encryption |
| 5 | Session | Ongoing dialogs: start, checkpoint, restart | Login sessions, RPC (remote procedure call) dialogs |
| 4 | Transport | Data flow between programs on two hosts | TCP, UDP |
| 3 | Network | Addressing and routing across networks | IP |
| 2 | Data link | Delivery to a neighbor on the same link | Ethernet, Wi-Fi |
| 1 | Physical | Bits on a medium | Copper, fiber, radio |
The OSI protocols that ISO built to go with the model mostly lost to TCP/IP, but the model's vocabulary won. Engineers everywhere say "layer 2" for switching, "layer 3" for routing, "layer 4" for transport and "layer 7" for applications, as in "a layer 7 load balancer". A few OSI-born protocols also live on inside TCP/IP networks, such as the IS-IS (Intermediate System to Intermediate System) routing protocol that many large providers use.
Some terms from the lower layers come up again and again:
- A link connects neighbors. If it connects exactly two devices, such as a fiber between two routers, it's point-to-point. If many devices share it, as on Ethernet or Wi-Fi, it's a multi-access network, and the link layer needs rules for who may send when.
- The network layer provides one packet format that works over every kind of link, a global addressing scheme, and the routing that decides where packets go next.
- From the transport layer up, protocols run (in principle) only in the end hosts.
The TCP/IP model
The Internet's protocols follow an older and simpler model that grew out of research on the ARPANET, the US research network of the late 1960s and 1970s. It's usually drawn with four layers (some books split the bottom one and count five):

From the bottom up:
- Link. Whatever technology carries packets to the next device: Ethernet, Wi-Fi, a cellular radio, a fiber line. TCP/IP doesn't define these. It runs over them. Article 14 starts the link-layer part of the course.
- Internet. IP (the Internet Protocol), in its two versions, IPv4 and IPv6. IP's unit is the IP datagram, which this course usually just calls a packet. An IPv4 datagram can be up to 65,535 bytes. An IPv6 one has the same normal limit, and up to about 4 GB as a rarely used "jumbogram". When a datagram is bigger than a link can carry, IP can split it into fragments and the destination puts them back together (reassembly), as article 46 explains.
- Transport. TCP and UDP, plus newer protocols such as QUIC (an encrypted transport that runs on top of UDP) and SCTP. Transport protocols identify programs by port numbers.
- Application. Everything else: HTTP, DNS (the Domain Name System, which turns names into addresses), SMTP (email delivery), SSH (Secure Shell, for remote logins), TLS (Transport Layer Security, for encryption). There's no separate session or presentation layer. Applications handle those jobs themselves, which is why TLS, a "presentation"-style job in OSI terms, is just a library an application uses.
The two transports, and their siblings
The two main transport protocols could hardly be more different:
- TCP (the Transmission Control Protocol) is connection-oriented and reliable. It splits the application's byte stream into segments, numbers every byte, acknowledges what arrives, retransmits what doesn't, and puts everything back in order. As article 1 showed, it doesn't preserve message boundaries.
- UDP (the User Datagram Protocol) adds almost nothing to IP: port numbers so the right program gets each datagram, and a checksum. It preserves message boundaries but makes no promises about delivery, order or rate.
Two other transports were designed to sit between them. SCTP (the Stream Control Transmission Protocol) gives reliable delivery with message boundaries and several independent streams in one association. It's used in telephone signaling and inside WebRTC data channels (carried over DTLS, a version of TLS for datagrams, and UDP). DCCP (the Datagram Congestion Control Protocol) offered unreliable datagrams with congestion control. It never caught on: Linux removed its implementation in kernel 6.16 (2025). The niche DCCP aimed at is now filled by QUIC, covered in article 86.
Helper protocols: layers 2.5 and 3.5
A few important protocols don't fit neatly into one layer, and people often give them half numbers:
- ARP (the Address Resolution Protocol, "layer 2.5") finds the Ethernet address that goes with an IPv4 address on the local network. IPv6 does the same job with ICMPv6 Neighbor Discovery. Articles 21 and 42 cover both.
- ICMP (the Internet Control Message Protocol, "layer 3.5") carries error reports and diagnostics for IP, such as "destination unreachable" or "time exceeded".
pingandtracerouteare built on it. It's carried inside IP datagrams just like a transport protocol, but it's part of IP's own machinery. ICMPv6 is the IPv6 version and does much more. - IGMP (the Internet Group Management Protocol, for IPv4) and MLD (Multicast Listener Discovery, for IPv6) manage membership of multicast groups, sets of hosts that all receive the same packets, as article 44 explains.
Which devices implement which layers
Not every device uses every layer to do its job:

- End hosts (also called end systems), such as laptops, phones and servers, run every layer.
- Routers forward using the Internet and link layers. A router has at least two network interfaces, because its job is to connect networks. It's called an intermediate system, and IP is a hop-by-hop protocol because it runs on every router along the path.
- Switches (and the older bridges) forward using only the link layer. IP doesn't address them, so from the routers' and hosts' point of view they're invisible.
The transport and application layers are end-to-end: they exist only in the two hosts.
Real devices blur this. A managed switch or a router also runs SSH, a web interface and SNMP (the Simple Network Management Protocol) for management, so it has a full stack and acts as a host for that traffic. Any machine with more than one network interface is multihomed. A multihomed host isn't a router unless it's configured to forward packets between its interfaces. On Linux that's one setting (net.ipv4.ip_forward=1), and the course lab's router is just an ordinary Linux network namespace with that setting turned on.
Encapsulation: wrapping each layer's data
When an application sends data, each layer adds its own information and passes the result down:
- The application hands TCP some bytes, here an HTTP request.
- TCP puts a TCP header in front, making a segment.
- IP puts an IP header in front of the segment, making a datagram.
- The link layer puts a frame header in front and, for Ethernet, a checksum trailer behind, making a frame.
Each layer treats what it got from above as opaque data. IP doesn't look inside the TCP segment, and Ethernet doesn't look inside the IP datagram. This wrapping is called encapsulation, and the unwrapping at the receiver is decapsulation. The unit at each layer is called that layer's protocol data unit (PDU): frame, datagram, segment, message.
Here's a real example. In the course lab, curl (a command-line web client) on the host client (192.0.2.10) fetched http://www.example.com/ from an nginx web server on server (198.51.100.10), while tcpdump captured every packet. This is the whole exchange, as summarized by tshark, the command-line version of the Wireshark packet analyzer (article 6 introduces these tools):
1 0.000000 192.0.2.10 → 198.51.100.10 TCP 74 58686 → 80 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM TSval=3370912022 TSecr=0 WS=128
2 0.000055 198.51.100.10 → 192.0.2.10 TCP 74 80 → 58686 [SYN, ACK] Seq=0 Ack=1 Win=65160 Len=0 MSS=1460 SACK_PERM TSval=1055565174 TSecr=3370912022 WS=128
3 0.000063 192.0.2.10 → 198.51.100.10 TCP 66 58686 → 80 [ACK] Seq=1 Ack=1 Win=64256 Len=0 TSval=3370912022 TSecr=1055565174
4 0.000122 192.0.2.10 → 198.51.100.10 HTTP 145 GET / HTTP/1.1
5 0.000129 198.51.100.10 → 192.0.2.10 TCP 66 80 → 58686 [ACK] Seq=1 Ack=80 Win=65152 Len=0 TSval=1055565174 TSecr=3370912022
6 0.000263 198.51.100.10 → 192.0.2.10 HTTP 372 HTTP/1.1 200 OK (text/html)
7 0.000266 192.0.2.10 → 198.51.100.10 TCP 66 58686 → 80 [ACK] Seq=80 Ack=307 Win=64128 Len=0 TSval=3370912022 TSecr=1055565174
8 0.000324 192.0.2.10 → 198.51.100.10 TCP 66 58686 → 80 [FIN, ACK] Seq=80 Ack=307 Win=64128 Len=0 TSval=3370912022 TSecr=1055565174
9 0.000960 198.51.100.10 → 192.0.2.10 TCP 66 80 → 58686 [FIN, ACK] Seq=307 Ack=81 Win=65152 Len=0 TSval=1055565175 TSecr=3370912022
10 0.000962 192.0.2.10 → 198.51.100.10 TCP 66 58686 → 80 [ACK] Seq=81 Ack=308 Win=64128 Len=0 TSval=3370912023 TSecr=1055565175
Each line is one frame: its number, the time in seconds since the capture started, source → destination address, the protocol tshark recognized, the frame length in bytes, and a summary. In the summary, 58686 → 80 are the source and destination ports, and the brackets hold TCP flags: SYN opens a connection, ACK acknowledges data, and FIN closes. Seq, Ack and Len are byte positions in the stream and the amount of data carried, and Win is the receive window from article 2. The rest (MSS, SACK_PERM, TSval, TSecr, WS) are TCP options, covered in the TCP articles. You don't need them yet.
Ten frames: a three-way handshake (1–3), the request (4), its acknowledgment (5), the response (6, 7), and the connection shutting down (8–10). The number after TCP or HTTP is the frame length in bytes. Frame 4 carries the request, and it's 145 bytes. Here's what those bytes are made of:

The TCP header is 32 bytes instead of the minimum 20 because it carries 12 bytes of options, here timestamps that TCP uses to measure round-trip time (article 60). The Ethernet frame also ends with a 4-byte frame check sequence (FCS), a CRC. Network cards check and remove it on receipt, so captures normally don't include it.
The same frame as raw bytes
Wireshark-style tools decode those layers for you, but it's worth seeing the raw bytes once. This is frame 4 as hexadecimal, 16 bytes per line, with the offset (the position of the line's first byte, also in hex) at the left. The right column shows the same bytes as text, with a dot for each byte that isn't a printable character:
0000 02 00 00 00 01 01 02 00 00 00 01 10 08 00 45 00 ..............E.
0010 00 83 35 88 40 00 40 06 18 a5 c0 00 02 0a c6 33 ..5.@.@........3
0020 64 0a e5 3e 00 50 d6 84 6d 3b 5a 50 dc 8b 80 18 d..>.P..m;ZP....
0030 01 f6 ec bd 00 00 01 01 08 0a c8 ec 09 16 3e ea ..............>.
0040 a5 76 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 .vGET / HTTP/1.1
0050 0d 0a 48 6f 73 74 3a 20 77 77 77 2e 65 78 61 6d ..Host: www.exam
0060 70 6c 65 2e 63 6f 6d 0d 0a 55 73 65 72 2d 41 67 ple.com..User-Ag
0070 65 6e 74 3a 20 63 75 72 6c 2f 38 2e 31 34 2e 31 ent: curl/8.14.1
0080 0d 0a 41 63 63 65 70 74 3a 20 2a 2f 2a 0d 0a 0d ..Accept: */*...
0090 0a .
Reading it with the layer boundaries in mind. Each hex digit is 4 bits, so two digits make one byte, and you convert by place value: 83 is 8 × 16 + 3 = 131, and e5 3e is 0xe53e = 14 × 4096 + 5 × 256 + 3 × 16 + 14 = 58686. A MAC address is the hardware address of a network card (article 14 covers them):
| Offset (hex) | Bytes | Layer | Meaning |
|---|---|---|---|
00–05 | 02 00 00 00 01 01 | Ethernet | Destination MAC: the router r1 |
06–0b | 02 00 00 00 01 10 | Ethernet | Source MAC: client |
0c–0d | 08 00 | Ethernet | Type 0x0800: the payload is IPv4 |
0e | 45 | IP | First digit: version 4. Second digit: header length 5 × 4 = 20 bytes |
10–11 | 00 83 | IP | Total length 0x83 = 131 bytes |
16 | 40 | IP | Time to live 0x40 = 64 (a hop limit, article 23) |
17 | 06 | IP | Protocol 6: the payload is TCP |
1a–1d | c0 00 02 0a | IP | Source address 192.0.2.10 |
1e–21 | c6 33 64 0a | IP | Destination address 198.51.100.10 |
22–23 | e5 3e | TCP | Source port 0xe53e = 58686 |
24–25 | 00 50 | TCP | Destination port 0x50 = 80 |
2e | 80 | TCP | First digit: header length 8 × 4 = 32 bytes |
42 onward | 47 45 54 20 ... | HTTP | GET / HTTP/1.1 and the rest of the request |
Notice that the destination MAC address belongs to the router, not the server. The server is on another network, so the frame only has to get to the next hop. The IP header carries the server's address, and stays the same all the way. Article 27 follows a packet through this process in detail.
How much is header?
Headers cost bandwidth. This chart splits every frame of the exchange into its layers:

For this tiny page, only 385 of the 1,061 bytes captured were HTTP data, and 8 of the 10 frames carried no application data at all. That's typical of short exchanges. For a large download, full-sized frames carry about 1,448 bytes of data each behind 66 bytes of headers, so the overhead falls to under 5%. This is one reason protocols such as HTTP/2 and HTTP/3 reuse one connection for many requests instead of opening a new one each time.
Demultiplexing: delivering a packet upward
Encapsulation also lets many protocols share one link. This is called protocol multiplexing. On the way up, each layer reads one field of its header to decide which protocol or program gets the payload next. That's demultiplexing:

Following frame 4 up through the receiving host:
- Link layer. The network card accepts the frame because the destination MAC address is its own (or a broadcast or multicast address it listens to), and checks the FCS. The Ethernet Type field, 0x0800, says the payload is IPv4. Other common values are 0x86DD for IPv6 and 0x0806 for ARP.
- IP layer. IP checks the header checksum (IPv4 only) and the destination address. If the packet is addressed to this host, it reads the Protocol field (called Next Header in IPv6). Common values are 1 (ICMP), 2 (IGMP), 6 (TCP), 17 (UDP) and 58 (ICMPv6). If the packet arrived in fragments, IP reassembles it first.
- Transport layer. TCP checks its checksum, finds the connection that matches the addresses and ports, and puts the 79 bytes in that connection's receive buffer. The program that owns the socket, here nginx listening on port 80, reads them.
Two values in that list are surprising: Protocol 4 means "another IPv4 packet is inside this one", and 41 means "an IPv6 packet is inside". An IP packet carrying an IP packet breaks the neat layering picture, but it's the basis of tunneling, which VPNs (virtual private networks) and IPv6 transition mechanisms depend on. Article 20 covers tunnels.
At every layer, the receiver also checks that the data is really for it and undamaged: the MAC address, the IP address, and the checksums. A failure at any layer just drops the packet, as you saw in article 2.
Common questions
Is TLS a layer 6 or a layer 7 protocol?
It depends on who you ask, which shows the limits of the OSI model. TLS does presentation-style work (encryption and encoding), but in the TCP/IP model it's simply part of the application layer: a library the application calls, running over TCP. QUIC builds TLS 1.3 into the transport itself. It's better to know what TLS does than to argue about its number.
If the TCP/IP model has four layers, why does everyone say "layer 7"?
Because the OSI numbering became the common vocabulary. "Layer 7" just means the application layer, "layer 4" transport, "layer 3" IP and "layer 2" the link. Nobody is claiming the Internet has seven real layers.
Why does my capture never show the Ethernet FCS?
Network cards check the FCS and remove it before handing the frame to the operating system, so capture tools never see it. Some cards can be told to keep it, which is occasionally useful when debugging hardware.
Why is the destination MAC address in frame 4 the router's and not the server's?
MAC addresses only matter on one link. The server is on another network, so the client sends the frame to its router, using the router's MAC address. The router then builds a new frame for the next link. The IP addresses inside stay the same end to end.
Does a router ever look at TCP or HTTP headers?
A plain router doesn't need to. But many devices do: firewalls read ports, NAT rewrites ports, load balancers can read HTTP, and routers spread traffic over several links by hashing the port numbers. Each of these breaks strict layering a little. Articles 34 and 35 cover firewalls and NAT.
How big can the headers get?
An Ethernet header is 14 bytes (18 with a VLAN tag, a 4-byte label that article 15 explains). An IPv4 header is 20 to 60 bytes, and an IPv6 header is a fixed 40 bytes plus any extension headers. A TCP header is 20 to 60 bytes and a UDP header is always 8. A typical TCP packet over IPv4 and Ethernet has 54 to 66 bytes of headers (66 with the timestamps option, as in the lab capture), and over IPv6 74 to 86.
What does "decapsulation" do with the header it removes?
The header has done its job by the time it's removed: it said who the packet was for, which protocol comes next, and, for TCP, where the bytes fit in the stream. The receiving layer uses that information and passes only the payload up.
Summary
- Network software is built in layers. Each layer offers a service to the one above, uses the one below, and talks to its peer layer on the other machine.
- The seven-layer OSI model supplies the common vocabulary (layer 2, 3, 4, 7). The TCP/IP model has four layers: link, internet (IP), transport (TCP, UDP, QUIC) and application. Helper protocols ARP, ICMP, IGMP and MLD sit between layers.
- End hosts run all layers. Routers forward at the IP layer, hop by hop, and switches at the link layer, invisible to IP. Transport and application are end to end.
- Encapsulation: each layer adds its header to the data from above. In the lab capture, a 79-byte HTTP request became a 111-byte TCP segment, a 131-byte IP datagram and a 145-byte Ethernet frame.
- Demultiplexing: on the way up, the Ethernet Type, the IP Protocol (or Next Header) and the destination port each pick the next receiver. Protocols 4 and 41 mean another IP packet is inside, a tunnel.
- Headers dominate short exchanges (385 of 1,061 bytes were data in the lab), but cost under 5% in a bulk transfer.
Practice questions
1. A frame arrives with Ethernet Type 0x86DD and, inside, Next Header 17 and destination port 53. Which protocols handle it, from the bottom up?
Answer
Ethernet, then IPv6 (0x86DD), then UDP (17), then whatever program is listening on UDP port 53, normally a DNS server.
2. A UDP datagram carries 100 bytes of data over IPv4 and Ethernet, with no IP options and no VLAN tag. How long is the frame as captured, and how long on the wire including the FCS?
Answer
100 bytes of data + 8 (UDP) + 20 (IPv4) + 14 (Ethernet) = 142 bytes as captured. With the 4-byte FCS it's 146 bytes. (On the wire there are also 8 bytes of preamble and a 12-byte gap between frames, which no capture shows.)
3. In the hex dump, which byte tells the receiving host that the IP payload is TCP, and what's its value?
Answer
The byte at offset 0x17, the IPv4 Protocol field. Its value is 06, which means TCP.
4. Which of these devices normally needs to run TCP to do its forwarding job: an Ethernet switch, an IP router, a web server? Which might run TCP anyway, and why?
Answer
Only the web server needs TCP for its main job. A switch forwards at the link layer and a router at the IP layer. Both often run TCP anyway, for management: SSH, a web interface, or routing protocols such as BGP (the Border Gateway Protocol, which networks use to exchange routes), which runs over TCP.
5. Why do the 8 frames with no HTTP data in the lab exchange matter for performance?
Answer
Each costs a full set of headers and its own processing at every device, but delivers no application data. Opening a new connection for every small request multiplies this overhead (and adds round trips), which is one reason modern HTTP reuses connections.
6. A packet has IPv4 Protocol 41. What's inside it, and where might you see one?
Answer
An entire IPv6 packet. This is IPv6-in-IPv4 tunneling, used by some IPv6 transition mechanisms and VPN-style tunnels to carry IPv6 across an IPv4-only network.
Previous: The Design Principles of the Internet: End-to-End, Fate Sharing, Error and Flow Control
Next: Ports, Names and Applications: Client/Server, Peer-to-Peer and the Sockets API

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.