blob: aba1458e7b0703b7f8eb8e36784828ed8c5e9a12 [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 Ramachandranf4f6c8d2014-06-23 09:54:06 -070048// BEGIN CONSTANTS --------------------------------------------------------------------------------
49
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070050const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090051const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070052const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070053const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010054const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070055const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
56const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
57const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
58const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070059const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070060const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070061const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070062const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070063const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070064const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittia2c23052014-07-29 09:25:44 +000065const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 23000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090066const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070067
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070068const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070069const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
70const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
71
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070072const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070073const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
74const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
75
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070076const char* const ROUTE_TABLE_NAME_LOCAL = "local";
77const char* const ROUTE_TABLE_NAME_MAIN = "main";
78
Lorenzo Colitti2b078672016-12-16 18:45:03 +090079// TODO: These values aren't defined by the Linux kernel, because legacy UID routing (as used in N
80// and below) was not upstreamed. Now that the UID routing code is upstream, we should remove these
81// and rely on the kernel header values.
Lorenzo Colitti59656512014-06-25 03:20:29 +090082const uint16_t FRA_UID_START = 18;
83const uint16_t FRA_UID_END = 19;
Lorenzo Colitti2b078672016-12-16 18:45:03 +090084
85// These values are upstream, but not yet in our headers.
86// TODO: delete these definitions when updating the headers.
87const uint16_t FRA_UID_RANGE = 20;
88struct fib_rule_uid_range {
89 __u32 start;
90 __u32 end;
91};
Lorenzo Colitti59656512014-06-25 03:20:29 +090092
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070093const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
94
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070095const uid_t UID_ROOT = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090096const char* const IIF_LOOPBACK = "lo";
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070097const char* const IIF_NONE = NULL;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070098const char* const OIF_NONE = NULL;
99const bool ACTION_ADD = true;
100const bool ACTION_DEL = false;
101const bool MODIFY_NON_UID_BASED_RULES = true;
102
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700103const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700104const 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 -0700105
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700106// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
107// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
108constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
109 return RTA_LENGTH(x);
110}
111
112// These are practically const, but can't be declared so, because they are used to initialize
113// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900114rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
115rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
116rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
117rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
118rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START };
119rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END };
120rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700121
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900122rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
123rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700124
125uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
126
127// END CONSTANTS ----------------------------------------------------------------------------------
128
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900129const char *actionName(uint16_t action) {
130 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
131 return ops[action % 4];
132}
133
134const char *familyName(uint8_t family) {
135 switch (family) {
136 case AF_INET: return "IPv4";
137 case AF_INET6: return "IPv6";
138 default: return "???";
139 }
140}
141
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700142// No locks needed because RouteController is accessed only from one thread (in CommandListener).
143std::map<std::string, uint32_t> interfaceToTable;
Paul Jensena561e122014-06-12 16:46:37 -0400144
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700145uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700146 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400147 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700148 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
149 interfaceToTable[interface] = index;
150 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400151 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700152 // If the interface goes away if_nametoindex() will return 0 but we still need to know
153 // the index so we can remove the rules and routes.
154 auto iter = interfaceToTable.find(interface);
155 if (iter == interfaceToTable.end()) {
156 ALOGE("cannot find interface %s", interface);
157 return RT_TABLE_UNSPEC;
158 }
159 return iter->second;
160}
161
162void addTableName(uint32_t table, const std::string& name, std::string* contents) {
163 char tableString[UINT32_STRLEN];
164 snprintf(tableString, sizeof(tableString), "%u", table);
165 *contents += tableString;
166 *contents += " ";
167 *contents += name;
168 *contents += "\n";
169}
170
171// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
172void updateTableNamesFile() {
173 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700174
175 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
176 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
177
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700178 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700179 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
180 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700181
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700182 for (const auto& entry : interfaceToTable) {
183 addTableName(entry.second, entry.first, &contents);
184 }
185
Dan Albert5407e142015-03-16 10:05:59 -0700186 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800187 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700188 return;
189 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700190}
191
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700192// Returns 0 on success or negative errno on failure.
193int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
194 if (!input) {
195 *length = 0;
196 *padding = 0;
197 return 0;
198 }
199 *length = strlcpy(name, input, IFNAMSIZ) + 1;
200 if (*length > IFNAMSIZ) {
201 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
202 return -ENAMETOOLONG;
203 }
204 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
205 return 0;
206}
207
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700208// Adds or removes a routing rule for IPv4 and IPv6.
209//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900210// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100211// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700212// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
213// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700214// + If |iif| is non-NULL, the rule matches the specified incoming interface.
215// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
216// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
217// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900218//
219// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100220WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
221 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700222 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700223 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
224 if (fwmark & ~mask) {
225 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
226 return -ERANGE;
227 }
228
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700229 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900230 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700231 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
232 size_t iifLength, oifLength;
233 uint16_t iifPadding, oifPadding;
234 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
235 return ret;
236 }
237 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
238 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900239 }
240
Lorenzo Colitti59656512014-06-25 03:20:29 +0900241 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700242 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700243 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900244 return -EUSERS;
245 }
Robin Lee4ef94642016-04-01 11:50:49 +0100246
Lorenzo Colitti72723682014-06-26 13:51:10 +0900247 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900248
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900249 // Assemble a rule request and put it in an array of iovec structures.
250 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100251 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900252 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
253 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900254 };
255
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900256 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
257 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
258 // table 0, it's a wildcard that matches anything.
259 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
260 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
261 return -ENOTUNIQ;
262 }
263
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700264 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
265 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900266 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900267
268 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700269 { NULL, 0 },
270 { &rule, sizeof(rule) },
271 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
272 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700273 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
274 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700275 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
276 { &fwmark, mask ? sizeof(fwmark) : 0 },
277 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
278 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900279 // Rules that contain both legacy and new UID routing attributes will work on old kernels,
280 // which will simply ignore the FRA_UID_RANGE attribute since it is larger than their
281 // FRA_MAX. They will also work on kernels that are not too new:
282 // - FRA_UID_START clashes with FRA_PAD in 4.7, but that shouldn't be a problem because
283 // FRA_PAD has no validation.
284 // - FRA_UID_END clashes with FRA_L3MDEV in 4.8 and above, and will cause an error because
285 // FRA_L3MDEV has a maximum length of 1.
286 // TODO: delete the legacy UID routing code before running it on 4.8 or above.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700287 { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
288 { &uidStart, isUidRule ? sizeof(uidStart) : 0 },
289 { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 },
290 { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900291 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
292 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700293 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
294 { iifName, iifLength },
295 { PADDING_BUFFER, iifPadding },
296 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
297 { oifName, oifLength },
298 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900299 };
300
Lorenzo Colitti72723682014-06-26 13:51:10 +0900301 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700302 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
303 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900304 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900305 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
306 // Don't log when deleting a tethering rule that's not there. This matches the
307 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
308 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
309 strerror(-ret));
310 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900311 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700312 }
313 }
314
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900315 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700316}
317
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700318WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100319 uint32_t fwmark, uint32_t mask, const char* iif,
320 const char* oif, uid_t uidStart, uid_t uidEnd) {
321 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
322 uidEnd);
323}
324
325WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700326 uint32_t fwmark, uint32_t mask) {
327 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
328 INVALID_UID);
329}
330
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900331// Adds or deletes an IPv4 or IPv6 route.
332// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700333WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
334 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900335 // At least the destination must be non-null.
336 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700337 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900338 return -EFAULT;
339 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700340
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900341 // Parse the prefix.
342 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700343 uint8_t family;
344 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900345 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
346 &prefixLength);
347 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700348 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900349 return rawLength;
350 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700351
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900352 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700353 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900354 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
355 }
356
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700357 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900358 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900359 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700360
361 if (nexthop && !strcmp(nexthop, "unreachable")) {
362 type = RTN_UNREACHABLE;
363 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
364 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
365 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
366 interface = OIF_NONE;
367 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900368 } else if (nexthop && !strcmp(nexthop, "throw")) {
369 type = RTN_THROW;
370 interface = OIF_NONE;
371 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700372 } else {
373 // If an interface was specified, find the ifindex.
374 if (interface != OIF_NONE) {
375 ifindex = if_nametoindex(interface);
376 if (!ifindex) {
377 ALOGE("cannot find interface %s", interface);
378 return -ENODEV;
379 }
380 }
381
382 // If a nexthop was specified, parse it as the same family as the prefix.
383 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
384 ALOGE("inet_pton failed for nexthop %s", nexthop);
385 return -EINVAL;
386 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900387 }
388
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900389 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700390 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900391 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700392 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900393 .rtm_family = family,
394 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700395 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900396 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900397
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700398 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
399 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900400
401 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700402 { NULL, 0 },
403 { &route, sizeof(route) },
404 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
405 { &table, sizeof(table) },
406 { &rtaDst, sizeof(rtaDst) },
407 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700408 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
409 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700410 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
411 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900412 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900413
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700414 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
415 NETLINK_REQUEST_FLAGS;
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900416 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900417 if (ret) {
418 ALOGE("Error %s route %s -> %s %s to table %u: %s",
419 actionName(action), destination, nexthop, interface, table, strerror(-ret));
420 }
421 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700422}
423
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700424// An iptables rule to mark incoming packets on a network with the netId of the network.
425//
426// This is so that the kernel can:
427// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
428// replies, SYN-ACKs, etc).
429// + Mark sockets that accept connections from this interface so that the connection stays on the
430// same interface.
431WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
432 Permission permission, bool add) {
433 Fwmark fwmark;
434
435 fwmark.netId = netId;
436 fwmark.explicitlySelected = true;
437 fwmark.protectedFromVpn = true;
438 fwmark.permission = permission;
439
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700440 char markString[UINT32_HEX_STRLEN];
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700441 snprintf(markString, sizeof(markString), "0x%x", fwmark.intValue);
442
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700443 if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j",
444 "MARK", "--set-mark", markString, NULL)) {
445 ALOGE("failed to change iptables rule that sets incoming packet mark");
446 return -EREMOTEIO;
447 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700448
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700449 return 0;
450}
451
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700452// A rule to route responses to the local network forwarded via the VPN.
453//
454// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
455// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
456WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
457 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
458 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
459 INVALID_UID, INVALID_UID);
460}
461
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700462// A rule to route all traffic from a given set of UIDs to go over the VPN.
463//
464// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
465// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
466// bypass the VPN if the protectedFromVpn bit is set.
467WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700468 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700469 Fwmark fwmark;
470 Fwmark mask;
471
472 fwmark.protectedFromVpn = false;
473 mask.protectedFromVpn = true;
474
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700475 uint32_t priority;
476
477 if (secure) {
478 priority = RULE_PRIORITY_SECURE_VPN;
479 } else {
480 priority = RULE_PRIORITY_BYPASSABLE_VPN;
481
482 fwmark.explicitlySelected = false;
483 mask.explicitlySelected = true;
484 }
485
486 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900487 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700488}
489
490// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
491// set of UIDs.
492//
493// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
494// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700495WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
496 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700497 Fwmark fwmark;
498 Fwmark mask;
499
500 fwmark.netId = netId;
501 mask.netId = FWMARK_NET_ID_MASK;
502
503 fwmark.permission = PERMISSION_SYSTEM;
504 mask.permission = PERMISSION_SYSTEM;
505
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700506 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
507
508 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
509 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700510}
511
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700512// A rule to route traffic based on an explicitly chosen network.
513//
514// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
515//
516// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
517// to check it again in the rules here, because a network's permissions may have been updated via
518// modifyNetworkPermission().
519WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
520 Permission permission, uid_t uidStart,
521 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700522 Fwmark fwmark;
523 Fwmark mask;
524
525 fwmark.netId = netId;
526 mask.netId = FWMARK_NET_ID_MASK;
527
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700528 fwmark.explicitlySelected = true;
529 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700530
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700531 fwmark.permission = permission;
532 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700533
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700534 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700535 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700536}
537
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700538// A rule to route traffic based on a chosen outgoing interface.
539//
540// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
541// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900542WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
543 Permission permission, uid_t uidStart,
544 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700545 Fwmark fwmark;
546 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700547
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700548 fwmark.permission = permission;
549 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700550
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900551 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
552 // for UID. This covers forwarded packets and system daemons such as the tethering DHCP server.
553 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
554 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
555 table, fwmark.intValue, mask.intValue, IIF_NONE, interface,
556 UID_ROOT, UID_ROOT)) {
557 return ret;
558 }
559 }
560
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700561 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700562 fwmark.intValue, mask.intValue, IIF_NONE, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700563}
564
565// A rule to route traffic based on the chosen network.
566//
567// This is for sockets that have not explicitly requested a particular network, but have been
568// bound to one when they called connect(). This ensures that sockets connected on a particular
569// network stay on that network even if the default network changes.
570WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table,
571 Permission permission, bool add) {
572 Fwmark fwmark;
573 Fwmark mask;
574
575 fwmark.netId = netId;
576 mask.netId = FWMARK_NET_ID_MASK;
577
578 fwmark.explicitlySelected = false;
579 mask.explicitlySelected = true;
580
581 fwmark.permission = permission;
582 mask.permission = permission;
583
584 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700585 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700586}
587
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700588// A rule to enable split tunnel VPNs.
589//
590// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
591// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
592// permissions required by the default network.
593WARN_UNUSED_RESULT int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
594 const char* physicalInterface,
595 Permission permission) {
596 uint32_t table = getRouteTableForInterface(physicalInterface);
597 if (table == RT_TABLE_UNSPEC) {
598 return -ESRCH;
599 }
600
601 Fwmark fwmark;
602 Fwmark mask;
603
604 fwmark.netId = vpnNetId;
605 mask.netId = FWMARK_NET_ID_MASK;
606
607 fwmark.explicitlySelected = false;
608 mask.explicitlySelected = true;
609
610 fwmark.permission = permission;
611 mask.permission = permission;
612
613 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
614 mask.intValue);
615}
616
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700617// Add rules to allow legacy routes added through the requestRouteToHost() API.
618WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700619 Fwmark fwmark;
620 Fwmark mask;
621
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700622 fwmark.explicitlySelected = false;
623 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700624
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700625 // Rules to allow legacy routes to override the default network.
626 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
627 fwmark.intValue, mask.intValue)) {
628 return ret;
629 }
630 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
631 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
632 return ret;
633 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700634
635 fwmark.permission = PERMISSION_SYSTEM;
636 mask.permission = PERMISSION_SYSTEM;
637
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700638 // A rule to allow legacy routes from system apps to override VPNs.
639 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700640 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700641}
642
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700643// Add rules to lookup the local network when specified explicitly or otherwise.
644WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
645 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
646 INVALID_UID, INVALID_UID, ACTION_ADD)) {
647 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700648 }
649
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700650 Fwmark fwmark;
651 Fwmark mask;
652
653 fwmark.explicitlySelected = false;
654 mask.explicitlySelected = true;
655
656 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
657 fwmark.intValue, mask.intValue);
658}
659
Lorenzo Colitti36679362015-02-25 10:26:19 +0900660int configureDummyNetwork() {
661 const char *interface = DummyNetwork::INTERFACE_NAME;
662 uint32_t table = getRouteTableForInterface(interface);
663 if (table == RT_TABLE_UNSPEC) {
664 // getRouteTableForInterface has already looged an error.
665 return -ESRCH;
666 }
667
668 ifc_init();
669 int ret = ifc_up(interface);
670 ifc_close();
671 if (ret) {
672 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
673 return -errno;
674 }
675
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900676 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
677 INVALID_UID, INVALID_UID, ACTION_ADD))) {
678 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900679 return ret;
680 }
681
682 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900683 return ret;
684 }
685
686 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900687 return ret;
688 }
689
690 return 0;
691}
692
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000693// 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 +0900694// rule, but with a lower priority. We will never create routes in the main table; it should only be
695// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
696// This is necessary, for example, when adding a route through a directly-connected gateway: in
697// order to add the route, there must already be a directly-connected route that covers the gateway.
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000698WARN_UNUSED_RESULT int addDirectlyConnectedRule() {
699 Fwmark fwmark;
700 Fwmark mask;
701
702 fwmark.netId = NETID_UNSET;
703 mask.netId = FWMARK_NET_ID_MASK;
704
705 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
706 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT);
707}
708
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900709// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
710// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
711// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
712// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700713WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100714 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
715 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700716}
717
718WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700719 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
720 return ret;
721 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900722 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
723 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700724}
725
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700726WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
727 Permission permission, bool add) {
728 uint32_t table = getRouteTableForInterface(interface);
729 if (table == RT_TABLE_UNSPEC) {
730 return -ESRCH;
731 }
732
733 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
734 return ret;
735 }
736 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
737 add)) {
738 return ret;
739 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900740 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700741 add)) {
742 return ret;
743 }
744 return modifyImplicitNetworkRule(netId, table, permission, add);
745}
746
Robin Leeb8087362016-03-30 18:43:08 +0100747WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
748 Fwmark fwmark;
749 Fwmark mask;
750 fwmark.protectedFromVpn = false;
751 mask.protectedFromVpn = true;
752
Robin Leedc0d5782016-07-20 14:17:11 +0100753 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100754 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
755 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
756 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100757 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100758 return ret;
759 }
760 }
761
762 return 0;
763}
764
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700765WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700766 const UidRanges& uidRanges, bool secure, bool add,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700767 bool modifyNonUidBasedRules) {
768 uint32_t table = getRouteTableForInterface(interface);
769 if (table == RT_TABLE_UNSPEC) {
770 return -ESRCH;
771 }
772
Robin Leedc0d5782016-07-20 14:17:11 +0100773 for (const UidRange& range : uidRanges.getRanges()) {
774 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
775 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700776 return ret;
777 }
Robin Leedc0d5782016-07-20 14:17:11 +0100778 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
779 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700780 return ret;
781 }
Robin Leedc0d5782016-07-20 14:17:11 +0100782 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
783 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700784 return ret;
785 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700786 }
787
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700788 if (modifyNonUidBasedRules) {
789 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700790 return ret;
791 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700792 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
793 return ret;
794 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700795 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700796 return ret;
797 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700798 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700799 }
800
801 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700802}
803
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700804WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
805 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700806 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700807 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900808 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700809 }
810
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700811 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700812 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700813
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700814 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700815 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700816
817 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700818 mask.permission = permission;
819
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700820 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700821 mask.intValue);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700822}
823
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700824WARN_UNUSED_RESULT int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
825 const char* outputInterface) {
826 uint32_t table = getRouteTableForInterface(outputInterface);
827 if (table == RT_TABLE_UNSPEC) {
828 return -ESRCH;
829 }
830
831 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
832 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
833}
834
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700835// 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 +0900836// route, to the main table as well.
837// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700838WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
839 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700840 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700841 switch (tableType) {
842 case RouteController::INTERFACE: {
843 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700844 if (table == RT_TABLE_UNSPEC) {
845 return -ESRCH;
846 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700847 break;
848 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700849 case RouteController::LOCAL_NETWORK: {
850 table = ROUTE_TABLE_LOCAL_NETWORK;
851 break;
852 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700853 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700854 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700855 break;
856 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700857 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700858 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700859 break;
860 }
861 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700862
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900863 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700864 // Trying to add a route that already exists shouldn't cause an error.
865 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900866 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700867 }
868
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900869 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700870}
871
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900872WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
873 int ret = 0;
874 while (ret == 0) {
875 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
876 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
877 }
878
879 if (ret == -ENOENT) {
880 return 0;
881 } else {
882 return ret;
883 }
884}
885
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900886uint32_t getRulePriority(const nlmsghdr *nlh) {
887 return getRtmU32Attribute(nlh, FRA_PRIORITY);
888}
889
890uint32_t getRouteTable(const nlmsghdr *nlh) {
891 return getRtmU32Attribute(nlh, RTA_TABLE);
892}
893
894WARN_UNUSED_RESULT int flushRules() {
895 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
896 // Don't touch rules at priority 0 because by default they are used for local input.
897 return getRulePriority(nlh) != 0;
898 };
899 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
900}
901
902WARN_UNUSED_RESULT int flushRoutes(uint32_t table) {
903 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
904 return getRouteTable(nlh) == table;
905 };
906
907 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
908}
909
910// Returns 0 on success or negative errno on failure.
911WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
912 uint32_t table = getRouteTableForInterface(interface);
913 if (table == RT_TABLE_UNSPEC) {
914 return -ESRCH;
915 }
916
917 int ret = flushRoutes(table);
918
919 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
920 // track of its name.
921 if (ret == 0) {
922 interfaceToTable.erase(interface);
923 }
924
925 return ret;
926}
927
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700928int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700929 if (int ret = flushRules()) {
930 return ret;
931 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700932 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700933 return ret;
934 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700935 if (int ret = addLocalNetworkRules(localNetId)) {
936 return ret;
937 }
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000938 if (int ret = addDirectlyConnectedRule()) {
939 return ret;
940 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700941 if (int ret = addUnreachableRule()) {
942 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700943 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900944 // Don't complain if we can't add the dummy network, since not all devices support it.
945 configureDummyNetwork();
946
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700947 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700948 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700949}
950
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700951int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700952 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700953}
954
955int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700956 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700957}
958
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700959int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
960 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700961 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
962 return ret;
963 }
964 updateTableNamesFile();
965 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700966}
967
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700968int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
969 Permission permission) {
970 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700971 return ret;
972 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700973 if (int ret = flushRoutes(interface)) {
974 return ret;
975 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900976 if (int ret = clearTetheringRules(interface)) {
977 return ret;
978 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700979 updateTableNamesFile();
980 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700981}
982
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700983int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700984 bool secure, const UidRanges& uidRanges) {
985 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700986 MODIFY_NON_UID_BASED_RULES)) {
987 return ret;
988 }
989 updateTableNamesFile();
990 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700991}
992
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700993int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700994 bool secure, const UidRanges& uidRanges) {
995 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700996 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700997 return ret;
998 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700999 if (int ret = flushRoutes(interface)) {
1000 return ret;
1001 }
1002 updateTableNamesFile();
1003 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001004}
1005
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001006int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1007 Permission oldPermission,
1008 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001009 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001010 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001011 return ret;
1012 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001013 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001014}
1015
Robin Leeb8087362016-03-30 18:43:08 +01001016int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1017 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1018}
1019
1020int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1021 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1022}
1023
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001024int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001025 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001026 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001027 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001028}
1029
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001030int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001031 bool secure, const UidRanges& uidRanges) {
1032 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001033 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001034}
1035
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001036int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1037 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001038}
1039
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001040int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1041 Permission permission) {
1042 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001043}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001044
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001045int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001046 TableType tableType) {
1047 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001048}
1049
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001050int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001051 const char* nexthop, TableType tableType) {
1052 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001053}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001054
1055int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001056 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001057}
1058
1059int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001060 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001061}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001062
1063int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1064 Permission permission) {
1065 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1066}
1067
1068int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1069 const char* physicalInterface,
1070 Permission permission) {
1071 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1072}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001073
1074} // namespace net
1075} // namespace android