1 /* 2 * Copyright (c) 2020, 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 definition for ICMPv6 Neighbor Advertisement (ND) proxy management. 32 */ 33 34 #ifndef ND_PROXY_HPP_ 35 #define ND_PROXY_HPP_ 36 37 #include "openthread-br/config.h" 38 39 #if OTBR_ENABLE_DUA_ROUTING 40 41 #ifdef __APPLE__ 42 #define __APPLE_USE_RFC_3542 43 #endif 44 45 #include <inttypes.h> 46 #include <libnetfilter_queue/libnetfilter_queue.h> 47 #include <map> 48 #include <netinet/in.h> 49 #include <set> 50 #include <string> 51 #include <utility> 52 53 #include <openthread/backbone_router_ftd.h> 54 55 #include "common/code_utils.hpp" 56 #include "common/mainloop.hpp" 57 #include "common/types.hpp" 58 #include "ncp/ncp_openthread.hpp" 59 60 namespace otbr { 61 namespace BackboneRouter { 62 63 /** 64 * @addtogroup border-router-bbr 65 * 66 * @brief 67 * This module includes definition for ND Proxy manager. 68 * 69 * @{ 70 */ 71 72 /** 73 * This class implements ND Proxy manager. 74 * 75 */ 76 class NdProxyManager : public MainloopProcessor, private NonCopyable 77 { 78 public: 79 /** 80 * This constructor initializes a NdProxyManager instance. 81 * 82 */ NdProxyManager(otbr::Ncp::ControllerOpenThread & aNcp,std::string aBackboneInterfaceName)83 explicit NdProxyManager(otbr::Ncp::ControllerOpenThread &aNcp, std::string aBackboneInterfaceName) 84 : mNcp(aNcp) 85 , mBackboneInterfaceName(std::move(aBackboneInterfaceName)) 86 , mIcmp6RawSock(-1) 87 , mUnicastNsQueueSock(-1) 88 , mNfqHandler(nullptr) 89 , mNfqQueueHandler(nullptr) 90 { 91 } 92 93 /** 94 * This method initializes a ND Proxy manager instance. 95 * 96 */ 97 void Init(void); 98 99 /** 100 * This method enables the ND Proxy manager. 101 * 102 * @param[in] aDomainPrefix The Domain Prefix. 103 * 104 */ 105 void Enable(const Ip6Prefix &aDomainPrefix); 106 107 /** 108 * This method disables the ND Proxy manager. 109 * 110 */ 111 void Disable(void); 112 113 void Update(MainloopContext &aMainloop) override; 114 void Process(const MainloopContext &aMainloop) override; 115 116 /** 117 * This method handles a Backbone Router ND Proxy event. 118 * 119 * @param[in] aEvent The Backbone Router ND Proxy event type. 120 * @param[in] aDua The Domain Unicast Address of the ND Proxy, or `nullptr` if @p `aEvent` is 121 * `OT_BACKBONE_ROUTER_NDPROXY_CLEARED`. 122 * 123 */ 124 void HandleBackboneRouterNdProxyEvent(otBackboneRouterNdProxyEvent aEvent, const otIp6Address *aDua); 125 126 /** 127 * This method returns if the ND Proxy manager is enabled. 128 * 129 * @returns If the ND Proxy manager is enabled; 130 * 131 */ IsEnabled(void) const132 bool IsEnabled(void) const { return mIcmp6RawSock >= 0; } 133 134 private: 135 enum 136 { 137 kMaxICMP6PacketSize = 1500, ///< Max size of an ICMP6 packet in bytes. 138 }; 139 140 void SendNeighborAdvertisement(const Ip6Address &aTarget, const Ip6Address &aDst); 141 otbrError UpdateMacAddress(void); 142 otbrError InitIcmp6RawSocket(void); 143 void FiniIcmp6RawSocket(void); 144 otbrError InitNetfilterQueue(void); 145 void FiniNetfilterQueue(void); 146 void ProcessMulticastNeighborSolicition(void); 147 void ProcessUnicastNeighborSolicition(void); 148 void JoinSolicitedNodeMulticastGroup(const Ip6Address &aTarget) const; 149 void LeaveSolicitedNodeMulticastGroup(const Ip6Address &aTarget) const; 150 static int HandleNetfilterQueue(struct nfq_q_handle *aNfQueueHandler, 151 struct nfgenmsg *aNfMsg, 152 struct nfq_data *aNfData, 153 void *aContext); 154 int HandleNetfilterQueue(struct nfq_q_handle *aNfQueueHandler, struct nfgenmsg *aNfMsg, struct nfq_data *aNfData); 155 156 otbr::Ncp::ControllerOpenThread &mNcp; 157 std::string mBackboneInterfaceName; 158 std::set<Ip6Address> mNdProxySet; 159 uint32_t mBackboneIfIndex; 160 int mIcmp6RawSock; 161 int mUnicastNsQueueSock; 162 struct nfq_handle *mNfqHandler; ///< A pointer to an NFQUEUE handler. 163 struct nfq_q_handle *mNfqQueueHandler; ///< A pointer to a newly created queue. 164 MacAddress mMacAddress; 165 Ip6Prefix mDomainPrefix; 166 }; 167 168 /** 169 * @} 170 */ 171 172 } // namespace BackboneRouter 173 } // namespace otbr 174 175 #endif // OTBR_ENABLE_DUA_ROUTING 176 #endif // ND_PROXY_HPP_ 177