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
SetUpVpn(const sptr<SysVpnConfig> &config)136 int32_t NetworkVpnClient::SetUpVpn(const sptr<SysVpnConfig> &config)
137 {
138 if (config == nullptr) {
139 NETMGR_EXT_LOG_E("SetUpVpn param config is nullptr");
140 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
141 }
142 sptr<INetworkVpnService> proxy = GetProxy();
143 if (proxy == nullptr) {
144 NETMGR_EXT_LOG_E("SetUpVpn proxy is nullptr");
145 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
146 }
147 NETMGR_EXT_LOG_I("SetUpVpn id=%{public}s", config->vpnId_.c_str());
148 return proxy->SetUpVpn(config);
149 }
150
AddSysVpnConfig(sptr<SysVpnConfig> &config)151 int32_t NetworkVpnClient::AddSysVpnConfig(sptr<SysVpnConfig> &config)
152 {
153 if (config == nullptr) {
154 NETMGR_EXT_LOG_E("AddSysVpnConfig config is null");
155 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
156 }
157 sptr<INetworkVpnService> proxy = GetProxy();
158 if (proxy == nullptr) {
159 NETMGR_EXT_LOG_E("AddSysVpnConfig proxy is nullptr");
160 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
161 }
162 return proxy->AddSysVpnConfig(config);
163 }
164
DeleteSysVpnConfig(const std::string &vpnId)165 int32_t NetworkVpnClient::DeleteSysVpnConfig(const std::string &vpnId)
166 {
167 if (vpnId.empty()) {
168 NETMGR_EXT_LOG_E("DeleteSysVpnConfig vpnId is empty");
169 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
170 }
171 sptr<INetworkVpnService> proxy = GetProxy();
172 if (proxy == nullptr) {
173 NETMGR_EXT_LOG_E("DeleteSysVpnConfig proxy is nullptr");
174 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
175 }
176 return proxy->DeleteSysVpnConfig(vpnId);
177 }
178
GetSysVpnConfigList(std::vector<SysVpnConfig> &vpnList)179 int32_t NetworkVpnClient::GetSysVpnConfigList(std::vector<SysVpnConfig> &vpnList)
180 {
181 sptr<INetworkVpnService> proxy = GetProxy();
182 if (proxy == nullptr) {
183 NETMGR_EXT_LOG_E("GetSysVpnConfigList proxy is nullptr");
184 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
185 }
186 return proxy->GetSysVpnConfigList(vpnList);
187 }
188
GetSysVpnConfig(sptr<SysVpnConfig> &config, const std::string &vpnId)189 int32_t NetworkVpnClient::GetSysVpnConfig(sptr<SysVpnConfig> &config, const std::string &vpnId)
190 {
191 if (vpnId.empty()) {
192 NETMGR_EXT_LOG_E("DeleteSysVpnConfig vpnId is empty");
193 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
194 }
195 sptr<INetworkVpnService> proxy = GetProxy();
196 if (proxy == nullptr) {
197 NETMGR_EXT_LOG_E("GetSysVpnConfig proxy is nullptr");
198 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
199 }
200 return proxy->GetSysVpnConfig(config, vpnId);
201 }
202
GetConnectedSysVpnConfig(sptr<SysVpnConfig> &config)203 int32_t NetworkVpnClient::GetConnectedSysVpnConfig(sptr<SysVpnConfig> &config)
204 {
205 sptr<INetworkVpnService> proxy = GetProxy();
206 if (proxy == nullptr) {
207 NETMGR_EXT_LOG_E("GetConnectedSysVpnConfig proxy is nullptr");
208 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
209 }
210 return proxy->GetConnectedSysVpnConfig(config);
211 }
212
NotifyConnectStage(const std::string &stage, const int32_t &result)213 int32_t NetworkVpnClient::NotifyConnectStage(const std::string &stage, const int32_t &result)
214 {
215 sptr<INetworkVpnService> proxy = GetProxy();
216 if (proxy == nullptr) {
217 NETMGR_EXT_LOG_E("NotifyConnectStage proxy is nullptr");
218 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
219 }
220 return proxy->NotifyConnectStage(stage, result);
221 }
222
GetSysVpnCertUri(const int32_t certType, std::string &certUri)223 int32_t NetworkVpnClient::GetSysVpnCertUri(const int32_t certType, std::string &certUri)
224 {
225 sptr<INetworkVpnService> proxy = GetProxy();
226 if (proxy == nullptr) {
227 NETMGR_EXT_LOG_E("GetSysVpnCertUri proxy is nullptr");
228 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
229 }
230 return proxy->GetSysVpnCertUri(certType, certUri);
231 }
232 #endif // SUPPORT_SYSVPN
233
RegisterVpnEvent(sptr<IVpnEventCallback> callback)234 int32_t NetworkVpnClient::RegisterVpnEvent(sptr<IVpnEventCallback> callback)
235 {
236 if (callback == nullptr) {
237 NETMGR_EXT_LOG_E("RegisterVpnEvent callback is null.");
238 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
239 }
240 sptr<INetworkVpnService> proxy = GetProxy();
241 if (proxy == nullptr) {
242 NETMGR_EXT_LOG_E("RegisterVpnEvent proxy is nullptr");
243 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
244 }
245 return proxy->RegisterVpnEvent(callback);
246 }
247
UnregisterVpnEvent(sptr<IVpnEventCallback> callback)248 int32_t NetworkVpnClient::UnregisterVpnEvent(sptr<IVpnEventCallback> callback)
249 {
250 if (callback == nullptr) {
251 NETMGR_EXT_LOG_E("UnregisterVpnEvent callback is null.");
252 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
253 }
254 sptr<INetworkVpnService> proxy = GetProxy();
255 if (proxy == nullptr) {
256 NETMGR_EXT_LOG_E("UnregisterVpnEvent proxy is nullptr");
257 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
258 }
259 return proxy->UnregisterVpnEvent(callback);
260 }
261
CreateVpnConnection(bool isVpnExtCall)262 int32_t NetworkVpnClient::CreateVpnConnection(bool isVpnExtCall)
263 {
264 sptr<INetworkVpnService> proxy = GetProxy();
265 if (proxy == nullptr) {
266 NETMGR_EXT_LOG_E("CreateVpnConnection proxy is nullptr");
267 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
268 }
269 return proxy->CreateVpnConnection(isVpnExtCall);
270 }
271
RegisterBundleName(const std::string &bundleName)272 int32_t NetworkVpnClient::RegisterBundleName(const std::string &bundleName)
273 {
274 NETMGR_EXT_LOG_D("VpnClient::RegisterBundleName is %{public}s", bundleName.c_str());
275 sptr<INetworkVpnService> proxy = GetProxy();
276 if (proxy == nullptr) {
277 NETMGR_EXT_LOG_E("CreateVpnConnection proxy is nullptr");
278 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
279 }
280 return proxy->RegisterBundleName(bundleName);
281 }
282
GetProxy()283 sptr<INetworkVpnService> NetworkVpnClient::GetProxy()
284 {
285 std::lock_guard lock(mutex_);
286 if (networkVpnService_ != nullptr) {
287 return networkVpnService_;
288 }
289 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
290 if (sam == nullptr) {
291 NETMGR_EXT_LOG_E("get SystemAbilityManager failed");
292 return nullptr;
293 }
294 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID);
295 if (remote == nullptr) {
296 NETMGR_EXT_LOG_E("get Remote vpn service failed");
297 return nullptr;
298 }
299 deathRecipient_ = new (std::nothrow) MonitorVpnServiceDead(*this);
300 if (deathRecipient_ == nullptr) {
301 NETMGR_EXT_LOG_E("deathRecipient_ is nullptr");
302 return nullptr;
303 }
304 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
305 NETMGR_EXT_LOG_E("add death recipient failed");
306 return nullptr;
307 }
308 networkVpnService_ = iface_cast<INetworkVpnService>(remote);
309 if (networkVpnService_ == nullptr) {
310 NETMGR_EXT_LOG_E("get Remote service proxy failed");
311 return nullptr;
312 }
313 return networkVpnService_;
314 }
315
RecoverCallback()316 void NetworkVpnClient::RecoverCallback()
317 {
318 uint32_t count = 0;
319 while (GetProxy() == nullptr && count < MAX_GET_SERVICE_COUNT) {
320 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_FOR_SERVICE_TIME_MS));
321 count++;
322 }
323 auto proxy = GetProxy();
324 if (proxy != nullptr && clientVpnConfig_.first != nullptr) {
325 proxy->SetUpVpn(clientVpnConfig_.first, clientVpnConfig_.second);
326 }
327 NETMGR_EXT_LOG_D("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count);
328 if (proxy != nullptr && vpnEventCallback_ != nullptr) {
329 int32_t ret = proxy->RegisterVpnEvent(vpnEventCallback_);
330 NETMGR_EXT_LOG_D("Register result %{public}d", ret);
331 }
332 }
333
OnRemoteDied(const wptr<IRemoteObject> &remote)334 void NetworkVpnClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
335 {
336 if (remote == nullptr) {
337 NETMGR_EXT_LOG_E("remote object is nullptr");
338 return;
339 }
340 {
341 std::lock_guard lock(mutex_);
342 if (networkVpnService_ == nullptr) {
343 NETMGR_EXT_LOG_E("networkVpnService_ is nullptr");
344 return;
345 }
346 sptr<IRemoteObject> local = networkVpnService_->AsObject();
347 if (local != remote.promote()) {
348 NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
349 return;
350 }
351 local->RemoveDeathRecipient(deathRecipient_);
352 networkVpnService_ = nullptr;
353 }
354
355 if (vpnEventCallback_ != nullptr) {
356 NETMGR_EXT_LOG_D("on remote died recover callback");
357 std::thread t([this]() {
358 RecoverCallback();
359 });
360 std::string threadName = "networkvpnRecoverCallback";
361 pthread_setname_np(t.native_handle(), threadName.c_str());
362 t.detach();
363 }
364 }
365
multiUserSetUpEvent()366 void NetworkVpnClient::multiUserSetUpEvent()
367 {
368 vpnInterface_.CloseVpnInterfaceFd();
369 if (vpnEventCallback_ != nullptr) {
370 UnregisterVpnEvent(vpnEventCallback_);
371 vpnEventCallback_ = nullptr;
372 }
373 }
374
GetSelfAppName(std::string &selfAppName)375 int32_t NetworkVpnClient::GetSelfAppName(std::string &selfAppName)
376 {
377 auto proxy = GetProxy();
378 if (proxy == nullptr) {
379 NETMGR_EXT_LOG_E("GetSelfAppName proxy is nullptr");
380 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
381 }
382 return proxy->GetSelfAppName(selfAppName);
383 }
384 } // namespace NetManagerStandard
385 } // namespace OHOS
386