Subnetting

Intermediate

An organisation with 192.168.1.0/24 may want separate networks for staff, students and servers. Subnetting splits one network into smaller ones by moving the line between the network part and the host part to the right.

The mask

The subnet mask is 32 bits with 1s for the network part and 0s for the host part. /26 is twenty-six 1s and six 0s:

11111111.11111111.11111111.11000000 = 255.255.255.192

From an address and its mask, everything follows:

  • network address = address AND mask (all host bits 0);
  • broadcast address = the network address with all host bits 1;
  • hosts: everything between them, so a network with h host bits has 2^h − 2 usable addresses.
def to_number(ip):
    n = 0
    for part in ip.split("."):
        n = n * 256 + int(part)
    return n

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

ip, prefix = "192.168.1.10", 26
mask = (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF
network = to_number(ip) & mask
broadcast = network | (~mask & 0xFFFFFFFF)
print("mask:", to_ip(mask))
print("network:", to_ip(network))
print("broadcast:", to_ip(broadcast))
print("hosts:", to_ip(network + 1), "to", to_ip(broadcast - 1), "=", 2 ** (32 - prefix) - 2)
Open in compiler

Try your own addresses here. The blue bits are the network part, the yellow bits the host part:

Splitting a network

To split a network into n equal subnets, borrow b host bits for the network, where 2^b ≥ n. A /24 split in 4 borrows 2 bits and becomes four /26 networks of 64 addresses each (62 hosts). Each subnet starts where the last one ended:

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

start = (192 << 24) | (168 << 16) | (1 << 8)    # 192.168.1.0/24
wanted = 4
borrow = 0
while 2 ** borrow < wanted:
    borrow += 1
prefix = 24 + borrow
size = 2 ** (32 - prefix)
for k in range(2 ** borrow):
    net = start + k * size
    print(to_ip(net) + "/" + str(prefix), "hosts", to_ip(net + 1), "to", to_ip(net + size - 2))
Open in compiler

Planning by size

Subnets do not have to be equal. With VLSM (variable length subnet masks), give each group the smallest block that fits it: 50 staff need 6 host bits (/26, 62 hosts), 10 servers need 4 (/28, 14 hosts), and a link between two routers needs only /30 (2 hosts). Place the biggest blocks first so every block starts on a multiple of its size.

Common mistakes

  • Forgetting the "− 2": the network address and the broadcast address cannot be given to a host.
  • Borrowing too few bits: 3 subnets need 2 bits (4 subnets), not 1 (2 subnets).
  • Starting a subnet at an address that is not a multiple of its size, such as 192.168.1.32/26.

Quick check

1. What is the mask for /27?
2. How many usable hosts does a /28 have?
3. What is the network address of 172.16.5.200/24?
4. A /24 must be split into 6 equal subnets. What prefix do they get?

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.