Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 79 ยท Getting Started with Networks

Layered Models: OSI and TCP/IP

Understand why networks use layers: the seven OSI layers, the TCP/IP model, encapsulation, PDUs and the difference between a protocol and a service, with live code.

Sending a web page involves an astonishing number of jobs: choosing a route, correcting errors, turning bits into electrical or radio signals, making sure nothing is lost, and presenting data to a program. No single piece of software does all of it. Instead, networks split the work into layers. Each layer solves one problem, uses the layer below as a service, and offers a simpler service to the layer above.

This lesson introduces the two layered models you will meet everywhere, OSI and TCP/IP, explains encapsulation, and shows what every header costs. The rest of the tutorial is organized by layer, so this lesson is your map. It builds on How Data Travels, and you may find the short version in Networking Basics helpful.

Why layers?

Layering brings the same benefits as good software design:

  • Separation of concerns. The people who design Wi-Fi do not need to know how web browsers work, and the other way around.
  • Replaceable parts. HTTP works the same over Ethernet, Wi-Fi or a mobile network. A layer can change (IPv4 to IPv6, copper to fiber) while the others stay.
  • Easier learning and troubleshooting. When something breaks, you can ask which layer is at fault ("is the cable plugged in? does DNS answer? does the server respond?").
  • Interoperability. A standard interface between layers lets products from different vendors work together.

The cost is some overhead (every layer adds a header) and occasionally less efficiency, since layers cannot see what the others are doing. Real stacks sometimes "cheat" across layers, but the model remains the shared vocabulary.

The OSI reference model

The OSI model (Open Systems Interconnection) was defined by ISO as a theoretical reference with seven layers. Numbering goes from the bottom (1) to the top (7).

# Layer Job Examples PDU name
7 Application Services used directly by programs HTTP, DNS, SMTP Data / message
6 Presentation Data format, encoding, encryption, compression TLS (partly), character sets, JSON Data
5 Session Start, manage and end dialogues Session handling in RPC, NetBIOS Data
4 Transport Delivery between processes, reliability, ports TCP, UDP Segment / datagram
3 Network Addressing and routing between networks IP, ICMP Packet
2 Data link Delivery over one link, MAC addressing, framing Ethernet, Wi-Fi Frame
1 Physical Bits as signals on a medium Cables, radio, fiber Bits

A classic memory aid, from top to bottom: "All People Seem To Need Data Processing." (Application, Presentation, Session, Transport, Network, Data link, Physical.)

OSI was never implemented as a complete protocol suite. Its value is as a vocabulary: engineers say "a layer 2 switch", "a layer 3 problem" or "a layer 7 firewall" every day.

The TCP/IP model

The protocols that actually run the Internet follow the TCP/IP model, which is older and simpler. It has four layers (some books split the bottom one into two, giving five).

TCP/IP layer Covers OSI layers Examples
Application 5, 6, 7 HTTP, DNS, SMTP, TLS
Transport 4 TCP, UDP, QUIC
Internet (network) 3 IP, ICMP, routing protocols
Link (network access) 1, 2 Ethernet, Wi-Fi, PPP

The five-layer variant, common in teaching, splits the link layer into data link and physical. This tutorial uses that five-layer view and keeps the OSI numbers when helpful.

flowchart LR
  subgraph OSI["OSI (7 layers)"]
    direction TB
    o7["7 Application"] --- o6["6 Presentation"] --- o5["5 Session"] --- o4["4 Transport"] --- o3["3 Network"] --- o2["2 Data link"] --- o1["1 Physical"]
  end
  subgraph TCP["TCP/IP (5 layers)"]
    direction TB
    t5["Application"] --- t4["Transport"] --- t3["Internet"] --- t2["Data link"] --- t1["Physical"]
  end
  OSI ~~~ TCP

The three top OSI layers collapse into one application layer in TCP/IP.

A key difference of philosophy: OSI defined the layers first and the protocols afterwards, while TCP/IP grew out of working protocols. The Internet's design also puts intelligence at the edges: routers inside the network only forward packets (layers 1 to 3), and the end hosts implement the upper layers.

Which devices work at which layer?

  • A hub or repeater works at layer 1: it copies signals.
  • A switch works at layer 2: it reads MAC addresses in frames.
  • A router works at layer 3: it reads IP addresses in packets.
  • A firewall can work at layers 3 and 4 (addresses and ports) or at layer 7 (looking inside HTTP).
  • Hosts implement all layers.

Encapsulation and decapsulation

When an application sends data, each layer on the way down wraps it in its own header (and sometimes a trailer). This is encapsulation. At the destination, each layer on the way up removes its header and passes the rest upward: decapsulation.

flowchart TB
  A["Application: message"] --> T["Transport: segment<br/>[TCP header][message]"]
  T --> N["Network: packet<br/>[IP header][TCP header][message]"]
  N --> L["Link: frame<br/>[Ethernet header][IP header][TCP header][message][trailer]"]
  L --> P(["Physical: bits on the medium"])

Each layer treats everything from the layer above as an opaque payload.

The unit of data at each layer is its PDU (protocol data unit): message, segment, packet, frame, bits. A switch reads only the outermost (link) header, a router opens the frame to read the IP header, and only the destination host reads the TCP header and finally the message. When a router forwards a packet, it strips the old link header and builds a new one for the next link; the IP packet inside stays the same, apart from fields such as the time-to-live.

The example below builds all three headers with Python's struct module, using the real binary layouts for Ethernet II, IPv4 and TCP (the checksums are left at zero for simplicity), and then takes the frame apart again.

Python Encapsulation and decapsulation in bytes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The 43-byte request grows to 97 bytes, and 56% of the frame is now headers. In the hex dump you can spot the fields: 08 00 is the Ethernet type for IPv4, 45 means IP version 4 with a 20-byte header, 40 is a TTL of 64 and 06 the protocol number for TCP. (A real Ethernet frame also ends with a 4-byte checksum, which this sketch leaves out.)

Overhead is not the same for every message. The second example computes how large the share of headers is for small and large payloads, using 20 bytes for TCP, 20 for IP and 18 for Ethernet (header plus trailer).

Python What do headers cost?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A 10-byte message is 85.3% header, while a full 1,460-byte payload is only 3.8%. This is one more reason applications try to send fewer, larger messages instead of many tiny ones.

Protocol versus service

Two ideas are easy to mix up.

  • A service is what a layer offers to the layer above it through an interface. Example: "deliver this chunk of bytes reliably to that port."
  • A protocol is the set of rules two peer layers on different machines follow to talk to each other. Example: how TCP uses sequence numbers and acknowledgments.

Each layer on the sender communicates logically with the same layer on the receiver (peer-to-peer communication), even though the real data goes down the stack, across the wire and back up. The upper layers are unaware of the details below, which is what lets you swap the lower layers without rewriting the applications.

What is real and what is a stand-in? The header layouts and field values in the first example are the real binary formats for Ethernet II, IPv4 and TCP, but checksums are zero, no handshake happened, and no data was sent anywhere. The sizes of 20, 20 and 18 bytes are the minimum for TCP without options, IPv4 without options and Ethernet with its 4-byte trailer.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which OSI layer is responsible for routing packets between networks?

    The network layer (IP) provides logical addressing and routing across networks.
  2. 2. The TCP/IP application layer corresponds to which OSI layers?

    TCP/IP merges session, presentation and application into one application layer.
  3. 3. What is the correct name for the PDU at the data link layer?

    Frames at layer 2, packets at layer 3, segments (or datagrams) at layer 4, and messages or data at the top.
  4. 4. A switch forwards traffic using which information?

    A classic switch is a layer 2 device and reads only the link-layer header.
  5. 5. What best describes encapsulation?

    Going down the stack, each layer wraps the data it received; at the destination the wrappers are removed in reverse order.

Summary

  • Layers split a hard problem into small jobs, make parts replaceable and give everyone a shared vocabulary.
  • OSI has seven layers (physical to application); TCP/IP has four or five and is what the Internet really uses.
  • Switches work at layer 2, routers at layer 3, and hosts implement every layer.
  • Encapsulation wraps data in a header per layer on the way down; decapsulation unwraps it on the way up. PDUs are bits, frames, packets, segments and messages.
  • Headers cost bytes, which matters most for tiny messages.
  • A service is what a layer offers upward; a protocol is the rule set between peer layers on different machines.

Next: The Structure of the Internet.