Layer 3 is responsible for Addressing (giving every device an ID) and Routing (moving packets to the right destination).
The Core Analogy: The Postal System
- IP Address = Home Address. A unique identifier so the network knows where to send data.
- Packet = The Letter. Data is chopped into small pieces. Each has a “Source” (Sender) and “Destination” (Receiver) IP.
- Router = The Post Office. It doesn’t know the full path to every house in the world. It only knows the “Next Hop” (which truck to put the letter on).
- Subnet Mask = Zip Code. It defines your local neighborhood. It tells the device if the destination is “next door” (local) or “far away” (requires a router).
1. Addressing
1.1. IPv4 Addressing: The Deep Dive
What is it? A 32-bit binary number that identifies a network interface.
A. Structure
- Human Format: Four decimal numbers (0–255), e.g.,
192.168.1.7. - Binary Reality: Computers see 32 bits.
192.168.1.7=11000000.10101000.00000001.00000111.
- Interfaces vs. Devices: An IP identifies an interface, not a device. A laptop has two IPs: one for Wi-Fi, one for Ethernet. An IP address is a 32-bit number (in IPv4). It tells the internet where a packet came from and where it is going.
a. The Two Parts of an IP
Every IP is split into two logical sections:
- Network Part: Which “group” or network the device belongs to.
- Host Part: The specific device inside that network.
Example with /24 Mask: If your IP is
192.168.1.5with a/24mask:
192.168.1is the Network..5is the Host.
b. Why this split matters for Routers
Routers do not store the location of every single device in the world (that would make routing tables massive). Instead, they route based on the Network Part.
- Instead of storing:
192.168.1.5,192.168.1.6,192.168.1.7… - They store:
192.168.1.0/24 -> Send to Interface X. This keeps routing tables small, fast, and efficient.
B. Private vs. Public IPs (The “Apartment” Analogy)
- Public IP (Building Address): Assigned by your ISP. Unique across the entire internet. (e.g.,
103.45.22.12). - Private IP (Apartment Number): Used only inside your Local Area Network (LAN). Not reachable from the internet. (e.g.,
192.168.x.x).
C. NAT (Network Address Translation)
This is how private devices talk to the public internet using one Public IP.
- Outgoing: Your phone (
192.168.1.7) sends a request. The router replaces the source IP with its Public IP (103.45.22.12) and remembers the mapping in a NAT Table. - Incoming: The response returns to the router. The router checks the table and swaps the address back to
192.168.1.7.
1.1. DHCP: How Devices Get IPs (Deep Dive)
DHCP (Dynamic Host Configuration Protocol) automates IP assignment.
A. The DORA Process (Step-by-Step)
- D - Discover: Device broadcasts: “Is there a DHCP server out there?”
- O - Offer: Server replies: “I am here. You can have 192.168.1.10.”
- R - Request: Device replies: “Yes, I want that IP.”
- A - Acknowledge: Server confirms: “It’s yours. Here is your Subnet Mask, Gateway, and DNS.”
B. Lease & Renewal
- Lease: IPs are rented for a fixed time (e.g., 24 hours).
- Renewal: Halfway through, the device asks to keep the IP.
- Turning Wi-Fi Off/On: If the lease expires while you are offline, the router might give your IP to someone else. When you return, you get a new one.
C. Reservations (Static Lease)
You can force the router to give a specific device (like a printer) the same IP every time based on its MAC address.
1.3. Subnetting: The Logic & Math
The Subnet Mask answers the question: “Talk directly or go through the router?”
A. The Structure
- Format: Sequence of 1s (Network) and 0s (Host).
- Common Mask:
/24or255.255.255.0.- Binary:
11111111.11111111.11111111.00000000(First 24 bits are network).
- Binary:
B. The “Bitwise AND” Operation
To decide where to send a packet, a device performs a logical AND on the IP and Mask.
- Source:
192.168.1.7(Mask/24) → Network ID:192.168.1.0 - Destination:
192.168.1.50→ Network ID:192.168.1.0 - Result: Match = Same Subnet.
C. Direct vs. Router Communication
- Same Subnet (“Talk Directly”): Use ARP to find the MAC address and talk directly. (e.g., AirDrop, Printer, Chrome-cast).
- Different Subnet (“Via Router”): Send packet to Default Gateway. Router forwards it. (e.g., Internet browsing).
1.4. IPv6: The Future of Addressing (Deep Dive)
IPv6 was created because we ran out of IPv4 addresses.
A. Structure & Format
- Size: 128-bit (vs 32-bit for IPv4).
- Notation: Hexadecimal (0-9, A-F), separated by colons.
- Example:
2001:0db8:85a3:0000:0000:8a2e:0370:7334. - Shortening: You can replace strings of zeros with
::.2001:db8::1.
B. Massive Scale
- IPv4: ~4.3 billion addresses.
- IPv6: 340 Undecillion addresses ().
- Analogy: If IPv4 is a golf ball, IPv6 is the size of the sun. Every grain of sand on Earth could have its own IP.
C. No More NAT
- In a pure IPv6 world, NAT is unnecessary.
- Every device (phone, toaster, watch) gets its own Publicly Routable Global IP.
- This enables true End-to-End connectivity without a router having to translate addresses.
D. Why aren’t we fully there yet?
- Incompatibility: IPv4 and IPv6 cannot talk to each other directly.
- Cost: Updating old hardware (routers, ISP equipment) is expensive.
- NAT saved IPv4: NAT worked so well that the “emergency” of running out of addresses was delayed.
2. Routing
2.1. The IP Packet (The “Envelope”)
Data doesn’t travel the internet as one giant file. It is chopped into small, manageable pieces called Packets. An IP Packet has two main parts: the Header and the Payload.
A. The Header (The Metadata)
The header is at least 20 bytes and contains the “shipping instructions”:
- Source IP: Who sent this?
- Destination IP: Who should get this?
- Protocol: Is the data inside a TCP segment (reliable) or a UDP datagram (fast)?
- TTL (Time To Live): A “safety” number (usually 64 or 128). Every time a router handles the packet, the TTL drops by 1. If it hits 0, the packet is deleted. This prevents packets from looping forever if there’s a routing error.
- Checksum: A bit of math used to check if the header was corrupted during travel.
B. The Payload (The Actual Data)
This is what you are actually sending—like a piece of a WhatsApp message or a tiny slice of a YouTube video frame.
2.2. Routing (The “Post Office” Logic)
A router’s only job is to look at a packet’s Destination IP and decide which “exit door” (interface) to throw it out of.
A. The Routing Table
Every router has a “cheat sheet” called a Routing Table. It doesn’t store the path to every website; it stores Network Prefixes.
| Destination Network | Next Hop (Where to send) |
|---|---|
192.168.1.0/24 | Local LAN (Deliver directly to the device) |
17.0.0.0/8 | Gateway B (Apple’s network direction) |
0.0.0.0/0 | ISP Gateway (The “Default Route” for everything else) |
B. Hop-by-Hop Routing
Routers are “blind” to the total journey.
- Your Home Router sees a packet for Google. It says: “I don’t know where Google is, but my ISP does,” and sends it to the ISP.
- The ISP Router says: “I know Google is in that direction,” and sends it to a Core Router.
- Each jump from one router to another is called a Hop.
2.3. BGP: The Map of the Global Internet
How do routers in New York know that a 17.x.x.x address belongs to Apple in California? They use BGP (Border Gateway Protocol).
- Think of the internet as a collection of “Islands” (called Autonomous Systems or AS).
- ISPs and giant companies (Google, Facebook, Comcast) use BGP to tell each other: “Hey, if you have traffic for these 60,000 IP addresses, send them to me!”
- BGP is the “GPS” of the internet that keeps the global routing tables updated.
2.4. Encapsulation
This is a critical concept. As data moves down the layers, it gets “wrapped.”
- Layer 7 (Application): Your “Hello” message.
- Layer 4 (Transport): Wraps it in a TCP Header (adds Port numbers).
- Layer 3 (Network): Wraps that in an IP Header (adds IP addresses). This is now a Packet.
- Layer 2 (Data Link): Wraps that in an Ethernet Header (adds MAC addresses). This is now a Frame.
Layer 3 (The Router) only looks at the IP Header. It doesn’t care what the message is or what the port number is; it just looks at the Destination IP and moves the packet.