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" |
| 20 | #include "NetdConstants.h" |
| 21 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 22 | #include <arpa/inet.h> |
| 23 | #include <errno.h> |
| 24 | #include <linux/netlink.h> |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 25 | #include <linux/rtnetlink.h> |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 26 | #include <logwrap/logwrap.h> |
Paul Jensen | a561e12 | 2014-06-12 16:46:37 -0400 | [diff] [blame] | 27 | #include <map> |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 28 | #include <netinet/in.h> |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 29 | #include <net/if.h> |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 30 | #include <sys/socket.h> |
| 31 | #include <sys/uio.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | // Avoids "non-constant-expression cannot be narrowed from type 'unsigned int' to 'unsigned short'" |
| 35 | // warnings when using RTA_LENGTH(x) inside static initializers (even when x is already uint16_t). |
| 36 | #define U16_RTA_LENGTH(x) static_cast<uint16_t>(RTA_LENGTH((x))) |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 37 | |
| 38 | namespace { |
| 39 | |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 40 | const uint32_t RULE_PRIORITY_PRIVILEGED_LEGACY = 11000; |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 41 | const uint32_t RULE_PRIORITY_PER_NETWORK_EXPLICIT = 13000; |
| 42 | const uint32_t RULE_PRIORITY_PER_NETWORK_INTERFACE = 14000; |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 43 | const uint32_t RULE_PRIORITY_LEGACY = 16000; |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 44 | const uint32_t RULE_PRIORITY_PER_NETWORK_NORMAL = 17000; |
| 45 | const uint32_t RULE_PRIORITY_DEFAULT_NETWORK = 19000; |
| 46 | const uint32_t RULE_PRIORITY_MAIN = 20000; |
Sreeram Ramachandran | 56afacf | 2014-05-28 15:07:00 -0700 | [diff] [blame] | 47 | // TODO: Uncomment once we are sure everything works. |
| 48 | #if 0 |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 49 | const uint32_t RULE_PRIORITY_UNREACHABLE = 21000; |
Sreeram Ramachandran | 56afacf | 2014-05-28 15:07:00 -0700 | [diff] [blame] | 50 | #endif |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 51 | |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 52 | // TODO: These should be turned into per-UID tables once the kernel supports UID-based routing. |
| 53 | const int ROUTE_TABLE_PRIVILEGED_LEGACY = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX - 901; |
| 54 | const int ROUTE_TABLE_LEGACY = RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX - 902; |
| 55 | |
Paul Jensen | a561e12 | 2014-06-12 16:46:37 -0400 | [diff] [blame] | 56 | std::map<std::string, uint32_t> interfaceToIndex; |
| 57 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 58 | uint32_t getRouteTableForInterface(const char* interface) { |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 59 | uint32_t index = if_nametoindex(interface); |
Paul Jensen | a561e12 | 2014-06-12 16:46:37 -0400 | [diff] [blame] | 60 | if (index) { |
| 61 | interfaceToIndex[interface] = index; |
| 62 | } else { |
| 63 | // If the interface goes away if_nametoindex() will return 0 but we still need to know |
| 64 | // the index so we can remove the rules and routes. |
| 65 | std::map<std::string, uint32_t>::iterator it = interfaceToIndex.find(interface); |
| 66 | if (it != interfaceToIndex.end()) |
| 67 | index = it->second; |
| 68 | } |
Sreeram Ramachandran | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 69 | return index ? index + RouteController::ROUTE_TABLE_OFFSET_FROM_INDEX : 0; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 72 | // Adds or removes a routing rule for IPv4 and IPv6. |
| 73 | // |
| 74 | // + If |table| is non-zero, the rule points at the specified routing table. Otherwise, the rule |
| 75 | // returns ENETUNREACH. |
| 76 | // + If |mask| is non-zero, the rule matches the specified fwmark and mask. Otherwise, |fwmark| is |
| 77 | // ignored. |
| 78 | // + If |interface| is non-NULL, the rule matches the specified outgoing interface. |
| 79 | bool runIpRuleCommand(const char* action, uint32_t priority, uint32_t table, uint32_t fwmark, |
| 80 | uint32_t mask, const char* interface) { |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 81 | char priorityString[UINT32_STRLEN]; |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 82 | snprintf(priorityString, sizeof(priorityString), "%u", priority); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 83 | |
| 84 | char tableString[UINT32_STRLEN]; |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 85 | snprintf(tableString, sizeof(tableString), "%u", table); |
| 86 | |
| 87 | char fwmarkString[sizeof("0x12345678/0x12345678")]; |
| 88 | snprintf(fwmarkString, sizeof(fwmarkString), "0x%x/0x%x", fwmark, mask); |
| 89 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 90 | const char* version[] = {"-4", "-6"}; |
| 91 | for (size_t i = 0; i < ARRAY_SIZE(version); ++i) { |
| 92 | int argc = 0; |
| 93 | const char* argv[16]; |
| 94 | |
| 95 | argv[argc++] = IP_PATH; |
| 96 | argv[argc++] = version[i]; |
| 97 | argv[argc++] = "rule"; |
| 98 | argv[argc++] = action; |
| 99 | argv[argc++] = "priority"; |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 100 | argv[argc++] = priorityString; |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 101 | if (table) { |
| 102 | argv[argc++] = "table"; |
| 103 | argv[argc++] = tableString; |
| 104 | } else { |
| 105 | argv[argc++] = "unreachable"; |
| 106 | } |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 107 | if (mask) { |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 108 | argv[argc++] = "fwmark"; |
Lorenzo Colitti | a10ac32 | 2014-04-11 18:26:17 +0900 | [diff] [blame] | 109 | argv[argc++] = fwmarkString; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 110 | } |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 111 | if (interface) { |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 112 | argv[argc++] = "oif"; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 113 | argv[argc++] = interface; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 114 | } |
| 115 | if (android_fork_execvp(argc, const_cast<char**>(argv), NULL, false, false)) { |
| 116 | return false; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | |
Sreeram Ramachandran | ab359fe | 2014-06-24 15:03:52 -0700 | [diff] [blame^] | 123 | #if 0 |
| 124 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 125 | // Adds or deletes an IPv4 or IPv6 route. |
| 126 | // Returns 0 on success or negative errno on failure. |
| 127 | int modifyIpRoute(uint16_t action, uint32_t table, const char* interface, const char* destination, |
| 128 | const char* nexthop) { |
| 129 | // At least the destination must be non-null. |
| 130 | if (!destination) { |
| 131 | return -EFAULT; |
| 132 | } |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 133 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 134 | // Parse the prefix. |
| 135 | uint8_t rawAddress[sizeof(in6_addr)]; |
| 136 | uint8_t family, prefixLength; |
| 137 | int rawLength = parsePrefix(destination, &family, rawAddress, sizeof(rawAddress), |
| 138 | &prefixLength); |
| 139 | if (rawLength < 0) { |
| 140 | return rawLength; |
| 141 | } |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 142 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 143 | if (static_cast<size_t>(rawLength) > sizeof(rawAddress)) { |
| 144 | return -ENOBUFS; // Cannot happen; parsePrefix only supports IPv4 and IPv6. |
| 145 | } |
| 146 | |
| 147 | // If an interface was specified, find the ifindex. |
| 148 | uint32_t ifindex; |
| 149 | if (interface) { |
| 150 | ifindex = if_nametoindex(interface); |
| 151 | if (!ifindex) { |
| 152 | return -ENODEV; |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 156 | // If a nexthop was specified, parse it as the same family as the prefix. |
| 157 | uint8_t rawNexthop[sizeof(in6_addr)]; |
| 158 | if (nexthop && !inet_pton(family, nexthop, rawNexthop)) { |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | |
| 162 | // Assemble a netlink request and put it in an array of iovec structures. |
| 163 | nlmsghdr nlmsg = { |
| 164 | .nlmsg_type = action, |
| 165 | .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK, |
| 166 | }; |
| 167 | rtmsg rtmsg = { |
| 168 | .rtm_protocol = RTPROT_STATIC, |
| 169 | .rtm_type = RTN_UNICAST, |
| 170 | .rtm_family = family, |
| 171 | .rtm_dst_len = prefixLength, |
| 172 | }; |
| 173 | rtattr rta_table = { U16_RTA_LENGTH(sizeof(table)), RTA_TABLE }; |
| 174 | rtattr rta_oif = { U16_RTA_LENGTH(sizeof(ifindex)), RTA_OIF }; |
| 175 | rtattr rta_dst = { U16_RTA_LENGTH(rawLength), RTA_DST }; |
| 176 | rtattr rta_gateway = { U16_RTA_LENGTH(rawLength), RTA_GATEWAY }; |
| 177 | if (action == RTM_NEWROUTE) { |
| 178 | nlmsg.nlmsg_flags |= (NLM_F_CREATE | NLM_F_EXCL); |
| 179 | } |
| 180 | |
| 181 | iovec iov[] = { |
| 182 | { &nlmsg, sizeof(nlmsg) }, |
| 183 | { &rtmsg, sizeof(rtmsg) }, |
| 184 | { &rta_table, sizeof(rta_table) }, |
| 185 | { &table, sizeof(table) }, |
| 186 | { &rta_dst, sizeof(rta_dst) }, |
| 187 | { rawAddress, static_cast<size_t>(rawLength) }, |
| 188 | { &rta_oif, interface ? sizeof(rta_oif) : 0 }, |
| 189 | { &ifindex, interface ? sizeof(interface) : 0 }, |
| 190 | { &rta_gateway, nexthop ? sizeof(rta_gateway) : 0 }, |
| 191 | { rawNexthop, nexthop ? static_cast<size_t>(rawLength) : 0 }, |
| 192 | }; |
| 193 | int iovlen = ARRAY_SIZE(iov); |
| 194 | |
| 195 | for (int i = 0; i < iovlen; ++i) { |
| 196 | nlmsg.nlmsg_len += iov[i].iov_len; |
| 197 | } |
| 198 | |
| 199 | int ret; |
| 200 | struct { |
| 201 | nlmsghdr msg; |
| 202 | nlmsgerr err; |
| 203 | } response; |
| 204 | |
| 205 | sockaddr_nl kernel = {AF_NETLINK, 0, 0, 0}; |
| 206 | int sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE); |
| 207 | if (sock != -1 && |
| 208 | connect(sock, reinterpret_cast<sockaddr *>(&kernel), sizeof(kernel)) != -1 && |
| 209 | writev(sock, iov, iovlen) != -1 && |
| 210 | (ret = recv(sock, &response, sizeof(response), 0)) != -1) { |
| 211 | if (ret == sizeof(response)) { |
| 212 | ret = response.err.error; // Netlink errors are negative errno. |
| 213 | } else { |
| 214 | ret = -EBADMSG; |
| 215 | } |
| 216 | } else { |
| 217 | ret = -errno; |
| 218 | } |
| 219 | |
| 220 | if (sock != -1) { |
| 221 | close(sock); |
| 222 | } |
| 223 | |
| 224 | return ret; |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Sreeram Ramachandran | ab359fe | 2014-06-24 15:03:52 -0700 | [diff] [blame^] | 227 | #else |
| 228 | |
| 229 | int modifyIpRoute(int action, uint32_t table, const char* interface, const char* destination, |
| 230 | const char* nexthop) { |
| 231 | char tableString[UINT32_STRLEN]; |
| 232 | snprintf(tableString, sizeof(tableString), "%u", table); |
| 233 | |
| 234 | int argc = 0; |
| 235 | const char* argv[16]; |
| 236 | |
| 237 | argv[argc++] = IP_PATH; |
| 238 | argv[argc++] = "route"; |
| 239 | argv[argc++] = action == RTM_NEWROUTE ? ADD : DEL; |
| 240 | argv[argc++] = "table"; |
| 241 | argv[argc++] = tableString; |
| 242 | if (destination) { |
| 243 | argv[argc++] = destination; |
| 244 | argv[argc++] = "dev"; |
| 245 | argv[argc++] = interface; |
| 246 | if (nexthop) { |
| 247 | argv[argc++] = "via"; |
| 248 | argv[argc++] = nexthop; |
| 249 | } |
| 250 | } |
| 251 | return android_fork_execvp(argc, const_cast<char**>(argv), NULL, false, false); |
| 252 | } |
| 253 | |
| 254 | #endif |
| 255 | |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 256 | bool modifyPerNetworkRules(unsigned netId, const char* interface, Permission permission, bool add, |
| 257 | bool modifyIptables) { |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 258 | uint32_t table = getRouteTableForInterface(interface); |
| 259 | if (!table) { |
| 260 | return false; |
| 261 | } |
| 262 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 263 | const char* action = add ? ADD : DEL; |
| 264 | |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 265 | Fwmark fwmark; |
| 266 | fwmark.permission = permission; |
| 267 | |
| 268 | Fwmark mask; |
| 269 | mask.permission = permission; |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 270 | |
| 271 | // A rule to route traffic based on a chosen outgoing interface. |
| 272 | // |
| 273 | // Supports apps that use SO_BINDTODEVICE or IP_PKTINFO options and the kernel that already |
| 274 | // knows the outgoing interface (typically for link-local communications). |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 275 | if (!runIpRuleCommand(action, RULE_PRIORITY_PER_NETWORK_INTERFACE, table, fwmark.intValue, |
| 276 | mask.intValue, interface)) { |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
| 280 | // A rule to route traffic based on the chosen network. |
| 281 | // |
| 282 | // This is for sockets that have not explicitly requested a particular network, but have been |
| 283 | // bound to one when they called connect(). This ensures that sockets connected on a particular |
| 284 | // network stay on that network even if the default network changes. |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 285 | fwmark.netId = netId; |
| 286 | mask.netId = FWMARK_NET_ID_MASK; |
| 287 | if (!runIpRuleCommand(action, RULE_PRIORITY_PER_NETWORK_NORMAL, table, fwmark.intValue, |
| 288 | mask.intValue, NULL)) { |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | // A rule to route traffic based on an explicitly chosen network. |
| 293 | // |
| 294 | // Supports apps that use the multinetwork APIs to restrict their traffic to a network. |
| 295 | // |
| 296 | // We don't really need to check the permission bits of the fwmark here, as they would've been |
| 297 | // checked at the time the netId was set into the fwmark, but we do so to be consistent. |
| 298 | fwmark.explicitlySelected = true; |
| 299 | mask.explicitlySelected = true; |
| 300 | if (!runIpRuleCommand(action, RULE_PRIORITY_PER_NETWORK_EXPLICIT, table, fwmark.intValue, |
| 301 | mask.intValue, NULL)) { |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 302 | return false; |
| 303 | } |
| 304 | |
| 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 | a481180 | 2014-04-10 12:10:24 -0700 | [diff] [blame] | 309 | // ping replies). |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 310 | // + Mark sockets that accept connections from this interface so that the connection stays on |
| 311 | // the same interface. |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 312 | if (modifyIptables) { |
| 313 | action = add ? "-A" : "-D"; |
| 314 | char markString[UINT32_HEX_STRLEN]; |
| 315 | snprintf(markString, sizeof(markString), "0x%x", netId); |
| 316 | if (execIptables(V4V6, "-t", "mangle", action, "INPUT", "-i", interface, "-j", "MARK", |
| 317 | "--set-mark", markString, NULL)) { |
| 318 | return false; |
| 319 | } |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | return true; |
| 323 | } |
| 324 | |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 325 | bool modifyDefaultNetworkRules(const char* interface, Permission permission, const char* action) { |
| 326 | uint32_t table = getRouteTableForInterface(interface); |
| 327 | if (!table) { |
| 328 | return false; |
| 329 | } |
| 330 | |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 331 | Fwmark fwmark; |
| 332 | fwmark.netId = 0; |
| 333 | fwmark.permission = permission; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 334 | |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 335 | Fwmark mask; |
| 336 | mask.netId = FWMARK_NET_ID_MASK; |
| 337 | mask.permission = permission; |
| 338 | |
| 339 | return runIpRuleCommand(action, RULE_PRIORITY_DEFAULT_NETWORK, table, fwmark.intValue, |
| 340 | mask.intValue, NULL); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 343 | // Adds or removes an IPv4 or IPv6 route to the specified table and, if it's directly-connected |
| 344 | // route, to the main table as well. |
| 345 | // Returns 0 on success or negative errno on failure. |
| 346 | int modifyRoute(const char* interface, const char* destination, const char* nexthop, |
Sreeram Ramachandran | ab359fe | 2014-06-24 15:03:52 -0700 | [diff] [blame^] | 347 | int action, RouteController::TableType tableType, unsigned /* uid */) { |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 348 | uint32_t table = 0; |
| 349 | switch (tableType) { |
| 350 | case RouteController::INTERFACE: { |
| 351 | table = getRouteTableForInterface(interface); |
| 352 | break; |
| 353 | } |
| 354 | case RouteController::LEGACY: { |
| 355 | // TODO: Use the UID to assign a unique table per UID instead of this fixed table. |
| 356 | table = ROUTE_TABLE_LEGACY; |
| 357 | break; |
| 358 | } |
| 359 | case RouteController::PRIVILEGED_LEGACY: { |
| 360 | // TODO: Use the UID to assign a unique table per UID instead of this fixed table. |
| 361 | table = ROUTE_TABLE_PRIVILEGED_LEGACY; |
| 362 | break; |
| 363 | } |
| 364 | } |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 365 | if (!table) { |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 366 | return -ESRCH; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 369 | int ret = modifyIpRoute(action, table, interface, destination, nexthop); |
| 370 | if (ret != 0) { |
| 371 | return ret; |
Sreeram Ramachandran | c921337 | 2014-04-16 12:32:18 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | // 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] | 375 | // let the kernel find it when validating nexthops when global routes are added. |
| 376 | if (!nexthop) { |
| 377 | ret = modifyIpRoute(action, RT_TABLE_MAIN, interface, destination, NULL); |
| 378 | // A failure with action == ADD && errno == EEXIST means that the route already exists in |
| 379 | // the main table, perhaps because the kernel added it automatically as part of adding the |
| 380 | // IP address to the interface. Ignore this, but complain about everything else. |
| 381 | if (ret != 0 && !(action == RTM_NEWROUTE && ret == -EEXIST)) { |
| 382 | return ret; |
| 383 | } |
Sreeram Ramachandran | c921337 | 2014-04-16 12:32:18 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 386 | return 0; |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Sreeram Ramachandran | 92b66c4 | 2014-04-15 14:28:55 -0700 | [diff] [blame] | 389 | bool flushRoutes(const char* interface) { |
| 390 | uint32_t table = getRouteTableForInterface(interface); |
| 391 | if (!table) { |
| 392 | return false; |
| 393 | } |
Paul Jensen | a561e12 | 2014-06-12 16:46:37 -0400 | [diff] [blame] | 394 | interfaceToIndex.erase(interface); |
Sreeram Ramachandran | 92b66c4 | 2014-04-15 14:28:55 -0700 | [diff] [blame] | 395 | |
Lorenzo Colitti | 357e562 | 2014-06-17 16:14:17 +0900 | [diff] [blame] | 396 | char tableString[UINT32_STRLEN]; |
| 397 | snprintf(tableString, sizeof(tableString), "%u", table); |
| 398 | |
| 399 | const char* version[] = {"-4", "-6"}; |
| 400 | for (size_t i = 0; i < ARRAY_SIZE(version); ++i) { |
| 401 | const char* argv[] = { |
| 402 | IP_PATH, |
| 403 | version[i], |
| 404 | "route" |
| 405 | "flush", |
| 406 | "table", |
| 407 | tableString, |
| 408 | }; |
| 409 | int argc = ARRAY_SIZE(argv); |
| 410 | |
| 411 | if (!android_fork_execvp(argc, const_cast<char**>(argv), NULL, false, false)) { |
| 412 | return false; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | return true; |
Sreeram Ramachandran | 92b66c4 | 2014-04-15 14:28:55 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 419 | } // namespace |
| 420 | |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 421 | void RouteController::Init() { |
| 422 | // Add a new rule to look up the 'main' table, with the same selectors as the "default network" |
| 423 | // rule, but with a lower priority. Since the default network rule points to a table with a |
| 424 | // default route, the rule we're adding will never be used for normal routing lookups. However, |
| 425 | // the kernel may fall-through to it to find directly-connected routes when it validates that a |
| 426 | // nexthop (in a route being added) is reachable. |
Sreeram Ramachandran | 122f581 | 2014-05-11 20:29:49 -0700 | [diff] [blame] | 427 | Fwmark fwmark; |
| 428 | fwmark.netId = 0; |
| 429 | |
| 430 | Fwmark mask; |
| 431 | mask.netId = FWMARK_NET_ID_MASK; |
| 432 | |
| 433 | runIpRuleCommand(ADD, RULE_PRIORITY_MAIN, RT_TABLE_MAIN, fwmark.intValue, mask.intValue, NULL); |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 434 | |
Sreeram Ramachandran | 38b7af1 | 2014-05-22 14:21:49 -0700 | [diff] [blame] | 435 | // Add rules to allow lookup of legacy routes. |
| 436 | // |
| 437 | // TODO: Remove these once the kernel supports UID-based routing. Instead, add them on demand |
| 438 | // when routes are added. |
| 439 | fwmark.netId = 0; |
| 440 | mask.netId = 0; |
| 441 | |
| 442 | fwmark.explicitlySelected = false; |
| 443 | mask.explicitlySelected = true; |
| 444 | |
| 445 | runIpRuleCommand(ADD, RULE_PRIORITY_LEGACY, ROUTE_TABLE_LEGACY, fwmark.intValue, mask.intValue, |
| 446 | NULL); |
| 447 | |
| 448 | fwmark.permission = PERMISSION_CONNECTIVITY_INTERNAL; |
| 449 | mask.permission = PERMISSION_CONNECTIVITY_INTERNAL; |
| 450 | |
| 451 | runIpRuleCommand(ADD, RULE_PRIORITY_PRIVILEGED_LEGACY, ROUTE_TABLE_PRIVILEGED_LEGACY, |
| 452 | fwmark.intValue, mask.intValue, NULL); |
| 453 | |
Sreeram Ramachandran | 8fe9c8e | 2014-04-16 12:08:05 -0700 | [diff] [blame] | 454 | // TODO: Uncomment once we are sure everything works. |
| 455 | #if 0 |
| 456 | // Add a rule to preempt the pre-defined "from all lookup main" rule. This ensures that packets |
| 457 | // that are already marked with a specific NetId don't fall-through to the main table. |
| 458 | runIpRuleCommand(ADD, RULE_PRIORITY_UNREACHABLE, 0, 0, 0, NULL); |
| 459 | #endif |
| 460 | } |
| 461 | |
Paul Jensen | ae37e8a | 2014-04-28 10:35:51 -0400 | [diff] [blame] | 462 | bool RouteController::addInterfaceToNetwork(unsigned netId, const char* interface, |
| 463 | Permission permission) { |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 464 | return modifyPerNetworkRules(netId, interface, permission, true, true); |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Paul Jensen | ae37e8a | 2014-04-28 10:35:51 -0400 | [diff] [blame] | 467 | bool RouteController::removeInterfaceFromNetwork(unsigned netId, const char* interface, |
| 468 | Permission permission) { |
Sreeram Ramachandran | 92b66c4 | 2014-04-15 14:28:55 -0700 | [diff] [blame] | 469 | return modifyPerNetworkRules(netId, interface, permission, false, true) && |
| 470 | flushRoutes(interface); |
Sreeram Ramachandran | 379bd33 | 2014-04-10 19:58:06 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | bool RouteController::modifyNetworkPermission(unsigned netId, const char* interface, |
| 474 | Permission oldPermission, Permission newPermission) { |
| 475 | // Add the new rules before deleting the old ones, to avoid race conditions. |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 476 | return modifyPerNetworkRules(netId, interface, newPermission, true, false) && |
| 477 | modifyPerNetworkRules(netId, interface, oldPermission, false, false); |
| 478 | } |
| 479 | |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 480 | bool RouteController::addToDefaultNetwork(const char* interface, Permission permission) { |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 481 | return modifyDefaultNetworkRules(interface, permission, ADD); |
| 482 | } |
| 483 | |
Sreeram Ramachandran | 7260407 | 2014-05-21 13:19:43 -0700 | [diff] [blame] | 484 | bool RouteController::removeFromDefaultNetwork(const char* interface, Permission permission) { |
Sreeram Ramachandran | 9c0d313 | 2014-04-10 20:35:04 -0700 | [diff] [blame] | 485 | return modifyDefaultNetworkRules(interface, permission, DEL); |
Sreeram Ramachandran | 5c181bf | 2014-04-07 14:10:04 -0700 | [diff] [blame] | 486 | } |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 487 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 488 | int RouteController::addRoute(const char* interface, const char* destination, |
| 489 | const char* nexthop, TableType tableType, unsigned uid) { |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 490 | return modifyRoute(interface, destination, nexthop, RTM_NEWROUTE, tableType, uid); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Lorenzo Colitti | f7fc8ec | 2014-06-18 00:41:58 +0900 | [diff] [blame] | 493 | int RouteController::removeRoute(const char* interface, const char* destination, |
| 494 | const char* nexthop, TableType tableType, unsigned uid) { |
Lorenzo Colitti | ba25df9 | 2014-06-18 00:22:17 +0900 | [diff] [blame] | 495 | return modifyRoute(interface, destination, nexthop, RTM_DELROUTE, tableType, uid); |
Sreeram Ramachandran | 7619e1b | 2014-04-15 14:23:08 -0700 | [diff] [blame] | 496 | } |