IP Addressing & Subnetting
IPv4 and IPv6 addresses, CIDR notation, subnet masks and subnetting by hand, private ranges, NAT, DHCP and ICMP.
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.
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).
| CIDR | Mask | Addresses | Usable hosts | Typical use |
|---|---|---|---|---|
| /8 | 255.0.0.0 | 16,777,216 | 16,777,214 | Huge private networks (10.0.0.0/8) |
| /16 | 255.255.0.0 | 65,536 | 65,534 | A cloud VPC |
| /24 | 255.255.255.0 | 256 | 254 | A typical LAN or subnet |
| /28 | 255.255.255.240 | 16 | 14 | A small subnet |
| /30 | 255.255.255.252 | 4 | 2 | Point-to-point links |
| /32 | 255.255.255.255 | 1 | 1 (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?
- /26 → the mask is 255.255.255.192; the last octet has 2 network bits, so each block is 2⁶ = 64 addresses.
- Blocks in the last octet start at 0, 64, 128, 192. 130 falls in the block starting at 128.
- Network address: 172.16.45.128. Broadcast: 172.16.45.191.
- 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.191import ipaddress
net = ipaddress.ip_interface("172.16.45.130/26").network
net # IPv4Network('172.16.45.128/26')
net.broadcast_address # 172.16.45.191
net.num_addresses # 64
list(ipaddress.ip_network("10.0.0.0/16").subnets(new_prefix=18)) # split into 4
ipaddress.ip_address("10.0.3.7") in ipaddress.ip_network("10.0.0.0/16") # Trueconst toInt = (ip: string) => ip.split(".").reduce((a, o) => (a << 8) | +o, 0) >>> 0;
const mask = (bits: number) => (bits === 0 ? 0 : (~0 << (32 - bits)) >>> 0);
const inCidr = (ip: string, cidr: string) => {
const [net, bits] = cidr.split("/");
return ((toInt(ip) & mask(+bits)) >>> 0) === toInt(net);
};
inCidr("10.0.3.7", "10.0.0.0/16"); // trueSpecial ranges
| Range | Purpose |
|---|---|
10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 | Private (RFC 1918): not routed on the public Internet |
127.0.0.0/8 | Loopback (127.0.0.1 = this machine) |
169.254.0.0/16 | Link-local: assigned automatically when DHCP fails; also cloud metadata (169.254.169.254) |
100.64.0.0/10 | Carrier-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/4 | Multicast |
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.
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 ::).
| IPv4 | IPv6 | |
|---|---|---|
| Size | 32 bits (~4.3 × 10⁹) | 128 bits (~3.4 × 10³⁸) |
| NAT | Ubiquitous | Not needed: every device can have a global address |
| Configuration | DHCP | SLAAC (self-configuration) or DHCPv6 |
| Header | Variable, with a checksum | Fixed 40 bytes, no checksum; extension headers |
| Typical subnet | /24 | /64 (always for LANs) |
| Loopback | 127.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.
- 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.
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.