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 34namespace OHOS { 35namespace nmd { 36class ConnManager { 37public: 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 bool& routeRepeat); 155 156 /** 157 * Remove a route for specific network 158 * 159 * @param netId The network to remove the route 160 * @param interfaceName The name of interface of the route 161 * This interface should be assigned to the netID 162 * @param destination The destination of the route 163 * @param nextHop The route's next hop address 164 * 165 * @return Returns 0, successfully remove a route for specific network, otherwise it will fail 166 */ 167 int32_t RemoveRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop); 168 169 /** 170 * Update a route for specific network 171 * 172 * @param netId The network to update the route 173 * @param interfaceName The name of interface of the route 174 * This interface should be assigned to the netID 175 * @param destination The destination of the route 176 * @param nextHop The route's next hop address 177 * 178 * @return Returns 0, successfully update a route for specific network, otherwise it will fail 179 */ 180 int32_t UpdateRoute(int32_t netId, std::string interfaceName, std::string destination, std::string nextHop); 181 182 /** 183 * Get the mark for the given network id 184 * 185 * @param netId The network to get the mark 186 * 187 * @return A Mark of the given network id. 188 */ 189 int32_t GetFwmarkForNetwork(int32_t netId); 190 191 /** 192 * Set the permission required to access a specific network 193 * 194 * @param netId The network to set 195 * @param permission Network permission to use 196 * 197 * @return Returns 0, successfully set the permission for specific network, otherwise it will fail 198 */ 199 int32_t SetPermissionForNetwork(int32_t netId, NetworkPermission permission); 200 201 /** 202 * Find virtual network from netId 203 * 204 * @param netId The network id 205 * @return Returns nullptr, the netId is not virtual network 206 */ 207 std::shared_ptr<NetsysNetwork> FindVirtualNetwork(int32_t netId); 208 209 /** 210 * Add uids to virtual network 211 * 212 * @param netId The virtual network id 213 * @param uidRanges App uids to set 214 * 215 * @return Returns 0, successfully set the uids for specific network, otherwise it will fail 216 */ 217 int32_t AddUidsToNetwork(int32_t netId, const std::vector<NetManagerStandard::UidRange> &uidRanges); 218 219 /** 220 * Remove uids from virtual network 221 * 222 * @param netId The virtual network id 223 * @param uidRanges App uids to set 224 * 225 * @return Returns 0, successfully remove the uids for specific network, otherwise it will fail 226 */ 227 int32_t RemoveUidsFromNetwork(int32_t netId, const std::vector<NetManagerStandard::UidRange> &uidRanges); 228 229 /** 230 * Get the Dump Infos object 231 * 232 * @param infos The output message 233 */ 234 void GetDumpInfos(std::string &infos); 235 236 /** 237 * Set the policy to access the network of the specified application. 238 * 239 * @param uid - The specified UID of application. 240 * @param policy - the network access policy of application. For details, see {@link NetworkAccessPolicy}. 241 * @return Returns 0, successfully set the network access policy for application, otherwise it will fail 242 */ 243 int32_t SetNetworkAccessPolicy(uint32_t uid, NetManagerStandard::NetworkAccessPolicy policy, bool reconfirmFlag, 244 bool isBroker); 245 int32_t DeleteNetworkAccessPolicy(uint32_t uid); 246 int32_t NotifyNetBearerTypeChange(std::set<NetManagerStandard::NetBearType> bearerTypes); 247 int32_t CloseSocketsUid(const std::string &ipAddr, uint32_t uid); 248 249private: 250 int32_t defaultNetId_; 251 bool needReinitRouteFlag_; 252 std::map<int32_t, std::string> physicalInterfaceName_; 253 SafeMap<int32_t, std::shared_ptr<NetsysNetwork>> networks_; 254 std::mutex interfaceNameMutex_; 255 std::tuple<bool, std::shared_ptr<NetsysNetwork>> FindNetworkById(int32_t netId); 256 int32_t GetNetworkForInterface(int32_t netId, std::string &interfaceName); 257 RouteManager::TableType GetTableType(int32_t netId); 258}; 259} // namespace nmd 260} // namespace OHOS 261#endif // INCLUDE_CONN_MANAGER_H 262