blob: c4c3cfbe835596dbbbd5c8c7562748ec9cf2bc39 [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"
Bernie Innocenti189eb502018-10-01 23:10:18 +090041#include "netid_client.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090042#include "netutils/ifc.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070043
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090044using android::base::StringPrintf;
Dan Albert5407e142015-03-16 10:05:59 -070045using android::base::WriteStringToFile;
Luke Huang94658ac2018-10-18 19:35:12 +090046using android::net::UidRangeParcel;
Dan Albert5407e142015-03-16 10:05:59 -070047
Bernie Innocenti762dcf42019-06-14 19:52:49 +090048namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090049
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090050auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand;
51
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070052// BEGIN CONSTANTS --------------------------------------------------------------------------------
53
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070054const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090055const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070056const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070057const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010058const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070059const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
60const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
61const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
62const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070063const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070064const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070065const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070066const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070067const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070068const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090069const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070070
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070071const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070072const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
73const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
74
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070075const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070076const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
77const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
78
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070079const char* const ROUTE_TABLE_NAME_LOCAL = "local";
80const char* const ROUTE_TABLE_NAME_MAIN = "main";
81
Lorenzo Colittib5d19e92017-09-25 18:39:33 +090082// None of our regular routes specify priority, which causes them to have the default priority.
83// For default throw routes, we use a fixed priority of 100000.
Lorenzo Colitti5e03a892017-09-08 11:31:59 +090084uint32_t PRIO_THROW = 100000;
85
Lorenzo Colittid78843e2017-03-27 05:52:31 +090086const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
87
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070088const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
89
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070090const uid_t UID_ROOT = 0;
Lorenzo Colitti3093f562017-09-25 14:17:38 +090091const uint32_t FWMARK_NONE = 0;
92const uint32_t MASK_NONE = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090093const char* const IIF_LOOPBACK = "lo";
Yi Kongbdfd57e2018-07-25 13:26:10 -070094const char* const IIF_NONE = nullptr;
95const char* const OIF_NONE = nullptr;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070096const bool ACTION_ADD = true;
97const bool ACTION_DEL = false;
98const bool MODIFY_NON_UID_BASED_RULES = true;
99
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700100const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700101const 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 -0700102
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700103// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
104// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900105static constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700106 return RTA_LENGTH(x);
107}
108
109// These are practically const, but can't be declared so, because they are used to initialize
110// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900111rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
112rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
113rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
114rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900115rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700116
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900117rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
118rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900119rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700120
Tyler Wearfa94a272019-12-05 15:01:48 -0800121// One or more nested attributes in the RTA_METRICS attribute.
122rtattr RTATTRX_MTU = { U16_RTA_LENGTH(sizeof(uint32_t)), RTAX_MTU};
123
124// The RTA_METRICS attribute itself.
125constexpr int RTATTR_METRICS_SIZE = sizeof(RTATTRX_MTU) + sizeof(uint32_t);
126rtattr RTATTR_METRICS = { U16_RTA_LENGTH(RTATTR_METRICS_SIZE), RTA_METRICS };
127
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700128uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
129
130// END CONSTANTS ----------------------------------------------------------------------------------
131
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900132static const char* actionName(uint16_t action) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900133 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
134 return ops[action % 4];
135}
136
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900137static const char* familyName(uint8_t family) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900138 switch (family) {
139 case AF_INET: return "IPv4";
140 case AF_INET6: return "IPv6";
141 default: return "???";
142 }
143}
144
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900145// Caller must hold sInterfaceToTableLock.
146uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface) {
mtk137999f2913e2019-02-25 19:39:36 +0800147 // If we already know the routing table for this interface name, use it.
148 // This ensures we can remove rules and routes for an interface that has been removed,
149 // or has been removed and re-added with a different interface index.
150 //
151 // The caller is responsible for ensuring that an interface is never added to a network
152 // until it has been removed from any network it was previously in. This ensures that
153 // if the same interface disconnects and then reconnects with a different interface ID
154 // when the reconnect happens the interface will not be in the map, and the code will
155 // determine the new routing table from the interface ID, below.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900156 auto iter = sInterfaceToTable.find(interface);
mtk137999f2913e2019-02-25 19:39:36 +0800157 if (iter != sInterfaceToTable.end()) {
158 return iter->second;
159 }
160
161 uint32_t index = if_nametoindex(interface);
162 if (index == 0) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700163 ALOGE("cannot find interface %s", interface);
164 return RT_TABLE_UNSPEC;
165 }
mtk137999f2913e2019-02-25 19:39:36 +0800166 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
167 sInterfaceToTable[interface] = index;
168 return index;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700169}
170
Rubin Xu6c00b612018-04-27 14:27:59 +0100171uint32_t RouteController::getIfIndex(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900172 std::lock_guard lock(sInterfaceToTableLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100173
174 auto iter = sInterfaceToTable.find(interface);
175 if (iter == sInterfaceToTable.end()) {
176 ALOGE("getIfIndex: cannot find interface %s", interface);
177 return 0;
178 }
179
180 return iter->second - ROUTE_TABLE_OFFSET_FROM_INDEX;
181}
182
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900183uint32_t RouteController::getRouteTableForInterface(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900184 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900185 return getRouteTableForInterfaceLocked(interface);
186}
187
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700188void addTableName(uint32_t table, const std::string& name, std::string* contents) {
189 char tableString[UINT32_STRLEN];
190 snprintf(tableString, sizeof(tableString), "%u", table);
191 *contents += tableString;
192 *contents += " ";
193 *contents += name;
194 *contents += "\n";
195}
196
197// 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 +0900198void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700199 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700200
201 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
202 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
203
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700204 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700205 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
206 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700207
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900208 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900209 for (const auto& entry : sInterfaceToTable) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700210 addTableName(entry.second, entry.first, &contents);
211 }
212
Dan Albert5407e142015-03-16 10:05:59 -0700213 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800214 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700215 return;
216 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700217}
218
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700219// Returns 0 on success or negative errno on failure.
220int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
221 if (!input) {
222 *length = 0;
223 *padding = 0;
224 return 0;
225 }
226 *length = strlcpy(name, input, IFNAMSIZ) + 1;
227 if (*length > IFNAMSIZ) {
228 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
229 return -ENAMETOOLONG;
230 }
231 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
232 return 0;
233}
234
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700235// Adds or removes a routing rule for IPv4 and IPv6.
236//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900237// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100238// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700239// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
240// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700241// + If |iif| is non-NULL, the rule matches the specified incoming interface.
242// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
243// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
244// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900245//
246// Returns 0 on success or negative errno on failure.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900247[[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
248 uint32_t table, uint32_t fwmark, uint32_t mask,
249 const char* iif, const char* oif, uid_t uidStart,
250 uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700251 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
252 if (fwmark & ~mask) {
253 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
254 return -ERANGE;
255 }
256
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700257 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900258 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700259 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
260 size_t iifLength, oifLength;
261 uint16_t iifPadding, oifPadding;
262 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
263 return ret;
264 }
265 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
266 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900267 }
268
Lorenzo Colitti59656512014-06-25 03:20:29 +0900269 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700270 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700271 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900272 return -EUSERS;
273 }
Robin Lee4ef94642016-04-01 11:50:49 +0100274
Lorenzo Colitti72723682014-06-26 13:51:10 +0900275 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900276
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900277 // Assemble a rule request and put it in an array of iovec structures.
278 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100279 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900280 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
281 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900282 };
283
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900284 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
285 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
286 // table 0, it's a wildcard that matches anything.
287 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
288 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
289 return -ENOTUNIQ;
290 }
291
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700292 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
293 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900294 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900295
296 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700297 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700298 { &rule, sizeof(rule) },
299 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
300 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700301 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
302 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700303 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
304 { &fwmark, mask ? sizeof(fwmark) : 0 },
305 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
306 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900307 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
308 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700309 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
310 { iifName, iifLength },
311 { PADDING_BUFFER, iifPadding },
312 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
313 { oifName, oifLength },
314 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900315 };
316
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900317 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700318 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
319 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900320 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900321 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
322 // Don't log when deleting a tethering rule that's not there. This matches the
323 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
324 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
325 strerror(-ret));
326 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900327 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700328 }
329 }
330
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900331 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700332}
333
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900334[[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
335 uint32_t fwmark, uint32_t mask, const char* iif,
336 const char* oif, uid_t uidStart, uid_t uidEnd) {
Robin Lee4ef94642016-04-01 11:50:49 +0100337 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
338 uidEnd);
339}
340
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900341[[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
342 uint32_t fwmark, uint32_t mask) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700343 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
344 INVALID_UID);
345}
346
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900347// Adds or deletes an IPv4 or IPv6 route.
348// Returns 0 on success or negative errno on failure.
Tyler Wearfa94a272019-12-05 15:01:48 -0800349int modifyIpRoute(uint16_t action, uint16_t flags, uint32_t table, const char* interface,
350 const char* destination, const char* nexthop, uint32_t mtu) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900351 // At least the destination must be non-null.
352 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700353 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900354 return -EFAULT;
355 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700356
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900357 // Parse the prefix.
358 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700359 uint8_t family;
360 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900361 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
362 &prefixLength);
363 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700364 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900365 return rawLength;
366 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700367
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900368 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700369 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900370 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
371 }
372
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700373 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900374 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900375 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700376
377 if (nexthop && !strcmp(nexthop, "unreachable")) {
378 type = RTN_UNREACHABLE;
379 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
380 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
381 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
382 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700383 nexthop = nullptr;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900384 } else if (nexthop && !strcmp(nexthop, "throw")) {
385 type = RTN_THROW;
386 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700387 nexthop = nullptr;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700388 } else {
389 // If an interface was specified, find the ifindex.
390 if (interface != OIF_NONE) {
391 ifindex = if_nametoindex(interface);
392 if (!ifindex) {
393 ALOGE("cannot find interface %s", interface);
394 return -ENODEV;
395 }
396 }
397
398 // If a nexthop was specified, parse it as the same family as the prefix.
399 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
400 ALOGE("inet_pton failed for nexthop %s", nexthop);
401 return -EINVAL;
402 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900403 }
404
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900405 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
406
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900407 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700408 rtmsg route = {
Nick Desaulniers6b357502019-10-11 09:26:44 -0700409 .rtm_family = family,
410 .rtm_dst_len = prefixLength,
411 .rtm_protocol = RTPROT_STATIC,
412 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
413 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900414 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900415
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700416 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
417 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900418
419 iovec iov[] = {
Tyler Wearfa94a272019-12-05 15:01:48 -0800420 { nullptr, 0 },
421 { &route, sizeof(route) },
422 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
423 { &table, sizeof(table) },
424 { &rtaDst, sizeof(rtaDst) },
425 { rawAddress, static_cast<size_t>(rawLength) },
426 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
427 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
428 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
429 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
430 { &RTATTR_METRICS, mtu != 0 ? sizeof(RTATTR_METRICS) : 0 },
431 { &RTATTRX_MTU, mtu != 0 ? sizeof(RTATTRX_MTU) : 0 },
432 { &mtu, mtu != 0 ? sizeof(mtu) : 0 },
433 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
434 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900435 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900436
Jimmy Chence9e5782019-03-08 16:12:55 +0800437 // Allow creating multiple link-local routes in the same table, so we can make IPv6
438 // work on all interfaces in the local_network table.
439 if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) {
440 flags &= ~NLM_F_EXCL;
441 }
442
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900443 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900444 if (ret) {
445 ALOGE("Error %s route %s -> %s %s to table %u: %s",
446 actionName(action), destination, nexthop, interface, table, strerror(-ret));
447 }
448 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700449}
450
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700451// An iptables rule to mark incoming packets on a network with the netId of the network.
452//
453// This is so that the kernel can:
454// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
455// replies, SYN-ACKs, etc).
456// + Mark sockets that accept connections from this interface so that the connection stays on the
457// same interface.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900458int modifyIncomingPacketMark(unsigned netId, const char* interface, Permission permission,
459 bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700460 Fwmark fwmark;
461
462 fwmark.netId = netId;
463 fwmark.explicitlySelected = true;
464 fwmark.protectedFromVpn = true;
465 fwmark.permission = permission;
466
Benedict Wongb9baf262017-12-03 15:43:08 -0800467 const uint32_t mask = ~Fwmark::getUidBillingMask();
468
469 std::string cmd = StringPrintf(
470 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
471 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900472 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700473 ALOGE("failed to change iptables rule that sets incoming packet mark");
474 return -EREMOTEIO;
475 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700476
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700477 return 0;
478}
479
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700480// A rule to route responses to the local network forwarded via the VPN.
481//
482// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
483// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900484[[nodiscard]] static int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700485 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
486 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
487 INVALID_UID, INVALID_UID);
488}
489
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700490// A rule to route all traffic from a given set of UIDs to go over the VPN.
491//
492// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
493// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
494// bypass the VPN if the protectedFromVpn bit is set.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900495[[nodiscard]] static int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
496 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700497 Fwmark fwmark;
498 Fwmark mask;
499
500 fwmark.protectedFromVpn = false;
501 mask.protectedFromVpn = true;
502
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700503 uint32_t priority;
504
505 if (secure) {
506 priority = RULE_PRIORITY_SECURE_VPN;
507 } else {
508 priority = RULE_PRIORITY_BYPASSABLE_VPN;
509
510 fwmark.explicitlySelected = false;
511 mask.explicitlySelected = true;
512 }
513
514 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900515 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700516}
517
518// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
519// set of UIDs.
520//
521// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
522// target set, but where the DnsProxyListener itself is not.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900523[[nodiscard]] static int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
524 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700525 Fwmark fwmark;
526 Fwmark mask;
527
528 fwmark.netId = netId;
529 mask.netId = FWMARK_NET_ID_MASK;
530
531 fwmark.permission = PERMISSION_SYSTEM;
532 mask.permission = PERMISSION_SYSTEM;
533
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700534 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
535
536 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
537 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700538}
539
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700540// A rule to route traffic based on an explicitly chosen network.
541//
542// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
543//
544// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
545// to check it again in the rules here, because a network's permissions may have been updated via
546// modifyNetworkPermission().
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900547[[nodiscard]] static int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
548 Permission permission, uid_t uidStart,
549 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700550 Fwmark fwmark;
551 Fwmark mask;
552
553 fwmark.netId = netId;
554 mask.netId = FWMARK_NET_ID_MASK;
555
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700556 fwmark.explicitlySelected = true;
557 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700558
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700559 fwmark.permission = permission;
560 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700561
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700562 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900563 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700564}
565
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700566// A rule to route traffic based on a chosen outgoing interface.
567//
568// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
569// the outgoing interface (typically for link-local communications).
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900570[[nodiscard]] static int modifyOutputInterfaceRules(const char* interface, uint32_t table,
571 Permission permission, uid_t uidStart,
572 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700573 Fwmark fwmark;
574 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700575
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700576 fwmark.permission = permission;
577 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700578
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900579 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900580 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
581 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900582 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
583 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900584 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900585 UID_ROOT, UID_ROOT)) {
586 return ret;
587 }
588 }
589
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700590 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900591 fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700592}
593
594// A rule to route traffic based on the chosen network.
595//
596// This is for sockets that have not explicitly requested a particular network, but have been
597// bound to one when they called connect(). This ensures that sockets connected on a particular
598// network stay on that network even if the default network changes.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900599[[nodiscard]] static int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700600 Fwmark fwmark;
601 Fwmark mask;
602
603 fwmark.netId = netId;
604 mask.netId = FWMARK_NET_ID_MASK;
605
606 fwmark.explicitlySelected = false;
607 mask.explicitlySelected = true;
608
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900609 fwmark.permission = PERMISSION_NONE;
610 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700611
612 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900613 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
614 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700615}
616
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700617// A rule to enable split tunnel VPNs.
618//
619// 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 +0800620// go over the default network, provided it has the permissions required by the default network.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900621int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
622 const char* physicalInterface,
623 Permission permission) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700624 uint32_t table = getRouteTableForInterface(physicalInterface);
625 if (table == RT_TABLE_UNSPEC) {
626 return -ESRCH;
627 }
628
629 Fwmark fwmark;
630 Fwmark mask;
631
632 fwmark.netId = vpnNetId;
633 mask.netId = FWMARK_NET_ID_MASK;
634
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700635 fwmark.permission = permission;
636 mask.permission = permission;
637
638 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
639 mask.intValue);
640}
641
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700642// Add rules to allow legacy routes added through the requestRouteToHost() API.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900643[[nodiscard]] static int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700644 Fwmark fwmark;
645 Fwmark mask;
646
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700647 fwmark.explicitlySelected = false;
648 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700649
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700650 // Rules to allow legacy routes to override the default network.
651 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
652 fwmark.intValue, mask.intValue)) {
653 return ret;
654 }
655 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
656 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
657 return ret;
658 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700659
660 fwmark.permission = PERMISSION_SYSTEM;
661 mask.permission = PERMISSION_SYSTEM;
662
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700663 // A rule to allow legacy routes from system apps to override VPNs.
664 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700665 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700666}
667
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700668// Add rules to lookup the local network when specified explicitly or otherwise.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900669[[nodiscard]] static int addLocalNetworkRules(unsigned localNetId) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700670 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
671 INVALID_UID, INVALID_UID, ACTION_ADD)) {
672 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700673 }
674
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700675 Fwmark fwmark;
676 Fwmark mask;
677
678 fwmark.explicitlySelected = false;
679 mask.explicitlySelected = true;
680
681 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
682 fwmark.intValue, mask.intValue);
683}
684
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900685/* static */
686int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900687 const char *interface = DummyNetwork::INTERFACE_NAME;
688 uint32_t table = getRouteTableForInterface(interface);
689 if (table == RT_TABLE_UNSPEC) {
690 // getRouteTableForInterface has already looged an error.
691 return -ESRCH;
692 }
693
694 ifc_init();
695 int ret = ifc_up(interface);
696 ifc_close();
697 if (ret) {
698 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
699 return -errno;
700 }
701
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900702 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
703 INVALID_UID, INVALID_UID, ACTION_ADD))) {
704 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900705 return ret;
706 }
707
Tyler Wearfa94a272019-12-05 15:01:48 -0800708 if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface,
709 "0.0.0.0/0", nullptr, 0 /* mtu */))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900710 return ret;
711 }
712
Tyler Wearfa94a272019-12-05 15:01:48 -0800713 if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface, "::/0",
714 nullptr, 0 /* mtu */))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900715 return ret;
716 }
717
718 return 0;
719}
720
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900721// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
722// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
723// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
724// rule will hopefully make things even clearer.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900725[[nodiscard]] static int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100726 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
727 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700728}
729
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900730[[nodiscard]] static int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700731 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
732 return ret;
733 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900734 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
735 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700736}
737
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900738/* static */
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900739int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
740 Permission permission, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700741 uint32_t table = getRouteTableForInterface(interface);
742 if (table == RT_TABLE_UNSPEC) {
743 return -ESRCH;
744 }
745
746 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
747 return ret;
748 }
749 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
750 add)) {
751 return ret;
752 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900753 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700754 add)) {
755 return ret;
756 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900757
758 // Only set implicit rules for networks that don't require permissions.
759 //
760 // This is so that if the default network ceases to be the default network and then switches
761 // from requiring no permissions to requiring permissions, we ensure that apps only use the
762 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
763 // - it closes all sockets on the network except sockets that are explicitly selected.
764 //
765 // The lack of this rule only affects the special case above, because:
766 // - The only cases where we implicitly bind a socket to a network are the default network and
767 // the bypassable VPN that applies to the app, if any.
768 // - This rule doesn't affect VPNs because they don't support permissions at all.
769 // - The default network doesn't require permissions. While we support doing this, the framework
770 // never does it (partly because we'd end up in the situation where we tell apps that there is
771 // a default network, but they can't use it).
772 // - If the network is still the default network, the presence or absence of this rule does not
773 // matter.
774 //
775 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
776 // implicitly bound to a network because at the time of connect() it was the default, and that
777 // network must no longer be the default, and must now require permissions.
778 if (permission == PERMISSION_NONE) {
779 return modifyImplicitNetworkRule(netId, table, add);
780 }
781 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700782}
783
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900784[[nodiscard]] static int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
Robin Leeb8087362016-03-30 18:43:08 +0100785 Fwmark fwmark;
786 Fwmark mask;
787 fwmark.protectedFromVpn = false;
788 mask.protectedFromVpn = true;
789
Luke Huang94658ac2018-10-18 19:35:12 +0900790 for (const UidRangeParcel& range : uidRanges.getRanges()) {
791 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN,
792 FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
793 IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) {
Robin Leeb8087362016-03-30 18:43:08 +0100794 return ret;
795 }
796 }
797
798 return 0;
799}
800
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900801int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
802 const UidRanges& uidRanges, bool secure, bool add,
803 bool modifyNonUidBasedRules) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700804 uint32_t table = getRouteTableForInterface(interface);
805 if (table == RT_TABLE_UNSPEC) {
806 return -ESRCH;
807 }
808
Luke Huang94658ac2018-10-18 19:35:12 +0900809 for (const UidRangeParcel& range : uidRanges.getRanges()) {
810 if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, secure, add)) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700811 return ret;
812 }
Luke Huang94658ac2018-10-18 19:35:12 +0900813 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start,
814 range.stop, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700815 return ret;
816 }
Luke Huang94658ac2018-10-18 19:35:12 +0900817 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start,
818 range.stop, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700819 return ret;
820 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700821 }
822
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700823 if (modifyNonUidBasedRules) {
824 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700825 return ret;
826 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700827 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
828 return ret;
829 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700830 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700831 return ret;
832 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700833 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700834 }
835
836 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700837}
838
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900839int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
840 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700841 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700842 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900843 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700844 }
845
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700846 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700847 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700848
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700849 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700850 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700851
852 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700853 mask.permission = permission;
854
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700855 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900856 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700857}
858
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900859int RouteController::modifyTetheredNetwork(uint16_t action, const char* inputInterface,
860 const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700861 uint32_t table = getRouteTableForInterface(outputInterface);
862 if (table == RT_TABLE_UNSPEC) {
863 return -ESRCH;
864 }
865
866 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
867 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
868}
869
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900870// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900871// Returns 0 on success or negative errno on failure.
Tyler Wearfa94a272019-12-05 15:01:48 -0800872int RouteController::modifyRoute(uint16_t action, uint16_t flags, const char* interface,
873 const char* destination, const char* nexthop, TableType tableType,
874 int mtu) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700875 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700876 switch (tableType) {
877 case RouteController::INTERFACE: {
878 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700879 if (table == RT_TABLE_UNSPEC) {
880 return -ESRCH;
881 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700882 break;
883 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700884 case RouteController::LOCAL_NETWORK: {
885 table = ROUTE_TABLE_LOCAL_NETWORK;
886 break;
887 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700888 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700889 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700890 break;
891 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700892 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700893 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700894 break;
895 }
896 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700897
Tyler Wearfa94a272019-12-05 15:01:48 -0800898 int ret = modifyIpRoute(action, flags, table, interface, destination, nexthop, mtu);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700899 // Trying to add a route that already exists shouldn't cause an error.
900 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900901 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700902 }
903
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900904 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700905}
906
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900907[[nodiscard]] static int clearTetheringRules(const char* inputInterface) {
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900908 int ret = 0;
909 while (ret == 0) {
910 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
911 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
912 }
913
914 if (ret == -ENOENT) {
915 return 0;
916 } else {
917 return ret;
918 }
919}
920
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900921uint32_t getRulePriority(const nlmsghdr *nlh) {
922 return getRtmU32Attribute(nlh, FRA_PRIORITY);
923}
924
925uint32_t getRouteTable(const nlmsghdr *nlh) {
926 return getRtmU32Attribute(nlh, RTA_TABLE);
927}
928
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900929[[nodiscard]] static int flushRules() {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900930 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
931 // Don't touch rules at priority 0 because by default they are used for local input.
932 return getRulePriority(nlh) != 0;
933 };
934 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
935}
936
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900937int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900938 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
939 return getRouteTable(nlh) == table;
940 };
941
942 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
943}
944
945// Returns 0 on success or negative errno on failure.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900946int RouteController::flushRoutes(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900947 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900948
949 uint32_t table = getRouteTableForInterfaceLocked(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900950 if (table == RT_TABLE_UNSPEC) {
951 return -ESRCH;
952 }
953
954 int ret = flushRoutes(table);
955
956 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
957 // track of its name.
958 if (ret == 0) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900959 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900960 }
961
962 return ret;
963}
964
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700965int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700966 if (int ret = flushRules()) {
967 return ret;
968 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700969 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700970 return ret;
971 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700972 if (int ret = addLocalNetworkRules(localNetId)) {
973 return ret;
974 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700975 if (int ret = addUnreachableRule()) {
976 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700977 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900978 // Don't complain if we can't add the dummy network, since not all devices support it.
979 configureDummyNetwork();
980
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700981 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700982 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700983}
984
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700985int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700986 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700987}
988
989int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700990 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700991}
992
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700993int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
994 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700995 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
996 return ret;
997 }
998 updateTableNamesFile();
999 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001000}
1001
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001002int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
1003 Permission permission) {
1004 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001005 return ret;
1006 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001007 if (int ret = flushRoutes(interface)) {
1008 return ret;
1009 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001010 if (int ret = clearTetheringRules(interface)) {
1011 return ret;
1012 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001013 updateTableNamesFile();
1014 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001015}
1016
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001017int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001018 bool secure, const UidRanges& uidRanges) {
1019 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001020 MODIFY_NON_UID_BASED_RULES)) {
1021 return ret;
1022 }
1023 updateTableNamesFile();
1024 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001025}
1026
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001027int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001028 bool secure, const UidRanges& uidRanges) {
1029 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001030 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001031 return ret;
1032 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001033 if (int ret = flushRoutes(interface)) {
1034 return ret;
1035 }
1036 updateTableNamesFile();
1037 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001038}
1039
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001040int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1041 Permission oldPermission,
1042 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001043 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001044 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001045 return ret;
1046 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001047 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001048}
1049
Robin Leeb8087362016-03-30 18:43:08 +01001050int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1051 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1052}
1053
1054int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1055 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1056}
1057
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001058int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001059 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001060 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001061 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001062}
1063
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001064int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001065 bool secure, const UidRanges& uidRanges) {
1066 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001067 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001068}
1069
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001070int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1071 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001072}
1073
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001074int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1075 Permission permission) {
1076 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001077}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001078
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001079int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Tyler Wearfa94a272019-12-05 15:01:48 -08001080 TableType tableType, int mtu) {
1081 return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, interface, destination, nexthop,
1082 tableType, mtu);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001083}
1084
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001085int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001086 const char* nexthop, TableType tableType) {
Tyler Wearfa94a272019-12-05 15:01:48 -08001087 return modifyRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, interface, destination, nexthop,
1088 tableType, 0);
1089}
1090
1091int RouteController::updateRoute(const char* interface, const char* destination,
1092 const char* nexthop, TableType tableType, int mtu) {
1093 return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_REPLACE_FLAGS, interface, destination, nexthop,
1094 tableType, mtu);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001095}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001096
1097int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001098 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001099}
1100
1101int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001102 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001103}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001104
1105int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1106 Permission permission) {
1107 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1108}
1109
1110int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1111 const char* physicalInterface,
1112 Permission permission) {
1113 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1114}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001115
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001116// Protects sInterfaceToTable.
Luke Huang534980c2018-07-06 17:50:11 +08001117std::mutex RouteController::sInterfaceToTableLock;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001118std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1119
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001120} // namespace android::net