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