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 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 17 | #include <map> |
| 18 | #include <string> |
| 19 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 20 | #include <arpa/inet.h> |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 21 | #include <errno.h> |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 22 | #include <linux/if_arp.h> |
Maciej Żenczykowski | c8c38aa | 2019-03-29 01:24:51 -0700 | [diff] [blame] | 23 | #include <net/if.h> |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 24 | #include <netinet/in.h> |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 25 | #include <spawn.h> |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #include <sys/wait.h> |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 28 | #include <unistd.h> |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 29 | |
| 30 | #define LOG_TAG "ClatdController" |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 31 | #include <log/log.h> |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 32 | |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 33 | #include "ClatdController.h" |
| 34 | |
| 35 | #include "android-base/properties.h" |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 36 | #include "android-base/unique_fd.h" |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 37 | #include "bpf/BpfMap.h" |
| 38 | #include "netdbpf/bpf_shared.h" |
| 39 | #include "netdutils/DumpWriter.h" |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 40 | |
| 41 | extern "C" { |
| 42 | #include "netutils/checksum.h" |
| 43 | } |
| 44 | |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 45 | #include "ClatUtils.h" |
Lorenzo Colitti | 45d3dd0 | 2014-06-09 14:09:20 +0900 | [diff] [blame] | 46 | #include "Fwmark.h" |
Paul Jensen | 84c1d03 | 2014-05-30 13:29:41 -0400 | [diff] [blame] | 47 | #include "NetdConstants.h" |
| 48 | #include "NetworkController.h" |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 49 | #include "netid_client.h" |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 50 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 51 | static const char* kClatdPath = "/system/bin/clatd"; |
| 52 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 53 | // For historical reasons, start with 192.0.0.4, and after that, use all subsequent addresses in |
| 54 | // 192.0.0.0/29 (RFC 7335). |
| 55 | static const char* kV4AddrString = "192.0.0.4"; |
| 56 | static const in_addr kV4Addr = {inet_addr(kV4AddrString)}; |
| 57 | static const int kV4AddrLen = 29; |
| 58 | |
| 59 | using android::base::unique_fd; |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 60 | using android::bpf::BpfMap; |
| 61 | using android::netdutils::DumpWriter; |
| 62 | using android::netdutils::ScopedIndent; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 63 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 64 | namespace android { |
| 65 | namespace net { |
| 66 | |
Paul Jensen | 84c1d03 | 2014-05-30 13:29:41 -0400 | [diff] [blame] | 67 | ClatdController::ClatdController(NetworkController* controller) |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 68 | : mNetCtrl(controller) { |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | ClatdController::~ClatdController() { |
| 72 | } |
| 73 | |
Maciej Żenczykowski | 5628027 | 2019-03-30 03:32:51 -0700 | [diff] [blame^] | 74 | void ClatdController::init(void) { |
| 75 | std::lock_guard guard(mutex); |
| 76 | |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 77 | // TODO: should refactor into separate function for testability |
| 78 | if (bpf::getBpfSupportLevel() == bpf::BpfLevel::NONE) { |
| 79 | ALOGI("Pre-4.9 kernel or pre-P api shipping level - disabling clat ebpf."); |
| 80 | mClatEbpfMode = ClatEbpfDisabled; |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // We know the device initially shipped with at least P..., |
| 85 | // but did it ship with at least Q? |
| 86 | |
| 87 | uint64_t api_level = base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0); |
| 88 | if (api_level == 0) { |
| 89 | ALOGE("Cannot determine initial API level of the device."); |
| 90 | api_level = base::GetUintProperty<uint64_t>("ro.build.version.sdk", 0); |
| 91 | } |
| 92 | |
| 93 | // Note: MINIMUM_API_REQUIRED is for eBPF as a whole and is thus P |
| 94 | if (api_level > bpf::MINIMUM_API_REQUIRED) { |
| 95 | ALOGI("4.9+ kernel and device shipped with Q+ - clat ebpf should work."); |
| 96 | mClatEbpfMode = ClatEbpfEnabled; |
| 97 | } else { |
| 98 | // We cannot guarantee that 4.9-P kernels will include NET_CLS_BPF support. |
| 99 | ALOGI("4.9+ kernel and device shipped with P - clat ebpf might work."); |
| 100 | mClatEbpfMode = ClatEbpfMaybe; |
| 101 | } |
| 102 | |
| 103 | int rv = openNetlinkSocket(); |
| 104 | if (rv < 0) { |
| 105 | ALOGE("openNetlinkSocket() failure: %s", strerror(-rv)); |
| 106 | mClatEbpfMode = ClatEbpfDisabled; |
| 107 | return; |
| 108 | } |
| 109 | mNetlinkFd.reset(rv); |
| 110 | |
| 111 | rv = getClatIngressMapFd(); |
| 112 | if (rv < 0) { |
| 113 | ALOGE("getClatIngressMapFd() failure: %s", strerror(-rv)); |
| 114 | mClatEbpfMode = ClatEbpfDisabled; |
| 115 | mNetlinkFd.reset(-1); |
| 116 | return; |
| 117 | } |
| 118 | mClatIngressMap.reset(rv); |
| 119 | |
| 120 | int netlinkFd = mNetlinkFd.get(); |
| 121 | |
| 122 | // TODO: perhaps this initial cleanup should be in its own function? |
| 123 | const auto del = [&netlinkFd](const ClatIngressKey& key, |
| 124 | const BpfMap<ClatIngressKey, ClatIngressValue>&) { |
| 125 | ALOGW("Removing stale clat config on interface %d.", key.iif); |
| 126 | int rv = tcQdiscDelDevClsact(netlinkFd, key.iif); |
| 127 | if (rv < 0) ALOGE("tcQdiscDelDevClsact() failure: %s", strerror(-rv)); |
| 128 | return netdutils::status::ok; // keep on going regardless |
| 129 | }; |
| 130 | auto ret = mClatIngressMap.iterate(del); |
| 131 | if (!isOk(ret)) ALOGE("mClatIngressMap.iterate() failure: %s", strerror(ret.code())); |
| 132 | ret = mClatIngressMap.clear(); |
| 133 | if (!isOk(ret)) ALOGE("mClatIngressMap.clear() failure: %s", strerror(ret.code())); |
| 134 | } |
| 135 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 136 | bool ClatdController::isIpv4AddressFree(in_addr_t addr) { |
| 137 | int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 138 | if (s == -1) { |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | // Attempt to connect to the address. If the connection succeeds and getsockname returns the |
| 143 | // same then the address is already assigned to the system and we can't use it. |
| 144 | struct sockaddr_in sin = {.sin_family = AF_INET, .sin_addr = {addr}, .sin_port = 53}; |
| 145 | socklen_t len = sizeof(sin); |
| 146 | bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 && |
| 147 | getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) && |
| 148 | sin.sin_addr.s_addr == addr; |
| 149 | |
| 150 | close(s); |
| 151 | return !inuse; |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 152 | } |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 153 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 154 | // Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order. |
| 155 | // ip - the IP address from the configuration file |
| 156 | // prefixlen - the length of the prefix from which addresses may be selected. |
| 157 | // returns: the IPv4 address, or INADDR_NONE if no addresses were available |
| 158 | in_addr_t ClatdController::selectIpv4Address(const in_addr ip, int16_t prefixlen) { |
| 159 | // Don't accept prefixes that are too large because we scan addresses one by one. |
| 160 | if (prefixlen < 16 || prefixlen > 32) { |
| 161 | return INADDR_NONE; |
| 162 | } |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 163 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 164 | // All these are in host byte order. |
| 165 | in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen); |
| 166 | in_addr_t ipv4 = ntohl(ip.s_addr); |
| 167 | in_addr_t first_ipv4 = ipv4; |
| 168 | in_addr_t prefix = ipv4 & mask; |
| 169 | |
| 170 | // Pick the first IPv4 address in the pool, wrapping around if necessary. |
| 171 | // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0. |
| 172 | do { |
| 173 | if (isIpv4AddressFreeFunc(htonl(ipv4))) { |
| 174 | return htonl(ipv4); |
| 175 | } |
| 176 | ipv4 = prefix | ((ipv4 + 1) & ~mask); |
| 177 | } while (ipv4 != first_ipv4); |
| 178 | |
| 179 | return INADDR_NONE; |
| 180 | } |
| 181 | |
| 182 | // Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix. |
| 183 | void ClatdController::makeChecksumNeutral(in6_addr* v6, const in_addr v4, |
| 184 | const in6_addr& nat64Prefix) { |
| 185 | // Fill last 8 bytes of IPv6 address with random bits. |
| 186 | arc4random_buf(&v6->s6_addr[8], 8); |
| 187 | |
| 188 | // Make the IID checksum-neutral. That is, make it so that: |
| 189 | // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6) |
| 190 | // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4): |
| 191 | // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix) |
| 192 | // Do this by adjusting the two bytes in the middle of the IID. |
| 193 | |
| 194 | uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12]; |
| 195 | |
| 196 | uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4)); |
| 197 | uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) + |
| 198 | ip_checksum_add(0, v6, sizeof(*v6)); |
| 199 | |
| 200 | uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2); |
| 201 | v6->s6_addr[11] = delta >> 8; |
| 202 | v6->s6_addr[12] = delta & 0xff; |
| 203 | } |
| 204 | |
| 205 | // Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix. |
| 206 | int ClatdController::generateIpv6Address(const char* iface, const in_addr v4, |
| 207 | const in6_addr& nat64Prefix, in6_addr* v6) { |
| 208 | unique_fd s(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 209 | if (s == -1) return -errno; |
| 210 | |
| 211 | if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) { |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 212 | return -errno; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 213 | } |
| 214 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 215 | sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix}; |
| 216 | if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) { |
| 217 | return -errno; |
| 218 | } |
| 219 | |
| 220 | socklen_t len = sizeof(sin6); |
| 221 | if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) { |
| 222 | return -errno; |
| 223 | } |
| 224 | |
| 225 | *v6 = sin6.sin6_addr; |
| 226 | |
| 227 | if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) || |
| 228 | IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) { |
| 229 | return -ENETUNREACH; |
| 230 | } |
| 231 | |
| 232 | makeChecksumNeutral(v6, v4, nat64Prefix); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 237 | void ClatdController::maybeStartBpf(const ClatdTracker& tracker) { |
| 238 | if (mClatEbpfMode == ClatEbpfDisabled) return; |
| 239 | |
| 240 | int rv = hardwareAddressType(tracker.iface); |
| 241 | if (rv < 0) { |
| 242 | ALOGE("hardwareAddressType(%s[%d]) failure: %s", tracker.iface, tracker.ifIndex, |
| 243 | strerror(-rv)); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | bool isEthernet; |
| 248 | switch (rv) { |
| 249 | case ARPHRD_ETHER: |
| 250 | isEthernet = true; |
| 251 | break; |
| 252 | case ARPHRD_RAWIP: // in Linux 4.14+ rmnet support was upstreamed and this is 519 |
| 253 | case 530: // this is ARPHRD_RAWIP on some Android 4.9 kernels with rmnet |
| 254 | isEthernet = false; |
| 255 | break; |
| 256 | default: |
| 257 | ALOGE("hardwareAddressType(%s[%d]) returned unknown type %d.", tracker.iface, |
| 258 | tracker.ifIndex, rv); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | rv = getClatIngressProgFd(isEthernet); |
| 263 | if (rv < 0) { |
| 264 | ALOGE("getClatIngressProgFd(%d) failure: %s", isEthernet, strerror(-rv)); |
| 265 | return; |
| 266 | } |
| 267 | unique_fd progFd(rv); |
| 268 | |
| 269 | ClatIngressKey key = { |
| 270 | .iif = tracker.ifIndex, |
| 271 | .pfx96 = tracker.pfx96, |
| 272 | .local6 = tracker.v6, |
| 273 | }; |
| 274 | ClatIngressValue value = { |
| 275 | // Redirect the mangled packets to the same interface so we can see them in tcpdump. |
| 276 | // TODO: move the tun interface creation to netd, and use that ifindex instead. |
| 277 | // TODO: move all the clat code to eBPF and remove the tun interface entirely. |
| 278 | .oif = tracker.ifIndex, |
| 279 | .local4 = tracker.v4, |
| 280 | }; |
| 281 | |
| 282 | auto ret = mClatIngressMap.writeValue(key, value, BPF_ANY); |
| 283 | if (!isOk(ret)) { |
| 284 | ALOGE("mClatIngress.Map.writeValue failure: %s", strerror(ret.code())); |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | // We do tc setup *after* populating map, so scanning through map |
| 289 | // can always be used to tell us what needs cleanup. |
| 290 | |
| 291 | rv = tcQdiscAddDevClsact(mNetlinkFd, tracker.ifIndex); |
| 292 | if (rv) { |
| 293 | ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface, |
| 294 | strerror(-rv)); |
| 295 | ret = mClatIngressMap.deleteValue(key); |
| 296 | if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code())); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | rv = tcFilterAddDevBpf(mNetlinkFd, tracker.ifIndex, progFd, isEthernet); |
| 301 | if (rv) { |
| 302 | if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) { |
| 303 | ALOGI("tcFilterAddDevBpf(%d[%s], %d): %s", tracker.ifIndex, tracker.iface, isEthernet, |
| 304 | strerror(-rv)); |
| 305 | } else { |
| 306 | ALOGE("tcFilterAddDevBpf(%d[%s], %d) failure: %s", tracker.ifIndex, tracker.iface, |
| 307 | isEthernet, strerror(-rv)); |
| 308 | } |
| 309 | rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex); |
| 310 | if (rv) |
| 311 | ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface, |
| 312 | strerror(-rv)); |
| 313 | ret = mClatIngressMap.deleteValue(key); |
| 314 | if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code())); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | // success |
| 319 | } |
| 320 | |
| 321 | void ClatdController::maybeStopBpf(const ClatdTracker& tracker) { |
| 322 | if (mClatEbpfMode == ClatEbpfDisabled) return; |
| 323 | |
| 324 | // No need to remove filter, since we remove qdisc it is attached to, |
| 325 | // which automatically removes everything attached to the qdisc. |
| 326 | int rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex); |
| 327 | if (rv < 0) |
| 328 | ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface, |
| 329 | strerror(-rv)); |
| 330 | |
| 331 | // We cleanup map last, so scanning through map can be used to |
| 332 | // determine what still needs cleanup. |
| 333 | |
| 334 | ClatIngressKey key = { |
| 335 | .iif = tracker.ifIndex, |
| 336 | .pfx96 = tracker.pfx96, |
| 337 | .local6 = tracker.v6, |
| 338 | }; |
| 339 | |
| 340 | auto ret = mClatIngressMap.deleteValue(key); |
| 341 | if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code())); |
| 342 | } |
| 343 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 344 | // Finds the tracker of the clatd running on interface |interface|, or nullptr if clatd has not been |
| 345 | // started on |interface|. |
| 346 | ClatdController::ClatdTracker* ClatdController::getClatdTracker(const std::string& interface) { |
| 347 | auto it = mClatdTrackers.find(interface); |
| 348 | return (it == mClatdTrackers.end() ? nullptr : &it->second); |
| 349 | } |
| 350 | |
| 351 | // Initializes a ClatdTracker for the specified interface. |
| 352 | int ClatdController::ClatdTracker::init(const std::string& interface, |
| 353 | const std::string& nat64Prefix) { |
| 354 | netId = netCtrl->getNetworkForInterface(interface.c_str()); |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 355 | if (netId == NETID_UNSET) { |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 356 | ALOGE("Interface %s not assigned to any netId", interface.c_str()); |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 357 | errno = ENODEV; |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 358 | return -errno; |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 359 | } |
| 360 | |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 361 | fwmark.netId = netId; |
| 362 | fwmark.explicitlySelected = true; |
| 363 | fwmark.protectedFromVpn = true; |
| 364 | fwmark.permission = PERMISSION_SYSTEM; |
| 365 | |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 366 | snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 367 | snprintf(netIdString, sizeof(netIdString), "%u", netId); |
Maciej Żenczykowski | c8c38aa | 2019-03-29 01:24:51 -0700 | [diff] [blame] | 368 | ifIndex = if_nametoindex(interface.c_str()); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 369 | strlcpy(iface, interface.c_str(), sizeof(iface)); |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 370 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 371 | // Pass in everything that clatd needs: interface, a netid to use for DNS lookups, a fwmark for |
| 372 | // outgoing packets, the NAT64 prefix, and the IPv4 and IPv6 addresses. |
| 373 | // Validate the prefix and strip off the prefix length. |
| 374 | uint8_t family; |
| 375 | uint8_t prefixLen; |
Maciej Żenczykowski | 1c06f9c | 2019-03-29 23:19:19 -0700 | [diff] [blame] | 376 | int res = parsePrefix(nat64Prefix.c_str(), &family, &pfx96, sizeof(pfx96), &prefixLen); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 377 | // clatd only supports /96 prefixes. |
Maciej Żenczykowski | 1c06f9c | 2019-03-29 23:19:19 -0700 | [diff] [blame] | 378 | if (res != sizeof(pfx96)) return res; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 379 | if (family != AF_INET6) return -EAFNOSUPPORT; |
| 380 | if (prefixLen != 96) return -EINVAL; |
Maciej Żenczykowski | 1c06f9c | 2019-03-29 23:19:19 -0700 | [diff] [blame] | 381 | if (!inet_ntop(AF_INET6, &pfx96, pfx96String, sizeof(pfx96String))) return -errno; |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 382 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 383 | // Pick an IPv4 address. |
| 384 | // TODO: this picks the address based on other addresses that are assigned to interfaces, but |
| 385 | // the address is only actually assigned to an interface once clatd starts up. So we could end |
| 386 | // up with two clatd instances with the same IPv4 address. |
| 387 | // 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] | 388 | v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)}; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 389 | if (v4.s_addr == INADDR_NONE) { |
| 390 | ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen); |
| 391 | return -EADDRNOTAVAIL; |
| 392 | } |
| 393 | if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno; |
| 394 | |
| 395 | // Generate a checksum-neutral IID. |
Maciej Żenczykowski | 1c06f9c | 2019-03-29 23:19:19 -0700 | [diff] [blame] | 396 | if (generateIpv6Address(iface, v4, pfx96, &v6)) { |
| 397 | ALOGE("Unable to find global source address on %s for %s", iface, pfx96String); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 398 | return -EADDRNOTAVAIL; |
| 399 | } |
| 400 | if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno; |
| 401 | |
Maciej Żenczykowski | 1c06f9c | 2019-03-29 23:19:19 -0700 | [diff] [blame] | 402 | ALOGD("starting clatd on %s v4=%s v6=%s pfx96=%s", iface, v4Str, v6Str, pfx96String); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix, |
| 407 | std::string* v6Str) { |
Maciej Żenczykowski | 5628027 | 2019-03-30 03:32:51 -0700 | [diff] [blame^] | 408 | std::lock_guard guard(mutex); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 409 | ClatdTracker* existing = getClatdTracker(interface); |
| 410 | if (existing != nullptr) { |
| 411 | ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str()); |
| 412 | errno = EBUSY; |
| 413 | return -errno; |
| 414 | } |
| 415 | |
| 416 | ClatdTracker tracker(mNetCtrl); |
| 417 | if (int ret = tracker.init(interface, nat64Prefix)) { |
| 418 | return ret; |
| 419 | } |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 420 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 421 | std::string progname("clatd-"); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 422 | progname += tracker.iface; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 423 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 424 | // clang-format off |
| 425 | char* args[] = {(char*) progname.c_str(), |
| 426 | (char*) "-i", tracker.iface, |
| 427 | (char*) "-n", tracker.netIdString, |
| 428 | (char*) "-m", tracker.fwmarkString, |
Maciej Żenczykowski | 1c06f9c | 2019-03-29 23:19:19 -0700 | [diff] [blame] | 429 | (char*) "-p", tracker.pfx96String, |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 430 | (char*) "-4", tracker.v4Str, |
| 431 | (char*) "-6", tracker.v6Str, |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 432 | nullptr}; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 433 | // clang-format on |
| 434 | |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 435 | // Specify no flags and no actions, posix_spawn will use vfork and is |
| 436 | // guaranteed to return only once exec has been called. |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 437 | int res = posix_spawn(&tracker.pid, kClatdPath, nullptr, nullptr, args, nullptr); |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 438 | if (res) { |
| 439 | ALOGE("posix_spawn failed (%s)", strerror(res)); |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 440 | return -res; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 441 | } |
| 442 | |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 443 | maybeStartBpf(tracker); |
| 444 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 445 | mClatdTrackers[interface] = tracker; |
| 446 | ALOGD("clatd started on %s", interface.c_str()); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 447 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 448 | *v6Str = tracker.v6Str; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 452 | int ClatdController::stopClatd(const std::string& interface) { |
Maciej Żenczykowski | 5628027 | 2019-03-30 03:32:51 -0700 | [diff] [blame^] | 453 | std::lock_guard guard(mutex); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 454 | ClatdTracker* tracker = getClatdTracker(interface); |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 455 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 456 | if (tracker == nullptr) { |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 457 | ALOGE("clatd already stopped"); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 458 | return -ENODEV; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 459 | } |
| 460 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 461 | ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str()); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 462 | |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 463 | maybeStopBpf(*tracker); |
| 464 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 465 | kill(tracker->pid, SIGTERM); |
| 466 | waitpid(tracker->pid, nullptr, 0); |
| 467 | mClatdTrackers.erase(interface); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 468 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 469 | ALOGD("clatd on %s stopped", interface.c_str()); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 474 | void ClatdController::dump(DumpWriter& dw) { |
| 475 | std::lock_guard guard(mutex); |
| 476 | |
| 477 | ScopedIndent clatdIndent(dw); |
| 478 | dw.println("ClatdController"); |
| 479 | |
| 480 | { |
| 481 | ScopedIndent trackerIndent(dw); |
| 482 | dw.println("Trackers: iif[iface] nat64Prefix v6Addr -> v4Addr [netId]"); |
| 483 | |
| 484 | ScopedIndent trackerDetailIndent(dw); |
| 485 | for (const auto& pair : mClatdTrackers) { |
| 486 | const ClatdTracker& tracker = pair.second; |
| 487 | dw.println("%u[%s] %s/96 %s -> %s [%u]", tracker.ifIndex, tracker.iface, |
| 488 | tracker.pfx96String, tracker.v6Str, tracker.v4Str, tracker.netId); |
| 489 | } |
| 490 | } |
| 491 | |
Maciej Żenczykowski | abb2cbf | 2019-04-01 01:29:29 -0700 | [diff] [blame] | 492 | int mapFd = getClatIngressMapFd(); |
| 493 | if (mapFd < 0) return; // if unsupported just don't dump anything |
| 494 | BpfMap<ClatIngressKey, ClatIngressValue> configMap(mapFd); |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 495 | |
| 496 | ScopedIndent bpfIndent(dw); |
| 497 | dw.println("BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)"); |
| 498 | |
| 499 | ScopedIndent bpfDetailIndent(dw); |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 500 | const auto printClatMap = [&dw](const ClatIngressKey& key, const ClatIngressValue& value, |
| 501 | const BpfMap<ClatIngressKey, ClatIngressValue>&) { |
| 502 | char iifStr[IFNAMSIZ] = "?"; |
| 503 | char pfx96Str[INET6_ADDRSTRLEN] = "?"; |
| 504 | char local6Str[INET6_ADDRSTRLEN] = "?"; |
| 505 | char local4Str[INET_ADDRSTRLEN] = "?"; |
| 506 | char oifStr[IFNAMSIZ] = "?"; |
| 507 | |
| 508 | if_indextoname(key.iif, iifStr); |
| 509 | inet_ntop(AF_INET6, &key.pfx96, pfx96Str, sizeof(pfx96Str)); |
| 510 | inet_ntop(AF_INET6, &key.local6, local6Str, sizeof(local6Str)); |
| 511 | inet_ntop(AF_INET, &value.local4, local4Str, sizeof(local4Str)); |
| 512 | if_indextoname(value.oif, oifStr); |
| 513 | |
| 514 | dw.println("%u(%s) %s/96 %s -> %s %u(%s)", key.iif, iifStr, pfx96Str, local6Str, local4Str, |
| 515 | value.oif, oifStr); |
| 516 | return netdutils::status::ok; |
| 517 | }; |
| 518 | auto res = configMap.iterateWithValue(printClatMap); |
| 519 | if (!isOk(res)) { |
| 520 | dw.println("Error printing BPF map: %s", res.msg().c_str()); |
| 521 | } |
| 522 | } |
| 523 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 524 | auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree; |
| 525 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 526 | } // namespace net |
| 527 | } // namespace android |