[ip6] define NetworkPrefix type (#5213)

This commit adds `Ip6::NetworkPrefix` mirroring `otIp6NetworkPrefix`
which represents the Network Prefix (most significant 64 bits) of an
IPv6 address. Methods in `Ip6::Address` are updated to use the new
type. The `otMeshLocalPrefix` and `Mle::MeshLocalPrefix` are also
updated use the `NetworkPrefix` type.
This commit is contained in:
Abtin Keshavarzian
2020-07-20 08:38:16 -07:00
committed by GitHub
parent 434e6bbffc
commit d5370ac4cd
8 changed files with 78 additions and 46 deletions
+2 -12
View File
@@ -99,23 +99,13 @@ struct otExtendedPanId
*/
typedef struct otExtendedPanId otExtendedPanId;
#define OT_MESH_LOCAL_PREFIX_SIZE 8 ///< Size of the Mesh Local Prefix (bytes)
#define OT_MESH_LOCAL_PREFIX_SIZE OT_IP6_PREFIX_SIZE ///< Size of the Mesh Local Prefix (bytes)
/**
* This structure represents a Mesh Local Prefix.
*
*/
OT_TOOL_PACKED_BEGIN
struct otMeshLocalPrefix
{
uint8_t m8[OT_MESH_LOCAL_PREFIX_SIZE]; ///< Byte values
} OT_TOOL_PACKED_END;
/**
* This structure represents a Mesh Local Prefix.
*
*/
typedef struct otMeshLocalPrefix otMeshLocalPrefix;
typedef otIp6NetworkPrefix otMeshLocalPrefix;
#define OT_PSKC_MAX_SIZE 16 ///< Maximum size of the PSKc (bytes)
+1 -1
View File
@@ -53,7 +53,7 @@ extern "C" {
* @note This number versions both OpenThread platform and user APIs.
*
*/
#define OPENTHREAD_API_VERSION (16)
#define OPENTHREAD_API_VERSION (17)
/**
* @addtogroup api-instance
+1 -2
View File
@@ -322,8 +322,7 @@ otError ActiveDataset::CreateNewNetwork(otOperationalDataset &aDataset)
SuccessOrExit(error = static_cast<Pskc &>(aDataset.mPskc).GenerateRandom());
SuccessOrExit(error = Random::Crypto::FillBuffer(aDataset.mExtendedPanId.m8, sizeof(aDataset.mExtendedPanId)));
aDataset.mMeshLocalPrefix.m8[0] = 0xfd;
SuccessOrExit(error = Random::Crypto::FillBuffer(&aDataset.mMeshLocalPrefix.m8[1], OT_MESH_LOCAL_PREFIX_SIZE - 1));
SuccessOrExit(error = static_cast<Ip6::NetworkPrefix &>(aDataset.mMeshLocalPrefix).GenerateRandomUla());
aDataset.mSecurityPolicy.mFlags = Get<KeyManager>().GetSecurityPolicyFlags();
+17 -2
View File
@@ -46,6 +46,16 @@ using ot::Encoding::BigEndian::HostSwap32;
namespace ot {
namespace Ip6 {
//---------------------------------------------------------------------------------------------------------------------
// NetworkPrefix methods
otError NetworkPrefix::GenerateRandomUla(void)
{
m8[0] = 0xfd;
return Random::Crypto::FillBuffer(&m8[1], kSize - 1);
}
//---------------------------------------------------------------------------------------------------------------------
// InterfaceIdentifier methods
@@ -252,6 +262,11 @@ void Address::SetToRealmLocalAllMplForwarders(void)
*this = GetRealmLocalAllMplForwarders();
}
void Address::SetPrefix(const NetworkPrefix &aNetworkPrefix)
{
mFields.mComponents.mNetworkPrefix = aNetworkPrefix;
}
void Address::SetPrefix(const uint8_t *aPrefix, uint8_t aPrefixLength)
{
SetPrefix(0, aPrefix, aPrefixLength);
@@ -286,9 +301,9 @@ void Address::SetMulticastNetworkPrefix(const uint8_t *aPrefix, uint8_t aPrefixL
mFields.m8[kMulticastNetworkPrefixLengthOffset] = aPrefixLength;
}
void Address::SetToLocator(const Mle::MeshLocalPrefix &aMeshLocalPrefix, uint16_t aLocator)
void Address::SetToLocator(const NetworkPrefix &aNetworkPrefix, uint16_t aLocator)
{
SetPrefix(aMeshLocalPrefix);
SetPrefix(aNetworkPrefix);
GetIid().SetToLocator(aLocator);
}
+53 -20
View File
@@ -43,7 +43,6 @@
#include "common/equatable.hpp"
#include "common/string.hpp"
#include "mac/mac_types.hpp"
#include "thread/mle_types.hpp"
using ot::Encoding::BigEndian::HostSwap16;
@@ -57,6 +56,32 @@ namespace Ip6 {
*
*/
/**
* This class represents the Network Prefix of an IPv6 address (most significant 64 bits of the address).
*
*/
OT_TOOL_PACKED_BEGIN
class NetworkPrefix : public otIp6NetworkPrefix, public Equatable<NetworkPrefix>, public Clearable<NetworkPrefix>
{
public:
enum
{
kSize = OT_IP6_PREFIX_SIZE, ///< Size in bytes.
kLength = OT_IP6_PREFIX_SIZE * CHAR_BIT, ///< Length of Network Prefix in bits.
};
/**
* This method generates and sets the Network Prefix to a crypto-secure random Unique Local Address (ULA) based
* on the pattern `fdxx:xxxx:xxxx:` (RFC 4193).
*
* @retval OT_ERROR_NONE Successfully generated a random ULA Network Prefix
* @retval OT_ERROR_FAILED Failed to generate random ULA Network Prefix.
*
*/
otError GenerateRandomUla(void);
} OT_TOOL_PACKED_END;
/**
* This class represents the Interface Identifier of an IPv6 address.
*
@@ -461,29 +486,40 @@ public:
bool IsMulticastLargerThanRealmLocal(void) const;
/**
* This method sets the IPv6 address to a Routing Locator (RLOC) IPv6 address with a given Mesh-local prefix and
* This method sets the IPv6 address to a Routing Locator (RLOC) IPv6 address with a given Network Prefix and
* RLOC16 value.
*
* @param[in] aMeshLocalPrefix A Mesh Local Prefix.
* @param[in] aNetworkPrefix A Network Prefix.
* @param[in] aRloc16 A RLOC16 value.
*
*/
void SetToRoutingLocator(const Mle::MeshLocalPrefix &aMeshLocalPrefix, uint16_t aRloc16)
void SetToRoutingLocator(const NetworkPrefix &aNetworkPrefix, uint16_t aRloc16)
{
SetToLocator(aMeshLocalPrefix, aRloc16);
SetToLocator(aNetworkPrefix, aRloc16);
}
/**
* This method sets the IPv6 address to a Anycast Locator (ALOC) IPv6 address with a given Mesh-local prefix and
* This method sets the IPv6 address to a Anycast Locator (ALOC) IPv6 address with a given Network Prefix and
* ALOC16 value.
*
* @param[in] aMeshLocalPrefix A Mesh Local Prefix.
* @param[in] aNetworkPrefix A Network Prefix.
* @param[in] aAloc16 A ALOC16 value.
*
*/
void SetToAnycastLocator(const Mle::MeshLocalPrefix &aMeshLocalPrefix, uint16_t aAloc16)
void SetToAnycastLocator(const NetworkPrefix &aNetworkPrefix, uint16_t aAloc16)
{
SetToLocator(aMeshLocalPrefix, aAloc16);
SetToLocator(aNetworkPrefix, aAloc16);
}
/**
* This method returns the Network Prefix of the IPv6 address (most significant 64 bits of the address).
*
* @returns A reference to the Network Prefix.
*
*/
const NetworkPrefix &GetPrefix(void) const
{
return static_cast<const NetworkPrefix &>(mFields.mComponents.mNetworkPrefix);
}
/**
@@ -499,15 +535,12 @@ public:
void SetPrefix(const uint8_t *aPrefix, uint8_t aPrefixLength);
/**
* This method sets the IPv6 address prefix to the given Mesh Local Prefix.
* This method sets the IPv6 address prefix to the given Network Prefix.
*
* @param[in] aMeshLocalPrefix A Mesh Local Prefix.
* @param[in] aNetworkPrefix A Network Prefix.
*
*/
void SetPrefix(const Mle::MeshLocalPrefix &aMeshLocalPrefix)
{
SetPrefix(aMeshLocalPrefix.m8, Mle::MeshLocalPrefix::kLength);
}
void SetPrefix(const NetworkPrefix &aNetworkPrefix);
/**
* This method sets the prefix content of the Prefix-Based Multicast Address.
@@ -519,14 +552,14 @@ public:
void SetMulticastNetworkPrefix(const uint8_t *aPrefix, uint8_t aPrefixLength);
/**
* This method sets the prefix content of Mesh Local Prefix-Based Multicast Address.
* This method sets the prefix content of Prefix-Based Multicast Address.
*
* @param[in] aMeshLocalPrefix A reference to the Mesh Local Prefix.
* @param[in] aNetworkPrefix A reference to a Network Prefix.
*
*/
void SetMulticastNetworkPrefix(const Mle::MeshLocalPrefix &aMeshLocalPrefix)
void SetMulticastNetworkPrefix(const NetworkPrefix &aNetworkPrefix)
{
SetMulticastNetworkPrefix(aMeshLocalPrefix.m8, Mle::MeshLocalPrefix::kLength);
SetMulticastNetworkPrefix(aNetworkPrefix.m8, NetworkPrefix::kLength);
}
/**
@@ -618,7 +651,7 @@ public:
private:
void SetPrefix(uint8_t aOffset, const uint8_t *aPrefix, uint8_t aPrefixLength);
void SetToLocator(const Mle::MeshLocalPrefix &aMeshLocalPrefix, uint16_t aLocator);
void SetToLocator(const NetworkPrefix &aNetworkPrefix, uint16_t aLocator);
static const Address &GetLinkLocalAllNodesMulticast(void);
static const Address &GetLinkLocalAllRoutersMulticast(void);
+1 -1
View File
@@ -3849,7 +3849,7 @@ bool Mle::IsAnycastLocator(const Ip6::Address &aAddress) const
bool Mle::IsMeshLocalAddress(const Ip6::Address &aAddress) const
{
return (memcmp(&GetMeshLocalPrefix(), &aAddress, MeshLocalPrefix::kSize) == 0);
return (aAddress.GetPrefix() == GetMeshLocalPrefix());
}
otError Mle::CheckReachability(uint16_t aMeshDest, Ip6::Header &aIp6Header)
+1 -1
View File
@@ -554,7 +554,7 @@ public:
*/
const MeshLocalPrefix &GetMeshLocalPrefix(void) const
{
return reinterpret_cast<const MeshLocalPrefix &>(mMeshLocal16.GetAddress());
return static_cast<const MeshLocalPrefix &>(mMeshLocal16.GetAddress().GetPrefix());
}
/**
+2 -7
View File
@@ -47,6 +47,7 @@
#include "common/equatable.hpp"
#include "common/string.hpp"
#include "mac/mac_types.hpp"
#include "net/ip6_address.hpp"
namespace ot {
namespace Mle {
@@ -430,15 +431,9 @@ private:
*
*/
OT_TOOL_PACKED_BEGIN
class MeshLocalPrefix : public otMeshLocalPrefix, public Equatable<MeshLocalPrefix>
class MeshLocalPrefix : public Ip6::NetworkPrefix
{
public:
enum
{
kSize = OT_MESH_LOCAL_PREFIX_SIZE, ///< Size in bytes.
kLength = OT_MESH_LOCAL_PREFIX_SIZE * CHAR_BIT, ///< Length of Mesh Local Prefix in bits.
};
/**
* This method derives and sets the Mesh Local Prefix from an Extended PAN ID.
*