# OSI and TCP/IP Models

Understanding layered networking models is fundamental. They help you isolate problems and understand how data flows across networks.

## The OSI Model (7 Layers)

The Open Systems Interconnection model is a theoretical reference model.

| Layer | Name | PDU | Function | Examples |
|---|---|---|---|---|
| **7** | Application | Data | Network services to applications | HTTP, HTTPS, DNS, SMTP, SSH |
| **6** | Presentation | Data | Data translation, encryption, compression | TLS/SSL, JPEG, ASCII, UTF-8 |
| **5** | Session | Data | Session management, dialog control | NetBIOS, RPC |
| **4** | Transport | Segment (TCP) / Datagram (UDP) | End-to-end reliability, flow control, ports | TCP, UDP |
| **3** | Network | Packet | Logical addressing, routing between networks | IP (v4/v6), ICMP, OSPF, BGP |
| **2** | Data Link | Frame | Physical addressing (MAC), local delivery, error detection | Ethernet, Wi-Fi (802.11), switches |
| **1** | Physical | Bits | Transmission of bits over medium | Cables, fiber, radio, hubs |

**Mnemonic (top to bottom):** All People Seem To Need Data Processing  
**Mnemonic (bottom to top):** Please Do Not Throw Sausage Pizza Away

## The TCP/IP Model (4 Layers)

The practical model used on the internet.

| Layer | OSI Equivalent | Protocols | Purpose |
|---|---|---|---|
| **Application** | 5,6,7 | HTTP, HTTPS, DNS, SSH, SMTP, FTP | Applications and services |
| **Transport** | 4 | TCP, UDP | End-to-end communication |
| **Internet** | 3 | IP, ICMP, ARP, IGMP | Routing and addressing |
| **Network Access (Link)** | 1,2 | Ethernet, Wi-Fi, PPP | Physical and local network access |

## Encapsulation

As data travels down the stack, each layer adds its own header (and sometimes trailer).

```mermaid
graph LR
    A[Application Data] --> B[TCP Segment]
    B --> C[IP Packet]
    C --> D[Ethernet Frame]
    D --> E[Bits on Wire]
    
    style A fill:#e1f5fe
    style B fill:#fff3e0
    style C fill:#f3e5f5
    style D fill:#e8f5e9
    style E fill:#c8e6c9
```

| PDU Name | Layer | Contains |
|---|---|---|
| **Data** | Application/Presentation/Session | User data |
| **Segment/Datagram** | Transport | Data + TCP/UDP header (source/dest ports) |
| **Packet** | Network | Segment + IP header (source/dest IP) |
| **Frame** | Data Link | Packet + Ethernet header/trailer (source/dest MAC) |
| **Bits** | Physical | Electrical/optical/radio signals |

## Decapsulation

On the receiving end, headers are removed layer by layer as data moves up the stack.

## Practical Debugging by Layer

| Issue Type | Likely Layer | Tools/Checks |
|---|---|---|
| **Can't connect to app** | 7 (Application) | Check app logs, curl, browser dev tools |
| **TLS/certificate errors** | 6 (Presentation) | openssl s_client, browser cert check |
| **Port issues** | 4 (Transport) | netstat, ss, telnet/nc to port |
| **Can't reach remote network** | 3 (Network) | ping, traceroute/tracert, ip route |
| **Local connectivity issues** | 2 (Data Link) | arp, switch logs, MAC table |
| **Physical issues** | 1 (Physical) | Cables, link lights, interface up/down |

## When to Use Which Model

- **OSI Model**: Great for troubleshooting (systematic layer-by-layer approach), certification exams
- **TCP/IP Model**: Reflects reality of how internet actually works

## Example: Loading a Website

1. **Application (7/HTTP)**: Browser sends HTTP GET request
2. **Transport (4/TCP)**: TCP establishes connection (3-way handshake), segments data
3. **Network (3/IP)**: IP adds source/dest IP addresses, routes packet
4. **Data Link (2/Ethernet)**: Adds MAC addresses for local delivery
5. **Physical (1)**: Converts to bits and transmits

## Related Notes

- [[Foundations/Packet-Flow-Encapsulation.md]] — Detailed packet walkthrough
- [[Foundations/Addressing-Subnetting-CIDR.md]] — IP addressing
- [[Core-Protocols/IP-TCP-UDP.md]] — Transport and network protocols
- [[Automation-Observability/Packet-Analysis-Wireshark.md]] — See layers in packets

## Sources

* Kurose & Ross - *Computer Networking: A Top-Down Approach*  
* Tanenbaum & Feamster - *Computer Networks*  
* RFC 1122 (Requirements for Internet Hosts)  
* ISO 7498-1 (OSI Reference Model)  
