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