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