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 #endif // SUPPORT_SYSVPN
35 #include "vpn_config.h"
36 
37 namespace OHOS {
38 namespace NetManagerStandard {
39 constexpr const char *TUN_CARD_NAME = "vpn-tun";
40 
41 class NetVpnImpl {
42 public:
43     NetVpnImpl(sptr<VpnConfig> config, const std::string &pkg, int32_t userId, std::vector<int32_t> &activeUserIds);
44     virtual ~NetVpnImpl() = default;
45 
46     virtual bool IsInternalVpn() = 0;
47     virtual int32_t SetUp() = 0;
48     virtual int32_t Destroy() = 0;
49 #ifdef SUPPORT_SYSVPN
50     virtual int32_t GetConnectedSysVpnConfig(sptr<SysVpnConfig> &sysVpnConfig);
51     virtual int32_t NotifyConnectStage(const std::string &stage, const int32_t &result);
52     virtual int32_t GetSysVpnCertUri(const int32_t certType, std::string &certUri);
53     virtual bool IsSystemVpn();
54 #endif // SUPPORT_SYSVPN
55     int32_t RegisterConnectStateChangedCb(std::shared_ptr<IVpnConnStateCb> callback);
56     void NotifyConnectState(const VpnConnectState &state);
57 
58 public:
GetVpnConfig() const59     inline sptr<VpnConfig> GetVpnConfig() const
60     {
61         return vpnConfig_;
62     }
GetVpnPkg() const63     inline std::string GetVpnPkg() const
64     {
65         return pkgName_;
66     }
GetUserId() const67     inline int32_t GetUserId() const
68     {
69         return userId_;
70     }
IsVpnConnecting() const71     inline bool IsVpnConnecting() const
72     {
73         return isVpnConnecting_;
74     }
GetInterfaceName() const75     inline std::string GetInterfaceName() const
76     {
77         return TUN_CARD_NAME;
78     }
79 
80     int32_t ResumeUids();
81 
82 protected:
83     bool UpdateNetLinkInfo();
84 
85 private:
86     bool RegisterNetSupplier(NetConnClient &netConnClientIns);
87     void UnregisterNetSupplier(NetConnClient &netConnClientIns);
88     bool UpdateNetSupplierInfo(NetConnClient &netConnClientIns, bool isAvailable);
89 
90     void DelNetLinkInfo(NetConnClient &netConnClientIns);
91     void AdjustRouteInfo(Route &route);
92     void SetIpv4DefaultRoute(Route &ipv4DefaultRoute);
93     void SetIpv6DefaultRoute(Route &ipv6DefaultRoute);
94 
95     void GenerateUidRangesByAcceptedApps(const std::set<int32_t> &uids, std::vector<int32_t> &beginUids,
96                                          std::vector<int32_t> &endUids);
97     void GenerateUidRangesByRefusedApps(int32_t userId, const std::set<int32_t> &uids, std::vector<int32_t> &beginUids,
98                                         std::vector<int32_t> &endUids);
99     std::set<int32_t> GetAppsUids(int32_t userId, const std::vector<std::string> &applications);
100     int32_t GenerateUidRanges(int32_t userId, std::vector<int32_t> &beginUids, std::vector<int32_t> &endUids);
101 
102 protected:
103     sptr<VpnConfig> vpnConfig_ = nullptr;
104 
105 private:
106     std::string pkgName_;
107     int32_t userId_ = -1; // the calling app's user
108     std::vector<int32_t> activeUserIds_;
109     bool isVpnConnecting_ = false;
110 
111     int32_t netId_ = -1;
112     uint32_t netSupplierId_ = 0;
113     std::vector<int32_t> beginUids_;
114     std::vector<int32_t> endUids_;
115     std::shared_ptr<IVpnConnStateCb> connChangedCb_;
116     sptr<NetSupplierInfo> netSupplierInfo_ = nullptr;
117 
118     void SetAllUidRanges();
119 };
120 } // namespace NetManagerStandard
121 } // namespace OHOS
122 #endif // NET_VPN_IMPL_H
123