1 /*
2  * Copyright (c) 2023 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 #include "virtual_network.h"
17 
18 #include <cinttypes>
19 
20 #include "net_manager_constants.h"
21 #include "netmanager_base_common_utils.h"
22 #include "netnative_log_wrapper.h"
23 #include "route_manager.h"
24 #include "vpn_manager.h"
25 #include "vnic_manager.h"
26 
27 namespace OHOS {
28 namespace nmd {
29 
VirtualNetwork(uint16_t netId,bool hasDns)30 VirtualNetwork::VirtualNetwork(uint16_t netId, bool hasDns) : NetsysNetwork(netId), hasDns_(hasDns) {}
31 
GetHasDns() const32 bool VirtualNetwork::GetHasDns() const
33 {
34     return hasDns_;
35 }
36 
AddUids(const std::vector<UidRange> & uidVec)37 int32_t VirtualNetwork::AddUids(const std::vector<UidRange> &uidVec)
38 {
39     std::lock_guard<std::mutex> lock(mutex_);
40     NETNATIVE_LOG_D("VirtualNetwork::AddUids update uidRanges_");
41     auto middle = uidRanges_.insert(uidRanges_.end(), uidVec.begin(), uidVec.end());
42     std::inplace_merge(uidRanges_.begin(), middle, uidRanges_.end()); // restart sort
43 
44     for (const auto &interface : interfaces_) {
45         if (RouteManager::AddUsersToVirtualNetwork(netId_, interface, uidVec)) {
46             NETNATIVE_LOGE("failed to add uids on interface %s of netId %u", interface.c_str(), netId_);
47             return NETMANAGER_ERROR;
48         }
49     }
50     return NETMANAGER_SUCCESS;
51 }
52 
RemoveUids(const std::vector<UidRange> & uidVec)53 int32_t VirtualNetwork::RemoveUids(const std::vector<UidRange> &uidVec)
54 {
55     std::lock_guard<std::mutex> lock(mutex_);
56     auto end =
57         std::set_difference(uidRanges_.begin(), uidRanges_.end(), uidVec.begin(), uidVec.end(), uidRanges_.begin());
58     uidRanges_.erase(end, uidRanges_.end());
59 
60     for (const auto &interface : interfaces_) {
61         if (RouteManager::RemoveUsersFromVirtualNetwork(netId_, interface, uidVec)) {
62             NETNATIVE_LOGE("failed to remove uids on interface %s of netId %u", interface.c_str(), netId_);
63             return NETMANAGER_ERROR;
64         }
65     }
66     return NETMANAGER_SUCCESS;
67 }
68 
AddInterface(std::string & interfaceName)69 int32_t VirtualNetwork::AddInterface(std::string &interfaceName)
70 {
71     NETNATIVE_LOGI("Entry VirtualNetwork::AddInterface %{public}s", interfaceName.c_str());
72     if (ExistInterface(interfaceName)) {
73         NETNATIVE_LOGW("Failed to add interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
74         return NETMANAGER_ERROR;
75     }
76 
77     if (VpnManager::GetInstance().CreateVpnInterface()) {
78         NETNATIVE_LOGE("create vpn interface error");
79         return NETMANAGER_ERROR;
80     }
81 
82     if (RouteManager::AddInterfaceToVirtualNetwork(netId_, interfaceName)) {
83         NETNATIVE_LOGE("Failed to add interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
84         return NETMANAGER_ERROR;
85     }
86 
87     std::lock_guard<std::mutex> lock(mutex_);
88     interfaces_.insert(interfaceName);
89     return NETMANAGER_SUCCESS;
90 }
91 
RemoveInterface(std::string & interfaceName)92 int32_t VirtualNetwork::RemoveInterface(std::string &interfaceName)
93 {
94     NETNATIVE_LOGI("Entry VirtualNetwork::RemoveInterface %{public}s", interfaceName.c_str());
95     if (!ExistInterface(interfaceName)) {
96         NETNATIVE_LOGW("Failed to remove interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
97         return NETMANAGER_SUCCESS;
98     }
99 
100     if (RouteManager::RemoveInterfaceFromVirtualNetwork(netId_, interfaceName)) {
101         NETNATIVE_LOGE("Failed to remove interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
102         return NETMANAGER_ERROR;
103     }
104 
105     VpnManager::GetInstance().DestroyVpnInterface();
106 
107     std::lock_guard<std::mutex> lock(mutex_);
108     interfaces_.erase(interfaceName);
109     return NETMANAGER_SUCCESS;
110 }
111 } // namespace nmd
112 } // namespace OHOS
113