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 				sctp->sctp_pd_point = sctp->sctp_rwnd;
1230 			}
1231 			/*
1232 			 * XXX should we return the rwnd here
1233 			 * and sctp_opt_get ?
1234 			 */
1235 			break;
1236 		case SO_ALLZONES:
1237 			if (secpolicy_ip(sctp->sctp_credp, OP_CONFIG,
1238 			    B_TRUE)) {
1239 				retval = EACCES;
1240 				break;
1241 			}
1242 			if (sctp->sctp_state >= SCTPS_BOUND) {
1243 				retval = EINVAL;
1244 				break;
1245 			}
1246 			sctp->sctp_allzones = onoff;
1247 			break;
1248 		case SO_MAC_EXEMPT:
1249 			if (secpolicy_net_mac_aware(sctp->sctp_credp) != 0) {
1250 				retval = EACCES;
1251 				break;
1252 			}
1253 			if (sctp->sctp_state >= SCTPS_BOUND) {
1254 				retval = EINVAL;
1255 				break;
1256 			}
1257 			connp->conn_mac_exempt = onoff;
1258 			break;
1259 		default:
1260 			retval = ENOPROTOOPT;
1261 			break;
1262 		}
1263 		break;
1264 
1265 	case IPPROTO_SCTP:
1266 		if (inlen < sizeof (int32_t)) {
1267 			retval = EINVAL;
1268 			break;
1269 		}
1270 		switch (name) {
1271 		case SCTP_RTOINFO:
1272 			retval = sctp_set_rtoinfo(sctp, invalp, inlen);
1273 			break;
1274 		case SCTP_ASSOCINFO:
1275 			retval = sctp_set_assocparams(sctp, invalp, inlen);
1276 			break;
1277 		case SCTP_INITMSG:
1278 			retval = sctp_set_initmsg(sctp, invalp, inlen);
1279 			break;
1280 		case SCTP_NODELAY:
1281 			sctp->sctp_ndelay = ONOFF(*i1);
1282 			break;
1283 		case SCTP_AUTOCLOSE:
1284 			if (SEC_TO_TICK(*i1) < 0) {
1285 				retval = EINVAL;
1286 				break;
1287 			}
1288 			/* Convert the number of seconds to ticks. */
1289 			sctp->sctp_autoclose = SEC_TO_TICK(*i1);
1290 			sctp_heartbeat_timer(sctp);
1291 			break;
1292 		case SCTP_SET_PEER_PRIMARY_ADDR:
1293 			retval = sctp_set_peerprim(sctp, invalp, inlen);
1294 			break;
1295 		case SCTP_PRIMARY_ADDR:
1296 			retval = sctp_set_prim(sctp, invalp, inlen);
1297 			break;
1298 		case SCTP_ADAPTION_LAYER: {
1299 			struct sctp_setadaption *ssb;
1300 
1301 			if (inlen < sizeof (struct sctp_setadaption)) {
1302 				retval = EINVAL;
1303 				break;
1304 			}
1305 			ssb = (struct sctp_setadaption *)invalp;
1306 			sctp->sctp_send_adaption = 1;
1307 			sctp->sctp_tx_adaption_code = ssb->ssb_adaption_ind;
1308 			break;
1309 		}
1310 		case SCTP_PEER_ADDR_PARAMS:
1311 			retval = sctp_set_peer_addr_params(sctp, invalp,
1312 			    inlen);
1313 			break;
1314 		case SCTP_DEFAULT_SEND_PARAM:
1315 			retval = sctp_set_def_send_params(sctp, invalp, inlen);
1316 			break;
1317 		case SCTP_EVENTS: {
1318 			struct sctp_event_subscribe *ev;
1319 
1320 			if (inlen < sizeof (struct sctp_event_subscribe)) {
1321 				retval = EINVAL;
1322 				break;
1323 			}
1324 			ev = (struct sctp_event_subscribe *)invalp;
1325 			sctp->sctp_recvsndrcvinfo =
1326 			    ONOFF(ev->sctp_data_io_event);
1327 			sctp->sctp_recvassocevnt =
1328 			    ONOFF(ev->sctp_association_event);
1329 			sctp->sctp_recvpathevnt =
1330 			    ONOFF(ev->sctp_address_event);
1331 			sctp->sctp_recvsendfailevnt =
1332 			    ONOFF(ev->sctp_send_failure_event);
1333 			sctp->sctp_recvpeererr =
1334 			    ONOFF(ev->sctp_peer_error_event);
1335 			sctp->sctp_recvshutdownevnt =
1336 			    ONOFF(ev->sctp_shutdown_event);
1337 			sctp->sctp_recvpdevnt =
1338 			    ONOFF(ev->sctp_partial_delivery_event);
1339 			sctp->sctp_recvalevnt =
1340 				ONOFF(ev->sctp_adaption_layer_event);
1341 			break;
1342 		}
1343 		case SCTP_ADD_ADDR:
1344 		case SCTP_REM_ADDR:
1345 			/*
1346 			 * The sctp_t has to be bound first before
1347 			 * the address list can be changed.
1348 			 */
1349 			if (sctp->sctp_state < SCTPS_BOUND) {
1350 				retval = EINVAL;
1351 				break;
1352 			}
1353 			if (sctp->sctp_family == AF_INET) {
1354 				addrcnt = inlen / sizeof (struct sockaddr_in);
1355 			} else {
1356 				ASSERT(sctp->sctp_family == AF_INET6);
1357 				addrcnt = inlen / sizeof (struct sockaddr_in6);
1358 			}
1359 			if (name == SCTP_ADD_ADDR) {
1360 				retval = sctp_bind_add(sctp, invalp, addrcnt,
1361 				    B_TRUE, sctp->sctp_lport);
1362 			} else {
1363 				retval = sctp_bind_del(sctp, invalp, addrcnt,
1364 				    B_TRUE);
1365 			}
1366 			break;
1367 		case SCTP_UC_SWAP: {
1368 			struct sctp_uc_swap *us;
1369 
1370 			/*
1371 			 * Change handle & upcalls.
1372 			 */
1373 			if (inlen < sizeof (*us)) {
1374 				retval = EINVAL;
1375 				break;
1376 			}
1377 			us = (struct sctp_uc_swap *)invalp;
1378 			sctp->sctp_ulpd = us->sus_handle;
1379 			bcopy(us->sus_upcalls, &sctp->sctp_upcalls,
1380 			    sizeof (sctp_upcalls_t));
1381 			break;
1382 		}
1383 		case SCTP_PRSCTP:
1384 			sctp->sctp_prsctp_aware = onoff;
1385 			break;
1386 		case SCTP_I_WANT_MAPPED_V4_ADDR:
1387 		case SCTP_MAXSEG:
1388 		case SCTP_DISABLE_FRAGMENTS:
1389 			/* Not yet supported. */
1390 		default:
1391 			retval = ENOPROTOOPT;
1392 			break;
1393 		}
1394 		break;
1395 
1396 	case IPPROTO_IP:
1397 		if (sctp->sctp_family != AF_INET) {
1398 			retval = ENOPROTOOPT;
1399 			break;
1400 		}
1401 		if ((name != IP_OPTIONS) && (inlen < sizeof (int32_t))) {
1402 			retval = EINVAL;
1403 			break;
1404 		}
1405 		switch (name) {
1406 		case IP_OPTIONS:
1407 		case T_IP_OPTIONS:
1408 			retval = sctp_opt_set_header(sctp, invalp, inlen);
1409 			break;
1410 		case IP_TOS:
1411 		case T_IP_TOS:
1412 			sctp->sctp_ipha->ipha_type_of_service = (uchar_t)*i1;
1413 			break;
1414 		case IP_TTL:
1415 			sctp->sctp_ipha->ipha_ttl = (uchar_t)*i1;
1416 			break;
1417 		case IP_SEC_OPT:
1418 			/*
1419 			 * We should not allow policy setting after
1420 			 * we start listening for connections.
1421 			 */
1422 			if (sctp->sctp_state >= SCTPS_LISTEN) {
1423 				retval = EINVAL;
1424 			} else {
1425 				retval = ipsec_set_req(sctp->sctp_credp,
1426 				    sctp->sctp_connp, (ipsec_req_t *)invalp);
1427 			}
1428 			break;
1429 		/* IP level options */
1430 		case IP_RECVIF:
1431 			connp->conn_recvif = onoff;
1432 			break;
1433 		case IP_RECVSLLA:
1434 			connp->conn_recvslla = onoff;
1435 			break;
1436 		case IP_UNSPEC_SRC:
1437 			connp->conn_unspec_src = onoff;
1438 			break;
1439 		case IP_NEXTHOP: {
1440 			ipaddr_t addr = *i1;
1441 			ipif_t *ipif = NULL;
1442 			ill_t *ill;
1443 			ip_stack_t *ipst = sctps->sctps_netstack->netstack_ip;
1444 
1445 			if (secpolicy_ip(sctp->sctp_credp, OP_CONFIG,
1446 			    B_TRUE) == 0) {
1447 				ipif =
1448 				    ipif_lookup_onlink_addr(addr,
1449 					connp->conn_zoneid, ipst);
1450 				if (ipif == NULL) {
1451 					retval = EHOSTUNREACH;
1452 					break;
1453 				}
1454 				ill = ipif->ipif_ill;
1455 				mutex_enter(&ill->ill_lock);
1456 				if ((ill->ill_state_flags & ILL_CONDEMNED) ||
1457 				    (ipif->ipif_state_flags & IPIF_CONDEMNED)) {
1458 					mutex_exit(&ill->ill_lock);
1459 					ipif_refrele(ipif);
1460 					retval =  EHOSTUNREACH;
1461 					break;
1462 				}
1463 				mutex_exit(&ill->ill_lock);
1464 				ipif_refrele(ipif);
1465 				mutex_enter(&connp->conn_lock);
1466 				connp->conn_nexthop_v4 = addr;
1467 				connp->conn_nexthop_set = B_TRUE;
1468 				mutex_exit(&connp->conn_lock);
1469 			}
1470 			break;
1471 		}
1472 		default:
1473 			retval = ENOPROTOOPT;
1474 			break;
1475 		}
1476 		break;
1477 	case IPPROTO_IPV6: {
1478 		if (sctp->sctp_family != AF_INET6) {
1479 			retval = ENOPROTOOPT;
1480 			break;
1481 		}
1482 
1483 		switch (name) {
1484 		case IPV6_UNICAST_HOPS:
1485 			if (inlen < sizeof (int32_t)) {
1486 				retval = EINVAL;
1487 				break;
1488 			}
1489 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
1490 				retval = EINVAL;
1491 				break;
1492 			}
1493 			if (*i1 == -1) {
1494 				ipp->ipp_unicast_hops =
1495 				    sctps->sctps_ipv6_hoplimit;
1496 				ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
1497 			} else {
1498 				ipp->ipp_unicast_hops = (uint8_t)*i1;
1499 				ipp->ipp_fields |= IPPF_UNICAST_HOPS;
1500 			}
1501 			retval = sctp_build_hdrs(sctp);
1502 			break;
1503 		case IPV6_UNSPEC_SRC:
1504 			if (inlen < sizeof (int32_t)) {
1505 				retval = EINVAL;
1506 				break;
1507 			}
1508 			connp->conn_unspec_src = onoff;
1509 			break;
1510 		case IPV6_RECVPKTINFO:
1511 			if (inlen < sizeof (int32_t)) {
1512 				retval = EINVAL;
1513 				break;
1514 			}
1515 			if (onoff)
1516 				sctp->sctp_ipv6_recvancillary |=
1517 				    SCTP_IPV6_RECVPKTINFO;
1518 			else
1519 				sctp->sctp_ipv6_recvancillary &=
1520 				    ~SCTP_IPV6_RECVPKTINFO;
1521 			/* Send it with the next msg */
1522 			sctp->sctp_recvifindex = 0;
1523 			connp->conn_ip_recvpktinfo = onoff;
1524 			break;
1525 		case IPV6_RECVHOPLIMIT:
1526 			if (inlen < sizeof (int32_t)) {
1527 				retval = EINVAL;
1528 				break;
1529 			}
1530 			if (onoff)
1531 				sctp->sctp_ipv6_recvancillary |=
1532 				    SCTP_IPV6_RECVHOPLIMIT;
1533 			else
1534 				sctp->sctp_ipv6_recvancillary &=
1535 				    ~SCTP_IPV6_RECVHOPLIMIT;
1536 			sctp->sctp_recvhops = 0xffffffffU;
1537 			connp->conn_ipv6_recvhoplimit = onoff;
1538 			break;
1539 		case IPV6_RECVHOPOPTS:
1540 			if (inlen < sizeof (int32_t)) {
1541 				retval = EINVAL;
1542 				break;
1543 			}
1544 			if (onoff)
1545 				sctp->sctp_ipv6_recvancillary |=
1546 				    SCTP_IPV6_RECVHOPOPTS;
1547 			else
1548 				sctp->sctp_ipv6_recvancillary &=
1549 				    ~SCTP_IPV6_RECVHOPOPTS;
1550 			connp->conn_ipv6_recvhopopts = onoff;
1551 			break;
1552 		case IPV6_RECVDSTOPTS:
1553 			if (inlen < sizeof (int32_t)) {
1554 				retval = EINVAL;
1555 				break;
1556 			}
1557 			if (onoff)
1558 				sctp->sctp_ipv6_recvancillary |=
1559 				    SCTP_IPV6_RECVDSTOPTS;
1560 			else
1561 				sctp->sctp_ipv6_recvancillary &=
1562 				    ~SCTP_IPV6_RECVDSTOPTS;
1563 			connp->conn_ipv6_recvdstopts = onoff;
1564 			break;
1565 		case IPV6_RECVRTHDR:
1566 			if (inlen < sizeof (int32_t)) {
1567 				retval = EINVAL;
1568 				break;
1569 			}
1570 			if (onoff)
1571 				sctp->sctp_ipv6_recvancillary |=
1572 				    SCTP_IPV6_RECVRTHDR;
1573 			else
1574 				sctp->sctp_ipv6_recvancillary &=
1575 				    ~SCTP_IPV6_RECVRTHDR;
1576 			connp->conn_ipv6_recvrthdr = onoff;
1577 			break;
1578 		case IPV6_RECVRTHDRDSTOPTS:
1579 			if (inlen < sizeof (int32_t)) {
1580 				retval = EINVAL;
1581 				break;
1582 			}
1583 			if (onoff)
1584 				sctp->sctp_ipv6_recvancillary |=
1585 				    SCTP_IPV6_RECVRTDSTOPTS;
1586 			else
1587 				sctp->sctp_ipv6_recvancillary &=
1588 				    ~SCTP_IPV6_RECVRTDSTOPTS;
1589 			connp->conn_ipv6_recvrtdstopts = onoff;
1590 			break;
1591 		case IPV6_PKTINFO:
1592 			if (inlen != 0 &&
1593 			    inlen != sizeof (struct in6_pktinfo)) {
1594 				retval = EINVAL;
1595 				break;
1596 			}
1597 
1598 			if (inlen == 0) {
1599 				ipp->ipp_fields &= ~(IPPF_IFINDEX |IPPF_ADDR);
1600 			} else  {
1601 				struct in6_pktinfo *pkti;
1602 
1603 				pkti = (struct in6_pktinfo *)invalp;
1604 				/* XXX Need to check if the index exists */
1605 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
1606 				ipp->ipp_addr = pkti->ipi6_addr;
1607 				if (ipp->ipp_ifindex != 0)
1608 					ipp->ipp_fields |= IPPF_IFINDEX;
1609 				else
1610 					ipp->ipp_fields &= ~IPPF_IFINDEX;
1611 				if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr))
1612 					ipp->ipp_fields |= IPPF_ADDR;
1613 				else
1614 					ipp->ipp_fields &= ~IPPF_ADDR;
1615 			}
1616 			retval = sctp_build_hdrs(sctp);
1617 			break;
1618 		case IPV6_NEXTHOP: {
1619 			struct sockaddr_in6 *sin6;
1620 			ip_stack_t *ipst = sctps->sctps_netstack->netstack_ip;
1621 
1622 			if (inlen != 0 && inlen != sizeof (sin6_t)) {
1623 				retval = EINVAL;
1624 				break;
1625 			}
1626 
1627 			if (inlen == 0) {
1628 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
1629 			} else {
1630 				sin6 = (struct sockaddr_in6 *)invalp;
1631 				if (sin6->sin6_family != AF_INET6) {
1632 					retval = EAFNOSUPPORT;
1633 					break;
1634 				}
1635 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1636 					retval = EADDRNOTAVAIL;
1637 					break;
1638 				}
1639 				ipp->ipp_nexthop = sin6->sin6_addr;
1640 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1641 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
1642 				} else {
1643 					ire_t	*ire;
1644 
1645 					ire = ire_route_lookup_v6(
1646 					    &sin6->sin6_addr, NULL, NULL, 0,
1647 					    NULL, NULL, ALL_ZONES, NULL,
1648 					    MATCH_IRE_DEFAULT, ipst);
1649 					if (ire == NULL) {
1650 						retval = EHOSTUNREACH;
1651 						break;
1652 					}
1653 					ire_refrele(ire);
1654 					ipp->ipp_fields |= IPPF_NEXTHOP;
1655 				}
1656 			}
1657 			retval = sctp_build_hdrs(sctp);
1658 			break;
1659 		}
1660 		case IPV6_HOPOPTS: {
1661 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
1662 
1663 			if (inlen != 0 &&
1664 			    inlen != (8 * (hopts->ip6h_len + 1))) {
1665 				retval = EINVAL;
1666 				break;
1667 			}
1668 
1669 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1670 			    B_TRUE, (uchar_t **)&ipp->ipp_hopopts,
1671 			    &ipp->ipp_hopoptslen, sctp->sctp_v6label_len);
1672 			if (retval != 0)
1673 				break;
1674 			if (ipp->ipp_hopoptslen == 0)
1675 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
1676 			else
1677 				ipp->ipp_fields |= IPPF_HOPOPTS;
1678 			retval = sctp_build_hdrs(sctp);
1679 			break;
1680 		}
1681 		case IPV6_RTHDRDSTOPTS: {
1682 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
1683 
1684 			if (inlen != 0 &&
1685 			    inlen != (8 * (dopts->ip6d_len + 1))) {
1686 				retval = EINVAL;
1687 				break;
1688 			}
1689 
1690 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1691 			    B_TRUE, (uchar_t **)&ipp->ipp_rtdstopts,
1692 			    &ipp->ipp_rtdstoptslen, 0);
1693 			if (retval != 0)
1694 				break;
1695 			if (ipp->ipp_rtdstoptslen == 0)
1696 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
1697 			else
1698 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
1699 			retval = sctp_build_hdrs(sctp);
1700 			break;
1701 		}
1702 		case IPV6_DSTOPTS: {
1703 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
1704 
1705 			if (inlen != 0 &&
1706 			    inlen != (8 * (dopts->ip6d_len + 1))) {
1707 				retval = EINVAL;
1708 				break;
1709 			}
1710 
1711 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1712 			    B_TRUE, (uchar_t **)&ipp->ipp_dstopts,
1713 			    &ipp->ipp_dstoptslen, 0);
1714 			if (retval != 0)
1715 				break;
1716 			if (ipp->ipp_dstoptslen == 0)
1717 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
1718 			else
1719 				ipp->ipp_fields |= IPPF_DSTOPTS;
1720 			retval = sctp_build_hdrs(sctp);
1721 			break;
1722 		}
1723 		case IPV6_RTHDR: {
1724 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
1725 
1726 			if (inlen != 0 &&
1727 			    inlen != (8 * (rt->ip6r_len + 1))) {
1728 				retval = EINVAL;
1729 				break;
1730 			}
1731 
1732 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1733 			    B_TRUE, (uchar_t **)&ipp->ipp_rthdr,
1734 			    &ipp->ipp_rthdrlen, 0);
1735 			if (retval != 0)
1736 				break;
1737 			if (ipp->ipp_rthdrlen == 0)
1738 				ipp->ipp_fields &= ~IPPF_RTHDR;
1739 			else
1740 				ipp->ipp_fields |= IPPF_RTHDR;
1741 			retval = sctp_build_hdrs(sctp);
1742 			break;
1743 		}
1744 		case IPV6_SEC_OPT:
1745 			/*
1746 			 * We should not allow policy setting after
1747 			 * we start listening for connections.
1748 			 */
1749 			if (sctp->sctp_state >= SCTPS_LISTEN) {
1750 				retval = EINVAL;
1751 			} else {
1752 				retval = ipsec_set_req(sctp->sctp_credp,
1753 				    sctp->sctp_connp, (ipsec_req_t *)invalp);
1754 			}
1755 			break;
1756 		case IPV6_V6ONLY:
1757 			/*
1758 			 * After the bound state, setting the v6only option
1759 			 * is too late.
1760 			 */
1761 			if (sctp->sctp_state >= SCTPS_BOUND) {
1762 				retval = EINVAL;
1763 			} else {
1764 				sctp->sctp_connp->conn_ipv6_v6only = onoff;
1765 			}
1766 			break;
1767 		default:
1768 			retval = ENOPROTOOPT;
1769 			break;
1770 		}
1771 		break;
1772 	}
1773 	default:
1774 		retval = ENOPROTOOPT;
1775 		break;
1776 	}
1777 
1778 	WAKE_SCTP(sctp);
1779 	return (retval);
1780 }
1781 
1782 /*
1783  * SCTP exported kernel interface for geting the first source address of
1784  * a sctp_t.  The parameter addr is assumed to have enough space to hold
1785  * one socket address.
1786  */
1787 int
1788 sctp_getsockname(sctp_t *sctp, struct sockaddr *addr, socklen_t *addrlen)
1789 {
1790 	int	err = 0;
1791 	int	addrcnt = 1;
1792 	sin_t	*sin4;
1793 	sin6_t	*sin6;
1794 
1795 	ASSERT(sctp != NULL);
1796 
1797 	RUN_SCTP(sctp);
1798 	addr->sa_family = sctp->sctp_family;
1799 	switch (sctp->sctp_family) {
1800 	case AF_INET:
1801 		sin4 = (sin_t *)addr;
1802 		if ((sctp->sctp_state <= SCTPS_LISTEN) &&
1803 		    sctp->sctp_bound_to_all) {
1804 			sin4->sin_addr.s_addr = INADDR_ANY;
1805 			sin4->sin_port = sctp->sctp_lport;
1806 		} else {
1807 			err = sctp_getmyaddrs(sctp, sin4, &addrcnt);
1808 			if (err != 0) {
1809 				*addrlen = 0;
1810 				break;
1811 			}
1812 		}
1813 		*addrlen = sizeof (struct sockaddr_in);
1814 		break;
1815 	case AF_INET6:
1816 		sin6 = (sin6_t *)addr;
1817 		if ((sctp->sctp_state <= SCTPS_LISTEN) &&
1818 		    sctp->sctp_bound_to_all) {
1819 			bzero(&sin6->sin6_addr, sizeof (sin6->sin6_addr));
1820 			sin6->sin6_port = sctp->sctp_lport;
1821 		} else {
1822 			err = sctp_getmyaddrs(sctp, sin6, &addrcnt);
1823 			if (err != 0) {
1824 				*addrlen = 0;
1825 				break;
1826 			}
1827 		}
1828 		*addrlen = sizeof (struct sockaddr_in6);
1829 		sin6->sin6_flowinfo = sctp->sctp_ip6h->ip6_vcf &
1830 		    ~IPV6_VERS_AND_FLOW_MASK;
1831 		sin6->sin6_scope_id = 0;
1832 		sin6->__sin6_src_id = 0;
1833 		break;
1834 	}
1835 	WAKE_SCTP(sctp);
1836 	return (err);
1837 }
1838 
1839 /*
1840  * SCTP exported kernel interface for geting the primary peer address of
1841  * a sctp_t.  The parameter addr is assumed to have enough space to hold
1842  * one socket address.
1843  */
1844 int
1845 sctp_getpeername(sctp_t *sctp, struct sockaddr *addr, socklen_t *addrlen)
1846 {
1847 	int	err = 0;
1848 	int	addrcnt = 1;
1849 	sin6_t	*sin6;
1850 
1851 	ASSERT(sctp != NULL);
1852 
1853 	RUN_SCTP(sctp);
1854 	addr->sa_family = sctp->sctp_family;
1855 	switch (sctp->sctp_family) {
1856 	case AF_INET:
1857 		err = sctp_getpeeraddrs(sctp, addr, &addrcnt);
1858 		if (err != 0) {
1859 			*addrlen = 0;
1860 			break;
1861 		}
1862 		*addrlen = sizeof (struct sockaddr_in);
1863 		break;
1864 	case AF_INET6:
1865 		sin6 = (sin6_t *)addr;
1866 		err = sctp_getpeeraddrs(sctp, sin6, &addrcnt);
1867 		if (err != 0) {
1868 			*addrlen = 0;
1869 			break;
1870 		}
1871 		*addrlen = sizeof (struct sockaddr_in6);
1872 		sin6->sin6_flowinfo = 0;
1873 		sin6->sin6_scope_id = 0;
1874 		sin6->__sin6_src_id = 0;
1875 		break;
1876 	}
1877 	WAKE_SCTP(sctp);
1878 	return (err);
1879 }
1880 
1881 /*
1882  * Return a list of IP addresses of the peer endpoint of this sctp_t.
1883  * The parameter paddrs is supposed to be either (struct sockaddr_in *) or
1884  * (struct sockaddr_in6 *) depending on the address family of the sctp_t.
1885  */
1886 int
1887 sctp_getpeeraddrs(sctp_t *sctp, void *paddrs, int *addrcnt)
1888 {
1889 	int			family;
1890 	struct sockaddr_in	*sin4;
1891 	struct sockaddr_in6	*sin6;
1892 	int			max;
1893 	int			cnt;
1894 	sctp_faddr_t		*fp = sctp->sctp_faddrs;
1895 	in6_addr_t		addr;
1896 
1897 	ASSERT(sctp != NULL);
1898 
1899 	if (sctp->sctp_faddrs == NULL)
1900 		return (ENOTCONN);
1901 
1902 	family = sctp->sctp_family;
1903 	max = *addrcnt;
1904 
1905 	/* If we want only one, give the primary */
1906 	if (max == 1) {
1907 		addr = sctp->sctp_primary->faddr;
1908 		switch (family) {
1909 		case AF_INET:
1910 			sin4 = paddrs;
1911 			IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
1912 			sin4->sin_port = sctp->sctp_fport;
1913 			sin4->sin_family = AF_INET;
1914 			break;
1915 
1916 		case AF_INET6:
1917 			sin6 = paddrs;
1918 			sin6->sin6_addr = addr;
1919 			sin6->sin6_port = sctp->sctp_fport;
1920 			sin6->sin6_family = AF_INET6;
1921 			break;
1922 		}
1923 		return (0);
1924 	}
1925 
1926 	for (cnt = 0; cnt < max && fp != NULL; cnt++, fp = fp->next) {
1927 		addr = fp->faddr;
1928 		switch (family) {
1929 		case AF_INET:
1930 			ASSERT(IN6_IS_ADDR_V4MAPPED(&addr));
1931 			sin4 = (struct sockaddr_in *)paddrs + cnt;
1932 			IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
1933 			sin4->sin_port = sctp->sctp_fport;
1934 			sin4->sin_family = AF_INET;
1935 			break;
1936 		case AF_INET6:
1937 			sin6 = (struct sockaddr_in6 *)paddrs + cnt;
1938 			sin6->sin6_addr = addr;
1939 			sin6->sin6_port = sctp->sctp_fport;
1940 			sin6->sin6_family = AF_INET6;
1941 			break;
1942 		}
1943 	}
1944 	*addrcnt = cnt;
1945 	return (0);
1946 }
1947