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
43 void test_tcp_md5_getsockopt(int);
44
45 void
test_tcp_md5_getsockopt(int v6)46 test_tcp_md5_getsockopt(int v6)
47 {
48 struct sockaddr_in *s;
49 struct sockaddr_in6 s6 = { 0 };
50 struct sockaddr_in s4 = { 0 };
51 socklen_t len;
52 int csock, ssock, opt;
53 int pf;
54
55 if (v6) {
56 pf = PF_INET6;
57 len = sizeof(s6);
58
59 s6.sin6_family = pf;
60 s6.sin6_len = sizeof(s6);
61 s6.sin6_addr = in6addr_loopback;
62 s6.sin6_port = 0;
63
64 s = (struct sockaddr_in *)&s6;
65 } else {
66 pf = PF_INET;
67 len = sizeof(s4);
68
69 s4.sin_family = pf;
70 s4.sin_len = sizeof(s4);
71 s4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
72 s4.sin_port = 0;
73
74 s = &s4;
75 }
76
77 if ((ssock = socket(pf, SOCK_STREAM, 0)) == -1)
78 atf_tc_fail("creating server socket");
79
80 fcntl(ssock, F_SETFL, O_NONBLOCK);
81
82 if ((bind(ssock, (struct sockaddr *)s, len) == -1))
83 atf_tc_fail("binding to localhost");
84
85 getsockname(ssock, (struct sockaddr *)s, &len);
86
87 listen(ssock, 1);
88
89 if ((csock = socket(pf, SOCK_STREAM, 0)) == -1)
90 atf_tc_fail("creating client socket");
91
92 if (connect(csock, (struct sockaddr *)s, len) == -1)
93 atf_tc_fail("connecting to server instance");
94
95 if (getsockopt(csock, IPPROTO_TCP, TCP_MD5SIG, &opt, &len) == -1)
96 atf_tc_fail("getsockopt");
97
98 close(csock);
99 close(ssock);
100
101 atf_tc_pass();
102 }
103
104 ATF_TC(tcp_md5_getsockopt_v4);
ATF_TC_HEAD(tcp_md5_getsockopt_v4,tc)105 ATF_TC_HEAD(tcp_md5_getsockopt_v4, tc)
106 {
107 atf_tc_set_md_var(tc, "descr", "Test getsockopt for TCP MD5 SIG (IPv4)");
108 atf_tc_set_md_var(tc, "require.kmods", "tcpmd5");
109 }
110
ATF_TC_BODY(tcp_md5_getsockopt_v4,tc)111 ATF_TC_BODY(tcp_md5_getsockopt_v4, tc)
112 {
113 test_tcp_md5_getsockopt(0);
114 }
115
116 ATF_TC(tcp_md5_getsockopt_v6);
ATF_TC_HEAD(tcp_md5_getsockopt_v6,tc)117 ATF_TC_HEAD(tcp_md5_getsockopt_v6, tc)
118 {
119 atf_tc_set_md_var(tc, "descr", "Test getsockopt for TCP MD5 SIG (IPv6)");
120 atf_tc_set_md_var(tc, "require.kmods", "tcpmd5");
121 }
122
ATF_TC_BODY(tcp_md5_getsockopt_v6,tc)123 ATF_TC_BODY(tcp_md5_getsockopt_v6, tc)
124 {
125 test_tcp_md5_getsockopt(1);
126 }
127
ATF_TP_ADD_TCS(tp)128 ATF_TP_ADD_TCS(tp)
129 {
130 ATF_TP_ADD_TC(tp, tcp_md5_getsockopt_v4);
131 ATF_TP_ADD_TC(tp, tcp_md5_getsockopt_v6);
132
133 return atf_no_error();
134 }
135