blob: 8e3a4a934a75d59357f11780feb5e3a406b908de [file] [log] [blame]
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "RouteController.h"
18
Dan Albertaa1be2b2015-01-06 09:36:17 -080019#include <arpa/inet.h>
20#include <errno.h>
21#include <fcntl.h>
22#include <linux/fib_rules.h>
23#include <net/if.h>
24#include <sys/stat.h>
25
Elliott Hughesbd378322015-02-04 13:25:14 -080026#include <private/android_filesystem_config.h>
27
Dan Albertaa1be2b2015-01-06 09:36:17 -080028#include <map>
29
Lorenzo Colitti7035f222017-02-13 18:29:00 +090030#include "DummyNetwork.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070031#include "Fwmark.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090032#include "NetdConstants.h"
Lorenzo Colitti1ef549d2017-02-13 18:32:09 +090033#include "NetlinkCommands.h"
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070034#include "UidRanges.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070035
Elliott Hughesbbd56262015-12-04 15:45:10 -080036#include "android-base/file.h"
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090037#include <android-base/stringprintf.h>
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070038#define LOG_TAG "Netd"
39#include "log/log.h"
40#include "logwrap/logwrap.h"
Lorenzo Colitti36679362015-02-25 10:26:19 +090041#include "netutils/ifc.h"
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070042#include "resolv_netid.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070043
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090044using android::base::StringPrintf;
Dan Albert5407e142015-03-16 10:05:59 -070045using android::base::WriteStringToFile;
Robin Leedc0d5782016-07-20 14:17:11 +010046using android::net::UidRange;
Dan Albert5407e142015-03-16 10:05:59 -070047
Lorenzo Colitti7035f222017-02-13 18:29:00 +090048namespace android {
49namespace net {
50
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090051auto RouteController::iptablesRestoreCommandFunction = execIptablesRestoreCommand;
52
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070053// BEGIN CONSTANTS --------------------------------------------------------------------------------
54
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070055const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
Lorenzo Colitti57947f02015-02-27 16:45:55 +090056const uint32_t RULE_PRIORITY_VPN_OVERRIDE_OIF = 10500;
Sreeram Ramachandran111bec22014-07-22 18:51:06 -070057const uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070058const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
Robin Lee6c84ef62016-05-03 13:17:58 +010059const uint32_t RULE_PRIORITY_PROHIBIT_NON_VPN = 12500;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070060const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
61const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
62const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
63const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070064const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070065const uint32_t RULE_PRIORITY_TETHERING = 18000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070066const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -070067const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -070068const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070069const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
Lorenzo Colittia2c23052014-07-29 09:25:44 +000070const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 23000;
Lorenzo Colittidb74dba2014-07-29 18:26:21 +090071const uint32_t RULE_PRIORITY_UNREACHABLE = 32000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070072
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070073const uint32_t ROUTE_TABLE_LOCAL_NETWORK = 97;
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070074const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
75const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
76
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070077const char* const ROUTE_TABLE_NAME_LOCAL_NETWORK = "local_network";
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070078const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
79const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
80
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -070081const char* const ROUTE_TABLE_NAME_LOCAL = "local";
82const char* const ROUTE_TABLE_NAME_MAIN = "main";
83
Lorenzo Colittib5d19e92017-09-25 18:39:33 +090084// None of our regular routes specify priority, which causes them to have the default priority.
85// For default throw routes, we use a fixed priority of 100000.
Lorenzo Colitti5e03a892017-09-08 11:31:59 +090086uint32_t PRIO_THROW = 100000;
87
Lorenzo Colittid78843e2017-03-27 05:52:31 +090088const char* const RouteController::LOCAL_MANGLE_INPUT = "routectrl_mangle_INPUT";
89
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070090const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
91
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070092const uid_t UID_ROOT = 0;
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +090093const char* const IIF_LOOPBACK = "lo";
Sreeram Ramachandran87475a12014-07-15 16:20:28 -070094const char* const IIF_NONE = NULL;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070095const char* const OIF_NONE = NULL;
96const bool ACTION_ADD = true;
97const bool ACTION_DEL = false;
98const bool MODIFY_NON_UID_BASED_RULES = true;
99
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700100const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700101const mode_t RT_TABLES_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // mode 0644, rw-r--r--
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700102
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700103// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
104// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
105constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
106 return RTA_LENGTH(x);
107}
108
109// These are practically const, but can't be declared so, because they are used to initialize
110// non-const pointers ("void* iov_base") in iovec arrays.
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900111rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
112rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
113rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
114rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900115rtattr FRATTR_UID_RANGE = { U16_RTA_LENGTH(sizeof(fib_rule_uid_range)), FRA_UID_RANGE };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700116
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900117rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
118rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900119rtattr RTATTR_PRIO = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_PRIORITY };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700120
121uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
122
123// END CONSTANTS ----------------------------------------------------------------------------------
124
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900125const char *actionName(uint16_t action) {
126 static const char *ops[4] = {"adding", "deleting", "getting", "???"};
127 return ops[action % 4];
128}
129
130const char *familyName(uint8_t family) {
131 switch (family) {
132 case AF_INET: return "IPv4";
133 case AF_INET6: return "IPv6";
134 default: return "???";
135 }
136}
137
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700138// No locks needed because RouteController is accessed only from one thread (in CommandListener).
139std::map<std::string, uint32_t> interfaceToTable;
Paul Jensena561e122014-06-12 16:46:37 -0400140
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700141uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700142 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400143 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700144 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
145 interfaceToTable[interface] = index;
146 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400147 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700148 // If the interface goes away if_nametoindex() will return 0 but we still need to know
149 // the index so we can remove the rules and routes.
150 auto iter = interfaceToTable.find(interface);
151 if (iter == interfaceToTable.end()) {
152 ALOGE("cannot find interface %s", interface);
153 return RT_TABLE_UNSPEC;
154 }
155 return iter->second;
156}
157
158void addTableName(uint32_t table, const std::string& name, std::string* contents) {
159 char tableString[UINT32_STRLEN];
160 snprintf(tableString, sizeof(tableString), "%u", table);
161 *contents += tableString;
162 *contents += " ";
163 *contents += name;
164 *contents += "\n";
165}
166
167// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
168void updateTableNamesFile() {
169 std::string contents;
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700170
171 addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
172 addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents);
173
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700174 addTableName(ROUTE_TABLE_LOCAL_NETWORK, ROUTE_TABLE_NAME_LOCAL_NETWORK, &contents);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700175 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
176 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
Sreeram Ramachandranbb40d512014-07-11 11:45:14 -0700177
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700178 for (const auto& entry : interfaceToTable) {
179 addTableName(entry.second, entry.first, &contents);
180 }
181
Dan Albert5407e142015-03-16 10:05:59 -0700182 if (!WriteStringToFile(contents, RT_TABLES_PATH, RT_TABLES_MODE, AID_SYSTEM, AID_WIFI)) {
Elliott Hughesbd378322015-02-04 13:25:14 -0800183 ALOGE("failed to write to %s (%s)", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700184 return;
185 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700186}
187
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700188// Returns 0 on success or negative errno on failure.
189int padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
190 if (!input) {
191 *length = 0;
192 *padding = 0;
193 return 0;
194 }
195 *length = strlcpy(name, input, IFNAMSIZ) + 1;
196 if (*length > IFNAMSIZ) {
197 ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
198 return -ENAMETOOLONG;
199 }
200 *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
201 return 0;
202}
203
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700204// Adds or removes a routing rule for IPv4 and IPv6.
205//
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900206// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the table is
Robin Lee4ef94642016-04-01 11:50:49 +0100207// unspecified. An unspecified table is not allowed when creating an FR_ACT_TO_TBL rule.
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700208// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
209// ignored.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700210// + If |iif| is non-NULL, the rule matches the specified incoming interface.
211// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
212// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
213// range (inclusive). Otherwise, the rule matches packets from all UIDs.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900214//
215// Returns 0 on success or negative errno on failure.
Robin Lee4ef94642016-04-01 11:50:49 +0100216WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint8_t ruleType,
217 uint32_t table, uint32_t fwmark, uint32_t mask, const char* iif,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700218 const char* oif, uid_t uidStart, uid_t uidEnd) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700219 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
220 if (fwmark & ~mask) {
221 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
222 return -ERANGE;
223 }
224
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700225 // Interface names must include exactly one terminating NULL and be properly padded, or older
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900226 // kernels will refuse to delete rules.
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700227 char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
228 size_t iifLength, oifLength;
229 uint16_t iifPadding, oifPadding;
230 if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
231 return ret;
232 }
233 if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
234 return ret;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900235 }
236
Lorenzo Colitti59656512014-06-25 03:20:29 +0900237 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700238 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700239 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900240 return -EUSERS;
241 }
Robin Lee4ef94642016-04-01 11:50:49 +0100242
Lorenzo Colitti72723682014-06-26 13:51:10 +0900243 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900244
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900245 // Assemble a rule request and put it in an array of iovec structures.
246 fib_rule_hdr rule = {
Robin Lee4ef94642016-04-01 11:50:49 +0100247 .action = ruleType,
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900248 // Note that here we're implicitly setting rule.table to 0. When we want to specify a
249 // non-zero table, we do this via the FRATTR_TABLE attribute.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900250 };
251
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900252 // Don't ever create a rule that looks up table 0, because table 0 is the local table.
253 // It's OK to specify a table ID of 0 when deleting a rule, because that doesn't actually select
254 // table 0, it's a wildcard that matches anything.
255 if (table == RT_TABLE_UNSPEC && rule.action == FR_ACT_TO_TBL && action != RTM_DELRULE) {
256 ALOGE("RT_TABLE_UNSPEC only allowed when deleting rules");
257 return -ENOTUNIQ;
258 }
259
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700260 rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
261 rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900262 struct fib_rule_uid_range uidRange = { uidStart, uidEnd };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900263
264 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700265 { NULL, 0 },
266 { &rule, sizeof(rule) },
267 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
268 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700269 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
270 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700271 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
272 { &fwmark, mask ? sizeof(fwmark) : 0 },
273 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
274 { &mask, mask ? sizeof(mask) : 0 },
Lorenzo Colitti2b078672016-12-16 18:45:03 +0900275 { &FRATTR_UID_RANGE, isUidRule ? sizeof(FRATTR_UID_RANGE) : 0 },
276 { &uidRange, isUidRule ? sizeof(uidRange) : 0 },
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700277 { &fraIifName, iif != IIF_NONE ? sizeof(fraIifName) : 0 },
278 { iifName, iifLength },
279 { PADDING_BUFFER, iifPadding },
280 { &fraOifName, oif != OIF_NONE ? sizeof(fraOifName) : 0 },
281 { oifName, oifLength },
282 { PADDING_BUFFER, oifPadding },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900283 };
284
Lorenzo Colitti72723682014-06-26 13:51:10 +0900285 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700286 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
287 rule.family = AF_FAMILIES[i];
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900288 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr)) {
Lorenzo Colitti220ca732017-02-14 17:57:55 +0900289 if (!(action == RTM_DELRULE && ret == -ENOENT && priority == RULE_PRIORITY_TETHERING)) {
290 // Don't log when deleting a tethering rule that's not there. This matches the
291 // behaviour of clearTetheringRules, which ignores ENOENT in this case.
292 ALOGE("Error %s %s rule: %s", actionName(action), familyName(rule.family),
293 strerror(-ret));
294 }
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900295 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700296 }
297 }
298
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900299 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700300}
301
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700302WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Robin Lee4ef94642016-04-01 11:50:49 +0100303 uint32_t fwmark, uint32_t mask, const char* iif,
304 const char* oif, uid_t uidStart, uid_t uidEnd) {
305 return modifyIpRule(action, priority, FR_ACT_TO_TBL, table, fwmark, mask, iif, oif, uidStart,
306 uidEnd);
307}
308
309WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700310 uint32_t fwmark, uint32_t mask) {
311 return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
312 INVALID_UID);
313}
314
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900315// Adds or deletes an IPv4 or IPv6 route.
316// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700317WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
318 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900319 // At least the destination must be non-null.
320 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700321 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900322 return -EFAULT;
323 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700324
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900325 // Parse the prefix.
326 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700327 uint8_t family;
328 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900329 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
330 &prefixLength);
331 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700332 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900333 return rawLength;
334 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700335
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900336 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700337 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900338 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
339 }
340
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700341 uint8_t type = RTN_UNICAST;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900342 uint32_t ifindex;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900343 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700344
345 if (nexthop && !strcmp(nexthop, "unreachable")) {
346 type = RTN_UNREACHABLE;
347 // 'interface' is likely non-NULL, as the caller (modifyRoute()) likely used it to lookup
348 // the table number. But it's an error to specify an interface ("dev ...") or a nexthop for
349 // unreachable routes, so nuke them. (IPv6 allows them to be specified; IPv4 doesn't.)
350 interface = OIF_NONE;
351 nexthop = NULL;
Lorenzo Colitti4c95a122014-09-18 16:01:50 +0900352 } else if (nexthop && !strcmp(nexthop, "throw")) {
353 type = RTN_THROW;
354 interface = OIF_NONE;
355 nexthop = NULL;
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700356 } else {
357 // If an interface was specified, find the ifindex.
358 if (interface != OIF_NONE) {
359 ifindex = if_nametoindex(interface);
360 if (!ifindex) {
361 ALOGE("cannot find interface %s", interface);
362 return -ENODEV;
363 }
364 }
365
366 // If a nexthop was specified, parse it as the same family as the prefix.
367 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
368 ALOGE("inet_pton failed for nexthop %s", nexthop);
369 return -EINVAL;
370 }
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900371 }
372
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900373 bool isDefaultThrowRoute = (type == RTN_THROW && prefixLength == 0);
374
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900375 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700376 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900377 .rtm_protocol = RTPROT_STATIC,
Sreeram Ramachandrande5d5df2014-07-26 18:43:25 -0700378 .rtm_type = type,
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900379 .rtm_family = family,
380 .rtm_dst_len = prefixLength,
Sreeram Ramachandran2bff72e2014-07-18 13:03:47 -0700381 .rtm_scope = static_cast<uint8_t>(nexthop ? RT_SCOPE_UNIVERSE : RT_SCOPE_LINK),
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900382 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900383
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700384 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
385 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900386
387 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700388 { NULL, 0 },
389 { &route, sizeof(route) },
390 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
391 { &table, sizeof(table) },
392 { &rtaDst, sizeof(rtaDst) },
393 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700394 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
395 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700396 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
397 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colitti5e03a892017-09-08 11:31:59 +0900398 { &RTATTR_PRIO, isDefaultThrowRoute ? sizeof(RTATTR_PRIO) : 0 },
399 { &PRIO_THROW, isDefaultThrowRoute ? sizeof(PRIO_THROW) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900400 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900401
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700402 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
403 NETLINK_REQUEST_FLAGS;
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900404 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov), nullptr);
Lorenzo Colitti0b073fb2017-02-10 07:49:12 +0900405 if (ret) {
406 ALOGE("Error %s route %s -> %s %s to table %u: %s",
407 actionName(action), destination, nexthop, interface, table, strerror(-ret));
408 }
409 return ret;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700410}
411
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700412// An iptables rule to mark incoming packets on a network with the netId of the network.
413//
414// This is so that the kernel can:
415// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
416// replies, SYN-ACKs, etc).
417// + Mark sockets that accept connections from this interface so that the connection stays on the
418// same interface.
419WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
420 Permission permission, bool add) {
421 Fwmark fwmark;
422
423 fwmark.netId = netId;
424 fwmark.explicitlySelected = true;
425 fwmark.protectedFromVpn = true;
426 fwmark.permission = permission;
427
Lorenzo Colittid78843e2017-03-27 05:52:31 +0900428 std::string cmd = StringPrintf("%s %s -i %s -j MARK --set-mark 0x%x",
429 add ? "-A" : "-D", RouteController::LOCAL_MANGLE_INPUT,
430 interface, fwmark.intValue);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +0900431 if (RouteController::iptablesRestoreCommandFunction(V4V6, "mangle", cmd, nullptr) != 0) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700432 ALOGE("failed to change iptables rule that sets incoming packet mark");
433 return -EREMOTEIO;
434 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700435
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700436 return 0;
437}
438
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700439// A rule to route responses to the local network forwarded via the VPN.
440//
441// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
442// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
443WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
444 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
445 ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
446 INVALID_UID, INVALID_UID);
447}
448
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700449// A rule to route all traffic from a given set of UIDs to go over the VPN.
450//
451// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
452// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
453// bypass the VPN if the protectedFromVpn bit is set.
454WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700455 bool secure, bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700456 Fwmark fwmark;
457 Fwmark mask;
458
459 fwmark.protectedFromVpn = false;
460 mask.protectedFromVpn = true;
461
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700462 uint32_t priority;
463
464 if (secure) {
465 priority = RULE_PRIORITY_SECURE_VPN;
466 } else {
467 priority = RULE_PRIORITY_BYPASSABLE_VPN;
468
469 fwmark.explicitlySelected = false;
470 mask.explicitlySelected = true;
471 }
472
473 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
Lorenzo Colitti5ad4e982015-02-26 17:34:32 +0900474 mask.intValue, IIF_LOOPBACK, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700475}
476
477// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
478// set of UIDs.
479//
480// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
481// target set, but where the DnsProxyListener itself is not.
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700482WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
483 bool add) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700484 Fwmark fwmark;
485 Fwmark mask;
486
487 fwmark.netId = netId;
488 mask.netId = FWMARK_NET_ID_MASK;
489
490 fwmark.permission = PERMISSION_SYSTEM;
491 mask.permission = PERMISSION_SYSTEM;
492
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700493 uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
494
495 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
496 mask.intValue);
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700497}
498
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700499// A rule to route traffic based on an explicitly chosen network.
500//
501// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
502//
503// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
504// to check it again in the rules here, because a network's permissions may have been updated via
505// modifyNetworkPermission().
506WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
507 Permission permission, uid_t uidStart,
508 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700509 Fwmark fwmark;
510 Fwmark mask;
511
512 fwmark.netId = netId;
513 mask.netId = FWMARK_NET_ID_MASK;
514
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700515 fwmark.explicitlySelected = true;
516 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700517
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700518 fwmark.permission = permission;
519 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700520
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700521 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700522 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700523}
524
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700525// A rule to route traffic based on a chosen outgoing interface.
526//
527// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
528// the outgoing interface (typically for link-local communications).
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900529WARN_UNUSED_RESULT int modifyOutputInterfaceRules(const char* interface, uint32_t table,
530 Permission permission, uid_t uidStart,
531 uid_t uidEnd, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700532 Fwmark fwmark;
533 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700534
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700535 fwmark.permission = permission;
536 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700537
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900538 // If this rule does not specify a UID range, then also add a corresponding high-priority rule
539 // for UID. This covers forwarded packets and system daemons such as the tethering DHCP server.
540 if (uidStart == INVALID_UID && uidEnd == INVALID_UID) {
541 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OVERRIDE_OIF,
542 table, fwmark.intValue, mask.intValue, IIF_NONE, interface,
543 UID_ROOT, UID_ROOT)) {
544 return ret;
545 }
546 }
547
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700548 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700549 fwmark.intValue, mask.intValue, IIF_NONE, interface, uidStart, uidEnd);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700550}
551
552// A rule to route traffic based on the chosen network.
553//
554// This is for sockets that have not explicitly requested a particular network, but have been
555// bound to one when they called connect(). This ensures that sockets connected on a particular
556// network stay on that network even if the default network changes.
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900557WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, bool add) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700558 Fwmark fwmark;
559 Fwmark mask;
560
561 fwmark.netId = netId;
562 mask.netId = FWMARK_NET_ID_MASK;
563
564 fwmark.explicitlySelected = false;
565 mask.explicitlySelected = true;
566
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900567 fwmark.permission = PERMISSION_NONE;
568 mask.permission = PERMISSION_NONE;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700569
570 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700571 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700572}
573
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -0700574// A rule to enable split tunnel VPNs.
575//
576// If a packet with a VPN's netId doesn't find a route in the VPN's routing table, it's allowed to
577// go over the default network, provided it wasn't explicitly restricted to the VPN and has the
578// permissions required by the default network.
579WARN_UNUSED_RESULT int modifyVpnFallthroughRule(uint16_t action, unsigned vpnNetId,
580 const char* physicalInterface,
581 Permission permission) {
582 uint32_t table = getRouteTableForInterface(physicalInterface);
583 if (table == RT_TABLE_UNSPEC) {
584 return -ESRCH;
585 }
586
587 Fwmark fwmark;
588 Fwmark mask;
589
590 fwmark.netId = vpnNetId;
591 mask.netId = FWMARK_NET_ID_MASK;
592
593 fwmark.explicitlySelected = false;
594 mask.explicitlySelected = true;
595
596 fwmark.permission = permission;
597 mask.permission = permission;
598
599 return modifyIpRule(action, RULE_PRIORITY_VPN_FALLTHROUGH, table, fwmark.intValue,
600 mask.intValue);
601}
602
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700603// Add rules to allow legacy routes added through the requestRouteToHost() API.
604WARN_UNUSED_RESULT int addLegacyRouteRules() {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700605 Fwmark fwmark;
606 Fwmark mask;
607
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700608 fwmark.explicitlySelected = false;
609 mask.explicitlySelected = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700610
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700611 // Rules to allow legacy routes to override the default network.
612 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
613 fwmark.intValue, mask.intValue)) {
614 return ret;
615 }
616 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
617 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
618 return ret;
619 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700620
621 fwmark.permission = PERMISSION_SYSTEM;
622 mask.permission = PERMISSION_SYSTEM;
623
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700624 // A rule to allow legacy routes from system apps to override VPNs.
625 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700626 fwmark.intValue, mask.intValue);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700627}
628
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700629// Add rules to lookup the local network when specified explicitly or otherwise.
630WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
631 if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
632 INVALID_UID, INVALID_UID, ACTION_ADD)) {
633 return ret;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700634 }
635
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700636 Fwmark fwmark;
637 Fwmark mask;
638
639 fwmark.explicitlySelected = false;
640 mask.explicitlySelected = true;
641
642 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
643 fwmark.intValue, mask.intValue);
644}
645
Lorenzo Colitti36679362015-02-25 10:26:19 +0900646int configureDummyNetwork() {
647 const char *interface = DummyNetwork::INTERFACE_NAME;
648 uint32_t table = getRouteTableForInterface(interface);
649 if (table == RT_TABLE_UNSPEC) {
650 // getRouteTableForInterface has already looged an error.
651 return -ESRCH;
652 }
653
654 ifc_init();
655 int ret = ifc_up(interface);
656 ifc_close();
657 if (ret) {
658 ALOGE("Can't bring up %s: %s", interface, strerror(errno));
659 return -errno;
660 }
661
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900662 if ((ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
663 INVALID_UID, INVALID_UID, ACTION_ADD))) {
664 ALOGE("Can't create oif rules for %s: %s", interface, strerror(-ret));
Lorenzo Colitti36679362015-02-25 10:26:19 +0900665 return ret;
666 }
667
668 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "0.0.0.0/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900669 return ret;
670 }
671
672 if ((ret = modifyIpRoute(RTM_NEWROUTE, table, interface, "::/0", NULL))) {
Lorenzo Colitti36679362015-02-25 10:26:19 +0900673 return ret;
674 }
675
676 return 0;
677}
678
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000679// 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 +0900680// rule, but with a lower priority. We will never create routes in the main table; it should only be
681// used for directly-connected routes implicitly created by the kernel when adding IP addresses.
682// This is necessary, for example, when adding a route through a directly-connected gateway: in
683// order to add the route, there must already be a directly-connected route that covers the gateway.
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000684WARN_UNUSED_RESULT int addDirectlyConnectedRule() {
685 Fwmark fwmark;
686 Fwmark mask;
687
688 fwmark.netId = NETID_UNSET;
689 mask.netId = FWMARK_NET_ID_MASK;
690
691 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
692 fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT);
693}
694
Lorenzo Colittidb74dba2014-07-29 18:26:21 +0900695// Add an explicit unreachable rule close to the end of the prioriy list to make it clear that
696// relying on the kernel-default "from all lookup main" rule at priority 32766 is not intended
697// behaviour. We do flush the kernel-default rules at startup, but having an explicit unreachable
698// rule will hopefully make things even clearer.
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700699WARN_UNUSED_RESULT int addUnreachableRule() {
Robin Leec125fe42016-05-02 08:53:34 +0100700 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, FR_ACT_UNREACHABLE, RT_TABLE_UNSPEC,
701 MARK_UNSET, MARK_UNSET, IIF_NONE, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700702}
703
704WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700705 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
706 return ret;
707 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900708 return modifyOutputInterfaceRules(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
709 INVALID_UID, INVALID_UID, add);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700710}
711
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700712WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
713 Permission permission, bool add) {
714 uint32_t table = getRouteTableForInterface(interface);
715 if (table == RT_TABLE_UNSPEC) {
716 return -ESRCH;
717 }
718
719 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
720 return ret;
721 }
722 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
723 add)) {
724 return ret;
725 }
Lorenzo Colitti57947f02015-02-27 16:45:55 +0900726 if (int ret = modifyOutputInterfaceRules(interface, table, permission, INVALID_UID, INVALID_UID,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700727 add)) {
728 return ret;
729 }
Lorenzo Colittibe0c7c32017-09-06 16:07:02 +0900730
731 // Only set implicit rules for networks that don't require permissions.
732 //
733 // This is so that if the default network ceases to be the default network and then switches
734 // from requiring no permissions to requiring permissions, we ensure that apps only use the
735 // network if they explicitly select it. This is consistent with destroySocketsLackingPermission
736 // - it closes all sockets on the network except sockets that are explicitly selected.
737 //
738 // The lack of this rule only affects the special case above, because:
739 // - The only cases where we implicitly bind a socket to a network are the default network and
740 // the bypassable VPN that applies to the app, if any.
741 // - This rule doesn't affect VPNs because they don't support permissions at all.
742 // - The default network doesn't require permissions. While we support doing this, the framework
743 // never does it (partly because we'd end up in the situation where we tell apps that there is
744 // a default network, but they can't use it).
745 // - If the network is still the default network, the presence or absence of this rule does not
746 // matter.
747 //
748 // Therefore, for the lack of this rule to affect a socket, the socket has to have been
749 // implicitly bound to a network because at the time of connect() it was the default, and that
750 // network must no longer be the default, and must now require permissions.
751 if (permission == PERMISSION_NONE) {
752 return modifyImplicitNetworkRule(netId, table, add);
753 }
754 return 0;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700755}
756
Robin Leeb8087362016-03-30 18:43:08 +0100757WARN_UNUSED_RESULT int modifyRejectNonSecureNetworkRule(const UidRanges& uidRanges, bool add) {
758 Fwmark fwmark;
759 Fwmark mask;
760 fwmark.protectedFromVpn = false;
761 mask.protectedFromVpn = true;
762
Robin Leedc0d5782016-07-20 14:17:11 +0100763 for (const UidRange& range : uidRanges.getRanges()) {
Robin Leeb8087362016-03-30 18:43:08 +0100764 if (int ret = modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE,
765 RULE_PRIORITY_PROHIBIT_NON_VPN, FR_ACT_PROHIBIT, RT_TABLE_UNSPEC,
766 fwmark.intValue, mask.intValue, IIF_LOOPBACK, OIF_NONE,
Robin Leedc0d5782016-07-20 14:17:11 +0100767 range.getStart(), range.getStop())) {
Robin Leeb8087362016-03-30 18:43:08 +0100768 return ret;
769 }
770 }
771
772 return 0;
773}
774
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700775WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700776 const UidRanges& uidRanges, bool secure, bool add,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700777 bool modifyNonUidBasedRules) {
778 uint32_t table = getRouteTableForInterface(interface);
779 if (table == RT_TABLE_UNSPEC) {
780 return -ESRCH;
781 }
782
Robin Leedc0d5782016-07-20 14:17:11 +0100783 for (const UidRange& range : uidRanges.getRanges()) {
784 if (int ret = modifyVpnUidRangeRule(table, range.getStart(), range.getStop(), secure, add))
785 {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700786 return ret;
787 }
Robin Leedc0d5782016-07-20 14:17:11 +0100788 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.getStart(),
789 range.getStop(), add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700790 return ret;
791 }
Robin Leedc0d5782016-07-20 14:17:11 +0100792 if (int ret = modifyOutputInterfaceRules(interface, table, PERMISSION_NONE,
793 range.getStart(), range.getStop(), add)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700794 return ret;
795 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700796 }
797
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700798 if (modifyNonUidBasedRules) {
799 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700800 return ret;
801 }
Sreeram Ramachandran111bec22014-07-22 18:51:06 -0700802 if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
803 return ret;
804 }
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700805 if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700806 return ret;
807 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700808 return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700809 }
810
811 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700812}
813
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700814WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
815 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700816 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700817 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900818 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700819 }
820
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700821 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700822 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700823
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700824 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700825 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700826
827 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700828 mask.permission = permission;
829
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700830 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700831 mask.intValue);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700832}
833
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700834WARN_UNUSED_RESULT int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
835 const char* outputInterface) {
836 uint32_t table = getRouteTableForInterface(outputInterface);
837 if (table == RT_TABLE_UNSPEC) {
838 return -ESRCH;
839 }
840
841 return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
842 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
843}
844
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700845// 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 +0900846// route, to the main table as well.
847// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700848WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
849 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700850 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700851 switch (tableType) {
852 case RouteController::INTERFACE: {
853 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700854 if (table == RT_TABLE_UNSPEC) {
855 return -ESRCH;
856 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700857 break;
858 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700859 case RouteController::LOCAL_NETWORK: {
860 table = ROUTE_TABLE_LOCAL_NETWORK;
861 break;
862 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700863 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700864 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700865 break;
866 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700867 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700868 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700869 break;
870 }
871 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700872
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900873 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran03213152014-10-30 10:01:07 -0700874 // Trying to add a route that already exists shouldn't cause an error.
875 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900876 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700877 }
878
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900879 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700880}
881
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900882WARN_UNUSED_RESULT int clearTetheringRules(const char* inputInterface) {
883 int ret = 0;
884 while (ret == 0) {
885 ret = modifyIpRule(RTM_DELRULE, RULE_PRIORITY_TETHERING, 0, MARK_UNSET, MARK_UNSET,
886 inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
887 }
888
889 if (ret == -ENOENT) {
890 return 0;
891 } else {
892 return ret;
893 }
894}
895
Lorenzo Colittif3e299a2017-02-14 17:24:28 +0900896uint32_t getRulePriority(const nlmsghdr *nlh) {
897 return getRtmU32Attribute(nlh, FRA_PRIORITY);
898}
899
900uint32_t getRouteTable(const nlmsghdr *nlh) {
901 return getRtmU32Attribute(nlh, RTA_TABLE);
902}
903
904WARN_UNUSED_RESULT int flushRules() {
905 NetlinkDumpFilter shouldDelete = [] (nlmsghdr *nlh) {
906 // Don't touch rules at priority 0 because by default they are used for local input.
907 return getRulePriority(nlh) != 0;
908 };
909 return rtNetlinkFlush(RTM_GETRULE, RTM_DELRULE, "rules", shouldDelete);
910}
911
912WARN_UNUSED_RESULT int flushRoutes(uint32_t table) {
913 NetlinkDumpFilter shouldDelete = [table] (nlmsghdr *nlh) {
914 return getRouteTable(nlh) == table;
915 };
916
917 return rtNetlinkFlush(RTM_GETROUTE, RTM_DELROUTE, "routes", shouldDelete);
918}
919
920// Returns 0 on success or negative errno on failure.
921WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
922 uint32_t table = getRouteTableForInterface(interface);
923 if (table == RT_TABLE_UNSPEC) {
924 return -ESRCH;
925 }
926
927 int ret = flushRoutes(table);
928
929 // If we failed to flush routes, the caller may elect to keep this interface around, so keep
930 // track of its name.
931 if (ret == 0) {
932 interfaceToTable.erase(interface);
933 }
934
935 return ret;
936}
937
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700938int RouteController::Init(unsigned localNetId) {
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700939 if (int ret = flushRules()) {
940 return ret;
941 }
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700942 if (int ret = addLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700943 return ret;
944 }
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -0700945 if (int ret = addLocalNetworkRules(localNetId)) {
946 return ret;
947 }
Lorenzo Colittia2c23052014-07-29 09:25:44 +0000948 if (int ret = addDirectlyConnectedRule()) {
949 return ret;
950 }
Sreeram Ramachandranb717e742014-07-19 00:22:15 -0700951 if (int ret = addUnreachableRule()) {
952 return ret;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700953 }
Lorenzo Colitti36679362015-02-25 10:26:19 +0900954 // Don't complain if we can't add the dummy network, since not all devices support it.
955 configureDummyNetwork();
956
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700957 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700958 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700959}
960
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700961int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700962 return modifyLocalNetwork(netId, interface, ACTION_ADD);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700963}
964
965int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
Sreeram Ramachandran87475a12014-07-15 16:20:28 -0700966 return modifyLocalNetwork(netId, interface, ACTION_DEL);
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700967}
968
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700969int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
970 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700971 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
972 return ret;
973 }
974 updateTableNamesFile();
975 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700976}
977
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700978int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
979 Permission permission) {
980 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700981 return ret;
982 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700983 if (int ret = flushRoutes(interface)) {
984 return ret;
985 }
Lorenzo Colitti6b6f25f2015-03-03 17:22:57 +0900986 if (int ret = clearTetheringRules(interface)) {
987 return ret;
988 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700989 updateTableNamesFile();
990 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700991}
992
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700993int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -0700994 bool secure, const UidRanges& uidRanges) {
995 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700996 MODIFY_NON_UID_BASED_RULES)) {
997 return ret;
998 }
999 updateTableNamesFile();
1000 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001001}
1002
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001003int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001004 bool secure, const UidRanges& uidRanges) {
1005 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001006 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001007 return ret;
1008 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -07001009 if (int ret = flushRoutes(interface)) {
1010 return ret;
1011 }
1012 updateTableNamesFile();
1013 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -07001014}
1015
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001016int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
1017 Permission oldPermission,
1018 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -07001019 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001020 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001021 return ret;
1022 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001023 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001024}
1025
Robin Leeb8087362016-03-30 18:43:08 +01001026int RouteController::addUsersToRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1027 return modifyRejectNonSecureNetworkRule(uidRanges, true);
1028}
1029
1030int RouteController::removeUsersFromRejectNonSecureNetworkRule(const UidRanges& uidRanges) {
1031 return modifyRejectNonSecureNetworkRule(uidRanges, false);
1032}
1033
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001034int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001035 const UidRanges& uidRanges) {
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001036 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001037 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001038}
1039
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001040int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
Sreeram Ramachandran95684ba2014-07-23 13:27:31 -07001041 bool secure, const UidRanges& uidRanges) {
1042 return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001043 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001044}
1045
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001046int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
1047 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -07001048}
1049
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -07001050int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
1051 Permission permission) {
1052 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -07001053}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001054
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -07001055int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001056 TableType tableType) {
1057 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001058}
1059
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +09001060int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -07001061 const char* nexthop, TableType tableType) {
1062 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -07001063}
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001064
1065int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001066 return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001067}
1068
1069int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
Sreeram Ramachandranfa9f4dc2014-07-22 18:16:44 -07001070 return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
Sreeram Ramachandran87475a12014-07-15 16:20:28 -07001071}
Sreeram Ramachandran48e19b02014-07-22 22:23:20 -07001072
1073int RouteController::addVirtualNetworkFallthrough(unsigned vpnNetId, const char* physicalInterface,
1074 Permission permission) {
1075 return modifyVpnFallthroughRule(RTM_NEWRULE, vpnNetId, physicalInterface, permission);
1076}
1077
1078int RouteController::removeVirtualNetworkFallthrough(unsigned vpnNetId,
1079 const char* physicalInterface,
1080 Permission permission) {
1081 return modifyVpnFallthroughRule(RTM_DELRULE, vpnNetId, physicalInterface, permission);
1082}
Lorenzo Colitti7035f222017-02-13 18:29:00 +09001083
1084} // namespace net
1085} // namespace android