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> |
Hungming Chen | 8ff032b | 2021-12-17 21:12:53 +0800 | [diff] [blame] | 20 | #include <linux/if_packet.h> |
| 21 | #include <linux/if_tun.h> |
Hungming Chen | 2477caa | 2022-06-05 22:12:22 +0800 | [diff] [blame] | 22 | #include <netinet/in.h> |
Hungming Chen | 8ff032b | 2021-12-17 21:12:53 +0800 | [diff] [blame] | 23 | #include "tun_interface.h" |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 24 | |
| 25 | extern "C" { |
| 26 | #include "checksum.h" |
| 27 | } |
| 28 | |
| 29 | // Default translation parameters. |
| 30 | static const char kIPv4LocalAddr[] = "192.0.0.4"; |
| 31 | |
| 32 | namespace android { |
| 33 | namespace net { |
| 34 | namespace clat { |
| 35 | |
Hungming Chen | 8ff032b | 2021-12-17 21:12:53 +0800 | [diff] [blame] | 36 | using android::net::TunInterface; |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 37 | using base::StringPrintf; |
| 38 | |
| 39 | class ClatUtils : public ::testing::Test {}; |
| 40 | |
Hungming Chen | 2f623f3 | 2021-12-17 17:24:58 +0800 | [diff] [blame] | 41 | // Mock functions for isIpv4AddressFree. |
| 42 | bool neverFree(in_addr_t /* addr */) { |
| 43 | return 0; |
| 44 | } |
| 45 | bool alwaysFree(in_addr_t /* addr */) { |
| 46 | return 1; |
| 47 | } |
| 48 | bool only2Free(in_addr_t addr) { |
| 49 | return (ntohl(addr) & 0xff) == 2; |
| 50 | } |
| 51 | bool over6Free(in_addr_t addr) { |
| 52 | return (ntohl(addr) & 0xff) >= 6; |
| 53 | } |
| 54 | bool only10Free(in_addr_t addr) { |
| 55 | return (ntohl(addr) & 0xff) == 10; |
| 56 | } |
| 57 | |
| 58 | // Apply mocked isIpv4AddressFree function for selectIpv4Address test. |
| 59 | in_addr_t selectIpv4Address(const in_addr ip, int16_t prefixlen, |
| 60 | isIpv4AddrFreeFn fn /* mocked function */) { |
| 61 | // Call internal function to replace isIpv4AddressFreeFn for testing. |
| 62 | return selectIpv4AddressInternal(ip, prefixlen, fn); |
| 63 | } |
| 64 | |
| 65 | TEST_F(ClatUtils, SelectIpv4Address) { |
| 66 | struct in_addr addr; |
| 67 | |
| 68 | inet_pton(AF_INET, kIPv4LocalAddr, &addr); |
| 69 | |
| 70 | // If no addresses are free, return INADDR_NONE. |
| 71 | EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 29, neverFree)); |
| 72 | EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 16, neverFree)); |
| 73 | |
| 74 | // If the configured address is free, pick that. But a prefix that's too big is invalid. |
| 75 | EXPECT_EQ(inet_addr(kIPv4LocalAddr), selectIpv4Address(addr, 29, alwaysFree)); |
| 76 | EXPECT_EQ(inet_addr(kIPv4LocalAddr), selectIpv4Address(addr, 20, alwaysFree)); |
| 77 | EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 15, alwaysFree)); |
| 78 | |
| 79 | // A prefix length of 32 works, but anything above it is invalid. |
| 80 | EXPECT_EQ(inet_addr(kIPv4LocalAddr), selectIpv4Address(addr, 32, alwaysFree)); |
| 81 | EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 33, alwaysFree)); |
| 82 | |
| 83 | // If another address is free, pick it. |
| 84 | EXPECT_EQ(inet_addr("192.0.0.6"), selectIpv4Address(addr, 29, over6Free)); |
| 85 | |
| 86 | // Check that we wrap around to addresses that are lower than the first address. |
| 87 | EXPECT_EQ(inet_addr("192.0.0.2"), selectIpv4Address(addr, 29, only2Free)); |
| 88 | EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 30, only2Free)); |
| 89 | |
| 90 | // If a free address exists outside the prefix, we don't pick it. |
| 91 | EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 29, only10Free)); |
| 92 | EXPECT_EQ(inet_addr("192.0.0.10"), selectIpv4Address(addr, 24, only10Free)); |
| 93 | |
| 94 | // Now try using the real function which sees if IP addresses are free using bind(). |
| 95 | // Assume that the machine running the test has the address 127.0.0.1, but not 8.8.8.8. |
| 96 | addr.s_addr = inet_addr("8.8.8.8"); |
| 97 | EXPECT_EQ(inet_addr("8.8.8.8"), selectIpv4Address(addr, 29)); |
| 98 | |
| 99 | addr.s_addr = inet_addr("127.0.0.1"); |
| 100 | EXPECT_EQ(inet_addr("127.0.0.2"), selectIpv4Address(addr, 29)); |
| 101 | } |
| 102 | |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 103 | TEST_F(ClatUtils, MakeChecksumNeutral) { |
| 104 | // We can't test generateIPv6Address here since it requires manipulating routing, which we can't |
| 105 | // do without talking to the real netd on the system. |
| 106 | uint32_t rand = arc4random_uniform(0xffffffff); |
| 107 | uint16_t rand1 = rand & 0xffff; |
| 108 | uint16_t rand2 = (rand >> 16) & 0xffff; |
| 109 | std::string v6PrefixStr = StringPrintf("2001:db8:%x:%x", rand1, rand2); |
| 110 | std::string v6InterfaceAddrStr = StringPrintf("%s::%x:%x", v6PrefixStr.c_str(), rand2, rand1); |
| 111 | std::string nat64PrefixStr = StringPrintf("2001:db8:%x:%x::", rand2, rand1); |
| 112 | |
| 113 | in_addr v4 = {inet_addr(kIPv4LocalAddr)}; |
| 114 | in6_addr v6InterfaceAddr; |
| 115 | ASSERT_TRUE(inet_pton(AF_INET6, v6InterfaceAddrStr.c_str(), &v6InterfaceAddr)); |
| 116 | in6_addr nat64Prefix; |
| 117 | ASSERT_TRUE(inet_pton(AF_INET6, nat64PrefixStr.c_str(), &nat64Prefix)); |
| 118 | |
| 119 | // Generate a boatload of random IIDs. |
| 120 | int onebits = 0; |
| 121 | uint64_t prev_iid = 0; |
| 122 | for (int i = 0; i < 100000; i++) { |
| 123 | in6_addr v6 = v6InterfaceAddr; |
| 124 | makeChecksumNeutral(&v6, v4, nat64Prefix); |
| 125 | |
| 126 | // Check the generated IP address is in the same prefix as the interface IPv6 address. |
| 127 | EXPECT_EQ(0, memcmp(&v6, &v6InterfaceAddr, 8)); |
| 128 | |
| 129 | // Check that consecutive IIDs are not the same. |
| 130 | uint64_t iid = *(uint64_t*)(&v6.s6_addr[8]); |
| 131 | ASSERT_TRUE(iid != prev_iid) |
| 132 | << "Two consecutive random IIDs are the same: " << std::showbase << std::hex << iid |
| 133 | << "\n"; |
| 134 | prev_iid = iid; |
| 135 | |
| 136 | // Check that the IID is checksum-neutral with the NAT64 prefix and the |
| 137 | // local prefix. |
| 138 | uint16_t c1 = ip_checksum_finish(ip_checksum_add(0, &v4, sizeof(v4))); |
| 139 | uint16_t c2 = ip_checksum_finish(ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) + |
| 140 | ip_checksum_add(0, &v6, sizeof(v6))); |
| 141 | |
| 142 | if (c1 != c2) { |
| 143 | char v6Str[INET6_ADDRSTRLEN]; |
| 144 | inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str)); |
| 145 | FAIL() << "Bad IID: " << v6Str << " not checksum-neutral with " << kIPv4LocalAddr |
| 146 | << " and " << nat64PrefixStr.c_str() << std::showbase << std::hex |
| 147 | << "\n IPv4 checksum: " << c1 << "\n IPv6 checksum: " << c2 << "\n"; |
| 148 | } |
| 149 | |
| 150 | // Check that IIDs are roughly random and use all the bits by counting the |
| 151 | // total number of bits set to 1 in a random sample of 100000 generated IIDs. |
| 152 | onebits += __builtin_popcountll(*(uint64_t*)&iid); |
| 153 | } |
| 154 | EXPECT_LE(3190000, onebits); |
| 155 | EXPECT_GE(3210000, onebits); |
| 156 | } |
| 157 | |
Hungming Chen | 86a56de | 2021-12-17 19:40:59 +0800 | [diff] [blame] | 158 | TEST_F(ClatUtils, DetectMtu) { |
| 159 | // ::1 with bottom 32 bits set to 1 is still ::1 which routes via lo with mtu of 64KiB |
| 160 | ASSERT_EQ(detect_mtu(&in6addr_loopback, htonl(1), 0 /*MARK_UNSET*/), 65536); |
| 161 | } |
| 162 | |
Hungming Chen | 8ff032b | 2021-12-17 21:12:53 +0800 | [diff] [blame] | 163 | TEST_F(ClatUtils, ConfigurePacketSocket) { |
| 164 | // Create an interface for configure_packet_socket to attach socket filter to. |
| 165 | TunInterface v6Iface; |
| 166 | ASSERT_EQ(0, v6Iface.init()); |
| 167 | |
Maciej Żenczykowski | 2460294 | 2023-03-08 03:24:41 +0000 | [diff] [blame] | 168 | const int s = socket(AF_PACKET, SOCK_RAW | SOCK_CLOEXEC, htons(ETH_P_IPV6)); |
Hungming Chen | 8ff032b | 2021-12-17 21:12:53 +0800 | [diff] [blame] | 169 | EXPECT_LE(0, s); |
| 170 | struct in6_addr addr6; |
| 171 | EXPECT_EQ(1, inet_pton(AF_INET6, "2001:db8::f00", &addr6)); |
| 172 | EXPECT_EQ(0, configure_packet_socket(s, &addr6, v6Iface.ifindex())); |
| 173 | |
| 174 | // Check that the packet socket is bound to the interface. We can't check the socket filter |
| 175 | // because there is no way to fetch it from the kernel. |
| 176 | sockaddr_ll sll; |
| 177 | socklen_t len = sizeof(sll); |
| 178 | ASSERT_EQ(0, getsockname(s, reinterpret_cast<sockaddr*>(&sll), &len)); |
| 179 | EXPECT_EQ(htons(ETH_P_IPV6), sll.sll_protocol); |
| 180 | EXPECT_EQ(sll.sll_ifindex, v6Iface.ifindex()); |
| 181 | |
| 182 | close(s); |
| 183 | v6Iface.destroy(); |
| 184 | } |
| 185 | |
Hungming Chen | 2477caa | 2022-06-05 22:12:22 +0800 | [diff] [blame] | 186 | // This is not a realistic test because we can't test generateIPv6Address here since it requires |
| 187 | // manipulating routing, which we can't do without talking to the real netd on the system. |
| 188 | // See test MakeChecksumNeutral. |
| 189 | // TODO: remove this test once EthernetTetheringTest can test on mainline test coverage branch. |
| 190 | TEST_F(ClatUtils, GenerateIpv6AddressFailWithUlaSocketAddress) { |
| 191 | // Create an interface for generateIpv6Address to connect to. |
| 192 | TunInterface tun; |
| 193 | ASSERT_EQ(0, tun.init()); |
| 194 | |
| 195 | // Unused because v6 address is ULA and makeChecksumNeutral has never called. |
| 196 | in_addr v4 = {inet_addr(kIPv4LocalAddr)}; |
| 197 | // Not a NAT64 prefix because test can't connect to in generateIpv6Address. |
| 198 | // Used to be a fake address from the tun interface for generating an IPv6 socket address. |
| 199 | // nat64Prefix won't be used in MakeChecksumNeutral because MakeChecksumNeutral has never |
| 200 | // be called. |
| 201 | in6_addr nat64Prefix = tun.dstAddr(); // not realistic |
| 202 | in6_addr v6; |
| 203 | EXPECT_EQ(1, inet_pton(AF_INET6, "::", &v6)); // initialize as zero |
| 204 | |
t-m-w | 130e75b | 2022-10-24 02:54:07 +0000 | [diff] [blame] | 205 | // 0u is MARK_UNSET |
| 206 | EXPECT_EQ(-ENETUNREACH, generateIpv6Address(tun.name().c_str(), v4, nat64Prefix, &v6, 0u)); |
Hungming Chen | 2477caa | 2022-06-05 22:12:22 +0800 | [diff] [blame] | 207 | EXPECT_TRUE(IN6_IS_ADDR_ULA(&v6)); |
| 208 | |
| 209 | tun.destroy(); |
| 210 | } |
| 211 | |
Hungming Chen | ed7b460 | 2021-12-17 15:03:47 +0800 | [diff] [blame] | 212 | } // namespace clat |
| 213 | } // namespace net |
| 214 | } // namespace android |