From 85cc73b34b1f681e8a2f6396dbddc83ba1d526a9 Mon Sep 17 00:00:00 2001 From: Yakun Xu Date: Thu, 13 May 2021 23:23:07 +0800 Subject: [PATCH] [ip] make mesh-local addresses non-preferred (#6532) This commit marks anycast addresses and mesh-local addresses as IPv6 deprecated addresses. --- include/openthread/ip6.h | 2 +- src/core/net/netif.cpp | 14 +++++++------- src/core/net/netif.hpp | 8 +++++--- src/core/thread/mle.cpp | 2 +- src/posix/platform/netif.cpp | 4 ++-- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/include/openthread/ip6.h b/include/openthread/ip6.h index f2f093e35..59a0e8a9f 100644 --- a/include/openthread/ip6.h +++ b/include/openthread/ip6.h @@ -427,7 +427,7 @@ typedef struct otIp6AddressInfo const otIp6Address *mAddress; ///< A pointer to the IPv6 address. uint8_t mPrefixLength; ///< The prefix length of mAddress if it is a unicast address. uint8_t mScope : 4; ///< The scope of this address. - bool mIsAnycast : 1; ///< Whether this is an anycast address. + bool mPreferred : 1; ///< Whether this is a preferred address. } otIp6AddressInfo; /** diff --git a/src/core/net/netif.cpp b/src/core/net/netif.cpp index 9a0a608ec..9d7c11ef5 100644 --- a/src/core/net/netif.cpp +++ b/src/core/net/netif.cpp @@ -45,12 +45,12 @@ namespace Ip6 { class AddressInfo : public otIp6AddressInfo { public: - AddressInfo(const NetifUnicastAddress &aAddress, Instance &aInstance) + explicit AddressInfo(const NetifUnicastAddress &aAddress) { mAddress = &aAddress.mAddress; mPrefixLength = aAddress.mPrefixLength; mScope = aAddress.GetScope(); - mIsAnycast = aInstance.Get().IsAnycastLocator(aAddress.GetAddress()); + mPreferred = aAddress.mPreferred; } explicit AddressInfo(const NetifMulticastAddress &aAddress) @@ -58,7 +58,7 @@ public: mAddress = &aAddress.GetAddress(); mPrefixLength = kMulticastPrefixLength; mScope = aAddress.GetAddress().GetScope(); - mIsAnycast = false; + mPreferred = false; } private: @@ -439,7 +439,7 @@ void Netif::AddUnicastAddress(NetifUnicastAddress &aAddress) VerifyOrExit(mAddressCallback != nullptr); { - AddressInfo addressInfo(aAddress, GetInstance()); + AddressInfo addressInfo(aAddress); mAddressCallback(&addressInfo, /* IsAdded */ true, mAddressCallbackContext); @@ -457,7 +457,7 @@ void Netif::RemoveUnicastAddress(const NetifUnicastAddress &aAddress) VerifyOrExit(mAddressCallback != nullptr); { - AddressInfo addressInfo(aAddress, GetInstance()); + AddressInfo addressInfo(aAddress); mAddressCallback(&addressInfo, /* IsAdded */ false, mAddressCallbackContext); } @@ -543,12 +543,12 @@ bool Netif::IsUnicastAddressExternal(const NetifUnicastAddress &aAddress) const return mExtUnicastAddressPool.IsPoolEntry(aAddress); } -void NetifUnicastAddress::InitAsThreadOrigin(void) +void NetifUnicastAddress::InitAsThreadOrigin(bool aPreferred) { Clear(); mPrefixLength = NetworkPrefix::kLength; mAddressOrigin = OT_ADDRESS_ORIGIN_THREAD; - mPreferred = true; + mPreferred = aPreferred; mValid = true; } diff --git a/src/core/net/netif.hpp b/src/core/net/netif.hpp index a3fd12559..ce5a6c510 100644 --- a/src/core/net/netif.hpp +++ b/src/core/net/netif.hpp @@ -80,12 +80,14 @@ public: * This method clears and initializes the unicast address as a preferred, valid, thread-origin address with 64-bit * prefix length. * + * @param[in] aPreferred Whether to initialize as a preferred address. + * */ - void InitAsThreadOrigin(void); + void InitAsThreadOrigin(bool aPreferred = false); /** - * This method clears and initializes the unicast address as a preferred, valid, thread-origin, realm-local scope - * (overridden) address with 64-bit prefix length. + * This method clears and initializes the unicast address as a valid (but not preferred), thread-origin, realm-local + * scope (overridden) address with 64-bit prefix length. * */ void InitAsThreadOriginRealmLocalScope(void); diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 66d227d16..33cf4b56b 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -124,7 +124,7 @@ Mle::Mle(Instance &aInstance) mParentCandidate.Clear(); ResetCounters(); - mLinkLocal64.InitAsThreadOrigin(); + mLinkLocal64.InitAsThreadOrigin(/* aPreferred */ true); mLinkLocal64.GetAddress().SetToLinkLocalAddress(Get().GetExtAddress()); mLeaderAloc.InitAsThreadOriginRealmLocalScope(); diff --git a/src/posix/platform/netif.cpp b/src/posix/platform/netif.cpp index 6fd6f04e7..c4421bc55 100644 --- a/src/posix/platform/netif.cpp +++ b/src/posix/platform/netif.cpp @@ -344,7 +344,7 @@ static void UpdateUnicastLinux(const otIp6AddressInfo &aAddressInfo, bool aIsAdd req.nh.nlmsg_len = NLMSG_ALIGN(req.nh.nlmsg_len) + rta->rta_len; - if (aAddressInfo.mIsAnycast) + if (!aAddressInfo.mPreferred) { struct ifa_cacheinfo cacheinfo; @@ -402,7 +402,7 @@ static void UpdateUnicast(otInstance *aInstance, const otIp6AddressInfo &aAddres #if defined(__APPLE__) ifr6.ifra_lifetime.ia6t_expire = ND6_INFINITE_LIFETIME; - ifr6.ifra_lifetime.ia6t_preferred = ND6_INFINITE_LIFETIME; + ifr6.ifra_lifetime.ia6t_preferred = (aAddressInfo.mPreferred ? ND6_INFINITE_LIFETIME : 0); #endif rval = ioctl(sIpFd, aIsAdded ? SIOCAIFADDR_IN6 : SIOCDIFADDR_IN6, &ifr6);