1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/stream.h>
31 #define	_SUN_TPI_VERSION 2
32 #include <sys/tihdr.h>
33 #include <sys/socket.h>
34 #include <sys/xti_inet.h>
35 #include <sys/systm.h>
36 #include <sys/ddi.h>
37 #include <sys/sunddi.h>
38 #include <sys/kmem.h>
39 #include <sys/strsubr.h>
40 #include <sys/strsun.h>
41 #include <sys/policy.h>
42 
43 #include <inet/common.h>
44 #include <netinet/ip6.h>
45 #include <inet/ip.h>
46 #include <inet/ip_ire.h>
47 #include <inet/ip_if.h>
48 #include <inet/ipclassifier.h>
49 #include <inet/ipsec_impl.h>
50 
51 #include <netinet/in.h>
52 #include <netinet/ip.h>
53 #include <netinet/tcp.h>
54 
55 #include <inet/common.h>
56 #include <inet/ip.h>
57 #include <inet/ip6.h>
58 #include <inet/sctp_itf.h>
59 #include "sctp_impl.h"
60 #include "sctp_asconf.h"
61 #include "sctp_addr.h"
62 
63 static int	sctp_getpeeraddrs(sctp_t *, void *, int *);
64 
65 /*
66  * Copy the standard header into its new location,
67  * lay in the new options and then update the relevant
68  * fields in both sctp_t and the standard header.
69  * Returns 0 on success, errno otherwise.
70  */
71 static int
72 sctp_opt_set_header(sctp_t *sctp, const void *ptr, uint_t len)
73 {
74 	uint8_t *ip_optp;
75 	sctp_hdr_t *new_sctph;
76 
77 	if ((len > SCTP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3))
78 		return (EINVAL);
79 
80 	if (len > IP_MAX_OPT_LENGTH - sctp->sctp_v4label_len)
81 		return (EINVAL);
82 
83 	ip_optp = (uint8_t *)sctp->sctp_ipha + IP_SIMPLE_HDR_LENGTH;
84 
85 	if (sctp->sctp_v4label_len > 0) {
86 		int padlen;
87 		uint8_t opt;
88 
89 		/* convert list termination to no-ops as needed */
90 		padlen = sctp->sctp_v4label_len - ip_optp[IPOPT_OLEN];
91 		ip_optp += ip_optp[IPOPT_OLEN];
92 		opt = len > 0 ? IPOPT_NOP : IPOPT_EOL;
93 		while (--padlen >= 0)
94 			*ip_optp++ = opt;
95 		ASSERT(ip_optp == (uint8_t *)sctp->sctp_ipha +
96 		    IP_SIMPLE_HDR_LENGTH + sctp->sctp_v4label_len);
97 	}
98 
99 	/*
100 	 * Move the existing SCTP header out where it belongs.
101 	 */
102 	new_sctph = (sctp_hdr_t *)(ip_optp + len);
103 	ovbcopy(sctp->sctp_sctph, new_sctph, sizeof (sctp_hdr_t));
104 	sctp->sctp_sctph = new_sctph;
105 
106 	/*
107 	 * Insert the new user-supplied IP options.
108 	 */
109 	if (len > 0)
110 		bcopy(ptr, ip_optp, len);
111 
112 	len += sctp->sctp_v4label_len;
113 	sctp->sctp_ip_hdr_len = len;
114 	sctp->sctp_ipha->ipha_version_and_hdr_length =
115 	    (IP_VERSION << 4) | (len >> 2);
116 	sctp->sctp_hdr_len = len + sizeof (sctp_hdr_t);
117 
118 	if (sctp->sctp_current) {
119 		/*
120 		 * Could be setting options before setting up connection.
121 		 */
122 		sctp_set_ulp_prop(sctp);
123 	}
124 	return (0);
125 }
126 
127 static int
128 sctp_get_status(sctp_t *sctp, void *ptr)
129 {
130 	struct sctp_status *sstat = ptr;
131 	sctp_faddr_t *fp;
132 	struct sockaddr_in *sin;
133 	struct sockaddr_in6 *sin6;
134 	struct sctp_paddrinfo *sp;
135 	mblk_t *meta, *mp;
136 	int i;
137 
138 	sstat->sstat_state = sctp->sctp_state;
139 	sstat->sstat_rwnd = sctp->sctp_frwnd;
140 
141 	sp = &sstat->sstat_primary;
142 	if (!sctp->sctp_primary) {
143 		bzero(sp, sizeof (*sp));
144 		goto noprim;
145 	}
146 	fp = sctp->sctp_primary;
147 
148 	if (fp->isv4) {
149 		sin = (struct sockaddr_in *)&sp->spinfo_address;
150 		sin->sin_family = AF_INET;
151 		sin->sin_port = sctp->sctp_fport;
152 		IN6_V4MAPPED_TO_INADDR(&fp->faddr, &sin->sin_addr);
153 		sp->spinfo_mtu = sctp->sctp_hdr_len;
154 	} else {
155 		sin6 = (struct sockaddr_in6 *)&sp->spinfo_address;
156 		sin6->sin6_family = AF_INET6;
157 		sin6->sin6_port = sctp->sctp_fport;
158 		sin6->sin6_addr = fp->faddr;
159 		sp->spinfo_mtu = sctp->sctp_hdr6_len;
160 	}
161 	sp->spinfo_state = fp->state == SCTP_FADDRS_ALIVE ? SCTP_ACTIVE :
162 	    SCTP_INACTIVE;
163 	sp->spinfo_cwnd = fp->cwnd;
164 	sp->spinfo_srtt = fp->srtt;
165 	sp->spinfo_rto = fp->rto;
166 	sp->spinfo_mtu += fp->sfa_pmss;
167 
168 noprim:
169 	sstat->sstat_unackdata = 0;
170 	sstat->sstat_penddata = 0;
171 	sstat->sstat_instrms = sctp->sctp_num_istr;
172 	sstat->sstat_outstrms = sctp->sctp_num_ostr;
173 	sstat->sstat_fragmentation_point = sctp->sctp_mss -
174 	    sizeof (sctp_data_hdr_t);
175 
176 	/* count unack'd */
177 	for (meta = sctp->sctp_xmit_head; meta; meta = meta->b_next) {
178 		for (mp = meta->b_cont; mp; mp = mp->b_next) {
179 			if (!SCTP_CHUNK_ISSENT(mp)) {
180 				break;
181 			}
182 			if (!SCTP_CHUNK_ISACKED(mp)) {
183 				sstat->sstat_unackdata++;
184 			}
185 		}
186 	}
187 
188 	/*
189 	 * Count penddata chunks. We can only count chunks in SCTP (not
190 	 * data already delivered to socket layer).
191 	 */
192 	if (sctp->sctp_instr != NULL) {
193 		for (i = 0; i < sctp->sctp_num_istr; i++) {
194 			for (meta = sctp->sctp_instr[i].istr_reass;
195 			    meta != NULL; meta = meta->b_next) {
196 				for (mp = meta->b_cont; mp; mp = mp->b_cont) {
197 					if (DB_TYPE(mp) != M_CTL) {
198 						sstat->sstat_penddata++;
199 					}
200 				}
201 			}
202 		}
203 	}
204 	/* Un-Ordered Frag list */
205 	for (meta = sctp->sctp_uo_frags; meta != NULL; meta = meta->b_next)
206 		sstat->sstat_penddata++;
207 
208 	return (sizeof (*sstat));
209 }
210 
211 /*
212  * SCTP_GET_PEER_ADDR_INFO
213  */
214 static int
215 sctp_get_paddrinfo(sctp_t *sctp, void *ptr, socklen_t *optlen)
216 {
217 	struct sctp_paddrinfo	*infop = ptr;
218 	struct sockaddr_in	*sin4;
219 	struct sockaddr_in6	*sin6;
220 	in6_addr_t		faddr;
221 	sctp_faddr_t		*fp;
222 
223 	switch (infop->spinfo_address.ss_family) {
224 	case AF_INET:
225 		sin4 = (struct sockaddr_in *)&infop->spinfo_address;
226 		IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &faddr);
227 		break;
228 	case AF_INET6:
229 		sin6 = (struct sockaddr_in6 *)&infop->spinfo_address;
230 		faddr = sin6->sin6_addr;
231 		break;
232 	default:
233 		return (EAFNOSUPPORT);
234 	}
235 
236 	if ((fp = sctp_lookup_faddr(sctp, &faddr)) == NULL)
237 		return (EINVAL);
238 
239 	infop->spinfo_state = (fp->state == SCTP_FADDRS_ALIVE) ? SCTP_ACTIVE :
240 	    SCTP_INACTIVE;
241 	infop->spinfo_cwnd = fp->cwnd;
242 	infop->spinfo_srtt = TICK_TO_MSEC(fp->srtt);
243 	infop->spinfo_rto = TICK_TO_MSEC(fp->rto);
244 	infop->spinfo_mtu = fp->sfa_pmss;
245 
246 	*optlen = sizeof (struct sctp_paddrinfo);
247 	return (0);
248 }
249 
250 /*
251  * SCTP_RTOINFO
252  */
253 static int
254 sctp_get_rtoinfo(sctp_t *sctp, void *ptr)
255 {
256 	struct sctp_rtoinfo *srto = ptr;
257 
258 	srto->srto_initial = TICK_TO_MSEC(sctp->sctp_rto_initial);
259 	srto->srto_max = TICK_TO_MSEC(sctp->sctp_rto_max);
260 	srto->srto_min = TICK_TO_MSEC(sctp->sctp_rto_min);
261 
262 	return (sizeof (*srto));
263 }
264 
265 static int
266 sctp_set_rtoinfo(sctp_t *sctp, const void *invalp, uint_t inlen)
267 {
268 	const struct sctp_rtoinfo *srto;
269 	boolean_t ispriv;
270 	sctp_stack_t	*sctps = sctp->sctp_sctps;
271 
272 	if (inlen < sizeof (*srto)) {
273 		return (EINVAL);
274 	}
275 	srto = invalp;
276 
277 	ispriv = secpolicy_ip_config(sctp->sctp_credp, B_TRUE) == 0;
278 
279 	/*
280 	 * Bounds checking.  Priviledged user can set the RTO initial
281 	 * outside the ndd boundary.
282 	 */
283 	if (srto->srto_initial != 0 &&
284 	    (!ispriv && (srto->srto_initial < sctps->sctps_rto_initialg_low ||
285 		srto->srto_initial > sctps->sctps_rto_initialg_high))) {
286 		return (EINVAL);
287 	}
288 	if (srto->srto_max != 0 &&
289 	    (!ispriv && (srto->srto_max < sctps->sctps_rto_maxg_low ||
290 		srto->srto_max > sctps->sctps_rto_maxg_high))) {
291 		return (EINVAL);
292 	}
293 	if (srto->srto_min != 0 &&
294 	    (!ispriv && (srto->srto_min < sctps->sctps_rto_ming_low ||
295 		srto->srto_min > sctps->sctps_rto_ming_high))) {
296 		return (EINVAL);
297 	}
298 
299 	if (srto->srto_initial != 0) {
300 		sctp->sctp_rto_initial = MSEC_TO_TICK(srto->srto_initial);
301 	}
302 	if (srto->srto_max != 0) {
303 		sctp->sctp_rto_max = MSEC_TO_TICK(srto->srto_max);
304 	}
305 	if (srto->srto_min != 0) {
306 		sctp->sctp_rto_min = MSEC_TO_TICK(srto->srto_min);
307 	}
308 
309 	return (0);
310 }
311 
312 /*
313  * SCTP_ASSOCINFO
314  */
315 static int
316 sctp_get_assocparams(sctp_t *sctp, void *ptr)
317 {
318 	struct sctp_assocparams *sap = ptr;
319 	sctp_faddr_t *fp;
320 	uint16_t i;
321 
322 	sap->sasoc_asocmaxrxt = sctp->sctp_pa_max_rxt;
323 
324 	/*
325 	 * Count the number of peer addresses
326 	 */
327 	for (i = 0, fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
328 		i++;
329 	}
330 	sap->sasoc_number_peer_destinations = i;
331 	sap->sasoc_peer_rwnd = sctp->sctp_frwnd;
332 	sap->sasoc_local_rwnd = sctp->sctp_rwnd;
333 	sap->sasoc_cookie_life = TICK_TO_MSEC(sctp->sctp_cookie_lifetime);
334 
335 	return (sizeof (*sap));
336 }
337 
338 static int
339 sctp_set_assocparams(sctp_t *sctp, const void *invalp, uint_t inlen)
340 {
341 	const struct sctp_assocparams *sap = invalp;
342 	uint32_t sum = 0;
343 	sctp_faddr_t *fp;
344 	sctp_stack_t	*sctps = sctp->sctp_sctps;
345 
346 	if (inlen < sizeof (*sap)) {
347 		return (EINVAL);
348 	}
349 
350 	if (sap->sasoc_asocmaxrxt) {
351 		if (sctp->sctp_faddrs) {
352 			/*
353 			 * Bounds check: as per rfc2960, assoc max retr cannot
354 			 * exceed the sum of all individual path max retr's.
355 			 */
356 			for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
357 				sum += fp->max_retr;
358 			}
359 			if (sap->sasoc_asocmaxrxt > sum) {
360 				return (EINVAL);
361 			}
362 		}
363 		if (sap->sasoc_asocmaxrxt < sctps->sctps_pa_max_retr_low ||
364 		    sap->sasoc_asocmaxrxt > sctps->sctps_pa_max_retr_high) {
365 			/*
366 			 * Out of bounds.
367 			 */
368 			return (EINVAL);
369 		}
370 	}
371 	if (sap->sasoc_cookie_life != 0 &&
372 	    (sap->sasoc_cookie_life < sctps->sctps_cookie_life_low ||
373 		sap->sasoc_cookie_life > sctps->sctps_cookie_life_high)) {
374 			return (EINVAL);
375 	}
376 
377 	if (sap->sasoc_asocmaxrxt > 0) {
378 		sctp->sctp_pa_max_rxt = sap->sasoc_asocmaxrxt;
379 	}
380 	if (sap->sasoc_cookie_life > 0) {
381 		sctp->sctp_cookie_lifetime = MSEC_TO_TICK(
382 		    sap->sasoc_cookie_life);
383 	}
384 	return (0);
385 }
386 
387 /*
388  * SCTP_INITMSG
389  */
390 static int
391 sctp_get_initmsg(sctp_t *sctp, void *ptr)
392 {
393 	struct sctp_initmsg *si = ptr;
394 
395 	si->sinit_num_ostreams = sctp->sctp_num_ostr;
396 	si->sinit_max_instreams = sctp->sctp_num_istr;
397 	si->sinit_max_attempts = sctp->sctp_max_init_rxt;
398 	si->sinit_max_init_timeo = TICK_TO_MSEC(sctp->sctp_init_rto_max);
399 
400 	return (sizeof (*si));
401 }
402 
403 static int
404 sctp_set_initmsg(sctp_t *sctp, const void *invalp, uint_t inlen)
405 {
406 	const struct sctp_initmsg *si = invalp;
407 	sctp_stack_t	*sctps = sctp->sctp_sctps;
408 
409 	if (sctp->sctp_state > SCTPS_LISTEN) {
410 		return (EINVAL);
411 	}
412 	if (inlen < sizeof (*si)) {
413 		return (EINVAL);
414 	}
415 	if (si->sinit_num_ostreams != 0 &&
416 	    (si->sinit_num_ostreams < sctps->sctps_initial_out_streams_low ||
417 	    si->sinit_num_ostreams >
418 	    sctps->sctps_initial_out_streams_high)) {
419 		/*
420 		 * Out of bounds.
421 		 */
422 		return (EINVAL);
423 	}
424 	if (si->sinit_max_instreams != 0 &&
425 	    (si->sinit_max_instreams < sctps->sctps_max_in_streams_low ||
426 		si->sinit_max_instreams > sctps->sctps_max_in_streams_high)) {
427 		return (EINVAL);
428 	}
429 	if (si->sinit_max_attempts != 0 &&
430 	    (si->sinit_max_attempts < sctps->sctps_max_init_retr_low ||
431 		si->sinit_max_attempts > sctps->sctps_max_init_retr_high)) {
432 		return (EINVAL);
433 	}
434 	if (si->sinit_max_init_timeo != 0 &&
435 	    (secpolicy_ip_config(sctp->sctp_credp, B_TRUE) != 0 &&
436 		(si->sinit_max_init_timeo < sctps->sctps_rto_maxg_low ||
437 		si->sinit_max_init_timeo > sctps->sctps_rto_maxg_high))) {
438 		return (EINVAL);
439 	}
440 	if (si->sinit_num_ostreams != 0)
441 		sctp->sctp_num_ostr = si->sinit_num_ostreams;
442 
443 	if (si->sinit_max_instreams != 0)
444 		sctp->sctp_num_istr = si->sinit_max_instreams;
445 
446 	if (si->sinit_max_attempts != 0)
447 		sctp->sctp_max_init_rxt = si->sinit_max_attempts;
448 
449 	if (si->sinit_max_init_timeo != 0) {
450 		sctp->sctp_init_rto_max =
451 		    MSEC_TO_TICK(si->sinit_max_init_timeo);
452 	}
453 	return (0);
454 }
455 
456 /*
457  * SCTP_PEER_ADDR_PARAMS
458  */
459 static int
460 sctp_find_peer_fp(sctp_t *sctp, const struct sockaddr_storage *ss,
461     sctp_faddr_t **fpp)
462 {
463 	struct sockaddr_in *sin;
464 	struct sockaddr_in6 *sin6;
465 	in6_addr_t addr;
466 
467 	if (ss->ss_family == AF_INET) {
468 		sin = (struct sockaddr_in *)ss;
469 		IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &addr);
470 	} else if (ss->ss_family == AF_INET6) {
471 		sin6 = (struct sockaddr_in6 *)ss;
472 		addr = sin6->sin6_addr;
473 	} else if (ss->ss_family) {
474 		return (EAFNOSUPPORT);
475 	}
476 
477 	if (!ss->ss_family ||
478 	    SCTP_IS_ADDR_UNSPEC(IN6_IS_ADDR_V4MAPPED(&addr), addr)) {
479 		*fpp = NULL;
480 	} else {
481 		*fpp = sctp_lookup_faddr(sctp, &addr);
482 		if (*fpp == NULL) {
483 			return (EINVAL);
484 		}
485 	}
486 	return (0);
487 }
488 
489 static int
490 sctp_get_peer_addr_params(sctp_t *sctp, void *ptr)
491 {
492 	struct sctp_paddrparams *spp = ptr;
493 	sctp_faddr_t *fp;
494 	int retval;
495 
496 	retval = sctp_find_peer_fp(sctp, &spp->spp_address, &fp);
497 	if (retval) {
498 		return (retval);
499 	}
500 	if (fp) {
501 		spp->spp_hbinterval = TICK_TO_MSEC(fp->hb_interval);
502 		spp->spp_pathmaxrxt = fp->max_retr;
503 	} else {
504 		spp->spp_hbinterval = TICK_TO_MSEC(sctp->sctp_hb_interval);
505 		spp->spp_pathmaxrxt = sctp->sctp_pp_max_rxt;
506 	}
507 	return (sizeof (*spp));
508 }
509 
510 static int
511 sctp_set_peer_addr_params(sctp_t *sctp, const void *invalp, uint_t inlen)
512 {
513 	const struct sctp_paddrparams *spp = invalp;
514 	sctp_faddr_t *fp, *fp2;
515 	int retval;
516 	uint32_t sum = 0;
517 	int64_t now;
518 	sctp_stack_t	*sctps = sctp->sctp_sctps;
519 
520 	if (inlen < sizeof (*spp)) {
521 		return (EINVAL);
522 	}
523 
524 	retval = sctp_find_peer_fp(sctp, &spp->spp_address, &fp);
525 	if (retval != 0) {
526 		return (retval);
527 	}
528 
529 	if (spp->spp_hbinterval && spp->spp_hbinterval != UINT32_MAX &&
530 	    (spp->spp_hbinterval < sctps->sctps_heartbeat_interval_low ||
531 		spp->spp_hbinterval > sctps->sctps_heartbeat_interval_high)) {
532 		return (EINVAL);
533 	}
534 	if (spp->spp_pathmaxrxt &&
535 	    (spp->spp_pathmaxrxt < sctps->sctps_pp_max_retr_low ||
536 		spp->spp_pathmaxrxt > sctps->sctps_pp_max_retr_high)) {
537 		return (EINVAL);
538 	}
539 	if (spp->spp_pathmaxrxt && sctp->sctp_faddrs) {
540 		for (fp2 = sctp->sctp_faddrs; fp2; fp2 = fp2->next) {
541 			if (!fp || fp2 == fp) {
542 				sum += spp->spp_pathmaxrxt;
543 			} else {
544 				sum += fp2->max_retr;
545 			}
546 		}
547 		if (sctp->sctp_pa_max_rxt > sum) {
548 			return (EINVAL);
549 		}
550 	}
551 
552 	now = lbolt64;
553 	if (fp != NULL) {
554 		if (spp->spp_hbinterval == UINT32_MAX) {
555 			/*
556 			 * Send heartbeat immediatelly, don't modify the
557 			 * current setting.
558 			 */
559 			sctp_send_heartbeat(sctp, fp);
560 		} else {
561 			fp->hb_interval = MSEC_TO_TICK(spp->spp_hbinterval);
562 			fp->hb_expiry = now + SET_HB_INTVL(fp);
563 			/*
564 			 * Restart the heartbeat timer using the new intrvl.
565 			 * We need to call sctp_heartbeat_timer() to set
566 			 * the earliest heartbeat expiry time.
567 			 */
568 			sctp_heartbeat_timer(sctp);
569 		}
570 		if (spp->spp_pathmaxrxt) {
571 			fp->max_retr = spp->spp_pathmaxrxt;
572 		}
573 	} else {
574 		for (fp2 = sctp->sctp_faddrs; fp2 != NULL; fp2 = fp2->next) {
575 			if (spp->spp_hbinterval == UINT32_MAX) {
576 				/*
577 				 * Send heartbeat immediatelly, don't modify
578 				 * the current setting.
579 				 */
580 				sctp_send_heartbeat(sctp, fp2);
581 			} else {
582 				fp2->hb_interval = MSEC_TO_TICK(
583 				    spp->spp_hbinterval);
584 				fp2->hb_expiry = now + SET_HB_INTVL(fp2);
585 			}
586 			if (spp->spp_pathmaxrxt) {
587 				fp2->max_retr = spp->spp_pathmaxrxt;
588 			}
589 		}
590 		if (spp->spp_hbinterval != UINT32_MAX) {
591 			sctp->sctp_hb_interval = MSEC_TO_TICK(
592 			    spp->spp_hbinterval);
593 			/* Restart the heartbeat timer using the new intrvl. */
594 			sctp_timer(sctp, sctp->sctp_heartbeat_mp,
595 			    sctp->sctp_hb_interval);
596 		}
597 		if (spp->spp_pathmaxrxt) {
598 			sctp->sctp_pp_max_rxt = spp->spp_pathmaxrxt;
599 		}
600 	}
601 	return (0);
602 }
603 
604 /*
605  * SCTP_DEFAULT_SEND_PARAM
606  */
607 static int
608 sctp_get_def_send_params(sctp_t *sctp, void *ptr)
609 {
610 	struct sctp_sndrcvinfo *sinfo = ptr;
611 
612 	sinfo->sinfo_stream = sctp->sctp_def_stream;
613 	sinfo->sinfo_ssn = 0;
614 	sinfo->sinfo_flags = sctp->sctp_def_flags;
615 	sinfo->sinfo_ppid = sctp->sctp_def_ppid;
616 	sinfo->sinfo_context = sctp->sctp_def_context;
617 	sinfo->sinfo_timetolive = sctp->sctp_def_timetolive;
618 	sinfo->sinfo_tsn = 0;
619 	sinfo->sinfo_cumtsn = 0;
620 
621 	return (sizeof (*sinfo));
622 }
623 
624 static int
625 sctp_set_def_send_params(sctp_t *sctp, const void *invalp, uint_t inlen)
626 {
627 	const struct sctp_sndrcvinfo *sinfo = invalp;
628 
629 	if (inlen < sizeof (*sinfo)) {
630 		return (EINVAL);
631 	}
632 	if (sinfo->sinfo_stream >= sctp->sctp_num_ostr) {
633 		return (EINVAL);
634 	}
635 
636 	sctp->sctp_def_stream = sinfo->sinfo_stream;
637 	sctp->sctp_def_flags = sinfo->sinfo_flags;
638 	sctp->sctp_def_ppid = sinfo->sinfo_ppid;
639 	sctp->sctp_def_context = sinfo->sinfo_context;
640 	sctp->sctp_def_timetolive = sinfo->sinfo_timetolive;
641 
642 	return (0);
643 }
644 
645 static int
646 sctp_set_prim(sctp_t *sctp, const void *invalp, uint_t inlen)
647 {
648 	const struct	sctp_setpeerprim *pp = invalp;
649 	int		retval;
650 	sctp_faddr_t	*fp;
651 
652 	if (inlen < sizeof (*pp)) {
653 		return (EINVAL);
654 	}
655 
656 	retval = sctp_find_peer_fp(sctp, &pp->sspp_addr, &fp);
657 	if (retval)
658 		return (retval);
659 
660 	if (fp == NULL)
661 		return (EINVAL);
662 	if (fp == sctp->sctp_primary)
663 		return (0);
664 	sctp->sctp_primary = fp;
665 
666 	/* Only switch current if fp is alive */
667 	if (fp->state != SCTP_FADDRS_ALIVE || fp == sctp->sctp_current) {
668 		return (0);
669 	}
670 	sctp_set_faddr_current(sctp, fp);
671 
672 	return (0);
673 }
674 
675 /* Handy on off switch for socket option processing. */
676 #define	ONOFF(x)	((x) == 0 ? 0 : 1)
677 
678 /*
679  * SCTP routine to get the values of options.
680  */
681 int
682 sctp_get_opt(sctp_t *sctp, int level, int name, void *ptr, socklen_t *optlen)
683 {
684 	int	*i1 = (int *)ptr;
685 	int	retval = 0;
686 	int	buflen = *optlen;
687 	conn_t		*connp = sctp->sctp_connp;
688 	ip6_pkt_t	*ipp = &sctp->sctp_sticky_ipp;
689 
690 	/* In most cases, the return buffer is just an int */
691 	*optlen = sizeof (int32_t);
692 
693 	RUN_SCTP(sctp);
694 
695 	switch (level) {
696 	case SOL_SOCKET:
697 		switch (name) {
698 		case SO_LINGER:	{
699 			struct linger *lgr = (struct linger *)ptr;
700 
701 			lgr->l_onoff = sctp->sctp_linger ? SO_LINGER : 0;
702 			lgr->l_linger = TICK_TO_MSEC(sctp->sctp_lingertime);
703 			*optlen = sizeof (struct linger);
704 			break;
705 		}
706 		case SO_DEBUG:
707 			*i1 = sctp->sctp_debug ? SO_DEBUG : 0;
708 			break;
709 		case SO_DONTROUTE:
710 			*i1 = connp->conn_dontroute ? SO_DONTROUTE : 0;
711 			break;
712 		case SO_USELOOPBACK:
713 			*i1 = connp->conn_loopback ? SO_USELOOPBACK : 0;
714 			break;
715 		case SO_BROADCAST:
716 			*i1 = connp->conn_broadcast ? SO_BROADCAST : 0;
717 			break;
718 		case SO_REUSEADDR:
719 			*i1 = connp->conn_reuseaddr ? SO_REUSEADDR : 0;
720 			break;
721 		case SO_DGRAM_ERRIND:
722 			*i1 = sctp->sctp_dgram_errind ? SO_DGRAM_ERRIND : 0;
723 			break;
724 		case SO_SNDBUF:
725 			*i1 = sctp->sctp_xmit_hiwater;
726 			break;
727 		case SO_RCVBUF:
728 			*i1 = sctp->sctp_rwnd;
729 			break;
730 		case SO_ALLZONES:
731 			*i1 = connp->conn_allzones;
732 			break;
733 		case SO_MAC_EXEMPT:
734 			*i1 = connp->conn_mac_exempt;
735 			break;
736 		case SO_PROTOTYPE:
737 			*i1 = IPPROTO_SCTP;
738 			break;
739 		case SO_DOMAIN:
740 			*i1 = sctp->sctp_family;
741 			break;
742 		default:
743 			retval = ENOPROTOOPT;
744 			break;
745 		}
746 		break;
747 
748 	case IPPROTO_SCTP:
749 		switch (name) {
750 		case SCTP_RTOINFO:
751 			if (buflen < sizeof (struct sctp_rtoinfo)) {
752 				retval = EINVAL;
753 				break;
754 			}
755 			*optlen = sctp_get_rtoinfo(sctp, ptr);
756 			break;
757 		case SCTP_ASSOCINFO:
758 			if (buflen < sizeof (struct sctp_assocparams)) {
759 				retval = EINVAL;
760 				break;
761 			}
762 			*optlen = sctp_get_assocparams(sctp, ptr);
763 			break;
764 		case SCTP_INITMSG:
765 			if (buflen < sizeof (struct sctp_initmsg)) {
766 				retval = EINVAL;
767 				break;
768 			}
769 			*optlen = sctp_get_initmsg(sctp, ptr);
770 			break;
771 		case SCTP_NODELAY:
772 			*i1 = sctp->sctp_ndelay;
773 			break;
774 		case SCTP_AUTOCLOSE:
775 			*i1 = TICK_TO_SEC(sctp->sctp_autoclose);
776 			break;
777 		case SCTP_ADAPTION_LAYER:
778 			if (buflen < sizeof (struct sctp_setadaption)) {
779 				retval = EINVAL;
780 				break;
781 			}
782 			((struct sctp_setadaption *)ptr)->ssb_adaption_ind =
783 			    sctp->sctp_tx_adaption_code;
784 			break;
785 		case SCTP_PEER_ADDR_PARAMS:
786 			if (buflen < sizeof (struct sctp_paddrparams)) {
787 				retval = EINVAL;
788 				break;
789 			}
790 			*optlen = sctp_get_peer_addr_params(sctp, ptr);
791 			break;
792 		case SCTP_DEFAULT_SEND_PARAM:
793 			if (buflen < sizeof (struct sctp_sndrcvinfo)) {
794 				retval = EINVAL;
795 				break;
796 			}
797 			*optlen = sctp_get_def_send_params(sctp, ptr);
798 			break;
799 		case SCTP_EVENTS: {
800 			struct sctp_event_subscribe *ev;
801 
802 			if (buflen < sizeof (struct sctp_event_subscribe)) {
803 				retval = EINVAL;
804 				break;
805 			}
806 			ev = (struct sctp_event_subscribe *)ptr;
807 			ev->sctp_data_io_event =
808 			    ONOFF(sctp->sctp_recvsndrcvinfo);
809 			ev->sctp_association_event =
810 			    ONOFF(sctp->sctp_recvassocevnt);
811 			ev->sctp_address_event =
812 			    ONOFF(sctp->sctp_recvpathevnt);
813 			ev->sctp_send_failure_event =
814 			    ONOFF(sctp->sctp_recvsendfailevnt);
815 			ev->sctp_peer_error_event =
816 			    ONOFF(sctp->sctp_recvpeererr);
817 			ev->sctp_shutdown_event =
818 			    ONOFF(sctp->sctp_recvshutdownevnt);
819 			ev->sctp_partial_delivery_event =
820 			    ONOFF(sctp->sctp_recvpdevnt);
821 			ev->sctp_adaption_layer_event =
822 				ONOFF(sctp->sctp_recvalevnt);
823 			*optlen = sizeof (struct sctp_event_subscribe);
824 			break;
825 		}
826 		case SCTP_STATUS:
827 			if (buflen < sizeof (struct sctp_status)) {
828 				retval = EINVAL;
829 				break;
830 			}
831 			*optlen = sctp_get_status(sctp, ptr);
832 			break;
833 		case SCTP_GET_PEER_ADDR_INFO:
834 			if (buflen < sizeof (struct sctp_paddrinfo)) {
835 				retval = EINVAL;
836 				break;
837 			}
838 			retval = sctp_get_paddrinfo(sctp, ptr, optlen);
839 			break;
840 		case SCTP_GET_NLADDRS:
841 			*(int32_t *)ptr = sctp->sctp_nsaddrs;
842 			break;
843 		case SCTP_GET_LADDRS: {
844 			int addr_cnt;
845 			int addr_size;
846 
847 			if (sctp->sctp_family == AF_INET)
848 				addr_size = sizeof (struct sockaddr_in);
849 			else
850 				addr_size = sizeof (struct sockaddr_in6);
851 			addr_cnt = buflen / addr_size;
852 			retval = sctp_getmyaddrs(sctp, ptr, &addr_cnt);
853 			if (retval == 0)
854 				*optlen = addr_cnt * addr_size;
855 			break;
856 		}
857 		case SCTP_GET_NPADDRS: {
858 			int i;
859 			sctp_faddr_t *fp;
860 
861 			for (i = 0, fp = sctp->sctp_faddrs; fp != NULL;
862 			    i++, fp = fp->next)
863 				;
864 			*(int32_t *)ptr = i;
865 			break;
866 		}
867 		case SCTP_GET_PADDRS: {
868 			int addr_cnt;
869 			int addr_size;
870 
871 			if (sctp->sctp_family == AF_INET)
872 				addr_size = sizeof (struct sockaddr_in);
873 			else
874 				addr_size = sizeof (struct sockaddr_in6);
875 			addr_cnt = buflen / addr_size;
876 			retval = sctp_getpeeraddrs(sctp, ptr, &addr_cnt);
877 			if (retval == 0)
878 				*optlen = addr_cnt * addr_size;
879 			break;
880 		}
881 		case SCTP_PRSCTP:
882 			*i1 = sctp->sctp_prsctp_aware ? 1 : 0;
883 			break;
884 		case SCTP_I_WANT_MAPPED_V4_ADDR:
885 		case SCTP_MAXSEG:
886 		case SCTP_DISABLE_FRAGMENTS:
887 			/* Not yet supported. */
888 		default:
889 			retval = ENOPROTOOPT;
890 			break;
891 		}
892 		break;
893 
894 	case IPPROTO_IP:
895 		if (sctp->sctp_family != AF_INET) {
896 			retval = EINVAL;
897 			break;
898 		}
899 		switch (name) {
900 		case IP_OPTIONS:
901 		case T_IP_OPTIONS: {
902 			/*
903 			 * This is compatible with BSD in that in only return
904 			 * the reverse source route with the final destination
905 			 * as the last entry. The first 4 bytes of the option
906 			 * will contain the final destination. Allocate a
907 			 * buffer large enough to hold all the options, we
908 			 * add IP_ADDR_LEN to SCTP_MAX_IP_OPTIONS_LENGTH since
909 			 * ip_opt_get_user() adds the final destination
910 			 * at the start.
911 			 */
912 			char	*opt_ptr;
913 			int	opt_len;
914 			uchar_t	obuf[SCTP_MAX_IP_OPTIONS_LENGTH + IP_ADDR_LEN];
915 
916 			opt_ptr = (char *)sctp->sctp_ipha +
917 			    IP_SIMPLE_HDR_LENGTH;
918 			opt_len = (char *)sctp->sctp_sctph - opt_ptr;
919 			/* Caller ensures enough space */
920 			if (opt_len > 0) {
921 				/*
922 				 * TODO: Do we have to handle getsockopt on an
923 				 * initiator as well?
924 				 */
925 				opt_len = ip_opt_get_user(sctp->sctp_ipha,
926 				    obuf);
927 				ASSERT(opt_len <= sizeof (obuf));
928 			} else {
929 				opt_len = 0;
930 			}
931 			if (buflen < opt_len) {
932 				/* Silently truncate */
933 				opt_len = buflen;
934 			}
935 			*optlen = opt_len;
936 			bcopy(obuf, ptr, opt_len);
937 			break;
938 		}
939 		case IP_TOS:
940 		case T_IP_TOS:
941 			*i1 = (int)sctp->sctp_ipha->ipha_type_of_service;
942 			break;
943 		case IP_TTL:
944 			*i1 = (int)sctp->sctp_ipha->ipha_ttl;
945 			break;
946 		case IP_NEXTHOP:
947 			if (connp->conn_nexthop_set) {
948 				*(ipaddr_t *)ptr = connp->conn_nexthop_v4;
949 				*optlen = sizeof (ipaddr_t);
950 			} else {
951 				*optlen = 0;
952 			}
953 			break;
954 		default:
955 			retval = ENOPROTOOPT;
956 			break;
957 		}
958 		break;
959 	case IPPROTO_IPV6:
960 		if (sctp->sctp_family != AF_INET6) {
961 			retval = EINVAL;
962 			break;
963 		}
964 		switch (name) {
965 		case IPV6_UNICAST_HOPS:
966 			*i1 = (unsigned int) sctp->sctp_ip6h->ip6_hops;
967 			break;	/* goto sizeof (int) option return */
968 		case IPV6_RECVPKTINFO:
969 			if (sctp->sctp_ipv6_recvancillary &
970 			    SCTP_IPV6_RECVPKTINFO) {
971 				*i1 = 1;
972 			} else {
973 				*i1 = 0;
974 			}
975 			break;	/* goto sizeof (int) option return */
976 		case IPV6_RECVHOPLIMIT:
977 			if (sctp->sctp_ipv6_recvancillary &
978 			    SCTP_IPV6_RECVHOPLIMIT) {
979 				*i1 = 1;
980 			} else {
981 				*i1 = 0;
982 			}
983 			break;	/* goto sizeof (int) option return */
984 		case IPV6_RECVHOPOPTS:
985 			if (sctp->sctp_ipv6_recvancillary &
986 			    SCTP_IPV6_RECVHOPOPTS) {
987 				*i1 = 1;
988 			} else {
989 				*i1 = 0;
990 			}
991 			break;	/* goto sizeof (int) option return */
992 		case IPV6_RECVDSTOPTS:
993 			if (sctp->sctp_ipv6_recvancillary &
994 			    SCTP_IPV6_RECVDSTOPTS) {
995 				*i1 = 1;
996 			} else {
997 				*i1 = 0;
998 			}
999 			break;	/* goto sizeof (int) option return */
1000 		case IPV6_RECVRTHDR:
1001 			if (sctp->sctp_ipv6_recvancillary &
1002 			    SCTP_IPV6_RECVRTHDR) {
1003 				*i1 = 1;
1004 			} else {
1005 				*i1 = 0;
1006 			}
1007 			break;	/* goto sizeof (int) option return */
1008 		case IPV6_RECVRTHDRDSTOPTS:
1009 			if (sctp->sctp_ipv6_recvancillary &
1010 			    SCTP_IPV6_RECVRTDSTOPTS) {
1011 				*i1 = 1;
1012 			} else {
1013 				*i1 = 0;
1014 			}
1015 			break;	/* goto sizeof (int) option return */
1016 		case IPV6_PKTINFO: {
1017 			struct in6_pktinfo *pkti;
1018 
1019 			if (buflen < sizeof (struct in6_pktinfo)) {
1020 				retval = EINVAL;
1021 				break;
1022 			}
1023 			pkti = (struct in6_pktinfo *)ptr;
1024 			if (ipp->ipp_fields & IPPF_IFINDEX)
1025 				pkti->ipi6_ifindex = ipp->ipp_ifindex;
1026 			else
1027 				pkti->ipi6_ifindex = 0;
1028 			if (ipp->ipp_fields & IPPF_ADDR)
1029 				pkti->ipi6_addr = ipp->ipp_addr;
1030 			else
1031 				pkti->ipi6_addr = ipv6_all_zeros;
1032 			*optlen = sizeof (struct in6_pktinfo);
1033 			break;
1034 		}
1035 		case IPV6_NEXTHOP: {
1036 			sin6_t *sin6;
1037 
1038 			if (buflen < sizeof (sin6_t)) {
1039 				retval = EINVAL;
1040 				break;
1041 			}
1042 			sin6 = (sin6_t *)ptr;
1043 			if (!(ipp->ipp_fields & IPPF_NEXTHOP))
1044 				break;
1045 			*sin6 = sctp_sin6_null;
1046 			sin6->sin6_family = AF_INET6;
1047 			sin6->sin6_addr = ipp->ipp_nexthop;
1048 			*optlen = sizeof (sin6_t);
1049 			break;
1050 		}
1051 		case IPV6_HOPOPTS: {
1052 			int len;
1053 
1054 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
1055 				break;
1056 			len = ipp->ipp_hopoptslen - sctp->sctp_v6label_len;
1057 			if (len <= 0)
1058 				break;
1059 			if (buflen < len) {
1060 				retval = EINVAL;
1061 				break;
1062 			}
1063 			bcopy((char *)ipp->ipp_hopopts +
1064 			    sctp->sctp_v6label_len, ptr, len);
1065 			if (sctp->sctp_v6label_len > 0) {
1066 				char *cptr = ptr;
1067 
1068 				/*
1069 				 * If the label length is greater than zero,
1070 				 * then we need to hide the label from user.
1071 				 * Make it look as though a normal Hop-By-Hop
1072 				 * Options Header is present here.
1073 				 */
1074 				cptr[0] = ((char *)ipp->ipp_hopopts)[0];
1075 				cptr[1] = (len + 7) / 8 - 1;
1076 			}
1077 			*optlen = len;
1078 			break;
1079 		}
1080 		case IPV6_RTHDRDSTOPTS:
1081 			if (!(ipp->ipp_fields & IPPF_RTDSTOPTS))
1082 				break;
1083 			if (buflen < ipp->ipp_rtdstoptslen) {
1084 				retval = EINVAL;
1085 				break;
1086 			}
1087 			bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen);
1088 			*optlen  = ipp->ipp_rtdstoptslen;
1089 			break;
1090 		case IPV6_RTHDR:
1091 			if (!(ipp->ipp_fields & IPPF_RTHDR))
1092 				break;
1093 			if (buflen < ipp->ipp_rthdrlen) {
1094 				retval = EINVAL;
1095 				break;
1096 			}
1097 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
1098 			*optlen = ipp->ipp_rthdrlen;
1099 			break;
1100 		case IPV6_DSTOPTS:
1101 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
1102 				break;
1103 			if (buflen < ipp->ipp_dstoptslen) {
1104 				retval = EINVAL;
1105 				break;
1106 			}
1107 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
1108 			*optlen  = ipp->ipp_dstoptslen;
1109 			break;
1110 		case IPV6_V6ONLY:
1111 			*i1 = sctp->sctp_connp->conn_ipv6_v6only;
1112 			break;
1113 		default:
1114 			retval = ENOPROTOOPT;
1115 			break;
1116 		}
1117 		break;
1118 
1119 	default:
1120 		retval = ENOPROTOOPT;
1121 		break;
1122 	}
1123 	WAKE_SCTP(sctp);
1124 	return (retval);
1125 }
1126 
1127 int
1128 sctp_set_opt(sctp_t *sctp, int level, int name, const void *invalp,
1129     socklen_t inlen)
1130 {
1131 	ip6_pkt_t	*ipp = &sctp->sctp_sticky_ipp;
1132 	int		*i1 = (int *)invalp;
1133 	boolean_t	onoff;
1134 	int		retval = 0, addrcnt;
1135 	conn_t		*connp = sctp->sctp_connp;
1136 	sctp_stack_t	*sctps = sctp->sctp_sctps;
1137 
1138 	/* In all cases, the size of the option must be bigger than int */
1139 	if (inlen >= sizeof (int32_t)) {
1140 		onoff = ONOFF(*i1);
1141 	}
1142 	retval = 0;
1143 
1144 	RUN_SCTP(sctp);
1145 
1146 	switch (level) {
1147 	case SOL_SOCKET:
1148 		if (inlen < sizeof (int32_t)) {
1149 			retval = EINVAL;
1150 			break;
1151 		}
1152 		switch (name) {
1153 		case SO_LINGER: {
1154 			struct linger *lgr;
1155 
1156 			if (inlen != sizeof (struct linger)) {
1157 				retval = EINVAL;
1158 				break;
1159 			}
1160 			lgr = (struct linger *)invalp;
1161 			if (lgr->l_onoff != 0) {
1162 				sctp->sctp_linger = 1;
1163 				sctp->sctp_lingertime = MSEC_TO_TICK(
1164 				    lgr->l_linger);
1165 			} else {
1166 				sctp->sctp_linger = 0;
1167 				sctp->sctp_lingertime = 0;
1168 			}
1169 			break;
1170 		}
1171 		case SO_DEBUG:
1172 			sctp->sctp_debug = onoff;
1173 			break;
1174 		case SO_KEEPALIVE:
1175 			break;
1176 		case SO_DONTROUTE:
1177 			/*
1178 			 * SO_DONTROUTE, SO_USELOOPBACK and SO_BROADCAST are
1179 			 * only of interest to IP.
1180 			 */
1181 			connp->conn_dontroute = onoff;
1182 			break;
1183 		case SO_USELOOPBACK:
1184 			connp->conn_loopback = onoff;
1185 			break;
1186 		case SO_BROADCAST:
1187 			connp->conn_broadcast = onoff;
1188 			break;
1189 		case SO_REUSEADDR:
1190 			connp->conn_reuseaddr = onoff;
1191 			break;
1192 		case SO_DGRAM_ERRIND:
1193 			sctp->sctp_dgram_errind = onoff;
1194 			break;
1195 		case SO_SNDBUF:
1196 			if (*i1 > sctps->sctps_max_buf) {
1197 				retval = ENOBUFS;
1198 				break;
1199 			}
1200 			if (*i1 < 0) {
1201 				retval = EINVAL;
1202 				break;
1203 			}
1204 			sctp->sctp_xmit_hiwater = *i1;
1205 			if (sctps->sctps_snd_lowat_fraction != 0)
1206 				sctp->sctp_xmit_lowater =
1207 				    sctp->sctp_xmit_hiwater /
1208 				    sctps->sctps_snd_lowat_fraction;
1209 			break;
1210 		case SO_RCVBUF:
1211 			if (*i1 > sctps->sctps_max_buf) {
1212 				retval = ENOBUFS;
1213 				break;
1214 			}
1215 			/* Silently ignore zero */
1216 			if (*i1 != 0) {
1217 				/*
1218 				 * Insist on a receive window that is at least
1219 				 * sctp_recv_hiwat_minmss * MSS (default 4*MSS)
1220 				 * to avoid funny interactions of Nagle
1221 				 * algorithm, SWS avoidance and delayed
1222 				 * acknowledgement.
1223 				 */
1224 				*i1 = MAX(*i1,
1225 				    sctps->sctps_recv_hiwat_minmss *
1226 				    sctp->sctp_mss);
1227 				sctp->sctp_rwnd = *i1;
1228 				sctp->sctp_irwnd = sctp->sctp_rwnd;
1229 			}
1230 			/*
1231 			 * XXX should we return the rwnd here
1232 			 * and sctp_opt_get ?
1233 			 */
1234 			break;
1235 		case SO_ALLZONES:
1236 			if (secpolicy_ip(sctp->sctp_credp, OP_CONFIG,
1237 			    B_TRUE)) {
1238 				retval = EACCES;
1239 				break;
1240 			}
1241 			if (sctp->sctp_state >= SCTPS_BOUND) {
1242 				retval = EINVAL;
1243 				break;
1244 			}
1245 			sctp->sctp_allzones = onoff;
1246 			break;
1247 		case SO_MAC_EXEMPT:
1248 			if (secpolicy_net_mac_aware(sctp->sctp_credp) != 0) {
1249 				retval = EACCES;
1250 				break;
1251 			}
1252 			if (sctp->sctp_state >= SCTPS_BOUND) {
1253 				retval = EINVAL;
1254 				break;
1255 			}
1256 			connp->conn_mac_exempt = onoff;
1257 			break;
1258 		default:
1259 			retval = ENOPROTOOPT;
1260 			break;
1261 		}
1262 		break;
1263 
1264 	case IPPROTO_SCTP:
1265 		if (inlen < sizeof (int32_t)) {
1266 			retval = EINVAL;
1267 			break;
1268 		}
1269 		switch (name) {
1270 		case SCTP_RTOINFO:
1271 			retval = sctp_set_rtoinfo(sctp, invalp, inlen);
1272 			break;
1273 		case SCTP_ASSOCINFO:
1274 			retval = sctp_set_assocparams(sctp, invalp, inlen);
1275 			break;
1276 		case SCTP_INITMSG:
1277 			retval = sctp_set_initmsg(sctp, invalp, inlen);
1278 			break;
1279 		case SCTP_NODELAY:
1280 			sctp->sctp_ndelay = ONOFF(*i1);
1281 			break;
1282 		case SCTP_AUTOCLOSE:
1283 			if (SEC_TO_TICK(*i1) < 0) {
1284 				retval = EINVAL;
1285 				break;
1286 			}
1287 			/* Convert the number of seconds to ticks. */
1288 			sctp->sctp_autoclose = SEC_TO_TICK(*i1);
1289 			sctp_heartbeat_timer(sctp);
1290 			break;
1291 		case SCTP_SET_PEER_PRIMARY_ADDR:
1292 			retval = sctp_set_peerprim(sctp, invalp, inlen);
1293 			break;
1294 		case SCTP_PRIMARY_ADDR:
1295 			retval = sctp_set_prim(sctp, invalp, inlen);
1296 			break;
1297 		case SCTP_ADAPTION_LAYER: {
1298 			struct sctp_setadaption *ssb;
1299 
1300 			if (inlen < sizeof (struct sctp_setadaption)) {
1301 				retval = EINVAL;
1302 				break;
1303 			}
1304 			ssb = (struct sctp_setadaption *)invalp;
1305 			sctp->sctp_send_adaption = 1;
1306 			sctp->sctp_tx_adaption_code = ssb->ssb_adaption_ind;
1307 			break;
1308 		}
1309 		case SCTP_PEER_ADDR_PARAMS:
1310 			retval = sctp_set_peer_addr_params(sctp, invalp,
1311 			    inlen);
1312 			break;
1313 		case SCTP_DEFAULT_SEND_PARAM:
1314 			retval = sctp_set_def_send_params(sctp, invalp, inlen);
1315 			break;
1316 		case SCTP_EVENTS: {
1317 			struct sctp_event_subscribe *ev;
1318 
1319 			if (inlen < sizeof (struct sctp_event_subscribe)) {
1320 				retval = EINVAL;
1321 				break;
1322 			}
1323 			ev = (struct sctp_event_subscribe *)invalp;
1324 			sctp->sctp_recvsndrcvinfo =
1325 			    ONOFF(ev->sctp_data_io_event);
1326 			sctp->sctp_recvassocevnt =
1327 			    ONOFF(ev->sctp_association_event);
1328 			sctp->sctp_recvpathevnt =
1329 			    ONOFF(ev->sctp_address_event);
1330 			sctp->sctp_recvsendfailevnt =
1331 			    ONOFF(ev->sctp_send_failure_event);
1332 			sctp->sctp_recvpeererr =
1333 			    ONOFF(ev->sctp_peer_error_event);
1334 			sctp->sctp_recvshutdownevnt =
1335 			    ONOFF(ev->sctp_shutdown_event);
1336 			sctp->sctp_recvpdevnt =
1337 			    ONOFF(ev->sctp_partial_delivery_event);
1338 			sctp->sctp_recvalevnt =
1339 				ONOFF(ev->sctp_adaption_layer_event);
1340 			break;
1341 		}
1342 		case SCTP_ADD_ADDR:
1343 		case SCTP_REM_ADDR:
1344 			/*
1345 			 * The sctp_t has to be bound first before
1346 			 * the address list can be changed.
1347 			 */
1348 			if (sctp->sctp_state < SCTPS_BOUND) {
1349 				retval = EINVAL;
1350 				break;
1351 			}
1352 			if (sctp->sctp_family == AF_INET) {
1353 				addrcnt = inlen / sizeof (struct sockaddr_in);
1354 			} else {
1355 				ASSERT(sctp->sctp_family == AF_INET6);
1356 				addrcnt = inlen / sizeof (struct sockaddr_in6);
1357 			}
1358 			if (name == SCTP_ADD_ADDR) {
1359 				retval = sctp_bind_add(sctp, invalp, addrcnt,
1360 				    B_TRUE, sctp->sctp_lport);
1361 			} else {
1362 				retval = sctp_bind_del(sctp, invalp, addrcnt,
1363 				    B_TRUE);
1364 			}
1365 			break;
1366 		case SCTP_UC_SWAP: {
1367 			struct sctp_uc_swap *us;
1368 
1369 			/*
1370 			 * Change handle & upcalls.
1371 			 */
1372 			if (inlen < sizeof (*us)) {
1373 				retval = EINVAL;
1374 				break;
1375 			}
1376 			us = (struct sctp_uc_swap *)invalp;
1377 			sctp->sctp_ulpd = us->sus_handle;
1378 			bcopy(us->sus_upcalls, &sctp->sctp_upcalls,
1379 			    sizeof (sctp_upcalls_t));
1380 			break;
1381 		}
1382 		case SCTP_PRSCTP:
1383 			sctp->sctp_prsctp_aware = onoff;
1384 			break;
1385 		case SCTP_I_WANT_MAPPED_V4_ADDR:
1386 		case SCTP_MAXSEG:
1387 		case SCTP_DISABLE_FRAGMENTS:
1388 			/* Not yet supported. */
1389 		default:
1390 			retval = ENOPROTOOPT;
1391 			break;
1392 		}
1393 		break;
1394 
1395 	case IPPROTO_IP:
1396 		if (sctp->sctp_family != AF_INET) {
1397 			retval = ENOPROTOOPT;
1398 			break;
1399 		}
1400 		if ((name != IP_OPTIONS) && (inlen < sizeof (int32_t))) {
1401 			retval = EINVAL;
1402 			break;
1403 		}
1404 		switch (name) {
1405 		case IP_OPTIONS:
1406 		case T_IP_OPTIONS:
1407 			retval = sctp_opt_set_header(sctp, invalp, inlen);
1408 			break;
1409 		case IP_TOS:
1410 		case T_IP_TOS:
1411 			sctp->sctp_ipha->ipha_type_of_service = (uchar_t)*i1;
1412 			break;
1413 		case IP_TTL:
1414 			sctp->sctp_ipha->ipha_ttl = (uchar_t)*i1;
1415 			break;
1416 		case IP_SEC_OPT:
1417 			/*
1418 			 * We should not allow policy setting after
1419 			 * we start listening for connections.
1420 			 */
1421 			if (sctp->sctp_state >= SCTPS_LISTEN) {
1422 				retval = EINVAL;
1423 			} else {
1424 				retval = ipsec_set_req(sctp->sctp_credp,
1425 				    sctp->sctp_connp, (ipsec_req_t *)invalp);
1426 			}
1427 			break;
1428 		/* IP level options */
1429 		case IP_RECVIF:
1430 			connp->conn_recvif = onoff;
1431 			break;
1432 		case IP_RECVSLLA:
1433 			connp->conn_recvslla = onoff;
1434 			break;
1435 		case IP_UNSPEC_SRC:
1436 			connp->conn_unspec_src = onoff;
1437 			break;
1438 		case IP_NEXTHOP: {
1439 			ipaddr_t addr = *i1;
1440 			ipif_t *ipif = NULL;
1441 			ill_t *ill;
1442 			ip_stack_t *ipst = sctps->sctps_netstack->netstack_ip;
1443 
1444 			if (secpolicy_ip(sctp->sctp_credp, OP_CONFIG,
1445 			    B_TRUE) == 0) {
1446 				ipif =
1447 				    ipif_lookup_onlink_addr(addr,
1448 					connp->conn_zoneid, ipst);
1449 				if (ipif == NULL) {
1450 					retval = EHOSTUNREACH;
1451 					break;
1452 				}
1453 				ill = ipif->ipif_ill;
1454 				mutex_enter(&ill->ill_lock);
1455 				if ((ill->ill_state_flags & ILL_CONDEMNED) ||
1456 				    (ipif->ipif_state_flags & IPIF_CONDEMNED)) {
1457 					mutex_exit(&ill->ill_lock);
1458 					ipif_refrele(ipif);
1459 					retval =  EHOSTUNREACH;
1460 					break;
1461 				}
1462 				mutex_exit(&ill->ill_lock);
1463 				ipif_refrele(ipif);
1464 				mutex_enter(&connp->conn_lock);
1465 				connp->conn_nexthop_v4 = addr;
1466 				connp->conn_nexthop_set = B_TRUE;
1467 				mutex_exit(&connp->conn_lock);
1468 			}
1469 			break;
1470 		}
1471 		default:
1472 			retval = ENOPROTOOPT;
1473 			break;
1474 		}
1475 		break;
1476 	case IPPROTO_IPV6: {
1477 		if (sctp->sctp_family != AF_INET6) {
1478 			retval = ENOPROTOOPT;
1479 			break;
1480 		}
1481 
1482 		switch (name) {
1483 		case IPV6_UNICAST_HOPS:
1484 			if (inlen < sizeof (int32_t)) {
1485 				retval = EINVAL;
1486 				break;
1487 			}
1488 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
1489 				retval = EINVAL;
1490 				break;
1491 			}
1492 			if (*i1 == -1) {
1493 				ipp->ipp_unicast_hops =
1494 				    sctps->sctps_ipv6_hoplimit;
1495 				ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
1496 			} else {
1497 				ipp->ipp_unicast_hops = (uint8_t)*i1;
1498 				ipp->ipp_fields |= IPPF_UNICAST_HOPS;
1499 			}
1500 			retval = sctp_build_hdrs(sctp);
1501 			break;
1502 		case IPV6_UNSPEC_SRC:
1503 			if (inlen < sizeof (int32_t)) {
1504 				retval = EINVAL;
1505 				break;
1506 			}
1507 			connp->conn_unspec_src = onoff;
1508 			break;
1509 		case IPV6_RECVPKTINFO:
1510 			if (inlen < sizeof (int32_t)) {
1511 				retval = EINVAL;
1512 				break;
1513 			}
1514 			if (onoff)
1515 				sctp->sctp_ipv6_recvancillary |=
1516 				    SCTP_IPV6_RECVPKTINFO;
1517 			else
1518 				sctp->sctp_ipv6_recvancillary &=
1519 				    ~SCTP_IPV6_RECVPKTINFO;
1520 			/* Send it with the next msg */
1521 			sctp->sctp_recvifindex = 0;
1522 			connp->conn_ip_recvpktinfo = onoff;
1523 			break;
1524 		case IPV6_RECVHOPLIMIT:
1525 			if (inlen < sizeof (int32_t)) {
1526 				retval = EINVAL;
1527 				break;
1528 			}
1529 			if (onoff)
1530 				sctp->sctp_ipv6_recvancillary |=
1531 				    SCTP_IPV6_RECVHOPLIMIT;
1532 			else
1533 				sctp->sctp_ipv6_recvancillary &=
1534 				    ~SCTP_IPV6_RECVHOPLIMIT;
1535 			sctp->sctp_recvhops = 0xffffffffU;
1536 			connp->conn_ipv6_recvhoplimit = onoff;
1537 			break;
1538 		case IPV6_RECVHOPOPTS:
1539 			if (inlen < sizeof (int32_t)) {
1540 				retval = EINVAL;
1541 				break;
1542 			}
1543 			if (onoff)
1544 				sctp->sctp_ipv6_recvancillary |=
1545 				    SCTP_IPV6_RECVHOPOPTS;
1546 			else
1547 				sctp->sctp_ipv6_recvancillary &=
1548 				    ~SCTP_IPV6_RECVHOPOPTS;
1549 			connp->conn_ipv6_recvhopopts = onoff;
1550 			break;
1551 		case IPV6_RECVDSTOPTS:
1552 			if (inlen < sizeof (int32_t)) {
1553 				retval = EINVAL;
1554 				break;
1555 			}
1556 			if (onoff)
1557 				sctp->sctp_ipv6_recvancillary |=
1558 				    SCTP_IPV6_RECVDSTOPTS;
1559 			else
1560 				sctp->sctp_ipv6_recvancillary &=
1561 				    ~SCTP_IPV6_RECVDSTOPTS;
1562 			connp->conn_ipv6_recvdstopts = onoff;
1563 			break;
1564 		case IPV6_RECVRTHDR:
1565 			if (inlen < sizeof (int32_t)) {
1566 				retval = EINVAL;
1567 				break;
1568 			}
1569 			if (onoff)
1570 				sctp->sctp_ipv6_recvancillary |=
1571 				    SCTP_IPV6_RECVRTHDR;
1572 			else
1573 				sctp->sctp_ipv6_recvancillary &=
1574 				    ~SCTP_IPV6_RECVRTHDR;
1575 			connp->conn_ipv6_recvrthdr = onoff;
1576 			break;
1577 		case IPV6_RECVRTHDRDSTOPTS:
1578 			if (inlen < sizeof (int32_t)) {
1579 				retval = EINVAL;
1580 				break;
1581 			}
1582 			if (onoff)
1583 				sctp->sctp_ipv6_recvancillary |=
1584 				    SCTP_IPV6_RECVRTDSTOPTS;
1585 			else
1586 				sctp->sctp_ipv6_recvancillary &=
1587 				    ~SCTP_IPV6_RECVRTDSTOPTS;
1588 			connp->conn_ipv6_recvrtdstopts = onoff;
1589 			break;
1590 		case IPV6_PKTINFO:
1591 			if (inlen != 0 &&
1592 			    inlen != sizeof (struct in6_pktinfo)) {
1593 				retval = EINVAL;
1594 				break;
1595 			}
1596 
1597 			if (inlen == 0) {
1598 				ipp->ipp_fields &= ~(IPPF_IFINDEX |IPPF_ADDR);
1599 			} else  {
1600 				struct in6_pktinfo *pkti;
1601 
1602 				pkti = (struct in6_pktinfo *)invalp;
1603 				/* XXX Need to check if the index exists */
1604 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
1605 				ipp->ipp_addr = pkti->ipi6_addr;
1606 				if (ipp->ipp_ifindex != 0)
1607 					ipp->ipp_fields |= IPPF_IFINDEX;
1608 				else
1609 					ipp->ipp_fields &= ~IPPF_IFINDEX;
1610 				if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr))
1611 					ipp->ipp_fields |= IPPF_ADDR;
1612 				else
1613 					ipp->ipp_fields &= ~IPPF_ADDR;
1614 			}
1615 			retval = sctp_build_hdrs(sctp);
1616 			break;
1617 		case IPV6_NEXTHOP: {
1618 			struct sockaddr_in6 *sin6;
1619 			ip_stack_t *ipst = sctps->sctps_netstack->netstack_ip;
1620 
1621 			if (inlen != 0 && inlen != sizeof (sin6_t)) {
1622 				retval = EINVAL;
1623 				break;
1624 			}
1625 
1626 			if (inlen == 0) {
1627 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
1628 			} else {
1629 				sin6 = (struct sockaddr_in6 *)invalp;
1630 				if (sin6->sin6_family != AF_INET6) {
1631 					retval = EAFNOSUPPORT;
1632 					break;
1633 				}
1634 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1635 					retval = EADDRNOTAVAIL;
1636 					break;
1637 				}
1638 				ipp->ipp_nexthop = sin6->sin6_addr;
1639 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1640 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
1641 				} else {
1642 					ire_t	*ire;
1643 
1644 					ire = ire_route_lookup_v6(
1645 					    &sin6->sin6_addr, NULL, NULL, 0,
1646 					    NULL, NULL, ALL_ZONES, NULL,
1647 					    MATCH_IRE_DEFAULT, ipst);
1648 					if (ire == NULL) {
1649 						retval = EHOSTUNREACH;
1650 						break;
1651 					}
1652 					ire_refrele(ire);
1653 					ipp->ipp_fields |= IPPF_NEXTHOP;
1654 				}
1655 			}
1656 			retval = sctp_build_hdrs(sctp);
1657 			break;
1658 		}
1659 		case IPV6_HOPOPTS: {
1660 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
1661 
1662 			if (inlen != 0 &&
1663 			    inlen != (8 * (hopts->ip6h_len + 1))) {
1664 				retval = EINVAL;
1665 				break;
1666 			}
1667 
1668 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1669 			    B_TRUE, (uchar_t **)&ipp->ipp_hopopts,
1670 			    &ipp->ipp_hopoptslen, sctp->sctp_v6label_len);
1671 			if (retval != 0)
1672 				break;
1673 			if (ipp->ipp_hopoptslen == 0)
1674 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
1675 			else
1676 				ipp->ipp_fields |= IPPF_HOPOPTS;
1677 			retval = sctp_build_hdrs(sctp);
1678 			break;
1679 		}
1680 		case IPV6_RTHDRDSTOPTS: {
1681 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
1682 
1683 			if (inlen != 0 &&
1684 			    inlen != (8 * (dopts->ip6d_len + 1))) {
1685 				retval = EINVAL;
1686 				break;
1687 			}
1688 
1689 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1690 			    B_TRUE, (uchar_t **)&ipp->ipp_rtdstopts,
1691 			    &ipp->ipp_rtdstoptslen, 0);
1692 			if (retval != 0)
1693 				break;
1694 			if (ipp->ipp_rtdstoptslen == 0)
1695 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
1696 			else
1697 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
1698 			retval = sctp_build_hdrs(sctp);
1699 			break;
1700 		}
1701 		case IPV6_DSTOPTS: {
1702 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
1703 
1704 			if (inlen != 0 &&
1705 			    inlen != (8 * (dopts->ip6d_len + 1))) {
1706 				retval = EINVAL;
1707 				break;
1708 			}
1709 
1710 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1711 			    B_TRUE, (uchar_t **)&ipp->ipp_dstopts,
1712 			    &ipp->ipp_dstoptslen, 0);
1713 			if (retval != 0)
1714 				break;
1715 			if (ipp->ipp_dstoptslen == 0)
1716 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
1717 			else
1718 				ipp->ipp_fields |= IPPF_DSTOPTS;
1719 			retval = sctp_build_hdrs(sctp);
1720 			break;
1721 		}
1722 		case IPV6_RTHDR: {
1723 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
1724 
1725 			if (inlen != 0 &&
1726 			    inlen != (8 * (rt->ip6r_len + 1))) {
1727 				retval = EINVAL;
1728 				break;
1729 			}
1730 
1731 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1732 			    B_TRUE, (uchar_t **)&ipp->ipp_rthdr,
1733 			    &ipp->ipp_rthdrlen, 0);
1734 			if (retval != 0)
1735 				break;
1736 			if (ipp->ipp_rthdrlen == 0)
1737 				ipp->ipp_fields &= ~IPPF_RTHDR;
1738 			else
1739 				ipp->ipp_fields |= IPPF_RTHDR;
1740 			retval = sctp_build_hdrs(sctp);
1741 			break;
1742 		}
1743 		case IPV6_SEC_OPT:
1744 			/*
1745 			 * We should not allow policy setting after
1746 			 * we start listening for connections.
1747 			 */
1748 			if (sctp->sctp_state >= SCTPS_LISTEN) {
1749 				retval = EINVAL;
1750 			} else {
1751 				retval = ipsec_set_req(sctp->sctp_credp,
1752 				    sctp->sctp_connp, (ipsec_req_t *)invalp);
1753 			}
1754 			break;
1755 		case IPV6_V6ONLY:
1756 			/*
1757 			 * After the bound state, setting the v6only option
1758 			 * is too late.
1759 			 */
1760 			if (sctp->sctp_state >= SCTPS_BOUND) {
1761 				retval = EINVAL;
1762 			} else {
1763 				sctp->sctp_connp->conn_ipv6_v6only = onoff;
1764 			}
1765 			break;
1766 		default:
1767 			retval = ENOPROTOOPT;
1768 			break;
1769 		}
1770 		break;
1771 	}
1772 	default:
1773 		retval = ENOPROTOOPT;
1774 		break;
1775 	}
1776 
1777 	WAKE_SCTP(sctp);
1778 	return (retval);
1779 }
1780 
1781 /*
1782  * SCTP exported kernel interface for geting the first source address of
1783  * a sctp_t.  The parameter addr is assumed to have enough space to hold
1784  * one socket address.
1785  */
1786 int
1787 sctp_getsockname(sctp_t *sctp, struct sockaddr *addr, socklen_t *addrlen)
1788 {
1789 	int	err = 0;
1790 	int	addrcnt = 1;
1791 	sin_t	*sin4;
1792 	sin6_t	*sin6;
1793 
1794 	ASSERT(sctp != NULL);
1795 
1796 	RUN_SCTP(sctp);
1797 	addr->sa_family = sctp->sctp_family;
1798 	switch (sctp->sctp_family) {
1799 	case AF_INET:
1800 		sin4 = (sin_t *)addr;
1801 		if ((sctp->sctp_state <= SCTPS_LISTEN) &&
1802 		    sctp->sctp_bound_to_all) {
1803 			sin4->sin_addr.s_addr = INADDR_ANY;
1804 			sin4->sin_port = sctp->sctp_lport;
1805 		} else {
1806 			err = sctp_getmyaddrs(sctp, sin4, &addrcnt);
1807 			if (err != 0) {
1808 				*addrlen = 0;
1809 				break;
1810 			}
1811 		}
1812 		*addrlen = sizeof (struct sockaddr_in);
1813 		break;
1814 	case AF_INET6:
1815 		sin6 = (sin6_t *)addr;
1816 		if ((sctp->sctp_state <= SCTPS_LISTEN) &&
1817 		    sctp->sctp_bound_to_all) {
1818 			bzero(&sin6->sin6_addr, sizeof (sin6->sin6_addr));
1819 			sin6->sin6_port = sctp->sctp_lport;
1820 		} else {
1821 			err = sctp_getmyaddrs(sctp, sin6, &addrcnt);
1822 			if (err != 0) {
1823 				*addrlen = 0;
1824 				break;
1825 			}
1826 		}
1827 		*addrlen = sizeof (struct sockaddr_in6);
1828 		sin6->sin6_flowinfo = sctp->sctp_ip6h->ip6_vcf &
1829 		    ~IPV6_VERS_AND_FLOW_MASK;
1830 		sin6->sin6_scope_id = 0;
1831 		sin6->__sin6_src_id = 0;
1832 		break;
1833 	}
1834 	WAKE_SCTP(sctp);
1835 	return (err);
1836 }
1837 
1838 /*
1839  * SCTP exported kernel interface for geting the primary peer address of
1840  * a sctp_t.  The parameter addr is assumed to have enough space to hold
1841  * one socket address.
1842  */
1843 int
1844 sctp_getpeername(sctp_t *sctp, struct sockaddr *addr, socklen_t *addrlen)
1845 {
1846 	int	err = 0;
1847 	int	addrcnt = 1;
1848 	sin6_t	*sin6;
1849 
1850 	ASSERT(sctp != NULL);
1851 
1852 	RUN_SCTP(sctp);
1853 	addr->sa_family = sctp->sctp_family;
1854 	switch (sctp->sctp_family) {
1855 	case AF_INET:
1856 		err = sctp_getpeeraddrs(sctp, addr, &addrcnt);
1857 		if (err != 0) {
1858 			*addrlen = 0;
1859 			break;
1860 		}
1861 		*addrlen = sizeof (struct sockaddr_in);
1862 		break;
1863 	case AF_INET6:
1864 		sin6 = (sin6_t *)addr;
1865 		err = sctp_getpeeraddrs(sctp, sin6, &addrcnt);
1866 		if (err != 0) {
1867 			*addrlen = 0;
1868 			break;
1869 		}
1870 		*addrlen = sizeof (struct sockaddr_in6);
1871 		sin6->sin6_flowinfo = 0;
1872 		sin6->sin6_scope_id = 0;
1873 		sin6->__sin6_src_id = 0;
1874 		break;
1875 	}
1876 	WAKE_SCTP(sctp);
1877 	return (err);
1878 }
1879 
1880 /*
1881  * Return a list of IP addresses of the peer endpoint of this sctp_t.
1882  * The parameter paddrs is supposed to be either (struct sockaddr_in *) or
1883  * (struct sockaddr_in6 *) depending on the address family of the sctp_t.
1884  */
1885 int
1886 sctp_getpeeraddrs(sctp_t *sctp, void *paddrs, int *addrcnt)
1887 {
1888 	int			family;
1889 	struct sockaddr_in	*sin4;
1890 	struct sockaddr_in6	*sin6;
1891 	int			max;
1892 	int			cnt;
1893 	sctp_faddr_t		*fp = sctp->sctp_faddrs;
1894 	in6_addr_t		addr;
1895 
1896 	ASSERT(sctp != NULL);
1897 
1898 	if (sctp->sctp_faddrs == NULL)
1899 		return (ENOTCONN);
1900 
1901 	family = sctp->sctp_family;
1902 	max = *addrcnt;
1903 
1904 	/* If we want only one, give the primary */
1905 	if (max == 1) {
1906 		addr = sctp->sctp_primary->faddr;
1907 		switch (family) {
1908 		case AF_INET:
1909 			sin4 = paddrs;
1910 			IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
1911 			sin4->sin_port = sctp->sctp_fport;
1912 			sin4->sin_family = AF_INET;
1913 			break;
1914 
1915 		case AF_INET6:
1916 			sin6 = paddrs;
1917 			sin6->sin6_addr = addr;
1918 			sin6->sin6_port = sctp->sctp_fport;
1919 			sin6->sin6_family = AF_INET6;
1920 			break;
1921 		}
1922 		return (0);
1923 	}
1924 
1925 	for (cnt = 0; cnt < max && fp != NULL; cnt++, fp = fp->next) {
1926 		addr = fp->faddr;
1927 		switch (family) {
1928 		case AF_INET:
1929 			ASSERT(IN6_IS_ADDR_V4MAPPED(&addr));
1930 			sin4 = (struct sockaddr_in *)paddrs + cnt;
1931 			IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
1932 			sin4->sin_port = sctp->sctp_fport;
1933 			sin4->sin_family = AF_INET;
1934 			break;
1935 		case AF_INET6:
1936 			sin6 = (struct sockaddr_in6 *)paddrs + cnt;
1937 			sin6->sin6_addr = addr;
1938 			sin6->sin6_port = sctp->sctp_fport;
1939 			sin6->sin6_family = AF_INET6;
1940 			break;
1941 		}
1942 	}
1943 	*addrcnt = cnt;
1944 	return (0);
1945 }
1946