1 /* 2 * Copyright (c) 2016, The OpenThread Authors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Neither the name of the copyright holder nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /** 30 * @file 31 * This file includes definitions for Thread global IPv6 address configuration with SLAAC. 32 */ 33 34 #ifndef SLAAC_ADDRESS_HPP_ 35 #define SLAAC_ADDRESS_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #if OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE 40 41 #include "common/locator.hpp" 42 #include "common/non_copyable.hpp" 43 #include "common/notifier.hpp" 44 #include "net/netif.hpp" 45 #include "thread/network_data.hpp" 46 47 namespace ot { 48 namespace Utils { 49 50 /** 51 * @addtogroup core-slaac-address 52 * 53 * @brief 54 * This module includes definitions for Thread global IPv6 address configuration with SLAAC. 55 * 56 * @{ 57 */ 58 59 /** 60 * This class implements the SLAAC utility for Thread protocol. 61 * 62 */ 63 class Slaac : public InstanceLocator, private NonCopyable 64 { 65 friend class ot::Notifier; 66 67 public: 68 /** 69 * This type represents the secret key used for generating semantically opaque IID (per RFC 7217). 70 * 71 */ 72 struct IidSecretKey 73 { 74 static constexpr uint16_t kSize = 32; ///< Secret key size for generating semantically opaque IID. 75 76 uint8_t m8[kSize]; 77 }; 78 79 /** 80 * This constructor initializes the SLAAC manager object. 81 * 82 * Note that SLAAC module starts enabled. 83 * 84 * @param[in] aInstance A reference to the OpenThread instance. 85 * 86 */ 87 explicit Slaac(Instance &aInstance); 88 89 /** 90 * This method enables the SLAAC module. 91 * 92 * When enabled, new SLAAC addresses are generated and added from on-mesh prefixes in network data. 93 * 94 */ 95 void Enable(void); 96 97 /** 98 * This method disables the SLAAC module. 99 * 100 * When disabled, any previously added SLAAC address by this module is removed. 101 * 102 */ 103 void Disable(void); 104 105 /** 106 * This method indicates whether SLAAC module is enabled or not. 107 * 108 * @retval TRUE SLAAC module is enabled. 109 * @retval FALSE SLAAC module is disabled. 110 * 111 */ IsEnabled(void) const112 bool IsEnabled(void) const { return mEnabled; } 113 114 /** 115 * This method sets a SLAAC prefix filter handler. 116 * 117 * The handler is invoked by SLAAC module when it is about to add a SLAAC address based on a prefix. The return 118 * boolean value from handler determines whether the address is filtered or added (TRUE to filter the address, 119 * FALSE to add address). 120 * 121 * The filter can be set to `nullptr` to disable filtering (i.e., allow SLAAC addresses for all prefixes). 122 * 123 */ 124 void SetFilter(otIp6SlaacPrefixFilter aFilter); 125 126 /** 127 * This method generates the IID of an IPv6 address. 128 * 129 * @param[in,out] aAddress A reference to the address that will be filled with the IID generated. 130 * Note the prefix of the address must already be filled and will be used 131 * to generate the IID. 132 * @param[in] aNetworkId A pointer to a byte array of Network_ID to generate IID. 133 * @param[in] aNetworkIdLength The size of array @p aNetworkId. 134 * @param[in,out] aDadCounter A pointer to the DAD_Counter that is employed to resolve Duplicate 135 * Address Detection connflicts. 136 * 137 * @retval kErrorNone If successfully generated the IID. 138 * @retval kErrorFailed If no valid IID was generated. 139 * 140 */ 141 Error GenerateIid(Ip6::Netif::UnicastAddress &aAddress, 142 uint8_t * aNetworkId = nullptr, 143 uint8_t aNetworkIdLength = 0, 144 uint8_t * aDadCounter = nullptr) const; 145 146 private: 147 static constexpr uint16_t kMaxIidCreationAttempts = 256; // Maximum number of attempts when generating IID. 148 149 typedef uint8_t UpdateMode; 150 151 // Values for `UpdateMode` input parameter in `Update()`. 152 153 static constexpr UpdateMode kModeNone = 0x0; // No action. 154 static constexpr UpdateMode kModeAdd = 1 << 0; // Add new SLAAC addresses for new prefixes in network data. 155 156 // Remove SLAAC addresses. 157 // - When SLAAC is enabled, remove addresses with no matching prefix in network data, 158 // - When SLAAC is disabled, remove all previously added addresses. 159 static constexpr UpdateMode kModeRemove = 1 << 1; 160 161 bool ShouldFilter(const Ip6::Prefix &aPrefix) const; 162 void Update(UpdateMode aMode); 163 void GetIidSecretKey(IidSecretKey &aKey) const; 164 void HandleNotifierEvents(Events aEvents); 165 static bool DoesConfigMatchNetifAddr(const NetworkData::OnMeshPrefixConfig &aConfig, 166 const Ip6::Netif::UnicastAddress & aAddr); 167 168 bool mEnabled; 169 otIp6SlaacPrefixFilter mFilter; 170 Ip6::Netif::UnicastAddress mAddresses[OPENTHREAD_CONFIG_IP6_SLAAC_NUM_ADDRESSES]; 171 }; 172 173 /** 174 * @} 175 */ 176 177 } // namespace Utils 178 } // namespace ot 179 180 #endif // OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE 181 182 #endif // SLAAC_ADDRESS_HPP_ 183