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 * Ardelle Fan <ardelle.fan@intle.com>
37 * Sridhar Samudrala <sri@us.ibm.com>
38 */
39
40 /* This is a functional test to verify the ungraceful abort of an
41 * association.
42 */
43
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <sys/uio.h>
51 #include <netinet/in.h>
52 #include <sys/errno.h>
53 #include <errno.h>
54 #include <netinet/sctp.h>
55 #include <sctputil.h>
56 #include "tst_kernel.h"
57
58 char *TCID = __FILE__;
59 int TST_TOTAL = 1;
60 int TST_CNT = 0;
61
62 #define MAX_CLIENTS 10
63
64 int
main(void)65 main(void)
66 {
67 int svr_sk, clt_sk[MAX_CLIENTS];
68 sockaddr_storage_t svr_loop, clt_loop[MAX_CLIENTS];
69 sctp_assoc_t svr_associd[MAX_CLIENTS];
70 struct iovec iov;
71 struct msghdr inmessage;
72 struct msghdr outmessage;
73 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
74 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
75 struct cmsghdr *cmsg;
76 struct sctp_sndrcvinfo *sinfo;
77 struct iovec out_iov;
78 int error;
79 uint32_t ppid;
80 uint32_t stream;
81 struct sctp_assoc_change *sac;
82 char *big_buffer;
83 int i;
84 char *message = "hello, world!\n";
85 struct sctp_status status;
86 socklen_t status_len;
87
88 if (tst_check_driver("sctp"))
89 tst_brkm(TCONF, tst_exit, "sctp driver not available");
90
91 /* Rather than fflush() throughout the code, set stdout to
92 * be unbuffered.
93 */
94 setvbuf(stdout, NULL, _IONBF, 0);
95
96 /* Create and bind the server socket. */
97 svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
98 svr_loop.v4.sin_family = AF_INET;
99 svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
100 svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
101 test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop));
102
103 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
104 test_enable_assoc_change(svr_sk);
105
106 /* Mark server socket as being able to accept new associations. */
107 test_listen(svr_sk, 1);
108
109 /* Create and bind all the client sockets. */
110 for (i = 0; i < MAX_CLIENTS; i++) {
111 clt_sk[i] = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
112
113 clt_loop[i].v4.sin_family = AF_INET;
114 clt_loop[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
115 clt_loop[i].v4.sin_port = htons(SCTP_TESTPORT_2 + i);
116 test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i]));
117
118 test_enable_assoc_change(clt_sk[i]);
119 }
120
121 /* Build up a msghdr structure we can use for all sending. */
122 outmessage.msg_name = &svr_loop;
123 outmessage.msg_namelen = sizeof(svr_loop);
124 outmessage.msg_iov = &out_iov;
125 outmessage.msg_iovlen = 1;
126 outmessage.msg_control = outcmsg;
127 outmessage.msg_controllen = sizeof(outcmsg);
128 outmessage.msg_flags = 0;
129 cmsg = CMSG_FIRSTHDR(&outmessage);
130 cmsg->cmsg_level = IPPROTO_SCTP;
131 cmsg->cmsg_type = SCTP_SNDRCV;
132 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
133 outmessage.msg_controllen = cmsg->cmsg_len;
134 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
135 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
136 ppid = rand(); /* Choose an arbitrary value. */
137 stream = 1;
138 sinfo->sinfo_ppid = ppid;
139 sinfo->sinfo_stream = stream;
140 out_iov.iov_base = message;
141 out_iov.iov_len = strlen(message) + 1;
142
143 /* Send the first message from all the clients to the server. This
144 * will create the associations.
145 */
146 for (i = 0; i < MAX_CLIENTS; i++)
147 test_sendmsg(clt_sk[i], &outmessage, 0, strlen(message) + 1);
148
149 /* Initialize inmessage for all receives. */
150 big_buffer = test_malloc(REALLY_BIG);
151 memset(&inmessage, 0, sizeof(inmessage));
152 iov.iov_base = big_buffer;
153 iov.iov_len = REALLY_BIG;
154 inmessage.msg_iov = &iov;
155 inmessage.msg_iovlen = 1;
156 inmessage.msg_control = incmsg;
157
158 /* Get the communication up message on all client sockets. */
159 for (i = 0; i < MAX_CLIENTS; i++) {
160 inmessage.msg_controllen = sizeof(incmsg);
161 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
162 test_check_msg_notification(&inmessage, error,
163 sizeof(struct sctp_assoc_change),
164 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
165 }
166
167 /* Get the communication up message and the data message on the
168 * server sockets for all the clients.
169 */
170 for (i = 0; i < MAX_CLIENTS; i++) {
171 inmessage.msg_controllen = sizeof(incmsg);
172 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
173 test_check_msg_notification(&inmessage, error,
174 sizeof(struct sctp_assoc_change),
175 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
176
177 inmessage.msg_controllen = sizeof(incmsg);
178 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
179 test_check_msg_data(&inmessage, error, strlen(message) + 1,
180 MSG_EOR, stream, ppid);
181 sac = (struct sctp_assoc_change *)iov.iov_base;
182 svr_associd[i] = sac->sac_assoc_id;
183 }
184
185 outmessage.msg_name = NULL;
186 outmessage.msg_namelen = 0;
187 outmessage.msg_iov = NULL;
188 outmessage.msg_iovlen = 0;
189 outmessage.msg_control = outcmsg;
190 outmessage.msg_controllen = sizeof(outcmsg);
191 outmessage.msg_flags = 0;
192 cmsg = CMSG_FIRSTHDR(&outmessage);
193 cmsg->cmsg_level = IPPROTO_SCTP;
194 cmsg->cmsg_type = SCTP_SNDRCV;
195 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
196 outmessage.msg_controllen = cmsg->cmsg_len;
197 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
198 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
199 sinfo->sinfo_flags |= SCTP_ABORT;
200
201 /* Shutdown all the associations of the server socket in a loop. */
202 for (i = 0; i < MAX_CLIENTS; i++) {
203 sinfo->sinfo_assoc_id = svr_associd[i];
204
205 /* Verify that the association is present. */
206 memset(&status, 0, sizeof(struct sctp_status));
207 status.sstat_assoc_id = sinfo->sinfo_assoc_id;
208 status_len = sizeof(struct sctp_status);
209 error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS,
210 &status, &status_len);
211 if (error)
212 tst_brkm(TBROK, tst_exit,
213 "getsockopt(SCTP_STATUS): %s",
214 strerror(errno));
215
216 /* Call sendmsg() to abort the association. */
217 test_sendmsg(svr_sk, &outmessage, 0, 0);
218
219 /* Verify that the association is no longer present. */
220 memset(&status, 0, sizeof(struct sctp_status));
221 status.sstat_assoc_id = sinfo->sinfo_assoc_id;
222 status_len = sizeof(struct sctp_status);
223 error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS,
224 &status, &status_len);
225 if ((error != -1) && (errno != EINVAL))
226 tst_brkm(TBROK, tst_exit,
227 "getsockopt(SCTP_STATUS) "
228 "error:%d errno:%d", error, errno);
229 }
230
231 close(svr_sk);
232
233 /* Get the COMM_LOST notification. */
234 for (i = 0; i < MAX_CLIENTS; i++) {
235 inmessage.msg_controllen = sizeof(incmsg);
236 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
237 test_check_msg_notification(&inmessage, error,
238 sizeof(struct sctp_assoc_change)+4,
239 SCTP_ASSOC_CHANGE, SCTP_COMM_LOST);
240
241 close(clt_sk[i]);
242 }
243
244 tst_resm(TPASS, "ABORT an association using SCTP_ABORT");
245
246 /* Indicate successful completion. */
247 return 0;
248 }
249