Sub topic 2
The OSI Reference Model: Seven Layers
This chapter assumes you know what a client and a server are, you have used a network, and you can run commands in a terminal. It describes the OSI reference model: what each of its seven layers does, which protocols you will meet at each one, and which mistakes people commonly make when using it.
What the OSI model is
The Open Systems Interconnection (OSI) model is a reference model published by the International Organization for Standardization (ISO). It splits network communication into seven layers. Each layer has one job and offers a service to the layer above it. It uses the service of the layer below it.
The model is a way of describing and discussing networks. It is not a protocol and it is not software. No single product "implements OSI" as a stack in the way the Linux kernel implements TCP/IP. The internet grew up on TCP/IP, and OSI's own protocol suite was largely abandoned. The OSI vocabulary survived anyway, because it gives engineers shared names for the parts of a network conversation. When someone says "that is a layer 2 problem" or "we need a layer 7 load balancer", they are using OSI numbering.
Two terms appear throughout this chapter:
- A protocol is a set of rules that two peers follow to talk to each other at the same layer.
- A service is what a layer offers to the layer directly above it, such as "deliver this block of data to the other side".
A layer on one machine talks logically to the same layer on the other machine through its protocol. In practice, the data travels down the layers on the sender, across the wire, and up the layers on the receiver.
The seven layers at a glance
Layers are numbered from the bottom. Layer 1 is closest to the physical medium and layer 7 is closest to the application.
| Number | Name | Main job | Example protocols or technologies |
|---|---|---|---|
| 7 | Application | Network services used directly by applications | HTTP, DNS, SMTP, FTP, SSH |
| 6 | Presentation | Data format, encoding, compression, encryption | TLS (often placed here), character encodings, JPEG, MIME |
| 5 | Session | Starting, managing, and ending dialogues | RPC session handling, NetBIOS sessions |
| 4 | Transport | End-to-end delivery between processes | TCP, UDP |
| 3 | Network | Addressing and routing between networks | IP, ICMP, routing protocols |
| 2 | Data link | Delivery between neighbouring devices on one link | Ethernet, Wi-Fi (802.11), ARP |
| 1 | Physical | Moving raw bits over a medium | Copper cable, fibre, radio, voltage levels |
A common memory aid, from layer 7 down to layer 1, is "All People Seem To Need Data Processing". From layer 1 up, people use "Please Do Not Throw Sausage Pizza Away".
Layer by layer
Layer 1: Physical
The physical layer moves individual bits. It defines the things you can touch or measure: connector shapes, cable types, voltages, light pulses, radio frequencies, and signal timing. It has no idea what the bits mean.
Typical faults at this layer are a loose or damaged cable, a dead transceiver, a duplex or speed mismatch, and radio interference on Wi-Fi. Hubs and repeaters work here, because they only regenerate signals.
Layer 2: Data link
The data link layer delivers data between two devices that share the same link, such as two machines on one Ethernet segment or one Wi-Fi network. It does three main things:
- It groups bits into frames.
- It identifies devices on the link with hardware addresses (MAC addresses).
- It detects transmission errors, usually with a frame check sequence.
Ethernet switches work at layer 2. They look at the destination MAC address of each frame and forward it out of the right port. A layer 2 address only matters on the local link. When a packet crosses a router, the frame is rebuilt with new MAC addresses.
The Address Resolution Protocol (ARP) finds the MAC address that goes with an IPv4 address on the local link. It does not fit cleanly into one layer, and a later chapter returns to that.
Layer 3: Network
The network layer moves packets between different networks. It provides logical addresses that can be organised into hierarchies, so that routers can forward traffic across the whole internet without knowing every device.
- IP (IPv4 and IPv6) provides addressing and forwarding.
- Routers choose the next hop for each packet by consulting a routing table.
- ICMP carries error and diagnostic messages, such as "destination unreachable". The
pingandtraceroutetools use it.
This layer is best-effort in IP. It does not promise that a packet arrives, arrives once, or arrives in order.
Layer 4: Transport
The transport layer connects processes, not just machines. Layer 3 gets a packet to the right host. Layer 4 gets the data to the right program on that host, using port numbers.
- TCP provides a reliable, ordered byte stream. It sets up a connection, numbers the data, retransmits what is lost, and slows down when the network is congested.
- UDP provides a thin, connectionless service. It adds ports and an optional checksum and nothing else. The application deals with loss and ordering itself, if it cares.
Firewalls that filter by port number work at this layer. So do load balancers that forward TCP connections without reading their contents ("layer 4 load balancers").
Layer 5: Session
The session layer, in the OSI description, manages a dialogue between two applications: opening it, keeping track of it, and closing it. It may also provide checkpoints so a long transfer can resume after a failure.
This layer is the one that real-world protocols fit worst. Most modern protocols handle sessions inside the application or the transport protocol. Examples that are sometimes called "session layer" include remote procedure call (RPC) session handling and NetBIOS sessions. Web cookies and login sessions are also called "sessions", but they are application-level state and not OSI layer 5 in the strict sense.
Layer 6: Presentation
The presentation layer deals with how data is represented, so that two systems with different internal formats can still understand each other. Typical tasks are:
- Character encoding, such as converting between UTF-8 and other encodings.
- Serialisation of data structures into a transmittable form.
- Compression.
- Encryption and decryption.
TLS is usually described as sitting here because it encrypts application data, though it also performs a handshake and manages a secure session, which is why it is hard to place. A later chapter covers this.
Layer 7: Application
The application layer holds the protocols that applications use to provide network services. This is not the same as the application program. A web browser is a program. HTTP is the layer 7 protocol it speaks.
Examples are HTTP for the web, DNS for name lookups, SMTP for sending email, and SSH for remote login. "Layer 7" in everyday speech often means "anything that understands the content of the request", such as a layer 7 load balancer that routes by URL path.
Services between layers
Each layer adds its own control information to the data it receives from above. That control information is called a header, and at layer 2 there can also be a trailer. This wrapping process is encapsulation, covered fully in the next chapters. Here the point is that a layer only needs to understand its own header. A router reads the layer 3 header and does not need to understand HTTP. A web server reads the HTTP request and does not need to know which cable carried it.
This separation gives the model its practical value:
- You can replace one layer without changing the others. IP runs over Ethernet, Wi-Fi, fibre, and mobile networks.
- You can troubleshoot in a structured way, from the bottom up or the top down.
- Vendors and standards bodies can work on layers independently.
Common errors when using the model
Treating the model as a description of real stacks
Real systems rarely have seven separate pieces of software. Applications on the internet typically handle the work of layers 5 to 7 together in one program, possibly with a TLS library. Do not look for a "session layer module" in your operating system.
Placing devices in one layer only
Devices are often labelled by one layer, but many work across several:
- A switch is a layer 2 device. A "layer 3 switch" also routes.
- A router works at layer 3 but must also handle layer 2 framing on each interface.
- A firewall may inspect layers 3, 4, and 7.
- A load balancer can be layer 4 or layer 7, depending on what it reads.
Assuming strict layering
Layers are not perfectly isolated in practice. TCP computes its checksum using fields from the IP header. Network address translation (NAT) rewrites layer 3 addresses and layer 4 ports. Some protocols break the layer boundaries on purpose for performance. Use the model to reason and not as a law.
Mixing up "application" meanings
The OSI application layer is a set of protocols. It is not your program and it is not the user interface.
Counting layers wrongly in conversation
People say "layer 2", "layer 3", "layer 4", and "layer 7" far more often than "layer 5" or "layer 6". Be careful when someone says "layer 8" as a joke. It refers to the user or to organisational politics, and it is not part of the model.
Looking at the layers from a terminal
You can see several layers with ordinary tools. The commands below are safe, read-only checks on a Linux host.
ip link show
ip -4 addr show
ip route show
ss -tnip link showlists network interfaces and their MAC addresses (layer 2).ip -4 addr showlists IPv4 addresses (layer 3).ip route showdisplays the routing table used by layer 3 forwarding.ss -tnlists TCP connections with their local and remote ports (layer 4).
Reading the output from the bottom to the top of the model is a useful habit. If the link is down, nothing above it can work.
Exercise
A router receives an Ethernet frame, removes the Ethernet header, reads the destination IP address, and forwards the packet out of another interface in a new frame. Which OSI layers did the router use to decide where to send the packet, and which layer's addresses changed on the way?
Show answer
The router used layer 1 to receive and send the bits and layer 2 to receive and build frames, but it made its forwarding decision at layer 3 by reading the destination IP address. The layer 2 addresses (source and destination MAC addresses) changed, because they only have meaning on one link. The layer 3 IP addresses stayed the same, unless NAT rewrote them.
Exercise
A load balancer sends requests to different servers based on the URL path in an HTTP request. At which OSI layer does it operate, and why can a layer 4 load balancer not do the same?
Show answer
It operates at layer 7, because it must read the HTTP request to see the URL path. A layer 4 load balancer sees only IP addresses and TCP or UDP ports, so it can choose a server by connection but cannot see the content of the request.
Exercise
Why is the session layer (layer 5) hard to find in real protocol stacks?
Show answer
TCP/IP-based protocols handle session-like functions either in the transport protocol (for example TCP's connection setup and teardown) or inside the application protocol itself. There is no separate layer of software for it, so the OSI session layer is mostly a conceptual category.