blob: fd30fe87b3b18247739b5c804c8669711646cfa8 [file] [log] [blame]
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "RouteController.h"
18
Dan Albertaa1be2b2015-01-06 09:36:17 -080019#include <arpa/inet.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <linux/fib_rules.h>
23#include <net/if.h>
24#include <sys/stat.h>
25
Elliott Hughesbd378322015-02-04 13:25:14 -080026#include <private/android_filesystem_config.h>
27
Dan Albertaa1be2b2015-01-06 09:36:17 -080028#include <map>
29
Lorenzo Colitti7035f222017-02-13 18:29:00 +090030#include "DummyNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070031#include "Fwmark.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090032#include "NetdConstants.h"
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090033#include "NetlinkCommands.h"
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070034#include "UidRanges.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070035
Elliott Hughesbbd56262015-12-04 15:45:10 -080036#include "android-base/file.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090037#include <android-base/stringprintf.h>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070038#define LOG_TAG "Netd"
39#include "log/log.h"
40#include "logwrap/logwrap.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090041#include "netutils/ifc.h"
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070042#include "resolv_netid.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;
Robin Leedc0d5782016-07-20 14:17:11 +010046using android::net::UidRange;
Dan Albert5407e142015-03-16 10:05:59 -070047
Lorenzo Colitti7035f222017-02-13 18:29:00 +090048namespace android {
49namespace net {
50
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090051auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand;
52
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070053// BEGIN CONSTANTS --------------------------------------------------------------------------------
54
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070055const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090056const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070057const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070058const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010059const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070060const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
61const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
62const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
63const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070064const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070065const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070066const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070067const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070068const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070069const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090070const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070071
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070072const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070073const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
74const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
75
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070076const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070077const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
78const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
79
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070080const char* const ROUTE_TABLE_NAME_LOCAL = "local";
81const char* const ROUTE_TABLE_NAME_MAIN = "main";
82
Maarten Derks6cb2c6b2020-09-25 10:50:17 +020083// TODO: These values aren't defined by the Linux kernel, because our UID routing changes are not
84// upstream (yet?), so we can't just pick them up from kernel headers. When (if?) the changes make
85// it upstream, we'll remove this and rely on the kernel header values. For now, add a static assert
86// that will warn us if upstream has given these values some other meaning.
Maarten Derksa6b22202020-09-25 10:39:44 +020087const uint16_t FRA_UID_START = 18;
88const uint16_t FRA_UID_END = 19;
Karsten Tausche4a61e5f2020-10-19 18:00:33 +020089// static_assert(FRA_UID_START > FRA_MAX,
90// "Android-specific FRA_UID_{START,END} values also assigned in Linux uapi. "
91// "Check that these values match what the kernel does and then update this assertion.");
Maarten Derksa6b22202020-09-25 10:39:44 +020092
Lorenzo Colittib5d19e92017-09-25 18:39:33 +090093// None of our regular routes specify priority, which causes them to have the default priority.
94// For default throw routes, we use a fixed priority of 100000.
Lorenzo Colitti5e03a892017-09-08 11:31:59 +090095uint32_t PRIO_THROW = 100000;
96
Lorenzo Colittid78843e2017-03-27 05:52:31 +090097const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
98
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070099const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
100
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700101const uid_t UID_ROOT = 0;
Lorenzo Colitti3093f562017-09-25 14:17:38 +0900102const uint32_t FWMARK_NONE = 0;
103const uint32_t MASK_NONE = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900104const char* const IIF_LOOPBACK = "lo";
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700105const char* const IIF_NONE = NULL;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700106const char* const OIF_NONE = NULL;
107const bool ACTION_ADD = true;
108const bool ACTION_DEL = false;
109const bool MODIFY_NON_UID_BASED_RULES = true;
110
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700111const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700112const 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 -0700113
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700114// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
115// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
116constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
117 return RTA_LENGTH(x);
118}
119
120// These are practically const, but can't be declared so, because they are used to initialize
121// non-const pointers ("void* iov_base") in iovec arrays.
Maarten Derks6cb2c6b2020-09-25 10:50:17 +0200122rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
123rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
124rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
125rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
126rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START };
127rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700128
Maarten Derks6cb2c6b2020-09-25 10:50:17 +0200129rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
130rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900131rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700132
133uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
134
135// END CONSTANTS ----------------------------------------------------------------------------------
136
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900137const char *actionName(uint16_t action) {
138 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
139 return ops[action % 4];
140}
141
142const char *familyName(uint8_t family) {
143 switch (family) {
144 case AF_INET: return "IPv4";
145 case AF_INET6: return "IPv6";
146 default: return "???";
147 }
148}
149
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900150// Caller must hold sInterfaceToTableLock.
151uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700152 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400153 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700154 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900155 sInterfaceToTable[interface] = index;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700156 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400157 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700158 // If the interface goes away if_nametoindex() will return 0 but we still need to know
159 // the index so we can remove the rules and routes.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900160 auto iter = sInterfaceToTable.find(interface);
161 if (iter == sInterfaceToTable.end()) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700162 ALOGE("cannot find interface %s", interface);
163 return RT_TABLE_UNSPEC;
164 }
165 return iter->second;
166}
167
Rubin Xuacbb6b72018-04-27 14:27:59 +0100168uint32_t RouteController::getIfIndex(const char* interface) {
169 android::RWLock::AutoRLock lock(sInterfaceToTableLock);
170
171 auto iter = sInterfaceToTable.find(interface);
172 if (iter == sInterfaceToTable.end()) {
173 ALOGE("getIfIndex: cannot find interface %s", interface);
174 return 0;
175 }
176
177 return iter->second - ROUTE_TABLE_OFFSET_FROM_INDEX;
178}
179
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900180uint32_t RouteController::getRouteTableForInterface(const char* interface) {
181 android::RWLock::AutoRLock lock(sInterfaceToTableLock);
182 return getRouteTableForInterfaceLocked(interface);
183}
184
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700185void addTableName(uint32_t table, const std::string& name, std::string* contents) {
186 char tableString[UINT32_STRLEN];
187 snprintf(tableString, sizeof(tableString), "%u", table);
188 *contents += tableString;
189 *contents += " ";
190 *contents += name;
191 *contents += "\n";
192}
193
194// 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 +0900195void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700196 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700197
198 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
199 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
200
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700201 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700202 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
203 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700204
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900205 android::RWLock::AutoRLock lock(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900206 for (const auto& entry : sInterfaceToTable) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700207 addTableName(entry.second, entry.first, &contents);
208 }
209
Dan Albert5407e142015-03-16 10:05:59 -0700210 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800211 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700212 return;
213 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700214}
215
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700216// Returns 0 on success or negative errno on failure.
217int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
218 if (!input) {
219 *length = 0;
220 *padding = 0;
221 return 0;
222 }
223 *length = strlcpy(name, input, IFNAMSIZ) + 1;
224 if (*length > IFNAMSIZ) {
225 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
226 return -ENAMETOOLONG;
227 }
228 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
229 return 0;
230}
231
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700232// Adds or removes a routing rule for IPv4 and IPv6.
233//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900234// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100235// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700236// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
237// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700238// + If |iif| is non-NULL, the rule matches the specified incoming interface.
239// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
240// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
241// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900242//
243// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100244WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
245 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700246 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700247 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
248 if (fwmark & ~mask) {
249 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
250 return -ERANGE;
251 }
252
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700253 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900254 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700255 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
256 size_t iifLength, oifLength;
257 uint16_t iifPadding, oifPadding;
258 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
259 return ret;
260 }
261 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
262 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900263 }
264
Lorenzo Colitti59656512014-06-25 03:20:29 +0900265 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700266 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700267 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900268 return -EUSERS;
269 }
Robin Lee4ef94642016-04-01 11:50:49 +0100270
Lorenzo Colitti72723682014-06-26 13:51:10 +0900271 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900272
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900273 // Assemble a rule request and put it in an array of iovec structures.
274 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100275 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900276 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
277 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900278 };
279
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900280 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
281 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
282 // table 0, it's a wildcard that matches anything.
283 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
284 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
285 return -ENOTUNIQ;
286 }
287
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700288 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
289 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900290
291 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700292 { NULL, 0 },
293 { &rule, sizeof(rule) },
294 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
295 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700296 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
297 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700298 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
299 { &fwmark, mask ? sizeof(fwmark) : 0 },
300 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
301 { &mask, mask ? sizeof(mask) : 0 },
Maarten Derksa6b22202020-09-25 10:39:44 +0200302 { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
303 { &uidStart, isUidRule ? sizeof(uidStart) : 0 },
304 { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 },
305 { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700306 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
307 { iifName, iifLength },
308 { PADDING_BUFFER, iifPadding },
309 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
310 { oifName, oifLength },
311 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900312 };
313
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900314 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700315 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
316 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900317 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900318 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
319 // Don't log when deleting a tethering rule that's not there. This matches the
320 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
321 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
322 strerror(-ret));
323 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900324 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700325 }
326 }
327
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900328 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700329}
330
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700331WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100332 uint32_t fwmark, uint32_t mask, const char* iif,
333 const char* oif, uid_t uidStart, uid_t uidEnd) {
334 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
335 uidEnd);
336}
337
338WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700339 uint32_t fwmark, uint32_t mask) {
340 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
341 INVALID_UID);
342}
343
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900344// Adds or deletes an IPv4 or IPv6 route.
345// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700346WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
347 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900348 // At least the destination must be non-null.
349 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700350 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900351 return -EFAULT;
352 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700353
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900354 // Parse the prefix.
355 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700356 uint8_t family;
357 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900358 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
359 &prefixLength);
360 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700361 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900362 return rawLength;
363 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700364
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900365 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700366 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900367 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
368 }
369
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700370 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900371 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900372 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700373
374 if (nexthop && !strcmp(nexthop, "unreachable")) {
375 type = RTN_UNREACHABLE;
376 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
377 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
378 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
379 interface = OIF_NONE;
380 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900381 } else if (nexthop && !strcmp(nexthop, "throw")) {
382 type = RTN_THROW;
383 interface = OIF_NONE;
384 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700385 } else {
386 // If an interface was specified, find the ifindex.
387 if (interface != OIF_NONE) {
388 ifindex = if_nametoindex(interface);
389 if (!ifindex) {
390 ALOGE("cannot find interface %s", interface);
391 return -ENODEV;
392 }
393 }
394
395 // If a nexthop was specified, parse it as the same family as the prefix.
396 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
397 ALOGE("inet_pton failed for nexthop %s", nexthop);
398 return -EINVAL;
399 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900400 }
401
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900402 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
403
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900404 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700405 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900406 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700407 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900408 .rtm_family = family,
409 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700410 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900411 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900412
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700413 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
414 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900415
416 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700417 { NULL, 0 },
418 { &route, sizeof(route) },
419 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
420 { &table, sizeof(table) },
421 { &rtaDst, sizeof(rtaDst) },
422 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700423 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
424 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700425 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
426 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900427 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
428 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900429 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900430
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900431 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_ROUTE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900432 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900433 if (ret) {
434 ALOGE("Error %s route %s -> %s %s to table %u: %s",
435 actionName(action), destination, nexthop, interface, table, strerror(-ret));
436 }
437 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700438}
439
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700440// An iptables rule to mark incoming packets on a network with the netId of the network.
441//
442// This is so that the kernel can:
443// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
444// replies, SYN-ACKs, etc).
445// + Mark sockets that accept connections from this interface so that the connection stays on the
446// same interface.
447WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
448 Permission permission, bool add) {
449 Fwmark fwmark;
450
451 fwmark.netId = netId;
452 fwmark.explicitlySelected = true;
453 fwmark.protectedFromVpn = true;
454 fwmark.permission = permission;
455
Benedict Wongb9baf262017-12-03 15:43:08 -0800456 const uint32_t mask = ~Fwmark::getUidBillingMask();
457
458 std::string cmd = StringPrintf(
459 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
460 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900461 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700462 ALOGE("failed to change iptables rule that sets incoming packet mark");
463 return -EREMOTEIO;
464 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700465
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700466 return 0;
467}
468
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700469// A rule to route responses to the local network forwarded via the VPN.
470//
471// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
472// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
473WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
474 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
475 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
476 INVALID_UID, INVALID_UID);
477}
478
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700479// A rule to route all traffic from a given set of UIDs to go over the VPN.
480//
481// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
482// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
483// bypass the VPN if the protectedFromVpn bit is set.
484WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700485 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700486 Fwmark fwmark;
487 Fwmark mask;
488
489 fwmark.protectedFromVpn = false;
490 mask.protectedFromVpn = true;
491
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700492 uint32_t priority;
493
494 if (secure) {
495 priority = RULE_PRIORITY_SECURE_VPN;
496 } else {
497 priority = RULE_PRIORITY_BYPASSABLE_VPN;
498
499 fwmark.explicitlySelected = false;
500 mask.explicitlySelected = true;
501 }
502
503 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900504 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700505}
506
507// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
508// set of UIDs.
509//
510// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
511// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700512WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
513 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700514 Fwmark fwmark;
515 Fwmark mask;
516
517 fwmark.netId = netId;
518 mask.netId = FWMARK_NET_ID_MASK;
519
520 fwmark.permission = PERMISSION_SYSTEM;
521 mask.permission = PERMISSION_SYSTEM;
522
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700523 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
524
525 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
526 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700527}
528
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700529// A rule to route traffic based on an explicitly chosen network.
530//
531// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
532//
533// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
534// to check it again in the rules here, because a network's permissions may have been updated via
535// modifyNetworkPermission().
536WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
537 Permission permission, uid_t uidStart,
538 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700539 Fwmark fwmark;
540 Fwmark mask;
541
542 fwmark.netId = netId;
543 mask.netId = FWMARK_NET_ID_MASK;
544
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700545 fwmark.explicitlySelected = true;
546 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700547
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700548 fwmark.permission = permission;
549 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700550
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700551 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Lorenzo Colittidcb11892018-03-15 01:49:20 +0900552 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700553}
554
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700555// A rule to route traffic based on a chosen outgoing interface.
556//
557// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
558// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900559WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
560 Permission permission, uid_t uidStart,
561 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700562 Fwmark fwmark;
563 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700564
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700565 fwmark.permission = permission;
566 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700567
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900568 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colittidcb11892018-03-15 01:49:20 +0900569 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
570 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900571 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
572 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colittidcb11892018-03-15 01:49:20 +0900573 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900574 UID_ROOT, UID_ROOT)) {
575 return ret;
576 }
577 }
578
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700579 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Lorenzo Colittidcb11892018-03-15 01:49:20 +0900580 fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700581}
582
583// A rule to route traffic based on the chosen network.
584//
585// This is for sockets that have not explicitly requested a particular network, but have been
586// bound to one when they called connect(). This ensures that sockets connected on a particular
587// network stay on that network even if the default network changes.
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900588WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700589 Fwmark fwmark;
590 Fwmark mask;
591
592 fwmark.netId = netId;
593 mask.netId = FWMARK_NET_ID_MASK;
594
595 fwmark.explicitlySelected = false;
596 mask.explicitlySelected = true;
597
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900598 fwmark.permission = PERMISSION_NONE;
599 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700600
601 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colittidcb11892018-03-15 01:49:20 +0900602 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
603 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700604}
605
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700606// A rule to enable split tunnel VPNs.
607//
608// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
609// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
610// permissions required by the default network.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900611WARN_UNUSED_RESULT int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
612 const char* physicalInterface,
613 Permission permission) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700614 uint32_t table = getRouteTableForInterface(physicalInterface);
615 if (table == RT_TABLE_UNSPEC) {
616 return -ESRCH;
617 }
618
619 Fwmark fwmark;
620 Fwmark mask;
621
622 fwmark.netId = vpnNetId;
623 mask.netId = FWMARK_NET_ID_MASK;
624
625 fwmark.explicitlySelected = false;
626 mask.explicitlySelected = true;
627
628 fwmark.permission = permission;
629 mask.permission = permission;
630
631 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
632 mask.intValue);
633}
634
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700635// Add rules to allow legacy routes added through the requestRouteToHost() API.
636WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700637 Fwmark fwmark;
638 Fwmark mask;
639
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700640 fwmark.explicitlySelected = false;
641 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700642
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700643 // Rules to allow legacy routes to override the default network.
644 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
645 fwmark.intValue, mask.intValue)) {
646 return ret;
647 }
648 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
649 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
650 return ret;
651 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700652
653 fwmark.permission = PERMISSION_SYSTEM;
654 mask.permission = PERMISSION_SYSTEM;
655
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700656 // A rule to allow legacy routes from system apps to override VPNs.
657 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700658 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700659}
660
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700661// Add rules to lookup the local network when specified explicitly or otherwise.
662WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
663 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
664 INVALID_UID, INVALID_UID, ACTION_ADD)) {
665 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700666 }
667
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700668 Fwmark fwmark;
669 Fwmark mask;
670
671 fwmark.explicitlySelected = false;
672 mask.explicitlySelected = true;
673
674 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
675 fwmark.intValue, mask.intValue);
676}
677
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900678/* static */
679int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900680 const char *interface = DummyNetwork::INTERFACE_NAME;
681 uint32_t table = getRouteTableForInterface(interface);
682 if (table == RT_TABLE_UNSPEC) {
683 // getRouteTableForInterface has already looged an error.
684 return -ESRCH;
685 }
686
687 ifc_init();
688 int ret = ifc_up(interface);
689 ifc_close();
690 if (ret) {
691 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
692 return -errno;
693 }
694
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900695 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
696 INVALID_UID, INVALID_UID, ACTION_ADD))) {
697 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900698 return ret;
699 }
700
701 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900702 return ret;
703 }
704
705 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900706 return ret;
707 }
708
709 return 0;
710}
711
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900712// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
713// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
714// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
715// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700716WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100717 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
718 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700719}
720
721WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700722 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
723 return ret;
724 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900725 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
726 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700727}
728
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900729/* static */
730WARN_UNUSED_RESULT int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
731 Permission permission, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700732 uint32_t table = getRouteTableForInterface(interface);
733 if (table == RT_TABLE_UNSPEC) {
734 return -ESRCH;
735 }
736
737 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
738 return ret;
739 }
740 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
741 add)) {
742 return ret;
743 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900744 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700745 add)) {
746 return ret;
747 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900748
749 // Only set implicit rules for networks that don't require permissions.
750 //
751 // This is so that if the default network ceases to be the default network and then switches
752 // from requiring no permissions to requiring permissions, we ensure that apps only use the
753 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
754 // - it closes all sockets on the network except sockets that are explicitly selected.
755 //
756 // The lack of this rule only affects the special case above, because:
757 // - The only cases where we implicitly bind a socket to a network are the default network and
758 // the bypassable VPN that applies to the app, if any.
759 // - This rule doesn't affect VPNs because they don't support permissions at all.
760 // - The default network doesn't require permissions. While we support doing this, the framework
761 // never does it (partly because we'd end up in the situation where we tell apps that there is
762 // a default network, but they can't use it).
763 // - If the network is still the default network, the presence or absence of this rule does not
764 // matter.
765 //
766 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
767 // implicitly bound to a network because at the time of connect() it was the default, and that
768 // network must no longer be the default, and must now require permissions.
769 if (permission == PERMISSION_NONE) {
770 return modifyImplicitNetworkRule(netId, table, add);
771 }
772 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700773}
774
Robin Leeb8087362016-03-30 18:43:08 +0100775WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
776 Fwmark fwmark;
777 Fwmark mask;
778 fwmark.protectedFromVpn = false;
779 mask.protectedFromVpn = true;
780
Robin Leedc0d5782016-07-20 14:17:11 +0100781 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100782 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
783 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
784 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100785 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100786 return ret;
787 }
788 }
789
790 return 0;
791}
792
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900793WARN_UNUSED_RESULT int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
794 const UidRanges& uidRanges,
795 bool secure, bool add,
796 bool modifyNonUidBasedRules) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700797 uint32_t table = getRouteTableForInterface(interface);
798 if (table == RT_TABLE_UNSPEC) {
799 return -ESRCH;
800 }
801
Robin Leedc0d5782016-07-20 14:17:11 +0100802 for (const UidRange& range : uidRanges.getRanges()) {
803 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
804 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700805 return ret;
806 }
Robin Leedc0d5782016-07-20 14:17:11 +0100807 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
808 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700809 return ret;
810 }
Robin Leedc0d5782016-07-20 14:17:11 +0100811 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
812 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700813 return ret;
814 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700815 }
816
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700817 if (modifyNonUidBasedRules) {
818 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700819 return ret;
820 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700821 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
822 return ret;
823 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700824 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700825 return ret;
826 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700827 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700828 }
829
830 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700831}
832
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900833WARN_UNUSED_RESULT int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
834 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700835 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700836 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900837 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700838 }
839
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700840 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700841 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700842
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700843 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700844 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700845
846 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700847 mask.permission = permission;
848
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700849 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colittidcb11892018-03-15 01:49:20 +0900850 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700851}
852
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900853WARN_UNUSED_RESULT int RouteController::modifyTetheredNetwork(uint16_t action,
854 const char* inputInterface,
855 const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700856 uint32_t table = getRouteTableForInterface(outputInterface);
857 if (table == RT_TABLE_UNSPEC) {
858 return -ESRCH;
859 }
860
861 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
862 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
863}
864
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900865// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900866// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900867WARN_UNUSED_RESULT int RouteController::modifyRoute(uint16_t action, const char* interface,
868 const char* destination, const char* nexthop,
869 TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700870 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700871 switch (tableType) {
872 case RouteController::INTERFACE: {
873 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700874 if (table == RT_TABLE_UNSPEC) {
875 return -ESRCH;
876 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700877 break;
878 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700879 case RouteController::LOCAL_NETWORK: {
880 table = ROUTE_TABLE_LOCAL_NETWORK;
881 break;
882 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700883 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700884 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700885 break;
886 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700887 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700888 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700889 break;
890 }
891 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700892
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900893 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700894 // Trying to add a route that already exists shouldn't cause an error.
895 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900896 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700897 }
898
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900899 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700900}
901
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900902WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
903 int ret = 0;
904 while (ret == 0) {
905 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
906 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
907 }
908
909 if (ret == -ENOENT) {
910 return 0;
911 } else {
912 return ret;
913 }
914}
915
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900916uint32_t getRulePriority(const nlmsghdr *nlh) {
917 return getRtmU32Attribute(nlh, FRA_PRIORITY);
918}
919
920uint32_t getRouteTable(const nlmsghdr *nlh) {
921 return getRtmU32Attribute(nlh, RTA_TABLE);
922}
923
924WARN_UNUSED_RESULT int flushRules() {
925 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
926 // Don't touch rules at priority 0 because by default they are used for local input.
927 return getRulePriority(nlh) != 0;
928 };
929 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
930}
931
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900932WARN_UNUSED_RESULT int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900933 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
934 return getRouteTable(nlh) == table;
935 };
936
937 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
938}
939
940// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900941WARN_UNUSED_RESULT int RouteController::flushRoutes(const char* interface) {
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900942 android::RWLock::AutoWLock lock(sInterfaceToTableLock);
943
944 uint32_t table = getRouteTableForInterfaceLocked(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900945 if (table == RT_TABLE_UNSPEC) {
946 return -ESRCH;
947 }
948
949 int ret = flushRoutes(table);
950
951 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
952 // track of its name.
953 if (ret == 0) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900954 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900955 }
956
957 return ret;
958}
959
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700960int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700961 if (int ret = flushRules()) {
962 return ret;
963 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700964 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700965 return ret;
966 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700967 if (int ret = addLocalNetworkRules(localNetId)) {
968 return ret;
969 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700970 if (int ret = addUnreachableRule()) {
971 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700972 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900973 // Don't complain if we can't add the dummy network, since not all devices support it.
974 configureDummyNetwork();
975
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700976 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700977 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700978}
979
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700980int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700981 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700982}
983
984int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700985 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700986}
987
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700988int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
989 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700990 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
991 return ret;
992 }
993 updateTableNamesFile();
994 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700995}
996
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700997int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
998 Permission permission) {
999 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001000 return ret;
1001 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001002 if (int ret = flushRoutes(interface)) {
1003 return ret;
1004 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001005 if (int ret = clearTetheringRules(interface)) {
1006 return ret;
1007 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001008 updateTableNamesFile();
1009 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001010}
1011
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001012int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001013 bool secure, const UidRanges& uidRanges) {
1014 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001015 MODIFY_NON_UID_BASED_RULES)) {
1016 return ret;
1017 }
1018 updateTableNamesFile();
1019 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001020}
1021
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001022int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001023 bool secure, const UidRanges& uidRanges) {
1024 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001025 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001026 return ret;
1027 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001028 if (int ret = flushRoutes(interface)) {
1029 return ret;
1030 }
1031 updateTableNamesFile();
1032 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001033}
1034
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001035int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1036 Permission oldPermission,
1037 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001038 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001039 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001040 return ret;
1041 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001042 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001043}
1044
Robin Leeb8087362016-03-30 18:43:08 +01001045int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1046 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1047}
1048
1049int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1050 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1051}
1052
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001053int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001054 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001055 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001056 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001057}
1058
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001059int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001060 bool secure, const UidRanges& uidRanges) {
1061 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001062 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001063}
1064
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001065int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1066 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001067}
1068
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001069int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1070 Permission permission) {
1071 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001072}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001073
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001074int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001075 TableType tableType) {
1076 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001077}
1078
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001079int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001080 const char* nexthop, TableType tableType) {
1081 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001082}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001083
1084int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001085 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001086}
1087
1088int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001089 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001090}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001091
1092int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1093 Permission permission) {
1094 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1095}
1096
1097int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1098 const char* physicalInterface,
1099 Permission permission) {
1100 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1101}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001102
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001103// Protects sInterfaceToTable.
1104android::RWLock RouteController::sInterfaceToTableLock;
1105
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001106std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1107
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001108} // namespace net
1109} // namespace android