| 1 | /* This purpose of this test is to examine the |
| 2 | * support of the interleaving |
| 3 | * The following tests are done in sequence: |
| 4 | * - Verify SCTP_FRAGMENT_INTERLEAVE and SCTP_INTERLEAVING_SUPPORTED |
| 5 | * socket option by doing a setsockopt() followed by a getsockopt() |
| 6 | * - Verify the integrity of the data sent by client using a sendmsg() |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <unistd.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <sys/socket.h> |
| 15 | #include <sys/uio.h> |
| 16 | #include <netinet/in.h> |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <netinet/sctp.h> |
| 20 | #include <sctputil.h> |
| 21 | |
| 22 | #define MSG_SIZE 1000 |
| 23 | |
| 24 | char *TCID = __FILE__; |
| 25 | int TST_TOTAL = 2; |
| 26 | int TST_CNT = 0; |
| 27 | |
| 28 | int |
| 29 | main(int argc, char *argv[]) |
| 30 | { |
| 31 | int sk1, sk2; |
| 32 | int pf_class; |
| 33 | int error; |
| 34 | int fd; |
| 35 | int msg_len, bytes_sent; |
| 36 | int frag_interleave, get_result; |
| 37 | sockaddr_storage_t loop1; |
| 38 | sockaddr_storage_t loop2; |
| 39 | struct iovec iov; |
| 40 | struct iovec out_iov; |
| 41 | struct msghdr inmessage; |
| 42 | struct msghdr outmessage; |
| 43 | struct cmsghdr *cmsg; |
| 44 | struct sctp_sndrcvinfo *sinfo; |
| 45 | struct sctp_assoc_value assoc, get_assoc; |
| 46 | uint32_t ppid; |
| 47 | uint32_t stream; |
| 48 | char *buffer; |
| 49 | char setting[4]; |
| 50 | char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))]; |
| 51 | char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; |
| 52 | void *msg_buffer; |
| 53 | socklen_t optlen; |
| 54 | |
| 55 | /* Rather than fflush() throughout the code, set stdout to |
| 56 | * be unbuffered. |
| 57 | */ |
| 58 | setvbuf(stdout, NULL, _IONBF, 0); |
| 59 | |
| 60 | /* Set some basic values which depend on the address family. */ |
| 61 | #if TEST_V6 |
| 62 | pf_class = PF_INET6; |
| 63 | |
| 64 | loop1.v6.sin6_family = AF_INET6; |
| 65 | loop1.v6.sin6_addr = in6addr_loopback; |
| 66 | loop1.v6.sin6_port = htons(SCTP_TESTPORT_1); |
| 67 | |
| 68 | loop2.v6.sin6_family = AF_INET6; |
| 69 | loop2.v6.sin6_addr = in6addr_loopback; |
| 70 | loop2.v6.sin6_port = htons(SCTP_TESTPORT_2); |
| 71 | #else |
| 72 | pf_class = PF_INET; |
| 73 | |
| 74 | loop1.v4.sin_family = AF_INET; |
| 75 | loop1.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 76 | loop1.v4.sin_port = htons(SCTP_TESTPORT_1); |
| 77 | |
| 78 | loop2.v4.sin_family = AF_INET; |
| 79 | loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK; |
| 80 | loop2.v4.sin_port = htons(SCTP_TESTPORT_2); |
| 81 | #endif /* TEST_V6 */ |
| 82 | |
| 83 | /* Create the two endpoints which will talk to each other */ |
| 84 | sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 85 | sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP); |
| 86 | |
| 87 | /* Enable ASSOC_CHANGE and SNDRCVINFO notifications */ |
| 88 | test_enable_assoc_change(sk1); |
| 89 | test_enable_assoc_change(sk2); |
| 90 | |
| 91 | /* Bind these sockets to the test ports */ |
| 92 | test_bind(sk1, &loop1.sa, sizeof(loop1)); |
| 93 | test_bind(sk2, &loop2.sa, sizeof(loop2)); |
| 94 | |
| 95 | /* Let sk2 listen to new associations */ |
| 96 | test_listen(sk2, 1); |
| 97 | |
| 98 | /* TEST #1: verify SCTP_FRAGMENT_INTERLEAVE option |
| 99 | * followed by a getsockopt() |
| 100 | */ |
| 101 | frag_interleave = 1; |
| 102 | test_setsockopt(sk1, SCTP_FRAGMENT_INTERLEAVE, |
| 103 | &frag_interleave, sizeof(frag_interleave)); |
| 104 | test_setsockopt(sk2, SCTP_FRAGMENT_INTERLEAVE, |
| 105 | &frag_interleave, sizeof(frag_interleave)); |
| 106 | |
| 107 | tst_resm(TPASS, "setsockopt(SCTP_FRAGMENT_INTERLEAVE)"); |
| 108 | |
| 109 | get_result = 0; |
| 110 | optlen = sizeof(get_result); |
| 111 | error = test_getsockopt(sk1, SCTP_FRAGMENT_INTERLEAVE, |
| 112 | &get_result, &optlen); |
| 113 | if (get_result != frag_interleave) |
| 114 | tst_brkm(TBROK, tst_exit, "getsockopt(SCTP_FRAGMENT_INTERLEAVE) " |
| 115 | "error: %d errno: %d get_result: %d", |
| 116 | error, errno, get_result); |
| 117 | get_result = 0; |
| 118 | error = test_getsockopt(sk2, SCTP_FRAGMENT_INTERLEAVE, |
| 119 | &get_result, &optlen); |
| 120 | if (get_result != frag_interleave) |
| 121 | tst_brkm(TBROK, tst_exit, "getsockopt(SCTP_FRAGMENT_INTERLEAVE) " |
| 122 | "error: %d errno: %d get_result: %d", |
| 123 | error, errno, get_result); |
| 124 | |
| 125 | tst_resm(TPASS, "getsockopt(SCTP_FRAGMENT_INTERLEAVE)"); |
| 126 | |
| 127 | /* Check if net.sctp.intl_enable=1, exit if not */ |
| 128 | fd = open("/proc/sys/net/sctp/intl_enable", O_RDONLY); |
| 129 | if (fd < 0 || |
| 130 | read(fd, &setting, 4) < 0 || |
| 131 | strncmp("1", setting, 1) != 0) { |
| 132 | tst_resm(TINFO, "intl_enable is not set to 1, skip test\n"); |
| 133 | return 0; |
| 134 | } |
| 135 | close(fd); |
| 136 | |
| 137 | assoc.assoc_id = 0; |
| 138 | assoc.assoc_value = 1; |
| 139 | test_setsockopt(sk1, SCTP_INTERLEAVING_SUPPORTED, |
| 140 | &assoc, sizeof(assoc)); |
| 141 | test_setsockopt(sk2, SCTP_INTERLEAVING_SUPPORTED, |
| 142 | &assoc, sizeof(assoc)); |
| 143 | |
| 144 | tst_resm(TPASS, "setsockopt(SCTP_INTERLEAVING_SUPPORTED)"); |
| 145 | |
| 146 | |
| 147 | memset(&get_assoc, 0x00, sizeof(struct sctp_assoc_value)); |
| 148 | optlen = sizeof(get_assoc); |
| 149 | error = test_getsockopt(sk1, SCTP_INTERLEAVING_SUPPORTED, |
| 150 | &get_assoc, &optlen); |
| 151 | if (get_assoc.assoc_value != 1) |
| 152 | tst_brkm(TBROK, tst_exit, |
| 153 | "getsockopt(SCTP_INTERLEAVING_SUPPORTED) " |
| 154 | "error: %d errno: %d get_result: %d", |
| 155 | error, errno, get_assoc.assoc_value); |
| 156 | error = test_getsockopt(sk2, SCTP_INTERLEAVING_SUPPORTED, |
| 157 | &get_assoc, &optlen); |
| 158 | if (get_assoc.assoc_value != 1) |
| 159 | tst_brkm(TBROK, tst_exit, |
| 160 | "getsockopt(SCTP_INTERLEAVING_SUPPORTED) " |
| 161 | "error: %d errno: %d get_result: %d", |
| 162 | error, errno, get_assoc.assoc_value); |
| 163 | |
| 164 | tst_resm(TPASS, "getsockopt(SCTP_INTERLEAVING_SUPPORTED)"); |
| 165 | /* End of TEST #1 */ |
| 166 | |
| 167 | /* Send the first message to create an association */ |
| 168 | outmessage.msg_name = &loop2; |
| 169 | outmessage.msg_namelen = sizeof(loop2); |
| 170 | outmessage.msg_iov = &out_iov; |
| 171 | outmessage.msg_iovlen = 1; |
| 172 | outmessage.msg_control = outcmsg; |
| 173 | outmessage.msg_controllen = sizeof(outcmsg); |
| 174 | outmessage.msg_flags = 0; |
| 175 | cmsg = CMSG_FIRSTHDR(&outmessage); |
| 176 | cmsg->cmsg_level = IPPROTO_SCTP; |
| 177 | cmsg->cmsg_type = SCTP_SNDRCV; |
| 178 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); |
| 179 | sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); |
| 180 | memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo)); |
| 181 | ppid = rand(); /* Choose an arbitrary value */ |
| 182 | stream = 1; |
| 183 | sinfo->sinfo_ppid = ppid; |
| 184 | sinfo->sinfo_stream = stream; |
| 185 | msg_len = 10; |
| 186 | msg_buffer = test_build_msg(10); |
| 187 | outmessage.msg_iov->iov_base = msg_buffer; |
| 188 | outmessage.msg_iov->iov_len = msg_len; |
| 189 | test_sendmsg(sk1, &outmessage, 0, msg_len); |
| 190 | |
| 191 | /* Initialize inmessage for all receives */ |
| 192 | buffer = test_malloc(REALLY_BIG); |
| 193 | memset(&inmessage, 0x00, sizeof(inmessage)); |
| 194 | iov.iov_base = buffer; |
| 195 | iov.iov_len = REALLY_BIG; |
| 196 | inmessage.msg_iov = &iov; |
| 197 | inmessage.msg_iovlen = 1; |
| 198 | inmessage.msg_control = incmsg; |
| 199 | |
| 200 | /* Get the communication up message on sk2 */ |
| 201 | inmessage.msg_controllen = sizeof(incmsg); |
| 202 | error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); |
| 203 | test_check_msg_notification(&inmessage, error, |
| 204 | sizeof(struct sctp_assoc_change), |
| 205 | SCTP_ASSOC_CHANGE, SCTP_COMM_UP); |
| 206 | |
| 207 | /* Get the communication up message on sk1 */ |
| 208 | inmessage.msg_controllen = sizeof(incmsg); |
| 209 | error = test_recvmsg(sk1, &inmessage, MSG_WAITALL); |
| 210 | test_check_msg_notification(&inmessage, error, |
| 211 | sizeof(struct sctp_assoc_change), |
| 212 | SCTP_ASSOC_CHANGE, SCTP_COMM_UP); |
| 213 | |
| 214 | /* Get the first message which was sent. */ |
| 215 | inmessage.msg_controllen = sizeof(incmsg); |
| 216 | error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); |
| 217 | test_check_msg_data(&inmessage, error, msg_len, MSG_EOR, stream, ppid); |
| 218 | free(msg_buffer); |
| 219 | |
| 220 | /* TEST #2: Verify data integrity */ |
| 221 | msg_len = MSG_SIZE; |
| 222 | msg_buffer = test_build_msg(msg_len); |
| 223 | outmessage.msg_iov->iov_base = msg_buffer; |
| 224 | outmessage.msg_iov->iov_len = msg_len; |
| 225 | bytes_sent = test_sendmsg(sk1, &outmessage, 0, msg_len); |
| 226 | |
| 227 | tst_resm(TINFO, "Sent %d byte message", bytes_sent); |
| 228 | |
| 229 | inmessage.msg_controllen = sizeof(incmsg); |
| 230 | error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); |
| 231 | |
| 232 | tst_resm(TINFO, "Received %d byte message", error); |
| 233 | |
| 234 | test_check_msg_data(&inmessage, error, bytes_sent, |
| 235 | MSG_EOR, stream, ppid); |
| 236 | |
| 237 | tst_resm(TPASS, "Received same byte of message"); |
| 238 | /* End of TEST #2 */ |
| 239 | |
| 240 | /* Shut down the link */ |
| 241 | close(sk1); |
| 242 | |
| 243 | /* Get the shutdown complete notification */ |
| 244 | inmessage.msg_controllen = sizeof(incmsg); |
| 245 | error = test_recvmsg(sk2, &inmessage, MSG_WAITALL); |
| 246 | test_check_msg_notification(&inmessage, error, |
| 247 | sizeof(struct sctp_assoc_change), |
| 248 | SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP); |
| 249 | close(sk2); |
| 250 | |
| 251 | free(buffer); |
| 252 | free(msg_buffer); |
| 253 | /* Indicate successful completion */ |
| 254 | return 0; |
| 255 | } |