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 (at) 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 (at) 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 54 char *TCID = __FILE__; 55 int TST_TOTAL = 22; 56 int TST_CNT = 0; 57 58 #define MAX_CLIENTS 10 59 60 int 61 main(int argc, char *argv[]) 62 { 63 int clt_sk[MAX_CLIENTS], accept_sk[MAX_CLIENTS]; 64 int listen_sk, clt2_sk, accept2_sk; 65 sockaddr_storage_t clt_loop[MAX_CLIENTS]; 66 sockaddr_storage_t svr_loop, accept_loop, clt2_loop; 67 socklen_t addrlen; 68 int error, i; 69 char *message = "hello, world!\n"; 70 char msgbuf[100]; 71 int pf_class; 72 struct pollfd poll_fd; 73 fd_set set; 74 struct msghdr outmessage; 75 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; 76 struct iovec out_iov; 77 struct cmsghdr *cmsg; 78 struct sctp_sndrcvinfo *sinfo; 79 struct msghdr inmessage; 80 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))]; 81 char *big_buffer; 82 struct iovec iov; 83 84 /* Rather than fflush() throughout the code, set stdout to 85 * be unbuffered. 86 */ 87 setvbuf(stdout, NULL, _IONBF, 0); 88 89 /* Initialize the server and client addresses. */ 90 #if TEST_V6 91 pf_class = PF_INET6; 92 svr_loop.v6.sin6_family = AF_INET6; 93 svr_loop.v6.sin6_addr = in6addr_loopback; 94 svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1); 95 for (i = 0; i < MAX_CLIENTS; i++) { 96 clt_loop[i].v6.sin6_family = AF_INET6; 97 clt_loop[i].v6.sin6_addr = in6addr_loopback; 98 clt_loop[i].v6.sin6_port = htons(SCTP_TESTPORT_2 + i); 99 } 100 clt2_loop.v6.sin6_family = AF_INET6; 101 clt2_loop.v6.sin6_addr = in6addr_loopback; 102 clt2_loop.v6.sin6_port = htons(SCTP_TESTPORT_2 + i); 103 #else 104 pf_class = PF_INET; 105 svr_loop.v4.sin_family = AF_INET; 106 svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK; 107 svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1); 108 for (i = 0; i < MAX_CLIENTS; i++) { 109 clt_loop[i].v4.sin_family = AF_INET; 110 clt_loop[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK; 111 clt_loop[i].v4.sin_port = htons(SCTP_TESTPORT_2 + i); 112 } 113 clt2_loop.v4.sin_family = AF_INET; 114 clt2_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK; 115 clt2_loop.v4.sin_port = htons(SCTP_TESTPORT_2 + i); 116 #endif 117 118 /* Create and bind the listening server socket. */ 119 listen_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP); 120 test_bind(listen_sk, &svr_loop.sa, sizeof(svr_loop)); 121 122 /* Mark listen_sk as being able to accept new associations. */ 123 test_listen(listen_sk, MAX_CLIENTS-1); 124 125 /* Create and bind the client sockets. */ 126 for (i = 0; i < MAX_CLIENTS; i++) { 127 clt_sk[i] = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP); 128 test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i])); 129 } 130 clt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP); 131 test_bind(clt2_sk, &clt2_loop.sa, sizeof(clt2_loop)); 132 133 addrlen = sizeof(accept_loop); 134 /* Try to do accept on a non-listening socket. It should fail. */ 135 error = accept(clt_sk[0], &accept_loop.sa, &addrlen); 136 if ((-1 != error) && (EINVAL != errno)) 137 tst_brkm(TBROK, tst_exit, "accept on non-listening socket " 138 "error:%d, errno:%d", error, errno); 139 140 tst_resm(TPASS, "accept on non-listening socket"); 141 142 /* Try to do a connect from a listening socket. It should fail. */ 143 error = connect(listen_sk, (struct sockaddr *)&clt_loop[0], 144 sizeof(clt_loop[0])); 145 if ((-1 != error) && (EISCONN != errno)) 146 tst_brkm(TBROK, tst_exit, "connect to non-listening socket " 147 "error:%d, errno:%d", error, errno); 148 149 tst_resm(TPASS, "connect to non-listening socket"); 150 151 /* Do a blocking connect from clt_sk's to listen_sk */ 152 for (i = 0; i < MAX_CLIENTS; i++) 153 test_connect(clt_sk[i], &svr_loop.sa, sizeof(svr_loop)); 154 155 tst_resm(TPASS, "connect to listening socket"); 156 157 /* Verify that no more connect's can be done after the acceptq 158 * backlog has reached the max value. 159 */ 160 error = connect(clt2_sk, &svr_loop.sa, sizeof(svr_loop)); 161 if ((-1 != error) && (ECONNREFUSED != errno)) 162 tst_brkm(TBROK, tst_exit, "connect after max backlog " 163 "error:%d, errno:%d", error, errno); 164 165 tst_resm(TPASS, "connect after max backlog"); 166 167 /* Extract the associations on the listening socket as new sockets. */ 168 for (i = 0; i < MAX_CLIENTS; i++) { 169 poll_fd.fd = listen_sk; 170 poll_fd.events = POLLIN; 171 poll_fd.revents = 0; 172 error = poll(&poll_fd, 1, -1); 173 if ((1 != error) && (1 != poll_fd.revents)) 174 tst_brkm(TBROK, tst_exit, "Unexpected return value " 175 "with poll, error:%d errno:%d, revents:%d", 176 error, errno, poll_fd.revents); 177 178 addrlen = sizeof(accept_loop); 179 accept_sk[i] = test_accept(listen_sk, &accept_loop.sa, 180 &addrlen); 181 } 182 183 tst_resm(TPASS, "accept from listening socket"); 184 185 /* Try to do a connect on an established socket. It should fail. */ 186 error = connect(accept_sk[0], &clt_loop[0].sa, sizeof(clt_loop[0])); 187 if ((-1 != error) || (EISCONN != errno)) 188 tst_brkm(TBROK, tst_exit, "connect on an established socket " 189 "error:%d errno:%d", error, errno); 190 191 tst_resm(TPASS, "connect on an established socket"); 192 193 /* Try to do accept on an established socket. It should fail. */ 194 error = accept(accept_sk[0], &accept_loop.sa, &addrlen); 195 if ((-1 != error) && (EINVAL != errno)) 196 tst_brkm(TBROK, tst_exit, "accept on an established socket " 197 "error:%d errno:%d", error, errno); 198 199 error = accept(clt_sk[0], &accept_loop.sa, &addrlen); 200 if ((-1 != error) && (EINVAL != errno)) 201 tst_brkm(TBROK, tst_exit, "accept on an established socket " 202 "failure: error:%d errno:%d", error, errno); 203 204 tst_resm(TPASS, "accept on an established socket"); 205 206 /* Send and receive a message from the client sockets to the accepted 207 * sockets. 208 */ 209 for (i = 0; i < MAX_CLIENTS; i++) { 210 test_send(clt_sk[i], message, strlen(message), 0); 211 test_recv(accept_sk[i], msgbuf, 100, 0); 212 } 213 214 tst_resm(TPASS, "client sockets -> accepted sockets"); 215 216 /* Send and receive a message from the accepted sockets to the client 217 * sockets. 218 */ 219 for (i = 0; i < MAX_CLIENTS; i++) { 220 test_send(accept_sk[i], message, strlen(message), 0); 221 test_recv(clt_sk[i], msgbuf, 100, 0); 222 } 223 224 tst_resm(TPASS, "accepted sockets -> client sockets"); 225 226 /* Sending a message on a listening socket should fail. */ 227 error = send(listen_sk, message, strlen(message), MSG_NOSIGNAL); 228 if ((-1 != error) || (EPIPE != errno)) 229 tst_brkm(TBROK, tst_exit, "send on a listening socket " 230 "error:%d, errno:%d", error, errno); 231 232 tst_resm(TPASS, "send on a listening socket"); 233 234 /* Trying to receive a message on a listening socket should fail. */ 235 error = recv(listen_sk, msgbuf, 100, 0); 236 if ((-1 != error) || (ENOTCONN != errno)) 237 tst_brkm(TBROK, tst_exit, "recv on a listening socket " 238 "error:%d, errno:%d", error, errno); 239 240 tst_resm(TPASS, "recv on a listening socket"); 241 242 /* TESTCASES for shutdown() */ 243 errno = 0; 244 test_send(accept_sk[0], message, strlen(message), 0); 245 246 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */ 247 test_enable_assoc_change(clt_sk[0]); 248 249 /* Do a SHUT_WR on clt_sk[0] to disable any new sends. */ 250 test_shutdown(clt_sk[0], SHUT_WR); 251 252 /* Reading on a socket that has received SHUTDOWN should return 0 253 * indicating EOF. 254 */ 255 error = recv(accept_sk[0], msgbuf, 100, 0); 256 if ((0 != error) || (0 != errno)) 257 tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN received socket " 258 "error:%d errno:%d", error, errno); 259 260 tst_resm(TPASS, "recv on a SHUTDOWN received socket"); 261 262 /* Read the pending message on clt_sk[0] that was received before 263 * SHUTDOWN call. 264 */ 265 test_recv(clt_sk[0], msgbuf, 100, 0); 266 267 /* Initialize inmessage for all receives. */ 268 big_buffer = test_malloc(REALLY_BIG); 269 memset(&inmessage, 0, sizeof(inmessage)); 270 iov.iov_base = big_buffer; 271 iov.iov_len = REALLY_BIG; 272 inmessage.msg_iov = &iov; 273 inmessage.msg_iovlen = 1; 274 inmessage.msg_control = incmsg; 275 inmessage.msg_controllen = sizeof(incmsg); 276 277 /* Receive the SHUTDOWN_COMP notification as they are enabled. */ 278 error = test_recvmsg(clt_sk[0], &inmessage, MSG_WAITALL); 279 test_check_msg_notification(&inmessage, error, 280 sizeof(struct sctp_assoc_change), 281 SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP); 282 283 tst_resm(TPASS, "recv SHUTDOWN_COMP notification on a SHUT_WR socket"); 284 285 /* No more messages and the association is SHUTDOWN, should fail. */ 286 error = recv(clt_sk[0], msgbuf, 100, 0); 287 if ((-1 != error) || (ENOTCONN != errno)) 288 tst_brkm(TBROK, tst_exit, "recv on a SHUTDOWN sent socket " 289 "error:%d, errno:%d", error, errno); 290 291 tst_resm(TPASS, "recv on a SHUTDOWN sent socket"); 292 293 errno = 0; 294 295 /* Do a SHUT_RD on clt_sk[1] to disable any new receives. */ 296 test_shutdown(clt_sk[1], SHUT_RD); 297 298 error = recv(clt_sk[1], msgbuf, 100, 0); 299 if ((0 != error) || (0 != errno)) 300 tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket " 301 "error:%d, errno:%d", error, errno); 302 303 /* Sending a message on SHUT_RD socket. */ 304 test_send(clt_sk[1], message, strlen(message), 0); 305 306 /* Receive the message sent on SHUT_RD socket. */ 307 test_recv(accept_sk[1], msgbuf, 100, 0); 308 309 /* Send a message to the SHUT_RD socket. */ 310 test_send(accept_sk[1], message, strlen(message), 0); 311 312 /* We should not receive the message as the socket is SHUT_RD */ 313 error = recv(clt_sk[1], msgbuf, 100, 0); 314 if ((0 != error) || (0 != errno)) 315 tst_brkm(TBROK, tst_exit, "recv on a SHUT_RD socket " 316 "error:%d, errno:%d", error, errno); 317 318 tst_resm(TPASS, "recv on a SHUT_RD socket"); 319 320 /* Do a SHUT_RDWR on clt_sk[2] to disable any new sends/receives. */ 321 test_shutdown(clt_sk[2], SHUT_RDWR); 322 323 error = recv(accept_sk[2], msgbuf, 100, 0); 324 if ((0 != error) || (0 != errno)) 325 tst_brkm(TBROK, tst_exit, "recv on a SHUT_RDWR socket " 326 "error:%d, errno:%d", error, errno); 327 328 error = recv(clt_sk[2], msgbuf, 100, 0); 329 if ((0 != error) || (0 != errno)) 330 tst_brkm(TBROK, tst_exit, "recv on a SHUT_RDWR socket " 331 "error:%d, errno:%d", error, errno); 332 333 tst_resm(TPASS, "recv on a SHUT_RDWR socket"); 334 335 error = 0; 336 337 for (i = 0; i < MAX_CLIENTS; i++) 338 close(clt_sk[i]); 339 for (i = 0; i < MAX_CLIENTS; i++) 340 close(accept_sk[i]); 341 342 /* Test case to verify accept of a CLOSED association. */ 343 /* Do a connect, send and a close to ESTABLISH and CLOSE an 344 * association on the listening socket. 345 */ 346 test_connect(clt2_sk, &svr_loop.sa, sizeof(svr_loop)); 347 348 test_send(clt2_sk, message, strlen(message), 0); 349 350 close(clt2_sk); 351 352 FD_ZERO(&set); 353 FD_SET(listen_sk, &set); 354 355 error = select(listen_sk + 1, &set, NULL, NULL, NULL); 356 if (1 != error) 357 tst_brkm(TBROK, tst_exit, "select error:%d, " 358 "errno: %d", error, errno); 359 360 /* Now accept the CLOSED association waiting on the listening 361 * socket. 362 */ 363 accept2_sk = test_accept(listen_sk, &accept_loop.sa, &addrlen); 364 365 /* Receive the message sent before doing a close. */ 366 test_recv(accept2_sk, msgbuf, 100, 0); 367 368 /* Receive EOF indication as there are no more messages and the 369 * socket is SHUTDOWN. 370 */ 371 error = recv(accept2_sk, msgbuf, 100, 0); 372 if ((0 != error) || (0 != errno)) 373 tst_brkm(TBROK, tst_exit, "Unexpected error return on " 374 "recv(error:%d, errno:%d)", error, errno); 375 376 tst_resm(TPASS, "accept of a CLOSED association"); 377 378 /* Trying to send a message over the CLOSED association should 379 * generate EPIPE. 380 */ 381 error = send(accept2_sk, message, strlen(message), MSG_NOSIGNAL); 382 if ((-1 != error) || (EPIPE != errno)) 383 tst_brkm(TBROK, tst_exit, "send to a CLOSED association " 384 "error:%d, errno:%d", error, errno); 385 386 tst_resm(TPASS, "send to a CLOSED association"); 387 388 error = 0; 389 close(accept2_sk); 390 391 /* Verify that auto-connect can be done on a TCP-style socket using 392 * sendto/sendmsg. 393 */ 394 clt2_sk = test_socket(pf_class, SOCK_STREAM, IPPROTO_SCTP); 395 test_bind(clt2_sk, &clt2_loop.sa, sizeof(clt2_loop)); 396 397 /* Do a sendto() without a connect() */ 398 test_sendto(clt2_sk, message, strlen(message), 0, &svr_loop.sa, 399 sizeof(svr_loop)); 400 401 accept2_sk = test_accept(listen_sk, &accept_loop.sa, &addrlen); 402 403 test_recv(accept2_sk, msgbuf, 100, 0); 404 405 tst_resm(TPASS, "auto-connect using sendto"); 406 407 outmessage.msg_name = &svr_loop; 408 outmessage.msg_namelen = sizeof(svr_loop); 409 outmessage.msg_iov = NULL; 410 outmessage.msg_iovlen = 0; 411 outmessage.msg_control = outcmsg; 412 outmessage.msg_controllen = sizeof(outcmsg); 413 outmessage.msg_flags = 0; 414 415 cmsg = CMSG_FIRSTHDR(&outmessage); 416 cmsg->cmsg_level = IPPROTO_SCTP; 417 cmsg->cmsg_type = SCTP_SNDRCV; 418 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); 419 outmessage.msg_controllen = cmsg->cmsg_len; 420 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); 421 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); 422 423 /* Verify that SCTP_EOF cannot be used to shutdown an association 424 * on a TCP-style socket. 425 */ 426 sinfo->sinfo_flags |= SCTP_EOF; 427 error = sendmsg(clt2_sk, &outmessage, 0); 428 if ((-1 != error) || (EINVAL != errno)) 429 tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_EOF flag " 430 "error:%d, errno:%d", error, errno); 431 432 tst_resm(TPASS, "sendmsg with SCTP_EOF flag"); 433 434 /* Verify that SCTP_ABORT cannot be used to abort an association 435 * on a TCP-style socket. 436 */ 437 sinfo->sinfo_flags |= SCTP_ABORT; 438 error = sendmsg(clt2_sk, &outmessage, 0); 439 if ((-1 != error) || (EINVAL != errno)) 440 tst_brkm(TBROK, tst_exit, "sendmsg with SCTP_ABORT flag " 441 "error:%d, errno:%d", error, errno); 442 443 tst_resm(TPASS, "sendmsg with SCTP_ABORT flag"); 444 445 /* Verify that a normal message can be sent using sendmsg. */ 446 outmessage.msg_iov = &out_iov; 447 outmessage.msg_iovlen = 1; 448 out_iov.iov_base = message; 449 out_iov.iov_len = strlen(message) + 1; 450 sinfo->sinfo_flags = 0; 451 test_sendmsg(clt2_sk, &outmessage, 0, strlen(message)+1); 452 453 test_recv(accept2_sk, msgbuf, 100, 0); 454 455 tst_resm(TPASS, "sendmsg with no flags"); 456 457 close(clt2_sk); 458 close(accept2_sk); 459 close(listen_sk); 460 461 /* Indicate successful completion. */ 462 return 0; 463 } 464