RouteController.cpp revision cf891383224e420d99996f7e63728a7cc902415e
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
26#include <arpa/inet.h>
27#include <linux/fib_rules.h>
28#include <map>
29#include <net/if.h>
30
31namespace {
32
33// BEGIN CONSTANTS --------------------------------------------------------------------------------
34
35const uint32_t RULE_PRIORITY_PRIVILEGED_LEGACY     = 11000;
36const uint32_t RULE_PRIORITY_SECURE_VPN            = 12000;
37const uint32_t RULE_PRIORITY_PER_NETWORK_EXPLICIT  = 13000;
38const uint32_t RULE_PRIORITY_PER_NETWORK_INTERFACE = 14000;
39const uint32_t RULE_PRIORITY_LEGACY                = 16000;
40const uint32_t RULE_PRIORITY_PER_NETWORK_NORMAL    = 17000;
41const uint32_t RULE_PRIORITY_DEFAULT_NETWORK       = 19000;
42const uint32_t RULE_PRIORITY_MAIN                  = 20000;
43// TODO: Uncomment once we are sure everything works.
44#if 0
45const uint32_t RULE_PRIORITY_UNREACHABLE           = 21000;
46#endif
47
48// TODO: These should be turned into per-UID tables once the kernel supports UID-based routing.
49const int ROUTE_TABLE_PRIVILEGED_LEGACY = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX - 901;
50const int ROUTE_TABLE_LEGACY            = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX - 902;
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
71// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
72// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
73constexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
74    return RTA_LENGTH(x);
75}
76
77// These are practically const, but can't be declared so, because they are used to initialize
78// non-const pointers ("void* iov_base") in iovec arrays.
79rtattr FRATTR_PRIORITY  = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
80rtattr FRATTR_TABLE     = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
81rtattr FRATTR_FWMARK    = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
82rtattr FRATTR_FWMASK    = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
83rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)),    FRA_UID_START };
84rtattr FRATTR_UID_END   = { U16_RTA_LENGTH(sizeof(uid_t)),    FRA_UID_END };
85
86rtattr RTATTR_TABLE     = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
87rtattr RTATTR_OIF       = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
88
89uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
90
91// END CONSTANTS ----------------------------------------------------------------------------------
92
93std::map<std::string, uint32_t> interfaceToIndex;
94
95uint32_t getRouteTableForInterface(const char* interface) {
96    uint32_t index = if_nametoindex(interface);
97    if (index) {
98        interfaceToIndex[interface] = index;
99    } else {
100        // If the interface goes away if_nametoindex() will return 0 but we still need to know
101        // the index so we can remove the rules and routes.
102        std::map<std::string, uint32_t>::iterator it = interfaceToIndex.find(interface);
103        if (it != interfaceToIndex.end()) {
104            index = it->second;
105        }
106    }
107    return index ? index + RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX : 0;
108}
109
110// Sends a netlink request and expects an ack.
111// |iov| is an array of struct iovec that contains the netlink message payload.
112// The netlink header is generated by this function based on |action| and |flags|.
113// Returns -errno if there was an error or if the kernel reported an error.
114WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
115    nlmsghdr nlmsg = {
116        .nlmsg_type = action,
117        .nlmsg_flags = flags,
118    };
119    iov[0].iov_base = &nlmsg;
120    iov[0].iov_len = sizeof(nlmsg);
121    for (int i = 0; i < iovlen; ++i) {
122        nlmsg.nlmsg_len += iov[i].iov_len;
123    }
124
125    int ret;
126    struct {
127        nlmsghdr msg;
128        nlmsgerr err;
129    } response;
130
131    int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
132    if (sock != -1 &&
133            connect(sock, reinterpret_cast<const sockaddr*>(&NETLINK_ADDRESS),
134                    sizeof(NETLINK_ADDRESS)) != -1 &&
135            writev(sock, iov, iovlen) != -1 &&
136            (ret = recv(sock, &response, sizeof(response), 0)) != -1) {
137        if (ret == sizeof(response)) {
138            ret = response.err.error;  // Netlink errors are negative errno.
139            if (ret) {
140                ALOGE("netlink response contains error (%s)", strerror(-ret));
141            }
142        } else {
143            ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response));
144            ret = -EBADMSG;
145        }
146    } else {
147        ALOGE("netlink socket/connect/writev/recv failed (%s)", strerror(errno));
148        ret = -errno;
149    }
150
151    if (sock != -1) {
152        close(sock);
153    }
154
155    return ret;
156}
157
158// Adds or removes a routing rule for IPv4 and IPv6.
159//
160// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the rule
161//   returns ENETUNREACH.
162// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
163//   ignored.
164// + If |interface| is non-NULL, the rule matches the specified outgoing interface.
165//
166// Returns 0 on success or negative errno on failure.
167WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
168                                    uint32_t fwmark, uint32_t mask, const char* interface,
169                                    uid_t uidStart, uid_t uidEnd) {
170    // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
171    if (fwmark & ~mask) {
172        ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
173        return -ERANGE;
174    }
175
176    // The interface name must include exactly one terminating NULL and be properly padded, or older
177    // kernels will refuse to delete rules.
178    uint16_t paddingLength = 0;
179    size_t interfaceLength = 0;
180    char oifname[IFNAMSIZ];
181    if (interface) {
182        interfaceLength = strlcpy(oifname, interface, IFNAMSIZ) + 1;
183        if (interfaceLength > IFNAMSIZ) {
184            ALOGE("interface name too long (%zu > %u)", interfaceLength, IFNAMSIZ);
185            return -ENAMETOOLONG;
186        }
187        paddingLength = RTA_SPACE(interfaceLength) - RTA_LENGTH(interfaceLength);
188    }
189
190    // Either both start and end UID must be specified, or neither.
191    if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
192        ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
193        return -EUSERS;
194    }
195    bool isUidRule = (uidStart != INVALID_UID);
196
197    // Assemble a rule request and put it in an array of iovec structures.
198    fib_rule_hdr rule = {
199        .action = static_cast<uint8_t>(table ? FR_ACT_TO_TBL : FR_ACT_UNREACHABLE),
200    };
201
202    rtattr fraOifname = { U16_RTA_LENGTH(interfaceLength), FRA_OIFNAME };
203
204    iovec iov[] = {
205        { NULL,              0 },
206        { &rule,             sizeof(rule) },
207        { &FRATTR_PRIORITY,  sizeof(FRATTR_PRIORITY) },
208        { &priority,         sizeof(priority) },
209        { &FRATTR_TABLE,     table ? sizeof(FRATTR_TABLE) : 0 },
210        { &table,            table ? sizeof(table) : 0 },
211        { &FRATTR_FWMARK,    mask ? sizeof(FRATTR_FWMARK) : 0 },
212        { &fwmark,           mask ? sizeof(fwmark) : 0 },
213        { &FRATTR_FWMASK,    mask ? sizeof(FRATTR_FWMASK) : 0 },
214        { &mask,             mask ? sizeof(mask) : 0 },
215        { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
216        { &uidStart,         isUidRule ? sizeof(uidStart) : 0 },
217        { &FRATTR_UID_END,   isUidRule ? sizeof(FRATTR_UID_END) : 0 },
218        { &uidEnd,           isUidRule ? sizeof(uidEnd) : 0 },
219        { &fraOifname,       interface ? sizeof(fraOifname) : 0 },
220        { oifname,           interfaceLength },
221        { PADDING_BUFFER,    paddingLength },
222    };
223
224    uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
225    for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
226        rule.family = AF_FAMILIES[i];
227        if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov))) {
228            return ret;
229        }
230    }
231
232    return 0;
233}
234
235// Adds or deletes an IPv4 or IPv6 route.
236// Returns 0 on success or negative errno on failure.
237WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
238                                     const char* destination, const char* nexthop) {
239    // At least the destination must be non-null.
240    if (!destination) {
241        ALOGE("null destination");
242        return -EFAULT;
243    }
244
245    // Parse the prefix.
246    uint8_t rawAddress[sizeof(in6_addr)];
247    uint8_t family;
248    uint8_t prefixLength;
249    int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
250                                &prefixLength);
251    if (rawLength < 0) {
252        ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
253        return rawLength;
254    }
255
256    if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
257        ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
258        return -ENOBUFS;  // Cannot happen; parsePrefix only supports IPv4 and IPv6.
259    }
260
261    // If an interface was specified, find the ifindex.
262    uint32_t ifindex;
263    if (interface) {
264        ifindex = if_nametoindex(interface);
265        if (!ifindex) {
266            ALOGE("cannot find interface %s", interface);
267            return -ENODEV;
268        }
269    }
270
271    // If a nexthop was specified, parse it as the same family as the prefix.
272    uint8_t rawNexthop[sizeof(in6_addr)];
273    if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
274        ALOGE("inet_pton failed for nexthop %s", nexthop);
275        return -EINVAL;
276    }
277
278    // Assemble a rtmsg and put it in an array of iovec structures.
279    rtmsg route = {
280        .rtm_protocol = RTPROT_STATIC,
281        .rtm_type = RTN_UNICAST,
282        .rtm_family = family,
283        .rtm_dst_len = prefixLength,
284    };
285
286    rtattr rtaDst     = { U16_RTA_LENGTH(rawLength), RTA_DST };
287    rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
288
289    iovec iov[] = {
290        { NULL,          0 },
291        { &route,        sizeof(route) },
292        { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
293        { &table,        sizeof(table) },
294        { &rtaDst,       sizeof(rtaDst) },
295        { rawAddress,    static_cast<size_t>(rawLength) },
296        { &RTATTR_OIF,   interface ? sizeof(RTATTR_OIF) : 0 },
297        { &ifindex,      interface ? sizeof(ifindex) : 0 },
298        { &rtaGateway,   nexthop ? sizeof(rtaGateway) : 0 },
299        { rawNexthop,    nexthop ? static_cast<size_t>(rawLength) : 0 },
300    };
301
302    uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
303                                                NETLINK_REQUEST_FLAGS;
304    return sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
305}
306
307WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface, bool add) {
308    // An iptables rule to mark incoming packets on a network with the netId of the network.
309    //
310    // This is so that the kernel can:
311    // + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors,
312    //   ping replies).
313    // + Mark sockets that accept connections from this interface so that the connection stays on
314    //   the same interface.
315    char markString[UINT32_HEX_STRLEN];
316    snprintf(markString, sizeof(markString), "0x%x", netId);
317    if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j",
318                     "MARK", "--set-mark", markString, NULL)) {
319        ALOGE("failed to change iptables rule that sets incoming packet mark");
320        return -EREMOTEIO;
321    }
322    return 0;
323}
324
325WARN_UNUSED_RESULT int modifyPerNetworkRules(unsigned netId, const char* interface, uint32_t table,
326                                             Permission permission, uid_t uidStart, uid_t uidEnd,
327                                             bool add, bool modifyIptables) {
328    if (!table) {
329        table = getRouteTableForInterface(interface);
330        if (!table) {
331            ALOGE("cannot find interface %s", interface);
332            return -ESRCH;
333        }
334    }
335
336    uint16_t action = add ? RTM_NEWRULE : RTM_DELRULE;
337
338    Fwmark fwmark;
339    Fwmark mask;
340
341    // A rule to route traffic based on a chosen outgoing interface.
342    //
343    // Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already
344    // knows the outgoing interface (typically for link-local communications).
345    fwmark.permission = permission;
346    mask.permission = permission;
347    if (int ret = modifyIpRule(action, RULE_PRIORITY_PER_NETWORK_INTERFACE, table, fwmark.intValue,
348                               mask.intValue, interface, uidStart, uidEnd)) {
349        return ret;
350    }
351
352    // A rule to route traffic based on the chosen network.
353    //
354    // This is for sockets that have not explicitly requested a particular network, but have been
355    // bound to one when they called connect(). This ensures that sockets connected on a particular
356    // network stay on that network even if the default network changes.
357    fwmark.netId = netId;
358    mask.netId = FWMARK_NET_ID_MASK;
359    if (int ret = modifyIpRule(action, RULE_PRIORITY_PER_NETWORK_NORMAL, table, fwmark.intValue,
360                               mask.intValue, NULL, uidStart, uidEnd)) {
361        return ret;
362    }
363
364    // A rule to route traffic based on an explicitly chosen network.
365    //
366    // Supports apps that use the multinetwork APIs to restrict their traffic to a network.
367    //
368    // Even though we check permissions at the time we set a netId into the fwmark of a socket, we
369    // still need to check it again in the rules here, because a network's permissions may have been
370    // updated via modifyNetworkPermission().
371    fwmark.explicitlySelected = true;
372    mask.explicitlySelected = true;
373    if (int ret = modifyIpRule(action, RULE_PRIORITY_PER_NETWORK_EXPLICIT, table, fwmark.intValue,
374                               mask.intValue, NULL, uidStart, uidEnd)) {
375        return ret;
376    }
377
378    if (modifyIptables) {
379        if (int ret = modifyIncomingPacketMark(netId, interface, add)) {
380            return ret;
381        }
382    }
383
384    return 0;
385}
386
387// Adds or removes rules for VPNs that affect UIDs in |uidRanges|. If |modifyInterfaceBasedRules|
388// is true, also modifies the rules that are based only on the |interface| and not on |uidRanges|.
389// When adding or removing an interface to the VPN, set it to true. When adding or removing UIDs
390// without changing the VPN's interfaces, set it to false.
391WARN_UNUSED_RESULT int modifyVpnRules(unsigned netId, const char* interface,
392                                      const UidRanges& uidRanges, bool add,
393                                      bool modifyInterfaceBasedRules) {
394    uint32_t table = getRouteTableForInterface(interface);
395    if (!table) {
396        ALOGE("cannot find interface %s", interface);
397        return -ESRCH;
398    }
399
400    uint16_t action = add ? RTM_NEWRULE : RTM_DELRULE;
401
402    Fwmark fwmark;
403    Fwmark mask;
404
405    fwmark.protectedFromVpn = false;
406    mask.protectedFromVpn = true;
407
408    for (const std::pair<uid_t, uid_t>& range : uidRanges.getRanges()) {
409        if (int ret = modifyPerNetworkRules(netId, interface, table, PERMISSION_NONE, range.first,
410                                            range.second, add, false)) {
411            return ret;
412        }
413
414        // A rule to route all traffic from a given set of UIDs to go over the VPN.
415        //
416        // Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket
417        // may have, if they are subject to this VPN, their traffic has to go through it. Allows the
418        // traffic to bypass the VPN if the protectedFromVpn bit is set.
419        if (int ret = modifyIpRule(action, RULE_PRIORITY_SECURE_VPN, table, fwmark.intValue,
420                                   mask.intValue, NULL, range.first, range.second)) {
421            return ret;
422        }
423    }
424
425    if (modifyInterfaceBasedRules) {
426        if (int ret = modifyIncomingPacketMark(netId, interface, add)) {
427            return ret;
428        }
429
430        // A rule to allow privileged apps to send traffic over this VPN even if they are not part
431        // of the target set of UIDs.
432        //
433        // This is needed for DnsProxyListener to correctly resolve a request for a user who is in
434        // the target set, but where the DnsProxyListener itself is not.
435        fwmark.protectedFromVpn = false;
436        mask.protectedFromVpn = false;
437
438        fwmark.netId = netId;
439        mask.netId = FWMARK_NET_ID_MASK;
440
441        fwmark.permission = PERMISSION_CONNECTIVITY_INTERNAL;
442        mask.permission = PERMISSION_CONNECTIVITY_INTERNAL;
443
444        if (int ret = modifyIpRule(action, RULE_PRIORITY_SECURE_VPN, table, fwmark.intValue,
445                                   mask.intValue, NULL, INVALID_UID, INVALID_UID)) {
446            return ret;
447        }
448    }
449
450    return 0;
451}
452
453WARN_UNUSED_RESULT int modifyDefaultNetworkRules(const char* interface, Permission permission,
454                                                 uint16_t action) {
455    uint32_t table = getRouteTableForInterface(interface);
456    if (!table) {
457        ALOGE("cannot find interface %s", interface);
458        return -ESRCH;
459    }
460
461    Fwmark fwmark;
462    Fwmark mask;
463
464    fwmark.netId = 0;
465    mask.netId = FWMARK_NET_ID_MASK;
466
467    fwmark.permission = permission;
468    mask.permission = permission;
469
470    return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
471                        mask.intValue, NULL, INVALID_UID, INVALID_UID);
472}
473
474// Adds or removes an IPv4 or IPv6 route to the specified table and, if it's a directly-connected
475// route, to the main table as well.
476// Returns 0 on success or negative errno on failure.
477WARN_UNUSED_RESULT int modifyRoute(const char* interface, const char* destination,
478                                   const char* nexthop, uint16_t action,
479                                   RouteController::TableType tableType, uid_t /*uid*/) {
480    uint32_t table = 0;
481    switch (tableType) {
482        case RouteController::INTERFACE: {
483            table = getRouteTableForInterface(interface);
484            break;
485        }
486        case RouteController::LEGACY: {
487            // TODO: Use the UID to assign a unique table per UID instead of this fixed table.
488            table = ROUTE_TABLE_LEGACY;
489            break;
490        }
491        case RouteController::PRIVILEGED_LEGACY: {
492            // TODO: Use the UID to assign a unique table per UID instead of this fixed table.
493            table = ROUTE_TABLE_PRIVILEGED_LEGACY;
494            break;
495        }
496    }
497    if (!table) {
498        ALOGE("cannot find table for interface %s and tableType %d", interface, tableType);
499        return -ESRCH;
500    }
501
502    int ret = modifyIpRoute(action, table, interface, destination, nexthop);
503    // We allow apps to call requestRouteToHost() multiple times with the same route, so ignore
504    // EEXIST failures when adding routes to legacy tables.
505    if (ret != 0 && !(action == RTM_NEWROUTE && ret == -EEXIST &&
506                      (tableType == RouteController::LEGACY ||
507                       tableType == RouteController::PRIVILEGED_LEGACY))) {
508        return ret;
509    }
510
511    // If there's no nexthop, this is a directly connected route. Add it to the main table also, to
512    // let the kernel find it when validating nexthops when global routes are added.
513    if (!nexthop) {
514        ret = modifyIpRoute(action, RT_TABLE_MAIN, interface, destination, NULL);
515        // A failure with action == ADD && errno == EEXIST means that the route already exists in
516        // the main table, perhaps because the kernel added it automatically as part of adding the
517        // IP address to the interface. Ignore this, but complain about everything else.
518        if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
519            return ret;
520        }
521    }
522
523    return 0;
524}
525
526// Returns 0 on success or negative errno on failure.
527WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
528    uint32_t table = getRouteTableForInterface(interface);
529    if (!table) {
530        ALOGE("cannot find interface %s", interface);
531        return -ESRCH;
532    }
533    interfaceToIndex.erase(interface);
534
535    char tableString[UINT32_STRLEN];
536    snprintf(tableString, sizeof(tableString), "%u", table);
537
538    for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
539        const char* argv[] = {
540            IP_PATH,
541            IP_VERSIONS[i],
542            "route"
543            "flush",
544            "table",
545            tableString,
546        };
547        if (android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, false, false)) {
548            ALOGE("failed to flush routes");
549            return -EREMOTEIO;
550        }
551    }
552
553    return 0;
554}
555
556}  // namespace
557
558int RouteController::Init() {
559    Fwmark fwmark;
560    Fwmark mask;
561
562    // Add a new rule to look up the 'main' table, with the same selectors as the "default network"
563    // rule, but with a lower priority. Since the default network rule points to a table with a
564    // default route, the rule we're adding will never be used for normal routing lookups. However,
565    // the kernel may fall-through to it to find directly-connected routes when it validates that a
566    // nexthop (in a route being added) is reachable.
567    //
568    // TODO: This isn't true if the default network requires non-zero permissions. In that case, an
569    // app without those permissions may still be able to access directly-connected routes, since
570    // it won't match the default network rule. Fix this by only allowing the root UID (as a proxy
571    // for the kernel) to lookup this main table rule.
572    fwmark.netId = 0;
573    mask.netId = FWMARK_NET_ID_MASK;
574    if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_MAIN, RT_TABLE_MAIN, fwmark.intValue,
575                               mask.intValue, NULL, INVALID_UID, INVALID_UID)) {
576        return ret;
577    }
578
579    // Add rules to allow lookup of legacy routes.
580    //
581    // TODO: Remove these once the kernel supports UID-based routing. Instead, add them on demand
582    // when routes are added.
583    fwmark.netId = 0;
584    mask.netId = 0;
585
586    fwmark.explicitlySelected = false;
587    mask.explicitlySelected = true;
588    if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY, ROUTE_TABLE_LEGACY,
589                               fwmark.intValue, mask.intValue, NULL, INVALID_UID, INVALID_UID)) {
590        return ret;
591    }
592
593    fwmark.permission = PERMISSION_CONNECTIVITY_INTERNAL;
594    mask.permission = PERMISSION_CONNECTIVITY_INTERNAL;
595
596    if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_PRIVILEGED_LEGACY,
597                               ROUTE_TABLE_PRIVILEGED_LEGACY, fwmark.intValue, mask.intValue, NULL,
598                               INVALID_UID, INVALID_UID)) {
599        return ret;
600    }
601
602// TODO: Uncomment once we are sure everything works.
603#if 0
604    // Add a rule to preempt the pre-defined "from all lookup main" rule. This ensures that packets
605    // that are already marked with a specific NetId don't fall-through to the main table.
606    return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, 0, 0, 0, NULL, INVALID_UID,
607                        INVALID_UID);
608#else
609    return 0;
610#endif
611}
612
613int RouteController::addInterfaceToNetwork(unsigned netId, const char* interface,
614                                           Permission permission) {
615    return modifyPerNetworkRules(netId, interface, 0, permission, INVALID_UID, INVALID_UID, true,
616                                 true);
617}
618
619int RouteController::removeInterfaceFromNetwork(unsigned netId, const char* interface,
620                                                Permission permission) {
621    if (int ret = modifyPerNetworkRules(netId, interface, 0, permission, INVALID_UID, INVALID_UID,
622                                        false, true)) {
623        return ret;
624    }
625    return flushRoutes(interface);
626}
627
628int RouteController::addInterfaceToVpn(unsigned netId, const char* interface,
629                                       const UidRanges& uidRanges) {
630    return modifyVpnRules(netId, interface, uidRanges, true, true);
631}
632
633int RouteController::removeInterfaceFromVpn(unsigned netId, const char* interface,
634                                            const UidRanges& uidRanges) {
635    if (int ret = modifyVpnRules(netId, interface, uidRanges, false, true)) {
636        return ret;
637    }
638    return flushRoutes(interface);
639}
640
641int RouteController::modifyNetworkPermission(unsigned netId, const char* interface,
642                                             Permission oldPermission, Permission newPermission) {
643    // Add the new rules before deleting the old ones, to avoid race conditions.
644    if (int ret = modifyPerNetworkRules(netId, interface, 0, newPermission, INVALID_UID,
645                                        INVALID_UID, true, false)) {
646        return ret;
647    }
648    return modifyPerNetworkRules(netId, interface, 0, oldPermission, INVALID_UID, INVALID_UID,
649                                 false, false);
650}
651
652int RouteController::addUsersToVpn(unsigned netId, const char* interface,
653                                   const UidRanges& uidRanges) {
654    return modifyVpnRules(netId, interface, uidRanges, true, false);
655}
656
657int RouteController::removeUsersFromVpn(unsigned netId, const char* interface,
658                                        const UidRanges& uidRanges) {
659    return modifyVpnRules(netId, interface, uidRanges, false, false);
660}
661
662int RouteController::addToDefaultNetwork(const char* interface, Permission permission) {
663    return modifyDefaultNetworkRules(interface, permission, RTM_NEWRULE);
664}
665
666int RouteController::removeFromDefaultNetwork(const char* interface, Permission permission) {
667    return modifyDefaultNetworkRules(interface, permission, RTM_DELRULE);
668}
669
670int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
671                              TableType tableType, uid_t uid) {
672    return modifyRoute(interface, destination, nexthop, RTM_NEWROUTE, tableType, uid);
673}
674
675int RouteController::removeRoute(const char* interface, const char* destination,
676                                 const char* nexthop, TableType tableType, uid_t uid) {
677    return modifyRoute(interface, destination, nexthop, RTM_DELROUTE, tableType, uid);
678}
679