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