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 #ifdef SUPPORT_SYSVPN
27 #include "multi_vpn_manager.h"
28 #endif
29
30 namespace OHOS {
31 namespace nmd {
32
VirtualNetwork(uint16_t netId,bool hasDns)33 VirtualNetwork::VirtualNetwork(uint16_t netId, bool hasDns) : NetsysNetwork(netId), hasDns_(hasDns) {}
34
GetHasDns() const35 bool VirtualNetwork::GetHasDns() const
36 {
37 return hasDns_;
38 }
39
AddUids(const std::vector<UidRange> & uidVec)40 int32_t VirtualNetwork::AddUids(const std::vector<UidRange> &uidVec)
41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 NETNATIVE_LOG_D("VirtualNetwork::AddUids update uidRanges_");
44 auto middle = uidRanges_.insert(uidRanges_.end(), uidVec.begin(), uidVec.end());
45 std::inplace_merge(uidRanges_.begin(), middle, uidRanges_.end()); // restart sort
46
47 for (const auto &interface : interfaces_) {
48 if (RouteManager::AddUsersToVirtualNetwork(netId_, interface, uidVec)) {
49 NETNATIVE_LOGE("failed to add uids on interface %s of netId %u", interface.c_str(), netId_);
50 return NETMANAGER_ERROR;
51 }
52 }
53 return NETMANAGER_SUCCESS;
54 }
55
RemoveUids(const std::vector<UidRange> & uidVec)56 int32_t VirtualNetwork::RemoveUids(const std::vector<UidRange> &uidVec)
57 {
58 std::lock_guard<std::mutex> lock(mutex_);
59 auto end =
60 std::set_difference(uidRanges_.begin(), uidRanges_.end(), uidVec.begin(), uidVec.end(), uidRanges_.begin());
61 uidRanges_.erase(end, uidRanges_.end());
62
63 for (const auto &interface : interfaces_) {
64 if (RouteManager::RemoveUsersFromVirtualNetwork(netId_, interface, uidVec)) {
65 NETNATIVE_LOGE("failed to remove uids on interface %s of netId %u", interface.c_str(), netId_);
66 return NETMANAGER_ERROR;
67 }
68 }
69 return NETMANAGER_SUCCESS;
70 }
71
AddInterface(std::string & interfaceName)72 int32_t VirtualNetwork::AddInterface(std::string &interfaceName)
73 {
74 NETNATIVE_LOGI("Entry VirtualNetwork::AddInterface %{public}s", interfaceName.c_str());
75 if (ExistInterface(interfaceName)) {
76 NETNATIVE_LOGW("Failed to add interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
77 return NETMANAGER_ERROR;
78 }
79 int ret = NETMANAGER_SUCCESS;
80 #ifdef SUPPORT_SYSVPN
81 if ((strncmp(interfaceName.c_str(), XFRM_CARD_NAME, strlen(XFRM_CARD_NAME)) == 0) ||
82 (strncmp(interfaceName.c_str(), PPP_CARD_NAME, strlen(PPP_CARD_NAME)) == 0) ||
83 (strncmp(interfaceName.c_str(), MULTI_TUN_CARD_NAME, strlen(MULTI_TUN_CARD_NAME)) == 0)) {
84 ret = MultiVpnManager::GetInstance().CreateVpnInterface(interfaceName);
85 } else {
86 ret = VpnManager::GetInstance().CreateVpnInterface();
87 }
88 #else
89 ret = VpnManager::GetInstance().CreateVpnInterface();
90 #endif // SUPPORT_SYSVPN
91 if (ret != NETMANAGER_SUCCESS) {
92 NETNATIVE_LOGE("create vpn interface error");
93 return NETMANAGER_ERROR;
94 }
95
96 if (RouteManager::AddInterfaceToVirtualNetwork(netId_, interfaceName)) {
97 NETNATIVE_LOGE("Failed to add interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
98 return NETMANAGER_ERROR;
99 }
100
101 std::lock_guard<std::mutex> lock(mutex_);
102 interfaces_.insert(interfaceName);
103 return NETMANAGER_SUCCESS;
104 }
105
RemoveInterface(std::string & interfaceName)106 int32_t VirtualNetwork::RemoveInterface(std::string &interfaceName)
107 {
108 NETNATIVE_LOGI("Entry VirtualNetwork::RemoveInterface %{public}s", interfaceName.c_str());
109 if (!ExistInterface(interfaceName)) {
110 NETNATIVE_LOGW("Failed to remove interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
111 return NETMANAGER_SUCCESS;
112 }
113
114 if (RouteManager::RemoveInterfaceFromVirtualNetwork(netId_, interfaceName)) {
115 NETNATIVE_LOGE("Failed to remove interface %{public}s to netId_ %{public}u", interfaceName.c_str(), netId_);
116 return NETMANAGER_ERROR;
117 }
118 #ifdef SUPPORT_SYSVPN
119 if ((strncmp(interfaceName.c_str(), XFRM_CARD_NAME, strlen(XFRM_CARD_NAME)) == 0) ||
120 (strncmp(interfaceName.c_str(), PPP_CARD_NAME, strlen(PPP_CARD_NAME)) == 0) ||
121 (strncmp(interfaceName.c_str(), MULTI_TUN_CARD_NAME, strlen(MULTI_TUN_CARD_NAME)) == 0)) {
122 MultiVpnManager::GetInstance().DestroyVpnInterface(interfaceName);
123 } else {
124 VpnManager::GetInstance().DestroyVpnInterface();
125 }
126 #else
127 VpnManager::GetInstance().DestroyVpnInterface();
128 #endif // SUPPORT_SYSVPN
129 std::lock_guard<std::mutex> lock(mutex_);
130 interfaces_.erase(interfaceName);
131 return NETMANAGER_SUCCESS;
132 }
133
134 #ifdef SUPPORT_SYSVPN
UpdateVpnRules(const std::vector<std::string> & extMessages,bool add)135 int32_t VirtualNetwork::UpdateVpnRules(const std::vector<std::string> &extMessages, bool add)
136 {
137 std::lock_guard<std::mutex> lock(mutex_);
138 NETNATIVE_LOGI("VirtualNetwork::UpdateVpnRules netId %{public}d", netId_);
139 return RouteManager::UpdateVpnRules(netId_, *interfaces_.begin(), extMessages, add);
140 }
141 #endif // SUPPORT_SYSVPN
142 } // namespace nmd
143 } // namespace OHOS
144