blob: a62ed6b5ec5f749f4edf07b09591ff12d22a726e [file] [log] [blame]
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "RouteController.h"
18
Dan Albertaa1be2b2015-01-06 09:36:17 -080019#include <arpa/inet.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <linux/fib_rules.h>
23#include <net/if.h>
24#include <sys/stat.h>
25
Elliott Hughesbd378322015-02-04 13:25:14 -080026#include <private/android_filesystem_config.h>
27
Dan Albertaa1be2b2015-01-06 09:36:17 -080028#include <map>
29
Luke Huang94658ac2018-10-18 19:35:12 +090030#define LOG_TAG "Netd"
31
Lorenzo Colitti7035f222017-02-13 18:29:00 +090032#include "DummyNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070033#include "Fwmark.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090034#include "NetdConstants.h"
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090035#include "NetlinkCommands.h"
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070036#include "UidRanges.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070037
Bernie Innocenti189eb502018-10-01 23:10:18 +090038#include <android-base/file.h>
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090039#include <android-base/stringprintf.h>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070040#include "log/log.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090041#include "netid_client.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090042#include "netutils/ifc.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070043
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090044using android::base::StringPrintf;
Dan Albert5407e142015-03-16 10:05:59 -070045using android::base::WriteStringToFile;
Luke Huang94658ac2018-10-18 19:35:12 +090046using android::net::UidRangeParcel;
Dan Albert5407e142015-03-16 10:05:59 -070047
Bernie Innocenti762dcf42019-06-14 19:52:49 +090048namespace android::net {
Lorenzo Colitti7035f222017-02-13 18:29:00 +090049
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090050auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand;
51
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070052// BEGIN CONSTANTS --------------------------------------------------------------------------------
53
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070054const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090055const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070056const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070057const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010058const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070059const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
60const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
61const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
62const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070063const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070064const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070065const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070066const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070067const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070068const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090069const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070070
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070071const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070072const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
73const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
74
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070075const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070076const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
77const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
78
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070079const char* const ROUTE_TABLE_NAME_LOCAL = "local";
80const char* const ROUTE_TABLE_NAME_MAIN = "main";
81
Lorenzo Colittib5d19e92017-09-25 18:39:33 +090082// None of our regular routes specify priority, which causes them to have the default priority.
83// For default throw routes, we use a fixed priority of 100000.
Lorenzo Colitti5e03a892017-09-08 11:31:59 +090084uint32_t PRIO_THROW = 100000;
85
Lorenzo Colittid78843e2017-03-27 05:52:31 +090086const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
87
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070088const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
89
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070090const uid_t UID_ROOT = 0;
Lorenzo Colitti3093f562017-09-25 14:17:38 +090091const uint32_t FWMARK_NONE = 0;
92const uint32_t MASK_NONE = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090093const char* const IIF_LOOPBACK = "lo";
Yi Kongbdfd57e2018-07-25 13:26:10 -070094const char* const IIF_NONE = nullptr;
95const char* const OIF_NONE = nullptr;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070096const bool ACTION_ADD = true;
97const bool ACTION_DEL = false;
98const bool MODIFY_NON_UID_BASED_RULES = true;
99
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700100const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700101const mode_t RT_TABLES_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // mode 0644, rw-r--r--
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700102
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700103// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
104// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900105static constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700106 return RTA_LENGTH(x);
107}
108
109// These are practically const, but can't be declared so, because they are used to initialize
110// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900111rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
112rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
113rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
114rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900115rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700116
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900117rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
118rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900119rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700120
121uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
122
123// END CONSTANTS ----------------------------------------------------------------------------------
124
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900125static const char* actionName(uint16_t action) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900126 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
127 return ops[action % 4];
128}
129
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900130static const char* familyName(uint8_t family) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900131 switch (family) {
132 case AF_INET: return "IPv4";
133 case AF_INET6: return "IPv6";
134 default: return "???";
135 }
136}
137
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900138// Caller must hold sInterfaceToTableLock.
139uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface) {
mtk137999f2913e2019-02-25 19:39:36 +0800140 // If we already know the routing table for this interface name, use it.
141 // This ensures we can remove rules and routes for an interface that has been removed,
142 // or has been removed and re-added with a different interface index.
143 //
144 // The caller is responsible for ensuring that an interface is never added to a network
145 // until it has been removed from any network it was previously in. This ensures that
146 // if the same interface disconnects and then reconnects with a different interface ID
147 // when the reconnect happens the interface will not be in the map, and the code will
148 // determine the new routing table from the interface ID, below.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900149 auto iter = sInterfaceToTable.find(interface);
mtk137999f2913e2019-02-25 19:39:36 +0800150 if (iter != sInterfaceToTable.end()) {
151 return iter->second;
152 }
153
154 uint32_t index = if_nametoindex(interface);
155 if (index == 0) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700156 ALOGE("cannot find interface %s", interface);
157 return RT_TABLE_UNSPEC;
158 }
mtk137999f2913e2019-02-25 19:39:36 +0800159 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
160 sInterfaceToTable[interface] = index;
161 return index;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700162}
163
Rubin Xu6c00b612018-04-27 14:27:59 +0100164uint32_t RouteController::getIfIndex(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900165 std::lock_guard lock(sInterfaceToTableLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100166
167 auto iter = sInterfaceToTable.find(interface);
168 if (iter == sInterfaceToTable.end()) {
169 ALOGE("getIfIndex: cannot find interface %s", interface);
170 return 0;
171 }
172
173 return iter->second - ROUTE_TABLE_OFFSET_FROM_INDEX;
174}
175
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900176uint32_t RouteController::getRouteTableForInterface(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900177 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900178 return getRouteTableForInterfaceLocked(interface);
179}
180
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700181void addTableName(uint32_t table, const std::string& name, std::string* contents) {
182 char tableString[UINT32_STRLEN];
183 snprintf(tableString, sizeof(tableString), "%u", table);
184 *contents += tableString;
185 *contents += " ";
186 *contents += name;
187 *contents += "\n";
188}
189
190// 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 +0900191void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700192 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700193
194 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
195 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
196
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700197 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700198 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
199 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700200
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900201 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900202 for (const auto& entry : sInterfaceToTable) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700203 addTableName(entry.second, entry.first, &contents);
204 }
205
Dan Albert5407e142015-03-16 10:05:59 -0700206 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800207 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700208 return;
209 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700210}
211
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700212// Returns 0 on success or negative errno on failure.
213int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
214 if (!input) {
215 *length = 0;
216 *padding = 0;
217 return 0;
218 }
219 *length = strlcpy(name, input, IFNAMSIZ) + 1;
220 if (*length > IFNAMSIZ) {
221 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
222 return -ENAMETOOLONG;
223 }
224 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
225 return 0;
226}
227
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700228// Adds or removes a routing rule for IPv4 and IPv6.
229//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900230// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100231// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700232// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
233// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700234// + If |iif| is non-NULL, the rule matches the specified incoming interface.
235// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
236// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
237// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900238//
239// Returns 0 on success or negative errno on failure.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900240[[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
241 uint32_t table, uint32_t fwmark, uint32_t mask,
242 const char* iif, const char* oif, uid_t uidStart,
243 uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700244 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
245 if (fwmark & ~mask) {
246 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
247 return -ERANGE;
248 }
249
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700250 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900251 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700252 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
253 size_t iifLength, oifLength;
254 uint16_t iifPadding, oifPadding;
255 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
256 return ret;
257 }
258 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
259 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900260 }
261
Lorenzo Colitti59656512014-06-25 03:20:29 +0900262 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700263 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700264 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900265 return -EUSERS;
266 }
Robin Lee4ef94642016-04-01 11:50:49 +0100267
Lorenzo Colitti72723682014-06-26 13:51:10 +0900268 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900269
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900270 // Assemble a rule request and put it in an array of iovec structures.
271 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100272 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900273 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
274 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900275 };
276
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900277 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
278 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
279 // table 0, it's a wildcard that matches anything.
280 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
281 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
282 return -ENOTUNIQ;
283 }
284
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700285 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
286 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900287 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900288
289 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700290 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700291 { &rule, sizeof(rule) },
292 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
293 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700294 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
295 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700296 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
297 { &fwmark, mask ? sizeof(fwmark) : 0 },
298 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
299 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900300 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
301 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700302 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
303 { iifName, iifLength },
304 { PADDING_BUFFER, iifPadding },
305 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
306 { oifName, oifLength },
307 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900308 };
309
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900310 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700311 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
312 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900313 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900314 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
315 // Don't log when deleting a tethering rule that's not there. This matches the
316 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
317 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
318 strerror(-ret));
319 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900320 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700321 }
322 }
323
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900324 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700325}
326
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900327[[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
328 uint32_t fwmark, uint32_t mask, const char* iif,
329 const char* oif, uid_t uidStart, uid_t uidEnd) {
Robin Lee4ef94642016-04-01 11:50:49 +0100330 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
331 uidEnd);
332}
333
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900334[[nodiscard]] static int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
335 uint32_t fwmark, uint32_t mask) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700336 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
337 INVALID_UID);
338}
339
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900340// Adds or deletes an IPv4 or IPv6 route.
341// Returns 0 on success or negative errno on failure.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900342int modifyIpRoute(uint16_t action, uint32_t table, const char* interface, const char* destination,
343 const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900344 // At least the destination must be non-null.
345 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700346 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900347 return -EFAULT;
348 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700349
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900350 // Parse the prefix.
351 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700352 uint8_t family;
353 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900354 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
355 &prefixLength);
356 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700357 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900358 return rawLength;
359 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700360
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900361 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700362 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900363 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
364 }
365
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700366 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900367 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900368 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700369
370 if (nexthop && !strcmp(nexthop, "unreachable")) {
371 type = RTN_UNREACHABLE;
372 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
373 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
374 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
375 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700376 nexthop = nullptr;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900377 } else if (nexthop && !strcmp(nexthop, "throw")) {
378 type = RTN_THROW;
379 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700380 nexthop = nullptr;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700381 } else {
382 // If an interface was specified, find the ifindex.
383 if (interface != OIF_NONE) {
384 ifindex = if_nametoindex(interface);
385 if (!ifindex) {
386 ALOGE("cannot find interface %s", interface);
387 return -ENODEV;
388 }
389 }
390
391 // If a nexthop was specified, parse it as the same family as the prefix.
392 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
393 ALOGE("inet_pton failed for nexthop %s", nexthop);
394 return -EINVAL;
395 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900396 }
397
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900398 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
399
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900400 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700401 rtmsg route = {
Nick Desaulniers6b357502019-10-11 09:26:44 -0700402 .rtm_family = family,
403 .rtm_dst_len = prefixLength,
404 .rtm_protocol = RTPROT_STATIC,
405 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
406 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900407 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900408
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700409 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
410 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900411
412 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700413 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700414 { &route, sizeof(route) },
415 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
416 { &table, sizeof(table) },
417 { &rtaDst, sizeof(rtaDst) },
418 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700419 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
420 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700421 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
422 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900423 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
424 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900425 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900426
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900427 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_ROUTE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Jimmy Chence9e5782019-03-08 16:12:55 +0800428
429 // Allow creating multiple link-local routes in the same table, so we can make IPv6
430 // work on all interfaces in the local_network table.
431 if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) {
432 flags &= ~NLM_F_EXCL;
433 }
434
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900435 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900436 if (ret) {
437 ALOGE("Error %s route %s -> %s %s to table %u: %s",
438 actionName(action), destination, nexthop, interface, table, strerror(-ret));
439 }
440 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700441}
442
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700443// An iptables rule to mark incoming packets on a network with the netId of the network.
444//
445// This is so that the kernel can:
446// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
447// replies, SYN-ACKs, etc).
448// + Mark sockets that accept connections from this interface so that the connection stays on the
449// same interface.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900450int modifyIncomingPacketMark(unsigned netId, const char* interface, Permission permission,
451 bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700452 Fwmark fwmark;
453
454 fwmark.netId = netId;
455 fwmark.explicitlySelected = true;
456 fwmark.protectedFromVpn = true;
457 fwmark.permission = permission;
458
Benedict Wongb9baf262017-12-03 15:43:08 -0800459 const uint32_t mask = ~Fwmark::getUidBillingMask();
460
461 std::string cmd = StringPrintf(
462 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
463 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900464 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700465 ALOGE("failed to change iptables rule that sets incoming packet mark");
466 return -EREMOTEIO;
467 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700468
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700469 return 0;
470}
471
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700472// A rule to route responses to the local network forwarded via the VPN.
473//
474// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
475// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900476[[nodiscard]] static int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700477 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
478 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
479 INVALID_UID, INVALID_UID);
480}
481
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700482// A rule to route all traffic from a given set of UIDs to go over the VPN.
483//
484// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
485// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
486// bypass the VPN if the protectedFromVpn bit is set.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900487[[nodiscard]] static int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
488 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700489 Fwmark fwmark;
490 Fwmark mask;
491
492 fwmark.protectedFromVpn = false;
493 mask.protectedFromVpn = true;
494
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700495 uint32_t priority;
496
497 if (secure) {
498 priority = RULE_PRIORITY_SECURE_VPN;
499 } else {
500 priority = RULE_PRIORITY_BYPASSABLE_VPN;
501
502 fwmark.explicitlySelected = false;
503 mask.explicitlySelected = true;
504 }
505
506 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900507 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700508}
509
510// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
511// set of UIDs.
512//
513// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
514// target set, but where the DnsProxyListener itself is not.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900515[[nodiscard]] static int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
516 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700517 Fwmark fwmark;
518 Fwmark mask;
519
520 fwmark.netId = netId;
521 mask.netId = FWMARK_NET_ID_MASK;
522
523 fwmark.permission = PERMISSION_SYSTEM;
524 mask.permission = PERMISSION_SYSTEM;
525
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700526 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
527
528 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
529 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700530}
531
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700532// A rule to route traffic based on an explicitly chosen network.
533//
534// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
535//
536// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
537// to check it again in the rules here, because a network's permissions may have been updated via
538// modifyNetworkPermission().
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900539[[nodiscard]] static int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
540 Permission permission, uid_t uidStart,
541 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700542 Fwmark fwmark;
543 Fwmark mask;
544
545 fwmark.netId = netId;
546 mask.netId = FWMARK_NET_ID_MASK;
547
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700548 fwmark.explicitlySelected = true;
549 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700550
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700551 fwmark.permission = permission;
552 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700553
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700554 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900555 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700556}
557
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700558// A rule to route traffic based on a chosen outgoing interface.
559//
560// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
561// the outgoing interface (typically for link-local communications).
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900562[[nodiscard]] static int modifyOutputInterfaceRules(const char* interface, uint32_t table,
563 Permission permission, uid_t uidStart,
564 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700565 Fwmark fwmark;
566 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700567
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700568 fwmark.permission = permission;
569 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700570
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900571 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900572 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
573 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900574 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
575 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900576 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900577 UID_ROOT, UID_ROOT)) {
578 return ret;
579 }
580 }
581
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700582 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900583 fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700584}
585
586// A rule to route traffic based on the chosen network.
587//
588// This is for sockets that have not explicitly requested a particular network, but have been
589// bound to one when they called connect(). This ensures that sockets connected on a particular
590// network stay on that network even if the default network changes.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900591[[nodiscard]] static int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700592 Fwmark fwmark;
593 Fwmark mask;
594
595 fwmark.netId = netId;
596 mask.netId = FWMARK_NET_ID_MASK;
597
598 fwmark.explicitlySelected = false;
599 mask.explicitlySelected = true;
600
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900601 fwmark.permission = PERMISSION_NONE;
602 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700603
604 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900605 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
606 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700607}
608
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700609// A rule to enable split tunnel VPNs.
610//
611// 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 +0800612// go over the default network, provided it has the permissions required by the default network.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900613int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
614 const char* physicalInterface,
615 Permission permission) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700616 uint32_t table = getRouteTableForInterface(physicalInterface);
617 if (table == RT_TABLE_UNSPEC) {
618 return -ESRCH;
619 }
620
621 Fwmark fwmark;
622 Fwmark mask;
623
624 fwmark.netId = vpnNetId;
625 mask.netId = FWMARK_NET_ID_MASK;
626
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700627 fwmark.permission = permission;
628 mask.permission = permission;
629
630 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
631 mask.intValue);
632}
633
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700634// Add rules to allow legacy routes added through the requestRouteToHost() API.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900635[[nodiscard]] static int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700636 Fwmark fwmark;
637 Fwmark mask;
638
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700639 fwmark.explicitlySelected = false;
640 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700641
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700642 // Rules to allow legacy routes to override the default network.
643 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
644 fwmark.intValue, mask.intValue)) {
645 return ret;
646 }
647 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
648 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
649 return ret;
650 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700651
652 fwmark.permission = PERMISSION_SYSTEM;
653 mask.permission = PERMISSION_SYSTEM;
654
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700655 // A rule to allow legacy routes from system apps to override VPNs.
656 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700657 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700658}
659
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700660// Add rules to lookup the local network when specified explicitly or otherwise.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900661[[nodiscard]] static int addLocalNetworkRules(unsigned localNetId) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700662 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
663 INVALID_UID, INVALID_UID, ACTION_ADD)) {
664 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700665 }
666
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700667 Fwmark fwmark;
668 Fwmark mask;
669
670 fwmark.explicitlySelected = false;
671 mask.explicitlySelected = true;
672
673 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
674 fwmark.intValue, mask.intValue);
675}
676
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900677/* static */
678int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900679 const char *interface = DummyNetwork::INTERFACE_NAME;
680 uint32_t table = getRouteTableForInterface(interface);
681 if (table == RT_TABLE_UNSPEC) {
682 // getRouteTableForInterface has already looged an error.
683 return -ESRCH;
684 }
685
686 ifc_init();
687 int ret = ifc_up(interface);
688 ifc_close();
689 if (ret) {
690 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
691 return -errno;
692 }
693
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900694 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
695 INVALID_UID, INVALID_UID, ACTION_ADD))) {
696 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900697 return ret;
698 }
699
Yi Kongbdfd57e2018-07-25 13:26:10 -0700700 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", nullptr))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900701 return ret;
702 }
703
Yi Kongbdfd57e2018-07-25 13:26:10 -0700704 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", nullptr))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900705 return ret;
706 }
707
708 return 0;
709}
710
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900711// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
712// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
713// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
714// rule will hopefully make things even clearer.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900715[[nodiscard]] static int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100716 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
717 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700718}
719
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900720[[nodiscard]] static int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700721 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
722 return ret;
723 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900724 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
725 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700726}
727
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900728/* static */
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900729int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
730 Permission permission, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700731 uint32_t table = getRouteTableForInterface(interface);
732 if (table == RT_TABLE_UNSPEC) {
733 return -ESRCH;
734 }
735
736 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
737 return ret;
738 }
739 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
740 add)) {
741 return ret;
742 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900743 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700744 add)) {
745 return ret;
746 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900747
748 // Only set implicit rules for networks that don't require permissions.
749 //
750 // This is so that if the default network ceases to be the default network and then switches
751 // from requiring no permissions to requiring permissions, we ensure that apps only use the
752 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
753 // - it closes all sockets on the network except sockets that are explicitly selected.
754 //
755 // The lack of this rule only affects the special case above, because:
756 // - The only cases where we implicitly bind a socket to a network are the default network and
757 // the bypassable VPN that applies to the app, if any.
758 // - This rule doesn't affect VPNs because they don't support permissions at all.
759 // - The default network doesn't require permissions. While we support doing this, the framework
760 // never does it (partly because we'd end up in the situation where we tell apps that there is
761 // a default network, but they can't use it).
762 // - If the network is still the default network, the presence or absence of this rule does not
763 // matter.
764 //
765 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
766 // implicitly bound to a network because at the time of connect() it was the default, and that
767 // network must no longer be the default, and must now require permissions.
768 if (permission == PERMISSION_NONE) {
769 return modifyImplicitNetworkRule(netId, table, add);
770 }
771 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700772}
773
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900774[[nodiscard]] static int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
Robin Leeb8087362016-03-30 18:43:08 +0100775 Fwmark fwmark;
776 Fwmark mask;
777 fwmark.protectedFromVpn = false;
778 mask.protectedFromVpn = true;
779
Luke Huang94658ac2018-10-18 19:35:12 +0900780 for (const UidRangeParcel& range : uidRanges.getRanges()) {
781 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN,
782 FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
783 IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) {
Robin Leeb8087362016-03-30 18:43:08 +0100784 return ret;
785 }
786 }
787
788 return 0;
789}
790
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900791int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
792 const UidRanges& uidRanges, bool secure, bool add,
793 bool modifyNonUidBasedRules) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700794 uint32_t table = getRouteTableForInterface(interface);
795 if (table == RT_TABLE_UNSPEC) {
796 return -ESRCH;
797 }
798
Luke Huang94658ac2018-10-18 19:35:12 +0900799 for (const UidRangeParcel& range : uidRanges.getRanges()) {
800 if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, secure, add)) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700801 return ret;
802 }
Luke Huang94658ac2018-10-18 19:35:12 +0900803 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start,
804 range.stop, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700805 return ret;
806 }
Luke Huang94658ac2018-10-18 19:35:12 +0900807 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start,
808 range.stop, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700809 return ret;
810 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700811 }
812
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700813 if (modifyNonUidBasedRules) {
814 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700815 return ret;
816 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700817 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
818 return ret;
819 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700820 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700821 return ret;
822 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700823 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700824 }
825
826 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700827}
828
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900829int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
830 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700831 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700832 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900833 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700834 }
835
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700836 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700837 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700838
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700839 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700840 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700841
842 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700843 mask.permission = permission;
844
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700845 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900846 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700847}
848
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900849int RouteController::modifyTetheredNetwork(uint16_t action, const char* inputInterface,
850 const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700851 uint32_t table = getRouteTableForInterface(outputInterface);
852 if (table == RT_TABLE_UNSPEC) {
853 return -ESRCH;
854 }
855
856 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
857 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
858}
859
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900860// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900861// Returns 0 on success or negative errno on failure.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900862int RouteController::modifyRoute(uint16_t action, const char* interface, const char* destination,
863 const char* nexthop, TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700864 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700865 switch (tableType) {
866 case RouteController::INTERFACE: {
867 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700868 if (table == RT_TABLE_UNSPEC) {
869 return -ESRCH;
870 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700871 break;
872 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700873 case RouteController::LOCAL_NETWORK: {
874 table = ROUTE_TABLE_LOCAL_NETWORK;
875 break;
876 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700877 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700878 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700879 break;
880 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700881 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700882 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700883 break;
884 }
885 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700886
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900887 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700888 // Trying to add a route that already exists shouldn't cause an error.
889 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900890 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700891 }
892
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900893 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700894}
895
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900896[[nodiscard]] static int clearTetheringRules(const char* inputInterface) {
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900897 int ret = 0;
898 while (ret == 0) {
899 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
900 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
901 }
902
903 if (ret == -ENOENT) {
904 return 0;
905 } else {
906 return ret;
907 }
908}
909
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900910uint32_t getRulePriority(const nlmsghdr *nlh) {
911 return getRtmU32Attribute(nlh, FRA_PRIORITY);
912}
913
914uint32_t getRouteTable(const nlmsghdr *nlh) {
915 return getRtmU32Attribute(nlh, RTA_TABLE);
916}
917
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900918[[nodiscard]] static int flushRules() {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900919 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
920 // Don't touch rules at priority 0 because by default they are used for local input.
921 return getRulePriority(nlh) != 0;
922 };
923 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
924}
925
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900926int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900927 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
928 return getRouteTable(nlh) == table;
929 };
930
931 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
932}
933
934// Returns 0 on success or negative errno on failure.
Bernie Innocenti762dcf42019-06-14 19:52:49 +0900935int RouteController::flushRoutes(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900936 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900937
938 uint32_t table = getRouteTableForInterfaceLocked(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900939 if (table == RT_TABLE_UNSPEC) {
940 return -ESRCH;
941 }
942
943 int ret = flushRoutes(table);
944
945 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
946 // track of its name.
947 if (ret == 0) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900948 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900949 }
950
951 return ret;
952}
953
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700954int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700955 if (int ret = flushRules()) {
956 return ret;
957 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700958 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700959 return ret;
960 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700961 if (int ret = addLocalNetworkRules(localNetId)) {
962 return ret;
963 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700964 if (int ret = addUnreachableRule()) {
965 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700966 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900967 // Don't complain if we can't add the dummy network, since not all devices support it.
968 configureDummyNetwork();
969
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700970 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700971 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700972}
973
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700974int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700975 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700976}
977
978int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700979 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700980}
981
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700982int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
983 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700984 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
985 return ret;
986 }
987 updateTableNamesFile();
988 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700989}
990
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700991int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
992 Permission permission) {
993 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700994 return ret;
995 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700996 if (int ret = flushRoutes(interface)) {
997 return ret;
998 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900999 if (int ret = clearTetheringRules(interface)) {
1000 return ret;
1001 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001002 updateTableNamesFile();
1003 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001004}
1005
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001006int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001007 bool secure, const UidRanges& uidRanges) {
1008 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001009 MODIFY_NON_UID_BASED_RULES)) {
1010 return ret;
1011 }
1012 updateTableNamesFile();
1013 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001014}
1015
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001016int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001017 bool secure, const UidRanges& uidRanges) {
1018 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001019 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001020 return ret;
1021 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001022 if (int ret = flushRoutes(interface)) {
1023 return ret;
1024 }
1025 updateTableNamesFile();
1026 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001027}
1028
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001029int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1030 Permission oldPermission,
1031 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001032 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001033 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001034 return ret;
1035 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001036 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001037}
1038
Robin Leeb8087362016-03-30 18:43:08 +01001039int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1040 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1041}
1042
1043int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1044 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1045}
1046
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001047int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001048 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001049 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001050 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001051}
1052
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001053int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001054 bool secure, const UidRanges& uidRanges) {
1055 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001056 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001057}
1058
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001059int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1060 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001061}
1062
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001063int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1064 Permission permission) {
1065 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001066}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001067
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001068int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001069 TableType tableType) {
1070 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001071}
1072
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001073int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001074 const char* nexthop, TableType tableType) {
1075 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001076}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001077
1078int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001079 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001080}
1081
1082int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001083 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001084}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001085
1086int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1087 Permission permission) {
1088 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1089}
1090
1091int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1092 const char* physicalInterface,
1093 Permission permission) {
1094 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1095}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001096
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001097// Protects sInterfaceToTable.
Luke Huang534980c2018-07-06 17:50:11 +08001098std::mutex RouteController::sInterfaceToTableLock;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001099std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1100
Bernie Innocenti762dcf42019-06-14 19:52:49 +09001101} // namespace android::net