blob: 8fb7a00eae7a31c508f9ff7bade8c3c89ca6895d [file] [log] [blame]
Simon Xu6f224942013-12-06 11:51:16 +08001/* SCTP kernel Implementation
uid5281789de9aa2003-10-23 18:42:24 +00002 * (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 *
Simon Xu6f224942013-12-06 11:51:16 +08008 * The SCTP implementation is free software;
uid5281789de9aa2003-10-23 18:42:24 +00009 * 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 *
Simon Xu6f224942013-12-06 11:51:16 +080014 * The SCTP implementation is distributed in the hope that it
uid5281789de9aa2003-10-23 18:42:24 +000015 * 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
Simon Xu6f224942013-12-06 11:51:16 +080039/* This is a Functional test to verify the new SCTP interface sctp_peeloff()
40 * that can be used to branch off an association into a separate socket.
uid5281789de9aa2003-10-23 18:42:24 +000041 */
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 <errno.h>
52#include <netinet/sctp.h>
53#include <sctputil.h>
54
55char *TCID = __FILE__;
56int TST_TOTAL = 6;
57int TST_CNT = 0;
58
59#define MAX_CLIENTS 10
60
Simon Xu6f224942013-12-06 11:51:16 +080061int
62main(int argc, char *argv[])
uid5281789de9aa2003-10-23 18:42:24 +000063{
64 int svr_sk, clt_sk[MAX_CLIENTS], peeloff_sk[MAX_CLIENTS];
Simon Xu6f224942013-12-06 11:51:16 +080065 sctp_assoc_t svr_associd[MAX_CLIENTS];
uid5281789de9aa2003-10-23 18:42:24 +000066 sockaddr_storage_t svr_loop, clt_loop[MAX_CLIENTS];
67 struct iovec iov;
68 struct msghdr inmessage;
69 struct msghdr outmessage;
70 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
71 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
72 struct cmsghdr *cmsg;
73 struct sctp_sndrcvinfo *sinfo;
74 struct iovec out_iov;
75 int error;
76 uint32_t ppid;
77 uint32_t stream;
78 struct sctp_assoc_change *sac;
79 char *big_buffer;
80 int i;
Simon Xu6f224942013-12-06 11:51:16 +080081 char *message = "hello, world!\n";
uid5281789de9aa2003-10-23 18:42:24 +000082 int pf_class;
83
Simon Xu6f224942013-12-06 11:51:16 +080084 /* Rather than fflush() throughout the code, set stdout to
85 * be unbuffered.
86 */
87 setvbuf(stdout, NULL, _IONBF, 0);
uid5281789de9aa2003-10-23 18:42:24 +000088
89#if TEST_V6
90 pf_class = PF_INET6;
Simon Xu6f224942013-12-06 11:51:16 +080091 svr_loop.v6.sin6_family = AF_INET6;
92 svr_loop.v6.sin6_addr = in6addr_loopback;
93 svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1);
uid5281789de9aa2003-10-23 18:42:24 +000094#else
95 pf_class = PF_INET;
96 svr_loop.v4.sin_family = AF_INET;
97 svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
98 svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
99#endif
100
101 /* Create and bind the server socket. */
Simon Xu6f224942013-12-06 11:51:16 +0800102 svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
uid5281789de9aa2003-10-23 18:42:24 +0000103 test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop));
104
105 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
106 test_enable_assoc_change(svr_sk);
107
108 /* Mark server socket as being able to accept new associations. */
109 test_listen(svr_sk, 1);
110
111 /* Create and bind all the client sockets. */
112 for (i = 0; i < MAX_CLIENTS; i++) {
113 clt_sk[i] = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
114#if TEST_V6
Simon Xu6f224942013-12-06 11:51:16 +0800115 clt_loop[i].v6.sin6_family = AF_INET6;
116 clt_loop[i].v6.sin6_addr = in6addr_loopback;
117 clt_loop[i].v6.sin6_port = htons(SCTP_TESTPORT_2 + i);
uid5281789de9aa2003-10-23 18:42:24 +0000118#else
119 clt_loop[i].v4.sin_family = AF_INET;
120 clt_loop[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
121 clt_loop[i].v4.sin_port = htons(SCTP_TESTPORT_2 + i);
122#endif
123 test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i]));
124
125 test_enable_assoc_change(clt_sk[i]);
126 }
127
Simon Xu6f224942013-12-06 11:51:16 +0800128 /* Send the first message from all the clients to the server. This
129 * will create the associations.
uid5281789de9aa2003-10-23 18:42:24 +0000130 */
131 outmessage.msg_name = &svr_loop;
132 outmessage.msg_namelen = sizeof(svr_loop);
133 outmessage.msg_iov = &out_iov;
134 outmessage.msg_iovlen = 1;
135 outmessage.msg_control = outcmsg;
136 outmessage.msg_controllen = sizeof(outcmsg);
137 outmessage.msg_flags = 0;
138 cmsg = CMSG_FIRSTHDR(&outmessage);
139 cmsg->cmsg_level = IPPROTO_SCTP;
140 cmsg->cmsg_type = SCTP_SNDRCV;
141 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
142 outmessage.msg_controllen = cmsg->cmsg_len;
143 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
144 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
Simon Xu6f224942013-12-06 11:51:16 +0800145 ppid = rand(); /* Choose an arbitrary value. */
146 stream = 1;
uid5281789de9aa2003-10-23 18:42:24 +0000147 sinfo->sinfo_ppid = ppid;
148 sinfo->sinfo_stream = stream;
149 outmessage.msg_iov->iov_base = message;
150 outmessage.msg_iov->iov_len = strlen(message) + 1;
151 for (i = 0; i < MAX_CLIENTS; i++)
Simon Xu6f224942013-12-06 11:51:16 +0800152 test_sendmsg(clt_sk[i], &outmessage, 0,
153 strlen(message)+1);
154
155 /* Initialize inmessage for all receives. */
uid5281789de9aa2003-10-23 18:42:24 +0000156 big_buffer = test_malloc(REALLY_BIG);
Simon Xu6f224942013-12-06 11:51:16 +0800157 memset(&inmessage, 0, sizeof(inmessage));
uid5281789de9aa2003-10-23 18:42:24 +0000158 iov.iov_base = big_buffer;
159 iov.iov_len = REALLY_BIG;
160 inmessage.msg_iov = &iov;
161 inmessage.msg_iovlen = 1;
162 inmessage.msg_control = incmsg;
163
164 /* Get the communication up message on all client sockets. */
165 for (i = 0; i < MAX_CLIENTS; i++) {
166 inmessage.msg_controllen = sizeof(incmsg);
167 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
168 test_check_msg_notification(&inmessage, error,
169 sizeof(struct sctp_assoc_change),
Garrett Cooper2c282152010-12-16 00:55:50 -0800170 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
Simon Xu6f224942013-12-06 11:51:16 +0800171#if 0
uid5281789de9aa2003-10-23 18:42:24 +0000172 sac = (struct sctp_assoc_change *)iov.iov_base;
173 clt_associd[i] = sac->sac_assoc_id;
Simon Xu6f224942013-12-06 11:51:16 +0800174#endif
uid5281789de9aa2003-10-23 18:42:24 +0000175 }
176
177 /* Get the communication up message and the data message on the
Simon Xu6f224942013-12-06 11:51:16 +0800178 * server sockets for all the clients.
uid5281789de9aa2003-10-23 18:42:24 +0000179 */
180 for (i = 0; i < MAX_CLIENTS; i++) {
181 inmessage.msg_controllen = sizeof(incmsg);
182 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
183 test_check_msg_notification(&inmessage, error,
184 sizeof(struct sctp_assoc_change),
Simon Xu6f224942013-12-06 11:51:16 +0800185 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
uid5281789de9aa2003-10-23 18:42:24 +0000186 sac = (struct sctp_assoc_change *)iov.iov_base;
187 svr_associd[i] = sac->sac_assoc_id;
188
189 inmessage.msg_controllen = sizeof(incmsg);
190 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
191 test_check_msg_data(&inmessage, error, strlen(message) + 1,
192 MSG_EOR, stream, ppid);
193 }
194
195 /* Branch off all the associations on the server socket to separate
196 * individual sockets.
Simon Xu6f224942013-12-06 11:51:16 +0800197 */
uid5281789de9aa2003-10-23 18:42:24 +0000198 for (i = 0; i < MAX_CLIENTS; i++)
Simon Xu6f224942013-12-06 11:51:16 +0800199 peeloff_sk[i] = test_sctp_peeloff(svr_sk, svr_associd[i]);
uid5281789de9aa2003-10-23 18:42:24 +0000200
201 tst_resm(TPASS, "sctp_peeloff");
202
203 errno = 0;
204 /* Verify that a peeled off socket is not allowed to do a listen(). */
205 error = listen(peeloff_sk[0], 1);
206 if (error != -1)
Simon Xu6f224942013-12-06 11:51:16 +0800207 tst_brkm(TBROK, tst_exit, "listen on a peeled off socket "
208 "error: %d, errno: %d", error, errno);
uid5281789de9aa2003-10-23 18:42:24 +0000209
210 tst_resm(TPASS, "listen on a peeled off socket");
211
212 errno = 0;
213 /* Verify that an association cannot be branched off an already
214 * peeled-off socket.
215 */
216 if ((-1 != sctp_peeloff(peeloff_sk[0], svr_associd[0])) ||
217 (EINVAL != errno))
Simon Xu6f224942013-12-06 11:51:16 +0800218 tst_brkm(TBROK, tst_exit, "sctp_peeloff on a peeled off "
219 "socket error:%d, errno:%d",
220 error, errno);
uid5281789de9aa2003-10-23 18:42:24 +0000221
222 tst_resm(TPASS, "sctp_peeloff on a peeled off socket");
223
224 /* Send a message from all the client sockets to the server socket. */
225 for (i = 0; i < MAX_CLIENTS; i++)
Simon Xu6f224942013-12-06 11:51:16 +0800226 test_sendmsg(clt_sk[i], &outmessage, 0, strlen(message)+1);
uid5281789de9aa2003-10-23 18:42:24 +0000227
Simon Xu6f224942013-12-06 11:51:16 +0800228 /* Receive the sent messages on the peeled off server sockets. */
uid5281789de9aa2003-10-23 18:42:24 +0000229 for (i = 0; i < MAX_CLIENTS; i++) {
230 inmessage.msg_controllen = sizeof(incmsg);
231 error = test_recvmsg(peeloff_sk[i], &inmessage, MSG_WAITALL);
232 test_check_msg_data(&inmessage, error, strlen(message) + 1,
233 MSG_EOR, stream, ppid);
234 }
235
236 tst_resm(TPASS, "Receive msgs on peeled off sockets");
237
Simon Xu6f224942013-12-06 11:51:16 +0800238 /* Send a message from all the peeled off server sockets to the client
239 * sockets.
uid5281789de9aa2003-10-23 18:42:24 +0000240 */
241 for (i = 0; i < MAX_CLIENTS; i++) {
242 outmessage.msg_name = &clt_loop[i];
243 outmessage.msg_namelen = sizeof(clt_loop[i]);
Simon Xu6f224942013-12-06 11:51:16 +0800244 test_sendmsg(peeloff_sk[i], &outmessage, 0, strlen(message)+1);
uid5281789de9aa2003-10-23 18:42:24 +0000245 }
246
Simon Xu6f224942013-12-06 11:51:16 +0800247 /* Receive the messages sent from the peeled of server sockets on
uid5281789de9aa2003-10-23 18:42:24 +0000248 * the client sockets.
249 */
250 for (i = 0; i < MAX_CLIENTS; i++) {
251 inmessage.msg_controllen = sizeof(incmsg);
252 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
253 test_check_msg_data(&inmessage, error, strlen(message) + 1,
254 MSG_EOR, stream, ppid);
255 }
256
257 tst_resm(TPASS, "Send msgs on peeled off sockets");
258
259 errno = 0;
Simon Xu6f224942013-12-06 11:51:16 +0800260 /* Verify that a peeled-off socket cannot initialize a new
uid5281789de9aa2003-10-23 18:42:24 +0000261 * association by trying to send a message to a client that is not
262 * associated with the peeled-off socket.
263 * The message is sent to the client that is associated with the
264 * socket.
Simon Xu6f224942013-12-06 11:51:16 +0800265 */
uid5281789de9aa2003-10-23 18:42:24 +0000266 outmessage.msg_name = &clt_loop[1];
267 outmessage.msg_namelen = sizeof(clt_loop[1]);
Simon Xu6f224942013-12-06 11:51:16 +0800268 test_sendmsg(peeloff_sk[0], &outmessage, 0, strlen(message)+1);
uid5281789de9aa2003-10-23 18:42:24 +0000269
270 inmessage.msg_controllen = sizeof(incmsg);
271 error = test_recvmsg(clt_sk[0], &inmessage, MSG_WAITALL);
272 test_check_msg_data(&inmessage, error, strlen(message) + 1,
273 MSG_EOR, stream, ppid);
274
275 tst_resm(TPASS, "peeled off socket cannot initialize a new assoc");
276
277 close(svr_sk);
278
279 /* Close all the peeled off server sockets. */
280 for (i = 0; i < MAX_CLIENTS; i++)
281 close(peeloff_sk[i]);
282
Simon Xu6f224942013-12-06 11:51:16 +0800283 /* Get the shutdown complete notification from all the client
284 * sockets.
uid5281789de9aa2003-10-23 18:42:24 +0000285 */
286 for (i = 0; i < MAX_CLIENTS; i++) {
287 inmessage.msg_controllen = sizeof(incmsg);
288 error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
289 test_check_msg_notification(&inmessage, error,
290 sizeof(struct sctp_assoc_change),
291 SCTP_ASSOC_CHANGE,
Simon Xu6f224942013-12-06 11:51:16 +0800292 SCTP_SHUTDOWN_COMP);
uid5281789de9aa2003-10-23 18:42:24 +0000293
294 close(clt_sk[i]);
295 }
296
Simon Xu6f224942013-12-06 11:51:16 +0800297 /* Indicate successful completion. */
298 return 0;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700299}