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