blob: ad6f6a6a104148c50ec0dab4256468c71f58c81e [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
Luke Huang94658ac2018-10-18 19:35:12 +090030#define LOG_TAG "Netd"
31
Lorenzo Colitti7035f222017-02-13 18:29:00 +090032#include "DummyNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070033#include "Fwmark.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090034#include "NetdConstants.h"
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090035#include "NetlinkCommands.h"
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070036#include "UidRanges.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070037
Bernie Innocenti189eb502018-10-01 23:10:18 +090038#include <android-base/file.h>
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090039#include <android-base/stringprintf.h>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070040#include "log/log.h"
41#include "logwrap/logwrap.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090042#include "netid_client.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090043#include "netutils/ifc.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070044
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090045using android::base::StringPrintf;
Dan Albert5407e142015-03-16 10:05:59 -070046using android::base::WriteStringToFile;
Luke Huang94658ac2018-10-18 19:35:12 +090047using android::net::UidRangeParcel;
Dan Albert5407e142015-03-16 10:05:59 -070048
Lorenzo Colitti7035f222017-02-13 18:29:00 +090049namespace android {
50namespace net {
51
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090052auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand;
53
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070054// BEGIN CONSTANTS --------------------------------------------------------------------------------
55
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070056const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090057const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070058const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070059const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010060const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070061const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
62const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
63const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
64const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070065const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070066const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070067const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070068const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070069const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070070const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090071const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070072
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070073const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070074const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
75const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
76
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070077const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070078const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
79const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
80
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070081const char* const ROUTE_TABLE_NAME_LOCAL = "local";
82const char* const ROUTE_TABLE_NAME_MAIN = "main";
83
Lorenzo Colittib5d19e92017-09-25 18:39:33 +090084// None of our regular routes specify priority, which causes them to have the default priority.
85// For default throw routes, we use a fixed priority of 100000.
Lorenzo Colitti5e03a892017-09-08 11:31:59 +090086uint32_t PRIO_THROW = 100000;
87
Lorenzo Colittid78843e2017-03-27 05:52:31 +090088const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
89
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070090const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
91
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070092const uid_t UID_ROOT = 0;
Lorenzo Colitti3093f562017-09-25 14:17:38 +090093const uint32_t FWMARK_NONE = 0;
94const uint32_t MASK_NONE = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090095const char* const IIF_LOOPBACK = "lo";
Yi Kongbdfd57e2018-07-25 13:26:10 -070096const char* const IIF_NONE = nullptr;
97const char* const OIF_NONE = nullptr;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070098const bool ACTION_ADD = true;
99const bool ACTION_DEL = false;
100const bool MODIFY_NON_UID_BASED_RULES = true;
101
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700102const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700103const 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 -0700104
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700105// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
106// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
107constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
108 return RTA_LENGTH(x);
109}
110
111// These are practically const, but can't be declared so, because they are used to initialize
112// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900113rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
114rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
115rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
116rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900117rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700118
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900119rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
120rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900121rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700122
123uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
124
125// END CONSTANTS ----------------------------------------------------------------------------------
126
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900127const char *actionName(uint16_t action) {
128 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
129 return ops[action % 4];
130}
131
132const char *familyName(uint8_t family) {
133 switch (family) {
134 case AF_INET: return "IPv4";
135 case AF_INET6: return "IPv6";
136 default: return "???";
137 }
138}
139
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900140// Caller must hold sInterfaceToTableLock.
141uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700142 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400143 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700144 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900145 sInterfaceToTable[interface] = index;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700146 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400147 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700148 // If the interface goes away if_nametoindex() will return 0 but we still need to know
149 // the index so we can remove the rules and routes.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900150 auto iter = sInterfaceToTable.find(interface);
151 if (iter == sInterfaceToTable.end()) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700152 ALOGE("cannot find interface %s", interface);
153 return RT_TABLE_UNSPEC;
154 }
155 return iter->second;
156}
157
Rubin Xu6c00b612018-04-27 14:27:59 +0100158uint32_t RouteController::getIfIndex(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900159 std::lock_guard lock(sInterfaceToTableLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100160
161 auto iter = sInterfaceToTable.find(interface);
162 if (iter == sInterfaceToTable.end()) {
163 ALOGE("getIfIndex: cannot find interface %s", interface);
164 return 0;
165 }
166
167 return iter->second - ROUTE_TABLE_OFFSET_FROM_INDEX;
168}
169
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900170uint32_t RouteController::getRouteTableForInterface(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900171 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900172 return getRouteTableForInterfaceLocked(interface);
173}
174
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700175void addTableName(uint32_t table, const std::string& name, std::string* contents) {
176 char tableString[UINT32_STRLEN];
177 snprintf(tableString, sizeof(tableString), "%u", table);
178 *contents += tableString;
179 *contents += " ";
180 *contents += name;
181 *contents += "\n";
182}
183
184// 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 +0900185void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700186 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700187
188 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
189 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
190
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700191 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700192 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
193 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700194
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900195 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900196 for (const auto& entry : sInterfaceToTable) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700197 addTableName(entry.second, entry.first, &contents);
198 }
199
Dan Albert5407e142015-03-16 10:05:59 -0700200 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800201 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700202 return;
203 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700204}
205
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700206// Returns 0 on success or negative errno on failure.
207int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
208 if (!input) {
209 *length = 0;
210 *padding = 0;
211 return 0;
212 }
213 *length = strlcpy(name, input, IFNAMSIZ) + 1;
214 if (*length > IFNAMSIZ) {
215 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
216 return -ENAMETOOLONG;
217 }
218 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
219 return 0;
220}
221
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700222// Adds or removes a routing rule for IPv4 and IPv6.
223//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900224// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100225// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700226// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
227// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700228// + If |iif| is non-NULL, the rule matches the specified incoming interface.
229// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
230// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
231// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900232//
233// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100234WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
235 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700236 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700237 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
238 if (fwmark & ~mask) {
239 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
240 return -ERANGE;
241 }
242
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700243 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900244 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700245 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
246 size_t iifLength, oifLength;
247 uint16_t iifPadding, oifPadding;
248 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
249 return ret;
250 }
251 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
252 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900253 }
254
Lorenzo Colitti59656512014-06-25 03:20:29 +0900255 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700256 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700257 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900258 return -EUSERS;
259 }
Robin Lee4ef94642016-04-01 11:50:49 +0100260
Lorenzo Colitti72723682014-06-26 13:51:10 +0900261 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900262
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900263 // Assemble a rule request and put it in an array of iovec structures.
264 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100265 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900266 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
267 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900268 };
269
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900270 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
271 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
272 // table 0, it's a wildcard that matches anything.
273 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
274 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
275 return -ENOTUNIQ;
276 }
277
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700278 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
279 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900280 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900281
282 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700283 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700284 { &rule, sizeof(rule) },
285 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
286 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700287 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
288 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700289 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
290 { &fwmark, mask ? sizeof(fwmark) : 0 },
291 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
292 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900293 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
294 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700295 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
296 { iifName, iifLength },
297 { PADDING_BUFFER, iifPadding },
298 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
299 { oifName, oifLength },
300 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900301 };
302
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900303 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700304 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
305 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900306 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900307 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
308 // Don't log when deleting a tethering rule that's not there. This matches the
309 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
310 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
311 strerror(-ret));
312 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900313 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700314 }
315 }
316
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900317 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700318}
319
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700320WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100321 uint32_t fwmark, uint32_t mask, const char* iif,
322 const char* oif, uid_t uidStart, uid_t uidEnd) {
323 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
324 uidEnd);
325}
326
327WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700328 uint32_t fwmark, uint32_t mask) {
329 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
330 INVALID_UID);
331}
332
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900333// Adds or deletes an IPv4 or IPv6 route.
334// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700335WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
336 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900337 // At least the destination must be non-null.
338 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700339 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900340 return -EFAULT;
341 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700342
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900343 // Parse the prefix.
344 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700345 uint8_t family;
346 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900347 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
348 &prefixLength);
349 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700350 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900351 return rawLength;
352 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700353
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900354 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700355 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900356 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
357 }
358
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700359 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900360 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900361 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700362
363 if (nexthop && !strcmp(nexthop, "unreachable")) {
364 type = RTN_UNREACHABLE;
365 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
366 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
367 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
368 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700369 nexthop = nullptr;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900370 } else if (nexthop && !strcmp(nexthop, "throw")) {
371 type = RTN_THROW;
372 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700373 nexthop = nullptr;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700374 } else {
375 // If an interface was specified, find the ifindex.
376 if (interface != OIF_NONE) {
377 ifindex = if_nametoindex(interface);
378 if (!ifindex) {
379 ALOGE("cannot find interface %s", interface);
380 return -ENODEV;
381 }
382 }
383
384 // If a nexthop was specified, parse it as the same family as the prefix.
385 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
386 ALOGE("inet_pton failed for nexthop %s", nexthop);
387 return -EINVAL;
388 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900389 }
390
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900391 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
392
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900393 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700394 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900395 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700396 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900397 .rtm_family = family,
398 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700399 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900400 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900401
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700402 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
403 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900404
405 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700406 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700407 { &route, sizeof(route) },
408 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
409 { &table, sizeof(table) },
410 { &rtaDst, sizeof(rtaDst) },
411 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700412 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
413 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700414 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
415 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900416 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
417 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900418 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900419
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900420 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_ROUTE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900421 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900422 if (ret) {
423 ALOGE("Error %s route %s -> %s %s to table %u: %s",
424 actionName(action), destination, nexthop, interface, table, strerror(-ret));
425 }
426 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700427}
428
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700429// An iptables rule to mark incoming packets on a network with the netId of the network.
430//
431// This is so that the kernel can:
432// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
433// replies, SYN-ACKs, etc).
434// + Mark sockets that accept connections from this interface so that the connection stays on the
435// same interface.
436WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
437 Permission permission, bool add) {
438 Fwmark fwmark;
439
440 fwmark.netId = netId;
441 fwmark.explicitlySelected = true;
442 fwmark.protectedFromVpn = true;
443 fwmark.permission = permission;
444
Benedict Wongb9baf262017-12-03 15:43:08 -0800445 const uint32_t mask = ~Fwmark::getUidBillingMask();
446
447 std::string cmd = StringPrintf(
448 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
449 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900450 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700451 ALOGE("failed to change iptables rule that sets incoming packet mark");
452 return -EREMOTEIO;
453 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700454
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700455 return 0;
456}
457
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700458// A rule to route responses to the local network forwarded via the VPN.
459//
460// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
461// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
462WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
463 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
464 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
465 INVALID_UID, INVALID_UID);
466}
467
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700468// A rule to route all traffic from a given set of UIDs to go over the VPN.
469//
470// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
471// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
472// bypass the VPN if the protectedFromVpn bit is set.
473WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700474 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700475 Fwmark fwmark;
476 Fwmark mask;
477
478 fwmark.protectedFromVpn = false;
479 mask.protectedFromVpn = true;
480
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700481 uint32_t priority;
482
483 if (secure) {
484 priority = RULE_PRIORITY_SECURE_VPN;
485 } else {
486 priority = RULE_PRIORITY_BYPASSABLE_VPN;
487
488 fwmark.explicitlySelected = false;
489 mask.explicitlySelected = true;
490 }
491
492 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900493 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700494}
495
496// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
497// set of UIDs.
498//
499// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
500// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700501WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
502 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700503 Fwmark fwmark;
504 Fwmark mask;
505
506 fwmark.netId = netId;
507 mask.netId = FWMARK_NET_ID_MASK;
508
509 fwmark.permission = PERMISSION_SYSTEM;
510 mask.permission = PERMISSION_SYSTEM;
511
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700512 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
513
514 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
515 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700516}
517
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700518// A rule to route traffic based on an explicitly chosen network.
519//
520// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
521//
522// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
523// to check it again in the rules here, because a network's permissions may have been updated via
524// modifyNetworkPermission().
525WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
526 Permission permission, uid_t uidStart,
527 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700528 Fwmark fwmark;
529 Fwmark mask;
530
531 fwmark.netId = netId;
532 mask.netId = FWMARK_NET_ID_MASK;
533
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700534 fwmark.explicitlySelected = true;
535 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700536
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700537 fwmark.permission = permission;
538 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700539
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700540 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900541 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700542}
543
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700544// A rule to route traffic based on a chosen outgoing interface.
545//
546// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
547// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900548WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
549 Permission permission, uid_t uidStart,
550 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700551 Fwmark fwmark;
552 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700553
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700554 fwmark.permission = permission;
555 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700556
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900557 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900558 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
559 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900560 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
561 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900562 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900563 UID_ROOT, UID_ROOT)) {
564 return ret;
565 }
566 }
567
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700568 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900569 fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700570}
571
572// A rule to route traffic based on the chosen network.
573//
574// This is for sockets that have not explicitly requested a particular network, but have been
575// bound to one when they called connect(). This ensures that sockets connected on a particular
576// network stay on that network even if the default network changes.
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900577WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700578 Fwmark fwmark;
579 Fwmark mask;
580
581 fwmark.netId = netId;
582 mask.netId = FWMARK_NET_ID_MASK;
583
584 fwmark.explicitlySelected = false;
585 mask.explicitlySelected = true;
586
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900587 fwmark.permission = PERMISSION_NONE;
588 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700589
590 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900591 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
592 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700593}
594
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700595// A rule to enable split tunnel VPNs.
596//
597// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
598// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
599// permissions required by the default network.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900600WARN_UNUSED_RESULT int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
601 const char* physicalInterface,
602 Permission permission) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700603 uint32_t table = getRouteTableForInterface(physicalInterface);
604 if (table == RT_TABLE_UNSPEC) {
605 return -ESRCH;
606 }
607
608 Fwmark fwmark;
609 Fwmark mask;
610
611 fwmark.netId = vpnNetId;
612 mask.netId = FWMARK_NET_ID_MASK;
613
614 fwmark.explicitlySelected = false;
615 mask.explicitlySelected = true;
616
617 fwmark.permission = permission;
618 mask.permission = permission;
619
620 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
621 mask.intValue);
622}
623
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700624// Add rules to allow legacy routes added through the requestRouteToHost() API.
625WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700626 Fwmark fwmark;
627 Fwmark mask;
628
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700629 fwmark.explicitlySelected = false;
630 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700631
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700632 // Rules to allow legacy routes to override the default network.
633 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
634 fwmark.intValue, mask.intValue)) {
635 return ret;
636 }
637 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
638 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
639 return ret;
640 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700641
642 fwmark.permission = PERMISSION_SYSTEM;
643 mask.permission = PERMISSION_SYSTEM;
644
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700645 // A rule to allow legacy routes from system apps to override VPNs.
646 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700647 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700648}
649
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700650// Add rules to lookup the local network when specified explicitly or otherwise.
651WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
652 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
653 INVALID_UID, INVALID_UID, ACTION_ADD)) {
654 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700655 }
656
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700657 Fwmark fwmark;
658 Fwmark mask;
659
660 fwmark.explicitlySelected = false;
661 mask.explicitlySelected = true;
662
663 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
664 fwmark.intValue, mask.intValue);
665}
666
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900667/* static */
668int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900669 const char *interface = DummyNetwork::INTERFACE_NAME;
670 uint32_t table = getRouteTableForInterface(interface);
671 if (table == RT_TABLE_UNSPEC) {
672 // getRouteTableForInterface has already looged an error.
673 return -ESRCH;
674 }
675
676 ifc_init();
677 int ret = ifc_up(interface);
678 ifc_close();
679 if (ret) {
680 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
681 return -errno;
682 }
683
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900684 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
685 INVALID_UID, INVALID_UID, ACTION_ADD))) {
686 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900687 return ret;
688 }
689
Yi Kongbdfd57e2018-07-25 13:26:10 -0700690 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", nullptr))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900691 return ret;
692 }
693
Yi Kongbdfd57e2018-07-25 13:26:10 -0700694 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", nullptr))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900695 return ret;
696 }
697
698 return 0;
699}
700
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900701// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
702// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
703// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
704// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700705WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100706 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
707 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700708}
709
710WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700711 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
712 return ret;
713 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900714 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
715 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700716}
717
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900718/* static */
719WARN_UNUSED_RESULT int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
720 Permission permission, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700721 uint32_t table = getRouteTableForInterface(interface);
722 if (table == RT_TABLE_UNSPEC) {
723 return -ESRCH;
724 }
725
726 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
727 return ret;
728 }
729 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
730 add)) {
731 return ret;
732 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900733 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700734 add)) {
735 return ret;
736 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900737
738 // Only set implicit rules for networks that don't require permissions.
739 //
740 // This is so that if the default network ceases to be the default network and then switches
741 // from requiring no permissions to requiring permissions, we ensure that apps only use the
742 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
743 // - it closes all sockets on the network except sockets that are explicitly selected.
744 //
745 // The lack of this rule only affects the special case above, because:
746 // - The only cases where we implicitly bind a socket to a network are the default network and
747 // the bypassable VPN that applies to the app, if any.
748 // - This rule doesn't affect VPNs because they don't support permissions at all.
749 // - The default network doesn't require permissions. While we support doing this, the framework
750 // never does it (partly because we'd end up in the situation where we tell apps that there is
751 // a default network, but they can't use it).
752 // - If the network is still the default network, the presence or absence of this rule does not
753 // matter.
754 //
755 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
756 // implicitly bound to a network because at the time of connect() it was the default, and that
757 // network must no longer be the default, and must now require permissions.
758 if (permission == PERMISSION_NONE) {
759 return modifyImplicitNetworkRule(netId, table, add);
760 }
761 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700762}
763
Robin Leeb8087362016-03-30 18:43:08 +0100764WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
765 Fwmark fwmark;
766 Fwmark mask;
767 fwmark.protectedFromVpn = false;
768 mask.protectedFromVpn = true;
769
Luke Huang94658ac2018-10-18 19:35:12 +0900770 for (const UidRangeParcel& range : uidRanges.getRanges()) {
771 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN,
772 FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
773 IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) {
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
Luke Huang94658ac2018-10-18 19:35:12 +0900790 for (const UidRangeParcel& range : uidRanges.getRanges()) {
791 if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, secure, add)) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700792 return ret;
793 }
Luke Huang94658ac2018-10-18 19:35:12 +0900794 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start,
795 range.stop, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700796 return ret;
797 }
Luke Huang94658ac2018-10-18 19:35:12 +0900798 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start,
799 range.stop, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700800 return ret;
801 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700802 }
803
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700804 if (modifyNonUidBasedRules) {
805 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700806 return ret;
807 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700808 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
809 return ret;
810 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700811 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700812 return ret;
813 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700814 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700815 }
816
817 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700818}
819
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900820WARN_UNUSED_RESULT int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
821 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700822 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700823 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900824 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700825 }
826
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700827 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700828 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700829
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700830 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700831 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700832
833 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700834 mask.permission = permission;
835
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700836 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900837 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700838}
839
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900840WARN_UNUSED_RESULT int RouteController::modifyTetheredNetwork(uint16_t action,
841 const char* inputInterface,
842 const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700843 uint32_t table = getRouteTableForInterface(outputInterface);
844 if (table == RT_TABLE_UNSPEC) {
845 return -ESRCH;
846 }
847
848 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
849 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
850}
851
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900852// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900853// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900854WARN_UNUSED_RESULT int RouteController::modifyRoute(uint16_t action, const char* interface,
855 const char* destination, const char* nexthop,
856 TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700857 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700858 switch (tableType) {
859 case RouteController::INTERFACE: {
860 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700861 if (table == RT_TABLE_UNSPEC) {
862 return -ESRCH;
863 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700864 break;
865 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700866 case RouteController::LOCAL_NETWORK: {
867 table = ROUTE_TABLE_LOCAL_NETWORK;
868 break;
869 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700870 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700871 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700872 break;
873 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700874 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700875 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700876 break;
877 }
878 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700879
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900880 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700881 // Trying to add a route that already exists shouldn't cause an error.
882 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900883 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700884 }
885
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900886 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700887}
888
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900889WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
890 int ret = 0;
891 while (ret == 0) {
892 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
893 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
894 }
895
896 if (ret == -ENOENT) {
897 return 0;
898 } else {
899 return ret;
900 }
901}
902
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900903uint32_t getRulePriority(const nlmsghdr *nlh) {
904 return getRtmU32Attribute(nlh, FRA_PRIORITY);
905}
906
907uint32_t getRouteTable(const nlmsghdr *nlh) {
908 return getRtmU32Attribute(nlh, RTA_TABLE);
909}
910
911WARN_UNUSED_RESULT int flushRules() {
912 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
913 // Don't touch rules at priority 0 because by default they are used for local input.
914 return getRulePriority(nlh) != 0;
915 };
916 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
917}
918
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900919WARN_UNUSED_RESULT int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900920 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
921 return getRouteTable(nlh) == table;
922 };
923
924 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
925}
926
927// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900928WARN_UNUSED_RESULT int RouteController::flushRoutes(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900929 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900930
931 uint32_t table = getRouteTableForInterfaceLocked(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900932 if (table == RT_TABLE_UNSPEC) {
933 return -ESRCH;
934 }
935
936 int ret = flushRoutes(table);
937
938 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
939 // track of its name.
940 if (ret == 0) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900941 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900942 }
943
944 return ret;
945}
946
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700947int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700948 if (int ret = flushRules()) {
949 return ret;
950 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700951 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700952 return ret;
953 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700954 if (int ret = addLocalNetworkRules(localNetId)) {
955 return ret;
956 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700957 if (int ret = addUnreachableRule()) {
958 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700959 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900960 // Don't complain if we can't add the dummy network, since not all devices support it.
961 configureDummyNetwork();
962
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700963 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700964 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700965}
966
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700967int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700968 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700969}
970
971int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700972 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700973}
974
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700975int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
976 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700977 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
978 return ret;
979 }
980 updateTableNamesFile();
981 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700982}
983
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700984int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
985 Permission permission) {
986 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700987 return ret;
988 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700989 if (int ret = flushRoutes(interface)) {
990 return ret;
991 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900992 if (int ret = clearTetheringRules(interface)) {
993 return ret;
994 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700995 updateTableNamesFile();
996 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700997}
998
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700999int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001000 bool secure, const UidRanges& uidRanges) {
1001 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001002 MODIFY_NON_UID_BASED_RULES)) {
1003 return ret;
1004 }
1005 updateTableNamesFile();
1006 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001007}
1008
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001009int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001010 bool secure, const UidRanges& uidRanges) {
1011 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001012 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001013 return ret;
1014 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001015 if (int ret = flushRoutes(interface)) {
1016 return ret;
1017 }
1018 updateTableNamesFile();
1019 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001020}
1021
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001022int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1023 Permission oldPermission,
1024 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001025 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001026 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001027 return ret;
1028 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001029 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001030}
1031
Robin Leeb8087362016-03-30 18:43:08 +01001032int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1033 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1034}
1035
1036int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1037 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1038}
1039
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001040int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001041 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001042 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001043 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001044}
1045
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001046int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001047 bool secure, const UidRanges& uidRanges) {
1048 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001049 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001050}
1051
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001052int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1053 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001054}
1055
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001056int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1057 Permission permission) {
1058 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001059}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001060
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001061int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001062 TableType tableType) {
1063 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001064}
1065
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001066int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001067 const char* nexthop, TableType tableType) {
1068 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001069}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001070
1071int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001072 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001073}
1074
1075int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001076 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001077}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001078
1079int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1080 Permission permission) {
1081 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1082}
1083
1084int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1085 const char* physicalInterface,
1086 Permission permission) {
1087 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1088}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001089
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001090// Protects sInterfaceToTable.
Luke Huang534980c2018-07-06 17:50:11 +08001091std::mutex RouteController::sInterfaceToTableLock;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001092std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1093
Luke Huangd1ee4622018-06-29 13:49:58 +08001094
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001095} // namespace net
1096} // namespace android