Skip to content

Commit 2bfe43d

Browse files
committed
Merge bitcoin/bitcoin#27374: p2p: skip netgroup diversity of new connections for tor/i2p/cjdns
b5585ba p2p: skip netgroup diversity of new connections for tor/i2p/cjdns networks (stratospher) Pull request description: Follow up for #27264. In order to make sure that our persistent outbound slots belong to different netgroups, distinct net groups of our peers are added to `setConnected`. We’d only open a persistent outbound connection to peers which have a different netgroup compared to those netgroups present in `setConnected`. Current `GetGroup()` logic assumes route-based diversification behaviour for tor/i2p/cjdns addresses (addresses are public key based and not route-based). Distinct netgroups possible (according to the current `GetGroup()` logic) for: 1. tor => 030f, 031f, .. 03ff (16 possibilities) 2. i2p => 040f, 041f, .. 04ff (16 possibilities) 3. cjdns => 05fc0f, 05fc1f, ... 05fcff (16 possibilities) `setConnected` is used in `ThreadOpenConnections()` before making [outbound](https://github.com/bitcoin/bitcoin/blob/84f4ac39fda7ffa5dc84e92d92dd1eeeb5e20f8c/src/net.cpp#L1846) and [anchor](https://github.com/bitcoin/bitcoin/blob/84f4ac39fda7ffa5dc84e92d92dd1eeeb5e20f8c/src/net.cpp#L1805) connections to new peers so that they belong to distinct netgroups. **behaviour on master** - if we run a node only on tor/i2p/cjdns - we wouldn't be able to open more than 16 outbound connections(manual, block-relay-only anchor, outbound full relay, block-relay-only connections) because we run out of possible netgroups. - see bitcoin/bitcoin#27264 (comment) - tested by changing `MAX_OUTBOUND_FULL_RELAY_CONNECTIONS` to 17 with `onlynet=onion` and observed how node wouldn't make more than 16 outbound connections. **behaviour on PR** - netgroup diversity checks are skipped for tor/i2p/cjdns addresses. - we don't insert tor/i2p/cjdns address in `setConnected` and `GetGroup` doesn't get called on tor/i2p/cjdns(see #27369) ACKs for top commit: achow101: ACK b5585ba mzumsande: ACK b5585ba vasild: ACK b5585ba Tree-SHA512: c120b3f9ca7f0be3f29ea665cd2f7dfb40cd1d7ec7058984252fb6e0295e414f736c5b4fba03c31188188a5ae4f543fb2654f6ee9776bad745c7ca72d23d5b9b
2 parents 19764dc + b5585ba commit 2bfe43d

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/net.cpp

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,18 +1703,19 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17031703
//
17041704
CAddress addrConnect;
17051705

1706-
// Only connect out to one peer per network group (/16 for IPv4).
1706+
// Only connect out to one peer per ipv4/ipv6 network group (/16 for IPv4).
17071707
int nOutboundFullRelay = 0;
17081708
int nOutboundBlockRelay = 0;
1709-
std::set<std::vector<unsigned char> > setConnected;
1709+
int outbound_privacy_network_peers = 0;
1710+
std::set<std::vector<unsigned char>> setConnected; // netgroups of our ipv4/ipv6 outbound peers
17101711

17111712
{
17121713
LOCK(m_nodes_mutex);
17131714
for (const CNode* pnode : m_nodes) {
17141715
if (pnode->IsFullOutboundConn()) nOutboundFullRelay++;
17151716
if (pnode->IsBlockOnlyConn()) nOutboundBlockRelay++;
17161717

1717-
// Make sure our persistent outbound slots belong to different netgroups.
1718+
// Make sure our persistent outbound slots to ipv4/ipv6 peers belong to different netgroups.
17181719
switch (pnode->m_conn_type) {
17191720
// We currently don't take inbound connections into account. Since they are
17201721
// free to make, an attacker could make them to prevent us from connecting to
@@ -1728,7 +1729,19 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17281729
case ConnectionType::MANUAL:
17291730
case ConnectionType::OUTBOUND_FULL_RELAY:
17301731
case ConnectionType::BLOCK_RELAY:
1731-
setConnected.insert(m_netgroupman.GetGroup(pnode->addr));
1732+
CAddress address{pnode->addr};
1733+
if (address.IsTor() || address.IsI2P() || address.IsCJDNS()) {
1734+
// Since our addrman-groups for these networks are
1735+
// random, without relation to the route we
1736+
// take to connect to these peers or to the
1737+
// difficulty in obtaining addresses with diverse
1738+
// groups, we don't worry about diversity with
1739+
// respect to our addrman groups when connecting to
1740+
// these networks.
1741+
++outbound_privacy_network_peers;
1742+
} else {
1743+
setConnected.insert(m_netgroupman.GetGroup(address));
1744+
}
17321745
} // no default case, so the compiler can warn about missing cases
17331746
}
17341747
}
@@ -1886,8 +1899,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18861899
}
18871900
LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToStringAddrPort());
18881901
}
1889-
1890-
OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant, nullptr, conn_type);
1902+
// Record addrman failure attempts when node has at least 2 persistent outbound connections to peers with
1903+
// different netgroups in ipv4/ipv6 networks + all peers in Tor/I2P/CJDNS networks.
1904+
// Don't record addrman failure attempts when node is offline. This can be identified since all local
1905+
// network connections(if any) belong in the same netgroup and size of setConnected would only be 1.
1906+
OpenNetworkConnection(addrConnect, (int)setConnected.size() + outbound_privacy_network_peers >= std::min(nMaxConnections - 1, 2), &grant, nullptr, conn_type);
18911907
}
18921908
}
18931909
}

0 commit comments

Comments
 (0)