Skip to content

bugfix: send shortened IPv6 addresses to SOCKS5 proxies in full - #36675

Open
instagibbs wants to merge 1 commit into
bitcoin:masterfrom
instagibbs:2026-10-socks5-ipv6-expand
Open

instagibbs wants to merge 1 commit into
bitcoin:masterfrom
instagibbs:2026-10-socks5-ipv6-expand

Conversation

@instagibbs

@instagibbs instagibbs commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Found while auditing #36628

Already known upstream issue with C-tor: https://gitlab.torproject.org/tpo/core/tor/-/issues/32314

With -proxy pointed at Tor, outbound connections to IPv6 peers whose
address contains an inner :: always fail with "general failure".

Core sends every SOCKS5 destination as a DOMAINNAME, with IPv6 in its
compressed form. Tor copies the name into its relay BEGIN cell as
<name>:<port>, and the exit's tor_addr_port_split first tries to parse
the whole string as an IPv6 address. 2001:db8::1:8333 is a valid one,
because the :: absorbs the port as an eighth group. The exit then sees no
port and closes the stream. Addresses written with all eight groups, or
ending in ::, are unaffected: appending :port to them does not give a
valid address.

About 20% of the IPv6 addresses in a gossip-filled addrman, and 29% of
current IPv6 DNS seed answers, have an inner ::. The failure is closed;
there is no fallback to a direct connection.

Note that this also effects private broadcast by default.

This PR sends such addresses with all eight groups written out
(2001:db8:0:0:0:0:0:1). <name>:<port> then has nine groups, so the exit
splits at the last colon. Other forms:

Form Tor Tor, SafeSocks 1 getaddrinfo-based proxies
2001:db8::1 (today) fails fails works
[2001:db8::1] works works fails
ATYP IPv6 works, logs a DNS-leak warning refused works
eight groups (this PR) works works works

Tested against tor 0.4.9.13: on mainnet, master could not reach four
seed-provided peers with inner-:: addresses (30 "general failure"); with
this change all four connected and completed the version handshake.
feature_proxy.py and a new netbase_tests case check the bytes sent.

@DrahtBot

DrahtBot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK andrewtoth, jpk68
Concept ACK darosior

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36994 (net: reduce ASMap lookup and address grouping costs by l0rinc)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@darosior darosior left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Workaround seems fine to do. But it's surprising the Tor daemon does not follow its own spec and format the IPv6 with brackets.

EDIT: had bot lookup the Tor sources and even the latest version still formats IPv6:port ambiguously, contrary to the spec. Arti does implement the spec correctly. May be worth upstreaming a bug report.

@instagibbs
instagibbs force-pushed the 2026-10-socks5-ipv6-expand branch from 592d277 to 721e2f5 Compare October 27, 2026 14:52
@instagibbs

Copy link
Copy Markdown
Member Author

@darosior https://gitlab.torproject.org/tpo/core/tor/-/issues/32314 already known and closed. "Just use Arti"

@darosior

Copy link
Copy Markdown
Member

Concept ACK

@instagibbs instagibbs changed the title netbase: send shortened IPv6 addresses to SOCKS5 proxies in full bugfix: send shortened IPv6 addresses to SOCKS5 proxies in full Oct 27, 2026

@andrewtoth andrewtoth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 721e2f5

Nice find. Tested with a small patch to only select IPV6 peers via private broadcast. Many SOCKS connection failures to addresses with :: on master, successfully connected to an address with :: with this patch.

This will also cause issues with anyone using the -proxy= with tor feature.

I see from a cursory analysis in bitnod.es that this would fail to connect to ~50% of known reachable ipv6 peers.

Comment thread src/netbase.cpp Resolved
assert_equal(cmd.port, 5443)
rv.append(cmd)
self.network_test(node, addr, network=NET_IPV6)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could dedupe this with the above test.

diff --git a/test/functional/feature_proxy.py b/test/functional/feature_proxy.py
index 67274f9648..719737b340 100755
--- a/test/functional/feature_proxy.py
+++ b/test/functional/feature_proxy.py
@@ -164,15 +164,18 @@ class ProxyTest(BitcoinTestFramework):
         rv.append(cmd)
         self.network_test(node, addr, network=NET_IPV4)
 
-        if self.have_ipv6:
-            addr = "[1233:3432:2434:2343:3234:2345:6546:4534]:5443"
+        for host, expected in [
+            ("1233:3432:2434:2343:3234:2345:6546:4534", b"1233:3432:2434:2343:3234:2345:6546:4534"),
+            ("1233:3432::6546:4534", b"1233:3432:0:0:0:0:6546:4534"),
+        ]:
+            addr = f"[{host}]:5443"
             self.log.debug(f"Test: outgoing IPv6 connection through node {node.index} for address {addr}")
             node.addnode(addr, "onetry", v2transport=False)
             cmd = proxies[1].queue.get()
             assert isinstance(cmd, Socks5Command)
             # Note: bitcoind's SOCKS5 implementation only sends atyp DOMAINNAME, even if connecting directly to IPv4/IPv6
             assert_equal(cmd.atyp, AddressType.DOMAINNAME)
-            assert_equal(cmd.addr, b"1233:3432:2434:2343:3234:2345:6546:4534")
+            assert_equal(cmd.addr, expected)
             assert_equal(cmd.port, 5443)
             if not auth:
                 assert_equal(cmd.username, None)
@@ -180,18 +183,6 @@ class ProxyTest(BitcoinTestFramework):
             rv.append(cmd)
             self.network_test(node, addr, network=NET_IPV6)
 
-            addr = "[1233:3432::6546:4534]:5443"
-            self.log.debug(f"Test: outgoing IPv6 connection through node {node.index} for shortened address {addr}")
-            node.addnode(addr, "onetry", v2transport=False)
-            cmd = proxies[1].queue.get()
-            assert isinstance(cmd, Socks5Command)
-            # A shortened IPv6 address is sent with all eight groups, so that Tor does not read "<addr>:<port>" as one address
-            assert_equal(cmd.atyp, AddressType.DOMAINNAME)
-            assert_equal(cmd.addr, b"1233:3432:0:0:0:0:6546:4534")
-            assert_equal(cmd.port, 5443)
-            rv.append(cmd)
-            self.network_test(node, addr, network=NET_IPV6)
-
         if test_onion:
             addr = "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:8333"
             self.log.debug(f"Test: outgoing onion connection through node {node.index} for address {addr}")

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

applied but kept the have_ipv6 guard

@DrahtBot
DrahtBot requested a review from darosior November 4, 2026 00:18
Tor copies a SOCKS5 DOMAINNAME into its relay BEGIN cell as
"<name>:<port>", and an exit parses "2001:db8::1:8333" as an IPv6
address with no port and closes the stream. Outbound connections
through a Tor -proxy to IPv6 addresses that contain "::" therefore fail
with "general failure". About a third of the IPv6 addresses the DNS
seeds return have this form.

Write such addresses with all eight groups. "<name>:<port>" is then not
an IPv6 address, so the exit splits it at the last colon, and the name
is still an address to proxies that resolve it with getaddrinfo, which
brackets would break.
@instagibbs
instagibbs force-pushed the 2026-10-socks5-ipv6-expand branch from 721e2f5 to e8469d7 Compare November 4, 2026 16:23

@andrewtoth andrewtoth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reACK e8469d7

@jpk68

jpk68 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

ACK e8469d7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants