blob: d63dbd2efe5a5cbb7705142f6092e8ccf296767b [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;
46
Bernie Innocenti762dcf42019-06-14 19:52:49 +090047namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090048
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090049auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand;
Chiachang Wangf79e3562022-01-15 13:31:04 +080050auto RouteController::ifNameToIndexFunction = if_nametoindex;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070051// BEGIN CONSTANTS --------------------------------------------------------------------------------
52
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070053const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070054const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
55const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
56
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070057const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070058const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
59const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
60
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070061const char* const ROUTE_TABLE_NAME_LOCAL = "local";
62const char* const ROUTE_TABLE_NAME_MAIN = "main";
63
Lorenzo Colittid78843e2017-03-27 05:52:31 +090064const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
65
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070066const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
67
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070068const uid_t UID_ROOT = 0;
Lorenzo Colitti3093f562017-09-25 14:17:38 +090069const uint32_t FWMARK_NONE = 0;
70const uint32_t MASK_NONE = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090071const char* const IIF_LOOPBACK = "lo";
Yi Kongbdfd57e2018-07-25 13:26:10 -070072const char* const IIF_NONE = nullptr;
73const char* const OIF_NONE = nullptr;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070074const bool ACTION_ADD = true;
75const bool ACTION_DEL = false;
76const bool MODIFY_NON_UID_BASED_RULES = true;
77
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -070078const 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 -070079
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070080// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
81// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
Bernie Innocenti762dcf42019-06-14 19:52:49 +090082static constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070083 return RTA_LENGTH(x);
84}
85
86// These are practically const, but can't be declared so, because they are used to initialize
87// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +090088rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
89rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
90rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
91rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
Lorenzo Colitti2b078672016-12-16 18:45:03 +090092rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070093
Lorenzo Colitti2b078672016-12-16 18:45:03 +090094rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
95rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +090096rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070097
Tyler Wearfa94a272019-12-05 15:01:48 -080098// One or more nested attributes in the RTA_METRICS attribute.
99rtattr RTATTRX_MTU = { U16_RTA_LENGTH(sizeof(uint32_t)), RTAX_MTU};
Lorenzo Colitti57d54682020-01-24 09:18:13 +0900100constexpr size_t RTATTRX_MTU_SIZE = RTA_SPACE(sizeof(uint32_t));
Tyler Wearfa94a272019-12-05 15:01:48 -0800101
102// The RTA_METRICS attribute itself.
Lorenzo Colitti57d54682020-01-24 09:18:13 +0900103constexpr size_t RTATTR_METRICS_SIZE = RTATTRX_MTU_SIZE;
Tyler Wearfa94a272019-12-05 15:01:48 -0800104rtattr RTATTR_METRICS = { U16_RTA_LENGTH(RTATTR_METRICS_SIZE), RTA_METRICS };
105
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700106uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
107
Ken Chena2206ec2021-03-24 17:15:44 +0800108constexpr bool EXPLICIT = true;
109constexpr bool IMPLICIT = false;
110
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700111// END CONSTANTS ----------------------------------------------------------------------------------
112
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900113static const char* actionName(uint16_t action) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900114 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
115 return ops[action % 4];
116}
117
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900118static const char* familyName(uint8_t family) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900119 switch (family) {
120 case AF_INET: return "IPv4";
121 case AF_INET6: return "IPv6";
122 default: return "???";
123 }
124}
125
Lorenzo Colitti3810c972021-01-06 11:44:02 +0900126static void maybeModifyQdiscClsact(const char* interface, bool add);
127
Chiachang Wangf79e3562022-01-15 13:31:04 +0800128static uint32_t getRouteTableIndexFromGlobalRouteTableIndex(uint32_t index, bool local) {
129 // The local table is
130 // "global table - ROUTE_TABLE_OFFSET_FROM_INDEX + ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL"
131 const uint32_t localTableOffset = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL -
132 RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
133 return local ? index + localTableOffset : index;
134}
135
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900136// Caller must hold sInterfaceToTableLock.
Chiachang Wangf79e3562022-01-15 13:31:04 +0800137uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface, bool local) {
mtk137999f2913e2019-02-25 19:39:36 +0800138 // If we already know the routing table for this interface name, use it.
139 // This ensures we can remove rules and routes for an interface that has been removed,
140 // or has been removed and re-added with a different interface index.
141 //
142 // The caller is responsible for ensuring that an interface is never added to a network
143 // until it has been removed from any network it was previously in. This ensures that
144 // if the same interface disconnects and then reconnects with a different interface ID
145 // when the reconnect happens the interface will not be in the map, and the code will
146 // determine the new routing table from the interface ID, below.
Chiachang Wangf79e3562022-01-15 13:31:04 +0800147 //
148 // sInterfaceToTable stores the *global* routing table for the interface, and the local table is
149 // "global table - ROUTE_TABLE_OFFSET_FROM_INDEX + ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL"
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900150 auto iter = sInterfaceToTable.find(interface);
mtk137999f2913e2019-02-25 19:39:36 +0800151 if (iter != sInterfaceToTable.end()) {
Chiachang Wangf79e3562022-01-15 13:31:04 +0800152 return getRouteTableIndexFromGlobalRouteTableIndex(iter->second, local);
mtk137999f2913e2019-02-25 19:39:36 +0800153 }
154
Chiachang Wangf79e3562022-01-15 13:31:04 +0800155 uint32_t index = RouteController::ifNameToIndexFunction(interface);
mtk137999f2913e2019-02-25 19:39:36 +0800156 if (index == 0) {
Lorenzo Colittib6dc40a2020-03-24 00:58:50 +0900157 ALOGE("cannot find interface %s: %s", interface, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700158 return RT_TABLE_UNSPEC;
159 }
mtk137999f2913e2019-02-25 19:39:36 +0800160 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
161 sInterfaceToTable[interface] = index;
Chiachang Wangf79e3562022-01-15 13:31:04 +0800162 return getRouteTableIndexFromGlobalRouteTableIndex(index, local);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700163}
164
Rubin Xu6c00b612018-04-27 14:27:59 +0100165uint32_t RouteController::getIfIndex(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900166 std::lock_guard lock(sInterfaceToTableLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100167
168 auto iter = sInterfaceToTable.find(interface);
169 if (iter == sInterfaceToTable.end()) {
170 ALOGE("getIfIndex: cannot find interface %s", interface);
171 return 0;
172 }
173
Lorenzo Colittib6dc40a2020-03-24 00:58:50 +0900174 // For interfaces that are not in the local network, the routing table is always the interface
175 // index plus ROUTE_TABLE_OFFSET_FROM_INDEX. But for interfaces in the local network, there's no
176 // way to know the interface index from this table. Return 0 here so callers of this method do
177 // not get confused.
178 // TODO: stop calling this method from any caller that only wants interfaces in client mode.
179 int ifindex = iter->second;
180 if (ifindex == ROUTE_TABLE_LOCAL_NETWORK) {
181 return 0;
182 }
183
184 return ifindex - ROUTE_TABLE_OFFSET_FROM_INDEX;
Rubin Xu6c00b612018-04-27 14:27:59 +0100185}
186
Chiachang Wangf79e3562022-01-15 13:31:04 +0800187uint32_t RouteController::getRouteTableForInterface(const char* interface, bool local) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900188 std::lock_guard lock(sInterfaceToTableLock);
Chiachang Wangf79e3562022-01-15 13:31:04 +0800189 return getRouteTableForInterfaceLocked(interface, local);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900190}
191
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700192void addTableName(uint32_t table, const std::string& name, std::string* contents) {
193 char tableString[UINT32_STRLEN];
194 snprintf(tableString, sizeof(tableString), "%u", table);
195 *contents += tableString;
196 *contents += " ";
197 *contents += name;
198 *contents += "\n";
199}
200
201// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900202void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700203 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700204
205 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
206 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
207
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700208 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700209 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
210 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700211
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900212 std::lock_guard lock(sInterfaceToTableLock);
Chiachang Wangf79e3562022-01-15 13:31:04 +0800213 for (const auto& [ifName, ifIndex] : sInterfaceToTable) {
214 addTableName(ifIndex, ifName, &contents);
215 // Add table for the local route of the network. It's expected to be used for excluding the
216 // local traffic in the VPN network.
217 // Start from ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL plus with the interface table index.
218 uint32_t offset = ROUTE_TABLE_OFFSET_FROM_INDEX_FOR_LOCAL - ROUTE_TABLE_OFFSET_FROM_INDEX;
219 addTableName(offset + ifIndex, ifName + INTERFACE_LOCAL_SUFFIX, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700220 }
221
Dan Albert5407e142015-03-16 10:05:59 -0700222 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800223 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700224 return;
225 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700226}
227
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700228// Returns 0 on success or negative errno on failure.
229int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
230 if (!input) {
231 *length = 0;
232 *padding = 0;
233 return 0;
234 }
235 *length = strlcpy(name, input, IFNAMSIZ) + 1;
236 if (*length > IFNAMSIZ) {
237 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
238 return -ENAMETOOLONG;
239 }
240 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
241 return 0;
242}
243
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700244// Adds or removes a routing rule for IPv4 and IPv6.
245//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900246// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100247// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700248// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
249// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700250// + If |iif| is non-NULL, the rule matches the specified incoming interface.
251// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
252// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
253// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900254//
255// Returns 0 on success or negative errno on failure.
Ken Chen53360bf2021-12-10 02:41:05 +0800256[[nodiscard]] static int modifyIpRule(uint16_t action, int32_t priority, uint8_t ruleType,
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900257 uint32_t table, uint32_t fwmark, uint32_t mask,
258 const char* iif, const char* oif, uid_t uidStart,
259 uid_t uidEnd) {
Ken Chen53360bf2021-12-10 02:41:05 +0800260 if (priority < 0) {
261 ALOGE("invalid IP-rule priority %d", priority);
262 return -ERANGE;
263 }
264
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700265 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
266 if (fwmark & ~mask) {
267 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
268 return -ERANGE;
269 }
270
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700271 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900272 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700273 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
274 size_t iifLength, oifLength;
275 uint16_t iifPadding, oifPadding;
276 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
277 return ret;
278 }
279 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
280 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900281 }
282
Lorenzo Colitti59656512014-06-25 03:20:29 +0900283 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700284 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700285 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900286 return -EUSERS;
287 }
Robin Lee4ef94642016-04-01 11:50:49 +0100288
Lorenzo Colitti72723682014-06-26 13:51:10 +0900289 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900290
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900291 // Assemble a rule request and put it in an array of iovec structures.
292 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100293 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900294 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
295 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900296 };
297
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900298 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
299 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
300 // table 0, it's a wildcard that matches anything.
301 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
302 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
303 return -ENOTUNIQ;
304 }
305
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700306 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
307 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900308 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900309
310 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700311 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700312 { &rule, sizeof(rule) },
313 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
314 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700315 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
316 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700317 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
318 { &fwmark, mask ? sizeof(fwmark) : 0 },
319 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
320 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900321 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
322 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700323 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
324 { iifName, iifLength },
325 { PADDING_BUFFER, iifPadding },
326 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
327 { oifName, oifLength },
328 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900329 };
330
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900331 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700332 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
333 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900334 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900335 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
336 // Don't log when deleting a tethering rule that's not there. This matches the
337 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
338 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
339 strerror(-ret));
340 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900341 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700342 }
343 }
344
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900345 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700346}
347
Ken Chen53360bf2021-12-10 02:41:05 +0800348[[nodiscard]] static int modifyIpRule(uint16_t action, int32_t priority, uint32_t table,
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900349 uint32_t fwmark, uint32_t mask, const char* iif,
350 const char* oif, uid_t uidStart, uid_t uidEnd) {
Robin Lee4ef94642016-04-01 11:50:49 +0100351 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
352 uidEnd);
353}
354
Ken Chen53360bf2021-12-10 02:41:05 +0800355[[nodiscard]] static int modifyIpRule(uint16_t action, int32_t priority, uint32_t table,
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900356 uint32_t fwmark, uint32_t mask) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700357 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
358 INVALID_UID);
359}
360
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900361// Adds or deletes an IPv4 or IPv6 route.
362// Returns 0 on success or negative errno on failure.
Tyler Wearfa94a272019-12-05 15:01:48 -0800363int modifyIpRoute(uint16_t action, uint16_t flags, uint32_t table, const char* interface,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200364 const char* destination, const char* nexthop, uint32_t mtu, uint32_t priority) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900365 // At least the destination must be non-null.
366 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700367 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900368 return -EFAULT;
369 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700370
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900371 // Parse the prefix.
372 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700373 uint8_t family;
374 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900375 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
376 &prefixLength);
377 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700378 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900379 return rawLength;
380 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700381
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900382 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700383 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900384 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
385 }
386
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700387 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900388 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900389 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700390
391 if (nexthop && !strcmp(nexthop, "unreachable")) {
392 type = RTN_UNREACHABLE;
393 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
394 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
395 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
396 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700397 nexthop = nullptr;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900398 } else if (nexthop && !strcmp(nexthop, "throw")) {
399 type = RTN_THROW;
400 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700401 nexthop = nullptr;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700402 } else {
403 // If an interface was specified, find the ifindex.
404 if (interface != OIF_NONE) {
Chiachang Wangf79e3562022-01-15 13:31:04 +0800405 ifindex = RouteController::ifNameToIndexFunction(interface);
406
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700407 if (!ifindex) {
408 ALOGE("cannot find interface %s", interface);
409 return -ENODEV;
410 }
411 }
412
413 // If a nexthop was specified, parse it as the same family as the prefix.
414 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
415 ALOGE("inet_pton failed for nexthop %s", nexthop);
416 return -EINVAL;
417 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900418 }
419
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900420 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700421 rtmsg route = {
Nick Desaulniers6b357502019-10-11 09:26:44 -0700422 .rtm_family = family,
423 .rtm_dst_len = prefixLength,
424 .rtm_protocol = RTPROT_STATIC,
425 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
426 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900427 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900428
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700429 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
430 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900431
432 iovec iov[] = {
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200433 {nullptr, 0},
434 {&route, sizeof(route)},
435 {&RTATTR_TABLE, sizeof(RTATTR_TABLE)},
436 {&table, sizeof(table)},
437 {&rtaDst, sizeof(rtaDst)},
438 {rawAddress, static_cast<size_t>(rawLength)},
439 {&RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0},
440 {&ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0},
441 {&rtaGateway, nexthop ? sizeof(rtaGateway) : 0},
442 {rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0},
443 {&RTATTR_METRICS, mtu != 0 ? sizeof(RTATTR_METRICS) : 0},
444 {&RTATTRX_MTU, mtu != 0 ? sizeof(RTATTRX_MTU) : 0},
445 {&mtu, mtu != 0 ? sizeof(mtu) : 0},
446 {&RTATTR_PRIO, priority != 0 ? sizeof(RTATTR_PRIO) : 0},
447 {&priority, priority != 0 ? sizeof(priority) : 0},
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900448 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900449
Jimmy Chence9e5782019-03-08 16:12:55 +0800450 // Allow creating multiple link-local routes in the same table, so we can make IPv6
451 // work on all interfaces in the local_network table.
452 if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) {
453 flags &= ~NLM_F_EXCL;
454 }
455
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900456 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900457 if (ret) {
458 ALOGE("Error %s route %s -> %s %s to table %u: %s",
459 actionName(action), destination, nexthop, interface, table, strerror(-ret));
460 }
461 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700462}
463
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700464// An iptables rule to mark incoming packets on a network with the netId of the network.
465//
466// This is so that the kernel can:
467// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
468// replies, SYN-ACKs, etc).
469// + Mark sockets that accept connections from this interface so that the connection stays on the
470// same interface.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900471int modifyIncomingPacketMark(unsigned netId, const char* interface, Permission permission,
472 bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700473 Fwmark fwmark;
474
475 fwmark.netId = netId;
476 fwmark.explicitlySelected = true;
477 fwmark.protectedFromVpn = true;
478 fwmark.permission = permission;
479
Benedict Wongb9baf262017-12-03 15:43:08 -0800480 const uint32_t mask = ~Fwmark::getUidBillingMask();
481
482 std::string cmd = StringPrintf(
483 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
484 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900485 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700486 ALOGE("failed to change iptables rule that sets incoming packet mark");
487 return -EREMOTEIO;
488 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700489
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700490 return 0;
491}
492
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700493// A rule to route responses to the local network forwarded via the VPN.
494//
495// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
496// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900497[[nodiscard]] static int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700498 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
499 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
500 INVALID_UID, INVALID_UID);
501}
502
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700503// A rule to route all traffic from a given set of UIDs to go over the VPN.
504//
505// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
506// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
507// bypass the VPN if the protectedFromVpn bit is set.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900508[[nodiscard]] static int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Chiachang Wang2e9443f2022-01-14 22:55:36 +0800509 int32_t subPriority, bool secure, bool add,
510 bool excludeLocalRoutes) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700511 Fwmark fwmark;
512 Fwmark mask;
513
514 fwmark.protectedFromVpn = false;
515 mask.protectedFromVpn = true;
516
Ken Chen53360bf2021-12-10 02:41:05 +0800517 int32_t priority;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700518
519 if (secure) {
520 priority = RULE_PRIORITY_SECURE_VPN;
521 } else {
Chiachang Wang2e9443f2022-01-14 22:55:36 +0800522 priority = excludeLocalRoutes ? RULE_PRIORITY_BYPASSABLE_VPN_LOCAL_EXCLUSION
523 : RULE_PRIORITY_BYPASSABLE_VPN_NO_LOCAL_EXCLUSION;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700524
525 fwmark.explicitlySelected = false;
526 mask.explicitlySelected = true;
527 }
528
Ken Chen4ea88462021-05-23 14:56:43 +0800529 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority + subPriority, table,
530 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700531}
532
533// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
534// set of UIDs.
535//
536// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
537// target set, but where the DnsProxyListener itself is not.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900538[[nodiscard]] static int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
Chiachang Wang2e9443f2022-01-14 22:55:36 +0800539 bool add, bool excludeLocalRoutes) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700540 Fwmark fwmark;
541 Fwmark mask;
542
543 fwmark.netId = netId;
544 mask.netId = FWMARK_NET_ID_MASK;
545
546 fwmark.permission = PERMISSION_SYSTEM;
547 mask.permission = PERMISSION_SYSTEM;
548
Chiachang Wang2e9443f2022-01-14 22:55:36 +0800549 uint32_t priority;
550
551 if (secure) {
552 priority = RULE_PRIORITY_SECURE_VPN;
553 } else {
554 priority = excludeLocalRoutes ? RULE_PRIORITY_BYPASSABLE_VPN_LOCAL_EXCLUSION
555 : RULE_PRIORITY_BYPASSABLE_VPN_NO_LOCAL_EXCLUSION;
556 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700557
558 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
559 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700560}
561
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700562// A rule to route traffic based on an explicitly chosen network.
563//
564// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
565//
566// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
567// to check it again in the rules here, because a network's permissions may have been updated via
568// modifyNetworkPermission().
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900569[[nodiscard]] static int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
570 Permission permission, uid_t uidStart,
Ken Chen53360bf2021-12-10 02:41:05 +0800571 uid_t uidEnd, int32_t subPriority, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700572 Fwmark fwmark;
573 Fwmark mask;
574
575 fwmark.netId = netId;
576 mask.netId = FWMARK_NET_ID_MASK;
577
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700578 fwmark.explicitlySelected = true;
579 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700580
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700581 fwmark.permission = permission;
582 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700583
Ken Chen4ea88462021-05-23 14:56:43 +0800584 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
585 RULE_PRIORITY_EXPLICIT_NETWORK + subPriority, table, fwmark.intValue,
586 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700587}
588
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700589// A rule to route traffic based on a chosen outgoing interface.
590//
591// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
592// the outgoing interface (typically for link-local communications).
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900593[[nodiscard]] static int modifyOutputInterfaceRules(const char* interface, uint32_t table,
594 Permission permission, uid_t uidStart,
Ken Chen53360bf2021-12-10 02:41:05 +0800595 uid_t uidEnd, int32_t subPriority, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700596 Fwmark fwmark;
597 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700598
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700599 fwmark.permission = permission;
600 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700601
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900602 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900603 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
604 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900605 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
606 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900607 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900608 UID_ROOT, UID_ROOT)) {
609 return ret;
610 }
611 }
612
Ken Chen4ea88462021-05-23 14:56:43 +0800613 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
614 RULE_PRIORITY_OUTPUT_INTERFACE + subPriority, table, fwmark.intValue,
615 mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700616}
617
618// A rule to route traffic based on the chosen network.
619//
620// This is for sockets that have not explicitly requested a particular network, but have been
621// bound to one when they called connect(). This ensures that sockets connected on a particular
622// network stay on that network even if the default network changes.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900623[[nodiscard]] static int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700624 Fwmark fwmark;
625 Fwmark mask;
626
627 fwmark.netId = netId;
628 mask.netId = FWMARK_NET_ID_MASK;
629
630 fwmark.explicitlySelected = false;
631 mask.explicitlySelected = true;
632
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900633 fwmark.permission = PERMISSION_NONE;
634 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700635
636 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900637 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
638 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700639}
640
Chiachang Wang8b9cdd22022-01-27 10:43:28 +0800641int RouteController::modifyVpnLocalExclusionRule(bool add, const char* physicalInterface) {
Chiachang Wange7bf5f52022-01-17 11:37:39 +0800642 uint32_t table = getRouteTableForInterface(physicalInterface, true /* local */);
643 if (table == RT_TABLE_UNSPEC) {
644 return -ESRCH;
645 }
646
647 Fwmark fwmark;
648 Fwmark mask;
649
650 fwmark.explicitlySelected = false;
651 mask.explicitlySelected = true;
652
653 fwmark.permission = PERMISSION_NONE;
654 mask.permission = PERMISSION_NONE;
655
chiachangwang31902a42022-04-15 20:06:27 +0800656 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_LOCAL_ROUTES, table,
657 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
658 INVALID_UID);
Chiachang Wange7bf5f52022-01-17 11:37:39 +0800659}
660
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700661// A rule to enable split tunnel VPNs.
662//
663// 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 +0800664// go over the default network, provided it has the permissions required by the default network.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900665int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
666 const char* physicalInterface,
667 Permission permission) {
Chiachang Wangf79e3562022-01-15 13:31:04 +0800668 uint32_t table = getRouteTableForInterface(physicalInterface, false /* local */);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700669 if (table == RT_TABLE_UNSPEC) {
670 return -ESRCH;
671 }
672
673 Fwmark fwmark;
674 Fwmark mask;
675
676 fwmark.netId = vpnNetId;
677 mask.netId = FWMARK_NET_ID_MASK;
678
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700679 fwmark.permission = permission;
680 mask.permission = permission;
681
682 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
683 mask.intValue);
684}
685
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700686// Add rules to allow legacy routes added through the requestRouteToHost() API.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900687[[nodiscard]] static int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700688 Fwmark fwmark;
689 Fwmark mask;
690
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700691 fwmark.explicitlySelected = false;
692 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700693
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700694 // Rules to allow legacy routes to override the default network.
695 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
696 fwmark.intValue, mask.intValue)) {
697 return ret;
698 }
699 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
700 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
701 return ret;
702 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700703
704 fwmark.permission = PERMISSION_SYSTEM;
705 mask.permission = PERMISSION_SYSTEM;
706
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700707 // A rule to allow legacy routes from system apps to override VPNs.
708 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700709 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700710}
711
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700712// Add rules to lookup the local network when specified explicitly or otherwise.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900713[[nodiscard]] static int addLocalNetworkRules(unsigned localNetId) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700714 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
Ken Chen4ea88462021-05-23 14:56:43 +0800715 INVALID_UID, INVALID_UID,
Patrick Rohre2f1b5a2022-01-25 21:36:50 +0100716 UidRanges::SUB_PRIORITY_HIGHEST, ACTION_ADD)) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700717 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700718 }
719
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700720 Fwmark fwmark;
721 Fwmark mask;
722
723 fwmark.explicitlySelected = false;
724 mask.explicitlySelected = true;
725
726 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
727 fwmark.intValue, mask.intValue);
728}
729
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900730/* static */
731int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900732 const char *interface = DummyNetwork::INTERFACE_NAME;
Chiachang Wangf79e3562022-01-15 13:31:04 +0800733 uint32_t table = getRouteTableForInterface(interface, false /* local */);
Lorenzo Colitti36679362015-02-25 10:26:19 +0900734 if (table == RT_TABLE_UNSPEC) {
Hungming Chen7f725432020-02-07 17:47:23 +0800735 // getRouteTableForInterface has already logged an error.
Lorenzo Colitti36679362015-02-25 10:26:19 +0900736 return -ESRCH;
737 }
738
739 ifc_init();
740 int ret = ifc_up(interface);
741 ifc_close();
742 if (ret) {
743 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
744 return -errno;
745 }
746
Ken Chen4ea88462021-05-23 14:56:43 +0800747 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, INVALID_UID,
Patrick Rohre2f1b5a2022-01-25 21:36:50 +0100748 INVALID_UID, UidRanges::SUB_PRIORITY_HIGHEST,
Ken Chen4ea88462021-05-23 14:56:43 +0800749 ACTION_ADD))) {
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900750 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900751 return ret;
752 }
753
Tyler Wearfa94a272019-12-05 15:01:48 -0800754 if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200755 "0.0.0.0/0", nullptr, 0 /* mtu */, 0 /* priority */))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900756 return ret;
757 }
758
Tyler Wearfa94a272019-12-05 15:01:48 -0800759 if ((ret = modifyIpRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, table, interface, "::/0",
Taras Antoshchuk0cceda22021-09-24 18:40:03 +0200760 nullptr, 0 /* mtu */, 0 /* priority */))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900761 return ret;
762 }
763
764 return 0;
765}
766
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900767// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
768// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
769// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
770// rule will hopefully make things even clearer.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900771[[nodiscard]] static int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100772 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
773 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700774}
775
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900776[[nodiscard]] static int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700777 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
778 return ret;
779 }
Lorenzo Colitti3810c972021-01-06 11:44:02 +0900780 maybeModifyQdiscClsact(interface, add);
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900781 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
Patrick Rohre2f1b5a2022-01-25 21:36:50 +0100782 INVALID_UID, INVALID_UID, UidRanges::SUB_PRIORITY_HIGHEST,
Ken Chen4ea88462021-05-23 14:56:43 +0800783 add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700784}
785
Ken Chena2206ec2021-03-24 17:15:44 +0800786[[nodiscard]] static int modifyUidNetworkRule(unsigned netId, uint32_t table, uid_t uidStart,
Ken Chen53360bf2021-12-10 02:41:05 +0800787 uid_t uidEnd, int32_t subPriority, bool add,
Ken Chen4ea88462021-05-23 14:56:43 +0800788 bool explicitSelect) {
Ken Chen8738e1c2020-11-24 11:38:54 +0800789 if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
Ken Chena2206ec2021-03-24 17:15:44 +0800790 ALOGE("modifyUidNetworkRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
Ken Chen8738e1c2020-11-24 11:38:54 +0800791 return -EUSERS;
792 }
793
794 Fwmark fwmark;
795 Fwmark mask;
796
797 fwmark.netId = netId;
798 mask.netId = FWMARK_NET_ID_MASK;
799
Ken Chena2206ec2021-03-24 17:15:44 +0800800 fwmark.explicitlySelected = explicitSelect;
Ken Chen8738e1c2020-11-24 11:38:54 +0800801 mask.explicitlySelected = true;
802
803 // Access to this network is controlled by UID rules, not permission bits.
804 fwmark.permission = PERMISSION_NONE;
805 mask.permission = PERMISSION_NONE;
806
Ken Chena2206ec2021-03-24 17:15:44 +0800807 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
Ken Chen4ea88462021-05-23 14:56:43 +0800808 explicitSelect ? (RULE_PRIORITY_UID_EXPLICIT_NETWORK + subPriority)
809 : (RULE_PRIORITY_UID_IMPLICIT_NETWORK + subPriority),
Ken Chena2206ec2021-03-24 17:15:44 +0800810 table, fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart,
811 uidEnd);
Ken Chen8738e1c2020-11-24 11:38:54 +0800812}
813
814[[nodiscard]] static int modifyUidDefaultNetworkRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Ken Chen53360bf2021-12-10 02:41:05 +0800815 int32_t subPriority, bool add) {
Ken Chen8738e1c2020-11-24 11:38:54 +0800816 if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
817 ALOGE("modifyUidDefaultNetworkRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
818 return -EUSERS;
819 }
820
821 Fwmark fwmark;
822 Fwmark mask;
823
824 fwmark.netId = NETID_UNSET;
825 mask.netId = FWMARK_NET_ID_MASK;
826
827 // Access to this network is controlled by UID rules, not permission bits.
828 fwmark.permission = PERMISSION_NONE;
829 mask.permission = PERMISSION_NONE;
830
Ken Chen4ea88462021-05-23 14:56:43 +0800831 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
832 RULE_PRIORITY_UID_DEFAULT_NETWORK + subPriority, table, fwmark.intValue,
833 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Ken Chen8738e1c2020-11-24 11:38:54 +0800834}
835
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900836/* static */
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900837int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
Ken Chen4ea88462021-05-23 14:56:43 +0800838 const UidRangeMap& uidRangeMap, Permission permission,
Ken Chen8738e1c2020-11-24 11:38:54 +0800839 bool add, bool modifyNonUidBasedRules) {
Chiachang Wangf79e3562022-01-15 13:31:04 +0800840 uint32_t table = getRouteTableForInterface(interface, false /* local */);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700841 if (table == RT_TABLE_UNSPEC) {
842 return -ESRCH;
843 }
844
Ken Chen4ea88462021-05-23 14:56:43 +0800845 for (const auto& [subPriority, uidRanges] : uidRangeMap) {
846 for (const UidRangeParcel& range : uidRanges.getRanges()) {
847 if (int ret = modifyUidNetworkRule(netId, table, range.start, range.stop, subPriority,
848 add, EXPLICIT)) {
849 return ret;
850 }
851 if (int ret = modifyUidNetworkRule(netId, table, range.start, range.stop, subPriority,
852 add, IMPLICIT)) {
853 return ret;
854 }
Patrick Rohre6f198c2022-01-25 13:50:31 +0100855 // SUB_PRIORITY_NO_DEFAULT is "special" and does not require a
856 // default network rule, see UidRanges.h.
857 if (subPriority != UidRanges::SUB_PRIORITY_NO_DEFAULT) {
858 if (int ret = modifyUidDefaultNetworkRule(table, range.start, range.stop,
859 subPriority, add)) {
860 return ret;
861 }
Ken Chen4ea88462021-05-23 14:56:43 +0800862 }
Ken Chen8738e1c2020-11-24 11:38:54 +0800863 }
864 }
865
866 if (!modifyNonUidBasedRules) {
867 // we are done.
868 return 0;
869 }
870
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700871 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
872 return ret;
873 }
874 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
Patrick Rohre2f1b5a2022-01-25 21:36:50 +0100875 UidRanges::SUB_PRIORITY_HIGHEST, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700876 return ret;
877 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900878 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Patrick Rohre2f1b5a2022-01-25 21:36:50 +0100879 UidRanges::SUB_PRIORITY_HIGHEST, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700880 return ret;
881 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900882
883 // Only set implicit rules for networks that don't require permissions.
884 //
885 // This is so that if the default network ceases to be the default network and then switches
886 // from requiring no permissions to requiring permissions, we ensure that apps only use the
887 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
888 // - it closes all sockets on the network except sockets that are explicitly selected.
889 //
890 // The lack of this rule only affects the special case above, because:
891 // - The only cases where we implicitly bind a socket to a network are the default network and
892 // the bypassable VPN that applies to the app, if any.
893 // - This rule doesn't affect VPNs because they don't support permissions at all.
894 // - The default network doesn't require permissions. While we support doing this, the framework
895 // never does it (partly because we'd end up in the situation where we tell apps that there is
896 // a default network, but they can't use it).
897 // - If the network is still the default network, the presence or absence of this rule does not
898 // matter.
899 //
900 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
901 // implicitly bound to a network because at the time of connect() it was the default, and that
902 // network must no longer be the default, and must now require permissions.
903 if (permission == PERMISSION_NONE) {
904 return modifyImplicitNetworkRule(netId, table, add);
905 }
906 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700907}
908
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800909[[nodiscard]] static int modifyUidUnreachableRule(unsigned netId, uid_t uidStart, uid_t uidEnd,
Ken Chen53360bf2021-12-10 02:41:05 +0800910 int32_t subPriority, bool add,
Ken Chen4ea88462021-05-23 14:56:43 +0800911 bool explicitSelect) {
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800912 if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
913 ALOGE("modifyUidUnreachableRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
914 return -EUSERS;
915 }
916
917 Fwmark fwmark;
918 Fwmark mask;
919
920 fwmark.netId = netId;
921 mask.netId = FWMARK_NET_ID_MASK;
922
923 fwmark.explicitlySelected = explicitSelect;
924 mask.explicitlySelected = true;
925
926 // Access to this network is controlled by UID rules, not permission bits.
927 fwmark.permission = PERMISSION_NONE;
928 mask.permission = PERMISSION_NONE;
929
930 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
Ken Chen4ea88462021-05-23 14:56:43 +0800931 explicitSelect ? (RULE_PRIORITY_UID_EXPLICIT_NETWORK + subPriority)
932 : (RULE_PRIORITY_UID_IMPLICIT_NETWORK + subPriority),
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800933 FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
934 IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
935}
936
Ken Chen4ea88462021-05-23 14:56:43 +0800937[[nodiscard]] static int modifyUidDefaultUnreachableRule(uid_t uidStart, uid_t uidEnd,
Ken Chen53360bf2021-12-10 02:41:05 +0800938 int32_t subPriority, bool add) {
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800939 if ((uidStart == INVALID_UID) || (uidEnd == INVALID_UID)) {
Ken Chen4ea88462021-05-23 14:56:43 +0800940 ALOGE("modifyUidDefaultUnreachableRule, invalid UIDs (%u, %u)", uidStart, uidEnd);
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800941 return -EUSERS;
942 }
943
944 Fwmark fwmark;
945 Fwmark mask;
946
947 fwmark.netId = NETID_UNSET;
948 mask.netId = FWMARK_NET_ID_MASK;
949
950 // Access to this network is controlled by UID rules, not permission bits.
951 fwmark.permission = PERMISSION_NONE;
952 mask.permission = PERMISSION_NONE;
953
Ken Chen4ea88462021-05-23 14:56:43 +0800954 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
955 RULE_PRIORITY_UID_DEFAULT_UNREACHABLE + subPriority, FR_ACT_UNREACHABLE,
956 RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
957 uidStart, uidEnd);
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800958}
959
Ken Chen4ea88462021-05-23 14:56:43 +0800960int RouteController::modifyUnreachableNetwork(unsigned netId, const UidRangeMap& uidRangeMap,
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800961 bool add) {
Ken Chen4ea88462021-05-23 14:56:43 +0800962 for (const auto& [subPriority, uidRanges] : uidRangeMap) {
963 for (const UidRangeParcel& range : uidRanges.getRanges()) {
964 if (int ret = modifyUidUnreachableRule(netId, range.start, range.stop, subPriority, add,
965 EXPLICIT)) {
966 return ret;
967 }
968 if (int ret = modifyUidUnreachableRule(netId, range.start, range.stop, subPriority, add,
969 IMPLICIT)) {
970 return ret;
971 }
972 if (int ret = modifyUidDefaultUnreachableRule(range.start, range.stop, subPriority,
973 add)) {
974 return ret;
975 }
Ken Chen4e8ef9b2021-03-17 01:57:19 +0800976 }
977 }
978
979 return 0;
980}
981
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900982[[nodiscard]] static int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
Robin Leeb8087362016-03-30 18:43:08 +0100983 Fwmark fwmark;
984 Fwmark mask;
985 fwmark.protectedFromVpn = false;
986 mask.protectedFromVpn = true;
987
Luke Huang94658ac2018-10-18 19:35:12 +0900988 for (const UidRangeParcel& range : uidRanges.getRanges()) {
989 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN,
990 FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
991 IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) {
Robin Leeb8087362016-03-30 18:43:08 +0100992 return ret;
993 }
994 }
995
996 return 0;
997}
998
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900999int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
Ken Chen4ea88462021-05-23 14:56:43 +08001000 const UidRangeMap& uidRangeMap, bool secure, bool add,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001001 bool modifyNonUidBasedRules, bool excludeLocalRoutes) {
Chiachang Wangf79e3562022-01-15 13:31:04 +08001002 uint32_t table = getRouteTableForInterface(interface, false /* false */);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001003 if (table == RT_TABLE_UNSPEC) {
1004 return -ESRCH;
1005 }
1006
Ken Chen4ea88462021-05-23 14:56:43 +08001007 for (const auto& [subPriority, uidRanges] : uidRangeMap) {
1008 for (const UidRangeParcel& range : uidRanges.getRanges()) {
1009 if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, subPriority, secure,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001010 add, excludeLocalRoutes)) {
Ken Chen4ea88462021-05-23 14:56:43 +08001011 return ret;
1012 }
1013 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start,
1014 range.stop, subPriority, add)) {
1015 return ret;
1016 }
1017 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start,
1018 range.stop, subPriority, add)) {
1019 return ret;
1020 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001021 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001022 }
1023
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001024 if (modifyNonUidBasedRules) {
1025 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001026 return ret;
1027 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -07001028 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
1029 return ret;
1030 }
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001031 if (int ret =
1032 modifyVpnSystemPermissionRule(netId, table, secure, add, excludeLocalRoutes)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001033 return ret;
1034 }
Ken Chen4ea88462021-05-23 14:56:43 +08001035 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT,
Patrick Rohre2f1b5a2022-01-25 21:36:50 +01001036 UidRanges::SUB_PRIORITY_HIGHEST, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001037 }
1038
1039 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001040}
1041
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001042int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
1043 Permission permission) {
Chiachang Wangf79e3562022-01-15 13:31:04 +08001044 uint32_t table = getRouteTableForInterface(interface, false /* local */);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001045 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +09001046 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001047 }
1048
Sreeram Ramachandran122f5812014-05-11 20:29:49 -07001049 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -07001050 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001051
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001052 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -07001053 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001054
1055 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -07001056 mask.permission = permission;
1057
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001058 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colitti758627c2018-03-15 01:49:20 +09001059 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001060}
1061
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001062int RouteController::modifyTetheredNetwork(uint16_t action, const char* inputInterface,
1063 const char* outputInterface) {
Chiachang Wangf79e3562022-01-15 13:31:04 +08001064 uint32_t table = getRouteTableForInterface(outputInterface, false /* local */);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001065 if (table == RT_TABLE_UNSPEC) {
1066 return -ESRCH;
1067 }
1068
1069 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
1070 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
1071}
1072
Lorenzo Colitti92e8f962017-09-26 19:13:50 +09001073// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001074// Returns 0 on success or negative errno on failure.
Tyler Wearfa94a272019-12-05 15:01:48 -08001075int RouteController::modifyRoute(uint16_t action, uint16_t flags, const char* interface,
1076 const char* destination, const char* nexthop, TableType tableType,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001077 int mtu, int priority) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001078 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -07001079 switch (tableType) {
1080 case RouteController::INTERFACE: {
Chiachang Wangf79e3562022-01-15 13:31:04 +08001081 table = getRouteTableForInterface(interface, false /* local */);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001082 if (table == RT_TABLE_UNSPEC) {
1083 return -ESRCH;
1084 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -07001085 break;
1086 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001087 case RouteController::LOCAL_NETWORK: {
1088 table = ROUTE_TABLE_LOCAL_NETWORK;
1089 break;
1090 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001091 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001092 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -07001093 break;
1094 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001095 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001096 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -07001097 break;
1098 }
1099 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001100
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001101 int ret = modifyIpRoute(action, flags, table, interface, destination, nexthop, mtu, priority);
Sreeram Ramachandran03213152014-10-30 10:01:07 -07001102 // Trying to add a route that already exists shouldn't cause an error.
1103 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001104 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -07001105 }
1106
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001107 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001108}
1109
Lorenzo Colitti3810c972021-01-06 11:44:02 +09001110static void maybeModifyQdiscClsact(const char* interface, bool add) {
Hungming Chen7f725432020-02-07 17:47:23 +08001111 // The clsact attaching of v4- tun interface is triggered by ClatdController::maybeStartBpf
1112 // because the clat is started before the v4- interface is added to the network and the
1113 // clat startup needs to add {in, e}gress filters.
1114 // TODO: remove this workaround once v4- tun interface clsact attaching is moved out from
1115 // ClatdController::maybeStartBpf.
1116 if (StartsWith(interface, "v4-") && add) return;
1117
1118 // The interface may have already gone away in the delete case.
Chiachang Wangf79e3562022-01-15 13:31:04 +08001119 uint32_t ifindex = RouteController::ifNameToIndexFunction(interface);
Hungming Chen7f725432020-02-07 17:47:23 +08001120 if (!ifindex) {
1121 ALOGE("cannot find interface %s", interface);
1122 return;
1123 }
1124
1125 if (add) {
1126 if (int ret = tcQdiscAddDevClsact(ifindex)) {
1127 ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", ifindex, interface, strerror(-ret));
1128 return;
1129 }
1130 } else {
1131 if (int ret = tcQdiscDelDevClsact(ifindex)) {
1132 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", ifindex, interface, strerror(-ret));
1133 return;
1134 }
1135 }
1136
1137 return;
1138}
1139
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001140[[nodiscard]] static int clearTetheringRules(const char* inputInterface) {
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001141 int ret = 0;
1142 while (ret == 0) {
1143 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
1144 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
1145 }
1146
1147 if (ret == -ENOENT) {
1148 return 0;
1149 } else {
1150 return ret;
1151 }
1152}
1153
Lorenzo Colittif3e299a2017-02-14 17:24:28 +09001154uint32_t getRulePriority(const nlmsghdr *nlh) {
1155 return getRtmU32Attribute(nlh, FRA_PRIORITY);
1156}
1157
1158uint32_t getRouteTable(const nlmsghdr *nlh) {
1159 return getRtmU32Attribute(nlh, RTA_TABLE);
1160}
1161
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001162[[nodiscard]] static int flushRules() {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +09001163 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
1164 // Don't touch rules at priority 0 because by default they are used for local input.
1165 return getRulePriority(nlh) != 0;
1166 };
1167 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
1168}
1169
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001170int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +09001171 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
1172 return getRouteTable(nlh) == table;
1173 };
1174
1175 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
1176}
1177
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001178int RouteController::flushRoutes(const char* interface) {
Chiachang Wangf79e3562022-01-15 13:31:04 +08001179 // Try to flush both local and global routing tables.
1180 //
1181 // Flush local first because flush global routing tables may erase the sInterfaceToTable map.
1182 // Then the fake <iface>_local interface will be unable to find the index because the local
1183 // interface depends physical interface to find the correct index.
1184 int ret = flushRoutes(interface, true);
1185 ret |= flushRoutes(interface, false);
1186 return ret;
1187}
1188
1189// Returns 0 on success or negative errno on failure.
1190int RouteController::flushRoutes(const char* interface, bool local) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +09001191 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001192
Chiachang Wangf79e3562022-01-15 13:31:04 +08001193 uint32_t table = getRouteTableForInterfaceLocked(interface, local);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +09001194 if (table == RT_TABLE_UNSPEC) {
1195 return -ESRCH;
1196 }
1197
1198 int ret = flushRoutes(table);
1199
1200 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
1201 // track of its name.
Chiachang Wangf79e3562022-01-15 13:31:04 +08001202 // Skip erasing local fake interface since it does not exist in sInterfaceToTable.
1203 if (ret == 0 && !local) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001204 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +09001205 }
1206
1207 return ret;
1208}
1209
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001210int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -07001211 if (int ret = flushRules()) {
1212 return ret;
1213 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001214 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001215 return ret;
1216 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001217 if (int ret = addLocalNetworkRules(localNetId)) {
1218 return ret;
1219 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -07001220 if (int ret = addUnreachableRule()) {
1221 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001222 }
Lorenzo Colitti36679362015-02-25 10:26:19 +09001223 // Don't complain if we can't add the dummy network, since not all devices support it.
1224 configureDummyNetwork();
1225
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001226 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001227 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -07001228}
1229
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001230int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Lorenzo Colittib6dc40a2020-03-24 00:58:50 +09001231 if (int ret = modifyLocalNetwork(netId, interface, ACTION_ADD)) {
1232 return ret;
1233 }
1234 std::lock_guard lock(sInterfaceToTableLock);
1235 sInterfaceToTable[interface] = ROUTE_TABLE_LOCAL_NETWORK;
1236 return 0;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001237}
1238
1239int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Lorenzo Colittib6dc40a2020-03-24 00:58:50 +09001240 if (int ret = modifyLocalNetwork(netId, interface, ACTION_DEL)) {
1241 return ret;
1242 }
1243 std::lock_guard lock(sInterfaceToTableLock);
1244 sInterfaceToTable.erase(interface);
1245 return 0;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001246}
1247
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001248int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
Ken Chen8738e1c2020-11-24 11:38:54 +08001249 Permission permission,
Ken Chen4ea88462021-05-23 14:56:43 +08001250 const UidRangeMap& uidRangeMap) {
1251 if (int ret = modifyPhysicalNetwork(netId, interface, uidRangeMap, permission, ACTION_ADD,
Ken Chen8738e1c2020-11-24 11:38:54 +08001252 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001253 return ret;
1254 }
Chiachang Wange7bf5f52022-01-17 11:37:39 +08001255
Hungming Chen7f725432020-02-07 17:47:23 +08001256 maybeModifyQdiscClsact(interface, ACTION_ADD);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001257 updateTableNamesFile();
1258 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001259}
1260
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001261int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
Ken Chen8738e1c2020-11-24 11:38:54 +08001262 Permission permission,
Ken Chen4ea88462021-05-23 14:56:43 +08001263 const UidRangeMap& uidRangeMap) {
1264 if (int ret = modifyPhysicalNetwork(netId, interface, uidRangeMap, permission, ACTION_DEL,
Ken Chen8738e1c2020-11-24 11:38:54 +08001265 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001266 return ret;
1267 }
Chiachang Wange7bf5f52022-01-17 11:37:39 +08001268
Chiachang8a03faa2022-05-04 07:45:37 +00001269 if (int ret = flushRoutes(interface)) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001270 return ret;
1271 }
Chiachang Wange7bf5f52022-01-17 11:37:39 +08001272
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001273 if (int ret = clearTetheringRules(interface)) {
1274 return ret;
1275 }
Chiachang Wange7bf5f52022-01-17 11:37:39 +08001276
Hungming Chen7f725432020-02-07 17:47:23 +08001277 maybeModifyQdiscClsact(interface, ACTION_DEL);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001278 updateTableNamesFile();
1279 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001280}
1281
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001282int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001283 bool secure, const UidRangeMap& uidRangeMap,
1284 bool excludeLocalRoutes) {
Ken Chen4ea88462021-05-23 14:56:43 +08001285 if (int ret = modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_ADD,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001286 MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes)) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001287 return ret;
1288 }
1289 updateTableNamesFile();
1290 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001291}
1292
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001293int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001294 bool secure, const UidRangeMap& uidRangeMap,
1295 bool excludeLocalRoutes) {
Ken Chen4ea88462021-05-23 14:56:43 +08001296 if (int ret = modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_DEL,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001297 MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001298 return ret;
1299 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001300 if (int ret = flushRoutes(interface)) {
1301 return ret;
1302 }
1303 updateTableNamesFile();
1304 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001305}
1306
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001307int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1308 Permission oldPermission,
1309 Permission newPermission) {
Ken Chen4ea88462021-05-23 14:56:43 +08001310 // Physical network rules either use permission bits or UIDs, but not both.
1311 // So permission changes don't affect any UID-based rules.
1312 UidRangeMap emptyUidRangeMap;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001313 // Add the new rules before deleting the old ones, to avoid race conditions.
Ken Chen4ea88462021-05-23 14:56:43 +08001314 if (int ret = modifyPhysicalNetwork(netId, interface, emptyUidRangeMap, newPermission,
1315 ACTION_ADD, MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001316 return ret;
1317 }
Ken Chen4ea88462021-05-23 14:56:43 +08001318 return modifyPhysicalNetwork(netId, interface, emptyUidRangeMap, oldPermission, ACTION_DEL,
Ken Chen8738e1c2020-11-24 11:38:54 +08001319 MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001320}
1321
Robin Leeb8087362016-03-30 18:43:08 +01001322int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1323 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1324}
1325
1326int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1327 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1328}
1329
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001330int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001331 const UidRangeMap& uidRangeMap,
1332 bool excludeLocalRoutes) {
Ken Chen4ea88462021-05-23 14:56:43 +08001333 return modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_ADD,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001334 !MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001335}
1336
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001337int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001338 bool secure, const UidRangeMap& uidRangeMap,
1339 bool excludeLocalRoutes) {
Ken Chen4ea88462021-05-23 14:56:43 +08001340 return modifyVirtualNetwork(netId, interface, uidRangeMap, secure, ACTION_DEL,
Chiachang Wang2e9443f2022-01-14 22:55:36 +08001341 !MODIFY_NON_UID_BASED_RULES, excludeLocalRoutes);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001342}
1343
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001344int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1345 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001346}
1347
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001348int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1349 Permission permission) {
1350 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001351}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001352
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001353int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001354 TableType tableType, int mtu, int priority) {
Tyler Wearfa94a272019-12-05 15:01:48 -08001355 return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_CREATE_FLAGS, interface, destination, nexthop,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001356 tableType, mtu, priority);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001357}
1358
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001359int RouteController::removeRoute(const char* interface, const char* destination,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001360 const char* nexthop, TableType tableType, int priority) {
Tyler Wearfa94a272019-12-05 15:01:48 -08001361 return modifyRoute(RTM_DELROUTE, NETLINK_REQUEST_FLAGS, interface, destination, nexthop,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001362 tableType, 0 /* mtu */, priority);
Tyler Wearfa94a272019-12-05 15:01:48 -08001363}
1364
1365int RouteController::updateRoute(const char* interface, const char* destination,
1366 const char* nexthop, TableType tableType, int mtu) {
1367 return modifyRoute(RTM_NEWROUTE, NETLINK_ROUTE_REPLACE_FLAGS, interface, destination, nexthop,
Taras Antoshchuk0cceda22021-09-24 18:40:03 +02001368 tableType, mtu, 0 /* priority */);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001369}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001370
1371int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001372 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001373}
1374
1375int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001376 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001377}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001378
1379int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1380 Permission permission) {
Chiachang8a03faa2022-05-04 07:45:37 +00001381 if (int ret = modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission)) {
1382 return ret;
1383 }
1384
1385 return modifyVpnLocalExclusionRule(true /* add */, physicalInterface);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001386}
1387
1388int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1389 const char* physicalInterface,
1390 Permission permission) {
Chiachang8a03faa2022-05-04 07:45:37 +00001391 if (int ret = modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission)) {
1392 return ret;
1393 }
1394
1395 return modifyVpnLocalExclusionRule(false /* add */, physicalInterface);
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001396}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001397
Ken Chen8738e1c2020-11-24 11:38:54 +08001398int RouteController::addUsersToPhysicalNetwork(unsigned netId, const char* interface,
Ken Chen4ea88462021-05-23 14:56:43 +08001399 const UidRangeMap& uidRangeMap) {
1400 return modifyPhysicalNetwork(netId, interface, uidRangeMap, PERMISSION_NONE, ACTION_ADD,
Ken Chen8738e1c2020-11-24 11:38:54 +08001401 !MODIFY_NON_UID_BASED_RULES);
1402}
1403
1404int RouteController::removeUsersFromPhysicalNetwork(unsigned netId, const char* interface,
Ken Chen4ea88462021-05-23 14:56:43 +08001405 const UidRangeMap& uidRangeMap) {
1406 return modifyPhysicalNetwork(netId, interface, uidRangeMap, PERMISSION_NONE, ACTION_DEL,
Ken Chen8738e1c2020-11-24 11:38:54 +08001407 !MODIFY_NON_UID_BASED_RULES);
1408}
1409
Ken Chen4ea88462021-05-23 14:56:43 +08001410int RouteController::addUsersToUnreachableNetwork(unsigned netId, const UidRangeMap& uidRangeMap) {
1411 return modifyUnreachableNetwork(netId, uidRangeMap, ACTION_ADD);
Ken Chen4e8ef9b2021-03-17 01:57:19 +08001412}
1413
Ken Chen4ea88462021-05-23 14:56:43 +08001414int RouteController::removeUsersFromUnreachableNetwork(unsigned netId,
1415 const UidRangeMap& uidRangeMap) {
1416 return modifyUnreachableNetwork(netId, uidRangeMap, ACTION_DEL);
Ken Chen4e8ef9b2021-03-17 01:57:19 +08001417}
1418
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001419// Protects sInterfaceToTable.
Luke Huang534980c2018-07-06 17:50:11 +08001420std::mutex RouteController::sInterfaceToTableLock;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001421std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1422
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001423} // namespace android::net