blob: 27990759fa865ccb2e5d7c54220423053cc1407b [file] [log] [blame]
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "RouteController.h"
18
Dan Albertaa1be2b2015-01-06 09:36:17 -080019#include <arpa/inet.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <linux/fib_rules.h>
23#include <net/if.h>
24#include <sys/stat.h>
25
Elliott Hughesbd378322015-02-04 13:25:14 -080026#include <private/android_filesystem_config.h>
27
Dan Albertaa1be2b2015-01-06 09:36:17 -080028#include <map>
29
Lorenzo Colitti7035f222017-02-13 18:29:00 +090030#include "DummyNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070031#include "Fwmark.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090032#include "NetdConstants.h"
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090033#include "NetlinkCommands.h"
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070034#include "UidRanges.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070035
Elliott Hughesbbd56262015-12-04 15:45:10 -080036#include "android-base/file.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090037#include <android-base/stringprintf.h>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070038#define LOG_TAG "Netd"
39#include "log/log.h"
40#include "logwrap/logwrap.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090041#include "netutils/ifc.h"
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070042#include "resolv_netid.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070043
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090044using android::base::StringPrintf;
Dan Albert5407e142015-03-16 10:05:59 -070045using android::base::WriteStringToFile;
Robin Leedc0d5782016-07-20 14:17:11 +010046using android::net::UidRange;
Dan Albert5407e142015-03-16 10:05:59 -070047
Lorenzo Colitti7035f222017-02-13 18:29:00 +090048namespace android {
49namespace net {
50
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090051auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand;
52
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070053// BEGIN CONSTANTS --------------------------------------------------------------------------------
54
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070055const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090056const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070057const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070058const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010059const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070060const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
61const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
62const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
63const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070064const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070065const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070066const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070067const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070068const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070069const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090070const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070071
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070072const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070073const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
74const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
75
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070076const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070077const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
78const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
79
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070080const char* const ROUTE_TABLE_NAME_LOCAL = "local";
81const char* const ROUTE_TABLE_NAME_MAIN = "main";
82
Lorenzo Colittib5d19e92017-09-25 18:39:33 +090083// None of our regular routes specify priority, which causes them to have the default priority.
84// For default throw routes, we use a fixed priority of 100000.
Lorenzo Colitti5e03a892017-09-08 11:31:59 +090085uint32_t PRIO_THROW = 100000;
86
Lorenzo Colittid78843e2017-03-27 05:52:31 +090087const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
88
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070089const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
90
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070091const uid_t UID_ROOT = 0;
Lorenzo Colitti3093f562017-09-25 14:17:38 +090092const uint32_t FWMARK_NONE = 0;
93const uint32_t MASK_NONE = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090094const char* const IIF_LOOPBACK = "lo";
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070095const char* const IIF_NONE = NULL;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070096const char* const OIF_NONE = NULL;
97const bool ACTION_ADD = true;
98const bool ACTION_DEL = false;
99const bool MODIFY_NON_UID_BASED_RULES = true;
100
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700101const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700102const 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 -0700103
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700104// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
105// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
106constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
107 return RTA_LENGTH(x);
108}
109
110// These are practically const, but can't be declared so, because they are used to initialize
111// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900112rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
113rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
114rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
115rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900116rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700117
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900118rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
119rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900120rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700121
122uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
123
124// END CONSTANTS ----------------------------------------------------------------------------------
125
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900126const char *actionName(uint16_t action) {
127 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
128 return ops[action % 4];
129}
130
131const char *familyName(uint8_t family) {
132 switch (family) {
133 case AF_INET: return "IPv4";
134 case AF_INET6: return "IPv6";
135 default: return "???";
136 }
137}
138
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700139// No locks needed because RouteController is accessed only from one thread (in CommandListener).
140std::map<std::string, uint32_t> interfaceToTable;
Paul Jensena561e122014-06-12 16:46:37 -0400141
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700142uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700143 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400144 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700145 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
146 interfaceToTable[interface] = index;
147 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400148 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700149 // If the interface goes away if_nametoindex() will return 0 but we still need to know
150 // the index so we can remove the rules and routes.
151 auto iter = interfaceToTable.find(interface);
152 if (iter == interfaceToTable.end()) {
153 ALOGE("cannot find interface %s", interface);
154 return RT_TABLE_UNSPEC;
155 }
156 return iter->second;
157}
158
159void addTableName(uint32_t table, const std::string& name, std::string* contents) {
160 char tableString[UINT32_STRLEN];
161 snprintf(tableString, sizeof(tableString), "%u", table);
162 *contents += tableString;
163 *contents += " ";
164 *contents += name;
165 *contents += "\n";
166}
167
168// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
169void updateTableNamesFile() {
170 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700171
172 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
173 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
174
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700175 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700176 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
177 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700178
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700179 for (const auto& entry : interfaceToTable) {
180 addTableName(entry.second, entry.first, &contents);
181 }
182
Dan Albert5407e142015-03-16 10:05:59 -0700183 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800184 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700185 return;
186 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700187}
188
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700189// Returns 0 on success or negative errno on failure.
190int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
191 if (!input) {
192 *length = 0;
193 *padding = 0;
194 return 0;
195 }
196 *length = strlcpy(name, input, IFNAMSIZ) + 1;
197 if (*length > IFNAMSIZ) {
198 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
199 return -ENAMETOOLONG;
200 }
201 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
202 return 0;
203}
204
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700205// Adds or removes a routing rule for IPv4 and IPv6.
206//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900207// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100208// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700209// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
210// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700211// + If |iif| is non-NULL, the rule matches the specified incoming interface.
212// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
213// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
214// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900215//
216// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100217WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
218 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700219 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700220 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
221 if (fwmark & ~mask) {
222 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
223 return -ERANGE;
224 }
225
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700226 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900227 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700228 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
229 size_t iifLength, oifLength;
230 uint16_t iifPadding, oifPadding;
231 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
232 return ret;
233 }
234 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
235 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900236 }
237
Lorenzo Colitti59656512014-06-25 03:20:29 +0900238 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700239 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700240 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900241 return -EUSERS;
242 }
Robin Lee4ef94642016-04-01 11:50:49 +0100243
Lorenzo Colitti72723682014-06-26 13:51:10 +0900244 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900245
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900246 // Assemble a rule request and put it in an array of iovec structures.
247 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100248 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900249 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
250 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900251 };
252
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900253 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
254 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
255 // table 0, it's a wildcard that matches anything.
256 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
257 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
258 return -ENOTUNIQ;
259 }
260
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700261 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
262 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900263 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900264
265 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700266 { NULL, 0 },
267 { &rule, sizeof(rule) },
268 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
269 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700270 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
271 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700272 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
273 { &fwmark, mask ? sizeof(fwmark) : 0 },
274 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
275 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900276 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
277 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700278 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
279 { iifName, iifLength },
280 { PADDING_BUFFER, iifPadding },
281 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
282 { oifName, oifLength },
283 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900284 };
285
Lorenzo Colitti72723682014-06-26 13:51:10 +0900286 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700287 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
288 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900289 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900290 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
291 // Don't log when deleting a tethering rule that's not there. This matches the
292 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
293 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
294 strerror(-ret));
295 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900296 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700297 }
298 }
299
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900300 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700301}
302
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700303WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100304 uint32_t fwmark, uint32_t mask, const char* iif,
305 const char* oif, uid_t uidStart, uid_t uidEnd) {
306 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
307 uidEnd);
308}
309
310WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700311 uint32_t fwmark, uint32_t mask) {
312 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
313 INVALID_UID);
314}
315
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900316// Adds or deletes an IPv4 or IPv6 route.
317// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700318WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
319 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900320 // At least the destination must be non-null.
321 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700322 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900323 return -EFAULT;
324 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700325
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900326 // Parse the prefix.
327 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700328 uint8_t family;
329 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900330 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
331 &prefixLength);
332 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700333 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900334 return rawLength;
335 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700336
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900337 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700338 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900339 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
340 }
341
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700342 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900343 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900344 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700345
346 if (nexthop && !strcmp(nexthop, "unreachable")) {
347 type = RTN_UNREACHABLE;
348 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
349 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
350 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
351 interface = OIF_NONE;
352 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900353 } else if (nexthop && !strcmp(nexthop, "throw")) {
354 type = RTN_THROW;
355 interface = OIF_NONE;
356 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700357 } else {
358 // If an interface was specified, find the ifindex.
359 if (interface != OIF_NONE) {
360 ifindex = if_nametoindex(interface);
361 if (!ifindex) {
362 ALOGE("cannot find interface %s", interface);
363 return -ENODEV;
364 }
365 }
366
367 // If a nexthop was specified, parse it as the same family as the prefix.
368 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
369 ALOGE("inet_pton failed for nexthop %s", nexthop);
370 return -EINVAL;
371 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900372 }
373
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900374 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
375
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900376 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700377 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900378 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700379 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900380 .rtm_family = family,
381 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700382 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900383 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900384
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700385 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
386 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900387
388 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700389 { NULL, 0 },
390 { &route, sizeof(route) },
391 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
392 { &table, sizeof(table) },
393 { &rtaDst, sizeof(rtaDst) },
394 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700395 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
396 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700397 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
398 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900399 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
400 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900401 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900402
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700403 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
404 NETLINK_REQUEST_FLAGS;
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900405 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900406 if (ret) {
407 ALOGE("Error %s route %s -> %s %s to table %u: %s",
408 actionName(action), destination, nexthop, interface, table, strerror(-ret));
409 }
410 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700411}
412
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700413// An iptables rule to mark incoming packets on a network with the netId of the network.
414//
415// This is so that the kernel can:
416// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
417// replies, SYN-ACKs, etc).
418// + Mark sockets that accept connections from this interface so that the connection stays on the
419// same interface.
420WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
421 Permission permission, bool add) {
422 Fwmark fwmark;
423
424 fwmark.netId = netId;
425 fwmark.explicitlySelected = true;
426 fwmark.protectedFromVpn = true;
427 fwmark.permission = permission;
428
Lorenzo Colittid78843e2017-03-27 05:52:31 +0900429 std::string cmd = StringPrintf("%s %s -i %s -j MARK --set-mark 0x%x",
430 add ? "-A" : "-D", RouteController::LOCAL_MANGLE_INPUT,
431 interface, fwmark.intValue);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900432 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700433 ALOGE("failed to change iptables rule that sets incoming packet mark");
434 return -EREMOTEIO;
435 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700436
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700437 return 0;
438}
439
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700440// A rule to route responses to the local network forwarded via the VPN.
441//
442// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
443// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
444WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
445 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
446 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
447 INVALID_UID, INVALID_UID);
448}
449
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700450// A rule to route all traffic from a given set of UIDs to go over the VPN.
451//
452// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
453// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
454// bypass the VPN if the protectedFromVpn bit is set.
455WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700456 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700457 Fwmark fwmark;
458 Fwmark mask;
459
460 fwmark.protectedFromVpn = false;
461 mask.protectedFromVpn = true;
462
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700463 uint32_t priority;
464
465 if (secure) {
466 priority = RULE_PRIORITY_SECURE_VPN;
467 } else {
468 priority = RULE_PRIORITY_BYPASSABLE_VPN;
469
470 fwmark.explicitlySelected = false;
471 mask.explicitlySelected = true;
472 }
473
474 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900475 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700476}
477
478// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
479// set of UIDs.
480//
481// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
482// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700483WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
484 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700485 Fwmark fwmark;
486 Fwmark mask;
487
488 fwmark.netId = netId;
489 mask.netId = FWMARK_NET_ID_MASK;
490
491 fwmark.permission = PERMISSION_SYSTEM;
492 mask.permission = PERMISSION_SYSTEM;
493
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700494 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
495
496 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
497 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700498}
499
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700500// A rule to route traffic based on an explicitly chosen network.
501//
502// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
503//
504// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
505// to check it again in the rules here, because a network's permissions may have been updated via
506// modifyNetworkPermission().
507WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
508 Permission permission, uid_t uidStart,
509 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700510 Fwmark fwmark;
511 Fwmark mask;
512
513 fwmark.netId = netId;
514 mask.netId = FWMARK_NET_ID_MASK;
515
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700516 fwmark.explicitlySelected = true;
517 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700518
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700519 fwmark.permission = permission;
520 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700521
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700522 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700523 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700524}
525
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700526// A rule to route traffic based on a chosen outgoing interface.
527//
528// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
529// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900530WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
531 Permission permission, uid_t uidStart,
532 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700533 Fwmark fwmark;
534 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700535
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700536 fwmark.permission = permission;
537 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700538
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900539 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
Lorenzo Colitti3093f562017-09-25 14:17:38 +0900540 // for root. This covers forwarded packets and system daemons such as the tethering DHCP server.
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900541 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
542 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
Lorenzo Colitti3093f562017-09-25 14:17:38 +0900543 table, FWMARK_NONE, MASK_NONE, IIF_NONE, interface,
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900544 UID_ROOT, UID_ROOT)) {
545 return ret;
546 }
547 }
548
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700549 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700550 fwmark.intValue, mask.intValue, IIF_NONE, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700551}
552
553// A rule to route traffic based on the chosen network.
554//
555// This is for sockets that have not explicitly requested a particular network, but have been
556// bound to one when they called connect(). This ensures that sockets connected on a particular
557// network stay on that network even if the default network changes.
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900558WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700559 Fwmark fwmark;
560 Fwmark mask;
561
562 fwmark.netId = netId;
563 mask.netId = FWMARK_NET_ID_MASK;
564
565 fwmark.explicitlySelected = false;
566 mask.explicitlySelected = true;
567
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900568 fwmark.permission = PERMISSION_NONE;
569 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700570
571 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700572 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700573}
574
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700575// A rule to enable split tunnel VPNs.
576//
577// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
578// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
579// permissions required by the default network.
580WARN_UNUSED_RESULT int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
581 const char* physicalInterface,
582 Permission permission) {
583 uint32_t table = getRouteTableForInterface(physicalInterface);
584 if (table == RT_TABLE_UNSPEC) {
585 return -ESRCH;
586 }
587
588 Fwmark fwmark;
589 Fwmark mask;
590
591 fwmark.netId = vpnNetId;
592 mask.netId = FWMARK_NET_ID_MASK;
593
594 fwmark.explicitlySelected = false;
595 mask.explicitlySelected = true;
596
597 fwmark.permission = permission;
598 mask.permission = permission;
599
600 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
601 mask.intValue);
602}
603
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700604// Add rules to allow legacy routes added through the requestRouteToHost() API.
605WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700606 Fwmark fwmark;
607 Fwmark mask;
608
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700609 fwmark.explicitlySelected = false;
610 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700611
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700612 // Rules to allow legacy routes to override the default network.
613 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
614 fwmark.intValue, mask.intValue)) {
615 return ret;
616 }
617 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
618 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
619 return ret;
620 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700621
622 fwmark.permission = PERMISSION_SYSTEM;
623 mask.permission = PERMISSION_SYSTEM;
624
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700625 // A rule to allow legacy routes from system apps to override VPNs.
626 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700627 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700628}
629
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700630// Add rules to lookup the local network when specified explicitly or otherwise.
631WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
632 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
633 INVALID_UID, INVALID_UID, ACTION_ADD)) {
634 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700635 }
636
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700637 Fwmark fwmark;
638 Fwmark mask;
639
640 fwmark.explicitlySelected = false;
641 mask.explicitlySelected = true;
642
643 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
644 fwmark.intValue, mask.intValue);
645}
646
Lorenzo Colitti36679362015-02-25 10:26:19 +0900647int configureDummyNetwork() {
648 const char *interface = DummyNetwork::INTERFACE_NAME;
649 uint32_t table = getRouteTableForInterface(interface);
650 if (table == RT_TABLE_UNSPEC) {
651 // getRouteTableForInterface has already looged an error.
652 return -ESRCH;
653 }
654
655 ifc_init();
656 int ret = ifc_up(interface);
657 ifc_close();
658 if (ret) {
659 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
660 return -errno;
661 }
662
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900663 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
664 INVALID_UID, INVALID_UID, ACTION_ADD))) {
665 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900666 return ret;
667 }
668
669 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900670 return ret;
671 }
672
673 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900674 return ret;
675 }
676
677 return 0;
678}
679
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900680// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
681// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
682// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
683// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700684WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100685 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
686 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700687}
688
689WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700690 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
691 return ret;
692 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900693 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
694 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700695}
696
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700697WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
698 Permission permission, bool add) {
699 uint32_t table = getRouteTableForInterface(interface);
700 if (table == RT_TABLE_UNSPEC) {
701 return -ESRCH;
702 }
703
704 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
705 return ret;
706 }
707 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
708 add)) {
709 return ret;
710 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900711 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700712 add)) {
713 return ret;
714 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900715
716 // Only set implicit rules for networks that don't require permissions.
717 //
718 // This is so that if the default network ceases to be the default network and then switches
719 // from requiring no permissions to requiring permissions, we ensure that apps only use the
720 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
721 // - it closes all sockets on the network except sockets that are explicitly selected.
722 //
723 // The lack of this rule only affects the special case above, because:
724 // - The only cases where we implicitly bind a socket to a network are the default network and
725 // the bypassable VPN that applies to the app, if any.
726 // - This rule doesn't affect VPNs because they don't support permissions at all.
727 // - The default network doesn't require permissions. While we support doing this, the framework
728 // never does it (partly because we'd end up in the situation where we tell apps that there is
729 // a default network, but they can't use it).
730 // - If the network is still the default network, the presence or absence of this rule does not
731 // matter.
732 //
733 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
734 // implicitly bound to a network because at the time of connect() it was the default, and that
735 // network must no longer be the default, and must now require permissions.
736 if (permission == PERMISSION_NONE) {
737 return modifyImplicitNetworkRule(netId, table, add);
738 }
739 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700740}
741
Robin Leeb8087362016-03-30 18:43:08 +0100742WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
743 Fwmark fwmark;
744 Fwmark mask;
745 fwmark.protectedFromVpn = false;
746 mask.protectedFromVpn = true;
747
Robin Leedc0d5782016-07-20 14:17:11 +0100748 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100749 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
750 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
751 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100752 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100753 return ret;
754 }
755 }
756
757 return 0;
758}
759
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700760WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700761 const UidRanges& uidRanges, bool secure, bool add,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700762 bool modifyNonUidBasedRules) {
763 uint32_t table = getRouteTableForInterface(interface);
764 if (table == RT_TABLE_UNSPEC) {
765 return -ESRCH;
766 }
767
Robin Leedc0d5782016-07-20 14:17:11 +0100768 for (const UidRange& range : uidRanges.getRanges()) {
769 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
770 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700771 return ret;
772 }
Robin Leedc0d5782016-07-20 14:17:11 +0100773 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
774 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700775 return ret;
776 }
Robin Leedc0d5782016-07-20 14:17:11 +0100777 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
778 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700779 return ret;
780 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700781 }
782
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700783 if (modifyNonUidBasedRules) {
784 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700785 return ret;
786 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700787 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
788 return ret;
789 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700790 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700791 return ret;
792 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700793 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700794 }
795
796 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700797}
798
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700799WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
800 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700801 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700802 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900803 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700804 }
805
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700806 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700807 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700808
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700809 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700810 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700811
812 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700813 mask.permission = permission;
814
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700815 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700816 mask.intValue);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700817}
818
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700819WARN_UNUSED_RESULT int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
820 const char* outputInterface) {
821 uint32_t table = getRouteTableForInterface(outputInterface);
822 if (table == RT_TABLE_UNSPEC) {
823 return -ESRCH;
824 }
825
826 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
827 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
828}
829
Lorenzo Colitti92e8f962017-09-26 19:13:50 +0900830// Adds or removes an IPv4 or IPv6 route to the specified table.
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900831// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700832WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
833 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700834 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700835 switch (tableType) {
836 case RouteController::INTERFACE: {
837 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700838 if (table == RT_TABLE_UNSPEC) {
839 return -ESRCH;
840 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700841 break;
842 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700843 case RouteController::LOCAL_NETWORK: {
844 table = ROUTE_TABLE_LOCAL_NETWORK;
845 break;
846 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700847 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700848 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700849 break;
850 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700851 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700852 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700853 break;
854 }
855 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700856
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900857 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700858 // Trying to add a route that already exists shouldn't cause an error.
859 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900860 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700861 }
862
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900863 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700864}
865
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900866WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
867 int ret = 0;
868 while (ret == 0) {
869 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
870 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
871 }
872
873 if (ret == -ENOENT) {
874 return 0;
875 } else {
876 return ret;
877 }
878}
879
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900880uint32_t getRulePriority(const nlmsghdr *nlh) {
881 return getRtmU32Attribute(nlh, FRA_PRIORITY);
882}
883
884uint32_t getRouteTable(const nlmsghdr *nlh) {
885 return getRtmU32Attribute(nlh, RTA_TABLE);
886}
887
888WARN_UNUSED_RESULT int flushRules() {
889 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
890 // Don't touch rules at priority 0 because by default they are used for local input.
891 return getRulePriority(nlh) != 0;
892 };
893 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
894}
895
896WARN_UNUSED_RESULT int flushRoutes(uint32_t table) {
897 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
898 return getRouteTable(nlh) == table;
899 };
900
901 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
902}
903
904// Returns 0 on success or negative errno on failure.
905WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
906 uint32_t table = getRouteTableForInterface(interface);
907 if (table == RT_TABLE_UNSPEC) {
908 return -ESRCH;
909 }
910
911 int ret = flushRoutes(table);
912
913 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
914 // track of its name.
915 if (ret == 0) {
916 interfaceToTable.erase(interface);
917 }
918
919 return ret;
920}
921
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700922int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700923 if (int ret = flushRules()) {
924 return ret;
925 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700926 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700927 return ret;
928 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700929 if (int ret = addLocalNetworkRules(localNetId)) {
930 return ret;
931 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700932 if (int ret = addUnreachableRule()) {
933 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700934 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900935 // Don't complain if we can't add the dummy network, since not all devices support it.
936 configureDummyNetwork();
937
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700938 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700939 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700940}
941
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700942int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700943 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700944}
945
946int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700947 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700948}
949
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700950int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
951 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700952 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
953 return ret;
954 }
955 updateTableNamesFile();
956 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700957}
958
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700959int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
960 Permission permission) {
961 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700962 return ret;
963 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700964 if (int ret = flushRoutes(interface)) {
965 return ret;
966 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900967 if (int ret = clearTetheringRules(interface)) {
968 return ret;
969 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700970 updateTableNamesFile();
971 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700972}
973
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700974int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700975 bool secure, const UidRanges& uidRanges) {
976 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700977 MODIFY_NON_UID_BASED_RULES)) {
978 return ret;
979 }
980 updateTableNamesFile();
981 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700982}
983
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700984int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700985 bool secure, const UidRanges& uidRanges) {
986 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700987 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700988 return ret;
989 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700990 if (int ret = flushRoutes(interface)) {
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::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
998 Permission oldPermission,
999 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001000 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001001 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001002 return ret;
1003 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001004 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001005}
1006
Robin Leeb8087362016-03-30 18:43:08 +01001007int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1008 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1009}
1010
1011int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1012 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1013}
1014
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001015int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001016 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001017 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001018 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001019}
1020
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001021int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001022 bool secure, const UidRanges& uidRanges) {
1023 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001024 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001025}
1026
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001027int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1028 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001029}
1030
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001031int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1032 Permission permission) {
1033 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001034}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001035
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001036int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001037 TableType tableType) {
1038 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001039}
1040
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001041int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001042 const char* nexthop, TableType tableType) {
1043 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001044}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001045
1046int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001047 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001048}
1049
1050int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001051 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001052}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001053
1054int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1055 Permission permission) {
1056 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1057}
1058
1059int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1060 const char* physicalInterface,
1061 Permission permission) {
1062 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1063}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001064
1065} // namespace net
1066} // namespace android