Sub topic 11
OSI versus the TCP/IP model
This chapter assumes you know the seven OSI layers, the idea of encapsulation, and the common protocols at each layer (Ethernet, IP, TCP, UDP, HTTP, TLS). It compares OSI with the TCP/IP model, shows where the two disagree, and explains why engineers use both vocabularies.
Two models with different origins
The OSI reference model and the TCP/IP model describe the same job: moving data between hosts through layers. They were built for different reasons.
- OSI was published by ISO as a reference model. It was designed first as a conceptual framework, and a family of OSI protocols was meant to be built to match it. Those protocols (such as X.400 mail and CLNP) lost out in the market.
- TCP/IP grew out of ARPANET research. The protocols were written and deployed first. The model is a description of what those protocols already did, found in documents such as RFC 1122.
The result is that TCP/IP is a model of protocols that run the Internet. OSI is a vocabulary that outlived its own protocol suite. Almost no real network runs OSI protocols end to end, yet "Layer 2", "Layer 3", "Layer 4" and "Layer 7" are the standard shorthand in textbooks, job interviews, product datasheets and incident reports.
The layers side by side
The TCP/IP model is usually drawn with four layers. Some textbooks draw five by splitting the bottom layer in two. The table uses the four-layer form from RFC 1122.
| OSI layer | TCP/IP layer (4-layer) | Typical protocols |
|---|---|---|
| 7 Application | Application | HTTP, DNS, SMTP, SSH |
| 6 Presentation | Application | TLS record encoding, JSON, UTF-8 |
| 5 Session | Application | TLS sessions, RPC, NetBIOS sessions |
| 4 Transport | Transport | TCP, UDP |
| 3 Network | Internet | IPv4, IPv6, ICMP |
| 2 Data Link | Link (Network Access) | Ethernet, Wi-Fi (802.11), ARP |
| 1 Physical | Link (Network Access) | Copper, fibre, radio signalling |
The mapping is approximate. It is a rule of thumb, not a precise equivalence, and the sections below cover where it breaks.
What the TCP/IP model merges
Layers 5 to 7 become one Application layer
In the TCP/IP model, the application itself is responsible for session handling, data encoding and encryption. There is no separate layer for them. This matches how software works. A web browser does not hand its data to a "presentation service" in the operating system. It calls a TLS library, serializes JSON itself, and tracks its own cookies and sessions.
OSI separates these concerns to make the model teachable. In practice, a single protocol often touches all three:
- HTTP/1.1 defines requests (Layer 7), uses
Content-Encoding: gzipfor compression (Layer 6 in OSI terms), and uses cookies to keep a session (Layer 5 in OSI terms). - TLS encrypts data (Layer 6 work) but also negotiates and resumes sessions (Layer 5 work) and is invoked by the application over a TCP socket.
This is why Layers 5 and 6 are the hardest to assign to a protocol. Engineers rarely argue about which of the three a protocol belongs to. They say "application layer" and move on.
Layers 1 and 2 become one Link layer
TCP/IP treats everything below IP as "whatever carries IP packets across one network segment". IP does not care whether that is Ethernet over copper, Wi-Fi, a cellular link or a tunnel. OSI splits this into the physical signalling (Layer 1) and the framing and local addressing (Layer 2). The split is still useful when you troubleshoot: a bad cable is a Layer 1 problem, and a duplicate MAC address or a VLAN mismatch is a Layer 2 problem.
What the two models share
Both models agree on the core structure.
- Each layer provides a service to the layer above and uses the service of the layer below.
- Data is encapsulated on the way down and decapsulated on the way up.
- The network layer (OSI 3 or TCP/IP Internet) routes between networks.
- The transport layer provides end-to-end communication between processes, identified by ports.
A packet on the wire looks the same whichever model you use to describe it. Only the naming differs.
Where the models genuinely differ
Strict layering versus a pragmatic stack
OSI describes layers that are cleanly separated, each talking only to its neighbours. TCP/IP is looser. Real protocols reach across layers:
- TCP computes its checksum over a pseudo-header that includes the IP source and destination addresses. This ties Layer 4 to Layer 3 data.
- NAT devices rewrite IP addresses (Layer 3) and also port numbers (Layer 4), and some application protocols such as FTP in active mode embed IP addresses inside the payload, so a NAT must look into Layer 7.
- ARP carries IP addresses but runs directly over Ethernet. It has no IP header, so it does not fit cleanly into Layer 2 or Layer 3. It is usually called a Layer 2 or "Layer 2.5" protocol.
- MPLS inserts a label between the Layer 2 header and the IP header. It is also called "Layer 2.5".
Those cases are the reason to treat the layer numbers as labels for conversation, not as laws.
Service definitions and connection models
OSI defined formal services at each layer and described both connection-oriented and connectionless variants at the network layer. TCP/IP made a different design choice. IP is connectionless and gives no delivery guarantee. Reliability is pushed to the endpoints, in TCP. This is the "end-to-end principle": keep the network core simple, and put intelligence in the hosts.
Where each model stops
OSI starts at the wire. The TCP/IP model in RFC 1122 says little about the physical medium. It only requires that the link layer can carry IP datagrams. The layers below IP are described elsewhere (IEEE 802 standards).
Seeing the layers in running code
Both models can be seen in real code. The Ethernet header below belongs to OSI Layer 2, or the TCP/IP Link layer. The EtherType field tells the receiver which protocol comes next, so it is the hand-off point to the layer above.
import struct
def mac(b):
return ":".join(f"{x:02x}" for x in b)
# 14-byte Ethernet II header: destination MAC, source MAC, EtherType
header = bytes.fromhex("ffffffffffff" "001122334455" "0806")
dst, src, ethertype = struct.unpack("!6s6sH", header)
print(mac(dst), mac(src), hex(ethertype))
# ff:ff:ff:ff:ff:ff 00:11:22:33:44:55 0x806The value 0x0806 identifies ARP. A value of 0x0800 would identify IPv4, and 0x86dd would identify IPv6. The Link layer does not need to understand the payload. It only needs to deliver it to the right protocol.
The next example crosses most of the stack in a few lines. The operating system handles TCP (Layer 4 or Transport), IP (Layer 3 or Internet) and the link below. The program writes only the HTTP text (Layer 7 or Application).
import socket
request = b"GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n"
with socket.create_connection(("example.com", 80), timeout=5) as s:
s.sendall(request)
data = s.recv(200)
print(data.decode("ascii", errors="replace").splitlines()[0])This split matches the TCP/IP model closely. Application code sees a socket, the kernel owns the Transport and Internet layers, and the network driver and hardware own the Link layer. The socket API is the boundary between "your program" and "the stack". OSI's Layers 5 and 6 do not appear anywhere in this boundary, which is a good illustration of why TCP/IP folds them into the application.
Common mistakes
- Treating the mapping as exact. TLS, ARP, MPLS and VPN protocols such as WireGuard or IPsec do not sit neatly in one layer. Say "TLS is usually described as Layer 6, but it runs on top of TCP and is invoked by the application" instead of insisting on one answer.
- Believing the Internet runs OSI protocols. It runs TCP/IP protocols. OSI is the reference vocabulary.
- Mixing numbering schemes. "Layer 3" always means OSI numbering. TCP/IP layers are normally named, not numbered. If someone says "Layer 4 load balancer", they mean it forwards based on IP and port (OSI 4). If they say "Layer 7 load balancer", they mean it reads the HTTP request.
- Forgetting the five-layer variant. Some books split the Link layer and call the result a "five-layer TCP/IP model". Check which one a source uses before comparing counts.
- Assuming the layer tells you the device. A "Layer 3 switch" forwards at both Layer 2 and Layer 3. A firewall may inspect Layers 3 to 7. Device names describe the highest layer they act on, not the only one.
Choosing which model to use
Use OSI numbering when you need to be precise about location in conversation or documentation: "the fault is at Layer 2", "this appliance works at Layer 7". Use the TCP/IP model when you reason about how real protocols and operating systems are structured: sockets, kernel network stack, and protocol RFCs.
The troubleshooting sequence below combines both. Each command tests one layer, using OSI numbers as the labels and TCP/IP tools as the probes.
ip link show # Layers 1-2: is the interface up?
ip addr show # Layer 3: does it have an IP address?
ping -c 4 example.com # Layer 3: can packets reach the host (ICMP)?
nc -zv example.com 443 # Layer 4: does a TCP connection to port 443 succeed?
curl -I https://example.com # Layers 6-7: does TLS and HTTP respond?If ping succeeds but nc fails, the network path works, so look at Layer 4 and above: a firewall rule, a closed port, or a service that is not listening.
Summary of the differences
| Question | OSI | TCP/IP |
|---|---|---|
| Number of layers | 7 | 4 (or 5 in some books) |
| Origin | Reference model from ISO | Description of protocols already in use |
| Session and presentation | Separate layers | Part of the application |
| Physical and data link | Separate layers | One Link layer |
| Layering | Strict and formal | Flexible, with cross-layer shortcuts |
| Main use today | Shared vocabulary and teaching | Describing the real protocol stack |
Exercise
A colleague says "our Layer 7 load balancer routes /api requests to one pool and /images requests to another." Which OSI layer information does it need to read, which TCP/IP layer does that correspond to, and why could a Layer 4 load balancer not do the same routing?
Show answer
Routing by URL path needs the HTTP request, which is OSI Layer 7 and the TCP/IP Application layer. A Layer 4 load balancer sees only IP addresses and TCP or UDP ports, which are the same for /api and /images on one site, so it cannot tell the requests apart. It can only balance whole connections, not individual requests. (With HTTPS it would also need to terminate TLS to read the path.)
Exercise
Using the table in this chapter, name the TCP/IP layer for each of these: (a) an Ethernet frame, (b) an IPv6 packet, (c) a UDP datagram, (d) a TLS-encrypted JSON body. Then explain why placing TLS in a single OSI layer is awkward.
Show answer
(a) Link, (b) Internet, (c) Transport, (d) Application. TLS is awkward to place because it encrypts and encodes data (Layer 6 work), negotiates and resumes sessions (Layer 5 work), runs over TCP, and is called by the application. The TCP/IP model avoids the argument by putting it all in the Application layer.
Exercise
You run the five commands from the troubleshooting sequence. ip addr show lists a valid address and ping -c 4 example.com gets replies, but nc -zv example.com 443 times out. Which layer do you investigate next, and give two possible causes.
Show answer
Investigate Layer 4 and above. Possible causes: a firewall (local, network or on the server) blocking TCP port 443, or no service listening on port 443 on the server. Layers 1 to 3 are working, because the interface is up, the host has an address, and ICMP reaches the target.