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 class Dependencies 53 { 54 public: 55 virtual ~Dependencies(void) = default; 56 57 virtual otbrError Ip6Send(const uint8_t *aData, uint16_t aLength); 58 virtual otbrError Ip6MulAddrUpdateSubscription(const otIp6Address &aAddress, bool aIsAdded); 59 }; 60 61 Netif(Dependencies &aDependencies); 62 63 otbrError Init(const std::string &aInterfaceName); 64 void Deinit(void); 65 66 void Process(const MainloopContext *aContext); 67 void UpdateFdSet(MainloopContext *aContext); 68 void UpdateIp6UnicastAddresses(const std::vector<Ip6AddressInfo> &aAddrInfos); 69 otbrError UpdateIp6MulticastAddresses(const std::vector<Ip6Address> &aAddrs); 70 void SetNetifState(bool aState); 71 72 void Ip6Receive(const uint8_t *aBuf, uint16_t aLen); 73 74 private: 75 // TODO: Retrieve the Maximum Ip6 size from the coprocessor. 76 static constexpr size_t kIp6Mtu = 1280; 77 78 void Clear(void); 79 80 otbrError CreateTunDevice(const std::string &aInterfaceName); 81 otbrError InitNetlink(void); 82 otbrError InitMldListener(void); 83 84 void PlatformSpecificInit(void); 85 void SetAddrGenModeToNone(void); 86 void ProcessUnicastAddressChange(const Ip6AddressInfo &aAddressInfo, bool aIsAdded); 87 otbrError ProcessMulticastAddressChange(const Ip6Address &aAddress, bool aIsAdded); 88 void ProcessIp6Send(void); 89 void ProcessMldEvent(void); 90 91 int mTunFd; ///< Used to exchange IPv6 packets. 92 int mIpFd; ///< Used to manage IPv6 stack on the network interface. 93 int mNetlinkFd; ///< Used to receive netlink events. 94 int mMldFd; ///< Used to receive MLD events. 95 uint32_t mNetlinkSequence; ///< Netlink message sequence. 96 97 unsigned int mNetifIndex; 98 std::string mNetifName; 99 100 std::vector<Ip6AddressInfo> mIp6UnicastAddresses; 101 std::vector<Ip6Address> mIp6MulticastAddresses; 102 Dependencies &mDeps; 103 }; 104 105 } // namespace otbr 106 107 #endif // OTBR_AGENT_POSIX_NETIF_HPP_ 108