Part 1 · Foundations

OSI and TCP/IP models: network layers and encapsulation

lesson 02 / 1915 min1 labpublished updated
Course contents
On this page
  1. Why layers?
  2. The four layers the Internet uses
  3. The OSI model: the numbers everyone uses
  4. Encapsulation: envelopes inside envelopes
  5. Who handles which layer?

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:

LayerIts jobProtocolsAddress it usesIts unit of data is called
LinkDeliver data to the next machine on the same local networkEthernet, Wi-FiMAC address (the network card’s hardware address)frame
InternetDeliver a packet across many networks to the right machineIP (and ICMP, which ping uses)IP addresspacket
TransportDeliver data to the right program, complete and in order if neededTCP, UDPportsegment (TCP), datagram (UDP)
ApplicationThe conversation the program actually wants to haveHTTP, DNS, SSH, SMTP, and TLS for encryptionnames, URLs, email addressesmessage

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:

OSITCP/IPexamples7 ApplicationdataHTTP DNS SSH6 PresentationdataTLS, encoding5 Sessiondatasessions4 TransportsegmentTCP UDP QUIC3 NetworkpacketIP ICMP2 Data LinkframeEthernet Wi-Fi1 Physicalbitscopper fiber radioApplicationTransportInternetLink
The seven OSI layers next to the four TCP/IP layers, with example protocols and the name of the data unit at each layer.
OSI #OSI nameIn TCP/IPWhat it means in practice
7ApplicationApplicationHTTP, DNS, SSH: what the program says
6PresentationApplicationData formats and encryption: UTF-8, JSON, TLS (roughly)
5SessionApplicationKeeping a dialog going; in practice handled by the application
4TransportTransportTCP, UDP, ports
3NetworkInternetIP, routing
2Data linkLinkEthernet, Wi-Fi, MAC addresses, switches
1PhysicalLinkThe 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.

Application: dataHTTP request bytesTransport: TCP segmentTCP hdrHTTP request bytesNetwork: IP packetIP hdrTCP hdrHTTP request bytesLink: Ethernet frameEth hdrIP hdrTCP hdrHTTP request bytesFCSeach layer prepends a header; Ethernet adds a trailer
Encapsulation: application data inside a TCP segment, inside an IP packet, inside an Ethernet frame.

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 (0x0800 means “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?

ComponentLayersWhat it does
Your programApplicationSpeaks HTTP, DNS and so on, through the socket API: connect, send, recv
The operating system (kernel)Transport and Internet, part of LinkTCP and UDP, IP, routing, finding MAC addresses
Driver and network cardLinkBuilds frames and turns them into signals
SwitchLink (L2)Moves frames between the machines of one local network, by MAC address
RouterInternet (L3)Moves packets between networks, by IP address
Firewall, load balancerL4 or L7Depends 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.