TExcelTAD
Home › Computer Networking › Subnetting & CIDR

Subnetting & CIDR

Subnetting means splitting one block of IP addresses into smaller networks (subnets). An organisation given 10.20.0.0/16 might carve it into one subnet per building, per floor or per VLAN. It is a core skill for every network and cloud engineer, and it is mostly binary arithmetic.

Prefix length and block size

In CIDR notation /n means "the first n bits are the network". The remaining 32 − n host bits decide the size of the block:

Prefix Mask Addresses Usable hosts
/8 255.0.0.0 16,777,216 16,777,214
/16 255.255.0.0 65,536 65,534
/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
/29 255.255.255.248 8 6
/30 255.255.255.252 4 2
/31 255.255.255.254 2 2 (point-to-point links, RFC 3021)
/32 255.255.255.255 1 a single host route
python · try it yourself
import ipaddress

print(f"{'prefix':>6} {'mask':<16} {'wildcard':<16} {'addresses':>10}")
for prefix in [8, 16, 20, 24, 26, 28, 30, 32]:
    net = ipaddress.ip_network(f"0.0.0.0/{prefix}")
    print(f"/{prefix:<5} {str(net.netmask):<16} {str(net.hostmask):<16} {net.num_addresses:>10,}")
prefix mask             wildcard          addresses
/8     255.0.0.0        0.255.255.255    16,777,216
/16    255.255.0.0      0.0.255.255          65,536
/20    255.255.240.0    0.0.15.255            4,096
/24    255.255.255.0    0.0.0.255               256
/26    255.255.255.192  0.0.0.63                 64
/28    255.255.255.240  0.0.0.15                 16
/30    255.255.255.252  0.0.0.3                   4
/32    255.255.255.255  0.0.0.0                   1

The wildcard (host mask) is the inverse of the mask; Cisco ACLs and OSPF configuration use it.

Analysing an address

The classic exam question: given 172.16.77.130/26, find the network, broadcast, first and last host. The trick is the block size: 2^(32−26) = 64. Subnets of a /26 start at multiples of 64 in the last octet: 0, 64, 128, 192. 130 falls in the block starting at 128.

python · try it yourself
import ipaddress

def analyse(cidr):
    iface = ipaddress.ip_interface(cidr)
    net = iface.network
    hosts = list(net.hosts())
    print(f"{cidr}")
    print(f"  network   {net.network_address}")
    print(f"  broadcast {net.broadcast_address}")
    print(f"  hosts     {hosts[0]} - {hosts[-1]} ({len(hosts)} usable)")
    print(f"  mask      {net.netmask}")

analyse("172.16.77.130/26")
analyse("10.1.200.7/20")
analyse("192.168.5.33/30")
172.16.77.130/26
  network   172.16.77.128
  broadcast 172.16.77.191
  hosts     172.16.77.129 - 172.16.77.190 (62 usable)
  mask      255.255.255.192
10.1.200.7/20
  network   10.1.192.0
  broadcast 10.1.207.255
  hosts     10.1.192.1 - 10.1.207.254 (4094 usable)
  mask      255.255.240.0
192.168.5.33/30
  network   192.168.5.32
  broadcast 192.168.5.35
  hosts     192.168.5.33 - 192.168.5.34 (2 usable)
  mask      255.255.255.252

Let's verify the first one by hand with binary AND and OR, which is what the ipaddress module does internally:

python · try it yourself
ip = (172 << 24) | (16 << 16) | (77 << 8) | 130
prefix = 26
mask = (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF

def dotted(n):
    return ".".join(str((n >> s) & 255) for s in (24, 16, 8, 0))

network = ip & mask
broadcast = network | (~mask & 0xFFFFFFFF)
print("ip       ", f"{ip:032b}", dotted(ip))
print("mask     ", f"{mask:032b}", dotted(mask))
print("network  ", f"{network:032b}", dotted(network))
print("broadcast", f"{broadcast:032b}", dotted(broadcast))
ip        10101100000100000100110110000010 172.16.77.130
mask      11111111111111111111111111000000 255.255.255.192
network   10101100000100000100110110000000 172.16.77.128
broadcast 10101100000100000100110110111111 172.16.77.191

Splitting a network into subnets

Borrowing k host bits creates 2^k subnets. Splitting a /24 into four /26s borrows 2 bits:

python · try it yourself
import ipaddress

office = ipaddress.ip_network("192.168.10.0/24")
for i, subnet in enumerate(office.subnets(new_prefix=26), start=1):
    first, *_, last = subnet.hosts()
    print(f"subnet {i}: {subnet}  hosts {first} - {last}  broadcast {subnet.broadcast_address}")

print(len(list(office.subnets(prefixlen_diff=3))), "subnets with 3 borrowed bits")
subnet 1: 192.168.10.0/26  hosts 192.168.10.1 - 192.168.10.62  broadcast 192.168.10.63
subnet 2: 192.168.10.64/26  hosts 192.168.10.65 - 192.168.10.126  broadcast 192.168.10.127
subnet 3: 192.168.10.128/26  hosts 192.168.10.129 - 192.168.10.190  broadcast 192.168.10.191
subnet 4: 192.168.10.192/26  hosts 192.168.10.193 - 192.168.10.254  broadcast 192.168.10.255
8 subnets with 3 borrowed bits

How big a subnet do I need?

To fit h hosts you need 2^bits − 2 ≥ h, so the smallest prefix is 32 − bits:

python · try it yourself
import math

def prefix_for(hosts):
    bits = math.ceil(math.log2(hosts + 2))
    return 32 - bits

for need in [2, 10, 30, 31, 100, 500, 1000]:
    p = prefix_for(need)
    print(f"{need:>4} hosts -> /{p} ({2 ** (32 - p) - 2} usable)")
   2 hosts -> /30 (2 usable)
  10 hosts -> /28 (14 usable)
  30 hosts -> /27 (30 usable)
  31 hosts -> /26 (62 usable)
 100 hosts -> /25 (126 usable)
 500 hosts -> /23 (510 usable)
1000 hosts -> /22 (1022 usable)

Note the jump at 31 hosts: a /27 holds 30, so 31 needs a /26 with 62. Always remember the two reserved addresses (and in practice, one more for the gateway).

Is this host on my subnet?

Two hosts can talk directly only if they're in the same subnet. A frequent misconfiguration is a wrong mask on one host:

python · try it yourself
import ipaddress

pairs = [
    ("192.168.1.10/24", "192.168.1.200"),
    ("192.168.1.10/25", "192.168.1.200"),
    ("10.0.4.17/22", "10.0.7.250"),
    ("10.0.4.17/22", "10.0.8.1"),
]
for me, other in pairs:
    net = ipaddress.ip_interface(me).network
    verdict = "same subnet" if ipaddress.ip_address(other) in net else "different subnet: via router"
    print(f"{me:<16} -> {other:<14} {net}  {verdict}")
192.168.1.10/24  -> 192.168.1.200  192.168.1.0/24  same subnet
192.168.1.10/25  -> 192.168.1.200  192.168.1.0/25  different subnet: via router
10.0.4.17/22     -> 10.0.7.250     10.0.4.0/22  same subnet
10.0.4.17/22     -> 10.0.8.1       10.0.4.0/22  different subnet: via router

Tip: Cloud providers reserve extra addresses in each subnet. AWS, for example, keeps the first four and the last one, so a /28 gives you 11 usable addresses, not 14.

Test yourself

What is the broadcast address of 192.168.10.70/26?

  1. 192.168.10.63
  2. 192.168.10.127
  3. 192.168.10.255
  4. 192.168.10.71

Which prefix is the smallest subnet that fits 50 hosts?

  1. /25
  2. /26
  3. /27
  4. /28

How many /27 subnets fit in a /24?

  1. 4
  2. 8
  3. 16
  4. 32