blob: 87a56982127b148da7d3e43960bfffde1ca97f76 [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 Colittia2c23052014-07-29 09:25:44 +000070const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 23000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090071const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070072
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070073const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070074const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
75const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
76
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070077const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070078const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
79const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
80
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070081const char* const ROUTE_TABLE_NAME_LOCAL = "local";
82const char* const ROUTE_TABLE_NAME_MAIN = "main";
83
Lorenzo Colittid78843e2017-03-27 05:52:31 +090084const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
85
Lorenzo Colitti2b078672016-12-16 18:45:03 +090086// These values are upstream, but not yet in our headers.
87// TODO: delete these definitions when updating the headers.
88const uint16_t FRA_UID_RANGE = 20;
89struct fib_rule_uid_range {
90 __u32 start;
91 __u32 end;
92};
Lorenzo Colitti59656512014-06-25 03:20:29 +090093
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070094const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
95
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070096const uid_t UID_ROOT = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090097const char* const IIF_LOOPBACK = "lo";
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070098const char* const IIF_NONE = NULL;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070099const char* const OIF_NONE = NULL;
100const bool ACTION_ADD = true;
101const bool ACTION_DEL = false;
102const bool MODIFY_NON_UID_BASED_RULES = true;
103
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700104const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700105const 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 -0700106
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700107// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
108// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
109constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
110 return RTA_LENGTH(x);
111}
112
113// These are practically const, but can't be declared so, because they are used to initialize
114// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900115rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
116rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
117rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
118rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900119rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700120
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900121rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
122rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700123
124uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
125
126// END CONSTANTS ----------------------------------------------------------------------------------
127
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900128const char *actionName(uint16_t action) {
129 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
130 return ops[action % 4];
131}
132
133const char *familyName(uint8_t family) {
134 switch (family) {
135 case AF_INET: return "IPv4";
136 case AF_INET6: return "IPv6";
137 default: return "???";
138 }
139}
140
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700141// No locks needed because RouteController is accessed only from one thread (in CommandListener).
142std::map<std::string, uint32_t> interfaceToTable;
Paul Jensena561e122014-06-12 16:46:37 -0400143
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700144uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700145 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400146 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700147 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
148 interfaceToTable[interface] = index;
149 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400150 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700151 // If the interface goes away if_nametoindex() will return 0 but we still need to know
152 // the index so we can remove the rules and routes.
153 auto iter = interfaceToTable.find(interface);
154 if (iter == interfaceToTable.end()) {
155 ALOGE("cannot find interface %s", interface);
156 return RT_TABLE_UNSPEC;
157 }
158 return iter->second;
159}
160
161void addTableName(uint32_t table, const std::string& name, std::string* contents) {
162 char tableString[UINT32_STRLEN];
163 snprintf(tableString, sizeof(tableString), "%u", table);
164 *contents += tableString;
165 *contents += " ";
166 *contents += name;
167 *contents += "\n";
168}
169
170// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
171void updateTableNamesFile() {
172 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700173
174 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
175 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
176
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700177 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700178 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
179 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700180
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700181 for (const auto& entry : interfaceToTable) {
182 addTableName(entry.second, entry.first, &contents);
183 }
184
Dan Albert5407e142015-03-16 10:05:59 -0700185 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800186 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700187 return;
188 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700189}
190
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700191// Returns 0 on success or negative errno on failure.
192int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
193 if (!input) {
194 *length = 0;
195 *padding = 0;
196 return 0;
197 }
198 *length = strlcpy(name, input, IFNAMSIZ) + 1;
199 if (*length > IFNAMSIZ) {
200 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
201 return -ENAMETOOLONG;
202 }
203 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
204 return 0;
205}
206
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700207// Adds or removes a routing rule for IPv4 and IPv6.
208//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900209// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100210// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700211// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
212// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700213// + If |iif| is non-NULL, the rule matches the specified incoming interface.
214// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
215// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
216// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900217//
218// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100219WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
220 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700221 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700222 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
223 if (fwmark & ~mask) {
224 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
225 return -ERANGE;
226 }
227
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700228 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900229 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700230 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
231 size_t iifLength, oifLength;
232 uint16_t iifPadding, oifPadding;
233 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
234 return ret;
235 }
236 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
237 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900238 }
239
Lorenzo Colitti59656512014-06-25 03:20:29 +0900240 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700241 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700242 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900243 return -EUSERS;
244 }
Robin Lee4ef94642016-04-01 11:50:49 +0100245
Lorenzo Colitti72723682014-06-26 13:51:10 +0900246 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900247
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900248 // Assemble a rule request and put it in an array of iovec structures.
249 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100250 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900251 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
252 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900253 };
254
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900255 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
256 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
257 // table 0, it's a wildcard that matches anything.
258 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
259 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
260 return -ENOTUNIQ;
261 }
262
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700263 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
264 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900265 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900266
267 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700268 { NULL, 0 },
269 { &rule, sizeof(rule) },
270 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
271 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700272 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
273 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700274 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
275 { &fwmark, mask ? sizeof(fwmark) : 0 },
276 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
277 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900278 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
279 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700280 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
281 { iifName, iifLength },
282 { PADDING_BUFFER, iifPadding },
283 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
284 { oifName, oifLength },
285 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900286 };
287
Lorenzo Colitti72723682014-06-26 13:51:10 +0900288 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700289 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
290 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900291 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900292 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
293 // Don't log when deleting a tethering rule that's not there. This matches the
294 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
295 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
296 strerror(-ret));
297 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900298 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700299 }
300 }
301
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900302 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700303}
304
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700305WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100306 uint32_t fwmark, uint32_t mask, const char* iif,
307 const char* oif, uid_t uidStart, uid_t uidEnd) {
308 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
309 uidEnd);
310}
311
312WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700313 uint32_t fwmark, uint32_t mask) {
314 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
315 INVALID_UID);
316}
317
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900318// Adds or deletes an IPv4 or IPv6 route.
319// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700320WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
321 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900322 // At least the destination must be non-null.
323 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700324 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900325 return -EFAULT;
326 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700327
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900328 // Parse the prefix.
329 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700330 uint8_t family;
331 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900332 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
333 &prefixLength);
334 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700335 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900336 return rawLength;
337 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700338
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900339 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700340 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900341 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
342 }
343
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700344 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900345 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900346 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700347
348 if (nexthop && !strcmp(nexthop, "unreachable")) {
349 type = RTN_UNREACHABLE;
350 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
351 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
352 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
353 interface = OIF_NONE;
354 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900355 } else if (nexthop && !strcmp(nexthop, "throw")) {
356 type = RTN_THROW;
357 interface = OIF_NONE;
358 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700359 } else {
360 // If an interface was specified, find the ifindex.
361 if (interface != OIF_NONE) {
362 ifindex = if_nametoindex(interface);
363 if (!ifindex) {
364 ALOGE("cannot find interface %s", interface);
365 return -ENODEV;
366 }
367 }
368
369 // If a nexthop was specified, parse it as the same family as the prefix.
370 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
371 ALOGE("inet_pton failed for nexthop %s", nexthop);
372 return -EINVAL;
373 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900374 }
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 Colittiba25df92014-06-18 00:22:17 +0900399 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900400
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700401 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
402 NETLINK_REQUEST_FLAGS;
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900403 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900404 if (ret) {
405 ALOGE("Error %s route %s -> %s %s to table %u: %s",
406 actionName(action), destination, nexthop, interface, table, strerror(-ret));
407 }
408 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700409}
410
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700411// An iptables rule to mark incoming packets on a network with the netId of the network.
412//
413// This is so that the kernel can:
414// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
415// replies, SYN-ACKs, etc).
416// + Mark sockets that accept connections from this interface so that the connection stays on the
417// same interface.
418WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
419 Permission permission, bool add) {
420 Fwmark fwmark;
421
422 fwmark.netId = netId;
423 fwmark.explicitlySelected = true;
424 fwmark.protectedFromVpn = true;
425 fwmark.permission = permission;
426
Lorenzo Colittid78843e2017-03-27 05:52:31 +0900427 std::string cmd = StringPrintf("%s %s -i %s -j MARK --set-mark 0x%x",
428 add ? "-A" : "-D", RouteController::LOCAL_MANGLE_INPUT,
429 interface, fwmark.intValue);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900430 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700431 ALOGE("failed to change iptables rule that sets incoming packet mark");
432 return -EREMOTEIO;
433 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700434
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700435 return 0;
436}
437
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700438// A rule to route responses to the local network forwarded via the VPN.
439//
440// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
441// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
442WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
443 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
444 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
445 INVALID_UID, INVALID_UID);
446}
447
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700448// A rule to route all traffic from a given set of UIDs to go over the VPN.
449//
450// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
451// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
452// bypass the VPN if the protectedFromVpn bit is set.
453WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700454 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700455 Fwmark fwmark;
456 Fwmark mask;
457
458 fwmark.protectedFromVpn = false;
459 mask.protectedFromVpn = true;
460
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700461 uint32_t priority;
462
463 if (secure) {
464 priority = RULE_PRIORITY_SECURE_VPN;
465 } else {
466 priority = RULE_PRIORITY_BYPASSABLE_VPN;
467
468 fwmark.explicitlySelected = false;
469 mask.explicitlySelected = true;
470 }
471
472 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900473 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700474}
475
476// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
477// set of UIDs.
478//
479// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
480// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700481WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
482 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700483 Fwmark fwmark;
484 Fwmark mask;
485
486 fwmark.netId = netId;
487 mask.netId = FWMARK_NET_ID_MASK;
488
489 fwmark.permission = PERMISSION_SYSTEM;
490 mask.permission = PERMISSION_SYSTEM;
491
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700492 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
493
494 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
495 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700496}
497
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700498// A rule to route traffic based on an explicitly chosen network.
499//
500// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
501//
502// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
503// to check it again in the rules here, because a network's permissions may have been updated via
504// modifyNetworkPermission().
505WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
506 Permission permission, uid_t uidStart,
507 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700508 Fwmark fwmark;
509 Fwmark mask;
510
511 fwmark.netId = netId;
512 mask.netId = FWMARK_NET_ID_MASK;
513
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700514 fwmark.explicitlySelected = true;
515 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700516
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700517 fwmark.permission = permission;
518 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700519
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700520 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700521 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700522}
523
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700524// A rule to route traffic based on a chosen outgoing interface.
525//
526// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
527// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900528WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
529 Permission permission, uid_t uidStart,
530 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700531 Fwmark fwmark;
532 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700533
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700534 fwmark.permission = permission;
535 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700536
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900537 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
538 // for UID. This covers forwarded packets and system daemons such as the tethering DHCP server.
539 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
540 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
541 table, fwmark.intValue, mask.intValue, IIF_NONE, interface,
542 UID_ROOT, UID_ROOT)) {
543 return ret;
544 }
545 }
546
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700547 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700548 fwmark.intValue, mask.intValue, IIF_NONE, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700549}
550
551// A rule to route traffic based on the chosen network.
552//
553// This is for sockets that have not explicitly requested a particular network, but have been
554// bound to one when they called connect(). This ensures that sockets connected on a particular
555// network stay on that network even if the default network changes.
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900556WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700557 Fwmark fwmark;
558 Fwmark mask;
559
560 fwmark.netId = netId;
561 mask.netId = FWMARK_NET_ID_MASK;
562
563 fwmark.explicitlySelected = false;
564 mask.explicitlySelected = true;
565
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900566 fwmark.permission = PERMISSION_NONE;
567 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700568
569 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700570 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700571}
572
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700573// A rule to enable split tunnel VPNs.
574//
575// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
576// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
577// permissions required by the default network.
578WARN_UNUSED_RESULT int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
579 const char* physicalInterface,
580 Permission permission) {
581 uint32_t table = getRouteTableForInterface(physicalInterface);
582 if (table == RT_TABLE_UNSPEC) {
583 return -ESRCH;
584 }
585
586 Fwmark fwmark;
587 Fwmark mask;
588
589 fwmark.netId = vpnNetId;
590 mask.netId = FWMARK_NET_ID_MASK;
591
592 fwmark.explicitlySelected = false;
593 mask.explicitlySelected = true;
594
595 fwmark.permission = permission;
596 mask.permission = permission;
597
598 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
599 mask.intValue);
600}
601
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700602// Add rules to allow legacy routes added through the requestRouteToHost() API.
603WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700604 Fwmark fwmark;
605 Fwmark mask;
606
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700607 fwmark.explicitlySelected = false;
608 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700609
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700610 // Rules to allow legacy routes to override the default network.
611 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
612 fwmark.intValue, mask.intValue)) {
613 return ret;
614 }
615 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
616 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
617 return ret;
618 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700619
620 fwmark.permission = PERMISSION_SYSTEM;
621 mask.permission = PERMISSION_SYSTEM;
622
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700623 // A rule to allow legacy routes from system apps to override VPNs.
624 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700625 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700626}
627
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700628// Add rules to lookup the local network when specified explicitly or otherwise.
629WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
630 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
631 INVALID_UID, INVALID_UID, ACTION_ADD)) {
632 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700633 }
634
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700635 Fwmark fwmark;
636 Fwmark mask;
637
638 fwmark.explicitlySelected = false;
639 mask.explicitlySelected = true;
640
641 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
642 fwmark.intValue, mask.intValue);
643}
644
Lorenzo Colitti36679362015-02-25 10:26:19 +0900645int configureDummyNetwork() {
646 const char *interface = DummyNetwork::INTERFACE_NAME;
647 uint32_t table = getRouteTableForInterface(interface);
648 if (table == RT_TABLE_UNSPEC) {
649 // getRouteTableForInterface has already looged an error.
650 return -ESRCH;
651 }
652
653 ifc_init();
654 int ret = ifc_up(interface);
655 ifc_close();
656 if (ret) {
657 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
658 return -errno;
659 }
660
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900661 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
662 INVALID_UID, INVALID_UID, ACTION_ADD))) {
663 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900664 return ret;
665 }
666
667 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900668 return ret;
669 }
670
671 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900672 return ret;
673 }
674
675 return 0;
676}
677
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000678// 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 +0900679// rule, but with a lower priority. We will never create routes in the main table; it should only be
680// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
681// This is necessary, for example, when adding a route through a directly-connected gateway: in
682// order to add the route, there must already be a directly-connected route that covers the gateway.
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000683WARN_UNUSED_RESULT int addDirectlyConnectedRule() {
684 Fwmark fwmark;
685 Fwmark mask;
686
687 fwmark.netId = NETID_UNSET;
688 mask.netId = FWMARK_NET_ID_MASK;
689
690 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
691 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT);
692}
693
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900694// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
695// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
696// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
697// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700698WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100699 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
700 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700701}
702
703WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700704 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
705 return ret;
706 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900707 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
708 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700709}
710
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700711WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
712 Permission permission, bool add) {
713 uint32_t table = getRouteTableForInterface(interface);
714 if (table == RT_TABLE_UNSPEC) {
715 return -ESRCH;
716 }
717
718 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
719 return ret;
720 }
721 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
722 add)) {
723 return ret;
724 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900725 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700726 add)) {
727 return ret;
728 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900729
730 // Only set implicit rules for networks that don't require permissions.
731 //
732 // This is so that if the default network ceases to be the default network and then switches
733 // from requiring no permissions to requiring permissions, we ensure that apps only use the
734 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
735 // - it closes all sockets on the network except sockets that are explicitly selected.
736 //
737 // The lack of this rule only affects the special case above, because:
738 // - The only cases where we implicitly bind a socket to a network are the default network and
739 // the bypassable VPN that applies to the app, if any.
740 // - This rule doesn't affect VPNs because they don't support permissions at all.
741 // - The default network doesn't require permissions. While we support doing this, the framework
742 // never does it (partly because we'd end up in the situation where we tell apps that there is
743 // a default network, but they can't use it).
744 // - If the network is still the default network, the presence or absence of this rule does not
745 // matter.
746 //
747 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
748 // implicitly bound to a network because at the time of connect() it was the default, and that
749 // network must no longer be the default, and must now require permissions.
750 if (permission == PERMISSION_NONE) {
751 return modifyImplicitNetworkRule(netId, table, add);
752 }
753 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700754}
755
Robin Leeb8087362016-03-30 18:43:08 +0100756WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
757 Fwmark fwmark;
758 Fwmark mask;
759 fwmark.protectedFromVpn = false;
760 mask.protectedFromVpn = true;
761
Robin Leedc0d5782016-07-20 14:17:11 +0100762 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100763 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
764 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
765 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100766 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100767 return ret;
768 }
769 }
770
771 return 0;
772}
773
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700774WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700775 const UidRanges& uidRanges, bool secure, bool add,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700776 bool modifyNonUidBasedRules) {
777 uint32_t table = getRouteTableForInterface(interface);
778 if (table == RT_TABLE_UNSPEC) {
779 return -ESRCH;
780 }
781
Robin Leedc0d5782016-07-20 14:17:11 +0100782 for (const UidRange& range : uidRanges.getRanges()) {
783 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
784 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700785 return ret;
786 }
Robin Leedc0d5782016-07-20 14:17:11 +0100787 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
788 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700789 return ret;
790 }
Robin Leedc0d5782016-07-20 14:17:11 +0100791 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
792 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700793 return ret;
794 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700795 }
796
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700797 if (modifyNonUidBasedRules) {
798 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700799 return ret;
800 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700801 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
802 return ret;
803 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700804 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700805 return ret;
806 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700807 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700808 }
809
810 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700811}
812
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700813WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
814 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700815 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700816 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900817 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700818 }
819
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700820 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700821 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700822
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700823 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700824 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700825
826 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700827 mask.permission = permission;
828
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700829 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700830 mask.intValue);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700831}
832
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700833WARN_UNUSED_RESULT int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
834 const char* outputInterface) {
835 uint32_t table = getRouteTableForInterface(outputInterface);
836 if (table == RT_TABLE_UNSPEC) {
837 return -ESRCH;
838 }
839
840 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
841 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
842}
843
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700844// 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 +0900845// route, to the main table as well.
846// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700847WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
848 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700849 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700850 switch (tableType) {
851 case RouteController::INTERFACE: {
852 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700853 if (table == RT_TABLE_UNSPEC) {
854 return -ESRCH;
855 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700856 break;
857 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700858 case RouteController::LOCAL_NETWORK: {
859 table = ROUTE_TABLE_LOCAL_NETWORK;
860 break;
861 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700862 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700863 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700864 break;
865 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700866 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700867 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700868 break;
869 }
870 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700871
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900872 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700873 // Trying to add a route that already exists shouldn't cause an error.
874 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900875 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700876 }
877
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900878 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700879}
880
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900881WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
882 int ret = 0;
883 while (ret == 0) {
884 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
885 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
886 }
887
888 if (ret == -ENOENT) {
889 return 0;
890 } else {
891 return ret;
892 }
893}
894
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900895uint32_t getRulePriority(const nlmsghdr *nlh) {
896 return getRtmU32Attribute(nlh, FRA_PRIORITY);
897}
898
899uint32_t getRouteTable(const nlmsghdr *nlh) {
900 return getRtmU32Attribute(nlh, RTA_TABLE);
901}
902
903WARN_UNUSED_RESULT int flushRules() {
904 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
905 // Don't touch rules at priority 0 because by default they are used for local input.
906 return getRulePriority(nlh) != 0;
907 };
908 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
909}
910
911WARN_UNUSED_RESULT int flushRoutes(uint32_t table) {
912 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
913 return getRouteTable(nlh) == table;
914 };
915
916 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
917}
918
919// Returns 0 on success or negative errno on failure.
920WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
921 uint32_t table = getRouteTableForInterface(interface);
922 if (table == RT_TABLE_UNSPEC) {
923 return -ESRCH;
924 }
925
926 int ret = flushRoutes(table);
927
928 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
929 // track of its name.
930 if (ret == 0) {
931 interfaceToTable.erase(interface);
932 }
933
934 return ret;
935}
936
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700937int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700938 if (int ret = flushRules()) {
939 return ret;
940 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700941 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700942 return ret;
943 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700944 if (int ret = addLocalNetworkRules(localNetId)) {
945 return ret;
946 }
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000947 if (int ret = addDirectlyConnectedRule()) {
948 return ret;
949 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700950 if (int ret = addUnreachableRule()) {
951 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700952 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900953 // Don't complain if we can't add the dummy network, since not all devices support it.
954 configureDummyNetwork();
955
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700956 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700957 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700958}
959
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700960int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700961 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700962}
963
964int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700965 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700966}
967
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700968int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
969 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700970 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
971 return ret;
972 }
973 updateTableNamesFile();
974 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700975}
976
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700977int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
978 Permission permission) {
979 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700980 return ret;
981 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700982 if (int ret = flushRoutes(interface)) {
983 return ret;
984 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900985 if (int ret = clearTetheringRules(interface)) {
986 return ret;
987 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700988 updateTableNamesFile();
989 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700990}
991
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700992int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700993 bool secure, const UidRanges& uidRanges) {
994 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700995 MODIFY_NON_UID_BASED_RULES)) {
996 return ret;
997 }
998 updateTableNamesFile();
999 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001000}
1001
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001002int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001003 bool secure, const UidRanges& uidRanges) {
1004 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001005 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001006 return ret;
1007 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001008 if (int ret = flushRoutes(interface)) {
1009 return ret;
1010 }
1011 updateTableNamesFile();
1012 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001013}
1014
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001015int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1016 Permission oldPermission,
1017 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001018 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001019 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001020 return ret;
1021 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001022 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001023}
1024
Robin Leeb8087362016-03-30 18:43:08 +01001025int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1026 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1027}
1028
1029int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1030 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1031}
1032
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001033int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001034 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001035 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001036 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001037}
1038
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001039int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001040 bool secure, const UidRanges& uidRanges) {
1041 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001042 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001043}
1044
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001045int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1046 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001047}
1048
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001049int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1050 Permission permission) {
1051 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001052}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001053
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001054int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001055 TableType tableType) {
1056 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001057}
1058
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001059int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001060 const char* nexthop, TableType tableType) {
1061 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001062}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001063
1064int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001065 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001066}
1067
1068int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001069 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001070}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001071
1072int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1073 Permission permission) {
1074 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1075}
1076
1077int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1078 const char* physicalInterface,
1079 Permission permission) {
1080 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1081}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001082
1083} // namespace net
1084} // namespace android