1 /* SCTP kernel Implementation
2  * Copyright (c) 2003 Hewlett-Packard Development Company, L.P
3  * (C) Copyright IBM Corp. 2004
4  *
5  * This file has test cases to test the sctp_connectx () call for 1-1 style sockets
6  *
7  * TEST1: Bad socket descriptor
8  * TEST2: Invalid socket
9  * TEST3: Invalid address length
10  * TEST4: Invalid address family
11  * TEST5: Valid blocking sctp_connectx
12  * TEST6: Connect when accept queue is full
13  * TEST7: On a listening socket
14  * TEST8: On established socket
15  * TEST9: Connect to re-establish a closed association.
16  *
17  * The SCTP implementation is free software;
18  * you can redistribute it and/or modify it under the terms of
19  * the GNU General Public License as published by
20  * the Free Software Foundation; either version 2, or (at your option)
21  * any later version.
22  *
23  * The SCTP implementation is distributed in the hope that it
24  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
25  *                 ************************
26  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27  * See the GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with GNU CC; see the file COPYING.  If not, write to
31  * the Free Software Foundation, 59 Temple Place - Suite 330,
32  * Boston, MA 02111-1307, USA.
33  *
34  * Please send any bug reports or fixes you make to the
35  * email address(es):
36  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
37  *
38  * Or submit a bug report through the following website:
39  *    http://www.sf.net/projects/lksctp
40  *
41  * Any bugs reported given to us we will try to fix... any fixes shared will
42  * be incorporated into the next SCTP release
43  *
44  */
45 
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <sys/types.h>
52 #include <sys/socket.h>
53 #include <linux/socket.h>
54 #include <netinet/in.h>         /* for sockaddr_in */
55 #include <arpa/inet.h>
56 #include <sys/errno.h>
57 #include <sys/uio.h>
58 #include <netinet/sctp.h>
59 #include "sctputil.h"
60 #include "tst_kernel.h"
61 
62 char *TCID = __FILE__;
63 int TST_TOTAL = 10;
64 int TST_CNT = 0;
65 
66 #define SK_MAX 10
67 
68 int
main(void)69 main(void)
70 {
71 	int error,i;
72 	socklen_t len;
73 	int sk,lstn_sk,clnt_sk[SK_MAX],acpt_sk[SK_MAX],pf_class;
74 	int sk1,clnt2_sk;
75 
76 	struct sockaddr_in conn_addr,lstn_addr,acpt_addr;
77 
78 	if (tst_check_driver("sctp"))
79 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
80 
81 	/* Rather than fflush() throughout the code, set stdout to
82 	 * be unbuffered.
83 	 */
84 	setvbuf(stdout, NULL, _IONBF, 0);
85 	setvbuf(stderr, NULL, _IONBF, 0);
86 
87 	pf_class = PF_INET;
88 
89 	sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
90 	sk1 = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
91 
92 	/*Creating a listen socket*/
93 	lstn_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
94 
95 	/*Creating a regular socket*/
96 	for (i = 0 ; i < SK_MAX ; i++)
97 		clnt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
98 
99 	clnt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
100 
101 	conn_addr.sin_family = AF_INET;
102 	conn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
103 	conn_addr.sin_port = htons(SCTP_TESTPORT_1);
104 
105 	lstn_addr.sin_family = AF_INET;
106 	lstn_addr.sin_addr.s_addr = SCTP_IP_LOOPBACK;
107 	lstn_addr.sin_port = htons(SCTP_TESTPORT_1);
108 
109 	/*Binding the listen socket*/
110 	test_bind(lstn_sk, (struct sockaddr *) &lstn_addr, sizeof(lstn_addr));
111 
112 	/*Listening the socket*/
113 	test_listen(lstn_sk, SK_MAX-1);
114 
115 
116 	/*sctp_connectx () TEST1: Bad socket descriptor, EBADF Expected error*/
117 	len = sizeof(struct sockaddr_in);
118 	error = sctp_connectx(-1, (struct sockaddr *) &conn_addr, 1, NULL);
119 	if (error != -1 || errno != EBADF)
120 		tst_brkm(TBROK, tst_exit, "sctp_connectx with bad socket "
121 			 "descriptor error:%d, errno:%d", error, errno);
122 
123 	tst_resm(TPASS, "sctp_connectx() with bad socket descriptor - EBADF");
124 
125 	/*sctp_connectx () TEST2: Invalid socket, ENOTSOCK Expected error*/
126 	error = sctp_connectx(0, (struct sockaddr *) &conn_addr, 1, NULL);
127 	if (error != -1 || errno != ENOTSOCK)
128 		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid socket "
129 	                 "error:%d, errno:%d", error, errno);
130 
131 	tst_resm(TPASS, "sctp_connectx() with invalid socket - ENOTSOCK");
132 
133 	/*sctp_connectx () TEST3: Invalid address length, EINVAL Expected error*/
134 	error = sctp_connectx(sk, (struct sockaddr *) &conn_addr, 0, NULL);
135 	if (error != -1 || errno != EINVAL)
136 		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address length "
137 	                 "error:%d, errno:%d", error, errno);
138 
139 	tst_resm(TPASS, "sctp_connectx() with invalid address length - EINVAL");
140 
141 	/*sctp_connectx () TEST4: Invalid address family, EINVAL Expect error*/
142 	conn_addr.sin_family = 9090; /*Assigning invalid address family*/
143 	error = sctp_connectx(sk, (struct sockaddr *) &conn_addr, 1, NULL);
144 	if (error != -1 || errno != EINVAL)
145 		tst_brkm(TBROK, tst_exit, "sctp_connectx with invalid address family "
146 	                 "error:%d, errno:%d", error, errno);
147 
148 	tst_resm(TPASS, "sctp_connectx() with invalid address family - EINVAL");
149 
150 	conn_addr.sin_family = AF_INET;
151 
152 	/*sctp_connectx () TEST5: Blocking sctp_connectx, should pass*/
153 	/*All the be below blocking sctp_connectx should pass as socket will be
154 	listening SK_MAX clients*/
155 	for (i = 0 ; i < SK_MAX ; i++) {
156 		error = sctp_connectx(clnt_sk[i], (struct sockaddr *)&conn_addr,
157 			      1, NULL);
158 		if (error < 0)
159 			tst_brkm(TBROK, tst_exit, "valid blocking sctp_connectx "
160 				 "error:%d, errno:%d", error, errno);
161 	}
162 
163 	tst_resm(TPASS, "valid blocking sctp_connectx() - SUCCESS");
164 
165 	/*sctp_connectx () TEST6: sctp_connectx when accept queue is full, ECONNREFUSED
166 	Expect error*/
167 	/*Now that accept queue is full, the below sctp_connectx should fail*/
168 	error = sctp_connectx(clnt2_sk, (struct sockaddr *) &conn_addr, 1, NULL);
169 	if (error != -1 || errno != ECONNREFUSED)
170 		tst_brkm(TBROK, tst_exit, "sctp_connectx when accept queue is full "
171 	                 "error:%d, errno:%d", error, errno);
172 
173 	tst_resm(TPASS, "sctp_connectx() when accept queue is full - ECONNREFUSED");
174 
175 	/*Calling a accept first to estblish the pending sctp_connectxions*/
176 	for (i=0 ; i < SK_MAX ; i++)
177 		acpt_sk[i] = test_accept(lstn_sk,
178 					 (struct sockaddr *) &acpt_addr, &len);
179 
180 	/*sctp_connectx () TEST7: from a listening socket, EISCONN Expect error*/
181 	error = sctp_connectx(lstn_sk, (struct sockaddr *) &lstn_addr, 1, NULL);
182 	if (error != -1 || errno != EISCONN)
183 		tst_brkm(TBROK, tst_exit, "sctp_connectx on a listening socket "
184 	                 "error:%d, errno:%d", error, errno);
185 
186 	tst_resm(TPASS, "sctp_connectx() on a listening socket - EISCONN");
187 
188 	/*sctp_connectx() TEST8: On established socket, EISCONN Expect error*/
189 	i=0;
190 	error = sctp_connectx(acpt_sk[i], (struct sockaddr *) &lstn_addr, 1, NULL);
191 	if (error != -1 || errno != EISCONN)
192 		tst_brkm(TBROK, tst_exit, "sctp_connectx on an established socket "
193 	                 "error:%d, errno:%d", error, errno);
194 
195 	tst_resm(TPASS, "sctp_connectx() on an established socket - EISCONN");
196 
197 	for (i = 0 ; i < 4 ; i++) {
198 		close(clnt_sk[i]);
199 		close(acpt_sk[i]);
200 	}
201 
202 	/* sctp_connectx() TEST9: Re-establish an association that is closed.
203 	 * should succeed.
204 	 */
205 	error = sctp_connectx(sk1, (struct sockaddr *)&conn_addr, 1, NULL);
206 	if (error < 0)
207 		tst_brkm(TBROK, tst_exit, "Re-establish an association that "
208 				 "is closed error:%d, errno:%d", error, errno);
209 
210 	tst_resm(TPASS, "sctp_connectx() to re-establish a closed association - "
211 		 "SUCCESS");
212 
213 	close(sk);
214 	close(sk1);
215 	close(lstn_sk);
216 
217 	return 0;
218 }
219