blob: 36030f819b1d3c5778c2a8a04fddde1d1f3d7d66 [file] [log] [blame]
mridgea72abc52006-01-05 20:54:48 +00001/* SCTP kernel reference Implementation
2 * (C) Copyright IBM Corp. 2002, 2003
3 * Copyright (c) 1999-2001 Motorola, Inc.
4 *
5 * The SCTP reference implementation is free software;
6 * you can redistribute it and/or modify it under the terms of
7 * the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * The SCTP reference implementation is distributed in the hope that it
12 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
13 * ************************
14 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU CC; see the file COPYING. If not, write to
19 * the Free Software Foundation, 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 * Please send any bug reports or fixes you make to the
23 * email address(es):
24 * lksctp developers <lksctp-developers@lists.sourceforge.net>
25 *
26 * Or submit a bug report through the following website:
27 * http://www.sf.net/projects/lksctp
28 *
29 * Any bugs reported given to us we will try to fix... any fixes shared will
30 * be incorporated into the next SCTP release.
31 *
32 * Written or modified by:
33 * Sridhar Samudrala <sri@us.ibm.com>
34 */
35
36/* This is a kernel test to verify the one-to-many style sctp_connectx()
37 * in blocking and non-blocking modes.
38 */
39
40#include <stdio.h>
41#include <unistd.h>
42#include <stdlib.h>
43#include <string.h>
44#include <fcntl.h>
45#include <sys/types.h>
46#include <sys/socket.h>
47#include <sys/uio.h>
48#include <netinet/in.h>
49#include <errno.h>
50#include <netinet/sctp.h>
51#include <sctputil.h>
52
53char *TCID = __FILE__;
54int TST_TOTAL = 9;
55int TST_CNT = 0;
56
57#define NUMADDR 6
58#define SCTP_IP_LOOPBACK_I(I) htonl(0x7f000001 + I)
59
60#define NIPQUAD(addr) \
61 ((unsigned char *)&addr)[0], \
62 ((unsigned char *)&addr)[1], \
63 ((unsigned char *)&addr)[2], \
64 ((unsigned char *)&addr)[3]
65
66int
67main(int argc, char *argv[])
68{
69 int svr_sk, clt_sk1, clt_sk2, peeloff_sk;
70 sctp_assoc_t svr_associd1, svr_associd2, clt_associd1, clt_associd2;
71 struct iovec iov;
72 struct msghdr inmessage;
73 int error, i;
74 struct sctp_assoc_change *sac;
75 char *big_buffer;
76 int flags;
77 struct sockaddr_in svr_loop[NUMADDR];
78 struct sockaddr_in svr_try[NUMADDR];
79 struct sockaddr_in clt_loop1[NUMADDR];
80 struct sockaddr_in clt_loop2[NUMADDR];
81 struct sockaddr_in clt_loop3[NUMADDR];
82 sockaddr_storage_t svr_test[NUMADDR], clt_test1[NUMADDR], clt_test2[NUMADDR];
83
84 /* Rather than fflush() throughout the code, set stdout to
85 * be unbuffered.
86 */
87 setvbuf(stdout, NULL, _IONBF, 0);
88
89 for (i = 0; i < NUMADDR; i++) {
90 /* Initialize the server and client addresses. */
91 svr_loop[i].sin_family = AF_INET;
92 svr_loop[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i);
93 svr_loop[i].sin_port = htons(SCTP_TESTPORT_1);
94 svr_test[i].v4.sin_family = AF_INET;
95 svr_test[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i);
96 svr_test[i].v4.sin_port = htons(SCTP_TESTPORT_1);
97 svr_try[i].sin_family = AF_INET;
98 if (i < (NUMADDR-1)) {
99 svr_try[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i);
100 } else {
101 /* Make last address invalid. */
102 svr_try[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x400);
103 }
104 svr_try[i].sin_port = htons(SCTP_TESTPORT_1);
105 clt_loop1[i].sin_family = AF_INET;
106 clt_loop1[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x100);
107 clt_loop1[i].sin_port = htons(SCTP_TESTPORT_2);
108 clt_test1[i].v4.sin_family = AF_INET;
109 clt_test1[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x100);
110 clt_test1[i].v4.sin_port = htons(SCTP_TESTPORT_2);
111 clt_loop2[i].sin_family = AF_INET;
112 clt_loop2[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x200);
113 clt_loop2[i].sin_port = htons(SCTP_TESTPORT_2+1);
114 clt_test2[i].v4.sin_family = AF_INET;
115 clt_test2[i].v4.sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x200);
116 clt_test2[i].v4.sin_port = htons(SCTP_TESTPORT_2+1);
117 clt_loop3[i].sin_family = AF_INET;
118 clt_loop3[i].sin_addr.s_addr = SCTP_IP_LOOPBACK_I(i + 0x300);
119 clt_loop3[i].sin_port = htons(SCTP_TESTPORT_2+2);
120 }
121
122 /* Create and bind the server socket. */
123 svr_sk = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
124 test_bind(svr_sk, (struct sockaddr *)&svr_loop[0], sizeof(svr_loop[0]));
125 test_bindx_add(svr_sk, (struct sockaddr *)&svr_loop[1], NUMADDR-1);
126
127 /* Mark server socket as being able to accept new associations. */
128 test_listen(svr_sk, 1);
129
130 /* Create and bind the client sockets. */
131 clt_sk1 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
132 test_bind(clt_sk1, (struct sockaddr *)&clt_loop1[0], sizeof(clt_loop1));
133 test_bindx_add(clt_sk1, (struct sockaddr *)&clt_loop1[1], NUMADDR-1);
134 clt_sk2 = test_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
135 test_bind(clt_sk2, (struct sockaddr *)&clt_loop2[0], sizeof(clt_loop2));
136 test_bindx_add(clt_sk2, (struct sockaddr *)&clt_loop2[1], NUMADDR-1);
137
138 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
139 test_enable_assoc_change(svr_sk);
140 test_enable_assoc_change(clt_sk1);
141 test_enable_assoc_change(clt_sk2);
142
143 /* Set clt_sk1 as non-blocking. */
144 flags = fcntl(clt_sk1, F_GETFL, 0);
145 if (flags < 0)
146 tst_brkm(TBROK, tst_exit, "fcntl F_GETFL: %s", strerror(errno));
147 if (fcntl(clt_sk1, F_SETFL, flags | O_NONBLOCK) < 0)
148 tst_brkm(TBROK, tst_exit, "fcntl F_SETFL: %s", strerror(errno));
149
150 /* Do a non-blocking connectx from clt_sk1 to svr_sk */
151 error = sctp_connectx(clt_sk1, (struct sockaddr *)svr_try, NUMADDR);
152 /* Non-blocking connectx should return immediately with EINPROGRESS. */
153 if ((error != -1) || (EINPROGRESS != errno))
154 tst_brkm(TBROK, tst_exit, "non-blocking connectx error: %d"
155 "errno:%d", error, errno);
156
157 tst_resm(TPASS, "non-blocking connectx");
158
159 /* Doing a connectx on a socket to create an association that is
160 * is already established should return EISCONN.
161 */
162 error = sctp_connectx(clt_sk1, (struct sockaddr *)svr_try, NUMADDR);
163 if ((error != -1) || (EISCONN != errno))
164 tst_brkm(TBROK, tst_exit, "connectx on a socket to create an "
165 "assoc that is already established error:%d errno:%d",
166 error, errno);
167
168 tst_resm(TPASS, "connectx on a socket to create an assoc that is "
169 "already established");
170
171 /* Initialize inmessage for all receives. */
172 memset(&inmessage, 0, sizeof(inmessage));
173 big_buffer = test_malloc(REALLY_BIG);
174 iov.iov_base = big_buffer;
175 iov.iov_len = REALLY_BIG;
176 inmessage.msg_iov = &iov;
177 inmessage.msg_iovlen = 1;
178 inmessage.msg_control = NULL;
179
180 /* Get COMM_UP on clt_sk1 */
181 error = test_recvmsg(clt_sk1, &inmessage, MSG_WAITALL);
182 test_check_msg_notification(&inmessage, error,
183 sizeof(struct sctp_assoc_change),
184 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
185 sac = (struct sctp_assoc_change *)iov.iov_base;
186 clt_associd1 = sac->sac_assoc_id;
187
188 /* Get COMM_UP on svr_sk */
189 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
190 test_check_msg_notification(&inmessage, error,
191 sizeof(struct sctp_assoc_change),
192 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
193 sac = (struct sctp_assoc_change *)iov.iov_base;
194 svr_associd1 = sac->sac_assoc_id;
195
196 /* Do a blocking connectx from clt_sk2 to svr_sk.
197 * Blocking connectx should block until the association is established
198 * and return success.
199 */
200 test_connectx(clt_sk2, (struct sockaddr *)svr_try, NUMADDR);
201
202 /* Get COMM_UP on clt_sk2 */
203 error = test_recvmsg(clt_sk2, &inmessage, MSG_WAITALL);
204 test_check_msg_notification(&inmessage, error,
205 sizeof(struct sctp_assoc_change),
206 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
207 sac = (struct sctp_assoc_change *)iov.iov_base;
208 clt_associd2 = sac->sac_assoc_id;
209
210 /* Get COMM_UP on svr_sk */
211 error = test_recvmsg(svr_sk, &inmessage, MSG_WAITALL);
212 test_check_msg_notification(&inmessage, error,
213 sizeof(struct sctp_assoc_change),
214 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
215 sac = (struct sctp_assoc_change *)iov.iov_base;
216 svr_associd2 = sac->sac_assoc_id;
217
218 tst_resm(TPASS, "blocking connectx");
219
220 peeloff_sk = test_sctp_peeloff(svr_sk, svr_associd1);
221
222 /* Doing a connectx on a peeled off socket should fail. */
223 error = sctp_connectx(peeloff_sk, (struct sockaddr *)clt_loop3, NUMADDR);
224 if ((error != -1) || (EISCONN != errno))
225 tst_brkm(TBROK, tst_exit, "connectx on a peeled off socket "
226 "error:%d, errno:%d", error, errno);
227
228 tst_resm(TPASS, "connectx on a peeled off socket");
229
230 /* Trying to create an association on a socket that matches an
231 * existing peeled-off association should fail.
232 */
233 error = sctp_connectx(svr_sk, (struct sockaddr *)clt_loop1, NUMADDR);
234 if ((error != -1) || (EADDRNOTAVAIL != errno))
235 tst_brkm(TBROK, tst_exit, "connectx to create an assoc that "
236 "matches a peeled off assoc error:%d errno:%d",
237 error, errno);
238
239 tst_resm(TPASS, "connectx to create an assoc that matches a peeled off "
240 "assoc");
241
242 test_peer_addr(peeloff_sk, svr_associd1, clt_test1, NUMADDR);
243 tst_resm(TPASS, "server association 1 peers ok");
244 test_peer_addr(svr_sk, svr_associd2, clt_test2, NUMADDR);
245 tst_resm(TPASS, "server association 2 peers ok");
246 test_peer_addr(clt_sk1, clt_associd1, svr_test, NUMADDR);
247 tst_resm(TPASS, "client association 1 peers ok");
248 test_peer_addr(clt_sk2, clt_associd2, svr_test, NUMADDR);
249 tst_resm(TPASS, "client association 2 peers ok");
250 close(svr_sk);
251 close(clt_sk1);
252 close(clt_sk2);
253 close(peeloff_sk);
254
255 /* Indicate successful completion. */
256 return 0;
257}