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