Sub topic 1
Why Networks Are Built in Layers
Prerequisites
This chapter assumes you know what a network and an IP address are, can read bytes in hexadecimal, and are comfortable running commands in a terminal. It introduces the layered model that the rest of the tutorial builds on.
The problem layers solve
Sending a web page from one computer to another involves many separate jobs. Something must pick the right application on the receiving host. Something must find a route across many networks. Something must put bits on a copper cable, a fiber or a radio channel. If one program handled all of this, every application would need to know about every kind of cable and Wi-Fi chip.
Networks avoid this by splitting the work into layers. Each layer does one job. It uses the service of the layer below and offers a service to the layer above. A layer does not need to know how the other layers work. It only needs to know what they promise.
The TCP/IP model
The Internet uses the TCP/IP model, which has four layers. Many textbooks split the bottom layer in two and show five.
| Layer | Job | Example protocols | Address used |
|---|---|---|---|
| Application | Meaning of the data exchanged | HTTP, DNS, TLS | Names, such as example.com |
| Transport | Delivery to the right program, and reliability if needed | TCP, UDP | Port numbers |
| Network (Internet) | Routing across networks | IP, ICMP | IP addresses |
| Link | Delivery across one local network | Ethernet, Wi-Fi | MAC addresses |
The OSI model is an older reference model with seven layers. It splits the application layer into session, presentation and application, and splits the link layer into data link and physical. You will meet OSI layer numbers in job descriptions and vendor documents, such as "layer 2 switch" for a link-layer device and "layer 3 router" for a network-layer device. This tutorial uses the TCP/IP names.
What a layer boundary gives you
A clear boundary between layers has practical effects:
- Replaceable parts. HTTP runs unchanged over Ethernet, Wi-Fi or a mobile network, because HTTP only talks to TCP, and TCP only talks to IP.
- Independent change. The Internet moved from IPv4 to IPv6 in the network layer without rewriting every web browser.
- Reuse. One TCP implementation serves email, web, file transfer and databases.
- Easier diagnosis. You can ask which layer is failing. A cable that is unplugged is a link problem. A wrong route is a network problem. A closed port is a transport problem.
Each layer also talks to its peer. TCP on your laptop exchanges information with TCP on the server. IP on your laptop works with IP on each router. In practice the data physically moves down the stack on the sender, across the wire, and up the stack on the receiver. The mechanism that makes this possible is that each layer adds its own header in front of the data it receives from above. That is encapsulation, which a later chapter covers in detail.
Where layering costs you
Layers are not free, and they are not perfectly clean.
- Overhead. Each layer adds a header, so the useful data is a smaller share of every frame.
- Leaky boundaries. Some protocols reach across layers. TCP checksums include IP addresses. NAT devices rewrite addresses and ports together. A firewall may inspect the application layer.
- Imperfect mapping. Some protocols do not fit one layer. TLS sits between the application and transport layers, and ARP sits between the network and link layers.
Treat the model as a map for reasoning, not as a strict law of how every protocol behaves.
Common mistakes
- Treating OSI layer numbers as the structure of the Internet. The Internet was designed around TCP/IP, and OSI is mainly a vocabulary.
- Assuming a router only looks at IP. A router receives a link-layer frame, removes it, reads the IP header, and builds a new frame for the next link.
- Assuming the layers are separate programs. In most systems, the transport and network layers are code inside the operating system kernel, and the link layer is split between the kernel driver and the network card.
Exercise
A "layer 3 router" forwards your traffic. Which TCP/IP layer does it mainly work at, and which kind of address does it use to decide where to send a packet?
Show answer
It works at the network layer (OSI layer 3) and uses the destination IP address to choose the next hop.
Exercise
Name two benefits of splitting network functions into layers, and one cost.
Show answer
Benefits include replaceable parts (HTTP works over Ethernet or Wi-Fi unchanged), independent change (IPv4 to IPv6 without rewriting browsers), reuse, and easier diagnosis. A cost is overhead, because each layer adds its own header. Boundaries can also leak, as with NAT or firewalls that inspect several layers.