• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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 <sys/errno.h>
56 #include <errno.h>
57 #include <netinet/sctp.h>
58 #include <sctputil.h>
59 
60 char *TCID = __FILE__;
61 int TST_TOTAL = 15;
62 int TST_CNT = 0;
63 
main(void)64 int main(void)
65 {
66         int sk1, sk2;
67         sockaddr_storage_t loop1;
68         sockaddr_storage_t loop2;
69 	sockaddr_storage_t msgname;
70         struct iovec iov;
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))];
75 	struct cmsghdr *cmsg;
76 	struct sctp_sndrcvinfo *sinfo;
77         struct iovec out_iov;
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;
82 	int pf_class;
83 	uint32_t ppid;
84 	uint32_t stream;
85 	sctp_assoc_t associd1, associd2;
86 	struct sctp_assoc_change *sac;
87 	char *big_buffer;
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;
93 	void *addr_buf;
94 
95         /* Rather than fflush() throughout the code, set stdout to
96 	 * be unbuffered.
97 	 */
98 	setvbuf(stdout, NULL, _IONBF, 0);
99 
100 	/* Set some basic values which depend on the address family. */
101 #if TEST_V6
102 	pf_class = PF_INET6;
103 
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);
107 
108         loop2.v6.sin6_family = AF_INET6;
109         loop2.v6.sin6_addr = in6addr_loopback;
110         loop2.v6.sin6_port = htons(SCTP_TESTPORT_2);
111 #else
112 	pf_class = PF_INET;
113 
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);
117 
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);
121 #endif /* TEST_V6 */
122 
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);
126 
127 	tst_resm(TPASS, "socket");
128 
129         /* Bind these sockets to the test ports.  */
130         test_bind(sk1, &loop1.sa, sizeof(loop1));
131         test_bind(sk2, &loop2.sa, sizeof(loop2));
132 
133 	tst_resm(TPASS, "bind");
134 
135 	/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
136 	test_enable_assoc_change(sk1);
137 	test_enable_assoc_change(sk2);
138 
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;
148 
149         /* Try to read on socket 2.  This should fail since we are
150 	 * neither listening, nor established.
151 	 */
152         inmessage.msg_controllen = sizeof(incmsg);
153         error = recvmsg(sk2, &inmessage, MSG_WAITALL);
154         if (error > 0)
155                 tst_brkm(TBROK, tst_exit, "recvmsg on a socket neither"
156 			 "listening nor established error: %d", error);
157 
158 	tst_resm(TPASS, "recvmsg on a socket neither listening nor "
159 		 "established");
160 
161        /* Mark sk2 as being able to accept new associations.  */
162 	error = test_listen(sk2, 1);
163 
164 	tst_resm(TPASS, "listen");
165 
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. */
182 	stream = 1;
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);
188 
189 	tst_resm(TPASS, "sendmsg with a valid msg_name");
190 
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);
198 #if TEST_V6
199 
200 	if (inmessage.msg_namelen != sizeof(struct sockaddr_in6)) {
201 		DUMP_CORE;
202 	}
203 	if (msgname.v6.sin6_port != htons(SCTP_TESTPORT_1)) {
204 		DUMP_CORE;
205 	}
206 
207 	if (msgname.v6.sin6_family != AF_INET6) {
208 		DUMP_CORE;
209 	}
210 
211 	if (memcmp(&msgname.v6.sin6_addr, &in6addr_loopback,
212 		   sizeof(msgname.v6.sin6_addr))) {
213 		DUMP_CORE;
214 	}
215 #else
216 	if (inmessage.msg_namelen != sizeof(struct sockaddr_in)) {
217 		DUMP_CORE;
218 	}
219 	if (msgname.v4.sin_port != htons(SCTP_TESTPORT_1)) {
220 		DUMP_CORE;
221 	}
222 
223 	if (msgname.v4.sin_family != AF_INET) {
224 		DUMP_CORE;
225 	}
226 	if (msgname.v4.sin_addr.s_addr != SCTP_IP_LOOPBACK) {
227 		DUMP_CORE;
228 	}
229 #endif
230 	sac = (struct sctp_assoc_change *)iov.iov_base;
231 	associd2 = sac->sac_assoc_id;
232 
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;
244 
245 	tst_resm(TPASS, "recvmsg COMM_UP notifications");
246 
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);
254 #if TEST_V6
255 
256 	if (inmessage.msg_namelen != sizeof(struct sockaddr_in6)) {
257 		DUMP_CORE;
258 	}
259 	if (msgname.v6.sin6_port != htons(SCTP_TESTPORT_1)) {
260 		DUMP_CORE;
261 	}
262 
263 	if (msgname.v6.sin6_family != AF_INET6) {
264 		DUMP_CORE;
265 	}
266 
267 	if (memcmp(&msgname.v6.sin6_addr, &in6addr_loopback,
268 		   sizeof(msgname.v6.sin6_addr))) {
269 		DUMP_CORE;
270 	}
271 #else
272 	if (inmessage.msg_namelen != sizeof(struct sockaddr_in)) {
273 		DUMP_CORE;
274 	}
275 	if (msgname.v4.sin_port != htons(SCTP_TESTPORT_1)) {
276 		DUMP_CORE;
277 	}
278 	if (msgname.v4.sin_family != AF_INET) {
279 		DUMP_CORE;
280 	}
281 	if (msgname.v4.sin_addr.s_addr != SCTP_IP_LOOPBACK) {
282 		DUMP_CORE;
283 	}
284 #endif
285 
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));
296 	ppid++;
297 	stream++;
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);
308 
309 	tst_resm(TPASS, "sendmsg with NULL associd and NULL msg_name");
310 
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);
317 
318 	tst_resm(TPASS, "sendmsg with incorrect associd");
319 
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);
324 
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);
330 
331 	tst_resm(TPASS, "sendmsg with valid associd");
332 
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);
338 
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);
343 
344 	tst_resm(TPASS, "recvmsg");
345 
346 	n_laddrs = sctp_getladdrs(sk1, associd1, &laddrs);
347 	if (n_laddrs <= 0)
348                 tst_brkm(TBROK, tst_exit, "sctp_getladdrs: %s",
349 			 strerror(errno));
350 
351 	tst_resm(TPASS, "sctp_getladdrs");
352 
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);
362 		} else {
363 			in6_addr = (struct sockaddr_in6 *)sa_addr;
364 			tst_resm(TINFO,
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);
369 		}
370 	}
371 
372 	sctp_freeladdrs(laddrs);
373 
374 	tst_resm(TPASS, "sctp_freeladdrs");
375 
376 	n_paddrs = sctp_getpaddrs(sk1, associd1, &paddrs);
377 	if (n_paddrs <= 0)
378                 tst_brkm(TBROK, tst_exit, "sctp_getpaddrs: %s",
379 			 strerror(errno));
380 
381 	tst_resm(TPASS, "sctp_getpaddrs");
382 
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);
392 		} else {
393 			in6_addr = (struct sockaddr_in6 *)sa_addr;
394 			tst_resm(TINFO,
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);
399 		}
400 	}
401 
402 	sctp_freepaddrs(paddrs);
403 
404 	tst_resm(TPASS, "sctp_freepaddrs");
405 
406         /* Shut down the link.  */
407         close(sk1);
408 
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);
417 #if TEST_V6
418 
419 	if (inmessage.msg_namelen != sizeof(struct sockaddr_in6)) {
420 		DUMP_CORE;
421 	}
422 	if (msgname.v6.sin6_port != htons(SCTP_TESTPORT_1)) {
423 		DUMP_CORE;
424 	}
425 
426 	if (msgname.v6.sin6_family != AF_INET6) {
427 		DUMP_CORE;
428 	}
429 
430 	if (memcmp(&msgname.v6.sin6_addr, &in6addr_loopback,
431 		   sizeof(msgname.v6.sin6_addr))) {
432 		DUMP_CORE;
433 	}
434 #else
435 	if (inmessage.msg_namelen != sizeof(struct sockaddr_in)) {
436 		DUMP_CORE;
437 	}
438 	if (msgname.v4.sin_port != htons(SCTP_TESTPORT_1)) {
439 		DUMP_CORE;
440 	}
441 
442 	if (msgname.v4.sin_family != AF_INET) {
443 		DUMP_CORE;
444 	}
445 	if (msgname.v4.sin_addr.s_addr != SCTP_IP_LOOPBACK) {
446 		DUMP_CORE;
447 	}
448 #endif
449 
450 	tst_resm(TPASS, "recvmsg SHUTDOWN_COMP notification");
451 
452         close(sk2);
453 
454         /* Indicate successful completion.  */
455        	return 0;
456 }
457