blob: ccec437dba27d0a0d9b60996b029312180899af5 [file] [log] [blame]
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001/*
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 Albertaa1be2b2015-01-06 09:36:17 -080019#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 Hughesbd378322015-02-04 13:25:14 -080026#include <private/android_filesystem_config.h>
27
Dan Albertaa1be2b2015-01-06 09:36:17 -080028#include <map>
29
Lorenzo Colitti7035f222017-02-13 18:29:00 +090030#include "DummyNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070031#include "Fwmark.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090032#include "NetdConstants.h"
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090033#include "NetlinkCommands.h"
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070034#include "UidRanges.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070035
Elliott Hughesbbd56262015-12-04 15:45:10 -080036#include "android-base/file.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090037#include <android-base/stringprintf.h>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070038#define LOG_TAG "Netd"
39#include "log/log.h"
40#include "logwrap/logwrap.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090041#include "netutils/ifc.h"
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070042#include "resolv_netid.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070043
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090044using android::base::StringPrintf;
Dan Albert5407e142015-03-16 10:05:59 -070045using android::base::WriteStringToFile;
Robin Leedc0d5782016-07-20 14:17:11 +010046using android::net::UidRange;
Dan Albert5407e142015-03-16 10:05:59 -070047
Lorenzo Colitti7035f222017-02-13 18:29:00 +090048namespace android {
49namespace net {
50
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090051auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand;
52
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070053// BEGIN CONSTANTS --------------------------------------------------------------------------------
54
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070055const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090056const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070057const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070058const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010059const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070060const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
61const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
62const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
63const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070064const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070065const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070066const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070067const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070068const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070069const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090070const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070071
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070072const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070073const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
74const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
75
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070076const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070077const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
78const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
79
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070080const char* const ROUTE_TABLE_NAME_LOCAL = "local";
81const char* const ROUTE_TABLE_NAME_MAIN = "main";
82
Lorenzo Colittib5d19e92017-09-25 18:39:33 +090083// 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 Colitti5e03a892017-09-08 11:31:59 +090085uint32_t PRIO_THROW = 100000;
86
Lorenzo Colittid78843e2017-03-27 05:52:31 +090087const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
88
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070089const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
90
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070091const uid_t UID_ROOT = 0;
Lorenzo Colitti3093f562017-09-25 14:17:38 +090092const uint32_t FWMARK_NONE = 0;
93const uint32_t MASK_NONE = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090094const char* const IIF_LOOPBACK = "lo";
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070095const char* const IIF_NONE = NULL;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070096const char* const OIF_NONE = NULL;
97const bool ACTION_ADD = true;
98const bool ACTION_DEL = false;
99const bool MODIFY_NON_UID_BASED_RULES = true;
100
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700101const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700102const mode_t RT_TABLES_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // mode 0644, rw-r--r--
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700103
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700104// 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).
106constexpr 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 Colitti2b078672016-12-16 18:45:03 +0900112rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
113rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
114rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
115rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900116rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700117
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900118rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
119rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900120rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700121
122uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
123
124// END CONSTANTS ----------------------------------------------------------------------------------
125
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900126const char *actionName(uint16_t action) {
127 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
128 return ops[action % 4];
129}
130
131const 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
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900139// Caller must hold sInterfaceToTableLock.
140uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700141 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400142 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700143 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900144 sInterfaceToTable[interface] = index;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700145 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400146 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700147 // If the interface goes away if_nametoindex() will return 0 but we still need to know
148 // the index so we can remove the rules and routes.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900149 auto iter = sInterfaceToTable.find(interface);
150 if (iter == sInterfaceToTable.end()) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700151 ALOGE("cannot find interface %s", interface);
152 return RT_TABLE_UNSPEC;
153 }
154 return iter->second;
155}
156
Rubin Xu6c00b612018-04-27 14:27:59 +0100157uint32_t RouteController::getIfIndex(const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800158 std::shared_lock<std::shared_mutex> lock(sInterfaceToTableLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100159
160 auto iter = sInterfaceToTable.find(interface);
161 if (iter == sInterfaceToTable.end()) {
162 ALOGE("getIfIndex: cannot find interface %s", interface);
163 return 0;
164 }
165
166 return iter->second - ROUTE_TABLE_OFFSET_FROM_INDEX;
167}
168
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900169uint32_t RouteController::getRouteTableForInterface(const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800170 std::shared_lock<std::shared_mutex> lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900171 return getRouteTableForInterfaceLocked(interface);
172}
173
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700174void addTableName(uint32_t table, const std::string& name, std::string* contents) {
175 char tableString[UINT32_STRLEN];
176 snprintf(tableString, sizeof(tableString), "%u", table);
177 *contents += tableString;
178 *contents += " ";
179 *contents += name;
180 *contents += "\n";
181}
182
183// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900184void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700185 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700186
187 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
188 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
189
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700190 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700191 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
192 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700193
Luke Huangd1ee4622018-06-29 13:49:58 +0800194 std::shared_lock<std::shared_mutex> lock(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900195 for (const auto& entry : sInterfaceToTable) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700196 addTableName(entry.second, entry.first, &contents);
197 }
198
Dan Albert5407e142015-03-16 10:05:59 -0700199 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800200 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700201 return;
202 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700203}
204
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700205// Returns 0 on success or negative errno on failure.
206int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
207 if (!input) {
208 *length = 0;
209 *padding = 0;
210 return 0;
211 }
212 *length = strlcpy(name, input, IFNAMSIZ) + 1;
213 if (*length > IFNAMSIZ) {
214 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
215 return -ENAMETOOLONG;
216 }
217 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
218 return 0;
219}
220
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700221// Adds or removes a routing rule for IPv4 and IPv6.
222//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900223// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100224// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700225// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
226// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700227// + If |iif| is non-NULL, the rule matches the specified incoming interface.
228// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
229// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
230// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900231//
232// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100233WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
234 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700235 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700236 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
237 if (fwmark & ~mask) {
238 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
239 return -ERANGE;
240 }
241
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700242 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900243 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700244 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
245 size_t iifLength, oifLength;
246 uint16_t iifPadding, oifPadding;
247 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
248 return ret;
249 }
250 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
251 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900252 }
253
Lorenzo Colitti59656512014-06-25 03:20:29 +0900254 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700255 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700256 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900257 return -EUSERS;
258 }
Robin Lee4ef94642016-04-01 11:50:49 +0100259
Lorenzo Colitti72723682014-06-26 13:51:10 +0900260 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900261
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900262 // Assemble a rule request and put it in an array of iovec structures.
263 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100264 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900265 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
266 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900267 };
268
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900269 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
270 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
271 // table 0, it's a wildcard that matches anything.
272 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
273 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
274 return -ENOTUNIQ;
275 }
276
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700277 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
278 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900279 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900280
281 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700282 { NULL, 0 },
283 { &rule, sizeof(rule) },
284 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
285 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700286 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
287 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700288 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
289 { &fwmark, mask ? sizeof(fwmark) : 0 },
290 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
291 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900292 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
293 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700294 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
295 { iifName, iifLength },
296 { PADDING_BUFFER, iifPadding },
297 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
298 { oifName, oifLength },
299 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900300 };
301
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900302 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700303 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
304 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900305 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900306 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
307 // Don't log when deleting a tethering rule that's not there. This matches the
308 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
309 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
310 strerror(-ret));
311 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900312 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700313 }
314 }
315
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900316 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700317}
318
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700319WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100320 uint32_t fwmark, uint32_t mask, const char* iif,
321 const char* oif, uid_t uidStart, uid_t uidEnd) {
322 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
323 uidEnd);
324}
325
326WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700327 uint32_t fwmark, uint32_t mask) {
328 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
329 INVALID_UID);
330}
331
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900332// Adds or deletes an IPv4 or IPv6 route.
333// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700334WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
335 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900336 // At least the destination must be non-null.
337 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700338 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900339 return -EFAULT;
340 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700341
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900342 // Parse the prefix.
343 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700344 uint8_t family;
345 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900346 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
347 &prefixLength);
348 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700349 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900350 return rawLength;
351 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700352
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900353 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700354 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900355 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
356 }
357
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700358 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900359 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900360 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700361
362 if (nexthop && !strcmp(nexthop, "unreachable")) {
363 type = RTN_UNREACHABLE;
364 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
365 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
366 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
367 interface = OIF_NONE;
368 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900369 } else if (nexthop && !strcmp(nexthop, "throw")) {
370 type = RTN_THROW;
371 interface = OIF_NONE;
372 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700373 } else {
374 // If an interface was specified, find the ifindex.
375 if (interface != OIF_NONE) {
376 ifindex = if_nametoindex(interface);
377 if (!ifindex) {
378 ALOGE("cannot find interface %s", interface);
379 return -ENODEV;
380 }
381 }
382
383 // If a nexthop was specified, parse it as the same family as the prefix.
384 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
385 ALOGE("inet_pton failed for nexthop %s", nexthop);
386 return -EINVAL;
387 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900388 }
389
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900390 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
391
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900392 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700393 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900394 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700395 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900396 .rtm_family = family,
397 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700398 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900399 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900400
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700401 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
402 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900403
404 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700405 { NULL, 0 },
406 { &route, sizeof(route) },
407 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
408 { &table, sizeof(table) },
409 { &rtaDst, sizeof(rtaDst) },
410 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700411 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
412 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700413 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
414 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900415 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
416 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900417 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900418
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900419 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_ROUTE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900420 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900421 if (ret) {
422 ALOGE("Error %s route %s -> %s %s to table %u: %s",
423 actionName(action), destination, nexthop, interface, table, strerror(-ret));
424 }
425 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700426}
427
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700428// An iptables rule to mark incoming packets on a network with the netId of the network.
429//
430// This is so that the kernel can:
431// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
432// replies, SYN-ACKs, etc).
433// + Mark sockets that accept connections from this interface so that the connection stays on the
434// same interface.
435WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
436 Permission permission, bool add) {
437 Fwmark fwmark;
438
439 fwmark.netId = netId;
440 fwmark.explicitlySelected = true;
441 fwmark.protectedFromVpn = true;
442 fwmark.permission = permission;
443
Benedict Wongb9baf262017-12-03 15:43:08 -0800444 const uint32_t mask = ~Fwmark::getUidBillingMask();
445
446 std::string cmd = StringPrintf(
447 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
448 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900449 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700450 ALOGE("failed to change iptables rule that sets incoming packet mark");
451 return -EREMOTEIO;
452 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700453
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700454 return 0;
455}
456
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700457// A rule to route responses to the local network forwarded via the VPN.
458//
459// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
460// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
461WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
462 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
463 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
464 INVALID_UID, INVALID_UID);
465}
466
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700467// A rule to route all traffic from a given set of UIDs to go over the VPN.
468//
469// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
470// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
471// bypass the VPN if the protectedFromVpn bit is set.
472WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700473 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700474 Fwmark fwmark;
475 Fwmark mask;
476
477 fwmark.protectedFromVpn = false;
478 mask.protectedFromVpn = true;
479
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700480 uint32_t priority;
481
482 if (secure) {
483 priority = RULE_PRIORITY_SECURE_VPN;
484 } else {
485 priority = RULE_PRIORITY_BYPASSABLE_VPN;
486
487 fwmark.explicitlySelected = false;
488 mask.explicitlySelected = true;
489 }
490
491 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900492 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700493}
494
495// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
496// set of UIDs.
497//
498// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
499// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700500WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
501 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700502 Fwmark fwmark;
503 Fwmark mask;
504
505 fwmark.netId = netId;
506 mask.netId = FWMARK_NET_ID_MASK;
507
508 fwmark.permission = PERMISSION_SYSTEM;
509 mask.permission = PERMISSION_SYSTEM;
510
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700511 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
512
513 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
514 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700515}
516
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700517// A rule to route traffic based on an explicitly chosen network.
518//
519// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
520//
521// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
522// to check it again in the rules here, because a network's permissions may have been updated via
523// modifyNetworkPermission().
524WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
525 Permission permission, uid_t uidStart,
526 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700527 Fwmark fwmark;
528 Fwmark mask;
529
530 fwmark.netId = netId;
531 mask.netId = FWMARK_NET_ID_MASK;
532
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700533 fwmark.explicitlySelected = true;
534 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700535
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700536 fwmark.permission = permission;
537 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700538
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700539 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900540 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700541}
542
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700543// A rule to route traffic based on a chosen outgoing interface.
544//
545// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
546// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900547WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
548 Permission permission, uid_t uidStart,
549 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700550 Fwmark fwmark;
551 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700552
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700553 fwmark.permission = permission;
554 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700555
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900556 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900557 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
558 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900559 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
560 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900561 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900562 UID_ROOT, UID_ROOT)) {
563 return ret;
564 }
565 }
566
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700567 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900568 fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700569}
570
571// A rule to route traffic based on the chosen network.
572//
573// This is for sockets that have not explicitly requested a particular network, but have been
574// bound to one when they called connect(). This ensures that sockets connected on a particular
575// network stay on that network even if the default network changes.
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900576WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700577 Fwmark fwmark;
578 Fwmark mask;
579
580 fwmark.netId = netId;
581 mask.netId = FWMARK_NET_ID_MASK;
582
583 fwmark.explicitlySelected = false;
584 mask.explicitlySelected = true;
585
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900586 fwmark.permission = PERMISSION_NONE;
587 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700588
589 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900590 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
591 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700592}
593
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700594// A rule to enable split tunnel VPNs.
595//
596// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
597// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
598// permissions required by the default network.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900599WARN_UNUSED_RESULT int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
600 const char* physicalInterface,
601 Permission permission) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700602 uint32_t table = getRouteTableForInterface(physicalInterface);
603 if (table == RT_TABLE_UNSPEC) {
604 return -ESRCH;
605 }
606
607 Fwmark fwmark;
608 Fwmark mask;
609
610 fwmark.netId = vpnNetId;
611 mask.netId = FWMARK_NET_ID_MASK;
612
613 fwmark.explicitlySelected = false;
614 mask.explicitlySelected = true;
615
616 fwmark.permission = permission;
617 mask.permission = permission;
618
619 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
620 mask.intValue);
621}
622
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700623// Add rules to allow legacy routes added through the requestRouteToHost() API.
624WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700625 Fwmark fwmark;
626 Fwmark mask;
627
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700628 fwmark.explicitlySelected = false;
629 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700630
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700631 // Rules to allow legacy routes to override the default network.
632 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
633 fwmark.intValue, mask.intValue)) {
634 return ret;
635 }
636 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
637 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
638 return ret;
639 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700640
641 fwmark.permission = PERMISSION_SYSTEM;
642 mask.permission = PERMISSION_SYSTEM;
643
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700644 // A rule to allow legacy routes from system apps to override VPNs.
645 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700646 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700647}
648
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700649// Add rules to lookup the local network when specified explicitly or otherwise.
650WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
651 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
652 INVALID_UID, INVALID_UID, ACTION_ADD)) {
653 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700654 }
655
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700656 Fwmark fwmark;
657 Fwmark mask;
658
659 fwmark.explicitlySelected = false;
660 mask.explicitlySelected = true;
661
662 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
663 fwmark.intValue, mask.intValue);
664}
665
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900666/* static */
667int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900668 const char *interface = DummyNetwork::INTERFACE_NAME;
669 uint32_t table = getRouteTableForInterface(interface);
670 if (table == RT_TABLE_UNSPEC) {
671 // getRouteTableForInterface has already looged an error.
672 return -ESRCH;
673 }
674
675 ifc_init();
676 int ret = ifc_up(interface);
677 ifc_close();
678 if (ret) {
679 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
680 return -errno;
681 }
682
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900683 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
684 INVALID_UID, INVALID_UID, ACTION_ADD))) {
685 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900686 return ret;
687 }
688
689 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900690 return ret;
691 }
692
693 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900694 return ret;
695 }
696
697 return 0;
698}
699
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900700// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
701// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
702// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
703// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700704WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100705 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
706 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700707}
708
709WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700710 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
711 return ret;
712 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900713 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
714 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700715}
716
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900717/* static */
718WARN_UNUSED_RESULT int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
719 Permission permission, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700720 uint32_t table = getRouteTableForInterface(interface);
721 if (table == RT_TABLE_UNSPEC) {
722 return -ESRCH;
723 }
724
725 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
726 return ret;
727 }
728 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
729 add)) {
730 return ret;
731 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900732 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700733 add)) {
734 return ret;
735 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900736
737 // Only set implicit rules for networks that don't require permissions.
738 //
739 // This is so that if the default network ceases to be the default network and then switches
740 // from requiring no permissions to requiring permissions, we ensure that apps only use the
741 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
742 // - it closes all sockets on the network except sockets that are explicitly selected.
743 //
744 // The lack of this rule only affects the special case above, because:
745 // - The only cases where we implicitly bind a socket to a network are the default network and
746 // the bypassable VPN that applies to the app, if any.
747 // - This rule doesn't affect VPNs because they don't support permissions at all.
748 // - The default network doesn't require permissions. While we support doing this, the framework
749 // never does it (partly because we'd end up in the situation where we tell apps that there is
750 // a default network, but they can't use it).
751 // - If the network is still the default network, the presence or absence of this rule does not
752 // matter.
753 //
754 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
755 // implicitly bound to a network because at the time of connect() it was the default, and that
756 // network must no longer be the default, and must now require permissions.
757 if (permission == PERMISSION_NONE) {
758 return modifyImplicitNetworkRule(netId, table, add);
759 }
760 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700761}
762
Robin Leeb8087362016-03-30 18:43:08 +0100763WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
764 Fwmark fwmark;
765 Fwmark mask;
766 fwmark.protectedFromVpn = false;
767 mask.protectedFromVpn = true;
768
Robin Leedc0d5782016-07-20 14:17:11 +0100769 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100770 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
771 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
772 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100773 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100774 return ret;
775 }
776 }
777
778 return 0;
779}
780
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900781WARN_UNUSED_RESULT int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
782 const UidRanges& uidRanges,
783 bool secure, bool add,
784 bool modifyNonUidBasedRules) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700785 uint32_t table = getRouteTableForInterface(interface);
786 if (table == RT_TABLE_UNSPEC) {
787 return -ESRCH;
788 }
789
Robin Leedc0d5782016-07-20 14:17:11 +0100790 for (const UidRange& range : uidRanges.getRanges()) {
791 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
792 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700793 return ret;
794 }
Robin Leedc0d5782016-07-20 14:17:11 +0100795 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
796 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700797 return ret;
798 }
Robin Leedc0d5782016-07-20 14:17:11 +0100799 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
800 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700801 return ret;
802 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700803 }
804
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700805 if (modifyNonUidBasedRules) {
806 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700807 return ret;
808 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700809 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
810 return ret;
811 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700812 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700813 return ret;
814 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700815 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700816 }
817
818 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700819}
820
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900821WARN_UNUSED_RESULT int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
822 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700823 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700824 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900825 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700826 }
827
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700828 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700829 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700830
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700831 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700832 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700833
834 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700835 mask.permission = permission;
836
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700837 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900838 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700839}
840
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900841WARN_UNUSED_RESULT int RouteController::modifyTetheredNetwork(uint16_t action,
842 const char* inputInterface,
843 const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700844 uint32_t table = getRouteTableForInterface(outputInterface);
845 if (table == RT_TABLE_UNSPEC) {
846 return -ESRCH;
847 }
848
849 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
850 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
851}
852
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900853// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900854// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900855WARN_UNUSED_RESULT int RouteController::modifyRoute(uint16_t action, const char* interface,
856 const char* destination, const char* nexthop,
857 TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700858 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700859 switch (tableType) {
860 case RouteController::INTERFACE: {
861 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700862 if (table == RT_TABLE_UNSPEC) {
863 return -ESRCH;
864 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700865 break;
866 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700867 case RouteController::LOCAL_NETWORK: {
868 table = ROUTE_TABLE_LOCAL_NETWORK;
869 break;
870 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700871 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700872 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700873 break;
874 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700875 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700876 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700877 break;
878 }
879 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700880
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900881 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700882 // Trying to add a route that already exists shouldn't cause an error.
883 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900884 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700885 }
886
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900887 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700888}
889
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900890WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
891 int ret = 0;
892 while (ret == 0) {
893 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
894 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
895 }
896
897 if (ret == -ENOENT) {
898 return 0;
899 } else {
900 return ret;
901 }
902}
903
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900904uint32_t getRulePriority(const nlmsghdr *nlh) {
905 return getRtmU32Attribute(nlh, FRA_PRIORITY);
906}
907
908uint32_t getRouteTable(const nlmsghdr *nlh) {
909 return getRtmU32Attribute(nlh, RTA_TABLE);
910}
911
912WARN_UNUSED_RESULT int flushRules() {
913 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
914 // Don't touch rules at priority 0 because by default they are used for local input.
915 return getRulePriority(nlh) != 0;
916 };
917 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
918}
919
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900920WARN_UNUSED_RESULT int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900921 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
922 return getRouteTable(nlh) == table;
923 };
924
925 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
926}
927
928// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900929WARN_UNUSED_RESULT int RouteController::flushRoutes(const char* interface) {
Luke Huangd1ee4622018-06-29 13:49:58 +0800930 std::lock_guard<std::shared_mutex> lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900931
932 uint32_t table = getRouteTableForInterfaceLocked(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900933 if (table == RT_TABLE_UNSPEC) {
934 return -ESRCH;
935 }
936
937 int ret = flushRoutes(table);
938
939 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
940 // track of its name.
941 if (ret == 0) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900942 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900943 }
944
945 return ret;
946}
947
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700948int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700949 if (int ret = flushRules()) {
950 return ret;
951 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700952 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700953 return ret;
954 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700955 if (int ret = addLocalNetworkRules(localNetId)) {
956 return ret;
957 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700958 if (int ret = addUnreachableRule()) {
959 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700960 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900961 // Don't complain if we can't add the dummy network, since not all devices support it.
962 configureDummyNetwork();
963
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700964 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700965 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700966}
967
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700968int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700969 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700970}
971
972int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700973 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700974}
975
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700976int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
977 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700978 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
979 return ret;
980 }
981 updateTableNamesFile();
982 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700983}
984
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700985int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
986 Permission permission) {
987 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700988 return ret;
989 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700990 if (int ret = flushRoutes(interface)) {
991 return ret;
992 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900993 if (int ret = clearTetheringRules(interface)) {
994 return ret;
995 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700996 updateTableNamesFile();
997 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700998}
999
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001000int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001001 bool secure, const UidRanges& uidRanges) {
1002 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001003 MODIFY_NON_UID_BASED_RULES)) {
1004 return ret;
1005 }
1006 updateTableNamesFile();
1007 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001008}
1009
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001010int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001011 bool secure, const UidRanges& uidRanges) {
1012 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001013 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001014 return ret;
1015 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001016 if (int ret = flushRoutes(interface)) {
1017 return ret;
1018 }
1019 updateTableNamesFile();
1020 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001021}
1022
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001023int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1024 Permission oldPermission,
1025 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001026 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001027 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001028 return ret;
1029 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001030 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001031}
1032
Robin Leeb8087362016-03-30 18:43:08 +01001033int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1034 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1035}
1036
1037int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1038 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1039}
1040
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001041int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001042 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001043 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001044 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001045}
1046
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001047int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001048 bool secure, const UidRanges& uidRanges) {
1049 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001050 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001051}
1052
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001053int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1054 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001055}
1056
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001057int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1058 Permission permission) {
1059 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001060}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001061
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001062int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001063 TableType tableType) {
1064 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001065}
1066
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001067int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001068 const char* nexthop, TableType tableType) {
1069 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001070}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001071
1072int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001073 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001074}
1075
1076int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001077 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001078}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001079
1080int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1081 Permission permission) {
1082 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1083}
1084
1085int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1086 const char* physicalInterface,
1087 Permission permission) {
1088 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1089}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001090
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001091// Protects sInterfaceToTable.
Luke Huangd1ee4622018-06-29 13:49:58 +08001092std::shared_mutex RouteController::sInterfaceToTableLock;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001093std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1094
Luke Huangd1ee4622018-06-29 13:49:58 +08001095
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001096} // namespace net
1097} // namespace android