1 /* 2 * Copyright (c) 2022, 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 infrastructure network interface. 32 * 33 */ 34 35 #ifndef INFRA_IF_HPP_ 36 #define INFRA_IF_HPP_ 37 38 #include "openthread-core-config.h" 39 40 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE 41 42 #include <openthread/platform/infra_if.h> 43 44 #include "common/data.hpp" 45 #include "common/error.hpp" 46 #include "common/locator.hpp" 47 #include "common/string.hpp" 48 #include "net/ip6.hpp" 49 50 namespace ot { 51 namespace BorderRouter { 52 53 /** 54 * This class represents the infrastructure network interface on a border router. 55 * 56 */ 57 class InfraIf : public InstanceLocator 58 { 59 public: 60 static constexpr uint16_t kInfoStringSize = 20; ///< Max chars for the info string (`ToString()`). 61 62 typedef String<kInfoStringSize> InfoString; ///< String type returned from `ToString()`. 63 typedef Data<kWithUint16Length> Icmp6Packet; ///< An IMCPv6 packet (data containing the IP payload) 64 65 /** 66 * This constructor initializes the `InfraIf`. 67 * 68 * @param[in] aInstance A OpenThread instance. 69 * 70 */ 71 explicit InfraIf(Instance &aInstance); 72 73 /** 74 * This method initializes the `InfraIf`. 75 * 76 * @param[in] aIfIndex The infrastructure interface index. 77 * 78 * @retval kErrorNone Successfully initialized the `InfraIf`. 79 * @retval kErrorInvalidArgs The index of the infra interface is not valid. 80 * @retval kErrorInvalidState The `InfraIf` is already initialized. 81 * 82 */ 83 Error Init(uint32_t aIfIndex); 84 85 /** 86 * This method deinitilaizes the `InfraIf`. 87 * 88 */ 89 void Deinit(void); 90 91 /** 92 * This method indicates whether or not the `InfraIf` is initialized. 93 * 94 * @retval TRUE The `InfraIf` is initialized. 95 * @retval FALSE The `InfraIf` is not initialized. 96 * 97 */ IsInitialized(void) const98 bool IsInitialized(void) const { return mInitialized; } 99 100 /** 101 * This method indicates whether or not the infra interface is running. 102 * 103 * @retval TRUE The infrastructure interface is running. 104 * @retval FALSE The infrastructure interface is not running. 105 * 106 */ IsRunning(void) const107 bool IsRunning(void) const { return mIsRunning; } 108 109 /** 110 * This method returns the infrastructure interface index. 111 * 112 * @returns The interface index or zero if not initialized. 113 * 114 */ GetIfIndex(void) const115 uint32_t GetIfIndex(void) const { return mIfIndex; } 116 117 /** 118 * This method indicates whether or not the infra interface has the given IPv6 address assigned. 119 * 120 * This method MUST be used when interface is initialized. 121 * 122 * @param[in] aAddress The IPv6 address. 123 * 124 * @retval TRUE The infrastructure interface has @p aAddress. 125 * @retval FALSE The infrastructure interface does not have @p aAddress. 126 * 127 */ 128 bool HasAddress(const Ip6::Address &aAddress); 129 130 /** 131 * This method sends an ICMPv6 Neighbor Discovery packet on the infrastructure interface. 132 * 133 * This method MUST be used when interface is initialized. 134 * 135 * @param[in] aPacket The ICMPv6 packet to send. 136 * @param[in] aDestination The destination address. 137 * 138 * @retval kErrorNone Successfully sent the ICMPv6 message. 139 * @retval kErrorFailed Failed to send the ICMPv6 message. 140 * 141 */ 142 Error Send(const Icmp6Packet &aPacket, const Ip6::Address &aDestination); 143 144 /** 145 * This method processes a received ICMPv6 Neighbor Discovery packet from an infrastructure interface. 146 * 147 * @param[in] aIfIndex The infrastructure interface index on which the ICMPv6 message is received. 148 * @param[in] aSource The IPv6 source address. 149 * @param[in] aPacket The ICMPv6 packet. 150 * 151 */ 152 void HandledReceived(uint32_t aIfIndex, const Ip6::Address &aSource, const Icmp6Packet &aPacket); 153 154 /** 155 * This method handles infrastructure interface state changes. 156 * 157 * @param[in] aIfIndex The infrastructure interface index. 158 * @param[in] aIsRunning A boolean that indicates whether the infrastructure interface is running. 159 * 160 * @retval kErrorNone Successfully updated the infra interface status. 161 * @retval kErrorInvalidState The `InfraIf` is not initialized. 162 * @retval kErrorInvalidArgs The @p IfIndex does not match the interface index of `InfraIf`. 163 * 164 */ 165 Error HandleStateChanged(uint32_t aIfIndex, bool aIsRunning); 166 167 /** 168 * This method converts the `InfraIf` to a human-readable string. 169 * 170 * @returns The string representation of `InfraIf`. 171 * 172 */ 173 InfoString ToString(void) const; 174 175 private: 176 bool mInitialized : 1; 177 bool mIsRunning : 1; 178 uint32_t mIfIndex; 179 }; 180 181 } // namespace BorderRouter 182 } // namespace ot 183 184 #endif // OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE 185 186 #endif // INFRA_IF_HPP_ 187