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