diff --git a/src/posix/platform/netif.cpp b/src/posix/platform/netif.cpp index fb4a36c73..1fb3fa0bc 100644 --- a/src/posix/platform/netif.cpp +++ b/src/posix/platform/netif.cpp @@ -72,6 +72,7 @@ #include #include #ifdef __linux__ +#include #include #include #include @@ -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 } diff --git a/src/posix/platform/openthread-posix-config.h b/src/posix/platform/openthread-posix-config.h index a7785be38..c1955ae9a 100644 --- a/src/posix/platform/openthread-posix-config.h +++ b/src/posix/platform/openthread-posix-config.h @@ -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+. */