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.
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)
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.
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.
25 * Please send any bug reports or fixes you make to the
27 * lksctp developers <lksctp-developers@lists.sourceforge.net>
29 * Or submit a bug report through the following website:
30 * http://www.sf.net/projects/lksctp
32 * Any bugs reported to us we will try to fix... any fixes shared will
33 * be incorporated into the next SCTP release.
35 * Written or modified by:
36 * Ardelle Fan <ardelle.fan@intle.com>
37 * Sridhar Samudrala <sri@us.ibm.com>
40 /* This is a functional test to verify the ungraceful abort of an
48 #include <sys/types.h>
49 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <sys/errno.h>
54 #include <netinet/sctp.h>
57 char *TCID = __FILE__;
61 #define MAX_CLIENTS 10
64 main(int argc, char *argv[])
66 int svr_sk, clt_sk[MAX_CLIENTS];
67 sockaddr_storage_t svr_loop, clt_loop[MAX_CLIENTS];
68 sctp_assoc_t svr_associd[MAX_CLIENTS];
70 struct msghdr inmessage;
71 struct msghdr outmessage;
72 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
73 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
75 struct sctp_sndrcvinfo *sinfo;
80 struct sctp_assoc_change *sac;
83 char *message = "hello, world!\n";
84 struct sctp_status status;
87 /* Rather than fflush() throughout the code, set stdout to
90 setvbuf(stdout, NULL, _IONBF, 0);
92 /* Create and bind the server socket. */
93 svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
94 svr_loop.v4.sin_family = AF_INET;
95 svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
96 svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
97 test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop));
99 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
100 test_enable_assoc_change(svr_sk);
102 /* Mark server socket as being able to accept new associations. */
103 test_listen(svr_sk, 1);
105 /* Create and bind all the client sockets. */
106 for (i = 0; i < MAX_CLIENTS; i++) {
107 clt_sk[i] = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
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 test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i]));
114 test_enable_assoc_change(clt_sk[i]);
117 /* Build up a msghdr structure we can use for all sending. */
118 outmessage.msg_name = &svr_loop;
119 outmessage.msg_namelen = sizeof(svr_loop);
120 outmessage.msg_iov = &out_iov;
121 outmessage.msg_iovlen = 1;
122 outmessage.msg_control = outcmsg;
123 outmessage.msg_controllen = sizeof(outcmsg);
124 outmessage.msg_flags = 0;
125 cmsg = CMSG_FIRSTHDR(&outmessage);
126 cmsg->cmsg_level = IPPROTO_SCTP;
127 cmsg->cmsg_type = SCTP_SNDRCV;
128 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
129 outmessage.msg_controllen = cmsg->cmsg_len;
130 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
131 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
132 ppid = rand(); /* Choose an arbitrary value. */
134 sinfo->sinfo_ppid = ppid;
135 sinfo->sinfo_stream = stream;
136 out_iov.iov_base = message;
137 out_iov.iov_len = strlen(message) + 1;
139 /* Send the first message from all the clients to the server. This
140 * will create the associations.
142 for (i = 0; i < MAX_CLIENTS; i++)
143 test_sendmsg(clt_sk[i], &outmessage, 0, strlen(message) + 1);
145 /* Initialize inmessage for all receives. */
146 big_buffer = test_malloc(REALLY_BIG);
147 memset(&inmessage, 0, sizeof(inmessage));
148 iov.iov_base = big_buffer;
149 iov.iov_len = REALLY_BIG;
150 inmessage.msg_iov = &iov;
151 inmessage.msg_iovlen = 1;
152 inmessage.msg_control = incmsg;
154 /* Get the communication up message on all client sockets. */
155 for (i = 0; i < MAX_CLIENTS; i++) {
156 inmessage.msg_controllen = sizeof(incmsg);
157 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
158 test_check_msg_notification(&inmessage, error,
159 sizeof(struct sctp_assoc_change),
160 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
163 /* Get the communication up message and the data message on the
164 * server sockets for all the clients.
166 for (i = 0; i < MAX_CLIENTS; i++) {
167 inmessage.msg_controllen = sizeof(incmsg);
168 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
169 test_check_msg_notification(&inmessage, error,
170 sizeof(struct sctp_assoc_change),
171 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
173 inmessage.msg_controllen = sizeof(incmsg);
174 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
175 test_check_msg_data(&inmessage, error, strlen(message) + 1,
176 MSG_EOR, stream, ppid);
177 sac = (struct sctp_assoc_change *)iov.iov_base;
178 svr_associd[i] = sac->sac_assoc_id;
181 outmessage.msg_name = NULL;
182 outmessage.msg_namelen = 0;
183 outmessage.msg_iov = NULL;
184 outmessage.msg_iovlen = 0;
185 outmessage.msg_control = outcmsg;
186 outmessage.msg_controllen = sizeof(outcmsg);
187 outmessage.msg_flags = 0;
188 cmsg = CMSG_FIRSTHDR(&outmessage);
189 cmsg->cmsg_level = IPPROTO_SCTP;
190 cmsg->cmsg_type = SCTP_SNDRCV;
191 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
192 outmessage.msg_controllen = cmsg->cmsg_len;
193 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
194 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
195 sinfo->sinfo_flags |= SCTP_ABORT;
197 /* Shutdown all the associations of the server socket in a loop. */
198 for (i = 0; i < MAX_CLIENTS; i++) {
199 sinfo->sinfo_assoc_id = svr_associd[i];
201 /* Verify that the association is present. */
202 memset(&status, 0, sizeof(struct sctp_status));
203 status.sstat_assoc_id = sinfo->sinfo_assoc_id;
204 status_len = sizeof(struct sctp_status);
205 error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS,
206 &status, &status_len);
208 tst_brkm(TBROK, tst_exit,
209 "getsockopt(SCTP_STATUS): %s",
212 /* Call sendmsg() to abort the association. */
213 test_sendmsg(svr_sk, &outmessage, 0, 0);
215 /* Verify that the association is no longer present. */
216 memset(&status, 0, sizeof(struct sctp_status));
217 status.sstat_assoc_id = sinfo->sinfo_assoc_id;
218 status_len = sizeof(struct sctp_status);
219 error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS,
220 &status, &status_len);
221 if ((error != -1) && (errno != EINVAL))
222 tst_brkm(TBROK, tst_exit,
223 "getsockopt(SCTP_STATUS) "
224 "error:%d errno:%d", error, errno);
229 /* Get the COMM_LOST notification. */
230 for (i = 0; i < MAX_CLIENTS; i++) {
231 inmessage.msg_controllen = sizeof(incmsg);
232 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
233 test_check_msg_notification(&inmessage, error,
234 sizeof(struct sctp_assoc_change)+4,
235 SCTP_ASSOC_CHANGE, SCTP_COMM_LOST);
240 tst_resm(TPASS, "ABORT an association using SCTP_ABORT");
242 /* Indicate successful completion. */