Network Models — OSI & TCP/IP
Why layering exists, the seven OSI layers and the four/five TCP/IP layers with protocols, devices and PDUs at each, encapsulation and decapsulation, ports and sockets, and how to use the model to debug problems.
Why layers?
Networking is a huge problem: electrical signals, local delivery, global routing, reliability, application semantics. Layering splits it so each layer:
- offers a service to the layer above,
- uses the service of the layer below,
- talks to its peer layer on the other machine through a protocol.
Swap a layer (Wi-Fi → Ethernet, IPv4 → IPv6, HTTP/1.1 → HTTP/2) and the rest keeps working.
OSI vs TCP/IP
What each layer does
| Layer | Problem it solves | Addressing | Key ideas |
|---|---|---|---|
| Physical | moving bits | — | encoding, bandwidth, signal |
| Data link | delivery on one local network | MAC address (48-bit) | framing, switching, error detection (CRC), MTU (1500 B Ethernet) |
| Network | delivery across networks | IP address | routing, fragmentation, TTL, best effort |
| Transport | delivery to the right process, reliability | port (16-bit) | TCP (reliable stream), UDP (datagrams), congestion control |
| Session/Presentation/Application | meaning of the data | URLs, hostnames | HTTP semantics, encoding, encryption, sessions |
Encapsulation
Important header fields
| Header | Fields worth knowing |
|---|---|
| Ethernet | source/destination MAC, EtherType, FCS |
| IPv4 | source/destination IP, TTL (hop limit, decremented per router), protocol (6 = TCP, 17 = UDP), total length, fragmentation flags, checksum |
| TCP | source/destination port, sequence and acknowledgement numbers, flags (SYN, ACK, FIN, RST, PSH, URG), window size, options (MSS, window scale, SACK, timestamps) |
| UDP | ports, length, checksum — that's all (8 bytes) |
Ports and sockets
- Port: 16-bit number identifying a process/service on a host. Well-known: 22 SSH, 25 SMTP, 53 DNS, 80 HTTP, 443 HTTPS, 5432 Postgres, 6379 Redis, 9092 Kafka. Ephemeral (client) ports: typically 32768–60999 on Linux.
- Socket: an endpoint = IP + port (+ protocol), exposed to programs as a file descriptor.
- Connection (TCP): the 5-tuple (protocol, src IP, src port, dst IP, dst port).
Python
import socket
# minimal TCP client: everything below HTTP is done by the OS
with socket.create_connection(("example.com", 80), timeout=5) as s:
s.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n")
print(s.recv(200).decode(errors="replace"))
Using layers to debug
| Symptom | Check (bottom-up) | Tools |
|---|---|---|
| no link | cable/Wi-Fi, interface up | ip link, ethtool |
| can't reach the gateway | L2/L3: IP, subnet, ARP | ip addr, ip neigh, ping gateway |
| can't reach the internet | routing, DNS vs IP | ping 1.1.1.1, traceroute, mtr |
| name doesn't resolve | DNS | dig, nslookup |
| connection refused/timeout | L4: port open? firewall? | nc -vz host 443, ss -tlnp, telnet |
| TLS errors | certificates, SNI, versions | openssl s_client -connect host:443 -servername host |
| HTTP errors | status codes, headers | curl -v, browser devtools |
| weird behaviour | look at the packets | tcpdump, Wireshark |
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.