blob: d0ceefa26a4ad3b8a345e7a4a115646aa5e06f60 [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;
Jimmy Chence9e5782019-03-08 16:12:55 +0800421
422 // Allow creating multiple link-local routes in the same table, so we can make IPv6
423 // work on all interfaces in the local_network table.
424 if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) {
425 flags &= ~NLM_F_EXCL;
426 }
427
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900428 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900429 if (ret) {
430 ALOGE("Error %s route %s -> %s %s to table %u: %s",
431 actionName(action), destination, nexthop, interface, table, strerror(-ret));
432 }
433 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700434}
435
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700436// An iptables rule to mark incoming packets on a network with the netId of the network.
437//
438// This is so that the kernel can:
439// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
440// replies, SYN-ACKs, etc).
441// + Mark sockets that accept connections from this interface so that the connection stays on the
442// same interface.
443WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
444 Permission permission, bool add) {
445 Fwmark fwmark;
446
447 fwmark.netId = netId;
448 fwmark.explicitlySelected = true;
449 fwmark.protectedFromVpn = true;
450 fwmark.permission = permission;
451
Benedict Wongb9baf262017-12-03 15:43:08 -0800452 const uint32_t mask = ~Fwmark::getUidBillingMask();
453
454 std::string cmd = StringPrintf(
455 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
456 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900457 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700458 ALOGE("failed to change iptables rule that sets incoming packet mark");
459 return -EREMOTEIO;
460 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700461
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700462 return 0;
463}
464
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700465// A rule to route responses to the local network forwarded via the VPN.
466//
467// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
468// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
469WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
470 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
471 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
472 INVALID_UID, INVALID_UID);
473}
474
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700475// A rule to route all traffic from a given set of UIDs to go over the VPN.
476//
477// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
478// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
479// bypass the VPN if the protectedFromVpn bit is set.
480WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700481 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700482 Fwmark fwmark;
483 Fwmark mask;
484
485 fwmark.protectedFromVpn = false;
486 mask.protectedFromVpn = true;
487
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700488 uint32_t priority;
489
490 if (secure) {
491 priority = RULE_PRIORITY_SECURE_VPN;
492 } else {
493 priority = RULE_PRIORITY_BYPASSABLE_VPN;
494
495 fwmark.explicitlySelected = false;
496 mask.explicitlySelected = true;
497 }
498
499 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900500 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700501}
502
503// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
504// set of UIDs.
505//
506// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
507// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700508WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
509 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700510 Fwmark fwmark;
511 Fwmark mask;
512
513 fwmark.netId = netId;
514 mask.netId = FWMARK_NET_ID_MASK;
515
516 fwmark.permission = PERMISSION_SYSTEM;
517 mask.permission = PERMISSION_SYSTEM;
518
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700519 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
520
521 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
522 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700523}
524
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700525// A rule to route traffic based on an explicitly chosen network.
526//
527// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
528//
529// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
530// to check it again in the rules here, because a network's permissions may have been updated via
531// modifyNetworkPermission().
532WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
533 Permission permission, uid_t uidStart,
534 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700535 Fwmark fwmark;
536 Fwmark mask;
537
538 fwmark.netId = netId;
539 mask.netId = FWMARK_NET_ID_MASK;
540
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700541 fwmark.explicitlySelected = true;
542 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700543
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700544 fwmark.permission = permission;
545 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700546
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700547 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900548 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700549}
550
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700551// A rule to route traffic based on a chosen outgoing interface.
552//
553// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
554// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900555WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
556 Permission permission, uid_t uidStart,
557 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700558 Fwmark fwmark;
559 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700560
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700561 fwmark.permission = permission;
562 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700563
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900564 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900565 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
566 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900567 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
568 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900569 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900570 UID_ROOT, UID_ROOT)) {
571 return ret;
572 }
573 }
574
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700575 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900576 fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700577}
578
579// A rule to route traffic based on the chosen network.
580//
581// This is for sockets that have not explicitly requested a particular network, but have been
582// bound to one when they called connect(). This ensures that sockets connected on a particular
583// network stay on that network even if the default network changes.
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900584WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700585 Fwmark fwmark;
586 Fwmark mask;
587
588 fwmark.netId = netId;
589 mask.netId = FWMARK_NET_ID_MASK;
590
591 fwmark.explicitlySelected = false;
592 mask.explicitlySelected = true;
593
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900594 fwmark.permission = PERMISSION_NONE;
595 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700596
597 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900598 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
599 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700600}
601
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700602// A rule to enable split tunnel VPNs.
603//
604// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
605// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
606// permissions required by the default network.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900607WARN_UNUSED_RESULT int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
608 const char* physicalInterface,
609 Permission permission) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700610 uint32_t table = getRouteTableForInterface(physicalInterface);
611 if (table == RT_TABLE_UNSPEC) {
612 return -ESRCH;
613 }
614
615 Fwmark fwmark;
616 Fwmark mask;
617
618 fwmark.netId = vpnNetId;
619 mask.netId = FWMARK_NET_ID_MASK;
620
621 fwmark.explicitlySelected = false;
622 mask.explicitlySelected = true;
623
624 fwmark.permission = permission;
625 mask.permission = permission;
626
627 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
628 mask.intValue);
629}
630
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700631// Add rules to allow legacy routes added through the requestRouteToHost() API.
632WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700633 Fwmark fwmark;
634 Fwmark mask;
635
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700636 fwmark.explicitlySelected = false;
637 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700638
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700639 // Rules to allow legacy routes to override the default network.
640 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
641 fwmark.intValue, mask.intValue)) {
642 return ret;
643 }
644 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
645 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
646 return ret;
647 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700648
649 fwmark.permission = PERMISSION_SYSTEM;
650 mask.permission = PERMISSION_SYSTEM;
651
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700652 // A rule to allow legacy routes from system apps to override VPNs.
653 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700654 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700655}
656
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700657// Add rules to lookup the local network when specified explicitly or otherwise.
658WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
659 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
660 INVALID_UID, INVALID_UID, ACTION_ADD)) {
661 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700662 }
663
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700664 Fwmark fwmark;
665 Fwmark mask;
666
667 fwmark.explicitlySelected = false;
668 mask.explicitlySelected = true;
669
670 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
671 fwmark.intValue, mask.intValue);
672}
673
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900674/* static */
675int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900676 const char *interface = DummyNetwork::INTERFACE_NAME;
677 uint32_t table = getRouteTableForInterface(interface);
678 if (table == RT_TABLE_UNSPEC) {
679 // getRouteTableForInterface has already looged an error.
680 return -ESRCH;
681 }
682
683 ifc_init();
684 int ret = ifc_up(interface);
685 ifc_close();
686 if (ret) {
687 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
688 return -errno;
689 }
690
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900691 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
692 INVALID_UID, INVALID_UID, ACTION_ADD))) {
693 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900694 return ret;
695 }
696
Yi Kongbdfd57e2018-07-25 13:26:10 -0700697 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", nullptr))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900698 return ret;
699 }
700
Yi Kongbdfd57e2018-07-25 13:26:10 -0700701 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", nullptr))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900702 return ret;
703 }
704
705 return 0;
706}
707
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900708// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
709// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
710// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
711// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700712WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100713 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
714 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700715}
716
717WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700718 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
719 return ret;
720 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900721 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
722 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700723}
724
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900725/* static */
726WARN_UNUSED_RESULT int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
727 Permission permission, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700728 uint32_t table = getRouteTableForInterface(interface);
729 if (table == RT_TABLE_UNSPEC) {
730 return -ESRCH;
731 }
732
733 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
734 return ret;
735 }
736 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
737 add)) {
738 return ret;
739 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900740 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700741 add)) {
742 return ret;
743 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900744
745 // Only set implicit rules for networks that don't require permissions.
746 //
747 // This is so that if the default network ceases to be the default network and then switches
748 // from requiring no permissions to requiring permissions, we ensure that apps only use the
749 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
750 // - it closes all sockets on the network except sockets that are explicitly selected.
751 //
752 // The lack of this rule only affects the special case above, because:
753 // - The only cases where we implicitly bind a socket to a network are the default network and
754 // the bypassable VPN that applies to the app, if any.
755 // - This rule doesn't affect VPNs because they don't support permissions at all.
756 // - The default network doesn't require permissions. While we support doing this, the framework
757 // never does it (partly because we'd end up in the situation where we tell apps that there is
758 // a default network, but they can't use it).
759 // - If the network is still the default network, the presence or absence of this rule does not
760 // matter.
761 //
762 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
763 // implicitly bound to a network because at the time of connect() it was the default, and that
764 // network must no longer be the default, and must now require permissions.
765 if (permission == PERMISSION_NONE) {
766 return modifyImplicitNetworkRule(netId, table, add);
767 }
768 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700769}
770
Robin Leeb8087362016-03-30 18:43:08 +0100771WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
772 Fwmark fwmark;
773 Fwmark mask;
774 fwmark.protectedFromVpn = false;
775 mask.protectedFromVpn = true;
776
Luke Huang94658ac2018-10-18 19:35:12 +0900777 for (const UidRangeParcel& range : uidRanges.getRanges()) {
778 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN,
779 FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
780 IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) {
Robin Leeb8087362016-03-30 18:43:08 +0100781 return ret;
782 }
783 }
784
785 return 0;
786}
787
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900788WARN_UNUSED_RESULT int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
789 const UidRanges& uidRanges,
790 bool secure, bool add,
791 bool modifyNonUidBasedRules) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700792 uint32_t table = getRouteTableForInterface(interface);
793 if (table == RT_TABLE_UNSPEC) {
794 return -ESRCH;
795 }
796
Luke Huang94658ac2018-10-18 19:35:12 +0900797 for (const UidRangeParcel& range : uidRanges.getRanges()) {
798 if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, secure, add)) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700799 return ret;
800 }
Luke Huang94658ac2018-10-18 19:35:12 +0900801 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start,
802 range.stop, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700803 return ret;
804 }
Luke Huang94658ac2018-10-18 19:35:12 +0900805 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start,
806 range.stop, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700807 return ret;
808 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700809 }
810
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700811 if (modifyNonUidBasedRules) {
812 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700813 return ret;
814 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700815 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
816 return ret;
817 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700818 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700819 return ret;
820 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700821 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700822 }
823
824 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700825}
826
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900827WARN_UNUSED_RESULT int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
828 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700829 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700830 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900831 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700832 }
833
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700834 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700835 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700836
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700837 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700838 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700839
840 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700841 mask.permission = permission;
842
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700843 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900844 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700845}
846
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900847WARN_UNUSED_RESULT int RouteController::modifyTetheredNetwork(uint16_t action,
848 const char* inputInterface,
849 const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700850 uint32_t table = getRouteTableForInterface(outputInterface);
851 if (table == RT_TABLE_UNSPEC) {
852 return -ESRCH;
853 }
854
855 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
856 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
857}
858
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900859// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900860// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900861WARN_UNUSED_RESULT int RouteController::modifyRoute(uint16_t action, const char* interface,
862 const char* destination, const char* nexthop,
863 TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700864 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700865 switch (tableType) {
866 case RouteController::INTERFACE: {
867 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700868 if (table == RT_TABLE_UNSPEC) {
869 return -ESRCH;
870 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700871 break;
872 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700873 case RouteController::LOCAL_NETWORK: {
874 table = ROUTE_TABLE_LOCAL_NETWORK;
875 break;
876 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700877 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700878 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700879 break;
880 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700881 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700882 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700883 break;
884 }
885 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700886
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900887 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700888 // Trying to add a route that already exists shouldn't cause an error.
889 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900890 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700891 }
892
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900893 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700894}
895
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900896WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
897 int ret = 0;
898 while (ret == 0) {
899 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
900 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
901 }
902
903 if (ret == -ENOENT) {
904 return 0;
905 } else {
906 return ret;
907 }
908}
909
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900910uint32_t getRulePriority(const nlmsghdr *nlh) {
911 return getRtmU32Attribute(nlh, FRA_PRIORITY);
912}
913
914uint32_t getRouteTable(const nlmsghdr *nlh) {
915 return getRtmU32Attribute(nlh, RTA_TABLE);
916}
917
918WARN_UNUSED_RESULT int flushRules() {
919 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
920 // Don't touch rules at priority 0 because by default they are used for local input.
921 return getRulePriority(nlh) != 0;
922 };
923 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
924}
925
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900926WARN_UNUSED_RESULT int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900927 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
928 return getRouteTable(nlh) == table;
929 };
930
931 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
932}
933
934// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900935WARN_UNUSED_RESULT int RouteController::flushRoutes(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900936 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900937
938 uint32_t table = getRouteTableForInterfaceLocked(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900939 if (table == RT_TABLE_UNSPEC) {
940 return -ESRCH;
941 }
942
943 int ret = flushRoutes(table);
944
945 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
946 // track of its name.
947 if (ret == 0) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900948 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900949 }
950
951 return ret;
952}
953
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700954int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700955 if (int ret = flushRules()) {
956 return ret;
957 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700958 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700959 return ret;
960 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700961 if (int ret = addLocalNetworkRules(localNetId)) {
962 return ret;
963 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700964 if (int ret = addUnreachableRule()) {
965 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700966 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900967 // Don't complain if we can't add the dummy network, since not all devices support it.
968 configureDummyNetwork();
969
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700970 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700971 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700972}
973
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700974int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700975 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700976}
977
978int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700979 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700980}
981
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700982int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
983 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700984 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
985 return ret;
986 }
987 updateTableNamesFile();
988 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700989}
990
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700991int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
992 Permission permission) {
993 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700994 return ret;
995 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700996 if (int ret = flushRoutes(interface)) {
997 return ret;
998 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900999 if (int ret = clearTetheringRules(interface)) {
1000 return ret;
1001 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001002 updateTableNamesFile();
1003 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001004}
1005
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001006int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001007 bool secure, const UidRanges& uidRanges) {
1008 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001009 MODIFY_NON_UID_BASED_RULES)) {
1010 return ret;
1011 }
1012 updateTableNamesFile();
1013 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001014}
1015
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001016int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001017 bool secure, const UidRanges& uidRanges) {
1018 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001019 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001020 return ret;
1021 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001022 if (int ret = flushRoutes(interface)) {
1023 return ret;
1024 }
1025 updateTableNamesFile();
1026 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001027}
1028
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001029int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1030 Permission oldPermission,
1031 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001032 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001033 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001034 return ret;
1035 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001036 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001037}
1038
Robin Leeb8087362016-03-30 18:43:08 +01001039int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1040 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1041}
1042
1043int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1044 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1045}
1046
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001047int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001048 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001049 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001050 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001051}
1052
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001053int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001054 bool secure, const UidRanges& uidRanges) {
1055 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001056 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001057}
1058
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001059int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1060 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001061}
1062
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001063int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1064 Permission permission) {
1065 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001066}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001067
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001068int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001069 TableType tableType) {
1070 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001071}
1072
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001073int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001074 const char* nexthop, TableType tableType) {
1075 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001076}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001077
1078int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001079 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001080}
1081
1082int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001083 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001084}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001085
1086int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1087 Permission permission) {
1088 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1089}
1090
1091int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1092 const char* physicalInterface,
1093 Permission permission) {
1094 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1095}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001096
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001097// Protects sInterfaceToTable.
Luke Huang534980c2018-07-06 17:50:11 +08001098std::mutex RouteController::sInterfaceToTableLock;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001099std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1100
Luke Huangd1ee4622018-06-29 13:49:58 +08001101
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001102} // namespace net
1103} // namespace android