IP, Subnets & Routing
CIDR without tears: what an IP address really encodes, how subnets carve networks, and how NAT lets billions share.
An IP Address is Two Numbers Glued Together
An IPv4 address is 32 bits, split by a prefix length into a network part and a host part. 192.168.1.37/24 means: first 24 bits (192.168.1) name the network, last 8 bits (.37) name the host inside it. That /24 is CIDR notation, and it replaces the old class A/B/C system.
| CIDR | Addresses | Usable hosts | Feel |
|---|---|---|---|
/8 | 16.7 M | 16,777,214 | Huge — 10.0.0.0/8 |
/16 | 65,536 | 65,534 | Campus |
/24 | 256 | 254 | One office LAN |
/30 | 4 | 2 | Point-to-point link |
Private Ranges & NAT
Three ranges are reserved for private networks and never routed on the public internet: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16. Your whole house shares one public IP: the router does NAT — rewriting each outgoing packet’s private source to its public IP (tracking each flow in a table), and un-rewriting the replies.
How a Router Picks a Path
A router keeps a routing table: prefix → next hop. For each packet it picks the longest matching prefix — the most specific route wins. Your laptop has one too:
$ ip route default via 192.168.1.1 dev wlan0 # everything else → home router 192.168.1.0/24 dev wlan0 scope link # my LAN → deliver directly # which route would this destination use? $ ip route get 8.8.8.8 8.8.8.8 via 192.168.1.1 dev wlan0
Same-subnet destinations go direct (ARP finds the MAC); everything else goes to the default gateway. Across the internet, routers exchange routes with BGP — the protocol that glues 70,000+ networks into one.
10.0.0.0/8 → A and 10.1.0.0/16 → B, a packet to 10.1.2.3 goes to B — /16 is more specific than /8.IPv4 vs IPv6 — the 30-Second Version
| IPv4 | IPv6 | |
|---|---|---|
| Size | 32-bit — 4.3 B addresses (exhausted) | 128-bit — effectively unlimited |
| Format | 192.168.1.37 | 2001:db8::8a2e:370:7334 |
| NAT | Everywhere, by necessity | Unnecessary — end-to-end restored |
| Config | DHCP | SLAAC (self-configure from router advertisements) |
Adoption is ~40% and climbing; dual-stack (both at once) is the transition reality. For interviews: know why (address exhaustion) and what NAT stopped being needed for.
Interview Questions
What does /26 mean and how many hosts fit?
26 bits of network, 6 bits of host → 2⁶ = 64 addresses, 62 usable (network + broadcast reserved). It carves a /24 into four equal subnets.
How does NAT work, in one breath?
The router rewrites outgoing packets’ private src IP:port to its public IP and a chosen port, records the mapping, and reverses it for replies — many private hosts multiplexed onto one public address.
Longest prefix match — why longest?
More prefix bits = more specific route = closer knowledge of the destination. Specific overrides general, letting a default route (/0) coexist with precise internal routes.
Same subnet vs different subnet — what changes in delivery?
Same subnet: sender ARPs for the destination MAC and delivers directly at L2. Different subnet: sender forwards to its default gateway’s MAC, and routing (L3) takes over hop by hop.