blob: d85bde47e0ca21fa5aee7631fab5a54fa23ca22d [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
Elliott Hughesbbd56262015-12-04 15:45:10 -080034#include "android-base/file.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070035#define LOG_TAG "Netd"
36#include "log/log.h"
37#include "logwrap/logwrap.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090038#include "netutils/ifc.h"
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070039#include "resolv_netid.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070040
Dan Albert5407e142015-03-16 10:05:59 -070041using android::base::WriteStringToFile;
Robin Leedc0d5782016-07-20 14:17:11 +010042using android::net::UidRange;
Dan Albert5407e142015-03-16 10:05:59 -070043
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070044namespace {
45
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070046// BEGIN CONSTANTS --------------------------------------------------------------------------------
47
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070048const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090049const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070050const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070051const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010052const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070053const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
54const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
55const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
56const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070057const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070058const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070059const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070060const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070061const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070062const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittia2c23052014-07-29 09:25:44 +000063const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 23000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090064const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070065
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070066const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070067const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
68const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
69
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070070const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070071const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
72const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
73
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070074const char* const ROUTE_TABLE_NAME_LOCAL = "local";
75const char* const ROUTE_TABLE_NAME_MAIN = "main";
76
Lorenzo Colitti59656512014-06-25 03:20:29 +090077// TODO: These values aren't defined by the Linux kernel, because our UID routing changes are not
78// upstream (yet?), so we can't just pick them up from kernel headers. When (if?) the changes make
79// it upstream, we'll remove this and rely on the kernel header values. For now, add a static assert
80// that will warn us if upstream has given these values some other meaning.
81const uint16_t FRA_UID_START = 18;
82const uint16_t FRA_UID_END = 19;
83static_assert(FRA_UID_START > FRA_MAX,
84 "Android-specific FRA_UID_{START,END} values also assigned in Linux uapi. "
85 "Check that these values match what the kernel does and then update this assertion.");
86
Lorenzo Colitti72723682014-06-26 13:51:10 +090087const uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK;
88const uint16_t NETLINK_CREATE_REQUEST_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE | NLM_F_EXCL;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090089
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070090const sockaddr_nl NETLINK_ADDRESS = {AF_NETLINK, 0, 0, 0};
91
92const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
93
94const char* const IP_VERSIONS[] = {"-4", "-6"};
95
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070096const uid_t UID_ROOT = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090097const char* const IIF_LOOPBACK = "lo";
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070098const char* const IIF_NONE = NULL;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070099const char* const OIF_NONE = NULL;
100const bool ACTION_ADD = true;
101const bool ACTION_DEL = false;
102const bool MODIFY_NON_UID_BASED_RULES = true;
103
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700104const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700105const mode_t RT_TABLES_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // mode 0644, rw-r--r--
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700106
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900107const unsigned ROUTE_FLUSH_ATTEMPTS = 2;
108
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700109// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
110// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
111constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
112 return RTA_LENGTH(x);
113}
114
115// These are practically const, but can't be declared so, because they are used to initialize
116// non-const pointers ("void* iov_base") in iovec arrays.
117rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
118rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
119rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
120rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
121rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START };
122rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END };
123
124rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
125rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
126
127uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
128
129// END CONSTANTS ----------------------------------------------------------------------------------
130
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700131// No locks needed because RouteController is accessed only from one thread (in CommandListener).
132std::map<std::string, uint32_t> interfaceToTable;
Paul Jensena561e122014-06-12 16:46:37 -0400133
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700134uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700135 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400136 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700137 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
138 interfaceToTable[interface] = index;
139 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400140 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700141 // If the interface goes away if_nametoindex() will return 0 but we still need to know
142 // the index so we can remove the rules and routes.
143 auto iter = interfaceToTable.find(interface);
144 if (iter == interfaceToTable.end()) {
145 ALOGE("cannot find interface %s", interface);
146 return RT_TABLE_UNSPEC;
147 }
148 return iter->second;
149}
150
151void addTableName(uint32_t table, const std::string& name, std::string* contents) {
152 char tableString[UINT32_STRLEN];
153 snprintf(tableString, sizeof(tableString), "%u", table);
154 *contents += tableString;
155 *contents += " ";
156 *contents += name;
157 *contents += "\n";
158}
159
160// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
161void updateTableNamesFile() {
162 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700163
164 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
165 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
166
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700167 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700168 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
169 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700170
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700171 for (const auto& entry : interfaceToTable) {
172 addTableName(entry.second, entry.first, &contents);
173 }
174
Dan Albert5407e142015-03-16 10:05:59 -0700175 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800176 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700177 return;
178 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700179}
180
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900181// Sends a netlink request and expects an ack.
182// |iov| is an array of struct iovec that contains the netlink message payload.
183// The netlink header is generated by this function based on |action| and |flags|.
184// Returns -errno if there was an error or if the kernel reported an error.
Evgenii Stepanovf65122c2016-02-11 16:55:56 -0800185
186// Disable optimizations in ASan build.
187// ASan reports an out-of-bounds 32-bit(!) access in the first loop of the
188// function (over iov[]).
189#ifdef __clang__
190#if __has_feature(address_sanitizer)
191__attribute__((optnone))
192#endif
193#endif
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700194WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900195 nlmsghdr nlmsg = {
196 .nlmsg_type = action,
197 .nlmsg_flags = flags,
198 };
199 iov[0].iov_base = &nlmsg;
200 iov[0].iov_len = sizeof(nlmsg);
201 for (int i = 0; i < iovlen; ++i) {
202 nlmsg.nlmsg_len += iov[i].iov_len;
203 }
204
205 int ret;
206 struct {
207 nlmsghdr msg;
208 nlmsgerr err;
209 } response;
210
Nick Kralevich53ea9ca2015-01-31 13:54:00 -0800211 int sock = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900212 if (sock != -1 &&
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700213 connect(sock, reinterpret_cast<const sockaddr*>(&NETLINK_ADDRESS),
214 sizeof(NETLINK_ADDRESS)) != -1 &&
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900215 writev(sock, iov, iovlen) != -1 &&
216 (ret = recv(sock, &response, sizeof(response), 0)) != -1) {
217 if (ret == sizeof(response)) {
218 ret = response.err.error; // Netlink errors are negative errno.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700219 if (ret) {
220 ALOGE("netlink response contains error (%s)", strerror(-ret));
221 }
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900222 } else {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700223 ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response));
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900224 ret = -EBADMSG;
225 }
226 } else {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700227 ALOGE("netlink socket/connect/writev/recv failed (%s)", strerror(errno));
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900228 ret = -errno;
229 }
230
231 if (sock != -1) {
232 close(sock);
233 }
234
235 return ret;
236}
237
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700238// Returns 0 on success or negative errno on failure.
239int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
240 if (!input) {
241 *length = 0;
242 *padding = 0;
243 return 0;
244 }
245 *length = strlcpy(name, input, IFNAMSIZ) + 1;
246 if (*length > IFNAMSIZ) {
247 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
248 return -ENAMETOOLONG;
249 }
250 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
251 return 0;
252}
253
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700254// Adds or removes a routing rule for IPv4 and IPv6.
255//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900256// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100257// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700258// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
259// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700260// + If |iif| is non-NULL, the rule matches the specified incoming interface.
261// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
262// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
263// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900264//
265// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100266WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
267 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700268 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700269 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
270 if (fwmark & ~mask) {
271 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
272 return -ERANGE;
273 }
274
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700275 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900276 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700277 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
278 size_t iifLength, oifLength;
279 uint16_t iifPadding, oifPadding;
280 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
281 return ret;
282 }
283 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
284 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900285 }
286
Lorenzo Colitti59656512014-06-25 03:20:29 +0900287 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700288 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700289 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900290 return -EUSERS;
291 }
Robin Lee4ef94642016-04-01 11:50:49 +0100292
Lorenzo Colitti72723682014-06-26 13:51:10 +0900293 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900294
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900295 // Assemble a rule request and put it in an array of iovec structures.
296 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100297 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900298 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
299 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900300 };
301
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900302 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
303 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
304 // table 0, it's a wildcard that matches anything.
305 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
306 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
307 return -ENOTUNIQ;
308 }
309
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700310 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
311 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900312
313 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700314 { NULL, 0 },
315 { &rule, sizeof(rule) },
316 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
317 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700318 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
319 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700320 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
321 { &fwmark, mask ? sizeof(fwmark) : 0 },
322 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
323 { &mask, mask ? sizeof(mask) : 0 },
324 { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
325 { &uidStart, isUidRule ? sizeof(uidStart) : 0 },
326 { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 },
327 { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700328 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
329 { iifName, iifLength },
330 { PADDING_BUFFER, iifPadding },
331 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
332 { oifName, oifLength },
333 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900334 };
335
Lorenzo Colitti72723682014-06-26 13:51:10 +0900336 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700337 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
338 rule.family = AF_FAMILIES[i];
339 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov))) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900340 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700341 }
342 }
343
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900344 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700345}
346
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700347WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100348 uint32_t fwmark, uint32_t mask, const char* iif,
349 const char* oif, uid_t uidStart, uid_t uidEnd) {
350 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
351 uidEnd);
352}
353
354WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700355 uint32_t fwmark, uint32_t mask) {
356 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
357 INVALID_UID);
358}
359
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900360// Adds or deletes an IPv4 or IPv6 route.
361// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700362WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
363 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900364 // At least the destination must be non-null.
365 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700366 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900367 return -EFAULT;
368 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700369
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900370 // Parse the prefix.
371 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700372 uint8_t family;
373 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900374 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
375 &prefixLength);
376 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700377 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900378 return rawLength;
379 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700380
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900381 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700382 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900383 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
384 }
385
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700386 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900387 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900388 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700389
390 if (nexthop && !strcmp(nexthop, "unreachable")) {
391 type = RTN_UNREACHABLE;
392 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
393 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
394 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
395 interface = OIF_NONE;
396 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900397 } else if (nexthop && !strcmp(nexthop, "throw")) {
398 type = RTN_THROW;
399 interface = OIF_NONE;
400 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700401 } else {
402 // If an interface was specified, find the ifindex.
403 if (interface != OIF_NONE) {
404 ifindex = if_nametoindex(interface);
405 if (!ifindex) {
406 ALOGE("cannot find interface %s", interface);
407 return -ENODEV;
408 }
409 }
410
411 // If a nexthop was specified, parse it as the same family as the prefix.
412 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
413 ALOGE("inet_pton failed for nexthop %s", nexthop);
414 return -EINVAL;
415 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900416 }
417
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900418 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700419 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900420 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700421 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900422 .rtm_family = family,
423 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700424 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900425 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900426
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700427 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
428 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900429
430 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700431 { NULL, 0 },
432 { &route, sizeof(route) },
433 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
434 { &table, sizeof(table) },
435 { &rtaDst, sizeof(rtaDst) },
436 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700437 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
438 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700439 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
440 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900441 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900442
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700443 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
444 NETLINK_REQUEST_FLAGS;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900445 return sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700446}
447
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700448// An iptables rule to mark incoming packets on a network with the netId of the network.
449//
450// This is so that the kernel can:
451// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
452// replies, SYN-ACKs, etc).
453// + Mark sockets that accept connections from this interface so that the connection stays on the
454// same interface.
455WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
456 Permission permission, bool add) {
457 Fwmark fwmark;
458
459 fwmark.netId = netId;
460 fwmark.explicitlySelected = true;
461 fwmark.protectedFromVpn = true;
462 fwmark.permission = permission;
463
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700464 char markString[UINT32_HEX_STRLEN];
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700465 snprintf(markString, sizeof(markString), "0x%x", fwmark.intValue);
466
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700467 if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j",
468 "MARK", "--set-mark", markString, NULL)) {
469 ALOGE("failed to change iptables rule that sets incoming packet mark");
470 return -EREMOTEIO;
471 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700472
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700473 return 0;
474}
475
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700476// A rule to route responses to the local network forwarded via the VPN.
477//
478// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
479// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
480WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
481 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
482 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
483 INVALID_UID, INVALID_UID);
484}
485
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700486// A rule to route all traffic from a given set of UIDs to go over the VPN.
487//
488// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
489// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
490// bypass the VPN if the protectedFromVpn bit is set.
491WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700492 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700493 Fwmark fwmark;
494 Fwmark mask;
495
496 fwmark.protectedFromVpn = false;
497 mask.protectedFromVpn = true;
498
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700499 uint32_t priority;
500
501 if (secure) {
502 priority = RULE_PRIORITY_SECURE_VPN;
503 } else {
504 priority = RULE_PRIORITY_BYPASSABLE_VPN;
505
506 fwmark.explicitlySelected = false;
507 mask.explicitlySelected = true;
508 }
509
510 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900511 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700512}
513
514// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
515// set of UIDs.
516//
517// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
518// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700519WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
520 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700521 Fwmark fwmark;
522 Fwmark mask;
523
524 fwmark.netId = netId;
525 mask.netId = FWMARK_NET_ID_MASK;
526
527 fwmark.permission = PERMISSION_SYSTEM;
528 mask.permission = PERMISSION_SYSTEM;
529
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700530 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
531
532 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
533 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700534}
535
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700536// A rule to route traffic based on an explicitly chosen network.
537//
538// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
539//
540// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
541// to check it again in the rules here, because a network's permissions may have been updated via
542// modifyNetworkPermission().
543WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
544 Permission permission, uid_t uidStart,
545 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700546 Fwmark fwmark;
547 Fwmark mask;
548
549 fwmark.netId = netId;
550 mask.netId = FWMARK_NET_ID_MASK;
551
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700552 fwmark.explicitlySelected = true;
553 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700554
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700555 fwmark.permission = permission;
556 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700557
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700558 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700559 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700560}
561
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700562// A rule to route traffic based on a chosen outgoing interface.
563//
564// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
565// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900566WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
567 Permission permission, uid_t uidStart,
568 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700569 Fwmark fwmark;
570 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700571
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700572 fwmark.permission = permission;
573 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700574
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900575 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
576 // for UID. This covers forwarded packets and system daemons such as the tethering DHCP server.
577 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
578 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
579 table, fwmark.intValue, mask.intValue, IIF_NONE, interface,
580 UID_ROOT, UID_ROOT)) {
581 return ret;
582 }
583 }
584
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700585 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700586 fwmark.intValue, mask.intValue, IIF_NONE, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700587}
588
589// A rule to route traffic based on the chosen network.
590//
591// This is for sockets that have not explicitly requested a particular network, but have been
592// bound to one when they called connect(). This ensures that sockets connected on a particular
593// network stay on that network even if the default network changes.
594WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table,
595 Permission permission, bool add) {
596 Fwmark fwmark;
597 Fwmark mask;
598
599 fwmark.netId = netId;
600 mask.netId = FWMARK_NET_ID_MASK;
601
602 fwmark.explicitlySelected = false;
603 mask.explicitlySelected = true;
604
605 fwmark.permission = permission;
606 mask.permission = permission;
607
608 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700609 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700610}
611
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700612// A rule to enable split tunnel VPNs.
613//
614// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
615// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
616// permissions required by the default network.
617WARN_UNUSED_RESULT int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
618 const char* physicalInterface,
619 Permission permission) {
620 uint32_t table = getRouteTableForInterface(physicalInterface);
621 if (table == RT_TABLE_UNSPEC) {
622 return -ESRCH;
623 }
624
625 Fwmark fwmark;
626 Fwmark mask;
627
628 fwmark.netId = vpnNetId;
629 mask.netId = FWMARK_NET_ID_MASK;
630
631 fwmark.explicitlySelected = false;
632 mask.explicitlySelected = true;
633
634 fwmark.permission = permission;
635 mask.permission = permission;
636
637 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
638 mask.intValue);
639}
640
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700641// Add rules to allow legacy routes added through the requestRouteToHost() API.
642WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700643 Fwmark fwmark;
644 Fwmark mask;
645
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700646 fwmark.explicitlySelected = false;
647 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700648
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700649 // Rules to allow legacy routes to override the default network.
650 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
651 fwmark.intValue, mask.intValue)) {
652 return ret;
653 }
654 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
655 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
656 return ret;
657 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700658
659 fwmark.permission = PERMISSION_SYSTEM;
660 mask.permission = PERMISSION_SYSTEM;
661
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700662 // A rule to allow legacy routes from system apps to override VPNs.
663 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700664 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700665}
666
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700667// Add rules to lookup the local network when specified explicitly or otherwise.
668WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
669 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
670 INVALID_UID, INVALID_UID, ACTION_ADD)) {
671 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700672 }
673
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700674 Fwmark fwmark;
675 Fwmark mask;
676
677 fwmark.explicitlySelected = false;
678 mask.explicitlySelected = true;
679
680 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
681 fwmark.intValue, mask.intValue);
682}
683
Lorenzo Colitti36679362015-02-25 10:26:19 +0900684int configureDummyNetwork() {
685 const char *interface = DummyNetwork::INTERFACE_NAME;
686 uint32_t table = getRouteTableForInterface(interface);
687 if (table == RT_TABLE_UNSPEC) {
688 // getRouteTableForInterface has already looged an error.
689 return -ESRCH;
690 }
691
692 ifc_init();
693 int ret = ifc_up(interface);
694 ifc_close();
695 if (ret) {
696 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
697 return -errno;
698 }
699
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900700 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
701 INVALID_UID, INVALID_UID, ACTION_ADD))) {
702 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900703 return ret;
704 }
705
706 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
707 ALOGE("Can't add IPv4 default route to %s: %s", interface, strerror(-ret));
708 return ret;
709 }
710
711 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
712 ALOGE("Can't add IPv6 default route to %s: %s", interface, strerror(-ret));
713 return ret;
714 }
715
716 return 0;
717}
718
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000719// 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 +0900720// rule, but with a lower priority. We will never create routes in the main table; it should only be
721// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
722// This is necessary, for example, when adding a route through a directly-connected gateway: in
723// order to add the route, there must already be a directly-connected route that covers the gateway.
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000724WARN_UNUSED_RESULT int addDirectlyConnectedRule() {
725 Fwmark fwmark;
726 Fwmark mask;
727
728 fwmark.netId = NETID_UNSET;
729 mask.netId = FWMARK_NET_ID_MASK;
730
731 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
732 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT);
733}
734
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900735// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
736// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
737// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
738// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700739WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100740 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
741 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700742}
743
744WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700745 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
746 return ret;
747 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900748 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
749 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700750}
751
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700752WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
753 Permission permission, bool add) {
754 uint32_t table = getRouteTableForInterface(interface);
755 if (table == RT_TABLE_UNSPEC) {
756 return -ESRCH;
757 }
758
759 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
760 return ret;
761 }
762 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
763 add)) {
764 return ret;
765 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900766 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700767 add)) {
768 return ret;
769 }
770 return modifyImplicitNetworkRule(netId, table, permission, add);
771}
772
Robin Leeb8087362016-03-30 18:43:08 +0100773WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
774 Fwmark fwmark;
775 Fwmark mask;
776 fwmark.protectedFromVpn = false;
777 mask.protectedFromVpn = true;
778
Robin Leedc0d5782016-07-20 14:17:11 +0100779 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100780 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
781 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
782 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100783 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100784 return ret;
785 }
786 }
787
788 return 0;
789}
790
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700791WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700792 const UidRanges& uidRanges, bool secure, bool add,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700793 bool modifyNonUidBasedRules) {
794 uint32_t table = getRouteTableForInterface(interface);
795 if (table == RT_TABLE_UNSPEC) {
796 return -ESRCH;
797 }
798
Robin Leedc0d5782016-07-20 14:17:11 +0100799 for (const UidRange& range : uidRanges.getRanges()) {
800 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
801 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700802 return ret;
803 }
Robin Leedc0d5782016-07-20 14:17:11 +0100804 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
805 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700806 return ret;
807 }
Robin Leedc0d5782016-07-20 14:17:11 +0100808 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
809 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700810 return ret;
811 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700812 }
813
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700814 if (modifyNonUidBasedRules) {
815 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700816 return ret;
817 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700818 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
819 return ret;
820 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700821 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700822 return ret;
823 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700824 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700825 }
826
827 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700828}
829
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700830WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
831 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700832 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700833 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900834 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700835 }
836
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700837 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700838 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700839
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700840 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700841 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700842
843 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700844 mask.permission = permission;
845
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700846 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700847 mask.intValue);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700848}
849
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700850WARN_UNUSED_RESULT int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
851 const char* outputInterface) {
852 uint32_t table = getRouteTableForInterface(outputInterface);
853 if (table == RT_TABLE_UNSPEC) {
854 return -ESRCH;
855 }
856
857 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
858 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
859}
860
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700861// Returns 0 on success or negative errno on failure.
862WARN_UNUSED_RESULT int flushRules() {
863 for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
864 const char* argv[] = {
865 IP_PATH,
866 IP_VERSIONS[i],
867 "rule",
868 "flush",
869 };
870 if (android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, false, false)) {
871 ALOGE("failed to flush rules");
872 return -EREMOTEIO;
873 }
874 }
875 return 0;
876}
877
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700878// 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 +0900879// route, to the main table as well.
880// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700881WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
882 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700883 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700884 switch (tableType) {
885 case RouteController::INTERFACE: {
886 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700887 if (table == RT_TABLE_UNSPEC) {
888 return -ESRCH;
889 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700890 break;
891 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700892 case RouteController::LOCAL_NETWORK: {
893 table = ROUTE_TABLE_LOCAL_NETWORK;
894 break;
895 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700896 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700897 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700898 break;
899 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700900 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700901 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700902 break;
903 }
904 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700905
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900906 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700907 // Trying to add a route that already exists shouldn't cause an error.
908 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900909 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700910 }
911
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900912 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700913}
914
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700915// Returns 0 on success or negative errno on failure.
916WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700917 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700918 if (table == RT_TABLE_UNSPEC) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700919 return -ESRCH;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700920 }
921
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900922 char tableString[UINT32_STRLEN];
923 snprintf(tableString, sizeof(tableString), "%u", table);
924
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900925 int ret = 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700926 for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900927 const char* argv[] = {
928 IP_PATH,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700929 IP_VERSIONS[i],
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700930 "route",
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900931 "flush",
932 "table",
933 tableString,
934 };
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900935
936 // A flush works by dumping routes and deleting each route as it's returned, and it can
937 // fail if something else deletes the route between the dump and the delete. This can
938 // happen, for example, if an interface goes down while we're trying to flush its routes.
939 // So try multiple times and only return an error if the last attempt fails.
940 //
941 // TODO: replace this with our own netlink code.
942 unsigned attempts = 0;
943 int err;
944 do {
945 err = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv),
946 NULL, false, false);
947 ++attempts;
948 } while (err != 0 && attempts < ROUTE_FLUSH_ATTEMPTS);
949 if (err) {
950 ALOGE("failed to flush %s routes in table %s after %d attempts",
951 IP_VERSIONS[i], tableString, attempts);
952 ret = -EREMOTEIO;
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900953 }
954 }
955
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900956 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
957 // track of its name.
958 if (!ret) {
959 interfaceToTable.erase(interface);
960 }
961
962 return ret;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700963}
964
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900965WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
966 int ret = 0;
967 while (ret == 0) {
968 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
969 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
970 }
971
972 if (ret == -ENOENT) {
973 return 0;
974 } else {
975 return ret;
976 }
977}
978
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700979} // namespace
980
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700981int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700982 if (int ret = flushRules()) {
983 return ret;
984 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700985 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700986 return ret;
987 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700988 if (int ret = addLocalNetworkRules(localNetId)) {
989 return ret;
990 }
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000991 if (int ret = addDirectlyConnectedRule()) {
992 return ret;
993 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700994 if (int ret = addUnreachableRule()) {
995 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700996 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900997 // Don't complain if we can't add the dummy network, since not all devices support it.
998 configureDummyNetwork();
999
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001000 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001001 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -07001002}
1003
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001004int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001005 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001006}
1007
1008int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001009 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001010}
1011
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001012int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
1013 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001014 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
1015 return ret;
1016 }
1017 updateTableNamesFile();
1018 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001019}
1020
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001021int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
1022 Permission permission) {
1023 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001024 return ret;
1025 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001026 if (int ret = flushRoutes(interface)) {
1027 return ret;
1028 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001029 if (int ret = clearTetheringRules(interface)) {
1030 return ret;
1031 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001032 updateTableNamesFile();
1033 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001034}
1035
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001036int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001037 bool secure, const UidRanges& uidRanges) {
1038 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001039 MODIFY_NON_UID_BASED_RULES)) {
1040 return ret;
1041 }
1042 updateTableNamesFile();
1043 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001044}
1045
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001046int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001047 bool secure, const UidRanges& uidRanges) {
1048 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001049 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001050 return ret;
1051 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001052 if (int ret = flushRoutes(interface)) {
1053 return ret;
1054 }
1055 updateTableNamesFile();
1056 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001057}
1058
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001059int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1060 Permission oldPermission,
1061 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001062 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001063 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001064 return ret;
1065 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001066 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001067}
1068
Robin Leeb8087362016-03-30 18:43:08 +01001069int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1070 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1071}
1072
1073int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1074 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1075}
1076
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001077int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001078 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001079 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001080 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001081}
1082
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001083int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001084 bool secure, const UidRanges& uidRanges) {
1085 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001086 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001087}
1088
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001089int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1090 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001091}
1092
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001093int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1094 Permission permission) {
1095 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001096}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001097
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001098int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001099 TableType tableType) {
1100 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001101}
1102
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001103int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001104 const char* nexthop, TableType tableType) {
1105 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001106}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001107
1108int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001109 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001110}
1111
1112int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001113 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001114}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001115
1116int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1117 Permission permission) {
1118 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1119}
1120
1121int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1122 const char* physicalInterface,
1123 Permission permission) {
1124 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1125}