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 | |
| 17 | #include <android-base/stringprintf.h> |
| 18 | #include <arpa/inet.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | extern "C" { |
| 22 | #include "checksum.h" |
| 23 | } |
| 24 | |
| 25 | // Default translation parameters. |
| 26 | static const char kIPv4LocalAddr[] = "192.0.0.4"; |
| 27 | |
| 28 | namespace android { |
| 29 | namespace net { |
| 30 | namespace clat { |
| 31 | |
| 32 | using base::StringPrintf; |
| 33 | |
| 34 | class ClatUtils : public ::testing::Test {}; |
| 35 | |
| 36 | TEST_F(ClatUtils, MakeChecksumNeutral) { |
| 37 | // We can't test generateIPv6Address here since it requires manipulating routing, which we can't |
| 38 | // do without talking to the real netd on the system. |
| 39 | uint32_t rand = arc4random_uniform(0xffffffff); |
| 40 | uint16_t rand1 = rand & 0xffff; |
| 41 | uint16_t rand2 = (rand >> 16) & 0xffff; |
| 42 | std::string v6PrefixStr = StringPrintf("2001:db8:%x:%x", rand1, rand2); |
| 43 | std::string v6InterfaceAddrStr = StringPrintf("%s::%x:%x", v6PrefixStr.c_str(), rand2, rand1); |
| 44 | std::string nat64PrefixStr = StringPrintf("2001:db8:%x:%x::", rand2, rand1); |
| 45 | |
| 46 | in_addr v4 = {inet_addr(kIPv4LocalAddr)}; |
| 47 | in6_addr v6InterfaceAddr; |
| 48 | ASSERT_TRUE(inet_pton(AF_INET6, v6InterfaceAddrStr.c_str(), &v6InterfaceAddr)); |
| 49 | in6_addr nat64Prefix; |
| 50 | ASSERT_TRUE(inet_pton(AF_INET6, nat64PrefixStr.c_str(), &nat64Prefix)); |
| 51 | |
| 52 | // Generate a boatload of random IIDs. |
| 53 | int onebits = 0; |
| 54 | uint64_t prev_iid = 0; |
| 55 | for (int i = 0; i < 100000; i++) { |
| 56 | in6_addr v6 = v6InterfaceAddr; |
| 57 | makeChecksumNeutral(&v6, v4, nat64Prefix); |
| 58 | |
| 59 | // Check the generated IP address is in the same prefix as the interface IPv6 address. |
| 60 | EXPECT_EQ(0, memcmp(&v6, &v6InterfaceAddr, 8)); |
| 61 | |
| 62 | // Check that consecutive IIDs are not the same. |
| 63 | uint64_t iid = *(uint64_t*)(&v6.s6_addr[8]); |
| 64 | ASSERT_TRUE(iid != prev_iid) |
| 65 | << "Two consecutive random IIDs are the same: " << std::showbase << std::hex << iid |
| 66 | << "\n"; |
| 67 | prev_iid = iid; |
| 68 | |
| 69 | // Check that the IID is checksum-neutral with the NAT64 prefix and the |
| 70 | // local prefix. |
| 71 | uint16_t c1 = ip_checksum_finish(ip_checksum_add(0, &v4, sizeof(v4))); |
| 72 | uint16_t c2 = ip_checksum_finish(ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) + |
| 73 | ip_checksum_add(0, &v6, sizeof(v6))); |
| 74 | |
| 75 | if (c1 != c2) { |
| 76 | char v6Str[INET6_ADDRSTRLEN]; |
| 77 | inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str)); |
| 78 | FAIL() << "Bad IID: " << v6Str << " not checksum-neutral with " << kIPv4LocalAddr |
| 79 | << " and " << nat64PrefixStr.c_str() << std::showbase << std::hex |
| 80 | << "\n IPv4 checksum: " << c1 << "\n IPv6 checksum: " << c2 << "\n"; |
| 81 | } |
| 82 | |
| 83 | // Check that IIDs are roughly random and use all the bits by counting the |
| 84 | // total number of bits set to 1 in a random sample of 100000 generated IIDs. |
| 85 | onebits += __builtin_popcountll(*(uint64_t*)&iid); |
| 86 | } |
| 87 | EXPECT_LE(3190000, onebits); |
| 88 | EXPECT_GE(3210000, onebits); |
| 89 | } |
| 90 | |
| 91 | } // namespace clat |
| 92 | } // namespace net |
| 93 | } // namespace android |