Sub topic 2
Anatomy of an IPv6 address
This chapter assumes you know IPv4 dotted-decimal notation, how binary and hexadecimal numbers work, and what a network prefix is. It covers the structure of an address: how many bits it has, how those bits are grouped, and how to read the groups. The next chapter covers the shortening rules, and the chapter after that covers prefixes.
The 128-bit address
An IPv6 address is a 128-bit number. An IPv4 address has 32 bits, so the IPv6 space is 2^96 times larger. In numbers:
- IPv4: 2^32 = 4,294,967,296 addresses.
- IPv6: 2^128 = 340,282,366,920,938,463,463,374,607,431,768,211,456 addresses.
The size does not exist so that every device can have a unique address. A large space lets designers give each network a fixed-size block and add structure to the bits, with no need to ration addresses.
Writing 128 bits in binary or decimal would be impractical, so IPv6 uses hexadecimal. Each hex digit stands for exactly 4 bits. This makes conversion between text and bits mechanical, which matters later when you work with prefixes.
Eight groups of sixteen bits
The text form divides the 128 bits into eight groups of 16 bits. Each group is written as up to four hex digits, and groups are separated by colons. The structure is:
- 128 bits = 8 groups × 16 bits.
- 16 bits = 4 hex digits × 4 bits.
- The full address has 32 hex digits and 7 colons, so 39 characters.
Here is a full address:
2001:0db8:abcd:0012:0000:0000:0000:0001The groups are numbered 1 to 8 from the left. The leftmost group holds the most significant bits.
| Group | Bits (from the left) | Example value |
|---|---|---|
| 1 | 0–15 | 2001 |
| 2 | 16–31 | 0db8 |
| 3 | 32–47 | abcd |
| 4 | 48–63 | 0012 |
| 5 | 64–79 | 0000 |
| 6 | 80–95 | 0000 |
| 7 | 96–111 | 0000 |
| 8 | 112–127 | 0001 |
Bit 0 here means the most significant bit. This is the convention used for prefixes: a "/48" prefix is the first 48 bits, which are groups 1 to 3.
The 64/64 convention
Most IPv6 networks that hosts connect to use a fixed split of the address. The first 64 bits (groups 1 to 4) identify the network. The last 64 bits (groups 5 to 8) identify the interface within it. The second half is called the interface identifier, or IID.
In the example above:
- Network half:
2001:0db8:abcd:0012 - Interface half:
0000:0000:0000:0001
This split is a convention, not part of the address format. Any prefix length from 0 to 128 is valid in principle. However, features such as stateless address autoconfiguration (covered later in the tutorial) assume a 64-bit interface identifier on ordinary LANs. For that reason, a subnet that hosts attach to is almost always a /64.
Note a difference from IPv4. An IPv4 address has no fixed network/host boundary. You need the subnet mask or prefix length to know where it falls. IPv6 also needs a prefix length to be unambiguous, but the /64 convention means the boundary is predictable for most host-facing links.
Reading the bits
Because each group is 16 bits, you can read a group as a number from 0 to 65,535 (0000 to ffff). The leading digits of the first group often tell you the address type, because types are assigned by bit patterns at the start of the address. For example, 2001:db8::1 starts with 2, which is binary 0010. You will see in a later chapter that addresses beginning with binary 001 are global unicast.
The Python standard library makes it easy to take an address apart. The following program splits an address into its eight groups and its two 64-bit halves:
import ipaddress
addr = ipaddress.IPv6Address("2001:db8:abcd:12::1")
value = int(addr) # the address as one 128-bit integer
groups = [(value >> shift) & 0xFFFF for shift in range(112, -1, -16)]
print([f"{g:04x}" for g in groups])
# ['2001', '0db8', 'abcd', '0012', '0000', '0000', '0000', '0001']
network_half = value >> 64
interface_half = value & (2**64 - 1)
print(f"{network_half:016x}") # 20010db8abcd0012
print(f"{interface_half:016x}") # 0000000000000001
print(addr.packed.hex()) # 20010db8abcd00120000000000000001The shifts start at 112 because group 1 occupies bits 112 to 127 when the integer is counted from its least significant bit. Each step moves 16 bits to the right. The packed property returns the 16 raw bytes, which is the form a network stack puts into a packet header.
Text conventions
Several rules about how addresses are written apply even before you learn the compression rules.
- Hex digits are case-insensitive.
2001:DB8::1and2001:db8::1are the same address. RFC 5952 recommends lowercase for output, so use lowercase in documents, logs, and configuration files to make text comparison and searching reliable. - A group may be written with fewer than four digits.
db8means0db8. Leading zeros within a group may be dropped, but trailing zeros may not:0db8anddb80are different values. - Colons are separators, not part of any group. Groups are never split across a colon.
- An address can end with an embedded IPv4 address in dotted decimal, such as
::ffff:192.0.2.1. The dotted part stands for the last two groups (32 bits). - In a URL, an IPv6 literal goes in square brackets, because the colon would otherwise be confused with the port separator:
http://[2001:db8::1]:8080/. - A link-local address may carry a zone identifier after a percent sign, such as
fe80::1%eth0. The zone names the interface, because the same link-local address can be valid on several links of one host. The zone is not part of the 128 bits.
Validating an address
An address is well formed when it has exactly 128 bits after expansion. Common errors include:
- A group with more than four hex digits, such as
12345. - A non-hex character, such as
g. - Too many or too few groups when no compression is used. Seven groups or nine groups is invalid.
- Stray single colons at the start or end.
Do not write your own parser for production code. Use a library: Python's ipaddress raises AddressValueError on malformed input.
import ipaddress
for text in ["2001:db8:0:0:0:0:0:1", "2001:db8:0:0:0:0:0", "2001:db8:12345::1"]:
try:
ipaddress.IPv6Address(text)
print(text, "valid")
except ipaddress.AddressValueError as err:
print(text, "invalid")
# 2001:db8:0:0:0:0:0:1 valid
# 2001:db8:0:0:0:0:0 invalid
# 2001:db8:12345::1 invalidThe second string has only seven groups and no compression, so it is not 128 bits. The third has a five-digit group.
What to remember
An IPv6 address is 128 bits, written as eight colon-separated 16-bit groups of up to four hex digits. The first bits of the address usually tell you what kind of address it is, and on most host-facing networks the first 64 bits name the network and the last 64 name the interface. Later chapters build on this: shortening the text form, applying prefix lengths, and classifying addresses by their leading bits.
Exercise
An address has 32 hex digits when fully written out. (a) How many bits is that? (b) Which groups form the interface identifier on a standard /64 subnet? (c) Are 2001:db8:0:0:0:0:0:1 and 2001:0DB8:0000:0000:0000:0000:0000:0001 the same address?
Show answer
(a) 32 hex digits × 4 bits = 128 bits. (b) Groups 5 to 8, which are bits 64 to 127. (c) Yes. Hex digits are case-insensitive, and dropping leading zeros inside a group does not change its value. Both expand to the same 128-bit number.
Exercise
Using the integer approach from the chapter, which expression gives the value of group 4 (counting from 1 at the left) of an IPv6 address stored in the integer value?
Show answer
(value >> 64) & 0xFFFF. Group 4 covers bits 48 to 63 from the left, which is bits 64 to 79 counted from the least significant bit. Shifting right by 64 moves it to the bottom, and the mask keeps 16 bits. For the example address 2001:db8:abcd:12::1, this gives 0x0012.