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