1 /* 2 * Copyright (C) 2021-2022 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_ROUTE_MANAGER_H 17 #define INCLUDE_ROUTE_MANAGER_H 18 19 #include <linux/netlink.h> 20 #include <map> 21 #include <netinet/in.h> 22 23 #include "netlink_msg.h" 24 #include "network_permission.h" 25 26 namespace OHOS { 27 namespace nmd { 28 typedef struct RuleInfo { 29 uint32_t ruleTable; 30 uint32_t rulePriority; 31 uint32_t ruleFwmark; 32 uint32_t ruleMask; 33 std::string ruleIif; 34 std::string ruleOif; 35 } RuleInfo; 36 37 typedef struct RouteInfo { 38 uint32_t routeTable; 39 std::string routeInterfaceName; 40 std::string routeDestinationName; 41 std::string routeNextHop; 42 } RouteInfo; 43 44 typedef struct InetAddr { 45 int32_t family; 46 int32_t bitlen; 47 int32_t prefixlen; 48 uint8_t data[sizeof(struct in6_addr)]; 49 } InetAddr; 50 51 class RouteManager { 52 public: 53 RouteManager(); 54 ~RouteManager() = default; 55 56 /** 57 * Route table type 58 * 59 */ 60 enum TableType { 61 INTERFACE, 62 VPN_NETWORK, 63 LOCAL_NETWORK, 64 }; 65 66 /** 67 * The interface is add route table 68 * 69 * @param tableType Route table type.Must be one of INTERFACE/VPN_NETWORK/LOCAL_NETWORK. 70 * @param interfaceName Output network device name of the route item 71 * @param destinationName Destination address of route item 72 * @param nextHop Gateway address of the route item 73 * @return Returns 0, add route table successfully, otherwise it will fail 74 */ 75 static int32_t AddRoute(TableType tableType, const std::string &interfaceName, const std::string &destinationName, 76 const std::string &nextHop); 77 78 /** 79 * The interface is remove route table 80 * 81 * @param tableType Route table type.Must be one of INTERFACE/VPN_NETWORK/LOCAL_NETWORK. 82 * @param interfaceName Output network device name of the route item 83 * @param destinationName Destination address of route item 84 * @param nextHop Gateway address of the route item 85 * @return Returns 0, remove route table successfully, otherwise it will fail 86 */ 87 static int32_t RemoveRoute(TableType tableType, const std::string &interfaceName, 88 const std::string &destinationName, const std::string &nextHop); 89 90 /** 91 * The interface is update route table 92 * 93 * @param tableType Route table type.Must be one of INTERFACE/VPN_NETWORK/LOCAL_NETWORK. 94 * @param interfaceName Output network device name of the route item 95 * @param destinationName Destination address of route item 96 * @param nextHop Gateway address of the route item 97 * @return Returns 0, update route table successfully, otherwise it will fail 98 */ 99 static int32_t UpdateRoute(TableType tableType, const std::string &interfaceName, 100 const std::string &destinationName, const std::string &nextHop); 101 102 /** 103 * Add interface to default network 104 * 105 * @param interfaceName Output network device name of the route item 106 * @param permission Network permission. Must be one of 107 * PERMISSION_NONE/PERMISSION_NETWORK/PERMISSION_SYSTEM. 108 * @return Returns 0, add interface to default network successfully, otherwise it will fail 109 */ 110 static int32_t AddInterfaceToDefaultNetwork(const std::string &interfaceName, NetworkPermission permission); 111 112 /** 113 * Remove interface from default network 114 * 115 * @param interfaceName Output network device name of the route item 116 * @param permission Network permission. Must be one of 117 * PERMISSION_NONE/PERMISSION_NETWORK/PERMISSION_SYSTEM. 118 * @return Returns 0, remove interface from default network successfully, otherwise it will fail 119 */ 120 static int32_t RemoveInterfaceFromDefaultNetwork(const std::string &interfaceName, NetworkPermission permission); 121 122 /** 123 * Add interface to physical network 124 * 125 * @param netId Network number 126 * @param interfaceName Output network device name of the route item 127 * @param permission Network permission. Must be one of 128 * PERMISSION_NONE/PERMISSION_NETWORK/PERMISSION_SYSTEM. 129 * @return Returns 0, add interface to physical network successfully, otherwise it will fail 130 */ 131 static int32_t AddInterfaceToPhysicalNetwork(uint16_t netId, const std::string &interfaceName, 132 NetworkPermission permission); 133 134 /** 135 * Remove interface from physical network 136 * 137 * @param netId Network number 138 * @param interfaceName Output network device name of the route item 139 * @param permission Network permission. Must be one of 140 * PERMISSION_NONE/PERMISSION_NETWORK/PERMISSION_SYSTEM. 141 * @return Returns 0, remove interface from physical network successfully, otherwise it will fail 142 */ 143 static int32_t RemoveInterfaceFromPhysicalNetwork(uint16_t netId, const std::string &interfaceName, 144 NetworkPermission permission); 145 146 /** 147 * Modify physical network permission 148 * 149 * @param netId Network number 150 * @param interfaceName Output network device name of the route item 151 * @param oldPermission Old network permission. Must be one of 152 * PERMISSION_NONE/PERMISSION_NETWORK/PERMISSION_SYSTEM. 153 * @param newPermission New network permission. Must be one of 154 * PERMISSION_NONE/PERMISSION_NETWORK/PERMISSION_SYSTEM. 155 * @return Returns 0, modify physical network permission successfully, otherwise it will fail 156 */ 157 static int32_t ModifyPhysicalNetworkPermission(uint16_t netId, const std::string &interfaceName, 158 NetworkPermission oldPermission, NetworkPermission newPermission); 159 160 /** 161 * Add interface to local network 162 * 163 * @param netId Network number 164 * @param interfaceName Output network device name of the route item 165 * @return Returns 0, add interface to local network successfully, otherwise it will fail 166 */ 167 static int32_t AddInterfaceToLocalNetwork(uint16_t netId, const std::string &interfaceName); 168 169 /** 170 * Remove interface from local network 171 * 172 * @param netId Network number 173 * @param interfaceName Output network device name of the route item 174 * @return Returns 0, remove interface from local network successfully, otherwise it will fail 175 */ 176 static int32_t RemoveInterfaceFromLocalNetwork(uint16_t netId, const std::string &interfaceName); 177 178 /** 179 * Enable sharing network 180 * 181 * @param inputInterface Input network device name of the route item 182 * @param outputInterface Output network device name of the route item 183 * @return Returns 0, enable sharing network successfully, otherwise it will fail 184 */ 185 static int32_t EnableSharing(const std::string &inputInterface, const std::string &outputInterface); 186 187 /** 188 * Disable sharing network 189 * 190 * @param inputInterface Input network device name of the route item 191 * @param outputInterface Output network device name of the route item 192 * @return Returns 0, disable sharing network successfully, otherwise it will fail 193 */ 194 static int32_t DisableSharing(const std::string &inputInterface, const std::string &outputInterface); 195 196 /** 197 * Parse destination address 198 * 199 * @param addr Address to be parse 200 * @param res Parse result 201 * @return Returns 0, parse destination address successfully, otherwise it will fail 202 */ 203 static int32_t ReadAddr(const std::string &addr, InetAddr *res); 204 205 /** 206 * Parse gateway address 207 * 208 * @param addr Address to be parse 209 * @param res Parse result 210 * @return Returns 0, parse gateway address successfully, otherwise it will fail 211 */ 212 static int32_t ReadAddrGw(const std::string &addr, InetAddr *res); 213 214 private: 215 static std::mutex interfaceToTableLock_; 216 static std::map<std::string, uint32_t> interfaceToTable_; 217 static int32_t Init(); 218 static int32_t ClearRules(); 219 static int32_t ClearRoutes(const std::string &interfaceName); 220 static int32_t AddLocalNetworkRules(); 221 static int32_t UpdatePhysicalNetwork(uint16_t netId, const std::string &interfaceName, 222 NetworkPermission permission, bool add); 223 static int32_t UpdateLocalNetwork(uint16_t netId, const std::string &interfaceName, bool add); 224 static int32_t UpdateIncomingPacketMark(uint16_t netId, const std::string &interfaceName, 225 NetworkPermission permission, bool add); 226 static int32_t UpdateExplicitNetworkRule(uint16_t netId, uint32_t table, NetworkPermission permission, bool add); 227 static int32_t UpdateOutputInterfaceRules(const std::string &interfaceName, uint32_t table, 228 NetworkPermission permission, bool add); 229 static int32_t UpdateSharingNetwork(uint16_t action, const std::string &inputInterface, 230 const std::string &outputInterface); 231 static int32_t ClearSharingRules(const std::string &inputInterface); 232 static int32_t UpdateRuleInfo(uint32_t action, uint8_t ruleType, RuleInfo ruleInfo); 233 static int32_t SendRuleToKernel(uint32_t action, uint16_t ruleFlag, uint8_t ruleType, RuleInfo ruleInfo); 234 static int32_t UpdateRouteRule(uint16_t action, uint16_t flags, RouteInfo routeInfo); 235 static int32_t SendRouteToKernel(uint16_t action, uint16_t routeFlag, rtmsg msg, RouteInfo routeInfo, 236 uint32_t index); 237 static uint32_t FindTableByInterfacename(const std::string &interfaceName); 238 static uint32_t GetRouteTableFromType(TableType tableType, const std::string &interfaceName); 239 }; 240 } // namespace nmd 241 } // namespace OHOS 242 #endif // INCLUDE_ROUTE_MANAGER_H 243