1 /* 2 * Copyright (c) 2021, 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 implements the infrastructure interface for posix. 32 */ 33 34 #include "openthread-posix-config.h" 35 36 #include <net/if.h> 37 #include <openthread/nat64.h> 38 #include <openthread/openthread-system.h> 39 40 #include "core/common/non_copyable.hpp" 41 #include "posix/platform/mainloop.hpp" 42 43 #if OPENTHREAD_POSIX_CONFIG_INFRA_IF_ENABLE 44 45 namespace ot { 46 namespace Posix { 47 48 /** 49 * Manages infrastructure network interface. 50 * 51 */ 52 class InfraNetif : public Mainloop::Source, private NonCopyable 53 { 54 public: 55 /** 56 * Updates the fd_set and timeout for mainloop. 57 * 58 * @param[in,out] aContext A reference to the mainloop context. 59 * 60 */ 61 void Update(otSysMainloopContext &aContext) override; 62 63 /** 64 * Performs infrastructure network interface processing. 65 * 66 * @param[in] aContext A reference to the mainloop context. 67 * 68 */ 69 void Process(const otSysMainloopContext &aContext) override; 70 71 /** 72 * Initializes the infrastructure network interface. 73 * 74 * @note This method is called before OpenThread instance is created. 75 * 76 * @param[in] aIfName A pointer to infrastructure network interface name. 77 * 78 */ 79 void Init(const char *aIfName); 80 81 /** 82 * Sets up the infrastructure network interface. 83 * 84 * @note This method is called after OpenThread instance is created. 85 * 86 */ 87 void SetUp(void); 88 89 /** 90 * Tears down the infrastructure network interface. 91 * 92 * @note This method is called before OpenThread instance is destructed. 93 * 94 */ 95 void TearDown(void); 96 97 /** 98 * Deinitializes the infrastructure network interface. 99 * 100 * @note This method is called after OpenThread instance is destructed. 101 * 102 */ 103 void Deinit(void); 104 105 /** 106 * Checks whether the infrastructure network interface is running. 107 * 108 */ 109 bool IsRunning(void) const; 110 111 /** 112 * Returns the ifr_flags of the infrastructure network interface. 113 * 114 * @returns The ifr_flags of the infrastructure network interface. 115 * 116 */ 117 uint32_t GetFlags(void) const; 118 119 /** 120 * This functions counts the number of addresses on the infrastructure network interface. 121 * 122 * @param[out] aAddressCounters The counters of addresses on infrastructure network interface. 123 * 124 */ 125 void CountAddresses(otSysInfraNetIfAddressCounters &aAddressCounters) const; 126 127 /** 128 * Sends an ICMPv6 Neighbor Discovery message on given infrastructure interface. 129 * 130 * See RFC 4861: https://tools.ietf.org/html/rfc4861. 131 * 132 * @param[in] aInfraIfIndex The index of the infrastructure interface this message is sent to. 133 * @param[in] aDestAddress The destination address this message is sent to. 134 * @param[in] aBuffer The ICMPv6 message buffer. The ICMPv6 checksum is left zero and the 135 * platform should do the checksum calculate. 136 * @param[in] aBufferLength The length of the message buffer. 137 * 138 * @note Per RFC 4861, the implementation should send the message with IPv6 link-local source address 139 * of interface @p aInfraIfIndex and IP Hop Limit 255. 140 * 141 * @retval OT_ERROR_NONE Successfully sent the ICMPv6 message. 142 * @retval OT_ERROR_FAILED Failed to send the ICMPv6 message. 143 * 144 */ 145 otError SendIcmp6Nd(uint32_t aInfraIfIndex, 146 const otIp6Address &aDestAddress, 147 const uint8_t *aBuffer, 148 uint16_t aBufferLength); 149 150 /** 151 * Sends an asynchronous address lookup for the well-known host name "ipv4only.arpa" 152 * to discover the NAT64 prefix. 153 * 154 * @param[in] aInfraIfIndex The index of the infrastructure interface the address look-up is sent to. 155 * 156 * @retval OT_ERROR_NONE Successfully request address look-up. 157 * @retval OT_ERROR_FAILED Failed to request address look-up. 158 * 159 */ 160 otError DiscoverNat64Prefix(uint32_t aInfraIfIndex); 161 162 /** 163 * Gets the infrastructure network interface name. 164 * 165 * @returns The infrastructure network interface name, or `nullptr` if not specified. 166 * 167 */ GetNetifName(void) const168 const char *GetNetifName(void) const { return (mInfraIfIndex != 0) ? mInfraIfName : nullptr; } 169 170 /** 171 * Gets the infrastructure network interface singleton. 172 * 173 * @returns The singleton object. 174 * 175 */ 176 static InfraNetif &Get(void); 177 178 private: 179 static const char kWellKnownIpv4OnlyName[]; // "ipv4only.arpa" 180 static const otIp4Address kWellKnownIpv4OnlyAddress1; // 192.0.0.170 181 static const otIp4Address kWellKnownIpv4OnlyAddress2; // 192.0.0.171 182 static const uint8_t kValidNat64PrefixLength[]; 183 184 char mInfraIfName[IFNAMSIZ]; 185 uint32_t mInfraIfIndex = 0; 186 int mInfraIfIcmp6Socket = -1; 187 int mNetLinkSocket = -1; 188 189 void ReceiveNetLinkMessage(void); 190 void ReceiveIcmp6Message(void); 191 bool HasLinkLocalAddress(void) const; 192 static void DiscoverNat64PrefixDone(union sigval sv); 193 }; 194 195 } // namespace Posix 196 } // namespace ot 197 #endif // OPENTHREAD_POSIX_CONFIG_INFRA_IF_ENABLE 198