Skip to content

The OSI model and the TCP/IP model, layer by layer

All seven OSI layers and the four TCP/IP layers side by side, how they map onto each other, and how encapsulation turns data into a frame on the wire.

9 min read

The Open Systems Interconnection (OSI) model is a seven-layer reference framework for how devices communicate across a network. The TCP/IP model describes what the internet runs, in four layers instead of seven, and the two map onto each other closely enough to use side by side. Both name which job is done where, between an application and a wire.

Every tool that touches a network reads one layer of it and is blind to the rest. A switch reads hardware addresses and cannot see a web request. A firewall rule written for ports cannot see which page was asked for. Knowing which layer a tool reads tells you what its output can and cannot show.

What a layered model is for

Sending a file across the world is five separate problems at once: turning bits into a signal, getting a frame to the machine at the other end of the cable, choosing a route across networks that have never met, keeping the pieces in order, and agreeing what the bytes mean on arrival.

A layered model splits those into separate jobs with fixed boundaries between them. Each layer does one thing, uses the layer below it, and does not care how that layer works. So the same browser works over Wi-Fi and over Ethernet.

The OSI model in one table

The OSI model is a conceptual reference and nothing on your machine is "the session layer", so its value is shared vocabulary. Its seven layers, top to bottom:

LayerNameResponsibilityExamples
7ApplicationNetwork services used directly by applicationsHTTP, DNS, FTP, SMTP, SSH
6PresentationFormatting, encoding, encryption, compressionTLS, JSON, XML, character encodings
5SessionEstablishing, managing and ending sessionsSession setup and teardown
4TransportEnd-to-end delivery between applicationsTCP, UDP
3NetworkLogical addressing and routing between networksIP, ICMP, routers
2Data linkNode-to-node delivery on one link, framing, error detectionEthernet, Wi-Fi (802.11), MAC addresses, switches
1PhysicalTransmission of raw bits over a mediumCables, fibre, radio, hubs

Two mnemonics fix the order:

  • Layer 7 down to layer 1: All People Seem To Need Data Processing.
  • Layer 1 up to layer 7: Please Do Not Throw Sausage Pizza Away.

Layer 1: the physical layer

The physical layer transmits raw bits over a medium. It defines voltages, light pulses, radio signalling, connector shapes and pin assignments, and it does not know what any bit means.

Cables, fibre, radio and hubs live here. A hub is a layer 1 device in the strictest sense: it repeats an incoming signal to every other port without reading it, which made classic Ethernet a star that behaved as a bus.

The data link layer delivers data between two devices on the same link. It wraps data into frames, addresses them with media access control (MAC) addresses, and detects frames that arrived corrupted.

Ethernet and Wi-Fi are layer 2. So is a switch, which reads the destination MAC address of a frame and sends it out one port. Its reach is limited: a MAC address identifies an interface on one local link and is rewritten at every router, so nothing at layer 2 can address a machine on another network.

A packet capture shows where the two addressing schemes meet.

bash
termshark -r arp-only.pcapng
output
termshark showing two ARP frames. The first, sent to Broadcast, asks who has 192.0.2.53; the second answers that 192.0.2.53 is at 00:00:5e:00:53:02. The detail pane for the reply lists Frame, Ethernet II and Address Resolution Protocol (reply) expanded to show hardware type Ethernet and protocol type IPv4, with no IP layer between them
A layer 3 address going in and a layer 2 address coming back. Note what is missing from the detail pane

A host needed to send something to 192.0.2.53 and did not know which interface that was, so it asked the whole link: Who has 192.0.2.53?, addressed to every device on the segment at once. The owner answered with its own hardware address, 192.0.2.53 is at 00:00:5e:00:53:02, and the sender can now build a frame.

The detail pane lists Frame, then Ethernet II, then Address Resolution Protocol, with no Internet Protocol between them. The Address Resolution Protocol (ARP) is carried directly by Ethernet instead of inside IP. That has to be so: ARP finds the layer 2 address that an IP packet has not been given yet, so it cannot depend on IP. Its own fields, Hardware type: Ethernet and Protocol type: IPv4, name the two layers it translates between.

The lab uses ranges reserved for documentation, so no address in the picture belongs to anyone: 192.0.2.0/24 from RFC 5737, and hardware addresses from the RFC 7042 documentation block.

Layer 3: the network layer

The network layer addresses devices logically and routes packets between separate networks. It is where an IP address lives, and where a router decides which way to send a packet next.

Layer 2 gets a frame across one link; layer 3 gets a packet across a chain of links, without either end knowing what they are made of. The Internet Control Message Protocol (ICMP), which ping uses, is also layer 3.

Layer 4: the transport layer

The transport layer provides end-to-end communication between applications rather than between machines. It adds port numbers, which is what lets one machine run a web server and a mail server at once, and it can add reliability on top of a network layer that offers none.

Two protocols dominate:

  • TCP (Transmission Control Protocol) numbers what it sends, acknowledges what it receives, retransmits what is lost and delivers the result in order.
  • UDP (User Datagram Protocol) sends datagrams and stops there. No ordering, no retransmission, far less delay.

Neither is better. TCP is right for a file that must arrive intact; UDP is right for a voice call, where a packet that arrives late is worse than one that never arrives.

Layers 5, 6 and 7: session, presentation and application

Real protocol stacks do not draw firm lines between layers 5, 6 and 7, so they share a section here, and the TCP/IP model does not separate them either.

The session layer establishes, manages and ends conversations between applications. The presentation layer translates, formats, encrypts and compresses so that two different systems agree what the bytes mean; Transport Layer Security (TLS) and character encoding are usually put here. The application layer is the one applications speak directly: HTTP, DNS, the File Transfer Protocol (FTP), the Simple Mail Transfer Protocol (SMTP) and Secure Shell (SSH).

The TCP/IP model, and how it maps onto OSI

The TCP/IP model is the protocol suite the internet runs on, in four layers instead of seven.

LayerNameResponsibilityExamplesOSI equivalent
4ApplicationServices applications useHTTP, DNS, SSH, FTP7, 6, 5
3TransportEnd-to-end delivery, ports, reliabilityTCP, UDP4
2InternetLogical addressing and routingIP, ICMP3
1Network accessDelivery across one physical networkEthernet, Wi-Fi, ARP2, 1

The Address Resolution Protocol (ARP) sits at the boundary between addressing and delivery, so the four-layer table can list it under network access while the seven-layer table above puts it at no layer at all.

The two models do not disagree about how networks work. OSI was designed as a reference model, and TCP/IP was built first and described afterwards, so it merges the layers nobody implemented separately.

Use OSI's numbers when talking to people, because "a layer 3 problem" and "a layer 7 rule" are common shorthand. Use TCP/IP's structure when reasoning about what is running.

Encapsulation: how data becomes a frame

Encapsulation is the process of adding each layer's own control information to the data as it travels down the stack on the sending device. Every layer wraps what it received from the layer above and hands the result down.

LayerWhat the unit is called hereWhat this layer adds
ApplicationDataThe application's own headers, such as an HTTP request line
TransportSegment (TCP) or datagram (UDP)Source and destination port numbers
NetworkPacketSource and destination IP addresses, and a time to live
Data linkFrameSource and destination MAC addresses, and a frame check sequence
PhysicalBitsNothing. It signals what it was handed

"Packet" and "frame" are not synonyms: a frame is a packet with layer 2 addressing wrapped around it, and the frame is discarded and rebuilt at every hop while the packet crosses the whole path.

Every layer, on one real packet

This is one packet from an ordinary HTTPS request, opened in termshark:

bash
curl -k --http1.1 --resolve example.com:443:192.0.2.53 -o /dev/null https://example.com/
termshark -r tls-handshake.pcapng
output
termshark showing a TLS Client Hello with the server name example.com. The detail pane lists five collapsed layers in order: Frame 11, Ethernet II with source and destination hardware addresses, Internet Protocol Version 4 from 192.0.2.10 to 192.0.2.53, Transmission Control Protocol with destination port 443, and Transport Layer Security
The detail pane, collapsed, is the encapsulation: frame, then Ethernet, then IP, then TCP, then TLS, outermost first

The detail pane is the encapsulation, drawn by a dissector. Reading down it: the frame as it arrived, the Ethernet header carrying hardware addresses at layer 2, the IP header carrying addresses at layer 3, the TCP header carrying port 443 at layer 4, and the Transport Layer Security (TLS) record above it. Reading up it gives the order the sending machine built them in.

The hex pane at the bottom is the same packet as bytes, and the highlighted run at offset zero is the Ethernet header the row above it describes.

Same lab and same documentation ranges as above: 192.0.2.0/24 from RFC 5737, and hardware addresses from the RFC 7042 documentation block. The exchange and the protocols are real, and the machine was a pair of virtual interfaces that existed for four seconds.

Decapsulation: the same trip in reverse

Decapsulation is the reverse, on the receiving device. Each layer removes the header its counterpart added, acts on it, and hands what is left to the layer above.

The data link layer checks the frame arrived intact and strips the MAC addresses. The network layer confirms the destination address is its own and strips the IP header. The transport layer reads the port number and hands the payload to whichever application is listening on it.

EncapsulationDecapsulation
Happens atThe senderThe receiver
Direction through the stackDownwards, application to physicalUpwards, physical to application
HeadersAddedRead, then removed

Seeing the top of the stack

Everything below the application layer is invisible without packet capture, which needs privileges an ordinary command does not have. The top of the stack needs none.

bash
curl -sI https://example.com | bat -l http --style=plain --color=always
output
curl -sI https://example.com returning HTTP/2 200 with headers: date, content-type text/html, server cloudflare, last-modified, allow, accept-ranges, age 13746, cf-cache-status HIT and a cf-ray identifier
Layer 7, and only layer 7. Everything that carried it has already been stripped off by the time curl prints this

curl prints only layer 7. Underneath are a TLS session at layer 6, a TCP connection to port 443 at layer 4, packets carrying IP addresses at layer 3, and a frame per hop at layer 2, each built, sent and stripped to produce those ten lines.

The HTTP/2 on the first line is an application-layer detail, and nothing below layer 4 knew which version was in use.

Using the models to locate a fault

Neither model describes a real program, and treating either as one causes more confusion than it resolves. They are good for locating a problem.

A machine that cannot reach anything, including its own gateway, is failing at layer 1 or 2. A machine that reaches its gateway and nothing beyond is failing at layer 3. A machine that reaches the server but is refused on one port is at layer 4. A machine that connects and gets an error page is at layer 7.

Asking these four questions in order narrows a fault to one layer, because each answer rules out the layers below it.

The first question has the most physical answers, and network cabling covers them: what the cable is made of, what connector is on the end of it, and how fast the link can run.