• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SCTP kernel Implementation
2  * (C) Copyright IBM Corp. 2003
3  * Copyright (c) 1999-2001 Motorola, Inc.
4  *
5  * This file is part of the SCTP kernel Implementation
6  *
7  * The SCTP implementation is free software;
8  * you can redistribute it and/or modify it under the terms of
9  * the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * The SCTP implementation is distributed in the hope that it
14  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
15  *                 ************************
16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17  * See the GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with GNU CC; see the file COPYING.  If not, write to
21  * the Free Software Foundation, 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  *
24  * Please send any bug reports or fixes you make to the
25  * email address(es):
26  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
27  *
28  * Or submit a bug report through the following website:
29  *    http://www.sf.net/projects/lksctp
30  *
31  * Any bugs reported to us we will try to fix... any fixes shared will
32  * be incorporated into the next SCTP release.
33  *
34  * Written or modified by:
35  *    Sridhar Samudrala		<sri@us.ibm.com>
36  */
37 
38 /* This is a kernel test to verify the TCP-style socket interfaces. */
39 
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <fcntl.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/uio.h>
48 #include <sys/poll.h>
49 #include <netinet/in.h>
50 #include <errno.h>
51 #include <netinet/sctp.h>
52 #include <sctputil.h>
53 #include "tst_kernel.h"
54 
55 char *TCID = __FILE__;
56 int TST_TOTAL = 22;
57 int TST_CNT = 0;
58 
59 #define MAX_CLIENTS 10
60 
61 int
main(void)62 main(void)
63 {
64 	int clt_sk[MAX_CLIENTS], accept_sk[MAX_CLIENTS];
65 	int listen_sk, clt2_sk, accept2_sk;
66 	sockaddr_storage_t clt_loop[MAX_CLIENTS];
67 	sockaddr_storage_t svr_loop, accept_loop, clt2_loop;
68 	socklen_t addrlen;
69 	int error, i;
70         char *message = "hello, world!\n";
71 	char msgbuf[100];
72 	int pf_class;
73 	struct pollfd poll_fd;
74 	fd_set set;
75 	struct msghdr outmessage;
76 	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
77 	struct iovec out_iov;
78 	struct cmsghdr *cmsg;
79 	struct sctp_sndrcvinfo *sinfo;
80 	struct msghdr inmessage;
81 	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
82 	char *big_buffer;
83 	struct iovec iov;
84 
85 	if (tst_check_driver("sctp"))
86 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
87 
88         /* Rather than fflush() throughout the code, set stdout to
89 	 * be unbuffered.
90 	 */
91 	setvbuf(stdout, NULL, _IONBF, 0);
92 
93 	/* Initialize the server and client addresses. */
94 #if TEST_V6
95 	pf_class = PF_INET6;
96         svr_loop.v6.sin6_family = AF_INET6;
97         svr_loop.v6.sin6_addr = in6addr_loopback;
98         svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1);
99 	for (i = 0; i < MAX_CLIENTS; i++) {
100         	clt_loop[i].v6.sin6_family = AF_INET6;
101         	clt_loop[i].v6.sin6_addr = in6addr_loopback;
102         	clt_loop[i].v6.sin6_port = htons(SCTP_TESTPORT_2 + i);
103 	}
104         clt2_loop.v6.sin6_family = AF_INET6;
105         clt2_loop.v6.sin6_addr = in6addr_loopback;
106         clt2_loop.v6.sin6_port = htons(SCTP_TESTPORT_2 + i);
107 #else
108 	pf_class = PF_INET;
109 	svr_loop.v4.sin_family = AF_INET;
110 	svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
111 	svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
112 	for (i = 0; i < MAX_CLIENTS; i++) {
113 		clt_loop[i].v4.sin_family = AF_INET;
114 		clt_loop[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
115 		clt_loop[i].v4.sin_port = htons(SCTP_TESTPORT_2 + i);
116 	}
117 	clt2_loop.v4.sin_family = AF_INET;
118 	clt2_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
119 	clt2_loop.v4.sin_port = htons(SCTP_TESTPORT_2 + i);
120 #endif
121 
122 	/* Create and bind the listening server socket.  */
123         listen_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
124 	test_bind(listen_sk, &svr_loop.sa, sizeof(svr_loop));
125 
126 	/* Mark listen_sk as being able to accept new associations.  */
127 	test_listen(listen_sk, MAX_CLIENTS-1);
128 
129 	/* Create and bind the client sockets.  */
130 	for (i = 0; i < MAX_CLIENTS; i++) {
131 		clt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
132 		test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i]));
133 	}
134 	clt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
135 	test_bind(clt2_sk, &clt2_loop.sa, sizeof(clt2_loop));
136 
137 	addrlen = sizeof(accept_loop);
138 	/* Try to do accept on a non-listening socket. It should fail. */
139 	error = accept(clt_sk[0], &accept_loop.sa, &addrlen);
140 	if ((-1 != error) && (EINVAL != errno))
141 		tst_brkm(TBROK, tst_exit, "accept on non-listening socket "
142 			 "error:%d, errno:%d", error, errno);
143 
144 	tst_resm(TPASS, "accept on non-listening socket");
145 
146 	/* Try to do a connect from a listening socket. It should fail. */
147 	error = connect(listen_sk, (struct sockaddr *)&clt_loop[0],
148 			sizeof(clt_loop[0]));
149 	if ((-1 != error) && (EISCONN != errno))
150 		tst_brkm(TBROK, tst_exit, "connect to non-listening socket "
151 			 "error:%d, errno:%d", error, errno);
152 
153 	tst_resm(TPASS, "connect to non-listening socket");
154 
155 	/* Do a blocking connect from clt_sk's to listen_sk */
156 	for (i = 0; i < MAX_CLIENTS; i++)
157 		test_connect(clt_sk[i], &svr_loop.sa, sizeof(svr_loop));
158 
159 	tst_resm(TPASS, "connect to listening socket");
160 
161 	/* Verify that no more connect's can be done after the acceptq
162 	 * backlog has reached the max value.
163 	 */
164 	error = connect(clt2_sk, &svr_loop.sa, sizeof(svr_loop));
165 	if ((-1 != error) && (ECONNREFUSED != errno))
166 		tst_brkm(TBROK, tst_exit, "connect after max backlog "
167 			 "error:%d, errno:%d", error, errno);
168 
169 	tst_resm(TPASS, "connect after max backlog");
170 
171 	/* Extract the associations on the listening socket as new sockets. */
172 	for (i = 0; i < MAX_CLIENTS; i++) {
173 		poll_fd.fd = listen_sk;
174 		poll_fd.events = POLLIN;
175 		poll_fd.revents = 0;
176 		error = poll(&poll_fd, 1, -1);
177 		if ((1 != error) && (1 != poll_fd.revents))
178 			tst_brkm(TBROK, tst_exit, "Unexpected return value "
179 				 "with poll, error:%d errno:%d, revents:%d",
180 				 error, errno, poll_fd.revents);
181 
182 		addrlen = sizeof(accept_loop);
183 		accept_sk[i] = test_accept(listen_sk, &accept_loop.sa,
184 					   &addrlen);
185 	}
186 
187 	tst_resm(TPASS, "accept from listening socket");
188 
189 	/* Try to do a connect on an established socket. It should fail. */
190 	error = connect(accept_sk[0], &clt_loop[0].sa, sizeof(clt_loop[0]));
191 	if ((-1 != error) || (EISCONN != errno))
192 		tst_brkm(TBROK, tst_exit, "connect on an established socket "
193 			 "error:%d errno:%d", error, errno);
194 
195 	tst_resm(TPASS, "connect on an established socket");
196 
197 	/* Try to do accept on an established socket. It should fail. */
198 	error = accept(accept_sk[0], &accept_loop.sa, &addrlen);
199 	if ((-1 != error) && (EINVAL != errno))
200 		tst_brkm(TBROK, tst_exit, "accept on an established socket "
201 			 "error:%d errno:%d", error, errno);
202 
203 	error = accept(clt_sk[0], &accept_loop.sa, &addrlen);
204 	if ((-1 != error) && (EINVAL != errno))
205 		tst_brkm(TBROK, tst_exit, "accept on an established socket "
206 			 "failure: error:%d errno:%d", error, errno);
207 
208 	tst_resm(TPASS, "accept on an established socket");
209 
210 	/* Send and receive a message from the client sockets to the accepted
211 	 * sockets.
212 	 */
213 	for (i = 0; i < MAX_CLIENTS; i++) {
214 		test_send(clt_sk[i], message, strlen(message), 0);
215 		test_recv(accept_sk[i], msgbuf, 100, 0);
216 	}
217 
218 	tst_resm(TPASS, "client sockets -> accepted sockets");
219 
220 	/* Send and receive a message from the accepted sockets to the client
221 	 * sockets.
222 	 */
223 	for (i = 0; i < MAX_CLIENTS; i++) {
224 		test_send(accept_sk[i], message, strlen(message), 0);
225 		test_recv(clt_sk[i], msgbuf, 100, 0);
226 	}
227 
228 	tst_resm(TPASS, "accepted sockets -> client sockets");
229 
230 	/* Sending a message on a listening socket should fail. */
231 	error = send(listen_sk, message, strlen(message), MSG_NOSIGNAL);
232 	if ((-1 != error) || (EPIPE != errno))
233 		tst_brkm(TBROK, tst_exit, "send on a listening socket "
234 			 "error:%d, errno:%d", error, errno);
235 
236 	tst_resm(TPASS, "send on a listening socket");
237 
238 	/* Trying to receive a message on a listening socket should fail. */
239 	error = recv(listen_sk, msgbuf, 100, 0);
240 	if ((-1 != error) || (ENOTCONN != errno))
241 		tst_brkm(TBROK, tst_exit, "recv on a listening socket "
242 			 "error:%d, errno:%d", error, errno);
243 
244 	tst_resm(TPASS, "recv on a listening socket");
245 
246 	/* TESTCASES for shutdown() */
247 	errno = 0;
248 	test_send(accept_sk[0], message, strlen(message), 0);
249 
250 	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
251 	test_enable_assoc_change(clt_sk[0]);
252 
253 	/* Do a SHUT_WR on clt_sk[0] to disable any new sends. */
254 	test_shutdown(clt_sk[0], SHUT_WR);
255 
256 	/* Reading on a socket that has received SHUTDOWN should return 0
257 	 * indicating EOF.
258 	 */
259 	error = recv(accept_sk[0], msgbuf, 100, 0);
260 	if ((0 != error) || (0 != errno))
261 		tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN received socket "
262 			 "error:%d errno:%d", error, errno);
263 
264 	tst_resm(TPASS, "recv on a SHUTDOWN received socket");
265 
266 	/* Read the pending message on clt_sk[0] that was received before
267 	 * SHUTDOWN call.
268 	 */
269 	test_recv(clt_sk[0], msgbuf, 100, 0);
270 
271 	/* Initialize inmessage for all receives. */
272 	big_buffer = test_malloc(REALLY_BIG);
273 	memset(&inmessage, 0, sizeof(inmessage));
274 	iov.iov_base = big_buffer;
275 	iov.iov_len = REALLY_BIG;
276 	inmessage.msg_iov = &iov;
277 	inmessage.msg_iovlen = 1;
278 	inmessage.msg_control = incmsg;
279 	inmessage.msg_controllen = sizeof(incmsg);
280 
281 	/* Receive the SHUTDOWN_COMP notification as they are enabled. */
282 	error = test_recvmsg(clt_sk[0], &inmessage, MSG_WAITALL);
283 	test_check_msg_notification(&inmessage, error,
284 				    sizeof(struct sctp_assoc_change),
285 				    SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);
286 
287 	tst_resm(TPASS, "recv SHUTDOWN_COMP notification on a SHUT_WR socket");
288 
289 	/* No more messages and the association is SHUTDOWN, should fail. */
290 	error = recv(clt_sk[0], msgbuf, 100, 0);
291 	if ((-1 != error) || (ENOTCONN != errno))
292 		tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN sent socket "
293 			 "error:%d, errno:%d", error, errno);
294 
295 	tst_resm(TPASS, "recv on a SHUTDOWN sent socket");
296 
297 	errno = 0;
298 
299 	/* Do a SHUT_RD on clt_sk[1] to disable any new receives. */
300 	test_shutdown(clt_sk[1], SHUT_RD);
301 
302 	error = recv(clt_sk[1], msgbuf, 100, 0);
303 	if ((0 != error) || (0 != errno))
304 		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket "
305 			 "error:%d, errno:%d", error, errno);
306 
307 	/* Sending a message on SHUT_RD socket. */
308 	test_send(clt_sk[1], message, strlen(message), 0);
309 
310 	/* Receive the message sent on SHUT_RD socket. */
311 	test_recv(accept_sk[1], msgbuf, 100, 0);
312 
313 	/* Send a message to the SHUT_RD socket. */
314 	test_send(accept_sk[1], message, strlen(message), 0);
315 
316 	/* We should not receive the message as the socket is SHUT_RD */
317 	error = recv(clt_sk[1], msgbuf, 100, 0);
318 	if ((0 != error) || (0 != errno))
319 		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket "
320 			 "error:%d, errno:%d", error, errno);
321 
322 	tst_resm(TPASS, "recv on a SHUT_RD socket");
323 
324 	/* Do a SHUT_RDWR on clt_sk[2] to disable any new sends/receives. */
325 	test_shutdown(clt_sk[2], SHUT_RDWR);
326 
327 	error = recv(accept_sk[2], msgbuf, 100, 0);
328 	if ((0 != error) || (0 != errno))
329 		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RDWR socket "
330 			 "error:%d, errno:%d", error, errno);
331 
332 	error = recv(clt_sk[2], msgbuf, 100, 0);
333 	if ((0 != error) || (0 != errno))
334 		tst_brkm(TBROK, tst_exit, "recv on a SHUT_RDWR socket "
335 			 "error:%d, errno:%d", error, errno);
336 
337 	tst_resm(TPASS, "recv on a SHUT_RDWR socket");
338 
339 	error = 0;
340 
341 	for (i = 0; i < MAX_CLIENTS; i++)
342 		close(clt_sk[i]);
343 	for (i = 0; i < MAX_CLIENTS; i++)
344 		close(accept_sk[i]);
345 
346 	/* Test case to verify accept of a CLOSED association. */
347 	/* Do a connect, send and a close to ESTABLISH and CLOSE an
348 	 * association on the listening socket.
349 	 */
350 	test_connect(clt2_sk, &svr_loop.sa, sizeof(svr_loop));
351 
352 	test_send(clt2_sk, message, strlen(message), 0);
353 
354 	close(clt2_sk);
355 
356 	FD_ZERO(&set);
357 	FD_SET(listen_sk, &set);
358 
359 	error = select(listen_sk + 1, &set, NULL, NULL, NULL);
360 	if (1 != error)
361 		tst_brkm(TBROK, tst_exit, "select error:%d, "
362 			 "errno: %d", error, errno);
363 
364 	/* Now accept the CLOSED association waiting on the listening
365 	 * socket.
366 	 */
367 	accept2_sk = test_accept(listen_sk, &accept_loop.sa, &addrlen);
368 
369 	/* Receive the message sent before doing a close. */
370 	test_recv(accept2_sk, msgbuf, 100, 0);
371 
372 	/* Receive EOF indication as there are no more messages and the
373 	 * socket is SHUTDOWN.
374 	 */
375 	error = recv(accept2_sk, msgbuf, 100, 0);
376 	if ((0 != error) || (0 != errno))
377 		tst_brkm(TBROK, tst_exit, "Unexpected error return on "
378 			 "recv(error:%d, errno:%d)", error, errno);
379 
380 	tst_resm(TPASS, "accept of a CLOSED association");
381 
382 	/* Trying to send a message over the CLOSED association should
383 	 * generate EPIPE.
384 	 */
385 	error = send(accept2_sk, message, strlen(message), MSG_NOSIGNAL);
386 	if ((-1 != error) || (EPIPE != errno))
387 		tst_brkm(TBROK, tst_exit, "send to a CLOSED association "
388 			 "error:%d, errno:%d", error, errno);
389 
390 	tst_resm(TPASS, "send to a CLOSED association");
391 
392 	error = 0;
393 	close(accept2_sk);
394 
395 	/* Verify that auto-connect can be done on a TCP-style socket using
396 	 * sendto/sendmsg.
397 	 */
398 	clt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP);
399 	test_bind(clt2_sk, &clt2_loop.sa, sizeof(clt2_loop));
400 
401 	/* Do a sendto() without a connect() */
402 	test_sendto(clt2_sk, message, strlen(message), 0, &svr_loop.sa,
403 		    sizeof(svr_loop));
404 
405 	accept2_sk = test_accept(listen_sk, &accept_loop.sa, &addrlen);
406 
407 	test_recv(accept2_sk, msgbuf, 100, 0);
408 
409 	tst_resm(TPASS, "auto-connect using sendto");
410 
411 	outmessage.msg_name = &svr_loop;
412 	outmessage.msg_namelen = sizeof(svr_loop);
413 	outmessage.msg_iov = NULL;
414 	outmessage.msg_iovlen = 0;
415 	outmessage.msg_control = outcmsg;
416 	outmessage.msg_controllen = sizeof(outcmsg);
417 	outmessage.msg_flags = 0;
418 
419 	cmsg = CMSG_FIRSTHDR(&outmessage);
420 	cmsg->cmsg_level = IPPROTO_SCTP;
421 	cmsg->cmsg_type = SCTP_SNDRCV;
422 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
423 	outmessage.msg_controllen = cmsg->cmsg_len;
424 	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
425 	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
426 
427 	/* Verify that SCTP_EOF cannot be used to shutdown an association
428 	 * on a TCP-style socket.
429 	 */
430 	sinfo->sinfo_flags |= SCTP_EOF;
431 	error = sendmsg(clt2_sk, &outmessage, 0);
432 	if ((-1 != error) || (EINVAL != errno))
433 		tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_EOF flag "
434 			 "error:%d, errno:%d", error, errno);
435 
436 	tst_resm(TPASS, "sendmsg with SCTP_EOF flag");
437 
438 	/* Verify that SCTP_ABORT cannot be used to abort an association
439 	 * on a TCP-style socket.
440 	 */
441 	sinfo->sinfo_flags |= SCTP_ABORT;
442 	error = sendmsg(clt2_sk, &outmessage, 0);
443 	if ((-1 != error) || (EINVAL != errno))
444 		tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_ABORT flag "
445 			 "error:%d, errno:%d", error, errno);
446 
447 	tst_resm(TPASS, "sendmsg with SCTP_ABORT flag");
448 
449 	/* Verify that a normal message can be sent using sendmsg. */
450 	outmessage.msg_iov = &out_iov;
451 	outmessage.msg_iovlen = 1;
452 	out_iov.iov_base = message;
453 	out_iov.iov_len = strlen(message) + 1;
454 	sinfo->sinfo_flags = 0;
455 	test_sendmsg(clt2_sk, &outmessage, 0, strlen(message)+1);
456 
457 	test_recv(accept2_sk, msgbuf, 100, 0);
458 
459 	tst_resm(TPASS, "sendmsg with no flags");
460 
461 	close(clt2_sk);
462 	close(accept2_sk);
463 	close(listen_sk);
464 
465         /* Indicate successful completion.  */
466 	return 0;
467 }
468