[posix] make the link-local route's metric configurable (#11486)

This commit allows to configure the link-local route's metric via
macro on Linux. By using a larger metric, we can prevent host
processes from accidentally sending traffic to Thread network
interface.

For example, when the mDNS daemon on the BR wants to respond to a `QU`
mDNS question, it will send the mDNS response to a link-local
address. In a multi-network environment, it could wrongly go to the
Thread network interface if the socket is not explicitly bound to the
desired interface.
This commit is contained in:
Handa Wang
2025-05-12 20:11:39 -07:00
committed by GitHub
parent c3823f648f
commit 7b1fdbf65f
2 changed files with 31 additions and 7 deletions
+17 -5
View File
@@ -72,6 +72,7 @@
#include <fcntl.h>
#include <ifaddrs.h>
#ifdef __linux__
#include <linux/if_addr.h>
#include <linux/if_link.h>
#include <linux/if_tun.h>
#include <linux/netlink.h>
@@ -433,6 +434,7 @@ static void UpdateUnicastLinux(otInstance *aInstance, const otIp6AddressInfo &aA
{
OT_UNUSED_VARIABLE(aInstance);
static constexpr uint8_t kLinkLocalScope = 2;
struct
{
struct nlmsghdr nh;
@@ -456,7 +458,7 @@ static void UpdateUnicastLinux(otInstance *aInstance, const otIp6AddressInfo &aA
AddRtAttr(&req.nh, sizeof(req), IFA_LOCAL, aAddressInfo.mAddress, sizeof(*aAddressInfo.mAddress));
if (!aAddressInfo.mPreferred || aAddressInfo.mMeshLocal)
if (!aAddressInfo.mPreferred || aAddressInfo.mMeshLocal || aAddressInfo.mScope == kLinkLocalScope)
{
struct ifa_cacheinfo cacheinfo;
@@ -479,12 +481,22 @@ static void UpdateUnicastLinux(otInstance *aInstance, const otIp6AddressInfo &aA
else
#endif
{
#if OPENTHREAD_POSIX_CONFIG_NETIF_PREFIX_ROUTE_METRIC > 0
static constexpr uint8_t kLinkLocalScope = 2;
uint32_t route_metric = 0;
OT_UNUSED_VARIABLE(route_metric);
if (aAddressInfo.mScope > kLinkLocalScope)
if (aAddressInfo.mScope == kLinkLocalScope)
{
AddRtAttrUint32(&req.nh, sizeof(req), IFA_RT_PRIORITY, OPENTHREAD_POSIX_CONFIG_NETIF_PREFIX_ROUTE_METRIC);
route_metric = OPENTHREAD_POSIX_CONFIG_NETIF_LINK_LOCAL_ROUTE_METRIC;
}
else if (aAddressInfo.mScope > kLinkLocalScope)
{
route_metric = OPENTHREAD_POSIX_CONFIG_NETIF_PREFIX_ROUTE_METRIC;
}
#if OPENTHREAD_POSIX_CONFIG_NETIF_LINK_LOCAL_ROUTE_METRIC || OPENTHREAD_POSIX_CONFIG_NETIF_PREFIX_ROUTE_METRIC
if (route_metric > 0)
{
AddRtAttrUint32(&req.nh, sizeof(req), IFA_RT_PRIORITY, route_metric);
}
#endif
}
+14 -2
View File
@@ -140,11 +140,23 @@
#define OPENTHREAD_POSIX_CONFIG_SECURE_SETTINGS_ENABLE 0
#endif
/**
* @def OPENTHREAD_POSIX_CONFIG_NETIF_LINK_LOCAL_ROUTE_METRIC
*
* This setting configures the link-local route metric on the Thread network interface.
* Define as 0 to use the default prefix route metric.
*
* Note: The feature works on Linux kernel v4.18+.
*/
#ifndef OPENTHREAD_POSIX_CONFIG_NETIF_LINK_LOCAL_ROUTE_METRIC
#define OPENTHREAD_POSIX_CONFIG_NETIF_LINK_LOCAL_ROUTE_METRIC 0
#endif
/**
* @def OPENTHREAD_POSIX_CONFIG_NETIF_PREFIX_ROUTE_METRIC
*
* This setting configures the prefix route metric on the Thread network interface.
* Define as 0 to use use the default prefix route metric.
* This setting configures the non-link-local prefix route metric on the Thread network interface.
* Define as 0 to use the default prefix route metric.
*
* Note: The feature works on Linux kernel v4.18+.
*/