blob: c65fde229642ad5fff1921faa55f4355c2ff7b6b [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
Lorenzo Colitti7035f222017-02-13 18:29:00 +090030#include "DummyNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070031#include "Fwmark.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090032#include "NetdConstants.h"
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090033#include "NetlinkCommands.h"
Patrick Rohr70d42502021-10-12 18:24:10 +020034#include "TcUtils.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070035
Bernie Innocenti189eb502018-10-01 23:10:18 +090036#include <android-base/file.h>
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090037#include <android-base/stringprintf.h>
Hungming Chen7f725432020-02-07 17:47:23 +080038#include <android-base/strings.h>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070039#include "log/log.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090040#include "netid_client.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090041#include "netutils/ifc.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070042
Hungming Chen7f725432020-02-07 17:47:23 +080043using android::base::StartsWith;
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;
Chiachang Wangf79e3562022-01-15 13:31:04 +080051auto RouteController::ifNameToIndexFunction = if_nametoindex;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070052// BEGIN CONSTANTS --------------------------------------------------------------------------------
53
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070054const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070055const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
56const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
57
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070058const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070059const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
60const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
61
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070062const char* const ROUTE_TABLE_NAME_LOCAL = "local";
63const char* const ROUTE_TABLE_NAME_MAIN = "main";
64
Lorenzo Colittid78843e2017-03-27 05:52:31 +090065const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
66
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070067const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
68
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070069const uid_t UID_ROOT = 0;
Lorenzo Colitti3093f562017-09-25 14:17:38 +090070const uint32_t FWMARK_NONE = 0;
71const uint32_t MASK_NONE = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090072const char* const IIF_LOOPBACK = "lo";
Yi Kongbdfd57e2018-07-25 13:26:10 -070073const char* const IIF_NONE = nullptr;
74const char* const OIF_NONE = nullptr;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070075const bool ACTION_ADD = true;
76const bool ACTION_DEL = false;
77const bool MODIFY_NON_UID_BASED_RULES = true;
78
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -070079const 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 -070080
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070081// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
82// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
Bernie Innocenti762dcf42019-06-14 19:52:49 +090083static constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070084 return RTA_LENGTH(x);
85}
86
87// These are practically const, but can't be declared so, because they are used to initialize
88// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +090089rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
90rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
91rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
92rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
Lorenzo Colitti2b078672016-12-16 18:45:03 +090093rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070094
Lorenzo Colitti2b078672016-12-16 18:45:03 +090095rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
96rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +090097rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070098
Tyler Wearfa94a272019-12-05 15:01:48 -080099// One or more nested attributes in the RTA_METRICS attribute.
100rtattr RTATTRX_MTU = { U16_RTA_LENGTH(sizeof(uint32_t)), RTAX_MTU};
Lorenzo Colitti57d54682020-01-24 09:18:13 +0900101constexpr size_t RTATTRX_MTU_SIZE = RTA_SPACE(sizeof(uint32_t));
Tyler Wearfa94a272019-12-05 15:01:48 -0800102
103// The RTA_METRICS attribute itself.
Lorenzo Colitti57d54682020-01-24 09:18:13 +0900104constexpr size_t RTATTR_METRICS_SIZE = RTATTRX_MTU_SIZE;
Tyler Wearfa94a272019-12-05 15:01:48 -0800105rtattr RTATTR_METRICS = { U16_RTA_LENGTH(RTATTR_METRICS_SIZE), RTA_METRICS };
106
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700107uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
108
Ken Chena2206ec2021-03-24 17:15:44 +0800109constexpr bool EXPLICIT = true;
110constexpr bool IMPLICIT = false;
111
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700112// END CONSTANTS ----------------------------------------------------------------------------------
113
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900114static const char* actionName(uint16_t action) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900115 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
116 return ops[action % 4];
117}
118
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900119static const char* familyName(uint8_t family) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900120 switch (family) {
121 case AF_INET: return "IPv4";
122 case AF_INET6: return "IPv6";
123 default: return "???";
124 }
125}
126
Lorenzo Colitti3810c972021-01-06 11:44:02 +0900127static void maybeModifyQdiscClsact(const char* interface, bool add);
128
Chiachang Wangf79e3562022-01-15 13:31:04 +0800129static uint32_t getRouteTableIndexFromGlobalRouteTableIndex(uint32_t index, bool local) {
130 // The local table is
131 // "global table - ROUTE_TABLE_OFFSET_FROM_INDEX + ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL"
132 const uint32_t localTableOffset = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL -
133 RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
134 return local ? index + localTableOffset : index;
135}
136
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900137// Caller must hold sInterfaceToTableLock.
Chiachang Wangf79e3562022-01-15 13:31:04 +0800138uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface, bool local) {
mtk137999f2913e2019-02-25 19:39:36 +0800139 // If we already know the routing table for this interface name, use it.
140 // This ensures we can remove rules and routes for an interface that has been removed,
141 // or has been removed and re-added with a different interface index.
142 //
143 // The caller is responsible for ensuring that an interface is never added to a network
144 // until it has been removed from any network it was previously in. This ensures that
145 // if the same interface disconnects and then reconnects with a different interface ID
146 // when the reconnect happens the interface will not be in the map, and the code will
147 // determine the new routing table from the interface ID, below.
Chiachang Wangf79e3562022-01-15 13:31:04 +0800148 //
149 // sInterfaceToTable stores the *global* routing table for the interface, and the local table is
150 // "global table - ROUTE_TABLE_OFFSET_FROM_INDEX + ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL"
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900151 auto iter = sInterfaceToTable.find(interface);
mtk137999f2913e2019-02-25 19:39:36 +0800152 if (iter != sInterfaceToTable.end()) {
Chiachang Wangf79e3562022-01-15 13:31:04 +0800153 return getRouteTableIndexFromGlobalRouteTableIndex(iter->second, local);
mtk137999f2913e2019-02-25 19:39:36 +0800154 }
155
Chiachang Wangf79e3562022-01-15 13:31:04 +0800156 uint32_t index = RouteController::ifNameToIndexFunction(interface);
mtk137999f2913e2019-02-25 19:39:36 +0800157 if (index == 0) {
Lorenzo Colittib6dc40a2020-03-24 00:58:50 +0900158 ALOGE("cannot find interface %s: %s", interface, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700159 return RT_TABLE_UNSPEC;
160 }
mtk137999f2913e2019-02-25 19:39:36 +0800161 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
162 sInterfaceToTable[interface] = index;
Chiachang Wangf79e3562022-01-15 13:31:04 +0800163 return getRouteTableIndexFromGlobalRouteTableIndex(index, local);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700164}
165
Rubin Xu6c00b612018-04-27 14:27:59 +0100166uint32_t RouteController::getIfIndex(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900167 std::lock_guard lock(sInterfaceToTableLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100168
169 auto iter = sInterfaceToTable.find(interface);
170 if (iter == sInterfaceToTable.end()) {
171 ALOGE("getIfIndex: cannot find interface %s", interface);
172 return 0;
173 }
174
Lorenzo Colittib6dc40a2020-03-24 00:58:50 +0900175 // For interfaces that are not in the local network, the routing table is always the interface
176 // index plus ROUTE_TABLE_OFFSET_FROM_INDEX. But for interfaces in the local network, there's no
177 // way to know the interface index from this table. Return 0 here so callers of this method do
178 // not get confused.
179 // TODO: stop calling this method from any caller that only wants interfaces in client mode.
180 int ifindex = iter->second;
181 if (ifindex == ROUTE_TABLE_LOCAL_NETWORK) {
182 return 0;
183 }
184
185 return ifindex - ROUTE_TABLE_OFFSET_FROM_INDEX;
Rubin Xu6c00b612018-04-27 14:27:59 +0100186}
187
Chiachang Wangf79e3562022-01-15 13:31:04 +0800188uint32_t RouteController::getRouteTableForInterface(const char* interface, bool local) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900189 std::lock_guard lock(sInterfaceToTableLock);
Chiachang Wangf79e3562022-01-15 13:31:04 +0800190 return getRouteTableForInterfaceLocked(interface, local);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900191}
192
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700193void addTableName(uint32_t table, const std::string& name, std::string* contents) {
194 char tableString[UINT32_STRLEN];
195 snprintf(tableString, sizeof(tableString), "%u", table);
196 *contents += tableString;
197 *contents += " ";
198 *contents += name;
199 *contents += "\n";
200}
201
202// 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 +0900203void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700204 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700205
206 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
207 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
208
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700209 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700210 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
211 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700212
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900213 std::lock_guard lock(sInterfaceToTableLock);
Chiachang Wangf79e3562022-01-15 13:31:04 +0800214 for (const auto& [ifName, ifIndex] : sInterfaceToTable) {
215 addTableName(ifIndex, ifName, &contents);
216 // Add table for the local route of the network. It's expected to be used for excluding the
217 // local traffic in the VPN network.
218 // Start from ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL plus with the interface table index.
219 uint32_t offset = ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL - ROUTE_TABLE_OFFSET_FROM_INDEX;
220 addTableName(offset + ifIndex, ifName + INTERFACE_LOCAL_SUFFIX, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700221 }
222
Dan Albert5407e142015-03-16 10:05:59 -0700223 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800224 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700225 return;
226 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700227}
228
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700229// Returns 0 on success or negative errno on failure.
230int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
231 if (!input) {
232 *length = 0;
233 *padding = 0;
234 return 0;
235 }
236 *length = strlcpy(name, input, IFNAMSIZ) + 1;
237 if (*length > IFNAMSIZ) {
238 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
239 return -ENAMETOOLONG;
240 }
241 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
242 return 0;
243}
244
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700245// Adds or removes a routing rule for IPv4 and IPv6.
246//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900247// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100248// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700249// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
250// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700251// + If |iif| is non-NULL, the rule matches the specified incoming interface.
252// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
253// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
254// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900255//
256// Returns 0 on success or negative errno on failure.
Ken Chen53360bf2021-12-10 02:41:05 +0800257[[nodiscard]] static int modifyIpRule(uint16_t action, int32_t priority, uint8_t ruleType,
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900258 uint32_t table, uint32_t fwmark, uint32_t mask,
259 const char* iif, const char* oif, uid_t uidStart,
260 uid_t uidEnd) {
Ken Chen53360bf2021-12-10 02:41:05 +0800261 if (priority < 0) {
262 ALOGE("invalid IP-rule priority %d", priority);
263 return -ERANGE;
264 }
265
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700266 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
267 if (fwmark & ~mask) {
268 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
269 return -ERANGE;
270 }
271
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700272 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900273 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700274 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
275 size_t iifLength, oifLength;
276 uint16_t iifPadding, oifPadding;
277 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
278 return ret;
279 }
280 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
281 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900282 }
283
Lorenzo Colitti59656512014-06-25 03:20:29 +0900284 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700285 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700286 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900287 return -EUSERS;
288 }
Robin Lee4ef94642016-04-01 11:50:49 +0100289
Lorenzo Colitti72723682014-06-26 13:51:10 +0900290 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900291
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900292 // Assemble a rule request and put it in an array of iovec structures.
293 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100294 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900295 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
296 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900297 };
298
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900299 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
300 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
301 // table 0, it's a wildcard that matches anything.
302 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
303 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
304 return -ENOTUNIQ;
305 }
306
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700307 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
308 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900309 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900310
311 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700312 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700313 { &rule, sizeof(rule) },
314 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
315 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700316 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
317 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700318 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
319 { &fwmark, mask ? sizeof(fwmark) : 0 },
320 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
321 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900322 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
323 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700324 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
325 { iifName, iifLength },
326 { PADDING_BUFFER, iifPadding },
327 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
328 { oifName, oifLength },
329 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900330 };
331
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900332 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700333 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
334 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900335 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900336 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
337 // Don't log when deleting a tethering rule that's not there. This matches the
338 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
339 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
340 strerror(-ret));
341 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900342 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700343 }
344 }
345
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900346 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700347}
348
Ken Chen53360bf2021-12-10 02:41:05 +0800349[[nodiscard]] static int modifyIpRule(uint16_t action, int32_t priority, uint32_t table,
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900350 uint32_t fwmark, uint32_t mask, const char* iif,
351 const char* oif, uid_t uidStart, uid_t uidEnd) {
Robin Lee4ef94642016-04-01 11:50:49 +0100352 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
353 uidEnd);
354}
355
Ken Chen53360bf2021-12-10 02:41:05 +0800356[[nodiscard]] static int modifyIpRule(uint16_t action, int32_t priority, uint32_t table,
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900357 uint32_t fwmark, uint32_t mask) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700358 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
359 INVALID_UID);
360}
361
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900362// Adds or deletes an IPv4 or IPv6 route.
363// Returns 0 on success or negative errno on failure.
Tyler Wearfa94a272019-12-05 15:01:48 -0800364int modifyIpRoute(uint16_t action, uint16_t flags, uint32_t table, const char* interface,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200365 const char* destination, const char* nexthop, uint32_t mtu, uint32_t priority) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900366 // At least the destination must be non-null.
367 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700368 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900369 return -EFAULT;
370 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700371
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900372 // Parse the prefix.
373 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700374 uint8_t family;
375 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900376 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
377 &prefixLength);
378 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700379 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900380 return rawLength;
381 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700382
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900383 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700384 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900385 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
386 }
387
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700388 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900389 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900390 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700391
392 if (nexthop && !strcmp(nexthop, "unreachable")) {
393 type = RTN_UNREACHABLE;
394 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
395 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
396 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
397 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700398 nexthop = nullptr;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900399 } else if (nexthop && !strcmp(nexthop, "throw")) {
400 type = RTN_THROW;
401 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700402 nexthop = nullptr;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700403 } else {
404 // If an interface was specified, find the ifindex.
405 if (interface != OIF_NONE) {
Chiachang Wangf79e3562022-01-15 13:31:04 +0800406 ifindex = RouteController::ifNameToIndexFunction(interface);
407
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700408 if (!ifindex) {
409 ALOGE("cannot find interface %s", interface);
410 return -ENODEV;
411 }
412 }
413
414 // If a nexthop was specified, parse it as the same family as the prefix.
415 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
416 ALOGE("inet_pton failed for nexthop %s", nexthop);
417 return -EINVAL;
418 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900419 }
420
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900421 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700422 rtmsg route = {
Nick Desaulniers6b357502019-10-11 09:26:44 -0700423 .rtm_family = family,
424 .rtm_dst_len = prefixLength,
425 .rtm_protocol = RTPROT_STATIC,
426 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
427 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900428 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900429
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700430 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
431 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900432
433 iovec iov[] = {
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200434 {nullptr, 0},
435 {&route, sizeof(route)},
436 {&RTATTR_TABLE, sizeof(RTATTR_TABLE)},
437 {&table, sizeof(table)},
438 {&rtaDst, sizeof(rtaDst)},
439 {rawAddress, static_cast<size_t>(rawLength)},
440 {&RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0},
441 {&ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0},
442 {&rtaGateway, nexthop ? sizeof(rtaGateway) : 0},
443 {rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0},
444 {&RTATTR_METRICS, mtu != 0 ? sizeof(RTATTR_METRICS) : 0},
445 {&RTATTRX_MTU, mtu != 0 ? sizeof(RTATTRX_MTU) : 0},
446 {&mtu, mtu != 0 ? sizeof(mtu) : 0},
447 {&RTATTR_PRIO, priority != 0 ? sizeof(RTATTR_PRIO) : 0},
448 {&priority, priority != 0 ? sizeof(priority) : 0},
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900449 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900450
Jimmy Chence9e5782019-03-08 16:12:55 +0800451 // Allow creating multiple link-local routes in the same table, so we can make IPv6
452 // work on all interfaces in the local_network table.
453 if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) {
454 flags &= ~NLM_F_EXCL;
455 }
456
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900457 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900458 if (ret) {
459 ALOGE("Error %s route %s -> %s %s to table %u: %s",
460 actionName(action), destination, nexthop, interface, table, strerror(-ret));
461 }
462 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700463}
464
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700465// An iptables rule to mark incoming packets on a network with the netId of the network.
466//
467// This is so that the kernel can:
468// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
469// replies, SYN-ACKs, etc).
470// + Mark sockets that accept connections from this interface so that the connection stays on the
471// same interface.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900472int modifyIncomingPacketMark(unsigned netId, const char* interface, Permission permission,
473 bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700474 Fwmark fwmark;
475
476 fwmark.netId = netId;
477 fwmark.explicitlySelected = true;
478 fwmark.protectedFromVpn = true;
479 fwmark.permission = permission;
480
Benedict Wongb9baf262017-12-03 15:43:08 -0800481 const uint32_t mask = ~Fwmark::getUidBillingMask();
482
483 std::string cmd = StringPrintf(
484 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
485 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900486 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700487 ALOGE("failed to change iptables rule that sets incoming packet mark");
488 return -EREMOTEIO;
489 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700490
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700491 return 0;
492}
493
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700494// A rule to route responses to the local network forwarded via the VPN.
495//
496// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
497// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900498[[nodiscard]] static int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700499 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
500 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
501 INVALID_UID, INVALID_UID);
502}
503
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700504// A rule to route all traffic from a given set of UIDs to go over the VPN.
505//
506// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
507// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
508// bypass the VPN if the protectedFromVpn bit is set.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900509[[nodiscard]] static int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Chiachang Wang2e9443f2022-01-14 22:55:36 +0800510 int32_t subPriority, bool secure, bool add,
511 bool excludeLocalRoutes) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700512 Fwmark fwmark;
513 Fwmark mask;
514
515 fwmark.protectedFromVpn = false;
516 mask.protectedFromVpn = true;
517
Ken Chen53360bf2021-12-10 02:41:05 +0800518 int32_t priority;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700519
520 if (secure) {
521 priority = RULE_PRIORITY_SECURE_VPN;
522 } else {
Chiachang Wang2e9443f2022-01-14 22:55:36 +0800523 priority = excludeLocalRoutes ? RULE_PRIORITY_BYPASSABLE_VPN_LOCAL_EXCLUSION
524 : RULE_PRIORITY_BYPASSABLE_VPN_NO_LOCAL_EXCLUSION;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700525
526 fwmark.explicitlySelected = false;
527 mask.explicitlySelected = true;
528 }
529
Ken Chen4ea88462021-05-23 14:56:43 +0800530 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority + subPriority, table,
531 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700532}
533
534// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
535// set of UIDs.
536//
537// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
538// target set, but where the DnsProxyListener itself is not.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900539[[nodiscard]] static int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
Chiachang Wang2e9443f2022-01-14 22:55:36 +0800540 bool add, bool excludeLocalRoutes) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700541 Fwmark fwmark;
542 Fwmark mask;
543
544 fwmark.netId = netId;
545 mask.netId = FWMARK_NET_ID_MASK;
546
547 fwmark.permission = PERMISSION_SYSTEM;
548 mask.permission = PERMISSION_SYSTEM;
549
Chiachang Wang2e9443f2022-01-14 22:55:36 +0800550 uint32_t priority;
551
552 if (secure) {
553 priority = RULE_PRIORITY_SECURE_VPN;
554 } else {
555 priority = excludeLocalRoutes ? RULE_PRIORITY_BYPASSABLE_VPN_LOCAL_EXCLUSION
556 : RULE_PRIORITY_BYPASSABLE_VPN_NO_LOCAL_EXCLUSION;
557 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700558
559 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
560 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700561}
562
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700563// A rule to route traffic based on an explicitly chosen network.
564//
565// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
566//
567// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
568// to check it again in the rules here, because a network's permissions may have been updated via
569// modifyNetworkPermission().
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900570[[nodiscard]] static int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
571 Permission permission, uid_t uidStart,
Ken Chen53360bf2021-12-10 02:41:05 +0800572 uid_t uidEnd, int32_t subPriority, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700573 Fwmark fwmark;
574 Fwmark mask;
575
576 fwmark.netId = netId;
577 mask.netId = FWMARK_NET_ID_MASK;
578
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700579 fwmark.explicitlySelected = true;
580 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700581
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700582 fwmark.permission = permission;
583 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700584
Ken Chen4ea88462021-05-23 14:56:43 +0800585 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
586 RULE_PRIORITY_EXPLICIT_NETWORK + subPriority, table, fwmark.intValue,
587 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700588}
589
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700590// A rule to route traffic based on a chosen outgoing interface.
591//
592// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
593// the outgoing interface (typically for link-local communications).
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900594[[nodiscard]] static int modifyOutputInterfaceRules(const char* interface, uint32_t table,
595 Permission permission, uid_t uidStart,
Ken Chen53360bf2021-12-10 02:41:05 +0800596 uid_t uidEnd, int32_t subPriority, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700597 Fwmark fwmark;
598 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700599
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700600 fwmark.permission = permission;
601 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700602
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900603 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900604 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
605 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900606 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
607 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900608 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900609 UID_ROOT, UID_ROOT)) {
610 return ret;
611 }
612 }
613
Ken Chen4ea88462021-05-23 14:56:43 +0800614 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
615 RULE_PRIORITY_OUTPUT_INTERFACE + subPriority, table, fwmark.intValue,
616 mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700617}
618
619// A rule to route traffic based on the chosen network.
620//
621// This is for sockets that have not explicitly requested a particular network, but have been
622// bound to one when they called connect(). This ensures that sockets connected on a particular
623// network stay on that network even if the default network changes.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900624[[nodiscard]] static int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700625 Fwmark fwmark;
626 Fwmark mask;
627
628 fwmark.netId = netId;
629 mask.netId = FWMARK_NET_ID_MASK;
630
631 fwmark.explicitlySelected = false;
632 mask.explicitlySelected = true;
633
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900634 fwmark.permission = PERMISSION_NONE;
635 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700636
637 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900638 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
639 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700640}
641
Chiachang Wang8b9cdd22022-01-27 10:43:28 +0800642int RouteController::modifyVpnLocalExclusionRule(bool add, const char* physicalInterface) {
Chiachang Wange7bf5f52022-01-17 11:37:39 +0800643 uint32_t table = getRouteTableForInterface(physicalInterface, true /* local */);
644 if (table == RT_TABLE_UNSPEC) {
645 return -ESRCH;
646 }
647
648 Fwmark fwmark;
649 Fwmark mask;
650
651 fwmark.explicitlySelected = false;
652 mask.explicitlySelected = true;
653
654 fwmark.permission = PERMISSION_NONE;
655 mask.permission = PERMISSION_NONE;
656
Chiachang Wang8b9cdd22022-01-27 10:43:28 +0800657 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_LOCAL_ROUTES, table,
658 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
659 INVALID_UID)) {
660 return ret;
661 }
662 return modifyVpnLocalExclusionRoutes(add, physicalInterface);
663}
664
665// TODO: Update the local exclusion routes based on what actual subnet the network is.
666int RouteController::modifyVpnLocalExclusionRoutes(bool add, const char* interface) {
667 for (size_t i = 0; i < ARRAY_SIZE(LOCAL_EXCLUSION_ROUTES_V4); ++i) {
668 if (int ret = modifyVpnLocalExclusionRoute(add, interface, LOCAL_EXCLUSION_ROUTES_V4[i])) {
669 // Routes are updated regardless of subnet. The destinations may be unreachable and
670 // cause EACCES error. This may happen now so ignore to not to cause an error.
671 if (ret != -EACCES) {
672 return ret;
673 }
674 }
675 }
676
677 for (size_t i = 0; i < ARRAY_SIZE(LOCAL_EXCLUSION_ROUTES_V6); ++i) {
678 if (int ret = modifyVpnLocalExclusionRoute(add, interface, LOCAL_EXCLUSION_ROUTES_V6[i])) {
679 // Routes are updated regardless of subnet. The destinations may be unreachable and
680 // cause EACCES error. This may happen now so ignore to not to cause an error.
681 if (ret != -EACCES) {
682 return ret;
683 }
684 }
685 }
686 return 0;
687}
688
689int RouteController::modifyVpnLocalExclusionRoute(bool add, const char* interface,
690 const char* destination) {
691 uint32_t table = getRouteTableForInterface(interface, true /* local */);
692 if (table == RT_TABLE_UNSPEC) {
693 return -ESRCH;
694 }
695
696 if (int ret = modifyIpRoute(add ? RTM_NEWROUTE : RTM_DELROUTE,
697 add ? NETLINK_ROUTE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS, table,
698 interface, destination, nullptr, 0 /* mtu */, 0 /* priority */)) {
699 // Trying to delete a route that already deleted shouldn't cause an error.
700 if (add || ret != -ESRCH) {
701 return ret;
702 }
703 }
704
705 return 0;
Chiachang Wange7bf5f52022-01-17 11:37:39 +0800706}
707
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700708// A rule to enable split tunnel VPNs.
709//
710// 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 +0800711// go over the default network, provided it has the permissions required by the default network.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900712int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
713 const char* physicalInterface,
714 Permission permission) {
Chiachang Wangf79e3562022-01-15 13:31:04 +0800715 uint32_t table = getRouteTableForInterface(physicalInterface, false /* local */);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700716 if (table == RT_TABLE_UNSPEC) {
717 return -ESRCH;
718 }
719
720 Fwmark fwmark;
721 Fwmark mask;
722
723 fwmark.netId = vpnNetId;
724 mask.netId = FWMARK_NET_ID_MASK;
725
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700726 fwmark.permission = permission;
727 mask.permission = permission;
728
729 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
730 mask.intValue);
731}
732
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700733// Add rules to allow legacy routes added through the requestRouteToHost() API.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900734[[nodiscard]] static int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700735 Fwmark fwmark;
736 Fwmark mask;
737
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700738 fwmark.explicitlySelected = false;
739 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700740
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700741 // Rules to allow legacy routes to override the default network.
742 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
743 fwmark.intValue, mask.intValue)) {
744 return ret;
745 }
746 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
747 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
748 return ret;
749 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700750
751 fwmark.permission = PERMISSION_SYSTEM;
752 mask.permission = PERMISSION_SYSTEM;
753
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700754 // A rule to allow legacy routes from system apps to override VPNs.
755 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700756 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700757}
758
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700759// Add rules to lookup the local network when specified explicitly or otherwise.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900760[[nodiscard]] static int addLocalNetworkRules(unsigned localNetId) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700761 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
Ken Chen4ea88462021-05-23 14:56:43 +0800762 INVALID_UID, INVALID_UID,
763 UidRanges::DEFAULT_SUB_PRIORITY, ACTION_ADD)) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700764 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700765 }
766
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700767 Fwmark fwmark;
768 Fwmark mask;
769
770 fwmark.explicitlySelected = false;
771 mask.explicitlySelected = true;
772
773 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
774 fwmark.intValue, mask.intValue);
775}
776
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900777/* static */
778int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900779 const char *interface = DummyNetwork::INTERFACE_NAME;
Chiachang Wangf79e3562022-01-15 13:31:04 +0800780 uint32_t table = getRouteTableForInterface(interface, false /* local */);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900781 if (table == RT_TABLE_UNSPEC) {
Hungming Chen7f725432020-02-07 17:47:23 +0800782 // getRouteTableForInterface has already logged an error.
Lorenzo Colitti36679362015-02-25 10:26:19 +0900783 return -ESRCH;
784 }
785
786 ifc_init();
787 int ret = ifc_up(interface);
788 ifc_close();
789 if (ret) {
790 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
791 return -errno;
792 }
793
Ken Chen4ea88462021-05-23 14:56:43 +0800794 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, INVALID_UID,
795 INVALID_UID, UidRanges::DEFAULT_SUB_PRIORITY,
796 ACTION_ADD))) {
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900797 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900798 return ret;
799 }
800
Tyler Wearfa94a272019-12-05 15:01:48 -0800801 if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200802 "0.0.0.0/0", nullptr, 0 /* mtu */, 0 /* priority */))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900803 return ret;
804 }
805
Tyler Wearfa94a272019-12-05 15:01:48 -0800806 if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface, "::/0",
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200807 nullptr, 0 /* mtu */, 0 /* priority */))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900808 return ret;
809 }
810
811 return 0;
812}
813
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900814// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
815// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
816// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
817// rule will hopefully make things even clearer.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900818[[nodiscard]] static int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100819 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
820 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700821}
822
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900823[[nodiscard]] static int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700824 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
825 return ret;
826 }
Lorenzo Colitti3810c972021-01-06 11:44:02 +0900827 maybeModifyQdiscClsact(interface, add);
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900828 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
Ken Chen4ea88462021-05-23 14:56:43 +0800829 INVALID_UID, INVALID_UID, UidRanges::DEFAULT_SUB_PRIORITY,
830 add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700831}
832
Ken Chena2206ec2021-03-24 17:15:44 +0800833[[nodiscard]] static int modifyUidNetworkRule(unsigned netId, uint32_t table, uid_t uidStart,
Ken Chen53360bf2021-12-10 02:41:05 +0800834 uid_t uidEnd, int32_t subPriority, bool add,
Ken Chen4ea88462021-05-23 14:56:43 +0800835 bool explicitSelect) {
Ken Chen8738e1c2020-11-24 11:38:54 +0800836 if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
Ken Chena2206ec2021-03-24 17:15:44 +0800837 ALOGE("modifyUidNetworkRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
Ken Chen8738e1c2020-11-24 11:38:54 +0800838 return -EUSERS;
839 }
840
841 Fwmark fwmark;
842 Fwmark mask;
843
844 fwmark.netId = netId;
845 mask.netId = FWMARK_NET_ID_MASK;
846
Ken Chena2206ec2021-03-24 17:15:44 +0800847 fwmark.explicitlySelected = explicitSelect;
Ken Chen8738e1c2020-11-24 11:38:54 +0800848 mask.explicitlySelected = true;
849
850 // Access to this network is controlled by UID rules, not permission bits.
851 fwmark.permission = PERMISSION_NONE;
852 mask.permission = PERMISSION_NONE;
853
Ken Chena2206ec2021-03-24 17:15:44 +0800854 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
Ken Chen4ea88462021-05-23 14:56:43 +0800855 explicitSelect ? (RULE_PRIORITY_UID_EXPLICIT_NETWORK + subPriority)
856 : (RULE_PRIORITY_UID_IMPLICIT_NETWORK + subPriority),
Ken Chena2206ec2021-03-24 17:15:44 +0800857 table, fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart,
858 uidEnd);
Ken Chen8738e1c2020-11-24 11:38:54 +0800859}
860
861[[nodiscard]] static int modifyUidDefaultNetworkRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Ken Chen53360bf2021-12-10 02:41:05 +0800862 int32_t subPriority, bool add) {
Ken Chen8738e1c2020-11-24 11:38:54 +0800863 if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
864 ALOGE("modifyUidDefaultNetworkRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
865 return -EUSERS;
866 }
867
868 Fwmark fwmark;
869 Fwmark mask;
870
871 fwmark.netId = NETID_UNSET;
872 mask.netId = FWMARK_NET_ID_MASK;
873
874 // Access to this network is controlled by UID rules, not permission bits.
875 fwmark.permission = PERMISSION_NONE;
876 mask.permission = PERMISSION_NONE;
877
Ken Chen4ea88462021-05-23 14:56:43 +0800878 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
879 RULE_PRIORITY_UID_DEFAULT_NETWORK + subPriority, table, fwmark.intValue,
880 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Ken Chen8738e1c2020-11-24 11:38:54 +0800881}
882
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900883/* static */
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900884int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
Ken Chen4ea88462021-05-23 14:56:43 +0800885 const UidRangeMap& uidRangeMap, Permission permission,
Ken Chen8738e1c2020-11-24 11:38:54 +0800886 bool add, bool modifyNonUidBasedRules) {
Chiachang Wangf79e3562022-01-15 13:31:04 +0800887 uint32_t table = getRouteTableForInterface(interface, false /* local */);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700888 if (table == RT_TABLE_UNSPEC) {
889 return -ESRCH;
890 }
891
Ken Chen4ea88462021-05-23 14:56:43 +0800892 for (const auto& [subPriority, uidRanges] : uidRangeMap) {
893 for (const UidRangeParcel& range : uidRanges.getRanges()) {
894 if (int ret = modifyUidNetworkRule(netId, table, range.start, range.stop, subPriority,
895 add, EXPLICIT)) {
896 return ret;
897 }
898 if (int ret = modifyUidNetworkRule(netId, table, range.start, range.stop, subPriority,
899 add, IMPLICIT)) {
900 return ret;
901 }
902 if (int ret = modifyUidDefaultNetworkRule(table, range.start, range.stop, subPriority,
903 add)) {
904 return ret;
905 }
Ken Chen8738e1c2020-11-24 11:38:54 +0800906 }
907 }
908
909 if (!modifyNonUidBasedRules) {
910 // we are done.
911 return 0;
912 }
913
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700914 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
915 return ret;
916 }
917 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
Ken Chen4ea88462021-05-23 14:56:43 +0800918 UidRanges::DEFAULT_SUB_PRIORITY, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700919 return ret;
920 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900921 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Ken Chen4ea88462021-05-23 14:56:43 +0800922 UidRanges::DEFAULT_SUB_PRIORITY, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700923 return ret;
924 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900925
926 // Only set implicit rules for networks that don't require permissions.
927 //
928 // This is so that if the default network ceases to be the default network and then switches
929 // from requiring no permissions to requiring permissions, we ensure that apps only use the
930 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
931 // - it closes all sockets on the network except sockets that are explicitly selected.
932 //
933 // The lack of this rule only affects the special case above, because:
934 // - The only cases where we implicitly bind a socket to a network are the default network and
935 // the bypassable VPN that applies to the app, if any.
936 // - This rule doesn't affect VPNs because they don't support permissions at all.
937 // - The default network doesn't require permissions. While we support doing this, the framework
938 // never does it (partly because we'd end up in the situation where we tell apps that there is
939 // a default network, but they can't use it).
940 // - If the network is still the default network, the presence or absence of this rule does not
941 // matter.
942 //
943 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
944 // implicitly bound to a network because at the time of connect() it was the default, and that
945 // network must no longer be the default, and must now require permissions.
946 if (permission == PERMISSION_NONE) {
947 return modifyImplicitNetworkRule(netId, table, add);
948 }
949 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700950}
951
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800952[[nodiscard]] static int modifyUidUnreachableRule(unsigned netId, uid_t uidStart, uid_t uidEnd,
Ken Chen53360bf2021-12-10 02:41:05 +0800953 int32_t subPriority, bool add,
Ken Chen4ea88462021-05-23 14:56:43 +0800954 bool explicitSelect) {
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800955 if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
956 ALOGE("modifyUidUnreachableRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
957 return -EUSERS;
958 }
959
960 Fwmark fwmark;
961 Fwmark mask;
962
963 fwmark.netId = netId;
964 mask.netId = FWMARK_NET_ID_MASK;
965
966 fwmark.explicitlySelected = explicitSelect;
967 mask.explicitlySelected = true;
968
969 // Access to this network is controlled by UID rules, not permission bits.
970 fwmark.permission = PERMISSION_NONE;
971 mask.permission = PERMISSION_NONE;
972
973 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
Ken Chen4ea88462021-05-23 14:56:43 +0800974 explicitSelect ? (RULE_PRIORITY_UID_EXPLICIT_NETWORK + subPriority)
975 : (RULE_PRIORITY_UID_IMPLICIT_NETWORK + subPriority),
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800976 FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
977 IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
978}
979
Ken Chen4ea88462021-05-23 14:56:43 +0800980[[nodiscard]] static int modifyUidDefaultUnreachableRule(uid_t uidStart, uid_t uidEnd,
Ken Chen53360bf2021-12-10 02:41:05 +0800981 int32_t subPriority, bool add) {
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800982 if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
Ken Chen4ea88462021-05-23 14:56:43 +0800983 ALOGE("modifyUidDefaultUnreachableRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800984 return -EUSERS;
985 }
986
987 Fwmark fwmark;
988 Fwmark mask;
989
990 fwmark.netId = NETID_UNSET;
991 mask.netId = FWMARK_NET_ID_MASK;
992
993 // Access to this network is controlled by UID rules, not permission bits.
994 fwmark.permission = PERMISSION_NONE;
995 mask.permission = PERMISSION_NONE;
996
Ken Chen4ea88462021-05-23 14:56:43 +0800997 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
998 RULE_PRIORITY_UID_DEFAULT_UNREACHABLE + subPriority, FR_ACT_UNREACHABLE,
999 RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
1000 uidStart, uidEnd);
Ken Chen4e8ef9b2021-03-17 01:57:19 +08001001}
1002
Ken Chen4ea88462021-05-23 14:56:43 +08001003int RouteController::modifyUnreachableNetwork(unsigned netId, const UidRangeMap& uidRangeMap,
Ken Chen4e8ef9b2021-03-17 01:57:19 +08001004 bool add) {
Ken Chen4ea88462021-05-23 14:56:43 +08001005 for (const auto& [subPriority, uidRanges] : uidRangeMap) {
1006 for (const UidRangeParcel& range : uidRanges.getRanges()) {
1007 if (int ret = modifyUidUnreachableRule(netId, range.start, range.stop, subPriority, add,
1008 EXPLICIT)) {
1009 return ret;
1010 }
1011 if (int ret = modifyUidUnreachableRule(netId, range.start, range.stop, subPriority, add,
1012 IMPLICIT)) {
1013 return ret;
1014 }
1015 if (int ret = modifyUidDefaultUnreachableRule(range.start, range.stop, subPriority,
1016 add)) {
1017 return ret;
1018 }
Ken Chen4e8ef9b2021-03-17 01:57:19 +08001019 }
1020 }
1021
1022 return 0;
1023}
1024
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001025[[nodiscard]] static int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
Robin Leeb8087362016-03-30 18:43:08 +01001026 Fwmark fwmark;
1027 Fwmark mask;
1028 fwmark.protectedFromVpn = false;
1029 mask.protectedFromVpn = true;
1030
Luke Huang94658ac2018-10-18 19:35:12 +09001031 for (const UidRangeParcel& range : uidRanges.getRanges()) {
1032 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN,
1033 FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
1034 IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) {
Robin Leeb8087362016-03-30 18:43:08 +01001035 return ret;
1036 }
1037 }
1038
1039 return 0;
1040}
1041
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001042int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
Ken Chen4ea88462021-05-23 14:56:43 +08001043 const UidRangeMap& uidRangeMap, bool secure, bool add,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001044 bool modifyNonUidBasedRules, bool excludeLocalRoutes) {
Chiachang Wangf79e3562022-01-15 13:31:04 +08001045 uint32_t table = getRouteTableForInterface(interface, false /* false */);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001046 if (table == RT_TABLE_UNSPEC) {
1047 return -ESRCH;
1048 }
1049
Ken Chen4ea88462021-05-23 14:56:43 +08001050 for (const auto& [subPriority, uidRanges] : uidRangeMap) {
1051 for (const UidRangeParcel& range : uidRanges.getRanges()) {
1052 if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, subPriority, secure,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001053 add, excludeLocalRoutes)) {
Ken Chen4ea88462021-05-23 14:56:43 +08001054 return ret;
1055 }
1056 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start,
1057 range.stop, subPriority, add)) {
1058 return ret;
1059 }
1060 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start,
1061 range.stop, subPriority, add)) {
1062 return ret;
1063 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001064 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001065 }
1066
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001067 if (modifyNonUidBasedRules) {
1068 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001069 return ret;
1070 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -07001071 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
1072 return ret;
1073 }
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001074 if (int ret =
1075 modifyVpnSystemPermissionRule(netId, table, secure, add, excludeLocalRoutes)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001076 return ret;
1077 }
Ken Chen4ea88462021-05-23 14:56:43 +08001078 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT,
1079 UidRanges::DEFAULT_SUB_PRIORITY, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001080 }
1081
1082 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001083}
1084
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001085int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
1086 Permission permission) {
Chiachang Wangf79e3562022-01-15 13:31:04 +08001087 uint32_t table = getRouteTableForInterface(interface, false /* local */);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001088 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +09001089 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001090 }
1091
Sreeram Ramachandran122f5812014-05-11 20:29:49 -07001092 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -07001093 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001094
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001095 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -07001096 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001097
1098 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -07001099 mask.permission = permission;
1100
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001101 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colitti758627c2018-03-15 01:49:20 +09001102 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001103}
1104
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001105int RouteController::modifyTetheredNetwork(uint16_t action, const char* inputInterface,
1106 const char* outputInterface) {
Chiachang Wangf79e3562022-01-15 13:31:04 +08001107 uint32_t table = getRouteTableForInterface(outputInterface, false /* local */);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001108 if (table == RT_TABLE_UNSPEC) {
1109 return -ESRCH;
1110 }
1111
1112 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
1113 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
1114}
1115
Lorenzo Colitti92e8f962017-09-26 19:13:50 +09001116// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001117// Returns 0 on success or negative errno on failure.
Tyler Wearfa94a272019-12-05 15:01:48 -08001118int RouteController::modifyRoute(uint16_t action, uint16_t flags, const char* interface,
1119 const char* destination, const char* nexthop, TableType tableType,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001120 int mtu, int priority) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001121 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -07001122 switch (tableType) {
1123 case RouteController::INTERFACE: {
Chiachang Wangf79e3562022-01-15 13:31:04 +08001124 table = getRouteTableForInterface(interface, false /* local */);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001125 if (table == RT_TABLE_UNSPEC) {
1126 return -ESRCH;
1127 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -07001128 break;
1129 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001130 case RouteController::LOCAL_NETWORK: {
1131 table = ROUTE_TABLE_LOCAL_NETWORK;
1132 break;
1133 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001134 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001135 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -07001136 break;
1137 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001138 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001139 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -07001140 break;
1141 }
1142 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001143
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001144 int ret = modifyIpRoute(action, flags, table, interface, destination, nexthop, mtu, priority);
Sreeram Ramachandran03213152014-10-30 10:01:07 -07001145 // Trying to add a route that already exists shouldn't cause an error.
1146 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001147 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -07001148 }
1149
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001150 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001151}
1152
Lorenzo Colitti3810c972021-01-06 11:44:02 +09001153static void maybeModifyQdiscClsact(const char* interface, bool add) {
Hungming Chen7f725432020-02-07 17:47:23 +08001154 // The clsact attaching of v4- tun interface is triggered by ClatdController::maybeStartBpf
1155 // because the clat is started before the v4- interface is added to the network and the
1156 // clat startup needs to add {in, e}gress filters.
1157 // TODO: remove this workaround once v4- tun interface clsact attaching is moved out from
1158 // ClatdController::maybeStartBpf.
1159 if (StartsWith(interface, "v4-") && add) return;
1160
1161 // The interface may have already gone away in the delete case.
Chiachang Wangf79e3562022-01-15 13:31:04 +08001162 uint32_t ifindex = RouteController::ifNameToIndexFunction(interface);
Hungming Chen7f725432020-02-07 17:47:23 +08001163 if (!ifindex) {
1164 ALOGE("cannot find interface %s", interface);
1165 return;
1166 }
1167
1168 if (add) {
1169 if (int ret = tcQdiscAddDevClsact(ifindex)) {
1170 ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", ifindex, interface, strerror(-ret));
1171 return;
1172 }
1173 } else {
1174 if (int ret = tcQdiscDelDevClsact(ifindex)) {
1175 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", ifindex, interface, strerror(-ret));
1176 return;
1177 }
1178 }
1179
1180 return;
1181}
1182
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001183[[nodiscard]] static int clearTetheringRules(const char* inputInterface) {
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001184 int ret = 0;
1185 while (ret == 0) {
1186 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
1187 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
1188 }
1189
1190 if (ret == -ENOENT) {
1191 return 0;
1192 } else {
1193 return ret;
1194 }
1195}
1196
Lorenzo Colittif3e299a2017-02-14 17:24:28 +09001197uint32_t getRulePriority(const nlmsghdr *nlh) {
1198 return getRtmU32Attribute(nlh, FRA_PRIORITY);
1199}
1200
1201uint32_t getRouteTable(const nlmsghdr *nlh) {
1202 return getRtmU32Attribute(nlh, RTA_TABLE);
1203}
1204
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001205[[nodiscard]] static int flushRules() {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +09001206 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
1207 // Don't touch rules at priority 0 because by default they are used for local input.
1208 return getRulePriority(nlh) != 0;
1209 };
1210 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
1211}
1212
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001213int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +09001214 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
1215 return getRouteTable(nlh) == table;
1216 };
1217
1218 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
1219}
1220
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001221int RouteController::flushRoutes(const char* interface) {
Chiachang Wangf79e3562022-01-15 13:31:04 +08001222 // Try to flush both local and global routing tables.
1223 //
1224 // Flush local first because flush global routing tables may erase the sInterfaceToTable map.
1225 // Then the fake <iface>_local interface will be unable to find the index because the local
1226 // interface depends physical interface to find the correct index.
1227 int ret = flushRoutes(interface, true);
1228 ret |= flushRoutes(interface, false);
1229 return ret;
1230}
1231
1232// Returns 0 on success or negative errno on failure.
1233int RouteController::flushRoutes(const char* interface, bool local) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +09001234 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001235
Chiachang Wangf79e3562022-01-15 13:31:04 +08001236 uint32_t table = getRouteTableForInterfaceLocked(interface, local);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +09001237 if (table == RT_TABLE_UNSPEC) {
1238 return -ESRCH;
1239 }
1240
1241 int ret = flushRoutes(table);
1242
1243 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
1244 // track of its name.
Chiachang Wangf79e3562022-01-15 13:31:04 +08001245 // Skip erasing local fake interface since it does not exist in sInterfaceToTable.
1246 if (ret == 0 && !local) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001247 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +09001248 }
1249
1250 return ret;
1251}
1252
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001253int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -07001254 if (int ret = flushRules()) {
1255 return ret;
1256 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001257 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001258 return ret;
1259 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001260 if (int ret = addLocalNetworkRules(localNetId)) {
1261 return ret;
1262 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -07001263 if (int ret = addUnreachableRule()) {
1264 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001265 }
Lorenzo Colitti36679362015-02-25 10:26:19 +09001266 // Don't complain if we can't add the dummy network, since not all devices support it.
1267 configureDummyNetwork();
1268
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001269 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001270 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -07001271}
1272
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001273int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Lorenzo Colittib6dc40a2020-03-24 00:58:50 +09001274 if (int ret = modifyLocalNetwork(netId, interface, ACTION_ADD)) {
1275 return ret;
1276 }
1277 std::lock_guard lock(sInterfaceToTableLock);
1278 sInterfaceToTable[interface] = ROUTE_TABLE_LOCAL_NETWORK;
1279 return 0;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001280}
1281
1282int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Lorenzo Colittib6dc40a2020-03-24 00:58:50 +09001283 if (int ret = modifyLocalNetwork(netId, interface, ACTION_DEL)) {
1284 return ret;
1285 }
1286 std::lock_guard lock(sInterfaceToTableLock);
1287 sInterfaceToTable.erase(interface);
1288 return 0;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001289}
1290
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001291int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
Ken Chen8738e1c2020-11-24 11:38:54 +08001292 Permission permission,
Ken Chen4ea88462021-05-23 14:56:43 +08001293 const UidRangeMap& uidRangeMap) {
1294 if (int ret = modifyPhysicalNetwork(netId, interface, uidRangeMap, permission, ACTION_ADD,
Ken Chen8738e1c2020-11-24 11:38:54 +08001295 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001296 return ret;
1297 }
Chiachang Wange7bf5f52022-01-17 11:37:39 +08001298 // TODO: Consider to remove regular table if adding local table failed.
Chiachang Wang8b9cdd22022-01-27 10:43:28 +08001299 if (int ret = modifyVpnLocalExclusionRule(true, interface)) {
Chiachang Wange7bf5f52022-01-17 11:37:39 +08001300 return ret;
1301 }
1302
Hungming Chen7f725432020-02-07 17:47:23 +08001303 maybeModifyQdiscClsact(interface, ACTION_ADD);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001304 updateTableNamesFile();
1305 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001306}
1307
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001308int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
Ken Chen8738e1c2020-11-24 11:38:54 +08001309 Permission permission,
Ken Chen4ea88462021-05-23 14:56:43 +08001310 const UidRangeMap& uidRangeMap) {
1311 if (int ret = modifyPhysicalNetwork(netId, interface, uidRangeMap, permission, ACTION_DEL,
Ken Chen8738e1c2020-11-24 11:38:54 +08001312 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001313 return ret;
1314 }
Chiachang Wange7bf5f52022-01-17 11:37:39 +08001315
Chiachang Wang8b9cdd22022-01-27 10:43:28 +08001316 int ret = modifyVpnLocalExclusionRule(false, interface);
1317 // Always perform flushRoute even if removing local exclusion rules failed.
1318 ret |= flushRoutes(interface);
1319 if (ret) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001320 return ret;
1321 }
Chiachang Wange7bf5f52022-01-17 11:37:39 +08001322
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001323 if (int ret = clearTetheringRules(interface)) {
1324 return ret;
1325 }
Chiachang Wange7bf5f52022-01-17 11:37:39 +08001326
Hungming Chen7f725432020-02-07 17:47:23 +08001327 maybeModifyQdiscClsact(interface, ACTION_DEL);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001328 updateTableNamesFile();
1329 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001330}
1331
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001332int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001333 bool secure, const UidRangeMap& uidRangeMap,
1334 bool excludeLocalRoutes) {
Ken Chen4ea88462021-05-23 14:56:43 +08001335 if (int ret = modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_ADD,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001336 MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes)) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001337 return ret;
1338 }
1339 updateTableNamesFile();
1340 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001341}
1342
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001343int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001344 bool secure, const UidRangeMap& uidRangeMap,
1345 bool excludeLocalRoutes) {
Ken Chen4ea88462021-05-23 14:56:43 +08001346 if (int ret = modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_DEL,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001347 MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001348 return ret;
1349 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001350 if (int ret = flushRoutes(interface)) {
1351 return ret;
1352 }
1353 updateTableNamesFile();
1354 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001355}
1356
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001357int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1358 Permission oldPermission,
1359 Permission newPermission) {
Ken Chen4ea88462021-05-23 14:56:43 +08001360 // Physical network rules either use permission bits or UIDs, but not both.
1361 // So permission changes don't affect any UID-based rules.
1362 UidRangeMap emptyUidRangeMap;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001363 // Add the new rules before deleting the old ones, to avoid race conditions.
Ken Chen4ea88462021-05-23 14:56:43 +08001364 if (int ret = modifyPhysicalNetwork(netId, interface, emptyUidRangeMap, newPermission,
1365 ACTION_ADD, MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001366 return ret;
1367 }
Ken Chen4ea88462021-05-23 14:56:43 +08001368 return modifyPhysicalNetwork(netId, interface, emptyUidRangeMap, oldPermission, ACTION_DEL,
Ken Chen8738e1c2020-11-24 11:38:54 +08001369 MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001370}
1371
Robin Leeb8087362016-03-30 18:43:08 +01001372int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1373 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1374}
1375
1376int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1377 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1378}
1379
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001380int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001381 const UidRangeMap& uidRangeMap,
1382 bool excludeLocalRoutes) {
Ken Chen4ea88462021-05-23 14:56:43 +08001383 return modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_ADD,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001384 !MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001385}
1386
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001387int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001388 bool secure, const UidRangeMap& uidRangeMap,
1389 bool excludeLocalRoutes) {
Ken Chen4ea88462021-05-23 14:56:43 +08001390 return modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_DEL,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001391 !MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001392}
1393
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001394int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1395 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001396}
1397
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001398int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1399 Permission permission) {
1400 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001401}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001402
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001403int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001404 TableType tableType, int mtu, int priority) {
Tyler Wearfa94a272019-12-05 15:01:48 -08001405 return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, interface, destination, nexthop,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001406 tableType, mtu, priority);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001407}
1408
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001409int RouteController::removeRoute(const char* interface, const char* destination,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001410 const char* nexthop, TableType tableType, int priority) {
Tyler Wearfa94a272019-12-05 15:01:48 -08001411 return modifyRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, interface, destination, nexthop,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001412 tableType, 0 /* mtu */, priority);
Tyler Wearfa94a272019-12-05 15:01:48 -08001413}
1414
1415int RouteController::updateRoute(const char* interface, const char* destination,
1416 const char* nexthop, TableType tableType, int mtu) {
1417 return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_REPLACE_FLAGS, interface, destination, nexthop,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001418 tableType, mtu, 0 /* priority */);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001419}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001420
1421int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001422 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001423}
1424
1425int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001426 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001427}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001428
1429int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1430 Permission permission) {
1431 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1432}
1433
1434int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1435 const char* physicalInterface,
1436 Permission permission) {
1437 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1438}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001439
Ken Chen8738e1c2020-11-24 11:38:54 +08001440int RouteController::addUsersToPhysicalNetwork(unsigned netId, const char* interface,
Ken Chen4ea88462021-05-23 14:56:43 +08001441 const UidRangeMap& uidRangeMap) {
1442 return modifyPhysicalNetwork(netId, interface, uidRangeMap, PERMISSION_NONE, ACTION_ADD,
Ken Chen8738e1c2020-11-24 11:38:54 +08001443 !MODIFY_NON_UID_BASED_RULES);
1444}
1445
1446int RouteController::removeUsersFromPhysicalNetwork(unsigned netId, const char* interface,
Ken Chen4ea88462021-05-23 14:56:43 +08001447 const UidRangeMap& uidRangeMap) {
1448 return modifyPhysicalNetwork(netId, interface, uidRangeMap, PERMISSION_NONE, ACTION_DEL,
Ken Chen8738e1c2020-11-24 11:38:54 +08001449 !MODIFY_NON_UID_BASED_RULES);
1450}
1451
Ken Chen4ea88462021-05-23 14:56:43 +08001452int RouteController::addUsersToUnreachableNetwork(unsigned netId, const UidRangeMap& uidRangeMap) {
1453 return modifyUnreachableNetwork(netId, uidRangeMap, ACTION_ADD);
Ken Chen4e8ef9b2021-03-17 01:57:19 +08001454}
1455
Ken Chen4ea88462021-05-23 14:56:43 +08001456int RouteController::removeUsersFromUnreachableNetwork(unsigned netId,
1457 const UidRangeMap& uidRangeMap) {
1458 return modifyUnreachableNetwork(netId, uidRangeMap, ACTION_DEL);
Ken Chen4e8ef9b2021-03-17 01:57:19 +08001459}
1460
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001461// Protects sInterfaceToTable.
Luke Huang534980c2018-07-06 17:50:11 +08001462std::mutex RouteController::sInterfaceToTableLock;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001463std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1464
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001465} // namespace android::net