blob: 69076c55421efdaac73fc65a16e815b03275a6d4 [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
15#include "libclat/clatutils.h"
16
Hungming Chen6139d872021-12-17 15:47:26 +080017#include <errno.h>
Hungming Chened7b4602021-12-17 15:03:47 +080018#include <stdlib.h>
Hungming Chen6139d872021-12-17 15:47:26 +080019#include <string.h>
20#include <unistd.h>
Hungming Chened7b4602021-12-17 15:03:47 +080021
22extern "C" {
23#include "checksum.h"
24}
25
26namespace android {
27namespace net {
28namespace clat {
29
30// Alters the bits in the IPv6 address to make them checksum neutral with v4 and nat64Prefix.
31void 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 Chen6139d872021-12-17 15:47:26 +080052// Picks a random interface ID that is checksum neutral with the IPv4 address and the NAT64 prefix.
53int 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 Chened7b4602021-12-17 15:03:47 +080089} // namespace clat
90} // namespace net
91} // namespace android