mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-05-30 07:43:48 +02:00
net: handle multi-part netlink responses
Handle multi-part netlink responses to prevent truncated results from large routing tables. Previously, we only made a single recv call, which led to incomplete results when the kernel split the message into multiple responses (which happens frequently with NLM_F_DUMP). Also guard against a potential hanging issue where the code would indefinitely wait for NLMSG_DONE for non-multi-part responses by detecting the NLM_F_MULTI flag and only continue waiting when necessary.
This commit is contained in:
@@ -36,6 +36,9 @@ namespace {
|
|||||||
// will fail, so we skip that.
|
// will fail, so we skip that.
|
||||||
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 1400000)
|
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 1400000)
|
||||||
|
|
||||||
|
// Good for responses containing ~ 10,000-15,000 routes.
|
||||||
|
static constexpr ssize_t NETLINK_MAX_RESPONSE_SIZE{1'048'576};
|
||||||
|
|
||||||
std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t family)
|
std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t family)
|
||||||
{
|
{
|
||||||
// Create a netlink socket.
|
// Create a netlink socket.
|
||||||
@@ -84,6 +87,9 @@ std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t family)
|
|||||||
|
|
||||||
// Receive response.
|
// Receive response.
|
||||||
char response[4096];
|
char response[4096];
|
||||||
|
ssize_t total_bytes_read{0};
|
||||||
|
bool done{false};
|
||||||
|
while (!done) {
|
||||||
int64_t recv_result;
|
int64_t recv_result;
|
||||||
do {
|
do {
|
||||||
recv_result = sock->Recv(response, sizeof(response), 0);
|
recv_result = sock->Recv(response, sizeof(response), 0);
|
||||||
@@ -93,7 +99,22 @@ std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t family)
|
|||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
total_bytes_read += recv_result;
|
||||||
|
if (total_bytes_read > NETLINK_MAX_RESPONSE_SIZE) {
|
||||||
|
LogPrintLevel(BCLog::NET, BCLog::Level::Warning, "Netlink response exceeded size limit (%zu bytes, family=%d)\n", NETLINK_MAX_RESPONSE_SIZE, family);
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
for (nlmsghdr* hdr = (nlmsghdr*)response; NLMSG_OK(hdr, recv_result); hdr = NLMSG_NEXT(hdr, recv_result)) {
|
for (nlmsghdr* hdr = (nlmsghdr*)response; NLMSG_OK(hdr, recv_result); hdr = NLMSG_NEXT(hdr, recv_result)) {
|
||||||
|
if (!(hdr->nlmsg_flags & NLM_F_MULTI)) {
|
||||||
|
done = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hdr->nlmsg_type == NLMSG_DONE) {
|
||||||
|
done = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
rtmsg* r = (rtmsg*)NLMSG_DATA(hdr);
|
rtmsg* r = (rtmsg*)NLMSG_DATA(hdr);
|
||||||
int remaining_len = RTM_PAYLOAD(hdr);
|
int remaining_len = RTM_PAYLOAD(hdr);
|
||||||
|
|
||||||
@@ -107,7 +128,7 @@ std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t family)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Iterate over the attributes.
|
// Iterate over the attributes.
|
||||||
rtattr *rta_gateway = nullptr;
|
rtattr* rta_gateway = nullptr;
|
||||||
int scope_id = 0;
|
int scope_id = 0;
|
||||||
for (rtattr* attr = RTM_RTA(r); RTA_OK(attr, remaining_len); attr = RTA_NEXT(attr, remaining_len)) {
|
for (rtattr* attr = RTM_RTA(r); RTA_OK(attr, remaining_len); attr = RTA_NEXT(attr, remaining_len)) {
|
||||||
if (attr->rta_type == RTA_GATEWAY) {
|
if (attr->rta_type == RTA_GATEWAY) {
|
||||||
@@ -130,6 +151,7 @@ std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t family)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user