Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 1 | // 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 Chen | 86a56de | 2021-12-17 19:40:59 +0800 | [diff] [blame] | 15 | #define LOG_TAG "clatutils" |
| 16 | |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 17 | #include "libclat/clatutils.h" |
| 18 | |
Hungming Chen | 6139d87 | 2021-12-17 15:47:26 +0800 | [diff] [blame] | 19 | #include <errno.h> |
Hungming Chen | 8ff032b | 2021-12-17 21:12:53 +0800 | [diff] [blame] | 20 | #include <linux/filter.h> |
| 21 | #include <linux/if_packet.h> |
| 22 | #include <linux/if_tun.h> |
Hungming Chen | 86a56de | 2021-12-17 19:40:59 +0800 | [diff] [blame] | 23 | #include <log/log.h> |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 24 | #include <stdlib.h> |
Hungming Chen | 6139d87 | 2021-12-17 15:47:26 +0800 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include <unistd.h> |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 27 | |
| 28 | extern "C" { |
| 29 | #include "checksum.h" |
| 30 | } |
| 31 | |
Hungming Chen | 8ff032b | 2021-12-17 21:12:53 +0800 | [diff] [blame] | 32 | // Sync from external/android-clat/clatd.h |
| 33 | #define MAXMTU 65536 |
| 34 | #define PACKETLEN (MAXMTU + sizeof(struct tun_pi)) |
| 35 | |
Hungming Chen | 86a56de | 2021-12-17 19:40:59 +0800 | [diff] [blame] | 36 | // Sync from system/netd/include/netid_client.h. |
| 37 | #define MARK_UNSET 0u |
| 38 | |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 39 | namespace android { |
| 40 | namespace net { |
| 41 | namespace clat { |
| 42 | |
Hungming Chen | 2f623f3 | 2021-12-17 17:24:58 +0800 | [diff] [blame] | 43 | bool isIpv4AddressFree(in_addr_t addr) { |
| 44 | int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 45 | if (s == -1) { |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | // Attempt to connect to the address. If the connection succeeds and getsockname returns the |
| 50 | // same then the address is already assigned to the system and we can't use it. |
| 51 | struct sockaddr_in sin = { |
| 52 | .sin_family = AF_INET, |
| 53 | .sin_port = htons(53), |
| 54 | .sin_addr = {addr}, |
| 55 | }; |
| 56 | socklen_t len = sizeof(sin); |
| 57 | bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 && |
| 58 | getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) && |
| 59 | sin.sin_addr.s_addr == addr; |
| 60 | |
| 61 | close(s); |
| 62 | return !inuse; |
| 63 | } |
| 64 | |
| 65 | // Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order. |
| 66 | // ip - the IP address from the configuration file |
| 67 | // prefixlen - the length of the prefix from which addresses may be selected. |
| 68 | // returns: the IPv4 address, or INADDR_NONE if no addresses were available |
| 69 | in_addr_t selectIpv4Address(const in_addr ip, int16_t prefixlen) { |
| 70 | return selectIpv4AddressInternal(ip, prefixlen, isIpv4AddressFree); |
| 71 | } |
| 72 | |
| 73 | // Only allow testing to use this function directly. Otherwise call selectIpv4Address(ip, pfxlen) |
| 74 | // which has applied valid isIpv4AddressFree function pointer. |
| 75 | in_addr_t selectIpv4AddressInternal(const in_addr ip, int16_t prefixlen, |
| 76 | isIpv4AddrFreeFn isIpv4AddressFreeFunc) { |
| 77 | // Impossible! Only test allows to apply fn. |
| 78 | if (isIpv4AddressFreeFunc == nullptr) { |
| 79 | return INADDR_NONE; |
| 80 | } |
| 81 | |
| 82 | // Don't accept prefixes that are too large because we scan addresses one by one. |
| 83 | if (prefixlen < 16 || prefixlen > 32) { |
| 84 | return INADDR_NONE; |
| 85 | } |
| 86 | |
| 87 | // All these are in host byte order. |
| 88 | in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen); |
| 89 | in_addr_t ipv4 = ntohl(ip.s_addr); |
| 90 | in_addr_t first_ipv4 = ipv4; |
| 91 | in_addr_t prefix = ipv4 & mask; |
| 92 | |
| 93 | // Pick the first IPv4 address in the pool, wrapping around if necessary. |
| 94 | // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0. |
| 95 | do { |
| 96 | if (isIpv4AddressFreeFunc(htonl(ipv4))) { |
| 97 | return htonl(ipv4); |
| 98 | } |
| 99 | ipv4 = prefix | ((ipv4 + 1) & ~mask); |
| 100 | } while (ipv4 != first_ipv4); |
| 101 | |
| 102 | return INADDR_NONE; |
| 103 | } |
| 104 | |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 105 | // Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix. |
| 106 | void makeChecksumNeutral(in6_addr* v6, const in_addr v4, const in6_addr& nat64Prefix) { |
| 107 | // Fill last 8 bytes of IPv6 address with random bits. |
| 108 | arc4random_buf(&v6->s6_addr[8], 8); |
| 109 | |
| 110 | // Make the IID checksum-neutral. That is, make it so that: |
| 111 | // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6) |
| 112 | // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4): |
| 113 | // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix) |
| 114 | // Do this by adjusting the two bytes in the middle of the IID. |
| 115 | |
| 116 | uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12]; |
| 117 | |
| 118 | uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4)); |
| 119 | uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) + |
| 120 | ip_checksum_add(0, v6, sizeof(*v6)); |
| 121 | |
| 122 | uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2); |
| 123 | v6->s6_addr[11] = delta >> 8; |
| 124 | v6->s6_addr[12] = delta & 0xff; |
| 125 | } |
| 126 | |
Hungming Chen | 6139d87 | 2021-12-17 15:47:26 +0800 | [diff] [blame] | 127 | // Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix. |
| 128 | int generateIpv6Address(const char* iface, const in_addr v4, const in6_addr& nat64Prefix, |
t-m-w | 130e75b | 2022-10-24 02:54:07 +0000 | [diff] [blame^] | 129 | in6_addr* v6, uint32_t mark) { |
Hungming Chen | 6139d87 | 2021-12-17 15:47:26 +0800 | [diff] [blame] | 130 | int s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 131 | if (s == -1) return -errno; |
| 132 | |
t-m-w | 130e75b | 2022-10-24 02:54:07 +0000 | [diff] [blame^] | 133 | // Socket's mark affects routing decisions (network selection) |
| 134 | // An fwmark is necessary for clat to bypass the VPN during initialization. |
| 135 | if ((mark != MARK_UNSET) && setsockopt(s, SOL_SOCKET, SO_MARK, &mark, sizeof(mark))) { |
| 136 | int ret = errno; |
| 137 | ALOGE("setsockopt(SOL_SOCKET, SO_MARK) failed: %s", strerror(errno)); |
| 138 | close(s); |
| 139 | return -ret; |
| 140 | } |
| 141 | |
Hungming Chen | 6139d87 | 2021-12-17 15:47:26 +0800 | [diff] [blame] | 142 | if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) { |
| 143 | close(s); |
| 144 | return -errno; |
| 145 | } |
| 146 | |
| 147 | sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix}; |
| 148 | if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) { |
| 149 | close(s); |
| 150 | return -errno; |
| 151 | } |
| 152 | |
| 153 | socklen_t len = sizeof(sin6); |
| 154 | if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) { |
| 155 | close(s); |
| 156 | return -errno; |
| 157 | } |
| 158 | |
| 159 | *v6 = sin6.sin6_addr; |
| 160 | |
| 161 | if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) || |
| 162 | IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) { |
| 163 | close(s); |
| 164 | return -ENETUNREACH; |
| 165 | } |
| 166 | |
| 167 | makeChecksumNeutral(v6, v4, nat64Prefix); |
| 168 | close(s); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
Hungming Chen | 86a56de | 2021-12-17 19:40:59 +0800 | [diff] [blame] | 173 | int detect_mtu(const struct in6_addr* plat_subnet, uint32_t plat_suffix, uint32_t mark) { |
| 174 | // Create an IPv6 UDP socket. |
| 175 | int s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 176 | if (s < 0) { |
| 177 | int ret = errno; |
| 178 | ALOGE("socket(AF_INET6, SOCK_DGRAM, 0) failed: %s", strerror(errno)); |
| 179 | return -ret; |
| 180 | } |
| 181 | |
| 182 | // Socket's mark affects routing decisions (network selection) |
| 183 | if ((mark != MARK_UNSET) && setsockopt(s, SOL_SOCKET, SO_MARK, &mark, sizeof(mark))) { |
| 184 | int ret = errno; |
| 185 | ALOGE("setsockopt(SOL_SOCKET, SO_MARK) failed: %s", strerror(errno)); |
| 186 | close(s); |
| 187 | return -ret; |
| 188 | } |
| 189 | |
| 190 | // Try to connect udp socket to plat_subnet(96 bits):plat_suffix(32 bits) |
| 191 | struct sockaddr_in6 dst = { |
| 192 | .sin6_family = AF_INET6, |
| 193 | .sin6_addr = *plat_subnet, |
| 194 | }; |
| 195 | dst.sin6_addr.s6_addr32[3] = plat_suffix; |
| 196 | if (connect(s, (struct sockaddr*)&dst, sizeof(dst))) { |
| 197 | int ret = errno; |
| 198 | ALOGE("connect() failed: %s", strerror(errno)); |
| 199 | close(s); |
| 200 | return -ret; |
| 201 | } |
| 202 | |
| 203 | // Fetch the socket's IPv6 mtu - this is effectively fetching mtu from routing table |
| 204 | int mtu; |
| 205 | socklen_t sz_mtu = sizeof(mtu); |
| 206 | if (getsockopt(s, SOL_IPV6, IPV6_MTU, &mtu, &sz_mtu)) { |
| 207 | int ret = errno; |
| 208 | ALOGE("getsockopt(SOL_IPV6, IPV6_MTU) failed: %s", strerror(errno)); |
| 209 | close(s); |
| 210 | return -ret; |
| 211 | } |
| 212 | if (sz_mtu != sizeof(mtu)) { |
| 213 | ALOGE("getsockopt(SOL_IPV6, IPV6_MTU) returned unexpected size: %d", sz_mtu); |
| 214 | close(s); |
| 215 | return -EFAULT; |
| 216 | } |
| 217 | close(s); |
| 218 | |
| 219 | return mtu; |
| 220 | } |
| 221 | |
Hungming Chen | 8ff032b | 2021-12-17 21:12:53 +0800 | [diff] [blame] | 222 | /* function: configure_packet_socket |
| 223 | * Binds the packet socket and attaches the receive filter to it. |
| 224 | * sock - the socket to configure |
| 225 | * addr - the IP address to filter |
| 226 | * ifindex - index of interface to add the filter to |
| 227 | * returns: 0 on success, -errno on failure |
| 228 | */ |
| 229 | int configure_packet_socket(int sock, in6_addr* addr, int ifindex) { |
| 230 | uint32_t* ipv6 = addr->s6_addr32; |
| 231 | |
| 232 | // clang-format off |
| 233 | struct sock_filter filter_code[] = { |
| 234 | // Load the first four bytes of the IPv6 destination address (starts 24 bytes in). |
| 235 | // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads |
| 236 | // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it |
| 237 | // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other |
| 238 | // three words of the IPv6 address, and if they all match, return PACKETLEN (accept packet). |
| 239 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24), |
| 240 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7), |
| 241 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28), |
| 242 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5), |
| 243 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32), |
| 244 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3), |
| 245 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36), |
| 246 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1), |
| 247 | BPF_STMT(BPF_RET | BPF_K, PACKETLEN), |
| 248 | BPF_STMT(BPF_RET | BPF_K, 0), |
| 249 | }; |
| 250 | // clang-format on |
| 251 | struct sock_fprog filter = {sizeof(filter_code) / sizeof(filter_code[0]), filter_code}; |
| 252 | |
| 253 | if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) { |
| 254 | int res = errno; |
| 255 | ALOGE("attach packet filter failed: %s", strerror(errno)); |
| 256 | return -res; |
| 257 | } |
| 258 | |
| 259 | struct sockaddr_ll sll = { |
| 260 | .sll_family = AF_PACKET, |
| 261 | .sll_protocol = htons(ETH_P_IPV6), |
| 262 | .sll_ifindex = ifindex, |
| 263 | .sll_pkttype = |
| 264 | PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel. |
| 265 | }; |
| 266 | if (bind(sock, (struct sockaddr*)&sll, sizeof(sll))) { |
| 267 | int res = errno; |
| 268 | ALOGE("binding packet socket: %s", strerror(errno)); |
| 269 | return -res; |
| 270 | } |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 275 | } // namespace clat |
| 276 | } // namespace net |
| 277 | } // namespace android |