@@ -36,6 +36,9 @@ namespace {
3636// will fail, so we skip that.
3737#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 1400000)
3838
39+ // Good for responses containing ~ 10,000-15,000 routes.
40+ static constexpr ssize_t NETLINK_MAX_RESPONSE_SIZE{1'048'576 };
41+
3942std::optional<CNetAddr> QueryDefaultGatewayImpl (sa_family_t family)
4043{
4144 // Create a netlink socket.
@@ -84,49 +87,68 @@ std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t family)
8487
8588 // Receive response.
8689 char response[4096 ];
87- int64_t recv_result;
88- do {
89- recv_result = sock->Recv (response, sizeof (response), 0 );
90- } while (recv_result < 0 && (errno == EINTR || errno == EAGAIN));
91- if (recv_result < 0 ) {
92- LogPrintLevel (BCLog::NET, BCLog::Level::Error, " recv() from netlink socket: %s\n " , NetworkErrorString (errno));
93- return std::nullopt ;
94- }
95-
96- for (nlmsghdr* hdr = (nlmsghdr*)response; NLMSG_OK (hdr, recv_result); hdr = NLMSG_NEXT (hdr, recv_result)) {
97- rtmsg* r = (rtmsg*)NLMSG_DATA (hdr);
98- int remaining_len = RTM_PAYLOAD (hdr);
99-
100- if (hdr->nlmsg_type != RTM_NEWROUTE) {
101- continue ; // Skip non-route messages
90+ ssize_t total_bytes_read{0 };
91+ bool done{false };
92+ while (!done) {
93+ int64_t recv_result;
94+ do {
95+ recv_result = sock->Recv (response, sizeof (response), 0 );
96+ } while (recv_result < 0 && (errno == EINTR || errno == EAGAIN));
97+ if (recv_result < 0 ) {
98+ LogPrintLevel (BCLog::NET, BCLog::Level::Error, " recv() from netlink socket: %s\n " , NetworkErrorString (errno));
99+ return std::nullopt ;
102100 }
103101
104- // Only consider default routes (destination prefix length of 0).
105- if (r->rtm_dst_len != 0 ) {
106- continue ;
102+ total_bytes_read += recv_result;
103+ if (total_bytes_read > NETLINK_MAX_RESPONSE_SIZE) {
104+ LogPrintLevel (BCLog::NET, BCLog::Level::Warning, " Netlink response exceeded size limit (%zu bytes, family=%d)\n " , NETLINK_MAX_RESPONSE_SIZE, family);
105+ return std::nullopt ;
107106 }
108107
109- // Iterate over the attributes.
110- rtattr *rta_gateway = nullptr ;
111- int scope_id = 0 ;
112- for (rtattr* attr = RTM_RTA (r); RTA_OK (attr, remaining_len); attr = RTA_NEXT (attr, remaining_len)) {
113- if (attr->rta_type == RTA_GATEWAY) {
114- rta_gateway = attr;
115- } else if (attr->rta_type == RTA_OIF && sizeof (int ) == RTA_PAYLOAD (attr)) {
116- std::memcpy (&scope_id, RTA_DATA (attr), sizeof (scope_id));
108+ for (nlmsghdr* hdr = (nlmsghdr*)response; NLMSG_OK (hdr, recv_result); hdr = NLMSG_NEXT (hdr, recv_result)) {
109+ if (!(hdr->nlmsg_flags & NLM_F_MULTI)) {
110+ done = true ;
117111 }
118- }
119112
120- // Found gateway?
121- if (rta_gateway != nullptr ) {
122- if (family == AF_INET && sizeof (in_addr) == RTA_PAYLOAD (rta_gateway)) {
123- in_addr gw;
124- std::memcpy (&gw, RTA_DATA (rta_gateway), sizeof (gw));
125- return CNetAddr (gw);
126- } else if (family == AF_INET6 && sizeof (in6_addr) == RTA_PAYLOAD (rta_gateway)) {
127- in6_addr gw;
128- std::memcpy (&gw, RTA_DATA (rta_gateway), sizeof (gw));
129- return CNetAddr (gw, scope_id);
113+ if (hdr->nlmsg_type == NLMSG_DONE) {
114+ done = true ;
115+ break ;
116+ }
117+
118+ rtmsg* r = (rtmsg*)NLMSG_DATA (hdr);
119+ int remaining_len = RTM_PAYLOAD (hdr);
120+
121+ if (hdr->nlmsg_type != RTM_NEWROUTE) {
122+ continue ; // Skip non-route messages
123+ }
124+
125+ // Only consider default routes (destination prefix length of 0).
126+ if (r->rtm_dst_len != 0 ) {
127+ continue ;
128+ }
129+
130+ // Iterate over the attributes.
131+ rtattr* rta_gateway = nullptr ;
132+ int scope_id = 0 ;
133+ for (rtattr* attr = RTM_RTA (r); RTA_OK (attr, remaining_len); attr = RTA_NEXT (attr, remaining_len)) {
134+ if (attr->rta_type == RTA_GATEWAY) {
135+ rta_gateway = attr;
136+ } else if (attr->rta_type == RTA_OIF && sizeof (int ) == RTA_PAYLOAD (attr)) {
137+ std::memcpy (&scope_id, RTA_DATA (attr), sizeof (scope_id));
138+ }
139+ }
140+
141+ // Found gateway?
142+ if (rta_gateway != nullptr ) {
143+ if (family == AF_INET && sizeof (in_addr) == RTA_PAYLOAD (rta_gateway)) {
144+ in_addr gw;
145+ std::memcpy (&gw, RTA_DATA (rta_gateway), sizeof (gw));
146+ return CNetAddr (gw);
147+ } else if (family == AF_INET6 && sizeof (in6_addr) == RTA_PAYLOAD (rta_gateway)) {
148+ in6_addr gw;
149+ std::memcpy (&gw, RTA_DATA (rta_gateway), sizeof (gw));
150+ return CNetAddr (gw, scope_id);
151+ }
130152 }
131153 }
132154 }
0 commit comments