Sub topic 3
Subnetting and subnet masks
This chapter assumes you can convert between binary and decimal, and that you know an IPv4 address is a 32-bit number written as four decimal bytes. It also assumes you know the earlier classful chapter's idea that an address splits into a network part and a host part.
What a subnet mask does
A subnet mask is a 32-bit value with 1s in the positions that identify the network and 0s in the positions that identify the host. The 1s are always contiguous and on the left. A router or host applies the mask with a bitwise AND:
- Network address = IP address AND mask.
- Broadcast address = network address with all host bits set to 1.
- Usable hosts = 2^h − 2, where h is the number of host bits. The two reserved addresses are the network address and the broadcast address.
Subnetting means borrowing bits from the host part and using them as extra network bits. Borrowing s bits gives 2^s subnets, and each subnet has 2^(h−s) addresses. Prefix notation such as /26 simply counts the 1s in the mask.
Worked example
Find the subnet of 192.168.1.130 with mask 255.255.255.192.
- 192 in binary is 11000000, so the prefix is /26. That leaves 6 host bits.
- Only the last octet is "interesting", because it is the only one that is neither 255 nor 0.
- The block size is 256 − 192 = 64. The subnets start at 0, 64, 128 and 192.
- 130 lies in the block that starts at 128.
- Network address: 192.168.1.128. Broadcast address: 192.168.1.191. Usable range: 192.168.1.129 to 192.168.1.190. That is 2^6 − 2 = 62 hosts.
Common masks for a /24 block
Splitting one /24 network produces these subnets.
| Prefix | Mask | Subnets | Addresses each | Usable hosts each |
|---|---|---|---|---|
| /25 | 255.255.255.128 | 2 | 128 | 126 |
| /26 | 255.255.255.192 | 4 | 64 | 62 |
| /27 | 255.255.255.224 | 8 | 32 | 30 |
| /28 | 255.255.255.240 | 16 | 16 | 14 |
| /29 | 255.255.255.248 | 32 | 8 | 6 |
| /30 | 255.255.255.252 | 64 | 4 | 2 |
The pattern is that each extra borrowed bit doubles the subnet count and roughly halves the hosts.
Checking the arithmetic in code
Python's ipaddress module reproduces the table. This program splits 192.168.1.0/24 into four /26 subnets and prints the usable host count of each.
import ipaddress
net = ipaddress.ip_network("192.168.1.0/24")
for sub in net.subnets(new_prefix=26):
print(sub, sub.num_addresses - 2)It prints four lines: 192.168.1.0/26, 192.168.1.64/26, 192.168.1.128/26 and 192.168.1.192/26, each followed by 62.
Same subnet or not
Two hosts are on the same subnet when their addresses give the same result after the AND with the mask. A host uses this test to decide whether to deliver directly or send to its default gateway. If the destination's network address equals its own, the host delivers directly. Otherwise it sends the packet to the gateway.
Larger blocks that cross an octet
The block-size method works on any octet. For 172.16.37.200 with mask 255.255.252.0 (/22), the interesting octet is the third one. The block size is 256 − 252 = 4, and 37 falls in the block starting at 36. The network address is 172.16.36.0 and the broadcast address is 172.16.39.255. There are 10 host bits, so 2^10 − 2 = 1022 usable hosts.
Common errors
- Forgetting the −2. The number of addresses is 2^h. The number of usable hosts is 2^h − 2.
- Applying the block size to the wrong octet. Only the interesting octet changes. Octets to its right become 0 in the network address and 255 in the broadcast address.
- Using a non-contiguous mask. A value such as 255.255.0.255 is not a valid subnet mask.
- Mixing up the number of subnets and hosts. Subnets depend on borrowed bits. Hosts depend on remaining bits.
- Choosing the mask from the wrong requirement. Pick the smallest h with 2^h − 2 at least the required hosts. Then the prefix is 32 − h.
Exercise
A host has address 172.16.37.200 and mask 255.255.252.0. Give its network address, broadcast address and number of usable hosts.
Show answer
The prefix is /22, so there are 10 host bits. The third octet is interesting, with block size 256 − 252 = 4. The value 37 lies in the block starting at 36. The network address is 172.16.36.0, the broadcast address is 172.16.39.255, and there are 2^10 − 2 = 1022 usable hosts.
Exercise
The network 200.10.10.0/24 must be divided into equal subnets with at least 25 usable hosts each. Give the mask, the number of subnets and the usable hosts per subnet.
Show answer
You need 2^h − 2 ≥ 25, so h = 5 (30 usable hosts). The prefix is 32 − 5 = 27, and the mask is 255.255.255.224. Three bits are borrowed from the host part, giving 2^3 = 8 subnets with 30 usable hosts each.