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 * Sridhar Samudrala <sri@us.ibm.com>
37 */
38
39 /* This is a functional test to verify the graceful shutdown of an
40 * association.
41 */
42
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <sys/uio.h>
50 #include <netinet/in.h>
51 #include <sys/errno.h>
52 #include <errno.h>
53 #include <netinet/sctp.h>
54 #include <sctputil.h>
55
56 char *TCID = __FILE__;
57 int TST_TOTAL = 1;
58 int TST_CNT = 0;
59
60 #define MAX_CLIENTS 10
61 #include "tst_kernel.h"
62
63 int
main(void)64 main(void)
65 {
66 int svr_sk, clt_sk[MAX_CLIENTS];
67 sctp_assoc_t svr_associd[MAX_CLIENTS];
68 sockaddr_storage_t svr_loop, clt_loop[MAX_CLIENTS];
69 struct iovec iov;
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))];
74 struct cmsghdr *cmsg;
75 struct sctp_sndrcvinfo *sinfo;
76 struct iovec out_iov;
77 int error;
78 uint32_t ppid;
79 uint32_t stream;
80 struct sctp_assoc_change *sac;
81 char *big_buffer;
82 int i;
83 char *message = "hello, world!\n";
84 struct sctp_status status;
85 socklen_t status_len;
86
87 if (tst_check_driver("sctp"))
88 tst_brkm(TCONF, tst_exit, "sctp driver not available");
89
90 /* Rather than fflush() throughout the code, set stdout to
91 * be unbuffered.
92 */
93 setvbuf(stdout, NULL, _IONBF, 0);
94
95 /* Create and bind the server socket. */
96 svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
97
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 /* Build up a msghdr structure we can use for all sending. */
186 outmessage.msg_name = NULL;
187 outmessage.msg_namelen = 0;
188 outmessage.msg_iov = NULL;
189 outmessage.msg_iovlen = 0;
190 outmessage.msg_control = outcmsg;
191 outmessage.msg_controllen = sizeof(outcmsg);
192 outmessage.msg_flags = 0;
193 cmsg = CMSG_FIRSTHDR(&outmessage);
194 cmsg->cmsg_level = IPPROTO_SCTP;
195 cmsg->cmsg_type = SCTP_SNDRCV;
196 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
197 outmessage.msg_controllen = cmsg->cmsg_len;
198 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
199 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
200 sinfo->sinfo_flags |= SCTP_EOF;
201
202 /* Shutdown all the associations of the server socket in a loop. */
203 for (i = 0; i < MAX_CLIENTS; i++) {
204 sinfo->sinfo_assoc_id = svr_associd[i];
205
206 /* Verify that the association is present. */
207 memset(&status, 0, sizeof(struct sctp_status));
208 status.sstat_assoc_id = sinfo->sinfo_assoc_id;
209 status_len = sizeof(struct sctp_status);
210 error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS,
211 &status, &status_len);
212 if (error)
213 tst_brkm(TBROK, tst_exit,
214 "getsockopt(SCTP_STATUS): %s",
215 strerror(errno));
216
217 /* Call sendmsg() to shutdown the association. */
218 test_sendmsg(svr_sk, &outmessage, 0, 0);
219
220 /* Verify that the association is no longer present. */
221 memset(&status, 0, sizeof(struct sctp_status));
222 status.sstat_assoc_id = sinfo->sinfo_assoc_id;
223 status_len = sizeof(struct sctp_status);
224 error = getsockopt(svr_sk, SOL_SCTP, SCTP_STATUS,
225 &status, &status_len);
226 if ((error != -1) && (errno != EINVAL))
227 tst_brkm(TBROK, tst_exit,
228 "getsockopt(SCTP_STATUS) "
229 "error:%d errno:%d", error, errno);
230 }
231
232 close(svr_sk);
233
234 /* Get the shutdown complete notification. */
235 for (i = 0; i < MAX_CLIENTS; i++) {
236 inmessage.msg_controllen = sizeof(incmsg);
237 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
238 test_check_msg_notification(&inmessage, error,
239 sizeof(struct sctp_assoc_change),
240 SCTP_ASSOC_CHANGE,
241 SCTP_SHUTDOWN_COMP);
242
243 close(clt_sk[i]);
244 }
245
246 tst_resm(TPASS, "Graceful shutdown of associations using SCTP_EOF");
247
248 /* Indicate successful completion. */
249 return 0;
250 }
251