blob: f61a5effd20258a21ee1044c19bb5b7d1f54d656 [file] [log] [blame]
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "RouteController.h"
18
Dan Albertaa1be2b2015-01-06 09:36:17 -080019#include <arpa/inet.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <linux/fib_rules.h>
23#include <net/if.h>
24#include <sys/stat.h>
25
Elliott Hughesbd378322015-02-04 13:25:14 -080026#include <private/android_filesystem_config.h>
27
Dan Albertaa1be2b2015-01-06 09:36:17 -080028#include <map>
29
Lorenzo Colitti7035f222017-02-13 18:29:00 +090030#include "DummyNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070031#include "Fwmark.h"
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070032#include "UidRanges.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
Lorenzo Colitti7035f222017-02-13 18:29:00 +090044namespace android {
45namespace net {
46
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070047namespace {
48
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070049// BEGIN CONSTANTS --------------------------------------------------------------------------------
50
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070051const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090052const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070053const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070054const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010055const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070056const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
57const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
58const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
59const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070060const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070061const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070062const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070063const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070064const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070065const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittia2c23052014-07-29 09:25:44 +000066const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 23000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090067const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070068
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070069const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070070const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
71const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
72
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070073const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070074const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
75const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
76
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070077const char* const ROUTE_TABLE_NAME_LOCAL = "local";
78const char* const ROUTE_TABLE_NAME_MAIN = "main";
79
Lorenzo Colitti2b078672016-12-16 18:45:03 +090080// TODO: These values aren't defined by the Linux kernel, because legacy UID routing (as used in N
81// and below) was not upstreamed. Now that the UID routing code is upstream, we should remove these
82// and rely on the kernel header values.
Lorenzo Colitti59656512014-06-25 03:20:29 +090083const uint16_t FRA_UID_START = 18;
84const uint16_t FRA_UID_END = 19;
Lorenzo Colitti2b078672016-12-16 18:45:03 +090085
86// These values are upstream, but not yet in our headers.
87// TODO: delete these definitions when updating the headers.
88const uint16_t FRA_UID_RANGE = 20;
89struct fib_rule_uid_range {
90 __u32 start;
91 __u32 end;
92};
Lorenzo Colitti59656512014-06-25 03:20:29 +090093
Lorenzo Colitti72723682014-06-26 13:51:10 +090094const uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK;
95const uint16_t NETLINK_CREATE_REQUEST_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE | NLM_F_EXCL;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090096
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070097const sockaddr_nl NETLINK_ADDRESS = {AF_NETLINK, 0, 0, 0};
98
99const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
100
101const char* const IP_VERSIONS[] = {"-4", "-6"};
102
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700103const uid_t UID_ROOT = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900104const char* const IIF_LOOPBACK = "lo";
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700105const char* const IIF_NONE = NULL;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700106const char* const OIF_NONE = NULL;
107const bool ACTION_ADD = true;
108const bool ACTION_DEL = false;
109const bool MODIFY_NON_UID_BASED_RULES = true;
110
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700111const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700112const 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 -0700113
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900114const unsigned ROUTE_FLUSH_ATTEMPTS = 2;
115
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700116// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
117// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
118constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
119 return RTA_LENGTH(x);
120}
121
122// These are practically const, but can't be declared so, because they are used to initialize
123// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900124rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
125rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
126rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
127rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
128rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START };
129rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END };
130rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700131
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900132rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
133rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700134
135uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
136
137// END CONSTANTS ----------------------------------------------------------------------------------
138
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900139const char *actionName(uint16_t action) {
140 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
141 return ops[action % 4];
142}
143
144const char *familyName(uint8_t family) {
145 switch (family) {
146 case AF_INET: return "IPv4";
147 case AF_INET6: return "IPv6";
148 default: return "???";
149 }
150}
151
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700152// No locks needed because RouteController is accessed only from one thread (in CommandListener).
153std::map<std::string, uint32_t> interfaceToTable;
Paul Jensena561e122014-06-12 16:46:37 -0400154
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700155uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700156 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400157 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700158 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
159 interfaceToTable[interface] = index;
160 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400161 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700162 // If the interface goes away if_nametoindex() will return 0 but we still need to know
163 // the index so we can remove the rules and routes.
164 auto iter = interfaceToTable.find(interface);
165 if (iter == interfaceToTable.end()) {
166 ALOGE("cannot find interface %s", interface);
167 return RT_TABLE_UNSPEC;
168 }
169 return iter->second;
170}
171
172void addTableName(uint32_t table, const std::string& name, std::string* contents) {
173 char tableString[UINT32_STRLEN];
174 snprintf(tableString, sizeof(tableString), "%u", table);
175 *contents += tableString;
176 *contents += " ";
177 *contents += name;
178 *contents += "\n";
179}
180
181// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
182void updateTableNamesFile() {
183 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700184
185 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
186 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
187
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700188 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700189 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
190 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700191
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700192 for (const auto& entry : interfaceToTable) {
193 addTableName(entry.second, entry.first, &contents);
194 }
195
Dan Albert5407e142015-03-16 10:05:59 -0700196 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800197 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700198 return;
199 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700200}
201
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900202// Sends a netlink request and possibly expects an ack.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900203// |iov| is an array of struct iovec that contains the netlink message payload.
204// The netlink header is generated by this function based on |action| and |flags|.
205// Returns -errno if there was an error or if the kernel reported an error.
Evgenii Stepanovf65122c2016-02-11 16:55:56 -0800206
207// Disable optimizations in ASan build.
208// ASan reports an out-of-bounds 32-bit(!) access in the first loop of the
209// function (over iov[]).
210#ifdef __clang__
211#if __has_feature(address_sanitizer)
212__attribute__((optnone))
213#endif
214#endif
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700215WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900216 nlmsghdr nlmsg = {
217 .nlmsg_type = action,
218 .nlmsg_flags = flags,
219 };
220 iov[0].iov_base = &nlmsg;
221 iov[0].iov_len = sizeof(nlmsg);
222 for (int i = 0; i < iovlen; ++i) {
223 nlmsg.nlmsg_len += iov[i].iov_len;
224 }
225
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900226 int sock = socket(AF_NETLINK, SOCK_DGRAM | SOCK_CLOEXEC, NETLINK_ROUTE);
227 if (sock == -1) {
228 return -errno;
229 }
230
231 int ret = 0;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900232 struct {
233 nlmsghdr msg;
234 nlmsgerr err;
235 } response;
236
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900237
238 if (connect(sock, reinterpret_cast<const sockaddr*>(&NETLINK_ADDRESS),
239 sizeof(NETLINK_ADDRESS)) == -1 || writev(sock, iov, iovlen) == -1) {
240 ret = -errno;
241 ALOGE("netlink socket connect/writev failed (%s)", strerror(-ret));
242 close(sock);
243 return ret;
244 }
245
246 if (flags & NLM_F_ACK) {
247 ret = recv(sock, &response, sizeof(response), 0);
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900248 if (ret == sizeof(response)) {
249 ret = response.err.error; // Netlink errors are negative errno.
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900250 } else if (ret == -1) {
251 ret = -errno;
252 ALOGE("netlink recv failed (%s)", strerror(-ret));
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900253 } else {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700254 ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response));
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900255 ret = -EBADMSG;
256 }
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900257 }
258
259 if (sock != -1) {
260 close(sock);
261 }
262
263 return ret;
264}
265
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700266// Returns 0 on success or negative errno on failure.
267int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
268 if (!input) {
269 *length = 0;
270 *padding = 0;
271 return 0;
272 }
273 *length = strlcpy(name, input, IFNAMSIZ) + 1;
274 if (*length > IFNAMSIZ) {
275 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
276 return -ENAMETOOLONG;
277 }
278 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
279 return 0;
280}
281
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700282// Adds or removes a routing rule for IPv4 and IPv6.
283//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900284// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100285// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700286// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
287// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700288// + If |iif| is non-NULL, the rule matches the specified incoming interface.
289// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
290// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
291// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900292//
293// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100294WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
295 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700296 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700297 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
298 if (fwmark & ~mask) {
299 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
300 return -ERANGE;
301 }
302
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700303 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900304 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700305 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
306 size_t iifLength, oifLength;
307 uint16_t iifPadding, oifPadding;
308 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
309 return ret;
310 }
311 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
312 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900313 }
314
Lorenzo Colitti59656512014-06-25 03:20:29 +0900315 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700316 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700317 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900318 return -EUSERS;
319 }
Robin Lee4ef94642016-04-01 11:50:49 +0100320
Lorenzo Colitti72723682014-06-26 13:51:10 +0900321 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900322
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900323 // Assemble a rule request and put it in an array of iovec structures.
324 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100325 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900326 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
327 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900328 };
329
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900330 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
331 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
332 // table 0, it's a wildcard that matches anything.
333 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
334 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
335 return -ENOTUNIQ;
336 }
337
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700338 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
339 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900340 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900341
342 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700343 { NULL, 0 },
344 { &rule, sizeof(rule) },
345 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
346 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700347 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
348 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700349 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
350 { &fwmark, mask ? sizeof(fwmark) : 0 },
351 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
352 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900353 // Rules that contain both legacy and new UID routing attributes will work on old kernels,
354 // which will simply ignore the FRA_UID_RANGE attribute since it is larger than their
355 // FRA_MAX. They will also work on kernels that are not too new:
356 // - FRA_UID_START clashes with FRA_PAD in 4.7, but that shouldn't be a problem because
357 // FRA_PAD has no validation.
358 // - FRA_UID_END clashes with FRA_L3MDEV in 4.8 and above, and will cause an error because
359 // FRA_L3MDEV has a maximum length of 1.
360 // TODO: delete the legacy UID routing code before running it on 4.8 or above.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700361 { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
362 { &uidStart, isUidRule ? sizeof(uidStart) : 0 },
363 { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 },
364 { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900365 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
366 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700367 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
368 { iifName, iifLength },
369 { PADDING_BUFFER, iifPadding },
370 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
371 { oifName, oifLength },
372 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900373 };
374
Lorenzo Colitti72723682014-06-26 13:51:10 +0900375 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700376 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
377 rule.family = AF_FAMILIES[i];
378 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov))) {
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900379 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
380 strerror(-ret));
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900381 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700382 }
383 }
384
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900385 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700386}
387
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700388WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100389 uint32_t fwmark, uint32_t mask, const char* iif,
390 const char* oif, uid_t uidStart, uid_t uidEnd) {
391 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
392 uidEnd);
393}
394
395WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700396 uint32_t fwmark, uint32_t mask) {
397 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
398 INVALID_UID);
399}
400
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900401// Adds or deletes an IPv4 or IPv6 route.
402// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700403WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
404 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900405 // At least the destination must be non-null.
406 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700407 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900408 return -EFAULT;
409 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700410
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900411 // Parse the prefix.
412 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700413 uint8_t family;
414 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900415 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
416 &prefixLength);
417 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700418 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900419 return rawLength;
420 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700421
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900422 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700423 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900424 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
425 }
426
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700427 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900428 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900429 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700430
431 if (nexthop && !strcmp(nexthop, "unreachable")) {
432 type = RTN_UNREACHABLE;
433 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
434 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
435 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
436 interface = OIF_NONE;
437 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900438 } else if (nexthop && !strcmp(nexthop, "throw")) {
439 type = RTN_THROW;
440 interface = OIF_NONE;
441 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700442 } else {
443 // If an interface was specified, find the ifindex.
444 if (interface != OIF_NONE) {
445 ifindex = if_nametoindex(interface);
446 if (!ifindex) {
447 ALOGE("cannot find interface %s", interface);
448 return -ENODEV;
449 }
450 }
451
452 // If a nexthop was specified, parse it as the same family as the prefix.
453 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
454 ALOGE("inet_pton failed for nexthop %s", nexthop);
455 return -EINVAL;
456 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900457 }
458
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900459 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700460 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900461 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700462 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900463 .rtm_family = family,
464 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700465 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900466 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900467
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700468 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
469 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900470
471 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700472 { NULL, 0 },
473 { &route, sizeof(route) },
474 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
475 { &table, sizeof(table) },
476 { &rtaDst, sizeof(rtaDst) },
477 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700478 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
479 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700480 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
481 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900482 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900483
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700484 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
485 NETLINK_REQUEST_FLAGS;
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900486 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
487 if (ret) {
488 ALOGE("Error %s route %s -> %s %s to table %u: %s",
489 actionName(action), destination, nexthop, interface, table, strerror(-ret));
490 }
491 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700492}
493
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700494// An iptables rule to mark incoming packets on a network with the netId of the network.
495//
496// This is so that the kernel can:
497// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
498// replies, SYN-ACKs, etc).
499// + Mark sockets that accept connections from this interface so that the connection stays on the
500// same interface.
501WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
502 Permission permission, bool add) {
503 Fwmark fwmark;
504
505 fwmark.netId = netId;
506 fwmark.explicitlySelected = true;
507 fwmark.protectedFromVpn = true;
508 fwmark.permission = permission;
509
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700510 char markString[UINT32_HEX_STRLEN];
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700511 snprintf(markString, sizeof(markString), "0x%x", fwmark.intValue);
512
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700513 if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j",
514 "MARK", "--set-mark", markString, NULL)) {
515 ALOGE("failed to change iptables rule that sets incoming packet mark");
516 return -EREMOTEIO;
517 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700518
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700519 return 0;
520}
521
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700522// A rule to route responses to the local network forwarded via the VPN.
523//
524// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
525// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
526WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
527 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
528 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
529 INVALID_UID, INVALID_UID);
530}
531
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700532// A rule to route all traffic from a given set of UIDs to go over the VPN.
533//
534// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
535// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
536// bypass the VPN if the protectedFromVpn bit is set.
537WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700538 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700539 Fwmark fwmark;
540 Fwmark mask;
541
542 fwmark.protectedFromVpn = false;
543 mask.protectedFromVpn = true;
544
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700545 uint32_t priority;
546
547 if (secure) {
548 priority = RULE_PRIORITY_SECURE_VPN;
549 } else {
550 priority = RULE_PRIORITY_BYPASSABLE_VPN;
551
552 fwmark.explicitlySelected = false;
553 mask.explicitlySelected = true;
554 }
555
556 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900557 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700558}
559
560// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
561// set of UIDs.
562//
563// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
564// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700565WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
566 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700567 Fwmark fwmark;
568 Fwmark mask;
569
570 fwmark.netId = netId;
571 mask.netId = FWMARK_NET_ID_MASK;
572
573 fwmark.permission = PERMISSION_SYSTEM;
574 mask.permission = PERMISSION_SYSTEM;
575
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700576 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
577
578 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
579 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700580}
581
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700582// A rule to route traffic based on an explicitly chosen network.
583//
584// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
585//
586// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
587// to check it again in the rules here, because a network's permissions may have been updated via
588// modifyNetworkPermission().
589WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
590 Permission permission, uid_t uidStart,
591 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700592 Fwmark fwmark;
593 Fwmark mask;
594
595 fwmark.netId = netId;
596 mask.netId = FWMARK_NET_ID_MASK;
597
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700598 fwmark.explicitlySelected = true;
599 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700600
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700601 fwmark.permission = permission;
602 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700603
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700604 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700605 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700606}
607
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700608// A rule to route traffic based on a chosen outgoing interface.
609//
610// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
611// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900612WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
613 Permission permission, uid_t uidStart,
614 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700615 Fwmark fwmark;
616 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700617
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700618 fwmark.permission = permission;
619 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700620
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900621 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
622 // for UID. This covers forwarded packets and system daemons such as the tethering DHCP server.
623 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
624 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
625 table, fwmark.intValue, mask.intValue, IIF_NONE, interface,
626 UID_ROOT, UID_ROOT)) {
627 return ret;
628 }
629 }
630
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700631 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700632 fwmark.intValue, mask.intValue, IIF_NONE, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700633}
634
635// A rule to route traffic based on the chosen network.
636//
637// This is for sockets that have not explicitly requested a particular network, but have been
638// bound to one when they called connect(). This ensures that sockets connected on a particular
639// network stay on that network even if the default network changes.
640WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table,
641 Permission permission, bool add) {
642 Fwmark fwmark;
643 Fwmark mask;
644
645 fwmark.netId = netId;
646 mask.netId = FWMARK_NET_ID_MASK;
647
648 fwmark.explicitlySelected = false;
649 mask.explicitlySelected = true;
650
651 fwmark.permission = permission;
652 mask.permission = permission;
653
654 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700655 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700656}
657
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700658// A rule to enable split tunnel VPNs.
659//
660// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
661// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
662// permissions required by the default network.
663WARN_UNUSED_RESULT int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
664 const char* physicalInterface,
665 Permission permission) {
666 uint32_t table = getRouteTableForInterface(physicalInterface);
667 if (table == RT_TABLE_UNSPEC) {
668 return -ESRCH;
669 }
670
671 Fwmark fwmark;
672 Fwmark mask;
673
674 fwmark.netId = vpnNetId;
675 mask.netId = FWMARK_NET_ID_MASK;
676
677 fwmark.explicitlySelected = false;
678 mask.explicitlySelected = true;
679
680 fwmark.permission = permission;
681 mask.permission = permission;
682
683 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
684 mask.intValue);
685}
686
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700687// Add rules to allow legacy routes added through the requestRouteToHost() API.
688WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700689 Fwmark fwmark;
690 Fwmark mask;
691
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700692 fwmark.explicitlySelected = false;
693 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700694
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700695 // Rules to allow legacy routes to override the default network.
696 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
697 fwmark.intValue, mask.intValue)) {
698 return ret;
699 }
700 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
701 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
702 return ret;
703 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700704
705 fwmark.permission = PERMISSION_SYSTEM;
706 mask.permission = PERMISSION_SYSTEM;
707
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700708 // A rule to allow legacy routes from system apps to override VPNs.
709 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700710 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700711}
712
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700713// Add rules to lookup the local network when specified explicitly or otherwise.
714WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
715 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
716 INVALID_UID, INVALID_UID, ACTION_ADD)) {
717 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700718 }
719
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700720 Fwmark fwmark;
721 Fwmark mask;
722
723 fwmark.explicitlySelected = false;
724 mask.explicitlySelected = true;
725
726 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
727 fwmark.intValue, mask.intValue);
728}
729
Lorenzo Colitti36679362015-02-25 10:26:19 +0900730int configureDummyNetwork() {
731 const char *interface = DummyNetwork::INTERFACE_NAME;
732 uint32_t table = getRouteTableForInterface(interface);
733 if (table == RT_TABLE_UNSPEC) {
734 // getRouteTableForInterface has already looged an error.
735 return -ESRCH;
736 }
737
738 ifc_init();
739 int ret = ifc_up(interface);
740 ifc_close();
741 if (ret) {
742 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
743 return -errno;
744 }
745
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900746 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
747 INVALID_UID, INVALID_UID, ACTION_ADD))) {
748 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900749 return ret;
750 }
751
752 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900753 return ret;
754 }
755
756 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900757 return ret;
758 }
759
760 return 0;
761}
762
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000763// 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 +0900764// rule, but with a lower priority. We will never create routes in the main table; it should only be
765// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
766// This is necessary, for example, when adding a route through a directly-connected gateway: in
767// order to add the route, there must already be a directly-connected route that covers the gateway.
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000768WARN_UNUSED_RESULT int addDirectlyConnectedRule() {
769 Fwmark fwmark;
770 Fwmark mask;
771
772 fwmark.netId = NETID_UNSET;
773 mask.netId = FWMARK_NET_ID_MASK;
774
775 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
776 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT);
777}
778
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900779// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
780// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
781// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
782// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700783WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100784 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
785 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700786}
787
788WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700789 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
790 return ret;
791 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900792 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
793 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700794}
795
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700796WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
797 Permission permission, bool add) {
798 uint32_t table = getRouteTableForInterface(interface);
799 if (table == RT_TABLE_UNSPEC) {
800 return -ESRCH;
801 }
802
803 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
804 return ret;
805 }
806 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
807 add)) {
808 return ret;
809 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900810 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700811 add)) {
812 return ret;
813 }
814 return modifyImplicitNetworkRule(netId, table, permission, add);
815}
816
Robin Leeb8087362016-03-30 18:43:08 +0100817WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
818 Fwmark fwmark;
819 Fwmark mask;
820 fwmark.protectedFromVpn = false;
821 mask.protectedFromVpn = true;
822
Robin Leedc0d5782016-07-20 14:17:11 +0100823 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100824 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
825 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
826 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100827 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100828 return ret;
829 }
830 }
831
832 return 0;
833}
834
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700835WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700836 const UidRanges& uidRanges, bool secure, bool add,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700837 bool modifyNonUidBasedRules) {
838 uint32_t table = getRouteTableForInterface(interface);
839 if (table == RT_TABLE_UNSPEC) {
840 return -ESRCH;
841 }
842
Robin Leedc0d5782016-07-20 14:17:11 +0100843 for (const UidRange& range : uidRanges.getRanges()) {
844 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
845 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700846 return ret;
847 }
Robin Leedc0d5782016-07-20 14:17:11 +0100848 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
849 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700850 return ret;
851 }
Robin Leedc0d5782016-07-20 14:17:11 +0100852 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
853 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700854 return ret;
855 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700856 }
857
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700858 if (modifyNonUidBasedRules) {
859 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700860 return ret;
861 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700862 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
863 return ret;
864 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700865 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700866 return ret;
867 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700868 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700869 }
870
871 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700872}
873
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700874WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
875 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700876 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700877 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900878 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700879 }
880
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700881 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700882 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700883
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700884 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700885 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700886
887 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700888 mask.permission = permission;
889
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700890 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700891 mask.intValue);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700892}
893
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700894WARN_UNUSED_RESULT int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
895 const char* outputInterface) {
896 uint32_t table = getRouteTableForInterface(outputInterface);
897 if (table == RT_TABLE_UNSPEC) {
898 return -ESRCH;
899 }
900
901 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
902 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
903}
904
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700905// Returns 0 on success or negative errno on failure.
906WARN_UNUSED_RESULT int flushRules() {
907 for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
908 const char* argv[] = {
909 IP_PATH,
910 IP_VERSIONS[i],
911 "rule",
912 "flush",
913 };
914 if (android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, false, false)) {
915 ALOGE("failed to flush rules");
916 return -EREMOTEIO;
917 }
918 }
919 return 0;
920}
921
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700922// 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 +0900923// route, to the main table as well.
924// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700925WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
926 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700927 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700928 switch (tableType) {
929 case RouteController::INTERFACE: {
930 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700931 if (table == RT_TABLE_UNSPEC) {
932 return -ESRCH;
933 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700934 break;
935 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700936 case RouteController::LOCAL_NETWORK: {
937 table = ROUTE_TABLE_LOCAL_NETWORK;
938 break;
939 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700940 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700941 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700942 break;
943 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700944 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700945 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700946 break;
947 }
948 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700949
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900950 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700951 // Trying to add a route that already exists shouldn't cause an error.
952 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900953 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700954 }
955
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900956 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700957}
958
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700959// Returns 0 on success or negative errno on failure.
960WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700961 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700962 if (table == RT_TABLE_UNSPEC) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700963 return -ESRCH;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700964 }
965
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900966 char tableString[UINT32_STRLEN];
967 snprintf(tableString, sizeof(tableString), "%u", table);
968
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900969 int ret = 0;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700970 for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900971 const char* argv[] = {
972 IP_PATH,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700973 IP_VERSIONS[i],
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700974 "route",
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900975 "flush",
976 "table",
977 tableString,
978 };
Lorenzo Colitti99286fe2014-08-12 15:08:00 +0900979
980 // A flush works by dumping routes and deleting each route as it's returned, and it can
981 // fail if something else deletes the route between the dump and the delete. This can
982 // happen, for example, if an interface goes down while we're trying to flush its routes.
983 // So try multiple times and only return an error if the last attempt fails.
984 //
985 // TODO: replace this with our own netlink code.
986 unsigned attempts = 0;
987 int err;
988 do {
989 err = android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv),
990 NULL, false, false);
991 ++attempts;
992 } while (err != 0 && attempts < ROUTE_FLUSH_ATTEMPTS);
993 if (err) {
994 ALOGE("failed to flush %s routes in table %s after %d attempts",
995 IP_VERSIONS[i], tableString, attempts);
996 ret = -EREMOTEIO;
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900997 }
998 }
999
Lorenzo Colitti99286fe2014-08-12 15:08:00 +09001000 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
1001 // track of its name.
1002 if (!ret) {
1003 interfaceToTable.erase(interface);
1004 }
1005
1006 return ret;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -07001007}
1008
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001009WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
1010 int ret = 0;
1011 while (ret == 0) {
1012 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
1013 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
1014 }
1015
1016 if (ret == -ENOENT) {
1017 return 0;
1018 } else {
1019 return ret;
1020 }
1021}
1022
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001023} // namespace
1024
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001025int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -07001026 if (int ret = flushRules()) {
1027 return ret;
1028 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001029 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001030 return ret;
1031 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001032 if (int ret = addLocalNetworkRules(localNetId)) {
1033 return ret;
1034 }
Lorenzo Colittia2c23052014-07-29 09:25:44 +00001035 if (int ret = addDirectlyConnectedRule()) {
1036 return ret;
1037 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -07001038 if (int ret = addUnreachableRule()) {
1039 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001040 }
Lorenzo Colitti36679362015-02-25 10:26:19 +09001041 // Don't complain if we can't add the dummy network, since not all devices support it.
1042 configureDummyNetwork();
1043
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001044 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001045 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -07001046}
1047
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001048int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001049 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001050}
1051
1052int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001053 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -07001054}
1055
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001056int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
1057 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001058 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
1059 return ret;
1060 }
1061 updateTableNamesFile();
1062 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001063}
1064
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001065int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
1066 Permission permission) {
1067 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001068 return ret;
1069 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001070 if (int ret = flushRoutes(interface)) {
1071 return ret;
1072 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +09001073 if (int ret = clearTetheringRules(interface)) {
1074 return ret;
1075 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001076 updateTableNamesFile();
1077 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001078}
1079
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001080int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001081 bool secure, const UidRanges& uidRanges) {
1082 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001083 MODIFY_NON_UID_BASED_RULES)) {
1084 return ret;
1085 }
1086 updateTableNamesFile();
1087 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001088}
1089
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001090int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001091 bool secure, const UidRanges& uidRanges) {
1092 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001093 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001094 return ret;
1095 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001096 if (int ret = flushRoutes(interface)) {
1097 return ret;
1098 }
1099 updateTableNamesFile();
1100 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001101}
1102
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001103int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1104 Permission oldPermission,
1105 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001106 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001107 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001108 return ret;
1109 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001110 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001111}
1112
Robin Leeb8087362016-03-30 18:43:08 +01001113int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1114 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1115}
1116
1117int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1118 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1119}
1120
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001121int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001122 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001123 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001124 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001125}
1126
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001127int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001128 bool secure, const UidRanges& uidRanges) {
1129 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001130 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001131}
1132
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001133int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1134 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001135}
1136
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001137int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1138 Permission permission) {
1139 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001140}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001141
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001142int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001143 TableType tableType) {
1144 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001145}
1146
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001147int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001148 const char* nexthop, TableType tableType) {
1149 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001150}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001151
1152int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001153 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001154}
1155
1156int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001157 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001158}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001159
1160int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1161 Permission permission) {
1162 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1163}
1164
1165int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1166 const char* physicalInterface,
1167 Permission permission) {
1168 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1169}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001170
1171} // namespace net
1172} // namespace android