Test that a dual-stack listen socket replies with a signed RST to an IPv4 segment from a peer whose key was added with a v4-mapped address. Commit ef3e21c61baa ("net/tcp-ao: fix reset key lookup for IPv4 segments on IPv6 listeners") fixed the key lookup for this RST, and a selftest was suggested in its review [1]. The server kills the accepted socket without sending a FIN or a RST, so the data that the client sends next reaches the listen socket, which replies with a RST. That RST is signed without the ISNs of the connection and the client drops it as a bad segment, so the test checks the OutRsts counter on the server and expects a tcp_ao_mismatch event on the client. The case runs after the netstat checks, as those expect no bad segments. In the _ipv4 build the listen socket is an AF_INET6 one bound to :: and the key is added for the v4-mapped address of the client. Let __test_listen_socket() take the family from the address, and let test_cmp_getsockopt_setsockopt() compare a v4-mapped key address with the AF_INET address that getsockopt() returns for it. Without the fix, rst_ipv4 fails: not ok 15 listen RST: OutRsts counter did not increase: 3 <= 3 not ok 16 Some trace events were expected, but didn't occur Link: https://lore.kernel.org/all/CAL4…@mail.gmail.com/ [1] Signed-off-by: Hungyu Lin <denn…@gmail.com> --- tools/testing/selftests/net/tcp_ao/lib/sock.c | 13 ++- tools/testing/selftests/net/tcp_ao/rst.c | 99 ++++++++++++++++++- 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/net/tcp_ao/lib/sock.c b/tools/testing/selftests/net/tcp_ao/lib/sock.c index 2e7b06a1a156..7121275cce3b 100644 --- a/tools/testing/selftests/net/tcp_ao/lib/sock.c +++ b/tools/testing/selftests/net/tcp_ao/lib/sock.c @@ -10,7 +10,8 @@ const unsigned int test_server_port = 7010; int __test_listen_socket(int backlog, void *addr, size_t addr_sz) { - int err, sk = socket(test_family, SOCK_STREAM, IPPROTO_TCP); + int err, sk = socket(((struct sockaddr *)addr)->sa_family, + SOCK_STREAM, IPPROTO_TCP); long flags; if (sk < 0) @@ -302,6 +303,8 @@ int test_set_ao_info(int sk, struct tcp_ao_info_opt *in) int test_cmp_getsockopt_setsockopt(const struct tcp_ao_add *a, const struct tcp_ao_getsockopt *b) { + const struct sockaddr_in6 *a6 = (void *)&a->addr; + const struct sockaddr_in *b4 = (void *)&b->addr; bool is_kdf_aes_128_cmac = false; bool is_cmac_aes = false; @@ -343,7 +346,13 @@ do { \ b->key, a->key); return -1; } - if (memcmp(&b->addr, &a->addr, sizeof(b->addr))) { + if (a6->sin6_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&a6->sin6_addr)) { + if (b4->sin_family != AF_INET || + b4->sin_addr.s_addr != a6->sin6_addr.s6_addr32[3]) { + test_fail("getsockopt(): returned address is different"); + return -1; + } + } else if (memcmp(&b->addr, &a->addr, sizeof(b->addr))) { test_fail("getsockopt(): returned address is different"); return -1; } diff --git a/tools/testing/selftests/net/tcp_ao/rst.c b/tools/testing/selftests/net/tcp_ao/rst.c index 883cddf377cf..ce9d24ac6437 100644 --- a/tools/testing/selftests/net/tcp_ao/rst.c +++ b/tools/testing/selftests/net/tcp_ao/rst.c @@ -170,6 +170,74 @@ static void test_server_passive_rst(unsigned int port) synchronize_threads(); /* 6: server exits */ } +#ifndef IPV6_TEST +static int test_listen_socket_v4mapped(unsigned int port) +{ + struct sockaddr_in6 addr = { + .sin6_family = AF_INET6, + .sin6_port = htons(port), + }; + struct tcp_ao_add ao; + int sk; + + sk = __test_listen_socket(1, &addr, sizeof(addr)); + + addr.sin6_port = 0; + addr.sin6_addr.s6_addr32[2] = htonl(0xffff); + addr.sin6_addr.s6_addr32[3] = this_ip_dest.a4.s_addr; + + if (test_prepare_key_sockaddr(&ao, DEFAULT_TEST_ALGO, &addr, sizeof(addr), + false, false, DEFAULT_TEST_PREFIX, 0, 100, 100, + 0, 0, strlen(DEFAULT_TEST_PASSWORD), + DEFAULT_TEST_PASSWORD)) + test_error("test_prepare_key_sockaddr()"); + if (setsockopt(sk, IPPROTO_TCP, TCP_AO_ADD_KEY, &ao, sizeof(ao))) + test_error("setsockopt(TCP_AO_ADD_KEY)"); + if (test_verify_socket_key(sk, &ao)) + test_error("test_verify_socket_key()"); + + return sk; +} +#endif + +static void test_server_listen_rst(unsigned int port) +{ + uint64_t before_cnt, after_cnt; + int sk, lsk; + +#ifdef IPV6_TEST + lsk = test_listen_socket(this_ip_addr, port, 1); + if (test_add_key(lsk, DEFAULT_TEST_PASSWORD, this_ip_dest, -1, 100, 100)) + test_error("setsockopt(TCP_AO_ADD_KEY)"); +#else + lsk = test_listen_socket_v4mapped(port); +#endif + + synchronize_threads(); /* 1: MKT added => connect() */ + if (test_wait_fd(lsk, TEST_TIMEOUT_SEC, 0)) + test_error("test_wait_fd()"); + + sk = accept(lsk, NULL, NULL); + if (sk < 0) + test_error("accept()"); + + test_kill_sk(sk); + before_cnt = netstat_get_one("OutRsts", NULL); + + synchronize_threads(); /* 2: accepted socket killed => send data */ + synchronize_threads(); /* 3: client counters read => data sent */ + synchronize_threads(); /* 4: client waited for the RST */ + after_cnt = netstat_get_one("OutRsts", NULL); + if (after_cnt <= before_cnt) + test_fail("listen RST: OutRsts counter did not increase: %" PRIu64 " <= %" PRIu64, + after_cnt, before_cnt); + else + test_ok("listen RST: counter OutRsts increased %" PRIu64 " => %" PRIu64, + before_cnt, after_cnt); + + close(lsk); +} + static void *server_fn(void *arg) { struct netstat *ns_before, *ns_after; @@ -184,6 +252,8 @@ static void *server_fn(void *arg) netstats_check(ns_before, ns_after, "server"); netstat_free(ns_after); netstat_free(ns_before); + + test_server_listen_rst(port++); synchronize_threads(); /* exit */ synchronize_threads(); /* don't race to exit() - client exits */ @@ -433,6 +503,32 @@ static void test_client_passive_rst(unsigned int port) test_assert_counters("client passive RST", &cnt1, &cnt2, TEST_CNT_GOOD); } +static void test_client_listen_rst(unsigned int port) +{ + int sk, dummy; + + 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()"); + + trace_ao_event_expect(TCP_AO_MISMATCH, this_ip_dest, this_ip_addr, + port, -1, 0, 0, 0, 1, 0, 0, 100, 100, -1); + + synchronize_threads(); /* 2: accepted socket killed => send data */ + if (!test_skpair_client(sk, packet_sz, 1, TEST_CNT_BAD, &dummy)) + test_fail("listen RST: client sock is yet connected"); + + synchronize_threads(); /* 4: client waited for the RST */ + test_kill_sk(sk); +} + static void *client_fn(void *arg) { struct netstat *ns_before, *ns_after; @@ -448,12 +544,13 @@ static void *client_fn(void *arg) netstat_free(ns_after); netstat_free(ns_before); + test_client_listen_rst(port++); synchronize_threads(); /* exit */ return NULL; } int main(int argc, char *argv[]) { - test_init(15, server_fn, client_fn); + test_init(16, server_fn, client_fn); return 0; }
-- 2.43.0