xref: /illumos-gate/usr/src/uts/common/inet/sctp/sctp_opt_data.c (revision cbab2b2687744cbfdc12fae90f8088127a0b266c)
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 = sctp->sctp_dontroute ? SO_DONTROUTE : 0;
705 			break;
706 		case SO_USELOOPBACK:
707 			*i1 = sctp->sctp_useloopback ? SO_USELOOPBACK : 0;
708 			break;
709 		case SO_BROADCAST:
710 			*i1 = sctp->sctp_broadcast ? SO_BROADCAST : 0;
711 			break;
712 		case SO_REUSEADDR:
713 			*i1 = sctp->sctp_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_MAC_EXEMPT:
725 			*i1 = sctp->sctp_mac_exempt ? SO_MAC_EXEMPT : 0;
726 			break;
727 		default:
728 			retval = EINVAL;
729 			break;
730 		}
731 		break;
732 
733 	case IPPROTO_SCTP:
734 		switch (name) {
735 		case SCTP_RTOINFO:
736 			if (buflen < sizeof (struct sctp_rtoinfo)) {
737 				retval = EINVAL;
738 				break;
739 			}
740 			*optlen = sctp_get_rtoinfo(sctp, ptr);
741 			break;
742 		case SCTP_ASSOCINFO:
743 			if (buflen < sizeof (struct sctp_assocparams)) {
744 				retval = EINVAL;
745 				break;
746 			}
747 			*optlen = sctp_get_assocparams(sctp, ptr);
748 			break;
749 		case SCTP_INITMSG:
750 			if (buflen < sizeof (struct sctp_initmsg)) {
751 				retval = EINVAL;
752 				break;
753 			}
754 			*optlen = sctp_get_initmsg(sctp, ptr);
755 			break;
756 		case SCTP_NODELAY:
757 			*i1 = sctp->sctp_ndelay;
758 			break;
759 		case SCTP_AUTOCLOSE:
760 			*i1 = TICK_TO_SEC(sctp->sctp_autoclose);
761 			break;
762 		case SCTP_ADAPTION_LAYER:
763 			if (buflen < sizeof (struct sctp_setadaption)) {
764 				retval = EINVAL;
765 				break;
766 			}
767 			((struct sctp_setadaption *)ptr)->ssb_adaption_ind =
768 			    sctp->sctp_tx_adaption_code;
769 			break;
770 		case SCTP_PEER_ADDR_PARAMS:
771 			if (buflen < sizeof (struct sctp_paddrparams)) {
772 				retval = EINVAL;
773 				break;
774 			}
775 			*optlen = sctp_get_peer_addr_params(sctp, ptr);
776 			break;
777 		case SCTP_DEFAULT_SEND_PARAM:
778 			if (buflen < sizeof (struct sctp_sndrcvinfo)) {
779 				retval = EINVAL;
780 				break;
781 			}
782 			*optlen = sctp_get_def_send_params(sctp, ptr);
783 			break;
784 		case SCTP_EVENTS: {
785 			struct sctp_event_subscribe *ev;
786 
787 			if (buflen < sizeof (struct sctp_event_subscribe)) {
788 				retval = EINVAL;
789 				break;
790 			}
791 			ev = (struct sctp_event_subscribe *)ptr;
792 			ev->sctp_data_io_event =
793 			    ONOFF(sctp->sctp_recvsndrcvinfo);
794 			ev->sctp_association_event =
795 			    ONOFF(sctp->sctp_recvassocevnt);
796 			ev->sctp_address_event =
797 			    ONOFF(sctp->sctp_recvpathevnt);
798 			ev->sctp_send_failure_event =
799 			    ONOFF(sctp->sctp_recvsendfailevnt);
800 			ev->sctp_peer_error_event =
801 			    ONOFF(sctp->sctp_recvpeererr);
802 			ev->sctp_shutdown_event =
803 			    ONOFF(sctp->sctp_recvshutdownevnt);
804 			ev->sctp_partial_delivery_event =
805 			    ONOFF(sctp->sctp_recvpdevnt);
806 			ev->sctp_adaption_layer_event =
807 				ONOFF(sctp->sctp_recvalevnt);
808 			*optlen = sizeof (struct sctp_event_subscribe);
809 			break;
810 		}
811 		case SCTP_STATUS:
812 			if (buflen < sizeof (struct sctp_status)) {
813 				retval = EINVAL;
814 				break;
815 			}
816 			*optlen = sctp_get_status(sctp, ptr);
817 			break;
818 		case SCTP_GET_PEER_ADDR_INFO:
819 			if (buflen < sizeof (struct sctp_paddrinfo)) {
820 				retval = EINVAL;
821 				break;
822 			}
823 			retval = sctp_get_paddrinfo(sctp, ptr, optlen);
824 			break;
825 		case SCTP_GET_NLADDRS:
826 			*(int32_t *)ptr = sctp->sctp_nsaddrs;
827 			break;
828 		case SCTP_GET_LADDRS: {
829 			int addr_cnt;
830 			int addr_size;
831 
832 			if (sctp->sctp_family == AF_INET)
833 				addr_size = sizeof (struct sockaddr_in);
834 			else
835 				addr_size = sizeof (struct sockaddr_in6);
836 			addr_cnt = buflen / addr_size;
837 			retval = sctp_getmyaddrs(sctp, ptr, &addr_cnt);
838 			if (retval == 0)
839 				*optlen = addr_cnt * addr_size;
840 			break;
841 		}
842 		case SCTP_GET_NPADDRS: {
843 			int i;
844 			sctp_faddr_t *fp;
845 
846 			for (i = 0, fp = sctp->sctp_faddrs; fp != NULL;
847 			    i++, fp = fp->next)
848 				;
849 			*(int32_t *)ptr = i;
850 			break;
851 		}
852 		case SCTP_GET_PADDRS: {
853 			int addr_cnt;
854 			int addr_size;
855 
856 			if (sctp->sctp_family == AF_INET)
857 				addr_size = sizeof (struct sockaddr_in);
858 			else
859 				addr_size = sizeof (struct sockaddr_in6);
860 			addr_cnt = buflen / addr_size;
861 			retval = sctp_getpeeraddrs(sctp, ptr, &addr_cnt);
862 			if (retval == 0)
863 				*optlen = addr_cnt * addr_size;
864 			break;
865 		}
866 		case SCTP_PRSCTP:
867 			*i1 = sctp->sctp_prsctp_aware ? 1 : 0;
868 			break;
869 		case SCTP_I_WANT_MAPPED_V4_ADDR:
870 		case SCTP_MAXSEG:
871 		case SCTP_DISABLE_FRAGMENTS:
872 			/* Not yet supported. */
873 		default:
874 			retval = EINVAL;
875 			break;
876 		}
877 		break;
878 
879 	case IPPROTO_IP:
880 		if (sctp->sctp_family != AF_INET) {
881 			retval = EINVAL;
882 			break;
883 		}
884 		switch (name) {
885 		case IP_OPTIONS:
886 		case T_IP_OPTIONS: {
887 			/*
888 			 * This is compatible with BSD in that in only return
889 			 * the reverse source route with the final destination
890 			 * as the last entry. The first 4 bytes of the option
891 			 * will contain the final destination. Allocate a
892 			 * buffer large enough to hold all the options, we
893 			 * add IP_ADDR_LEN to SCTP_MAX_IP_OPTIONS_LENGTH since
894 			 * ip_opt_get_user() adds the final destination
895 			 * at the start.
896 			 */
897 			char	*opt_ptr;
898 			int	opt_len;
899 			uchar_t	obuf[SCTP_MAX_IP_OPTIONS_LENGTH + IP_ADDR_LEN];
900 
901 			opt_ptr = (char *)sctp->sctp_ipha +
902 			    IP_SIMPLE_HDR_LENGTH;
903 			opt_len = (char *)sctp->sctp_sctph - opt_ptr;
904 			/* Caller ensures enough space */
905 			if (opt_len > 0) {
906 				/*
907 				 * TODO: Do we have to handle getsockopt on an
908 				 * initiator as well?
909 				 */
910 				opt_len = ip_opt_get_user(sctp->sctp_ipha,
911 				    obuf);
912 				ASSERT(opt_len <= sizeof (obuf));
913 			} else {
914 				opt_len = 0;
915 			}
916 			if (buflen < opt_len) {
917 				/* Silently truncate */
918 				opt_len = buflen;
919 			}
920 			*optlen = opt_len;
921 			bcopy(obuf, ptr, opt_len);
922 			break;
923 		}
924 		case IP_TOS:
925 		case T_IP_TOS:
926 			*i1 = (int)sctp->sctp_ipha->ipha_type_of_service;
927 			break;
928 		case IP_TTL:
929 			*i1 = (int)sctp->sctp_ipha->ipha_ttl;
930 			break;
931 		case IP_NEXTHOP:
932 			if (connp->conn_nexthop_set) {
933 				*(ipaddr_t *)ptr = connp->conn_nexthop_v4;
934 				*optlen = sizeof (ipaddr_t);
935 			} else {
936 				*optlen = 0;
937 			}
938 			break;
939 		default:
940 			retval = EINVAL;
941 			break;
942 		}
943 		break;
944 	case IPPROTO_IPV6:
945 		if (sctp->sctp_family != AF_INET6) {
946 			retval = EINVAL;
947 			break;
948 		}
949 		switch (name) {
950 		case IPV6_UNICAST_HOPS:
951 			*i1 = (unsigned int) sctp->sctp_ip6h->ip6_hops;
952 			break;	/* goto sizeof (int) option return */
953 		case IPV6_RECVPKTINFO:
954 			if (sctp->sctp_ipv6_recvancillary &
955 			    SCTP_IPV6_RECVPKTINFO) {
956 				*i1 = 1;
957 			} else {
958 				*i1 = 0;
959 			}
960 			break;	/* goto sizeof (int) option return */
961 		case IPV6_RECVHOPLIMIT:
962 			if (sctp->sctp_ipv6_recvancillary &
963 			    SCTP_IPV6_RECVHOPLIMIT) {
964 				*i1 = 1;
965 			} else {
966 				*i1 = 0;
967 			}
968 			break;	/* goto sizeof (int) option return */
969 		case IPV6_RECVHOPOPTS:
970 			if (sctp->sctp_ipv6_recvancillary &
971 			    SCTP_IPV6_RECVHOPOPTS) {
972 				*i1 = 1;
973 			} else {
974 				*i1 = 0;
975 			}
976 			break;	/* goto sizeof (int) option return */
977 		case IPV6_RECVDSTOPTS:
978 			if (sctp->sctp_ipv6_recvancillary &
979 			    SCTP_IPV6_RECVDSTOPTS) {
980 				*i1 = 1;
981 			} else {
982 				*i1 = 0;
983 			}
984 			break;	/* goto sizeof (int) option return */
985 		case IPV6_RECVRTHDR:
986 			if (sctp->sctp_ipv6_recvancillary &
987 			    SCTP_IPV6_RECVRTHDR) {
988 				*i1 = 1;
989 			} else {
990 				*i1 = 0;
991 			}
992 			break;	/* goto sizeof (int) option return */
993 		case IPV6_RECVRTHDRDSTOPTS:
994 			if (sctp->sctp_ipv6_recvancillary &
995 			    SCTP_IPV6_RECVRTDSTOPTS) {
996 				*i1 = 1;
997 			} else {
998 				*i1 = 0;
999 			}
1000 			break;	/* goto sizeof (int) option return */
1001 		case IPV6_PKTINFO: {
1002 			struct in6_pktinfo *pkti;
1003 
1004 			if (buflen < sizeof (struct in6_pktinfo)) {
1005 				retval = EINVAL;
1006 				break;
1007 			}
1008 			pkti = (struct in6_pktinfo *)ptr;
1009 			if (ipp->ipp_fields & IPPF_IFINDEX)
1010 				pkti->ipi6_ifindex = ipp->ipp_ifindex;
1011 			else
1012 				pkti->ipi6_ifindex = 0;
1013 			if (ipp->ipp_fields & IPPF_ADDR)
1014 				pkti->ipi6_addr = ipp->ipp_addr;
1015 			else
1016 				pkti->ipi6_addr = ipv6_all_zeros;
1017 			*optlen = sizeof (struct in6_pktinfo);
1018 			break;
1019 		}
1020 		case IPV6_NEXTHOP: {
1021 			sin6_t *sin6;
1022 
1023 			if (buflen < sizeof (sin6_t)) {
1024 				retval = EINVAL;
1025 				break;
1026 			}
1027 			sin6 = (sin6_t *)ptr;
1028 			if (!(ipp->ipp_fields & IPPF_NEXTHOP))
1029 				break;
1030 			*sin6 = sctp_sin6_null;
1031 			sin6->sin6_family = AF_INET6;
1032 			sin6->sin6_addr = ipp->ipp_nexthop;
1033 			*optlen = sizeof (sin6_t);
1034 			break;
1035 		}
1036 		case IPV6_HOPOPTS: {
1037 			int len;
1038 
1039 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
1040 				break;
1041 			len = ipp->ipp_hopoptslen - sctp->sctp_v6label_len;
1042 			if (len <= 0)
1043 				break;
1044 			if (buflen < len) {
1045 				retval = EINVAL;
1046 				break;
1047 			}
1048 			bcopy((char *)ipp->ipp_hopopts +
1049 			    sctp->sctp_v6label_len, ptr, len);
1050 			if (sctp->sctp_v6label_len > 0) {
1051 				char *cptr = ptr;
1052 
1053 				/*
1054 				 * If the label length is greater than zero,
1055 				 * then we need to hide the label from user.
1056 				 * Make it look as though a normal Hop-By-Hop
1057 				 * Options Header is present here.
1058 				 */
1059 				cptr[0] = ((char *)ipp->ipp_hopopts)[0];
1060 				cptr[1] = (len + 7) / 8 - 1;
1061 			}
1062 			*optlen = len;
1063 			break;
1064 		}
1065 		case IPV6_RTHDRDSTOPTS:
1066 			if (!(ipp->ipp_fields & IPPF_RTDSTOPTS))
1067 				break;
1068 			if (buflen < ipp->ipp_rtdstoptslen) {
1069 				retval = EINVAL;
1070 				break;
1071 			}
1072 			bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen);
1073 			*optlen  = ipp->ipp_rtdstoptslen;
1074 			break;
1075 		case IPV6_RTHDR:
1076 			if (!(ipp->ipp_fields & IPPF_RTHDR))
1077 				break;
1078 			if (buflen < ipp->ipp_rthdrlen) {
1079 				retval = EINVAL;
1080 				break;
1081 			}
1082 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
1083 			*optlen = ipp->ipp_rthdrlen;
1084 			break;
1085 		case IPV6_DSTOPTS:
1086 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
1087 				break;
1088 			if (buflen < ipp->ipp_dstoptslen) {
1089 				retval = EINVAL;
1090 				break;
1091 			}
1092 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
1093 			*optlen  = ipp->ipp_dstoptslen;
1094 			break;
1095 		case IPV6_V6ONLY:
1096 			*i1 = sctp->sctp_connp->conn_ipv6_v6only;
1097 			break;
1098 		default:
1099 			retval = EINVAL;
1100 			break;
1101 		}
1102 		break;
1103 
1104 	default:
1105 		retval = EINVAL;
1106 		break;
1107 	}
1108 	WAKE_SCTP(sctp);
1109 	return (retval);
1110 }
1111 
1112 int
1113 sctp_set_opt(sctp_t *sctp, int level, int name, const void *invalp,
1114     socklen_t inlen)
1115 {
1116 	ip6_pkt_t	*ipp = &sctp->sctp_sticky_ipp;
1117 	int		*i1 = (int *)invalp;
1118 	boolean_t	onoff;
1119 	int		retval = 0, addrcnt;
1120 	conn_t		*connp = sctp->sctp_connp;
1121 
1122 	/* In all cases, the size of the option must be bigger than int */
1123 	if (inlen >= sizeof (int32_t)) {
1124 		onoff = ONOFF(*i1);
1125 	}
1126 	retval = 0;
1127 
1128 	RUN_SCTP(sctp);
1129 
1130 	switch (level) {
1131 	case SOL_SOCKET:
1132 		if (inlen < sizeof (int32_t)) {
1133 			retval = EINVAL;
1134 			break;
1135 		}
1136 		switch (name) {
1137 		case SO_LINGER: {
1138 			struct linger *lgr;
1139 
1140 			if (inlen != sizeof (struct linger)) {
1141 				retval = EINVAL;
1142 				break;
1143 			}
1144 			lgr = (struct linger *)invalp;
1145 			if (lgr->l_onoff != 0) {
1146 				sctp->sctp_linger = 1;
1147 				sctp->sctp_lingertime = MSEC_TO_TICK(
1148 				    lgr->l_linger);
1149 			} else {
1150 				sctp->sctp_linger = 0;
1151 				sctp->sctp_lingertime = 0;
1152 			}
1153 			break;
1154 		}
1155 		case SO_DEBUG:
1156 			sctp->sctp_debug = onoff;
1157 			break;
1158 		case SO_KEEPALIVE:
1159 			break;
1160 		case SO_DONTROUTE:
1161 			/*
1162 			 * SO_DONTROUTE, SO_USELOOPBACK and SO_BROADCAST are
1163 			 * only of interest to IP.  We track them here only so
1164 			 * that we can report their current value.
1165 			 */
1166 			sctp->sctp_dontroute = onoff;
1167 			connp->conn_dontroute = onoff;
1168 			break;
1169 		case SO_USELOOPBACK:
1170 			sctp->sctp_useloopback = onoff;
1171 			connp->conn_loopback = onoff;
1172 			break;
1173 		case SO_BROADCAST:
1174 			sctp->sctp_broadcast = onoff;
1175 			connp->conn_broadcast = onoff;
1176 			break;
1177 		case SO_REUSEADDR:
1178 			sctp->sctp_reuseaddr = onoff;
1179 			connp->conn_reuseaddr = onoff;
1180 			break;
1181 		case SO_DGRAM_ERRIND:
1182 			sctp->sctp_dgram_errind = onoff;
1183 			break;
1184 		case SO_SNDBUF:
1185 			if (*i1 > sctp_max_buf) {
1186 				retval = ENOBUFS;
1187 				break;
1188 			}
1189 			if (*i1 < 0) {
1190 				retval = EINVAL;
1191 				break;
1192 			}
1193 			sctp->sctp_xmit_hiwater = *i1;
1194 			if (sctp_snd_lowat_fraction != 0)
1195 				sctp->sctp_xmit_lowater =
1196 				    sctp->sctp_xmit_hiwater /
1197 				    sctp_snd_lowat_fraction;
1198 			break;
1199 		case SO_RCVBUF:
1200 			if (*i1 > sctp_max_buf) {
1201 				retval = ENOBUFS;
1202 				break;
1203 			}
1204 			/* Silently ignore zero */
1205 			if (*i1 != 0) {
1206 				/*
1207 				 * Insist on a receive window that is at least
1208 				 * sctp_recv_hiwat_minmss * MSS (default 4*MSS)
1209 				 * to avoid funny interactions of Nagle
1210 				 * algorithm, SWS avoidance and delayed
1211 				 * acknowledgement.
1212 				 */
1213 				*i1 = MAX(*i1,
1214 				    sctp_recv_hiwat_minmss * sctp->sctp_mss);
1215 				sctp->sctp_rwnd = *i1;
1216 				sctp->sctp_irwnd = sctp->sctp_rwnd;
1217 			}
1218 			/*
1219 			 * XXX should we return the rwnd here
1220 			 * and sctp_opt_get ?
1221 			 */
1222 			break;
1223 		case SO_MAC_EXEMPT:
1224 			if (secpolicy_net_mac_aware(sctp->sctp_credp) != 0 ||
1225 			    sctp->sctp_state >= SCTPS_BOUND) {
1226 				retval = EACCES;
1227 			} else {
1228 				sctp->sctp_mac_exempt = onoff;
1229 				connp->conn_mac_exempt = onoff;
1230 			}
1231 			break;
1232 		default:
1233 			retval = EINVAL;
1234 			break;
1235 		}
1236 		break;
1237 
1238 	case IPPROTO_SCTP:
1239 		if (inlen < sizeof (int32_t)) {
1240 			retval = EINVAL;
1241 			break;
1242 		}
1243 		switch (name) {
1244 		case SCTP_RTOINFO:
1245 			retval = sctp_set_rtoinfo(sctp, invalp, inlen);
1246 			break;
1247 		case SCTP_ASSOCINFO:
1248 			retval = sctp_set_assocparams(sctp, invalp, inlen);
1249 			break;
1250 		case SCTP_INITMSG:
1251 			retval = sctp_set_initmsg(sctp, invalp, inlen);
1252 			break;
1253 		case SCTP_NODELAY:
1254 			sctp->sctp_ndelay = ONOFF(*i1);
1255 			break;
1256 		case SCTP_AUTOCLOSE:
1257 			if (SEC_TO_TICK(*i1) < 0) {
1258 				retval = EINVAL;
1259 				break;
1260 			}
1261 			/* Convert the number of seconds to ticks. */
1262 			sctp->sctp_autoclose = SEC_TO_TICK(*i1);
1263 			sctp_heartbeat_timer(sctp);
1264 			break;
1265 		case SCTP_SET_PEER_PRIMARY_ADDR:
1266 			retval = sctp_set_peerprim(sctp, invalp, inlen);
1267 			break;
1268 		case SCTP_PRIMARY_ADDR:
1269 			retval = sctp_set_prim(sctp, invalp, inlen);
1270 			break;
1271 		case SCTP_ADAPTION_LAYER: {
1272 			struct sctp_setadaption *ssb;
1273 
1274 			if (inlen < sizeof (struct sctp_setadaption)) {
1275 				retval = EINVAL;
1276 				break;
1277 			}
1278 			ssb = (struct sctp_setadaption *)invalp;
1279 			sctp->sctp_send_adaption = 1;
1280 			sctp->sctp_tx_adaption_code = ssb->ssb_adaption_ind;
1281 			break;
1282 		}
1283 		case SCTP_PEER_ADDR_PARAMS:
1284 			retval = sctp_set_peer_addr_params(sctp, invalp,
1285 			    inlen);
1286 			break;
1287 		case SCTP_DEFAULT_SEND_PARAM:
1288 			retval = sctp_set_def_send_params(sctp, invalp, inlen);
1289 			break;
1290 		case SCTP_EVENTS: {
1291 			struct sctp_event_subscribe *ev;
1292 
1293 			if (inlen < sizeof (struct sctp_event_subscribe)) {
1294 				retval = EINVAL;
1295 				break;
1296 			}
1297 			ev = (struct sctp_event_subscribe *)invalp;
1298 			sctp->sctp_recvsndrcvinfo =
1299 			    ONOFF(ev->sctp_data_io_event);
1300 			sctp->sctp_recvassocevnt =
1301 			    ONOFF(ev->sctp_association_event);
1302 			sctp->sctp_recvpathevnt =
1303 			    ONOFF(ev->sctp_address_event);
1304 			sctp->sctp_recvsendfailevnt =
1305 			    ONOFF(ev->sctp_send_failure_event);
1306 			sctp->sctp_recvpeererr =
1307 			    ONOFF(ev->sctp_peer_error_event);
1308 			sctp->sctp_recvshutdownevnt =
1309 			    ONOFF(ev->sctp_shutdown_event);
1310 			sctp->sctp_recvpdevnt =
1311 			    ONOFF(ev->sctp_partial_delivery_event);
1312 			sctp->sctp_recvalevnt =
1313 				ONOFF(ev->sctp_adaption_layer_event);
1314 			break;
1315 		}
1316 		case SCTP_ADD_ADDR:
1317 		case SCTP_REM_ADDR:
1318 			/*
1319 			 * The sctp_t has to be bound first before
1320 			 * the address list can be changed.
1321 			 */
1322 			if (sctp->sctp_state < SCTPS_BOUND) {
1323 				retval = EINVAL;
1324 				break;
1325 			}
1326 			if (sctp->sctp_family == AF_INET) {
1327 				addrcnt = inlen / sizeof (struct sockaddr_in);
1328 			} else {
1329 				ASSERT(sctp->sctp_family == AF_INET6);
1330 				addrcnt = inlen / sizeof (struct sockaddr_in6);
1331 			}
1332 			if (name == SCTP_ADD_ADDR) {
1333 				retval = sctp_bind_add(sctp, invalp, addrcnt,
1334 				    B_TRUE, sctp->sctp_lport);
1335 			} else {
1336 				retval = sctp_bind_del(sctp, invalp, addrcnt,
1337 				    B_TRUE);
1338 			}
1339 			break;
1340 		case SCTP_UC_SWAP: {
1341 			struct sctp_uc_swap *us;
1342 
1343 			/*
1344 			 * Change handle & upcalls.
1345 			 */
1346 			if (inlen < sizeof (*us)) {
1347 				retval = EINVAL;
1348 				break;
1349 			}
1350 			us = (struct sctp_uc_swap *)invalp;
1351 			sctp->sctp_ulpd = us->sus_handle;
1352 			bcopy(us->sus_upcalls, &sctp->sctp_upcalls,
1353 			    sizeof (sctp_upcalls_t));
1354 			break;
1355 		}
1356 		case SCTP_PRSCTP:
1357 			sctp->sctp_prsctp_aware = onoff;
1358 			break;
1359 		case SCTP_I_WANT_MAPPED_V4_ADDR:
1360 		case SCTP_MAXSEG:
1361 		case SCTP_DISABLE_FRAGMENTS:
1362 			/* Not yet supported. */
1363 		default:
1364 			retval = EINVAL;
1365 			break;
1366 		}
1367 		break;
1368 
1369 	case IPPROTO_IP:
1370 		if (sctp->sctp_family != AF_INET) {
1371 			retval = ENOPROTOOPT;
1372 			break;
1373 		}
1374 		if ((name != IP_OPTIONS) && (inlen < sizeof (int32_t))) {
1375 			retval = EINVAL;
1376 			break;
1377 		}
1378 		switch (name) {
1379 		case IP_OPTIONS:
1380 		case T_IP_OPTIONS:
1381 			retval = sctp_opt_set_header(sctp, invalp, inlen);
1382 			break;
1383 		case IP_TOS:
1384 		case T_IP_TOS:
1385 			sctp->sctp_ipha->ipha_type_of_service = (uchar_t)*i1;
1386 			break;
1387 		case IP_TTL:
1388 			sctp->sctp_ipha->ipha_ttl = (uchar_t)*i1;
1389 			break;
1390 		case IP_SEC_OPT:
1391 			/*
1392 			 * We should not allow policy setting after
1393 			 * we start listening for connections.
1394 			 */
1395 			if (sctp->sctp_state >= SCTPS_LISTEN) {
1396 				retval = EINVAL;
1397 			} else {
1398 				retval = ipsec_set_req(sctp->sctp_credp,
1399 				    sctp->sctp_connp, (ipsec_req_t *)invalp);
1400 			}
1401 			break;
1402 		/* IP level options */
1403 		case IP_RECVIF:
1404 			connp->conn_recvif = onoff;
1405 			break;
1406 		case IP_RECVSLLA:
1407 			connp->conn_recvslla = onoff;
1408 			break;
1409 		case IP_UNSPEC_SRC:
1410 			connp->conn_unspec_src = onoff;
1411 			break;
1412 		case IP_NEXTHOP: {
1413 			ipaddr_t addr = *i1;
1414 			ipif_t *ipif = NULL;
1415 			ill_t *ill;
1416 
1417 			if (secpolicy_net(CRED(), OP_CONFIG, B_TRUE) == 0) {
1418 				ipif =
1419 				    ipif_lookup_onlink_addr(addr,
1420 				    connp->conn_zoneid);
1421 				if (ipif == NULL) {
1422 					retval = EHOSTUNREACH;
1423 					break;
1424 				}
1425 				ill = ipif->ipif_ill;
1426 				mutex_enter(&ill->ill_lock);
1427 				if ((ill->ill_state_flags & ILL_CONDEMNED) ||
1428 				    (ipif->ipif_state_flags & IPIF_CONDEMNED)) {
1429 					mutex_exit(&ill->ill_lock);
1430 					ipif_refrele(ipif);
1431 					retval =  EHOSTUNREACH;
1432 					break;
1433 				}
1434 				mutex_exit(&ill->ill_lock);
1435 				ipif_refrele(ipif);
1436 				mutex_enter(&connp->conn_lock);
1437 				connp->conn_nexthop_v4 = addr;
1438 				connp->conn_nexthop_set = B_TRUE;
1439 				mutex_exit(&connp->conn_lock);
1440 			}
1441 			break;
1442 		}
1443 		default:
1444 			retval = EINVAL;
1445 			break;
1446 		}
1447 		break;
1448 	case IPPROTO_IPV6: {
1449 		if (sctp->sctp_family != AF_INET6) {
1450 			retval = ENOPROTOOPT;
1451 			break;
1452 		}
1453 
1454 		switch (name) {
1455 		case IPV6_UNICAST_HOPS:
1456 			if (inlen < sizeof (int32_t)) {
1457 				retval = EINVAL;
1458 				break;
1459 			}
1460 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
1461 				retval = EINVAL;
1462 				break;
1463 			}
1464 			if (*i1 == -1) {
1465 				ipp->ipp_unicast_hops = sctp_ipv6_hoplimit;
1466 				ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
1467 			} else {
1468 				ipp->ipp_unicast_hops = (uint8_t)*i1;
1469 				ipp->ipp_fields |= IPPF_UNICAST_HOPS;
1470 			}
1471 			retval = sctp_build_hdrs(sctp);
1472 			break;
1473 		case IPV6_UNSPEC_SRC:
1474 			if (inlen < sizeof (int32_t)) {
1475 				retval = EINVAL;
1476 				break;
1477 			}
1478 			connp->conn_unspec_src = onoff;
1479 			break;
1480 		case IPV6_RECVPKTINFO:
1481 			if (inlen < sizeof (int32_t)) {
1482 				retval = EINVAL;
1483 				break;
1484 			}
1485 			if (onoff)
1486 				sctp->sctp_ipv6_recvancillary |=
1487 				    SCTP_IPV6_RECVPKTINFO;
1488 			else
1489 				sctp->sctp_ipv6_recvancillary &=
1490 				    ~SCTP_IPV6_RECVPKTINFO;
1491 			/* Send it with the next msg */
1492 			sctp->sctp_recvifindex = 0;
1493 			connp->conn_ipv6_recvpktinfo = onoff;
1494 			break;
1495 		case IPV6_RECVHOPLIMIT:
1496 			if (inlen < sizeof (int32_t)) {
1497 				retval = EINVAL;
1498 				break;
1499 			}
1500 			if (onoff)
1501 				sctp->sctp_ipv6_recvancillary |=
1502 				    SCTP_IPV6_RECVHOPLIMIT;
1503 			else
1504 				sctp->sctp_ipv6_recvancillary &=
1505 				    ~SCTP_IPV6_RECVHOPLIMIT;
1506 			sctp->sctp_recvhops = 0xffffffffU;
1507 			connp->conn_ipv6_recvhoplimit = onoff;
1508 			break;
1509 		case IPV6_RECVHOPOPTS:
1510 			if (inlen < sizeof (int32_t)) {
1511 				retval = EINVAL;
1512 				break;
1513 			}
1514 			if (onoff)
1515 				sctp->sctp_ipv6_recvancillary |=
1516 				    SCTP_IPV6_RECVHOPOPTS;
1517 			else
1518 				sctp->sctp_ipv6_recvancillary &=
1519 				    ~SCTP_IPV6_RECVHOPOPTS;
1520 			connp->conn_ipv6_recvhopopts = onoff;
1521 			break;
1522 		case IPV6_RECVDSTOPTS:
1523 			if (inlen < sizeof (int32_t)) {
1524 				retval = EINVAL;
1525 				break;
1526 			}
1527 			if (onoff)
1528 				sctp->sctp_ipv6_recvancillary |=
1529 				    SCTP_IPV6_RECVDSTOPTS;
1530 			else
1531 				sctp->sctp_ipv6_recvancillary &=
1532 				    ~SCTP_IPV6_RECVDSTOPTS;
1533 			connp->conn_ipv6_recvdstopts = onoff;
1534 			break;
1535 		case IPV6_RECVRTHDR:
1536 			if (inlen < sizeof (int32_t)) {
1537 				retval = EINVAL;
1538 				break;
1539 			}
1540 			if (onoff)
1541 				sctp->sctp_ipv6_recvancillary |=
1542 				    SCTP_IPV6_RECVRTHDR;
1543 			else
1544 				sctp->sctp_ipv6_recvancillary &=
1545 				    ~SCTP_IPV6_RECVRTHDR;
1546 			connp->conn_ipv6_recvrthdr = onoff;
1547 			break;
1548 		case IPV6_RECVRTHDRDSTOPTS:
1549 			if (inlen < sizeof (int32_t)) {
1550 				retval = EINVAL;
1551 				break;
1552 			}
1553 			if (onoff)
1554 				sctp->sctp_ipv6_recvancillary |=
1555 				    SCTP_IPV6_RECVRTDSTOPTS;
1556 			else
1557 				sctp->sctp_ipv6_recvancillary &=
1558 				    ~SCTP_IPV6_RECVRTDSTOPTS;
1559 			connp->conn_ipv6_recvrtdstopts = onoff;
1560 			break;
1561 		case IPV6_PKTINFO:
1562 			if (inlen != 0 &&
1563 			    inlen != sizeof (struct in6_pktinfo)) {
1564 				retval = EINVAL;
1565 				break;
1566 			}
1567 
1568 			if (inlen == 0) {
1569 				ipp->ipp_fields &= ~(IPPF_IFINDEX |IPPF_ADDR);
1570 			} else  {
1571 				struct in6_pktinfo *pkti;
1572 
1573 				pkti = (struct in6_pktinfo *)invalp;
1574 				/* XXX Need to check if the index exists */
1575 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
1576 				ipp->ipp_addr = pkti->ipi6_addr;
1577 				if (ipp->ipp_ifindex != 0)
1578 					ipp->ipp_fields |= IPPF_IFINDEX;
1579 				else
1580 					ipp->ipp_fields &= ~IPPF_IFINDEX;
1581 				if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr))
1582 					ipp->ipp_fields |= IPPF_ADDR;
1583 				else
1584 					ipp->ipp_fields &= ~IPPF_ADDR;
1585 			}
1586 			retval = sctp_build_hdrs(sctp);
1587 			break;
1588 		case IPV6_NEXTHOP: {
1589 			struct sockaddr_in6 *sin6;
1590 
1591 			if (inlen != 0 && inlen != sizeof (sin6_t)) {
1592 				retval = EINVAL;
1593 				break;
1594 			}
1595 
1596 			if (inlen == 0) {
1597 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
1598 			} else {
1599 				sin6 = (struct sockaddr_in6 *)invalp;
1600 				if (sin6->sin6_family != AF_INET6) {
1601 					retval = EAFNOSUPPORT;
1602 					break;
1603 				}
1604 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1605 					retval = EADDRNOTAVAIL;
1606 					break;
1607 				}
1608 				ipp->ipp_nexthop = sin6->sin6_addr;
1609 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1610 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
1611 				} else {
1612 					ire_t	*ire;
1613 
1614 					ire = ire_route_lookup_v6(
1615 					    &sin6->sin6_addr, NULL, NULL, 0,
1616 					    NULL, NULL, ALL_ZONES, NULL,
1617 					    MATCH_IRE_DEFAULT);
1618 					if (ire == NULL) {
1619 						retval = EHOSTUNREACH;
1620 						break;
1621 					}
1622 					ire_refrele(ire);
1623 					ipp->ipp_fields |= IPPF_NEXTHOP;
1624 				}
1625 			}
1626 			retval = sctp_build_hdrs(sctp);
1627 			break;
1628 		}
1629 		case IPV6_HOPOPTS: {
1630 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
1631 
1632 			if (inlen != 0 &&
1633 			    inlen != (8 * (hopts->ip6h_len + 1))) {
1634 				retval = EINVAL;
1635 				break;
1636 			}
1637 
1638 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1639 			    B_TRUE, (uchar_t **)&ipp->ipp_hopopts,
1640 			    &ipp->ipp_hopoptslen, sctp->sctp_v6label_len);
1641 			if (retval != 0)
1642 				break;
1643 			if (ipp->ipp_hopoptslen == 0)
1644 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
1645 			else
1646 				ipp->ipp_fields |= IPPF_HOPOPTS;
1647 			retval = sctp_build_hdrs(sctp);
1648 			break;
1649 		}
1650 		case IPV6_RTHDRDSTOPTS: {
1651 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
1652 
1653 			if (inlen != 0 &&
1654 			    inlen != (8 * (dopts->ip6d_len + 1))) {
1655 				retval = EINVAL;
1656 				break;
1657 			}
1658 
1659 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1660 			    B_TRUE, (uchar_t **)&ipp->ipp_rtdstopts,
1661 			    &ipp->ipp_rtdstoptslen, 0);
1662 			if (retval != 0)
1663 				break;
1664 			if (ipp->ipp_rtdstoptslen == 0)
1665 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
1666 			else
1667 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
1668 			retval = sctp_build_hdrs(sctp);
1669 			break;
1670 		}
1671 		case IPV6_DSTOPTS: {
1672 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
1673 
1674 			if (inlen != 0 &&
1675 			    inlen != (8 * (dopts->ip6d_len + 1))) {
1676 				retval = EINVAL;
1677 				break;
1678 			}
1679 
1680 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1681 			    B_TRUE, (uchar_t **)&ipp->ipp_dstopts,
1682 			    &ipp->ipp_dstoptslen, 0);
1683 			if (retval != 0)
1684 				break;
1685 			if (ipp->ipp_dstoptslen == 0)
1686 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
1687 			else
1688 				ipp->ipp_fields |= IPPF_DSTOPTS;
1689 			retval = sctp_build_hdrs(sctp);
1690 			break;
1691 		}
1692 		case IPV6_RTHDR: {
1693 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
1694 
1695 			if (inlen != 0 &&
1696 			    inlen != (8 * (rt->ip6r_len + 1))) {
1697 				retval = EINVAL;
1698 				break;
1699 			}
1700 
1701 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1702 			    B_TRUE, (uchar_t **)&ipp->ipp_rthdr,
1703 			    &ipp->ipp_rthdrlen, 0);
1704 			if (retval != 0)
1705 				break;
1706 			if (ipp->ipp_rthdrlen == 0)
1707 				ipp->ipp_fields &= ~IPPF_RTHDR;
1708 			else
1709 				ipp->ipp_fields |= IPPF_RTHDR;
1710 			retval = sctp_build_hdrs(sctp);
1711 			break;
1712 		}
1713 		case IPV6_SEC_OPT:
1714 			/*
1715 			 * We should not allow policy setting after
1716 			 * we start listening for connections.
1717 			 */
1718 			if (sctp->sctp_state >= SCTPS_LISTEN) {
1719 				retval = EINVAL;
1720 			} else {
1721 				retval = ipsec_set_req(sctp->sctp_credp,
1722 				    sctp->sctp_connp, (ipsec_req_t *)invalp);
1723 			}
1724 			break;
1725 		case IPV6_V6ONLY:
1726 			/*
1727 			 * After the bound state, setting the v6only option
1728 			 * is too late.
1729 			 */
1730 			if (sctp->sctp_state >= SCTPS_BOUND) {
1731 				retval = EINVAL;
1732 			} else {
1733 				sctp->sctp_connp->conn_ipv6_v6only = onoff;
1734 			}
1735 			break;
1736 		default:
1737 			retval = EINVAL;
1738 			break;
1739 		}
1740 		break;
1741 	}
1742 	default:
1743 		retval = EINVAL;
1744 		break;
1745 	}
1746 
1747 	WAKE_SCTP(sctp);
1748 	return (retval);
1749 }
1750 
1751 /*
1752  * SCTP exported kernel interface for geting the first source address of
1753  * a sctp_t.  The parameter addr is assumed to have enough space to hold
1754  * one socket address.
1755  */
1756 int
1757 sctp_getsockname(sctp_t *sctp, struct sockaddr *addr, socklen_t *addrlen)
1758 {
1759 	int	err = 0;
1760 	int	addrcnt = 1;
1761 	sin_t	*sin4;
1762 	sin6_t	*sin6;
1763 
1764 	ASSERT(sctp != NULL);
1765 
1766 	RUN_SCTP(sctp);
1767 	addr->sa_family = sctp->sctp_family;
1768 	switch (sctp->sctp_family) {
1769 	case AF_INET:
1770 		sin4 = (sin_t *)addr;
1771 		if ((sctp->sctp_state <= SCTPS_LISTEN) &&
1772 		    sctp->sctp_bound_to_all) {
1773 			sin4->sin_addr.s_addr = INADDR_ANY;
1774 			sin4->sin_port = sctp->sctp_lport;
1775 		} else {
1776 			err = sctp_getmyaddrs(sctp, sin4, &addrcnt);
1777 			if (err != 0) {
1778 				*addrlen = 0;
1779 				break;
1780 			}
1781 		}
1782 		*addrlen = sizeof (struct sockaddr_in);
1783 		break;
1784 	case AF_INET6:
1785 		sin6 = (sin6_t *)addr;
1786 		if ((sctp->sctp_state <= SCTPS_LISTEN) &&
1787 		    sctp->sctp_bound_to_all) {
1788 			bzero(&sin6->sin6_addr, sizeof (sin6->sin6_addr));
1789 			sin6->sin6_port = sctp->sctp_lport;
1790 		} else {
1791 			err = sctp_getmyaddrs(sctp, sin6, &addrcnt);
1792 			if (err != 0) {
1793 				*addrlen = 0;
1794 				break;
1795 			}
1796 		}
1797 		*addrlen = sizeof (struct sockaddr_in6);
1798 		sin6->sin6_flowinfo = sctp->sctp_ip6h->ip6_vcf &
1799 		    ~IPV6_VERS_AND_FLOW_MASK;
1800 		sin6->sin6_scope_id = 0;
1801 		sin6->__sin6_src_id = 0;
1802 		break;
1803 	}
1804 	WAKE_SCTP(sctp);
1805 	return (err);
1806 }
1807 
1808 /*
1809  * SCTP exported kernel interface for geting the primary peer address of
1810  * a sctp_t.  The parameter addr is assumed to have enough space to hold
1811  * one socket address.
1812  */
1813 int
1814 sctp_getpeername(sctp_t *sctp, struct sockaddr *addr, socklen_t *addrlen)
1815 {
1816 	int	err = 0;
1817 	int	addrcnt = 1;
1818 	sin6_t	*sin6;
1819 
1820 	ASSERT(sctp != NULL);
1821 
1822 	RUN_SCTP(sctp);
1823 	addr->sa_family = sctp->sctp_family;
1824 	switch (sctp->sctp_family) {
1825 	case AF_INET:
1826 		err = sctp_getpeeraddrs(sctp, addr, &addrcnt);
1827 		if (err != 0) {
1828 			*addrlen = 0;
1829 			break;
1830 		}
1831 		*addrlen = sizeof (struct sockaddr_in);
1832 		break;
1833 	case AF_INET6:
1834 		sin6 = (sin6_t *)addr;
1835 		err = sctp_getpeeraddrs(sctp, sin6, &addrcnt);
1836 		if (err != 0) {
1837 			*addrlen = 0;
1838 			break;
1839 		}
1840 		*addrlen = sizeof (struct sockaddr_in6);
1841 		sin6->sin6_flowinfo = 0;
1842 		sin6->sin6_scope_id = 0;
1843 		sin6->__sin6_src_id = 0;
1844 		break;
1845 	}
1846 	WAKE_SCTP(sctp);
1847 	return (err);
1848 }
1849 
1850 /*
1851  * Return a list of IP addresses of the peer endpoint of this sctp_t.
1852  * The parameter paddrs is supposed to be either (struct sockaddr_in *) or
1853  * (struct sockaddr_in6 *) depending on the address family of the sctp_t.
1854  */
1855 int
1856 sctp_getpeeraddrs(sctp_t *sctp, void *paddrs, int *addrcnt)
1857 {
1858 	int			family;
1859 	struct sockaddr_in	*sin4;
1860 	struct sockaddr_in6	*sin6;
1861 	int			max;
1862 	int			cnt;
1863 	sctp_faddr_t		*fp = sctp->sctp_faddrs;
1864 	in6_addr_t		addr;
1865 
1866 	ASSERT(sctp != NULL);
1867 
1868 	if (sctp->sctp_faddrs == NULL)
1869 		return (ENOTCONN);
1870 
1871 	family = sctp->sctp_family;
1872 	max = *addrcnt;
1873 
1874 	/* If we want only one, give the primary */
1875 	if (max == 1) {
1876 		addr = sctp->sctp_primary->faddr;
1877 		switch (family) {
1878 		case AF_INET:
1879 			sin4 = paddrs;
1880 			IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
1881 			sin4->sin_port = sctp->sctp_fport;
1882 			sin4->sin_family = AF_INET;
1883 			break;
1884 
1885 		case AF_INET6:
1886 			sin6 = paddrs;
1887 			sin6->sin6_addr = addr;
1888 			sin6->sin6_port = sctp->sctp_fport;
1889 			sin6->sin6_family = AF_INET6;
1890 			break;
1891 		}
1892 		return (0);
1893 	}
1894 
1895 	for (cnt = 0; cnt < max && fp != NULL; cnt++, fp = fp->next) {
1896 		addr = fp->faddr;
1897 		switch (family) {
1898 		case AF_INET:
1899 			ASSERT(IN6_IS_ADDR_V4MAPPED(&addr));
1900 			sin4 = (struct sockaddr_in *)paddrs + cnt;
1901 			IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
1902 			sin4->sin_port = sctp->sctp_fport;
1903 			sin4->sin_family = AF_INET;
1904 			break;
1905 		case AF_INET6:
1906 			sin6 = (struct sockaddr_in6 *)paddrs + cnt;
1907 			sin6->sin6_addr = addr;
1908 			sin6->sin6_port = sctp->sctp_fport;
1909 			sin6->sin6_family = AF_INET6;
1910 			break;
1911 		}
1912 	}
1913 	*addrcnt = cnt;
1914 	return (0);
1915 }
1916