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 "networkvpn_client.h"
17
18 #include <thread>
19 #ifdef SUPPORT_SYSVPN
20 #include <vector>
21 #endif // SUPPORT_SYSVPN
22
23 #include "fwmark_client.h"
24 #include "iservice_registry.h"
25 #include "net_manager_constants.h"
26 #include "netmgr_ext_log_wrapper.h"
27 #include "system_ability_definition.h"
28
29 namespace OHOS {
30 namespace NetManagerStandard {
31
32 static constexpr uint32_t WAIT_FOR_SERVICE_TIME_MS = 500;
33 static constexpr uint32_t MAX_GET_SERVICE_COUNT = 10;
34
OnVpnMultiUserSetUp()35 void VpnSetUpEventCallback::OnVpnMultiUserSetUp()
36 {
37 NETMGR_EXT_LOG_I("vpn multiple user setup event.");
38 NetworkVpnClient::GetInstance().multiUserSetUpEvent();
39 }
40
GetInstance()41 NetworkVpnClient &NetworkVpnClient::GetInstance()
42 {
43 static NetworkVpnClient instance;
44 return instance;
45 }
46
Prepare(bool & isExistVpn,bool & isRun,std::string & pkg)47 int32_t NetworkVpnClient::Prepare(bool &isExistVpn, bool &isRun, std::string &pkg)
48 {
49 sptr<INetworkVpnService> proxy = GetProxy();
50 if (proxy == nullptr) {
51 NETMGR_EXT_LOG_E("Prepare proxy is nullptr");
52 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
53 }
54 return proxy->Prepare(isExistVpn, isRun, pkg);
55 }
56
Protect(int32_t socketFd,bool isVpnExtCall)57 int32_t NetworkVpnClient::Protect(int32_t socketFd, bool isVpnExtCall)
58 {
59 if (socketFd <= 0) {
60 NETMGR_EXT_LOG_E("Invalid socket file discriptor");
61 return NETWORKVPN_ERROR_INVALID_FD;
62 }
63
64 sptr<INetworkVpnService> proxy = GetProxy();
65 if (proxy == nullptr) {
66 NETMGR_EXT_LOG_E("Protect proxy is nullptr");
67 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
68 }
69 int32_t result = proxy->Protect(isVpnExtCall);
70 if (result != NETMANAGER_EXT_SUCCESS) {
71 return result;
72 }
73 nmd::FwmarkClient fwmarkClient;
74 int32_t protectResult = fwmarkClient.ProtectFromVpn(socketFd);
75 if (protectResult == NETMANAGER_ERROR) {
76 return NETWORKVPN_ERROR_INVALID_FD;
77 }
78 return protectResult;
79 }
80
SetUpVpn(sptr<VpnConfig> config,int32_t & tunFd,bool isVpnExtCall)81 int32_t NetworkVpnClient::SetUpVpn(sptr<VpnConfig> config, int32_t &tunFd, bool isVpnExtCall)
82 {
83 if (config == nullptr) {
84 NETMGR_EXT_LOG_E("SetUpVpn param config is nullptr");
85 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
86 }
87
88 sptr<INetworkVpnService> proxy = GetProxy();
89 if (proxy == nullptr) {
90 NETMGR_EXT_LOG_E("SetUpVpn proxy is nullptr");
91 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
92 }
93 NETMGR_EXT_LOG_I("enter SetUpVpn 1, %{public}d", isVpnExtCall);
94 int32_t result = proxy->SetUpVpn(config, isVpnExtCall);
95 if (result != NETMANAGER_EXT_SUCCESS) {
96 tunFd = 0;
97 return result;
98 }
99 clientVpnConfig_.first = config;
100 clientVpnConfig_.second = isVpnExtCall;
101
102 tunFd = vpnInterface_.GetVpnInterfaceFd();
103 if (tunFd <= 0) {
104 return NETMANAGER_EXT_ERR_INTERNAL;
105 }
106
107 if (vpnEventCallback_ != nullptr) {
108 UnregisterVpnEvent(vpnEventCallback_);
109 }
110 vpnEventCallback_ = new (std::nothrow) VpnSetUpEventCallback();
111 if (vpnEventCallback_ == nullptr) {
112 NETMGR_EXT_LOG_E("vpnEventCallback_ is nullptr");
113 return NETMANAGER_EXT_ERR_INTERNAL;
114 }
115 RegisterVpnEvent(vpnEventCallback_);
116 return NETMANAGER_EXT_SUCCESS;
117 }
118
DestroyVpn(bool isVpnExtCall)119 int32_t NetworkVpnClient::DestroyVpn(bool isVpnExtCall)
120 {
121 vpnInterface_.CloseVpnInterfaceFd();
122 if (vpnEventCallback_ != nullptr) {
123 UnregisterVpnEvent(vpnEventCallback_);
124 vpnEventCallback_ = nullptr;
125 }
126
127 sptr<INetworkVpnService> proxy = GetProxy();
128 if (proxy == nullptr) {
129 NETMGR_EXT_LOG_E("DestroyVpn proxy is nullptr");
130 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
131 }
132 return proxy->DestroyVpn(isVpnExtCall);
133 }
134
135 #ifdef SUPPORT_SYSVPN
AddSysVpnConfig(sptr<SysVpnConfig> & config)136 int32_t NetworkVpnClient::AddSysVpnConfig(sptr<SysVpnConfig> &config)
137 {
138 if (config == nullptr) {
139 NETMGR_EXT_LOG_E("AddSysVpnConfig config is null");
140 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
141 }
142 sptr<INetworkVpnService> proxy = GetProxy();
143 if (proxy == nullptr) {
144 NETMGR_EXT_LOG_E("AddSysVpnConfig proxy is nullptr");
145 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
146 }
147 return proxy->AddSysVpnConfig(config);
148 }
149
DeleteSysVpnConfig(std::string & vpnId)150 int32_t NetworkVpnClient::DeleteSysVpnConfig(std::string &vpnId)
151 {
152 if (vpnId.empty()) {
153 NETMGR_EXT_LOG_E("DeleteSysVpnConfig vpnId is null");
154 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
155 }
156 sptr<INetworkVpnService> proxy = GetProxy();
157 if (proxy == nullptr) {
158 NETMGR_EXT_LOG_E("DeleteSysVpnConfig proxy is nullptr");
159 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
160 }
161 return proxy->DeleteSysVpnConfig(vpnId);
162 }
163
GetSysVpnConfigList(std::vector<SysVpnConfig> & vpnList)164 int32_t NetworkVpnClient::GetSysVpnConfigList(std::vector<SysVpnConfig> &vpnList)
165 {
166 sptr<INetworkVpnService> proxy = GetProxy();
167 if (proxy == nullptr) {
168 NETMGR_EXT_LOG_E("GetSysVpnConfigList proxy is nullptr");
169 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
170 }
171 return proxy->GetSysVpnConfigList(vpnList);
172 }
173
GetSysVpnConfig(sptr<SysVpnConfig> & config,std::string & vpnId)174 int32_t NetworkVpnClient::GetSysVpnConfig(sptr<SysVpnConfig> &config, std::string &vpnId)
175 {
176 if (vpnId.empty()) {
177 NETMGR_EXT_LOG_E("DeleteSysVpnConfig vpnId is null");
178 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
179 }
180 sptr<INetworkVpnService> proxy = GetProxy();
181 if (proxy == nullptr) {
182 NETMGR_EXT_LOG_E("GetSysVpnConfig proxy is nullptr");
183 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
184 }
185 return proxy->GetSysVpnConfig(config, vpnId);
186 }
187
GetConnectedSysVpnConfig(sptr<SysVpnConfig> & config)188 int32_t NetworkVpnClient::GetConnectedSysVpnConfig(sptr<SysVpnConfig> &config)
189 {
190 sptr<INetworkVpnService> proxy = GetProxy();
191 if (proxy == nullptr) {
192 NETMGR_EXT_LOG_E("GetConnectedSysVpnConfig proxy is nullptr");
193 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
194 }
195 return proxy->GetConnectedSysVpnConfig(config);
196 }
197 #endif // SUPPORT_SYSVPN
198
RegisterVpnEvent(sptr<IVpnEventCallback> callback)199 int32_t NetworkVpnClient::RegisterVpnEvent(sptr<IVpnEventCallback> callback)
200 {
201 if (callback == nullptr) {
202 NETMGR_EXT_LOG_E("RegisterVpnEvent callback is null.");
203 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
204 }
205 sptr<INetworkVpnService> proxy = GetProxy();
206 if (proxy == nullptr) {
207 NETMGR_EXT_LOG_E("RegisterVpnEvent proxy is nullptr");
208 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
209 }
210 return proxy->RegisterVpnEvent(callback);
211 }
212
UnregisterVpnEvent(sptr<IVpnEventCallback> callback)213 int32_t NetworkVpnClient::UnregisterVpnEvent(sptr<IVpnEventCallback> callback)
214 {
215 if (callback == nullptr) {
216 NETMGR_EXT_LOG_E("UnregisterVpnEvent callback is null.");
217 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
218 }
219 sptr<INetworkVpnService> proxy = GetProxy();
220 if (proxy == nullptr) {
221 NETMGR_EXT_LOG_E("UnregisterVpnEvent proxy is nullptr");
222 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
223 }
224 return proxy->UnregisterVpnEvent(callback);
225 }
226
CreateVpnConnection(bool isVpnExtCall)227 int32_t NetworkVpnClient::CreateVpnConnection(bool isVpnExtCall)
228 {
229 sptr<INetworkVpnService> proxy = GetProxy();
230 if (proxy == nullptr) {
231 NETMGR_EXT_LOG_E("CreateVpnConnection proxy is nullptr");
232 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
233 }
234 return proxy->CreateVpnConnection(isVpnExtCall);
235 }
236
RegisterBundleName(const std::string & bundleName)237 int32_t NetworkVpnClient::RegisterBundleName(const std::string &bundleName)
238 {
239 NETMGR_EXT_LOG_D("VpnClient::RegisterBundleName is %{public}s", bundleName.c_str());
240 sptr<INetworkVpnService> proxy = GetProxy();
241 if (proxy == nullptr) {
242 NETMGR_EXT_LOG_E("CreateVpnConnection proxy is nullptr");
243 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
244 }
245 return proxy->RegisterBundleName(bundleName);
246 }
247
GetProxy()248 sptr<INetworkVpnService> NetworkVpnClient::GetProxy()
249 {
250 std::lock_guard lock(mutex_);
251 if (networkVpnService_ != nullptr) {
252 return networkVpnService_;
253 }
254 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
255 if (sam == nullptr) {
256 NETMGR_EXT_LOG_E("get SystemAbilityManager failed");
257 return nullptr;
258 }
259 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID);
260 if (remote == nullptr) {
261 NETMGR_EXT_LOG_E("get Remote vpn service failed");
262 return nullptr;
263 }
264 deathRecipient_ = new (std::nothrow) MonitorVpnServiceDead(*this);
265 if (deathRecipient_ == nullptr) {
266 NETMGR_EXT_LOG_E("deathRecipient_ is nullptr");
267 return nullptr;
268 }
269 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
270 NETMGR_EXT_LOG_E("add death recipient failed");
271 return nullptr;
272 }
273 networkVpnService_ = iface_cast<INetworkVpnService>(remote);
274 if (networkVpnService_ == nullptr) {
275 NETMGR_EXT_LOG_E("get Remote service proxy failed");
276 return nullptr;
277 }
278 return networkVpnService_;
279 }
280
RecoverCallback()281 void NetworkVpnClient::RecoverCallback()
282 {
283 uint32_t count = 0;
284 while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
285 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
286 count++;
287 }
288 auto proxy = GetProxy();
289 if (proxy != nullptr && clientVpnConfig_.first != nullptr) {
290 proxy->SetUpVpn(clientVpnConfig_.first, clientVpnConfig_.second);
291 }
292 NETMGR_EXT_LOG_D("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
293 if (proxy != nullptr && vpnEventCallback_ != nullptr) {
294 int32_t ret = proxy->RegisterVpnEvent(vpnEventCallback_);
295 NETMGR_EXT_LOG_D("Register result %{public}d", ret);
296 }
297 }
298
OnRemoteDied(const wptr<IRemoteObject> & remote)299 void NetworkVpnClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
300 {
301 if (remote == nullptr) {
302 NETMGR_EXT_LOG_E("remote object is nullptr");
303 return;
304 }
305 {
306 std::lock_guard lock(mutex_);
307 if (networkVpnService_ == nullptr) {
308 NETMGR_EXT_LOG_E("networkVpnService_ is nullptr");
309 return;
310 }
311 sptr<IRemoteObject> local = networkVpnService_->AsObject();
312 if (local != remote.promote()) {
313 NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
314 return;
315 }
316 local->RemoveDeathRecipient(deathRecipient_);
317 networkVpnService_ = nullptr;
318 }
319
320 if (vpnEventCallback_ != nullptr) {
321 NETMGR_EXT_LOG_D("on remote died recover callback");
322 std::thread t([this]() {
323 RecoverCallback();
324 });
325 std::string threadName = "networkvpnRecoverCallback";
326 pthread_setname_np(t.native_handle(), threadName.c_str());
327 t.detach();
328 }
329 }
330
multiUserSetUpEvent()331 void NetworkVpnClient::multiUserSetUpEvent()
332 {
333 vpnInterface_.CloseVpnInterfaceFd();
334 if (vpnEventCallback_ != nullptr) {
335 UnregisterVpnEvent(vpnEventCallback_);
336 vpnEventCallback_ = nullptr;
337 }
338 }
339
GetSelfAppName(std::string & selfAppName)340 int32_t NetworkVpnClient::GetSelfAppName(std::string &selfAppName)
341 {
342 auto proxy = GetProxy();
343 if (proxy == nullptr) {
344 NETMGR_EXT_LOG_E("GetSelfAppName proxy is nullptr");
345 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
346 }
347 return proxy->GetSelfAppName(selfAppName);
348 }
349 } // namespace NetManagerStandard
350 } // namespace OHOS
351