blob: 9dbd021be91c1cc08b3e96b3b1f50153eba5716f [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"
20#include "NetdConstants.h"
21
Lorenzo Colittiba25df92014-06-18 00:22:17 +090022#include <arpa/inet.h>
23#include <errno.h>
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090024#include <linux/fib_rules.h>
Lorenzo Colittiba25df92014-06-18 00:22:17 +090025#include <linux/netlink.h>
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070026#include <linux/rtnetlink.h>
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070027#include <logwrap/logwrap.h>
Paul Jensena561e122014-06-12 16:46:37 -040028#include <map>
Lorenzo Colittiba25df92014-06-18 00:22:17 +090029#include <netinet/in.h>
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070030#include <net/if.h>
Lorenzo Colittiba25df92014-06-18 00:22:17 +090031#include <sys/socket.h>
32#include <sys/uio.h>
33#include <unistd.h>
34
35// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
36// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
37#define U16_RTA_LENGTH(x) static_cast<uint16_t>(RTA_LENGTH((x)))
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070038
39namespace {
40
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -070041const uint32_t RULE_PRIORITY_PRIVILEGED_LEGACY = 11000;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070042const uint32_t RULE_PRIORITY_PER_NETWORK_EXPLICIT = 13000;
43const uint32_t RULE_PRIORITY_PER_NETWORK_INTERFACE = 14000;
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -070044const uint32_t RULE_PRIORITY_LEGACY = 16000;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070045const uint32_t RULE_PRIORITY_PER_NETWORK_NORMAL = 17000;
46const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 19000;
47const uint32_t RULE_PRIORITY_MAIN = 20000;
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -070048// TODO: Uncomment once we are sure everything works.
49#if 0
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -070050const uint32_t RULE_PRIORITY_UNREACHABLE = 21000;
Sreeram Ramachandran56afacf2014-05-28 15:07:00 -070051#endif
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070052
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -070053// TODO: These should be turned into per-UID tables once the kernel supports UID-based routing.
54const int ROUTE_TABLE_PRIVILEGED_LEGACY = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX - 901;
55const int ROUTE_TABLE_LEGACY = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX - 902;
56
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090057const uint16_t kNetlinkRequestFlags = NLM_F_REQUEST | NLM_F_ACK;
58const uint16_t kNetlinkCreateRequestFlags = kNetlinkRequestFlags | NLM_F_CREATE | NLM_F_EXCL;
59
Paul Jensena561e122014-06-12 16:46:37 -040060std::map<std::string, uint32_t> interfaceToIndex;
61
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070062uint32_t getRouteTableForInterface(const char* interface) {
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070063 uint32_t index = if_nametoindex(interface);
Paul Jensena561e122014-06-12 16:46:37 -040064 if (index) {
65 interfaceToIndex[interface] = index;
66 } else {
67 // If the interface goes away if_nametoindex() will return 0 but we still need to know
68 // the index so we can remove the rules and routes.
69 std::map<std::string, uint32_t>::iterator it = interfaceToIndex.find(interface);
70 if (it != interfaceToIndex.end())
71 index = it->second;
72 }
Sreeram Ramachandrana4811802014-04-10 12:10:24 -070073 return index ? index + RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX : 0;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -070074}
75
Lorenzo Colitti4753afd2014-06-20 23:03:29 +090076// Sends a netlink request and expects an ack.
77// |iov| is an array of struct iovec that contains the netlink message payload.
78// The netlink header is generated by this function based on |action| and |flags|.
79// Returns -errno if there was an error or if the kernel reported an error.
80int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
81 nlmsghdr nlmsg = {
82 .nlmsg_type = action,
83 .nlmsg_flags = flags,
84 };
85 iov[0].iov_base = &nlmsg;
86 iov[0].iov_len = sizeof(nlmsg);
87 for (int i = 0; i < iovlen; ++i) {
88 nlmsg.nlmsg_len += iov[i].iov_len;
89 }
90
91 int ret;
92 struct {
93 nlmsghdr msg;
94 nlmsgerr err;
95 } response;
96
97 sockaddr_nl kernel = {AF_NETLINK, 0, 0, 0};
98 int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
99 if (sock != -1 &&
100 connect(sock, reinterpret_cast<sockaddr*>(&kernel), sizeof(kernel)) != -1 &&
101 writev(sock, iov, iovlen) != -1 &&
102 (ret = recv(sock, &response, sizeof(response), 0)) != -1) {
103 if (ret == sizeof(response)) {
104 ret = response.err.error; // Netlink errors are negative errno.
105 } else {
106 ret = -EBADMSG;
107 }
108 } else {
109 ret = -errno;
110 }
111
112 if (sock != -1) {
113 close(sock);
114 }
115
116 return ret;
117}
118
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700119// Adds or removes a routing rule for IPv4 and IPv6.
120//
121// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the rule
122// returns ENETUNREACH.
123// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
124// ignored.
125// + If |interface| is non-NULL, the rule matches the specified outgoing interface.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900126bool modifyIpRule(uint16_t action, uint32_t priority, uint32_t table, uint32_t fwmark,
127 uint32_t mask, const char* interface) {
128 // The interface name must include exactly one terminating NULL and be properly padded, or older
129 // kernels will refuse to delete rules.
130 uint8_t padding[RTA_ALIGNTO] = {0, 0, 0, 0};
131 uint16_t paddingLength = 0;
132 size_t interfaceLength = 0;
133 char oifname[IFNAMSIZ];
134 if (interface) {
135 interfaceLength = strlcpy(oifname, interface, IFNAMSIZ) + 1;
136 if (interfaceLength > IFNAMSIZ) {
137 return -ENAMETOOLONG;
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700138 }
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900139 paddingLength = RTA_SPACE(interfaceLength) - RTA_LENGTH(interfaceLength);
140 }
141
142 // Assemble a rule request and put it in an array of iovec structures.
143 fib_rule_hdr rule = {
144 .action = static_cast<uint8_t>(table ? FR_ACT_TO_TBL : FR_ACT_UNREACHABLE),
145 };
146
147 rtattr fra_priority = { U16_RTA_LENGTH(sizeof(priority)), FRA_PRIORITY };
148 rtattr fra_table = { U16_RTA_LENGTH(sizeof(table)), FRA_TABLE };
149 rtattr fra_fwmark = { U16_RTA_LENGTH(sizeof(fwmark)), FRA_FWMARK };
150 rtattr fra_fwmask = { U16_RTA_LENGTH(sizeof(mask)), FRA_FWMASK };
151 rtattr fra_oifname = { U16_RTA_LENGTH(interfaceLength), FRA_OIFNAME };
152
153 iovec iov[] = {
154 { NULL, 0 },
155 { &rule, sizeof(rule) },
156 { &fra_priority, sizeof(fra_priority) },
157 { &priority, sizeof(priority) },
158 { &fra_table, table ? sizeof(fra_table) : 0 },
159 { &table, table ? sizeof(table) : 0 },
160 { &fra_fwmark, mask ? sizeof(fra_fwmark) : 0 },
161 { &fwmark, mask ? sizeof(fwmark) : 0 },
162 { &fra_fwmask, mask ? sizeof(fra_fwmask) : 0 },
163 { &mask, mask ? sizeof(mask) : 0 },
164 { &fra_oifname, interface ? sizeof(fra_oifname) : 0 },
165 { oifname, interfaceLength },
166 { padding, paddingLength },
167 };
168
169 uint16_t flags = (action == RTM_NEWRULE) ? kNetlinkCreateRequestFlags : kNetlinkRequestFlags;
170 uint8_t family[] = {AF_INET, AF_INET6};
171 for (size_t i = 0; i < ARRAY_SIZE(family); ++i) {
172 rule.family = family[i];
173 int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
174 if (ret) {
175 errno = -ret;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700176 return false;
177 }
178 }
179
180 return true;
181}
182
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900183// Adds or deletes an IPv4 or IPv6 route.
184// Returns 0 on success or negative errno on failure.
185int modifyIpRoute(uint16_t action, uint32_t table, const char* interface, const char* destination,
186 const char* nexthop) {
187 // At least the destination must be non-null.
188 if (!destination) {
189 return -EFAULT;
190 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700191
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900192 // Parse the prefix.
193 uint8_t rawAddress[sizeof(in6_addr)];
194 uint8_t family, prefixLength;
195 int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
196 &prefixLength);
197 if (rawLength < 0) {
198 return rawLength;
199 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700200
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900201 if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
202 return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6.
203 }
204
205 // If an interface was specified, find the ifindex.
206 uint32_t ifindex;
207 if (interface) {
208 ifindex = if_nametoindex(interface);
209 if (!ifindex) {
210 return -ENODEV;
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700211 }
212 }
213
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900214 // If a nexthop was specified, parse it as the same family as the prefix.
215 uint8_t rawNexthop[sizeof(in6_addr)];
216 if (nexthop && !inet_pton(family, nexthop, rawNexthop)) {
217 return -EINVAL;
218 }
219
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900220 // Assemble a rtmsg and put it in an array of iovec structures.
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900221 rtmsg rtmsg = {
222 .rtm_protocol = RTPROT_STATIC,
223 .rtm_type = RTN_UNICAST,
224 .rtm_family = family,
225 .rtm_dst_len = prefixLength,
226 };
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900227
228 rtattr rta_table = { U16_RTA_LENGTH(sizeof(table)), RTA_TABLE };
229 rtattr rta_oif = { U16_RTA_LENGTH(sizeof(ifindex)), RTA_OIF };
230 rtattr rta_dst = { U16_RTA_LENGTH(rawLength), RTA_DST };
231 rtattr rta_gateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900232
233 iovec iov[] = {
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900234 { NULL, 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900235 { &rtmsg, sizeof(rtmsg) },
236 { &rta_table, sizeof(rta_table) },
237 { &table, sizeof(table) },
238 { &rta_dst, sizeof(rta_dst) },
239 { rawAddress, static_cast<size_t>(rawLength) },
240 { &rta_oif, interface ? sizeof(rta_oif) : 0 },
Sreeram Ramachandran7f972fb2014-06-24 16:09:21 -0700241 { &ifindex, interface ? sizeof(ifindex) : 0 },
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900242 { &rta_gateway, nexthop ? sizeof(rta_gateway) : 0 },
243 { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 },
244 };
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900245
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900246 uint16_t flags = (action == RTM_NEWROUTE) ? kNetlinkCreateRequestFlags : kNetlinkRequestFlags;
247 return sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700248}
249
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700250bool modifyPerNetworkRules(unsigned netId, const char* interface, Permission permission, bool add,
251 bool modifyIptables) {
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700252 uint32_t table = getRouteTableForInterface(interface);
253 if (!table) {
254 return false;
255 }
256
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900257 uint16_t action = add ? RTM_NEWRULE : RTM_DELRULE;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700258
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700259 Fwmark fwmark;
260 fwmark.permission = permission;
261
262 Fwmark mask;
263 mask.permission = permission;
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700264
265 // A rule to route traffic based on a chosen outgoing interface.
266 //
267 // Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already
268 // knows the outgoing interface (typically for link-local communications).
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900269 if (!modifyIpRule(action, RULE_PRIORITY_PER_NETWORK_INTERFACE, table, fwmark.intValue,
270 mask.intValue, interface)) {
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700271 return false;
272 }
273
274 // A rule to route traffic based on the chosen network.
275 //
276 // This is for sockets that have not explicitly requested a particular network, but have been
277 // bound to one when they called connect(). This ensures that sockets connected on a particular
278 // network stay on that network even if the default network changes.
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700279 fwmark.netId = netId;
280 mask.netId = FWMARK_NET_ID_MASK;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900281 if (!modifyIpRule(action, RULE_PRIORITY_PER_NETWORK_NORMAL, table, fwmark.intValue,
282 mask.intValue, NULL)) {
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700283 return false;
284 }
285
286 // A rule to route traffic based on an explicitly chosen network.
287 //
288 // Supports apps that use the multinetwork APIs to restrict their traffic to a network.
289 //
290 // We don't really need to check the permission bits of the fwmark here, as they would've been
291 // checked at the time the netId was set into the fwmark, but we do so to be consistent.
292 fwmark.explicitlySelected = true;
293 mask.explicitlySelected = true;
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900294 if (!modifyIpRule(action, RULE_PRIORITY_PER_NETWORK_EXPLICIT, table, fwmark.intValue,
295 mask.intValue, NULL)) {
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700296 return false;
297 }
298
299 // An iptables rule to mark incoming packets on a network with the netId of the network.
300 //
301 // This is so that the kernel can:
302 // + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors,
Sreeram Ramachandrana4811802014-04-10 12:10:24 -0700303 // ping replies).
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700304 // + Mark sockets that accept connections from this interface so that the connection stays on
305 // the same interface.
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700306 if (modifyIptables) {
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900307 const char* iptablesAction = add ? "-A" : "-D";
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700308 char markString[UINT32_HEX_STRLEN];
309 snprintf(markString, sizeof(markString), "0x%x", netId);
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900310 if (execIptables(V4V6, "-t", "mangle", iptablesAction, "INPUT", "-i", interface,
311 "-j", "MARK", "--set-mark", markString, NULL)) {
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700312 return false;
313 }
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700314 }
315
316 return true;
317}
318
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900319bool modifyDefaultNetworkRules(const char* interface, Permission permission, uint16_t action) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700320 uint32_t table = getRouteTableForInterface(interface);
321 if (!table) {
322 return false;
323 }
324
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700325 Fwmark fwmark;
326 fwmark.netId = 0;
327 fwmark.permission = permission;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700328
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700329 Fwmark mask;
330 mask.netId = FWMARK_NET_ID_MASK;
331 mask.permission = permission;
332
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900333 return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
334 mask.intValue, NULL);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700335}
336
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900337// Adds or removes an IPv4 or IPv6 route to the specified table and, if it's directly-connected
338// route, to the main table as well.
339// Returns 0 on success or negative errno on failure.
340int modifyRoute(const char* interface, const char* destination, const char* nexthop,
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900341 uint16_t action, RouteController::TableType tableType, unsigned /* uid */) {
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700342 uint32_t table = 0;
343 switch (tableType) {
344 case RouteController::INTERFACE: {
345 table = getRouteTableForInterface(interface);
346 break;
347 }
348 case RouteController::LEGACY: {
349 // TODO: Use the UID to assign a unique table per UID instead of this fixed table.
350 table = ROUTE_TABLE_LEGACY;
351 break;
352 }
353 case RouteController::PRIVILEGED_LEGACY: {
354 // TODO: Use the UID to assign a unique table per UID instead of this fixed table.
355 table = ROUTE_TABLE_PRIVILEGED_LEGACY;
356 break;
357 }
358 }
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700359 if (!table) {
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900360 return -ESRCH;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700361 }
362
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900363 int ret = modifyIpRoute(action, table, interface, destination, nexthop);
364 if (ret != 0) {
365 return ret;
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700366 }
367
368 // 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 +0900369 // let the kernel find it when validating nexthops when global routes are added.
370 if (!nexthop) {
371 ret = modifyIpRoute(action, RT_TABLE_MAIN, interface, destination, NULL);
372 // A failure with action == ADD && errno == EEXIST means that the route already exists in
373 // the main table, perhaps because the kernel added it automatically as part of adding the
374 // IP address to the interface. Ignore this, but complain about everything else.
375 if (ret != 0 && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
376 return ret;
377 }
Sreeram Ramachandranc9213372014-04-16 12:32:18 -0700378 }
379
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900380 return 0;
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700381}
382
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700383bool flushRoutes(const char* interface) {
384 uint32_t table = getRouteTableForInterface(interface);
385 if (!table) {
386 return false;
387 }
Paul Jensena561e122014-06-12 16:46:37 -0400388 interfaceToIndex.erase(interface);
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700389
Lorenzo Colitti357e5622014-06-17 16:14:17 +0900390 char tableString[UINT32_STRLEN];
391 snprintf(tableString, sizeof(tableString), "%u", table);
392
393 const char* version[] = {"-4", "-6"};
394 for (size_t i = 0; i < ARRAY_SIZE(version); ++i) {
395 const char* argv[] = {
396 IP_PATH,
397 version[i],
398 "route"
399 "flush",
400 "table",
401 tableString,
402 };
403 int argc = ARRAY_SIZE(argv);
404
405 if (!android_fork_execvp(argc, const_cast<char**>(argv), NULL, false, false)) {
406 return false;
407 }
408 }
409
410 return true;
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700411}
412
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700413} // namespace
414
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700415void RouteController::Init() {
416 // Add a new rule to look up the 'main' table, with the same selectors as the "default network"
417 // rule, but with a lower priority. Since the default network rule points to a table with a
418 // default route, the rule we're adding will never be used for normal routing lookups. However,
419 // the kernel may fall-through to it to find directly-connected routes when it validates that a
420 // nexthop (in a route being added) is reachable.
Sreeram Ramachandran122f5812014-05-11 20:29:49 -0700421 Fwmark fwmark;
422 fwmark.netId = 0;
423
424 Fwmark mask;
425 mask.netId = FWMARK_NET_ID_MASK;
426
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900427 modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_MAIN, RT_TABLE_MAIN, fwmark.intValue, mask.intValue,
428 NULL);
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700429
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700430 // Add rules to allow lookup of legacy routes.
431 //
432 // TODO: Remove these once the kernel supports UID-based routing. Instead, add them on demand
433 // when routes are added.
434 fwmark.netId = 0;
435 mask.netId = 0;
436
437 fwmark.explicitlySelected = false;
438 mask.explicitlySelected = true;
439
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900440 modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY, ROUTE_TABLE_LEGACY, fwmark.intValue,
441 mask.intValue, NULL);
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700442
443 fwmark.permission = PERMISSION_CONNECTIVITY_INTERNAL;
444 mask.permission = PERMISSION_CONNECTIVITY_INTERNAL;
445
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900446 modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_PRIVILEGED_LEGACY, ROUTE_TABLE_PRIVILEGED_LEGACY,
447 fwmark.intValue, mask.intValue, NULL);
Sreeram Ramachandran38b7af12014-05-22 14:21:49 -0700448
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700449// TODO: Uncomment once we are sure everything works.
450#if 0
451 // Add a rule to preempt the pre-defined "from all lookup main" rule. This ensures that packets
452 // that are already marked with a specific NetId don't fall-through to the main table.
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900453 modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, 0, 0, 0, NULL);
Sreeram Ramachandran8fe9c8e2014-04-16 12:08:05 -0700454#endif
455}
456
Paul Jensenae37e8a2014-04-28 10:35:51 -0400457bool RouteController::addInterfaceToNetwork(unsigned netId, const char* interface,
458 Permission permission) {
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700459 return modifyPerNetworkRules(netId, interface, permission, true, true);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700460}
461
Paul Jensenae37e8a2014-04-28 10:35:51 -0400462bool RouteController::removeInterfaceFromNetwork(unsigned netId, const char* interface,
463 Permission permission) {
Sreeram Ramachandran92b66c42014-04-15 14:28:55 -0700464 return modifyPerNetworkRules(netId, interface, permission, false, true) &&
465 flushRoutes(interface);
Sreeram Ramachandran379bd332014-04-10 19:58:06 -0700466}
467
468bool RouteController::modifyNetworkPermission(unsigned netId, const char* interface,
469 Permission oldPermission, Permission newPermission) {
470 // Add the new rules before deleting the old ones, to avoid race conditions.
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700471 return modifyPerNetworkRules(netId, interface, newPermission, true, false) &&
472 modifyPerNetworkRules(netId, interface, oldPermission, false, false);
473}
474
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700475bool RouteController::addToDefaultNetwork(const char* interface, Permission permission) {
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900476 return modifyDefaultNetworkRules(interface, permission, RTM_NEWRULE);
Sreeram Ramachandran9c0d3132014-04-10 20:35:04 -0700477}
478
Sreeram Ramachandran72604072014-05-21 13:19:43 -0700479bool RouteController::removeFromDefaultNetwork(const char* interface, Permission permission) {
Lorenzo Colitti4753afd2014-06-20 23:03:29 +0900480 return modifyDefaultNetworkRules(interface, permission, RTM_DELRULE);
Sreeram Ramachandran5c181bf2014-04-07 14:10:04 -0700481}
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700482
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900483int RouteController::addRoute(const char* interface, const char* destination,
484 const char* nexthop, TableType tableType, unsigned uid) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900485 return modifyRoute(interface, destination, nexthop, RTM_NEWROUTE, tableType, uid);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700486}
487
Lorenzo Colittif7fc8ec2014-06-18 00:41:58 +0900488int RouteController::removeRoute(const char* interface, const char* destination,
489 const char* nexthop, TableType tableType, unsigned uid) {
Lorenzo Colittiba25df92014-06-18 00:22:17 +0900490 return modifyRoute(interface, destination, nexthop, RTM_DELROUTE, tableType, uid);
Sreeram Ramachandran7619e1b2014-04-15 14:23:08 -0700491}