The TCP/IP model and encapsulation
TCP/IP is the 4-layer model the real internet runs on. Encapsulation is how each layer wraps the data in its own header (port, IP, MAC) on the sender, and decapsulation is how the receiver removes them in reverse order.
3.1 Why is it used?
- A browser's HTTP request (GET /) alone cannot be delivered; three questions are still open.
- Which application on the server? Answered by the port (Transport header).
- Which computer on the internet? Answered by the IP address (Internet header).
- Which next device on your LAN? Answered by the MAC address (Link header).
- Each answer is written by a different layer in its own header.
3.2 The 4 layers of the TCP/IP model
| Layer | What it adds | Examples |
|---|---|---|
| Application | The app's data | HTTP, DNS, SMTP |
| Transport | TCP/UDP header + port number (which application) | TCP, UDP |
| Internet | IP header: source + destination IP | IP, ICMP |
| Link (Network Access) | Ethernet/Wi-Fi header with MAC addresses; sends the frame on cable or air | Ethernet, Wi-Fi |
3.3 Comparison: OSI vs TCP/IP
| OSI layer(s) | TCP/IP layer |
|---|---|
| 7 Application, 6 Presentation, 5 Session | Application |
| 4 Transport | Transport |
| 3 Network | Internet |
| 2 Data Link + 1 Physical | Link |
- OSI: 7 layers, a teaching and troubleshooting model.
- TCP/IP: 4 layers, the practical model today's internet actually runs on.
3.4 How it works: encapsulation
- Step 1
1.Application: browser creates GET / (data)
- Step 2
2.Transport: TCP header, source port -> 80 (segment)
- Step 3
3.Internet: IP header .10 -> .20 (packet)
- Step 4
4.Link: Ethernet header with MACs + FCS trailer (frame)
- Step 5
5.Frame goes onto the wire
Like packing a gift in a box, then in a courier envelope
3.5 How it works: decapsulation
- Step 1
1.Link: destination MAC matches, Ethernet header removed
- Step 2
2.Internet: destination IP matches, IP header removed
- Step 3
3.Transport: destination port 80 -> the web app
- Step 4
4.Application: gets exactly GET / and replies 200 OK: hello-from-web
Headers come off in reverse order; the outermost (Ethernet) goes first
3.6 MAC + IP + port in one frame
| Header | Size | Key fields | PDU at this layer |
|---|---|---|---|
| Ethernet | 14 bytes | Destination MAC, source MAC, type | Frame |
| IP | 20 bytes | TTL, protocol, source/destination IP | Packet |
| TCP | 20+ bytes | Source port, destination port, seq | Segment |
| Data (payload) | varies | HTTP: GET / HTTP/1.1 | Data |
3.7 Practical: client MAC, IP and one request
Docker lab: client 192.168.23.10, web server 192.168.23.20.
# lab: client .10 → web server .20ip -br link show eth0ip -br -4 addr show eth0curl -s http://192.168.23.20/ip neigh$ ip -br link show eth0eth0@if258 UP 4a:9f:9d:ca:5e:c2$ ip -br -4 addr show eth0eth0@if258 UP 192.168.23.10/24$ curl -s http://192.168.23.20/hello-from-web$ ip neigh192.168.23.20 dev eth0 lladdr f6:72:d4:41:76:03 REACHABLE- ip -br link: the client's MAC 4a:9f:9d:ca:5e:c2.
- ip -br addr: the client's IP 192.168.23.10/24.
- curl gets hello-from-web from the server.
- ip neigh: the server's MAC f6:72:d4:41:76:03, learned through ARP.
3.8 tcpdump -e: MAC + IP + port
# capture on the server's eth0tcpdump -i eth0 -n -c 10 -w web.pcap tcp port 80# every frame: MAC, IP and port in one linetcpdump -r web.pcap -e -n -t -q$ tcpdump -i eth0 -n -c 10 -w web.pcap tcp port 80tcpdump: listening on eth0, link-type EN10MB (Ethernet)10 packets captured$ tcpdump -r web.pcap -e -n -t -q4a:9f:9d:ca:5e:c2 > f6:72:d4:41:76:03, IPv4, length 74: 192.168.23.10.57444 > 192.168.23.20.80: tcp 0f6:72:d4:41:76:03 > 4a:9f:9d:ca:5e:c2, IPv4, length 74: 192.168.23.20.80 > 192.168.23.10.57444: tcp 04a:9f:9d:ca:5e:c2 > f6:72:d4:41:76:03, IPv4, length 142: 192.168.23.10.57444 > 192.168.23.20.80: tcp 76- -i picks the interface, -w saves packets to a file, the filter tcp port 80 keeps only web traffic.
- -e prints the Ethernet header (MAC addresses); without it you see only IP and ports.
- Each line: source MAC > destination MAC, then IP.port > IP.port: all three addresses in one frame.
3.9 tshark: one frame, layer by layer
# the GET frame, one layer at a timetshark -r web.pcap -Y http.request -O eth,ip,tcp,httpFrame 4: 142 bytes on wire (1136 bits)Ethernet II Destination: f6:72:d4:41:76:03 Source: 4a:9f:9d:ca:5e:c2 Type: IPv4 (0x0800)Internet Protocol Version 4, Src: 192.168.23.10, Dst: 192.168.23.20 Time to Live: 64 Protocol: TCP (6)Transmission Control Protocol, Src Port: 57444, Dst Port: 80, Len: 76 Header Length: 32 bytes (8)Hypertext Transfer Protocol GET / HTTP/1.1\r\n Host: 192.168.23.20\r\n- Outermost: Ethernet II with destination MAC, source MAC, Type IPv4 (0x0800).
- Inside: IPv4 with TTL 64, protocol TCP (6), both IPs.
- Then TCP with ports 57444 -> 80, and inside it the HTTP GET request.
3.10 The raw bytes of a frame (-XX)
# first frame, as raw bytestcpdump -r web.pcap -n -t -XX -c 1IP 192.168.23.10.57444 > 192.168.23.20.80: Flags [S]0x0000: f672 d441 7603 4a9f 9dca 5ec2 0800 45000x0010: 003c 2ed3 4000 4006 5c7a c0a8 170a c0a80x0020: 1714 e064 0050 f630 013f 0000 0000 a002| Bytes | Meaning |
|---|---|
| f672 d441 7603 | Destination MAC f6:72:d4:41:76:03 |
| 4a9f 9dca 5ec2 | Source MAC 4a:9f:9d:ca:5e:c2 |
| 0800 | EtherType: IPv4 inside |
| 45.. | Start of the IP header (version 4, 20-byte header) |
| c0a8 170a / c0a8 1714 | 192.168.23.10 / 192.168.23.20 |
| e064 / 0050 | Port 57444 / port 80 |
3.11 Common mistakes
- Thinking the MAC stays the same end to end: every router removes the old Ethernet header and writes a new one for the next hop.
- The IP addresses and ports stay the same for the whole journey; only TTL drops by 1 at every router.
- Putting the port in the IP header: ports live in the TCP/UDP (Transport) header.
- Forgetting -e in tcpdump and then wondering where the MAC addresses are.
- Step 1
1.Frame 1: laptop 192.168.1.10 MAC -> Router 1 MAC
- Step 2
2.Router 1 removes the old Ethernet header, adds a new one
- Step 3
3.Frame 2: Router 1 MAC -> Router 2 (10.20.0.1) MAC
- Step 4
4.Frame 3: Router 2 MAC -> HR server 10.20.0.80 MAC
- Step 5
5.Same source/destination IP and ports all the way; TTL -1 per router
3.12 Interview tips
- Encapsulation: on the sender, data picks up a header at every layer going down (TCP/UDP, then IP, then Ethernet header + trailer).
- Decapsulation: the receiver removes headers in reverse order; the Ethernet header goes first.
- TCP/IP = 4 layers: Application, Transport, Internet, Link; a practical grouping of the 7 OSI layers.
- PDU names: data -> segment -> packet -> frame.
- MAC is hop by hop, IP and port are end to end.