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 DHCPv6 Client. 32 */ 33 34 #ifndef DHCP6_CLIENT_HPP_ 35 #define DHCP6_CLIENT_HPP_ 36 37 #include "openthread-core-config.h" 38 39 #if OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE 40 41 #include "common/locator.hpp" 42 #include "common/message.hpp" 43 #include "common/non_copyable.hpp" 44 #include "common/timer.hpp" 45 #include "common/trickle_timer.hpp" 46 #include "mac/mac.hpp" 47 #include "mac/mac_types.hpp" 48 #include "net/dhcp6.hpp" 49 #include "net/netif.hpp" 50 #include "net/udp6.hpp" 51 52 namespace ot { 53 54 namespace Dhcp6 { 55 56 /** 57 * @addtogroup core-dhcp6 58 * 59 * @brief 60 * This module includes definitions for DHCPv6 Client. 61 * 62 * @{ 63 */ 64 65 /** 66 * Implements DHCPv6 Client. 67 */ 68 class Client : public InstanceLocator, private NonCopyable 69 { 70 public: 71 /** 72 * Initializes the object. 73 * 74 * @param[in] aInstance A reference to the OpenThread instance. 75 */ 76 explicit Client(Instance &aInstance); 77 78 /** 79 * Update addresses that shall be automatically created using DHCP. 80 */ 81 void UpdateAddresses(void); 82 83 private: 84 static constexpr uint32_t kTrickleTimerImin = 1; 85 static constexpr uint32_t kTrickleTimerImax = 120; 86 87 enum IaStatus : uint8_t 88 { 89 kIaStatusInvalid, 90 kIaStatusSolicit, 91 kIaStatusSoliciting, 92 kIaStatusSolicitReplied, 93 }; 94 95 struct IdentityAssociation 96 { 97 Ip6::Netif::UnicastAddress mNetifAddress; 98 uint32_t mPreferredLifetime; 99 uint32_t mValidLifetime; 100 uint16_t mPrefixAgentRloc; 101 IaStatus mStatus; 102 }; 103 104 void Start(void); 105 void Stop(void); 106 107 static bool MatchNetifAddressWithPrefix(const Ip6::Netif::UnicastAddress &aNetifAddress, 108 const Ip6::Prefix &aIp6Prefix); 109 110 void Solicit(uint16_t aRloc16); 111 112 void AddIdentityAssociation(uint16_t aRloc16, otIp6Prefix &aIp6Prefix); 113 void RemoveIdentityAssociation(uint16_t aRloc16, otIp6Prefix &aIp6Prefix); 114 115 bool ProcessNextIdentityAssociation(void); 116 117 Error AppendHeader(Message &aMessage); 118 Error AppendClientIdentifier(Message &aMessage); 119 Error AppendIaNa(Message &aMessage, uint16_t aRloc16); 120 Error AppendIaAddress(Message &aMessage, uint16_t aRloc16); 121 Error AppendElapsedTime(Message &aMessage); 122 Error AppendRapidCommit(Message &aMessage); 123 124 void HandleUdpReceive(Message &aMessage, const Ip6::MessageInfo &aMessageInfo); 125 126 void ProcessReply(Message &aMessage); 127 uint16_t FindOption(Message &aMessage, uint16_t aOffset, uint16_t aLength, Code aCode); 128 Error ProcessServerIdentifier(Message &aMessage, uint16_t aOffset); 129 Error ProcessClientIdentifier(Message &aMessage, uint16_t aOffset); 130 Error ProcessIaNa(Message &aMessage, uint16_t aOffset); 131 Error ProcessStatusCode(Message &aMessage, uint16_t aOffset); 132 Error ProcessIaAddress(Message &aMessage, uint16_t aOffset); 133 134 static void HandleTrickleTimer(TrickleTimer &aTrickleTimer); 135 void HandleTrickleTimer(void); 136 137 using ClientSocket = Ip6::Udp::SocketIn<Client, &Client::HandleUdpReceive>; 138 139 ClientSocket mSocket; 140 TrickleTimer mTrickleTimer; 141 142 TransactionId mTransactionId; 143 TimeMilli mStartTime; 144 145 IdentityAssociation mIdentityAssociations[OPENTHREAD_CONFIG_DHCP6_CLIENT_NUM_PREFIXES]; 146 IdentityAssociation *mIdentityAssociationCurrent; 147 }; 148 149 /** 150 * @} 151 */ 152 153 } // namespace Dhcp6 154 } // namespace ot 155 156 #else // OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE 157 158 #if OPENTHREAD_ENABLE_DHCP6_MULTICAST_SOLICIT 159 #error "OPENTHREAD_ENABLE_DHCP6_MULTICAST_SOLICIT requires OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE to be also set." 160 #endif 161 162 #endif // OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE 163 164 #endif // DHCP6_CLIENT_HPP_ 165