• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "l2tp_vpn_ctl.h"
17 
18 #include <string>
19 
20 #include "base64_utils.h"
21 #include "netmgr_ext_log_wrapper.h"
22 #include "netmanager_base_common_utils.h"
23 #include "net_manager_ext_constants.h"
24 
25 namespace OHOS {
26 namespace NetManagerStandard {
L2tpVpnCtl(sptr<VpnConfig> config,const std::string & pkg,int32_t userId,std::vector<int32_t> & activeUserIds)27 L2tpVpnCtl::L2tpVpnCtl(sptr<VpnConfig> config, const std::string &pkg, int32_t userId,
28     std::vector<int32_t> &activeUserIds)
29     : IpsecVpnCtl(config, pkg, userId, activeUserIds)
30 {}
31 
StopSysVpn()32 int32_t L2tpVpnCtl::StopSysVpn()
33 {
34     NETMGR_EXT_LOG_I("stop l2tp vpn");
35     state_ = IpsecVpnStateCode::STATE_DISCONNECTED;
36     NetsysController::GetInstance().ProcessVpnStage(SysVpnStageCode::VPN_STAGE_DOWN_HOME);
37     NetsysController::GetInstance().ProcessVpnStage(SysVpnStageCode::VPN_STAGE_STOP);
38     NotifyConnectState(VpnConnectState::VPN_DISCONNECTED);
39     return NETMANAGER_EXT_SUCCESS;
40 }
41 
StartSysVpn()42 int32_t L2tpVpnCtl::StartSysVpn()
43 {
44     NETMGR_EXT_LOG_I("start l2tp vpn");
45     state_ = IpsecVpnStateCode::STATE_INIT;
46     InitConfigFile();
47     NetsysController::GetInstance().ProcessVpnStage(SysVpnStageCode::VPN_STAGE_RESTART);
48     return NETMANAGER_EXT_SUCCESS;
49 }
50 
InitConfigFile()51 int32_t L2tpVpnCtl::InitConfigFile()
52 {
53     CleanTempFiles();
54     if (l2tpVpnConfig_ == nullptr) {
55         NETMGR_EXT_LOG_E("InitConfigFile failed, l2tpVpnConfig_ is null");
56         return NETMANAGER_EXT_ERR_INTERNAL;
57     }
58     if (!l2tpVpnConfig_->strongswanConf_.empty()) {
59         std::string strongswanCfg = Base64::Decode(l2tpVpnConfig_->strongswanConf_);
60         if (!strongswanCfg.empty()) {
61             CommonUtils::WriteFile(SWAN_CONFIG_FILE, strongswanCfg);
62         }
63     }
64     if (!l2tpVpnConfig_->xl2tpdConf_.empty()) {
65         std::string xl2tpdConf = Base64::Decode(l2tpVpnConfig_->xl2tpdConf_);
66         if (!xl2tpdConf.empty()) {
67             CommonUtils::WriteFile(L2TP_CFG, xl2tpdConf);
68         }
69     }
70     if (!l2tpVpnConfig_->ipsecConf_.empty()) {
71         std::string ipsecConf = Base64::Decode(l2tpVpnConfig_->ipsecConf_);
72         if (!ipsecConf.empty()) {
73             CommonUtils::WriteFile(L2TP_IPSEC_CFG, ipsecConf);
74         }
75     }
76     return NETMANAGER_EXT_SUCCESS;
77 }
78 
NotifyConnectStage(const std::string & stage,const int32_t & result)79 int32_t L2tpVpnCtl::NotifyConnectStage(const std::string &stage, const int32_t &result)
80 {
81     if (stage.empty()) {
82         NETMGR_EXT_LOG_E("stage is empty");
83         return NETMANAGER_EXT_ERR_PARAMETER_ERROR;
84     }
85     if (result != NETMANAGER_EXT_SUCCESS) {
86         NETMGR_EXT_LOG_E("l2tpVpn stage: %{public}s failed, result: %{public}d", stage.c_str(), result);
87         return NETMANAGER_EXT_ERR_INTERNAL;
88     }
89     switch (state_) {
90         case IpsecVpnStateCode::STATE_INIT:
91             if (stage.compare(IPSEC_START_TAG) == 0) {
92                 // 1. start l2tp
93                 NETMGR_EXT_LOG_I("l2tp vpn setup step 1: start l2tp");
94                 state_ = IpsecVpnStateCode::STATE_STARTED;
95                 NetsysController::GetInstance().ProcessVpnStage(SysVpnStageCode::VPN_STAGE_L2TP_LOAD);
96             }
97             break;
98         case IpsecVpnStateCode::STATE_STARTED:
99             if (stage.compare(L2TP_IPSEC_CONFIGURED_TAG) == 0) {
100                 // 2. start connect
101                 NETMGR_EXT_LOG_I("l2tp vpn setup step 2: start connect");
102                 state_ = IpsecVpnStateCode::STATE_CONFIGED;
103                 NetsysController::GetInstance().ProcessVpnStage(SysVpnStageCode::VPN_STAGE_UP_HOME);
104             }
105             break;
106         case IpsecVpnStateCode::STATE_CONFIGED:
107             if (stage.compare(IPSEC_CONNECT_TAG) == 0) {
108                 // 3. set stage IPSEC_L2TP_CTL
109                 NETMGR_EXT_LOG_I("l2tp vpn setup step 3: set stage IPSEC_L2TP_CTL");
110                 state_ = IpsecVpnStateCode::STATE_CONTROLLED;
111                 NetsysController::GetInstance().ProcessVpnStage(SysVpnStageCode::VPN_STAGE_L2TP_CTL);
112             }
113             break;
114         case IpsecVpnStateCode::STATE_CONTROLLED:
115             if (stage.compare(L2TP_IPSEC_CONNECTED_TAG) == 0) {
116                 // 4. is connected
117                 NETMGR_EXT_LOG_I("l2tp vpn setup step 4: is connected");
118                 state_ = IpsecVpnStateCode::STATE_CONNECTED;
119                 NotifyConnectState(VpnConnectState::VPN_CONNECTED);
120             }
121             break;
122         default:
123             NETMGR_EXT_LOG_E("invalid state: %{public}d", state_);
124             return NETMANAGER_EXT_ERR_INTERNAL;
125     }
126     return NETMANAGER_EXT_SUCCESS;
127 }
128 
GetSysVpnCertUri(const int32_t certType,std::string & certUri)129 int32_t L2tpVpnCtl::GetSysVpnCertUri(const int32_t certType, std::string &certUri)
130 {
131     if (l2tpVpnConfig_ == nullptr) {
132         NETMGR_EXT_LOG_E("GetSysVpnCertUri l2tpVpnConfig_ is null");
133         return NETMANAGER_EXT_ERR_INTERNAL;
134     }
135     switch (certType) {
136         case IpsecVpnCertType::CA_CERT:
137             certUri = l2tpVpnConfig_->ipsecCaCertConf_;
138             break;
139         case IpsecVpnCertType::USER_CERT:
140             certUri = l2tpVpnConfig_->ipsecPublicUserCertConf_;
141             break;
142         case IpsecVpnCertType::SERVER_CERT:
143             certUri = l2tpVpnConfig_->ipsecPublicServerCertConf_;
144             break;
145         case IpsecVpnCertType::OPTIONS_L2TP_CLIENT_CONF:
146             certUri = Base64::Decode(l2tpVpnConfig_->optionsL2tpdClient_);
147             break;
148         case IpsecVpnCertType::L2TP_IPSEC_SECRETS_CONF:
149             certUri = Base64::Decode(l2tpVpnConfig_->ipsecSecrets_);
150             break;
151         default:
152             NETMGR_EXT_LOG_E("invalid certType: %{public}d", certType);
153             break;
154     }
155     return NETMANAGER_EXT_SUCCESS;
156 }
157 
GetConnectedSysVpnConfig(sptr<SysVpnConfig> & sysVpnConfig)158 int32_t L2tpVpnCtl::GetConnectedSysVpnConfig(sptr<SysVpnConfig> &sysVpnConfig)
159 {
160     if (state_ == IpsecVpnStateCode::STATE_CONNECTED && l2tpVpnConfig_ != nullptr) {
161         NETMGR_EXT_LOG_I("GetConnectedSysVpnConfig success");
162         sysVpnConfig = l2tpVpnConfig_;
163     }
164     return NETMANAGER_EXT_SUCCESS;
165 }
166 } // namespace NetManagerStandard
167 } // namespace OHOS
168