blob: 2bbc0d6440fd572966d606436bcc5529cf091ffa [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;
Lukas061059c1b632018-09-01 12:16:45 +020071#ifdef NEEDS_NETD_DIRECT_CONNECT_RULE
72const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 23000;
73#endif
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090074const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070075
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070076const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070077const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
78const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
79
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070080const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070081const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
82const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
83
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070084const char* const ROUTE_TABLE_NAME_LOCAL = "local";
85const char* const ROUTE_TABLE_NAME_MAIN = "main";
86
Lorenzo Colittib5d19e92017-09-25 18:39:33 +090087// None of our regular routes specify priority, which causes them to have the default priority.
88// For default throw routes, we use a fixed priority of 100000.
Lorenzo Colitti5e03a892017-09-08 11:31:59 +090089uint32_t PRIO_THROW = 100000;
90
Lorenzo Colittid78843e2017-03-27 05:52:31 +090091const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
92
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070093const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
94
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070095const uid_t UID_ROOT = 0;
Lorenzo Colitti3093f562017-09-25 14:17:38 +090096const uint32_t FWMARK_NONE = 0;
97const uint32_t MASK_NONE = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090098const char* const IIF_LOOPBACK = "lo";
Yi Kongbdfd57e2018-07-25 13:26:10 -070099const char* const IIF_NONE = nullptr;
100const char* const OIF_NONE = nullptr;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700101const bool ACTION_ADD = true;
102const bool ACTION_DEL = false;
103const bool MODIFY_NON_UID_BASED_RULES = true;
104
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700105const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700106const 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 -0700107
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700108// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
109// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
110constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
111 return RTA_LENGTH(x);
112}
113
114// These are practically const, but can't be declared so, because they are used to initialize
115// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900116rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
117rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
118rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
119rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900120rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700121
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900122rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
123rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900124rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700125
126uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
127
128// END CONSTANTS ----------------------------------------------------------------------------------
129
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900130const char *actionName(uint16_t action) {
131 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
132 return ops[action % 4];
133}
134
135const char *familyName(uint8_t family) {
136 switch (family) {
137 case AF_INET: return "IPv4";
138 case AF_INET6: return "IPv6";
139 default: return "???";
140 }
141}
142
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900143// Caller must hold sInterfaceToTableLock.
144uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700145 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400146 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700147 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900148 sInterfaceToTable[interface] = index;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700149 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400150 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700151 // If the interface goes away if_nametoindex() will return 0 but we still need to know
152 // the index so we can remove the rules and routes.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900153 auto iter = sInterfaceToTable.find(interface);
154 if (iter == sInterfaceToTable.end()) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700155 ALOGE("cannot find interface %s", interface);
156 return RT_TABLE_UNSPEC;
157 }
158 return iter->second;
159}
160
Rubin Xu6c00b612018-04-27 14:27:59 +0100161uint32_t RouteController::getIfIndex(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900162 std::lock_guard lock(sInterfaceToTableLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100163
164 auto iter = sInterfaceToTable.find(interface);
165 if (iter == sInterfaceToTable.end()) {
166 ALOGE("getIfIndex: cannot find interface %s", interface);
167 return 0;
168 }
169
170 return iter->second - ROUTE_TABLE_OFFSET_FROM_INDEX;
171}
172
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900173uint32_t RouteController::getRouteTableForInterface(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900174 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900175 return getRouteTableForInterfaceLocked(interface);
176}
177
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700178void addTableName(uint32_t table, const std::string& name, std::string* contents) {
179 char tableString[UINT32_STRLEN];
180 snprintf(tableString, sizeof(tableString), "%u", table);
181 *contents += tableString;
182 *contents += " ";
183 *contents += name;
184 *contents += "\n";
185}
186
187// 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 +0900188void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700189 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700190
191 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
192 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
193
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700194 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700195 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
196 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700197
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900198 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900199 for (const auto& entry : sInterfaceToTable) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700200 addTableName(entry.second, entry.first, &contents);
201 }
202
Dan Albert5407e142015-03-16 10:05:59 -0700203 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800204 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700205 return;
206 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700207}
208
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700209// Returns 0 on success or negative errno on failure.
210int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
211 if (!input) {
212 *length = 0;
213 *padding = 0;
214 return 0;
215 }
216 *length = strlcpy(name, input, IFNAMSIZ) + 1;
217 if (*length > IFNAMSIZ) {
218 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
219 return -ENAMETOOLONG;
220 }
221 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
222 return 0;
223}
224
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700225// Adds or removes a routing rule for IPv4 and IPv6.
226//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900227// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100228// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700229// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
230// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700231// + If |iif| is non-NULL, the rule matches the specified incoming interface.
232// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
233// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
234// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900235//
236// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100237WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
238 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700239 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700240 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
241 if (fwmark & ~mask) {
242 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
243 return -ERANGE;
244 }
245
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700246 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900247 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700248 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
249 size_t iifLength, oifLength;
250 uint16_t iifPadding, oifPadding;
251 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
252 return ret;
253 }
254 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
255 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900256 }
257
Lorenzo Colitti59656512014-06-25 03:20:29 +0900258 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700259 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700260 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900261 return -EUSERS;
262 }
Robin Lee4ef94642016-04-01 11:50:49 +0100263
Lorenzo Colitti72723682014-06-26 13:51:10 +0900264 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900265
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900266 // Assemble a rule request and put it in an array of iovec structures.
267 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100268 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900269 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
270 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900271 };
272
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900273 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
274 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
275 // table 0, it's a wildcard that matches anything.
276 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
277 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
278 return -ENOTUNIQ;
279 }
280
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700281 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
282 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900283 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900284
285 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700286 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700287 { &rule, sizeof(rule) },
288 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
289 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700290 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
291 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700292 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
293 { &fwmark, mask ? sizeof(fwmark) : 0 },
294 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
295 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900296 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
297 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700298 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
299 { iifName, iifLength },
300 { PADDING_BUFFER, iifPadding },
301 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
302 { oifName, oifLength },
303 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900304 };
305
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900306 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700307 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
308 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900309 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900310 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
311 // Don't log when deleting a tethering rule that's not there. This matches the
312 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
313 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
314 strerror(-ret));
315 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900316 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700317 }
318 }
319
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900320 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700321}
322
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700323WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100324 uint32_t fwmark, uint32_t mask, const char* iif,
325 const char* oif, uid_t uidStart, uid_t uidEnd) {
326 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
327 uidEnd);
328}
329
330WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700331 uint32_t fwmark, uint32_t mask) {
332 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
333 INVALID_UID);
334}
335
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900336// Adds or deletes an IPv4 or IPv6 route.
337// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700338WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
339 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900340 // At least the destination must be non-null.
341 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700342 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900343 return -EFAULT;
344 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700345
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900346 // Parse the prefix.
347 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700348 uint8_t family;
349 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900350 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
351 &prefixLength);
352 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700353 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900354 return rawLength;
355 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700356
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900357 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700358 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900359 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
360 }
361
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700362 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900363 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900364 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700365
366 if (nexthop && !strcmp(nexthop, "unreachable")) {
367 type = RTN_UNREACHABLE;
368 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
369 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
370 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
371 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700372 nexthop = nullptr;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900373 } else if (nexthop && !strcmp(nexthop, "throw")) {
374 type = RTN_THROW;
375 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700376 nexthop = nullptr;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700377 } else {
378 // If an interface was specified, find the ifindex.
379 if (interface != OIF_NONE) {
380 ifindex = if_nametoindex(interface);
381 if (!ifindex) {
382 ALOGE("cannot find interface %s", interface);
383 return -ENODEV;
384 }
385 }
386
387 // If a nexthop was specified, parse it as the same family as the prefix.
388 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
389 ALOGE("inet_pton failed for nexthop %s", nexthop);
390 return -EINVAL;
391 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900392 }
393
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900394 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
395
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900396 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700397 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900398 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700399 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900400 .rtm_family = family,
401 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700402 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900403 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900404
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700405 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
406 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900407
408 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700409 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700410 { &route, sizeof(route) },
411 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
412 { &table, sizeof(table) },
413 { &rtaDst, sizeof(rtaDst) },
414 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700415 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
416 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700417 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
418 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900419 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
420 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900421 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900422
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900423 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_ROUTE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Jimmy Chence9e5782019-03-08 16:12:55 +0800424
425 // Allow creating multiple link-local routes in the same table, so we can make IPv6
426 // work on all interfaces in the local_network table.
427 if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) {
428 flags &= ~NLM_F_EXCL;
429 }
430
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900431 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900432 if (ret) {
433 ALOGE("Error %s route %s -> %s %s to table %u: %s",
434 actionName(action), destination, nexthop, interface, table, strerror(-ret));
435 }
436 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700437}
438
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700439// An iptables rule to mark incoming packets on a network with the netId of the network.
440//
441// This is so that the kernel can:
442// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
443// replies, SYN-ACKs, etc).
444// + Mark sockets that accept connections from this interface so that the connection stays on the
445// same interface.
446WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
447 Permission permission, bool add) {
448 Fwmark fwmark;
449
450 fwmark.netId = netId;
451 fwmark.explicitlySelected = true;
452 fwmark.protectedFromVpn = true;
453 fwmark.permission = permission;
454
Benedict Wongb9baf262017-12-03 15:43:08 -0800455 const uint32_t mask = ~Fwmark::getUidBillingMask();
456
457 std::string cmd = StringPrintf(
458 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
459 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900460 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700461 ALOGE("failed to change iptables rule that sets incoming packet mark");
462 return -EREMOTEIO;
463 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700464
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700465 return 0;
466}
467
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700468// A rule to route responses to the local network forwarded via the VPN.
469//
470// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
471// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
472WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
473 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
474 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
475 INVALID_UID, INVALID_UID);
476}
477
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700478// A rule to route all traffic from a given set of UIDs to go over the VPN.
479//
480// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
481// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
482// bypass the VPN if the protectedFromVpn bit is set.
483WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700484 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700485 Fwmark fwmark;
486 Fwmark mask;
487
488 fwmark.protectedFromVpn = false;
489 mask.protectedFromVpn = true;
490
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700491 uint32_t priority;
492
493 if (secure) {
494 priority = RULE_PRIORITY_SECURE_VPN;
495 } else {
496 priority = RULE_PRIORITY_BYPASSABLE_VPN;
497
498 fwmark.explicitlySelected = false;
499 mask.explicitlySelected = true;
500 }
501
502 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900503 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700504}
505
506// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
507// set of UIDs.
508//
509// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
510// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700511WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
512 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700513 Fwmark fwmark;
514 Fwmark mask;
515
516 fwmark.netId = netId;
517 mask.netId = FWMARK_NET_ID_MASK;
518
519 fwmark.permission = PERMISSION_SYSTEM;
520 mask.permission = PERMISSION_SYSTEM;
521
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700522 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
523
524 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
525 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700526}
527
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700528// A rule to route traffic based on an explicitly chosen network.
529//
530// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
531//
532// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
533// to check it again in the rules here, because a network's permissions may have been updated via
534// modifyNetworkPermission().
535WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
536 Permission permission, uid_t uidStart,
537 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700538 Fwmark fwmark;
539 Fwmark mask;
540
541 fwmark.netId = netId;
542 mask.netId = FWMARK_NET_ID_MASK;
543
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700544 fwmark.explicitlySelected = true;
545 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700546
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700547 fwmark.permission = permission;
548 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700549
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700550 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900551 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700552}
553
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700554// A rule to route traffic based on a chosen outgoing interface.
555//
556// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
557// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900558WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
559 Permission permission, uid_t uidStart,
560 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700561 Fwmark fwmark;
562 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700563
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700564 fwmark.permission = permission;
565 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700566
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900567 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900568 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
569 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900570 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
571 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900572 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900573 UID_ROOT, UID_ROOT)) {
574 return ret;
575 }
576 }
577
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700578 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900579 fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700580}
581
582// A rule to route traffic based on the chosen network.
583//
584// This is for sockets that have not explicitly requested a particular network, but have been
585// bound to one when they called connect(). This ensures that sockets connected on a particular
586// network stay on that network even if the default network changes.
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900587WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700588 Fwmark fwmark;
589 Fwmark mask;
590
591 fwmark.netId = netId;
592 mask.netId = FWMARK_NET_ID_MASK;
593
594 fwmark.explicitlySelected = false;
595 mask.explicitlySelected = true;
596
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900597 fwmark.permission = PERMISSION_NONE;
598 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700599
600 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900601 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
602 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700603}
604
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700605// A rule to enable split tunnel VPNs.
606//
607// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
Luke Huang0a5f8972019-05-23 06:14:36 -0700608// go over the default network, provided it has the permissions required by the default network.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900609WARN_UNUSED_RESULT int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
610 const char* physicalInterface,
611 Permission permission) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700612 uint32_t table = getRouteTableForInterface(physicalInterface);
613 if (table == RT_TABLE_UNSPEC) {
614 return -ESRCH;
615 }
616
617 Fwmark fwmark;
618 Fwmark mask;
619
620 fwmark.netId = vpnNetId;
621 mask.netId = FWMARK_NET_ID_MASK;
622
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700623 fwmark.permission = permission;
624 mask.permission = permission;
625
626 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
627 mask.intValue);
628}
629
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700630// Add rules to allow legacy routes added through the requestRouteToHost() API.
631WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700632 Fwmark fwmark;
633 Fwmark mask;
634
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700635 fwmark.explicitlySelected = false;
636 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700637
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700638 // Rules to allow legacy routes to override the default network.
639 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
640 fwmark.intValue, mask.intValue)) {
641 return ret;
642 }
643 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
644 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
645 return ret;
646 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700647
648 fwmark.permission = PERMISSION_SYSTEM;
649 mask.permission = PERMISSION_SYSTEM;
650
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700651 // A rule to allow legacy routes from system apps to override VPNs.
652 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700653 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700654}
655
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700656// Add rules to lookup the local network when specified explicitly or otherwise.
657WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
658 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
659 INVALID_UID, INVALID_UID, ACTION_ADD)) {
660 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700661 }
662
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700663 Fwmark fwmark;
664 Fwmark mask;
665
666 fwmark.explicitlySelected = false;
667 mask.explicitlySelected = true;
668
669 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
670 fwmark.intValue, mask.intValue);
671}
672
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900673/* static */
674int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900675 const char *interface = DummyNetwork::INTERFACE_NAME;
676 uint32_t table = getRouteTableForInterface(interface);
677 if (table == RT_TABLE_UNSPEC) {
678 // getRouteTableForInterface has already looged an error.
679 return -ESRCH;
680 }
681
682 ifc_init();
683 int ret = ifc_up(interface);
684 ifc_close();
685 if (ret) {
686 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
687 return -errno;
688 }
689
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900690 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
691 INVALID_UID, INVALID_UID, ACTION_ADD))) {
692 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900693 return ret;
694 }
695
Yi Kongbdfd57e2018-07-25 13:26:10 -0700696 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", nullptr))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900697 return ret;
698 }
699
Yi Kongbdfd57e2018-07-25 13:26:10 -0700700 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", nullptr))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900701 return ret;
702 }
703
704 return 0;
705}
706
Lukas061059c1b632018-09-01 12:16:45 +0200707#ifdef NEEDS_NETD_DIRECT_CONNECT_RULE
708// Add a new rule to look up the 'main' table, with the same selectors as the "default network"
709// rule, but with a lower priority. We will never create routes in the main table; it should only be
710// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
711// This is necessary, for example, when adding a route through a directly-connected gateway: in
712// order to add the route, there must already be a directly-connected route that covers the gateway.
713WARN_UNUSED_RESULT int addDirectlyConnectedRule() {
714 Fwmark fwmark;
715 Fwmark mask;
716
717 fwmark.netId = NETID_UNSET;
718 mask.netId = FWMARK_NET_ID_MASK;
719
720 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
721 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT);
722}
723#endif
724
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900725// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
726// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
727// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
728// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700729WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100730 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
731 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700732}
733
734WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700735 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
736 return ret;
737 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900738 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
739 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700740}
741
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900742/* static */
743WARN_UNUSED_RESULT int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
744 Permission permission, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700745 uint32_t table = getRouteTableForInterface(interface);
746 if (table == RT_TABLE_UNSPEC) {
747 return -ESRCH;
748 }
749
750 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
751 return ret;
752 }
753 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
754 add)) {
755 return ret;
756 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900757 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700758 add)) {
759 return ret;
760 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900761
762 // Only set implicit rules for networks that don't require permissions.
763 //
764 // This is so that if the default network ceases to be the default network and then switches
765 // from requiring no permissions to requiring permissions, we ensure that apps only use the
766 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
767 // - it closes all sockets on the network except sockets that are explicitly selected.
768 //
769 // The lack of this rule only affects the special case above, because:
770 // - The only cases where we implicitly bind a socket to a network are the default network and
771 // the bypassable VPN that applies to the app, if any.
772 // - This rule doesn't affect VPNs because they don't support permissions at all.
773 // - The default network doesn't require permissions. While we support doing this, the framework
774 // never does it (partly because we'd end up in the situation where we tell apps that there is
775 // a default network, but they can't use it).
776 // - If the network is still the default network, the presence or absence of this rule does not
777 // matter.
778 //
779 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
780 // implicitly bound to a network because at the time of connect() it was the default, and that
781 // network must no longer be the default, and must now require permissions.
782 if (permission == PERMISSION_NONE) {
783 return modifyImplicitNetworkRule(netId, table, add);
784 }
785 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700786}
787
Robin Leeb8087362016-03-30 18:43:08 +0100788WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
789 Fwmark fwmark;
790 Fwmark mask;
791 fwmark.protectedFromVpn = false;
792 mask.protectedFromVpn = true;
793
Luke Huang94658ac2018-10-18 19:35:12 +0900794 for (const UidRangeParcel& range : uidRanges.getRanges()) {
795 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN,
796 FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
797 IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) {
Robin Leeb8087362016-03-30 18:43:08 +0100798 return ret;
799 }
800 }
801
802 return 0;
803}
804
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900805WARN_UNUSED_RESULT int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
806 const UidRanges& uidRanges,
807 bool secure, bool add,
808 bool modifyNonUidBasedRules) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700809 uint32_t table = getRouteTableForInterface(interface);
810 if (table == RT_TABLE_UNSPEC) {
811 return -ESRCH;
812 }
813
Luke Huang94658ac2018-10-18 19:35:12 +0900814 for (const UidRangeParcel& range : uidRanges.getRanges()) {
815 if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, secure, add)) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700816 return ret;
817 }
Luke Huang94658ac2018-10-18 19:35:12 +0900818 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start,
819 range.stop, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700820 return ret;
821 }
Luke Huang94658ac2018-10-18 19:35:12 +0900822 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start,
823 range.stop, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700824 return ret;
825 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700826 }
827
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700828 if (modifyNonUidBasedRules) {
829 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700830 return ret;
831 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700832 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
833 return ret;
834 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700835 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700836 return ret;
837 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700838 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700839 }
840
841 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700842}
843
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900844WARN_UNUSED_RESULT int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
845 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700846 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700847 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900848 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700849 }
850
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700851 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700852 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700853
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700854 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700855 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700856
857 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700858 mask.permission = permission;
859
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700860 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900861 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700862}
863
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900864WARN_UNUSED_RESULT int RouteController::modifyTetheredNetwork(uint16_t action,
865 const char* inputInterface,
866 const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700867 uint32_t table = getRouteTableForInterface(outputInterface);
868 if (table == RT_TABLE_UNSPEC) {
869 return -ESRCH;
870 }
871
872 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
873 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
874}
875
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900876// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900877// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900878WARN_UNUSED_RESULT int RouteController::modifyRoute(uint16_t action, const char* interface,
879 const char* destination, const char* nexthop,
880 TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700881 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700882 switch (tableType) {
883 case RouteController::INTERFACE: {
884 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700885 if (table == RT_TABLE_UNSPEC) {
886 return -ESRCH;
887 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700888 break;
889 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700890 case RouteController::LOCAL_NETWORK: {
891 table = ROUTE_TABLE_LOCAL_NETWORK;
892 break;
893 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700894 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700895 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700896 break;
897 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700898 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700899 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700900 break;
901 }
902 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700903
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900904 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700905 // Trying to add a route that already exists shouldn't cause an error.
906 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900907 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700908 }
909
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900910 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700911}
912
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900913WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
914 int ret = 0;
915 while (ret == 0) {
916 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
917 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
918 }
919
920 if (ret == -ENOENT) {
921 return 0;
922 } else {
923 return ret;
924 }
925}
926
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900927uint32_t getRulePriority(const nlmsghdr *nlh) {
928 return getRtmU32Attribute(nlh, FRA_PRIORITY);
929}
930
931uint32_t getRouteTable(const nlmsghdr *nlh) {
932 return getRtmU32Attribute(nlh, RTA_TABLE);
933}
934
935WARN_UNUSED_RESULT int flushRules() {
936 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
937 // Don't touch rules at priority 0 because by default they are used for local input.
938 return getRulePriority(nlh) != 0;
939 };
940 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
941}
942
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900943WARN_UNUSED_RESULT int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900944 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
945 return getRouteTable(nlh) == table;
946 };
947
948 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
949}
950
951// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900952WARN_UNUSED_RESULT int RouteController::flushRoutes(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900953 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900954
955 uint32_t table = getRouteTableForInterfaceLocked(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900956 if (table == RT_TABLE_UNSPEC) {
957 return -ESRCH;
958 }
959
960 int ret = flushRoutes(table);
961
962 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
963 // track of its name.
964 if (ret == 0) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900965 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900966 }
967
968 return ret;
969}
970
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700971int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700972 if (int ret = flushRules()) {
973 return ret;
974 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700975 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700976 return ret;
977 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700978 if (int ret = addLocalNetworkRules(localNetId)) {
979 return ret;
980 }
Lukas061059c1b632018-09-01 12:16:45 +0200981#ifdef NEEDS_NETD_DIRECT_CONNECT_RULE
982 if (int ret = addDirectlyConnectedRule()) {
983 return ret;
984 }
985#endif
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700986 if (int ret = addUnreachableRule()) {
987 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700988 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900989 // Don't complain if we can't add the dummy network, since not all devices support it.
990 configureDummyNetwork();
991
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700992 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700993 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700994}
995
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700996int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700997 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700998}
999
1000int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001001 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001002}
1003
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001004int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
1005 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001006 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
1007 return ret;
1008 }
1009 updateTableNamesFile();
1010 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001011}
1012
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001013int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
1014 Permission permission) {
1015 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001016 return ret;
1017 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001018 if (int ret = flushRoutes(interface)) {
1019 return ret;
1020 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001021 if (int ret = clearTetheringRules(interface)) {
1022 return ret;
1023 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001024 updateTableNamesFile();
1025 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001026}
1027
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001028int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001029 bool secure, const UidRanges& uidRanges) {
1030 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001031 MODIFY_NON_UID_BASED_RULES)) {
1032 return ret;
1033 }
1034 updateTableNamesFile();
1035 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001036}
1037
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001038int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001039 bool secure, const UidRanges& uidRanges) {
1040 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001041 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001042 return ret;
1043 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001044 if (int ret = flushRoutes(interface)) {
1045 return ret;
1046 }
1047 updateTableNamesFile();
1048 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001049}
1050
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001051int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1052 Permission oldPermission,
1053 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001054 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001055 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001056 return ret;
1057 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001058 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001059}
1060
Robin Leeb8087362016-03-30 18:43:08 +01001061int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1062 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1063}
1064
1065int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1066 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1067}
1068
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001069int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001070 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001071 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001072 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001073}
1074
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001075int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001076 bool secure, const UidRanges& uidRanges) {
1077 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001078 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001079}
1080
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001081int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1082 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001083}
1084
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001085int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1086 Permission permission) {
1087 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001088}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001089
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001090int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001091 TableType tableType) {
1092 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001093}
1094
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001095int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001096 const char* nexthop, TableType tableType) {
1097 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001098}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001099
1100int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001101 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001102}
1103
1104int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001105 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001106}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001107
1108int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1109 Permission permission) {
1110 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1111}
1112
1113int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1114 const char* physicalInterface,
1115 Permission permission) {
1116 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1117}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001118
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001119// Protects sInterfaceToTable.
Luke Huang534980c2018-07-06 17:50:11 +08001120std::mutex RouteController::sInterfaceToTableLock;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001121std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1122
Luke Huangd1ee4622018-06-29 13:49:58 +08001123
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001124} // namespace net
1125} // namespace android