1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
/* SCTP kernel Implementation
* (C) Copyright IBM Corp. 2001, 2003
* Copyright (C) 1999 Cisco
* Copyright (C) 1999-2000 Motorola
# Copyright (C) 2001 Nokia
* Copyright (C) 2001 La Monte H.P. Yarroll
*
* The SCTP implementation is free software;
* you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* The SCTP implementation is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* ************************
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU CC; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Please send any bug reports or fixes you make to the
* email address(es):
* lksctp developers <lksctp-developers@lists.sourceforge.net>
*
* Or submit a bug report through the following website:
* http://www.sf.net/projects/lksctp
*
* Any bugs reported to us we will try to fix... any fixes shared will
* be incorporated into the next SCTP release.
*
* Written or modified by:
* La Monte H.P. Yarroll <piggy@acm.org>
* Narasimha Budihal <narsi@refcode.org>
* Karl Knutson <karl@athena.chicago.il.us>
* Jon Grimm <jgrimm@us.ibm.com>
* Daisy Chang <daisyc@us.ibm.com>
* Sridhar Samudrala <sri@us.ibm.com>
*/
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <sys/errno.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/sctp.h>
#include <sctputil.h>
/* This function prints the cmsg data. */
void
test_print_cmsg(sctp_cmsg_t type, sctp_cmsg_data_t *data)
{
switch(type) {
case SCTP_INIT:
printf("INIT\n");
printf(" sinit_num_ostreams %d\n",
data->init.sinit_num_ostreams);
printf(" sinit_max_instreams %d\n",
data->init.sinit_max_instreams);
printf(" sinit_max_attempts %d\n",
data->init.sinit_max_attempts);
printf(" sinit_max_init_timeo %d\n",
data->init.sinit_max_init_timeo);
break;
case SCTP_SNDRCV:
printf("SNDRCV\n");
printf(" sinfo_stream %u\n", data->sndrcv.sinfo_stream);
printf(" sinfo_ssn %u\n", data->sndrcv.sinfo_ssn);
printf(" sinfo_flags 0x%x\n", data->sndrcv.sinfo_flags);
printf(" sinfo_ppid %u\n", data->sndrcv.sinfo_ppid);
printf(" sinfo_context %x\n", data->sndrcv.sinfo_context);
printf(" sinfo_tsn %u\n", data->sndrcv.sinfo_tsn);
printf(" sinfo_cumtsn %u\n", data->sndrcv.sinfo_cumtsn);
printf(" sinfo_assoc_id %u\n", data->sndrcv.sinfo_assoc_id);
break;
default:
printf("UNKNOWN CMSG: %d\n", type);
break;
}
}
/* This function prints the message. */
void
test_print_message(int sk, struct msghdr *msg, size_t msg_len)
{
sctp_cmsg_data_t *data;
struct cmsghdr *cmsg;
int i;
int done = 0;
char save;
union sctp_notification *sn;
for (cmsg = CMSG_FIRSTHDR(msg);
cmsg != NULL;
cmsg = CMSG_NXTHDR(msg, cmsg)) {
data = (sctp_cmsg_data_t *)CMSG_DATA(cmsg);
test_print_cmsg(cmsg->cmsg_type, data);
}
if (!(MSG_NOTIFICATION & msg->msg_flags)) {
int index = 0;
/* Make sure that everything is printable and that we
* are NUL terminated...
*/
printf("DATA(%d): ", msg_len);
while ( msg_len > 0 ) {
char *text;
int len;
text = msg->msg_iov[index].iov_base;
len = msg->msg_iov[index].iov_len;
save = text[msg_len-1];
if ( len > msg_len ) {
text[(len = msg_len) - 1] = '\0';
}
if ( (msg_len -= len) > 0 ) { index++; }
for (i = 0; i < len - 1; ++i) {
if (!isprint(text[i])) text[i] = '.';
}
printf("%s", text);
text[msg_len-1] = save;
if ( (done = !strcmp(text, "exit")) ) { break; }
}
} else {
printf("NOTIFICATION: ");
sn = (union sctp_notification *)msg->msg_iov[0].iov_base;
switch (sn->sn_header.sn_type) {
case SCTP_ASSOC_CHANGE:
switch (sn->sn_assoc_change.sac_state) {
case SCTP_COMM_UP:
printf("ASSOC_CHANGE - COMM_UP");
break;
case SCTP_COMM_LOST:
printf("ASSOC_CHANGE - COMM_LOST");
break;
case SCTP_RESTART:
printf("ASSOC_CHANGE - RESTART");
break;
case SCTP_SHUTDOWN_COMP:
printf("ASSOC_CHANGE - SHUTDOWN_COMP");
break;
case SCTP_CANT_STR_ASSOC:
printf("ASSOC_CHANGE - CANT_STR_ASSOC");
break;
default:
printf("ASSOC_CHANGE - UNEXPECTED(%d)",
sn->sn_assoc_change.sac_state);
break;
}
break;
default:
printf("%d", sn->sn_header.sn_type);
break;
}
}
printf("\n");
}
/* Check if a buf/msg_flags matches a notification, its type, and possibly an
* additional field in the corresponding notification structure.
*/
void
test_check_buf_notification(void *buf, int datalen, int msg_flags,
int expected_datalen, uint16_t expected_sn_type,
uint32_t expected_additional)
{
union sctp_notification *sn;
if (!(msg_flags & MSG_NOTIFICATION))
tst_brkm(TBROK, tst_exit, "Got a datamsg, expecting "
"notification");
if (expected_datalen <= 0)
return;
if (datalen != expected_datalen)
tst_brkm(TBROK, tst_exit, "Got a notification of unexpected "
"length:%d, expected length:%d", datalen,
expected_datalen);
sn = (union sctp_notification *)buf;
if (sn->sn_header.sn_type != expected_sn_type)
tst_brkm(TBROK, tst_exit, "Unexpected notification:%d"
"expected:%d", sn->sn_header.sn_type,
expected_sn_type);
switch(sn->sn_header.sn_type){
case SCTP_ASSOC_CHANGE:
if (sn->sn_assoc_change.sac_state != expected_additional)
tst_brkm(TBROK, tst_exit, "Unexpected sac_state:%d "
"expected:%d", sn->sn_assoc_change.sac_state,
expected_additional);
break;
default:
break;
}
}
/* Check if a message matches a notification, its type, and possibly an
* additional field in the corresponding notification structure.
*/
void
test_check_msg_notification(struct msghdr *msg, int datalen,
int expected_datalen, uint16_t expected_sn_type,
uint32_t expected_additional)
{
test_check_buf_notification(msg->msg_iov[0].iov_base, datalen,
msg->msg_flags, expected_datalen,
expected_sn_type, expected_additional);
}
/* Check if a buf/msg_flags/sinfo corresponds to data, its length, msg_flags,
* stream and ppid.
*/
void
test_check_buf_data(void *buf, int datalen, int msg_flags,
struct sctp_sndrcvinfo *sinfo, int expected_datalen,
int expected_msg_flags, uint16_t expected_stream,
uint32_t expected_ppid)
{
if (msg_flags & MSG_NOTIFICATION)
tst_brkm(TBROK, tst_exit, "Got a notification, expecting a"
"datamsg");
if (expected_datalen <= 0)
return;
if (datalen != expected_datalen)
tst_brkm(TBROK, tst_exit, "Got a datamsg of unexpected "
"length:%d, expected length:%d", datalen,
expected_datalen);
if ((msg_flags & ~0x80000000) != expected_msg_flags)
tst_brkm(TBROK, tst_exit, "Unexpected msg_flags:0x%x "
"expecting:0x%x", msg_flags, expected_msg_flags);
if ((0 == expected_stream) && (0 == expected_ppid))
return;
if (!sinfo)
tst_brkm(TBROK, tst_exit, "Null sinfo, but expected "
"stream:%d expected ppid:%d", expected_stream,
expected_ppid);
if (sinfo->sinfo_stream != expected_stream)
tst_brkm(TBROK, tst_exit, "stream mismatch: expected:%x "
"got:%x", expected_stream, sinfo->sinfo_stream);
if (sinfo->sinfo_ppid != expected_ppid)
tst_brkm(TBROK, tst_exit, "ppid mismatch: expected:%x "
"got:%x\n", expected_ppid, sinfo->sinfo_ppid);
}
/* Check if a message corresponds to data, its length, msg_flags, stream and
* ppid.
*/
void
test_check_msg_data(struct msghdr *msg, int datalen, int expected_datalen,
int expected_msg_flags, uint16_t expected_stream,
uint32_t expected_ppid)
{
struct cmsghdr *cmsg = NULL;
struct sctp_sndrcvinfo *sinfo = NULL;
/* Receive auxiliary data in msgh. */
for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
cmsg = CMSG_NXTHDR(msg, cmsg)){
if (IPPROTO_SCTP == cmsg->cmsg_level &&
SCTP_SNDRCV == cmsg->cmsg_type)
break;
} /* for( all cmsgs) */
if ((!cmsg) ||
(cmsg->cmsg_len < CMSG_LEN(sizeof(struct sctp_sndrcvinfo))))
sinfo = NULL;
else
sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
test_check_buf_data(msg->msg_iov[0].iov_base, datalen, msg->msg_flags,
sinfo, expected_datalen, expected_msg_flags,
expected_stream, expected_ppid);
}
/* Allocate a buffer of requested len and fill in with data. */
void *
test_build_msg(int len)
{
int i = len - 1;
int n;
unsigned char msg[] =
"012345678901234567890123456789012345678901234567890";
char *msg_buf, *p;
msg_buf = (char *)malloc(len);
if (!msg_buf)
tst_brkm(TBROK, tst_exit, "malloc failed");
p = msg_buf;
do {
n = ((i > 50)?50:i);
memcpy(p, msg, ((i > 50)?50:i));
p += n;
i -= n;
} while (i > 0);
msg_buf[len-1] = '\0';
return(msg_buf);
}
/* Enable ASSOC_CHANGE and SNDRCVINFO notifications. */
void test_enable_assoc_change(int fd)
{
struct sctp_event_subscribe subscribe;
memset(&subscribe, 0, sizeof(subscribe));
subscribe.sctp_data_io_event = 1;
subscribe.sctp_association_event = 1;
test_setsockopt(fd, SCTP_EVENTS, (char *)&subscribe,
sizeof(subscribe));
}
static int cmp_addr(sockaddr_storage_t *addr1, sockaddr_storage_t *addr2)
{
if (addr1->sa.sa_family != addr2->sa.sa_family)
return 0;
switch (addr1->sa.sa_family) {
case AF_INET6:
if (addr1->v6.sin6_port != addr2->v6.sin6_port)
return -1;
return memcmp(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr,
sizeof(addr1->v6.sin6_addr));
case AF_INET:
if (addr1->v4.sin_port != addr2->v4.sin_port)
return 0;
return memcmp(&addr1->v4.sin_addr, &addr2->v4.sin_addr,
sizeof(addr1->v4.sin_addr));
default:
tst_brkm(TBROK, tst_exit, "invalid address type %d",
addr1->sa.sa_family);
return -1;
}
}
/* Test peer addresses for association. */
int test_peer_addr(int sk, sctp_assoc_t asoc, sockaddr_storage_t *peers, int count)
{
struct sockaddr *addrs;
int error, i, j;
struct sockaddr *sa_addr;
socklen_t addrs_size = 0;
void *addrbuf;
char *found = (char *) malloc(count);
memset(found, 0, count);
error = sctp_getpaddrs(sk, asoc, &addrs);
if (-1 == error) {
tst_brkm(TBROK, tst_exit, "sctp_getpaddrs: %s", strerror(errno));
return error;
}
if (error != count) {
sctp_freepaddrs(addrs);
tst_brkm(TBROK, tst_exit, "peer count %d mismatch, expected %d",
error, count);
}
addrbuf = addrs;
for (i = 0; i < count; i++) {
sa_addr = (struct sockaddr *)addrbuf;
switch (sa_addr->sa_family) {
case AF_INET:
addrs_size += sizeof(struct sockaddr_in);
addrbuf += sizeof(struct sockaddr_in);
break;
case AF_INET6:
addrs_size += sizeof(struct sockaddr_in6);
addrbuf += sizeof(struct sockaddr_in6);
break;
default:
errno = EINVAL;
sctp_freepaddrs(addrs);
tst_brkm(TBROK, tst_exit, "sctp_getpaddrs: %s", strerror(errno));
return -1;
}
for (j = 0; j < count; j++) {
if (cmp_addr((sockaddr_storage_t *)sa_addr,
&peers[j]) == 0) {
found[j] = 1;
}
}
}
for (j = 0; j < count; j++) {
if (found[j] == 0) {
tst_brkm(TBROK, tst_exit, "peer address %d not found", j);
}
}
sctp_freepaddrs(addrs);
return 0;
}
|