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 | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 23 | #include <linux/if_tun.h> |
| 24 | #include <linux/ioctl.h> |
Maciej Żenczykowski | c8c38aa | 2019-03-29 01:24:51 -0700 | [diff] [blame] | 25 | #include <net/if.h> |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 26 | #include <netinet/in.h> |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 27 | #include <spawn.h> |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
Luke Huang | 94c43a1 | 2019-02-14 19:51:38 +0800 | [diff] [blame] | 30 | #include <unistd.h> |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 31 | |
| 32 | #define LOG_TAG "ClatdController" |
Logan Chien | 3f46148 | 2018-04-23 14:31:32 +0800 | [diff] [blame] | 33 | #include <log/log.h> |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 34 | |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 35 | #include "ClatdController.h" |
Maciej Żenczykowski | a169995 | 2019-05-11 17:07:44 -0700 | [diff] [blame] | 36 | #include "InterfaceController.h" |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 37 | |
| 38 | #include "android-base/properties.h" |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 39 | #include "android-base/scopeguard.h" |
Lorenzo Colitti | 91fd580 | 2019-06-28 19:22:01 +0900 | [diff] [blame] | 40 | #include "android-base/stringprintf.h" |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 41 | #include "android-base/unique_fd.h" |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 42 | #include "bpf/BpfMap.h" |
| 43 | #include "netdbpf/bpf_shared.h" |
| 44 | #include "netdutils/DumpWriter.h" |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 45 | |
| 46 | extern "C" { |
| 47 | #include "netutils/checksum.h" |
| 48 | } |
| 49 | |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 50 | #include "ClatUtils.h" |
Lorenzo Colitti | 45d3dd0 | 2014-06-09 14:09:20 +0900 | [diff] [blame] | 51 | #include "Fwmark.h" |
Paul Jensen | 84c1d03 | 2014-05-30 13:29:41 -0400 | [diff] [blame] | 52 | #include "NetdConstants.h" |
| 53 | #include "NetworkController.h" |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 54 | #include "netid_client.h" |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 55 | |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 56 | static const char* kClatdPath = "/system/bin/clatd"; |
| 57 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 58 | // For historical reasons, start with 192.0.0.4, and after that, use all subsequent addresses in |
| 59 | // 192.0.0.0/29 (RFC 7335). |
| 60 | static const char* kV4AddrString = "192.0.0.4"; |
| 61 | static const in_addr kV4Addr = {inet_addr(kV4AddrString)}; |
| 62 | static const int kV4AddrLen = 29; |
| 63 | |
Lorenzo Colitti | 91fd580 | 2019-06-28 19:22:01 +0900 | [diff] [blame] | 64 | using android::base::StringPrintf; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 65 | using android::base::unique_fd; |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 66 | using android::bpf::BpfMap; |
| 67 | using android::netdutils::DumpWriter; |
| 68 | using android::netdutils::ScopedIndent; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 69 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 70 | namespace android { |
| 71 | namespace net { |
| 72 | |
Maciej Żenczykowski | a8ef6f9 | 2019-12-16 15:53:36 -0800 | [diff] [blame^] | 73 | void ClatdController::resetEgressMap() { |
| 74 | int netlinkFd = mNetlinkFd.get(); |
| 75 | |
| 76 | const auto del = [&netlinkFd](const ClatEgressKey& key, |
| 77 | const BpfMap<ClatEgressKey, ClatEgressValue>&) { |
| 78 | ALOGW("Removing stale clat config on interface %d.", key.iif); |
| 79 | int rv = tcQdiscDelDevClsact(netlinkFd, key.iif); |
| 80 | if (rv < 0) ALOGE("tcQdiscDelDevClsact() failure: %s", strerror(-rv)); |
| 81 | return netdutils::status::ok; // keep on going regardless |
| 82 | }; |
| 83 | auto ret = mClatEgressMap.iterate(del); |
| 84 | if (!isOk(ret)) ALOGE("mClatEgressMap.iterate() failure: %s", strerror(ret.code())); |
| 85 | ret = mClatEgressMap.clear(); |
| 86 | if (!isOk(ret)) ALOGE("mClatEgressMap.clear() failure: %s", strerror(ret.code())); |
| 87 | } |
| 88 | |
Maciej Żenczykowski | 35bc6ed | 2019-12-16 15:39:04 -0800 | [diff] [blame] | 89 | void ClatdController::resetIngressMap() { |
| 90 | int netlinkFd = mNetlinkFd.get(); |
| 91 | |
| 92 | const auto del = [&netlinkFd](const ClatIngressKey& key, |
| 93 | const BpfMap<ClatIngressKey, ClatIngressValue>&) { |
| 94 | ALOGW("Removing stale clat config on interface %d.", key.iif); |
| 95 | int rv = tcQdiscDelDevClsact(netlinkFd, key.iif); |
| 96 | if (rv < 0) ALOGE("tcQdiscDelDevClsact() failure: %s", strerror(-rv)); |
| 97 | return netdutils::status::ok; // keep on going regardless |
| 98 | }; |
| 99 | auto ret = mClatIngressMap.iterate(del); |
| 100 | if (!isOk(ret)) ALOGE("mClatIngressMap.iterate() failure: %s", strerror(ret.code())); |
| 101 | ret = mClatIngressMap.clear(); |
| 102 | if (!isOk(ret)) ALOGE("mClatIngressMap.clear() failure: %s", strerror(ret.code())); |
| 103 | } |
| 104 | |
Maciej Żenczykowski | 5628027 | 2019-03-30 03:32:51 -0700 | [diff] [blame] | 105 | void ClatdController::init(void) { |
| 106 | std::lock_guard guard(mutex); |
| 107 | |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 108 | // TODO: should refactor into separate function for testability |
| 109 | if (bpf::getBpfSupportLevel() == bpf::BpfLevel::NONE) { |
| 110 | ALOGI("Pre-4.9 kernel or pre-P api shipping level - disabling clat ebpf."); |
| 111 | mClatEbpfMode = ClatEbpfDisabled; |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // We know the device initially shipped with at least P..., |
| 116 | // but did it ship with at least Q? |
| 117 | |
| 118 | uint64_t api_level = base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0); |
| 119 | if (api_level == 0) { |
| 120 | ALOGE("Cannot determine initial API level of the device."); |
| 121 | api_level = base::GetUintProperty<uint64_t>("ro.build.version.sdk", 0); |
| 122 | } |
| 123 | |
| 124 | // Note: MINIMUM_API_REQUIRED is for eBPF as a whole and is thus P |
| 125 | if (api_level > bpf::MINIMUM_API_REQUIRED) { |
| 126 | ALOGI("4.9+ kernel and device shipped with Q+ - clat ebpf should work."); |
| 127 | mClatEbpfMode = ClatEbpfEnabled; |
| 128 | } else { |
| 129 | // We cannot guarantee that 4.9-P kernels will include NET_CLS_BPF support. |
| 130 | ALOGI("4.9+ kernel and device shipped with P - clat ebpf might work."); |
| 131 | mClatEbpfMode = ClatEbpfMaybe; |
| 132 | } |
| 133 | |
| 134 | int rv = openNetlinkSocket(); |
| 135 | if (rv < 0) { |
| 136 | ALOGE("openNetlinkSocket() failure: %s", strerror(-rv)); |
| 137 | mClatEbpfMode = ClatEbpfDisabled; |
| 138 | return; |
| 139 | } |
| 140 | mNetlinkFd.reset(rv); |
| 141 | |
Maciej Żenczykowski | 0bd8da7 | 2019-12-16 15:16:28 -0800 | [diff] [blame] | 142 | rv = getClatEgressMapFd(); |
| 143 | if (rv < 0) { |
| 144 | ALOGE("getClatEgressMapFd() failure: %s", strerror(-rv)); |
| 145 | mClatEbpfMode = ClatEbpfDisabled; |
| 146 | mNetlinkFd.reset(-1); |
| 147 | return; |
| 148 | } |
| 149 | mClatEgressMap.reset(rv); |
| 150 | |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 151 | rv = getClatIngressMapFd(); |
| 152 | if (rv < 0) { |
| 153 | ALOGE("getClatIngressMapFd() failure: %s", strerror(-rv)); |
| 154 | mClatEbpfMode = ClatEbpfDisabled; |
Maciej Żenczykowski | 0bd8da7 | 2019-12-16 15:16:28 -0800 | [diff] [blame] | 155 | mClatEgressMap.reset(-1); |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 156 | mNetlinkFd.reset(-1); |
| 157 | return; |
| 158 | } |
| 159 | mClatIngressMap.reset(rv); |
| 160 | |
Maciej Żenczykowski | a8ef6f9 | 2019-12-16 15:53:36 -0800 | [diff] [blame^] | 161 | resetEgressMap(); |
Maciej Żenczykowski | 35bc6ed | 2019-12-16 15:39:04 -0800 | [diff] [blame] | 162 | resetIngressMap(); |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 165 | bool ClatdController::isIpv4AddressFree(in_addr_t addr) { |
| 166 | int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 167 | if (s == -1) { |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | // Attempt to connect to the address. If the connection succeeds and getsockname returns the |
| 172 | // same then the address is already assigned to the system and we can't use it. |
Nick Desaulniers | 6b35750 | 2019-10-11 09:26:44 -0700 | [diff] [blame] | 173 | struct sockaddr_in sin = { |
| 174 | .sin_family = AF_INET, |
Maciej Żenczykowski | 268190e | 2019-10-31 23:47:53 -0700 | [diff] [blame] | 175 | .sin_port = htons(53), |
Nick Desaulniers | 6b35750 | 2019-10-11 09:26:44 -0700 | [diff] [blame] | 176 | .sin_addr = {addr}, |
| 177 | }; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 178 | socklen_t len = sizeof(sin); |
| 179 | bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 && |
| 180 | getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) && |
| 181 | sin.sin_addr.s_addr == addr; |
| 182 | |
| 183 | close(s); |
| 184 | return !inuse; |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 185 | } |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 186 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 187 | // Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order. |
| 188 | // ip - the IP address from the configuration file |
| 189 | // prefixlen - the length of the prefix from which addresses may be selected. |
| 190 | // returns: the IPv4 address, or INADDR_NONE if no addresses were available |
| 191 | in_addr_t ClatdController::selectIpv4Address(const in_addr ip, int16_t prefixlen) { |
| 192 | // Don't accept prefixes that are too large because we scan addresses one by one. |
| 193 | if (prefixlen < 16 || prefixlen > 32) { |
| 194 | return INADDR_NONE; |
| 195 | } |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 196 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 197 | // All these are in host byte order. |
| 198 | in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen); |
| 199 | in_addr_t ipv4 = ntohl(ip.s_addr); |
| 200 | in_addr_t first_ipv4 = ipv4; |
| 201 | in_addr_t prefix = ipv4 & mask; |
| 202 | |
| 203 | // Pick the first IPv4 address in the pool, wrapping around if necessary. |
| 204 | // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0. |
| 205 | do { |
| 206 | if (isIpv4AddressFreeFunc(htonl(ipv4))) { |
| 207 | return htonl(ipv4); |
| 208 | } |
| 209 | ipv4 = prefix | ((ipv4 + 1) & ~mask); |
| 210 | } while (ipv4 != first_ipv4); |
| 211 | |
| 212 | return INADDR_NONE; |
| 213 | } |
| 214 | |
| 215 | // Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix. |
| 216 | void ClatdController::makeChecksumNeutral(in6_addr* v6, const in_addr v4, |
| 217 | const in6_addr& nat64Prefix) { |
| 218 | // Fill last 8 bytes of IPv6 address with random bits. |
| 219 | arc4random_buf(&v6->s6_addr[8], 8); |
| 220 | |
| 221 | // Make the IID checksum-neutral. That is, make it so that: |
| 222 | // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6) |
| 223 | // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4): |
| 224 | // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix) |
| 225 | // Do this by adjusting the two bytes in the middle of the IID. |
| 226 | |
| 227 | uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12]; |
| 228 | |
| 229 | uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4)); |
| 230 | uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) + |
| 231 | ip_checksum_add(0, v6, sizeof(*v6)); |
| 232 | |
| 233 | uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2); |
| 234 | v6->s6_addr[11] = delta >> 8; |
| 235 | v6->s6_addr[12] = delta & 0xff; |
| 236 | } |
| 237 | |
| 238 | // Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix. |
| 239 | int ClatdController::generateIpv6Address(const char* iface, const in_addr v4, |
| 240 | const in6_addr& nat64Prefix, in6_addr* v6) { |
| 241 | unique_fd s(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0)); |
| 242 | if (s == -1) return -errno; |
| 243 | |
| 244 | if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) { |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 245 | return -errno; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 246 | } |
| 247 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 248 | sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix}; |
| 249 | if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) { |
| 250 | return -errno; |
| 251 | } |
| 252 | |
| 253 | socklen_t len = sizeof(sin6); |
| 254 | if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) { |
| 255 | return -errno; |
| 256 | } |
| 257 | |
| 258 | *v6 = sin6.sin6_addr; |
| 259 | |
| 260 | if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) || |
| 261 | IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) { |
| 262 | return -ENETUNREACH; |
| 263 | } |
| 264 | |
| 265 | makeChecksumNeutral(v6, v4, nat64Prefix); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 270 | void ClatdController::maybeStartBpf(const ClatdTracker& tracker) { |
| 271 | if (mClatEbpfMode == ClatEbpfDisabled) return; |
| 272 | |
| 273 | int rv = hardwareAddressType(tracker.iface); |
| 274 | if (rv < 0) { |
| 275 | ALOGE("hardwareAddressType(%s[%d]) failure: %s", tracker.iface, tracker.ifIndex, |
| 276 | strerror(-rv)); |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | bool isEthernet; |
| 281 | switch (rv) { |
| 282 | case ARPHRD_ETHER: |
| 283 | isEthernet = true; |
| 284 | break; |
| 285 | case ARPHRD_RAWIP: // in Linux 4.14+ rmnet support was upstreamed and this is 519 |
| 286 | case 530: // this is ARPHRD_RAWIP on some Android 4.9 kernels with rmnet |
| 287 | isEthernet = false; |
| 288 | break; |
| 289 | default: |
| 290 | ALOGE("hardwareAddressType(%s[%d]) returned unknown type %d.", tracker.iface, |
| 291 | tracker.ifIndex, rv); |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | rv = getClatIngressProgFd(isEthernet); |
| 296 | if (rv < 0) { |
| 297 | ALOGE("getClatIngressProgFd(%d) failure: %s", isEthernet, strerror(-rv)); |
| 298 | return; |
| 299 | } |
| 300 | unique_fd progFd(rv); |
| 301 | |
| 302 | ClatIngressKey key = { |
| 303 | .iif = tracker.ifIndex, |
| 304 | .pfx96 = tracker.pfx96, |
| 305 | .local6 = tracker.v6, |
| 306 | }; |
| 307 | ClatIngressValue value = { |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 308 | // TODO: move all the clat code to eBPF and remove the tun interface entirely. |
Maciej Żenczykowski | 39b0b90 | 2019-05-08 00:36:30 -0700 | [diff] [blame] | 309 | .oif = tracker.v4ifIndex, |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 310 | .local4 = tracker.v4, |
| 311 | }; |
| 312 | |
| 313 | auto ret = mClatIngressMap.writeValue(key, value, BPF_ANY); |
| 314 | if (!isOk(ret)) { |
| 315 | ALOGE("mClatIngress.Map.writeValue failure: %s", strerror(ret.code())); |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | // We do tc setup *after* populating map, so scanning through map |
| 320 | // can always be used to tell us what needs cleanup. |
| 321 | |
| 322 | rv = tcQdiscAddDevClsact(mNetlinkFd, tracker.ifIndex); |
| 323 | if (rv) { |
| 324 | ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface, |
| 325 | strerror(-rv)); |
| 326 | ret = mClatIngressMap.deleteValue(key); |
| 327 | if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code())); |
| 328 | return; |
| 329 | } |
| 330 | |
Maciej Żenczykowski | b140a3a | 2019-12-15 11:53:46 -0800 | [diff] [blame] | 331 | rv = tcFilterAddDevIngressBpf(mNetlinkFd, tracker.ifIndex, progFd, isEthernet); |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 332 | if (rv) { |
| 333 | if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) { |
Maciej Żenczykowski | b140a3a | 2019-12-15 11:53:46 -0800 | [diff] [blame] | 334 | ALOGI("tcFilterAddDevIngressBpf(%d[%s], %d): %s", tracker.ifIndex, tracker.iface, |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 335 | isEthernet, strerror(-rv)); |
Maciej Żenczykowski | b140a3a | 2019-12-15 11:53:46 -0800 | [diff] [blame] | 336 | } else { |
| 337 | ALOGE("tcFilterAddDevIngressBpf(%d[%s], %d) failure: %s", tracker.ifIndex, |
| 338 | tracker.iface, isEthernet, strerror(-rv)); |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 339 | } |
| 340 | rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex); |
| 341 | if (rv) |
| 342 | ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface, |
| 343 | strerror(-rv)); |
| 344 | ret = mClatIngressMap.deleteValue(key); |
| 345 | if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code())); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | // success |
| 350 | } |
| 351 | |
Lorenzo Colitti | 91fd580 | 2019-06-28 19:22:01 +0900 | [diff] [blame] | 352 | void ClatdController::maybeSetIptablesDropRule(bool add, const char* pfx96Str, const char* v6Str) { |
| 353 | if (mClatEbpfMode == ClatEbpfDisabled) return; |
| 354 | |
| 355 | std::string cmd = StringPrintf( |
| 356 | "*raw\n" |
| 357 | "%s %s -s %s/96 -d %s -j DROP\n" |
| 358 | "COMMIT\n", |
| 359 | (add ? "-A" : "-D"), LOCAL_RAW_PREROUTING, pfx96Str, v6Str); |
| 360 | |
| 361 | iptablesRestoreFunction(V6, cmd); |
| 362 | } |
| 363 | |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 364 | void ClatdController::maybeStopBpf(const ClatdTracker& tracker) { |
| 365 | if (mClatEbpfMode == ClatEbpfDisabled) return; |
| 366 | |
| 367 | // No need to remove filter, since we remove qdisc it is attached to, |
| 368 | // which automatically removes everything attached to the qdisc. |
| 369 | int rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex); |
| 370 | if (rv < 0) |
| 371 | ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface, |
| 372 | strerror(-rv)); |
| 373 | |
| 374 | // We cleanup map last, so scanning through map can be used to |
| 375 | // determine what still needs cleanup. |
| 376 | |
| 377 | ClatIngressKey key = { |
| 378 | .iif = tracker.ifIndex, |
| 379 | .pfx96 = tracker.pfx96, |
| 380 | .local6 = tracker.v6, |
| 381 | }; |
| 382 | |
| 383 | auto ret = mClatIngressMap.deleteValue(key); |
| 384 | if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code())); |
| 385 | } |
| 386 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 387 | // Finds the tracker of the clatd running on interface |interface|, or nullptr if clatd has not been |
| 388 | // started on |interface|. |
| 389 | ClatdController::ClatdTracker* ClatdController::getClatdTracker(const std::string& interface) { |
| 390 | auto it = mClatdTrackers.find(interface); |
| 391 | return (it == mClatdTrackers.end() ? nullptr : &it->second); |
| 392 | } |
| 393 | |
| 394 | // Initializes a ClatdTracker for the specified interface. |
Maciej Żenczykowski | a56b2e6 | 2019-04-24 13:17:18 -0700 | [diff] [blame] | 395 | int ClatdController::ClatdTracker::init(unsigned networkId, const std::string& interface, |
Maciej Żenczykowski | 4657e51 | 2019-05-08 06:35:08 -0700 | [diff] [blame] | 396 | const std::string& v4interface, |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 397 | const std::string& nat64Prefix) { |
Maciej Żenczykowski | a56b2e6 | 2019-04-24 13:17:18 -0700 | [diff] [blame] | 398 | netId = networkId; |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 399 | |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 400 | fwmark.netId = netId; |
| 401 | fwmark.explicitlySelected = true; |
| 402 | fwmark.protectedFromVpn = true; |
| 403 | fwmark.permission = PERMISSION_SYSTEM; |
| 404 | |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 405 | snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 406 | snprintf(netIdString, sizeof(netIdString), "%u", netId); |
| 407 | strlcpy(iface, interface.c_str(), sizeof(iface)); |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 408 | ifIndex = if_nametoindex(iface); |
Maciej Żenczykowski | 4657e51 | 2019-05-08 06:35:08 -0700 | [diff] [blame] | 409 | strlcpy(v4iface, v4interface.c_str(), sizeof(v4iface)); |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 410 | v4ifIndex = if_nametoindex(v4iface); |
Lorenzo Colitti | 32b2e79 | 2015-01-07 15:11:30 +0900 | [diff] [blame] | 411 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 412 | // Pass in everything that clatd needs: interface, a netid to use for DNS lookups, a fwmark for |
| 413 | // outgoing packets, the NAT64 prefix, and the IPv4 and IPv6 addresses. |
| 414 | // Validate the prefix and strip off the prefix length. |
| 415 | uint8_t family; |
| 416 | uint8_t prefixLen; |
Maciej Żenczykowski | 1c06f9c | 2019-03-29 23:19:19 -0700 | [diff] [blame] | 417 | int res = parsePrefix(nat64Prefix.c_str(), &family, &pfx96, sizeof(pfx96), &prefixLen); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 418 | // clatd only supports /96 prefixes. |
Maciej Żenczykowski | 1c06f9c | 2019-03-29 23:19:19 -0700 | [diff] [blame] | 419 | if (res != sizeof(pfx96)) return res; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 420 | if (family != AF_INET6) return -EAFNOSUPPORT; |
| 421 | if (prefixLen != 96) return -EINVAL; |
Maciej Żenczykowski | 1c06f9c | 2019-03-29 23:19:19 -0700 | [diff] [blame] | 422 | if (!inet_ntop(AF_INET6, &pfx96, pfx96String, sizeof(pfx96String))) return -errno; |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 423 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 424 | // Pick an IPv4 address. |
| 425 | // TODO: this picks the address based on other addresses that are assigned to interfaces, but |
| 426 | // the address is only actually assigned to an interface once clatd starts up. So we could end |
| 427 | // up with two clatd instances with the same IPv4 address. |
| 428 | // 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] | 429 | v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)}; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 430 | if (v4.s_addr == INADDR_NONE) { |
| 431 | ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen); |
| 432 | return -EADDRNOTAVAIL; |
| 433 | } |
| 434 | if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno; |
| 435 | |
| 436 | // Generate a checksum-neutral IID. |
Maciej Żenczykowski | 1c06f9c | 2019-03-29 23:19:19 -0700 | [diff] [blame] | 437 | if (generateIpv6Address(iface, v4, pfx96, &v6)) { |
| 438 | 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] | 439 | return -EADDRNOTAVAIL; |
| 440 | } |
| 441 | if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno; |
| 442 | |
Maciej Żenczykowski | 1c06f9c | 2019-03-29 23:19:19 -0700 | [diff] [blame] | 443 | 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] | 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix, |
| 448 | std::string* v6Str) { |
Maciej Żenczykowski | 5628027 | 2019-03-30 03:32:51 -0700 | [diff] [blame] | 449 | std::lock_guard guard(mutex); |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 450 | |
| 451 | // 1. fail if pre-existing tracker already exists |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 452 | ClatdTracker* existing = getClatdTracker(interface); |
| 453 | if (existing != nullptr) { |
| 454 | ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str()); |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 455 | return -EBUSY; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 456 | } |
| 457 | |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 458 | // 2. get network id associated with this external interface |
Maciej Żenczykowski | a56b2e6 | 2019-04-24 13:17:18 -0700 | [diff] [blame] | 459 | unsigned networkId = mNetCtrl->getNetworkForInterface(interface.c_str()); |
| 460 | if (networkId == NETID_UNSET) { |
| 461 | ALOGE("Interface %s not assigned to any netId", interface.c_str()); |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 462 | return -ENODEV; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 463 | } |
JP Abgrall | 69261cb | 2014-06-19 18:35:24 -0700 | [diff] [blame] | 464 | |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 465 | // 3. open the tun device in non blocking mode as required by clatd |
| 466 | int res = open("/dev/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC); |
| 467 | if (res == -1) { |
| 468 | res = errno; |
| 469 | ALOGE("open of tun device failed (%s)", strerror(res)); |
| 470 | return -res; |
| 471 | } |
| 472 | unique_fd tmpTunFd(res); |
| 473 | |
| 474 | // 4. create the v4-... tun interface |
Maciej Żenczykowski | 4657e51 | 2019-05-08 06:35:08 -0700 | [diff] [blame] | 475 | std::string v4interface("v4-"); |
| 476 | v4interface += interface; |
| 477 | |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 478 | struct ifreq ifr = { |
| 479 | .ifr_flags = IFF_TUN, |
| 480 | }; |
Maciej Żenczykowski | 4657e51 | 2019-05-08 06:35:08 -0700 | [diff] [blame] | 481 | strlcpy(ifr.ifr_name, v4interface.c_str(), sizeof(ifr.ifr_name)); |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 482 | |
| 483 | res = ioctl(tmpTunFd, TUNSETIFF, &ifr, sizeof(ifr)); |
| 484 | if (res == -1) { |
| 485 | res = errno; |
| 486 | ALOGE("ioctl(TUNSETIFF) failed (%s)", strerror(res)); |
| 487 | return -res; |
| 488 | } |
| 489 | |
Maciej Żenczykowski | a169995 | 2019-05-11 17:07:44 -0700 | [diff] [blame] | 490 | // disable IPv6 on it - failing to do so is not a critical error |
| 491 | res = InterfaceController::setEnableIPv6(v4interface.c_str(), 0); |
| 492 | if (res) ALOGE("setEnableIPv6 %s failed (%s)", v4interface.c_str(), strerror(res)); |
| 493 | |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 494 | // 5. initialize tracker object |
Maciej Żenczykowski | a56b2e6 | 2019-04-24 13:17:18 -0700 | [diff] [blame] | 495 | ClatdTracker tracker; |
Maciej Żenczykowski | 4657e51 | 2019-05-08 06:35:08 -0700 | [diff] [blame] | 496 | int ret = tracker.init(networkId, interface, v4interface, nat64Prefix); |
Maciej Żenczykowski | a56b2e6 | 2019-04-24 13:17:18 -0700 | [diff] [blame] | 497 | if (ret) return ret; |
| 498 | |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 499 | // 6. create a throwaway socket to reserve a file descriptor number |
| 500 | res = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 501 | if (res == -1) { |
| 502 | res = errno; |
| 503 | ALOGE("socket(ipv6/udp) failed (%s)", strerror(res)); |
| 504 | return -res; |
| 505 | } |
| 506 | unique_fd passedTunFd(res); |
| 507 | |
| 508 | // 7. this is the FD we'll pass to clatd on the cli, so need it as a string |
| 509 | char passedTunFdStr[INT32_STRLEN]; |
| 510 | snprintf(passedTunFdStr, sizeof(passedTunFdStr), "%d", passedTunFd.get()); |
| 511 | |
| 512 | // 8. we're going to use this as argv[0] to clatd to make ps output more useful |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 513 | std::string progname("clatd-"); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 514 | progname += tracker.iface; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 515 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 516 | // clang-format off |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 517 | const char* args[] = {progname.c_str(), |
| 518 | "-i", tracker.iface, |
| 519 | "-n", tracker.netIdString, |
| 520 | "-m", tracker.fwmarkString, |
| 521 | "-p", tracker.pfx96String, |
| 522 | "-4", tracker.v4Str, |
| 523 | "-6", tracker.v6Str, |
| 524 | "-t", passedTunFdStr, |
| 525 | nullptr}; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 526 | // clang-format on |
| 527 | |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 528 | // 9. register vfork requirement |
| 529 | posix_spawnattr_t attr; |
| 530 | res = posix_spawnattr_init(&attr); |
| 531 | if (res) { |
| 532 | ALOGE("posix_spawnattr_init failed (%s)", strerror(res)); |
| 533 | return -res; |
| 534 | } |
| 535 | const android::base::ScopeGuard attrGuard = [&] { posix_spawnattr_destroy(&attr); }; |
| 536 | res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_USEVFORK); |
| 537 | if (res) { |
| 538 | ALOGE("posix_spawnattr_setflags failed (%s)", strerror(res)); |
| 539 | return -res; |
| 540 | } |
| 541 | |
| 542 | // 10. register dup2() action: this is what 'clears' the CLOEXEC flag |
| 543 | // on the tun fd that we want the child clatd process to inherit |
| 544 | // (this will happen after the vfork, and before the execve) |
| 545 | posix_spawn_file_actions_t fa; |
| 546 | res = posix_spawn_file_actions_init(&fa); |
| 547 | if (res) { |
| 548 | ALOGE("posix_spawn_file_actions_init failed (%s)", strerror(res)); |
| 549 | return -res; |
| 550 | } |
| 551 | const android::base::ScopeGuard faGuard = [&] { posix_spawn_file_actions_destroy(&fa); }; |
| 552 | res = posix_spawn_file_actions_adddup2(&fa, tmpTunFd, passedTunFd); |
| 553 | if (res) { |
| 554 | ALOGE("posix_spawn_file_actions_adddup2 failed (%s)", strerror(res)); |
| 555 | return -res; |
| 556 | } |
| 557 | |
Lorenzo Colitti | 91fd580 | 2019-06-28 19:22:01 +0900 | [diff] [blame] | 558 | // 11. If necessary, add the drop rule for iptables. |
| 559 | maybeSetIptablesDropRule(true, tracker.pfx96String, tracker.v6Str); |
| 560 | |
| 561 | // 12. actually perform vfork/dup2/execve |
Maciej Żenczykowski | f4b44fe | 2019-04-08 16:18:50 -0700 | [diff] [blame] | 562 | res = posix_spawn(&tracker.pid, kClatdPath, &fa, &attr, (char* const*)args, nullptr); |
Luke Huang | 4096244 | 2019-02-26 11:46:10 +0800 | [diff] [blame] | 563 | if (res) { |
| 564 | ALOGE("posix_spawn failed (%s)", strerror(res)); |
Luke Huang | 6d30123 | 2018-08-01 14:05:18 +0800 | [diff] [blame] | 565 | return -res; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 566 | } |
| 567 | |
Lorenzo Colitti | 91fd580 | 2019-06-28 19:22:01 +0900 | [diff] [blame] | 568 | // 13. configure eBPF offload - if possible |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 569 | maybeStartBpf(tracker); |
| 570 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 571 | mClatdTrackers[interface] = tracker; |
| 572 | ALOGD("clatd started on %s", interface.c_str()); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 573 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 574 | *v6Str = tracker.v6Str; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 575 | return 0; |
| 576 | } |
| 577 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 578 | int ClatdController::stopClatd(const std::string& interface) { |
Maciej Żenczykowski | 5628027 | 2019-03-30 03:32:51 -0700 | [diff] [blame] | 579 | std::lock_guard guard(mutex); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 580 | ClatdTracker* tracker = getClatdTracker(interface); |
Lorenzo Colitti | ac7fefc | 2014-10-20 17:14:13 +0900 | [diff] [blame] | 581 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 582 | if (tracker == nullptr) { |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 583 | ALOGE("clatd already stopped"); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 584 | return -ENODEV; |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 585 | } |
| 586 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 587 | ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str()); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 588 | |
Maciej Żenczykowski | 1c086e5 | 2019-03-29 23:13:49 -0700 | [diff] [blame] | 589 | maybeStopBpf(*tracker); |
| 590 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 591 | kill(tracker->pid, SIGTERM); |
| 592 | waitpid(tracker->pid, nullptr, 0); |
Lorenzo Colitti | 91fd580 | 2019-06-28 19:22:01 +0900 | [diff] [blame] | 593 | |
| 594 | maybeSetIptablesDropRule(false, tracker->pfx96String, tracker->v6Str); |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 595 | mClatdTrackers.erase(interface); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 596 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 597 | ALOGD("clatd on %s stopped", interface.c_str()); |
Daniel Drown | 0da73fc | 2012-06-20 16:51:39 -0500 | [diff] [blame] | 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
Maciej Żenczykowski | 1afbd99 | 2019-12-16 11:44:14 -0800 | [diff] [blame] | 602 | void ClatdController::dumpEgress(DumpWriter& dw) { |
| 603 | int mapFd = getClatEgressMapFd(); |
| 604 | if (mapFd < 0) return; // if unsupported just don't dump anything |
| 605 | BpfMap<ClatEgressKey, ClatEgressValue> configMap(mapFd); |
| 606 | |
| 607 | ScopedIndent bpfIndent(dw); |
| 608 | dw.println("BPF egress map: iif(iface) v4Addr -> v6Addr nat64Prefix oif(iface)"); |
| 609 | |
| 610 | ScopedIndent bpfDetailIndent(dw); |
| 611 | const auto printClatMap = [&dw](const ClatEgressKey& key, const ClatEgressValue& value, |
| 612 | const BpfMap<ClatEgressKey, ClatEgressValue>&) { |
| 613 | char iifStr[IFNAMSIZ] = "?"; |
| 614 | char local4Str[INET_ADDRSTRLEN] = "?"; |
| 615 | char local6Str[INET6_ADDRSTRLEN] = "?"; |
| 616 | char pfx96Str[INET6_ADDRSTRLEN] = "?"; |
| 617 | char oifStr[IFNAMSIZ] = "?"; |
| 618 | |
| 619 | if_indextoname(key.iif, iifStr); |
| 620 | inet_ntop(AF_INET, &key.local4, local4Str, sizeof(local4Str)); |
| 621 | inet_ntop(AF_INET6, &value.local6, local6Str, sizeof(local6Str)); |
| 622 | inet_ntop(AF_INET6, &value.pfx96, pfx96Str, sizeof(pfx96Str)); |
| 623 | if_indextoname(value.oif, oifStr); |
| 624 | |
| 625 | dw.println("%u(%s) %s -> %s %s/96 %u(%s)", key.iif, iifStr, local4Str, local6Str, pfx96Str, |
| 626 | value.oif, oifStr); |
| 627 | return netdutils::status::ok; |
| 628 | }; |
| 629 | auto res = configMap.iterateWithValue(printClatMap); |
| 630 | if (!isOk(res)) { |
| 631 | dw.println("Error printing BPF map: %s", res.msg().c_str()); |
| 632 | } |
| 633 | } |
| 634 | |
Maciej Żenczykowski | 7dffa6f | 2019-12-16 11:20:44 -0800 | [diff] [blame] | 635 | void ClatdController::dumpIngress(DumpWriter& dw) { |
Maciej Żenczykowski | abb2cbf | 2019-04-01 01:29:29 -0700 | [diff] [blame] | 636 | int mapFd = getClatIngressMapFd(); |
| 637 | if (mapFd < 0) return; // if unsupported just don't dump anything |
| 638 | BpfMap<ClatIngressKey, ClatIngressValue> configMap(mapFd); |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 639 | |
| 640 | ScopedIndent bpfIndent(dw); |
| 641 | dw.println("BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)"); |
| 642 | |
| 643 | ScopedIndent bpfDetailIndent(dw); |
Maciej Żenczykowski | 5526271 | 2019-03-29 23:44:56 -0700 | [diff] [blame] | 644 | const auto printClatMap = [&dw](const ClatIngressKey& key, const ClatIngressValue& value, |
| 645 | const BpfMap<ClatIngressKey, ClatIngressValue>&) { |
| 646 | char iifStr[IFNAMSIZ] = "?"; |
| 647 | char pfx96Str[INET6_ADDRSTRLEN] = "?"; |
| 648 | char local6Str[INET6_ADDRSTRLEN] = "?"; |
| 649 | char local4Str[INET_ADDRSTRLEN] = "?"; |
| 650 | char oifStr[IFNAMSIZ] = "?"; |
| 651 | |
| 652 | if_indextoname(key.iif, iifStr); |
| 653 | inet_ntop(AF_INET6, &key.pfx96, pfx96Str, sizeof(pfx96Str)); |
| 654 | inet_ntop(AF_INET6, &key.local6, local6Str, sizeof(local6Str)); |
| 655 | inet_ntop(AF_INET, &value.local4, local4Str, sizeof(local4Str)); |
| 656 | if_indextoname(value.oif, oifStr); |
| 657 | |
| 658 | dw.println("%u(%s) %s/96 %s -> %s %u(%s)", key.iif, iifStr, pfx96Str, local6Str, local4Str, |
| 659 | value.oif, oifStr); |
| 660 | return netdutils::status::ok; |
| 661 | }; |
| 662 | auto res = configMap.iterateWithValue(printClatMap); |
| 663 | if (!isOk(res)) { |
| 664 | dw.println("Error printing BPF map: %s", res.msg().c_str()); |
| 665 | } |
| 666 | } |
| 667 | |
Maciej Żenczykowski | 4c26217 | 2019-12-16 11:31:24 -0800 | [diff] [blame] | 668 | void ClatdController::dumpTrackers(DumpWriter& dw) { |
| 669 | ScopedIndent trackerIndent(dw); |
| 670 | dw.println("Trackers: iif[iface] nat64Prefix v6Addr -> v4Addr v4iif[v4iface] [netId]"); |
| 671 | |
| 672 | ScopedIndent trackerDetailIndent(dw); |
| 673 | for (const auto& pair : mClatdTrackers) { |
| 674 | const ClatdTracker& tracker = pair.second; |
| 675 | dw.println("%u[%s] %s/96 %s -> %s %u[%s] [%u]", tracker.ifIndex, tracker.iface, |
| 676 | tracker.pfx96String, tracker.v6Str, tracker.v4Str, tracker.v4ifIndex, |
| 677 | tracker.v4iface, tracker.netId); |
| 678 | } |
| 679 | } |
| 680 | |
Maciej Żenczykowski | 7dffa6f | 2019-12-16 11:20:44 -0800 | [diff] [blame] | 681 | void ClatdController::dump(DumpWriter& dw) { |
| 682 | std::lock_guard guard(mutex); |
| 683 | |
| 684 | ScopedIndent clatdIndent(dw); |
| 685 | dw.println("ClatdController"); |
| 686 | |
Maciej Żenczykowski | 4c26217 | 2019-12-16 11:31:24 -0800 | [diff] [blame] | 687 | dumpTrackers(dw); |
Maciej Żenczykowski | 7dffa6f | 2019-12-16 11:20:44 -0800 | [diff] [blame] | 688 | dumpIngress(dw); |
Maciej Żenczykowski | 1afbd99 | 2019-12-16 11:44:14 -0800 | [diff] [blame] | 689 | dumpEgress(dw); |
Maciej Żenczykowski | 7dffa6f | 2019-12-16 11:20:44 -0800 | [diff] [blame] | 690 | } |
| 691 | |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 692 | auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree; |
Lorenzo Colitti | 91fd580 | 2019-06-28 19:22:01 +0900 | [diff] [blame] | 693 | auto ClatdController::iptablesRestoreFunction = execIptablesRestore; |
Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame] | 694 | |
Lorenzo Colitti | 7035f22 | 2017-02-13 18:29:00 +0900 | [diff] [blame] | 695 | } // namespace net |
| 696 | } // namespace android |