blob: d2d1841a68cae3079d07ea614e746d6fc4ac2d8a [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 Ramachandran5c181bf2014-04-07 14:10:04 -070025
Lorenzo Colittiba25df92014-06-18 00:22:17 +090026#include <arpa/inet.h>
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090027#include <linux/fib_rules.h>
Paul Jensena561e122014-06-12 16:46:37 -040028#include <map>
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070029#include <net/if.h>
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070030
31namespace {
32
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070033// BEGIN CONSTANTS --------------------------------------------------------------------------------
34
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -070035const uint32_t RULE_PRIORITY_VPN_OVERRIDES = 11000;
36const uint32_t RULE_PRIORITY_SECURE_VPN = 12000;
37const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000;
38const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000;
39const uint32_t RULE_PRIORITY_PRIVILEGED_LEGACY = 16000;
40const uint32_t RULE_PRIORITY_LEGACY = 17000;
41const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 18000;
42const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 21000;
43const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 22000;
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -070044// TODO: Uncomment once we are sure everything works.
45#if 0
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -070046const uint32_t RULE_PRIORITY_UNREACHABLE = 23000;
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -070047#endif
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070048
Lorenzo Colitti59656512014-06-25 03:20:29 +090049// TODO: These values aren't defined by the Linux kernel, because our UID routing changes are not
50// upstream (yet?), so we can't just pick them up from kernel headers. When (if?) the changes make
51// it upstream, we'll remove this and rely on the kernel header values. For now, add a static assert
52// that will warn us if upstream has given these values some other meaning.
53const uint16_t FRA_UID_START = 18;
54const uint16_t FRA_UID_END = 19;
55static_assert(FRA_UID_START > FRA_MAX,
56 "Android-specific FRA_UID_{START,END} values also assigned in Linux uapi. "
57 "Check that these values match what the kernel does and then update this assertion.");
58
Lorenzo Colitti72723682014-06-26 13:51:10 +090059const uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK;
60const uint16_t NETLINK_CREATE_REQUEST_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE | NLM_F_EXCL;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090061
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070062const sockaddr_nl NETLINK_ADDRESS = {AF_NETLINK, 0, 0, 0};
63
64const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
65
66const char* const IP_VERSIONS[] = {"-4", "-6"};
67
68// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
69// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
70constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
71 return RTA_LENGTH(x);
72}
73
74// These are practically const, but can't be declared so, because they are used to initialize
75// non-const pointers ("void* iov_base") in iovec arrays.
76rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
77rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
78rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
79rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
80rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START };
81rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END };
82
83rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
84rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
85
86uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
87
88// END CONSTANTS ----------------------------------------------------------------------------------
89
Paul Jensena561e122014-06-12 16:46:37 -040090std::map<std::string, uint32_t> interfaceToIndex;
91
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070092uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070093 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -040094 if (index) {
95 interfaceToIndex[interface] = index;
96 } else {
97 // If the interface goes away if_nametoindex() will return 0 but we still need to know
98 // the index so we can remove the rules and routes.
99 std::map<std::string, uint32_t>::iterator it = interfaceToIndex.find(interface);
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700100 if (it != interfaceToIndex.end()) {
Paul Jensena561e122014-06-12 16:46:37 -0400101 index = it->second;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700102 }
Paul Jensena561e122014-06-12 16:46:37 -0400103 }
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700104 return index ? index + RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX : 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700105}
106
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900107// Sends a netlink request and expects an ack.
108// |iov| is an array of struct iovec that contains the netlink message payload.
109// The netlink header is generated by this function based on |action| and |flags|.
110// Returns -errno if there was an error or if the kernel reported an error.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700111WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900112 nlmsghdr nlmsg = {
113 .nlmsg_type = action,
114 .nlmsg_flags = flags,
115 };
116 iov[0].iov_base = &nlmsg;
117 iov[0].iov_len = sizeof(nlmsg);
118 for (int i = 0; i < iovlen; ++i) {
119 nlmsg.nlmsg_len += iov[i].iov_len;
120 }
121
122 int ret;
123 struct {
124 nlmsghdr msg;
125 nlmsgerr err;
126 } response;
127
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900128 int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
129 if (sock != -1 &&
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700130 connect(sock, reinterpret_cast<const sockaddr*>(&NETLINK_ADDRESS),
131 sizeof(NETLINK_ADDRESS)) != -1 &&
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900132 writev(sock, iov, iovlen) != -1 &&
133 (ret = recv(sock, &response, sizeof(response), 0)) != -1) {
134 if (ret == sizeof(response)) {
135 ret = response.err.error; // Netlink errors are negative errno.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700136 if (ret) {
137 ALOGE("netlink response contains error (%s)", strerror(-ret));
138 }
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900139 } else {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700140 ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response));
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900141 ret = -EBADMSG;
142 }
143 } else {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700144 ALOGE("netlink socket/connect/writev/recv failed (%s)", strerror(errno));
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900145 ret = -errno;
146 }
147
148 if (sock != -1) {
149 close(sock);
150 }
151
152 return ret;
153}
154
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700155// Adds or removes a routing rule for IPv4 and IPv6.
156//
157// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the rule
158// returns ENETUNREACH.
159// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
160// ignored.
161// + If |interface| is non-NULL, the rule matches the specified outgoing interface.
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900162//
163// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700164WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
165 uint32_t fwmark, uint32_t mask, const char* interface,
166 uid_t uidStart, uid_t uidEnd) {
167 // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
168 if (fwmark & ~mask) {
169 ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
170 return -ERANGE;
171 }
172
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900173 // The interface name must include exactly one terminating NULL and be properly padded, or older
174 // kernels will refuse to delete rules.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900175 uint16_t paddingLength = 0;
176 size_t interfaceLength = 0;
177 char oifname[IFNAMSIZ];
178 if (interface) {
179 interfaceLength = strlcpy(oifname, interface, IFNAMSIZ) + 1;
180 if (interfaceLength > IFNAMSIZ) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700181 ALOGE("interface name too long (%zu > %u)", interfaceLength, IFNAMSIZ);
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900182 return -ENAMETOOLONG;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700183 }
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900184 paddingLength = RTA_SPACE(interfaceLength) - RTA_LENGTH(interfaceLength);
185 }
186
Lorenzo Colitti59656512014-06-25 03:20:29 +0900187 // Either both start and end UID must be specified, or neither.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700188 if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
Sreeram Ramachandrancf891382014-07-01 15:49:20 -0700189 ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900190 return -EUSERS;
191 }
Lorenzo Colitti72723682014-06-26 13:51:10 +0900192 bool isUidRule = (uidStart != INVALID_UID);
Lorenzo Colitti59656512014-06-25 03:20:29 +0900193
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900194 // Assemble a rule request and put it in an array of iovec structures.
195 fib_rule_hdr rule = {
196 .action = static_cast<uint8_t>(table ? FR_ACT_TO_TBL : FR_ACT_UNREACHABLE),
197 };
198
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700199 rtattr fraOifname = { U16_RTA_LENGTH(interfaceLength), FRA_OIFNAME };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900200
201 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700202 { NULL, 0 },
203 { &rule, sizeof(rule) },
204 { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) },
205 { &priority, sizeof(priority) },
206 { &FRATTR_TABLE, table ? sizeof(FRATTR_TABLE) : 0 },
207 { &table, table ? sizeof(table) : 0 },
208 { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 },
209 { &fwmark, mask ? sizeof(fwmark) : 0 },
210 { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 },
211 { &mask, mask ? sizeof(mask) : 0 },
212 { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
213 { &uidStart, isUidRule ? sizeof(uidStart) : 0 },
214 { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 },
215 { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 },
216 { &fraOifname, interface ? sizeof(fraOifname) : 0 },
217 { oifname, interfaceLength },
218 { PADDING_BUFFER, paddingLength },
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900219 };
220
Lorenzo Colitti72723682014-06-26 13:51:10 +0900221 uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700222 for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
223 rule.family = AF_FAMILIES[i];
224 if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov))) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900225 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700226 }
227 }
228
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900229 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700230}
231
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900232// Adds or deletes an IPv4 or IPv6 route.
233// Returns 0 on success or negative errno on failure.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700234WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
235 const char* destination, const char* nexthop) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900236 // At least the destination must be non-null.
237 if (!destination) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700238 ALOGE("null destination");
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900239 return -EFAULT;
240 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700241
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900242 // Parse the prefix.
243 uint8_t rawAddress[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700244 uint8_t family;
245 uint8_t prefixLength;
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900246 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
247 &prefixLength);
248 if (rawLength < 0) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700249 ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900250 return rawLength;
251 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700252
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900253 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
Sreeram Ramachandran1201e842014-07-01 15:06:05 -0700254 ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900255 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
256 }
257
258 // If an interface was specified, find the ifindex.
259 uint32_t ifindex;
260 if (interface) {
261 ifindex = if_nametoindex(interface);
262 if (!ifindex) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700263 ALOGE("cannot find interface %s", interface);
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900264 return -ENODEV;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700265 }
266 }
267
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900268 // If a nexthop was specified, parse it as the same family as the prefix.
269 uint8_t rawNexthop[sizeof(in6_addr)];
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700270 if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
271 ALOGE("inet_pton failed for nexthop %s", nexthop);
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900272 return -EINVAL;
273 }
274
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900275 // Assemble a rtmsg and put it in an array of iovec structures.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700276 rtmsg route = {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900277 .rtm_protocol = RTPROT_STATIC,
278 .rtm_type = RTN_UNICAST,
279 .rtm_family = family,
280 .rtm_dst_len = prefixLength,
281 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900282
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700283 rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST };
284 rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900285
286 iovec iov[] = {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700287 { NULL, 0 },
288 { &route, sizeof(route) },
289 { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
290 { &table, sizeof(table) },
291 { &rtaDst, sizeof(rtaDst) },
292 { rawAddress, static_cast<size_t>(rawLength) },
293 { &RTATTR_OIF, interface ? sizeof(RTATTR_OIF) : 0 },
294 { &ifindex, interface ? sizeof(ifindex) : 0 },
295 { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 },
296 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900297 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900298
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700299 uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
300 NETLINK_REQUEST_FLAGS;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900301 return sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700302}
303
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700304WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface, bool add) {
305 // An iptables rule to mark incoming packets on a network with the netId of the network.
306 //
307 // This is so that the kernel can:
308 // + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700309 // ping replies, SYN-ACKs, etc).
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700310 // + Mark sockets that accept connections from this interface so that the connection stays on
311 // the same interface.
312 char markString[UINT32_HEX_STRLEN];
313 snprintf(markString, sizeof(markString), "0x%x", netId);
314 if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j",
315 "MARK", "--set-mark", markString, NULL)) {
316 ALOGE("failed to change iptables rule that sets incoming packet mark");
317 return -EREMOTEIO;
318 }
319 return 0;
320}
321
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700322// A rule to let UID 0 (as a proxy for the kernel) access the routes on a given network.
323WARN_UNUSED_RESULT int modifyKernelAccessRule(uint16_t action, unsigned netId, uint32_t priority,
324 uint32_t table) {
325 Fwmark fwmark;
326 Fwmark mask;
327
328 fwmark.netId = netId;
329 mask.netId = FWMARK_NET_ID_MASK;
330
331 return modifyIpRule(action, priority, table, fwmark.intValue, mask.intValue, NULL, 0, 0);
332}
333
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700334WARN_UNUSED_RESULT int modifyPerNetworkRules(unsigned netId, const char* interface, uint32_t table,
335 Permission permission, uid_t uidStart, uid_t uidEnd,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700336 bool add, bool modifyInterfaceBasedRules) {
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700337 if (!table) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700338 table = getRouteTableForInterface(interface);
339 if (!table) {
340 ALOGE("cannot find interface %s", interface);
341 return -ESRCH;
342 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700343 }
344
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900345 uint16_t action = add ? RTM_NEWRULE : RTM_DELRULE;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700346
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700347 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700348 Fwmark mask;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700349
350 // A rule to route traffic based on a chosen outgoing interface.
351 //
352 // Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already
353 // knows the outgoing interface (typically for link-local communications).
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700354 fwmark.permission = permission;
355 mask.permission = permission;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700356 if (int ret = modifyIpRule(action, RULE_PRIORITY_OUTPUT_INTERFACE, table, fwmark.intValue,
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700357 mask.intValue, interface, uidStart, uidEnd)) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900358 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700359 }
360
361 // A rule to route traffic based on the chosen network.
362 //
363 // This is for sockets that have not explicitly requested a particular network, but have been
364 // bound to one when they called connect(). This ensures that sockets connected on a particular
365 // network stay on that network even if the default network changes.
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700366 fwmark.netId = netId;
367 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700368 if (int ret = modifyIpRule(action, RULE_PRIORITY_IMPLICIT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700369 mask.intValue, NULL, uidStart, uidEnd)) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900370 return ret;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700371 }
372
373 // A rule to route traffic based on an explicitly chosen network.
374 //
375 // Supports apps that use the multinetwork APIs to restrict their traffic to a network.
376 //
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700377 // Even though we check permissions at the time we set a netId into the fwmark of a socket, we
378 // still need to check it again in the rules here, because a network's permissions may have been
379 // updated via modifyNetworkPermission().
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700380 fwmark.explicitlySelected = true;
381 mask.explicitlySelected = true;
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700382 if (int ret = modifyIpRule(action, RULE_PRIORITY_EXPLICIT_NETWORK, table, fwmark.intValue,
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700383 mask.intValue, NULL, uidStart, uidEnd)) {
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900384 return ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700385 }
386
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700387 if (permission != PERMISSION_NONE) {
388 if (int ret = modifyKernelAccessRule(action, netId, RULE_PRIORITY_IMPLICIT_NETWORK,
389 table)) {
390 return ret;
391 }
392 }
393
394 if (modifyInterfaceBasedRules) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700395 if (int ret = modifyIncomingPacketMark(netId, interface, add)) {
396 return ret;
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700397 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700398 }
399
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900400 return 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700401}
402
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700403// Adds or removes rules for VPNs that affect UIDs in |uidRanges|. If |modifyInterfaceBasedRules|
404// is true, also modifies the rules that are based only on the |interface| and not on |uidRanges|.
405// When adding or removing an interface to the VPN, set it to true. When adding or removing UIDs
406// without changing the VPN's interfaces, set it to false.
407WARN_UNUSED_RESULT int modifyVpnRules(unsigned netId, const char* interface,
408 const UidRanges& uidRanges, bool add,
409 bool modifyInterfaceBasedRules) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700410 uint32_t table = getRouteTableForInterface(interface);
411 if (!table) {
412 ALOGE("cannot find interface %s", interface);
413 return -ESRCH;
414 }
415
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700416 uint16_t action = add ? RTM_NEWRULE : RTM_DELRULE;
417
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700418 Fwmark fwmark;
419 Fwmark mask;
420
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700421 fwmark.protectedFromVpn = false;
422 mask.protectedFromVpn = true;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700423
424 for (const std::pair<uid_t, uid_t>& range : uidRanges.getRanges()) {
425 if (int ret = modifyPerNetworkRules(netId, interface, table, PERMISSION_NONE, range.first,
426 range.second, add, false)) {
427 return ret;
428 }
429
430 // A rule to route all traffic from a given set of UIDs to go over the VPN.
431 //
432 // Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket
433 // may have, if they are subject to this VPN, their traffic has to go through it. Allows the
434 // traffic to bypass the VPN if the protectedFromVpn bit is set.
435 if (int ret = modifyIpRule(action, RULE_PRIORITY_SECURE_VPN, table, fwmark.intValue,
436 mask.intValue, NULL, range.first, range.second)) {
437 return ret;
438 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700439 }
440
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700441 if (modifyInterfaceBasedRules) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700442 if (int ret = modifyKernelAccessRule(action, netId, RULE_PRIORITY_IMPLICIT_NETWORK,
443 table)) {
444 return ret;
445 }
446
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700447 if (int ret = modifyIncomingPacketMark(netId, interface, add)) {
448 return ret;
449 }
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700450
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700451 // A rule to allow privileged apps to send traffic over this VPN even if they are not part
452 // of the target set of UIDs.
453 //
454 // This is needed for DnsProxyListener to correctly resolve a request for a user who is in
455 // the target set, but where the DnsProxyListener itself is not.
456 fwmark.protectedFromVpn = false;
457 mask.protectedFromVpn = false;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700458
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700459 fwmark.netId = netId;
460 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700461
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700462 fwmark.permission = PERMISSION_SYSTEM;
463 mask.permission = PERMISSION_SYSTEM;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700464
465 if (int ret = modifyIpRule(action, RULE_PRIORITY_SECURE_VPN, table, fwmark.intValue,
466 mask.intValue, NULL, INVALID_UID, INVALID_UID)) {
467 return ret;
468 }
469 }
470
471 return 0;
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700472}
473
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700474WARN_UNUSED_RESULT int modifyDefaultNetworkRules(uint16_t action, const char* interface,
475 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700476 uint32_t table = getRouteTableForInterface(interface);
477 if (!table) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700478 ALOGE("cannot find interface %s", interface);
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900479 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700480 }
481
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700482 Fwmark fwmark;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700483 Fwmark mask;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700484
485 fwmark.netId = 0;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700486 mask.netId = FWMARK_NET_ID_MASK;
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700487
488 fwmark.permission = permission;
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700489 mask.permission = permission;
490
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700491 if (int ret = modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
492 mask.intValue, NULL, INVALID_UID, INVALID_UID)) {
493 return ret;
494 }
495
496 if (permission != PERMISSION_NONE) {
497 if (int ret = modifyKernelAccessRule(action, 0, RULE_PRIORITY_DEFAULT_NETWORK, table)) {
498 return ret;
499 }
500 }
501
502 return 0;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700503}
504
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700505// 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 +0900506// route, to the main table as well.
507// Returns 0 on success or negative errno on failure.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700508WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
509 const char* nexthop, RouteController::TableType tableType) {
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700510 uint32_t table = 0;
511 switch (tableType) {
512 case RouteController::INTERFACE: {
513 table = getRouteTableForInterface(interface);
514 break;
515 }
516 case RouteController::LEGACY: {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700517 table = RouteController::ROUTE_TABLE_LEGACY;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700518 break;
519 }
520 case RouteController::PRIVILEGED_LEGACY: {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700521 table = RouteController::ROUTE_TABLE_PRIVILEGED_LEGACY;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700522 break;
523 }
524 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700525 if (!table) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700526 ALOGE("cannot find table for interface %s and tableType %d", interface, tableType);
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900527 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700528 }
529
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900530 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
Sreeram Ramachandran1077d292014-06-27 06:42:11 -0700531 // We allow apps to call requestRouteToHost() multiple times with the same route, so ignore
532 // EEXIST failures when adding routes to legacy tables.
533 if (ret != 0 && !(action == RTM_NEWROUTE && ret == -EEXIST &&
534 (tableType == RouteController::LEGACY ||
535 tableType == RouteController::PRIVILEGED_LEGACY))) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900536 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700537 }
538
539 // 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 +0900540 // let the kernel find it when validating nexthops when global routes are added.
541 if (!nexthop) {
542 ret = modifyIpRoute(action, RT_TABLE_MAIN, interface, destination, NULL);
543 // A failure with action == ADD && errno == EEXIST means that the route already exists in
544 // the main table, perhaps because the kernel added it automatically as part of adding the
545 // IP address to the interface. Ignore this, but complain about everything else.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700546 if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900547 return ret;
548 }
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700549 }
550
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900551 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700552}
553
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700554// Returns 0 on success or negative errno on failure.
555WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700556 uint32_t table = getRouteTableForInterface(interface);
557 if (!table) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700558 ALOGE("cannot find interface %s", interface);
559 return -ESRCH;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700560 }
Paul Jensena561e122014-06-12 16:46:37 -0400561 interfaceToIndex.erase(interface);
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700562
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900563 char tableString[UINT32_STRLEN];
564 snprintf(tableString, sizeof(tableString), "%u", table);
565
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700566 for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900567 const char* argv[] = {
568 IP_PATH,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700569 IP_VERSIONS[i],
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700570 "route",
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900571 "flush",
572 "table",
573 tableString,
574 };
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700575 if (android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, false, false)) {
576 ALOGE("failed to flush routes");
577 return -EREMOTEIO;
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900578 }
579 }
580
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700581 return 0;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700582}
583
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700584} // namespace
585
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700586int RouteController::Init() {
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700587 // Add a new rule to look up the 'main' table, with the same selectors as the "default network"
588 // rule, but with a lower priority. Since the default network rule points to a table with a
589 // default route, the rule we're adding will never be used for normal routing lookups. However,
590 // the kernel may fall-through to it to find directly-connected routes when it validates that a
591 // nexthop (in a route being added) is reachable.
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700592 if (int ret = modifyKernelAccessRule(RTM_NEWRULE, 0, RULE_PRIORITY_DIRECTLY_CONNECTED,
593 RT_TABLE_MAIN)) {
594 return ret;
595 }
596
597 Fwmark fwmark;
598 Fwmark mask;
599
600 // Add rules to allow legacy routes to override the default network.
601 fwmark.explicitlySelected = false;
602 mask.explicitlySelected = true;
603
604 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_PRIVILEGED_LEGACY,
605 RouteController::ROUTE_TABLE_PRIVILEGED_LEGACY, fwmark.intValue,
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700606 mask.intValue, NULL, INVALID_UID, INVALID_UID)) {
607 return ret;
608 }
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700609
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700610 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY,
611 RouteController::ROUTE_TABLE_LEGACY, fwmark.intValue, mask.intValue,
612 NULL, INVALID_UID, INVALID_UID)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700613 return ret;
614 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700615
Sreeram Ramachandraned4bd1f2014-07-05 12:31:05 -0700616 // Add a rule to allow legacy routes from system apps to override VPNs.
617 fwmark.permission = PERMISSION_SYSTEM;
618 mask.permission = PERMISSION_SYSTEM;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700619
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700620 if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDES,
621 RouteController::ROUTE_TABLE_PRIVILEGED_LEGACY, fwmark.intValue,
622 mask.intValue, NULL, INVALID_UID, INVALID_UID)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700623 return ret;
624 }
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700625
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700626// TODO: Uncomment once we are sure everything works.
627#if 0
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700628 // Add a rule to preempt the pre-defined "from all lookup main" rule. Packets that reach this
629 // rule will be null-routed, and won't fall-through to the main table.
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700630 return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, 0, 0, 0, NULL, INVALID_UID,
631 INVALID_UID);
632#else
633 return 0;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700634#endif
635}
636
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900637int RouteController::addInterfaceToNetwork(unsigned netId, const char* interface,
638 Permission permission) {
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700639 return modifyPerNetworkRules(netId, interface, 0, permission, INVALID_UID, INVALID_UID, true,
640 true);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700641}
642
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900643int RouteController::removeInterfaceFromNetwork(unsigned netId, const char* interface,
644 Permission permission) {
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700645 if (int ret = modifyPerNetworkRules(netId, interface, 0, permission, INVALID_UID, INVALID_UID,
646 false, true)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700647 return ret;
648 }
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700649 return flushRoutes(interface);
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700650}
651
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700652int RouteController::addInterfaceToVpn(unsigned netId, const char* interface,
653 const UidRanges& uidRanges) {
654 return modifyVpnRules(netId, interface, uidRanges, true, true);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700655}
656
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700657int RouteController::removeInterfaceFromVpn(unsigned netId, const char* interface,
658 const UidRanges& uidRanges) {
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700659 if (int ret = modifyVpnRules(netId, interface, uidRanges, false, true)) {
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700660 return ret;
661 }
Sreeram Ramachandran72999d62014-07-02 18:06:34 -0700662 return flushRoutes(interface);
Sreeram Ramachandran4043f012014-06-23 12:41:37 -0700663}
664
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900665int RouteController::modifyNetworkPermission(unsigned netId, const char* interface,
666 Permission oldPermission, Permission newPermission) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700667 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700668 if (int ret = modifyPerNetworkRules(netId, interface, 0, newPermission, INVALID_UID,
669 INVALID_UID, true, false)) {
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700670 return ret;
671 }
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -0700672 return modifyPerNetworkRules(netId, interface, 0, oldPermission, INVALID_UID, INVALID_UID,
673 false, false);
674}
675
676int RouteController::addUsersToVpn(unsigned netId, const char* interface,
677 const UidRanges& uidRanges) {
678 return modifyVpnRules(netId, interface, uidRanges, true, false);
679}
680
681int RouteController::removeUsersFromVpn(unsigned netId, const char* interface,
682 const UidRanges& uidRanges) {
683 return modifyVpnRules(netId, interface, uidRanges, false, false);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700684}
685
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900686int RouteController::addToDefaultNetwork(const char* interface, Permission permission) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700687 return modifyDefaultNetworkRules(RTM_NEWRULE, interface, permission);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700688}
689
Lorenzo Colitti96f261e2014-06-23 15:09:54 +0900690int RouteController::removeFromDefaultNetwork(const char* interface, Permission permission) {
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700691 return modifyDefaultNetworkRules(RTM_DELRULE, interface, permission);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700692}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700693
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -0700694int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700695 TableType tableType) {
696 return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700697}
698
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900699int RouteController::removeRoute(const char* interface, const char* destination,
Sreeram Ramachandraneb27b7e2014-07-01 14:30:30 -0700700 const char* nexthop, TableType tableType) {
701 return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700702}