Lesson 03 beginner

The OSI and TCP/IP Models

Why networks are built in layers, the seven OSI layers and the four TCP/IP layers, what each one adds, how encapsulation wraps data into segments, packets and frames, and how to watch the layers in a real capture.

Networking is a stack of small jobs. One layer moves bits over a wire, another finds the next hop, another makes sure nothing is lost, another speaks HTTP. Each layer only talks to the layer directly above and below it, so you can swap Wi-Fi for fiber without rewriting your browser. Two models describe the stack: OSI, a seven-layer teaching model, and TCP/IP, the four-layer model the internet actually runs on.

The seven OSI layers

#LayerJobUnit of dataExamples
7ApplicationThe service the user or program wantsDataHTTP, DNS, SMTP, SSH
6PresentationFormat, encoding, encryption, compressionDataTLS, UTF-8, JSON, gzip
5SessionOpening, keeping and closing conversationsDataRPC sessions, TLS session resumption
4TransportProcess to process delivery, ports, reliabilitySegment (TCP) / datagram (UDP)TCP, UDP, QUIC
3NetworkLogical addressing and routing between networksPacketIPv4, IPv6, ICMP
2Data linkDelivery on one local link, MAC addressesFrameEthernet, Wi-Fi (802.11), PPP
1PhysicalBits as signals on a mediumBitCopper, fiber, radio, connectors

A classic mnemonic from the bottom up is “Please Do Not Throw Sausage Pizza Away”.

The four TCP/IP layers

TCP/IP layerMaps to OSIMain protocols
Application5, 6 and 7HTTP, DNS, TLS, SSH, SMTP
Transport4TCP, UDP, QUIC
Internet3IP, ICMP
Link (network access)1 and 2Ethernet, Wi-Fi, ARP

TCP/IP came from working code; OSI came from a standards committee. TCP/IP won the internet, but engineers still use OSI numbers every day: “it’s a layer 2 problem” means a switching or VLAN issue, and a “layer 7 load balancer” reads HTTP.

Where TLS lives: TLS encrypts data (a presentation job) and manages sessions (a session job), but runs as part of the application in TCP/IP terms. Do not lose sleep over exact placement; know what the protocol does.

Encapsulation

On the way down, each layer wraps what it got from above in its own header (and Ethernet adds a trailer too). On the way up, the receiver peels the wrappers off in reverse order.

Application   [ HTTP request: GET /tutorials/ ... ]
Transport     [ TCP header | HTTP request ]                         -> segment
Network       [ IP header | TCP header | HTTP request ]              -> packet
Data link     [ Eth header | IP | TCP | HTTP request | Eth trailer ] -> frame
Physical      0110100101110101...                                   -> bits

Each header answers one question:

  • TCP header: which process? (source and destination ports) and which bytes? (sequence numbers)
  • IP header: which host? (source and destination IP addresses)
  • Ethernet header: which device on this link? (source and destination MAC addresses)

A key detail: the IP addresses stay the same from end to end (unless NAT rewrites them), but the MAC addresses change at every router, because each hop is a new local link.

Watch the layers in a capture

tcpdump -e prints the layer 2 header, and -nn keeps numbers instead of names:

$ sudo tcpdump -i enp3s0 -e -nn -c 1 'tcp port 443'
14:02:11.318842 3c:2c:4a:1b:9c:10 > 00:1a:2b:3c:4d:5e, ethertype IPv4 (0x0800), length 74:
    192.168.1.42.51644 > 203.0.113.10.443: Flags [S], seq 2849021137, win 64240,
    options [mss 1460,sackOK,TS val 391724 ecr 0,nop,wscale 7], length 0

Read it layer by layer:

Part of the lineLayerMeaning
3c:2c:… > 00:1a:…2From my NIC to the router’s MAC
ethertype IPv42 to 3The payload is an IPv4 packet
192.168.1.42 > 203.0.113.103From my host to the web server
.51644 > .4434From a random client port to HTTPS
Flags [S]4A TCP SYN, the first step of a connection

For a full layer-by-layer view, save the capture with -w web.pcap and open it in Wireshark, which shows each header as an expandable tree.

Troubleshooting with layers

The layers give you a checklist. Work bottom up and stop at the first layer that fails:

LayerQuestionCommand
1Is there a link?ip link, ethtool enp3s0
2Can I reach the gateway’s MAC?ip neigh
3Do I have an address and a route? Can I reach the gateway, then the internet?ip addr, ip route, ping
4Is the port open?ss -tln, nc -zv host 443
7Does the application answer correctly?curl -v https://host/

DNS sits at layer 7, but it breaks so often that it gets its own step: if ping 1.1.1.1 works but ping example.com does not, it is DNS. Lesson 24 turns this into a full playbook.

Exercise

Which header changes at every router hop on the way to a website (without NAT)?

  1. The IP header’s destination address
  2. The TCP destination port
  3. The Ethernet source and destination MAC addresses
  4. The HTTP Host header
Show answer

Answer: C. Each hop is a new local link, so the router writes new layer 2 addresses. IP addresses and ports stay the same end to end unless NAT rewrites them.

Exercise

A colleague says “the firewall drops it at layer 4”. What is the firewall most likely matching on?

  1. TCP or UDP port numbers
  2. MAC addresses
  3. Voltage levels on the cable
  4. The HTML in the response
Show answer

Answer: A. Layer 4 is the transport layer, so the rule matches ports (and protocol). Layer 2 would be MAC addresses, layer 7 would be application content.

Exercise

What do we call the unit of data at the network layer?

  1. Frame
  2. Segment
  3. Bit
  4. Packet
Show answer

Answer: D. Bits at layer 1, frames at layer 2, packets at layer 3, segments (TCP) or datagrams (UDP) at layer 4.

Key takeaways

  • OSI has seven layers; TCP/IP has four and runs the internet.
  • Each layer adds a header on the way down and removes it on the way up.
  • Ports pick the process, IP picks the host, MAC picks the next device on the link.
  • MAC addresses change every hop; IP addresses do not (unless NAT).
  • Troubleshoot bottom up: link, local delivery, routing, ports, application.

FAQ

Why learn OSI if the internet uses TCP/IP?

Because the vocabulary is everywhere. Vendors, job interviews and incident channels say “layer 2”, “layer 4” and “layer 7” constantly, and those numbers come from OSI.

Which layer does ARP belong to?

ARP maps IP addresses (layer 3) to MAC addresses (layer 2), so it sits between them. Most people call it a layer 2 protocol because its messages travel in Ethernet frames and never cross a router.

Is HTTPS a different layer from HTTP?

No. HTTPS is HTTP carried inside TLS. HTTP is still the application protocol; TLS adds encryption beneath it.

Do I need Wireshark if I have tcpdump?

tcpdump is enough to capture and filter on a server. Wireshark is far nicer for reading what you captured, so a common habit is to capture with tcpdump -w on the server and open the file in Wireshark on your laptop.

↑ ↓ navigate Enter open Esc close