1/* SCTP kernel Implementation 2 * (C) Copyright IBM Corp. 2001, 2003 3 * Copyright (c) 1999-2000 Cisco, Inc. 4 * Copyright (c) 1999-2001 Motorola, Inc. 5 * Copyright (c) 2001 Intel Corp. 6 * Copyright (c) 2001 Nokia, Inc. 7 * 8 * The SCTP implementation is free software; 9 * you can redistribute it and/or modify it under the terms of 10 * the GNU General Public License as published by 11 * the Free Software Foundation; either version 2, or (at your option) 12 * any later version. 13 * 14 * The SCTP implementation is distributed in the hope that it 15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied 16 * ************************ 17 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 18 * See the GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with GNU CC; see the file COPYING. If not, write to 22 * the Free Software Foundation, 59 Temple Place - Suite 330, 23 * Boston, MA 02111-1307, USA. 24 * 25 * Please send any bug reports or fixes you make to the 26 * email address(es): 27 * lksctp developers <lksctp-developers@lists.sourceforge.net> 28 * 29 * Or submit a bug report through the following website: 30 * http://www.sf.net/projects/lksctp 31 * 32 * Any bugs reported to us we will try to fix... any fixes shared will 33 * be incorporated into the next SCTP release. 34 * 35 * Written or modified by: 36 * La Monte H.P. Yarroll <piggy@acm.org> 37 * Karl Knutson <karl@athena.chicago.il.us> 38 * Hui Huang <hui.huang@nokia.com> 39 * Jon Grimm <jgrimm@us.ibm.com> 40 * Sridhar Samudrala <samudrala@us.ibm.com> 41 */ 42 43/* This is a basic functional test for the SCTP kernel 44 * implementation state machine. 45 */ 46 47#include <stdio.h> 48#include <unistd.h> 49#include <stdlib.h> 50#include <string.h> 51#include <sys/types.h> 52#include <sys/socket.h> 53#include <sys/uio.h> 54#include <netinet/in.h> 55#include <sys/errno.h> 56#include <errno.h> 57#include <netinet/sctp.h> 58#include <sctputil.h> 59#include "tst_kernel.h" 60 61char *TCID = __FILE__; 62int TST_TOTAL = 15; 63int TST_CNT = 0; 64 65int main(void) 66{ 67 int sk1, sk2; 68 sockaddr_storage_t loop1; 69 sockaddr_storage_t loop2; 70 sockaddr_storage_t msgname; 71 struct iovec iov; 72 struct msghdr inmessage; 73 struct msghdr outmessage; 74 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))]; 75 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; 76 struct cmsghdr *cmsg; 77 struct sctp_sndrcvinfo *sinfo; 78 struct iovec out_iov; 79 char *message = "hello, world!\n"; 80 char *telephone = "Watson, come here! I need you!\n"; 81 char *telephone_resp = "I already brought your coffee...\n"; 82 int error, bytes_sent; 83 int pf_class; 84 uint32_t ppid; 85 uint32_t stream; 86 sctp_assoc_t associd1, associd2; 87 struct sctp_assoc_change *sac; 88 char *big_buffer; 89 struct sockaddr *laddrs, *paddrs; 90 int n_laddrs, n_paddrs, i; 91 struct sockaddr *sa_addr; 92 struct sockaddr_in *in_addr; 93 struct sockaddr_in6 *in6_addr; 94 void *addr_buf; 95 96 if (tst_check_driver("sctp")) 97 tst_brkm(TCONF, tst_exit, "sctp driver not available"); 98 99 /* Rather than fflush() throughout the code, set stdout to 100 * be unbuffered. 101 */ 102 setvbuf(stdout, NULL, _IONBF, 0); 103 104 /* Set some basic values which depend on the address family. */ 105#if TEST_V6 106 pf_class = PF_INET6; 107 108 loop1.v6.sin6_family = AF_INET6; 109 loop1.v6.sin6_addr = (struct in6_addr)SCTP_IN6ADDR_ANY_INIT; 110 loop1.v6.sin6_port = htons(SCTP_TESTPORT_1); 111 112 loop2.v6.sin6_family = AF_INET6; 113 loop2.v6.sin6_addr = in6addr_loopback; 114 loop2.v6.sin6_port = htons(SCTP_TESTPORT_2); 115#else 116 pf_class = PF_INET; 117 118 loop1.v4.sin_family = AF_INET; 119 loop1.v4.sin_addr.s_addr = INADDR_ANY; 120 loop1.v4.sin_port = htons(SCTP_TESTPORT_1); 121 122 loop2.v4.sin_family = AF_INET; 123 loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK; 124 loop2.v4.sin_port = htons(SCTP_TESTPORT_2); 125#endif /* TEST_V6 */ 126 127 /* Create the two endpoints which will talk to each other. */ 128 sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP); 129 sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP); 130 131 tst_resm(TPASS, "socket"); 132 133 /* Bind these sockets to the test ports. */ 134 test_bind(sk1, &loop1.sa, sizeof(loop1)); 135 test_bind(sk2, &loop2.sa, sizeof(loop2)); 136 137 tst_resm(TPASS, "bind"); 138 139 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */ 140 test_enable_assoc_change(sk1); 141 test_enable_assoc_change(sk2); 142 143 /* Initialize inmessage for all receives. */ 144 big_buffer = test_malloc(REALLY_BIG); 145 memset(&inmessage, 0, sizeof(inmessage)); 146 iov.iov_base = big_buffer; 147 iov.iov_len = REALLY_BIG; 148 inmessage.msg_iov = &iov; 149 inmessage.msg_iovlen = 1; 150 inmessage.msg_control = incmsg; 151 inmessage.msg_name = &msgname; 152 153 /* Try to read on socket 2. This should fail since we are 154 * neither listening, nor established. 155 */ 156 inmessage.msg_controllen = sizeof(incmsg); 157 error = recvmsg(sk2, &inmessage, MSG_WAITALL); 158 if (error > 0) 159 tst_brkm(TBROK, tst_exit, "recvmsg on a socket neither " 160 "listening nor established error: %d", error); 161 162 tst_resm(TPASS, "recvmsg on a socket neither listening nor " 163 "established"); 164 165 /* Mark sk2 as being able to accept new associations. */ 166 error = test_listen(sk2, 1); 167 168 tst_resm(TPASS, "listen"); 169 170 /* Send the first message. This will create the association. */ 171 outmessage.msg_name = &loop2; 172 outmessage.msg_namelen = sizeof(loop2); 173 outmessage.msg_iov = &out_iov; 174 outmessage.msg_iovlen = 1; 175 outmessage.msg_control = outcmsg; 176 outmessage.msg_controllen = sizeof(outcmsg); 177 outmessage.msg_flags = 0; 178 cmsg = CMSG_FIRSTHDR(&outmessage); 179 cmsg->cmsg_level = IPPROTO_SCTP; 180 cmsg->cmsg_type = SCTP_SNDRCV; 181 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); 182 outmessage.msg_controllen = cmsg->cmsg_len; 183 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); 184 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); 185 ppid = rand(); /* Choose an arbitrary value. */ 186 stream = 1; 187 sinfo->sinfo_ppid = ppid; 188 sinfo->sinfo_stream = stream; 189 outmessage.msg_iov->iov_base = message; 190 outmessage.msg_iov->iov_len = strlen(message) + 1; 191 test_sendmsg(sk1, &outmessage, 0, strlen(message)+1); 192 193 tst_resm(TPASS, "sendmsg with a valid msg_name"); 194 195 /* Get the communication up message on sk2. */ 196 inmessage.msg_controllen = sizeof(incmsg); 197 inmessage.msg_namelen = sizeof(msgname); 198 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); 199 test_check_msg_notification(&inmessage, error, 200 sizeof(struct sctp_assoc_change), 201 SCTP_ASSOC_CHANGE, SCTP_COMM_UP); 202#if TEST_V6 203 204 if (inmessage.msg_namelen != sizeof(struct sockaddr_in6)) { 205 DUMP_CORE; 206 } 207 if (msgname.v6.sin6_port != htons(SCTP_TESTPORT_1)) { 208 DUMP_CORE; 209 } 210 211 if (msgname.v6.sin6_family != AF_INET6) { 212 DUMP_CORE; 213 } 214 215 if (memcmp(&msgname.v6.sin6_addr, &in6addr_loopback, 216 sizeof(msgname.v6.sin6_addr))) { 217 DUMP_CORE; 218 } 219#else 220 if (inmessage.msg_namelen != sizeof(struct sockaddr_in)) { 221 DUMP_CORE; 222 } 223 if (msgname.v4.sin_port != htons(SCTP_TESTPORT_1)) { 224 DUMP_CORE; 225 } 226 227 if (msgname.v4.sin_family != AF_INET) { 228 DUMP_CORE; 229 } 230 if (msgname.v4.sin_addr.s_addr != SCTP_IP_LOOPBACK) { 231 DUMP_CORE; 232 } 233#endif 234 sac = (struct sctp_assoc_change *)iov.iov_base; 235 associd2 = sac->sac_assoc_id; 236 237 /* Get the communication up message on sk1. */ 238 iov.iov_base = big_buffer; 239 iov.iov_len = REALLY_BIG; 240 inmessage.msg_control = incmsg; 241 inmessage.msg_controllen = sizeof(incmsg); 242 error = test_recvmsg(sk1, &inmessage, MSG_WAITALL); 243 test_check_msg_notification(&inmessage, error, 244 sizeof(struct sctp_assoc_change), 245 SCTP_ASSOC_CHANGE, SCTP_COMM_UP); 246 sac = (struct sctp_assoc_change *)iov.iov_base; 247 associd1 = sac->sac_assoc_id; 248 249 tst_resm(TPASS, "recvmsg COMM_UP notifications"); 250 251 /* Get the first message which was sent. */ 252 inmessage.msg_controllen = sizeof(incmsg); 253 inmessage.msg_namelen = sizeof(msgname); 254 memset(&msgname, 0, sizeof(msgname)); 255 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); 256 test_check_msg_data(&inmessage, error, strlen(message) + 1, 257 MSG_EOR, stream, ppid); 258#if TEST_V6 259 260 if (inmessage.msg_namelen != sizeof(struct sockaddr_in6)) { 261 DUMP_CORE; 262 } 263 if (msgname.v6.sin6_port != htons(SCTP_TESTPORT_1)) { 264 DUMP_CORE; 265 } 266 267 if (msgname.v6.sin6_family != AF_INET6) { 268 DUMP_CORE; 269 } 270 271 if (memcmp(&msgname.v6.sin6_addr, &in6addr_loopback, 272 sizeof(msgname.v6.sin6_addr))) { 273 DUMP_CORE; 274 } 275#else 276 if (inmessage.msg_namelen != sizeof(struct sockaddr_in)) { 277 DUMP_CORE; 278 } 279 if (msgname.v4.sin_port != htons(SCTP_TESTPORT_1)) { 280 DUMP_CORE; 281 } 282 if (msgname.v4.sin_family != AF_INET) { 283 DUMP_CORE; 284 } 285 if (msgname.v4.sin_addr.s_addr != SCTP_IP_LOOPBACK) { 286 DUMP_CORE; 287 } 288#endif 289 290 /* Try to send a message with NULL msg_name and associd, should fail */ 291 outmessage.msg_controllen = sizeof(outcmsg); 292 outmessage.msg_flags = 0; 293 cmsg = CMSG_FIRSTHDR(&outmessage); 294 cmsg->cmsg_level = IPPROTO_SCTP; 295 cmsg->cmsg_type = SCTP_SNDRCV; 296 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); 297 outmessage.msg_controllen = cmsg->cmsg_len; 298 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); 299 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); 300 ppid++; 301 stream++; 302 sinfo->sinfo_ppid = ppid; 303 sinfo->sinfo_stream = stream; 304 outmessage.msg_iov->iov_base = telephone; 305 outmessage.msg_iov->iov_len = strlen(telephone) + 1; 306 outmessage.msg_name = NULL; 307 outmessage.msg_namelen = 0; 308 bytes_sent = sendmsg(sk1, &outmessage, MSG_NOSIGNAL); 309 if ((bytes_sent > 0) || (EPIPE != errno)) 310 tst_brkm(TBROK, tst_exit, "sendmsg with NULL associd and " 311 "NULL msg_name error:%d errno:%d", error, errno); 312 313 tst_resm(TPASS, "sendmsg with NULL associd and NULL msg_name"); 314 315 /* Fill in a incorrect assoc_id, which should cause an error. */ 316 sinfo->sinfo_assoc_id = associd2; 317 bytes_sent = sendmsg(sk1, &outmessage, MSG_NOSIGNAL); 318 if ((bytes_sent > 0) || (EPIPE != errno)) 319 tst_brkm(TBROK, tst_exit, "sendmsg with incorrect associd " 320 "error:%d errno:%d", error, errno); 321 322 tst_resm(TPASS, "sendmsg with incorrect associd"); 323 324 /* Fill in a correct assoc_id and get back to the normal testing. */ 325 sinfo->sinfo_assoc_id = associd1; 326 /* Send two more messages, to cause a second SACK. */ 327 test_sendmsg(sk1, &outmessage, 0, strlen(telephone)+1); 328 329 outmessage.msg_name = &loop2; 330 outmessage.msg_namelen = sizeof(loop2); 331 outmessage.msg_iov->iov_base = telephone_resp; 332 outmessage.msg_iov->iov_len = strlen(telephone_resp) + 1; 333 test_sendmsg(sk1, &outmessage, 0, strlen(telephone_resp)+1); 334 335 tst_resm(TPASS, "sendmsg with valid associd"); 336 337 /* Get those two messages. */ 338 inmessage.msg_controllen = sizeof(incmsg); 339 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); 340 test_check_msg_data(&inmessage, error, strlen(telephone) + 1, 341 MSG_EOR, stream, ppid); 342 343 inmessage.msg_controllen = sizeof(incmsg); 344 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); 345 test_check_msg_data(&inmessage, error, strlen(telephone_resp) + 1, 346 MSG_EOR, stream, ppid); 347 348 tst_resm(TPASS, "recvmsg"); 349 350 n_laddrs = sctp_getladdrs(sk1, associd1, &laddrs); 351 if (n_laddrs <= 0) 352 tst_brkm(TBROK, tst_exit, "sctp_getladdrs: %s", 353 strerror(errno)); 354 355 tst_resm(TPASS, "sctp_getladdrs"); 356 357 addr_buf = (void *)laddrs; 358 for (i = 0; i < n_laddrs; i++) { 359 sa_addr = (struct sockaddr *)addr_buf; 360 if (AF_INET == sa_addr->sa_family) { 361 in_addr = (struct sockaddr_in *)sa_addr; 362 tst_resm(TINFO, "LOCAL ADDR %d.%d.%d.%d PORT %d", 363 NIPQUAD(in_addr->sin_addr), 364 ntohs(in_addr->sin_port)); 365 addr_buf += sizeof(struct sockaddr_in); 366 } else { 367 in6_addr = (struct sockaddr_in6 *)sa_addr; 368 tst_resm(TINFO, 369 "LOCAL ADDR %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x PORT %d", 370 NIP6(in6_addr->sin6_addr), 371 ntohs(in6_addr->sin6_port)); 372 addr_buf += sizeof(struct sockaddr_in6); 373 } 374 } 375 376 sctp_freeladdrs(laddrs); 377 378 tst_resm(TPASS, "sctp_freeladdrs"); 379 380 n_paddrs = sctp_getpaddrs(sk1, associd1, &paddrs); 381 if (n_paddrs <= 0) 382 tst_brkm(TBROK, tst_exit, "sctp_getpaddrs: %s", 383 strerror(errno)); 384 385 tst_resm(TPASS, "sctp_getpaddrs"); 386 387 addr_buf = (void *)paddrs; 388 for (i = 0; i < n_paddrs; i++) { 389 sa_addr = (struct sockaddr *)addr_buf; 390 if (AF_INET == sa_addr->sa_family) { 391 in_addr = (struct sockaddr_in *)sa_addr; 392 tst_resm(TINFO, "PEER ADDR %d.%d.%d.%d PORT %d", 393 NIPQUAD(in_addr->sin_addr), 394 ntohs(in_addr->sin_port)); 395 addr_buf += sizeof(struct sockaddr_in); 396 } else { 397 in6_addr = (struct sockaddr_in6 *)sa_addr; 398 tst_resm(TINFO, 399 "PEER ADDR %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x PORT %d", 400 NIP6(in6_addr->sin6_addr), 401 ntohs(in6_addr->sin6_port)); 402 addr_buf += sizeof(struct sockaddr_in6); 403 } 404 } 405 406 sctp_freepaddrs(paddrs); 407 408 tst_resm(TPASS, "sctp_freepaddrs"); 409 410 /* Shut down the link. */ 411 close(sk1); 412 413 /* Get the shutdown complete notification. */ 414 inmessage.msg_controllen = sizeof(incmsg); 415 inmessage.msg_namelen = sizeof(msgname); 416 memset(&msgname, 0, sizeof(msgname)); 417 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); 418 test_check_msg_notification(&inmessage, error, 419 sizeof(struct sctp_assoc_change), 420 SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP); 421#if TEST_V6 422 423 if (inmessage.msg_namelen != sizeof(struct sockaddr_in6)) { 424 DUMP_CORE; 425 } 426 if (msgname.v6.sin6_port != htons(SCTP_TESTPORT_1)) { 427 DUMP_CORE; 428 } 429 430 if (msgname.v6.sin6_family != AF_INET6) { 431 DUMP_CORE; 432 } 433 434 if (memcmp(&msgname.v6.sin6_addr, &in6addr_loopback, 435 sizeof(msgname.v6.sin6_addr))) { 436 DUMP_CORE; 437 } 438#else 439 if (inmessage.msg_namelen != sizeof(struct sockaddr_in)) { 440 DUMP_CORE; 441 } 442 if (msgname.v4.sin_port != htons(SCTP_TESTPORT_1)) { 443 DUMP_CORE; 444 } 445 446 if (msgname.v4.sin_family != AF_INET) { 447 DUMP_CORE; 448 } 449 if (msgname.v4.sin_addr.s_addr != SCTP_IP_LOOPBACK) { 450 DUMP_CORE; 451 } 452#endif 453 454 tst_resm(TPASS, "recvmsg SHUTDOWN_COMP notification"); 455 456 close(sk2); 457 458 /* Indicate successful completion. */ 459 return 0; 460} 461