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 #ifndef IPSEC_VPN_CTL_H 17 #define IPSEC_VPN_CTL_H 18 19 #include <cstdint> 20 21 #include "ipsecvpn_config.h" 22 #include "l2tpvpn_config.h" 23 #include "net_vpn_impl.h" 24 #include "netsys_controller.h" 25 26 #define IPSEC_PIDDIR "/data/service/el1/public/vpn" 27 28 namespace OHOS { 29 namespace NetManagerStandard { 30 namespace { 31 const std::string SWAN_CONFIG_FILE = IPSEC_PIDDIR "/strongswan.conf"; 32 const std::string L2TP_IPSEC_CFG = IPSEC_PIDDIR "/ipsec.conf"; 33 const std::string L2TP_CFG = IPSEC_PIDDIR "/xl2tpd.conf"; 34 const std::string IPSEC_START_TAG = "start"; 35 const std::string SWANCTL_START_TAG = "config"; 36 const std::string IPSEC_CONNECT_TAG = "connect"; 37 } // namespace 38 using namespace NetsysNative; 39 enum IpsecVpnStateCode { 40 STATE_INIT = 0, 41 STATE_STARTED, // ipsec restart compelete 42 STATE_CONFIGED, // swanctl load files compelete or xl2tpd start 43 STATE_CONTROLLED, // control pppd startup 44 STATE_CONNECTED, // ipsec up home or pppd started 45 STATE_DISCONNECTED, // stop 46 }; 47 48 enum IpsecVpnCertType : int32_t { 49 CA_CERT = 0, 50 USER_CERT, 51 SERVER_CERT, 52 SWAN_CTL_CONF, 53 OPTIONS_L2TP_CLIENT_CONF, 54 L2TP_IPSEC_SECRETS_CONF, 55 }; 56 57 class IpsecVpnCtl : public NetVpnImpl { 58 public: 59 IpsecVpnCtl(sptr<VpnConfig> config, const std::string &pkg, int32_t userId, std::vector<int32_t> &activeUserIds); 60 virtual ~IpsecVpnCtl(); 61 62 sptr<IpsecVpnConfig> ipsecVpnConfig_ = nullptr; 63 sptr<L2tpVpnConfig> l2tpVpnConfig_ = nullptr; 64 65 bool IsInternalVpn() override; 66 int32_t SetUp() override; 67 int32_t Destroy() override; 68 int32_t GetConnectedSysVpnConfig(sptr<SysVpnConfig> &sysVpnConfig) override; 69 int32_t NotifyConnectStage(const std::string &stage, const int32_t &result) override; 70 int32_t GetSysVpnCertUri(const int32_t certType, std::string &certUri) override; 71 bool IsSystemVpn() override; 72 73 protected: 74 int32_t state_ = STATE_INIT; 75 virtual int32_t StartSysVpn(); 76 virtual int32_t StopSysVpn(); 77 virtual int32_t InitConfigFile(); 78 void CleanTempFiles(); 79 void DeleteTempFile(const std::string &fileName); 80 }; 81 } // namespace NetManagerStandard 82 } // namespace OHOS 83 #endif // IPSEC_VPN_CTL_H 84