blob: 98a965e7ce954de04c29264989b024e28ae923c1 [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
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070030#include "Fwmark.h"
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070031#include "UidRanges.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090032#include "DummyNetwork.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070033
34#define LOG_TAG "Netd"
35#include "log/log.h"
36#include "logwrap/logwrap.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090037#include "netutils/ifc.h"
Nicolas Geoffray4a0ab5f2015-03-16 10:28:37 +000038#include "utils/file.h"
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070039#include "resolv_netid.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070040
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070041namespace {
42
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070043// BEGIN CONSTANTS --------------------------------------------------------------------------------
44
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070045const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090046const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070047const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070048const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
49const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
50const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
51const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
52const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070053const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070054const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070055const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070056const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070057const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070058const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittia2c23052014-07-29 09:25:44 +000059const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 23000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090060const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070061
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070062const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070063const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
64const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
65
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070066const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070067const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
68const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
69
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070070const char* const ROUTE_TABLE_NAME_LOCAL = "local";
71const char* const ROUTE_TABLE_NAME_MAIN = "main";
72
Lorenzo Colitti59656512014-06-25 03:20:29 +090073// TODO: These values aren't defined by the Linux kernel, because our UID routing changes are not
74// upstream (yet?), so we can't just pick them up from kernel headers. When (if?) the changes make
75// it upstream, we'll remove this and rely on the kernel header values. For now, add a static assert
76// that will warn us if upstream has given these values some other meaning.
77const uint16_t FRA_UID_START = 18;
78const uint16_t FRA_UID_END = 19;
79static_assert(FRA_UID_START > FRA_MAX,
80 "Android-specific FRA_UID_{START,END} values also assigned in Linux uapi. "
81 "Check that these values match what the kernel does and then update this assertion.");
82
Lorenzo Colitti72723682014-06-26 13:51:10 +090083const uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK;
84const uint16_t NETLINK_CREATE_REQUEST_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE | NLM_F_EXCL;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090085
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070086const sockaddr_nl NETLINK_ADDRESS = {AF_NETLINK, 0, 0, 0};
87
88const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
89
90const char* const IP_VERSIONS[] = {"-4", "-6"};
91
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070092const uid_t UID_ROOT = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090093const char* const IIF_LOOPBACK = "lo";
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070094const char* const IIF_NONE = NULL;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070095const char* const OIF_NONE = NULL;
96const bool ACTION_ADD = true;
97const bool ACTION_DEL = false;
98const bool MODIFY_NON_UID_BASED_RULES = true;
99
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700100const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700101const 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 -0700102
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900103const unsigned ROUTE_FLUSH_ATTEMPTS = 2;
104
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700105// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
106// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
107constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
108 return RTA_LENGTH(x);
109}
110
111// These are practically const, but can't be declared so, because they are used to initialize
112// non-const pointers ("void* iov_base") in iovec arrays.
113rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
114rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
115rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
116rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
117rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START };
118rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END };
119
120rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
121rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
122
123uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
124
125// END CONSTANTS ----------------------------------------------------------------------------------
126
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700127// No locks needed because RouteController is accessed only from one thread (in CommandListener).
128std::map<std::string, uint32_t> interfaceToTable;
Paul Jensena561e122014-06-12 16:46:37 -0400129
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700130uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700131 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400132 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700133 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
134 interfaceToTable[interface] = index;
135 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400136 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700137 // If the interface goes away if_nametoindex() will return 0 but we still need to know
138 // the index so we can remove the rules and routes.
139 auto iter = interfaceToTable.find(interface);
140 if (iter == interfaceToTable.end()) {
141 ALOGE("cannot find interface %s", interface);
142 return RT_TABLE_UNSPEC;
143 }
144 return iter->second;
145}
146
147void addTableName(uint32_t table, const std::string& name, std::string* contents) {
148 char tableString[UINT32_STRLEN];
149 snprintf(tableString, sizeof(tableString), "%u", table);
150 *contents += tableString;
151 *contents += " ";
152 *contents += name;
153 *contents += "\n";
154}
155
156// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
157void updateTableNamesFile() {
158 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700159
160 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
161 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
162
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700163 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700164 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
165 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700166
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700167 for (const auto& entry : interfaceToTable) {
168 addTableName(entry.second, entry.first, &contents);
169 }
170
Nicolas Geoffray4a0ab5f2015-03-16 10:28:37 +0000171 if (!android::WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800172 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700173 return;
174 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700175}
176
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900177// Sends a netlink request and expects an ack.
178// |iov| is an array of struct iovec that contains the netlink message payload.
179// The netlink header is generated by this function based on |action| and |flags|.
180// Returns -errno if there was an error or if the kernel reported an error.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700181WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900182 nlmsghdr nlmsg = {
183 .nlmsg_type = action,
184 .nlmsg_flags = flags,
185 };
186 iov[0].iov_base = &nlmsg;
187 iov[0].iov_len = sizeof(nlmsg);
188 for (int i = 0; i < iovlen; ++i) {
189 nlmsg.nlmsg_len += iov[i].iov_len;
190 }
191
192 int ret;
193 struct {
194 nlmsghdr msg;
195 nlmsgerr err;
196 } response;
197
Nick Kralevich53ea9ca2015-01-31 13:54:00 -0800198 int sock = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900199 if (sock != -1 &&
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700200 connect(sock, reinterpret_cast<const sockaddr*>(&NETLINK_ADDRESS),
201 sizeof(NETLINK_ADDRESS)) != -1 &&
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900202 writev(sock, iov, iovlen) != -1 &&
203 (ret = recv(sock, &response, sizeof(response), 0)) != -1) {
204 if (ret == sizeof(response)) {
205 ret = response.err.error; // Netlink errors are negative errno.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700206 if (ret) {
207 ALOGE("netlink response contains error (%s)", strerror(-ret));
208 }
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900209 } else {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700210 ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response));
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900211 ret = -EBADMSG;
212 }
213 } else {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700214 ALOGE("netlink socket/connect/writev/recv failed (%s)", strerror(errno));
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900215 ret = -errno;
216 }
217
218 if (sock != -1) {
219 close(sock);
220 }
221
222 return ret;
223}
224
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700225// Returns 0 on success or negative errno on failure.
226int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
227 if (!input) {
228 *length = 0;
229 *padding = 0;
230 return 0;
231 }
232 *length = strlcpy(name, input, IFNAMSIZ) + 1;
233 if (*length > IFNAMSIZ) {
234 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
235 return -ENAMETOOLONG;
236 }
237 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
238 return 0;
239}
240
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700241// Adds or removes a routing rule for IPv4 and IPv6.
242//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900243// + If |priority| is RULE_PRIORITY_UNREACHABLE, the rule returns ENETUNREACH (i.e., specifies an
244// action of FR_ACT_UNREACHABLE). Otherwise, the rule specifies an action of FR_ACT_TO_TBL.
245// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
246// unspecified. An unspecified table is only allowed when deleting a rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700247// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
248// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700249// + If |iif| is non-NULL, the rule matches the specified incoming interface.
250// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
251// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
252// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900253//
254// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700255WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700256 uint32_t fwmark, uint32_t mask, const char* iif,
257 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700258 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
259 if (fwmark & ~mask) {
260 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
261 return -ERANGE;
262 }
263
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700264 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900265 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700266 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
267 size_t iifLength, oifLength;
268 uint16_t iifPadding, oifPadding;
269 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
270 return ret;
271 }
272 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
273 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900274 }
275
Lorenzo Colitti59656512014-06-25 03:20:29 +0900276 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700277 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700278 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900279 return -EUSERS;
280 }
Lorenzo Colitti72723682014-06-26 13:51:10 +0900281 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900282
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900283 // Assemble a rule request and put it in an array of iovec structures.
284 fib_rule_hdr rule = {
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900285 .action = static_cast<uint8_t>(priority != RULE_PRIORITY_UNREACHABLE ? FR_ACT_TO_TBL :
286 FR_ACT_UNREACHABLE),
287 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
288 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900289 };
290
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900291 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
292 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
293 // table 0, it's a wildcard that matches anything.
294 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
295 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
296 return -ENOTUNIQ;
297 }
298
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700299 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
300 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900301
302 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700303 { NULL, 0 },
304 { &rule, sizeof(rule) },
305 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
306 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700307 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
308 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700309 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
310 { &fwmark, mask ? sizeof(fwmark) : 0 },
311 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
312 { &mask, mask ? sizeof(mask) : 0 },
313 { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
314 { &uidStart, isUidRule ? sizeof(uidStart) : 0 },
315 { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 },
316 { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700317 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
318 { iifName, iifLength },
319 { PADDING_BUFFER, iifPadding },
320 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
321 { oifName, oifLength },
322 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900323 };
324
Lorenzo Colitti72723682014-06-26 13:51:10 +0900325 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700326 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
327 rule.family = AF_FAMILIES[i];
328 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov))) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900329 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700330 }
331 }
332
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900333 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700334}
335
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700336WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
337 uint32_t fwmark, uint32_t mask) {
338 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
339 INVALID_UID);
340}
341
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900342// Adds or deletes an IPv4 or IPv6 route.
343// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700344WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
345 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900346 // At least the destination must be non-null.
347 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700348 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900349 return -EFAULT;
350 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700351
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900352 // Parse the prefix.
353 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700354 uint8_t family;
355 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900356 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
357 &prefixLength);
358 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700359 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900360 return rawLength;
361 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700362
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900363 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700364 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900365 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
366 }
367
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700368 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900369 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900370 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700371
372 if (nexthop && !strcmp(nexthop, "unreachable")) {
373 type = RTN_UNREACHABLE;
374 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
375 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
376 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
377 interface = OIF_NONE;
378 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900379 } else if (nexthop && !strcmp(nexthop, "throw")) {
380 type = RTN_THROW;
381 interface = OIF_NONE;
382 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700383 } else {
384 // If an interface was specified, find the ifindex.
385 if (interface != OIF_NONE) {
386 ifindex = if_nametoindex(interface);
387 if (!ifindex) {
388 ALOGE("cannot find interface %s", interface);
389 return -ENODEV;
390 }
391 }
392
393 // If a nexthop was specified, parse it as the same family as the prefix.
394 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
395 ALOGE("inet_pton failed for nexthop %s", nexthop);
396 return -EINVAL;
397 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900398 }
399
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900400 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700401 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900402 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700403 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900404 .rtm_family = family,
405 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700406 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900407 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900408
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700409 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
410 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900411
412 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700413 { NULL, 0 },
414 { &route, sizeof(route) },
415 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
416 { &table, sizeof(table) },
417 { &rtaDst, sizeof(rtaDst) },
418 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700419 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
420 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700421 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
422 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900423 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900424
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700425 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
426 NETLINK_REQUEST_FLAGS;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900427 return sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700428}
429
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700430// An iptables rule to mark incoming packets on a network with the netId of the network.
431//
432// This is so that the kernel can:
433// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
434// replies, SYN-ACKs, etc).
435// + Mark sockets that accept connections from this interface so that the connection stays on the
436// same interface.
437WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
438 Permission permission, bool add) {
439 Fwmark fwmark;
440
441 fwmark.netId = netId;
442 fwmark.explicitlySelected = true;
443 fwmark.protectedFromVpn = true;
444 fwmark.permission = permission;
445
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700446 char markString[UINT32_HEX_STRLEN];
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700447 snprintf(markString, sizeof(markString), "0x%x", fwmark.intValue);
448
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700449 if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j",
450 "MARK", "--set-mark", markString, NULL)) {
451 ALOGE("failed to change iptables rule that sets incoming packet mark");
452 return -EREMOTEIO;
453 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700454
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700455 return 0;
456}
457
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700458// A rule to route responses to the local network forwarded via the VPN.
459//
460// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
461// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
462WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
463 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
464 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
465 INVALID_UID, INVALID_UID);
466}
467
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700468// A rule to route all traffic from a given set of UIDs to go over the VPN.
469//
470// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
471// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
472// bypass the VPN if the protectedFromVpn bit is set.
473WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700474 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700475 Fwmark fwmark;
476 Fwmark mask;
477
478 fwmark.protectedFromVpn = false;
479 mask.protectedFromVpn = true;
480
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700481 uint32_t priority;
482
483 if (secure) {
484 priority = RULE_PRIORITY_SECURE_VPN;
485 } else {
486 priority = RULE_PRIORITY_BYPASSABLE_VPN;
487
488 fwmark.explicitlySelected = false;
489 mask.explicitlySelected = true;
490 }
491
492 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900493 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700494}
495
496// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
497// set of UIDs.
498//
499// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
500// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700501WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
502 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700503 Fwmark fwmark;
504 Fwmark mask;
505
506 fwmark.netId = netId;
507 mask.netId = FWMARK_NET_ID_MASK;
508
509 fwmark.permission = PERMISSION_SYSTEM;
510 mask.permission = PERMISSION_SYSTEM;
511
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700512 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
513
514 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
515 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700516}
517
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700518// A rule to route traffic based on an explicitly chosen network.
519//
520// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
521//
522// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
523// to check it again in the rules here, because a network's permissions may have been updated via
524// modifyNetworkPermission().
525WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
526 Permission permission, uid_t uidStart,
527 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700528 Fwmark fwmark;
529 Fwmark mask;
530
531 fwmark.netId = netId;
532 mask.netId = FWMARK_NET_ID_MASK;
533
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700534 fwmark.explicitlySelected = true;
535 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700536
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700537 fwmark.permission = permission;
538 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700539
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700540 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700541 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700542}
543
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700544// A rule to route traffic based on a chosen outgoing interface.
545//
546// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
547// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900548WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
549 Permission permission, uid_t uidStart,
550 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700551 Fwmark fwmark;
552 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700553
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700554 fwmark.permission = permission;
555 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700556
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900557 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
558 // for UID. This covers forwarded packets and system daemons such as the tethering DHCP server.
559 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
560 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
561 table, fwmark.intValue, mask.intValue, IIF_NONE, interface,
562 UID_ROOT, UID_ROOT)) {
563 return ret;
564 }
565 }
566
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700567 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700568 fwmark.intValue, mask.intValue, IIF_NONE, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700569}
570
571// A rule to route traffic based on the chosen network.
572//
573// This is for sockets that have not explicitly requested a particular network, but have been
574// bound to one when they called connect(). This ensures that sockets connected on a particular
575// network stay on that network even if the default network changes.
576WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table,
577 Permission permission, bool add) {
578 Fwmark fwmark;
579 Fwmark mask;
580
581 fwmark.netId = netId;
582 mask.netId = FWMARK_NET_ID_MASK;
583
584 fwmark.explicitlySelected = false;
585 mask.explicitlySelected = true;
586
587 fwmark.permission = permission;
588 mask.permission = permission;
589
590 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700591 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700592}
593
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700594// A rule to enable split tunnel VPNs.
595//
596// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
597// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
598// permissions required by the default network.
599WARN_UNUSED_RESULT int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
600 const char* physicalInterface,
601 Permission permission) {
602 uint32_t table = getRouteTableForInterface(physicalInterface);
603 if (table == RT_TABLE_UNSPEC) {
604 return -ESRCH;
605 }
606
607 Fwmark fwmark;
608 Fwmark mask;
609
610 fwmark.netId = vpnNetId;
611 mask.netId = FWMARK_NET_ID_MASK;
612
613 fwmark.explicitlySelected = false;
614 mask.explicitlySelected = true;
615
616 fwmark.permission = permission;
617 mask.permission = permission;
618
619 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
620 mask.intValue);
621}
622
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700623// Add rules to allow legacy routes added through the requestRouteToHost() API.
624WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700625 Fwmark fwmark;
626 Fwmark mask;
627
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700628 fwmark.explicitlySelected = false;
629 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700630
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700631 // Rules to allow legacy routes to override the default network.
632 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
633 fwmark.intValue, mask.intValue)) {
634 return ret;
635 }
636 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
637 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
638 return ret;
639 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700640
641 fwmark.permission = PERMISSION_SYSTEM;
642 mask.permission = PERMISSION_SYSTEM;
643
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700644 // A rule to allow legacy routes from system apps to override VPNs.
645 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700646 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700647}
648
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700649// Add rules to lookup the local network when specified explicitly or otherwise.
650WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
651 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
652 INVALID_UID, INVALID_UID, ACTION_ADD)) {
653 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700654 }
655
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700656 Fwmark fwmark;
657 Fwmark mask;
658
659 fwmark.explicitlySelected = false;
660 mask.explicitlySelected = true;
661
662 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
663 fwmark.intValue, mask.intValue);
664}
665
Lorenzo Colitti36679362015-02-25 10:26:19 +0900666int configureDummyNetwork() {
667 const char *interface = DummyNetwork::INTERFACE_NAME;
668 uint32_t table = getRouteTableForInterface(interface);
669 if (table == RT_TABLE_UNSPEC) {
670 // getRouteTableForInterface has already looged an error.
671 return -ESRCH;
672 }
673
674 ifc_init();
675 int ret = ifc_up(interface);
676 ifc_close();
677 if (ret) {
678 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
679 return -errno;
680 }
681
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900682 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
683 INVALID_UID, INVALID_UID, ACTION_ADD))) {
684 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900685 return ret;
686 }
687
688 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
689 ALOGE("Can't add IPv4 default route to %s: %s", interface, strerror(-ret));
690 return ret;
691 }
692
693 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
694 ALOGE("Can't add IPv6 default route to %s: %s", interface, strerror(-ret));
695 return ret;
696 }
697
698 return 0;
699}
700
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000701// 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 +0900702// rule, but with a lower priority. We will never create routes in the main table; it should only be
703// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
704// This is necessary, for example, when adding a route through a directly-connected gateway: in
705// order to add the route, there must already be a directly-connected route that covers the gateway.
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000706WARN_UNUSED_RESULT int addDirectlyConnectedRule() {
707 Fwmark fwmark;
708 Fwmark mask;
709
710 fwmark.netId = NETID_UNSET;
711 mask.netId = FWMARK_NET_ID_MASK;
712
713 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
714 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT);
715}
716
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900717// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
718// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
719// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
720// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700721WARN_UNUSED_RESULT int addUnreachableRule() {
722 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, RT_TABLE_UNSPEC, MARK_UNSET,
723 MARK_UNSET);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700724}
725
726WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700727 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
728 return ret;
729 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900730 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
731 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700732}
733
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700734WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
735 Permission permission, bool add) {
736 uint32_t table = getRouteTableForInterface(interface);
737 if (table == RT_TABLE_UNSPEC) {
738 return -ESRCH;
739 }
740
741 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
742 return ret;
743 }
744 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
745 add)) {
746 return ret;
747 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900748 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700749 add)) {
750 return ret;
751 }
752 return modifyImplicitNetworkRule(netId, table, permission, add);
753}
754
755WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700756 const UidRanges& uidRanges, bool secure, bool add,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700757 bool modifyNonUidBasedRules) {
758 uint32_t table = getRouteTableForInterface(interface);
759 if (table == RT_TABLE_UNSPEC) {
760 return -ESRCH;
761 }
762
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700763 for (const UidRanges::Range& range : uidRanges.getRanges()) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700764 if (int ret = modifyVpnUidRangeRule(table, range.first, range.second, secure, add)) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700765 return ret;
766 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700767 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.first,
768 range.second, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700769 return ret;
770 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900771 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE, range.first,
772 range.second, add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700773 return ret;
774 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700775 }
776
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700777 if (modifyNonUidBasedRules) {
778 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700779 return ret;
780 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700781 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
782 return ret;
783 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700784 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700785 return ret;
786 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700787 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700788 }
789
790 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700791}
792
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700793WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
794 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700795 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700796 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900797 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700798 }
799
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700800 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700801 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700802
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700803 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700804 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700805
806 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700807 mask.permission = permission;
808
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700809 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700810 mask.intValue);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700811}
812
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700813WARN_UNUSED_RESULT int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
814 const char* outputInterface) {
815 uint32_t table = getRouteTableForInterface(outputInterface);
816 if (table == RT_TABLE_UNSPEC) {
817 return -ESRCH;
818 }
819
820 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
821 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
822}
823
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700824// Returns 0 on success or negative errno on failure.
825WARN_UNUSED_RESULT int flushRules() {
826 for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
827 const char* argv[] = {
828 IP_PATH,
829 IP_VERSIONS[i],
830 "rule",
831 "flush",
832 };
833 if (android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, false, false)) {
834 ALOGE("failed to flush rules");
835 return -EREMOTEIO;
836 }
837 }
838 return 0;
839}
840
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700841// 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 +0900842// route, to the main table as well.
843// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700844WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
845 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700846 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700847 switch (tableType) {
848 case RouteController::INTERFACE: {
849 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700850 if (table == RT_TABLE_UNSPEC) {
851 return -ESRCH;
852 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700853 break;
854 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700855 case RouteController::LOCAL_NETWORK: {
856 table = ROUTE_TABLE_LOCAL_NETWORK;
857 break;
858 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700859 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700860 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700861 break;
862 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700863 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700864 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700865 break;
866 }
867 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700868
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900869 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700870 // Trying to add a route that already exists shouldn't cause an error.
871 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900872 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700873 }
874
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900875 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700876}
877
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700878// Returns 0 on success or negative errno on failure.
879WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700880 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700881 if (table == RT_TABLE_UNSPEC) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700882 return -ESRCH;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700883 }
884
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900885 char tableString[UINT32_STRLEN];
886 snprintf(tableString, sizeof(tableString), "%u", table);
887
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900888 int ret = 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700889 for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900890 const char* argv[] = {
891 IP_PATH,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700892 IP_VERSIONS[i],
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700893 "route",
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900894 "flush",
895 "table",
896 tableString,
897 };
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900898
899 // A flush works by dumping routes and deleting each route as it's returned, and it can
900 // fail if something else deletes the route between the dump and the delete. This can
901 // happen, for example, if an interface goes down while we're trying to flush its routes.
902 // So try multiple times and only return an error if the last attempt fails.
903 //
904 // TODO: replace this with our own netlink code.
905 unsigned attempts = 0;
906 int err;
907 do {
908 err = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv),
909 NULL, false, false);
910 ++attempts;
911 } while (err != 0 && attempts < ROUTE_FLUSH_ATTEMPTS);
912 if (err) {
913 ALOGE("failed to flush %s routes in table %s after %d attempts",
914 IP_VERSIONS[i], tableString, attempts);
915 ret = -EREMOTEIO;
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900916 }
917 }
918
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900919 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
920 // track of its name.
921 if (!ret) {
922 interfaceToTable.erase(interface);
923 }
924
925 return ret;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700926}
927
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900928WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
929 int ret = 0;
930 while (ret == 0) {
931 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
932 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
933 }
934
935 if (ret == -ENOENT) {
936 return 0;
937 } else {
938 return ret;
939 }
940}
941
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700942} // namespace
943
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700944int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700945 if (int ret = flushRules()) {
946 return ret;
947 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700948 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700949 return ret;
950 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700951 if (int ret = addLocalNetworkRules(localNetId)) {
952 return ret;
953 }
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000954 if (int ret = addDirectlyConnectedRule()) {
955 return ret;
956 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700957 if (int ret = addUnreachableRule()) {
958 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700959 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900960 // Don't complain if we can't add the dummy network, since not all devices support it.
961 configureDummyNetwork();
962
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700963 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700964 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700965}
966
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700967int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700968 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700969}
970
971int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700972 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700973}
974
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700975int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
976 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700977 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
978 return ret;
979 }
980 updateTableNamesFile();
981 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700982}
983
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700984int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
985 Permission permission) {
986 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700987 return ret;
988 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700989 if (int ret = flushRoutes(interface)) {
990 return ret;
991 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900992 if (int ret = clearTetheringRules(interface)) {
993 return ret;
994 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700995 updateTableNamesFile();
996 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700997}
998
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700999int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001000 bool secure, const UidRanges& uidRanges) {
1001 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001002 MODIFY_NON_UID_BASED_RULES)) {
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::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001010 bool secure, const UidRanges& uidRanges) {
1011 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001012 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001013 return ret;
1014 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001015 if (int ret = flushRoutes(interface)) {
1016 return ret;
1017 }
1018 updateTableNamesFile();
1019 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001020}
1021
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001022int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1023 Permission oldPermission,
1024 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001025 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001026 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001027 return ret;
1028 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001029 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001030}
1031
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001032int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001033 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001034 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001035 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001036}
1037
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001038int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001039 bool secure, const UidRanges& uidRanges) {
1040 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001041 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001042}
1043
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001044int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1045 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001046}
1047
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001048int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1049 Permission permission) {
1050 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001051}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001052
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001053int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001054 TableType tableType) {
1055 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001056}
1057
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001058int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001059 const char* nexthop, TableType tableType) {
1060 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001061}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001062
1063int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001064 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001065}
1066
1067int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001068 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001069}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001070
1071int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1072 Permission permission) {
1073 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1074}
1075
1076int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1077 const char* physicalInterface,
1078 Permission permission) {
1079 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1080}