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