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.
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)
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.
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.
25 * Please send any bug reports or fixes you make to the
27 * lksctp developers <lksctp-developers@lists.sourceforge.net>
29 * Or submit a bug report through the following website:
30 * http://www.sf.net/projects/lksctp
32 * Any bugs reported to us we will try to fix... any fixes shared will
33 * be incorporated into the next SCTP release.
35 * Written or modified by:
36 * La Monte H.P. Yarroll <piggy@acm.org>
37 * Karl Knutson <karl@athena.chicago.il.us>
38 * Hui Huang <hui.huang@nokia.com>
39 * Jon Grimm <jgrimm@us.ibm.com>
40 * Sridhar Samudrala <samudrala@us.ibm.com>
43 /* This is a basic functional test for the SCTP kernel
44 * implementation state machine.
51 #include <sys/types.h>
52 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <sys/errno.h>
57 #include <netinet/sctp.h>
60 char *TCID = __FILE__;
67 sockaddr_storage_t loop1;
68 sockaddr_storage_t loop2;
69 sockaddr_storage_t msgname;
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))];
76 struct sctp_sndrcvinfo *sinfo;
78 char *message = "hello, world!\n";
79 char *telephone = "Watson, come here! I need you!\n";
80 char *telephone_resp = "I already brought your coffee...\n";
81 int error, bytes_sent;
85 sctp_assoc_t associd1, associd2;
86 struct sctp_assoc_change *sac;
88 struct sockaddr *laddrs, *paddrs;
89 int n_laddrs, n_paddrs, i;
90 struct sockaddr *sa_addr;
91 struct sockaddr_in *in_addr;
92 struct sockaddr_in6 *in6_addr;
95 /* Rather than fflush() throughout the code, set stdout to
98 setvbuf(stdout, NULL, _IONBF, 0);
100 /* Set some basic values which depend on the address family. */
104 loop1.v6.sin6_family = AF_INET6;
105 loop1.v6.sin6_addr = (struct in6_addr)SCTP_IN6ADDR_ANY_INIT;
106 loop1.v6.sin6_port = htons(SCTP_TESTPORT_1);
108 loop2.v6.sin6_family = AF_INET6;
109 loop2.v6.sin6_addr = in6addr_loopback;
110 loop2.v6.sin6_port = htons(SCTP_TESTPORT_2);
114 loop1.v4.sin_family = AF_INET;
115 loop1.v4.sin_addr.s_addr = INADDR_ANY;
116 loop1.v4.sin_port = htons(SCTP_TESTPORT_1);
118 loop2.v4.sin_family = AF_INET;
119 loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
120 loop2.v4.sin_port = htons(SCTP_TESTPORT_2);
123 /* Create the two endpoints which will talk to each other. */
124 sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
125 sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
127 tst_resm(TPASS, "socket");
129 /* Bind these sockets to the test ports. */
130 test_bind(sk1, &loop1.sa, sizeof(loop1));
131 test_bind(sk2, &loop2.sa, sizeof(loop2));
133 tst_resm(TPASS, "bind");
135 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
136 test_enable_assoc_change(sk1);
137 test_enable_assoc_change(sk2);
139 /* Initialize inmessage for all receives. */
140 big_buffer = test_malloc(REALLY_BIG);
141 memset(&inmessage, 0, sizeof(inmessage));
142 iov.iov_base = big_buffer;
143 iov.iov_len = REALLY_BIG;
144 inmessage.msg_iov = &iov;
145 inmessage.msg_iovlen = 1;
146 inmessage.msg_control = incmsg;
147 inmessage.msg_name = &msgname;
149 /* Try to read on socket 2. This should fail since we are
150 * neither listening, nor established.
152 inmessage.msg_controllen = sizeof(incmsg);
153 error = recvmsg(sk2, &inmessage, MSG_WAITALL);
155 tst_brkm(TBROK, tst_exit, "recvmsg on a socket neither"
156 "listening nor established error: %d", error);
158 tst_resm(TPASS, "recvmsg on a socket neither listening nor "
161 /* Mark sk2 as being able to accept new associations. */
162 error = test_listen(sk2, 1);
164 tst_resm(TPASS, "listen");
166 /* Send the first message. This will create the association. */
167 outmessage.msg_name = &loop2;
168 outmessage.msg_namelen = sizeof(loop2);
169 outmessage.msg_iov = &out_iov;
170 outmessage.msg_iovlen = 1;
171 outmessage.msg_control = outcmsg;
172 outmessage.msg_controllen = sizeof(outcmsg);
173 outmessage.msg_flags = 0;
174 cmsg = CMSG_FIRSTHDR(&outmessage);
175 cmsg->cmsg_level = IPPROTO_SCTP;
176 cmsg->cmsg_type = SCTP_SNDRCV;
177 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
178 outmessage.msg_controllen = cmsg->cmsg_len;
179 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
180 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
181 ppid = rand(); /* Choose an arbitrary value. */
183 sinfo->sinfo_ppid = ppid;
184 sinfo->sinfo_stream = stream;
185 outmessage.msg_iov->iov_base = message;
186 outmessage.msg_iov->iov_len = strlen(message) + 1;
187 test_sendmsg(sk1, &outmessage, 0, strlen(message)+1);
189 tst_resm(TPASS, "sendmsg with a valid msg_name");
191 /* Get the communication up message on sk2. */
192 inmessage.msg_controllen = sizeof(incmsg);
193 inmessage.msg_namelen = sizeof(msgname);
194 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
195 test_check_msg_notification(&inmessage, error,
196 sizeof(struct sctp_assoc_change),
197 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
200 if (inmessage.msg_namelen != sizeof(struct sockaddr_in6)) {
203 if (msgname.v6.sin6_port != htons(SCTP_TESTPORT_1)) {
207 if (msgname.v6.sin6_family != AF_INET6) {
211 if (memcmp(&msgname.v6.sin6_addr, &in6addr_loopback,
212 sizeof(msgname.v6.sin6_addr))) {
216 if (inmessage.msg_namelen != sizeof(struct sockaddr_in)) {
219 if (msgname.v4.sin_port != htons(SCTP_TESTPORT_1)) {
223 if (msgname.v4.sin_family != AF_INET) {
226 if (msgname.v4.sin_addr.s_addr != SCTP_IP_LOOPBACK) {
230 sac = (struct sctp_assoc_change *)iov.iov_base;
231 associd2 = sac->sac_assoc_id;
233 /* Get the communication up message on sk1. */
234 iov.iov_base = big_buffer;
235 iov.iov_len = REALLY_BIG;
236 inmessage.msg_control = incmsg;
237 inmessage.msg_controllen = sizeof(incmsg);
238 error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
239 test_check_msg_notification(&inmessage, error,
240 sizeof(struct sctp_assoc_change),
241 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
242 sac = (struct sctp_assoc_change *)iov.iov_base;
243 associd1 = sac->sac_assoc_id;
245 tst_resm(TPASS, "recvmsg COMM_UP notifications");
247 /* Get the first message which was sent. */
248 inmessage.msg_controllen = sizeof(incmsg);
249 inmessage.msg_namelen = sizeof(msgname);
250 memset(&msgname, 0, sizeof(msgname));
251 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
252 test_check_msg_data(&inmessage, error, strlen(message) + 1,
253 MSG_EOR, stream, ppid);
256 if (inmessage.msg_namelen != sizeof(struct sockaddr_in6)) {
259 if (msgname.v6.sin6_port != htons(SCTP_TESTPORT_1)) {
263 if (msgname.v6.sin6_family != AF_INET6) {
267 if (memcmp(&msgname.v6.sin6_addr, &in6addr_loopback,
268 sizeof(msgname.v6.sin6_addr))) {
272 if (inmessage.msg_namelen != sizeof(struct sockaddr_in)) {
275 if (msgname.v4.sin_port != htons(SCTP_TESTPORT_1)) {
278 if (msgname.v4.sin_family != AF_INET) {
281 if (msgname.v4.sin_addr.s_addr != SCTP_IP_LOOPBACK) {
286 /* Try to send a message with NULL msg_name and associd, should fail */
287 outmessage.msg_controllen = sizeof(outcmsg);
288 outmessage.msg_flags = 0;
289 cmsg = CMSG_FIRSTHDR(&outmessage);
290 cmsg->cmsg_level = IPPROTO_SCTP;
291 cmsg->cmsg_type = SCTP_SNDRCV;
292 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
293 outmessage.msg_controllen = cmsg->cmsg_len;
294 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
295 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
298 sinfo->sinfo_ppid = ppid;
299 sinfo->sinfo_stream = stream;
300 outmessage.msg_iov->iov_base = telephone;
301 outmessage.msg_iov->iov_len = strlen(telephone) + 1;
302 outmessage.msg_name = NULL;
303 outmessage.msg_namelen = 0;
304 bytes_sent = sendmsg(sk1, &outmessage, MSG_NOSIGNAL);
305 if ((bytes_sent > 0) || (EPIPE != errno))
306 tst_brkm(TBROK, tst_exit, "sendmsg with NULL associd and "
307 "NULL msg_name error:%d errno:%d", error, errno);
309 tst_resm(TPASS, "sendmsg with NULL associd and NULL msg_name");
311 /* Fill in a incorrect assoc_id, which should cause an error. */
312 sinfo->sinfo_assoc_id = associd2;
313 bytes_sent = sendmsg(sk1, &outmessage, MSG_NOSIGNAL);
314 if ((bytes_sent > 0) || (EPIPE != errno))
315 tst_brkm(TBROK, tst_exit, "sendmsg with incorrect associd "
316 "error:%d errno:%d", error, errno);
318 tst_resm(TPASS, "sendmsg with incorrect associd");
320 /* Fill in a correct assoc_id and get back to the normal testing. */
321 sinfo->sinfo_assoc_id = associd1;
322 /* Send two more messages, to cause a second SACK. */
323 test_sendmsg(sk1, &outmessage, 0, strlen(telephone)+1);
325 outmessage.msg_name = &loop2;
326 outmessage.msg_namelen = sizeof(loop2);
327 outmessage.msg_iov->iov_base = telephone_resp;
328 outmessage.msg_iov->iov_len = strlen(telephone_resp) + 1;
329 test_sendmsg(sk1, &outmessage, 0, strlen(telephone_resp)+1);
331 tst_resm(TPASS, "sendmsg with valid associd");
333 /* Get those two messages. */
334 inmessage.msg_controllen = sizeof(incmsg);
335 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
336 test_check_msg_data(&inmessage, error, strlen(telephone) + 1,
337 MSG_EOR, stream, ppid);
339 inmessage.msg_controllen = sizeof(incmsg);
340 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
341 test_check_msg_data(&inmessage, error, strlen(telephone_resp) + 1,
342 MSG_EOR, stream, ppid);
344 tst_resm(TPASS, "recvmsg");
346 n_laddrs = sctp_getladdrs(sk1, associd1, &laddrs);
348 tst_brkm(TBROK, tst_exit, "sctp_getladdrs: %s",
351 tst_resm(TPASS, "sctp_getladdrs");
353 addr_buf = (void *)laddrs;
354 for (i = 0; i < n_laddrs; i++) {
355 sa_addr = (struct sockaddr *)addr_buf;
356 if (AF_INET == sa_addr->sa_family) {
357 in_addr = (struct sockaddr_in *)sa_addr;
358 tst_resm(TINFO, "LOCAL ADDR %d.%d.%d.%d PORT %d",
359 NIPQUAD(in_addr->sin_addr),
360 ntohs(in_addr->sin_port));
361 addr_buf += sizeof(struct sockaddr_in);
363 in6_addr = (struct sockaddr_in6 *)sa_addr;
365 "LOCAL ADDR %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x PORT %d",
366 NIP6(in6_addr->sin6_addr),
367 ntohs(in6_addr->sin6_port));
368 addr_buf += sizeof(struct sockaddr_in6);
372 sctp_freeladdrs(laddrs);
374 tst_resm(TPASS, "sctp_freeladdrs");
376 n_paddrs = sctp_getpaddrs(sk1, associd1, &paddrs);
378 tst_brkm(TBROK, tst_exit, "sctp_getpaddrs: %s",
381 tst_resm(TPASS, "sctp_getpaddrs");
383 addr_buf = (void *)paddrs;
384 for (i = 0; i < n_paddrs; i++) {
385 sa_addr = (struct sockaddr *)addr_buf;
386 if (AF_INET == sa_addr->sa_family) {
387 in_addr = (struct sockaddr_in *)sa_addr;
388 tst_resm(TINFO, "PEER ADDR %d.%d.%d.%d PORT %d",
389 NIPQUAD(in_addr->sin_addr),
390 ntohs(in_addr->sin_port));
391 addr_buf += sizeof(struct sockaddr_in);
393 in6_addr = (struct sockaddr_in6 *)sa_addr;
395 "PEER ADDR %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x PORT %d",
396 NIP6(in6_addr->sin6_addr),
397 ntohs(in6_addr->sin6_port));
398 addr_buf += sizeof(struct sockaddr_in6);
402 sctp_freepaddrs(paddrs);
404 tst_resm(TPASS, "sctp_freepaddrs");
406 /* Shut down the link. */
409 /* Get the shutdown complete notification. */
410 inmessage.msg_controllen = sizeof(incmsg);
411 inmessage.msg_namelen = sizeof(msgname);
412 memset(&msgname, 0, sizeof(msgname));
413 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
414 test_check_msg_notification(&inmessage, error,
415 sizeof(struct sctp_assoc_change),
416 SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);
419 if (inmessage.msg_namelen != sizeof(struct sockaddr_in6)) {
422 if (msgname.v6.sin6_port != htons(SCTP_TESTPORT_1)) {
426 if (msgname.v6.sin6_family != AF_INET6) {
430 if (memcmp(&msgname.v6.sin6_addr, &in6addr_loopback,
431 sizeof(msgname.v6.sin6_addr))) {
435 if (inmessage.msg_namelen != sizeof(struct sockaddr_in)) {
438 if (msgname.v4.sin_port != htons(SCTP_TESTPORT_1)) {
442 if (msgname.v4.sin_family != AF_INET) {
445 if (msgname.v4.sin_addr.s_addr != SCTP_IP_LOOPBACK) {
450 tst_resm(TPASS, "recvmsg SHUTDOWN_COMP notification");
454 /* Indicate successful completion. */