17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*45916cd2Sjpk  * Common Development and Distribution License (the "License").
6*45916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*45916cd2Sjpk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/stream.h>
307c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
317c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
327c478bd9Sstevel@tonic-gate #include <sys/socket.h>
337c478bd9Sstevel@tonic-gate #include <sys/xti_inet.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
397c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
407c478bd9Sstevel@tonic-gate #include <sys/policy.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <inet/common.h>
437c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
447c478bd9Sstevel@tonic-gate #include <inet/ip.h>
457c478bd9Sstevel@tonic-gate #include <inet/ip_ire.h>
4643d18f1cSpriyanka #include <inet/ip_if.h>
477c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
487c478bd9Sstevel@tonic-gate #include <inet/ipsec_impl.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #include <netinet/in.h>
517c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
527c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <inet/common.h>
557c478bd9Sstevel@tonic-gate #include <inet/ip.h>
567c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
577c478bd9Sstevel@tonic-gate #include <inet/sctp_itf.h>
587c478bd9Sstevel@tonic-gate #include "sctp_impl.h"
597c478bd9Sstevel@tonic-gate #include "sctp_asconf.h"
607c478bd9Sstevel@tonic-gate #include "sctp_addr.h"
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate static int	sctp_getpeeraddrs(sctp_t *, void *, int *);
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /*
65*45916cd2Sjpk  * Copy the standard header into its new location,
66*45916cd2Sjpk  * lay in the new options and then update the relevant
67*45916cd2Sjpk  * fields in both sctp_t and the standard header.
68*45916cd2Sjpk  * Returns 0 on success, errno otherwise.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate static int
71*45916cd2Sjpk sctp_opt_set_header(sctp_t *sctp, const void *ptr, uint_t len)
727c478bd9Sstevel@tonic-gate {
73*45916cd2Sjpk 	uint8_t *ip_optp;
74*45916cd2Sjpk 	sctp_hdr_t *new_sctph;
757c478bd9Sstevel@tonic-gate 
76*45916cd2Sjpk 	if ((len > SCTP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3))
77*45916cd2Sjpk 		return (EINVAL);
787c478bd9Sstevel@tonic-gate 
79*45916cd2Sjpk 	if (len > IP_MAX_OPT_LENGTH - sctp->sctp_v4label_len)
80*45916cd2Sjpk 		return (EINVAL);
817c478bd9Sstevel@tonic-gate 
82*45916cd2Sjpk 	ip_optp = (uint8_t *)sctp->sctp_ipha + IP_SIMPLE_HDR_LENGTH;
837c478bd9Sstevel@tonic-gate 
84*45916cd2Sjpk 	if (sctp->sctp_v4label_len > 0) {
85*45916cd2Sjpk 		int padlen;
86*45916cd2Sjpk 		uint8_t opt;
877c478bd9Sstevel@tonic-gate 
88*45916cd2Sjpk 		/* convert list termination to no-ops as needed */
89*45916cd2Sjpk 		padlen = sctp->sctp_v4label_len - ip_optp[IPOPT_OLEN];
90*45916cd2Sjpk 		ip_optp += ip_optp[IPOPT_OLEN];
91*45916cd2Sjpk 		opt = len > 0 ? IPOPT_NOP : IPOPT_EOL;
92*45916cd2Sjpk 		while (--padlen >= 0)
93*45916cd2Sjpk 			*ip_optp++ = opt;
94*45916cd2Sjpk 		ASSERT(ip_optp == (uint8_t *)sctp->sctp_ipha +
95*45916cd2Sjpk 		    IP_SIMPLE_HDR_LENGTH + sctp->sctp_v4label_len);
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
98*45916cd2Sjpk 	/*
99*45916cd2Sjpk 	 * Move the existing SCTP header out where it belongs.
100*45916cd2Sjpk 	 */
101*45916cd2Sjpk 	new_sctph = (sctp_hdr_t *)(ip_optp + len);
102*45916cd2Sjpk 	ovbcopy(sctp->sctp_sctph, new_sctph, sizeof (sctp_hdr_t));
103*45916cd2Sjpk 	sctp->sctp_sctph = new_sctph;
1047c478bd9Sstevel@tonic-gate 
105*45916cd2Sjpk 	/*
106*45916cd2Sjpk 	 * Insert the new user-supplied IP options.
107*45916cd2Sjpk 	 */
108*45916cd2Sjpk 	if (len > 0)
109*45916cd2Sjpk 		bcopy(ptr, ip_optp, len);
1107c478bd9Sstevel@tonic-gate 
111*45916cd2Sjpk 	len += sctp->sctp_v4label_len;
1127c478bd9Sstevel@tonic-gate 	sctp->sctp_ip_hdr_len = len;
1137c478bd9Sstevel@tonic-gate 	sctp->sctp_ipha->ipha_version_and_hdr_length =
114*45916cd2Sjpk 	    (IP_VERSION << 4) | (len >> 2);
115*45916cd2Sjpk 	sctp->sctp_hdr_len = len + sizeof (sctp_hdr_t);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (sctp->sctp_current) {
1187c478bd9Sstevel@tonic-gate 		/*
1197c478bd9Sstevel@tonic-gate 		 * Could be setting options before setting up connection.
1207c478bd9Sstevel@tonic-gate 		 */
1217c478bd9Sstevel@tonic-gate 		sctp_set_ulp_prop(sctp);
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate 	return (0);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate static int
1277c478bd9Sstevel@tonic-gate sctp_get_status(sctp_t *sctp, void *ptr)
1287c478bd9Sstevel@tonic-gate {
1297c478bd9Sstevel@tonic-gate 	struct sctp_status *sstat = ptr;
1307c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
1317c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
1327c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
1337c478bd9Sstevel@tonic-gate 	struct sctp_paddrinfo *sp;
1347c478bd9Sstevel@tonic-gate 	mblk_t *meta, *mp;
1357c478bd9Sstevel@tonic-gate 	int i;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	sstat->sstat_state = sctp->sctp_state;
1387c478bd9Sstevel@tonic-gate 	sstat->sstat_rwnd = sctp->sctp_frwnd;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	sp = &sstat->sstat_primary;
1417c478bd9Sstevel@tonic-gate 	if (!sctp->sctp_primary) {
1427c478bd9Sstevel@tonic-gate 		bzero(sp, sizeof (*sp));
1437c478bd9Sstevel@tonic-gate 		goto noprim;
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate 	fp = sctp->sctp_primary;
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	if (fp->isv4) {
1487c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)&sp->spinfo_address;
1497c478bd9Sstevel@tonic-gate 		sin->sin_family = AF_INET;
1507c478bd9Sstevel@tonic-gate 		sin->sin_port = sctp->sctp_fport;
1517c478bd9Sstevel@tonic-gate 		IN6_V4MAPPED_TO_INADDR(&fp->faddr, &sin->sin_addr);
1527c478bd9Sstevel@tonic-gate 		sp->spinfo_mtu = sctp->sctp_hdr_len;
1537c478bd9Sstevel@tonic-gate 	} else {
1547c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&sp->spinfo_address;
1557c478bd9Sstevel@tonic-gate 		sin6->sin6_family = AF_INET6;
1567c478bd9Sstevel@tonic-gate 		sin6->sin6_port = sctp->sctp_fport;
1577c478bd9Sstevel@tonic-gate 		sin6->sin6_addr = fp->faddr;
1587c478bd9Sstevel@tonic-gate 		sp->spinfo_mtu = sctp->sctp_hdr6_len;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 	sp->spinfo_state = fp->state == SCTP_FADDRS_ALIVE ? SCTP_ACTIVE :
1617c478bd9Sstevel@tonic-gate 	    SCTP_INACTIVE;
1627c478bd9Sstevel@tonic-gate 	sp->spinfo_cwnd = fp->cwnd;
1637c478bd9Sstevel@tonic-gate 	sp->spinfo_srtt = fp->srtt;
1647c478bd9Sstevel@tonic-gate 	sp->spinfo_rto = fp->rto;
1657c478bd9Sstevel@tonic-gate 	sp->spinfo_mtu += fp->sfa_pmss;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate noprim:
1687c478bd9Sstevel@tonic-gate 	sstat->sstat_unackdata = 0;
1697c478bd9Sstevel@tonic-gate 	sstat->sstat_penddata = 0;
1707c478bd9Sstevel@tonic-gate 	sstat->sstat_instrms = sctp->sctp_num_istr;
1717c478bd9Sstevel@tonic-gate 	sstat->sstat_outstrms = sctp->sctp_num_ostr;
1727c478bd9Sstevel@tonic-gate 	sstat->sstat_fragmentation_point = sctp->sctp_mss -
1737c478bd9Sstevel@tonic-gate 	    sizeof (sctp_data_hdr_t);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/* count unack'd */
1767c478bd9Sstevel@tonic-gate 	for (meta = sctp->sctp_xmit_head; meta; meta = meta->b_next) {
1777c478bd9Sstevel@tonic-gate 		for (mp = meta->b_cont; mp; mp = mp->b_next) {
1787c478bd9Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISSENT(mp)) {
1797c478bd9Sstevel@tonic-gate 				break;
1807c478bd9Sstevel@tonic-gate 			}
1817c478bd9Sstevel@tonic-gate 			if (!SCTP_CHUNK_ISACKED(mp)) {
1827c478bd9Sstevel@tonic-gate 				sstat->sstat_unackdata++;
1837c478bd9Sstevel@tonic-gate 			}
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	/*
1887c478bd9Sstevel@tonic-gate 	 * Count penddata chunks. We can only count chunks in SCTP (not
1897c478bd9Sstevel@tonic-gate 	 * data already delivered to socket layer).
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	if (sctp->sctp_instr != NULL) {
1927c478bd9Sstevel@tonic-gate 		for (i = 0; i < sctp->sctp_num_istr; i++) {
1937c478bd9Sstevel@tonic-gate 			for (meta = sctp->sctp_instr[i].istr_reass;
1947c478bd9Sstevel@tonic-gate 			    meta != NULL; meta = meta->b_next) {
1957c478bd9Sstevel@tonic-gate 				for (mp = meta->b_cont; mp; mp = mp->b_cont) {
1967c478bd9Sstevel@tonic-gate 					if (DB_TYPE(mp) != M_CTL) {
1977c478bd9Sstevel@tonic-gate 						sstat->sstat_penddata++;
1987c478bd9Sstevel@tonic-gate 					}
1997c478bd9Sstevel@tonic-gate 				}
2007c478bd9Sstevel@tonic-gate 			}
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 	}
2037c478bd9Sstevel@tonic-gate 	/* Un-Ordered Frag list */
2047c478bd9Sstevel@tonic-gate 	for (meta = sctp->sctp_uo_frags; meta != NULL; meta = meta->b_next)
2057c478bd9Sstevel@tonic-gate 		sstat->sstat_penddata++;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	return (sizeof (*sstat));
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * SCTP_GET_PEER_ADDR_INFO
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate static int
2147c478bd9Sstevel@tonic-gate sctp_get_paddrinfo(sctp_t *sctp, void *ptr, socklen_t *optlen)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	struct sctp_paddrinfo	*infop = ptr;
2177c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
2187c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
2197c478bd9Sstevel@tonic-gate 	in6_addr_t		faddr;
2207c478bd9Sstevel@tonic-gate 	sctp_faddr_t		*fp;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	switch (infop->spinfo_address.ss_family) {
2237c478bd9Sstevel@tonic-gate 	case AF_INET:
2247c478bd9Sstevel@tonic-gate 		sin4 = (struct sockaddr_in *)&infop->spinfo_address;
2257c478bd9Sstevel@tonic-gate 		IN6_INADDR_TO_V4MAPPED(&sin4->sin_addr, &faddr);
2267c478bd9Sstevel@tonic-gate 		break;
2277c478bd9Sstevel@tonic-gate 	case AF_INET6:
2287c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)&infop->spinfo_address;
2297c478bd9Sstevel@tonic-gate 		faddr = sin6->sin6_addr;
2307c478bd9Sstevel@tonic-gate 		break;
2317c478bd9Sstevel@tonic-gate 	default:
2327c478bd9Sstevel@tonic-gate 		return (EAFNOSUPPORT);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	if ((fp = sctp_lookup_faddr(sctp, &faddr)) == NULL)
2367c478bd9Sstevel@tonic-gate 		return (EINVAL);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 	infop->spinfo_state = (fp->state == SCTP_FADDRS_ALIVE) ? SCTP_ACTIVE :
2397c478bd9Sstevel@tonic-gate 	    SCTP_INACTIVE;
2407c478bd9Sstevel@tonic-gate 	infop->spinfo_cwnd = fp->cwnd;
2417c478bd9Sstevel@tonic-gate 	infop->spinfo_srtt = TICK_TO_MSEC(fp->srtt);
2427c478bd9Sstevel@tonic-gate 	infop->spinfo_rto = TICK_TO_MSEC(fp->rto);
2437c478bd9Sstevel@tonic-gate 	infop->spinfo_mtu = fp->sfa_pmss;
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	*optlen = sizeof (struct sctp_paddrinfo);
2467c478bd9Sstevel@tonic-gate 	return (0);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * SCTP_RTOINFO
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate static int
2537c478bd9Sstevel@tonic-gate sctp_get_rtoinfo(sctp_t *sctp, void *ptr)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	struct sctp_rtoinfo *srto = ptr;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	srto->srto_initial = TICK_TO_MSEC(sctp->sctp_rto_initial);
2587c478bd9Sstevel@tonic-gate 	srto->srto_max = TICK_TO_MSEC(sctp->sctp_rto_max);
2597c478bd9Sstevel@tonic-gate 	srto->srto_min = TICK_TO_MSEC(sctp->sctp_rto_min);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	return (sizeof (*srto));
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static int
2657c478bd9Sstevel@tonic-gate sctp_set_rtoinfo(sctp_t *sctp, const void *invalp, uint_t inlen)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	const struct sctp_rtoinfo *srto;
2687c478bd9Sstevel@tonic-gate 	boolean_t ispriv;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (inlen < sizeof (*srto)) {
2717c478bd9Sstevel@tonic-gate 		return (EINVAL);
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 	srto = invalp;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	ispriv = secpolicy_net_config(CRED(), B_TRUE) == 0;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	/*
2787c478bd9Sstevel@tonic-gate 	 * Bounds checking.  Priviledged user can set the RTO initial
2797c478bd9Sstevel@tonic-gate 	 * outside the ndd boundary.
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	if (srto->srto_initial != 0 &&
2827c478bd9Sstevel@tonic-gate 	    (!ispriv && (srto->srto_initial < sctp_rto_initialg_low ||
2837c478bd9Sstevel@tonic-gate 		srto->srto_initial > sctp_rto_initialg_high))) {
2847c478bd9Sstevel@tonic-gate 		return (EINVAL);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	if (srto->srto_max != 0 &&
2877c478bd9Sstevel@tonic-gate 	    (!ispriv && (srto->srto_max < sctp_rto_maxg_low ||
2887c478bd9Sstevel@tonic-gate 		srto->srto_max > sctp_rto_maxg_high))) {
2897c478bd9Sstevel@tonic-gate 		return (EINVAL);
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	if (srto->srto_min != 0 &&
2927c478bd9Sstevel@tonic-gate 	    (!ispriv && (srto->srto_min < sctp_rto_ming_low ||
2937c478bd9Sstevel@tonic-gate 		srto->srto_min > sctp_rto_ming_high))) {
2947c478bd9Sstevel@tonic-gate 		return (EINVAL);
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 	if (srto->srto_initial != 0) {
2987c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_initial = MSEC_TO_TICK(srto->srto_initial);
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	if (srto->srto_max != 0) {
3017c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_max = MSEC_TO_TICK(srto->srto_max);
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 	if (srto->srto_min != 0) {
3047c478bd9Sstevel@tonic-gate 		sctp->sctp_rto_min = MSEC_TO_TICK(srto->srto_min);
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	return (0);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate  * SCTP_ASSOCINFO
3127c478bd9Sstevel@tonic-gate  */
3137c478bd9Sstevel@tonic-gate static int
3147c478bd9Sstevel@tonic-gate sctp_get_assocparams(sctp_t *sctp, void *ptr)
3157c478bd9Sstevel@tonic-gate {
3167c478bd9Sstevel@tonic-gate 	struct sctp_assocparams *sap = ptr;
3177c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
3187c478bd9Sstevel@tonic-gate 	uint16_t i;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	sap->sasoc_asocmaxrxt = sctp->sctp_pa_max_rxt;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/*
3237c478bd9Sstevel@tonic-gate 	 * Count the number of peer addresses
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	for (i = 0, fp = sctp->sctp_faddrs; fp != NULL; fp = fp->next) {
3267c478bd9Sstevel@tonic-gate 		i++;
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 	sap->sasoc_number_peer_destinations = i;
3297c478bd9Sstevel@tonic-gate 	sap->sasoc_peer_rwnd = sctp->sctp_frwnd;
3307c478bd9Sstevel@tonic-gate 	sap->sasoc_local_rwnd = sctp->sctp_rwnd;
3317c478bd9Sstevel@tonic-gate 	sap->sasoc_cookie_life = TICK_TO_MSEC(sctp->sctp_cookie_lifetime);
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	return (sizeof (*sap));
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate static int
3377c478bd9Sstevel@tonic-gate sctp_set_assocparams(sctp_t *sctp, const void *invalp, uint_t inlen)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	const struct sctp_assocparams *sap = invalp;
3407c478bd9Sstevel@tonic-gate 	uint32_t sum = 0;
3417c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (inlen < sizeof (*sap)) {
3447c478bd9Sstevel@tonic-gate 		return (EINVAL);
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (sap->sasoc_asocmaxrxt) {
3487c478bd9Sstevel@tonic-gate 		if (sctp->sctp_faddrs) {
3497c478bd9Sstevel@tonic-gate 			/*
3507c478bd9Sstevel@tonic-gate 			 * Bounds check: as per rfc2960, assoc max retr cannot
3517c478bd9Sstevel@tonic-gate 			 * exceed the sum of all individual path max retr's.
3527c478bd9Sstevel@tonic-gate 			 */
3537c478bd9Sstevel@tonic-gate 			for (fp = sctp->sctp_faddrs; fp; fp = fp->next) {
3547c478bd9Sstevel@tonic-gate 				sum += fp->max_retr;
3557c478bd9Sstevel@tonic-gate 			}
3567c478bd9Sstevel@tonic-gate 			if (sap->sasoc_asocmaxrxt > sum) {
3577c478bd9Sstevel@tonic-gate 				return (EINVAL);
3587c478bd9Sstevel@tonic-gate 			}
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 		if (sap->sasoc_asocmaxrxt < sctp_pa_max_retr_low ||
3617c478bd9Sstevel@tonic-gate 		    sap->sasoc_asocmaxrxt > sctp_pa_max_retr_high) {
3627c478bd9Sstevel@tonic-gate 			/*
3637c478bd9Sstevel@tonic-gate 			 * Out of bounds.
3647c478bd9Sstevel@tonic-gate 			 */
3657c478bd9Sstevel@tonic-gate 			return (EINVAL);
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate 	if (sap->sasoc_cookie_life != 0 &&
3697c478bd9Sstevel@tonic-gate 	    (sap->sasoc_cookie_life < sctp_cookie_life_low ||
3707c478bd9Sstevel@tonic-gate 		sap->sasoc_cookie_life > sctp_cookie_life_high)) {
3717c478bd9Sstevel@tonic-gate 			return (EINVAL);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	if (sap->sasoc_asocmaxrxt > 0) {
3757c478bd9Sstevel@tonic-gate 		sctp->sctp_pa_max_rxt = sap->sasoc_asocmaxrxt;
3767c478bd9Sstevel@tonic-gate 	}
3777c478bd9Sstevel@tonic-gate 	if (sap->sasoc_cookie_life > 0) {
3787c478bd9Sstevel@tonic-gate 		sctp->sctp_cookie_lifetime = MSEC_TO_TICK(
3797c478bd9Sstevel@tonic-gate 		    sap->sasoc_cookie_life);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 	return (0);
3827c478bd9Sstevel@tonic-gate }
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate  * SCTP_INITMSG
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate static int
3887c478bd9Sstevel@tonic-gate sctp_get_initmsg(sctp_t *sctp, void *ptr)
3897c478bd9Sstevel@tonic-gate {
3907c478bd9Sstevel@tonic-gate 	struct sctp_initmsg *si = ptr;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	si->sinit_num_ostreams = sctp->sctp_num_ostr;
3937c478bd9Sstevel@tonic-gate 	si->sinit_max_instreams = sctp->sctp_num_istr;
3947c478bd9Sstevel@tonic-gate 	si->sinit_max_attempts = sctp->sctp_max_init_rxt;
3957c478bd9Sstevel@tonic-gate 	si->sinit_max_init_timeo = TICK_TO_MSEC(sctp->sctp_init_rto_max);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	return (sizeof (*si));
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate static int
4017c478bd9Sstevel@tonic-gate sctp_set_initmsg(sctp_t *sctp, const void *invalp, uint_t inlen)
4027c478bd9Sstevel@tonic-gate {
4037c478bd9Sstevel@tonic-gate 	const struct sctp_initmsg *si = invalp;
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (sctp->sctp_state > SCTPS_LISTEN) {
4067c478bd9Sstevel@tonic-gate 		return (EINVAL);
4077c478bd9Sstevel@tonic-gate 	}
4087c478bd9Sstevel@tonic-gate 	if (inlen < sizeof (*si)) {
4097c478bd9Sstevel@tonic-gate 		return (EINVAL);
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate 	if (si->sinit_num_ostreams != 0 &&
4127c478bd9Sstevel@tonic-gate 	    (si->sinit_num_ostreams < sctp_initial_out_streams_low ||
4137c478bd9Sstevel@tonic-gate 		si->sinit_num_ostreams > sctp_initial_out_streams_high)) {
4147c478bd9Sstevel@tonic-gate 		/*
4157c478bd9Sstevel@tonic-gate 		 * Out of bounds.
4167c478bd9Sstevel@tonic-gate 		 */
4177c478bd9Sstevel@tonic-gate 		return (EINVAL);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 	if (si->sinit_max_instreams != 0 &&
4207c478bd9Sstevel@tonic-gate 	    (si->sinit_max_instreams < sctp_max_in_streams_low ||
4217c478bd9Sstevel@tonic-gate 		si->sinit_max_instreams > sctp_max_in_streams_high)) {
4227c478bd9Sstevel@tonic-gate 		return (EINVAL);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 	if (si->sinit_max_attempts != 0 &&
4257c478bd9Sstevel@tonic-gate 	    (si->sinit_max_attempts < sctp_max_init_retr_low ||
4267c478bd9Sstevel@tonic-gate 		si->sinit_max_attempts > sctp_max_init_retr_high)) {
4277c478bd9Sstevel@tonic-gate 		return (EINVAL);
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 	if (si->sinit_max_init_timeo != 0 &&
4307c478bd9Sstevel@tonic-gate 	    (secpolicy_net_config(CRED(), B_TRUE) != 0 &&
4317c478bd9Sstevel@tonic-gate 		(si->sinit_max_init_timeo < sctp_rto_maxg_low ||
4327c478bd9Sstevel@tonic-gate 		si->sinit_max_init_timeo > sctp_rto_maxg_high))) {
4337c478bd9Sstevel@tonic-gate 		return (EINVAL);
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	if (si->sinit_num_ostreams != 0)
4367c478bd9Sstevel@tonic-gate 		sctp->sctp_num_ostr = si->sinit_num_ostreams;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	if (si->sinit_max_instreams != 0)
4397c478bd9Sstevel@tonic-gate 		sctp->sctp_num_istr = si->sinit_max_instreams;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if (si->sinit_max_attempts != 0)
4427c478bd9Sstevel@tonic-gate 		sctp->sctp_max_init_rxt = si->sinit_max_attempts;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	if (si->sinit_max_init_timeo != 0) {
4457c478bd9Sstevel@tonic-gate 		sctp->sctp_init_rto_max =
4467c478bd9Sstevel@tonic-gate 		    MSEC_TO_TICK(si->sinit_max_init_timeo);
4477c478bd9Sstevel@tonic-gate 	}
4487c478bd9Sstevel@tonic-gate 	return (0);
4497c478bd9Sstevel@tonic-gate }
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate /*
4527c478bd9Sstevel@tonic-gate  * SCTP_PEER_ADDR_PARAMS
4537c478bd9Sstevel@tonic-gate  */
4547c478bd9Sstevel@tonic-gate static int
4557c478bd9Sstevel@tonic-gate sctp_find_peer_fp(sctp_t *sctp, const struct sockaddr_storage *ss,
4567c478bd9Sstevel@tonic-gate     sctp_faddr_t **fpp)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	struct sockaddr_in *sin;
4597c478bd9Sstevel@tonic-gate 	struct sockaddr_in6 *sin6;
4607c478bd9Sstevel@tonic-gate 	in6_addr_t addr;
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	if (ss->ss_family == AF_INET) {
4637c478bd9Sstevel@tonic-gate 		sin = (struct sockaddr_in *)ss;
4647c478bd9Sstevel@tonic-gate 		IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &addr);
4657c478bd9Sstevel@tonic-gate 	} else if (ss->ss_family == AF_INET6) {
4667c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)ss;
4677c478bd9Sstevel@tonic-gate 		addr = sin6->sin6_addr;
4687c478bd9Sstevel@tonic-gate 	} else if (ss->ss_family) {
4697c478bd9Sstevel@tonic-gate 		return (EAFNOSUPPORT);
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (!ss->ss_family ||
4737c478bd9Sstevel@tonic-gate 	    SCTP_IS_ADDR_UNSPEC(IN6_IS_ADDR_V4MAPPED(&addr), addr)) {
4747c478bd9Sstevel@tonic-gate 		*fpp = NULL;
4757c478bd9Sstevel@tonic-gate 	} else {
4767c478bd9Sstevel@tonic-gate 		*fpp = sctp_lookup_faddr(sctp, &addr);
4777c478bd9Sstevel@tonic-gate 		if (*fpp == NULL) {
4787c478bd9Sstevel@tonic-gate 			return (EINVAL);
4797c478bd9Sstevel@tonic-gate 		}
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 	return (0);
4827c478bd9Sstevel@tonic-gate }
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate static int
4857c478bd9Sstevel@tonic-gate sctp_get_peer_addr_params(sctp_t *sctp, void *ptr)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate 	struct sctp_paddrparams *spp = ptr;
4887c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp;
4897c478bd9Sstevel@tonic-gate 	int retval;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	retval = sctp_find_peer_fp(sctp, &spp->spp_address, &fp);
4927c478bd9Sstevel@tonic-gate 	if (retval) {
4937c478bd9Sstevel@tonic-gate 		return (retval);
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	if (fp) {
4967c478bd9Sstevel@tonic-gate 		spp->spp_hbinterval = TICK_TO_MSEC(fp->hb_interval);
4977c478bd9Sstevel@tonic-gate 		spp->spp_pathmaxrxt = fp->max_retr;
4987c478bd9Sstevel@tonic-gate 	} else {
4997c478bd9Sstevel@tonic-gate 		spp->spp_hbinterval = TICK_TO_MSEC(sctp->sctp_hb_interval);
5007c478bd9Sstevel@tonic-gate 		spp->spp_pathmaxrxt = sctp->sctp_pp_max_rxt;
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 	return (sizeof (*spp));
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate static int
5067c478bd9Sstevel@tonic-gate sctp_set_peer_addr_params(sctp_t *sctp, const void *invalp, uint_t inlen)
5077c478bd9Sstevel@tonic-gate {
5087c478bd9Sstevel@tonic-gate 	const struct sctp_paddrparams *spp = invalp;
5097c478bd9Sstevel@tonic-gate 	sctp_faddr_t *fp, *fp2;
5107c478bd9Sstevel@tonic-gate 	int retval;
5117c478bd9Sstevel@tonic-gate 	uint32_t sum = 0;
5127c478bd9Sstevel@tonic-gate 	int64_t now;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	if (inlen < sizeof (*spp)) {
5157c478bd9Sstevel@tonic-gate 		return (EINVAL);
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	retval = sctp_find_peer_fp(sctp, &spp->spp_address, &fp);
5197c478bd9Sstevel@tonic-gate 	if (retval != 0) {
5207c478bd9Sstevel@tonic-gate 		return (retval);
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	if (spp->spp_hbinterval && spp->spp_hbinterval != UINT32_MAX &&
5247c478bd9Sstevel@tonic-gate 	    (spp->spp_hbinterval < sctp_heartbeat_interval_low ||
5257c478bd9Sstevel@tonic-gate 		spp->spp_hbinterval > sctp_heartbeat_interval_high)) {
5267c478bd9Sstevel@tonic-gate 		return (EINVAL);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 	if (spp->spp_pathmaxrxt &&
5297c478bd9Sstevel@tonic-gate 	    (spp->spp_pathmaxrxt < sctp_pp_max_retr_low ||
5307c478bd9Sstevel@tonic-gate 		spp->spp_pathmaxrxt > sctp_pp_max_retr_high)) {
5317c478bd9Sstevel@tonic-gate 		return (EINVAL);
5327c478bd9Sstevel@tonic-gate 	}
5337c478bd9Sstevel@tonic-gate 	if (spp->spp_pathmaxrxt && sctp->sctp_faddrs) {
5347c478bd9Sstevel@tonic-gate 		for (fp2 = sctp->sctp_faddrs; fp2; fp2 = fp2->next) {
5357c478bd9Sstevel@tonic-gate 			if (!fp || fp2 == fp) {
5367c478bd9Sstevel@tonic-gate 				sum += spp->spp_pathmaxrxt;
5377c478bd9Sstevel@tonic-gate 			} else {
5387c478bd9Sstevel@tonic-gate 				sum += fp2->max_retr;
5397c478bd9Sstevel@tonic-gate 			}
5407c478bd9Sstevel@tonic-gate 		}
5417c478bd9Sstevel@tonic-gate 		if (sctp->sctp_pa_max_rxt > sum) {
5427c478bd9Sstevel@tonic-gate 			return (EINVAL);
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	now = lbolt64;
5477c478bd9Sstevel@tonic-gate 	if (fp != NULL) {
5487c478bd9Sstevel@tonic-gate 		if (spp->spp_hbinterval == UINT32_MAX) {
5497c478bd9Sstevel@tonic-gate 			/*
5507c478bd9Sstevel@tonic-gate 			 * Send heartbeat immediatelly, don't modify the
5517c478bd9Sstevel@tonic-gate 			 * current setting.
5527c478bd9Sstevel@tonic-gate 			 */
5537c478bd9Sstevel@tonic-gate 			sctp_send_heartbeat(sctp, fp);
5547c478bd9Sstevel@tonic-gate 		} else {
5557c478bd9Sstevel@tonic-gate 			fp->hb_interval = MSEC_TO_TICK(spp->spp_hbinterval);
5567c478bd9Sstevel@tonic-gate 			fp->hb_expiry = now + SET_HB_INTVL(fp);
5577c478bd9Sstevel@tonic-gate 			/*
5587c478bd9Sstevel@tonic-gate 			 * Restart the heartbeat timer using the new intrvl.
5597c478bd9Sstevel@tonic-gate 			 * We need to call sctp_heartbeat_timer() to set
5607c478bd9Sstevel@tonic-gate 			 * the earliest heartbeat expiry time.
5617c478bd9Sstevel@tonic-gate 			 */
5627c478bd9Sstevel@tonic-gate 			sctp_heartbeat_timer(sctp);
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate 		if (spp->spp_pathmaxrxt) {
5657c478bd9Sstevel@tonic-gate 			fp->max_retr = spp->spp_pathmaxrxt;
5667c478bd9Sstevel@tonic-gate 		}
5677c478bd9Sstevel@tonic-gate 	} else {
5687c478bd9Sstevel@tonic-gate 		for (fp2 = sctp->sctp_faddrs; fp2 != NULL; fp2 = fp2->next) {
5697c478bd9Sstevel@tonic-gate 			if (spp->spp_hbinterval == UINT32_MAX) {
5707c478bd9Sstevel@tonic-gate 				/*
5717c478bd9Sstevel@tonic-gate 				 * Send heartbeat immediatelly, don't modify
5727c478bd9Sstevel@tonic-gate 				 * the current setting.
5737c478bd9Sstevel@tonic-gate 				 */
5747c478bd9Sstevel@tonic-gate 				sctp_send_heartbeat(sctp, fp2);
5757c478bd9Sstevel@tonic-gate 			} else {
5767c478bd9Sstevel@tonic-gate 				fp2->hb_interval = MSEC_TO_TICK(
5777c478bd9Sstevel@tonic-gate 				    spp->spp_hbinterval);
5787c478bd9Sstevel@tonic-gate 				fp2->hb_expiry = now + SET_HB_INTVL(fp2);
5797c478bd9Sstevel@tonic-gate 			}
5807c478bd9Sstevel@tonic-gate 			if (spp->spp_pathmaxrxt) {
5817c478bd9Sstevel@tonic-gate 				fp2->max_retr = spp->spp_pathmaxrxt;
5827c478bd9Sstevel@tonic-gate 			}
5837c478bd9Sstevel@tonic-gate 		}
5847c478bd9Sstevel@tonic-gate 		if (spp->spp_hbinterval != UINT32_MAX) {
5857c478bd9Sstevel@tonic-gate 			sctp->sctp_hb_interval = MSEC_TO_TICK(
5867c478bd9Sstevel@tonic-gate 			    spp->spp_hbinterval);
5877c478bd9Sstevel@tonic-gate 			/* Restart the heartbeat timer using the new intrvl. */
5887c478bd9Sstevel@tonic-gate 			sctp_timer(sctp, sctp->sctp_heartbeat_mp,
5897c478bd9Sstevel@tonic-gate 			    sctp->sctp_hb_interval);
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 		if (spp->spp_pathmaxrxt) {
5927c478bd9Sstevel@tonic-gate 			sctp->sctp_pp_max_rxt = spp->spp_pathmaxrxt;
5937c478bd9Sstevel@tonic-gate 		}
5947c478bd9Sstevel@tonic-gate 	}
5957c478bd9Sstevel@tonic-gate 	return (0);
5967c478bd9Sstevel@tonic-gate }
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate /*
5997c478bd9Sstevel@tonic-gate  * SCTP_DEFAULT_SEND_PARAM
6007c478bd9Sstevel@tonic-gate  */
6017c478bd9Sstevel@tonic-gate static int
6027c478bd9Sstevel@tonic-gate sctp_get_def_send_params(sctp_t *sctp, void *ptr)
6037c478bd9Sstevel@tonic-gate {
6047c478bd9Sstevel@tonic-gate 	struct sctp_sndrcvinfo *sinfo = ptr;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	sinfo->sinfo_stream = sctp->sctp_def_stream;
6077c478bd9Sstevel@tonic-gate 	sinfo->sinfo_ssn = 0;
6087c478bd9Sstevel@tonic-gate 	sinfo->sinfo_flags = sctp->sctp_def_flags;
6097c478bd9Sstevel@tonic-gate 	sinfo->sinfo_ppid = sctp->sctp_def_ppid;
6107c478bd9Sstevel@tonic-gate 	sinfo->sinfo_context = sctp->sctp_def_context;
6117c478bd9Sstevel@tonic-gate 	sinfo->sinfo_timetolive = sctp->sctp_def_timetolive;
6127c478bd9Sstevel@tonic-gate 	sinfo->sinfo_tsn = 0;
6137c478bd9Sstevel@tonic-gate 	sinfo->sinfo_cumtsn = 0;
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	return (sizeof (*sinfo));
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate static int
6197c478bd9Sstevel@tonic-gate sctp_set_def_send_params(sctp_t *sctp, const void *invalp, uint_t inlen)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	const struct sctp_sndrcvinfo *sinfo = invalp;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (inlen < sizeof (*sinfo)) {
6247c478bd9Sstevel@tonic-gate 		return (EINVAL);
6257c478bd9Sstevel@tonic-gate 	}
6267c478bd9Sstevel@tonic-gate 	if (sinfo->sinfo_stream >= sctp->sctp_num_ostr) {
6277c478bd9Sstevel@tonic-gate 		return (EINVAL);
6287c478bd9Sstevel@tonic-gate 	}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	sctp->sctp_def_stream = sinfo->sinfo_stream;
6317c478bd9Sstevel@tonic-gate 	sctp->sctp_def_flags = sinfo->sinfo_flags;
6327c478bd9Sstevel@tonic-gate 	sctp->sctp_def_ppid = sinfo->sinfo_ppid;
6337c478bd9Sstevel@tonic-gate 	sctp->sctp_def_context = sinfo->sinfo_context;
6347c478bd9Sstevel@tonic-gate 	sctp->sctp_def_timetolive = sinfo->sinfo_timetolive;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	return (0);
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate static int
6407c478bd9Sstevel@tonic-gate sctp_set_prim(sctp_t *sctp, const void *invalp, uint_t inlen)
6417c478bd9Sstevel@tonic-gate {
6427c478bd9Sstevel@tonic-gate 	const struct	sctp_setpeerprim *pp = invalp;
6437c478bd9Sstevel@tonic-gate 	int		retval;
6447c478bd9Sstevel@tonic-gate 	sctp_faddr_t	*fp;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	if (inlen < sizeof (*pp)) {
6477c478bd9Sstevel@tonic-gate 		return (EINVAL);
6487c478bd9Sstevel@tonic-gate 	}
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	retval = sctp_find_peer_fp(sctp, &pp->sspp_addr, &fp);
6517c478bd9Sstevel@tonic-gate 	if (retval)
6527c478bd9Sstevel@tonic-gate 		return (retval);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	if (fp == NULL)
6557c478bd9Sstevel@tonic-gate 		return (EINVAL);
6567c478bd9Sstevel@tonic-gate 	if (fp == sctp->sctp_primary)
6577c478bd9Sstevel@tonic-gate 		return (0);
6587c478bd9Sstevel@tonic-gate 	sctp->sctp_primary = fp;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/* Only switch current if fp is alive */
6617c478bd9Sstevel@tonic-gate 	if (fp->state != SCTP_FADDRS_ALIVE || fp == sctp->sctp_current) {
6627c478bd9Sstevel@tonic-gate 		return (0);
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 	sctp->sctp_current = fp;
6657c478bd9Sstevel@tonic-gate 	sctp->sctp_mss = fp->sfa_pmss;
6667c478bd9Sstevel@tonic-gate 	/* Reset the addrs in the composite header */
6677c478bd9Sstevel@tonic-gate 	sctp_faddr2hdraddr(fp, sctp);
6687c478bd9Sstevel@tonic-gate 	sctp_set_ulp_prop(sctp);
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	return (0);
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate /* Handy on off switch for socket option processing. */
6747c478bd9Sstevel@tonic-gate #define	ONOFF(x)	((x) == 0 ? 0 : 1)
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate  * SCTP routine to get the values of options.
6787c478bd9Sstevel@tonic-gate  */
6797c478bd9Sstevel@tonic-gate int
6807c478bd9Sstevel@tonic-gate sctp_get_opt(sctp_t *sctp, int level, int name, void *ptr, socklen_t *optlen)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	int	*i1 = (int *)ptr;
6837c478bd9Sstevel@tonic-gate 	int	retval = 0;
6847c478bd9Sstevel@tonic-gate 	int	buflen = *optlen;
68543d18f1cSpriyanka 	conn_t		*connp = sctp->sctp_connp;
6867c478bd9Sstevel@tonic-gate 	ip6_pkt_t	*ipp = &sctp->sctp_sticky_ipp;
6877c478bd9Sstevel@tonic-gate 	/* In most cases, the return buffer is just an int */
6887c478bd9Sstevel@tonic-gate 	*optlen = sizeof (int32_t);
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 	switch (level) {
6937c478bd9Sstevel@tonic-gate 	case SOL_SOCKET:
6947c478bd9Sstevel@tonic-gate 		switch (name) {
6957c478bd9Sstevel@tonic-gate 		case SO_LINGER:	{
6967c478bd9Sstevel@tonic-gate 			struct linger *lgr = (struct linger *)ptr;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 			lgr->l_onoff = sctp->sctp_linger ? SO_LINGER : 0;
6997c478bd9Sstevel@tonic-gate 			lgr->l_linger = TICK_TO_MSEC(sctp->sctp_lingertime);
7007c478bd9Sstevel@tonic-gate 			*optlen = sizeof (struct linger);
7017c478bd9Sstevel@tonic-gate 			break;
7027c478bd9Sstevel@tonic-gate 		}
7037c478bd9Sstevel@tonic-gate 		case SO_DEBUG:
7047c478bd9Sstevel@tonic-gate 			*i1 = sctp->sctp_debug ? SO_DEBUG : 0;
7057c478bd9Sstevel@tonic-gate 			break;
7067c478bd9Sstevel@tonic-gate 		case SO_DONTROUTE:
7077c478bd9Sstevel@tonic-gate 			*i1 = sctp->sctp_dontroute ? SO_DONTROUTE : 0;
7087c478bd9Sstevel@tonic-gate 			break;
7097c478bd9Sstevel@tonic-gate 		case SO_USELOOPBACK:
7107c478bd9Sstevel@tonic-gate 			*i1 = sctp->sctp_useloopback ? SO_USELOOPBACK : 0;
7117c478bd9Sstevel@tonic-gate 			break;
7127c478bd9Sstevel@tonic-gate 		case SO_BROADCAST:
7137c478bd9Sstevel@tonic-gate 			*i1 = sctp->sctp_broadcast ? SO_BROADCAST : 0;
7147c478bd9Sstevel@tonic-gate 			break;
7157c478bd9Sstevel@tonic-gate 		case SO_REUSEADDR:
7167c478bd9Sstevel@tonic-gate 			*i1 = sctp->sctp_reuseaddr ? SO_REUSEADDR : 0;
7177c478bd9Sstevel@tonic-gate 			break;
7187c478bd9Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
7197c478bd9Sstevel@tonic-gate 			*i1 = sctp->sctp_dgram_errind ? SO_DGRAM_ERRIND : 0;
7207c478bd9Sstevel@tonic-gate 			break;
7217c478bd9Sstevel@tonic-gate 		case SO_SNDBUF:
7227c478bd9Sstevel@tonic-gate 			*i1 = sctp->sctp_xmit_hiwater;
7237c478bd9Sstevel@tonic-gate 			break;
7247c478bd9Sstevel@tonic-gate 		case SO_RCVBUF:
7257c478bd9Sstevel@tonic-gate 			*i1 = sctp->sctp_rwnd;
7267c478bd9Sstevel@tonic-gate 			break;
727*45916cd2Sjpk 		case SO_MAC_EXEMPT:
728*45916cd2Sjpk 			*i1 = sctp->sctp_mac_exempt ? SO_MAC_EXEMPT : 0;
729*45916cd2Sjpk 			break;
7307c478bd9Sstevel@tonic-gate 		default:
7317c478bd9Sstevel@tonic-gate 			retval = EINVAL;
7327c478bd9Sstevel@tonic-gate 			break;
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 		break;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	case IPPROTO_SCTP:
7377c478bd9Sstevel@tonic-gate 		switch (name) {
7387c478bd9Sstevel@tonic-gate 		case SCTP_RTOINFO:
7397c478bd9Sstevel@tonic-gate 			if (buflen < sizeof (struct sctp_rtoinfo)) {
7407c478bd9Sstevel@tonic-gate 				retval = EINVAL;
7417c478bd9Sstevel@tonic-gate 				break;
7427c478bd9Sstevel@tonic-gate 			}
7437c478bd9Sstevel@tonic-gate 			*optlen = sctp_get_rtoinfo(sctp, ptr);
7447c478bd9Sstevel@tonic-gate 			break;
7457c478bd9Sstevel@tonic-gate 		case SCTP_ASSOCINFO:
7467c478bd9Sstevel@tonic-gate 			if (buflen < sizeof (struct sctp_assocparams)) {
7477c478bd9Sstevel@tonic-gate 				retval = EINVAL;
7487c478bd9Sstevel@tonic-gate 				break;
7497c478bd9Sstevel@tonic-gate 			}
7507c478bd9Sstevel@tonic-gate 			*optlen = sctp_get_assocparams(sctp, ptr);
7517c478bd9Sstevel@tonic-gate 			break;
7527c478bd9Sstevel@tonic-gate 		case SCTP_INITMSG:
7537c478bd9Sstevel@tonic-gate 			if (buflen < sizeof (struct sctp_initmsg)) {
7547c478bd9Sstevel@tonic-gate 				retval = EINVAL;
7557c478bd9Sstevel@tonic-gate 				break;
7567c478bd9Sstevel@tonic-gate 			}
7577c478bd9Sstevel@tonic-gate 			*optlen = sctp_get_initmsg(sctp, ptr);
7587c478bd9Sstevel@tonic-gate 			break;
7597c478bd9Sstevel@tonic-gate 		case SCTP_NODELAY:
7607c478bd9Sstevel@tonic-gate 			*i1 = sctp->sctp_ndelay;
7617c478bd9Sstevel@tonic-gate 			break;
7627c478bd9Sstevel@tonic-gate 		case SCTP_AUTOCLOSE:
7637c478bd9Sstevel@tonic-gate 			*i1 = TICK_TO_SEC(sctp->sctp_autoclose);
7647c478bd9Sstevel@tonic-gate 			break;
7657c478bd9Sstevel@tonic-gate 		case SCTP_ADAPTION_LAYER:
7667c478bd9Sstevel@tonic-gate 			if (buflen < sizeof (struct sctp_setadaption)) {
7677c478bd9Sstevel@tonic-gate 				retval = EINVAL;
7687c478bd9Sstevel@tonic-gate 				break;
7697c478bd9Sstevel@tonic-gate 			}
7707c478bd9Sstevel@tonic-gate 			((struct sctp_setadaption *)ptr)->ssb_adaption_ind =
7717c478bd9Sstevel@tonic-gate 			    sctp->sctp_tx_adaption_code;
7727c478bd9Sstevel@tonic-gate 			break;
7737c478bd9Sstevel@tonic-gate 		case SCTP_PEER_ADDR_PARAMS:
7747c478bd9Sstevel@tonic-gate 			if (buflen < sizeof (struct sctp_paddrparams)) {
7757c478bd9Sstevel@tonic-gate 				retval = EINVAL;
7767c478bd9Sstevel@tonic-gate 				break;
7777c478bd9Sstevel@tonic-gate 			}
7787c478bd9Sstevel@tonic-gate 			*optlen = sctp_get_peer_addr_params(sctp, ptr);
7797c478bd9Sstevel@tonic-gate 			break;
7807c478bd9Sstevel@tonic-gate 		case SCTP_DEFAULT_SEND_PARAM:
7817c478bd9Sstevel@tonic-gate 			if (buflen < sizeof (struct sctp_sndrcvinfo)) {
7827c478bd9Sstevel@tonic-gate 				retval = EINVAL;
7837c478bd9Sstevel@tonic-gate 				break;
7847c478bd9Sstevel@tonic-gate 			}
7857c478bd9Sstevel@tonic-gate 			*optlen = sctp_get_def_send_params(sctp, ptr);
7867c478bd9Sstevel@tonic-gate 			break;
7877c478bd9Sstevel@tonic-gate 		case SCTP_EVENTS: {
7887c478bd9Sstevel@tonic-gate 			struct sctp_event_subscribe *ev;
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 			if (buflen < sizeof (struct sctp_event_subscribe)) {
7917c478bd9Sstevel@tonic-gate 				retval = EINVAL;
7927c478bd9Sstevel@tonic-gate 				break;
7937c478bd9Sstevel@tonic-gate 			}
7947c478bd9Sstevel@tonic-gate 			ev = (struct sctp_event_subscribe *)ptr;
7957c478bd9Sstevel@tonic-gate 			ev->sctp_data_io_event =
7967c478bd9Sstevel@tonic-gate 			    ONOFF(sctp->sctp_recvsndrcvinfo);
7977c478bd9Sstevel@tonic-gate 			ev->sctp_association_event =
7987c478bd9Sstevel@tonic-gate 			    ONOFF(sctp->sctp_recvassocevnt);
7997c478bd9Sstevel@tonic-gate 			ev->sctp_address_event =
8007c478bd9Sstevel@tonic-gate 			    ONOFF(sctp->sctp_recvpathevnt);
8017c478bd9Sstevel@tonic-gate 			ev->sctp_send_failure_event =
8027c478bd9Sstevel@tonic-gate 			    ONOFF(sctp->sctp_recvsendfailevnt);
8037c478bd9Sstevel@tonic-gate 			ev->sctp_peer_error_event =
8047c478bd9Sstevel@tonic-gate 			    ONOFF(sctp->sctp_recvpeererr);
8057c478bd9Sstevel@tonic-gate 			ev->sctp_shutdown_event =
8067c478bd9Sstevel@tonic-gate 			    ONOFF(sctp->sctp_recvshutdownevnt);
8077c478bd9Sstevel@tonic-gate 			ev->sctp_partial_delivery_event =
8087c478bd9Sstevel@tonic-gate 			    ONOFF(sctp->sctp_recvpdevnt);
8097c478bd9Sstevel@tonic-gate 			ev->sctp_adaption_layer_event =
8107c478bd9Sstevel@tonic-gate 				ONOFF(sctp->sctp_recvalevnt);
8117c478bd9Sstevel@tonic-gate 			*optlen = sizeof (struct sctp_event_subscribe);
8127c478bd9Sstevel@tonic-gate 			break;
8137c478bd9Sstevel@tonic-gate 		}
8147c478bd9Sstevel@tonic-gate 		case SCTP_STATUS:
8157c478bd9Sstevel@tonic-gate 			if (buflen < sizeof (struct sctp_status)) {
8167c478bd9Sstevel@tonic-gate 				retval = EINVAL;
8177c478bd9Sstevel@tonic-gate 				break;
8187c478bd9Sstevel@tonic-gate 			}
8197c478bd9Sstevel@tonic-gate 			*optlen = sctp_get_status(sctp, ptr);
8207c478bd9Sstevel@tonic-gate 			break;
8217c478bd9Sstevel@tonic-gate 		case SCTP_GET_PEER_ADDR_INFO:
8227c478bd9Sstevel@tonic-gate 			if (buflen < sizeof (struct sctp_paddrinfo)) {
8237c478bd9Sstevel@tonic-gate 				retval = EINVAL;
8247c478bd9Sstevel@tonic-gate 				break;
8257c478bd9Sstevel@tonic-gate 			}
8267c478bd9Sstevel@tonic-gate 			retval = sctp_get_paddrinfo(sctp, ptr, optlen);
8277c478bd9Sstevel@tonic-gate 			break;
8287c478bd9Sstevel@tonic-gate 		case SCTP_GET_NLADDRS:
8297c478bd9Sstevel@tonic-gate 			*(int32_t *)ptr = sctp->sctp_nsaddrs;
8307c478bd9Sstevel@tonic-gate 			break;
8317c478bd9Sstevel@tonic-gate 		case SCTP_GET_LADDRS: {
8327c478bd9Sstevel@tonic-gate 			int addr_cnt;
8337c478bd9Sstevel@tonic-gate 			int addr_size;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 			if (sctp->sctp_family == AF_INET)
8367c478bd9Sstevel@tonic-gate 				addr_size = sizeof (struct sockaddr_in);
8377c478bd9Sstevel@tonic-gate 			else
8387c478bd9Sstevel@tonic-gate 				addr_size = sizeof (struct sockaddr_in6);
8397c478bd9Sstevel@tonic-gate 			addr_cnt = buflen / addr_size;
8407c478bd9Sstevel@tonic-gate 			retval = sctp_getmyaddrs(sctp, ptr, &addr_cnt);
8417c478bd9Sstevel@tonic-gate 			if (retval == 0)
8427c478bd9Sstevel@tonic-gate 				*optlen = addr_cnt * addr_size;
8437c478bd9Sstevel@tonic-gate 			break;
8447c478bd9Sstevel@tonic-gate 		}
8457c478bd9Sstevel@tonic-gate 		case SCTP_GET_NPADDRS: {
8467c478bd9Sstevel@tonic-gate 			int i;
8477c478bd9Sstevel@tonic-gate 			sctp_faddr_t *fp;
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 			for (i = 0, fp = sctp->sctp_faddrs; fp != NULL;
8507c478bd9Sstevel@tonic-gate 			    i++, fp = fp->next)
8517c478bd9Sstevel@tonic-gate 				;
8527c478bd9Sstevel@tonic-gate 			*(int32_t *)ptr = i;
8537c478bd9Sstevel@tonic-gate 			break;
8547c478bd9Sstevel@tonic-gate 		}
8557c478bd9Sstevel@tonic-gate 		case SCTP_GET_PADDRS: {
8567c478bd9Sstevel@tonic-gate 			int addr_cnt;
8577c478bd9Sstevel@tonic-gate 			int addr_size;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 			if (sctp->sctp_family == AF_INET)
8607c478bd9Sstevel@tonic-gate 				addr_size = sizeof (struct sockaddr_in);
8617c478bd9Sstevel@tonic-gate 			else
8627c478bd9Sstevel@tonic-gate 				addr_size = sizeof (struct sockaddr_in6);
8637c478bd9Sstevel@tonic-gate 			addr_cnt = buflen / addr_size;
8647c478bd9Sstevel@tonic-gate 			retval = sctp_getpeeraddrs(sctp, ptr, &addr_cnt);
8657c478bd9Sstevel@tonic-gate 			if (retval == 0)
8667c478bd9Sstevel@tonic-gate 				*optlen = addr_cnt * addr_size;
8677c478bd9Sstevel@tonic-gate 			break;
8687c478bd9Sstevel@tonic-gate 		}
8697c478bd9Sstevel@tonic-gate 		case SCTP_PRSCTP:
8707c478bd9Sstevel@tonic-gate 			*i1 = sctp->sctp_prsctp_aware ? 1 : 0;
8717c478bd9Sstevel@tonic-gate 			break;
8727c478bd9Sstevel@tonic-gate 		case SCTP_I_WANT_MAPPED_V4_ADDR:
8737c478bd9Sstevel@tonic-gate 		case SCTP_MAXSEG:
8747c478bd9Sstevel@tonic-gate 		case SCTP_DISABLE_FRAGMENTS:
8757c478bd9Sstevel@tonic-gate 			/* Not yet supported. */
8767c478bd9Sstevel@tonic-gate 		default:
8777c478bd9Sstevel@tonic-gate 			retval = EINVAL;
8787c478bd9Sstevel@tonic-gate 			break;
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 		break;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	case IPPROTO_IP:
8837c478bd9Sstevel@tonic-gate 		if (sctp->sctp_family != AF_INET) {
8847c478bd9Sstevel@tonic-gate 			retval = EINVAL;
8857c478bd9Sstevel@tonic-gate 			break;
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 		switch (name) {
8887c478bd9Sstevel@tonic-gate 		case IP_OPTIONS:
8897c478bd9Sstevel@tonic-gate 		case T_IP_OPTIONS: {
8907c478bd9Sstevel@tonic-gate 			/*
8917c478bd9Sstevel@tonic-gate 			 * This is compatible with BSD in that in only return
8927c478bd9Sstevel@tonic-gate 			 * the reverse source route with the final destination
8937c478bd9Sstevel@tonic-gate 			 * as the last entry. The first 4 bytes of the option
8947c478bd9Sstevel@tonic-gate 			 * will contain the final destination. Allocate a
8957c478bd9Sstevel@tonic-gate 			 * buffer large enough to hold all the options, we
8967c478bd9Sstevel@tonic-gate 			 * add IP_ADDR_LEN to SCTP_MAX_IP_OPTIONS_LENGTH since
897*45916cd2Sjpk 			 * ip_opt_get_user() adds the final destination
8987c478bd9Sstevel@tonic-gate 			 * at the start.
8997c478bd9Sstevel@tonic-gate 			 */
9007c478bd9Sstevel@tonic-gate 			char	*opt_ptr;
9017c478bd9Sstevel@tonic-gate 			int	opt_len;
9027c478bd9Sstevel@tonic-gate 			uchar_t	obuf[SCTP_MAX_IP_OPTIONS_LENGTH + IP_ADDR_LEN];
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 			opt_ptr = (char *)sctp->sctp_ipha +
9057c478bd9Sstevel@tonic-gate 			    IP_SIMPLE_HDR_LENGTH;
9067c478bd9Sstevel@tonic-gate 			opt_len = (char *)sctp->sctp_sctph - opt_ptr;
9077c478bd9Sstevel@tonic-gate 			/* Caller ensures enough space */
9087c478bd9Sstevel@tonic-gate 			if (opt_len > 0) {
9097c478bd9Sstevel@tonic-gate 				/*
9107c478bd9Sstevel@tonic-gate 				 * TODO: Do we have to handle getsockopt on an
9117c478bd9Sstevel@tonic-gate 				 * initiator as well?
9127c478bd9Sstevel@tonic-gate 				 */
913*45916cd2Sjpk 				opt_len = ip_opt_get_user(sctp->sctp_ipha,
9147c478bd9Sstevel@tonic-gate 				    obuf);
9157c478bd9Sstevel@tonic-gate 				ASSERT(opt_len <= sizeof (obuf));
9167c478bd9Sstevel@tonic-gate 			} else {
9177c478bd9Sstevel@tonic-gate 				opt_len = 0;
9187c478bd9Sstevel@tonic-gate 			}
9197c478bd9Sstevel@tonic-gate 			if (buflen < opt_len) {
9207c478bd9Sstevel@tonic-gate 				/* Silently truncate */
9217c478bd9Sstevel@tonic-gate 				opt_len = buflen;
9227c478bd9Sstevel@tonic-gate 			}
9237c478bd9Sstevel@tonic-gate 			*optlen = opt_len;
9247c478bd9Sstevel@tonic-gate 			bcopy(obuf, ptr, opt_len);
9257c478bd9Sstevel@tonic-gate 			break;
9267c478bd9Sstevel@tonic-gate 		}
9277c478bd9Sstevel@tonic-gate 		case IP_TOS:
9287c478bd9Sstevel@tonic-gate 		case T_IP_TOS:
9297c478bd9Sstevel@tonic-gate 			*i1 = (int)sctp->sctp_ipha->ipha_type_of_service;
9307c478bd9Sstevel@tonic-gate 			break;
9317c478bd9Sstevel@tonic-gate 		case IP_TTL:
9327c478bd9Sstevel@tonic-gate 			*i1 = (int)sctp->sctp_ipha->ipha_ttl;
9337c478bd9Sstevel@tonic-gate 			break;
93443d18f1cSpriyanka 		case IP_NEXTHOP:
93543d18f1cSpriyanka 			if (connp->conn_nexthop_set) {
93643d18f1cSpriyanka 				*(ipaddr_t *)ptr = connp->conn_nexthop_v4;
93743d18f1cSpriyanka 				*optlen = sizeof (ipaddr_t);
93843d18f1cSpriyanka 			} else {
93943d18f1cSpriyanka 				*optlen = 0;
94043d18f1cSpriyanka 			}
94143d18f1cSpriyanka 			break;
9427c478bd9Sstevel@tonic-gate 		default:
9437c478bd9Sstevel@tonic-gate 			retval = EINVAL;
9447c478bd9Sstevel@tonic-gate 			break;
9457c478bd9Sstevel@tonic-gate 		}
9467c478bd9Sstevel@tonic-gate 		break;
9477c478bd9Sstevel@tonic-gate 	case IPPROTO_IPV6:
9487c478bd9Sstevel@tonic-gate 		if (sctp->sctp_family != AF_INET6) {
9497c478bd9Sstevel@tonic-gate 			retval = EINVAL;
9507c478bd9Sstevel@tonic-gate 			break;
9517c478bd9Sstevel@tonic-gate 		}
9527c478bd9Sstevel@tonic-gate 		switch (name) {
9537c478bd9Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
9547c478bd9Sstevel@tonic-gate 			*i1 = (unsigned int) sctp->sctp_ip6h->ip6_hops;
9557c478bd9Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
9567c478bd9Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
9577c478bd9Sstevel@tonic-gate 			if (sctp->sctp_ipv6_recvancillary &
9587c478bd9Sstevel@tonic-gate 			    SCTP_IPV6_RECVPKTINFO) {
9597c478bd9Sstevel@tonic-gate 				*i1 = 1;
9607c478bd9Sstevel@tonic-gate 			} else {
9617c478bd9Sstevel@tonic-gate 				*i1 = 0;
9627c478bd9Sstevel@tonic-gate 			}
9637c478bd9Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
9647c478bd9Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
9657c478bd9Sstevel@tonic-gate 			if (sctp->sctp_ipv6_recvancillary &
9667c478bd9Sstevel@tonic-gate 			    SCTP_IPV6_RECVHOPLIMIT) {
9677c478bd9Sstevel@tonic-gate 				*i1 = 1;
9687c478bd9Sstevel@tonic-gate 			} else {
9697c478bd9Sstevel@tonic-gate 				*i1 = 0;
9707c478bd9Sstevel@tonic-gate 			}
9717c478bd9Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
9727c478bd9Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
9737c478bd9Sstevel@tonic-gate 			if (sctp->sctp_ipv6_recvancillary &
9747c478bd9Sstevel@tonic-gate 			    SCTP_IPV6_RECVHOPOPTS) {
9757c478bd9Sstevel@tonic-gate 				*i1 = 1;
9767c478bd9Sstevel@tonic-gate 			} else {
9777c478bd9Sstevel@tonic-gate 				*i1 = 0;
9787c478bd9Sstevel@tonic-gate 			}
9797c478bd9Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
9807c478bd9Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
9817c478bd9Sstevel@tonic-gate 			if (sctp->sctp_ipv6_recvancillary &
9827c478bd9Sstevel@tonic-gate 			    SCTP_IPV6_RECVDSTOPTS) {
9837c478bd9Sstevel@tonic-gate 				*i1 = 1;
9847c478bd9Sstevel@tonic-gate 			} else {
9857c478bd9Sstevel@tonic-gate 				*i1 = 0;
9867c478bd9Sstevel@tonic-gate 			}
9877c478bd9Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
9887c478bd9Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
9897c478bd9Sstevel@tonic-gate 			if (sctp->sctp_ipv6_recvancillary &
9907c478bd9Sstevel@tonic-gate 			    SCTP_IPV6_RECVRTHDR) {
9917c478bd9Sstevel@tonic-gate 				*i1 = 1;
9927c478bd9Sstevel@tonic-gate 			} else {
9937c478bd9Sstevel@tonic-gate 				*i1 = 0;
9947c478bd9Sstevel@tonic-gate 			}
9957c478bd9Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
9967c478bd9Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
9977c478bd9Sstevel@tonic-gate 			if (sctp->sctp_ipv6_recvancillary &
9987c478bd9Sstevel@tonic-gate 			    SCTP_IPV6_RECVRTDSTOPTS) {
9997c478bd9Sstevel@tonic-gate 				*i1 = 1;
10007c478bd9Sstevel@tonic-gate 			} else {
10017c478bd9Sstevel@tonic-gate 				*i1 = 0;
10027c478bd9Sstevel@tonic-gate 			}
10037c478bd9Sstevel@tonic-gate 			break;	/* goto sizeof (int) option return */
10047c478bd9Sstevel@tonic-gate 		case IPV6_PKTINFO: {
10057c478bd9Sstevel@tonic-gate 			struct in6_pktinfo *pkti;
10067c478bd9Sstevel@tonic-gate 
10077c478bd9Sstevel@tonic-gate 			if (buflen < sizeof (struct in6_pktinfo)) {
10087c478bd9Sstevel@tonic-gate 				retval = EINVAL;
10097c478bd9Sstevel@tonic-gate 				break;
10107c478bd9Sstevel@tonic-gate 			}
10117c478bd9Sstevel@tonic-gate 			pkti = (struct in6_pktinfo *)ptr;
10127c478bd9Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_IFINDEX)
10137c478bd9Sstevel@tonic-gate 				pkti->ipi6_ifindex = ipp->ipp_ifindex;
10147c478bd9Sstevel@tonic-gate 			else
10157c478bd9Sstevel@tonic-gate 				pkti->ipi6_ifindex = 0;
10167c478bd9Sstevel@tonic-gate 			if (ipp->ipp_fields & IPPF_ADDR)
10177c478bd9Sstevel@tonic-gate 				pkti->ipi6_addr = ipp->ipp_addr;
10187c478bd9Sstevel@tonic-gate 			else
10197c478bd9Sstevel@tonic-gate 				pkti->ipi6_addr = ipv6_all_zeros;
10207c478bd9Sstevel@tonic-gate 			*optlen = sizeof (struct in6_pktinfo);
10217c478bd9Sstevel@tonic-gate 			break;
10227c478bd9Sstevel@tonic-gate 		}
10237c478bd9Sstevel@tonic-gate 		case IPV6_NEXTHOP: {
10247c478bd9Sstevel@tonic-gate 			sin6_t *sin6;
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 			if (buflen < sizeof (sin6_t)) {
10277c478bd9Sstevel@tonic-gate 				retval = EINVAL;
10287c478bd9Sstevel@tonic-gate 				break;
10297c478bd9Sstevel@tonic-gate 			}
10307c478bd9Sstevel@tonic-gate 			sin6 = (sin6_t *)ptr;
10317c478bd9Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_NEXTHOP))
10327c478bd9Sstevel@tonic-gate 				break;
10337c478bd9Sstevel@tonic-gate 			*sin6 = sctp_sin6_null;
10347c478bd9Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
10357c478bd9Sstevel@tonic-gate 			sin6->sin6_addr = ipp->ipp_nexthop;
10367c478bd9Sstevel@tonic-gate 			*optlen = sizeof (sin6_t);
10377c478bd9Sstevel@tonic-gate 			break;
10387c478bd9Sstevel@tonic-gate 		}
1039*45916cd2Sjpk 		case IPV6_HOPOPTS: {
1040*45916cd2Sjpk 			int len;
1041*45916cd2Sjpk 
10427c478bd9Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_HOPOPTS))
10437c478bd9Sstevel@tonic-gate 				break;
1044*45916cd2Sjpk 			len = ipp->ipp_hopoptslen - sctp->sctp_v6label_len;
1045*45916cd2Sjpk 			if (len <= 0)
1046*45916cd2Sjpk 				break;
1047*45916cd2Sjpk 			if (buflen < len) {
10487c478bd9Sstevel@tonic-gate 				retval = EINVAL;
10497c478bd9Sstevel@tonic-gate 				break;
10507c478bd9Sstevel@tonic-gate 			}
1051*45916cd2Sjpk 			bcopy((char *)ipp->ipp_hopopts +
1052*45916cd2Sjpk 			    sctp->sctp_v6label_len, ptr, len);
1053*45916cd2Sjpk 			if (sctp->sctp_v6label_len > 0) {
1054*45916cd2Sjpk 				char *cptr = ptr;
1055*45916cd2Sjpk 
1056*45916cd2Sjpk 				/*
1057*45916cd2Sjpk 				 * If the label length is greater than zero,
1058*45916cd2Sjpk 				 * then we need to hide the label from user.
1059*45916cd2Sjpk 				 * Make it look as though a normal Hop-By-Hop
1060*45916cd2Sjpk 				 * Options Header is present here.
1061*45916cd2Sjpk 				 */
1062*45916cd2Sjpk 				cptr[0] = ((char *)ipp->ipp_hopopts)[0];
1063*45916cd2Sjpk 				cptr[1] = (len + 7) / 8 - 1;
1064*45916cd2Sjpk 			}
1065*45916cd2Sjpk 			*optlen = len;
10667c478bd9Sstevel@tonic-gate 			break;
1067*45916cd2Sjpk 		}
10687c478bd9Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS:
10697c478bd9Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTDSTOPTS))
10707c478bd9Sstevel@tonic-gate 				break;
10717c478bd9Sstevel@tonic-gate 			if (buflen < ipp->ipp_rtdstoptslen) {
10727c478bd9Sstevel@tonic-gate 				retval = EINVAL;
10737c478bd9Sstevel@tonic-gate 				break;
10747c478bd9Sstevel@tonic-gate 			}
10757c478bd9Sstevel@tonic-gate 			bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen);
10767c478bd9Sstevel@tonic-gate 			*optlen  = ipp->ipp_rtdstoptslen;
10777c478bd9Sstevel@tonic-gate 			break;
10787c478bd9Sstevel@tonic-gate 		case IPV6_RTHDR:
10797c478bd9Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_RTHDR))
10807c478bd9Sstevel@tonic-gate 				break;
10817c478bd9Sstevel@tonic-gate 			if (buflen < ipp->ipp_rthdrlen) {
10827c478bd9Sstevel@tonic-gate 				retval = EINVAL;
10837c478bd9Sstevel@tonic-gate 				break;
10847c478bd9Sstevel@tonic-gate 			}
10857c478bd9Sstevel@tonic-gate 			bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen);
10867c478bd9Sstevel@tonic-gate 			*optlen = ipp->ipp_rthdrlen;
10877c478bd9Sstevel@tonic-gate 			break;
10887c478bd9Sstevel@tonic-gate 		case IPV6_DSTOPTS:
10897c478bd9Sstevel@tonic-gate 			if (!(ipp->ipp_fields & IPPF_DSTOPTS))
10907c478bd9Sstevel@tonic-gate 				break;
10917c478bd9Sstevel@tonic-gate 			if (buflen < ipp->ipp_dstoptslen) {
10927c478bd9Sstevel@tonic-gate 				retval = EINVAL;
10937c478bd9Sstevel@tonic-gate 				break;
10947c478bd9Sstevel@tonic-gate 			}
10957c478bd9Sstevel@tonic-gate 			bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen);
10967c478bd9Sstevel@tonic-gate 			*optlen  = ipp->ipp_dstoptslen;
10977c478bd9Sstevel@tonic-gate 			break;
10987c478bd9Sstevel@tonic-gate 		case IPV6_V6ONLY:
10997c478bd9Sstevel@tonic-gate 			*i1 = sctp->sctp_connp->conn_ipv6_v6only;
11007c478bd9Sstevel@tonic-gate 			break;
11017c478bd9Sstevel@tonic-gate 		default:
11027c478bd9Sstevel@tonic-gate 			retval = EINVAL;
11037c478bd9Sstevel@tonic-gate 			break;
11047c478bd9Sstevel@tonic-gate 		}
11057c478bd9Sstevel@tonic-gate 		break;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	default:
11087c478bd9Sstevel@tonic-gate 		retval = EINVAL;
11097c478bd9Sstevel@tonic-gate 		break;
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
11127c478bd9Sstevel@tonic-gate 	return (retval);
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate int
11167c478bd9Sstevel@tonic-gate sctp_set_opt(sctp_t *sctp, int level, int name, const void *invalp,
11177c478bd9Sstevel@tonic-gate     socklen_t inlen)
11187c478bd9Sstevel@tonic-gate {
11197c478bd9Sstevel@tonic-gate 	ip6_pkt_t	*ipp = &sctp->sctp_sticky_ipp;
11207c478bd9Sstevel@tonic-gate 	int		*i1 = (int *)invalp;
11217c478bd9Sstevel@tonic-gate 	boolean_t	onoff;
11227c478bd9Sstevel@tonic-gate 	int		retval = 0, addrcnt;
11237c478bd9Sstevel@tonic-gate 	conn_t		*connp = sctp->sctp_connp;
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	/* In all cases, the size of the option must be bigger than int */
11267c478bd9Sstevel@tonic-gate 	if (inlen >= sizeof (int32_t)) {
11277c478bd9Sstevel@tonic-gate 		onoff = ONOFF(*i1);
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 	retval = 0;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	switch (level) {
11347c478bd9Sstevel@tonic-gate 	case SOL_SOCKET:
11357c478bd9Sstevel@tonic-gate 		if (inlen < sizeof (int32_t)) {
11367c478bd9Sstevel@tonic-gate 			retval = EINVAL;
11377c478bd9Sstevel@tonic-gate 			break;
11387c478bd9Sstevel@tonic-gate 		}
11397c478bd9Sstevel@tonic-gate 		switch (name) {
11407c478bd9Sstevel@tonic-gate 		case SO_LINGER: {
11417c478bd9Sstevel@tonic-gate 			struct linger *lgr;
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 			if (inlen != sizeof (struct linger)) {
11447c478bd9Sstevel@tonic-gate 				retval = EINVAL;
11457c478bd9Sstevel@tonic-gate 				break;
11467c478bd9Sstevel@tonic-gate 			}
11477c478bd9Sstevel@tonic-gate 			lgr = (struct linger *)invalp;
11487c478bd9Sstevel@tonic-gate 			if (lgr->l_onoff != 0) {
11497c478bd9Sstevel@tonic-gate 				sctp->sctp_linger = 1;
11507c478bd9Sstevel@tonic-gate 				sctp->sctp_lingertime = MSEC_TO_TICK(
11517c478bd9Sstevel@tonic-gate 				    lgr->l_linger);
11527c478bd9Sstevel@tonic-gate 			} else {
11537c478bd9Sstevel@tonic-gate 				sctp->sctp_linger = 0;
11547c478bd9Sstevel@tonic-gate 				sctp->sctp_lingertime = 0;
11557c478bd9Sstevel@tonic-gate 			}
11567c478bd9Sstevel@tonic-gate 			break;
11577c478bd9Sstevel@tonic-gate 		}
11587c478bd9Sstevel@tonic-gate 		case SO_DEBUG:
11597c478bd9Sstevel@tonic-gate 			sctp->sctp_debug = onoff;
11607c478bd9Sstevel@tonic-gate 			break;
11617c478bd9Sstevel@tonic-gate 		case SO_KEEPALIVE:
11627c478bd9Sstevel@tonic-gate 			break;
11637c478bd9Sstevel@tonic-gate 		case SO_DONTROUTE:
11647c478bd9Sstevel@tonic-gate 			/*
11657c478bd9Sstevel@tonic-gate 			 * SO_DONTROUTE, SO_USELOOPBACK and SO_BROADCAST are
11667c478bd9Sstevel@tonic-gate 			 * only of interest to IP.  We track them here only so
11677c478bd9Sstevel@tonic-gate 			 * that we can report their current value.
11687c478bd9Sstevel@tonic-gate 			 */
11697c478bd9Sstevel@tonic-gate 			sctp->sctp_dontroute = onoff;
11707c478bd9Sstevel@tonic-gate 			connp->conn_dontroute = onoff;
11717c478bd9Sstevel@tonic-gate 			break;
11727c478bd9Sstevel@tonic-gate 		case SO_USELOOPBACK:
11737c478bd9Sstevel@tonic-gate 			sctp->sctp_useloopback = onoff;
11747c478bd9Sstevel@tonic-gate 			connp->conn_loopback = onoff;
11757c478bd9Sstevel@tonic-gate 			break;
11767c478bd9Sstevel@tonic-gate 		case SO_BROADCAST:
11777c478bd9Sstevel@tonic-gate 			sctp->sctp_broadcast = onoff;
11787c478bd9Sstevel@tonic-gate 			connp->conn_broadcast = onoff;
11797c478bd9Sstevel@tonic-gate 			break;
11807c478bd9Sstevel@tonic-gate 		case SO_REUSEADDR:
11817c478bd9Sstevel@tonic-gate 			sctp->sctp_reuseaddr = onoff;
11827c478bd9Sstevel@tonic-gate 			connp->conn_reuseaddr = onoff;
11837c478bd9Sstevel@tonic-gate 			break;
11847c478bd9Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
11857c478bd9Sstevel@tonic-gate 			sctp->sctp_dgram_errind = onoff;
11867c478bd9Sstevel@tonic-gate 			break;
11877c478bd9Sstevel@tonic-gate 		case SO_SNDBUF:
11887c478bd9Sstevel@tonic-gate 			if (*i1 > sctp_max_buf) {
11897c478bd9Sstevel@tonic-gate 				retval = ENOBUFS;
11907c478bd9Sstevel@tonic-gate 				break;
11917c478bd9Sstevel@tonic-gate 			}
11927c478bd9Sstevel@tonic-gate 			if (*i1 < 0) {
11937c478bd9Sstevel@tonic-gate 				retval = EINVAL;
11947c478bd9Sstevel@tonic-gate 				break;
11957c478bd9Sstevel@tonic-gate 			}
11967c478bd9Sstevel@tonic-gate 			sctp->sctp_xmit_hiwater = *i1;
11977c478bd9Sstevel@tonic-gate 			if (sctp_snd_lowat_fraction != 0)
11987c478bd9Sstevel@tonic-gate 				sctp->sctp_xmit_lowater =
11997c478bd9Sstevel@tonic-gate 				    sctp->sctp_xmit_hiwater /
12007c478bd9Sstevel@tonic-gate 				    sctp_snd_lowat_fraction;
12017c478bd9Sstevel@tonic-gate 			break;
12027c478bd9Sstevel@tonic-gate 		case SO_RCVBUF:
12037c478bd9Sstevel@tonic-gate 			if (*i1 > sctp_max_buf) {
12047c478bd9Sstevel@tonic-gate 				retval = ENOBUFS;
12057c478bd9Sstevel@tonic-gate 				break;
12067c478bd9Sstevel@tonic-gate 			}
12077c478bd9Sstevel@tonic-gate 			/* Silently ignore zero */
12087c478bd9Sstevel@tonic-gate 			if (*i1 != 0) {
12097c478bd9Sstevel@tonic-gate 				/*
12107c478bd9Sstevel@tonic-gate 				 * Insist on a receive window that is at least
12117c478bd9Sstevel@tonic-gate 				 * sctp_recv_hiwat_minmss * MSS (default 4*MSS)
12127c478bd9Sstevel@tonic-gate 				 * to avoid funny interactions of Nagle
12137c478bd9Sstevel@tonic-gate 				 * algorithm, SWS avoidance and delayed
12147c478bd9Sstevel@tonic-gate 				 * acknowledgement.
12157c478bd9Sstevel@tonic-gate 				 */
12167c478bd9Sstevel@tonic-gate 				*i1 = MAX(*i1,
12177c478bd9Sstevel@tonic-gate 				    sctp_recv_hiwat_minmss * sctp->sctp_mss);
12187c478bd9Sstevel@tonic-gate 				sctp->sctp_rwnd = *i1;
12191d8c4025Svi 				sctp->sctp_irwnd = sctp->sctp_rwnd;
12207c478bd9Sstevel@tonic-gate 			}
12217c478bd9Sstevel@tonic-gate 			/*
12227c478bd9Sstevel@tonic-gate 			 * XXX should we return the rwnd here
12237c478bd9Sstevel@tonic-gate 			 * and sctp_opt_get ?
12247c478bd9Sstevel@tonic-gate 			 */
12257c478bd9Sstevel@tonic-gate 			break;
1226*45916cd2Sjpk 		case SO_MAC_EXEMPT:
1227*45916cd2Sjpk 			if (secpolicy_net_mac_aware(sctp->sctp_credp) != 0 ||
1228*45916cd2Sjpk 			    sctp->sctp_state >= SCTPS_BOUND) {
1229*45916cd2Sjpk 				retval = EACCES;
1230*45916cd2Sjpk 			} else {
1231*45916cd2Sjpk 				sctp->sctp_mac_exempt = onoff;
1232*45916cd2Sjpk 				connp->conn_mac_exempt = onoff;
1233*45916cd2Sjpk 			}
1234*45916cd2Sjpk 			break;
12357c478bd9Sstevel@tonic-gate 		default:
12367c478bd9Sstevel@tonic-gate 			retval = EINVAL;
12377c478bd9Sstevel@tonic-gate 			break;
12387c478bd9Sstevel@tonic-gate 		}
12397c478bd9Sstevel@tonic-gate 		break;
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	case IPPROTO_SCTP:
12427c478bd9Sstevel@tonic-gate 		if (inlen < sizeof (int32_t)) {
12437c478bd9Sstevel@tonic-gate 			retval = EINVAL;
12447c478bd9Sstevel@tonic-gate 			break;
12457c478bd9Sstevel@tonic-gate 		}
12467c478bd9Sstevel@tonic-gate 		switch (name) {
12477c478bd9Sstevel@tonic-gate 		case SCTP_RTOINFO:
12487c478bd9Sstevel@tonic-gate 			retval = sctp_set_rtoinfo(sctp, invalp, inlen);
12497c478bd9Sstevel@tonic-gate 			break;
12507c478bd9Sstevel@tonic-gate 		case SCTP_ASSOCINFO:
12517c478bd9Sstevel@tonic-gate 			retval = sctp_set_assocparams(sctp, invalp, inlen);
12527c478bd9Sstevel@tonic-gate 			break;
12537c478bd9Sstevel@tonic-gate 		case SCTP_INITMSG:
12547c478bd9Sstevel@tonic-gate 			retval = sctp_set_initmsg(sctp, invalp, inlen);
12557c478bd9Sstevel@tonic-gate 			break;
12567c478bd9Sstevel@tonic-gate 		case SCTP_NODELAY:
12577c478bd9Sstevel@tonic-gate 			sctp->sctp_ndelay = ONOFF(*i1);
12587c478bd9Sstevel@tonic-gate 			break;
12597c478bd9Sstevel@tonic-gate 		case SCTP_AUTOCLOSE:
12607c478bd9Sstevel@tonic-gate 			if (SEC_TO_TICK(*i1) < 0) {
12617c478bd9Sstevel@tonic-gate 				retval = EINVAL;
12627c478bd9Sstevel@tonic-gate 				break;
12637c478bd9Sstevel@tonic-gate 			}
12647c478bd9Sstevel@tonic-gate 			/* Convert the number of seconds to ticks. */
12657c478bd9Sstevel@tonic-gate 			sctp->sctp_autoclose = SEC_TO_TICK(*i1);
12667c478bd9Sstevel@tonic-gate 			sctp_heartbeat_timer(sctp);
12677c478bd9Sstevel@tonic-gate 			break;
12687c478bd9Sstevel@tonic-gate 		case SCTP_SET_PEER_PRIMARY_ADDR:
12697c478bd9Sstevel@tonic-gate 			retval = sctp_set_peerprim(sctp, invalp, inlen);
12707c478bd9Sstevel@tonic-gate 			break;
12717c478bd9Sstevel@tonic-gate 		case SCTP_PRIMARY_ADDR:
12727c478bd9Sstevel@tonic-gate 			retval = sctp_set_prim(sctp, invalp, inlen);
12737c478bd9Sstevel@tonic-gate 			break;
12747c478bd9Sstevel@tonic-gate 		case SCTP_ADAPTION_LAYER: {
12757c478bd9Sstevel@tonic-gate 			struct sctp_setadaption *ssb;
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 			if (inlen < sizeof (struct sctp_setadaption)) {
12787c478bd9Sstevel@tonic-gate 				retval = EINVAL;
12797c478bd9Sstevel@tonic-gate 				break;
12807c478bd9Sstevel@tonic-gate 			}
12817c478bd9Sstevel@tonic-gate 			ssb = (struct sctp_setadaption *)invalp;
12827c478bd9Sstevel@tonic-gate 			sctp->sctp_send_adaption = 1;
12837c478bd9Sstevel@tonic-gate 			sctp->sctp_tx_adaption_code = ssb->ssb_adaption_ind;
12847c478bd9Sstevel@tonic-gate 			break;
12857c478bd9Sstevel@tonic-gate 		}
12867c478bd9Sstevel@tonic-gate 		case SCTP_PEER_ADDR_PARAMS:
12877c478bd9Sstevel@tonic-gate 			retval = sctp_set_peer_addr_params(sctp, invalp,
12887c478bd9Sstevel@tonic-gate 			    inlen);
12897c478bd9Sstevel@tonic-gate 			break;
12907c478bd9Sstevel@tonic-gate 		case SCTP_DEFAULT_SEND_PARAM:
12917c478bd9Sstevel@tonic-gate 			retval = sctp_set_def_send_params(sctp, invalp, inlen);
12927c478bd9Sstevel@tonic-gate 			break;
12937c478bd9Sstevel@tonic-gate 		case SCTP_EVENTS: {
12947c478bd9Sstevel@tonic-gate 			struct sctp_event_subscribe *ev;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 			if (inlen < sizeof (struct sctp_event_subscribe)) {
12977c478bd9Sstevel@tonic-gate 				retval = EINVAL;
12987c478bd9Sstevel@tonic-gate 				break;
12997c478bd9Sstevel@tonic-gate 			}
13007c478bd9Sstevel@tonic-gate 			ev = (struct sctp_event_subscribe *)invalp;
13017c478bd9Sstevel@tonic-gate 			sctp->sctp_recvsndrcvinfo =
13027c478bd9Sstevel@tonic-gate 			    ONOFF(ev->sctp_data_io_event);
13037c478bd9Sstevel@tonic-gate 			sctp->sctp_recvassocevnt =
13047c478bd9Sstevel@tonic-gate 			    ONOFF(ev->sctp_association_event);
13057c478bd9Sstevel@tonic-gate 			sctp->sctp_recvpathevnt =
13067c478bd9Sstevel@tonic-gate 			    ONOFF(ev->sctp_address_event);
13077c478bd9Sstevel@tonic-gate 			sctp->sctp_recvsendfailevnt =
13087c478bd9Sstevel@tonic-gate 			    ONOFF(ev->sctp_send_failure_event);
13097c478bd9Sstevel@tonic-gate 			sctp->sctp_recvpeererr =
13107c478bd9Sstevel@tonic-gate 			    ONOFF(ev->sctp_peer_error_event);
13117c478bd9Sstevel@tonic-gate 			sctp->sctp_recvshutdownevnt =
13127c478bd9Sstevel@tonic-gate 			    ONOFF(ev->sctp_shutdown_event);
13137c478bd9Sstevel@tonic-gate 			sctp->sctp_recvpdevnt =
13147c478bd9Sstevel@tonic-gate 			    ONOFF(ev->sctp_partial_delivery_event);
13157c478bd9Sstevel@tonic-gate 			sctp->sctp_recvalevnt =
13167c478bd9Sstevel@tonic-gate 				ONOFF(ev->sctp_adaption_layer_event);
13177c478bd9Sstevel@tonic-gate 			break;
13187c478bd9Sstevel@tonic-gate 		}
13197c478bd9Sstevel@tonic-gate 		case SCTP_ADD_ADDR:
13207c478bd9Sstevel@tonic-gate 		case SCTP_REM_ADDR:
13217c478bd9Sstevel@tonic-gate 			/*
13227c478bd9Sstevel@tonic-gate 			 * The sctp_t has to be bound first before
13237c478bd9Sstevel@tonic-gate 			 * the address list can be changed.
13247c478bd9Sstevel@tonic-gate 			 */
13257c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state < SCTPS_BOUND) {
13267c478bd9Sstevel@tonic-gate 				retval = EINVAL;
13277c478bd9Sstevel@tonic-gate 				break;
13287c478bd9Sstevel@tonic-gate 			}
13297c478bd9Sstevel@tonic-gate 			if (sctp->sctp_family == AF_INET) {
13307c478bd9Sstevel@tonic-gate 				addrcnt = inlen / sizeof (struct sockaddr_in);
13317c478bd9Sstevel@tonic-gate 			} else {
13327c478bd9Sstevel@tonic-gate 				ASSERT(sctp->sctp_family == AF_INET6);
13337c478bd9Sstevel@tonic-gate 				addrcnt = inlen / sizeof (struct sockaddr_in6);
13347c478bd9Sstevel@tonic-gate 			}
13357c478bd9Sstevel@tonic-gate 			if (name == SCTP_ADD_ADDR) {
13367c478bd9Sstevel@tonic-gate 				retval = sctp_bind_add(sctp, invalp, addrcnt,
13371d8c4025Svi 				    B_TRUE, sctp->sctp_lport);
13387c478bd9Sstevel@tonic-gate 			} else {
13397c478bd9Sstevel@tonic-gate 				retval = sctp_bind_del(sctp, invalp, addrcnt,
13407c478bd9Sstevel@tonic-gate 				    B_TRUE);
13417c478bd9Sstevel@tonic-gate 			}
13427c478bd9Sstevel@tonic-gate 			break;
13437c478bd9Sstevel@tonic-gate 		case SCTP_UC_SWAP: {
13447c478bd9Sstevel@tonic-gate 			struct sctp_uc_swap *us;
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 			/*
13477c478bd9Sstevel@tonic-gate 			 * Change handle & upcalls.
13487c478bd9Sstevel@tonic-gate 			 */
13497c478bd9Sstevel@tonic-gate 			if (inlen < sizeof (*us)) {
13507c478bd9Sstevel@tonic-gate 				retval = EINVAL;
13517c478bd9Sstevel@tonic-gate 				break;
13527c478bd9Sstevel@tonic-gate 			}
13537c478bd9Sstevel@tonic-gate 			us = (struct sctp_uc_swap *)invalp;
13547c478bd9Sstevel@tonic-gate 			sctp->sctp_ulpd = us->sus_handle;
13557c478bd9Sstevel@tonic-gate 			bcopy(us->sus_upcalls, &sctp->sctp_upcalls,
13567c478bd9Sstevel@tonic-gate 			    sizeof (sctp_upcalls_t));
13577c478bd9Sstevel@tonic-gate 			break;
13587c478bd9Sstevel@tonic-gate 		}
13597c478bd9Sstevel@tonic-gate 		case SCTP_PRSCTP:
13607c478bd9Sstevel@tonic-gate 			sctp->sctp_prsctp_aware = onoff;
13617c478bd9Sstevel@tonic-gate 			break;
13627c478bd9Sstevel@tonic-gate 		case SCTP_I_WANT_MAPPED_V4_ADDR:
13637c478bd9Sstevel@tonic-gate 		case SCTP_MAXSEG:
13647c478bd9Sstevel@tonic-gate 		case SCTP_DISABLE_FRAGMENTS:
13657c478bd9Sstevel@tonic-gate 			/* Not yet supported. */
13667c478bd9Sstevel@tonic-gate 		default:
13677c478bd9Sstevel@tonic-gate 			retval = EINVAL;
13687c478bd9Sstevel@tonic-gate 			break;
13697c478bd9Sstevel@tonic-gate 		}
13707c478bd9Sstevel@tonic-gate 		break;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	case IPPROTO_IP:
13737c478bd9Sstevel@tonic-gate 		if (sctp->sctp_family != AF_INET) {
13747c478bd9Sstevel@tonic-gate 			retval = ENOPROTOOPT;
13757c478bd9Sstevel@tonic-gate 			break;
13767c478bd9Sstevel@tonic-gate 		}
13777c478bd9Sstevel@tonic-gate 		if ((name != IP_OPTIONS) && (inlen < sizeof (int32_t))) {
13787c478bd9Sstevel@tonic-gate 			retval = EINVAL;
13797c478bd9Sstevel@tonic-gate 			break;
13807c478bd9Sstevel@tonic-gate 		}
13817c478bd9Sstevel@tonic-gate 		switch (name) {
13827c478bd9Sstevel@tonic-gate 		case IP_OPTIONS:
13837c478bd9Sstevel@tonic-gate 		case T_IP_OPTIONS:
1384*45916cd2Sjpk 			retval = sctp_opt_set_header(sctp, invalp, inlen);
13857c478bd9Sstevel@tonic-gate 			break;
13867c478bd9Sstevel@tonic-gate 		case IP_TOS:
13877c478bd9Sstevel@tonic-gate 		case T_IP_TOS:
13887c478bd9Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_type_of_service = (uchar_t)*i1;
13897c478bd9Sstevel@tonic-gate 			break;
13907c478bd9Sstevel@tonic-gate 		case IP_TTL:
13917c478bd9Sstevel@tonic-gate 			sctp->sctp_ipha->ipha_ttl = (uchar_t)*i1;
13927c478bd9Sstevel@tonic-gate 			break;
13937c478bd9Sstevel@tonic-gate 		case IP_SEC_OPT:
13947c478bd9Sstevel@tonic-gate 			/*
13957c478bd9Sstevel@tonic-gate 			 * We should not allow policy setting after
13967c478bd9Sstevel@tonic-gate 			 * we start listening for connections.
13977c478bd9Sstevel@tonic-gate 			 */
13987c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state >= SCTPS_LISTEN) {
13997c478bd9Sstevel@tonic-gate 				retval = EINVAL;
14007c478bd9Sstevel@tonic-gate 			} else {
14017c478bd9Sstevel@tonic-gate 				retval = ipsec_set_req(sctp->sctp_credp,
14027c478bd9Sstevel@tonic-gate 				    sctp->sctp_connp, (ipsec_req_t *)invalp);
14037c478bd9Sstevel@tonic-gate 			}
14047c478bd9Sstevel@tonic-gate 			break;
14057c478bd9Sstevel@tonic-gate 		/* IP level options */
14067c478bd9Sstevel@tonic-gate 		case IP_RECVIF:
14077c478bd9Sstevel@tonic-gate 			connp->conn_recvif = onoff;
14087c478bd9Sstevel@tonic-gate 			break;
14097c478bd9Sstevel@tonic-gate 		case IP_RECVSLLA:
14107c478bd9Sstevel@tonic-gate 			connp->conn_recvslla = onoff;
14117c478bd9Sstevel@tonic-gate 			break;
14127c478bd9Sstevel@tonic-gate 		case IP_UNSPEC_SRC:
14137c478bd9Sstevel@tonic-gate 			connp->conn_unspec_src = onoff;
14147c478bd9Sstevel@tonic-gate 			break;
141543d18f1cSpriyanka 		case IP_NEXTHOP: {
141643d18f1cSpriyanka 			ipaddr_t addr = *i1;
141743d18f1cSpriyanka 			ipif_t *ipif = NULL;
141843d18f1cSpriyanka 			ill_t *ill;
141943d18f1cSpriyanka 
142043d18f1cSpriyanka 			if (secpolicy_net(CRED(), OP_CONFIG, B_TRUE) == 0) {
142143d18f1cSpriyanka 				ipif =
142243d18f1cSpriyanka 				    ipif_lookup_onlink_addr(addr,
142343d18f1cSpriyanka 				    connp->conn_zoneid);
142443d18f1cSpriyanka 				if (ipif == NULL) {
142543d18f1cSpriyanka 					retval = EHOSTUNREACH;
142643d18f1cSpriyanka 					break;
142743d18f1cSpriyanka 				}
142843d18f1cSpriyanka 				ill = ipif->ipif_ill;
142943d18f1cSpriyanka 				mutex_enter(&ill->ill_lock);
143043d18f1cSpriyanka 				if ((ill->ill_state_flags & ILL_CONDEMNED) ||
143143d18f1cSpriyanka 				    (ipif->ipif_state_flags & IPIF_CONDEMNED)) {
143243d18f1cSpriyanka 					mutex_exit(&ill->ill_lock);
143343d18f1cSpriyanka 					ipif_refrele(ipif);
143443d18f1cSpriyanka 					retval =  EHOSTUNREACH;
143543d18f1cSpriyanka 					break;
143643d18f1cSpriyanka 				}
143743d18f1cSpriyanka 				mutex_exit(&ill->ill_lock);
143843d18f1cSpriyanka 				ipif_refrele(ipif);
143943d18f1cSpriyanka 				mutex_enter(&connp->conn_lock);
144043d18f1cSpriyanka 				connp->conn_nexthop_v4 = addr;
144143d18f1cSpriyanka 				connp->conn_nexthop_set = B_TRUE;
144243d18f1cSpriyanka 				mutex_exit(&connp->conn_lock);
144343d18f1cSpriyanka 			}
144443d18f1cSpriyanka 			break;
144543d18f1cSpriyanka 		}
14467c478bd9Sstevel@tonic-gate 		default:
14477c478bd9Sstevel@tonic-gate 			retval = EINVAL;
14487c478bd9Sstevel@tonic-gate 			break;
14497c478bd9Sstevel@tonic-gate 		}
14507c478bd9Sstevel@tonic-gate 		break;
14517c478bd9Sstevel@tonic-gate 	case IPPROTO_IPV6: {
14527c478bd9Sstevel@tonic-gate 		if (sctp->sctp_family != AF_INET6) {
14537c478bd9Sstevel@tonic-gate 			retval = ENOPROTOOPT;
14547c478bd9Sstevel@tonic-gate 			break;
14557c478bd9Sstevel@tonic-gate 		}
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 		switch (name) {
14587c478bd9Sstevel@tonic-gate 		case IPV6_UNICAST_HOPS:
14597c478bd9Sstevel@tonic-gate 			if (inlen < sizeof (int32_t)) {
14607c478bd9Sstevel@tonic-gate 				retval = EINVAL;
14617c478bd9Sstevel@tonic-gate 				break;
14627c478bd9Sstevel@tonic-gate 			}
14637c478bd9Sstevel@tonic-gate 			if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) {
14647c478bd9Sstevel@tonic-gate 				retval = EINVAL;
14657c478bd9Sstevel@tonic-gate 				break;
14667c478bd9Sstevel@tonic-gate 			}
1467b3d0fa4fSseb 			if (*i1 == -1) {
1468b3d0fa4fSseb 				ipp->ipp_unicast_hops = sctp_ipv6_hoplimit;
1469b3d0fa4fSseb 				ipp->ipp_fields &= ~IPPF_UNICAST_HOPS;
1470b3d0fa4fSseb 			} else {
1471b3d0fa4fSseb 				ipp->ipp_unicast_hops = (uint8_t)*i1;
1472b3d0fa4fSseb 				ipp->ipp_fields |= IPPF_UNICAST_HOPS;
1473b3d0fa4fSseb 			}
1474b3d0fa4fSseb 			retval = sctp_build_hdrs(sctp);
14757c478bd9Sstevel@tonic-gate 			break;
14767c478bd9Sstevel@tonic-gate 		case IPV6_UNSPEC_SRC:
14777c478bd9Sstevel@tonic-gate 			if (inlen < sizeof (int32_t)) {
14787c478bd9Sstevel@tonic-gate 				retval = EINVAL;
14797c478bd9Sstevel@tonic-gate 				break;
14807c478bd9Sstevel@tonic-gate 			}
14817c478bd9Sstevel@tonic-gate 			connp->conn_unspec_src = onoff;
14827c478bd9Sstevel@tonic-gate 			break;
14837c478bd9Sstevel@tonic-gate 		case IPV6_RECVPKTINFO:
14847c478bd9Sstevel@tonic-gate 			if (inlen < sizeof (int32_t)) {
14857c478bd9Sstevel@tonic-gate 				retval = EINVAL;
14867c478bd9Sstevel@tonic-gate 				break;
14877c478bd9Sstevel@tonic-gate 			}
14887c478bd9Sstevel@tonic-gate 			if (onoff)
14897c478bd9Sstevel@tonic-gate 				sctp->sctp_ipv6_recvancillary |=
14907c478bd9Sstevel@tonic-gate 				    SCTP_IPV6_RECVPKTINFO;
14917c478bd9Sstevel@tonic-gate 			else
14927c478bd9Sstevel@tonic-gate 				sctp->sctp_ipv6_recvancillary &=
14937c478bd9Sstevel@tonic-gate 				    ~SCTP_IPV6_RECVPKTINFO;
14947c478bd9Sstevel@tonic-gate 			/* Send it with the next msg */
14957c478bd9Sstevel@tonic-gate 			sctp->sctp_recvifindex = 0;
14967c478bd9Sstevel@tonic-gate 			connp->conn_ipv6_recvpktinfo = onoff;
14977c478bd9Sstevel@tonic-gate 			break;
14987c478bd9Sstevel@tonic-gate 		case IPV6_RECVHOPLIMIT:
14997c478bd9Sstevel@tonic-gate 			if (inlen < sizeof (int32_t)) {
15007c478bd9Sstevel@tonic-gate 				retval = EINVAL;
15017c478bd9Sstevel@tonic-gate 				break;
15027c478bd9Sstevel@tonic-gate 			}
15037c478bd9Sstevel@tonic-gate 			if (onoff)
15047c478bd9Sstevel@tonic-gate 				sctp->sctp_ipv6_recvancillary |=
15057c478bd9Sstevel@tonic-gate 				    SCTP_IPV6_RECVHOPLIMIT;
15067c478bd9Sstevel@tonic-gate 			else
15077c478bd9Sstevel@tonic-gate 				sctp->sctp_ipv6_recvancillary &=
15087c478bd9Sstevel@tonic-gate 				    ~SCTP_IPV6_RECVHOPLIMIT;
15097c478bd9Sstevel@tonic-gate 			sctp->sctp_recvhops = 0xffffffffU;
15107c478bd9Sstevel@tonic-gate 			connp->conn_ipv6_recvhoplimit = onoff;
15117c478bd9Sstevel@tonic-gate 			break;
15127c478bd9Sstevel@tonic-gate 		case IPV6_RECVHOPOPTS:
15137c478bd9Sstevel@tonic-gate 			if (inlen < sizeof (int32_t)) {
15147c478bd9Sstevel@tonic-gate 				retval = EINVAL;
15157c478bd9Sstevel@tonic-gate 				break;
15167c478bd9Sstevel@tonic-gate 			}
15177c478bd9Sstevel@tonic-gate 			if (onoff)
15187c478bd9Sstevel@tonic-gate 				sctp->sctp_ipv6_recvancillary |=
15197c478bd9Sstevel@tonic-gate 				    SCTP_IPV6_RECVHOPOPTS;
15207c478bd9Sstevel@tonic-gate 			else
15217c478bd9Sstevel@tonic-gate 				sctp->sctp_ipv6_recvancillary &=
15227c478bd9Sstevel@tonic-gate 				    ~SCTP_IPV6_RECVHOPOPTS;
15237c478bd9Sstevel@tonic-gate 			connp->conn_ipv6_recvhopopts = onoff;
15247c478bd9Sstevel@tonic-gate 			break;
15257c478bd9Sstevel@tonic-gate 		case IPV6_RECVDSTOPTS:
15267c478bd9Sstevel@tonic-gate 			if (inlen < sizeof (int32_t)) {
15277c478bd9Sstevel@tonic-gate 				retval = EINVAL;
15287c478bd9Sstevel@tonic-gate 				break;
15297c478bd9Sstevel@tonic-gate 			}
15307c478bd9Sstevel@tonic-gate 			if (onoff)
15317c478bd9Sstevel@tonic-gate 				sctp->sctp_ipv6_recvancillary |=
15327c478bd9Sstevel@tonic-gate 				    SCTP_IPV6_RECVDSTOPTS;
15337c478bd9Sstevel@tonic-gate 			else
15347c478bd9Sstevel@tonic-gate 				sctp->sctp_ipv6_recvancillary &=
15357c478bd9Sstevel@tonic-gate 				    ~SCTP_IPV6_RECVDSTOPTS;
15367c478bd9Sstevel@tonic-gate 			connp->conn_ipv6_recvdstopts = onoff;
15377c478bd9Sstevel@tonic-gate 			break;
15387c478bd9Sstevel@tonic-gate 		case IPV6_RECVRTHDR:
15397c478bd9Sstevel@tonic-gate 			if (inlen < sizeof (int32_t)) {
15407c478bd9Sstevel@tonic-gate 				retval = EINVAL;
15417c478bd9Sstevel@tonic-gate 				break;
15427c478bd9Sstevel@tonic-gate 			}
15437c478bd9Sstevel@tonic-gate 			if (onoff)
15447c478bd9Sstevel@tonic-gate 				sctp->sctp_ipv6_recvancillary |=
15457c478bd9Sstevel@tonic-gate 				    SCTP_IPV6_RECVRTHDR;
15467c478bd9Sstevel@tonic-gate 			else
15477c478bd9Sstevel@tonic-gate 				sctp->sctp_ipv6_recvancillary &=
15487c478bd9Sstevel@tonic-gate 				    ~SCTP_IPV6_RECVRTHDR;
15497c478bd9Sstevel@tonic-gate 			connp->conn_ipv6_recvrthdr = onoff;
15507c478bd9Sstevel@tonic-gate 			break;
15517c478bd9Sstevel@tonic-gate 		case IPV6_RECVRTHDRDSTOPTS:
15527c478bd9Sstevel@tonic-gate 			if (inlen < sizeof (int32_t)) {
15537c478bd9Sstevel@tonic-gate 				retval = EINVAL;
15547c478bd9Sstevel@tonic-gate 				break;
15557c478bd9Sstevel@tonic-gate 			}
15567c478bd9Sstevel@tonic-gate 			if (onoff)
15577c478bd9Sstevel@tonic-gate 				sctp->sctp_ipv6_recvancillary |=
15587c478bd9Sstevel@tonic-gate 				    SCTP_IPV6_RECVRTDSTOPTS;
15597c478bd9Sstevel@tonic-gate 			else
15607c478bd9Sstevel@tonic-gate 				sctp->sctp_ipv6_recvancillary &=
15617c478bd9Sstevel@tonic-gate 				    ~SCTP_IPV6_RECVRTDSTOPTS;
15627c478bd9Sstevel@tonic-gate 			connp->conn_ipv6_recvrtdstopts = onoff;
15637c478bd9Sstevel@tonic-gate 			break;
15647c478bd9Sstevel@tonic-gate 		case IPV6_PKTINFO:
15657c478bd9Sstevel@tonic-gate 			if (inlen != 0 &&
15667c478bd9Sstevel@tonic-gate 			    inlen != sizeof (struct in6_pktinfo)) {
15677c478bd9Sstevel@tonic-gate 				retval = EINVAL;
15687c478bd9Sstevel@tonic-gate 				break;
15697c478bd9Sstevel@tonic-gate 			}
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 			if (inlen == 0) {
15727c478bd9Sstevel@tonic-gate 				ipp->ipp_fields &= ~(IPPF_IFINDEX |IPPF_ADDR);
15737c478bd9Sstevel@tonic-gate 			} else  {
15747c478bd9Sstevel@tonic-gate 				struct in6_pktinfo *pkti;
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 				pkti = (struct in6_pktinfo *)invalp;
15777c478bd9Sstevel@tonic-gate 				/* XXX Need to check if the index exists */
15787c478bd9Sstevel@tonic-gate 				ipp->ipp_ifindex = pkti->ipi6_ifindex;
15797c478bd9Sstevel@tonic-gate 				ipp->ipp_addr = pkti->ipi6_addr;
15807c478bd9Sstevel@tonic-gate 				if (ipp->ipp_ifindex != 0)
15817c478bd9Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_IFINDEX;
15827c478bd9Sstevel@tonic-gate 				else
15837c478bd9Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_IFINDEX;
15847c478bd9Sstevel@tonic-gate 				if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr))
15857c478bd9Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_ADDR;
15867c478bd9Sstevel@tonic-gate 				else
15877c478bd9Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_ADDR;
15887c478bd9Sstevel@tonic-gate 			}
15897c478bd9Sstevel@tonic-gate 			retval = sctp_build_hdrs(sctp);
15907c478bd9Sstevel@tonic-gate 			break;
15917c478bd9Sstevel@tonic-gate 		case IPV6_NEXTHOP: {
15927c478bd9Sstevel@tonic-gate 			struct sockaddr_in6 *sin6;
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 			if (inlen != 0 && inlen != sizeof (sin6_t)) {
15957c478bd9Sstevel@tonic-gate 				retval = EINVAL;
15967c478bd9Sstevel@tonic-gate 				break;
15977c478bd9Sstevel@tonic-gate 			}
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 			if (inlen == 0) {
16007c478bd9Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_NEXTHOP;
16017c478bd9Sstevel@tonic-gate 			} else {
16027c478bd9Sstevel@tonic-gate 				sin6 = (struct sockaddr_in6 *)invalp;
16037c478bd9Sstevel@tonic-gate 				if (sin6->sin6_family != AF_INET6) {
16047c478bd9Sstevel@tonic-gate 					retval = EAFNOSUPPORT;
16057c478bd9Sstevel@tonic-gate 					break;
16067c478bd9Sstevel@tonic-gate 				}
16077c478bd9Sstevel@tonic-gate 				if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
16087c478bd9Sstevel@tonic-gate 					retval = EADDRNOTAVAIL;
16097c478bd9Sstevel@tonic-gate 					break;
16107c478bd9Sstevel@tonic-gate 				}
16117c478bd9Sstevel@tonic-gate 				ipp->ipp_nexthop = sin6->sin6_addr;
16127c478bd9Sstevel@tonic-gate 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
16137c478bd9Sstevel@tonic-gate 					ipp->ipp_fields &= ~IPPF_NEXTHOP;
16147c478bd9Sstevel@tonic-gate 				} else {
16157c478bd9Sstevel@tonic-gate 					ire_t	*ire;
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 					ire = ire_route_lookup_v6(
1618*45916cd2Sjpk 					    &sin6->sin6_addr, NULL, NULL, 0,
1619*45916cd2Sjpk 					    NULL, NULL, ALL_ZONES, NULL,
1620*45916cd2Sjpk 					    MATCH_IRE_DEFAULT);
16217c478bd9Sstevel@tonic-gate 					if (ire == NULL) {
16227c478bd9Sstevel@tonic-gate 						retval = EHOSTUNREACH;
16237c478bd9Sstevel@tonic-gate 						break;
16247c478bd9Sstevel@tonic-gate 					}
16257c478bd9Sstevel@tonic-gate 					ire_refrele(ire);
16267c478bd9Sstevel@tonic-gate 					ipp->ipp_fields |= IPPF_NEXTHOP;
16277c478bd9Sstevel@tonic-gate 				}
16287c478bd9Sstevel@tonic-gate 			}
16297c478bd9Sstevel@tonic-gate 			retval = sctp_build_hdrs(sctp);
16307c478bd9Sstevel@tonic-gate 			break;
16317c478bd9Sstevel@tonic-gate 		}
16327c478bd9Sstevel@tonic-gate 		case IPV6_HOPOPTS: {
16337c478bd9Sstevel@tonic-gate 			ip6_hbh_t *hopts = (ip6_hbh_t *)invalp;
16347c478bd9Sstevel@tonic-gate 
16357c478bd9Sstevel@tonic-gate 			if (inlen != 0 &&
16367c478bd9Sstevel@tonic-gate 			    inlen != (8 * (hopts->ip6h_len + 1))) {
16377c478bd9Sstevel@tonic-gate 				retval = EINVAL;
16387c478bd9Sstevel@tonic-gate 				break;
16397c478bd9Sstevel@tonic-gate 			}
16407c478bd9Sstevel@tonic-gate 
1641*45916cd2Sjpk 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1642*45916cd2Sjpk 			    B_TRUE, (uchar_t **)&ipp->ipp_hopopts,
1643*45916cd2Sjpk 			    &ipp->ipp_hopoptslen, sctp->sctp_v6label_len);
1644*45916cd2Sjpk 			if (retval != 0)
1645*45916cd2Sjpk 				break;
1646*45916cd2Sjpk 			if (ipp->ipp_hopoptslen == 0)
16477c478bd9Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_HOPOPTS;
1648*45916cd2Sjpk 			else
16497c478bd9Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_HOPOPTS;
16507c478bd9Sstevel@tonic-gate 			retval = sctp_build_hdrs(sctp);
16517c478bd9Sstevel@tonic-gate 			break;
16527c478bd9Sstevel@tonic-gate 		}
16537c478bd9Sstevel@tonic-gate 		case IPV6_RTHDRDSTOPTS: {
16547c478bd9Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
16557c478bd9Sstevel@tonic-gate 
16567c478bd9Sstevel@tonic-gate 			if (inlen != 0 &&
16577c478bd9Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1))) {
16587c478bd9Sstevel@tonic-gate 				retval = EINVAL;
16597c478bd9Sstevel@tonic-gate 				break;
16607c478bd9Sstevel@tonic-gate 			}
16617c478bd9Sstevel@tonic-gate 
1662*45916cd2Sjpk 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1663*45916cd2Sjpk 			    B_TRUE, (uchar_t **)&ipp->ipp_rtdstopts,
1664*45916cd2Sjpk 			    &ipp->ipp_rtdstoptslen, 0);
1665*45916cd2Sjpk 			if (retval != 0)
1666*45916cd2Sjpk 				break;
1667*45916cd2Sjpk 			if (ipp->ipp_rtdstoptslen == 0)
16687c478bd9Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTDSTOPTS;
1669*45916cd2Sjpk 			else
16707c478bd9Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTDSTOPTS;
16717c478bd9Sstevel@tonic-gate 			retval = sctp_build_hdrs(sctp);
16727c478bd9Sstevel@tonic-gate 			break;
16737c478bd9Sstevel@tonic-gate 		}
16747c478bd9Sstevel@tonic-gate 		case IPV6_DSTOPTS: {
16757c478bd9Sstevel@tonic-gate 			ip6_dest_t *dopts = (ip6_dest_t *)invalp;
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate 			if (inlen != 0 &&
16787c478bd9Sstevel@tonic-gate 			    inlen != (8 * (dopts->ip6d_len + 1))) {
16797c478bd9Sstevel@tonic-gate 				retval = EINVAL;
16807c478bd9Sstevel@tonic-gate 				break;
16817c478bd9Sstevel@tonic-gate 			}
16827c478bd9Sstevel@tonic-gate 
1683*45916cd2Sjpk 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1684*45916cd2Sjpk 			    B_TRUE, (uchar_t **)&ipp->ipp_dstopts,
1685*45916cd2Sjpk 			    &ipp->ipp_dstoptslen, 0);
1686*45916cd2Sjpk 			if (retval != 0)
1687*45916cd2Sjpk 				break;
1688*45916cd2Sjpk 			if (ipp->ipp_dstoptslen == 0)
16897c478bd9Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_DSTOPTS;
1690*45916cd2Sjpk 			else
16917c478bd9Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_DSTOPTS;
16927c478bd9Sstevel@tonic-gate 			retval = sctp_build_hdrs(sctp);
16937c478bd9Sstevel@tonic-gate 			break;
16947c478bd9Sstevel@tonic-gate 		}
16957c478bd9Sstevel@tonic-gate 		case IPV6_RTHDR: {
16967c478bd9Sstevel@tonic-gate 			ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp;
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 			if (inlen != 0 &&
16997c478bd9Sstevel@tonic-gate 			    inlen != (8 * (rt->ip6r_len + 1))) {
17007c478bd9Sstevel@tonic-gate 				retval = EINVAL;
17017c478bd9Sstevel@tonic-gate 				break;
17027c478bd9Sstevel@tonic-gate 			}
17037c478bd9Sstevel@tonic-gate 
1704*45916cd2Sjpk 			retval = optcom_pkt_set((uchar_t *)invalp, inlen,
1705*45916cd2Sjpk 			    B_TRUE, (uchar_t **)&ipp->ipp_rthdr,
1706*45916cd2Sjpk 			    &ipp->ipp_rthdrlen, 0);
1707*45916cd2Sjpk 			if (retval != 0)
1708*45916cd2Sjpk 				break;
1709*45916cd2Sjpk 			if (ipp->ipp_rthdrlen == 0)
17107c478bd9Sstevel@tonic-gate 				ipp->ipp_fields &= ~IPPF_RTHDR;
1711*45916cd2Sjpk 			else
17127c478bd9Sstevel@tonic-gate 				ipp->ipp_fields |= IPPF_RTHDR;
17137c478bd9Sstevel@tonic-gate 			retval = sctp_build_hdrs(sctp);
17147c478bd9Sstevel@tonic-gate 			break;
17157c478bd9Sstevel@tonic-gate 		}
17167c478bd9Sstevel@tonic-gate 		case IPV6_SEC_OPT:
17177c478bd9Sstevel@tonic-gate 			/*
17187c478bd9Sstevel@tonic-gate 			 * We should not allow policy setting after
17197c478bd9Sstevel@tonic-gate 			 * we start listening for connections.
17207c478bd9Sstevel@tonic-gate 			 */
17217c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state >= SCTPS_LISTEN) {
17227c478bd9Sstevel@tonic-gate 				retval = EINVAL;
17237c478bd9Sstevel@tonic-gate 			} else {
17247c478bd9Sstevel@tonic-gate 				retval = ipsec_set_req(sctp->sctp_credp,
17257c478bd9Sstevel@tonic-gate 				    sctp->sctp_connp, (ipsec_req_t *)invalp);
17267c478bd9Sstevel@tonic-gate 			}
17277c478bd9Sstevel@tonic-gate 			break;
17287c478bd9Sstevel@tonic-gate 		case IPV6_V6ONLY:
17297c478bd9Sstevel@tonic-gate 			/*
17307c478bd9Sstevel@tonic-gate 			 * After the bound state, setting the v6only option
17317c478bd9Sstevel@tonic-gate 			 * is too late.
17327c478bd9Sstevel@tonic-gate 			 */
17337c478bd9Sstevel@tonic-gate 			if (sctp->sctp_state >= SCTPS_BOUND) {
17347c478bd9Sstevel@tonic-gate 				retval = EINVAL;
17357c478bd9Sstevel@tonic-gate 			} else {
17367c478bd9Sstevel@tonic-gate 				sctp->sctp_connp->conn_ipv6_v6only = onoff;
17377c478bd9Sstevel@tonic-gate 			}
17387c478bd9Sstevel@tonic-gate 			break;
17397c478bd9Sstevel@tonic-gate 		default:
17407c478bd9Sstevel@tonic-gate 			retval = EINVAL;
17417c478bd9Sstevel@tonic-gate 			break;
17427c478bd9Sstevel@tonic-gate 		}
17437c478bd9Sstevel@tonic-gate 		break;
17447c478bd9Sstevel@tonic-gate 	}
17457c478bd9Sstevel@tonic-gate 	default:
17467c478bd9Sstevel@tonic-gate 		retval = EINVAL;
17477c478bd9Sstevel@tonic-gate 		break;
17487c478bd9Sstevel@tonic-gate 	}
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
17517c478bd9Sstevel@tonic-gate 	return (retval);
17527c478bd9Sstevel@tonic-gate }
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate /*
17557c478bd9Sstevel@tonic-gate  * SCTP exported kernel interface for geting the first source address of
17567c478bd9Sstevel@tonic-gate  * a sctp_t.  The parameter addr is assumed to have enough space to hold
17577c478bd9Sstevel@tonic-gate  * one socket address.
17587c478bd9Sstevel@tonic-gate  */
17597c478bd9Sstevel@tonic-gate int
17607c478bd9Sstevel@tonic-gate sctp_getsockname(sctp_t *sctp, struct sockaddr *addr, socklen_t *addrlen)
17617c478bd9Sstevel@tonic-gate {
17627c478bd9Sstevel@tonic-gate 	int	err = 0;
17637c478bd9Sstevel@tonic-gate 	int	addrcnt = 1;
17647c478bd9Sstevel@tonic-gate 	sin_t	*sin4;
17657c478bd9Sstevel@tonic-gate 	sin6_t	*sin6;
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	ASSERT(sctp != NULL);
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
17707c478bd9Sstevel@tonic-gate 	addr->sa_family = sctp->sctp_family;
17717c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_family) {
17727c478bd9Sstevel@tonic-gate 	case AF_INET:
17737c478bd9Sstevel@tonic-gate 		sin4 = (sin_t *)addr;
17747c478bd9Sstevel@tonic-gate 		if ((sctp->sctp_state <= SCTPS_LISTEN) &&
17757c478bd9Sstevel@tonic-gate 		    sctp->sctp_bound_to_all) {
17767c478bd9Sstevel@tonic-gate 			sin4->sin_addr.s_addr = INADDR_ANY;
17777c478bd9Sstevel@tonic-gate 			sin4->sin_port = sctp->sctp_lport;
17787c478bd9Sstevel@tonic-gate 		} else {
17797c478bd9Sstevel@tonic-gate 			err = sctp_getmyaddrs(sctp, sin4, &addrcnt);
17807c478bd9Sstevel@tonic-gate 			if (err != 0) {
17817c478bd9Sstevel@tonic-gate 				*addrlen = 0;
17827c478bd9Sstevel@tonic-gate 				break;
17837c478bd9Sstevel@tonic-gate 			}
17847c478bd9Sstevel@tonic-gate 		}
17857c478bd9Sstevel@tonic-gate 		*addrlen = sizeof (struct sockaddr_in);
17867c478bd9Sstevel@tonic-gate 		break;
17877c478bd9Sstevel@tonic-gate 	case AF_INET6:
17887c478bd9Sstevel@tonic-gate 		sin6 = (sin6_t *)addr;
17897c478bd9Sstevel@tonic-gate 		if ((sctp->sctp_state <= SCTPS_LISTEN) &&
17907c478bd9Sstevel@tonic-gate 		    sctp->sctp_bound_to_all) {
17917c478bd9Sstevel@tonic-gate 			bzero(&sin6->sin6_addr, sizeof (sin6->sin6_addr));
17927c478bd9Sstevel@tonic-gate 			sin6->sin6_port = sctp->sctp_lport;
17937c478bd9Sstevel@tonic-gate 		} else {
17947c478bd9Sstevel@tonic-gate 			err = sctp_getmyaddrs(sctp, sin6, &addrcnt);
17957c478bd9Sstevel@tonic-gate 			if (err != 0) {
17967c478bd9Sstevel@tonic-gate 				*addrlen = 0;
17977c478bd9Sstevel@tonic-gate 				break;
17987c478bd9Sstevel@tonic-gate 			}
17997c478bd9Sstevel@tonic-gate 		}
18007c478bd9Sstevel@tonic-gate 		*addrlen = sizeof (struct sockaddr_in6);
18017c478bd9Sstevel@tonic-gate 		sin6->sin6_flowinfo = sctp->sctp_ip6h->ip6_vcf &
18027c478bd9Sstevel@tonic-gate 		    ~IPV6_VERS_AND_FLOW_MASK;
18037c478bd9Sstevel@tonic-gate 		sin6->sin6_scope_id = 0;
18047c478bd9Sstevel@tonic-gate 		sin6->__sin6_src_id = 0;
18057c478bd9Sstevel@tonic-gate 		break;
18067c478bd9Sstevel@tonic-gate 	}
18077c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
18087c478bd9Sstevel@tonic-gate 	return (err);
18097c478bd9Sstevel@tonic-gate }
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate /*
18127c478bd9Sstevel@tonic-gate  * SCTP exported kernel interface for geting the primary peer address of
18137c478bd9Sstevel@tonic-gate  * a sctp_t.  The parameter addr is assumed to have enough space to hold
18147c478bd9Sstevel@tonic-gate  * one socket address.
18157c478bd9Sstevel@tonic-gate  */
18167c478bd9Sstevel@tonic-gate int
18177c478bd9Sstevel@tonic-gate sctp_getpeername(sctp_t *sctp, struct sockaddr *addr, socklen_t *addrlen)
18187c478bd9Sstevel@tonic-gate {
18197c478bd9Sstevel@tonic-gate 	int	err = 0;
18207c478bd9Sstevel@tonic-gate 	int	addrcnt = 1;
18217c478bd9Sstevel@tonic-gate 	sin6_t	*sin6;
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	ASSERT(sctp != NULL);
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	RUN_SCTP(sctp);
18267c478bd9Sstevel@tonic-gate 	addr->sa_family = sctp->sctp_family;
18277c478bd9Sstevel@tonic-gate 	switch (sctp->sctp_family) {
18287c478bd9Sstevel@tonic-gate 	case AF_INET:
18297c478bd9Sstevel@tonic-gate 		err = sctp_getpeeraddrs(sctp, addr, &addrcnt);
18307c478bd9Sstevel@tonic-gate 		if (err != 0) {
18317c478bd9Sstevel@tonic-gate 			*addrlen = 0;
18327c478bd9Sstevel@tonic-gate 			break;
18337c478bd9Sstevel@tonic-gate 		}
18347c478bd9Sstevel@tonic-gate 		*addrlen = sizeof (struct sockaddr_in);
18357c478bd9Sstevel@tonic-gate 		break;
18367c478bd9Sstevel@tonic-gate 	case AF_INET6:
18377c478bd9Sstevel@tonic-gate 		sin6 = (sin6_t *)addr;
18387c478bd9Sstevel@tonic-gate 		err = sctp_getpeeraddrs(sctp, sin6, &addrcnt);
18397c478bd9Sstevel@tonic-gate 		if (err != 0) {
18407c478bd9Sstevel@tonic-gate 			*addrlen = 0;
18417c478bd9Sstevel@tonic-gate 			break;
18427c478bd9Sstevel@tonic-gate 		}
18437c478bd9Sstevel@tonic-gate 		*addrlen = sizeof (struct sockaddr_in6);
18447c478bd9Sstevel@tonic-gate 		sin6->sin6_flowinfo = 0;
18457c478bd9Sstevel@tonic-gate 		sin6->sin6_scope_id = 0;
18467c478bd9Sstevel@tonic-gate 		sin6->__sin6_src_id = 0;
18477c478bd9Sstevel@tonic-gate 		break;
18487c478bd9Sstevel@tonic-gate 	}
18497c478bd9Sstevel@tonic-gate 	WAKE_SCTP(sctp);
18507c478bd9Sstevel@tonic-gate 	return (err);
18517c478bd9Sstevel@tonic-gate }
18527c478bd9Sstevel@tonic-gate 
18537c478bd9Sstevel@tonic-gate /*
18547c478bd9Sstevel@tonic-gate  * Return a list of IP addresses of the peer endpoint of this sctp_t.
18557c478bd9Sstevel@tonic-gate  * The parameter paddrs is supposed to be either (struct sockaddr_in *) or
18567c478bd9Sstevel@tonic-gate  * (struct sockaddr_in6 *) depending on the address family of the sctp_t.
18577c478bd9Sstevel@tonic-gate  */
18587c478bd9Sstevel@tonic-gate int
18597c478bd9Sstevel@tonic-gate sctp_getpeeraddrs(sctp_t *sctp, void *paddrs, int *addrcnt)
18607c478bd9Sstevel@tonic-gate {
18617c478bd9Sstevel@tonic-gate 	int			family;
18627c478bd9Sstevel@tonic-gate 	struct sockaddr_in	*sin4;
18637c478bd9Sstevel@tonic-gate 	struct sockaddr_in6	*sin6;
18647c478bd9Sstevel@tonic-gate 	int			max;
18657c478bd9Sstevel@tonic-gate 	int			cnt;
18667c478bd9Sstevel@tonic-gate 	sctp_faddr_t		*fp = sctp->sctp_faddrs;
18677c478bd9Sstevel@tonic-gate 	in6_addr_t		addr;
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 	ASSERT(sctp != NULL);
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate 	if (sctp->sctp_faddrs == NULL)
18727c478bd9Sstevel@tonic-gate 		return (ENOTCONN);
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 	family = sctp->sctp_family;
18757c478bd9Sstevel@tonic-gate 	max = *addrcnt;
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate 	/* If we want only one, give the primary */
18787c478bd9Sstevel@tonic-gate 	if (max == 1) {
18797c478bd9Sstevel@tonic-gate 		addr = sctp->sctp_primary->faddr;
18807c478bd9Sstevel@tonic-gate 		switch (family) {
18817c478bd9Sstevel@tonic-gate 		case AF_INET:
18827c478bd9Sstevel@tonic-gate 			sin4 = paddrs;
18837c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
18847c478bd9Sstevel@tonic-gate 			sin4->sin_port = sctp->sctp_fport;
18857c478bd9Sstevel@tonic-gate 			sin4->sin_family = AF_INET;
18867c478bd9Sstevel@tonic-gate 			break;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 		case AF_INET6:
18897c478bd9Sstevel@tonic-gate 			sin6 = paddrs;
18907c478bd9Sstevel@tonic-gate 			sin6->sin6_addr = addr;
18917c478bd9Sstevel@tonic-gate 			sin6->sin6_port = sctp->sctp_fport;
18927c478bd9Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
18937c478bd9Sstevel@tonic-gate 			break;
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate 		return (0);
18967c478bd9Sstevel@tonic-gate 	}
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate 	for (cnt = 0; cnt < max && fp != NULL; cnt++, fp = fp->next) {
18997c478bd9Sstevel@tonic-gate 		addr = fp->faddr;
19007c478bd9Sstevel@tonic-gate 		switch (family) {
19017c478bd9Sstevel@tonic-gate 		case AF_INET:
19027c478bd9Sstevel@tonic-gate 			ASSERT(IN6_IS_ADDR_V4MAPPED(&addr));
19037c478bd9Sstevel@tonic-gate 			sin4 = (struct sockaddr_in *)paddrs + cnt;
19047c478bd9Sstevel@tonic-gate 			IN6_V4MAPPED_TO_INADDR(&addr, &sin4->sin_addr);
19057c478bd9Sstevel@tonic-gate 			sin4->sin_port = sctp->sctp_fport;
19067c478bd9Sstevel@tonic-gate 			sin4->sin_family = AF_INET;
19077c478bd9Sstevel@tonic-gate 			break;
19087c478bd9Sstevel@tonic-gate 		case AF_INET6:
19097c478bd9Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)paddrs + cnt;
19107c478bd9Sstevel@tonic-gate 			sin6->sin6_addr = addr;
19117c478bd9Sstevel@tonic-gate 			sin6->sin6_port = sctp->sctp_fport;
19127c478bd9Sstevel@tonic-gate 			sin6->sin6_family = AF_INET6;
19137c478bd9Sstevel@tonic-gate 			break;
19147c478bd9Sstevel@tonic-gate 		}
19157c478bd9Sstevel@tonic-gate 	}
19167c478bd9Sstevel@tonic-gate 	*addrcnt = cnt;
19177c478bd9Sstevel@tonic-gate 	return (0);
19187c478bd9Sstevel@tonic-gate }
1919