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