pcp.cpp
1 // Copyright (c) 2024-present The Bitcoin Core developers 2 // Distributed under the MIT software license, see the accompanying 3 // file COPYING or https://www.opensource.org/licenses/mit-license.php. 4 5 #include <common/pcp.h> 6 7 #include <common/netif.h> 8 #include <crypto/common.h> 9 #include <logging.h> 10 #include <netaddress.h> 11 #include <netbase.h> 12 #include <random.h> 13 #include <span.h> 14 #include <util/check.h> 15 #include <util/readwritefile.h> 16 #include <util/sock.h> 17 #include <util/strencodings.h> 18 #include <util/threadinterrupt.h> 19 20 namespace { 21 22 // RFC6886 NAT-PMP and RFC6887 Port Control Protocol (PCP) implementation. 23 // NAT-PMP and PCP use network byte order (big-endian). 24 25 // NAT-PMP (v0) protocol constants. 26 //! NAT-PMP uses a fixed server port number (RFC6887 section 1.1). 27 constexpr uint16_t NATPMP_SERVER_PORT = 5351; 28 //! Version byte for NATPMP (RFC6886 1.1) 29 constexpr uint8_t NATPMP_VERSION = 0; 30 //! Request opcode base (RFC6886 3). 31 constexpr uint8_t NATPMP_REQUEST = 0x00; 32 //! Response opcode base (RFC6886 3). 33 constexpr uint8_t NATPMP_RESPONSE = 0x80; 34 //! Get external address (RFC6886 3.2) 35 constexpr uint8_t NATPMP_OP_GETEXTERNAL = 0x00; 36 //! Map TCP port (RFC6886 3.3) 37 constexpr uint8_t NATPMP_OP_MAP_TCP = 0x02; 38 //! Shared request header size in bytes. 39 constexpr size_t NATPMP_REQUEST_HDR_SIZE = 2; 40 //! Shared response header (minimum) size in bytes. 41 constexpr size_t NATPMP_RESPONSE_HDR_SIZE = 8; 42 //! GETEXTERNAL request size in bytes, including header (RFC6886 3.2). 43 constexpr size_t NATPMP_GETEXTERNAL_REQUEST_SIZE = NATPMP_REQUEST_HDR_SIZE + 0; 44 //! GETEXTERNAL response size in bytes, including header (RFC6886 3.2). 45 constexpr size_t NATPMP_GETEXTERNAL_RESPONSE_SIZE = NATPMP_RESPONSE_HDR_SIZE + 4; 46 //! MAP request size in bytes, including header (RFC6886 3.3). 47 constexpr size_t NATPMP_MAP_REQUEST_SIZE = NATPMP_REQUEST_HDR_SIZE + 10; 48 //! MAP response size in bytes, including header (RFC6886 3.3). 49 constexpr size_t NATPMP_MAP_RESPONSE_SIZE = NATPMP_RESPONSE_HDR_SIZE + 8; 50 51 // Shared header offsets (RFC6886 3.2, 3.3), relative to start of packet. 52 //! Offset of version field in packets. 53 constexpr size_t NATPMP_HDR_VERSION_OFS = 0; 54 //! Offset of opcode field in packets 55 constexpr size_t NATPMP_HDR_OP_OFS = 1; 56 //! Offset of result code in packets. Result codes are 16 bit in NAT-PMP instead of 8 bit in PCP. 57 constexpr size_t NATPMP_RESPONSE_HDR_RESULT_OFS = 2; 58 59 // GETEXTERNAL response offsets (RFC6886 3.2), relative to start of packet. 60 //! Returned external address 61 constexpr size_t NATPMP_GETEXTERNAL_RESPONSE_IP_OFS = 8; 62 63 // MAP request offsets (RFC6886 3.3), relative to start of packet. 64 //! Internal port to be mapped. 65 constexpr size_t NATPMP_MAP_REQUEST_INTERNAL_PORT_OFS = 4; 66 //! Suggested external port for mapping. 67 constexpr size_t NATPMP_MAP_REQUEST_EXTERNAL_PORT_OFS = 6; 68 //! Requested port mapping lifetime in seconds. 69 constexpr size_t NATPMP_MAP_REQUEST_LIFETIME_OFS = 8; 70 71 // MAP response offsets (RFC6886 3.3), relative to start of packet. 72 //! Internal port for mapping (will match internal port of request). 73 constexpr size_t NATPMP_MAP_RESPONSE_INTERNAL_PORT_OFS = 8; 74 //! External port for mapping. 75 constexpr size_t NATPMP_MAP_RESPONSE_EXTERNAL_PORT_OFS = 10; 76 //! Created port mapping lifetime in seconds. 77 constexpr size_t NATPMP_MAP_RESPONSE_LIFETIME_OFS = 12; 78 79 // Relevant NETPMP result codes (RFC6886 3.5). 80 //! Result code representing success status. 81 constexpr uint8_t NATPMP_RESULT_SUCCESS = 0; 82 //! Result code representing unsupported version. 83 constexpr uint8_t NATPMP_RESULT_UNSUPP_VERSION = 1; 84 //! Result code representing lack of resources. 85 constexpr uint8_t NATPMP_RESULT_NO_RESOURCES = 4; 86 87 //! Mapping of NATPMP result code to string (RFC6886 3.5). Result codes <=2 match PCP. 88 const std::map<uint16_t, std::string> NATPMP_RESULT_STR{ 89 {0, "SUCCESS"}, 90 {1, "UNSUPP_VERSION"}, 91 {2, "NOT_AUTHORIZED"}, 92 {3, "NETWORK_FAILURE"}, 93 {4, "NO_RESOURCES"}, 94 {5, "UNSUPP_OPCODE"}, 95 }; 96 97 // PCP (v2) protocol constants. 98 //! Maximum packet size in bytes (RFC6887 section 7). 99 constexpr size_t PCP_MAX_SIZE = 1100; 100 //! PCP uses a fixed server port number (RFC6887 section 19.1). Shared with NAT-PMP. 101 constexpr uint16_t PCP_SERVER_PORT = NATPMP_SERVER_PORT; 102 //! Version byte. 0 is NAT-PMP (RFC6886), 1 is forbidden, 2 for PCP (RFC6887). 103 constexpr uint8_t PCP_VERSION = 2; 104 //! PCP Request Header. See RFC6887 section 7.1. Shared with NAT-PMP. 105 constexpr uint8_t PCP_REQUEST = NATPMP_REQUEST; // R = 0 106 //! PCP Response Header. See RFC6887 section 7.2. Shared with NAT-PMP. 107 constexpr uint8_t PCP_RESPONSE = NATPMP_RESPONSE; // R = 1 108 //! Map opcode. See RFC6887 section 19.2 109 constexpr uint8_t PCP_OP_MAP = 0x01; 110 //! TCP protocol number (IANA). 111 constexpr uint16_t PCP_PROTOCOL_TCP = 6; 112 //! Request and response header size in bytes (RFC6887 section 7.1). 113 constexpr size_t PCP_HDR_SIZE = 24; 114 //! Map request and response size in bytes (RFC6887 section 11.1). 115 constexpr size_t PCP_MAP_SIZE = 36; 116 117 // Header offsets shared between request and responses (RFC6887 7.1, 7.2), relative to start of packet. 118 //! Version field (1 byte). 119 constexpr size_t PCP_HDR_VERSION_OFS = NATPMP_HDR_VERSION_OFS; 120 //! Opcode field (1 byte). 121 constexpr size_t PCP_HDR_OP_OFS = NATPMP_HDR_OP_OFS; 122 //! Requested lifetime (request), granted lifetime (response) (4 bytes). 123 constexpr size_t PCP_HDR_LIFETIME_OFS = 4; 124 125 // Request header offsets (RFC6887 7.1), relative to start of packet. 126 //! PCP client's IP address (16 bytes). 127 constexpr size_t PCP_REQUEST_HDR_IP_OFS = 8; 128 129 // Response header offsets (RFC6887 7.2), relative to start of packet. 130 //! Result code (1 byte). 131 constexpr size_t PCP_RESPONSE_HDR_RESULT_OFS = 3; 132 133 // MAP request/response offsets (RFC6887 11.1), relative to start of opcode-specific data. 134 //! Mapping nonce (12 bytes). 135 constexpr size_t PCP_MAP_NONCE_OFS = 0; 136 //! Protocol (1 byte). 137 constexpr size_t PCP_MAP_PROTOCOL_OFS = 12; 138 //! Internal port for mapping (2 bytes). 139 constexpr size_t PCP_MAP_INTERNAL_PORT_OFS = 16; 140 //! Suggested external port (request), assigned external port (response) (2 bytes). 141 constexpr size_t PCP_MAP_EXTERNAL_PORT_OFS = 18; 142 //! Suggested external IP (request), assigned external IP (response) (16 bytes). 143 constexpr size_t PCP_MAP_EXTERNAL_IP_OFS = 20; 144 145 //! Result code representing success (RFC6887 7.4), shared with NAT-PMP. 146 constexpr uint8_t PCP_RESULT_SUCCESS = NATPMP_RESULT_SUCCESS; 147 //! Result code representing lack of resources (RFC6887 7.4). 148 constexpr uint8_t PCP_RESULT_NO_RESOURCES = 8; 149 150 //! Mapping of PCP result code to string (RFC6887 7.4). Result codes <=2 match NAT-PMP. 151 const std::map<uint8_t, std::string> PCP_RESULT_STR{ 152 {0, "SUCCESS"}, 153 {1, "UNSUPP_VERSION"}, 154 {2, "NOT_AUTHORIZED"}, 155 {3, "MALFORMED_REQUEST"}, 156 {4, "UNSUPP_OPCODE"}, 157 {5, "UNSUPP_OPTION"}, 158 {6, "MALFORMED_OPTION"}, 159 {7, "NETWORK_FAILURE"}, 160 {8, "NO_RESOURCES"}, 161 {9, "UNSUPP_PROTOCOL"}, 162 {10, "USER_EX_QUOTA"}, 163 {11, "CANNOT_PROVIDE_EXTERNAL"}, 164 {12, "ADDRESS_MISMATCH"}, 165 {13, "EXCESSIVE_REMOTE_PEER"}, 166 }; 167 168 //! Return human-readable string from NATPMP result code. 169 std::string NATPMPResultString(uint16_t result_code) 170 { 171 auto result_i = NATPMP_RESULT_STR.find(result_code); 172 return strprintf("%s (code %d)", result_i == NATPMP_RESULT_STR.end() ? "(unknown)" : result_i->second, result_code); 173 } 174 175 //! Return human-readable string from PCP result code. 176 std::string PCPResultString(uint8_t result_code) 177 { 178 auto result_i = PCP_RESULT_STR.find(result_code); 179 return strprintf("%s (code %d)", result_i == PCP_RESULT_STR.end() ? "(unknown)" : result_i->second, result_code); 180 } 181 182 //! Wrap address in IPv6 according to RFC6887. wrapped_addr needs to be able to store 16 bytes. 183 [[nodiscard]] bool PCPWrapAddress(std::span<uint8_t> wrapped_addr, const CNetAddr &addr) 184 { 185 Assume(wrapped_addr.size() == ADDR_IPV6_SIZE); 186 if (addr.IsIPv4()) { 187 struct in_addr addr4; 188 if (!addr.GetInAddr(&addr4)) return false; 189 // Section 5: "When the address field holds an IPv4 address, an IPv4-mapped IPv6 address [RFC4291] is used (::ffff:0:0/96)." 190 std::memcpy(wrapped_addr.data(), IPV4_IN_IPV6_PREFIX.data(), IPV4_IN_IPV6_PREFIX.size()); 191 std::memcpy(wrapped_addr.data() + IPV4_IN_IPV6_PREFIX.size(), &addr4, ADDR_IPV4_SIZE); 192 return true; 193 } else if (addr.IsIPv6()) { 194 struct in6_addr addr6; 195 if (!addr.GetIn6Addr(&addr6)) return false; 196 std::memcpy(wrapped_addr.data(), &addr6, ADDR_IPV6_SIZE); 197 return true; 198 } else { 199 return false; 200 } 201 } 202 203 //! Unwrap PCP-encoded address according to RFC6887. 204 CNetAddr PCPUnwrapAddress(std::span<const uint8_t> wrapped_addr) 205 { 206 Assume(wrapped_addr.size() == ADDR_IPV6_SIZE); 207 if (util::HasPrefix(wrapped_addr, IPV4_IN_IPV6_PREFIX)) { 208 struct in_addr addr4; 209 std::memcpy(&addr4, wrapped_addr.data() + IPV4_IN_IPV6_PREFIX.size(), ADDR_IPV4_SIZE); 210 return CNetAddr(addr4); 211 } else { 212 struct in6_addr addr6; 213 std::memcpy(&addr6, wrapped_addr.data(), ADDR_IPV6_SIZE); 214 return CNetAddr(addr6); 215 } 216 } 217 218 //! PCP or NAT-PMP send-receive loop. 219 std::optional<std::vector<uint8_t>> PCPSendRecv(Sock &sock, const std::string &protocol, std::span<const uint8_t> request, int num_tries, 220 std::chrono::milliseconds timeout_per_try, 221 std::function<bool(std::span<const uint8_t>)> check_packet, 222 CThreadInterrupt& interrupt) 223 { 224 using namespace std::chrono; 225 // UDP is a potentially lossy protocol, so we try to send again a few times. 226 uint8_t response[PCP_MAX_SIZE]; 227 bool got_response = false; 228 int recvsz = 0; 229 for (int ntry = 0; !got_response && ntry < num_tries; ++ntry) { 230 if (ntry > 0) { 231 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Retrying (%d)\n", protocol, ntry); 232 } 233 // Dispatch packet to gateway. 234 if (sock.Send(request.data(), request.size(), 0) != static_cast<ssize_t>(request.size())) { 235 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Could not send request: %s\n", protocol, NetworkErrorString(WSAGetLastError())); 236 return std::nullopt; // Network-level error, probably no use retrying. 237 } 238 239 // Wait for response(s) until we get a valid response, a network error, or time out. 240 auto cur_time = time_point_cast<milliseconds>(MockableSteadyClock::now()); 241 auto deadline = cur_time + timeout_per_try; 242 while ((cur_time = time_point_cast<milliseconds>(MockableSteadyClock::now())) < deadline) { 243 if (interrupt) return std::nullopt; 244 Sock::Event occurred = 0; 245 if (!sock.Wait(deadline - cur_time, Sock::RECV, &occurred)) { 246 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "%s: Could not wait on socket: %s\n", protocol, NetworkErrorString(WSAGetLastError())); 247 return std::nullopt; // Network-level error, probably no use retrying. 248 } 249 if (!occurred) { 250 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Timeout\n", protocol); 251 break; // Retry. 252 } 253 254 // Receive response. 255 recvsz = sock.Recv(response, sizeof(response), MSG_DONTWAIT); 256 if (recvsz < 0) { 257 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Could not receive response: %s\n", protocol, NetworkErrorString(WSAGetLastError())); 258 return std::nullopt; // Network-level error, probably no use retrying. 259 } 260 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Received response of %d bytes: %s\n", protocol, recvsz, HexStr(std::span(response, recvsz))); 261 262 if (check_packet(std::span<uint8_t>(response, recvsz))) { 263 got_response = true; // Got expected response, break from receive loop as well as from retry loop. 264 break; 265 } 266 } 267 } 268 if (!got_response) { 269 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "%s: Giving up after %d tries\n", protocol, num_tries); 270 return std::nullopt; 271 } 272 return std::vector<uint8_t>(response, response + recvsz); 273 } 274 275 } 276 277 std::variant<MappingResult, MappingError> NATPMPRequestPortMap(const CNetAddr &gateway, uint16_t port, uint32_t lifetime, CThreadInterrupt& interrupt, int num_tries, std::chrono::milliseconds timeout_per_try) 278 { 279 struct sockaddr_storage dest_addr; 280 socklen_t dest_addrlen = sizeof(struct sockaddr_storage); 281 282 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "natpmp: Requesting port mapping port %d from gateway %s\n", port, gateway.ToStringAddr()); 283 284 // Validate gateway, make sure it's IPv4. NAT-PMP does not support IPv6. 285 if (!CService(gateway, PCP_SERVER_PORT).GetSockAddr((struct sockaddr*)&dest_addr, &dest_addrlen)) return MappingError::NETWORK_ERROR; 286 if (dest_addr.ss_family != AF_INET) return MappingError::NETWORK_ERROR; 287 288 // Create IPv4 UDP socket 289 auto sock{CreateSock(AF_INET, SOCK_DGRAM, IPPROTO_UDP)}; 290 if (!sock) { 291 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Could not create UDP socket: %s\n", NetworkErrorString(WSAGetLastError())); 292 return MappingError::NETWORK_ERROR; 293 } 294 295 // Associate UDP socket to gateway. 296 if (sock->Connect((struct sockaddr*)&dest_addr, dest_addrlen) != 0) { 297 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Could not connect to gateway: %s\n", NetworkErrorString(WSAGetLastError())); 298 return MappingError::NETWORK_ERROR; 299 } 300 301 // Use getsockname to get the address toward the default gateway (the internal address). 302 struct sockaddr_in internal; 303 socklen_t internal_addrlen = sizeof(struct sockaddr_in); 304 if (sock->GetSockName((struct sockaddr*)&internal, &internal_addrlen) != 0) { 305 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Could not get sock name: %s\n", NetworkErrorString(WSAGetLastError())); 306 return MappingError::NETWORK_ERROR; 307 } 308 309 // Request external IP address (RFC6886 section 3.2). 310 std::vector<uint8_t> request(NATPMP_GETEXTERNAL_REQUEST_SIZE); 311 request[NATPMP_HDR_VERSION_OFS] = NATPMP_VERSION; 312 request[NATPMP_HDR_OP_OFS] = NATPMP_REQUEST | NATPMP_OP_GETEXTERNAL; 313 314 auto recv_res = PCPSendRecv(*sock, "natpmp", request, num_tries, timeout_per_try, 315 [&](const std::span<const uint8_t> response) -> bool { 316 if (response.size() < NATPMP_GETEXTERNAL_RESPONSE_SIZE) { 317 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response too small\n"); 318 return false; // Wasn't response to what we expected, try receiving next packet. 319 } 320 if (response[NATPMP_HDR_VERSION_OFS] != NATPMP_VERSION || response[NATPMP_HDR_OP_OFS] != (NATPMP_RESPONSE | NATPMP_OP_GETEXTERNAL)) { 321 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response to wrong command\n"); 322 return false; // Wasn't response to what we expected, try receiving next packet. 323 } 324 return true; 325 }, 326 interrupt); 327 328 struct in_addr external_addr; 329 if (recv_res) { 330 const std::span<const uint8_t> response = *recv_res; 331 332 Assume(response.size() >= NATPMP_GETEXTERNAL_RESPONSE_SIZE); 333 uint16_t result_code = ReadBE16(response.data() + NATPMP_RESPONSE_HDR_RESULT_OFS); 334 if (result_code != NATPMP_RESULT_SUCCESS) { 335 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Getting external address failed with result %s\n", NATPMPResultString(result_code)); 336 return MappingError::PROTOCOL_ERROR; 337 } 338 339 std::memcpy(&external_addr, response.data() + NATPMP_GETEXTERNAL_RESPONSE_IP_OFS, ADDR_IPV4_SIZE); 340 } else { 341 return MappingError::NETWORK_ERROR; 342 } 343 344 // Create TCP mapping request (RFC6886 section 3.3). 345 request = std::vector<uint8_t>(NATPMP_MAP_REQUEST_SIZE); 346 request[NATPMP_HDR_VERSION_OFS] = NATPMP_VERSION; 347 request[NATPMP_HDR_OP_OFS] = NATPMP_REQUEST | NATPMP_OP_MAP_TCP; 348 WriteBE16(request.data() + NATPMP_MAP_REQUEST_INTERNAL_PORT_OFS, port); 349 WriteBE16(request.data() + NATPMP_MAP_REQUEST_EXTERNAL_PORT_OFS, port); 350 WriteBE32(request.data() + NATPMP_MAP_REQUEST_LIFETIME_OFS, lifetime); 351 352 recv_res = PCPSendRecv(*sock, "natpmp", request, num_tries, timeout_per_try, 353 [&](const std::span<const uint8_t> response) -> bool { 354 if (response.size() < NATPMP_MAP_RESPONSE_SIZE) { 355 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response too small\n"); 356 return false; // Wasn't response to what we expected, try receiving next packet. 357 } 358 if (response[0] != NATPMP_VERSION || response[1] != (NATPMP_RESPONSE | NATPMP_OP_MAP_TCP)) { 359 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response to wrong command\n"); 360 return false; // Wasn't response to what we expected, try receiving next packet. 361 } 362 uint16_t internal_port = ReadBE16(response.data() + NATPMP_MAP_RESPONSE_INTERNAL_PORT_OFS); 363 if (internal_port != port) { 364 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Response port doesn't match request\n"); 365 return false; // Wasn't response to what we expected, try receiving next packet. 366 } 367 return true; 368 }, 369 interrupt); 370 371 if (recv_res) { 372 const std::span<uint8_t> response = *recv_res; 373 374 Assume(response.size() >= NATPMP_MAP_RESPONSE_SIZE); 375 uint16_t result_code = ReadBE16(response.data() + NATPMP_RESPONSE_HDR_RESULT_OFS); 376 if (result_code != NATPMP_RESULT_SUCCESS) { 377 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "natpmp: Port mapping failed with result %s\n", NATPMPResultString(result_code)); 378 if (result_code == NATPMP_RESULT_NO_RESOURCES) { 379 return MappingError::NO_RESOURCES; 380 } 381 return MappingError::PROTOCOL_ERROR; 382 } 383 384 uint32_t lifetime_ret = ReadBE32(response.data() + NATPMP_MAP_RESPONSE_LIFETIME_OFS); 385 uint16_t external_port = ReadBE16(response.data() + NATPMP_MAP_RESPONSE_EXTERNAL_PORT_OFS); 386 return MappingResult(NATPMP_VERSION, CService(internal.sin_addr, port), CService(external_addr, external_port), lifetime_ret); 387 } else { 388 return MappingError::NETWORK_ERROR; 389 } 390 } 391 392 std::variant<MappingResult, MappingError> PCPRequestPortMap(const PCPMappingNonce &nonce, const CNetAddr &gateway, const CNetAddr &bind, uint16_t port, uint32_t lifetime, CThreadInterrupt& interrupt, int num_tries, std::chrono::milliseconds timeout_per_try) 393 { 394 struct sockaddr_storage dest_addr, bind_addr; 395 socklen_t dest_addrlen = sizeof(struct sockaddr_storage), bind_addrlen = sizeof(struct sockaddr_storage); 396 397 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "pcp: Requesting port mapping for addr %s port %d from gateway %s\n", bind.ToStringAddr(), port, gateway.ToStringAddr()); 398 399 // Validate addresses, make sure they're the same network family. 400 if (!CService(gateway, PCP_SERVER_PORT).GetSockAddr((struct sockaddr*)&dest_addr, &dest_addrlen)) return MappingError::NETWORK_ERROR; 401 if (!CService(bind, 0).GetSockAddr((struct sockaddr*)&bind_addr, &bind_addrlen)) return MappingError::NETWORK_ERROR; 402 if (dest_addr.ss_family != bind_addr.ss_family) return MappingError::NETWORK_ERROR; 403 404 // Create UDP socket (IPv4 or IPv6 based on provided gateway). 405 auto sock{CreateSock(dest_addr.ss_family, SOCK_DGRAM, IPPROTO_UDP)}; 406 if (!sock) { 407 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Could not create UDP socket: %s\n", NetworkErrorString(WSAGetLastError())); 408 return MappingError::NETWORK_ERROR; 409 } 410 411 // Make sure that we send from requested destination address, anything else will be 412 // rejected by a security-conscious router. 413 if (sock->Bind((struct sockaddr*)&bind_addr, bind_addrlen) != 0) { 414 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Could not bind to address: %s\n", NetworkErrorString(WSAGetLastError())); 415 return MappingError::NETWORK_ERROR; 416 } 417 418 // Associate UDP socket to gateway. 419 if (sock->Connect((struct sockaddr*)&dest_addr, dest_addrlen) != 0) { 420 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Could not connect to gateway: %s\n", NetworkErrorString(WSAGetLastError())); 421 return MappingError::NETWORK_ERROR; 422 } 423 424 // Use getsockname to get the address toward the default gateway (the internal address), 425 // in case we don't know what address to map 426 // (this is only needed if bind is INADDR_ANY, but it doesn't hurt as an extra check). 427 struct sockaddr_storage internal_addr; 428 socklen_t internal_addrlen = sizeof(struct sockaddr_storage); 429 if (sock->GetSockName((struct sockaddr*)&internal_addr, &internal_addrlen) != 0) { 430 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Could not get sock name: %s\n", NetworkErrorString(WSAGetLastError())); 431 return MappingError::NETWORK_ERROR; 432 } 433 CService internal; 434 if (!internal.SetSockAddr((struct sockaddr*)&internal_addr, internal_addrlen)) return MappingError::NETWORK_ERROR; 435 LogPrintLevel(BCLog::NET, BCLog::Level::Debug, "pcp: Internal address after connect: %s\n", internal.ToStringAddr()); 436 437 // Build request packet. Make sure the packet is zeroed so that reserved fields are zero 438 // as required by the spec (and not potentially leak data). 439 // Make sure there's space for the request header and MAP specific request data. 440 std::vector<uint8_t> request(PCP_HDR_SIZE + PCP_MAP_SIZE); 441 // Fill in request header, See RFC6887 Figure 2. 442 size_t ofs = 0; 443 request[ofs + PCP_HDR_VERSION_OFS] = PCP_VERSION; 444 request[ofs + PCP_HDR_OP_OFS] = PCP_REQUEST | PCP_OP_MAP; 445 WriteBE32(request.data() + ofs + PCP_HDR_LIFETIME_OFS, lifetime); 446 if (!PCPWrapAddress(std::span(request).subspan(ofs + PCP_REQUEST_HDR_IP_OFS, ADDR_IPV6_SIZE), internal)) return MappingError::NETWORK_ERROR; 447 448 ofs += PCP_HDR_SIZE; 449 450 // Fill in MAP request packet, See RFC6887 Figure 9. 451 // Randomize mapping nonce (this is repeated in the response, to be able to 452 // correlate requests and responses, and used to authenticate changes to the mapping). 453 std::memcpy(request.data() + ofs + PCP_MAP_NONCE_OFS, nonce.data(), PCP_MAP_NONCE_SIZE); 454 request[ofs + PCP_MAP_PROTOCOL_OFS] = PCP_PROTOCOL_TCP; 455 WriteBE16(request.data() + ofs + PCP_MAP_INTERNAL_PORT_OFS, port); 456 WriteBE16(request.data() + ofs + PCP_MAP_EXTERNAL_PORT_OFS, port); 457 if (!PCPWrapAddress(std::span(request).subspan(ofs + PCP_MAP_EXTERNAL_IP_OFS, ADDR_IPV6_SIZE), bind)) return MappingError::NETWORK_ERROR; 458 459 ofs += PCP_MAP_SIZE; 460 Assume(ofs == request.size()); 461 462 // Receive loop. 463 bool is_natpmp = false; 464 auto recv_res = PCPSendRecv(*sock, "pcp", request, num_tries, timeout_per_try, 465 [&](const std::span<const uint8_t> response) -> bool { 466 // Unsupported version according to RFC6887 appendix A and RFC6886 section 3.5, can fall back to NAT-PMP. 467 if (response.size() == NATPMP_RESPONSE_HDR_SIZE && response[PCP_HDR_VERSION_OFS] == NATPMP_VERSION && response[PCP_RESPONSE_HDR_RESULT_OFS] == NATPMP_RESULT_UNSUPP_VERSION) { 468 is_natpmp = true; 469 return true; // Let it through to caller. 470 } 471 if (response.size() < (PCP_HDR_SIZE + PCP_MAP_SIZE)) { 472 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Response too small\n"); 473 return false; // Wasn't response to what we expected, try receiving next packet. 474 } 475 if (response[PCP_HDR_VERSION_OFS] != PCP_VERSION || response[PCP_HDR_OP_OFS] != (PCP_RESPONSE | PCP_OP_MAP)) { 476 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Response to wrong command\n"); 477 return false; // Wasn't response to what we expected, try receiving next packet. 478 } 479 // Handle MAP opcode response. See RFC6887 Figure 10. 480 // Check that returned mapping nonce matches our request. 481 if (!std::ranges::equal(response.subspan(PCP_HDR_SIZE + PCP_MAP_NONCE_OFS, PCP_MAP_NONCE_SIZE), nonce)) { 482 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Mapping nonce mismatch\n"); 483 return false; // Wasn't response to what we expected, try receiving next packet. 484 } 485 uint8_t protocol = response[PCP_HDR_SIZE + 12]; 486 uint16_t internal_port = ReadBE16(response.data() + PCP_HDR_SIZE + 16); 487 if (protocol != PCP_PROTOCOL_TCP || internal_port != port) { 488 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Response protocol or port doesn't match request\n"); 489 return false; // Wasn't response to what we expected, try receiving next packet. 490 } 491 return true; 492 }, 493 interrupt); 494 495 if (!recv_res) { 496 return MappingError::NETWORK_ERROR; 497 } 498 if (is_natpmp) { 499 return MappingError::UNSUPP_VERSION; 500 } 501 502 const std::span<const uint8_t> response = *recv_res; 503 // If we get here, we got a valid MAP response to our request. 504 // Check to see if we got the result we expected. 505 Assume(response.size() >= (PCP_HDR_SIZE + PCP_MAP_SIZE)); 506 uint8_t result_code = response[PCP_RESPONSE_HDR_RESULT_OFS]; 507 uint32_t lifetime_ret = ReadBE32(response.data() + PCP_HDR_LIFETIME_OFS); 508 uint16_t external_port = ReadBE16(response.data() + PCP_HDR_SIZE + PCP_MAP_EXTERNAL_PORT_OFS); 509 CNetAddr external_addr{PCPUnwrapAddress(response.subspan(PCP_HDR_SIZE + PCP_MAP_EXTERNAL_IP_OFS, ADDR_IPV6_SIZE))}; 510 if (result_code != PCP_RESULT_SUCCESS) { 511 LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "pcp: Mapping failed with result %s\n", PCPResultString(result_code)); 512 if (result_code == PCP_RESULT_NO_RESOURCES) { 513 return MappingError::NO_RESOURCES; 514 } 515 return MappingError::PROTOCOL_ERROR; 516 } 517 518 return MappingResult(PCP_VERSION, CService(internal, port), CService(external_addr, external_port), lifetime_ret); 519 } 520 521 std::string MappingResult::ToString() const 522 { 523 Assume(version == NATPMP_VERSION || version == PCP_VERSION); 524 return strprintf("%s:%s -> %s (for %ds)", 525 version == NATPMP_VERSION ? "natpmp" : "pcp", 526 external.ToStringAddrPort(), 527 internal.ToStringAddrPort(), 528 lifetime 529 ); 530 }