blob: 0668de3d449991231727e5cf552a86a8e7a2917e [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
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090050#include "Fwmark.h"
Paul Jensen84c1d032014-05-30 13:29:41 -040051#include "NetdConstants.h"
52#include "NetworkController.h"
Maciej Żenczykowskieec72082020-02-04 23:29:41 -080053#include "OffloadUtils.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
Steven Morelanda3074542020-01-13 14:13:44 -080064using android::base::Result;
Lorenzo Colitti91fd5802019-06-28 19:22:01 +090065using android::base::StringPrintf;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090066using android::base::unique_fd;
Maciej Żenczykowski55262712019-03-29 23:44:56 -070067using android::bpf::BpfMap;
68using android::netdutils::DumpWriter;
69using android::netdutils::ScopedIndent;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090070
Lorenzo Colitti7035f222017-02-13 18:29:00 +090071namespace android {
72namespace net {
73
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080074void ClatdController::resetEgressMap() {
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080075 const auto del = [](const ClatEgressKey& key, const BpfMap<ClatEgressKey, ClatEgressValue>&) {
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080076 ALOGW("Removing stale clat config on interface %d.", key.iif);
Hungming Chen7f725432020-02-07 17:47:23 +080077 int rv = tcFilterDelDevEgressClatIpv4(key.iif);
78 if (rv < 0) ALOGE("tcFilterDelDevEgressClatIpv4() failure: %s", strerror(-rv));
Steven Morelanda3074542020-01-13 14:13:44 -080079 return Result<void>(); // keep on going regardless
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080080 };
81 auto ret = mClatEgressMap.iterate(del);
Bernie Innocenti615fd742020-02-06 03:55:09 +090082 if (!ret.ok()) ALOGE("mClatEgressMap.iterate() failure: %s", strerror(ret.error().code()));
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080083 ret = mClatEgressMap.clear();
Bernie Innocenti615fd742020-02-06 03:55:09 +090084 if (!ret.ok()) ALOGE("mClatEgressMap.clear() failure: %s", strerror(ret.error().code()));
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080085}
86
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -080087void ClatdController::resetIngressMap() {
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080088 const auto del = [](const ClatIngressKey& key,
89 const BpfMap<ClatIngressKey, ClatIngressValue>&) {
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -080090 ALOGW("Removing stale clat config on interface %d.", key.iif);
Hungming Chen7f725432020-02-07 17:47:23 +080091 int rv = tcFilterDelDevIngressClatIpv6(key.iif);
92 if (rv < 0) ALOGE("tcFilterDelDevIngressClatIpv6() failure: %s", strerror(-rv));
Steven Morelanda3074542020-01-13 14:13:44 -080093 return Result<void>(); // keep on going regardless
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -080094 };
95 auto ret = mClatIngressMap.iterate(del);
Bernie Innocenti615fd742020-02-06 03:55:09 +090096 if (!ret.ok()) ALOGE("mClatIngressMap.iterate() failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -080097 ret = mClatIngressMap.clear();
Bernie Innocenti615fd742020-02-06 03:55:09 +090098 if (!ret.ok()) ALOGE("mClatIngressMap.clear() failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -080099}
100
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700101void ClatdController::init(void) {
102 std::lock_guard guard(mutex);
103
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700104 // TODO: should refactor into separate function for testability
Maciej Żenczykowski69cd8222020-02-11 15:36:40 -0800105 if (!bpf::isBpfSupported()) {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700106 ALOGI("Pre-4.9 kernel or pre-P api shipping level - disabling clat ebpf.");
107 mClatEbpfMode = ClatEbpfDisabled;
108 return;
109 }
110
111 // We know the device initially shipped with at least P...,
112 // but did it ship with at least Q?
113
114 uint64_t api_level = base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
115 if (api_level == 0) {
116 ALOGE("Cannot determine initial API level of the device.");
117 api_level = base::GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
118 }
119
120 // Note: MINIMUM_API_REQUIRED is for eBPF as a whole and is thus P
121 if (api_level > bpf::MINIMUM_API_REQUIRED) {
122 ALOGI("4.9+ kernel and device shipped with Q+ - clat ebpf should work.");
123 mClatEbpfMode = ClatEbpfEnabled;
124 } else {
125 // We cannot guarantee that 4.9-P kernels will include NET_CLS_BPF support.
126 ALOGI("4.9+ kernel and device shipped with P - clat ebpf might work.");
127 mClatEbpfMode = ClatEbpfMaybe;
128 }
129
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800130 int rv = getClatEgressMapFd();
Maciej Żenczykowski0bd8da72019-12-16 15:16:28 -0800131 if (rv < 0) {
132 ALOGE("getClatEgressMapFd() failure: %s", strerror(-rv));
133 mClatEbpfMode = ClatEbpfDisabled;
Maciej Żenczykowski0bd8da72019-12-16 15:16:28 -0800134 return;
135 }
136 mClatEgressMap.reset(rv);
137
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700138 rv = getClatIngressMapFd();
139 if (rv < 0) {
140 ALOGE("getClatIngressMapFd() failure: %s", strerror(-rv));
141 mClatEbpfMode = ClatEbpfDisabled;
Maciej Żenczykowski0bd8da72019-12-16 15:16:28 -0800142 mClatEgressMap.reset(-1);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700143 return;
144 }
145 mClatIngressMap.reset(rv);
146
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -0800147 resetEgressMap();
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -0800148 resetIngressMap();
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700149}
150
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900151bool ClatdController::isIpv4AddressFree(in_addr_t addr) {
152 int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
153 if (s == -1) {
154 return 0;
155 }
156
157 // Attempt to connect to the address. If the connection succeeds and getsockname returns the
158 // same then the address is already assigned to the system and we can't use it.
Nick Desaulniers6b357502019-10-11 09:26:44 -0700159 struct sockaddr_in sin = {
160 .sin_family = AF_INET,
Maciej Żenczykowski268190e2019-10-31 23:47:53 -0700161 .sin_port = htons(53),
Nick Desaulniers6b357502019-10-11 09:26:44 -0700162 .sin_addr = {addr},
163 };
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900164 socklen_t len = sizeof(sin);
165 bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 &&
166 getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) &&
167 sin.sin_addr.s_addr == addr;
168
169 close(s);
170 return !inuse;
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900171}
Daniel Drown0da73fc2012-06-20 16:51:39 -0500172
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900173// Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order.
174// ip - the IP address from the configuration file
175// prefixlen - the length of the prefix from which addresses may be selected.
176// returns: the IPv4 address, or INADDR_NONE if no addresses were available
177in_addr_t ClatdController::selectIpv4Address(const in_addr ip, int16_t prefixlen) {
178 // Don't accept prefixes that are too large because we scan addresses one by one.
179 if (prefixlen < 16 || prefixlen > 32) {
180 return INADDR_NONE;
181 }
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900182
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900183 // All these are in host byte order.
184 in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen);
185 in_addr_t ipv4 = ntohl(ip.s_addr);
186 in_addr_t first_ipv4 = ipv4;
187 in_addr_t prefix = ipv4 & mask;
188
189 // Pick the first IPv4 address in the pool, wrapping around if necessary.
190 // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0.
191 do {
192 if (isIpv4AddressFreeFunc(htonl(ipv4))) {
193 return htonl(ipv4);
194 }
195 ipv4 = prefix | ((ipv4 + 1) & ~mask);
196 } while (ipv4 != first_ipv4);
197
198 return INADDR_NONE;
199}
200
201// Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix.
202void ClatdController::makeChecksumNeutral(in6_addr* v6, const in_addr v4,
203 const in6_addr& nat64Prefix) {
204 // Fill last 8 bytes of IPv6 address with random bits.
205 arc4random_buf(&v6->s6_addr[8], 8);
206
207 // Make the IID checksum-neutral. That is, make it so that:
208 // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6)
209 // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4):
210 // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix)
211 // Do this by adjusting the two bytes in the middle of the IID.
212
213 uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12];
214
215 uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4));
216 uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) +
217 ip_checksum_add(0, v6, sizeof(*v6));
218
219 uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2);
220 v6->s6_addr[11] = delta >> 8;
221 v6->s6_addr[12] = delta & 0xff;
222}
223
224// Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix.
225int ClatdController::generateIpv6Address(const char* iface, const in_addr v4,
226 const in6_addr& nat64Prefix, in6_addr* v6) {
227 unique_fd s(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0));
228 if (s == -1) return -errno;
229
230 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) {
Luke Huang6d301232018-08-01 14:05:18 +0800231 return -errno;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500232 }
233
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900234 sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix};
235 if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) {
236 return -errno;
237 }
238
239 socklen_t len = sizeof(sin6);
240 if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) {
241 return -errno;
242 }
243
244 *v6 = sin6.sin6_addr;
245
246 if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) ||
247 IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) {
248 return -ENETUNREACH;
249 }
250
251 makeChecksumNeutral(v6, v4, nat64Prefix);
252
253 return 0;
254}
255
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700256void ClatdController::maybeStartBpf(const ClatdTracker& tracker) {
257 if (mClatEbpfMode == ClatEbpfDisabled) return;
258
259 int rv = hardwareAddressType(tracker.iface);
260 if (rv < 0) {
261 ALOGE("hardwareAddressType(%s[%d]) failure: %s", tracker.iface, tracker.ifIndex,
262 strerror(-rv));
263 return;
264 }
265
266 bool isEthernet;
267 switch (rv) {
268 case ARPHRD_ETHER:
269 isEthernet = true;
270 break;
271 case ARPHRD_RAWIP: // in Linux 4.14+ rmnet support was upstreamed and this is 519
272 case 530: // this is ARPHRD_RAWIP on some Android 4.9 kernels with rmnet
273 isEthernet = false;
274 break;
275 default:
276 ALOGE("hardwareAddressType(%s[%d]) returned unknown type %d.", tracker.iface,
277 tracker.ifIndex, rv);
278 return;
279 }
280
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800281 // This program will be attached to the v4-* interface which is a TUN and thus always rawip.
Maciej Żenczykowskiad730892020-02-13 18:21:32 -0800282 rv = getClatEgressProgFd(RAWIP);
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800283 if (rv < 0) {
Maciej Żenczykowskiad730892020-02-13 18:21:32 -0800284 ALOGE("getClatEgressProgFd(RAWIP) failure: %s", strerror(-rv));
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800285 return;
286 }
287 unique_fd txRawIpProgFd(rv);
288
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700289 rv = getClatIngressProgFd(isEthernet);
290 if (rv < 0) {
291 ALOGE("getClatIngressProgFd(%d) failure: %s", isEthernet, strerror(-rv));
292 return;
293 }
Maciej Żenczykowski75f4dc32019-12-16 18:38:54 -0800294 unique_fd rxProgFd(rv);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700295
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800296 ClatEgressKey txKey = {
297 .iif = tracker.v4ifIndex,
298 .local4 = tracker.v4,
299 };
300 ClatEgressValue txValue = {
301 .oif = tracker.ifIndex,
302 .local6 = tracker.v6,
303 .pfx96 = tracker.pfx96,
Maciej Żenczykowski97ec1d82019-12-17 12:11:21 -0800304 .oifIsEthernet = isEthernet,
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800305 };
306
307 auto ret = mClatEgressMap.writeValue(txKey, txValue, BPF_ANY);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900308 if (!ret.ok()) {
Maciej Żenczykowski4460b8c2020-01-20 03:47:41 -0800309 ALOGE("mClatEgressMap.writeValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800310 return;
311 }
312
313 ClatIngressKey rxKey = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700314 .iif = tracker.ifIndex,
315 .pfx96 = tracker.pfx96,
316 .local6 = tracker.v6,
317 };
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800318 ClatIngressValue rxValue = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700319 // TODO: move all the clat code to eBPF and remove the tun interface entirely.
Maciej Żenczykowski39b0b902019-05-08 00:36:30 -0700320 .oif = tracker.v4ifIndex,
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700321 .local4 = tracker.v4,
322 };
323
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800324 ret = mClatIngressMap.writeValue(rxKey, rxValue, BPF_ANY);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900325 if (!ret.ok()) {
Maciej Żenczykowski4460b8c2020-01-20 03:47:41 -0800326 ALOGE("mClatIngressMap.writeValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800327 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900328 if (!ret.ok())
329 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700330 return;
331 }
332
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800333 // We do tc setup *after* populating the maps, so scanning through them
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700334 // can always be used to tell us what needs cleanup.
335
Hungming Chen7f725432020-02-07 17:47:23 +0800336 // Usually the clsact will be added in RouteController::addInterfaceToPhysicalNetwork.
337 // But clat is started before the v4- interface is added to the network. The clat startup have
338 // to add clsact of v4- tun interface first for adding bpf filter in maybeStartBpf.
339 // TODO: move "qdisc add clsact" of v4- tun interface out from ClatdController.
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800340 rv = tcQdiscAddDevClsact(tracker.v4ifIndex);
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800341 if (rv) {
342 ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", tracker.v4ifIndex, tracker.v4iface,
343 strerror(-rv));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800344 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900345 if (!ret.ok())
346 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800347 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900348 if (!ret.ok())
349 ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800350 return;
351 }
352
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800353 rv = tcFilterAddDevEgressClatIpv4(tracker.v4ifIndex, txRawIpProgFd, RAWIP);
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800354 if (rv) {
355 if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) {
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800356 ALOGI("tcFilterAddDevEgressClatIpv4(%d[%s], RAWIP): %s", tracker.v4ifIndex,
357 tracker.v4iface, strerror(-rv));
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800358 } else {
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800359 ALOGE("tcFilterAddDevEgressClatIpv4(%d[%s], RAWIP) failure: %s", tracker.v4ifIndex,
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800360 tracker.v4iface, strerror(-rv));
361 }
Hungming Chen7f725432020-02-07 17:47:23 +0800362
363 // The v4- interface clsact is not deleted for unwinding error because once it is created
364 // with interface addition, the lifetime is till interface deletion. Moreover, the clsact
365 // has no clat filter now. It should not break anything.
366
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800367 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900368 if (!ret.ok())
369 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800370 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900371 if (!ret.ok())
372 ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800373 return;
374 }
375
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800376 rv = tcFilterAddDevIngressClatIpv6(tracker.ifIndex, rxProgFd, isEthernet);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700377 if (rv) {
378 if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) {
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800379 ALOGI("tcFilterAddDevIngressClatIpv6(%d[%s], %d): %s", tracker.ifIndex, tracker.iface,
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700380 isEthernet, strerror(-rv));
Maciej Żenczykowskib140a3a2019-12-15 11:53:46 -0800381 } else {
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800382 ALOGE("tcFilterAddDevIngressClatIpv6(%d[%s], %d) failure: %s", tracker.ifIndex,
Maciej Żenczykowskib140a3a2019-12-15 11:53:46 -0800383 tracker.iface, isEthernet, strerror(-rv));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700384 }
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800385 rv = tcFilterDelDevEgressClatIpv4(tracker.v4ifIndex);
Hungming Chene55f45d2020-02-07 15:10:52 +0800386 if (rv) {
387 ALOGE("tcFilterDelDevEgressClatIpv4(%d[%s]) failure: %s", tracker.v4ifIndex,
388 tracker.v4iface, strerror(-rv));
389 }
Hungming Chen7f725432020-02-07 17:47:23 +0800390
391 // The v4- interface clsact is not deleted. See the reason in the error unwinding code of
392 // the egress filter attaching of v4- tun interface.
393
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800394 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900395 if (!ret.ok())
396 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800397 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900398 if (!ret.ok())
399 ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700400 return;
401 }
402
403 // success
404}
405
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800406void ClatdController::setIptablesDropRule(bool add, const char* iface, const char* pfx96Str,
407 const char* v6Str) {
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900408 std::string cmd = StringPrintf(
409 "*raw\n"
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800410 "%s %s -i %s -s %s/96 -d %s -j DROP\n"
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900411 "COMMIT\n",
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800412 (add ? "-A" : "-D"), LOCAL_RAW_PREROUTING, iface, pfx96Str, v6Str);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900413
414 iptablesRestoreFunction(V6, cmd);
415}
416
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700417void ClatdController::maybeStopBpf(const ClatdTracker& tracker) {
418 if (mClatEbpfMode == ClatEbpfDisabled) return;
419
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800420 int rv = tcFilterDelDevIngressClatIpv6(tracker.ifIndex);
Hungming Chene55f45d2020-02-07 15:10:52 +0800421 if (rv < 0) {
422 ALOGE("tcFilterDelDevIngressClatIpv6(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
423 strerror(-rv));
424 }
425
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800426 rv = tcFilterDelDevEgressClatIpv4(tracker.v4ifIndex);
Hungming Chene55f45d2020-02-07 15:10:52 +0800427 if (rv < 0) {
428 ALOGE("tcFilterDelDevEgressClatIpv4(%d[%s]) failure: %s", tracker.v4ifIndex,
429 tracker.v4iface, strerror(-rv));
430 }
431
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800432 // We cleanup the maps last, so scanning through them can be used to
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700433 // determine what still needs cleanup.
434
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800435 ClatEgressKey txKey = {
436 .iif = tracker.v4ifIndex,
437 .local4 = tracker.v4,
438 };
439
440 auto ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900441 if (!ret.ok()) ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800442
443 ClatIngressKey rxKey = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700444 .iif = tracker.ifIndex,
445 .pfx96 = tracker.pfx96,
446 .local6 = tracker.v6,
447 };
448
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800449 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900450 if (!ret.ok()) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700451}
452
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900453// Finds the tracker of the clatd running on interface |interface|, or nullptr if clatd has not been
454// started on |interface|.
455ClatdController::ClatdTracker* ClatdController::getClatdTracker(const std::string& interface) {
456 auto it = mClatdTrackers.find(interface);
457 return (it == mClatdTrackers.end() ? nullptr : &it->second);
458}
459
460// Initializes a ClatdTracker for the specified interface.
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700461int ClatdController::ClatdTracker::init(unsigned networkId, const std::string& interface,
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700462 const std::string& v4interface,
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900463 const std::string& nat64Prefix) {
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700464 netId = networkId;
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900465
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900466 fwmark.netId = netId;
467 fwmark.explicitlySelected = true;
468 fwmark.protectedFromVpn = true;
469 fwmark.permission = PERMISSION_SYSTEM;
470
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900471 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900472 snprintf(netIdString, sizeof(netIdString), "%u", netId);
473 strlcpy(iface, interface.c_str(), sizeof(iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700474 ifIndex = if_nametoindex(iface);
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700475 strlcpy(v4iface, v4interface.c_str(), sizeof(v4iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700476 v4ifIndex = if_nametoindex(v4iface);
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900477
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900478 // Pass in everything that clatd needs: interface, a netid to use for DNS lookups, a fwmark for
479 // outgoing packets, the NAT64 prefix, and the IPv4 and IPv6 addresses.
480 // Validate the prefix and strip off the prefix length.
481 uint8_t family;
482 uint8_t prefixLen;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700483 int res = parsePrefix(nat64Prefix.c_str(), &family, &pfx96, sizeof(pfx96), &prefixLen);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900484 // clatd only supports /96 prefixes.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700485 if (res != sizeof(pfx96)) return res;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900486 if (family != AF_INET6) return -EAFNOSUPPORT;
487 if (prefixLen != 96) return -EINVAL;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700488 if (!inet_ntop(AF_INET6, &pfx96, pfx96String, sizeof(pfx96String))) return -errno;
Luke Huang6d301232018-08-01 14:05:18 +0800489
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900490 // Pick an IPv4 address.
491 // TODO: this picks the address based on other addresses that are assigned to interfaces, but
492 // the address is only actually assigned to an interface once clatd starts up. So we could end
493 // up with two clatd instances with the same IPv4 address.
494 // Stop doing this and instead pick a free one from the kV4Addr pool.
Maciej Żenczykowski55cacfb2019-03-30 02:01:35 -0700495 v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900496 if (v4.s_addr == INADDR_NONE) {
497 ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen);
498 return -EADDRNOTAVAIL;
499 }
500 if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno;
501
502 // Generate a checksum-neutral IID.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700503 if (generateIpv6Address(iface, v4, pfx96, &v6)) {
504 ALOGE("Unable to find global source address on %s for %s", iface, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900505 return -EADDRNOTAVAIL;
506 }
507 if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno;
508
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700509 ALOGD("starting clatd on %s v4=%s v6=%s pfx96=%s", iface, v4Str, v6Str, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900510 return 0;
511}
512
513int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix,
514 std::string* v6Str) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700515 std::lock_guard guard(mutex);
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700516
517 // 1. fail if pre-existing tracker already exists
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900518 ClatdTracker* existing = getClatdTracker(interface);
519 if (existing != nullptr) {
520 ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700521 return -EBUSY;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900522 }
523
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700524 // 2. get network id associated with this external interface
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700525 unsigned networkId = mNetCtrl->getNetworkForInterface(interface.c_str());
526 if (networkId == NETID_UNSET) {
527 ALOGE("Interface %s not assigned to any netId", interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700528 return -ENODEV;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900529 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700530
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700531 // 3. open the tun device in non blocking mode as required by clatd
Maciej Żenczykowski94db6582020-01-27 21:47:06 -0800532 int res = open("/dev/net/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC);
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700533 if (res == -1) {
534 res = errno;
535 ALOGE("open of tun device failed (%s)", strerror(res));
536 return -res;
537 }
538 unique_fd tmpTunFd(res);
539
540 // 4. create the v4-... tun interface
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700541 std::string v4interface("v4-");
542 v4interface += interface;
543
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700544 struct ifreq ifr = {
545 .ifr_flags = IFF_TUN,
546 };
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700547 strlcpy(ifr.ifr_name, v4interface.c_str(), sizeof(ifr.ifr_name));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700548
549 res = ioctl(tmpTunFd, TUNSETIFF, &ifr, sizeof(ifr));
550 if (res == -1) {
551 res = errno;
552 ALOGE("ioctl(TUNSETIFF) failed (%s)", strerror(res));
553 return -res;
554 }
555
Maciej Żenczykowskia1699952019-05-11 17:07:44 -0700556 // disable IPv6 on it - failing to do so is not a critical error
557 res = InterfaceController::setEnableIPv6(v4interface.c_str(), 0);
558 if (res) ALOGE("setEnableIPv6 %s failed (%s)", v4interface.c_str(), strerror(res));
559
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700560 // 5. initialize tracker object
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700561 ClatdTracker tracker;
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700562 int ret = tracker.init(networkId, interface, v4interface, nat64Prefix);
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700563 if (ret) return ret;
564
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700565 // 6. create a throwaway socket to reserve a file descriptor number
566 res = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
567 if (res == -1) {
568 res = errno;
569 ALOGE("socket(ipv6/udp) failed (%s)", strerror(res));
570 return -res;
571 }
572 unique_fd passedTunFd(res);
573
574 // 7. this is the FD we'll pass to clatd on the cli, so need it as a string
575 char passedTunFdStr[INT32_STRLEN];
576 snprintf(passedTunFdStr, sizeof(passedTunFdStr), "%d", passedTunFd.get());
577
578 // 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 +0900579 std::string progname("clatd-");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900580 progname += tracker.iface;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500581
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900582 // clang-format off
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700583 const char* args[] = {progname.c_str(),
584 "-i", tracker.iface,
585 "-n", tracker.netIdString,
586 "-m", tracker.fwmarkString,
587 "-p", tracker.pfx96String,
588 "-4", tracker.v4Str,
589 "-6", tracker.v6Str,
590 "-t", passedTunFdStr,
591 nullptr};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900592 // clang-format on
593
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700594 // 9. register vfork requirement
595 posix_spawnattr_t attr;
596 res = posix_spawnattr_init(&attr);
597 if (res) {
598 ALOGE("posix_spawnattr_init failed (%s)", strerror(res));
599 return -res;
600 }
601 const android::base::ScopeGuard attrGuard = [&] { posix_spawnattr_destroy(&attr); };
602 res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_USEVFORK);
603 if (res) {
604 ALOGE("posix_spawnattr_setflags failed (%s)", strerror(res));
605 return -res;
606 }
607
608 // 10. register dup2() action: this is what 'clears' the CLOEXEC flag
609 // on the tun fd that we want the child clatd process to inherit
610 // (this will happen after the vfork, and before the execve)
611 posix_spawn_file_actions_t fa;
612 res = posix_spawn_file_actions_init(&fa);
613 if (res) {
614 ALOGE("posix_spawn_file_actions_init failed (%s)", strerror(res));
615 return -res;
616 }
617 const android::base::ScopeGuard faGuard = [&] { posix_spawn_file_actions_destroy(&fa); };
618 res = posix_spawn_file_actions_adddup2(&fa, tmpTunFd, passedTunFd);
619 if (res) {
620 ALOGE("posix_spawn_file_actions_adddup2 failed (%s)", strerror(res));
621 return -res;
622 }
623
Maciej Żenczykowski083688f2019-12-23 14:43:09 -0800624 // 11. add the drop rule for iptables.
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800625 setIptablesDropRule(true, tracker.iface, tracker.pfx96String, tracker.v6Str);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900626
627 // 12. actually perform vfork/dup2/execve
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700628 res = posix_spawn(&tracker.pid, kClatdPath, &fa, &attr, (char* const*)args, nullptr);
Luke Huang40962442019-02-26 11:46:10 +0800629 if (res) {
630 ALOGE("posix_spawn failed (%s)", strerror(res));
Luke Huang6d301232018-08-01 14:05:18 +0800631 return -res;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500632 }
633
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900634 // 13. configure eBPF offload - if possible
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700635 maybeStartBpf(tracker);
636
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900637 mClatdTrackers[interface] = tracker;
638 ALOGD("clatd started on %s", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500639
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900640 *v6Str = tracker.v6Str;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500641 return 0;
642}
643
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900644int ClatdController::stopClatd(const std::string& interface) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700645 std::lock_guard guard(mutex);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900646 ClatdTracker* tracker = getClatdTracker(interface);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900647
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900648 if (tracker == nullptr) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500649 ALOGE("clatd already stopped");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900650 return -ENODEV;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500651 }
652
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900653 ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500654
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700655 maybeStopBpf(*tracker);
656
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900657 kill(tracker->pid, SIGTERM);
658 waitpid(tracker->pid, nullptr, 0);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900659
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800660 setIptablesDropRule(false, tracker->iface, tracker->pfx96String, tracker->v6Str);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900661 mClatdTrackers.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500662
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900663 ALOGD("clatd on %s stopped", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500664
665 return 0;
666}
667
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800668void ClatdController::dumpEgress(DumpWriter& dw) {
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800669 if (!mClatEgressMap.isValid()) return; // if unsupported just don't dump anything
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800670
671 ScopedIndent bpfIndent(dw);
672 dw.println("BPF egress map: iif(iface) v4Addr -> v6Addr nat64Prefix oif(iface)");
673
674 ScopedIndent bpfDetailIndent(dw);
675 const auto printClatMap = [&dw](const ClatEgressKey& key, const ClatEgressValue& value,
676 const BpfMap<ClatEgressKey, ClatEgressValue>&) {
677 char iifStr[IFNAMSIZ] = "?";
678 char local4Str[INET_ADDRSTRLEN] = "?";
679 char local6Str[INET6_ADDRSTRLEN] = "?";
680 char pfx96Str[INET6_ADDRSTRLEN] = "?";
681 char oifStr[IFNAMSIZ] = "?";
682
683 if_indextoname(key.iif, iifStr);
684 inet_ntop(AF_INET, &key.local4, local4Str, sizeof(local4Str));
685 inet_ntop(AF_INET6, &value.local6, local6Str, sizeof(local6Str));
686 inet_ntop(AF_INET6, &value.pfx96, pfx96Str, sizeof(pfx96Str));
687 if_indextoname(value.oif, oifStr);
688
Maciej Żenczykowski97ec1d82019-12-17 12:11:21 -0800689 dw.println("%u(%s) %s -> %s %s/96 %u(%s) %s", key.iif, iifStr, local4Str, local6Str,
690 pfx96Str, value.oif, oifStr, value.oifIsEthernet ? "ether" : "rawip");
Steven Morelanda3074542020-01-13 14:13:44 -0800691 return Result<void>();
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800692 };
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800693 auto res = mClatEgressMap.iterateWithValue(printClatMap);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900694 if (!res.ok()) {
Steven Morelanda3074542020-01-13 14:13:44 -0800695 dw.println("Error printing BPF map: %s", res.error().message().c_str());
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800696 }
697}
698
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800699void ClatdController::dumpIngress(DumpWriter& dw) {
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800700 if (!mClatIngressMap.isValid()) return; // if unsupported just don't dump anything
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700701
702 ScopedIndent bpfIndent(dw);
703 dw.println("BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)");
704
705 ScopedIndent bpfDetailIndent(dw);
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700706 const auto printClatMap = [&dw](const ClatIngressKey& key, const ClatIngressValue& value,
707 const BpfMap<ClatIngressKey, ClatIngressValue>&) {
708 char iifStr[IFNAMSIZ] = "?";
709 char pfx96Str[INET6_ADDRSTRLEN] = "?";
710 char local6Str[INET6_ADDRSTRLEN] = "?";
711 char local4Str[INET_ADDRSTRLEN] = "?";
712 char oifStr[IFNAMSIZ] = "?";
713
714 if_indextoname(key.iif, iifStr);
715 inet_ntop(AF_INET6, &key.pfx96, pfx96Str, sizeof(pfx96Str));
716 inet_ntop(AF_INET6, &key.local6, local6Str, sizeof(local6Str));
717 inet_ntop(AF_INET, &value.local4, local4Str, sizeof(local4Str));
718 if_indextoname(value.oif, oifStr);
719
720 dw.println("%u(%s) %s/96 %s -> %s %u(%s)", key.iif, iifStr, pfx96Str, local6Str, local4Str,
721 value.oif, oifStr);
Steven Morelanda3074542020-01-13 14:13:44 -0800722 return Result<void>();
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700723 };
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800724 auto res = mClatIngressMap.iterateWithValue(printClatMap);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900725 if (!res.ok()) {
Steven Morelanda3074542020-01-13 14:13:44 -0800726 dw.println("Error printing BPF map: %s", res.error().message().c_str());
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700727 }
728}
729
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800730void ClatdController::dumpTrackers(DumpWriter& dw) {
731 ScopedIndent trackerIndent(dw);
732 dw.println("Trackers: iif[iface] nat64Prefix v6Addr -> v4Addr v4iif[v4iface] [netId]");
733
734 ScopedIndent trackerDetailIndent(dw);
735 for (const auto& pair : mClatdTrackers) {
736 const ClatdTracker& tracker = pair.second;
737 dw.println("%u[%s] %s/96 %s -> %s %u[%s] [%u]", tracker.ifIndex, tracker.iface,
738 tracker.pfx96String, tracker.v6Str, tracker.v4Str, tracker.v4ifIndex,
739 tracker.v4iface, tracker.netId);
740 }
741}
742
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800743void ClatdController::dump(DumpWriter& dw) {
744 std::lock_guard guard(mutex);
745
746 ScopedIndent clatdIndent(dw);
747 dw.println("ClatdController");
748
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800749 dumpTrackers(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800750 dumpIngress(dw);
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800751 dumpEgress(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800752}
753
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900754auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree;
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900755auto ClatdController::iptablesRestoreFunction = execIptablesRestore;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900756
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900757} // namespace net
758} // namespace android