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 | |
| 15 | #include "libclat/clatutils.h" |
| 16 | |
Hungming Chen | 6139d87 | 2021-12-17 15:47:26 +0800 | [diff] [blame] | 17 | #include <errno.h> |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 18 | #include <stdlib.h> |
Hungming Chen | 6139d87 | 2021-12-17 15:47:26 +0800 | [diff] [blame] | 19 | #include <string.h> |
| 20 | #include <unistd.h> |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 21 | |
| 22 | extern "C" { |
| 23 | #include "checksum.h" |
| 24 | } |
| 25 | |
| 26 | namespace android { |
| 27 | namespace net { |
| 28 | namespace clat { |
| 29 | |
Hungming Chen | 2f623f3 | 2021-12-17 17:24:58 +0800 | [diff] [blame^] | 30 | bool isIpv4AddressFree(in_addr_t addr) { |
| 31 | int s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 32 | if (s == -1) { |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | // Attempt to connect to the address. If the connection succeeds and getsockname returns the |
| 37 | // same then the address is already assigned to the system and we can't use it. |
| 38 | struct sockaddr_in sin = { |
| 39 | .sin_family = AF_INET, |
| 40 | .sin_port = htons(53), |
| 41 | .sin_addr = {addr}, |
| 42 | }; |
| 43 | socklen_t len = sizeof(sin); |
| 44 | bool inuse = connect(s, (struct sockaddr*)&sin, sizeof(sin)) == 0 && |
| 45 | getsockname(s, (struct sockaddr*)&sin, &len) == 0 && (size_t)len >= sizeof(sin) && |
| 46 | sin.sin_addr.s_addr == addr; |
| 47 | |
| 48 | close(s); |
| 49 | return !inuse; |
| 50 | } |
| 51 | |
| 52 | // Picks a free IPv4 address, starting from ip and trying all addresses in the prefix in order. |
| 53 | // ip - the IP address from the configuration file |
| 54 | // prefixlen - the length of the prefix from which addresses may be selected. |
| 55 | // returns: the IPv4 address, or INADDR_NONE if no addresses were available |
| 56 | in_addr_t selectIpv4Address(const in_addr ip, int16_t prefixlen) { |
| 57 | return selectIpv4AddressInternal(ip, prefixlen, isIpv4AddressFree); |
| 58 | } |
| 59 | |
| 60 | // Only allow testing to use this function directly. Otherwise call selectIpv4Address(ip, pfxlen) |
| 61 | // which has applied valid isIpv4AddressFree function pointer. |
| 62 | in_addr_t selectIpv4AddressInternal(const in_addr ip, int16_t prefixlen, |
| 63 | isIpv4AddrFreeFn isIpv4AddressFreeFunc) { |
| 64 | // Impossible! Only test allows to apply fn. |
| 65 | if (isIpv4AddressFreeFunc == nullptr) { |
| 66 | return INADDR_NONE; |
| 67 | } |
| 68 | |
| 69 | // Don't accept prefixes that are too large because we scan addresses one by one. |
| 70 | if (prefixlen < 16 || prefixlen > 32) { |
| 71 | return INADDR_NONE; |
| 72 | } |
| 73 | |
| 74 | // All these are in host byte order. |
| 75 | in_addr_t mask = 0xffffffff >> (32 - prefixlen) << (32 - prefixlen); |
| 76 | in_addr_t ipv4 = ntohl(ip.s_addr); |
| 77 | in_addr_t first_ipv4 = ipv4; |
| 78 | in_addr_t prefix = ipv4 & mask; |
| 79 | |
| 80 | // Pick the first IPv4 address in the pool, wrapping around if necessary. |
| 81 | // So, for example, 192.0.0.4 -> 192.0.0.5 -> 192.0.0.6 -> 192.0.0.7 -> 192.0.0.0. |
| 82 | do { |
| 83 | if (isIpv4AddressFreeFunc(htonl(ipv4))) { |
| 84 | return htonl(ipv4); |
| 85 | } |
| 86 | ipv4 = prefix | ((ipv4 + 1) & ~mask); |
| 87 | } while (ipv4 != first_ipv4); |
| 88 | |
| 89 | return INADDR_NONE; |
| 90 | } |
| 91 | |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 92 | // Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix. |
| 93 | void makeChecksumNeutral(in6_addr* v6, const in_addr v4, const in6_addr& nat64Prefix) { |
| 94 | // Fill last 8 bytes of IPv6 address with random bits. |
| 95 | arc4random_buf(&v6->s6_addr[8], 8); |
| 96 | |
| 97 | // Make the IID checksum-neutral. That is, make it so that: |
| 98 | // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6) |
| 99 | // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4): |
| 100 | // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix) |
| 101 | // Do this by adjusting the two bytes in the middle of the IID. |
| 102 | |
| 103 | uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12]; |
| 104 | |
| 105 | uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4)); |
| 106 | uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) + |
| 107 | ip_checksum_add(0, v6, sizeof(*v6)); |
| 108 | |
| 109 | uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2); |
| 110 | v6->s6_addr[11] = delta >> 8; |
| 111 | v6->s6_addr[12] = delta & 0xff; |
| 112 | } |
| 113 | |
Hungming Chen | 6139d87 | 2021-12-17 15:47:26 +0800 | [diff] [blame] | 114 | // Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix. |
| 115 | int generateIpv6Address(const char* iface, const in_addr v4, const in6_addr& nat64Prefix, |
| 116 | in6_addr* v6) { |
| 117 | int s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 118 | if (s == -1) return -errno; |
| 119 | |
| 120 | if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) { |
| 121 | close(s); |
| 122 | return -errno; |
| 123 | } |
| 124 | |
| 125 | sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix}; |
| 126 | if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) { |
| 127 | close(s); |
| 128 | return -errno; |
| 129 | } |
| 130 | |
| 131 | socklen_t len = sizeof(sin6); |
| 132 | if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) { |
| 133 | close(s); |
| 134 | return -errno; |
| 135 | } |
| 136 | |
| 137 | *v6 = sin6.sin6_addr; |
| 138 | |
| 139 | if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) || |
| 140 | IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) { |
| 141 | close(s); |
| 142 | return -ENETUNREACH; |
| 143 | } |
| 144 | |
| 145 | makeChecksumNeutral(v6, v4, nat64Prefix); |
| 146 | close(s); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 151 | } // namespace clat |
| 152 | } // namespace net |
| 153 | } // namespace android |