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