IPv6
IPv6 is the successor to IPv4, standardised in 1998 (RFC 2460, now RFC 8200). Its headline feature is huge 128-bit addresses: about 3.4 × 10^38 of them, enough to give every device its own globally unique address and do away with NAT. Today a large and growing share of Internet traffic (around 40–50% of Google's users) arrives over IPv6.
Writing IPv6 addresses
An IPv6 address is eight groups of four hex digits (16 bits each), separated by colons:
2001:0db8:0000:0000:0000:ff00:0042:8329Two rules shorten it:
- Leading zeros in a group may be dropped:
0db8→db8,0042→42. - One run of consecutive all-zero groups may be replaced by
::.
So the address above is usually written 2001:db8::ff00:42:8329.
import ipaddress
for text in ["2001:0db8:0000:0000:0000:ff00:0042:8329", "2001:db8::1",
"fe80::1c2b:3aff:fe4d:5e6f", "::1", "::"]:
ip = ipaddress.IPv6Address(text)
print(f"{ip.compressed:<26} {ip.exploded}")2001:db8::ff00:42:8329 2001:0db8:0000:0000:0000:ff00:0042:8329
2001:db8::1 2001:0db8:0000:0000:0000:0000:0000:0001
fe80::1c2b:3aff:fe4d:5e6f fe80:0000:0000:0000:1c2b:3aff:fe4d:5e6f
::1 0000:0000:0000:0000:0000:0000:0000:0001
:: 0000:0000:0000:0000:0000:0000:0000:0000
:: can only appear once: 2001::1::2 would be ambiguous, because you couldn't tell how many zero groups each :: hides.
Address structure
A typical global unicast address splits into a 64-bit network prefix and a 64-bit interface ID:
| 48 bits global routing prefix | 16 bits subnet | 64 bits interface ID |
2001:db8:acad 0001 0000:0000:0000:0010An ISP typically gives a home a /56 or /48. With a /48, a company has 65,536 subnets of size /64, and each /64 has 2^64 addresses. Subnets in IPv6 are basically always /64.
import ipaddress
site = ipaddress.ip_network("2001:db8:acad::/48")
subnets = site.subnets(new_prefix=64)
for _ in range(3):
print(next(subnets))
print("subnets in a /48:", 2 ** (64 - 48))
print("addresses per /64:", f"{2 ** 64:,}")
print("2001:db8:acad:1::10 in site:", ipaddress.ip_address("2001:db8:acad:1::10") in site)2001:db8:acad::/64
2001:db8:acad:1::/64
2001:db8:acad:2::/64
subnets in a /48: 65536
addresses per /64: 18,446,744,073,709,551,616
2001:db8:acad:1::10 in site: True
Address types
| Prefix | Type | Like IPv4's… |
|---|---|---|
2000::/3 |
global unicast | public addresses |
fe80::/10 |
link-local (every interface has one; never routed) | 169.254.0.0/16 |
fc00::/7 (in practice fd00::/8) |
unique local addresses (ULA) | private RFC 1918 |
::1/128 |
loopback | 127.0.0.1 |
ff00::/8 |
multicast (ff02::1 = all nodes on the link) |
multicast/broadcast |
2001:db8::/32 |
documentation only | 192.0.2.0/24 |
There is no broadcast in IPv6: multicast groups replace it.
import ipaddress
for text in ["2a00:1450:4001:82a::200e", "fe80::1", "fd12:3456:789a::1", "ff02::1", "::1", "2001:db8::5"]:
ip = ipaddress.ip_address(text)
kind = ("loopback" if ip.is_loopback else "link-local" if ip.is_link_local
else "multicast" if ip.is_multicast else "global" if ip.is_global
else "private/ULA or reserved")
print(f"{text:<26} {kind}")2a00:1450:4001:82a::200e global
fe80::1 link-local
fd12:3456:789a::1 private/ULA or reserved
ff02::1 multicast
::1 loopback
2001:db8::5 private/ULA or reserved
Automatic interface IDs (EUI-64)
IPv6 hosts can configure their own addresses without DHCP (SLAAC: the router announces the /64 prefix, the host picks the interface ID). The original method, modified EUI-64, derives the ID from the MAC address: insert ff:fe in the middle and flip the universal/local bit.
import ipaddress
def eui64(prefix, mac):
b = bytearray.fromhex(mac.replace(":", ""))
b[0] ^= 0x02 # flip the U/L bit
interface_id = bytes(b[:3]) + b"\xff\xfe" + bytes(b[3:])
network = ipaddress.ip_network(prefix)
return network.network_address + int.from_bytes(interface_id, "big")
print(eui64("fe80::/64", "1e:2b:3a:4d:5e:6f"))
print(eui64("2001:db8:acad:1::/64", "00:1a:2b:3c:4d:5e"))fe80::1c2b:3aff:fe4d:5e6f
2001:db8:acad:1:21a:2bff:fe3c:4d5e
Because the MAC address shows through, these addresses let websites track a device across networks. Modern operating systems therefore use privacy addresses (RFC 4941/8981) with a random-looking interface ID that changes over time.
The IPv6 header
The IPv6 header is a fixed 40 bytes, simpler than IPv4's: no checksum (link and transport layers already check), no fragmentation fields (only the sender may fragment, using an extension header), and the TTL is renamed Hop Limit.
import ipaddress
import struct
def ipv6_header(src, dst, payload_len, next_header, hop_limit=64, flow=0):
first_word = (6 << 28) | (0 << 20) | flow # version, traffic class, flow label
return struct.pack("!IHBB16s16s", first_word, payload_len, next_header, hop_limit,
ipaddress.IPv6Address(src).packed, ipaddress.IPv6Address(dst).packed)
hdr = ipv6_header("2001:db8:acad:1::10", "2001:db8::53", payload_len=32, next_header=17)
print(len(hdr), "bytes")
print(hdr[:8].hex(" "))
word, length, nxt, hops = struct.unpack("!IHBB", hdr[:8])
print("version", word >> 28, "| payload", length, "| next header", nxt, "(UDP) | hop limit", hops)40 bytes
60 00 00 00 00 20 11 40
version 6 | payload 32 | next header 17 (UDP) | hop limit 64
Dual stack and transition
Most networks run dual stack: every host has both an IPv4 and an IPv6 address, and applications prefer IPv6 when the destination has one (the "Happy Eyeballs" algorithm races both and uses whichever connects first). Other transition tools include tunnels (6in4) and NAT64/DNS64 for IPv6-only networks reaching IPv4 servers.
ip -6 addr show dev wlp2s0Sample output:
3: wlp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 state UP qlen 1000
inet6 2001:db8:acad:1:8d4e:2c91:b07a:31f5/64 scope global temporary dynamic
valid_lft 86123sec preferred_lft 14123sec
inet6 2001:db8:acad:1:1c2b:3aff:fe4d:5e6f/64 scope global dynamic mngtmpaddr
valid_lft 86123sec preferred_lft 14123sec
inet6 fe80::1c2b:3aff:fe4d:5e6f/64 scope link
valid_lft forever preferred_lft foreverTest yourself
What is the shortest valid form of 2001:0db8:0000:0000:0000:0000:0000:0001?
- 2001:db8::1
- 2001:db8:0:0::1:
- 2001:db8:::1
- 21:db8::1
Which IPv6 prefix is link-local?
- 2000::/3
- fe80::/10
- ff00::/8
- fd00::/8
Which IPv4 header feature does IPv6 drop entirely?
- Source address
- Hop limit / TTL
- Header checksum
- Payload length