• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef NET_VPN_IMPL_H
17 #define NET_VPN_IMPL_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <set>
22 #include <vector>
23 
24 #include "bundle_mgr_proxy.h"
25 #include "i_vpn_conn_state_cb.h"
26 #include "net_all_capabilities.h"
27 #include "net_conn_client.h"
28 #include "net_manager_ext_constants.h"
29 #include "net_specifier.h"
30 #include "net_supplier_info.h"
31 #include "networkvpn_hisysevent.h"
32 #ifdef SUPPORT_SYSVPN
33 #include "sysvpn_config.h"
34 #include "multi_vpn_helper.h"
35 #endif // SUPPORT_SYSVPN
36 #include "vpn_config.h"
37 
38 namespace OHOS {
39 namespace NetManagerStandard {
40 constexpr const char *TUN_CARD_NAME = "vpn-tun";
41 
42 class NetVpnImpl {
43 public:
44     NetVpnImpl(sptr<VpnConfig> config, const std::string &pkg, int32_t userId, std::vector<int32_t> &activeUserIds);
45     virtual ~NetVpnImpl() = default;
46 
47     virtual bool IsInternalVpn() = 0;
48     virtual int32_t SetUp() = 0;
49     virtual int32_t Destroy() = 0;
50 #ifdef SUPPORT_SYSVPN
51     virtual int32_t GetVpnCertData(const int32_t certType, std::vector<int8_t> &certData);
52     virtual int32_t GetConnectedSysVpnConfig(sptr<SysVpnConfig> &sysVpnConfig);
53     virtual int32_t NotifyConnectStage(const std::string &stage, const int32_t &result);
54     virtual int32_t GetSysVpnCertUri(const int32_t certType, std::string &certUri);
55     virtual bool IsSystemVpn();
56 #endif // SUPPORT_SYSVPN
57     int32_t RegisterConnectStateChangedCb(std::shared_ptr<IVpnConnStateCb> callback);
58     void NotifyConnectState(const VpnConnectState &state);
59 
60 public:
GetVpnConfig()61     inline sptr<VpnConfig> GetVpnConfig() const
62     {
63         return vpnConfig_;
64     }
GetVpnPkg()65     inline std::string GetVpnPkg() const
66     {
67         return pkgName_;
68     }
GetUserId()69     inline int32_t GetUserId() const
70     {
71         return userId_;
72     }
IsVpnConnecting()73     inline bool IsVpnConnecting() const
74     {
75         return isVpnConnecting_;
76     }
GetInterfaceName()77     inline std::string GetInterfaceName() const
78     {
79 #ifdef SUPPORT_SYSVPN
80         if (multiVpnInfo_ != nullptr && !multiVpnInfo_->ifName.empty()) {
81             return multiVpnInfo_->ifName;
82         }
83 #endif
84         return TUN_CARD_NAME;
85     }
86 
87     int32_t ResumeUids();
88 
89 protected:
90     bool UpdateNetLinkInfo();
91 
92 private:
93     bool RegisterNetSupplier(NetConnClient &netConnClientIns);
94     void UnregisterNetSupplier(NetConnClient &netConnClientIns);
95     bool UpdateNetSupplierInfo(NetConnClient &netConnClientIns, bool isAvailable);
96 
97     void DelNetLinkInfo(NetConnClient &netConnClientIns);
98     void AdjustRouteInfo(Route &route);
99     void SetIpv4DefaultRoute(Route &ipv4DefaultRoute);
100     void SetIpv6DefaultRoute(Route &ipv6DefaultRoute);
101 
102     void GenerateUidRangesByAcceptedApps(const std::set<int32_t> &uids, std::vector<int32_t> &beginUids,
103                                          std::vector<int32_t> &endUids);
104     void GenerateUidRangesByRefusedApps(int32_t userId, const std::set<int32_t> &uids, std::vector<int32_t> &beginUids,
105                                         std::vector<int32_t> &endUids);
106     std::set<int32_t> GetAppsUids(int32_t userId, const std::vector<std::string> &applications);
107     int32_t GenerateUidRanges(int32_t userId, std::vector<int32_t> &beginUids, std::vector<int32_t> &endUids);
108     std::string ConvertVpnIpv4Address(uint32_t addressIpv4);
109 
110 #ifdef SUPPORT_SYSVPN
111     void ProcessUpRules(bool isUp);
112 public:
113     sptr<MultiVpnInfo> multiVpnInfo_ = nullptr;
114 #endif // SUPPORT_SYSVPN
115 
116 protected:
117     sptr<VpnConfig> vpnConfig_ = nullptr;
118 
119 private:
120     std::string pkgName_;
121     int32_t userId_ = -1; // the calling app's user
122     std::vector<int32_t> activeUserIds_;
123     bool isVpnConnecting_ = false;
124 
125     int32_t netId_ = -1;
126     uint32_t netSupplierId_ = 0;
127     std::vector<int32_t> beginUids_;
128     std::vector<int32_t> endUids_;
129     std::shared_ptr<IVpnConnStateCb> connChangedCb_;
130     sptr<NetSupplierInfo> netSupplierInfo_ = nullptr;
131 
132     void SetAllUidRanges();
133 };
134 } // namespace NetManagerStandard
135 } // namespace OHOS
136 #endif // NET_VPN_IMPL_H
137