xref: /freebsd/lib/libc/net/sctp_sys_calls.c (revision df2fca7ba89976ad70dc4fb4ea022959efa67308)
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 
38d6dda9b2SRandall Stewart #include <stdio.h>
39d6dda9b2SRandall Stewart #include <string.h>
40d6dda9b2SRandall Stewart #include <errno.h>
41d6dda9b2SRandall Stewart #include <stdlib.h>
42d6dda9b2SRandall Stewart #include <unistd.h>
43d6dda9b2SRandall Stewart #include <sys/types.h>
44d6dda9b2SRandall Stewart #include <sys/socket.h>
45d6dda9b2SRandall Stewart #include <sys/errno.h>
46d6dda9b2SRandall Stewart #include <sys/syscall.h>
47d6dda9b2SRandall Stewart #include <sys/uio.h>
48d6dda9b2SRandall Stewart #include <netinet/in.h>
49d6dda9b2SRandall Stewart #include <arpa/inet.h>
50d6dda9b2SRandall Stewart #include <netinet/sctp_uio.h>
51d6dda9b2SRandall Stewart #include <netinet/sctp.h>
52d6dda9b2SRandall Stewart 
53d6dda9b2SRandall Stewart #ifndef IN6_IS_ADDR_V4MAPPED
54d6dda9b2SRandall Stewart #define IN6_IS_ADDR_V4MAPPED(a)		      \
553d36ac98SRebecca Cran 	((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) &&	\
563d36ac98SRebecca Cran 	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) &&	\
573d36ac98SRebecca Cran 	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == ntohl(0x0000ffff)))
58d6dda9b2SRandall Stewart #endif
59d6dda9b2SRandall Stewart 
60d6dda9b2SRandall Stewart #define SCTP_CONTROL_VEC_SIZE_RCV  16384
61d6dda9b2SRandall Stewart 
62d6dda9b2SRandall Stewart 
63d6dda9b2SRandall Stewart static void
64d6dda9b2SRandall Stewart in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
65d6dda9b2SRandall Stewart {
66d6dda9b2SRandall Stewart 	bzero(sin, sizeof(*sin));
67d6dda9b2SRandall Stewart 	sin->sin_len = sizeof(struct sockaddr_in);
68d6dda9b2SRandall Stewart 	sin->sin_family = AF_INET;
69d6dda9b2SRandall Stewart 	sin->sin_port = sin6->sin6_port;
70d6dda9b2SRandall Stewart 	sin->sin_addr.s_addr = sin6->sin6_addr.__u6_addr.__u6_addr32[3];
71d6dda9b2SRandall Stewart }
72d6dda9b2SRandall Stewart 
73d6dda9b2SRandall Stewart int
74d6dda9b2SRandall Stewart sctp_getaddrlen(sa_family_t family)
75d6dda9b2SRandall Stewart {
76e2e7c62eSMichael Tuexen 	int ret, sd;
77d6dda9b2SRandall Stewart 	socklen_t siz;
78d6dda9b2SRandall Stewart 	struct sctp_assoc_value av;
79d6dda9b2SRandall Stewart 
80d6dda9b2SRandall Stewart 	av.assoc_value = family;
81d6dda9b2SRandall Stewart 	siz = sizeof(av);
82d6dda9b2SRandall Stewart #if defined(AF_INET)
83d6dda9b2SRandall Stewart 	sd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
84d6dda9b2SRandall Stewart #elif defined(AF_INET6)
85d6dda9b2SRandall Stewart 	sd = socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP);
86e2e7c62eSMichael Tuexen #else
87e2e7c62eSMichael Tuexen 	sd = -1;
88d6dda9b2SRandall Stewart #endif
89d6dda9b2SRandall Stewart 	if (sd == -1) {
90a593094eSRandall Stewart 		return (-1);
91d6dda9b2SRandall Stewart 	}
92e2e7c62eSMichael Tuexen 	ret = getsockopt(sd, IPPROTO_SCTP, SCTP_GET_ADDR_LEN, &av, &siz);
93d6dda9b2SRandall Stewart 	close(sd);
94e2e7c62eSMichael Tuexen 	if (ret == 0) {
95d6dda9b2SRandall Stewart 		return ((int)av.assoc_value);
96d6dda9b2SRandall Stewart 	} else {
97a593094eSRandall Stewart 		return (-1);
98d6dda9b2SRandall Stewart 	}
99d6dda9b2SRandall Stewart }
100d6dda9b2SRandall Stewart 
101d6dda9b2SRandall Stewart int
1022c356be2SRandall Stewart sctp_connectx(int sd, const struct sockaddr *addrs, int addrcnt,
1032c356be2SRandall Stewart     sctp_assoc_t *id)
104d6dda9b2SRandall Stewart {
1057f15a8dfSMichael Tuexen 	char *buf;
10610e6d832SMichael Tuexen 	int i, ret, *aa;
107d6dda9b2SRandall Stewart 	char *cpto;
108d6dda9b2SRandall Stewart 	const struct sockaddr *at;
10910e6d832SMichael Tuexen 	size_t len;
110d6dda9b2SRandall Stewart 
1112c356be2SRandall Stewart 	/* validate the address count and list */
1122c356be2SRandall Stewart 	if ((addrs == NULL) || (addrcnt <= 0)) {
1132c356be2SRandall Stewart 		errno = EINVAL;
1142c356be2SRandall Stewart 		return (-1);
1152c356be2SRandall Stewart 	}
1167f15a8dfSMichael Tuexen 	if ((buf = malloc(sizeof(int) + (size_t)addrcnt * sizeof(struct sockaddr_in6))) == NULL) {
1177f15a8dfSMichael Tuexen 		errno = E2BIG;
1187f15a8dfSMichael Tuexen 		return (-1);
1197f15a8dfSMichael Tuexen 	}
12010e6d832SMichael Tuexen 	len = sizeof(int);
121d6dda9b2SRandall Stewart 	at = addrs;
1227f15a8dfSMichael Tuexen 	cpto = buf + sizeof(int);
123d6dda9b2SRandall Stewart 	/* validate all the addresses and get the size */
124d6dda9b2SRandall Stewart 	for (i = 0; i < addrcnt; i++) {
12593409822SMichael Tuexen 		switch (at->sa_family) {
12693409822SMichael Tuexen 		case AF_INET:
12725d63f19SRandall Stewart 			if (at->sa_len != sizeof(struct sockaddr_in)) {
1287f15a8dfSMichael Tuexen 				free(buf);
12925d63f19SRandall Stewart 				errno = EINVAL;
13025d63f19SRandall Stewart 				return (-1);
13125d63f19SRandall Stewart 			}
13293409822SMichael Tuexen 			memcpy(cpto, at, sizeof(struct sockaddr_in));
13393409822SMichael Tuexen 			cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in));
13493409822SMichael Tuexen 			len += sizeof(struct sockaddr_in);
13593409822SMichael Tuexen 			break;
13693409822SMichael Tuexen 		case AF_INET6:
13725d63f19SRandall Stewart 			if (at->sa_len != sizeof(struct sockaddr_in6)) {
1387f15a8dfSMichael Tuexen 				free(buf);
13925d63f19SRandall Stewart 				errno = EINVAL;
14025d63f19SRandall Stewart 				return (-1);
14125d63f19SRandall Stewart 			}
142d6dda9b2SRandall Stewart 			if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)at)->sin6_addr)) {
143d6dda9b2SRandall Stewart 				in6_sin6_2_sin((struct sockaddr_in *)cpto, (struct sockaddr_in6 *)at);
144d6dda9b2SRandall Stewart 				cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in));
145d6dda9b2SRandall Stewart 				len += sizeof(struct sockaddr_in);
146d6dda9b2SRandall Stewart 			} else {
14793409822SMichael Tuexen 				memcpy(cpto, at, sizeof(struct sockaddr_in6));
14893409822SMichael Tuexen 				cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in6));
14993409822SMichael Tuexen 				len += sizeof(struct sockaddr_in6);
150d6dda9b2SRandall Stewart 			}
15193409822SMichael Tuexen 			break;
15293409822SMichael Tuexen 		default:
1537f15a8dfSMichael Tuexen 			free(buf);
154d6dda9b2SRandall Stewart 			errno = EINVAL;
155d6dda9b2SRandall Stewart 			return (-1);
156d6dda9b2SRandall Stewart 		}
1577f15a8dfSMichael Tuexen 		at = (struct sockaddr *)((caddr_t)at + at->sa_len);
158d6dda9b2SRandall Stewart 	}
159d6dda9b2SRandall Stewart 	aa = (int *)buf;
1607f15a8dfSMichael Tuexen 	*aa = addrcnt;
161d6dda9b2SRandall Stewart 	ret = setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X, (void *)buf,
162d6dda9b2SRandall Stewart 	    (socklen_t)len);
1637f15a8dfSMichael Tuexen 	if ((ret == 0) && (id != NULL)) {
1647f15a8dfSMichael Tuexen 		*id = *(sctp_assoc_t *)buf;
16542551e99SRandall Stewart 	}
16610e6d832SMichael Tuexen 	free(buf);
167d6dda9b2SRandall Stewart 	return (ret);
168d6dda9b2SRandall Stewart }
169d6dda9b2SRandall Stewart 
170d6dda9b2SRandall Stewart int
171d6dda9b2SRandall Stewart sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, int flags)
172d6dda9b2SRandall Stewart {
173d6dda9b2SRandall Stewart 	struct sctp_getaddresses *gaddrs;
174d6dda9b2SRandall Stewart 	struct sockaddr *sa;
1751b649582SRandall Stewart 	struct sockaddr_in *sin;
1761b649582SRandall Stewart 	struct sockaddr_in6 *sin6;
1775dc6a815SMichael Tuexen 	int i;
1785dc6a815SMichael Tuexen 	size_t argsz;
1791b649582SRandall Stewart 	uint16_t sport = 0;
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 	}
1921b649582SRandall Stewart 	/* First pre-screen the addresses */
193d6dda9b2SRandall Stewart 	sa = addrs;
194d6dda9b2SRandall Stewart 	for (i = 0; i < addrcnt; i++) {
19593409822SMichael Tuexen 		switch (sa->sa_family) {
19693409822SMichael Tuexen 		case AF_INET:
19793409822SMichael Tuexen 			if (sa->sa_len != sizeof(struct sockaddr_in)) {
19893409822SMichael Tuexen 				errno = EINVAL;
19993409822SMichael Tuexen 				return (-1);
20093409822SMichael Tuexen 			}
2011b649582SRandall Stewart 			sin = (struct sockaddr_in *)sa;
2021b649582SRandall Stewart 			if (sin->sin_port) {
2031b649582SRandall Stewart 				/* non-zero port, check or save */
2041b649582SRandall Stewart 				if (sport) {
2051b649582SRandall Stewart 					/* Check against our port */
2061b649582SRandall Stewart 					if (sport != sin->sin_port) {
20793409822SMichael Tuexen 						errno = EINVAL;
20893409822SMichael Tuexen 						return (-1);
2091b649582SRandall Stewart 					}
2101b649582SRandall Stewart 				} else {
2111b649582SRandall Stewart 					/* save off the port */
2121b649582SRandall Stewart 					sport = sin->sin_port;
2131b649582SRandall Stewart 				}
2141b649582SRandall Stewart 			}
21593409822SMichael Tuexen 			break;
21693409822SMichael Tuexen 		case AF_INET6:
21793409822SMichael Tuexen 			if (sa->sa_len != sizeof(struct sockaddr_in6)) {
21893409822SMichael Tuexen 				errno = EINVAL;
21993409822SMichael Tuexen 				return (-1);
22093409822SMichael Tuexen 			}
2211b649582SRandall Stewart 			sin6 = (struct sockaddr_in6 *)sa;
2221b649582SRandall Stewart 			if (sin6->sin6_port) {
2231b649582SRandall Stewart 				/* non-zero port, check or save */
2241b649582SRandall Stewart 				if (sport) {
2251b649582SRandall Stewart 					/* Check against our port */
2261b649582SRandall Stewart 					if (sport != sin6->sin6_port) {
22793409822SMichael Tuexen 						errno = EINVAL;
22893409822SMichael Tuexen 						return (-1);
2291b649582SRandall Stewart 					}
2301b649582SRandall Stewart 				} else {
2311b649582SRandall Stewart 					/* save off the port */
2321b649582SRandall Stewart 					sport = sin6->sin6_port;
2331b649582SRandall Stewart 				}
2341b649582SRandall Stewart 			}
23593409822SMichael Tuexen 			break;
23693409822SMichael Tuexen 		default:
23793409822SMichael Tuexen 			/* Invalid address family specified. */
2381dd0c905SMichael Tuexen 			errno = EAFNOSUPPORT;
23993409822SMichael Tuexen 			return (-1);
2401b649582SRandall Stewart 		}
2415dc6a815SMichael Tuexen 		sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
2421b649582SRandall Stewart 	}
24393409822SMichael Tuexen 	argsz = sizeof(struct sctp_getaddresses) +
24493409822SMichael Tuexen 	    sizeof(struct sockaddr_storage);
24593409822SMichael Tuexen 	if ((gaddrs = (struct sctp_getaddresses *)malloc(argsz)) == NULL) {
24693409822SMichael Tuexen 		errno = ENOMEM;
247d6dda9b2SRandall Stewart 		return (-1);
248d6dda9b2SRandall Stewart 	}
24993409822SMichael Tuexen 	sa = addrs;
25093409822SMichael Tuexen 	for (i = 0; i < addrcnt; i++) {
25125d63f19SRandall Stewart 		memset(gaddrs, 0, argsz);
25225d63f19SRandall Stewart 		gaddrs->sget_assoc_id = 0;
2535dc6a815SMichael Tuexen 		memcpy(gaddrs->addr, sa, sa->sa_len);
2541dd0c905SMichael Tuexen 		/*
2551dd0c905SMichael Tuexen 		 * Now, if there was a port mentioned, assure that the first
2561dd0c905SMichael Tuexen 		 * address has that port to make sure it fails or succeeds
2571dd0c905SMichael Tuexen 		 * correctly.
2581dd0c905SMichael Tuexen 		 */
2591dd0c905SMichael Tuexen 		if ((i == 0) && (sport != 0)) {
2601dd0c905SMichael Tuexen 			switch (gaddrs->addr->sa_family) {
2611dd0c905SMichael Tuexen 			case AF_INET:
2621dd0c905SMichael Tuexen 				sin = (struct sockaddr_in *)gaddrs->addr;
2631dd0c905SMichael Tuexen 				sin->sin_port = sport;
2641dd0c905SMichael Tuexen 				break;
2651dd0c905SMichael Tuexen 			case AF_INET6:
2661dd0c905SMichael Tuexen 				sin6 = (struct sockaddr_in6 *)gaddrs->addr;
2671dd0c905SMichael Tuexen 				sin6->sin6_port = sport;
2681dd0c905SMichael Tuexen 				break;
2691dd0c905SMichael Tuexen 			}
2701dd0c905SMichael Tuexen 		}
27125d63f19SRandall Stewart 		if (setsockopt(sd, IPPROTO_SCTP, flags, gaddrs,
27225d63f19SRandall Stewart 		    (socklen_t)argsz) != 0) {
273d6dda9b2SRandall Stewart 			free(gaddrs);
274d6dda9b2SRandall Stewart 			return (-1);
275d6dda9b2SRandall Stewart 		}
2765dc6a815SMichael Tuexen 		sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
277d6dda9b2SRandall Stewart 	}
278d6dda9b2SRandall Stewart 	free(gaddrs);
279d6dda9b2SRandall Stewart 	return (0);
280d6dda9b2SRandall Stewart }
281d6dda9b2SRandall Stewart 
282d6dda9b2SRandall Stewart int
283d6dda9b2SRandall Stewart sctp_opt_info(int sd, sctp_assoc_t id, int opt, void *arg, socklen_t *size)
284d6dda9b2SRandall Stewart {
285d6dda9b2SRandall Stewart 	if (arg == NULL) {
286602afc03SRandall Stewart 		errno = EINVAL;
287602afc03SRandall Stewart 		return (-1);
288d6dda9b2SRandall Stewart 	}
289b71f5853SMichael Tuexen 	if ((id == SCTP_CURRENT_ASSOC) ||
290b71f5853SMichael Tuexen 	    (id == SCTP_ALL_ASSOC)) {
291b71f5853SMichael Tuexen 		errno = EINVAL;
292b71f5853SMichael Tuexen 		return (-1);
293b71f5853SMichael Tuexen 	}
2941b649582SRandall Stewart 	switch (opt) {
2951b649582SRandall Stewart 	case SCTP_RTOINFO:
2961b649582SRandall Stewart 		((struct sctp_rtoinfo *)arg)->srto_assoc_id = id;
2971b649582SRandall Stewart 		break;
2981b649582SRandall Stewart 	case SCTP_ASSOCINFO:
2991b649582SRandall Stewart 		((struct sctp_assocparams *)arg)->sasoc_assoc_id = id;
3001b649582SRandall Stewart 		break;
3011b649582SRandall Stewart 	case SCTP_DEFAULT_SEND_PARAM:
3021b649582SRandall Stewart 		((struct sctp_assocparams *)arg)->sasoc_assoc_id = id;
3031b649582SRandall Stewart 		break;
3041b649582SRandall Stewart 	case SCTP_PRIMARY_ADDR:
3051b649582SRandall Stewart 		((struct sctp_setprim *)arg)->ssp_assoc_id = id;
3061b649582SRandall Stewart 		break;
3071b649582SRandall Stewart 	case SCTP_PEER_ADDR_PARAMS:
3081b649582SRandall Stewart 		((struct sctp_paddrparams *)arg)->spp_assoc_id = id;
3091b649582SRandall Stewart 		break;
3101b649582SRandall Stewart 	case SCTP_MAXSEG:
3111b649582SRandall Stewart 		((struct sctp_assoc_value *)arg)->assoc_id = id;
3121b649582SRandall Stewart 		break;
3131b649582SRandall Stewart 	case SCTP_AUTH_KEY:
3141b649582SRandall Stewart 		((struct sctp_authkey *)arg)->sca_assoc_id = id;
3151b649582SRandall Stewart 		break;
3161b649582SRandall Stewart 	case SCTP_AUTH_ACTIVE_KEY:
3171b649582SRandall Stewart 		((struct sctp_authkeyid *)arg)->scact_assoc_id = id;
3181b649582SRandall Stewart 		break;
3191b649582SRandall Stewart 	case SCTP_DELAYED_SACK:
3201b649582SRandall Stewart 		((struct sctp_sack_info *)arg)->sack_assoc_id = id;
3211b649582SRandall Stewart 		break;
3221b649582SRandall Stewart 	case SCTP_CONTEXT:
3231b649582SRandall Stewart 		((struct sctp_assoc_value *)arg)->assoc_id = id;
3241b649582SRandall Stewart 		break;
3251b649582SRandall Stewart 	case SCTP_STATUS:
3261b649582SRandall Stewart 		((struct sctp_status *)arg)->sstat_assoc_id = id;
3271b649582SRandall Stewart 		break;
3281b649582SRandall Stewart 	case SCTP_GET_PEER_ADDR_INFO:
3291b649582SRandall Stewart 		((struct sctp_paddrinfo *)arg)->spinfo_assoc_id = id;
3301b649582SRandall Stewart 		break;
3311b649582SRandall Stewart 	case SCTP_PEER_AUTH_CHUNKS:
3321b649582SRandall Stewart 		((struct sctp_authchunks *)arg)->gauth_assoc_id = id;
3331b649582SRandall Stewart 		break;
3341b649582SRandall Stewart 	case SCTP_LOCAL_AUTH_CHUNKS:
3351b649582SRandall Stewart 		((struct sctp_authchunks *)arg)->gauth_assoc_id = id;
3361b649582SRandall Stewart 		break;
33748f65f00SMichael Tuexen 	case SCTP_TIMEOUTS:
33848f65f00SMichael Tuexen 		((struct sctp_timeouts *)arg)->stimo_assoc_id = id;
33948f65f00SMichael Tuexen 		break;
340e2e7c62eSMichael Tuexen 	case SCTP_EVENT:
341e2e7c62eSMichael Tuexen 		((struct sctp_event *)arg)->se_assoc_id = id;
342e2e7c62eSMichael Tuexen 		break;
34313aae0bfSMichael Tuexen 	case SCTP_DEFAULT_SNDINFO:
34413aae0bfSMichael Tuexen 		((struct sctp_sndinfo *)arg)->snd_assoc_id = id;
34513aae0bfSMichael Tuexen 		break;
34613aae0bfSMichael Tuexen 	case SCTP_DEFAULT_PRINFO:
34713aae0bfSMichael Tuexen 		((struct sctp_default_prinfo *)arg)->pr_assoc_id = id;
34813aae0bfSMichael Tuexen 		break;
349ca85e948SMichael Tuexen 	case SCTP_PEER_ADDR_THLDS:
350ca85e948SMichael Tuexen 		((struct sctp_paddrthlds *)arg)->spt_assoc_id = id;
351ca85e948SMichael Tuexen 		break;
352c9c58059SMichael Tuexen 	case SCTP_REMOTE_UDP_ENCAPS_PORT:
353c9c58059SMichael Tuexen 		((struct sctp_udpencaps *)arg)->sue_assoc_id = id;
354c9c58059SMichael Tuexen 		break;
355f342355aSMichael Tuexen 	case SCTP_ECN_SUPPORTED:
356f342355aSMichael Tuexen 		((struct sctp_assoc_value *)arg)->assoc_id = id;
357f342355aSMichael Tuexen 		break;
358dd973b0eSMichael Tuexen 	case SCTP_PR_SUPPORTED:
359dd973b0eSMichael Tuexen 		((struct sctp_assoc_value *)arg)->assoc_id = id;
360dd973b0eSMichael Tuexen 		break;
361c79bec9cSMichael Tuexen 	case SCTP_AUTH_SUPPORTED:
362c79bec9cSMichael Tuexen 		((struct sctp_assoc_value *)arg)->assoc_id = id;
363c79bec9cSMichael Tuexen 		break;
364c79bec9cSMichael Tuexen 	case SCTP_ASCONF_SUPPORTED:
365c79bec9cSMichael Tuexen 		((struct sctp_assoc_value *)arg)->assoc_id = id;
366c79bec9cSMichael Tuexen 		break;
367317e00efSMichael Tuexen 	case SCTP_RECONFIG_SUPPORTED:
368317e00efSMichael Tuexen 		((struct sctp_assoc_value *)arg)->assoc_id = id;
369317e00efSMichael Tuexen 		break;
370caea9879SMichael Tuexen 	case SCTP_NRSACK_SUPPORTED:
371caea9879SMichael Tuexen 		((struct sctp_assoc_value *)arg)->assoc_id = id;
372caea9879SMichael Tuexen 		break;
373cb9b8e6fSMichael Tuexen 	case SCTP_PKTDROP_SUPPORTED:
374cb9b8e6fSMichael Tuexen 		((struct sctp_assoc_value *)arg)->assoc_id = id;
375cb9b8e6fSMichael Tuexen 		break;
376bb2c20c1SMichael Tuexen 	case SCTP_MAX_BURST:
377bb2c20c1SMichael Tuexen 		((struct sctp_assoc_value *)arg)->assoc_id = id;
378bb2c20c1SMichael Tuexen 		break;
3797c9b6492SMichael Tuexen 	case SCTP_ENABLE_STREAM_RESET:
3807c9b6492SMichael Tuexen 		((struct sctp_assoc_value *)arg)->assoc_id = id;
3817c9b6492SMichael Tuexen 		break;
382f0396ad1SMichael Tuexen 	case SCTP_PR_STREAM_STATUS:
383f0396ad1SMichael Tuexen 		((struct sctp_prstatus *)arg)->sprstat_assoc_id = id;
384f0396ad1SMichael Tuexen 		break;
385f0396ad1SMichael Tuexen 	case SCTP_PR_ASSOC_STATUS:
386f0396ad1SMichael Tuexen 		((struct sctp_prstatus *)arg)->sprstat_assoc_id = id;
387f0396ad1SMichael Tuexen 		break;
38859b6d5beSMichael Tuexen 	case SCTP_MAX_CWND:
38959b6d5beSMichael Tuexen 		((struct sctp_assoc_value *)arg)->assoc_id = id;
39059b6d5beSMichael Tuexen 		break;
3911b649582SRandall Stewart 	default:
3921b649582SRandall Stewart 		break;
3931b649582SRandall Stewart 	}
394d6dda9b2SRandall Stewart 	return (getsockopt(sd, IPPROTO_SCTP, opt, arg, size));
395d6dda9b2SRandall Stewart }
396d6dda9b2SRandall Stewart 
397d6dda9b2SRandall Stewart int
398d6dda9b2SRandall Stewart sctp_getpaddrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs)
399d6dda9b2SRandall Stewart {
400d6dda9b2SRandall Stewart 	struct sctp_getaddresses *addrs;
401d6dda9b2SRandall Stewart 	struct sockaddr *sa;
402d6dda9b2SRandall Stewart 	sctp_assoc_t asoc;
403d6dda9b2SRandall Stewart 	caddr_t lim;
4045dc6a815SMichael Tuexen 	socklen_t opt_len;
405d6dda9b2SRandall Stewart 	int cnt;
406d6dda9b2SRandall Stewart 
407d6dda9b2SRandall Stewart 	if (raddrs == NULL) {
408d6dda9b2SRandall Stewart 		errno = EFAULT;
409d6dda9b2SRandall Stewart 		return (-1);
410d6dda9b2SRandall Stewart 	}
411d6dda9b2SRandall Stewart 	asoc = id;
4125dc6a815SMichael Tuexen 	opt_len = (socklen_t)sizeof(sctp_assoc_t);
413d6dda9b2SRandall Stewart 	if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_REMOTE_ADDR_SIZE,
4145dc6a815SMichael Tuexen 	    &asoc, &opt_len) != 0) {
415d6dda9b2SRandall Stewart 		return (-1);
416d6dda9b2SRandall Stewart 	}
417d6dda9b2SRandall Stewart 	/* size required is returned in 'asoc' */
4188124c05fSMichael Tuexen 	opt_len = (socklen_t)((size_t)asoc + sizeof(sctp_assoc_t));
4195dc6a815SMichael Tuexen 	addrs = calloc(1, (size_t)opt_len);
420d6dda9b2SRandall Stewart 	if (addrs == NULL) {
4214ed0ebf6SMichael Tuexen 		errno = ENOMEM;
422d6dda9b2SRandall Stewart 		return (-1);
423d6dda9b2SRandall Stewart 	}
424d6dda9b2SRandall Stewart 	addrs->sget_assoc_id = id;
425d6dda9b2SRandall Stewart 	/* Now lets get the array of addresses */
426d6dda9b2SRandall Stewart 	if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_PEER_ADDRESSES,
4275dc6a815SMichael Tuexen 	    addrs, &opt_len) != 0) {
428d6dda9b2SRandall Stewart 		free(addrs);
429d6dda9b2SRandall Stewart 		return (-1);
430d6dda9b2SRandall Stewart 	}
4315dc6a815SMichael Tuexen 	*raddrs = (struct sockaddr *)&addrs->addr[0];
432d6dda9b2SRandall Stewart 	cnt = 0;
433d6dda9b2SRandall Stewart 	sa = (struct sockaddr *)&addrs->addr[0];
4345dc6a815SMichael Tuexen 	lim = (caddr_t)addrs + opt_len;
435d6dda9b2SRandall Stewart 	while (((caddr_t)sa < lim) && (sa->sa_len > 0)) {
436d6dda9b2SRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
437d6dda9b2SRandall Stewart 		cnt++;
438d6dda9b2SRandall Stewart 	}
439d6dda9b2SRandall Stewart 	return (cnt);
440d6dda9b2SRandall Stewart }
441d6dda9b2SRandall Stewart 
442d6dda9b2SRandall Stewart void
443d6dda9b2SRandall Stewart sctp_freepaddrs(struct sockaddr *addrs)
444d6dda9b2SRandall Stewart {
445d6dda9b2SRandall Stewart 	void *fr_addr;
446d6dda9b2SRandall Stewart 
4477f15a8dfSMichael Tuexen 	/* Take away the hidden association id */
448d6dda9b2SRandall Stewart 	fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t));
449d6dda9b2SRandall Stewart 	/* Now free it */
450d6dda9b2SRandall Stewart 	free(fr_addr);
451d6dda9b2SRandall Stewart }
452d6dda9b2SRandall Stewart 
453d6dda9b2SRandall Stewart int
454d6dda9b2SRandall Stewart sctp_getladdrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs)
455d6dda9b2SRandall Stewart {
456d6dda9b2SRandall Stewart 	struct sctp_getaddresses *addrs;
457d6dda9b2SRandall Stewart 	caddr_t lim;
458d6dda9b2SRandall Stewart 	struct sockaddr *sa;
4595dc6a815SMichael Tuexen 	size_t size_of_addresses;
4605dc6a815SMichael Tuexen 	socklen_t opt_len;
461d6dda9b2SRandall Stewart 	int cnt;
462d6dda9b2SRandall Stewart 
463d6dda9b2SRandall Stewart 	if (raddrs == NULL) {
464d6dda9b2SRandall Stewart 		errno = EFAULT;
465d6dda9b2SRandall Stewart 		return (-1);
466d6dda9b2SRandall Stewart 	}
467d6dda9b2SRandall Stewart 	size_of_addresses = 0;
4685dc6a815SMichael Tuexen 	opt_len = (socklen_t)sizeof(int);
469d6dda9b2SRandall Stewart 	if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDR_SIZE,
4705dc6a815SMichael Tuexen 	    &size_of_addresses, &opt_len) != 0) {
471d6dda9b2SRandall Stewart 		errno = ENOMEM;
472d6dda9b2SRandall Stewart 		return (-1);
473d6dda9b2SRandall Stewart 	}
474d6dda9b2SRandall Stewart 	if (size_of_addresses == 0) {
475d6dda9b2SRandall Stewart 		errno = ENOTCONN;
476d6dda9b2SRandall Stewart 		return (-1);
477d6dda9b2SRandall Stewart 	}
4788124c05fSMichael Tuexen 	opt_len = (socklen_t)(size_of_addresses + sizeof(sctp_assoc_t));
4795dc6a815SMichael Tuexen 	addrs = calloc(1, (size_t)opt_len);
480d6dda9b2SRandall Stewart 	if (addrs == NULL) {
481d6dda9b2SRandall Stewart 		errno = ENOMEM;
482d6dda9b2SRandall Stewart 		return (-1);
483d6dda9b2SRandall Stewart 	}
484d6dda9b2SRandall Stewart 	addrs->sget_assoc_id = id;
485d6dda9b2SRandall Stewart 	/* Now lets get the array of addresses */
486d6dda9b2SRandall Stewart 	if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDRESSES, addrs,
4875dc6a815SMichael Tuexen 	    &opt_len) != 0) {
488d6dda9b2SRandall Stewart 		free(addrs);
489d6dda9b2SRandall Stewart 		errno = ENOMEM;
490d6dda9b2SRandall Stewart 		return (-1);
491d6dda9b2SRandall Stewart 	}
4925dc6a815SMichael Tuexen 	*raddrs = (struct sockaddr *)&addrs->addr[0];
493d6dda9b2SRandall Stewart 	cnt = 0;
494d6dda9b2SRandall Stewart 	sa = (struct sockaddr *)&addrs->addr[0];
4955dc6a815SMichael Tuexen 	lim = (caddr_t)addrs + opt_len;
496d6dda9b2SRandall Stewart 	while (((caddr_t)sa < lim) && (sa->sa_len > 0)) {
497d6dda9b2SRandall Stewart 		sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
498d6dda9b2SRandall Stewart 		cnt++;
499d6dda9b2SRandall Stewart 	}
500d6dda9b2SRandall Stewart 	return (cnt);
501d6dda9b2SRandall Stewart }
502d6dda9b2SRandall Stewart 
503d6dda9b2SRandall Stewart void
504d6dda9b2SRandall Stewart sctp_freeladdrs(struct sockaddr *addrs)
505d6dda9b2SRandall Stewart {
506d6dda9b2SRandall Stewart 	void *fr_addr;
507d6dda9b2SRandall Stewart 
5087f15a8dfSMichael Tuexen 	/* Take away the hidden association id */
509d6dda9b2SRandall Stewart 	fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t));
510d6dda9b2SRandall Stewart 	/* Now free it */
511d6dda9b2SRandall Stewart 	free(fr_addr);
512d6dda9b2SRandall Stewart }
513d6dda9b2SRandall Stewart 
514d6dda9b2SRandall Stewart ssize_t
515d6dda9b2SRandall Stewart sctp_sendmsg(int s,
516d6dda9b2SRandall Stewart     const void *data,
517d6dda9b2SRandall Stewart     size_t len,
518d6dda9b2SRandall Stewart     const struct sockaddr *to,
519002b1f0bSRandall Stewart     socklen_t tolen,
5203d36ac98SRebecca Cran     uint32_t ppid,
5213d36ac98SRebecca Cran     uint32_t flags,
5223d36ac98SRebecca Cran     uint16_t stream_no,
5233d36ac98SRebecca Cran     uint32_t timetolive,
5243d36ac98SRebecca Cran     uint32_t context)
525d6dda9b2SRandall Stewart {
526d6dda9b2SRandall Stewart #ifdef SYS_sctp_generic_sendmsg
527d6dda9b2SRandall Stewart 	struct sctp_sndrcvinfo sinfo;
528d6dda9b2SRandall Stewart 
529539bb45aSMichael Tuexen 	memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
530d6dda9b2SRandall Stewart 	sinfo.sinfo_ppid = ppid;
531d6dda9b2SRandall Stewart 	sinfo.sinfo_flags = flags;
532d6dda9b2SRandall Stewart 	sinfo.sinfo_stream = stream_no;
533d6dda9b2SRandall Stewart 	sinfo.sinfo_timetolive = timetolive;
534d6dda9b2SRandall Stewart 	sinfo.sinfo_context = context;
535d6dda9b2SRandall Stewart 	sinfo.sinfo_assoc_id = 0;
536d6dda9b2SRandall Stewart 	return (syscall(SYS_sctp_generic_sendmsg, s,
537d6dda9b2SRandall Stewart 	    data, len, to, tolen, &sinfo, 0));
538d6dda9b2SRandall Stewart #else
539d6dda9b2SRandall Stewart 	struct msghdr msg;
5407f15a8dfSMichael Tuexen 	struct sctp_sndrcvinfo *sinfo;
54148f65f00SMichael Tuexen 	struct iovec iov;
5427f15a8dfSMichael Tuexen 	char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
543d6dda9b2SRandall Stewart 	struct cmsghdr *cmsg;
544d6dda9b2SRandall Stewart 	struct sockaddr *who = NULL;
545d6dda9b2SRandall Stewart 	union {
546d6dda9b2SRandall Stewart 		struct sockaddr_in in;
547d6dda9b2SRandall Stewart 		struct sockaddr_in6 in6;
548d6dda9b2SRandall Stewart 	}     addr;
549d6dda9b2SRandall Stewart 
55048f65f00SMichael Tuexen 	if ((tolen > 0) &&
55148f65f00SMichael Tuexen 	    ((to == NULL) || (tolen < sizeof(struct sockaddr)))) {
552002b1f0bSRandall Stewart 		errno = EINVAL;
5534224e03aSMichael Tuexen 		return (-1);
554002b1f0bSRandall Stewart 	}
5557f15a8dfSMichael Tuexen 	if ((to != NULL) && (tolen > 0)) {
5567f15a8dfSMichael Tuexen 		switch (to->sa_family) {
5577f15a8dfSMichael Tuexen 		case AF_INET:
558002b1f0bSRandall Stewart 			if (tolen != sizeof(struct sockaddr_in)) {
559002b1f0bSRandall Stewart 				errno = EINVAL;
5604224e03aSMichael Tuexen 				return (-1);
561002b1f0bSRandall Stewart 			}
56248f65f00SMichael Tuexen 			if ((to->sa_len > 0) &&
56348f65f00SMichael Tuexen 			    (to->sa_len != sizeof(struct sockaddr_in))) {
564002b1f0bSRandall Stewart 				errno = EINVAL;
5654224e03aSMichael Tuexen 				return (-1);
566002b1f0bSRandall Stewart 			}
567d6dda9b2SRandall Stewart 			memcpy(&addr, to, sizeof(struct sockaddr_in));
568d6dda9b2SRandall Stewart 			addr.in.sin_len = sizeof(struct sockaddr_in);
5697f15a8dfSMichael Tuexen 			break;
5707f15a8dfSMichael Tuexen 		case AF_INET6:
571002b1f0bSRandall Stewart 			if (tolen != sizeof(struct sockaddr_in6)) {
572002b1f0bSRandall Stewart 				errno = EINVAL;
5734224e03aSMichael Tuexen 				return (-1);
574002b1f0bSRandall Stewart 			}
57548f65f00SMichael Tuexen 			if ((to->sa_len > 0) &&
57648f65f00SMichael Tuexen 			    (to->sa_len != sizeof(struct sockaddr_in6))) {
577002b1f0bSRandall Stewart 				errno = EINVAL;
5784224e03aSMichael Tuexen 				return (-1);
579002b1f0bSRandall Stewart 			}
580d6dda9b2SRandall Stewart 			memcpy(&addr, to, sizeof(struct sockaddr_in6));
581d6dda9b2SRandall Stewart 			addr.in6.sin6_len = sizeof(struct sockaddr_in6);
5827f15a8dfSMichael Tuexen 			break;
5837f15a8dfSMichael Tuexen 		default:
584002b1f0bSRandall Stewart 			errno = EAFNOSUPPORT;
5854224e03aSMichael Tuexen 			return (-1);
586d6dda9b2SRandall Stewart 		}
587d6dda9b2SRandall Stewart 		who = (struct sockaddr *)&addr;
588d6dda9b2SRandall Stewart 	}
589*df2fca7bSMichael Tuexen 
59048f65f00SMichael Tuexen 	iov.iov_base = (char *)data;
59148f65f00SMichael Tuexen 	iov.iov_len = len;
592d6dda9b2SRandall Stewart 
593002b1f0bSRandall Stewart 	if (who) {
594d6dda9b2SRandall Stewart 		msg.msg_name = (caddr_t)who;
595d6dda9b2SRandall Stewart 		msg.msg_namelen = who->sa_len;
596d6dda9b2SRandall Stewart 	} else {
597d6dda9b2SRandall Stewart 		msg.msg_name = (caddr_t)NULL;
598d6dda9b2SRandall Stewart 		msg.msg_namelen = 0;
599d6dda9b2SRandall Stewart 	}
60048f65f00SMichael Tuexen 	msg.msg_iov = &iov;
601d6dda9b2SRandall Stewart 	msg.msg_iovlen = 1;
6027f15a8dfSMichael Tuexen 	msg.msg_control = cmsgbuf;
6037f15a8dfSMichael Tuexen 	msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
6042b8a4d80SMichael Tuexen 	msg.msg_flags = 0;
6057f15a8dfSMichael Tuexen 	cmsg = (struct cmsghdr *)cmsgbuf;
606d6dda9b2SRandall Stewart 	cmsg->cmsg_level = IPPROTO_SCTP;
607d6dda9b2SRandall Stewart 	cmsg->cmsg_type = SCTP_SNDRCV;
608d6dda9b2SRandall Stewart 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
6097f15a8dfSMichael Tuexen 	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
610c79bec9cSMichael Tuexen 	memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
6117f15a8dfSMichael Tuexen 	sinfo->sinfo_stream = stream_no;
6127f15a8dfSMichael Tuexen 	sinfo->sinfo_ssn = 0;
6137f15a8dfSMichael Tuexen 	sinfo->sinfo_flags = flags;
6147f15a8dfSMichael Tuexen 	sinfo->sinfo_ppid = ppid;
6157f15a8dfSMichael Tuexen 	sinfo->sinfo_context = context;
6167f15a8dfSMichael Tuexen 	sinfo->sinfo_assoc_id = 0;
6177f15a8dfSMichael Tuexen 	sinfo->sinfo_timetolive = timetolive;
6187f15a8dfSMichael Tuexen 	return (sendmsg(s, &msg, 0));
619d6dda9b2SRandall Stewart #endif
620d6dda9b2SRandall Stewart }
621d6dda9b2SRandall Stewart 
622d6dda9b2SRandall Stewart 
623d6dda9b2SRandall Stewart sctp_assoc_t
624d6dda9b2SRandall Stewart sctp_getassocid(int sd, struct sockaddr *sa)
625d6dda9b2SRandall Stewart {
626b54d3a6cSRandall Stewart 	struct sctp_paddrinfo sp;
627066f54d8SCraig Rodrigues 	socklen_t siz;
628d6dda9b2SRandall Stewart 
629d6dda9b2SRandall Stewart 	/* First get the assoc id */
630b54d3a6cSRandall Stewart 	siz = sizeof(sp);
631d6dda9b2SRandall Stewart 	memset(&sp, 0, sizeof(sp));
632b54d3a6cSRandall Stewart 	memcpy((caddr_t)&sp.spinfo_address, sa, sa->sa_len);
633d6dda9b2SRandall Stewart 	if (getsockopt(sd, IPPROTO_SCTP,
634b54d3a6cSRandall Stewart 	    SCTP_GET_PEER_ADDR_INFO, &sp, &siz) != 0) {
6357f15a8dfSMichael Tuexen 		/* We depend on the fact that 0 can never be returned */
636d6dda9b2SRandall Stewart 		return ((sctp_assoc_t)0);
637d6dda9b2SRandall Stewart 	}
638b54d3a6cSRandall Stewart 	return (sp.spinfo_assoc_id);
639d6dda9b2SRandall Stewart }
640d6dda9b2SRandall Stewart 
641d6dda9b2SRandall Stewart ssize_t
642d6dda9b2SRandall Stewart sctp_send(int sd, const void *data, size_t len,
643d6dda9b2SRandall Stewart     const struct sctp_sndrcvinfo *sinfo,
644d6dda9b2SRandall Stewart     int flags)
645d6dda9b2SRandall Stewart {
646d6dda9b2SRandall Stewart 
647d6dda9b2SRandall Stewart #ifdef SYS_sctp_generic_sendmsg
648d6dda9b2SRandall Stewart 	struct sockaddr *to = NULL;
649d6dda9b2SRandall Stewart 
650d6dda9b2SRandall Stewart 	return (syscall(SYS_sctp_generic_sendmsg, sd,
651d6dda9b2SRandall Stewart 	    data, len, to, 0, sinfo, flags));
652d6dda9b2SRandall Stewart #else
653d6dda9b2SRandall Stewart 	struct msghdr msg;
65448f65f00SMichael Tuexen 	struct iovec iov;
6557f15a8dfSMichael Tuexen 	char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
656d6dda9b2SRandall Stewart 	struct cmsghdr *cmsg;
657d6dda9b2SRandall Stewart 
658d6dda9b2SRandall Stewart 	if (sinfo == NULL) {
659b54d3a6cSRandall Stewart 		errno = EINVAL;
660b54d3a6cSRandall Stewart 		return (-1);
661d6dda9b2SRandall Stewart 	}
66248f65f00SMichael Tuexen 	iov.iov_base = (char *)data;
66348f65f00SMichael Tuexen 	iov.iov_len = len;
664d6dda9b2SRandall Stewart 
6657f15a8dfSMichael Tuexen 	msg.msg_name = NULL;
666d6dda9b2SRandall Stewart 	msg.msg_namelen = 0;
66748f65f00SMichael Tuexen 	msg.msg_iov = &iov;
668d6dda9b2SRandall Stewart 	msg.msg_iovlen = 1;
6697f15a8dfSMichael Tuexen 	msg.msg_control = cmsgbuf;
6707f15a8dfSMichael Tuexen 	msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
6712b8a4d80SMichael Tuexen 	msg.msg_flags = 0;
6727f15a8dfSMichael Tuexen 	cmsg = (struct cmsghdr *)cmsgbuf;
673d6dda9b2SRandall Stewart 	cmsg->cmsg_level = IPPROTO_SCTP;
674d6dda9b2SRandall Stewart 	cmsg->cmsg_type = SCTP_SNDRCV;
675d6dda9b2SRandall Stewart 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
6767f15a8dfSMichael Tuexen 	memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo));
6777f15a8dfSMichael Tuexen 	return (sendmsg(sd, &msg, flags));
678d6dda9b2SRandall Stewart #endif
679d6dda9b2SRandall Stewart }
680d6dda9b2SRandall Stewart 
681d6dda9b2SRandall Stewart 
682d6dda9b2SRandall Stewart 
683d6dda9b2SRandall Stewart ssize_t
684d6dda9b2SRandall Stewart sctp_sendx(int sd, const void *msg, size_t msg_len,
685d6dda9b2SRandall Stewart     struct sockaddr *addrs, int addrcnt,
686d6dda9b2SRandall Stewart     struct sctp_sndrcvinfo *sinfo,
687d6dda9b2SRandall Stewart     int flags)
688d6dda9b2SRandall Stewart {
689335a2d00SRandall Stewart 	struct sctp_sndrcvinfo __sinfo;
690d6dda9b2SRandall Stewart 	ssize_t ret;
691d6dda9b2SRandall Stewart 	int i, cnt, *aa, saved_errno;
692d6dda9b2SRandall Stewart 	char *buf;
6935dc6a815SMichael Tuexen 	int no_end_cx = 0;
6945dc6a815SMichael Tuexen 	size_t len, add_len;
695d6dda9b2SRandall Stewart 	struct sockaddr *at;
696d6dda9b2SRandall Stewart 
6971b649582SRandall Stewart 	if (addrs == NULL) {
6981b649582SRandall Stewart 		errno = EINVAL;
6991b649582SRandall Stewart 		return (-1);
7001b649582SRandall Stewart 	}
701804cf641SRandall Stewart #ifdef SYS_sctp_generic_sendmsg
70248f65f00SMichael Tuexen 	if (addrcnt == 1) {
703804cf641SRandall Stewart 		socklen_t l;
70432d0a77dSMichael Tuexen 		ssize_t ret;
705804cf641SRandall Stewart 
706804cf641SRandall Stewart 		/*
707804cf641SRandall Stewart 		 * Quick way, we don't need to do a connectx so lets use the
708804cf641SRandall Stewart 		 * syscall directly.
709804cf641SRandall Stewart 		 */
710804cf641SRandall Stewart 		l = addrs->sa_len;
71132d0a77dSMichael Tuexen 		ret = syscall(SYS_sctp_generic_sendmsg, sd,
71232d0a77dSMichael Tuexen 		    msg, msg_len, addrs, l, sinfo, flags);
71332d0a77dSMichael Tuexen 		if ((ret >= 0) && (sinfo != NULL)) {
71432d0a77dSMichael Tuexen 			sinfo->sinfo_assoc_id = sctp_getassocid(sd, addrs);
71532d0a77dSMichael Tuexen 		}
71632d0a77dSMichael Tuexen 		return (ret);
717804cf641SRandall Stewart 	}
718804cf641SRandall Stewart #endif
719b54d3a6cSRandall Stewart 
720d6dda9b2SRandall Stewart 	len = sizeof(int);
721d6dda9b2SRandall Stewart 	at = addrs;
722d6dda9b2SRandall Stewart 	cnt = 0;
723d6dda9b2SRandall Stewart 	/* validate all the addresses and get the size */
724d6dda9b2SRandall Stewart 	for (i = 0; i < addrcnt; i++) {
725d6dda9b2SRandall Stewart 		if (at->sa_family == AF_INET) {
726d6dda9b2SRandall Stewart 			add_len = sizeof(struct sockaddr_in);
727d6dda9b2SRandall Stewart 		} else if (at->sa_family == AF_INET6) {
728d6dda9b2SRandall Stewart 			add_len = sizeof(struct sockaddr_in6);
729d6dda9b2SRandall Stewart 		} else {
730d6dda9b2SRandall Stewart 			errno = EINVAL;
731d6dda9b2SRandall Stewart 			return (-1);
732d6dda9b2SRandall Stewart 		}
733d6dda9b2SRandall Stewart 		len += add_len;
734d6dda9b2SRandall Stewart 		at = (struct sockaddr *)((caddr_t)at + add_len);
735d6dda9b2SRandall Stewart 		cnt++;
736d6dda9b2SRandall Stewart 	}
737d6dda9b2SRandall Stewart 	/* do we have any? */
738d6dda9b2SRandall Stewart 	if (cnt == 0) {
739d6dda9b2SRandall Stewart 		errno = EINVAL;
740d6dda9b2SRandall Stewart 		return (-1);
741d6dda9b2SRandall Stewart 	}
742d6dda9b2SRandall Stewart 	buf = malloc(len);
743d6dda9b2SRandall Stewart 	if (buf == NULL) {
7444ed0ebf6SMichael Tuexen 		errno = ENOMEM;
745b54d3a6cSRandall Stewart 		return (-1);
746d6dda9b2SRandall Stewart 	}
747d6dda9b2SRandall Stewart 	aa = (int *)buf;
748d6dda9b2SRandall Stewart 	*aa = cnt;
749d6dda9b2SRandall Stewart 	aa++;
7505dc6a815SMichael Tuexen 	memcpy((caddr_t)aa, addrs, (size_t)(len - sizeof(int)));
751d6dda9b2SRandall Stewart 	ret = setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_DELAYED, (void *)buf,
752d6dda9b2SRandall Stewart 	    (socklen_t)len);
753d6dda9b2SRandall Stewart 
754d6dda9b2SRandall Stewart 	free(buf);
755d6dda9b2SRandall Stewart 	if (ret != 0) {
756d6dda9b2SRandall Stewart 		if (errno == EALREADY) {
757ecf4b67aSRebecca Cran 			no_end_cx = 1;
758d6dda9b2SRandall Stewart 			goto continue_send;
759d6dda9b2SRandall Stewart 		}
760d6dda9b2SRandall Stewart 		return (ret);
761d6dda9b2SRandall Stewart 	}
762d6dda9b2SRandall Stewart continue_send:
763335a2d00SRandall Stewart 	if (sinfo == NULL) {
764335a2d00SRandall Stewart 		sinfo = &__sinfo;
765335a2d00SRandall Stewart 		memset(&__sinfo, 0, sizeof(__sinfo));
766335a2d00SRandall Stewart 	}
767d6dda9b2SRandall Stewart 	sinfo->sinfo_assoc_id = sctp_getassocid(sd, addrs);
768d6dda9b2SRandall Stewart 	if (sinfo->sinfo_assoc_id == 0) {
769d6dda9b2SRandall Stewart 		(void)setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_COMPLETE, (void *)addrs,
770d6dda9b2SRandall Stewart 		    (socklen_t)addrs->sa_len);
771d6dda9b2SRandall Stewart 		errno = ENOENT;
772d6dda9b2SRandall Stewart 		return (-1);
773d6dda9b2SRandall Stewart 	}
774d6dda9b2SRandall Stewart 	ret = sctp_send(sd, msg, msg_len, sinfo, flags);
775d6dda9b2SRandall Stewart 	saved_errno = errno;
776d6dda9b2SRandall Stewart 	if (no_end_cx == 0)
777d6dda9b2SRandall Stewart 		(void)setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_COMPLETE, (void *)addrs,
778d6dda9b2SRandall Stewart 		    (socklen_t)addrs->sa_len);
779d6dda9b2SRandall Stewart 
780d6dda9b2SRandall Stewart 	errno = saved_errno;
781d6dda9b2SRandall Stewart 	return (ret);
782d6dda9b2SRandall Stewart }
783d6dda9b2SRandall Stewart 
784d6dda9b2SRandall Stewart ssize_t
785d6dda9b2SRandall Stewart sctp_sendmsgx(int sd,
786d6dda9b2SRandall Stewart     const void *msg,
787d6dda9b2SRandall Stewart     size_t len,
788d6dda9b2SRandall Stewart     struct sockaddr *addrs,
789d6dda9b2SRandall Stewart     int addrcnt,
7903d36ac98SRebecca Cran     uint32_t ppid,
7913d36ac98SRebecca Cran     uint32_t flags,
7923d36ac98SRebecca Cran     uint16_t stream_no,
7933d36ac98SRebecca Cran     uint32_t timetolive,
7943d36ac98SRebecca Cran     uint32_t context)
795d6dda9b2SRandall Stewart {
796d6dda9b2SRandall Stewart 	struct sctp_sndrcvinfo sinfo;
797d6dda9b2SRandall Stewart 
798d6dda9b2SRandall Stewart 	memset((void *)&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
799d6dda9b2SRandall Stewart 	sinfo.sinfo_ppid = ppid;
800d6dda9b2SRandall Stewart 	sinfo.sinfo_flags = flags;
801159efc33SMichael Tuexen 	sinfo.sinfo_stream = stream_no;
802d6dda9b2SRandall Stewart 	sinfo.sinfo_timetolive = timetolive;
803d6dda9b2SRandall Stewart 	sinfo.sinfo_context = context;
8044224e03aSMichael Tuexen 	return (sctp_sendx(sd, msg, len, addrs, addrcnt, &sinfo, 0));
805d6dda9b2SRandall Stewart }
806d6dda9b2SRandall Stewart 
807d6dda9b2SRandall Stewart ssize_t
808d6dda9b2SRandall Stewart sctp_recvmsg(int s,
809d6dda9b2SRandall Stewart     void *dbuf,
810d6dda9b2SRandall Stewart     size_t len,
811d6dda9b2SRandall Stewart     struct sockaddr *from,
812d6dda9b2SRandall Stewart     socklen_t *fromlen,
813d6dda9b2SRandall Stewart     struct sctp_sndrcvinfo *sinfo,
814d6dda9b2SRandall Stewart     int *msg_flags)
815d6dda9b2SRandall Stewart {
816d6dda9b2SRandall Stewart #ifdef SYS_sctp_generic_recvmsg
81748f65f00SMichael Tuexen 	struct iovec iov;
818d6dda9b2SRandall Stewart 
81948f65f00SMichael Tuexen 	iov.iov_base = dbuf;
82048f65f00SMichael Tuexen 	iov.iov_len = len;
821d6dda9b2SRandall Stewart 	return (syscall(SYS_sctp_generic_recvmsg, s,
82248f65f00SMichael Tuexen 	    &iov, 1, from, fromlen, sinfo, msg_flags));
823d6dda9b2SRandall Stewart #else
824d6dda9b2SRandall Stewart 	ssize_t sz;
825d6dda9b2SRandall Stewart 	struct msghdr msg;
82648f65f00SMichael Tuexen 	struct iovec iov;
8277f15a8dfSMichael Tuexen 	char cmsgbuf[SCTP_CONTROL_VEC_SIZE_RCV];
828d6dda9b2SRandall Stewart 	struct cmsghdr *cmsg;
829d6dda9b2SRandall Stewart 
830d6dda9b2SRandall Stewart 	if (msg_flags == NULL) {
831d6dda9b2SRandall Stewart 		errno = EINVAL;
832d6dda9b2SRandall Stewart 		return (-1);
833d6dda9b2SRandall Stewart 	}
83448f65f00SMichael Tuexen 	iov.iov_base = dbuf;
83548f65f00SMichael Tuexen 	iov.iov_len = len;
836d6dda9b2SRandall Stewart 	msg.msg_name = (caddr_t)from;
837d6dda9b2SRandall Stewart 	if (fromlen == NULL)
838d6dda9b2SRandall Stewart 		msg.msg_namelen = 0;
839d6dda9b2SRandall Stewart 	else
840d6dda9b2SRandall Stewart 		msg.msg_namelen = *fromlen;
84148f65f00SMichael Tuexen 	msg.msg_iov = &iov;
842d6dda9b2SRandall Stewart 	msg.msg_iovlen = 1;
8437f15a8dfSMichael Tuexen 	msg.msg_control = cmsgbuf;
8447f15a8dfSMichael Tuexen 	msg.msg_controllen = sizeof(cmsgbuf);
8452b8a4d80SMichael Tuexen 	msg.msg_flags = 0;
8462c356be2SRandall Stewart 	sz = recvmsg(s, &msg, *msg_flags);
84748f65f00SMichael Tuexen 	*msg_flags = msg.msg_flags;
84848f65f00SMichael Tuexen 	if (sz <= 0) {
849d6dda9b2SRandall Stewart 		return (sz);
85048f65f00SMichael Tuexen 	}
85148f65f00SMichael Tuexen 	if (sinfo) {
852d6dda9b2SRandall Stewart 		sinfo->sinfo_assoc_id = 0;
85348f65f00SMichael Tuexen 	}
8547f15a8dfSMichael Tuexen 	if ((msg.msg_controllen > 0) && (sinfo != NULL)) {
855d6dda9b2SRandall Stewart 		/*
856d6dda9b2SRandall Stewart 		 * parse through and see if we find the sctp_sndrcvinfo (if
857d6dda9b2SRandall Stewart 		 * the user wants it).
858d6dda9b2SRandall Stewart 		 */
8597f15a8dfSMichael Tuexen 		for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
8607f15a8dfSMichael Tuexen 			if (cmsg->cmsg_level != IPPROTO_SCTP) {
8617f15a8dfSMichael Tuexen 				continue;
862d6dda9b2SRandall Stewart 			}
863d6dda9b2SRandall Stewart 			if (cmsg->cmsg_type == SCTP_SNDRCV) {
8647f15a8dfSMichael Tuexen 				memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo));
865d6dda9b2SRandall Stewart 				break;
8667f15a8dfSMichael Tuexen 			}
8677f15a8dfSMichael Tuexen 			if (cmsg->cmsg_type == SCTP_EXTRCV) {
868d6dda9b2SRandall Stewart 				/*
8697f15a8dfSMichael Tuexen 				 * Let's hope that the user provided enough
8707f15a8dfSMichael Tuexen 				 * enough memory. At least he asked for more
8717f15a8dfSMichael Tuexen 				 * information.
872d6dda9b2SRandall Stewart 				 */
8737f15a8dfSMichael Tuexen 				memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_extrcvinfo));
874d6dda9b2SRandall Stewart 				break;
875d6dda9b2SRandall Stewart 			}
876d6dda9b2SRandall Stewart 		}
877d6dda9b2SRandall Stewart 	}
878d6dda9b2SRandall Stewart 	return (sz);
879d6dda9b2SRandall Stewart #endif
880d6dda9b2SRandall Stewart }
881d6dda9b2SRandall Stewart 
882e2e7c62eSMichael Tuexen ssize_t
883e2e7c62eSMichael Tuexen sctp_recvv(int sd,
884e2e7c62eSMichael Tuexen     const struct iovec *iov,
885e2e7c62eSMichael Tuexen     int iovlen,
886e2e7c62eSMichael Tuexen     struct sockaddr *from,
887e2e7c62eSMichael Tuexen     socklen_t *fromlen,
888e2e7c62eSMichael Tuexen     void *info,
889e2e7c62eSMichael Tuexen     socklen_t *infolen,
890e2e7c62eSMichael Tuexen     unsigned int *infotype,
891e2e7c62eSMichael Tuexen     int *flags)
892d6dda9b2SRandall Stewart {
8937f15a8dfSMichael Tuexen 	char cmsgbuf[SCTP_CONTROL_VEC_SIZE_RCV];
894e2e7c62eSMichael Tuexen 	struct msghdr msg;
895e2e7c62eSMichael Tuexen 	struct cmsghdr *cmsg;
8967f15a8dfSMichael Tuexen 	ssize_t ret;
897e2e7c62eSMichael Tuexen 	struct sctp_rcvinfo *rcvinfo;
898e2e7c62eSMichael Tuexen 	struct sctp_nxtinfo *nxtinfo;
899d6dda9b2SRandall Stewart 
9000d958bd4SMichael Tuexen 	if (((info != NULL) && (infolen == NULL)) ||
9010b064106SMichael Tuexen 	    ((info == NULL) && (infolen != NULL) && (*infolen != 0)) ||
9020b064106SMichael Tuexen 	    ((info != NULL) && (infotype == NULL))) {
9030b064106SMichael Tuexen 		errno = EINVAL;
9040b064106SMichael Tuexen 		return (-1);
9050b064106SMichael Tuexen 	}
906e2e7c62eSMichael Tuexen 	if (infotype) {
907e2e7c62eSMichael Tuexen 		*infotype = SCTP_RECVV_NOINFO;
908e2e7c62eSMichael Tuexen 	}
909e2e7c62eSMichael Tuexen 	msg.msg_name = from;
910e2e7c62eSMichael Tuexen 	if (fromlen == NULL) {
911e2e7c62eSMichael Tuexen 		msg.msg_namelen = 0;
912d6dda9b2SRandall Stewart 	} else {
913e2e7c62eSMichael Tuexen 		msg.msg_namelen = *fromlen;
914d6dda9b2SRandall Stewart 	}
915e2e7c62eSMichael Tuexen 	msg.msg_iov = (struct iovec *)iov;
916e2e7c62eSMichael Tuexen 	msg.msg_iovlen = iovlen;
9177f15a8dfSMichael Tuexen 	msg.msg_control = cmsgbuf;
9187f15a8dfSMichael Tuexen 	msg.msg_controllen = sizeof(cmsgbuf);
9192b8a4d80SMichael Tuexen 	msg.msg_flags = 0;
9207f15a8dfSMichael Tuexen 	ret = recvmsg(sd, &msg, *flags);
921e2e7c62eSMichael Tuexen 	*flags = msg.msg_flags;
9227f15a8dfSMichael Tuexen 	if ((ret > 0) &&
923e2e7c62eSMichael Tuexen 	    (msg.msg_controllen > 0) &&
924e2e7c62eSMichael Tuexen 	    (infotype != NULL) &&
925e2e7c62eSMichael Tuexen 	    (infolen != NULL) &&
926e2e7c62eSMichael Tuexen 	    (*infolen > 0)) {
927e2e7c62eSMichael Tuexen 		rcvinfo = NULL;
928e2e7c62eSMichael Tuexen 		nxtinfo = NULL;
929e2e7c62eSMichael Tuexen 		for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
930e2e7c62eSMichael Tuexen 			if (cmsg->cmsg_level != IPPROTO_SCTP) {
931e2e7c62eSMichael Tuexen 				continue;
932e2e7c62eSMichael Tuexen 			}
933e2e7c62eSMichael Tuexen 			if (cmsg->cmsg_type == SCTP_RCVINFO) {
934e2e7c62eSMichael Tuexen 				rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
9357f15a8dfSMichael Tuexen 				if (nxtinfo != NULL) {
9367f15a8dfSMichael Tuexen 					break;
9377f15a8dfSMichael Tuexen 				} else {
9387f15a8dfSMichael Tuexen 					continue;
9397f15a8dfSMichael Tuexen 				}
940e2e7c62eSMichael Tuexen 			}
941e2e7c62eSMichael Tuexen 			if (cmsg->cmsg_type == SCTP_NXTINFO) {
942e2e7c62eSMichael Tuexen 				nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmsg);
9437f15a8dfSMichael Tuexen 				if (rcvinfo != NULL) {
944e2e7c62eSMichael Tuexen 					break;
9457f15a8dfSMichael Tuexen 				} else {
9467f15a8dfSMichael Tuexen 					continue;
947e2e7c62eSMichael Tuexen 				}
948e2e7c62eSMichael Tuexen 			}
9497f15a8dfSMichael Tuexen 		}
9507f15a8dfSMichael Tuexen 		if (rcvinfo != NULL) {
9517f15a8dfSMichael Tuexen 			if ((nxtinfo != NULL) && (*infolen >= sizeof(struct sctp_recvv_rn))) {
952e2e7c62eSMichael Tuexen 				struct sctp_recvv_rn *rn_info;
953e2e7c62eSMichael Tuexen 
954e2e7c62eSMichael Tuexen 				rn_info = (struct sctp_recvv_rn *)info;
955e2e7c62eSMichael Tuexen 				rn_info->recvv_rcvinfo = *rcvinfo;
956e2e7c62eSMichael Tuexen 				rn_info->recvv_nxtinfo = *nxtinfo;
957e2e7c62eSMichael Tuexen 				*infolen = (socklen_t)sizeof(struct sctp_recvv_rn);
958e2e7c62eSMichael Tuexen 				*infotype = SCTP_RECVV_RN;
9597f15a8dfSMichael Tuexen 			} else if (*infolen >= sizeof(struct sctp_rcvinfo)) {
960e2e7c62eSMichael Tuexen 				memcpy(info, rcvinfo, sizeof(struct sctp_rcvinfo));
961e2e7c62eSMichael Tuexen 				*infolen = (socklen_t)sizeof(struct sctp_rcvinfo);
962e2e7c62eSMichael Tuexen 				*infotype = SCTP_RECVV_RCVINFO;
963e2e7c62eSMichael Tuexen 			}
9647f15a8dfSMichael Tuexen 		} else if (nxtinfo != NULL) {
9657f15a8dfSMichael Tuexen 			if (*infolen >= sizeof(struct sctp_nxtinfo)) {
966e2e7c62eSMichael Tuexen 				memcpy(info, nxtinfo, sizeof(struct sctp_nxtinfo));
967e2e7c62eSMichael Tuexen 				*infolen = (socklen_t)sizeof(struct sctp_nxtinfo);
968e2e7c62eSMichael Tuexen 				*infotype = SCTP_RECVV_NXTINFO;
969e2e7c62eSMichael Tuexen 			}
970e2e7c62eSMichael Tuexen 		}
971e2e7c62eSMichael Tuexen 	}
9727f15a8dfSMichael Tuexen 	return (ret);
973d6dda9b2SRandall Stewart }
974d6dda9b2SRandall Stewart 
975e2e7c62eSMichael Tuexen ssize_t
976e2e7c62eSMichael Tuexen sctp_sendv(int sd,
977e2e7c62eSMichael Tuexen     const struct iovec *iov, int iovcnt,
978e2e7c62eSMichael Tuexen     struct sockaddr *addrs, int addrcnt,
979e2e7c62eSMichael Tuexen     void *info, socklen_t infolen, unsigned int infotype,
980e2e7c62eSMichael Tuexen     int flags)
981e2e7c62eSMichael Tuexen {
982e2e7c62eSMichael Tuexen 	ssize_t ret;
983e2e7c62eSMichael Tuexen 	int i;
9840b064106SMichael Tuexen 	socklen_t addr_len;
985e2e7c62eSMichael Tuexen 	struct msghdr msg;
9860b064106SMichael Tuexen 	in_port_t port;
9870b064106SMichael Tuexen 	struct sctp_sendv_spa *spa_info;
988e2e7c62eSMichael Tuexen 	struct cmsghdr *cmsg;
989e2e7c62eSMichael Tuexen 	char *cmsgbuf;
990e2e7c62eSMichael Tuexen 	struct sockaddr *addr;
991e2e7c62eSMichael Tuexen 	struct sockaddr_in *addr_in;
992e2e7c62eSMichael Tuexen 	struct sockaddr_in6 *addr_in6;
993c7f6ce28SMichael Tuexen 	sctp_assoc_t *assoc_id;
994e2e7c62eSMichael Tuexen 
9950b064106SMichael Tuexen 	if ((addrcnt < 0) ||
9960b064106SMichael Tuexen 	    (iovcnt < 0) ||
997c67a03f9SMichael Tuexen 	    ((addrs == NULL) && (addrcnt > 0)) ||
998c67a03f9SMichael Tuexen 	    ((addrs != NULL) && (addrcnt == 0)) ||
9990b064106SMichael Tuexen 	    ((iov == NULL) && (iovcnt > 0)) ||
10000b064106SMichael Tuexen 	    ((iov != NULL) && (iovcnt == 0))) {
1001e2e7c62eSMichael Tuexen 		errno = EINVAL;
1002e2e7c62eSMichael Tuexen 		return (-1);
1003e2e7c62eSMichael Tuexen 	}
1004e2e7c62eSMichael Tuexen 	cmsgbuf = malloc(CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1005e2e7c62eSMichael Tuexen 	    CMSG_SPACE(sizeof(struct sctp_prinfo)) +
1006e2e7c62eSMichael Tuexen 	    CMSG_SPACE(sizeof(struct sctp_authinfo)) +
10075dc6a815SMichael Tuexen 	    (size_t)addrcnt * CMSG_SPACE(sizeof(struct in6_addr)));
1008e2e7c62eSMichael Tuexen 	if (cmsgbuf == NULL) {
10094ed0ebf6SMichael Tuexen 		errno = ENOMEM;
1010e2e7c62eSMichael Tuexen 		return (-1);
1011e2e7c62eSMichael Tuexen 	}
1012c7f6ce28SMichael Tuexen 	assoc_id = NULL;
1013e2e7c62eSMichael Tuexen 	msg.msg_control = cmsgbuf;
1014e2e7c62eSMichael Tuexen 	msg.msg_controllen = 0;
1015e2e7c62eSMichael Tuexen 	cmsg = (struct cmsghdr *)cmsgbuf;
1016e2e7c62eSMichael Tuexen 	switch (infotype) {
10170b064106SMichael Tuexen 	case SCTP_SENDV_NOINFO:
10180b064106SMichael Tuexen 		if ((infolen != 0) || (info != NULL)) {
10190b064106SMichael Tuexen 			free(cmsgbuf);
10200b064106SMichael Tuexen 			errno = EINVAL;
10210b064106SMichael Tuexen 			return (-1);
10220b064106SMichael Tuexen 		}
10230b064106SMichael Tuexen 		break;
1024e2e7c62eSMichael Tuexen 	case SCTP_SENDV_SNDINFO:
10250b064106SMichael Tuexen 		if ((info == NULL) || (infolen < sizeof(struct sctp_sndinfo))) {
1026e2e7c62eSMichael Tuexen 			free(cmsgbuf);
1027e2e7c62eSMichael Tuexen 			errno = EINVAL;
1028e2e7c62eSMichael Tuexen 			return (-1);
1029e2e7c62eSMichael Tuexen 		}
1030e2e7c62eSMichael Tuexen 		cmsg->cmsg_level = IPPROTO_SCTP;
1031e2e7c62eSMichael Tuexen 		cmsg->cmsg_type = SCTP_SNDINFO;
1032e2e7c62eSMichael Tuexen 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1033e2e7c62eSMichael Tuexen 		memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_sndinfo));
1034e2e7c62eSMichael Tuexen 		msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1035e2e7c62eSMichael Tuexen 		cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo)));
1036c7f6ce28SMichael Tuexen 		assoc_id = &(((struct sctp_sndinfo *)info)->snd_assoc_id);
1037e2e7c62eSMichael Tuexen 		break;
1038e2e7c62eSMichael Tuexen 	case SCTP_SENDV_PRINFO:
10390b064106SMichael Tuexen 		if ((info == NULL) || (infolen < sizeof(struct sctp_prinfo))) {
1040e2e7c62eSMichael Tuexen 			free(cmsgbuf);
1041e2e7c62eSMichael Tuexen 			errno = EINVAL;
1042e2e7c62eSMichael Tuexen 			return (-1);
1043e2e7c62eSMichael Tuexen 		}
1044e2e7c62eSMichael Tuexen 		cmsg->cmsg_level = IPPROTO_SCTP;
1045e2e7c62eSMichael Tuexen 		cmsg->cmsg_type = SCTP_PRINFO;
1046e2e7c62eSMichael Tuexen 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1047e2e7c62eSMichael Tuexen 		memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_prinfo));
1048e2e7c62eSMichael Tuexen 		msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1049e2e7c62eSMichael Tuexen 		cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo)));
1050e2e7c62eSMichael Tuexen 		break;
1051e2e7c62eSMichael Tuexen 	case SCTP_SENDV_AUTHINFO:
10520b064106SMichael Tuexen 		if ((info == NULL) || (infolen < sizeof(struct sctp_authinfo))) {
1053e2e7c62eSMichael Tuexen 			free(cmsgbuf);
1054e2e7c62eSMichael Tuexen 			errno = EINVAL;
1055e2e7c62eSMichael Tuexen 			return (-1);
1056e2e7c62eSMichael Tuexen 		}
1057e2e7c62eSMichael Tuexen 		cmsg->cmsg_level = IPPROTO_SCTP;
1058e2e7c62eSMichael Tuexen 		cmsg->cmsg_type = SCTP_AUTHINFO;
1059e2e7c62eSMichael Tuexen 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo));
1060e2e7c62eSMichael Tuexen 		memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_authinfo));
1061e2e7c62eSMichael Tuexen 		msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo));
1062e2e7c62eSMichael Tuexen 		cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo)));
1063e2e7c62eSMichael Tuexen 		break;
1064e2e7c62eSMichael Tuexen 	case SCTP_SENDV_SPA:
10650b064106SMichael Tuexen 		if ((info == NULL) || (infolen < sizeof(struct sctp_sendv_spa))) {
1066e2e7c62eSMichael Tuexen 			free(cmsgbuf);
1067e2e7c62eSMichael Tuexen 			errno = EINVAL;
1068e2e7c62eSMichael Tuexen 			return (-1);
1069e2e7c62eSMichael Tuexen 		}
1070e2e7c62eSMichael Tuexen 		spa_info = (struct sctp_sendv_spa *)info;
1071e2e7c62eSMichael Tuexen 		if (spa_info->sendv_flags & SCTP_SEND_SNDINFO_VALID) {
1072e2e7c62eSMichael Tuexen 			cmsg->cmsg_level = IPPROTO_SCTP;
1073e2e7c62eSMichael Tuexen 			cmsg->cmsg_type = SCTP_SNDINFO;
1074e2e7c62eSMichael Tuexen 			cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1075e2e7c62eSMichael Tuexen 			memcpy(CMSG_DATA(cmsg), &spa_info->sendv_sndinfo, sizeof(struct sctp_sndinfo));
1076e2e7c62eSMichael Tuexen 			msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1077e2e7c62eSMichael Tuexen 			cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo)));
1078c7f6ce28SMichael Tuexen 			assoc_id = &(spa_info->sendv_sndinfo.snd_assoc_id);
1079e2e7c62eSMichael Tuexen 		}
1080e2e7c62eSMichael Tuexen 		if (spa_info->sendv_flags & SCTP_SEND_PRINFO_VALID) {
1081e2e7c62eSMichael Tuexen 			cmsg->cmsg_level = IPPROTO_SCTP;
1082e2e7c62eSMichael Tuexen 			cmsg->cmsg_type = SCTP_PRINFO;
1083e2e7c62eSMichael Tuexen 			cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1084e2e7c62eSMichael Tuexen 			memcpy(CMSG_DATA(cmsg), &spa_info->sendv_prinfo, sizeof(struct sctp_prinfo));
1085e2e7c62eSMichael Tuexen 			msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1086e2e7c62eSMichael Tuexen 			cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo)));
1087e2e7c62eSMichael Tuexen 		}
1088e2e7c62eSMichael Tuexen 		if (spa_info->sendv_flags & SCTP_SEND_AUTHINFO_VALID) {
1089e2e7c62eSMichael Tuexen 			cmsg->cmsg_level = IPPROTO_SCTP;
1090e2e7c62eSMichael Tuexen 			cmsg->cmsg_type = SCTP_AUTHINFO;
1091e2e7c62eSMichael Tuexen 			cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo));
1092e2e7c62eSMichael Tuexen 			memcpy(CMSG_DATA(cmsg), &spa_info->sendv_authinfo, sizeof(struct sctp_authinfo));
1093e2e7c62eSMichael Tuexen 			msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo));
1094e2e7c62eSMichael Tuexen 			cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo)));
1095e2e7c62eSMichael Tuexen 		}
1096e2e7c62eSMichael Tuexen 		break;
1097e2e7c62eSMichael Tuexen 	default:
1098e2e7c62eSMichael Tuexen 		free(cmsgbuf);
1099e2e7c62eSMichael Tuexen 		errno = EINVAL;
1100e2e7c62eSMichael Tuexen 		return (-1);
1101e2e7c62eSMichael Tuexen 	}
1102e2e7c62eSMichael Tuexen 	addr = addrs;
11030b064106SMichael Tuexen 	msg.msg_name = NULL;
11040b064106SMichael Tuexen 	msg.msg_namelen = 0;
11050b064106SMichael Tuexen 
11060b064106SMichael Tuexen 	for (i = 0; i < addrcnt; i++) {
1107e2e7c62eSMichael Tuexen 		switch (addr->sa_family) {
1108e2e7c62eSMichael Tuexen 		case AF_INET:
11090b064106SMichael Tuexen 			addr_len = (socklen_t)sizeof(struct sockaddr_in);
11100b064106SMichael Tuexen 			addr_in = (struct sockaddr_in *)addr;
11110b064106SMichael Tuexen 			if (addr_in->sin_len != addr_len) {
1112e2e7c62eSMichael Tuexen 				free(cmsgbuf);
1113e2e7c62eSMichael Tuexen 				errno = EINVAL;
1114e2e7c62eSMichael Tuexen 				return (-1);
1115e2e7c62eSMichael Tuexen 			}
11160b064106SMichael Tuexen 			if (i == 0) {
11170b064106SMichael Tuexen 				port = addr_in->sin_port;
1118e2e7c62eSMichael Tuexen 			} else {
11190b064106SMichael Tuexen 				if (port == addr_in->sin_port) {
1120e2e7c62eSMichael Tuexen 					cmsg->cmsg_level = IPPROTO_SCTP;
1121e2e7c62eSMichael Tuexen 					cmsg->cmsg_type = SCTP_DSTADDRV4;
1122e2e7c62eSMichael Tuexen 					cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
1123e2e7c62eSMichael Tuexen 					memcpy(CMSG_DATA(cmsg), &addr_in->sin_addr, sizeof(struct in_addr));
1124e2e7c62eSMichael Tuexen 					msg.msg_controllen += CMSG_SPACE(sizeof(struct in_addr));
1125e2e7c62eSMichael Tuexen 					cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in_addr)));
11260b064106SMichael Tuexen 				} else {
11270b064106SMichael Tuexen 					free(cmsgbuf);
11280b064106SMichael Tuexen 					errno = EINVAL;
11290b064106SMichael Tuexen 					return (-1);
11300b064106SMichael Tuexen 				}
11310b064106SMichael Tuexen 			}
1132e2e7c62eSMichael Tuexen 			break;
1133e2e7c62eSMichael Tuexen 		case AF_INET6:
11340b064106SMichael Tuexen 			addr_len = (socklen_t)sizeof(struct sockaddr_in6);
1135e2e7c62eSMichael Tuexen 			addr_in6 = (struct sockaddr_in6 *)addr;
11360b064106SMichael Tuexen 			if (addr_in6->sin6_len != addr_len) {
11370b064106SMichael Tuexen 				free(cmsgbuf);
11380b064106SMichael Tuexen 				errno = EINVAL;
11390b064106SMichael Tuexen 				return (-1);
11400b064106SMichael Tuexen 			}
11410b064106SMichael Tuexen 			if (i == 0) {
11420b064106SMichael Tuexen 				port = addr_in6->sin6_port;
11430b064106SMichael Tuexen 			} else {
11440b064106SMichael Tuexen 				if (port == addr_in6->sin6_port) {
1145e2e7c62eSMichael Tuexen 					cmsg->cmsg_level = IPPROTO_SCTP;
1146e2e7c62eSMichael Tuexen 					cmsg->cmsg_type = SCTP_DSTADDRV6;
1147e2e7c62eSMichael Tuexen 					cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_addr));
1148e2e7c62eSMichael Tuexen 					memcpy(CMSG_DATA(cmsg), &addr_in6->sin6_addr, sizeof(struct in6_addr));
1149e2e7c62eSMichael Tuexen 					msg.msg_controllen += CMSG_SPACE(sizeof(struct in6_addr));
1150e2e7c62eSMichael Tuexen 					cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in6_addr)));
11510b064106SMichael Tuexen 				} else {
11520b064106SMichael Tuexen 					free(cmsgbuf);
11530b064106SMichael Tuexen 					errno = EINVAL;
11540b064106SMichael Tuexen 					return (-1);
11550b064106SMichael Tuexen 				}
11560b064106SMichael Tuexen 			}
1157e2e7c62eSMichael Tuexen 			break;
1158e2e7c62eSMichael Tuexen 		default:
1159e2e7c62eSMichael Tuexen 			free(cmsgbuf);
1160e2e7c62eSMichael Tuexen 			errno = EINVAL;
1161e2e7c62eSMichael Tuexen 			return (-1);
1162e2e7c62eSMichael Tuexen 		}
11630b064106SMichael Tuexen 		if (i == 0) {
11640b064106SMichael Tuexen 			msg.msg_name = addr;
11650b064106SMichael Tuexen 			msg.msg_namelen = addr_len;
11660b064106SMichael Tuexen 		}
1167e2e7c62eSMichael Tuexen 		addr = (struct sockaddr *)((caddr_t)addr + addr_len);
1168e2e7c62eSMichael Tuexen 	}
11690b064106SMichael Tuexen 	if (msg.msg_controllen == 0) {
11700b064106SMichael Tuexen 		msg.msg_control = NULL;
1171e2e7c62eSMichael Tuexen 	}
1172e2e7c62eSMichael Tuexen 	msg.msg_iov = (struct iovec *)iov;
1173e2e7c62eSMichael Tuexen 	msg.msg_iovlen = iovcnt;
1174e2e7c62eSMichael Tuexen 	msg.msg_flags = 0;
1175e2e7c62eSMichael Tuexen 	ret = sendmsg(sd, &msg, flags);
1176e2e7c62eSMichael Tuexen 	free(cmsgbuf);
1177c7f6ce28SMichael Tuexen 	if ((ret >= 0) && (addrs != NULL) && (assoc_id != NULL)) {
1178c7f6ce28SMichael Tuexen 		*assoc_id = sctp_getassocid(sd, addrs);
1179c7f6ce28SMichael Tuexen 	}
1180e2e7c62eSMichael Tuexen 	return (ret);
1181e2e7c62eSMichael Tuexen }
1182e2e7c62eSMichael Tuexen 
1183d6dda9b2SRandall Stewart 
1184d6dda9b2SRandall Stewart #if !defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT)
1185d6dda9b2SRandall Stewart 
1186d6dda9b2SRandall Stewart int
1187d6dda9b2SRandall Stewart sctp_peeloff(int sd, sctp_assoc_t assoc_id)
1188d6dda9b2SRandall Stewart {
1189d6dda9b2SRandall Stewart 	/* NOT supported, return invalid sd */
1190d6dda9b2SRandall Stewart 	errno = ENOTSUP;
1191d6dda9b2SRandall Stewart 	return (-1);
1192d6dda9b2SRandall Stewart }
1193d6dda9b2SRandall Stewart 
1194d6dda9b2SRandall Stewart #endif
1195d6dda9b2SRandall Stewart #if defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT)
1196d6dda9b2SRandall Stewart int
1197d6dda9b2SRandall Stewart sctp_peeloff(int sd, sctp_assoc_t assoc_id)
1198d6dda9b2SRandall Stewart {
1199d6dda9b2SRandall Stewart 	return (syscall(SYS_sctp_peeloff, sd, assoc_id));
1200d6dda9b2SRandall Stewart }
1201d6dda9b2SRandall Stewart 
1202d6dda9b2SRandall Stewart #endif
1203804cf641SRandall Stewart 
12042c0d559dSRandall Stewart #undef SCTP_CONTROL_VEC_SIZE_RCV
1205