Sub topic 6
Subnet Masks and Prefix Length
This chapter assumes you can convert between decimal and binary, know that an IPv4 address is 32 bits split into a network part and a host part, and have seen the default masks of classes A, B, and C. The mask is what turns that split from a rule tied to the first octet into a setting you can choose.
What a mask does
A subnet mask is a 32-bit number written like an address. In binary it always has a run of 1s followed by a run of 0s. A 1 marks a bit that belongs to the network part. A 0 marks a bit that belongs to the host part.
The default masks of the classful scheme are just three fixed masks:
| Class | Mask | Prefix length |
|---|---|---|
| A | 255.0.0.0 | /8 |
| B | 255.255.0.0 | /16 |
| C | 255.255.255.0 | /24 |
The prefix length is the count of leading 1 bits. Writing 192.168.1.10/24 means the same as giving the address with mask 255.255.255.0. The prefix form is shorter and harder to mistype.
Finding the network with a bitwise AND
A host finds its network address by applying a bitwise AND between the address and the mask. Each result bit is 1 only if both input bits are 1. Network bits pass through unchanged, and host bits become 0.
Take 192.168.1.130 with mask 255.255.255.192 (/26):
address 11000000.10101000.00000001.10000010
mask 11111111.11111111.11111111.11000000
network 11000000.10101000.00000001.10000000The network address is 192.168.1.128. The mask has 26 ones, so 6 bits remain for hosts.
Mask and prefix in code
The ipaddress module performs this conversion for you. Passing strict=False lets you give a host address and receive its network.
import ipaddress
iface = ipaddress.ip_interface("192.168.1.130/26")
print(iface.netmask) # 255.255.255.192
print(iface.network) # 192.168.1.128/26
print(iface.network.prefixlen) # 26
# The same AND done by hand
address = int(ipaddress.ip_address("192.168.1.130"))
mask = int(ipaddress.ip_address("255.255.255.192"))
print(ipaddress.ip_address(address & mask)) # 192.168.1.128Converting between mask and prefix
To go from a mask to a prefix, count the 1 bits. To go the other way, write that many 1s, pad with 0s to 32 bits, and read each octet.
Each octet of a valid mask is one of a small set of values, because the 1s must be contiguous:
| Octet value | Binary | Ones |
|---|---|---|
| 128 | 10000000 | 1 |
| 192 | 11000000 | 2 |
| 224 | 11100000 | 3 |
| 240 | 11110000 | 4 |
| 248 | 11111000 | 5 |
| 252 | 11111100 | 6 |
| 254 | 11111110 | 7 |
| 255 | 11111111 | 8 |
So /26 is 8 + 8 + 8 + 2 ones, which gives 255.255.255.192. A /20 is 8 + 8 + 4 ones, which gives 255.255.240.0.
Common errors
- Non-contiguous masks. A value such as
255.0.255.0mixes 1s and 0s. Modern routing treats masks as prefixes, so this is invalid. Theipaddressmodule rejects it. - Reading the mask by octet only. A /20 boundary falls inside the third octet. The network of
10.1.37.9/20is10.1.32.0, not10.1.37.0, because 37 is00100101and the mask keeps only the top 4 bits of that octet. - Mismatched masks on one segment. If two hosts on the same link use different masks, they disagree about which addresses are local. One may send directly where the other expects a router.
- Assuming the class sets the mask. A class A address such as
10.1.2.3can use/24. The first octet suggests a default, and the mask decides.
Exercise
A host has the address 172.16.45.200 and the mask 255.255.240.0. What is the prefix length, and what is its network address?
Show answer
The mask is 8 + 8 + 4 ones, so the prefix length is /20. In the third octet, 45 is 00101101 and the mask 240 is 11110000. The AND gives 00100000, which is 32. The host octet bits are cleared in the fourth octet, so the network address is 172.16.32.0.