blob: 52fdcda032fff1f82abb4fb782f62118ad71cf5b [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"
Hungming Chen7f725432020-02-07 17:47:23 +080036#include "OffloadUtils.h"
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070037#include "UidRanges.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070038
Bernie Innocenti189eb502018-10-01 23:10:18 +090039#include <android-base/file.h>
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090040#include <android-base/stringprintf.h>
Hungming Chen7f725432020-02-07 17:47:23 +080041#include <android-base/strings.h>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070042#include "log/log.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090043#include "netid_client.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090044#include "netutils/ifc.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070045
Hungming Chen7f725432020-02-07 17:47:23 +080046using android::base::StartsWith;
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090047using android::base::StringPrintf;
Dan Albert5407e142015-03-16 10:05:59 -070048using android::base::WriteStringToFile;
Luke Huang94658ac2018-10-18 19:35:12 +090049using android::net::UidRangeParcel;
Dan Albert5407e142015-03-16 10:05:59 -070050
Bernie Innocenti762dcf42019-06-14 19:52:49 +090051namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090052
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090053auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand;
54
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070055// BEGIN CONSTANTS --------------------------------------------------------------------------------
56
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070057const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090058const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070059const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070060const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010061const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070062const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
63const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
64const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
65const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070066const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070067const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070068const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070069const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070070const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070071const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090072const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070073
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070074const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070075const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
76const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
77
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070078const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070079const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
80const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
81
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070082const char* const ROUTE_TABLE_NAME_LOCAL = "local";
83const char* const ROUTE_TABLE_NAME_MAIN = "main";
84
Lorenzo Colittib5d19e92017-09-25 18:39:33 +090085// None of our regular routes specify priority, which causes them to have the default priority.
86// For default throw routes, we use a fixed priority of 100000.
Lorenzo Colitti5e03a892017-09-08 11:31:59 +090087uint32_t PRIO_THROW = 100000;
88
Lorenzo Colittid78843e2017-03-27 05:52:31 +090089const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
90
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070091const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
92
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070093const uid_t UID_ROOT = 0;
Lorenzo Colitti3093f562017-09-25 14:17:38 +090094const uint32_t FWMARK_NONE = 0;
95const uint32_t MASK_NONE = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090096const char* const IIF_LOOPBACK = "lo";
Yi Kongbdfd57e2018-07-25 13:26:10 -070097const char* const IIF_NONE = nullptr;
98const char* const OIF_NONE = nullptr;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070099const bool ACTION_ADD = true;
100const bool ACTION_DEL = false;
101const bool MODIFY_NON_UID_BASED_RULES = true;
102
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700103const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700104const 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 -0700105
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700106// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
107// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900108static constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700109 return RTA_LENGTH(x);
110}
111
112// These are practically const, but can't be declared so, because they are used to initialize
113// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900114rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
115rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
116rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
117rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900118rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700119
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900120rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
121rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900122rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700123
Tyler Wearfa94a272019-12-05 15:01:48 -0800124// One or more nested attributes in the RTA_METRICS attribute.
125rtattr RTATTRX_MTU = { U16_RTA_LENGTH(sizeof(uint32_t)), RTAX_MTU};
Lorenzo Colitti57d54682020-01-24 09:18:13 +0900126constexpr size_t RTATTRX_MTU_SIZE = RTA_SPACE(sizeof(uint32_t));
Tyler Wearfa94a272019-12-05 15:01:48 -0800127
128// The RTA_METRICS attribute itself.
Lorenzo Colitti57d54682020-01-24 09:18:13 +0900129constexpr size_t RTATTR_METRICS_SIZE = RTATTRX_MTU_SIZE;
Tyler Wearfa94a272019-12-05 15:01:48 -0800130rtattr RTATTR_METRICS = { U16_RTA_LENGTH(RTATTR_METRICS_SIZE), RTA_METRICS };
131
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700132uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
133
134// END CONSTANTS ----------------------------------------------------------------------------------
135
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900136static const char* actionName(uint16_t action) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900137 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
138 return ops[action % 4];
139}
140
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900141static const char* familyName(uint8_t family) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900142 switch (family) {
143 case AF_INET: return "IPv4";
144 case AF_INET6: return "IPv6";
145 default: return "???";
146 }
147}
148
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900149// Caller must hold sInterfaceToTableLock.
150uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface) {
mtk137999f2913e2019-02-25 19:39:36 +0800151 // If we already know the routing table for this interface name, use it.
152 // This ensures we can remove rules and routes for an interface that has been removed,
153 // or has been removed and re-added with a different interface index.
154 //
155 // The caller is responsible for ensuring that an interface is never added to a network
156 // until it has been removed from any network it was previously in. This ensures that
157 // if the same interface disconnects and then reconnects with a different interface ID
158 // when the reconnect happens the interface will not be in the map, and the code will
159 // determine the new routing table from the interface ID, below.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900160 auto iter = sInterfaceToTable.find(interface);
mtk137999f2913e2019-02-25 19:39:36 +0800161 if (iter != sInterfaceToTable.end()) {
162 return iter->second;
163 }
164
165 uint32_t index = if_nametoindex(interface);
166 if (index == 0) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700167 ALOGE("cannot find interface %s", interface);
168 return RT_TABLE_UNSPEC;
169 }
mtk137999f2913e2019-02-25 19:39:36 +0800170 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
171 sInterfaceToTable[interface] = index;
172 return index;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700173}
174
Rubin Xu6c00b612018-04-27 14:27:59 +0100175uint32_t RouteController::getIfIndex(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900176 std::lock_guard lock(sInterfaceToTableLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100177
178 auto iter = sInterfaceToTable.find(interface);
179 if (iter == sInterfaceToTable.end()) {
180 ALOGE("getIfIndex: cannot find interface %s", interface);
181 return 0;
182 }
183
184 return iter->second - ROUTE_TABLE_OFFSET_FROM_INDEX;
185}
186
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900187uint32_t RouteController::getRouteTableForInterface(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900188 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900189 return getRouteTableForInterfaceLocked(interface);
190}
191
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700192void addTableName(uint32_t table, const std::string& name, std::string* contents) {
193 char tableString[UINT32_STRLEN];
194 snprintf(tableString, sizeof(tableString), "%u", table);
195 *contents += tableString;
196 *contents += " ";
197 *contents += name;
198 *contents += "\n";
199}
200
201// 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 +0900202void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700203 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700204
205 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
206 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
207
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700208 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700209 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
210 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700211
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900212 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900213 for (const auto& entry : sInterfaceToTable) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700214 addTableName(entry.second, entry.first, &contents);
215 }
216
Dan Albert5407e142015-03-16 10:05:59 -0700217 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800218 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700219 return;
220 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700221}
222
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700223// Returns 0 on success or negative errno on failure.
224int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
225 if (!input) {
226 *length = 0;
227 *padding = 0;
228 return 0;
229 }
230 *length = strlcpy(name, input, IFNAMSIZ) + 1;
231 if (*length > IFNAMSIZ) {
232 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
233 return -ENAMETOOLONG;
234 }
235 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
236 return 0;
237}
238
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700239// Adds or removes a routing rule for IPv4 and IPv6.
240//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900241// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100242// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700243// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
244// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700245// + If |iif| is non-NULL, the rule matches the specified incoming interface.
246// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
247// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
248// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900249//
250// Returns 0 on success or negative errno on failure.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900251[[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
252 uint32_t table, uint32_t fwmark, uint32_t mask,
253 const char* iif, const char* oif, uid_t uidStart,
254 uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700255 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
256 if (fwmark & ~mask) {
257 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
258 return -ERANGE;
259 }
260
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700261 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900262 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700263 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
264 size_t iifLength, oifLength;
265 uint16_t iifPadding, oifPadding;
266 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
267 return ret;
268 }
269 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
270 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900271 }
272
Lorenzo Colitti59656512014-06-25 03:20:29 +0900273 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700274 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700275 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900276 return -EUSERS;
277 }
Robin Lee4ef94642016-04-01 11:50:49 +0100278
Lorenzo Colitti72723682014-06-26 13:51:10 +0900279 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900280
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900281 // Assemble a rule request and put it in an array of iovec structures.
282 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100283 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900284 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
285 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900286 };
287
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900288 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
289 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
290 // table 0, it's a wildcard that matches anything.
291 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
292 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
293 return -ENOTUNIQ;
294 }
295
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700296 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
297 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900298 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900299
300 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700301 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700302 { &rule, sizeof(rule) },
303 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
304 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700305 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
306 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700307 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
308 { &fwmark, mask ? sizeof(fwmark) : 0 },
309 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
310 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900311 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
312 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700313 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
314 { iifName, iifLength },
315 { PADDING_BUFFER, iifPadding },
316 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
317 { oifName, oifLength },
318 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900319 };
320
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900321 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700322 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
323 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900324 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900325 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
326 // Don't log when deleting a tethering rule that's not there. This matches the
327 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
328 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
329 strerror(-ret));
330 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900331 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700332 }
333 }
334
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900335 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700336}
337
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900338[[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
339 uint32_t fwmark, uint32_t mask, const char* iif,
340 const char* oif, uid_t uidStart, uid_t uidEnd) {
Robin Lee4ef94642016-04-01 11:50:49 +0100341 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
342 uidEnd);
343}
344
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900345[[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
346 uint32_t fwmark, uint32_t mask) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700347 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
348 INVALID_UID);
349}
350
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900351// Adds or deletes an IPv4 or IPv6 route.
352// Returns 0 on success or negative errno on failure.
Tyler Wearfa94a272019-12-05 15:01:48 -0800353int modifyIpRoute(uint16_t action, uint16_t flags, uint32_t table, const char* interface,
354 const char* destination, const char* nexthop, uint32_t mtu) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900355 // At least the destination must be non-null.
356 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700357 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900358 return -EFAULT;
359 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700360
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900361 // Parse the prefix.
362 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700363 uint8_t family;
364 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900365 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
366 &prefixLength);
367 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700368 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900369 return rawLength;
370 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700371
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900372 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700373 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900374 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
375 }
376
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700377 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900378 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900379 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700380
381 if (nexthop && !strcmp(nexthop, "unreachable")) {
382 type = RTN_UNREACHABLE;
383 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
384 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
385 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
386 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700387 nexthop = nullptr;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900388 } else if (nexthop && !strcmp(nexthop, "throw")) {
389 type = RTN_THROW;
390 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700391 nexthop = nullptr;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700392 } else {
393 // If an interface was specified, find the ifindex.
394 if (interface != OIF_NONE) {
395 ifindex = if_nametoindex(interface);
396 if (!ifindex) {
397 ALOGE("cannot find interface %s", interface);
398 return -ENODEV;
399 }
400 }
401
402 // If a nexthop was specified, parse it as the same family as the prefix.
403 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
404 ALOGE("inet_pton failed for nexthop %s", nexthop);
405 return -EINVAL;
406 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900407 }
408
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900409 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
410
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900411 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700412 rtmsg route = {
Nick Desaulniers6b357502019-10-11 09:26:44 -0700413 .rtm_family = family,
414 .rtm_dst_len = prefixLength,
415 .rtm_protocol = RTPROT_STATIC,
416 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
417 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900418 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900419
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700420 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
421 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900422
423 iovec iov[] = {
Tyler Wearfa94a272019-12-05 15:01:48 -0800424 { nullptr, 0 },
425 { &route, sizeof(route) },
426 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
427 { &table, sizeof(table) },
428 { &rtaDst, sizeof(rtaDst) },
429 { rawAddress, static_cast<size_t>(rawLength) },
430 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
431 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
432 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
433 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
434 { &RTATTR_METRICS, mtu != 0 ? sizeof(RTATTR_METRICS) : 0 },
435 { &RTATTRX_MTU, mtu != 0 ? sizeof(RTATTRX_MTU) : 0 },
436 { &mtu, mtu != 0 ? sizeof(mtu) : 0 },
437 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
438 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900439 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900440
Jimmy Chence9e5782019-03-08 16:12:55 +0800441 // Allow creating multiple link-local routes in the same table, so we can make IPv6
442 // work on all interfaces in the local_network table.
443 if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) {
444 flags &= ~NLM_F_EXCL;
445 }
446
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900447 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900448 if (ret) {
449 ALOGE("Error %s route %s -> %s %s to table %u: %s",
450 actionName(action), destination, nexthop, interface, table, strerror(-ret));
451 }
452 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700453}
454
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700455// An iptables rule to mark incoming packets on a network with the netId of the network.
456//
457// This is so that the kernel can:
458// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
459// replies, SYN-ACKs, etc).
460// + Mark sockets that accept connections from this interface so that the connection stays on the
461// same interface.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900462int modifyIncomingPacketMark(unsigned netId, const char* interface, Permission permission,
463 bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700464 Fwmark fwmark;
465
466 fwmark.netId = netId;
467 fwmark.explicitlySelected = true;
468 fwmark.protectedFromVpn = true;
469 fwmark.permission = permission;
470
Benedict Wongb9baf262017-12-03 15:43:08 -0800471 const uint32_t mask = ~Fwmark::getUidBillingMask();
472
473 std::string cmd = StringPrintf(
474 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
475 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900476 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700477 ALOGE("failed to change iptables rule that sets incoming packet mark");
478 return -EREMOTEIO;
479 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700480
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700481 return 0;
482}
483
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700484// A rule to route responses to the local network forwarded via the VPN.
485//
486// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
487// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900488[[nodiscard]] static int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700489 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
490 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
491 INVALID_UID, INVALID_UID);
492}
493
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700494// A rule to route all traffic from a given set of UIDs to go over the VPN.
495//
496// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
497// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
498// bypass the VPN if the protectedFromVpn bit is set.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900499[[nodiscard]] static int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
500 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700501 Fwmark fwmark;
502 Fwmark mask;
503
504 fwmark.protectedFromVpn = false;
505 mask.protectedFromVpn = true;
506
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700507 uint32_t priority;
508
509 if (secure) {
510 priority = RULE_PRIORITY_SECURE_VPN;
511 } else {
512 priority = RULE_PRIORITY_BYPASSABLE_VPN;
513
514 fwmark.explicitlySelected = false;
515 mask.explicitlySelected = true;
516 }
517
518 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900519 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700520}
521
522// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
523// set of UIDs.
524//
525// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
526// target set, but where the DnsProxyListener itself is not.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900527[[nodiscard]] static int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
528 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700529 Fwmark fwmark;
530 Fwmark mask;
531
532 fwmark.netId = netId;
533 mask.netId = FWMARK_NET_ID_MASK;
534
535 fwmark.permission = PERMISSION_SYSTEM;
536 mask.permission = PERMISSION_SYSTEM;
537
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700538 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
539
540 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
541 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700542}
543
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700544// A rule to route traffic based on an explicitly chosen network.
545//
546// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
547//
548// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
549// to check it again in the rules here, because a network's permissions may have been updated via
550// modifyNetworkPermission().
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900551[[nodiscard]] static int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
552 Permission permission, uid_t uidStart,
553 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700554 Fwmark fwmark;
555 Fwmark mask;
556
557 fwmark.netId = netId;
558 mask.netId = FWMARK_NET_ID_MASK;
559
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700560 fwmark.explicitlySelected = true;
561 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700562
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700563 fwmark.permission = permission;
564 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700565
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700566 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900567 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700568}
569
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700570// A rule to route traffic based on a chosen outgoing interface.
571//
572// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
573// the outgoing interface (typically for link-local communications).
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900574[[nodiscard]] static int modifyOutputInterfaceRules(const char* interface, uint32_t table,
575 Permission permission, uid_t uidStart,
576 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700577 Fwmark fwmark;
578 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700579
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700580 fwmark.permission = permission;
581 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700582
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900583 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900584 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
585 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900586 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
587 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900588 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900589 UID_ROOT, UID_ROOT)) {
590 return ret;
591 }
592 }
593
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700594 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900595 fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700596}
597
598// A rule to route traffic based on the chosen network.
599//
600// This is for sockets that have not explicitly requested a particular network, but have been
601// bound to one when they called connect(). This ensures that sockets connected on a particular
602// network stay on that network even if the default network changes.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900603[[nodiscard]] static int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700604 Fwmark fwmark;
605 Fwmark mask;
606
607 fwmark.netId = netId;
608 mask.netId = FWMARK_NET_ID_MASK;
609
610 fwmark.explicitlySelected = false;
611 mask.explicitlySelected = true;
612
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900613 fwmark.permission = PERMISSION_NONE;
614 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700615
616 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900617 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
618 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700619}
620
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700621// A rule to enable split tunnel VPNs.
622//
623// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
Luke Huangd2861982019-05-17 19:47:28 +0800624// go over the default network, provided it has the permissions required by the default network.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900625int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
626 const char* physicalInterface,
627 Permission permission) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700628 uint32_t table = getRouteTableForInterface(physicalInterface);
629 if (table == RT_TABLE_UNSPEC) {
630 return -ESRCH;
631 }
632
633 Fwmark fwmark;
634 Fwmark mask;
635
636 fwmark.netId = vpnNetId;
637 mask.netId = FWMARK_NET_ID_MASK;
638
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700639 fwmark.permission = permission;
640 mask.permission = permission;
641
642 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
643 mask.intValue);
644}
645
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700646// Add rules to allow legacy routes added through the requestRouteToHost() API.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900647[[nodiscard]] static int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700648 Fwmark fwmark;
649 Fwmark mask;
650
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700651 fwmark.explicitlySelected = false;
652 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700653
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700654 // Rules to allow legacy routes to override the default network.
655 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
656 fwmark.intValue, mask.intValue)) {
657 return ret;
658 }
659 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
660 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
661 return ret;
662 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700663
664 fwmark.permission = PERMISSION_SYSTEM;
665 mask.permission = PERMISSION_SYSTEM;
666
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700667 // A rule to allow legacy routes from system apps to override VPNs.
668 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700669 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700670}
671
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700672// Add rules to lookup the local network when specified explicitly or otherwise.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900673[[nodiscard]] static int addLocalNetworkRules(unsigned localNetId) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700674 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
675 INVALID_UID, INVALID_UID, ACTION_ADD)) {
676 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700677 }
678
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700679 Fwmark fwmark;
680 Fwmark mask;
681
682 fwmark.explicitlySelected = false;
683 mask.explicitlySelected = true;
684
685 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
686 fwmark.intValue, mask.intValue);
687}
688
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900689/* static */
690int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900691 const char *interface = DummyNetwork::INTERFACE_NAME;
692 uint32_t table = getRouteTableForInterface(interface);
693 if (table == RT_TABLE_UNSPEC) {
Hungming Chen7f725432020-02-07 17:47:23 +0800694 // getRouteTableForInterface has already logged an error.
Lorenzo Colitti36679362015-02-25 10:26:19 +0900695 return -ESRCH;
696 }
697
698 ifc_init();
699 int ret = ifc_up(interface);
700 ifc_close();
701 if (ret) {
702 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
703 return -errno;
704 }
705
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900706 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
707 INVALID_UID, INVALID_UID, ACTION_ADD))) {
708 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900709 return ret;
710 }
711
Tyler Wearfa94a272019-12-05 15:01:48 -0800712 if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface,
713 "0.0.0.0/0", nullptr, 0 /* mtu */))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900714 return ret;
715 }
716
Tyler Wearfa94a272019-12-05 15:01:48 -0800717 if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface, "::/0",
718 nullptr, 0 /* mtu */))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900719 return ret;
720 }
721
722 return 0;
723}
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.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900729[[nodiscard]] static 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
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900734[[nodiscard]] static 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 */
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900743int 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
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900788[[nodiscard]] static int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
Robin Leeb8087362016-03-30 18:43:08 +0100789 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
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900805int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
806 const UidRanges& uidRanges, bool secure, bool add,
807 bool modifyNonUidBasedRules) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700808 uint32_t table = getRouteTableForInterface(interface);
809 if (table == RT_TABLE_UNSPEC) {
810 return -ESRCH;
811 }
812
Luke Huang94658ac2018-10-18 19:35:12 +0900813 for (const UidRangeParcel& range : uidRanges.getRanges()) {
814 if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, secure, add)) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700815 return ret;
816 }
Luke Huang94658ac2018-10-18 19:35:12 +0900817 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start,
818 range.stop, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700819 return ret;
820 }
Luke Huang94658ac2018-10-18 19:35:12 +0900821 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start,
822 range.stop, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700823 return ret;
824 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700825 }
826
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700827 if (modifyNonUidBasedRules) {
828 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700829 return ret;
830 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700831 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
832 return ret;
833 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700834 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700835 return ret;
836 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700837 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700838 }
839
840 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700841}
842
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900843int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
844 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700845 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700846 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900847 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700848 }
849
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700850 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700851 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700852
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700853 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700854 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700855
856 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700857 mask.permission = permission;
858
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700859 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900860 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700861}
862
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900863int RouteController::modifyTetheredNetwork(uint16_t action, const char* inputInterface,
864 const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700865 uint32_t table = getRouteTableForInterface(outputInterface);
866 if (table == RT_TABLE_UNSPEC) {
867 return -ESRCH;
868 }
869
870 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
871 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
872}
873
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900874// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900875// Returns 0 on success or negative errno on failure.
Tyler Wearfa94a272019-12-05 15:01:48 -0800876int RouteController::modifyRoute(uint16_t action, uint16_t flags, const char* interface,
877 const char* destination, const char* nexthop, TableType tableType,
878 int mtu) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700879 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700880 switch (tableType) {
881 case RouteController::INTERFACE: {
882 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700883 if (table == RT_TABLE_UNSPEC) {
884 return -ESRCH;
885 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700886 break;
887 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700888 case RouteController::LOCAL_NETWORK: {
889 table = ROUTE_TABLE_LOCAL_NETWORK;
890 break;
891 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700892 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700893 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700894 break;
895 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700896 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700897 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700898 break;
899 }
900 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700901
Tyler Wearfa94a272019-12-05 15:01:48 -0800902 int ret = modifyIpRoute(action, flags, table, interface, destination, nexthop, mtu);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700903 // Trying to add a route that already exists shouldn't cause an error.
904 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900905 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700906 }
907
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900908 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700909}
910
Hungming Chen7f725432020-02-07 17:47:23 +0800911void maybeModifyQdiscClsact(const char* interface, bool add) {
912 if (!bpf::isBpfSupported()) return;
913
914 // The clsact attaching of v4- tun interface is triggered by ClatdController::maybeStartBpf
915 // because the clat is started before the v4- interface is added to the network and the
916 // clat startup needs to add {in, e}gress filters.
917 // TODO: remove this workaround once v4- tun interface clsact attaching is moved out from
918 // ClatdController::maybeStartBpf.
919 if (StartsWith(interface, "v4-") && add) return;
920
921 // The interface may have already gone away in the delete case.
922 uint32_t ifindex = if_nametoindex(interface);
923 if (!ifindex) {
924 ALOGE("cannot find interface %s", interface);
925 return;
926 }
927
928 if (add) {
929 if (int ret = tcQdiscAddDevClsact(ifindex)) {
930 ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", ifindex, interface, strerror(-ret));
931 return;
932 }
933 } else {
934 if (int ret = tcQdiscDelDevClsact(ifindex)) {
935 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", ifindex, interface, strerror(-ret));
936 return;
937 }
938 }
939
940 return;
941}
942
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900943[[nodiscard]] static int clearTetheringRules(const char* inputInterface) {
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900944 int ret = 0;
945 while (ret == 0) {
946 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
947 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
948 }
949
950 if (ret == -ENOENT) {
951 return 0;
952 } else {
953 return ret;
954 }
955}
956
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900957uint32_t getRulePriority(const nlmsghdr *nlh) {
958 return getRtmU32Attribute(nlh, FRA_PRIORITY);
959}
960
961uint32_t getRouteTable(const nlmsghdr *nlh) {
962 return getRtmU32Attribute(nlh, RTA_TABLE);
963}
964
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900965[[nodiscard]] static int flushRules() {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900966 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
967 // Don't touch rules at priority 0 because by default they are used for local input.
968 return getRulePriority(nlh) != 0;
969 };
970 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
971}
972
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900973int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900974 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
975 return getRouteTable(nlh) == table;
976 };
977
978 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
979}
980
981// Returns 0 on success or negative errno on failure.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900982int RouteController::flushRoutes(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900983 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900984
985 uint32_t table = getRouteTableForInterfaceLocked(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900986 if (table == RT_TABLE_UNSPEC) {
987 return -ESRCH;
988 }
989
990 int ret = flushRoutes(table);
991
992 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
993 // track of its name.
994 if (ret == 0) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900995 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900996 }
997
998 return ret;
999}
1000
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001001int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -07001002 if (int ret = flushRules()) {
1003 return ret;
1004 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001005 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001006 return ret;
1007 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001008 if (int ret = addLocalNetworkRules(localNetId)) {
1009 return ret;
1010 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -07001011 if (int ret = addUnreachableRule()) {
1012 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001013 }
Lorenzo Colitti36679362015-02-25 10:26:19 +09001014 // Don't complain if we can't add the dummy network, since not all devices support it.
1015 configureDummyNetwork();
1016
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001017 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001018 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -07001019}
1020
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001021int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001022 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001023}
1024
1025int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001026 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001027}
1028
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001029int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
1030 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001031 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
1032 return ret;
1033 }
Hungming Chen7f725432020-02-07 17:47:23 +08001034 maybeModifyQdiscClsact(interface, ACTION_ADD);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001035 updateTableNamesFile();
1036 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001037}
1038
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001039int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
1040 Permission permission) {
1041 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001042 return ret;
1043 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001044 if (int ret = flushRoutes(interface)) {
1045 return ret;
1046 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001047 if (int ret = clearTetheringRules(interface)) {
1048 return ret;
1049 }
Hungming Chen7f725432020-02-07 17:47:23 +08001050 maybeModifyQdiscClsact(interface, ACTION_DEL);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001051 updateTableNamesFile();
1052 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001053}
1054
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001055int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001056 bool secure, const UidRanges& uidRanges) {
1057 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001058 MODIFY_NON_UID_BASED_RULES)) {
1059 return ret;
1060 }
1061 updateTableNamesFile();
1062 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001063}
1064
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001065int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001066 bool secure, const UidRanges& uidRanges) {
1067 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001068 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001069 return ret;
1070 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001071 if (int ret = flushRoutes(interface)) {
1072 return ret;
1073 }
1074 updateTableNamesFile();
1075 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001076}
1077
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001078int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1079 Permission oldPermission,
1080 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001081 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001082 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001083 return ret;
1084 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001085 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001086}
1087
Robin Leeb8087362016-03-30 18:43:08 +01001088int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1089 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1090}
1091
1092int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1093 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1094}
1095
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001096int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001097 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001098 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001099 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001100}
1101
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001102int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001103 bool secure, const UidRanges& uidRanges) {
1104 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001105 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001106}
1107
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001108int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1109 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001110}
1111
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001112int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1113 Permission permission) {
1114 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001115}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001116
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001117int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Tyler Wearfa94a272019-12-05 15:01:48 -08001118 TableType tableType, int mtu) {
1119 return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, interface, destination, nexthop,
1120 tableType, mtu);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001121}
1122
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001123int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001124 const char* nexthop, TableType tableType) {
Tyler Wearfa94a272019-12-05 15:01:48 -08001125 return modifyRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, interface, destination, nexthop,
1126 tableType, 0);
1127}
1128
1129int RouteController::updateRoute(const char* interface, const char* destination,
1130 const char* nexthop, TableType tableType, int mtu) {
1131 return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_REPLACE_FLAGS, interface, destination, nexthop,
1132 tableType, mtu);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001133}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001134
1135int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001136 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001137}
1138
1139int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001140 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001141}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001142
1143int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1144 Permission permission) {
1145 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1146}
1147
1148int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1149 const char* physicalInterface,
1150 Permission permission) {
1151 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1152}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001153
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001154// Protects sInterfaceToTable.
Luke Huang534980c2018-07-06 17:50:11 +08001155std::mutex RouteController::sInterfaceToTableLock;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001156std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1157
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001158} // namespace android::net