RouteController.cpp revision 96f261e8b28048b8cb48f5a4e81822c73bb813f4
15c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran/*
25c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * Copyright (C) 2014 The Android Open Source Project
35c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran *
45c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * Licensed under the Apache License, Version 2.0 (the "License");
55c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * you may not use this file except in compliance with the License.
65c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * You may obtain a copy of the License at
75c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran *
85c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran *      http://www.apache.org/licenses/LICENSE-2.0
95c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran *
105c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * Unless required by applicable law or agreed to in writing, software
115c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * distributed under the License is distributed on an "AS IS" BASIS,
125c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * See the License for the specific language governing permissions and
145c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran * limitations under the License.
155c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran */
165c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
175c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran#include "RouteController.h"
185c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
195c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran#include "Fwmark.h"
205c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran#include "NetdConstants.h"
215c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
22ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti#include <arpa/inet.h>
23ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti#include <errno.h>
244753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti#include <linux/fib_rules.h>
25ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti#include <linux/netlink.h>
268fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran#include <linux/rtnetlink.h>
275c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran#include <logwrap/logwrap.h>
28a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen#include <map>
29ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti#include <netinet/in.h>
305c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran#include <net/if.h>
31ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti#include <sys/socket.h>
32ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti#include <sys/uio.h>
33ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti#include <unistd.h>
34ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
35ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
36ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
37ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti#define U16_RTA_LENGTH(x) static_cast<uint16_t>(RTA_LENGTH((x)))
385c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
395c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandrannamespace {
405c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
4138b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandranconst uint32_t RULE_PRIORITY_PRIVILEGED_LEGACY     = 11000;
428fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandranconst uint32_t RULE_PRIORITY_PER_NETWORK_EXPLICIT  = 13000;
438fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandranconst uint32_t RULE_PRIORITY_PER_NETWORK_INTERFACE = 14000;
4438b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandranconst uint32_t RULE_PRIORITY_LEGACY                = 16000;
458fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandranconst uint32_t RULE_PRIORITY_PER_NETWORK_NORMAL    = 17000;
468fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandranconst uint32_t RULE_PRIORITY_DEFAULT_NETWORK       = 19000;
478fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandranconst uint32_t RULE_PRIORITY_MAIN                  = 20000;
4856afacf838d24cf8e54d2cf0d8ab9182ab704125Sreeram Ramachandran// TODO: Uncomment once we are sure everything works.
4956afacf838d24cf8e54d2cf0d8ab9182ab704125Sreeram Ramachandran#if 0
508fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandranconst uint32_t RULE_PRIORITY_UNREACHABLE           = 21000;
5156afacf838d24cf8e54d2cf0d8ab9182ab704125Sreeram Ramachandran#endif
525c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
5338b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran// TODO: These should be turned into per-UID tables once the kernel supports UID-based routing.
5438b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandranconst int ROUTE_TABLE_PRIVILEGED_LEGACY = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX - 901;
5538b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandranconst int ROUTE_TABLE_LEGACY            = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX - 902;
5638b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran
574753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitticonst uint16_t kNetlinkRequestFlags = NLM_F_REQUEST | NLM_F_ACK;
584753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitticonst uint16_t kNetlinkCreateRequestFlags = kNetlinkRequestFlags | NLM_F_CREATE | NLM_F_EXCL;
594753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti
60a561e121c724e9163b2e256e15eef660e3a326daPaul Jensenstd::map<std::string, uint32_t> interfaceToIndex;
61a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen
625c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandranuint32_t getRouteTableForInterface(const char* interface) {
63a48118062412f16ae712bfc8c8a539d3b6a85e47Sreeram Ramachandran    uint32_t index = if_nametoindex(interface);
64a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen    if (index) {
65a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen        interfaceToIndex[interface] = index;
66a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen    } else {
67a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen        // If the interface goes away if_nametoindex() will return 0 but we still need to know
68a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen        // the index so we can remove the rules and routes.
69a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen        std::map<std::string, uint32_t>::iterator it = interfaceToIndex.find(interface);
70a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen        if (it != interfaceToIndex.end())
71a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen            index = it->second;
72a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen    }
73a48118062412f16ae712bfc8c8a539d3b6a85e47Sreeram Ramachandran    return index ? index + RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX : 0;
745c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran}
755c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
764753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti// Sends a netlink request and expects an ack.
774753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti// |iov| is an array of struct iovec that contains the netlink message payload.
784753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti// The netlink header is generated by this function based on |action| and |flags|.
794753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti// Returns -errno if there was an error or if the kernel reported an error.
804753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colittiint sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
814753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    nlmsghdr nlmsg = {
824753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        .nlmsg_type = action,
834753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        .nlmsg_flags = flags,
844753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    };
854753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    iov[0].iov_base = &nlmsg;
864753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    iov[0].iov_len = sizeof(nlmsg);
874753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    for (int i = 0; i < iovlen; ++i) {
884753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        nlmsg.nlmsg_len += iov[i].iov_len;
894753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    }
904753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti
914753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    int ret;
924753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    struct {
934753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        nlmsghdr msg;
944753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        nlmsgerr err;
954753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    } response;
964753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti
974753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    sockaddr_nl kernel = {AF_NETLINK, 0, 0, 0};
984753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
994753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    if (sock != -1 &&
1004753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti            connect(sock, reinterpret_cast<sockaddr*>(&kernel), sizeof(kernel)) != -1 &&
1014753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti            writev(sock, iov, iovlen) != -1 &&
1024753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti            (ret = recv(sock, &response, sizeof(response), 0)) != -1) {
1034753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        if (ret == sizeof(response)) {
1044753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti            ret = response.err.error;  // Netlink errors are negative errno.
1054753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        } else {
1064753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti            ret = -EBADMSG;
1074753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        }
1084753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    } else {
1094753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        ret = -errno;
1104753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    }
1114753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti
1124753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    if (sock != -1) {
1134753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        close(sock);
1144753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    }
1154753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti
1164753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    return ret;
1174753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti}
1184753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti
1198fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran// Adds or removes a routing rule for IPv4 and IPv6.
1208fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran//
1218fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the rule
1228fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran//   returns ENETUNREACH.
1238fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
1248fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran//   ignored.
1258fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran// + If |interface| is non-NULL, the rule matches the specified outgoing interface.
12696f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti//
12796f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti// Returns 0 on success or negative errno on failure.
12896f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colittiint modifyIpRule(uint16_t action, uint32_t priority, uint32_t table, uint32_t fwmark, uint32_t mask,
12996f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti                 const char* interface) {
1304753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    // The interface name must include exactly one terminating NULL and be properly padded, or older
1314753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    // kernels will refuse to delete rules.
1324753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    uint8_t padding[RTA_ALIGNTO] = {0, 0, 0, 0};
1334753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    uint16_t paddingLength = 0;
1344753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    size_t interfaceLength = 0;
1354753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    char oifname[IFNAMSIZ];
1364753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    if (interface) {
1374753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        interfaceLength = strlcpy(oifname, interface, IFNAMSIZ) + 1;
1384753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        if (interfaceLength > IFNAMSIZ) {
1394753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti            return -ENAMETOOLONG;
1404753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        }
1414753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        paddingLength = RTA_SPACE(interfaceLength) - RTA_LENGTH(interfaceLength);
1424753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    }
1437619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran
1444753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    // Assemble a rule request and put it in an array of iovec structures.
1454753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    fib_rule_hdr rule = {
1464753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        .action = static_cast<uint8_t>(table ? FR_ACT_TO_TBL : FR_ACT_UNREACHABLE),
1474753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    };
148a10ac3214f6a582b7fdb66acc43c702731e53d81Lorenzo Colitti
1494753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    rtattr fra_priority = { U16_RTA_LENGTH(sizeof(priority)),  FRA_PRIORITY };
1504753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    rtattr fra_table    = { U16_RTA_LENGTH(sizeof(table)),     FRA_TABLE };
1514753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    rtattr fra_fwmark   = { U16_RTA_LENGTH(sizeof(fwmark)),    FRA_FWMARK };
1524753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    rtattr fra_fwmask   = { U16_RTA_LENGTH(sizeof(mask)),      FRA_FWMASK };
1534753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    rtattr fra_oifname  = { U16_RTA_LENGTH(interfaceLength),   FRA_OIFNAME };
154a10ac3214f6a582b7fdb66acc43c702731e53d81Lorenzo Colitti
1554753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    iovec iov[] = {
1564753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { NULL,           0 },
1574753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { &rule,          sizeof(rule) },
1584753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { &fra_priority,  sizeof(fra_priority) },
1594753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { &priority,      sizeof(priority) },
1604753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { &fra_table,     table ? sizeof(fra_table) : 0 },
1614753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { &table,         table ? sizeof(table) : 0 },
1624753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { &fra_fwmark,    mask ? sizeof(fra_fwmark) : 0 },
1634753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { &fwmark,        mask ? sizeof(fwmark) : 0 },
1644753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { &fra_fwmask,    mask ? sizeof(fra_fwmask) : 0 },
1654753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { &mask,          mask ? sizeof(mask) : 0 },
1664753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { &fra_oifname,   interface ? sizeof(fra_oifname) : 0 },
1674753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { oifname,        interfaceLength },
1684753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { padding,        paddingLength },
1694753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    };
1704753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti
1714753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    uint16_t flags = (action == RTM_NEWRULE) ? kNetlinkCreateRequestFlags : kNetlinkRequestFlags;
1724753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    uint8_t family[] = {AF_INET, AF_INET6};
1734753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    for (size_t i = 0; i < ARRAY_SIZE(family); ++i) {
1744753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        rule.family = family[i];
1754753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
1764753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        if (ret) {
17796f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti            return ret;
1785c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran        }
1795c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    }
1805c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
18196f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti    return 0;
1825c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran}
1835c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
184ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti// Adds or deletes an IPv4 or IPv6 route.
185ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti// Returns 0 on success or negative errno on failure.
186ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colittiint modifyIpRoute(uint16_t action, uint32_t table, const char* interface, const char* destination,
187ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti                  const char* nexthop) {
188ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    // At least the destination must be non-null.
189ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    if (!destination) {
190ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        return -EFAULT;
191ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    }
192ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
193ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    // Parse the prefix.
194ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    uint8_t rawAddress[sizeof(in6_addr)];
195ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    uint8_t family, prefixLength;
196ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
197ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti                                &prefixLength);
198ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    if (rawLength < 0) {
199ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        return rawLength;
200ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    }
2017619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran
202ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
203ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        return -ENOBUFS;  // Cannot happen; parsePrefix only supports IPv4 and IPv6.
204ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    }
205ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
206ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    // If an interface was specified, find the ifindex.
207ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    uint32_t ifindex;
208ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    if (interface) {
209ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        ifindex = if_nametoindex(interface);
210ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        if (!ifindex) {
211ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti            return -ENODEV;
2127619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran        }
2137619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran    }
2147619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran
215ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    // If a nexthop was specified, parse it as the same family as the prefix.
216ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    uint8_t rawNexthop[sizeof(in6_addr)];
217ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    if (nexthop && !inet_pton(family, nexthop, rawNexthop)) {
218ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        return -EINVAL;
219ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    }
220ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
2214753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    // Assemble a rtmsg and put it in an array of iovec structures.
222ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    rtmsg rtmsg = {
223ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        .rtm_protocol = RTPROT_STATIC,
224ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        .rtm_type = RTN_UNICAST,
225ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        .rtm_family = family,
226ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        .rtm_dst_len = prefixLength,
227ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    };
2284753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti
2294753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    rtattr rta_table   = { U16_RTA_LENGTH(sizeof(table)),    RTA_TABLE };
2304753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    rtattr rta_oif     = { U16_RTA_LENGTH(sizeof(ifindex)),  RTA_OIF };
2314753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    rtattr rta_dst     = { U16_RTA_LENGTH(rawLength),        RTA_DST };
2324753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    rtattr rta_gateway = { U16_RTA_LENGTH(rawLength),        RTA_GATEWAY };
233ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
234ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    iovec iov[] = {
2354753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        { NULL,          0 },
236ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        { &rtmsg,        sizeof(rtmsg) },
237ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        { &rta_table,    sizeof(rta_table) },
238ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        { &table,        sizeof(table) },
239ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        { &rta_dst,      sizeof(rta_dst) },
240ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        { rawAddress,    static_cast<size_t>(rawLength) },
241ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        { &rta_oif,      interface ? sizeof(rta_oif) : 0 },
2427f972fb1cd3c26af76779a7a3220b9cf5fb63a0aSreeram Ramachandran        { &ifindex,      interface ? sizeof(ifindex) : 0 },
243ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        { &rta_gateway,  nexthop ? sizeof(rta_gateway) : 0 },
244ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti        { rawNexthop,    nexthop ? static_cast<size_t>(rawLength) : 0 },
245ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    };
246ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti
2474753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    uint16_t flags = (action == RTM_NEWROUTE) ? kNetlinkCreateRequestFlags : kNetlinkRequestFlags;
2484753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    return sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
2497619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran}
2507619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran
25196f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colittiint modifyPerNetworkRules(unsigned netId, const char* interface, Permission permission, bool add,
25296f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti                          bool modifyIptables) {
2535c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    uint32_t table = getRouteTableForInterface(interface);
2545c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    if (!table) {
25596f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti        return -ESRCH;
2565c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    }
2575c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
2584753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    uint16_t action = add ? RTM_NEWRULE : RTM_DELRULE;
25996f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti    int ret;
2605c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
261122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    Fwmark fwmark;
262122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    fwmark.permission = permission;
263122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran
264122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    Fwmark mask;
265122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    mask.permission = permission;
2665c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
2675c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    // A rule to route traffic based on a chosen outgoing interface.
2685c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    //
2695c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    // Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already
2705c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    // knows the outgoing interface (typically for link-local communications).
27196f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti    if ((ret = modifyIpRule(action, RULE_PRIORITY_PER_NETWORK_INTERFACE, table, fwmark.intValue,
27296f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti                            mask.intValue, interface)) != 0) {
27396f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti        return ret;
2745c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    }
2755c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
2765c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    // A rule to route traffic based on the chosen network.
2775c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    //
2785c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    // This is for sockets that have not explicitly requested a particular network, but have been
2795c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    // bound to one when they called connect(). This ensures that sockets connected on a particular
2805c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    // network stay on that network even if the default network changes.
281122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    fwmark.netId = netId;
282122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    mask.netId = FWMARK_NET_ID_MASK;
28396f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti    if ((ret = modifyIpRule(action, RULE_PRIORITY_PER_NETWORK_NORMAL, table, fwmark.intValue,
28496f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti                            mask.intValue, NULL)) != 0) {
28596f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti        return ret;
286122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    }
287122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran
288122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    // A rule to route traffic based on an explicitly chosen network.
289122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    //
290122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    // Supports apps that use the multinetwork APIs to restrict their traffic to a network.
291122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    //
292122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    // We don't really need to check the permission bits of the fwmark here, as they would've been
293122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    // checked at the time the netId was set into the fwmark, but we do so to be consistent.
294122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    fwmark.explicitlySelected = true;
295122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    mask.explicitlySelected = true;
29696f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti    if ((ret = modifyIpRule(action, RULE_PRIORITY_PER_NETWORK_EXPLICIT, table, fwmark.intValue,
29796f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti                            mask.intValue, NULL)) != 0) {
29896f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti        return ret;
2995c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    }
3005c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
3015c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    // An iptables rule to mark incoming packets on a network with the netId of the network.
3025c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    //
3035c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    // This is so that the kernel can:
3045c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    // + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors,
305a48118062412f16ae712bfc8c8a539d3b6a85e47Sreeram Ramachandran    //   ping replies).
3065c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    // + Mark sockets that accept connections from this interface so that the connection stays on
3075c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    //   the same interface.
308379bd33f7640e2c4bef902be0ed6cb96378c8c2eSreeram Ramachandran    if (modifyIptables) {
3094753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        const char* iptablesAction = add ? "-A" : "-D";
310379bd33f7640e2c4bef902be0ed6cb96378c8c2eSreeram Ramachandran        char markString[UINT32_HEX_STRLEN];
311379bd33f7640e2c4bef902be0ed6cb96378c8c2eSreeram Ramachandran        snprintf(markString, sizeof(markString), "0x%x", netId);
3124753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti        if (execIptables(V4V6, "-t", "mangle", iptablesAction, "INPUT", "-i", interface,
3134753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti                         "-j", "MARK", "--set-mark", markString, NULL)) {
31496f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti            return -EREMOTEIO;
315379bd33f7640e2c4bef902be0ed6cb96378c8c2eSreeram Ramachandran        }
3165c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran    }
3175c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
31896f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti    return 0;
3195c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran}
3205c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
32196f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colittiint modifyDefaultNetworkRules(const char* interface, Permission permission, uint16_t action) {
3229c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran    uint32_t table = getRouteTableForInterface(interface);
3239c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran    if (!table) {
32496f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti        return -ESRCH;
3259c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran    }
3269c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran
327122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    Fwmark fwmark;
328122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    fwmark.netId = 0;
329122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    fwmark.permission = permission;
330122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran
331122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    Fwmark mask;
332122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    mask.netId = FWMARK_NET_ID_MASK;
333122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    mask.permission = permission;
3349c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran
3354753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
3364753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti                        mask.intValue, NULL);
3377619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran}
3387619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran
339f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti// Adds or removes an IPv4 or IPv6 route to the specified table and, if it's directly-connected
340f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti// route, to the main table as well.
341f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti// Returns 0 on success or negative errno on failure.
342f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colittiint modifyRoute(const char* interface, const char* destination, const char* nexthop,
3434753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti                uint16_t action, RouteController::TableType tableType, unsigned /* uid */) {
34438b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    uint32_t table = 0;
34538b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    switch (tableType) {
34638b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran        case RouteController::INTERFACE: {
34738b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran            table = getRouteTableForInterface(interface);
34838b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran            break;
34938b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran        }
35038b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran        case RouteController::LEGACY: {
35138b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran            // TODO: Use the UID to assign a unique table per UID instead of this fixed table.
35238b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran            table = ROUTE_TABLE_LEGACY;
35338b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran            break;
35438b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran        }
35538b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran        case RouteController::PRIVILEGED_LEGACY: {
35638b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran            // TODO: Use the UID to assign a unique table per UID instead of this fixed table.
35738b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran            table = ROUTE_TABLE_PRIVILEGED_LEGACY;
35838b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran            break;
35938b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran        }
36038b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    }
3617619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran    if (!table) {
362f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti        return -ESRCH;
3639c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran    }
3649c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran
365f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti    int ret = modifyIpRoute(action, table, interface, destination, nexthop);
366f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti    if (ret != 0) {
367f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti        return ret;
368c92133732378aae815120c39edd62a7b4eb773b3Sreeram Ramachandran    }
369c92133732378aae815120c39edd62a7b4eb773b3Sreeram Ramachandran
370c92133732378aae815120c39edd62a7b4eb773b3Sreeram Ramachandran    // If there's no nexthop, this is a directly connected route. Add it to the main table also, to
371f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti    // let the kernel find it when validating nexthops when global routes are added.
372f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti    if (!nexthop) {
373f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti        ret = modifyIpRoute(action, RT_TABLE_MAIN, interface, destination, NULL);
374f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti        // A failure with action == ADD && errno == EEXIST means that the route already exists in
375f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti        // the main table, perhaps because the kernel added it automatically as part of adding the
376f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti        // IP address to the interface. Ignore this, but complain about everything else.
377f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti        if (ret != 0 && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
378f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti            return ret;
379f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti        }
380c92133732378aae815120c39edd62a7b4eb773b3Sreeram Ramachandran    }
381c92133732378aae815120c39edd62a7b4eb773b3Sreeram Ramachandran
382f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti    return 0;
3839c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran}
3849c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran
38592b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandranbool flushRoutes(const char* interface) {
38692b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran    uint32_t table = getRouteTableForInterface(interface);
38792b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran    if (!table) {
38892b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran        return false;
38992b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran    }
390a561e121c724e9163b2e256e15eef660e3a326daPaul Jensen    interfaceToIndex.erase(interface);
39192b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran
392357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti    char tableString[UINT32_STRLEN];
393357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti    snprintf(tableString, sizeof(tableString), "%u", table);
394357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti
395357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti    const char* version[] = {"-4", "-6"};
396357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti    for (size_t i = 0; i < ARRAY_SIZE(version); ++i) {
397357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti        const char* argv[] = {
398357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti            IP_PATH,
399357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti            version[i],
400357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti            "route"
401357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti            "flush",
402357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti            "table",
403357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti            tableString,
404357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti        };
405357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti        int argc = ARRAY_SIZE(argv);
406357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti
407357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti        if (!android_fork_execvp(argc, const_cast<char**>(argv), NULL, false, false)) {
408357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti            return false;
409357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti        }
410357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti    }
411357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti
412357e5629bb4c745296ab40340ec8679372337155Lorenzo Colitti    return true;
41392b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran}
41492b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran
4155c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran}  // namespace
4165c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
4178fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandranvoid RouteController::Init() {
4188fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran    // Add a new rule to look up the 'main' table, with the same selectors as the "default network"
4198fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran    // rule, but with a lower priority. Since the default network rule points to a table with a
4208fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran    // default route, the rule we're adding will never be used for normal routing lookups. However,
4218fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran    // the kernel may fall-through to it to find directly-connected routes when it validates that a
4228fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran    // nexthop (in a route being added) is reachable.
423122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    Fwmark fwmark;
424122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    fwmark.netId = 0;
425122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran
426122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    Fwmark mask;
427122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran    mask.netId = FWMARK_NET_ID_MASK;
428122f581eb16e06c70cbbc40bd40995775075151fSreeram Ramachandran
4294753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_MAIN, RT_TABLE_MAIN, fwmark.intValue, mask.intValue,
4304753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti                 NULL);
4318fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran
43238b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    // Add rules to allow lookup of legacy routes.
43338b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    //
43438b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    // TODO: Remove these once the kernel supports UID-based routing. Instead, add them on demand
43538b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    // when routes are added.
43638b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    fwmark.netId = 0;
43738b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    mask.netId = 0;
43838b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran
43938b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    fwmark.explicitlySelected = false;
44038b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    mask.explicitlySelected = true;
44138b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran
4424753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY, ROUTE_TABLE_LEGACY, fwmark.intValue,
4434753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti                 mask.intValue, NULL);
44438b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran
44538b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    fwmark.permission = PERMISSION_CONNECTIVITY_INTERNAL;
44638b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran    mask.permission = PERMISSION_CONNECTIVITY_INTERNAL;
44738b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran
4484753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_PRIVILEGED_LEGACY, ROUTE_TABLE_PRIVILEGED_LEGACY,
4494753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti                 fwmark.intValue, mask.intValue, NULL);
45038b7af1f2cb9579895465fabc37865f5dadcac25Sreeram Ramachandran
4518fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran// TODO: Uncomment once we are sure everything works.
4528fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran#if 0
4538fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran    // Add a rule to preempt the pre-defined "from all lookup main" rule. This ensures that packets
4548fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran    // that are already marked with a specific NetId don't fall-through to the main table.
4554753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, 0, 0, 0, NULL);
4568fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran#endif
4578fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran}
4588fe9c8e0a2b1c5cd2a34720efaccc641d9ab8fb6Sreeram Ramachandran
45996f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colittiint RouteController::addInterfaceToNetwork(unsigned netId, const char* interface,
46096f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti                                           Permission permission) {
4619c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran    return modifyPerNetworkRules(netId, interface, permission, true, true);
4625c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran}
4635c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran
46496f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colittiint RouteController::removeInterfaceFromNetwork(unsigned netId, const char* interface,
46596f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti                                                Permission permission) {
46692b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran    return modifyPerNetworkRules(netId, interface, permission, false, true) &&
46792b66c4990b4a0ab608aa9c31da946f36085203bSreeram Ramachandran           flushRoutes(interface);
468379bd33f7640e2c4bef902be0ed6cb96378c8c2eSreeram Ramachandran}
469379bd33f7640e2c4bef902be0ed6cb96378c8c2eSreeram Ramachandran
47096f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colittiint RouteController::modifyNetworkPermission(unsigned netId, const char* interface,
47196f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colitti                                             Permission oldPermission, Permission newPermission) {
472379bd33f7640e2c4bef902be0ed6cb96378c8c2eSreeram Ramachandran    // Add the new rules before deleting the old ones, to avoid race conditions.
4739c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran    return modifyPerNetworkRules(netId, interface, newPermission, true, false) &&
4749c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran           modifyPerNetworkRules(netId, interface, oldPermission, false, false);
4759c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran}
4769c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran
47796f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colittiint RouteController::addToDefaultNetwork(const char* interface, Permission permission) {
4784753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    return modifyDefaultNetworkRules(interface, permission, RTM_NEWRULE);
4799c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran}
4809c0d313de6a3157fadd3b52a9927c77216ca435eSreeram Ramachandran
48196f261e8b28048b8cb48f5a4e81822c73bb813f4Lorenzo Colittiint RouteController::removeFromDefaultNetwork(const char* interface, Permission permission) {
4824753afd79e130d5f1c888f549c36b4da92dbe680Lorenzo Colitti    return modifyDefaultNetworkRules(interface, permission, RTM_DELRULE);
4835c181bf8ca0c89bd9e3e6d8e40bac53d0ee7082fSreeram Ramachandran}
4847619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran
485f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colittiint RouteController::addRoute(const char* interface, const char* destination,
486f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti                              const char* nexthop, TableType tableType, unsigned uid) {
487ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    return modifyRoute(interface, destination, nexthop, RTM_NEWROUTE, tableType, uid);
4887619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran}
4897619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran
490f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colittiint RouteController::removeRoute(const char* interface, const char* destination,
491f7fc8eccb0a6a4fbca4cafdf53f5c167c8f1d755Lorenzo Colitti                                 const char* nexthop, TableType tableType, unsigned uid) {
492ba25df989b48f36b784ad39307a49a4fd9c3fd66Lorenzo Colitti    return modifyRoute(interface, destination, nexthop, RTM_DELROUTE, tableType, uid);
4937619e1bbebdfe643c35ee6be4ac054f5255f0706Sreeram Ramachandran}
494