Topic Overview
OSI Model (7 Layers)
Learn the OSI 7-layer model for understanding network communication protocols.
The OSI (Open Systems Interconnection) model is a conceptual framework that divides network communication into 7 layers, each with specific functions.
The 7 Layers
Layer 7: Application
Purpose: Interface between user and network Examples: HTTP, HTTPS, FTP, SMTP, DNS Data unit: Message
Layer 6: Presentation
Purpose: Data translation, encryption, compression Examples: SSL/TLS, JPEG, MPEG Data unit: Message
Layer 5: Session
Purpose: Establish, manage, terminate sessions Examples: NetBIOS, RPC Data unit: Message
Layer 4: Transport
Purpose: End-to-end communication, error recovery Examples: TCP, UDP Data unit: Segment (TCP) / Datagram (UDP)
Layer 3: Network
Purpose: Routing, logical addressing Examples: IP, ICMP, OSPF, BGP Data unit: Packet
Layer 2: Data Link
Purpose: Framing, error detection, MAC addressing Examples: Ethernet, PPP, Switch Data unit: Frame
Layer 1: Physical
Purpose: Physical transmission of bits Examples: Cables, hubs, repeaters Data unit: Bit
Data Flow
Application Data
↓
[Layer 7] Adds application header
↓
[Layer 6] Adds presentation header
↓
[Layer 5] Adds session header
↓
[Layer 4] Adds transport header (TCP/UDP)
↓
[Layer 3] Adds network header (IP)
↓
[Layer 2] Adds data link header (Ethernet)
↓
[Layer 1] Transmits bits over physical medium
Encapsulation: Each layer adds its header to the data from the layer above.
Decapsulation: Each layer removes its header and passes data to the layer above.
Examples
HTTP Request Flow
1. Application Layer: Browser creates HTTP request
GET /index.html HTTP/1.1
Host: example.com
2. Transport Layer: TCP adds port numbers
Source Port: 49152
Dest Port: 80
3. Network Layer: IP adds IP addresses
Source IP: 192.168.1.100
Dest IP: 93.184.216.34
4. Data Link Layer: Ethernet adds MAC addresses
Source MAC: 00:1B:44:11:3A:B7
Dest MAC: 00:0C:29:AB:CD:EF
5. Physical Layer: Transmits bits over cable
Common Pitfalls
- Confusing layers: Not understanding which layer handles what. Fix: Remember "All People Seem To Need Data Processing" (Application to Physical)
- Mixing OSI and TCP/IP: TCP/IP has 4 layers, OSI has 7. Fix: Understand both models
- Layer boundaries: Some protocols span multiple layers. Fix: Focus on primary function
- Not understanding encapsulation: How data flows through layers. Fix: Trace a packet through each layer
Interview Questions
Beginner
Q: What is the OSI model and why is it important?
A: The OSI model is a 7-layer conceptual framework that standardizes network communication functions.
Why important:
- Standardization: Common language for networking
- Troubleshooting: Identify which layer has issues
- Design: Understand how protocols work together
- Education: Foundation for networking concepts
Layers (top to bottom): Application, Presentation, Session, Transport, Network, Data Link, Physical.
Intermediate
Q: Explain how an HTTP request flows through the OSI layers. What happens at each layer?
A:
Flow:
-
Application (Layer 7): Browser creates HTTP request
GET /page.html HTTP/1.1 Host: example.com -
Presentation (Layer 6): Data encoding/compression (if any)
-
Session (Layer 5): Establishes session (HTTP is stateless, minimal here)
-
Transport (Layer 4): TCP adds:
- Source port (ephemeral, e.g., 49152)
- Destination port (80 for HTTP)
- Sequence numbers, ACK numbers
- Creates TCP segment
-
Network (Layer 3): IP adds:
- Source IP (192.168.1.100)
- Destination IP (93.184.216.34)
- Creates IP packet
-
Data Link (Layer 2): Ethernet adds:
- Source MAC address
- Destination MAC (router's MAC)
- Creates Ethernet frame
-
Physical (Layer 1): Transmits bits over cable/wireless
Reverse on receiving side: Each layer removes its header and passes to layer above.
Senior
Q: Design a network troubleshooting system that identifies issues at different OSI layers. How do you detect and diagnose problems at each layer?
A:
Design:
class OSILayerDiagnostics {
// Layer 1 (Physical): Check physical connectivity
async diagnosePhysical(): Promise<Diagnostic> {
const checks = {
cableConnected: await this.checkCable(),
linkUp: await this.checkLinkStatus(),
signalStrength: await this.getSignalStrength()
};
if (!checks.cableConnected) {
return { layer: 1, issue: 'Cable not connected', fix: 'Check physical connections' };
}
if (!checks.linkUp) {
return { layer: 1, issue: 'Link down', fix: 'Check network interface' };
}
return { layer: 1, status: 'healthy' };
}
// Layer 2 (Data Link): Check MAC addressing, switching
async diagnoseDataLink(): Promise<Diagnostic> {
const checks = {
macAddress: await this.getMACAddress(),
arpTable: await this.checkARPTable(),
switchPort: await this.checkSwitchPort()
};
if (!checks.arpTable.has(checks.macAddress)) {
return { layer: 2, issue: 'ARP failure', fix: 'Check ARP table, network segment' };
}
return { layer: 2, status: 'healthy' };
}
// Layer 3 (Network): Check IP addressing, routing
async diagnoseNetwork(): Promise<Diagnostic> {
const checks = {
ipAddress: await this.getIPAddress(),
routingTable: await this.getRoutingTable(),
ping: await this.pingGateway()
};
if (!checks.ipAddress) {
return { layer: 3, issue: 'No IP address', fix: 'Check DHCP or static config' };
}
if (!checks.ping) {
return { layer: 3, issue: 'Cannot reach gateway', fix: 'Check routing table, gateway config' };
}
return { layer: 3, status: 'healthy' };
}
// Layer 4 (Transport): Check TCP/UDP, ports
async diagnoseTransport(): Promise<Diagnostic> {
const checks = {
tcpConnection: await this.testTCPConnection(),
portOpen: await this.checkPort(80),
firewall: await this.checkFirewallRules()
};
if (!checks.tcpConnection) {
return { layer: 4, issue: 'TCP connection failed', fix: 'Check firewall, port availability' };
}
return { layer: 4, status: 'healthy' };
}
// Layer 5-7 (Session, Presentation, Application)
async diagnoseApplication(): Promise<Diagnostic> {
const checks = {
dnsResolution: await this.resolveDNS('example.com'),
httpResponse: await this.testHTTP(),
sslCertificate: await this.checkSSL()
};
if (!checks.dnsResolution) {
return { layer: 7, issue: 'DNS resolution failed', fix: 'Check DNS server, network connectivity' };
}
if (!checks.httpResponse) {
return { layer: 7, issue: 'HTTP request failed', fix: 'Check application server, SSL/TLS' };
}
return { layer: 7, status: 'healthy' };
}
// Comprehensive diagnosis
async diagnoseAll(): Promise<DiagnosticReport> {
const results = await Promise.all([
this.diagnosePhysical(),
this.diagnoseDataLink(),
this.diagnoseNetwork(),
this.diagnoseTransport(),
this.diagnoseApplication()
]);
const issues = results.filter(r => r.issue);
return {
healthy: issues.length === 0,
issues,
recommendations: this.generateRecommendations(issues)
};
}
}
Tools for each layer:
- Layer 1:
ethtool, link status LEDs - Layer 2:
arp,ifconfig, switch logs - Layer 3:
ping,traceroute,ip route - Layer 4:
netstat,ss,telnet - Layer 5-7:
dig,curl,wireshark
Key Takeaways
- OSI model divides networking into 7 layers for standardization
- Encapsulation: Each layer adds header, wraps data from layer above
- Decapsulation: Each layer removes header, passes to layer above
- Layer functions: Application (user interface), Transport (end-to-end), Network (routing), Data Link (framing), Physical (bits)
- Troubleshooting: Identify which layer has the problem
- Protocol mapping: Understand which protocols operate at which layers
- TCP/IP vs OSI: TCP/IP has 4 layers, OSI has 7 (both useful for different purposes)