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 * 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>
41 */
42
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
52 * msg_sizes[].
53 */
54
55 #include <stdio.h>
56 #include <unistd.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <sys/uio.h>
62 #include <netinet/in.h>
63 #include <sys/errno.h>
64 #include <errno.h>
65 #include <netinet/sctp.h>
66 #include <sctputil.h>
67 #include "tst_kernel.h"
68
69 char *TCID = __FILE__;
70 int TST_TOTAL = 4;
71 int TST_CNT = 0;
72
73 int msg_sizes[] = {1353, 2000, 5000, 10000, 20000, 32768};
74
75 int
main(void)76 main(void)
77 {
78 int sk1, sk2;
79 sockaddr_storage_t loop1;
80 sockaddr_storage_t loop2;
81 struct iovec iov;
82 struct msghdr inmessage;
83 struct msghdr outmessage;
84 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
85 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
86 struct cmsghdr *cmsg;
87 struct sctp_sndrcvinfo *sinfo;
88 struct iovec out_iov;
89 int error, bytes_sent;
90 int pf_class;
91 uint32_t ppid;
92 uint32_t stream;
93 char *big_buffer;
94 int msg_len, msg_cnt, i;
95 void *msg_buf;
96 int disable_frag;
97 socklen_t optlen;
98
99 if (tst_check_driver("sctp"))
100 tst_brkm(TCONF, tst_exit, "sctp driver not available");
101
102 /* Rather than fflush() throughout the code, set stdout to
103 * be unbuffered.
104 */
105 setvbuf(stdout, NULL, _IONBF, 0);
106
107 /* Set some basic values which depend on the address family. */
108 #if TEST_V6
109 pf_class = PF_INET6;
110
111 loop1.v6.sin6_family = AF_INET6;
112 loop1.v6.sin6_addr = in6addr_loopback;
113 loop1.v6.sin6_port = htons(SCTP_TESTPORT_1);
114
115 loop2.v6.sin6_family = AF_INET6;
116 loop2.v6.sin6_addr = in6addr_loopback;
117 loop2.v6.sin6_port = htons(SCTP_TESTPORT_2);
118 #else
119 pf_class = PF_INET;
120
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);
124
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);
128 #endif /* TEST_V6 */
129
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);
133
134 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
135 test_enable_assoc_change(sk1);
136 test_enable_assoc_change(sk2);
137
138 /* Bind these sockets to the test ports. */
139 test_bind(sk1, &loop1.sa, sizeof(loop1));
140 test_bind(sk2, &loop2.sa, sizeof(loop2));
141
142 /* Mark sk2 as being able to accept new associations. */
143 test_listen(sk2, 1);
144
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. */
161 stream = 1;
162 sinfo->sinfo_ppid = ppid;
163 sinfo->sinfo_stream = stream;
164 msg_len = 10;
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);
169
170
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;
179
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 #if 0
187 sac = (struct sctp_assoc_change *)iov.iov_base;
188 associd2 = sac->sac_assoc_id;
189 #endif
190 /* Get the communication up message on sk1. */
191 inmessage.msg_controllen = sizeof(incmsg);
192 error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
193 test_check_msg_notification(&inmessage, error,
194 sizeof(struct sctp_assoc_change),
195 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
196 #if 0
197 sac = (struct sctp_assoc_change *)iov.iov_base;
198 associd1 = sac->sac_assoc_id;
199 #endif
200 /* Get the first message which was sent. */
201 inmessage.msg_controllen = sizeof(incmsg);
202 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
203 test_check_msg_data(&inmessage, error, msg_len, MSG_EOR, stream, ppid);
204
205 free(msg_buf);
206
207 /* Disable fragmentation. */
208 disable_frag = 1;
209 test_setsockopt(sk1, SCTP_DISABLE_FRAGMENTS, &disable_frag,
210 sizeof(disable_frag));
211
212 tst_resm(TPASS, "setsockopt(SCTP_DISABLE_FRAGMENTS)");
213
214 /* Do a getsockopt() and verify that fragmentation is disabled. */
215 disable_frag = 0;
216 optlen = sizeof(disable_frag);
217 error = test_getsockopt(sk1, SCTP_DISABLE_FRAGMENTS, &disable_frag,
218 &optlen);
219 if ((error != 0) && (disable_frag != 1))
220 tst_brkm(TBROK, tst_exit, "getsockopt(SCTP_DISABLE_FRAGMENTS) "
221 "error:%d errno:%d disable_frag:%d",
222 error, errno, disable_frag);
223
224 tst_resm(TPASS, "getsockopt(SCTP_DISABLE_FRAGMENTS)");
225
226 /* Try to send a messsage that exceeds association fragmentation point
227 * and verify that it fails.
228 */
229 msg_len = 100000;
230 msg_buf = test_build_msg(msg_len);
231 outmessage.msg_iov->iov_base = msg_buf;
232 outmessage.msg_iov->iov_len = msg_len;
233 error = sendmsg(sk1, &outmessage, 0);
234 if ((error != -1) || (errno != EMSGSIZE))
235 tst_brkm(TBROK, tst_exit, "Send a message that exceeds "
236 "assoc frag point error:%d errno:%d", error, errno);
237
238 tst_resm(TPASS, "Send a message that exceeds assoc frag point");
239
240 /* Enable Fragmentation. */
241 disable_frag = 0;
242 test_setsockopt(sk1, SCTP_DISABLE_FRAGMENTS, &disable_frag,
243 sizeof(disable_frag));
244
245 msg_cnt = sizeof(msg_sizes) / sizeof(int);
246
247 /* Send and receive the messages of different sizes specified in the
248 * msg_sizes array in a loop.
249 */
250 for (i = 0; i < msg_cnt; i++) {
251
252 msg_len = msg_sizes[i];
253 msg_buf = test_build_msg(msg_len);
254 outmessage.msg_iov->iov_base = msg_buf;
255 outmessage.msg_iov->iov_len = msg_len;
256 bytes_sent = test_sendmsg(sk1, &outmessage, 0, msg_len);
257
258 tst_resm(TINFO, "Sent %d byte message", bytes_sent);
259
260 inmessage.msg_controllen = sizeof(incmsg);
261 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
262 /* Handle Partial Reads. */
263 if (inmessage.msg_flags & MSG_EOR) {
264 test_check_msg_data(&inmessage, error, bytes_sent,
265 MSG_EOR, stream, ppid);
266 tst_resm(TINFO, "Received %d byte message", error);
267 } else {
268 int remain;
269
270 test_check_msg_data(&inmessage, error, error, 0,
271 stream, ppid);
272 tst_resm(TINFO, "Received %d byte message", error);
273
274 /* Read the remaining message. */
275 inmessage.msg_controllen = sizeof(incmsg);
276 remain = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
277 test_check_msg_data(&inmessage, remain,
278 bytes_sent - error,
279 MSG_EOR, stream, ppid);
280 tst_resm(TINFO, "Received %d byte message", error);
281 }
282
283 free(msg_buf);
284 }
285
286 tst_resm(TPASS, "Send/Receive fragmented messages");
287
288 /* Shut down the link. */
289 close(sk1);
290
291 /* Get the shutdown complete notification. */
292 inmessage.msg_controllen = sizeof(incmsg);
293 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
294 test_check_msg_notification(&inmessage, error,
295 sizeof(struct sctp_assoc_change),
296 SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);
297
298 close(sk2);
299
300 /* Indicate successful completion. */
301 return 0;
302 }
303