blob: b5988c84c708d9cfc5d422e260367572e5a86b2c [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>
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
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090034#include "android-base/unique_fd.h"
35
36extern "C" {
37#include "netutils/checksum.h"
38}
39
Lorenzo Colitti45d3dd02014-06-09 14:09:20 +090040#include "Fwmark.h"
Paul Jensen84c1d032014-05-30 13:29:41 -040041#include "NetdConstants.h"
42#include "NetworkController.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090043#include "netid_client.h"
Daniel Drown0da73fc2012-06-20 16:51:39 -050044
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090045static const char* kClatdPath = "/system/bin/clatd";
46
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090047// For historical reasons, start with 192.0.0.4, and after that, use all subsequent addresses in
48// 192.0.0.0/29 (RFC 7335).
49static const char* kV4AddrString = "192.0.0.4";
50static const in_addr kV4Addr = {inet_addr(kV4AddrString)};
51static const int kV4AddrLen = 29;
52
53using android::base::unique_fd;
54
Lorenzo Colitti7035f222017-02-13 18:29:00 +090055namespace android {
56namespace net {
57
Paul Jensen84c1d032014-05-30 13:29:41 -040058ClatdController::ClatdController(NetworkController* controller)
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090059 : mNetCtrl(controller) {
Daniel Drown0da73fc2012-06-20 16:51:39 -050060}
61
62ClatdController::~ClatdController() {
63}
64
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090065bool ClatdController::isIpv4AddressFree(in_addr_t addr) {
66 int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
67 if (s == -1) {
68 return 0;
69 }
70
71 // Attempt to connect to the address. If the connection succeeds and getsockname returns the
72 // same then the address is already assigned to the system and we can't use it.
73 struct sockaddr_in sin = {.sin_family = AF_INET, .sin_addr = {addr}, .sin_port = 53};
74 socklen_t len = sizeof(sin);
75 bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 &&
76 getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) &&
77 sin.sin_addr.s_addr == addr;
78
79 close(s);
80 return !inuse;
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090081}
Daniel Drown0da73fc2012-06-20 16:51:39 -050082
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090083// Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order.
84// ip - the IP address from the configuration file
85// prefixlen - the length of the prefix from which addresses may be selected.
86// returns: the IPv4 address, or INADDR_NONE if no addresses were available
87in_addr_t ClatdController::selectIpv4Address(const in_addr ip, int16_t prefixlen) {
88 // Don't accept prefixes that are too large because we scan addresses one by one.
89 if (prefixlen < 16 || prefixlen > 32) {
90 return INADDR_NONE;
91 }
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +090092
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +090093 // All these are in host byte order.
94 in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen);
95 in_addr_t ipv4 = ntohl(ip.s_addr);
96 in_addr_t first_ipv4 = ipv4;
97 in_addr_t prefix = ipv4 & mask;
98
99 // Pick the first IPv4 address in the pool, wrapping around if necessary.
100 // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0.
101 do {
102 if (isIpv4AddressFreeFunc(htonl(ipv4))) {
103 return htonl(ipv4);
104 }
105 ipv4 = prefix | ((ipv4 + 1) & ~mask);
106 } while (ipv4 != first_ipv4);
107
108 return INADDR_NONE;
109}
110
111// Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix.
112void ClatdController::makeChecksumNeutral(in6_addr* v6, const in_addr v4,
113 const in6_addr& nat64Prefix) {
114 // Fill last 8 bytes of IPv6 address with random bits.
115 arc4random_buf(&v6->s6_addr[8], 8);
116
117 // Make the IID checksum-neutral. That is, make it so that:
118 // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6)
119 // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4):
120 // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix)
121 // Do this by adjusting the two bytes in the middle of the IID.
122
123 uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12];
124
125 uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4));
126 uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) +
127 ip_checksum_add(0, v6, sizeof(*v6));
128
129 uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2);
130 v6->s6_addr[11] = delta >> 8;
131 v6->s6_addr[12] = delta & 0xff;
132}
133
134// Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix.
135int ClatdController::generateIpv6Address(const char* iface, const in_addr v4,
136 const in6_addr& nat64Prefix, in6_addr* v6) {
137 unique_fd s(socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0));
138 if (s == -1) return -errno;
139
140 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) {
Luke Huang6d301232018-08-01 14:05:18 +0800141 return -errno;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500142 }
143
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900144 sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix};
145 if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) {
146 return -errno;
147 }
148
149 socklen_t len = sizeof(sin6);
150 if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) {
151 return -errno;
152 }
153
154 *v6 = sin6.sin6_addr;
155
156 if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) ||
157 IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) {
158 return -ENETUNREACH;
159 }
160
161 makeChecksumNeutral(v6, v4, nat64Prefix);
162
163 return 0;
164}
165
166// Finds the tracker of the clatd running on interface |interface|, or nullptr if clatd has not been
167// started on |interface|.
168ClatdController::ClatdTracker* ClatdController::getClatdTracker(const std::string& interface) {
169 auto it = mClatdTrackers.find(interface);
170 return (it == mClatdTrackers.end() ? nullptr : &it->second);
171}
172
173// Initializes a ClatdTracker for the specified interface.
174int ClatdController::ClatdTracker::init(const std::string& interface,
175 const std::string& nat64Prefix) {
176 netId = netCtrl->getNetworkForInterface(interface.c_str());
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900177 if (netId == NETID_UNSET) {
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900178 ALOGE("Interface %s not assigned to any netId", interface.c_str());
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900179 errno = ENODEV;
Luke Huang6d301232018-08-01 14:05:18 +0800180 return -errno;
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900181 }
182
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900183 fwmark.netId = netId;
184 fwmark.explicitlySelected = true;
185 fwmark.protectedFromVpn = true;
186 fwmark.permission = PERMISSION_SYSTEM;
187
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900188 snprintf(fwmarkString, sizeof(fwmarkString), "0x%x", fwmark.intValue);
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900189 snprintf(netIdString, sizeof(netIdString), "%u", netId);
Maciej Żenczykowskic8c38aa2019-03-29 01:24:51 -0700190 ifIndex = if_nametoindex(interface.c_str());
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900191 strlcpy(iface, interface.c_str(), sizeof(iface));
Lorenzo Colitti32b2e792015-01-07 15:11:30 +0900192
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900193 // Pass in everything that clatd needs: interface, a netid to use for DNS lookups, a fwmark for
194 // outgoing packets, the NAT64 prefix, and the IPv4 and IPv6 addresses.
195 // Validate the prefix and strip off the prefix length.
196 uint8_t family;
197 uint8_t prefixLen;
198 int res = parsePrefix(nat64Prefix.c_str(), &family, &dst, sizeof(dst), &prefixLen);
199 // clatd only supports /96 prefixes.
200 if (res != sizeof(dst)) return res;
201 if (family != AF_INET6) return -EAFNOSUPPORT;
202 if (prefixLen != 96) return -EINVAL;
203 if (!inet_ntop(AF_INET6, &dst, dstString, sizeof(dstString))) return -errno;
Luke Huang6d301232018-08-01 14:05:18 +0800204
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900205 // Pick an IPv4 address.
206 // TODO: this picks the address based on other addresses that are assigned to interfaces, but
207 // the address is only actually assigned to an interface once clatd starts up. So we could end
208 // up with two clatd instances with the same IPv4 address.
209 // Stop doing this and instead pick a free one from the kV4Addr pool.
Maciej Żenczykowski55cacfb2019-03-30 02:01:35 -0700210 v4 = {selectIpv4Address(kV4Addr, kV4AddrLen)};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900211 if (v4.s_addr == INADDR_NONE) {
212 ALOGE("No free IPv4 address in %s/%d", kV4AddrString, kV4AddrLen);
213 return -EADDRNOTAVAIL;
214 }
215 if (!inet_ntop(AF_INET, &v4, v4Str, sizeof(v4Str))) return -errno;
216
217 // Generate a checksum-neutral IID.
218 if (generateIpv6Address(iface, v4, dst, &v6)) {
219 ALOGE("Unable to find global source address on %s for %s", iface, dstString);
220 return -EADDRNOTAVAIL;
221 }
222 if (!inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str))) return -errno;
223
224 ALOGD("starting clatd on %s v4=%s v6=%s dst=%s", iface, v4Str, v6Str, dstString);
225 return 0;
226}
227
228int ClatdController::startClatd(const std::string& interface, const std::string& nat64Prefix,
229 std::string* v6Str) {
230 ClatdTracker* existing = getClatdTracker(interface);
231 if (existing != nullptr) {
232 ALOGE("clatd pid=%d already started on %s", existing->pid, interface.c_str());
233 errno = EBUSY;
234 return -errno;
235 }
236
237 ClatdTracker tracker(mNetCtrl);
238 if (int ret = tracker.init(interface, nat64Prefix)) {
239 return ret;
240 }
JP Abgrall69261cb2014-06-19 18:35:24 -0700241
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900242 std::string progname("clatd-");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900243 progname += tracker.iface;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500244
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900245 // clang-format off
246 char* args[] = {(char*) progname.c_str(),
247 (char*) "-i", tracker.iface,
248 (char*) "-n", tracker.netIdString,
249 (char*) "-m", tracker.fwmarkString,
250 (char*) "-p", tracker.dstString,
251 (char*) "-4", tracker.v4Str,
252 (char*) "-6", tracker.v6Str,
Luke Huang94c43a12019-02-14 19:51:38 +0800253 nullptr};
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900254 // clang-format on
255
Luke Huang94c43a12019-02-14 19:51:38 +0800256 // Specify no flags and no actions, posix_spawn will use vfork and is
257 // guaranteed to return only once exec has been called.
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900258 int res = posix_spawn(&tracker.pid, kClatdPath, nullptr, nullptr, args, nullptr);
Luke Huang40962442019-02-26 11:46:10 +0800259 if (res) {
260 ALOGE("posix_spawn failed (%s)", strerror(res));
Luke Huang6d301232018-08-01 14:05:18 +0800261 return -res;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500262 }
263
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900264 mClatdTrackers[interface] = tracker;
265 ALOGD("clatd started on %s", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500266
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900267 *v6Str = tracker.v6Str;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500268 return 0;
269}
270
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900271int ClatdController::stopClatd(const std::string& interface) {
272 ClatdTracker* tracker = getClatdTracker(interface);
Lorenzo Colittiac7fefc2014-10-20 17:14:13 +0900273
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900274 if (tracker == nullptr) {
Daniel Drown0da73fc2012-06-20 16:51:39 -0500275 ALOGE("clatd already stopped");
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900276 return -ENODEV;
Daniel Drown0da73fc2012-06-20 16:51:39 -0500277 }
278
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900279 ALOGD("Stopping clatd pid=%d on %s", tracker->pid, interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500280
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900281 kill(tracker->pid, SIGTERM);
282 waitpid(tracker->pid, nullptr, 0);
283 mClatdTrackers.erase(interface);
Daniel Drown0da73fc2012-06-20 16:51:39 -0500284
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900285 ALOGD("clatd on %s stopped", interface.c_str());
Daniel Drown0da73fc2012-06-20 16:51:39 -0500286
287 return 0;
288}
289
Lorenzo Colitti7ef8c0f2019-01-11 22:34:58 +0900290auto ClatdController::isIpv4AddressFreeFunc = isIpv4AddressFree;
291
Lorenzo Colitti7035f222017-02-13 18:29:00 +0900292} // namespace net
293} // namespace android