blob: b1aa5aca14530d6f4710a090dd39c5803eac7e04 [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
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900157uint32_t RouteController::getRouteTableForInterface(const char* interface) {
158 android::RWLock::AutoRLock lock(sInterfaceToTableLock);
159 return getRouteTableForInterfaceLocked(interface);
160}
161
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700162void addTableName(uint32_t table, const std::string& name, std::string* contents) {
163 char tableString[UINT32_STRLEN];
164 snprintf(tableString, sizeof(tableString), "%u", table);
165 *contents += tableString;
166 *contents += " ";
167 *contents += name;
168 *contents += "\n";
169}
170
171// 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 +0900172void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700173 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700174
175 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
176 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
177
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700178 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700179 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
180 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700181
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900182 android::RWLock::AutoRLock lock(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900183 for (const auto& entry : sInterfaceToTable) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700184 addTableName(entry.second, entry.first, &contents);
185 }
186
Dan Albert5407e142015-03-16 10:05:59 -0700187 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800188 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700189 return;
190 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700191}
192
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700193// Returns 0 on success or negative errno on failure.
194int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
195 if (!input) {
196 *length = 0;
197 *padding = 0;
198 return 0;
199 }
200 *length = strlcpy(name, input, IFNAMSIZ) + 1;
201 if (*length > IFNAMSIZ) {
202 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
203 return -ENAMETOOLONG;
204 }
205 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
206 return 0;
207}
208
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700209// Adds or removes a routing rule for IPv4 and IPv6.
210//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900211// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100212// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700213// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
214// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700215// + If |iif| is non-NULL, the rule matches the specified incoming interface.
216// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
217// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
218// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900219//
220// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100221WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
222 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700223 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700224 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
225 if (fwmark & ~mask) {
226 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
227 return -ERANGE;
228 }
229
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700230 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900231 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700232 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
233 size_t iifLength, oifLength;
234 uint16_t iifPadding, oifPadding;
235 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
236 return ret;
237 }
238 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
239 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900240 }
241
Lorenzo Colitti59656512014-06-25 03:20:29 +0900242 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700243 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700244 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900245 return -EUSERS;
246 }
Robin Lee4ef94642016-04-01 11:50:49 +0100247
Lorenzo Colitti72723682014-06-26 13:51:10 +0900248 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900249
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900250 // Assemble a rule request and put it in an array of iovec structures.
251 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100252 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900253 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
254 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900255 };
256
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900257 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
258 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
259 // table 0, it's a wildcard that matches anything.
260 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
261 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
262 return -ENOTUNIQ;
263 }
264
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700265 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
266 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900267 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900268
269 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700270 { NULL, 0 },
271 { &rule, sizeof(rule) },
272 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
273 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700274 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
275 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700276 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
277 { &fwmark, mask ? sizeof(fwmark) : 0 },
278 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
279 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900280 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
281 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700282 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
283 { iifName, iifLength },
284 { PADDING_BUFFER, iifPadding },
285 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
286 { oifName, oifLength },
287 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900288 };
289
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900290 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700291 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
292 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900293 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900294 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
295 // Don't log when deleting a tethering rule that's not there. This matches the
296 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
297 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
298 strerror(-ret));
299 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900300 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700301 }
302 }
303
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900304 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700305}
306
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700307WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100308 uint32_t fwmark, uint32_t mask, const char* iif,
309 const char* oif, uid_t uidStart, uid_t uidEnd) {
310 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
311 uidEnd);
312}
313
314WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700315 uint32_t fwmark, uint32_t mask) {
316 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
317 INVALID_UID);
318}
319
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900320// Adds or deletes an IPv4 or IPv6 route.
321// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700322WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
323 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900324 // At least the destination must be non-null.
325 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700326 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900327 return -EFAULT;
328 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700329
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900330 // Parse the prefix.
331 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700332 uint8_t family;
333 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900334 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
335 &prefixLength);
336 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700337 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900338 return rawLength;
339 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700340
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900341 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700342 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900343 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
344 }
345
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700346 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900347 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900348 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700349
350 if (nexthop && !strcmp(nexthop, "unreachable")) {
351 type = RTN_UNREACHABLE;
352 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
353 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
354 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
355 interface = OIF_NONE;
356 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900357 } else if (nexthop && !strcmp(nexthop, "throw")) {
358 type = RTN_THROW;
359 interface = OIF_NONE;
360 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700361 } else {
362 // If an interface was specified, find the ifindex.
363 if (interface != OIF_NONE) {
364 ifindex = if_nametoindex(interface);
365 if (!ifindex) {
366 ALOGE("cannot find interface %s", interface);
367 return -ENODEV;
368 }
369 }
370
371 // If a nexthop was specified, parse it as the same family as the prefix.
372 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
373 ALOGE("inet_pton failed for nexthop %s", nexthop);
374 return -EINVAL;
375 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900376 }
377
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900378 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
379
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900380 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700381 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900382 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700383 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900384 .rtm_family = family,
385 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700386 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900387 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900388
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700389 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
390 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900391
392 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700393 { NULL, 0 },
394 { &route, sizeof(route) },
395 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
396 { &table, sizeof(table) },
397 { &rtaDst, sizeof(rtaDst) },
398 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700399 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
400 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700401 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
402 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900403 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
404 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900405 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900406
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900407 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_ROUTE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900408 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900409 if (ret) {
410 ALOGE("Error %s route %s -> %s %s to table %u: %s",
411 actionName(action), destination, nexthop, interface, table, strerror(-ret));
412 }
413 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700414}
415
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700416// An iptables rule to mark incoming packets on a network with the netId of the network.
417//
418// This is so that the kernel can:
419// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
420// replies, SYN-ACKs, etc).
421// + Mark sockets that accept connections from this interface so that the connection stays on the
422// same interface.
423WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
424 Permission permission, bool add) {
425 Fwmark fwmark;
426
427 fwmark.netId = netId;
428 fwmark.explicitlySelected = true;
429 fwmark.protectedFromVpn = true;
430 fwmark.permission = permission;
431
Benedict Wongb9baf262017-12-03 15:43:08 -0800432 const uint32_t mask = ~Fwmark::getUidBillingMask();
433
434 std::string cmd = StringPrintf(
435 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
436 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900437 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700438 ALOGE("failed to change iptables rule that sets incoming packet mark");
439 return -EREMOTEIO;
440 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700441
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700442 return 0;
443}
444
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700445// A rule to route responses to the local network forwarded via the VPN.
446//
447// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
448// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
449WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
450 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
451 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
452 INVALID_UID, INVALID_UID);
453}
454
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700455// A rule to route all traffic from a given set of UIDs to go over the VPN.
456//
457// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
458// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
459// bypass the VPN if the protectedFromVpn bit is set.
460WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700461 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700462 Fwmark fwmark;
463 Fwmark mask;
464
465 fwmark.protectedFromVpn = false;
466 mask.protectedFromVpn = true;
467
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700468 uint32_t priority;
469
470 if (secure) {
471 priority = RULE_PRIORITY_SECURE_VPN;
472 } else {
473 priority = RULE_PRIORITY_BYPASSABLE_VPN;
474
475 fwmark.explicitlySelected = false;
476 mask.explicitlySelected = true;
477 }
478
479 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900480 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700481}
482
483// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
484// set of UIDs.
485//
486// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
487// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700488WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
489 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700490 Fwmark fwmark;
491 Fwmark mask;
492
493 fwmark.netId = netId;
494 mask.netId = FWMARK_NET_ID_MASK;
495
496 fwmark.permission = PERMISSION_SYSTEM;
497 mask.permission = PERMISSION_SYSTEM;
498
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700499 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
500
501 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
502 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700503}
504
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700505// A rule to route traffic based on an explicitly chosen network.
506//
507// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
508//
509// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
510// to check it again in the rules here, because a network's permissions may have been updated via
511// modifyNetworkPermission().
512WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
513 Permission permission, uid_t uidStart,
514 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700515 Fwmark fwmark;
516 Fwmark mask;
517
518 fwmark.netId = netId;
519 mask.netId = FWMARK_NET_ID_MASK;
520
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700521 fwmark.explicitlySelected = true;
522 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700523
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700524 fwmark.permission = permission;
525 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700526
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700527 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900528 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700529}
530
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700531// A rule to route traffic based on a chosen outgoing interface.
532//
533// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
534// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900535WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
536 Permission permission, uid_t uidStart,
537 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700538 Fwmark fwmark;
539 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700540
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700541 fwmark.permission = permission;
542 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700543
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900544 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900545 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
546 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900547 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
548 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900549 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900550 UID_ROOT, UID_ROOT)) {
551 return ret;
552 }
553 }
554
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700555 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900556 fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700557}
558
559// A rule to route traffic based on the chosen network.
560//
561// This is for sockets that have not explicitly requested a particular network, but have been
562// bound to one when they called connect(). This ensures that sockets connected on a particular
563// network stay on that network even if the default network changes.
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900564WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700565 Fwmark fwmark;
566 Fwmark mask;
567
568 fwmark.netId = netId;
569 mask.netId = FWMARK_NET_ID_MASK;
570
571 fwmark.explicitlySelected = false;
572 mask.explicitlySelected = true;
573
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900574 fwmark.permission = PERMISSION_NONE;
575 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700576
577 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900578 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
579 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700580}
581
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700582// A rule to enable split tunnel VPNs.
583//
584// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
585// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
586// permissions required by the default network.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900587WARN_UNUSED_RESULT int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
588 const char* physicalInterface,
589 Permission permission) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700590 uint32_t table = getRouteTableForInterface(physicalInterface);
591 if (table == RT_TABLE_UNSPEC) {
592 return -ESRCH;
593 }
594
595 Fwmark fwmark;
596 Fwmark mask;
597
598 fwmark.netId = vpnNetId;
599 mask.netId = FWMARK_NET_ID_MASK;
600
601 fwmark.explicitlySelected = false;
602 mask.explicitlySelected = true;
603
604 fwmark.permission = permission;
605 mask.permission = permission;
606
607 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
608 mask.intValue);
609}
610
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700611// Add rules to allow legacy routes added through the requestRouteToHost() API.
612WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700613 Fwmark fwmark;
614 Fwmark mask;
615
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700616 fwmark.explicitlySelected = false;
617 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700618
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700619 // Rules to allow legacy routes to override the default network.
620 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
621 fwmark.intValue, mask.intValue)) {
622 return ret;
623 }
624 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
625 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
626 return ret;
627 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700628
629 fwmark.permission = PERMISSION_SYSTEM;
630 mask.permission = PERMISSION_SYSTEM;
631
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700632 // A rule to allow legacy routes from system apps to override VPNs.
633 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700634 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700635}
636
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700637// Add rules to lookup the local network when specified explicitly or otherwise.
638WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
639 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
640 INVALID_UID, INVALID_UID, ACTION_ADD)) {
641 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700642 }
643
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700644 Fwmark fwmark;
645 Fwmark mask;
646
647 fwmark.explicitlySelected = false;
648 mask.explicitlySelected = true;
649
650 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
651 fwmark.intValue, mask.intValue);
652}
653
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900654/* static */
655int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900656 const char *interface = DummyNetwork::INTERFACE_NAME;
657 uint32_t table = getRouteTableForInterface(interface);
658 if (table == RT_TABLE_UNSPEC) {
659 // getRouteTableForInterface has already looged an error.
660 return -ESRCH;
661 }
662
663 ifc_init();
664 int ret = ifc_up(interface);
665 ifc_close();
666 if (ret) {
667 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
668 return -errno;
669 }
670
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900671 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
672 INVALID_UID, INVALID_UID, ACTION_ADD))) {
673 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900674 return ret;
675 }
676
677 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900678 return ret;
679 }
680
681 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900682 return ret;
683 }
684
685 return 0;
686}
687
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900688// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
689// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
690// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
691// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700692WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100693 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
694 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700695}
696
697WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700698 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
699 return ret;
700 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900701 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
702 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700703}
704
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900705/* static */
706WARN_UNUSED_RESULT int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
707 Permission permission, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700708 uint32_t table = getRouteTableForInterface(interface);
709 if (table == RT_TABLE_UNSPEC) {
710 return -ESRCH;
711 }
712
713 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
714 return ret;
715 }
716 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
717 add)) {
718 return ret;
719 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900720 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700721 add)) {
722 return ret;
723 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900724
725 // Only set implicit rules for networks that don't require permissions.
726 //
727 // This is so that if the default network ceases to be the default network and then switches
728 // from requiring no permissions to requiring permissions, we ensure that apps only use the
729 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
730 // - it closes all sockets on the network except sockets that are explicitly selected.
731 //
732 // The lack of this rule only affects the special case above, because:
733 // - The only cases where we implicitly bind a socket to a network are the default network and
734 // the bypassable VPN that applies to the app, if any.
735 // - This rule doesn't affect VPNs because they don't support permissions at all.
736 // - The default network doesn't require permissions. While we support doing this, the framework
737 // never does it (partly because we'd end up in the situation where we tell apps that there is
738 // a default network, but they can't use it).
739 // - If the network is still the default network, the presence or absence of this rule does not
740 // matter.
741 //
742 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
743 // implicitly bound to a network because at the time of connect() it was the default, and that
744 // network must no longer be the default, and must now require permissions.
745 if (permission == PERMISSION_NONE) {
746 return modifyImplicitNetworkRule(netId, table, add);
747 }
748 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700749}
750
Robin Leeb8087362016-03-30 18:43:08 +0100751WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
752 Fwmark fwmark;
753 Fwmark mask;
754 fwmark.protectedFromVpn = false;
755 mask.protectedFromVpn = true;
756
Robin Leedc0d5782016-07-20 14:17:11 +0100757 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100758 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
759 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
760 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100761 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100762 return ret;
763 }
764 }
765
766 return 0;
767}
768
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900769WARN_UNUSED_RESULT int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
770 const UidRanges& uidRanges,
771 bool secure, bool add,
772 bool modifyNonUidBasedRules) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700773 uint32_t table = getRouteTableForInterface(interface);
774 if (table == RT_TABLE_UNSPEC) {
775 return -ESRCH;
776 }
777
Robin Leedc0d5782016-07-20 14:17:11 +0100778 for (const UidRange& range : uidRanges.getRanges()) {
779 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
780 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700781 return ret;
782 }
Robin Leedc0d5782016-07-20 14:17:11 +0100783 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
784 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700785 return ret;
786 }
Robin Leedc0d5782016-07-20 14:17:11 +0100787 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
788 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700789 return ret;
790 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700791 }
792
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700793 if (modifyNonUidBasedRules) {
794 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700795 return ret;
796 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700797 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
798 return ret;
799 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700800 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700801 return ret;
802 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700803 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700804 }
805
806 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700807}
808
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900809WARN_UNUSED_RESULT int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
810 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700811 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700812 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900813 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700814 }
815
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700816 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700817 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700818
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700819 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700820 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700821
822 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700823 mask.permission = permission;
824
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700825 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900826 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700827}
828
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900829WARN_UNUSED_RESULT int RouteController::modifyTetheredNetwork(uint16_t action,
830 const char* inputInterface,
831 const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700832 uint32_t table = getRouteTableForInterface(outputInterface);
833 if (table == RT_TABLE_UNSPEC) {
834 return -ESRCH;
835 }
836
837 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
838 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
839}
840
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900841// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900842// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900843WARN_UNUSED_RESULT int RouteController::modifyRoute(uint16_t action, const char* interface,
844 const char* destination, const char* nexthop,
845 TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700846 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700847 switch (tableType) {
848 case RouteController::INTERFACE: {
849 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700850 if (table == RT_TABLE_UNSPEC) {
851 return -ESRCH;
852 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700853 break;
854 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700855 case RouteController::LOCAL_NETWORK: {
856 table = ROUTE_TABLE_LOCAL_NETWORK;
857 break;
858 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700859 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700860 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700861 break;
862 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700863 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700864 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700865 break;
866 }
867 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700868
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900869 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700870 // Trying to add a route that already exists shouldn't cause an error.
871 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900872 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700873 }
874
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900875 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700876}
877
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900878WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
879 int ret = 0;
880 while (ret == 0) {
881 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
882 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
883 }
884
885 if (ret == -ENOENT) {
886 return 0;
887 } else {
888 return ret;
889 }
890}
891
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900892uint32_t getRulePriority(const nlmsghdr *nlh) {
893 return getRtmU32Attribute(nlh, FRA_PRIORITY);
894}
895
896uint32_t getRouteTable(const nlmsghdr *nlh) {
897 return getRtmU32Attribute(nlh, RTA_TABLE);
898}
899
900WARN_UNUSED_RESULT int flushRules() {
901 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
902 // Don't touch rules at priority 0 because by default they are used for local input.
903 return getRulePriority(nlh) != 0;
904 };
905 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
906}
907
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900908WARN_UNUSED_RESULT int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900909 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
910 return getRouteTable(nlh) == table;
911 };
912
913 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
914}
915
916// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900917WARN_UNUSED_RESULT int RouteController::flushRoutes(const char* interface) {
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900918 android::RWLock::AutoWLock lock(sInterfaceToTableLock);
919
920 uint32_t table = getRouteTableForInterfaceLocked(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900921 if (table == RT_TABLE_UNSPEC) {
922 return -ESRCH;
923 }
924
925 int ret = flushRoutes(table);
926
927 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
928 // track of its name.
929 if (ret == 0) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900930 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900931 }
932
933 return ret;
934}
935
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700936int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700937 if (int ret = flushRules()) {
938 return ret;
939 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700940 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700941 return ret;
942 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700943 if (int ret = addLocalNetworkRules(localNetId)) {
944 return ret;
945 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700946 if (int ret = addUnreachableRule()) {
947 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700948 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900949 // Don't complain if we can't add the dummy network, since not all devices support it.
950 configureDummyNetwork();
951
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700952 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700953 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700954}
955
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700956int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700957 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700958}
959
960int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700961 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700962}
963
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700964int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
965 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700966 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
967 return ret;
968 }
969 updateTableNamesFile();
970 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700971}
972
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700973int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
974 Permission permission) {
975 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700976 return ret;
977 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700978 if (int ret = flushRoutes(interface)) {
979 return ret;
980 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900981 if (int ret = clearTetheringRules(interface)) {
982 return ret;
983 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700984 updateTableNamesFile();
985 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700986}
987
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700988int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700989 bool secure, const UidRanges& uidRanges) {
990 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700991 MODIFY_NON_UID_BASED_RULES)) {
992 return ret;
993 }
994 updateTableNamesFile();
995 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700996}
997
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700998int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700999 bool secure, const UidRanges& uidRanges) {
1000 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001001 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001002 return ret;
1003 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001004 if (int ret = flushRoutes(interface)) {
1005 return ret;
1006 }
1007 updateTableNamesFile();
1008 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001009}
1010
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001011int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1012 Permission oldPermission,
1013 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001014 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001015 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001016 return ret;
1017 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001018 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001019}
1020
Robin Leeb8087362016-03-30 18:43:08 +01001021int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1022 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1023}
1024
1025int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1026 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1027}
1028
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001029int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001030 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001031 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001032 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001033}
1034
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001035int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001036 bool secure, const UidRanges& uidRanges) {
1037 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001038 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001039}
1040
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001041int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1042 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001043}
1044
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001045int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1046 Permission permission) {
1047 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001048}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001049
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001050int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001051 TableType tableType) {
1052 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001053}
1054
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001055int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001056 const char* nexthop, TableType tableType) {
1057 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001058}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001059
1060int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001061 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001062}
1063
1064int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001065 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001066}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001067
1068int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1069 Permission permission) {
1070 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1071}
1072
1073int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1074 const char* physicalInterface,
1075 Permission permission) {
1076 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1077}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001078
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001079// Protects sInterfaceToTable.
1080android::RWLock RouteController::sInterfaceToTableLock;
1081
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001082std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1083
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001084} // namespace net
1085} // namespace android