Skip to content

Commit eab6630

Browse files
committed
net: don't discourage private broadcast peers
Keep private broadcast connections outside normal discouragement handling. Misbehaving private broadcast peers are still disconnected.
1 parent f839ee1 commit eab6630

4 files changed

Lines changed: 44 additions & 5 deletions

File tree

‎src/net.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3924,13 +3924,13 @@ bool CConnman::DisconnectNode(std::string_view strNode)
39243924
return false;
39253925
}
39263926

3927-
bool CConnman::DisconnectNode(const CSubNet& subnet)
3927+
bool CConnman::DisconnectNode(const CSubNet& subnet, bool disconnect_private_broadcast)
39283928
{
39293929
AssertLockNotHeld(m_nodes_mutex);
39303930
bool disconnected = false;
39313931
LOCK(m_nodes_mutex);
39323932
for (CNode* pnode : m_nodes) {
3933-
if (subnet.Match(pnode->addr)) {
3933+
if (subnet.Match(pnode->addr) && (disconnect_private_broadcast || !pnode->IsPrivateBroadcastConn())) {
39343934
LogDebug(BCLog::NET, "disconnect by subnet%s match, %s", (fLogIPs ? strprintf("=%s", subnet.ToString()) : ""), pnode->DisconnectMsg());
39353935
pnode->fDisconnect = true;
39363936
disconnected = true;

‎src/net.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1385,7 +1385,7 @@ class CConnman
13851385
uint32_t GetMappedAS(const CNetAddr& addr) const;
13861386
void GetNodeStats(std::vector<CNodeStats>& vstats) const EXCLUSIVE_LOCKS_REQUIRED(!m_nodes_mutex);
13871387
bool DisconnectNode(std::string_view node) EXCLUSIVE_LOCKS_REQUIRED(!m_nodes_mutex);
1388-
bool DisconnectNode(const CSubNet& subnet) EXCLUSIVE_LOCKS_REQUIRED(!m_nodes_mutex);
1388+
bool DisconnectNode(const CSubNet& subnet, bool disconnect_private_broadcast = true) EXCLUSIVE_LOCKS_REQUIRED(!m_nodes_mutex);
13891389
bool DisconnectNode(const CNetAddr& addr) EXCLUSIVE_LOCKS_REQUIRED(!m_nodes_mutex);
13901390
bool DisconnectNode(NodeId id) EXCLUSIVE_LOCKS_REQUIRED(!m_nodes_mutex);
13911391

‎src/net_processing.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5389,6 +5389,12 @@ bool PeerManagerImpl::MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer)
53895389
return false;
53905390
}
53915391

5392+
if (pnode.IsPrivateBroadcastConn()) {
5393+
LogDebug(BCLog::PRIVBROADCAST, "Disconnecting misbehaving private broadcast peer=%d", peer.m_id);
5394+
pnode.fDisconnect = true;
5395+
return true;
5396+
}
5397+
53925398
if (pnode.addr.IsLocal()) {
53935399
// We disconnect local peers for bad behavior but don't discourage (since that would discourage
53945400
// all peers on the same local address)
@@ -5401,7 +5407,7 @@ bool PeerManagerImpl::MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer)
54015407
// Normal case: Disconnect the peer and discourage all nodes sharing the address
54025408
LogDebug(BCLog::NET, "Disconnecting and discouraging peer %d!\n", peer.m_id);
54035409
if (m_banman) m_banman->Discourage(pnode.addr);
5404-
m_connman.DisconnectNode(pnode.addr);
5410+
m_connman.DisconnectNode(CSubNet{pnode.addr}, /*disconnect_private_broadcast=*/false);
54055411
return true;
54065412
}
54075413

‎src/test/denialofservice_tests.cpp‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
319319

320320
const CNetAddr other_addr{ip(0xa0b0ff01)}; // Not any of addr[].
321321

322-
std::array<CNode*, 3> nodes;
322+
std::array<CNode*, 5> nodes;
323323

324324
banman->ClearBanned();
325325
NodeId id{0};
@@ -389,6 +389,20 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
389389
peerLogic->InitializeNode(*nodes[2], NODE_NETWORK);
390390
nodes[2]->fSuccessfullyConnected = true;
391391
connman->AddTestNode(*nodes[2]);
392+
393+
nodes[3] = new CNode{/*id=*/id++,
394+
/*sock=*/nullptr,
395+
/*addrIn=*/addr[2],
396+
/*nKeyedNetGroupIn=*/0,
397+
/*nLocalHostNonceIn=*/0,
398+
/*addrBindIn=*/CAddress{},
399+
/*addrNameIn=*/"",
400+
/*conn_type_in=*/ConnectionType::PRIVATE_BROADCAST,
401+
/*inbound_onion=*/false,
402+
/*network_key=*/0};
403+
peerLogic->InitializeNode(*nodes[3], NODE_NETWORK);
404+
connman->AddTestNode(*nodes[3]);
405+
392406
peerLogic->UnitTestMisbehaving(nodes[2]->GetId());
393407
BOOST_CHECK(peerLogic->SendMessages(*nodes[2]));
394408
BOOST_CHECK(banman->IsDiscouraged(addr[0]));
@@ -397,6 +411,25 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
397411
BOOST_CHECK(nodes[0]->fDisconnect);
398412
BOOST_CHECK(nodes[1]->fDisconnect);
399413
BOOST_CHECK(nodes[2]->fDisconnect);
414+
BOOST_CHECK(!nodes[3]->fDisconnect);
415+
416+
nodes[4] = new CNode{/*id=*/id++,
417+
/*sock=*/nullptr,
418+
/*addrIn=*/CAddress{CService{other_addr, Params().GetDefaultPort()}, NODE_NONE},
419+
/*nKeyedNetGroupIn=*/0,
420+
/*nLocalHostNonceIn=*/0,
421+
/*addrBindIn=*/CAddress{},
422+
/*addrNameIn=*/"",
423+
/*conn_type_in=*/ConnectionType::PRIVATE_BROADCAST,
424+
/*inbound_onion=*/false,
425+
/*network_key=*/0};
426+
peerLogic->InitializeNode(*nodes[4], NODE_NETWORK);
427+
connman->AddTestNode(*nodes[4]);
428+
429+
peerLogic->UnitTestMisbehaving(nodes[4]->GetId());
430+
BOOST_CHECK(peerLogic->SendMessages(*nodes[4]));
431+
BOOST_CHECK(nodes[4]->fDisconnect);
432+
BOOST_CHECK(!banman->IsDiscouraged(other_addr));
400433

401434
for (CNode* node : nodes) {
402435
peerLogic->FinalizeNode(*node);

0 commit comments

Comments
 (0)