blob: 8f037ad07e45142164afe9f0cb3434f5a1855ee1 [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 Chen86a56de2021-12-17 19:40:59 +080020#include <log/log.h>
Hungming Chened7b4602021-12-17 15:03:47 +080021#include <stdlib.h>
Hungming Chen6139d872021-12-17 15:47:26 +080022#include <string.h>
23#include <unistd.h>
Hungming Chened7b4602021-12-17 15:03:47 +080024
25extern "C" {
26#include "checksum.h"
27}
28
Hungming Chen86a56de2021-12-17 19:40:59 +080029// Sync from system/netd/include/netid_client.h.
30#define MARK_UNSET 0u
31
Hungming Chened7b4602021-12-17 15:03:47 +080032namespace android {
33namespace net {
34namespace clat {
35
Hungming Chen2f623f32021-12-17 17:24:58 +080036bool isIpv4AddressFree(in_addr_t addr) {
37 int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
38 if (s == -1) {
39 return 0;
40 }
41
42 // Attempt to connect to the address. If the connection succeeds and getsockname returns the
43 // same then the address is already assigned to the system and we can't use it.
44 struct sockaddr_in sin = {
45 .sin_family = AF_INET,
46 .sin_port = htons(53),
47 .sin_addr = {addr},
48 };
49 socklen_t len = sizeof(sin);
50 bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 &&
51 getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) &&
52 sin.sin_addr.s_addr == addr;
53
54 close(s);
55 return !inuse;
56}
57
58// Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order.
59// ip - the IP address from the configuration file
60// prefixlen - the length of the prefix from which addresses may be selected.
61// returns: the IPv4 address, or INADDR_NONE if no addresses were available
62in_addr_t selectIpv4Address(const in_addr ip, int16_t prefixlen) {
63 return selectIpv4AddressInternal(ip, prefixlen, isIpv4AddressFree);
64}
65
66// Only allow testing to use this function directly. Otherwise call selectIpv4Address(ip, pfxlen)
67// which has applied valid isIpv4AddressFree function pointer.
68in_addr_t selectIpv4AddressInternal(const in_addr ip, int16_t prefixlen,
69 isIpv4AddrFreeFn isIpv4AddressFreeFunc) {
70 // Impossible! Only test allows to apply fn.
71 if (isIpv4AddressFreeFunc == nullptr) {
72 return INADDR_NONE;
73 }
74
75 // Don't accept prefixes that are too large because we scan addresses one by one.
76 if (prefixlen < 16 || prefixlen > 32) {
77 return INADDR_NONE;
78 }
79
80 // All these are in host byte order.
81 in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen);
82 in_addr_t ipv4 = ntohl(ip.s_addr);
83 in_addr_t first_ipv4 = ipv4;
84 in_addr_t prefix = ipv4 & mask;
85
86 // Pick the first IPv4 address in the pool, wrapping around if necessary.
87 // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0.
88 do {
89 if (isIpv4AddressFreeFunc(htonl(ipv4))) {
90 return htonl(ipv4);
91 }
92 ipv4 = prefix | ((ipv4 + 1) & ~mask);
93 } while (ipv4 != first_ipv4);
94
95 return INADDR_NONE;
96}
97
Hungming Chened7b4602021-12-17 15:03:47 +080098// Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix.
99void makeChecksumNeutral(in6_addr* v6, const in_addr v4, const in6_addr& nat64Prefix) {
100 // Fill last 8 bytes of IPv6 address with random bits.
101 arc4random_buf(&v6->s6_addr[8], 8);
102
103 // Make the IID checksum-neutral. That is, make it so that:
104 // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6)
105 // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4):
106 // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix)
107 // Do this by adjusting the two bytes in the middle of the IID.
108
109 uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12];
110
111 uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4));
112 uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) +
113 ip_checksum_add(0, v6, sizeof(*v6));
114
115 uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2);
116 v6->s6_addr[11] = delta >> 8;
117 v6->s6_addr[12] = delta & 0xff;
118}
119
Hungming Chen6139d872021-12-17 15:47:26 +0800120// Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix.
121int generateIpv6Address(const char* iface, const in_addr v4, const in6_addr& nat64Prefix,
122 in6_addr* v6) {
123 int s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
124 if (s == -1) return -errno;
125
126 if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) {
127 close(s);
128 return -errno;
129 }
130
131 sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix};
132 if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) {
133 close(s);
134 return -errno;
135 }
136
137 socklen_t len = sizeof(sin6);
138 if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) {
139 close(s);
140 return -errno;
141 }
142
143 *v6 = sin6.sin6_addr;
144
145 if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) ||
146 IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) {
147 close(s);
148 return -ENETUNREACH;
149 }
150
151 makeChecksumNeutral(v6, v4, nat64Prefix);
152 close(s);
153
154 return 0;
155}
156
Hungming Chen86a56de2021-12-17 19:40:59 +0800157int detect_mtu(const struct in6_addr* plat_subnet, uint32_t plat_suffix, uint32_t mark) {
158 // Create an IPv6 UDP socket.
159 int s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
160 if (s < 0) {
161 int ret = errno;
162 ALOGE("socket(AF_INET6, SOCK_DGRAM, 0) failed: %s", strerror(errno));
163 return -ret;
164 }
165
166 // Socket's mark affects routing decisions (network selection)
167 if ((mark != MARK_UNSET) && setsockopt(s, SOL_SOCKET, SO_MARK, &mark, sizeof(mark))) {
168 int ret = errno;
169 ALOGE("setsockopt(SOL_SOCKET, SO_MARK) failed: %s", strerror(errno));
170 close(s);
171 return -ret;
172 }
173
174 // Try to connect udp socket to plat_subnet(96 bits):plat_suffix(32 bits)
175 struct sockaddr_in6 dst = {
176 .sin6_family = AF_INET6,
177 .sin6_addr = *plat_subnet,
178 };
179 dst.sin6_addr.s6_addr32[3] = plat_suffix;
180 if (connect(s, (struct sockaddr*)&dst, sizeof(dst))) {
181 int ret = errno;
182 ALOGE("connect() failed: %s", strerror(errno));
183 close(s);
184 return -ret;
185 }
186
187 // Fetch the socket's IPv6 mtu - this is effectively fetching mtu from routing table
188 int mtu;
189 socklen_t sz_mtu = sizeof(mtu);
190 if (getsockopt(s, SOL_IPV6, IPV6_MTU, &mtu, &sz_mtu)) {
191 int ret = errno;
192 ALOGE("getsockopt(SOL_IPV6, IPV6_MTU) failed: %s", strerror(errno));
193 close(s);
194 return -ret;
195 }
196 if (sz_mtu != sizeof(mtu)) {
197 ALOGE("getsockopt(SOL_IPV6, IPV6_MTU) returned unexpected size: %d", sz_mtu);
198 close(s);
199 return -EFAULT;
200 }
201 close(s);
202
203 return mtu;
204}
205
Hungming Chened7b4602021-12-17 15:03:47 +0800206} // namespace clat
207} // namespace net
208} // namespace android