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