IP Addressing, Subnetting & Routing
IPv4 and IPv6 addressing, CIDR and subnetting worked step by step (including VLSM), private ranges, ARP, DHCP, NAT/PAT, longest-prefix-match routing, OSPF vs BGP, ICMP/ping/traceroute, MTU and fragmentation, and cloud VPC networking.
IPv4 addresses
A 32-bit number written as four octets: 203.0.113.42. It has a network part and a host part, separated by the prefix length (CIDR notation /24) or subnet mask (255.255.255.0).
Special ranges
| Range | Purpose |
|---|---|
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 | private (RFC 1918) |
100.64.0.0/10 | carrier-grade NAT shared space |
127.0.0.0/8 | loopback (localhost) |
169.254.0.0/16 | link-local (no DHCP); cloud metadata service at 169.254.169.254 |
0.0.0.0/0 | "everything" — the default route |
224.0.0.0/4 | multicast |
255.255.255.255 | limited broadcast |
Subnetting, step by step
Formulas for prefix length p:
- addresses in the subnet = 2^(32 − p)
- usable hosts = 2^(32 − p) − 2 (network + broadcast addresses; cloud providers reserve more, e.g. AWS reserves 5 per subnet)
- block size in the "interesting" octet = 256 − mask value in that octet
| Prefix | Mask | Addresses | Usable hosts |
|---|---|---|---|
| /24 | 255.255.255.0 | 256 | 254 |
| /25 | 255.255.255.128 | 128 | 126 |
| /26 | 255.255.255.192 | 64 | 62 |
| /27 | 255.255.255.224 | 32 | 30 |
| /28 | 255.255.255.240 | 16 | 14 |
| /30 | 255.255.255.252 | 4 | 2 (point-to-point links) |
| /16 | 255.255.0.0 | 65,536 | 65,534 |
Example 1: which subnet is 192.168.10.77/26 in?
In binary: 77 = 01001101. A /26 keeps the first 2 bits of the last octet (01) → network 01000000 = 64; broadcast 01111111 = 127.
Example 2: VLSM — carve 10.0.5.0/24 for 100, 50 and 20 hosts
Allocate the largest first:
| Need | Smallest fitting prefix | Subnet | Usable range | Broadcast |
|---|---|---|---|---|
| 100 hosts | /25 (126) | 10.0.5.0/25 | .1 – .126 | .127 |
| 50 hosts | /26 (62) | 10.0.5.128/26 | .129 – .190 | .191 |
| 20 hosts | /27 (30) | 10.0.5.192/27 | .193 – .222 | .223 |
| spare | /27 | 10.0.5.224/27 | .225 – .254 | .255 |
Example 3: are two hosts on the same subnet?
10.1.4.20/22 and 10.1.7.200/22: /22 → block 4 in the third octet (blocks 0, 4, 8, …). Both third octets (4 and 7) are in the 4–7 block → same subnet 10.1.4.0/22 (hosts 10.1.4.1 – 10.1.7.254).
import ipaddress
net = ipaddress.ip_network("192.168.10.77/26", strict=False)
print(net, net.netmask, net.broadcast_address, net.num_addresses - 2)
# 192.168.10.64/26 255.255.255.192 192.168.10.127 62
print(list(ipaddress.ip_network("10.0.5.0/24").subnets(new_prefix=26)))
IPv6 in brief
- 128-bit addresses:
2001:0db8:0000:0000:0000:ff00:0042:8329→2001:db8::ff00:42:8329. - Typical allocation: /48 to a site, /64 per subnet (SLAAC requires /64).
- Types: global unicast (
2000::/3), link-local (fe80::/10), unique local (fc00::/7), multicast (ff00::/8), loopback::1. - No broadcast; Neighbor Discovery (ICMPv6) replaces ARP; routers don't fragment.
ARP and DHCP
ARP then resolves the gateway's IP to its MAC so the host can send frames off-subnet.
NAT and PAT
Routing
- Longest prefix match chooses the most specific route.
- Interior protocols (within an organisation): OSPF, IS-IS (link-state: every router knows the topology and runs Dijkstra), RIP (distance vector, legacy).
- Exterior: BGP between autonomous systems (ISPs, clouds, large companies). Path-vector, policy-driven. BGP misconfigurations or hijacks have taken down large parts of the internet (Facebook's 2021 outage withdrew its own routes, making its DNS unreachable).
- Anycast: the same IP announced from many locations; BGP sends users to the nearest — used by DNS root servers, CDNs and DDoS protection.
ICMP, ping and traceroute
pingsends ICMP Echo Request/Reply → reachability and RTT.tracerouteexploits TTL expiry to list hops.- ICMP "Destination Unreachable", "Time Exceeded", "Fragmentation Needed" carry essential signals — don't block all ICMP.
MTU and fragmentation
Ethernet MTU 1500 B → TCP payload (MSS) ≈ 1460 B. Tunnels (VPN, VXLAN, GRE, IPsec) add headers and shrink the effective MTU. Blocked ICMP → PMTUD black holes: TCP handshakes and small requests succeed but large transfers hang.
Cloud networking (VPC) — what system designs assume
| Concept | Meaning |
|---|---|
| VPC / VNet | your private network in the cloud, with a CIDR like 10.0.0.0/16 |
| Subnets | per availability zone; public (route to an internet gateway) or private (outbound via NAT gateway) |
| Route tables | where traffic goes (local, IGW, NAT GW, peering, transit gateway) |
| Security groups | stateful, per-instance allow rules |
| Network ACLs | stateless, per-subnet allow/deny rules |
| Load balancers | public LB in public subnets → app servers in private subnets |
| Private endpoints | reach managed services (S3, databases) without the internet |
| Peering / transit gateway / VPN / Direct Connect | connect VPCs and on-prem networks |
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.