blob: ec64d29fd485fbf551a69d3e6e64c1e9c11a8ba4 [file] [log] [blame]
Daniel Drown0da73fc2012-06-20 16:51:39 -05001/*
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 Innocenti51a0e0f2018-10-05 20:24:06 +090016
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090017#include <map>
18#include <string>
19
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090020#include <arpa/inet.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050021#include <errno.h>
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -070022#include <linux/if_arp.h>
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -070023#include <linux/if_tun.h>
24#include <linux/ioctl.h>
Maciej Żenczykowskic8c38aa2019-03-29 01:24:51 -070025#include <net/if.h>
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090026#include <netinet/in.h>
Luke Huang94c43a12019-02-14 19:51:38 +080027#include <spawn.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050028#include <sys/types.h>
29#include <sys/wait.h>
Luke Huang94c43a12019-02-14 19:51:38 +080030#include <unistd.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050031
32#define LOG_TAG "ClatdController"
Logan Chien3f461482018-04-23 14:31:32 +080033#include <log/log.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050034
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -070035#include "ClatdController.h"
Maciej Żenczykowskia1699952019-05-11 17:07:44 -070036#include "InterfaceController.h"
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -070037
38#include "android-base/properties.h"
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -070039#include "android-base/scopeguard.h"
Lorenzo Colitti91fd5802019-06-28 19:22:01 +090040#include "android-base/stringprintf.h"
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090041#include "android-base/unique_fd.h"
Maciej Żenczykowski55262712019-03-29 23:44:56 -070042#include "bpf/BpfMap.h"
43#include "netdbpf/bpf_shared.h"
44#include "netdutils/DumpWriter.h"
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090045
46extern "C" {
47#include "netutils/checksum.h"
48}
49
Maciej Żenczykowski55262712019-03-29 23:44:56 -070050#include "ClatUtils.h"
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090051#include "Fwmark.h"
Paul Jensen84c1d032014-05-30 13:29:41 -040052#include "NetdConstants.h"
53#include "NetworkController.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090054#include "netid_client.h"
Daniel Drown0da73fc2012-06-20 16:51:39 -050055
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090056static const char* kClatdPath = "/system/bin/clatd";
57
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090058// 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).
60static const char* kV4AddrString = "192.0.0.4";
61static const in_addr kV4Addr = {inet_addr(kV4AddrString)};
62static const int kV4AddrLen = 29;
63
Lorenzo Colitti91fd5802019-06-28 19:22:01 +090064using android::base::StringPrintf;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090065using android::base::unique_fd;
Maciej Żenczykowski55262712019-03-29 23:44:56 -070066using android::bpf::BpfMap;
67using android::netdutils::DumpWriter;
68using android::netdutils::ScopedIndent;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090069
Lorenzo Colitti7035f222017-02-13 18:29:00 +090070namespace android {
71namespace net {
72
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080073void 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 Żenczykowski35bc6ed2019-12-16 15:39:04 -080089void 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 Żenczykowski56280272019-03-30 03:32:51 -0700105void ClatdController::init(void) {
106 std::lock_guard guard(mutex);
107
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700108 // 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 Żenczykowski0bd8da72019-12-16 15:16:28 -0800142 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 Żenczykowski1c086e52019-03-29 23:13:49 -0700151 rv = getClatIngressMapFd();
152 if (rv < 0) {
153 ALOGE("getClatIngressMapFd() failure: %s", strerror(-rv));
154 mClatEbpfMode = ClatEbpfDisabled;
Maciej Żenczykowski0bd8da72019-12-16 15:16:28 -0800155 mClatEgressMap.reset(-1);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700156 mNetlinkFd.reset(-1);
157 return;
158 }
159 mClatIngressMap.reset(rv);
160
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -0800161 resetEgressMap();
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -0800162 resetIngressMap();
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700163}
164
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900165bool 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 Desaulniers6b357502019-10-11 09:26:44 -0700173 struct sockaddr_in sin = {
174 .sin_family = AF_INET,
Maciej Żenczykowski268190e2019-10-31 23:47:53 -0700175 .sin_port = htons(53),
Nick Desaulniers6b357502019-10-11 09:26:44 -0700176 .sin_addr = {addr},
177 };
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900178 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 Colittiac7fefc2014-10-20 17:14:13 +0900185}
Daniel Drown0da73fc2012-06-20 16:51:39 -0500186
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900187// 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
191in_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 Colittiac7fefc2014-10-20 17:14:13 +0900196
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900197 // 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.
216void 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.
239int 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 Huang6d301232018-08-01 14:05:18 +0800245 return -errno;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500246 }
247
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900248 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 Żenczykowski1c086e52019-03-29 23:13:49 -0700270void 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
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800295 // This program will be attached to the v4-* interface which is a TUN and thus always rawip.
296 rv = getClatEgressProgFd(false);
297 if (rv < 0) {
298 ALOGE("getClatEgressProgFd(false) failure: %s", strerror(-rv));
299 return;
300 }
301 unique_fd txRawIpProgFd(rv);
302
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700303 rv = getClatIngressProgFd(isEthernet);
304 if (rv < 0) {
305 ALOGE("getClatIngressProgFd(%d) failure: %s", isEthernet, strerror(-rv));
306 return;
307 }
Maciej Żenczykowski75f4dc32019-12-16 18:38:54 -0800308 unique_fd rxProgFd(rv);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700309
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800310 ClatEgressKey txKey = {
311 .iif = tracker.v4ifIndex,
312 .local4 = tracker.v4,
313 };
314 ClatEgressValue txValue = {
315 .oif = tracker.ifIndex,
316 .local6 = tracker.v6,
317 .pfx96 = tracker.pfx96,
Maciej Żenczykowski97ec1d82019-12-17 12:11:21 -0800318 .oifIsEthernet = isEthernet,
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800319 };
320
321 auto ret = mClatEgressMap.writeValue(txKey, txValue, BPF_ANY);
322 if (!isOk(ret)) {
323 ALOGE("mClatEgress.Map.writeValue failure: %s", strerror(ret.code()));
324 return;
325 }
326
327 ClatIngressKey rxKey = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700328 .iif = tracker.ifIndex,
329 .pfx96 = tracker.pfx96,
330 .local6 = tracker.v6,
331 };
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800332 ClatIngressValue rxValue = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700333 // TODO: move all the clat code to eBPF and remove the tun interface entirely.
Maciej Żenczykowski39b0b902019-05-08 00:36:30 -0700334 .oif = tracker.v4ifIndex,
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700335 .local4 = tracker.v4,
336 };
337
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800338 ret = mClatIngressMap.writeValue(rxKey, rxValue, BPF_ANY);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700339 if (!isOk(ret)) {
340 ALOGE("mClatIngress.Map.writeValue failure: %s", strerror(ret.code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800341 ret = mClatEgressMap.deleteValue(txKey);
342 if (!isOk(ret)) ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700343 return;
344 }
345
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800346 // We do tc setup *after* populating the maps, so scanning through them
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700347 // can always be used to tell us what needs cleanup.
348
349 rv = tcQdiscAddDevClsact(mNetlinkFd, tracker.ifIndex);
350 if (rv) {
351 ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
352 strerror(-rv));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800353 ret = mClatEgressMap.deleteValue(txKey);
354 if (!isOk(ret)) ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.code()));
355 ret = mClatIngressMap.deleteValue(rxKey);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700356 if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code()));
357 return;
358 }
359
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800360 rv = tcQdiscAddDevClsact(mNetlinkFd, tracker.v4ifIndex);
361 if (rv) {
362 ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", tracker.v4ifIndex, tracker.v4iface,
363 strerror(-rv));
364 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex);
365 if (rv < 0) {
366 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
367 strerror(-rv));
368 }
369 ret = mClatEgressMap.deleteValue(txKey);
370 if (!isOk(ret)) ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.code()));
371 ret = mClatIngressMap.deleteValue(rxKey);
372 if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code()));
373 return;
374 }
375
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800376 rv = tcFilterAddDevEgressBpf(mNetlinkFd, tracker.v4ifIndex, txRawIpProgFd, false);
377 if (rv) {
378 if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) {
379 ALOGI("tcFilterAddDevEgressBpf(%d[%s], false): %s", tracker.v4ifIndex, tracker.v4iface,
380 strerror(-rv));
381 } else {
382 ALOGE("tcFilterAddDevEgressBpf(%d[%s], false) failure: %s", tracker.v4ifIndex,
383 tracker.v4iface, strerror(-rv));
384 }
385 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex);
386 if (rv) {
387 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
388 strerror(-rv));
389 }
390 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.v4ifIndex);
391 if (rv) {
392 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.v4ifIndex, tracker.v4iface,
393 strerror(-rv));
394 }
395 ret = mClatEgressMap.deleteValue(txKey);
396 if (!isOk(ret)) ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.code()));
397 ret = mClatIngressMap.deleteValue(rxKey);
398 if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code()));
399 return;
400 }
401
Maciej Żenczykowski75f4dc32019-12-16 18:38:54 -0800402 rv = tcFilterAddDevIngressBpf(mNetlinkFd, tracker.ifIndex, rxProgFd, isEthernet);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700403 if (rv) {
404 if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) {
Maciej Żenczykowskib140a3a2019-12-15 11:53:46 -0800405 ALOGI("tcFilterAddDevIngressBpf(%d[%s], %d): %s", tracker.ifIndex, tracker.iface,
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700406 isEthernet, strerror(-rv));
Maciej Żenczykowskib140a3a2019-12-15 11:53:46 -0800407 } else {
408 ALOGE("tcFilterAddDevIngressBpf(%d[%s], %d) failure: %s", tracker.ifIndex,
409 tracker.iface, isEthernet, strerror(-rv));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700410 }
411 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex);
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800412 if (rv) {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700413 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
414 strerror(-rv));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800415 }
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800416 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.v4ifIndex);
417 if (rv) {
418 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.v4ifIndex, tracker.v4iface,
419 strerror(-rv));
420 }
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800421 ret = mClatEgressMap.deleteValue(txKey);
422 if (!isOk(ret)) ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.code()));
423 ret = mClatIngressMap.deleteValue(rxKey);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700424 if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code()));
425 return;
426 }
427
428 // success
429}
430
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900431void ClatdController::maybeSetIptablesDropRule(bool add, const char* pfx96Str, const char* v6Str) {
432 if (mClatEbpfMode == ClatEbpfDisabled) return;
433
434 std::string cmd = StringPrintf(
435 "*raw\n"
436 "%s %s -s %s/96 -d %s -j DROP\n"
437 "COMMIT\n",
438 (add ? "-A" : "-D"), LOCAL_RAW_PREROUTING, pfx96Str, v6Str);
439
440 iptablesRestoreFunction(V6, cmd);
441}
442
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700443void ClatdController::maybeStopBpf(const ClatdTracker& tracker) {
444 if (mClatEbpfMode == ClatEbpfDisabled) return;
445
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800446 // No need to remove filters, since we remove qdiscs they are attached to,
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700447 // which automatically removes everything attached to the qdisc.
448 int rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex);
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800449 if (rv < 0) {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700450 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
451 strerror(-rv));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800452 }
453
454 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.v4ifIndex);
455 if (rv < 0) {
456 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.v4ifIndex, tracker.v4iface,
457 strerror(-rv));
458 }
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700459
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800460 // We cleanup the maps last, so scanning through them can be used to
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700461 // determine what still needs cleanup.
462
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800463 ClatEgressKey txKey = {
464 .iif = tracker.v4ifIndex,
465 .local4 = tracker.v4,
466 };
467
468 auto ret = mClatEgressMap.deleteValue(txKey);
469 if (!isOk(ret)) ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.code()));
470
471 ClatIngressKey rxKey = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700472 .iif = tracker.ifIndex,
473 .pfx96 = tracker.pfx96,
474 .local6 = tracker.v6,
475 };
476
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800477 ret = mClatIngressMap.deleteValue(rxKey);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700478 if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code()));
479}
480
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900481// Finds the tracker of the clatd running on interface |interface|, or nullptr if clatd has not been
482// started on |interface|.
483ClatdController::ClatdTracker* ClatdController::getClatdTracker(const std::string& interface) {
484 auto it = mClatdTrackers.find(interface);
485 return (it == mClatdTrackers.end() ? nullptr : &it->second);
486}
487
488// Initializes a ClatdTracker for the specified interface.
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700489int ClatdController::ClatdTracker::init(unsigned networkId, const std::string& interface,
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700490 const std::string& v4interface,
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900491 const std::string& nat64Prefix) {
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700492 netId = networkId;
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900493
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900494 fwmark.netId = netId;
495 fwmark.explicitlySelected = true;
496 fwmark.protectedFromVpn = true;
497 fwmark.permission = PERMISSION_SYSTEM;
498
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900499 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900500 snprintf(netIdString, sizeof(netIdString), "%u", netId);
501 strlcpy(iface, interface.c_str(), sizeof(iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700502 ifIndex = if_nametoindex(iface);
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700503 strlcpy(v4iface, v4interface.c_str(), sizeof(v4iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700504 v4ifIndex = if_nametoindex(v4iface);
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900505
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900506 // Pass in everything that clatd needs: interface, a netid to use for DNS lookups, a fwmark for
507 // outgoing packets, the NAT64 prefix, and the IPv4 and IPv6 addresses.
508 // Validate the prefix and strip off the prefix length.
509 uint8_t family;
510 uint8_t prefixLen;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700511 int res = parsePrefix(nat64Prefix.c_str(), &family, &pfx96, sizeof(pfx96), &prefixLen);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900512 // clatd only supports /96 prefixes.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700513 if (res != sizeof(pfx96)) return res;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900514 if (family != AF_INET6) return -EAFNOSUPPORT;
515 if (prefixLen != 96) return -EINVAL;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700516 if (!inet_ntop(AF_INET6, &pfx96, pfx96String, sizeof(pfx96String))) return -errno;
Luke Huang6d301232018-08-01 14:05:18 +0800517
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900518 // Pick an IPv4 address.
519 // TODO: this picks the address based on other addresses that are assigned to interfaces, but
520 // the address is only actually assigned to an interface once clatd starts up. So we could end
521 // up with two clatd instances with the same IPv4 address.
522 // Stop doing this and instead pick a free one from the kV4Addr pool.
Maciej Żenczykowski55cacfb2019-03-30 02:01:35 -0700523 v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900524 if (v4.s_addr == INADDR_NONE) {
525 ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen);
526 return -EADDRNOTAVAIL;
527 }
528 if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno;
529
530 // Generate a checksum-neutral IID.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700531 if (generateIpv6Address(iface, v4, pfx96, &v6)) {
532 ALOGE("Unable to find global source address on %s for %s", iface, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900533 return -EADDRNOTAVAIL;
534 }
535 if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno;
536
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700537 ALOGD("starting clatd on %s v4=%s v6=%s pfx96=%s", iface, v4Str, v6Str, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900538 return 0;
539}
540
541int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix,
542 std::string* v6Str) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700543 std::lock_guard guard(mutex);
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700544
545 // 1. fail if pre-existing tracker already exists
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900546 ClatdTracker* existing = getClatdTracker(interface);
547 if (existing != nullptr) {
548 ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700549 return -EBUSY;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900550 }
551
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700552 // 2. get network id associated with this external interface
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700553 unsigned networkId = mNetCtrl->getNetworkForInterface(interface.c_str());
554 if (networkId == NETID_UNSET) {
555 ALOGE("Interface %s not assigned to any netId", interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700556 return -ENODEV;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900557 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700558
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700559 // 3. open the tun device in non blocking mode as required by clatd
560 int res = open("/dev/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC);
561 if (res == -1) {
562 res = errno;
563 ALOGE("open of tun device failed (%s)", strerror(res));
564 return -res;
565 }
566 unique_fd tmpTunFd(res);
567
568 // 4. create the v4-... tun interface
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700569 std::string v4interface("v4-");
570 v4interface += interface;
571
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700572 struct ifreq ifr = {
573 .ifr_flags = IFF_TUN,
574 };
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700575 strlcpy(ifr.ifr_name, v4interface.c_str(), sizeof(ifr.ifr_name));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700576
577 res = ioctl(tmpTunFd, TUNSETIFF, &ifr, sizeof(ifr));
578 if (res == -1) {
579 res = errno;
580 ALOGE("ioctl(TUNSETIFF) failed (%s)", strerror(res));
581 return -res;
582 }
583
Maciej Żenczykowskia1699952019-05-11 17:07:44 -0700584 // disable IPv6 on it - failing to do so is not a critical error
585 res = InterfaceController::setEnableIPv6(v4interface.c_str(), 0);
586 if (res) ALOGE("setEnableIPv6 %s failed (%s)", v4interface.c_str(), strerror(res));
587
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700588 // 5. initialize tracker object
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700589 ClatdTracker tracker;
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700590 int ret = tracker.init(networkId, interface, v4interface, nat64Prefix);
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700591 if (ret) return ret;
592
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700593 // 6. create a throwaway socket to reserve a file descriptor number
594 res = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
595 if (res == -1) {
596 res = errno;
597 ALOGE("socket(ipv6/udp) failed (%s)", strerror(res));
598 return -res;
599 }
600 unique_fd passedTunFd(res);
601
602 // 7. this is the FD we'll pass to clatd on the cli, so need it as a string
603 char passedTunFdStr[INT32_STRLEN];
604 snprintf(passedTunFdStr, sizeof(passedTunFdStr), "%d", passedTunFd.get());
605
606 // 8. we're going to use this as argv[0] to clatd to make ps output more useful
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900607 std::string progname("clatd-");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900608 progname += tracker.iface;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500609
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900610 // clang-format off
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700611 const char* args[] = {progname.c_str(),
612 "-i", tracker.iface,
613 "-n", tracker.netIdString,
614 "-m", tracker.fwmarkString,
615 "-p", tracker.pfx96String,
616 "-4", tracker.v4Str,
617 "-6", tracker.v6Str,
618 "-t", passedTunFdStr,
619 nullptr};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900620 // clang-format on
621
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700622 // 9. register vfork requirement
623 posix_spawnattr_t attr;
624 res = posix_spawnattr_init(&attr);
625 if (res) {
626 ALOGE("posix_spawnattr_init failed (%s)", strerror(res));
627 return -res;
628 }
629 const android::base::ScopeGuard attrGuard = [&] { posix_spawnattr_destroy(&attr); };
630 res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_USEVFORK);
631 if (res) {
632 ALOGE("posix_spawnattr_setflags failed (%s)", strerror(res));
633 return -res;
634 }
635
636 // 10. register dup2() action: this is what 'clears' the CLOEXEC flag
637 // on the tun fd that we want the child clatd process to inherit
638 // (this will happen after the vfork, and before the execve)
639 posix_spawn_file_actions_t fa;
640 res = posix_spawn_file_actions_init(&fa);
641 if (res) {
642 ALOGE("posix_spawn_file_actions_init failed (%s)", strerror(res));
643 return -res;
644 }
645 const android::base::ScopeGuard faGuard = [&] { posix_spawn_file_actions_destroy(&fa); };
646 res = posix_spawn_file_actions_adddup2(&fa, tmpTunFd, passedTunFd);
647 if (res) {
648 ALOGE("posix_spawn_file_actions_adddup2 failed (%s)", strerror(res));
649 return -res;
650 }
651
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900652 // 11. If necessary, add the drop rule for iptables.
653 maybeSetIptablesDropRule(true, tracker.pfx96String, tracker.v6Str);
654
655 // 12. actually perform vfork/dup2/execve
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700656 res = posix_spawn(&tracker.pid, kClatdPath, &fa, &attr, (char* const*)args, nullptr);
Luke Huang40962442019-02-26 11:46:10 +0800657 if (res) {
658 ALOGE("posix_spawn failed (%s)", strerror(res));
Luke Huang6d301232018-08-01 14:05:18 +0800659 return -res;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500660 }
661
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900662 // 13. configure eBPF offload - if possible
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700663 maybeStartBpf(tracker);
664
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900665 mClatdTrackers[interface] = tracker;
666 ALOGD("clatd started on %s", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500667
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900668 *v6Str = tracker.v6Str;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500669 return 0;
670}
671
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900672int ClatdController::stopClatd(const std::string& interface) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700673 std::lock_guard guard(mutex);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900674 ClatdTracker* tracker = getClatdTracker(interface);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900675
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900676 if (tracker == nullptr) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500677 ALOGE("clatd already stopped");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900678 return -ENODEV;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500679 }
680
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900681 ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500682
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700683 maybeStopBpf(*tracker);
684
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900685 kill(tracker->pid, SIGTERM);
686 waitpid(tracker->pid, nullptr, 0);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900687
688 maybeSetIptablesDropRule(false, tracker->pfx96String, tracker->v6Str);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900689 mClatdTrackers.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500690
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900691 ALOGD("clatd on %s stopped", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500692
693 return 0;
694}
695
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800696void ClatdController::dumpEgress(DumpWriter& dw) {
697 int mapFd = getClatEgressMapFd();
698 if (mapFd < 0) return; // if unsupported just don't dump anything
699 BpfMap<ClatEgressKey, ClatEgressValue> configMap(mapFd);
700
701 ScopedIndent bpfIndent(dw);
702 dw.println("BPF egress map: iif(iface) v4Addr -> v6Addr nat64Prefix oif(iface)");
703
704 ScopedIndent bpfDetailIndent(dw);
705 const auto printClatMap = [&dw](const ClatEgressKey& key, const ClatEgressValue& value,
706 const BpfMap<ClatEgressKey, ClatEgressValue>&) {
707 char iifStr[IFNAMSIZ] = "?";
708 char local4Str[INET_ADDRSTRLEN] = "?";
709 char local6Str[INET6_ADDRSTRLEN] = "?";
710 char pfx96Str[INET6_ADDRSTRLEN] = "?";
711 char oifStr[IFNAMSIZ] = "?";
712
713 if_indextoname(key.iif, iifStr);
714 inet_ntop(AF_INET, &key.local4, local4Str, sizeof(local4Str));
715 inet_ntop(AF_INET6, &value.local6, local6Str, sizeof(local6Str));
716 inet_ntop(AF_INET6, &value.pfx96, pfx96Str, sizeof(pfx96Str));
717 if_indextoname(value.oif, oifStr);
718
Maciej Żenczykowski97ec1d82019-12-17 12:11:21 -0800719 dw.println("%u(%s) %s -> %s %s/96 %u(%s) %s", key.iif, iifStr, local4Str, local6Str,
720 pfx96Str, value.oif, oifStr, value.oifIsEthernet ? "ether" : "rawip");
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800721 return netdutils::status::ok;
722 };
723 auto res = configMap.iterateWithValue(printClatMap);
724 if (!isOk(res)) {
725 dw.println("Error printing BPF map: %s", res.msg().c_str());
726 }
727}
728
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800729void ClatdController::dumpIngress(DumpWriter& dw) {
Maciej Żenczykowskiabb2cbf2019-04-01 01:29:29 -0700730 int mapFd = getClatIngressMapFd();
731 if (mapFd < 0) return; // if unsupported just don't dump anything
732 BpfMap<ClatIngressKey, ClatIngressValue> configMap(mapFd);
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700733
734 ScopedIndent bpfIndent(dw);
735 dw.println("BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)");
736
737 ScopedIndent bpfDetailIndent(dw);
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700738 const auto printClatMap = [&dw](const ClatIngressKey& key, const ClatIngressValue& value,
739 const BpfMap<ClatIngressKey, ClatIngressValue>&) {
740 char iifStr[IFNAMSIZ] = "?";
741 char pfx96Str[INET6_ADDRSTRLEN] = "?";
742 char local6Str[INET6_ADDRSTRLEN] = "?";
743 char local4Str[INET_ADDRSTRLEN] = "?";
744 char oifStr[IFNAMSIZ] = "?";
745
746 if_indextoname(key.iif, iifStr);
747 inet_ntop(AF_INET6, &key.pfx96, pfx96Str, sizeof(pfx96Str));
748 inet_ntop(AF_INET6, &key.local6, local6Str, sizeof(local6Str));
749 inet_ntop(AF_INET, &value.local4, local4Str, sizeof(local4Str));
750 if_indextoname(value.oif, oifStr);
751
752 dw.println("%u(%s) %s/96 %s -> %s %u(%s)", key.iif, iifStr, pfx96Str, local6Str, local4Str,
753 value.oif, oifStr);
754 return netdutils::status::ok;
755 };
756 auto res = configMap.iterateWithValue(printClatMap);
757 if (!isOk(res)) {
758 dw.println("Error printing BPF map: %s", res.msg().c_str());
759 }
760}
761
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800762void ClatdController::dumpTrackers(DumpWriter& dw) {
763 ScopedIndent trackerIndent(dw);
764 dw.println("Trackers: iif[iface] nat64Prefix v6Addr -> v4Addr v4iif[v4iface] [netId]");
765
766 ScopedIndent trackerDetailIndent(dw);
767 for (const auto& pair : mClatdTrackers) {
768 const ClatdTracker& tracker = pair.second;
769 dw.println("%u[%s] %s/96 %s -> %s %u[%s] [%u]", tracker.ifIndex, tracker.iface,
770 tracker.pfx96String, tracker.v6Str, tracker.v4Str, tracker.v4ifIndex,
771 tracker.v4iface, tracker.netId);
772 }
773}
774
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800775void ClatdController::dump(DumpWriter& dw) {
776 std::lock_guard guard(mutex);
777
778 ScopedIndent clatdIndent(dw);
779 dw.println("ClatdController");
780
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800781 dumpTrackers(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800782 dumpIngress(dw);
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800783 dumpEgress(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800784}
785
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900786auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree;
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900787auto ClatdController::iptablesRestoreFunction = execIptablesRestore;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900788
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900789} // namespace net
790} // namespace android