1 // SPDX-License-Identifier: GPL-2.0
3 * The test checks that both active and passive reset have correct TCP-AO
4 * signature. An "active" reset (abort) here is procured from closing
5 * listen() socket with non-accepted connections in the queue:
6 * inet_csk_listen_stop() => inet_child_forget() =>
7 * => tcp_disconnect() => tcp_send_active_reset()
9 * The passive reset is quite hard to get on established TCP connections.
10 * It could be procured from non-established states, but the synchronization
11 * part from userspace in order to reliably get RST seems uneasy.
12 * So, instead it's procured by corrupting SEQ number on TIMED-WAIT state.
14 * It's important to test both passive and active RST as they go through
15 * different code-paths:
16 * - tcp_send_active_reset() makes no-data skb, sends it with tcp_transmit_skb()
17 * - tcp_v*_send_reset() create their reply skbs and send them with
18 * ip_send_unicast_reply()
20 * In both cases TCP-AO signatures have to be correct, which is verified by
21 * (1) checking that the TCP-AO connection was reset and (2) TCP-AO counters.
23 * Author: Dmitry Safonov <dima@arista.com>
26 #include "../../../../include/linux/kernel.h"
29 const size_t quota = 1000;
30 const size_t packet_sz = 100;
32 * Backlog == 0 means 1 connection in queue, see:
33 * commit 64a146513f8f ("[NET]: Revert incorrect accept queue...")
35 const unsigned int backlog;
37 static void netstats_check(struct netstat *before, struct netstat *after,
40 uint64_t before_cnt, after_cnt;
42 before_cnt = netstat_get(before, "TCPAORequired", NULL);
43 after_cnt = netstat_get(after, "TCPAORequired", NULL);
44 if (after_cnt > before_cnt)
45 test_fail("Segments without AO sign (%s): %" PRIu64 " => %" PRIu64,
46 msg, before_cnt, after_cnt);
48 test_ok("No segments without AO sign (%s)", msg);
50 before_cnt = netstat_get(before, "TCPAOGood", NULL);
51 after_cnt = netstat_get(after, "TCPAOGood", NULL);
52 if (after_cnt <= before_cnt)
53 test_fail("Signed AO segments (%s): %" PRIu64 " => %" PRIu64,
54 msg, before_cnt, after_cnt);
56 test_ok("Signed AO segments (%s): %" PRIu64 " => %" PRIu64,
57 msg, before_cnt, after_cnt);
59 before_cnt = netstat_get(before, "TCPAOBad", NULL);
60 after_cnt = netstat_get(after, "TCPAOBad", NULL);
61 if (after_cnt > before_cnt)
62 test_fail("Segments with bad AO sign (%s): %" PRIu64 " => %" PRIu64,
63 msg, before_cnt, after_cnt);
65 test_ok("No segments with bad AO sign (%s)", msg);
69 * Another way to send RST, but not through tcp_v{4,6}_send_reset()
70 * is tcp_send_active_reset(), that is not in reply to inbound segment,
71 * but rather active send. It uses tcp_transmit_skb(), so that should
72 * work, but as it also sends RST - nice that it can be covered as well.
74 static void close_forced(int sk)
80 if (setsockopt(sk, SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)))
81 test_error("setsockopt(SO_LINGER)");
85 static void test_server_active_rst(unsigned int port)
87 struct tcp_ao_counters cnt1, cnt2;
91 lsk = test_listen_socket(this_ip_addr, port, backlog);
92 if (test_add_key(lsk, DEFAULT_TEST_PASSWORD, this_ip_dest, -1, 100, 100))
93 test_error("setsockopt(TCP_AO_ADD_KEY)");
94 if (test_get_tcp_ao_counters(lsk, &cnt1))
95 test_error("test_get_tcp_ao_counters()");
97 synchronize_threads(); /* 1: MKT added */
98 if (test_wait_fd(lsk, TEST_TIMEOUT_SEC, 0))
99 test_error("test_wait_fd()");
101 sk = accept(lsk, NULL, NULL);
103 test_error("accept()");
105 synchronize_threads(); /* 2: connection accept()ed, another queued */
106 if (test_get_tcp_ao_counters(lsk, &cnt2))
107 test_error("test_get_tcp_ao_counters()");
109 synchronize_threads(); /* 3: close listen socket */
111 bytes = test_server_run(sk, quota, 0);
113 test_error("servered only %zd bytes", bytes);
115 test_ok("servered %zd bytes", bytes);
117 synchronize_threads(); /* 4: finishing up */
120 synchronize_threads(); /* 5: closed active sk */
122 synchronize_threads(); /* 6: counters checks */
123 if (test_tcp_ao_counters_cmp("active RST server", &cnt1, &cnt2, TEST_CNT_GOOD))
124 test_fail("MKT counters (server) have not only good packets");
126 test_ok("MKT counters are good on server");
129 static void test_server_passive_rst(unsigned int port)
131 struct tcp_ao_counters ao1, ao2;
135 lsk = test_listen_socket(this_ip_addr, port, 1);
137 if (test_add_key(lsk, DEFAULT_TEST_PASSWORD, this_ip_dest, -1, 100, 100))
138 test_error("setsockopt(TCP_AO_ADD_KEY)");
140 synchronize_threads(); /* 1: MKT added => connect() */
141 if (test_wait_fd(lsk, TEST_TIMEOUT_SEC, 0))
142 test_error("test_wait_fd()");
144 sk = accept(lsk, NULL, NULL);
146 test_error("accept()");
148 synchronize_threads(); /* 2: accepted => send data */
150 if (test_get_tcp_ao_counters(sk, &ao1))
151 test_error("test_get_tcp_ao_counters()");
153 bytes = test_server_run(sk, quota, TEST_TIMEOUT_SEC);
154 if (bytes != quota) {
156 test_fail("server served: %zd", bytes);
158 test_fail("server returned %zd", bytes);
161 synchronize_threads(); /* 3: checkpoint the client */
162 synchronize_threads(); /* 4: close the server, creating twsk */
163 if (test_get_tcp_ao_counters(sk, &ao2))
164 test_error("test_get_tcp_ao_counters()");
167 synchronize_threads(); /* 5: restore the socket, send more data */
168 test_tcp_ao_counters_cmp("passive RST server", &ao1, &ao2, TEST_CNT_GOOD);
170 synchronize_threads(); /* 6: server exits */
173 static void *server_fn(void *arg)
175 struct netstat *ns_before, *ns_after;
176 unsigned int port = test_server_port;
178 ns_before = netstat_read();
180 test_server_active_rst(port++);
181 test_server_passive_rst(port++);
183 ns_after = netstat_read();
184 netstats_check(ns_before, ns_after, "server");
185 netstat_free(ns_after);
186 netstat_free(ns_before);
187 synchronize_threads(); /* exit */
189 synchronize_threads(); /* don't race to exit() - client exits */
193 static int test_wait_fds(int sk[], size_t nr, bool is_writable[],
194 ssize_t wait_for, time_t sec)
196 struct timeval tv = { .tv_sec = sec };
197 struct timeval *ptv = NULL;
203 for (i = 0; i < nr; i++) {
204 FD_SET(sk[i], &left);
206 is_writable[i] = false;
213 bool is_empty = true;
219 for (i = 0; i < nr; i++) {
220 if (!FD_ISSET(sk[i], &left))
227 FD_SET(sk[i], &efds);
234 ret = select(nfd + 1, NULL, &fds, &efds, ptv);
239 for (i = 0; i < nr; i++) {
240 if (FD_ISSET(sk[i], &fds)) {
242 is_writable[i] = true;
243 FD_CLR(sk[i], &left);
247 if (FD_ISSET(sk[i], &efds)) {
248 FD_CLR(sk[i], &left);
252 } while (wait_for > 0);
257 static void test_client_active_rst(unsigned int port)
259 /* one in queue, another accept()ed */
260 unsigned int wait_for = backlog + 2;
262 bool is_writable[ARRAY_SIZE(sk)] = {false};
263 unsigned int last = ARRAY_SIZE(sk) - 1;
265 for (i = 0; i < ARRAY_SIZE(sk); i++) {
266 sk[i] = socket(test_family, SOCK_STREAM, IPPROTO_TCP);
268 test_error("socket()");
269 if (test_add_key(sk[i], DEFAULT_TEST_PASSWORD,
270 this_ip_dest, -1, 100, 100))
271 test_error("setsockopt(TCP_AO_ADD_KEY)");
274 synchronize_threads(); /* 1: MKT added */
275 for (i = 0; i < last; i++) {
276 err = _test_connect_socket(sk[i], this_ip_dest, port,
277 (i == 0) ? TEST_TIMEOUT_SEC : -1);
280 test_error("failed to connect()");
283 synchronize_threads(); /* 2: connection accept()ed, another queued */
284 err = test_wait_fds(sk, last, is_writable, wait_for, TEST_TIMEOUT_SEC);
286 test_error("test_wait_fds(): %d", err);
288 synchronize_threads(); /* 3: close listen socket */
289 if (test_client_verify(sk[0], packet_sz, quota / packet_sz, TEST_TIMEOUT_SEC))
290 test_fail("Failed to send data on connected socket");
292 test_ok("Verified established tcp connection");
294 synchronize_threads(); /* 4: finishing up */
295 err = _test_connect_socket(sk[last], this_ip_dest, port, -1);
297 test_error("failed to connect()");
299 synchronize_threads(); /* 5: closed active sk */
300 err = test_wait_fds(sk, ARRAY_SIZE(sk), NULL,
301 wait_for, TEST_TIMEOUT_SEC);
303 test_error("select(): %d", err);
305 for (i = 0; i < ARRAY_SIZE(sk); i++) {
306 socklen_t slen = sizeof(err);
308 if (getsockopt(sk[i], SOL_SOCKET, SO_ERROR, &err, &slen))
309 test_error("getsockopt()");
310 if (is_writable[i] && err != ECONNRESET) {
311 test_fail("sk[%d] = %d, err = %d, connection wasn't reset",
314 test_ok("sk[%d] = %d%s", i, sk[i],
315 is_writable[i] ? ", connection was reset" : "");
318 synchronize_threads(); /* 6: counters checks */
321 static void test_client_passive_rst(unsigned int port)
323 struct tcp_ao_counters ao1, ao2;
324 struct tcp_ao_repair ao_img;
325 struct tcp_sock_state img;
329 sk = socket(test_family, SOCK_STREAM, IPPROTO_TCP);
331 test_error("socket()");
333 if (test_add_key(sk, DEFAULT_TEST_PASSWORD, this_ip_dest, -1, 100, 100))
334 test_error("setsockopt(TCP_AO_ADD_KEY)");
336 synchronize_threads(); /* 1: MKT added => connect() */
337 if (test_connect_socket(sk, this_ip_dest, port) <= 0)
338 test_error("failed to connect()");
340 synchronize_threads(); /* 2: accepted => send data */
341 if (test_client_verify(sk, packet_sz, quota / packet_sz, TEST_TIMEOUT_SEC))
342 test_fail("Failed to send data on connected socket");
344 test_ok("Verified established tcp connection");
346 synchronize_threads(); /* 3: checkpoint the client */
347 test_enable_repair(sk);
348 test_sock_checkpoint(sk, &img, &saddr);
349 test_ao_checkpoint(sk, &ao_img);
350 test_disable_repair(sk);
352 synchronize_threads(); /* 4: close the server, creating twsk */
355 * The "corruption" in SEQ has to be small enough to fit into TCP
356 * window, see tcp_timewait_state_process() for out-of-window
359 img.out.seq += 5; /* 5 is more noticeable in tcpdump than 1 */
362 * FIXME: This is kind-of ugly and dirty, but it works.
364 * At this moment, the server has close'ed(sk).
365 * The passive RST that is being targeted here is new data after
366 * half-duplex close, see tcp_timewait_state_process() => TCP_TW_RST
368 * What is needed here is:
369 * (1) wait for FIN from the server
370 * (2) make sure that the ACK from the client went out
371 * (3) make sure that the ACK was received and processed by the server
373 * Otherwise, the data that will be sent from "repaired" socket
374 * post SEQ corruption may get to the server before it's in
377 * (1) is easy with select()/poll()
378 * (2) is possible by polling tcpi_state from TCP_INFO
379 * (3) is quite complex: as server's socket was already closed,
380 * probably the way to do it would be tcp-diag.
382 sleep(TEST_RETRANSMIT_SEC);
384 synchronize_threads(); /* 5: restore the socket, send more data */
387 sk = socket(test_family, SOCK_STREAM, IPPROTO_TCP);
389 test_error("socket()");
391 test_enable_repair(sk);
392 test_sock_restore(sk, &img, &saddr, this_ip_dest, port);
393 if (test_add_repaired_key(sk, DEFAULT_TEST_PASSWORD, 0, this_ip_dest, -1, 100, 100))
394 test_error("setsockopt(TCP_AO_ADD_KEY)");
395 test_ao_restore(sk, &ao_img);
397 if (test_get_tcp_ao_counters(sk, &ao1))
398 test_error("test_get_tcp_ao_counters()");
400 test_disable_repair(sk);
401 test_sock_state_free(&img);
404 * This is how "passive reset" is acquired in this test from TCP_TW_RST:
406 * IP 10.0.254.1.7011 > 10.0.1.1.59772: Flags [P.], seq 901:1001, ack 1001, win 249,
407 * options [tcp-ao keyid 100 rnextkeyid 100 mac 0x10217d6c36a22379086ef3b1], length 100
408 * IP 10.0.254.1.7011 > 10.0.1.1.59772: Flags [F.], seq 1001, ack 1001, win 249,
409 * options [tcp-ao keyid 100 rnextkeyid 100 mac 0x104ffc99b98c10a5298cc268], length 0
410 * IP 10.0.1.1.59772 > 10.0.254.1.7011: Flags [.], ack 1002, win 251,
411 * options [tcp-ao keyid 100 rnextkeyid 100 mac 0xe496dd4f7f5a8a66873c6f93,nop,nop,sack 1 {1001:1002}], length 0
412 * IP 10.0.1.1.59772 > 10.0.254.1.7011: Flags [P.], seq 1006:1106, ack 1001, win 251,
413 * options [tcp-ao keyid 100 rnextkeyid 100 mac 0x1b5f3330fb23fbcd0c77d0ca], length 100
414 * IP 10.0.254.1.7011 > 10.0.1.1.59772: Flags [R], seq 3215596252, win 0,
415 * options [tcp-ao keyid 100 rnextkeyid 100 mac 0x0bcfbbf497bce844312304b2], length 0
417 err = test_client_verify(sk, packet_sz, quota / packet_sz, 2 * TEST_TIMEOUT_SEC);
418 /* Make sure that the connection was reset, not timeouted */
419 if (err && err == -ECONNRESET)
420 test_ok("client sock was passively reset post-seq-adjust");
422 test_fail("client sock was not reset post-seq-adjust: %d", err);
424 test_fail("client sock is yet connected post-seq-adjust");
426 if (test_get_tcp_ao_counters(sk, &ao2))
427 test_error("test_get_tcp_ao_counters()");
429 synchronize_threads(); /* 6: server exits */
431 test_tcp_ao_counters_cmp("client passive RST", &ao1, &ao2, TEST_CNT_GOOD);
434 static void *client_fn(void *arg)
436 struct netstat *ns_before, *ns_after;
437 unsigned int port = test_server_port;
439 ns_before = netstat_read();
441 test_client_active_rst(port++);
442 test_client_passive_rst(port++);
444 ns_after = netstat_read();
445 netstats_check(ns_before, ns_after, "client");
446 netstat_free(ns_after);
447 netstat_free(ns_before);
449 synchronize_threads(); /* exit */
453 int main(int argc, char *argv[])
455 test_init(14, server_fn, client_fn);