What is NAT?
NAT is a technology that converts private IP addresses to public IP addresses.
01Why is NAT Needed?
IPv4 Problem:
- IPv4 = ~4.3 billion addresses
- 8+ billion devices worldwide
- Not enough!
Solution:
- Use private IPs in internal network
- Share single public IP with NAT
code
Internal (Private) NAT Internet (Public)
192.168.1.10 ────┐
192.168.1.11 ────┼───→ [Router] ───→ 203.0.113.5
192.168.1.12 ────┘ (NAT)
02Private IP Ranges
Reserved by RFC 1918:
| Class | Range | CIDR | Address Count |
|---|---|---|---|
| A | 10.0.0.0 - 10.255.255.255 | /8 | 16.7 million |
| B | 172.16.0.0 - 172.31.255.255 | /12 | 1 million |
| C | 192.168.0.0 - 192.168.255.255 | /16 | 65,536 |
These IPs are not routed on the internet!
03NAT Types
1. Static NAT (1:1)
code
Internal: 192.168.1.10 ←→ External: 203.0.113.10
- Fixed external IP for each internal IP
- Used for servers
- Expensive (needs many IPs)
2. Dynamic NAT
code
Internal IPs → [NAT Pool] → External IPs
- Dynamic assignment from IP pool
- Wait when pool is full
3. PAT (Port Address Translation)
code
192.168.1.10:5000 → 203.0.113.5:40001
192.168.1.11:5000 → 203.0.113.5:40002
192.168.1.12:5000 → 203.0.113.5:40003
- Most common type
- Single IP, different ports
- Also known as "NAT Overload"
04How PAT Works?
Outgoing Packet:
code
PC1: 192.168.1.10:3000 → google.com:443
Router writes to NAT table:
| Internal IP:Port | External IP:Port |
|------------------|------------------|
| 192.168.1.10:3000 | 203.0.113.5:40001 |
Outgoing packet:
203.0.113.5:40001 → google.com:443
Incoming Response:
code
google.com:443 → 203.0.113.5:40001
Router looks at NAT table:
40001 → 192.168.1.10:3000
Forwards to internal network:
google.com:443 → 192.168.1.10:3000
05NAT Limitations
- Peer-to-peer difficulty - Two devices behind NAT
- Some protocols problematic - FTP, SIP
- Port limit - ~65,000 simultaneous connections
- No end-to-end connection - IP changes
06Port Forwarding
Access server behind NAT:
code
Internet → Router (203.0.113.5:80) → Web Server (192.168.1.100:80)
Router rule:
- External port 80 → Internal 192.168.1.100:80
07Summary
- NAT = Private IP → Public IP conversion
- PAT = Multiple device sharing with ports
- Solution for IPv4 insufficiency
- Home routers use PAT