Sub topic 1
Why Networks Are Built in Layers
Prerequisites
This chapter assumes you already know the following:
- A network connects hosts, and each host has an IP address.
- Bytes can be written in hexadecimal, for example
0x45. - You can run basic commands in a terminal.
You don't need to know any header formats yet. Later chapters cover them one by one.
The problem layering solves
Sending a web page from a server to your laptop involves many separate jobs:
- Turning a request into bytes.
- Making sure the bytes arrive complete and in order.
- Choosing a path across dozens of networks.
- Pushing electrical, optical or radio signals over a cable or through the air.
A single program that did all of this would be huge. It would also be tied to specific hardware. A browser written for Wi-Fi would not work over Ethernet, and a new cable type would force a rewrite of every application.
Network designers split the work into layers. A layer is a group of related functions with a narrow, well-defined job. Each layer does its own job and relies on the layer below it for everything else.
The two rules of a layered model
Layering works because of two rules.
- Each layer offers a service to the layer above. For example, the transport layer offers "deliver this stream of bytes to a program on another host." The layer above only needs to know the service exists. It does not need to know how it works.
- Each layer talks to its peer. The transport layer on your laptop exchanges information with the transport layer on the server. It does this by adding information to the data it passes down. The layers below carry that information without reading it.
The second rule matters most for the rest of this tutorial. Logically, each layer has a conversation with the same layer on the other host. Physically, all the data travels down the stack on one side, across the wire, and up the stack on the other side.
What layering buys you
Layering has four practical benefits.
- Independence. You can replace one layer without changing the others. IPv4 runs over Ethernet, Wi-Fi, cellular links and fiber, because the Internet layer only expects "something that can carry a packet to the next hop."
- Reuse. One transport protocol such as TCP serves thousands of applications. HTTP, SSH and email clients all use it, and none of them reimplement retransmission.
- Smaller problems. Each layer can be designed, tested and debugged on its own. Engineers specialize: a browser developer does not need to understand radio modulation.
- Standard interfaces. Vendors can build compatible products. A router from one company forwards packets from a laptop from another because both follow the same IP rules.
The TCP/IP model and the OSI model
Two layered models are in common use. The TCP/IP model describes how the Internet actually works. The OSI model is a seven-layer reference model that is widely used for teaching and for naming layers. The table maps one to the other.
| OSI layer | TCP/IP layer | Typical job | Examples |
|---|---|---|---|
| 7 Application | Application | Meaning of the data exchanged | HTTP, DNS, SSH |
| 6 Presentation | Application | Encoding, compression, encryption | TLS, character sets |
| 5 Session | Application | Dialog management | Often part of the application |
| 4 Transport | Transport | Ports, reliability, ordering | TCP, UDP |
| 3 Network | Internet | End-to-end addressing and routing | IP, ICMP |
| 2 Data link | Link | Delivery on one local network | Ethernet, Wi-Fi |
| 1 Physical | Link | Signals and media | Copper, fiber, radio |
This tutorial uses four TCP/IP layers: application, transport, network (also called Internet) and link. The names vary between books. Many engineers still say "layer 2" for Ethernet and "layer 3" for IP, using the OSI numbers. Those numbers are common shorthand in documentation and product names, such as "layer 3 switch."
The OSI model is not the real protocol stack. Real protocols often do not fit one OSI layer. TLS is the usual example: it encrypts data like a presentation function, but it runs on top of TCP and below HTTP. Don't spend effort deciding which OSI layer a protocol "really" belongs to. Use the layers to describe what a protocol does and where it sits relative to others.
What each layer is responsible for
The responsibilities below are previews. Later chapters cover each layer in detail.
- Application. Defines what the data means. An HTTP request says "give me this page." The application layer does not care how the bytes travel.
- Transport. Connects a program on one host to a program on another, using port numbers. TCP adds reliability and ordering. UDP adds only ports and a checksum.
- Network. Gets a packet from the source host to the destination host across many networks, using IP addresses. The Internet layer does not guarantee delivery.
- Link. Gets a frame from one device to the next device on the same local network, using link-layer addresses such as MAC addresses.
Notice the change in scope as you go down. Transport is about two programs. The network layer is about two hosts. The link layer is about two neighbors. This difference explains why IP addresses stay the same across the whole journey while MAC addresses change at every router. Later chapters return to this point.
Where the layers live
Layers are implemented in different places.
- The application layer is in user programs such as browsers.
- Transport and network layers are usually in the operating system kernel.
- The link layer is split between the kernel driver and the network card.
- Routers implement the network and link layers only. They do not look at transport headers to forward a packet.
- Switches implement the link layer only.
This is why the network can carry any application without being upgraded. A router does not know whether a packet carries a web page or a video call.
Layering has a cost
Layering is a design choice with trade-offs, and it helps to know where it hurts.
- Overhead. Each layer adds its own header. A small payload can end up with a large fraction of header bytes. A 10-byte message sent over TCP and IPv4 on Ethernet carries 20 + 20 + 14 = 54 header bytes before the link-layer trailer.
- Hidden information. A layer cannot easily use facts known to another layer. TCP, for example, treats packet loss as a sign of congestion. On a lossy Wi-Fi link, that assumption is wrong, and TCP slows down more than it should.
- Leaky layers. Real systems break the clean boundaries. NAT devices rewrite IP addresses and also port numbers, so they look into the transport layer. Firewalls inspect headers from several layers. Load balancers may read HTTP.
- Duplicated work. Several layers may each offer error checking or retransmission, and these can interact badly.
Treat the model as a thinking tool, not a law. When a device crosses layers, it is doing so deliberately and you need to know about it.
Using layers to troubleshoot
A layered view gives you a systematic way to find faults. Start at the bottom and move up. If a lower layer fails, everything above it fails too, so checking a higher layer first wastes time.
# Link layer: is the interface up, and does it have a MAC address?
ip link show
# Network layer: does the interface have an IP address and a route?
ip addr show
ip route show
# Network layer, reachability of the gateway (use your gateway's address)
ping -c 3 192.168.1.1
# Network layer plus name resolution (DNS is an application protocol)
ping -c 3 example.com
# Transport layer: can we open a TCP connection to port 80?
nc -zv example.com 80
# Application layer: does the HTTP request succeed?
curl -I http://example.comRead the results in order:
| Symptom | Likely layer | Why |
|---|---|---|
| Interface shows `state DOWN` | Link | No cable, no Wi-Fi association, or interface disabled |
| No IP address or no default route | Network | DHCP failed or configuration is missing |
| Gateway pings, `example.com` does not resolve | Application (DNS) | Addressing works, name lookup fails |
| Ping works, `nc` to port 80 fails | Transport | A firewall or the server blocks that port |
| `nc` connects, `curl` returns an error status | Application | The server received the request and rejected it |
Note that ping works even when a firewall blocks TCP, because ping uses ICMP, a network-layer protocol, and not TCP. A successful ping does not prove that your web request will work.
Common mistakes
- Treating the OSI model as the real stack. Use it for vocabulary. Real behavior is defined by the protocols themselves.
- Mixing up layer scope. Saying "the packet's MAC address" or "the frame's IP address" blurs which layer owns which field. Each address belongs to the header of one layer.
- Assuming a lower layer provides a guarantee. IP does not promise delivery or ordering. If you need those, the transport layer or the application must provide them.
- Debugging from the top. Start low, as shown above.
- Believing layers never interact. NAT, firewalls and tunnels all cross layer boundaries. The next chapters show how headers are added and removed, and that explains how they do it.
Exercise
Your laptop shows a valid IP address and can ping its default gateway, but ping example.com reports "Name or service not known". Which layer-based reasoning explains where the fault is, and which layers can you rule out?
Show answer
The link and network layers work: the interface is up, the host has an address and a route, and ICMP packets reach the gateway. The fault is in name resolution, which is DNS, an application-layer protocol. Check the configured DNS server (for example in /etc/resolv.conf) and test it directly. You can rule out the link layer and basic IP connectivity to the gateway, though a problem further along the path to the DNS server is still possible.
Exercise
A router forwards packets between two networks. Which TCP/IP layers does it need to implement to do this, and why can the same router carry both web traffic and email without changes?
Show answer
A router implements the network layer (to read the IP header and choose a next hop) and the link layer (to receive and send frames on each connected network). It does not need the transport or application layers to forward a packet. Web and email traffic look the same to it, since both are IP packets, and the router never interprets the data above the IP header.