Sub topic 9
Loopback and unspecified addresses
This chapter covers the two IPv6 addresses that never identify a real device on a network: ::1 and ::. It assumes you know IPv6 notation and :: compression, prefix length notation, and the difference between a host address and a network prefix. You should also know what an IPv4 loopback address (127.0.0.1) and the IPv4 wildcard (0.0.0.0) do.
The two special addresses
Both addresses are written with :: compression, and both are single addresses with a /128 prefix.
| Address | Full form | Prefix | Name | IPv4 counterpart |
|---|---|---|---|---|
| `::1` | `0000:0000:0000:0000:0000:0000:0000:0001` | `::1/128` | Loopback | `127.0.0.1` (inside `127.0.0.0/8`) |
| `::` | `0000:0000:0000:0000:0000:0000:0000:0000` | `::/128` | Unspecified | `0.0.0.0` |
IPv4 reserves a whole /8 for loopback, about 16.7 million addresses. IPv6 reserves exactly one address. Any packet to 127.0.0.5 loops back on a typical IPv4 host. In IPv6 only ::1 does.
The loopback address `::1`
::1 means "this host itself". A packet sent to ::1 never reaches a network interface card or a cable. The operating system's IPv6 stack hands it straight back to a local socket, usually through a virtual interface called lo on Linux.
The rules from the IPv6 addressing architecture (RFC 4291) are:
- A node can assign
::1only to a loopback interface. - A packet with
::1as its source or destination must never be sent out of a link. - A router must not forward a packet that carries
::1as source or destination. It must drop it.
Because of these rules, ::1 is a safe address for services meant to be reachable only from the local machine, such as a development database or an admin interface.
Using `::1` from code
This script starts a TCP server on ::1, connects to it from a client, and prints what each side sees. It is complete and runs on any host with IPv6 enabled on its loopback interface.
import socket
import threading
def serve(listener):
conn, peer = listener.accept()
with conn:
print("server saw client at", peer[0])
conn.sendall(b"hello over ::1")
listener = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
listener.bind(("::1", 0)) # port 0 asks the OS to pick a free port
listener.listen(1)
port = listener.getsockname()[1]
worker = threading.Thread(target=serve, args=(listener,))
worker.start()
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as client:
client.connect(("::1", port))
print("client received", client.recv(100).decode())
worker.join()
listener.close()Both ends report ::1 as the address. Since the traffic never leaves the host, no other machine can connect to this server.
Checking the loopback interface
ip -6 addr show lo
ping -6 -c 3 ::1The first command should list inet6 ::1/128 scope host. The scope host label means the address is valid only inside this machine. A successful ping -6 ::1 shows that the local IPv6 stack works. It says nothing about your network connection, because no packet left the machine.
The unspecified address `::`
:: means "no address" or "any address", depending on where it appears. The standard allows it in only a few places.
- As a source address, when a host has no address yet. The packet says "I am sending this, but I don't have an address to identify myself".
- As a bind address in a socket, where it means "all of my addresses".
- As a placeholder in configuration and routing tables, where it means "unknown" or "not set".
The rules for :: are the opposite of loopback in one respect. You must never assign it to an interface, and it must never be the destination of a packet. A router must not forward a packet whose source is ::.
Source address during duplicate address detection
The main protocol use of :: as a source is Duplicate Address Detection (DAD). Before a host uses a new address, it checks that no one else on the link already has it. The new address is called tentative. The host cannot use it as a source yet, so it uses :: instead.
The solicitation goes to the solicited-node multicast address built from the last 24 bits of the tentative address. Only nodes whose addresses share those bits listen on it, which keeps the cost low. If another node owns the address, it answers and the new host drops the address. If nobody answers, the host assigns it.
Binding a server to `::`
A server that binds to :: accepts connections on every IPv6 address of the host, as 0.0.0.0 does in IPv4. This is the common way to make a service reachable from the network.
import socket
server = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
server.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
server.bind(("::", 8080))
server.listen()
print("listening on", server.getsockname()[:2])
server.close()The IPV6_V6ONLY option controls a gotcha. On Linux, a socket bound to :: accepts IPv4 clients too by default, which appear as IPv4-mapped addresses such as ::ffff:192.0.2.7. Setting IPV6_V6ONLY to 1 restricts the socket to IPv6, so you can run a separate IPv4 listener on the same port. Defaults differ between operating systems, so set the option explicitly when it matters.
On Linux, ss -6 -ltn shows a socket bound to the wildcard as [::]:8080 (some versions print *:8080). A socket bound only to loopback shows [::1]:8080.
Do not confuse `::` and `::/0`
The notation ::/0 is the IPv6 default route. It is a /0 prefix and matches every destination. The address :: with a /128 prefix is a single address. The two share the characters :: but are unrelated.
ip -6 route show defaultA typical result is default via fe80::1 dev eth0. Here default is the same entry as ::/0.
Classifying both addresses in Python
The ipaddress module recognizes both addresses through separate properties.
import ipaddress
for text in ["::1", "::", "fe80::1", "2001:db8::1"]:
a = ipaddress.ip_address(text)
print(f"{text:12} loopback={a.is_loopback} unspecified={a.is_unspecified} global={a.is_global}")Expected output:
::1 loopback=True unspecified=False global=False
:: loopback=False unspecified=True global=False
fe80::1 loopback=False unspecified=False global=False
2001:db8::1 loopback=False unspecified=False global=FalseThe documentation address 2001:db8::1 is not global because that range is reserved for examples. Neither special address is globally routable.
Common errors
- Using
::1to test remote reachability. A successfulping ::1proves only that the local stack works. Ping your default gateway or a global address to test the network. - Binding to
::1and expecting remote access. Remote clients cannot reach a service bound to loopback. Bind to::or to a specific address instead. - Treating
::as a valid host address. A host cannot be configured with::, and nothing can send to it. - Reading
::in a log as an error. A source of::in a captured Neighbor Solicitation is normal DAD traffic. - Assuming a /8 of loopback addresses. Scripts that use
127.0.0.2for extra local addresses have no direct IPv6 equivalent. Use additional addresses on the loopback interface, such as unique local addresses, if you need several.
Exercise
A colleague runs a web service bound to ::1 on port 8080 and says other machines on the network cannot connect. Explain why, and name two ways to make the service reachable from the network.
Show answer
::1 is the loopback address. Traffic to it stays inside the host, and a router must drop any packet that carries it. Remote clients cannot reach the service. To expose it, bind the socket to :: (all IPv6 addresses of the host), or bind it to a specific non-loopback address such as a global unicast address of the host. Set IPV6_V6ONLY deliberately if IPv4 clients should not connect through the same socket.
Exercise
During Duplicate Address Detection, why does a host send its Neighbor Solicitation from :: instead of from the address it is testing?
Show answer
The address is tentative, so the host does not own it yet. Using it as a source before confirming it is unique could cause other nodes to cache a mapping for an address that another node may legitimately hold. Using :: signals that the sender has no usable address. A node that owns the tested address answers, and the host then abandons the address.