RouteController.cpp revision 5009d5ef3fbcdc69d772b528fd22184b7d605afa
1/*
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 "UidRanges.h"
21
22#define LOG_TAG "Netd"
23#include "log/log.h"
24#include "logwrap/logwrap.h"
25#include "resolv_netid.h"
26
27#include <arpa/inet.h>
28#include <linux/fib_rules.h>
29#include <map>
30#include <net/if.h>
31
32namespace {
33
34// BEGIN CONSTANTS --------------------------------------------------------------------------------
35
36const 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;
51
52// 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
62const 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;
64
65const 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
71const 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
77// 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
99std::map<std::string, uint32_t> interfaceToIndex;
100
101uint32_t getRouteTableForInterface(const char* interface) {
102    uint32_t index = if_nametoindex(interface);
103    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.
108        auto iter = interfaceToIndex.find(interface);
109        if (iter == interfaceToIndex.end()) {
110            ALOGE("cannot find interface %s", interface);
111            return RT_TABLE_UNSPEC;
112        }
113        index = iter->second;
114    }
115    return index + RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
116}
117
118// 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.
122WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
123    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
139    int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
140    if (sock != -1 &&
141            connect(sock, reinterpret_cast<const sockaddr*>(&NETLINK_ADDRESS),
142                    sizeof(NETLINK_ADDRESS)) != -1 &&
143            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.
147            if (ret) {
148                ALOGE("netlink response contains error (%s)", strerror(-ret));
149            }
150        } else {
151            ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response));
152            ret = -EBADMSG;
153        }
154    } else {
155        ALOGE("netlink socket/connect/writev/recv failed (%s)", strerror(errno));
156        ret = -errno;
157    }
158
159    if (sock != -1) {
160        close(sock);
161    }
162
163    return ret;
164}
165
166// 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.
173//
174// Returns 0 on success or negative errno on failure.
175WARN_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
184    // The interface name must include exactly one terminating NULL and be properly padded, or older
185    // kernels will refuse to delete rules.
186    uint16_t paddingLength = 0;
187    size_t interfaceLength = 0;
188    char oifname[IFNAMSIZ];
189    if (interface != OIF_NONE) {
190        interfaceLength = strlcpy(oifname, interface, IFNAMSIZ) + 1;
191        if (interfaceLength > IFNAMSIZ) {
192            ALOGE("interface name too long (%zu > %u)", interfaceLength, IFNAMSIZ);
193            return -ENAMETOOLONG;
194        }
195        paddingLength = RTA_SPACE(interfaceLength) - RTA_LENGTH(interfaceLength);
196    }
197
198    // Either both start and end UID must be specified, or neither.
199    if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
200        ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
201        return -EUSERS;
202    }
203    bool isUidRule = (uidStart != INVALID_UID);
204
205    // Assemble a rule request and put it in an array of iovec structures.
206    fib_rule_hdr rule = {
207        .action = static_cast<uint8_t>(table != RT_TABLE_UNSPEC ? FR_ACT_TO_TBL :
208                                                                  FR_ACT_UNREACHABLE),
209    };
210
211    rtattr fraOifname = { U16_RTA_LENGTH(interfaceLength), FRA_OIFNAME };
212
213    iovec iov[] = {
214        { NULL,              0 },
215        { &rule,             sizeof(rule) },
216        { &FRATTR_PRIORITY,  sizeof(FRATTR_PRIORITY) },
217        { &priority,         sizeof(priority) },
218        { &FRATTR_TABLE,     table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
219        { &table,            table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
220        { &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 },
228        { &fraOifname,       interface != OIF_NONE ? sizeof(fraOifname) : 0 },
229        { oifname,           interfaceLength },
230        { PADDING_BUFFER,    paddingLength },
231    };
232
233    uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
234    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))) {
237            return ret;
238        }
239    }
240
241    return 0;
242}
243
244// Adds or deletes an IPv4 or IPv6 route.
245// Returns 0 on success or negative errno on failure.
246WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
247                                     const char* destination, const char* nexthop) {
248    // At least the destination must be non-null.
249    if (!destination) {
250        ALOGE("null destination");
251        return -EFAULT;
252    }
253
254    // Parse the prefix.
255    uint8_t rawAddress[sizeof(in6_addr)];
256    uint8_t family;
257    uint8_t prefixLength;
258    int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
259                                &prefixLength);
260    if (rawLength < 0) {
261        ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
262        return rawLength;
263    }
264
265    if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
266        ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
267        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;
272    if (interface != OIF_NONE) {
273        ifindex = if_nametoindex(interface);
274        if (!ifindex) {
275            ALOGE("cannot find interface %s", interface);
276            return -ENODEV;
277        }
278    }
279
280    // If a nexthop was specified, parse it as the same family as the prefix.
281    uint8_t rawNexthop[sizeof(in6_addr)];
282    if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
283        ALOGE("inet_pton failed for nexthop %s", nexthop);
284        return -EINVAL;
285    }
286
287    // Assemble a rtmsg and put it in an array of iovec structures.
288    rtmsg route = {
289        .rtm_protocol = RTPROT_STATIC,
290        .rtm_type = RTN_UNICAST,
291        .rtm_family = family,
292        .rtm_dst_len = prefixLength,
293    };
294
295    rtattr rtaDst     = { U16_RTA_LENGTH(rawLength), RTA_DST };
296    rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
297
298    iovec iov[] = {
299        { 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) },
305        { &RTATTR_OIF,   interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
306        { &ifindex,      interface != OIF_NONE ? sizeof(ifindex) : 0 },
307        { &rtaGateway,   nexthop ? sizeof(rtaGateway) : 0 },
308        { rawNexthop,    nexthop ? static_cast<size_t>(rawLength) : 0 },
309    };
310
311    uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
312                                                NETLINK_REQUEST_FLAGS;
313    return sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
314}
315
316// 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
384    char markString[UINT32_HEX_STRLEN];
385    snprintf(markString, sizeof(markString), "0x%x", fwmark.intValue);
386
387    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    }
392
393    return 0;
394}
395
396// 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) {
406    Fwmark fwmark;
407    Fwmark mask;
408
409    fwmark.netId = netId;
410    mask.netId = FWMARK_NET_ID_MASK;
411
412    fwmark.explicitlySelected = true;
413    mask.explicitlySelected = true;
414
415    fwmark.permission = permission;
416    mask.permission = permission;
417
418    return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
419                        fwmark.intValue, mask.intValue, OIF_NONE, uidStart, uidEnd);
420}
421
422// 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;
431
432    fwmark.permission = permission;
433    mask.permission = permission;
434
435    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) {
469    Fwmark fwmark;
470    Fwmark mask;
471
472    fwmark.protectedFromVpn = false;
473    mask.protectedFromVpn = true;
474
475    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
527    for (const std::pair<uid_t, uid_t>& range : uidRanges.getRanges()) {
528        if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.first,
529                                                range.second, add)) {
530            return ret;
531        }
532        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)) {
537            return ret;
538        }
539    }
540
541    if (modifyNonUidBasedRules) {
542        if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
543            return ret;
544        }
545        if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT,
546                                                add)) {
547            return ret;
548        }
549        return modifyVpnSystemPermissionRule(netId, table, add);
550    }
551
552    return 0;
553}
554
555WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
556                                            Permission permission) {
557    uint32_t table = getRouteTableForInterface(interface);
558    if (table == RT_TABLE_UNSPEC) {
559        return -ESRCH;
560    }
561
562    Fwmark fwmark;
563    Fwmark mask;
564
565    fwmark.netId = NETID_UNSET;
566    mask.netId = FWMARK_NET_ID_MASK;
567
568    fwmark.permission = permission;
569    mask.permission = permission;
570
571    return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
572                        mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID);
573}
574
575// Adds or removes an IPv4 or IPv6 route to the specified table and, if it's a directly-connected
576// route, to the main table as well.
577// Returns 0 on success or negative errno on failure.
578WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
579                                   const char* nexthop, RouteController::TableType tableType) {
580    uint32_t table;
581    switch (tableType) {
582        case RouteController::INTERFACE: {
583            table = getRouteTableForInterface(interface);
584            if (table == RT_TABLE_UNSPEC) {
585                return -ESRCH;
586            }
587            break;
588        }
589        case RouteController::LEGACY_NETWORK: {
590            table = RouteController::ROUTE_TABLE_LEGACY_NETWORK;
591            break;
592        }
593        case RouteController::LEGACY_SYSTEM: {
594            table = RouteController::ROUTE_TABLE_LEGACY_SYSTEM;
595            break;
596        }
597    }
598
599    int ret = modifyIpRoute(action, table, interface, destination, nexthop);
600    // We allow apps to call requestRouteToHost() multiple times with the same route, so ignore
601    // EEXIST failures when adding routes to legacy tables.
602    if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST &&
603                 tableType != RouteController::INTERFACE)) {
604        return ret;
605    }
606
607    // If there's no nexthop, this is a directly connected route. Add it to the main table also, to
608    // 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.
614        if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
615            return ret;
616        }
617    }
618
619    return 0;
620}
621
622// Returns 0 on success or negative errno on failure.
623WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
624    uint32_t table = getRouteTableForInterface(interface);
625    if (table == RT_TABLE_UNSPEC) {
626        return -ESRCH;
627    }
628
629    char tableString[UINT32_STRLEN];
630    snprintf(tableString, sizeof(tableString), "%u", table);
631
632    for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
633        const char* argv[] = {
634            IP_PATH,
635            IP_VERSIONS[i],
636            "route",
637            "flush",
638            "table",
639            tableString,
640        };
641        if (android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, false, false)) {
642            ALOGE("failed to flush routes");
643            return -EREMOTEIO;
644        }
645    }
646
647    interfaceToIndex.erase(interface);
648    return 0;
649}
650
651}  // namespace
652
653int RouteController::Init() {
654    if (int ret = AddDirectlyConnectedRule()) {
655        return ret;
656    }
657
658    if (int ret = AddLegacyRouteRules()) {
659        return ret;
660    }
661    // TODO: Enable once we are sure everything works.
662    if (false) {
663        return AddUnreachableRule();
664    }
665    return 0;
666}
667
668int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
669                                                   Permission permission) {
670    return modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD);
671}
672
673int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
674                                                        Permission permission) {
675    if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
676        return ret;
677    }
678    return flushRoutes(interface);
679}
680
681int 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);
685}
686
687int 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)) {
691        return ret;
692    }
693    return flushRoutes(interface);
694}
695
696int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
697                                                     Permission oldPermission,
698                                                     Permission newPermission) {
699    // Add the new rules before deleting the old ones, to avoid race conditions.
700    if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
701        return ret;
702    }
703    return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
704}
705
706int 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);
710}
711
712int 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);
716}
717
718int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
719    return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
720}
721
722int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
723                                                       Permission permission) {
724    return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
725}
726
727int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
728                              TableType tableType) {
729    return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
730}
731
732int RouteController::removeRoute(const char* interface, const char* destination,
733                                 const char* nexthop, TableType tableType) {
734    return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
735}
736