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