github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/proc_net_udp.cc (about) 1 // Copyright 2019 Google LLC 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 <netinet/tcp.h> 16 #include <sys/socket.h> 17 #include <sys/stat.h> 18 #include <sys/types.h> 19 #include <unistd.h> 20 21 #include "gtest/gtest.h" 22 #include "absl/strings/numbers.h" 23 #include "absl/strings/str_join.h" 24 #include "absl/strings/str_split.h" 25 #include "test/syscalls/linux/ip_socket_test_util.h" 26 #include "test/util/file_descriptor.h" 27 #include "test/util/test_util.h" 28 29 namespace gvisor { 30 namespace testing { 31 namespace { 32 33 using absl::StrCat; 34 using absl::StrFormat; 35 using absl::StrSplit; 36 37 constexpr char kProcNetUDPHeader[] = 38 " sl local_address rem_address st tx_queue rx_queue tr tm->when " 39 "retrnsmt uid timeout inode ref pointer drops "; 40 41 // UDPEntry represents a single entry from /proc/net/udp. 42 struct UDPEntry { 43 uint32_t local_addr; 44 uint16_t local_port; 45 46 uint32_t remote_addr; 47 uint16_t remote_port; 48 49 uint64_t state; 50 uint64_t uid; 51 uint64_t inode; 52 }; 53 54 std::string DescribeFirstInetSocket(const SocketPair& sockets) { 55 const struct sockaddr* addr = sockets.first_addr(); 56 return StrFormat("First test socket: fd:%d %8X:%4X", sockets.first_fd(), 57 IPFromInetSockaddr(addr), PortFromInetSockaddr(addr)); 58 } 59 60 std::string DescribeSecondInetSocket(const SocketPair& sockets) { 61 const struct sockaddr* addr = sockets.second_addr(); 62 return StrFormat("Second test socket fd:%d %8X:%4X", sockets.second_fd(), 63 IPFromInetSockaddr(addr), PortFromInetSockaddr(addr)); 64 } 65 66 // Finds the first entry in 'entries' for which 'predicate' returns true. 67 // Returns true on match, and set 'match' to a copy of the matching entry. If 68 // 'match' is null, it's ignored. 69 bool FindBy(const std::vector<UDPEntry>& entries, UDPEntry* match, 70 std::function<bool(const UDPEntry&)> predicate) { 71 for (const UDPEntry& entry : entries) { 72 if (predicate(entry)) { 73 if (match != nullptr) { 74 *match = entry; 75 } 76 return true; 77 } 78 } 79 return false; 80 } 81 82 bool FindByLocalAddr(const std::vector<UDPEntry>& entries, UDPEntry* match, 83 const struct sockaddr* addr) { 84 uint32_t host = IPFromInetSockaddr(addr); 85 uint16_t port = PortFromInetSockaddr(addr); 86 return FindBy(entries, match, [host, port](const UDPEntry& e) { 87 return (e.local_addr == host && e.local_port == port); 88 }); 89 } 90 91 bool FindByRemoteAddr(const std::vector<UDPEntry>& entries, UDPEntry* match, 92 const struct sockaddr* addr) { 93 uint32_t host = IPFromInetSockaddr(addr); 94 uint16_t port = PortFromInetSockaddr(addr); 95 return FindBy(entries, match, [host, port](const UDPEntry& e) { 96 return (e.remote_addr == host && e.remote_port == port); 97 }); 98 } 99 100 PosixErrorOr<uint64_t> InodeFromSocketFD(int fd) { 101 ASSIGN_OR_RETURN_ERRNO(struct stat s, Fstat(fd)); 102 if (!S_ISSOCK(s.st_mode)) { 103 return PosixError(EINVAL, StrFormat("FD %d is not a socket", fd)); 104 } 105 return s.st_ino; 106 } 107 108 PosixErrorOr<bool> FindByFD(const std::vector<UDPEntry>& entries, 109 UDPEntry* match, int fd) { 110 ASSIGN_OR_RETURN_ERRNO(uint64_t inode, InodeFromSocketFD(fd)); 111 return FindBy(entries, match, 112 [inode](const UDPEntry& e) { return (e.inode == inode); }); 113 } 114 115 // Returns a parsed representation of /proc/net/udp entries. 116 PosixErrorOr<std::vector<UDPEntry>> ProcNetUDPEntries() { 117 std::string content; 118 RETURN_IF_ERRNO(GetContents("/proc/net/udp", &content)); 119 120 bool found_header = false; 121 std::vector<UDPEntry> entries; 122 std::vector<std::string> lines = StrSplit(content, '\n'); 123 std::cerr << "<contents of /proc/net/udp>" << std::endl; 124 for (const std::string& line : lines) { 125 std::cerr << line << std::endl; 126 127 if (!found_header) { 128 EXPECT_EQ(line, kProcNetUDPHeader); 129 found_header = true; 130 continue; 131 } 132 if (line.empty()) { 133 continue; 134 } 135 136 // Parse a single entry from /proc/net/udp. 137 // 138 // Example entries: 139 // 140 // clang-format off 141 // 142 // sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt uid timeout inode ref pointer drops 143 // 3503: 0100007F:0035 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 33317 2 0000000000000000 0 144 // 3518: 00000000:0044 00000000:0000 07 00000000:00000000 00:00000000 00000000 0 0 40394 2 0000000000000000 0 145 // ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 146 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 147 // 148 // clang-format on 149 150 UDPEntry entry; 151 std::vector<std::string> fields = 152 StrSplit(line, absl::ByAnyChar(": "), absl::SkipEmpty()); 153 154 ASSIGN_OR_RETURN_ERRNO(entry.local_addr, AtoiBase(fields[1], 16)); 155 ASSIGN_OR_RETURN_ERRNO(entry.local_port, AtoiBase(fields[2], 16)); 156 157 ASSIGN_OR_RETURN_ERRNO(entry.remote_addr, AtoiBase(fields[3], 16)); 158 ASSIGN_OR_RETURN_ERRNO(entry.remote_port, AtoiBase(fields[4], 16)); 159 160 ASSIGN_OR_RETURN_ERRNO(entry.state, AtoiBase(fields[5], 16)); 161 ASSIGN_OR_RETURN_ERRNO(entry.uid, Atoi<uint64_t>(fields[11])); 162 ASSIGN_OR_RETURN_ERRNO(entry.inode, Atoi<uint64_t>(fields[13])); 163 164 // Linux shares internal data structures between TCP and UDP sockets. The 165 // proc entries for UDP sockets share some fields with TCP sockets, but 166 // these fields should always be zero as they're not meaningful for UDP 167 // sockets. 168 EXPECT_EQ(fields[8], "00") << StrFormat("sl:%s, tr", fields[0]); 169 EXPECT_EQ(fields[9], "00000000") << StrFormat("sl:%s, tm->when", fields[0]); 170 EXPECT_EQ(fields[10], "00000000") 171 << StrFormat("sl:%s, retrnsmt", fields[0]); 172 EXPECT_EQ(fields[12], "0") << StrFormat("sl:%s, timeout", fields[0]); 173 174 entries.push_back(entry); 175 } 176 std::cerr << "<end of /proc/net/udp>" << std::endl; 177 178 return entries; 179 } 180 181 TEST(ProcNetUDP, Exists) { 182 const std::string content = 183 ASSERT_NO_ERRNO_AND_VALUE(GetContents("/proc/net/udp")); 184 const std::string header_line = StrCat(kProcNetUDPHeader, "\n"); 185 EXPECT_THAT(content, ::testing::StartsWith(header_line)); 186 } 187 188 TEST(ProcNetUDP, EntryUID) { 189 auto sockets = 190 ASSERT_NO_ERRNO_AND_VALUE(IPv4UDPBidirectionalBindSocketPair(0).Create()); 191 std::vector<UDPEntry> entries = 192 ASSERT_NO_ERRNO_AND_VALUE(ProcNetUDPEntries()); 193 UDPEntry e; 194 ASSERT_TRUE(FindByLocalAddr(entries, &e, sockets->first_addr())) 195 << DescribeFirstInetSocket(*sockets); 196 EXPECT_EQ(e.uid, geteuid()); 197 ASSERT_TRUE(FindByRemoteAddr(entries, &e, sockets->first_addr())) 198 << DescribeSecondInetSocket(*sockets); 199 EXPECT_EQ(e.uid, geteuid()); 200 } 201 202 TEST(ProcNetUDP, FindMutualEntries) { 203 auto sockets = 204 ASSERT_NO_ERRNO_AND_VALUE(IPv4UDPBidirectionalBindSocketPair(0).Create()); 205 std::vector<UDPEntry> entries = 206 ASSERT_NO_ERRNO_AND_VALUE(ProcNetUDPEntries()); 207 208 EXPECT_TRUE(FindByLocalAddr(entries, nullptr, sockets->first_addr())) 209 << DescribeFirstInetSocket(*sockets); 210 EXPECT_TRUE(FindByRemoteAddr(entries, nullptr, sockets->first_addr())) 211 << DescribeSecondInetSocket(*sockets); 212 213 EXPECT_TRUE(FindByLocalAddr(entries, nullptr, sockets->second_addr())) 214 << DescribeSecondInetSocket(*sockets); 215 EXPECT_TRUE(FindByRemoteAddr(entries, nullptr, sockets->second_addr())) 216 << DescribeFirstInetSocket(*sockets); 217 } 218 219 TEST(ProcNetUDP, EntriesRemovedOnClose) { 220 auto sockets = 221 ASSERT_NO_ERRNO_AND_VALUE(IPv4UDPBidirectionalBindSocketPair(0).Create()); 222 std::vector<UDPEntry> entries = 223 ASSERT_NO_ERRNO_AND_VALUE(ProcNetUDPEntries()); 224 225 EXPECT_TRUE(FindByLocalAddr(entries, nullptr, sockets->first_addr())) 226 << DescribeFirstInetSocket(*sockets); 227 EXPECT_TRUE(FindByLocalAddr(entries, nullptr, sockets->second_addr())) 228 << DescribeSecondInetSocket(*sockets); 229 230 EXPECT_THAT(close(sockets->release_first_fd()), SyscallSucceeds()); 231 entries = ASSERT_NO_ERRNO_AND_VALUE(ProcNetUDPEntries()); 232 // First socket's entry should be gone, but the second socket's entry should 233 // still exist. 234 EXPECT_FALSE(FindByLocalAddr(entries, nullptr, sockets->first_addr())) 235 << DescribeFirstInetSocket(*sockets); 236 EXPECT_TRUE(FindByLocalAddr(entries, nullptr, sockets->second_addr())) 237 << DescribeSecondInetSocket(*sockets); 238 239 EXPECT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 240 entries = ASSERT_NO_ERRNO_AND_VALUE(ProcNetUDPEntries()); 241 // Both entries should be gone. 242 EXPECT_FALSE(FindByLocalAddr(entries, nullptr, sockets->first_addr())) 243 << DescribeFirstInetSocket(*sockets); 244 EXPECT_FALSE(FindByLocalAddr(entries, nullptr, sockets->second_addr())) 245 << DescribeSecondInetSocket(*sockets); 246 } 247 248 PosixErrorOr<std::unique_ptr<FileDescriptor>> BoundUDPSocket() { 249 ASSIGN_OR_RETURN_ERRNO(std::unique_ptr<FileDescriptor> socket, 250 IPv4UDPUnboundSocket(0).Create()); 251 struct sockaddr_in addr; 252 addr.sin_family = AF_INET; 253 addr.sin_addr.s_addr = htonl(INADDR_ANY); 254 addr.sin_port = 0; 255 256 int res = bind(socket->get(), reinterpret_cast<const struct sockaddr*>(&addr), 257 sizeof(addr)); 258 if (res) { 259 return PosixError(errno, "bind()"); 260 } 261 return socket; 262 } 263 264 TEST(ProcNetUDP, BoundEntry) { 265 std::unique_ptr<FileDescriptor> socket = 266 ASSERT_NO_ERRNO_AND_VALUE(BoundUDPSocket()); 267 struct sockaddr addr; 268 socklen_t len = sizeof(addr); 269 ASSERT_THAT(getsockname(socket->get(), &addr, &len), SyscallSucceeds()); 270 uint16_t port = PortFromInetSockaddr(&addr); 271 272 std::vector<UDPEntry> entries = 273 ASSERT_NO_ERRNO_AND_VALUE(ProcNetUDPEntries()); 274 UDPEntry e; 275 ASSERT_TRUE(ASSERT_NO_ERRNO_AND_VALUE(FindByFD(entries, &e, socket->get()))); 276 EXPECT_EQ(e.local_port, port); 277 EXPECT_EQ(e.remote_addr, 0); 278 EXPECT_EQ(e.remote_port, 0); 279 } 280 281 TEST(ProcNetUDP, BoundSocketStateClosed) { 282 std::unique_ptr<FileDescriptor> socket = 283 ASSERT_NO_ERRNO_AND_VALUE(BoundUDPSocket()); 284 std::vector<UDPEntry> entries = 285 ASSERT_NO_ERRNO_AND_VALUE(ProcNetUDPEntries()); 286 UDPEntry e; 287 ASSERT_TRUE(ASSERT_NO_ERRNO_AND_VALUE(FindByFD(entries, &e, socket->get()))); 288 EXPECT_EQ(e.state, TCP_CLOSE); 289 } 290 291 TEST(ProcNetUDP, ConnectedSocketStateEstablished) { 292 auto sockets = 293 ASSERT_NO_ERRNO_AND_VALUE(IPv4UDPBidirectionalBindSocketPair(0).Create()); 294 std::vector<UDPEntry> entries = 295 ASSERT_NO_ERRNO_AND_VALUE(ProcNetUDPEntries()); 296 297 UDPEntry e; 298 ASSERT_TRUE(FindByLocalAddr(entries, &e, sockets->first_addr())) 299 << DescribeFirstInetSocket(*sockets); 300 EXPECT_EQ(e.state, TCP_ESTABLISHED); 301 302 ASSERT_TRUE(FindByLocalAddr(entries, &e, sockets->second_addr())) 303 << DescribeSecondInetSocket(*sockets); 304 EXPECT_EQ(e.state, TCP_ESTABLISHED); 305 } 306 307 } // namespace 308 } // namespace testing 309 } // namespace gvisor