Sub topic 4
Prefixes and prefix length
This chapter assumes you know CIDR notation and subnet masks from IPv4, can convert between binary and hexadecimal, and can read the compressed address form from the previous chapter. It covers how a prefix length divides an IPv6 address, how to work out the prefix, and which lengths you will meet in practice.
What a prefix is
An IPv6 address has 128 bits. A prefix is the first n bits of an address. It identifies a group of addresses that share those bits. The number n is the prefix length. You write them together as address/length, for example 2001:db8:abcd:12::/64.
IPv6 has no dotted-decimal subnet masks. The slash form is the only notation, and routers, hosts, and configuration files all use it.
A prefix has two uses:
- Routing. A router stores prefixes in its routing table and sends a packet toward the entry whose prefix matches the destination.
- Addressing. The prefix on an interface tells the host which addresses are on the same link.
Network prefix and interface identifier
For most subnets, the 128 bits split in half. The first 64 bits are the network prefix. The last 64 bits are the interface identifier (IID), which names one interface within that subnet.
The first 64 bits can be split further. A typical site has this layout:
Take 2001:db8:abcd:12:21a:2bff:fe3c:4d5e/64. The first four groups, 2001:db8:abcd:12, are the network prefix. The last four groups, 21a:2bff:fe3c:4d5e, are the interface identifier. Inside the prefix, 2001:db8:abcd is the 48-bit site prefix and 0012 is the subnet ID.
You can compute the split with Python:
import ipaddress
iface = ipaddress.ip_interface("2001:db8:abcd:12:21a:2bff:fe3c:4d5e/64")
print(iface.network) # 2001:db8:abcd:12::/64
print(iface.ip) # 2001:db8:abcd:12:21a:2bff:fe3c:4d5e
iid = int(iface.ip) & ((1 << 64) - 1) # keep only the low 64 bits
print(f"{iid:016x}") # 021a2bfffe3c4d5eip_interface keeps both the host address and its prefix length. .network clears the bits after the prefix. The mask (1 << 64) - 1 is 64 one-bits, so the & operation leaves only the interface identifier.
Prefix lengths in common use
Any length from 0 to 128 is valid. In practice a few values appear again and again.
| Length | Typical use | Addresses covered |
|---|---|---|
| /0 | `::/0`, the default route | all of them |
| /32 | An allocation to an ISP | 2^96 |
| /48 | A site, such as a company campus | 2^80 (65,536 subnets) |
| /56 | A small site or home customer | 2^72 (256 subnets) |
| /64 | One subnet or link | 2^64 |
| /127 | A point-to-point router link | 2 |
| /128 | A single address, such as `::1/128` | 1 |
The address count for a prefix of length n is 2^(128 − n). Networks deliberately waste addresses, so a /64 holding three hosts is normal and expected.
Why /64 is the standard subnet size
A /64 gives each subnet a 64-bit interface identifier. Several IPv6 features assume that size, including SLAAC (covered in a later chapter) and the standard interface identifier methods. If you configure a LAN with a /80, SLAAC will not work on it. Reserve other lengths for special cases:
- Point-to-point links between routers can use /127, which RFC 6164 recommends. A /127 limits the effect of some neighbor-cache exhaustion attacks. Some networks still use /64 on these links and filter traffic to unused addresses.
- Loopback and host routes use /128, because they name exactly one address.
Working with prefix bits
When the prefix length is a multiple of 4, the boundary falls between two hexadecimal digits. This is a nibble boundary. Lengths such as /32, /48, /52, /56, and /64 are easy to read because you can count digits. A length like /50 ends in the middle of a digit and needs binary arithmetic.
Take 2001:db8:abcd:12::/60. The prefix covers 60 bits, which is the first three full groups (48 bits) plus the top 12 bits of the fourth group. The fourth group is 0012 in hex, which is 0000 0000 0001 0010 in binary. Keep the top 12 bits and zero the last 4. The result is 0000 0000 0001 0000, which is 0010. The network address is 2001:db8:abcd:10::/60.
import ipaddress
net = ipaddress.ip_network("2001:db8:abcd:12::/60", strict=False)
print(net) # 2001:db8:abcd:10::/60
print(net.network_address) # 2001:db8:abcd:10::
print(net.num_addresses) # 4722366482869645213696
probe = ipaddress.ip_address("2001:db8:abcd:1f::1")
print(probe in net) # Truestrict=False tells Python to clear the extra bits instead of raising a ValueError. With the default strict=True, an address that has bits set after the prefix is rejected.
The subnet IDs from 10 to 1f all fall inside this /60, which gives 16 subnets of /64 each.
Longest prefix match
A router may hold several prefixes that all match a destination. It picks the longest prefix match, meaning the entry with the greatest prefix length. For example:
| Route | Next hop |
|---|---|
| `::/0` | upstream ISP |
| `2001:db8::/32` | core router |
| `2001:db8:abcd::/48` | site router |
A packet to 2001:db8:abcd:12::5 matches all three. The /48 entry is the longest, so the site router receives it. A packet to 2001:db8:ffff::1 matches only the /32 and ::/0, so the core router receives it.
On a host, the same rule decides whether a destination is on-link. The host compares the destination with each prefix configured on its interfaces. If one matches, the host sends the packet directly to the neighbor. If none matches, the host sends the packet to the default gateway.
You can see these entries on Linux with ip -6 route show. Each line starts with a prefix and its length, followed by an interface or a gateway.
Common mistakes
- Counting the prefix in groups. A /64 is 64 bits, which happens to equal four groups. A /60 is not "3.75 groups" in any useful sense. Work in bits.
- Forgetting compression hides zeros. In
2001:db8::/32, the::stands for the zeros after the prefix. The prefix length still counts bits of the full 128-bit address. - Assuming the prefix is always 64 bits. The network portion ranges from /0 to /128. Only subnet-level addressing is fixed at /64.
- Borrowing bits from the interface identifier. Take subnet bits from the subnet ID, which is 16 bits in a /48. Do not shrink the interface identifier below 64 bits on a normal LAN.
Exercise
A host has the address 2001:db8:77:a3c5:0:0:0:9/62. What is the network prefix in compressed form, and how many /64 subnets does that prefix contain?
Show answer
The prefix covers 62 bits. The first three groups (48 bits) stay as they are. The fourth group a3c5 is 1010 0011 1100 0101 in binary, and 62 − 48 = 14 bits are kept. Clear the last 2 bits (01), which gives 1010 0011 1100 0100, or a3c4. The network prefix is 2001:db8:77:a3c4::/62. A /62 holds 2^(64 − 62) = 4 subnets of /64, namely subnet IDs a3c4 to a3c7.