// SPDX-License-Identifier: GPL-2.0 /* * The test checks that both active and passive reset have correct TCP-AO * signature. An "active" reset (abort) here is procured from closing * listen() socket with non-accepted connections in the queue: * inet_csk_listen_stop() => inet_child_forget() => * => tcp_disconnect() => tcp_send_active_reset() * * The passive reset is quite hard to get on established TCP connections. * It could be procured from non-established states, but the synchronization * part from userspace in order to reliably get RST seems uneasy. * So, instead it's procured by corrupting SEQ number on TIMED-WAIT state. * * It's important to test both passive and active RST as they go through * different code-paths: * - tcp_send_active_reset() makes no-data skb, sends it with tcp_transmit_skb() * - tcp_v*_send_reset() create their reply skbs and send them with * ip_send_unicast_reply() * * In both cases TCP-AO signatures have to be correct, which is verified by * (1) checking that the TCP-AO connection was reset and (2) TCP-AO counters. * * Author: Dmitry Safonov <dima@arista.com>
*/ #include <inttypes.h> #include"../../../../include/linux/kernel.h" #include"aolib.h"
before_cnt = netstat_get(before, "TCPAOBad", NULL);
after_cnt = netstat_get(after, "TCPAOBad", NULL); if (after_cnt > before_cnt)
test_fail("Segments with bad AO sign (%s): %" PRIu64 " => %" PRIu64,
msg, before_cnt, after_cnt); else
test_ok("No segments with bad AO sign (%s)", msg);
}
/* * Another way to send RST, but not through tcp_v{4,6}_send_reset() * is tcp_send_active_reset(), that is not in reply to inbound segment, * but rather active send. It uses tcp_transmit_skb(), so that should * work, but as it also sends RST - nice that it can be covered as well.
*/ staticvoid close_forced(int sk)
{ struct linger sl;
sk = accept(lsk, NULL, NULL); if (sk < 0)
test_error("accept()");
synchronize_threads(); /* 2: connection accept()ed, another queued */ if (test_get_tcp_counters(lsk, &cnt2))
test_error("test_get_tcp_counters()");
synchronize_threads(); /* 3: close listen socket */
close(lsk);
bytes = test_server_run(sk, quota, 0); if (bytes != quota)
test_error("servered only %zd bytes", bytes); else
test_ok("servered %zd bytes", bytes);
synchronize_threads(); /* 4: finishing up */
close_forced(sk);
synchronize_threads(); /* 5: closed active sk */
synchronize_threads(); /* 6: counters checks */ if (test_assert_counters("active RST server", &cnt1, &cnt2, TEST_CNT_GOOD))
test_fail("MKT counters (server) have not only good packets"); else
test_ok("MKT counters are good on server");
}
errno = 0;
ret = select(nfd + 1, NULL, &fds, &efds, ptv); if (ret < 0) return -errno; if (!ret) return -ETIMEDOUT; for (i = 0; i < nr; i++) { if (FD_ISSET(sk[i], &fds)) { if (is_writable)
is_writable[i] = true;
FD_CLR(sk[i], &left);
wait_for--; continue;
} if (FD_ISSET(sk[i], &efds)) {
FD_CLR(sk[i], &left);
wait_for--;
}
}
} while (wait_for > 0);
return 0;
}
staticvoid test_client_active_rst(unsignedint port)
{ int i, sk[3], err; bool is_writable[ARRAY_SIZE(sk)] = {false}; unsignedint last = ARRAY_SIZE(sk) - 1;
for (i = 0; i < ARRAY_SIZE(sk); i++) {
sk[i] = socket(test_family, SOCK_STREAM, IPPROTO_TCP); if (sk[i] < 0)
test_error("socket()"); if (test_add_key(sk[i], DEFAULT_TEST_PASSWORD,
this_ip_dest, -1, 100, 100))
test_error("setsockopt(TCP_AO_ADD_KEY)");
}
synchronize_threads(); /* 1: MKT added */ for (i = 0; i < last; i++) {
err = _test_connect_socket(sk[i], this_ip_dest, port, i != 0); if (err < 0)
test_error("failed to connect()");
}
synchronize_threads(); /* 2: two connections: one accept()ed, another queued */
err = test_wait_fds(sk, last, is_writable, last, TEST_TIMEOUT_SEC); if (err < 0)
test_error("test_wait_fds(): %d", err);
/* async connect() with third sk to get into request_sock_queue */
err = _test_connect_socket(sk[last], this_ip_dest, port, 1); if (err < 0)
test_error("failed to connect()");
synchronize_threads(); /* 3: close listen socket */ if (test_client_verify(sk[0], packet_sz, quota / packet_sz))
test_fail("Failed to send data on connected socket"); else
test_ok("Verified established tcp connection");
synchronize_threads(); /* 4: finishing up */
synchronize_threads(); /* 5: closed active sk */ /* * Wait for 2 connections: one accepted, another in the accept queue, * the one in request_sock_queue won't get fully established, so * doesn't receive an active RST, see inet_csk_listen_stop().
*/
err = test_wait_fds(sk, last, NULL, last, TEST_TIMEOUT_SEC); if (err < 0)
test_error("select(): %d", err);
for (i = 0; i < ARRAY_SIZE(sk); i++) {
socklen_t slen = sizeof(err);
sk = socket(test_family, SOCK_STREAM, IPPROTO_TCP); if (sk < 0)
test_error("socket()");
if (test_add_key(sk, DEFAULT_TEST_PASSWORD, this_ip_dest, -1, 100, 100))
test_error("setsockopt(TCP_AO_ADD_KEY)");
synchronize_threads(); /* 1: MKT added => connect() */ if (test_connect_socket(sk, this_ip_dest, port) <= 0)
test_error("failed to connect()");
synchronize_threads(); /* 2: accepted => send data */ if (test_client_verify(sk, packet_sz, quota / packet_sz))
test_fail("Failed to send data on connected socket"); else
test_ok("Verified established tcp connection");
synchronize_threads(); /* 4: close the server, creating twsk */
/* * The "corruption" in SEQ has to be small enough to fit into TCP * window, see tcp_timewait_state_process() for out-of-window * segments.
*/
img.out.seq += 5; /* 5 is more noticeable in tcpdump than 1 */
/* * FIXME: This is kind-of ugly and dirty, but it works. * * At this moment, the server has close'ed(sk). * The passive RST that is being targeted here is new data after * half-duplex close, see tcp_timewait_state_process() => TCP_TW_RST * * What is needed here is: * (1) wait for FIN from the server * (2) make sure that the ACK from the client went out * (3) make sure that the ACK was received and processed by the server * * Otherwise, the data that will be sent from "repaired" socket * post SEQ corruption may get to the server before it's in * TCP_FIN_WAIT2. * * (1) is easy with select()/poll() * (2) is possible by polling tcpi_state from TCP_INFO * (3) is quite complex: as server's socket was already closed, * probably the way to do it would be tcp-diag.
*/
sleep(TEST_RETRANSMIT_SEC);
synchronize_threads(); /* 5: restore the socket, send more data */
test_kill_sk(sk);
sk = socket(test_family, SOCK_STREAM, IPPROTO_TCP); if (sk < 0)
test_error("socket()");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.