Learn › Part 1 ยท Networking foundations

IPv4 addressing, subnetting and CIDR

Classful addressing, casting types, subnet masks, bitwise AND routing, CIDR validity rules and worked subnetting.

IPv4 basics

An IPv4 address is 32 bits, written as four octets, giving 232 ≈ 4.29 billion addresses. Every address splits into a network ID (used by routers) and a host ID (the interface inside that network).

Classful addressing (1981–1993)

The space was split by successive halving on the leading bits: 232 = 231 (A) + 230 (B) + 229 (C) + 228 (D) + 228 (E).

ClassLeading bitsFirst octetDefault maskNetworksUsable hosts each
A00–127/827 = 128224 − 2 = 16,777,214
B10128–191/16214 = 16,38465,534
C110192–223/24221 = 2,097,152254
D1110224–239Multicast
E1111240–255Experimental

Casting and special addresses

  • Unicast: one sender to one receiver.
  • Limited broadcast 255.255.255.255: every host on the local network; routers never forward it.
  • Directed broadcast: a remote network's ID with all host bits set to 1 (e.g. 20.255.255.255 for 20.0.0.0/8); routed to that network, then broadcast there.
  • Network address: all host bits 0; identifies the network and is not assigned to a host.

Subnetting formulas

Borrow n host bits ⇒ 2n subnets. With h host bits left: 2h addresses, 2h − 2 usable hosts.

Network ID = destination IP AND mask. Broadcast = network ID with all host bits set to 1.

Worked example: four departments from 192.168.1.0/24

Need 4 subnets ⇒ 22 = 4, borrow 2 bits ⇒ /26, mask 255.255.255.192, 6 host bits ⇒ 64 addresses, 62 usable.

BitsNetworkHostsBroadcast
00192.168.1.0.1–.62.63
01192.168.1.64.65–.126.127
10192.168.1.128.129–.190.191
11192.168.1.192.193–.254.255

A packet for 192.168.1.70: 70 = 01000110, 192 = 11000000, AND = 01000000 = 64 ⇒ network 192.168.1.64, so it leaves on the second subnet's interface. Routers store networks, not hosts, which is why they mask instead of looking up the host address.

CIDR

Classless Inter-Domain Routing (1993) allows any prefix from /1 to /32, so blocks fit the need (an organisation needing 1,000 hosts gets a /22 of 1,024 addresses instead of a Class B of 65,536). A range is a valid CIDR block only if:

  1. the addresses are contiguous;
  2. the count is a power of two (2n);
  3. the first address is divisible by the block size (all host bits zero).

Worked example

100.1.2.32–100.1.2.47: 16 addresses = 24, and 32 / 16 = 2 exactly ⇒ valid, 100.1.2.32/28. By contrast 100.1.2.40–.55 also holds 16 addresses but 40 is not a multiple of 16, so it is not a block.

Aggregation follows the same rules: 200.1.4.0/24 to 200.1.7.0/24 summarise to 200.1.4.0/22, but 200.1.2.0 to 200.1.5.0 cannot, because 2 is not a multiple of 4.

Common pitfalls

  • Forgetting the −2 for network and broadcast addresses.
  • Assuming octet boundaries: in 10.45.77.200/20 the network is 10.45.64.0, not 10.45.77.0.
  • Counting subnets from host bits (or the reverse): a /24 holds 16 /28 subnets of 14 usable hosts each.
  • In VLSM, allocate the largest subnets first so blocks stay aligned and do not overlap.