1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022, Klara Inc.
5 * Copyright (c) 2022, Claudio Jeker <claudio@openbsd.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in
14 *    the documentation and/or other materials provided with the
15 *    distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/param.h>
31#include <sys/linker.h>
32#include <sys/socket.h>
33
34#include <netinet/in.h>
35#include <netinet/tcp.h>
36
37#include <fcntl.h>
38#include <unistd.h>
39#include <err.h>
40
41#include <atf-c.h>
42
43void test_tcp_md5_getsockopt(int);
44
45void
46test_tcp_md5_getsockopt(int v6)
47{
48	if (kldfind("tcpmd5.ko") == -1)
49		atf_tc_skip("Test requires the tcpmd5 kernel module to be loaded");
50
51        struct sockaddr_in *s;
52        struct sockaddr_in6 s6 = { 0 };
53        struct sockaddr_in s4 = { 0 };
54        socklen_t len;
55        int csock, ssock, opt;
56	int pf;
57
58	if (v6) {
59		pf = PF_INET6;
60		len = sizeof(s6);
61
62		s6.sin6_family = pf;
63		s6.sin6_len = sizeof(s6);
64		s6.sin6_addr = in6addr_loopback;
65		s6.sin6_port = 0;
66
67		s = (struct sockaddr_in *)&s6;
68	} else {
69		pf = PF_INET;
70		len = sizeof(s4);
71
72		s4.sin_family = pf;
73		s4.sin_len = sizeof(s4);
74		s4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
75		s4.sin_port = 0;
76
77		s = &s4;
78	}
79
80        if ((ssock = socket(pf, SOCK_STREAM, 0)) == -1)
81                atf_tc_fail("creating server socket");
82
83	fcntl(ssock, F_SETFL, O_NONBLOCK);
84
85	if ((bind(ssock, (struct sockaddr *)s, len) == -1))
86		atf_tc_fail("binding to localhost");
87
88	getsockname(ssock, (struct sockaddr *)s, &len);
89
90	listen(ssock, 1);
91
92        if ((csock = socket(pf, SOCK_STREAM, 0)) == -1)
93                atf_tc_fail("creating client socket");
94
95        if (connect(csock, (struct sockaddr *)s, len) == -1)
96                atf_tc_fail("connecting to server instance");
97
98        if (getsockopt(csock, IPPROTO_TCP, TCP_MD5SIG, &opt, &len) == -1)
99                atf_tc_fail("getsockopt");
100
101	close(csock);
102	close(ssock);
103
104	atf_tc_pass();
105}
106
107ATF_TC(tcp_md5_getsockopt_v4);
108ATF_TC_HEAD(tcp_md5_getsockopt_v4, tc)
109{
110	atf_tc_set_md_var(tc, "descr", "Test getsockopt for TCP MD5 SIG (IPv4)");
111}
112
113ATF_TC_BODY(tcp_md5_getsockopt_v4, tc)
114{
115	test_tcp_md5_getsockopt(0);
116}
117
118ATF_TC(tcp_md5_getsockopt_v6);
119ATF_TC_HEAD(tcp_md5_getsockopt_v6, tc)
120{
121	atf_tc_set_md_var(tc, "descr", "Test getsockopt for TCP MD5 SIG (IPv6)");
122}
123
124ATF_TC_BODY(tcp_md5_getsockopt_v6, tc)
125{
126	test_tcp_md5_getsockopt(1);
127}
128
129ATF_TP_ADD_TCS(tp)
130{
131	ATF_TP_ADD_TC(tp, tcp_md5_getsockopt_v4);
132	ATF_TP_ADD_TC(tp, tcp_md5_getsockopt_v6);
133
134	return atf_no_error();
135}
136