blob: 85ab43a52034ff9c995c180a64278dc1eab1e635 [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 Colitti2b078672016-12-16 18:45:03 +090084// TODO: These values aren't defined by the Linux kernel, because legacy UID routing (as used in N
85// and below) was not upstreamed. Now that the UID routing code is upstream, we should remove these
86// and rely on the kernel header values.
Lorenzo Colitti59656512014-06-25 03:20:29 +090087const uint16_t FRA_UID_START = 18;
88const uint16_t FRA_UID_END = 19;
Lorenzo Colitti2b078672016-12-16 18:45:03 +090089
90// These values are upstream, but not yet in our headers.
91// TODO: delete these definitions when updating the headers.
92const uint16_t FRA_UID_RANGE = 20;
93struct fib_rule_uid_range {
94 __u32 start;
95 __u32 end;
96};
Lorenzo Colitti59656512014-06-25 03:20:29 +090097
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070098const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
99
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700100const uid_t UID_ROOT = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900101const char* const IIF_LOOPBACK = "lo";
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700102const char* const IIF_NONE = NULL;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700103const char* const OIF_NONE = NULL;
104const bool ACTION_ADD = true;
105const bool ACTION_DEL = false;
106const bool MODIFY_NON_UID_BASED_RULES = true;
107
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700108const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700109const 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 -0700110
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700111// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
112// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
113constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
114 return RTA_LENGTH(x);
115}
116
117// These are practically const, but can't be declared so, because they are used to initialize
118// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900119rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
120rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
121rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
122rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
123rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START };
124rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END };
125rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700126
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900127rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
128rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700129
130uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
131
132// END CONSTANTS ----------------------------------------------------------------------------------
133
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900134const char *actionName(uint16_t action) {
135 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
136 return ops[action % 4];
137}
138
139const char *familyName(uint8_t family) {
140 switch (family) {
141 case AF_INET: return "IPv4";
142 case AF_INET6: return "IPv6";
143 default: return "???";
144 }
145}
146
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700147// No locks needed because RouteController is accessed only from one thread (in CommandListener).
148std::map<std::string, uint32_t> interfaceToTable;
Paul Jensena561e122014-06-12 16:46:37 -0400149
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700150uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700151 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400152 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700153 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
154 interfaceToTable[interface] = index;
155 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400156 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700157 // If the interface goes away if_nametoindex() will return 0 but we still need to know
158 // the index so we can remove the rules and routes.
159 auto iter = interfaceToTable.find(interface);
160 if (iter == interfaceToTable.end()) {
161 ALOGE("cannot find interface %s", interface);
162 return RT_TABLE_UNSPEC;
163 }
164 return iter->second;
165}
166
167void addTableName(uint32_t table, const std::string& name, std::string* contents) {
168 char tableString[UINT32_STRLEN];
169 snprintf(tableString, sizeof(tableString), "%u", table);
170 *contents += tableString;
171 *contents += " ";
172 *contents += name;
173 *contents += "\n";
174}
175
176// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
177void updateTableNamesFile() {
178 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700179
180 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
181 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
182
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700183 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700184 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
185 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700186
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700187 for (const auto& entry : interfaceToTable) {
188 addTableName(entry.second, entry.first, &contents);
189 }
190
Dan Albert5407e142015-03-16 10:05:59 -0700191 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800192 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700193 return;
194 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700195}
196
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700197// Returns 0 on success or negative errno on failure.
198int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
199 if (!input) {
200 *length = 0;
201 *padding = 0;
202 return 0;
203 }
204 *length = strlcpy(name, input, IFNAMSIZ) + 1;
205 if (*length > IFNAMSIZ) {
206 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
207 return -ENAMETOOLONG;
208 }
209 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
210 return 0;
211}
212
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700213// Adds or removes a routing rule for IPv4 and IPv6.
214//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900215// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100216// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700217// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
218// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700219// + If |iif| is non-NULL, the rule matches the specified incoming interface.
220// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
221// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
222// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900223//
224// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100225WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
226 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700227 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700228 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
229 if (fwmark & ~mask) {
230 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
231 return -ERANGE;
232 }
233
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700234 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900235 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700236 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
237 size_t iifLength, oifLength;
238 uint16_t iifPadding, oifPadding;
239 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
240 return ret;
241 }
242 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
243 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900244 }
245
Lorenzo Colitti59656512014-06-25 03:20:29 +0900246 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700247 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700248 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900249 return -EUSERS;
250 }
Robin Lee4ef94642016-04-01 11:50:49 +0100251
Lorenzo Colitti72723682014-06-26 13:51:10 +0900252 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900253
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900254 // Assemble a rule request and put it in an array of iovec structures.
255 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100256 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900257 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
258 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900259 };
260
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900261 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
262 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
263 // table 0, it's a wildcard that matches anything.
264 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
265 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
266 return -ENOTUNIQ;
267 }
268
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700269 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
270 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900271 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900272
273 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700274 { NULL, 0 },
275 { &rule, sizeof(rule) },
276 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
277 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700278 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
279 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700280 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
281 { &fwmark, mask ? sizeof(fwmark) : 0 },
282 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
283 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900284 // Rules that contain both legacy and new UID routing attributes will work on old kernels,
285 // which will simply ignore the FRA_UID_RANGE attribute since it is larger than their
286 // FRA_MAX. They will also work on kernels that are not too new:
287 // - FRA_UID_START clashes with FRA_PAD in 4.7, but that shouldn't be a problem because
288 // FRA_PAD has no validation.
289 // - FRA_UID_END clashes with FRA_L3MDEV in 4.8 and above, and will cause an error because
290 // FRA_L3MDEV has a maximum length of 1.
291 // TODO: delete the legacy UID routing code before running it on 4.8 or above.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700292 { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
293 { &uidStart, isUidRule ? sizeof(uidStart) : 0 },
294 { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 },
295 { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900296 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
297 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700298 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
299 { iifName, iifLength },
300 { PADDING_BUFFER, iifPadding },
301 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
302 { oifName, oifLength },
303 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900304 };
305
Lorenzo Colitti72723682014-06-26 13:51:10 +0900306 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700307 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
308 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900309 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900310 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
311 // Don't log when deleting a tethering rule that's not there. This matches the
312 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
313 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
314 strerror(-ret));
315 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900316 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700317 }
318 }
319
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900320 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700321}
322
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700323WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100324 uint32_t fwmark, uint32_t mask, const char* iif,
325 const char* oif, uid_t uidStart, uid_t uidEnd) {
326 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
327 uidEnd);
328}
329
330WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700331 uint32_t fwmark, uint32_t mask) {
332 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
333 INVALID_UID);
334}
335
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900336// Adds or deletes an IPv4 or IPv6 route.
337// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700338WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
339 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900340 // At least the destination must be non-null.
341 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700342 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900343 return -EFAULT;
344 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700345
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900346 // Parse the prefix.
347 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700348 uint8_t family;
349 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900350 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
351 &prefixLength);
352 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700353 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900354 return rawLength;
355 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700356
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900357 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700358 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900359 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
360 }
361
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700362 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900363 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900364 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700365
366 if (nexthop && !strcmp(nexthop, "unreachable")) {
367 type = RTN_UNREACHABLE;
368 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
369 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
370 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
371 interface = OIF_NONE;
372 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900373 } else if (nexthop && !strcmp(nexthop, "throw")) {
374 type = RTN_THROW;
375 interface = OIF_NONE;
376 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700377 } else {
378 // If an interface was specified, find the ifindex.
379 if (interface != OIF_NONE) {
380 ifindex = if_nametoindex(interface);
381 if (!ifindex) {
382 ALOGE("cannot find interface %s", interface);
383 return -ENODEV;
384 }
385 }
386
387 // If a nexthop was specified, parse it as the same family as the prefix.
388 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
389 ALOGE("inet_pton failed for nexthop %s", nexthop);
390 return -EINVAL;
391 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900392 }
393
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900394 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700395 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900396 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700397 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900398 .rtm_family = family,
399 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700400 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900401 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900402
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700403 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
404 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900405
406 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700407 { NULL, 0 },
408 { &route, sizeof(route) },
409 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
410 { &table, sizeof(table) },
411 { &rtaDst, sizeof(rtaDst) },
412 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700413 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
414 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700415 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
416 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900417 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900418
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700419 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
420 NETLINK_REQUEST_FLAGS;
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900421 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900422 if (ret) {
423 ALOGE("Error %s route %s -> %s %s to table %u: %s",
424 actionName(action), destination, nexthop, interface, table, strerror(-ret));
425 }
426 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700427}
428
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700429// An iptables rule to mark incoming packets on a network with the netId of the network.
430//
431// This is so that the kernel can:
432// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
433// replies, SYN-ACKs, etc).
434// + Mark sockets that accept connections from this interface so that the connection stays on the
435// same interface.
436WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
437 Permission permission, bool add) {
438 Fwmark fwmark;
439
440 fwmark.netId = netId;
441 fwmark.explicitlySelected = true;
442 fwmark.protectedFromVpn = true;
443 fwmark.permission = permission;
444
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900445 std::string cmd = StringPrintf("%s INPUT -i %s -j MARK --set-mark 0x%x",
446 add ? "-A" : "-D", interface, fwmark.intValue);
447 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700448 ALOGE("failed to change iptables rule that sets incoming packet mark");
449 return -EREMOTEIO;
450 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700451
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700452 return 0;
453}
454
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700455// A rule to route responses to the local network forwarded via the VPN.
456//
457// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
458// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
459WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
460 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
461 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
462 INVALID_UID, INVALID_UID);
463}
464
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700465// A rule to route all traffic from a given set of UIDs to go over the VPN.
466//
467// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
468// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
469// bypass the VPN if the protectedFromVpn bit is set.
470WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700471 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700472 Fwmark fwmark;
473 Fwmark mask;
474
475 fwmark.protectedFromVpn = false;
476 mask.protectedFromVpn = true;
477
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700478 uint32_t priority;
479
480 if (secure) {
481 priority = RULE_PRIORITY_SECURE_VPN;
482 } else {
483 priority = RULE_PRIORITY_BYPASSABLE_VPN;
484
485 fwmark.explicitlySelected = false;
486 mask.explicitlySelected = true;
487 }
488
489 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900490 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700491}
492
493// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
494// set of UIDs.
495//
496// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
497// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700498WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
499 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700500 Fwmark fwmark;
501 Fwmark mask;
502
503 fwmark.netId = netId;
504 mask.netId = FWMARK_NET_ID_MASK;
505
506 fwmark.permission = PERMISSION_SYSTEM;
507 mask.permission = PERMISSION_SYSTEM;
508
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700509 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
510
511 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
512 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700513}
514
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700515// A rule to route traffic based on an explicitly chosen network.
516//
517// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
518//
519// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
520// to check it again in the rules here, because a network's permissions may have been updated via
521// modifyNetworkPermission().
522WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
523 Permission permission, uid_t uidStart,
524 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700525 Fwmark fwmark;
526 Fwmark mask;
527
528 fwmark.netId = netId;
529 mask.netId = FWMARK_NET_ID_MASK;
530
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700531 fwmark.explicitlySelected = true;
532 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700533
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700534 fwmark.permission = permission;
535 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700536
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700537 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700538 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700539}
540
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700541// A rule to route traffic based on a chosen outgoing interface.
542//
543// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
544// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900545WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
546 Permission permission, uid_t uidStart,
547 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700548 Fwmark fwmark;
549 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700550
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700551 fwmark.permission = permission;
552 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700553
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900554 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
555 // for UID. This covers forwarded packets and system daemons such as the tethering DHCP server.
556 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
557 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
558 table, fwmark.intValue, mask.intValue, IIF_NONE, interface,
559 UID_ROOT, UID_ROOT)) {
560 return ret;
561 }
562 }
563
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700564 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700565 fwmark.intValue, mask.intValue, IIF_NONE, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700566}
567
568// A rule to route traffic based on the chosen network.
569//
570// This is for sockets that have not explicitly requested a particular network, but have been
571// bound to one when they called connect(). This ensures that sockets connected on a particular
572// network stay on that network even if the default network changes.
573WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table,
574 Permission permission, bool add) {
575 Fwmark fwmark;
576 Fwmark mask;
577
578 fwmark.netId = netId;
579 mask.netId = FWMARK_NET_ID_MASK;
580
581 fwmark.explicitlySelected = false;
582 mask.explicitlySelected = true;
583
584 fwmark.permission = permission;
585 mask.permission = permission;
586
587 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700588 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700589}
590
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700591// A rule to enable split tunnel VPNs.
592//
593// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
594// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
595// permissions required by the default network.
596WARN_UNUSED_RESULT int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
597 const char* physicalInterface,
598 Permission permission) {
599 uint32_t table = getRouteTableForInterface(physicalInterface);
600 if (table == RT_TABLE_UNSPEC) {
601 return -ESRCH;
602 }
603
604 Fwmark fwmark;
605 Fwmark mask;
606
607 fwmark.netId = vpnNetId;
608 mask.netId = FWMARK_NET_ID_MASK;
609
610 fwmark.explicitlySelected = false;
611 mask.explicitlySelected = true;
612
613 fwmark.permission = permission;
614 mask.permission = permission;
615
616 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
617 mask.intValue);
618}
619
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700620// Add rules to allow legacy routes added through the requestRouteToHost() API.
621WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700622 Fwmark fwmark;
623 Fwmark mask;
624
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700625 fwmark.explicitlySelected = false;
626 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700627
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700628 // Rules to allow legacy routes to override the default network.
629 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
630 fwmark.intValue, mask.intValue)) {
631 return ret;
632 }
633 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
634 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
635 return ret;
636 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700637
638 fwmark.permission = PERMISSION_SYSTEM;
639 mask.permission = PERMISSION_SYSTEM;
640
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700641 // A rule to allow legacy routes from system apps to override VPNs.
642 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700643 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700644}
645
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700646// Add rules to lookup the local network when specified explicitly or otherwise.
647WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
648 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
649 INVALID_UID, INVALID_UID, ACTION_ADD)) {
650 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700651 }
652
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700653 Fwmark fwmark;
654 Fwmark mask;
655
656 fwmark.explicitlySelected = false;
657 mask.explicitlySelected = true;
658
659 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
660 fwmark.intValue, mask.intValue);
661}
662
Lorenzo Colitti36679362015-02-25 10:26:19 +0900663int configureDummyNetwork() {
664 const char *interface = DummyNetwork::INTERFACE_NAME;
665 uint32_t table = getRouteTableForInterface(interface);
666 if (table == RT_TABLE_UNSPEC) {
667 // getRouteTableForInterface has already looged an error.
668 return -ESRCH;
669 }
670
671 ifc_init();
672 int ret = ifc_up(interface);
673 ifc_close();
674 if (ret) {
675 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
676 return -errno;
677 }
678
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900679 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
680 INVALID_UID, INVALID_UID, ACTION_ADD))) {
681 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900682 return ret;
683 }
684
685 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900686 return ret;
687 }
688
689 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900690 return ret;
691 }
692
693 return 0;
694}
695
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000696// 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 +0900697// rule, but with a lower priority. We will never create routes in the main table; it should only be
698// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
699// This is necessary, for example, when adding a route through a directly-connected gateway: in
700// order to add the route, there must already be a directly-connected route that covers the gateway.
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000701WARN_UNUSED_RESULT int addDirectlyConnectedRule() {
702 Fwmark fwmark;
703 Fwmark mask;
704
705 fwmark.netId = NETID_UNSET;
706 mask.netId = FWMARK_NET_ID_MASK;
707
708 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
709 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT);
710}
711
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900712// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
713// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
714// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
715// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700716WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100717 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
718 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700719}
720
721WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700722 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
723 return ret;
724 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900725 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
726 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700727}
728
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700729WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
730 Permission permission, bool add) {
731 uint32_t table = getRouteTableForInterface(interface);
732 if (table == RT_TABLE_UNSPEC) {
733 return -ESRCH;
734 }
735
736 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
737 return ret;
738 }
739 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
740 add)) {
741 return ret;
742 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900743 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700744 add)) {
745 return ret;
746 }
747 return modifyImplicitNetworkRule(netId, table, permission, add);
748}
749
Robin Leeb8087362016-03-30 18:43:08 +0100750WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
751 Fwmark fwmark;
752 Fwmark mask;
753 fwmark.protectedFromVpn = false;
754 mask.protectedFromVpn = true;
755
Robin Leedc0d5782016-07-20 14:17:11 +0100756 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100757 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
758 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
759 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100760 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100761 return ret;
762 }
763 }
764
765 return 0;
766}
767
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700768WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700769 const UidRanges& uidRanges, bool secure, bool add,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700770 bool modifyNonUidBasedRules) {
771 uint32_t table = getRouteTableForInterface(interface);
772 if (table == RT_TABLE_UNSPEC) {
773 return -ESRCH;
774 }
775
Robin Leedc0d5782016-07-20 14:17:11 +0100776 for (const UidRange& range : uidRanges.getRanges()) {
777 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
778 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700779 return ret;
780 }
Robin Leedc0d5782016-07-20 14:17:11 +0100781 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
782 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700783 return ret;
784 }
Robin Leedc0d5782016-07-20 14:17:11 +0100785 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
786 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700787 return ret;
788 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700789 }
790
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700791 if (modifyNonUidBasedRules) {
792 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700793 return ret;
794 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700795 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
796 return ret;
797 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700798 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700799 return ret;
800 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700801 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700802 }
803
804 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700805}
806
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700807WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
808 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700809 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700810 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900811 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700812 }
813
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700814 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700815 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700816
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700817 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700818 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700819
820 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700821 mask.permission = permission;
822
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700823 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700824 mask.intValue);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700825}
826
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700827WARN_UNUSED_RESULT int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
828 const char* outputInterface) {
829 uint32_t table = getRouteTableForInterface(outputInterface);
830 if (table == RT_TABLE_UNSPEC) {
831 return -ESRCH;
832 }
833
834 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
835 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
836}
837
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700838// 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 +0900839// route, to the main table as well.
840// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700841WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
842 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700843 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700844 switch (tableType) {
845 case RouteController::INTERFACE: {
846 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700847 if (table == RT_TABLE_UNSPEC) {
848 return -ESRCH;
849 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700850 break;
851 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700852 case RouteController::LOCAL_NETWORK: {
853 table = ROUTE_TABLE_LOCAL_NETWORK;
854 break;
855 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700856 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700857 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700858 break;
859 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700860 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700861 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700862 break;
863 }
864 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700865
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900866 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700867 // Trying to add a route that already exists shouldn't cause an error.
868 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900869 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700870 }
871
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900872 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700873}
874
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900875WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
876 int ret = 0;
877 while (ret == 0) {
878 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
879 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
880 }
881
882 if (ret == -ENOENT) {
883 return 0;
884 } else {
885 return ret;
886 }
887}
888
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900889uint32_t getRulePriority(const nlmsghdr *nlh) {
890 return getRtmU32Attribute(nlh, FRA_PRIORITY);
891}
892
893uint32_t getRouteTable(const nlmsghdr *nlh) {
894 return getRtmU32Attribute(nlh, RTA_TABLE);
895}
896
897WARN_UNUSED_RESULT int flushRules() {
898 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
899 // Don't touch rules at priority 0 because by default they are used for local input.
900 return getRulePriority(nlh) != 0;
901 };
902 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
903}
904
905WARN_UNUSED_RESULT int flushRoutes(uint32_t table) {
906 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
907 return getRouteTable(nlh) == table;
908 };
909
910 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
911}
912
913// Returns 0 on success or negative errno on failure.
914WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
915 uint32_t table = getRouteTableForInterface(interface);
916 if (table == RT_TABLE_UNSPEC) {
917 return -ESRCH;
918 }
919
920 int ret = flushRoutes(table);
921
922 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
923 // track of its name.
924 if (ret == 0) {
925 interfaceToTable.erase(interface);
926 }
927
928 return ret;
929}
930
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700931int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700932 if (int ret = flushRules()) {
933 return ret;
934 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700935 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700936 return ret;
937 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700938 if (int ret = addLocalNetworkRules(localNetId)) {
939 return ret;
940 }
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000941 if (int ret = addDirectlyConnectedRule()) {
942 return ret;
943 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700944 if (int ret = addUnreachableRule()) {
945 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700946 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900947 // Don't complain if we can't add the dummy network, since not all devices support it.
948 configureDummyNetwork();
949
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700950 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700951 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700952}
953
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700954int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700955 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700956}
957
958int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700959 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700960}
961
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700962int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
963 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700964 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
965 return ret;
966 }
967 updateTableNamesFile();
968 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700969}
970
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700971int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
972 Permission permission) {
973 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700974 return ret;
975 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700976 if (int ret = flushRoutes(interface)) {
977 return ret;
978 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900979 if (int ret = clearTetheringRules(interface)) {
980 return ret;
981 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700982 updateTableNamesFile();
983 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700984}
985
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700986int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700987 bool secure, const UidRanges& uidRanges) {
988 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700989 MODIFY_NON_UID_BASED_RULES)) {
990 return ret;
991 }
992 updateTableNamesFile();
993 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700994}
995
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700996int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700997 bool secure, const UidRanges& uidRanges) {
998 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700999 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001000 return ret;
1001 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001002 if (int ret = flushRoutes(interface)) {
1003 return ret;
1004 }
1005 updateTableNamesFile();
1006 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001007}
1008
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001009int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1010 Permission oldPermission,
1011 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001012 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001013 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001014 return ret;
1015 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001016 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001017}
1018
Robin Leeb8087362016-03-30 18:43:08 +01001019int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1020 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1021}
1022
1023int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1024 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1025}
1026
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001027int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001028 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001029 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001030 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001031}
1032
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001033int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001034 bool secure, const UidRanges& uidRanges) {
1035 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001036 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001037}
1038
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001039int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1040 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001041}
1042
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001043int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1044 Permission permission) {
1045 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001046}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001047
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001048int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001049 TableType tableType) {
1050 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001051}
1052
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001053int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001054 const char* nexthop, TableType tableType) {
1055 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001056}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001057
1058int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001059 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001060}
1061
1062int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001063 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001064}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001065
1066int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1067 Permission permission) {
1068 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1069}
1070
1071int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1072 const char* physicalInterface,
1073 Permission permission) {
1074 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1075}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001076
1077} // namespace net
1078} // namespace android