blob: c2163cd3bb0ea23fd82d4e572e7c477fb6b6d790 [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};
Lorenzo Colitti57d54682020-01-24 09:18:13 +0900123constexpr size_t RTATTRX_MTU_SIZE = RTA_SPACE(sizeof(uint32_t));
Tyler Wearfa94a272019-12-05 15:01:48 -0800124
125// The RTA_METRICS attribute itself.
Lorenzo Colitti57d54682020-01-24 09:18:13 +0900126constexpr size_t RTATTR_METRICS_SIZE = RTATTRX_MTU_SIZE;
Tyler Wearfa94a272019-12-05 15:01:48 -0800127rtattr RTATTR_METRICS = { U16_RTA_LENGTH(RTATTR_METRICS_SIZE), RTA_METRICS };
128
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700129uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
130
131// END CONSTANTS ----------------------------------------------------------------------------------
132
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900133static const char* actionName(uint16_t action) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900134 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
135 return ops[action % 4];
136}
137
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900138static const char* familyName(uint8_t family) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900139 switch (family) {
140 case AF_INET: return "IPv4";
141 case AF_INET6: return "IPv6";
142 default: return "???";
143 }
144}
145
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900146// Caller must hold sInterfaceToTableLock.
147uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface) {
mtk137999f2913e2019-02-25 19:39:36 +0800148 // If we already know the routing table for this interface name, use it.
149 // This ensures we can remove rules and routes for an interface that has been removed,
150 // or has been removed and re-added with a different interface index.
151 //
152 // The caller is responsible for ensuring that an interface is never added to a network
153 // until it has been removed from any network it was previously in. This ensures that
154 // if the same interface disconnects and then reconnects with a different interface ID
155 // when the reconnect happens the interface will not be in the map, and the code will
156 // determine the new routing table from the interface ID, below.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900157 auto iter = sInterfaceToTable.find(interface);
mtk137999f2913e2019-02-25 19:39:36 +0800158 if (iter != sInterfaceToTable.end()) {
159 return iter->second;
160 }
161
162 uint32_t index = if_nametoindex(interface);
163 if (index == 0) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700164 ALOGE("cannot find interface %s", interface);
165 return RT_TABLE_UNSPEC;
166 }
mtk137999f2913e2019-02-25 19:39:36 +0800167 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
168 sInterfaceToTable[interface] = index;
169 return index;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700170}
171
Rubin Xu6c00b612018-04-27 14:27:59 +0100172uint32_t RouteController::getIfIndex(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900173 std::lock_guard lock(sInterfaceToTableLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100174
175 auto iter = sInterfaceToTable.find(interface);
176 if (iter == sInterfaceToTable.end()) {
177 ALOGE("getIfIndex: cannot find interface %s", interface);
178 return 0;
179 }
180
181 return iter->second - ROUTE_TABLE_OFFSET_FROM_INDEX;
182}
183
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900184uint32_t RouteController::getRouteTableForInterface(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900185 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900186 return getRouteTableForInterfaceLocked(interface);
187}
188
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700189void addTableName(uint32_t table, const std::string& name, std::string* contents) {
190 char tableString[UINT32_STRLEN];
191 snprintf(tableString, sizeof(tableString), "%u", table);
192 *contents += tableString;
193 *contents += " ";
194 *contents += name;
195 *contents += "\n";
196}
197
198// 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 +0900199void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700200 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700201
202 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
203 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
204
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700205 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700206 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
207 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700208
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900209 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900210 for (const auto& entry : sInterfaceToTable) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700211 addTableName(entry.second, entry.first, &contents);
212 }
213
Dan Albert5407e142015-03-16 10:05:59 -0700214 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800215 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700216 return;
217 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700218}
219
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700220// Returns 0 on success or negative errno on failure.
221int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
222 if (!input) {
223 *length = 0;
224 *padding = 0;
225 return 0;
226 }
227 *length = strlcpy(name, input, IFNAMSIZ) + 1;
228 if (*length > IFNAMSIZ) {
229 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
230 return -ENAMETOOLONG;
231 }
232 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
233 return 0;
234}
235
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700236// Adds or removes a routing rule for IPv4 and IPv6.
237//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900238// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100239// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700240// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
241// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700242// + If |iif| is non-NULL, the rule matches the specified incoming interface.
243// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
244// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
245// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900246//
247// Returns 0 on success or negative errno on failure.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900248[[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
249 uint32_t table, uint32_t fwmark, uint32_t mask,
250 const char* iif, const char* oif, uid_t uidStart,
251 uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700252 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
253 if (fwmark & ~mask) {
254 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
255 return -ERANGE;
256 }
257
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700258 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900259 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700260 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
261 size_t iifLength, oifLength;
262 uint16_t iifPadding, oifPadding;
263 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
264 return ret;
265 }
266 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
267 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900268 }
269
Lorenzo Colitti59656512014-06-25 03:20:29 +0900270 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700271 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700272 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900273 return -EUSERS;
274 }
Robin Lee4ef94642016-04-01 11:50:49 +0100275
Lorenzo Colitti72723682014-06-26 13:51:10 +0900276 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900277
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900278 // Assemble a rule request and put it in an array of iovec structures.
279 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100280 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900281 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
282 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900283 };
284
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900285 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
286 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
287 // table 0, it's a wildcard that matches anything.
288 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
289 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
290 return -ENOTUNIQ;
291 }
292
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700293 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
294 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900295 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900296
297 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700298 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700299 { &rule, sizeof(rule) },
300 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
301 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700302 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
303 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700304 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
305 { &fwmark, mask ? sizeof(fwmark) : 0 },
306 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
307 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900308 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
309 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700310 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
311 { iifName, iifLength },
312 { PADDING_BUFFER, iifPadding },
313 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
314 { oifName, oifLength },
315 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900316 };
317
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900318 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700319 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
320 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900321 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900322 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
323 // Don't log when deleting a tethering rule that's not there. This matches the
324 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
325 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
326 strerror(-ret));
327 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900328 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700329 }
330 }
331
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900332 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700333}
334
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900335[[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
336 uint32_t fwmark, uint32_t mask, const char* iif,
337 const char* oif, uid_t uidStart, uid_t uidEnd) {
Robin Lee4ef94642016-04-01 11:50:49 +0100338 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
339 uidEnd);
340}
341
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900342[[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
343 uint32_t fwmark, uint32_t mask) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700344 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
345 INVALID_UID);
346}
347
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900348// Adds or deletes an IPv4 or IPv6 route.
349// Returns 0 on success or negative errno on failure.
Tyler Wearfa94a272019-12-05 15:01:48 -0800350int modifyIpRoute(uint16_t action, uint16_t flags, uint32_t table, const char* interface,
351 const char* destination, const char* nexthop, uint32_t mtu) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900352 // At least the destination must be non-null.
353 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700354 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900355 return -EFAULT;
356 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700357
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900358 // Parse the prefix.
359 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700360 uint8_t family;
361 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900362 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
363 &prefixLength);
364 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700365 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900366 return rawLength;
367 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700368
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900369 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700370 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900371 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
372 }
373
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700374 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900375 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900376 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700377
378 if (nexthop && !strcmp(nexthop, "unreachable")) {
379 type = RTN_UNREACHABLE;
380 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
381 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
382 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
383 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700384 nexthop = nullptr;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900385 } else if (nexthop && !strcmp(nexthop, "throw")) {
386 type = RTN_THROW;
387 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700388 nexthop = nullptr;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700389 } else {
390 // If an interface was specified, find the ifindex.
391 if (interface != OIF_NONE) {
392 ifindex = if_nametoindex(interface);
393 if (!ifindex) {
394 ALOGE("cannot find interface %s", interface);
395 return -ENODEV;
396 }
397 }
398
399 // If a nexthop was specified, parse it as the same family as the prefix.
400 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
401 ALOGE("inet_pton failed for nexthop %s", nexthop);
402 return -EINVAL;
403 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900404 }
405
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900406 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
407
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900408 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700409 rtmsg route = {
Nick Desaulniers6b357502019-10-11 09:26:44 -0700410 .rtm_family = family,
411 .rtm_dst_len = prefixLength,
412 .rtm_protocol = RTPROT_STATIC,
413 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
414 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900415 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900416
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700417 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
418 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900419
420 iovec iov[] = {
Tyler Wearfa94a272019-12-05 15:01:48 -0800421 { nullptr, 0 },
422 { &route, sizeof(route) },
423 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
424 { &table, sizeof(table) },
425 { &rtaDst, sizeof(rtaDst) },
426 { rawAddress, static_cast<size_t>(rawLength) },
427 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
428 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
429 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
430 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
431 { &RTATTR_METRICS, mtu != 0 ? sizeof(RTATTR_METRICS) : 0 },
432 { &RTATTRX_MTU, mtu != 0 ? sizeof(RTATTRX_MTU) : 0 },
433 { &mtu, mtu != 0 ? sizeof(mtu) : 0 },
434 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
435 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900436 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900437
Jimmy Chence9e5782019-03-08 16:12:55 +0800438 // Allow creating multiple link-local routes in the same table, so we can make IPv6
439 // work on all interfaces in the local_network table.
440 if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) {
441 flags &= ~NLM_F_EXCL;
442 }
443
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900444 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900445 if (ret) {
446 ALOGE("Error %s route %s -> %s %s to table %u: %s",
447 actionName(action), destination, nexthop, interface, table, strerror(-ret));
448 }
449 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700450}
451
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700452// An iptables rule to mark incoming packets on a network with the netId of the network.
453//
454// This is so that the kernel can:
455// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
456// replies, SYN-ACKs, etc).
457// + Mark sockets that accept connections from this interface so that the connection stays on the
458// same interface.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900459int modifyIncomingPacketMark(unsigned netId, const char* interface, Permission permission,
460 bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700461 Fwmark fwmark;
462
463 fwmark.netId = netId;
464 fwmark.explicitlySelected = true;
465 fwmark.protectedFromVpn = true;
466 fwmark.permission = permission;
467
Benedict Wongb9baf262017-12-03 15:43:08 -0800468 const uint32_t mask = ~Fwmark::getUidBillingMask();
469
470 std::string cmd = StringPrintf(
471 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
472 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900473 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700474 ALOGE("failed to change iptables rule that sets incoming packet mark");
475 return -EREMOTEIO;
476 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700477
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700478 return 0;
479}
480
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700481// A rule to route responses to the local network forwarded via the VPN.
482//
483// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
484// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900485[[nodiscard]] static int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700486 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
487 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
488 INVALID_UID, INVALID_UID);
489}
490
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700491// A rule to route all traffic from a given set of UIDs to go over the VPN.
492//
493// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
494// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
495// bypass the VPN if the protectedFromVpn bit is set.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900496[[nodiscard]] static int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
497 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700498 Fwmark fwmark;
499 Fwmark mask;
500
501 fwmark.protectedFromVpn = false;
502 mask.protectedFromVpn = true;
503
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700504 uint32_t priority;
505
506 if (secure) {
507 priority = RULE_PRIORITY_SECURE_VPN;
508 } else {
509 priority = RULE_PRIORITY_BYPASSABLE_VPN;
510
511 fwmark.explicitlySelected = false;
512 mask.explicitlySelected = true;
513 }
514
515 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900516 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700517}
518
519// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
520// set of UIDs.
521//
522// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
523// target set, but where the DnsProxyListener itself is not.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900524[[nodiscard]] static int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
525 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700526 Fwmark fwmark;
527 Fwmark mask;
528
529 fwmark.netId = netId;
530 mask.netId = FWMARK_NET_ID_MASK;
531
532 fwmark.permission = PERMISSION_SYSTEM;
533 mask.permission = PERMISSION_SYSTEM;
534
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700535 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
536
537 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
538 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700539}
540
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700541// A rule to route traffic based on an explicitly chosen network.
542//
543// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
544//
545// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
546// to check it again in the rules here, because a network's permissions may have been updated via
547// modifyNetworkPermission().
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900548[[nodiscard]] static int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
549 Permission permission, uid_t uidStart,
550 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700551 Fwmark fwmark;
552 Fwmark mask;
553
554 fwmark.netId = netId;
555 mask.netId = FWMARK_NET_ID_MASK;
556
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700557 fwmark.explicitlySelected = true;
558 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700559
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700560 fwmark.permission = permission;
561 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700562
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700563 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900564 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700565}
566
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700567// A rule to route traffic based on a chosen outgoing interface.
568//
569// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
570// the outgoing interface (typically for link-local communications).
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900571[[nodiscard]] static int modifyOutputInterfaceRules(const char* interface, uint32_t table,
572 Permission permission, uid_t uidStart,
573 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700574 Fwmark fwmark;
575 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700576
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700577 fwmark.permission = permission;
578 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700579
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900580 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900581 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
582 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900583 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
584 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900585 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900586 UID_ROOT, UID_ROOT)) {
587 return ret;
588 }
589 }
590
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700591 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900592 fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700593}
594
595// A rule to route traffic based on the chosen network.
596//
597// This is for sockets that have not explicitly requested a particular network, but have been
598// bound to one when they called connect(). This ensures that sockets connected on a particular
599// network stay on that network even if the default network changes.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900600[[nodiscard]] static int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700601 Fwmark fwmark;
602 Fwmark mask;
603
604 fwmark.netId = netId;
605 mask.netId = FWMARK_NET_ID_MASK;
606
607 fwmark.explicitlySelected = false;
608 mask.explicitlySelected = true;
609
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900610 fwmark.permission = PERMISSION_NONE;
611 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700612
613 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900614 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
615 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700616}
617
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700618// A rule to enable split tunnel VPNs.
619//
620// 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 +0800621// go over the default network, provided it has the permissions required by the default network.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900622int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
623 const char* physicalInterface,
624 Permission permission) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700625 uint32_t table = getRouteTableForInterface(physicalInterface);
626 if (table == RT_TABLE_UNSPEC) {
627 return -ESRCH;
628 }
629
630 Fwmark fwmark;
631 Fwmark mask;
632
633 fwmark.netId = vpnNetId;
634 mask.netId = FWMARK_NET_ID_MASK;
635
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700636 fwmark.permission = permission;
637 mask.permission = permission;
638
639 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
640 mask.intValue);
641}
642
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700643// Add rules to allow legacy routes added through the requestRouteToHost() API.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900644[[nodiscard]] static int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700645 Fwmark fwmark;
646 Fwmark mask;
647
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700648 fwmark.explicitlySelected = false;
649 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700650
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700651 // Rules to allow legacy routes to override the default network.
652 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
653 fwmark.intValue, mask.intValue)) {
654 return ret;
655 }
656 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
657 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
658 return ret;
659 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700660
661 fwmark.permission = PERMISSION_SYSTEM;
662 mask.permission = PERMISSION_SYSTEM;
663
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700664 // A rule to allow legacy routes from system apps to override VPNs.
665 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700666 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700667}
668
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700669// Add rules to lookup the local network when specified explicitly or otherwise.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900670[[nodiscard]] static int addLocalNetworkRules(unsigned localNetId) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700671 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
672 INVALID_UID, INVALID_UID, ACTION_ADD)) {
673 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700674 }
675
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700676 Fwmark fwmark;
677 Fwmark mask;
678
679 fwmark.explicitlySelected = false;
680 mask.explicitlySelected = true;
681
682 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
683 fwmark.intValue, mask.intValue);
684}
685
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900686/* static */
687int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900688 const char *interface = DummyNetwork::INTERFACE_NAME;
689 uint32_t table = getRouteTableForInterface(interface);
690 if (table == RT_TABLE_UNSPEC) {
691 // getRouteTableForInterface has already looged an error.
692 return -ESRCH;
693 }
694
695 ifc_init();
696 int ret = ifc_up(interface);
697 ifc_close();
698 if (ret) {
699 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
700 return -errno;
701 }
702
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900703 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
704 INVALID_UID, INVALID_UID, ACTION_ADD))) {
705 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900706 return ret;
707 }
708
Tyler Wearfa94a272019-12-05 15:01:48 -0800709 if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface,
710 "0.0.0.0/0", nullptr, 0 /* mtu */))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900711 return ret;
712 }
713
Tyler Wearfa94a272019-12-05 15:01:48 -0800714 if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface, "::/0",
715 nullptr, 0 /* mtu */))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900716 return ret;
717 }
718
719 return 0;
720}
721
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900722// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
723// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
724// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
725// rule will hopefully make things even clearer.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900726[[nodiscard]] static int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100727 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
728 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700729}
730
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900731[[nodiscard]] static int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700732 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
733 return ret;
734 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900735 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
736 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700737}
738
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900739/* static */
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900740int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
741 Permission permission, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700742 uint32_t table = getRouteTableForInterface(interface);
743 if (table == RT_TABLE_UNSPEC) {
744 return -ESRCH;
745 }
746
747 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
748 return ret;
749 }
750 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
751 add)) {
752 return ret;
753 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900754 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700755 add)) {
756 return ret;
757 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900758
759 // Only set implicit rules for networks that don't require permissions.
760 //
761 // This is so that if the default network ceases to be the default network and then switches
762 // from requiring no permissions to requiring permissions, we ensure that apps only use the
763 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
764 // - it closes all sockets on the network except sockets that are explicitly selected.
765 //
766 // The lack of this rule only affects the special case above, because:
767 // - The only cases where we implicitly bind a socket to a network are the default network and
768 // the bypassable VPN that applies to the app, if any.
769 // - This rule doesn't affect VPNs because they don't support permissions at all.
770 // - The default network doesn't require permissions. While we support doing this, the framework
771 // never does it (partly because we'd end up in the situation where we tell apps that there is
772 // a default network, but they can't use it).
773 // - If the network is still the default network, the presence or absence of this rule does not
774 // matter.
775 //
776 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
777 // implicitly bound to a network because at the time of connect() it was the default, and that
778 // network must no longer be the default, and must now require permissions.
779 if (permission == PERMISSION_NONE) {
780 return modifyImplicitNetworkRule(netId, table, add);
781 }
782 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700783}
784
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900785[[nodiscard]] static int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
Robin Leeb8087362016-03-30 18:43:08 +0100786 Fwmark fwmark;
787 Fwmark mask;
788 fwmark.protectedFromVpn = false;
789 mask.protectedFromVpn = true;
790
Luke Huang94658ac2018-10-18 19:35:12 +0900791 for (const UidRangeParcel& range : uidRanges.getRanges()) {
792 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN,
793 FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
794 IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) {
Robin Leeb8087362016-03-30 18:43:08 +0100795 return ret;
796 }
797 }
798
799 return 0;
800}
801
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900802int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
803 const UidRanges& uidRanges, bool secure, bool add,
804 bool modifyNonUidBasedRules) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700805 uint32_t table = getRouteTableForInterface(interface);
806 if (table == RT_TABLE_UNSPEC) {
807 return -ESRCH;
808 }
809
Luke Huang94658ac2018-10-18 19:35:12 +0900810 for (const UidRangeParcel& range : uidRanges.getRanges()) {
811 if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, secure, add)) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700812 return ret;
813 }
Luke Huang94658ac2018-10-18 19:35:12 +0900814 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start,
815 range.stop, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700816 return ret;
817 }
Luke Huang94658ac2018-10-18 19:35:12 +0900818 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start,
819 range.stop, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700820 return ret;
821 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700822 }
823
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700824 if (modifyNonUidBasedRules) {
825 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700826 return ret;
827 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700828 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
829 return ret;
830 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700831 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700832 return ret;
833 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700834 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700835 }
836
837 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700838}
839
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900840int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
841 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700842 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700843 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900844 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700845 }
846
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700847 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700848 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700849
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700850 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700851 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700852
853 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700854 mask.permission = permission;
855
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700856 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900857 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700858}
859
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900860int RouteController::modifyTetheredNetwork(uint16_t action, const char* inputInterface,
861 const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700862 uint32_t table = getRouteTableForInterface(outputInterface);
863 if (table == RT_TABLE_UNSPEC) {
864 return -ESRCH;
865 }
866
867 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
868 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
869}
870
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900871// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900872// Returns 0 on success or negative errno on failure.
Tyler Wearfa94a272019-12-05 15:01:48 -0800873int RouteController::modifyRoute(uint16_t action, uint16_t flags, const char* interface,
874 const char* destination, const char* nexthop, TableType tableType,
875 int mtu) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700876 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700877 switch (tableType) {
878 case RouteController::INTERFACE: {
879 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700880 if (table == RT_TABLE_UNSPEC) {
881 return -ESRCH;
882 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700883 break;
884 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700885 case RouteController::LOCAL_NETWORK: {
886 table = ROUTE_TABLE_LOCAL_NETWORK;
887 break;
888 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700889 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700890 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700891 break;
892 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700893 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700894 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700895 break;
896 }
897 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700898
Tyler Wearfa94a272019-12-05 15:01:48 -0800899 int ret = modifyIpRoute(action, flags, table, interface, destination, nexthop, mtu);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700900 // Trying to add a route that already exists shouldn't cause an error.
901 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900902 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700903 }
904
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900905 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700906}
907
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900908[[nodiscard]] static int clearTetheringRules(const char* inputInterface) {
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900909 int ret = 0;
910 while (ret == 0) {
911 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
912 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
913 }
914
915 if (ret == -ENOENT) {
916 return 0;
917 } else {
918 return ret;
919 }
920}
921
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900922uint32_t getRulePriority(const nlmsghdr *nlh) {
923 return getRtmU32Attribute(nlh, FRA_PRIORITY);
924}
925
926uint32_t getRouteTable(const nlmsghdr *nlh) {
927 return getRtmU32Attribute(nlh, RTA_TABLE);
928}
929
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900930[[nodiscard]] static int flushRules() {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900931 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
932 // Don't touch rules at priority 0 because by default they are used for local input.
933 return getRulePriority(nlh) != 0;
934 };
935 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
936}
937
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900938int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900939 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
940 return getRouteTable(nlh) == table;
941 };
942
943 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
944}
945
946// Returns 0 on success or negative errno on failure.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900947int RouteController::flushRoutes(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900948 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900949
950 uint32_t table = getRouteTableForInterfaceLocked(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900951 if (table == RT_TABLE_UNSPEC) {
952 return -ESRCH;
953 }
954
955 int ret = flushRoutes(table);
956
957 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
958 // track of its name.
959 if (ret == 0) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900960 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900961 }
962
963 return ret;
964}
965
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700966int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700967 if (int ret = flushRules()) {
968 return ret;
969 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700970 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700971 return ret;
972 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700973 if (int ret = addLocalNetworkRules(localNetId)) {
974 return ret;
975 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700976 if (int ret = addUnreachableRule()) {
977 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700978 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900979 // Don't complain if we can't add the dummy network, since not all devices support it.
980 configureDummyNetwork();
981
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700982 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700983 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700984}
985
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700986int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700987 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700988}
989
990int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700991 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700992}
993
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700994int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
995 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700996 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
997 return ret;
998 }
999 updateTableNamesFile();
1000 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001001}
1002
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001003int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
1004 Permission permission) {
1005 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001006 return ret;
1007 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001008 if (int ret = flushRoutes(interface)) {
1009 return ret;
1010 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001011 if (int ret = clearTetheringRules(interface)) {
1012 return ret;
1013 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001014 updateTableNamesFile();
1015 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001016}
1017
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001018int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001019 bool secure, const UidRanges& uidRanges) {
1020 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001021 MODIFY_NON_UID_BASED_RULES)) {
1022 return ret;
1023 }
1024 updateTableNamesFile();
1025 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001026}
1027
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001028int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001029 bool secure, const UidRanges& uidRanges) {
1030 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001031 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001032 return ret;
1033 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001034 if (int ret = flushRoutes(interface)) {
1035 return ret;
1036 }
1037 updateTableNamesFile();
1038 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001039}
1040
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001041int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1042 Permission oldPermission,
1043 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001044 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001045 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001046 return ret;
1047 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001048 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001049}
1050
Robin Leeb8087362016-03-30 18:43:08 +01001051int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1052 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1053}
1054
1055int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1056 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1057}
1058
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001059int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001060 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001061 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001062 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001063}
1064
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001065int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001066 bool secure, const UidRanges& uidRanges) {
1067 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001068 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001069}
1070
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001071int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1072 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001073}
1074
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001075int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1076 Permission permission) {
1077 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001078}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001079
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001080int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Tyler Wearfa94a272019-12-05 15:01:48 -08001081 TableType tableType, int mtu) {
1082 return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, interface, destination, nexthop,
1083 tableType, mtu);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001084}
1085
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001086int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001087 const char* nexthop, TableType tableType) {
Tyler Wearfa94a272019-12-05 15:01:48 -08001088 return modifyRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, interface, destination, nexthop,
1089 tableType, 0);
1090}
1091
1092int RouteController::updateRoute(const char* interface, const char* destination,
1093 const char* nexthop, TableType tableType, int mtu) {
1094 return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_REPLACE_FLAGS, interface, destination, nexthop,
1095 tableType, mtu);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001096}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001097
1098int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001099 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001100}
1101
1102int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001103 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001104}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001105
1106int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1107 Permission permission) {
1108 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1109}
1110
1111int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1112 const char* physicalInterface,
1113 Permission permission) {
1114 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1115}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001116
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001117// Protects sInterfaceToTable.
Luke Huang534980c2018-07-06 17:50:11 +08001118std::mutex RouteController::sInterfaceToTableLock;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001119std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1120
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001121} // namespace android::net