xref: /aosp_15_r20/external/ot-br-posix/src/ncp/posix/netif.hpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1 /*
2  *  Copyright (c) 2024, 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 definitions of the posix Netif of otbr-agent.
32  */
33 
34 #ifndef OTBR_AGENT_POSIX_NETIF_HPP_
35 #define OTBR_AGENT_POSIX_NETIF_HPP_
36 
37 #include <net/if.h>
38 
39 #include <functional>
40 #include <vector>
41 
42 #include <openthread/ip6.h>
43 
44 #include "common/mainloop.hpp"
45 #include "common/types.hpp"
46 
47 namespace otbr {
48 
49 class Netif
50 {
51 public:
52     using Ip6SendFunc = std::function<otbrError(const uint8_t *, uint16_t)>;
53 
54     Netif(void);
55 
56     otbrError Init(const std::string &aInterfaceName, const Ip6SendFunc &aIp6SendFunc);
57     void      Deinit(void);
58 
59     void      Process(const MainloopContext *aContext);
60     void      UpdateFdSet(MainloopContext *aContext);
61     void      UpdateIp6UnicastAddresses(const std::vector<Ip6AddressInfo> &aAddrInfos);
62     otbrError UpdateIp6MulticastAddresses(const std::vector<Ip6Address> &aAddrs);
63     void      SetNetifState(bool aState);
64 
65     void Ip6Receive(const uint8_t *aBuf, uint16_t aLen);
66 
67 private:
68     // TODO: Retrieve the Maximum Ip6 size from the coprocessor.
69     static constexpr size_t kIp6Mtu = 1280;
70 
71     void Clear(void);
72 
73     otbrError CreateTunDevice(const std::string &aInterfaceName);
74     otbrError InitNetlink(void);
75 
76     void      PlatformSpecificInit(void);
77     void      SetAddrGenModeToNone(void);
78     void      ProcessUnicastAddressChange(const Ip6AddressInfo &aAddressInfo, bool aIsAdded);
79     otbrError ProcessMulticastAddressChange(const Ip6Address &aAddress, bool aIsAdded);
80     void      ProcessIp6Send(void);
81 
82     int      mTunFd;           ///< Used to exchange IPv6 packets.
83     int      mIpFd;            ///< Used to manage IPv6 stack on the network interface.
84     int      mNetlinkFd;       ///< Used to receive netlink events.
85     uint32_t mNetlinkSequence; ///< Netlink message sequence.
86 
87     unsigned int mNetifIndex;
88     std::string  mNetifName;
89 
90     std::vector<Ip6AddressInfo> mIp6UnicastAddresses;
91     std::vector<Ip6Address>     mIp6MulticastAddresses;
92     Ip6SendFunc                 mIp6SendFunc;
93 };
94 
95 } // namespace otbr
96 
97 #endif // OTBR_AGENT_POSIX_NETIF_HPP_
98