MarzleyTech Learn

Home / Learn / Networking / The OSI and TCP/IP models: all seven layers, encapsulation, PDUs and layered troubleshooting

The OSI and TCP/IP models: all seven layers, encapsulation, PDUs and layered troubleshooting

When you open a website, many things happen: your browser makes a request, it's split into pieces, addressed, sent as electrical, light or radio signals across many devices, and put back together at the other end. That's a lot of different jobs. Network models divide the work into layers, so each layer has one clear job and only talks to the layers just above and below it.

Why it matters to you:

  • Design: vendors can build a Wi-Fi card (layer 1–2) without caring which website you'll visit (layer 7).
  • Troubleshooting: engineers ask "is this a layer 1 cable problem or a layer 3 IP problem?" and fix faults faster.
  • Exams and interviews: the OSI model is in every networking certification (Network+, CCNA) and most networking interviews.

The OSI model (7 layers)

OSI (Open Systems Interconnection) was published by ISO as a reference model. Count the layers from the bottom (1) to the top (7).

#LayerJobExamplesData unit (PDU)
7ApplicationNetwork services for the user's programsHTTP, HTTPS, DNS, SMTP, FTP, SSHData
6PresentationFormat, encryption, compressionTLS encryption, JPEG, UTF-8Data
5SessionOpens, manages and closes conversationsLogin sessions, RPCData
4TransportEnd-to-end delivery, ports, reliabilityTCP, UDPSegment (TCP) / datagram (UDP)
3NetworkLogical addressing and routing between networksIP, ICMP, routersPacket
2Data linkDelivery on the local network using MAC addressesEthernet, Wi-Fi (802.11), switchesFrame
1PhysicalBits as signals on the mediumCables, fibre, radio, connectors, hubsBits

Memory trick (top to bottom, 7→1): All People Seem To Need Data Processing. Bottom to top (1→7): Please Do Not Throw Sausage Pizza Away.

Each layer in more detail

Layer 1: Physical

Moves raw bits as voltages on copper, pulses of light on fibre, or radio waves. Defines cables (Cat6, fibre), connectors (RJ45, LC), pin-outs, signal speeds and Wi-Fi radio frequencies. Problems here: unplugged or damaged cables, bad crimping, weak Wi-Fi signal, a dead port.

Layer 2: Data link

Delivers frames between devices on the same local network using MAC addresses (hardware addresses like 3C:52:82:1A:0F:9B). Detects transmission errors with a checksum (FCS) at the end of each frame. Switches, network cards and Wi-Fi access points work here. VLANs and ARP live around this layer.

Layer 3: Network

Gives devices logical addresses (IP addresses) and routes packets between different networks, choosing paths across the internet. Routers and layer 3 switches work here. ICMP (used by ping and traceroute) is a layer 3 helper protocol.

Layer 4: Transport

Delivers data between applications on the two devices using port numbers (HTTPS uses 443, SSH 22). TCP is reliable: it numbers segments, resends lost ones and puts them in order. UDP is fast and simple with no guarantees, used for video calls, gaming, DNS lookups and streaming.

Layer 5: Session

Sets up, manages and ends conversations between applications: keeping a login session alive, resuming an interrupted transfer. In TCP/IP, applications usually handle this themselves.

Layer 6: Presentation

Makes sure data is in a format the receiver understands: character encoding (UTF-8), file formats (JPEG, MP4), compression and encryption (TLS, which turns HTTP into HTTPS).

Layer 7: Application

The protocols programs use to talk over the network: HTTP/HTTPS for the web, DNS to look up names, SMTP/IMAP for email, SSH for remote login, FTP/SFTP for files. Note: the application layer is the protocol, not the program itself (Chrome uses HTTP; Chrome isn't layer 7).

The TCP/IP model (what the internet actually uses)

The internet was built on the TCP/IP protocol suite, which uses a simpler 4-layer model:

TCP/IP layerMatches OSI layersProtocols
Application5, 6, 7HTTP, DNS, DHCP, SMTP, SSH, TLS
Transport4TCP, UDP
Internet3IPv4, IPv6, ICMP
Network access (Link)1, 2Ethernet, Wi-Fi, fibre

Some books show a 5-layer version that splits network access into Data link and Physical. Engineers use OSI numbers as shorthand ("a layer 2 switch", "a layer 7 firewall") but the real protocols follow TCP/IP.

Devices by layer

DeviceLayerWhy
Hub, repeater, cable, media converter1Just pass signals
Switch, bridge, access point, NIC2Use MAC addresses
Router, layer 3 switch3Use IP addresses
Traditional firewall3–4Filters by IP addresses and ports
Next-generation firewall, load balancer, proxyup to 7Understand applications (e.g. block a specific website)

Encapsulation

As data goes down the layers on the sender, each layer wraps it with its own header (like putting a letter in an envelope, then in a courier bag, then in a crate):

  1. Your browser creates an HTTP request → data.
  2. TCP adds a header with source and destination ports (e.g. 51515 → 443) and sequence numbers → segment.
  3. IP adds a header with source and destination IP addresses → packet.
  4. Ethernet/Wi-Fi adds a header with source and destination MAC addresses and a trailer (FCS checksum) → frame.
  5. The network card sends the frame as bits.
[ Ethernet header | IP header | TCP header | HTTP data | Ethernet trailer ]
     layer 2         layer 3      layer 4       layer 7        layer 2

The receiver does the opposite, de-encapsulation: each layer reads and removes its header, then passes the rest up.

A key detail: along the path, routers rewrite the layer 2 header at every hop (new MAC addresses for each link), but the IP addresses stay the same end to end (unless NAT changes them, covered in the NAT lesson).

Python · runs live in the interactive lesson
# A toy model of encapsulation: each layer wraps the one above
data = "GET /index.html"
segment = {"src_port": 51515, "dst_port": 443, "payload": data}
packet = {"src_ip": "192.168.1.20", "dst_ip": "93.184.216.34", "payload": segment}
frame = {"src_mac": "3C:52:82:1A:0F:9B", "dst_mac": "A4:91:B1:00:11:22", "payload": packet}

layer = frame
for name in ["Frame (L2)", "Packet (L3)", "Segment (L4)"]:
    print(name, {k: v for k, v in layer.items() if k != "payload"})
    layer = layer["payload"]
print("Data (L7):", layer)

Troubleshooting with layers

The bottom-up method: start at layer 1 and work up until you find the fault.

LayerQuestionsTools
1 PhysicalCable plugged in? Link lights on? Wi-Fi on and signal strong?Eyes, cable tester, Wi-Fi analyser
2 Data linkConnected to the right Wi-Fi/VLAN? Port enabled?Switch interface status, arp -a
3 NetworkHas a valid IP address? Can you ping the gateway? Route correct?ipconfig/ip a, ping, tracert
4 TransportIs the port open, or blocked by a firewall?Test-NetConnection, nc, ss
7 ApplicationIs the service running? Does DNS resolve? Right URL/credentials?Browser, nslookup, logs

Other approaches: top-down (start with the application, good when you suspect software) and divide-and-conquer (start in the middle with ping: if pinging works, layers 1–3 are fine).

Example

A cashier says "the till can't reach the M-Pesa system".

  1. L1: cable is plugged in, switch port light is on.
  2. L3: ipconfig shows 169.254.x.x (an automatic address used when DHCP fails). The till never got an IP address → the problem is DHCP or the switch/VLAN, not M-Pesa.
Think about it: A user can open websites by IP address (e.g. http://93.184.216.34) but not by name (example.com). Which layer and service is the likely problem?Show answer

Layers 1–4 work (data reaches the server by IP). The failure is name resolution: DNS, an application-layer (layer 7) service. Check the DNS server settings with nslookup or ipconfig /all.

Summary

  • Layers split networking into jobs; OSI has 7 (Physical, Data link, Network, Transport, Session, Presentation, Application).
  • PDUs: bits (1), frames (2), packets (3), segments/datagrams (4), data (5–7).
  • TCP/IP has 4 layers: Network access, Internet, Transport, Application.
  • Encapsulation adds headers going down (ports, IPs, MACs); de-encapsulation removes them going up.
  • Switches are layer 2, routers layer 3; troubleshoot bottom-up, top-down or divide-and-conquer.

Check yourself

  1. Which OSI layer number is the Network layer?

    Show answer

    3

  2. Which OSI layer do switches mainly work at? (number)

    Show answer

    2

  3. What is the data unit called at the Network layer?

    Show answer

    packet

  4. Which layer adds port numbers: Transport or Network?

    Show answer

    transport

  5. In the TCP/IP model, which layer does IP belong to?

    Show answer

    internet

  6. Which OSI layer handles encryption and formatting (name)?

    Show answer

    presentation

  7. What is the data unit at layer 2 called?

    Show answer

    frame

Learn more: Cloudflare: What is the OSI model? · Professor Messer's free Network+ videos

Lesson 3 of 24 in Networking · Written by · Course notes