In this lesson:
- why networking is split into layers, each with one job
- the OSI 7-layer reference model vs the TCP/IP 4-layer practical model
- encapsulation: how each layer wraps the one above with its own header
- where an embedded stack (lwIP-style) fits
- the jargon (layer 2/3) and the "model β implementation" trap
Why layers
Sending a sensor reading to a cloud server involves dozens of concerns: voltage on a wire, addressing, reliable delivery, encryption, message format. Trying to solve all of that in one blob is unmanageable. Networking is instead split into layers, each handling one job and talking only to the layer directly above and below it.
The payoff is modularity: swap the bottom layer (WiFi instead of Ethernet) and everything above keeps working unchanged; improve TCP without touching your application. Each layer is a contract.
Two models
OSI (Open Systems Interconnection) is the 7-layer reference model, a teaching and design framework. TCP/IP is the 4-layer model the real internet runs on. They map onto each other:
OSI (7-layer reference) TCP/IP (4-layer, real internet)
βββββββββββββββββββββββββ βββββββββββββββββββββββββ
β 7 Application β β β
β 6 Presentation β ββββββββΆ β Application β HTTP, MQTT, CoAP, TLS
β 5 Session β β β
βββββββββββββββββββββββββ€ βββββββββββββββββββββββββ€
β 4 Transport β ββββββββΆ β Transport β TCP, UDP
βββββββββββββββββββββββββ€ βββββββββββββββββββββββββ€
β 3 Network β ββββββββΆ β Internet β IP, ICMP
βββββββββββββββββββββββββ€ βββββββββββββββββββββββββ€
β 2 Data Link β ββββββββΆ β Link β Ethernet, WiFi MAC
β 1 Physical β β β (cables, radio, PHY)
βββββββββββββββββββββββββ βββββββββββββββββββββββββWhat each TCP/IP layer does:
| Layer | Job | Examples |
|---|---|---|
| Application | the actual service / message format | HTTP, MQTT, CoAP, DNS, TLS |
| Transport | end-to-end delivery between programs (ports) | TCP (reliable), UDP (best-effort) |
| Internet | addressing and routing between networks | IP, ICMP |
| Link | move frames over one physical link | Ethernet, WiFi, PHY |
OSI's upper three (session, presentation, application) collapse into TCP/IP's single Application layer; in practice the internet is TCP/IP, and OSI is the vocabulary everyone uses to discuss it.
Encapsulation: wrapping on the way down
When you send data, each layer wraps the data from the layer above in its own header (and the link layer adds a trailer). The receiver peels them off in reverse.
your data: [ payload ]
Transport (TCP): add TCP header [TCP| payload ] β "segment"
Internet (IP): add IP header [IP |TCP| payload ] β "packet"
Link (Ethernet): add MAC hdr+FCS [ETH|IP |TCP| payload|FCS] β "frame"
Physical: send as bits ββββββββ...Each layer only reads its header and treats everything inside as opaque payload. That's why a router (Internet layer) inspects the IP header but doesn't care about your TCP or application data, and a switch (Link layer) looks only at MAC addresses. The names matter in interviews: segment (transport) β packet (IP) β frame (link).
Encapsulation also has a cost: every header is overhead. On a constrained IoT link with a small MTU (maximum transmission unit), TCP+IP+link headers can be a big fraction of a small message, a reason lightweight stacks and CoAP-over-UDP exist.
Where an embedded stack fits
On an MCU you typically run a small TCP/IP stack like lwIP (lightweight IP) that implements the Link, Internet, and Transport layers (Ethernet/WiFi driver β IP β TCP/UDP), exposing a socket-like API. You write the Application layer, the MQTT client, the CoAP handler, the sensor protocol. The stack is trimmed for RAM (fewer buffers, smaller windows) compared to a desktop OS's stack.
Gotchas
- OSI is a reference model, not what runs. The internet is TCP/IP; OSI is the shared vocabulary and a design lens. Don't expect a literal 7-layer implementation, sessions/presentation rarely exist as separate layers.
- "Layer 2" vs "Layer 3" jargon. People say layer-2 (Link, switches, MAC) and layer-3 (Internet, routers, IP) constantly. A switch forwards by MAC (L2); a router forwards by IP (L3). Know the numbers.
- Headers are overhead. Each layer adds bytes; on small IoT messages the header-to-payload ratio is high, which motivates UDP-based, compact protocols (CoAP) over TCP+HTTP.
- Layer boundaries are about what, not where. A layer can be in hardware (Ethernet MAC/PHY), in the stack (IP/TCP), or in your app, the model describes responsibilities, not a fixed code location.
TL;DR
- Networking is split into layers, each with one job, talking only to adjacent layers, giving modularity (swap WiFi for Ethernet without touching TCP) and clear contracts.
- OSI is the 7-layer reference model; TCP/IP is the 4-layer model the internet actually uses: Application (HTTP/MQTT/CoAP/TLS), Transport (TCP/UDP), Internet (IP), Link (Ethernet/WiFi).
- Encapsulation wraps your payload layer by layer with headers (segment β packet β frame); each layer reads only its own header and treats the rest as opaque.
- Encapsulation costs overhead, significant for tiny IoT messages, motivating compact UDP-based protocols.
- On embedded, a light stack (lwIP) handles Link/Internet/Transport and you write the Application layer; remember the L2 = switch/MAC, L3 = router/IP jargon and that OSI is a model, not the implementation.