1 /*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * sock_diag_test.cpp - unit tests for SockDiag.cpp
17 */
18
19 #include <arpa/inet.h>
20 #include <netinet/in.h>
21 #include <linux/inet_diag.h>
22
23 #include <gtest/gtest.h>
24 #include <netdutils/NetNativeTestBase.h>
25
26 #include "NetdConstants.h"
27 #include "SockDiag.h"
28
29
30 #define NUM_SOCKETS 500
31
32
33 class SockDiagTest : public NetNativeTestBase {
34 };
35
bindAndListen(int s)36 uint16_t bindAndListen(int s) {
37 for (int i = 0; i < 10; i++) {
38 uint16_t port = 1024 + arc4random_uniform(0xffff - 1024);
39 sockaddr_in6 sin6 = { .sin6_family = AF_INET6, .sin6_port = htons(port) };
40 if (bind(s, (sockaddr *) &sin6, sizeof(sin6)) == 0) {
41 listen(s, 1);
42 return port;
43 }
44 }
45 close(s);
46 return 0;
47 }
48
tcpStateName(uint8_t state)49 const char *tcpStateName(uint8_t state) {
50 static const char *states[] = {
51 "???",
52 "TCP_ESTABLISHED",
53 "TCP_SYN_SENT",
54 "TCP_SYN_RECV",
55 "TCP_FIN_WAIT1",
56 "TCP_FIN_WAIT2",
57 "TCP_TIME_WAIT",
58 "TCP_CLOSE",
59 "TCP_CLOSE_WAIT",
60 "TCP_LAST_ACK",
61 "TCP_LISTEN",
62 "TCP_CLOSING",
63 "TCP_NEW_SYN_RECV",
64 };
65 return states[(state < ARRAY_SIZE(states)) ? state : 0];
66 }
67
TEST_F(SockDiagTest,TestDump)68 TEST_F(SockDiagTest, TestDump) {
69 int v4socket = socket(AF_INET, SOCK_STREAM, 0);
70 int v6socket = socket(AF_INET6, SOCK_STREAM, 0);
71 int listensocket = socket(AF_INET6, SOCK_STREAM, 0);
72 ASSERT_NE(-1, v4socket) << "Failed to open IPv4 socket";
73 ASSERT_NE(-1, v6socket) << "Failed to open IPv6 socket";
74 ASSERT_NE(-1, listensocket) << "Failed to open listen socket";
75
76 uint16_t port = bindAndListen(listensocket);
77 ASSERT_NE(0, port) << "Can't bind to server port";
78
79 // Connect to loopback.
80 sockaddr_in server4 = { .sin_family = AF_INET, .sin_port = htons(port) };
81 sockaddr_in6 server6 = { .sin6_family = AF_INET6, .sin6_port = htons(port) };
82 ASSERT_EQ(0, connect(v4socket, (sockaddr *) &server4, sizeof(server4)))
83 << "IPv4 connect failed: " << strerror(errno);
84 ASSERT_EQ(0, connect(v6socket, (sockaddr *) &server6, sizeof(server6)))
85 << "IPv6 connect failed: " << strerror(errno);
86
87 sockaddr_in6 client46, client6;
88 socklen_t clientlen = std::max(sizeof(client46), sizeof(client6));
89 int accepted4 = accept(listensocket, (sockaddr *) &client46, &clientlen);
90 int accepted6 = accept(listensocket, (sockaddr *) &client6, &clientlen);
91 ASSERT_NE(-1, accepted4);
92 ASSERT_NE(-1, accepted6);
93
94 int v4SocketsSeen = 0;
95 bool seenclient46 = false;
96 bool seenNull = false;
97 char src[INET6_ADDRSTRLEN], dst[INET6_ADDRSTRLEN];
98
99 fprintf(stderr, "Ports:\n server=%d. client46=%d, client6=%d\n",
100 port, ntohs(client46.sin6_port), ntohs(client6.sin6_port));
101
102 auto checkIPv4Dump = [&] (uint8_t /* proto */, const inet_diag_msg *msg) {
103 if (msg == nullptr) {
104 EXPECT_FALSE(seenNull);
105 seenNull = true;
106 return 0;
107 }
108 EXPECT_EQ(htonl(INADDR_LOOPBACK), msg->id.idiag_src[0]);
109 v4SocketsSeen++;
110 seenclient46 |= (msg->id.idiag_sport == client46.sin6_port);
111 inet_ntop(AF_INET, msg->id.idiag_src, src, sizeof(src));
112 inet_ntop(AF_INET, msg->id.idiag_src, dst, sizeof(dst));
113 fprintf(stderr, " v4 %s:%d -> %s:%d %s\n",
114 src, htons(msg->id.idiag_sport),
115 dst, htons(msg->id.idiag_dport),
116 tcpStateName(msg->idiag_state));
117 return 0;
118 };
119
120 int v6SocketsSeen = 0;
121 bool seenClient6 = false, seenServer46 = false, seenServer6 = false;
122
123 auto checkIPv6Dump = [&] (uint8_t /* proto */, const inet_diag_msg *msg) {
124 if (msg == nullptr) {
125 EXPECT_FALSE(seenNull);
126 seenNull = true;
127 return 0;
128 }
129 struct in6_addr *saddr = (struct in6_addr *) msg->id.idiag_src;
130 EXPECT_TRUE(
131 IN6_IS_ADDR_LOOPBACK(saddr) ||
132 (IN6_IS_ADDR_V4MAPPED(saddr) && saddr->s6_addr32[3] == htonl(INADDR_LOOPBACK)));
133 v6SocketsSeen++;
134 seenClient6 |= (msg->id.idiag_sport == client6.sin6_port);
135 seenServer46 |= (msg->id.idiag_sport == htons(port));
136 seenServer6 |= (msg->id.idiag_sport == htons(port));
137 inet_ntop(AF_INET6, msg->id.idiag_src, src, sizeof(src));
138 inet_ntop(AF_INET6, msg->id.idiag_src, dst, sizeof(dst));
139 fprintf(stderr, " v6 [%s]:%d -> [%s]:%d %s\n",
140 src, htons(msg->id.idiag_sport),
141 dst, htons(msg->id.idiag_dport),
142 tcpStateName(msg->idiag_state));
143 return 0;
144 };
145
146 SockDiag sd;
147 ASSERT_TRUE(sd.open()) << "Failed to open SOCK_DIAG socket";
148
149 seenNull = false;
150 int ret = sd.sendDumpRequest(IPPROTO_TCP, AF_INET, "127.0.0.1");
151 ASSERT_EQ(0, ret) << "Failed to send IPv4 dump request: " << strerror(-ret);
152 fprintf(stderr, "Sent IPv4 dump\n");
153 sd.readDiagMsg(IPPROTO_TCP, checkIPv4Dump);
154 EXPECT_GE(v4SocketsSeen, 1);
155 EXPECT_TRUE(seenclient46);
156 EXPECT_FALSE(seenServer46);
157
158 seenNull = false;
159 ret = sd.sendDumpRequest(IPPROTO_TCP, AF_INET6, "127.0.0.1");
160 ASSERT_EQ(0, ret) << "Failed to send mapped dump request: " << strerror(-ret);
161 fprintf(stderr, "Sent mapped dump\n");
162 sd.readDiagMsg(IPPROTO_TCP, checkIPv6Dump);
163 EXPECT_TRUE(seenServer46);
164
165 seenNull = false;
166 ret = sd.sendDumpRequest(IPPROTO_TCP, AF_INET6, "::1");
167 ASSERT_EQ(0, ret) << "Failed to send IPv6 dump request: " << strerror(-ret);
168 fprintf(stderr, "Sent IPv6 dump\n");
169
170 sd.readDiagMsg(IPPROTO_TCP, checkIPv6Dump);
171 EXPECT_GE(v6SocketsSeen, 1);
172 EXPECT_TRUE(seenClient6);
173 EXPECT_TRUE(seenServer6);
174
175 close(v4socket);
176 close(v6socket);
177 close(listensocket);
178 close(accepted4);
179 close(accepted6);
180 }
181
TEST_F(SockDiagTest,TestMicroBenchmark)182 TEST_F(SockDiagTest, TestMicroBenchmark) {
183 fprintf(stderr, "Benchmarking closing %d sockets\n", NUM_SOCKETS);
184
185 int listensocket = socket(AF_INET6, SOCK_STREAM, 0);
186 ASSERT_NE(-1, listensocket) << "Failed to open listen socket";
187
188 uint16_t port = bindAndListen(listensocket);
189 ASSERT_NE(0, port) << "Can't bind to server port";
190 sockaddr_in6 server = { .sin6_family = AF_INET6, .sin6_port = htons(port) };
191
192 using ms = std::chrono::duration<float, std::ratio<1, 1000>>;
193
194 int clientsockets[NUM_SOCKETS], serversockets[NUM_SOCKETS];
195 uint16_t clientports[NUM_SOCKETS];
196 sockaddr_in6 client;
197 socklen_t clientlen;
198
199 auto start = std::chrono::steady_clock::now();
200 for (int i = 0; i < NUM_SOCKETS; i++) {
201 int s = socket(AF_INET6, SOCK_STREAM, 0);
202 clientlen = sizeof(client);
203 ASSERT_EQ(0, connect(s, (sockaddr *) &server, sizeof(server)))
204 << "Connecting socket " << i << " failed " << strerror(errno);
205 serversockets[i] = accept(listensocket, (sockaddr *) &client, &clientlen);
206 ASSERT_NE(-1, serversockets[i])
207 << "Accepting socket " << i << " failed " << strerror(errno);
208 clientports[i] = client.sin6_port;
209 clientsockets[i] = s;
210 }
211 fprintf(stderr, " Connecting: %6.1f ms\n",
212 std::chrono::duration_cast<ms>(std::chrono::steady_clock::now() - start).count());
213
214 SockDiag sd;
215 ASSERT_TRUE(sd.open()) << "Failed to open SOCK_DIAG socket";
216
217 start = std::chrono::steady_clock::now();
218 int ret = sd.destroySockets("::1");
219 EXPECT_LE(0, ret) << ": Failed to destroy sockets on ::1: " << strerror(-ret);
220 fprintf(stderr, " Destroying: %6.1f ms\n",
221 std::chrono::duration_cast<ms>(std::chrono::steady_clock::now() - start).count());
222
223 int err;
224 start = std::chrono::steady_clock::now();
225 for (int i = 0; i < NUM_SOCKETS; i++) {
226 ret = send(clientsockets[i], "foo", sizeof("foo"), 0);
227 err = errno;
228 EXPECT_EQ(-1, ret) << "Client socket " << i << " not closed";
229 if (ret == -1) {
230 // Since we're connected to ourselves, the error might be ECONNABORTED (if we destroyed
231 // the socket) or ECONNRESET (if the other end was destroyed and sent a RST).
232 EXPECT_TRUE(errno == ECONNABORTED || errno == ECONNRESET)
233 << "Client socket: unexpected error: " << strerror(errno);
234 }
235
236 ret = send(serversockets[i], "foo", sizeof("foo"), 0);
237 err = errno;
238 EXPECT_EQ(-1, ret) << "Server socket " << i << " not closed";
239 if (ret == -1) {
240 EXPECT_TRUE(errno == ECONNABORTED || errno == ECONNRESET)
241 << "Server socket: unexpected error: " << strerror(errno);
242 }
243 }
244 fprintf(stderr, " Verifying: %6.1f ms\n",
245 std::chrono::duration_cast<ms>(std::chrono::steady_clock::now() - start).count());
246
247
248
249 start = std::chrono::steady_clock::now();
250 for (int i = 0; i < NUM_SOCKETS; i++) {
251 close(clientsockets[i]);
252 close(serversockets[i]);
253 }
254 fprintf(stderr, " Closing: %6.1f ms\n",
255 std::chrono::duration_cast<ms>(std::chrono::steady_clock::now() - start).count());
256
257 close(listensocket);
258 }
259