1. Layers & Encapsulation
How a few bytes of HTTP become a frame on the wire
Imagine you work at a company and need to send a contract to a client in another city. You write the letter, fold it, and put it in an envelope with the client's name on it. You drop it in the out-tray. Someone from the mailroom picks it up and slips it into a courier pouch labelled with the client's street address. The courier company puts the pouch in a crate labelled with the depot it's heading to next, and loads that crate on a truck.
Notice what each person actually looks at. The truck driver reads the crate label and nothing else. The depot opens the crate, reads the pouch label, and puts the pouch in a new crate for the next leg. The client's receptionist opens the pouch and reads the name on the envelope. Only the client opens the envelope and reads the letter. Nobody needs to understand anybody else's job. The truck driver doesn't care what's in the contract; you don't care which highway the truck takes.
That is the whole idea of network layering. When your browser sends
GET / HTTP/1.1 to a web server, those bytes get wrapped in a TCP "envelope", which
gets wrapped in an IP "pouch", which gets wrapped in an Ethernet "crate". Each wrapping
is called encapsulation, and by the end of this page you'll be able to read every
single byte of such a package in a hex dump.
Why Layer at All?
Getting bytes from a program on one machine to a program on another involves a ridiculous number of separate problems:
- Turning bits into voltages, light pulses, or radio waves, and back.
- Deciding who gets to talk on a shared cable or Wi-Fi channel, and noticing when a frame got corrupted.
- Giving every machine on the planet an address and finding a path between networks.
- Getting the data to the right program on that machine, in order, without gaps.
- Agreeing on what the bytes actually mean: a web page, an email, a DNS query.
You could write one giant program that does all of this. It would also need rewriting every time someone invented a new kind of cable. Layering splits the problem into a stack, where each layer solves one problem and makes two promises:
-
It offers a service to the layer above
IP tells TCP: "hand me some bytes and an address, and I'll try to get them to that machine." TCP doesn't need to know whether the path is fibre, copper, or a satellite.
-
It uses the service of the layer below
IP in turn asks the link layer: "deliver this to that box on the local network," without caring whether "the local network" is Ethernet or Wi-Fi.
-
It talks only to its peer
The TCP code on your laptop is, logically, having a conversation with the TCP code on the server. The header TCP adds is a note to the other TCP, and it is the only thing that will read it. The same goes for IP-to-IP and Ethernet-to-Ethernet. That's the sense in which layers have peer-to-peer conversations.
The payoff is that you can swap pieces. Your laptop moves from Ethernet to Wi-Fi to a phone hotspot, and your browser never notices. IPv6 replaces IPv4 underneath, and HTTP doesn't change. A brand-new transport like QUIC can be deployed without changing routers, because routers stop looking at the stack before they reach it (though middleboxes are another story, as we'll see below).
The rule of layering
Layer N treats everything handed down from layer N+1 as an opaque blob of bytes. It sticks its own header on the front (and sometimes a trailer on the back), and passes the bigger blob down. On the way up, each layer strips off exactly the header its peer added, and passes the rest up.
Two Models: OSI and TCP/IP
There are two famous ways of drawing the stack, and you'll see both everywhere.
The OSI reference model was published by ISO as ISO 7498 in 1984. It has seven layers. The TCP/IP model (also called the Internet model) is the one written down by the people who actually built the Internet; RFC 1122 (October 1989) describes four layers: Link, Internet, Transport, and Application. Textbooks often use a five-layer hybrid that splits TCP/IP's Link layer into a separate Link and Physical layer.
The units have names
Each layer's chunk of data is called a PDU, and people are surprisingly fussy about the names:
| Layer (TCP/IP) | OSI # | PDU name | What it adds | Example |
|---|---|---|---|---|
| Application | 5–7 | message (or just "data") | Meaning: requests, responses, records | HTTP, DNS |
| Transport | 4 | segment (TCP) / datagram (UDP) | Ports, ordering, reliability | TCP, UDP |
| Internet | 3 | packet | Global addresses, hop limit | IPv4, IPv6 |
| Link | 2 | frame | Local addresses, error check | Ethernet, Wi-Fi |
| Physical | 1 | bits / symbols | Signalling on the medium | 1000BASE-T, fibre |
In casual conversation everyone says "packet" for everything, and that's fine. But when someone says "the frame was dropped" versus "the packet was dropped", they're telling you which layer they're thinking about.
An honest word about OSI
OSI is how networking is taught. TCP/IP is what is deployed.
OSI started out as more than a diagram: it was meant to come with a whole family of protocols (the transport protocol TP4, the network protocol CLNP, X.400 email, the X.500 directory) that would replace the ad-hoc Internet protocols. In the "protocol wars" of the 1980s and early 90s, the simpler, already-running TCP/IP suite won. Today almost none of the OSI protocol suite is in everyday use. A few pieces survived: IS-IS routing is still common inside large ISPs, and the X.509 certificates in every TLS handshake come from the X.500 directory standards.
What did survive is OSI's vocabulary. Engineers say "a Layer 2 switch", "a Layer 3 router", "a Layer 7 load balancer", and those numbers are OSI numbers. Layers 5 and 6 (Session and Presentation) are the awkward ones: on the real Internet there is no separate session or presentation protocol in the stack; those jobs (keeping sessions alive, character encoding, compression, encryption) are done inside the application or by libraries it uses. TLS is a good example of how fuzzy the boundaries get: it runs on top of TCP, below HTTP, and people variously call it layer 4, 5, 6, or 7.
Use OSI numbers as names, TCP/IP as the map
Treat the OSI model as a shared numbering scheme for talking about the stack, and the TCP/IP model as the description of what the software actually does. The rest of this series uses the TCP/IP layers, with OSI numbers where the industry does.
Encapsulation: Envelopes Inside Envelopes
Let's follow a real request down the stack. Your browser wants the home page of
example.com. The smallest valid HTTP/1.1 request is:
The application data (37 bytes)
GET / HTTP/1.1\r\n
Host: example.com\r\n
\r\n
\r\n is a carriage return + line feed (bytes 0d 0a), which
HTTP/1.1 uses to end every line. A blank line ends the headers. Counting:
14 + 2 + 17 + 2 + 2 = 37 bytes.
The browser hands these 37 bytes to the operating system through a socket. From here, each layer of the kernel's network stack wraps it:
Notice that the HTTP bytes never move or change: each layer just adds bytes around them.
A packet capture of this frame is literally the Ethernet header, then the IPv4 header,
then the TCP header, then GET / HTTP/1.1..., then a 4-byte checksum, back to back.
Let's open each envelope, from the inside out.
The TCP header (20 bytes)
TCP's job is getting bytes to the right program and in the right order. Its header is a note from your laptop's TCP to the server's TCP:
| Bytes | Field | Our value | Meaning |
|---|---|---|---|
| 0–1 | Source port | c7 38 = 51000 |
The browser's temporary port, picked by the OS |
| 2–3 | Destination port | 00 50 = 80 |
HTTP's well-known port |
| 4–7 | Sequence number | 1a 2b 3c 4d = 439041101 |
Position of this segment's first byte in the byte stream |
| 8–11 | Acknowledgment number | 5e 6f 70 81 = 1584361601 |
"I've received everything from you up to here" |
| 12 (high 4 bits) | Data offset | 5 (×4 = 20 bytes) |
Header length in 32-bit words |
| 12 (low 4 bits) | Reserved | 0 |
Must be zero |
| 13 | Flags | 18 = ACK + PSH |
Bits are CWR ECE URG ACK PSH RST SYN FIN |
| 14–15 | Window | fa f0 = 64240 |
How many more bytes I'm willing to receive |
| 16–17 | Checksum | 81 1c |
Covers TCP header, data, and a pseudo-header of the IP addresses |
| 18–19 | Urgent pointer | 00 00 |
Only meaningful if URG is set; essentially unused |
The ports are what let one machine run a web server, an SSH server, and a mail server at once: the destination port tells the server's kernel which program gets the data. The sequence and acknowledgment numbers are what make TCP reliable; they look random because each connection starts from a randomly chosen ISN. The TCP article later in this series covers how they work.
Simplified on purpose
A real Linux or macOS segment would usually carry a 12-byte timestamps option, making the TCP header 32 bytes with a data offset of 8. We're using the minimal 20-byte header with no options to keep the arithmetic clean. And of course a real request would be preceded by the three-way handshake (SYN, SYN-ACK, ACK) that establishes the connection.
The IPv4 header (20 bytes)
IP's job is getting the segment to the right machine, possibly across dozens of networks. Its header is a note from your laptop's IP to the server's IP, but routers along the way read it too (we'll come back to that):
| Bytes | Field | Our value | Meaning |
|---|---|---|---|
| 0 | Version / IHL | 45 |
Version 4; header length 5 × 4 = 20 bytes |
| 1 | DSCP / ECN | 00 |
Traffic class (6 bits) and congestion signalling (2 bits); 0 = ordinary |
| 2–3 | Total length | 00 4d = 77 |
Whole packet: 20 (IP) + 20 (TCP) + 37 (data) |
| 4–5 | Identification | 1c 46 |
Ties together fragments of the same packet |
| 6–7 | Flags / fragment offset | 40 00 |
Top bits 010 = Don't Fragment; offset 0 |
| 8 | TTL | 40 = 64 |
Hop limit: each router subtracts 1; at 0 the packet is dropped |
| 9 | Protocol | 06 = TCP |
What's inside: 6 = TCP, 17 = UDP, 1 = ICMP |
| 10–11 | Header checksum | 20 62 |
Covers the IP header only |
| 12–15 | Source address | c0 a8 01 0a = 192.168.1.10 |
Your laptop |
| 16–19 | Destination address | cb 00 71 50 = 203.0.113.80 |
The web server |
The Protocol field is the hand-off label: it tells the receiving IP layer which
layer above should get the payload. TCP is 6, so the server's kernel knows to pass these
57 bytes to TCP. (We're using 203.0.113.80 as the server because the
203.0.113.0/24
block is reserved for documentation. The real example.com has a different address.)
The header checksum is the 16-bit Internet checksum, and it's small enough to compute by hand:
Treat the header as ten 16-bit words, with the checksum field set to zero, and add them:
Fold the carry back in: . Flip every bit: . That's the value in bytes 10–11.
The receiver adds all ten words including the checksum; for an intact header the
result folds to ffff, whose complement is zero. The
Checksums & CRC article explains why this works and what it can and
can't catch.
The Ethernet header (14 bytes) and trailer (4 bytes)
Ethernet's job is getting the packet to the next box on this local network. On a home network that next box is your router, not the web server: Ethernet addresses only mean something on the local link.
| Bytes | Field | Our value | Meaning |
|---|---|---|---|
| 0–5 | Destination MAC | 02:00:00:cc:dd:02 |
Your router's interface |
| 6–11 | Source MAC | 02:00:00:aa:bb:01 |
Your laptop's network card |
| 12–13 | EtherType | 08 00 |
What's inside: 0800 = IPv4, 86dd = IPv6, 0806 = ARP |
| (end) | FCS | 21 57 c1 37 |
CRC-32 over destination MAC through payload and pad, sent after the payload |
The EtherType plays the same role as IP's Protocol field and TCP's port: it tells
the receiver who to hand the payload to. (Values of 1536 / 0600 and up are types;
values of 1500 and below are interpreted as a length, a leftover from the original
IEEE 802.3 framing.)
The FCS (Frame Check Sequence) is a
CRC-32
over everything from the destination MAC to the last payload byte. Unlike the other
headers, it's a trailer, because the sender computes it while the bytes stream out and
appends the result at the end. The MAC addresses here start with 02, which marks them as
locally administered
addresses, so they can't collide with any real hardware. The
Ethernet article goes into MAC addresses properly, and
ARP explains how your laptop learned the router's MAC in the first place.
On the actual wire, a frame is also preceded by an 8-byte preamble and start-of-frame delimiter (a fixed pattern that lets the receiver lock on to the bit timing) and followed by a mandatory 12-byte idle gap. Those are physical-layer details: packet capture tools never show them, and usually strip the FCS as well.
The Whole Frame, Byte by Byte
Here is the complete 95-byte frame, exactly as it would appear in a capture that kept the FCS. Every value is real: the lengths, protocol numbers, IPv4 checksum, TCP checksum (including its pseudo-header), and Ethernet CRC-32 were all computed for these exact bytes.
Hex dump: 95 bytes
0000 02 00 00 cc dd 02 02 00 00 aa bb 01 08 00 45 00 ..............E.
0010 00 4d 1c 46 40 00 40 06 20 62 c0 a8 01 0a cb 00 .M.F@.@. b......
0020 71 50 c7 38 00 50 1a 2b 3c 4d 5e 6f 70 81 50 18 qP.8.P.+<M^op.P.
0030 fa f0 81 1c 00 00 47 45 54 20 2f 20 48 54 54 50 ......GET / HTTP
0040 2f 31 2e 31 0d 0a 48 6f 73 74 3a 20 65 78 61 6d /1.1..Host: exam
0050 70 6c 65 2e 63 6f 6d 0d 0a 0d 0a 21 57 c1 37 ple.com....!W.7
Ethernet bytes 0–13 and 91–94 · IPv4 bytes 14–33 · TCP bytes 34–53 · HTTP bytes 54–90
Try reading it the way a receiver would:
-
Offset 0x00: Ethernet
Six bytes of destination MAC, six of source MAC, then
08 00. EtherType0800means "an IPv4 packet starts right after me", at offset 14. -
Offset 0x0e: IPv4
45: version 4, header is 5 words = 20 bytes long, so the next header starts at 14 + 20 = 34. Total length00 4d= 77 bytes, so the packet ends at 14 + 77 = 91. Protocol06: the payload is TCP. -
Offset 0x22: TCP
Ports
c7 38→00 50(51000 → 80). At offset 46, the byte50: data offset 5 words = 20 bytes, so the data starts at 34 + 20 = 54. Flags18= ACK and PSH. -
Offset 0x36: HTTP
Bytes 54 to 90 are the 37 bytes of
GET / HTTP/1.1.... You can read them in the ASCII column. Destination port 80 is how the server knows to hand them to its web server. -
Offset 0x5b: FCS
The last 4 bytes are the CRC-32 of bytes 0–90. Its value is
0x37C15721, and Ethernet sends it least-significant byte first, which is why the dump reads21 57 c1 37.
Every header carries enough information to find the next one: a length field tells you where it ends, and a type field tells you what comes after. That pair, "how long am I" and "what's inside me", is the glue that makes layering work.
Pseudocode
The sending side is just "prepend a header, pass it down" repeated:
function send_http(request_bytes, conn):
segment = tcp_header(conn.src_port, conn.dst_port,
conn.seq, conn.ack, flags=ACK|PSH) + request_bytes
segment.checksum = internet_checksum(pseudo_header(conn) + segment)
packet = ipv4_header(total_length = 20 + len(segment),
ttl = 64, protocol = 6, # 6 = TCP
src = conn.src_ip, dst = conn.dst_ip) + segment
packet.header_checksum = internet_checksum(packet[0:20])
next_hop_mac = arp_lookup(route_lookup(conn.dst_ip)) # usually the router
frame = eth_header(dst = next_hop_mac, src = my_mac,
ethertype = 0x0800) + packet # 0x0800 = IPv4
frame = frame + crc32(frame) # FCS trailer
nic_transmit(frame)
The receiving side reverses it, using each type field to pick who goes next:
function receive(frame):
if crc32_check(frame) fails: drop
if frame.dst_mac != my_mac and not broadcast: drop
match frame.ethertype:
0x0800 -> packet = frame.payload # IPv4
0x86DD -> handle_ipv6(frame.payload)
0x0806 -> handle_arp(frame.payload)
if packet.dst != my_ip: drop (or forward, if I'm a router)
match packet.protocol:
6 -> segment = packet.payload # TCP
17 -> handle_udp(packet.payload)
socket = lookup(segment.dst_port, segment.src_port, packet.src, packet.dst)
socket.deliver(segment.payload) # "GET / HTTP/1.1 ..."
Try It: The Encapsulation Stepper
Application data
Type any request. Escapes \r \n \t \0 \\ and \xHH become single bytes; a real line break is 0a. Up to 1460 bytes (the MSS).
Envelopes (to scale)
Hex dump
Decapsulation: Who Opens Which Envelope
On the receiving side, each layer peels off its own header and passes the rest up, as in the pseudocode above. But the frame passes through other devices before it reaches the server, and the interesting question is how far up the stack each device reads.
Switch (Layer 2)
Reads the destination MAC and looks it up in its table of which port each address lives on, and notes the source MAC so it learns which port the laptop is on. It forwards the frame out of that one port (or floods it out of all ports if the address is unknown), unchanged. A store-and-forward switch also checks the FCS and drops corrupt frames. It never looks at the IP header; to a switch, everything after the EtherType is just payload.
Router (Layer 3)
Checks the FCS, then throws the Ethernet header away. Reads the destination IP, looks up the next hop, decrements the TTL (64 → 63), and updates the IPv4 header checksum to match. Then it builds a brand-new Ethernet header (its own MAC as source, the next hop's MAC as destination) and a new FCS. The TCP segment and HTTP data pass through untouched.
That last point is worth sitting with. The IP addresses in our packet stay the same from your laptop all the way to the server (with one big exception, below), but the Ethernet header is replaced at every router. Each hop is a separate local delivery with its own frame. The IP packet is the pouch; the Ethernet frame is the crate, and every depot uses a fresh crate.
Finally, the server checks the FCS and sees its own MAC, sees EtherType 0800 and
hands the packet to IP. IP sees its own address and protocol 6 and hands the segment to
TCP. TCP matches the four-tuple (src IP, src port, dst IP, dst port) to an open connection
and delivers 37 bytes to the web server process, which finally reads
GET / HTTP/1.1.
Real networks bend the rules
The clean picture above is the ideal. In practice, many boxes peek above "their" layer. Your home router does NAT, rewriting the source IP and the TCP source port (and fixing the TCP checksum). Firewalls inspect ports, load balancers read HTTP headers, and "Layer 3 switches" route. These are deliberate layer violations, and they are one reason new transport protocols are hard to deploy: middleboxes break on anything they don't recognise. QUIC sidesteps this by running over UDP and encrypting nearly all of its own header.
Device
Field by field
Overhead and the MTU
Headers aren't free. Our request carried 37 bytes of actual HTTP in a 95-byte frame:
That's bad, but it's a tiny request. The fixed 58 bytes of headers and trailer are amortised much better over big payloads, and payloads can only get so big. Standard Ethernet limits the frame payload (the IP packet) to 1500 bytes, a limit called the MTU. Take away 20 bytes of IPv4 header and 20 of TCP header and you get the largest TCP payload per frame, the MSS, of 1460 bytes.
A full-size frame
Ethernet header 14
IPv4 header 20
TCP header 20
TCP payload 1460 (MSS)
FCS 4
----
Frame 1518 bytes
+ preamble/SFD 8
+ inter-frame gap 12
----
Wire time 1538 byte-times
Payload efficiency: 1460 / 1518 ≈ 96.2% of the frame, or 1460 / 1538 ≈ 94.9% of the wire. That's why a "1 Gb/s" link moves roughly 949 Mb/s of TCP payload at best (less with TCP options, more with jumbo frames).
At the other extreme, Ethernet has a minimum frame size of 64 bytes including the FCS, so the payload must be at least 46 bytes. A sender pads anything shorter. Our 77-byte IP packet clears that easily, so no padding was needed.
What happens when a packet is bigger than the next link's MTU? In IPv4, a router can split it into fragments (that's what the Identification and fragment-offset fields are for) unless the Don't Fragment bit is set, which it was in our packet. Modern stacks set DF and instead discover the path's MTU, avoiding fragmentation entirely. The IPv4 addressing article picks this thread up.
Where to Go Next
This page skimmed every layer. The rest of the Foundations section slows down:
- Ethernet: MAC addresses, how switches learn which port is which, and why frames have minimum sizes.
- ARP: how your laptop found
02:00:00:cc:dd:02given only the router's IP. - IPv4 Addressing: subnets, prefixes, and how a host decides "local or via the router?".
- TCP: what those sequence and acknowledgment numbers are actually doing.
Two articles in other series zoom in on pieces of this page:
- TLS (Encryption): what happens when one more envelope, an encrypted one, sits between TCP and HTTP.
- Checksums & CRC (Error Correction): the maths behind the IPv4 checksum and the Ethernet FCS.
Key Takeaways
- Layering splits networking into independent jobs; each layer serves the one above, uses the one below, and talks only to its peer on the other machine.
- OSI's seven layers are a teaching model and a source of names ("Layer 2", "Layer 7"); the four-layer TCP/IP model is what's actually deployed.
- Encapsulation means prepending headers: 37 bytes of HTTP + 20 TCP + 20 IPv4 + 14 Ethernet + 4 FCS = a 95-byte frame.
- Every header says how long it is and what's inside it (EtherType
0800, Protocol6, port 80). That chain is how a receiver decapsulates. - Switches read only Ethernet headers; routers read IP, decrement the TTL, and replace the Ethernet header at every hop; only the end hosts open TCP and the application data.
- With a 1500-byte MTU, a full TCP segment carries 1460 bytes of payload, about 95% efficiency on the wire.
Every header in that hex dump will get its own article, starting with the outermost envelope. Next up: Ethernet, the frame that actually crosses the wire.