The OSI Model and TCP/IP Networking Architecture

The Open Systems Interconnection (OSI) 7-layer model and the practical 4-layer TCP/IP stack define how data is encapsulated, addressed, routed, and delivered across computer networks. This guide covers layer mechanics, protocol data units (PDUs), packet encapsulation headers, ARP, MTU fragmentation, and structured bottom-up network diagnostics.


⚡ Quick Dive

OSI vs. TCP/IP Architecture Comparison

OSI Layer Name TCP/IP Layer Protocol Data Unit (PDU) Core Protocols & Technologies Diagnostic Tool
Layer 7 Application Application Data / Message HTTP/HTTPS, DNS, SSH, gRPC, SMTP curl, dig, grpcurl
Layer 6 Presentation Application Data / Message TLS/SSL, JSON, Protobuf, ASCII, Gzip openssl s_client
Layer 5 Session Application Data / Message SOCKS5, RPC, NetBIOS, gRPC streams lsof, ss
Layer 4 Transport Transport Segment (TCP) / Datagram (UDP) TCP, UDP, QUIC, SCTP (Port numbers) nc -zv, telnet, iperf3
Layer 3 Network Internet Packet IPv4, IPv6, ICMP, BGP, OSPF, IPsec ping, traceroute, mtr, ip route
Layer 2 Data Link Network Access / Link Frame Ethernet (802.3), Wi-Fi (802.11), ARP, VLAN (802.1Q) ip link, arp -a, bridge
Layer 1 Physical Network Access / Link Bits Fiber optics, Cat6 copper, Transceivers, PHY chips ethtool, dmesg

📖 Extended Guide

1. Packet Encapsulation & Decapsulation Pipeline

As data travels down the stack from an application process, each layer prepends its own control metadata header (Encapsulation). On the receiving host, headers are stripped sequentially as data moves up (Decapsulation):

Application Layer:  [ HTTP Request Payload (Data) ]
                               │
Transport Layer:    [ TCP Header (Src Port: 54321, Dst Port: 443) ][ Payload ]  ◄── (Segment)
                               │
Network Layer:      [ IP Header (Src IP: 192.168.1.5, Dst IP: 93.184.216.34) ][ TCP Header ][ Payload ]  ◄── (Packet)
                               │
Data Link Layer:    [ Ethernet Header (Src MAC, Dst MAC) ][ IP Header ][ TCP ][ Payload ][ Frame Check Sequence (CRC32) ]  ◄── (Frame)
                               │
Physical Layer:     01001000 01100101 01101100 01101100 01101111 ...  ◄── (Physical Electrical/Optical Bits)

2. Address Resolution Protocol (ARP) & L2/L3 Bridging

Routers and switches forward packets using IP addresses at Layer 3, but physical NICs require 48-bit MAC addresses at Layer 2.

Host A (192.168.1.10) wants to send packet to Host B (192.168.1.20):
1. Host A checks its local kernel ARP cache (`ip neigh`).
2. If absent, Host A broadcasts an ARP Request: "Who has 192.168.1.20? Tell 192.168.1.10" (MAC: FF:FF:FF:FF:FF:FF).
3. Host B responds via unicast ARP Reply: "192.168.1.20 is at MAC 00:1A:2B:3C:4D:5E".
4. Host A caches the mapping and transmits the Ethernet frame.

3. Maximum Transmission Unit (MTU) & IP Fragmentation

  • Standard Ethernet MTU: $1500$ bytes (Maximum IP packet size transmitted without fragmentation).
  • MSS (Maximum Segment Size): The maximum TCP payload size: $\text{MSS} = \text{MTU} - (\text{IP Header: } 20\text{B}) - (\text{TCP Header: } 20\text{B}) = 1460\text{ bytes}$.
  • Path MTU Discovery (PMTUD): TCP sets the Don't Fragment (DF) bit in the IP header. If a router along the path has a smaller MTU (e.g., VPN tunnel with 1420B MTU), it drops the packet and replies with an ICMP "Fragmentation Needed" error, instructing the sender to lower its MSS.

4. Structured Bottom-Up Network Troubleshooting

When diagnosing connectivity outages, always test systematically from Layer 1 up to Layer 7:

# 1. Layer 1 (Physical): Is the link physically up and negotiated?
ethtool eth0 | grep "Link detected"

# 2. Layer 2 (Data Link): Is the interface assigned an IP and communicating with gateway MAC?
ip link show eth0
ip neigh show

# 3. Layer 3 (Network): Can we route packets to default gateway and public IPs?
ping -c 3 192.168.1.1
mtr -rwc 10 8.8.8.8

# 4. Layer 4 (Transport): Is the destination port open and accepting TCP handshakes?
nc -zv 192.168.1.50 443

# 5. Layer 7 (Application): Does the server respond with valid HTTP/TLS status codes?
curl -Iv https://example.com/healthz