blob: 2723933b9440fa23b855d36fb3de9bbd587f6fdf [file] [log] [blame]
Hungming Chened7b4602021-12-17 15:03:47 +08001// Copyright (C) 2022 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Hungming Chen86a56de2021-12-17 19:40:59 +080015#define LOG_TAG "clatutils"
16
Hungming Chened7b4602021-12-17 15:03:47 +080017#include "libclat/clatutils.h"
18
Hungming Chen6139d872021-12-17 15:47:26 +080019#include <errno.h>
Hungming Chen8ff032b2021-12-17 21:12:53 +080020#include <linux/filter.h>
21#include <linux/if_packet.h>
22#include <linux/if_tun.h>
Hungming Chen86a56de2021-12-17 19:40:59 +080023#include <log/log.h>
Hungming Chened7b4602021-12-17 15:03:47 +080024#include <stdlib.h>
Hungming Chen6139d872021-12-17 15:47:26 +080025#include <string.h>
26#include <unistd.h>
Hungming Chened7b4602021-12-17 15:03:47 +080027
28extern "C" {
29#include "checksum.h"
30}
31
32namespace android {
33namespace net {
34namespace clat {
35
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -080036bool isIpv4AddressFree(const in_addr_t addr) {
37 const int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
38 if (s == -1) return 0;
Hungming Chen2f623f32021-12-17 17:24:58 +080039
40 // Attempt to connect to the address. If the connection succeeds and getsockname returns the
41 // same then the address is already assigned to the system and we can't use it.
42 struct sockaddr_in sin = {
43 .sin_family = AF_INET,
44 .sin_port = htons(53),
45 .sin_addr = {addr},
46 };
47 socklen_t len = sizeof(sin);
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -080048 const bool inuse = !connect(s, (struct sockaddr*)&sin, sizeof(sin)) &&
49 !getsockname(s, (struct sockaddr*)&sin, &len) &&
50 len == (socklen_t)sizeof(sin) &&
51 sin.sin_addr.s_addr == addr;
Hungming Chen2f623f32021-12-17 17:24:58 +080052
53 close(s);
54 return !inuse;
55}
56
57// Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order.
58// ip - the IP address from the configuration file
59// prefixlen - the length of the prefix from which addresses may be selected.
60// returns: the IPv4 address, or INADDR_NONE if no addresses were available
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -080061in_addr_t selectIpv4Address(const in_addr ip, const int16_t prefixlen) {
Hungming Chen2f623f32021-12-17 17:24:58 +080062 return selectIpv4AddressInternal(ip, prefixlen, isIpv4AddressFree);
63}
64
65// Only allow testing to use this function directly. Otherwise call selectIpv4Address(ip, pfxlen)
66// which has applied valid isIpv4AddressFree function pointer.
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -080067in_addr_t selectIpv4AddressInternal(const in_addr ip, const int16_t prefixlen,
68 const isIpv4AddrFreeFn isIpv4AddressFreeFunc) {
Hungming Chen2f623f32021-12-17 17:24:58 +080069 // Impossible! Only test allows to apply fn.
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -080070 if (isIpv4AddressFreeFunc == nullptr) return INADDR_NONE;
Hungming Chen2f623f32021-12-17 17:24:58 +080071
72 // Don't accept prefixes that are too large because we scan addresses one by one.
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -080073 if (prefixlen < 16 || prefixlen > 32) return INADDR_NONE;
Hungming Chen2f623f32021-12-17 17:24:58 +080074
75 // All these are in host byte order.
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -080076 const uint32_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen);
77 uint32_t ipv4 = ntohl(ip.s_addr);
78 const uint32_t first_ipv4 = ipv4;
79 const uint32_t prefix = ipv4 & mask;
Hungming Chen2f623f32021-12-17 17:24:58 +080080
81 // Pick the first IPv4 address in the pool, wrapping around if necessary.
82 // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0.
83 do {
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -080084 if (isIpv4AddressFreeFunc(htonl(ipv4))) return htonl(ipv4);
Hungming Chen2f623f32021-12-17 17:24:58 +080085 ipv4 = prefix | ((ipv4 + 1) & ~mask);
86 } while (ipv4 != first_ipv4);
87
88 return INADDR_NONE;
89}
90
Hungming Chened7b4602021-12-17 15:03:47 +080091// Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix.
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -080092void makeChecksumNeutral(in6_addr* const v6, const in_addr v4, const in6_addr& nat64Prefix) {
Hungming Chened7b4602021-12-17 15:03:47 +080093 // Fill last 8 bytes of IPv6 address with random bits.
94 arc4random_buf(&v6->s6_addr[8], 8);
95
96 // Make the IID checksum-neutral. That is, make it so that:
97 // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6)
98 // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4):
99 // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix)
100 // Do this by adjusting the two bytes in the middle of the IID.
101
102 uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12];
103
104 uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4));
105 uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) +
106 ip_checksum_add(0, v6, sizeof(*v6));
107
108 uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2);
109 v6->s6_addr[11] = delta >> 8;
110 v6->s6_addr[12] = delta & 0xff;
111}
112
Hungming Chen6139d872021-12-17 15:47:26 +0800113// Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix.
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800114int generateIpv6Address(const char* const iface, const in_addr v4, const in6_addr& nat64Prefix,
115 in6_addr* const v6, const uint32_t mark) {
116 const int s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
Hungming Chen6139d872021-12-17 15:47:26 +0800117 if (s == -1) return -errno;
118
t-m-w130e75b2022-10-24 02:54:07 +0000119 // Socket's mark affects routing decisions (network selection)
120 // An fwmark is necessary for clat to bypass the VPN during initialization.
Maciej Żenczykowski8bf59672023-01-17 23:52:02 +0000121 if (setsockopt(s, SOL_SOCKET, SO_MARK, &mark, sizeof(mark))) {
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800122 const int err = errno;
123 ALOGE("setsockopt(SOL_SOCKET, SO_MARK) failed: %s", strerror(err));
t-m-w130e75b2022-10-24 02:54:07 +0000124 close(s);
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800125 return -err;
t-m-w130e75b2022-10-24 02:54:07 +0000126 }
127
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800128 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1)) {
129 const int err = errno;
130 ALOGE("setsockopt(SOL_SOCKET, SO_BINDTODEVICE, '%s') failed: %s", iface, strerror(err));
Hungming Chen6139d872021-12-17 15:47:26 +0800131 close(s);
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800132 return -err;
Hungming Chen6139d872021-12-17 15:47:26 +0800133 }
134
135 sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix};
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800136 if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6))) {
Hungming Chen6139d872021-12-17 15:47:26 +0800137 close(s);
138 return -errno;
139 }
140
141 socklen_t len = sizeof(sin6);
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800142 if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len)) {
Hungming Chen6139d872021-12-17 15:47:26 +0800143 close(s);
144 return -errno;
145 }
146
147 *v6 = sin6.sin6_addr;
148
149 if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) ||
150 IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) {
151 close(s);
152 return -ENETUNREACH;
153 }
154
155 makeChecksumNeutral(v6, v4, nat64Prefix);
156 close(s);
157
158 return 0;
159}
160
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800161int detect_mtu(const struct in6_addr* const plat_subnet, const uint32_t plat_suffix,
162 const uint32_t mark) {
Hungming Chen86a56de2021-12-17 19:40:59 +0800163 // Create an IPv6 UDP socket.
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800164 const int s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
Hungming Chen86a56de2021-12-17 19:40:59 +0800165 if (s < 0) {
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800166 const int err = errno;
167 ALOGE("socket(AF_INET6, SOCK_DGRAM, 0) failed: %s", strerror(err));
168 return -err;
Hungming Chen86a56de2021-12-17 19:40:59 +0800169 }
170
171 // Socket's mark affects routing decisions (network selection)
Maciej Żenczykowski8bf59672023-01-17 23:52:02 +0000172 if (setsockopt(s, SOL_SOCKET, SO_MARK, &mark, sizeof(mark))) {
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800173 const int err = errno;
174 ALOGE("setsockopt(SOL_SOCKET, SO_MARK) failed: %s", strerror(err));
Hungming Chen86a56de2021-12-17 19:40:59 +0800175 close(s);
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800176 return -err;
Hungming Chen86a56de2021-12-17 19:40:59 +0800177 }
178
179 // Try to connect udp socket to plat_subnet(96 bits):plat_suffix(32 bits)
180 struct sockaddr_in6 dst = {
181 .sin6_family = AF_INET6,
182 .sin6_addr = *plat_subnet,
183 };
184 dst.sin6_addr.s6_addr32[3] = plat_suffix;
185 if (connect(s, (struct sockaddr*)&dst, sizeof(dst))) {
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800186 const int err = errno;
187 ALOGE("connect() failed: %s", strerror(err));
Hungming Chen86a56de2021-12-17 19:40:59 +0800188 close(s);
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800189 return -err;
Hungming Chen86a56de2021-12-17 19:40:59 +0800190 }
191
192 // Fetch the socket's IPv6 mtu - this is effectively fetching mtu from routing table
193 int mtu;
194 socklen_t sz_mtu = sizeof(mtu);
195 if (getsockopt(s, SOL_IPV6, IPV6_MTU, &mtu, &sz_mtu)) {
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800196 const int err = errno;
197 ALOGE("getsockopt(SOL_IPV6, IPV6_MTU) failed: %s", strerror(err));
Hungming Chen86a56de2021-12-17 19:40:59 +0800198 close(s);
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800199 return -err;
Hungming Chen86a56de2021-12-17 19:40:59 +0800200 }
201 if (sz_mtu != sizeof(mtu)) {
202 ALOGE("getsockopt(SOL_IPV6, IPV6_MTU) returned unexpected size: %d", sz_mtu);
203 close(s);
204 return -EFAULT;
205 }
206 close(s);
207
208 return mtu;
209}
210
Hungming Chen8ff032b2021-12-17 21:12:53 +0800211/* function: configure_packet_socket
212 * Binds the packet socket and attaches the receive filter to it.
213 * sock - the socket to configure
214 * addr - the IP address to filter
215 * ifindex - index of interface to add the filter to
216 * returns: 0 on success, -errno on failure
217 */
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800218int configure_packet_socket(const int sock, const in6_addr* const addr, const int ifindex) {
219 const uint32_t* ipv6 = addr->s6_addr32;
Hungming Chen8ff032b2021-12-17 21:12:53 +0800220
221 // clang-format off
222 struct sock_filter filter_code[] = {
223 // Load the first four bytes of the IPv6 destination address (starts 24 bytes in).
224 // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads
225 // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it
226 // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other
Maciej Żenczykowski4fcf8a02023-01-17 23:49:55 +0000227 // three words of the IPv6 address, and if they all match, return full packet (accept packet).
Hungming Chen8ff032b2021-12-17 21:12:53 +0800228 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24),
229 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7),
230 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28),
231 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5),
232 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32),
233 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3),
234 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36),
235 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1),
Maciej Żenczykowski4fcf8a02023-01-17 23:49:55 +0000236 BPF_STMT(BPF_RET | BPF_K, 0xFFFFFFFF),
Hungming Chen8ff032b2021-12-17 21:12:53 +0800237 BPF_STMT(BPF_RET | BPF_K, 0),
238 };
239 // clang-format on
240 struct sock_fprog filter = {sizeof(filter_code) / sizeof(filter_code[0]), filter_code};
241
242 if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) {
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800243 const int err = errno;
244 ALOGE("attach packet filter failed: %s", strerror(err));
245 return -err;
Hungming Chen8ff032b2021-12-17 21:12:53 +0800246 }
247
248 struct sockaddr_ll sll = {
249 .sll_family = AF_PACKET,
250 .sll_protocol = htons(ETH_P_IPV6),
251 .sll_ifindex = ifindex,
252 .sll_pkttype =
253 PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel.
254 };
255 if (bind(sock, (struct sockaddr*)&sll, sizeof(sll))) {
Maciej Żenczykowski26b8e852023-03-08 17:42:14 -0800256 const int err = errno;
257 ALOGE("binding packet socket: %s", strerror(err));
258 return -err;
Hungming Chen8ff032b2021-12-17 21:12:53 +0800259 }
260
261 return 0;
262}
263
Hungming Chened7b4602021-12-17 15:03:47 +0800264} // namespace clat
265} // namespace net
266} // namespace android