Notes
Four layers
The internet works in layers. The application layer decides what to say, for example an HTTP request. The transport layer decides which program on the machine gets it, using port numbers, and whether delivery is reliable, TCP, or best effort, UDP. The network layer, IP, gets the packet to the right machine, even across many networks. The link layer moves it one hop, across one cable or Wi-Fi network, using MAC addresses.
- Application what to say HTTP, DNS, SSH, TLS
- Transport which program, reliable or not: TCP, UDP
- Network which machine, across networks: IP
- Link the next hop on this cable: Ethernet, MAC
OSI numbers
You will also hear the older OSI model, with seven layers. Real networks follow the four layer TCP IP model, but the OSI numbers stuck as vocabulary. Layer seven is the application, layer four is transport, layer three is IP, layer two is Ethernet. So a layer four load balancer looks at IP addresses and ports, and a layer seven load balancer reads the HTTP request itself. Layers five and six barely exist as separate things.
- 7 Application HTTP, gRPC
- 6 Presentation (TLS, encoding)
- 5 Session
- 4 Transport TCP, UDP
- 3 Network IP, ICMP
- 2 Data link Ethernet, ARP
- 1 Physical cables, radio
- real stacks merge 5, 6, 7
Encapsulation
When a program sends data, each layer wraps it in its own header, like envelopes inside envelopes. TCP adds ports and sequence numbers and the result is a segment. IP adds source and destination addresses, making a packet. Ethernet adds MAC addresses, making a frame, which goes on the wire. The other machine unwraps them in reverse: Ethernet, then IP, then TCP, and finally hands the data to the right program.
- data "hello shoprung"
- + TCP hdr ports, seq, flags = segment
- + IP hdr source, dest IP = packet
- + Eth hdr MAC addresses = frame
- the receiver unwraps in reverse order
Watch it
Let's see it. We ran a tiny TCP echo server on port nine thousand and captured the traffic with tcpdump. Our client sent the fourteen bytes hello shoprung. tcpdump printed IP, the network layer, then source and destination addresses with their port numbers, the transport layer, then the TCP flags and a length of fourteen: our data. One line shows three layers wrapped around one message.
IP 127.0.0.1.51334 > 127.0.0.1.9000: Flags [P.], length 14 "hello shoprung" IP = network layer .51334 .9000 = TCP ports Flags, length = TCP header 14 = our bytes of data
Size limits
Every header costs bytes, and every link has a maximum packet size, the MTU. Our machine's interface had an MTU of fourteen hundred bytes. An IP version four header is twenty bytes and a basic TCP header is twenty more, so each packet carried at most about thirteen hundred and sixty bytes of data. A larger HTTP response is split into many packets and reassembled by TCP. Episode seventeen shows what happens when MTUs disagree.
$ ip link show eth0 mtu 1400 IPv4 header 20 bytes TCP header 20 bytes (+ options) room for data per packet = MTU - 40 = 1360 here bigger messages are split into many packets
Debug by layer
The layers give you a checklist. At layer seven, curl, dig and openssl test the application, DNS and TLS. At layer four, ss and netcat show whether the port is open and something is listening. At layer three, ip route, ping and mtr test whether the address is reachable at all. At layer two, ip link and ip neigh show whether the link is up and ARP works. Work through them in order and you stop guessing.
- L7 curl -v, dig, openssl s_client: app, DNS, TLS
- L4 ss -tlnp, nc -zv: is the port open and listening?
- L3 ip route, ping, mtr: can I reach that IP?
- L2 ip link, ip neigh: is the link up, is ARP working?
- go bottom-up or top-down
Map the error
Most error messages tell you the layer. Could not resolve host is DNS. Connection refused means the machine answered but nothing listens on that port, layer four. Connection timed out means packets got no answer at all, usually a firewall or routing problem. Certificate verify failed is TLS. And a five oh two from a proxy means the proxy worked but could not reach the app behind it. Every one of these appears in this part with a real example.
- "could not resolve host" → DNS (application)
- "connection refused" → L4: nothing listening
- "connection timed out" → L3/L4: dropped, firewall
- "certificate verify failed" → TLS
- "502 Bad Gateway" → L7 proxy could not reach the app
Common mistakes
Mistakes. Saying the network is down without naming which layer fails. Testing only with ping, when many networks block ping but allow web traffic. Debugging the application code before checking DNS and whether the port is open. Mixing up layer four and layer seven load balancing. And forgetting that packet size limits exist.
- "the network is down" with no layer named
- testing with ping only (ICMP may be blocked)
- debugging the app before checking DNS and the port
- confusing L4 and L7 load balancing
- ignoring the MTU
Say it like this
In the interview: real networks use four layers, link, internet, transport and application, and each wraps the data above it in its own header. The OSI numbers are the shared vocabulary: layer three is IP, layer four is TCP and UDP ports, layer seven is HTTP. Then show you can use it: when something is unreachable, I check DNS, the route, whether the port is open, then TLS and the application.
- "Real stacks use four layers: link, IP, TCP or UDP, and the application. Each wraps the data above in a header. OSI
- numbers name them: L3 is IP, L4
- is ports, L7 is HTTP. When I
- debug, I check DNS, then the route, the port, then TLS."