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 | |
| 32 | namespace android { |
| 33 | namespace net { |
| 34 | namespace clat { |
| 35 | |
Hungming Chen | 2f623f3 | 2021-12-17 17:24:58 +0800 | [diff] [blame] | 36 | bool 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 |
| 62 | in_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. |
| 68 | in_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 Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 98 | // Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix. |
| 99 | void 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 Chen | 6139d87 | 2021-12-17 15:47:26 +0800 | [diff] [blame] | 120 | // Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix. |
| 121 | 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] | 122 | in6_addr* v6, uint32_t mark) { |
Hungming Chen | 6139d87 | 2021-12-17 15:47:26 +0800 | [diff] [blame] | 123 | int s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 124 | if (s == -1) return -errno; |
| 125 | |
t-m-w | 130e75b | 2022-10-24 02:54:07 +0000 | [diff] [blame] | 126 | // Socket's mark affects routing decisions (network selection) |
| 127 | // An fwmark is necessary for clat to bypass the VPN during initialization. |
Maciej Żenczykowski | 8bf5967 | 2023-01-17 23:52:02 +0000 | [diff] [blame] | 128 | if (setsockopt(s, SOL_SOCKET, SO_MARK, &mark, sizeof(mark))) { |
t-m-w | 130e75b | 2022-10-24 02:54:07 +0000 | [diff] [blame] | 129 | int ret = errno; |
| 130 | ALOGE("setsockopt(SOL_SOCKET, SO_MARK) failed: %s", strerror(errno)); |
| 131 | close(s); |
| 132 | return -ret; |
| 133 | } |
| 134 | |
Hungming Chen | 6139d87 | 2021-12-17 15:47:26 +0800 | [diff] [blame] | 135 | if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) { |
| 136 | close(s); |
| 137 | return -errno; |
| 138 | } |
| 139 | |
| 140 | sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix}; |
| 141 | if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) { |
| 142 | close(s); |
| 143 | return -errno; |
| 144 | } |
| 145 | |
| 146 | socklen_t len = sizeof(sin6); |
| 147 | if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) { |
| 148 | close(s); |
| 149 | return -errno; |
| 150 | } |
| 151 | |
| 152 | *v6 = sin6.sin6_addr; |
| 153 | |
| 154 | if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) || |
| 155 | IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) { |
| 156 | close(s); |
| 157 | return -ENETUNREACH; |
| 158 | } |
| 159 | |
| 160 | makeChecksumNeutral(v6, v4, nat64Prefix); |
| 161 | close(s); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Hungming Chen | 86a56de | 2021-12-17 19:40:59 +0800 | [diff] [blame] | 166 | int detect_mtu(const struct in6_addr* plat_subnet, uint32_t plat_suffix, uint32_t mark) { |
| 167 | // Create an IPv6 UDP socket. |
| 168 | int s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 169 | if (s < 0) { |
| 170 | int ret = errno; |
| 171 | ALOGE("socket(AF_INET6, SOCK_DGRAM, 0) failed: %s", strerror(errno)); |
| 172 | return -ret; |
| 173 | } |
| 174 | |
| 175 | // Socket's mark affects routing decisions (network selection) |
Maciej Żenczykowski | 8bf5967 | 2023-01-17 23:52:02 +0000 | [diff] [blame] | 176 | if (setsockopt(s, SOL_SOCKET, SO_MARK, &mark, sizeof(mark))) { |
Hungming Chen | 86a56de | 2021-12-17 19:40:59 +0800 | [diff] [blame] | 177 | int ret = errno; |
| 178 | ALOGE("setsockopt(SOL_SOCKET, SO_MARK) failed: %s", strerror(errno)); |
| 179 | close(s); |
| 180 | return -ret; |
| 181 | } |
| 182 | |
| 183 | // Try to connect udp socket to plat_subnet(96 bits):plat_suffix(32 bits) |
| 184 | struct sockaddr_in6 dst = { |
| 185 | .sin6_family = AF_INET6, |
| 186 | .sin6_addr = *plat_subnet, |
| 187 | }; |
| 188 | dst.sin6_addr.s6_addr32[3] = plat_suffix; |
| 189 | if (connect(s, (struct sockaddr*)&dst, sizeof(dst))) { |
| 190 | int ret = errno; |
| 191 | ALOGE("connect() failed: %s", strerror(errno)); |
| 192 | close(s); |
| 193 | return -ret; |
| 194 | } |
| 195 | |
| 196 | // Fetch the socket's IPv6 mtu - this is effectively fetching mtu from routing table |
| 197 | int mtu; |
| 198 | socklen_t sz_mtu = sizeof(mtu); |
| 199 | if (getsockopt(s, SOL_IPV6, IPV6_MTU, &mtu, &sz_mtu)) { |
| 200 | int ret = errno; |
| 201 | ALOGE("getsockopt(SOL_IPV6, IPV6_MTU) failed: %s", strerror(errno)); |
| 202 | close(s); |
| 203 | return -ret; |
| 204 | } |
| 205 | if (sz_mtu != sizeof(mtu)) { |
| 206 | ALOGE("getsockopt(SOL_IPV6, IPV6_MTU) returned unexpected size: %d", sz_mtu); |
| 207 | close(s); |
| 208 | return -EFAULT; |
| 209 | } |
| 210 | close(s); |
| 211 | |
| 212 | return mtu; |
| 213 | } |
| 214 | |
Hungming Chen | 8ff032b | 2021-12-17 21:12:53 +0800 | [diff] [blame] | 215 | /* function: configure_packet_socket |
| 216 | * Binds the packet socket and attaches the receive filter to it. |
| 217 | * sock - the socket to configure |
| 218 | * addr - the IP address to filter |
| 219 | * ifindex - index of interface to add the filter to |
| 220 | * returns: 0 on success, -errno on failure |
| 221 | */ |
| 222 | int configure_packet_socket(int sock, in6_addr* addr, int ifindex) { |
| 223 | uint32_t* ipv6 = addr->s6_addr32; |
| 224 | |
| 225 | // clang-format off |
| 226 | struct sock_filter filter_code[] = { |
| 227 | // Load the first four bytes of the IPv6 destination address (starts 24 bytes in). |
| 228 | // Compare it against the first four bytes of our IPv6 address, in host byte order (BPF loads |
| 229 | // are always in host byte order). If it matches, continue with next instruction (JMP 0). If it |
| 230 | // doesn't match, jump ahead to statement that returns 0 (ignore packet). Repeat for the other |
Maciej Żenczykowski | 4fcf8a0 | 2023-01-17 23:49:55 +0000 | [diff] [blame] | 231 | // three words of the IPv6 address, and if they all match, return full packet (accept packet). |
Hungming Chen | 8ff032b | 2021-12-17 21:12:53 +0800 | [diff] [blame] | 232 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 24), |
| 233 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[0]), 0, 7), |
| 234 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 28), |
| 235 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[1]), 0, 5), |
| 236 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 32), |
| 237 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[2]), 0, 3), |
| 238 | BPF_STMT(BPF_LD | BPF_W | BPF_ABS, 36), |
| 239 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htonl(ipv6[3]), 0, 1), |
Maciej Żenczykowski | 4fcf8a0 | 2023-01-17 23:49:55 +0000 | [diff] [blame] | 240 | BPF_STMT(BPF_RET | BPF_K, 0xFFFFFFFF), |
Hungming Chen | 8ff032b | 2021-12-17 21:12:53 +0800 | [diff] [blame] | 241 | BPF_STMT(BPF_RET | BPF_K, 0), |
| 242 | }; |
| 243 | // clang-format on |
| 244 | struct sock_fprog filter = {sizeof(filter_code) / sizeof(filter_code[0]), filter_code}; |
| 245 | |
| 246 | if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) { |
| 247 | int res = errno; |
| 248 | ALOGE("attach packet filter failed: %s", strerror(errno)); |
| 249 | return -res; |
| 250 | } |
| 251 | |
| 252 | struct sockaddr_ll sll = { |
| 253 | .sll_family = AF_PACKET, |
| 254 | .sll_protocol = htons(ETH_P_IPV6), |
| 255 | .sll_ifindex = ifindex, |
| 256 | .sll_pkttype = |
| 257 | PACKET_OTHERHOST, // The 464xlat IPv6 address is not assigned to the kernel. |
| 258 | }; |
| 259 | if (bind(sock, (struct sockaddr*)&sll, sizeof(sll))) { |
| 260 | int res = errno; |
| 261 | ALOGE("binding packet socket: %s", strerror(errno)); |
| 262 | return -res; |
| 263 | } |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 268 | } // namespace clat |
| 269 | } // namespace net |
| 270 | } // namespace android |