blob: b9a6075775f47a991fa55223cfd7230c92f177ac [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() {
75 int netlinkFd = mNetlinkFd.get();
76
77 const auto del = [&netlinkFd](const ClatEgressKey& key,
78 const BpfMap<ClatEgressKey, ClatEgressValue>&) {
79 ALOGW("Removing stale clat config on interface %d.", key.iif);
80 int rv = tcQdiscDelDevClsact(netlinkFd, key.iif);
81 if (rv < 0) ALOGE("tcQdiscDelDevClsact() failure: %s", strerror(-rv));
Steven Morelanda3074542020-01-13 14:13:44 -080082 return Result<void>(); // keep on going regardless
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080083 };
84 auto ret = mClatEgressMap.iterate(del);
Bernie Innocenti615fd742020-02-06 03:55:09 +090085 if (!ret.ok()) ALOGE("mClatEgressMap.iterate() failure: %s", strerror(ret.error().code()));
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080086 ret = mClatEgressMap.clear();
Bernie Innocenti615fd742020-02-06 03:55:09 +090087 if (!ret.ok()) ALOGE("mClatEgressMap.clear() failure: %s", strerror(ret.error().code()));
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080088}
89
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -080090void ClatdController::resetIngressMap() {
91 int netlinkFd = mNetlinkFd.get();
92
93 const auto del = [&netlinkFd](const ClatIngressKey& key,
94 const BpfMap<ClatIngressKey, ClatIngressValue>&) {
95 ALOGW("Removing stale clat config on interface %d.", key.iif);
96 int rv = tcQdiscDelDevClsact(netlinkFd, key.iif);
97 if (rv < 0) ALOGE("tcQdiscDelDevClsact() failure: %s", strerror(-rv));
Steven Morelanda3074542020-01-13 14:13:44 -080098 return Result<void>(); // keep on going regardless
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -080099 };
100 auto ret = mClatIngressMap.iterate(del);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900101 if (!ret.ok()) ALOGE("mClatIngressMap.iterate() failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -0800102 ret = mClatIngressMap.clear();
Bernie Innocenti615fd742020-02-06 03:55:09 +0900103 if (!ret.ok()) ALOGE("mClatIngressMap.clear() failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -0800104}
105
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700106void ClatdController::init(void) {
107 std::lock_guard guard(mutex);
108
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700109 // TODO: should refactor into separate function for testability
110 if (bpf::getBpfSupportLevel() == bpf::BpfLevel::NONE) {
111 ALOGI("Pre-4.9 kernel or pre-P api shipping level - disabling clat ebpf.");
112 mClatEbpfMode = ClatEbpfDisabled;
113 return;
114 }
115
116 // We know the device initially shipped with at least P...,
117 // but did it ship with at least Q?
118
119 uint64_t api_level = base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
120 if (api_level == 0) {
121 ALOGE("Cannot determine initial API level of the device.");
122 api_level = base::GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
123 }
124
125 // Note: MINIMUM_API_REQUIRED is for eBPF as a whole and is thus P
126 if (api_level > bpf::MINIMUM_API_REQUIRED) {
127 ALOGI("4.9+ kernel and device shipped with Q+ - clat ebpf should work.");
128 mClatEbpfMode = ClatEbpfEnabled;
129 } else {
130 // We cannot guarantee that 4.9-P kernels will include NET_CLS_BPF support.
131 ALOGI("4.9+ kernel and device shipped with P - clat ebpf might work.");
132 mClatEbpfMode = ClatEbpfMaybe;
133 }
134
135 int rv = openNetlinkSocket();
136 if (rv < 0) {
137 ALOGE("openNetlinkSocket() failure: %s", strerror(-rv));
138 mClatEbpfMode = ClatEbpfDisabled;
139 return;
140 }
141 mNetlinkFd.reset(rv);
142
Maciej Żenczykowski0bd8da72019-12-16 15:16:28 -0800143 rv = getClatEgressMapFd();
144 if (rv < 0) {
145 ALOGE("getClatEgressMapFd() failure: %s", strerror(-rv));
146 mClatEbpfMode = ClatEbpfDisabled;
147 mNetlinkFd.reset(-1);
148 return;
149 }
150 mClatEgressMap.reset(rv);
151
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700152 rv = getClatIngressMapFd();
153 if (rv < 0) {
154 ALOGE("getClatIngressMapFd() failure: %s", strerror(-rv));
155 mClatEbpfMode = ClatEbpfDisabled;
Maciej Żenczykowski0bd8da72019-12-16 15:16:28 -0800156 mClatEgressMap.reset(-1);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700157 mNetlinkFd.reset(-1);
158 return;
159 }
160 mClatIngressMap.reset(rv);
161
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -0800162 resetEgressMap();
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -0800163 resetIngressMap();
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700164}
165
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900166bool ClatdController::isIpv4AddressFree(in_addr_t addr) {
167 int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
168 if (s == -1) {
169 return 0;
170 }
171
172 // Attempt to connect to the address. If the connection succeeds and getsockname returns the
173 // same then the address is already assigned to the system and we can't use it.
Nick Desaulniers6b357502019-10-11 09:26:44 -0700174 struct sockaddr_in sin = {
175 .sin_family = AF_INET,
Maciej Żenczykowski268190e2019-10-31 23:47:53 -0700176 .sin_port = htons(53),
Nick Desaulniers6b357502019-10-11 09:26:44 -0700177 .sin_addr = {addr},
178 };
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900179 socklen_t len = sizeof(sin);
180 bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 &&
181 getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) &&
182 sin.sin_addr.s_addr == addr;
183
184 close(s);
185 return !inuse;
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900186}
Daniel Drown0da73fc2012-06-20 16:51:39 -0500187
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900188// Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order.
189// ip - the IP address from the configuration file
190// prefixlen - the length of the prefix from which addresses may be selected.
191// returns: the IPv4 address, or INADDR_NONE if no addresses were available
192in_addr_t ClatdController::selectIpv4Address(const in_addr ip, int16_t prefixlen) {
193 // Don't accept prefixes that are too large because we scan addresses one by one.
194 if (prefixlen < 16 || prefixlen > 32) {
195 return INADDR_NONE;
196 }
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900197
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900198 // All these are in host byte order.
199 in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen);
200 in_addr_t ipv4 = ntohl(ip.s_addr);
201 in_addr_t first_ipv4 = ipv4;
202 in_addr_t prefix = ipv4 & mask;
203
204 // Pick the first IPv4 address in the pool, wrapping around if necessary.
205 // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0.
206 do {
207 if (isIpv4AddressFreeFunc(htonl(ipv4))) {
208 return htonl(ipv4);
209 }
210 ipv4 = prefix | ((ipv4 + 1) & ~mask);
211 } while (ipv4 != first_ipv4);
212
213 return INADDR_NONE;
214}
215
216// Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix.
217void ClatdController::makeChecksumNeutral(in6_addr* v6, const in_addr v4,
218 const in6_addr& nat64Prefix) {
219 // Fill last 8 bytes of IPv6 address with random bits.
220 arc4random_buf(&v6->s6_addr[8], 8);
221
222 // Make the IID checksum-neutral. That is, make it so that:
223 // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6)
224 // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4):
225 // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix)
226 // Do this by adjusting the two bytes in the middle of the IID.
227
228 uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12];
229
230 uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4));
231 uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) +
232 ip_checksum_add(0, v6, sizeof(*v6));
233
234 uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2);
235 v6->s6_addr[11] = delta >> 8;
236 v6->s6_addr[12] = delta & 0xff;
237}
238
239// Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix.
240int ClatdController::generateIpv6Address(const char* iface, const in_addr v4,
241 const in6_addr& nat64Prefix, in6_addr* v6) {
242 unique_fd s(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0));
243 if (s == -1) return -errno;
244
245 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) {
Luke Huang6d301232018-08-01 14:05:18 +0800246 return -errno;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500247 }
248
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900249 sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix};
250 if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) {
251 return -errno;
252 }
253
254 socklen_t len = sizeof(sin6);
255 if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) {
256 return -errno;
257 }
258
259 *v6 = sin6.sin6_addr;
260
261 if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) ||
262 IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) {
263 return -ENETUNREACH;
264 }
265
266 makeChecksumNeutral(v6, v4, nat64Prefix);
267
268 return 0;
269}
270
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700271void ClatdController::maybeStartBpf(const ClatdTracker& tracker) {
272 if (mClatEbpfMode == ClatEbpfDisabled) return;
273
274 int rv = hardwareAddressType(tracker.iface);
275 if (rv < 0) {
276 ALOGE("hardwareAddressType(%s[%d]) failure: %s", tracker.iface, tracker.ifIndex,
277 strerror(-rv));
278 return;
279 }
280
281 bool isEthernet;
282 switch (rv) {
283 case ARPHRD_ETHER:
284 isEthernet = true;
285 break;
286 case ARPHRD_RAWIP: // in Linux 4.14+ rmnet support was upstreamed and this is 519
287 case 530: // this is ARPHRD_RAWIP on some Android 4.9 kernels with rmnet
288 isEthernet = false;
289 break;
290 default:
291 ALOGE("hardwareAddressType(%s[%d]) returned unknown type %d.", tracker.iface,
292 tracker.ifIndex, rv);
293 return;
294 }
295
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800296 // This program will be attached to the v4-* interface which is a TUN and thus always rawip.
297 rv = getClatEgressProgFd(false);
298 if (rv < 0) {
299 ALOGE("getClatEgressProgFd(false) failure: %s", strerror(-rv));
300 return;
301 }
302 unique_fd txRawIpProgFd(rv);
303
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700304 rv = getClatIngressProgFd(isEthernet);
305 if (rv < 0) {
306 ALOGE("getClatIngressProgFd(%d) failure: %s", isEthernet, strerror(-rv));
307 return;
308 }
Maciej Żenczykowski75f4dc32019-12-16 18:38:54 -0800309 unique_fd rxProgFd(rv);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700310
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800311 ClatEgressKey txKey = {
312 .iif = tracker.v4ifIndex,
313 .local4 = tracker.v4,
314 };
315 ClatEgressValue txValue = {
316 .oif = tracker.ifIndex,
317 .local6 = tracker.v6,
318 .pfx96 = tracker.pfx96,
Maciej Żenczykowski97ec1d82019-12-17 12:11:21 -0800319 .oifIsEthernet = isEthernet,
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800320 };
321
322 auto ret = mClatEgressMap.writeValue(txKey, txValue, BPF_ANY);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900323 if (!ret.ok()) {
Maciej Żenczykowski4460b8c2020-01-20 03:47:41 -0800324 ALOGE("mClatEgressMap.writeValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800325 return;
326 }
327
328 ClatIngressKey rxKey = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700329 .iif = tracker.ifIndex,
330 .pfx96 = tracker.pfx96,
331 .local6 = tracker.v6,
332 };
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800333 ClatIngressValue rxValue = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700334 // TODO: move all the clat code to eBPF and remove the tun interface entirely.
Maciej Żenczykowski39b0b902019-05-08 00:36:30 -0700335 .oif = tracker.v4ifIndex,
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700336 .local4 = tracker.v4,
337 };
338
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800339 ret = mClatIngressMap.writeValue(rxKey, rxValue, BPF_ANY);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900340 if (!ret.ok()) {
Maciej Żenczykowski4460b8c2020-01-20 03:47:41 -0800341 ALOGE("mClatIngressMap.writeValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800342 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900343 if (!ret.ok())
344 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700345 return;
346 }
347
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800348 // We do tc setup *after* populating the maps, so scanning through them
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700349 // can always be used to tell us what needs cleanup.
350
351 rv = tcQdiscAddDevClsact(mNetlinkFd, tracker.ifIndex);
352 if (rv) {
353 ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
354 strerror(-rv));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800355 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900356 if (!ret.ok())
357 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800358 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900359 if (!ret.ok())
360 ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700361 return;
362 }
363
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800364 rv = tcQdiscAddDevClsact(mNetlinkFd, tracker.v4ifIndex);
365 if (rv) {
366 ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", tracker.v4ifIndex, tracker.v4iface,
367 strerror(-rv));
368 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex);
369 if (rv < 0) {
370 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
371 strerror(-rv));
372 }
373 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900374 if (!ret.ok())
375 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800376 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900377 if (!ret.ok())
378 ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800379 return;
380 }
381
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800382 rv = tcFilterAddDevEgressBpf(mNetlinkFd, tracker.v4ifIndex, txRawIpProgFd, false);
383 if (rv) {
384 if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) {
385 ALOGI("tcFilterAddDevEgressBpf(%d[%s], false): %s", tracker.v4ifIndex, tracker.v4iface,
386 strerror(-rv));
387 } else {
388 ALOGE("tcFilterAddDevEgressBpf(%d[%s], false) failure: %s", tracker.v4ifIndex,
389 tracker.v4iface, strerror(-rv));
390 }
391 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex);
392 if (rv) {
393 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
394 strerror(-rv));
395 }
396 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.v4ifIndex);
397 if (rv) {
398 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.v4ifIndex, tracker.v4iface,
399 strerror(-rv));
400 }
401 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900402 if (!ret.ok())
403 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800404 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900405 if (!ret.ok())
406 ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800407 return;
408 }
409
Maciej Żenczykowski75f4dc32019-12-16 18:38:54 -0800410 rv = tcFilterAddDevIngressBpf(mNetlinkFd, tracker.ifIndex, rxProgFd, isEthernet);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700411 if (rv) {
412 if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) {
Maciej Żenczykowskib140a3a2019-12-15 11:53:46 -0800413 ALOGI("tcFilterAddDevIngressBpf(%d[%s], %d): %s", tracker.ifIndex, tracker.iface,
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700414 isEthernet, strerror(-rv));
Maciej Żenczykowskib140a3a2019-12-15 11:53:46 -0800415 } else {
416 ALOGE("tcFilterAddDevIngressBpf(%d[%s], %d) failure: %s", tracker.ifIndex,
417 tracker.iface, isEthernet, strerror(-rv));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700418 }
419 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex);
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800420 if (rv) {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700421 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
422 strerror(-rv));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800423 }
Hungming Chene55f45d2020-02-07 15:10:52 +0800424 rv = tcFilterDelDevEgressClatIpv4(mNetlinkFd, tracker.v4ifIndex);
425 if (rv) {
426 ALOGE("tcFilterDelDevEgressClatIpv4(%d[%s]) failure: %s", tracker.v4ifIndex,
427 tracker.v4iface, strerror(-rv));
428 }
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800429 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.v4ifIndex);
430 if (rv) {
431 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.v4ifIndex, tracker.v4iface,
432 strerror(-rv));
433 }
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800434 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900435 if (!ret.ok())
436 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800437 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900438 if (!ret.ok())
439 ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700440 return;
441 }
442
443 // success
444}
445
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800446void ClatdController::setIptablesDropRule(bool add, const char* iface, const char* pfx96Str,
447 const char* v6Str) {
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900448 std::string cmd = StringPrintf(
449 "*raw\n"
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800450 "%s %s -i %s -s %s/96 -d %s -j DROP\n"
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900451 "COMMIT\n",
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800452 (add ? "-A" : "-D"), LOCAL_RAW_PREROUTING, iface, pfx96Str, v6Str);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900453
454 iptablesRestoreFunction(V6, cmd);
455}
456
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700457void ClatdController::maybeStopBpf(const ClatdTracker& tracker) {
458 if (mClatEbpfMode == ClatEbpfDisabled) return;
459
Hungming Chene55f45d2020-02-07 15:10:52 +0800460 int rv = tcFilterDelDevIngressClatIpv6(mNetlinkFd, tracker.ifIndex);
461 if (rv < 0) {
462 ALOGE("tcFilterDelDevIngressClatIpv6(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
463 strerror(-rv));
464 }
465
466 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex);
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800467 if (rv < 0) {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700468 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
469 strerror(-rv));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800470 }
471
Hungming Chene55f45d2020-02-07 15:10:52 +0800472 rv = tcFilterDelDevEgressClatIpv4(mNetlinkFd, tracker.v4ifIndex);
473 if (rv < 0) {
474 ALOGE("tcFilterDelDevEgressClatIpv4(%d[%s]) failure: %s", tracker.v4ifIndex,
475 tracker.v4iface, strerror(-rv));
476 }
477
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800478 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.v4ifIndex);
479 if (rv < 0) {
480 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.v4ifIndex, tracker.v4iface,
481 strerror(-rv));
482 }
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700483
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800484 // We cleanup the maps last, so scanning through them can be used to
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700485 // determine what still needs cleanup.
486
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800487 ClatEgressKey txKey = {
488 .iif = tracker.v4ifIndex,
489 .local4 = tracker.v4,
490 };
491
492 auto ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900493 if (!ret.ok()) ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800494
495 ClatIngressKey rxKey = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700496 .iif = tracker.ifIndex,
497 .pfx96 = tracker.pfx96,
498 .local6 = tracker.v6,
499 };
500
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800501 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900502 if (!ret.ok()) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700503}
504
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900505// Finds the tracker of the clatd running on interface |interface|, or nullptr if clatd has not been
506// started on |interface|.
507ClatdController::ClatdTracker* ClatdController::getClatdTracker(const std::string& interface) {
508 auto it = mClatdTrackers.find(interface);
509 return (it == mClatdTrackers.end() ? nullptr : &it->second);
510}
511
512// Initializes a ClatdTracker for the specified interface.
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700513int ClatdController::ClatdTracker::init(unsigned networkId, const std::string& interface,
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700514 const std::string& v4interface,
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900515 const std::string& nat64Prefix) {
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700516 netId = networkId;
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900517
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900518 fwmark.netId = netId;
519 fwmark.explicitlySelected = true;
520 fwmark.protectedFromVpn = true;
521 fwmark.permission = PERMISSION_SYSTEM;
522
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900523 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900524 snprintf(netIdString, sizeof(netIdString), "%u", netId);
525 strlcpy(iface, interface.c_str(), sizeof(iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700526 ifIndex = if_nametoindex(iface);
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700527 strlcpy(v4iface, v4interface.c_str(), sizeof(v4iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700528 v4ifIndex = if_nametoindex(v4iface);
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900529
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900530 // Pass in everything that clatd needs: interface, a netid to use for DNS lookups, a fwmark for
531 // outgoing packets, the NAT64 prefix, and the IPv4 and IPv6 addresses.
532 // Validate the prefix and strip off the prefix length.
533 uint8_t family;
534 uint8_t prefixLen;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700535 int res = parsePrefix(nat64Prefix.c_str(), &family, &pfx96, sizeof(pfx96), &prefixLen);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900536 // clatd only supports /96 prefixes.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700537 if (res != sizeof(pfx96)) return res;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900538 if (family != AF_INET6) return -EAFNOSUPPORT;
539 if (prefixLen != 96) return -EINVAL;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700540 if (!inet_ntop(AF_INET6, &pfx96, pfx96String, sizeof(pfx96String))) return -errno;
Luke Huang6d301232018-08-01 14:05:18 +0800541
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900542 // Pick an IPv4 address.
543 // TODO: this picks the address based on other addresses that are assigned to interfaces, but
544 // the address is only actually assigned to an interface once clatd starts up. So we could end
545 // up with two clatd instances with the same IPv4 address.
546 // Stop doing this and instead pick a free one from the kV4Addr pool.
Maciej Żenczykowski55cacfb2019-03-30 02:01:35 -0700547 v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900548 if (v4.s_addr == INADDR_NONE) {
549 ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen);
550 return -EADDRNOTAVAIL;
551 }
552 if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno;
553
554 // Generate a checksum-neutral IID.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700555 if (generateIpv6Address(iface, v4, pfx96, &v6)) {
556 ALOGE("Unable to find global source address on %s for %s", iface, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900557 return -EADDRNOTAVAIL;
558 }
559 if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno;
560
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700561 ALOGD("starting clatd on %s v4=%s v6=%s pfx96=%s", iface, v4Str, v6Str, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900562 return 0;
563}
564
565int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix,
566 std::string* v6Str) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700567 std::lock_guard guard(mutex);
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700568
569 // 1. fail if pre-existing tracker already exists
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900570 ClatdTracker* existing = getClatdTracker(interface);
571 if (existing != nullptr) {
572 ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700573 return -EBUSY;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900574 }
575
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700576 // 2. get network id associated with this external interface
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700577 unsigned networkId = mNetCtrl->getNetworkForInterface(interface.c_str());
578 if (networkId == NETID_UNSET) {
579 ALOGE("Interface %s not assigned to any netId", interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700580 return -ENODEV;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900581 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700582
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700583 // 3. open the tun device in non blocking mode as required by clatd
Maciej Żenczykowski94db6582020-01-27 21:47:06 -0800584 int res = open("/dev/net/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC);
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700585 if (res == -1) {
586 res = errno;
587 ALOGE("open of tun device failed (%s)", strerror(res));
588 return -res;
589 }
590 unique_fd tmpTunFd(res);
591
592 // 4. create the v4-... tun interface
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700593 std::string v4interface("v4-");
594 v4interface += interface;
595
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700596 struct ifreq ifr = {
597 .ifr_flags = IFF_TUN,
598 };
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700599 strlcpy(ifr.ifr_name, v4interface.c_str(), sizeof(ifr.ifr_name));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700600
601 res = ioctl(tmpTunFd, TUNSETIFF, &ifr, sizeof(ifr));
602 if (res == -1) {
603 res = errno;
604 ALOGE("ioctl(TUNSETIFF) failed (%s)", strerror(res));
605 return -res;
606 }
607
Maciej Żenczykowskia1699952019-05-11 17:07:44 -0700608 // disable IPv6 on it - failing to do so is not a critical error
609 res = InterfaceController::setEnableIPv6(v4interface.c_str(), 0);
610 if (res) ALOGE("setEnableIPv6 %s failed (%s)", v4interface.c_str(), strerror(res));
611
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700612 // 5. initialize tracker object
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700613 ClatdTracker tracker;
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700614 int ret = tracker.init(networkId, interface, v4interface, nat64Prefix);
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700615 if (ret) return ret;
616
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700617 // 6. create a throwaway socket to reserve a file descriptor number
618 res = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
619 if (res == -1) {
620 res = errno;
621 ALOGE("socket(ipv6/udp) failed (%s)", strerror(res));
622 return -res;
623 }
624 unique_fd passedTunFd(res);
625
626 // 7. this is the FD we'll pass to clatd on the cli, so need it as a string
627 char passedTunFdStr[INT32_STRLEN];
628 snprintf(passedTunFdStr, sizeof(passedTunFdStr), "%d", passedTunFd.get());
629
630 // 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 +0900631 std::string progname("clatd-");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900632 progname += tracker.iface;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500633
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900634 // clang-format off
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700635 const char* args[] = {progname.c_str(),
636 "-i", tracker.iface,
637 "-n", tracker.netIdString,
638 "-m", tracker.fwmarkString,
639 "-p", tracker.pfx96String,
640 "-4", tracker.v4Str,
641 "-6", tracker.v6Str,
642 "-t", passedTunFdStr,
643 nullptr};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900644 // clang-format on
645
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700646 // 9. register vfork requirement
647 posix_spawnattr_t attr;
648 res = posix_spawnattr_init(&attr);
649 if (res) {
650 ALOGE("posix_spawnattr_init failed (%s)", strerror(res));
651 return -res;
652 }
653 const android::base::ScopeGuard attrGuard = [&] { posix_spawnattr_destroy(&attr); };
654 res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_USEVFORK);
655 if (res) {
656 ALOGE("posix_spawnattr_setflags failed (%s)", strerror(res));
657 return -res;
658 }
659
660 // 10. register dup2() action: this is what 'clears' the CLOEXEC flag
661 // on the tun fd that we want the child clatd process to inherit
662 // (this will happen after the vfork, and before the execve)
663 posix_spawn_file_actions_t fa;
664 res = posix_spawn_file_actions_init(&fa);
665 if (res) {
666 ALOGE("posix_spawn_file_actions_init failed (%s)", strerror(res));
667 return -res;
668 }
669 const android::base::ScopeGuard faGuard = [&] { posix_spawn_file_actions_destroy(&fa); };
670 res = posix_spawn_file_actions_adddup2(&fa, tmpTunFd, passedTunFd);
671 if (res) {
672 ALOGE("posix_spawn_file_actions_adddup2 failed (%s)", strerror(res));
673 return -res;
674 }
675
Maciej Żenczykowski083688f2019-12-23 14:43:09 -0800676 // 11. add the drop rule for iptables.
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800677 setIptablesDropRule(true, tracker.iface, tracker.pfx96String, tracker.v6Str);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900678
679 // 12. actually perform vfork/dup2/execve
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700680 res = posix_spawn(&tracker.pid, kClatdPath, &fa, &attr, (char* const*)args, nullptr);
Luke Huang40962442019-02-26 11:46:10 +0800681 if (res) {
682 ALOGE("posix_spawn failed (%s)", strerror(res));
Luke Huang6d301232018-08-01 14:05:18 +0800683 return -res;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500684 }
685
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900686 // 13. configure eBPF offload - if possible
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700687 maybeStartBpf(tracker);
688
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900689 mClatdTrackers[interface] = tracker;
690 ALOGD("clatd started on %s", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500691
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900692 *v6Str = tracker.v6Str;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500693 return 0;
694}
695
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900696int ClatdController::stopClatd(const std::string& interface) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700697 std::lock_guard guard(mutex);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900698 ClatdTracker* tracker = getClatdTracker(interface);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900699
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900700 if (tracker == nullptr) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500701 ALOGE("clatd already stopped");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900702 return -ENODEV;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500703 }
704
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900705 ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500706
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700707 maybeStopBpf(*tracker);
708
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900709 kill(tracker->pid, SIGTERM);
710 waitpid(tracker->pid, nullptr, 0);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900711
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800712 setIptablesDropRule(false, tracker->iface, tracker->pfx96String, tracker->v6Str);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900713 mClatdTrackers.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500714
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900715 ALOGD("clatd on %s stopped", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500716
717 return 0;
718}
719
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800720void ClatdController::dumpEgress(DumpWriter& dw) {
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800721 if (!mClatEgressMap.isValid()) return; // if unsupported just don't dump anything
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800722
723 ScopedIndent bpfIndent(dw);
724 dw.println("BPF egress map: iif(iface) v4Addr -> v6Addr nat64Prefix oif(iface)");
725
726 ScopedIndent bpfDetailIndent(dw);
727 const auto printClatMap = [&dw](const ClatEgressKey& key, const ClatEgressValue& value,
728 const BpfMap<ClatEgressKey, ClatEgressValue>&) {
729 char iifStr[IFNAMSIZ] = "?";
730 char local4Str[INET_ADDRSTRLEN] = "?";
731 char local6Str[INET6_ADDRSTRLEN] = "?";
732 char pfx96Str[INET6_ADDRSTRLEN] = "?";
733 char oifStr[IFNAMSIZ] = "?";
734
735 if_indextoname(key.iif, iifStr);
736 inet_ntop(AF_INET, &key.local4, local4Str, sizeof(local4Str));
737 inet_ntop(AF_INET6, &value.local6, local6Str, sizeof(local6Str));
738 inet_ntop(AF_INET6, &value.pfx96, pfx96Str, sizeof(pfx96Str));
739 if_indextoname(value.oif, oifStr);
740
Maciej Żenczykowski97ec1d82019-12-17 12:11:21 -0800741 dw.println("%u(%s) %s -> %s %s/96 %u(%s) %s", key.iif, iifStr, local4Str, local6Str,
742 pfx96Str, value.oif, oifStr, value.oifIsEthernet ? "ether" : "rawip");
Steven Morelanda3074542020-01-13 14:13:44 -0800743 return Result<void>();
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800744 };
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800745 auto res = mClatEgressMap.iterateWithValue(printClatMap);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900746 if (!res.ok()) {
Steven Morelanda3074542020-01-13 14:13:44 -0800747 dw.println("Error printing BPF map: %s", res.error().message().c_str());
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800748 }
749}
750
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800751void ClatdController::dumpIngress(DumpWriter& dw) {
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800752 if (!mClatIngressMap.isValid()) return; // if unsupported just don't dump anything
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700753
754 ScopedIndent bpfIndent(dw);
755 dw.println("BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)");
756
757 ScopedIndent bpfDetailIndent(dw);
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700758 const auto printClatMap = [&dw](const ClatIngressKey& key, const ClatIngressValue& value,
759 const BpfMap<ClatIngressKey, ClatIngressValue>&) {
760 char iifStr[IFNAMSIZ] = "?";
761 char pfx96Str[INET6_ADDRSTRLEN] = "?";
762 char local6Str[INET6_ADDRSTRLEN] = "?";
763 char local4Str[INET_ADDRSTRLEN] = "?";
764 char oifStr[IFNAMSIZ] = "?";
765
766 if_indextoname(key.iif, iifStr);
767 inet_ntop(AF_INET6, &key.pfx96, pfx96Str, sizeof(pfx96Str));
768 inet_ntop(AF_INET6, &key.local6, local6Str, sizeof(local6Str));
769 inet_ntop(AF_INET, &value.local4, local4Str, sizeof(local4Str));
770 if_indextoname(value.oif, oifStr);
771
772 dw.println("%u(%s) %s/96 %s -> %s %u(%s)", key.iif, iifStr, pfx96Str, local6Str, local4Str,
773 value.oif, oifStr);
Steven Morelanda3074542020-01-13 14:13:44 -0800774 return Result<void>();
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700775 };
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800776 auto res = mClatIngressMap.iterateWithValue(printClatMap);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900777 if (!res.ok()) {
Steven Morelanda3074542020-01-13 14:13:44 -0800778 dw.println("Error printing BPF map: %s", res.error().message().c_str());
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700779 }
780}
781
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800782void ClatdController::dumpTrackers(DumpWriter& dw) {
783 ScopedIndent trackerIndent(dw);
784 dw.println("Trackers: iif[iface] nat64Prefix v6Addr -> v4Addr v4iif[v4iface] [netId]");
785
786 ScopedIndent trackerDetailIndent(dw);
787 for (const auto& pair : mClatdTrackers) {
788 const ClatdTracker& tracker = pair.second;
789 dw.println("%u[%s] %s/96 %s -> %s %u[%s] [%u]", tracker.ifIndex, tracker.iface,
790 tracker.pfx96String, tracker.v6Str, tracker.v4Str, tracker.v4ifIndex,
791 tracker.v4iface, tracker.netId);
792 }
793}
794
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800795void ClatdController::dump(DumpWriter& dw) {
796 std::lock_guard guard(mutex);
797
798 ScopedIndent clatdIndent(dw);
799 dw.println("ClatdController");
800
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800801 dumpTrackers(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800802 dumpIngress(dw);
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800803 dumpEgress(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800804}
805
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900806auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree;
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900807auto ClatdController::iptablesRestoreFunction = execIptablesRestore;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900808
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900809} // namespace net
810} // namespace android