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