blob: 0e8fdb8d76a7a73c4b087e7fb4fd6900fd23f808 [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 Żenczykowskia8ef6f92019-12-16 15:53:36 -080073void ClatdController::resetEgressMap() {
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080074 const auto del = [](const ClatEgressKey& key, const BpfMap<ClatEgressKey, ClatEgressValue>&) {
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080075 ALOGW("Removing stale clat config on interface %d.", key.iif);
Hungming Chen7f725432020-02-07 17:47:23 +080076 int rv = tcFilterDelDevEgressClatIpv4(key.iif);
77 if (rv < 0) ALOGE("tcFilterDelDevEgressClatIpv4() failure: %s", strerror(-rv));
Steven Morelanda3074542020-01-13 14:13:44 -080078 return Result<void>(); // keep on going regardless
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080079 };
80 auto ret = mClatEgressMap.iterate(del);
Bernie Innocenti615fd742020-02-06 03:55:09 +090081 if (!ret.ok()) ALOGE("mClatEgressMap.iterate() failure: %s", strerror(ret.error().code()));
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080082 ret = mClatEgressMap.clear();
Bernie Innocenti615fd742020-02-06 03:55:09 +090083 if (!ret.ok()) ALOGE("mClatEgressMap.clear() failure: %s", strerror(ret.error().code()));
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -080084}
85
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -080086void ClatdController::resetIngressMap() {
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -080087 const auto del = [](const ClatIngressKey& key,
88 const BpfMap<ClatIngressKey, ClatIngressValue>&) {
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -080089 ALOGW("Removing stale clat config on interface %d.", key.iif);
Hungming Chen7f725432020-02-07 17:47:23 +080090 int rv = tcFilterDelDevIngressClatIpv6(key.iif);
91 if (rv < 0) ALOGE("tcFilterDelDevIngressClatIpv6() failure: %s", strerror(-rv));
Steven Morelanda3074542020-01-13 14:13:44 -080092 return Result<void>(); // keep on going regardless
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -080093 };
94 auto ret = mClatIngressMap.iterate(del);
Bernie Innocenti615fd742020-02-06 03:55:09 +090095 if (!ret.ok()) ALOGE("mClatIngressMap.iterate() failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -080096 ret = mClatIngressMap.clear();
Bernie Innocenti615fd742020-02-06 03:55:09 +090097 if (!ret.ok()) ALOGE("mClatIngressMap.clear() failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -080098}
99
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700100void ClatdController::init(void) {
101 std::lock_guard guard(mutex);
102
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700103 // TODO: should refactor into separate function for testability
Maciej Żenczykowski69cd8222020-02-11 15:36:40 -0800104 if (!bpf::isBpfSupported()) {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700105 ALOGI("Pre-4.9 kernel or pre-P api shipping level - disabling clat ebpf.");
106 mClatEbpfMode = ClatEbpfDisabled;
107 return;
108 }
109
110 // We know the device initially shipped with at least P...,
111 // but did it ship with at least Q?
112
113 uint64_t api_level = base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
114 if (api_level == 0) {
115 ALOGE("Cannot determine initial API level of the device.");
116 api_level = base::GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
117 }
118
119 // Note: MINIMUM_API_REQUIRED is for eBPF as a whole and is thus P
120 if (api_level > bpf::MINIMUM_API_REQUIRED) {
121 ALOGI("4.9+ kernel and device shipped with Q+ - clat ebpf should work.");
122 mClatEbpfMode = ClatEbpfEnabled;
123 } else {
124 // We cannot guarantee that 4.9-P kernels will include NET_CLS_BPF support.
125 ALOGI("4.9+ kernel and device shipped with P - clat ebpf might work.");
126 mClatEbpfMode = ClatEbpfMaybe;
127 }
128
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800129 int rv = getClatEgressMapFd();
Maciej Żenczykowski0bd8da72019-12-16 15:16:28 -0800130 if (rv < 0) {
131 ALOGE("getClatEgressMapFd() failure: %s", strerror(-rv));
132 mClatEbpfMode = ClatEbpfDisabled;
Maciej Żenczykowski0bd8da72019-12-16 15:16:28 -0800133 return;
134 }
135 mClatEgressMap.reset(rv);
136
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700137 rv = getClatIngressMapFd();
138 if (rv < 0) {
139 ALOGE("getClatIngressMapFd() failure: %s", strerror(-rv));
140 mClatEbpfMode = ClatEbpfDisabled;
Maciej Żenczykowski0bd8da72019-12-16 15:16:28 -0800141 mClatEgressMap.reset(-1);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700142 return;
143 }
144 mClatIngressMap.reset(rv);
145
Maciej Żenczykowskia8ef6f92019-12-16 15:53:36 -0800146 resetEgressMap();
Maciej Żenczykowski35bc6ed2019-12-16 15:39:04 -0800147 resetIngressMap();
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700148}
149
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900150bool ClatdController::isIpv4AddressFree(in_addr_t addr) {
151 int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
152 if (s == -1) {
153 return 0;
154 }
155
156 // Attempt to connect to the address. If the connection succeeds and getsockname returns the
157 // same then the address is already assigned to the system and we can't use it.
Nick Desaulniers6b357502019-10-11 09:26:44 -0700158 struct sockaddr_in sin = {
159 .sin_family = AF_INET,
Maciej Żenczykowski268190e2019-10-31 23:47:53 -0700160 .sin_port = htons(53),
Nick Desaulniers6b357502019-10-11 09:26:44 -0700161 .sin_addr = {addr},
162 };
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900163 socklen_t len = sizeof(sin);
164 bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 &&
165 getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) &&
166 sin.sin_addr.s_addr == addr;
167
168 close(s);
169 return !inuse;
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900170}
Daniel Drown0da73fc2012-06-20 16:51:39 -0500171
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900172// Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order.
173// ip - the IP address from the configuration file
174// prefixlen - the length of the prefix from which addresses may be selected.
175// returns: the IPv4 address, or INADDR_NONE if no addresses were available
176in_addr_t ClatdController::selectIpv4Address(const in_addr ip, int16_t prefixlen) {
177 // Don't accept prefixes that are too large because we scan addresses one by one.
178 if (prefixlen < 16 || prefixlen > 32) {
179 return INADDR_NONE;
180 }
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900181
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900182 // All these are in host byte order.
183 in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen);
184 in_addr_t ipv4 = ntohl(ip.s_addr);
185 in_addr_t first_ipv4 = ipv4;
186 in_addr_t prefix = ipv4 & mask;
187
188 // Pick the first IPv4 address in the pool, wrapping around if necessary.
189 // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0.
190 do {
191 if (isIpv4AddressFreeFunc(htonl(ipv4))) {
192 return htonl(ipv4);
193 }
194 ipv4 = prefix | ((ipv4 + 1) & ~mask);
195 } while (ipv4 != first_ipv4);
196
197 return INADDR_NONE;
198}
199
200// Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix.
201void ClatdController::makeChecksumNeutral(in6_addr* v6, const in_addr v4,
202 const in6_addr& nat64Prefix) {
203 // Fill last 8 bytes of IPv6 address with random bits.
204 arc4random_buf(&v6->s6_addr[8], 8);
205
206 // Make the IID checksum-neutral. That is, make it so that:
207 // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6)
208 // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4):
209 // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix)
210 // Do this by adjusting the two bytes in the middle of the IID.
211
212 uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12];
213
214 uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4));
215 uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) +
216 ip_checksum_add(0, v6, sizeof(*v6));
217
218 uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2);
219 v6->s6_addr[11] = delta >> 8;
220 v6->s6_addr[12] = delta & 0xff;
221}
222
223// Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix.
224int ClatdController::generateIpv6Address(const char* iface, const in_addr v4,
225 const in6_addr& nat64Prefix, in6_addr* v6) {
226 unique_fd s(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0));
227 if (s == -1) return -errno;
228
229 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) {
Luke Huang6d301232018-08-01 14:05:18 +0800230 return -errno;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500231 }
232
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900233 sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix};
234 if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) {
235 return -errno;
236 }
237
238 socklen_t len = sizeof(sin6);
239 if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) {
240 return -errno;
241 }
242
243 *v6 = sin6.sin6_addr;
244
245 if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) ||
246 IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) {
247 return -ENETUNREACH;
248 }
249
250 makeChecksumNeutral(v6, v4, nat64Prefix);
251
252 return 0;
253}
254
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700255void ClatdController::maybeStartBpf(const ClatdTracker& tracker) {
256 if (mClatEbpfMode == ClatEbpfDisabled) return;
257
Hungming Chenec203382020-02-13 19:15:33 +0800258 auto isEthernet = android::net::isEthernet(tracker.iface);
259 if (!isEthernet.ok()) {
260 ALOGE("isEthernet(%s[%d]) failure: %s", tracker.iface, tracker.ifIndex,
261 isEthernet.error().message().c_str());
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700262 return;
263 }
264
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800265 // This program will be attached to the v4-* interface which is a TUN and thus always rawip.
Hungming Chenec203382020-02-13 19:15:33 +0800266 int rv = getClatEgressProgFd(RAWIP);
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800267 if (rv < 0) {
Maciej Żenczykowskiad730892020-02-13 18:21:32 -0800268 ALOGE("getClatEgressProgFd(RAWIP) failure: %s", strerror(-rv));
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800269 return;
270 }
271 unique_fd txRawIpProgFd(rv);
272
Hungming Chenec203382020-02-13 19:15:33 +0800273 rv = getClatIngressProgFd(isEthernet.value());
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700274 if (rv < 0) {
Hungming Chenec203382020-02-13 19:15:33 +0800275 ALOGE("getClatIngressProgFd(%d) failure: %s", isEthernet.value(), strerror(-rv));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700276 return;
277 }
Maciej Żenczykowski75f4dc32019-12-16 18:38:54 -0800278 unique_fd rxProgFd(rv);
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700279
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800280 ClatEgressKey txKey = {
281 .iif = tracker.v4ifIndex,
282 .local4 = tracker.v4,
283 };
284 ClatEgressValue txValue = {
285 .oif = tracker.ifIndex,
286 .local6 = tracker.v6,
287 .pfx96 = tracker.pfx96,
Hungming Chenec203382020-02-13 19:15:33 +0800288 .oifIsEthernet = isEthernet.value(),
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800289 };
290
291 auto ret = mClatEgressMap.writeValue(txKey, txValue, BPF_ANY);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900292 if (!ret.ok()) {
Maciej Żenczykowski4460b8c2020-01-20 03:47:41 -0800293 ALOGE("mClatEgressMap.writeValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800294 return;
295 }
296
297 ClatIngressKey rxKey = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700298 .iif = tracker.ifIndex,
299 .pfx96 = tracker.pfx96,
300 .local6 = tracker.v6,
301 };
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800302 ClatIngressValue rxValue = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700303 // TODO: move all the clat code to eBPF and remove the tun interface entirely.
Maciej Żenczykowski39b0b902019-05-08 00:36:30 -0700304 .oif = tracker.v4ifIndex,
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700305 .local4 = tracker.v4,
306 };
307
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800308 ret = mClatIngressMap.writeValue(rxKey, rxValue, BPF_ANY);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900309 if (!ret.ok()) {
Maciej Żenczykowski4460b8c2020-01-20 03:47:41 -0800310 ALOGE("mClatIngressMap.writeValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800311 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900312 if (!ret.ok())
313 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700314 return;
315 }
316
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800317 // We do tc setup *after* populating the maps, so scanning through them
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700318 // can always be used to tell us what needs cleanup.
319
Hungming Chen7f725432020-02-07 17:47:23 +0800320 // Usually the clsact will be added in RouteController::addInterfaceToPhysicalNetwork.
321 // But clat is started before the v4- interface is added to the network. The clat startup have
322 // to add clsact of v4- tun interface first for adding bpf filter in maybeStartBpf.
323 // TODO: move "qdisc add clsact" of v4- tun interface out from ClatdController.
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800324 rv = tcQdiscAddDevClsact(tracker.v4ifIndex);
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800325 if (rv) {
326 ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", tracker.v4ifIndex, tracker.v4iface,
327 strerror(-rv));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800328 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900329 if (!ret.ok())
330 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800331 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900332 if (!ret.ok())
333 ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1ca95922019-12-17 03:38:32 -0800334 return;
335 }
336
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800337 rv = tcFilterAddDevEgressClatIpv4(tracker.v4ifIndex, txRawIpProgFd, RAWIP);
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800338 if (rv) {
339 if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) {
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800340 ALOGI("tcFilterAddDevEgressClatIpv4(%d[%s], RAWIP): %s", tracker.v4ifIndex,
341 tracker.v4iface, strerror(-rv));
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800342 } else {
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800343 ALOGE("tcFilterAddDevEgressClatIpv4(%d[%s], RAWIP) failure: %s", tracker.v4ifIndex,
Maciej Żenczykowskife929612019-12-15 13:18:05 -0800344 tracker.v4iface, strerror(-rv));
345 }
Hungming Chen7f725432020-02-07 17:47:23 +0800346
347 // The v4- interface clsact is not deleted for unwinding error because once it is created
348 // with interface addition, the lifetime is till interface deletion. Moreover, the clsact
349 // has no clat filter now. It should not break anything.
350
Maciej Żenczykowskife929612019-12-15 13:18:05 -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 Żenczykowskife929612019-12-15 13:18:05 -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 Żenczykowskife929612019-12-15 13:18:05 -0800357 return;
358 }
359
Hungming Chenec203382020-02-13 19:15:33 +0800360 rv = tcFilterAddDevIngressClatIpv6(tracker.ifIndex, rxProgFd, isEthernet.value());
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700361 if (rv) {
362 if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) {
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800363 ALOGI("tcFilterAddDevIngressClatIpv6(%d[%s], %d): %s", tracker.ifIndex, tracker.iface,
Hungming Chenec203382020-02-13 19:15:33 +0800364 isEthernet.value(), strerror(-rv));
Maciej Żenczykowskib140a3a2019-12-15 11:53:46 -0800365 } else {
Maciej Żenczykowski33163822020-02-16 17:12:41 -0800366 ALOGE("tcFilterAddDevIngressClatIpv6(%d[%s], %d) failure: %s", tracker.ifIndex,
Hungming Chenec203382020-02-13 19:15:33 +0800367 tracker.iface, isEthernet.value(), strerror(-rv));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700368 }
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800369 rv = tcFilterDelDevEgressClatIpv4(tracker.v4ifIndex);
Hungming Chene55f45d2020-02-07 15:10:52 +0800370 if (rv) {
371 ALOGE("tcFilterDelDevEgressClatIpv4(%d[%s]) failure: %s", tracker.v4ifIndex,
372 tracker.v4iface, strerror(-rv));
373 }
Hungming Chen7f725432020-02-07 17:47:23 +0800374
375 // The v4- interface clsact is not deleted. See the reason in the error unwinding code of
376 // the egress filter attaching of v4- tun interface.
377
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800378 ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900379 if (!ret.ok())
380 ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800381 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900382 if (!ret.ok())
383 ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700384 return;
385 }
386
387 // success
388}
389
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800390void ClatdController::setIptablesDropRule(bool add, const char* iface, const char* pfx96Str,
391 const char* v6Str) {
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900392 std::string cmd = StringPrintf(
393 "*raw\n"
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800394 "%s %s -i %s -s %s/96 -d %s -j DROP\n"
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900395 "COMMIT\n",
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800396 (add ? "-A" : "-D"), LOCAL_RAW_PREROUTING, iface, pfx96Str, v6Str);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900397
398 iptablesRestoreFunction(V6, cmd);
399}
400
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700401void ClatdController::maybeStopBpf(const ClatdTracker& tracker) {
402 if (mClatEbpfMode == ClatEbpfDisabled) return;
403
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800404 int rv = tcFilterDelDevIngressClatIpv6(tracker.ifIndex);
Hungming Chene55f45d2020-02-07 15:10:52 +0800405 if (rv < 0) {
406 ALOGE("tcFilterDelDevIngressClatIpv6(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
407 strerror(-rv));
408 }
409
Maciej Żenczykowskif89e9112020-02-12 05:22:56 -0800410 rv = tcFilterDelDevEgressClatIpv4(tracker.v4ifIndex);
Hungming Chene55f45d2020-02-07 15:10:52 +0800411 if (rv < 0) {
412 ALOGE("tcFilterDelDevEgressClatIpv4(%d[%s]) failure: %s", tracker.v4ifIndex,
413 tracker.v4iface, strerror(-rv));
414 }
415
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800416 // We cleanup the maps last, so scanning through them can be used to
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700417 // determine what still needs cleanup.
418
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800419 ClatEgressKey txKey = {
420 .iif = tracker.v4ifIndex,
421 .local4 = tracker.v4,
422 };
423
424 auto ret = mClatEgressMap.deleteValue(txKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900425 if (!ret.ok()) ALOGE("mClatEgressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800426
427 ClatIngressKey rxKey = {
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700428 .iif = tracker.ifIndex,
429 .pfx96 = tracker.pfx96,
430 .local6 = tracker.v6,
431 };
432
Maciej Żenczykowski869bca52019-12-16 18:23:21 -0800433 ret = mClatIngressMap.deleteValue(rxKey);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900434 if (!ret.ok()) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.error().code()));
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700435}
436
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900437// Finds the tracker of the clatd running on interface |interface|, or nullptr if clatd has not been
438// started on |interface|.
439ClatdController::ClatdTracker* ClatdController::getClatdTracker(const std::string& interface) {
440 auto it = mClatdTrackers.find(interface);
441 return (it == mClatdTrackers.end() ? nullptr : &it->second);
442}
443
444// Initializes a ClatdTracker for the specified interface.
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700445int ClatdController::ClatdTracker::init(unsigned networkId, const std::string& interface,
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700446 const std::string& v4interface,
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900447 const std::string& nat64Prefix) {
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700448 netId = networkId;
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900449
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900450 fwmark.netId = netId;
451 fwmark.explicitlySelected = true;
452 fwmark.protectedFromVpn = true;
453 fwmark.permission = PERMISSION_SYSTEM;
454
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900455 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900456 snprintf(netIdString, sizeof(netIdString), "%u", netId);
457 strlcpy(iface, interface.c_str(), sizeof(iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700458 ifIndex = if_nametoindex(iface);
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700459 strlcpy(v4iface, v4interface.c_str(), sizeof(v4iface));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700460 v4ifIndex = if_nametoindex(v4iface);
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900461
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900462 // Pass in everything that clatd needs: interface, a netid to use for DNS lookups, a fwmark for
463 // outgoing packets, the NAT64 prefix, and the IPv4 and IPv6 addresses.
464 // Validate the prefix and strip off the prefix length.
465 uint8_t family;
466 uint8_t prefixLen;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700467 int res = parsePrefix(nat64Prefix.c_str(), &family, &pfx96, sizeof(pfx96), &prefixLen);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900468 // clatd only supports /96 prefixes.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700469 if (res != sizeof(pfx96)) return res;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900470 if (family != AF_INET6) return -EAFNOSUPPORT;
471 if (prefixLen != 96) return -EINVAL;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700472 if (!inet_ntop(AF_INET6, &pfx96, pfx96String, sizeof(pfx96String))) return -errno;
Luke Huang6d301232018-08-01 14:05:18 +0800473
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900474 // Pick an IPv4 address.
475 // TODO: this picks the address based on other addresses that are assigned to interfaces, but
476 // the address is only actually assigned to an interface once clatd starts up. So we could end
477 // up with two clatd instances with the same IPv4 address.
478 // Stop doing this and instead pick a free one from the kV4Addr pool.
Maciej Żenczykowski55cacfb2019-03-30 02:01:35 -0700479 v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900480 if (v4.s_addr == INADDR_NONE) {
481 ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen);
482 return -EADDRNOTAVAIL;
483 }
484 if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno;
485
486 // Generate a checksum-neutral IID.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700487 if (generateIpv6Address(iface, v4, pfx96, &v6)) {
488 ALOGE("Unable to find global source address on %s for %s", iface, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900489 return -EADDRNOTAVAIL;
490 }
491 if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno;
492
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700493 ALOGD("starting clatd on %s v4=%s v6=%s pfx96=%s", iface, v4Str, v6Str, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900494 return 0;
495}
496
497int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix,
498 std::string* v6Str) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700499 std::lock_guard guard(mutex);
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700500
501 // 1. fail if pre-existing tracker already exists
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900502 ClatdTracker* existing = getClatdTracker(interface);
503 if (existing != nullptr) {
504 ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700505 return -EBUSY;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900506 }
507
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700508 // 2. get network id associated with this external interface
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700509 unsigned networkId = mNetCtrl->getNetworkForInterface(interface.c_str());
510 if (networkId == NETID_UNSET) {
511 ALOGE("Interface %s not assigned to any netId", interface.c_str());
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700512 return -ENODEV;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900513 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700514
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700515 // 3. open the tun device in non blocking mode as required by clatd
Maciej Żenczykowski94db6582020-01-27 21:47:06 -0800516 int res = open("/dev/net/tun", O_RDWR | O_NONBLOCK | O_CLOEXEC);
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700517 if (res == -1) {
518 res = errno;
519 ALOGE("open of tun device failed (%s)", strerror(res));
520 return -res;
521 }
522 unique_fd tmpTunFd(res);
523
524 // 4. create the v4-... tun interface
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700525 std::string v4interface("v4-");
526 v4interface += interface;
527
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700528 struct ifreq ifr = {
529 .ifr_flags = IFF_TUN,
530 };
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700531 strlcpy(ifr.ifr_name, v4interface.c_str(), sizeof(ifr.ifr_name));
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700532
533 res = ioctl(tmpTunFd, TUNSETIFF, &ifr, sizeof(ifr));
534 if (res == -1) {
535 res = errno;
536 ALOGE("ioctl(TUNSETIFF) failed (%s)", strerror(res));
537 return -res;
538 }
539
Maciej Żenczykowskia1699952019-05-11 17:07:44 -0700540 // disable IPv6 on it - failing to do so is not a critical error
541 res = InterfaceController::setEnableIPv6(v4interface.c_str(), 0);
542 if (res) ALOGE("setEnableIPv6 %s failed (%s)", v4interface.c_str(), strerror(res));
543
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700544 // 5. initialize tracker object
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700545 ClatdTracker tracker;
Maciej Żenczykowski4657e512019-05-08 06:35:08 -0700546 int ret = tracker.init(networkId, interface, v4interface, nat64Prefix);
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700547 if (ret) return ret;
548
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700549 // 6. create a throwaway socket to reserve a file descriptor number
550 res = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
551 if (res == -1) {
552 res = errno;
553 ALOGE("socket(ipv6/udp) failed (%s)", strerror(res));
554 return -res;
555 }
556 unique_fd passedTunFd(res);
557
558 // 7. this is the FD we'll pass to clatd on the cli, so need it as a string
559 char passedTunFdStr[INT32_STRLEN];
560 snprintf(passedTunFdStr, sizeof(passedTunFdStr), "%d", passedTunFd.get());
561
562 // 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 +0900563 std::string progname("clatd-");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900564 progname += tracker.iface;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500565
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900566 // clang-format off
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700567 const char* args[] = {progname.c_str(),
568 "-i", tracker.iface,
569 "-n", tracker.netIdString,
570 "-m", tracker.fwmarkString,
571 "-p", tracker.pfx96String,
572 "-4", tracker.v4Str,
573 "-6", tracker.v6Str,
574 "-t", passedTunFdStr,
575 nullptr};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900576 // clang-format on
577
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700578 // 9. register vfork requirement
579 posix_spawnattr_t attr;
580 res = posix_spawnattr_init(&attr);
581 if (res) {
582 ALOGE("posix_spawnattr_init failed (%s)", strerror(res));
583 return -res;
584 }
585 const android::base::ScopeGuard attrGuard = [&] { posix_spawnattr_destroy(&attr); };
586 res = posix_spawnattr_setflags(&attr, POSIX_SPAWN_USEVFORK);
587 if (res) {
588 ALOGE("posix_spawnattr_setflags failed (%s)", strerror(res));
589 return -res;
590 }
591
592 // 10. register dup2() action: this is what 'clears' the CLOEXEC flag
593 // on the tun fd that we want the child clatd process to inherit
594 // (this will happen after the vfork, and before the execve)
595 posix_spawn_file_actions_t fa;
596 res = posix_spawn_file_actions_init(&fa);
597 if (res) {
598 ALOGE("posix_spawn_file_actions_init failed (%s)", strerror(res));
599 return -res;
600 }
601 const android::base::ScopeGuard faGuard = [&] { posix_spawn_file_actions_destroy(&fa); };
602 res = posix_spawn_file_actions_adddup2(&fa, tmpTunFd, passedTunFd);
603 if (res) {
604 ALOGE("posix_spawn_file_actions_adddup2 failed (%s)", strerror(res));
605 return -res;
606 }
607
Maciej Żenczykowski083688f2019-12-23 14:43:09 -0800608 // 11. add the drop rule for iptables.
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800609 setIptablesDropRule(true, tracker.iface, tracker.pfx96String, tracker.v6Str);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900610
611 // 12. actually perform vfork/dup2/execve
Maciej Żenczykowskif4b44fe2019-04-08 16:18:50 -0700612 res = posix_spawn(&tracker.pid, kClatdPath, &fa, &attr, (char* const*)args, nullptr);
Luke Huang40962442019-02-26 11:46:10 +0800613 if (res) {
614 ALOGE("posix_spawn failed (%s)", strerror(res));
Luke Huang6d301232018-08-01 14:05:18 +0800615 return -res;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500616 }
617
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900618 // 13. configure eBPF offload - if possible
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700619 maybeStartBpf(tracker);
620
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900621 mClatdTrackers[interface] = tracker;
622 ALOGD("clatd started on %s", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500623
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900624 *v6Str = tracker.v6Str;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500625 return 0;
626}
627
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900628int ClatdController::stopClatd(const std::string& interface) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700629 std::lock_guard guard(mutex);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900630 ClatdTracker* tracker = getClatdTracker(interface);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900631
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900632 if (tracker == nullptr) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500633 ALOGE("clatd already stopped");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900634 return -ENODEV;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500635 }
636
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900637 ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500638
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700639 maybeStopBpf(*tracker);
640
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900641 kill(tracker->pid, SIGTERM);
642 waitpid(tracker->pid, nullptr, 0);
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900643
Maciej Żenczykowskif007de62019-12-23 15:27:53 -0800644 setIptablesDropRule(false, tracker->iface, tracker->pfx96String, tracker->v6Str);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900645 mClatdTrackers.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500646
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900647 ALOGD("clatd on %s stopped", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500648
649 return 0;
650}
651
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800652void ClatdController::dumpEgress(DumpWriter& dw) {
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800653 if (!mClatEgressMap.isValid()) return; // if unsupported just don't dump anything
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800654
655 ScopedIndent bpfIndent(dw);
656 dw.println("BPF egress map: iif(iface) v4Addr -> v6Addr nat64Prefix oif(iface)");
657
658 ScopedIndent bpfDetailIndent(dw);
659 const auto printClatMap = [&dw](const ClatEgressKey& key, const ClatEgressValue& value,
660 const BpfMap<ClatEgressKey, ClatEgressValue>&) {
661 char iifStr[IFNAMSIZ] = "?";
662 char local4Str[INET_ADDRSTRLEN] = "?";
663 char local6Str[INET6_ADDRSTRLEN] = "?";
664 char pfx96Str[INET6_ADDRSTRLEN] = "?";
665 char oifStr[IFNAMSIZ] = "?";
666
667 if_indextoname(key.iif, iifStr);
668 inet_ntop(AF_INET, &key.local4, local4Str, sizeof(local4Str));
669 inet_ntop(AF_INET6, &value.local6, local6Str, sizeof(local6Str));
670 inet_ntop(AF_INET6, &value.pfx96, pfx96Str, sizeof(pfx96Str));
671 if_indextoname(value.oif, oifStr);
672
Maciej Żenczykowski97ec1d82019-12-17 12:11:21 -0800673 dw.println("%u(%s) %s -> %s %s/96 %u(%s) %s", key.iif, iifStr, local4Str, local6Str,
674 pfx96Str, value.oif, oifStr, value.oifIsEthernet ? "ether" : "rawip");
Steven Morelanda3074542020-01-13 14:13:44 -0800675 return Result<void>();
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800676 };
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800677 auto res = mClatEgressMap.iterateWithValue(printClatMap);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900678 if (!res.ok()) {
Steven Morelanda3074542020-01-13 14:13:44 -0800679 dw.println("Error printing BPF map: %s", res.error().message().c_str());
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800680 }
681}
682
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800683void ClatdController::dumpIngress(DumpWriter& dw) {
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800684 if (!mClatIngressMap.isValid()) return; // if unsupported just don't dump anything
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700685
686 ScopedIndent bpfIndent(dw);
687 dw.println("BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)");
688
689 ScopedIndent bpfDetailIndent(dw);
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700690 const auto printClatMap = [&dw](const ClatIngressKey& key, const ClatIngressValue& value,
691 const BpfMap<ClatIngressKey, ClatIngressValue>&) {
692 char iifStr[IFNAMSIZ] = "?";
693 char pfx96Str[INET6_ADDRSTRLEN] = "?";
694 char local6Str[INET6_ADDRSTRLEN] = "?";
695 char local4Str[INET_ADDRSTRLEN] = "?";
696 char oifStr[IFNAMSIZ] = "?";
697
698 if_indextoname(key.iif, iifStr);
699 inet_ntop(AF_INET6, &key.pfx96, pfx96Str, sizeof(pfx96Str));
700 inet_ntop(AF_INET6, &key.local6, local6Str, sizeof(local6Str));
701 inet_ntop(AF_INET, &value.local4, local4Str, sizeof(local4Str));
702 if_indextoname(value.oif, oifStr);
703
704 dw.println("%u(%s) %s/96 %s -> %s %u(%s)", key.iif, iifStr, pfx96Str, local6Str, local4Str,
705 value.oif, oifStr);
Steven Morelanda3074542020-01-13 14:13:44 -0800706 return Result<void>();
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700707 };
Maciej Żenczykowski674cd992020-01-20 03:48:35 -0800708 auto res = mClatIngressMap.iterateWithValue(printClatMap);
Bernie Innocenti615fd742020-02-06 03:55:09 +0900709 if (!res.ok()) {
Steven Morelanda3074542020-01-13 14:13:44 -0800710 dw.println("Error printing BPF map: %s", res.error().message().c_str());
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700711 }
712}
713
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800714void ClatdController::dumpTrackers(DumpWriter& dw) {
715 ScopedIndent trackerIndent(dw);
716 dw.println("Trackers: iif[iface] nat64Prefix v6Addr -> v4Addr v4iif[v4iface] [netId]");
717
718 ScopedIndent trackerDetailIndent(dw);
719 for (const auto& pair : mClatdTrackers) {
720 const ClatdTracker& tracker = pair.second;
721 dw.println("%u[%s] %s/96 %s -> %s %u[%s] [%u]", tracker.ifIndex, tracker.iface,
722 tracker.pfx96String, tracker.v6Str, tracker.v4Str, tracker.v4ifIndex,
723 tracker.v4iface, tracker.netId);
724 }
725}
726
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800727void ClatdController::dump(DumpWriter& dw) {
728 std::lock_guard guard(mutex);
729
730 ScopedIndent clatdIndent(dw);
731 dw.println("ClatdController");
732
Maciej Żenczykowski4c262172019-12-16 11:31:24 -0800733 dumpTrackers(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800734 dumpIngress(dw);
Maciej Żenczykowski1afbd992019-12-16 11:44:14 -0800735 dumpEgress(dw);
Maciej Żenczykowski7dffa6f2019-12-16 11:20:44 -0800736}
737
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900738auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree;
Lorenzo Colitti91fd5802019-06-28 19:22:01 +0900739auto ClatdController::iptablesRestoreFunction = execIptablesRestore;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900740
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900741} // namespace net
742} // namespace android