Sub topic 2
Subnetting and Subnet Masks
Prerequisites and working model
This chapter assumes that you can convert between binary and decimal, understand IPv4 addresses as 32-bit values, and know that a router forwards packets between networks.
Subnetting divides one IPv4 network into smaller networks. A subnet mask identifies which bits represent the network and which bits represent hosts. In CIDR notation, the prefix length after the slash gives the number of network bits. For example, /24 means 24 network bits and 8 host bits.
The number of addresses in a subnet is:
\[ 2^{\text{host bits}} \]
For an ordinary subnet, one address identifies the network and one identifies the broadcast address. Therefore:
\[ \text{usable hosts} = 2^{\text{host bits}} - 2 \]
Reading a subnet mask
A /26 prefix leaves \(32 - 26 = 6\) host bits. It therefore contains \(2^6 = 64\) addresses and supports 62 usable host addresses.
The dotted-decimal mask for /26 is 255.255.255.192. The last octet has two network bits and six host bits.
| Prefix | Mask | Total addresses | Usable hosts |
|---|---|---|---|
| `/24` | `255.255.255.0` | 256 | 254 |
| `/26` | `255.255.255.192` | 64 | 62 |
| `/30` | `255.255.255.252` | 4 | 2 |
To find subnet boundaries, calculate the block size:
\[ \text{block size} = 256 - \text{mask value in the changing octet} \]
For /26, the block size is \(256 - 192 = 64\). Inside 192.168.1.0/24, the subnet network addresses are therefore .0, .64, .128, and .192.
import ipaddress
net = ipaddress.ip_network("192.168.1.0/24")
for subnet in net.subnets(new_prefix=26):
hosts = subnet.num_addresses - 2
print(subnet, hosts)Fixed-length subnetting
Fixed-length subnetting gives every subnet the same prefix length. Borrowing two host bits from a /24 changes it to /26, producing four equal subnets:
192.168.1.0/26: usable range.1–.62, broadcast.63192.168.1.64/26: usable range.65–.126, broadcast.127192.168.1.128/26: usable range.129–.190, broadcast.191192.168.1.192/26: usable range.193–.254, broadcast.255
Common errors
- Treating the prefix length as host bits instead of network bits.
- Using
2^has the usable-host count without subtracting two. - Choosing a subnet boundary that is not aligned with the block size.
- Confusing the subnet mask with the subnet's broadcast address.
Exercise
What are the network address, broadcast address, and usable host range for 172.16.5.70/27?
Show answer
The block size is 32. The address belongs to 172.16.5.64/27, with broadcast 172.16.5.95 and usable range 172.16.5.65–172.16.5.94.