OSI & TCP/IP Models
Seven-layer OSI model, four-layer TCP/IP stack, encapsulation, and how HTTP maps to each layer.
Overview
The OSI model is a conceptual framework that divides network communication into seven layers. Each layer has one job, and each has its own tools for testing it.
- Layer 1, Physical: bits and signals (cables, radio, fiber, NICs). Check it with ethtool and the link lights.
- Layer 2, Data Link: MAC addressing, ARP, Ethernet frames, switches. Check it with arp and tcpdump.
- Layer 3, Network: IP addressing and routing (routers, VPC route tables), plus ICMP. Check it with ping, traceroute and ip route.
- Layer 4, Transport: TCP (reliable, ordered) and UDP (connectionless), addressed by port. Check it with nc, ss and netstat.
- Layer 5, Session: session establishment and teardown.
- Layer 6, Presentation: encoding and encryption (TLS spans L4-L6). Check TLS with openssl s_client.
- Layer 7, Application: HTTP, HTTPS, DNS, SMTP, gRPC. Check it with curl, dig and grpcurl.
The TCP/IP model (the actual Internet stack) collapses OSI into four layers: Link (OSI 1-2), Internet (OSI 3), Transport (OSI 4), Application (OSI 5-7).
Encapsulation
Encapsulation means each layer wraps the payload above with its own header as data travels down the stack, and strips headers traveling up (decapsulation). An HTTP request becomes an HTTP payload, wrapped in a TCP segment, wrapped in an IP packet, wrapped in an Ethernet frame. Follow one HTTPS request down the stack.
- Application payload: the browser's request is HTTP GET /index.html.
- TLS record (L6 wraps): a TLS record header goes on, and the HTTP payload behind it is encrypted.
- TCP segment (L4 wraps): the segment header carries source port 48210, destination port 443, and the seq/ack numbers and flags.
- IP packet: the packet header carries source 10.0.1.5, destination 93.184.216.34, TTL=64 and proto=TCP.
- Ethernet frame (L2 wraps): the frame adds the source and destination MAC addresses and EtherType=0x0800, the code for an IPv4 payload.
- Signal (L1 sends): the frame leaves as an electrical or optical signal on the wire.
In AWS, two filters inspect that TCP segment. A Security Group is a stateful L4 filter: a rule that allows port 443 out also lets the replies back in. A NACL is a stateless L4 filter, so you must check its rules in both directions.
Key concepts
- Encapsulation
- Layer 3 (Network)
- Layer 4 (Transport)
- Layer 7 (Application)
- PDU names by layer
Interview questions
- What happens at each layer when you type a URL in a browser and press Enter?
- What is the difference between AWS ALB and NLB? When do you choose each?
Sign up free to read the full topic
More Networking topics
Start free on Cariara · Coding problems · System design studio