blob: bc50dc415e8474ab1fe2113da83cc8f7fc00ebee [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>
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090028#include <linux/fib_rules.h>
Paul Jensena561e122014-06-12 16:46:37 -040029#include <map>
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070030#include <net/if.h>
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070031
32namespace {
33
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070034// BEGIN CONSTANTS --------------------------------------------------------------------------------
35
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070036const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
37// const uint32_t RULE_PRIORITY_VPN_OVERRIDE_LOCAL = 11000;
38const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
39const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
40const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
41const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000;
42const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000;
43// const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000;
44// const uint32_t RULE_PRIORITY_TETHERING = 18000;
45const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000;
46// const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000;
47// const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000;
48const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000;
49const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 23000;
50const uint32_t RULE_PRIORITY_UNREACHABLE = 24000;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070051
Lorenzo Colitti59656512014-06-25 03:20:29 +090052// TODO: These values aren't defined by the Linux kernel, because our UID routing changes are not
53// upstream (yet?), so we can't just pick them up from kernel headers. When (if?) the changes make
54// it upstream, we'll remove this and rely on the kernel header values. For now, add a static assert
55// that will warn us if upstream has given these values some other meaning.
56const uint16_t FRA_UID_START = 18;
57const uint16_t FRA_UID_END = 19;
58static_assert(FRA_UID_START > FRA_MAX,
59 "Android-specific FRA_UID_{START,END} values also assigned in Linux uapi. "
60 "Check that these values match what the kernel does and then update this assertion.");
61
Lorenzo Colitti72723682014-06-26 13:51:10 +090062const uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK;
63const uint16_t NETLINK_CREATE_REQUEST_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE | NLM_F_EXCL;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090064
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070065const sockaddr_nl NETLINK_ADDRESS = {AF_NETLINK, 0, 0, 0};
66
67const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
68
69const char* const IP_VERSIONS[] = {"-4", "-6"};
70
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -070071const uid_t UID_ROOT = 0;
72const char* const OIF_NONE = NULL;
73const bool ACTION_ADD = true;
74const bool ACTION_DEL = false;
75const bool MODIFY_NON_UID_BASED_RULES = true;
76
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070077// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
78// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
79constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
80 return RTA_LENGTH(x);
81}
82
83// These are practically const, but can't be declared so, because they are used to initialize
84// non-const pointers ("void* iov_base") in iovec arrays.
85rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
86rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
87rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
88rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
89rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START };
90rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END };
91
92rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
93rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
94
95uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
96
97// END CONSTANTS ----------------------------------------------------------------------------------
98
Paul Jensena561e122014-06-12 16:46:37 -040099std::map<std::string, uint32_t> interfaceToIndex;
100
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700101uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700102 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -0400103 if (index) {
104 interfaceToIndex[interface] = index;
105 } else {
106 // If the interface goes away if_nametoindex() will return 0 but we still need to know
107 // the index so we can remove the rules and routes.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700108 auto iter = interfaceToIndex.find(interface);
109 if (iter == interfaceToIndex.end()) {
110 ALOGE("cannot find interface %s", interface);
111 return RT_TABLE_UNSPEC;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700112 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700113 index = iter->second;
Paul Jensena561e122014-06-12 16:46:37 -0400114 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700115 return index + RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700116}
117
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900118// Sends a netlink request and expects an ack.
119// |iov| is an array of struct iovec that contains the netlink message payload.
120// The netlink header is generated by this function based on |action| and |flags|.
121// Returns -errno if there was an error or if the kernel reported an error.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700122WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900123 nlmsghdr nlmsg = {
124 .nlmsg_type = action,
125 .nlmsg_flags = flags,
126 };
127 iov[0].iov_base = &nlmsg;
128 iov[0].iov_len = sizeof(nlmsg);
129 for (int i = 0; i < iovlen; ++i) {
130 nlmsg.nlmsg_len += iov[i].iov_len;
131 }
132
133 int ret;
134 struct {
135 nlmsghdr msg;
136 nlmsgerr err;
137 } response;
138
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900139 int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
140 if (sock != -1 &&
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700141 connect(sock, reinterpret_cast<const sockaddr*>(&NETLINK_ADDRESS),
142 sizeof(NETLINK_ADDRESS)) != -1 &&
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900143 writev(sock, iov, iovlen) != -1 &&
144 (ret = recv(sock, &response, sizeof(response), 0)) != -1) {
145 if (ret == sizeof(response)) {
146 ret = response.err.error; // Netlink errors are negative errno.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700147 if (ret) {
148 ALOGE("netlink response contains error (%s)", strerror(-ret));
149 }
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900150 } else {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700151 ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response));
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900152 ret = -EBADMSG;
153 }
154 } else {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700155 ALOGE("netlink socket/connect/writev/recv failed (%s)", strerror(errno));
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900156 ret = -errno;
157 }
158
159 if (sock != -1) {
160 close(sock);
161 }
162
163 return ret;
164}
165
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700166// Adds or removes a routing rule for IPv4 and IPv6.
167//
168// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the rule
169// returns ENETUNREACH.
170// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
171// ignored.
172// + If |interface| is non-NULL, the rule matches the specified outgoing interface.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900173//
174// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700175WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
176 uint32_t fwmark, uint32_t mask, const char* interface,
177 uid_t uidStart, uid_t uidEnd) {
178 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
179 if (fwmark & ~mask) {
180 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
181 return -ERANGE;
182 }
183
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900184 // The interface name must include exactly one terminating NULL and be properly padded, or older
185 // kernels will refuse to delete rules.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900186 uint16_t paddingLength = 0;
187 size_t interfaceLength = 0;
188 char oifname[IFNAMSIZ];
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700189 if (interface != OIF_NONE) {
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900190 interfaceLength = strlcpy(oifname, interface, IFNAMSIZ) + 1;
191 if (interfaceLength > IFNAMSIZ) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700192 ALOGE("interface name too long (%zu > %u)", interfaceLength, IFNAMSIZ);
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900193 return -ENAMETOOLONG;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700194 }
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900195 paddingLength = RTA_SPACE(interfaceLength) - RTA_LENGTH(interfaceLength);
196 }
197
Lorenzo Colitti59656512014-06-25 03:20:29 +0900198 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700199 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700200 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900201 return -EUSERS;
202 }
Lorenzo Colitti72723682014-06-26 13:51:10 +0900203 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900204
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900205 // Assemble a rule request and put it in an array of iovec structures.
206 fib_rule_hdr rule = {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700207 .action = static_cast<uint8_t>(table != RT_TABLE_UNSPEC ? FR_ACT_TO_TBL :
208 FR_ACT_UNREACHABLE),
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900209 };
210
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700211 rtattr fraOifname = { U16_RTA_LENGTH(interfaceLength), FRA_OIFNAME };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900212
213 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700214 { NULL, 0 },
215 { &rule, sizeof(rule) },
216 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
217 { &priority, sizeof(priority) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700218 { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
219 { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700220 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
221 { &fwmark, mask ? sizeof(fwmark) : 0 },
222 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
223 { &mask, mask ? sizeof(mask) : 0 },
224 { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
225 { &uidStart, isUidRule ? sizeof(uidStart) : 0 },
226 { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 },
227 { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700228 { &fraOifname, interface != OIF_NONE ? sizeof(fraOifname) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700229 { oifname, interfaceLength },
230 { PADDING_BUFFER, paddingLength },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900231 };
232
Lorenzo Colitti72723682014-06-26 13:51:10 +0900233 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700234 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
235 rule.family = AF_FAMILIES[i];
236 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov))) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900237 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700238 }
239 }
240
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900241 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700242}
243
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900244// Adds or deletes an IPv4 or IPv6 route.
245// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700246WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
247 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900248 // At least the destination must be non-null.
249 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700250 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900251 return -EFAULT;
252 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700253
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900254 // Parse the prefix.
255 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700256 uint8_t family;
257 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900258 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
259 &prefixLength);
260 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700261 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900262 return rawLength;
263 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700264
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900265 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700266 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900267 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
268 }
269
270 // If an interface was specified, find the ifindex.
271 uint32_t ifindex;
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700272 if (interface != OIF_NONE) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900273 ifindex = if_nametoindex(interface);
274 if (!ifindex) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700275 ALOGE("cannot find interface %s", interface);
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900276 return -ENODEV;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700277 }
278 }
279
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900280 // If a nexthop was specified, parse it as the same family as the prefix.
281 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700282 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
283 ALOGE("inet_pton failed for nexthop %s", nexthop);
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900284 return -EINVAL;
285 }
286
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900287 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700288 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900289 .rtm_protocol = RTPROT_STATIC,
290 .rtm_type = RTN_UNICAST,
291 .rtm_family = family,
292 .rtm_dst_len = prefixLength,
293 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900294
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700295 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
296 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900297
298 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700299 { NULL, 0 },
300 { &route, sizeof(route) },
301 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
302 { &table, sizeof(table) },
303 { &rtaDst, sizeof(rtaDst) },
304 { rawAddress, static_cast<size_t>(rawLength) },
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700305 { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
306 { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 },
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700307 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
308 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900309 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900310
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700311 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
312 NETLINK_REQUEST_FLAGS;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900313 return sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700314}
315
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700316// Add rules to allow legacy routes added through the requestRouteToHost() API.
317WARN_UNUSED_RESULT int AddLegacyRouteRules() {
318 Fwmark fwmark;
319 Fwmark mask;
320
321 fwmark.explicitlySelected = false;
322 mask.explicitlySelected = true;
323
324 // Rules to allow legacy routes to override the default network.
325 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM,
326 RouteController::ROUTE_TABLE_LEGACY_SYSTEM, fwmark.intValue,
327 mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID)) {
328 return ret;
329 }
330 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
331 RouteController::ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue,
332 mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID)) {
333 return ret;
334 }
335
336 fwmark.permission = PERMISSION_SYSTEM;
337 mask.permission = PERMISSION_SYSTEM;
338
339 // A rule to allow legacy routes from system apps to override VPNs.
340 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM,
341 RouteController::ROUTE_TABLE_LEGACY_SYSTEM, fwmark.intValue, mask.intValue,
342 OIF_NONE, INVALID_UID, INVALID_UID);
343}
344
345// Add a new rule to look up the 'main' table, with the same selectors as the "default network"
346// rule, but with a lower priority. Since the default network rule points to a table with a default
347// route, the rule we're adding will never be used for normal routing lookups. However, the kernel
348// may fall-through to it to find directly-connected routes when it validates that a nexthop (in a
349// route being added) is reachable.
350WARN_UNUSED_RESULT int AddDirectlyConnectedRule() {
351 Fwmark fwmark;
352 Fwmark mask;
353
354 fwmark.netId = NETID_UNSET;
355 mask.netId = FWMARK_NET_ID_MASK;
356
357 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
358 fwmark.intValue, mask.intValue, OIF_NONE, UID_ROOT, UID_ROOT);
359}
360
361// Add a rule to preempt the pre-defined "from all lookup main" rule. Packets that reach this rule
362// will be null-routed, and won't fall-through to the main table.
363WARN_UNUSED_RESULT int AddUnreachableRule() {
364 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, RT_TABLE_UNSPEC, MARK_UNSET,
365 MARK_UNSET, OIF_NONE, INVALID_UID, INVALID_UID);
366}
367
368// An iptables rule to mark incoming packets on a network with the netId of the network.
369//
370// This is so that the kernel can:
371// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
372// replies, SYN-ACKs, etc).
373// + Mark sockets that accept connections from this interface so that the connection stays on the
374// same interface.
375WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
376 Permission permission, bool add) {
377 Fwmark fwmark;
378
379 fwmark.netId = netId;
380 fwmark.explicitlySelected = true;
381 fwmark.protectedFromVpn = true;
382 fwmark.permission = permission;
383
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700384 char markString[UINT32_HEX_STRLEN];
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700385 snprintf(markString, sizeof(markString), "0x%x", fwmark.intValue);
386
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700387 if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j",
388 "MARK", "--set-mark", markString, NULL)) {
389 ALOGE("failed to change iptables rule that sets incoming packet mark");
390 return -EREMOTEIO;
391 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700392
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700393 return 0;
394}
395
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700396// A rule to route traffic based on an explicitly chosen network.
397//
398// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
399//
400// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
401// to check it again in the rules here, because a network's permissions may have been updated via
402// modifyNetworkPermission().
403WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
404 Permission permission, uid_t uidStart,
405 uid_t uidEnd, bool add) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700406 Fwmark fwmark;
407 Fwmark mask;
408
409 fwmark.netId = netId;
410 mask.netId = FWMARK_NET_ID_MASK;
411
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700412 fwmark.explicitlySelected = true;
413 mask.explicitlySelected = true;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700414
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700415 fwmark.permission = permission;
416 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700417
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700418 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
419 fwmark.intValue, mask.intValue, OIF_NONE, uidStart, uidEnd);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700420}
421
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700422// A rule to route traffic based on a chosen outgoing interface.
423//
424// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
425// the outgoing interface (typically for link-local communications).
426WARN_UNUSED_RESULT int modifyOutputInterfaceRule(const char* interface, uint32_t table,
427 Permission permission, uid_t uidStart,
428 uid_t uidEnd, bool add) {
429 Fwmark fwmark;
430 Fwmark mask;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700431
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700432 fwmark.permission = permission;
433 mask.permission = permission;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700434
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700435 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
436 fwmark.intValue, mask.intValue, interface, uidStart, uidEnd);
437}
438
439// A rule to route traffic based on the chosen network.
440//
441// This is for sockets that have not explicitly requested a particular network, but have been
442// bound to one when they called connect(). This ensures that sockets connected on a particular
443// network stay on that network even if the default network changes.
444WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table,
445 Permission permission, bool add) {
446 Fwmark fwmark;
447 Fwmark mask;
448
449 fwmark.netId = netId;
450 mask.netId = FWMARK_NET_ID_MASK;
451
452 fwmark.explicitlySelected = false;
453 mask.explicitlySelected = true;
454
455 fwmark.permission = permission;
456 mask.permission = permission;
457
458 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
459 fwmark.intValue, mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID);
460}
461
462// A rule to route all traffic from a given set of UIDs to go over the VPN.
463//
464// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
465// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
466// bypass the VPN if the protectedFromVpn bit is set.
467WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
468 bool add) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700469 Fwmark fwmark;
470 Fwmark mask;
471
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700472 fwmark.protectedFromVpn = false;
473 mask.protectedFromVpn = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700474
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700475 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_SECURE_VPN, table,
476 fwmark.intValue, mask.intValue, OIF_NONE, uidStart, uidEnd);
477}
478
479// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
480// set of UIDs.
481//
482// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
483// target set, but where the DnsProxyListener itself is not.
484WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool add) {
485 Fwmark fwmark;
486 Fwmark mask;
487
488 fwmark.netId = netId;
489 mask.netId = FWMARK_NET_ID_MASK;
490
491 fwmark.permission = PERMISSION_SYSTEM;
492 mask.permission = PERMISSION_SYSTEM;
493
494 return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_SECURE_VPN, table,
495 fwmark.intValue, mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID);
496}
497
498WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
499 Permission permission, bool add) {
500 uint32_t table = getRouteTableForInterface(interface);
501 if (table == RT_TABLE_UNSPEC) {
502 return -ESRCH;
503 }
504
505 if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
506 return ret;
507 }
508 if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
509 add)) {
510 return ret;
511 }
512 if (int ret = modifyOutputInterfaceRule(interface, table, permission, INVALID_UID, INVALID_UID,
513 add)) {
514 return ret;
515 }
516 return modifyImplicitNetworkRule(netId, table, permission, add);
517}
518
519WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
520 const UidRanges& uidRanges, bool add,
521 bool modifyNonUidBasedRules) {
522 uint32_t table = getRouteTableForInterface(interface);
523 if (table == RT_TABLE_UNSPEC) {
524 return -ESRCH;
525 }
526
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -0700527 for (const UidRanges::Range& range : uidRanges.getRanges()) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700528 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.first,
529 range.second, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700530 return ret;
531 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700532 if (int ret = modifyOutputInterfaceRule(interface, table, PERMISSION_NONE, range.first,
533 range.second, add)) {
534 return ret;
535 }
536 if (int ret = modifyVpnUidRangeRule(table, range.first, range.second, add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700537 return ret;
538 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700539 }
540
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700541 if (modifyNonUidBasedRules) {
542 if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700543 return ret;
544 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700545 if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT,
546 add)) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700547 return ret;
548 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700549 return modifyVpnSystemPermissionRule(netId, table, add);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700550 }
551
552 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700553}
554
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700555WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
556 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700557 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700558 if (table == RT_TABLE_UNSPEC) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900559 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700560 }
561
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700562 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700563 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700564
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700565 fwmark.netId = NETID_UNSET;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700566 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700567
568 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700569 mask.permission = permission;
570
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700571 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
572 mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700573}
574
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700575// 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 +0900576// route, to the main table as well.
577// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700578WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
579 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700580 uint32_t table;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700581 switch (tableType) {
582 case RouteController::INTERFACE: {
583 table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700584 if (table == RT_TABLE_UNSPEC) {
585 return -ESRCH;
586 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700587 break;
588 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700589 case RouteController::LEGACY_NETWORK: {
590 table = RouteController::ROUTE_TABLE_LEGACY_NETWORK;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700591 break;
592 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700593 case RouteController::LEGACY_SYSTEM: {
594 table = RouteController::ROUTE_TABLE_LEGACY_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700595 break;
596 }
597 }
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700598
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900599 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran1077d292014-06-27 06:42:11 -0700600 // We allow apps to call requestRouteToHost() multiple times with the same route, so ignore
601 // EEXIST failures when adding routes to legacy tables.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700602 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST &&
603 tableType != RouteController::INTERFACE)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900604 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700605 }
606
607 // 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 +0900608 // let the kernel find it when validating nexthops when global routes are added.
609 if (!nexthop) {
610 ret = modifyIpRoute(action, RT_TABLE_MAIN, interface, destination, NULL);
611 // A failure with action == ADD && errno == EEXIST means that the route already exists in
612 // the main table, perhaps because the kernel added it automatically as part of adding the
613 // IP address to the interface. Ignore this, but complain about everything else.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700614 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900615 return ret;
616 }
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700617 }
618
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900619 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700620}
621
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700622// Returns 0 on success or negative errno on failure.
623WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700624 uint32_t table = getRouteTableForInterface(interface);
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700625 if (table == RT_TABLE_UNSPEC) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700626 return -ESRCH;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700627 }
628
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900629 char tableString[UINT32_STRLEN];
630 snprintf(tableString, sizeof(tableString), "%u", table);
631
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700632 for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900633 const char* argv[] = {
634 IP_PATH,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700635 IP_VERSIONS[i],
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700636 "route",
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900637 "flush",
638 "table",
639 tableString,
640 };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700641 if (android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, false, false)) {
642 ALOGE("failed to flush routes");
643 return -EREMOTEIO;
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900644 }
645 }
646
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700647 interfaceToIndex.erase(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700648 return 0;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700649}
650
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700651} // namespace
652
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700653int RouteController::Init() {
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700654 if (int ret = AddDirectlyConnectedRule()) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700655 return ret;
656 }
657
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700658 if (int ret = AddLegacyRouteRules()) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700659 return ret;
660 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700661 // TODO: Enable once we are sure everything works.
662 if (false) {
663 return AddUnreachableRule();
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700664 }
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700665 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700666}
667
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700668int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
669 Permission permission) {
670 return modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700671}
672
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700673int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
674 Permission permission) {
675 if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700676 return ret;
677 }
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700678 return flushRoutes(interface);
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700679}
680
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700681int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
682 const UidRanges& uidRanges) {
683 return modifyVirtualNetwork(netId, interface, uidRanges, ACTION_ADD,
684 MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700685}
686
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700687int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
688 const UidRanges& uidRanges) {
689 if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, ACTION_DEL,
690 MODIFY_NON_UID_BASED_RULES)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700691 return ret;
692 }
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700693 return flushRoutes(interface);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700694}
695
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700696int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
697 Permission oldPermission,
698 Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700699 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700700 if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700701 return ret;
702 }
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700703 return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700704}
705
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700706int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface,
707 const UidRanges& uidRanges) {
708 return modifyVirtualNetwork(netId, interface, uidRanges, ACTION_ADD,
709 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700710}
711
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700712int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
713 const UidRanges& uidRanges) {
714 return modifyVirtualNetwork(netId, interface, uidRanges, ACTION_DEL,
715 !MODIFY_NON_UID_BASED_RULES);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700716}
717
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700718int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
719 return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700720}
721
Sreeram Ramachandran5009d5e2014-07-03 12:20:48 -0700722int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
723 Permission permission) {
724 return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700725}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700726
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700727int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700728 TableType tableType) {
729 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700730}
731
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900732int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700733 const char* nexthop, TableType tableType) {
734 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700735}