blob: 87b0043c475c04aacbac8dfff538c27a3b10cb8e [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 Colitti1ef549d2017-02-13 18:32:09 +090032#include "NetlinkCommands.h"
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070033#include "UidRanges.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070034
Elliott Hughesbbd56262015-12-04 15:45:10 -080035#include "android-base/file.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070036#define LOG_TAG "Netd"
37#include "log/log.h"
38#include "logwrap/logwrap.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090039#include "netutils/ifc.h"
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070040#include "resolv_netid.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070041
Dan Albert5407e142015-03-16 10:05:59 -070042using android::base::WriteStringToFile;
Robin Leedc0d5782016-07-20 14:17:11 +010043using android::net::UidRange;
Dan Albert5407e142015-03-16 10:05:59 -070044
Lorenzo Colitti7035f222017-02-13 18:29:00 +090045namespace android {
46namespace net {
47
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070048namespace {
49
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070050// BEGIN CONSTANTS --------------------------------------------------------------------------------
51
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070052const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090053const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070054const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070055const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010056const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070057const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
58const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
59const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
60const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070061const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070062const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070063const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070064const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070065const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070066const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittia2c23052014-07-29 09:25:44 +000067const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 23000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090068const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070069
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070070const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070071const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
72const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
73
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070074const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070075const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
76const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
77
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070078const char* const ROUTE_TABLE_NAME_LOCAL = "local";
79const char* const ROUTE_TABLE_NAME_MAIN = "main";
80
Lorenzo Colitti2b078672016-12-16 18:45:03 +090081// TODO: These values aren't defined by the Linux kernel, because legacy UID routing (as used in N
82// and below) was not upstreamed. Now that the UID routing code is upstream, we should remove these
83// and rely on the kernel header values.
Lorenzo Colitti59656512014-06-25 03:20:29 +090084const uint16_t FRA_UID_START = 18;
85const uint16_t FRA_UID_END = 19;
Lorenzo Colitti2b078672016-12-16 18:45:03 +090086
87// These values are upstream, but not yet in our headers.
88// TODO: delete these definitions when updating the headers.
89const uint16_t FRA_UID_RANGE = 20;
90struct fib_rule_uid_range {
91 __u32 start;
92 __u32 end;
93};
Lorenzo Colitti59656512014-06-25 03:20:29 +090094
Lorenzo Colitti72723682014-06-26 13:51:10 +090095const uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK;
96const uint16_t NETLINK_CREATE_REQUEST_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE | NLM_F_EXCL;
Lorenzo Colitti219f3282017-02-10 10:02:49 +090097const uint16_t NETLINK_DUMP_FLAGS = NLM_F_REQUEST | NLM_F_DUMP;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090098
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070099const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
100
101const char* const IP_VERSIONS[] = {"-4", "-6"};
102
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700103const uid_t UID_ROOT = 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
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900114const unsigned ROUTE_FLUSH_ATTEMPTS = 2;
115
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700116// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
117// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
118constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
119 return RTA_LENGTH(x);
120}
121
122// These are practically const, but can't be declared so, because they are used to initialize
123// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900124rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
125rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
126rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
127rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
128rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START };
129rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END };
130rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700131
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900132rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
133rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700134
135uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
136
137// END CONSTANTS ----------------------------------------------------------------------------------
138
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900139const char *actionName(uint16_t action) {
140 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
141 return ops[action % 4];
142}
143
144const char *familyName(uint8_t family) {
145 switch (family) {
146 case AF_INET: return "IPv4";
147 case AF_INET6: return "IPv6";
148 default: return "???";
149 }
150}
151
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700152// No locks needed because RouteController is accessed only from one thread (in CommandListener).
153std::map<std::string, uint32_t> interfaceToTable;
Paul Jensena561e122014-06-12 16:46:37 -0400154
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700155uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700156 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400157 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700158 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
159 interfaceToTable[interface] = index;
160 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400161 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700162 // If the interface goes away if_nametoindex() will return 0 but we still need to know
163 // the index so we can remove the rules and routes.
164 auto iter = interfaceToTable.find(interface);
165 if (iter == interfaceToTable.end()) {
166 ALOGE("cannot find interface %s", interface);
167 return RT_TABLE_UNSPEC;
168 }
169 return iter->second;
170}
171
172void addTableName(uint32_t table, const std::string& name, std::string* contents) {
173 char tableString[UINT32_STRLEN];
174 snprintf(tableString, sizeof(tableString), "%u", table);
175 *contents += tableString;
176 *contents += " ";
177 *contents += name;
178 *contents += "\n";
179}
180
181// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
182void updateTableNamesFile() {
183 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700184
185 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
186 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
187
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700188 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700189 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
190 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700191
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700192 for (const auto& entry : interfaceToTable) {
193 addTableName(entry.second, entry.first, &contents);
194 }
195
Dan Albert5407e142015-03-16 10:05:59 -0700196 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800197 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700198 return;
199 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700200}
201
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700202// Returns 0 on success or negative errno on failure.
203int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
204 if (!input) {
205 *length = 0;
206 *padding = 0;
207 return 0;
208 }
209 *length = strlcpy(name, input, IFNAMSIZ) + 1;
210 if (*length > IFNAMSIZ) {
211 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
212 return -ENAMETOOLONG;
213 }
214 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
215 return 0;
216}
217
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700218// Adds or removes a routing rule for IPv4 and IPv6.
219//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900220// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100221// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700222// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
223// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700224// + If |iif| is non-NULL, the rule matches the specified incoming interface.
225// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
226// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
227// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900228//
229// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100230WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
231 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700232 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700233 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
234 if (fwmark & ~mask) {
235 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
236 return -ERANGE;
237 }
238
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700239 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900240 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700241 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
242 size_t iifLength, oifLength;
243 uint16_t iifPadding, oifPadding;
244 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
245 return ret;
246 }
247 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
248 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900249 }
250
Lorenzo Colitti59656512014-06-25 03:20:29 +0900251 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700252 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700253 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900254 return -EUSERS;
255 }
Robin Lee4ef94642016-04-01 11:50:49 +0100256
Lorenzo Colitti72723682014-06-26 13:51:10 +0900257 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900258
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900259 // Assemble a rule request and put it in an array of iovec structures.
260 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100261 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900262 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
263 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900264 };
265
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900266 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
267 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
268 // table 0, it's a wildcard that matches anything.
269 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
270 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
271 return -ENOTUNIQ;
272 }
273
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700274 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
275 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900276 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900277
278 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700279 { NULL, 0 },
280 { &rule, sizeof(rule) },
281 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
282 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700283 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
284 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700285 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
286 { &fwmark, mask ? sizeof(fwmark) : 0 },
287 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
288 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900289 // Rules that contain both legacy and new UID routing attributes will work on old kernels,
290 // which will simply ignore the FRA_UID_RANGE attribute since it is larger than their
291 // FRA_MAX. They will also work on kernels that are not too new:
292 // - FRA_UID_START clashes with FRA_PAD in 4.7, but that shouldn't be a problem because
293 // FRA_PAD has no validation.
294 // - FRA_UID_END clashes with FRA_L3MDEV in 4.8 and above, and will cause an error because
295 // FRA_L3MDEV has a maximum length of 1.
296 // TODO: delete the legacy UID routing code before running it on 4.8 or above.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700297 { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
298 { &uidStart, isUidRule ? sizeof(uidStart) : 0 },
299 { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 },
300 { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900301 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
302 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700303 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
304 { iifName, iifLength },
305 { PADDING_BUFFER, iifPadding },
306 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
307 { oifName, oifLength },
308 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900309 };
310
Lorenzo Colitti72723682014-06-26 13:51:10 +0900311 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700312 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
313 rule.family = AF_FAMILIES[i];
314 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov))) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900315 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
316 // Don't log when deleting a tethering rule that's not there. This matches the
317 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
318 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
319 strerror(-ret));
320 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900321 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700322 }
323 }
324
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900325 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700326}
327
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700328WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100329 uint32_t fwmark, uint32_t mask, const char* iif,
330 const char* oif, uid_t uidStart, uid_t uidEnd) {
331 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
332 uidEnd);
333}
334
335WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700336 uint32_t fwmark, uint32_t mask) {
337 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
338 INVALID_UID);
339}
340
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900341// Adds or deletes an IPv4 or IPv6 route.
342// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700343WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
344 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900345 // At least the destination must be non-null.
346 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700347 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900348 return -EFAULT;
349 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700350
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900351 // Parse the prefix.
352 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700353 uint8_t family;
354 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900355 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
356 &prefixLength);
357 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700358 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900359 return rawLength;
360 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700361
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900362 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700363 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900364 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
365 }
366
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700367 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900368 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900369 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700370
371 if (nexthop && !strcmp(nexthop, "unreachable")) {
372 type = RTN_UNREACHABLE;
373 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
374 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
375 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
376 interface = OIF_NONE;
377 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900378 } else if (nexthop && !strcmp(nexthop, "throw")) {
379 type = RTN_THROW;
380 interface = OIF_NONE;
381 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700382 } else {
383 // If an interface was specified, find the ifindex.
384 if (interface != OIF_NONE) {
385 ifindex = if_nametoindex(interface);
386 if (!ifindex) {
387 ALOGE("cannot find interface %s", interface);
388 return -ENODEV;
389 }
390 }
391
392 // If a nexthop was specified, parse it as the same family as the prefix.
393 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
394 ALOGE("inet_pton failed for nexthop %s", nexthop);
395 return -EINVAL;
396 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900397 }
398
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900399 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700400 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900401 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700402 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900403 .rtm_family = family,
404 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700405 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900406 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900407
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700408 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
409 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900410
411 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700412 { NULL, 0 },
413 { &route, sizeof(route) },
414 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
415 { &table, sizeof(table) },
416 { &rtaDst, sizeof(rtaDst) },
417 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700418 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
419 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700420 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
421 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900422 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900423
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700424 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
425 NETLINK_REQUEST_FLAGS;
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900426 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
427 if (ret) {
428 ALOGE("Error %s route %s -> %s %s to table %u: %s",
429 actionName(action), destination, nexthop, interface, table, strerror(-ret));
430 }
431 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700432}
433
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700434// An iptables rule to mark incoming packets on a network with the netId of the network.
435//
436// This is so that the kernel can:
437// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
438// replies, SYN-ACKs, etc).
439// + Mark sockets that accept connections from this interface so that the connection stays on the
440// same interface.
441WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
442 Permission permission, bool add) {
443 Fwmark fwmark;
444
445 fwmark.netId = netId;
446 fwmark.explicitlySelected = true;
447 fwmark.protectedFromVpn = true;
448 fwmark.permission = permission;
449
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700450 char markString[UINT32_HEX_STRLEN];
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700451 snprintf(markString, sizeof(markString), "0x%x", fwmark.intValue);
452
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700453 if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j",
454 "MARK", "--set-mark", markString, NULL)) {
455 ALOGE("failed to change iptables rule that sets incoming packet mark");
456 return -EREMOTEIO;
457 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700458
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700459 return 0;
460}
461
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700462// A rule to route responses to the local network forwarded via the VPN.
463//
464// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
465// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
466WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
467 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
468 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
469 INVALID_UID, INVALID_UID);
470}
471
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700472// A rule to route all traffic from a given set of UIDs to go over the VPN.
473//
474// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
475// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
476// bypass the VPN if the protectedFromVpn bit is set.
477WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700478 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700479 Fwmark fwmark;
480 Fwmark mask;
481
482 fwmark.protectedFromVpn = false;
483 mask.protectedFromVpn = true;
484
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700485 uint32_t priority;
486
487 if (secure) {
488 priority = RULE_PRIORITY_SECURE_VPN;
489 } else {
490 priority = RULE_PRIORITY_BYPASSABLE_VPN;
491
492 fwmark.explicitlySelected = false;
493 mask.explicitlySelected = true;
494 }
495
496 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900497 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700498}
499
500// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
501// set of UIDs.
502//
503// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
504// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700505WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
506 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700507 Fwmark fwmark;
508 Fwmark mask;
509
510 fwmark.netId = netId;
511 mask.netId = FWMARK_NET_ID_MASK;
512
513 fwmark.permission = PERMISSION_SYSTEM;
514 mask.permission = PERMISSION_SYSTEM;
515
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700516 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
517
518 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
519 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700520}
521
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700522// A rule to route traffic based on an explicitly chosen network.
523//
524// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
525//
526// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
527// to check it again in the rules here, because a network's permissions may have been updated via
528// modifyNetworkPermission().
529WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
530 Permission permission, uid_t uidStart,
531 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700532 Fwmark fwmark;
533 Fwmark mask;
534
535 fwmark.netId = netId;
536 mask.netId = FWMARK_NET_ID_MASK;
537
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700538 fwmark.explicitlySelected = true;
539 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700540
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700541 fwmark.permission = permission;
542 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700543
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700544 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700545 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700546}
547
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700548// A rule to route traffic based on a chosen outgoing interface.
549//
550// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
551// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900552WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
553 Permission permission, uid_t uidStart,
554 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700555 Fwmark fwmark;
556 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700557
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700558 fwmark.permission = permission;
559 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700560
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900561 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
562 // for UID. This covers forwarded packets and system daemons such as the tethering DHCP server.
563 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
564 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
565 table, fwmark.intValue, mask.intValue, IIF_NONE, interface,
566 UID_ROOT, UID_ROOT)) {
567 return ret;
568 }
569 }
570
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700571 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700572 fwmark.intValue, mask.intValue, IIF_NONE, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700573}
574
575// A rule to route traffic based on the chosen network.
576//
577// This is for sockets that have not explicitly requested a particular network, but have been
578// bound to one when they called connect(). This ensures that sockets connected on a particular
579// network stay on that network even if the default network changes.
580WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table,
581 Permission permission, bool add) {
582 Fwmark fwmark;
583 Fwmark mask;
584
585 fwmark.netId = netId;
586 mask.netId = FWMARK_NET_ID_MASK;
587
588 fwmark.explicitlySelected = false;
589 mask.explicitlySelected = true;
590
591 fwmark.permission = permission;
592 mask.permission = permission;
593
594 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700595 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700596}
597
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700598// A rule to enable split tunnel VPNs.
599//
600// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
601// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
602// permissions required by the default network.
603WARN_UNUSED_RESULT int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
604 const char* physicalInterface,
605 Permission permission) {
606 uint32_t table = getRouteTableForInterface(physicalInterface);
607 if (table == RT_TABLE_UNSPEC) {
608 return -ESRCH;
609 }
610
611 Fwmark fwmark;
612 Fwmark mask;
613
614 fwmark.netId = vpnNetId;
615 mask.netId = FWMARK_NET_ID_MASK;
616
617 fwmark.explicitlySelected = false;
618 mask.explicitlySelected = true;
619
620 fwmark.permission = permission;
621 mask.permission = permission;
622
623 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
624 mask.intValue);
625}
626
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700627// Add rules to allow legacy routes added through the requestRouteToHost() API.
628WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700629 Fwmark fwmark;
630 Fwmark mask;
631
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700632 fwmark.explicitlySelected = false;
633 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700634
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700635 // Rules to allow legacy routes to override the default network.
636 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
637 fwmark.intValue, mask.intValue)) {
638 return ret;
639 }
640 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
641 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
642 return ret;
643 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700644
645 fwmark.permission = PERMISSION_SYSTEM;
646 mask.permission = PERMISSION_SYSTEM;
647
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700648 // A rule to allow legacy routes from system apps to override VPNs.
649 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700650 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700651}
652
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700653// Add rules to lookup the local network when specified explicitly or otherwise.
654WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
655 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
656 INVALID_UID, INVALID_UID, ACTION_ADD)) {
657 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700658 }
659
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700660 Fwmark fwmark;
661 Fwmark mask;
662
663 fwmark.explicitlySelected = false;
664 mask.explicitlySelected = true;
665
666 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
667 fwmark.intValue, mask.intValue);
668}
669
Lorenzo Colitti36679362015-02-25 10:26:19 +0900670int configureDummyNetwork() {
671 const char *interface = DummyNetwork::INTERFACE_NAME;
672 uint32_t table = getRouteTableForInterface(interface);
673 if (table == RT_TABLE_UNSPEC) {
674 // getRouteTableForInterface has already looged an error.
675 return -ESRCH;
676 }
677
678 ifc_init();
679 int ret = ifc_up(interface);
680 ifc_close();
681 if (ret) {
682 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
683 return -errno;
684 }
685
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900686 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
687 INVALID_UID, INVALID_UID, ACTION_ADD))) {
688 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900689 return ret;
690 }
691
692 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900693 return ret;
694 }
695
696 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900697 return ret;
698 }
699
700 return 0;
701}
702
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000703// Add a new rule to look up the 'main' table, with the same selectors as the "default network"
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900704// rule, but with a lower priority. We will never create routes in the main table; it should only be
705// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
706// This is necessary, for example, when adding a route through a directly-connected gateway: in
707// order to add the route, there must already be a directly-connected route that covers the gateway.
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000708WARN_UNUSED_RESULT int addDirectlyConnectedRule() {
709 Fwmark fwmark;
710 Fwmark mask;
711
712 fwmark.netId = NETID_UNSET;
713 mask.netId = FWMARK_NET_ID_MASK;
714
715 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
716 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT);
717}
718
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900719// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
720// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
721// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
722// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700723WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100724 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
725 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700726}
727
728WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700729 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
730 return ret;
731 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900732 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
733 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700734}
735
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700736WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
737 Permission permission, bool add) {
738 uint32_t table = getRouteTableForInterface(interface);
739 if (table == RT_TABLE_UNSPEC) {
740 return -ESRCH;
741 }
742
743 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
744 return ret;
745 }
746 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
747 add)) {
748 return ret;
749 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900750 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700751 add)) {
752 return ret;
753 }
754 return modifyImplicitNetworkRule(netId, table, permission, add);
755}
756
Robin Leeb8087362016-03-30 18:43:08 +0100757WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
758 Fwmark fwmark;
759 Fwmark mask;
760 fwmark.protectedFromVpn = false;
761 mask.protectedFromVpn = true;
762
Robin Leedc0d5782016-07-20 14:17:11 +0100763 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100764 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
765 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
766 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100767 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100768 return ret;
769 }
770 }
771
772 return 0;
773}
774
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700775WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700776 const UidRanges& uidRanges, bool secure, bool add,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700777 bool modifyNonUidBasedRules) {
778 uint32_t table = getRouteTableForInterface(interface);
779 if (table == RT_TABLE_UNSPEC) {
780 return -ESRCH;
781 }
782
Robin Leedc0d5782016-07-20 14:17:11 +0100783 for (const UidRange& range : uidRanges.getRanges()) {
784 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
785 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700786 return ret;
787 }
Robin Leedc0d5782016-07-20 14:17:11 +0100788 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
789 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700790 return ret;
791 }
Robin Leedc0d5782016-07-20 14:17:11 +0100792 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
793 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700794 return ret;
795 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700796 }
797
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700798 if (modifyNonUidBasedRules) {
799 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700800 return ret;
801 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700802 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
803 return ret;
804 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700805 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700806 return ret;
807 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700808 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700809 }
810
811 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700812}
813
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700814WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
815 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700816 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700817 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900818 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700819 }
820
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700821 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700822 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700823
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700824 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700825 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700826
827 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700828 mask.permission = permission;
829
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700830 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700831 mask.intValue);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700832}
833
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700834WARN_UNUSED_RESULT int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
835 const char* outputInterface) {
836 uint32_t table = getRouteTableForInterface(outputInterface);
837 if (table == RT_TABLE_UNSPEC) {
838 return -ESRCH;
839 }
840
841 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
842 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
843}
844
Lorenzo Colitti219f3282017-02-10 10:02:49 +0900845uint32_t getRulePriority(const nlmsghdr *nlh) {
846 uint32_t rta_len = RTM_PAYLOAD(nlh);
847 rtmsg *msg = reinterpret_cast<rtmsg *>(NLMSG_DATA(nlh));
848 rtattr *rta = reinterpret_cast<rtattr *> RTM_RTA(msg);
849 for (; RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) {
850 if (rta->rta_type == FRA_PRIORITY) {
851 return *(static_cast<uint32_t *>(RTA_DATA(rta)));
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700852 }
853 }
854 return 0;
855}
856
Lorenzo Colitti219f3282017-02-10 10:02:49 +0900857// Returns 0 on success or negative errno on failure.
858WARN_UNUSED_RESULT int flushRules() {
859 int readSock = openRtNetlinkSocket();
860 if (readSock < 0) {
861 return readSock;
862 }
863
864 int writeSock = openRtNetlinkSocket();
865 if (writeSock < 0) {
866 close(readSock);
867 return writeSock;
868 }
869
870 NetlinkDumpCallback callback = [writeSock] (nlmsghdr *nlh) {
Lorenzo Colitti219f3282017-02-10 10:02:49 +0900871 // Don't touch rules at priority 0 because by default they are used for local input.
872 if (getRulePriority(nlh) == 0) return;
873
874 nlh->nlmsg_type = RTM_DELRULE;
875 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
876 if (write(writeSock, nlh, nlh->nlmsg_len) == -1) {
877 ALOGE("Error writing flush request: %s", strerror(errno));
878 return;
879 }
880
881 int ret = recvNetlinkAck(writeSock);
882 if (ret != 0 && ret != -ENOENT) {
883 ALOGW("Flushing rules: %s", strerror(-ret));
884 }
885 };
886
887 int ret = 0;
888 for (const int family : { AF_INET, AF_INET6 }) {
889 // struct fib_rule_hdr and struct rtmsg are functionally identical.
890 rtmsg rule = {
891 .rtm_family = static_cast<uint8_t>(family),
892 };
893 iovec iov[] = {
894 { NULL, 0 },
895 { &rule, sizeof(rule) },
896 };
897 uint16_t action = RTM_GETRULE;
898 uint16_t flags = NETLINK_DUMP_FLAGS;
899
900 if ((ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), callback)) != 0) {
901 break;
902 }
903 }
904
905 close(readSock);
906 close(writeSock);
907
908 return ret;
909}
910
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700911// Adds or removes an IPv4 or IPv6 route to the specified table and, if it's a directly-connected
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900912// route, to the main table as well.
913// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700914WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
915 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700916 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700917 switch (tableType) {
918 case RouteController::INTERFACE: {
919 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700920 if (table == RT_TABLE_UNSPEC) {
921 return -ESRCH;
922 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700923 break;
924 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700925 case RouteController::LOCAL_NETWORK: {
926 table = ROUTE_TABLE_LOCAL_NETWORK;
927 break;
928 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700929 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700930 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700931 break;
932 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700933 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700934 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700935 break;
936 }
937 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700938
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900939 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700940 // Trying to add a route that already exists shouldn't cause an error.
941 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900942 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700943 }
944
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900945 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700946}
947
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700948// Returns 0 on success or negative errno on failure.
949WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700950 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700951 if (table == RT_TABLE_UNSPEC) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700952 return -ESRCH;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700953 }
954
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900955 char tableString[UINT32_STRLEN];
956 snprintf(tableString, sizeof(tableString), "%u", table);
957
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900958 int ret = 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700959 for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900960 const char* argv[] = {
961 IP_PATH,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700962 IP_VERSIONS[i],
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700963 "route",
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900964 "flush",
965 "table",
966 tableString,
967 };
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900968
969 // A flush works by dumping routes and deleting each route as it's returned, and it can
970 // fail if something else deletes the route between the dump and the delete. This can
971 // happen, for example, if an interface goes down while we're trying to flush its routes.
972 // So try multiple times and only return an error if the last attempt fails.
973 //
974 // TODO: replace this with our own netlink code.
975 unsigned attempts = 0;
976 int err;
977 do {
978 err = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv),
979 NULL, false, false);
980 ++attempts;
981 } while (err != 0 && attempts < ROUTE_FLUSH_ATTEMPTS);
982 if (err) {
983 ALOGE("failed to flush %s routes in table %s after %d attempts",
984 IP_VERSIONS[i], tableString, attempts);
985 ret = -EREMOTEIO;
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900986 }
987 }
988
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900989 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
990 // track of its name.
991 if (!ret) {
992 interfaceToTable.erase(interface);
993 }
994
995 return ret;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700996}
997
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900998WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
999 int ret = 0;
1000 while (ret == 0) {
1001 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
1002 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
1003 }
1004
1005 if (ret == -ENOENT) {
1006 return 0;
1007 } else {
1008 return ret;
1009 }
1010}
1011
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001012} // namespace
1013
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001014int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -07001015 if (int ret = flushRules()) {
1016 return ret;
1017 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001018 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001019 return ret;
1020 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001021 if (int ret = addLocalNetworkRules(localNetId)) {
1022 return ret;
1023 }
Lorenzo Colittia2c23052014-07-29 09:25:44 +00001024 if (int ret = addDirectlyConnectedRule()) {
1025 return ret;
1026 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -07001027 if (int ret = addUnreachableRule()) {
1028 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001029 }
Lorenzo Colitti36679362015-02-25 10:26:19 +09001030 // Don't complain if we can't add the dummy network, since not all devices support it.
1031 configureDummyNetwork();
1032
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001033 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001034 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -07001035}
1036
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001037int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001038 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001039}
1040
1041int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001042 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001043}
1044
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001045int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
1046 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001047 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
1048 return ret;
1049 }
1050 updateTableNamesFile();
1051 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001052}
1053
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001054int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
1055 Permission permission) {
1056 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001057 return ret;
1058 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001059 if (int ret = flushRoutes(interface)) {
1060 return ret;
1061 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001062 if (int ret = clearTetheringRules(interface)) {
1063 return ret;
1064 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001065 updateTableNamesFile();
1066 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001067}
1068
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001069int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001070 bool secure, const UidRanges& uidRanges) {
1071 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001072 MODIFY_NON_UID_BASED_RULES)) {
1073 return ret;
1074 }
1075 updateTableNamesFile();
1076 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001077}
1078
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001079int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001080 bool secure, const UidRanges& uidRanges) {
1081 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001082 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001083 return ret;
1084 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001085 if (int ret = flushRoutes(interface)) {
1086 return ret;
1087 }
1088 updateTableNamesFile();
1089 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001090}
1091
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001092int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1093 Permission oldPermission,
1094 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001095 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001096 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001097 return ret;
1098 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001099 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001100}
1101
Robin Leeb8087362016-03-30 18:43:08 +01001102int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1103 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1104}
1105
1106int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1107 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1108}
1109
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001110int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001111 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001112 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001113 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001114}
1115
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001116int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001117 bool secure, const UidRanges& uidRanges) {
1118 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001119 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001120}
1121
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001122int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1123 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001124}
1125
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001126int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1127 Permission permission) {
1128 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001129}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001130
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001131int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001132 TableType tableType) {
1133 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001134}
1135
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001136int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001137 const char* nexthop, TableType tableType) {
1138 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001139}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001140
1141int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001142 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001143}
1144
1145int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001146 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001147}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001148
1149int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1150 Permission permission) {
1151 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1152}
1153
1154int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1155 const char* physicalInterface,
1156 Permission permission) {
1157 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1158}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001159
1160} // namespace net
1161} // namespace android