Network Layer
IP addresses, routing, and how packets find their way across the internet.
What Layer 3 Does
Layer 2 (Data Link) handles communication on a single network segment—devices connected to the same switch or hub. But the internet is millions of separate networks. How does a packet get from your laptop to a server in another country?
That's Layer 3's job: routing packets between networks.
IP Addresses
While MAC addresses identify hardware, IP addresses identify network locations. Think of it like:
IPv4 Structure
IPv4 addresses are 32 bits, written as four decimal octets: 192.168.1.100
Network vs Host
Every IP address has two parts: the network portion (which network you're on) and the host portion (which device on that network). The subnet mask defines where this split happens.
With a /24 mask, the first 24 bits identify the network, leaving 8 bits for hosts. That's 28 - 2 = 254 usable addresses per network (minus network address and broadcast).
CIDR Notation
Instead of writing the full subnet mask, CIDR notation just appends the number of network bits:
192.168.1.0/24256 addresses (254 usable)10.0.0.0/816.7 million addresses172.16.0.0/121 million addressesSubnet Mask Visualizer
| CIDR | Mask | Hosts | Use Case |
|---|---|---|---|
| /8 | 255.0.0.0 | 16.7M | Large enterprise, ISP |
| /16 | 255.255.0.0 | 65K | Campus network |
| /24 | 255.255.255.0 | 254 | Home/small office |
| /28 | 255.255.255.240 | 14 | Small VLAN |
| /30 | 255.255.255.252 | 2 | Point-to-point link |
| /32 | 255.255.255.255 | 1 | Single host route |
Private vs Public Addresses
With only ~4.3 billion IPv4 addresses and billions of devices, we'd run out fast. The solution: private address ranges that can be reused on every local network.
| Range | CIDR | Addresses |
|---|---|---|
| 10.0.0.0 – 10.255.255.255 | /8 | 16.7 million |
| 172.16.0.0 – 172.31.255.255 | /12 | 1 million |
| 192.168.0.0 – 192.168.255.255 | /16 | 65,536 |
Your home router has a public IP (assigned by your ISP) and assigns private IPs to devices on your network. NAT (Network Address Translation) rewrites packet headers so all your devices share one public IP.
IP Packet Structure
Just like Ethernet frames wrap data at Layer 2, IP packets wrap data at Layer 3. The Ethernet frame's payload contains the IP packet.
Key Fields
6 = TCP,17 = UDP,1 = ICMPHow Routing Works
Every router maintains a routing table—a list of network destinations and where to forward packets for each. When a packet arrives:
Example Routing Table
| Destination | Gateway | Interface |
|---|---|---|
| 192.168.1.0/24 | directly connected | eth0 |
| 10.0.0.0/8 | 192.168.1.1 | eth0 |
| 0.0.0.0/0 | 192.168.1.254 | eth0 |
0.0.0.0/0 entry is the default route—used when no other entry matches.Longest Prefix Match
If multiple routes match, the router picks the most specific one(longest prefix). For destination 10.1.2.3:
10.0.0.0/8matches (8 bits)10.1.0.0/16matches (16 bits)10.1.2.0/24wins! (24 bits - most specific)ICMP: Network Diagnostics
ICMP (Internet Control Message Protocol) is Layer 3's error reporting and diagnostic tool. It's what powers ping and traceroute.
ping sends Echo Request (type 8), target responds with Echo Reply (type 0)traceroute maps the path to a destination.How Traceroute Works
IPv6: The Future
IPv4's 4.3 billion addresses weren't enough. IPv6 expands to 128 bits—enough for 340 undecillion addresses (3.4 × 1038).
IPv6 Shorthand
2001:0db8:0000:0000:0000:0000:0000:00012001:db8::1Leading zeros can be dropped. A single :: can replace consecutive groups of zeros (but only once per address).
Key Differences from IPv4
Layer 3 Summary
Next up: Layer 4 (Transport) — TCP and UDP, ports, reliable delivery, and how applications multiplex over a single IP address.