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 | |
| 30 | // Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix. |
| 31 | void makeChecksumNeutral(in6_addr* v6, const in_addr v4, const in6_addr& nat64Prefix) { |
| 32 | // Fill last 8 bytes of IPv6 address with random bits. |
| 33 | arc4random_buf(&v6->s6_addr[8], 8); |
| 34 | |
| 35 | // Make the IID checksum-neutral. That is, make it so that: |
| 36 | // checksum(Local IPv4 | Remote IPv4) = checksum(Local IPv6 | Remote IPv6) |
| 37 | // in other words (because remote IPv6 = NAT64 prefix | Remote IPv4): |
| 38 | // checksum(Local IPv4) = checksum(Local IPv6 | NAT64 prefix) |
| 39 | // Do this by adjusting the two bytes in the middle of the IID. |
| 40 | |
| 41 | uint16_t middlebytes = (v6->s6_addr[11] << 8) + v6->s6_addr[12]; |
| 42 | |
| 43 | uint32_t c1 = ip_checksum_add(0, &v4, sizeof(v4)); |
| 44 | uint32_t c2 = ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) + |
| 45 | ip_checksum_add(0, v6, sizeof(*v6)); |
| 46 | |
| 47 | uint16_t delta = ip_checksum_adjust(middlebytes, c1, c2); |
| 48 | v6->s6_addr[11] = delta >> 8; |
| 49 | v6->s6_addr[12] = delta & 0xff; |
| 50 | } |
| 51 | |
Hungming Chen | 6139d87 | 2021-12-17 15:47:26 +0800 | [diff] [blame^] | 52 | // Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix. |
| 53 | int generateIpv6Address(const char* iface, const in_addr v4, const in6_addr& nat64Prefix, |
| 54 | in6_addr* v6) { |
| 55 | int s = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
| 56 | if (s == -1) return -errno; |
| 57 | |
| 58 | if (setsockopt(s, SOL_SOCKET, SO_BINDTODEVICE, iface, strlen(iface) + 1) == -1) { |
| 59 | close(s); |
| 60 | return -errno; |
| 61 | } |
| 62 | |
| 63 | sockaddr_in6 sin6 = {.sin6_family = AF_INET6, .sin6_addr = nat64Prefix}; |
| 64 | if (connect(s, reinterpret_cast<struct sockaddr*>(&sin6), sizeof(sin6)) == -1) { |
| 65 | close(s); |
| 66 | return -errno; |
| 67 | } |
| 68 | |
| 69 | socklen_t len = sizeof(sin6); |
| 70 | if (getsockname(s, reinterpret_cast<struct sockaddr*>(&sin6), &len) == -1) { |
| 71 | close(s); |
| 72 | return -errno; |
| 73 | } |
| 74 | |
| 75 | *v6 = sin6.sin6_addr; |
| 76 | |
| 77 | if (IN6_IS_ADDR_UNSPECIFIED(v6) || IN6_IS_ADDR_LOOPBACK(v6) || IN6_IS_ADDR_LINKLOCAL(v6) || |
| 78 | IN6_IS_ADDR_SITELOCAL(v6) || IN6_IS_ADDR_ULA(v6)) { |
| 79 | close(s); |
| 80 | return -ENETUNREACH; |
| 81 | } |
| 82 | |
| 83 | makeChecksumNeutral(v6, v4, nat64Prefix); |
| 84 | close(s); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 89 | } // namespace clat |
| 90 | } // namespace net |
| 91 | } // namespace android |