Networking · The TCP/IP Stack

The TCP/IP Stack

Four layers, one idea: each layer solves exactly one problem and trusts the layer below.

Networking Basics → Interview
01

Why Layers?

Sending bytes across the planet involves dozens of problems — signal encoding, addressing, routing, reliability, naming, encryption. No single protocol could own all of it. So the internet splits the work into layers: each layer offers a service to the one above and consumes a service from the one below. Swap WiFi for fiber and nothing above the link layer notices; swap HTTP for SMTP and nothing below the transport layer cares.

LayerJobProtocolsUnit
ApplicationWhat to sayHTTP, DNS, SMTP, SSHMessage
TransportProcess-to-process deliveryTCP, UDPSegment / Datagram
InternetHost-to-host across networksIP, ICMPPacket
LinkNeighbour-to-neighbour on one mediumEthernet, WiFi, ARPFrame
The 7-layer OSI model is the textbook version; the 4-layer TCP/IP model is what the internet actually runs. Interviewers accept either — know how they map (OSI 5-7 ≈ Application, OSI 1-2 ≈ Link).
02

Encapsulation — Wrapping the Data

Each layer wraps the payload from above with its own header, like nested envelopes. Your HTTP request becomes the payload of a TCP segment, which becomes the payload of an IP packet, which rides inside an Ethernet frame. The receiver unwraps in reverse.

HTTP data TCP data IP TCP + data Eth IP + TCP + data FCS L7L4L3L2

Headers carry the metadata each layer needs: TCP adds ports and sequence numbers, IP adds source/destination addresses, Ethernet adds MAC addresses and a checksum trailer.

03

Who Reads What

Not every device reads every header. A switch looks only at frames (L2). A router unwraps to the IP packet (L3), picks the next hop, and re-wraps in a fresh frame. Only the end hosts ever look at TCP and HTTP — the network core stays dumb, the intelligence lives at the edges.

Router touches
  • Ethernet frame — stripped & rebuilt per hop
  • IP header — TTL decremented, checksum redone
Router never touches
  • TCP ports & sequence numbers
  • HTTP request itself
  • TLS-encrypted payload
This is why traceroute works: it sends packets with increasing TTL. Each router that decrements TTL to zero sends back an ICMP error — revealing itself.
04

See the Stack in Action

bash — watch the layers
# L3/L4: show the path packet takes (TTL trick)
traceroute example.com

# L4: see live TCP connections and their states
ss -t          # or netstat -t

# L7 over L4 over L3: verbose curl shows DNS, TCP connect, TLS, HTTP
curl -v https://example.com

curl -v is a one-command tour of the whole stack: name resolution (DNS), the TCP connect, the TLS handshake, then the HTTP request and response.

05

Interview Questions

Why is the internet built in layers?

Separation of concerns: each layer solves one problem behind a stable interface, so technologies can be swapped independently — WiFi for Ethernet, HTTP/3 for HTTP/1.1 — without rewriting the rest of the stack.

What is encapsulation?

Each layer prepends its own header to the payload from the layer above — HTTP inside TCP inside IP inside an Ethernet frame. The receiving host strips headers in reverse order.

Difference between a switch and a router?

A switch forwards frames within one network using MAC addresses (L2). A router connects networks, forwarding packets using IP addresses (L3), decrementing TTL each hop.

Map OSI to TCP/IP.

OSI application+presentation+session ≈ TCP/IP application layer; OSI transport = transport; OSI network = internet; OSI data-link+physical ≈ link layer.

Quick Quiz

1. Which layer delivers data process-to-process (port-to-port)?
2. A router primarily operates at which layer?
3. The unit of data at the link layer is a…
4. Encapsulation means…
5. traceroute discovers routers by exploiting…