xref: /freebsd/lib/libc/net/sctp_sys_calls.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
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 	case SCTP_MAX_CWND:
387 		((struct sctp_assoc_value *)arg)->assoc_id = id;
388 		break;
389 	default:
390 		break;
391 	}
392 	return (getsockopt(sd, IPPROTO_SCTP, opt, arg, size));
393 }
394 
395 int
396 sctp_getpaddrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs)
397 {
398 	struct sctp_getaddresses *addrs;
399 	struct sockaddr *sa;
400 	sctp_assoc_t asoc;
401 	caddr_t lim;
402 	socklen_t opt_len;
403 	int cnt;
404 
405 	if (raddrs == NULL) {
406 		errno = EFAULT;
407 		return (-1);
408 	}
409 	asoc = id;
410 	opt_len = (socklen_t) sizeof(sctp_assoc_t);
411 	if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_REMOTE_ADDR_SIZE,
412 	    &asoc, &opt_len) != 0) {
413 		return (-1);
414 	}
415 	/* size required is returned in 'asoc' */
416 	opt_len = (socklen_t) ((size_t)asoc + sizeof(struct sctp_getaddresses));
417 	addrs = calloc(1, (size_t)opt_len);
418 	if (addrs == NULL) {
419 		errno = ENOMEM;
420 		return (-1);
421 	}
422 	addrs->sget_assoc_id = id;
423 	/* Now lets get the array of addresses */
424 	if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_PEER_ADDRESSES,
425 	    addrs, &opt_len) != 0) {
426 		free(addrs);
427 		return (-1);
428 	}
429 	*raddrs = (struct sockaddr *)&addrs->addr[0];
430 	cnt = 0;
431 	sa = (struct sockaddr *)&addrs->addr[0];
432 	lim = (caddr_t)addrs + opt_len;
433 	while (((caddr_t)sa < lim) && (sa->sa_len > 0)) {
434 		sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
435 		cnt++;
436 	}
437 	return (cnt);
438 }
439 
440 void
441 sctp_freepaddrs(struct sockaddr *addrs)
442 {
443 	void *fr_addr;
444 
445 	/* Take away the hidden association id */
446 	fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t));
447 	/* Now free it */
448 	free(fr_addr);
449 }
450 
451 int
452 sctp_getladdrs(int sd, sctp_assoc_t id, struct sockaddr **raddrs)
453 {
454 	struct sctp_getaddresses *addrs;
455 	caddr_t lim;
456 	struct sockaddr *sa;
457 	size_t size_of_addresses;
458 	socklen_t opt_len;
459 	int cnt;
460 
461 	if (raddrs == NULL) {
462 		errno = EFAULT;
463 		return (-1);
464 	}
465 	size_of_addresses = 0;
466 	opt_len = (socklen_t) sizeof(int);
467 	if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDR_SIZE,
468 	    &size_of_addresses, &opt_len) != 0) {
469 		errno = ENOMEM;
470 		return (-1);
471 	}
472 	if (size_of_addresses == 0) {
473 		errno = ENOTCONN;
474 		return (-1);
475 	}
476 	opt_len = (socklen_t) (size_of_addresses +
477 	    sizeof(struct sockaddr_storage) +
478 	    sizeof(struct sctp_getaddresses));
479 	addrs = calloc(1, (size_t)opt_len);
480 	if (addrs == NULL) {
481 		errno = ENOMEM;
482 		return (-1);
483 	}
484 	addrs->sget_assoc_id = id;
485 	/* Now lets get the array of addresses */
486 	if (getsockopt(sd, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDRESSES, addrs,
487 	    &opt_len) != 0) {
488 		free(addrs);
489 		errno = ENOMEM;
490 		return (-1);
491 	}
492 	*raddrs = (struct sockaddr *)&addrs->addr[0];
493 	cnt = 0;
494 	sa = (struct sockaddr *)&addrs->addr[0];
495 	lim = (caddr_t)addrs + opt_len;
496 	while (((caddr_t)sa < lim) && (sa->sa_len > 0)) {
497 		sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
498 		cnt++;
499 	}
500 	return (cnt);
501 }
502 
503 void
504 sctp_freeladdrs(struct sockaddr *addrs)
505 {
506 	void *fr_addr;
507 
508 	/* Take away the hidden association id */
509 	fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t));
510 	/* Now free it */
511 	free(fr_addr);
512 }
513 
514 ssize_t
515 sctp_sendmsg(int s,
516     const void *data,
517     size_t len,
518     const struct sockaddr *to,
519     socklen_t tolen,
520     uint32_t ppid,
521     uint32_t flags,
522     uint16_t stream_no,
523     uint32_t timetolive,
524     uint32_t context)
525 {
526 #ifdef SYS_sctp_generic_sendmsg
527 	struct sctp_sndrcvinfo sinfo;
528 
529 	memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
530 	sinfo.sinfo_ppid = ppid;
531 	sinfo.sinfo_flags = flags;
532 	sinfo.sinfo_stream = stream_no;
533 	sinfo.sinfo_timetolive = timetolive;
534 	sinfo.sinfo_context = context;
535 	sinfo.sinfo_assoc_id = 0;
536 	return (syscall(SYS_sctp_generic_sendmsg, s,
537 	    data, len, to, tolen, &sinfo, 0));
538 #else
539 	struct msghdr msg;
540 	struct sctp_sndrcvinfo *sinfo;
541 	struct iovec iov;
542 	char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
543 	struct cmsghdr *cmsg;
544 	struct sockaddr *who = NULL;
545 	union {
546 		struct sockaddr_in in;
547 		struct sockaddr_in6 in6;
548 	}     addr;
549 
550 	if ((tolen > 0) &&
551 	    ((to == NULL) || (tolen < sizeof(struct sockaddr)))) {
552 		errno = EINVAL;
553 		return (-1);
554 	}
555 	if ((to != NULL) && (tolen > 0)) {
556 		switch (to->sa_family) {
557 		case AF_INET:
558 			if (tolen != sizeof(struct sockaddr_in)) {
559 				errno = EINVAL;
560 				return (-1);
561 			}
562 			if ((to->sa_len > 0) &&
563 			    (to->sa_len != sizeof(struct sockaddr_in))) {
564 				errno = EINVAL;
565 				return (-1);
566 			}
567 			memcpy(&addr, to, sizeof(struct sockaddr_in));
568 			addr.in.sin_len = sizeof(struct sockaddr_in);
569 			break;
570 		case AF_INET6:
571 			if (tolen != sizeof(struct sockaddr_in6)) {
572 				errno = EINVAL;
573 				return (-1);
574 			}
575 			if ((to->sa_len > 0) &&
576 			    (to->sa_len != sizeof(struct sockaddr_in6))) {
577 				errno = EINVAL;
578 				return (-1);
579 			}
580 			memcpy(&addr, to, sizeof(struct sockaddr_in6));
581 			addr.in6.sin6_len = sizeof(struct sockaddr_in6);
582 			break;
583 		default:
584 			errno = EAFNOSUPPORT;
585 			return (-1);
586 		}
587 		who = (struct sockaddr *)&addr;
588 	}
589 	iov.iov_base = (char *)data;
590 	iov.iov_len = len;
591 
592 	if (who) {
593 		msg.msg_name = (caddr_t)who;
594 		msg.msg_namelen = who->sa_len;
595 	} else {
596 		msg.msg_name = (caddr_t)NULL;
597 		msg.msg_namelen = 0;
598 	}
599 	msg.msg_iov = &iov;
600 	msg.msg_iovlen = 1;
601 	msg.msg_control = cmsgbuf;
602 	msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
603 	msg.msg_flags = 0;
604 	cmsg = (struct cmsghdr *)cmsgbuf;
605 	cmsg->cmsg_level = IPPROTO_SCTP;
606 	cmsg->cmsg_type = SCTP_SNDRCV;
607 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
608 	sinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
609 	memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
610 	sinfo->sinfo_stream = stream_no;
611 	sinfo->sinfo_ssn = 0;
612 	sinfo->sinfo_flags = flags;
613 	sinfo->sinfo_ppid = ppid;
614 	sinfo->sinfo_context = context;
615 	sinfo->sinfo_assoc_id = 0;
616 	sinfo->sinfo_timetolive = timetolive;
617 	return (sendmsg(s, &msg, 0));
618 #endif
619 }
620 
621 
622 sctp_assoc_t
623 sctp_getassocid(int sd, struct sockaddr *sa)
624 {
625 	struct sctp_paddrinfo sp;
626 	socklen_t siz;
627 
628 	/* First get the assoc id */
629 	siz = sizeof(sp);
630 	memset(&sp, 0, sizeof(sp));
631 	memcpy((caddr_t)&sp.spinfo_address, sa, sa->sa_len);
632 	if (getsockopt(sd, IPPROTO_SCTP,
633 	    SCTP_GET_PEER_ADDR_INFO, &sp, &siz) != 0) {
634 		/* We depend on the fact that 0 can never be returned */
635 		return ((sctp_assoc_t) 0);
636 	}
637 	return (sp.spinfo_assoc_id);
638 }
639 
640 ssize_t
641 sctp_send(int sd, const void *data, size_t len,
642     const struct sctp_sndrcvinfo *sinfo,
643     int flags)
644 {
645 
646 #ifdef SYS_sctp_generic_sendmsg
647 	struct sockaddr *to = NULL;
648 
649 	return (syscall(SYS_sctp_generic_sendmsg, sd,
650 	    data, len, to, 0, sinfo, flags));
651 #else
652 	struct msghdr msg;
653 	struct iovec iov;
654 	char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
655 	struct cmsghdr *cmsg;
656 
657 	if (sinfo == NULL) {
658 		errno = EINVAL;
659 		return (-1);
660 	}
661 	iov.iov_base = (char *)data;
662 	iov.iov_len = len;
663 
664 	msg.msg_name = NULL;
665 	msg.msg_namelen = 0;
666 	msg.msg_iov = &iov;
667 	msg.msg_iovlen = 1;
668 	msg.msg_control = cmsgbuf;
669 	msg.msg_controllen = CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
670 	msg.msg_flags = 0;
671 	cmsg = (struct cmsghdr *)cmsgbuf;
672 	cmsg->cmsg_level = IPPROTO_SCTP;
673 	cmsg->cmsg_type = SCTP_SNDRCV;
674 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
675 	memcpy(CMSG_DATA(cmsg), sinfo, sizeof(struct sctp_sndrcvinfo));
676 	return (sendmsg(sd, &msg, flags));
677 #endif
678 }
679 
680 
681 
682 ssize_t
683 sctp_sendx(int sd, const void *msg, size_t msg_len,
684     struct sockaddr *addrs, int addrcnt,
685     struct sctp_sndrcvinfo *sinfo,
686     int flags)
687 {
688 	struct sctp_sndrcvinfo __sinfo;
689 	ssize_t ret;
690 	int i, cnt, *aa, saved_errno;
691 	char *buf;
692 	int no_end_cx = 0;
693 	size_t len, add_len;
694 	struct sockaddr *at;
695 
696 	if (addrs == NULL) {
697 		errno = EINVAL;
698 		return (-1);
699 	}
700 #ifdef SYS_sctp_generic_sendmsg
701 	if (addrcnt == 1) {
702 		socklen_t l;
703 		ssize_t ret;
704 
705 		/*
706 		 * Quick way, we don't need to do a connectx so lets use the
707 		 * syscall directly.
708 		 */
709 		l = addrs->sa_len;
710 		ret = syscall(SYS_sctp_generic_sendmsg, sd,
711 		    msg, msg_len, addrs, l, sinfo, flags);
712 		if ((ret >= 0) && (sinfo != NULL)) {
713 			sinfo->sinfo_assoc_id = sctp_getassocid(sd, addrs);
714 		}
715 		return (ret);
716 	}
717 #endif
718 
719 	len = sizeof(int);
720 	at = addrs;
721 	cnt = 0;
722 	/* validate all the addresses and get the size */
723 	for (i = 0; i < addrcnt; i++) {
724 		if (at->sa_family == AF_INET) {
725 			add_len = sizeof(struct sockaddr_in);
726 		} else if (at->sa_family == AF_INET6) {
727 			add_len = sizeof(struct sockaddr_in6);
728 		} else {
729 			errno = EINVAL;
730 			return (-1);
731 		}
732 		len += add_len;
733 		at = (struct sockaddr *)((caddr_t)at + add_len);
734 		cnt++;
735 	}
736 	/* do we have any? */
737 	if (cnt == 0) {
738 		errno = EINVAL;
739 		return (-1);
740 	}
741 	buf = malloc(len);
742 	if (buf == NULL) {
743 		errno = ENOMEM;
744 		return (-1);
745 	}
746 	aa = (int *)buf;
747 	*aa = cnt;
748 	aa++;
749 	memcpy((caddr_t)aa, addrs, (size_t)(len - sizeof(int)));
750 	ret = setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_DELAYED, (void *)buf,
751 	    (socklen_t) len);
752 
753 	free(buf);
754 	if (ret != 0) {
755 		if (errno == EALREADY) {
756 			no_end_cx = 1;
757 			goto continue_send;
758 		}
759 		return (ret);
760 	}
761 continue_send:
762 	if (sinfo == NULL) {
763 		sinfo = &__sinfo;
764 		memset(&__sinfo, 0, sizeof(__sinfo));
765 	}
766 	sinfo->sinfo_assoc_id = sctp_getassocid(sd, addrs);
767 	if (sinfo->sinfo_assoc_id == 0) {
768 		(void)setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_COMPLETE, (void *)addrs,
769 		    (socklen_t) addrs->sa_len);
770 		errno = ENOENT;
771 		return (-1);
772 	}
773 	ret = sctp_send(sd, msg, msg_len, sinfo, flags);
774 	saved_errno = errno;
775 	if (no_end_cx == 0)
776 		(void)setsockopt(sd, IPPROTO_SCTP, SCTP_CONNECT_X_COMPLETE, (void *)addrs,
777 		    (socklen_t) addrs->sa_len);
778 
779 	errno = saved_errno;
780 	return (ret);
781 }
782 
783 ssize_t
784 sctp_sendmsgx(int sd,
785     const void *msg,
786     size_t len,
787     struct sockaddr *addrs,
788     int addrcnt,
789     uint32_t ppid,
790     uint32_t flags,
791     uint16_t stream_no,
792     uint32_t timetolive,
793     uint32_t context)
794 {
795 	struct sctp_sndrcvinfo sinfo;
796 
797 	memset((void *)&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
798 	sinfo.sinfo_ppid = ppid;
799 	sinfo.sinfo_flags = flags;
800 	sinfo.sinfo_ssn = stream_no;
801 	sinfo.sinfo_timetolive = timetolive;
802 	sinfo.sinfo_context = context;
803 	return (sctp_sendx(sd, msg, len, addrs, addrcnt, &sinfo, 0));
804 }
805 
806 ssize_t
807 sctp_recvmsg(int s,
808     void *dbuf,
809     size_t len,
810     struct sockaddr *from,
811     socklen_t * fromlen,
812     struct sctp_sndrcvinfo *sinfo,
813     int *msg_flags)
814 {
815 #ifdef SYS_sctp_generic_recvmsg
816 	struct iovec iov;
817 
818 	iov.iov_base = dbuf;
819 	iov.iov_len = len;
820 	return (syscall(SYS_sctp_generic_recvmsg, s,
821 	    &iov, 1, from, fromlen, sinfo, msg_flags));
822 #else
823 	ssize_t sz;
824 	struct msghdr msg;
825 	struct iovec iov;
826 	char cmsgbuf[SCTP_CONTROL_VEC_SIZE_RCV];
827 	struct cmsghdr *cmsg;
828 
829 	if (msg_flags == NULL) {
830 		errno = EINVAL;
831 		return (-1);
832 	}
833 	iov.iov_base = dbuf;
834 	iov.iov_len = len;
835 	msg.msg_name = (caddr_t)from;
836 	if (fromlen == NULL)
837 		msg.msg_namelen = 0;
838 	else
839 		msg.msg_namelen = *fromlen;
840 	msg.msg_iov = &iov;
841 	msg.msg_iovlen = 1;
842 	msg.msg_control = cmsgbuf;
843 	msg.msg_controllen = sizeof(cmsgbuf);
844 	msg.msg_flags = 0;
845 	sz = recvmsg(s, &msg, *msg_flags);
846 	*msg_flags = msg.msg_flags;
847 	if (sz <= 0) {
848 		return (sz);
849 	}
850 	if (sinfo) {
851 		sinfo->sinfo_assoc_id = 0;
852 	}
853 	if ((msg.msg_controllen > 0) && (sinfo != NULL)) {
854 		/*
855 		 * parse through and see if we find the sctp_sndrcvinfo (if
856 		 * the user wants it).
857 		 */
858 		for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
859 			if (cmsg->cmsg_level != IPPROTO_SCTP) {
860 				continue;
861 			}
862 			if (cmsg->cmsg_type == SCTP_SNDRCV) {
863 				memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_sndrcvinfo));
864 				break;
865 			}
866 			if (cmsg->cmsg_type == SCTP_EXTRCV) {
867 				/*
868 				 * Let's hope that the user provided enough
869 				 * enough memory. At least he asked for more
870 				 * information.
871 				 */
872 				memcpy(sinfo, CMSG_DATA(cmsg), sizeof(struct sctp_extrcvinfo));
873 				break;
874 			}
875 		}
876 	}
877 	return (sz);
878 #endif
879 }
880 
881 ssize_t
882 sctp_recvv(int sd,
883     const struct iovec *iov,
884     int iovlen,
885     struct sockaddr *from,
886     socklen_t * fromlen,
887     void *info,
888     socklen_t * infolen,
889     unsigned int *infotype,
890     int *flags)
891 {
892 	char cmsgbuf[SCTP_CONTROL_VEC_SIZE_RCV];
893 	struct msghdr msg;
894 	struct cmsghdr *cmsg;
895 	ssize_t ret;
896 	struct sctp_rcvinfo *rcvinfo;
897 	struct sctp_nxtinfo *nxtinfo;
898 
899 	if (((info != NULL) && (infolen == NULL)) ||
900 	    ((info == NULL) && (infolen != NULL) && (*infolen != 0)) ||
901 	    ((info != NULL) && (infotype == NULL))) {
902 		errno = EINVAL;
903 		return (-1);
904 	}
905 	if (infotype) {
906 		*infotype = SCTP_RECVV_NOINFO;
907 	}
908 	msg.msg_name = from;
909 	if (fromlen == NULL) {
910 		msg.msg_namelen = 0;
911 	} else {
912 		msg.msg_namelen = *fromlen;
913 	}
914 	msg.msg_iov = (struct iovec *)iov;
915 	msg.msg_iovlen = iovlen;
916 	msg.msg_control = cmsgbuf;
917 	msg.msg_controllen = sizeof(cmsgbuf);
918 	msg.msg_flags = 0;
919 	ret = recvmsg(sd, &msg, *flags);
920 	*flags = msg.msg_flags;
921 	if ((ret > 0) &&
922 	    (msg.msg_controllen > 0) &&
923 	    (infotype != NULL) &&
924 	    (infolen != NULL) &&
925 	    (*infolen > 0)) {
926 		rcvinfo = NULL;
927 		nxtinfo = NULL;
928 		for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
929 			if (cmsg->cmsg_level != IPPROTO_SCTP) {
930 				continue;
931 			}
932 			if (cmsg->cmsg_type == SCTP_RCVINFO) {
933 				rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
934 				if (nxtinfo != NULL) {
935 					break;
936 				} else {
937 					continue;
938 				}
939 			}
940 			if (cmsg->cmsg_type == SCTP_NXTINFO) {
941 				nxtinfo = (struct sctp_nxtinfo *)CMSG_DATA(cmsg);
942 				if (rcvinfo != NULL) {
943 					break;
944 				} else {
945 					continue;
946 				}
947 			}
948 		}
949 		if (rcvinfo != NULL) {
950 			if ((nxtinfo != NULL) && (*infolen >= sizeof(struct sctp_recvv_rn))) {
951 				struct sctp_recvv_rn *rn_info;
952 
953 				rn_info = (struct sctp_recvv_rn *)info;
954 				rn_info->recvv_rcvinfo = *rcvinfo;
955 				rn_info->recvv_nxtinfo = *nxtinfo;
956 				*infolen = (socklen_t) sizeof(struct sctp_recvv_rn);
957 				*infotype = SCTP_RECVV_RN;
958 			} else if (*infolen >= sizeof(struct sctp_rcvinfo)) {
959 				memcpy(info, rcvinfo, sizeof(struct sctp_rcvinfo));
960 				*infolen = (socklen_t) sizeof(struct sctp_rcvinfo);
961 				*infotype = SCTP_RECVV_RCVINFO;
962 			}
963 		} else if (nxtinfo != NULL) {
964 			if (*infolen >= sizeof(struct sctp_nxtinfo)) {
965 				memcpy(info, nxtinfo, sizeof(struct sctp_nxtinfo));
966 				*infolen = (socklen_t) sizeof(struct sctp_nxtinfo);
967 				*infotype = SCTP_RECVV_NXTINFO;
968 			}
969 		}
970 	}
971 	return (ret);
972 }
973 
974 ssize_t
975 sctp_sendv(int sd,
976     const struct iovec *iov, int iovcnt,
977     struct sockaddr *addrs, int addrcnt,
978     void *info, socklen_t infolen, unsigned int infotype,
979     int flags)
980 {
981 	ssize_t ret;
982 	int i;
983 	socklen_t addr_len;
984 	struct msghdr msg;
985 	in_port_t port;
986 	struct sctp_sendv_spa *spa_info;
987 	struct cmsghdr *cmsg;
988 	char *cmsgbuf;
989 	struct sockaddr *addr;
990 	struct sockaddr_in *addr_in;
991 	struct sockaddr_in6 *addr_in6;
992 	sctp_assoc_t *assoc_id;
993 
994 	if ((addrcnt < 0) ||
995 	    (iovcnt < 0) ||
996 	    ((addrs == NULL) && (addrcnt > 0)) ||
997 	    ((addrs != NULL) && (addrcnt == 0)) ||
998 	    ((iov == NULL) && (iovcnt > 0)) ||
999 	    ((iov != NULL) && (iovcnt == 0))) {
1000 		errno = EINVAL;
1001 		return (-1);
1002 	}
1003 	cmsgbuf = malloc(CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1004 	    CMSG_SPACE(sizeof(struct sctp_prinfo)) +
1005 	    CMSG_SPACE(sizeof(struct sctp_authinfo)) +
1006 	    (size_t)addrcnt * CMSG_SPACE(sizeof(struct in6_addr)));
1007 	if (cmsgbuf == NULL) {
1008 		errno = ENOMEM;
1009 		return (-1);
1010 	}
1011 	assoc_id = NULL;
1012 	msg.msg_control = cmsgbuf;
1013 	msg.msg_controllen = 0;
1014 	cmsg = (struct cmsghdr *)cmsgbuf;
1015 	switch (infotype) {
1016 	case SCTP_SENDV_NOINFO:
1017 		if ((infolen != 0) || (info != NULL)) {
1018 			free(cmsgbuf);
1019 			errno = EINVAL;
1020 			return (-1);
1021 		}
1022 		break;
1023 	case SCTP_SENDV_SNDINFO:
1024 		if ((info == NULL) || (infolen < sizeof(struct sctp_sndinfo))) {
1025 			free(cmsgbuf);
1026 			errno = EINVAL;
1027 			return (-1);
1028 		}
1029 		cmsg->cmsg_level = IPPROTO_SCTP;
1030 		cmsg->cmsg_type = SCTP_SNDINFO;
1031 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1032 		memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_sndinfo));
1033 		msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1034 		cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo)));
1035 		assoc_id = &(((struct sctp_sndinfo *)info)->snd_assoc_id);
1036 		break;
1037 	case SCTP_SENDV_PRINFO:
1038 		if ((info == NULL) || (infolen < sizeof(struct sctp_prinfo))) {
1039 			free(cmsgbuf);
1040 			errno = EINVAL;
1041 			return (-1);
1042 		}
1043 		cmsg->cmsg_level = IPPROTO_SCTP;
1044 		cmsg->cmsg_type = SCTP_PRINFO;
1045 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1046 		memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_prinfo));
1047 		msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1048 		cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo)));
1049 		break;
1050 	case SCTP_SENDV_AUTHINFO:
1051 		if ((info == NULL) || (infolen < sizeof(struct sctp_authinfo))) {
1052 			free(cmsgbuf);
1053 			errno = EINVAL;
1054 			return (-1);
1055 		}
1056 		cmsg->cmsg_level = IPPROTO_SCTP;
1057 		cmsg->cmsg_type = SCTP_AUTHINFO;
1058 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo));
1059 		memcpy(CMSG_DATA(cmsg), info, sizeof(struct sctp_authinfo));
1060 		msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo));
1061 		cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo)));
1062 		break;
1063 	case SCTP_SENDV_SPA:
1064 		if ((info == NULL) || (infolen < sizeof(struct sctp_sendv_spa))) {
1065 			free(cmsgbuf);
1066 			errno = EINVAL;
1067 			return (-1);
1068 		}
1069 		spa_info = (struct sctp_sendv_spa *)info;
1070 		if (spa_info->sendv_flags & SCTP_SEND_SNDINFO_VALID) {
1071 			cmsg->cmsg_level = IPPROTO_SCTP;
1072 			cmsg->cmsg_type = SCTP_SNDINFO;
1073 			cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1074 			memcpy(CMSG_DATA(cmsg), &spa_info->sendv_sndinfo, sizeof(struct sctp_sndinfo));
1075 			msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1076 			cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_sndinfo)));
1077 			assoc_id = &(spa_info->sendv_sndinfo.snd_assoc_id);
1078 		}
1079 		if (spa_info->sendv_flags & SCTP_SEND_PRINFO_VALID) {
1080 			cmsg->cmsg_level = IPPROTO_SCTP;
1081 			cmsg->cmsg_type = SCTP_PRINFO;
1082 			cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1083 			memcpy(CMSG_DATA(cmsg), &spa_info->sendv_prinfo, sizeof(struct sctp_prinfo));
1084 			msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1085 			cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_prinfo)));
1086 		}
1087 		if (spa_info->sendv_flags & SCTP_SEND_AUTHINFO_VALID) {
1088 			cmsg->cmsg_level = IPPROTO_SCTP;
1089 			cmsg->cmsg_type = SCTP_AUTHINFO;
1090 			cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_authinfo));
1091 			memcpy(CMSG_DATA(cmsg), &spa_info->sendv_authinfo, sizeof(struct sctp_authinfo));
1092 			msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_authinfo));
1093 			cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct sctp_authinfo)));
1094 		}
1095 		break;
1096 	default:
1097 		free(cmsgbuf);
1098 		errno = EINVAL;
1099 		return (-1);
1100 	}
1101 	addr = addrs;
1102 	msg.msg_name = NULL;
1103 	msg.msg_namelen = 0;
1104 
1105 	for (i = 0; i < addrcnt; i++) {
1106 		switch (addr->sa_family) {
1107 		case AF_INET:
1108 			addr_len = (socklen_t) sizeof(struct sockaddr_in);
1109 			addr_in = (struct sockaddr_in *)addr;
1110 			if (addr_in->sin_len != addr_len) {
1111 				free(cmsgbuf);
1112 				errno = EINVAL;
1113 				return (-1);
1114 			}
1115 			if (i == 0) {
1116 				port = addr_in->sin_port;
1117 			} else {
1118 				if (port == addr_in->sin_port) {
1119 					cmsg->cmsg_level = IPPROTO_SCTP;
1120 					cmsg->cmsg_type = SCTP_DSTADDRV4;
1121 					cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
1122 					memcpy(CMSG_DATA(cmsg), &addr_in->sin_addr, sizeof(struct in_addr));
1123 					msg.msg_controllen += CMSG_SPACE(sizeof(struct in_addr));
1124 					cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in_addr)));
1125 				} else {
1126 					free(cmsgbuf);
1127 					errno = EINVAL;
1128 					return (-1);
1129 				}
1130 			}
1131 			break;
1132 		case AF_INET6:
1133 			addr_len = (socklen_t) sizeof(struct sockaddr_in6);
1134 			addr_in6 = (struct sockaddr_in6 *)addr;
1135 			if (addr_in6->sin6_len != addr_len) {
1136 				free(cmsgbuf);
1137 				errno = EINVAL;
1138 				return (-1);
1139 			}
1140 			if (i == 0) {
1141 				port = addr_in6->sin6_port;
1142 			} else {
1143 				if (port == addr_in6->sin6_port) {
1144 					cmsg->cmsg_level = IPPROTO_SCTP;
1145 					cmsg->cmsg_type = SCTP_DSTADDRV6;
1146 					cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_addr));
1147 					memcpy(CMSG_DATA(cmsg), &addr_in6->sin6_addr, sizeof(struct in6_addr));
1148 					msg.msg_controllen += CMSG_SPACE(sizeof(struct in6_addr));
1149 					cmsg = (struct cmsghdr *)((caddr_t)cmsg + CMSG_SPACE(sizeof(struct in6_addr)));
1150 				} else {
1151 					free(cmsgbuf);
1152 					errno = EINVAL;
1153 					return (-1);
1154 				}
1155 			}
1156 			break;
1157 		default:
1158 			free(cmsgbuf);
1159 			errno = EINVAL;
1160 			return (-1);
1161 		}
1162 		if (i == 0) {
1163 			msg.msg_name = addr;
1164 			msg.msg_namelen = addr_len;
1165 		}
1166 		addr = (struct sockaddr *)((caddr_t)addr + addr_len);
1167 	}
1168 	if (msg.msg_controllen == 0) {
1169 		msg.msg_control = NULL;
1170 	}
1171 	msg.msg_iov = (struct iovec *)iov;
1172 	msg.msg_iovlen = iovcnt;
1173 	msg.msg_flags = 0;
1174 	ret = sendmsg(sd, &msg, flags);
1175 	free(cmsgbuf);
1176 	if ((ret >= 0) && (addrs != NULL) && (assoc_id != NULL)) {
1177 		*assoc_id = sctp_getassocid(sd, addrs);
1178 	}
1179 	return (ret);
1180 }
1181 
1182 
1183 #if !defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT)
1184 
1185 int
1186 sctp_peeloff(int sd, sctp_assoc_t assoc_id)
1187 {
1188 	/* NOT supported, return invalid sd */
1189 	errno = ENOTSUP;
1190 	return (-1);
1191 }
1192 
1193 #endif
1194 #if defined(SYS_sctp_peeloff) && !defined(HAVE_SCTP_PEELOFF_SOCKOPT)
1195 int
1196 sctp_peeloff(int sd, sctp_assoc_t assoc_id)
1197 {
1198 	return (syscall(SYS_sctp_peeloff, sd, assoc_id));
1199 }
1200 
1201 #endif
1202 
1203 #undef SCTP_CONTROL_VEC_SIZE_RCV
1204