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