IPv6 Addressing Basics
Why IPv6 exists and how its 128-bit addresses work: notation and compression, /64 subnets with 2^64 addresses, address types, and how IPv4 and IPv6 coexist. Checked with Python's ipaddress module.
IPv6 uses 128-bit addresses, about 3.4 x 10^38 of them, written as eight groups of hexadecimal digits where leading zeros can be dropped and one run of zero groups replaced by ::. The detail interviewers expect: a standard LAN subnet is a /64, with 64 network bits and 64 host bits, and IPv6 has no broadcast.
IPv4 has about 4.3 billion addresses; IPv6 has 2^128, written in hexadecimal groups with zero runs compressed to ::. A standard subnet is a /64 holding 2^64 addresses. Python's ipaddress module shows the notation rules, address classes, and how a /48 splits into 65,536 subnets.
- IPv4 addresses are 32 bits, giving 4,294,967,296 addresses, while IPv6 addresses are 128 bits, giving 2^128, about 3.4 x 10^38.
- An IPv6 address is written as eight groups of up to four hex digits; leading zeros can be dropped and one run of all-zero groups replaced by ::, which may appear only once.
- A standard IPv6 LAN subnet is a /64 holding 2^64 addresses, and a /48 site prefix splits into 65,536 /64 subnets.
- IPv6 link-local addresses use fe80::/10, unique local addresses use fc00::/7, loopback is ::1, and multicast in ff00::/8 replaces broadcast.
- IPv6 hosts can build their own addresses with SLAAC from the prefix in Router Advertisements, and the Neighbor Discovery Protocol over ICMPv6 replaces ARP.
- The IPv6 header is a fixed 40 bytes, against 20 to 60 bytes for IPv4, and only the sending host fragments packets.
01Why a new IP version
IPv4 addresses are 32 bits, which gives 4,294,967,296 addresses. That sounded like plenty in the 1980s. By the 2010s the regional registries had run out of new blocks to hand out. The workaround, network address translation (NAT), lets many devices share one public address, but it breaks the idea that any device can be reached directly, and complicates peer-to-peer applications and troubleshooting.
IPv6 uses 128-bit addresses: 2 to the power 128, about 3.4 x 10^38. It was designed to let every device have its own globally unique address again. It also simplifies the packet header (a fixed 40 bytes, against IPv4's 20 to 60 with options) and removes in-network fragmentation: only the sending host fragments.
02Reading and shortening addresses
An IPv6 address is written as eight groups of up to four hexadecimal digits, separated by colons. Two rules shorten it: leading zeros in a group can be dropped, and one run of consecutive all-zero groups can be replaced by ::. The :: may appear only once, otherwise the address would be ambiguous.
import ipaddress as ip
a = ip.ip_address('2001:0db8:0000:0000:0000:ff00:0042:8329')
print(a.compressed) # 2001:db8::ff00:42:8329
print(a.exploded) # 2001:0db8:0000:0000:0000:ff00:0042:8329The three zero groups became :: and the leading zeros in 0db8 and 0042 were dropped. The prefix 2001:db8::/32 is reserved for documentation and examples, so it appears in tutorials and should never be routed on the internet.
03Prefixes and the /64 subnet
Like IPv4's CIDR notation, address/length states how many leading bits are the network prefix. By convention an ordinary LAN subnet is a /64: the top 64 bits are the network and the bottom 64 bits identify the host (the interface identifier).
import ipaddress as ip
n = ip.ip_network('2001:db8:abcd:12::/64')
print(n.num_addresses == 2**64) # True: 18,446,744,073,709,551,616
subs = list(ip.ip_network('2001:db8:1::/48').subnets(new_prefix=64))
print(len(subs), subs[0], subs[-1]) # 65536 2001:db8:1::/64 2001:db8:1:ffff::/64A single /64 holds as many addresses as the whole IPv4 address space squared (2^32 x 2^32). That is deliberate: it makes subnetting simple, with no need to count hosts, and allows hosts to build their own addresses. A typical site is given a /48 by its provider, which splits into 65,536 /64 subnets, and an ISP might give a home a /56 (256 subnets).
04Address types
import ipaddress as ip
print(ip.ip_address('::1').is_loopback) # True
print(ip.ip_address('fe80::1').is_link_local) # True
print(ip.ip_address('fd12:3456:789a::1').is_private) # True
print(ip.ip_address('2001:db8::1').is_global) # False (documentation range)Global unicast addresses (currently from 2000::/3) are routable on the internet. Link-local addresses (fe80::/10) exist on every IPv6 interface and work only on the local link, used for neighbour discovery and routing protocols. Unique local addresses (fc00::/7, in practice fd...) are the private-network equivalent, like 10.0.0.0/8. ::1 is loopback. There is no broadcast in IPv6: multicast addresses (ff00::/8) replace it, which cuts needless interrupts on hosts that do not care.
05How hosts get addresses
A router announces the network prefix in Router Advertisement messages. A host can then create its own address with SLAAC (stateless address autoconfiguration): the prefix plus a 64-bit interface identifier. Modern systems use random, changing identifiers instead of one derived from the hardware address, so a device is not trackable by a fixed suffix. DHCPv6 is available when an administrator wants tighter control.
ARP is replaced by the Neighbor Discovery Protocol (NDP), which uses ICMPv6 messages to find the link-layer address of a neighbour and detect duplicate addresses.
06Living with IPv4
Most networks run dual-stack, with both protocols active, and applications prefer IPv6 when it works. An IPv4 address can be embedded in IPv6 notation: ::ffff:192.0.2.1 is recognised by Python as the IPv4-mapped address 192.0.2.1, used by dual-stack sockets. Other transition tools are tunnels that carry IPv6 over IPv4 networks, and translators such as NAT64 with DNS64 that let IPv6-only clients reach IPv4-only servers.
From a programmer's view, avoid storing addresses as 32-bit integers or in fixed 15-character strings, parse them with a library, and use getaddrinfo so the right family is chosen for you. In my sandbox, socket.has_ipv6 was true while localhost resolved only to 127.0.0.1, a reminder that what is available depends on the environment.
07Common interview questions
Q: How many bits is an IPv6 address and why?
A: 128, to remove address scarcity and let every device have a unique address.
Q: How do you shorten 2001:0db8:0000:0000:0000:ff00:0042:8329?
A: Drop leading zeros and replace one run of zero groups with ::, giving 2001:db8::ff00:42:8329.
Q: Why a /64 for a LAN?
A: It splits the address into 64 network bits and 64 host bits, which supports SLAAC and keeps subnetting simple.
Q: Is there broadcast in IPv6?
A: No. Multicast replaces it.
Q: What replaces ARP?
A: The Neighbor Discovery Protocol over ICMPv6.
Q: How do IPv4 and IPv6 coexist?
A: Dual-stack hosts, tunnelling, and translation such as NAT64.