Part 1 · Foundations
OSI and TCP/IP models: network layers and encapsulation
Course contents
In the first lesson you saw that loading one web page takes several protocols: Ethernet or Wi-Fi, IP, TCP, TLS and HTTP. This lesson shows how they fit together. There are only two ideas: protocols are stacked in layers, and each layer wraps the data of the layer above it, like an envelope inside an envelope (encapsulation). At the end you will capture one real packet and point at every layer inside it.
Why layers?
Think of sending a gift abroad. You write a card and put it in a box (that’s your part). You write the recipient’s address on the box. The post office puts your box in a bag with others going to the same city. A truck, then a plane, then another truck move the bag.
Each step only reads its own label. The truck driver never opens your box; you don’t care whether it flew or went by sea. If the post office switches from trucks to trains tomorrow, you write your card exactly as before.
Networks work the same way, and so does software. Your code calls an HTTP library; the library opens a connection through the operating system; the operating system hands packets to the network card. Each layer uses the layer below through a simple interface and doesn’t care how that layer does its job. That is why your web browser works the same over Wi-Fi, a cable or a phone network, and why HTTP could move from version 1.1 to 2 to 3 without anyone replacing their Wi-Fi.
The four layers the Internet uses
The Internet is built on the TCP/IP model (RFC 1122), which has four layers. From the bottom up:
| Layer | Its job | Protocols | Address it uses | Its unit of data is called |
|---|---|---|---|---|
| Link | Deliver data to the next machine on the same local network | Ethernet, Wi-Fi | MAC address (the network card’s hardware address) | frame |
| Internet | Deliver a packet across many networks to the right machine | IP (and ICMP, which ping uses) | IP address | packet |
| Transport | Deliver data to the right program, complete and in order if needed | TCP, UDP | port | segment (TCP), datagram (UDP) |
| Application | The conversation the program actually wants to have | HTTP, DNS, SSH, SMTP, and TLS for encryption | names, URLs, email addresses | message |
Read the “address” column from the bottom up and you see the idea: a MAC address gets data across one cable or Wi-Fi hop, an IP address gets it to the right machine anywhere in the world, a port gets it to the right program on that machine, and the application decides what the data means. MAC addresses are new; you’ll meet them properly in the next lesson.
In everyday speech people say “packet” for any of these units. When precision matters, the names in the last column tell you which layer someone means.
The OSI model: the numbers everyone uses
You will often hear layers called by number: “an L2 switch”, “a layer 3 problem”, “an L7 load balancer”. Those numbers come from an older model, OSI (Open Systems Interconnection, ISO/IEC 7498-1), which has seven layers instead of four. OSI’s own protocols lost to TCP/IP in the 1980s, but its numbering stuck, so you need to know how the two line up:
| OSI # | OSI name | In TCP/IP | What it means in practice |
|---|---|---|---|
| 7 | Application | Application | HTTP, DNS, SSH: what the program says |
| 6 | Presentation | Application | Data formats and encryption: UTF-8, JSON, TLS (roughly) |
| 5 | Session | Application | Keeping a dialog going; in practice handled by the application |
| 4 | Transport | Transport | TCP, UDP, ports |
| 3 | Network | Internet | IP, routing |
| 2 | Data link | Link | Ethernet, Wi-Fi, MAC addresses, switches |
| 1 | Physical | Link | The actual signals: voltages on copper, light in fiber, radio waves |
So “L2” means Ethernet and MAC addresses, “L3” means IP, “L4” means TCP or UDP and ports, and “L7” means the application protocol, usually HTTP. Layers 5 and 6 are rarely mentioned on their own. A mnemonic for the seven, from the bottom up: Please Do Not Throw Sausage Pizza Away.
Encapsulation: envelopes inside envelopes
When a program sends data, it goes down the layers on the sending machine. Each layer adds its own header in front of what it got from the layer above. (Ethernet also adds a short trailer at the end, a checksum to detect damaged frames.) On the receiving machine, the data goes up: each layer reads and removes its own header and hands the rest to the layer above. Adding headers is encapsulation; removing them is decapsulation.
A small HTTP request sent over Ethernet looks like this on the wire:
Ethernet header 14 bytes from which MAC, to which MAC, "inside is IPv4"
IPv4 header 20 bytes from which IP, to which IP, "inside is TCP"
TCP header 20-60 bytes from which port, to which port, ordering info
HTTP request ... GET / HTTP/1.1 ...
Ethernet trailer 4 bytes checksum
Two things to notice:
- Each header says what is inside it, so the receiver knows which layer to hand the payload to next. The Ethernet header carries an EtherType (
0x0800means “IPv4 inside”). The IP header has a Protocol field (6 means TCP, 17 means UDP). The TCP or UDP header has the destination port, which picks the program. - Every link has a maximum frame size. The largest packet a link can carry is its MTU (maximum transmission unit). On Ethernet it is usually 1,500 bytes, which is why packets are at most about 1,500 bytes, as you read in lesson 1. Bigger data is simply cut into more packets.
To a programmer, each header is just a struct with fixed fields, and the payload is an array of bytes that the layer doesn’t look inside.
Who handles which layer?
| Component | Layers | What it does |
|---|---|---|
| Your program | Application | Speaks HTTP, DNS and so on, through the socket API: connect, send, recv |
| The operating system (kernel) | Transport and Internet, part of Link | TCP and UDP, IP, routing, finding MAC addresses |
| Driver and network card | Link | Builds frames and turns them into signals |
| Switch | Link (L2) | Moves frames between the machines of one local network, by MAC address |
| Router | Internet (L3) | Moves packets between networks, by IP address |
| Firewall, load balancer | L4 or L7 | Depends on the product; see the firewalls and load balancers lesson |
The boundary between your program and the kernel is the socket API. When you call send(), your data is copied into the kernel and the call returns. Cutting it into packets, adding the headers and resending anything that gets lost is the kernel’s job, not yours.