blob: f48e958d84885902f74b95caa41520f38d13848f [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
Luca Weissaf928dc2021-07-02 16:48:12 +020083// TODO: These values aren't defined by the Linux kernel, because legacy UID routing (as used in N
84// and below) was not upstreamed. Now that the UID routing code is upstream, we should remove these
85// and rely on the kernel header values.
Maarten Derksa6b22202020-09-25 10:39:44 +020086const uint16_t FRA_UID_START = 18;
87const uint16_t FRA_UID_END = 19;
88
Lorenzo Colittib5d19e92017-09-25 18:39:33 +090089// None of our regular routes specify priority, which causes them to have the default priority.
90// For default throw routes, we use a fixed priority of 100000.
Lorenzo Colitti5e03a892017-09-08 11:31:59 +090091uint32_t PRIO_THROW = 100000;
92
Lorenzo Colittid78843e2017-03-27 05:52:31 +090093const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
94
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070095const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
96
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070097const uid_t UID_ROOT = 0;
Lorenzo Colitti3093f562017-09-25 14:17:38 +090098const uint32_t FWMARK_NONE = 0;
99const uint32_t MASK_NONE = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900100const char* const IIF_LOOPBACK = "lo";
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700101const char* const IIF_NONE = NULL;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700102const char* const OIF_NONE = NULL;
103const bool ACTION_ADD = true;
104const bool ACTION_DEL = false;
105const bool MODIFY_NON_UID_BASED_RULES = true;
106
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700107const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700108const 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 -0700109
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700110// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
111// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
112constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
113 return RTA_LENGTH(x);
114}
115
116// These are practically const, but can't be declared so, because they are used to initialize
117// non-const pointers ("void* iov_base") in iovec arrays.
Luca Weissaf928dc2021-07-02 16:48:12 +0200118rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
119rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
120rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
121rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
122rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START };
123rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END };
124rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700125
Luca Weissaf928dc2021-07-02 16:48:12 +0200126rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
127rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900128rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700129
130uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
131
132// END CONSTANTS ----------------------------------------------------------------------------------
133
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900134const char *actionName(uint16_t action) {
135 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
136 return ops[action % 4];
137}
138
139const char *familyName(uint8_t family) {
140 switch (family) {
141 case AF_INET: return "IPv4";
142 case AF_INET6: return "IPv6";
143 default: return "???";
144 }
145}
146
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900147// Caller must hold sInterfaceToTableLock.
148uint32_t RouteController::getRouteTableForInterfaceLocked(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700149 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400150 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700151 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900152 sInterfaceToTable[interface] = index;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700153 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400154 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700155 // If the interface goes away if_nametoindex() will return 0 but we still need to know
156 // the index so we can remove the rules and routes.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900157 auto iter = sInterfaceToTable.find(interface);
158 if (iter == sInterfaceToTable.end()) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700159 ALOGE("cannot find interface %s", interface);
160 return RT_TABLE_UNSPEC;
161 }
162 return iter->second;
163}
164
Rubin Xuacbb6b72018-04-27 14:27:59 +0100165uint32_t RouteController::getIfIndex(const char* interface) {
166 android::RWLock::AutoRLock lock(sInterfaceToTableLock);
167
168 auto iter = sInterfaceToTable.find(interface);
169 if (iter == sInterfaceToTable.end()) {
170 ALOGE("getIfIndex: cannot find interface %s", interface);
171 return 0;
172 }
173
174 return iter->second - ROUTE_TABLE_OFFSET_FROM_INDEX;
175}
176
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900177uint32_t RouteController::getRouteTableForInterface(const char* interface) {
178 android::RWLock::AutoRLock lock(sInterfaceToTableLock);
179 return getRouteTableForInterfaceLocked(interface);
180}
181
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700182void addTableName(uint32_t table, const std::string& name, std::string* contents) {
183 char tableString[UINT32_STRLEN];
184 snprintf(tableString, sizeof(tableString), "%u", table);
185 *contents += tableString;
186 *contents += " ";
187 *contents += name;
188 *contents += "\n";
189}
190
191// 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 +0900192void RouteController::updateTableNamesFile() {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700193 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700194
195 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
196 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
197
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700198 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700199 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
200 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700201
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900202 android::RWLock::AutoRLock lock(sInterfaceToTableLock);
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900203 for (const auto& entry : sInterfaceToTable) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700204 addTableName(entry.second, entry.first, &contents);
205 }
206
Dan Albert5407e142015-03-16 10:05:59 -0700207 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800208 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700209 return;
210 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700211}
212
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700213// Returns 0 on success or negative errno on failure.
214int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
215 if (!input) {
216 *length = 0;
217 *padding = 0;
218 return 0;
219 }
220 *length = strlcpy(name, input, IFNAMSIZ) + 1;
221 if (*length > IFNAMSIZ) {
222 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
223 return -ENAMETOOLONG;
224 }
225 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
226 return 0;
227}
228
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700229// Adds or removes a routing rule for IPv4 and IPv6.
230//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900231// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100232// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700233// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
234// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700235// + If |iif| is non-NULL, the rule matches the specified incoming interface.
236// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
237// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
238// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900239//
240// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100241WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
242 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700243 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700244 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
245 if (fwmark & ~mask) {
246 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
247 return -ERANGE;
248 }
249
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700250 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900251 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700252 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
253 size_t iifLength, oifLength;
254 uint16_t iifPadding, oifPadding;
255 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
256 return ret;
257 }
258 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
259 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900260 }
261
Lorenzo Colitti59656512014-06-25 03:20:29 +0900262 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700263 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700264 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900265 return -EUSERS;
266 }
Robin Lee4ef94642016-04-01 11:50:49 +0100267
Lorenzo Colitti72723682014-06-26 13:51:10 +0900268 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900269
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900270 // Assemble a rule request and put it in an array of iovec structures.
271 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100272 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900273 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
274 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900275 };
276
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900277 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
278 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
279 // table 0, it's a wildcard that matches anything.
280 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
281 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
282 return -ENOTUNIQ;
283 }
284
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700285 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
286 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Luca Weissaf928dc2021-07-02 16:48:12 +0200287 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900288
289 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700290 { NULL, 0 },
291 { &rule, sizeof(rule) },
292 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
293 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700294 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
295 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700296 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
297 { &fwmark, mask ? sizeof(fwmark) : 0 },
298 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
299 { &mask, mask ? sizeof(mask) : 0 },
Luca Weissaf928dc2021-07-02 16:48:12 +0200300 // Rules that contain both legacy and new UID routing attributes will work on old kernels,
301 // which will simply ignore the FRA_UID_RANGE attribute since it is larger than their
302 // FRA_MAX. They will also work on kernels that are not too new:
303 // - FRA_UID_START clashes with FRA_PAD in 4.7, but that shouldn't be a problem because
304 // FRA_PAD has no validation.
305 // - FRA_UID_END clashes with FRA_L3MDEV in 4.8 and above, and will cause an error because
306 // FRA_L3MDEV has a maximum length of 1.
307 // TODO: delete the legacy UID routing code before running it on 4.8 or above.
Maarten Derksa6b22202020-09-25 10:39:44 +0200308 { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
309 { &uidStart, isUidRule ? sizeof(uidStart) : 0 },
310 { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 },
311 { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 },
Luca Weissaf928dc2021-07-02 16:48:12 +0200312 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
313 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700314 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
315 { iifName, iifLength },
316 { PADDING_BUFFER, iifPadding },
317 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
318 { oifName, oifLength },
319 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900320 };
321
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900322 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_RULE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700323 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
324 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900325 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900326 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
327 // Don't log when deleting a tethering rule that's not there. This matches the
328 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
329 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
330 strerror(-ret));
331 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900332 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700333 }
334 }
335
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900336 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700337}
338
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700339WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100340 uint32_t fwmark, uint32_t mask, const char* iif,
341 const char* oif, uid_t uidStart, uid_t uidEnd) {
342 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
343 uidEnd);
344}
345
346WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700347 uint32_t fwmark, uint32_t mask) {
348 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
349 INVALID_UID);
350}
351
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900352// Adds or deletes an IPv4 or IPv6 route.
353// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700354WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
355 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900356 // At least the destination must be non-null.
357 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700358 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900359 return -EFAULT;
360 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700361
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900362 // Parse the prefix.
363 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700364 uint8_t family;
365 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900366 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
367 &prefixLength);
368 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700369 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900370 return rawLength;
371 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700372
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900373 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700374 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900375 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
376 }
377
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700378 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900379 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900380 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700381
382 if (nexthop && !strcmp(nexthop, "unreachable")) {
383 type = RTN_UNREACHABLE;
384 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
385 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
386 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
387 interface = OIF_NONE;
388 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900389 } else if (nexthop && !strcmp(nexthop, "throw")) {
390 type = RTN_THROW;
391 interface = OIF_NONE;
392 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700393 } else {
394 // If an interface was specified, find the ifindex.
395 if (interface != OIF_NONE) {
396 ifindex = if_nametoindex(interface);
397 if (!ifindex) {
398 ALOGE("cannot find interface %s", interface);
399 return -ENODEV;
400 }
401 }
402
403 // If a nexthop was specified, parse it as the same family as the prefix.
404 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
405 ALOGE("inet_pton failed for nexthop %s", nexthop);
406 return -EINVAL;
407 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900408 }
409
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900410 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
411
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900412 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700413 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900414 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700415 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900416 .rtm_family = family,
417 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700418 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900419 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900420
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700421 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
422 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900423
424 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700425 { NULL, 0 },
426 { &route, sizeof(route) },
427 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
428 { &table, sizeof(table) },
429 { &rtaDst, sizeof(rtaDst) },
430 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700431 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
432 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700433 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
434 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900435 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
436 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900437 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900438
Lorenzo Colitti5c437992017-11-28 01:26:02 +0900439 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_ROUTE_CREATE_FLAGS : NETLINK_REQUEST_FLAGS;
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900440 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900441 if (ret) {
442 ALOGE("Error %s route %s -> %s %s to table %u: %s",
443 actionName(action), destination, nexthop, interface, table, strerror(-ret));
444 }
445 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700446}
447
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700448// An iptables rule to mark incoming packets on a network with the netId of the network.
449//
450// This is so that the kernel can:
451// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
452// replies, SYN-ACKs, etc).
453// + Mark sockets that accept connections from this interface so that the connection stays on the
454// same interface.
455WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
456 Permission permission, bool add) {
457 Fwmark fwmark;
458
459 fwmark.netId = netId;
460 fwmark.explicitlySelected = true;
461 fwmark.protectedFromVpn = true;
462 fwmark.permission = permission;
463
Benedict Wongb9baf262017-12-03 15:43:08 -0800464 const uint32_t mask = ~Fwmark::getUidBillingMask();
465
466 std::string cmd = StringPrintf(
467 "%s %s -i %s -j MARK --set-mark 0x%x/0x%x", add ? "-A" : "-D",
468 RouteController::LOCAL_MANGLE_INPUT, interface, fwmark.intValue, mask);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900469 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700470 ALOGE("failed to change iptables rule that sets incoming packet mark");
471 return -EREMOTEIO;
472 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700473
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700474 return 0;
475}
476
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700477// A rule to route responses to the local network forwarded via the VPN.
478//
479// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
480// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
481WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
482 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
483 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
484 INVALID_UID, INVALID_UID);
485}
486
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700487// A rule to route all traffic from a given set of UIDs to go over the VPN.
488//
489// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
490// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
491// bypass the VPN if the protectedFromVpn bit is set.
492WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700493 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700494 Fwmark fwmark;
495 Fwmark mask;
496
497 fwmark.protectedFromVpn = false;
498 mask.protectedFromVpn = true;
499
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700500 uint32_t priority;
501
502 if (secure) {
503 priority = RULE_PRIORITY_SECURE_VPN;
504 } else {
505 priority = RULE_PRIORITY_BYPASSABLE_VPN;
506
507 fwmark.explicitlySelected = false;
508 mask.explicitlySelected = true;
509 }
510
511 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900512 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700513}
514
515// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
516// set of UIDs.
517//
518// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
519// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700520WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
521 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700522 Fwmark fwmark;
523 Fwmark mask;
524
525 fwmark.netId = netId;
526 mask.netId = FWMARK_NET_ID_MASK;
527
528 fwmark.permission = PERMISSION_SYSTEM;
529 mask.permission = PERMISSION_SYSTEM;
530
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700531 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
532
533 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
534 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700535}
536
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700537// A rule to route traffic based on an explicitly chosen network.
538//
539// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
540//
541// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
542// to check it again in the rules here, because a network's permissions may have been updated via
543// modifyNetworkPermission().
544WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
545 Permission permission, uid_t uidStart,
546 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700547 Fwmark fwmark;
548 Fwmark mask;
549
550 fwmark.netId = netId;
551 mask.netId = FWMARK_NET_ID_MASK;
552
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700553 fwmark.explicitlySelected = true;
554 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700555
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700556 fwmark.permission = permission;
557 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700558
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700559 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Lorenzo Colittidcb11892018-03-15 01:49:20 +0900560 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700561}
562
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700563// A rule to route traffic based on a chosen outgoing interface.
564//
565// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
566// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900567WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
568 Permission permission, uid_t uidStart,
569 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700570 Fwmark fwmark;
571 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700572
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700573 fwmark.permission = permission;
574 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700575
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900576 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colittidcb11892018-03-15 01:49:20 +0900577 // for root. This covers kernel-originated packets, TEEd packets and any local daemons that open
578 // sockets as root.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900579 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
580 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colittidcb11892018-03-15 01:49:20 +0900581 table, FWMARK_NONE, MASK_NONE, IIF_LOOPBACK, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900582 UID_ROOT, UID_ROOT)) {
583 return ret;
584 }
585 }
586
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700587 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Lorenzo Colittidcb11892018-03-15 01:49:20 +0900588 fwmark.intValue, mask.intValue, IIF_LOOPBACK, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700589}
590
591// A rule to route traffic based on the chosen network.
592//
593// This is for sockets that have not explicitly requested a particular network, but have been
594// bound to one when they called connect(). This ensures that sockets connected on a particular
595// network stay on that network even if the default network changes.
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900596WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700597 Fwmark fwmark;
598 Fwmark mask;
599
600 fwmark.netId = netId;
601 mask.netId = FWMARK_NET_ID_MASK;
602
603 fwmark.explicitlySelected = false;
604 mask.explicitlySelected = true;
605
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900606 fwmark.permission = PERMISSION_NONE;
607 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700608
609 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Lorenzo Colittidcb11892018-03-15 01:49:20 +0900610 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID,
611 INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700612}
613
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700614// A rule to enable split tunnel VPNs.
615//
616// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
617// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
618// permissions required by the default network.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900619WARN_UNUSED_RESULT int RouteController::modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
620 const char* physicalInterface,
621 Permission permission) {
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700622 uint32_t table = getRouteTableForInterface(physicalInterface);
623 if (table == RT_TABLE_UNSPEC) {
624 return -ESRCH;
625 }
626
627 Fwmark fwmark;
628 Fwmark mask;
629
630 fwmark.netId = vpnNetId;
631 mask.netId = FWMARK_NET_ID_MASK;
632
633 fwmark.explicitlySelected = false;
634 mask.explicitlySelected = true;
635
636 fwmark.permission = permission;
637 mask.permission = permission;
638
639 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
640 mask.intValue);
641}
642
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700643// Add rules to allow legacy routes added through the requestRouteToHost() API.
644WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700645 Fwmark fwmark;
646 Fwmark mask;
647
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700648 fwmark.explicitlySelected = false;
649 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700650
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700651 // Rules to allow legacy routes to override the default network.
652 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
653 fwmark.intValue, mask.intValue)) {
654 return ret;
655 }
656 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
657 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
658 return ret;
659 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700660
661 fwmark.permission = PERMISSION_SYSTEM;
662 mask.permission = PERMISSION_SYSTEM;
663
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700664 // A rule to allow legacy routes from system apps to override VPNs.
665 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700666 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700667}
668
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700669// Add rules to lookup the local network when specified explicitly or otherwise.
670WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
671 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
672 INVALID_UID, INVALID_UID, ACTION_ADD)) {
673 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700674 }
675
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700676 Fwmark fwmark;
677 Fwmark mask;
678
679 fwmark.explicitlySelected = false;
680 mask.explicitlySelected = true;
681
682 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
683 fwmark.intValue, mask.intValue);
684}
685
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900686/* static */
687int RouteController::configureDummyNetwork() {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900688 const char *interface = DummyNetwork::INTERFACE_NAME;
689 uint32_t table = getRouteTableForInterface(interface);
690 if (table == RT_TABLE_UNSPEC) {
691 // getRouteTableForInterface has already looged an error.
692 return -ESRCH;
693 }
694
695 ifc_init();
696 int ret = ifc_up(interface);
697 ifc_close();
698 if (ret) {
699 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
700 return -errno;
701 }
702
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900703 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
704 INVALID_UID, INVALID_UID, ACTION_ADD))) {
705 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900706 return ret;
707 }
708
709 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900710 return ret;
711 }
712
713 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900714 return ret;
715 }
716
717 return 0;
718}
719
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900720// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
721// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
722// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
723// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700724WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100725 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
726 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700727}
728
729WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700730 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
731 return ret;
732 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900733 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
734 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700735}
736
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900737/* static */
738WARN_UNUSED_RESULT int RouteController::modifyPhysicalNetwork(unsigned netId, const char* interface,
739 Permission permission, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700740 uint32_t table = getRouteTableForInterface(interface);
741 if (table == RT_TABLE_UNSPEC) {
742 return -ESRCH;
743 }
744
745 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
746 return ret;
747 }
748 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
749 add)) {
750 return ret;
751 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900752 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700753 add)) {
754 return ret;
755 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900756
757 // Only set implicit rules for networks that don't require permissions.
758 //
759 // This is so that if the default network ceases to be the default network and then switches
760 // from requiring no permissions to requiring permissions, we ensure that apps only use the
761 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
762 // - it closes all sockets on the network except sockets that are explicitly selected.
763 //
764 // The lack of this rule only affects the special case above, because:
765 // - The only cases where we implicitly bind a socket to a network are the default network and
766 // the bypassable VPN that applies to the app, if any.
767 // - This rule doesn't affect VPNs because they don't support permissions at all.
768 // - The default network doesn't require permissions. While we support doing this, the framework
769 // never does it (partly because we'd end up in the situation where we tell apps that there is
770 // a default network, but they can't use it).
771 // - If the network is still the default network, the presence or absence of this rule does not
772 // matter.
773 //
774 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
775 // implicitly bound to a network because at the time of connect() it was the default, and that
776 // network must no longer be the default, and must now require permissions.
777 if (permission == PERMISSION_NONE) {
778 return modifyImplicitNetworkRule(netId, table, add);
779 }
780 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700781}
782
Robin Leeb8087362016-03-30 18:43:08 +0100783WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
784 Fwmark fwmark;
785 Fwmark mask;
786 fwmark.protectedFromVpn = false;
787 mask.protectedFromVpn = true;
788
Robin Leedc0d5782016-07-20 14:17:11 +0100789 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100790 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
791 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
792 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100793 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100794 return ret;
795 }
796 }
797
798 return 0;
799}
800
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900801WARN_UNUSED_RESULT int RouteController::modifyVirtualNetwork(unsigned netId, const char* interface,
802 const UidRanges& uidRanges,
803 bool secure, bool add,
804 bool modifyNonUidBasedRules) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700805 uint32_t table = getRouteTableForInterface(interface);
806 if (table == RT_TABLE_UNSPEC) {
807 return -ESRCH;
808 }
809
Robin Leedc0d5782016-07-20 14:17:11 +0100810 for (const UidRange& range : uidRanges.getRanges()) {
811 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
812 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700813 return ret;
814 }
Robin Leedc0d5782016-07-20 14:17:11 +0100815 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
816 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700817 return ret;
818 }
Robin Leedc0d5782016-07-20 14:17:11 +0100819 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
820 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700821 return ret;
822 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700823 }
824
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700825 if (modifyNonUidBasedRules) {
826 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700827 return ret;
828 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700829 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
830 return ret;
831 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700832 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700833 return ret;
834 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700835 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700836 }
837
838 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700839}
840
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900841WARN_UNUSED_RESULT int RouteController::modifyDefaultNetwork(uint16_t action, const char* interface,
842 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700843 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700844 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900845 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700846 }
847
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700848 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700849 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700850
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700851 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700852 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700853
854 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700855 mask.permission = permission;
856
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700857 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Lorenzo Colittidcb11892018-03-15 01:49:20 +0900858 mask.intValue, IIF_LOOPBACK, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700859}
860
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900861WARN_UNUSED_RESULT int RouteController::modifyTetheredNetwork(uint16_t action,
862 const char* inputInterface,
863 const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700864 uint32_t table = getRouteTableForInterface(outputInterface);
865 if (table == RT_TABLE_UNSPEC) {
866 return -ESRCH;
867 }
868
869 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
870 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
871}
872
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900873// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900874// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900875WARN_UNUSED_RESULT int RouteController::modifyRoute(uint16_t action, const char* interface,
876 const char* destination, const char* nexthop,
877 TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700878 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700879 switch (tableType) {
880 case RouteController::INTERFACE: {
881 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700882 if (table == RT_TABLE_UNSPEC) {
883 return -ESRCH;
884 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700885 break;
886 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700887 case RouteController::LOCAL_NETWORK: {
888 table = ROUTE_TABLE_LOCAL_NETWORK;
889 break;
890 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700891 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700892 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700893 break;
894 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700895 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700896 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700897 break;
898 }
899 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700900
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900901 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700902 // Trying to add a route that already exists shouldn't cause an error.
903 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900904 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700905 }
906
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900907 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700908}
909
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900910WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
911 int ret = 0;
912 while (ret == 0) {
913 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
914 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
915 }
916
917 if (ret == -ENOENT) {
918 return 0;
919 } else {
920 return ret;
921 }
922}
923
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900924uint32_t getRulePriority(const nlmsghdr *nlh) {
925 return getRtmU32Attribute(nlh, FRA_PRIORITY);
926}
927
928uint32_t getRouteTable(const nlmsghdr *nlh) {
929 return getRtmU32Attribute(nlh, RTA_TABLE);
930}
931
932WARN_UNUSED_RESULT int flushRules() {
933 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
934 // Don't touch rules at priority 0 because by default they are used for local input.
935 return getRulePriority(nlh) != 0;
936 };
937 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
938}
939
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900940WARN_UNUSED_RESULT int RouteController::flushRoutes(uint32_t table) {
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900941 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
942 return getRouteTable(nlh) == table;
943 };
944
945 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
946}
947
948// Returns 0 on success or negative errno on failure.
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900949WARN_UNUSED_RESULT int RouteController::flushRoutes(const char* interface) {
Lorenzo Colitti107075a2017-10-30 19:24:46 +0900950 android::RWLock::AutoWLock lock(sInterfaceToTableLock);
951
952 uint32_t table = getRouteTableForInterfaceLocked(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900953 if (table == RT_TABLE_UNSPEC) {
954 return -ESRCH;
955 }
956
957 int ret = flushRoutes(table);
958
959 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
960 // track of its name.
961 if (ret == 0) {
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +0900962 sInterfaceToTable.erase(interface);
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900963 }
964
965 return ret;
966}
967
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700968int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700969 if (int ret = flushRules()) {
970 return ret;
971 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700972 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700973 return ret;
974 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700975 if (int ret = addLocalNetworkRules(localNetId)) {
976 return ret;
977 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700978 if (int ret = addUnreachableRule()) {
979 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700980 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900981 // Don't complain if we can't add the dummy network, since not all devices support it.
982 configureDummyNetwork();
983
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700984 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700985 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700986}
987
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700988int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700989 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700990}
991
992int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700993 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700994}
995
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700996int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
997 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700998 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
999 return ret;
1000 }
1001 updateTableNamesFile();
1002 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001003}
1004
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001005int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
1006 Permission permission) {
1007 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001008 return ret;
1009 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001010 if (int ret = flushRoutes(interface)) {
1011 return ret;
1012 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001013 if (int ret = clearTetheringRules(interface)) {
1014 return ret;
1015 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001016 updateTableNamesFile();
1017 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001018}
1019
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001020int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001021 bool secure, const UidRanges& uidRanges) {
1022 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001023 MODIFY_NON_UID_BASED_RULES)) {
1024 return ret;
1025 }
1026 updateTableNamesFile();
1027 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001028}
1029
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001030int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001031 bool secure, const UidRanges& uidRanges) {
1032 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001033 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001034 return ret;
1035 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001036 if (int ret = flushRoutes(interface)) {
1037 return ret;
1038 }
1039 updateTableNamesFile();
1040 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001041}
1042
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001043int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1044 Permission oldPermission,
1045 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001046 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001047 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001048 return ret;
1049 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001050 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001051}
1052
Robin Leeb8087362016-03-30 18:43:08 +01001053int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1054 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1055}
1056
1057int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1058 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1059}
1060
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001061int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001062 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001063 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001064 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001065}
1066
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001067int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001068 bool secure, const UidRanges& uidRanges) {
1069 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001070 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001071}
1072
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001073int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1074 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001075}
1076
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001077int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1078 Permission permission) {
1079 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001080}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001081
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001082int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001083 TableType tableType) {
1084 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001085}
1086
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001087int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001088 const char* nexthop, TableType tableType) {
1089 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001090}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001091
1092int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001093 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001094}
1095
1096int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001097 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001098}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001099
1100int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1101 Permission permission) {
1102 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1103}
1104
1105int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1106 const char* physicalInterface,
1107 Permission permission) {
1108 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1109}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001110
Lorenzo Colitti107075a2017-10-30 19:24:46 +09001111// Protects sInterfaceToTable.
1112android::RWLock RouteController::sInterfaceToTableLock;
1113
Lorenzo Colitti02cb80a2017-10-30 19:21:06 +09001114std::map<std::string, uint32_t> RouteController::sInterfaceToTable;
1115
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001116} // namespace net
1117} // namespace android