Branch data Line data Source code
1 : : // Copyright (c) 2021-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <netgroup.h>
6 : :
7 : : #include <hash.h>
8 : : #include <uint256.h>
9 : : #include <util/asmap.h>
10 : : #include <util/log.h>
11 : :
12 : : #include <cassert>
13 : : #include <cstddef>
14 : : #include <utility>
15 : :
16 : 350 : NetGroupManager::NetGroupManager(std::span<const std::byte> embedded_asmap, std::vector<std::byte>&& loaded_asmap)
17 : 350 : : m_asmap{embedded_asmap},
18 [ + - ]: 350 : m_loaded_asmap{std::move(loaded_asmap)},
19 [ + - ]: 350 : m_asmap_version{AsmapVersion(m_asmap)}
20 : : {
21 [ - + - - ]: 350 : assert(m_loaded_asmap.empty() || m_asmap.data() == m_loaded_asmap.data());
22 : 350 : }
23 : :
24 : 16 : uint256 NetGroupManager::GetAsmapVersion() const
25 : : {
26 : 16 : return m_asmap_version;
27 : : }
28 : :
29 : 20754 : std::vector<unsigned char> NetGroupManager::GetGroup(const CNetAddr& address) const
30 : : {
31 : 20754 : std::vector<unsigned char> vchRet;
32 : : // If non-empty asmap is supplied and the address is IPv4/IPv6,
33 : : // return ASN to be used for bucketing.
34 [ + - ]: 20754 : uint32_t asn = GetMappedAS(address);
35 [ + + ]: 20754 : if (asn != 0) { // Either asmap was empty, or address has non-asmappable net class (e.g. TOR).
36 [ + - ]: 3628 : vchRet.push_back(NET_IPV6); // IPv4 and IPv6 with same ASN should be in the same bucket
37 [ + + ]: 18140 : for (int i = 0; i < 4; i++) {
38 [ + - ]: 14512 : vchRet.push_back((asn >> (8 * i)) & 0xFF);
39 : : }
40 : : return vchRet;
41 : : }
42 : :
43 [ + - + - ]: 17126 : vchRet.push_back(address.GetNetClass());
44 : 17126 : int nStartByte{0};
45 : 17126 : int nBits{0};
46 : :
47 [ + - + + ]: 17126 : if (address.IsLocal()) {
48 : : // all local addresses belong to the same group
49 [ + - + + ]: 17125 : } else if (address.IsInternal()) {
50 : : // All internal-usage addresses get their own group.
51 : : // Skip over the INTERNAL_IN_IPV6_PREFIX returned by CAddress::GetAddrBytes().
52 : : nStartByte = INTERNAL_IN_IPV6_PREFIX.size();
53 : : nBits = ADDR_INTERNAL_SIZE * 8;
54 [ + - + + ]: 17124 : } else if (!address.IsRoutable()) {
55 : : // all other unroutable addresses belong to the same group
56 [ + - + + ]: 17098 : } else if (address.HasLinkedIPv4()) {
57 : : // IPv4 addresses (and mapped IPv4 addresses) use /16 groups
58 [ + - ]: 17085 : uint32_t ipv4 = address.GetLinkedIPv4();
59 [ + - ]: 17085 : vchRet.push_back((ipv4 >> 24) & 0xFF);
60 [ + - ]: 20754 : vchRet.push_back((ipv4 >> 16) & 0xFF);
61 : : return vchRet;
62 [ + - + + ]: 13 : } else if (address.IsTor() || address.IsI2P()) {
63 : : nBits = 4;
64 [ + - ]: 2 : } else if (address.IsCJDNS()) {
65 : : // Treat in the same way as Tor and I2P because the address in all of
66 : : // them is "random" bytes (derived from a public key). However in CJDNS
67 : : // the first byte is a constant (see CJDNS_PREFIX), so the random bytes
68 : : // come after it. Thus skip the constant 8 bits at the start.
69 : : nBits = 12;
70 [ + - + + ]: 2 : } else if (address.IsHeNet()) {
71 : : // for he.net, use /36 groups
72 : : nBits = 36;
73 : : } else {
74 : : // for the rest of the IPv6 network, use /32 groups
75 : 1 : nBits = 32;
76 : : }
77 : :
78 : : // Push our address onto vchRet.
79 [ + - ]: 41 : auto addr_bytes = address.GetAddrBytes();
80 : 41 : const size_t num_bytes = nBits / 8;
81 [ + - ]: 41 : vchRet.insert(vchRet.end(), addr_bytes.begin() + nStartByte, addr_bytes.begin() + nStartByte + num_bytes);
82 : 41 : nBits %= 8;
83 : : // ...for the last byte, push nBits and for the rest of the byte push 1's
84 [ + + ]: 41 : if (nBits > 0) {
85 [ - + - + ]: 12 : assert(num_bytes < addr_bytes.size());
86 [ + - ]: 12 : vchRet.push_back(addr_bytes[num_bytes + nStartByte] | ((1 << (8 - nBits)) - 1));
87 : : }
88 : :
89 : 41 : return vchRet;
90 : 41 : }
91 : :
92 : 23420 : uint32_t NetGroupManager::GetMappedAS(const CNetAddr& address) const
93 : : {
94 : 23420 : uint32_t net_class = address.GetNetClass();
95 [ + + + + ]: 23420 : if (m_asmap.empty() || (net_class != NET_IPV4 && net_class != NET_IPV6)) {
96 : : return 0; // Indicates not found, safe because AS0 is reserved per RFC7607.
97 : : }
98 : 4126 : std::vector<std::byte> ip_bytes(16);
99 [ + - + + ]: 4126 : if (address.HasLinkedIPv4()) {
100 : : // For lookup, treat as if it was just an IPv4 address (IPV4_IN_IPV6_PREFIX + IPv4 bits)
101 : 4107 : std::copy_n(std::as_bytes(std::span{IPV4_IN_IPV6_PREFIX}).begin(),
102 : : IPV4_IN_IPV6_PREFIX.size(), ip_bytes.begin());
103 [ + - ]: 4107 : uint32_t ipv4 = address.GetLinkedIPv4();
104 [ + + ]: 20535 : for (int i = 0; i < 4; ++i) {
105 : 16428 : ip_bytes[12 + i] = std::byte((ipv4 >> (24 - i * 8)) & 0xFF);
106 : : }
107 : : } else {
108 : : // Use all 128 bits of the IPv6 address otherwise
109 [ - + ]: 19 : assert(address.IsIPv6());
110 [ + - ]: 19 : auto addr_bytes = address.GetAddrBytes();
111 [ - + - + : 19 : assert(addr_bytes.size() == ip_bytes.size());
- + ]
112 : 19 : std::copy_n(std::as_bytes(std::span{addr_bytes}).begin(),
113 : : addr_bytes.size(), ip_bytes.begin());
114 : 19 : }
115 [ - + + - ]: 4126 : uint32_t mapped_as = Interpret(m_asmap, ip_bytes);
116 : 4126 : return mapped_as;
117 : 4126 : }
118 : :
119 : 0 : void NetGroupManager::ASMapHealthCheck(const std::vector<CNetAddr>& clearnet_addrs) const {
120 : 0 : std::set<uint32_t> clearnet_asns{};
121 : 0 : int unmapped_count{0};
122 : :
123 [ # # ]: 0 : for (const auto& addr : clearnet_addrs) {
124 [ # # ]: 0 : uint32_t asn = GetMappedAS(addr);
125 [ # # ]: 0 : if (asn == 0) {
126 : 0 : ++unmapped_count;
127 : 0 : continue;
128 : : }
129 [ # # ]: 0 : clearnet_asns.insert(asn);
130 : : }
131 : :
132 [ # # # # ]: 0 : LogInfo("ASMap Health Check: %i clearnet peers are mapped to %i ASNs with %i peers being unmapped\n", clearnet_addrs.size(), clearnet_asns.size(), unmapped_count);
133 : 0 : }
134 : :
135 : 5 : bool NetGroupManager::UsingASMap() const {
136 : 5 : return m_asmap.size() > 0;
137 : : }
|