1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef NET_VNIC_MANAGER_H 17 #define NET_VNIC_MANAGER_H 18 19 #include "uid_range.h" 20 #include <cstdint> 21 #include <linux/if.h> 22 #include <map> 23 #include <set> 24 #include <string> 25 #include <mutex> 26 27 namespace OHOS { 28 namespace NetManagerStandard { 29 30 class VnicManager { 31 private: 32 VnicManager() = default; 33 ~VnicManager() = default; 34 VnicManager(const VnicManager &) = delete; 35 VnicManager &operator=(const VnicManager &) = delete; 36 37 public: GetInstance()38 static VnicManager &GetInstance() 39 { 40 static VnicManager instance; 41 return instance; 42 } 43 44 public: 45 int32_t CreateVnic(uint16_t mtu, const std::string &tunAddr, int32_t prefix, const std::set<int32_t> &uids); 46 int32_t DestroyVnic(); 47 int32_t CreateVnicInterface(); 48 void DestroyVnicInterface(); 49 int32_t SetVnicMtu(const std::string &ifName, int32_t mtu); 50 int32_t SetVnicAddress(const std::string &ifName, const std::string &tunAddr, int32_t prefix); 51 52 private: 53 std::atomic_int& GetNetSock(bool ipv4); 54 int32_t SetVnicUp(); 55 int32_t SetVnicDown(); 56 int32_t AddDefaultRoute(); 57 int32_t DelDefaultRoute(); 58 int32_t InitIfreq(ifreq &ifr, const std::string &cardName); 59 int32_t SetVnicResult(std::atomic_int &fd, unsigned long cmd, ifreq &ifr); 60 61 private: 62 std::atomic_int tunFd_ = 0; 63 std::atomic_int net4Sock_ = 0; 64 std::atomic_int net6Sock_ = 0; 65 std::vector<NetManagerStandard::UidRange> uidRanges; 66 std::mutex vnicMutex_; 67 }; 68 } // namespace NetManagerStandard 69 } // namespace OHOS 70 #endif // NET_VNIC_MANAGER_H 71