• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
38 #include "core/common/non_copyable.hpp"
39 #include "posix/platform/mainloop.hpp"
40 
41 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
42 
43 namespace ot {
44 namespace Posix {
45 
46 /**
47  * This class manages infrastructure network interface.
48  *
49  */
50 class InfraNetif : public Mainloop::Source, private NonCopyable
51 {
52 public:
53     /**
54      * This method updates the fd_set and timeout for mainloop.
55      *
56      * @param[in,out]   aContext    A reference to the mainloop context.
57      *
58      */
59     void Update(otSysMainloopContext &aContext) override;
60 
61     /**
62      * This method performs infrastructure network interface processing.
63      *
64      * @param[in]   aContext   A reference to the mainloop context.
65      *
66      */
67     void Process(const otSysMainloopContext &aContext) override;
68 
69     /**
70      * This method initializes the infrastructure network interface.
71      *
72      * @note This method is called before OpenThread instance is created.
73      *
74      * @param[in]  aIfName      A pointer to infrastructure network interface name.
75      *
76      */
77     void Init(const char *aIfName);
78 
79     /**
80      * This method sets up the infrastructure network interface.
81      *
82      * @note This method is called after OpenThread instance is created.
83      *
84      */
85     void SetUp(void);
86 
87     /**
88      * This method tears down the infrastructure network interface.
89      *
90      * @note This method is called before OpenThread instance is destructed.
91      *
92      */
93     void TearDown(void);
94 
95     /**
96      * This method deinitializes the infrastructure network interface.
97      *
98      * @note This method is called after OpenThread instance is destructed.
99      *
100      */
101     void Deinit(void);
102 
103     /**
104      * This method checks whether the infrastructure network interface is running.
105      *
106      */
107     bool IsRunning(void) const;
108 
109     /**
110      * This method sends an ICMPv6 Neighbor Discovery message on given infrastructure interface.
111      *
112      * See RFC 4861: https://tools.ietf.org/html/rfc4861.
113      *
114      * @param[in]  aInfraIfIndex  The index of the infrastructure interface this message is sent to.
115      * @param[in]  aDestAddress   The destination address this message is sent to.
116      * @param[in]  aBuffer        The ICMPv6 message buffer. The ICMPv6 checksum is left zero and the
117      *                            platform should do the checksum calculate.
118      * @param[in]  aBufferLength  The length of the message buffer.
119      *
120      * @note  Per RFC 4861, the implementation should send the message with IPv6 link-local source address
121      *        of interface @p aInfraIfIndex and IP Hop Limit 255.
122      *
123      * @retval OT_ERROR_NONE    Successfully sent the ICMPv6 message.
124      * @retval OT_ERROR_FAILED  Failed to send the ICMPv6 message.
125      *
126      */
127     otError SendIcmp6Nd(uint32_t            aInfraIfIndex,
128                         const otIp6Address &aDestAddress,
129                         const uint8_t *     aBuffer,
130                         uint16_t            aBufferLength);
131 
132     /**
133      * This method gets the infrastructure network interface name.
134      *
135      * @returns The infrastructure network interface name, or `nullptr` if not specified.
136      *
137      */
GetNetifName(void) const138     const char *GetNetifName(void) const { return (mInfraIfIndex != 0) ? mInfraIfName : nullptr; }
139 
140     /**
141      * This function gets the infrastructure network interface singleton.
142      *
143      * @returns The singleton object.
144      *
145      */
146     static InfraNetif &Get(void);
147 
148 private:
149     char     mInfraIfName[IFNAMSIZ];
150     uint32_t mInfraIfIndex       = 0;
151     int      mInfraIfIcmp6Socket = -1;
152     int      mNetLinkSocket      = -1;
153 
154     void ReceiveNetLinkMessage(void);
155     void ReceiveIcmp6Message(void);
156     bool HasLinkLocalAddress(void) const;
157 };
158 
159 } // namespace Posix
160 } // namespace ot
161 #endif // OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
162