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 <sri@us.ibm.com>
43 /* This is a functional test to verify the data fragmentation, reassembly
44 * support and SCTP_DISABLE_FRAGMENTS socket option.
45 * The following tests are done in sequence.
46 * - Verify SCTP_DISABLE_FRAGMENTS socket option by doing a setsockopt()
47 * followed by a getsockopt().
48 * - Verify that a message size exceeding the association fragmentation
49 * point cannot be sent when fragmentation is disabled.
50 * - Send and receive a set of messages that are bigger than the path mtu.
51 * The different message sizes to be tested are specified in the array
59 #include <sys/types.h>
60 #include <sys/socket.h>
62 #include <netinet/in.h>
63 #include <sys/errno.h>
65 #include <netinet/sctp.h>
68 char *TCID = __FILE__;
72 int msg_sizes[] = {1353, 2000, 5000, 10000, 20000, 32768};
75 main(int argc, char *argv[])
78 sockaddr_storage_t loop1;
79 sockaddr_storage_t loop2;
81 struct msghdr inmessage;
82 struct msghdr outmessage;
83 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
84 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
86 struct sctp_sndrcvinfo *sinfo;
88 int error, bytes_sent;
89 int pf_class, af_family;
92 sctp_assoc_t associd1, associd2;
93 struct sctp_assoc_change *sac;
95 int msg_len, msg_cnt, i;
100 /* Rather than fflush() throughout the code, set stdout to
103 setvbuf(stdout, NULL, _IONBF, 0);
105 /* Set some basic values which depend on the address family. */
108 af_family = AF_INET6;
110 loop1.v6.sin6_family = AF_INET6;
111 loop1.v6.sin6_addr = in6addr_loopback;
112 loop1.v6.sin6_port = htons(SCTP_TESTPORT_1);
114 loop2.v6.sin6_family = AF_INET6;
115 loop2.v6.sin6_addr = in6addr_loopback;
116 loop2.v6.sin6_port = htons(SCTP_TESTPORT_2);
121 loop1.v4.sin_family = AF_INET;
122 loop1.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
123 loop1.v4.sin_port = htons(SCTP_TESTPORT_1);
125 loop2.v4.sin_family = AF_INET;
126 loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
127 loop2.v4.sin_port = htons(SCTP_TESTPORT_2);
130 /* Create the two endpoints which will talk to each other. */
131 sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
132 sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
134 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
135 test_enable_assoc_change(sk1);
136 test_enable_assoc_change(sk2);
138 /* Bind these sockets to the test ports. */
139 test_bind(sk1, &loop1.sa, sizeof(loop1));
140 test_bind(sk2, &loop2.sa, sizeof(loop2));
142 /* Mark sk2 as being able to accept new associations. */
145 /* Send the first message. This will create the association. */
146 outmessage.msg_name = &loop2;
147 outmessage.msg_namelen = sizeof(loop2);
148 outmessage.msg_iov = &out_iov;
149 outmessage.msg_iovlen = 1;
150 outmessage.msg_control = outcmsg;
151 outmessage.msg_controllen = sizeof(outcmsg);
152 outmessage.msg_flags = 0;
153 cmsg = CMSG_FIRSTHDR(&outmessage);
154 cmsg->cmsg_level = IPPROTO_SCTP;
155 cmsg->cmsg_type = SCTP_SNDRCV;
156 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
157 outmessage.msg_controllen = cmsg->cmsg_len;
158 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
159 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
160 ppid = rand(); /* Choose an arbitrary value. */
162 sinfo->sinfo_ppid = ppid;
163 sinfo->sinfo_stream = stream;
165 msg_buf = test_build_msg(10);
166 outmessage.msg_iov->iov_base = msg_buf;
167 outmessage.msg_iov->iov_len = msg_len;
168 test_sendmsg(sk1, &outmessage, 0, msg_len);
171 /* Initialize inmessage for all receives. */
172 big_buffer = test_malloc(REALLY_BIG);
173 memset(&inmessage, 0, sizeof(inmessage));
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 = incmsg;
180 /* Get the communication up message on sk2. */
181 inmessage.msg_controllen = sizeof(incmsg);
182 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
183 test_check_msg_notification(&inmessage, error,
184 sizeof(struct sctp_assoc_change),
185 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
186 sac = (struct sctp_assoc_change *)iov.iov_base;
187 associd2 = sac->sac_assoc_id;
189 /* Get the communication up message on sk1. */
190 inmessage.msg_controllen = sizeof(incmsg);
191 error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
192 test_check_msg_notification(&inmessage, error,
193 sizeof(struct sctp_assoc_change),
194 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
195 sac = (struct sctp_assoc_change *)iov.iov_base;
196 associd1 = sac->sac_assoc_id;
198 /* Get the first message which was sent. */
199 inmessage.msg_controllen = sizeof(incmsg);
200 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
201 test_check_msg_data(&inmessage, error, msg_len, MSG_EOR, stream, ppid);
205 /* Disable fragmentation. */
207 test_setsockopt(sk1, SCTP_DISABLE_FRAGMENTS, &disable_frag,
208 sizeof(disable_frag));
210 tst_resm(TPASS, "setsockopt(SCTP_DISABLE_FRAGMENTS)");
212 /* Do a getsockopt() and verify that fragmentation is disabled. */
214 optlen = sizeof(disable_frag);
215 error = test_getsockopt(sk1, SCTP_DISABLE_FRAGMENTS, &disable_frag,
217 if ((error != 0) && (disable_frag != 1))
218 tst_brkm(TBROK, tst_exit, "getsockopt(SCTP_DISABLE_FRAGMENTS) "
219 "error:%d errno:%d disable_frag:%d",
220 error, errno, disable_frag);
222 tst_resm(TPASS, "getsockopt(SCTP_DISABLE_FRAGMENTS)");
224 /* Try to send a messsage that exceeds association fragmentation point
225 * and verify that it fails.
228 msg_buf = test_build_msg(msg_len);
229 outmessage.msg_iov->iov_base = msg_buf;
230 outmessage.msg_iov->iov_len = msg_len;
231 error = sendmsg(sk1, &outmessage, 0);
232 if ((error != -1) || (errno != EMSGSIZE))
233 tst_brkm(TBROK, tst_exit, "Send a message that exceeds "
234 "assoc frag point error:%d errno:%d", error, errno);
236 tst_resm(TPASS, "Send a message that exceeds assoc frag point");
238 /* Enable Fragmentation. */
240 test_setsockopt(sk1, SCTP_DISABLE_FRAGMENTS, &disable_frag,
241 sizeof(disable_frag));
243 msg_cnt = sizeof(msg_sizes) / sizeof(int);
245 /* Send and receive the messages of different sizes specified in the
246 * msg_sizes array in a loop.
248 for (i = 0; i < msg_cnt; i++) {
250 msg_len = msg_sizes[i];
251 msg_buf = test_build_msg(msg_len);
252 outmessage.msg_iov->iov_base = msg_buf;
253 outmessage.msg_iov->iov_len = msg_len;
254 bytes_sent = test_sendmsg(sk1, &outmessage, 0, msg_len);
256 tst_resm(TINFO, "Sent %d byte message", bytes_sent);
258 inmessage.msg_controllen = sizeof(incmsg);
259 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
260 /* Handle Partial Reads. */
261 if (inmessage.msg_flags & MSG_EOR) {
262 test_check_msg_data(&inmessage, error, bytes_sent,
263 MSG_EOR, stream, ppid);
264 tst_resm(TINFO, "Received %d byte message", error);
268 test_check_msg_data(&inmessage, error, error, 0,
270 tst_resm(TINFO, "Received %d byte message", error);
272 /* Read the remaining message. */
273 inmessage.msg_controllen = sizeof(incmsg);
274 remain = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
275 test_check_msg_data(&inmessage, remain,
277 MSG_EOR, stream, ppid);
278 tst_resm(TINFO, "Received %d byte message", error);
284 tst_resm(TPASS, "Send/Receive fragmented messages");
286 /* Shut down the link. */
289 /* Get the shutdown complete notification. */
290 inmessage.msg_controllen = sizeof(incmsg);
291 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
292 test_check_msg_notification(&inmessage, error,
293 sizeof(struct sctp_assoc_change),
294 SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);
298 /* Indicate successful completion. */