Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "RouteController.h" |
| 18 | |
Dan Albert | aa1be2b | 2015-01-06 09:36:17 -0800 | [diff] [blame] | 19 | #include <arpa/inet.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <linux/fib_rules.h> |
| 23 | #include <net/if.h> |
| 24 | #include <sys/stat.h> |
| 25 | |
Elliott Hughes | bd37832 | 2015-02-04 13:25:14 -0800 | [diff] [blame] | 26 | #include <private/android_filesystem_config.h> |
| 27 | |
Dan Albert | aa1be2b | 2015-01-06 09:36:17 -0800 | [diff] [blame] | 28 | #include <map> |
| 29 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 30 | #define LOG_TAG "Netd" |
| 31 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 32 | #include "DummyNetwork.h" |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 33 | #include "Fwmark.h" |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 34 | #include "NetdConstants.h" |
Lorenzo Colitti | 1ef549d | 2017-02-13 18:32:09 +0900 | [diff] [blame] | 35 | #include "NetlinkCommands.h" |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 36 | #include "UidRanges.h" |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 37 | |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 38 | #include <android-base/file.h> |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 39 | #include <android-base/stringprintf.h> |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 40 | #include "log/log.h" |
| 41 | #include "logwrap/logwrap.h" |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 42 | #include "netid_client.h" |
Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 43 | #include "netutils/ifc.h" |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 44 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 45 | using android::base::StringPrintf; |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 46 | using android::base::WriteStringToFile; |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 47 | using android::net::UidRangeParcel; |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 48 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 49 | namespace android { |
| 50 | namespace net { |
| 51 | |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 52 | auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand; |
| 53 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 54 | // BEGIN CONSTANTS -------------------------------------------------------------------------------- |
| 55 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 56 | const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000; |
Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 57 | const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500; |
Sreeram Ramachandran | 111bec2 | 2014-07-22 18:51:06 -0700 | [diff] [blame] | 58 | const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000; |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 59 | const uint32_t RULE_PRIORITY_SECURE_VPN = 12000; |
Robin Lee | 6c84ef6 | 2016-05-03 13:17:58 +0100 | [diff] [blame] | 60 | const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500; |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 61 | const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000; |
| 62 | const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000; |
| 63 | const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000; |
| 64 | const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000; |
Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 65 | const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000; |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 66 | const uint32_t RULE_PRIORITY_TETHERING = 18000; |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 67 | const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000; |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 68 | const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000; |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 69 | const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000; |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 70 | const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000; |
Lukas0610 | 59c1b63 | 2018-09-01 12:16:45 +0200 | [diff] [blame] | 71 | #ifdef NEEDS_NETD_DIRECT_CONNECT_RULE |
| 72 | const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 23000; |
| 73 | #endif |
Lorenzo Colitti | db74dba | 2014-07-29 18:26:21 +0900 | [diff] [blame] | 74 | const uint32_t RULE_PRIORITY_UNREACHABLE = 32000; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 75 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 76 | const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97; |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 77 | const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98; |
| 78 | const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99; |
| 79 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 80 | const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network"; |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 81 | const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network"; |
| 82 | const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system"; |
| 83 | |
Sreeram Ramachandran | bb40d51 | 2014-07-11 11:45:14 -0700 | [diff] [blame] | 84 | const char* const ROUTE_TABLE_NAME_LOCAL = "local"; |
| 85 | const char* const ROUTE_TABLE_NAME_MAIN = "main"; |
| 86 | |
Lorenzo Colitti | b5d19e9 | 2017-09-25 18:39:33 +0900 | [diff] [blame] | 87 | // None of our regular routes specify priority, which causes them to have the default priority. |
| 88 | // For default throw routes, we use a fixed priority of 100000. |
Lorenzo Colitti | 5e03a89 | 2017-09-08 11:31:59 +0900 | [diff] [blame] | 89 | uint32_t PRIO_THROW = 100000; |
| 90 | |
Lorenzo Colitti | d78843e | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 91 | const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT"; |
| 92 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 93 | const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6}; |
| 94 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 95 | const uid_t UID_ROOT = 0; |
Lorenzo Colitti | 3093f56 | 2017-09-25 14:17:38 +0900 | [diff] [blame] | 96 | const uint32_t FWMARK_NONE = 0; |
| 97 | const uint32_t MASK_NONE = 0; |
Lorenzo Colitti | 5ad4e98 | 2015-02-26 17:34:32 +0900 | [diff] [blame] | 98 | const char* const IIF_LOOPBACK = "lo"; |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 99 | const char* const IIF_NONE = nullptr; |
| 100 | const char* const OIF_NONE = nullptr; |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 101 | const bool ACTION_ADD = true; |
| 102 | const bool ACTION_DEL = false; |
| 103 | const bool MODIFY_NON_UID_BASED_RULES = true; |
| 104 | |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 105 | const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; |
Sreeram Ramachandran | c7d804c | 2014-07-09 07:39:30 -0700 | [diff] [blame] | 106 | 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] | 107 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 108 | // Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'" |
| 109 | // warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t). |
| 110 | constexpr uint16_t U16_RTA_LENGTH(uint16_t x) { |
| 111 | return RTA_LENGTH(x); |
| 112 | } |
| 113 | |
| 114 | // These are practically const, but can't be declared so, because they are used to initialize |
| 115 | // non-const pointers ("void* iov_base") in iovec arrays. |
Lorenzo Colitti | 2b07867 | 2016-12-16 18:45:03 +0900 | [diff] [blame] | 116 | rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY }; |
| 117 | rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE }; |
| 118 | rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK }; |
| 119 | rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK }; |
Lorenzo Colitti | 2b07867 | 2016-12-16 18:45:03 +0900 | [diff] [blame] | 120 | 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] | 121 | |
Lorenzo Colitti | 2b07867 | 2016-12-16 18:45:03 +0900 | [diff] [blame] | 122 | rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE }; |
| 123 | rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF }; |
Lorenzo Colitti | 5e03a89 | 2017-09-08 11:31:59 +0900 | [diff] [blame] | 124 | rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY }; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 125 | |
| 126 | uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0}; |
| 127 | |
| 128 | // END CONSTANTS ---------------------------------------------------------------------------------- |
| 129 | |
Lorenzo Colitti | 0b073fb | 2017-02-10 07:49:12 +0900 | [diff] [blame] | 130 | const char *actionName(uint16_t action) { |
| 131 | static const char *ops[4] = {"adding", "deleting", "getting", "???"}; |
| 132 | return ops[action % 4]; |
| 133 | } |
| 134 | |
| 135 | const char *familyName(uint8_t family) { |
| 136 | switch (family) { |
| 137 | case AF_INET: return "IPv4"; |
| 138 | case AF_INET6: return "IPv6"; |
| 139 | default: return "???"; |
| 140 | } |
| 141 | } |
| 142 | |
Lorenzo Colitti | 107075a | 2017-10-30 19:24:46 +0900 | [diff] [blame] | 143 | // Caller must hold sInterfaceToTableLock. |
| 144 | uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface) { |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 145 | uint32_t index = if_nametoindex(interface); |
Paul Jensen | a561e12 | 2014-06-12 16:46:37 -0400 | [diff] [blame] | 146 | if (index) { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 147 | index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX; |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 148 | sInterfaceToTable[interface] = index; |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 149 | return index; |
Paul Jensen | a561e12 | 2014-06-12 16:46:37 -0400 | [diff] [blame] | 150 | } |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 151 | // If the interface goes away if_nametoindex() will return 0 but we still need to know |
| 152 | // the index so we can remove the rules and routes. |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 153 | auto iter = sInterfaceToTable.find(interface); |
| 154 | if (iter == sInterfaceToTable.end()) { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 155 | ALOGE("cannot find interface %s", interface); |
| 156 | return RT_TABLE_UNSPEC; |
| 157 | } |
| 158 | return iter->second; |
| 159 | } |
| 160 | |
Rubin Xu | 6c00b61 | 2018-04-27 14:27:59 +0100 | [diff] [blame] | 161 | uint32_t RouteController::getIfIndex(const char* interface) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 162 | std::lock_guard lock(sInterfaceToTableLock); |
Rubin Xu | 6c00b61 | 2018-04-27 14:27:59 +0100 | [diff] [blame] | 163 | |
| 164 | auto iter = sInterfaceToTable.find(interface); |
| 165 | if (iter == sInterfaceToTable.end()) { |
| 166 | ALOGE("getIfIndex: cannot find interface %s", interface); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | return iter->second - ROUTE_TABLE_OFFSET_FROM_INDEX; |
| 171 | } |
| 172 | |
Lorenzo Colitti | 107075a | 2017-10-30 19:24:46 +0900 | [diff] [blame] | 173 | uint32_t RouteController::getRouteTableForInterface(const char* interface) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 174 | std::lock_guard lock(sInterfaceToTableLock); |
Lorenzo Colitti | 107075a | 2017-10-30 19:24:46 +0900 | [diff] [blame] | 175 | return getRouteTableForInterfaceLocked(interface); |
| 176 | } |
| 177 | |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 178 | void addTableName(uint32_t table, const std::string& name, std::string* contents) { |
| 179 | char tableString[UINT32_STRLEN]; |
| 180 | snprintf(tableString, sizeof(tableString), "%u", table); |
| 181 | *contents += tableString; |
| 182 | *contents += " "; |
| 183 | *contents += name; |
| 184 | *contents += "\n"; |
| 185 | } |
| 186 | |
| 187 | // 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] | 188 | void RouteController::updateTableNamesFile() { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 189 | std::string contents; |
Sreeram Ramachandran | bb40d51 | 2014-07-11 11:45:14 -0700 | [diff] [blame] | 190 | |
| 191 | addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents); |
| 192 | addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents); |
| 193 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 194 | addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents); |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 195 | addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents); |
| 196 | addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents); |
Sreeram Ramachandran | bb40d51 | 2014-07-11 11:45:14 -0700 | [diff] [blame] | 197 | |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 198 | std::lock_guard lock(sInterfaceToTableLock); |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 199 | for (const auto& entry : sInterfaceToTable) { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 200 | addTableName(entry.second, entry.first, &contents); |
| 201 | } |
| 202 | |
Dan Albert | 5407e14 | 2015-03-16 10:05:59 -0700 | [diff] [blame] | 203 | 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] | 204 | ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno)); |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 205 | return; |
| 206 | } |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 209 | // Returns 0 on success or negative errno on failure. |
| 210 | int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) { |
| 211 | if (!input) { |
| 212 | *length = 0; |
| 213 | *padding = 0; |
| 214 | return 0; |
| 215 | } |
| 216 | *length = strlcpy(name, input, IFNAMSIZ) + 1; |
| 217 | if (*length > IFNAMSIZ) { |
| 218 | ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ); |
| 219 | return -ENAMETOOLONG; |
| 220 | } |
| 221 | *padding = RTA_SPACE(*length) - RTA_LENGTH(*length); |
| 222 | return 0; |
| 223 | } |
| 224 | |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 225 | // Adds or removes a routing rule for IPv4 and IPv6. |
| 226 | // |
Lorenzo Colitti | 6b6f25f | 2015-03-03 17:22:57 +0900 | [diff] [blame] | 227 | // + 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] | 228 | // 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] | 229 | // + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is |
| 230 | // ignored. |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 231 | // + If |iif| is non-NULL, the rule matches the specified incoming interface. |
| 232 | // + If |oif| is non-NULL, the rule matches the specified outgoing interface. |
| 233 | // + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that |
| 234 | // range (inclusive). Otherwise, the rule matches packets from all UIDs. |
Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 235 | // |
| 236 | // Returns 0 on success or negative errno on failure. |
Robin Lee | 4ef9464 | 2016-04-01 11:50:49 +0100 | [diff] [blame] | 237 | WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType, |
| 238 | uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif, |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 239 | const char* oif, uid_t uidStart, uid_t uidEnd) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 240 | // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask. |
| 241 | if (fwmark & ~mask) { |
| 242 | ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark); |
| 243 | return -ERANGE; |
| 244 | } |
| 245 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 246 | // 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] | 247 | // kernels will refuse to delete rules. |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 248 | char iifName[IFNAMSIZ], oifName[IFNAMSIZ]; |
| 249 | size_t iifLength, oifLength; |
| 250 | uint16_t iifPadding, oifPadding; |
| 251 | if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) { |
| 252 | return ret; |
| 253 | } |
| 254 | if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) { |
| 255 | return ret; |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 256 | } |
| 257 | |
Lorenzo Colitti | 5965651 | 2014-06-25 03:20:29 +0900 | [diff] [blame] | 258 | // Either both start and end UID must be specified, or neither. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 259 | if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) { |
Sreeram Ramachandran | cf89138 | 2014-07-01 15:49:20 -0700 | [diff] [blame] | 260 | ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd); |
Lorenzo Colitti | 5965651 | 2014-06-25 03:20:29 +0900 | [diff] [blame] | 261 | return -EUSERS; |
| 262 | } |
Robin Lee | 4ef9464 | 2016-04-01 11:50:49 +0100 | [diff] [blame] | 263 | |
Lorenzo Colitti | 7272368 | 2014-06-26 13:51:10 +0900 | [diff] [blame] | 264 | bool isUidRule = (uidStart != INVALID_UID); |
Lorenzo Colitti | 5965651 | 2014-06-25 03:20:29 +0900 | [diff] [blame] | 265 | |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 266 | // Assemble a rule request and put it in an array of iovec structures. |
| 267 | fib_rule_hdr rule = { |
Robin Lee | 4ef9464 | 2016-04-01 11:50:49 +0100 | [diff] [blame] | 268 | .action = ruleType, |
Lorenzo Colitti | 6b6f25f | 2015-03-03 17:22:57 +0900 | [diff] [blame] | 269 | // Note that here we're implicitly setting rule.table to 0. When we want to specify a |
| 270 | // non-zero table, we do this via the FRATTR_TABLE attribute. |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 271 | }; |
| 272 | |
Lorenzo Colitti | 6b6f25f | 2015-03-03 17:22:57 +0900 | [diff] [blame] | 273 | // Don't ever create a rule that looks up table 0, because table 0 is the local table. |
| 274 | // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select |
| 275 | // table 0, it's a wildcard that matches anything. |
| 276 | if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) { |
| 277 | ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules"); |
| 278 | return -ENOTUNIQ; |
| 279 | } |
| 280 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 281 | rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME }; |
| 282 | rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME }; |
Lorenzo Colitti | 2b07867 | 2016-12-16 18:45:03 +0900 | [diff] [blame] | 283 | struct fib_rule_uid_range uidRange = { uidStart, uidEnd }; |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 284 | |
| 285 | iovec iov[] = { |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 286 | { nullptr, 0 }, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 287 | { &rule, sizeof(rule) }, |
| 288 | { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) }, |
| 289 | { &priority, sizeof(priority) }, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 290 | { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 }, |
| 291 | { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 }, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 292 | { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 }, |
| 293 | { &fwmark, mask ? sizeof(fwmark) : 0 }, |
| 294 | { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 }, |
| 295 | { &mask, mask ? sizeof(mask) : 0 }, |
Lorenzo Colitti | 2b07867 | 2016-12-16 18:45:03 +0900 | [diff] [blame] | 296 | { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 }, |
| 297 | { &uidRange, isUidRule ? sizeof(uidRange) : 0 }, |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 298 | { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 }, |
| 299 | { iifName, iifLength }, |
| 300 | { PADDING_BUFFER, iifPadding }, |
| 301 | { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 }, |
| 302 | { oifName, oifLength }, |
| 303 | { PADDING_BUFFER, oifPadding }, |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 304 | }; |
| 305 | |
Lorenzo Colitti | 5c43799 | 2017-11-28 01:26:02 +0900 | [diff] [blame] | 306 | 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] | 307 | for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) { |
| 308 | rule.family = AF_FAMILIES[i]; |
Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 309 | if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) { |
Lorenzo Colitti | 220ca73 | 2017-02-14 17:57:55 +0900 | [diff] [blame] | 310 | if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) { |
| 311 | // Don't log when deleting a tethering rule that's not there. This matches the |
| 312 | // behaviour of clearTetheringRules, which ignores ENOENT in this case. |
| 313 | ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family), |
| 314 | strerror(-ret)); |
| 315 | } |
Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 316 | return ret; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 320 | return 0; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 323 | WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table, |
Robin Lee | 4ef9464 | 2016-04-01 11:50:49 +0100 | [diff] [blame] | 324 | uint32_t fwmark, uint32_t mask, const char* iif, |
| 325 | const char* oif, uid_t uidStart, uid_t uidEnd) { |
| 326 | return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart, |
| 327 | uidEnd); |
| 328 | } |
| 329 | |
| 330 | WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table, |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 331 | uint32_t fwmark, uint32_t mask) { |
| 332 | return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID, |
| 333 | INVALID_UID); |
| 334 | } |
| 335 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 336 | // Adds or deletes an IPv4 or IPv6 route. |
| 337 | // Returns 0 on success or negative errno on failure. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 338 | WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface, |
| 339 | const char* destination, const char* nexthop) { |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 340 | // At least the destination must be non-null. |
| 341 | if (!destination) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 342 | ALOGE("null destination"); |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 343 | return -EFAULT; |
| 344 | } |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 345 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 346 | // Parse the prefix. |
| 347 | uint8_t rawAddress[sizeof(in6_addr)]; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 348 | uint8_t family; |
| 349 | uint8_t prefixLength; |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 350 | int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress), |
| 351 | &prefixLength); |
| 352 | if (rawLength < 0) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 353 | ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength)); |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 354 | return rawLength; |
| 355 | } |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 356 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 357 | if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) { |
Sreeram Ramachandran | 1201e84 | 2014-07-01 15:06:05 -0700 | [diff] [blame] | 358 | ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress)); |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 359 | return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6. |
| 360 | } |
| 361 | |
Sreeram Ramachandran | de5d5df | 2014-07-26 18:43:25 -0700 | [diff] [blame] | 362 | uint8_t type = RTN_UNICAST; |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 363 | uint32_t ifindex; |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 364 | uint8_t rawNexthop[sizeof(in6_addr)]; |
Sreeram Ramachandran | de5d5df | 2014-07-26 18:43:25 -0700 | [diff] [blame] | 365 | |
| 366 | if (nexthop && !strcmp(nexthop, "unreachable")) { |
| 367 | type = RTN_UNREACHABLE; |
| 368 | // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup |
| 369 | // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for |
| 370 | // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.) |
| 371 | interface = OIF_NONE; |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 372 | nexthop = nullptr; |
Lorenzo Colitti | 4c95a12 | 2014-09-18 16:01:50 +0900 | [diff] [blame] | 373 | } else if (nexthop && !strcmp(nexthop, "throw")) { |
| 374 | type = RTN_THROW; |
| 375 | interface = OIF_NONE; |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 376 | nexthop = nullptr; |
Sreeram Ramachandran | de5d5df | 2014-07-26 18:43:25 -0700 | [diff] [blame] | 377 | } else { |
| 378 | // If an interface was specified, find the ifindex. |
| 379 | if (interface != OIF_NONE) { |
| 380 | ifindex = if_nametoindex(interface); |
| 381 | if (!ifindex) { |
| 382 | ALOGE("cannot find interface %s", interface); |
| 383 | return -ENODEV; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // If a nexthop was specified, parse it as the same family as the prefix. |
| 388 | if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) { |
| 389 | ALOGE("inet_pton failed for nexthop %s", nexthop); |
| 390 | return -EINVAL; |
| 391 | } |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 392 | } |
| 393 | |
Lorenzo Colitti | 5e03a89 | 2017-09-08 11:31:59 +0900 | [diff] [blame] | 394 | bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0); |
| 395 | |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 396 | // Assemble a rtmsg and put it in an array of iovec structures. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 397 | rtmsg route = { |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 398 | .rtm_protocol = RTPROT_STATIC, |
Sreeram Ramachandran | de5d5df | 2014-07-26 18:43:25 -0700 | [diff] [blame] | 399 | .rtm_type = type, |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 400 | .rtm_family = family, |
| 401 | .rtm_dst_len = prefixLength, |
Sreeram Ramachandran | 2bff72e | 2014-07-18 13:03:47 -0700 | [diff] [blame] | 402 | .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK), |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 403 | }; |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 404 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 405 | rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST }; |
| 406 | rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY }; |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 407 | |
| 408 | iovec iov[] = { |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 409 | { nullptr, 0 }, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 410 | { &route, sizeof(route) }, |
| 411 | { &RTATTR_TABLE, sizeof(RTATTR_TABLE) }, |
| 412 | { &table, sizeof(table) }, |
| 413 | { &rtaDst, sizeof(rtaDst) }, |
| 414 | { rawAddress, static_cast<size_t>(rawLength) }, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 415 | { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 }, |
| 416 | { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 }, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 417 | { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 }, |
| 418 | { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 }, |
Lorenzo Colitti | 5e03a89 | 2017-09-08 11:31:59 +0900 | [diff] [blame] | 419 | { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 }, |
| 420 | { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 }, |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 421 | }; |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 422 | |
Lorenzo Colitti | 5c43799 | 2017-11-28 01:26:02 +0900 | [diff] [blame] | 423 | uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_ROUTE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS; |
Jimmy Chen | ce9e578 | 2019-03-08 16:12:55 +0800 | [diff] [blame] | 424 | |
| 425 | // Allow creating multiple link-local routes in the same table, so we can make IPv6 |
| 426 | // work on all interfaces in the local_network table. |
| 427 | if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) { |
| 428 | flags &= ~NLM_F_EXCL; |
| 429 | } |
| 430 | |
Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 431 | int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr); |
Lorenzo Colitti | 0b073fb | 2017-02-10 07:49:12 +0900 | [diff] [blame] | 432 | if (ret) { |
| 433 | ALOGE("Error %s route %s -> %s %s to table %u: %s", |
| 434 | actionName(action), destination, nexthop, interface, table, strerror(-ret)); |
| 435 | } |
| 436 | return ret; |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 439 | // An iptables rule to mark incoming packets on a network with the netId of the network. |
| 440 | // |
| 441 | // This is so that the kernel can: |
| 442 | // + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping |
| 443 | // replies, SYN-ACKs, etc). |
| 444 | // + Mark sockets that accept connections from this interface so that the connection stays on the |
| 445 | // same interface. |
| 446 | WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface, |
| 447 | Permission permission, bool add) { |
| 448 | Fwmark fwmark; |
| 449 | |
| 450 | fwmark.netId = netId; |
| 451 | fwmark.explicitlySelected = true; |
| 452 | fwmark.protectedFromVpn = true; |
| 453 | fwmark.permission = permission; |
| 454 | |
Benedict Wong | b9baf26 | 2017-12-03 15:43:08 -0800 | [diff] [blame] | 455 | const uint32_t mask = ~Fwmark::getUidBillingMask(); |
| 456 | |
| 457 | std::string cmd = StringPrintf( |
| 458 | "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D", |
| 459 | RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask); |
Lorenzo Colitti | c1306ea | 2017-03-27 05:52:31 +0900 | [diff] [blame] | 460 | if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) { |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 461 | ALOGE("failed to change iptables rule that sets incoming packet mark"); |
| 462 | return -EREMOTEIO; |
| 463 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 464 | |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | |
Sreeram Ramachandran | 111bec2 | 2014-07-22 18:51:06 -0700 | [diff] [blame] | 468 | // A rule to route responses to the local network forwarded via the VPN. |
| 469 | // |
| 470 | // When a VPN is in effect, packets from the local network to upstream networks are forwarded into |
| 471 | // the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel. |
| 472 | WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) { |
| 473 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL, |
| 474 | ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE, |
| 475 | INVALID_UID, INVALID_UID); |
| 476 | } |
| 477 | |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 478 | // A rule to route all traffic from a given set of UIDs to go over the VPN. |
| 479 | // |
| 480 | // Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may |
| 481 | // have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to |
| 482 | // bypass the VPN if the protectedFromVpn bit is set. |
| 483 | WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd, |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 484 | bool secure, bool add) { |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 485 | Fwmark fwmark; |
| 486 | Fwmark mask; |
| 487 | |
| 488 | fwmark.protectedFromVpn = false; |
| 489 | mask.protectedFromVpn = true; |
| 490 | |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 491 | uint32_t priority; |
| 492 | |
| 493 | if (secure) { |
| 494 | priority = RULE_PRIORITY_SECURE_VPN; |
| 495 | } else { |
| 496 | priority = RULE_PRIORITY_BYPASSABLE_VPN; |
| 497 | |
| 498 | fwmark.explicitlySelected = false; |
| 499 | mask.explicitlySelected = true; |
| 500 | } |
| 501 | |
| 502 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue, |
Lorenzo Colitti | 5ad4e98 | 2015-02-26 17:34:32 +0900 | [diff] [blame] | 503 | mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd); |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | // A rule to allow system apps to send traffic over this VPN even if they are not part of the target |
| 507 | // set of UIDs. |
| 508 | // |
| 509 | // This is needed for DnsProxyListener to correctly resolve a request for a user who is in the |
| 510 | // target set, but where the DnsProxyListener itself is not. |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 511 | WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure, |
| 512 | bool add) { |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 513 | Fwmark fwmark; |
| 514 | Fwmark mask; |
| 515 | |
| 516 | fwmark.netId = netId; |
| 517 | mask.netId = FWMARK_NET_ID_MASK; |
| 518 | |
| 519 | fwmark.permission = PERMISSION_SYSTEM; |
| 520 | mask.permission = PERMISSION_SYSTEM; |
| 521 | |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 522 | uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN; |
| 523 | |
| 524 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue, |
| 525 | mask.intValue); |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 528 | // A rule to route traffic based on an explicitly chosen network. |
| 529 | // |
| 530 | // Supports apps that use the multinetwork APIs to restrict their traffic to a network. |
| 531 | // |
| 532 | // Even though we check permissions at the time we set a netId into the fwmark of a socket, we need |
| 533 | // to check it again in the rules here, because a network's permissions may have been updated via |
| 534 | // modifyNetworkPermission(). |
| 535 | WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table, |
| 536 | Permission permission, uid_t uidStart, |
| 537 | uid_t uidEnd, bool add) { |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 538 | Fwmark fwmark; |
| 539 | Fwmark mask; |
| 540 | |
| 541 | fwmark.netId = netId; |
| 542 | mask.netId = FWMARK_NET_ID_MASK; |
| 543 | |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 544 | fwmark.explicitlySelected = true; |
| 545 | mask.explicitlySelected = true; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 546 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 547 | fwmark.permission = permission; |
| 548 | mask.permission = permission; |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 549 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 550 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table, |
Lorenzo Colitti | 758627c | 2018-03-15 01:49:20 +0900 | [diff] [blame] | 551 | fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd); |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 554 | // A rule to route traffic based on a chosen outgoing interface. |
| 555 | // |
| 556 | // Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows |
| 557 | // the outgoing interface (typically for link-local communications). |
Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 558 | WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table, |
| 559 | Permission permission, uid_t uidStart, |
| 560 | uid_t uidEnd, bool add) { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 561 | Fwmark fwmark; |
| 562 | Fwmark mask; |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 563 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 564 | fwmark.permission = permission; |
| 565 | mask.permission = permission; |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 566 | |
Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 567 | // 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] | 568 | // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open |
| 569 | // sockets as root. |
Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 570 | if (uidStart == INVALID_UID && uidEnd == INVALID_UID) { |
| 571 | 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] | 572 | table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface, |
Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 573 | UID_ROOT, UID_ROOT)) { |
| 574 | return ret; |
| 575 | } |
| 576 | } |
| 577 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 578 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table, |
Lorenzo Colitti | 758627c | 2018-03-15 01:49:20 +0900 | [diff] [blame] | 579 | fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd); |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | // A rule to route traffic based on the chosen network. |
| 583 | // |
| 584 | // This is for sockets that have not explicitly requested a particular network, but have been |
| 585 | // bound to one when they called connect(). This ensures that sockets connected on a particular |
| 586 | // network stay on that network even if the default network changes. |
Lorenzo Colitti | be0c7c3 | 2017-09-06 16:07:02 +0900 | [diff] [blame] | 587 | WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 588 | Fwmark fwmark; |
| 589 | Fwmark mask; |
| 590 | |
| 591 | fwmark.netId = netId; |
| 592 | mask.netId = FWMARK_NET_ID_MASK; |
| 593 | |
| 594 | fwmark.explicitlySelected = false; |
| 595 | mask.explicitlySelected = true; |
| 596 | |
Lorenzo Colitti | be0c7c3 | 2017-09-06 16:07:02 +0900 | [diff] [blame] | 597 | fwmark.permission = PERMISSION_NONE; |
| 598 | mask.permission = PERMISSION_NONE; |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 599 | |
| 600 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table, |
Lorenzo Colitti | 758627c | 2018-03-15 01:49:20 +0900 | [diff] [blame] | 601 | fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, |
| 602 | INVALID_UID); |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 605 | // A rule to enable split tunnel VPNs. |
| 606 | // |
| 607 | // 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 | 0a5f897 | 2019-05-23 06:14:36 -0700 | [diff] [blame] | 608 | // go over the default network, provided it has the permissions required by the default network. |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 609 | WARN_UNUSED_RESULT int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId, |
| 610 | const char* physicalInterface, |
| 611 | Permission permission) { |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 612 | uint32_t table = getRouteTableForInterface(physicalInterface); |
| 613 | if (table == RT_TABLE_UNSPEC) { |
| 614 | return -ESRCH; |
| 615 | } |
| 616 | |
| 617 | Fwmark fwmark; |
| 618 | Fwmark mask; |
| 619 | |
| 620 | fwmark.netId = vpnNetId; |
| 621 | mask.netId = FWMARK_NET_ID_MASK; |
| 622 | |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 623 | fwmark.permission = permission; |
| 624 | mask.permission = permission; |
| 625 | |
| 626 | return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue, |
| 627 | mask.intValue); |
| 628 | } |
| 629 | |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 630 | // Add rules to allow legacy routes added through the requestRouteToHost() API. |
| 631 | WARN_UNUSED_RESULT int addLegacyRouteRules() { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 632 | Fwmark fwmark; |
| 633 | Fwmark mask; |
| 634 | |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 635 | fwmark.explicitlySelected = false; |
| 636 | mask.explicitlySelected = true; |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 637 | |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 638 | // Rules to allow legacy routes to override the default network. |
| 639 | if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM, |
| 640 | fwmark.intValue, mask.intValue)) { |
| 641 | return ret; |
| 642 | } |
| 643 | if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK, |
| 644 | ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) { |
| 645 | return ret; |
| 646 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 647 | |
| 648 | fwmark.permission = PERMISSION_SYSTEM; |
| 649 | mask.permission = PERMISSION_SYSTEM; |
| 650 | |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 651 | // A rule to allow legacy routes from system apps to override VPNs. |
| 652 | 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] | 653 | fwmark.intValue, mask.intValue); |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 656 | // Add rules to lookup the local network when specified explicitly or otherwise. |
| 657 | WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) { |
| 658 | if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE, |
| 659 | INVALID_UID, INVALID_UID, ACTION_ADD)) { |
| 660 | return ret; |
Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 661 | } |
| 662 | |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 663 | Fwmark fwmark; |
| 664 | Fwmark mask; |
| 665 | |
| 666 | fwmark.explicitlySelected = false; |
| 667 | mask.explicitlySelected = true; |
| 668 | |
| 669 | return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK, |
| 670 | fwmark.intValue, mask.intValue); |
| 671 | } |
| 672 | |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 673 | /* static */ |
| 674 | int RouteController::configureDummyNetwork() { |
Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 675 | const char *interface = DummyNetwork::INTERFACE_NAME; |
| 676 | uint32_t table = getRouteTableForInterface(interface); |
| 677 | if (table == RT_TABLE_UNSPEC) { |
| 678 | // getRouteTableForInterface has already looged an error. |
| 679 | return -ESRCH; |
| 680 | } |
| 681 | |
| 682 | ifc_init(); |
| 683 | int ret = ifc_up(interface); |
| 684 | ifc_close(); |
| 685 | if (ret) { |
| 686 | ALOGE("Can't bring up %s: %s", interface, strerror(errno)); |
| 687 | return -errno; |
| 688 | } |
| 689 | |
Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 690 | if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, |
| 691 | INVALID_UID, INVALID_UID, ACTION_ADD))) { |
| 692 | ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret)); |
Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 693 | return ret; |
| 694 | } |
| 695 | |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 696 | if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", nullptr))) { |
Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 697 | return ret; |
| 698 | } |
| 699 | |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 700 | if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", nullptr))) { |
Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | return 0; |
| 705 | } |
| 706 | |
Lukas0610 | 59c1b63 | 2018-09-01 12:16:45 +0200 | [diff] [blame] | 707 | #ifdef NEEDS_NETD_DIRECT_CONNECT_RULE |
| 708 | // Add a new rule to look up the 'main' table, with the same selectors as the "default network" |
| 709 | // rule, but with a lower priority. We will never create routes in the main table; it should only be |
| 710 | // used for directly-connected routes implicitly created by the kernel when adding IP addresses. |
| 711 | // This is necessary, for example, when adding a route through a directly-connected gateway: in |
| 712 | // order to add the route, there must already be a directly-connected route that covers the gateway. |
| 713 | WARN_UNUSED_RESULT int addDirectlyConnectedRule() { |
| 714 | Fwmark fwmark; |
| 715 | Fwmark mask; |
| 716 | |
| 717 | fwmark.netId = NETID_UNSET; |
| 718 | mask.netId = FWMARK_NET_ID_MASK; |
| 719 | |
| 720 | return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN, |
| 721 | fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT); |
| 722 | } |
| 723 | #endif |
| 724 | |
Lorenzo Colitti | db74dba | 2014-07-29 18:26:21 +0900 | [diff] [blame] | 725 | // Add an explicit unreachable rule close to the end of the prioriy list to make it clear that |
| 726 | // relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended |
| 727 | // behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable |
| 728 | // rule will hopefully make things even clearer. |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 729 | WARN_UNUSED_RESULT int addUnreachableRule() { |
Robin Lee | c125fe4 | 2016-05-02 08:53:34 +0100 | [diff] [blame] | 730 | return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC, |
| 731 | MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID); |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) { |
Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 735 | if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) { |
| 736 | return ret; |
| 737 | } |
Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 738 | return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE, |
| 739 | INVALID_UID, INVALID_UID, add); |
Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 740 | } |
| 741 | |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 742 | /* static */ |
| 743 | WARN_UNUSED_RESULT int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface, |
| 744 | Permission permission, bool add) { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 745 | uint32_t table = getRouteTableForInterface(interface); |
| 746 | if (table == RT_TABLE_UNSPEC) { |
| 747 | return -ESRCH; |
| 748 | } |
| 749 | |
| 750 | if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) { |
| 751 | return ret; |
| 752 | } |
| 753 | if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID, |
| 754 | add)) { |
| 755 | return ret; |
| 756 | } |
Lorenzo Colitti | 57947f0 | 2015-02-27 16:45:55 +0900 | [diff] [blame] | 757 | if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 758 | add)) { |
| 759 | return ret; |
| 760 | } |
Lorenzo Colitti | be0c7c3 | 2017-09-06 16:07:02 +0900 | [diff] [blame] | 761 | |
| 762 | // Only set implicit rules for networks that don't require permissions. |
| 763 | // |
| 764 | // This is so that if the default network ceases to be the default network and then switches |
| 765 | // from requiring no permissions to requiring permissions, we ensure that apps only use the |
| 766 | // network if they explicitly select it. This is consistent with destroySocketsLackingPermission |
| 767 | // - it closes all sockets on the network except sockets that are explicitly selected. |
| 768 | // |
| 769 | // The lack of this rule only affects the special case above, because: |
| 770 | // - The only cases where we implicitly bind a socket to a network are the default network and |
| 771 | // the bypassable VPN that applies to the app, if any. |
| 772 | // - This rule doesn't affect VPNs because they don't support permissions at all. |
| 773 | // - The default network doesn't require permissions. While we support doing this, the framework |
| 774 | // never does it (partly because we'd end up in the situation where we tell apps that there is |
| 775 | // a default network, but they can't use it). |
| 776 | // - If the network is still the default network, the presence or absence of this rule does not |
| 777 | // matter. |
| 778 | // |
| 779 | // Therefore, for the lack of this rule to affect a socket, the socket has to have been |
| 780 | // implicitly bound to a network because at the time of connect() it was the default, and that |
| 781 | // network must no longer be the default, and must now require permissions. |
| 782 | if (permission == PERMISSION_NONE) { |
| 783 | return modifyImplicitNetworkRule(netId, table, add); |
| 784 | } |
| 785 | return 0; |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 788 | WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) { |
| 789 | Fwmark fwmark; |
| 790 | Fwmark mask; |
| 791 | fwmark.protectedFromVpn = false; |
| 792 | mask.protectedFromVpn = true; |
| 793 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 794 | for (const UidRangeParcel& range : uidRanges.getRanges()) { |
| 795 | if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN, |
| 796 | FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue, |
| 797 | IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) { |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 798 | return ret; |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 805 | WARN_UNUSED_RESULT int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface, |
| 806 | const UidRanges& uidRanges, |
| 807 | bool secure, bool add, |
| 808 | bool modifyNonUidBasedRules) { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 809 | uint32_t table = getRouteTableForInterface(interface); |
| 810 | if (table == RT_TABLE_UNSPEC) { |
| 811 | return -ESRCH; |
| 812 | } |
| 813 | |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 814 | for (const UidRangeParcel& range : uidRanges.getRanges()) { |
| 815 | if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, secure, add)) { |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 816 | return ret; |
| 817 | } |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 818 | if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start, |
| 819 | range.stop, add)) { |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 820 | return ret; |
| 821 | } |
Luke Huang | 94658ac | 2018-10-18 19:35:12 +0900 | [diff] [blame] | 822 | if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start, |
| 823 | range.stop, add)) { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 824 | return ret; |
| 825 | } |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 828 | if (modifyNonUidBasedRules) { |
| 829 | if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) { |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 830 | return ret; |
| 831 | } |
Sreeram Ramachandran | 111bec2 | 2014-07-22 18:51:06 -0700 | [diff] [blame] | 832 | if (int ret = modifyVpnOutputToLocalRule(interface, add)) { |
| 833 | return ret; |
| 834 | } |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 835 | if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) { |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 836 | return ret; |
| 837 | } |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 838 | return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | return 0; |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 844 | WARN_UNUSED_RESULT int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface, |
| 845 | Permission permission) { |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 846 | uint32_t table = getRouteTableForInterface(interface); |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 847 | if (table == RT_TABLE_UNSPEC) { |
Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 848 | return -ESRCH; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 849 | } |
| 850 | |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 851 | Fwmark fwmark; |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 852 | Fwmark mask; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 853 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 854 | fwmark.netId = NETID_UNSET; |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 855 | mask.netId = FWMARK_NET_ID_MASK; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 856 | |
| 857 | fwmark.permission = permission; |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 858 | mask.permission = permission; |
| 859 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 860 | return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue, |
Lorenzo Colitti | 758627c | 2018-03-15 01:49:20 +0900 | [diff] [blame] | 861 | mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 862 | } |
| 863 | |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 864 | WARN_UNUSED_RESULT int RouteController::modifyTetheredNetwork(uint16_t action, |
| 865 | const char* inputInterface, |
| 866 | const char* outputInterface) { |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 867 | uint32_t table = getRouteTableForInterface(outputInterface); |
| 868 | if (table == RT_TABLE_UNSPEC) { |
| 869 | return -ESRCH; |
| 870 | } |
| 871 | |
| 872 | return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET, |
| 873 | inputInterface, OIF_NONE, INVALID_UID, INVALID_UID); |
| 874 | } |
| 875 | |
Lorenzo Colitti | 92e8f96 | 2017-09-26 19:13:50 +0900 | [diff] [blame] | 876 | // Adds or removes an IPv4 or IPv6 route to the specified table. |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 877 | // Returns 0 on success or negative errno on failure. |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 878 | WARN_UNUSED_RESULT int RouteController::modifyRoute(uint16_t action, const char* interface, |
| 879 | const char* destination, const char* nexthop, |
| 880 | TableType tableType) { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 881 | uint32_t table; |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 882 | switch (tableType) { |
| 883 | case RouteController::INTERFACE: { |
| 884 | table = getRouteTableForInterface(interface); |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 885 | if (table == RT_TABLE_UNSPEC) { |
| 886 | return -ESRCH; |
| 887 | } |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 888 | break; |
| 889 | } |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 890 | case RouteController::LOCAL_NETWORK: { |
| 891 | table = ROUTE_TABLE_LOCAL_NETWORK; |
| 892 | break; |
| 893 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 894 | case RouteController::LEGACY_NETWORK: { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 895 | table = ROUTE_TABLE_LEGACY_NETWORK; |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 896 | break; |
| 897 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 898 | case RouteController::LEGACY_SYSTEM: { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 899 | table = ROUTE_TABLE_LEGACY_SYSTEM; |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 900 | break; |
| 901 | } |
| 902 | } |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 903 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 904 | int ret = modifyIpRoute(action, table, interface, destination, nexthop); |
Sreeram Ramachandran | 0321315 | 2014-10-30 10:01:07 -0700 | [diff] [blame] | 905 | // Trying to add a route that already exists shouldn't cause an error. |
| 906 | if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) { |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 907 | return ret; |
Sreeram Ramachandran | c921337 | 2014-04-16 12:32:18 -0700 | [diff] [blame] | 908 | } |
| 909 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 910 | return 0; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Lorenzo Colitti | 6b6f25f | 2015-03-03 17:22:57 +0900 | [diff] [blame] | 913 | WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) { |
| 914 | int ret = 0; |
| 915 | while (ret == 0) { |
| 916 | ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET, |
| 917 | inputInterface, OIF_NONE, INVALID_UID, INVALID_UID); |
| 918 | } |
| 919 | |
| 920 | if (ret == -ENOENT) { |
| 921 | return 0; |
| 922 | } else { |
| 923 | return ret; |
| 924 | } |
| 925 | } |
| 926 | |
Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 927 | uint32_t getRulePriority(const nlmsghdr *nlh) { |
| 928 | return getRtmU32Attribute(nlh, FRA_PRIORITY); |
| 929 | } |
| 930 | |
| 931 | uint32_t getRouteTable(const nlmsghdr *nlh) { |
| 932 | return getRtmU32Attribute(nlh, RTA_TABLE); |
| 933 | } |
| 934 | |
| 935 | WARN_UNUSED_RESULT int flushRules() { |
| 936 | NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) { |
| 937 | // Don't touch rules at priority 0 because by default they are used for local input. |
| 938 | return getRulePriority(nlh) != 0; |
| 939 | }; |
| 940 | return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete); |
| 941 | } |
| 942 | |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 943 | WARN_UNUSED_RESULT int RouteController::flushRoutes(uint32_t table) { |
Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 944 | NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) { |
| 945 | return getRouteTable(nlh) == table; |
| 946 | }; |
| 947 | |
| 948 | return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete); |
| 949 | } |
| 950 | |
| 951 | // Returns 0 on success or negative errno on failure. |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 952 | WARN_UNUSED_RESULT int RouteController::flushRoutes(const char* interface) { |
Bernie Innocenti | abf8a34 | 2018-08-10 15:17:16 +0900 | [diff] [blame] | 953 | std::lock_guard lock(sInterfaceToTableLock); |
Lorenzo Colitti | 107075a | 2017-10-30 19:24:46 +0900 | [diff] [blame] | 954 | |
| 955 | uint32_t table = getRouteTableForInterfaceLocked(interface); |
Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 956 | if (table == RT_TABLE_UNSPEC) { |
| 957 | return -ESRCH; |
| 958 | } |
| 959 | |
| 960 | int ret = flushRoutes(table); |
| 961 | |
| 962 | // If we failed to flush routes, the caller may elect to keep this interface around, so keep |
| 963 | // track of its name. |
| 964 | if (ret == 0) { |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 965 | sInterfaceToTable.erase(interface); |
Lorenzo Colitti | f3e299a | 2017-02-14 17:24:28 +0900 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | return ret; |
| 969 | } |
| 970 | |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 971 | int RouteController::Init(unsigned localNetId) { |
Sreeram Ramachandran | b717e74 | 2014-07-19 00:22:15 -0700 | [diff] [blame] | 972 | if (int ret = flushRules()) { |
| 973 | return ret; |
| 974 | } |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 975 | if (int ret = addLegacyRouteRules()) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 976 | return ret; |
| 977 | } |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 978 | if (int ret = addLocalNetworkRules(localNetId)) { |
| 979 | return ret; |
| 980 | } |
Lukas0610 | 59c1b63 | 2018-09-01 12:16:45 +0200 | [diff] [blame] | 981 | #ifdef NEEDS_NETD_DIRECT_CONNECT_RULE |
| 982 | if (int ret = addDirectlyConnectedRule()) { |
| 983 | return ret; |
| 984 | } |
| 985 | #endif |
Sreeram Ramachandran | b717e74 | 2014-07-19 00:22:15 -0700 | [diff] [blame] | 986 | if (int ret = addUnreachableRule()) { |
| 987 | return ret; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 988 | } |
Lorenzo Colitti | 3667936 | 2015-02-25 10:26:19 +0900 | [diff] [blame] | 989 | // Don't complain if we can't add the dummy network, since not all devices support it. |
| 990 | configureDummyNetwork(); |
| 991 | |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 992 | updateTableNamesFile(); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 993 | return 0; |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 994 | } |
| 995 | |
Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 996 | int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) { |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 997 | return modifyLocalNetwork(netId, interface, ACTION_ADD); |
Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) { |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 1001 | return modifyLocalNetwork(netId, interface, ACTION_DEL); |
Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1004 | int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface, |
| 1005 | Permission permission) { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1006 | if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) { |
| 1007 | return ret; |
| 1008 | } |
| 1009 | updateTableNamesFile(); |
| 1010 | return 0; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1013 | int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface, |
| 1014 | Permission permission) { |
| 1015 | if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 1016 | return ret; |
| 1017 | } |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1018 | if (int ret = flushRoutes(interface)) { |
| 1019 | return ret; |
| 1020 | } |
Lorenzo Colitti | 6b6f25f | 2015-03-03 17:22:57 +0900 | [diff] [blame] | 1021 | if (int ret = clearTetheringRules(interface)) { |
| 1022 | return ret; |
| 1023 | } |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1024 | updateTableNamesFile(); |
| 1025 | return 0; |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1028 | int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface, |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 1029 | bool secure, const UidRanges& uidRanges) { |
| 1030 | if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD, |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1031 | MODIFY_NON_UID_BASED_RULES)) { |
| 1032 | return ret; |
| 1033 | } |
| 1034 | updateTableNamesFile(); |
| 1035 | return 0; |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1038 | int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface, |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 1039 | bool secure, const UidRanges& uidRanges) { |
| 1040 | if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1041 | MODIFY_NON_UID_BASED_RULES)) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 1042 | return ret; |
| 1043 | } |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 1044 | if (int ret = flushRoutes(interface)) { |
| 1045 | return ret; |
| 1046 | } |
| 1047 | updateTableNamesFile(); |
| 1048 | return 0; |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 1049 | } |
| 1050 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1051 | int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface, |
| 1052 | Permission oldPermission, |
| 1053 | Permission newPermission) { |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 1054 | // Add the new rules before deleting the old ones, to avoid race conditions. |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1055 | if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 1056 | return ret; |
| 1057 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1058 | return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 1061 | int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) { |
| 1062 | return modifyRejectNonSecureNetworkRule(uidRanges, true); |
| 1063 | } |
| 1064 | |
| 1065 | int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) { |
| 1066 | return modifyRejectNonSecureNetworkRule(uidRanges, false); |
| 1067 | } |
| 1068 | |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 1069 | int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1070 | const UidRanges& uidRanges) { |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 1071 | return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1072 | !MODIFY_NON_UID_BASED_RULES); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1075 | int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface, |
Sreeram Ramachandran | 95684ba | 2014-07-23 13:27:31 -0700 | [diff] [blame] | 1076 | bool secure, const UidRanges& uidRanges) { |
| 1077 | return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1078 | !MODIFY_NON_UID_BASED_RULES); |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1081 | int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) { |
| 1082 | return modifyDefaultNetwork(RTM_NEWRULE, interface, permission); |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 1085 | int RouteController::removeInterfaceFromDefaultNetwork(const char* interface, |
| 1086 | Permission permission) { |
| 1087 | return modifyDefaultNetwork(RTM_DELRULE, interface, permission); |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 1088 | } |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 1089 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 1090 | int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop, |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 1091 | TableType tableType) { |
| 1092 | return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 1095 | int RouteController::removeRoute(const char* interface, const char* destination, |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 1096 | const char* nexthop, TableType tableType) { |
| 1097 | return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 1098 | } |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 1099 | |
| 1100 | int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) { |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 1101 | return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface); |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) { |
Sreeram Ramachandran | fa9f4dc | 2014-07-22 18:16:44 -0700 | [diff] [blame] | 1105 | return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface); |
Sreeram Ramachandran | 87475a1 | 2014-07-15 16:20:28 -0700 | [diff] [blame] | 1106 | } |
Sreeram Ramachandran | 48e19b0 | 2014-07-22 22:23:20 -0700 | [diff] [blame] | 1107 | |
| 1108 | int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface, |
| 1109 | Permission permission) { |
| 1110 | return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission); |
| 1111 | } |
| 1112 | |
| 1113 | int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId, |
| 1114 | const char* physicalInterface, |
| 1115 | Permission permission) { |
| 1116 | return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission); |
| 1117 | } |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 1118 | |
Lorenzo Colitti | 107075a | 2017-10-30 19:24:46 +0900 | [diff] [blame] | 1119 | // Protects sInterfaceToTable. |
Luke Huang | 534980c | 2018-07-06 17:50:11 +0800 | [diff] [blame] | 1120 | std::mutex RouteController::sInterfaceToTableLock; |
Lorenzo Colitti | 02cb80a | 2017-10-30 19:21:06 +0900 | [diff] [blame] | 1121 | std::map<std::string, uint32_t> RouteController::sInterfaceToTable; |
| 1122 | |
Luke Huang | d1ee462 | 2018-06-29 13:49:58 +0800 | [diff] [blame] | 1123 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 1124 | } // namespace net |
| 1125 | } // namespace android |