blob: 92288b27246786ebf0668f543d0123bd22fc0738 [file] [log] [blame]
Austin Schuh8d0a2852019-12-28 22:54:28 -08001/* 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 <samudrala@us.ibm.com>
41 */
42
43/* This is a basic functional test for the SCTP kernel
44 * implementation state machine.
45 */
46
47#include <stdio.h>
48#include <unistd.h>
49#include <stdlib.h>
50#include <string.h>
51#include <sys/types.h>
52#include <sys/socket.h>
53#include <sys/uio.h>
54#include <netinet/in.h>
55#include <errno.h>
56#include <netinet/sctp.h>
57#include <sctputil.h>
58
59char *TCID = __FILE__;
60int TST_TOTAL = 15;
61int TST_CNT = 0;
62
63int main(void)
64{
65 int sk1, sk2;
66 sockaddr_storage_t loop1;
67 sockaddr_storage_t loop2;
68 sockaddr_storage_t msgname;
69 struct iovec iov;
70 struct msghdr inmessage;
71 struct msghdr outmessage;
72 char incmsg[CMSG_SPACE(sizeof(sctp_cmsg_data_t))];
73 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
74 struct cmsghdr *cmsg;
75 struct sctp_sndrcvinfo *sinfo;
76 struct iovec out_iov;
77 char *message = "hello, world!\n";
78 char *telephone = "Watson, come here! I need you!\n";
79 char *telephone_resp = "I already brought your coffee...\n";
80 int error, bytes_sent;
81 int pf_class;
82 uint32_t ppid;
83 uint32_t stream;
84 sctp_assoc_t associd1, associd2;
85 struct sctp_assoc_change *sac;
86 char *big_buffer;
87 struct sockaddr *laddrs, *paddrs;
88 int n_laddrs, n_paddrs, i;
89 struct sockaddr *sa_addr;
90 struct sockaddr_in *in_addr;
91 struct sockaddr_in6 *in6_addr;
92 void *addr_buf;
93
94 /* Rather than fflush() throughout the code, set stdout to
95 * be unbuffered.
96 */
97 setvbuf(stdout, NULL, _IONBF, 0);
98
99 /* Set some basic values which depend on the address family. */
100#if TEST_V6
101 pf_class = PF_INET6;
102
103 loop1.v6.sin6_family = AF_INET6;
104 loop1.v6.sin6_addr = (struct in6_addr)SCTP_IN6ADDR_ANY_INIT;
105 loop1.v6.sin6_port = htons(SCTP_TESTPORT_1);
106
107 loop2.v6.sin6_family = AF_INET6;
108 loop2.v6.sin6_addr = in6addr_loopback;
109 loop2.v6.sin6_port = htons(SCTP_TESTPORT_2);
110#else
111 pf_class = PF_INET;
112
113 loop1.v4.sin_family = AF_INET;
114 loop1.v4.sin_addr.s_addr = INADDR_ANY;
115 loop1.v4.sin_port = htons(SCTP_TESTPORT_1);
116
117 loop2.v4.sin_family = AF_INET;
118 loop2.v4.sin_addr.s_addr = SCTP_IP_LOOPBACK;
119 loop2.v4.sin_port = htons(SCTP_TESTPORT_2);
120#endif /* TEST_V6 */
121
122 /* Create the two endpoints which will talk to each other. */
123 sk1 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
124 sk2 = test_socket(pf_class, SOCK_SEQPACKET, IPPROTO_SCTP);
125
126 tst_resm(TPASS, "socket");
127
128 /* Bind these sockets to the test ports. */
129 test_bind(sk1, &loop1.sa, sizeof(loop1));
130 test_bind(sk2, &loop2.sa, sizeof(loop2));
131
132 tst_resm(TPASS, "bind");
133
134 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
135 test_enable_assoc_change(sk1);
136 test_enable_assoc_change(sk2);
137
138 /* Initialize inmessage for all receives. */
139 big_buffer = test_malloc(REALLY_BIG);
140 memset(&inmessage, 0, sizeof(inmessage));
141 iov.iov_base = big_buffer;
142 iov.iov_len = REALLY_BIG;
143 inmessage.msg_iov = &iov;
144 inmessage.msg_iovlen = 1;
145 inmessage.msg_control = incmsg;
146 inmessage.msg_name = &msgname;
147
148 /* Try to read on socket 2. This should fail since we are
149 * neither listening, nor established.
150 */
151 inmessage.msg_controllen = sizeof(incmsg);
152 error = recvmsg(sk2, &inmessage, MSG_WAITALL);
153 if (error > 0)
154 tst_brkm(TBROK, tst_exit, "recvmsg on a socket neither"
155 "listening nor established error: %d", error);
156
157 tst_resm(TPASS, "recvmsg on a socket neither listening nor "
158 "established");
159
160 /* Mark sk2 as being able to accept new associations. */
161 error = test_listen(sk2, 1);
162
163 tst_resm(TPASS, "listen");
164
165 /* Send the first message. This will create the association. */
166 outmessage.msg_name = &loop2;
167 outmessage.msg_namelen = sizeof(loop2);
168 outmessage.msg_iov = &out_iov;
169 outmessage.msg_iovlen = 1;
170 outmessage.msg_control = outcmsg;
171 outmessage.msg_controllen = sizeof(outcmsg);
172 outmessage.msg_flags = 0;
173 cmsg = CMSG_FIRSTHDR(&outmessage);
174 cmsg->cmsg_level = IPPROTO_SCTP;
175 cmsg->cmsg_type = SCTP_SNDRCV;
176 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
177 outmessage.msg_controllen = cmsg->cmsg_len;
178 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
179 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
180 ppid = rand(); /* Choose an arbitrary value. */
181 stream = 1;
182 sinfo->sinfo_ppid = ppid;
183 sinfo->sinfo_stream = stream;
184 outmessage.msg_iov->iov_base = message;
185 outmessage.msg_iov->iov_len = strlen(message) + 1;
186 test_sendmsg(sk1, &outmessage, 0, strlen(message)+1);
187
188 tst_resm(TPASS, "sendmsg with a valid msg_name");
189
190 /* Get the communication up message on sk2. */
191 inmessage.msg_controllen = sizeof(incmsg);
192 inmessage.msg_namelen = sizeof(msgname);
193 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
194 test_check_msg_notification(&inmessage, error,
195 sizeof(struct sctp_assoc_change),
196 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
197#if TEST_V6
198
199 if (inmessage.msg_namelen != sizeof(struct sockaddr_in6)) {
200 DUMP_CORE;
201 }
202 if (msgname.v6.sin6_port != htons(SCTP_TESTPORT_1)) {
203 DUMP_CORE;
204 }
205
206 if (msgname.v6.sin6_family != AF_INET6) {
207 DUMP_CORE;
208 }
209
210 if (memcmp(&msgname.v6.sin6_addr, &in6addr_loopback,
211 sizeof(msgname.v6.sin6_addr))) {
212 DUMP_CORE;
213 }
214#else
215 if (inmessage.msg_namelen != sizeof(struct sockaddr_in)) {
216 DUMP_CORE;
217 }
218 if (msgname.v4.sin_port != htons(SCTP_TESTPORT_1)) {
219 DUMP_CORE;
220 }
221
222 if (msgname.v4.sin_family != AF_INET) {
223 DUMP_CORE;
224 }
225 if (msgname.v4.sin_addr.s_addr != SCTP_IP_LOOPBACK) {
226 DUMP_CORE;
227 }
228#endif
229 sac = (struct sctp_assoc_change *)iov.iov_base;
230 associd2 = sac->sac_assoc_id;
231
232 /* Get the communication up message on sk1. */
233 iov.iov_base = big_buffer;
234 iov.iov_len = REALLY_BIG;
235 inmessage.msg_control = incmsg;
236 inmessage.msg_controllen = sizeof(incmsg);
237 error = test_recvmsg(sk1, &inmessage, MSG_WAITALL);
238 test_check_msg_notification(&inmessage, error,
239 sizeof(struct sctp_assoc_change),
240 SCTP_ASSOC_CHANGE, SCTP_COMM_UP);
241 sac = (struct sctp_assoc_change *)iov.iov_base;
242 associd1 = sac->sac_assoc_id;
243
244 tst_resm(TPASS, "recvmsg COMM_UP notifications");
245
246 /* Get the first message which was sent. */
247 inmessage.msg_controllen = sizeof(incmsg);
248 inmessage.msg_namelen = sizeof(msgname);
249 memset(&msgname, 0, sizeof(msgname));
250 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
251 test_check_msg_data(&inmessage, error, strlen(message) + 1,
252 MSG_EOR, stream, ppid);
253#if TEST_V6
254
255 if (inmessage.msg_namelen != sizeof(struct sockaddr_in6)) {
256 DUMP_CORE;
257 }
258 if (msgname.v6.sin6_port != htons(SCTP_TESTPORT_1)) {
259 DUMP_CORE;
260 }
261
262 if (msgname.v6.sin6_family != AF_INET6) {
263 DUMP_CORE;
264 }
265
266 if (memcmp(&msgname.v6.sin6_addr, &in6addr_loopback,
267 sizeof(msgname.v6.sin6_addr))) {
268 DUMP_CORE;
269 }
270#else
271 if (inmessage.msg_namelen != sizeof(struct sockaddr_in)) {
272 DUMP_CORE;
273 }
274 if (msgname.v4.sin_port != htons(SCTP_TESTPORT_1)) {
275 DUMP_CORE;
276 }
277 if (msgname.v4.sin_family != AF_INET) {
278 DUMP_CORE;
279 }
280 if (msgname.v4.sin_addr.s_addr != SCTP_IP_LOOPBACK) {
281 DUMP_CORE;
282 }
283#endif
284
285 /* Try to send a message with NULL msg_name and associd, should fail */
286 outmessage.msg_controllen = sizeof(outcmsg);
287 outmessage.msg_flags = 0;
288 cmsg = CMSG_FIRSTHDR(&outmessage);
289 cmsg->cmsg_level = IPPROTO_SCTP;
290 cmsg->cmsg_type = SCTP_SNDRCV;
291 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
292 outmessage.msg_controllen = cmsg->cmsg_len;
293 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
294 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
295 ppid++;
296 stream++;
297 sinfo->sinfo_ppid = ppid;
298 sinfo->sinfo_stream = stream;
299 outmessage.msg_iov->iov_base = telephone;
300 outmessage.msg_iov->iov_len = strlen(telephone) + 1;
301 outmessage.msg_name = NULL;
302 outmessage.msg_namelen = 0;
303 bytes_sent = sendmsg(sk1, &outmessage, MSG_NOSIGNAL);
304 if ((bytes_sent > 0) || (EPIPE != errno))
305 tst_brkm(TBROK, tst_exit, "sendmsg with NULL associd and "
306 "NULL msg_name error:%d errno:%d", error, errno);
307
308 tst_resm(TPASS, "sendmsg with NULL associd and NULL msg_name");
309
310 /* Fill in a incorrect assoc_id, which should cause an error. */
311 sinfo->sinfo_assoc_id = associd2;
312 bytes_sent = sendmsg(sk1, &outmessage, MSG_NOSIGNAL);
313 if ((bytes_sent > 0) || (EPIPE != errno))
314 tst_brkm(TBROK, tst_exit, "sendmsg with incorrect associd "
315 "error:%d errno:%d", error, errno);
316
317 tst_resm(TPASS, "sendmsg with incorrect associd");
318
319 /* Fill in a correct assoc_id and get back to the normal testing. */
320 sinfo->sinfo_assoc_id = associd1;
321 /* Send two more messages, to cause a second SACK. */
322 test_sendmsg(sk1, &outmessage, 0, strlen(telephone)+1);
323
324 outmessage.msg_name = &loop2;
325 outmessage.msg_namelen = sizeof(loop2);
326 outmessage.msg_iov->iov_base = telephone_resp;
327 outmessage.msg_iov->iov_len = strlen(telephone_resp) + 1;
328 test_sendmsg(sk1, &outmessage, 0, strlen(telephone_resp)+1);
329
330 tst_resm(TPASS, "sendmsg with valid associd");
331
332 /* Get those two messages. */
333 inmessage.msg_controllen = sizeof(incmsg);
334 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
335 test_check_msg_data(&inmessage, error, strlen(telephone) + 1,
336 MSG_EOR, stream, ppid);
337
338 inmessage.msg_controllen = sizeof(incmsg);
339 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
340 test_check_msg_data(&inmessage, error, strlen(telephone_resp) + 1,
341 MSG_EOR, stream, ppid);
342
343 tst_resm(TPASS, "recvmsg");
344
345 n_laddrs = sctp_getladdrs(sk1, associd1, &laddrs);
346 if (n_laddrs <= 0)
347 tst_brkm(TBROK, tst_exit, "sctp_getladdrs: %s",
348 strerror(errno));
349
350 tst_resm(TPASS, "sctp_getladdrs");
351
352 addr_buf = (void *)laddrs;
353 for (i = 0; i < n_laddrs; i++) {
354 sa_addr = (struct sockaddr *)addr_buf;
355 if (AF_INET == sa_addr->sa_family) {
356 in_addr = (struct sockaddr_in *)sa_addr;
357 tst_resm(TINFO, "LOCAL ADDR %d.%d.%d.%d PORT %d",
358 NIPQUAD(in_addr->sin_addr),
359 ntohs(in_addr->sin_port));
360 addr_buf += sizeof(struct sockaddr_in);
361 } else {
362 in6_addr = (struct sockaddr_in6 *)sa_addr;
363 tst_resm(TINFO,
364 "LOCAL ADDR %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x PORT %d",
365 NIP6(in6_addr->sin6_addr),
366 ntohs(in6_addr->sin6_port));
367 addr_buf += sizeof(struct sockaddr_in6);
368 }
369 }
370
371 sctp_freeladdrs(laddrs);
372
373 tst_resm(TPASS, "sctp_freeladdrs");
374
375 n_paddrs = sctp_getpaddrs(sk1, associd1, &paddrs);
376 if (n_paddrs <= 0)
377 tst_brkm(TBROK, tst_exit, "sctp_getpaddrs: %s",
378 strerror(errno));
379
380 tst_resm(TPASS, "sctp_getpaddrs");
381
382 addr_buf = (void *)paddrs;
383 for (i = 0; i < n_paddrs; i++) {
384 sa_addr = (struct sockaddr *)addr_buf;
385 if (AF_INET == sa_addr->sa_family) {
386 in_addr = (struct sockaddr_in *)sa_addr;
387 tst_resm(TINFO, "PEER ADDR %d.%d.%d.%d PORT %d",
388 NIPQUAD(in_addr->sin_addr),
389 ntohs(in_addr->sin_port));
390 addr_buf += sizeof(struct sockaddr_in);
391 } else {
392 in6_addr = (struct sockaddr_in6 *)sa_addr;
393 tst_resm(TINFO,
394 "PEER ADDR %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x PORT %d",
395 NIP6(in6_addr->sin6_addr),
396 ntohs(in6_addr->sin6_port));
397 addr_buf += sizeof(struct sockaddr_in6);
398 }
399 }
400
401 sctp_freepaddrs(paddrs);
402
403 tst_resm(TPASS, "sctp_freepaddrs");
404
405 /* Shut down the link. */
406 close(sk1);
407
408 /* Get the shutdown complete notification. */
409 inmessage.msg_controllen = sizeof(incmsg);
410 inmessage.msg_namelen = sizeof(msgname);
411 memset(&msgname, 0, sizeof(msgname));
412 error = test_recvmsg(sk2, &inmessage, MSG_WAITALL);
413 test_check_msg_notification(&inmessage, error,
414 sizeof(struct sctp_assoc_change),
415 SCTP_ASSOC_CHANGE, SCTP_SHUTDOWN_COMP);
416#if TEST_V6
417
418 if (inmessage.msg_namelen != sizeof(struct sockaddr_in6)) {
419 DUMP_CORE;
420 }
421 if (msgname.v6.sin6_port != htons(SCTP_TESTPORT_1)) {
422 DUMP_CORE;
423 }
424
425 if (msgname.v6.sin6_family != AF_INET6) {
426 DUMP_CORE;
427 }
428
429 if (memcmp(&msgname.v6.sin6_addr, &in6addr_loopback,
430 sizeof(msgname.v6.sin6_addr))) {
431 DUMP_CORE;
432 }
433#else
434 if (inmessage.msg_namelen != sizeof(struct sockaddr_in)) {
435 DUMP_CORE;
436 }
437 if (msgname.v4.sin_port != htons(SCTP_TESTPORT_1)) {
438 DUMP_CORE;
439 }
440
441 if (msgname.v4.sin_family != AF_INET) {
442 DUMP_CORE;
443 }
444 if (msgname.v4.sin_addr.s_addr != SCTP_IP_LOOPBACK) {
445 DUMP_CORE;
446 }
447#endif
448
449 tst_resm(TPASS, "recvmsg SHUTDOWN_COMP notification");
450
451 close(sk2);
452
453 /* Indicate successful completion. */
454 return 0;
455}