Sub topic 5
Variable-length subnet masks
This chapter assumes you can convert between binary and decimal and can subnet with a fixed mask. It also assumes you know CIDR prefix notation such as /26. Variable-length subnet masks (VLSM) let you cut one address block into subnets of different sizes, so each subnet gets a mask that fits its host count.
Why one mask is not enough
With fixed-length subnetting, every subnet uses the same mask. Suppose you own 192.168.10.0/24 and need networks for 100, 50, 25 and 2 hosts. The largest network needs a /25, which has 126 usable addresses. Splitting the /24 into /25s gives only two subnets, not four. Using /26 for all four subnets gives four networks, but each holds only 62 hosts, so the 100-host network does not fit.
VLSM fixes this by giving each requirement its own prefix length. Small links no longer waste a whole /25.
Sizing each subnet
A subnet with h host bits has 2^h addresses. Two are reserved, the network address and the broadcast address, so it has 2^h - 2 usable hosts. To size a subnet, find the smallest h where 2^h - 2 is at least the required host count. The prefix is then 32 - h.
| Hosts needed | Host bits | Block size | Prefix | Usable hosts |
|---|---|---|---|---|
| 100 | 7 | 128 | /25 | 126 |
| 50 | 6 | 64 | /26 | 62 |
| 25 | 5 | 32 | /27 | 30 |
| 2 | 2 | 4 | /30 | 2 |
The allocation procedure
- Sort the requirements from largest to smallest.
- Give each requirement the next free block that is aligned to its own size. A block of size 64 must start at an address divisible by 64.
- Continue from the end of the previous block.
- Check that the last block ends inside the parent block.
Placing large blocks first keeps every block aligned without gaps. The sizes are all powers of two, and each block's start is a multiple of every smaller block size.
For the example, the result is:
- A (100 hosts):
192.168.10.0/25, covering .0 to .127. - B (50 hosts):
192.168.10.128/26, covering .128 to .191. - C (25 hosts):
192.168.10.192/27, covering .192 to .223. - D (2 hosts):
192.168.10.224/30, covering .224 to .227.
The addresses .228 to .255 remain free for later subnets.
Doing it in code
This script applies the same procedure. It uses (hosts + 1).bit_length() to compute the host bits, which avoids floating-point logarithms. That expression equals the smallest h with 2^h >= hosts + 2.
import ipaddress
def vlsm(block, requirements):
pool = ipaddress.ip_network(block)
next_addr = int(pool.network_address)
end = int(pool.broadcast_address) + 1
plan = []
for name, hosts in sorted(requirements.items(), key=lambda kv: -kv[1]):
host_bits = (hosts + 1).bit_length()
size = 2 ** host_bits
if next_addr % size:
next_addr += size - next_addr % size
if next_addr + size > end:
raise ValueError(f"no room left for {name}")
net = ipaddress.ip_network(f"{ipaddress.ip_address(next_addr)}/{32 - host_bits}")
plan.append((name, net, net.num_addresses - 2))
next_addr += size
return plan
for name, net, usable in vlsm("192.168.10.0/24", {"A": 100, "B": 50, "C": 25, "D": 2}):
print(name, net, usable)It prints A 192.168.10.0/25 126, B 192.168.10.128/26 62, C 192.168.10.192/27 30 and D 192.168.10.224/30 2.
Common errors
- Sizing by host count alone. A requirement of 30 hosts fits a
/27exactly (30 usable). A requirement of 31 hosts needs a/26, because the network and broadcast addresses use two of the addresses. - Allocating smallest first. If D takes
.0/30, a/25for A can no longer start at.0. A must then start at.128, and the space left over cannot hold the other subnets. - Misaligned starts. A
/26cannot begin at.100. Its start must be a multiple of 64. - Overlap. Two blocks overlap when one lies inside the other. Check each new block's start against the end of the previous one.
- Forgetting the point-to-point link. A router-to-router link needs two host addresses, so it takes a
/30. Wasting a/24on it is the mistake VLSM exists to prevent. (Modern practice also allows a/31on such links, but GATE questions usually expect/30.)
Related GATE patterns
Questions on VLSM usually take one of three forms. The first asks for the prefix length of a subnet that holds n hosts. The second asks for the first or last usable address of the k-th subnet. The third asks how many addresses remain unallocated. For the third, subtract the allocated block sizes from the parent size. In the example, 256 - (128 + 64 + 32 + 4) = 28, which matches the free range .228 to .255.
Exercise
You must split 10.0.0.0/24 into subnets for 60, 30, 10 and 2 hosts using VLSM, largest first. Give the prefix and the network address of each subnet.
Show answer
Sizes are 64 (/26), 32 (/27), 16 (/28) and 4 (/30). The 30-host subnet fits a /27 exactly, since a /27 has 30 usable addresses. The allocation is 10.0.0.0/26 for 60 hosts, 10.0.0.64/27 for 30 hosts, 10.0.0.96/28 for 10 hosts and 10.0.0.112/30 for 2 hosts. This uses 116 addresses, so 140 remain, starting at 10.0.0.116.