net: filter for default routes in netlink responses

Filter netlink responses to only consider default routes by checking the
destination prefix length (rtm_dst_len == 0).

Previously, we selected the first route with an RTA_GATEWAY attribute,
which for IPv6 often resulted in choosing a non-default route instead of
the actual default.

This caused occasional PCP port mapping failures because a gateway for a
non-default route was selected.
This commit is contained in:
willcl-ark 2025-04-02 09:46:56 +01:00
parent 74d9598bfb
commit 57ce645f05
No known key found for this signature in database
GPG key ID: CE6EC49945C17EA6

View file

@ -97,6 +97,11 @@ std::optional<CNetAddr> QueryDefaultGatewayImpl(sa_family_t family)
rtmsg* r = (rtmsg*)NLMSG_DATA(hdr);
int remaining_len = RTM_PAYLOAD(hdr);
// Only consider default routes (destination prefix length of 0).
if (r->rtm_dst_len != 0) {
continue;
}
// Iterate over the attributes.
rtattr *rta_gateway = nullptr;
int scope_id = 0;