Chapters

TCP/IP & OSI models

Section 1 built up one piece at a time: IP, masks, gateways, MAC, ARP, NAT. This chapter gives those pieces a place. Layers are the map every later chapter hangs on.

Layers, routing and transport · chapter 11 of 34 · 6 min read

Why layers

Networking is hard because it does many jobs at once. When your laptop calls kade-api, something has to:

JobSolved in
1Say what the app wants: "GET /profile"HTTP (chapter 1)
2Reach the right program on the server, reliably and in orderTCP and ports (coming)
3Find a way across many networksIP, routing (chapters 2-6)
4Get across one local link, as signalsMAC, ARP, WiFi (chapters 7-9)

Layering splits these jobs apart. Each job is handled by one layer, and each layer only talks to the layer directly above and below it.

The TCP/IP model: 4 layers

The model the internet actually runs on is the TCP/IP model. Top to bottom:

LayerJobProtocolsAddress usedUnit of data
4 · ApplicationWhat your program speaksHTTP, DNS, TLS, SSHnames, URLsmessage
3 · TransportApp-to-app deliveryTCP (reliable), UDP (fast)portssegment (TCP), datagram (UDP)
2 · InternetRouting across networksIP, ICMPIP addressespacket
1 · LinkOne local hop, as signalsEthernet, WiFi, ARPMAC addressesframe
These are the same colours used in every diagram since chapter 1: plum application, amber transport, green IP, teal link.

The OSI model: 7 layers

The OSI model is the classic teaching model. It splits the top and bottom of TCP/IP into finer pieces:

#LayerJobExamplesUnit
7ApplicationNetwork services an app usesHTTP, DNS, SSHdata
6PresentationFormat of the data: encoding, encryption, compressionUTF-8, JSON, gzip, TLS (partly)data
5SessionOpening, keeping and closing a conversationlogin sessions, TLS session resumptiondata
4TransportApp-to-app deliveryTCP, UDPsegment / datagram
3NetworkRouting across networksIP, ICMP, routerspacket
2Data linkDelivery on one local linkEthernet, WiFi, MAC, switchesframe
1PhysicalBits as signalscables, radio, light, the NIC's transmitterbits
In real software, layers 5 and 6 are rarely separate. HTTP and TLS do those jobs themselves. That is why TCP/IP folds them into one Application layer.

How the two line up

The models are not rivals. They describe the same stack at different levels of detail:

OSI · 7 layersTCP/IP · 4 layers7 · Application6 · Presentation5 · Session4 · Transport3 · Network2 · Data link1 · PhysicalApplicationHTTP, DNS, TLSTransportTCP, UDPInternetIP, ICMPLinkEthernet, WiFi, ARP

Wide by design - scroll it sideways.

  • OSI's top three (Application, Presentation, Session) → TCP/IP Application.
  • OSI Transport → TCP/IP Transport. OSI Network → TCP/IP Internet.
  • OSI's bottom two (Data link, Physical) → TCP/IP Link.

Layer numbers in everyday talk

When people say "layer N", they mean the OSI number. Here is what the common phrases mean, with Kadé examples:

#You hearIt meansKadé example
2"a layer-2 switch"Moves frames by MAC address inside one networkThe staff network's switch (chapter 6)
3"a layer-3 device"Routes packets by IP address between networksThe office router
4"a layer-4 load balancer"Spreads connections using only IP and port; never reads HTTPA GCP passthrough network load balancer
7"a layer-7 proxy"Reads the HTTP request (path, headers) to decideA GCP application load balancer sending /api/* to kade-api
3/4"a layer-3/4 firewall"Allows or blocks by IP, protocol and portGCP VPC firewall rules: only kade-api to kade-db on 5432
7"a WAF"Web application firewall: inspects HTTP contentCloudflare or Cloud Armor blocking bad requests

Layers explain encapsulation

Chapter 1's three envelopes were the layers at work. Going down the stack, each layer wraps what it got from above in its own header. At the receiver, each layer removes its own header and passes the rest up.

LaptopRouterkade-apiApplication · HTTPTransport · TCPInternet · IPLink · WiFinever opened hereInternet · IPLink · WiFi / EthernetApplication · HTTPTransport · TCPInternet · IPLink · EthernetHTTP talks to HTTPTCP talks to TCP (end to end)down the stack = wrapup to IP, look up, down againup the stack = unwrapA router only unwraps as far as the Internet layer. It reads the IP header to choose the next hop, thenbuilds a new frame. It never reads your TCP or HTTP (NAT is the one exception: it also rewrites ports).

Wide by design - scroll it sideways.

LayerAddsResult is called
Applicationthe HTTP request itselfmessage (data)
TransportTCP or UDP header: ports, sequence numbersegment / datagram
InternetIP header: source and destination IP, TTLpacket
Linkframe header and trailer: MACs, FCSframe
(Physical)nothing: turned into signalsbits

Remembering the OSI order

Bottom → top (1 to 7)

Please Do Not Throw Sausage Pizza Away

Physical, Data link, Network, Transport, Session, Presentation, Application

Top → bottom (7 to 1)

All People Seem To Need Data Processing

Application, Presentation, Session, Transport, Network, Data link, Physical

The model as a filing system

When you meet a new protocol, ask "what layer is this?" and you immediately know its job. Test yourself, including a few tricky ones:

  1. HTTP
  2. TCP
  3. IP
  4. Ethernet
  5. DNS
  6. UDP
  7. ICMP
  8. ARP
  9. TLS
  10. DHCP
  11. BGP
  12. WiFi
  13. SSH
  14. NAT

Score: 0 / 0 answered, of 14.

Debugging layer by layer

Layers also give you a debugging order. When "kade-api is down" from the office, check from the bottom up. Each step only makes sense once the one below it works:

#LayerQuestionCommandIf it fails
1LinkAm I connected to the local network?ip link, arp -aCable, WiFi or switch problem. Gateway MAC missing.
2InternetCan packets get there?ping 172.16.1.1, ping 1.1.1.1, tracerouteRouting, gateway or ISP problem (chapters 5, 12, 13)
3TransportIs the port open?nc -vz api.kade.lk 443Firewall rule, or the app is not listening
4ApplicationDoes the app answer correctly?dig api.kade.lk, curl -v https://api.kade.lk/healthDNS, TLS certificate or app bug

Summary

#IdeaIn one lineNew terms
1LayeringOne job per layer; each talks only to its neighbourslayer
2TCP/IPApplication, Transport, Internet, Link: what the internet runsTCP/IP model
3OSI7 layers for teaching and talking; 5 and 6 rarely separateOSI model
4MappingOSI 7-5 → Application, 4 → Transport, 3 → Internet, 2-1 → Link
5Talk"L4" = IP + port only; "L7" = reads HTTPL4 / L7
6EncapsulationWrap going down, unwrap going up; routers stop at the Internet layersegment, datagram, packet, frame
8FilingKnow the layer, know the job
9DebuggingCheck bottom-up: link, IP, port, app

Try it yourself

Three packets, all layers at once

  • What to look for: -e adds the MACs (link), then IPs (internet), then ports and TCP flags (transport).
  • Runs on: macOS (Linux: -i wlan0 or eth0)
  • From section: 6. Layers explain encapsulation
sudo tcpdump -e -n -c 3 -i en0 port 443

The same, as a tree

  • Steps: Wireshark
  • What to look for: Click one packet: Frame → Ethernet → IP → TCP → TLS/HTTP, one layer per line.
  • From section: 6. Layers explain encapsulation

A transport-layer check

  • What to look for: "succeeded" means the port is open, without speaking HTTP at all.
  • Runs on: any host and port
  • From section: 9. Debugging layer by layer
nc -vz api.kade.lk 443

An application-layer check

curl -v https://example.com

Spotted a mistake, or something unclear? Suggest an edit on GitHub