• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 new SCTP interface sctp_peeloff()
40  * that can be used to branch off an association into a separate socket.
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 <errno.h>
52 #include <netinet/sctp.h>
53 #include <sctputil.h>
54 #include "tst_kernel.h"
55 
56 char *TCID = __FILE__;
57 int TST_TOTAL = 6;
58 int TST_CNT = 0;
59 
60 #define MAX_CLIENTS 10
61 
62 int
main(void)63 main(void)
64 {
65 	int svr_sk, clt_sk[MAX_CLIENTS], peeloff_sk[MAX_CLIENTS];
66 	sctp_assoc_t svr_associd[MAX_CLIENTS];
67 	sockaddr_storage_t svr_loop, clt_loop[MAX_CLIENTS];
68 	struct iovec iov;
69 	struct msghdr inmessage;
70 	struct msghdr outmessage;
71 	char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
72 	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
73 	struct cmsghdr *cmsg;
74 	struct sctp_sndrcvinfo *sinfo;
75 	struct iovec out_iov;
76 	int error;
77 	uint32_t ppid;
78 	uint32_t stream;
79 	struct sctp_assoc_change *sac;
80 	char *big_buffer;
81 	int i;
82         char *message = "hello, world!\n";
83 	int pf_class;
84 
85 	if (tst_check_driver("sctp"))
86 		tst_brkm(TCONF, tst_exit, "sctp driver not available");
87 
88         /* Rather than fflush() throughout the code, set stdout to
89 	 * be unbuffered.
90 	 */
91 	setvbuf(stdout, NULL, _IONBF, 0);
92 
93 #if TEST_V6
94 	pf_class = PF_INET6;
95         svr_loop.v6.sin6_family = AF_INET6;
96         svr_loop.v6.sin6_addr = in6addr_loopback;
97         svr_loop.v6.sin6_port = htons(SCTP_TESTPORT_1);
98 #else
99 	pf_class = PF_INET;
100 	svr_loop.v4.sin_family = AF_INET;
101 	svr_loop.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
102 	svr_loop.v4.sin_port = htons(SCTP_TESTPORT_1);
103 #endif
104 
105 	/* Create and bind the server socket.  */
106         svr_sk = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
107 	test_bind(svr_sk, &svr_loop.sa, sizeof(svr_loop));
108 
109 	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
110 	test_enable_assoc_change(svr_sk);
111 
112 	/* Mark server socket as being able to accept new associations.  */
113 	test_listen(svr_sk, 1);
114 
115 	/* Create and bind all the client sockets.  */
116 	for (i = 0; i < MAX_CLIENTS; i++) {
117 		clt_sk[i] = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
118 #if TEST_V6
119         	clt_loop[i].v6.sin6_family = AF_INET6;
120         	clt_loop[i].v6.sin6_addr = in6addr_loopback;
121         	clt_loop[i].v6.sin6_port = htons(SCTP_TESTPORT_2 + i);
122 #else
123 		clt_loop[i].v4.sin_family = AF_INET;
124 		clt_loop[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
125 		clt_loop[i].v4.sin_port = htons(SCTP_TESTPORT_2 + i);
126 #endif
127 		test_bind(clt_sk[i], &clt_loop[i].sa, sizeof(clt_loop[i]));
128 
129 		test_enable_assoc_change(clt_sk[i]);
130 	}
131 
132         /* Send the first message from all the clients to the server.  This
133 	 * will create the associations.
134 	 */
135 	outmessage.msg_name = &svr_loop;
136 	outmessage.msg_namelen = sizeof(svr_loop);
137 	outmessage.msg_iov = &out_iov;
138 	outmessage.msg_iovlen = 1;
139 	outmessage.msg_control = outcmsg;
140 	outmessage.msg_controllen = sizeof(outcmsg);
141 	outmessage.msg_flags = 0;
142 	cmsg = CMSG_FIRSTHDR(&outmessage);
143 	cmsg->cmsg_level = IPPROTO_SCTP;
144 	cmsg->cmsg_type = SCTP_SNDRCV;
145 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
146 	outmessage.msg_controllen = cmsg->cmsg_len;
147 	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
148 	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
149 	ppid = rand(); /* Choose an arbitrary value. */
150 	stream = 1;
151 	sinfo->sinfo_ppid = ppid;
152 	sinfo->sinfo_stream = stream;
153 	outmessage.msg_iov->iov_base = message;
154 	outmessage.msg_iov->iov_len = strlen(message) + 1;
155 	for (i = 0; i < MAX_CLIENTS; i++)
156 		test_sendmsg(clt_sk[i], &outmessage, 0,
157 					  strlen(message)+1);
158 
159 	/* Initialize inmessage for all receives. */
160 	big_buffer = test_malloc(REALLY_BIG);
161 	memset(&inmessage, 0, sizeof(inmessage));
162 	iov.iov_base = big_buffer;
163 	iov.iov_len = REALLY_BIG;
164 	inmessage.msg_iov = &iov;
165 	inmessage.msg_iovlen = 1;
166 	inmessage.msg_control = incmsg;
167 
168 	/* Get the communication up message on all client sockets.  */
169 	for (i = 0; i < MAX_CLIENTS; i++) {
170 		inmessage.msg_controllen = sizeof(incmsg);
171 		error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
172 		test_check_msg_notification(&inmessage, error,
173 					    sizeof(struct sctp_assoc_change),
174 					    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
175 #if 0
176 		sac = (struct sctp_assoc_change *)iov.iov_base;
177 		clt_associd[i] = sac->sac_assoc_id;
178 #endif
179 	}
180 
181 	/* Get the communication up message and the data message on the
182 	 * server sockets for all the clients.
183 	 */
184 	for (i = 0; i < MAX_CLIENTS; i++) {
185 		inmessage.msg_controllen = sizeof(incmsg);
186 		error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
187 		test_check_msg_notification(&inmessage, error,
188 					    sizeof(struct sctp_assoc_change),
189 					    SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
190 		sac = (struct sctp_assoc_change *)iov.iov_base;
191 		svr_associd[i] = sac->sac_assoc_id;
192 
193 		inmessage.msg_controllen = sizeof(incmsg);
194 		error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
195 		test_check_msg_data(&inmessage, error, strlen(message) + 1,
196 				    MSG_EOR, stream, ppid);
197 	}
198 
199 	/* Branch off all the associations on the server socket to separate
200 	 * individual sockets.
201 	 */
202 	for (i = 0; i < MAX_CLIENTS; i++)
203 		peeloff_sk[i] = test_sctp_peeloff(svr_sk, svr_associd[i]);
204 
205 	tst_resm(TPASS, "sctp_peeloff");
206 
207 	errno = 0;
208 	/* Verify that a peeled off socket is not allowed to do a listen().  */
209 	error = listen(peeloff_sk[0], 1);
210 	if (error != -1)
211 		tst_brkm(TBROK, tst_exit, "listen on a peeled off socket "
212 			 "error: %d, errno: %d", error, errno);
213 
214 	tst_resm(TPASS, "listen on a peeled off socket");
215 
216 	errno = 0;
217 	/* Verify that an association cannot be branched off an already
218 	 * peeled-off socket.
219 	 */
220 	if ((-1 != sctp_peeloff(peeloff_sk[0], svr_associd[0])) ||
221 	    (EINVAL != errno))
222 		tst_brkm(TBROK, tst_exit, "sctp_peeloff on a peeled off "
223 			 "socket error:%d, errno:%d",
224 			 error, errno);
225 
226 	tst_resm(TPASS, "sctp_peeloff on a peeled off socket");
227 
228 	/* Send a message from all the client sockets to the server socket. */
229 	for (i = 0; i < MAX_CLIENTS; i++)
230 		test_sendmsg(clt_sk[i], &outmessage, 0, strlen(message)+1);
231 
232 	/* Receive the sent messages on the peeled off server sockets.  */
233 	for (i = 0; i < MAX_CLIENTS; i++) {
234 		inmessage.msg_controllen = sizeof(incmsg);
235 		error = test_recvmsg(peeloff_sk[i], &inmessage, MSG_WAITALL);
236 		test_check_msg_data(&inmessage, error, strlen(message) + 1,
237 				    MSG_EOR, stream, ppid);
238 	}
239 
240 	tst_resm(TPASS, "Receive msgs on peeled off sockets");
241 
242 	/* Send a message from all the peeled off server sockets to the client
243 	 * sockets.
244 	 */
245 	for (i = 0; i < MAX_CLIENTS; i++) {
246 		outmessage.msg_name = &clt_loop[i];
247 		outmessage.msg_namelen = sizeof(clt_loop[i]);
248 		test_sendmsg(peeloff_sk[i], &outmessage, 0, strlen(message)+1);
249 	}
250 
251 	/* Receive the messages sent from the peeled of server sockets on
252 	 * the client sockets.
253 	 */
254 	for (i = 0; i < MAX_CLIENTS; i++) {
255 		inmessage.msg_controllen = sizeof(incmsg);
256 		error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
257 		test_check_msg_data(&inmessage, error, strlen(message) + 1,
258 				    MSG_EOR, stream, ppid);
259 	}
260 
261 	tst_resm(TPASS, "Send msgs on peeled off sockets");
262 
263 	errno = 0;
264 	/* Verify that a peeled-off socket cannot initialize a new
265 	 * association by trying to send a message to a client that is not
266 	 * associated with the peeled-off socket.
267 	 * The message is sent to the client that is associated with the
268 	 * socket.
269 	 */
270 	outmessage.msg_name = &clt_loop[1];
271 	outmessage.msg_namelen = sizeof(clt_loop[1]);
272 	test_sendmsg(peeloff_sk[0], &outmessage, 0, strlen(message)+1);
273 
274 	inmessage.msg_controllen = sizeof(incmsg);
275 	error = test_recvmsg(clt_sk[0], &inmessage, MSG_WAITALL);
276 	test_check_msg_data(&inmessage, error, strlen(message) + 1,
277 			    MSG_EOR, stream, ppid);
278 
279 	tst_resm(TPASS, "peeled off socket cannot initialize a new assoc");
280 
281 	close(svr_sk);
282 
283 	/* Close all the peeled off server sockets.  */
284 	for (i = 0; i < MAX_CLIENTS; i++)
285 		close(peeloff_sk[i]);
286 
287 	/* Get the shutdown complete notification from all the client
288 	 * sockets.
289 	 */
290 	for (i = 0; i < MAX_CLIENTS; i++) {
291 		inmessage.msg_controllen = sizeof(incmsg);
292 		error = test_recvmsg(clt_sk[i], &inmessage, MSG_WAITALL);
293 		test_check_msg_notification(&inmessage, error,
294 					    sizeof(struct sctp_assoc_change),
295 					    SCTP_ASSOC_CHANGE,
296 					    SCTP_SHUTDOWN_COMP);
297 
298 		close(clt_sk[i]);
299 	}
300 
301         /* Indicate successful completion.  */
302        	return 0;
303 }
304