blob: 142c4ca043067a1219323c4252f686cf39800226 [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
17#include "ClatdController.h"
18
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090019#include <map>
20#include <string>
21
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090022#include <arpa/inet.h>
Daniel Drown0da73fc2012-06-20 16:51:39 -050023#include <errno.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
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090033#include "android-base/unique_fd.h"
34
35extern "C" {
36#include "netutils/checksum.h"
37}
38
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090039#include "Fwmark.h"
Paul Jensen84c1d032014-05-30 13:29:41 -040040#include "NetdConstants.h"
41#include "NetworkController.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090042#include "netid_client.h"
Daniel Drown0da73fc2012-06-20 16:51:39 -050043
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090044static const char* kClatdPath = "/system/bin/clatd";
45
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090046// For historical reasons, start with 192.0.0.4, and after that, use all subsequent addresses in
47// 192.0.0.0/29 (RFC 7335).
48static const char* kV4AddrString = "192.0.0.4";
49static const in_addr kV4Addr = {inet_addr(kV4AddrString)};
50static const int kV4AddrLen = 29;
51
52using android::base::unique_fd;
53
Lorenzo Colitti7035f222017-02-13 18:29:00 +090054namespace android {
55namespace net {
56
Paul Jensen84c1d032014-05-30 13:29:41 -040057ClatdController::ClatdController(NetworkController* controller)
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090058 : mNetCtrl(controller) {
Daniel Drown0da73fc2012-06-20 16:51:39 -050059}
60
61ClatdController::~ClatdController() {
62}
63
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090064bool ClatdController::isIpv4AddressFree(in_addr_t addr) {
65 int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
66 if (s == -1) {
67 return 0;
68 }
69
70 // Attempt to connect to the address. If the connection succeeds and getsockname returns the
71 // same then the address is already assigned to the system and we can't use it.
72 struct sockaddr_in sin = {.sin_family = AF_INET, .sin_addr = {addr}, .sin_port = 53};
73 socklen_t len = sizeof(sin);
74 bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 &&
75 getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) &&
76 sin.sin_addr.s_addr == addr;
77
78 close(s);
79 return !inuse;
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090080}
Daniel Drown0da73fc2012-06-20 16:51:39 -050081
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090082// Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order.
83// ip - the IP address from the configuration file
84// prefixlen - the length of the prefix from which addresses may be selected.
85// returns: the IPv4 address, or INADDR_NONE if no addresses were available
86in_addr_t ClatdController::selectIpv4Address(const in_addr ip, int16_t prefixlen) {
87 // Don't accept prefixes that are too large because we scan addresses one by one.
88 if (prefixlen < 16 || prefixlen > 32) {
89 return INADDR_NONE;
90 }
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090091
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090092 // All these are in host byte order.
93 in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen);
94 in_addr_t ipv4 = ntohl(ip.s_addr);
95 in_addr_t first_ipv4 = ipv4;
96 in_addr_t prefix = ipv4 & mask;
97
98 // Pick the first IPv4 address in the pool, wrapping around if necessary.
99 // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0.
100 do {
101 if (isIpv4AddressFreeFunc(htonl(ipv4))) {
102 return htonl(ipv4);
103 }
104 ipv4 = prefix | ((ipv4 + 1) & ~mask);
105 } while (ipv4 != first_ipv4);
106
107 return INADDR_NONE;
108}
109
110// Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix.
111void ClatdController::makeChecksumNeutral(in6_addr* v6, const in_addr v4,
112 const in6_addr& nat64Prefix) {
113 // Fill last 8 bytes of IPv6 address with random bits.
114 arc4random_buf(&v6->s6_addr[8], 8);
115
116 // Make the IID checksum-neutral. That is, make it so that:
117 // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6)
118 // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4):
119 // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix)
120 // Do this by adjusting the two bytes in the middle of the IID.
121
122 uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12];
123
124 uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4));
125 uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) +
126 ip_checksum_add(0, v6, sizeof(*v6));
127
128 uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2);
129 v6->s6_addr[11] = delta >> 8;
130 v6->s6_addr[12] = delta & 0xff;
131}
132
133// Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix.
134int ClatdController::generateIpv6Address(const char* iface, const in_addr v4,
135 const in6_addr& nat64Prefix, in6_addr* v6) {
136 unique_fd s(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0));
137 if (s == -1) return -errno;
138
139 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) {
Luke Huang6d301232018-08-01 14:05:18 +0800140 return -errno;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500141 }
142
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900143 sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix};
144 if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) {
145 return -errno;
146 }
147
148 socklen_t len = sizeof(sin6);
149 if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) {
150 return -errno;
151 }
152
153 *v6 = sin6.sin6_addr;
154
155 if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) ||
156 IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) {
157 return -ENETUNREACH;
158 }
159
160 makeChecksumNeutral(v6, v4, nat64Prefix);
161
162 return 0;
163}
164
165// Finds the tracker of the clatd running on interface |interface|, or nullptr if clatd has not been
166// started on |interface|.
167ClatdController::ClatdTracker* ClatdController::getClatdTracker(const std::string& interface) {
168 auto it = mClatdTrackers.find(interface);
169 return (it == mClatdTrackers.end() ? nullptr : &it->second);
170}
171
172// Initializes a ClatdTracker for the specified interface.
173int ClatdController::ClatdTracker::init(const std::string& interface,
174 const std::string& nat64Prefix) {
175 netId = netCtrl->getNetworkForInterface(interface.c_str());
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900176 if (netId == NETID_UNSET) {
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900177 ALOGE("Interface %s not assigned to any netId", interface.c_str());
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900178 errno = ENODEV;
Luke Huang6d301232018-08-01 14:05:18 +0800179 return -errno;
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900180 }
181
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900182 fwmark.netId = netId;
183 fwmark.explicitlySelected = true;
184 fwmark.protectedFromVpn = true;
185 fwmark.permission = PERMISSION_SYSTEM;
186
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900187 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900188 snprintf(netIdString, sizeof(netIdString), "%u", netId);
189 strlcpy(iface, interface.c_str(), sizeof(iface));
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900190
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900191 // Pass in everything that clatd needs: interface, a netid to use for DNS lookups, a fwmark for
192 // outgoing packets, the NAT64 prefix, and the IPv4 and IPv6 addresses.
193 // Validate the prefix and strip off the prefix length.
194 uint8_t family;
195 uint8_t prefixLen;
196 int res = parsePrefix(nat64Prefix.c_str(), &family, &dst, sizeof(dst), &prefixLen);
197 // clatd only supports /96 prefixes.
198 if (res != sizeof(dst)) return res;
199 if (family != AF_INET6) return -EAFNOSUPPORT;
200 if (prefixLen != 96) return -EINVAL;
201 if (!inet_ntop(AF_INET6, &dst, dstString, sizeof(dstString))) return -errno;
Luke Huang6d301232018-08-01 14:05:18 +0800202
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900203 // Pick an IPv4 address.
204 // TODO: this picks the address based on other addresses that are assigned to interfaces, but
205 // the address is only actually assigned to an interface once clatd starts up. So we could end
206 // up with two clatd instances with the same IPv4 address.
207 // Stop doing this and instead pick a free one from the kV4Addr pool.
208 in_addr v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)};
209 if (v4.s_addr == INADDR_NONE) {
210 ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen);
211 return -EADDRNOTAVAIL;
212 }
213 if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno;
214
215 // Generate a checksum-neutral IID.
216 if (generateIpv6Address(iface, v4, dst, &v6)) {
217 ALOGE("Unable to find global source address on %s for %s", iface, dstString);
218 return -EADDRNOTAVAIL;
219 }
220 if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno;
221
222 ALOGD("starting clatd on %s v4=%s v6=%s dst=%s", iface, v4Str, v6Str, dstString);
223 return 0;
224}
225
226int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix,
227 std::string* v6Str) {
228 ClatdTracker* existing = getClatdTracker(interface);
229 if (existing != nullptr) {
230 ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str());
231 errno = EBUSY;
232 return -errno;
233 }
234
235 ClatdTracker tracker(mNetCtrl);
236 if (int ret = tracker.init(interface, nat64Prefix)) {
237 return ret;
238 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700239
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900240 std::string progname("clatd-");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900241 progname += tracker.iface;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500242
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900243 // clang-format off
244 char* args[] = {(char*) progname.c_str(),
245 (char*) "-i", tracker.iface,
246 (char*) "-n", tracker.netIdString,
247 (char*) "-m", tracker.fwmarkString,
248 (char*) "-p", tracker.dstString,
249 (char*) "-4", tracker.v4Str,
250 (char*) "-6", tracker.v6Str,
Luke Huang94c43a12019-02-14 19:51:38 +0800251 nullptr};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900252 // clang-format on
253
Luke Huang94c43a12019-02-14 19:51:38 +0800254 // Specify no flags and no actions, posix_spawn will use vfork and is
255 // guaranteed to return only once exec has been called.
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900256 int res = posix_spawn(&tracker.pid, kClatdPath, nullptr, nullptr, args, nullptr);
Luke Huang40962442019-02-26 11:46:10 +0800257 if (res) {
258 ALOGE("posix_spawn failed (%s)", strerror(res));
Luke Huang6d301232018-08-01 14:05:18 +0800259 return -res;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500260 }
261
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900262 mClatdTrackers[interface] = tracker;
263 ALOGD("clatd started on %s", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500264
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900265 *v6Str = tracker.v6Str;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500266 return 0;
267}
268
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900269int ClatdController::stopClatd(const std::string& interface) {
270 ClatdTracker* tracker = getClatdTracker(interface);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900271
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900272 if (tracker == nullptr) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500273 ALOGE("clatd already stopped");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900274 return -ENODEV;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500275 }
276
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900277 ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500278
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900279 kill(tracker->pid, SIGTERM);
280 waitpid(tracker->pid, nullptr, 0);
281 mClatdTrackers.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500282
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900283 ALOGD("clatd on %s stopped", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500284
285 return 0;
286}
287
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900288auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree;
289
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900290} // namespace net
291} // namespace android