| 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 <[email protected]> |
| 28 | * |
| 29 | * Or submit a bug report through the following website: |
| 30 | * https://github.com/sctp/lksctp-tools/ |
| 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 <[email protected]> |
| 37 | * Karl Knutson <[email protected]> |
| 38 | * Hui Huang <[email protected]> |
| 39 | * Jon Grimm <[email protected]> |
| 40 | * Sridhar Samudrala <[email protected]> |
| 41 | */ |
| 42 | |
| 43 | /* This is a functional test to verify the data fragmentation, reassembly |
| 44 | * support and SCTP_DISABLE_FRAGMENTS socket option. |
| 45 | * The following tests are done in sequence. |
| 46 | * - Verify SCTP_DISABLE_FRAGMENTS socket option by doing a setsockopt() |
| 47 | * followed by a getsockopt(). |
| 48 | * - Verify that a message size exceeding the association fragmentation |
| 49 | * point cannot be sent when fragmentation is disabled. |
| 50 | * - Send and receive a set of messages that are bigger than the path mtu. |
| 51 | * The different message sizes to be tested are specified in the array |
| 52 | * msg_sizes[]. |
| 53 | */ |
| 54 | |
| 55 | #include <stdio.h> |
| 56 | #include <unistd.h> |
| 57 | #include <stdlib.h> |
| 58 | #include <string.h> |
| 59 | #include <sys/types.h> |
| 60 | #include <sys/socket.h> |
| 61 | #include <sys/uio.h> |
| 62 | #include <netinet/in.h> |
| 63 | #include <errno.h> |
| 64 | #include <netinet/sctp.h> |
| 65 | #include <sctputil.h> |
| 66 | |
| 67 | char *TCID = __FILE__; |
| 68 | int TST_TOTAL = 4; |
| 69 | int TST_CNT = 0; |
| 70 | |
| 71 | int msg_sizes[] = {1353, 2000, 5000, 10000, 20000, 32768}; |
| 72 | |
| 73 | int |
| 74 | main(int argc, char *argv[]) |
| 75 | { |
| 76 | int sk1, sk2; |
| 77 | sockaddr_storage_t loop1; |
| 78 | sockaddr_storage_t loop2; |
| 79 | struct iovec iov; |
| 80 | struct msghdr inmessage; |
| 81 | struct msghdr outmessage; |
| 82 | char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))]; |
| 83 | char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 84 | struct cmsghdr *cmsg; |
| 85 | struct sctp_sndrcvinfo *sinfo; |
| 86 | struct iovec out_iov; |
| 87 | int error, bytes_sent; |
| 88 | int pf_class; |
| 89 | uint32_t ppid; |
| 90 | uint32_t stream; |
| 91 | char *big_buffer; |
| 92 | int msg_len, msg_cnt, i; |
| 93 | void *msg_buf; |
| 94 | int disable_frag; |
| 95 | socklen_t optlen; |
| 96 | |
| 97 | /* Rather than fflush() throughout the code, set stdout to |
| 98 | * be unbuffered. |
| 99 | */ |
| 100 | setvbuf(stdout, NULL, _IONBF, 0); |
| 101 | |
| 102 | /* Set some basic values which depend on the address family. */ |
| 103 | #if TEST_V6 |
| 104 | pf_class = PF_INET6; |
| 105 | |
| 106 | loop1.v6.sin6_family = AF_INET6; |
| 107 | loop1.v6.sin6_addr = in6addr_loopback; |
| 108 | loop1.v6.sin6_port = htons(SCTP_TESTPORT_1); |
| 109 | |
| 110 | loop2.v6.sin6_family = AF_INET6; |
| 111 | loop2.v6.sin6_addr = in6addr_loopback; |
| 112 | loop2.v6.sin6_port = htons(SCTP_TESTPORT_2); |
| 113 | #else |
| 114 | pf_class = PF_INET; |
| 115 | |
| 116 | loop1.v4.sin_family = AF_INET; |
| 117 | loop1.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 118 | loop1.v4.sin_port = htons(SCTP_TESTPORT_1); |
| 119 | |
| 120 | loop2.v4.sin_family = AF_INET; |
| 121 | loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 122 | loop2.v4.sin_port = htons(SCTP_TESTPORT_2); |
| 123 | #endif /* TEST_V6 */ |
| 124 | |
| 125 | /* Create the two endpoints which will talk to each other. */ |
| 126 | sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 127 | sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 128 | |
| 129 | /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */ |
| 130 | test_enable_assoc_change(sk1); |
| 131 | test_enable_assoc_change(sk2); |
| 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 | /* Mark sk2 as being able to accept new associations. */ |
| 138 | test_listen(sk2, 1); |
| 139 | |
| 140 | /* Send the first message. This will create the association. */ |
| 141 | outmessage.msg_name = &loop2; |
| 142 | outmessage.msg_namelen = sizeof(loop2); |
| 143 | outmessage.msg_iov = &out_iov; |
| 144 | outmessage.msg_iovlen = 1; |
| 145 | outmessage.msg_control = outcmsg; |
| 146 | outmessage.msg_controllen = sizeof(outcmsg); |
| 147 | outmessage.msg_flags = 0; |
| 148 | cmsg = CMSG_FIRSTHDR(&outmessage); |
| 149 | cmsg->cmsg_level = IPPROTO_SCTP; |
| 150 | cmsg->cmsg_type = SCTP_SNDRCV; |
| 151 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 152 | outmessage.msg_controllen = cmsg->cmsg_len; |
| 153 | sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
| 154 | memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); |
| 155 | ppid = rand(); /* Choose an arbitrary value. */ |
| 156 | stream = 1; |
| 157 | sinfo->sinfo_ppid = ppid; |
| 158 | sinfo->sinfo_stream = stream; |
| 159 | msg_len = 10; |
| 160 | msg_buf = test_build_msg(10); |
| 161 | outmessage.msg_iov->iov_base = msg_buf; |
| 162 | outmessage.msg_iov->iov_len = msg_len; |
| 163 | test_sendmsg(sk1, &outmessage, 0, msg_len); |
| 164 | |
| 165 | |
| 166 | /* Initialize inmessage for all receives. */ |
| 167 | big_buffer = test_malloc(REALLY_BIG); |
| 168 | memset(&inmessage, 0, sizeof(inmessage)); |
| 169 | iov.iov_base = big_buffer; |
| 170 | iov.iov_len = REALLY_BIG; |
| 171 | inmessage.msg_iov = &iov; |
| 172 | inmessage.msg_iovlen = 1; |
| 173 | inmessage.msg_control = incmsg; |
| 174 | |
| 175 | /* Get the communication up message on sk2. */ |
| 176 | inmessage.msg_controllen = sizeof(incmsg); |
| 177 | error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); |
| 178 | test_check_msg_notification(&inmessage, error, |
| 179 | sizeof(struct sctp_assoc_change), |
| 180 | SCTP_ASSOC_CHANGE, SCTP_COMM_UP); |
| 181 | #if 0 |
| 182 | sac = (struct sctp_assoc_change *)iov.iov_base; |
| 183 | associd2 = sac->sac_assoc_id; |
| 184 | #endif |
| 185 | /* Get the communication up message on sk1. */ |
| 186 | inmessage.msg_controllen = sizeof(incmsg); |
| 187 | error = test_recvmsg(sk1, &inmessage, MSG_WAITALL); |
| 188 | test_check_msg_notification(&inmessage, error, |
| 189 | sizeof(struct sctp_assoc_change), |
| 190 | SCTP_ASSOC_CHANGE, SCTP_COMM_UP); |
| 191 | #if 0 |
| 192 | sac = (struct sctp_assoc_change *)iov.iov_base; |
| 193 | associd1 = sac->sac_assoc_id; |
| 194 | #endif |
| 195 | /* Get the first message which was sent. */ |
| 196 | inmessage.msg_controllen = sizeof(incmsg); |
| 197 | error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); |
| 198 | test_check_msg_data(&inmessage, error, msg_len, MSG_EOR, stream, ppid); |
| 199 | |
| 200 | free(msg_buf); |
| 201 | |
| 202 | /* Disable fragmentation. */ |
| 203 | disable_frag = 1; |
| 204 | test_setsockopt(sk1, SCTP_DISABLE_FRAGMENTS, &disable_frag, |
| 205 | sizeof(disable_frag)); |
| 206 | |
| 207 | tst_resm(TPASS, "setsockopt(SCTP_DISABLE_FRAGMENTS)"); |
| 208 | |
| 209 | /* Do a getsockopt() and verify that fragmentation is disabled. */ |
| 210 | disable_frag = 0; |
| 211 | optlen = sizeof(disable_frag); |
| 212 | error = test_getsockopt(sk1, SCTP_DISABLE_FRAGMENTS, &disable_frag, |
| 213 | &optlen); |
| 214 | if ((error != 0) && (disable_frag != 1)) |
| 215 | tst_brkm(TBROK, tst_exit, "getsockopt(SCTP_DISABLE_FRAGMENTS) " |
| 216 | "error:%d errno:%d disable_frag:%d", |
| 217 | error, errno, disable_frag); |
| 218 | |
| 219 | tst_resm(TPASS, "getsockopt(SCTP_DISABLE_FRAGMENTS)"); |
| 220 | |
| 221 | /* Try to send a messsage that exceeds association fragmentation point |
| 222 | * and verify that it fails. |
| 223 | */ |
| 224 | msg_len = 100000; |
| 225 | msg_buf = test_build_msg(msg_len); |
| 226 | outmessage.msg_iov->iov_base = msg_buf; |
| 227 | outmessage.msg_iov->iov_len = msg_len; |
| 228 | error = sendmsg(sk1, &outmessage, 0); |
| 229 | if ((error != -1) || (errno != EMSGSIZE)) |
| 230 | tst_brkm(TBROK, tst_exit, "Send a message that exceeds " |
| 231 | "assoc frag point error:%d errno:%d", error, errno); |
| 232 | free(msg_buf); |
| 233 | |
| 234 | tst_resm(TPASS, "Send a message that exceeds assoc frag point"); |
| 235 | |
| 236 | /* Enable Fragmentation. */ |
| 237 | disable_frag = 0; |
| 238 | test_setsockopt(sk1, SCTP_DISABLE_FRAGMENTS, &disable_frag, |
| 239 | sizeof(disable_frag)); |
| 240 | |
| 241 | msg_cnt = sizeof(msg_sizes) / sizeof(int); |
| 242 | |
| 243 | /* Send and receive the messages of different sizes specified in the |
| 244 | * msg_sizes array in a loop. |
| 245 | */ |
| 246 | for (i = 0; i < msg_cnt; i++) { |
| 247 | |
| 248 | msg_len = msg_sizes[i]; |
| 249 | msg_buf = test_build_msg(msg_len); |
| 250 | outmessage.msg_iov->iov_base = msg_buf; |
| 251 | outmessage.msg_iov->iov_len = msg_len; |
| 252 | bytes_sent = test_sendmsg(sk1, &outmessage, 0, msg_len); |
| 253 | |
| 254 | tst_resm(TINFO, "Sent %d byte message", bytes_sent); |
| 255 | |
| 256 | inmessage.msg_controllen = sizeof(incmsg); |
| 257 | error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); |
| 258 | /* Handle Partial Reads. */ |
| 259 | if (inmessage.msg_flags & MSG_EOR) { |
| 260 | test_check_msg_data(&inmessage, error, bytes_sent, |
| 261 | MSG_EOR, stream, ppid); |
| 262 | tst_resm(TINFO, "Received %d byte message", error); |
| 263 | } else { |
| 264 | int remain; |
| 265 | |
| 266 | test_check_msg_data(&inmessage, error, error, 0, |
| 267 | stream, ppid); |
| 268 | tst_resm(TINFO, "Received %d byte message", error); |
| 269 | |
| 270 | /* Read the remaining message. */ |
| 271 | inmessage.msg_controllen = sizeof(incmsg); |
| 272 | remain = test_recvmsg(sk2, &inmessage, MSG_WAITALL); |
| 273 | test_check_msg_data(&inmessage, remain, |
| 274 | bytes_sent - error, |
| 275 | MSG_EOR, stream, ppid); |
| 276 | tst_resm(TINFO, "Received %d byte message", error); |
| 277 | } |
| 278 | |
| 279 | free(msg_buf); |
| 280 | } |
| 281 | |
| 282 | tst_resm(TPASS, "Send/Receive fragmented messages"); |
| 283 | |
| 284 | /* Shut down the link. */ |
| 285 | close(sk1); |
| 286 | |
| 287 | /* Get the shutdown complete notification. */ |
| 288 | inmessage.msg_controllen = sizeof(incmsg); |
| 289 | error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); |
| 290 | test_check_msg_notification(&inmessage, error, |
| 291 | sizeof(struct sctp_assoc_change), |
| 292 | SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP); |
| 293 | |
| 294 | close(sk2); |
| 295 | |
| 296 | free(big_buffer); |
| 297 | /* Indicate successful completion. */ |
| 298 | return 0; |
| 299 | } |