blob: 19907eb9adadd8650ba9e260e6cc0c32632286dc [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 Colitti0b073fb2017-02-10 07:49:12 +0900315 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
316 strerror(-ret));
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900317 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700318 }
319 }
320
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900321 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700322}
323
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700324WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100325 uint32_t fwmark, uint32_t mask, const char* iif,
326 const char* oif, uid_t uidStart, uid_t uidEnd) {
327 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
328 uidEnd);
329}
330
331WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700332 uint32_t fwmark, uint32_t mask) {
333 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
334 INVALID_UID);
335}
336
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900337// Adds or deletes an IPv4 or IPv6 route.
338// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700339WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
340 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900341 // At least the destination must be non-null.
342 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700343 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900344 return -EFAULT;
345 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700346
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900347 // Parse the prefix.
348 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700349 uint8_t family;
350 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900351 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
352 &prefixLength);
353 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700354 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900355 return rawLength;
356 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700357
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900358 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700359 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900360 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
361 }
362
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700363 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900364 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900365 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700366
367 if (nexthop && !strcmp(nexthop, "unreachable")) {
368 type = RTN_UNREACHABLE;
369 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
370 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
371 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
372 interface = OIF_NONE;
373 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900374 } else if (nexthop && !strcmp(nexthop, "throw")) {
375 type = RTN_THROW;
376 interface = OIF_NONE;
377 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700378 } else {
379 // If an interface was specified, find the ifindex.
380 if (interface != OIF_NONE) {
381 ifindex = if_nametoindex(interface);
382 if (!ifindex) {
383 ALOGE("cannot find interface %s", interface);
384 return -ENODEV;
385 }
386 }
387
388 // If a nexthop was specified, parse it as the same family as the prefix.
389 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
390 ALOGE("inet_pton failed for nexthop %s", nexthop);
391 return -EINVAL;
392 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900393 }
394
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900395 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700396 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900397 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700398 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900399 .rtm_family = family,
400 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700401 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900402 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900403
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700404 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
405 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900406
407 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700408 { NULL, 0 },
409 { &route, sizeof(route) },
410 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
411 { &table, sizeof(table) },
412 { &rtaDst, sizeof(rtaDst) },
413 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700414 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
415 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700416 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
417 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900418 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900419
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700420 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
421 NETLINK_REQUEST_FLAGS;
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900422 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
423 if (ret) {
424 ALOGE("Error %s route %s -> %s %s to table %u: %s",
425 actionName(action), destination, nexthop, interface, table, strerror(-ret));
426 }
427 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700428}
429
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700430// An iptables rule to mark incoming packets on a network with the netId of the network.
431//
432// This is so that the kernel can:
433// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
434// replies, SYN-ACKs, etc).
435// + Mark sockets that accept connections from this interface so that the connection stays on the
436// same interface.
437WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
438 Permission permission, bool add) {
439 Fwmark fwmark;
440
441 fwmark.netId = netId;
442 fwmark.explicitlySelected = true;
443 fwmark.protectedFromVpn = true;
444 fwmark.permission = permission;
445
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700446 char markString[UINT32_HEX_STRLEN];
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700447 snprintf(markString, sizeof(markString), "0x%x", fwmark.intValue);
448
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700449 if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j",
450 "MARK", "--set-mark", markString, NULL)) {
451 ALOGE("failed to change iptables rule that sets incoming packet mark");
452 return -EREMOTEIO;
453 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700454
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700455 return 0;
456}
457
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700458// A rule to route responses to the local network forwarded via the VPN.
459//
460// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
461// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
462WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
463 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
464 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
465 INVALID_UID, INVALID_UID);
466}
467
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700468// A rule to route all traffic from a given set of UIDs to go over the VPN.
469//
470// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
471// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
472// bypass the VPN if the protectedFromVpn bit is set.
473WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700474 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700475 Fwmark fwmark;
476 Fwmark mask;
477
478 fwmark.protectedFromVpn = false;
479 mask.protectedFromVpn = true;
480
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700481 uint32_t priority;
482
483 if (secure) {
484 priority = RULE_PRIORITY_SECURE_VPN;
485 } else {
486 priority = RULE_PRIORITY_BYPASSABLE_VPN;
487
488 fwmark.explicitlySelected = false;
489 mask.explicitlySelected = true;
490 }
491
492 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900493 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700494}
495
496// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
497// set of UIDs.
498//
499// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
500// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700501WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
502 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700503 Fwmark fwmark;
504 Fwmark mask;
505
506 fwmark.netId = netId;
507 mask.netId = FWMARK_NET_ID_MASK;
508
509 fwmark.permission = PERMISSION_SYSTEM;
510 mask.permission = PERMISSION_SYSTEM;
511
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700512 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
513
514 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
515 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700516}
517
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700518// A rule to route traffic based on an explicitly chosen network.
519//
520// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
521//
522// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
523// to check it again in the rules here, because a network's permissions may have been updated via
524// modifyNetworkPermission().
525WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
526 Permission permission, uid_t uidStart,
527 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700528 Fwmark fwmark;
529 Fwmark mask;
530
531 fwmark.netId = netId;
532 mask.netId = FWMARK_NET_ID_MASK;
533
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700534 fwmark.explicitlySelected = true;
535 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700536
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700537 fwmark.permission = permission;
538 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700539
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700540 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700541 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700542}
543
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700544// A rule to route traffic based on a chosen outgoing interface.
545//
546// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
547// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900548WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
549 Permission permission, uid_t uidStart,
550 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700551 Fwmark fwmark;
552 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700553
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700554 fwmark.permission = permission;
555 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700556
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900557 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
558 // for UID. This covers forwarded packets and system daemons such as the tethering DHCP server.
559 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
560 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
561 table, fwmark.intValue, mask.intValue, IIF_NONE, interface,
562 UID_ROOT, UID_ROOT)) {
563 return ret;
564 }
565 }
566
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700567 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700568 fwmark.intValue, mask.intValue, IIF_NONE, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700569}
570
571// A rule to route traffic based on the chosen network.
572//
573// This is for sockets that have not explicitly requested a particular network, but have been
574// bound to one when they called connect(). This ensures that sockets connected on a particular
575// network stay on that network even if the default network changes.
576WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table,
577 Permission permission, bool add) {
578 Fwmark fwmark;
579 Fwmark mask;
580
581 fwmark.netId = netId;
582 mask.netId = FWMARK_NET_ID_MASK;
583
584 fwmark.explicitlySelected = false;
585 mask.explicitlySelected = true;
586
587 fwmark.permission = permission;
588 mask.permission = permission;
589
590 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700591 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700592}
593
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700594// A rule to enable split tunnel VPNs.
595//
596// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
597// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
598// permissions required by the default network.
599WARN_UNUSED_RESULT int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
600 const char* physicalInterface,
601 Permission permission) {
602 uint32_t table = getRouteTableForInterface(physicalInterface);
603 if (table == RT_TABLE_UNSPEC) {
604 return -ESRCH;
605 }
606
607 Fwmark fwmark;
608 Fwmark mask;
609
610 fwmark.netId = vpnNetId;
611 mask.netId = FWMARK_NET_ID_MASK;
612
613 fwmark.explicitlySelected = false;
614 mask.explicitlySelected = true;
615
616 fwmark.permission = permission;
617 mask.permission = permission;
618
619 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
620 mask.intValue);
621}
622
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700623// Add rules to allow legacy routes added through the requestRouteToHost() API.
624WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700625 Fwmark fwmark;
626 Fwmark mask;
627
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700628 fwmark.explicitlySelected = false;
629 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700630
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700631 // Rules to allow legacy routes to override the default network.
632 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
633 fwmark.intValue, mask.intValue)) {
634 return ret;
635 }
636 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
637 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
638 return ret;
639 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700640
641 fwmark.permission = PERMISSION_SYSTEM;
642 mask.permission = PERMISSION_SYSTEM;
643
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700644 // A rule to allow legacy routes from system apps to override VPNs.
645 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700646 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700647}
648
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700649// Add rules to lookup the local network when specified explicitly or otherwise.
650WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
651 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
652 INVALID_UID, INVALID_UID, ACTION_ADD)) {
653 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700654 }
655
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700656 Fwmark fwmark;
657 Fwmark mask;
658
659 fwmark.explicitlySelected = false;
660 mask.explicitlySelected = true;
661
662 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
663 fwmark.intValue, mask.intValue);
664}
665
Lorenzo Colitti36679362015-02-25 10:26:19 +0900666int configureDummyNetwork() {
667 const char *interface = DummyNetwork::INTERFACE_NAME;
668 uint32_t table = getRouteTableForInterface(interface);
669 if (table == RT_TABLE_UNSPEC) {
670 // getRouteTableForInterface has already looged an error.
671 return -ESRCH;
672 }
673
674 ifc_init();
675 int ret = ifc_up(interface);
676 ifc_close();
677 if (ret) {
678 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
679 return -errno;
680 }
681
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900682 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
683 INVALID_UID, INVALID_UID, ACTION_ADD))) {
684 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900685 return ret;
686 }
687
688 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900689 return ret;
690 }
691
692 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900693 return ret;
694 }
695
696 return 0;
697}
698
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000699// 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 +0900700// rule, but with a lower priority. We will never create routes in the main table; it should only be
701// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
702// This is necessary, for example, when adding a route through a directly-connected gateway: in
703// order to add the route, there must already be a directly-connected route that covers the gateway.
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000704WARN_UNUSED_RESULT int addDirectlyConnectedRule() {
705 Fwmark fwmark;
706 Fwmark mask;
707
708 fwmark.netId = NETID_UNSET;
709 mask.netId = FWMARK_NET_ID_MASK;
710
711 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
712 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT);
713}
714
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900715// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
716// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
717// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
718// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700719WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100720 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
721 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700722}
723
724WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700725 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
726 return ret;
727 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900728 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
729 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700730}
731
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700732WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
733 Permission permission, bool add) {
734 uint32_t table = getRouteTableForInterface(interface);
735 if (table == RT_TABLE_UNSPEC) {
736 return -ESRCH;
737 }
738
739 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
740 return ret;
741 }
742 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
743 add)) {
744 return ret;
745 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900746 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700747 add)) {
748 return ret;
749 }
750 return modifyImplicitNetworkRule(netId, table, permission, add);
751}
752
Robin Leeb8087362016-03-30 18:43:08 +0100753WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
754 Fwmark fwmark;
755 Fwmark mask;
756 fwmark.protectedFromVpn = false;
757 mask.protectedFromVpn = true;
758
Robin Leedc0d5782016-07-20 14:17:11 +0100759 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100760 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
761 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
762 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100763 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100764 return ret;
765 }
766 }
767
768 return 0;
769}
770
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700771WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700772 const UidRanges& uidRanges, bool secure, bool add,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700773 bool modifyNonUidBasedRules) {
774 uint32_t table = getRouteTableForInterface(interface);
775 if (table == RT_TABLE_UNSPEC) {
776 return -ESRCH;
777 }
778
Robin Leedc0d5782016-07-20 14:17:11 +0100779 for (const UidRange& range : uidRanges.getRanges()) {
780 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
781 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700782 return ret;
783 }
Robin Leedc0d5782016-07-20 14:17:11 +0100784 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
785 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700786 return ret;
787 }
Robin Leedc0d5782016-07-20 14:17:11 +0100788 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
789 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700790 return ret;
791 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700792 }
793
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700794 if (modifyNonUidBasedRules) {
795 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700796 return ret;
797 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700798 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
799 return ret;
800 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700801 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700802 return ret;
803 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700804 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700805 }
806
807 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700808}
809
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700810WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
811 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700812 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700813 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900814 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700815 }
816
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700817 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700818 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700819
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700820 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700821 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700822
823 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700824 mask.permission = permission;
825
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700826 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700827 mask.intValue);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700828}
829
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700830WARN_UNUSED_RESULT int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
831 const char* outputInterface) {
832 uint32_t table = getRouteTableForInterface(outputInterface);
833 if (table == RT_TABLE_UNSPEC) {
834 return -ESRCH;
835 }
836
837 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
838 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
839}
840
Lorenzo Colitti219f3282017-02-10 10:02:49 +0900841uint32_t getRulePriority(const nlmsghdr *nlh) {
842 uint32_t rta_len = RTM_PAYLOAD(nlh);
843 rtmsg *msg = reinterpret_cast<rtmsg *>(NLMSG_DATA(nlh));
844 rtattr *rta = reinterpret_cast<rtattr *> RTM_RTA(msg);
845 for (; RTA_OK(rta, rta_len); rta = RTA_NEXT(rta, rta_len)) {
846 if (rta->rta_type == FRA_PRIORITY) {
847 return *(static_cast<uint32_t *>(RTA_DATA(rta)));
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700848 }
849 }
850 return 0;
851}
852
Lorenzo Colitti219f3282017-02-10 10:02:49 +0900853// Returns 0 on success or negative errno on failure.
854WARN_UNUSED_RESULT int flushRules() {
855 int readSock = openRtNetlinkSocket();
856 if (readSock < 0) {
857 return readSock;
858 }
859
860 int writeSock = openRtNetlinkSocket();
861 if (writeSock < 0) {
862 close(readSock);
863 return writeSock;
864 }
865
866 NetlinkDumpCallback callback = [writeSock] (nlmsghdr *nlh) {
Lorenzo Colitti219f3282017-02-10 10:02:49 +0900867 // Don't touch rules at priority 0 because by default they are used for local input.
868 if (getRulePriority(nlh) == 0) return;
869
870 nlh->nlmsg_type = RTM_DELRULE;
871 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
872 if (write(writeSock, nlh, nlh->nlmsg_len) == -1) {
873 ALOGE("Error writing flush request: %s", strerror(errno));
874 return;
875 }
876
877 int ret = recvNetlinkAck(writeSock);
878 if (ret != 0 && ret != -ENOENT) {
879 ALOGW("Flushing rules: %s", strerror(-ret));
880 }
881 };
882
883 int ret = 0;
884 for (const int family : { AF_INET, AF_INET6 }) {
885 // struct fib_rule_hdr and struct rtmsg are functionally identical.
886 rtmsg rule = {
887 .rtm_family = static_cast<uint8_t>(family),
888 };
889 iovec iov[] = {
890 { NULL, 0 },
891 { &rule, sizeof(rule) },
892 };
893 uint16_t action = RTM_GETRULE;
894 uint16_t flags = NETLINK_DUMP_FLAGS;
895
896 if ((ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), callback)) != 0) {
897 break;
898 }
899 }
900
901 close(readSock);
902 close(writeSock);
903
904 return ret;
905}
906
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700907// 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 +0900908// route, to the main table as well.
909// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700910WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
911 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700912 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700913 switch (tableType) {
914 case RouteController::INTERFACE: {
915 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700916 if (table == RT_TABLE_UNSPEC) {
917 return -ESRCH;
918 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700919 break;
920 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700921 case RouteController::LOCAL_NETWORK: {
922 table = ROUTE_TABLE_LOCAL_NETWORK;
923 break;
924 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700925 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700926 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700927 break;
928 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700929 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700930 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700931 break;
932 }
933 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700934
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900935 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700936 // Trying to add a route that already exists shouldn't cause an error.
937 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900938 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700939 }
940
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900941 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700942}
943
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700944// Returns 0 on success or negative errno on failure.
945WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700946 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700947 if (table == RT_TABLE_UNSPEC) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700948 return -ESRCH;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700949 }
950
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900951 char tableString[UINT32_STRLEN];
952 snprintf(tableString, sizeof(tableString), "%u", table);
953
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900954 int ret = 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700955 for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900956 const char* argv[] = {
957 IP_PATH,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700958 IP_VERSIONS[i],
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700959 "route",
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900960 "flush",
961 "table",
962 tableString,
963 };
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900964
965 // A flush works by dumping routes and deleting each route as it's returned, and it can
966 // fail if something else deletes the route between the dump and the delete. This can
967 // happen, for example, if an interface goes down while we're trying to flush its routes.
968 // So try multiple times and only return an error if the last attempt fails.
969 //
970 // TODO: replace this with our own netlink code.
971 unsigned attempts = 0;
972 int err;
973 do {
974 err = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv),
975 NULL, false, false);
976 ++attempts;
977 } while (err != 0 && attempts < ROUTE_FLUSH_ATTEMPTS);
978 if (err) {
979 ALOGE("failed to flush %s routes in table %s after %d attempts",
980 IP_VERSIONS[i], tableString, attempts);
981 ret = -EREMOTEIO;
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900982 }
983 }
984
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900985 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
986 // track of its name.
987 if (!ret) {
988 interfaceToTable.erase(interface);
989 }
990
991 return ret;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700992}
993
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900994WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
995 int ret = 0;
996 while (ret == 0) {
997 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
998 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
999 }
1000
1001 if (ret == -ENOENT) {
1002 return 0;
1003 } else {
1004 return ret;
1005 }
1006}
1007
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001008} // namespace
1009
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001010int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -07001011 if (int ret = flushRules()) {
1012 return ret;
1013 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001014 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001015 return ret;
1016 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001017 if (int ret = addLocalNetworkRules(localNetId)) {
1018 return ret;
1019 }
Lorenzo Colittia2c23052014-07-29 09:25:44 +00001020 if (int ret = addDirectlyConnectedRule()) {
1021 return ret;
1022 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -07001023 if (int ret = addUnreachableRule()) {
1024 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001025 }
Lorenzo Colitti36679362015-02-25 10:26:19 +09001026 // Don't complain if we can't add the dummy network, since not all devices support it.
1027 configureDummyNetwork();
1028
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001029 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001030 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -07001031}
1032
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001033int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001034 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001035}
1036
1037int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001038 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001039}
1040
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001041int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
1042 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001043 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
1044 return ret;
1045 }
1046 updateTableNamesFile();
1047 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001048}
1049
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001050int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
1051 Permission permission) {
1052 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001053 return ret;
1054 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001055 if (int ret = flushRoutes(interface)) {
1056 return ret;
1057 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001058 if (int ret = clearTetheringRules(interface)) {
1059 return ret;
1060 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001061 updateTableNamesFile();
1062 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001063}
1064
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001065int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001066 bool secure, const UidRanges& uidRanges) {
1067 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001068 MODIFY_NON_UID_BASED_RULES)) {
1069 return ret;
1070 }
1071 updateTableNamesFile();
1072 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001073}
1074
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001075int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001076 bool secure, const UidRanges& uidRanges) {
1077 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001078 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001079 return ret;
1080 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001081 if (int ret = flushRoutes(interface)) {
1082 return ret;
1083 }
1084 updateTableNamesFile();
1085 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001086}
1087
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001088int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1089 Permission oldPermission,
1090 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001091 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001092 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001093 return ret;
1094 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001095 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001096}
1097
Robin Leeb8087362016-03-30 18:43:08 +01001098int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1099 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1100}
1101
1102int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1103 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1104}
1105
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001106int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001107 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001108 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001109 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001110}
1111
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001112int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001113 bool secure, const UidRanges& uidRanges) {
1114 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001115 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001116}
1117
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001118int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1119 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001120}
1121
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001122int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1123 Permission permission) {
1124 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001125}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001126
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001127int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001128 TableType tableType) {
1129 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001130}
1131
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001132int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001133 const char* nexthop, TableType tableType) {
1134 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001135}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001136
1137int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001138 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001139}
1140
1141int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001142 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001143}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001144
1145int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1146 Permission permission) {
1147 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1148}
1149
1150int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1151 const char* physicalInterface,
1152 Permission permission) {
1153 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1154}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001155
1156} // namespace net
1157} // namespace android