blob: e1a0e8d611825e4190758a3f8b9f398af8a989db [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
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070095const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
96
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070097const uid_t UID_ROOT = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090098const char* const IIF_LOOPBACK = "lo";
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070099const char* const IIF_NONE = NULL;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700100const char* const OIF_NONE = NULL;
101const bool ACTION_ADD = true;
102const bool ACTION_DEL = false;
103const bool MODIFY_NON_UID_BASED_RULES = true;
104
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700105const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700106const 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 -0700107
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700108// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
109// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
110constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
111 return RTA_LENGTH(x);
112}
113
114// These are practically const, but can't be declared so, because they are used to initialize
115// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900116rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
117rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
118rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
119rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
120rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START };
121rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END };
122rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700123
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900124rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
125rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700126
127uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
128
129// END CONSTANTS ----------------------------------------------------------------------------------
130
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900131const char *actionName(uint16_t action) {
132 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
133 return ops[action % 4];
134}
135
136const char *familyName(uint8_t family) {
137 switch (family) {
138 case AF_INET: return "IPv4";
139 case AF_INET6: return "IPv6";
140 default: return "???";
141 }
142}
143
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700144// No locks needed because RouteController is accessed only from one thread (in CommandListener).
145std::map<std::string, uint32_t> interfaceToTable;
Paul Jensena561e122014-06-12 16:46:37 -0400146
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700147uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700148 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400149 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700150 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
151 interfaceToTable[interface] = index;
152 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400153 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700154 // If the interface goes away if_nametoindex() will return 0 but we still need to know
155 // the index so we can remove the rules and routes.
156 auto iter = interfaceToTable.find(interface);
157 if (iter == interfaceToTable.end()) {
158 ALOGE("cannot find interface %s", interface);
159 return RT_TABLE_UNSPEC;
160 }
161 return iter->second;
162}
163
164void addTableName(uint32_t table, const std::string& name, std::string* contents) {
165 char tableString[UINT32_STRLEN];
166 snprintf(tableString, sizeof(tableString), "%u", table);
167 *contents += tableString;
168 *contents += " ";
169 *contents += name;
170 *contents += "\n";
171}
172
173// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
174void updateTableNamesFile() {
175 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700176
177 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
178 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
179
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700180 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700181 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
182 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700183
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700184 for (const auto& entry : interfaceToTable) {
185 addTableName(entry.second, entry.first, &contents);
186 }
187
Dan Albert5407e142015-03-16 10:05:59 -0700188 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800189 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700190 return;
191 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700192}
193
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700194// Returns 0 on success or negative errno on failure.
195int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
196 if (!input) {
197 *length = 0;
198 *padding = 0;
199 return 0;
200 }
201 *length = strlcpy(name, input, IFNAMSIZ) + 1;
202 if (*length > IFNAMSIZ) {
203 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
204 return -ENAMETOOLONG;
205 }
206 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
207 return 0;
208}
209
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700210// Adds or removes a routing rule for IPv4 and IPv6.
211//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900212// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100213// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700214// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
215// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700216// + If |iif| is non-NULL, the rule matches the specified incoming interface.
217// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
218// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
219// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900220//
221// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100222WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
223 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700224 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700225 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
226 if (fwmark & ~mask) {
227 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
228 return -ERANGE;
229 }
230
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700231 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900232 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700233 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
234 size_t iifLength, oifLength;
235 uint16_t iifPadding, oifPadding;
236 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
237 return ret;
238 }
239 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
240 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900241 }
242
Lorenzo Colitti59656512014-06-25 03:20:29 +0900243 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700244 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700245 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900246 return -EUSERS;
247 }
Robin Lee4ef94642016-04-01 11:50:49 +0100248
Lorenzo Colitti72723682014-06-26 13:51:10 +0900249 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900250
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900251 // Assemble a rule request and put it in an array of iovec structures.
252 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100253 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900254 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
255 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900256 };
257
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900258 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
259 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
260 // table 0, it's a wildcard that matches anything.
261 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
262 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
263 return -ENOTUNIQ;
264 }
265
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700266 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
267 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900268 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900269
270 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700271 { NULL, 0 },
272 { &rule, sizeof(rule) },
273 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
274 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700275 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
276 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700277 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
278 { &fwmark, mask ? sizeof(fwmark) : 0 },
279 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
280 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900281 // Rules that contain both legacy and new UID routing attributes will work on old kernels,
282 // which will simply ignore the FRA_UID_RANGE attribute since it is larger than their
283 // FRA_MAX. They will also work on kernels that are not too new:
284 // - FRA_UID_START clashes with FRA_PAD in 4.7, but that shouldn't be a problem because
285 // FRA_PAD has no validation.
286 // - FRA_UID_END clashes with FRA_L3MDEV in 4.8 and above, and will cause an error because
287 // FRA_L3MDEV has a maximum length of 1.
288 // TODO: delete the legacy UID routing code before running it on 4.8 or above.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700289 { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
290 { &uidStart, isUidRule ? sizeof(uidStart) : 0 },
291 { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 },
292 { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900293 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
294 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700295 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
296 { iifName, iifLength },
297 { PADDING_BUFFER, iifPadding },
298 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
299 { oifName, oifLength },
300 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900301 };
302
Lorenzo Colitti72723682014-06-26 13:51:10 +0900303 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700304 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
305 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900306 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900307 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
308 // Don't log when deleting a tethering rule that's not there. This matches the
309 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
310 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
311 strerror(-ret));
312 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900313 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700314 }
315 }
316
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900317 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700318}
319
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700320WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100321 uint32_t fwmark, uint32_t mask, const char* iif,
322 const char* oif, uid_t uidStart, uid_t uidEnd) {
323 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
324 uidEnd);
325}
326
327WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700328 uint32_t fwmark, uint32_t mask) {
329 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
330 INVALID_UID);
331}
332
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900333// Adds or deletes an IPv4 or IPv6 route.
334// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700335WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
336 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900337 // At least the destination must be non-null.
338 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700339 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900340 return -EFAULT;
341 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700342
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900343 // Parse the prefix.
344 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700345 uint8_t family;
346 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900347 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
348 &prefixLength);
349 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700350 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900351 return rawLength;
352 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700353
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900354 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700355 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900356 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
357 }
358
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700359 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900360 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900361 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700362
363 if (nexthop && !strcmp(nexthop, "unreachable")) {
364 type = RTN_UNREACHABLE;
365 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
366 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
367 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
368 interface = OIF_NONE;
369 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900370 } else if (nexthop && !strcmp(nexthop, "throw")) {
371 type = RTN_THROW;
372 interface = OIF_NONE;
373 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700374 } else {
375 // If an interface was specified, find the ifindex.
376 if (interface != OIF_NONE) {
377 ifindex = if_nametoindex(interface);
378 if (!ifindex) {
379 ALOGE("cannot find interface %s", interface);
380 return -ENODEV;
381 }
382 }
383
384 // If a nexthop was specified, parse it as the same family as the prefix.
385 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
386 ALOGE("inet_pton failed for nexthop %s", nexthop);
387 return -EINVAL;
388 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900389 }
390
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900391 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700392 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900393 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700394 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900395 .rtm_family = family,
396 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700397 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900398 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900399
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700400 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
401 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900402
403 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700404 { NULL, 0 },
405 { &route, sizeof(route) },
406 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
407 { &table, sizeof(table) },
408 { &rtaDst, sizeof(rtaDst) },
409 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700410 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
411 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700412 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
413 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900414 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900415
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700416 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
417 NETLINK_REQUEST_FLAGS;
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900418 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900419 if (ret) {
420 ALOGE("Error %s route %s -> %s %s to table %u: %s",
421 actionName(action), destination, nexthop, interface, table, strerror(-ret));
422 }
423 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700424}
425
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700426// An iptables rule to mark incoming packets on a network with the netId of the network.
427//
428// This is so that the kernel can:
429// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
430// replies, SYN-ACKs, etc).
431// + Mark sockets that accept connections from this interface so that the connection stays on the
432// same interface.
433WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
434 Permission permission, bool add) {
435 Fwmark fwmark;
436
437 fwmark.netId = netId;
438 fwmark.explicitlySelected = true;
439 fwmark.protectedFromVpn = true;
440 fwmark.permission = permission;
441
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700442 char markString[UINT32_HEX_STRLEN];
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700443 snprintf(markString, sizeof(markString), "0x%x", fwmark.intValue);
444
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700445 if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j",
446 "MARK", "--set-mark", markString, NULL)) {
447 ALOGE("failed to change iptables rule that sets incoming packet mark");
448 return -EREMOTEIO;
449 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700450
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700451 return 0;
452}
453
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700454// A rule to route responses to the local network forwarded via the VPN.
455//
456// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
457// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
458WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
459 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
460 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
461 INVALID_UID, INVALID_UID);
462}
463
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700464// A rule to route all traffic from a given set of UIDs to go over the VPN.
465//
466// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
467// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
468// bypass the VPN if the protectedFromVpn bit is set.
469WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700470 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700471 Fwmark fwmark;
472 Fwmark mask;
473
474 fwmark.protectedFromVpn = false;
475 mask.protectedFromVpn = true;
476
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700477 uint32_t priority;
478
479 if (secure) {
480 priority = RULE_PRIORITY_SECURE_VPN;
481 } else {
482 priority = RULE_PRIORITY_BYPASSABLE_VPN;
483
484 fwmark.explicitlySelected = false;
485 mask.explicitlySelected = true;
486 }
487
488 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900489 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700490}
491
492// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
493// set of UIDs.
494//
495// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
496// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700497WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
498 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700499 Fwmark fwmark;
500 Fwmark mask;
501
502 fwmark.netId = netId;
503 mask.netId = FWMARK_NET_ID_MASK;
504
505 fwmark.permission = PERMISSION_SYSTEM;
506 mask.permission = PERMISSION_SYSTEM;
507
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700508 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
509
510 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
511 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700512}
513
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700514// A rule to route traffic based on an explicitly chosen network.
515//
516// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
517//
518// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
519// to check it again in the rules here, because a network's permissions may have been updated via
520// modifyNetworkPermission().
521WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
522 Permission permission, uid_t uidStart,
523 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700524 Fwmark fwmark;
525 Fwmark mask;
526
527 fwmark.netId = netId;
528 mask.netId = FWMARK_NET_ID_MASK;
529
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700530 fwmark.explicitlySelected = true;
531 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700532
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700533 fwmark.permission = permission;
534 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700535
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700536 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700537 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700538}
539
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700540// A rule to route traffic based on a chosen outgoing interface.
541//
542// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
543// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900544WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
545 Permission permission, uid_t uidStart,
546 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700547 Fwmark fwmark;
548 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700549
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700550 fwmark.permission = permission;
551 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700552
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900553 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
554 // for UID. This covers forwarded packets and system daemons such as the tethering DHCP server.
555 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
556 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
557 table, fwmark.intValue, mask.intValue, IIF_NONE, interface,
558 UID_ROOT, UID_ROOT)) {
559 return ret;
560 }
561 }
562
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700563 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700564 fwmark.intValue, mask.intValue, IIF_NONE, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700565}
566
567// A rule to route traffic based on the chosen network.
568//
569// This is for sockets that have not explicitly requested a particular network, but have been
570// bound to one when they called connect(). This ensures that sockets connected on a particular
571// network stay on that network even if the default network changes.
572WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table,
573 Permission permission, bool add) {
574 Fwmark fwmark;
575 Fwmark mask;
576
577 fwmark.netId = netId;
578 mask.netId = FWMARK_NET_ID_MASK;
579
580 fwmark.explicitlySelected = false;
581 mask.explicitlySelected = true;
582
583 fwmark.permission = permission;
584 mask.permission = permission;
585
586 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700587 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700588}
589
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700590// A rule to enable split tunnel VPNs.
591//
592// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
593// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
594// permissions required by the default network.
595WARN_UNUSED_RESULT int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
596 const char* physicalInterface,
597 Permission permission) {
598 uint32_t table = getRouteTableForInterface(physicalInterface);
599 if (table == RT_TABLE_UNSPEC) {
600 return -ESRCH;
601 }
602
603 Fwmark fwmark;
604 Fwmark mask;
605
606 fwmark.netId = vpnNetId;
607 mask.netId = FWMARK_NET_ID_MASK;
608
609 fwmark.explicitlySelected = false;
610 mask.explicitlySelected = true;
611
612 fwmark.permission = permission;
613 mask.permission = permission;
614
615 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
616 mask.intValue);
617}
618
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700619// Add rules to allow legacy routes added through the requestRouteToHost() API.
620WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700621 Fwmark fwmark;
622 Fwmark mask;
623
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700624 fwmark.explicitlySelected = false;
625 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700626
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700627 // Rules to allow legacy routes to override the default network.
628 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
629 fwmark.intValue, mask.intValue)) {
630 return ret;
631 }
632 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
633 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
634 return ret;
635 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700636
637 fwmark.permission = PERMISSION_SYSTEM;
638 mask.permission = PERMISSION_SYSTEM;
639
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700640 // A rule to allow legacy routes from system apps to override VPNs.
641 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700642 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700643}
644
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700645// Add rules to lookup the local network when specified explicitly or otherwise.
646WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
647 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
648 INVALID_UID, INVALID_UID, ACTION_ADD)) {
649 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700650 }
651
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700652 Fwmark fwmark;
653 Fwmark mask;
654
655 fwmark.explicitlySelected = false;
656 mask.explicitlySelected = true;
657
658 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
659 fwmark.intValue, mask.intValue);
660}
661
Lorenzo Colitti36679362015-02-25 10:26:19 +0900662int configureDummyNetwork() {
663 const char *interface = DummyNetwork::INTERFACE_NAME;
664 uint32_t table = getRouteTableForInterface(interface);
665 if (table == RT_TABLE_UNSPEC) {
666 // getRouteTableForInterface has already looged an error.
667 return -ESRCH;
668 }
669
670 ifc_init();
671 int ret = ifc_up(interface);
672 ifc_close();
673 if (ret) {
674 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
675 return -errno;
676 }
677
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900678 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
679 INVALID_UID, INVALID_UID, ACTION_ADD))) {
680 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900681 return ret;
682 }
683
684 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900685 return ret;
686 }
687
688 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900689 return ret;
690 }
691
692 return 0;
693}
694
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000695// 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 +0900696// rule, but with a lower priority. We will never create routes in the main table; it should only be
697// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
698// This is necessary, for example, when adding a route through a directly-connected gateway: in
699// order to add the route, there must already be a directly-connected route that covers the gateway.
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000700WARN_UNUSED_RESULT int addDirectlyConnectedRule() {
701 Fwmark fwmark;
702 Fwmark mask;
703
704 fwmark.netId = NETID_UNSET;
705 mask.netId = FWMARK_NET_ID_MASK;
706
707 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
708 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT);
709}
710
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900711// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
712// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
713// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
714// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700715WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100716 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
717 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700718}
719
720WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700721 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
722 return ret;
723 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900724 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
725 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700726}
727
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700728WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
729 Permission permission, bool add) {
730 uint32_t table = getRouteTableForInterface(interface);
731 if (table == RT_TABLE_UNSPEC) {
732 return -ESRCH;
733 }
734
735 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
736 return ret;
737 }
738 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
739 add)) {
740 return ret;
741 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900742 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700743 add)) {
744 return ret;
745 }
746 return modifyImplicitNetworkRule(netId, table, permission, add);
747}
748
Robin Leeb8087362016-03-30 18:43:08 +0100749WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
750 Fwmark fwmark;
751 Fwmark mask;
752 fwmark.protectedFromVpn = false;
753 mask.protectedFromVpn = true;
754
Robin Leedc0d5782016-07-20 14:17:11 +0100755 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100756 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
757 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
758 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100759 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100760 return ret;
761 }
762 }
763
764 return 0;
765}
766
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700767WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700768 const UidRanges& uidRanges, bool secure, bool add,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700769 bool modifyNonUidBasedRules) {
770 uint32_t table = getRouteTableForInterface(interface);
771 if (table == RT_TABLE_UNSPEC) {
772 return -ESRCH;
773 }
774
Robin Leedc0d5782016-07-20 14:17:11 +0100775 for (const UidRange& range : uidRanges.getRanges()) {
776 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
777 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700778 return ret;
779 }
Robin Leedc0d5782016-07-20 14:17:11 +0100780 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
781 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700782 return ret;
783 }
Robin Leedc0d5782016-07-20 14:17:11 +0100784 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
785 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700786 return ret;
787 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700788 }
789
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700790 if (modifyNonUidBasedRules) {
791 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700792 return ret;
793 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700794 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
795 return ret;
796 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700797 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700798 return ret;
799 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700800 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700801 }
802
803 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700804}
805
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700806WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
807 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700808 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700809 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900810 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700811 }
812
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700813 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700814 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700815
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700816 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700817 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700818
819 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700820 mask.permission = permission;
821
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700822 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700823 mask.intValue);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700824}
825
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700826WARN_UNUSED_RESULT int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
827 const char* outputInterface) {
828 uint32_t table = getRouteTableForInterface(outputInterface);
829 if (table == RT_TABLE_UNSPEC) {
830 return -ESRCH;
831 }
832
833 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
834 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
835}
836
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700837// 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 +0900838// route, to the main table as well.
839// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700840WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
841 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700842 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700843 switch (tableType) {
844 case RouteController::INTERFACE: {
845 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700846 if (table == RT_TABLE_UNSPEC) {
847 return -ESRCH;
848 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700849 break;
850 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700851 case RouteController::LOCAL_NETWORK: {
852 table = ROUTE_TABLE_LOCAL_NETWORK;
853 break;
854 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700855 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700856 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700857 break;
858 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700859 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700860 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700861 break;
862 }
863 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700864
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900865 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700866 // Trying to add a route that already exists shouldn't cause an error.
867 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900868 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700869 }
870
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900871 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700872}
873
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900874WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
875 int ret = 0;
876 while (ret == 0) {
877 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
878 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
879 }
880
881 if (ret == -ENOENT) {
882 return 0;
883 } else {
884 return ret;
885 }
886}
887
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900888uint32_t getRulePriority(const nlmsghdr *nlh) {
889 return getRtmU32Attribute(nlh, FRA_PRIORITY);
890}
891
892uint32_t getRouteTable(const nlmsghdr *nlh) {
893 return getRtmU32Attribute(nlh, RTA_TABLE);
894}
895
896WARN_UNUSED_RESULT int flushRules() {
897 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
898 // Don't touch rules at priority 0 because by default they are used for local input.
899 return getRulePriority(nlh) != 0;
900 };
901 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
902}
903
904WARN_UNUSED_RESULT int flushRoutes(uint32_t table) {
905 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
906 return getRouteTable(nlh) == table;
907 };
908
909 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
910}
911
912// Returns 0 on success or negative errno on failure.
913WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
914 uint32_t table = getRouteTableForInterface(interface);
915 if (table == RT_TABLE_UNSPEC) {
916 return -ESRCH;
917 }
918
919 int ret = flushRoutes(table);
920
921 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
922 // track of its name.
923 if (ret == 0) {
924 interfaceToTable.erase(interface);
925 }
926
927 return ret;
928}
929
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700930} // namespace
931
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700932int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700933 if (int ret = flushRules()) {
934 return ret;
935 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700936 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700937 return ret;
938 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700939 if (int ret = addLocalNetworkRules(localNetId)) {
940 return ret;
941 }
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000942 if (int ret = addDirectlyConnectedRule()) {
943 return ret;
944 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700945 if (int ret = addUnreachableRule()) {
946 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700947 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900948 // Don't complain if we can't add the dummy network, since not all devices support it.
949 configureDummyNetwork();
950
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700951 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700952 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700953}
954
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700955int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700956 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700957}
958
959int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700960 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700961}
962
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700963int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
964 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700965 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
966 return ret;
967 }
968 updateTableNamesFile();
969 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700970}
971
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700972int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
973 Permission permission) {
974 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700975 return ret;
976 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700977 if (int ret = flushRoutes(interface)) {
978 return ret;
979 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900980 if (int ret = clearTetheringRules(interface)) {
981 return ret;
982 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700983 updateTableNamesFile();
984 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700985}
986
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700987int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700988 bool secure, const UidRanges& uidRanges) {
989 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700990 MODIFY_NON_UID_BASED_RULES)) {
991 return ret;
992 }
993 updateTableNamesFile();
994 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700995}
996
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700997int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700998 bool secure, const UidRanges& uidRanges) {
999 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001000 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001001 return ret;
1002 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001003 if (int ret = flushRoutes(interface)) {
1004 return ret;
1005 }
1006 updateTableNamesFile();
1007 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001008}
1009
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001010int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1011 Permission oldPermission,
1012 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001013 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001014 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001015 return ret;
1016 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001017 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001018}
1019
Robin Leeb8087362016-03-30 18:43:08 +01001020int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1021 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1022}
1023
1024int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1025 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1026}
1027
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001028int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001029 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001030 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001031 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001032}
1033
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001034int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001035 bool secure, const UidRanges& uidRanges) {
1036 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001037 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001038}
1039
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001040int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1041 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001042}
1043
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001044int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1045 Permission permission) {
1046 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001047}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001048
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001049int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001050 TableType tableType) {
1051 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001052}
1053
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001054int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001055 const char* nexthop, TableType tableType) {
1056 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001057}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001058
1059int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001060 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001061}
1062
1063int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001064 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001065}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001066
1067int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1068 Permission permission) {
1069 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1070}
1071
1072int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1073 const char* physicalInterface,
1074 Permission permission) {
1075 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1076}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001077
1078} // namespace net
1079} // namespace android