Bitcoin Core  0.20.99
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or
8 #include <addrdb.h>
9 #include <bloom.h>
10 #include <fs.h>
11 #include <net_types.h> // For banmap_t
12 #include <sync.h>
14 #include <chrono>
15 #include <cstdint>
16 #include <memory>
18 // NOTE: When adjusting this, update rpcnet:setban's help ("24h")
19 static constexpr unsigned int DEFAULT_MISBEHAVING_BANTIME = 60 * 60 * 24; // Default 24-hour ban
20 // How often to dump addresses to banlist.dat
21 static constexpr std::chrono::minutes DUMP_BANS_INTERVAL{15};
23 class CClientUIInterface;
24 class CNetAddr;
25 class CSubNet;
27 // Banman manages two related but distinct concepts:
28 //
29 // 1. Banning. This is configured manually by the user, through the setban RPC.
30 // If an address or subnet is banned, we never accept incoming connections from
31 // it and never create outgoing connections to it. We won't gossip its address
32 // to other peers in addr messages. Banned addresses and subnets are stored to
33 // banlist.dat on shutdown and reloaded on startup. Banning can be used to
34 // prevent connections with spy nodes or other griefers.
35 //
36 // 2. Discouragement. If a peer misbehaves enough (see Misbehaving() in
37 // net_processing.cpp), we'll mark that address as discouraged. We still allow
38 // incoming connections from them, but they're preferred for eviction when
39 // we receive new incoming connections. We never make outgoing connections to
40 // them, and do not gossip their address to other peers. This is implemented as
41 // a bloom filter. We can (probabilistically) test for membership, but can't
42 // list all discouraged addresses or unmark them as discouraged. Discouragement
43 // can prevent our limited connection slots being used up by incompatible
44 // or broken peers.
45 //
46 // Neither banning nor discouragement are protections against denial-of-service
47 // attacks, since if an attacker has a way to waste our resources and we
48 // disconnect from them and ban that address, it's trivial for them to
49 // reconnect from another IP address.
50 //
51 // Attempting to automatically disconnect or ban any class of peer carries the
52 // risk of splitting the network. For example, if we banned/disconnected for a
53 // transaction that fails a policy check and a future version changes the
54 // policy check so the transaction is accepted, then that transaction could
55 // cause the network to split between old nodes and new nodes.
57 class BanMan
58 {
59 public:
60  ~BanMan();
61  BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time);
62  void Ban(const CNetAddr& net_addr, int64_t ban_time_offset = 0, bool since_unix_epoch = false);
63  void Ban(const CSubNet& sub_net, int64_t ban_time_offset = 0, bool since_unix_epoch = false);
64  void Discourage(const CNetAddr& net_addr);
65  void ClearBanned();
68  bool IsBanned(const CNetAddr& net_addr);
71  bool IsBanned(const CSubNet& sub_net);
74  bool IsDiscouraged(const CNetAddr& net_addr);
76  bool Unban(const CNetAddr& net_addr);
77  bool Unban(const CSubNet& sub_net);
78  void GetBanned(banmap_t& banmap);
79  void DumpBanlist();
81 private:
82  void SetBanned(const banmap_t& banmap);
83  bool BannedSetIsDirty();
85  void SetBannedSetDirty(bool dirty = true);
87  void SweepBanned();
90  banmap_t m_banned GUARDED_BY(m_cs_banned);
91  bool m_is_dirty GUARDED_BY(m_cs_banned);
94  const int64_t m_default_ban_time;
95  CRollingBloomFilter m_discouraged GUARDED_BY(m_cs_banned) {50000, 0.000001};
96 };
98 #endif
void Ban(const CNetAddr &net_addr, int64_t ban_time_offset=0, bool since_unix_epoch=false)
Definition: banman.cpp:106
void Discourage(const CNetAddr &net_addr)
Definition: banman.cpp:112
Definition: banman.h:57
void SetBanned(const banmap_t &banmap)
Definition: banman.cpp:170
CClientUIInterface * m_client_interface
Definition: banman.h:92
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set. ...
Definition: bloom.h:99
void SetBannedSetDirty(bool dirty=true)
set the "dirty" flag for the banlist
Definition: banman.cpp:208
Signals for UI communication.
Definition: ui_interface.h:24
Definition: banman.cpp:37
void ClearBanned()
Definition: banman.cpp:60
Access to the banlist database (banlist.dat)
Definition: addrdb.h:66
bool IsDiscouraged(const CNetAddr &net_addr)
Return whether net_addr is discouraged.
Definition: banman.cpp:71
banmap_t m_banned GUARDED_BY(m_cs_banned)
void SweepBanned()
clean unused entries (if bantime has expired)
Definition: banman.cpp:177
static constexpr std::chrono::minutes DUMP_BANS_INTERVAL
Definition: banman.h:21
bool BannedSetIsDirty()
Definition: banman.cpp:202
RecursiveMutex m_cs_banned
Definition: banman.h:89
CBanDB m_ban_db
Definition: banman.h:93
void DumpBanlist()
Definition: banman.cpp:42
void GetBanned(banmap_t &banmap)
Definition: banman.cpp:162
CRollingBloomFilter m_discouraged GUARDED_BY(m_cs_banned)
Definition: banman.h:95
const int64_t m_default_ban_time
Definition: banman.h:94
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
Definition: netaddress.h:31
static constexpr unsigned int DEFAULT_MISBEHAVING_BANTIME
Definition: banman.h:19
bool Unban(const CNetAddr &net_addr)
Definition: banman.cpp:144
BanMan(fs::path ban_file, CClientUIInterface *client_interface, int64_t default_ban_time)
Definition: banman.cpp:15
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:13
bool IsBanned(const CNetAddr &net_addr)
Return whether net_addr is banned.
Definition: banman.cpp:77