Reading and writing IPv6 addresses
Convert a 16-bit group between hexadecimal and binary, and explain the 8 × 16-bit structure of an IPv6 address. Expand any compressed IPv6 address to its full 32-digit form. Write any IPv6 address in the RFC 5952 canonical text form. Read prefix notation, find the network prefix of an address, and recognise the IPv6 documentation prefixes.
Take the quiz · pass mark 70% · 10 questions
Explanation
128 bits, eight groups
An IPv6 address is 128 bits long. Writing 128 ones and zeros is unreadable, so the text form (RFC 4291, section 2.2) splits the address into eight groups of 16 bits. Each group is written as up to four hexadecimal digits, and the groups are separated by colons:
2001:0db8:0000:0000:0008:0800:200c:417a
Each hex digit stands for exactly 4 bits (a nibble), so each group is 4 nibbles and the whole address is 32 nibbles:
| Hex | Binary | Hex | Binary |
|---|---|---|---|
| 0 | 0000 | 8 | 1000 |
| 1 | 0001 | 9 | 1001 |
| 2 | 0010 | a | 1010 |
| 3 | 0011 | b | 1011 |
| 4 | 0100 | c | 1100 |
| 5 | 0101 | d | 1101 |
| 6 | 0110 | e | 1110 |
| 7 | 0111 | f | 1111 |
So the group 0db8 is 0000 1101 1011 1000 in binary. You will rarely convert whole addresses to binary, but you will often convert one group, when a prefix boundary falls inside it (see "Prefixes" below).
Drawing diagram…
Shortening rules (what RFC 4291 allows)
RFC 4291 allows two ways to shorten an address:
- Leading zeros in a group may be dropped.
0db8can be writtendb8,0000as0, and0042as42. Trailing zeros may not be dropped:2000stays2000. - One run of consecutive all-zero groups may be replaced by
::. The::can appear only once. If it appeared twice, nobody could tell how many zero groups each one stood for.
Because RFC 4291 allows these shortenings without requiring them, the same address can be written many ways:
2001:0db8:0000:0000:0008:0800:200c:417a
2001:db8:0:0:8:800:200c:417a
2001:db8::8:800:200c:417a
2001:DB8::8:800:200C:417A
All four are the same address. That is fine for computers but bad for people searching logs, comparing configurations or writing documentation. RFC 5952 fixes this.
The canonical form (RFC 5952)
RFC 5952 recommends one text form for every address, and that is what this course, our tools and our exams use:
- Leading zeros must be suppressed in every group (
0db8→db8,0000→0). ::must be used to shorten as much as possible: it replaces the longest run of all-zero groups.::must not be used for a single all-zero group. Write0instead.- If there are two equally long runs, the first one becomes
::. - Hex letters must be lower-case.
Drawing diagram…
Worked through for 2001:0DB8:0000:0000:0001:0000:0000:0001:
- Leading zeros:
2001:DB8:0:0:1:0:0:1 - Two runs of two zero groups (groups 3–4 and 6–7). They are equally long, so the first one wins:
2001:DB8::1:0:0:1 - Lower-case:
2001:db8::1:0:0:1
Expanding a compressed address
To expand, work out how many groups :: stands for: 8 minus the number of groups you can see. Then pad every group to four digits.
2001:db8::ff00:42:8329 shows 5 groups, so :: stands for 3 zero groups:
2001:0db8:0000:0000:0000:ff00:0042:8329
Edge cases:
::on its own is the unspecified address (all 128 bits zero).::1is the loopback address (127 zero bits, then a 1).fe80::and::ffff:0:0are valid:::may sit at the start or the end.
Prefixes
Like IPv4 CIDR, IPv6 writes a prefix as address/length, where the length is the number of leading bits that identify the network (RFC 4291, section 2.3):
2001:db8:4a00::/48 the first 48 bits (three groups) are the prefix
2001:db8:4a00:10::/64 a /64 subnet inside it
Rules of thumb:
- Every 4 bits of prefix length is one hex digit, and every 16 bits is one group. A /48 ends after group 3, a /56 ends after the second digit of group 4, and a /64 ends after group 4.
- Prefix lengths that are multiples of 4 (nibble boundaries) end on a hex digit. Those are much easier to read and to delegate in reverse DNS.
- To find the network prefix of an address, keep the first n bits and set the rest to zero. On a nibble boundary that just means "keep the first n/4 hex digits and zero the rest". Inside a nibble you need the binary of that one digit.
- Number of subnets of length m inside a prefix of length n: 2^(m − n).
2001:db8:4a00:1234::/64 sits in which /56? A /56 keeps 14 hex digits: 2001 0db8 4a00 12. So the answer is 2001:db8:4a00:1200::/56.
Addresses in URLs and with ports
A colon already separates host and port in URLs, so literal IPv6 addresses go inside square brackets (RFC 3986): https://[2001:db8::80]:8443/. RFC 5952 recommends the same bracket style when you write an address with a port in documentation.
Documentation prefixes
Use only these prefixes in examples, slides and lab write-ups, so that nobody copies a real network's addresses:
| Prefix | RFC | Typical use |
|---|---|---|
2001:db8::/32 | RFC 3849 | Everyday examples (most of this course) |
3fff::/20 | RFC 9637 | Examples that need more space than a /32, such as RIR- or ISP-scale allocation plans |
Worked examples
Example 1: Canonicalise an address
Question: Write 2001:0DB8:00A0:0000:0000:0000:0000:0BAD in canonical form.
Working: Leading zeros give 2001:DB8:A0:0:0:0:0:BAD. There is one run of four zero groups (groups 4–7), which becomes ::. Lower-case gives 2001:db8:a0::bad.
Example 2: The single-zero-group trap
Question: Write 2001:db8:0:1:1:1:1:1 in canonical form.
Working: There is only one all-zero group. RFC 5952 forbids :: for a single group, so the address is already canonical: 2001:db8:0:1:1:1:1:1. 2001:db8::1:1:1:1:1 is valid but not canonical.
Example 3: Expand an address
Question: Expand 2001:db8:a::1f.
Working: You can see four groups (2001, db8, a, 1f), so :: stands for 8 − 4 = 4 zero groups. Padding gives 2001:0db8:000a:0000:0000:0000:0000:001f.
Example 4: The prefix of an address, on and off a nibble boundary
Question: What is the /52 that contains 2001:db8:4a00:1234::5? And the /50?
Working:
- /52 = 13 hex digits:
2001 0db8 4a00 1followed by zeros. Answer:2001:db8:4a00:1000::/52. - /50 falls inside the 13th digit. The first 48 bits are
2001:db8:4a00, and we keep 2 more bits of group 4 (1234=0001 0010 0011 0100). Its first two bits are00, so group 4 becomes0000. Answer:2001:db8:4a00::/50.
Example 5: Counting subnets
Question: How many /64 subnets fit in a /56?
Working: 2^(64 − 56) = 2^8 = 256.
Lab
Goal: Build a small script that canonicalises IPv6 addresses, and check it against Python's ipaddress module and Linux's own output.
You need: Python 3.8 or later. Linux (or WSL or a Linux VM) for step 4.
Step 1: Use the reference implementation
Python's ipaddress module already produces the RFC 5952 form:
python3 -c "import ipaddress; print(ipaddress.IPv6Address('2001:0DB8:0000:0000:0001:0000:0000:0001'))"
python3 -c "import ipaddress; print(ipaddress.IPv6Address('2001:db8::1').exploded)"
python3 -c "import ipaddress; print(ipaddress.IPv6Network('2001:db8:4a00:1234::/56', strict=False))"
Write down what each command prints and explain each result using the rules in this lesson.
Step 2: Write your own canonicaliser
Create canon.py that does not use ipaddress for the conversion:
def expand(addr: str) -> list[int]:
"""Return the eight 16-bit groups as integers."""
if "::" in addr:
left, right = addr.split("::")
l = [g for g in left.split(":") if g]
r = [g for g in right.split(":") if g]
groups = l + ["0"] * (8 - len(l) - len(r)) + r
else:
groups = addr.split(":")
assert len(groups) == 8, "not 8 groups"
return [int(g, 16) for g in groups]
def canonical(addr: str) -> str:
g = expand(addr)
# TODO 1: find the longest run (length >= 2) of zero groups; on a tie keep the first
# TODO 2: join the groups in lower-case hex without leading zeros, using '::' for that run
raise NotImplementedError
Complete the two TODOs. (This simplified version does not handle embedded-IPv4 forms like ::ffff:192.0.2.1; that is fine for this lab.)
Step 3: Test against the reference
Add a test loop comparing canonical(x) with str(ipaddress.IPv6Address(x)) for at least these inputs:
2001:0DB8:0000:0000:0001:0000:0000:0001
2001:db8:0:1:1:1:1:1
2001:db8:0:0:1:0:0:0
0:0:0:0:0:0:0:1
fe80:0:0:0:0:0:0:0
2001:db8:a:0:0:0:0:1f
3fff:0:0:abcd:0:0:0:1
All must match. Then add 1,000 random addresses (random.getrandbits(128)), with random zero groups injected so that runs and ties happen.
Step 4: Compare with what Linux prints
sudo ip netns add canon
sudo ip -n canon link set lo up
sudo ip -n canon -6 addr add 2001:0DB8:0000:0000:0001:0000:0000:0001/64 dev lo
ip -n canon -6 addr show dev lo
sudo ip netns del canon
Note the form ip prints and compare it with your canonicaliser's output.
What to submit
Your completed canon.py, the test output showing every case matching, and one sentence on whether ip printed the canonical form.
Summary
- An IPv6 address is 128 bits, written as 8 groups of 16 bits in hex. Each hex digit is a 4-bit nibble.
- RFC 4291 allows dropping leading zeros and one
::for a run of zero groups. RFC 5952 turns that into one canonical form: suppress leading zeros, use::for the longest run of two or more zero groups (the first run on a tie, never for a single group), and write lower-case. - To expand,
::stands for 8 minus the visible groups. - A prefix
/nkeeps the first n bits. Nibble-aligned lengths end on a hex digit, and there are 2^(m − n) subnets of length m in a /n. - Put IPv6 literals in square brackets in URLs and with ports.
- Use only
2001:db8::/32(RFC 3849) and3fff::/20(RFC 9637) in examples. - Next lesson (L1.03): what the different kinds of address mean, from global unicast and link-local to multicast.
Quiz imported · 10 questions · pass mark 70%. Lab questions are not auto-marked.