The OSI Model
Networking is too complicated to design as one giant program. Instead it is split into layers. Each layer solves one problem, uses the services of the layer below, and offers services to the layer above. The best-known reference for this is the OSI model (Open Systems Interconnection), published by ISO in 1984. It has seven layers.
The seven layers
| # | Layer | Job | Unit of data | Examples |
|---|---|---|---|---|
| 7 | Application | network services for applications | data / message | HTTP, DNS, SMTP, SSH |
| 6 | Presentation | data format, encoding, encryption | data | UTF-8, JPEG, TLS (partly) |
| 5 | Session | opening, managing and closing dialogues | data | RPC sessions, NetBIOS |
| 4 | Transport | end-to-end delivery between programs (ports), reliability | segment (TCP) / datagram (UDP) | TCP, UDP, QUIC |
| 3 | Network | addressing and routing across networks | packet | IPv4, IPv6, ICMP |
| 2 | Data link | delivery to the next device on the same link | frame | Ethernet, Wi-Fi (802.11), ARP |
| 1 | Physical | sending raw bits as signals | bit | cables, radio, voltages, light pulses |
A classic mnemonic from layer 1 upward: Please Do Not Throw Sausage Pizza Away.
OSI = [
(1, "Physical", "bits"),
(2, "Data link", "frames"),
(3, "Network", "packets"),
(4, "Transport", "segments"),
(5, "Session", "data"),
(6, "Presentation", "data"),
(7, "Application", "data"),
]
mnemonic = "Please Do Not Throw Sausage Pizza Away".split()
for (num, name, pdu), word in zip(OSI, mnemonic):
assert name[0] == word[0]
print(f"L{num} {name:<13} {pdu:<9} ({word})")L1 Physical bits (Please)
L2 Data link frames (Do)
L3 Network packets (Not)
L4 Transport segments (Throw)
L5 Session data (Sausage)
L6 Presentation data (Pizza)
L7 Application data (Away)
Why layers?
- Independence: you can replace Wi-Fi with Ethernet (layer 2) without changing your web browser (layer 7).
- Interoperability: vendors only need to agree on the interface between layers.
- Troubleshooting: you can test one layer at a time ("is the cable plugged in?" before "is DNS working?").
Each layer on one host talks logically to the same layer on the other host (its peer), but physically the data goes down the stack, across the wire and up the other side.
Where things live
When diagnosing a problem, it helps to classify the symptom by layer:
symptoms = {
"cable unplugged, link light off": 1,
"Wi-Fi associated but frames have CRC errors": 2,
"two machines with the same IP address": 3,
"no route to the destination network": 3,
"connection refused on port 5432": 4,
"TLS certificate expired": 6,
"web server returns 404 Not Found": 7,
"DNS name does not resolve": 7,
}
names = {1: "Physical", 2: "Data link", 3: "Network", 4: "Transport",
5: "Session", 6: "Presentation", 7: "Application"}
for symptom, layer in sorted(symptoms.items(), key=lambda kv: kv[1]):
print(f"L{layer} {names[layer]:<13} {symptom}")L1 Physical cable unplugged, link light off
L2 Data link Wi-Fi associated but frames have CRC errors
L3 Network two machines with the same IP address
L3 Network no route to the destination network
L4 Transport connection refused on port 5432
L6 Presentation TLS certificate expired
L7 Application web server returns 404 Not Found
L7 Application DNS name does not resolve
Note: Real protocols don't fit the OSI boxes perfectly. TLS is often called layer 6 but is implemented as a library inside applications. ARP sits between layers 2 and 3. DNS is an application-layer protocol that the network layer depends on. The model is a vocabulary for thinking, not a law.
Layer addressing
Each layer that delivers data has its own kind of address. A packet in flight carries several at once:
packet = {
"L2 source MAC": "3c:22:fb:91:0a:7e",
"L2 destination MAC": "00:1a:2b:3c:4d:5e", # the next router, not the server!
"L3 source IP": "192.168.1.20",
"L3 destination IP": "93.184.215.14",
"L4 source port": 51514,
"L4 destination port": 443,
}
for field, value in packet.items():
print(f"{field:<20} {value}")L2 source MAC 3c:22:fb:91:0a:7e
L2 destination MAC 00:1a:2b:3c:4d:5e
L3 source IP 192.168.1.20
L3 destination IP 93.184.215.14
L4 source port 51514
L4 destination port 443
The MAC addresses change at every router (they only describe one hop), while the IP addresses stay the same from end to end (unless NAT rewrites them). This difference is one of the most important ideas in networking, and you will see it again and again.
OSI in practice
The OSI protocols themselves (like CLNP and X.400) lost to the Internet's TCP/IP protocols, but the OSI model and its numbering won as a vocabulary. When engineers say "a layer 2 problem", "an L4 load balancer" or "a layer 7 firewall", they mean OSI layers. The next lesson covers the TCP/IP model that the Internet actually uses.
Test yourself
At which OSI layer do routers make forwarding decisions?
- Layer 1
- Layer 2
- Layer 3
- Layer 4
What is the unit of data at the data link layer called?
- Bit
- Frame
- Packet
- Segment
Which address changes at every router hop?
- Destination IP address
- Source IP address
- Destination MAC address
- Destination port