RouteController.cpp revision ed4bd1f7d219f9f5f56763ea02cf4947e78397f6
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_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;
44// TODO: Uncomment once we are sure everything works.
45#if 0
46const uint32_t RULE_PRIORITY_UNREACHABLE        = 23000;
47#endif
48
49// 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
59const 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;
61
62const 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
90std::map<std::string, uint32_t> interfaceToIndex;
91
92uint32_t getRouteTableForInterface(const char* interface) {
93    uint32_t index = if_nametoindex(interface);
94    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);
100        if (it != interfaceToIndex.end()) {
101            index = it->second;
102        }
103    }
104    return index ? index + RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX : 0;
105}
106
107// 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.
111WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
112    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
128    int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
129    if (sock != -1 &&
130            connect(sock, reinterpret_cast<const sockaddr*>(&NETLINK_ADDRESS),
131                    sizeof(NETLINK_ADDRESS)) != -1 &&
132            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.
136            if (ret) {
137                ALOGE("netlink response contains error (%s)", strerror(-ret));
138            }
139        } else {
140            ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response));
141            ret = -EBADMSG;
142        }
143    } else {
144        ALOGE("netlink socket/connect/writev/recv failed (%s)", strerror(errno));
145        ret = -errno;
146    }
147
148    if (sock != -1) {
149        close(sock);
150    }
151
152    return ret;
153}
154
155// 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.
162//
163// Returns 0 on success or negative errno on failure.
164WARN_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
173    // The interface name must include exactly one terminating NULL and be properly padded, or older
174    // kernels will refuse to delete rules.
175    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) {
181            ALOGE("interface name too long (%zu > %u)", interfaceLength, IFNAMSIZ);
182            return -ENAMETOOLONG;
183        }
184        paddingLength = RTA_SPACE(interfaceLength) - RTA_LENGTH(interfaceLength);
185    }
186
187    // Either both start and end UID must be specified, or neither.
188    if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
189        ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
190        return -EUSERS;
191    }
192    bool isUidRule = (uidStart != INVALID_UID);
193
194    // 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
199    rtattr fraOifname = { U16_RTA_LENGTH(interfaceLength), FRA_OIFNAME };
200
201    iovec iov[] = {
202        { 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 },
219    };
220
221    uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
222    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))) {
225            return ret;
226        }
227    }
228
229    return 0;
230}
231
232// Adds or deletes an IPv4 or IPv6 route.
233// Returns 0 on success or negative errno on failure.
234WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
235                                     const char* destination, const char* nexthop) {
236    // At least the destination must be non-null.
237    if (!destination) {
238        ALOGE("null destination");
239        return -EFAULT;
240    }
241
242    // Parse the prefix.
243    uint8_t rawAddress[sizeof(in6_addr)];
244    uint8_t family;
245    uint8_t prefixLength;
246    int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
247                                &prefixLength);
248    if (rawLength < 0) {
249        ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
250        return rawLength;
251    }
252
253    if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
254        ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
255        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) {
263            ALOGE("cannot find interface %s", interface);
264            return -ENODEV;
265        }
266    }
267
268    // If a nexthop was specified, parse it as the same family as the prefix.
269    uint8_t rawNexthop[sizeof(in6_addr)];
270    if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
271        ALOGE("inet_pton failed for nexthop %s", nexthop);
272        return -EINVAL;
273    }
274
275    // Assemble a rtmsg and put it in an array of iovec structures.
276    rtmsg route = {
277        .rtm_protocol = RTPROT_STATIC,
278        .rtm_type = RTN_UNICAST,
279        .rtm_family = family,
280        .rtm_dst_len = prefixLength,
281    };
282
283    rtattr rtaDst     = { U16_RTA_LENGTH(rawLength), RTA_DST };
284    rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
285
286    iovec iov[] = {
287        { 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 },
297    };
298
299    uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
300                                                NETLINK_REQUEST_FLAGS;
301    return sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
302}
303
304WARN_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,
309    //   ping replies, SYN-ACKs, etc).
310    // + 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
322// 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
334WARN_UNUSED_RESULT int modifyPerNetworkRules(unsigned netId, const char* interface, uint32_t table,
335                                             Permission permission, uid_t uidStart, uid_t uidEnd,
336                                             bool add, bool modifyInterfaceBasedRules) {
337    if (!table) {
338        table = getRouteTableForInterface(interface);
339        if (!table) {
340            ALOGE("cannot find interface %s", interface);
341            return -ESRCH;
342        }
343    }
344
345    uint16_t action = add ? RTM_NEWRULE : RTM_DELRULE;
346
347    Fwmark fwmark;
348    Fwmark mask;
349
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).
354    fwmark.permission = permission;
355    mask.permission = permission;
356    if (int ret = modifyIpRule(action, RULE_PRIORITY_OUTPUT_INTERFACE, table, fwmark.intValue,
357                               mask.intValue, interface, uidStart, uidEnd)) {
358        return ret;
359    }
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.
366    fwmark.netId = netId;
367    mask.netId = FWMARK_NET_ID_MASK;
368    if (int ret = modifyIpRule(action, RULE_PRIORITY_IMPLICIT_NETWORK, table, fwmark.intValue,
369                               mask.intValue, NULL, uidStart, uidEnd)) {
370        return ret;
371    }
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    //
377    // 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().
380    fwmark.explicitlySelected = true;
381    mask.explicitlySelected = true;
382    if (int ret = modifyIpRule(action, RULE_PRIORITY_EXPLICIT_NETWORK, table, fwmark.intValue,
383                               mask.intValue, NULL, uidStart, uidEnd)) {
384        return ret;
385    }
386
387    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) {
395        if (int ret = modifyIncomingPacketMark(netId, interface, add)) {
396            return ret;
397        }
398    }
399
400    return 0;
401}
402
403// 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) {
410    uint32_t table = getRouteTableForInterface(interface);
411    if (!table) {
412        ALOGE("cannot find interface %s", interface);
413        return -ESRCH;
414    }
415
416    uint16_t action = add ? RTM_NEWRULE : RTM_DELRULE;
417
418    Fwmark fwmark;
419    Fwmark mask;
420
421    fwmark.protectedFromVpn = false;
422    mask.protectedFromVpn = true;
423
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        }
439    }
440
441    if (modifyInterfaceBasedRules) {
442        if (int ret = modifyKernelAccessRule(action, netId, RULE_PRIORITY_IMPLICIT_NETWORK,
443                                             table)) {
444            return ret;
445        }
446
447        if (int ret = modifyIncomingPacketMark(netId, interface, add)) {
448            return ret;
449        }
450
451        // 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;
458
459        fwmark.netId = netId;
460        mask.netId = FWMARK_NET_ID_MASK;
461
462        fwmark.permission = PERMISSION_SYSTEM;
463        mask.permission = PERMISSION_SYSTEM;
464
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;
472}
473
474WARN_UNUSED_RESULT int modifyDefaultNetworkRules(uint16_t action, const char* interface,
475                                                 Permission permission) {
476    uint32_t table = getRouteTableForInterface(interface);
477    if (!table) {
478        ALOGE("cannot find interface %s", interface);
479        return -ESRCH;
480    }
481
482    Fwmark fwmark;
483    Fwmark mask;
484
485    fwmark.netId = 0;
486    mask.netId = FWMARK_NET_ID_MASK;
487
488    fwmark.permission = permission;
489    mask.permission = permission;
490
491    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;
503}
504
505// Adds or removes an IPv4 or IPv6 route to the specified table and, if it's a directly-connected
506// route, to the main table as well.
507// Returns 0 on success or negative errno on failure.
508WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
509                                   const char* nexthop, RouteController::TableType tableType) {
510    uint32_t table = 0;
511    switch (tableType) {
512        case RouteController::INTERFACE: {
513            table = getRouteTableForInterface(interface);
514            break;
515        }
516        case RouteController::LEGACY: {
517            table = RouteController::ROUTE_TABLE_LEGACY;
518            break;
519        }
520        case RouteController::PRIVILEGED_LEGACY: {
521            table = RouteController::ROUTE_TABLE_PRIVILEGED_LEGACY;
522            break;
523        }
524    }
525    if (!table) {
526        ALOGE("cannot find table for interface %s and tableType %d", interface, tableType);
527        return -ESRCH;
528    }
529
530    int ret = modifyIpRoute(action, table, interface, destination, nexthop);
531    // 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))) {
536        return ret;
537    }
538
539    // If there's no nexthop, this is a directly connected route. Add it to the main table also, to
540    // 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.
546        if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
547            return ret;
548        }
549    }
550
551    return 0;
552}
553
554// Returns 0 on success or negative errno on failure.
555WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
556    uint32_t table = getRouteTableForInterface(interface);
557    if (!table) {
558        ALOGE("cannot find interface %s", interface);
559        return -ESRCH;
560    }
561    interfaceToIndex.erase(interface);
562
563    char tableString[UINT32_STRLEN];
564    snprintf(tableString, sizeof(tableString), "%u", table);
565
566    for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
567        const char* argv[] = {
568            IP_PATH,
569            IP_VERSIONS[i],
570            "route",
571            "flush",
572            "table",
573            tableString,
574        };
575        if (android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, false, false)) {
576            ALOGE("failed to flush routes");
577            return -EREMOTEIO;
578        }
579    }
580
581    return 0;
582}
583
584}  // namespace
585
586int RouteController::Init() {
587    // 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.
592    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,
606                               mask.intValue, NULL, INVALID_UID, INVALID_UID)) {
607        return ret;
608    }
609
610    if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY,
611                               RouteController::ROUTE_TABLE_LEGACY, fwmark.intValue, mask.intValue,
612                               NULL, INVALID_UID, INVALID_UID)) {
613        return ret;
614    }
615
616    // Add a rule to allow legacy routes from system apps to override VPNs.
617    fwmark.permission = PERMISSION_SYSTEM;
618    mask.permission = PERMISSION_SYSTEM;
619
620    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)) {
623        return ret;
624    }
625
626// TODO: Uncomment once we are sure everything works.
627#if 0
628    // 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.
630    return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, 0, 0, 0, NULL, INVALID_UID,
631                        INVALID_UID);
632#else
633    return 0;
634#endif
635}
636
637int RouteController::addInterfaceToNetwork(unsigned netId, const char* interface,
638                                           Permission permission) {
639    return modifyPerNetworkRules(netId, interface, 0, permission, INVALID_UID, INVALID_UID, true,
640                                 true);
641}
642
643int RouteController::removeInterfaceFromNetwork(unsigned netId, const char* interface,
644                                                Permission permission) {
645    if (int ret = modifyPerNetworkRules(netId, interface, 0, permission, INVALID_UID, INVALID_UID,
646                                        false, true)) {
647        return ret;
648    }
649    return flushRoutes(interface);
650}
651
652int RouteController::addInterfaceToVpn(unsigned netId, const char* interface,
653                                       const UidRanges& uidRanges) {
654    return modifyVpnRules(netId, interface, uidRanges, true, true);
655}
656
657int RouteController::removeInterfaceFromVpn(unsigned netId, const char* interface,
658                                            const UidRanges& uidRanges) {
659    if (int ret = modifyVpnRules(netId, interface, uidRanges, false, true)) {
660        return ret;
661    }
662    return flushRoutes(interface);
663}
664
665int RouteController::modifyNetworkPermission(unsigned netId, const char* interface,
666                                             Permission oldPermission, Permission newPermission) {
667    // Add the new rules before deleting the old ones, to avoid race conditions.
668    if (int ret = modifyPerNetworkRules(netId, interface, 0, newPermission, INVALID_UID,
669                                        INVALID_UID, true, false)) {
670        return ret;
671    }
672    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);
684}
685
686int RouteController::addToDefaultNetwork(const char* interface, Permission permission) {
687    return modifyDefaultNetworkRules(RTM_NEWRULE, interface, permission);
688}
689
690int RouteController::removeFromDefaultNetwork(const char* interface, Permission permission) {
691    return modifyDefaultNetworkRules(RTM_DELRULE, interface, permission);
692}
693
694int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
695                              TableType tableType) {
696    return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
697}
698
699int RouteController::removeRoute(const char* interface, const char* destination,
700                                 const char* nexthop, TableType tableType) {
701    return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
702}
703