Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / src / backbone_router / nd_proxy.hpp
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 #ifdef __APPLE__
38 #define __APPLE_USE_RFC_3542
39 #endif
40
41 #include <inttypes.h>
42 #include <libnetfilter_queue/libnetfilter_queue.h>
43 #include <map>
44 #include <netinet/in.h>
45 #include <set>
46 #include <string>
47
48 #include <openthread/backbone_router_ftd.h>
49
50 #include "agent/ncp_openthread.hpp"
51 #include "common/types.hpp"
52
53 namespace otbr {
54 namespace BackboneRouter {
55
56 /**
57  * @addtogroup border-router-bbr
58  *
59  * @brief
60  *   This module includes definition for ND Proxy manager.
61  *
62  * @{
63  */
64
65 /**
66  * This class implements ND Proxy manager.
67  *
68  */
69 class NdProxyManager
70 {
71 public:
72     /**
73      * This constructor initializes a NdProxyManager instance.
74      *
75      */
76     explicit NdProxyManager(otbr::Ncp::ControllerOpenThread &aNcp)
77         : mNcp(aNcp)
78         , mIcmp6RawSock(-1)
79         , mUnicastNsQueueSock(-1)
80         , mNfqHandler(nullptr)
81         , mNfqQueueHandler(nullptr)
82     {
83     }
84
85     /**
86      * This method initializes a ND Proxy manager instance.
87      *
88      */
89     void Init(void);
90
91     /**
92      * This method enables the ND Proxy manager.
93      *
94      * @param[in] aDomainPrefix  The Domain Prefix.
95      *
96      */
97     void Enable(const Ip6Prefix &aDomainPrefix);
98
99     /**
100      * This method disables the ND Proxy manager.
101      *
102      */
103     void Disable(void);
104
105     /**
106      * This method updates the fd_set and timeout for mainloop.
107      *
108      * @param[inout]    aReadFdSet      A reference to fd_set for polling read.
109      * @param[inout]    aWriteFdSet     A reference to fd_set for polling read.
110      * @param[inout]    aErrorFdSet     A reference to fd_set for polling error.
111      * @param[inout]    aMaxFd          A reference to the current max fd in @p aReadFdSet and @p aWriteFdSet.
112      * @param[inout]    aTimeout        A reference to the timeout.
113      *
114      */
115     void UpdateFdSet(fd_set & aReadFdSet,
116                      fd_set & aWriteFdSet,
117                      fd_set & aErrorFdSet,
118                      int &    aMaxFd,
119                      timeval &aTimeout) const;
120
121     /**
122      * This method performs border agent processing.
123      *
124      * @param[in]   aReadFdSet   A reference to read file descriptors.
125      * @param[in]   aWriteFdSet  A reference to write file descriptors.
126      * @param[in]   aErrorFdSet  A reference to error file descriptors.
127      *
128      */
129     void Process(const fd_set &aReadFdSet, const fd_set &aWriteFdSet, const fd_set &aErrorFdSet);
130
131     /**
132      * This method handles a Backbone Router ND Proxy event.
133      *
134      * @param[in] aEvent    The Backbone Router ND Proxy event type.
135      * @param[in] aDua      The Domain Unicast Address of the ND Proxy, or `nullptr` if @p `aEvent` is
136      *                      `OT_BACKBONE_ROUTER_NDPROXY_CLEARED`.
137      *
138      */
139     void HandleBackboneRouterNdProxyEvent(otBackboneRouterNdProxyEvent aEvent, const otIp6Address *aDua);
140
141     /**
142      * This method returns if the ND Proxy manager is enabled.
143      *
144      * @returns  If the ND Proxy manager is enabled;
145      *
146      */
147     bool IsEnabled(void) const { return mIcmp6RawSock >= 0; }
148
149 private:
150     enum
151     {
152         kMaxICMP6PacketSize = 1500, ///< Max size of an ICMP6 packet in bytes.
153     };
154
155     void       SendNeighborAdvertisement(const Ip6Address &aTarget, const Ip6Address &aDst);
156     otbrError  UpdateMacAddress(void);
157     otbrError  InitIcmp6RawSocket(void);
158     void       FiniIcmp6RawSocket(void);
159     otbrError  InitNetfilterQueue(void);
160     void       FiniNetfilterQueue(void);
161     void       ProcessMulticastNeighborSolicition(void);
162     void       ProcessUnicastNeighborSolicition(void);
163     void       JoinSolicitedNodeMulticastGroup(const Ip6Address &aTarget) const;
164     void       LeaveSolicitedNodeMulticastGroup(const Ip6Address &aTarget) const;
165     static int HandleNetfilterQueue(struct nfq_q_handle *aNfQueueHandler,
166                                     struct nfgenmsg *    aNfMsg,
167                                     struct nfq_data *    aNfData,
168                                     void *               aContext);
169     int HandleNetfilterQueue(struct nfq_q_handle *aNfQueueHandler, struct nfgenmsg *aNfMsg, struct nfq_data *aNfData);
170
171     otbr::Ncp::ControllerOpenThread &mNcp;
172     std::set<Ip6Address>             mNdProxySet;
173     uint32_t                         mBackboneIfIndex;
174     int                              mIcmp6RawSock;
175     int                              mUnicastNsQueueSock;
176     struct nfq_handle *              mNfqHandler;      ///< A pointer to an NFQUEUE handler.
177     struct nfq_q_handle *            mNfqQueueHandler; ///< A pointer to a newly created queue.
178     MacAddress                       mMacAddress;
179     Ip6Prefix                        mDomainPrefix;
180 };
181
182 /**
183  * @}
184  */
185
186 } // namespace BackboneRouter
187 } // namespace otbr
188
189 #endif // ND_PROXY_HPP_