xref: /aosp_15_r20/external/ot-br-posix/src/backbone_router/nd_proxy.hpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
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/rcp_host.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 class NdProxyManager : public MainloopProcessor, private NonCopyable
76 {
77 public:
78     /**
79      * This constructor initializes a NdProxyManager instance.
80      */
NdProxyManager(otbr::Ncp::RcpHost & aHost,std::string aBackboneInterfaceName)81     explicit NdProxyManager(otbr::Ncp::RcpHost &aHost, std::string aBackboneInterfaceName)
82         : mHost(aHost)
83         , mBackboneInterfaceName(std::move(aBackboneInterfaceName))
84         , mIcmp6RawSock(-1)
85         , mUnicastNsQueueSock(-1)
86         , mNfqHandler(nullptr)
87         , mNfqQueueHandler(nullptr)
88     {
89     }
90 
91     /**
92      * This method initializes a ND Proxy manager instance.
93      */
94     void Init(void);
95 
96     /**
97      * This method enables the ND Proxy manager.
98      *
99      * @param[in] aDomainPrefix  The Domain Prefix.
100      */
101     void Enable(const Ip6Prefix &aDomainPrefix);
102 
103     /**
104      * This method disables the ND Proxy manager.
105      */
106     void Disable(void);
107 
108     void Update(MainloopContext &aMainloop) override;
109     void Process(const MainloopContext &aMainloop) override;
110 
111     /**
112      * This method handles a Backbone Router ND Proxy event.
113      *
114      * @param[in] aEvent  The Backbone Router ND Proxy event type.
115      * @param[in] aDua    The Domain Unicast Address of the ND Proxy, or `nullptr` if @p `aEvent` is
116      *                    `OT_BACKBONE_ROUTER_NDPROXY_CLEARED`.
117      */
118     void HandleBackboneRouterNdProxyEvent(otBackboneRouterNdProxyEvent aEvent, const otIp6Address *aDua);
119 
120     /**
121      * This method returns if the ND Proxy manager is enabled.
122      *
123      * @returns If the ND Proxy manager is enabled;
124      */
IsEnabled(void) const125     bool IsEnabled(void) const { return mIcmp6RawSock >= 0; }
126 
127 private:
128     enum
129     {
130         kMaxICMP6PacketSize = 1500, ///< Max size of an ICMP6 packet in bytes.
131     };
132 
133     void       SendNeighborAdvertisement(const Ip6Address &aTarget, const Ip6Address &aDst);
134     otbrError  UpdateMacAddress(void);
135     otbrError  InitIcmp6RawSocket(void);
136     void       FiniIcmp6RawSocket(void);
137     otbrError  InitNetfilterQueue(void);
138     void       FiniNetfilterQueue(void);
139     void       ProcessMulticastNeighborSolicition(void);
140     void       ProcessUnicastNeighborSolicition(void);
141     void       JoinSolicitedNodeMulticastGroup(const Ip6Address &aTarget) const;
142     void       LeaveSolicitedNodeMulticastGroup(const Ip6Address &aTarget) const;
143     static int HandleNetfilterQueue(struct nfq_q_handle *aNfQueueHandler,
144                                     struct nfgenmsg     *aNfMsg,
145                                     struct nfq_data     *aNfData,
146                                     void                *aContext);
147     int HandleNetfilterQueue(struct nfq_q_handle *aNfQueueHandler, struct nfgenmsg *aNfMsg, struct nfq_data *aNfData);
148 
149     otbr::Ncp::RcpHost  &mHost;
150     std::string          mBackboneInterfaceName;
151     std::set<Ip6Address> mNdProxySet;
152     uint32_t             mBackboneIfIndex;
153     int                  mIcmp6RawSock;
154     int                  mUnicastNsQueueSock;
155     struct nfq_handle   *mNfqHandler;      ///< A pointer to an NFQUEUE handler.
156     struct nfq_q_handle *mNfqQueueHandler; ///< A pointer to a newly created queue.
157     MacAddress           mMacAddress;
158     Ip6Prefix            mDomainPrefix;
159 };
160 
161 /**
162  * @}
163  */
164 
165 } // namespace BackboneRouter
166 } // namespace otbr
167 
168 #endif // OTBR_ENABLE_DUA_ROUTING
169 #endif // ND_PROXY_HPP_
170