blob: 77eaf5a9dd3fc3f8f3d5636835402b9998c73b42 [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 Żenczykowskic8c38aa2019-03-29 01:24:51 -070023#include <net/if.h>
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090024#include <netinet/in.h>
Luke Huang94c43a12019-02-14 19:51:38 +080025#include <spawn.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050026#include <sys/types.h>
27#include <sys/wait.h>
Luke Huang94c43a12019-02-14 19:51:38 +080028#include <unistd.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050029
30#define LOG_TAG "ClatdController"
Logan Chien3f461482018-04-23 14:31:32 +080031#include <log/log.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050032
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -070033#include "ClatdController.h"
34
35#include "android-base/properties.h"
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090036#include "android-base/unique_fd.h"
Maciej Żenczykowski55262712019-03-29 23:44:56 -070037#include "bpf/BpfMap.h"
38#include "netdbpf/bpf_shared.h"
39#include "netdutils/DumpWriter.h"
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090040
41extern "C" {
42#include "netutils/checksum.h"
43}
44
Maciej Żenczykowski55262712019-03-29 23:44:56 -070045#include "ClatUtils.h"
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090046#include "Fwmark.h"
Paul Jensen84c1d032014-05-30 13:29:41 -040047#include "NetdConstants.h"
48#include "NetworkController.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090049#include "netid_client.h"
Daniel Drown0da73fc2012-06-20 16:51:39 -050050
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090051static const char* kClatdPath = "/system/bin/clatd";
52
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090053// For historical reasons, start with 192.0.0.4, and after that, use all subsequent addresses in
54// 192.0.0.0/29 (RFC 7335).
55static const char* kV4AddrString = "192.0.0.4";
56static const in_addr kV4Addr = {inet_addr(kV4AddrString)};
57static const int kV4AddrLen = 29;
58
59using android::base::unique_fd;
Maciej Żenczykowski55262712019-03-29 23:44:56 -070060using android::bpf::BpfMap;
61using android::netdutils::DumpWriter;
62using android::netdutils::ScopedIndent;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090063
Lorenzo Colitti7035f222017-02-13 18:29:00 +090064namespace android {
65namespace net {
66
Maciej Żenczykowski56280272019-03-30 03:32:51 -070067void ClatdController::init(void) {
68 std::lock_guard guard(mutex);
69
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -070070 // TODO: should refactor into separate function for testability
71 if (bpf::getBpfSupportLevel() == bpf::BpfLevel::NONE) {
72 ALOGI("Pre-4.9 kernel or pre-P api shipping level - disabling clat ebpf.");
73 mClatEbpfMode = ClatEbpfDisabled;
74 return;
75 }
76
77 // We know the device initially shipped with at least P...,
78 // but did it ship with at least Q?
79
80 uint64_t api_level = base::GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
81 if (api_level == 0) {
82 ALOGE("Cannot determine initial API level of the device.");
83 api_level = base::GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
84 }
85
86 // Note: MINIMUM_API_REQUIRED is for eBPF as a whole and is thus P
87 if (api_level > bpf::MINIMUM_API_REQUIRED) {
88 ALOGI("4.9+ kernel and device shipped with Q+ - clat ebpf should work.");
89 mClatEbpfMode = ClatEbpfEnabled;
90 } else {
91 // We cannot guarantee that 4.9-P kernels will include NET_CLS_BPF support.
92 ALOGI("4.9+ kernel and device shipped with P - clat ebpf might work.");
93 mClatEbpfMode = ClatEbpfMaybe;
94 }
95
96 int rv = openNetlinkSocket();
97 if (rv < 0) {
98 ALOGE("openNetlinkSocket() failure: %s", strerror(-rv));
99 mClatEbpfMode = ClatEbpfDisabled;
100 return;
101 }
102 mNetlinkFd.reset(rv);
103
104 rv = getClatIngressMapFd();
105 if (rv < 0) {
106 ALOGE("getClatIngressMapFd() failure: %s", strerror(-rv));
107 mClatEbpfMode = ClatEbpfDisabled;
108 mNetlinkFd.reset(-1);
109 return;
110 }
111 mClatIngressMap.reset(rv);
112
113 int netlinkFd = mNetlinkFd.get();
114
115 // TODO: perhaps this initial cleanup should be in its own function?
116 const auto del = [&netlinkFd](const ClatIngressKey& key,
117 const BpfMap<ClatIngressKey, ClatIngressValue>&) {
118 ALOGW("Removing stale clat config on interface %d.", key.iif);
119 int rv = tcQdiscDelDevClsact(netlinkFd, key.iif);
120 if (rv < 0) ALOGE("tcQdiscDelDevClsact() failure: %s", strerror(-rv));
121 return netdutils::status::ok; // keep on going regardless
122 };
123 auto ret = mClatIngressMap.iterate(del);
124 if (!isOk(ret)) ALOGE("mClatIngressMap.iterate() failure: %s", strerror(ret.code()));
125 ret = mClatIngressMap.clear();
126 if (!isOk(ret)) ALOGE("mClatIngressMap.clear() failure: %s", strerror(ret.code()));
127}
128
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900129bool ClatdController::isIpv4AddressFree(in_addr_t addr) {
130 int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
131 if (s == -1) {
132 return 0;
133 }
134
135 // Attempt to connect to the address. If the connection succeeds and getsockname returns the
136 // same then the address is already assigned to the system and we can't use it.
137 struct sockaddr_in sin = {.sin_family = AF_INET, .sin_addr = {addr}, .sin_port = 53};
138 socklen_t len = sizeof(sin);
139 bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 &&
140 getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) &&
141 sin.sin_addr.s_addr == addr;
142
143 close(s);
144 return !inuse;
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900145}
Daniel Drown0da73fc2012-06-20 16:51:39 -0500146
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900147// Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order.
148// ip - the IP address from the configuration file
149// prefixlen - the length of the prefix from which addresses may be selected.
150// returns: the IPv4 address, or INADDR_NONE if no addresses were available
151in_addr_t ClatdController::selectIpv4Address(const in_addr ip, int16_t prefixlen) {
152 // Don't accept prefixes that are too large because we scan addresses one by one.
153 if (prefixlen < 16 || prefixlen > 32) {
154 return INADDR_NONE;
155 }
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900156
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900157 // All these are in host byte order.
158 in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen);
159 in_addr_t ipv4 = ntohl(ip.s_addr);
160 in_addr_t first_ipv4 = ipv4;
161 in_addr_t prefix = ipv4 & mask;
162
163 // Pick the first IPv4 address in the pool, wrapping around if necessary.
164 // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0.
165 do {
166 if (isIpv4AddressFreeFunc(htonl(ipv4))) {
167 return htonl(ipv4);
168 }
169 ipv4 = prefix | ((ipv4 + 1) & ~mask);
170 } while (ipv4 != first_ipv4);
171
172 return INADDR_NONE;
173}
174
175// Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix.
176void ClatdController::makeChecksumNeutral(in6_addr* v6, const in_addr v4,
177 const in6_addr& nat64Prefix) {
178 // Fill last 8 bytes of IPv6 address with random bits.
179 arc4random_buf(&v6->s6_addr[8], 8);
180
181 // Make the IID checksum-neutral. That is, make it so that:
182 // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6)
183 // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4):
184 // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix)
185 // Do this by adjusting the two bytes in the middle of the IID.
186
187 uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12];
188
189 uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4));
190 uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) +
191 ip_checksum_add(0, v6, sizeof(*v6));
192
193 uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2);
194 v6->s6_addr[11] = delta >> 8;
195 v6->s6_addr[12] = delta & 0xff;
196}
197
198// Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix.
199int ClatdController::generateIpv6Address(const char* iface, const in_addr v4,
200 const in6_addr& nat64Prefix, in6_addr* v6) {
201 unique_fd s(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0));
202 if (s == -1) return -errno;
203
204 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) {
Luke Huang6d301232018-08-01 14:05:18 +0800205 return -errno;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500206 }
207
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900208 sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix};
209 if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) {
210 return -errno;
211 }
212
213 socklen_t len = sizeof(sin6);
214 if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) {
215 return -errno;
216 }
217
218 *v6 = sin6.sin6_addr;
219
220 if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) ||
221 IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) {
222 return -ENETUNREACH;
223 }
224
225 makeChecksumNeutral(v6, v4, nat64Prefix);
226
227 return 0;
228}
229
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700230void ClatdController::maybeStartBpf(const ClatdTracker& tracker) {
231 if (mClatEbpfMode == ClatEbpfDisabled) return;
232
233 int rv = hardwareAddressType(tracker.iface);
234 if (rv < 0) {
235 ALOGE("hardwareAddressType(%s[%d]) failure: %s", tracker.iface, tracker.ifIndex,
236 strerror(-rv));
237 return;
238 }
239
240 bool isEthernet;
241 switch (rv) {
242 case ARPHRD_ETHER:
243 isEthernet = true;
244 break;
245 case ARPHRD_RAWIP: // in Linux 4.14+ rmnet support was upstreamed and this is 519
246 case 530: // this is ARPHRD_RAWIP on some Android 4.9 kernels with rmnet
247 isEthernet = false;
248 break;
249 default:
250 ALOGE("hardwareAddressType(%s[%d]) returned unknown type %d.", tracker.iface,
251 tracker.ifIndex, rv);
252 return;
253 }
254
255 rv = getClatIngressProgFd(isEthernet);
256 if (rv < 0) {
257 ALOGE("getClatIngressProgFd(%d) failure: %s", isEthernet, strerror(-rv));
258 return;
259 }
260 unique_fd progFd(rv);
261
262 ClatIngressKey key = {
263 .iif = tracker.ifIndex,
264 .pfx96 = tracker.pfx96,
265 .local6 = tracker.v6,
266 };
267 ClatIngressValue value = {
268 // Redirect the mangled packets to the same interface so we can see them in tcpdump.
269 // TODO: move the tun interface creation to netd, and use that ifindex instead.
270 // TODO: move all the clat code to eBPF and remove the tun interface entirely.
271 .oif = tracker.ifIndex,
272 .local4 = tracker.v4,
273 };
274
275 auto ret = mClatIngressMap.writeValue(key, value, BPF_ANY);
276 if (!isOk(ret)) {
277 ALOGE("mClatIngress.Map.writeValue failure: %s", strerror(ret.code()));
278 return;
279 }
280
281 // We do tc setup *after* populating map, so scanning through map
282 // can always be used to tell us what needs cleanup.
283
284 rv = tcQdiscAddDevClsact(mNetlinkFd, tracker.ifIndex);
285 if (rv) {
286 ALOGE("tcQdiscAddDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
287 strerror(-rv));
288 ret = mClatIngressMap.deleteValue(key);
289 if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code()));
290 return;
291 }
292
293 rv = tcFilterAddDevBpf(mNetlinkFd, tracker.ifIndex, progFd, isEthernet);
294 if (rv) {
295 if ((rv == -ENOENT) && (mClatEbpfMode == ClatEbpfMaybe)) {
296 ALOGI("tcFilterAddDevBpf(%d[%s], %d): %s", tracker.ifIndex, tracker.iface, isEthernet,
297 strerror(-rv));
298 } else {
299 ALOGE("tcFilterAddDevBpf(%d[%s], %d) failure: %s", tracker.ifIndex, tracker.iface,
300 isEthernet, strerror(-rv));
301 }
302 rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex);
303 if (rv)
304 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
305 strerror(-rv));
306 ret = mClatIngressMap.deleteValue(key);
307 if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code()));
308 return;
309 }
310
311 // success
312}
313
314void ClatdController::maybeStopBpf(const ClatdTracker& tracker) {
315 if (mClatEbpfMode == ClatEbpfDisabled) return;
316
317 // No need to remove filter, since we remove qdisc it is attached to,
318 // which automatically removes everything attached to the qdisc.
319 int rv = tcQdiscDelDevClsact(mNetlinkFd, tracker.ifIndex);
320 if (rv < 0)
321 ALOGE("tcQdiscDelDevClsact(%d[%s]) failure: %s", tracker.ifIndex, tracker.iface,
322 strerror(-rv));
323
324 // We cleanup map last, so scanning through map can be used to
325 // determine what still needs cleanup.
326
327 ClatIngressKey key = {
328 .iif = tracker.ifIndex,
329 .pfx96 = tracker.pfx96,
330 .local6 = tracker.v6,
331 };
332
333 auto ret = mClatIngressMap.deleteValue(key);
334 if (!isOk(ret)) ALOGE("mClatIngressMap.deleteValue failure: %s", strerror(ret.code()));
335}
336
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900337// Finds the tracker of the clatd running on interface |interface|, or nullptr if clatd has not been
338// started on |interface|.
339ClatdController::ClatdTracker* ClatdController::getClatdTracker(const std::string& interface) {
340 auto it = mClatdTrackers.find(interface);
341 return (it == mClatdTrackers.end() ? nullptr : &it->second);
342}
343
344// Initializes a ClatdTracker for the specified interface.
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700345int ClatdController::ClatdTracker::init(unsigned networkId, const std::string& interface,
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900346 const std::string& nat64Prefix) {
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700347 netId = networkId;
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900348
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900349 fwmark.netId = netId;
350 fwmark.explicitlySelected = true;
351 fwmark.protectedFromVpn = true;
352 fwmark.permission = PERMISSION_SYSTEM;
353
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900354 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900355 snprintf(netIdString, sizeof(netIdString), "%u", netId);
Maciej Żenczykowskic8c38aa2019-03-29 01:24:51 -0700356 ifIndex = if_nametoindex(interface.c_str());
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900357 strlcpy(iface, interface.c_str(), sizeof(iface));
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900358
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900359 // Pass in everything that clatd needs: interface, a netid to use for DNS lookups, a fwmark for
360 // outgoing packets, the NAT64 prefix, and the IPv4 and IPv6 addresses.
361 // Validate the prefix and strip off the prefix length.
362 uint8_t family;
363 uint8_t prefixLen;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700364 int res = parsePrefix(nat64Prefix.c_str(), &family, &pfx96, sizeof(pfx96), &prefixLen);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900365 // clatd only supports /96 prefixes.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700366 if (res != sizeof(pfx96)) return res;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900367 if (family != AF_INET6) return -EAFNOSUPPORT;
368 if (prefixLen != 96) return -EINVAL;
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700369 if (!inet_ntop(AF_INET6, &pfx96, pfx96String, sizeof(pfx96String))) return -errno;
Luke Huang6d301232018-08-01 14:05:18 +0800370
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900371 // Pick an IPv4 address.
372 // TODO: this picks the address based on other addresses that are assigned to interfaces, but
373 // the address is only actually assigned to an interface once clatd starts up. So we could end
374 // up with two clatd instances with the same IPv4 address.
375 // Stop doing this and instead pick a free one from the kV4Addr pool.
Maciej Żenczykowski55cacfb2019-03-30 02:01:35 -0700376 v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900377 if (v4.s_addr == INADDR_NONE) {
378 ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen);
379 return -EADDRNOTAVAIL;
380 }
381 if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno;
382
383 // Generate a checksum-neutral IID.
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700384 if (generateIpv6Address(iface, v4, pfx96, &v6)) {
385 ALOGE("Unable to find global source address on %s for %s", iface, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900386 return -EADDRNOTAVAIL;
387 }
388 if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno;
389
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700390 ALOGD("starting clatd on %s v4=%s v6=%s pfx96=%s", iface, v4Str, v6Str, pfx96String);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900391 return 0;
392}
393
394int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix,
395 std::string* v6Str) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700396 std::lock_guard guard(mutex);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900397 ClatdTracker* existing = getClatdTracker(interface);
398 if (existing != nullptr) {
399 ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str());
400 errno = EBUSY;
401 return -errno;
402 }
403
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700404 unsigned networkId = mNetCtrl->getNetworkForInterface(interface.c_str());
405 if (networkId == NETID_UNSET) {
406 ALOGE("Interface %s not assigned to any netId", interface.c_str());
407 errno = ENODEV;
408 return -errno;
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900409 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700410
Maciej Żenczykowskia56b2e62019-04-24 13:17:18 -0700411 ClatdTracker tracker;
412 int ret = tracker.init(networkId, interface, nat64Prefix);
413 if (ret) return ret;
414
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900415 std::string progname("clatd-");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900416 progname += tracker.iface;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500417
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900418 // clang-format off
419 char* args[] = {(char*) progname.c_str(),
420 (char*) "-i", tracker.iface,
421 (char*) "-n", tracker.netIdString,
422 (char*) "-m", tracker.fwmarkString,
Maciej Żenczykowski1c06f9c2019-03-29 23:19:19 -0700423 (char*) "-p", tracker.pfx96String,
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900424 (char*) "-4", tracker.v4Str,
425 (char*) "-6", tracker.v6Str,
Luke Huang94c43a12019-02-14 19:51:38 +0800426 nullptr};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900427 // clang-format on
428
Luke Huang94c43a12019-02-14 19:51:38 +0800429 // Specify no flags and no actions, posix_spawn will use vfork and is
430 // guaranteed to return only once exec has been called.
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900431 int res = posix_spawn(&tracker.pid, kClatdPath, nullptr, nullptr, args, nullptr);
Luke Huang40962442019-02-26 11:46:10 +0800432 if (res) {
433 ALOGE("posix_spawn failed (%s)", strerror(res));
Luke Huang6d301232018-08-01 14:05:18 +0800434 return -res;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500435 }
436
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700437 maybeStartBpf(tracker);
438
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900439 mClatdTrackers[interface] = tracker;
440 ALOGD("clatd started on %s", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500441
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900442 *v6Str = tracker.v6Str;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500443 return 0;
444}
445
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900446int ClatdController::stopClatd(const std::string& interface) {
Maciej Żenczykowski56280272019-03-30 03:32:51 -0700447 std::lock_guard guard(mutex);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900448 ClatdTracker* tracker = getClatdTracker(interface);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900449
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900450 if (tracker == nullptr) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500451 ALOGE("clatd already stopped");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900452 return -ENODEV;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500453 }
454
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900455 ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500456
Maciej Żenczykowski1c086e52019-03-29 23:13:49 -0700457 maybeStopBpf(*tracker);
458
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900459 kill(tracker->pid, SIGTERM);
460 waitpid(tracker->pid, nullptr, 0);
461 mClatdTrackers.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500462
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900463 ALOGD("clatd on %s stopped", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500464
465 return 0;
466}
467
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700468void ClatdController::dump(DumpWriter& dw) {
469 std::lock_guard guard(mutex);
470
471 ScopedIndent clatdIndent(dw);
472 dw.println("ClatdController");
473
474 {
475 ScopedIndent trackerIndent(dw);
476 dw.println("Trackers: iif[iface] nat64Prefix v6Addr -> v4Addr [netId]");
477
478 ScopedIndent trackerDetailIndent(dw);
479 for (const auto& pair : mClatdTrackers) {
480 const ClatdTracker& tracker = pair.second;
481 dw.println("%u[%s] %s/96 %s -> %s [%u]", tracker.ifIndex, tracker.iface,
482 tracker.pfx96String, tracker.v6Str, tracker.v4Str, tracker.netId);
483 }
484 }
485
Maciej Żenczykowskiabb2cbf2019-04-01 01:29:29 -0700486 int mapFd = getClatIngressMapFd();
487 if (mapFd < 0) return; // if unsupported just don't dump anything
488 BpfMap<ClatIngressKey, ClatIngressValue> configMap(mapFd);
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700489
490 ScopedIndent bpfIndent(dw);
491 dw.println("BPF ingress map: iif(iface) nat64Prefix v6Addr -> v4Addr oif(iface)");
492
493 ScopedIndent bpfDetailIndent(dw);
Maciej Żenczykowski55262712019-03-29 23:44:56 -0700494 const auto printClatMap = [&dw](const ClatIngressKey& key, const ClatIngressValue& value,
495 const BpfMap<ClatIngressKey, ClatIngressValue>&) {
496 char iifStr[IFNAMSIZ] = "?";
497 char pfx96Str[INET6_ADDRSTRLEN] = "?";
498 char local6Str[INET6_ADDRSTRLEN] = "?";
499 char local4Str[INET_ADDRSTRLEN] = "?";
500 char oifStr[IFNAMSIZ] = "?";
501
502 if_indextoname(key.iif, iifStr);
503 inet_ntop(AF_INET6, &key.pfx96, pfx96Str, sizeof(pfx96Str));
504 inet_ntop(AF_INET6, &key.local6, local6Str, sizeof(local6Str));
505 inet_ntop(AF_INET, &value.local4, local4Str, sizeof(local4Str));
506 if_indextoname(value.oif, oifStr);
507
508 dw.println("%u(%s) %s/96 %s -> %s %u(%s)", key.iif, iifStr, pfx96Str, local6Str, local4Str,
509 value.oif, oifStr);
510 return netdutils::status::ok;
511 };
512 auto res = configMap.iterateWithValue(printClatMap);
513 if (!isOk(res)) {
514 dw.println("Error printing BPF map: %s", res.msg().c_str());
515 }
516}
517
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900518auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree;
519
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900520} // namespace net
521} // namespace android