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 #include <stdbool.h> 37 #include <stddef.h> 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 sockaddr *sa; 174 struct sockaddr_in *sin; 175 struct sockaddr_in6 *sin6; 176 int i; 177 uint16_t sport; 178 bool fix_port; 179 180 /* validate the flags */ 181 if ((flags != SCTP_BINDX_ADD_ADDR) && 182 (flags != SCTP_BINDX_REM_ADDR)) { 183 errno = EFAULT; 184 return (-1); 185 } 186 /* validate the address count and list */ 187 if ((addrcnt <= 0) || (addrs == NULL)) { 188 errno = EINVAL; 189 return (-1); 190 } 191 sport = 0; 192 fix_port = false; 193 /* First pre-screen the addresses */ 194 sa = addrs; 195 for (i = 0; i < addrcnt; i++) { 196 switch (sa->sa_family) { 197 case AF_INET: 198 if (sa->sa_len != sizeof(struct sockaddr_in)) { 199 errno = EINVAL; 200 return (-1); 201 } 202 sin = (struct sockaddr_in *)sa; 203 if (sin->sin_port) { 204 /* non-zero port, check or save */ 205 if (sport) { 206 /* Check against our port */ 207 if (sport != sin->sin_port) { 208 errno = EINVAL; 209 return (-1); 210 } 211 } else { 212 /* save off the port */ 213 sport = sin->sin_port; 214 fix_port = (i > 0); 215 } 216 } 217 break; 218 case AF_INET6: 219 if (sa->sa_len != sizeof(struct sockaddr_in6)) { 220 errno = EINVAL; 221 return (-1); 222 } 223 sin6 = (struct sockaddr_in6 *)sa; 224 if (sin6->sin6_port) { 225 /* non-zero port, check or save */ 226 if (sport) { 227 /* Check against our port */ 228 if (sport != sin6->sin6_port) { 229 errno = EINVAL; 230 return (-1); 231 } 232 } else { 233 /* save off the port */ 234 sport = sin6->sin6_port; 235 fix_port = (i > 0); 236 } 237 } 238 break; 239 default: 240 /* Invalid address family specified. */ 241 errno = EAFNOSUPPORT; 242 return (-1); 243 } 244 sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 245 } 246 sa = addrs; 247 for (i = 0; i < addrcnt; i++) { 248 /* 249 * Now, if there was a port mentioned, assure that the first 250 * address has that port to make sure it fails or succeeds 251 * correctly. 252 */ 253 if (fix_port) { 254 switch (sa->sa_family) { 255 case AF_INET: 256 ((struct sockaddr_in *)sa)->sin_port = sport; 257 break; 258 case AF_INET6: 259 ((struct sockaddr_in6 *)sa)->sin6_port = sport; 260 break; 261 } 262 fix_port = false; 263 } 264 if (setsockopt(sd, IPPROTO_SCTP, flags, sa, sa->sa_len) != 0) { 265 return (-1); 266 } 267 sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 268 } 269 return (0); 270 } 271 272 int 273 sctp_opt_info(int sd, sctp_assoc_t id, int opt, void *arg, socklen_t *size) 274 { 275 if (arg == NULL) { 276 errno = EINVAL; 277 return (-1); 278 } 279 if ((id == SCTP_CURRENT_ASSOC) || 280 (id == SCTP_ALL_ASSOC)) { 281 errno = EINVAL; 282 return (-1); 283 } 284 switch (opt) { 285 case SCTP_RTOINFO: 286 ((struct sctp_rtoinfo *)arg)->srto_assoc_id = id; 287 break; 288 case SCTP_ASSOCINFO: 289 ((struct sctp_assocparams *)arg)->sasoc_assoc_id = id; 290 break; 291 case SCTP_DEFAULT_SEND_PARAM: 292 ((struct sctp_assocparams *)arg)->sasoc_assoc_id = id; 293 break; 294 case SCTP_PRIMARY_ADDR: 295 ((struct sctp_setprim *)arg)->ssp_assoc_id = id; 296 break; 297 case SCTP_PEER_ADDR_PARAMS: 298 ((struct sctp_paddrparams *)arg)->spp_assoc_id = id; 299 break; 300 case SCTP_MAXSEG: 301 ((struct sctp_assoc_value *)arg)->assoc_id = id; 302 break; 303 case SCTP_AUTH_KEY: 304 ((struct sctp_authkey *)arg)->sca_assoc_id = id; 305 break; 306 case SCTP_AUTH_ACTIVE_KEY: 307 ((struct sctp_authkeyid *)arg)->scact_assoc_id = id; 308 break; 309 case SCTP_DELAYED_SACK: 310 ((struct sctp_sack_info *)arg)->sack_assoc_id = id; 311 break; 312 case SCTP_CONTEXT: 313 ((struct sctp_assoc_value *)arg)->assoc_id = id; 314 break; 315 case SCTP_STATUS: 316 ((struct sctp_status *)arg)->sstat_assoc_id = id; 317 break; 318 case SCTP_GET_PEER_ADDR_INFO: 319 ((struct sctp_paddrinfo *)arg)->spinfo_assoc_id = id; 320 break; 321 case SCTP_PEER_AUTH_CHUNKS: 322 ((struct sctp_authchunks *)arg)->gauth_assoc_id = id; 323 break; 324 case SCTP_LOCAL_AUTH_CHUNKS: 325 ((struct sctp_authchunks *)arg)->gauth_assoc_id = id; 326 break; 327 case SCTP_TIMEOUTS: 328 ((struct sctp_timeouts *)arg)->stimo_assoc_id = id; 329 break; 330 case SCTP_EVENT: 331 ((struct sctp_event *)arg)->se_assoc_id = id; 332 break; 333 case SCTP_DEFAULT_SNDINFO: 334 ((struct sctp_sndinfo *)arg)->snd_assoc_id = id; 335 break; 336 case SCTP_DEFAULT_PRINFO: 337 ((struct sctp_default_prinfo *)arg)->pr_assoc_id = id; 338 break; 339 case SCTP_PEER_ADDR_THLDS: 340 ((struct sctp_paddrthlds *)arg)->spt_assoc_id = id; 341 break; 342 case SCTP_REMOTE_UDP_ENCAPS_PORT: 343 ((struct sctp_udpencaps *)arg)->sue_assoc_id = id; 344 break; 345 case SCTP_ECN_SUPPORTED: 346 ((struct sctp_assoc_value *)arg)->assoc_id = id; 347 break; 348 case SCTP_PR_SUPPORTED: 349 ((struct sctp_assoc_value *)arg)->assoc_id = id; 350 break; 351 case SCTP_AUTH_SUPPORTED: 352 ((struct sctp_assoc_value *)arg)->assoc_id = id; 353 break; 354 case SCTP_ASCONF_SUPPORTED: 355 ((struct sctp_assoc_value *)arg)->assoc_id = id; 356 break; 357 case SCTP_RECONFIG_SUPPORTED: 358 ((struct sctp_assoc_value *)arg)->assoc_id = id; 359 break; 360 case SCTP_NRSACK_SUPPORTED: 361 ((struct sctp_assoc_value *)arg)->assoc_id = id; 362 break; 363 case SCTP_PKTDROP_SUPPORTED: 364 ((struct sctp_assoc_value *)arg)->assoc_id = id; 365 break; 366 case SCTP_MAX_BURST: 367 ((struct sctp_assoc_value *)arg)->assoc_id = id; 368 break; 369 case SCTP_ENABLE_STREAM_RESET: 370 ((struct sctp_assoc_value *)arg)->assoc_id = id; 371 break; 372 case SCTP_PR_STREAM_STATUS: 373 ((struct sctp_prstatus *)arg)->sprstat_assoc_id = id; 374 break; 375 case SCTP_PR_ASSOC_STATUS: 376 ((struct sctp_prstatus *)arg)->sprstat_assoc_id = id; 377 break; 378 case SCTP_MAX_CWND: 379 ((struct sctp_assoc_value *)arg)->assoc_id = id; 380 break; 381 default: 382 break; 383 } 384 return (getsockopt(sd, IPPROTO_SCTP, opt, arg, size)); 385 } 386 387 int 388 sctp_getpaddrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs) 389 { 390 struct sctp_getaddresses *addrs; 391 struct sockaddr *sa; 392 caddr_t lim; 393 socklen_t opt_len; 394 uint32_t size_of_addresses; 395 int cnt; 396 397 if (raddrs == NULL) { 398 errno = EFAULT; 399 return (-1); 400 } 401 /* When calling getsockopt(), the value contains the assoc_id. */ 402 size_of_addresses = (uint32_t)id; 403 opt_len = (socklen_t)sizeof(uint32_t); 404 if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_REMOTE_ADDR_SIZE, 405 &size_of_addresses, &opt_len) != 0) { 406 if (errno == ENOENT) { 407 return (0); 408 } else { 409 return (-1); 410 } 411 } 412 opt_len = (socklen_t)((size_t)size_of_addresses + sizeof(struct sctp_getaddresses)); 413 addrs = calloc(1, (size_t)opt_len); 414 if (addrs == NULL) { 415 errno = ENOMEM; 416 return (-1); 417 } 418 addrs->sget_assoc_id = id; 419 /* Now lets get the array of addresses */ 420 if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_PEER_ADDRESSES, 421 addrs, &opt_len) != 0) { 422 free(addrs); 423 return (-1); 424 } 425 *raddrs = &addrs->addr[0].sa; 426 cnt = 0; 427 sa = &addrs->addr[0].sa; 428 lim = (caddr_t)addrs + opt_len; 429 while (((caddr_t)sa < lim) && (sa->sa_len > 0)) { 430 sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 431 cnt++; 432 } 433 return (cnt); 434 } 435 436 void 437 sctp_freepaddrs(struct sockaddr *addrs) 438 { 439 void *fr_addr; 440 441 /* Take away the hidden association id */ 442 fr_addr = (void *)((caddr_t)addrs - offsetof(struct sctp_getaddresses, addr)); 443 /* Now free it */ 444 free(fr_addr); 445 } 446 447 int 448 sctp_getladdrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs) 449 { 450 struct sctp_getaddresses *addrs; 451 struct sockaddr *sa; 452 caddr_t lim; 453 socklen_t opt_len; 454 uint32_t size_of_addresses; 455 int cnt; 456 457 if (raddrs == NULL) { 458 errno = EFAULT; 459 return (-1); 460 } 461 size_of_addresses = 0; 462 opt_len = (socklen_t)sizeof(uint32_t); 463 if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDR_SIZE, 464 &size_of_addresses, &opt_len) != 0) { 465 return (-1); 466 } 467 opt_len = (socklen_t)((size_t)size_of_addresses + sizeof(struct sctp_getaddresses)); 468 addrs = calloc(1, (size_t)opt_len); 469 if (addrs == NULL) { 470 errno = ENOMEM; 471 return (-1); 472 } 473 addrs->sget_assoc_id = id; 474 /* Now lets get the array of addresses */ 475 if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDRESSES, addrs, 476 &opt_len) != 0) { 477 free(addrs); 478 return (-1); 479 } 480 if (size_of_addresses == 0) { 481 free(addrs); 482 return (0); 483 } 484 *raddrs = &addrs->addr[0].sa; 485 cnt = 0; 486 sa = &addrs->addr[0].sa; 487 lim = (caddr_t)addrs + opt_len; 488 while (((caddr_t)sa < lim) && (sa->sa_len > 0)) { 489 sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 490 cnt++; 491 } 492 return (cnt); 493 } 494 495 void 496 sctp_freeladdrs(struct sockaddr *addrs) 497 { 498 void *fr_addr; 499 500 /* Take away the hidden association id */ 501 fr_addr = (void *)((caddr_t)addrs - offsetof(struct sctp_getaddresses, addr)); 502 /* Now free it */ 503 free(fr_addr); 504 } 505 506 ssize_t 507 sctp_sendmsg(int s, 508 const void *data, 509 size_t len, 510 const struct sockaddr *to, 511 socklen_t tolen, 512 uint32_t ppid, 513 uint32_t flags, 514 uint16_t stream_no, 515 uint32_t timetolive, 516 uint32_t context) 517 { 518 #ifdef SYS_sctp_generic_sendmsg 519 struct sctp_sndrcvinfo sinfo; 520 521 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo)); 522 sinfo.sinfo_ppid = ppid; 523 sinfo.sinfo_flags = flags; 524 sinfo.sinfo_stream = stream_no; 525 sinfo.sinfo_timetolive = timetolive; 526 sinfo.sinfo_context = context; 527 sinfo.sinfo_assoc_id = 0; 528 return (syscall(SYS_sctp_generic_sendmsg, s, 529 data, len, to, tolen, &sinfo, 0)); 530 #else 531 struct msghdr msg; 532 struct sctp_sndrcvinfo *sinfo; 533 struct iovec iov; 534 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; 535 struct cmsghdr *cmsg; 536 struct sockaddr *who = NULL; 537 union { 538 struct sockaddr_in in; 539 struct sockaddr_in6 in6; 540 } addr; 541 542 if ((tolen > 0) && 543 ((to == NULL) || (tolen < sizeof(struct sockaddr)))) { 544 errno = EINVAL; 545 return (-1); 546 } 547 if ((to != NULL) && (tolen > 0)) { 548 switch (to->sa_family) { 549 case AF_INET: 550 if (tolen != sizeof(struct sockaddr_in)) { 551 errno = EINVAL; 552 return (-1); 553 } 554 if ((to->sa_len > 0) && 555 (to->sa_len != sizeof(struct sockaddr_in))) { 556 errno = EINVAL; 557 return (-1); 558 } 559 memcpy(&addr, to, sizeof(struct sockaddr_in)); 560 addr.in.sin_len = sizeof(struct sockaddr_in); 561 break; 562 case AF_INET6: 563 if (tolen != sizeof(struct sockaddr_in6)) { 564 errno = EINVAL; 565 return (-1); 566 } 567 if ((to->sa_len > 0) && 568 (to->sa_len != sizeof(struct sockaddr_in6))) { 569 errno = EINVAL; 570 return (-1); 571 } 572 memcpy(&addr, to, sizeof(struct sockaddr_in6)); 573 addr.in6.sin6_len = sizeof(struct sockaddr_in6); 574 break; 575 default: 576 errno = EAFNOSUPPORT; 577 return (-1); 578 } 579 who = (struct sockaddr *)&addr; 580 } 581 582 iov.iov_base = (char *)data; 583 iov.iov_len = len; 584 585 if (who) { 586 msg.msg_name = (caddr_t)who; 587 msg.msg_namelen = who->sa_len; 588 } else { 589 msg.msg_name = (caddr_t)NULL; 590 msg.msg_namelen = 0; 591 } 592 msg.msg_iov = &iov; 593 msg.msg_iovlen = 1; 594 msg.msg_control = cmsgbuf; 595 msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)); 596 msg.msg_flags = 0; 597 cmsg = (struct cmsghdr *)cmsgbuf; 598 cmsg->cmsg_level = IPPROTO_SCTP; 599 cmsg->cmsg_type = SCTP_SNDRCV; 600 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); 601 sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); 602 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo)); 603 sinfo->sinfo_stream = stream_no; 604 sinfo->sinfo_ssn = 0; 605 sinfo->sinfo_flags = flags; 606 sinfo->sinfo_ppid = ppid; 607 sinfo->sinfo_context = context; 608 sinfo->sinfo_assoc_id = 0; 609 sinfo->sinfo_timetolive = timetolive; 610 return (sendmsg(s, &msg, 0)); 611 #endif 612 } 613 614 615 sctp_assoc_t 616 sctp_getassocid(int sd, struct sockaddr *sa) 617 { 618 struct sctp_paddrinfo sp; 619 socklen_t siz; 620 621 /* First get the assoc id */ 622 siz = sizeof(sp); 623 memset(&sp, 0, sizeof(sp)); 624 memcpy((caddr_t)&sp.spinfo_address, sa, sa->sa_len); 625 if (getsockopt(sd, IPPROTO_SCTP, 626 SCTP_GET_PEER_ADDR_INFO, &sp, &siz) != 0) { 627 /* We depend on the fact that 0 can never be returned */ 628 return ((sctp_assoc_t)0); 629 } 630 return (sp.spinfo_assoc_id); 631 } 632 633 ssize_t 634 sctp_send(int sd, const void *data, size_t len, 635 const struct sctp_sndrcvinfo *sinfo, 636 int flags) 637 { 638 639 #ifdef SYS_sctp_generic_sendmsg 640 struct sockaddr *to = NULL; 641 642 return (syscall(SYS_sctp_generic_sendmsg, sd, 643 data, len, to, 0, sinfo, flags)); 644 #else 645 struct msghdr msg; 646 struct iovec iov; 647 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; 648 struct cmsghdr *cmsg; 649 650 if (sinfo == NULL) { 651 errno = EINVAL; 652 return (-1); 653 } 654 iov.iov_base = (char *)data; 655 iov.iov_len = len; 656 657 msg.msg_name = NULL; 658 msg.msg_namelen = 0; 659 msg.msg_iov = &iov; 660 msg.msg_iovlen = 1; 661 msg.msg_control = cmsgbuf; 662 msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)); 663 msg.msg_flags = 0; 664 cmsg = (struct cmsghdr *)cmsgbuf; 665 cmsg->cmsg_level = IPPROTO_SCTP; 666 cmsg->cmsg_type = SCTP_SNDRCV; 667 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); 668 memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo)); 669 return (sendmsg(sd, &msg, flags)); 670 #endif 671 } 672 673 674 675 ssize_t 676 sctp_sendx(int sd, const void *msg, size_t msg_len, 677 struct sockaddr *addrs, int addrcnt, 678 struct sctp_sndrcvinfo *sinfo, 679 int flags) 680 { 681 struct sctp_sndrcvinfo __sinfo; 682 ssize_t ret; 683 int i, cnt, *aa, saved_errno; 684 char *buf; 685 int no_end_cx = 0; 686 size_t len, add_len; 687 struct sockaddr *at; 688 689 if (addrs == NULL) { 690 errno = EINVAL; 691 return (-1); 692 } 693 #ifdef SYS_sctp_generic_sendmsg 694 if (addrcnt == 1) { 695 socklen_t l; 696 ssize_t ret; 697 698 /* 699 * Quick way, we don't need to do a connectx so lets use the 700 * syscall directly. 701 */ 702 l = addrs->sa_len; 703 ret = syscall(SYS_sctp_generic_sendmsg, sd, 704 msg, msg_len, addrs, l, sinfo, flags); 705 if ((ret >= 0) && (sinfo != NULL)) { 706 sinfo->sinfo_assoc_id = sctp_getassocid(sd, addrs); 707 } 708 return (ret); 709 } 710 #endif 711 712 len = sizeof(int); 713 at = addrs; 714 cnt = 0; 715 /* validate all the addresses and get the size */ 716 for (i = 0; i < addrcnt; i++) { 717 if (at->sa_family == AF_INET) { 718 add_len = sizeof(struct sockaddr_in); 719 } else if (at->sa_family == AF_INET6) { 720 add_len = sizeof(struct sockaddr_in6); 721 } else { 722 errno = EINVAL; 723 return (-1); 724 } 725 len += add_len; 726 at = (struct sockaddr *)((caddr_t)at + add_len); 727 cnt++; 728 } 729 /* do we have any? */ 730 if (cnt == 0) { 731 errno = EINVAL; 732 return (-1); 733 } 734 buf = malloc(len); 735 if (buf == NULL) { 736 errno = ENOMEM; 737 return (-1); 738 } 739 aa = (int *)buf; 740 *aa = cnt; 741 aa++; 742 memcpy((caddr_t)aa, addrs, (size_t)(len - sizeof(int))); 743 ret = setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_DELAYED, (void *)buf, 744 (socklen_t)len); 745 746 free(buf); 747 if (ret != 0) { 748 if (errno == EALREADY) { 749 no_end_cx = 1; 750 goto continue_send; 751 } 752 return (ret); 753 } 754 continue_send: 755 if (sinfo == NULL) { 756 sinfo = &__sinfo; 757 memset(&__sinfo, 0, sizeof(__sinfo)); 758 } 759 sinfo->sinfo_assoc_id = sctp_getassocid(sd, addrs); 760 if (sinfo->sinfo_assoc_id == 0) { 761 (void)setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_COMPLETE, (void *)addrs, 762 (socklen_t)addrs->sa_len); 763 errno = ENOENT; 764 return (-1); 765 } 766 ret = sctp_send(sd, msg, msg_len, sinfo, flags); 767 saved_errno = errno; 768 if (no_end_cx == 0) 769 (void)setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_COMPLETE, (void *)addrs, 770 (socklen_t)addrs->sa_len); 771 772 errno = saved_errno; 773 return (ret); 774 } 775 776 ssize_t 777 sctp_sendmsgx(int sd, 778 const void *msg, 779 size_t len, 780 struct sockaddr *addrs, 781 int addrcnt, 782 uint32_t ppid, 783 uint32_t flags, 784 uint16_t stream_no, 785 uint32_t timetolive, 786 uint32_t context) 787 { 788 struct sctp_sndrcvinfo sinfo; 789 790 memset((void *)&sinfo, 0, sizeof(struct sctp_sndrcvinfo)); 791 sinfo.sinfo_ppid = ppid; 792 sinfo.sinfo_flags = flags; 793 sinfo.sinfo_stream = stream_no; 794 sinfo.sinfo_timetolive = timetolive; 795 sinfo.sinfo_context = context; 796 return (sctp_sendx(sd, msg, len, addrs, addrcnt, &sinfo, 0)); 797 } 798 799 ssize_t 800 sctp_recvmsg(int s, 801 void *dbuf, 802 size_t len, 803 struct sockaddr *from, 804 socklen_t *fromlen, 805 struct sctp_sndrcvinfo *sinfo, 806 int *msg_flags) 807 { 808 #ifdef SYS_sctp_generic_recvmsg 809 struct iovec iov; 810 811 iov.iov_base = dbuf; 812 iov.iov_len = len; 813 return (syscall(SYS_sctp_generic_recvmsg, s, 814 &iov, 1, from, fromlen, sinfo, msg_flags)); 815 #else 816 ssize_t sz; 817 struct msghdr msg; 818 struct iovec iov; 819 char cmsgbuf[SCTP_CONTROL_VEC_SIZE_RCV]; 820 struct cmsghdr *cmsg; 821 822 if (msg_flags == NULL) { 823 errno = EINVAL; 824 return (-1); 825 } 826 iov.iov_base = dbuf; 827 iov.iov_len = len; 828 msg.msg_name = (caddr_t)from; 829 if (fromlen == NULL) 830 msg.msg_namelen = 0; 831 else 832 msg.msg_namelen = *fromlen; 833 msg.msg_iov = &iov; 834 msg.msg_iovlen = 1; 835 msg.msg_control = cmsgbuf; 836 msg.msg_controllen = sizeof(cmsgbuf); 837 msg.msg_flags = 0; 838 sz = recvmsg(s, &msg, *msg_flags); 839 *msg_flags = msg.msg_flags; 840 if (sz <= 0) { 841 return (sz); 842 } 843 if (sinfo) { 844 sinfo->sinfo_assoc_id = 0; 845 } 846 if ((msg.msg_controllen > 0) && (sinfo != NULL)) { 847 /* 848 * parse through and see if we find the sctp_sndrcvinfo (if 849 * the user wants it). 850 */ 851 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 852 if (cmsg->cmsg_level != IPPROTO_SCTP) { 853 continue; 854 } 855 if (cmsg->cmsg_type == SCTP_SNDRCV) { 856 memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo)); 857 break; 858 } 859 if (cmsg->cmsg_type == SCTP_EXTRCV) { 860 /* 861 * Let's hope that the user provided enough 862 * enough memory. At least he asked for more 863 * information. 864 */ 865 memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_extrcvinfo)); 866 break; 867 } 868 } 869 } 870 return (sz); 871 #endif 872 } 873 874 ssize_t 875 sctp_recvv(int sd, 876 const struct iovec *iov, 877 int iovlen, 878 struct sockaddr *from, 879 socklen_t *fromlen, 880 void *info, 881 socklen_t *infolen, 882 unsigned int *infotype, 883 int *flags) 884 { 885 char cmsgbuf[SCTP_CONTROL_VEC_SIZE_RCV]; 886 struct msghdr msg; 887 struct cmsghdr *cmsg; 888 ssize_t ret; 889 struct sctp_rcvinfo *rcvinfo; 890 struct sctp_nxtinfo *nxtinfo; 891 892 if (((info != NULL) && (infolen == NULL)) || 893 ((info == NULL) && (infolen != NULL) && (*infolen != 0)) || 894 ((info != NULL) && (infotype == NULL))) { 895 errno = EINVAL; 896 return (-1); 897 } 898 if (infotype) { 899 *infotype = SCTP_RECVV_NOINFO; 900 } 901 msg.msg_name = from; 902 if (fromlen == NULL) { 903 msg.msg_namelen = 0; 904 } else { 905 msg.msg_namelen = *fromlen; 906 } 907 msg.msg_iov = (struct iovec *)iov; 908 msg.msg_iovlen = iovlen; 909 msg.msg_control = cmsgbuf; 910 msg.msg_controllen = sizeof(cmsgbuf); 911 msg.msg_flags = 0; 912 ret = recvmsg(sd, &msg, *flags); 913 *flags = msg.msg_flags; 914 if ((ret > 0) && 915 (msg.msg_controllen > 0) && 916 (infotype != NULL) && 917 (infolen != NULL) && 918 (*infolen > 0)) { 919 rcvinfo = NULL; 920 nxtinfo = NULL; 921 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 922 if (cmsg->cmsg_level != IPPROTO_SCTP) { 923 continue; 924 } 925 if (cmsg->cmsg_type == SCTP_RCVINFO) { 926 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg); 927 if (nxtinfo != NULL) { 928 break; 929 } else { 930 continue; 931 } 932 } 933 if (cmsg->cmsg_type == SCTP_NXTINFO) { 934 nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmsg); 935 if (rcvinfo != NULL) { 936 break; 937 } else { 938 continue; 939 } 940 } 941 } 942 if (rcvinfo != NULL) { 943 if ((nxtinfo != NULL) && (*infolen >= sizeof(struct sctp_recvv_rn))) { 944 struct sctp_recvv_rn *rn_info; 945 946 rn_info = (struct sctp_recvv_rn *)info; 947 rn_info->recvv_rcvinfo = *rcvinfo; 948 rn_info->recvv_nxtinfo = *nxtinfo; 949 *infolen = (socklen_t)sizeof(struct sctp_recvv_rn); 950 *infotype = SCTP_RECVV_RN; 951 } else if (*infolen >= sizeof(struct sctp_rcvinfo)) { 952 memcpy(info, rcvinfo, sizeof(struct sctp_rcvinfo)); 953 *infolen = (socklen_t)sizeof(struct sctp_rcvinfo); 954 *infotype = SCTP_RECVV_RCVINFO; 955 } 956 } else if (nxtinfo != NULL) { 957 if (*infolen >= sizeof(struct sctp_nxtinfo)) { 958 memcpy(info, nxtinfo, sizeof(struct sctp_nxtinfo)); 959 *infolen = (socklen_t)sizeof(struct sctp_nxtinfo); 960 *infotype = SCTP_RECVV_NXTINFO; 961 } 962 } 963 } 964 return (ret); 965 } 966 967 ssize_t 968 sctp_sendv(int sd, 969 const struct iovec *iov, int iovcnt, 970 struct sockaddr *addrs, int addrcnt, 971 void *info, socklen_t infolen, unsigned int infotype, 972 int flags) 973 { 974 ssize_t ret; 975 int i; 976 socklen_t addr_len; 977 struct msghdr msg; 978 in_port_t port; 979 struct sctp_sendv_spa *spa_info; 980 struct cmsghdr *cmsg; 981 char *cmsgbuf; 982 struct sockaddr *addr; 983 struct sockaddr_in *addr_in; 984 struct sockaddr_in6 *addr_in6; 985 sctp_assoc_t *assoc_id; 986 987 if ((addrcnt < 0) || 988 (iovcnt < 0) || 989 ((addrs == NULL) && (addrcnt > 0)) || 990 ((addrs != NULL) && (addrcnt == 0)) || 991 ((iov == NULL) && (iovcnt > 0)) || 992 ((iov != NULL) && (iovcnt == 0))) { 993 errno = EINVAL; 994 return (-1); 995 } 996 cmsgbuf = malloc(CMSG_SPACE(sizeof(struct sctp_sndinfo)) + 997 CMSG_SPACE(sizeof(struct sctp_prinfo)) + 998 CMSG_SPACE(sizeof(struct sctp_authinfo)) + 999 (size_t)addrcnt * CMSG_SPACE(sizeof(struct in6_addr))); 1000 if (cmsgbuf == NULL) { 1001 errno = ENOMEM; 1002 return (-1); 1003 } 1004 assoc_id = NULL; 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 assoc_id = &(((struct sctp_sndinfo *)info)->snd_assoc_id); 1029 break; 1030 case SCTP_SENDV_PRINFO: 1031 if ((info == NULL) || (infolen < sizeof(struct sctp_prinfo))) { 1032 free(cmsgbuf); 1033 errno = EINVAL; 1034 return (-1); 1035 } 1036 cmsg->cmsg_level = IPPROTO_SCTP; 1037 cmsg->cmsg_type = SCTP_PRINFO; 1038 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo)); 1039 memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_prinfo)); 1040 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo)); 1041 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo))); 1042 break; 1043 case SCTP_SENDV_AUTHINFO: 1044 if ((info == NULL) || (infolen < sizeof(struct sctp_authinfo))) { 1045 free(cmsgbuf); 1046 errno = EINVAL; 1047 return (-1); 1048 } 1049 cmsg->cmsg_level = IPPROTO_SCTP; 1050 cmsg->cmsg_type = SCTP_AUTHINFO; 1051 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo)); 1052 memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_authinfo)); 1053 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo)); 1054 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo))); 1055 break; 1056 case SCTP_SENDV_SPA: 1057 if ((info == NULL) || (infolen < sizeof(struct sctp_sendv_spa))) { 1058 free(cmsgbuf); 1059 errno = EINVAL; 1060 return (-1); 1061 } 1062 spa_info = (struct sctp_sendv_spa *)info; 1063 if (spa_info->sendv_flags & SCTP_SEND_SNDINFO_VALID) { 1064 cmsg->cmsg_level = IPPROTO_SCTP; 1065 cmsg->cmsg_type = SCTP_SNDINFO; 1066 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo)); 1067 memcpy(CMSG_DATA(cmsg), &spa_info->sendv_sndinfo, sizeof(struct sctp_sndinfo)); 1068 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo)); 1069 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo))); 1070 assoc_id = &(spa_info->sendv_sndinfo.snd_assoc_id); 1071 } 1072 if (spa_info->sendv_flags & SCTP_SEND_PRINFO_VALID) { 1073 cmsg->cmsg_level = IPPROTO_SCTP; 1074 cmsg->cmsg_type = SCTP_PRINFO; 1075 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo)); 1076 memcpy(CMSG_DATA(cmsg), &spa_info->sendv_prinfo, sizeof(struct sctp_prinfo)); 1077 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo)); 1078 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo))); 1079 } 1080 if (spa_info->sendv_flags & SCTP_SEND_AUTHINFO_VALID) { 1081 cmsg->cmsg_level = IPPROTO_SCTP; 1082 cmsg->cmsg_type = SCTP_AUTHINFO; 1083 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo)); 1084 memcpy(CMSG_DATA(cmsg), &spa_info->sendv_authinfo, sizeof(struct sctp_authinfo)); 1085 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo)); 1086 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo))); 1087 } 1088 break; 1089 default: 1090 free(cmsgbuf); 1091 errno = EINVAL; 1092 return (-1); 1093 } 1094 addr = addrs; 1095 msg.msg_name = NULL; 1096 msg.msg_namelen = 0; 1097 1098 for (i = 0; i < addrcnt; i++) { 1099 switch (addr->sa_family) { 1100 case AF_INET: 1101 addr_len = (socklen_t)sizeof(struct sockaddr_in); 1102 addr_in = (struct sockaddr_in *)addr; 1103 if (addr_in->sin_len != addr_len) { 1104 free(cmsgbuf); 1105 errno = EINVAL; 1106 return (-1); 1107 } 1108 if (i == 0) { 1109 port = addr_in->sin_port; 1110 } else { 1111 if (port == addr_in->sin_port) { 1112 cmsg->cmsg_level = IPPROTO_SCTP; 1113 cmsg->cmsg_type = SCTP_DSTADDRV4; 1114 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); 1115 memcpy(CMSG_DATA(cmsg), &addr_in->sin_addr, sizeof(struct in_addr)); 1116 msg.msg_controllen += CMSG_SPACE(sizeof(struct in_addr)); 1117 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in_addr))); 1118 } else { 1119 free(cmsgbuf); 1120 errno = EINVAL; 1121 return (-1); 1122 } 1123 } 1124 break; 1125 case AF_INET6: 1126 addr_len = (socklen_t)sizeof(struct sockaddr_in6); 1127 addr_in6 = (struct sockaddr_in6 *)addr; 1128 if (addr_in6->sin6_len != addr_len) { 1129 free(cmsgbuf); 1130 errno = EINVAL; 1131 return (-1); 1132 } 1133 if (i == 0) { 1134 port = addr_in6->sin6_port; 1135 } else { 1136 if (port == addr_in6->sin6_port) { 1137 cmsg->cmsg_level = IPPROTO_SCTP; 1138 cmsg->cmsg_type = SCTP_DSTADDRV6; 1139 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_addr)); 1140 memcpy(CMSG_DATA(cmsg), &addr_in6->sin6_addr, sizeof(struct in6_addr)); 1141 msg.msg_controllen += CMSG_SPACE(sizeof(struct in6_addr)); 1142 cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in6_addr))); 1143 } else { 1144 free(cmsgbuf); 1145 errno = EINVAL; 1146 return (-1); 1147 } 1148 } 1149 break; 1150 default: 1151 free(cmsgbuf); 1152 errno = EINVAL; 1153 return (-1); 1154 } 1155 if (i == 0) { 1156 msg.msg_name = addr; 1157 msg.msg_namelen = addr_len; 1158 } 1159 addr = (struct sockaddr *)((caddr_t)addr + addr_len); 1160 } 1161 if (msg.msg_controllen == 0) { 1162 msg.msg_control = NULL; 1163 } 1164 msg.msg_iov = (struct iovec *)iov; 1165 msg.msg_iovlen = iovcnt; 1166 msg.msg_flags = 0; 1167 ret = sendmsg(sd, &msg, flags); 1168 free(cmsgbuf); 1169 if ((ret >= 0) && (addrs != NULL) && (assoc_id != NULL)) { 1170 *assoc_id = sctp_getassocid(sd, addrs); 1171 } 1172 return (ret); 1173 } 1174 1175 1176 #if !defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT) 1177 1178 int 1179 sctp_peeloff(int sd, sctp_assoc_t assoc_id) 1180 { 1181 /* NOT supported, return invalid sd */ 1182 errno = ENOTSUP; 1183 return (-1); 1184 } 1185 1186 #endif 1187 #if defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT) 1188 int 1189 sctp_peeloff(int sd, sctp_assoc_t assoc_id) 1190 { 1191 return (syscall(SYS_sctp_peeloff, sd, assoc_id)); 1192 } 1193 1194 #endif 1195 1196 #undef SCTP_CONTROL_VEC_SIZE_RCV 1197