1b1b8bc3fSopenharmony_ci/*
2b1b8bc3fSopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3b1b8bc3fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1b8bc3fSopenharmony_ci * you may not use this file except in compliance with the License.
5b1b8bc3fSopenharmony_ci * You may obtain a copy of the License at
6b1b8bc3fSopenharmony_ci *
7b1b8bc3fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b1b8bc3fSopenharmony_ci *
9b1b8bc3fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1b8bc3fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1b8bc3fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1b8bc3fSopenharmony_ci * See the License for the specific language governing permissions and
13b1b8bc3fSopenharmony_ci * limitations under the License.
14b1b8bc3fSopenharmony_ci */
15b1b8bc3fSopenharmony_ci
16b1b8bc3fSopenharmony_ci#ifndef INCLUDE_CONN_MANAGER_H
17b1b8bc3fSopenharmony_ci#define INCLUDE_CONN_MANAGER_H
18b1b8bc3fSopenharmony_ci
19b1b8bc3fSopenharmony_ci#include <map>
20b1b8bc3fSopenharmony_ci#include <memory>
21b1b8bc3fSopenharmony_ci#include <mutex>
22b1b8bc3fSopenharmony_ci#include <set>
23b1b8bc3fSopenharmony_ci#include <sys/types.h>
24b1b8bc3fSopenharmony_ci#include <vector>
25b1b8bc3fSopenharmony_ci#include <thread>
26b1b8bc3fSopenharmony_ci
27b1b8bc3fSopenharmony_ci#include "netsys_network.h"
28b1b8bc3fSopenharmony_ci#include "network_permission.h"
29b1b8bc3fSopenharmony_ci#include "route_manager.h"
30b1b8bc3fSopenharmony_ci#include "safe_map.h"
31b1b8bc3fSopenharmony_ci#include "netsys_access_policy.h"
32b1b8bc3fSopenharmony_ci#include "net_all_capabilities.h"
33b1b8bc3fSopenharmony_ci
34b1b8bc3fSopenharmony_cinamespace OHOS {
35b1b8bc3fSopenharmony_cinamespace nmd {
36b1b8bc3fSopenharmony_ciclass ConnManager {
37b1b8bc3fSopenharmony_cipublic:
38b1b8bc3fSopenharmony_ci    enum RouteAction {
39b1b8bc3fSopenharmony_ci        ROUTE_ADD,
40b1b8bc3fSopenharmony_ci        ROUTE_REMOVE,
41b1b8bc3fSopenharmony_ci        ROUTE_UPDATE,
42b1b8bc3fSopenharmony_ci    };
43b1b8bc3fSopenharmony_ci
44b1b8bc3fSopenharmony_ci    ConnManager();
45b1b8bc3fSopenharmony_ci    ~ConnManager();
46b1b8bc3fSopenharmony_ci
47b1b8bc3fSopenharmony_ci    /**
48b1b8bc3fSopenharmony_ci     * Disallow or allow a app to create AF_INET or AF_INET6 socket
49b1b8bc3fSopenharmony_ci     *
50b1b8bc3fSopenharmony_ci     * @param uid App's uid which need to be disallowed ot allowed to create AF_INET or AF_INET6 socket
51b1b8bc3fSopenharmony_ci     * @param allow 0 means disallow, 1 means allow
52b1b8bc3fSopenharmony_ci     * @return return 0 if OK, return error number if not OK
53b1b8bc3fSopenharmony_ci     */
54b1b8bc3fSopenharmony_ci    int32_t SetInternetPermission(uint32_t uid, uint8_t allow, uint8_t isBroker);
55b1b8bc3fSopenharmony_ci
56b1b8bc3fSopenharmony_ci    /**
57b1b8bc3fSopenharmony_ci     * Creates a physical network
58b1b8bc3fSopenharmony_ci     *
59b1b8bc3fSopenharmony_ci     * @param netId The network Id to create
60b1b8bc3fSopenharmony_ci     * @param permission The permission necessary to use the network. Must be one of
61b1b8bc3fSopenharmony_ci     *        PERMISSION_NONE/PERMISSION_NETWORK/PERMISSION_SYSTEM
62b1b8bc3fSopenharmony_ci     *
63b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully create the physical network, otherwise it will fail
64b1b8bc3fSopenharmony_ci     */
65b1b8bc3fSopenharmony_ci    int32_t CreatePhysicalNetwork(uint16_t netId, NetworkPermission permission);
66b1b8bc3fSopenharmony_ci
67b1b8bc3fSopenharmony_ci    /**
68b1b8bc3fSopenharmony_ci     * Creates a virtual network
69b1b8bc3fSopenharmony_ci     *
70b1b8bc3fSopenharmony_ci     * @param netId The network Id to create
71b1b8bc3fSopenharmony_ci     * @param hasDns true if this network set dns
72b1b8bc3fSopenharmony_ci     * @param secure true if set bypass=false
73b1b8bc3fSopenharmony_ci     *
74b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully create the physical network, otherwise it will fail
75b1b8bc3fSopenharmony_ci     */
76b1b8bc3fSopenharmony_ci    int32_t CreateVirtualNetwork(uint16_t netId, bool hasDns);
77b1b8bc3fSopenharmony_ci
78b1b8bc3fSopenharmony_ci    /**
79b1b8bc3fSopenharmony_ci     * Destroy a network. Any interfaces added to the network are removed, and the network ceases
80b1b8bc3fSopenharmony_ci     *        to be the default network
81b1b8bc3fSopenharmony_ci     *
82b1b8bc3fSopenharmony_ci     * @param netId The network to destroy
83b1b8bc3fSopenharmony_ci     *
84b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully destroy the network, otherwise it will fail
85b1b8bc3fSopenharmony_ci     */
86b1b8bc3fSopenharmony_ci    int32_t DestroyNetwork(int32_t netId);
87b1b8bc3fSopenharmony_ci
88b1b8bc3fSopenharmony_ci    /**
89b1b8bc3fSopenharmony_ci     * Set network as default network
90b1b8bc3fSopenharmony_ci     *
91b1b8bc3fSopenharmony_ci     * @param netId The network to set as the default
92b1b8bc3fSopenharmony_ci     *
93b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully Set default network, otherwise it will fail
94b1b8bc3fSopenharmony_ci     */
95b1b8bc3fSopenharmony_ci    int32_t SetDefaultNetwork(int32_t netId);
96b1b8bc3fSopenharmony_ci
97b1b8bc3fSopenharmony_ci    /**
98b1b8bc3fSopenharmony_ci     * Clear default network
99b1b8bc3fSopenharmony_ci     *
100b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully clear default network, otherwise it will fail
101b1b8bc3fSopenharmony_ci     */
102b1b8bc3fSopenharmony_ci    int32_t ClearDefaultNetwork();
103b1b8bc3fSopenharmony_ci
104b1b8bc3fSopenharmony_ci    /**
105b1b8bc3fSopenharmony_ci     * Get default network
106b1b8bc3fSopenharmony_ci     *
107b1b8bc3fSopenharmony_ci     * @return NetId of default network
108b1b8bc3fSopenharmony_ci     */
109b1b8bc3fSopenharmony_ci    int32_t GetDefaultNetwork() const;
110b1b8bc3fSopenharmony_ci
111b1b8bc3fSopenharmony_ci    /**
112b1b8bc3fSopenharmony_ci     * Add an interface to a network. The interface must not be assigned to any network, including
113b1b8bc3fSopenharmony_ci     *        the specified network
114b1b8bc3fSopenharmony_ci     *
115b1b8bc3fSopenharmony_ci     * @param netId The network to add the interface
116b1b8bc3fSopenharmony_ci     * @param interafceName The name of the interface to add
117b1b8bc3fSopenharmony_ci     *
118b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully add an interface to a network, otherwise it will fail
119b1b8bc3fSopenharmony_ci     */
120b1b8bc3fSopenharmony_ci    int32_t AddInterfaceToNetwork(int32_t netId, std::string &interafceName,
121b1b8bc3fSopenharmony_ci                                  NetManagerStandard::NetBearType netBearerType = NetManagerStandard::BEARER_DEFAULT);
122b1b8bc3fSopenharmony_ci
123b1b8bc3fSopenharmony_ci    /**
124b1b8bc3fSopenharmony_ci     * Remove an interface to a network. The interface must be assigned to the specified network
125b1b8bc3fSopenharmony_ci     *
126b1b8bc3fSopenharmony_ci     * @param netId The network to add the interface
127b1b8bc3fSopenharmony_ci     * @param interafceName The name of the interface to remove
128b1b8bc3fSopenharmony_ci     *
129b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully remove an interface to a network, otherwise it will fail
130b1b8bc3fSopenharmony_ci     */
131b1b8bc3fSopenharmony_ci    int32_t RemoveInterfaceFromNetwork(int32_t netId, std::string &interafceName);
132b1b8bc3fSopenharmony_ci
133b1b8bc3fSopenharmony_ci    /**
134b1b8bc3fSopenharmony_ci     * Reinit route when netmanager restart
135b1b8bc3fSopenharmony_ci     *
136b1b8bc3fSopenharmony_ci     * @param
137b1b8bc3fSopenharmony_ci     *
138b1b8bc3fSopenharmony_ci     * @return Returns 0, reinit route successfully, otherwise it will fail
139b1b8bc3fSopenharmony_ci     */
140b1b8bc3fSopenharmony_ci    int32_t ReinitRoute();
141b1b8bc3fSopenharmony_ci
142b1b8bc3fSopenharmony_ci    /**
143b1b8bc3fSopenharmony_ci     * Add a route for specific network
144b1b8bc3fSopenharmony_ci     *
145b1b8bc3fSopenharmony_ci     * @param netId The network to add the route
146b1b8bc3fSopenharmony_ci     * @param interfaceName The name of interface of the route
147b1b8bc3fSopenharmony_ci     *                      This interface should be assigned to the netID
148b1b8bc3fSopenharmony_ci     * @param destination The destination of the route
149b1b8bc3fSopenharmony_ci     * @param nextHop The route's next hop address
150b1b8bc3fSopenharmony_ci     *
151b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully add a route for specific network, otherwise it will fail
152b1b8bc3fSopenharmony_ci     */
153b1b8bc3fSopenharmony_ci    int32_t AddRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop,
154b1b8bc3fSopenharmony_ci                     bool& routeRepeat);
155b1b8bc3fSopenharmony_ci
156b1b8bc3fSopenharmony_ci    /**
157b1b8bc3fSopenharmony_ci     * Remove a route for specific network
158b1b8bc3fSopenharmony_ci     *
159b1b8bc3fSopenharmony_ci     * @param netId The network to remove the route
160b1b8bc3fSopenharmony_ci     * @param interfaceName The name of interface of the route
161b1b8bc3fSopenharmony_ci     *                      This interface should be assigned to the netID
162b1b8bc3fSopenharmony_ci     * @param destination The destination of the route
163b1b8bc3fSopenharmony_ci     * @param nextHop The route's next hop address
164b1b8bc3fSopenharmony_ci     *
165b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully remove a route for specific network, otherwise it will fail
166b1b8bc3fSopenharmony_ci     */
167b1b8bc3fSopenharmony_ci    int32_t RemoveRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop);
168b1b8bc3fSopenharmony_ci
169b1b8bc3fSopenharmony_ci    /**
170b1b8bc3fSopenharmony_ci     * Update a route for specific network
171b1b8bc3fSopenharmony_ci     *
172b1b8bc3fSopenharmony_ci     * @param netId The network to update the route
173b1b8bc3fSopenharmony_ci     * @param interfaceName The name of interface of the route
174b1b8bc3fSopenharmony_ci     *                      This interface should be assigned to the netID
175b1b8bc3fSopenharmony_ci     * @param destination The destination of the route
176b1b8bc3fSopenharmony_ci     * @param nextHop The route's next hop address
177b1b8bc3fSopenharmony_ci     *
178b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully update a route for specific network, otherwise it will fail
179b1b8bc3fSopenharmony_ci     */
180b1b8bc3fSopenharmony_ci    int32_t UpdateRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop);
181b1b8bc3fSopenharmony_ci
182b1b8bc3fSopenharmony_ci    /**
183b1b8bc3fSopenharmony_ci     * Get the mark for the given network id
184b1b8bc3fSopenharmony_ci     *
185b1b8bc3fSopenharmony_ci     * @param netId The network to get the mark
186b1b8bc3fSopenharmony_ci     *
187b1b8bc3fSopenharmony_ci     * @return A Mark of the given network id.
188b1b8bc3fSopenharmony_ci     */
189b1b8bc3fSopenharmony_ci    int32_t GetFwmarkForNetwork(int32_t netId);
190b1b8bc3fSopenharmony_ci
191b1b8bc3fSopenharmony_ci    /**
192b1b8bc3fSopenharmony_ci     * Set the permission required to access a specific network
193b1b8bc3fSopenharmony_ci     *
194b1b8bc3fSopenharmony_ci     * @param netId The network to set
195b1b8bc3fSopenharmony_ci     * @param permission Network permission to use
196b1b8bc3fSopenharmony_ci     *
197b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully set the permission for specific network, otherwise it will fail
198b1b8bc3fSopenharmony_ci     */
199b1b8bc3fSopenharmony_ci    int32_t SetPermissionForNetwork(int32_t netId, NetworkPermission permission);
200b1b8bc3fSopenharmony_ci
201b1b8bc3fSopenharmony_ci    /**
202b1b8bc3fSopenharmony_ci     * Find virtual network from netId
203b1b8bc3fSopenharmony_ci     *
204b1b8bc3fSopenharmony_ci     * @param netId The network id
205b1b8bc3fSopenharmony_ci     * @return Returns nullptr, the netId is not virtual network
206b1b8bc3fSopenharmony_ci     */
207b1b8bc3fSopenharmony_ci    std::shared_ptr<NetsysNetwork> FindVirtualNetwork(int32_t netId);
208b1b8bc3fSopenharmony_ci
209b1b8bc3fSopenharmony_ci    /**
210b1b8bc3fSopenharmony_ci     * Add uids to virtual network
211b1b8bc3fSopenharmony_ci     *
212b1b8bc3fSopenharmony_ci     * @param netId The virtual network id
213b1b8bc3fSopenharmony_ci     * @param uidRanges App uids to set
214b1b8bc3fSopenharmony_ci     *
215b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully set the uids for specific network, otherwise it will fail
216b1b8bc3fSopenharmony_ci     */
217b1b8bc3fSopenharmony_ci    int32_t AddUidsToNetwork(int32_t netId, const std::vector<NetManagerStandard::UidRange> &uidRanges);
218b1b8bc3fSopenharmony_ci
219b1b8bc3fSopenharmony_ci    /**
220b1b8bc3fSopenharmony_ci     * Remove uids from virtual network
221b1b8bc3fSopenharmony_ci     *
222b1b8bc3fSopenharmony_ci     * @param netId The virtual network id
223b1b8bc3fSopenharmony_ci     * @param uidRanges App uids to set
224b1b8bc3fSopenharmony_ci     *
225b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully remove the uids for specific network, otherwise it will fail
226b1b8bc3fSopenharmony_ci     */
227b1b8bc3fSopenharmony_ci    int32_t RemoveUidsFromNetwork(int32_t netId, const std::vector<NetManagerStandard::UidRange> &uidRanges);
228b1b8bc3fSopenharmony_ci
229b1b8bc3fSopenharmony_ci    /**
230b1b8bc3fSopenharmony_ci     * Get the Dump Infos object
231b1b8bc3fSopenharmony_ci     *
232b1b8bc3fSopenharmony_ci     * @param infos The output message
233b1b8bc3fSopenharmony_ci     */
234b1b8bc3fSopenharmony_ci    void GetDumpInfos(std::string &infos);
235b1b8bc3fSopenharmony_ci
236b1b8bc3fSopenharmony_ci    /**
237b1b8bc3fSopenharmony_ci     * Set the policy to access the network of the specified application.
238b1b8bc3fSopenharmony_ci     *
239b1b8bc3fSopenharmony_ci     * @param uid - The specified UID of application.
240b1b8bc3fSopenharmony_ci     * @param policy - the network access policy of application. For details, see {@link NetworkAccessPolicy}.
241b1b8bc3fSopenharmony_ci     * @return Returns 0, successfully set the network access policy for application, otherwise it will fail
242b1b8bc3fSopenharmony_ci     */
243b1b8bc3fSopenharmony_ci    int32_t SetNetworkAccessPolicy(uint32_t uid, NetManagerStandard::NetworkAccessPolicy policy, bool reconfirmFlag,
244b1b8bc3fSopenharmony_ci                                   bool isBroker);
245b1b8bc3fSopenharmony_ci    int32_t DeleteNetworkAccessPolicy(uint32_t uid);
246b1b8bc3fSopenharmony_ci    int32_t NotifyNetBearerTypeChange(std::set<NetManagerStandard::NetBearType> bearerTypes);
247b1b8bc3fSopenharmony_ci    int32_t CloseSocketsUid(const std::string &ipAddr, uint32_t uid);
248b1b8bc3fSopenharmony_ci
249b1b8bc3fSopenharmony_ciprivate:
250b1b8bc3fSopenharmony_ci    int32_t defaultNetId_;
251b1b8bc3fSopenharmony_ci    bool needReinitRouteFlag_;
252b1b8bc3fSopenharmony_ci    std::map<int32_t, std::string> physicalInterfaceName_;
253b1b8bc3fSopenharmony_ci    SafeMap<int32_t, std::shared_ptr<NetsysNetwork>> networks_;
254b1b8bc3fSopenharmony_ci    std::mutex interfaceNameMutex_;
255b1b8bc3fSopenharmony_ci    std::tuple<bool, std::shared_ptr<NetsysNetwork>> FindNetworkById(int32_t netId);
256b1b8bc3fSopenharmony_ci    int32_t GetNetworkForInterface(int32_t netId, std::string &interfaceName);
257b1b8bc3fSopenharmony_ci    RouteManager::TableType GetTableType(int32_t netId);
258b1b8bc3fSopenharmony_ci};
259b1b8bc3fSopenharmony_ci} // namespace nmd
260b1b8bc3fSopenharmony_ci} // namespace OHOS
261b1b8bc3fSopenharmony_ci#endif // INCLUDE_CONN_MANAGER_H
262