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