Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "RouteController.h" |
| 18 | |
| 19 | #include "Fwmark.h" |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 20 | #include "UidRanges.h" |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 21 | |
| 22 | #define LOG_TAG "Netd" |
| 23 | #include "log/log.h" |
| 24 | #include "logwrap/logwrap.h" |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 25 | #include "resolv_netid.h" |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 26 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 27 | #include <arpa/inet.h> |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 28 | #include <fcntl.h> |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 29 | #include <linux/fib_rules.h> |
Paul Jensen | a561e12 | 2014-06-12 16:46:37 -0400 | [diff] [blame] | 30 | #include <map> |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 31 | #include <net/if.h> |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 32 | #include <sys/stat.h> |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 33 | |
| 34 | namespace { |
| 35 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 36 | // BEGIN CONSTANTS -------------------------------------------------------------------------------- |
| 37 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 38 | const uint32_t RULE_PRIORITY_VPN_OVERRIDE_SYSTEM = 10000; |
| 39 | // const uint32_t RULE_PRIORITY_VPN_OVERRIDE_LOCAL = 11000; |
| 40 | const uint32_t RULE_PRIORITY_SECURE_VPN = 12000; |
| 41 | const uint32_t RULE_PRIORITY_EXPLICIT_NETWORK = 13000; |
| 42 | const uint32_t RULE_PRIORITY_OUTPUT_INTERFACE = 14000; |
| 43 | const uint32_t RULE_PRIORITY_LEGACY_SYSTEM = 15000; |
| 44 | const uint32_t RULE_PRIORITY_LEGACY_NETWORK = 16000; |
Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 45 | const uint32_t RULE_PRIORITY_LOCAL_NETWORK = 17000; |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 46 | // const uint32_t RULE_PRIORITY_TETHERING = 18000; |
| 47 | const uint32_t RULE_PRIORITY_IMPLICIT_NETWORK = 19000; |
| 48 | // const uint32_t RULE_PRIORITY_BYPASSABLE_VPN = 20000; |
| 49 | // const uint32_t RULE_PRIORITY_VPN_FALLTHROUGH = 21000; |
| 50 | const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 22000; |
| 51 | const uint32_t RULE_PRIORITY_DIRECTLY_CONNECTED = 23000; |
| 52 | const uint32_t RULE_PRIORITY_UNREACHABLE = 24000; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 53 | |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 54 | const uint32_t ROUTE_TABLE_LEGACY_NETWORK = 98; |
| 55 | const uint32_t ROUTE_TABLE_LEGACY_SYSTEM = 99; |
| 56 | |
| 57 | const char* const ROUTE_TABLE_NAME_LEGACY_NETWORK = "legacy_network"; |
| 58 | const char* const ROUTE_TABLE_NAME_LEGACY_SYSTEM = "legacy_system"; |
| 59 | |
Sreeram Ramachandran | bb40d51 | 2014-07-11 11:45:14 -0700 | [diff] [blame] | 60 | const char* const ROUTE_TABLE_NAME_LOCAL = "local"; |
| 61 | const char* const ROUTE_TABLE_NAME_MAIN = "main"; |
| 62 | |
Lorenzo Colitti | 5965651 | 2014-06-25 03:20:29 +0900 | [diff] [blame] | 63 | // TODO: These values aren't defined by the Linux kernel, because our UID routing changes are not |
| 64 | // upstream (yet?), so we can't just pick them up from kernel headers. When (if?) the changes make |
| 65 | // it upstream, we'll remove this and rely on the kernel header values. For now, add a static assert |
| 66 | // that will warn us if upstream has given these values some other meaning. |
| 67 | const uint16_t FRA_UID_START = 18; |
| 68 | const uint16_t FRA_UID_END = 19; |
| 69 | static_assert(FRA_UID_START > FRA_MAX, |
| 70 | "Android-specific FRA_UID_{START,END} values also assigned in Linux uapi. " |
| 71 | "Check that these values match what the kernel does and then update this assertion."); |
| 72 | |
Lorenzo Colitti | 7272368 | 2014-06-26 13:51:10 +0900 | [diff] [blame] | 73 | const uint16_t NETLINK_REQUEST_FLAGS = NLM_F_REQUEST | NLM_F_ACK; |
| 74 | const uint16_t NETLINK_CREATE_REQUEST_FLAGS = NETLINK_REQUEST_FLAGS | NLM_F_CREATE | NLM_F_EXCL; |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 75 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 76 | const sockaddr_nl NETLINK_ADDRESS = {AF_NETLINK, 0, 0, 0}; |
| 77 | |
| 78 | const uint8_t AF_FAMILIES[] = {AF_INET, AF_INET6}; |
| 79 | |
| 80 | const char* const IP_VERSIONS[] = {"-4", "-6"}; |
| 81 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 82 | const uid_t UID_ROOT = 0; |
| 83 | const char* const OIF_NONE = NULL; |
| 84 | const bool ACTION_ADD = true; |
| 85 | const bool ACTION_DEL = false; |
| 86 | const bool MODIFY_NON_UID_BASED_RULES = true; |
| 87 | |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 88 | const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; |
Sreeram Ramachandran | c7d804c | 2014-07-09 07:39:30 -0700 | [diff] [blame] | 89 | const int RT_TABLES_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC; |
| 90 | const mode_t RT_TABLES_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // mode 0644, rw-r--r-- |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 91 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 92 | // Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'" |
| 93 | // warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t). |
| 94 | constexpr uint16_t U16_RTA_LENGTH(uint16_t x) { |
| 95 | return RTA_LENGTH(x); |
| 96 | } |
| 97 | |
| 98 | // These are practically const, but can't be declared so, because they are used to initialize |
| 99 | // non-const pointers ("void* iov_base") in iovec arrays. |
| 100 | rtattr FRATTR_PRIORITY = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_PRIORITY }; |
| 101 | rtattr FRATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_TABLE }; |
| 102 | rtattr FRATTR_FWMARK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMARK }; |
| 103 | rtattr FRATTR_FWMASK = { U16_RTA_LENGTH(sizeof(uint32_t)), FRA_FWMASK }; |
| 104 | rtattr FRATTR_UID_START = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_START }; |
| 105 | rtattr FRATTR_UID_END = { U16_RTA_LENGTH(sizeof(uid_t)), FRA_UID_END }; |
| 106 | |
| 107 | rtattr RTATTR_TABLE = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_TABLE }; |
| 108 | rtattr RTATTR_OIF = { U16_RTA_LENGTH(sizeof(uint32_t)), RTA_OIF }; |
| 109 | |
| 110 | uint8_t PADDING_BUFFER[RTA_ALIGNTO] = {0, 0, 0, 0}; |
| 111 | |
| 112 | // END CONSTANTS ---------------------------------------------------------------------------------- |
| 113 | |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 114 | // No locks needed because RouteController is accessed only from one thread (in CommandListener). |
| 115 | std::map<std::string, uint32_t> interfaceToTable; |
Paul Jensen | a561e12 | 2014-06-12 16:46:37 -0400 | [diff] [blame] | 116 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 117 | uint32_t getRouteTableForInterface(const char* interface) { |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 118 | uint32_t index = if_nametoindex(interface); |
Paul Jensen | a561e12 | 2014-06-12 16:46:37 -0400 | [diff] [blame] | 119 | if (index) { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 120 | index += RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX; |
| 121 | interfaceToTable[interface] = index; |
| 122 | return index; |
Paul Jensen | a561e12 | 2014-06-12 16:46:37 -0400 | [diff] [blame] | 123 | } |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 124 | // If the interface goes away if_nametoindex() will return 0 but we still need to know |
| 125 | // the index so we can remove the rules and routes. |
| 126 | auto iter = interfaceToTable.find(interface); |
| 127 | if (iter == interfaceToTable.end()) { |
| 128 | ALOGE("cannot find interface %s", interface); |
| 129 | return RT_TABLE_UNSPEC; |
| 130 | } |
| 131 | return iter->second; |
| 132 | } |
| 133 | |
| 134 | void addTableName(uint32_t table, const std::string& name, std::string* contents) { |
| 135 | char tableString[UINT32_STRLEN]; |
| 136 | snprintf(tableString, sizeof(tableString), "%u", table); |
| 137 | *contents += tableString; |
| 138 | *contents += " "; |
| 139 | *contents += name; |
| 140 | *contents += "\n"; |
| 141 | } |
| 142 | |
| 143 | // Doesn't return success/failure as the file is optional; it's okay if we fail to update it. |
| 144 | void updateTableNamesFile() { |
| 145 | std::string contents; |
Sreeram Ramachandran | bb40d51 | 2014-07-11 11:45:14 -0700 | [diff] [blame] | 146 | |
| 147 | addTableName(RT_TABLE_LOCAL, ROUTE_TABLE_NAME_LOCAL, &contents); |
| 148 | addTableName(RT_TABLE_MAIN, ROUTE_TABLE_NAME_MAIN, &contents); |
| 149 | |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 150 | addTableName(ROUTE_TABLE_LEGACY_NETWORK, ROUTE_TABLE_NAME_LEGACY_NETWORK, &contents); |
| 151 | addTableName(ROUTE_TABLE_LEGACY_SYSTEM, ROUTE_TABLE_NAME_LEGACY_SYSTEM, &contents); |
Sreeram Ramachandran | bb40d51 | 2014-07-11 11:45:14 -0700 | [diff] [blame] | 152 | |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 153 | for (const auto& entry : interfaceToTable) { |
| 154 | addTableName(entry.second, entry.first, &contents); |
| 155 | } |
| 156 | |
Sreeram Ramachandran | c7d804c | 2014-07-09 07:39:30 -0700 | [diff] [blame] | 157 | int fd = open(RT_TABLES_PATH, RT_TABLES_FLAGS, RT_TABLES_MODE); |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 158 | if (fd == -1) { |
| 159 | ALOGE("failed to create %s (%s)", RT_TABLES_PATH, strerror(errno)); |
| 160 | return; |
| 161 | } |
| 162 | // File creation is affected by umask, so make sure the right mode bits are set. |
Sreeram Ramachandran | c7d804c | 2014-07-09 07:39:30 -0700 | [diff] [blame] | 163 | if (fchmod(fd, RT_TABLES_MODE) == -1) { |
| 164 | ALOGE("failed to set mode 0%o on %s (%s)", RT_TABLES_MODE, RT_TABLES_PATH, strerror(errno)); |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 165 | } |
| 166 | ssize_t bytesWritten = write(fd, contents.data(), contents.size()); |
| 167 | if (bytesWritten != static_cast<ssize_t>(contents.size())) { |
| 168 | ALOGE("failed to write to %s (%zd vs %zu bytes) (%s)", RT_TABLES_PATH, bytesWritten, |
| 169 | contents.size(), strerror(errno)); |
| 170 | } |
| 171 | close(fd); |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 174 | // Sends a netlink request and expects an ack. |
| 175 | // |iov| is an array of struct iovec that contains the netlink message payload. |
| 176 | // The netlink header is generated by this function based on |action| and |flags|. |
| 177 | // Returns -errno if there was an error or if the kernel reported an error. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 178 | WARN_UNUSED_RESULT int sendNetlinkRequest(uint16_t action, uint16_t flags, iovec* iov, int iovlen) { |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 179 | nlmsghdr nlmsg = { |
| 180 | .nlmsg_type = action, |
| 181 | .nlmsg_flags = flags, |
| 182 | }; |
| 183 | iov[0].iov_base = &nlmsg; |
| 184 | iov[0].iov_len = sizeof(nlmsg); |
| 185 | for (int i = 0; i < iovlen; ++i) { |
| 186 | nlmsg.nlmsg_len += iov[i].iov_len; |
| 187 | } |
| 188 | |
| 189 | int ret; |
| 190 | struct { |
| 191 | nlmsghdr msg; |
| 192 | nlmsgerr err; |
| 193 | } response; |
| 194 | |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 195 | int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); |
| 196 | if (sock != -1 && |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 197 | connect(sock, reinterpret_cast<const sockaddr*>(&NETLINK_ADDRESS), |
| 198 | sizeof(NETLINK_ADDRESS)) != -1 && |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 199 | writev(sock, iov, iovlen) != -1 && |
| 200 | (ret = recv(sock, &response, sizeof(response), 0)) != -1) { |
| 201 | if (ret == sizeof(response)) { |
| 202 | ret = response.err.error; // Netlink errors are negative errno. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 203 | if (ret) { |
| 204 | ALOGE("netlink response contains error (%s)", strerror(-ret)); |
| 205 | } |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 206 | } else { |
Sreeram Ramachandran | 1201e84 | 2014-07-01 15:06:05 -0700 | [diff] [blame] | 207 | ALOGE("bad netlink response message size (%d != %zu)", ret, sizeof(response)); |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 208 | ret = -EBADMSG; |
| 209 | } |
| 210 | } else { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 211 | ALOGE("netlink socket/connect/writev/recv failed (%s)", strerror(errno)); |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 212 | ret = -errno; |
| 213 | } |
| 214 | |
| 215 | if (sock != -1) { |
| 216 | close(sock); |
| 217 | } |
| 218 | |
| 219 | return ret; |
| 220 | } |
| 221 | |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 222 | // Adds or removes a routing rule for IPv4 and IPv6. |
| 223 | // |
| 224 | // + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the rule |
| 225 | // returns ENETUNREACH. |
| 226 | // + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is |
| 227 | // ignored. |
| 228 | // + If |interface| is non-NULL, the rule matches the specified outgoing interface. |
Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 229 | // |
| 230 | // Returns 0 on success or negative errno on failure. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 231 | WARN_UNUSED_RESULT int modifyIpRule(uint16_t action, uint32_t priority, uint32_t table, |
| 232 | uint32_t fwmark, uint32_t mask, const char* interface, |
| 233 | uid_t uidStart, uid_t uidEnd) { |
| 234 | // Ensure that if you set a bit in the fwmark, it's not being ignored by the mask. |
| 235 | if (fwmark & ~mask) { |
| 236 | ALOGE("mask 0x%x does not select all the bits set in fwmark 0x%x", mask, fwmark); |
| 237 | return -ERANGE; |
| 238 | } |
| 239 | |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 240 | // The interface name must include exactly one terminating NULL and be properly padded, or older |
| 241 | // kernels will refuse to delete rules. |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 242 | uint16_t paddingLength = 0; |
| 243 | size_t interfaceLength = 0; |
| 244 | char oifname[IFNAMSIZ]; |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 245 | if (interface != OIF_NONE) { |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 246 | interfaceLength = strlcpy(oifname, interface, IFNAMSIZ) + 1; |
| 247 | if (interfaceLength > IFNAMSIZ) { |
Sreeram Ramachandran | cf89138 | 2014-07-01 15:49:20 -0700 | [diff] [blame] | 248 | ALOGE("interface name too long (%zu > %u)", interfaceLength, IFNAMSIZ); |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 249 | return -ENAMETOOLONG; |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 250 | } |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 251 | paddingLength = RTA_SPACE(interfaceLength) - RTA_LENGTH(interfaceLength); |
| 252 | } |
| 253 | |
Lorenzo Colitti | 5965651 | 2014-06-25 03:20:29 +0900 | [diff] [blame] | 254 | // Either both start and end UID must be specified, or neither. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 255 | if ((uidStart == INVALID_UID) != (uidEnd == INVALID_UID)) { |
Sreeram Ramachandran | cf89138 | 2014-07-01 15:49:20 -0700 | [diff] [blame] | 256 | ALOGE("incompatible start and end UIDs (%u vs %u)", uidStart, uidEnd); |
Lorenzo Colitti | 5965651 | 2014-06-25 03:20:29 +0900 | [diff] [blame] | 257 | return -EUSERS; |
| 258 | } |
Lorenzo Colitti | 7272368 | 2014-06-26 13:51:10 +0900 | [diff] [blame] | 259 | bool isUidRule = (uidStart != INVALID_UID); |
Lorenzo Colitti | 5965651 | 2014-06-25 03:20:29 +0900 | [diff] [blame] | 260 | |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 261 | // Assemble a rule request and put it in an array of iovec structures. |
| 262 | fib_rule_hdr rule = { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 263 | .action = static_cast<uint8_t>(table != RT_TABLE_UNSPEC ? FR_ACT_TO_TBL : |
| 264 | FR_ACT_UNREACHABLE), |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 265 | }; |
| 266 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 267 | rtattr fraOifname = { U16_RTA_LENGTH(interfaceLength), FRA_OIFNAME }; |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 268 | |
| 269 | iovec iov[] = { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 270 | { NULL, 0 }, |
| 271 | { &rule, sizeof(rule) }, |
| 272 | { &FRATTR_PRIORITY, sizeof(FRATTR_PRIORITY) }, |
| 273 | { &priority, sizeof(priority) }, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 274 | { &FRATTR_TABLE, table != RT_TABLE_UNSPEC ? sizeof(FRATTR_TABLE) : 0 }, |
| 275 | { &table, table != RT_TABLE_UNSPEC ? sizeof(table) : 0 }, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 276 | { &FRATTR_FWMARK, mask ? sizeof(FRATTR_FWMARK) : 0 }, |
| 277 | { &fwmark, mask ? sizeof(fwmark) : 0 }, |
| 278 | { &FRATTR_FWMASK, mask ? sizeof(FRATTR_FWMASK) : 0 }, |
| 279 | { &mask, mask ? sizeof(mask) : 0 }, |
| 280 | { &FRATTR_UID_START, isUidRule ? sizeof(FRATTR_UID_START) : 0 }, |
| 281 | { &uidStart, isUidRule ? sizeof(uidStart) : 0 }, |
| 282 | { &FRATTR_UID_END, isUidRule ? sizeof(FRATTR_UID_END) : 0 }, |
| 283 | { &uidEnd, isUidRule ? sizeof(uidEnd) : 0 }, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 284 | { &fraOifname, interface != OIF_NONE ? sizeof(fraOifname) : 0 }, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 285 | { oifname, interfaceLength }, |
| 286 | { PADDING_BUFFER, paddingLength }, |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 287 | }; |
| 288 | |
Lorenzo Colitti | 7272368 | 2014-06-26 13:51:10 +0900 | [diff] [blame] | 289 | uint16_t flags = (action == RTM_NEWRULE) ? NETLINK_CREATE_REQUEST_FLAGS : NETLINK_REQUEST_FLAGS; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 290 | for (size_t i = 0; i < ARRAY_SIZE(AF_FAMILIES); ++i) { |
| 291 | rule.family = AF_FAMILIES[i]; |
| 292 | if (int ret = sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov))) { |
Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 293 | return ret; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 297 | return 0; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 300 | // Adds or deletes an IPv4 or IPv6 route. |
| 301 | // Returns 0 on success or negative errno on failure. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 302 | WARN_UNUSED_RESULT int modifyIpRoute(uint16_t action, uint32_t table, const char* interface, |
| 303 | const char* destination, const char* nexthop) { |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 304 | // At least the destination must be non-null. |
| 305 | if (!destination) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 306 | ALOGE("null destination"); |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 307 | return -EFAULT; |
| 308 | } |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 309 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 310 | // Parse the prefix. |
| 311 | uint8_t rawAddress[sizeof(in6_addr)]; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 312 | uint8_t family; |
| 313 | uint8_t prefixLength; |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 314 | int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress), |
| 315 | &prefixLength); |
| 316 | if (rawLength < 0) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 317 | ALOGE("parsePrefix failed for destination %s (%s)", destination, strerror(-rawLength)); |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 318 | return rawLength; |
| 319 | } |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 320 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 321 | if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) { |
Sreeram Ramachandran | 1201e84 | 2014-07-01 15:06:05 -0700 | [diff] [blame] | 322 | ALOGE("impossible! address too long (%d vs %zu)", rawLength, sizeof(rawAddress)); |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 323 | return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6. |
| 324 | } |
| 325 | |
| 326 | // If an interface was specified, find the ifindex. |
| 327 | uint32_t ifindex; |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 328 | if (interface != OIF_NONE) { |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 329 | ifindex = if_nametoindex(interface); |
| 330 | if (!ifindex) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 331 | ALOGE("cannot find interface %s", interface); |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 332 | return -ENODEV; |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 336 | // If a nexthop was specified, parse it as the same family as the prefix. |
| 337 | uint8_t rawNexthop[sizeof(in6_addr)]; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 338 | if (nexthop && inet_pton(family, nexthop, rawNexthop) <= 0) { |
| 339 | ALOGE("inet_pton failed for nexthop %s", nexthop); |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 340 | return -EINVAL; |
| 341 | } |
| 342 | |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 343 | // Assemble a rtmsg and put it in an array of iovec structures. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 344 | rtmsg route = { |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 345 | .rtm_protocol = RTPROT_STATIC, |
| 346 | .rtm_type = RTN_UNICAST, |
| 347 | .rtm_family = family, |
| 348 | .rtm_dst_len = prefixLength, |
| 349 | }; |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 350 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 351 | rtattr rtaDst = { U16_RTA_LENGTH(rawLength), RTA_DST }; |
| 352 | rtattr rtaGateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY }; |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 353 | |
| 354 | iovec iov[] = { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 355 | { NULL, 0 }, |
| 356 | { &route, sizeof(route) }, |
| 357 | { &RTATTR_TABLE, sizeof(RTATTR_TABLE) }, |
| 358 | { &table, sizeof(table) }, |
| 359 | { &rtaDst, sizeof(rtaDst) }, |
| 360 | { rawAddress, static_cast<size_t>(rawLength) }, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 361 | { &RTATTR_OIF, interface != OIF_NONE ? sizeof(RTATTR_OIF) : 0 }, |
| 362 | { &ifindex, interface != OIF_NONE ? sizeof(ifindex) : 0 }, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 363 | { &rtaGateway, nexthop ? sizeof(rtaGateway) : 0 }, |
| 364 | { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 }, |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 365 | }; |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 366 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 367 | uint16_t flags = (action == RTM_NEWROUTE) ? NETLINK_CREATE_REQUEST_FLAGS : |
| 368 | NETLINK_REQUEST_FLAGS; |
Lorenzo Colitti | 4753afd | 2014-06-20 23:03:29 +0900 | [diff] [blame] | 369 | return sendNetlinkRequest(action, flags, iov, ARRAY_SIZE(iov)); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 372 | // Add rules to allow legacy routes added through the requestRouteToHost() API. |
| 373 | WARN_UNUSED_RESULT int AddLegacyRouteRules() { |
| 374 | Fwmark fwmark; |
| 375 | Fwmark mask; |
| 376 | |
| 377 | fwmark.explicitlySelected = false; |
| 378 | mask.explicitlySelected = true; |
| 379 | |
| 380 | // Rules to allow legacy routes to override the default network. |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 381 | if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM, |
| 382 | fwmark.intValue, mask.intValue, OIF_NONE, INVALID_UID, |
| 383 | INVALID_UID)) { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 384 | return ret; |
| 385 | } |
| 386 | if (int ret = modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_LEGACY_NETWORK, |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 387 | ROUTE_TABLE_LEGACY_NETWORK, fwmark.intValue, |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 388 | mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID)) { |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | fwmark.permission = PERMISSION_SYSTEM; |
| 393 | mask.permission = PERMISSION_SYSTEM; |
| 394 | |
| 395 | // A rule to allow legacy routes from system apps to override VPNs. |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 396 | return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_VPN_OVERRIDE_SYSTEM, ROUTE_TABLE_LEGACY_SYSTEM, |
| 397 | fwmark.intValue, mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID); |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // Add a new rule to look up the 'main' table, with the same selectors as the "default network" |
| 401 | // rule, but with a lower priority. Since the default network rule points to a table with a default |
| 402 | // route, the rule we're adding will never be used for normal routing lookups. However, the kernel |
| 403 | // may fall-through to it to find directly-connected routes when it validates that a nexthop (in a |
| 404 | // route being added) is reachable. |
| 405 | WARN_UNUSED_RESULT int AddDirectlyConnectedRule() { |
| 406 | Fwmark fwmark; |
| 407 | Fwmark mask; |
| 408 | |
| 409 | fwmark.netId = NETID_UNSET; |
| 410 | mask.netId = FWMARK_NET_ID_MASK; |
| 411 | |
| 412 | return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_DIRECTLY_CONNECTED, RT_TABLE_MAIN, |
| 413 | fwmark.intValue, mask.intValue, OIF_NONE, UID_ROOT, UID_ROOT); |
| 414 | } |
| 415 | |
| 416 | // Add a rule to preempt the pre-defined "from all lookup main" rule. Packets that reach this rule |
| 417 | // will be null-routed, and won't fall-through to the main table. |
| 418 | WARN_UNUSED_RESULT int AddUnreachableRule() { |
| 419 | return modifyIpRule(RTM_NEWRULE, RULE_PRIORITY_UNREACHABLE, RT_TABLE_UNSPEC, MARK_UNSET, |
| 420 | MARK_UNSET, OIF_NONE, INVALID_UID, INVALID_UID); |
| 421 | } |
| 422 | |
| 423 | // An iptables rule to mark incoming packets on a network with the netId of the network. |
| 424 | // |
| 425 | // This is so that the kernel can: |
| 426 | // + Use the right fwmark for (and thus correctly route) replies (e.g.: TCP RST, ICMP errors, ping |
| 427 | // replies, SYN-ACKs, etc). |
| 428 | // + Mark sockets that accept connections from this interface so that the connection stays on the |
| 429 | // same interface. |
| 430 | WARN_UNUSED_RESULT int modifyIncomingPacketMark(unsigned netId, const char* interface, |
| 431 | Permission permission, bool add) { |
| 432 | Fwmark fwmark; |
| 433 | |
| 434 | fwmark.netId = netId; |
| 435 | fwmark.explicitlySelected = true; |
| 436 | fwmark.protectedFromVpn = true; |
| 437 | fwmark.permission = permission; |
| 438 | |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 439 | char markString[UINT32_HEX_STRLEN]; |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 440 | snprintf(markString, sizeof(markString), "0x%x", fwmark.intValue); |
| 441 | |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 442 | if (execIptables(V4V6, "-t", "mangle", add ? "-A" : "-D", "INPUT", "-i", interface, "-j", |
| 443 | "MARK", "--set-mark", markString, NULL)) { |
| 444 | ALOGE("failed to change iptables rule that sets incoming packet mark"); |
| 445 | return -EREMOTEIO; |
| 446 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 447 | |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 451 | // A rule to route traffic based on an explicitly chosen network. |
| 452 | // |
| 453 | // Supports apps that use the multinetwork APIs to restrict their traffic to a network. |
| 454 | // |
| 455 | // Even though we check permissions at the time we set a netId into the fwmark of a socket, we need |
| 456 | // to check it again in the rules here, because a network's permissions may have been updated via |
| 457 | // modifyNetworkPermission(). |
| 458 | WARN_UNUSED_RESULT int modifyExplicitNetworkRule(unsigned netId, uint32_t table, |
| 459 | Permission permission, uid_t uidStart, |
| 460 | uid_t uidEnd, bool add) { |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 461 | Fwmark fwmark; |
| 462 | Fwmark mask; |
| 463 | |
| 464 | fwmark.netId = netId; |
| 465 | mask.netId = FWMARK_NET_ID_MASK; |
| 466 | |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 467 | fwmark.explicitlySelected = true; |
| 468 | mask.explicitlySelected = true; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 469 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 470 | fwmark.permission = permission; |
| 471 | mask.permission = permission; |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 472 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 473 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_EXPLICIT_NETWORK, table, |
| 474 | fwmark.intValue, mask.intValue, OIF_NONE, uidStart, uidEnd); |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 475 | } |
| 476 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 477 | // A rule to route traffic based on a chosen outgoing interface. |
| 478 | // |
| 479 | // Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already knows |
| 480 | // the outgoing interface (typically for link-local communications). |
| 481 | WARN_UNUSED_RESULT int modifyOutputInterfaceRule(const char* interface, uint32_t table, |
| 482 | Permission permission, uid_t uidStart, |
| 483 | uid_t uidEnd, bool add) { |
| 484 | Fwmark fwmark; |
| 485 | Fwmark mask; |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 486 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 487 | fwmark.permission = permission; |
| 488 | mask.permission = permission; |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 489 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 490 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_OUTPUT_INTERFACE, table, |
| 491 | fwmark.intValue, mask.intValue, interface, uidStart, uidEnd); |
| 492 | } |
| 493 | |
| 494 | // A rule to route traffic based on the chosen network. |
| 495 | // |
| 496 | // This is for sockets that have not explicitly requested a particular network, but have been |
| 497 | // bound to one when they called connect(). This ensures that sockets connected on a particular |
| 498 | // network stay on that network even if the default network changes. |
| 499 | WARN_UNUSED_RESULT int modifyImplicitNetworkRule(unsigned netId, uint32_t table, |
| 500 | Permission permission, bool add) { |
| 501 | Fwmark fwmark; |
| 502 | Fwmark mask; |
| 503 | |
| 504 | fwmark.netId = netId; |
| 505 | mask.netId = FWMARK_NET_ID_MASK; |
| 506 | |
| 507 | fwmark.explicitlySelected = false; |
| 508 | mask.explicitlySelected = true; |
| 509 | |
| 510 | fwmark.permission = permission; |
| 511 | mask.permission = permission; |
| 512 | |
| 513 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_IMPLICIT_NETWORK, table, |
| 514 | fwmark.intValue, mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID); |
| 515 | } |
| 516 | |
| 517 | // A rule to route all traffic from a given set of UIDs to go over the VPN. |
| 518 | // |
| 519 | // Notice that this rule doesn't use the netId. I.e., no matter what netId the user's socket may |
| 520 | // have, if they are subject to this VPN, their traffic has to go through it. Allows the traffic to |
| 521 | // bypass the VPN if the protectedFromVpn bit is set. |
| 522 | WARN_UNUSED_RESULT int modifyVpnUidRangeRule(uint32_t table, uid_t uidStart, uid_t uidEnd, |
| 523 | bool add) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 524 | Fwmark fwmark; |
| 525 | Fwmark mask; |
| 526 | |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 527 | fwmark.protectedFromVpn = false; |
| 528 | mask.protectedFromVpn = true; |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 529 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 530 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_SECURE_VPN, table, |
| 531 | fwmark.intValue, mask.intValue, OIF_NONE, uidStart, uidEnd); |
| 532 | } |
| 533 | |
| 534 | // A rule to allow system apps to send traffic over this VPN even if they are not part of the target |
| 535 | // set of UIDs. |
| 536 | // |
| 537 | // This is needed for DnsProxyListener to correctly resolve a request for a user who is in the |
| 538 | // target set, but where the DnsProxyListener itself is not. |
| 539 | WARN_UNUSED_RESULT int modifyVpnSystemPermissionRule(unsigned netId, uint32_t table, bool add) { |
| 540 | Fwmark fwmark; |
| 541 | Fwmark mask; |
| 542 | |
| 543 | fwmark.netId = netId; |
| 544 | mask.netId = FWMARK_NET_ID_MASK; |
| 545 | |
| 546 | fwmark.permission = PERMISSION_SYSTEM; |
| 547 | mask.permission = PERMISSION_SYSTEM; |
| 548 | |
| 549 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_SECURE_VPN, table, |
| 550 | fwmark.intValue, mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID); |
| 551 | } |
| 552 | |
Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 553 | // A rule to allow local network routes to override the default network. |
| 554 | WARN_UNUSED_RESULT int modifyLocalOverrideRule(uint32_t table, bool add) { |
| 555 | Fwmark fwmark; |
| 556 | Fwmark mask; |
| 557 | |
| 558 | fwmark.explicitlySelected = false; |
| 559 | mask.explicitlySelected = true; |
| 560 | |
| 561 | return modifyIpRule(add ? RTM_NEWRULE : RTM_DELRULE, RULE_PRIORITY_LOCAL_NETWORK, table, |
| 562 | fwmark.intValue, mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID); |
| 563 | } |
| 564 | |
| 565 | WARN_UNUSED_RESULT int modifyLocalNetwork(unsigned netId, const char* interface, bool add) { |
| 566 | uint32_t table = getRouteTableForInterface(interface); |
| 567 | if (table == RT_TABLE_UNSPEC) { |
| 568 | return -ESRCH; |
| 569 | } |
| 570 | |
| 571 | if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) { |
| 572 | return ret; |
| 573 | } |
| 574 | if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, INVALID_UID, INVALID_UID, |
| 575 | add)) { |
| 576 | return ret; |
| 577 | } |
| 578 | if (int ret = modifyOutputInterfaceRule(interface, table, PERMISSION_NONE, INVALID_UID, |
| 579 | INVALID_UID, add)) { |
| 580 | return ret; |
| 581 | } |
| 582 | return modifyLocalOverrideRule(table, add); |
| 583 | } |
| 584 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 585 | WARN_UNUSED_RESULT int modifyPhysicalNetwork(unsigned netId, const char* interface, |
| 586 | Permission permission, bool add) { |
| 587 | uint32_t table = getRouteTableForInterface(interface); |
| 588 | if (table == RT_TABLE_UNSPEC) { |
| 589 | return -ESRCH; |
| 590 | } |
| 591 | |
| 592 | if (int ret = modifyIncomingPacketMark(netId, interface, permission, add)) { |
| 593 | return ret; |
| 594 | } |
| 595 | if (int ret = modifyExplicitNetworkRule(netId, table, permission, INVALID_UID, INVALID_UID, |
| 596 | add)) { |
| 597 | return ret; |
| 598 | } |
| 599 | if (int ret = modifyOutputInterfaceRule(interface, table, permission, INVALID_UID, INVALID_UID, |
| 600 | add)) { |
| 601 | return ret; |
| 602 | } |
| 603 | return modifyImplicitNetworkRule(netId, table, permission, add); |
| 604 | } |
| 605 | |
| 606 | WARN_UNUSED_RESULT int modifyVirtualNetwork(unsigned netId, const char* interface, |
| 607 | const UidRanges& uidRanges, bool add, |
| 608 | bool modifyNonUidBasedRules) { |
| 609 | uint32_t table = getRouteTableForInterface(interface); |
| 610 | if (table == RT_TABLE_UNSPEC) { |
| 611 | return -ESRCH; |
| 612 | } |
| 613 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 614 | for (const UidRanges::Range& range : uidRanges.getRanges()) { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 615 | if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, range.first, |
| 616 | range.second, add)) { |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 617 | return ret; |
| 618 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 619 | if (int ret = modifyOutputInterfaceRule(interface, table, PERMISSION_NONE, range.first, |
| 620 | range.second, add)) { |
| 621 | return ret; |
| 622 | } |
| 623 | if (int ret = modifyVpnUidRangeRule(table, range.first, range.second, add)) { |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 624 | return ret; |
| 625 | } |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 628 | if (modifyNonUidBasedRules) { |
| 629 | if (int ret = modifyIncomingPacketMark(netId, interface, PERMISSION_NONE, add)) { |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 630 | return ret; |
| 631 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 632 | if (int ret = modifyExplicitNetworkRule(netId, table, PERMISSION_NONE, UID_ROOT, UID_ROOT, |
| 633 | add)) { |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 634 | return ret; |
| 635 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 636 | return modifyVpnSystemPermissionRule(netId, table, add); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | return 0; |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 642 | WARN_UNUSED_RESULT int modifyDefaultNetwork(uint16_t action, const char* interface, |
| 643 | Permission permission) { |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 644 | uint32_t table = getRouteTableForInterface(interface); |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 645 | if (table == RT_TABLE_UNSPEC) { |
Lorenzo Colitti | 96f261e | 2014-06-23 15:09:54 +0900 | [diff] [blame] | 646 | return -ESRCH; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 649 | Fwmark fwmark; |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 650 | Fwmark mask; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 651 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 652 | fwmark.netId = NETID_UNSET; |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 653 | mask.netId = FWMARK_NET_ID_MASK; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 654 | |
| 655 | fwmark.permission = permission; |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 656 | mask.permission = permission; |
| 657 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 658 | return modifyIpRule(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue, |
| 659 | mask.intValue, OIF_NONE, INVALID_UID, INVALID_UID); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 660 | } |
| 661 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 662 | // Adds or removes an IPv4 or IPv6 route to the specified table and, if it's a directly-connected |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 663 | // route, to the main table as well. |
| 664 | // Returns 0 on success or negative errno on failure. |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 665 | WARN_UNUSED_RESULT int modifyRoute(uint16_t action, const char* interface, const char* destination, |
| 666 | const char* nexthop, RouteController::TableType tableType) { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 667 | uint32_t table; |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 668 | switch (tableType) { |
| 669 | case RouteController::INTERFACE: { |
| 670 | table = getRouteTableForInterface(interface); |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 671 | if (table == RT_TABLE_UNSPEC) { |
| 672 | return -ESRCH; |
| 673 | } |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 674 | break; |
| 675 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 676 | case RouteController::LEGACY_NETWORK: { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 677 | table = ROUTE_TABLE_LEGACY_NETWORK; |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 678 | break; |
| 679 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 680 | case RouteController::LEGACY_SYSTEM: { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 681 | table = ROUTE_TABLE_LEGACY_SYSTEM; |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 682 | break; |
| 683 | } |
| 684 | } |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 685 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 686 | int ret = modifyIpRoute(action, table, interface, destination, nexthop); |
Sreeram Ramachandran | 1077d29 | 2014-06-27 06:42:11 -0700 | [diff] [blame] | 687 | // We allow apps to call requestRouteToHost() multiple times with the same route, so ignore |
| 688 | // EEXIST failures when adding routes to legacy tables. |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 689 | if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST && |
| 690 | tableType != RouteController::INTERFACE)) { |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 691 | return ret; |
Sreeram Ramachandran | c921337 | 2014-04-16 12:32:18 -0700 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | // If there's no nexthop, this is a directly connected route. Add it to the main table also, to |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 695 | // let the kernel find it when validating nexthops when global routes are added. |
| 696 | if (!nexthop) { |
| 697 | ret = modifyIpRoute(action, RT_TABLE_MAIN, interface, destination, NULL); |
| 698 | // A failure with action == ADD && errno == EEXIST means that the route already exists in |
| 699 | // the main table, perhaps because the kernel added it automatically as part of adding the |
| 700 | // IP address to the interface. Ignore this, but complain about everything else. |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 701 | if (ret && !(action == RTM_NEWROUTE && ret == -EEXIST)) { |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 702 | return ret; |
| 703 | } |
Sreeram Ramachandran | c921337 | 2014-04-16 12:32:18 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 706 | return 0; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 707 | } |
| 708 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 709 | // Returns 0 on success or negative errno on failure. |
| 710 | WARN_UNUSED_RESULT int flushRoutes(const char* interface) { |
Sreeram Ramachandran | 92b66c4 | 2014-04-15 14:28:55 -0700 | [diff] [blame] | 711 | uint32_t table = getRouteTableForInterface(interface); |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 712 | if (table == RT_TABLE_UNSPEC) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 713 | return -ESRCH; |
Sreeram Ramachandran | 92b66c4 | 2014-04-15 14:28:55 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Lorenzo Colitti | 357e562 | 2014-06-17 16:14:17 +0900 | [diff] [blame] | 716 | char tableString[UINT32_STRLEN]; |
| 717 | snprintf(tableString, sizeof(tableString), "%u", table); |
| 718 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 719 | for (size_t i = 0; i < ARRAY_SIZE(IP_VERSIONS); ++i) { |
Lorenzo Colitti | 357e562 | 2014-06-17 16:14:17 +0900 | [diff] [blame] | 720 | const char* argv[] = { |
| 721 | IP_PATH, |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 722 | IP_VERSIONS[i], |
Sreeram Ramachandran | 72999d6 | 2014-07-02 18:06:34 -0700 | [diff] [blame] | 723 | "route", |
Lorenzo Colitti | 357e562 | 2014-06-17 16:14:17 +0900 | [diff] [blame] | 724 | "flush", |
| 725 | "table", |
| 726 | tableString, |
| 727 | }; |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 728 | if (android_fork_execvp(ARRAY_SIZE(argv), const_cast<char**>(argv), NULL, false, false)) { |
| 729 | ALOGE("failed to flush routes"); |
| 730 | return -EREMOTEIO; |
Lorenzo Colitti | 357e562 | 2014-06-17 16:14:17 +0900 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 734 | interfaceToTable.erase(interface); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 735 | return 0; |
Sreeram Ramachandran | 92b66c4 | 2014-04-15 14:28:55 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 738 | } // namespace |
| 739 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 740 | int RouteController::Init() { |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 741 | if (int ret = AddDirectlyConnectedRule()) { |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 742 | return ret; |
| 743 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 744 | if (int ret = AddLegacyRouteRules()) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 745 | return ret; |
| 746 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 747 | // TODO: Enable once we are sure everything works. |
| 748 | if (false) { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 749 | if (int ret = AddUnreachableRule()) { |
| 750 | return ret; |
| 751 | } |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 752 | } |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 753 | updateTableNamesFile(); |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 754 | return 0; |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Sreeram Ramachandran | 6a77353 | 2014-07-11 09:10:20 -0700 | [diff] [blame] | 757 | int RouteController::addInterfaceToLocalNetwork(unsigned netId, const char* interface) { |
| 758 | if (int ret = modifyLocalNetwork(netId, interface, ACTION_ADD)) { |
| 759 | return ret; |
| 760 | } |
| 761 | updateTableNamesFile(); |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | int RouteController::removeInterfaceFromLocalNetwork(unsigned netId, const char* interface) { |
| 766 | if (int ret = modifyLocalNetwork(netId, interface, ACTION_DEL)) { |
| 767 | return ret; |
| 768 | } |
| 769 | if (int ret = flushRoutes(interface)) { |
| 770 | return ret; |
| 771 | } |
| 772 | updateTableNamesFile(); |
| 773 | return 0; |
| 774 | } |
| 775 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 776 | int RouteController::addInterfaceToPhysicalNetwork(unsigned netId, const char* interface, |
| 777 | Permission permission) { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 778 | if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_ADD)) { |
| 779 | return ret; |
| 780 | } |
| 781 | updateTableNamesFile(); |
| 782 | return 0; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 785 | int RouteController::removeInterfaceFromPhysicalNetwork(unsigned netId, const char* interface, |
| 786 | Permission permission) { |
| 787 | if (int ret = modifyPhysicalNetwork(netId, interface, permission, ACTION_DEL)) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 788 | return ret; |
| 789 | } |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 790 | if (int ret = flushRoutes(interface)) { |
| 791 | return ret; |
| 792 | } |
| 793 | updateTableNamesFile(); |
| 794 | return 0; |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 797 | int RouteController::addInterfaceToVirtualNetwork(unsigned netId, const char* interface, |
| 798 | const UidRanges& uidRanges) { |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 799 | if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, ACTION_ADD, |
| 800 | MODIFY_NON_UID_BASED_RULES)) { |
| 801 | return ret; |
| 802 | } |
| 803 | updateTableNamesFile(); |
| 804 | return 0; |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 807 | int RouteController::removeInterfaceFromVirtualNetwork(unsigned netId, const char* interface, |
| 808 | const UidRanges& uidRanges) { |
| 809 | if (int ret = modifyVirtualNetwork(netId, interface, uidRanges, ACTION_DEL, |
| 810 | MODIFY_NON_UID_BASED_RULES)) { |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 811 | return ret; |
| 812 | } |
Sreeram Ramachandran | 4acd34a | 2014-07-07 22:11:37 -0700 | [diff] [blame] | 813 | if (int ret = flushRoutes(interface)) { |
| 814 | return ret; |
| 815 | } |
| 816 | updateTableNamesFile(); |
| 817 | return 0; |
Sreeram Ramachandran | 4043f01 | 2014-06-23 12:41:37 -0700 | [diff] [blame] | 818 | } |
| 819 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 820 | int RouteController::modifyPhysicalNetworkPermission(unsigned netId, const char* interface, |
| 821 | Permission oldPermission, |
| 822 | Permission newPermission) { |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 823 | // Add the new rules before deleting the old ones, to avoid race conditions. |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 824 | if (int ret = modifyPhysicalNetwork(netId, interface, newPermission, ACTION_ADD)) { |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 825 | return ret; |
| 826 | } |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 827 | return modifyPhysicalNetwork(netId, interface, oldPermission, ACTION_DEL); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 830 | int RouteController::addUsersToVirtualNetwork(unsigned netId, const char* interface, |
| 831 | const UidRanges& uidRanges) { |
| 832 | return modifyVirtualNetwork(netId, interface, uidRanges, ACTION_ADD, |
| 833 | !MODIFY_NON_UID_BASED_RULES); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 836 | int RouteController::removeUsersFromVirtualNetwork(unsigned netId, const char* interface, |
| 837 | const UidRanges& uidRanges) { |
| 838 | return modifyVirtualNetwork(netId, interface, uidRanges, ACTION_DEL, |
| 839 | !MODIFY_NON_UID_BASED_RULES); |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 842 | int RouteController::addInterfaceToDefaultNetwork(const char* interface, Permission permission) { |
| 843 | return modifyDefaultNetwork(RTM_NEWRULE, interface, permission); |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Sreeram Ramachandran | 5009d5e | 2014-07-03 12:20:48 -0700 | [diff] [blame] | 846 | int RouteController::removeInterfaceFromDefaultNetwork(const char* interface, |
| 847 | Permission permission) { |
| 848 | return modifyDefaultNetwork(RTM_DELRULE, interface, permission); |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 849 | } |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 850 | |
Sreeram Ramachandran | f4f6c8d | 2014-06-23 09:54:06 -0700 | [diff] [blame] | 851 | int RouteController::addRoute(const char* interface, const char* destination, const char* nexthop, |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 852 | TableType tableType) { |
| 853 | return modifyRoute(RTM_NEWROUTE, interface, destination, nexthop, tableType); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 856 | int RouteController::removeRoute(const char* interface, const char* destination, |
Sreeram Ramachandran | eb27b7e | 2014-07-01 14:30:30 -0700 | [diff] [blame] | 857 | const char* nexthop, TableType tableType) { |
| 858 | return modifyRoute(RTM_DELROUTE, interface, destination, nexthop, tableType); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 859 | } |