1 /*- 2 * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * a) Redistributions of source code must retain the above copyright notice, 10 * this list of conditions and the following disclaimer. 11 * 12 * b) Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the distribution. 15 * 16 * c) Neither the name of Cisco Systems, Inc. nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include <stdio.h> 37 #include <string.h> 38 #include <errno.h> 39 #include <stdlib.h> 40 #include <unistd.h> 41 #include <sys/types.h> 42 #include <sys/socket.h> 43 #include <sys/errno.h> 44 #include <sys/syscall.h> 45 #include <sys/uio.h> 46 #include <netinet/in.h> 47 #include <arpa/inet.h> 48 #include <netinet/sctp_uio.h> 49 #include <netinet/sctp.h> 50 51 #ifndef IN6_IS_ADDR_V4MAPPED 52 #define IN6_IS_ADDR_V4MAPPED(a) \ 53 ((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) && \ 54 (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) && \ 55 (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == ntohl(0x0000ffff))) 56 #endif 57 58 #define SCTP_CONTROL_VEC_SIZE_RCV 16384 59 60 61 static void 62 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 63 { 64 bzero(sin, sizeof(*sin)); 65 sin->sin_len = sizeof(struct sockaddr_in); 66 sin->sin_family = AF_INET; 67 sin->sin_port = sin6->sin6_port; 68 sin->sin_addr.s_addr = sin6->sin6_addr.__u6_addr.__u6_addr32[3]; 69 } 70 71 int 72 sctp_getaddrlen(sa_family_t family) 73 { 74 int ret, sd; 75 socklen_t siz; 76 struct sctp_assoc_value av; 77 78 av.assoc_value = family; 79 siz = sizeof(av); 80 #if defined(AF_INET) 81 sd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); 82 #elif defined(AF_INET6) 83 sd = socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP); 84 #else 85 sd = -1; 86 #endif 87 if (sd == -1) { 88 return (-1); 89 } 90 ret = getsockopt(sd, IPPROTO_SCTP, SCTP_GET_ADDR_LEN, &av, &siz); 91 close(sd); 92 if (ret == 0) { 93 return ((int)av.assoc_value); 94 } else { 95 return (-1); 96 } 97 } 98 99 int 100 sctp_connectx(int sd, const struct sockaddr *addrs, int addrcnt, 101 sctp_assoc_t * id) 102 { 103 char *buf; 104 int i, ret, *aa; 105 char *cpto; 106 const struct sockaddr *at; 107 size_t len; 108 109 /* validate the address count and list */ 110 if ((addrs == NULL) || (addrcnt <= 0)) { 111 errno = EINVAL; 112 return (-1); 113 } 114 if ((buf = malloc(sizeof(int) + (size_t)addrcnt * sizeof(struct sockaddr_in6))) == NULL) { 115 errno = E2BIG; 116 return (-1); 117 } 118 len = sizeof(int); 119 at = addrs; 120 cpto = buf + sizeof(int); 121 /* validate all the addresses and get the size */ 122 for (i = 0; i < addrcnt; i++) { 123 switch (at->sa_family) { 124 case AF_INET: 125 if (at->sa_len != sizeof(struct sockaddr_in)) { 126 free(buf); 127 errno = EINVAL; 128 return (-1); 129 } 130 memcpy(cpto, at, sizeof(struct sockaddr_in)); 131 cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in)); 132 len += sizeof(struct sockaddr_in); 133 break; 134 case AF_INET6: 135 if (at->sa_len != sizeof(struct sockaddr_in6)) { 136 free(buf); 137 errno = EINVAL; 138 return (-1); 139 } 140 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)at)->sin6_addr)) { 141 in6_sin6_2_sin((struct sockaddr_in *)cpto, (struct sockaddr_in6 *)at); 142 cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in)); 143 len += sizeof(struct sockaddr_in); 144 } else { 145 memcpy(cpto, at, sizeof(struct sockaddr_in6)); 146 cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in6)); 147 len += sizeof(struct sockaddr_in6); 148 } 149 break; 150 default: 151 free(buf); 152 errno = EINVAL; 153 return (-1); 154 } 155 at = (struct sockaddr *)((caddr_t)at + at->sa_len); 156 } 157 aa = (int *)buf; 158 *aa = addrcnt; 159 ret = setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X, (void *)buf, 160 (socklen_t) len); 161 if ((ret == 0) && (id != NULL)) { 162 *id = *(sctp_assoc_t *) buf; 163 } 164 free(buf); 165 return (ret); 166 } 167 168 int 169 sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, int flags) 170 { 171 struct sctp_getaddresses *gaddrs; 172 struct sockaddr *sa; 173 struct sockaddr_in *sin; 174 struct sockaddr_in6 *sin6; 175 int i; 176 size_t argsz; 177 uint16_t sport = 0; 178 179 /* validate the flags */ 180 if ((flags != SCTP_BINDX_ADD_ADDR) && 181 (flags != SCTP_BINDX_REM_ADDR)) { 182 errno = EFAULT; 183 return (-1); 184 } 185 /* validate the address count and list */ 186 if ((addrcnt <= 0) || (addrs == NULL)) { 187 errno = EINVAL; 188 return (-1); 189 } 190 /* First pre-screen the addresses */ 191 sa = addrs; 192 for (i = 0; i < addrcnt; i++) { 193 switch (sa->sa_family) { 194 case AF_INET: 195 if (sa->sa_len != sizeof(struct sockaddr_in)) { 196 errno = EINVAL; 197 return (-1); 198 } 199 sin = (struct sockaddr_in *)sa; 200 if (sin->sin_port) { 201 /* non-zero port, check or save */ 202 if (sport) { 203 /* Check against our port */ 204 if (sport != sin->sin_port) { 205 errno = EINVAL; 206 return (-1); 207 } 208 } else { 209 /* save off the port */ 210 sport = sin->sin_port; 211 } 212 } 213 break; 214 case AF_INET6: 215 if (sa->sa_len != sizeof(struct sockaddr_in6)) { 216 errno = EINVAL; 217 return (-1); 218 } 219 sin6 = (struct sockaddr_in6 *)sa; 220 if (sin6->sin6_port) { 221 /* non-zero port, check or save */ 222 if (sport) { 223 /* Check against our port */ 224 if (sport != sin6->sin6_port) { 225 errno = EINVAL; 226 return (-1); 227 } 228 } else { 229 /* save off the port */ 230 sport = sin6->sin6_port; 231 } 232 } 233 break; 234 default: 235 /* Invalid address family specified. */ 236 errno = EAFNOSUPPORT; 237 return (-1); 238 } 239 sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 240 } 241 argsz = sizeof(struct sctp_getaddresses) + 242 sizeof(struct sockaddr_storage); 243 if ((gaddrs = (struct sctp_getaddresses *)malloc(argsz)) == NULL) { 244 errno = ENOMEM; 245 return (-1); 246 } 247 sa = addrs; 248 for (i = 0; i < addrcnt; i++) { 249 memset(gaddrs, 0, argsz); 250 gaddrs->sget_assoc_id = 0; 251 memcpy(gaddrs->addr, sa, sa->sa_len); 252 /* 253 * Now, if there was a port mentioned, assure that the first 254 * address has that port to make sure it fails or succeeds 255 * correctly. 256 */ 257 if ((i == 0) && (sport != 0)) { 258 switch (gaddrs->addr->sa_family) { 259 case AF_INET: 260 sin = (struct sockaddr_in *)gaddrs->addr; 261 sin->sin_port = sport; 262 break; 263 case AF_INET6: 264 sin6 = (struct sockaddr_in6 *)gaddrs->addr; 265 sin6->sin6_port = sport; 266 break; 267 } 268 } 269 if (setsockopt(sd, IPPROTO_SCTP, flags, gaddrs, 270 (socklen_t) argsz) != 0) { 271 free(gaddrs); 272 return (-1); 273 } 274 sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 275 } 276 free(gaddrs); 277 return (0); 278 } 279 280 int 281 sctp_opt_info(int sd, sctp_assoc_t id, int opt, void *arg, socklen_t * size) 282 { 283 if (arg == NULL) { 284 errno = EINVAL; 285 return (-1); 286 } 287 if ((id == SCTP_CURRENT_ASSOC) || 288 (id == SCTP_ALL_ASSOC)) { 289 errno = EINVAL; 290 return (-1); 291 } 292 switch (opt) { 293 case SCTP_RTOINFO: 294 ((struct sctp_rtoinfo *)arg)->srto_assoc_id = id; 295 break; 296 case SCTP_ASSOCINFO: 297 ((struct sctp_assocparams *)arg)->sasoc_assoc_id = id; 298 break; 299 case SCTP_DEFAULT_SEND_PARAM: 300 ((struct sctp_assocparams *)arg)->sasoc_assoc_id = id; 301 break; 302 case SCTP_PRIMARY_ADDR: 303 ((struct sctp_setprim *)arg)->ssp_assoc_id = id; 304 break; 305 case SCTP_PEER_ADDR_PARAMS: 306 ((struct sctp_paddrparams *)arg)->spp_assoc_id = id; 307 break; 308 case SCTP_MAXSEG: 309 ((struct sctp_assoc_value *)arg)->assoc_id = id; 310 break; 311 case SCTP_AUTH_KEY: 312 ((struct sctp_authkey *)arg)->sca_assoc_id = id; 313 break; 314 case SCTP_AUTH_ACTIVE_KEY: 315 ((struct sctp_authkeyid *)arg)->scact_assoc_id = id; 316 break; 317 case SCTP_DELAYED_SACK: 318 ((struct sctp_sack_info *)arg)->sack_assoc_id = id; 319 break; 320 case SCTP_CONTEXT: 321 ((struct sctp_assoc_value *)arg)->assoc_id = id; 322 break; 323 case SCTP_STATUS: 324 ((struct sctp_status *)arg)->sstat_assoc_id = id; 325 break; 326 case SCTP_GET_PEER_ADDR_INFO: 327 ((struct sctp_paddrinfo *)arg)->spinfo_assoc_id = id; 328 break; 329 case SCTP_PEER_AUTH_CHUNKS: 330 ((struct sctp_authchunks *)arg)->gauth_assoc_id = id; 331 break; 332 case SCTP_LOCAL_AUTH_CHUNKS: 333 ((struct sctp_authchunks *)arg)->gauth_assoc_id = id; 334 break; 335 case SCTP_TIMEOUTS: 336 ((struct sctp_timeouts *)arg)->stimo_assoc_id = id; 337 break; 338 case SCTP_EVENT: 339 ((struct sctp_event *)arg)->se_assoc_id = id; 340 break; 341 case SCTP_DEFAULT_SNDINFO: 342 ((struct sctp_sndinfo *)arg)->snd_assoc_id = id; 343 break; 344 case SCTP_DEFAULT_PRINFO: 345 ((struct sctp_default_prinfo *)arg)->pr_assoc_id = id; 346 break; 347 case SCTP_PEER_ADDR_THLDS: 348 ((struct sctp_paddrthlds *)arg)->spt_assoc_id = id; 349 break; 350 case SCTP_REMOTE_UDP_ENCAPS_PORT: 351 ((struct sctp_udpencaps *)arg)->sue_assoc_id = id; 352 break; 353 case SCTP_ECN_SUPPORTED: 354 ((struct sctp_assoc_value *)arg)->assoc_id = id; 355 break; 356 case SCTP_PR_SUPPORTED: 357 ((struct sctp_assoc_value *)arg)->assoc_id = id; 358 break; 359 case SCTP_AUTH_SUPPORTED: 360 ((struct sctp_assoc_value *)arg)->assoc_id = id; 361 break; 362 case SCTP_ASCONF_SUPPORTED: 363 ((struct sctp_assoc_value *)arg)->assoc_id = id; 364 break; 365 case SCTP_RECONFIG_SUPPORTED: 366 ((struct sctp_assoc_value *)arg)->assoc_id = id; 367 break; 368 case SCTP_NRSACK_SUPPORTED: 369 ((struct sctp_assoc_value *)arg)->assoc_id = id; 370 break; 371 case SCTP_PKTDROP_SUPPORTED: 372 ((struct sctp_assoc_value *)arg)->assoc_id = id; 373 break; 374 case SCTP_MAX_BURST: 375 ((struct sctp_assoc_value *)arg)->assoc_id = id; 376 break; 377 case SCTP_ENABLE_STREAM_RESET: 378 ((struct sctp_assoc_value *)arg)->assoc_id = id; 379 break; 380 case SCTP_PR_STREAM_STATUS: 381 ((struct sctp_prstatus *)arg)->sprstat_assoc_id = id; 382 break; 383 case SCTP_PR_ASSOC_STATUS: 384 ((struct sctp_prstatus *)arg)->sprstat_assoc_id = id; 385 break; 386 case SCTP_MAX_CWND: 387 ((struct sctp_assoc_value *)arg)->assoc_id = id; 388 break; 389 default: 390 break; 391 } 392 return (getsockopt(sd, IPPROTO_SCTP, opt, arg, size)); 393 } 394 395 int 396 sctp_getpaddrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs) 397 { 398 struct sctp_getaddresses *addrs; 399 struct sockaddr *sa; 400 sctp_assoc_t asoc; 401 caddr_t lim; 402 socklen_t opt_len; 403 int cnt; 404 405 if (raddrs == NULL) { 406 errno = EFAULT; 407 return (-1); 408 } 409 asoc = id; 410 opt_len = (socklen_t) sizeof(sctp_assoc_t); 411 if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_REMOTE_ADDR_SIZE, 412 &asoc, &opt_len) != 0) { 413 return (-1); 414 } 415 /* size required is returned in 'asoc' */ 416 opt_len = (socklen_t) ((size_t)asoc + sizeof(struct sctp_getaddresses)); 417 addrs = calloc(1, (size_t)opt_len); 418 if (addrs == NULL) { 419 errno = ENOMEM; 420 return (-1); 421 } 422 addrs->sget_assoc_id = id; 423 /* Now lets get the array of addresses */ 424 if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_PEER_ADDRESSES, 425 addrs, &opt_len) != 0) { 426 free(addrs); 427 return (-1); 428 } 429 *raddrs = (struct sockaddr *)&addrs->addr[0]; 430 cnt = 0; 431 sa = (struct sockaddr *)&addrs->addr[0]; 432 lim = (caddr_t)addrs + opt_len; 433 while (((caddr_t)sa < lim) && (sa->sa_len > 0)) { 434 sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 435 cnt++; 436 } 437 return (cnt); 438 } 439 440 void 441 sctp_freepaddrs(struct sockaddr *addrs) 442 { 443 void *fr_addr; 444 445 /* Take away the hidden association id */ 446 fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t)); 447 /* Now free it */ 448 free(fr_addr); 449 } 450 451 int 452 sctp_getladdrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs) 453 { 454 struct sctp_getaddresses *addrs; 455 caddr_t lim; 456 struct sockaddr *sa; 457 size_t size_of_addresses; 458 socklen_t opt_len; 459 int cnt; 460 461 if (raddrs == NULL) { 462 errno = EFAULT; 463 return (-1); 464 } 465 size_of_addresses = 0; 466 opt_len = (socklen_t) sizeof(int); 467 if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDR_SIZE, 468 &size_of_addresses, &opt_len) != 0) { 469 errno = ENOMEM; 470 return (-1); 471 } 472 if (size_of_addresses == 0) { 473 errno = ENOTCONN; 474 return (-1); 475 } 476 opt_len = (socklen_t) (size_of_addresses + 477 sizeof(struct sockaddr_storage) + 478 sizeof(struct sctp_getaddresses)); 479 addrs = calloc(1, (size_t)opt_len); 480 if (addrs == NULL) { 481 errno = ENOMEM; 482 return (-1); 483 } 484 addrs->sget_assoc_id = id; 485 /* Now lets get the array of addresses */ 486 if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDRESSES, addrs, 487 &opt_len) != 0) { 488 free(addrs); 489 errno = ENOMEM; 490 return (-1); 491 } 492 *raddrs = (struct sockaddr *)&addrs->addr[0]; 493 cnt = 0; 494 sa = (struct sockaddr *)&addrs->addr[0]; 495 lim = (caddr_t)addrs + opt_len; 496 while (((caddr_t)sa < lim) && (sa->sa_len > 0)) { 497 sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 498 cnt++; 499 } 500 return (cnt); 501 } 502 503 void 504 sctp_freeladdrs(struct sockaddr *addrs) 505 { 506 void *fr_addr; 507 508 /* Take away the hidden association id */ 509 fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t)); 510 /* Now free it */ 511 free(fr_addr); 512 } 513 514 ssize_t 515 sctp_sendmsg(int s, 516 const void *data, 517 size_t len, 518 const struct sockaddr *to, 519 socklen_t tolen, 520 uint32_t ppid, 521 uint32_t flags, 522 uint16_t stream_no, 523 uint32_t timetolive, 524 uint32_t context) 525 { 526 #ifdef SYS_sctp_generic_sendmsg 527 struct sctp_sndrcvinfo sinfo; 528 529 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo)); 530 sinfo.sinfo_ppid = ppid; 531 sinfo.sinfo_flags = flags; 532 sinfo.sinfo_stream = stream_no; 533 sinfo.sinfo_timetolive = timetolive; 534 sinfo.sinfo_context = context; 535 sinfo.sinfo_assoc_id = 0; 536 return (syscall(SYS_sctp_generic_sendmsg, s, 537 data, len, to, tolen, &sinfo, 0)); 538 #else 539 struct msghdr msg; 540 struct sctp_sndrcvinfo *sinfo; 541 struct iovec iov; 542 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; 543 struct cmsghdr *cmsg; 544 struct sockaddr *who = NULL; 545 union { 546 struct sockaddr_in in; 547 struct sockaddr_in6 in6; 548 } addr; 549 550 if ((tolen > 0) && 551 ((to == NULL) || (tolen < sizeof(struct sockaddr)))) { 552 errno = EINVAL; 553 return (-1); 554 } 555 if ((to != NULL) && (tolen > 0)) { 556 switch (to->sa_family) { 557 case AF_INET: 558 if (tolen != sizeof(struct sockaddr_in)) { 559 errno = EINVAL; 560 return (-1); 561 } 562 if ((to->sa_len > 0) && 563 (to->sa_len != sizeof(struct sockaddr_in))) { 564 errno = EINVAL; 565 return (-1); 566 } 567 memcpy(&addr, to, sizeof(struct sockaddr_in)); 568 addr.in.sin_len = sizeof(struct sockaddr_in); 569 break; 570 case AF_INET6: 571 if (tolen != sizeof(struct sockaddr_in6)) { 572 errno = EINVAL; 573 return (-1); 574 } 575 if ((to->sa_len > 0) && 576 (to->sa_len != sizeof(struct sockaddr_in6))) { 577 errno = EINVAL; 578 return (-1); 579 } 580 memcpy(&addr, to, sizeof(struct sockaddr_in6)); 581 addr.in6.sin6_len = sizeof(struct sockaddr_in6); 582 break; 583 default: 584 errno = EAFNOSUPPORT; 585 return (-1); 586 } 587 who = (struct sockaddr *)&addr; 588 } 589 iov.iov_base = (char *)data; 590 iov.iov_len = len; 591 592 if (who) { 593 msg.msg_name = (caddr_t)who; 594 msg.msg_namelen = who->sa_len; 595 } else { 596 msg.msg_name = (caddr_t)NULL; 597 msg.msg_namelen = 0; 598 } 599 msg.msg_iov = &iov; 600 msg.msg_iovlen = 1; 601 msg.msg_control = cmsgbuf; 602 msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)); 603 msg.msg_flags = 0; 604 cmsg = (struct cmsghdr *)cmsgbuf; 605 cmsg->cmsg_level = IPPROTO_SCTP; 606 cmsg->cmsg_type = SCTP_SNDRCV; 607 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); 608 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); 609 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo)); 610 sinfo->sinfo_stream = stream_no; 611 sinfo->sinfo_ssn = 0; 612 sinfo->sinfo_flags = flags; 613 sinfo->sinfo_ppid = ppid; 614 sinfo->sinfo_context = context; 615 sinfo->sinfo_assoc_id = 0; 616 sinfo->sinfo_timetolive = timetolive; 617 return (sendmsg(s, &msg, 0)); 618 #endif 619 } 620 621 622 sctp_assoc_t 623 sctp_getassocid(int sd, struct sockaddr *sa) 624 { 625 struct sctp_paddrinfo sp; 626 socklen_t siz; 627 628 /* First get the assoc id */ 629 siz = sizeof(sp); 630 memset(&sp, 0, sizeof(sp)); 631 memcpy((caddr_t)&sp.spinfo_address, sa, sa->sa_len); 632 if (getsockopt(sd, IPPROTO_SCTP, 633 SCTP_GET_PEER_ADDR_INFO, &sp, &siz) != 0) { 634 /* We depend on the fact that 0 can never be returned */ 635 return ((sctp_assoc_t) 0); 636 } 637 return (sp.spinfo_assoc_id); 638 } 639 640 ssize_t 641 sctp_send(int sd, const void *data, size_t len, 642 const struct sctp_sndrcvinfo *sinfo, 643 int flags) 644 { 645 646 #ifdef SYS_sctp_generic_sendmsg 647 struct sockaddr *to = NULL; 648 649 return (syscall(SYS_sctp_generic_sendmsg, sd, 650 data, len, to, 0, sinfo, flags)); 651 #else 652 struct msghdr msg; 653 struct iovec iov; 654 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; 655 struct cmsghdr *cmsg; 656 657 if (sinfo == NULL) { 658 errno = EINVAL; 659 return (-1); 660 } 661 iov.iov_base = (char *)data; 662 iov.iov_len = len; 663 664 msg.msg_name = NULL; 665 msg.msg_namelen = 0; 666 msg.msg_iov = &iov; 667 msg.msg_iovlen = 1; 668 msg.msg_control = cmsgbuf; 669 msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)); 670 msg.msg_flags = 0; 671 cmsg = (struct cmsghdr *)cmsgbuf; 672 cmsg->cmsg_level = IPPROTO_SCTP; 673 cmsg->cmsg_type = SCTP_SNDRCV; 674 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); 675 memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo)); 676 return (sendmsg(sd, &msg, flags)); 677 #endif 678 } 679 680 681 682 ssize_t 683 sctp_sendx(int sd, const void *msg, size_t msg_len, 684 struct sockaddr *addrs, int addrcnt, 685 struct sctp_sndrcvinfo *sinfo, 686 int flags) 687 { 688 struct sctp_sndrcvinfo __sinfo; 689 ssize_t ret; 690 int i, cnt, *aa, saved_errno; 691 char *buf; 692 int no_end_cx = 0; 693 size_t len, add_len; 694 struct sockaddr *at; 695 696 if (addrs == NULL) { 697 errno = EINVAL; 698 return (-1); 699 } 700 #ifdef SYS_sctp_generic_sendmsg 701 if (addrcnt == 1) { 702 socklen_t l; 703 704 /* 705 * Quick way, we don't need to do a connectx so lets use the 706 * syscall directly. 707 */ 708 l = addrs->sa_len; 709 return (syscall(SYS_sctp_generic_sendmsg, sd, 710 msg, msg_len, addrs, l, sinfo, flags)); 711 } 712 #endif 713 714 len = sizeof(int); 715 at = addrs; 716 cnt = 0; 717 /* validate all the addresses and get the size */ 718 for (i = 0; i < addrcnt; i++) { 719 if (at->sa_family == AF_INET) { 720 add_len = sizeof(struct sockaddr_in); 721 } else if (at->sa_family == AF_INET6) { 722 add_len = sizeof(struct sockaddr_in6); 723 } else { 724 errno = EINVAL; 725 return (-1); 726 } 727 len += add_len; 728 at = (struct sockaddr *)((caddr_t)at + add_len); 729 cnt++; 730 } 731 /* do we have any? */ 732 if (cnt == 0) { 733 errno = EINVAL; 734 return (-1); 735 } 736 buf = malloc(len); 737 if (buf == NULL) { 738 errno = ENOMEM; 739 return (-1); 740 } 741 aa = (int *)buf; 742 *aa = cnt; 743 aa++; 744 memcpy((caddr_t)aa, addrs, (size_t)(len - sizeof(int))); 745 ret = setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_DELAYED, (void *)buf, 746 (socklen_t) len); 747 748 free(buf); 749 if (ret != 0) { 750 if (errno == EALREADY) { 751 no_end_cx = 1; 752 goto continue_send; 753 } 754 return (ret); 755 } 756 continue_send: 757 if (sinfo == NULL) { 758 sinfo = &__sinfo; 759 memset(&__sinfo, 0, sizeof(__sinfo)); 760 } 761 sinfo->sinfo_assoc_id = sctp_getassocid(sd, addrs); 762 if (sinfo->sinfo_assoc_id == 0) { 763 (void)setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_COMPLETE, (void *)addrs, 764 (socklen_t) addrs->sa_len); 765 errno = ENOENT; 766 return (-1); 767 } 768 ret = sctp_send(sd, msg, msg_len, sinfo, flags); 769 saved_errno = errno; 770 if (no_end_cx == 0) 771 (void)setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_COMPLETE, (void *)addrs, 772 (socklen_t) addrs->sa_len); 773 774 errno = saved_errno; 775 return (ret); 776 } 777 778 ssize_t 779 sctp_sendmsgx(int sd, 780 const void *msg, 781 size_t len, 782 struct sockaddr *addrs, 783 int addrcnt, 784 uint32_t ppid, 785 uint32_t flags, 786 uint16_t stream_no, 787 uint32_t timetolive, 788 uint32_t context) 789 { 790 struct sctp_sndrcvinfo sinfo; 791 792 memset((void *)&sinfo, 0, sizeof(struct sctp_sndrcvinfo)); 793 sinfo.sinfo_ppid = ppid; 794 sinfo.sinfo_flags = flags; 795 sinfo.sinfo_ssn = stream_no; 796 sinfo.sinfo_timetolive = timetolive; 797 sinfo.sinfo_context = context; 798 return (sctp_sendx(sd, msg, len, addrs, addrcnt, &sinfo, 0)); 799 } 800 801 ssize_t 802 sctp_recvmsg(int s, 803 void *dbuf, 804 size_t len, 805 struct sockaddr *from, 806 socklen_t * fromlen, 807 struct sctp_sndrcvinfo *sinfo, 808 int *msg_flags) 809 { 810 #ifdef SYS_sctp_generic_recvmsg 811 struct iovec iov; 812 813 iov.iov_base = dbuf; 814 iov.iov_len = len; 815 return (syscall(SYS_sctp_generic_recvmsg, s, 816 &iov, 1, from, fromlen, sinfo, msg_flags)); 817 #else 818 ssize_t sz; 819 struct msghdr msg; 820 struct iovec iov; 821 char cmsgbuf[SCTP_CONTROL_VEC_SIZE_RCV]; 822 struct cmsghdr *cmsg; 823 824 if (msg_flags == NULL) { 825 errno = EINVAL; 826 return (-1); 827 } 828 iov.iov_base = dbuf; 829 iov.iov_len = len; 830 msg.msg_name = (caddr_t)from; 831 if (fromlen == NULL) 832 msg.msg_namelen = 0; 833 else 834 msg.msg_namelen = *fromlen; 835 msg.msg_iov = &iov; 836 msg.msg_iovlen = 1; 837 msg.msg_control = cmsgbuf; 838 msg.msg_controllen = sizeof(cmsgbuf); 839 msg.msg_flags = 0; 840 sz = recvmsg(s, &msg, *msg_flags); 841 *msg_flags = msg.msg_flags; 842 if (sz <= 0) { 843 return (sz); 844 } 845 if (sinfo) { 846 sinfo->sinfo_assoc_id = 0; 847 } 848 if ((msg.msg_controllen > 0) && (sinfo != NULL)) { 849 /* 850 * parse through and see if we find the sctp_sndrcvinfo (if 851 * the user wants it). 852 */ 853 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 854 if (cmsg->cmsg_level != IPPROTO_SCTP) { 855 continue; 856 } 857 if (cmsg->cmsg_type == SCTP_SNDRCV) { 858 memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo)); 859 break; 860 } 861 if (cmsg->cmsg_type == SCTP_EXTRCV) { 862 /* 863 * Let's hope that the user provided enough 864 * enough memory. At least he asked for more 865 * information. 866 */ 867 memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_extrcvinfo)); 868 break; 869 } 870 } 871 } 872 return (sz); 873 #endif 874 } 875 876 ssize_t 877 sctp_recvv(int sd, 878 const struct iovec *iov, 879 int iovlen, 880 struct sockaddr *from, 881 socklen_t * fromlen, 882 void *info, 883 socklen_t * infolen, 884 unsigned int *infotype, 885 int *flags) 886 { 887 char cmsgbuf[SCTP_CONTROL_VEC_SIZE_RCV]; 888 struct msghdr msg; 889 struct cmsghdr *cmsg; 890 ssize_t ret; 891 struct sctp_rcvinfo *rcvinfo; 892 struct sctp_nxtinfo *nxtinfo; 893 894 if (((info != NULL) && (infolen == NULL)) || 895 ((info == NULL) && (infolen != NULL) && (*infolen != 0)) || 896 ((info != NULL) && (infotype == NULL))) { 897 errno = EINVAL; 898 return (-1); 899 } 900 if (infotype) { 901 *infotype = SCTP_RECVV_NOINFO; 902 } 903 msg.msg_name = from; 904 if (fromlen == NULL) { 905 msg.msg_namelen = 0; 906 } else { 907 msg.msg_namelen = *fromlen; 908 } 909 msg.msg_iov = (struct iovec *)iov; 910 msg.msg_iovlen = iovlen; 911 msg.msg_control = cmsgbuf; 912 msg.msg_controllen = sizeof(cmsgbuf); 913 msg.msg_flags = 0; 914 ret = recvmsg(sd, &msg, *flags); 915 *flags = msg.msg_flags; 916 if ((ret > 0) && 917 (msg.msg_controllen > 0) && 918 (infotype != NULL) && 919 (infolen != NULL) && 920 (*infolen > 0)) { 921 rcvinfo = NULL; 922 nxtinfo = NULL; 923 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 924 if (cmsg->cmsg_level != IPPROTO_SCTP) { 925 continue; 926 } 927 if (cmsg->cmsg_type == SCTP_RCVINFO) { 928 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg); 929 if (nxtinfo != NULL) { 930 break; 931 } else { 932 continue; 933 } 934 } 935 if (cmsg->cmsg_type == SCTP_NXTINFO) { 936 nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmsg); 937 if (rcvinfo != NULL) { 938 break; 939 } else { 940 continue; 941 } 942 } 943 } 944 if (rcvinfo != NULL) { 945 if ((nxtinfo != NULL) && (*infolen >= sizeof(struct sctp_recvv_rn))) { 946 struct sctp_recvv_rn *rn_info; 947 948 rn_info = (struct sctp_recvv_rn *)info; 949 rn_info->recvv_rcvinfo = *rcvinfo; 950 rn_info->recvv_nxtinfo = *nxtinfo; 951 *infolen = (socklen_t) sizeof(struct sctp_recvv_rn); 952 *infotype = SCTP_RECVV_RN; 953 } else if (*infolen >= sizeof(struct sctp_rcvinfo)) { 954 memcpy(info, rcvinfo, sizeof(struct sctp_rcvinfo)); 955 *infolen = (socklen_t) sizeof(struct sctp_rcvinfo); 956 *infotype = SCTP_RECVV_RCVINFO; 957 } 958 } else if (nxtinfo != NULL) { 959 if (*infolen >= sizeof(struct sctp_nxtinfo)) { 960 memcpy(info, nxtinfo, sizeof(struct sctp_nxtinfo)); 961 *infolen = (socklen_t) sizeof(struct sctp_nxtinfo); 962 *infotype = SCTP_RECVV_NXTINFO; 963 } 964 } 965 } 966 return (ret); 967 } 968 969 ssize_t 970 sctp_sendv(int sd, 971 const struct iovec *iov, int iovcnt, 972 struct sockaddr *addrs, int addrcnt, 973 void *info, socklen_t infolen, unsigned int infotype, 974 int flags) 975 { 976 ssize_t ret; 977 int i; 978 socklen_t addr_len; 979 struct msghdr msg; 980 in_port_t port; 981 struct sctp_sendv_spa *spa_info; 982 struct cmsghdr *cmsg; 983 char *cmsgbuf; 984 struct sockaddr *addr; 985 struct sockaddr_in *addr_in; 986 struct sockaddr_in6 *addr_in6; 987 988 if ((addrcnt < 0) || 989 (iovcnt < 0) || 990 ((addrs == NULL) && (addrcnt > 0)) || 991 ((addrs != NULL) && (addrcnt == 0)) || 992 ((iov == NULL) && (iovcnt > 0)) || 993 ((iov != NULL) && (iovcnt == 0))) { 994 errno = EINVAL; 995 return (-1); 996 } 997 cmsgbuf = malloc(CMSG_SPACE(sizeof(struct sctp_sndinfo)) + 998 CMSG_SPACE(sizeof(struct sctp_prinfo)) + 999 CMSG_SPACE(sizeof(struct sctp_authinfo)) + 1000 (size_t)addrcnt * CMSG_SPACE(sizeof(struct in6_addr))); 1001 if (cmsgbuf == NULL) { 1002 errno = ENOMEM; 1003 return (-1); 1004 } 1005 msg.msg_control = cmsgbuf; 1006 msg.msg_controllen = 0; 1007 cmsg = (struct cmsghdr *)cmsgbuf; 1008 switch (infotype) { 1009 case SCTP_SENDV_NOINFO: 1010 if ((infolen != 0) || (info != NULL)) { 1011 free(cmsgbuf); 1012 errno = EINVAL; 1013 return (-1); 1014 } 1015 break; 1016 case SCTP_SENDV_SNDINFO: 1017 if ((info == NULL) || (infolen < sizeof(struct sctp_sndinfo))) { 1018 free(cmsgbuf); 1019 errno = EINVAL; 1020 return (-1); 1021 } 1022 cmsg->cmsg_level = IPPROTO_SCTP; 1023 cmsg->cmsg_type = SCTP_SNDINFO; 1024 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo)); 1025 memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_sndinfo)); 1026 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo)); 1027 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo))); 1028 break; 1029 case SCTP_SENDV_PRINFO: 1030 if ((info == NULL) || (infolen < sizeof(struct sctp_prinfo))) { 1031 free(cmsgbuf); 1032 errno = EINVAL; 1033 return (-1); 1034 } 1035 cmsg->cmsg_level = IPPROTO_SCTP; 1036 cmsg->cmsg_type = SCTP_PRINFO; 1037 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo)); 1038 memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_prinfo)); 1039 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo)); 1040 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo))); 1041 break; 1042 case SCTP_SENDV_AUTHINFO: 1043 if ((info == NULL) || (infolen < sizeof(struct sctp_authinfo))) { 1044 free(cmsgbuf); 1045 errno = EINVAL; 1046 return (-1); 1047 } 1048 cmsg->cmsg_level = IPPROTO_SCTP; 1049 cmsg->cmsg_type = SCTP_AUTHINFO; 1050 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo)); 1051 memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_authinfo)); 1052 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo)); 1053 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo))); 1054 break; 1055 case SCTP_SENDV_SPA: 1056 if ((info == NULL) || (infolen < sizeof(struct sctp_sendv_spa))) { 1057 free(cmsgbuf); 1058 errno = EINVAL; 1059 return (-1); 1060 } 1061 spa_info = (struct sctp_sendv_spa *)info; 1062 if (spa_info->sendv_flags & SCTP_SEND_SNDINFO_VALID) { 1063 cmsg->cmsg_level = IPPROTO_SCTP; 1064 cmsg->cmsg_type = SCTP_SNDINFO; 1065 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo)); 1066 memcpy(CMSG_DATA(cmsg), &spa_info->sendv_sndinfo, sizeof(struct sctp_sndinfo)); 1067 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo)); 1068 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo))); 1069 } 1070 if (spa_info->sendv_flags & SCTP_SEND_PRINFO_VALID) { 1071 cmsg->cmsg_level = IPPROTO_SCTP; 1072 cmsg->cmsg_type = SCTP_PRINFO; 1073 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo)); 1074 memcpy(CMSG_DATA(cmsg), &spa_info->sendv_prinfo, sizeof(struct sctp_prinfo)); 1075 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo)); 1076 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo))); 1077 } 1078 if (spa_info->sendv_flags & SCTP_SEND_AUTHINFO_VALID) { 1079 cmsg->cmsg_level = IPPROTO_SCTP; 1080 cmsg->cmsg_type = SCTP_AUTHINFO; 1081 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo)); 1082 memcpy(CMSG_DATA(cmsg), &spa_info->sendv_authinfo, sizeof(struct sctp_authinfo)); 1083 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo)); 1084 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo))); 1085 } 1086 break; 1087 default: 1088 free(cmsgbuf); 1089 errno = EINVAL; 1090 return (-1); 1091 } 1092 addr = addrs; 1093 msg.msg_name = NULL; 1094 msg.msg_namelen = 0; 1095 1096 for (i = 0; i < addrcnt; i++) { 1097 switch (addr->sa_family) { 1098 case AF_INET: 1099 addr_len = (socklen_t) sizeof(struct sockaddr_in); 1100 addr_in = (struct sockaddr_in *)addr; 1101 if (addr_in->sin_len != addr_len) { 1102 free(cmsgbuf); 1103 errno = EINVAL; 1104 return (-1); 1105 } 1106 if (i == 0) { 1107 port = addr_in->sin_port; 1108 } else { 1109 if (port == addr_in->sin_port) { 1110 cmsg->cmsg_level = IPPROTO_SCTP; 1111 cmsg->cmsg_type = SCTP_DSTADDRV4; 1112 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); 1113 memcpy(CMSG_DATA(cmsg), &addr_in->sin_addr, sizeof(struct in_addr)); 1114 msg.msg_controllen += CMSG_SPACE(sizeof(struct in_addr)); 1115 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in_addr))); 1116 } else { 1117 free(cmsgbuf); 1118 errno = EINVAL; 1119 return (-1); 1120 } 1121 } 1122 break; 1123 case AF_INET6: 1124 addr_len = (socklen_t) sizeof(struct sockaddr_in6); 1125 addr_in6 = (struct sockaddr_in6 *)addr; 1126 if (addr_in6->sin6_len != addr_len) { 1127 free(cmsgbuf); 1128 errno = EINVAL; 1129 return (-1); 1130 } 1131 if (i == 0) { 1132 port = addr_in6->sin6_port; 1133 } else { 1134 if (port == addr_in6->sin6_port) { 1135 cmsg->cmsg_level = IPPROTO_SCTP; 1136 cmsg->cmsg_type = SCTP_DSTADDRV6; 1137 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_addr)); 1138 memcpy(CMSG_DATA(cmsg), &addr_in6->sin6_addr, sizeof(struct in6_addr)); 1139 msg.msg_controllen += CMSG_SPACE(sizeof(struct in6_addr)); 1140 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in6_addr))); 1141 } else { 1142 free(cmsgbuf); 1143 errno = EINVAL; 1144 return (-1); 1145 } 1146 } 1147 break; 1148 default: 1149 free(cmsgbuf); 1150 errno = EINVAL; 1151 return (-1); 1152 } 1153 if (i == 0) { 1154 msg.msg_name = addr; 1155 msg.msg_namelen = addr_len; 1156 } 1157 addr = (struct sockaddr *)((caddr_t)addr + addr_len); 1158 } 1159 if (msg.msg_controllen == 0) { 1160 msg.msg_control = NULL; 1161 } 1162 msg.msg_iov = (struct iovec *)iov; 1163 msg.msg_iovlen = iovcnt; 1164 msg.msg_flags = 0; 1165 ret = sendmsg(sd, &msg, flags); 1166 free(cmsgbuf); 1167 return (ret); 1168 } 1169 1170 1171 #if !defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT) 1172 1173 int 1174 sctp_peeloff(int sd, sctp_assoc_t assoc_id) 1175 { 1176 /* NOT supported, return invalid sd */ 1177 errno = ENOTSUP; 1178 return (-1); 1179 } 1180 1181 #endif 1182 #if defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT) 1183 int 1184 sctp_peeloff(int sd, sctp_assoc_t assoc_id) 1185 { 1186 return (syscall(SYS_sctp_peeloff, sd, assoc_id)); 1187 } 1188 1189 #endif 1190 1191 #undef SCTP_CONTROL_VEC_SIZE_RCV 1192