blob: 90afa3a45e534055c076a9f58f0446e689a00f66 [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 Żenczykowskif4b44fe2019-04-08 16:18:50 -070022#include <linux/if_tun.h>
23#include <linux/ioctl.h>
Maciej Żenczykowskic8c38aa2019-03-29 01:24:51 -070024#include <net/if.h>
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090025#include <netinet/in.h>
Luke Huang94c43a12019-02-14 19:51:38 +080026#include <spawn.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050027#include <sys/types.h>
28#include <sys/wait.h>
Luke Huang94c43a12019-02-14 19:51:38 +080029#include <unistd.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050030
31#define LOG_TAG "ClatdController"
Logan Chien3f461482018-04-23 14:31:32 +080032#include <log/log.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050033
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -070034#include "ClatdController.h"
Maciej Żenczykowskia1699952019-05-11 17:07:44 -070035#include "InterfaceController.h"
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -070036
37#include "android-base/properties.h"
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -070038#include "android-base/scopeguard.h"
Lorenzo Colitti91fd5802019-06-28 19:22:01 +090039#include "android-base/stringprintf.h"
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090040#include "android-base/unique_fd.h"
Maciej Żenczykowski55262712019-03-29 23:44:56 -070041#include "bpf/BpfMap.h"
42#include "netdbpf/bpf_shared.h"
43#include "netdutils/DumpWriter.h"
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090044
45extern "C" {
46#include "netutils/checksum.h"
47}
48
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090049#include "Fwmark.h"
Paul Jensen84c1d032014-05-30 13:29:41 -040050#include "NetdConstants.h"
51#include "NetworkController.h"
Maciej Żenczykowskieec72082020-02-04 23:29:41 -080052#include "OffloadUtils.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090053#include "netid_client.h"
Daniel Drown0da73fc2012-06-20 16:51:39 -050054
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090055static const char* kClatdPath = "/system/bin/clatd";
56
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090057// For historical reasons, start with 192.0.0.4, and after that, use all subsequent addresses in
58// 192.0.0.0/29 (RFC 7335).
59static const char* kV4AddrString = "192.0.0.4";
60static const in_addr kV4Addr = {inet_addr(kV4AddrString)};
61static const int kV4AddrLen = 29;
62
Steven Morelanda3074542020-01-13 14:13:44 -080063using android::base::Result;
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 Żenczykowski56280272019-03-30 03:32:51 -070073void ClatdController::init(void) {
74 std::lock_guard guard(mutex);
75
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -070076 // TODO: should refactor into separate function for testability
Maciej Żenczykowski69cd8222020-02-11 15:36:40 -080077 if (!bpf::isBpfSupported()) {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -070078 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
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800102 int rv = getClatEgressMapFd();
Maciej Żenczykowski0bd8da72019-12-16 15:16:28 -0800103 if (rv < 0) {
104 ALOGE("getClatEgressMapFd() failure: %s", strerror(-rv));
105 mClatEbpfMode = ClatEbpfDisabled;
Maciej Żenczykowski0bd8da72019-12-16 15:16:28 -0800106 return;
107 }
108 mClatEgressMap.reset(rv);
109
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700110 rv = getClatIngressMapFd();
111 if (rv < 0) {
112 ALOGE("getClatIngressMapFd() failure: %s", strerror(-rv));
113 mClatEbpfMode = ClatEbpfDisabled;
Maciej Żenczykowski0bd8da72019-12-16 15:16:28 -0800114 mClatEgressMap.reset(-1);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700115 return;
116 }
117 mClatIngressMap.reset(rv);
118
Maciej Żenczykowskie64baf42020-02-18 18:25:47 -0800119 mClatEgressMap.clear();
120 mClatIngressMap.clear();
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700121}
122
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900123bool ClatdController::isIpv4AddressFree(in_addr_t addr) {
124 int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
125 if (s == -1) {
126 return 0;
127 }
128
129 // Attempt to connect to the address. If the connection succeeds and getsockname returns the
130 // same then the address is already assigned to the system and we can't use it.
Nick Desaulniers6b357502019-10-11 09:26:44 -0700131 struct sockaddr_in sin = {
132 .sin_family = AF_INET,
Maciej Żenczykowski268190e2019-10-31 23:47:53 -0700133 .sin_port = htons(53),
Nick Desaulniers6b357502019-10-11 09:26:44 -0700134 .sin_addr = {addr},
135 };
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900136 socklen_t len = sizeof(sin);
137 bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 &&
138 getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) &&
139 sin.sin_addr.s_addr == addr;
140
141 close(s);
142 return !inuse;
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900143}
Daniel Drown0da73fc2012-06-20 16:51:39 -0500144
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900145// Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order.
146// ip - the IP address from the configuration file
147// prefixlen - the length of the prefix from which addresses may be selected.
148// returns: the IPv4 address, or INADDR_NONE if no addresses were available
149in_addr_t ClatdController::selectIpv4Address(const in_addr ip, int16_t prefixlen) {
150 // Don't accept prefixes that are too large because we scan addresses one by one.
151 if (prefixlen < 16 || prefixlen > 32) {
152 return INADDR_NONE;
153 }
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900154
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900155 // All these are in host byte order.
156 in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen);
157 in_addr_t ipv4 = ntohl(ip.s_addr);
158 in_addr_t first_ipv4 = ipv4;
159 in_addr_t prefix = ipv4 & mask;
160
161 // Pick the first IPv4 address in the pool, wrapping around if necessary.
162 // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0.
163 do {
164 if (isIpv4AddressFreeFunc(htonl(ipv4))) {
165 return htonl(ipv4);
166 }
167 ipv4 = prefix | ((ipv4 + 1) & ~mask);
168 } while (ipv4 != first_ipv4);
169
170 return INADDR_NONE;
171}
172
173// Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix.
174void ClatdController::makeChecksumNeutral(in6_addr* v6, const in_addr v4,
175 const in6_addr& nat64Prefix) {
176 // Fill last 8 bytes of IPv6 address with random bits.
177 arc4random_buf(&v6->s6_addr[8], 8);
178
179 // Make the IID checksum-neutral. That is, make it so that:
180 // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6)
181 // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4):
182 // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix)
183 // Do this by adjusting the two bytes in the middle of the IID.
184
185 uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12];
186
187 uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4));
188 uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) +
189 ip_checksum_add(0, v6, sizeof(*v6));
190
191 uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2);
192 v6->s6_addr[11] = delta >> 8;
193 v6->s6_addr[12] = delta & 0xff;
194}
195
196// Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix.
197int ClatdController::generateIpv6Address(const char* iface, const in_addr v4,
198 const in6_addr& nat64Prefix, in6_addr* v6) {
199 unique_fd s(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0));
200 if (s == -1) return -errno;
201
202 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) {
Luke Huang6d301232018-08-01 14:05:18 +0800203 return -errno;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500204 }
205
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900206 sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix};
207 if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) {
208 return -errno;
209 }
210
211 socklen_t len = sizeof(sin6);
212 if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) {
213 return -errno;
214 }
215
216 *v6 = sin6.sin6_addr;
217
218 if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) ||
219 IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) {
220 return -ENETUNREACH;
221 }
222
223 makeChecksumNeutral(v6, v4, nat64Prefix);
224
225 return 0;
226}
227
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700228void ClatdController::maybeStartBpf(const ClatdTracker& tracker) {
229 if (mClatEbpfMode == ClatEbpfDisabled) return;
230
Hungming Chenec203382020-02-13 19:15:33 +0800231 auto isEthernet = android::net::isEthernet(tracker.iface);
232 if (!isEthernet.ok()) {
233 ALOGE("isEthernet(%s[%d]) failure: %s", tracker.iface, tracker.ifIndex,
234 isEthernet.error().message().c_str());
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700235 return;
236 }
237
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800238 // This program will be attached to the v4-* interface which is a TUN and thus always rawip.
Hungming Chenec203382020-02-13 19:15:33 +0800239 int rv = getClatEgressProgFd(RAWIP);
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800240 if (rv < 0) {
Maciej Żenczykowskiad730892020-02-13 18:21:32 -0800241 ALOGE("getClatEgressProgFd(RAWIP) failure: %s", strerror(-rv));
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800242 return;
243 }
244 unique_fd txRawIpProgFd(rv);
245
Hungming Chenec203382020-02-13 19:15:33 +0800246 rv = getClatIngressProgFd(isEthernet.value());
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700247 if (rv < 0) {
Hungming Chenec203382020-02-13 19:15:33 +0800248 ALOGE("getClatIngressProgFd(%d) failure: %s", isEthernet.value(), strerror(-rv));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700249 return;
250 }
Maciej Żenczykowski75f4dc32019-12-16 18:38:54 -0800251 unique_fd rxProgFd(rv);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700252
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800253 ClatEgressKey txKey = {
254 .iif = tracker.v4ifIndex,
255 .local4 = tracker.v4,
256 };
257 ClatEgressValue txValue = {
258 .oif = tracker.ifIndex,
259 .local6 = tracker.v6,
260 .pfx96 = tracker.pfx96,
Hungming Chenec203382020-02-13 19:15:33 +0800261 .oifIsEthernet = isEthernet.value(),
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800262 };
263
264 auto ret = mClatEgressMap.writeValue(txKey, txValue, BPF_ANY);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900265 if (!ret.ok()) {
Maciej Żenczykowski4460b8c2020-01-20 03:47:41 -0800266 ALOGE("mClatEgressMap.writeValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800267 return;
268 }
269
270 ClatIngressKey rxKey = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700271 .iif = tracker.ifIndex,
272 .pfx96 = tracker.pfx96,
273 .local6 = tracker.v6,
274 };
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800275 ClatIngressValue rxValue = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700276 // TODO: move all the clat code to eBPF and remove the tun interface entirely.
Maciej Żenczykowski39b0b902019-05-08 00:36:30 -0700277 .oif = tracker.v4ifIndex,
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700278 .local4 = tracker.v4,
279 };
280
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800281 ret = mClatIngressMap.writeValue(rxKey, rxValue, BPF_ANY);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900282 if (!ret.ok()) {
Maciej Żenczykowski4460b8c2020-01-20 03:47:41 -0800283 ALOGE("mClatIngressMap.writeValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800284 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900285 if (!ret.ok())
286 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700287 return;
288 }
289
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800290 // We do tc setup *after* populating the maps, so scanning through them
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700291 // can always be used to tell us what needs cleanup.
292
Hungming Chen7f725432020-02-07 17:47:23 +0800293 // Usually the clsact will be added in RouteController::addInterfaceToPhysicalNetwork.
294 // But clat is started before the v4- interface is added to the network. The clat startup have
295 // to add clsact of v4- tun interface first for adding bpf filter in maybeStartBpf.
296 // TODO: move "qdisc add clsact" of v4- tun interface out from ClatdController.
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800297 rv = tcQdiscAddDevClsact(tracker.v4ifIndex);
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800298 if (rv) {
299 ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", tracker.v4ifIndex, tracker.v4iface,
300 strerror(-rv));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800301 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900302 if (!ret.ok())
303 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800304 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900305 if (!ret.ok())
306 ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800307 return;
308 }
309
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800310 rv = tcFilterAddDevEgressClatIpv4(tracker.v4ifIndex, txRawIpProgFd, RAWIP);
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800311 if (rv) {
312 if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) {
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800313 ALOGI("tcFilterAddDevEgressClatIpv4(%d[%s], RAWIP): %s", tracker.v4ifIndex,
314 tracker.v4iface, strerror(-rv));
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800315 } else {
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800316 ALOGE("tcFilterAddDevEgressClatIpv4(%d[%s], RAWIP) failure: %s", tracker.v4ifIndex,
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800317 tracker.v4iface, strerror(-rv));
318 }
Hungming Chen7f725432020-02-07 17:47:23 +0800319
320 // The v4- interface clsact is not deleted for unwinding error because once it is created
321 // with interface addition, the lifetime is till interface deletion. Moreover, the clsact
322 // has no clat filter now. It should not break anything.
323
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800324 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900325 if (!ret.ok())
326 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800327 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900328 if (!ret.ok())
329 ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800330 return;
331 }
332
Hungming Chenec203382020-02-13 19:15:33 +0800333 rv = tcFilterAddDevIngressClatIpv6(tracker.ifIndex, rxProgFd, isEthernet.value());
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700334 if (rv) {
335 if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) {
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800336 ALOGI("tcFilterAddDevIngressClatIpv6(%d[%s], %d): %s", tracker.ifIndex, tracker.iface,
Hungming Chenec203382020-02-13 19:15:33 +0800337 isEthernet.value(), strerror(-rv));
Maciej Żenczykowskib140a3a2019-12-15 11:53:46 -0800338 } else {
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800339 ALOGE("tcFilterAddDevIngressClatIpv6(%d[%s], %d) failure: %s", tracker.ifIndex,
Hungming Chenec203382020-02-13 19:15:33 +0800340 tracker.iface, isEthernet.value(), strerror(-rv));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700341 }
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800342 rv = tcFilterDelDevEgressClatIpv4(tracker.v4ifIndex);
Hungming Chene55f45d2020-02-07 15:10:52 +0800343 if (rv) {
344 ALOGE("tcFilterDelDevEgressClatIpv4(%d[%s]) failure: %s", tracker.v4ifIndex,
345 tracker.v4iface, strerror(-rv));
346 }
Hungming Chen7f725432020-02-07 17:47:23 +0800347
348 // The v4- interface clsact is not deleted. See the reason in the error unwinding code of
349 // the egress filter attaching of v4- tun interface.
350
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800351 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900352 if (!ret.ok())
353 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800354 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900355 if (!ret.ok())
356 ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700357 return;
358 }
359
360 // success
361}
362
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800363void ClatdController::setIptablesDropRule(bool add, const char* iface, const char* pfx96Str,
364 const char* v6Str) {
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900365 std::string cmd = StringPrintf(
366 "*raw\n"
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800367 "%s %s -i %s -s %s/96 -d %s -j DROP\n"
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900368 "COMMIT\n",
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800369 (add ? "-A" : "-D"), LOCAL_RAW_PREROUTING, iface, pfx96Str, v6Str);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900370
371 iptablesRestoreFunction(V6, cmd);
372}
373
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700374void ClatdController::maybeStopBpf(const ClatdTracker& tracker) {
375 if (mClatEbpfMode == ClatEbpfDisabled) return;
376
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800377 int rv = tcFilterDelDevIngressClatIpv6(tracker.ifIndex);
Hungming Chene55f45d2020-02-07 15:10:52 +0800378 if (rv < 0) {
379 ALOGE("tcFilterDelDevIngressClatIpv6(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
380 strerror(-rv));
381 }
382
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800383 rv = tcFilterDelDevEgressClatIpv4(tracker.v4ifIndex);
Hungming Chene55f45d2020-02-07 15:10:52 +0800384 if (rv < 0) {
385 ALOGE("tcFilterDelDevEgressClatIpv4(%d[%s]) failure: %s", tracker.v4ifIndex,
386 tracker.v4iface, strerror(-rv));
387 }
388
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800389 // We cleanup the maps last, so scanning through them can be used to
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700390 // determine what still needs cleanup.
391
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800392 ClatEgressKey txKey = {
393 .iif = tracker.v4ifIndex,
394 .local4 = tracker.v4,
395 };
396
397 auto ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900398 if (!ret.ok()) ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800399
400 ClatIngressKey rxKey = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700401 .iif = tracker.ifIndex,
402 .pfx96 = tracker.pfx96,
403 .local6 = tracker.v6,
404 };
405
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800406 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900407 if (!ret.ok()) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700408}
409
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900410// Finds the tracker of the clatd running on interface |interface|, or nullptr if clatd has not been
411// started on |interface|.
412ClatdController::ClatdTracker* ClatdController::getClatdTracker(const std::string& interface) {
413 auto it = mClatdTrackers.find(interface);
414 return (it == mClatdTrackers.end() ? nullptr : &it->second);
415}
416
417// Initializes a ClatdTracker for the specified interface.
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700418int ClatdController::ClatdTracker::init(unsigned networkId, const std::string& interface,
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700419 const std::string& v4interface,
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900420 const std::string& nat64Prefix) {
Treehugger Robota9cac132020-06-01 08:48:07 +0000421 fwmark.netId = networkId;
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900422 fwmark.explicitlySelected = true;
423 fwmark.protectedFromVpn = true;
424 fwmark.permission = PERMISSION_SYSTEM;
425
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900426 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900427 strlcpy(iface, interface.c_str(), sizeof(iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700428 ifIndex = if_nametoindex(iface);
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700429 strlcpy(v4iface, v4interface.c_str(), sizeof(v4iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700430 v4ifIndex = if_nametoindex(v4iface);
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900431
Treehugger Robota9cac132020-06-01 08:48:07 +0000432 // Pass in everything that clatd needs: interface, a fwmark for outgoing packets, the NAT64
433 // prefix, and the IPv4 and IPv6 addresses.
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900434 // Validate the prefix and strip off the prefix length.
435 uint8_t family;
436 uint8_t prefixLen;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700437 int res = parsePrefix(nat64Prefix.c_str(), &family, &pfx96, sizeof(pfx96), &prefixLen);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900438 // clatd only supports /96 prefixes.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700439 if (res != sizeof(pfx96)) return res;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900440 if (family != AF_INET6) return -EAFNOSUPPORT;
441 if (prefixLen != 96) return -EINVAL;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700442 if (!inet_ntop(AF_INET6, &pfx96, pfx96String, sizeof(pfx96String))) return -errno;
Luke Huang6d301232018-08-01 14:05:18 +0800443
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900444 // Pick an IPv4 address.
445 // TODO: this picks the address based on other addresses that are assigned to interfaces, but
446 // the address is only actually assigned to an interface once clatd starts up. So we could end
447 // up with two clatd instances with the same IPv4 address.
448 // Stop doing this and instead pick a free one from the kV4Addr pool.
Maciej Żenczykowski55cacfb2019-03-30 02:01:35 -0700449 v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900450 if (v4.s_addr == INADDR_NONE) {
451 ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen);
452 return -EADDRNOTAVAIL;
453 }
454 if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno;
455
456 // Generate a checksum-neutral IID.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700457 if (generateIpv6Address(iface, v4, pfx96, &v6)) {
458 ALOGE("Unable to find global source address on %s for %s", iface, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900459 return -EADDRNOTAVAIL;
460 }
461 if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno;
462
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700463 ALOGD("starting clatd on %s v4=%s v6=%s pfx96=%s", iface, v4Str, v6Str, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900464 return 0;
465}
466
467int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix,
468 std::string* v6Str) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700469 std::lock_guard guard(mutex);
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700470
471 // 1. fail if pre-existing tracker already exists
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900472 ClatdTracker* existing = getClatdTracker(interface);
473 if (existing != nullptr) {
474 ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700475 return -EBUSY;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900476 }
477
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700478 // 2. get network id associated with this external interface
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700479 unsigned networkId = mNetCtrl->getNetworkForInterface(interface.c_str());
480 if (networkId == NETID_UNSET) {
481 ALOGE("Interface %s not assigned to any netId", interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700482 return -ENODEV;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900483 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700484
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700485 // 3. open the tun device in non blocking mode as required by clatd
Maciej Żenczykowski94db6582020-01-27 21:47:06 -0800486 int res = open("/dev/net/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC);
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700487 if (res == -1) {
488 res = errno;
489 ALOGE("open of tun device failed (%s)", strerror(res));
490 return -res;
491 }
492 unique_fd tmpTunFd(res);
493
494 // 4. create the v4-... tun interface
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700495 std::string v4interface("v4-");
496 v4interface += interface;
497
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700498 struct ifreq ifr = {
499 .ifr_flags = IFF_TUN,
500 };
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700501 strlcpy(ifr.ifr_name, v4interface.c_str(), sizeof(ifr.ifr_name));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700502
503 res = ioctl(tmpTunFd, TUNSETIFF, &ifr, sizeof(ifr));
504 if (res == -1) {
505 res = errno;
506 ALOGE("ioctl(TUNSETIFF) failed (%s)", strerror(res));
507 return -res;
508 }
509
Maciej Żenczykowskia1699952019-05-11 17:07:44 -0700510 // disable IPv6 on it - failing to do so is not a critical error
511 res = InterfaceController::setEnableIPv6(v4interface.c_str(), 0);
512 if (res) ALOGE("setEnableIPv6 %s failed (%s)", v4interface.c_str(), strerror(res));
513
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700514 // 5. initialize tracker object
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700515 ClatdTracker tracker;
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700516 int ret = tracker.init(networkId, interface, v4interface, nat64Prefix);
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700517 if (ret) return ret;
518
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700519 // 6. create a throwaway socket to reserve a file descriptor number
520 res = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
521 if (res == -1) {
522 res = errno;
523 ALOGE("socket(ipv6/udp) failed (%s)", strerror(res));
524 return -res;
525 }
526 unique_fd passedTunFd(res);
527
528 // 7. this is the FD we'll pass to clatd on the cli, so need it as a string
529 char passedTunFdStr[INT32_STRLEN];
530 snprintf(passedTunFdStr, sizeof(passedTunFdStr), "%d", passedTunFd.get());
531
532 // 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 +0900533 std::string progname("clatd-");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900534 progname += tracker.iface;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500535
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900536 // clang-format off
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700537 const char* args[] = {progname.c_str(),
538 "-i", tracker.iface,
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700539 "-m", tracker.fwmarkString,
540 "-p", tracker.pfx96String,
541 "-4", tracker.v4Str,
542 "-6", tracker.v6Str,
543 "-t", passedTunFdStr,
544 nullptr};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900545 // clang-format on
546
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700547 // 9. register vfork requirement
548 posix_spawnattr_t attr;
549 res = posix_spawnattr_init(&attr);
550 if (res) {
551 ALOGE("posix_spawnattr_init failed (%s)", strerror(res));
552 return -res;
553 }
554 const android::base::ScopeGuard attrGuard = [&] { posix_spawnattr_destroy(&attr); };
555 res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_USEVFORK);
556 if (res) {
557 ALOGE("posix_spawnattr_setflags failed (%s)", strerror(res));
558 return -res;
559 }
560
561 // 10. register dup2() action: this is what 'clears' the CLOEXEC flag
562 // on the tun fd that we want the child clatd process to inherit
563 // (this will happen after the vfork, and before the execve)
564 posix_spawn_file_actions_t fa;
565 res = posix_spawn_file_actions_init(&fa);
566 if (res) {
567 ALOGE("posix_spawn_file_actions_init failed (%s)", strerror(res));
568 return -res;
569 }
570 const android::base::ScopeGuard faGuard = [&] { posix_spawn_file_actions_destroy(&fa); };
571 res = posix_spawn_file_actions_adddup2(&fa, tmpTunFd, passedTunFd);
572 if (res) {
573 ALOGE("posix_spawn_file_actions_adddup2 failed (%s)", strerror(res));
574 return -res;
575 }
576
Maciej Żenczykowski083688f2019-12-23 14:43:09 -0800577 // 11. add the drop rule for iptables.
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800578 setIptablesDropRule(true, tracker.iface, tracker.pfx96String, tracker.v6Str);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900579
580 // 12. actually perform vfork/dup2/execve
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700581 res = posix_spawn(&tracker.pid, kClatdPath, &fa, &attr, (char* const*)args, nullptr);
Luke Huang40962442019-02-26 11:46:10 +0800582 if (res) {
583 ALOGE("posix_spawn failed (%s)", strerror(res));
Luke Huang6d301232018-08-01 14:05:18 +0800584 return -res;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500585 }
586
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900587 // 13. configure eBPF offload - if possible
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700588 maybeStartBpf(tracker);
589
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900590 mClatdTrackers[interface] = tracker;
591 ALOGD("clatd started on %s", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500592
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900593 *v6Str = tracker.v6Str;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500594 return 0;
595}
596
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900597int ClatdController::stopClatd(const std::string& interface) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700598 std::lock_guard guard(mutex);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900599 ClatdTracker* tracker = getClatdTracker(interface);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900600
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900601 if (tracker == nullptr) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500602 ALOGE("clatd already stopped");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900603 return -ENODEV;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500604 }
605
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900606 ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500607
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700608 maybeStopBpf(*tracker);
609
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900610 kill(tracker->pid, SIGTERM);
611 waitpid(tracker->pid, nullptr, 0);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900612
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800613 setIptablesDropRule(false, tracker->iface, tracker->pfx96String, tracker->v6Str);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900614 mClatdTrackers.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500615
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900616 ALOGD("clatd on %s stopped", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500617
618 return 0;
619}
620
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800621void ClatdController::dumpEgress(DumpWriter& dw) {
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800622 if (!mClatEgressMap.isValid()) return; // if unsupported just don't dump anything
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800623
624 ScopedIndent bpfIndent(dw);
625 dw.println("BPF egress map: iif(iface) v4Addr -> v6Addr nat64Prefix oif(iface)");
626
627 ScopedIndent bpfDetailIndent(dw);
628 const auto printClatMap = [&dw](const ClatEgressKey& key, const ClatEgressValue& value,
629 const BpfMap<ClatEgressKey, ClatEgressValue>&) {
630 char iifStr[IFNAMSIZ] = "?";
631 char local4Str[INET_ADDRSTRLEN] = "?";
632 char local6Str[INET6_ADDRSTRLEN] = "?";
633 char pfx96Str[INET6_ADDRSTRLEN] = "?";
634 char oifStr[IFNAMSIZ] = "?";
635
636 if_indextoname(key.iif, iifStr);
637 inet_ntop(AF_INET, &key.local4, local4Str, sizeof(local4Str));
638 inet_ntop(AF_INET6, &value.local6, local6Str, sizeof(local6Str));
639 inet_ntop(AF_INET6, &value.pfx96, pfx96Str, sizeof(pfx96Str));
640 if_indextoname(value.oif, oifStr);
641
Maciej Żenczykowski97ec1d82019-12-17 12:11:21 -0800642 dw.println("%u(%s) %s -> %s %s/96 %u(%s) %s", key.iif, iifStr, local4Str, local6Str,
643 pfx96Str, value.oif, oifStr, value.oifIsEthernet ? "ether" : "rawip");
Steven Morelanda3074542020-01-13 14:13:44 -0800644 return Result<void>();
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800645 };
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800646 auto res = mClatEgressMap.iterateWithValue(printClatMap);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900647 if (!res.ok()) {
Steven Morelanda3074542020-01-13 14:13:44 -0800648 dw.println("Error printing BPF map: %s", res.error().message().c_str());
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800649 }
650}
651
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800652void ClatdController::dumpIngress(DumpWriter& dw) {
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800653 if (!mClatIngressMap.isValid()) return; // if unsupported just don't dump anything
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700654
655 ScopedIndent bpfIndent(dw);
656 dw.println("BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)");
657
658 ScopedIndent bpfDetailIndent(dw);
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700659 const auto printClatMap = [&dw](const ClatIngressKey& key, const ClatIngressValue& value,
660 const BpfMap<ClatIngressKey, ClatIngressValue>&) {
661 char iifStr[IFNAMSIZ] = "?";
662 char pfx96Str[INET6_ADDRSTRLEN] = "?";
663 char local6Str[INET6_ADDRSTRLEN] = "?";
664 char local4Str[INET_ADDRSTRLEN] = "?";
665 char oifStr[IFNAMSIZ] = "?";
666
667 if_indextoname(key.iif, iifStr);
668 inet_ntop(AF_INET6, &key.pfx96, pfx96Str, sizeof(pfx96Str));
669 inet_ntop(AF_INET6, &key.local6, local6Str, sizeof(local6Str));
670 inet_ntop(AF_INET, &value.local4, local4Str, sizeof(local4Str));
671 if_indextoname(value.oif, oifStr);
672
673 dw.println("%u(%s) %s/96 %s -> %s %u(%s)", key.iif, iifStr, pfx96Str, local6Str, local4Str,
674 value.oif, oifStr);
Steven Morelanda3074542020-01-13 14:13:44 -0800675 return Result<void>();
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700676 };
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800677 auto res = mClatIngressMap.iterateWithValue(printClatMap);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900678 if (!res.ok()) {
Steven Morelanda3074542020-01-13 14:13:44 -0800679 dw.println("Error printing BPF map: %s", res.error().message().c_str());
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700680 }
681}
682
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800683void ClatdController::dumpTrackers(DumpWriter& dw) {
684 ScopedIndent trackerIndent(dw);
Treehugger Robota9cac132020-06-01 08:48:07 +0000685 dw.println("Trackers: iif[iface] nat64Prefix v6Addr -> v4Addr v4iif[v4iface] [fwmark]");
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800686
687 ScopedIndent trackerDetailIndent(dw);
688 for (const auto& pair : mClatdTrackers) {
689 const ClatdTracker& tracker = pair.second;
Treehugger Robota9cac132020-06-01 08:48:07 +0000690 dw.println("%u[%s] %s/96 %s -> %s %u[%s] [%s]", tracker.ifIndex, tracker.iface,
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800691 tracker.pfx96String, tracker.v6Str, tracker.v4Str, tracker.v4ifIndex,
Treehugger Robota9cac132020-06-01 08:48:07 +0000692 tracker.v4iface, tracker.fwmarkString);
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800693 }
694}
695
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800696void ClatdController::dump(DumpWriter& dw) {
697 std::lock_guard guard(mutex);
698
699 ScopedIndent clatdIndent(dw);
700 dw.println("ClatdController");
701
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800702 dumpTrackers(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800703 dumpIngress(dw);
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800704 dumpEgress(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800705}
706
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900707auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree;
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900708auto ClatdController::iptablesRestoreFunction = execIptablesRestore;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900709
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900710} // namespace net
711} // namespace android