Sub topic 10
Variable-length subnet masking
Earlier chapters divided one block into equal subnets. Real networks rarely need equal subnets. A office LAN may need 100 hosts, a server segment 50, and a router-to-router link only 2. Variable-length subnet masking (VLSM) lets you give each subnet the prefix length it needs and no more.
This chapter assumes you can compute block size, network address, broadcast address, and usable host count for a given prefix. It also assumes you can convert between dotted-decimal masks and prefix lengths.
Why equal subnets waste addresses
Suppose you have 192.168.1.0/24 and four segments with these host requirements: 100, 50, 20, and 2. The largest segment needs 100 hosts, so an equal split must use /25 (126 usable hosts) for every subnet. A /24 holds only two /25 blocks, so you cannot even fit four segments. Moving to /26 fits four subnets, but 62 usable hosts is too few for the 100-host segment.
With VLSM you choose a different mask for each segment:
- 100 hosts fit in a /25, which has 126 usable hosts.
- 50 hosts fit in a /26, which has 62 usable hosts.
- 20 hosts fit in a /27, which has 30 usable hosts.
- 2 hosts fit in a /30, which has 2 usable hosts.
Together these use 128 + 64 + 32 + 4 = 228 of the 256 addresses, and all four fit.
Routing requirement
VLSM works only if the routing protocol carries the prefix length with each route. Classless protocols do this: RIPv2, OSPF, EIGRP, IS-IS, and BGP. RIPv1 does not, so it cannot represent a /25 and a /30 inside the same network. Static routes and modern hosts are always classless.
The VLSM procedure
Follow the same steps every time:
- List each segment and its required number of hosts.
- For each, find the smallest host-bit count
hsuch that 2^h − 2 ≥ hosts. The prefix is 32 − h. - Sort the segments from largest to smallest.
- Starting at the beginning of the pool, assign each segment the next free address that is aligned to its block size.
- Move the "next free" pointer to the address just after the block you assigned.
- Check that the last block ends inside the pool.
Step 2 uses "hosts + 2" because every ordinary subnet loses the network and broadcast addresses. Edge case: 126 hosts fit a /25, but 127 hosts need a /24, because a /25 has only 126 usable addresses.
Why largest first
A subnet's network address must be a multiple of its block size. A /25 must start at .0 or .128. A /26 must start at .0, .64, .128, or .192. If you place small blocks first, you push the pointer to an address that is not aligned for a big block. You then skip forward to the next boundary, and the skipped addresses are wasted. Allocating largest first keeps every pointer position aligned for all the smaller blocks that follow, because each block size divides every larger one.
Worked example
The plan for 192.168.1.0/24:
| Segment | Hosts needed | Prefix | Mask | Network | Usable range | Broadcast |
|---|---|---|---|---|---|---|
| A | 100 | /25 | 255.255.255.128 | 192.168.1.0 | .1 to .126 | 192.168.1.127 |
| B | 50 | /26 | 255.255.255.192 | 192.168.1.128 | .129 to .190 | 192.168.1.191 |
| C | 20 | /27 | 255.255.255.224 | 192.168.1.192 | .193 to .222 | 192.168.1.223 |
| D | 2 | /30 | 255.255.255.252 | 192.168.1.224 | .225 to .226 | 192.168.1.227 |
Work through the pointer for each step:
- A starts at .0 and has 128 addresses, so the pointer moves to .128.
- B starts at .128, which is a multiple of 64. It has 64 addresses, so the pointer moves to .192.
- C starts at .192, which is a multiple of 32. It has 32 addresses, so the pointer moves to .224.
- D starts at .224, which is a multiple of 4. It has 4 addresses, so the pointer moves to .228.
The addresses .228 to .255 are unallocated. That is 28 addresses, still available for growth.
Each split halves the remaining space, which is why the sizes are always powers of two.
Automating the allocation
This script performs the same procedure. It sorts by requirement, computes the host bits, and builds each subnet at the pointer position.
import ipaddress
import math
pool = ipaddress.ip_network("192.168.1.0/24")
needs = {"A": 100, "B": 50, "C": 20, "D": 2}
next_addr = int(pool.network_address)
for name, hosts in sorted(needs.items(), key=lambda kv: -kv[1]):
host_bits = math.ceil(math.log2(hosts + 2)) # +2 for network and broadcast
net = ipaddress.ip_network((next_addr, 32 - host_bits))
print(name, net)
next_addr += net.num_addresses
# A 192.168.1.0/25
# B 192.168.1.128/26
# C 192.168.1.192/27
# D 192.168.1.224/30The ip_network constructor raises ValueError if the address has host bits set for the given prefix. In this script that would signal an alignment bug, so the check works in your favor. The script does not check that the result stays inside the pool. If the requirements are too large, next_addr runs past the end of the pool without any error. Add a test such as net.subnet_of(pool) before accepting an allocation.
Verifying a plan for overlaps
Overlap is the most common VLSM mistake. Two subnets overlap when one contains the other or when their ranges cross. The overlaps method checks this directly.
import ipaddress
plan = [ipaddress.ip_network(n) for n in (
"192.168.1.0/25",
"192.168.1.128/26",
"192.168.1.192/27",
"192.168.1.224/30",
)]
pool = ipaddress.ip_network("192.168.1.0/24")
for i, a in enumerate(plan):
assert a.subnet_of(pool), f"{a} is outside the pool"
for b in plan[i + 1:]:
assert not a.overlaps(b), f"{a} overlaps {b}"
print("plan is valid")
print(sum(n.num_addresses for n in plan)) # 228Common errors
- Unaligned start address. Writing 192.168.1.100/26 as a network is wrong. A /26 must start on a multiple of 64, so the block containing .100 starts at .64.
- Forgetting the two reserved addresses. A requirement of 30 hosts does not fit a /27 if you also need to count the router interface. Count every device, including gateways, in the requirement.
- Allocating smallest first. This leaves gaps that no later block can use, and in a tight pool the last large block may not fit at all.
- Overlapping ranges. A /25 at .0 covers .0 to .127. Placing a /27 at .96 inside that range creates an overlap, even though both networks look valid on their own.
- Ignoring growth. A /30 for a link that later needs a third device forces a renumbering. Leave free space beside segments likely to grow, or choose the next larger prefix up front.
Point-to-point links
Router-to-router links need only two addresses. A /30 gives exactly two usable hosts and wastes two addresses on the network and broadcast values. RFC 3021 allows a /31 on point-to-point links, with both addresses usable and no broadcast address. A /31 saves half the space per link. Check that your equipment supports it before using it, because some older devices reject a /31 on an interface.
Using the leftover space
After the plan above, 192.168.1.228 through .255 is free. You can recover it as aligned blocks by always choosing the largest block that fits at the pointer:
- 192.168.1.228/30 covers .228 to .231 (the pointer .228 is a multiple of 4, but not of 8).
- 192.168.1.232/29 covers .232 to .239.
- 192.168.1.240/28 covers .240 to .255.
These three blocks hold 4 + 8 + 16 = 28 addresses, matching the free space exactly. Future segments can come from them without disturbing the existing plan.
Summary of the key rules
- Choose each prefix from the smallest block that holds the host count plus two.
- Allocate from largest to smallest.
- Align each network address to a multiple of its block size.
- Verify with code that blocks stay inside the pool and do not overlap.
- Use a classless routing protocol so the different prefix lengths are advertised correctly.
Exercise
You have 10.0.5.0/24 and need subnets for 60 hosts, 29 hosts, 12 hosts, and a router link of 2 hosts. Give the prefix and network address of each, allocating largest first.
Show answer
60 hosts needs 62 usable addresses, so /26: 10.0.5.0/26 (covers .0 to .63). 29 hosts fits a /27 (30 usable): 10.0.5.64/27 (covers .64 to .95). 12 hosts fits a /28 (14 usable): 10.0.5.96/28 (covers .96 to .111). The 2-host link fits a /30: 10.0.5.112/30 (covers .112 to .115). Each start address is a multiple of its block size (64, 32, 16, 4).
Exercise
A colleague assigns 192.168.1.0/25 to one segment and 192.168.1.96/27 to another. Is the plan valid? Explain.
Show answer
No. The /25 covers 192.168.1.0 to 192.168.1.127, and the /27 covers .96 to .127. The /27 lies entirely inside the /25, so the two subnets overlap. Hosts in the overlapping range would be ambiguous for routing. Moving the /27 to a free aligned block outside the /25, such as 192.168.1.128/27, fixes it.