blob: 763b1c3f072ad8b836dce76fc68e2282566c6f38 [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) {
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700421 netId = networkId;
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900422
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900423 fwmark.netId = netId;
424 fwmark.explicitlySelected = true;
425 fwmark.protectedFromVpn = true;
426 fwmark.permission = PERMISSION_SYSTEM;
427
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900428 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900429 snprintf(netIdString, sizeof(netIdString), "%u", netId);
430 strlcpy(iface, interface.c_str(), sizeof(iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700431 ifIndex = if_nametoindex(iface);
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700432 strlcpy(v4iface, v4interface.c_str(), sizeof(v4iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700433 v4ifIndex = if_nametoindex(v4iface);
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900434
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900435 // Pass in everything that clatd needs: interface, a netid to use for DNS lookups, a fwmark for
436 // outgoing packets, the NAT64 prefix, and the IPv4 and IPv6 addresses.
437 // Validate the prefix and strip off the prefix length.
438 uint8_t family;
439 uint8_t prefixLen;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700440 int res = parsePrefix(nat64Prefix.c_str(), &family, &pfx96, sizeof(pfx96), &prefixLen);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900441 // clatd only supports /96 prefixes.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700442 if (res != sizeof(pfx96)) return res;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900443 if (family != AF_INET6) return -EAFNOSUPPORT;
444 if (prefixLen != 96) return -EINVAL;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700445 if (!inet_ntop(AF_INET6, &pfx96, pfx96String, sizeof(pfx96String))) return -errno;
Luke Huang6d301232018-08-01 14:05:18 +0800446
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900447 // Pick an IPv4 address.
448 // TODO: this picks the address based on other addresses that are assigned to interfaces, but
449 // the address is only actually assigned to an interface once clatd starts up. So we could end
450 // up with two clatd instances with the same IPv4 address.
451 // Stop doing this and instead pick a free one from the kV4Addr pool.
Maciej Żenczykowski55cacfb2019-03-30 02:01:35 -0700452 v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900453 if (v4.s_addr == INADDR_NONE) {
454 ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen);
455 return -EADDRNOTAVAIL;
456 }
457 if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno;
458
459 // Generate a checksum-neutral IID.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700460 if (generateIpv6Address(iface, v4, pfx96, &v6)) {
461 ALOGE("Unable to find global source address on %s for %s", iface, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900462 return -EADDRNOTAVAIL;
463 }
464 if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno;
465
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700466 ALOGD("starting clatd on %s v4=%s v6=%s pfx96=%s", iface, v4Str, v6Str, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900467 return 0;
468}
469
470int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix,
471 std::string* v6Str) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700472 std::lock_guard guard(mutex);
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700473
474 // 1. fail if pre-existing tracker already exists
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900475 ClatdTracker* existing = getClatdTracker(interface);
476 if (existing != nullptr) {
477 ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700478 return -EBUSY;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900479 }
480
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700481 // 2. get network id associated with this external interface
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700482 unsigned networkId = mNetCtrl->getNetworkForInterface(interface.c_str());
483 if (networkId == NETID_UNSET) {
484 ALOGE("Interface %s not assigned to any netId", interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700485 return -ENODEV;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900486 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700487
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700488 // 3. open the tun device in non blocking mode as required by clatd
Maciej Żenczykowski94db6582020-01-27 21:47:06 -0800489 int res = open("/dev/net/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC);
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700490 if (res == -1) {
491 res = errno;
492 ALOGE("open of tun device failed (%s)", strerror(res));
493 return -res;
494 }
495 unique_fd tmpTunFd(res);
496
497 // 4. create the v4-... tun interface
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700498 std::string v4interface("v4-");
499 v4interface += interface;
500
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700501 struct ifreq ifr = {
502 .ifr_flags = IFF_TUN,
503 };
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700504 strlcpy(ifr.ifr_name, v4interface.c_str(), sizeof(ifr.ifr_name));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700505
506 res = ioctl(tmpTunFd, TUNSETIFF, &ifr, sizeof(ifr));
507 if (res == -1) {
508 res = errno;
509 ALOGE("ioctl(TUNSETIFF) failed (%s)", strerror(res));
510 return -res;
511 }
512
Maciej Żenczykowskia1699952019-05-11 17:07:44 -0700513 // disable IPv6 on it - failing to do so is not a critical error
514 res = InterfaceController::setEnableIPv6(v4interface.c_str(), 0);
515 if (res) ALOGE("setEnableIPv6 %s failed (%s)", v4interface.c_str(), strerror(res));
516
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700517 // 5. initialize tracker object
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700518 ClatdTracker tracker;
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700519 int ret = tracker.init(networkId, interface, v4interface, nat64Prefix);
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700520 if (ret) return ret;
521
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700522 // 6. create a throwaway socket to reserve a file descriptor number
523 res = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
524 if (res == -1) {
525 res = errno;
526 ALOGE("socket(ipv6/udp) failed (%s)", strerror(res));
527 return -res;
528 }
529 unique_fd passedTunFd(res);
530
531 // 7. this is the FD we'll pass to clatd on the cli, so need it as a string
532 char passedTunFdStr[INT32_STRLEN];
533 snprintf(passedTunFdStr, sizeof(passedTunFdStr), "%d", passedTunFd.get());
534
535 // 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 +0900536 std::string progname("clatd-");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900537 progname += tracker.iface;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500538
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900539 // clang-format off
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700540 const char* args[] = {progname.c_str(),
541 "-i", tracker.iface,
542 "-n", tracker.netIdString,
543 "-m", tracker.fwmarkString,
544 "-p", tracker.pfx96String,
545 "-4", tracker.v4Str,
546 "-6", tracker.v6Str,
547 "-t", passedTunFdStr,
548 nullptr};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900549 // clang-format on
550
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700551 // 9. register vfork requirement
552 posix_spawnattr_t attr;
553 res = posix_spawnattr_init(&attr);
554 if (res) {
555 ALOGE("posix_spawnattr_init failed (%s)", strerror(res));
556 return -res;
557 }
558 const android::base::ScopeGuard attrGuard = [&] { posix_spawnattr_destroy(&attr); };
559 res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_USEVFORK);
560 if (res) {
561 ALOGE("posix_spawnattr_setflags failed (%s)", strerror(res));
562 return -res;
563 }
564
565 // 10. register dup2() action: this is what 'clears' the CLOEXEC flag
566 // on the tun fd that we want the child clatd process to inherit
567 // (this will happen after the vfork, and before the execve)
568 posix_spawn_file_actions_t fa;
569 res = posix_spawn_file_actions_init(&fa);
570 if (res) {
571 ALOGE("posix_spawn_file_actions_init failed (%s)", strerror(res));
572 return -res;
573 }
574 const android::base::ScopeGuard faGuard = [&] { posix_spawn_file_actions_destroy(&fa); };
575 res = posix_spawn_file_actions_adddup2(&fa, tmpTunFd, passedTunFd);
576 if (res) {
577 ALOGE("posix_spawn_file_actions_adddup2 failed (%s)", strerror(res));
578 return -res;
579 }
580
Maciej Żenczykowski083688f2019-12-23 14:43:09 -0800581 // 11. add the drop rule for iptables.
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800582 setIptablesDropRule(true, tracker.iface, tracker.pfx96String, tracker.v6Str);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900583
584 // 12. actually perform vfork/dup2/execve
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700585 res = posix_spawn(&tracker.pid, kClatdPath, &fa, &attr, (char* const*)args, nullptr);
Luke Huang40962442019-02-26 11:46:10 +0800586 if (res) {
587 ALOGE("posix_spawn failed (%s)", strerror(res));
Luke Huang6d301232018-08-01 14:05:18 +0800588 return -res;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500589 }
590
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900591 // 13. configure eBPF offload - if possible
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700592 maybeStartBpf(tracker);
593
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900594 mClatdTrackers[interface] = tracker;
595 ALOGD("clatd started on %s", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500596
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900597 *v6Str = tracker.v6Str;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500598 return 0;
599}
600
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900601int ClatdController::stopClatd(const std::string& interface) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700602 std::lock_guard guard(mutex);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900603 ClatdTracker* tracker = getClatdTracker(interface);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900604
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900605 if (tracker == nullptr) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500606 ALOGE("clatd already stopped");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900607 return -ENODEV;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500608 }
609
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900610 ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500611
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700612 maybeStopBpf(*tracker);
613
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900614 kill(tracker->pid, SIGTERM);
615 waitpid(tracker->pid, nullptr, 0);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900616
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800617 setIptablesDropRule(false, tracker->iface, tracker->pfx96String, tracker->v6Str);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900618 mClatdTrackers.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500619
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900620 ALOGD("clatd on %s stopped", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500621
622 return 0;
623}
624
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800625void ClatdController::dumpEgress(DumpWriter& dw) {
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800626 if (!mClatEgressMap.isValid()) return; // if unsupported just don't dump anything
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800627
628 ScopedIndent bpfIndent(dw);
629 dw.println("BPF egress map: iif(iface) v4Addr -> v6Addr nat64Prefix oif(iface)");
630
631 ScopedIndent bpfDetailIndent(dw);
632 const auto printClatMap = [&dw](const ClatEgressKey& key, const ClatEgressValue& value,
633 const BpfMap<ClatEgressKey, ClatEgressValue>&) {
634 char iifStr[IFNAMSIZ] = "?";
635 char local4Str[INET_ADDRSTRLEN] = "?";
636 char local6Str[INET6_ADDRSTRLEN] = "?";
637 char pfx96Str[INET6_ADDRSTRLEN] = "?";
638 char oifStr[IFNAMSIZ] = "?";
639
640 if_indextoname(key.iif, iifStr);
641 inet_ntop(AF_INET, &key.local4, local4Str, sizeof(local4Str));
642 inet_ntop(AF_INET6, &value.local6, local6Str, sizeof(local6Str));
643 inet_ntop(AF_INET6, &value.pfx96, pfx96Str, sizeof(pfx96Str));
644 if_indextoname(value.oif, oifStr);
645
Maciej Żenczykowski97ec1d82019-12-17 12:11:21 -0800646 dw.println("%u(%s) %s -> %s %s/96 %u(%s) %s", key.iif, iifStr, local4Str, local6Str,
647 pfx96Str, value.oif, oifStr, value.oifIsEthernet ? "ether" : "rawip");
Steven Morelanda3074542020-01-13 14:13:44 -0800648 return Result<void>();
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800649 };
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800650 auto res = mClatEgressMap.iterateWithValue(printClatMap);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900651 if (!res.ok()) {
Steven Morelanda3074542020-01-13 14:13:44 -0800652 dw.println("Error printing BPF map: %s", res.error().message().c_str());
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800653 }
654}
655
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800656void ClatdController::dumpIngress(DumpWriter& dw) {
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800657 if (!mClatIngressMap.isValid()) return; // if unsupported just don't dump anything
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700658
659 ScopedIndent bpfIndent(dw);
660 dw.println("BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)");
661
662 ScopedIndent bpfDetailIndent(dw);
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700663 const auto printClatMap = [&dw](const ClatIngressKey& key, const ClatIngressValue& value,
664 const BpfMap<ClatIngressKey, ClatIngressValue>&) {
665 char iifStr[IFNAMSIZ] = "?";
666 char pfx96Str[INET6_ADDRSTRLEN] = "?";
667 char local6Str[INET6_ADDRSTRLEN] = "?";
668 char local4Str[INET_ADDRSTRLEN] = "?";
669 char oifStr[IFNAMSIZ] = "?";
670
671 if_indextoname(key.iif, iifStr);
672 inet_ntop(AF_INET6, &key.pfx96, pfx96Str, sizeof(pfx96Str));
673 inet_ntop(AF_INET6, &key.local6, local6Str, sizeof(local6Str));
674 inet_ntop(AF_INET, &value.local4, local4Str, sizeof(local4Str));
675 if_indextoname(value.oif, oifStr);
676
677 dw.println("%u(%s) %s/96 %s -> %s %u(%s)", key.iif, iifStr, pfx96Str, local6Str, local4Str,
678 value.oif, oifStr);
Steven Morelanda3074542020-01-13 14:13:44 -0800679 return Result<void>();
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700680 };
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800681 auto res = mClatIngressMap.iterateWithValue(printClatMap);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900682 if (!res.ok()) {
Steven Morelanda3074542020-01-13 14:13:44 -0800683 dw.println("Error printing BPF map: %s", res.error().message().c_str());
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700684 }
685}
686
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800687void ClatdController::dumpTrackers(DumpWriter& dw) {
688 ScopedIndent trackerIndent(dw);
689 dw.println("Trackers: iif[iface] nat64Prefix v6Addr -> v4Addr v4iif[v4iface] [netId]");
690
691 ScopedIndent trackerDetailIndent(dw);
692 for (const auto& pair : mClatdTrackers) {
693 const ClatdTracker& tracker = pair.second;
694 dw.println("%u[%s] %s/96 %s -> %s %u[%s] [%u]", tracker.ifIndex, tracker.iface,
695 tracker.pfx96String, tracker.v6Str, tracker.v4Str, tracker.v4ifIndex,
696 tracker.v4iface, tracker.netId);
697 }
698}
699
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800700void ClatdController::dump(DumpWriter& dw) {
701 std::lock_guard guard(mutex);
702
703 ScopedIndent clatdIndent(dw);
704 dw.println("ClatdController");
705
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800706 dumpTrackers(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800707 dumpIngress(dw);
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800708 dumpEgress(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800709}
710
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900711auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree;
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900712auto ClatdController::iptablesRestoreFunction = execIptablesRestore;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900713
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900714} // namespace net
715} // namespace android