blob: aaa03c17fff06b87e9c6918be447ff35163b0fda [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
Maciej Żenczykowski083688f2019-12-23 14:43:09 -0800431void ClatdController::setIptablesDropRule(bool add, const char* pfx96Str, const char* v6Str) {
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900432 std::string cmd = StringPrintf(
433 "*raw\n"
434 "%s %s -s %s/96 -d %s -j DROP\n"
435 "COMMIT\n",
436 (add ? "-A" : "-D"), LOCAL_RAW_PREROUTING, pfx96Str, v6Str);
437
438 iptablesRestoreFunction(V6, cmd);
439}
440
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700441void ClatdController::maybeStopBpf(const ClatdTracker& tracker) {
442 if (mClatEbpfMode == ClatEbpfDisabled) return;
443
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800444 // No need to remove filters, since we remove qdiscs they are attached to,
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700445 // which automatically removes everything attached to the qdisc.
446 int rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex);
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800447 if (rv < 0) {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700448 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
449 strerror(-rv));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800450 }
451
452 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.v4ifIndex);
453 if (rv < 0) {
454 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.v4ifIndex, tracker.v4iface,
455 strerror(-rv));
456 }
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700457
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800458 // We cleanup the maps last, so scanning through them can be used to
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700459 // determine what still needs cleanup.
460
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800461 ClatEgressKey txKey = {
462 .iif = tracker.v4ifIndex,
463 .local4 = tracker.v4,
464 };
465
466 auto ret = mClatEgressMap.deleteValue(txKey);
467 if (!isOk(ret)) ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.code()));
468
469 ClatIngressKey rxKey = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700470 .iif = tracker.ifIndex,
471 .pfx96 = tracker.pfx96,
472 .local6 = tracker.v6,
473 };
474
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800475 ret = mClatIngressMap.deleteValue(rxKey);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700476 if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code()));
477}
478
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900479// Finds the tracker of the clatd running on interface |interface|, or nullptr if clatd has not been
480// started on |interface|.
481ClatdController::ClatdTracker* ClatdController::getClatdTracker(const std::string& interface) {
482 auto it = mClatdTrackers.find(interface);
483 return (it == mClatdTrackers.end() ? nullptr : &it->second);
484}
485
486// Initializes a ClatdTracker for the specified interface.
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700487int ClatdController::ClatdTracker::init(unsigned networkId, const std::string& interface,
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700488 const std::string& v4interface,
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900489 const std::string& nat64Prefix) {
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700490 netId = networkId;
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900491
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900492 fwmark.netId = netId;
493 fwmark.explicitlySelected = true;
494 fwmark.protectedFromVpn = true;
495 fwmark.permission = PERMISSION_SYSTEM;
496
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900497 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900498 snprintf(netIdString, sizeof(netIdString), "%u", netId);
499 strlcpy(iface, interface.c_str(), sizeof(iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700500 ifIndex = if_nametoindex(iface);
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700501 strlcpy(v4iface, v4interface.c_str(), sizeof(v4iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700502 v4ifIndex = if_nametoindex(v4iface);
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900503
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900504 // Pass in everything that clatd needs: interface, a netid to use for DNS lookups, a fwmark for
505 // outgoing packets, the NAT64 prefix, and the IPv4 and IPv6 addresses.
506 // Validate the prefix and strip off the prefix length.
507 uint8_t family;
508 uint8_t prefixLen;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700509 int res = parsePrefix(nat64Prefix.c_str(), &family, &pfx96, sizeof(pfx96), &prefixLen);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900510 // clatd only supports /96 prefixes.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700511 if (res != sizeof(pfx96)) return res;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900512 if (family != AF_INET6) return -EAFNOSUPPORT;
513 if (prefixLen != 96) return -EINVAL;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700514 if (!inet_ntop(AF_INET6, &pfx96, pfx96String, sizeof(pfx96String))) return -errno;
Luke Huang6d301232018-08-01 14:05:18 +0800515
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900516 // Pick an IPv4 address.
517 // TODO: this picks the address based on other addresses that are assigned to interfaces, but
518 // the address is only actually assigned to an interface once clatd starts up. So we could end
519 // up with two clatd instances with the same IPv4 address.
520 // Stop doing this and instead pick a free one from the kV4Addr pool.
Maciej Żenczykowski55cacfb2019-03-30 02:01:35 -0700521 v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900522 if (v4.s_addr == INADDR_NONE) {
523 ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen);
524 return -EADDRNOTAVAIL;
525 }
526 if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno;
527
528 // Generate a checksum-neutral IID.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700529 if (generateIpv6Address(iface, v4, pfx96, &v6)) {
530 ALOGE("Unable to find global source address on %s for %s", iface, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900531 return -EADDRNOTAVAIL;
532 }
533 if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno;
534
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700535 ALOGD("starting clatd on %s v4=%s v6=%s pfx96=%s", iface, v4Str, v6Str, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900536 return 0;
537}
538
539int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix,
540 std::string* v6Str) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700541 std::lock_guard guard(mutex);
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700542
543 // 1. fail if pre-existing tracker already exists
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900544 ClatdTracker* existing = getClatdTracker(interface);
545 if (existing != nullptr) {
546 ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700547 return -EBUSY;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900548 }
549
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700550 // 2. get network id associated with this external interface
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700551 unsigned networkId = mNetCtrl->getNetworkForInterface(interface.c_str());
552 if (networkId == NETID_UNSET) {
553 ALOGE("Interface %s not assigned to any netId", interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700554 return -ENODEV;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900555 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700556
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700557 // 3. open the tun device in non blocking mode as required by clatd
558 int res = open("/dev/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC);
559 if (res == -1) {
560 res = errno;
561 ALOGE("open of tun device failed (%s)", strerror(res));
562 return -res;
563 }
564 unique_fd tmpTunFd(res);
565
566 // 4. create the v4-... tun interface
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700567 std::string v4interface("v4-");
568 v4interface += interface;
569
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700570 struct ifreq ifr = {
571 .ifr_flags = IFF_TUN,
572 };
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700573 strlcpy(ifr.ifr_name, v4interface.c_str(), sizeof(ifr.ifr_name));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700574
575 res = ioctl(tmpTunFd, TUNSETIFF, &ifr, sizeof(ifr));
576 if (res == -1) {
577 res = errno;
578 ALOGE("ioctl(TUNSETIFF) failed (%s)", strerror(res));
579 return -res;
580 }
581
Maciej Żenczykowskia1699952019-05-11 17:07:44 -0700582 // disable IPv6 on it - failing to do so is not a critical error
583 res = InterfaceController::setEnableIPv6(v4interface.c_str(), 0);
584 if (res) ALOGE("setEnableIPv6 %s failed (%s)", v4interface.c_str(), strerror(res));
585
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700586 // 5. initialize tracker object
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700587 ClatdTracker tracker;
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700588 int ret = tracker.init(networkId, interface, v4interface, nat64Prefix);
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700589 if (ret) return ret;
590
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700591 // 6. create a throwaway socket to reserve a file descriptor number
592 res = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
593 if (res == -1) {
594 res = errno;
595 ALOGE("socket(ipv6/udp) failed (%s)", strerror(res));
596 return -res;
597 }
598 unique_fd passedTunFd(res);
599
600 // 7. this is the FD we'll pass to clatd on the cli, so need it as a string
601 char passedTunFdStr[INT32_STRLEN];
602 snprintf(passedTunFdStr, sizeof(passedTunFdStr), "%d", passedTunFd.get());
603
604 // 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 +0900605 std::string progname("clatd-");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900606 progname += tracker.iface;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500607
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900608 // clang-format off
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700609 const char* args[] = {progname.c_str(),
610 "-i", tracker.iface,
611 "-n", tracker.netIdString,
612 "-m", tracker.fwmarkString,
613 "-p", tracker.pfx96String,
614 "-4", tracker.v4Str,
615 "-6", tracker.v6Str,
616 "-t", passedTunFdStr,
617 nullptr};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900618 // clang-format on
619
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700620 // 9. register vfork requirement
621 posix_spawnattr_t attr;
622 res = posix_spawnattr_init(&attr);
623 if (res) {
624 ALOGE("posix_spawnattr_init failed (%s)", strerror(res));
625 return -res;
626 }
627 const android::base::ScopeGuard attrGuard = [&] { posix_spawnattr_destroy(&attr); };
628 res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_USEVFORK);
629 if (res) {
630 ALOGE("posix_spawnattr_setflags failed (%s)", strerror(res));
631 return -res;
632 }
633
634 // 10. register dup2() action: this is what 'clears' the CLOEXEC flag
635 // on the tun fd that we want the child clatd process to inherit
636 // (this will happen after the vfork, and before the execve)
637 posix_spawn_file_actions_t fa;
638 res = posix_spawn_file_actions_init(&fa);
639 if (res) {
640 ALOGE("posix_spawn_file_actions_init failed (%s)", strerror(res));
641 return -res;
642 }
643 const android::base::ScopeGuard faGuard = [&] { posix_spawn_file_actions_destroy(&fa); };
644 res = posix_spawn_file_actions_adddup2(&fa, tmpTunFd, passedTunFd);
645 if (res) {
646 ALOGE("posix_spawn_file_actions_adddup2 failed (%s)", strerror(res));
647 return -res;
648 }
649
Maciej Żenczykowski083688f2019-12-23 14:43:09 -0800650 // 11. add the drop rule for iptables.
651 setIptablesDropRule(true, tracker.pfx96String, tracker.v6Str);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900652
653 // 12. actually perform vfork/dup2/execve
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700654 res = posix_spawn(&tracker.pid, kClatdPath, &fa, &attr, (char* const*)args, nullptr);
Luke Huang40962442019-02-26 11:46:10 +0800655 if (res) {
656 ALOGE("posix_spawn failed (%s)", strerror(res));
Luke Huang6d301232018-08-01 14:05:18 +0800657 return -res;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500658 }
659
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900660 // 13. configure eBPF offload - if possible
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700661 maybeStartBpf(tracker);
662
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900663 mClatdTrackers[interface] = tracker;
664 ALOGD("clatd started on %s", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500665
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900666 *v6Str = tracker.v6Str;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500667 return 0;
668}
669
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900670int ClatdController::stopClatd(const std::string& interface) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700671 std::lock_guard guard(mutex);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900672 ClatdTracker* tracker = getClatdTracker(interface);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900673
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900674 if (tracker == nullptr) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500675 ALOGE("clatd already stopped");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900676 return -ENODEV;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500677 }
678
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900679 ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500680
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700681 maybeStopBpf(*tracker);
682
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900683 kill(tracker->pid, SIGTERM);
684 waitpid(tracker->pid, nullptr, 0);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900685
Maciej Żenczykowski083688f2019-12-23 14:43:09 -0800686 setIptablesDropRule(false, tracker->pfx96String, tracker->v6Str);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900687 mClatdTrackers.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500688
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900689 ALOGD("clatd on %s stopped", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500690
691 return 0;
692}
693
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800694void ClatdController::dumpEgress(DumpWriter& dw) {
695 int mapFd = getClatEgressMapFd();
696 if (mapFd < 0) return; // if unsupported just don't dump anything
697 BpfMap<ClatEgressKey, ClatEgressValue> configMap(mapFd);
698
699 ScopedIndent bpfIndent(dw);
700 dw.println("BPF egress map: iif(iface) v4Addr -> v6Addr nat64Prefix oif(iface)");
701
702 ScopedIndent bpfDetailIndent(dw);
703 const auto printClatMap = [&dw](const ClatEgressKey& key, const ClatEgressValue& value,
704 const BpfMap<ClatEgressKey, ClatEgressValue>&) {
705 char iifStr[IFNAMSIZ] = "?";
706 char local4Str[INET_ADDRSTRLEN] = "?";
707 char local6Str[INET6_ADDRSTRLEN] = "?";
708 char pfx96Str[INET6_ADDRSTRLEN] = "?";
709 char oifStr[IFNAMSIZ] = "?";
710
711 if_indextoname(key.iif, iifStr);
712 inet_ntop(AF_INET, &key.local4, local4Str, sizeof(local4Str));
713 inet_ntop(AF_INET6, &value.local6, local6Str, sizeof(local6Str));
714 inet_ntop(AF_INET6, &value.pfx96, pfx96Str, sizeof(pfx96Str));
715 if_indextoname(value.oif, oifStr);
716
Maciej Żenczykowski97ec1d82019-12-17 12:11:21 -0800717 dw.println("%u(%s) %s -> %s %s/96 %u(%s) %s", key.iif, iifStr, local4Str, local6Str,
718 pfx96Str, value.oif, oifStr, value.oifIsEthernet ? "ether" : "rawip");
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800719 return netdutils::status::ok;
720 };
721 auto res = configMap.iterateWithValue(printClatMap);
722 if (!isOk(res)) {
723 dw.println("Error printing BPF map: %s", res.msg().c_str());
724 }
725}
726
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800727void ClatdController::dumpIngress(DumpWriter& dw) {
Maciej Żenczykowskiabb2cbf2019-04-01 01:29:29 -0700728 int mapFd = getClatIngressMapFd();
729 if (mapFd < 0) return; // if unsupported just don't dump anything
730 BpfMap<ClatIngressKey, ClatIngressValue> configMap(mapFd);
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700731
732 ScopedIndent bpfIndent(dw);
733 dw.println("BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)");
734
735 ScopedIndent bpfDetailIndent(dw);
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700736 const auto printClatMap = [&dw](const ClatIngressKey& key, const ClatIngressValue& value,
737 const BpfMap<ClatIngressKey, ClatIngressValue>&) {
738 char iifStr[IFNAMSIZ] = "?";
739 char pfx96Str[INET6_ADDRSTRLEN] = "?";
740 char local6Str[INET6_ADDRSTRLEN] = "?";
741 char local4Str[INET_ADDRSTRLEN] = "?";
742 char oifStr[IFNAMSIZ] = "?";
743
744 if_indextoname(key.iif, iifStr);
745 inet_ntop(AF_INET6, &key.pfx96, pfx96Str, sizeof(pfx96Str));
746 inet_ntop(AF_INET6, &key.local6, local6Str, sizeof(local6Str));
747 inet_ntop(AF_INET, &value.local4, local4Str, sizeof(local4Str));
748 if_indextoname(value.oif, oifStr);
749
750 dw.println("%u(%s) %s/96 %s -> %s %u(%s)", key.iif, iifStr, pfx96Str, local6Str, local4Str,
751 value.oif, oifStr);
752 return netdutils::status::ok;
753 };
754 auto res = configMap.iterateWithValue(printClatMap);
755 if (!isOk(res)) {
756 dw.println("Error printing BPF map: %s", res.msg().c_str());
757 }
758}
759
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800760void ClatdController::dumpTrackers(DumpWriter& dw) {
761 ScopedIndent trackerIndent(dw);
762 dw.println("Trackers: iif[iface] nat64Prefix v6Addr -> v4Addr v4iif[v4iface] [netId]");
763
764 ScopedIndent trackerDetailIndent(dw);
765 for (const auto& pair : mClatdTrackers) {
766 const ClatdTracker& tracker = pair.second;
767 dw.println("%u[%s] %s/96 %s -> %s %u[%s] [%u]", tracker.ifIndex, tracker.iface,
768 tracker.pfx96String, tracker.v6Str, tracker.v4Str, tracker.v4ifIndex,
769 tracker.v4iface, tracker.netId);
770 }
771}
772
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800773void ClatdController::dump(DumpWriter& dw) {
774 std::lock_guard guard(mutex);
775
776 ScopedIndent clatdIndent(dw);
777 dw.println("ClatdController");
778
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800779 dumpTrackers(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800780 dumpIngress(dw);
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800781 dumpEgress(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800782}
783
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900784auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree;
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900785auto ClatdController::iptablesRestoreFunction = execIptablesRestore;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900786
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900787} // namespace net
788} // namespace android