| Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "RouteController.h" |
| 18 | |
| Dan Albert | aa1be2b | 2015-01-06 09:36:17 -0800 | [diff] [blame] | 19 | #include <arpa/inet.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <linux/fib_rules.h> |
| 23 | #include <net/if.h> |
| 24 | #include <sys/stat.h> |
| 25 | |
| Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
| 27 | |
| Dan Albert | aa1be2b | 2015-01-06 09:36:17 -0800 | [diff] [blame] | 28 | #include <map> |
| 29 | |
| Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 30 | #include "DummyNetwork.h" |
| Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 31 | #include "Fwmark.h" |
| Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 32 | #include "NetdConstants.h" |
| Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame] | 33 | #include "NetlinkCommands.h" |
| Patrick Rohr | 70d4250 | 2021-10-12 18:24:10 +0200 | [diff] [blame] | 34 | #include "TcUtils.h" |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 35 | |
| Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 36 | #include <android-base/file.h> |
| Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> |
| Hungming Chen | 7f72543 | 2020-02-07 17:47:23 +0800 | [diff] [blame] | 38 | #include <android-base/strings.h> |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 39 | #include "log/log.h" |
| Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 40 | #include "netid_client.h" |
| Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 41 | #include "netutils/ifc.h" |
| Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 42 | |
| Hungming Chen | 7f72543 | 2020-02-07 17:47:23 +0800 | [diff] [blame] | 43 | using android::base::StartsWith; |
| Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 44 | using android::base::StringPrintf; |
| Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 45 | using android::base::WriteStringToFile; |
| Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 46 | using android::net::UidRangeParcel; |
| Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 47 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 48 | namespace android::net { |
| Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 49 | |
| Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 50 | auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand; |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 51 | auto RouteController::ifNameToIndexFunction = if_nametoindex; |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 52 | // BEGIN CONSTANTS -------------------------------------------------------------------------------- |
| 53 | |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 54 | const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97; |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 55 | const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98; |
| 56 | const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99; |
| 57 | |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 58 | const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network"; |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 59 | const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network"; |
| 60 | const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system"; |
| 61 | |
| Sreeram Ramachandran | bb40d51 | 2014-07-11 11:45:14 -0700 | [diff] [blame] | 62 | const char* const ROUTE_TABLE_NAME_LOCAL = "local"; |
| 63 | const char* const ROUTE_TABLE_NAME_MAIN = "main"; |
| 64 | |
| Lorenzo Colitti | d78843e | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 65 | const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT"; |
| 66 | |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 67 | const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6}; |
| 68 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 69 | const uid_t UID_ROOT = 0; |
| Lorenzo Colitti | 3093f56 | 2017-09-25 14:17:38 +0900 | [diff] [blame] | 70 | const uint32_t FWMARK_NONE = 0; |
| 71 | const uint32_t MASK_NONE = 0; |
| Lorenzo Colitti | 5ad4e98 | 2015-02-26 17:34:32 +0900 | [diff] [blame] | 72 | const char* const IIF_LOOPBACK = "lo"; |
| Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 73 | const char* const IIF_NONE = nullptr; |
| 74 | const char* const OIF_NONE = nullptr; |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 75 | const bool ACTION_ADD = true; |
| 76 | const bool ACTION_DEL = false; |
| 77 | const bool MODIFY_NON_UID_BASED_RULES = true; |
| 78 | |
| Sreeram Ramachandran | c7d804c | 2014-07-09 07:39:30 -0700 | [diff] [blame] | 79 | const mode_t RT_TABLES_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // mode 0644, rw-r--r-- |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 80 | |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 81 | // Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'" |
| 82 | // warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t). |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 83 | static constexpr uint16_t U16_RTA_LENGTH(uint16_t x) { |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 84 | return RTA_LENGTH(x); |
| 85 | } |
| 86 | |
| 87 | // These are practically const, but can't be declared so, because they are used to initialize |
| 88 | // non-const pointers ("void* iov_base") in iovec arrays. |
| Lorenzo Colitti | 2b07867 | 2016-12-16 18:45:03 +0900 | [diff] [blame] | 89 | rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY }; |
| 90 | rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE }; |
| 91 | rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK }; |
| 92 | rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK }; |
| Lorenzo Colitti | 2b07867 | 2016-12-16 18:45:03 +0900 | [diff] [blame] | 93 | rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE }; |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 94 | |
| Lorenzo Colitti | 2b07867 | 2016-12-16 18:45:03 +0900 | [diff] [blame] | 95 | rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE }; |
| 96 | rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF }; |
| Lorenzo Colitti | 5e03a89 | 2017-09-08 11:31:59 +0900 | [diff] [blame] | 97 | rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY }; |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 98 | |
| Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 99 | // One or more nested attributes in the RTA_METRICS attribute. |
| 100 | rtattr RTATTRX_MTU = { U16_RTA_LENGTH(sizeof(uint32_t)), RTAX_MTU}; |
| Lorenzo Colitti | 57d5468 | 2020-01-24 09:18:13 +0900 | [diff] [blame] | 101 | constexpr size_t RTATTRX_MTU_SIZE = RTA_SPACE(sizeof(uint32_t)); |
| Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 102 | |
| 103 | // The RTA_METRICS attribute itself. |
| Lorenzo Colitti | 57d5468 | 2020-01-24 09:18:13 +0900 | [diff] [blame] | 104 | constexpr size_t RTATTR_METRICS_SIZE = RTATTRX_MTU_SIZE; |
| Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 105 | rtattr RTATTR_METRICS = { U16_RTA_LENGTH(RTATTR_METRICS_SIZE), RTA_METRICS }; |
| 106 | |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 107 | uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0}; |
| 108 | |
| Ken Chen | a2206ec | 2021-03-24 17:15:44 +0800 | [diff] [blame] | 109 | constexpr bool EXPLICIT = true; |
| 110 | constexpr bool IMPLICIT = false; |
| 111 | |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 112 | // END CONSTANTS ---------------------------------------------------------------------------------- |
| 113 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 114 | static const char* actionName(uint16_t action) { |
| Lorenzo Colitti | 0b073fb | 2017-02-10 07:49:12 +0900 | [diff] [blame] | 115 | static const char *ops[4] = {"adding", "deleting", "getting", "???"}; |
| 116 | return ops[action % 4]; |
| 117 | } |
| 118 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 119 | static const char* familyName(uint8_t family) { |
| Lorenzo Colitti | 0b073fb | 2017-02-10 07:49:12 +0900 | [diff] [blame] | 120 | switch (family) { |
| 121 | case AF_INET: return "IPv4"; |
| 122 | case AF_INET6: return "IPv6"; |
| 123 | default: return "???"; |
| 124 | } |
| 125 | } |
| 126 | |
| Lorenzo Colitti | 3810c97 | 2021-01-06 11:44:02 +0900 | [diff] [blame] | 127 | static void maybeModifyQdiscClsact(const char* interface, bool add); |
| 128 | |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 129 | static uint32_t getRouteTableIndexFromGlobalRouteTableIndex(uint32_t index, bool local) { |
| 130 | // The local table is |
| 131 | // "global table - ROUTE_TABLE_OFFSET_FROM_INDEX + ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL" |
| 132 | const uint32_t localTableOffset = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL - |
| 133 | RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX; |
| 134 | return local ? index + localTableOffset : index; |
| 135 | } |
| 136 | |
| Lorenzo Colitti | 107075a | 2017-10-30 19:24:46 +0900 | [diff] [blame] | 137 | // Caller must hold sInterfaceToTableLock. |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 138 | uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface, bool local) { |
| mtk13799 | 9f2913e | 2019-02-25 19:39:36 +0800 | [diff] [blame] | 139 | // If we already know the routing table for this interface name, use it. |
| 140 | // This ensures we can remove rules and routes for an interface that has been removed, |
| 141 | // or has been removed and re-added with a different interface index. |
| 142 | // |
| 143 | // The caller is responsible for ensuring that an interface is never added to a network |
| 144 | // until it has been removed from any network it was previously in. This ensures that |
| 145 | // if the same interface disconnects and then reconnects with a different interface ID |
| 146 | // when the reconnect happens the interface will not be in the map, and the code will |
| 147 | // determine the new routing table from the interface ID, below. |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 148 | // |
| 149 | // sInterfaceToTable stores the *global* routing table for the interface, and the local table is |
| 150 | // "global table - ROUTE_TABLE_OFFSET_FROM_INDEX + ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL" |
| Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 151 | auto iter = sInterfaceToTable.find(interface); |
| mtk13799 | 9f2913e | 2019-02-25 19:39:36 +0800 | [diff] [blame] | 152 | if (iter != sInterfaceToTable.end()) { |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 153 | return getRouteTableIndexFromGlobalRouteTableIndex(iter->second, local); |
| mtk13799 | 9f2913e | 2019-02-25 19:39:36 +0800 | [diff] [blame] | 154 | } |
| 155 | |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 156 | uint32_t index = RouteController::ifNameToIndexFunction(interface); |
| mtk13799 | 9f2913e | 2019-02-25 19:39:36 +0800 | [diff] [blame] | 157 | if (index == 0) { |
| Lorenzo Colitti | b6dc40a | 2020-03-24 00:58:50 +0900 | [diff] [blame] | 158 | ALOGE("cannot find interface %s: %s", interface, strerror(errno)); |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 159 | return RT_TABLE_UNSPEC; |
| 160 | } |
| mtk13799 | 9f2913e | 2019-02-25 19:39:36 +0800 | [diff] [blame] | 161 | index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX; |
| 162 | sInterfaceToTable[interface] = index; |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 163 | return getRouteTableIndexFromGlobalRouteTableIndex(index, local); |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| Rubin Xu | 6c00b61 | 2018-04-27 14:27:59 +0100 | [diff] [blame] | 166 | uint32_t RouteController::getIfIndex(const char* interface) { |
| Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 167 | std::lock_guard lock(sInterfaceToTableLock); |
| Rubin Xu | 6c00b61 | 2018-04-27 14:27:59 +0100 | [diff] [blame] | 168 | |
| 169 | auto iter = sInterfaceToTable.find(interface); |
| 170 | if (iter == sInterfaceToTable.end()) { |
| 171 | ALOGE("getIfIndex: cannot find interface %s", interface); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| Lorenzo Colitti | b6dc40a | 2020-03-24 00:58:50 +0900 | [diff] [blame] | 175 | // For interfaces that are not in the local network, the routing table is always the interface |
| 176 | // index plus ROUTE_TABLE_OFFSET_FROM_INDEX. But for interfaces in the local network, there's no |
| 177 | // way to know the interface index from this table. Return 0 here so callers of this method do |
| 178 | // not get confused. |
| 179 | // TODO: stop calling this method from any caller that only wants interfaces in client mode. |
| 180 | int ifindex = iter->second; |
| 181 | if (ifindex == ROUTE_TABLE_LOCAL_NETWORK) { |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | return ifindex - ROUTE_TABLE_OFFSET_FROM_INDEX; |
| Rubin Xu | 6c00b61 | 2018-04-27 14:27:59 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 188 | uint32_t RouteController::getRouteTableForInterface(const char* interface, bool local) { |
| Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 189 | std::lock_guard lock(sInterfaceToTableLock); |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 190 | return getRouteTableForInterfaceLocked(interface, local); |
| Lorenzo Colitti | 107075a | 2017-10-30 19:24:46 +0900 | [diff] [blame] | 191 | } |
| 192 | |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 193 | void addTableName(uint32_t table, const std::string& name, std::string* contents) { |
| 194 | char tableString[UINT32_STRLEN]; |
| 195 | snprintf(tableString, sizeof(tableString), "%u", table); |
| 196 | *contents += tableString; |
| 197 | *contents += " "; |
| 198 | *contents += name; |
| 199 | *contents += "\n"; |
| 200 | } |
| 201 | |
| 202 | // Doesn't return success/failure as the file is optional; it's okay if we fail to update it. |
| Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 203 | void RouteController::updateTableNamesFile() { |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 204 | std::string contents; |
| Sreeram Ramachandran | bb40d51 | 2014-07-11 11:45:14 -0700 | [diff] [blame] | 205 | |
| 206 | addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents); |
| 207 | addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents); |
| 208 | |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 209 | addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents); |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 210 | addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents); |
| 211 | addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents); |
| Sreeram Ramachandran | bb40d51 | 2014-07-11 11:45:14 -0700 | [diff] [blame] | 212 | |
| Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 213 | std::lock_guard lock(sInterfaceToTableLock); |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 214 | for (const auto& [ifName, ifIndex] : sInterfaceToTable) { |
| 215 | addTableName(ifIndex, ifName, &contents); |
| 216 | // Add table for the local route of the network. It's expected to be used for excluding the |
| 217 | // local traffic in the VPN network. |
| 218 | // Start from ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL plus with the interface table index. |
| 219 | uint32_t offset = ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL - ROUTE_TABLE_OFFSET_FROM_INDEX; |
| 220 | addTableName(offset + ifIndex, ifName + INTERFACE_LOCAL_SUFFIX, &contents); |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 223 | if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) { |
| Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 224 | ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno)); |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 225 | return; |
| 226 | } |
| Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 229 | // Returns 0 on success or negative errno on failure. |
| 230 | int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) { |
| 231 | if (!input) { |
| 232 | *length = 0; |
| 233 | *padding = 0; |
| 234 | return 0; |
| 235 | } |
| 236 | *length = strlcpy(name, input, IFNAMSIZ) + 1; |
| 237 | if (*length > IFNAMSIZ) { |
| 238 | ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ); |
| 239 | return -ENAMETOOLONG; |
| 240 | } |
| 241 | *padding = RTA_SPACE(*length) - RTA_LENGTH(*length); |
| 242 | return 0; |
| 243 | } |
| 244 | |
| Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 245 | // Adds or removes a routing rule for IPv4 and IPv6. |
| 246 | // |
| Lorenzo Colitti | 6b6f25f | 2015-03-03 17:22:57 +0900 | [diff] [blame] | 247 | // + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is |
| Robin Lee | 4ef9464 | 2016-04-01 11:50:49 +0100 | [diff] [blame] | 248 | // unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule. |
| Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 249 | // + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is |
| 250 | // ignored. |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 251 | // + If |iif| is non-NULL, the rule matches the specified incoming interface. |
| 252 | // + If |oif| is non-NULL, the rule matches the specified outgoing interface. |
| 253 | // + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that |
| 254 | // range (inclusive). Otherwise, the rule matches packets from all UIDs. |
| Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 255 | // |
| 256 | // Returns 0 on success or negative errno on failure. |
| Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 257 | [[nodiscard]] static int modifyIpRule(uint16_t action, int32_t priority, uint8_t ruleType, |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 258 | uint32_t table, uint32_t fwmark, uint32_t mask, |
| 259 | const char* iif, const char* oif, uid_t uidStart, |
| 260 | uid_t uidEnd) { |
| Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 261 | if (priority < 0) { |
| 262 | ALOGE("invalid IP-rule priority %d", priority); |
| 263 | return -ERANGE; |
| 264 | } |
| 265 | |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 266 | // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask. |
| 267 | if (fwmark & ~mask) { |
| 268 | ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark); |
| 269 | return -ERANGE; |
| 270 | } |
| 271 | |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 272 | // Interface names must include exactly one terminating NULL and be properly padded, or older |
| Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 273 | // kernels will refuse to delete rules. |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 274 | char iifName[IFNAMSIZ], oifName[IFNAMSIZ]; |
| 275 | size_t iifLength, oifLength; |
| 276 | uint16_t iifPadding, oifPadding; |
| 277 | if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) { |
| 278 | return ret; |
| 279 | } |
| 280 | if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) { |
| 281 | return ret; |
| Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 282 | } |
| 283 | |
| Lorenzo Colitti | 5965651 | 2014-06-25 03:20:29 +0900 | [diff] [blame] | 284 | // Either both start and end UID must be specified, or neither. |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 285 | if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) { |
| Sreeram Ramachandran | cf89138 | 2014-07-01 15:49:20 -0700 | [diff] [blame] | 286 | ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd); |
| Lorenzo Colitti | 5965651 | 2014-06-25 03:20:29 +0900 | [diff] [blame] | 287 | return -EUSERS; |
| 288 | } |
| Robin Lee | 4ef9464 | 2016-04-01 11:50:49 +0100 | [diff] [blame] | 289 | |
| Lorenzo Colitti | 7272368 | 2014-06-26 13:51:10 +0900 | [diff] [blame] | 290 | bool isUidRule = (uidStart != INVALID_UID); |
| Lorenzo Colitti | 5965651 | 2014-06-25 03:20:29 +0900 | [diff] [blame] | 291 | |
| Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 292 | // Assemble a rule request and put it in an array of iovec structures. |
| 293 | fib_rule_hdr rule = { |
| Robin Lee | 4ef9464 | 2016-04-01 11:50:49 +0100 | [diff] [blame] | 294 | .action = ruleType, |
| Lorenzo Colitti | 6b6f25f | 2015-03-03 17:22:57 +0900 | [diff] [blame] | 295 | // Note that here we're implicitly setting rule.table to 0. When we want to specify a |
| 296 | // non-zero table, we do this via the FRATTR_TABLE attribute. |
| Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 297 | }; |
| 298 | |
| Lorenzo Colitti | 6b6f25f | 2015-03-03 17:22:57 +0900 | [diff] [blame] | 299 | // Don't ever create a rule that looks up table 0, because table 0 is the local table. |
| 300 | // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select |
| 301 | // table 0, it's a wildcard that matches anything. |
| 302 | if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) { |
| 303 | ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules"); |
| 304 | return -ENOTUNIQ; |
| 305 | } |
| 306 | |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 307 | rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME }; |
| 308 | rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME }; |
| Lorenzo Colitti | 2b07867 | 2016-12-16 18:45:03 +0900 | [diff] [blame] | 309 | struct fib_rule_uid_range uidRange = { uidStart, uidEnd }; |
| Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 310 | |
| 311 | iovec iov[] = { |
| Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 312 | { nullptr, 0 }, |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 313 | { &rule, sizeof(rule) }, |
| 314 | { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) }, |
| 315 | { &priority, sizeof(priority) }, |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 316 | { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 }, |
| 317 | { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 }, |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 318 | { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 }, |
| 319 | { &fwmark, mask ? sizeof(fwmark) : 0 }, |
| 320 | { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 }, |
| 321 | { &mask, mask ? sizeof(mask) : 0 }, |
| Lorenzo Colitti | 2b07867 | 2016-12-16 18:45:03 +0900 | [diff] [blame] | 322 | { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 }, |
| 323 | { &uidRange, isUidRule ? sizeof(uidRange) : 0 }, |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 324 | { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 }, |
| 325 | { iifName, iifLength }, |
| 326 | { PADDING_BUFFER, iifPadding }, |
| 327 | { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 }, |
| 328 | { oifName, oifLength }, |
| 329 | { PADDING_BUFFER, oifPadding }, |
| Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 330 | }; |
| 331 | |
| Lorenzo Colitti | 5c43799 | 2017-11-28 01:26:02 +0900 | [diff] [blame] | 332 | uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS; |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 333 | for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) { |
| 334 | rule.family = AF_FAMILIES[i]; |
| Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 335 | if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) { |
| Lorenzo Colitti | 220ca73 | 2017-02-14 17:57:55 +0900 | [diff] [blame] | 336 | if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) { |
| 337 | // Don't log when deleting a tethering rule that's not there. This matches the |
| 338 | // behaviour of clearTetheringRules, which ignores ENOENT in this case. |
| 339 | ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family), |
| 340 | strerror(-ret)); |
| 341 | } |
| Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 342 | return ret; |
| Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 346 | return 0; |
| Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 349 | [[nodiscard]] static int modifyIpRule(uint16_t action, int32_t priority, uint32_t table, |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 350 | uint32_t fwmark, uint32_t mask, const char* iif, |
| 351 | const char* oif, uid_t uidStart, uid_t uidEnd) { |
| Robin Lee | 4ef9464 | 2016-04-01 11:50:49 +0100 | [diff] [blame] | 352 | return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart, |
| 353 | uidEnd); |
| 354 | } |
| 355 | |
| Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 356 | [[nodiscard]] static int modifyIpRule(uint16_t action, int32_t priority, uint32_t table, |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 357 | uint32_t fwmark, uint32_t mask) { |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 358 | return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID, |
| 359 | INVALID_UID); |
| 360 | } |
| 361 | |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 362 | // Adds or deletes an IPv4 or IPv6 route. |
| 363 | // Returns 0 on success or negative errno on failure. |
| Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 364 | int modifyIpRoute(uint16_t action, uint16_t flags, uint32_t table, const char* interface, |
| Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 365 | const char* destination, const char* nexthop, uint32_t mtu, uint32_t priority) { |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 366 | // At least the destination must be non-null. |
| 367 | if (!destination) { |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 368 | ALOGE("null destination"); |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 369 | return -EFAULT; |
| 370 | } |
| Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 371 | |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 372 | // Parse the prefix. |
| 373 | uint8_t rawAddress[sizeof(in6_addr)]; |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 374 | uint8_t family; |
| 375 | uint8_t prefixLength; |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 376 | int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress), |
| 377 | &prefixLength); |
| 378 | if (rawLength < 0) { |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 379 | ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength)); |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 380 | return rawLength; |
| 381 | } |
| Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 382 | |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 383 | if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) { |
| Sreeram Ramachandran | 1201e84 | 2014-07-01 15:06:05 -0700 | [diff] [blame] | 384 | ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress)); |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 385 | return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6. |
| 386 | } |
| 387 | |
| Sreeram Ramachandran | de5d5df | 2014-07-26 18:43:25 -0700 | [diff] [blame] | 388 | uint8_t type = RTN_UNICAST; |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 389 | uint32_t ifindex; |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 390 | uint8_t rawNexthop[sizeof(in6_addr)]; |
| Sreeram Ramachandran | de5d5df | 2014-07-26 18:43:25 -0700 | [diff] [blame] | 391 | |
| 392 | if (nexthop && !strcmp(nexthop, "unreachable")) { |
| 393 | type = RTN_UNREACHABLE; |
| 394 | // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup |
| 395 | // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for |
| 396 | // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.) |
| 397 | interface = OIF_NONE; |
| Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 398 | nexthop = nullptr; |
| Lorenzo Colitti | 4c95a12 | 2014-09-18 16:01:50 +0900 | [diff] [blame] | 399 | } else if (nexthop && !strcmp(nexthop, "throw")) { |
| 400 | type = RTN_THROW; |
| 401 | interface = OIF_NONE; |
| Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 402 | nexthop = nullptr; |
| Sreeram Ramachandran | de5d5df | 2014-07-26 18:43:25 -0700 | [diff] [blame] | 403 | } else { |
| 404 | // If an interface was specified, find the ifindex. |
| 405 | if (interface != OIF_NONE) { |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 406 | ifindex = RouteController::ifNameToIndexFunction(interface); |
| 407 | |
| Sreeram Ramachandran | de5d5df | 2014-07-26 18:43:25 -0700 | [diff] [blame] | 408 | if (!ifindex) { |
| 409 | ALOGE("cannot find interface %s", interface); |
| 410 | return -ENODEV; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // If a nexthop was specified, parse it as the same family as the prefix. |
| 415 | if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) { |
| 416 | ALOGE("inet_pton failed for nexthop %s", nexthop); |
| 417 | return -EINVAL; |
| 418 | } |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 419 | } |
| 420 | |
| Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 421 | // Assemble a rtmsg and put it in an array of iovec structures. |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 422 | rtmsg route = { |
| Nick Desaulniers | 6b35750 | 2019-10-11 09:26:44 -0700 | [diff] [blame] | 423 | .rtm_family = family, |
| 424 | .rtm_dst_len = prefixLength, |
| 425 | .rtm_protocol = RTPROT_STATIC, |
| 426 | .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK), |
| 427 | .rtm_type = type, |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 428 | }; |
| Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 429 | |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 430 | rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST }; |
| 431 | rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY }; |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 432 | |
| 433 | iovec iov[] = { |
| Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 434 | {nullptr, 0}, |
| 435 | {&route, sizeof(route)}, |
| 436 | {&RTATTR_TABLE, sizeof(RTATTR_TABLE)}, |
| 437 | {&table, sizeof(table)}, |
| 438 | {&rtaDst, sizeof(rtaDst)}, |
| 439 | {rawAddress, static_cast<size_t>(rawLength)}, |
| 440 | {&RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0}, |
| 441 | {&ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0}, |
| 442 | {&rtaGateway, nexthop ? sizeof(rtaGateway) : 0}, |
| 443 | {rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0}, |
| 444 | {&RTATTR_METRICS, mtu != 0 ? sizeof(RTATTR_METRICS) : 0}, |
| 445 | {&RTATTRX_MTU, mtu != 0 ? sizeof(RTATTRX_MTU) : 0}, |
| 446 | {&mtu, mtu != 0 ? sizeof(mtu) : 0}, |
| 447 | {&RTATTR_PRIO, priority != 0 ? sizeof(RTATTR_PRIO) : 0}, |
| 448 | {&priority, priority != 0 ? sizeof(priority) : 0}, |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 449 | }; |
| Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 450 | |
| Jimmy Chen | ce9e578 | 2019-03-08 16:12:55 +0800 | [diff] [blame] | 451 | // Allow creating multiple link-local routes in the same table, so we can make IPv6 |
| 452 | // work on all interfaces in the local_network table. |
| 453 | if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) { |
| 454 | flags &= ~NLM_F_EXCL; |
| 455 | } |
| 456 | |
| Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 457 | int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr); |
| Lorenzo Colitti | 0b073fb | 2017-02-10 07:49:12 +0900 | [diff] [blame] | 458 | if (ret) { |
| 459 | ALOGE("Error %s route %s -> %s %s to table %u: %s", |
| 460 | actionName(action), destination, nexthop, interface, table, strerror(-ret)); |
| 461 | } |
| 462 | return ret; |
| Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 465 | // An iptables rule to mark incoming packets on a network with the netId of the network. |
| 466 | // |
| 467 | // This is so that the kernel can: |
| 468 | // + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping |
| 469 | // replies, SYN-ACKs, etc). |
| 470 | // + Mark sockets that accept connections from this interface so that the connection stays on the |
| 471 | // same interface. |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 472 | int modifyIncomingPacketMark(unsigned netId, const char* interface, Permission permission, |
| 473 | bool add) { |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 474 | Fwmark fwmark; |
| 475 | |
| 476 | fwmark.netId = netId; |
| 477 | fwmark.explicitlySelected = true; |
| 478 | fwmark.protectedFromVpn = true; |
| 479 | fwmark.permission = permission; |
| 480 | |
| Benedict Wong | b9baf26 | 2017-12-03 15:43:08 -0800 | [diff] [blame] | 481 | const uint32_t mask = ~Fwmark::getUidBillingMask(); |
| 482 | |
| 483 | std::string cmd = StringPrintf( |
| 484 | "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D", |
| 485 | RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask); |
| Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 486 | if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) { |
| Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 487 | ALOGE("failed to change iptables rule that sets incoming packet mark"); |
| 488 | return -EREMOTEIO; |
| 489 | } |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 490 | |
| Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
| Sreeram Ramachandran | 111bec2 | 2014-07-22 18:51:06 -0700 | [diff] [blame] | 494 | // A rule to route responses to the local network forwarded via the VPN. |
| 495 | // |
| 496 | // When a VPN is in effect, packets from the local network to upstream networks are forwarded into |
| 497 | // the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel. |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 498 | [[nodiscard]] static int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) { |
| Sreeram Ramachandran | 111bec2 | 2014-07-22 18:51:06 -0700 | [diff] [blame] | 499 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL, |
| 500 | ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE, |
| 501 | INVALID_UID, INVALID_UID); |
| 502 | } |
| 503 | |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 504 | // A rule to route all traffic from a given set of UIDs to go over the VPN. |
| 505 | // |
| 506 | // Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may |
| 507 | // have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to |
| 508 | // bypass the VPN if the protectedFromVpn bit is set. |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 509 | [[nodiscard]] static int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd, |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 510 | int32_t subPriority, bool secure, bool add, |
| 511 | bool excludeLocalRoutes) { |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 512 | Fwmark fwmark; |
| 513 | Fwmark mask; |
| 514 | |
| 515 | fwmark.protectedFromVpn = false; |
| 516 | mask.protectedFromVpn = true; |
| 517 | |
| Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 518 | int32_t priority; |
| Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 519 | |
| 520 | if (secure) { |
| 521 | priority = RULE_PRIORITY_SECURE_VPN; |
| 522 | } else { |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 523 | priority = excludeLocalRoutes ? RULE_PRIORITY_BYPASSABLE_VPN_LOCAL_EXCLUSION |
| 524 | : RULE_PRIORITY_BYPASSABLE_VPN_NO_LOCAL_EXCLUSION; |
| Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 525 | |
| 526 | fwmark.explicitlySelected = false; |
| 527 | mask.explicitlySelected = true; |
| 528 | } |
| 529 | |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 530 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority + subPriority, table, |
| 531 | fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd); |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | // A rule to allow system apps to send traffic over this VPN even if they are not part of the target |
| 535 | // set of UIDs. |
| 536 | // |
| 537 | // This is needed for DnsProxyListener to correctly resolve a request for a user who is in the |
| 538 | // target set, but where the DnsProxyListener itself is not. |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 539 | [[nodiscard]] static int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure, |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 540 | bool add, bool excludeLocalRoutes) { |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 541 | Fwmark fwmark; |
| 542 | Fwmark mask; |
| 543 | |
| 544 | fwmark.netId = netId; |
| 545 | mask.netId = FWMARK_NET_ID_MASK; |
| 546 | |
| 547 | fwmark.permission = PERMISSION_SYSTEM; |
| 548 | mask.permission = PERMISSION_SYSTEM; |
| 549 | |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 550 | uint32_t priority; |
| 551 | |
| 552 | if (secure) { |
| 553 | priority = RULE_PRIORITY_SECURE_VPN; |
| 554 | } else { |
| 555 | priority = excludeLocalRoutes ? RULE_PRIORITY_BYPASSABLE_VPN_LOCAL_EXCLUSION |
| 556 | : RULE_PRIORITY_BYPASSABLE_VPN_NO_LOCAL_EXCLUSION; |
| 557 | } |
| Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 558 | |
| 559 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue, |
| 560 | mask.intValue); |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 563 | // A rule to route traffic based on an explicitly chosen network. |
| 564 | // |
| 565 | // Supports apps that use the multinetwork APIs to restrict their traffic to a network. |
| 566 | // |
| 567 | // Even though we check permissions at the time we set a netId into the fwmark of a socket, we need |
| 568 | // to check it again in the rules here, because a network's permissions may have been updated via |
| 569 | // modifyNetworkPermission(). |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 570 | [[nodiscard]] static int modifyExplicitNetworkRule(unsigned netId, uint32_t table, |
| 571 | Permission permission, uid_t uidStart, |
| Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 572 | uid_t uidEnd, int32_t subPriority, bool add) { |
| Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 573 | Fwmark fwmark; |
| 574 | Fwmark mask; |
| 575 | |
| 576 | fwmark.netId = netId; |
| 577 | mask.netId = FWMARK_NET_ID_MASK; |
| 578 | |
| Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 579 | fwmark.explicitlySelected = true; |
| 580 | mask.explicitlySelected = true; |
| Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 581 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 582 | fwmark.permission = permission; |
| 583 | mask.permission = permission; |
| Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 584 | |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 585 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, |
| 586 | RULE_PRIORITY_EXPLICIT_NETWORK + subPriority, table, fwmark.intValue, |
| 587 | mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd); |
| Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 590 | // A rule to route traffic based on a chosen outgoing interface. |
| 591 | // |
| 592 | // Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows |
| 593 | // the outgoing interface (typically for link-local communications). |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 594 | [[nodiscard]] static int modifyOutputInterfaceRules(const char* interface, uint32_t table, |
| 595 | Permission permission, uid_t uidStart, |
| Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 596 | uid_t uidEnd, int32_t subPriority, bool add) { |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 597 | Fwmark fwmark; |
| 598 | Fwmark mask; |
| Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 599 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 600 | fwmark.permission = permission; |
| 601 | mask.permission = permission; |
| Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 602 | |
| Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 603 | // If this rule does not specify a UID range, then also add a corresponding high-priority rule |
| Lorenzo Colitti | 758627c | 2018-03-15 01:49:20 +0900 | [diff] [blame] | 604 | // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open |
| 605 | // sockets as root. |
| Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 606 | if (uidStart == INVALID_UID && uidEnd == INVALID_UID) { |
| 607 | if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF, |
| Lorenzo Colitti | 758627c | 2018-03-15 01:49:20 +0900 | [diff] [blame] | 608 | table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface, |
| Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 609 | UID_ROOT, UID_ROOT)) { |
| 610 | return ret; |
| 611 | } |
| 612 | } |
| 613 | |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 614 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, |
| 615 | RULE_PRIORITY_OUTPUT_INTERFACE + subPriority, table, fwmark.intValue, |
| 616 | mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd); |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | // A rule to route traffic based on the chosen network. |
| 620 | // |
| 621 | // This is for sockets that have not explicitly requested a particular network, but have been |
| 622 | // bound to one when they called connect(). This ensures that sockets connected on a particular |
| 623 | // network stay on that network even if the default network changes. |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 624 | [[nodiscard]] static int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) { |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 625 | Fwmark fwmark; |
| 626 | Fwmark mask; |
| 627 | |
| 628 | fwmark.netId = netId; |
| 629 | mask.netId = FWMARK_NET_ID_MASK; |
| 630 | |
| 631 | fwmark.explicitlySelected = false; |
| 632 | mask.explicitlySelected = true; |
| 633 | |
| Lorenzo Colitti | be0c7c3 | 2017-09-06 16:07:02 +0900 | [diff] [blame] | 634 | fwmark.permission = PERMISSION_NONE; |
| 635 | mask.permission = PERMISSION_NONE; |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 636 | |
| 637 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table, |
| Lorenzo Colitti | 758627c | 2018-03-15 01:49:20 +0900 | [diff] [blame] | 638 | fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, |
| 639 | INVALID_UID); |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| Chiachang Wang | 8b9cdd2 | 2022-01-27 10:43:28 +0800 | [diff] [blame^] | 642 | int RouteController::modifyVpnLocalExclusionRule(bool add, const char* physicalInterface) { |
| Chiachang Wang | e7bf5f5 | 2022-01-17 11:37:39 +0800 | [diff] [blame] | 643 | uint32_t table = getRouteTableForInterface(physicalInterface, true /* local */); |
| 644 | if (table == RT_TABLE_UNSPEC) { |
| 645 | return -ESRCH; |
| 646 | } |
| 647 | |
| 648 | Fwmark fwmark; |
| 649 | Fwmark mask; |
| 650 | |
| 651 | fwmark.explicitlySelected = false; |
| 652 | mask.explicitlySelected = true; |
| 653 | |
| 654 | fwmark.permission = PERMISSION_NONE; |
| 655 | mask.permission = PERMISSION_NONE; |
| 656 | |
| Chiachang Wang | 8b9cdd2 | 2022-01-27 10:43:28 +0800 | [diff] [blame^] | 657 | if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_LOCAL_ROUTES, table, |
| 658 | fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, |
| 659 | INVALID_UID)) { |
| 660 | return ret; |
| 661 | } |
| 662 | return modifyVpnLocalExclusionRoutes(add, physicalInterface); |
| 663 | } |
| 664 | |
| 665 | // TODO: Update the local exclusion routes based on what actual subnet the network is. |
| 666 | int RouteController::modifyVpnLocalExclusionRoutes(bool add, const char* interface) { |
| 667 | for (size_t i = 0; i < ARRAY_SIZE(LOCAL_EXCLUSION_ROUTES_V4); ++i) { |
| 668 | if (int ret = modifyVpnLocalExclusionRoute(add, interface, LOCAL_EXCLUSION_ROUTES_V4[i])) { |
| 669 | // Routes are updated regardless of subnet. The destinations may be unreachable and |
| 670 | // cause EACCES error. This may happen now so ignore to not to cause an error. |
| 671 | if (ret != -EACCES) { |
| 672 | return ret; |
| 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | for (size_t i = 0; i < ARRAY_SIZE(LOCAL_EXCLUSION_ROUTES_V6); ++i) { |
| 678 | if (int ret = modifyVpnLocalExclusionRoute(add, interface, LOCAL_EXCLUSION_ROUTES_V6[i])) { |
| 679 | // Routes are updated regardless of subnet. The destinations may be unreachable and |
| 680 | // cause EACCES error. This may happen now so ignore to not to cause an error. |
| 681 | if (ret != -EACCES) { |
| 682 | return ret; |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | int RouteController::modifyVpnLocalExclusionRoute(bool add, const char* interface, |
| 690 | const char* destination) { |
| 691 | uint32_t table = getRouteTableForInterface(interface, true /* local */); |
| 692 | if (table == RT_TABLE_UNSPEC) { |
| 693 | return -ESRCH; |
| 694 | } |
| 695 | |
| 696 | if (int ret = modifyIpRoute(add ? RTM_NEWROUTE : RTM_DELROUTE, |
| 697 | add ? NETLINK_ROUTE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS, table, |
| 698 | interface, destination, nullptr, 0 /* mtu */, 0 /* priority */)) { |
| 699 | // Trying to delete a route that already deleted shouldn't cause an error. |
| 700 | if (add || ret != -ESRCH) { |
| 701 | return ret; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | return 0; |
| Chiachang Wang | e7bf5f5 | 2022-01-17 11:37:39 +0800 | [diff] [blame] | 706 | } |
| 707 | |
| Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 708 | // A rule to enable split tunnel VPNs. |
| 709 | // |
| 710 | // If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to |
| Luke Huang | d286198 | 2019-05-17 19:47:28 +0800 | [diff] [blame] | 711 | // go over the default network, provided it has the permissions required by the default network. |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 712 | int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId, |
| 713 | const char* physicalInterface, |
| 714 | Permission permission) { |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 715 | uint32_t table = getRouteTableForInterface(physicalInterface, false /* local */); |
| Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 716 | if (table == RT_TABLE_UNSPEC) { |
| 717 | return -ESRCH; |
| 718 | } |
| 719 | |
| 720 | Fwmark fwmark; |
| 721 | Fwmark mask; |
| 722 | |
| 723 | fwmark.netId = vpnNetId; |
| 724 | mask.netId = FWMARK_NET_ID_MASK; |
| 725 | |
| Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 726 | fwmark.permission = permission; |
| 727 | mask.permission = permission; |
| 728 | |
| 729 | return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue, |
| 730 | mask.intValue); |
| 731 | } |
| 732 | |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 733 | // Add rules to allow legacy routes added through the requestRouteToHost() API. |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 734 | [[nodiscard]] static int addLegacyRouteRules() { |
| Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 735 | Fwmark fwmark; |
| 736 | Fwmark mask; |
| 737 | |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 738 | fwmark.explicitlySelected = false; |
| 739 | mask.explicitlySelected = true; |
| Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 740 | |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 741 | // Rules to allow legacy routes to override the default network. |
| 742 | if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM, |
| 743 | fwmark.intValue, mask.intValue)) { |
| 744 | return ret; |
| 745 | } |
| 746 | if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK, |
| 747 | ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) { |
| 748 | return ret; |
| 749 | } |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 750 | |
| 751 | fwmark.permission = PERMISSION_SYSTEM; |
| 752 | mask.permission = PERMISSION_SYSTEM; |
| 753 | |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 754 | // A rule to allow legacy routes from system apps to override VPNs. |
| 755 | return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM, |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 756 | fwmark.intValue, mask.intValue); |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 757 | } |
| 758 | |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 759 | // Add rules to lookup the local network when specified explicitly or otherwise. |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 760 | [[nodiscard]] static int addLocalNetworkRules(unsigned localNetId) { |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 761 | if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 762 | INVALID_UID, INVALID_UID, |
| 763 | UidRanges::DEFAULT_SUB_PRIORITY, ACTION_ADD)) { |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 764 | return ret; |
| Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 765 | } |
| 766 | |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 767 | Fwmark fwmark; |
| 768 | Fwmark mask; |
| 769 | |
| 770 | fwmark.explicitlySelected = false; |
| 771 | mask.explicitlySelected = true; |
| 772 | |
| 773 | return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK, |
| 774 | fwmark.intValue, mask.intValue); |
| 775 | } |
| 776 | |
| Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 777 | /* static */ |
| 778 | int RouteController::configureDummyNetwork() { |
| Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 779 | const char *interface = DummyNetwork::INTERFACE_NAME; |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 780 | uint32_t table = getRouteTableForInterface(interface, false /* local */); |
| Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 781 | if (table == RT_TABLE_UNSPEC) { |
| Hungming Chen | 7f72543 | 2020-02-07 17:47:23 +0800 | [diff] [blame] | 782 | // getRouteTableForInterface has already logged an error. |
| Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 783 | return -ESRCH; |
| 784 | } |
| 785 | |
| 786 | ifc_init(); |
| 787 | int ret = ifc_up(interface); |
| 788 | ifc_close(); |
| 789 | if (ret) { |
| 790 | ALOGE("Can't bring up %s: %s", interface, strerror(errno)); |
| 791 | return -errno; |
| 792 | } |
| 793 | |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 794 | if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, INVALID_UID, |
| 795 | INVALID_UID, UidRanges::DEFAULT_SUB_PRIORITY, |
| 796 | ACTION_ADD))) { |
| Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 797 | ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret)); |
| Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 798 | return ret; |
| 799 | } |
| 800 | |
| Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 801 | if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface, |
| Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 802 | "0.0.0.0/0", nullptr, 0 /* mtu */, 0 /* priority */))) { |
| Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 803 | return ret; |
| 804 | } |
| 805 | |
| Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 806 | if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface, "::/0", |
| Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 807 | nullptr, 0 /* mtu */, 0 /* priority */))) { |
| Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 808 | return ret; |
| 809 | } |
| 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
| Lorenzo Colitti | db74dba | 2014-07-29 18:26:21 +0900 | [diff] [blame] | 814 | // Add an explicit unreachable rule close to the end of the prioriy list to make it clear that |
| 815 | // relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended |
| 816 | // behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable |
| 817 | // rule will hopefully make things even clearer. |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 818 | [[nodiscard]] static int addUnreachableRule() { |
| Robin Lee | c125fe4 | 2016-05-02 08:53:34 +0100 | [diff] [blame] | 819 | return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC, |
| 820 | MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID); |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 823 | [[nodiscard]] static int modifyLocalNetwork(unsigned netId, const char* interface, bool add) { |
| Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 824 | if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) { |
| 825 | return ret; |
| 826 | } |
| Lorenzo Colitti | 3810c97 | 2021-01-06 11:44:02 +0900 | [diff] [blame] | 827 | maybeModifyQdiscClsact(interface, add); |
| Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 828 | return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 829 | INVALID_UID, INVALID_UID, UidRanges::DEFAULT_SUB_PRIORITY, |
| 830 | add); |
| Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 831 | } |
| 832 | |
| Ken Chen | a2206ec | 2021-03-24 17:15:44 +0800 | [diff] [blame] | 833 | [[nodiscard]] static int modifyUidNetworkRule(unsigned netId, uint32_t table, uid_t uidStart, |
| Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 834 | uid_t uidEnd, int32_t subPriority, bool add, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 835 | bool explicitSelect) { |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 836 | if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) { |
| Ken Chen | a2206ec | 2021-03-24 17:15:44 +0800 | [diff] [blame] | 837 | ALOGE("modifyUidNetworkRule, invalid UIDs (%u, %u)", uidStart, uidEnd); |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 838 | return -EUSERS; |
| 839 | } |
| 840 | |
| 841 | Fwmark fwmark; |
| 842 | Fwmark mask; |
| 843 | |
| 844 | fwmark.netId = netId; |
| 845 | mask.netId = FWMARK_NET_ID_MASK; |
| 846 | |
| Ken Chen | a2206ec | 2021-03-24 17:15:44 +0800 | [diff] [blame] | 847 | fwmark.explicitlySelected = explicitSelect; |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 848 | mask.explicitlySelected = true; |
| 849 | |
| 850 | // Access to this network is controlled by UID rules, not permission bits. |
| 851 | fwmark.permission = PERMISSION_NONE; |
| 852 | mask.permission = PERMISSION_NONE; |
| 853 | |
| Ken Chen | a2206ec | 2021-03-24 17:15:44 +0800 | [diff] [blame] | 854 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 855 | explicitSelect ? (RULE_PRIORITY_UID_EXPLICIT_NETWORK + subPriority) |
| 856 | : (RULE_PRIORITY_UID_IMPLICIT_NETWORK + subPriority), |
| Ken Chen | a2206ec | 2021-03-24 17:15:44 +0800 | [diff] [blame] | 857 | table, fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, |
| 858 | uidEnd); |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | [[nodiscard]] static int modifyUidDefaultNetworkRule(uint32_t table, uid_t uidStart, uid_t uidEnd, |
| Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 862 | int32_t subPriority, bool add) { |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 863 | if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) { |
| 864 | ALOGE("modifyUidDefaultNetworkRule, invalid UIDs (%u, %u)", uidStart, uidEnd); |
| 865 | return -EUSERS; |
| 866 | } |
| 867 | |
| 868 | Fwmark fwmark; |
| 869 | Fwmark mask; |
| 870 | |
| 871 | fwmark.netId = NETID_UNSET; |
| 872 | mask.netId = FWMARK_NET_ID_MASK; |
| 873 | |
| 874 | // Access to this network is controlled by UID rules, not permission bits. |
| 875 | fwmark.permission = PERMISSION_NONE; |
| 876 | mask.permission = PERMISSION_NONE; |
| 877 | |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 878 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, |
| 879 | RULE_PRIORITY_UID_DEFAULT_NETWORK + subPriority, table, fwmark.intValue, |
| 880 | mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd); |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 881 | } |
| 882 | |
| Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 883 | /* static */ |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 884 | int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 885 | const UidRangeMap& uidRangeMap, Permission permission, |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 886 | bool add, bool modifyNonUidBasedRules) { |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 887 | uint32_t table = getRouteTableForInterface(interface, false /* local */); |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 888 | if (table == RT_TABLE_UNSPEC) { |
| 889 | return -ESRCH; |
| 890 | } |
| 891 | |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 892 | for (const auto& [subPriority, uidRanges] : uidRangeMap) { |
| 893 | for (const UidRangeParcel& range : uidRanges.getRanges()) { |
| 894 | if (int ret = modifyUidNetworkRule(netId, table, range.start, range.stop, subPriority, |
| 895 | add, EXPLICIT)) { |
| 896 | return ret; |
| 897 | } |
| 898 | if (int ret = modifyUidNetworkRule(netId, table, range.start, range.stop, subPriority, |
| 899 | add, IMPLICIT)) { |
| 900 | return ret; |
| 901 | } |
| 902 | if (int ret = modifyUidDefaultNetworkRule(table, range.start, range.stop, subPriority, |
| 903 | add)) { |
| 904 | return ret; |
| 905 | } |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
| 909 | if (!modifyNonUidBasedRules) { |
| 910 | // we are done. |
| 911 | return 0; |
| 912 | } |
| 913 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 914 | if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) { |
| 915 | return ret; |
| 916 | } |
| 917 | if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 918 | UidRanges::DEFAULT_SUB_PRIORITY, add)) { |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 919 | return ret; |
| 920 | } |
| Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 921 | if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 922 | UidRanges::DEFAULT_SUB_PRIORITY, add)) { |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 923 | return ret; |
| 924 | } |
| Lorenzo Colitti | be0c7c3 | 2017-09-06 16:07:02 +0900 | [diff] [blame] | 925 | |
| 926 | // Only set implicit rules for networks that don't require permissions. |
| 927 | // |
| 928 | // This is so that if the default network ceases to be the default network and then switches |
| 929 | // from requiring no permissions to requiring permissions, we ensure that apps only use the |
| 930 | // network if they explicitly select it. This is consistent with destroySocketsLackingPermission |
| 931 | // - it closes all sockets on the network except sockets that are explicitly selected. |
| 932 | // |
| 933 | // The lack of this rule only affects the special case above, because: |
| 934 | // - The only cases where we implicitly bind a socket to a network are the default network and |
| 935 | // the bypassable VPN that applies to the app, if any. |
| 936 | // - This rule doesn't affect VPNs because they don't support permissions at all. |
| 937 | // - The default network doesn't require permissions. While we support doing this, the framework |
| 938 | // never does it (partly because we'd end up in the situation where we tell apps that there is |
| 939 | // a default network, but they can't use it). |
| 940 | // - If the network is still the default network, the presence or absence of this rule does not |
| 941 | // matter. |
| 942 | // |
| 943 | // Therefore, for the lack of this rule to affect a socket, the socket has to have been |
| 944 | // implicitly bound to a network because at the time of connect() it was the default, and that |
| 945 | // network must no longer be the default, and must now require permissions. |
| 946 | if (permission == PERMISSION_NONE) { |
| 947 | return modifyImplicitNetworkRule(netId, table, add); |
| 948 | } |
| 949 | return 0; |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 950 | } |
| 951 | |
| Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 952 | [[nodiscard]] static int modifyUidUnreachableRule(unsigned netId, uid_t uidStart, uid_t uidEnd, |
| Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 953 | int32_t subPriority, bool add, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 954 | bool explicitSelect) { |
| Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 955 | if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) { |
| 956 | ALOGE("modifyUidUnreachableRule, invalid UIDs (%u, %u)", uidStart, uidEnd); |
| 957 | return -EUSERS; |
| 958 | } |
| 959 | |
| 960 | Fwmark fwmark; |
| 961 | Fwmark mask; |
| 962 | |
| 963 | fwmark.netId = netId; |
| 964 | mask.netId = FWMARK_NET_ID_MASK; |
| 965 | |
| 966 | fwmark.explicitlySelected = explicitSelect; |
| 967 | mask.explicitlySelected = true; |
| 968 | |
| 969 | // Access to this network is controlled by UID rules, not permission bits. |
| 970 | fwmark.permission = PERMISSION_NONE; |
| 971 | mask.permission = PERMISSION_NONE; |
| 972 | |
| 973 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 974 | explicitSelect ? (RULE_PRIORITY_UID_EXPLICIT_NETWORK + subPriority) |
| 975 | : (RULE_PRIORITY_UID_IMPLICIT_NETWORK + subPriority), |
| Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 976 | FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue, |
| 977 | IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd); |
| 978 | } |
| 979 | |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 980 | [[nodiscard]] static int modifyUidDefaultUnreachableRule(uid_t uidStart, uid_t uidEnd, |
| Ken Chen | 53360bf | 2021-12-10 02:41:05 +0800 | [diff] [blame] | 981 | int32_t subPriority, bool add) { |
| Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 982 | if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) { |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 983 | ALOGE("modifyUidDefaultUnreachableRule, invalid UIDs (%u, %u)", uidStart, uidEnd); |
| Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 984 | return -EUSERS; |
| 985 | } |
| 986 | |
| 987 | Fwmark fwmark; |
| 988 | Fwmark mask; |
| 989 | |
| 990 | fwmark.netId = NETID_UNSET; |
| 991 | mask.netId = FWMARK_NET_ID_MASK; |
| 992 | |
| 993 | // Access to this network is controlled by UID rules, not permission bits. |
| 994 | fwmark.permission = PERMISSION_NONE; |
| 995 | mask.permission = PERMISSION_NONE; |
| 996 | |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 997 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, |
| 998 | RULE_PRIORITY_UID_DEFAULT_UNREACHABLE + subPriority, FR_ACT_UNREACHABLE, |
| 999 | RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, |
| 1000 | uidStart, uidEnd); |
| Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 1001 | } |
| 1002 | |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1003 | int RouteController::modifyUnreachableNetwork(unsigned netId, const UidRangeMap& uidRangeMap, |
| Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 1004 | bool add) { |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1005 | for (const auto& [subPriority, uidRanges] : uidRangeMap) { |
| 1006 | for (const UidRangeParcel& range : uidRanges.getRanges()) { |
| 1007 | if (int ret = modifyUidUnreachableRule(netId, range.start, range.stop, subPriority, add, |
| 1008 | EXPLICIT)) { |
| 1009 | return ret; |
| 1010 | } |
| 1011 | if (int ret = modifyUidUnreachableRule(netId, range.start, range.stop, subPriority, add, |
| 1012 | IMPLICIT)) { |
| 1013 | return ret; |
| 1014 | } |
| 1015 | if (int ret = modifyUidDefaultUnreachableRule(range.start, range.stop, subPriority, |
| 1016 | add)) { |
| 1017 | return ret; |
| 1018 | } |
| Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 1025 | [[nodiscard]] static int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) { |
| Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 1026 | Fwmark fwmark; |
| 1027 | Fwmark mask; |
| 1028 | fwmark.protectedFromVpn = false; |
| 1029 | mask.protectedFromVpn = true; |
| 1030 | |
| Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 1031 | for (const UidRangeParcel& range : uidRanges.getRanges()) { |
| 1032 | if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN, |
| 1033 | FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue, |
| 1034 | IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) { |
| Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 1035 | return ret; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 1042 | int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1043 | const UidRangeMap& uidRangeMap, bool secure, bool add, |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 1044 | bool modifyNonUidBasedRules, bool excludeLocalRoutes) { |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 1045 | uint32_t table = getRouteTableForInterface(interface, false /* false */); |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1046 | if (table == RT_TABLE_UNSPEC) { |
| 1047 | return -ESRCH; |
| 1048 | } |
| 1049 | |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1050 | for (const auto& [subPriority, uidRanges] : uidRangeMap) { |
| 1051 | for (const UidRangeParcel& range : uidRanges.getRanges()) { |
| 1052 | if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, subPriority, secure, |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 1053 | add, excludeLocalRoutes)) { |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1054 | return ret; |
| 1055 | } |
| 1056 | if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start, |
| 1057 | range.stop, subPriority, add)) { |
| 1058 | return ret; |
| 1059 | } |
| 1060 | if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start, |
| 1061 | range.stop, subPriority, add)) { |
| 1062 | return ret; |
| 1063 | } |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1064 | } |
| Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1067 | if (modifyNonUidBasedRules) { |
| 1068 | if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) { |
| Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 1069 | return ret; |
| 1070 | } |
| Sreeram Ramachandran | 111bec2 | 2014-07-22 18:51:06 -0700 | [diff] [blame] | 1071 | if (int ret = modifyVpnOutputToLocalRule(interface, add)) { |
| 1072 | return ret; |
| 1073 | } |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 1074 | if (int ret = |
| 1075 | modifyVpnSystemPermissionRule(netId, table, secure, add, excludeLocalRoutes)) { |
| Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 1076 | return ret; |
| 1077 | } |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1078 | return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, |
| 1079 | UidRanges::DEFAULT_SUB_PRIORITY, add); |
| Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | return 0; |
| Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 1085 | int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface, |
| 1086 | Permission permission) { |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 1087 | uint32_t table = getRouteTableForInterface(interface, false /* local */); |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1088 | if (table == RT_TABLE_UNSPEC) { |
| Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 1089 | return -ESRCH; |
| Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
| Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 1092 | Fwmark fwmark; |
| Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 1093 | Fwmark mask; |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 1094 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1095 | fwmark.netId = NETID_UNSET; |
| Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 1096 | mask.netId = FWMARK_NET_ID_MASK; |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 1097 | |
| 1098 | fwmark.permission = permission; |
| Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 1099 | mask.permission = permission; |
| 1100 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1101 | return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue, |
| Lorenzo Colitti | 758627c | 2018-03-15 01:49:20 +0900 | [diff] [blame] | 1102 | mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID); |
| Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 1105 | int RouteController::modifyTetheredNetwork(uint16_t action, const char* inputInterface, |
| 1106 | const char* outputInterface) { |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 1107 | uint32_t table = getRouteTableForInterface(outputInterface, false /* local */); |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 1108 | if (table == RT_TABLE_UNSPEC) { |
| 1109 | return -ESRCH; |
| 1110 | } |
| 1111 | |
| 1112 | return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET, |
| 1113 | inputInterface, OIF_NONE, INVALID_UID, INVALID_UID); |
| 1114 | } |
| 1115 | |
| Lorenzo Colitti | 92e8f96 | 2017-09-26 19:13:50 +0900 | [diff] [blame] | 1116 | // Adds or removes an IPv4 or IPv6 route to the specified table. |
| Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 1117 | // Returns 0 on success or negative errno on failure. |
| Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 1118 | int RouteController::modifyRoute(uint16_t action, uint16_t flags, const char* interface, |
| 1119 | const char* destination, const char* nexthop, TableType tableType, |
| Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 1120 | int mtu, int priority) { |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1121 | uint32_t table; |
| Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 1122 | switch (tableType) { |
| 1123 | case RouteController::INTERFACE: { |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 1124 | table = getRouteTableForInterface(interface, false /* local */); |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1125 | if (table == RT_TABLE_UNSPEC) { |
| 1126 | return -ESRCH; |
| 1127 | } |
| Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 1128 | break; |
| 1129 | } |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 1130 | case RouteController::LOCAL_NETWORK: { |
| 1131 | table = ROUTE_TABLE_LOCAL_NETWORK; |
| 1132 | break; |
| 1133 | } |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1134 | case RouteController::LEGACY_NETWORK: { |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1135 | table = ROUTE_TABLE_LEGACY_NETWORK; |
| Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 1136 | break; |
| 1137 | } |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1138 | case RouteController::LEGACY_SYSTEM: { |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1139 | table = ROUTE_TABLE_LEGACY_SYSTEM; |
| Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 1140 | break; |
| 1141 | } |
| 1142 | } |
| Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 1143 | |
| Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 1144 | int ret = modifyIpRoute(action, flags, table, interface, destination, nexthop, mtu, priority); |
| Sreeram Ramachandran | 0321315 | 2014-10-30 10:01:07 -0700 | [diff] [blame] | 1145 | // Trying to add a route that already exists shouldn't cause an error. |
| 1146 | if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) { |
| Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 1147 | return ret; |
| Sreeram Ramachandran | c921337 | 2014-04-16 12:32:18 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
| Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 1150 | return 0; |
| Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| Lorenzo Colitti | 3810c97 | 2021-01-06 11:44:02 +0900 | [diff] [blame] | 1153 | static void maybeModifyQdiscClsact(const char* interface, bool add) { |
| Hungming Chen | 7f72543 | 2020-02-07 17:47:23 +0800 | [diff] [blame] | 1154 | // The clsact attaching of v4- tun interface is triggered by ClatdController::maybeStartBpf |
| 1155 | // because the clat is started before the v4- interface is added to the network and the |
| 1156 | // clat startup needs to add {in, e}gress filters. |
| 1157 | // TODO: remove this workaround once v4- tun interface clsact attaching is moved out from |
| 1158 | // ClatdController::maybeStartBpf. |
| 1159 | if (StartsWith(interface, "v4-") && add) return; |
| 1160 | |
| 1161 | // The interface may have already gone away in the delete case. |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 1162 | uint32_t ifindex = RouteController::ifNameToIndexFunction(interface); |
| Hungming Chen | 7f72543 | 2020-02-07 17:47:23 +0800 | [diff] [blame] | 1163 | if (!ifindex) { |
| 1164 | ALOGE("cannot find interface %s", interface); |
| 1165 | return; |
| 1166 | } |
| 1167 | |
| 1168 | if (add) { |
| 1169 | if (int ret = tcQdiscAddDevClsact(ifindex)) { |
| 1170 | ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", ifindex, interface, strerror(-ret)); |
| 1171 | return; |
| 1172 | } |
| 1173 | } else { |
| 1174 | if (int ret = tcQdiscDelDevClsact(ifindex)) { |
| 1175 | ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", ifindex, interface, strerror(-ret)); |
| 1176 | return; |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | return; |
| 1181 | } |
| 1182 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 1183 | [[nodiscard]] static int clearTetheringRules(const char* inputInterface) { |
| Lorenzo Colitti | 6b6f25f | 2015-03-03 17:22:57 +0900 | [diff] [blame] | 1184 | int ret = 0; |
| 1185 | while (ret == 0) { |
| 1186 | ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET, |
| 1187 | inputInterface, OIF_NONE, INVALID_UID, INVALID_UID); |
| 1188 | } |
| 1189 | |
| 1190 | if (ret == -ENOENT) { |
| 1191 | return 0; |
| 1192 | } else { |
| 1193 | return ret; |
| 1194 | } |
| 1195 | } |
| 1196 | |
| Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 1197 | uint32_t getRulePriority(const nlmsghdr *nlh) { |
| 1198 | return getRtmU32Attribute(nlh, FRA_PRIORITY); |
| 1199 | } |
| 1200 | |
| 1201 | uint32_t getRouteTable(const nlmsghdr *nlh) { |
| 1202 | return getRtmU32Attribute(nlh, RTA_TABLE); |
| 1203 | } |
| 1204 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 1205 | [[nodiscard]] static int flushRules() { |
| Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 1206 | NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) { |
| 1207 | // Don't touch rules at priority 0 because by default they are used for local input. |
| 1208 | return getRulePriority(nlh) != 0; |
| 1209 | }; |
| 1210 | return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete); |
| 1211 | } |
| 1212 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 1213 | int RouteController::flushRoutes(uint32_t table) { |
| Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 1214 | NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) { |
| 1215 | return getRouteTable(nlh) == table; |
| 1216 | }; |
| 1217 | |
| 1218 | return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete); |
| 1219 | } |
| 1220 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 1221 | int RouteController::flushRoutes(const char* interface) { |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 1222 | // Try to flush both local and global routing tables. |
| 1223 | // |
| 1224 | // Flush local first because flush global routing tables may erase the sInterfaceToTable map. |
| 1225 | // Then the fake <iface>_local interface will be unable to find the index because the local |
| 1226 | // interface depends physical interface to find the correct index. |
| 1227 | int ret = flushRoutes(interface, true); |
| 1228 | ret |= flushRoutes(interface, false); |
| 1229 | return ret; |
| 1230 | } |
| 1231 | |
| 1232 | // Returns 0 on success or negative errno on failure. |
| 1233 | int RouteController::flushRoutes(const char* interface, bool local) { |
| Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 1234 | std::lock_guard lock(sInterfaceToTableLock); |
| Lorenzo Colitti | 107075a | 2017-10-30 19:24:46 +0900 | [diff] [blame] | 1235 | |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 1236 | uint32_t table = getRouteTableForInterfaceLocked(interface, local); |
| Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 1237 | if (table == RT_TABLE_UNSPEC) { |
| 1238 | return -ESRCH; |
| 1239 | } |
| 1240 | |
| 1241 | int ret = flushRoutes(table); |
| 1242 | |
| 1243 | // If we failed to flush routes, the caller may elect to keep this interface around, so keep |
| 1244 | // track of its name. |
| Chiachang Wang | f79e356 | 2022-01-15 13:31:04 +0800 | [diff] [blame] | 1245 | // Skip erasing local fake interface since it does not exist in sInterfaceToTable. |
| 1246 | if (ret == 0 && !local) { |
| Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 1247 | sInterfaceToTable.erase(interface); |
| Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | return ret; |
| 1251 | } |
| 1252 | |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 1253 | int RouteController::Init(unsigned localNetId) { |
| Sreeram Ramachandran | b717e74 | 2014-07-19 00:22:15 -0700 | [diff] [blame] | 1254 | if (int ret = flushRules()) { |
| 1255 | return ret; |
| 1256 | } |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 1257 | if (int ret = addLegacyRouteRules()) { |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 1258 | return ret; |
| 1259 | } |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 1260 | if (int ret = addLocalNetworkRules(localNetId)) { |
| 1261 | return ret; |
| 1262 | } |
| Sreeram Ramachandran | b717e74 | 2014-07-19 00:22:15 -0700 | [diff] [blame] | 1263 | if (int ret = addUnreachableRule()) { |
| 1264 | return ret; |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 1265 | } |
| Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 1266 | // Don't complain if we can't add the dummy network, since not all devices support it. |
| 1267 | configureDummyNetwork(); |
| 1268 | |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1269 | updateTableNamesFile(); |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 1270 | return 0; |
| Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
| Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 1273 | int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) { |
| Lorenzo Colitti | b6dc40a | 2020-03-24 00:58:50 +0900 | [diff] [blame] | 1274 | if (int ret = modifyLocalNetwork(netId, interface, ACTION_ADD)) { |
| 1275 | return ret; |
| 1276 | } |
| 1277 | std::lock_guard lock(sInterfaceToTableLock); |
| 1278 | sInterfaceToTable[interface] = ROUTE_TABLE_LOCAL_NETWORK; |
| 1279 | return 0; |
| Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) { |
| Lorenzo Colitti | b6dc40a | 2020-03-24 00:58:50 +0900 | [diff] [blame] | 1283 | if (int ret = modifyLocalNetwork(netId, interface, ACTION_DEL)) { |
| 1284 | return ret; |
| 1285 | } |
| 1286 | std::lock_guard lock(sInterfaceToTableLock); |
| 1287 | sInterfaceToTable.erase(interface); |
| 1288 | return 0; |
| Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1291 | int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface, |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 1292 | Permission permission, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1293 | const UidRangeMap& uidRangeMap) { |
| 1294 | if (int ret = modifyPhysicalNetwork(netId, interface, uidRangeMap, permission, ACTION_ADD, |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 1295 | MODIFY_NON_UID_BASED_RULES)) { |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1296 | return ret; |
| 1297 | } |
| Chiachang Wang | e7bf5f5 | 2022-01-17 11:37:39 +0800 | [diff] [blame] | 1298 | // TODO: Consider to remove regular table if adding local table failed. |
| Chiachang Wang | 8b9cdd2 | 2022-01-27 10:43:28 +0800 | [diff] [blame^] | 1299 | if (int ret = modifyVpnLocalExclusionRule(true, interface)) { |
| Chiachang Wang | e7bf5f5 | 2022-01-17 11:37:39 +0800 | [diff] [blame] | 1300 | return ret; |
| 1301 | } |
| 1302 | |
| Hungming Chen | 7f72543 | 2020-02-07 17:47:23 +0800 | [diff] [blame] | 1303 | maybeModifyQdiscClsact(interface, ACTION_ADD); |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1304 | updateTableNamesFile(); |
| 1305 | return 0; |
| Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1308 | int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface, |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 1309 | Permission permission, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1310 | const UidRangeMap& uidRangeMap) { |
| 1311 | if (int ret = modifyPhysicalNetwork(netId, interface, uidRangeMap, permission, ACTION_DEL, |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 1312 | MODIFY_NON_UID_BASED_RULES)) { |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 1313 | return ret; |
| 1314 | } |
| Chiachang Wang | e7bf5f5 | 2022-01-17 11:37:39 +0800 | [diff] [blame] | 1315 | |
| Chiachang Wang | 8b9cdd2 | 2022-01-27 10:43:28 +0800 | [diff] [blame^] | 1316 | int ret = modifyVpnLocalExclusionRule(false, interface); |
| 1317 | // Always perform flushRoute even if removing local exclusion rules failed. |
| 1318 | ret |= flushRoutes(interface); |
| 1319 | if (ret) { |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1320 | return ret; |
| 1321 | } |
| Chiachang Wang | e7bf5f5 | 2022-01-17 11:37:39 +0800 | [diff] [blame] | 1322 | |
| Lorenzo Colitti | 6b6f25f | 2015-03-03 17:22:57 +0900 | [diff] [blame] | 1323 | if (int ret = clearTetheringRules(interface)) { |
| 1324 | return ret; |
| 1325 | } |
| Chiachang Wang | e7bf5f5 | 2022-01-17 11:37:39 +0800 | [diff] [blame] | 1326 | |
| Hungming Chen | 7f72543 | 2020-02-07 17:47:23 +0800 | [diff] [blame] | 1327 | maybeModifyQdiscClsact(interface, ACTION_DEL); |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1328 | updateTableNamesFile(); |
| 1329 | return 0; |
| Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1332 | int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface, |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 1333 | bool secure, const UidRangeMap& uidRangeMap, |
| 1334 | bool excludeLocalRoutes) { |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1335 | if (int ret = modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_ADD, |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 1336 | MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes)) { |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1337 | return ret; |
| 1338 | } |
| 1339 | updateTableNamesFile(); |
| 1340 | return 0; |
| Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1343 | int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface, |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 1344 | bool secure, const UidRangeMap& uidRangeMap, |
| 1345 | bool excludeLocalRoutes) { |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1346 | if (int ret = modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_DEL, |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 1347 | MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes)) { |
| Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 1348 | return ret; |
| 1349 | } |
| Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1350 | if (int ret = flushRoutes(interface)) { |
| 1351 | return ret; |
| 1352 | } |
| 1353 | updateTableNamesFile(); |
| 1354 | return 0; |
| Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1357 | int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface, |
| 1358 | Permission oldPermission, |
| 1359 | Permission newPermission) { |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1360 | // Physical network rules either use permission bits or UIDs, but not both. |
| 1361 | // So permission changes don't affect any UID-based rules. |
| 1362 | UidRangeMap emptyUidRangeMap; |
| Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 1363 | // Add the new rules before deleting the old ones, to avoid race conditions. |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1364 | if (int ret = modifyPhysicalNetwork(netId, interface, emptyUidRangeMap, newPermission, |
| 1365 | ACTION_ADD, MODIFY_NON_UID_BASED_RULES)) { |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 1366 | return ret; |
| 1367 | } |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1368 | return modifyPhysicalNetwork(netId, interface, emptyUidRangeMap, oldPermission, ACTION_DEL, |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 1369 | MODIFY_NON_UID_BASED_RULES); |
| Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
| Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 1372 | int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) { |
| 1373 | return modifyRejectNonSecureNetworkRule(uidRanges, true); |
| 1374 | } |
| 1375 | |
| 1376 | int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) { |
| 1377 | return modifyRejectNonSecureNetworkRule(uidRanges, false); |
| 1378 | } |
| 1379 | |
| Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 1380 | int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure, |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 1381 | const UidRangeMap& uidRangeMap, |
| 1382 | bool excludeLocalRoutes) { |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1383 | return modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_ADD, |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 1384 | !MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes); |
| Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1387 | int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface, |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 1388 | bool secure, const UidRangeMap& uidRangeMap, |
| 1389 | bool excludeLocalRoutes) { |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1390 | return modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_DEL, |
| Chiachang Wang | 2e9443f | 2022-01-14 22:55:36 +0800 | [diff] [blame] | 1391 | !MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes); |
| Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1394 | int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) { |
| 1395 | return modifyDefaultNetwork(RTM_NEWRULE, interface, permission); |
| Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
| Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1398 | int RouteController::removeInterfaceFromDefaultNetwork(const char* interface, |
| 1399 | Permission permission) { |
| 1400 | return modifyDefaultNetwork(RTM_DELRULE, interface, permission); |
| Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 1401 | } |
| Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 1402 | |
| Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 1403 | int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop, |
| Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 1404 | TableType tableType, int mtu, int priority) { |
| Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 1405 | return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, interface, destination, nexthop, |
| Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 1406 | tableType, mtu, priority); |
| Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 1407 | } |
| 1408 | |
| Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 1409 | int RouteController::removeRoute(const char* interface, const char* destination, |
| Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 1410 | const char* nexthop, TableType tableType, int priority) { |
| Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 1411 | return modifyRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, interface, destination, nexthop, |
| Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 1412 | tableType, 0 /* mtu */, priority); |
| Tyler Wear | fa94a27 | 2019-12-05 15:01:48 -0800 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | int RouteController::updateRoute(const char* interface, const char* destination, |
| 1416 | const char* nexthop, TableType tableType, int mtu) { |
| 1417 | return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_REPLACE_FLAGS, interface, destination, nexthop, |
| Taras Antoshchuk | 0cceda2 | 2021-09-24 18:40:03 +0200 | [diff] [blame] | 1418 | tableType, mtu, 0 /* priority */); |
| Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 1419 | } |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 1420 | |
| 1421 | int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) { |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 1422 | return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface); |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) { |
| Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 1426 | return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface); |
| Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 1427 | } |
| Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 1428 | |
| 1429 | int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface, |
| 1430 | Permission permission) { |
| 1431 | return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission); |
| 1432 | } |
| 1433 | |
| 1434 | int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId, |
| 1435 | const char* physicalInterface, |
| 1436 | Permission permission) { |
| 1437 | return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission); |
| 1438 | } |
| Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 1439 | |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 1440 | int RouteController::addUsersToPhysicalNetwork(unsigned netId, const char* interface, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1441 | const UidRangeMap& uidRangeMap) { |
| 1442 | return modifyPhysicalNetwork(netId, interface, uidRangeMap, PERMISSION_NONE, ACTION_ADD, |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 1443 | !MODIFY_NON_UID_BASED_RULES); |
| 1444 | } |
| 1445 | |
| 1446 | int RouteController::removeUsersFromPhysicalNetwork(unsigned netId, const char* interface, |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1447 | const UidRangeMap& uidRangeMap) { |
| 1448 | return modifyPhysicalNetwork(netId, interface, uidRangeMap, PERMISSION_NONE, ACTION_DEL, |
| Ken Chen | 8738e1c | 2020-11-24 11:38:54 +0800 | [diff] [blame] | 1449 | !MODIFY_NON_UID_BASED_RULES); |
| 1450 | } |
| 1451 | |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1452 | int RouteController::addUsersToUnreachableNetwork(unsigned netId, const UidRangeMap& uidRangeMap) { |
| 1453 | return modifyUnreachableNetwork(netId, uidRangeMap, ACTION_ADD); |
| Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 1454 | } |
| 1455 | |
| Ken Chen | 4ea8846 | 2021-05-23 14:56:43 +0800 | [diff] [blame] | 1456 | int RouteController::removeUsersFromUnreachableNetwork(unsigned netId, |
| 1457 | const UidRangeMap& uidRangeMap) { |
| 1458 | return modifyUnreachableNetwork(netId, uidRangeMap, ACTION_DEL); |
| Ken Chen | 4e8ef9b | 2021-03-17 01:57:19 +0800 | [diff] [blame] | 1459 | } |
| 1460 | |
| Lorenzo Colitti | 107075a | 2017-10-30 19:24:46 +0900 | [diff] [blame] | 1461 | // Protects sInterfaceToTable. |
| Luke Huang | 534980c | 2018-07-06 17:50:11 +0800 | [diff] [blame] | 1462 | std::mutex RouteController::sInterfaceToTableLock; |
| Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 1463 | std::map<std::string, uint32_t> RouteController::sInterfaceToTable; |
| 1464 | |
| Bernie Innocenti | 762dcf4 | 2019-06-14 19:52:49 +0900 | [diff] [blame] | 1465 | } // namespace android::net |