Disclosure up front: I run IPYE, a company that leases IP space. This is a practical how-to, and IPYE comes up where it helps, with a short call to action at the end.
Linux can send traffic from any address in a routed IPv6 prefix without configuring each address one by one, thanks to the AnyIP local route and the IP_FREEBIND socket option. Combine that with a few lines of Python, and each request can use a different source address. That's useful for testing rate limiters, spreading crawler load across subnets, or simply keeping workers separate. This guide sets it up with a free IPv6 /48 from IPYE. The examples use the documentation prefix 2001:db8:100::/48, with 2001:db8:100:5::/64 set aside for the crawler.
Route the /64 to the host
If the /48 is announced from this host, or routed to it, add a local route for the /64:
ip -6 route add local 2001:db8:100:5::/64 dev lo
The local route tells the kernel that every address in the /64 belongs to this host, so it can send from any of them and receive replies to any of them. Replies arrive because upstream routing delivers the whole /48, or at least the /64, to this host.
Allow binding to addresses not on an interface
sysctl -w net.ipv6.ip_nonlocal_bind=1
Or set IP_FREEBIND on each socket, which avoids the global setting:
import socket, random, ipaddress
NET = ipaddress.IPv6Network("2001:db8:100:5::/64")
def random_addr():
return str(NET[random.getrandbits(64)])
def connect(host, port):
info = socket.getaddrinfo(host, port, socket.AF_INET6, socket.SOCK_STREAM)[0]
s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_IP, 15, 1) # IP_FREEBIND = 15 on Linux
s.bind((random_addr(), 0))
s.connect(info[4])
return s
With the requests library
Use a transport adapter that binds each new connection to a random source:
import requests
from requests.adapters import HTTPAdapter
from urllib3.poolmanager import PoolManager
class SourceAdapter(HTTPAdapter):
def __init__(self, src, **kw):
self.src = src
super().__init__(**kw)
def init_poolmanager(self, *a, **kw):
kw['source_address'] = (self.src, 0)
self.poolmanager = PoolManager(*a, **kw)
def session_with_random_source():
s = requests.Session()
s.mount('https://', SourceAdapter(random_addr()))
s.mount('http://', SourceAdapter(random_addr()))
return s
s = session_with_random_source()
print(s.get('https://ifconfig.co', timeout=10).text)
Run it a few times, and you'll see a different address from the /64 each time. With ip_nonlocal_bind=1 set, urllib3's bind succeeds even though the address isn't configured on an interface.
Why per-address rotation within a /64 rarely helps
Sites that handle IPv6 well rate-limit and block by /64, because they know any client can rotate within its /64. Random addresses within one /64 look like one client to them. To spread load properly, rotate across /64s:
BASE = ipaddress.IPv6Network("2001:db8:100::/48")
def random_addr_any_64():
subnet = random.randrange(0x0100, 0x0200) # the 256 /64s in 2001:db8:100:100::/56
iid = random.getrandbits(64)
return str(ipaddress.IPv6Address(int(BASE.network_address) | (subnet << 64) | iid))
Add a local route for that /56 (ip -6 route add local 2001:db8:100:100::/56 dev lo). And keep the total request rate per target site polite, because rotation is for spreading load, not for multiplying it.
Check the target supports IPv6
This only works for sites with AAAA records. Check first, and fall back to IPv4 for the rest:
socket.getaddrinfo('example.com', 443, socket.AF_INET6)
Reverse DNS
A wildcard-style PTR setup is impractical for 2^64 addresses. Either delegate the reverse zone to a server that generates answers on the fly, or accept that random addresses have no PTR. Some sites treat that as a negative signal.
Getting the prefix
A free /48 from IPYE gives you 65,536 /64s to work with, with a LOA and an automatically created ROA for your ASN.
Next step
Claim the free /48 in the IPYE portal, add a local route for the range you set aside, and test with the requests adapter above.
About IPYE
IPYE leases IPv4, IPv6 and ASN space with instant setup. Your first IPv6 /48 is free for the first year, a real allocation you can announce on your own ASN, with a LOA from the portal and the ROA created automatically. IPv4 /24 to /22 leases include LOA, ROA and per-/24 rDNS, and ASN and region changes apply automatically. Locations: Los Angeles, New York, Ashburn, Salt Lake City, London, Amsterdam, Frankfurt. More at ipye.com. Questions: ipye@turnel.com.
Disclosure: I run IPYE
Top comments (0)