Networking · IP, Subnets & Routing

IP, Subnets & Routing

CIDR without tears: what an IP address really encodes, how subnets carve networks, and how NAT lets billions share.

Networking Basics → Interview
01

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.

192 . 168 . 1 network — 24 bits . 37 host — 8 bits /24 → 2⁸ = 256 addresses → 254 usable hosts (.0 = network address, .255 = broadcast)
CIDRAddressesUsable hostsFeel
/816.7 M16,777,214Huge — 10.0.0.0/8
/1665,53665,534Campus
/24256254One office LAN
/3042Point-to-point link
02

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.

NAT is why your laptop can call any server, but servers can’t call your laptop — there’s no table entry until you initiate. It broke peer-to-peer, spawned STUN/TURN for video calls, and is half the reason IPv6 exists (no NAT needed: everyone gets real addresses).
03

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:

bash — routing table
$ 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.

Longest-prefix-match is an interview favourite: given routes 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.
04

IPv4 vs IPv6 — the 30-Second Version

IPv4IPv6
Size32-bit — 4.3 B addresses (exhausted)128-bit — effectively unlimited
Format192.168.1.372001:db8::8a2e:370:7334
NATEverywhere, by necessityUnnecessary — end-to-end restored
ConfigDHCPSLAAC (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.

05

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.

Quick Quiz

1. Usable hosts in a /24:
2. Which is a private range?
3. Routes 10.0.0.0/8→A and 10.1.0.0/16→B. Packet to 10.1.2.3 goes to…
4. NAT rewrites…
5. The protocol gluing networks across the internet: