1 /* SCTP kernel Implementation
2 * (C) Copyright IBM Corp. 2001, 2003
3 * Copyright (C) 1999 Cisco
4 * Copyright (C) 1999-2000 Motorola
5 # Copyright (C) 2001 Nokia
6 * Copyright (C) 2001 La Monte H.P. Yarroll
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 * Narasimha Budihal <narsi@refcode.org>
38 * Karl Knutson <karl@athena.chicago.il.us>
39 * Jon Grimm <jgrimm@us.ibm.com>
40 * Daisy Chang <daisyc@us.ibm.com>
41 * Sridhar Samudrala <sri@us.ibm.com>
48 #include <sys/types.h>
49 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <sys/errno.h>
55 #include <netinet/sctp.h>
58 /* This function prints the cmsg data. */
60 test_print_cmsg(sctp_cmsg_t type, sctp_cmsg_data_t *data)
65 printf(" sinit_num_ostreams %d\n",
66 data->init.sinit_num_ostreams);
67 printf(" sinit_max_instreams %d\n",
68 data->init.sinit_max_instreams);
69 printf(" sinit_max_attempts %d\n",
70 data->init.sinit_max_attempts);
71 printf(" sinit_max_init_timeo %d\n",
72 data->init.sinit_max_init_timeo);
77 printf(" sinfo_stream %u\n", data->sndrcv.sinfo_stream);
78 printf(" sinfo_ssn %u\n", data->sndrcv.sinfo_ssn);
79 printf(" sinfo_flags 0x%x\n", data->sndrcv.sinfo_flags);
80 printf(" sinfo_ppid %u\n", data->sndrcv.sinfo_ppid);
81 printf(" sinfo_context %x\n", data->sndrcv.sinfo_context);
82 printf(" sinfo_tsn %u\n", data->sndrcv.sinfo_tsn);
83 printf(" sinfo_cumtsn %u\n", data->sndrcv.sinfo_cumtsn);
84 printf(" sinfo_assoc_id %u\n", data->sndrcv.sinfo_assoc_id);
89 printf("UNKNOWN CMSG: %d\n", type);
94 /* This function prints the message. */
96 test_print_message(int sk, struct msghdr *msg, size_t msg_len)
98 sctp_cmsg_data_t *data;
103 union sctp_notification *sn;
105 for (cmsg = CMSG_FIRSTHDR(msg);
107 cmsg = CMSG_NXTHDR(msg, cmsg)) {
108 data = (sctp_cmsg_data_t *)CMSG_DATA(cmsg);
109 test_print_cmsg(cmsg->cmsg_type, data);
112 if (!(MSG_NOTIFICATION & msg->msg_flags)) {
114 /* Make sure that everything is printable and that we
115 * are NUL terminated...
117 printf("DATA(%d): ", msg_len);
118 while ( msg_len > 0 ) {
122 text = msg->msg_iov[index].iov_base;
123 len = msg->msg_iov[index].iov_len;
125 save = text[msg_len-1];
126 if ( len > msg_len ) {
127 text[(len = msg_len) - 1] = '\0';
130 if ( (msg_len -= len) > 0 ) { index++; }
132 for (i = 0; i < len - 1; ++i) {
133 if (!isprint(text[i])) text[i] = '.';
137 text[msg_len-1] = save;
139 if ( (done = !strcmp(text, "exit")) ) { break; }
142 printf("NOTIFICATION: ");
143 sn = (union sctp_notification *)msg->msg_iov[0].iov_base;
144 switch (sn->sn_header.sn_type) {
145 case SCTP_ASSOC_CHANGE:
146 switch (sn->sn_assoc_change.sac_state) {
148 printf("ASSOC_CHANGE - COMM_UP");
151 printf("ASSOC_CHANGE - COMM_LOST");
154 printf("ASSOC_CHANGE - RESTART");
156 case SCTP_SHUTDOWN_COMP:
157 printf("ASSOC_CHANGE - SHUTDOWN_COMP");
159 case SCTP_CANT_STR_ASSOC:
160 printf("ASSOC_CHANGE - CANT_STR_ASSOC");
163 printf("ASSOC_CHANGE - UNEXPECTED(%d)",
164 sn->sn_assoc_change.sac_state);
169 printf("%d", sn->sn_header.sn_type);
177 /* Check if a buf/msg_flags matches a notification, its type, and possibly an
178 * additional field in the corresponding notification structure.
181 test_check_buf_notification(void *buf, int datalen, int msg_flags,
182 int expected_datalen, uint16_t expected_sn_type,
183 uint32_t expected_additional)
185 union sctp_notification *sn;
187 if (!(msg_flags & MSG_NOTIFICATION))
188 tst_brkm(TBROK, tst_exit, "Got a datamsg, expecting "
191 if (expected_datalen <= 0)
194 if (datalen != expected_datalen)
195 tst_brkm(TBROK, tst_exit, "Got a notification of unexpected "
196 "length:%d, expected length:%d", datalen,
199 sn = (union sctp_notification *)buf;
200 if (sn->sn_header.sn_type != expected_sn_type)
201 tst_brkm(TBROK, tst_exit, "Unexpected notification:%d"
202 "expected:%d", sn->sn_header.sn_type,
205 switch(sn->sn_header.sn_type){
206 case SCTP_ASSOC_CHANGE:
207 if (sn->sn_assoc_change.sac_state != expected_additional)
208 tst_brkm(TBROK, tst_exit, "Unexpected sac_state:%d "
209 "expected:%d", sn->sn_assoc_change.sac_state,
210 expected_additional);
217 /* Check if a message matches a notification, its type, and possibly an
218 * additional field in the corresponding notification structure.
221 test_check_msg_notification(struct msghdr *msg, int datalen,
222 int expected_datalen, uint16_t expected_sn_type,
223 uint32_t expected_additional)
225 test_check_buf_notification(msg->msg_iov[0].iov_base, datalen,
226 msg->msg_flags, expected_datalen,
227 expected_sn_type, expected_additional);
230 /* Check if a buf/msg_flags/sinfo corresponds to data, its length, msg_flags,
234 test_check_buf_data(void *buf, int datalen, int msg_flags,
235 struct sctp_sndrcvinfo *sinfo, int expected_datalen,
236 int expected_msg_flags, uint16_t expected_stream,
237 uint32_t expected_ppid)
239 if (msg_flags & MSG_NOTIFICATION)
240 tst_brkm(TBROK, tst_exit, "Got a notification, expecting a"
243 if (expected_datalen <= 0)
246 if (datalen != expected_datalen)
247 tst_brkm(TBROK, tst_exit, "Got a datamsg of unexpected "
248 "length:%d, expected length:%d", datalen,
251 if ((msg_flags & ~0x80000000) != expected_msg_flags)
252 tst_brkm(TBROK, tst_exit, "Unexpected msg_flags:0x%x "
253 "expecting:0x%x", msg_flags, expected_msg_flags);
255 if ((0 == expected_stream) && (0 == expected_ppid))
259 tst_brkm(TBROK, tst_exit, "Null sinfo, but expected "
260 "stream:%d expected ppid:%d", expected_stream,
263 if (sinfo->sinfo_stream != expected_stream)
264 tst_brkm(TBROK, tst_exit, "stream mismatch: expected:%x "
265 "got:%x", expected_stream, sinfo->sinfo_stream);
266 if (sinfo->sinfo_ppid != expected_ppid)
267 tst_brkm(TBROK, tst_exit, "ppid mismatch: expected:%x "
268 "got:%x\n", expected_ppid, sinfo->sinfo_ppid);
271 /* Check if a message corresponds to data, its length, msg_flags, stream and
275 test_check_msg_data(struct msghdr *msg, int datalen, int expected_datalen,
276 int expected_msg_flags, uint16_t expected_stream,
277 uint32_t expected_ppid)
279 struct cmsghdr *cmsg = NULL;
280 struct sctp_sndrcvinfo *sinfo = NULL;
282 /* Receive auxiliary data in msgh. */
283 for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
284 cmsg = CMSG_NXTHDR(msg, cmsg)){
285 if (IPPROTO_SCTP == cmsg->cmsg_level &&
286 SCTP_SNDRCV == cmsg->cmsg_type)
288 } /* for( all cmsgs) */
291 (cmsg->cmsg_len < CMSG_LEN(sizeof(struct sctp_sndrcvinfo))))
294 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
296 test_check_buf_data(msg->msg_iov[0].iov_base, datalen, msg->msg_flags,
297 sinfo, expected_datalen, expected_msg_flags,
298 expected_stream, expected_ppid);
303 /* Allocate a buffer of requested len and fill in with data. */
305 test_build_msg(int len)
309 unsigned char msg[] =
310 "012345678901234567890123456789012345678901234567890";
313 msg_buf = (char *)malloc(len);
315 tst_brkm(TBROK, tst_exit, "malloc failed");
321 memcpy(p, msg, ((i > 50)?50:i));
326 msg_buf[len-1] = '\0';
331 /* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
332 void test_enable_assoc_change(int fd)
334 struct sctp_event_subscribe subscribe;
336 memset(&subscribe, 0, sizeof(subscribe));
337 subscribe.sctp_data_io_event = 1;
338 subscribe.sctp_association_event = 1;
339 test_setsockopt(fd, SCTP_EVENTS, (char *)&subscribe,
343 static int cmp_addr(sockaddr_storage_t *addr1, sockaddr_storage_t *addr2)
345 if (addr1->sa.sa_family != addr2->sa.sa_family)
347 switch (addr1->sa.sa_family) {
349 if (addr1->v6.sin6_port != addr2->v6.sin6_port)
351 return memcmp(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr,
352 sizeof(addr1->v6.sin6_addr));
354 if (addr1->v4.sin_port != addr2->v4.sin_port)
356 return memcmp(&addr1->v4.sin_addr, &addr2->v4.sin_addr,
357 sizeof(addr1->v4.sin_addr));
359 tst_brkm(TBROK, tst_exit, "invalid address type %d",
360 addr1->sa.sa_family);
365 /* Test peer addresses for association. */
366 int test_peer_addr(int sk, sctp_assoc_t asoc, sockaddr_storage_t *peers, int count)
368 struct sockaddr *addrs;
370 struct sockaddr *sa_addr;
371 socklen_t addrs_size = 0;
373 char *found = (char *) malloc(count);
374 memset(found, 0, count);
376 error = sctp_getpaddrs(sk, asoc, &addrs);
378 tst_brkm(TBROK, tst_exit, "sctp_getpaddrs: %s", strerror(errno));
381 if (error != count) {
382 sctp_freepaddrs(addrs);
383 tst_brkm(TBROK, tst_exit, "peer count %d mismatch, expected %d",
387 for (i = 0; i < count; i++) {
388 sa_addr = (struct sockaddr *)addrbuf;
389 switch (sa_addr->sa_family) {
391 addrs_size += sizeof(struct sockaddr_in);
392 addrbuf += sizeof(struct sockaddr_in);
395 addrs_size += sizeof(struct sockaddr_in6);
396 addrbuf += sizeof(struct sockaddr_in6);
400 sctp_freepaddrs(addrs);
401 tst_brkm(TBROK, tst_exit, "sctp_getpaddrs: %s", strerror(errno));
404 for (j = 0; j < count; j++) {
405 if (cmp_addr((sockaddr_storage_t *)sa_addr,
411 for (j = 0; j < count; j++) {
413 tst_brkm(TBROK, tst_exit, "peer address %d not found", j);
416 sctp_freepaddrs(addrs);