RouteController.cpp revision 95684ba176a9fe5ea59207d7202e47fa12bbfdbe
169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/*
269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Copyright (C) 2014 The Android Open Source Project
369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Licensed under the Apache License, Version 2.0 (the "License");
569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * you may not use this file except in compliance with the License.
669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * You may obtain a copy of the License at
769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *      http://www.apache.org/licenses/LICENSE-2.0
969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal *
1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * Unless required by applicable law or agreed to in writing, software
1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * distributed under the License is distributed on an "AS IS" BASIS,
1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * See the License for the specific language governing permissions and
1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * limitations under the License.
1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */
1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include "RouteController.h"
1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include "Fwmark.h"
2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include "UidRanges.h"
2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#define LOG_TAG "Netd"
2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include "log/log.h"
2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include "logwrap/logwrap.h"
2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include "resolv_netid.h"
2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include <arpa/inet.h>
2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include <fcntl.h>
2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include <linux/fib_rules.h>
3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include <map>
3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include <net/if.h>
3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal#include <sys/stat.h>
3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalnamespace {
3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// BEGIN CONSTANTS --------------------------------------------------------------------------------
3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000;
3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL = 11000;
4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_SECURE_VPN          = 12000;
4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_EXPLICIT_NETWORK    = 13000;
4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_OUTPUT_INTERFACE    = 14000;
4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_LEGACY_SYSTEM       = 15000;
4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_LEGACY_NETWORK      = 16000;
4569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_LOCAL_NETWORK       = 17000;
4669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_TETHERING           = 18000;
4769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_IMPLICIT_NETWORK    = 19000;
4869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_BYPASSABLE_VPN      = 20000;
4969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH     = 21000;
5069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_DEFAULT_NETWORK     = 22000;
5169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED  = 23000;
5269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t RULE_PRIORITY_UNREACHABLE         = 24000;
5369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t ROUTE_TABLE_LOCAL_NETWORK  = 97;
5569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98;
5669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint32_t ROUTE_TABLE_LEGACY_SYSTEM  = 99;
5769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
5869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst char* const ROUTE_TABLE_NAME_LOCAL_NETWORK  = "local_network";
5969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network";
6069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM  = "legacy_system";
6169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst char* const ROUTE_TABLE_NAME_LOCAL = "local";
6369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst char* const ROUTE_TABLE_NAME_MAIN  = "main";
6469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
6569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// TODO: These values aren't defined by the Linux kernel, because our UID routing changes are not
6669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// upstream (yet?), so we can't just pick them up from kernel headers. When (if?) the changes make
6769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// it upstream, we'll remove this and rely on the kernel header values. For now, add a static assert
6869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// that will warn us if upstream has given these values some other meaning.
6969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint16_t FRA_UID_START = 18;
7069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint16_t FRA_UID_END   = 19;
7169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalstatic_assert(FRA_UID_START > FRA_MAX,
7269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal             "Android-specific FRA_UID_{START,END} values also assigned in Linux uapi. "
7369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal             "Check that these values match what the kernel does and then update this assertion.");
7469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK;
7669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint16_t NETLINK_CREATE_REQUEST_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE | NLM_F_EXCL;
7769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
7869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst sockaddr_nl NETLINK_ADDRESS = {AF_NETLINK, 0, 0, 0};
7969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6};
8169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst char* const IP_VERSIONS[] = {"-4", "-6"};
8369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
8469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst uid_t UID_ROOT = 0;
8569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst char* const IIF_NONE = NULL;
8669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst char* const OIF_NONE = NULL;
8769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst bool ACTION_ADD = true;
8869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst bool ACTION_DEL = false;
8969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst bool MODIFY_NON_UID_BASED_RULES = true;
9069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
9169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
9269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst int RT_TABLES_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
9369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconst mode_t RT_TABLES_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;  // mode 0644, rw-r--r--
9469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
9569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'"
9669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t).
9769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalconstexpr uint16_t U16_RTA_LENGTH(uint16_t x) {
9869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    return RTA_LENGTH(x);
9969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
10069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
10169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// These are practically const, but can't be declared so, because they are used to initialize
10269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// non-const pointers ("void* iov_base") in iovec arrays.
10369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalrtattr FRATTR_PRIORITY  = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY };
10469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalrtattr FRATTR_TABLE     = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE };
10569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalrtattr FRATTR_FWMARK    = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK };
10669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalrtattr FRATTR_FWMASK    = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK };
10769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalrtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)),    FRA_UID_START };
10869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalrtattr FRATTR_UID_END   = { U16_RTA_LENGTH(sizeof(uid_t)),    FRA_UID_END };
10969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalrtattr RTATTR_TABLE     = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE };
11169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalrtattr RTATTR_OIF       = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF };
11269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigaluint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0};
11469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// END CONSTANTS ----------------------------------------------------------------------------------
11669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
11769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// No locks needed because RouteController is accessed only from one thread (in CommandListener).
11869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalstd::map<std::string, uint32_t> interfaceToTable;
11969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
12069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigaluint32_t getRouteTableForInterface(const char* interface) {
12169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    uint32_t index = if_nametoindex(interface);
12269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    if (index) {
12369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX;
12469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        interfaceToTable[interface] = index;
12569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return index;
12669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
12769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    // If the interface goes away if_nametoindex() will return 0 but we still need to know
12869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    // the index so we can remove the rules and routes.
12969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    auto iter = interfaceToTable.find(interface);
13069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    if (iter == interfaceToTable.end()) {
13169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ALOGE("cannot find interface %s", interface);
13269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return RT_TABLE_UNSPEC;
13369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
13469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    return iter->second;
13569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
13669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
13769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalvoid addTableName(uint32_t table, const std::string& name, std::string* contents) {
13869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    char tableString[UINT32_STRLEN];
13969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    snprintf(tableString, sizeof(tableString), "%u", table);
14069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    *contents += tableString;
14169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    *contents += " ";
14269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    *contents += name;
14369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    *contents += "\n";
14469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
14569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
14669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// Doesn't return success/failure as the file is optional; it's okay if we fail to update it.
14769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalvoid updateTableNamesFile() {
14869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    std::string contents;
14969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
15069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents);
15169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    addTableName(RT_TABLE_MAIN,  ROUTE_TABLE_NAME_MAIN,  &contents);
15269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
15369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    addTableName(ROUTE_TABLE_LOCAL_NETWORK,  ROUTE_TABLE_NAME_LOCAL_NETWORK,  &contents);
15469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents);
15569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    addTableName(ROUTE_TABLE_LEGACY_SYSTEM,  ROUTE_TABLE_NAME_LEGACY_SYSTEM,  &contents);
15669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
15769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    for (const auto& entry : interfaceToTable) {
15869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        addTableName(entry.second, entry.first, &contents);
15969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
16069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
16169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    int fd = open(RT_TABLES_PATH, RT_TABLES_FLAGS, RT_TABLES_MODE);
16269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    if (fd == -1) {
16369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ALOGE("failed to create %s (%s)", RT_TABLES_PATH, strerror(errno));
16469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return;
16569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
16669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    // File creation is affected by umask, so make sure the right mode bits are set.
16769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    if (fchmod(fd, RT_TABLES_MODE) == -1) {
16869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ALOGE("failed to set mode 0%o on %s (%s)", RT_TABLES_MODE, RT_TABLES_PATH, strerror(errno));
16969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
17069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    ssize_t bytesWritten = write(fd, contents.data(), contents.size());
17169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    if (bytesWritten != static_cast<ssize_t>(contents.size())) {
17269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ALOGE("failed to write to %s (%zd vs %zu bytes) (%s)", RT_TABLES_PATH, bytesWritten,
17369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal              contents.size(), strerror(errno));
17469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
17569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    close(fd);
17669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
17769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
17869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// Sends a netlink request and expects an ack.
17969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// |iov| is an array of struct iovec that contains the netlink message payload.
18069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// The netlink header is generated by this function based on |action| and |flags|.
18169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// Returns -errno if there was an error or if the kernel reported an error.
18269e17611504376e4d4603925f8528dfc890fd2c6Luis SigalWARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) {
18369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    nlmsghdr nlmsg = {
18469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        .nlmsg_type = action,
18569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        .nlmsg_flags = flags,
18669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    };
18769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    iov[0].iov_base = &nlmsg;
18869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    iov[0].iov_len = sizeof(nlmsg);
18969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    for (int i = 0; i < iovlen; ++i) {
19069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        nlmsg.nlmsg_len += iov[i].iov_len;
19169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
19269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
19369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    int ret;
19469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    struct {
19569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        nlmsghdr msg;
19669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        nlmsgerr err;
19769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    } response;
19869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
19969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
20069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    if (sock != -1 &&
20169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            connect(sock, reinterpret_cast<const sockaddr*>(&NETLINK_ADDRESS),
20269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                    sizeof(NETLINK_ADDRESS)) != -1 &&
20369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            writev(sock, iov, iovlen) != -1 &&
20469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            (ret = recv(sock, &response, sizeof(response), 0)) != -1) {
20569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        if (ret == sizeof(response)) {
20669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            ret = response.err.error;  // Netlink errors are negative errno.
20769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            if (ret) {
20869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                ALOGE("netlink response contains error (%s)", strerror(-ret));
20969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            }
21069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        } else {
21169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response));
21269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal            ret = -EBADMSG;
21369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        }
21469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    } else {
21569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ALOGE("netlink socket/connect/writev/recv failed (%s)", strerror(errno));
21669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ret = -errno;
21769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
21869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
21969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    if (sock != -1) {
22069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        close(sock);
22169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
22269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
22369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    return ret;
22469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
22569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
22669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// Returns 0 on success or negative errno on failure.
22769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalint padInterfaceName(const char* input, char* name, size_t* length, uint16_t* padding) {
22869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    if (!input) {
22969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        *length = 0;
23069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        *padding = 0;
23169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return 0;
23269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
23369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    *length = strlcpy(name, input, IFNAMSIZ) + 1;
23469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    if (*length > IFNAMSIZ) {
23569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ALOGE("interface name too long (%zu > %u)", *length, IFNAMSIZ);
23669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return -ENAMETOOLONG;
23769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
23869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    *padding = RTA_SPACE(*length) - RTA_LENGTH(*length);
23969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    return 0;
24069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal}
24169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
24269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// Adds or removes a routing rule for IPv4 and IPv6.
24369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal//
24469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the rule
24569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal//   returns ENETUNREACH.
24669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is
24769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal//   ignored.
24869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// + If |iif| is non-NULL, the rule matches the specified incoming interface.
24969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// + If |oif| is non-NULL, the rule matches the specified outgoing interface.
25069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// + If |uidStart| and |uidEnd| are not INVALID_UID, the rule matches packets from UIDs in that
25169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal//   range (inclusive). Otherwise, the rule matches packets from all UIDs.
25269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal//
25369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal// Returns 0 on success or negative errno on failure.
25469e17611504376e4d4603925f8528dfc890fd2c6Luis SigalWARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
25569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                    uint32_t fwmark, uint32_t mask, const char* iif,
25669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal                                    const char* oif, uid_t uidStart, uid_t uidEnd) {
25769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask.
25869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    if (fwmark & ~mask) {
25969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark);
26069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return -ERANGE;
26169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    }
26269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal
26369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    // Interface names must include exactly one terminating NULL and be properly padded, or older
26469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    // kernels will refuse to delete rules.
26569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    char iifName[IFNAMSIZ], oifName[IFNAMSIZ];
26669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    size_t iifLength, oifLength;
26769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    uint16_t iifPadding, oifPadding;
26869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal    if (int ret = padInterfaceName(iif, iifName, &iifLength, &iifPadding)) {
26969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal        return ret;
270    }
271    if (int ret = padInterfaceName(oif, oifName, &oifLength, &oifPadding)) {
272        return ret;
273    }
274
275    // Either both start and end UID must be specified, or neither.
276    if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) {
277        ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd);
278        return -EUSERS;
279    }
280    bool isUidRule = (uidStart != INVALID_UID);
281
282    // Assemble a rule request and put it in an array of iovec structures.
283    fib_rule_hdr rule = {
284        .action = static_cast<uint8_t>(table != RT_TABLE_UNSPEC ? FR_ACT_TO_TBL :
285                                                                  FR_ACT_UNREACHABLE),
286    };
287
288    rtattr fraIifName = { U16_RTA_LENGTH(iifLength), FRA_IIFNAME };
289    rtattr fraOifName = { U16_RTA_LENGTH(oifLength), FRA_OIFNAME };
290
291    iovec iov[] = {
292        { NULL,              0 },
293        { &rule,             sizeof(rule) },
294        { &FRATTR_PRIORITY,  sizeof(FRATTR_PRIORITY) },
295        { &priority,         sizeof(priority) },
296        { &FRATTR_TABLE,     table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 },
297        { &table,            table != RT_TABLE_UNSPEC ? sizeof(table) : 0 },
298        { &FRATTR_FWMARK,    mask ? sizeof(FRATTR_FWMARK) : 0 },
299        { &fwmark,           mask ? sizeof(fwmark) : 0 },
300        { &FRATTR_FWMASK,    mask ? sizeof(FRATTR_FWMASK) : 0 },
301        { &mask,             mask ? sizeof(mask) : 0 },
302        { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 },
303        { &uidStart,         isUidRule ? sizeof(uidStart) : 0 },
304        { &FRATTR_UID_END,   isUidRule ? sizeof(FRATTR_UID_END) : 0 },
305        { &uidEnd,           isUidRule ? sizeof(uidEnd) : 0 },
306        { &fraIifName,       iif != IIF_NONE ? sizeof(fraIifName) : 0 },
307        { iifName,           iifLength },
308        { PADDING_BUFFER,    iifPadding },
309        { &fraOifName,       oif != OIF_NONE ? sizeof(fraOifName) : 0 },
310        { oifName,           oifLength },
311        { PADDING_BUFFER,    oifPadding },
312    };
313
314    uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS;
315    for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) {
316        rule.family = AF_FAMILIES[i];
317        if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov))) {
318            return ret;
319        }
320    }
321
322    return 0;
323}
324
325WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table,
326                                    uint32_t fwmark, uint32_t mask) {
327    return modifyIpRule(action, priority, table, fwmark, mask, IIF_NONE, OIF_NONE, INVALID_UID,
328                        INVALID_UID);
329}
330
331// Adds or deletes an IPv4 or IPv6 route.
332// Returns 0 on success or negative errno on failure.
333WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface,
334                                     const char* destination, const char* nexthop) {
335    // At least the destination must be non-null.
336    if (!destination) {
337        ALOGE("null destination");
338        return -EFAULT;
339    }
340
341    // Parse the prefix.
342    uint8_t rawAddress[sizeof(in6_addr)];
343    uint8_t family;
344    uint8_t prefixLength;
345    int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress),
346                                &prefixLength);
347    if (rawLength < 0) {
348        ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength));
349        return rawLength;
350    }
351
352    if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) {
353        ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress));
354        return -ENOBUFS;  // Cannot happen; parsePrefix only supports IPv4 and IPv6.
355    }
356
357    // If an interface was specified, find the ifindex.
358    uint32_t ifindex;
359    if (interface != OIF_NONE) {
360        ifindex = if_nametoindex(interface);
361        if (!ifindex) {
362            ALOGE("cannot find interface %s", interface);
363            return -ENODEV;
364        }
365    }
366
367    // If a nexthop was specified, parse it as the same family as the prefix.
368    uint8_t rawNexthop[sizeof(in6_addr)];
369    if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) {
370        ALOGE("inet_pton failed for nexthop %s", nexthop);
371        return -EINVAL;
372    }
373
374    // Assemble a rtmsg and put it in an array of iovec structures.
375    rtmsg route = {
376        .rtm_protocol = RTPROT_STATIC,
377        .rtm_type = RTN_UNICAST,
378        .rtm_family = family,
379        .rtm_dst_len = prefixLength,
380    };
381
382    rtattr rtaDst     = { U16_RTA_LENGTH(rawLength), RTA_DST };
383    rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY };
384
385    iovec iov[] = {
386        { NULL,          0 },
387        { &route,        sizeof(route) },
388        { &RTATTR_TABLE, sizeof(RTATTR_TABLE) },
389        { &table,        sizeof(table) },
390        { &rtaDst,       sizeof(rtaDst) },
391        { rawAddress,    static_cast<size_t>(rawLength) },
392        { &RTATTR_OIF,   interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 },
393        { &ifindex,      interface != OIF_NONE ? sizeof(ifindex) : 0 },
394        { &rtaGateway,   nexthop ? sizeof(rtaGateway) : 0 },
395        { rawNexthop,    nexthop ? static_cast<size_t>(rawLength) : 0 },
396    };
397
398    uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS :
399                                                NETLINK_REQUEST_FLAGS;
400    return sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov));
401}
402
403// An iptables rule to mark incoming packets on a network with the netId of the network.
404//
405// This is so that the kernel can:
406// + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping
407//   replies, SYN-ACKs, etc).
408// + Mark sockets that accept connections from this interface so that the connection stays on the
409//   same interface.
410WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface,
411                                                Permission permission, bool add) {
412    Fwmark fwmark;
413
414    fwmark.netId = netId;
415    fwmark.explicitlySelected = true;
416    fwmark.protectedFromVpn = true;
417    fwmark.permission = permission;
418
419    char markString[UINT32_HEX_STRLEN];
420    snprintf(markString, sizeof(markString), "0x%x", fwmark.intValue);
421
422    if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j",
423                     "MARK", "--set-mark", markString, NULL)) {
424        ALOGE("failed to change iptables rule that sets incoming packet mark");
425        return -EREMOTEIO;
426    }
427
428    return 0;
429}
430
431// A rule to route responses to the local network forwarded via the VPN.
432//
433// When a VPN is in effect, packets from the local network to upstream networks are forwarded into
434// the VPN's tunnel interface. When the VPN forwards the responses, they emerge out of the tunnel.
435WARN_UNUSED_RESULT int modifyVpnOutputToLocalRule(const char* vpnInterface, bool add) {
436    return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_VPN_OUTPUT_TO_LOCAL,
437                        ROUTE_TABLE_LOCAL_NETWORK, MARK_UNSET, MARK_UNSET, vpnInterface, OIF_NONE,
438                        INVALID_UID, INVALID_UID);
439}
440
441// A rule to route all traffic from a given set of UIDs to go over the VPN.
442//
443// Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may
444// have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to
445// bypass the VPN if the protectedFromVpn bit is set.
446WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd,
447                                             bool secure, bool add) {
448    Fwmark fwmark;
449    Fwmark mask;
450
451    fwmark.protectedFromVpn = false;
452    mask.protectedFromVpn = true;
453
454    uint32_t priority;
455
456    if (secure) {
457        priority = RULE_PRIORITY_SECURE_VPN;
458    } else {
459        priority = RULE_PRIORITY_BYPASSABLE_VPN;
460
461        fwmark.explicitlySelected = false;
462        mask.explicitlySelected = true;
463    }
464
465    return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
466                        mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
467}
468
469// A rule to allow system apps to send traffic over this VPN even if they are not part of the target
470// set of UIDs.
471//
472// This is needed for DnsProxyListener to correctly resolve a request for a user who is in the
473// target set, but where the DnsProxyListener itself is not.
474WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool secure,
475                                                     bool add) {
476    Fwmark fwmark;
477    Fwmark mask;
478
479    fwmark.netId = netId;
480    mask.netId = FWMARK_NET_ID_MASK;
481
482    fwmark.permission = PERMISSION_SYSTEM;
483    mask.permission = PERMISSION_SYSTEM;
484
485    uint32_t priority = secure ? RULE_PRIORITY_SECURE_VPN : RULE_PRIORITY_BYPASSABLE_VPN;
486
487    return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, priority, table, fwmark.intValue,
488                        mask.intValue);
489}
490
491// A rule to route traffic based on an explicitly chosen network.
492//
493// Supports apps that use the multinetwork APIs to restrict their traffic to a network.
494//
495// Even though we check permissions at the time we set a netId into the fwmark of a socket, we need
496// to check it again in the rules here, because a network's permissions may have been updated via
497// modifyNetworkPermission().
498WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table,
499                                                 Permission permission, uid_t uidStart,
500                                                 uid_t uidEnd, bool add) {
501    Fwmark fwmark;
502    Fwmark mask;
503
504    fwmark.netId = netId;
505    mask.netId = FWMARK_NET_ID_MASK;
506
507    fwmark.explicitlySelected = true;
508    mask.explicitlySelected = true;
509
510    fwmark.permission = permission;
511    mask.permission = permission;
512
513    return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table,
514                        fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, uidStart, uidEnd);
515}
516
517// A rule to route traffic based on a chosen outgoing interface.
518//
519// Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows
520// the outgoing interface (typically for link-local communications).
521WARN_UNUSED_RESULT int modifyOutputInterfaceRule(const char* interface, uint32_t table,
522                                                 Permission permission, uid_t uidStart,
523                                                 uid_t uidEnd, bool add) {
524    Fwmark fwmark;
525    Fwmark mask;
526
527    fwmark.permission = permission;
528    mask.permission = permission;
529
530    return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table,
531                        fwmark.intValue, mask.intValue, IIF_NONE, interface, uidStart, uidEnd);
532}
533
534// A rule to route traffic based on the chosen network.
535//
536// This is for sockets that have not explicitly requested a particular network, but have been
537// bound to one when they called connect(). This ensures that sockets connected on a particular
538// network stay on that network even if the default network changes.
539WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table,
540                                                 Permission permission, bool add) {
541    Fwmark fwmark;
542    Fwmark mask;
543
544    fwmark.netId = netId;
545    mask.netId = FWMARK_NET_ID_MASK;
546
547    fwmark.explicitlySelected = false;
548    mask.explicitlySelected = true;
549
550    fwmark.permission = permission;
551    mask.permission = permission;
552
553    return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table,
554                        fwmark.intValue, mask.intValue);
555}
556
557// Add rules to allow legacy routes added through the requestRouteToHost() API.
558WARN_UNUSED_RESULT int addLegacyRouteRules() {
559    Fwmark fwmark;
560    Fwmark mask;
561
562    fwmark.explicitlySelected = false;
563    mask.explicitlySelected = true;
564
565    // Rules to allow legacy routes to override the default network.
566    if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
567                               fwmark.intValue, mask.intValue)) {
568        return ret;
569    }
570    if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK,
571                               ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, mask.intValue)) {
572        return ret;
573    }
574
575    fwmark.permission = PERMISSION_SYSTEM;
576    mask.permission = PERMISSION_SYSTEM;
577
578    // A rule to allow legacy routes from system apps to override VPNs.
579    return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM,
580                        fwmark.intValue, mask.intValue);
581}
582
583// Add rules to lookup the local network when specified explicitly or otherwise.
584WARN_UNUSED_RESULT int addLocalNetworkRules(unsigned localNetId) {
585    if (int ret = modifyExplicitNetworkRule(localNetId, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
586                                            INVALID_UID, INVALID_UID, ACTION_ADD)) {
587        return ret;
588    }
589
590    Fwmark fwmark;
591    Fwmark mask;
592
593    fwmark.explicitlySelected = false;
594    mask.explicitlySelected = true;
595
596    return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LOCAL_NETWORK, ROUTE_TABLE_LOCAL_NETWORK,
597                        fwmark.intValue, mask.intValue);
598}
599
600// Add a new rule to look up the 'main' table, with the same selectors as the "default network"
601// rule, but with a lower priority. Since the default network rule points to a table with a default
602// route, the rule we're adding will never be used for normal routing lookups. However, the kernel
603// may fall-through to it to find directly-connected routes when it validates that a nexthop (in a
604// route being added) is reachable.
605WARN_UNUSED_RESULT int addDirectlyConnectedRule() {
606    Fwmark fwmark;
607    Fwmark mask;
608
609    fwmark.netId = NETID_UNSET;
610    mask.netId = FWMARK_NET_ID_MASK;
611
612    return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN,
613                        fwmark.intValue, mask.intValue, IIF_NONE, OIF_NONE, UID_ROOT, UID_ROOT);
614}
615
616// Add a rule to preempt the pre-defined "from all lookup main" rule. Packets that reach this rule
617// will be null-routed, and won't fall-through to the main table.
618WARN_UNUSED_RESULT int addUnreachableRule() {
619    return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, RT_TABLE_UNSPEC, MARK_UNSET,
620                        MARK_UNSET);
621}
622
623WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) {
624    if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
625        return ret;
626    }
627    return modifyOutputInterfaceRule(interface, ROUTE_TABLE_LOCAL_NETWORK, PERMISSION_NONE,
628                                     INVALID_UID, INVALID_UID, add);
629}
630
631WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface,
632                                             Permission permission, bool add) {
633    uint32_t table = getRouteTableForInterface(interface);
634    if (table == RT_TABLE_UNSPEC) {
635        return -ESRCH;
636    }
637
638    if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) {
639        return ret;
640    }
641    if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID,
642                                            add)) {
643        return ret;
644    }
645    if (int ret = modifyOutputInterfaceRule(interface, table, permission, INVALID_UID, INVALID_UID,
646                                            add)) {
647        return ret;
648    }
649    return modifyImplicitNetworkRule(netId, table, permission, add);
650}
651
652WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface,
653                                            const UidRanges& uidRanges, bool secure, bool add,
654                                            bool modifyNonUidBasedRules) {
655    uint32_t table = getRouteTableForInterface(interface);
656    if (table == RT_TABLE_UNSPEC) {
657        return -ESRCH;
658    }
659
660    for (const UidRanges::Range& range : uidRanges.getRanges()) {
661        if (int ret = modifyVpnUidRangeRule(table, range.first, range.second, secure, add)) {
662            return ret;
663        }
664        if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.first,
665                                                range.second, add)) {
666            return ret;
667        }
668        if (int ret = modifyOutputInterfaceRule(interface, table, PERMISSION_NONE, range.first,
669                                                range.second, add)) {
670            return ret;
671        }
672    }
673
674    if (modifyNonUidBasedRules) {
675        if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) {
676            return ret;
677        }
678        if (int ret = modifyVpnOutputToLocalRule(interface, add)) {
679            return ret;
680        }
681        if (int ret = modifyVpnSystemPermissionRule(netId, table, secure, add)) {
682            return ret;
683        }
684        return modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, add);
685    }
686
687    return 0;
688}
689
690WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface,
691                                            Permission permission) {
692    uint32_t table = getRouteTableForInterface(interface);
693    if (table == RT_TABLE_UNSPEC) {
694        return -ESRCH;
695    }
696
697    Fwmark fwmark;
698    Fwmark mask;
699
700    fwmark.netId = NETID_UNSET;
701    mask.netId = FWMARK_NET_ID_MASK;
702
703    fwmark.permission = permission;
704    mask.permission = permission;
705
706    return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue,
707                        mask.intValue);
708}
709
710WARN_UNUSED_RESULT int modifyTetheredNetwork(uint16_t action, const char* inputInterface,
711                                             const char* outputInterface) {
712    uint32_t table = getRouteTableForInterface(outputInterface);
713    if (table == RT_TABLE_UNSPEC) {
714        return -ESRCH;
715    }
716
717    return modifyIpRule(action, RULE_PRIORITY_TETHERING, table, MARK_UNSET, MARK_UNSET,
718                        inputInterface, OIF_NONE, INVALID_UID, INVALID_UID);
719}
720
721// Returns 0 on success or negative errno on failure.
722WARN_UNUSED_RESULT int flushRules() {
723    for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
724        const char* argv[] = {
725            IP_PATH,
726            IP_VERSIONS[i],
727            "rule",
728            "flush",
729        };
730        if (android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, false, false)) {
731            ALOGE("failed to flush rules");
732            return -EREMOTEIO;
733        }
734    }
735    return 0;
736}
737
738// Adds or removes an IPv4 or IPv6 route to the specified table and, if it's a directly-connected
739// route, to the main table as well.
740// Returns 0 on success or negative errno on failure.
741WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination,
742                                   const char* nexthop, RouteController::TableType tableType) {
743    uint32_t table;
744    switch (tableType) {
745        case RouteController::INTERFACE: {
746            table = getRouteTableForInterface(interface);
747            if (table == RT_TABLE_UNSPEC) {
748                return -ESRCH;
749            }
750            break;
751        }
752        case RouteController::LOCAL_NETWORK: {
753            table = ROUTE_TABLE_LOCAL_NETWORK;
754            break;
755        }
756        case RouteController::LEGACY_NETWORK: {
757            table = ROUTE_TABLE_LEGACY_NETWORK;
758            break;
759        }
760        case RouteController::LEGACY_SYSTEM: {
761            table = ROUTE_TABLE_LEGACY_SYSTEM;
762            break;
763        }
764    }
765
766    int ret = modifyIpRoute(action, table, interface, destination, nexthop);
767    // We allow apps to call requestRouteToHost() multiple times with the same route, so ignore
768    // EEXIST failures when adding routes to legacy tables.
769    if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST &&
770                 (tableType == RouteController::LEGACY_NETWORK ||
771                  tableType == RouteController::LEGACY_SYSTEM))) {
772        return ret;
773    }
774
775    // If there's no nexthop, this is a directly connected route. Add it to the main table also, to
776    // let the kernel find it when validating nexthops when global routes are added.
777    if (!nexthop) {
778        ret = modifyIpRoute(action, RT_TABLE_MAIN, interface, destination, NULL);
779        // A failure with action == ADD && errno == EEXIST means that the route already exists in
780        // the main table, perhaps because the kernel added it automatically as part of adding the
781        // IP address to the interface. Ignore this, but complain about everything else.
782        if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) {
783            return ret;
784        }
785    }
786
787    return 0;
788}
789
790// Returns 0 on success or negative errno on failure.
791WARN_UNUSED_RESULT int flushRoutes(const char* interface) {
792    uint32_t table = getRouteTableForInterface(interface);
793    if (table == RT_TABLE_UNSPEC) {
794        return -ESRCH;
795    }
796
797    char tableString[UINT32_STRLEN];
798    snprintf(tableString, sizeof(tableString), "%u", table);
799
800    for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) {
801        const char* argv[] = {
802            IP_PATH,
803            IP_VERSIONS[i],
804            "route",
805            "flush",
806            "table",
807            tableString,
808        };
809        if (android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, false, false)) {
810            ALOGE("failed to flush routes");
811            return -EREMOTEIO;
812        }
813    }
814
815    interfaceToTable.erase(interface);
816    return 0;
817}
818
819}  // namespace
820
821int RouteController::Init(unsigned localNetId) {
822    if (int ret = flushRules()) {
823        return ret;
824    }
825    if (int ret = addLegacyRouteRules()) {
826        return ret;
827    }
828    if (int ret = addLocalNetworkRules(localNetId)) {
829        return ret;
830    }
831    if (int ret = addDirectlyConnectedRule()) {
832        return ret;
833    }
834    if (int ret = addUnreachableRule()) {
835        return ret;
836    }
837    updateTableNamesFile();
838    return 0;
839}
840
841int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) {
842    return modifyLocalNetwork(netId, interface, ACTION_ADD);
843}
844
845int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) {
846    return modifyLocalNetwork(netId, interface, ACTION_DEL);
847}
848
849int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface,
850                                                   Permission permission) {
851    if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) {
852        return ret;
853    }
854    updateTableNamesFile();
855    return 0;
856}
857
858int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface,
859                                                        Permission permission) {
860    if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) {
861        return ret;
862    }
863    if (int ret = flushRoutes(interface)) {
864        return ret;
865    }
866    updateTableNamesFile();
867    return 0;
868}
869
870int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface,
871                                                  bool secure, const UidRanges& uidRanges) {
872    if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
873                                       MODIFY_NON_UID_BASED_RULES)) {
874        return ret;
875    }
876    updateTableNamesFile();
877    return 0;
878}
879
880int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface,
881                                                       bool secure, const UidRanges& uidRanges) {
882    if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
883                                       MODIFY_NON_UID_BASED_RULES)) {
884        return ret;
885    }
886    if (int ret = flushRoutes(interface)) {
887        return ret;
888    }
889    updateTableNamesFile();
890    return 0;
891}
892
893int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface,
894                                                     Permission oldPermission,
895                                                     Permission newPermission) {
896    // Add the new rules before deleting the old ones, to avoid race conditions.
897    if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) {
898        return ret;
899    }
900    return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL);
901}
902
903int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, bool secure,
904                                              const UidRanges& uidRanges) {
905    return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_ADD,
906                                !MODIFY_NON_UID_BASED_RULES);
907}
908
909int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface,
910                                                   bool secure, const UidRanges& uidRanges) {
911    return modifyVirtualNetwork(netId, interface, uidRanges, secure, ACTION_DEL,
912                                !MODIFY_NON_UID_BASED_RULES);
913}
914
915int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) {
916    return modifyDefaultNetwork(RTM_NEWRULE, interface, permission);
917}
918
919int RouteController::removeInterfaceFromDefaultNetwork(const char* interface,
920                                                       Permission permission) {
921    return modifyDefaultNetwork(RTM_DELRULE, interface, permission);
922}
923
924int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop,
925                              TableType tableType) {
926    return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType);
927}
928
929int RouteController::removeRoute(const char* interface, const char* destination,
930                                 const char* nexthop, TableType tableType) {
931    return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType);
932}
933
934int RouteController::enableTethering(const char* inputInterface, const char* outputInterface) {
935    return modifyTetheredNetwork(RTM_NEWRULE, inputInterface, outputInterface);
936}
937
938int RouteController::disableTethering(const char* inputInterface, const char* outputInterface) {
939    return modifyTetheredNetwork(RTM_DELRULE, inputInterface, outputInterface);
940}
941