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