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 "fwmark_client.h"
19 #include "iservice_registry.h"
20 #include "net_manager_constants.h"
21 #include "netmgr_ext_log_wrapper.h"
22 #include "system_ability_definition.h"
23
24 namespace OHOS {
25 namespace NetManagerStandard {
26
OnVpnMultiUserSetUp()27 void VpnSetUpEventCallback::OnVpnMultiUserSetUp()
28 {
29 NETMGR_EXT_LOG_I("vpn multiple user setup event.");
30 NetworkVpnClient::GetInstance().multiUserSetUpEvent();
31 }
32
GetInstance()33 NetworkVpnClient &NetworkVpnClient::GetInstance()
34 {
35 static NetworkVpnClient instance;
36 return instance;
37 }
38
Prepare(bool & isExistVpn,bool & isRun,std::string & pkg)39 int32_t NetworkVpnClient::Prepare(bool &isExistVpn, bool &isRun, std::string &pkg)
40 {
41 sptr<INetworkVpnService> proxy = GetProxy();
42 if (proxy == nullptr) {
43 NETMGR_EXT_LOG_E("Prepare proxy is nullptr");
44 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
45 }
46 return proxy->Prepare(isExistVpn, isRun, pkg);
47 }
48
Protect(int32_t socketFd)49 int32_t NetworkVpnClient::Protect(int32_t socketFd)
50 {
51 if (socketFd <= 0) {
52 NETMGR_EXT_LOG_E("Invalid socket file discriptor");
53 return NETWORKVPN_ERROR_INVALID_FD;
54 }
55
56 sptr<INetworkVpnService> proxy = GetProxy();
57 if (proxy == nullptr) {
58 NETMGR_EXT_LOG_E("Protect proxy is nullptr");
59 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
60 }
61 int32_t result = proxy->Protect();
62 if (result != NETMANAGER_EXT_SUCCESS) {
63 return result;
64 }
65 nmd::FwmarkClient fwmarkClient;
66 return fwmarkClient.ProtectFromVpn(socketFd);
67 }
68
SetUpVpn(sptr<VpnConfig> config,int32_t & tunFd)69 int32_t NetworkVpnClient::SetUpVpn(sptr<VpnConfig> config, int32_t &tunFd)
70 {
71 if (config == nullptr) {
72 NETMGR_EXT_LOG_E("SetUpVpn param config is nullptr");
73 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
74 }
75
76 sptr<INetworkVpnService> proxy = GetProxy();
77 if (proxy == nullptr) {
78 NETMGR_EXT_LOG_E("SetUpVpn proxy is nullptr");
79 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
80 }
81 int32_t result = proxy->SetUpVpn(config);
82 if (result != NETMANAGER_EXT_SUCCESS) {
83 tunFd = 0;
84 return result;
85 }
86
87 tunFd = vpnInterface_.GetVpnInterfaceFd();
88 if (tunFd <= 0) {
89 return NETMANAGER_EXT_ERR_INTERNAL;
90 }
91
92 if (vpnEventCallback_ != nullptr) {
93 UnregisterVpnEvent(vpnEventCallback_);
94 }
95 vpnEventCallback_ = new (std::nothrow) VpnSetUpEventCallback();
96 if (vpnEventCallback_ == nullptr) {
97 NETMGR_EXT_LOG_E("vpnEventCallback_ is nullptr");
98 return NETMANAGER_EXT_ERR_INTERNAL;
99 }
100 RegisterVpnEvent(vpnEventCallback_);
101 return NETMANAGER_EXT_SUCCESS;
102 }
103
DestroyVpn()104 int32_t NetworkVpnClient::DestroyVpn()
105 {
106 vpnInterface_.CloseVpnInterfaceFd();
107 if (vpnEventCallback_ != nullptr) {
108 UnregisterVpnEvent(vpnEventCallback_);
109 vpnEventCallback_ = nullptr;
110 }
111
112 sptr<INetworkVpnService> proxy = GetProxy();
113 if (proxy == nullptr) {
114 NETMGR_EXT_LOG_E("DestroyVpn proxy is nullptr");
115 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
116 }
117 return proxy->DestroyVpn();
118 }
119
RegisterVpnEvent(sptr<IVpnEventCallback> callback)120 int32_t NetworkVpnClient::RegisterVpnEvent(sptr<IVpnEventCallback> callback)
121 {
122 if (callback == nullptr) {
123 NETMGR_EXT_LOG_E("RegisterVpnEvent callback is null.");
124 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
125 }
126 sptr<INetworkVpnService> proxy = GetProxy();
127 if (proxy == nullptr) {
128 NETMGR_EXT_LOG_E("RegisterVpnEvent proxy is nullptr");
129 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
130 }
131 return proxy->RegisterVpnEvent(callback);
132 }
133
UnregisterVpnEvent(sptr<IVpnEventCallback> callback)134 int32_t NetworkVpnClient::UnregisterVpnEvent(sptr<IVpnEventCallback> callback)
135 {
136 if (callback == nullptr) {
137 NETMGR_EXT_LOG_E("UnregisterVpnEvent callback is null.");
138 return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
139 }
140 sptr<INetworkVpnService> proxy = GetProxy();
141 if (proxy == nullptr) {
142 NETMGR_EXT_LOG_E("UnregisterVpnEvent proxy is nullptr");
143 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
144 }
145 return proxy->UnregisterVpnEvent(callback);
146 }
147
CreateVpnConnection()148 int32_t NetworkVpnClient::CreateVpnConnection()
149 {
150 sptr<INetworkVpnService> proxy = GetProxy();
151 if (proxy == nullptr) {
152 NETMGR_EXT_LOG_E("CreateVpnConnection proxy is nullptr");
153 return NETMANAGER_EXT_ERR_GET_PROXY_FAIL;
154 }
155 return proxy->CreateVpnConnection();
156 }
157
GetProxy()158 sptr<INetworkVpnService> NetworkVpnClient::GetProxy()
159 {
160 std::lock_guard lock(mutex_);
161 if (networkVpnService_ != nullptr) {
162 return networkVpnService_;
163 }
164 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
165 if (sam == nullptr) {
166 NETMGR_EXT_LOG_E("get SystemAbilityManager failed");
167 return nullptr;
168 }
169 sptr<IRemoteObject> remote = sam->CheckSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID);
170 if (remote == nullptr) {
171 NETMGR_EXT_LOG_E("get Remote vpn service failed");
172 return nullptr;
173 }
174 deathRecipient_ = new (std::nothrow) MonitorVpnServiceDead(*this);
175 if (deathRecipient_ == nullptr) {
176 NETMGR_EXT_LOG_E("deathRecipient_ is nullptr");
177 return nullptr;
178 }
179 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(deathRecipient_))) {
180 NETMGR_EXT_LOG_E("add death recipient failed");
181 return nullptr;
182 }
183 networkVpnService_ = iface_cast<INetworkVpnService>(remote);
184 if (networkVpnService_ == nullptr) {
185 NETMGR_EXT_LOG_E("get Remote service proxy failed");
186 return nullptr;
187 }
188 return networkVpnService_;
189 }
190
OnRemoteDied(const wptr<IRemoteObject> & remote)191 void NetworkVpnClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
192 {
193 if (remote == nullptr) {
194 NETMGR_EXT_LOG_E("remote object is nullptr");
195 return;
196 }
197 std::lock_guard lock(mutex_);
198 if (networkVpnService_ == nullptr) {
199 NETMGR_EXT_LOG_E("networkVpnService_ is nullptr");
200 return;
201 }
202 sptr<IRemoteObject> local = networkVpnService_->AsObject();
203 if (local != remote.promote()) {
204 NETMGR_EXT_LOG_E("proxy and stub is not same remote object");
205 return;
206 }
207 local->RemoveDeathRecipient(deathRecipient_);
208 networkVpnService_ = nullptr;
209 }
210
multiUserSetUpEvent()211 void NetworkVpnClient::multiUserSetUpEvent()
212 {
213 vpnInterface_.CloseVpnInterfaceFd();
214 if (vpnEventCallback_ != nullptr) {
215 UnregisterVpnEvent(vpnEventCallback_);
216 vpnEventCallback_ = nullptr;
217 }
218 }
219 } // namespace NetManagerStandard
220 } // namespace OHOS
221