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