xref: /freebsd/lib/libc/net/sctp_sys_calls.c (revision a831d7d1c538b93ffec095657d9a6e6e778db195)
1 /*-
2  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * a) Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  * b) Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the distribution.
15  *
16  * c) Neither the name of Cisco Systems, Inc. nor the names of its
17  *    contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <sys/errno.h>
44 #include <sys/syscall.h>
45 #include <sys/uio.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
48 #include <netinet/sctp_uio.h>
49 #include <netinet/sctp.h>
50 
51 #ifndef IN6_IS_ADDR_V4MAPPED
52 #define IN6_IS_ADDR_V4MAPPED(a)		      \
53 	((*(const uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) &&	\
54 	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) &&	\
55 	 (*(const uint32_t *)(const void *)(&(a)->s6_addr[8]) == ntohl(0x0000ffff)))
56 #endif
57 
58 #define SCTP_CONTROL_VEC_SIZE_RCV  16384
59 
60 
61 static void
62 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6)
63 {
64 	bzero(sin, sizeof(*sin));
65 	sin->sin_len = sizeof(struct sockaddr_in);
66 	sin->sin_family = AF_INET;
67 	sin->sin_port = sin6->sin6_port;
68 	sin->sin_addr.s_addr = sin6->sin6_addr.__u6_addr.__u6_addr32[3];
69 }
70 
71 int
72 sctp_getaddrlen(sa_family_t family)
73 {
74 	int ret, sd;
75 	socklen_t siz;
76 	struct sctp_assoc_value av;
77 
78 	av.assoc_value = family;
79 	siz = sizeof(av);
80 #if defined(AF_INET)
81 	sd = socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);
82 #elif defined(AF_INET6)
83 	sd = socket(AF_INET6, SOCK_SEQPACKET, IPPROTO_SCTP);
84 #else
85 	sd = -1;
86 #endif
87 	if (sd == -1) {
88 		return (-1);
89 	}
90 	ret = getsockopt(sd, IPPROTO_SCTP, SCTP_GET_ADDR_LEN, &av, &siz);
91 	close(sd);
92 	if (ret == 0) {
93 		return ((int)av.assoc_value);
94 	} else {
95 		return (-1);
96 	}
97 }
98 
99 int
100 sctp_connectx(int sd, const struct sockaddr *addrs, int addrcnt,
101     sctp_assoc_t * id)
102 {
103 	char *buf;
104 	int i, ret, *aa;
105 	char *cpto;
106 	const struct sockaddr *at;
107 	size_t len;
108 
109 	/* validate the address count and list */
110 	if ((addrs == NULL) || (addrcnt <= 0)) {
111 		errno = EINVAL;
112 		return (-1);
113 	}
114 	if ((buf = malloc(sizeof(int) + (size_t)addrcnt * sizeof(struct sockaddr_in6))) == NULL) {
115 		errno = E2BIG;
116 		return (-1);
117 	}
118 	len = sizeof(int);
119 	at = addrs;
120 	cpto = buf + sizeof(int);
121 	/* validate all the addresses and get the size */
122 	for (i = 0; i < addrcnt; i++) {
123 		switch (at->sa_family) {
124 		case AF_INET:
125 			if (at->sa_len != sizeof(struct sockaddr_in)) {
126 				free(buf);
127 				errno = EINVAL;
128 				return (-1);
129 			}
130 			memcpy(cpto, at, sizeof(struct sockaddr_in));
131 			cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in));
132 			len += sizeof(struct sockaddr_in);
133 			break;
134 		case AF_INET6:
135 			if (at->sa_len != sizeof(struct sockaddr_in6)) {
136 				free(buf);
137 				errno = EINVAL;
138 				return (-1);
139 			}
140 			if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)at)->sin6_addr)) {
141 				in6_sin6_2_sin((struct sockaddr_in *)cpto, (struct sockaddr_in6 *)at);
142 				cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in));
143 				len += sizeof(struct sockaddr_in);
144 			} else {
145 				memcpy(cpto, at, sizeof(struct sockaddr_in6));
146 				cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in6));
147 				len += sizeof(struct sockaddr_in6);
148 			}
149 			break;
150 		default:
151 			free(buf);
152 			errno = EINVAL;
153 			return (-1);
154 		}
155 		at = (struct sockaddr *)((caddr_t)at + at->sa_len);
156 	}
157 	aa = (int *)buf;
158 	*aa = addrcnt;
159 	ret = setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X, (void *)buf,
160 	    (socklen_t) len);
161 	if ((ret == 0) && (id != NULL)) {
162 		*id = *(sctp_assoc_t *) buf;
163 	}
164 	free(buf);
165 	return (ret);
166 }
167 
168 int
169 sctp_bindx(int sd, struct sockaddr *addrs, int addrcnt, int flags)
170 {
171 	struct sctp_getaddresses *gaddrs;
172 	struct sockaddr *sa;
173 	struct sockaddr_in *sin;
174 	struct sockaddr_in6 *sin6;
175 	int i;
176 	size_t argsz;
177 	uint16_t sport = 0;
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 	/* First pre-screen the addresses */
191 	sa = addrs;
192 	for (i = 0; i < addrcnt; i++) {
193 		switch (sa->sa_family) {
194 		case AF_INET:
195 			if (sa->sa_len != sizeof(struct sockaddr_in)) {
196 				errno = EINVAL;
197 				return (-1);
198 			}
199 			sin = (struct sockaddr_in *)sa;
200 			if (sin->sin_port) {
201 				/* non-zero port, check or save */
202 				if (sport) {
203 					/* Check against our port */
204 					if (sport != sin->sin_port) {
205 						errno = EINVAL;
206 						return (-1);
207 					}
208 				} else {
209 					/* save off the port */
210 					sport = sin->sin_port;
211 				}
212 			}
213 			break;
214 		case AF_INET6:
215 			if (sa->sa_len != sizeof(struct sockaddr_in6)) {
216 				errno = EINVAL;
217 				return (-1);
218 			}
219 			sin6 = (struct sockaddr_in6 *)sa;
220 			if (sin6->sin6_port) {
221 				/* non-zero port, check or save */
222 				if (sport) {
223 					/* Check against our port */
224 					if (sport != sin6->sin6_port) {
225 						errno = EINVAL;
226 						return (-1);
227 					}
228 				} else {
229 					/* save off the port */
230 					sport = sin6->sin6_port;
231 				}
232 			}
233 			break;
234 		default:
235 			/* Invalid address family specified. */
236 			errno = EAFNOSUPPORT;
237 			return (-1);
238 		}
239 		sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
240 	}
241 	argsz = sizeof(struct sctp_getaddresses) +
242 	    sizeof(struct sockaddr_storage);
243 	if ((gaddrs = (struct sctp_getaddresses *)malloc(argsz)) == NULL) {
244 		errno = ENOMEM;
245 		return (-1);
246 	}
247 	sa = addrs;
248 	for (i = 0; i < addrcnt; i++) {
249 		memset(gaddrs, 0, argsz);
250 		gaddrs->sget_assoc_id = 0;
251 		memcpy(gaddrs->addr, sa, sa->sa_len);
252 		/*
253 		 * Now, if there was a port mentioned, assure that the first
254 		 * address has that port to make sure it fails or succeeds
255 		 * correctly.
256 		 */
257 		if ((i == 0) && (sport != 0)) {
258 			switch (gaddrs->addr->sa_family) {
259 			case AF_INET:
260 				sin = (struct sockaddr_in *)gaddrs->addr;
261 				sin->sin_port = sport;
262 				break;
263 			case AF_INET6:
264 				sin6 = (struct sockaddr_in6 *)gaddrs->addr;
265 				sin6->sin6_port = sport;
266 				break;
267 			}
268 		}
269 		if (setsockopt(sd, IPPROTO_SCTP, flags, gaddrs,
270 		    (socklen_t) argsz) != 0) {
271 			free(gaddrs);
272 			return (-1);
273 		}
274 		sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
275 	}
276 	free(gaddrs);
277 	return (0);
278 }
279 
280 int
281 sctp_opt_info(int sd, sctp_assoc_t id, int opt, void *arg, socklen_t * size)
282 {
283 	if (arg == NULL) {
284 		errno = EINVAL;
285 		return (-1);
286 	}
287 	if ((id == SCTP_CURRENT_ASSOC) ||
288 	    (id == SCTP_ALL_ASSOC)) {
289 		errno = EINVAL;
290 		return (-1);
291 	}
292 	switch (opt) {
293 	case SCTP_RTOINFO:
294 		((struct sctp_rtoinfo *)arg)->srto_assoc_id = id;
295 		break;
296 	case SCTP_ASSOCINFO:
297 		((struct sctp_assocparams *)arg)->sasoc_assoc_id = id;
298 		break;
299 	case SCTP_DEFAULT_SEND_PARAM:
300 		((struct sctp_assocparams *)arg)->sasoc_assoc_id = id;
301 		break;
302 	case SCTP_PRIMARY_ADDR:
303 		((struct sctp_setprim *)arg)->ssp_assoc_id = id;
304 		break;
305 	case SCTP_PEER_ADDR_PARAMS:
306 		((struct sctp_paddrparams *)arg)->spp_assoc_id = id;
307 		break;
308 	case SCTP_MAXSEG:
309 		((struct sctp_assoc_value *)arg)->assoc_id = id;
310 		break;
311 	case SCTP_AUTH_KEY:
312 		((struct sctp_authkey *)arg)->sca_assoc_id = id;
313 		break;
314 	case SCTP_AUTH_ACTIVE_KEY:
315 		((struct sctp_authkeyid *)arg)->scact_assoc_id = id;
316 		break;
317 	case SCTP_DELAYED_SACK:
318 		((struct sctp_sack_info *)arg)->sack_assoc_id = id;
319 		break;
320 	case SCTP_CONTEXT:
321 		((struct sctp_assoc_value *)arg)->assoc_id = id;
322 		break;
323 	case SCTP_STATUS:
324 		((struct sctp_status *)arg)->sstat_assoc_id = id;
325 		break;
326 	case SCTP_GET_PEER_ADDR_INFO:
327 		((struct sctp_paddrinfo *)arg)->spinfo_assoc_id = id;
328 		break;
329 	case SCTP_PEER_AUTH_CHUNKS:
330 		((struct sctp_authchunks *)arg)->gauth_assoc_id = id;
331 		break;
332 	case SCTP_LOCAL_AUTH_CHUNKS:
333 		((struct sctp_authchunks *)arg)->gauth_assoc_id = id;
334 		break;
335 	case SCTP_TIMEOUTS:
336 		((struct sctp_timeouts *)arg)->stimo_assoc_id = id;
337 		break;
338 	case SCTP_EVENT:
339 		((struct sctp_event *)arg)->se_assoc_id = id;
340 		break;
341 	case SCTP_DEFAULT_SNDINFO:
342 		((struct sctp_sndinfo *)arg)->snd_assoc_id = id;
343 		break;
344 	case SCTP_DEFAULT_PRINFO:
345 		((struct sctp_default_prinfo *)arg)->pr_assoc_id = id;
346 		break;
347 	case SCTP_PEER_ADDR_THLDS:
348 		((struct sctp_paddrthlds *)arg)->spt_assoc_id = id;
349 		break;
350 	case SCTP_REMOTE_UDP_ENCAPS_PORT:
351 		((struct sctp_udpencaps *)arg)->sue_assoc_id = id;
352 		break;
353 	case SCTP_ECN_SUPPORTED:
354 		((struct sctp_assoc_value *)arg)->assoc_id = id;
355 		break;
356 	case SCTP_PR_SUPPORTED:
357 		((struct sctp_assoc_value *)arg)->assoc_id = id;
358 		break;
359 	case SCTP_AUTH_SUPPORTED:
360 		((struct sctp_assoc_value *)arg)->assoc_id = id;
361 		break;
362 	case SCTP_ASCONF_SUPPORTED:
363 		((struct sctp_assoc_value *)arg)->assoc_id = id;
364 		break;
365 	case SCTP_RECONFIG_SUPPORTED:
366 		((struct sctp_assoc_value *)arg)->assoc_id = id;
367 		break;
368 	case SCTP_NRSACK_SUPPORTED:
369 		((struct sctp_assoc_value *)arg)->assoc_id = id;
370 		break;
371 	case SCTP_PKTDROP_SUPPORTED:
372 		((struct sctp_assoc_value *)arg)->assoc_id = id;
373 		break;
374 	case SCTP_MAX_BURST:
375 		((struct sctp_assoc_value *)arg)->assoc_id = id;
376 		break;
377 	case SCTP_ENABLE_STREAM_RESET:
378 		((struct sctp_assoc_value *)arg)->assoc_id = id;
379 		break;
380 	case SCTP_PR_STREAM_STATUS:
381 		((struct sctp_prstatus *)arg)->sprstat_assoc_id = id;
382 		break;
383 	case SCTP_PR_ASSOC_STATUS:
384 		((struct sctp_prstatus *)arg)->sprstat_assoc_id = id;
385 		break;
386 	default:
387 		break;
388 	}
389 	return (getsockopt(sd, IPPROTO_SCTP, opt, arg, size));
390 }
391 
392 int
393 sctp_getpaddrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs)
394 {
395 	struct sctp_getaddresses *addrs;
396 	struct sockaddr *sa;
397 	sctp_assoc_t asoc;
398 	caddr_t lim;
399 	socklen_t opt_len;
400 	int cnt;
401 
402 	if (raddrs == NULL) {
403 		errno = EFAULT;
404 		return (-1);
405 	}
406 	asoc = id;
407 	opt_len = (socklen_t) sizeof(sctp_assoc_t);
408 	if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_REMOTE_ADDR_SIZE,
409 	    &asoc, &opt_len) != 0) {
410 		return (-1);
411 	}
412 	/* size required is returned in 'asoc' */
413 	opt_len = (socklen_t) ((size_t)asoc + sizeof(struct sctp_getaddresses));
414 	addrs = calloc(1, (size_t)opt_len);
415 	if (addrs == NULL) {
416 		errno = ENOMEM;
417 		return (-1);
418 	}
419 	addrs->sget_assoc_id = id;
420 	/* Now lets get the array of addresses */
421 	if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_PEER_ADDRESSES,
422 	    addrs, &opt_len) != 0) {
423 		free(addrs);
424 		return (-1);
425 	}
426 	*raddrs = (struct sockaddr *)&addrs->addr[0];
427 	cnt = 0;
428 	sa = (struct sockaddr *)&addrs->addr[0];
429 	lim = (caddr_t)addrs + opt_len;
430 	while (((caddr_t)sa < lim) && (sa->sa_len > 0)) {
431 		sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
432 		cnt++;
433 	}
434 	return (cnt);
435 }
436 
437 void
438 sctp_freepaddrs(struct sockaddr *addrs)
439 {
440 	void *fr_addr;
441 
442 	/* Take away the hidden association id */
443 	fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t));
444 	/* Now free it */
445 	free(fr_addr);
446 }
447 
448 int
449 sctp_getladdrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs)
450 {
451 	struct sctp_getaddresses *addrs;
452 	caddr_t lim;
453 	struct sockaddr *sa;
454 	size_t size_of_addresses;
455 	socklen_t opt_len;
456 	int cnt;
457 
458 	if (raddrs == NULL) {
459 		errno = EFAULT;
460 		return (-1);
461 	}
462 	size_of_addresses = 0;
463 	opt_len = (socklen_t) sizeof(int);
464 	if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDR_SIZE,
465 	    &size_of_addresses, &opt_len) != 0) {
466 		errno = ENOMEM;
467 		return (-1);
468 	}
469 	if (size_of_addresses == 0) {
470 		errno = ENOTCONN;
471 		return (-1);
472 	}
473 	opt_len = (socklen_t) (size_of_addresses +
474 	    sizeof(struct sockaddr_storage) +
475 	    sizeof(struct sctp_getaddresses));
476 	addrs = calloc(1, (size_t)opt_len);
477 	if (addrs == NULL) {
478 		errno = ENOMEM;
479 		return (-1);
480 	}
481 	addrs->sget_assoc_id = id;
482 	/* Now lets get the array of addresses */
483 	if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDRESSES, addrs,
484 	    &opt_len) != 0) {
485 		free(addrs);
486 		errno = ENOMEM;
487 		return (-1);
488 	}
489 	*raddrs = (struct sockaddr *)&addrs->addr[0];
490 	cnt = 0;
491 	sa = (struct sockaddr *)&addrs->addr[0];
492 	lim = (caddr_t)addrs + opt_len;
493 	while (((caddr_t)sa < lim) && (sa->sa_len > 0)) {
494 		sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
495 		cnt++;
496 	}
497 	return (cnt);
498 }
499 
500 void
501 sctp_freeladdrs(struct sockaddr *addrs)
502 {
503 	void *fr_addr;
504 
505 	/* Take away the hidden association id */
506 	fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t));
507 	/* Now free it */
508 	free(fr_addr);
509 }
510 
511 ssize_t
512 sctp_sendmsg(int s,
513     const void *data,
514     size_t len,
515     const struct sockaddr *to,
516     socklen_t tolen,
517     uint32_t ppid,
518     uint32_t flags,
519     uint16_t stream_no,
520     uint32_t timetolive,
521     uint32_t context)
522 {
523 #ifdef SYS_sctp_generic_sendmsg
524 	struct sctp_sndrcvinfo sinfo;
525 
526 	memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
527 	sinfo.sinfo_ppid = ppid;
528 	sinfo.sinfo_flags = flags;
529 	sinfo.sinfo_stream = stream_no;
530 	sinfo.sinfo_timetolive = timetolive;
531 	sinfo.sinfo_context = context;
532 	sinfo.sinfo_assoc_id = 0;
533 	return (syscall(SYS_sctp_generic_sendmsg, s,
534 	    data, len, to, tolen, &sinfo, 0));
535 #else
536 	struct msghdr msg;
537 	struct sctp_sndrcvinfo *sinfo;
538 	struct iovec iov;
539 	char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
540 	struct cmsghdr *cmsg;
541 	struct sockaddr *who = NULL;
542 	union {
543 		struct sockaddr_in in;
544 		struct sockaddr_in6 in6;
545 	}     addr;
546 
547 	if ((tolen > 0) &&
548 	    ((to == NULL) || (tolen < sizeof(struct sockaddr)))) {
549 		errno = EINVAL;
550 		return (-1);
551 	}
552 	if ((to != NULL) && (tolen > 0)) {
553 		switch (to->sa_family) {
554 		case AF_INET:
555 			if (tolen != sizeof(struct sockaddr_in)) {
556 				errno = EINVAL;
557 				return (-1);
558 			}
559 			if ((to->sa_len > 0) &&
560 			    (to->sa_len != sizeof(struct sockaddr_in))) {
561 				errno = EINVAL;
562 				return (-1);
563 			}
564 			memcpy(&addr, to, sizeof(struct sockaddr_in));
565 			addr.in.sin_len = sizeof(struct sockaddr_in);
566 			break;
567 		case AF_INET6:
568 			if (tolen != sizeof(struct sockaddr_in6)) {
569 				errno = EINVAL;
570 				return (-1);
571 			}
572 			if ((to->sa_len > 0) &&
573 			    (to->sa_len != sizeof(struct sockaddr_in6))) {
574 				errno = EINVAL;
575 				return (-1);
576 			}
577 			memcpy(&addr, to, sizeof(struct sockaddr_in6));
578 			addr.in6.sin6_len = sizeof(struct sockaddr_in6);
579 			break;
580 		default:
581 			errno = EAFNOSUPPORT;
582 			return (-1);
583 		}
584 		who = (struct sockaddr *)&addr;
585 	}
586 	iov.iov_base = (char *)data;
587 	iov.iov_len = len;
588 
589 	if (who) {
590 		msg.msg_name = (caddr_t)who;
591 		msg.msg_namelen = who->sa_len;
592 	} else {
593 		msg.msg_name = (caddr_t)NULL;
594 		msg.msg_namelen = 0;
595 	}
596 	msg.msg_iov = &iov;
597 	msg.msg_iovlen = 1;
598 	msg.msg_control = cmsgbuf;
599 	msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
600 	msg.msg_flags = 0;
601 	cmsg = (struct cmsghdr *)cmsgbuf;
602 	cmsg->cmsg_level = IPPROTO_SCTP;
603 	cmsg->cmsg_type = SCTP_SNDRCV;
604 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
605 	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
606 	memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
607 	sinfo->sinfo_stream = stream_no;
608 	sinfo->sinfo_ssn = 0;
609 	sinfo->sinfo_flags = flags;
610 	sinfo->sinfo_ppid = ppid;
611 	sinfo->sinfo_context = context;
612 	sinfo->sinfo_assoc_id = 0;
613 	sinfo->sinfo_timetolive = timetolive;
614 	return (sendmsg(s, &msg, 0));
615 #endif
616 }
617 
618 
619 sctp_assoc_t
620 sctp_getassocid(int sd, struct sockaddr *sa)
621 {
622 	struct sctp_paddrinfo sp;
623 	socklen_t siz;
624 
625 	/* First get the assoc id */
626 	siz = sizeof(sp);
627 	memset(&sp, 0, sizeof(sp));
628 	memcpy((caddr_t)&sp.spinfo_address, sa, sa->sa_len);
629 	if (getsockopt(sd, IPPROTO_SCTP,
630 	    SCTP_GET_PEER_ADDR_INFO, &sp, &siz) != 0) {
631 		/* We depend on the fact that 0 can never be returned */
632 		return ((sctp_assoc_t) 0);
633 	}
634 	return (sp.spinfo_assoc_id);
635 }
636 
637 ssize_t
638 sctp_send(int sd, const void *data, size_t len,
639     const struct sctp_sndrcvinfo *sinfo,
640     int flags)
641 {
642 
643 #ifdef SYS_sctp_generic_sendmsg
644 	struct sockaddr *to = NULL;
645 
646 	return (syscall(SYS_sctp_generic_sendmsg, sd,
647 	    data, len, to, 0, sinfo, flags));
648 #else
649 	struct msghdr msg;
650 	struct iovec iov;
651 	char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
652 	struct cmsghdr *cmsg;
653 
654 	if (sinfo == NULL) {
655 		errno = EINVAL;
656 		return (-1);
657 	}
658 	iov.iov_base = (char *)data;
659 	iov.iov_len = len;
660 
661 	msg.msg_name = NULL;
662 	msg.msg_namelen = 0;
663 	msg.msg_iov = &iov;
664 	msg.msg_iovlen = 1;
665 	msg.msg_control = cmsgbuf;
666 	msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
667 	msg.msg_flags = 0;
668 	cmsg = (struct cmsghdr *)cmsgbuf;
669 	cmsg->cmsg_level = IPPROTO_SCTP;
670 	cmsg->cmsg_type = SCTP_SNDRCV;
671 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
672 	memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo));
673 	return (sendmsg(sd, &msg, flags));
674 #endif
675 }
676 
677 
678 
679 ssize_t
680 sctp_sendx(int sd, const void *msg, size_t msg_len,
681     struct sockaddr *addrs, int addrcnt,
682     struct sctp_sndrcvinfo *sinfo,
683     int flags)
684 {
685 	struct sctp_sndrcvinfo __sinfo;
686 	ssize_t ret;
687 	int i, cnt, *aa, saved_errno;
688 	char *buf;
689 	int no_end_cx = 0;
690 	size_t len, add_len;
691 	struct sockaddr *at;
692 
693 	if (addrs == NULL) {
694 		errno = EINVAL;
695 		return (-1);
696 	}
697 #ifdef SYS_sctp_generic_sendmsg
698 	if (addrcnt == 1) {
699 		socklen_t l;
700 
701 		/*
702 		 * Quick way, we don't need to do a connectx so lets use the
703 		 * syscall directly.
704 		 */
705 		l = addrs->sa_len;
706 		return (syscall(SYS_sctp_generic_sendmsg, sd,
707 		    msg, msg_len, addrs, l, sinfo, flags));
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_ssn = 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 
985 	if ((addrcnt < 0) ||
986 	    (iovcnt < 0) ||
987 	    ((addrs == NULL) && (addrcnt > 0)) ||
988 	    ((addrs != NULL) && (addrcnt == 0)) ||
989 	    ((iov == NULL) && (iovcnt > 0)) ||
990 	    ((iov != NULL) && (iovcnt == 0))) {
991 		errno = EINVAL;
992 		return (-1);
993 	}
994 	cmsgbuf = malloc(CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
995 	    CMSG_SPACE(sizeof(struct sctp_prinfo)) +
996 	    CMSG_SPACE(sizeof(struct sctp_authinfo)) +
997 	    (size_t)addrcnt * CMSG_SPACE(sizeof(struct in6_addr)));
998 	if (cmsgbuf == NULL) {
999 		errno = ENOMEM;
1000 		return (-1);
1001 	}
1002 	msg.msg_control = cmsgbuf;
1003 	msg.msg_controllen = 0;
1004 	cmsg = (struct cmsghdr *)cmsgbuf;
1005 	switch (infotype) {
1006 	case SCTP_SENDV_NOINFO:
1007 		if ((infolen != 0) || (info != NULL)) {
1008 			free(cmsgbuf);
1009 			errno = EINVAL;
1010 			return (-1);
1011 		}
1012 		break;
1013 	case SCTP_SENDV_SNDINFO:
1014 		if ((info == NULL) || (infolen < sizeof(struct sctp_sndinfo))) {
1015 			free(cmsgbuf);
1016 			errno = EINVAL;
1017 			return (-1);
1018 		}
1019 		cmsg->cmsg_level = IPPROTO_SCTP;
1020 		cmsg->cmsg_type = SCTP_SNDINFO;
1021 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1022 		memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_sndinfo));
1023 		msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1024 		cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo)));
1025 		break;
1026 	case SCTP_SENDV_PRINFO:
1027 		if ((info == NULL) || (infolen < sizeof(struct sctp_prinfo))) {
1028 			free(cmsgbuf);
1029 			errno = EINVAL;
1030 			return (-1);
1031 		}
1032 		cmsg->cmsg_level = IPPROTO_SCTP;
1033 		cmsg->cmsg_type = SCTP_PRINFO;
1034 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1035 		memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_prinfo));
1036 		msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1037 		cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo)));
1038 		break;
1039 	case SCTP_SENDV_AUTHINFO:
1040 		if ((info == NULL) || (infolen < sizeof(struct sctp_authinfo))) {
1041 			free(cmsgbuf);
1042 			errno = EINVAL;
1043 			return (-1);
1044 		}
1045 		cmsg->cmsg_level = IPPROTO_SCTP;
1046 		cmsg->cmsg_type = SCTP_AUTHINFO;
1047 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo));
1048 		memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_authinfo));
1049 		msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo));
1050 		cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo)));
1051 		break;
1052 	case SCTP_SENDV_SPA:
1053 		if ((info == NULL) || (infolen < sizeof(struct sctp_sendv_spa))) {
1054 			free(cmsgbuf);
1055 			errno = EINVAL;
1056 			return (-1);
1057 		}
1058 		spa_info = (struct sctp_sendv_spa *)info;
1059 		if (spa_info->sendv_flags & SCTP_SEND_SNDINFO_VALID) {
1060 			cmsg->cmsg_level = IPPROTO_SCTP;
1061 			cmsg->cmsg_type = SCTP_SNDINFO;
1062 			cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1063 			memcpy(CMSG_DATA(cmsg), &spa_info->sendv_sndinfo, sizeof(struct sctp_sndinfo));
1064 			msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1065 			cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo)));
1066 		}
1067 		if (spa_info->sendv_flags & SCTP_SEND_PRINFO_VALID) {
1068 			cmsg->cmsg_level = IPPROTO_SCTP;
1069 			cmsg->cmsg_type = SCTP_PRINFO;
1070 			cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1071 			memcpy(CMSG_DATA(cmsg), &spa_info->sendv_prinfo, sizeof(struct sctp_prinfo));
1072 			msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1073 			cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo)));
1074 		}
1075 		if (spa_info->sendv_flags & SCTP_SEND_AUTHINFO_VALID) {
1076 			cmsg->cmsg_level = IPPROTO_SCTP;
1077 			cmsg->cmsg_type = SCTP_AUTHINFO;
1078 			cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo));
1079 			memcpy(CMSG_DATA(cmsg), &spa_info->sendv_authinfo, sizeof(struct sctp_authinfo));
1080 			msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo));
1081 			cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo)));
1082 		}
1083 		break;
1084 	default:
1085 		free(cmsgbuf);
1086 		errno = EINVAL;
1087 		return (-1);
1088 	}
1089 	addr = addrs;
1090 	msg.msg_name = NULL;
1091 	msg.msg_namelen = 0;
1092 
1093 	for (i = 0; i < addrcnt; i++) {
1094 		switch (addr->sa_family) {
1095 		case AF_INET:
1096 			addr_len = (socklen_t) sizeof(struct sockaddr_in);
1097 			addr_in = (struct sockaddr_in *)addr;
1098 			if (addr_in->sin_len != addr_len) {
1099 				free(cmsgbuf);
1100 				errno = EINVAL;
1101 				return (-1);
1102 			}
1103 			if (i == 0) {
1104 				port = addr_in->sin_port;
1105 			} else {
1106 				if (port == addr_in->sin_port) {
1107 					cmsg->cmsg_level = IPPROTO_SCTP;
1108 					cmsg->cmsg_type = SCTP_DSTADDRV4;
1109 					cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
1110 					memcpy(CMSG_DATA(cmsg), &addr_in->sin_addr, sizeof(struct in_addr));
1111 					msg.msg_controllen += CMSG_SPACE(sizeof(struct in_addr));
1112 					cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in_addr)));
1113 				} else {
1114 					free(cmsgbuf);
1115 					errno = EINVAL;
1116 					return (-1);
1117 				}
1118 			}
1119 			break;
1120 		case AF_INET6:
1121 			addr_len = (socklen_t) sizeof(struct sockaddr_in6);
1122 			addr_in6 = (struct sockaddr_in6 *)addr;
1123 			if (addr_in6->sin6_len != addr_len) {
1124 				free(cmsgbuf);
1125 				errno = EINVAL;
1126 				return (-1);
1127 			}
1128 			if (i == 0) {
1129 				port = addr_in6->sin6_port;
1130 			} else {
1131 				if (port == addr_in6->sin6_port) {
1132 					cmsg->cmsg_level = IPPROTO_SCTP;
1133 					cmsg->cmsg_type = SCTP_DSTADDRV6;
1134 					cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_addr));
1135 					memcpy(CMSG_DATA(cmsg), &addr_in6->sin6_addr, sizeof(struct in6_addr));
1136 					msg.msg_controllen += CMSG_SPACE(sizeof(struct in6_addr));
1137 					cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in6_addr)));
1138 				} else {
1139 					free(cmsgbuf);
1140 					errno = EINVAL;
1141 					return (-1);
1142 				}
1143 			}
1144 			break;
1145 		default:
1146 			free(cmsgbuf);
1147 			errno = EINVAL;
1148 			return (-1);
1149 		}
1150 		if (i == 0) {
1151 			msg.msg_name = addr;
1152 			msg.msg_namelen = addr_len;
1153 		}
1154 		addr = (struct sockaddr *)((caddr_t)addr + addr_len);
1155 	}
1156 	if (msg.msg_controllen == 0) {
1157 		msg.msg_control = NULL;
1158 	}
1159 	msg.msg_iov = (struct iovec *)iov;
1160 	msg.msg_iovlen = iovcnt;
1161 	msg.msg_flags = 0;
1162 	ret = sendmsg(sd, &msg, flags);
1163 	free(cmsgbuf);
1164 	return (ret);
1165 }
1166 
1167 
1168 #if !defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT)
1169 
1170 int
1171 sctp_peeloff(int sd, sctp_assoc_t assoc_id)
1172 {
1173 	/* NOT supported, return invalid sd */
1174 	errno = ENOTSUP;
1175 	return (-1);
1176 }
1177 
1178 #endif
1179 #if defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT)
1180 int
1181 sctp_peeloff(int sd, sctp_assoc_t assoc_id)
1182 {
1183 	return (syscall(SYS_sctp_peeloff, sd, assoc_id));
1184 }
1185 
1186 #endif
1187 
1188 #undef SCTP_CONTROL_VEC_SIZE_RCV
1189