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