IP Addressing & Subnetting

IPv4 and IPv6 addresses, CIDR notation, subnet masks and subnetting by hand, private ranges, NAT, DHCP and ICMP.

Intermediate 4 min readLesson 4 of 15 Interview Q&A

Every device on an IP network needs an IP address: the network-layer address routers use to deliver packets. Knowing how addresses and subnets work is essential for configuring cloud VPCs, firewalls, Kubernetes networks, and for debugging "why can't these two machines talk?".

IPv4 addresses

An IPv4 address is 32 bits, written as four decimal bytes: 192.168.10.37.

110000001010100000001010001001011921681037

Each address has two parts: a network prefix (which network) and a host part (which device on it). Where the split falls is given by the prefix length.

CIDR notation and subnet masks

192.168.10.0/24 means the first 24 bits are the network; the remaining 8 bits number the hosts. The same thing as a subnet mask is 255.255.255.0 (24 ones followed by zeros).

CIDRMaskAddressesUsable hostsTypical use
/8255.0.0.016,777,21616,777,214Huge private networks (10.0.0.0/8)
/16255.255.0.065,53665,534A cloud VPC
/24255.255.255.0256254A typical LAN or subnet
/28255.255.255.2401614A small subnet
/30255.255.255.25242Point-to-point links
/32255.255.255.25511 (a single host)Host routes, firewall rules

Usable hosts = 2^(32 − prefix) − 2, because the first address is the network address and the last is the broadcast address. (Cloud providers reserve a few more: AWS keeps 5 per subnet.)

Subnetting by hand

Question: which network is 172.16.45.130/26 in, and what's its range?

  1. /26 → the mask is 255.255.255.192; the last octet has 2 network bits, so each block is 2⁶ = 64 addresses.
  2. Blocks in the last octet start at 0, 64, 128, 192. 130 falls in the block starting at 128.
  3. Network address: 172.16.45.128. Broadcast: 172.16.45.191.
  4. Usable hosts: .129 – .190 (62 hosts).

The trick: block size = 256 − the mask's value in the interesting octet (256 − 192 = 64).

// Java has no built-in CIDR class; the arithmetic is simple:
int ip = (172 << 24) | (16 << 16) | (45 << 8) | 130;
int mask = -1 << (32 - 26);
int network = ip & mask;                  // 172.16.45.128
int broadcast = network | ~mask;          // 172.16.45.191

Special ranges

RangePurpose
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16Private (RFC 1918): not routed on the public Internet
127.0.0.0/8Loopback (127.0.0.1 = this machine)
169.254.0.0/16Link-local: assigned automatically when DHCP fails; also cloud metadata (169.254.169.254)
100.64.0.0/10Carrier-grade NAT (ISPs)
0.0.0.0"Any address" when binding a server; the default route as 0.0.0.0/0
224.0.0.0/4Multicast

NAT: many devices, one public address

IPv4 has only ~4.3 billion addresses, so homes and offices use private addresses internally, and the router performs network address translation: it rewrites the source IP (and port) of outgoing packets to its single public IP, remembers the mapping, and translates replies back.

203.0.113.7:40001 / :40002192.168.1.20:51000192.168.1.21:51000NAT routerpublic 203.0.113.7server :443

Consequence: devices behind NAT can make outbound connections but can't easily receive inbound ones. Peer-to-peer apps (video calls, games) need NAT traversal techniques: STUN, TURN and ICE (used by WebRTC).

DHCP: getting an address automatically

When a device joins a network, DHCP hands out an address in four steps — DORA: Discover (broadcast "any DHCP servers?"), Offer, Request, Acknowledge. The lease includes the IP address, subnet mask, default gateway, DNS servers and a lease duration.

IPv6

IPv6 addresses are 128 bits, written as eight groups of hex: 2001:0db8:0000:0000:0000:ff00:0042:8329, shortened to 2001:db8::ff00:42:8329 (leading zeros dropped, one run of zero groups replaced by ::).

IPv4IPv6
Size32 bits (~4.3 × 10⁹)128 bits (~3.4 × 10³⁸)
NATUbiquitousNot needed: every device can have a global address
ConfigurationDHCPSLAAC (self-configuration) or DHCPv6
HeaderVariable, with a checksumFixed 40 bytes, no checksum; extension headers
Typical subnet/24/64 (always for LANs)
Loopback127.0.0.1::1

Adoption is gradual: around 45% of Google's users connect over IPv6. Most networks run dual-stack (both at once).

ICMP

ICMP carries control and error messages for IP: "destination unreachable", "time exceeded" (TTL hit zero — what traceroute relies on), and echo request/reply (ping). Blocking all ICMP breaks path MTU discovery and causes mysterious hangs for large packets.

Key takeaways
  • IPv4 = 32 bits; CIDR /n = n network bits; hosts = 2^(32 − n) − 2.
  • Block size = 256 − mask octet; find the block containing the address.
  • Private ranges: 10/8, 172.16/12, 192.168/16; NAT maps them to a public IP.
  • DHCP assigns addresses (DORA); ICMP reports errors and powers ping and traceroute.
  • IPv6 = 128 bits, /64 subnets, no NAT needed.
Interview questions
QHow many usable hosts are in a /27, and what are the subnets of 192.168.1.0/24 split into /27s?medium

2⁵ − 2 = 30 usable hosts. There are 8 subnets with block size 32: .0, .32, .64, .96, .128, .160, .192 and .224 (each /27).

QTwo hosts on the same switch can't ping each other. What would you check?medium

That they're in the same subnet (IP address and mask), that ARP resolves (ip neigh), the cabling and VLAN membership, and host firewalls blocking ICMP. If they're in different subnets, traffic must go through a router or gateway.