blob: d993c85c525c73a2477e86b706b31c778d3d43c1 [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"
41#include "logwrap/logwrap.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090042#include "netid_client.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090043#include "netutils/ifc.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070044
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090045using android::base::StringPrintf;
Dan Albert5407e142015-03-16 10:05:59 -070046using android::base::WriteStringToFile;
Luke Huang94658ac2018-10-18 19:35:12 +090047using android::net::UidRangeParcel;
Dan Albert5407e142015-03-16 10:05:59 -070048
Lorenzo Colitti7035f222017-02-13 18:29:00 +090049namespace android {
50namespace net {
51
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090052auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand;
53
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070054// BEGIN CONSTANTS --------------------------------------------------------------------------------
55
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070056const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090057const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070058const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070059const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010060const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070061const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
62const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
63const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
64const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070065const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070066const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070067const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070068const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070069const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070070const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090071const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070072
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070073const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070074const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
75const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
76
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070077const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070078const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
79const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
80
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070081const char* const ROUTE_TABLE_NAME_LOCAL = "local";
82const char* const ROUTE_TABLE_NAME_MAIN = "main";
83
Lorenzo Colittib5d19e92017-09-25 18:39:33 +090084// None of our regular routes specify priority, which causes them to have the default priority.
85// For default throw routes, we use a fixed priority of 100000.
Lorenzo Colitti5e03a892017-09-08 11:31:59 +090086uint32_t PRIO_THROW = 100000;
87
Lorenzo Colittid78843e2017-03-27 05:52:31 +090088const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
89
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070090const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
91
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070092const uid_t UID_ROOT = 0;
Lorenzo Colitti3093f562017-09-25 14:17:38 +090093const uint32_t FWMARK_NONE = 0;
94const uint32_t MASK_NONE = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090095const char* const IIF_LOOPBACK = "lo";
Yi Kongbdfd57e2018-07-25 13:26:10 -070096const char* const IIF_NONE = nullptr;
97const char* const OIF_NONE = nullptr;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070098const bool ACTION_ADD = true;
99const bool ACTION_DEL = false;
100const bool MODIFY_NON_UID_BASED_RULES = true;
101
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700102const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700103const 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 -0700104
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700105// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
106// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
107constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
108 return RTA_LENGTH(x);
109}
110
111// These are practically const, but can't be declared so, because they are used to initialize
112// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900113rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
114rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
115rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
116rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900117rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700118
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900119rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
120rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900121rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700122
123uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
124
125// END CONSTANTS ----------------------------------------------------------------------------------
126
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900127const char *actionName(uint16_t action) {
128 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
129 return ops[action % 4];
130}
131
132const char *familyName(uint8_t family) {
133 switch (family) {
134 case AF_INET: return "IPv4";
135 case AF_INET6: return "IPv6";
136 default: return "???";
137 }
138}
139
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900140// Caller must hold sInterfaceToTableLock.
141uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface) {
mtk137999f2913e2019-02-25 19:39:36 +0800142 // If we already know the routing table for this interface name, use it.
143 // This ensures we can remove rules and routes for an interface that has been removed,
144 // or has been removed and re-added with a different interface index.
145 //
146 // The caller is responsible for ensuring that an interface is never added to a network
147 // until it has been removed from any network it was previously in. This ensures that
148 // if the same interface disconnects and then reconnects with a different interface ID
149 // when the reconnect happens the interface will not be in the map, and the code will
150 // determine the new routing table from the interface ID, below.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900151 auto iter = sInterfaceToTable.find(interface);
mtk137999f2913e2019-02-25 19:39:36 +0800152 if (iter != sInterfaceToTable.end()) {
153 return iter->second;
154 }
155
156 uint32_t index = if_nametoindex(interface);
157 if (index == 0) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700158 ALOGE("cannot find interface %s", interface);
159 return RT_TABLE_UNSPEC;
160 }
mtk137999f2913e2019-02-25 19:39:36 +0800161 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
162 sInterfaceToTable[interface] = index;
163 return index;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700164}
165
Rubin Xu6c00b612018-04-27 14:27:59 +0100166uint32_t RouteController::getIfIndex(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900167 std::lock_guard lock(sInterfaceToTableLock);
Rubin Xu6c00b612018-04-27 14:27:59 +0100168
169 auto iter = sInterfaceToTable.find(interface);
170 if (iter == sInterfaceToTable.end()) {
171 ALOGE("getIfIndex: cannot find interface %s", interface);
172 return 0;
173 }
174
175 return iter->second - ROUTE_TABLE_OFFSET_FROM_INDEX;
176}
177
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900178uint32_t RouteController::getRouteTableForInterface(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900179 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900180 return getRouteTableForInterfaceLocked(interface);
181}
182
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700183void addTableName(uint32_t table, const std::string& name, std::string* contents) {
184 char tableString[UINT32_STRLEN];
185 snprintf(tableString, sizeof(tableString), "%u", table);
186 *contents += tableString;
187 *contents += " ";
188 *contents += name;
189 *contents += "\n";
190}
191
192// 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 +0900193void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700194 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700195
196 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
197 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
198
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700199 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700200 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
201 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700202
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900203 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900204 for (const auto& entry : sInterfaceToTable) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700205 addTableName(entry.second, entry.first, &contents);
206 }
207
Dan Albert5407e142015-03-16 10:05:59 -0700208 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800209 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700210 return;
211 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700212}
213
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700214// Returns 0 on success or negative errno on failure.
215int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
216 if (!input) {
217 *length = 0;
218 *padding = 0;
219 return 0;
220 }
221 *length = strlcpy(name, input, IFNAMSIZ) + 1;
222 if (*length > IFNAMSIZ) {
223 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
224 return -ENAMETOOLONG;
225 }
226 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
227 return 0;
228}
229
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700230// Adds or removes a routing rule for IPv4 and IPv6.
231//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900232// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100233// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700234// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
235// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700236// + If |iif| is non-NULL, the rule matches the specified incoming interface.
237// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
238// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
239// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900240//
241// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100242WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
243 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700244 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700245 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
246 if (fwmark & ~mask) {
247 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
248 return -ERANGE;
249 }
250
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700251 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900252 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700253 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
254 size_t iifLength, oifLength;
255 uint16_t iifPadding, oifPadding;
256 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
257 return ret;
258 }
259 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
260 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900261 }
262
Lorenzo Colitti59656512014-06-25 03:20:29 +0900263 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700264 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700265 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900266 return -EUSERS;
267 }
Robin Lee4ef94642016-04-01 11:50:49 +0100268
Lorenzo Colitti72723682014-06-26 13:51:10 +0900269 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900270
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900271 // Assemble a rule request and put it in an array of iovec structures.
272 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100273 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900274 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
275 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900276 };
277
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900278 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
279 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
280 // table 0, it's a wildcard that matches anything.
281 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
282 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
283 return -ENOTUNIQ;
284 }
285
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700286 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
287 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900288 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900289
290 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700291 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700292 { &rule, sizeof(rule) },
293 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
294 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700295 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
296 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700297 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
298 { &fwmark, mask ? sizeof(fwmark) : 0 },
299 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
300 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900301 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
302 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700303 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
304 { iifName, iifLength },
305 { PADDING_BUFFER, iifPadding },
306 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
307 { oifName, oifLength },
308 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900309 };
310
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900311 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700312 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
313 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900314 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900315 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
316 // Don't log when deleting a tethering rule that's not there. This matches the
317 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
318 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
319 strerror(-ret));
320 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900321 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700322 }
323 }
324
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900325 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700326}
327
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700328WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100329 uint32_t fwmark, uint32_t mask, const char* iif,
330 const char* oif, uid_t uidStart, uid_t uidEnd) {
331 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
332 uidEnd);
333}
334
335WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700336 uint32_t fwmark, uint32_t mask) {
337 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
338 INVALID_UID);
339}
340
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900341// Adds or deletes an IPv4 or IPv6 route.
342// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700343WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
344 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900345 // At least the destination must be non-null.
346 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700347 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900348 return -EFAULT;
349 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700350
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900351 // Parse the prefix.
352 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700353 uint8_t family;
354 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900355 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
356 &prefixLength);
357 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700358 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900359 return rawLength;
360 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700361
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900362 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700363 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900364 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
365 }
366
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700367 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900368 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900369 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700370
371 if (nexthop && !strcmp(nexthop, "unreachable")) {
372 type = RTN_UNREACHABLE;
373 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
374 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
375 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
376 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700377 nexthop = nullptr;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900378 } else if (nexthop && !strcmp(nexthop, "throw")) {
379 type = RTN_THROW;
380 interface = OIF_NONE;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700381 nexthop = nullptr;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700382 } else {
383 // If an interface was specified, find the ifindex.
384 if (interface != OIF_NONE) {
385 ifindex = if_nametoindex(interface);
386 if (!ifindex) {
387 ALOGE("cannot find interface %s", interface);
388 return -ENODEV;
389 }
390 }
391
392 // If a nexthop was specified, parse it as the same family as the prefix.
393 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
394 ALOGE("inet_pton failed for nexthop %s", nexthop);
395 return -EINVAL;
396 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900397 }
398
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900399 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
400
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900401 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700402 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900403 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700404 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900405 .rtm_family = family,
406 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700407 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900408 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900409
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700410 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
411 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900412
413 iovec iov[] = {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700414 { nullptr, 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700415 { &route, sizeof(route) },
416 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
417 { &table, sizeof(table) },
418 { &rtaDst, sizeof(rtaDst) },
419 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700420 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
421 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700422 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
423 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900424 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
425 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900426 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900427
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900428 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_ROUTE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Jimmy Chence9e5782019-03-08 16:12:55 +0800429
430 // Allow creating multiple link-local routes in the same table, so we can make IPv6
431 // work on all interfaces in the local_network table.
432 if (family == AF_INET6 && IN6_IS_ADDR_LINKLOCAL(reinterpret_cast<in6_addr*>(rawAddress))) {
433 flags &= ~NLM_F_EXCL;
434 }
435
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900436 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900437 if (ret) {
438 ALOGE("Error %s route %s -> %s %s to table %u: %s",
439 actionName(action), destination, nexthop, interface, table, strerror(-ret));
440 }
441 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700442}
443
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700444// An iptables rule to mark incoming packets on a network with the netId of the network.
445//
446// This is so that the kernel can:
447// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
448// replies, SYN-ACKs, etc).
449// + Mark sockets that accept connections from this interface so that the connection stays on the
450// same interface.
451WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
452 Permission permission, bool add) {
453 Fwmark fwmark;
454
455 fwmark.netId = netId;
456 fwmark.explicitlySelected = true;
457 fwmark.protectedFromVpn = true;
458 fwmark.permission = permission;
459
Benedict Wongb9baf262017-12-03 15:43:08 -0800460 const uint32_t mask = ~Fwmark::getUidBillingMask();
461
462 std::string cmd = StringPrintf(
463 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
464 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900465 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700466 ALOGE("failed to change iptables rule that sets incoming packet mark");
467 return -EREMOTEIO;
468 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700469
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700470 return 0;
471}
472
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700473// A rule to route responses to the local network forwarded via the VPN.
474//
475// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
476// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
477WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
478 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
479 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
480 INVALID_UID, INVALID_UID);
481}
482
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700483// A rule to route all traffic from a given set of UIDs to go over the VPN.
484//
485// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
486// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
487// bypass the VPN if the protectedFromVpn bit is set.
488WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700489 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700490 Fwmark fwmark;
491 Fwmark mask;
492
493 fwmark.protectedFromVpn = false;
494 mask.protectedFromVpn = true;
495
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700496 uint32_t priority;
497
498 if (secure) {
499 priority = RULE_PRIORITY_SECURE_VPN;
500 } else {
501 priority = RULE_PRIORITY_BYPASSABLE_VPN;
502
503 fwmark.explicitlySelected = false;
504 mask.explicitlySelected = true;
505 }
506
507 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900508 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700509}
510
511// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
512// set of UIDs.
513//
514// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
515// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700516WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
517 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700518 Fwmark fwmark;
519 Fwmark mask;
520
521 fwmark.netId = netId;
522 mask.netId = FWMARK_NET_ID_MASK;
523
524 fwmark.permission = PERMISSION_SYSTEM;
525 mask.permission = PERMISSION_SYSTEM;
526
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700527 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
528
529 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
530 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700531}
532
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700533// A rule to route traffic based on an explicitly chosen network.
534//
535// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
536//
537// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
538// to check it again in the rules here, because a network's permissions may have been updated via
539// modifyNetworkPermission().
540WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
541 Permission permission, uid_t uidStart,
542 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700543 Fwmark fwmark;
544 Fwmark mask;
545
546 fwmark.netId = netId;
547 mask.netId = FWMARK_NET_ID_MASK;
548
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700549 fwmark.explicitlySelected = true;
550 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700551
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700552 fwmark.permission = permission;
553 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700554
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700555 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900556 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700557}
558
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700559// A rule to route traffic based on a chosen outgoing interface.
560//
561// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
562// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900563WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
564 Permission permission, uid_t uidStart,
565 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700566 Fwmark fwmark;
567 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700568
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700569 fwmark.permission = permission;
570 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700571
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900572 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900573 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
574 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900575 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
576 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900577 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900578 UID_ROOT, UID_ROOT)) {
579 return ret;
580 }
581 }
582
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700583 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900584 fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700585}
586
587// A rule to route traffic based on the chosen network.
588//
589// This is for sockets that have not explicitly requested a particular network, but have been
590// bound to one when they called connect(). This ensures that sockets connected on a particular
591// network stay on that network even if the default network changes.
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900592WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700593 Fwmark fwmark;
594 Fwmark mask;
595
596 fwmark.netId = netId;
597 mask.netId = FWMARK_NET_ID_MASK;
598
599 fwmark.explicitlySelected = false;
600 mask.explicitlySelected = true;
601
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900602 fwmark.permission = PERMISSION_NONE;
603 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700604
605 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900606 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
607 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700608}
609
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700610// A rule to enable split tunnel VPNs.
611//
612// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
613// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
614// permissions required by the default network.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900615WARN_UNUSED_RESULT int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
616 const char* physicalInterface,
617 Permission permission) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700618 uint32_t table = getRouteTableForInterface(physicalInterface);
619 if (table == RT_TABLE_UNSPEC) {
620 return -ESRCH;
621 }
622
623 Fwmark fwmark;
624 Fwmark mask;
625
626 fwmark.netId = vpnNetId;
627 mask.netId = FWMARK_NET_ID_MASK;
628
629 fwmark.explicitlySelected = false;
630 mask.explicitlySelected = true;
631
632 fwmark.permission = permission;
633 mask.permission = permission;
634
635 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
636 mask.intValue);
637}
638
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700639// Add rules to allow legacy routes added through the requestRouteToHost() API.
640WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700641 Fwmark fwmark;
642 Fwmark mask;
643
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700644 fwmark.explicitlySelected = false;
645 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700646
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700647 // Rules to allow legacy routes to override the default network.
648 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
649 fwmark.intValue, mask.intValue)) {
650 return ret;
651 }
652 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
653 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
654 return ret;
655 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700656
657 fwmark.permission = PERMISSION_SYSTEM;
658 mask.permission = PERMISSION_SYSTEM;
659
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700660 // A rule to allow legacy routes from system apps to override VPNs.
661 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700662 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700663}
664
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700665// Add rules to lookup the local network when specified explicitly or otherwise.
666WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
667 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
668 INVALID_UID, INVALID_UID, ACTION_ADD)) {
669 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700670 }
671
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700672 Fwmark fwmark;
673 Fwmark mask;
674
675 fwmark.explicitlySelected = false;
676 mask.explicitlySelected = true;
677
678 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
679 fwmark.intValue, mask.intValue);
680}
681
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900682/* static */
683int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900684 const char *interface = DummyNetwork::INTERFACE_NAME;
685 uint32_t table = getRouteTableForInterface(interface);
686 if (table == RT_TABLE_UNSPEC) {
687 // getRouteTableForInterface has already looged an error.
688 return -ESRCH;
689 }
690
691 ifc_init();
692 int ret = ifc_up(interface);
693 ifc_close();
694 if (ret) {
695 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
696 return -errno;
697 }
698
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900699 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
700 INVALID_UID, INVALID_UID, ACTION_ADD))) {
701 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900702 return ret;
703 }
704
Yi Kongbdfd57e2018-07-25 13:26:10 -0700705 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", nullptr))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900706 return ret;
707 }
708
Yi Kongbdfd57e2018-07-25 13:26:10 -0700709 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", nullptr))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900710 return ret;
711 }
712
713 return 0;
714}
715
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900716// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
717// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
718// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
719// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700720WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100721 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
722 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700723}
724
725WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700726 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
727 return ret;
728 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900729 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
730 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700731}
732
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900733/* static */
734WARN_UNUSED_RESULT int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
735 Permission permission, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700736 uint32_t table = getRouteTableForInterface(interface);
737 if (table == RT_TABLE_UNSPEC) {
738 return -ESRCH;
739 }
740
741 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
742 return ret;
743 }
744 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
745 add)) {
746 return ret;
747 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900748 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700749 add)) {
750 return ret;
751 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900752
753 // Only set implicit rules for networks that don't require permissions.
754 //
755 // This is so that if the default network ceases to be the default network and then switches
756 // from requiring no permissions to requiring permissions, we ensure that apps only use the
757 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
758 // - it closes all sockets on the network except sockets that are explicitly selected.
759 //
760 // The lack of this rule only affects the special case above, because:
761 // - The only cases where we implicitly bind a socket to a network are the default network and
762 // the bypassable VPN that applies to the app, if any.
763 // - This rule doesn't affect VPNs because they don't support permissions at all.
764 // - The default network doesn't require permissions. While we support doing this, the framework
765 // never does it (partly because we'd end up in the situation where we tell apps that there is
766 // a default network, but they can't use it).
767 // - If the network is still the default network, the presence or absence of this rule does not
768 // matter.
769 //
770 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
771 // implicitly bound to a network because at the time of connect() it was the default, and that
772 // network must no longer be the default, and must now require permissions.
773 if (permission == PERMISSION_NONE) {
774 return modifyImplicitNetworkRule(netId, table, add);
775 }
776 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700777}
778
Robin Leeb8087362016-03-30 18:43:08 +0100779WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
780 Fwmark fwmark;
781 Fwmark mask;
782 fwmark.protectedFromVpn = false;
783 mask.protectedFromVpn = true;
784
Luke Huang94658ac2018-10-18 19:35:12 +0900785 for (const UidRangeParcel& range : uidRanges.getRanges()) {
786 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_PROHIBIT_NON_VPN,
787 FR_ACT_PROHIBIT, RT_TABLE_UNSPEC, fwmark.intValue, mask.intValue,
788 IIF_LOOPBACK, OIF_NONE, range.start, range.stop)) {
Robin Leeb8087362016-03-30 18:43:08 +0100789 return ret;
790 }
791 }
792
793 return 0;
794}
795
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900796WARN_UNUSED_RESULT int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
797 const UidRanges& uidRanges,
798 bool secure, bool add,
799 bool modifyNonUidBasedRules) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700800 uint32_t table = getRouteTableForInterface(interface);
801 if (table == RT_TABLE_UNSPEC) {
802 return -ESRCH;
803 }
804
Luke Huang94658ac2018-10-18 19:35:12 +0900805 for (const UidRangeParcel& range : uidRanges.getRanges()) {
806 if (int ret = modifyVpnUidRangeRule(table, range.start, range.stop, secure, add)) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700807 return ret;
808 }
Luke Huang94658ac2018-10-18 19:35:12 +0900809 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.start,
810 range.stop, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700811 return ret;
812 }
Luke Huang94658ac2018-10-18 19:35:12 +0900813 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.start,
814 range.stop, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700815 return ret;
816 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700817 }
818
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700819 if (modifyNonUidBasedRules) {
820 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700821 return ret;
822 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700823 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
824 return ret;
825 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700826 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700827 return ret;
828 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700829 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700830 }
831
832 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700833}
834
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900835WARN_UNUSED_RESULT int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
836 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700837 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700838 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900839 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700840 }
841
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700842 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700843 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700844
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700845 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700846 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700847
848 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700849 mask.permission = permission;
850
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700851 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colitti758627c2018-03-15 01:49:20 +0900852 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700853}
854
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900855WARN_UNUSED_RESULT int RouteController::modifyTetheredNetwork(uint16_t action,
856 const char* inputInterface,
857 const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700858 uint32_t table = getRouteTableForInterface(outputInterface);
859 if (table == RT_TABLE_UNSPEC) {
860 return -ESRCH;
861 }
862
863 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
864 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
865}
866
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900867// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900868// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900869WARN_UNUSED_RESULT int RouteController::modifyRoute(uint16_t action, const char* interface,
870 const char* destination, const char* nexthop,
871 TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700872 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700873 switch (tableType) {
874 case RouteController::INTERFACE: {
875 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700876 if (table == RT_TABLE_UNSPEC) {
877 return -ESRCH;
878 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700879 break;
880 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700881 case RouteController::LOCAL_NETWORK: {
882 table = ROUTE_TABLE_LOCAL_NETWORK;
883 break;
884 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700885 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700886 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700887 break;
888 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700889 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700890 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700891 break;
892 }
893 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700894
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900895 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700896 // Trying to add a route that already exists shouldn't cause an error.
897 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900898 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700899 }
900
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900901 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700902}
903
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900904WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
905 int ret = 0;
906 while (ret == 0) {
907 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
908 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
909 }
910
911 if (ret == -ENOENT) {
912 return 0;
913 } else {
914 return ret;
915 }
916}
917
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900918uint32_t getRulePriority(const nlmsghdr *nlh) {
919 return getRtmU32Attribute(nlh, FRA_PRIORITY);
920}
921
922uint32_t getRouteTable(const nlmsghdr *nlh) {
923 return getRtmU32Attribute(nlh, RTA_TABLE);
924}
925
926WARN_UNUSED_RESULT int flushRules() {
927 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
928 // Don't touch rules at priority 0 because by default they are used for local input.
929 return getRulePriority(nlh) != 0;
930 };
931 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
932}
933
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900934WARN_UNUSED_RESULT int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900935 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
936 return getRouteTable(nlh) == table;
937 };
938
939 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
940}
941
942// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900943WARN_UNUSED_RESULT int RouteController::flushRoutes(const char* interface) {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900944 std::lock_guard lock(sInterfaceToTableLock);
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900945
946 uint32_t table = getRouteTableForInterfaceLocked(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900947 if (table == RT_TABLE_UNSPEC) {
948 return -ESRCH;
949 }
950
951 int ret = flushRoutes(table);
952
953 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
954 // track of its name.
955 if (ret == 0) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900956 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900957 }
958
959 return ret;
960}
961
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700962int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700963 if (int ret = flushRules()) {
964 return ret;
965 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700966 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700967 return ret;
968 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700969 if (int ret = addLocalNetworkRules(localNetId)) {
970 return ret;
971 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700972 if (int ret = addUnreachableRule()) {
973 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700974 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900975 // Don't complain if we can't add the dummy network, since not all devices support it.
976 configureDummyNetwork();
977
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700978 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700979 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700980}
981
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700982int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700983 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700984}
985
986int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700987 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700988}
989
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700990int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
991 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700992 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
993 return ret;
994 }
995 updateTableNamesFile();
996 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700997}
998
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700999int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
1000 Permission permission) {
1001 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001002 return ret;
1003 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001004 if (int ret = flushRoutes(interface)) {
1005 return ret;
1006 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001007 if (int ret = clearTetheringRules(interface)) {
1008 return ret;
1009 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001010 updateTableNamesFile();
1011 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001012}
1013
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001014int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001015 bool secure, const UidRanges& uidRanges) {
1016 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001017 MODIFY_NON_UID_BASED_RULES)) {
1018 return ret;
1019 }
1020 updateTableNamesFile();
1021 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001022}
1023
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001024int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001025 bool secure, const UidRanges& uidRanges) {
1026 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001027 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001028 return ret;
1029 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001030 if (int ret = flushRoutes(interface)) {
1031 return ret;
1032 }
1033 updateTableNamesFile();
1034 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001035}
1036
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001037int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1038 Permission oldPermission,
1039 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001040 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001041 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001042 return ret;
1043 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001044 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001045}
1046
Robin Leeb8087362016-03-30 18:43:08 +01001047int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1048 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1049}
1050
1051int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1052 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1053}
1054
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001055int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001056 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001057 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001058 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001059}
1060
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001061int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001062 bool secure, const UidRanges& uidRanges) {
1063 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001064 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001065}
1066
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001067int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1068 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001069}
1070
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001071int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1072 Permission permission) {
1073 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001074}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001075
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001076int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001077 TableType tableType) {
1078 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001079}
1080
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001081int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001082 const char* nexthop, TableType tableType) {
1083 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001084}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001085
1086int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001087 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001088}
1089
1090int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001091 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001092}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001093
1094int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1095 Permission permission) {
1096 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1097}
1098
1099int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1100 const char* physicalInterface,
1101 Permission permission) {
1102 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1103}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001104
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001105// Protects sInterfaceToTable.
Luke Huang534980c2018-07-06 17:50:11 +08001106std::mutex RouteController::sInterfaceToTableLock;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001107std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1108
Luke Huangd1ee4622018-06-29 13:49:58 +08001109
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001110} // namespace net
1111} // namespace android