Sub topic 4
Prefixes and Prefix Length
This chapter assumes you know IPv4 CIDR notation, can convert between binary and hexadecimal, and understand what a router and default gateway do. It also assumes you have read the previous chapter on writing and compressing IPv6 addresses.
What a prefix is
An IPv6 address has 128 bits. The leading bits identify the network. The remaining bits identify an interface on that network. The leading part is the prefix, and the prefix length is the number of bits it contains.
IPv6 writes this as address/length, the same way IPv4 CIDR does. For example, 2001:db8:abcd:12::/64 means the first 64 bits are the network prefix.
IPv6 has no dotted-decimal subnet masks. The prefix length is the only way to state the split.
Splitting an address
Take the address 2001:db8:abcd:12:21a:2bff:fe3c:4d5e/64. Each group is 16 bits, so 64 bits is exactly four groups.
| Part | Bits | Value |
|---|---|---|
| Network prefix | 64 | `2001:db8:abcd:12` |
| Interface identifier | 64 | `21a:2bff:fe3c:4d5e` |
When the prefix length is a multiple of 4, the boundary falls between hexadecimal digits. This makes the split easy to read by eye. When it is not, you must work in binary for the group that contains the boundary.
Prefix length in practice
Prefix lengths you will meet often:
/128is a single address, like/32in IPv4. Loopback::1/128is an example./64is the standard size of one subnet. SLAAC, covered later, requires it./48is a typical allocation for one site./32is a typical allocation for an ISP.
A shorter prefix covers more addresses. A /48 contains 2^16 = 65,536 separate /64 subnets.
Checking membership with Python
The ipaddress module can test whether an address belongs to a prefix and report its length.
import ipaddress
net = ipaddress.ip_network("2001:db8:abcd:12::/64")
host = ipaddress.ip_address("2001:db8:abcd:12:21a:2bff:fe3c:4d5e")
other = ipaddress.ip_address("2001:db8:abcd:13::1")
print(net.prefixlen) # 64
print(net.network_address) # 2001:db8:abcd:12::
print(host in net) # True
print(other in net) # FalseCommon errors
- Writing a mask. Using
ffff:ffff:ffff:ffff::as a mask is not how IPv6 is configured. Use/64. - Miscounting the boundary. In
2001:db8::/32, the prefix covers only2001:db8. Compressed forms hide how many groups are zero, so expand the address before counting. - Choosing a prefix longer than /64 for a normal LAN. SLAAC needs a 64-bit interface identifier, so a LAN using
/80will not autoconfigure. - Treating the address and the prefix as the same thing.
2001:db8::5/64is a host address on the network2001:db8::/64. Python'sip_networkrejects it with aValueErrorunless you passstrict=False.
Exercise
A site is allocated 2001:db8:abcd::/48. How many bits are available to number subnets if every subnet is a /64, and how many subnets does that give?
Show answer
The subnet ID is the bits between the prefix lengths, 64 - 48 = 16 bits. That gives 2^16 = 65,536 subnets.
Exercise
Does 2001:db8:1:2:aaaa::1 fall inside 2001:db8:1:3::/64? Explain.
Show answer
No. A /64 prefix covers the first four groups. The address begins 2001:db8:1:2, but the network is 2001:db8:1:3, so the fourth group differs.