blob: 8efbda41e30029b71d24dde6c08b7e4a92dab2b0 [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
19#include "Fwmark.h"
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070020#include "UidRanges.h"
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070021
22#define LOG_TAG "Netd"
23#include "log/log.h"
24#include "logwrap/logwrap.h"
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070025#include "resolv_netid.h"
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070026
Lorenzo Colittiba25df92014-06-18 00:22:17 +090027#include <arpa/inet.h>
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070028#include <fcntl.h>
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090029#include <linux/fib_rules.h>
Paul Jensena561e122014-06-12 16:46:37 -040030#include <map>
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070031#include <net/if.h>
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070032#include <sys/stat.h>
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070033
34namespace {
35
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070036// BEGIN CONSTANTS --------------------------------------------------------------------------------
37
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070038const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
39// const uint32_t RULE_PRIORITY_VPN_OVERRIDE_LOCAL = 11000;
40const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
41const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
42const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
43const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
44const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
Sreeram Ramachandran6a773532014-07-11 09:10:20 -070045const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070046// const uint32_t RULE_PRIORITY_TETHERING = 18000;
47const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
48// const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
49// const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
50const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
51const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 23000;
52const uint32_t RULE_PRIORITY_UNREACHABLE = 24000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070053
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070054const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
55const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99;
56
57const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
58const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system";
59
Lorenzo Colitti59656512014-06-25 03:20:29 +090060// TODO: These values aren't defined by the Linux kernel, because our UID routing changes are not
61// upstream (yet?), so we can't just pick them up from kernel headers. When (if?) the changes make
62// it upstream, we'll remove this and rely on the kernel header values. For now, add a static assert
63// that will warn us if upstream has given these values some other meaning.
64const uint16_t FRA_UID_START = 18;
65const uint16_t FRA_UID_END = 19;
66static_assert(FRA_UID_START > FRA_MAX,
67 "Android-specific FRA_UID_{START,END} values also assigned in Linux uapi. "
68 "Check that these values match what the kernel does and then update this assertion.");
69
Lorenzo Colitti72723682014-06-26 13:51:10 +090070const uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK;
71const uint16_t NETLINK_CREATE_REQUEST_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE | NLM_F_EXCL;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090072
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070073const sockaddr_nl NETLINK_ADDRESS = {AF_NETLINK, 0, 0, 0};
74
75const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
76
77const char* const IP_VERSIONS[] = {"-4", "-6"};
78
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070079const uid_t UID_ROOT = 0;
80const char* const OIF_NONE = NULL;
81const bool ACTION_ADD = true;
82const bool ACTION_DEL = false;
83const bool MODIFY_NON_UID_BASED_RULES = true;
84
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -070085const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -070086const int RT_TABLES_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
87const 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 -070088
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070089// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
90// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
91constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
92 return RTA_LENGTH(x);
93}
94
95// These are practically const, but can't be declared so, because they are used to initialize
96// non-const pointers ("void* iov_base") in iovec arrays.
97rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
98rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
99rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
100rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
101rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START };
102rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END };
103
104rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
105rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
106
107uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
108
109// END CONSTANTS ----------------------------------------------------------------------------------
110
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700111// No locks needed because RouteController is accessed only from one thread (in CommandListener).
112std::map<std::string, uint32_t> interfaceToTable;
Paul Jensena561e122014-06-12 16:46:37 -0400113
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700114uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700115 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400116 if (index) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700117 index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
118 interfaceToTable[interface] = index;
119 return index;
Paul Jensena561e122014-06-12 16:46:37 -0400120 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700121 // If the interface goes away if_nametoindex() will return 0 but we still need to know
122 // the index so we can remove the rules and routes.
123 auto iter = interfaceToTable.find(interface);
124 if (iter == interfaceToTable.end()) {
125 ALOGE("cannot find interface %s", interface);
126 return RT_TABLE_UNSPEC;
127 }
128 return iter->second;
129}
130
131void addTableName(uint32_t table, const std::string& name, std::string* contents) {
132 char tableString[UINT32_STRLEN];
133 snprintf(tableString, sizeof(tableString), "%u", table);
134 *contents += tableString;
135 *contents += " ";
136 *contents += name;
137 *contents += "\n";
138}
139
140// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
141void updateTableNamesFile() {
142 std::string contents;
143 addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
144 addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents);
145 for (const auto& entry : interfaceToTable) {
146 addTableName(entry.second, entry.first, &contents);
147 }
148
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700149 int fd = open(RT_TABLES_PATH, RT_TABLES_FLAGS, RT_TABLES_MODE);
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700150 if (fd == -1) {
151 ALOGE("failed to create %s (%s)", RT_TABLES_PATH, strerror(errno));
152 return;
153 }
154 // File creation is affected by umask, so make sure the right mode bits are set.
Sreeram Ramachandranc7d804c2014-07-09 07:39:30 -0700155 if (fchmod(fd, RT_TABLES_MODE) == -1) {
156 ALOGE("failed to set mode 0%o on %s (%s)", RT_TABLES_MODE, RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700157 }
158 ssize_t bytesWritten = write(fd, contents.data(), contents.size());
159 if (bytesWritten != static_cast<ssize_t>(contents.size())) {
160 ALOGE("failed to write to %s (%zd vs %zu bytes) (%s)", RT_TABLES_PATH, bytesWritten,
161 contents.size(), strerror(errno));
162 }
163 close(fd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700164}
165
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900166// Sends a netlink request and expects an ack.
167// |iov| is an array of struct iovec that contains the netlink message payload.
168// The netlink header is generated by this function based on |action| and |flags|.
169// Returns -errno if there was an error or if the kernel reported an error.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700170WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900171 nlmsghdr nlmsg = {
172 .nlmsg_type = action,
173 .nlmsg_flags = flags,
174 };
175 iov[0].iov_base = &nlmsg;
176 iov[0].iov_len = sizeof(nlmsg);
177 for (int i = 0; i < iovlen; ++i) {
178 nlmsg.nlmsg_len += iov[i].iov_len;
179 }
180
181 int ret;
182 struct {
183 nlmsghdr msg;
184 nlmsgerr err;
185 } response;
186
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900187 int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
188 if (sock != -1 &&
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700189 connect(sock, reinterpret_cast<const sockaddr*>(&NETLINK_ADDRESS),
190 sizeof(NETLINK_ADDRESS)) != -1 &&
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900191 writev(sock, iov, iovlen) != -1 &&
192 (ret = recv(sock, &response, sizeof(response), 0)) != -1) {
193 if (ret == sizeof(response)) {
194 ret = response.err.error; // Netlink errors are negative errno.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700195 if (ret) {
196 ALOGE("netlink response contains error (%s)", strerror(-ret));
197 }
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900198 } else {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700199 ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response));
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900200 ret = -EBADMSG;
201 }
202 } else {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700203 ALOGE("netlink socket/connect/writev/recv failed (%s)", strerror(errno));
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900204 ret = -errno;
205 }
206
207 if (sock != -1) {
208 close(sock);
209 }
210
211 return ret;
212}
213
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700214// Adds or removes a routing rule for IPv4 and IPv6.
215//
216// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the rule
217// returns ENETUNREACH.
218// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
219// ignored.
220// + If |interface| is non-NULL, the rule matches the specified outgoing interface.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900221//
222// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700223WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
224 uint32_t fwmark, uint32_t mask, const char* interface,
225 uid_t uidStart, uid_t uidEnd) {
226 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
227 if (fwmark & ~mask) {
228 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
229 return -ERANGE;
230 }
231
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900232 // The interface name must include exactly one terminating NULL and be properly padded, or older
233 // kernels will refuse to delete rules.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900234 uint16_t paddingLength = 0;
235 size_t interfaceLength = 0;
236 char oifname[IFNAMSIZ];
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700237 if (interface != OIF_NONE) {
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900238 interfaceLength = strlcpy(oifname, interface, IFNAMSIZ) + 1;
239 if (interfaceLength > IFNAMSIZ) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700240 ALOGE("interface name too long (%zu > %u)", interfaceLength, IFNAMSIZ);
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900241 return -ENAMETOOLONG;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700242 }
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900243 paddingLength = RTA_SPACE(interfaceLength) - RTA_LENGTH(interfaceLength);
244 }
245
Lorenzo Colitti59656512014-06-25 03:20:29 +0900246 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700247 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700248 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900249 return -EUSERS;
250 }
Lorenzo Colitti72723682014-06-26 13:51:10 +0900251 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900252
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900253 // Assemble a rule request and put it in an array of iovec structures.
254 fib_rule_hdr rule = {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700255 .action = static_cast<uint8_t>(table != RT_TABLE_UNSPEC ? FR_ACT_TO_TBL :
256 FR_ACT_UNREACHABLE),
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900257 };
258
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700259 rtattr fraOifname = { U16_RTA_LENGTH(interfaceLength), FRA_OIFNAME };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900260
261 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700262 { NULL, 0 },
263 { &rule, sizeof(rule) },
264 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
265 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700266 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
267 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700268 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
269 { &fwmark, mask ? sizeof(fwmark) : 0 },
270 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
271 { &mask, mask ? sizeof(mask) : 0 },
272 { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
273 { &uidStart, isUidRule ? sizeof(uidStart) : 0 },
274 { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 },
275 { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700276 { &fraOifname, interface != OIF_NONE ? sizeof(fraOifname) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700277 { oifname, interfaceLength },
278 { PADDING_BUFFER, paddingLength },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900279 };
280
Lorenzo Colitti72723682014-06-26 13:51:10 +0900281 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700282 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
283 rule.family = AF_FAMILIES[i];
284 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov))) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900285 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700286 }
287 }
288
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900289 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700290}
291
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900292// Adds or deletes an IPv4 or IPv6 route.
293// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700294WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
295 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900296 // At least the destination must be non-null.
297 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700298 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900299 return -EFAULT;
300 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700301
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900302 // Parse the prefix.
303 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700304 uint8_t family;
305 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900306 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
307 &prefixLength);
308 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700309 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900310 return rawLength;
311 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700312
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900313 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700314 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900315 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
316 }
317
318 // If an interface was specified, find the ifindex.
319 uint32_t ifindex;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700320 if (interface != OIF_NONE) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900321 ifindex = if_nametoindex(interface);
322 if (!ifindex) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700323 ALOGE("cannot find interface %s", interface);
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900324 return -ENODEV;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700325 }
326 }
327
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900328 // If a nexthop was specified, parse it as the same family as the prefix.
329 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700330 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
331 ALOGE("inet_pton failed for nexthop %s", nexthop);
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900332 return -EINVAL;
333 }
334
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900335 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700336 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900337 .rtm_protocol = RTPROT_STATIC,
338 .rtm_type = RTN_UNICAST,
339 .rtm_family = family,
340 .rtm_dst_len = prefixLength,
341 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900342
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700343 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
344 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900345
346 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700347 { NULL, 0 },
348 { &route, sizeof(route) },
349 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
350 { &table, sizeof(table) },
351 { &rtaDst, sizeof(rtaDst) },
352 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700353 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
354 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700355 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
356 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900357 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900358
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700359 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
360 NETLINK_REQUEST_FLAGS;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900361 return sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700362}
363
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700364// Add rules to allow legacy routes added through the requestRouteToHost() API.
365WARN_UNUSED_RESULT int AddLegacyRouteRules() {
366 Fwmark fwmark;
367 Fwmark mask;
368
369 fwmark.explicitlySelected = false;
370 mask.explicitlySelected = true;
371
372 // Rules to allow legacy routes to override the default network.
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700373 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
374 fwmark.intValue, mask.intValue, OIF_NONE, INVALID_UID,
375 INVALID_UID)) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700376 return ret;
377 }
378 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700379 ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue,
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700380 mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID)) {
381 return ret;
382 }
383
384 fwmark.permission = PERMISSION_SYSTEM;
385 mask.permission = PERMISSION_SYSTEM;
386
387 // A rule to allow legacy routes from system apps to override VPNs.
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700388 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
389 fwmark.intValue, mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700390}
391
392// Add a new rule to look up the 'main' table, with the same selectors as the "default network"
393// rule, but with a lower priority. Since the default network rule points to a table with a default
394// route, the rule we're adding will never be used for normal routing lookups. However, the kernel
395// may fall-through to it to find directly-connected routes when it validates that a nexthop (in a
396// route being added) is reachable.
397WARN_UNUSED_RESULT int AddDirectlyConnectedRule() {
398 Fwmark fwmark;
399 Fwmark mask;
400
401 fwmark.netId = NETID_UNSET;
402 mask.netId = FWMARK_NET_ID_MASK;
403
404 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
405 fwmark.intValue, mask.intValue, OIF_NONE, UID_ROOT, UID_ROOT);
406}
407
408// Add a rule to preempt the pre-defined "from all lookup main" rule. Packets that reach this rule
409// will be null-routed, and won't fall-through to the main table.
410WARN_UNUSED_RESULT int AddUnreachableRule() {
411 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, RT_TABLE_UNSPEC, MARK_UNSET,
412 MARK_UNSET, OIF_NONE, INVALID_UID, INVALID_UID);
413}
414
415// An iptables rule to mark incoming packets on a network with the netId of the network.
416//
417// This is so that the kernel can:
418// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
419// replies, SYN-ACKs, etc).
420// + Mark sockets that accept connections from this interface so that the connection stays on the
421// same interface.
422WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
423 Permission permission, bool add) {
424 Fwmark fwmark;
425
426 fwmark.netId = netId;
427 fwmark.explicitlySelected = true;
428 fwmark.protectedFromVpn = true;
429 fwmark.permission = permission;
430
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700431 char markString[UINT32_HEX_STRLEN];
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700432 snprintf(markString, sizeof(markString), "0x%x", fwmark.intValue);
433
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700434 if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j",
435 "MARK", "--set-mark", markString, NULL)) {
436 ALOGE("failed to change iptables rule that sets incoming packet mark");
437 return -EREMOTEIO;
438 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700439
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700440 return 0;
441}
442
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700443// A rule to route traffic based on an explicitly chosen network.
444//
445// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
446//
447// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
448// to check it again in the rules here, because a network's permissions may have been updated via
449// modifyNetworkPermission().
450WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
451 Permission permission, uid_t uidStart,
452 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700453 Fwmark fwmark;
454 Fwmark mask;
455
456 fwmark.netId = netId;
457 mask.netId = FWMARK_NET_ID_MASK;
458
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700459 fwmark.explicitlySelected = true;
460 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700461
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700462 fwmark.permission = permission;
463 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700464
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700465 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
466 fwmark.intValue, mask.intValue, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700467}
468
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700469// A rule to route traffic based on a chosen outgoing interface.
470//
471// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
472// the outgoing interface (typically for link-local communications).
473WARN_UNUSED_RESULT int modifyOutputInterfaceRule(const char* interface, uint32_t table,
474 Permission permission, uid_t uidStart,
475 uid_t uidEnd, bool add) {
476 Fwmark fwmark;
477 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700478
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700479 fwmark.permission = permission;
480 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700481
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700482 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
483 fwmark.intValue, mask.intValue, interface, uidStart, uidEnd);
484}
485
486// A rule to route traffic based on the chosen network.
487//
488// This is for sockets that have not explicitly requested a particular network, but have been
489// bound to one when they called connect(). This ensures that sockets connected on a particular
490// network stay on that network even if the default network changes.
491WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table,
492 Permission permission, bool add) {
493 Fwmark fwmark;
494 Fwmark mask;
495
496 fwmark.netId = netId;
497 mask.netId = FWMARK_NET_ID_MASK;
498
499 fwmark.explicitlySelected = false;
500 mask.explicitlySelected = true;
501
502 fwmark.permission = permission;
503 mask.permission = permission;
504
505 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
506 fwmark.intValue, mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID);
507}
508
509// A rule to route all traffic from a given set of UIDs to go over the VPN.
510//
511// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
512// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
513// bypass the VPN if the protectedFromVpn bit is set.
514WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
515 bool add) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700516 Fwmark fwmark;
517 Fwmark mask;
518
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700519 fwmark.protectedFromVpn = false;
520 mask.protectedFromVpn = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700521
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700522 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_SECURE_VPN, table,
523 fwmark.intValue, mask.intValue, OIF_NONE, uidStart, uidEnd);
524}
525
526// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
527// set of UIDs.
528//
529// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
530// target set, but where the DnsProxyListener itself is not.
531WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool add) {
532 Fwmark fwmark;
533 Fwmark mask;
534
535 fwmark.netId = netId;
536 mask.netId = FWMARK_NET_ID_MASK;
537
538 fwmark.permission = PERMISSION_SYSTEM;
539 mask.permission = PERMISSION_SYSTEM;
540
541 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_SECURE_VPN, table,
542 fwmark.intValue, mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID);
543}
544
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700545// A rule to allow local network routes to override the default network.
546WARN_UNUSED_RESULT int modifyLocalOverrideRule(uint32_t table, bool add) {
547 Fwmark fwmark;
548 Fwmark mask;
549
550 fwmark.explicitlySelected = false;
551 mask.explicitlySelected = true;
552
553 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_LOCAL_NETWORK, table,
554 fwmark.intValue, mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID);
555}
556
557WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
558 uint32_t table = getRouteTableForInterface(interface);
559 if (table == RT_TABLE_UNSPEC) {
560 return -ESRCH;
561 }
562
563 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
564 return ret;
565 }
566 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, INVALID_UID, INVALID_UID,
567 add)) {
568 return ret;
569 }
570 if (int ret = modifyOutputInterfaceRule(interface, table, PERMISSION_NONE, INVALID_UID,
571 INVALID_UID, add)) {
572 return ret;
573 }
574 return modifyLocalOverrideRule(table, add);
575}
576
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700577WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
578 Permission permission, bool add) {
579 uint32_t table = getRouteTableForInterface(interface);
580 if (table == RT_TABLE_UNSPEC) {
581 return -ESRCH;
582 }
583
584 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
585 return ret;
586 }
587 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
588 add)) {
589 return ret;
590 }
591 if (int ret = modifyOutputInterfaceRule(interface, table, permission, INVALID_UID, INVALID_UID,
592 add)) {
593 return ret;
594 }
595 return modifyImplicitNetworkRule(netId, table, permission, add);
596}
597
598WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
599 const UidRanges& uidRanges, bool add,
600 bool modifyNonUidBasedRules) {
601 uint32_t table = getRouteTableForInterface(interface);
602 if (table == RT_TABLE_UNSPEC) {
603 return -ESRCH;
604 }
605
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700606 for (const UidRanges::Range& range : uidRanges.getRanges()) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700607 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.first,
608 range.second, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700609 return ret;
610 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700611 if (int ret = modifyOutputInterfaceRule(interface, table, PERMISSION_NONE, range.first,
612 range.second, add)) {
613 return ret;
614 }
615 if (int ret = modifyVpnUidRangeRule(table, range.first, range.second, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700616 return ret;
617 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700618 }
619
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700620 if (modifyNonUidBasedRules) {
621 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700622 return ret;
623 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700624 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT,
625 add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700626 return ret;
627 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700628 return modifyVpnSystemPermissionRule(netId, table, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700629 }
630
631 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700632}
633
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700634WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
635 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700636 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700637 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900638 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700639 }
640
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700641 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700642 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700643
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700644 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700645 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700646
647 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700648 mask.permission = permission;
649
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700650 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
651 mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700652}
653
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700654// 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 +0900655// route, to the main table as well.
656// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700657WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
658 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700659 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700660 switch (tableType) {
661 case RouteController::INTERFACE: {
662 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700663 if (table == RT_TABLE_UNSPEC) {
664 return -ESRCH;
665 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700666 break;
667 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700668 case RouteController::LEGACY_NETWORK: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700669 table = ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700670 break;
671 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700672 case RouteController::LEGACY_SYSTEM: {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700673 table = ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700674 break;
675 }
676 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700677
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900678 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran1077d292014-06-27 06:42:11 -0700679 // We allow apps to call requestRouteToHost() multiple times with the same route, so ignore
680 // EEXIST failures when adding routes to legacy tables.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700681 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST &&
682 tableType != RouteController::INTERFACE)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900683 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700684 }
685
686 // If there's no nexthop, this is a directly connected route. Add it to the main table also, to
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900687 // let the kernel find it when validating nexthops when global routes are added.
688 if (!nexthop) {
689 ret = modifyIpRoute(action, RT_TABLE_MAIN, interface, destination, NULL);
690 // A failure with action == ADD && errno == EEXIST means that the route already exists in
691 // the main table, perhaps because the kernel added it automatically as part of adding the
692 // IP address to the interface. Ignore this, but complain about everything else.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700693 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900694 return ret;
695 }
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700696 }
697
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900698 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700699}
700
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700701// Returns 0 on success or negative errno on failure.
702WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700703 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700704 if (table == RT_TABLE_UNSPEC) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700705 return -ESRCH;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700706 }
707
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900708 char tableString[UINT32_STRLEN];
709 snprintf(tableString, sizeof(tableString), "%u", table);
710
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700711 for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900712 const char* argv[] = {
713 IP_PATH,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700714 IP_VERSIONS[i],
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700715 "route",
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900716 "flush",
717 "table",
718 tableString,
719 };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700720 if (android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, false, false)) {
721 ALOGE("failed to flush routes");
722 return -EREMOTEIO;
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900723 }
724 }
725
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700726 interfaceToTable.erase(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700727 return 0;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700728}
729
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700730} // namespace
731
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700732int RouteController::Init() {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700733 if (int ret = AddDirectlyConnectedRule()) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700734 return ret;
735 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700736 if (int ret = AddLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700737 return ret;
738 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700739 // TODO: Enable once we are sure everything works.
740 if (false) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700741 if (int ret = AddUnreachableRule()) {
742 return ret;
743 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700744 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700745 updateTableNamesFile();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700746 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700747}
748
Sreeram Ramachandran6a773532014-07-11 09:10:20 -0700749int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
750 if (int ret = modifyLocalNetwork(netId, interface, ACTION_ADD)) {
751 return ret;
752 }
753 updateTableNamesFile();
754 return 0;
755}
756
757int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
758 if (int ret = modifyLocalNetwork(netId, interface, ACTION_DEL)) {
759 return ret;
760 }
761 if (int ret = flushRoutes(interface)) {
762 return ret;
763 }
764 updateTableNamesFile();
765 return 0;
766}
767
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700768int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
769 Permission permission) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700770 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
771 return ret;
772 }
773 updateTableNamesFile();
774 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700775}
776
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700777int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
778 Permission permission) {
779 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700780 return ret;
781 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700782 if (int ret = flushRoutes(interface)) {
783 return ret;
784 }
785 updateTableNamesFile();
786 return 0;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700787}
788
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700789int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
790 const UidRanges& uidRanges) {
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700791 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, ACTION_ADD,
792 MODIFY_NON_UID_BASED_RULES)) {
793 return ret;
794 }
795 updateTableNamesFile();
796 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700797}
798
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700799int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
800 const UidRanges& uidRanges) {
801 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, ACTION_DEL,
802 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700803 return ret;
804 }
Sreeram Ramachandran4acd34a2014-07-07 22:11:37 -0700805 if (int ret = flushRoutes(interface)) {
806 return ret;
807 }
808 updateTableNamesFile();
809 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700810}
811
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700812int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
813 Permission oldPermission,
814 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700815 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700816 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700817 return ret;
818 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700819 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700820}
821
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700822int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface,
823 const UidRanges& uidRanges) {
824 return modifyVirtualNetwork(netId, interface, uidRanges, ACTION_ADD,
825 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700826}
827
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700828int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
829 const UidRanges& uidRanges) {
830 return modifyVirtualNetwork(netId, interface, uidRanges, ACTION_DEL,
831 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700832}
833
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700834int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
835 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700836}
837
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700838int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
839 Permission permission) {
840 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700841}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700842
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700843int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700844 TableType tableType) {
845 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700846}
847
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900848int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700849 const char* nexthop, TableType tableType) {
850 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700851}