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