148f65f00SMichael Tuexen /*- 248f65f00SMichael Tuexen * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved. 343dc9e2fSMichael Tuexen * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 443dc9e2fSMichael Tuexen * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 5d6dda9b2SRandall Stewart * 6d6dda9b2SRandall Stewart * Redistribution and use in source and binary forms, with or without 748f65f00SMichael Tuexen * modification, are permitted provided that the following conditions are met: 8d6dda9b2SRandall Stewart * 948f65f00SMichael Tuexen * a) Redistributions of source code must retain the above copyright notice, 1048f65f00SMichael Tuexen * this list of conditions and the following disclaimer. 1148f65f00SMichael Tuexen * 1248f65f00SMichael Tuexen * b) Redistributions in binary form must reproduce the above copyright 1348f65f00SMichael Tuexen * notice, this list of conditions and the following disclaimer in 1448f65f00SMichael Tuexen * the documentation and/or other materials provided with the distribution. 1548f65f00SMichael Tuexen * 1648f65f00SMichael Tuexen * c) Neither the name of Cisco Systems, Inc. nor the names of its 1748f65f00SMichael Tuexen * contributors may be used to endorse or promote products derived 1848f65f00SMichael Tuexen * from this software without specific prior written permission. 1948f65f00SMichael Tuexen * 2048f65f00SMichael Tuexen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2148f65f00SMichael Tuexen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 2248f65f00SMichael Tuexen * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2348f65f00SMichael Tuexen * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 2448f65f00SMichael Tuexen * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2548f65f00SMichael Tuexen * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2648f65f00SMichael Tuexen * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2748f65f00SMichael Tuexen * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2848f65f00SMichael Tuexen * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2948f65f00SMichael Tuexen * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 3048f65f00SMichael Tuexen * THE POSSIBILITY OF SUCH DAMAGE. 31d6dda9b2SRandall Stewart */ 3248f65f00SMichael Tuexen 33d6dda9b2SRandall Stewart #include <sys/cdefs.h> 34d6dda9b2SRandall Stewart __FBSDID("$FreeBSD$"); 3543dc9e2fSMichael Tuexen 36d6dda9b2SRandall Stewart #include <stdio.h> 37d6dda9b2SRandall Stewart #include <string.h> 38d6dda9b2SRandall Stewart #include <errno.h> 39d6dda9b2SRandall Stewart #include <stdlib.h> 40d6dda9b2SRandall Stewart #include <unistd.h> 41d6dda9b2SRandall Stewart #include <sys/types.h> 42d6dda9b2SRandall Stewart #include <sys/socket.h> 43d6dda9b2SRandall Stewart #include <sys/errno.h> 44d6dda9b2SRandall Stewart #include <sys/syscall.h> 45d6dda9b2SRandall Stewart #include <sys/uio.h> 46d6dda9b2SRandall Stewart #include <netinet/in.h> 47d6dda9b2SRandall Stewart #include <arpa/inet.h> 48d6dda9b2SRandall Stewart #include <netinet/sctp_uio.h> 49d6dda9b2SRandall Stewart #include <netinet/sctp.h> 50d6dda9b2SRandall Stewart 51d6dda9b2SRandall Stewart #ifndef IN6_IS_ADDR_V4MAPPED 52d6dda9b2SRandall Stewart #define IN6_IS_ADDR_V4MAPPED(a) \ 533d36ac98SRebecca Cran ((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) && \ 543d36ac98SRebecca Cran (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) && \ 553d36ac98SRebecca Cran (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == ntohl(0x0000ffff))) 56d6dda9b2SRandall Stewart #endif 57d6dda9b2SRandall Stewart 58d6dda9b2SRandall Stewart #define SCTP_CONTROL_VEC_SIZE_RCV 16384 59d6dda9b2SRandall Stewart 60d6dda9b2SRandall Stewart 61d6dda9b2SRandall Stewart static void 62d6dda9b2SRandall Stewart in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 63d6dda9b2SRandall Stewart { 64d6dda9b2SRandall Stewart bzero(sin, sizeof(*sin)); 65d6dda9b2SRandall Stewart sin->sin_len = sizeof(struct sockaddr_in); 66d6dda9b2SRandall Stewart sin->sin_family = AF_INET; 67d6dda9b2SRandall Stewart sin->sin_port = sin6->sin6_port; 68d6dda9b2SRandall Stewart sin->sin_addr.s_addr = sin6->sin6_addr.__u6_addr.__u6_addr32[3]; 69d6dda9b2SRandall Stewart } 70d6dda9b2SRandall Stewart 71d6dda9b2SRandall Stewart int 72d6dda9b2SRandall Stewart sctp_getaddrlen(sa_family_t family) 73d6dda9b2SRandall Stewart { 74e2e7c62eSMichael Tuexen int ret, sd; 75d6dda9b2SRandall Stewart socklen_t siz; 76d6dda9b2SRandall Stewart struct sctp_assoc_value av; 77d6dda9b2SRandall Stewart 78d6dda9b2SRandall Stewart av.assoc_value = family; 79d6dda9b2SRandall Stewart siz = sizeof(av); 80d6dda9b2SRandall Stewart #if defined(AF_INET) 81d6dda9b2SRandall Stewart sd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); 82d6dda9b2SRandall Stewart #elif defined(AF_INET6) 83d6dda9b2SRandall Stewart sd = socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP); 84e2e7c62eSMichael Tuexen #else 85e2e7c62eSMichael Tuexen sd = -1; 86d6dda9b2SRandall Stewart #endif 87d6dda9b2SRandall Stewart if (sd == -1) { 88a593094eSRandall Stewart return (-1); 89d6dda9b2SRandall Stewart } 90e2e7c62eSMichael Tuexen ret = getsockopt(sd, IPPROTO_SCTP, SCTP_GET_ADDR_LEN, &av, &siz); 91d6dda9b2SRandall Stewart close(sd); 92e2e7c62eSMichael Tuexen if (ret == 0) { 93d6dda9b2SRandall Stewart return ((int)av.assoc_value); 94d6dda9b2SRandall Stewart } else { 95a593094eSRandall Stewart return (-1); 96d6dda9b2SRandall Stewart } 97d6dda9b2SRandall Stewart } 98d6dda9b2SRandall Stewart 99d6dda9b2SRandall Stewart int 1002c356be2SRandall Stewart sctp_connectx(int sd, const struct sockaddr *addrs, int addrcnt, 1012c356be2SRandall Stewart sctp_assoc_t * id) 102d6dda9b2SRandall Stewart { 1037f15a8dfSMichael Tuexen char *buf; 10410e6d832SMichael Tuexen int i, ret, *aa; 105d6dda9b2SRandall Stewart char *cpto; 106d6dda9b2SRandall Stewart const struct sockaddr *at; 10710e6d832SMichael Tuexen size_t len; 108d6dda9b2SRandall Stewart 1092c356be2SRandall Stewart /* validate the address count and list */ 1102c356be2SRandall Stewart if ((addrs == NULL) || (addrcnt <= 0)) { 1112c356be2SRandall Stewart errno = EINVAL; 1122c356be2SRandall Stewart return (-1); 1132c356be2SRandall Stewart } 1147f15a8dfSMichael Tuexen if ((buf = malloc(sizeof(int) + (size_t)addrcnt * sizeof(struct sockaddr_in6))) == NULL) { 1157f15a8dfSMichael Tuexen errno = E2BIG; 1167f15a8dfSMichael Tuexen return (-1); 1177f15a8dfSMichael Tuexen } 11810e6d832SMichael Tuexen len = sizeof(int); 119d6dda9b2SRandall Stewart at = addrs; 1207f15a8dfSMichael Tuexen cpto = buf + sizeof(int); 121d6dda9b2SRandall Stewart /* validate all the addresses and get the size */ 122d6dda9b2SRandall Stewart for (i = 0; i < addrcnt; i++) { 12393409822SMichael Tuexen switch (at->sa_family) { 12493409822SMichael Tuexen case AF_INET: 12525d63f19SRandall Stewart if (at->sa_len != sizeof(struct sockaddr_in)) { 1267f15a8dfSMichael Tuexen free(buf); 12725d63f19SRandall Stewart errno = EINVAL; 12825d63f19SRandall Stewart return (-1); 12925d63f19SRandall Stewart } 13093409822SMichael Tuexen memcpy(cpto, at, sizeof(struct sockaddr_in)); 13193409822SMichael Tuexen cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in)); 13293409822SMichael Tuexen len += sizeof(struct sockaddr_in); 13393409822SMichael Tuexen break; 13493409822SMichael Tuexen case AF_INET6: 13525d63f19SRandall Stewart if (at->sa_len != sizeof(struct sockaddr_in6)) { 1367f15a8dfSMichael Tuexen free(buf); 13725d63f19SRandall Stewart errno = EINVAL; 13825d63f19SRandall Stewart return (-1); 13925d63f19SRandall Stewart } 140d6dda9b2SRandall Stewart if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)at)->sin6_addr)) { 141d6dda9b2SRandall Stewart in6_sin6_2_sin((struct sockaddr_in *)cpto, (struct sockaddr_in6 *)at); 142d6dda9b2SRandall Stewart cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in)); 143d6dda9b2SRandall Stewart len += sizeof(struct sockaddr_in); 144d6dda9b2SRandall Stewart } else { 14593409822SMichael Tuexen memcpy(cpto, at, sizeof(struct sockaddr_in6)); 14693409822SMichael Tuexen cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in6)); 14793409822SMichael Tuexen len += sizeof(struct sockaddr_in6); 148d6dda9b2SRandall Stewart } 14993409822SMichael Tuexen break; 15093409822SMichael Tuexen default: 1517f15a8dfSMichael Tuexen free(buf); 152d6dda9b2SRandall Stewart errno = EINVAL; 153d6dda9b2SRandall Stewart return (-1); 154d6dda9b2SRandall Stewart } 1557f15a8dfSMichael Tuexen at = (struct sockaddr *)((caddr_t)at + at->sa_len); 156d6dda9b2SRandall Stewart } 157d6dda9b2SRandall Stewart aa = (int *)buf; 1587f15a8dfSMichael Tuexen *aa = addrcnt; 159d6dda9b2SRandall Stewart ret = setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X, (void *)buf, 160d6dda9b2SRandall Stewart (socklen_t) len); 1617f15a8dfSMichael Tuexen if ((ret == 0) && (id != NULL)) { 1627f15a8dfSMichael Tuexen *id = *(sctp_assoc_t *) buf; 16342551e99SRandall Stewart } 16410e6d832SMichael Tuexen free(buf); 165d6dda9b2SRandall Stewart return (ret); 166d6dda9b2SRandall Stewart } 167d6dda9b2SRandall Stewart 168d6dda9b2SRandall Stewart int 169d6dda9b2SRandall Stewart sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, int flags) 170d6dda9b2SRandall Stewart { 171d6dda9b2SRandall Stewart struct sctp_getaddresses *gaddrs; 172d6dda9b2SRandall Stewart struct sockaddr *sa; 1731b649582SRandall Stewart struct sockaddr_in *sin; 1741b649582SRandall Stewart struct sockaddr_in6 *sin6; 1755dc6a815SMichael Tuexen int i; 1765dc6a815SMichael Tuexen size_t argsz; 1771b649582SRandall Stewart uint16_t sport = 0; 178d6dda9b2SRandall Stewart 1792c356be2SRandall Stewart /* validate the flags */ 180d6dda9b2SRandall Stewart if ((flags != SCTP_BINDX_ADD_ADDR) && 181d6dda9b2SRandall Stewart (flags != SCTP_BINDX_REM_ADDR)) { 182d6dda9b2SRandall Stewart errno = EFAULT; 183d6dda9b2SRandall Stewart return (-1); 184d6dda9b2SRandall Stewart } 1852c356be2SRandall Stewart /* validate the address count and list */ 1862c356be2SRandall Stewart if ((addrcnt <= 0) || (addrs == NULL)) { 1872c356be2SRandall Stewart errno = EINVAL; 1882c356be2SRandall Stewart return (-1); 1892c356be2SRandall Stewart } 1901b649582SRandall Stewart /* First pre-screen the addresses */ 191d6dda9b2SRandall Stewart sa = addrs; 192d6dda9b2SRandall Stewart for (i = 0; i < addrcnt; i++) { 19393409822SMichael Tuexen switch (sa->sa_family) { 19493409822SMichael Tuexen case AF_INET: 19593409822SMichael Tuexen if (sa->sa_len != sizeof(struct sockaddr_in)) { 19693409822SMichael Tuexen errno = EINVAL; 19793409822SMichael Tuexen return (-1); 19893409822SMichael Tuexen } 1991b649582SRandall Stewart sin = (struct sockaddr_in *)sa; 2001b649582SRandall Stewart if (sin->sin_port) { 2011b649582SRandall Stewart /* non-zero port, check or save */ 2021b649582SRandall Stewart if (sport) { 2031b649582SRandall Stewart /* Check against our port */ 2041b649582SRandall Stewart if (sport != sin->sin_port) { 20593409822SMichael Tuexen errno = EINVAL; 20693409822SMichael Tuexen return (-1); 2071b649582SRandall Stewart } 2081b649582SRandall Stewart } else { 2091b649582SRandall Stewart /* save off the port */ 2101b649582SRandall Stewart sport = sin->sin_port; 2111b649582SRandall Stewart } 2121b649582SRandall Stewart } 21393409822SMichael Tuexen break; 21493409822SMichael Tuexen case AF_INET6: 21593409822SMichael Tuexen if (sa->sa_len != sizeof(struct sockaddr_in6)) { 21693409822SMichael Tuexen errno = EINVAL; 21793409822SMichael Tuexen return (-1); 21893409822SMichael Tuexen } 2191b649582SRandall Stewart sin6 = (struct sockaddr_in6 *)sa; 2201b649582SRandall Stewart if (sin6->sin6_port) { 2211b649582SRandall Stewart /* non-zero port, check or save */ 2221b649582SRandall Stewart if (sport) { 2231b649582SRandall Stewart /* Check against our port */ 2241b649582SRandall Stewart if (sport != sin6->sin6_port) { 22593409822SMichael Tuexen errno = EINVAL; 22693409822SMichael Tuexen return (-1); 2271b649582SRandall Stewart } 2281b649582SRandall Stewart } else { 2291b649582SRandall Stewart /* save off the port */ 2301b649582SRandall Stewart sport = sin6->sin6_port; 2311b649582SRandall Stewart } 2321b649582SRandall Stewart } 23393409822SMichael Tuexen break; 23493409822SMichael Tuexen default: 23593409822SMichael Tuexen /* Invalid address family specified. */ 2361dd0c905SMichael Tuexen errno = EAFNOSUPPORT; 23793409822SMichael Tuexen return (-1); 2381b649582SRandall Stewart } 2395dc6a815SMichael Tuexen sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 2401b649582SRandall Stewart } 24193409822SMichael Tuexen argsz = sizeof(struct sctp_getaddresses) + 24293409822SMichael Tuexen sizeof(struct sockaddr_storage); 24393409822SMichael Tuexen if ((gaddrs = (struct sctp_getaddresses *)malloc(argsz)) == NULL) { 24493409822SMichael Tuexen errno = ENOMEM; 245d6dda9b2SRandall Stewart return (-1); 246d6dda9b2SRandall Stewart } 24793409822SMichael Tuexen sa = addrs; 24893409822SMichael Tuexen for (i = 0; i < addrcnt; i++) { 24925d63f19SRandall Stewart memset(gaddrs, 0, argsz); 25025d63f19SRandall Stewart gaddrs->sget_assoc_id = 0; 2515dc6a815SMichael Tuexen memcpy(gaddrs->addr, sa, sa->sa_len); 2521dd0c905SMichael Tuexen /* 2531dd0c905SMichael Tuexen * Now, if there was a port mentioned, assure that the first 2541dd0c905SMichael Tuexen * address has that port to make sure it fails or succeeds 2551dd0c905SMichael Tuexen * correctly. 2561dd0c905SMichael Tuexen */ 2571dd0c905SMichael Tuexen if ((i == 0) && (sport != 0)) { 2581dd0c905SMichael Tuexen switch (gaddrs->addr->sa_family) { 2591dd0c905SMichael Tuexen case AF_INET: 2601dd0c905SMichael Tuexen sin = (struct sockaddr_in *)gaddrs->addr; 2611dd0c905SMichael Tuexen sin->sin_port = sport; 2621dd0c905SMichael Tuexen break; 2631dd0c905SMichael Tuexen case AF_INET6: 2641dd0c905SMichael Tuexen sin6 = (struct sockaddr_in6 *)gaddrs->addr; 2651dd0c905SMichael Tuexen sin6->sin6_port = sport; 2661dd0c905SMichael Tuexen break; 2671dd0c905SMichael Tuexen } 2681dd0c905SMichael Tuexen } 26925d63f19SRandall Stewart if (setsockopt(sd, IPPROTO_SCTP, flags, gaddrs, 27025d63f19SRandall Stewart (socklen_t) argsz) != 0) { 271d6dda9b2SRandall Stewart free(gaddrs); 272d6dda9b2SRandall Stewart return (-1); 273d6dda9b2SRandall Stewart } 2745dc6a815SMichael Tuexen sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 275d6dda9b2SRandall Stewart } 276d6dda9b2SRandall Stewart free(gaddrs); 277d6dda9b2SRandall Stewart return (0); 278d6dda9b2SRandall Stewart } 279d6dda9b2SRandall Stewart 280d6dda9b2SRandall Stewart int 281d6dda9b2SRandall Stewart sctp_opt_info(int sd, sctp_assoc_t id, int opt, void *arg, socklen_t * size) 282d6dda9b2SRandall Stewart { 283d6dda9b2SRandall Stewart if (arg == NULL) { 284602afc03SRandall Stewart errno = EINVAL; 285602afc03SRandall Stewart return (-1); 286d6dda9b2SRandall Stewart } 287b71f5853SMichael Tuexen if ((id == SCTP_CURRENT_ASSOC) || 288b71f5853SMichael Tuexen (id == SCTP_ALL_ASSOC)) { 289b71f5853SMichael Tuexen errno = EINVAL; 290b71f5853SMichael Tuexen return (-1); 291b71f5853SMichael Tuexen } 2921b649582SRandall Stewart switch (opt) { 2931b649582SRandall Stewart case SCTP_RTOINFO: 2941b649582SRandall Stewart ((struct sctp_rtoinfo *)arg)->srto_assoc_id = id; 2951b649582SRandall Stewart break; 2961b649582SRandall Stewart case SCTP_ASSOCINFO: 2971b649582SRandall Stewart ((struct sctp_assocparams *)arg)->sasoc_assoc_id = id; 2981b649582SRandall Stewart break; 2991b649582SRandall Stewart case SCTP_DEFAULT_SEND_PARAM: 3001b649582SRandall Stewart ((struct sctp_assocparams *)arg)->sasoc_assoc_id = id; 3011b649582SRandall Stewart break; 3021b649582SRandall Stewart case SCTP_PRIMARY_ADDR: 3031b649582SRandall Stewart ((struct sctp_setprim *)arg)->ssp_assoc_id = id; 3041b649582SRandall Stewart break; 3051b649582SRandall Stewart case SCTP_PEER_ADDR_PARAMS: 3061b649582SRandall Stewart ((struct sctp_paddrparams *)arg)->spp_assoc_id = id; 3071b649582SRandall Stewart break; 3081b649582SRandall Stewart case SCTP_MAXSEG: 3091b649582SRandall Stewart ((struct sctp_assoc_value *)arg)->assoc_id = id; 3101b649582SRandall Stewart break; 3111b649582SRandall Stewart case SCTP_AUTH_KEY: 3121b649582SRandall Stewart ((struct sctp_authkey *)arg)->sca_assoc_id = id; 3131b649582SRandall Stewart break; 3141b649582SRandall Stewart case SCTP_AUTH_ACTIVE_KEY: 3151b649582SRandall Stewart ((struct sctp_authkeyid *)arg)->scact_assoc_id = id; 3161b649582SRandall Stewart break; 3171b649582SRandall Stewart case SCTP_DELAYED_SACK: 3181b649582SRandall Stewart ((struct sctp_sack_info *)arg)->sack_assoc_id = id; 3191b649582SRandall Stewart break; 3201b649582SRandall Stewart case SCTP_CONTEXT: 3211b649582SRandall Stewart ((struct sctp_assoc_value *)arg)->assoc_id = id; 3221b649582SRandall Stewart break; 3231b649582SRandall Stewart case SCTP_STATUS: 3241b649582SRandall Stewart ((struct sctp_status *)arg)->sstat_assoc_id = id; 3251b649582SRandall Stewart break; 3261b649582SRandall Stewart case SCTP_GET_PEER_ADDR_INFO: 3271b649582SRandall Stewart ((struct sctp_paddrinfo *)arg)->spinfo_assoc_id = id; 3281b649582SRandall Stewart break; 3291b649582SRandall Stewart case SCTP_PEER_AUTH_CHUNKS: 3301b649582SRandall Stewart ((struct sctp_authchunks *)arg)->gauth_assoc_id = id; 3311b649582SRandall Stewart break; 3321b649582SRandall Stewart case SCTP_LOCAL_AUTH_CHUNKS: 3331b649582SRandall Stewart ((struct sctp_authchunks *)arg)->gauth_assoc_id = id; 3341b649582SRandall Stewart break; 33548f65f00SMichael Tuexen case SCTP_TIMEOUTS: 33648f65f00SMichael Tuexen ((struct sctp_timeouts *)arg)->stimo_assoc_id = id; 33748f65f00SMichael Tuexen break; 338e2e7c62eSMichael Tuexen case SCTP_EVENT: 339e2e7c62eSMichael Tuexen ((struct sctp_event *)arg)->se_assoc_id = id; 340e2e7c62eSMichael Tuexen break; 34113aae0bfSMichael Tuexen case SCTP_DEFAULT_SNDINFO: 34213aae0bfSMichael Tuexen ((struct sctp_sndinfo *)arg)->snd_assoc_id = id; 34313aae0bfSMichael Tuexen break; 34413aae0bfSMichael Tuexen case SCTP_DEFAULT_PRINFO: 34513aae0bfSMichael Tuexen ((struct sctp_default_prinfo *)arg)->pr_assoc_id = id; 34613aae0bfSMichael Tuexen break; 347ca85e948SMichael Tuexen case SCTP_PEER_ADDR_THLDS: 348ca85e948SMichael Tuexen ((struct sctp_paddrthlds *)arg)->spt_assoc_id = id; 349ca85e948SMichael Tuexen break; 350c9c58059SMichael Tuexen case SCTP_REMOTE_UDP_ENCAPS_PORT: 351c9c58059SMichael Tuexen ((struct sctp_udpencaps *)arg)->sue_assoc_id = id; 352c9c58059SMichael Tuexen break; 353f342355aSMichael Tuexen case SCTP_ECN_SUPPORTED: 354f342355aSMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 355f342355aSMichael Tuexen break; 356*dd973b0eSMichael Tuexen case SCTP_PR_SUPPORTED: 357*dd973b0eSMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 358*dd973b0eSMichael Tuexen break; 359bb2c20c1SMichael Tuexen case SCTP_MAX_BURST: 360bb2c20c1SMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 361bb2c20c1SMichael Tuexen break; 3627c9b6492SMichael Tuexen case SCTP_ENABLE_STREAM_RESET: 3637c9b6492SMichael Tuexen ((struct sctp_assoc_value *)arg)->assoc_id = id; 3647c9b6492SMichael Tuexen break; 3651b649582SRandall Stewart default: 3661b649582SRandall Stewart break; 3671b649582SRandall Stewart } 368d6dda9b2SRandall Stewart return (getsockopt(sd, IPPROTO_SCTP, opt, arg, size)); 369d6dda9b2SRandall Stewart } 370d6dda9b2SRandall Stewart 371d6dda9b2SRandall Stewart int 372d6dda9b2SRandall Stewart sctp_getpaddrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs) 373d6dda9b2SRandall Stewart { 374d6dda9b2SRandall Stewart struct sctp_getaddresses *addrs; 375d6dda9b2SRandall Stewart struct sockaddr *sa; 376d6dda9b2SRandall Stewart sctp_assoc_t asoc; 377d6dda9b2SRandall Stewart caddr_t lim; 3785dc6a815SMichael Tuexen socklen_t opt_len; 379d6dda9b2SRandall Stewart int cnt; 380d6dda9b2SRandall Stewart 381d6dda9b2SRandall Stewart if (raddrs == NULL) { 382d6dda9b2SRandall Stewart errno = EFAULT; 383d6dda9b2SRandall Stewart return (-1); 384d6dda9b2SRandall Stewart } 385d6dda9b2SRandall Stewart asoc = id; 3865dc6a815SMichael Tuexen opt_len = (socklen_t) sizeof(sctp_assoc_t); 387d6dda9b2SRandall Stewart if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_REMOTE_ADDR_SIZE, 3885dc6a815SMichael Tuexen &asoc, &opt_len) != 0) { 389d6dda9b2SRandall Stewart return (-1); 390d6dda9b2SRandall Stewart } 391d6dda9b2SRandall Stewart /* size required is returned in 'asoc' */ 3925dc6a815SMichael Tuexen opt_len = (socklen_t) ((size_t)asoc + sizeof(struct sctp_getaddresses)); 3935dc6a815SMichael Tuexen addrs = calloc(1, (size_t)opt_len); 394d6dda9b2SRandall Stewart if (addrs == NULL) { 3954ed0ebf6SMichael Tuexen errno = ENOMEM; 396d6dda9b2SRandall Stewart return (-1); 397d6dda9b2SRandall Stewart } 398d6dda9b2SRandall Stewart addrs->sget_assoc_id = id; 399d6dda9b2SRandall Stewart /* Now lets get the array of addresses */ 400d6dda9b2SRandall Stewart if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_PEER_ADDRESSES, 4015dc6a815SMichael Tuexen addrs, &opt_len) != 0) { 402d6dda9b2SRandall Stewart free(addrs); 403d6dda9b2SRandall Stewart return (-1); 404d6dda9b2SRandall Stewart } 4055dc6a815SMichael Tuexen *raddrs = (struct sockaddr *)&addrs->addr[0]; 406d6dda9b2SRandall Stewart cnt = 0; 407d6dda9b2SRandall Stewart sa = (struct sockaddr *)&addrs->addr[0]; 4085dc6a815SMichael Tuexen lim = (caddr_t)addrs + opt_len; 409d6dda9b2SRandall Stewart while (((caddr_t)sa < lim) && (sa->sa_len > 0)) { 410d6dda9b2SRandall Stewart sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 411d6dda9b2SRandall Stewart cnt++; 412d6dda9b2SRandall Stewart } 413d6dda9b2SRandall Stewart return (cnt); 414d6dda9b2SRandall Stewart } 415d6dda9b2SRandall Stewart 416d6dda9b2SRandall Stewart void 417d6dda9b2SRandall Stewart sctp_freepaddrs(struct sockaddr *addrs) 418d6dda9b2SRandall Stewart { 419d6dda9b2SRandall Stewart void *fr_addr; 420d6dda9b2SRandall Stewart 4217f15a8dfSMichael Tuexen /* Take away the hidden association id */ 422d6dda9b2SRandall Stewart fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t)); 423d6dda9b2SRandall Stewart /* Now free it */ 424d6dda9b2SRandall Stewart free(fr_addr); 425d6dda9b2SRandall Stewart } 426d6dda9b2SRandall Stewart 427d6dda9b2SRandall Stewart int 428d6dda9b2SRandall Stewart sctp_getladdrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs) 429d6dda9b2SRandall Stewart { 430d6dda9b2SRandall Stewart struct sctp_getaddresses *addrs; 431d6dda9b2SRandall Stewart caddr_t lim; 432d6dda9b2SRandall Stewart struct sockaddr *sa; 4335dc6a815SMichael Tuexen size_t size_of_addresses; 4345dc6a815SMichael Tuexen socklen_t opt_len; 435d6dda9b2SRandall Stewart int cnt; 436d6dda9b2SRandall Stewart 437d6dda9b2SRandall Stewart if (raddrs == NULL) { 438d6dda9b2SRandall Stewart errno = EFAULT; 439d6dda9b2SRandall Stewart return (-1); 440d6dda9b2SRandall Stewart } 441d6dda9b2SRandall Stewart size_of_addresses = 0; 4425dc6a815SMichael Tuexen opt_len = (socklen_t) sizeof(int); 443d6dda9b2SRandall Stewart if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDR_SIZE, 4445dc6a815SMichael Tuexen &size_of_addresses, &opt_len) != 0) { 445d6dda9b2SRandall Stewart errno = ENOMEM; 446d6dda9b2SRandall Stewart return (-1); 447d6dda9b2SRandall Stewart } 448d6dda9b2SRandall Stewart if (size_of_addresses == 0) { 449d6dda9b2SRandall Stewart errno = ENOTCONN; 450d6dda9b2SRandall Stewart return (-1); 451d6dda9b2SRandall Stewart } 4525dc6a815SMichael Tuexen opt_len = (socklen_t) (size_of_addresses + 4535dc6a815SMichael Tuexen sizeof(struct sockaddr_storage) + 4545dc6a815SMichael Tuexen sizeof(struct sctp_getaddresses)); 4555dc6a815SMichael Tuexen addrs = calloc(1, (size_t)opt_len); 456d6dda9b2SRandall Stewart if (addrs == NULL) { 457d6dda9b2SRandall Stewart errno = ENOMEM; 458d6dda9b2SRandall Stewart return (-1); 459d6dda9b2SRandall Stewart } 460d6dda9b2SRandall Stewart addrs->sget_assoc_id = id; 461d6dda9b2SRandall Stewart /* Now lets get the array of addresses */ 462d6dda9b2SRandall Stewart if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDRESSES, addrs, 4635dc6a815SMichael Tuexen &opt_len) != 0) { 464d6dda9b2SRandall Stewart free(addrs); 465d6dda9b2SRandall Stewart errno = ENOMEM; 466d6dda9b2SRandall Stewart return (-1); 467d6dda9b2SRandall Stewart } 4685dc6a815SMichael Tuexen *raddrs = (struct sockaddr *)&addrs->addr[0]; 469d6dda9b2SRandall Stewart cnt = 0; 470d6dda9b2SRandall Stewart sa = (struct sockaddr *)&addrs->addr[0]; 4715dc6a815SMichael Tuexen lim = (caddr_t)addrs + opt_len; 472d6dda9b2SRandall Stewart while (((caddr_t)sa < lim) && (sa->sa_len > 0)) { 473d6dda9b2SRandall Stewart sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); 474d6dda9b2SRandall Stewart cnt++; 475d6dda9b2SRandall Stewart } 476d6dda9b2SRandall Stewart return (cnt); 477d6dda9b2SRandall Stewart } 478d6dda9b2SRandall Stewart 479d6dda9b2SRandall Stewart void 480d6dda9b2SRandall Stewart sctp_freeladdrs(struct sockaddr *addrs) 481d6dda9b2SRandall Stewart { 482d6dda9b2SRandall Stewart void *fr_addr; 483d6dda9b2SRandall Stewart 4847f15a8dfSMichael Tuexen /* Take away the hidden association id */ 485d6dda9b2SRandall Stewart fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t)); 486d6dda9b2SRandall Stewart /* Now free it */ 487d6dda9b2SRandall Stewart free(fr_addr); 488d6dda9b2SRandall Stewart } 489d6dda9b2SRandall Stewart 490d6dda9b2SRandall Stewart ssize_t 491d6dda9b2SRandall Stewart sctp_sendmsg(int s, 492d6dda9b2SRandall Stewart const void *data, 493d6dda9b2SRandall Stewart size_t len, 494d6dda9b2SRandall Stewart const struct sockaddr *to, 495002b1f0bSRandall Stewart socklen_t tolen, 4963d36ac98SRebecca Cran uint32_t ppid, 4973d36ac98SRebecca Cran uint32_t flags, 4983d36ac98SRebecca Cran uint16_t stream_no, 4993d36ac98SRebecca Cran uint32_t timetolive, 5003d36ac98SRebecca Cran uint32_t context) 501d6dda9b2SRandall Stewart { 502d6dda9b2SRandall Stewart #ifdef SYS_sctp_generic_sendmsg 503d6dda9b2SRandall Stewart struct sctp_sndrcvinfo sinfo; 504d6dda9b2SRandall Stewart 505539bb45aSMichael Tuexen memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo)); 506d6dda9b2SRandall Stewart sinfo.sinfo_ppid = ppid; 507d6dda9b2SRandall Stewart sinfo.sinfo_flags = flags; 508d6dda9b2SRandall Stewart sinfo.sinfo_stream = stream_no; 509d6dda9b2SRandall Stewart sinfo.sinfo_timetolive = timetolive; 510d6dda9b2SRandall Stewart sinfo.sinfo_context = context; 511d6dda9b2SRandall Stewart sinfo.sinfo_assoc_id = 0; 512d6dda9b2SRandall Stewart return (syscall(SYS_sctp_generic_sendmsg, s, 513d6dda9b2SRandall Stewart data, len, to, tolen, &sinfo, 0)); 514d6dda9b2SRandall Stewart #else 515d6dda9b2SRandall Stewart struct msghdr msg; 5167f15a8dfSMichael Tuexen struct sctp_sndrcvinfo *sinfo; 51748f65f00SMichael Tuexen struct iovec iov; 5187f15a8dfSMichael Tuexen char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; 519d6dda9b2SRandall Stewart struct cmsghdr *cmsg; 520d6dda9b2SRandall Stewart struct sockaddr *who = NULL; 521d6dda9b2SRandall Stewart union { 522d6dda9b2SRandall Stewart struct sockaddr_in in; 523d6dda9b2SRandall Stewart struct sockaddr_in6 in6; 524d6dda9b2SRandall Stewart } addr; 525d6dda9b2SRandall Stewart 52648f65f00SMichael Tuexen if ((tolen > 0) && 52748f65f00SMichael Tuexen ((to == NULL) || (tolen < sizeof(struct sockaddr)))) { 528002b1f0bSRandall Stewart errno = EINVAL; 5294224e03aSMichael Tuexen return (-1); 530002b1f0bSRandall Stewart } 5317f15a8dfSMichael Tuexen if ((to != NULL) && (tolen > 0)) { 5327f15a8dfSMichael Tuexen switch (to->sa_family) { 5337f15a8dfSMichael Tuexen case AF_INET: 534002b1f0bSRandall Stewart if (tolen != sizeof(struct sockaddr_in)) { 535002b1f0bSRandall Stewart errno = EINVAL; 5364224e03aSMichael Tuexen return (-1); 537002b1f0bSRandall Stewart } 53848f65f00SMichael Tuexen if ((to->sa_len > 0) && 53948f65f00SMichael Tuexen (to->sa_len != sizeof(struct sockaddr_in))) { 540002b1f0bSRandall Stewart errno = EINVAL; 5414224e03aSMichael Tuexen return (-1); 542002b1f0bSRandall Stewart } 543d6dda9b2SRandall Stewart memcpy(&addr, to, sizeof(struct sockaddr_in)); 544d6dda9b2SRandall Stewart addr.in.sin_len = sizeof(struct sockaddr_in); 5457f15a8dfSMichael Tuexen break; 5467f15a8dfSMichael Tuexen case AF_INET6: 547002b1f0bSRandall Stewart if (tolen != sizeof(struct sockaddr_in6)) { 548002b1f0bSRandall Stewart errno = EINVAL; 5494224e03aSMichael Tuexen return (-1); 550002b1f0bSRandall Stewart } 55148f65f00SMichael Tuexen if ((to->sa_len > 0) && 55248f65f00SMichael Tuexen (to->sa_len != sizeof(struct sockaddr_in6))) { 553002b1f0bSRandall Stewart errno = EINVAL; 5544224e03aSMichael Tuexen return (-1); 555002b1f0bSRandall Stewart } 556d6dda9b2SRandall Stewart memcpy(&addr, to, sizeof(struct sockaddr_in6)); 557d6dda9b2SRandall Stewart addr.in6.sin6_len = sizeof(struct sockaddr_in6); 5587f15a8dfSMichael Tuexen break; 5597f15a8dfSMichael Tuexen default: 560002b1f0bSRandall Stewart errno = EAFNOSUPPORT; 5614224e03aSMichael Tuexen return (-1); 562d6dda9b2SRandall Stewart } 563d6dda9b2SRandall Stewart who = (struct sockaddr *)&addr; 564d6dda9b2SRandall Stewart } 56548f65f00SMichael Tuexen iov.iov_base = (char *)data; 56648f65f00SMichael Tuexen iov.iov_len = len; 567d6dda9b2SRandall Stewart 568002b1f0bSRandall Stewart if (who) { 569d6dda9b2SRandall Stewart msg.msg_name = (caddr_t)who; 570d6dda9b2SRandall Stewart msg.msg_namelen = who->sa_len; 571d6dda9b2SRandall Stewart } else { 572d6dda9b2SRandall Stewart msg.msg_name = (caddr_t)NULL; 573d6dda9b2SRandall Stewart msg.msg_namelen = 0; 574d6dda9b2SRandall Stewart } 57548f65f00SMichael Tuexen msg.msg_iov = &iov; 576d6dda9b2SRandall Stewart msg.msg_iovlen = 1; 5777f15a8dfSMichael Tuexen msg.msg_control = cmsgbuf; 5787f15a8dfSMichael Tuexen msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)); 5797f15a8dfSMichael Tuexen cmsg = (struct cmsghdr *)cmsgbuf; 580d6dda9b2SRandall Stewart cmsg->cmsg_level = IPPROTO_SCTP; 581d6dda9b2SRandall Stewart cmsg->cmsg_type = SCTP_SNDRCV; 582d6dda9b2SRandall Stewart cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); 5837f15a8dfSMichael Tuexen sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg); 5847f15a8dfSMichael Tuexen sinfo->sinfo_stream = stream_no; 5857f15a8dfSMichael Tuexen sinfo->sinfo_ssn = 0; 5867f15a8dfSMichael Tuexen sinfo->sinfo_flags = flags; 5877f15a8dfSMichael Tuexen sinfo->sinfo_ppid = ppid; 5887f15a8dfSMichael Tuexen sinfo->sinfo_context = context; 5897f15a8dfSMichael Tuexen sinfo->sinfo_assoc_id = 0; 5907f15a8dfSMichael Tuexen sinfo->sinfo_timetolive = timetolive; 5917f15a8dfSMichael Tuexen return (sendmsg(s, &msg, 0)); 592d6dda9b2SRandall Stewart #endif 593d6dda9b2SRandall Stewart } 594d6dda9b2SRandall Stewart 595d6dda9b2SRandall Stewart 596d6dda9b2SRandall Stewart sctp_assoc_t 597d6dda9b2SRandall Stewart sctp_getassocid(int sd, struct sockaddr *sa) 598d6dda9b2SRandall Stewart { 599b54d3a6cSRandall Stewart struct sctp_paddrinfo sp; 600066f54d8SCraig Rodrigues socklen_t siz; 601d6dda9b2SRandall Stewart 602d6dda9b2SRandall Stewart /* First get the assoc id */ 603b54d3a6cSRandall Stewart siz = sizeof(sp); 604d6dda9b2SRandall Stewart memset(&sp, 0, sizeof(sp)); 605b54d3a6cSRandall Stewart memcpy((caddr_t)&sp.spinfo_address, sa, sa->sa_len); 606d6dda9b2SRandall Stewart if (getsockopt(sd, IPPROTO_SCTP, 607b54d3a6cSRandall Stewart SCTP_GET_PEER_ADDR_INFO, &sp, &siz) != 0) { 6087f15a8dfSMichael Tuexen /* We depend on the fact that 0 can never be returned */ 609d6dda9b2SRandall Stewart return ((sctp_assoc_t) 0); 610d6dda9b2SRandall Stewart } 611b54d3a6cSRandall Stewart return (sp.spinfo_assoc_id); 612d6dda9b2SRandall Stewart } 613d6dda9b2SRandall Stewart 614d6dda9b2SRandall Stewart ssize_t 615d6dda9b2SRandall Stewart sctp_send(int sd, const void *data, size_t len, 616d6dda9b2SRandall Stewart const struct sctp_sndrcvinfo *sinfo, 617d6dda9b2SRandall Stewart int flags) 618d6dda9b2SRandall Stewart { 619d6dda9b2SRandall Stewart 620d6dda9b2SRandall Stewart #ifdef SYS_sctp_generic_sendmsg 621d6dda9b2SRandall Stewart struct sockaddr *to = NULL; 622d6dda9b2SRandall Stewart 623d6dda9b2SRandall Stewart return (syscall(SYS_sctp_generic_sendmsg, sd, 624d6dda9b2SRandall Stewart data, len, to, 0, sinfo, flags)); 625d6dda9b2SRandall Stewart #else 626d6dda9b2SRandall Stewart struct msghdr msg; 62748f65f00SMichael Tuexen struct iovec iov; 6287f15a8dfSMichael Tuexen char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))]; 629d6dda9b2SRandall Stewart struct cmsghdr *cmsg; 630d6dda9b2SRandall Stewart 631d6dda9b2SRandall Stewart if (sinfo == NULL) { 632b54d3a6cSRandall Stewart errno = EINVAL; 633b54d3a6cSRandall Stewart return (-1); 634d6dda9b2SRandall Stewart } 63548f65f00SMichael Tuexen iov.iov_base = (char *)data; 63648f65f00SMichael Tuexen iov.iov_len = len; 637d6dda9b2SRandall Stewart 6387f15a8dfSMichael Tuexen msg.msg_name = NULL; 639d6dda9b2SRandall Stewart msg.msg_namelen = 0; 64048f65f00SMichael Tuexen msg.msg_iov = &iov; 641d6dda9b2SRandall Stewart msg.msg_iovlen = 1; 6427f15a8dfSMichael Tuexen msg.msg_control = cmsgbuf; 6437f15a8dfSMichael Tuexen msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo)); 6447f15a8dfSMichael Tuexen cmsg = (struct cmsghdr *)cmsgbuf; 645d6dda9b2SRandall Stewart cmsg->cmsg_level = IPPROTO_SCTP; 646d6dda9b2SRandall Stewart cmsg->cmsg_type = SCTP_SNDRCV; 647d6dda9b2SRandall Stewart cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo)); 6487f15a8dfSMichael Tuexen memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo)); 6497f15a8dfSMichael Tuexen return (sendmsg(sd, &msg, flags)); 650d6dda9b2SRandall Stewart #endif 651d6dda9b2SRandall Stewart } 652d6dda9b2SRandall Stewart 653d6dda9b2SRandall Stewart 654d6dda9b2SRandall Stewart 655d6dda9b2SRandall Stewart ssize_t 656d6dda9b2SRandall Stewart sctp_sendx(int sd, const void *msg, size_t msg_len, 657d6dda9b2SRandall Stewart struct sockaddr *addrs, int addrcnt, 658d6dda9b2SRandall Stewart struct sctp_sndrcvinfo *sinfo, 659d6dda9b2SRandall Stewart int flags) 660d6dda9b2SRandall Stewart { 661335a2d00SRandall Stewart struct sctp_sndrcvinfo __sinfo; 662d6dda9b2SRandall Stewart ssize_t ret; 663d6dda9b2SRandall Stewart int i, cnt, *aa, saved_errno; 664d6dda9b2SRandall Stewart char *buf; 6655dc6a815SMichael Tuexen int no_end_cx = 0; 6665dc6a815SMichael Tuexen size_t len, add_len; 667d6dda9b2SRandall Stewart struct sockaddr *at; 668d6dda9b2SRandall Stewart 6691b649582SRandall Stewart if (addrs == NULL) { 6701b649582SRandall Stewart errno = EINVAL; 6711b649582SRandall Stewart return (-1); 6721b649582SRandall Stewart } 673804cf641SRandall Stewart #ifdef SYS_sctp_generic_sendmsg 67448f65f00SMichael Tuexen if (addrcnt == 1) { 675804cf641SRandall Stewart socklen_t l; 676804cf641SRandall Stewart 677804cf641SRandall Stewart /* 678804cf641SRandall Stewart * Quick way, we don't need to do a connectx so lets use the 679804cf641SRandall Stewart * syscall directly. 680804cf641SRandall Stewart */ 681804cf641SRandall Stewart l = addrs->sa_len; 682804cf641SRandall Stewart return (syscall(SYS_sctp_generic_sendmsg, sd, 683804cf641SRandall Stewart msg, msg_len, addrs, l, sinfo, flags)); 684804cf641SRandall Stewart } 685804cf641SRandall Stewart #endif 686b54d3a6cSRandall Stewart 687d6dda9b2SRandall Stewart len = sizeof(int); 688d6dda9b2SRandall Stewart at = addrs; 689d6dda9b2SRandall Stewart cnt = 0; 690d6dda9b2SRandall Stewart /* validate all the addresses and get the size */ 691d6dda9b2SRandall Stewart for (i = 0; i < addrcnt; i++) { 692d6dda9b2SRandall Stewart if (at->sa_family == AF_INET) { 693d6dda9b2SRandall Stewart add_len = sizeof(struct sockaddr_in); 694d6dda9b2SRandall Stewart } else if (at->sa_family == AF_INET6) { 695d6dda9b2SRandall Stewart add_len = sizeof(struct sockaddr_in6); 696d6dda9b2SRandall Stewart } else { 697d6dda9b2SRandall Stewart errno = EINVAL; 698d6dda9b2SRandall Stewart return (-1); 699d6dda9b2SRandall Stewart } 700d6dda9b2SRandall Stewart len += add_len; 701d6dda9b2SRandall Stewart at = (struct sockaddr *)((caddr_t)at + add_len); 702d6dda9b2SRandall Stewart cnt++; 703d6dda9b2SRandall Stewart } 704d6dda9b2SRandall Stewart /* do we have any? */ 705d6dda9b2SRandall Stewart if (cnt == 0) { 706d6dda9b2SRandall Stewart errno = EINVAL; 707d6dda9b2SRandall Stewart return (-1); 708d6dda9b2SRandall Stewart } 709d6dda9b2SRandall Stewart buf = malloc(len); 710d6dda9b2SRandall Stewart if (buf == NULL) { 7114ed0ebf6SMichael Tuexen errno = ENOMEM; 712b54d3a6cSRandall Stewart return (-1); 713d6dda9b2SRandall Stewart } 714d6dda9b2SRandall Stewart aa = (int *)buf; 715d6dda9b2SRandall Stewart *aa = cnt; 716d6dda9b2SRandall Stewart aa++; 7175dc6a815SMichael Tuexen memcpy((caddr_t)aa, addrs, (size_t)(len - sizeof(int))); 718d6dda9b2SRandall Stewart ret = setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_DELAYED, (void *)buf, 719d6dda9b2SRandall Stewart (socklen_t) len); 720d6dda9b2SRandall Stewart 721d6dda9b2SRandall Stewart free(buf); 722d6dda9b2SRandall Stewart if (ret != 0) { 723d6dda9b2SRandall Stewart if (errno == EALREADY) { 724ecf4b67aSRebecca Cran no_end_cx = 1; 725d6dda9b2SRandall Stewart goto continue_send; 726d6dda9b2SRandall Stewart } 727d6dda9b2SRandall Stewart return (ret); 728d6dda9b2SRandall Stewart } 729d6dda9b2SRandall Stewart continue_send: 730335a2d00SRandall Stewart if (sinfo == NULL) { 731335a2d00SRandall Stewart sinfo = &__sinfo; 732335a2d00SRandall Stewart memset(&__sinfo, 0, sizeof(__sinfo)); 733335a2d00SRandall Stewart } 734d6dda9b2SRandall Stewart sinfo->sinfo_assoc_id = sctp_getassocid(sd, addrs); 735d6dda9b2SRandall Stewart if (sinfo->sinfo_assoc_id == 0) { 736d6dda9b2SRandall Stewart (void)setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_COMPLETE, (void *)addrs, 737d6dda9b2SRandall Stewart (socklen_t) addrs->sa_len); 738d6dda9b2SRandall Stewart errno = ENOENT; 739d6dda9b2SRandall Stewart return (-1); 740d6dda9b2SRandall Stewart } 741d6dda9b2SRandall Stewart ret = sctp_send(sd, msg, msg_len, sinfo, flags); 742d6dda9b2SRandall Stewart saved_errno = errno; 743d6dda9b2SRandall Stewart if (no_end_cx == 0) 744d6dda9b2SRandall Stewart (void)setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_COMPLETE, (void *)addrs, 745d6dda9b2SRandall Stewart (socklen_t) addrs->sa_len); 746d6dda9b2SRandall Stewart 747d6dda9b2SRandall Stewart errno = saved_errno; 748d6dda9b2SRandall Stewart return (ret); 749d6dda9b2SRandall Stewart } 750d6dda9b2SRandall Stewart 751d6dda9b2SRandall Stewart ssize_t 752d6dda9b2SRandall Stewart sctp_sendmsgx(int sd, 753d6dda9b2SRandall Stewart const void *msg, 754d6dda9b2SRandall Stewart size_t len, 755d6dda9b2SRandall Stewart struct sockaddr *addrs, 756d6dda9b2SRandall Stewart int addrcnt, 7573d36ac98SRebecca Cran uint32_t ppid, 7583d36ac98SRebecca Cran uint32_t flags, 7593d36ac98SRebecca Cran uint16_t stream_no, 7603d36ac98SRebecca Cran uint32_t timetolive, 7613d36ac98SRebecca Cran uint32_t context) 762d6dda9b2SRandall Stewart { 763d6dda9b2SRandall Stewart struct sctp_sndrcvinfo sinfo; 764d6dda9b2SRandall Stewart 765d6dda9b2SRandall Stewart memset((void *)&sinfo, 0, sizeof(struct sctp_sndrcvinfo)); 766d6dda9b2SRandall Stewart sinfo.sinfo_ppid = ppid; 767d6dda9b2SRandall Stewart sinfo.sinfo_flags = flags; 768d6dda9b2SRandall Stewart sinfo.sinfo_ssn = stream_no; 769d6dda9b2SRandall Stewart sinfo.sinfo_timetolive = timetolive; 770d6dda9b2SRandall Stewart sinfo.sinfo_context = context; 7714224e03aSMichael Tuexen return (sctp_sendx(sd, msg, len, addrs, addrcnt, &sinfo, 0)); 772d6dda9b2SRandall Stewart } 773d6dda9b2SRandall Stewart 774d6dda9b2SRandall Stewart ssize_t 775d6dda9b2SRandall Stewart sctp_recvmsg(int s, 776d6dda9b2SRandall Stewart void *dbuf, 777d6dda9b2SRandall Stewart size_t len, 778d6dda9b2SRandall Stewart struct sockaddr *from, 779d6dda9b2SRandall Stewart socklen_t * fromlen, 780d6dda9b2SRandall Stewart struct sctp_sndrcvinfo *sinfo, 781d6dda9b2SRandall Stewart int *msg_flags) 782d6dda9b2SRandall Stewart { 783d6dda9b2SRandall Stewart #ifdef SYS_sctp_generic_recvmsg 78448f65f00SMichael Tuexen struct iovec iov; 785d6dda9b2SRandall Stewart 78648f65f00SMichael Tuexen iov.iov_base = dbuf; 78748f65f00SMichael Tuexen iov.iov_len = len; 788d6dda9b2SRandall Stewart return (syscall(SYS_sctp_generic_recvmsg, s, 78948f65f00SMichael Tuexen &iov, 1, from, fromlen, sinfo, msg_flags)); 790d6dda9b2SRandall Stewart #else 791d6dda9b2SRandall Stewart ssize_t sz; 792d6dda9b2SRandall Stewart struct msghdr msg; 79348f65f00SMichael Tuexen struct iovec iov; 7947f15a8dfSMichael Tuexen char cmsgbuf[SCTP_CONTROL_VEC_SIZE_RCV]; 795d6dda9b2SRandall Stewart struct cmsghdr *cmsg; 796d6dda9b2SRandall Stewart 797d6dda9b2SRandall Stewart if (msg_flags == NULL) { 798d6dda9b2SRandall Stewart errno = EINVAL; 799d6dda9b2SRandall Stewart return (-1); 800d6dda9b2SRandall Stewart } 801d6dda9b2SRandall Stewart msg.msg_flags = 0; 80248f65f00SMichael Tuexen iov.iov_base = dbuf; 80348f65f00SMichael Tuexen iov.iov_len = len; 804d6dda9b2SRandall Stewart msg.msg_name = (caddr_t)from; 805d6dda9b2SRandall Stewart if (fromlen == NULL) 806d6dda9b2SRandall Stewart msg.msg_namelen = 0; 807d6dda9b2SRandall Stewart else 808d6dda9b2SRandall Stewart msg.msg_namelen = *fromlen; 80948f65f00SMichael Tuexen msg.msg_iov = &iov; 810d6dda9b2SRandall Stewart msg.msg_iovlen = 1; 8117f15a8dfSMichael Tuexen msg.msg_control = cmsgbuf; 8127f15a8dfSMichael Tuexen msg.msg_controllen = sizeof(cmsgbuf); 8132c356be2SRandall Stewart sz = recvmsg(s, &msg, *msg_flags); 81448f65f00SMichael Tuexen *msg_flags = msg.msg_flags; 81548f65f00SMichael Tuexen if (sz <= 0) { 816d6dda9b2SRandall Stewart return (sz); 81748f65f00SMichael Tuexen } 81848f65f00SMichael Tuexen if (sinfo) { 819d6dda9b2SRandall Stewart sinfo->sinfo_assoc_id = 0; 82048f65f00SMichael Tuexen } 8217f15a8dfSMichael Tuexen if ((msg.msg_controllen > 0) && (sinfo != NULL)) { 822d6dda9b2SRandall Stewart /* 823d6dda9b2SRandall Stewart * parse through and see if we find the sctp_sndrcvinfo (if 824d6dda9b2SRandall Stewart * the user wants it). 825d6dda9b2SRandall Stewart */ 8267f15a8dfSMichael Tuexen for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 8277f15a8dfSMichael Tuexen if (cmsg->cmsg_level != IPPROTO_SCTP) { 8287f15a8dfSMichael Tuexen continue; 829d6dda9b2SRandall Stewart } 830d6dda9b2SRandall Stewart if (cmsg->cmsg_type == SCTP_SNDRCV) { 8317f15a8dfSMichael Tuexen memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo)); 832d6dda9b2SRandall Stewart break; 8337f15a8dfSMichael Tuexen } 8347f15a8dfSMichael Tuexen if (cmsg->cmsg_type == SCTP_EXTRCV) { 835d6dda9b2SRandall Stewart /* 8367f15a8dfSMichael Tuexen * Let's hope that the user provided enough 8377f15a8dfSMichael Tuexen * enough memory. At least he asked for more 8387f15a8dfSMichael Tuexen * information. 839d6dda9b2SRandall Stewart */ 8407f15a8dfSMichael Tuexen memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_extrcvinfo)); 841d6dda9b2SRandall Stewart break; 842d6dda9b2SRandall Stewart } 843d6dda9b2SRandall Stewart } 844d6dda9b2SRandall Stewart } 845d6dda9b2SRandall Stewart return (sz); 846d6dda9b2SRandall Stewart #endif 847d6dda9b2SRandall Stewart } 848d6dda9b2SRandall Stewart 849e2e7c62eSMichael Tuexen ssize_t 850e2e7c62eSMichael Tuexen sctp_recvv(int sd, 851e2e7c62eSMichael Tuexen const struct iovec *iov, 852e2e7c62eSMichael Tuexen int iovlen, 853e2e7c62eSMichael Tuexen struct sockaddr *from, 854e2e7c62eSMichael Tuexen socklen_t * fromlen, 855e2e7c62eSMichael Tuexen void *info, 856e2e7c62eSMichael Tuexen socklen_t * infolen, 857e2e7c62eSMichael Tuexen unsigned int *infotype, 858e2e7c62eSMichael Tuexen int *flags) 859d6dda9b2SRandall Stewart { 8607f15a8dfSMichael Tuexen char cmsgbuf[SCTP_CONTROL_VEC_SIZE_RCV]; 861e2e7c62eSMichael Tuexen struct msghdr msg; 862e2e7c62eSMichael Tuexen struct cmsghdr *cmsg; 8637f15a8dfSMichael Tuexen ssize_t ret; 864e2e7c62eSMichael Tuexen struct sctp_rcvinfo *rcvinfo; 865e2e7c62eSMichael Tuexen struct sctp_nxtinfo *nxtinfo; 866d6dda9b2SRandall Stewart 8670b064106SMichael Tuexen if (((info != NULL) && (infolen == NULL)) | 8680b064106SMichael Tuexen ((info == NULL) && (infolen != NULL) && (*infolen != 0)) || 8690b064106SMichael Tuexen ((info != NULL) && (infotype == NULL))) { 8700b064106SMichael Tuexen errno = EINVAL; 8710b064106SMichael Tuexen return (-1); 8720b064106SMichael Tuexen } 873e2e7c62eSMichael Tuexen if (infotype) { 874e2e7c62eSMichael Tuexen *infotype = SCTP_RECVV_NOINFO; 875e2e7c62eSMichael Tuexen } 876e2e7c62eSMichael Tuexen msg.msg_name = from; 877e2e7c62eSMichael Tuexen if (fromlen == NULL) { 878e2e7c62eSMichael Tuexen msg.msg_namelen = 0; 879d6dda9b2SRandall Stewart } else { 880e2e7c62eSMichael Tuexen msg.msg_namelen = *fromlen; 881d6dda9b2SRandall Stewart } 882e2e7c62eSMichael Tuexen msg.msg_iov = (struct iovec *)iov; 883e2e7c62eSMichael Tuexen msg.msg_iovlen = iovlen; 8847f15a8dfSMichael Tuexen msg.msg_control = cmsgbuf; 8857f15a8dfSMichael Tuexen msg.msg_controllen = sizeof(cmsgbuf); 8867f15a8dfSMichael Tuexen ret = recvmsg(sd, &msg, *flags); 887e2e7c62eSMichael Tuexen *flags = msg.msg_flags; 8887f15a8dfSMichael Tuexen if ((ret > 0) && 889e2e7c62eSMichael Tuexen (msg.msg_controllen > 0) && 890e2e7c62eSMichael Tuexen (infotype != NULL) && 891e2e7c62eSMichael Tuexen (infolen != NULL) && 892e2e7c62eSMichael Tuexen (*infolen > 0)) { 893e2e7c62eSMichael Tuexen rcvinfo = NULL; 894e2e7c62eSMichael Tuexen nxtinfo = NULL; 895e2e7c62eSMichael Tuexen for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { 896e2e7c62eSMichael Tuexen if (cmsg->cmsg_level != IPPROTO_SCTP) { 897e2e7c62eSMichael Tuexen continue; 898e2e7c62eSMichael Tuexen } 899e2e7c62eSMichael Tuexen if (cmsg->cmsg_type == SCTP_RCVINFO) { 900e2e7c62eSMichael Tuexen rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg); 9017f15a8dfSMichael Tuexen if (nxtinfo != NULL) { 9027f15a8dfSMichael Tuexen break; 9037f15a8dfSMichael Tuexen } else { 9047f15a8dfSMichael Tuexen continue; 9057f15a8dfSMichael Tuexen } 906e2e7c62eSMichael Tuexen } 907e2e7c62eSMichael Tuexen if (cmsg->cmsg_type == SCTP_NXTINFO) { 908e2e7c62eSMichael Tuexen nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmsg); 9097f15a8dfSMichael Tuexen if (rcvinfo != NULL) { 910e2e7c62eSMichael Tuexen break; 9117f15a8dfSMichael Tuexen } else { 9127f15a8dfSMichael Tuexen continue; 913e2e7c62eSMichael Tuexen } 914e2e7c62eSMichael Tuexen } 9157f15a8dfSMichael Tuexen } 9167f15a8dfSMichael Tuexen if (rcvinfo != NULL) { 9177f15a8dfSMichael Tuexen if ((nxtinfo != NULL) && (*infolen >= sizeof(struct sctp_recvv_rn))) { 918e2e7c62eSMichael Tuexen struct sctp_recvv_rn *rn_info; 919e2e7c62eSMichael Tuexen 920e2e7c62eSMichael Tuexen rn_info = (struct sctp_recvv_rn *)info; 921e2e7c62eSMichael Tuexen rn_info->recvv_rcvinfo = *rcvinfo; 922e2e7c62eSMichael Tuexen rn_info->recvv_nxtinfo = *nxtinfo; 923e2e7c62eSMichael Tuexen *infolen = (socklen_t) sizeof(struct sctp_recvv_rn); 924e2e7c62eSMichael Tuexen *infotype = SCTP_RECVV_RN; 9257f15a8dfSMichael Tuexen } else if (*infolen >= sizeof(struct sctp_rcvinfo)) { 926e2e7c62eSMichael Tuexen memcpy(info, rcvinfo, sizeof(struct sctp_rcvinfo)); 927e2e7c62eSMichael Tuexen *infolen = (socklen_t) sizeof(struct sctp_rcvinfo); 928e2e7c62eSMichael Tuexen *infotype = SCTP_RECVV_RCVINFO; 929e2e7c62eSMichael Tuexen } 9307f15a8dfSMichael Tuexen } else if (nxtinfo != NULL) { 9317f15a8dfSMichael Tuexen if (*infolen >= sizeof(struct sctp_nxtinfo)) { 932e2e7c62eSMichael Tuexen memcpy(info, nxtinfo, sizeof(struct sctp_nxtinfo)); 933e2e7c62eSMichael Tuexen *infolen = (socklen_t) sizeof(struct sctp_nxtinfo); 934e2e7c62eSMichael Tuexen *infotype = SCTP_RECVV_NXTINFO; 935e2e7c62eSMichael Tuexen } 936e2e7c62eSMichael Tuexen } 937e2e7c62eSMichael Tuexen } 9387f15a8dfSMichael Tuexen return (ret); 939d6dda9b2SRandall Stewart } 940d6dda9b2SRandall Stewart 941e2e7c62eSMichael Tuexen ssize_t 942e2e7c62eSMichael Tuexen sctp_sendv(int sd, 943e2e7c62eSMichael Tuexen const struct iovec *iov, int iovcnt, 944e2e7c62eSMichael Tuexen struct sockaddr *addrs, int addrcnt, 945e2e7c62eSMichael Tuexen void *info, socklen_t infolen, unsigned int infotype, 946e2e7c62eSMichael Tuexen int flags) 947e2e7c62eSMichael Tuexen { 948e2e7c62eSMichael Tuexen ssize_t ret; 949e2e7c62eSMichael Tuexen int i; 9500b064106SMichael Tuexen socklen_t addr_len; 951e2e7c62eSMichael Tuexen struct msghdr msg; 9520b064106SMichael Tuexen in_port_t port; 9530b064106SMichael Tuexen struct sctp_sendv_spa *spa_info; 954e2e7c62eSMichael Tuexen struct cmsghdr *cmsg; 955e2e7c62eSMichael Tuexen char *cmsgbuf; 956e2e7c62eSMichael Tuexen struct sockaddr *addr; 957e2e7c62eSMichael Tuexen struct sockaddr_in *addr_in; 958e2e7c62eSMichael Tuexen struct sockaddr_in6 *addr_in6; 959e2e7c62eSMichael Tuexen 9600b064106SMichael Tuexen if ((addrcnt < 0) || 9610b064106SMichael Tuexen (iovcnt < 0) || 962c67a03f9SMichael Tuexen ((addrs == NULL) && (addrcnt > 0)) || 963c67a03f9SMichael Tuexen ((addrs != NULL) && (addrcnt == 0)) || 9640b064106SMichael Tuexen ((iov == NULL) && (iovcnt > 0)) || 9650b064106SMichael Tuexen ((iov != NULL) && (iovcnt == 0))) { 966e2e7c62eSMichael Tuexen errno = EINVAL; 967e2e7c62eSMichael Tuexen return (-1); 968e2e7c62eSMichael Tuexen } 969e2e7c62eSMichael Tuexen cmsgbuf = malloc(CMSG_SPACE(sizeof(struct sctp_sndinfo)) + 970e2e7c62eSMichael Tuexen CMSG_SPACE(sizeof(struct sctp_prinfo)) + 971e2e7c62eSMichael Tuexen CMSG_SPACE(sizeof(struct sctp_authinfo)) + 9725dc6a815SMichael Tuexen (size_t)addrcnt * CMSG_SPACE(sizeof(struct in6_addr))); 973e2e7c62eSMichael Tuexen if (cmsgbuf == NULL) { 9744ed0ebf6SMichael Tuexen errno = ENOMEM; 975e2e7c62eSMichael Tuexen return (-1); 976e2e7c62eSMichael Tuexen } 977e2e7c62eSMichael Tuexen msg.msg_control = cmsgbuf; 978e2e7c62eSMichael Tuexen msg.msg_controllen = 0; 979e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)cmsgbuf; 980e2e7c62eSMichael Tuexen switch (infotype) { 9810b064106SMichael Tuexen case SCTP_SENDV_NOINFO: 9820b064106SMichael Tuexen if ((infolen != 0) || (info != NULL)) { 9830b064106SMichael Tuexen free(cmsgbuf); 9840b064106SMichael Tuexen errno = EINVAL; 9850b064106SMichael Tuexen return (-1); 9860b064106SMichael Tuexen } 9870b064106SMichael Tuexen break; 988e2e7c62eSMichael Tuexen case SCTP_SENDV_SNDINFO: 9890b064106SMichael Tuexen if ((info == NULL) || (infolen < sizeof(struct sctp_sndinfo))) { 990e2e7c62eSMichael Tuexen free(cmsgbuf); 991e2e7c62eSMichael Tuexen errno = EINVAL; 992e2e7c62eSMichael Tuexen return (-1); 993e2e7c62eSMichael Tuexen } 994e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 995e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_SNDINFO; 996e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo)); 997e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_sndinfo)); 998e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo)); 999e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo))); 1000e2e7c62eSMichael Tuexen break; 1001e2e7c62eSMichael Tuexen case SCTP_SENDV_PRINFO: 10020b064106SMichael Tuexen if ((info == NULL) || (infolen < sizeof(struct sctp_prinfo))) { 1003e2e7c62eSMichael Tuexen free(cmsgbuf); 1004e2e7c62eSMichael Tuexen errno = EINVAL; 1005e2e7c62eSMichael Tuexen return (-1); 1006e2e7c62eSMichael Tuexen } 1007e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1008e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_PRINFO; 1009e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo)); 1010e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_prinfo)); 1011e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo)); 1012e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo))); 1013e2e7c62eSMichael Tuexen break; 1014e2e7c62eSMichael Tuexen case SCTP_SENDV_AUTHINFO: 10150b064106SMichael Tuexen if ((info == NULL) || (infolen < sizeof(struct sctp_authinfo))) { 1016e2e7c62eSMichael Tuexen free(cmsgbuf); 1017e2e7c62eSMichael Tuexen errno = EINVAL; 1018e2e7c62eSMichael Tuexen return (-1); 1019e2e7c62eSMichael Tuexen } 1020e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1021e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_AUTHINFO; 1022e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo)); 1023e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_authinfo)); 1024e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo)); 1025e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo))); 1026e2e7c62eSMichael Tuexen break; 1027e2e7c62eSMichael Tuexen case SCTP_SENDV_SPA: 10280b064106SMichael Tuexen if ((info == NULL) || (infolen < sizeof(struct sctp_sendv_spa))) { 1029e2e7c62eSMichael Tuexen free(cmsgbuf); 1030e2e7c62eSMichael Tuexen errno = EINVAL; 1031e2e7c62eSMichael Tuexen return (-1); 1032e2e7c62eSMichael Tuexen } 1033e2e7c62eSMichael Tuexen spa_info = (struct sctp_sendv_spa *)info; 1034e2e7c62eSMichael Tuexen if (spa_info->sendv_flags & SCTP_SEND_SNDINFO_VALID) { 1035e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1036e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_SNDINFO; 1037e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo)); 1038e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), &spa_info->sendv_sndinfo, sizeof(struct sctp_sndinfo)); 1039e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo)); 1040e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo))); 1041e2e7c62eSMichael Tuexen } 1042e2e7c62eSMichael Tuexen if (spa_info->sendv_flags & SCTP_SEND_PRINFO_VALID) { 1043e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1044e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_PRINFO; 1045e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo)); 1046e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), &spa_info->sendv_prinfo, sizeof(struct sctp_prinfo)); 1047e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo)); 1048e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo))); 1049e2e7c62eSMichael Tuexen } 1050e2e7c62eSMichael Tuexen if (spa_info->sendv_flags & SCTP_SEND_AUTHINFO_VALID) { 1051e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1052e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_AUTHINFO; 1053e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo)); 1054e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), &spa_info->sendv_authinfo, sizeof(struct sctp_authinfo)); 1055e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo)); 1056e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo))); 1057e2e7c62eSMichael Tuexen } 1058e2e7c62eSMichael Tuexen break; 1059e2e7c62eSMichael Tuexen default: 1060e2e7c62eSMichael Tuexen free(cmsgbuf); 1061e2e7c62eSMichael Tuexen errno = EINVAL; 1062e2e7c62eSMichael Tuexen return (-1); 1063e2e7c62eSMichael Tuexen } 1064e2e7c62eSMichael Tuexen addr = addrs; 10650b064106SMichael Tuexen msg.msg_name = NULL; 10660b064106SMichael Tuexen msg.msg_namelen = 0; 10670b064106SMichael Tuexen 10680b064106SMichael Tuexen for (i = 0; i < addrcnt; i++) { 1069e2e7c62eSMichael Tuexen switch (addr->sa_family) { 1070e2e7c62eSMichael Tuexen case AF_INET: 10710b064106SMichael Tuexen addr_len = (socklen_t) sizeof(struct sockaddr_in); 10720b064106SMichael Tuexen addr_in = (struct sockaddr_in *)addr; 10730b064106SMichael Tuexen if (addr_in->sin_len != addr_len) { 1074e2e7c62eSMichael Tuexen free(cmsgbuf); 1075e2e7c62eSMichael Tuexen errno = EINVAL; 1076e2e7c62eSMichael Tuexen return (-1); 1077e2e7c62eSMichael Tuexen } 10780b064106SMichael Tuexen if (i == 0) { 10790b064106SMichael Tuexen port = addr_in->sin_port; 1080e2e7c62eSMichael Tuexen } else { 10810b064106SMichael Tuexen if (port == addr_in->sin_port) { 1082e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1083e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_DSTADDRV4; 1084e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr)); 1085e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), &addr_in->sin_addr, sizeof(struct in_addr)); 1086e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct in_addr)); 1087e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in_addr))); 10880b064106SMichael Tuexen } else { 10890b064106SMichael Tuexen free(cmsgbuf); 10900b064106SMichael Tuexen errno = EINVAL; 10910b064106SMichael Tuexen return (-1); 10920b064106SMichael Tuexen } 10930b064106SMichael Tuexen } 1094e2e7c62eSMichael Tuexen break; 1095e2e7c62eSMichael Tuexen case AF_INET6: 10960b064106SMichael Tuexen addr_len = (socklen_t) sizeof(struct sockaddr_in6); 1097e2e7c62eSMichael Tuexen addr_in6 = (struct sockaddr_in6 *)addr; 10980b064106SMichael Tuexen if (addr_in6->sin6_len != addr_len) { 10990b064106SMichael Tuexen free(cmsgbuf); 11000b064106SMichael Tuexen errno = EINVAL; 11010b064106SMichael Tuexen return (-1); 11020b064106SMichael Tuexen } 11030b064106SMichael Tuexen if (i == 0) { 11040b064106SMichael Tuexen port = addr_in6->sin6_port; 11050b064106SMichael Tuexen } else { 11060b064106SMichael Tuexen if (port == addr_in6->sin6_port) { 1107e2e7c62eSMichael Tuexen cmsg->cmsg_level = IPPROTO_SCTP; 1108e2e7c62eSMichael Tuexen cmsg->cmsg_type = SCTP_DSTADDRV6; 1109e2e7c62eSMichael Tuexen cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_addr)); 1110e2e7c62eSMichael Tuexen memcpy(CMSG_DATA(cmsg), &addr_in6->sin6_addr, sizeof(struct in6_addr)); 1111e2e7c62eSMichael Tuexen msg.msg_controllen += CMSG_SPACE(sizeof(struct in6_addr)); 1112e2e7c62eSMichael Tuexen cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in6_addr))); 11130b064106SMichael Tuexen } else { 11140b064106SMichael Tuexen free(cmsgbuf); 11150b064106SMichael Tuexen errno = EINVAL; 11160b064106SMichael Tuexen return (-1); 11170b064106SMichael Tuexen } 11180b064106SMichael Tuexen } 1119e2e7c62eSMichael Tuexen break; 1120e2e7c62eSMichael Tuexen default: 1121e2e7c62eSMichael Tuexen free(cmsgbuf); 1122e2e7c62eSMichael Tuexen errno = EINVAL; 1123e2e7c62eSMichael Tuexen return (-1); 1124e2e7c62eSMichael Tuexen } 11250b064106SMichael Tuexen if (i == 0) { 11260b064106SMichael Tuexen msg.msg_name = addr; 11270b064106SMichael Tuexen msg.msg_namelen = addr_len; 11280b064106SMichael Tuexen } 1129e2e7c62eSMichael Tuexen addr = (struct sockaddr *)((caddr_t)addr + addr_len); 1130e2e7c62eSMichael Tuexen } 11310b064106SMichael Tuexen if (msg.msg_controllen == 0) { 11320b064106SMichael Tuexen msg.msg_control = NULL; 1133e2e7c62eSMichael Tuexen } 1134e2e7c62eSMichael Tuexen msg.msg_iov = (struct iovec *)iov; 1135e2e7c62eSMichael Tuexen msg.msg_iovlen = iovcnt; 1136e2e7c62eSMichael Tuexen msg.msg_flags = 0; 1137e2e7c62eSMichael Tuexen ret = sendmsg(sd, &msg, flags); 1138e2e7c62eSMichael Tuexen free(cmsgbuf); 1139e2e7c62eSMichael Tuexen return (ret); 1140e2e7c62eSMichael Tuexen } 1141e2e7c62eSMichael Tuexen 1142d6dda9b2SRandall Stewart 1143d6dda9b2SRandall Stewart #if !defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT) 1144d6dda9b2SRandall Stewart 1145d6dda9b2SRandall Stewart int 1146d6dda9b2SRandall Stewart sctp_peeloff(int sd, sctp_assoc_t assoc_id) 1147d6dda9b2SRandall Stewart { 1148d6dda9b2SRandall Stewart /* NOT supported, return invalid sd */ 1149d6dda9b2SRandall Stewart errno = ENOTSUP; 1150d6dda9b2SRandall Stewart return (-1); 1151d6dda9b2SRandall Stewart } 1152d6dda9b2SRandall Stewart 1153d6dda9b2SRandall Stewart #endif 1154d6dda9b2SRandall Stewart #if defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT) 1155d6dda9b2SRandall Stewart int 1156d6dda9b2SRandall Stewart sctp_peeloff(int sd, sctp_assoc_t assoc_id) 1157d6dda9b2SRandall Stewart { 1158d6dda9b2SRandall Stewart return (syscall(SYS_sctp_peeloff, sd, assoc_id)); 1159d6dda9b2SRandall Stewart } 1160d6dda9b2SRandall Stewart 1161d6dda9b2SRandall Stewart #endif 1162804cf641SRandall Stewart 11632c0d559dSRandall Stewart #undef SCTP_CONTROL_VEC_SIZE_RCV 1164