148f65f00SMichael Tuexen /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 448f65f00SMichael Tuexen * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 543dc9e2fSMichael Tuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 643dc9e2fSMichael Tuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 7d6dda9b2SRandall Stewart * 8d6dda9b2SRandall Stewart * Redistribution and use in source and binary forms, with or without 948f65f00SMichael Tuexen * modification, are permitted provided that the following conditions are met: 10d6dda9b2SRandall Stewart * 1148f65f00SMichael Tuexen * a) Redistributions of source code must retain the above copyright notice, 1248f65f00SMichael Tuexen * this list of conditions and the following disclaimer. 1348f65f00SMichael Tuexen * 1448f65f00SMichael Tuexen * b) Redistributions in binary form must reproduce the above copyright 1548f65f00SMichael Tuexen * notice, this list of conditions and the following disclaimer in 1648f65f00SMichael Tuexen * the documentation and/or other materials provided with the distribution. 1748f65f00SMichael Tuexen * 1848f65f00SMichael Tuexen * c) Neither the name of Cisco Systems, Inc. nor the names of its 1948f65f00SMichael Tuexen * contributors may be used to endorse or promote products derived 2048f65f00SMichael Tuexen * from this software without specific prior written permission. 2148f65f00SMichael Tuexen * 2248f65f00SMichael Tuexen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2348f65f00SMichael Tuexen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 2448f65f00SMichael Tuexen * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2548f65f00SMichael Tuexen * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 2648f65f00SMichael Tuexen * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2748f65f00SMichael Tuexen * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2848f65f00SMichael Tuexen * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2948f65f00SMichael Tuexen * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3048f65f00SMichael Tuexen * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3148f65f00SMichael Tuexen * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 3248f65f00SMichael Tuexen * THE POSSIBILITY OF SUCH DAMAGE. 33d6dda9b2SRandall Stewart */ 3448f65f00SMichael Tuexen 35d6dda9b2SRandall Stewart #include <sys/cdefs.h> 36d6dda9b2SRandall Stewart __FBSDID("$FreeBSD$"); 3743dc9e2fSMichael Tuexen 38*ed82c2edSMichael Tuexen #include <stdbool.h> 39d6dda9b2SRandall Stewart #include <stdio.h> 40d6dda9b2SRandall Stewart #include <string.h> 41d6dda9b2SRandall Stewart #include <errno.h> 42d6dda9b2SRandall Stewart #include <stdlib.h> 43d6dda9b2SRandall Stewart #include <unistd.h> 44d6dda9b2SRandall Stewart #include <sys/types.h> 45d6dda9b2SRandall Stewart #include <sys/socket.h> 46d6dda9b2SRandall Stewart #include <sys/errno.h> 47d6dda9b2SRandall Stewart #include <sys/syscall.h> 48d6dda9b2SRandall Stewart #include <sys/uio.h> 49d6dda9b2SRandall Stewart #include <netinet/in.h> 50d6dda9b2SRandall Stewart #include <arpa/inet.h> 51d6dda9b2SRandall Stewart #include <netinet/sctp_uio.h> 52d6dda9b2SRandall Stewart #include <netinet/sctp.h> 53d6dda9b2SRandall Stewart 54d6dda9b2SRandall Stewart #ifndef IN6_IS_ADDR_V4MAPPED 55d6dda9b2SRandall Stewart #define IN6_IS_ADDR_V4MAPPED(a) \ 563d36ac98SRebecca Cran ((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) && \ 573d36ac98SRebecca Cran (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) && \ 583d36ac98SRebecca Cran (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == ntohl(0x0000ffff))) 59d6dda9b2SRandall Stewart #endif 60d6dda9b2SRandall Stewart 61d6dda9b2SRandall Stewart #define SCTP_CONTROL_VEC_SIZE_RCV 16384 62d6dda9b2SRandall Stewart 63d6dda9b2SRandall Stewart 64d6dda9b2SRandall Stewart static void 65d6dda9b2SRandall Stewart in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 66d6dda9b2SRandall Stewart { 67d6dda9b2SRandall Stewart bzero(sin, sizeof(*sin)); 68d6dda9b2SRandall Stewart sin->sin_len = sizeof(struct sockaddr_in); 69d6dda9b2SRandall Stewart sin->sin_family = AF_INET; 70d6dda9b2SRandall Stewart sin->sin_port = sin6->sin6_port; 71d6dda9b2SRandall Stewart sin->sin_addr.s_addr = sin6->sin6_addr.__u6_addr.__u6_addr32[3]; 72d6dda9b2SRandall Stewart } 73d6dda9b2SRandall Stewart 74d6dda9b2SRandall Stewart int 75d6dda9b2SRandall Stewart sctp_getaddrlen(sa_family_t family) 76d6dda9b2SRandall Stewart { 77e2e7c62eSMichael Tuexen int ret, sd; 78d6dda9b2SRandall Stewart socklen_t siz; 79d6dda9b2SRandall Stewart struct sctp_assoc_value av; 80d6dda9b2SRandall Stewart 81d6dda9b2SRandall Stewart av.assoc_value = family; 82d6dda9b2SRandall Stewart siz = sizeof(av); 83d6dda9b2SRandall Stewart #if defined(AF_INET) 84d6dda9b2SRandall Stewart sd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); 85d6dda9b2SRandall Stewart #elif defined(AF_INET6) 86d6dda9b2SRandall Stewart sd = socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP); 87e2e7c62eSMichael Tuexen #else 88e2e7c62eSMichael Tuexen sd = -1; 89d6dda9b2SRandall Stewart #endif 90d6dda9b2SRandall Stewart if (sd == -1) { 91a593094eSRandall Stewart return (-1); 92d6dda9b2SRandall Stewart } 93e2e7c62eSMichael Tuexen ret = getsockopt(sd, IPPROTO_SCTP, SCTP_GET_ADDR_LEN, &av, &siz); 94d6dda9b2SRandall Stewart close(sd); 95e2e7c62eSMichael Tuexen if (ret == 0) { 96d6dda9b2SRandall Stewart return ((int)av.assoc_value); 97d6dda9b2SRandall Stewart } else { 98a593094eSRandall Stewart return (-1); 99d6dda9b2SRandall Stewart } 100d6dda9b2SRandall Stewart } 101d6dda9b2SRandall Stewart 102d6dda9b2SRandall Stewart int 1032c356be2SRandall Stewart sctp_connectx(int sd, const struct sockaddr *addrs, int addrcnt, 1042c356be2SRandall Stewart sctp_assoc_t *id) 105d6dda9b2SRandall Stewart { 1067f15a8dfSMichael Tuexen char *buf; 10710e6d832SMichael Tuexen int i, ret, *aa; 108d6dda9b2SRandall Stewart char *cpto; 109d6dda9b2SRandall Stewart const struct sockaddr *at; 11010e6d832SMichael Tuexen size_t len; 111d6dda9b2SRandall Stewart 1122c356be2SRandall Stewart /* validate the address count and list */ 1132c356be2SRandall Stewart if ((addrs == NULL) || (addrcnt <= 0)) { 1142c356be2SRandall Stewart errno = EINVAL; 1152c356be2SRandall Stewart return (-1); 1162c356be2SRandall Stewart } 1177f15a8dfSMichael Tuexen if ((buf = malloc(sizeof(int) + (size_t)addrcnt * sizeof(struct sockaddr_in6))) == NULL) { 1187f15a8dfSMichael Tuexen errno = E2BIG; 1197f15a8dfSMichael Tuexen return (-1); 1207f15a8dfSMichael Tuexen } 12110e6d832SMichael Tuexen len = sizeof(int); 122d6dda9b2SRandall Stewart at = addrs; 1237f15a8dfSMichael Tuexen cpto = buf + sizeof(int); 124d6dda9b2SRandall Stewart /* validate all the addresses and get the size */ 125d6dda9b2SRandall Stewart for (i = 0; i < addrcnt; i++) { 12693409822SMichael Tuexen switch (at->sa_family) { 12793409822SMichael Tuexen case AF_INET: 12825d63f19SRandall Stewart if (at->sa_len != sizeof(struct sockaddr_in)) { 1297f15a8dfSMichael Tuexen free(buf); 13025d63f19SRandall Stewart errno = EINVAL; 13125d63f19SRandall Stewart return (-1); 13225d63f19SRandall Stewart } 13393409822SMichael Tuexen memcpy(cpto, at, sizeof(struct sockaddr_in)); 13493409822SMichael Tuexen cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in)); 13593409822SMichael Tuexen len += sizeof(struct sockaddr_in); 13693409822SMichael Tuexen break; 13793409822SMichael Tuexen case AF_INET6: 13825d63f19SRandall Stewart if (at->sa_len != sizeof(struct sockaddr_in6)) { 1397f15a8dfSMichael Tuexen free(buf); 14025d63f19SRandall Stewart errno = EINVAL; 14125d63f19SRandall Stewart return (-1); 14225d63f19SRandall Stewart } 143d6dda9b2SRandall Stewart if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)at)->sin6_addr)) { 144d6dda9b2SRandall Stewart in6_sin6_2_sin((struct sockaddr_in *)cpto, (struct sockaddr_in6 *)at); 145d6dda9b2SRandall Stewart cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in)); 146d6dda9b2SRandall Stewart len += sizeof(struct sockaddr_in); 147d6dda9b2SRandall Stewart } else { 14893409822SMichael Tuexen memcpy(cpto, at, sizeof(struct sockaddr_in6)); 14993409822SMichael Tuexen cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in6)); 15093409822SMichael Tuexen len += sizeof(struct sockaddr_in6); 151d6dda9b2SRandall Stewart } 15293409822SMichael Tuexen break; 15393409822SMichael Tuexen default: 1547f15a8dfSMichael Tuexen free(buf); 155d6dda9b2SRandall Stewart errno = EINVAL; 156d6dda9b2SRandall Stewart return (-1); 157d6dda9b2SRandall Stewart } 1587f15a8dfSMichael Tuexen at = (struct sockaddr *)((caddr_t)at + at->sa_len); 159d6dda9b2SRandall Stewart } 160d6dda9b2SRandall Stewart aa = (int *)buf; 1617f15a8dfSMichael Tuexen *aa = addrcnt; 162d6dda9b2SRandall Stewart ret = setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X, (void *)buf, 163d6dda9b2SRandall Stewart (socklen_t)len); 1647f15a8dfSMichael Tuexen if ((ret == 0) && (id != NULL)) { 1657f15a8dfSMichael Tuexen *id = *(sctp_assoc_t *)buf; 16642551e99SRandall Stewart } 16710e6d832SMichael Tuexen free(buf); 168d6dda9b2SRandall Stewart return (ret); 169d6dda9b2SRandall Stewart } 170d6dda9b2SRandall Stewart 171d6dda9b2SRandall Stewart int 172d6dda9b2SRandall Stewart sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, int flags) 173d6dda9b2SRandall Stewart { 174d6dda9b2SRandall Stewart struct sockaddr *sa; 1751b649582SRandall Stewart struct sockaddr_in *sin; 1761b649582SRandall Stewart struct sockaddr_in6 *sin6; 1775dc6a815SMichael Tuexen int i; 178*ed82c2edSMichael Tuexen uint16_t sport; 179*ed82c2edSMichael Tuexen bool fix_port; 180d6dda9b2SRandall Stewart 1812c356be2SRandall Stewart /* validate the flags */ 182d6dda9b2SRandall Stewart if ((flags != SCTP_BINDX_ADD_ADDR) && 183d6dda9b2SRandall Stewart (flags != SCTP_BINDX_REM_ADDR)) { 184d6dda9b2SRandall Stewart errno = EFAULT; 185d6dda9b2SRandall Stewart return (-1); 186d6dda9b2SRandall Stewart } 1872c356be2SRandall Stewart /* validate the address count and list */ 1882c356be2SRandall Stewart if ((addrcnt <= 0) || (addrs == NULL)) { 1892c356be2SRandall Stewart errno = EINVAL; 1902c356be2SRandall Stewart return (-1); 1912c356be2SRandall Stewart } 192*ed82c2edSMichael Tuexen sport = 0; 193*ed82c2edSMichael Tuexen fix_port = false; 1941b649582SRandall Stewart /* First pre-screen the addresses */ 195d6dda9b2SRandall Stewart sa = addrs; 196d6dda9b2SRandall Stewart for (i = 0; i < addrcnt; i++) { 19793409822SMichael Tuexen switch (sa->sa_family) { 19893409822SMichael Tuexen case AF_INET: 19993409822SMichael Tuexen if (sa->sa_len != sizeof(struct sockaddr_in)) { 20093409822SMichael Tuexen errno = EINVAL; 20193409822SMichael Tuexen return (-1); 20293409822SMichael Tuexen } 2031b649582SRandall Stewart sin = (struct sockaddr_in *)sa; 2041b649582SRandall Stewart if (sin->sin_port) { 2051b649582SRandall Stewart /* non-zero port, check or save */ 2061b649582SRandall Stewart if (sport) { 2071b649582SRandall Stewart /* Check against our port */ 2081b649582SRandall Stewart if (sport != sin->sin_port) { 20993409822SMichael Tuexen errno = EINVAL; 21093409822SMichael Tuexen return (-1); 2111b649582SRandall Stewart } 2121b649582SRandall Stewart } else { 2131b649582SRandall Stewart /* save off the port */ 2141b649582SRandall Stewart sport = sin->sin_port; 215*ed82c2edSMichael Tuexen fix_port = (i > 0); 2161b649582SRandall Stewart } 2171b649582SRandall Stewart } 21893409822SMichael Tuexen break; 21993409822SMichael Tuexen case AF_INET6: 22093409822SMichael Tuexen if (sa->sa_len != sizeof(struct sockaddr_in6)) { 22193409822SMichael Tuexen errno = EINVAL; 22293409822SMichael Tuexen return (-1); 22393409822SMichael Tuexen } 2241b649582SRandall Stewart sin6 = (struct sockaddr_in6 *)sa; 2251b649582SRandall Stewart if (sin6->sin6_port) { 2261b649582SRandall Stewart /* non-zero port, check or save */ 2271b649582SRandall Stewart if (sport) { 2281b649582SRandall Stewart /* Check against our port */ 2291b649582SRandall Stewart if (sport != sin6->sin6_port) { 23093409822SMichael Tuexen errno = EINVAL; 23193409822SMichael Tuexen return (-1); 2321b649582SRandall Stewart } 2331b649582SRandall Stewart } else { 2341b649582SRandall Stewart /* save off the port */ 2351b649582SRandall Stewart sport = sin6->sin6_port; 236*ed82c2edSMichael Tuexen fix_port = (i > 0); 2371b649582SRandall Stewart } 2381b649582SRandall Stewart } 23993409822SMichael Tuexen break; 24093409822SMichael Tuexen default: 24193409822SMichael Tuexen /* Invalid address family specified. */ 2421dd0c905SMichael Tuexen errno = EAFNOSUPPORT; 24393409822SMichael Tuexen return (-1); 2441b649582SRandall Stewart } 2455dc6a815SMichael Tuexen sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 2461b649582SRandall Stewart } 24793409822SMichael Tuexen sa = addrs; 24893409822SMichael Tuexen for (i = 0; i < addrcnt; i++) { 2491dd0c905SMichael Tuexen /* 2501dd0c905SMichael Tuexen * Now, if there was a port mentioned, assure that the first 2511dd0c905SMichael Tuexen * address has that port to make sure it fails or succeeds 2521dd0c905SMichael Tuexen * correctly. 2531dd0c905SMichael Tuexen */ 254*ed82c2edSMichael Tuexen if (fix_port) { 255*ed82c2edSMichael Tuexen switch (sa->sa_family) { 2561dd0c905SMichael Tuexen case AF_INET: 257*ed82c2edSMichael Tuexen ((struct sockaddr_in *)sa)->sin_port = sport; 2581dd0c905SMichael Tuexen break; 2591dd0c905SMichael Tuexen case AF_INET6: 260*ed82c2edSMichael Tuexen ((struct sockaddr_in6 *)sa)->sin6_port = sport; 2611dd0c905SMichael Tuexen break; 2621dd0c905SMichael Tuexen } 263*ed82c2edSMichael Tuexen fix_port = false; 2641dd0c905SMichael Tuexen } 265*ed82c2edSMichael Tuexen if (setsockopt(sd, IPPROTO_SCTP, flags, sa, sa->sa_len) != 0) { 266d6dda9b2SRandall Stewart return (-1); 267d6dda9b2SRandall Stewart } 2685dc6a815SMichael Tuexen sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 269d6dda9b2SRandall Stewart } 270d6dda9b2SRandall Stewart return (0); 271d6dda9b2SRandall Stewart } 272d6dda9b2SRandall Stewart 273d6dda9b2SRandall Stewart int 274d6dda9b2SRandall Stewart sctp_opt_info(int sd, sctp_assoc_t id, int opt, void *arg, socklen_t *size) 275d6dda9b2SRandall Stewart { 276d6dda9b2SRandall Stewart if (arg == NULL) { 277602afc03SRandall Stewart errno = EINVAL; 278602afc03SRandall Stewart return (-1); 279d6dda9b2SRandall Stewart } 280b71f5853SMichael Tuexen if ((id == SCTP_CURRENT_ASSOC) || 281b71f5853SMichael Tuexen (id == SCTP_ALL_ASSOC)) { 282b71f5853SMichael Tuexen errno = EINVAL; 283b71f5853SMichael Tuexen return (-1); 284b71f5853SMichael Tuexen } 2851b649582SRandall Stewart switch (opt) { 2861b649582SRandall Stewart case SCTP_RTOINFO: 2871b649582SRandall Stewart ((struct sctp_rtoinfo *)arg)->srto_assoc_id = id; 2881b649582SRandall Stewart break; 2891b649582SRandall Stewart case SCTP_ASSOCINFO: 2901b649582SRandall Stewart ((struct sctp_assocparams *)arg)->sasoc_assoc_id = id; 2911b649582SRandall Stewart break; 2921b649582SRandall Stewart case SCTP_DEFAULT_SEND_PARAM: 2931b649582SRandall Stewart ((struct sctp_assocparams *)arg)->sasoc_assoc_id = id; 2941b649582SRandall Stewart break; 2951b649582SRandall Stewart case SCTP_PRIMARY_ADDR: 2961b649582SRandall Stewart ((struct sctp_setprim *)arg)->ssp_assoc_id = id; 2971b649582SRandall Stewart break; 2981b649582SRandall Stewart case SCTP_PEER_ADDR_PARAMS: 2991b649582SRandall Stewart ((struct sctp_paddrparams *)arg)->spp_assoc_id = id; 3001b649582SRandall Stewart break; 3011b649582SRandall Stewart case SCTP_MAXSEG: 3021b649582SRandall Stewart ((struct sctp_assoc_value *)arg)->assoc_id = id; 3031b649582SRandall Stewart break; 3041b649582SRandall Stewart case SCTP_AUTH_KEY: 3051b649582SRandall Stewart ((struct sctp_authkey *)arg)->sca_assoc_id = id; 3061b649582SRandall Stewart break; 3071b649582SRandall Stewart case SCTP_AUTH_ACTIVE_KEY: 3081b649582SRandall Stewart ((struct sctp_authkeyid *)arg)->scact_assoc_id = id; 3091b649582SRandall Stewart break; 3101b649582SRandall Stewart case SCTP_DELAYED_SACK: 3111b649582SRandall Stewart ((struct sctp_sack_info *)arg)->sack_assoc_id = id; 3121b649582SRandall Stewart break; 3131b649582SRandall Stewart case SCTP_CONTEXT: 3141b649582SRandall Stewart ((struct sctp_assoc_value *)arg)->assoc_id = id; 3151b649582SRandall Stewart break; 3161b649582SRandall Stewart case SCTP_STATUS: 3171b649582SRandall Stewart ((struct sctp_status *)arg)->sstat_assoc_id = id; 3181b649582SRandall Stewart break; 3191b649582SRandall Stewart case SCTP_GET_PEER_ADDR_INFO: 3201b649582SRandall Stewart ((struct sctp_paddrinfo *)arg)->spinfo_assoc_id = id; 3211b649582SRandall Stewart break; 3221b649582SRandall Stewart case SCTP_PEER_AUTH_CHUNKS: 3231b649582SRandall Stewart ((struct sctp_authchunks *)arg)->gauth_assoc_id = id; 3241b649582SRandall Stewart break; 3251b649582SRandall Stewart case SCTP_LOCAL_AUTH_CHUNKS: 3261b649582SRandall Stewart ((struct sctp_authchunks *)arg)->gauth_assoc_id = id; 3271b649582SRandall Stewart break; 32848f65f00SMichael Tuexen case SCTP_TIMEOUTS: 32948f65f00SMichael Tuexen ((struct sctp_timeouts *)arg)->stimo_assoc_id = id; 33048f65f00SMichael Tuexen break; 331e2e7c62eSMichael Tuexen case SCTP_EVENT: 332e2e7c62eSMichael Tuexen ((struct sctp_event *)arg)->se_assoc_id = id; 333e2e7c62eSMichael Tuexen break; 33413aae0bfSMichael Tuexen case SCTP_DEFAULT_SNDINFO: 33513aae0bfSMichael Tuexen ((struct sctp_sndinfo *)arg)->snd_assoc_id = id; 33613aae0bfSMichael Tuexen break; 33713aae0bfSMichael Tuexen case SCTP_DEFAULT_PRINFO: 33813aae0bfSMichael Tuexen ((struct sctp_default_prinfo *)arg)->pr_assoc_id = id; 33913aae0bfSMichael Tuexen break; 340ca85e948SMichael Tuexen case SCTP_PEER_ADDR_THLDS: 341ca85e948SMichael Tuexen ((struct sctp_paddrthlds *)arg)->spt_assoc_id = id; 342ca85e948SMichael Tuexen break; 343c9c58059SMichael Tuexen case SCTP_REMOTE_UDP_ENCAPS_PORT: 344c9c58059SMichael Tuexen ((struct sctp_udpencaps *)arg)->sue_assoc_id = id; 345c9c58059SMichael Tuexen break; 346f342355aSMichael Tuexen case SCTP_ECN_SUPPORTED: 347f342355aSMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 348f342355aSMichael Tuexen break; 349dd973b0eSMichael Tuexen case SCTP_PR_SUPPORTED: 350dd973b0eSMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 351dd973b0eSMichael Tuexen break; 352c79bec9cSMichael Tuexen case SCTP_AUTH_SUPPORTED: 353c79bec9cSMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 354c79bec9cSMichael Tuexen break; 355c79bec9cSMichael Tuexen case SCTP_ASCONF_SUPPORTED: 356c79bec9cSMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 357c79bec9cSMichael Tuexen break; 358317e00efSMichael Tuexen case SCTP_RECONFIG_SUPPORTED: 359317e00efSMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 360317e00efSMichael Tuexen break; 361caea9879SMichael Tuexen case SCTP_NRSACK_SUPPORTED: 362caea9879SMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 363caea9879SMichael Tuexen break; 364cb9b8e6fSMichael Tuexen case SCTP_PKTDROP_SUPPORTED: 365cb9b8e6fSMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 366cb9b8e6fSMichael Tuexen break; 367bb2c20c1SMichael Tuexen case SCTP_MAX_BURST: 368bb2c20c1SMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 369bb2c20c1SMichael Tuexen break; 3707c9b6492SMichael Tuexen case SCTP_ENABLE_STREAM_RESET: 3717c9b6492SMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 3727c9b6492SMichael Tuexen break; 373f0396ad1SMichael Tuexen case SCTP_PR_STREAM_STATUS: 374f0396ad1SMichael Tuexen ((struct sctp_prstatus *)arg)->sprstat_assoc_id = id; 375f0396ad1SMichael Tuexen break; 376f0396ad1SMichael Tuexen case SCTP_PR_ASSOC_STATUS: 377f0396ad1SMichael Tuexen ((struct sctp_prstatus *)arg)->sprstat_assoc_id = id; 378f0396ad1SMichael Tuexen break; 37959b6d5beSMichael Tuexen case SCTP_MAX_CWND: 38059b6d5beSMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 38159b6d5beSMichael Tuexen break; 3821b649582SRandall Stewart default: 3831b649582SRandall Stewart break; 3841b649582SRandall Stewart } 385d6dda9b2SRandall Stewart return (getsockopt(sd, IPPROTO_SCTP, opt, arg, size)); 386d6dda9b2SRandall Stewart } 387d6dda9b2SRandall Stewart 388d6dda9b2SRandall Stewart int 389d6dda9b2SRandall Stewart sctp_getpaddrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs) 390d6dda9b2SRandall Stewart { 391d6dda9b2SRandall Stewart struct sctp_getaddresses *addrs; 392d6dda9b2SRandall Stewart struct sockaddr *sa; 393d6dda9b2SRandall Stewart sctp_assoc_t asoc; 394d6dda9b2SRandall Stewart caddr_t lim; 3955dc6a815SMichael Tuexen socklen_t opt_len; 396d6dda9b2SRandall Stewart int cnt; 397d6dda9b2SRandall Stewart 398d6dda9b2SRandall Stewart if (raddrs == NULL) { 399d6dda9b2SRandall Stewart errno = EFAULT; 400d6dda9b2SRandall Stewart return (-1); 401d6dda9b2SRandall Stewart } 402d6dda9b2SRandall Stewart asoc = id; 4035dc6a815SMichael Tuexen opt_len = (socklen_t)sizeof(sctp_assoc_t); 404d6dda9b2SRandall Stewart if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_REMOTE_ADDR_SIZE, 4055dc6a815SMichael Tuexen &asoc, &opt_len) != 0) { 406d6dda9b2SRandall Stewart return (-1); 407d6dda9b2SRandall Stewart } 408d6dda9b2SRandall Stewart /* size required is returned in 'asoc' */ 4098124c05fSMichael Tuexen opt_len = (socklen_t)((size_t)asoc + sizeof(sctp_assoc_t)); 4105dc6a815SMichael Tuexen addrs = calloc(1, (size_t)opt_len); 411d6dda9b2SRandall Stewart if (addrs == NULL) { 4124ed0ebf6SMichael Tuexen errno = ENOMEM; 413d6dda9b2SRandall Stewart return (-1); 414d6dda9b2SRandall Stewart } 415d6dda9b2SRandall Stewart addrs->sget_assoc_id = id; 416d6dda9b2SRandall Stewart /* Now lets get the array of addresses */ 417d6dda9b2SRandall Stewart if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_PEER_ADDRESSES, 4185dc6a815SMichael Tuexen addrs, &opt_len) != 0) { 419d6dda9b2SRandall Stewart free(addrs); 420d6dda9b2SRandall Stewart return (-1); 421d6dda9b2SRandall Stewart } 4225dc6a815SMichael Tuexen *raddrs = (struct sockaddr *)&addrs->addr[0]; 423d6dda9b2SRandall Stewart cnt = 0; 424d6dda9b2SRandall Stewart sa = (struct sockaddr *)&addrs->addr[0]; 4255dc6a815SMichael Tuexen lim = (caddr_t)addrs + opt_len; 426d6dda9b2SRandall Stewart while (((caddr_t)sa < lim) && (sa->sa_len > 0)) { 427d6dda9b2SRandall Stewart sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 428d6dda9b2SRandall Stewart cnt++; 429d6dda9b2SRandall Stewart } 430d6dda9b2SRandall Stewart return (cnt); 431d6dda9b2SRandall Stewart } 432d6dda9b2SRandall Stewart 433d6dda9b2SRandall Stewart void 434d6dda9b2SRandall Stewart sctp_freepaddrs(struct sockaddr *addrs) 435d6dda9b2SRandall Stewart { 436d6dda9b2SRandall Stewart void *fr_addr; 437d6dda9b2SRandall Stewart 4387f15a8dfSMichael Tuexen /* Take away the hidden association id */ 439d6dda9b2SRandall Stewart fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t)); 440d6dda9b2SRandall Stewart /* Now free it */ 441d6dda9b2SRandall Stewart free(fr_addr); 442d6dda9b2SRandall Stewart } 443d6dda9b2SRandall Stewart 444d6dda9b2SRandall Stewart int 445d6dda9b2SRandall Stewart sctp_getladdrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs) 446d6dda9b2SRandall Stewart { 447d6dda9b2SRandall Stewart struct sctp_getaddresses *addrs; 448d6dda9b2SRandall Stewart caddr_t lim; 449d6dda9b2SRandall Stewart struct sockaddr *sa; 4505dc6a815SMichael Tuexen size_t size_of_addresses; 4515dc6a815SMichael Tuexen socklen_t opt_len; 452d6dda9b2SRandall Stewart int cnt; 453d6dda9b2SRandall Stewart 454d6dda9b2SRandall Stewart if (raddrs == NULL) { 455d6dda9b2SRandall Stewart errno = EFAULT; 456d6dda9b2SRandall Stewart return (-1); 457d6dda9b2SRandall Stewart } 458d6dda9b2SRandall Stewart size_of_addresses = 0; 4595dc6a815SMichael Tuexen opt_len = (socklen_t)sizeof(int); 460d6dda9b2SRandall Stewart if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDR_SIZE, 4615dc6a815SMichael Tuexen &size_of_addresses, &opt_len) != 0) { 462d6dda9b2SRandall Stewart errno = ENOMEM; 463d6dda9b2SRandall Stewart return (-1); 464d6dda9b2SRandall Stewart } 465d6dda9b2SRandall Stewart if (size_of_addresses == 0) { 466d6dda9b2SRandall Stewart errno = ENOTCONN; 467d6dda9b2SRandall Stewart return (-1); 468d6dda9b2SRandall Stewart } 4698124c05fSMichael Tuexen opt_len = (socklen_t)(size_of_addresses + sizeof(sctp_assoc_t)); 4705dc6a815SMichael Tuexen addrs = calloc(1, (size_t)opt_len); 471d6dda9b2SRandall Stewart if (addrs == NULL) { 472d6dda9b2SRandall Stewart errno = ENOMEM; 473d6dda9b2SRandall Stewart return (-1); 474d6dda9b2SRandall Stewart } 475d6dda9b2SRandall Stewart addrs->sget_assoc_id = id; 476d6dda9b2SRandall Stewart /* Now lets get the array of addresses */ 477d6dda9b2SRandall Stewart if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDRESSES, addrs, 4785dc6a815SMichael Tuexen &opt_len) != 0) { 479d6dda9b2SRandall Stewart free(addrs); 480d6dda9b2SRandall Stewart errno = ENOMEM; 481d6dda9b2SRandall Stewart return (-1); 482d6dda9b2SRandall Stewart } 4835dc6a815SMichael Tuexen *raddrs = (struct sockaddr *)&addrs->addr[0]; 484d6dda9b2SRandall Stewart cnt = 0; 485d6dda9b2SRandall Stewart sa = (struct sockaddr *)&addrs->addr[0]; 4865dc6a815SMichael Tuexen lim = (caddr_t)addrs + opt_len; 487d6dda9b2SRandall Stewart while (((caddr_t)sa < lim) && (sa->sa_len > 0)) { 488d6dda9b2SRandall Stewart sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 489d6dda9b2SRandall Stewart cnt++; 490d6dda9b2SRandall Stewart } 491d6dda9b2SRandall Stewart return (cnt); 492d6dda9b2SRandall Stewart } 493d6dda9b2SRandall Stewart 494d6dda9b2SRandall Stewart void 495d6dda9b2SRandall Stewart sctp_freeladdrs(struct sockaddr *addrs) 496d6dda9b2SRandall Stewart { 497d6dda9b2SRandall Stewart void *fr_addr; 498d6dda9b2SRandall Stewart 4997f15a8dfSMichael Tuexen /* Take away the hidden association id */ 500d6dda9b2SRandall Stewart fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t)); 501d6dda9b2SRandall Stewart /* Now free it */ 502d6dda9b2SRandall Stewart free(fr_addr); 503d6dda9b2SRandall Stewart } 504d6dda9b2SRandall Stewart 505d6dda9b2SRandall Stewart ssize_t 506d6dda9b2SRandall Stewart sctp_sendmsg(int s, 507d6dda9b2SRandall Stewart const void *data, 508d6dda9b2SRandall Stewart size_t len, 509d6dda9b2SRandall Stewart const struct sockaddr *to, 510002b1f0bSRandall Stewart socklen_t tolen, 5113d36ac98SRebecca Cran uint32_t ppid, 5123d36ac98SRebecca Cran uint32_t flags, 5133d36ac98SRebecca Cran uint16_t stream_no, 5143d36ac98SRebecca Cran uint32_t timetolive, 5153d36ac98SRebecca Cran uint32_t context) 516d6dda9b2SRandall Stewart { 517d6dda9b2SRandall Stewart #ifdef SYS_sctp_generic_sendmsg 518d6dda9b2SRandall Stewart struct sctp_sndrcvinfo sinfo; 519d6dda9b2SRandall Stewart 520539bb45aSMichael Tuexen memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo)); 521d6dda9b2SRandall Stewart sinfo.sinfo_ppid = ppid; 522d6dda9b2SRandall Stewart sinfo.sinfo_flags = flags; 523d6dda9b2SRandall Stewart sinfo.sinfo_stream = stream_no; 524d6dda9b2SRandall Stewart sinfo.sinfo_timetolive = timetolive; 525d6dda9b2SRandall Stewart sinfo.sinfo_context = context; 526d6dda9b2SRandall Stewart sinfo.sinfo_assoc_id = 0; 527d6dda9b2SRandall Stewart return (syscall(SYS_sctp_generic_sendmsg, s, 528d6dda9b2SRandall Stewart data, len, to, tolen, &sinfo, 0)); 529d6dda9b2SRandall Stewart #else 530d6dda9b2SRandall Stewart struct msghdr msg; 5317f15a8dfSMichael Tuexen struct sctp_sndrcvinfo *sinfo; 53248f65f00SMichael Tuexen struct iovec iov; 5337f15a8dfSMichael Tuexen char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; 534d6dda9b2SRandall Stewart struct cmsghdr *cmsg; 535d6dda9b2SRandall Stewart struct sockaddr *who = NULL; 536d6dda9b2SRandall Stewart union { 537d6dda9b2SRandall Stewart struct sockaddr_in in; 538d6dda9b2SRandall Stewart struct sockaddr_in6 in6; 539d6dda9b2SRandall Stewart } addr; 540d6dda9b2SRandall Stewart 54148f65f00SMichael Tuexen if ((tolen > 0) && 54248f65f00SMichael Tuexen ((to == NULL) || (tolen < sizeof(struct sockaddr)))) { 543002b1f0bSRandall Stewart errno = EINVAL; 5444224e03aSMichael Tuexen return (-1); 545002b1f0bSRandall Stewart } 5467f15a8dfSMichael Tuexen if ((to != NULL) && (tolen > 0)) { 5477f15a8dfSMichael Tuexen switch (to->sa_family) { 5487f15a8dfSMichael Tuexen case AF_INET: 549002b1f0bSRandall Stewart if (tolen != sizeof(struct sockaddr_in)) { 550002b1f0bSRandall Stewart errno = EINVAL; 5514224e03aSMichael Tuexen return (-1); 552002b1f0bSRandall Stewart } 55348f65f00SMichael Tuexen if ((to->sa_len > 0) && 55448f65f00SMichael Tuexen (to->sa_len != sizeof(struct sockaddr_in))) { 555002b1f0bSRandall Stewart errno = EINVAL; 5564224e03aSMichael Tuexen return (-1); 557002b1f0bSRandall Stewart } 558d6dda9b2SRandall Stewart memcpy(&addr, to, sizeof(struct sockaddr_in)); 559d6dda9b2SRandall Stewart addr.in.sin_len = sizeof(struct sockaddr_in); 5607f15a8dfSMichael Tuexen break; 5617f15a8dfSMichael Tuexen case AF_INET6: 562002b1f0bSRandall Stewart if (tolen != sizeof(struct sockaddr_in6)) { 563002b1f0bSRandall Stewart errno = EINVAL; 5644224e03aSMichael Tuexen return (-1); 565002b1f0bSRandall Stewart } 56648f65f00SMichael Tuexen if ((to->sa_len > 0) && 56748f65f00SMichael Tuexen (to->sa_len != sizeof(struct sockaddr_in6))) { 568002b1f0bSRandall Stewart errno = EINVAL; 5694224e03aSMichael Tuexen return (-1); 570002b1f0bSRandall Stewart } 571d6dda9b2SRandall Stewart memcpy(&addr, to, sizeof(struct sockaddr_in6)); 572d6dda9b2SRandall Stewart addr.in6.sin6_len = sizeof(struct sockaddr_in6); 5737f15a8dfSMichael Tuexen break; 5747f15a8dfSMichael Tuexen default: 575002b1f0bSRandall Stewart errno = EAFNOSUPPORT; 5764224e03aSMichael Tuexen return (-1); 577d6dda9b2SRandall Stewart } 578d6dda9b2SRandall Stewart who = (struct sockaddr *)&addr; 579d6dda9b2SRandall Stewart } 580df2fca7bSMichael Tuexen 58148f65f00SMichael Tuexen iov.iov_base = (char *)data; 58248f65f00SMichael Tuexen iov.iov_len = len; 583d6dda9b2SRandall Stewart 584002b1f0bSRandall Stewart if (who) { 585d6dda9b2SRandall Stewart msg.msg_name = (caddr_t)who; 586d6dda9b2SRandall Stewart msg.msg_namelen = who->sa_len; 587d6dda9b2SRandall Stewart } else { 588d6dda9b2SRandall Stewart msg.msg_name = (caddr_t)NULL; 589d6dda9b2SRandall Stewart msg.msg_namelen = 0; 590d6dda9b2SRandall Stewart } 59148f65f00SMichael Tuexen msg.msg_iov = &iov; 592d6dda9b2SRandall Stewart msg.msg_iovlen = 1; 5937f15a8dfSMichael Tuexen msg.msg_control = cmsgbuf; 5947f15a8dfSMichael Tuexen msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)); 5952b8a4d80SMichael Tuexen msg.msg_flags = 0; 5967f15a8dfSMichael Tuexen cmsg = (struct cmsghdr *)cmsgbuf; 597d6dda9b2SRandall Stewart cmsg->cmsg_level = IPPROTO_SCTP; 598d6dda9b2SRandall Stewart cmsg->cmsg_type = SCTP_SNDRCV; 599d6dda9b2SRandall Stewart cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); 6007f15a8dfSMichael Tuexen sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); 601c79bec9cSMichael Tuexen memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo)); 6027f15a8dfSMichael Tuexen sinfo->sinfo_stream = stream_no; 6037f15a8dfSMichael Tuexen sinfo->sinfo_ssn = 0; 6047f15a8dfSMichael Tuexen sinfo->sinfo_flags = flags; 6057f15a8dfSMichael Tuexen sinfo->sinfo_ppid = ppid; 6067f15a8dfSMichael Tuexen sinfo->sinfo_context = context; 6077f15a8dfSMichael Tuexen sinfo->sinfo_assoc_id = 0; 6087f15a8dfSMichael Tuexen sinfo->sinfo_timetolive = timetolive; 6097f15a8dfSMichael Tuexen return (sendmsg(s, &msg, 0)); 610d6dda9b2SRandall Stewart #endif 611d6dda9b2SRandall Stewart } 612d6dda9b2SRandall Stewart 613d6dda9b2SRandall Stewart 614d6dda9b2SRandall Stewart sctp_assoc_t 615d6dda9b2SRandall Stewart sctp_getassocid(int sd, struct sockaddr *sa) 616d6dda9b2SRandall Stewart { 617b54d3a6cSRandall Stewart struct sctp_paddrinfo sp; 618066f54d8SCraig Rodrigues socklen_t siz; 619d6dda9b2SRandall Stewart 620d6dda9b2SRandall Stewart /* First get the assoc id */ 621b54d3a6cSRandall Stewart siz = sizeof(sp); 622d6dda9b2SRandall Stewart memset(&sp, 0, sizeof(sp)); 623b54d3a6cSRandall Stewart memcpy((caddr_t)&sp.spinfo_address, sa, sa->sa_len); 624d6dda9b2SRandall Stewart if (getsockopt(sd, IPPROTO_SCTP, 625b54d3a6cSRandall Stewart SCTP_GET_PEER_ADDR_INFO, &sp, &siz) != 0) { 6267f15a8dfSMichael Tuexen /* We depend on the fact that 0 can never be returned */ 627d6dda9b2SRandall Stewart return ((sctp_assoc_t)0); 628d6dda9b2SRandall Stewart } 629b54d3a6cSRandall Stewart return (sp.spinfo_assoc_id); 630d6dda9b2SRandall Stewart } 631d6dda9b2SRandall Stewart 632d6dda9b2SRandall Stewart ssize_t 633d6dda9b2SRandall Stewart sctp_send(int sd, const void *data, size_t len, 634d6dda9b2SRandall Stewart const struct sctp_sndrcvinfo *sinfo, 635d6dda9b2SRandall Stewart int flags) 636d6dda9b2SRandall Stewart { 637d6dda9b2SRandall Stewart 638d6dda9b2SRandall Stewart #ifdef SYS_sctp_generic_sendmsg 639d6dda9b2SRandall Stewart struct sockaddr *to = NULL; 640d6dda9b2SRandall Stewart 641d6dda9b2SRandall Stewart return (syscall(SYS_sctp_generic_sendmsg, sd, 642d6dda9b2SRandall Stewart data, len, to, 0, sinfo, flags)); 643d6dda9b2SRandall Stewart #else 644d6dda9b2SRandall Stewart struct msghdr msg; 64548f65f00SMichael Tuexen struct iovec iov; 6467f15a8dfSMichael Tuexen char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; 647d6dda9b2SRandall Stewart struct cmsghdr *cmsg; 648d6dda9b2SRandall Stewart 649d6dda9b2SRandall Stewart if (sinfo == NULL) { 650b54d3a6cSRandall Stewart errno = EINVAL; 651b54d3a6cSRandall Stewart return (-1); 652d6dda9b2SRandall Stewart } 65348f65f00SMichael Tuexen iov.iov_base = (char *)data; 65448f65f00SMichael Tuexen iov.iov_len = len; 655d6dda9b2SRandall Stewart 6567f15a8dfSMichael Tuexen msg.msg_name = NULL; 657d6dda9b2SRandall Stewart msg.msg_namelen = 0; 65848f65f00SMichael Tuexen msg.msg_iov = &iov; 659d6dda9b2SRandall Stewart msg.msg_iovlen = 1; 6607f15a8dfSMichael Tuexen msg.msg_control = cmsgbuf; 6617f15a8dfSMichael Tuexen msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)); 6622b8a4d80SMichael Tuexen msg.msg_flags = 0; 6637f15a8dfSMichael Tuexen cmsg = (struct cmsghdr *)cmsgbuf; 664d6dda9b2SRandall Stewart cmsg->cmsg_level = IPPROTO_SCTP; 665d6dda9b2SRandall Stewart cmsg->cmsg_type = SCTP_SNDRCV; 666d6dda9b2SRandall Stewart cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); 6677f15a8dfSMichael Tuexen memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo)); 6687f15a8dfSMichael Tuexen return (sendmsg(sd, &msg, flags)); 669d6dda9b2SRandall Stewart #endif 670d6dda9b2SRandall Stewart } 671d6dda9b2SRandall Stewart 672d6dda9b2SRandall Stewart 673d6dda9b2SRandall Stewart 674d6dda9b2SRandall Stewart ssize_t 675d6dda9b2SRandall Stewart sctp_sendx(int sd, const void *msg, size_t msg_len, 676d6dda9b2SRandall Stewart struct sockaddr *addrs, int addrcnt, 677d6dda9b2SRandall Stewart struct sctp_sndrcvinfo *sinfo, 678d6dda9b2SRandall Stewart int flags) 679d6dda9b2SRandall Stewart { 680335a2d00SRandall Stewart struct sctp_sndrcvinfo __sinfo; 681d6dda9b2SRandall Stewart ssize_t ret; 682d6dda9b2SRandall Stewart int i, cnt, *aa, saved_errno; 683d6dda9b2SRandall Stewart char *buf; 6845dc6a815SMichael Tuexen int no_end_cx = 0; 6855dc6a815SMichael Tuexen size_t len, add_len; 686d6dda9b2SRandall Stewart struct sockaddr *at; 687d6dda9b2SRandall Stewart 6881b649582SRandall Stewart if (addrs == NULL) { 6891b649582SRandall Stewart errno = EINVAL; 6901b649582SRandall Stewart return (-1); 6911b649582SRandall Stewart } 692804cf641SRandall Stewart #ifdef SYS_sctp_generic_sendmsg 69348f65f00SMichael Tuexen if (addrcnt == 1) { 694804cf641SRandall Stewart socklen_t l; 69532d0a77dSMichael Tuexen ssize_t ret; 696804cf641SRandall Stewart 697804cf641SRandall Stewart /* 698804cf641SRandall Stewart * Quick way, we don't need to do a connectx so lets use the 699804cf641SRandall Stewart * syscall directly. 700804cf641SRandall Stewart */ 701804cf641SRandall Stewart l = addrs->sa_len; 70232d0a77dSMichael Tuexen ret = syscall(SYS_sctp_generic_sendmsg, sd, 70332d0a77dSMichael Tuexen msg, msg_len, addrs, l, sinfo, flags); 70432d0a77dSMichael Tuexen if ((ret >= 0) && (sinfo != NULL)) { 70532d0a77dSMichael Tuexen sinfo->sinfo_assoc_id = sctp_getassocid(sd, addrs); 70632d0a77dSMichael Tuexen } 70732d0a77dSMichael Tuexen return (ret); 708804cf641SRandall Stewart } 709804cf641SRandall Stewart #endif 710b54d3a6cSRandall Stewart 711d6dda9b2SRandall Stewart len = sizeof(int); 712d6dda9b2SRandall Stewart at = addrs; 713d6dda9b2SRandall Stewart cnt = 0; 714d6dda9b2SRandall Stewart /* validate all the addresses and get the size */ 715d6dda9b2SRandall Stewart for (i = 0; i < addrcnt; i++) { 716d6dda9b2SRandall Stewart if (at->sa_family == AF_INET) { 717d6dda9b2SRandall Stewart add_len = sizeof(struct sockaddr_in); 718d6dda9b2SRandall Stewart } else if (at->sa_family == AF_INET6) { 719d6dda9b2SRandall Stewart add_len = sizeof(struct sockaddr_in6); 720d6dda9b2SRandall Stewart } else { 721d6dda9b2SRandall Stewart errno = EINVAL; 722d6dda9b2SRandall Stewart return (-1); 723d6dda9b2SRandall Stewart } 724d6dda9b2SRandall Stewart len += add_len; 725d6dda9b2SRandall Stewart at = (struct sockaddr *)((caddr_t)at + add_len); 726d6dda9b2SRandall Stewart cnt++; 727d6dda9b2SRandall Stewart } 728d6dda9b2SRandall Stewart /* do we have any? */ 729d6dda9b2SRandall Stewart if (cnt == 0) { 730d6dda9b2SRandall Stewart errno = EINVAL; 731d6dda9b2SRandall Stewart return (-1); 732d6dda9b2SRandall Stewart } 733d6dda9b2SRandall Stewart buf = malloc(len); 734d6dda9b2SRandall Stewart if (buf == NULL) { 7354ed0ebf6SMichael Tuexen errno = ENOMEM; 736b54d3a6cSRandall Stewart return (-1); 737d6dda9b2SRandall Stewart } 738d6dda9b2SRandall Stewart aa = (int *)buf; 739d6dda9b2SRandall Stewart *aa = cnt; 740d6dda9b2SRandall Stewart aa++; 7415dc6a815SMichael Tuexen memcpy((caddr_t)aa, addrs, (size_t)(len - sizeof(int))); 742d6dda9b2SRandall Stewart ret = setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_DELAYED, (void *)buf, 743d6dda9b2SRandall Stewart (socklen_t)len); 744d6dda9b2SRandall Stewart 745d6dda9b2SRandall Stewart free(buf); 746d6dda9b2SRandall Stewart if (ret != 0) { 747d6dda9b2SRandall Stewart if (errno == EALREADY) { 748ecf4b67aSRebecca Cran no_end_cx = 1; 749d6dda9b2SRandall Stewart goto continue_send; 750d6dda9b2SRandall Stewart } 751d6dda9b2SRandall Stewart return (ret); 752d6dda9b2SRandall Stewart } 753d6dda9b2SRandall Stewart continue_send: 754335a2d00SRandall Stewart if (sinfo == NULL) { 755335a2d00SRandall Stewart sinfo = &__sinfo; 756335a2d00SRandall Stewart memset(&__sinfo, 0, sizeof(__sinfo)); 757335a2d00SRandall Stewart } 758d6dda9b2SRandall Stewart sinfo->sinfo_assoc_id = sctp_getassocid(sd, addrs); 759d6dda9b2SRandall Stewart if (sinfo->sinfo_assoc_id == 0) { 760d6dda9b2SRandall Stewart (void)setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_COMPLETE, (void *)addrs, 761d6dda9b2SRandall Stewart (socklen_t)addrs->sa_len); 762d6dda9b2SRandall Stewart errno = ENOENT; 763d6dda9b2SRandall Stewart return (-1); 764d6dda9b2SRandall Stewart } 765d6dda9b2SRandall Stewart ret = sctp_send(sd, msg, msg_len, sinfo, flags); 766d6dda9b2SRandall Stewart saved_errno = errno; 767d6dda9b2SRandall Stewart if (no_end_cx == 0) 768d6dda9b2SRandall Stewart (void)setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_COMPLETE, (void *)addrs, 769d6dda9b2SRandall Stewart (socklen_t)addrs->sa_len); 770d6dda9b2SRandall Stewart 771d6dda9b2SRandall Stewart errno = saved_errno; 772d6dda9b2SRandall Stewart return (ret); 773d6dda9b2SRandall Stewart } 774d6dda9b2SRandall Stewart 775d6dda9b2SRandall Stewart ssize_t 776d6dda9b2SRandall Stewart sctp_sendmsgx(int sd, 777d6dda9b2SRandall Stewart const void *msg, 778d6dda9b2SRandall Stewart size_t len, 779d6dda9b2SRandall Stewart struct sockaddr *addrs, 780d6dda9b2SRandall Stewart int addrcnt, 7813d36ac98SRebecca Cran uint32_t ppid, 7823d36ac98SRebecca Cran uint32_t flags, 7833d36ac98SRebecca Cran uint16_t stream_no, 7843d36ac98SRebecca Cran uint32_t timetolive, 7853d36ac98SRebecca Cran uint32_t context) 786d6dda9b2SRandall Stewart { 787d6dda9b2SRandall Stewart struct sctp_sndrcvinfo sinfo; 788d6dda9b2SRandall Stewart 789d6dda9b2SRandall Stewart memset((void *)&sinfo, 0, sizeof(struct sctp_sndrcvinfo)); 790d6dda9b2SRandall Stewart sinfo.sinfo_ppid = ppid; 791d6dda9b2SRandall Stewart sinfo.sinfo_flags = flags; 792159efc33SMichael Tuexen sinfo.sinfo_stream = stream_no; 793d6dda9b2SRandall Stewart sinfo.sinfo_timetolive = timetolive; 794d6dda9b2SRandall Stewart sinfo.sinfo_context = context; 7954224e03aSMichael Tuexen return (sctp_sendx(sd, msg, len, addrs, addrcnt, &sinfo, 0)); 796d6dda9b2SRandall Stewart } 797d6dda9b2SRandall Stewart 798d6dda9b2SRandall Stewart ssize_t 799d6dda9b2SRandall Stewart sctp_recvmsg(int s, 800d6dda9b2SRandall Stewart void *dbuf, 801d6dda9b2SRandall Stewart size_t len, 802d6dda9b2SRandall Stewart struct sockaddr *from, 803d6dda9b2SRandall Stewart socklen_t *fromlen, 804d6dda9b2SRandall Stewart struct sctp_sndrcvinfo *sinfo, 805d6dda9b2SRandall Stewart int *msg_flags) 806d6dda9b2SRandall Stewart { 807d6dda9b2SRandall Stewart #ifdef SYS_sctp_generic_recvmsg 80848f65f00SMichael Tuexen struct iovec iov; 809d6dda9b2SRandall Stewart 81048f65f00SMichael Tuexen iov.iov_base = dbuf; 81148f65f00SMichael Tuexen iov.iov_len = len; 812d6dda9b2SRandall Stewart return (syscall(SYS_sctp_generic_recvmsg, s, 81348f65f00SMichael Tuexen &iov, 1, from, fromlen, sinfo, msg_flags)); 814d6dda9b2SRandall Stewart #else 815d6dda9b2SRandall Stewart ssize_t sz; 816d6dda9b2SRandall Stewart struct msghdr msg; 81748f65f00SMichael Tuexen struct iovec iov; 8187f15a8dfSMichael Tuexen char cmsgbuf[SCTP_CONTROL_VEC_SIZE_RCV]; 819d6dda9b2SRandall Stewart struct cmsghdr *cmsg; 820d6dda9b2SRandall Stewart 821d6dda9b2SRandall Stewart if (msg_flags == NULL) { 822d6dda9b2SRandall Stewart errno = EINVAL; 823d6dda9b2SRandall Stewart return (-1); 824d6dda9b2SRandall Stewart } 82548f65f00SMichael Tuexen iov.iov_base = dbuf; 82648f65f00SMichael Tuexen iov.iov_len = len; 827d6dda9b2SRandall Stewart msg.msg_name = (caddr_t)from; 828d6dda9b2SRandall Stewart if (fromlen == NULL) 829d6dda9b2SRandall Stewart msg.msg_namelen = 0; 830d6dda9b2SRandall Stewart else 831d6dda9b2SRandall Stewart msg.msg_namelen = *fromlen; 83248f65f00SMichael Tuexen msg.msg_iov = &iov; 833d6dda9b2SRandall Stewart msg.msg_iovlen = 1; 8347f15a8dfSMichael Tuexen msg.msg_control = cmsgbuf; 8357f15a8dfSMichael Tuexen msg.msg_controllen = sizeof(cmsgbuf); 8362b8a4d80SMichael Tuexen msg.msg_flags = 0; 8372c356be2SRandall Stewart sz = recvmsg(s, &msg, *msg_flags); 83848f65f00SMichael Tuexen *msg_flags = msg.msg_flags; 83948f65f00SMichael Tuexen if (sz <= 0) { 840d6dda9b2SRandall Stewart return (sz); 84148f65f00SMichael Tuexen } 84248f65f00SMichael Tuexen if (sinfo) { 843d6dda9b2SRandall Stewart sinfo->sinfo_assoc_id = 0; 84448f65f00SMichael Tuexen } 8457f15a8dfSMichael Tuexen if ((msg.msg_controllen > 0) && (sinfo != NULL)) { 846d6dda9b2SRandall Stewart /* 847d6dda9b2SRandall Stewart * parse through and see if we find the sctp_sndrcvinfo (if 848d6dda9b2SRandall Stewart * the user wants it). 849d6dda9b2SRandall Stewart */ 8507f15a8dfSMichael Tuexen for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 8517f15a8dfSMichael Tuexen if (cmsg->cmsg_level != IPPROTO_SCTP) { 8527f15a8dfSMichael Tuexen continue; 853d6dda9b2SRandall Stewart } 854d6dda9b2SRandall Stewart if (cmsg->cmsg_type == SCTP_SNDRCV) { 8557f15a8dfSMichael Tuexen memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo)); 856d6dda9b2SRandall Stewart break; 8577f15a8dfSMichael Tuexen } 8587f15a8dfSMichael Tuexen if (cmsg->cmsg_type == SCTP_EXTRCV) { 859d6dda9b2SRandall Stewart /* 8607f15a8dfSMichael Tuexen * Let's hope that the user provided enough 8617f15a8dfSMichael Tuexen * enough memory. At least he asked for more 8627f15a8dfSMichael Tuexen * information. 863d6dda9b2SRandall Stewart */ 8647f15a8dfSMichael Tuexen memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_extrcvinfo)); 865d6dda9b2SRandall Stewart break; 866d6dda9b2SRandall Stewart } 867d6dda9b2SRandall Stewart } 868d6dda9b2SRandall Stewart } 869d6dda9b2SRandall Stewart return (sz); 870d6dda9b2SRandall Stewart #endif 871d6dda9b2SRandall Stewart } 872d6dda9b2SRandall Stewart 873e2e7c62eSMichael Tuexen ssize_t 874e2e7c62eSMichael Tuexen sctp_recvv(int sd, 875e2e7c62eSMichael Tuexen const struct iovec *iov, 876e2e7c62eSMichael Tuexen int iovlen, 877e2e7c62eSMichael Tuexen struct sockaddr *from, 878e2e7c62eSMichael Tuexen socklen_t *fromlen, 879e2e7c62eSMichael Tuexen void *info, 880e2e7c62eSMichael Tuexen socklen_t *infolen, 881e2e7c62eSMichael Tuexen unsigned int *infotype, 882e2e7c62eSMichael Tuexen int *flags) 883d6dda9b2SRandall Stewart { 8847f15a8dfSMichael Tuexen char cmsgbuf[SCTP_CONTROL_VEC_SIZE_RCV]; 885e2e7c62eSMichael Tuexen struct msghdr msg; 886e2e7c62eSMichael Tuexen struct cmsghdr *cmsg; 8877f15a8dfSMichael Tuexen ssize_t ret; 888e2e7c62eSMichael Tuexen struct sctp_rcvinfo *rcvinfo; 889e2e7c62eSMichael Tuexen struct sctp_nxtinfo *nxtinfo; 890d6dda9b2SRandall Stewart 8910d958bd4SMichael Tuexen if (((info != NULL) && (infolen == NULL)) || 8920b064106SMichael Tuexen ((info == NULL) && (infolen != NULL) && (*infolen != 0)) || 8930b064106SMichael Tuexen ((info != NULL) && (infotype == NULL))) { 8940b064106SMichael Tuexen errno = EINVAL; 8950b064106SMichael Tuexen return (-1); 8960b064106SMichael Tuexen } 897e2e7c62eSMichael Tuexen if (infotype) { 898e2e7c62eSMichael Tuexen *infotype = SCTP_RECVV_NOINFO; 899e2e7c62eSMichael Tuexen } 900e2e7c62eSMichael Tuexen msg.msg_name = from; 901e2e7c62eSMichael Tuexen if (fromlen == NULL) { 902e2e7c62eSMichael Tuexen msg.msg_namelen = 0; 903d6dda9b2SRandall Stewart } else { 904e2e7c62eSMichael Tuexen msg.msg_namelen = *fromlen; 905d6dda9b2SRandall Stewart } 906e2e7c62eSMichael Tuexen msg.msg_iov = (struct iovec *)iov; 907e2e7c62eSMichael Tuexen msg.msg_iovlen = iovlen; 9087f15a8dfSMichael Tuexen msg.msg_control = cmsgbuf; 9097f15a8dfSMichael Tuexen msg.msg_controllen = sizeof(cmsgbuf); 9102b8a4d80SMichael Tuexen msg.msg_flags = 0; 9117f15a8dfSMichael Tuexen ret = recvmsg(sd, &msg, *flags); 912e2e7c62eSMichael Tuexen *flags = msg.msg_flags; 9137f15a8dfSMichael Tuexen if ((ret > 0) && 914e2e7c62eSMichael Tuexen (msg.msg_controllen > 0) && 915e2e7c62eSMichael Tuexen (infotype != NULL) && 916e2e7c62eSMichael Tuexen (infolen != NULL) && 917e2e7c62eSMichael Tuexen (*infolen > 0)) { 918e2e7c62eSMichael Tuexen rcvinfo = NULL; 919e2e7c62eSMichael Tuexen nxtinfo = NULL; 920e2e7c62eSMichael Tuexen for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 921e2e7c62eSMichael Tuexen if (cmsg->cmsg_level != IPPROTO_SCTP) { 922e2e7c62eSMichael Tuexen continue; 923e2e7c62eSMichael Tuexen } 924e2e7c62eSMichael Tuexen if (cmsg->cmsg_type == SCTP_RCVINFO) { 925e2e7c62eSMichael Tuexen rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg); 9267f15a8dfSMichael Tuexen if (nxtinfo != NULL) { 9277f15a8dfSMichael Tuexen break; 9287f15a8dfSMichael Tuexen } else { 9297f15a8dfSMichael Tuexen continue; 9307f15a8dfSMichael Tuexen } 931e2e7c62eSMichael Tuexen } 932e2e7c62eSMichael Tuexen if (cmsg->cmsg_type == SCTP_NXTINFO) { 933e2e7c62eSMichael Tuexen nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmsg); 9347f15a8dfSMichael Tuexen if (rcvinfo != NULL) { 935e2e7c62eSMichael Tuexen break; 9367f15a8dfSMichael Tuexen } else { 9377f15a8dfSMichael Tuexen continue; 938e2e7c62eSMichael Tuexen } 939e2e7c62eSMichael Tuexen } 9407f15a8dfSMichael Tuexen } 9417f15a8dfSMichael Tuexen if (rcvinfo != NULL) { 9427f15a8dfSMichael Tuexen if ((nxtinfo != NULL) && (*infolen >= sizeof(struct sctp_recvv_rn))) { 943e2e7c62eSMichael Tuexen struct sctp_recvv_rn *rn_info; 944e2e7c62eSMichael Tuexen 945e2e7c62eSMichael Tuexen rn_info = (struct sctp_recvv_rn *)info; 946e2e7c62eSMichael Tuexen rn_info->recvv_rcvinfo = *rcvinfo; 947e2e7c62eSMichael Tuexen rn_info->recvv_nxtinfo = *nxtinfo; 948e2e7c62eSMichael Tuexen *infolen = (socklen_t)sizeof(struct sctp_recvv_rn); 949e2e7c62eSMichael Tuexen *infotype = SCTP_RECVV_RN; 9507f15a8dfSMichael Tuexen } else if (*infolen >= sizeof(struct sctp_rcvinfo)) { 951e2e7c62eSMichael Tuexen memcpy(info, rcvinfo, sizeof(struct sctp_rcvinfo)); 952e2e7c62eSMichael Tuexen *infolen = (socklen_t)sizeof(struct sctp_rcvinfo); 953e2e7c62eSMichael Tuexen *infotype = SCTP_RECVV_RCVINFO; 954e2e7c62eSMichael Tuexen } 9557f15a8dfSMichael Tuexen } else if (nxtinfo != NULL) { 9567f15a8dfSMichael Tuexen if (*infolen >= sizeof(struct sctp_nxtinfo)) { 957e2e7c62eSMichael Tuexen memcpy(info, nxtinfo, sizeof(struct sctp_nxtinfo)); 958e2e7c62eSMichael Tuexen *infolen = (socklen_t)sizeof(struct sctp_nxtinfo); 959e2e7c62eSMichael Tuexen *infotype = SCTP_RECVV_NXTINFO; 960e2e7c62eSMichael Tuexen } 961e2e7c62eSMichael Tuexen } 962e2e7c62eSMichael Tuexen } 9637f15a8dfSMichael Tuexen return (ret); 964d6dda9b2SRandall Stewart } 965d6dda9b2SRandall Stewart 966e2e7c62eSMichael Tuexen ssize_t 967e2e7c62eSMichael Tuexen sctp_sendv(int sd, 968e2e7c62eSMichael Tuexen const struct iovec *iov, int iovcnt, 969e2e7c62eSMichael Tuexen struct sockaddr *addrs, int addrcnt, 970e2e7c62eSMichael Tuexen void *info, socklen_t infolen, unsigned int infotype, 971e2e7c62eSMichael Tuexen int flags) 972e2e7c62eSMichael Tuexen { 973e2e7c62eSMichael Tuexen ssize_t ret; 974e2e7c62eSMichael Tuexen int i; 9750b064106SMichael Tuexen socklen_t addr_len; 976e2e7c62eSMichael Tuexen struct msghdr msg; 9770b064106SMichael Tuexen in_port_t port; 9780b064106SMichael Tuexen struct sctp_sendv_spa *spa_info; 979e2e7c62eSMichael Tuexen struct cmsghdr *cmsg; 980e2e7c62eSMichael Tuexen char *cmsgbuf; 981e2e7c62eSMichael Tuexen struct sockaddr *addr; 982e2e7c62eSMichael Tuexen struct sockaddr_in *addr_in; 983e2e7c62eSMichael Tuexen struct sockaddr_in6 *addr_in6; 984c7f6ce28SMichael Tuexen sctp_assoc_t *assoc_id; 985e2e7c62eSMichael Tuexen 9860b064106SMichael Tuexen if ((addrcnt < 0) || 9870b064106SMichael Tuexen (iovcnt < 0) || 988c67a03f9SMichael Tuexen ((addrs == NULL) && (addrcnt > 0)) || 989c67a03f9SMichael Tuexen ((addrs != NULL) && (addrcnt == 0)) || 9900b064106SMichael Tuexen ((iov == NULL) && (iovcnt > 0)) || 9910b064106SMichael Tuexen ((iov != NULL) && (iovcnt == 0))) { 992e2e7c62eSMichael Tuexen errno = EINVAL; 993e2e7c62eSMichael Tuexen return (-1); 994e2e7c62eSMichael Tuexen } 995e2e7c62eSMichael Tuexen cmsgbuf = malloc(CMSG_SPACE(sizeof(struct sctp_sndinfo)) + 996e2e7c62eSMichael Tuexen CMSG_SPACE(sizeof(struct sctp_prinfo)) + 997e2e7c62eSMichael Tuexen CMSG_SPACE(sizeof(struct sctp_authinfo)) + 9985dc6a815SMichael Tuexen (size_t)addrcnt * CMSG_SPACE(sizeof(struct in6_addr))); 999e2e7c62eSMichael Tuexen if (cmsgbuf == NULL) { 10004ed0ebf6SMichael Tuexen errno = ENOMEM; 1001e2e7c62eSMichael Tuexen return (-1); 1002e2e7c62eSMichael Tuexen } 1003c7f6ce28SMichael Tuexen assoc_id = NULL; 1004e2e7c62eSMichael Tuexen msg.msg_control = cmsgbuf; 1005e2e7c62eSMichael Tuexen msg.msg_controllen = 0; 1006e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)cmsgbuf; 1007e2e7c62eSMichael Tuexen switch (infotype) { 10080b064106SMichael Tuexen case SCTP_SENDV_NOINFO: 10090b064106SMichael Tuexen if ((infolen != 0) || (info != NULL)) { 10100b064106SMichael Tuexen free(cmsgbuf); 10110b064106SMichael Tuexen errno = EINVAL; 10120b064106SMichael Tuexen return (-1); 10130b064106SMichael Tuexen } 10140b064106SMichael Tuexen break; 1015e2e7c62eSMichael Tuexen case SCTP_SENDV_SNDINFO: 10160b064106SMichael Tuexen if ((info == NULL) || (infolen < sizeof(struct sctp_sndinfo))) { 1017e2e7c62eSMichael Tuexen free(cmsgbuf); 1018e2e7c62eSMichael Tuexen errno = EINVAL; 1019e2e7c62eSMichael Tuexen return (-1); 1020e2e7c62eSMichael Tuexen } 1021e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1022e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_SNDINFO; 1023e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo)); 1024e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_sndinfo)); 1025e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo)); 1026e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo))); 1027c7f6ce28SMichael Tuexen assoc_id = &(((struct sctp_sndinfo *)info)->snd_assoc_id); 1028e2e7c62eSMichael Tuexen break; 1029e2e7c62eSMichael Tuexen case SCTP_SENDV_PRINFO: 10300b064106SMichael Tuexen if ((info == NULL) || (infolen < sizeof(struct sctp_prinfo))) { 1031e2e7c62eSMichael Tuexen free(cmsgbuf); 1032e2e7c62eSMichael Tuexen errno = EINVAL; 1033e2e7c62eSMichael Tuexen return (-1); 1034e2e7c62eSMichael Tuexen } 1035e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1036e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_PRINFO; 1037e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo)); 1038e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_prinfo)); 1039e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo)); 1040e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo))); 1041e2e7c62eSMichael Tuexen break; 1042e2e7c62eSMichael Tuexen case SCTP_SENDV_AUTHINFO: 10430b064106SMichael Tuexen if ((info == NULL) || (infolen < sizeof(struct sctp_authinfo))) { 1044e2e7c62eSMichael Tuexen free(cmsgbuf); 1045e2e7c62eSMichael Tuexen errno = EINVAL; 1046e2e7c62eSMichael Tuexen return (-1); 1047e2e7c62eSMichael Tuexen } 1048e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1049e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_AUTHINFO; 1050e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo)); 1051e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_authinfo)); 1052e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo)); 1053e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo))); 1054e2e7c62eSMichael Tuexen break; 1055e2e7c62eSMichael Tuexen case SCTP_SENDV_SPA: 10560b064106SMichael Tuexen if ((info == NULL) || (infolen < sizeof(struct sctp_sendv_spa))) { 1057e2e7c62eSMichael Tuexen free(cmsgbuf); 1058e2e7c62eSMichael Tuexen errno = EINVAL; 1059e2e7c62eSMichael Tuexen return (-1); 1060e2e7c62eSMichael Tuexen } 1061e2e7c62eSMichael Tuexen spa_info = (struct sctp_sendv_spa *)info; 1062e2e7c62eSMichael Tuexen if (spa_info->sendv_flags & SCTP_SEND_SNDINFO_VALID) { 1063e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1064e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_SNDINFO; 1065e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo)); 1066e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), &spa_info->sendv_sndinfo, sizeof(struct sctp_sndinfo)); 1067e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo)); 1068e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo))); 1069c7f6ce28SMichael Tuexen assoc_id = &(spa_info->sendv_sndinfo.snd_assoc_id); 1070e2e7c62eSMichael Tuexen } 1071e2e7c62eSMichael Tuexen if (spa_info->sendv_flags & SCTP_SEND_PRINFO_VALID) { 1072e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1073e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_PRINFO; 1074e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo)); 1075e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), &spa_info->sendv_prinfo, sizeof(struct sctp_prinfo)); 1076e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo)); 1077e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo))); 1078e2e7c62eSMichael Tuexen } 1079e2e7c62eSMichael Tuexen if (spa_info->sendv_flags & SCTP_SEND_AUTHINFO_VALID) { 1080e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1081e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_AUTHINFO; 1082e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo)); 1083e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), &spa_info->sendv_authinfo, sizeof(struct sctp_authinfo)); 1084e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo)); 1085e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo))); 1086e2e7c62eSMichael Tuexen } 1087e2e7c62eSMichael Tuexen break; 1088e2e7c62eSMichael Tuexen default: 1089e2e7c62eSMichael Tuexen free(cmsgbuf); 1090e2e7c62eSMichael Tuexen errno = EINVAL; 1091e2e7c62eSMichael Tuexen return (-1); 1092e2e7c62eSMichael Tuexen } 1093e2e7c62eSMichael Tuexen addr = addrs; 10940b064106SMichael Tuexen msg.msg_name = NULL; 10950b064106SMichael Tuexen msg.msg_namelen = 0; 10960b064106SMichael Tuexen 10970b064106SMichael Tuexen for (i = 0; i < addrcnt; i++) { 1098e2e7c62eSMichael Tuexen switch (addr->sa_family) { 1099e2e7c62eSMichael Tuexen case AF_INET: 11000b064106SMichael Tuexen addr_len = (socklen_t)sizeof(struct sockaddr_in); 11010b064106SMichael Tuexen addr_in = (struct sockaddr_in *)addr; 11020b064106SMichael Tuexen if (addr_in->sin_len != addr_len) { 1103e2e7c62eSMichael Tuexen free(cmsgbuf); 1104e2e7c62eSMichael Tuexen errno = EINVAL; 1105e2e7c62eSMichael Tuexen return (-1); 1106e2e7c62eSMichael Tuexen } 11070b064106SMichael Tuexen if (i == 0) { 11080b064106SMichael Tuexen port = addr_in->sin_port; 1109e2e7c62eSMichael Tuexen } else { 11100b064106SMichael Tuexen if (port == addr_in->sin_port) { 1111e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1112e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_DSTADDRV4; 1113e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); 1114e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), &addr_in->sin_addr, sizeof(struct in_addr)); 1115e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct in_addr)); 1116e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in_addr))); 11170b064106SMichael Tuexen } else { 11180b064106SMichael Tuexen free(cmsgbuf); 11190b064106SMichael Tuexen errno = EINVAL; 11200b064106SMichael Tuexen return (-1); 11210b064106SMichael Tuexen } 11220b064106SMichael Tuexen } 1123e2e7c62eSMichael Tuexen break; 1124e2e7c62eSMichael Tuexen case AF_INET6: 11250b064106SMichael Tuexen addr_len = (socklen_t)sizeof(struct sockaddr_in6); 1126e2e7c62eSMichael Tuexen addr_in6 = (struct sockaddr_in6 *)addr; 11270b064106SMichael Tuexen if (addr_in6->sin6_len != addr_len) { 11280b064106SMichael Tuexen free(cmsgbuf); 11290b064106SMichael Tuexen errno = EINVAL; 11300b064106SMichael Tuexen return (-1); 11310b064106SMichael Tuexen } 11320b064106SMichael Tuexen if (i == 0) { 11330b064106SMichael Tuexen port = addr_in6->sin6_port; 11340b064106SMichael Tuexen } else { 11350b064106SMichael Tuexen if (port == addr_in6->sin6_port) { 1136e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1137e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_DSTADDRV6; 1138e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_addr)); 1139e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), &addr_in6->sin6_addr, sizeof(struct in6_addr)); 1140e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct in6_addr)); 1141e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in6_addr))); 11420b064106SMichael Tuexen } else { 11430b064106SMichael Tuexen free(cmsgbuf); 11440b064106SMichael Tuexen errno = EINVAL; 11450b064106SMichael Tuexen return (-1); 11460b064106SMichael Tuexen } 11470b064106SMichael Tuexen } 1148e2e7c62eSMichael Tuexen break; 1149e2e7c62eSMichael Tuexen default: 1150e2e7c62eSMichael Tuexen free(cmsgbuf); 1151e2e7c62eSMichael Tuexen errno = EINVAL; 1152e2e7c62eSMichael Tuexen return (-1); 1153e2e7c62eSMichael Tuexen } 11540b064106SMichael Tuexen if (i == 0) { 11550b064106SMichael Tuexen msg.msg_name = addr; 11560b064106SMichael Tuexen msg.msg_namelen = addr_len; 11570b064106SMichael Tuexen } 1158e2e7c62eSMichael Tuexen addr = (struct sockaddr *)((caddr_t)addr + addr_len); 1159e2e7c62eSMichael Tuexen } 11600b064106SMichael Tuexen if (msg.msg_controllen == 0) { 11610b064106SMichael Tuexen msg.msg_control = NULL; 1162e2e7c62eSMichael Tuexen } 1163e2e7c62eSMichael Tuexen msg.msg_iov = (struct iovec *)iov; 1164e2e7c62eSMichael Tuexen msg.msg_iovlen = iovcnt; 1165e2e7c62eSMichael Tuexen msg.msg_flags = 0; 1166e2e7c62eSMichael Tuexen ret = sendmsg(sd, &msg, flags); 1167e2e7c62eSMichael Tuexen free(cmsgbuf); 1168c7f6ce28SMichael Tuexen if ((ret >= 0) && (addrs != NULL) && (assoc_id != NULL)) { 1169c7f6ce28SMichael Tuexen *assoc_id = sctp_getassocid(sd, addrs); 1170c7f6ce28SMichael Tuexen } 1171e2e7c62eSMichael Tuexen return (ret); 1172e2e7c62eSMichael Tuexen } 1173e2e7c62eSMichael Tuexen 1174d6dda9b2SRandall Stewart 1175d6dda9b2SRandall Stewart #if !defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT) 1176d6dda9b2SRandall Stewart 1177d6dda9b2SRandall Stewart int 1178d6dda9b2SRandall Stewart sctp_peeloff(int sd, sctp_assoc_t assoc_id) 1179d6dda9b2SRandall Stewart { 1180d6dda9b2SRandall Stewart /* NOT supported, return invalid sd */ 1181d6dda9b2SRandall Stewart errno = ENOTSUP; 1182d6dda9b2SRandall Stewart return (-1); 1183d6dda9b2SRandall Stewart } 1184d6dda9b2SRandall Stewart 1185d6dda9b2SRandall Stewart #endif 1186d6dda9b2SRandall Stewart #if defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT) 1187d6dda9b2SRandall Stewart int 1188d6dda9b2SRandall Stewart sctp_peeloff(int sd, sctp_assoc_t assoc_id) 1189d6dda9b2SRandall Stewart { 1190d6dda9b2SRandall Stewart return (syscall(SYS_sctp_peeloff, sd, assoc_id)); 1191d6dda9b2SRandall Stewart } 1192d6dda9b2SRandall Stewart 1193d6dda9b2SRandall Stewart #endif 1194804cf641SRandall Stewart 11952c0d559dSRandall Stewart #undef SCTP_CONTROL_VEC_SIZE_RCV 1196