Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | */ |
Bernie Innocenti | 51a0e0f | 2018-10-05 20:24:06 +0900 | [diff] [blame] | 16 | |
| 17 | #include "ClatdController.h" |
| 18 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 19 | #include <map> |
| 20 | #include <string> |
| 21 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 22 | #include <arpa/inet.h> |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 23 | #include <errno.h> |
Maciej Żenczykowski | c8c38aa | 2019-03-29 01:24:51 -0700 | [diff] [blame^] | 24 | #include <net/if.h> |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 25 | #include <netinet/in.h> |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 26 | #include <spawn.h> |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <sys/wait.h> |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 29 | #include <unistd.h> |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 30 | |
| 31 | #define LOG_TAG "ClatdController" |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 32 | #include <log/log.h> |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 33 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 34 | #include "android-base/unique_fd.h" |
| 35 | |
| 36 | extern "C" { |
| 37 | #include "netutils/checksum.h" |
| 38 | } |
| 39 | |
Lorenzo Colitti | 45d3dd0 | 2014-06-09 14:09:20 +0900 | [diff] [blame] | 40 | #include "Fwmark.h" |
Paul Jensen | 84c1d03 | 2014-05-30 13:29:41 -0400 | [diff] [blame] | 41 | #include "NetdConstants.h" |
| 42 | #include "NetworkController.h" |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 43 | #include "netid_client.h" |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 44 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 45 | static const char* kClatdPath = "/system/bin/clatd"; |
| 46 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 47 | // For historical reasons, start with 192.0.0.4, and after that, use all subsequent addresses in |
| 48 | // 192.0.0.0/29 (RFC 7335). |
| 49 | static const char* kV4AddrString = "192.0.0.4"; |
| 50 | static const in_addr kV4Addr = {inet_addr(kV4AddrString)}; |
| 51 | static const int kV4AddrLen = 29; |
| 52 | |
| 53 | using android::base::unique_fd; |
| 54 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 55 | namespace android { |
| 56 | namespace net { |
| 57 | |
Paul Jensen | 84c1d03 | 2014-05-30 13:29:41 -0400 | [diff] [blame] | 58 | ClatdController::ClatdController(NetworkController* controller) |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 59 | : mNetCtrl(controller) { |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | ClatdController::~ClatdController() { |
| 63 | } |
| 64 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 65 | bool ClatdController::isIpv4AddressFree(in_addr_t addr) { |
| 66 | int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 67 | if (s == -1) { |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | // Attempt to connect to the address. If the connection succeeds and getsockname returns the |
| 72 | // same then the address is already assigned to the system and we can't use it. |
| 73 | struct sockaddr_in sin = {.sin_family = AF_INET, .sin_addr = {addr}, .sin_port = 53}; |
| 74 | socklen_t len = sizeof(sin); |
| 75 | bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 && |
| 76 | getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) && |
| 77 | sin.sin_addr.s_addr == addr; |
| 78 | |
| 79 | close(s); |
| 80 | return !inuse; |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 81 | } |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 82 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 83 | // Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order. |
| 84 | // ip - the IP address from the configuration file |
| 85 | // prefixlen - the length of the prefix from which addresses may be selected. |
| 86 | // returns: the IPv4 address, or INADDR_NONE if no addresses were available |
| 87 | in_addr_t ClatdController::selectIpv4Address(const in_addr ip, int16_t prefixlen) { |
| 88 | // Don't accept prefixes that are too large because we scan addresses one by one. |
| 89 | if (prefixlen < 16 || prefixlen > 32) { |
| 90 | return INADDR_NONE; |
| 91 | } |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 92 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 93 | // All these are in host byte order. |
| 94 | in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen); |
| 95 | in_addr_t ipv4 = ntohl(ip.s_addr); |
| 96 | in_addr_t first_ipv4 = ipv4; |
| 97 | in_addr_t prefix = ipv4 & mask; |
| 98 | |
| 99 | // Pick the first IPv4 address in the pool, wrapping around if necessary. |
| 100 | // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0. |
| 101 | do { |
| 102 | if (isIpv4AddressFreeFunc(htonl(ipv4))) { |
| 103 | return htonl(ipv4); |
| 104 | } |
| 105 | ipv4 = prefix | ((ipv4 + 1) & ~mask); |
| 106 | } while (ipv4 != first_ipv4); |
| 107 | |
| 108 | return INADDR_NONE; |
| 109 | } |
| 110 | |
| 111 | // Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix. |
| 112 | void ClatdController::makeChecksumNeutral(in6_addr* v6, const in_addr v4, |
| 113 | const in6_addr& nat64Prefix) { |
| 114 | // Fill last 8 bytes of IPv6 address with random bits. |
| 115 | arc4random_buf(&v6->s6_addr[8], 8); |
| 116 | |
| 117 | // Make the IID checksum-neutral. That is, make it so that: |
| 118 | // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6) |
| 119 | // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4): |
| 120 | // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix) |
| 121 | // Do this by adjusting the two bytes in the middle of the IID. |
| 122 | |
| 123 | uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12]; |
| 124 | |
| 125 | uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4)); |
| 126 | uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) + |
| 127 | ip_checksum_add(0, v6, sizeof(*v6)); |
| 128 | |
| 129 | uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2); |
| 130 | v6->s6_addr[11] = delta >> 8; |
| 131 | v6->s6_addr[12] = delta & 0xff; |
| 132 | } |
| 133 | |
| 134 | // Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix. |
| 135 | int ClatdController::generateIpv6Address(const char* iface, const in_addr v4, |
| 136 | const in6_addr& nat64Prefix, in6_addr* v6) { |
| 137 | unique_fd s(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 138 | if (s == -1) return -errno; |
| 139 | |
| 140 | if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) { |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 141 | return -errno; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 142 | } |
| 143 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 144 | sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix}; |
| 145 | if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) { |
| 146 | return -errno; |
| 147 | } |
| 148 | |
| 149 | socklen_t len = sizeof(sin6); |
| 150 | if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) { |
| 151 | return -errno; |
| 152 | } |
| 153 | |
| 154 | *v6 = sin6.sin6_addr; |
| 155 | |
| 156 | if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) || |
| 157 | IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) { |
| 158 | return -ENETUNREACH; |
| 159 | } |
| 160 | |
| 161 | makeChecksumNeutral(v6, v4, nat64Prefix); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | // Finds the tracker of the clatd running on interface |interface|, or nullptr if clatd has not been |
| 167 | // started on |interface|. |
| 168 | ClatdController::ClatdTracker* ClatdController::getClatdTracker(const std::string& interface) { |
| 169 | auto it = mClatdTrackers.find(interface); |
| 170 | return (it == mClatdTrackers.end() ? nullptr : &it->second); |
| 171 | } |
| 172 | |
| 173 | // Initializes a ClatdTracker for the specified interface. |
| 174 | int ClatdController::ClatdTracker::init(const std::string& interface, |
| 175 | const std::string& nat64Prefix) { |
| 176 | netId = netCtrl->getNetworkForInterface(interface.c_str()); |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 177 | if (netId == NETID_UNSET) { |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 178 | ALOGE("Interface %s not assigned to any netId", interface.c_str()); |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 179 | errno = ENODEV; |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 180 | return -errno; |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 181 | } |
| 182 | |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 183 | fwmark.netId = netId; |
| 184 | fwmark.explicitlySelected = true; |
| 185 | fwmark.protectedFromVpn = true; |
| 186 | fwmark.permission = PERMISSION_SYSTEM; |
| 187 | |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 188 | snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 189 | snprintf(netIdString, sizeof(netIdString), "%u", netId); |
Maciej Żenczykowski | c8c38aa | 2019-03-29 01:24:51 -0700 | [diff] [blame^] | 190 | ifIndex = if_nametoindex(interface.c_str()); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 191 | strlcpy(iface, interface.c_str(), sizeof(iface)); |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 192 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 193 | // Pass in everything that clatd needs: interface, a netid to use for DNS lookups, a fwmark for |
| 194 | // outgoing packets, the NAT64 prefix, and the IPv4 and IPv6 addresses. |
| 195 | // Validate the prefix and strip off the prefix length. |
| 196 | uint8_t family; |
| 197 | uint8_t prefixLen; |
| 198 | int res = parsePrefix(nat64Prefix.c_str(), &family, &dst, sizeof(dst), &prefixLen); |
| 199 | // clatd only supports /96 prefixes. |
| 200 | if (res != sizeof(dst)) return res; |
| 201 | if (family != AF_INET6) return -EAFNOSUPPORT; |
| 202 | if (prefixLen != 96) return -EINVAL; |
| 203 | if (!inet_ntop(AF_INET6, &dst, dstString, sizeof(dstString))) return -errno; |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 204 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 205 | // Pick an IPv4 address. |
| 206 | // TODO: this picks the address based on other addresses that are assigned to interfaces, but |
| 207 | // the address is only actually assigned to an interface once clatd starts up. So we could end |
| 208 | // up with two clatd instances with the same IPv4 address. |
| 209 | // Stop doing this and instead pick a free one from the kV4Addr pool. |
Maciej Żenczykowski | 55cacfb | 2019-03-30 02:01:35 -0700 | [diff] [blame] | 210 | v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)}; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 211 | if (v4.s_addr == INADDR_NONE) { |
| 212 | ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen); |
| 213 | return -EADDRNOTAVAIL; |
| 214 | } |
| 215 | if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno; |
| 216 | |
| 217 | // Generate a checksum-neutral IID. |
| 218 | if (generateIpv6Address(iface, v4, dst, &v6)) { |
| 219 | ALOGE("Unable to find global source address on %s for %s", iface, dstString); |
| 220 | return -EADDRNOTAVAIL; |
| 221 | } |
| 222 | if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno; |
| 223 | |
| 224 | ALOGD("starting clatd on %s v4=%s v6=%s dst=%s", iface, v4Str, v6Str, dstString); |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix, |
| 229 | std::string* v6Str) { |
| 230 | ClatdTracker* existing = getClatdTracker(interface); |
| 231 | if (existing != nullptr) { |
| 232 | ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str()); |
| 233 | errno = EBUSY; |
| 234 | return -errno; |
| 235 | } |
| 236 | |
| 237 | ClatdTracker tracker(mNetCtrl); |
| 238 | if (int ret = tracker.init(interface, nat64Prefix)) { |
| 239 | return ret; |
| 240 | } |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 241 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 242 | std::string progname("clatd-"); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 243 | progname += tracker.iface; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 244 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 245 | // clang-format off |
| 246 | char* args[] = {(char*) progname.c_str(), |
| 247 | (char*) "-i", tracker.iface, |
| 248 | (char*) "-n", tracker.netIdString, |
| 249 | (char*) "-m", tracker.fwmarkString, |
| 250 | (char*) "-p", tracker.dstString, |
| 251 | (char*) "-4", tracker.v4Str, |
| 252 | (char*) "-6", tracker.v6Str, |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 253 | nullptr}; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 254 | // clang-format on |
| 255 | |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 256 | // Specify no flags and no actions, posix_spawn will use vfork and is |
| 257 | // guaranteed to return only once exec has been called. |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 258 | int res = posix_spawn(&tracker.pid, kClatdPath, nullptr, nullptr, args, nullptr); |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 259 | if (res) { |
| 260 | ALOGE("posix_spawn failed (%s)", strerror(res)); |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 261 | return -res; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 262 | } |
| 263 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 264 | mClatdTrackers[interface] = tracker; |
| 265 | ALOGD("clatd started on %s", interface.c_str()); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 266 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 267 | *v6Str = tracker.v6Str; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 271 | int ClatdController::stopClatd(const std::string& interface) { |
| 272 | ClatdTracker* tracker = getClatdTracker(interface); |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 273 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 274 | if (tracker == nullptr) { |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 275 | ALOGE("clatd already stopped"); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 276 | return -ENODEV; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 277 | } |
| 278 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 279 | ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str()); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 280 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 281 | kill(tracker->pid, SIGTERM); |
| 282 | waitpid(tracker->pid, nullptr, 0); |
| 283 | mClatdTrackers.erase(interface); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 284 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 285 | ALOGD("clatd on %s stopped", interface.c_str()); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 290 | auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree; |
| 291 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 292 | } // namespace net |
| 293 | } // namespace android |